mirror of
https://github.com/nlohmann/json.git
synced 2026-09-07 16:57:59 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
50e392ab1a | ||
|
|
d2c2db92a9 |
@@ -996,21 +996,23 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief reads a definite-length CBOR string
|
@brief reads a CBOR string
|
||||||
|
|
||||||
Reads everything @ref get_cbor_string accepts except the indefinite-length
|
This function first reads starting bytes to determine the expected
|
||||||
form, which that function handles itself. The bytes are appended to @a
|
string length and then copies this number of bytes into a string.
|
||||||
result, so consecutive chunks of an indefinite-length string can be read
|
Additionally, CBOR's strings with indefinite lengths are supported.
|
||||||
into the same string.
|
|
||||||
|
|
||||||
@param[out] result string the bytes are appended to
|
@param[out] result created string
|
||||||
|
|
||||||
@return whether string creation completed
|
@return whether string creation completed
|
||||||
|
|
||||||
@pre @a current is not EOF
|
|
||||||
*/
|
*/
|
||||||
bool get_cbor_string_chunk(string_t& result)
|
bool get_cbor_string(string_t& result)
|
||||||
{
|
{
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "string")))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
switch (current)
|
switch (current)
|
||||||
{
|
{
|
||||||
// UTF-8 string (0x00..0x17 bytes follow)
|
// UTF-8 string (0x00..0x17 bytes follow)
|
||||||
@@ -1066,6 +1068,20 @@ class binary_reader
|
|||||||
return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
|
return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case 0x7F: // UTF-8 string (indefinite length)
|
||||||
|
{
|
||||||
|
while (get() != 0xFF)
|
||||||
|
{
|
||||||
|
string_t chunk;
|
||||||
|
if (!get_cbor_string(chunk))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
result.append(chunk);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
auto last_token = get_token_string();
|
auto last_token = get_token_string();
|
||||||
@@ -1076,82 +1092,23 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief reads a CBOR string
|
@brief reads a CBOR byte array
|
||||||
|
|
||||||
This function first reads starting bytes to determine the expected
|
This function first reads starting bytes to determine the expected
|
||||||
string length and then copies this number of bytes into a string.
|
byte array length and then copies this number of bytes into the byte array.
|
||||||
Additionally, CBOR's strings with indefinite lengths are supported.
|
Additionally, CBOR's byte arrays with indefinite lengths are supported.
|
||||||
|
|
||||||
@param[out] result created string
|
@param[out] result created byte array
|
||||||
|
|
||||||
@return whether string creation completed
|
|
||||||
*/
|
|
||||||
bool get_cbor_string(string_t& result)
|
|
||||||
{
|
|
||||||
// number of indefinite-length strings that have been opened and not
|
|
||||||
// closed yet. RFC 8949, Section 3.2.3 does not permit nesting them,
|
|
||||||
// but this reader has always accepted it, so the open levels are
|
|
||||||
// counted instead of recursed through, which overflowed the stack for
|
|
||||||
// an input of repeated 0x7F bytes (see #5104). Every chunk is appended
|
|
||||||
// to the same result, so no per-level state is needed.
|
|
||||||
std::size_t open = 0;
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "string")))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (current == 0x7F) // UTF-8 string (indefinite length)
|
|
||||||
{
|
|
||||||
++open;
|
|
||||||
get();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// a break marker closes the innermost indefinite-length string;
|
|
||||||
// outside of one it is not a string and falls through to the error
|
|
||||||
if (open != 0 && current == 0xFF)
|
|
||||||
{
|
|
||||||
if (--open == 0)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
get();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!get_cbor_string_chunk(result)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (open == 0)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
get();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
@brief reads a definite-length CBOR byte array
|
|
||||||
|
|
||||||
Reads everything @ref get_cbor_binary accepts except the indefinite-length
|
|
||||||
form, which that function handles itself. The bytes are appended to @a
|
|
||||||
result, so consecutive chunks of an indefinite-length byte array can be
|
|
||||||
read into the same byte array.
|
|
||||||
|
|
||||||
@param[out] result byte array the bytes are appended to
|
|
||||||
|
|
||||||
@return whether byte array creation completed
|
@return whether byte array creation completed
|
||||||
|
|
||||||
@pre @a current is not EOF
|
|
||||||
*/
|
*/
|
||||||
bool get_cbor_binary_chunk(binary_t& result)
|
bool get_cbor_binary(binary_t& result)
|
||||||
{
|
{
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "binary")))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
switch (current)
|
switch (current)
|
||||||
{
|
{
|
||||||
// Binary data (0x00..0x17 bytes follow)
|
// Binary data (0x00..0x17 bytes follow)
|
||||||
@@ -1211,6 +1168,20 @@ class binary_reader
|
|||||||
get_binary(input_format_t::cbor, len, result);
|
get_binary(input_format_t::cbor, len, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case 0x5F: // Binary data (indefinite length)
|
||||||
|
{
|
||||||
|
while (get() != 0xFF)
|
||||||
|
{
|
||||||
|
binary_t chunk;
|
||||||
|
if (!get_cbor_binary(chunk))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
result.insert(result.end(), chunk.begin(), chunk.end());
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
auto last_token = get_token_string();
|
auto last_token = get_token_string();
|
||||||
@@ -1220,63 +1191,6 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
@brief reads a CBOR byte array
|
|
||||||
|
|
||||||
This function first reads starting bytes to determine the expected
|
|
||||||
byte array length and then copies this number of bytes into the byte array.
|
|
||||||
Additionally, CBOR's byte arrays with indefinite lengths are supported.
|
|
||||||
|
|
||||||
@param[out] result created byte array
|
|
||||||
|
|
||||||
@return whether byte array creation completed
|
|
||||||
*/
|
|
||||||
bool get_cbor_binary(binary_t& result)
|
|
||||||
{
|
|
||||||
// the open indefinite-length byte arrays are counted rather than
|
|
||||||
// recursed through, for the reason given in @ref get_cbor_string
|
|
||||||
std::size_t open = 0;
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "binary")))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (current == 0x5F) // Binary data (indefinite length)
|
|
||||||
{
|
|
||||||
++open;
|
|
||||||
get();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// a break marker closes the innermost indefinite-length byte
|
|
||||||
// array; outside of one it falls through to the error below
|
|
||||||
if (open != 0 && current == 0xFF)
|
|
||||||
{
|
|
||||||
if (--open == 0)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
get();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!get_cbor_binary_chunk(result)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (open == 0)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
get();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief narrow a definite CBOR array/map length to std::size_t
|
@brief narrow a definite CBOR array/map length to std::size_t
|
||||||
|
|
||||||
|
|||||||
@@ -1647,6 +1647,20 @@ class binary_writer
|
|||||||
return 'D'; // float 64
|
return 'D'; // float 64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief checks whether a JSON number fits into @a TargetType
|
||||||
|
@param[in] el a JSON number of either the signed or unsigned integer kind
|
||||||
|
@return whether @a el's value can be represented by @a TargetType without
|
||||||
|
wrapping, regardless of which of the two kinds it is stored as
|
||||||
|
*/
|
||||||
|
template<typename TargetType>
|
||||||
|
static bool bjdata_ndarray_value_in_range(const BasicJsonType& el)
|
||||||
|
{
|
||||||
|
return el.is_number_unsigned()
|
||||||
|
? value_in_range_of<TargetType>(el.template get<std::uint64_t>())
|
||||||
|
: value_in_range_of<TargetType>(el.template get<std::int64_t>());
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@return false if the object is successfully converted to a bjdata ndarray, true if the type or size is invalid
|
@return false if the object is successfully converted to a bjdata ndarray, true if the type or size is invalid
|
||||||
*/
|
*/
|
||||||
@@ -1667,6 +1681,16 @@ class binary_writer
|
|||||||
}
|
}
|
||||||
CharType dtype = it->second;
|
CharType dtype = it->second;
|
||||||
|
|
||||||
|
// the 'B' (byte) marker is only defined by BJData Draft 3; emitting it
|
||||||
|
// under the default Draft 2 mode would produce a stream that Draft 2
|
||||||
|
// readers reject, so such an object falls back to a plain object
|
||||||
|
// encoding instead (see the "Binary values" section of the BJData
|
||||||
|
// documentation)
|
||||||
|
if (dtype == 'B' && bjdata_version != bjdata_version_t::draft3)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
key = "_ArraySize_";
|
key = "_ArraySize_";
|
||||||
// the dimensions are written verbatim as the header length below, so a
|
// the dimensions are written verbatim as the header length below, so a
|
||||||
// value that is not an array cannot produce a valid one: null emits 'Z'
|
// value that is not an array cannot produce a valid one: null emits 'Z'
|
||||||
@@ -1731,6 +1755,60 @@ class binary_writer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// every element is cast to the (possibly narrower) C++ type matching
|
||||||
|
// dtype below; a value that does not fit that type would silently
|
||||||
|
// wrap (integers) or overflow to infinity (the "single" precision
|
||||||
|
// float) instead of being reported, so such an object falls back to
|
||||||
|
// a plain object encoding as well
|
||||||
|
for (const auto& el : value.at(key))
|
||||||
|
{
|
||||||
|
bool in_range = true;
|
||||||
|
switch (dtype)
|
||||||
|
{
|
||||||
|
case 'U':
|
||||||
|
case 'C':
|
||||||
|
case 'B':
|
||||||
|
in_range = bjdata_ndarray_value_in_range<std::uint8_t>(el);
|
||||||
|
break;
|
||||||
|
case 'i':
|
||||||
|
in_range = bjdata_ndarray_value_in_range<std::int8_t>(el);
|
||||||
|
break;
|
||||||
|
case 'u':
|
||||||
|
in_range = bjdata_ndarray_value_in_range<std::uint16_t>(el);
|
||||||
|
break;
|
||||||
|
case 'I':
|
||||||
|
in_range = bjdata_ndarray_value_in_range<std::int16_t>(el);
|
||||||
|
break;
|
||||||
|
case 'm':
|
||||||
|
in_range = bjdata_ndarray_value_in_range<std::uint32_t>(el);
|
||||||
|
break;
|
||||||
|
case 'l':
|
||||||
|
in_range = bjdata_ndarray_value_in_range<std::int32_t>(el);
|
||||||
|
break;
|
||||||
|
case 'M':
|
||||||
|
in_range = bjdata_ndarray_value_in_range<std::uint64_t>(el);
|
||||||
|
break;
|
||||||
|
case 'L':
|
||||||
|
in_range = bjdata_ndarray_value_in_range<std::int64_t>(el);
|
||||||
|
break;
|
||||||
|
case 'd':
|
||||||
|
{
|
||||||
|
const auto dval = el.template get<double>();
|
||||||
|
in_range = !std::isfinite(dval) ||
|
||||||
|
(dval >= static_cast<double>(std::numeric_limits<float>::lowest()) &&
|
||||||
|
dval <= static_cast<double>((std::numeric_limits<float>::max)()));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
// 'D' (double) already spans the full range of number_float_t
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (!in_range)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
oa->write_character('[');
|
oa->write_character('[');
|
||||||
oa->write_character('$');
|
oa->write_character('$');
|
||||||
oa->write_character(dtype);
|
oa->write_character(dtype);
|
||||||
|
|||||||
+28
-70
@@ -4473,11 +4473,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json result;
|
basic_json result;
|
||||||
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler)) // cppcheck-suppress[accessMoved]
|
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler); // cppcheck-suppress[accessMoved]
|
||||||
{
|
return res ? result : basic_json(value_t::discarded);
|
||||||
result = value_t::discarded;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in CBOR format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
/// @brief create a JSON value from an input in CBOR format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
||||||
@@ -4493,11 +4490,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json result;
|
basic_json result;
|
||||||
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler)) // cppcheck-suppress[accessMoved]
|
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler); // cppcheck-suppress[accessMoved]
|
||||||
{
|
return res ? result : basic_json(value_t::discarded);
|
||||||
result = value_t::discarded;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -4522,11 +4516,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
auto ia = i.get();
|
auto ia = i.get();
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
||||||
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler)) // cppcheck-suppress[accessMoved]
|
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler); // cppcheck-suppress[accessMoved]
|
||||||
{
|
return res ? result : basic_json(value_t::discarded);
|
||||||
result = value_t::discarded;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in MessagePack format
|
/// @brief create a JSON value from an input in MessagePack format
|
||||||
@@ -4540,11 +4531,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json result;
|
basic_json result;
|
||||||
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||||
{
|
return res ? result : basic_json(value_t::discarded);
|
||||||
result = value_t::discarded;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in MessagePack format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
/// @brief create a JSON value from an input in MessagePack format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
||||||
@@ -4559,11 +4547,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json result;
|
basic_json result;
|
||||||
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||||
{
|
return res ? result : basic_json(value_t::discarded);
|
||||||
result = value_t::discarded;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -4586,11 +4571,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
auto ia = i.get();
|
auto ia = i.get();
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
||||||
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||||
{
|
return res ? result : basic_json(value_t::discarded);
|
||||||
result = value_t::discarded;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in UBJSON format
|
/// @brief create a JSON value from an input in UBJSON format
|
||||||
@@ -4604,11 +4586,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json result;
|
basic_json result;
|
||||||
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||||
{
|
return res ? result : basic_json(value_t::discarded);
|
||||||
result = value_t::discarded;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in UBJSON format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
/// @brief create a JSON value from an input in UBJSON format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
||||||
@@ -4623,11 +4602,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json result;
|
basic_json result;
|
||||||
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||||
{
|
return res ? result : basic_json(value_t::discarded);
|
||||||
result = value_t::discarded;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -4650,11 +4626,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
auto ia = i.get();
|
auto ia = i.get();
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
||||||
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||||
{
|
return res ? result : basic_json(value_t::discarded);
|
||||||
result = value_t::discarded;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in BJData format
|
/// @brief create a JSON value from an input in BJData format
|
||||||
@@ -4668,11 +4641,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json result;
|
basic_json result;
|
||||||
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||||
{
|
return res ? result : basic_json(value_t::discarded);
|
||||||
result = value_t::discarded;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in BJData format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
/// @brief create a JSON value from an input in BJData format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
||||||
@@ -4687,11 +4657,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json result;
|
basic_json result;
|
||||||
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||||
{
|
return res ? result : basic_json(value_t::discarded);
|
||||||
result = value_t::discarded;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in BSON format
|
/// @brief create a JSON value from an input in BSON format
|
||||||
@@ -4705,11 +4672,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json result;
|
basic_json result;
|
||||||
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||||
{
|
return res ? result : basic_json(value_t::discarded);
|
||||||
result = value_t::discarded;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in BSON format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
/// @brief create a JSON value from an input in BSON format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
||||||
@@ -4724,11 +4688,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json result;
|
basic_json result;
|
||||||
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||||
{
|
return res ? result : basic_json(value_t::discarded);
|
||||||
result = value_t::discarded;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -4751,11 +4712,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
auto ia = i.get();
|
auto ia = i.get();
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
||||||
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||||
{
|
return res ? result : basic_json(value_t::discarded);
|
||||||
result = value_t::discarded;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
/// @}
|
/// @}
|
||||||
|
|
||||||
|
|||||||
+155
-205
@@ -11683,21 +11683,23 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief reads a definite-length CBOR string
|
@brief reads a CBOR string
|
||||||
|
|
||||||
Reads everything @ref get_cbor_string accepts except the indefinite-length
|
This function first reads starting bytes to determine the expected
|
||||||
form, which that function handles itself. The bytes are appended to @a
|
string length and then copies this number of bytes into a string.
|
||||||
result, so consecutive chunks of an indefinite-length string can be read
|
Additionally, CBOR's strings with indefinite lengths are supported.
|
||||||
into the same string.
|
|
||||||
|
|
||||||
@param[out] result string the bytes are appended to
|
@param[out] result created string
|
||||||
|
|
||||||
@return whether string creation completed
|
@return whether string creation completed
|
||||||
|
|
||||||
@pre @a current is not EOF
|
|
||||||
*/
|
*/
|
||||||
bool get_cbor_string_chunk(string_t& result)
|
bool get_cbor_string(string_t& result)
|
||||||
{
|
{
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "string")))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
switch (current)
|
switch (current)
|
||||||
{
|
{
|
||||||
// UTF-8 string (0x00..0x17 bytes follow)
|
// UTF-8 string (0x00..0x17 bytes follow)
|
||||||
@@ -11753,6 +11755,20 @@ class binary_reader
|
|||||||
return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
|
return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case 0x7F: // UTF-8 string (indefinite length)
|
||||||
|
{
|
||||||
|
while (get() != 0xFF)
|
||||||
|
{
|
||||||
|
string_t chunk;
|
||||||
|
if (!get_cbor_string(chunk))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
result.append(chunk);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
auto last_token = get_token_string();
|
auto last_token = get_token_string();
|
||||||
@@ -11763,82 +11779,23 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief reads a CBOR string
|
@brief reads a CBOR byte array
|
||||||
|
|
||||||
This function first reads starting bytes to determine the expected
|
This function first reads starting bytes to determine the expected
|
||||||
string length and then copies this number of bytes into a string.
|
byte array length and then copies this number of bytes into the byte array.
|
||||||
Additionally, CBOR's strings with indefinite lengths are supported.
|
Additionally, CBOR's byte arrays with indefinite lengths are supported.
|
||||||
|
|
||||||
@param[out] result created string
|
@param[out] result created byte array
|
||||||
|
|
||||||
@return whether string creation completed
|
|
||||||
*/
|
|
||||||
bool get_cbor_string(string_t& result)
|
|
||||||
{
|
|
||||||
// number of indefinite-length strings that have been opened and not
|
|
||||||
// closed yet. RFC 8949, Section 3.2.3 does not permit nesting them,
|
|
||||||
// but this reader has always accepted it, so the open levels are
|
|
||||||
// counted instead of recursed through, which overflowed the stack for
|
|
||||||
// an input of repeated 0x7F bytes (see #5104). Every chunk is appended
|
|
||||||
// to the same result, so no per-level state is needed.
|
|
||||||
std::size_t open = 0;
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "string")))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (current == 0x7F) // UTF-8 string (indefinite length)
|
|
||||||
{
|
|
||||||
++open;
|
|
||||||
get();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// a break marker closes the innermost indefinite-length string;
|
|
||||||
// outside of one it is not a string and falls through to the error
|
|
||||||
if (open != 0 && current == 0xFF)
|
|
||||||
{
|
|
||||||
if (--open == 0)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
get();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!get_cbor_string_chunk(result)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (open == 0)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
get();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
@brief reads a definite-length CBOR byte array
|
|
||||||
|
|
||||||
Reads everything @ref get_cbor_binary accepts except the indefinite-length
|
|
||||||
form, which that function handles itself. The bytes are appended to @a
|
|
||||||
result, so consecutive chunks of an indefinite-length byte array can be
|
|
||||||
read into the same byte array.
|
|
||||||
|
|
||||||
@param[out] result byte array the bytes are appended to
|
|
||||||
|
|
||||||
@return whether byte array creation completed
|
@return whether byte array creation completed
|
||||||
|
|
||||||
@pre @a current is not EOF
|
|
||||||
*/
|
*/
|
||||||
bool get_cbor_binary_chunk(binary_t& result)
|
bool get_cbor_binary(binary_t& result)
|
||||||
{
|
{
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "binary")))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
switch (current)
|
switch (current)
|
||||||
{
|
{
|
||||||
// Binary data (0x00..0x17 bytes follow)
|
// Binary data (0x00..0x17 bytes follow)
|
||||||
@@ -11898,6 +11855,20 @@ class binary_reader
|
|||||||
get_binary(input_format_t::cbor, len, result);
|
get_binary(input_format_t::cbor, len, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case 0x5F: // Binary data (indefinite length)
|
||||||
|
{
|
||||||
|
while (get() != 0xFF)
|
||||||
|
{
|
||||||
|
binary_t chunk;
|
||||||
|
if (!get_cbor_binary(chunk))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
result.insert(result.end(), chunk.begin(), chunk.end());
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
auto last_token = get_token_string();
|
auto last_token = get_token_string();
|
||||||
@@ -11907,63 +11878,6 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
|
||||||
@brief reads a CBOR byte array
|
|
||||||
|
|
||||||
This function first reads starting bytes to determine the expected
|
|
||||||
byte array length and then copies this number of bytes into the byte array.
|
|
||||||
Additionally, CBOR's byte arrays with indefinite lengths are supported.
|
|
||||||
|
|
||||||
@param[out] result created byte array
|
|
||||||
|
|
||||||
@return whether byte array creation completed
|
|
||||||
*/
|
|
||||||
bool get_cbor_binary(binary_t& result)
|
|
||||||
{
|
|
||||||
// the open indefinite-length byte arrays are counted rather than
|
|
||||||
// recursed through, for the reason given in @ref get_cbor_string
|
|
||||||
std::size_t open = 0;
|
|
||||||
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "binary")))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (current == 0x5F) // Binary data (indefinite length)
|
|
||||||
{
|
|
||||||
++open;
|
|
||||||
get();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// a break marker closes the innermost indefinite-length byte
|
|
||||||
// array; outside of one it falls through to the error below
|
|
||||||
if (open != 0 && current == 0xFF)
|
|
||||||
{
|
|
||||||
if (--open == 0)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
get();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!get_cbor_binary_chunk(result)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (open == 0)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
get();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief narrow a definite CBOR array/map length to std::size_t
|
@brief narrow a definite CBOR array/map length to std::size_t
|
||||||
|
|
||||||
@@ -18741,6 +18655,20 @@ class binary_writer
|
|||||||
return 'D'; // float 64
|
return 'D'; // float 64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief checks whether a JSON number fits into @a TargetType
|
||||||
|
@param[in] el a JSON number of either the signed or unsigned integer kind
|
||||||
|
@return whether @a el's value can be represented by @a TargetType without
|
||||||
|
wrapping, regardless of which of the two kinds it is stored as
|
||||||
|
*/
|
||||||
|
template<typename TargetType>
|
||||||
|
static bool bjdata_ndarray_value_in_range(const BasicJsonType& el)
|
||||||
|
{
|
||||||
|
return el.is_number_unsigned()
|
||||||
|
? value_in_range_of<TargetType>(el.template get<std::uint64_t>())
|
||||||
|
: value_in_range_of<TargetType>(el.template get<std::int64_t>());
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@return false if the object is successfully converted to a bjdata ndarray, true if the type or size is invalid
|
@return false if the object is successfully converted to a bjdata ndarray, true if the type or size is invalid
|
||||||
*/
|
*/
|
||||||
@@ -18761,6 +18689,16 @@ class binary_writer
|
|||||||
}
|
}
|
||||||
CharType dtype = it->second;
|
CharType dtype = it->second;
|
||||||
|
|
||||||
|
// the 'B' (byte) marker is only defined by BJData Draft 3; emitting it
|
||||||
|
// under the default Draft 2 mode would produce a stream that Draft 2
|
||||||
|
// readers reject, so such an object falls back to a plain object
|
||||||
|
// encoding instead (see the "Binary values" section of the BJData
|
||||||
|
// documentation)
|
||||||
|
if (dtype == 'B' && bjdata_version != bjdata_version_t::draft3)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
key = "_ArraySize_";
|
key = "_ArraySize_";
|
||||||
// the dimensions are written verbatim as the header length below, so a
|
// the dimensions are written verbatim as the header length below, so a
|
||||||
// value that is not an array cannot produce a valid one: null emits 'Z'
|
// value that is not an array cannot produce a valid one: null emits 'Z'
|
||||||
@@ -18825,6 +18763,60 @@ class binary_writer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// every element is cast to the (possibly narrower) C++ type matching
|
||||||
|
// dtype below; a value that does not fit that type would silently
|
||||||
|
// wrap (integers) or overflow to infinity (the "single" precision
|
||||||
|
// float) instead of being reported, so such an object falls back to
|
||||||
|
// a plain object encoding as well
|
||||||
|
for (const auto& el : value.at(key))
|
||||||
|
{
|
||||||
|
bool in_range = true;
|
||||||
|
switch (dtype)
|
||||||
|
{
|
||||||
|
case 'U':
|
||||||
|
case 'C':
|
||||||
|
case 'B':
|
||||||
|
in_range = bjdata_ndarray_value_in_range<std::uint8_t>(el);
|
||||||
|
break;
|
||||||
|
case 'i':
|
||||||
|
in_range = bjdata_ndarray_value_in_range<std::int8_t>(el);
|
||||||
|
break;
|
||||||
|
case 'u':
|
||||||
|
in_range = bjdata_ndarray_value_in_range<std::uint16_t>(el);
|
||||||
|
break;
|
||||||
|
case 'I':
|
||||||
|
in_range = bjdata_ndarray_value_in_range<std::int16_t>(el);
|
||||||
|
break;
|
||||||
|
case 'm':
|
||||||
|
in_range = bjdata_ndarray_value_in_range<std::uint32_t>(el);
|
||||||
|
break;
|
||||||
|
case 'l':
|
||||||
|
in_range = bjdata_ndarray_value_in_range<std::int32_t>(el);
|
||||||
|
break;
|
||||||
|
case 'M':
|
||||||
|
in_range = bjdata_ndarray_value_in_range<std::uint64_t>(el);
|
||||||
|
break;
|
||||||
|
case 'L':
|
||||||
|
in_range = bjdata_ndarray_value_in_range<std::int64_t>(el);
|
||||||
|
break;
|
||||||
|
case 'd':
|
||||||
|
{
|
||||||
|
const auto dval = el.template get<double>();
|
||||||
|
in_range = !std::isfinite(dval) ||
|
||||||
|
(dval >= static_cast<double>(std::numeric_limits<float>::lowest()) &&
|
||||||
|
dval <= static_cast<double>((std::numeric_limits<float>::max)()));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
// 'D' (double) already spans the full range of number_float_t
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (!in_range)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
oa->write_character('[');
|
oa->write_character('[');
|
||||||
oa->write_character('$');
|
oa->write_character('$');
|
||||||
oa->write_character(dtype);
|
oa->write_character(dtype);
|
||||||
@@ -25987,11 +25979,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json result;
|
basic_json result;
|
||||||
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler)) // cppcheck-suppress[accessMoved]
|
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler); // cppcheck-suppress[accessMoved]
|
||||||
{
|
return res ? result : basic_json(value_t::discarded);
|
||||||
result = value_t::discarded;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in CBOR format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
/// @brief create a JSON value from an input in CBOR format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
||||||
@@ -26007,11 +25996,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json result;
|
basic_json result;
|
||||||
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler)) // cppcheck-suppress[accessMoved]
|
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler); // cppcheck-suppress[accessMoved]
|
||||||
{
|
return res ? result : basic_json(value_t::discarded);
|
||||||
result = value_t::discarded;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -26036,11 +26022,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
auto ia = i.get();
|
auto ia = i.get();
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
||||||
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler)) // cppcheck-suppress[accessMoved]
|
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler); // cppcheck-suppress[accessMoved]
|
||||||
{
|
return res ? result : basic_json(value_t::discarded);
|
||||||
result = value_t::discarded;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in MessagePack format
|
/// @brief create a JSON value from an input in MessagePack format
|
||||||
@@ -26054,11 +26037,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json result;
|
basic_json result;
|
||||||
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||||
{
|
return res ? result : basic_json(value_t::discarded);
|
||||||
result = value_t::discarded;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in MessagePack format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
/// @brief create a JSON value from an input in MessagePack format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
||||||
@@ -26073,11 +26053,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json result;
|
basic_json result;
|
||||||
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||||
{
|
return res ? result : basic_json(value_t::discarded);
|
||||||
result = value_t::discarded;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -26100,11 +26077,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
auto ia = i.get();
|
auto ia = i.get();
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
||||||
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||||
{
|
return res ? result : basic_json(value_t::discarded);
|
||||||
result = value_t::discarded;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in UBJSON format
|
/// @brief create a JSON value from an input in UBJSON format
|
||||||
@@ -26118,11 +26092,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json result;
|
basic_json result;
|
||||||
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||||
{
|
return res ? result : basic_json(value_t::discarded);
|
||||||
result = value_t::discarded;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in UBJSON format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
/// @brief create a JSON value from an input in UBJSON format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
||||||
@@ -26137,11 +26108,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json result;
|
basic_json result;
|
||||||
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||||
{
|
return res ? result : basic_json(value_t::discarded);
|
||||||
result = value_t::discarded;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -26164,11 +26132,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
auto ia = i.get();
|
auto ia = i.get();
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
||||||
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||||
{
|
return res ? result : basic_json(value_t::discarded);
|
||||||
result = value_t::discarded;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in BJData format
|
/// @brief create a JSON value from an input in BJData format
|
||||||
@@ -26182,11 +26147,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json result;
|
basic_json result;
|
||||||
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||||
{
|
return res ? result : basic_json(value_t::discarded);
|
||||||
result = value_t::discarded;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in BJData format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
/// @brief create a JSON value from an input in BJData format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
||||||
@@ -26201,11 +26163,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json result;
|
basic_json result;
|
||||||
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||||
{
|
return res ? result : basic_json(value_t::discarded);
|
||||||
result = value_t::discarded;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in BSON format
|
/// @brief create a JSON value from an input in BSON format
|
||||||
@@ -26219,11 +26178,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json result;
|
basic_json result;
|
||||||
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||||
{
|
return res ? result : basic_json(value_t::discarded);
|
||||||
result = value_t::discarded;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in BSON format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
/// @brief create a JSON value from an input in BSON format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
||||||
@@ -26238,11 +26194,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json result;
|
basic_json result;
|
||||||
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||||
{
|
return res ? result : basic_json(value_t::discarded);
|
||||||
result = value_t::discarded;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -26265,11 +26218,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
auto ia = i.get();
|
auto ia = i.get();
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
||||||
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict); // cppcheck-suppress[accessMoved]
|
||||||
{
|
return res ? result : basic_json(value_t::discarded);
|
||||||
result = value_t::discarded;
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
/// @}
|
/// @}
|
||||||
|
|
||||||
|
|||||||
@@ -2586,7 +2586,12 @@ TEST_CASE("BJData")
|
|||||||
CHECK(json::to_bjdata(json::from_bjdata(v_d), true, true) == v_d);
|
CHECK(json::to_bjdata(json::from_bjdata(v_d), true, true) == v_d);
|
||||||
CHECK(json::to_bjdata(json::from_bjdata(v_D), true, true) == v_D);
|
CHECK(json::to_bjdata(json::from_bjdata(v_D), true, true) == v_D);
|
||||||
CHECK(json::to_bjdata(json::from_bjdata(v_C), true, true) == v_C);
|
CHECK(json::to_bjdata(json::from_bjdata(v_C), true, true) == v_C);
|
||||||
CHECK(json::to_bjdata(json::from_bjdata(v_B), true, true) == v_B);
|
// v_B uses the Draft-3-only 'B' marker, so it round-trips only when
|
||||||
|
// Draft 3 is explicitly selected (see GitHub issue #5404); the
|
||||||
|
// default Draft 2 falls back to a plain object instead, covered by
|
||||||
|
// the "ndarray with _ArrayType_ "byte" is gated by the BJData draft
|
||||||
|
// version" section below
|
||||||
|
CHECK(json::to_bjdata(json::from_bjdata(v_B), true, true, json::bjdata_version_t::draft3) == v_B);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("ndarray with data not matching _ArrayType_ is written as an object")
|
SECTION("ndarray with data not matching _ArrayType_ is written as an object")
|
||||||
@@ -2629,8 +2634,10 @@ TEST_CASE("BJData")
|
|||||||
// the C++ API stores an int literal as number_integer, so _ArrayType_
|
// the C++ API stores an int literal as number_integer, so _ArrayType_
|
||||||
// names the wire type rather than the storage. Both storages have to
|
// names the wire type rather than the storage. Both storages have to
|
||||||
// produce the same typed array for every type.
|
// produce the same typed array for every type.
|
||||||
|
// "byte" is checked separately below since it additionally requires
|
||||||
|
// BJData Draft 3 to be selected explicitly (see GitHub issue #5404).
|
||||||
for (const char* type :
|
for (const char* type :
|
||||||
{"uint8", "int8", "uint16", "int16", "uint32", "int32", "uint64", "int64", "char", "byte"
|
{"uint8", "int8", "uint16", "int16", "uint32", "int32", "uint64", "int64", "char"
|
||||||
})
|
})
|
||||||
{
|
{
|
||||||
CAPTURE(type);
|
CAPTURE(type);
|
||||||
@@ -2641,6 +2648,14 @@ TEST_CASE("BJData")
|
|||||||
CHECK(from_text == json::to_bjdata(json({{"_ArrayType_", type}, {"_ArraySize_", {2, 3}}, {"_ArrayData_", {1, 2, 3, 4, 5, 6}}})));
|
CHECK(from_text == json::to_bjdata(json({{"_ArrayType_", type}, {"_ArraySize_", {2, 3}}, {"_ArrayData_", {1, 2, 3, 4, 5, 6}}})));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const std::string text = R"({"_ArrayType_":"byte","_ArraySize_":[2,3],"_ArrayData_":[1,2,3,4,5,6]})";
|
||||||
|
const auto from_text = json::to_bjdata(json::parse(text), true, true, json::bjdata_version_t::draft3);
|
||||||
|
CHECK(from_text.at(0) == '[');
|
||||||
|
CHECK(from_text == json::to_bjdata(json({{"_ArrayType_", "byte"}, {"_ArraySize_", {2, 3}}, {"_ArrayData_", {1, 2, 3, 4, 5, 6}}}),
|
||||||
|
true, true, json::bjdata_version_t::draft3));
|
||||||
|
}
|
||||||
|
|
||||||
// negative values under a signed type behave the same way
|
// negative values under a signed type behave the same way
|
||||||
const auto from_neg = json::to_bjdata(json::parse(R"({"_ArrayType_":"int32","_ArraySize_":[2],"_ArrayData_":[-5,7]})"));
|
const auto from_neg = json::to_bjdata(json::parse(R"({"_ArrayType_":"int32","_ArraySize_":[2],"_ArrayData_":[-5,7]})"));
|
||||||
CHECK(from_neg.at(0) == '[');
|
CHECK(from_neg.at(0) == '[');
|
||||||
@@ -2776,6 +2791,83 @@ TEST_CASE("BJData")
|
|||||||
CHECK(out_num.at(0) == '{');
|
CHECK(out_num.at(0) == '{');
|
||||||
CHECK(json::from_bjdata(out_num) == j_num);
|
CHECK(json::from_bjdata(out_num) == j_num);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("ndarray with out-of-range _ArrayData_ elements stays as object")
|
||||||
|
{
|
||||||
|
// each element is cast to the (possibly narrower) C++ type
|
||||||
|
// named by _ArrayType_ before being written; a value that
|
||||||
|
// does not fit that type would silently wrap instead of
|
||||||
|
// being reported, so such an object falls back to a plain
|
||||||
|
// object encoding that still round-trips (see GitHub issue #5403)
|
||||||
|
|
||||||
|
// an unsigned element that does not fit uint8
|
||||||
|
json const j_uint8 = json({{"_ArrayType_", "uint8"}, {"_ArraySize_", {2}}, {"_ArrayData_", {1, 256}}});
|
||||||
|
const auto out_uint8 = json::to_bjdata(j_uint8);
|
||||||
|
CHECK(out_uint8.at(0) == '{');
|
||||||
|
CHECK(json::from_bjdata(out_uint8) == j_uint8);
|
||||||
|
|
||||||
|
// a signed element that does not fit int8
|
||||||
|
json const j_int8 = json({{"_ArrayType_", "int8"}, {"_ArraySize_", {2}}, {"_ArrayData_", {1, 200}}});
|
||||||
|
const auto out_int8 = json::to_bjdata(j_int8);
|
||||||
|
CHECK(out_int8.at(0) == '{');
|
||||||
|
CHECK(json::from_bjdata(out_int8) == j_int8);
|
||||||
|
|
||||||
|
// a negative element is likewise out of range for an
|
||||||
|
// unsigned _ArrayType_
|
||||||
|
json const j_uint16_neg = json({{"_ArrayType_", "uint16"}, {"_ArraySize_", {2}}, {"_ArrayData_", {1, -1}}});
|
||||||
|
const auto out_uint16_neg = json::to_bjdata(j_uint16_neg);
|
||||||
|
CHECK(out_uint16_neg.at(0) == '{');
|
||||||
|
CHECK(json::from_bjdata(out_uint16_neg) == j_uint16_neg);
|
||||||
|
|
||||||
|
// a double element that overflows to infinity when narrowed
|
||||||
|
// to the "single" (float) precision named by _ArrayType_
|
||||||
|
json const j_single = json({{"_ArrayType_", "single"}, {"_ArraySize_", {2}}, {"_ArrayData_", {1.5, 1e40}}});
|
||||||
|
const auto out_single = json::to_bjdata(j_single);
|
||||||
|
CHECK(out_single.at(0) == '{');
|
||||||
|
CHECK(json::from_bjdata(out_single) == j_single);
|
||||||
|
|
||||||
|
// in-range boundary values still use the compact ndarray encoding
|
||||||
|
json const j_uint8_ok = json({{"_ArrayType_", "uint8"}, {"_ArraySize_", {2}}, {"_ArrayData_", {0, 255}}});
|
||||||
|
CHECK(json::to_bjdata(j_uint8_ok) == std::vector<uint8_t>({'[', '$', 'U', '#', '[', 'i', 2, ']', 0, 255}));
|
||||||
|
|
||||||
|
json const j_int8_ok = json({{"_ArrayType_", "int8"}, {"_ArraySize_", {2}}, {"_ArrayData_", {-128, 127}}});
|
||||||
|
CHECK(json::to_bjdata(j_int8_ok) == std::vector<uint8_t>({'[', '$', 'i', '#', '[', 'i', 2, ']', 0x80, 0x7F}));
|
||||||
|
|
||||||
|
json const j_single_ok = json({{"_ArrayType_", "single"}, {"_ArraySize_", {1}}, {"_ArrayData_", {1.5}}});
|
||||||
|
const auto out_single_ok = json::to_bjdata(j_single_ok);
|
||||||
|
CHECK(out_single_ok.at(0) == '[');
|
||||||
|
CHECK(json::from_bjdata(out_single_ok) == json({1.5f}));
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("ndarray with _ArrayType_ \"byte\" is gated by the BJData draft version")
|
||||||
|
{
|
||||||
|
// the 'B' (byte) marker used by _ArrayType_ "byte" is only defined
|
||||||
|
// by BJData Draft 3; Draft 2 (the default) has no such marker, so
|
||||||
|
// emitting it unconditionally produced a stream that a Draft 2
|
||||||
|
// reader could not parse as intended (see GitHub issue #5404).
|
||||||
|
// Two dimensions are used so that a successfully written ndarray
|
||||||
|
// round-trips back into the annotated object (a single dimension
|
||||||
|
// is, by the BJData ndarray convention, read back as a plain
|
||||||
|
// binary value rather than the annotated object, same as every
|
||||||
|
// other single-dimension ndarray of a non-"byte" type is read
|
||||||
|
// back as a plain array instead of the annotated object).
|
||||||
|
json const j_byte = json({{"_ArrayType_", "byte"}, {"_ArraySize_", {2, 3}}, {"_ArrayData_", {1, 2, 3, 4, 5, 6}}});
|
||||||
|
|
||||||
|
// default (Draft 2): falls back to a plain object and round-trips
|
||||||
|
const auto out_draft2 = json::to_bjdata(j_byte);
|
||||||
|
CHECK(out_draft2.at(0) == '{');
|
||||||
|
CHECK(json::from_bjdata(out_draft2) == j_byte);
|
||||||
|
|
||||||
|
// explicit Draft 2: same as the default
|
||||||
|
const auto out_draft2_explicit = json::to_bjdata(j_byte, true, true, json::bjdata_version_t::draft2);
|
||||||
|
CHECK(out_draft2_explicit.at(0) == '{');
|
||||||
|
CHECK(json::from_bjdata(out_draft2_explicit) == j_byte);
|
||||||
|
|
||||||
|
// Draft 3 explicitly selected: still uses the compact 'B' ndarray encoding
|
||||||
|
const auto out_draft3 = json::to_bjdata(j_byte, true, true, json::bjdata_version_t::draft3);
|
||||||
|
CHECK(out_draft3 == std::vector<uint8_t>({'[', '$', 'B', '#', '[', '$', 'i', '#', 'i', 2, 2, 3, 1, 2, 3, 4, 5, 6}));
|
||||||
|
CHECK(json::from_bjdata(out_draft3) == j_byte);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2035,58 +2035,6 @@ TEST_CASE("CBOR definite length equal to the indefinite-length sentinel")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("CBOR indefinite-length strings do not recurse per chunk")
|
|
||||||
{
|
|
||||||
// Reading an indefinite-length string or byte array used to call itself
|
|
||||||
// once per chunk, so a payload of repeated 0x7F (or 0x5F) bytes exhausted
|
|
||||||
// the call stack before any of the input was rejected. The open levels are
|
|
||||||
// counted now, and the levels below prove the reader still reads the same
|
|
||||||
// values and reports the same errors at the same byte offsets.
|
|
||||||
json _;
|
|
||||||
|
|
||||||
SECTION("many open levels are reported, not crashed on")
|
|
||||||
{
|
|
||||||
const std::vector<uint8_t> input(200000, 0x7F);
|
|
||||||
CHECK_THROWS_WITH_AS(_ = json::from_cbor(input), "[json.exception.parse_error.110] parse error at byte 200001: syntax error while parsing CBOR string: unexpected end of input", json::parse_error&);
|
|
||||||
CHECK(json::from_cbor(input, true, false).is_discarded());
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("many open levels are reported, not crashed on (binary)")
|
|
||||||
{
|
|
||||||
const std::vector<uint8_t> input(200000, 0x5F);
|
|
||||||
CHECK_THROWS_WITH_AS(_ = json::from_cbor(input), "[json.exception.parse_error.110] parse error at byte 200001: syntax error while parsing CBOR binary: unexpected end of input", json::parse_error&);
|
|
||||||
CHECK(json::from_cbor(input, true, false).is_discarded());
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("chunks are still concatenated")
|
|
||||||
{
|
|
||||||
CHECK(json::from_cbor(std::vector<uint8_t>({0x7F, 0xFF})) == json(""));
|
|
||||||
CHECK(json::from_cbor(std::vector<uint8_t>({0x7F, 0x61, 0x61, 0xFF})) == json("a"));
|
|
||||||
// nested indefinite-length strings are concatenated across levels
|
|
||||||
CHECK(json::from_cbor(std::vector<uint8_t>({0x7F, 0x7F, 0x61, 0x61, 0xFF, 0x61, 0x62, 0xFF})) == json("ab"));
|
|
||||||
CHECK(json::from_cbor(std::vector<uint8_t>({0x7F, 0x7F, 0x7F, 0x61, 0x7A, 0xFF, 0xFF, 0xFF})) == json("z"));
|
|
||||||
CHECK(json::from_cbor(std::vector<uint8_t>({0xA1, 0x7F, 0x61, 0x61, 0xFF, 0x01})) == json({{"a", 1}}));
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("chunks are still concatenated (binary)")
|
|
||||||
{
|
|
||||||
CHECK(json::from_cbor(std::vector<uint8_t>({0x5F, 0x41, 0x61, 0xFF})) == json::binary({0x61}));
|
|
||||||
CHECK(json::from_cbor(std::vector<uint8_t>({0x5F, 0x5F, 0x41, 0x61, 0xFF, 0x41, 0x62, 0xFF})) == json::binary({0x61, 0x62}));
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("a chunk that is not a string is still rejected")
|
|
||||||
{
|
|
||||||
CHECK_THROWS_WITH_AS(_ = json::from_cbor(std::vector<uint8_t>({0x7F, 0x7F, 0x00})), "[json.exception.parse_error.113] parse error at byte 3: syntax error while parsing CBOR string: expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x00", json::parse_error&);
|
|
||||||
CHECK_THROWS_WITH_AS(_ = json::from_cbor(std::vector<uint8_t>({0x5F, 0x5F, 0x00})), "[json.exception.parse_error.113] parse error at byte 3: syntax error while parsing CBOR binary: expected length specification (0x40-0x5B) or indefinite binary array type (0x5F); last byte: 0x00", json::parse_error&);
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("a break marker outside an indefinite-length string is not a string")
|
|
||||||
{
|
|
||||||
// 0xFF only closes a string that was opened; on its own it is not one
|
|
||||||
CHECK_THROWS_WITH_AS(_ = json::from_cbor(std::vector<uint8_t>({0xA1, 0xFF, 0x01})), "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing CBOR string: expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0xFF", json::parse_error&);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("CBOR roundtrips" * doctest::skip())
|
TEST_CASE("CBOR roundtrips" * doctest::skip())
|
||||||
{
|
{
|
||||||
SECTION("input from flynn")
|
SECTION("input from flynn")
|
||||||
|
|||||||
Reference in New Issue
Block a user