diff --git a/docs/mkdocs/docs/home/exceptions.md b/docs/mkdocs/docs/home/exceptions.md index 21f1d0c7a7..1b8d7ef83f 100644 --- a/docs/mkdocs/docs/home/exceptions.md +++ b/docs/mkdocs/docs/home/exceptions.md @@ -839,7 +839,7 @@ A parsed number could not be stored as without changing it to NaN or INF. ### json.exception.out_of_range.407 -UBJSON only support integer numbers up to 9223372036854775807. +UBJSON only supports integer numbers up to 9223372036854775807. !!! failure "Example message" diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index e3885f2c03..21b40f8996 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -322,17 +322,18 @@ class binary_reader return get_number(input_format_t::bson, value) && sax->number_integer(value); } + case 0x11: // uint64 + { + std::uint64_t value{}; + return get_number(input_format_t::bson, value) && sax->number_unsigned(value); + } + case 0x12: // int64 { std::int64_t value{}; return get_number(input_format_t::bson, value) && sax->number_integer(value); } - case 0x11: // uint64 - { - std::uint64_t value{}; - return get_number(input_format_t::bson, value) && sax->number_unsigned(value); - } default: // anything else not supported (yet) { diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp index f1a930996f..6dff77a8cd 100644 --- a/include/nlohmann/detail/output/binary_writer.hpp +++ b/include/nlohmann/detail/output/binary_writer.hpp @@ -1076,7 +1076,7 @@ class binary_writer { return (value <= static_cast((std::numeric_limits::max)())) ? sizeof(std::int32_t) - : sizeof(std::int64_t); + : sizeof(std::uint64_t); } /*! @@ -1090,11 +1090,6 @@ class binary_writer write_bson_entry_header(name, 0x10 /* int32 */); write_number(static_cast(j.m_data.m_value.number_unsigned), true); } - else if (j.m_data.m_value.number_unsigned <= static_cast((std::numeric_limits::max)())) - { - write_bson_entry_header(name, 0x12 /* int64 */); - write_number(static_cast(j.m_data.m_value.number_unsigned), true); - } else { write_bson_entry_header(name, 0x11 /* uint64 */); diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 14a16cefa8..e000f94a28 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -10000,17 +10000,18 @@ class binary_reader return get_number(input_format_t::bson, value) && sax->number_integer(value); } + case 0x11: // uint64 + { + std::uint64_t value{}; + return get_number(input_format_t::bson, value) && sax->number_unsigned(value); + } + case 0x12: // int64 { std::int64_t value{}; return get_number(input_format_t::bson, value) && sax->number_integer(value); } - case 0x11: // uint64 - { - std::uint64_t value{}; - return get_number(input_format_t::bson, value) && sax->number_unsigned(value); - } default: // anything else not supported (yet) { @@ -16718,7 +16719,7 @@ class binary_writer { return (value <= static_cast((std::numeric_limits::max)())) ? sizeof(std::int32_t) - : sizeof(std::int64_t); + : sizeof(std::uint64_t); } /*! @@ -16732,11 +16733,6 @@ class binary_writer write_bson_entry_header(name, 0x10 /* int32 */); write_number(static_cast(j.m_data.m_value.number_unsigned), true); } - else if (j.m_data.m_value.number_unsigned <= static_cast((std::numeric_limits::max)())) - { - write_bson_entry_header(name, 0x12 /* int64 */); - write_number(static_cast(j.m_data.m_value.number_unsigned), true); - } else { write_bson_entry_header(name, 0x11 /* uint64 */); diff --git a/tests/src/unit-bson.cpp b/tests/src/unit-bson.cpp index e35c338960..970ad509d7 100644 --- a/tests/src/unit-bson.cpp +++ b/tests/src/unit-bson.cpp @@ -339,7 +339,7 @@ TEST_CASE("BSON") std::vector const expected = { 0x14, 0x00, 0x00, 0x00, // size (little endian) - 0x12, /// entry: int64 + 0x11, /// entry: uint64 'e', 'n', 't', 'r', 'y', '\x00', 0x01, 0x02, 0x03, 0x04, 0x78, 0x56, 0x34, 0x12, 0x00 // end marker @@ -1132,7 +1132,7 @@ TEST_CASE("BSON numerical data") std::vector const expected_bson = { 0x14u, 0x00u, 0x00u, 0x00u, // size (little endian) - 0x12u, /// entry: int64 + 0x11u, /// entry: uint64 'e', 'n', 't', 'r', 'y', '\x00', static_cast((iu >> (8u * 0u)) & 0xffu), static_cast((iu >> (8u * 1u)) & 0xffu),