1
0
mirror of https://github.com/nlohmann/json synced 2024-11-22 20:10:06 +00:00

♻️ fix CBOR and BSON

This commit is contained in:
Niels Lohmann 2021-08-06 14:36:38 +02:00
parent 046df035fa
commit b7db1d68d9
No known key found for this signature in database
GPG Key ID: 7F3CEA63AE251B69
3 changed files with 48 additions and 12 deletions

View File

@ -281,9 +281,27 @@ class binary_writer
case value_t::binary: case value_t::binary:
{ {
if (j.m_value.binary->has_subtype()) if (j.m_value.binary->has_subtype())
{
if (j.m_value.binary->subtype() <= (std::numeric_limits<std::uint8_t>::max)())
{ {
write_number(static_cast<std::uint8_t>(0xd8)); write_number(static_cast<std::uint8_t>(0xd8));
write_number(j.m_value.binary->subtype()); write_number(static_cast<std::uint8_t>(j.m_value.binary->subtype()));
}
else if (j.m_value.binary->subtype() <= (std::numeric_limits<std::uint16_t>::max)())
{
write_number(static_cast<std::uint8_t>(0xd9));
write_number(static_cast<std::uint16_t>(j.m_value.binary->subtype()));
}
else if (j.m_value.binary->subtype() <= (std::numeric_limits<std::uint32_t>::max)())
{
write_number(static_cast<std::uint8_t>(0xda));
write_number(static_cast<std::uint32_t>(j.m_value.binary->subtype()));
}
else if (j.m_value.binary->subtype() <= (std::numeric_limits<std::uint64_t>::max)())
{
write_number(static_cast<std::uint8_t>(0xdb));
write_number(static_cast<std::uint64_t>(j.m_value.binary->subtype()));
}
} }
// step 1: write control byte and the binary array size // step 1: write control byte and the binary array size
@ -1096,7 +1114,7 @@ class binary_writer
write_bson_entry_header(name, 0x05); write_bson_entry_header(name, 0x05);
write_number<std::int32_t, true>(static_cast<std::int32_t>(value.size())); write_number<std::int32_t, true>(static_cast<std::int32_t>(value.size()));
write_number(value.has_subtype() ? value.subtype() : std::uint8_t(0x00)); write_number(value.has_subtype() ? static_cast<std::uint8_t>(value.subtype()) : std::uint8_t(0x00));
oa->write_characters(reinterpret_cast<const CharType*>(value.data()), value.size()); oa->write_characters(reinterpret_cast<const CharType*>(value.data()), value.size());
} }

View File

@ -1817,7 +1817,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
@since version 3.8.0 @since version 3.8.0
*/ */
JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_WARN_UNUSED_RESULT
static basic_json binary(const typename binary_t::container_type& init, std::uint8_t subtype) static basic_json binary(const typename binary_t::container_type& init, typename binary_t::subtype_type subtype)
{ {
auto res = basic_json(); auto res = basic_json();
res.m_type = value_t::binary; res.m_type = value_t::binary;
@ -1835,9 +1835,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
return res; return res;
} }
/// @copydoc binary(const typename binary_t::container_type&, std::uint8_t) /// @copydoc binary(const typename binary_t::container_type&, typename binary_t::subtype_type)
JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_WARN_UNUSED_RESULT
static basic_json binary(typename binary_t::container_type&& init, std::uint8_t subtype) static basic_json binary(typename binary_t::container_type&& init, typename binary_t::subtype_type subtype)
{ {
auto res = basic_json(); auto res = basic_json();
res.m_type = value_t::binary; res.m_type = value_t::binary;

View File

@ -13584,9 +13584,27 @@ class binary_writer
case value_t::binary: case value_t::binary:
{ {
if (j.m_value.binary->has_subtype()) if (j.m_value.binary->has_subtype())
{
if (j.m_value.binary->subtype() <= (std::numeric_limits<std::uint8_t>::max)())
{ {
write_number(static_cast<std::uint8_t>(0xd8)); write_number(static_cast<std::uint8_t>(0xd8));
write_number(j.m_value.binary->subtype()); write_number(static_cast<std::uint8_t>(j.m_value.binary->subtype()));
}
else if (j.m_value.binary->subtype() <= (std::numeric_limits<std::uint16_t>::max)())
{
write_number(static_cast<std::uint8_t>(0xd9));
write_number(static_cast<std::uint16_t>(j.m_value.binary->subtype()));
}
else if (j.m_value.binary->subtype() <= (std::numeric_limits<std::uint32_t>::max)())
{
write_number(static_cast<std::uint8_t>(0xda));
write_number(static_cast<std::uint32_t>(j.m_value.binary->subtype()));
}
else if (j.m_value.binary->subtype() <= (std::numeric_limits<std::uint64_t>::max)())
{
write_number(static_cast<std::uint8_t>(0xdb));
write_number(static_cast<std::uint64_t>(j.m_value.binary->subtype()));
}
} }
// step 1: write control byte and the binary array size // step 1: write control byte and the binary array size
@ -14399,7 +14417,7 @@ class binary_writer
write_bson_entry_header(name, 0x05); write_bson_entry_header(name, 0x05);
write_number<std::int32_t, true>(static_cast<std::int32_t>(value.size())); write_number<std::int32_t, true>(static_cast<std::int32_t>(value.size()));
write_number(value.has_subtype() ? value.subtype() : std::uint8_t(0x00)); write_number(value.has_subtype() ? static_cast<std::uint8_t>(value.subtype()) : std::uint8_t(0x00));
oa->write_characters(reinterpret_cast<const CharType*>(value.data()), value.size()); oa->write_characters(reinterpret_cast<const CharType*>(value.data()), value.size());
} }
@ -18911,7 +18929,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
@since version 3.8.0 @since version 3.8.0
*/ */
JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_WARN_UNUSED_RESULT
static basic_json binary(const typename binary_t::container_type& init, std::uint8_t subtype) static basic_json binary(const typename binary_t::container_type& init, typename binary_t::subtype_type subtype)
{ {
auto res = basic_json(); auto res = basic_json();
res.m_type = value_t::binary; res.m_type = value_t::binary;
@ -18929,9 +18947,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
return res; return res;
} }
/// @copydoc binary(const typename binary_t::container_type&, std::uint8_t) /// @copydoc binary(const typename binary_t::container_type&, typename binary_t::subtype_type)
JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_WARN_UNUSED_RESULT
static basic_json binary(typename binary_t::container_type&& init, std::uint8_t subtype) static basic_json binary(typename binary_t::container_type&& init, typename binary_t::subtype_type subtype)
{ {
auto res = basic_json(); auto res = basic_json();
res.m_type = value_t::binary; res.m_type = value_t::binary;