mirror of
https://github.com/nlohmann/json.git
synced 2026-10-24 06:48:55 +00:00
2.1.4: write_bytes helper for the byte-string write sites (#5891)
The 13 oa.write_characters(reinterpret_cast<const CharType*>(x.data()), n) sites now call write_bytes(const void*, size_t); copy_characters sites and the buffer-based writes are untouched. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
1 file changed
+20
-27
@@ -311,9 +311,7 @@ class binary_writer
|
|||||||
write_cbor_head(0x40, N);
|
write_cbor_head(0x40, N);
|
||||||
|
|
||||||
// step 2: write each element
|
// step 2: write each element
|
||||||
oa.write_characters(
|
write_bytes(j.m_data.m_value.binary->data(), N);
|
||||||
reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
|
|
||||||
N);
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -526,9 +524,7 @@ class binary_writer
|
|||||||
}
|
}
|
||||||
|
|
||||||
// step 2: write the byte string
|
// step 2: write the byte string
|
||||||
oa.write_characters(
|
write_bytes(j.m_data.m_value.binary->data(), N);
|
||||||
reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
|
|
||||||
N);
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -676,9 +672,7 @@ class binary_writer
|
|||||||
|
|
||||||
if (use_type)
|
if (use_type)
|
||||||
{
|
{
|
||||||
oa.write_characters(
|
write_bytes(j.m_data.m_value.binary->data(), j.m_data.m_value.binary->size());
|
||||||
reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
|
|
||||||
j.m_data.m_value.binary->size());
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -928,6 +922,13 @@ class binary_writer
|
|||||||
JSON_THROW(other_error::create(exception_id::size_marker_required, "use_type requires use_size = true", &j));
|
JSON_THROW(other_error::create(exception_id::size_marker_required, "use_type requires use_size = true", &j));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief write @a size bytes starting at @a data to the output adapter
|
||||||
|
JSON_HEDLEY_ALWAYS_INLINE
|
||||||
|
void write_bytes(const void* data, const std::size_t size)
|
||||||
|
{
|
||||||
|
oa.write_characters(static_cast<const CharType*>(data), size);
|
||||||
|
}
|
||||||
|
|
||||||
/// @brief write the MessagePack prefix of an array or object with @a N
|
/// @brief write the MessagePack prefix of an array or object with @a N
|
||||||
/// elements: the fix marker @a fix_marker | n, or @a marker_16 or
|
/// elements: the fix marker @a fix_marker | n, or @a marker_16 or
|
||||||
/// @a marker_32 followed by the length
|
/// @a marker_32 followed by the length
|
||||||
@@ -1497,13 +1498,13 @@ class binary_writer
|
|||||||
|
|
||||||
if (JSON_HEDLEY_LIKELY(error_handler == error_handler_t::keep || error_handler == error_handler_t::strict))
|
if (JSON_HEDLEY_LIKELY(error_handler == error_handler_t::keep || error_handler == error_handler_t::strict))
|
||||||
{
|
{
|
||||||
oa.write_characters(reinterpret_cast<const CharType*>(name.data()), name.size());
|
write_bytes(name.data(), name.size());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
string_t storage;
|
string_t storage;
|
||||||
const string_t& written = sanitize_utf8_for_bson_write(name, storage);
|
const string_t& written = sanitize_utf8_for_bson_write(name, storage);
|
||||||
oa.write_characters(reinterpret_cast<const CharType*>(written.data()), written.size());
|
write_bytes(written.data(), written.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
// the terminating null byte is written explicitly rather than taken
|
// the terminating null byte is written explicitly rather than taken
|
||||||
@@ -1583,9 +1584,7 @@ class binary_writer
|
|||||||
const string_t& written = sanitize_utf8_for_bson_write(value, storage);
|
const string_t& written = sanitize_utf8_for_bson_write(value, storage);
|
||||||
|
|
||||||
write_number<std::int32_t>(to_bson_length(written.size() + 1ul), true);
|
write_number<std::int32_t>(to_bson_length(written.size() + 1ul), true);
|
||||||
oa.write_characters(
|
write_bytes(written.data(), written.size());
|
||||||
reinterpret_cast<const CharType*>(written.data()),
|
|
||||||
written.size());
|
|
||||||
// the terminating null byte is written explicitly rather than taken
|
// the terminating null byte is written explicitly rather than taken
|
||||||
// from the buffer, so that string_t::data() need not be null-terminated
|
// from the buffer, so that string_t::data() need not be null-terminated
|
||||||
oa.write_character(to_char_type(0x00));
|
oa.write_character(to_char_type(0x00));
|
||||||
@@ -1691,7 +1690,7 @@ class binary_writer
|
|||||||
|
|
||||||
write_number(value.has_subtype() ? static_cast<std::uint8_t>(value.subtype()) : static_cast<std::uint8_t>(0x00));
|
write_number(value.has_subtype() ? static_cast<std::uint8_t>(value.subtype()) : static_cast<std::uint8_t>(0x00));
|
||||||
|
|
||||||
oa.write_characters(reinterpret_cast<const CharType*>(value.data()), value.size());
|
write_bytes(value.data(), value.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@@ -1903,7 +1902,7 @@ class binary_writer
|
|||||||
// the terminating null byte is written explicitly rather than
|
// the terminating null byte is written explicitly rather than
|
||||||
// taken from the buffer, so that string_t::data() need not be
|
// taken from the buffer, so that string_t::data() need not be
|
||||||
// null-terminated
|
// null-terminated
|
||||||
oa.write_characters(reinterpret_cast<const CharType*>(value.data()), value.size());
|
write_bytes(value.data(), value.size());
|
||||||
oa.write_character(to_char_type(0x00));
|
oa.write_character(to_char_type(0x00));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -1920,7 +1919,7 @@ class binary_writer
|
|||||||
p += sizeof(std::int32_t);
|
p += sizeof(std::int32_t);
|
||||||
*p++ = value.has_subtype() ? to_char_type(static_cast<std::uint8_t>(value.subtype())) : to_char_type(0x00);
|
*p++ = value.has_subtype() ? to_char_type(static_cast<std::uint8_t>(value.subtype())) : to_char_type(0x00);
|
||||||
oa.commit(static_cast<std::size_t>(p - out));
|
oa.commit(static_cast<std::size_t>(p - out));
|
||||||
oa.write_characters(reinterpret_cast<const CharType*>(value.data()), value.size());
|
write_bytes(value.data(), value.size());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3108,9 +3107,7 @@ class binary_writer
|
|||||||
write_cbor_head(0x60, sanitized.size());
|
write_cbor_head(0x60, sanitized.size());
|
||||||
|
|
||||||
// step 2: write the string
|
// step 2: write the string
|
||||||
oa.write_characters(
|
write_bytes(sanitized.data(), sanitized.size());
|
||||||
reinterpret_cast<const CharType*>(sanitized.data()),
|
|
||||||
sanitized.size());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/////////////
|
/////////////
|
||||||
@@ -3191,9 +3188,7 @@ class binary_writer
|
|||||||
}
|
}
|
||||||
|
|
||||||
// step 2: write the string
|
// step 2: write the string
|
||||||
oa.write_characters(
|
write_bytes(sanitized.data(), sanitized.size());
|
||||||
reinterpret_cast<const CharType*>(sanitized.data()),
|
|
||||||
sanitized.size());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////
|
////////////
|
||||||
@@ -3476,9 +3471,7 @@ class binary_writer
|
|||||||
oa.write_character(to_char_type('S'));
|
oa.write_character(to_char_type('S'));
|
||||||
}
|
}
|
||||||
write_number_with_ubjson_prefix(sanitized.size(), true, use_bjdata);
|
write_number_with_ubjson_prefix(sanitized.size(), true, use_bjdata);
|
||||||
oa.write_characters(
|
write_bytes(sanitized.data(), sanitized.size());
|
||||||
reinterpret_cast<const CharType*>(sanitized.data()),
|
|
||||||
sanitized.size());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@@ -4754,7 +4747,7 @@ class binary_writer
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
oa.write_characters(reinterpret_cast<const CharType*>(s.data()), s.size());
|
write_bytes(s.data(), s.size());
|
||||||
string_open = true;
|
string_open = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user