diff --git a/docs/mkdocs/docs/features/binary_formats/bson.md b/docs/mkdocs/docs/features/binary_formats/bson.md index a162672631..d3e4d5e35c 100644 --- a/docs/mkdocs/docs/features/binary_formats/bson.md +++ b/docs/mkdocs/docs/features/binary_formats/bson.md @@ -8,7 +8,7 @@ representation of data types that are not part of the JSON spec. For example, BS - [BSON Website](http://bsonspec.org) - the main source on BSON - [BSON Specification](http://bsonspec.org/spec.html) - the specification - + ## Serialization @@ -42,7 +42,7 @@ The library uses the following mapping from JSON values types to BSON types: ```cpp --8<-- "examples/to_bson.cpp" ``` - + Output: ```c @@ -93,3 +93,8 @@ The library maps BSON record types to JSON value types as follows: ```json --8<-- "examples/from_bson.output" ``` + +!!! note "Handling of BSON type 0x11" + + BSON type 0x11 is used to represent uint64 numbers. This library treats these values purely as uint64 numbers + and does not parse them into date-related formats. diff --git a/docs/mkdocs/docs/home/exceptions.md b/docs/mkdocs/docs/home/exceptions.md index 1b8d7ef83f..5f14770c7c 100644 --- a/docs/mkdocs/docs/home/exceptions.md +++ b/docs/mkdocs/docs/home/exceptions.md @@ -47,7 +47,7 @@ Note that [`JSON_THROW_USER`](../api/macros/json_throw_user.md) should leave the ```cpp #include - + #define JSON_TRY_USER if(true) #define JSON_CATCH_USER(exception) if(false) #define JSON_THROW_USER(exception) \ @@ -55,7 +55,7 @@ Note that [`JSON_THROW_USER`](../api/macros/json_throw_user.md) should leave the << " (function " << __FUNCTION__ << ") - " \ << (exception).what() << std::endl; \ std::abort();} - + #include ``` @@ -72,7 +72,7 @@ Exceptions in the library are thrown in the local context of the JSON value they ```cpp --8<-- "examples/diagnostics_standard.cpp" ``` - + Output: ``` @@ -90,7 +90,7 @@ As this global context comes at the price of storing one additional pointer per ```cpp --8<-- "examples/diagnostics_extended.cpp" ``` - + Output: ``` @@ -125,7 +125,7 @@ Exceptions have ids 1xx. ```cpp --8<-- "examples/parse_error.cpp" ``` - + Output: ``` @@ -377,7 +377,7 @@ Exceptions have ids 2xx. ```cpp --8<-- "examples/invalid_iterator.cpp" ``` - + Output: ``` @@ -541,7 +541,7 @@ Exceptions have ids 3xx. ```cpp --8<-- "examples/type_error.cpp" ``` - + Output: ``` @@ -735,7 +735,7 @@ The `dump()` function only works with UTF-8 encoded strings; that is, if you ass - Store the source file with UTF-8 encoding. - Pass an error handler as last parameter to the `dump()` function to avoid this exception: - - `json::error_handler_t::replace` will replace invalid bytes sequences with `U+FFFD` + - `json::error_handler_t::replace` will replace invalid bytes sequences with `U+FFFD` - `json::error_handler_t::ignore` will silently ignore invalid byte sequences ### json.exception.type_error.317 @@ -770,7 +770,7 @@ Exceptions have ids 4xx. ```cpp --8<-- "examples/out_of_range.cpp" ``` - + Output: ``` @@ -849,7 +849,7 @@ UBJSON only supports integer numbers up to 9223372036854775807. !!! note - Since version 3.9.0, integer numbers beyond int64 are serialized as high-precision UBJSON numbers, and this exception does not further occur. + Since version 3.9.0, integer numbers beyond int64 are serialized as high-precision UBJSON numbers, and this exception does not further occur. ### json.exception.out_of_range.408 @@ -885,7 +885,7 @@ Exceptions have ids 5xx. ```cpp --8<-- "examples/other_error.cpp" ``` - + Output: ``` diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index 27328d600c..02cf27419f 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -334,6 +334,12 @@ 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); + } + default: // anything else not supported (yet) { std::array cr{{}}; diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 6ca7646e9e..25d4a44929 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -10012,6 +10012,12 @@ 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); + } + default: // anything else not supported (yet) { std::array cr{{}}; diff --git a/tests/src/unit-bson.cpp b/tests/src/unit-bson.cpp index ac26298af8..970ad509d7 100644 --- a/tests/src/unit-bson.cpp +++ b/tests/src/unit-bson.cpp @@ -530,7 +530,6 @@ TEST_CASE("BSON") SECTION("Some more complex document") { - // directly encoding uint64 is not supported in bson (only for timestamp values) json const j = { {"double", 42.5}, @@ -1163,10 +1162,7 @@ TEST_CASE("BSON numerical data") std::vector const numbers { static_cast((std::numeric_limits::max)()) + 1ULL, - 10000000000000000000ULL, - 18000000000000000000ULL, - (std::numeric_limits::max)() - 1ULL, - (std::numeric_limits::max)(), + 0xffffffffffffffff, }; for (const auto i : numbers) @@ -1202,7 +1198,7 @@ TEST_CASE("BSON numerical data") auto j_roundtrip = json::from_bson(bson); CHECK(j.at("entry").is_number_unsigned()); - CHECK(j_roundtrip.at("entry").is_number_integer()); + CHECK(j_roundtrip.at("entry").is_number_unsigned()); CHECK(j_roundtrip == j); CHECK(json::from_bson(bson, true, false) == j); }