From c401d495a3140d5a74614ac0711b518dc528ecd7 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Tue, 1 Sep 2026 05:29:01 +0000 Subject: [PATCH] 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& 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 Claude-Session: https://claude.ai/code/session_01XAYM1qhSA2FDaDcGfPW3fG Signed-off-by: Niels Lohmann --- include/nlohmann/detail/output/serializer.hpp | 4 ++-- include/nlohmann/json.hpp | 4 ++-- single_include/nlohmann/json.hpp | 8 ++++---- tests/src/unit-convenience.cpp | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/include/nlohmann/detail/output/serializer.hpp b/include/nlohmann/detail/output/serializer.hpp index 4b9b63556..92795f089 100644 --- a/include/nlohmann/detail/output/serializer.hpp +++ b/include/nlohmann/detail/output/serializer.hpp @@ -68,9 +68,9 @@ class serializer @param[in] ichar indentation character to use @param[in] error_handler_ how to react on decoding errors */ - serializer(output_adapter_protocol* s, const char ichar, + serializer(output_adapter_protocol& s, const char ichar, error_handler_t error_handler_ = error_handler_t::strict) - : o(s) + : o(&s) , loc(std::localeconv()) , thousands_sep(loc->thousands_sep == nullptr ? '\0' : std::char_traits::to_char_type(* (loc->thousands_sep))) , decimal_point(loc->decimal_point == nullptr ? '\0' : std::char_traits::to_char_type(* (loc->decimal_point))) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 8a571b835..76452a4aa 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -1342,7 +1342,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec { string_t result; detail::output_string_adapter string_adapter(result); - serializer s(&string_adapter, indent_char, error_handler); + serializer s(string_adapter, indent_char, error_handler); if (indent >= 0) { @@ -4057,7 +4057,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec // do the actual serialization detail::output_stream_adapter stream_adapter(o); - serializer s(&stream_adapter, o.fill()); + serializer s(stream_adapter, o.fill()); s.dump(j, pretty_print, false, static_cast(indentation)); return o; } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 51c4f2287..50b1156a5 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -21048,9 +21048,9 @@ class serializer @param[in] ichar indentation character to use @param[in] error_handler_ how to react on decoding errors */ - serializer(output_adapter_protocol* s, const char ichar, + serializer(output_adapter_protocol& s, const char ichar, error_handler_t error_handler_ = error_handler_t::strict) - : o(s) + : o(&s) , loc(std::localeconv()) , thousands_sep(loc->thousands_sep == nullptr ? '\0' : std::char_traits::to_char_type(* (loc->thousands_sep))) , decimal_point(loc->decimal_point == nullptr ? '\0' : std::char_traits::to_char_type(* (loc->decimal_point))) @@ -24362,7 +24362,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec { string_t result; detail::output_string_adapter string_adapter(result); - serializer s(&string_adapter, indent_char, error_handler); + serializer s(string_adapter, indent_char, error_handler); if (indent >= 0) { @@ -27077,7 +27077,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec // do the actual serialization detail::output_stream_adapter stream_adapter(o); - serializer s(&stream_adapter, o.fill()); + serializer s(stream_adapter, o.fill()); s.dump(j, pretty_print, false, static_cast(indentation)); return o; } diff --git a/tests/src/unit-convenience.cpp b/tests/src/unit-convenience.cpp index 7476c75cb..6776a14c1 100644 --- a/tests/src/unit-convenience.cpp +++ b/tests/src/unit-convenience.cpp @@ -99,7 +99,7 @@ void check_escaped(const char* original, const char* escaped, const bool ensure_ { std::stringstream ss; nlohmann::detail::output_stream_adapter adapter(ss); - json::serializer s(&adapter, ' '); + json::serializer s(adapter, ' '); s.dump_escaped(original, ensure_ascii); s.flush(); // dump_escaped writes into the serializer's internal buffer CHECK(ss.str() == escaped);