mirror of
https://github.com/nlohmann/json.git
synced 2026-09-30 03:30:31 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
54ed6480d5 |
@@ -43,9 +43,6 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
||||
- Throws [`out_of_range.412`](../../home/exceptions.md#jsonexceptionout_of_range412) if the length of a document, array,
|
||||
string, or binary value exceeds the range of the 32-bit BSON length field; example:
|
||||
`"BSON length 2147483661 exceeds maximum of 2147483647"`
|
||||
- Throws [`out_of_range.415`](../../home/exceptions.md#jsonexceptionout_of_range415) if the subtype of a binary value
|
||||
exceeds 255, the maximum of the BSON binary subtype; example:
|
||||
`"subtype 70000 is too large for the BSON binary subtype (max 255)"`
|
||||
|
||||
## Complexity
|
||||
|
||||
@@ -81,4 +78,3 @@ pass before anything is written.
|
||||
|
||||
- Added in version 3.4.0.
|
||||
- Linear in the size of `j`, and no longer limited by the call stack for deeply nested values, since version 3.13.0.
|
||||
- `out_of_range.415` is now detected before anything is written, like the other exceptions above, since version 3.13.0.
|
||||
|
||||
@@ -725,7 +725,7 @@ std::is_constructible <decltype(std::declval<Compare>()(std::declval<A>(), std::
|
||||
// avoid their instantiation on all compilers, even when the first operand
|
||||
// is false. The dispatch on is_json_pointer_of can be removed once the
|
||||
// deprecated json_pointer comparison operators have been removed.
|
||||
template<typename Compare, typename A, typename B, bool = is_json_pointer_of<A, B>::value>
|
||||
template<typename Compare, typename A, typename B, bool = is_json_pointer_of<uncvref_t<A>, uncvref_t<B>>::value>
|
||||
struct is_comparable : std::false_type {};
|
||||
|
||||
template<typename Compare, typename A, typename B>
|
||||
|
||||
@@ -1055,26 +1055,15 @@ class binary_writer
|
||||
}
|
||||
|
||||
/*!
|
||||
@return The size of the BSON-encoded binary array in @a j
|
||||
@throw out_of_range.415 if the subtype of @a j does not fit into a byte,
|
||||
before anything is written
|
||||
@return The size of the BSON-encoded binary array @a value
|
||||
*/
|
||||
static std::size_t calc_bson_binary_size(const BasicJsonType& j)
|
||||
static std::size_t calc_bson_binary_size(const typename BasicJsonType::binary_t& value)
|
||||
{
|
||||
const auto& value = *j.m_data.m_value.binary;
|
||||
|
||||
if (value.has_subtype() && JSON_HEDLEY_UNLIKELY(value.subtype() > (std::numeric_limits<std::uint8_t>::max)()))
|
||||
{
|
||||
JSON_THROW(out_of_range::create(415, concat("subtype ", std::to_string(value.subtype()), " is too large for the BSON binary subtype (max 255)"), &j));
|
||||
}
|
||||
|
||||
return sizeof(std::int32_t) + value.size() + 1ul;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief Writes a BSON element with key @a name and binary value @a value
|
||||
@pre @a value's subtype, if any, fits into a byte; @ref calc_bson_sizes
|
||||
checks this for every binary value in the document beforehand.
|
||||
*/
|
||||
void write_bson_binary(const string_t& name,
|
||||
const binary_t& value)
|
||||
@@ -1083,6 +1072,11 @@ class binary_writer
|
||||
|
||||
write_number<std::int32_t>(to_bson_length(value.size()), true);
|
||||
|
||||
if (value.has_subtype() && JSON_HEDLEY_UNLIKELY(value.subtype() > (std::numeric_limits<std::uint8_t>::max)()))
|
||||
{
|
||||
JSON_THROW(out_of_range::create(415, concat("subtype ", std::to_string(value.subtype()), " is too large for the BSON binary subtype (max 255)"), nullptr));
|
||||
}
|
||||
|
||||
write_number(value.has_subtype() ? static_cast<std::uint8_t>(value.subtype()) : static_cast<std::uint8_t>(0x00));
|
||||
|
||||
oa.write_characters(reinterpret_cast<const CharType*>(value.data()), value.size());
|
||||
@@ -1091,15 +1085,13 @@ class binary_writer
|
||||
/*!
|
||||
@return The size of the value of the BSON document entry for @a j, which
|
||||
is neither an object nor an array
|
||||
@throw out_of_range.415 if @a j is binary with a subtype that does not fit
|
||||
into a byte, before anything is written
|
||||
*/
|
||||
static std::size_t calc_bson_value_size(const BasicJsonType& j)
|
||||
{
|
||||
switch (j.type())
|
||||
{
|
||||
case value_t::binary:
|
||||
return calc_bson_binary_size(j);
|
||||
return calc_bson_binary_size(*j.m_data.m_value.binary);
|
||||
|
||||
case value_t::boolean:
|
||||
return 1ul;
|
||||
@@ -1225,8 +1217,6 @@ class binary_writer
|
||||
@return the size of @a document
|
||||
@throw out_of_range.409 if a key contains U+0000, before anything is
|
||||
written
|
||||
@throw out_of_range.415 if a binary value's subtype does not fit into a
|
||||
byte, before anything is written
|
||||
*/
|
||||
static std::size_t calc_bson_sizes(const BasicJsonType& document, std::vector<std::size_t>& nested_sizes)
|
||||
{
|
||||
|
||||
@@ -4735,7 +4735,7 @@ std::is_constructible <decltype(std::declval<Compare>()(std::declval<A>(), std::
|
||||
// avoid their instantiation on all compilers, even when the first operand
|
||||
// is false. The dispatch on is_json_pointer_of can be removed once the
|
||||
// deprecated json_pointer comparison operators have been removed.
|
||||
template<typename Compare, typename A, typename B, bool = is_json_pointer_of<A, B>::value>
|
||||
template<typename Compare, typename A, typename B, bool = is_json_pointer_of<uncvref_t<A>, uncvref_t<B>>::value>
|
||||
struct is_comparable : std::false_type {};
|
||||
|
||||
template<typename Compare, typename A, typename B>
|
||||
@@ -21385,26 +21385,15 @@ class binary_writer
|
||||
}
|
||||
|
||||
/*!
|
||||
@return The size of the BSON-encoded binary array in @a j
|
||||
@throw out_of_range.415 if the subtype of @a j does not fit into a byte,
|
||||
before anything is written
|
||||
@return The size of the BSON-encoded binary array @a value
|
||||
*/
|
||||
static std::size_t calc_bson_binary_size(const BasicJsonType& j)
|
||||
static std::size_t calc_bson_binary_size(const typename BasicJsonType::binary_t& value)
|
||||
{
|
||||
const auto& value = *j.m_data.m_value.binary;
|
||||
|
||||
if (value.has_subtype() && JSON_HEDLEY_UNLIKELY(value.subtype() > (std::numeric_limits<std::uint8_t>::max)()))
|
||||
{
|
||||
JSON_THROW(out_of_range::create(415, concat("subtype ", std::to_string(value.subtype()), " is too large for the BSON binary subtype (max 255)"), &j));
|
||||
}
|
||||
|
||||
return sizeof(std::int32_t) + value.size() + 1ul;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief Writes a BSON element with key @a name and binary value @a value
|
||||
@pre @a value's subtype, if any, fits into a byte; @ref calc_bson_sizes
|
||||
checks this for every binary value in the document beforehand.
|
||||
*/
|
||||
void write_bson_binary(const string_t& name,
|
||||
const binary_t& value)
|
||||
@@ -21413,6 +21402,11 @@ class binary_writer
|
||||
|
||||
write_number<std::int32_t>(to_bson_length(value.size()), true);
|
||||
|
||||
if (value.has_subtype() && JSON_HEDLEY_UNLIKELY(value.subtype() > (std::numeric_limits<std::uint8_t>::max)()))
|
||||
{
|
||||
JSON_THROW(out_of_range::create(415, concat("subtype ", std::to_string(value.subtype()), " is too large for the BSON binary subtype (max 255)"), nullptr));
|
||||
}
|
||||
|
||||
write_number(value.has_subtype() ? static_cast<std::uint8_t>(value.subtype()) : static_cast<std::uint8_t>(0x00));
|
||||
|
||||
oa.write_characters(reinterpret_cast<const CharType*>(value.data()), value.size());
|
||||
@@ -21421,15 +21415,13 @@ class binary_writer
|
||||
/*!
|
||||
@return The size of the value of the BSON document entry for @a j, which
|
||||
is neither an object nor an array
|
||||
@throw out_of_range.415 if @a j is binary with a subtype that does not fit
|
||||
into a byte, before anything is written
|
||||
*/
|
||||
static std::size_t calc_bson_value_size(const BasicJsonType& j)
|
||||
{
|
||||
switch (j.type())
|
||||
{
|
||||
case value_t::binary:
|
||||
return calc_bson_binary_size(j);
|
||||
return calc_bson_binary_size(*j.m_data.m_value.binary);
|
||||
|
||||
case value_t::boolean:
|
||||
return 1ul;
|
||||
@@ -21555,8 +21547,6 @@ class binary_writer
|
||||
@return the size of @a document
|
||||
@throw out_of_range.409 if a key contains U+0000, before anything is
|
||||
written
|
||||
@throw out_of_range.415 if a binary value's subtype does not fit into a
|
||||
byte, before anything is written
|
||||
*/
|
||||
static std::size_t calc_bson_sizes(const BasicJsonType& document, std::vector<std::size_t>& nested_sizes)
|
||||
{
|
||||
|
||||
+1
-20
@@ -797,11 +797,7 @@ TEST_CASE("regression test - BSON binary subtype rejects a value that doesn't fi
|
||||
CHECK(json::from_bson(json::to_bson(doc255))["b"].get_binary().subtype() == 255);
|
||||
|
||||
CHECK_THROWS_AS(json::to_bson(json{{"b", json::binary({1, 2}, 256)}}), json::out_of_range);
|
||||
#if JSON_DIAGNOSTICS
|
||||
CHECK_THROWS_WITH_AS(json::to_bson(json {{"b", json::binary({1, 2}, 300)}}), "[json.exception.out_of_range.415] (/b) subtype 300 is too large for the BSON binary subtype (max 255)", json::out_of_range);
|
||||
#else
|
||||
CHECK_THROWS_WITH_AS(json::to_bson(json {{"b", json::binary({1, 2}, 300)}}), "[json.exception.out_of_range.415] subtype 300 is too large for the BSON binary subtype (max 255)", json::out_of_range);
|
||||
#endif
|
||||
CHECK_THROWS_WITH_AS(json::to_bson(json{{"b", json::binary({1, 2}, 300)}}), "[json.exception.out_of_range.415] subtype 300 is too large for the BSON binary subtype (max 255)", json::out_of_range);
|
||||
}
|
||||
|
||||
TEST_CASE("BSON input/output_adapters")
|
||||
@@ -1809,21 +1805,6 @@ value = depth % 2 == 0 ? json{{"a", std::move(value)}, {"b", {1, "x"}}} :
|
||||
CHECK(output.empty());
|
||||
}
|
||||
|
||||
SECTION("a binary subtype that doesn't fit a byte is rejected before anything is written (#5675)")
|
||||
{
|
||||
// the offending value is nested, so this also covers that the check
|
||||
// is not limited to a directly written value's own document
|
||||
json const j = {{"a", {{"b", json::binary({1, 2}, 300)}}}};
|
||||
|
||||
std::vector<std::uint8_t> vector_output;
|
||||
CHECK_THROWS_AS(json::to_bson(j, vector_output), json::out_of_range&);
|
||||
CHECK(vector_output.empty());
|
||||
|
||||
std::string string_output;
|
||||
CHECK_THROWS_AS(json::to_bson(j, string_output), json::out_of_range&);
|
||||
CHECK(string_output.empty());
|
||||
}
|
||||
|
||||
SECTION("values nested too deeply for the call stack (#5392)")
|
||||
{
|
||||
// serializing recursed once per nesting level, and computed every
|
||||
|
||||
@@ -101,12 +101,6 @@ TEST_CASE("Regression tests for extended diagnostics")
|
||||
CHECK_THROWS_WITH_AS(j.unflatten(), "[json.exception.type_error.315] (/~1foo) values in object must be primitive", json::type_error);
|
||||
}
|
||||
|
||||
SECTION("Regression test for issue #5675 - to_bson: out_of_range.415 has no diagnostics context")
|
||||
{
|
||||
json const j = {{"a", {{"b", json::binary({1, 2}, 300)}}}};
|
||||
CHECK_THROWS_WITH_AS(json::to_bson(j), "[json.exception.out_of_range.415] (/a/b) subtype 300 is too large for the BSON binary subtype (max 255)", json::out_of_range);
|
||||
}
|
||||
|
||||
SECTION("Regression test for issue #2838 - Assertion failure when inserting into arrays with JSON_DIAGNOSTICS set")
|
||||
{
|
||||
// void push_back(basic_json&& val)
|
||||
|
||||
@@ -858,6 +858,25 @@ TEST_CASE("JSON pointers")
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("value(json_pointer, default) with ordered_json #5664")
|
||||
{
|
||||
// ordered_json's transparent object comparator made value()'s
|
||||
// is_comparable_with_object_key check (which passes the pointer as
|
||||
// a reference) instantiate the deprecated json_pointer/string
|
||||
// comparison; this must compile without relying on it. The
|
||||
// deprecation warning itself is not observable here, since the
|
||||
// unit test build disables -Wdeprecated-declarations (see
|
||||
// cmake/clang_flags.cmake); it was checked manually instead.
|
||||
const nlohmann::ordered_json j = {{"n", 1}, {"s", "text"}};
|
||||
const nlohmann::ordered_json::json_pointer ptr_n("/n");
|
||||
const nlohmann::ordered_json::json_pointer ptr_s("/s");
|
||||
const nlohmann::ordered_json::json_pointer ptr_missing("/missing");
|
||||
|
||||
CHECK(j.value(ptr_n, 0) == 1);
|
||||
CHECK(j.value(ptr_s, std::string("x")) == "text");
|
||||
CHECK(j.value(ptr_missing, 42) == 42);
|
||||
}
|
||||
|
||||
// build with C++20
|
||||
// JSON_HAS_CPP_20
|
||||
#if defined(__cpp_char8_t)
|
||||
|
||||
Reference in New Issue
Block a user