mirror of
https://github.com/nlohmann/json.git
synced 2025-07-28 12:02:00 +03:00
BSON: Support objects with string members
This commit is contained in:
@ -6123,7 +6123,7 @@ class binary_reader
|
||||
/*!
|
||||
@return whether array creation completed
|
||||
*/
|
||||
bool get_bson_str(string_t& result)
|
||||
bool get_bson_cstr(string_t& result)
|
||||
{
|
||||
bool success = true;
|
||||
generate_until(std::back_inserter(result), [](char c)
|
||||
@ -6132,7 +6132,7 @@ class binary_reader
|
||||
}, [this, &success]
|
||||
{
|
||||
get();
|
||||
if (JSON_UNLIKELY(unexpect_eof()))
|
||||
if (JSON_UNLIKELY(not unexpect_eof()))
|
||||
{
|
||||
success = false;
|
||||
}
|
||||
@ -6156,20 +6156,33 @@ class binary_reader
|
||||
{
|
||||
switch (entry_type)
|
||||
{
|
||||
case 0x01:
|
||||
case 0x01: // double
|
||||
{
|
||||
string_t key;
|
||||
get_bson_str(key);
|
||||
get_bson_cstr(key);
|
||||
sax->key(key);
|
||||
double number;
|
||||
get_number_little_endian(number);
|
||||
sax->number_float(static_cast<number_float_t>(number), "");
|
||||
}
|
||||
break;
|
||||
case 0x08:
|
||||
case 0x02: // string
|
||||
{
|
||||
string_t key;
|
||||
get_bson_str(key);
|
||||
get_bson_cstr(key);
|
||||
sax->key(key);
|
||||
std::int32_t len;
|
||||
string_t value;
|
||||
get_number_little_endian(len);
|
||||
get_string(len - 1ul, value);
|
||||
get();
|
||||
sax->string(value);
|
||||
}
|
||||
break;
|
||||
case 0x08: // boolean
|
||||
{
|
||||
string_t key;
|
||||
get_bson_cstr(key);
|
||||
sax->key(key);
|
||||
sax->boolean(static_cast<bool>(get()));
|
||||
}
|
||||
@ -8481,7 +8494,7 @@ class binary_writer
|
||||
|
||||
std::size_t write_bson_double(const typename BasicJsonType::string_t& name, const BasicJsonType& j)
|
||||
{
|
||||
oa->write_character(static_cast<CharType>(0x01)); // boolean
|
||||
oa->write_character(static_cast<CharType>(0x01)); // double
|
||||
oa->write_characters(
|
||||
reinterpret_cast<const CharType*>(name.c_str()),
|
||||
name.size() + 1u);
|
||||
@ -8489,6 +8502,21 @@ class binary_writer
|
||||
return /*id*/ 1ul + name.size() + 1u + /*double value*/ 8u;
|
||||
}
|
||||
|
||||
std::size_t write_bson_string(const typename BasicJsonType::string_t& name, const BasicJsonType& j)
|
||||
{
|
||||
oa->write_character(static_cast<CharType>(0x02)); // string (UTF-8)
|
||||
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.string->size() + 1ul));
|
||||
oa->write_characters(
|
||||
reinterpret_cast<const CharType*>(j.m_value.string->c_str()),
|
||||
j.m_value.string->size() + 1);
|
||||
|
||||
return /*id*/ 1ul + name.size() + 1ul + sizeof(std::int32_t) + j.m_value.string->size() + 1ul;
|
||||
}
|
||||
|
||||
std::size_t write_bson_object_entry(const typename BasicJsonType::string_t& name, const BasicJsonType& j)
|
||||
{
|
||||
switch (j.type())
|
||||
@ -8500,6 +8528,8 @@ class binary_writer
|
||||
return write_bson_boolean(name, j);
|
||||
case value_t::number_float:
|
||||
return write_bson_double(name, j);
|
||||
case value_t::string:
|
||||
return write_bson_string(name, j);
|
||||
};
|
||||
|
||||
return 0ul;
|
||||
|
Reference in New Issue
Block a user