Support BSON uint64 de/serialization (#4590)

* Support BSON uint64 de/serialization

Signed-off-by: Michael Valladolid <mikevalladolid@gmail.com>

* Treat 0x11 as uint64 and not timestamp specific

Signed-off-by: Michael Valladolid <mikevalladolid@gmail.com>

---------

Signed-off-by: Michael Valladolid <mikevalladolid@gmail.com>
This commit is contained in:
Michael Valladolid
2025-01-10 22:59:55 +09:00
committed by GitHub
parent 1809b3d800
commit 2d42229f4d
6 changed files with 35 additions and 18 deletions

View File

@@ -331,7 +331,6 @@ TEST_CASE("BSON")
SECTION("non-empty object with unsigned integer (64-bit) member")
{
// directly encoding uint64 is not supported in bson (only for timestamp values)
json const j =
{
{ "entry", std::uint64_t{0x1234567804030201} }
@@ -531,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},
@@ -1164,10 +1162,7 @@ TEST_CASE("BSON numerical data")
std::vector<std::uint64_t> const numbers
{
static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()) + 1ULL,
10000000000000000000ULL,
18000000000000000000ULL,
(std::numeric_limits<std::uint64_t>::max)() - 1ULL,
(std::numeric_limits<std::uint64_t>::max)(),
0xffffffffffffffff,
};
for (const auto i : numbers)
@@ -1184,7 +1179,7 @@ TEST_CASE("BSON numerical data")
std::vector<std::uint8_t> const expected_bson =
{
0x14u, 0x00u, 0x00u, 0x00u, // size (little endian)
0x12u, /// entry: int64
0x11u, /// entry: uint64
'e', 'n', 't', 'r', 'y', '\x00',
static_cast<std::uint8_t>((iu >> (8u * 0u)) & 0xffu),
static_cast<std::uint8_t>((iu >> (8u * 1u)) & 0xffu),
@@ -1197,12 +1192,15 @@ TEST_CASE("BSON numerical data")
0x00u // end marker
};
CHECK_THROWS_AS(json::to_bson(j), json::out_of_range&);
#if JSON_DIAGNOSTICS
CHECK_THROWS_WITH_STD_STR(json::to_bson(j), "[json.exception.out_of_range.407] (/entry) integer number " + std::to_string(i) + " cannot be represented by BSON as it does not fit int64");
#else
CHECK_THROWS_WITH_STD_STR(json::to_bson(j), "[json.exception.out_of_range.407] integer number " + std::to_string(i) + " cannot be represented by BSON as it does not fit int64");
#endif
const auto bson = json::to_bson(j);
CHECK(bson == expected_bson);
auto j_roundtrip = json::from_bson(bson);
CHECK(j.at("entry").is_number_unsigned());
CHECK(j_roundtrip.at("entry").is_number_unsigned());
CHECK(j_roundtrip == j);
CHECK(json::from_bson(bson, true, false) == j);
}
}