mirror of
https://github.com/nlohmann/json.git
synced 2026-09-02 22:47:14 +00:00
Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d479d34677 | ||
|
|
09ae666612 | ||
|
|
78f71358c1 | ||
|
|
f707a2f997 | ||
|
|
3426a41391 |
@@ -63,13 +63,14 @@ class serializer
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
/*!
|
/*!
|
||||||
@param[in] s output stream to serialize to
|
@param[in] s output adapter to serialize to; not owned by the serializer,
|
||||||
|
so it must outlive it (it lives at the call site)
|
||||||
@param[in] ichar indentation character to use
|
@param[in] ichar indentation character to use
|
||||||
@param[in] error_handler_ how to react on decoding errors
|
@param[in] error_handler_ how to react on decoding errors
|
||||||
*/
|
*/
|
||||||
serializer(output_adapter_t<char> s, const char ichar,
|
serializer(output_adapter_protocol<char>& s, const char ichar,
|
||||||
error_handler_t error_handler_ = error_handler_t::strict)
|
error_handler_t error_handler_ = error_handler_t::strict)
|
||||||
: o(std::move(s))
|
: o(&s)
|
||||||
, loc(std::localeconv())
|
, loc(std::localeconv())
|
||||||
, thousands_sep(loc->thousands_sep == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->thousands_sep)))
|
, thousands_sep(loc->thousands_sep == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->thousands_sep)))
|
||||||
, decimal_point(loc->decimal_point == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->decimal_point)))
|
, decimal_point(loc->decimal_point == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->decimal_point)))
|
||||||
@@ -844,9 +845,21 @@ class serializer
|
|||||||
if (state == UTF8_ACCEPT)
|
if (state == UTF8_ACCEPT)
|
||||||
{
|
{
|
||||||
const auto* const data = reinterpret_cast<const unsigned char*>(s.data());
|
const auto* const data = reinterpret_cast<const unsigned char*>(s.data());
|
||||||
const std::size_t run = EnsureAscii
|
// A run can only be non-empty when the very first byte is one
|
||||||
? find_ascii_copyable_run(data + i, s.size() - i)
|
// the scanner may copy, so test that single byte before paying
|
||||||
: string_bulk_run(data + i, s.size() - i);
|
// for the scan. Without it, text whose characters all have to be
|
||||||
|
// escaped - CJK under ensure_ascii, where every byte is >= 0x80 -
|
||||||
|
// runs the scanner once per character only to be told zero.
|
||||||
|
const std::size_t run = [&]() -> std::size_t
|
||||||
|
{
|
||||||
|
if (EnsureAscii)
|
||||||
|
{
|
||||||
|
return is_ascii_copyable(data[i])
|
||||||
|
? find_ascii_copyable_run(data + i, s.size() - i)
|
||||||
|
: 0;
|
||||||
|
}
|
||||||
|
return string_bulk_run(data + i, s.size() - i);
|
||||||
|
}();
|
||||||
if (run != 0)
|
if (run != 0)
|
||||||
{
|
{
|
||||||
// emit any bytes still pending in string_buffer first to
|
// emit any bytes still pending in string_buffer first to
|
||||||
@@ -1677,8 +1690,8 @@ class serializer
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// the output of the serializer
|
/// the output of the serializer (non-owning; the adapter lives at the call site)
|
||||||
output_adapter_t<char> o = nullptr;
|
output_adapter_protocol<char>* o = nullptr;
|
||||||
|
|
||||||
/// a (hopefully) large enough character buffer
|
/// a (hopefully) large enough character buffer
|
||||||
std::array<char, 64> number_buffer{{}};
|
std::array<char, 64> number_buffer{{}};
|
||||||
|
|||||||
@@ -1341,7 +1341,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
const error_handler_t error_handler = error_handler_t::strict) const
|
const error_handler_t error_handler = error_handler_t::strict) const
|
||||||
{
|
{
|
||||||
string_t result;
|
string_t result;
|
||||||
serializer s(detail::output_adapter<char, string_t>(result), indent_char, error_handler);
|
detail::output_string_adapter<char, string_t> string_adapter(result);
|
||||||
|
serializer s(string_adapter, indent_char, error_handler);
|
||||||
|
|
||||||
if (indent >= 0)
|
if (indent >= 0)
|
||||||
{
|
{
|
||||||
@@ -4055,7 +4056,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
o.width(0);
|
o.width(0);
|
||||||
|
|
||||||
// do the actual serialization
|
// do the actual serialization
|
||||||
serializer s(detail::output_adapter<char>(o), o.fill());
|
detail::output_stream_adapter<char> stream_adapter(o);
|
||||||
|
serializer s(stream_adapter, o.fill());
|
||||||
s.dump(j, pretty_print, false, static_cast<unsigned int>(indentation));
|
s.dump(j, pretty_print, false, static_cast<unsigned int>(indentation));
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21123,13 +21123,14 @@ class serializer
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
/*!
|
/*!
|
||||||
@param[in] s output stream to serialize to
|
@param[in] s output adapter to serialize to; not owned by the serializer,
|
||||||
|
so it must outlive it (it lives at the call site)
|
||||||
@param[in] ichar indentation character to use
|
@param[in] ichar indentation character to use
|
||||||
@param[in] error_handler_ how to react on decoding errors
|
@param[in] error_handler_ how to react on decoding errors
|
||||||
*/
|
*/
|
||||||
serializer(output_adapter_t<char> s, const char ichar,
|
serializer(output_adapter_protocol<char>& s, const char ichar,
|
||||||
error_handler_t error_handler_ = error_handler_t::strict)
|
error_handler_t error_handler_ = error_handler_t::strict)
|
||||||
: o(std::move(s))
|
: o(&s)
|
||||||
, loc(std::localeconv())
|
, loc(std::localeconv())
|
||||||
, thousands_sep(loc->thousands_sep == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->thousands_sep)))
|
, thousands_sep(loc->thousands_sep == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->thousands_sep)))
|
||||||
, decimal_point(loc->decimal_point == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->decimal_point)))
|
, decimal_point(loc->decimal_point == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->decimal_point)))
|
||||||
@@ -21904,9 +21905,21 @@ class serializer
|
|||||||
if (state == UTF8_ACCEPT)
|
if (state == UTF8_ACCEPT)
|
||||||
{
|
{
|
||||||
const auto* const data = reinterpret_cast<const unsigned char*>(s.data());
|
const auto* const data = reinterpret_cast<const unsigned char*>(s.data());
|
||||||
const std::size_t run = EnsureAscii
|
// A run can only be non-empty when the very first byte is one
|
||||||
? find_ascii_copyable_run(data + i, s.size() - i)
|
// the scanner may copy, so test that single byte before paying
|
||||||
: string_bulk_run(data + i, s.size() - i);
|
// for the scan. Without it, text whose characters all have to be
|
||||||
|
// escaped - CJK under ensure_ascii, where every byte is >= 0x80 -
|
||||||
|
// runs the scanner once per character only to be told zero.
|
||||||
|
const std::size_t run = [&]() -> std::size_t
|
||||||
|
{
|
||||||
|
if (EnsureAscii)
|
||||||
|
{
|
||||||
|
return is_ascii_copyable(data[i])
|
||||||
|
? find_ascii_copyable_run(data + i, s.size() - i)
|
||||||
|
: 0;
|
||||||
|
}
|
||||||
|
return string_bulk_run(data + i, s.size() - i);
|
||||||
|
}();
|
||||||
if (run != 0)
|
if (run != 0)
|
||||||
{
|
{
|
||||||
// emit any bytes still pending in string_buffer first to
|
// emit any bytes still pending in string_buffer first to
|
||||||
@@ -22737,8 +22750,8 @@ class serializer
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// the output of the serializer
|
/// the output of the serializer (non-owning; the adapter lives at the call site)
|
||||||
output_adapter_t<char> o = nullptr;
|
output_adapter_protocol<char>* o = nullptr;
|
||||||
|
|
||||||
/// a (hopefully) large enough character buffer
|
/// a (hopefully) large enough character buffer
|
||||||
std::array<char, 64> number_buffer{{}};
|
std::array<char, 64> number_buffer{{}};
|
||||||
@@ -24440,7 +24453,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
const error_handler_t error_handler = error_handler_t::strict) const
|
const error_handler_t error_handler = error_handler_t::strict) const
|
||||||
{
|
{
|
||||||
string_t result;
|
string_t result;
|
||||||
serializer s(detail::output_adapter<char, string_t>(result), indent_char, error_handler);
|
detail::output_string_adapter<char, string_t> string_adapter(result);
|
||||||
|
serializer s(string_adapter, indent_char, error_handler);
|
||||||
|
|
||||||
if (indent >= 0)
|
if (indent >= 0)
|
||||||
{
|
{
|
||||||
@@ -27154,7 +27168,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
o.width(0);
|
o.width(0);
|
||||||
|
|
||||||
// do the actual serialization
|
// do the actual serialization
|
||||||
serializer s(detail::output_adapter<char>(o), o.fill());
|
detail::output_stream_adapter<char> stream_adapter(o);
|
||||||
|
serializer s(stream_adapter, o.fill());
|
||||||
s.dump(j, pretty_print, false, static_cast<unsigned int>(indentation));
|
s.dump(j, pretty_print, false, static_cast<unsigned int>(indentation));
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -98,7 +98,8 @@ void check_escaped(const char* original, const char* escaped = "", bool ensure_a
|
|||||||
void check_escaped(const char* original, const char* escaped, const bool ensure_ascii)
|
void check_escaped(const char* original, const char* escaped, const bool ensure_ascii)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
json::serializer s(nlohmann::detail::output_adapter<char>(ss), ' ');
|
nlohmann::detail::output_stream_adapter<char> adapter(ss);
|
||||||
|
json::serializer s(adapter, ' ');
|
||||||
s.dump_escaped(original, ensure_ascii);
|
s.dump_escaped(original, ensure_ascii);
|
||||||
s.flush(); // dump_escaped writes into the serializer's internal buffer
|
s.flush(); // dump_escaped writes into the serializer's internal buffer
|
||||||
CHECK(ss.str() == escaped);
|
CHECK(ss.str() == escaped);
|
||||||
|
|||||||
Reference in New Issue
Block a user