Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do proper endian conversions #1489

Merged
merged 1 commit into from
Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/nlohmann/detail/input/binary_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
nlohmann marked this conversation as resolved.
Show resolved Hide resolved
{
vec[sizeof(NumberType) - i - 1] = static_cast<uint8_t>(current);
}
Expand Down
2 changes: 1 addition & 1 deletion include/nlohmann/detail/output/binary_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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());
Expand Down
5 changes: 2 additions & 3 deletions test/src/unit-udt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
andreas-schwab marked this conversation as resolved.
Show resolved Hide resolved
nlohmann::to_json(j, value);
}
};
Expand Down