1
0
mirror of https://github.com/nlohmann/json synced 2024-12-24 17:41:05 +00:00

Do proper endian conversions

This fixes all testsuite failures on big endian hosts.
This commit is contained in:
Andreas Schwab 2019-02-21 11:55:21 +01:00
parent e326df211b
commit bb22b1003f
4 changed files with 6 additions and 7 deletions

View File

@ -1853,7 +1853,7 @@ class binary_reader
}
// reverse byte order prior to conversion if necessary
if (is_little_endian && !InputIsLittleEndian)
if (is_little_endian != InputIsLittleEndian)
{
vec[sizeof(NumberType) - i - 1] = static_cast<uint8_t>(current);
}

View File

@ -1278,7 +1278,7 @@ class binary_writer
std::memcpy(vec.data(), &n, sizeof(NumberType));
// step 2: write array to output (with possible reordering)
if (is_little_endian and not OutputIsLittleEndian)
if (is_little_endian != OutputIsLittleEndian)
{
// reverse byte order prior to conversion if necessary
std::reverse(vec.begin(), vec.end());

View File

@ -8333,7 +8333,7 @@ class binary_reader
}
// reverse byte order prior to conversion if necessary
if (is_little_endian && !InputIsLittleEndian)
if (is_little_endian != InputIsLittleEndian)
{
vec[sizeof(NumberType) - i - 1] = static_cast<uint8_t>(current);
}
@ -9746,7 +9746,7 @@ class binary_writer
std::memcpy(vec.data(), &n, sizeof(NumberType));
// step 2: write array to output (with possible reordering)
if (is_little_endian and not OutputIsLittleEndian)
if (is_little_endian != OutputIsLittleEndian)
{
// reverse byte order prior to conversion if necessary
std::reverse(vec.begin(), vec.end());

View File

@ -599,9 +599,8 @@ struct pod_serializer
static void to_json(BasicJsonType& j, const T& t) noexcept
{
auto bytes = static_cast< const unsigned char*>(static_cast<const void*>(&t));
std::uint64_t value = bytes[0];
for (auto i = 1; i < 8; ++i)
value |= std::uint64_t{bytes[i]} << 8 * i;
std::uint64_t value;
std::memcpy(&value, bytes, sizeof(value));
nlohmann::to_json(j, value);
}
};