mirror of
https://github.com/nlohmann/json.git
synced 2025-07-28 12:02:00 +03:00
BSON: unsigned integers
This commit is contained in:
@ -8557,14 +8557,42 @@ class binary_writer
|
||||
|
||||
std::size_t write_bson_integer(const typename BasicJsonType::string_t& name, const BasicJsonType& j)
|
||||
{
|
||||
if (j.m_value.number_integer <= static_cast<uint64_t>((std::numeric_limits<int32_t>::max)()))
|
||||
auto n = j.m_value.number_integer;
|
||||
if ((std::numeric_limits<int32_t>::min)() <= n and n <= (std::numeric_limits<int32_t>::max)())
|
||||
{
|
||||
oa->write_character(static_cast<CharType>(0x10)); // int32
|
||||
oa->write_characters(
|
||||
reinterpret_cast<const CharType*>(name.c_str()),
|
||||
name.size() + 1u);
|
||||
|
||||
write_number_little_endian(static_cast<std::int32_t>(j.m_value.number_integer));
|
||||
write_number_little_endian(static_cast<std::int32_t>(n));
|
||||
|
||||
return /*id*/ 1ul + name.size() + 1ul + sizeof(std::int32_t);
|
||||
}
|
||||
else
|
||||
{
|
||||
oa->write_character(static_cast<CharType>(0x12)); // int64
|
||||
oa->write_characters(
|
||||
reinterpret_cast<const CharType*>(name.c_str()),
|
||||
name.size() + 1u);
|
||||
|
||||
write_number_little_endian(static_cast<std::int64_t>(j.m_value.number_integer));
|
||||
|
||||
return /*id*/ 1ul + name.size() + 1ul + sizeof(std::int64_t);
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t write_bson_unsigned(const typename BasicJsonType::string_t& name, const BasicJsonType& j)
|
||||
{
|
||||
auto n = j.m_value.number_integer;
|
||||
if (n <= static_cast<uint64_t>((std::numeric_limits<uint32_t>::max)()))
|
||||
{
|
||||
oa->write_character(static_cast<CharType>(0x10)); // int32
|
||||
oa->write_characters(
|
||||
reinterpret_cast<const CharType*>(name.c_str()),
|
||||
name.size() + 1u);
|
||||
|
||||
write_number_little_endian(static_cast<std::int32_t>(n));
|
||||
|
||||
return /*id*/ 1ul + name.size() + 1ul + sizeof(std::int32_t);
|
||||
}
|
||||
@ -8594,6 +8622,8 @@ class binary_writer
|
||||
return write_bson_double(name, j);
|
||||
case value_t::number_integer:
|
||||
return write_bson_integer(name, j);
|
||||
case value_t::number_unsigned:
|
||||
return write_bson_unsigned(name, j);
|
||||
case value_t::string:
|
||||
return write_bson_string(name, j);
|
||||
case value_t::null:
|
||||
|
Reference in New Issue
Block a user