diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index cbe515855..8ac969c1b 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -3509,7 +3509,7 @@ class binary_reader } const bool negative = second >= 0xC0; - std::int64_t value = static_cast(negative ? (second & 0x3F) : second); + auto value = static_cast(negative ? (second & 0x3F) : second); std::int64_t offset = 0; int extra_bytes = 0; diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp index 9f6d0167f..d06e0480d 100644 --- a/include/nlohmann/detail/output/binary_writer.hpp +++ b/include/nlohmann/detail/output/binary_writer.hpp @@ -1454,6 +1454,21 @@ class binary_writer return to_char_type(std::is_same::value ? 0x8E : 0x8F); } + /// @return the type marker for a FloatType value in @a format (CBOR, MessagePack, or BON8) + template + static CharType get_compact_float_prefix(const detail::input_format_t format) + { + if (format == detail::input_format_t::cbor) + { + return get_cbor_float_prefix(FloatType{}); + } + if (format == detail::input_format_t::bon8) + { + return get_bon8_float_prefix(); + } + return get_msgpack_float_prefix(FloatType{}); + } + //////////// // UBJSON // //////////// @@ -2231,6 +2246,7 @@ class binary_writer */ static void check_bon8_utf8(const string_t& s, const BasicJsonType& context) { + static_cast(context); // only used when exceptions are enabled const auto* data = reinterpret_cast(s.data()); for (std::size_t i = 0; i < s.size();) { @@ -2346,15 +2362,15 @@ class binary_writer JSON_HEDLEY_DIAGNOSTIC_PUSH JSON_HEDLEY_PRAGMA(GCC diagnostic ignored "-Wfloat-equal") #endif - if (n == -1.0) + if (n == static_cast(-1)) { oa.write_character(to_char_type(0xFB)); } - else if (n == 0.0 && !std::signbit(n)) + else if (n == static_cast(0) && !std::signbit(n)) { oa.write_character(to_char_type(0xFC)); } - else if (n == 1.0) + else if (n == static_cast(1)) { oa.write_character(to_char_type(0xFD)); } @@ -2505,20 +2521,12 @@ class binary_writer static_cast(n) <= static_cast((std::numeric_limits::max)()) && static_cast(static_cast(n)) == static_cast(n)))) { - oa.write_character(format == detail::input_format_t::cbor - ? get_cbor_float_prefix(static_cast(n)) - : format == detail::input_format_t::bon8 - ? get_bon8_float_prefix() - : get_msgpack_float_prefix(static_cast(n))); + oa.write_character(get_compact_float_prefix(format)); write_number(static_cast(n)); } else { - oa.write_character(format == detail::input_format_t::cbor - ? get_cbor_float_prefix(n) - : format == detail::input_format_t::bon8 - ? get_bon8_float_prefix() - : get_msgpack_float_prefix(n)); + oa.write_character(get_compact_float_prefix(format)); write_number(n); } #ifdef __GNUC__ diff --git a/tests/src/unit-bon8.cpp b/tests/src/unit-bon8.cpp index 4040ad31d..f54a7ff6b 100644 --- a/tests/src/unit-bon8.cpp +++ b/tests/src/unit-bon8.cpp @@ -182,8 +182,8 @@ TEST_CASE("BON8") check_bon8(-264075, {0xF0, 0xC0, 0x00, 0x00}); check_bon8(-33818506, {0xF7, 0xFF, 0xFF, 0xFF}); check_bon8(-33818507, {0x8C, 0xFD, 0xFB, 0xF8, 0x75}); - check_bon8(-2147483648, {0x8C, 0x80, 0x00, 0x00, 0x00}); - check_bon8(-2147483649, {0x8D, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF}); + check_bon8(-2147483648LL, {0x8C, 0x80, 0x00, 0x00, 0x00}); + check_bon8(-2147483649LL, {0x8D, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF}); check_bon8((std::numeric_limits::min)(), {0x8D, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); }