-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Add support for BSON element 11: uint64 #4380
base: develop
Are you sure you want to change the base?
Changes from 9 commits
9ff8686
4c4c209
d56e85e
31863bc
7c616a2
3c4c28a
d142863
8fa4628
5345c07
df79b3f
36ce984
f24ecf7
465adfc
01215de
24de2ab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1066,7 +1066,7 @@ class binary_writer | |
{ | ||
return (value <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)())) | ||
? sizeof(std::int32_t) | ||
: sizeof(std::int64_t); | ||
: sizeof(std::uint64_t); | ||
} | ||
|
||
/*! | ||
|
@@ -1080,14 +1080,14 @@ class binary_writer | |
write_bson_entry_header(name, 0x10 /* int32 */); | ||
write_number<std::int32_t>(static_cast<std::int32_t>(j.m_data.m_value.number_unsigned), true); | ||
} | ||
else if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)())) | ||
else if (j.m_data.m_value.number_unsigned <= std::numeric_limits<std::uint64_t>::max()) | ||
slowriot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
write_bson_entry_header(name, 0x12 /* int64 */); | ||
write_number<std::int64_t>(static_cast<std::int64_t>(j.m_data.m_value.number_unsigned), true); | ||
write_bson_entry_header(name, 0x11 /* uint64 */); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change seems reasonable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change is logical, but I have some concerns about backward compatibility. It may break the following scenario:
With the changes in this PR, the comparison between |
||
write_number<std::uint64_t>(static_cast<std::uint64_t>(j.m_data.m_value.number_unsigned), true); | ||
} | ||
else | ||
{ | ||
JSON_THROW(out_of_range::create(407, concat("integer number ", std::to_string(j.m_data.m_value.number_unsigned), " cannot be represented by BSON as it does not fit int64"), &j)); | ||
JSON_THROW(out_of_range::create(407, concat("unsigned integer number ", std::to_string(j.m_data.m_value.number_unsigned), " cannot be represented by BSON as it does not fit into uint64"), &j)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I read the coverage information correctly, then this line is not covered in a test. Please check and add a test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this can only happen if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So far, I failed to compile the library with 128-bit integers. For CBOR, we assume all unsigned integers to fit into 64 bits, so I think it's fair to do the same here. (Any code like the one above could not be tested anyway.) |
||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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} } | ||
|
@@ -340,7 +339,7 @@ TEST_CASE("BSON") | |
std::vector<std::uint8_t> 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 | ||
|
@@ -1134,7 +1133,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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a consequence of the change in |
||
'e', 'n', 't', 'r', 'y', '\x00', | ||
static_cast<std::uint8_t>((iu >> (8u * 0u)) & 0xffu), | ||
static_cast<std::uint8_t>((iu >> (8u * 1u)) & 0xffu), | ||
|
@@ -1184,7 +1183,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 +1196,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_integer()); | ||
CHECK(j_roundtrip == j); | ||
CHECK(json::from_bson(bson, true, false) == j); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change seems reasonable.