Take the output adapter by reference at the serializer ctor

Per review: the serializer still holds the adapter as a non-owning
pointer, but the constructor now takes output_adapter_protocol<char>&
and takes its address internally, so every call site passes a
reference. A reference cannot be null and reads as a borrow, which
makes the lifetime contract harder to get wrong than handing over a
raw pointer. The stored member and the write path are unchanged.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XAYM1qhSA2FDaDcGfPW3fG
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann
2026-09-03 07:42:14 +02:00
co-authored by Claude
parent 386dc225fc
commit 5c331d1fab
4 changed files with 9 additions and 9 deletions
@@ -68,9 +68,9 @@ class serializer
@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_protocol<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(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)))
+2 -2
View File
@@ -1342,7 +1342,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
{ {
string_t result; string_t result;
detail::output_string_adapter<char, string_t> string_adapter(result); detail::output_string_adapter<char, string_t> string_adapter(result);
serializer s(&string_adapter, indent_char, error_handler); serializer s(string_adapter, indent_char, error_handler);
if (indent >= 0) if (indent >= 0)
{ {
@@ -4057,7 +4057,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
// do the actual serialization // do the actual serialization
detail::output_stream_adapter<char> stream_adapter(o); detail::output_stream_adapter<char> stream_adapter(o);
serializer s(&stream_adapter, o.fill()); 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;
} }
+4 -4
View File
@@ -21128,9 +21128,9 @@ class serializer
@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_protocol<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(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)))
@@ -24453,7 +24453,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
{ {
string_t result; string_t result;
detail::output_string_adapter<char, string_t> string_adapter(result); detail::output_string_adapter<char, string_t> string_adapter(result);
serializer s(&string_adapter, indent_char, error_handler); serializer s(string_adapter, indent_char, error_handler);
if (indent >= 0) if (indent >= 0)
{ {
@@ -27168,7 +27168,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
// do the actual serialization // do the actual serialization
detail::output_stream_adapter<char> stream_adapter(o); detail::output_stream_adapter<char> stream_adapter(o);
serializer s(&stream_adapter, o.fill()); 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;
} }
+1 -1
View File
@@ -99,7 +99,7 @@ void check_escaped(const char* original, const char* escaped, const bool ensure_
{ {
std::stringstream ss; std::stringstream ss;
nlohmann::detail::output_stream_adapter<char> adapter(ss); nlohmann::detail::output_stream_adapter<char> adapter(ss);
json::serializer s(&adapter, ' '); 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);