diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp index 2021278ab..7c5a60a9b 100644 --- a/include/nlohmann/detail/output/binary_writer.hpp +++ b/include/nlohmann/detail/output/binary_writer.hpp @@ -1173,8 +1173,7 @@ class binary_writer if (use_count) { - oa.write_character(to_char_type('#')); - write_number_with_ubjson_prefix(j.m_data.m_value.array->size(), true, use_bjdata); + write_ubjson_count(j.m_data.m_value.array->size(), use_bjdata); } return !use_count; @@ -1213,8 +1212,7 @@ class binary_writer if (use_count) { - oa.write_character(to_char_type('#')); - write_number_with_ubjson_prefix(j.m_data.m_value.object->size(), true, use_bjdata); + write_ubjson_count(j.m_data.m_value.object->size(), use_bjdata); } return !use_count; @@ -3410,6 +3408,23 @@ class binary_writer } } + /// write the count marker `#` and the number of elements of a container as + /// an integer; a count of up to 127, which is `i` and a byte, in one reservation + void write_ubjson_count(const std::size_t count, const bool use_bjdata) + { + if (JSON_HEDLEY_LIKELY(count <= 127)) + { + CharType* const out = oa.reserve(3); + out[0] = to_char_type('#'); + out[1] = to_char_type('i'); + out[2] = to_char_type(static_cast(count)); + oa.commit(3); + return; + } + oa.write_character(to_char_type('#')); + write_number_with_ubjson_prefix(count, true, use_bjdata); + } + /*! @brief write a UBJSON/BJData string or object key: the optional string marker `S`, the length as an integer with its marker, and the characters diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 087ce0032..e7aa00d78 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -26023,8 +26023,7 @@ class binary_writer if (use_count) { - oa.write_character(to_char_type('#')); - write_number_with_ubjson_prefix(j.m_data.m_value.array->size(), true, use_bjdata); + write_ubjson_count(j.m_data.m_value.array->size(), use_bjdata); } return !use_count; @@ -26063,8 +26062,7 @@ class binary_writer if (use_count) { - oa.write_character(to_char_type('#')); - write_number_with_ubjson_prefix(j.m_data.m_value.object->size(), true, use_bjdata); + write_ubjson_count(j.m_data.m_value.object->size(), use_bjdata); } return !use_count; @@ -28260,6 +28258,23 @@ class binary_writer } } + /// write the count marker `#` and the number of elements of a container as + /// an integer; a count of up to 127, which is `i` and a byte, in one reservation + void write_ubjson_count(const std::size_t count, const bool use_bjdata) + { + if (JSON_HEDLEY_LIKELY(count <= 127)) + { + CharType* const out = oa.reserve(3); + out[0] = to_char_type('#'); + out[1] = to_char_type('i'); + out[2] = to_char_type(static_cast(count)); + oa.commit(3); + return; + } + oa.write_character(to_char_type('#')); + write_number_with_ubjson_prefix(count, true, use_bjdata); + } + /*! @brief write a UBJSON/BJData string or object key: the optional string marker `S`, the length as an integer with its marker, and the characters