Skip to content

Commit

Permalink
BJData optimized binary array type
Browse files Browse the repository at this point in the history
  • Loading branch information
nebkat committed Jun 3, 2024
1 parent 9cca280 commit 37066db
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
8 changes: 8 additions & 0 deletions include/nlohmann/detail/input/binary_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2311,6 +2311,7 @@ class binary_reader
return sax->null();

case 'U':
case 'B':
{
std::uint8_t number{};
return get_number(input_format, number) && sax->number_unsigned(number);
Expand Down Expand Up @@ -2532,6 +2533,13 @@ class binary_reader
return (sax->end_array() && sax->end_object());
}

// If BJData type marker is 'B' decode as binary
if (input_format == input_format_t::bjdata && size_and_type.first != npos && size_and_type.second == 'B')
{
binary_t result;
return get_binary(input_format, size_and_type.first, result) && sax->binary(result);
}

if (size_and_type.first != npos)
{
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(size_and_type.first)))
Expand Down
4 changes: 2 additions & 2 deletions include/nlohmann/detail/output/binary_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ class binary_writer
{
JSON_ASSERT(use_count);
oa->write_character(to_char_type('$'));
oa->write_character('U');
oa->write_character(use_bjdata ? 'B' : 'U');
}

if (use_count)
Expand All @@ -870,7 +870,7 @@ class binary_writer
{
for (size_t i = 0; i < j.m_data.m_value.binary->size(); ++i)
{
oa->write_character(to_char_type('U'));
oa->write_character(to_char_type(use_bjdata ? 'B' : 'U'));
oa->write_character(j.m_data.m_value.binary->data()[i]);
}
}
Expand Down
12 changes: 10 additions & 2 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11456,6 +11456,7 @@ class binary_reader
return sax->null();

case 'U':
case 'B':
{
std::uint8_t number{};
return get_number(input_format, number) && sax->number_unsigned(number);
Expand Down Expand Up @@ -11677,6 +11678,13 @@ class binary_reader
return (sax->end_array() && sax->end_object());
}

// If BJData type marker is 'B' decode as binary
if (input_format == input_format_t::bjdata && size_and_type.first != npos && size_and_type.second == 'B')
{
binary_t result;
return get_binary(input_format, size_and_type.first, result) && sax->binary(result);
}

if (size_and_type.first != npos)
{
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(size_and_type.first)))
Expand Down Expand Up @@ -15890,7 +15898,7 @@ class binary_writer
{
JSON_ASSERT(use_count);
oa->write_character(to_char_type('$'));
oa->write_character('U');
oa->write_character(use_bjdata ? 'B' : 'U');
}

if (use_count)
Expand All @@ -15909,7 +15917,7 @@ class binary_writer
{
for (size_t i = 0; i < j.m_data.m_value.binary->size(); ++i)
{
oa->write_character(to_char_type('U'));
oa->write_character(to_char_type(use_bjdata ? 'B' : 'U'));
oa->write_character(j.m_data.m_value.binary->data()[i]);
}
}
Expand Down

0 comments on commit 37066db

Please sign in to comment.