From 07f1b20e26b87b041123f6dec07675cf57422f91 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 | 6 +++--- single_include/nlohmann/json.hpp | 10 +++++----- tests/src/unit-convenience.cpp | 2 +- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/include/nlohmann/detail/output/serializer.hpp b/include/nlohmann/detail/output/serializer.hpp index 3ea359109..9560729ad 100644 --- a/include/nlohmann/detail/output/serializer.hpp +++ b/include/nlohmann/detail/output/serializer.hpp @@ -77,12 +77,12 @@ class serializer being threaded through every call to @ref dump, @ref dump_internal and @ref dump_iteratively. */ - serializer(output_adapter_protocol* s, const char ichar, + serializer(output_adapter_protocol& s, const char ichar, const bool pretty_print_ = false, const bool ensure_ascii_ = false, const std::size_t indent_step_ = 0, error_handler_t error_handler_ = error_handler_t::strict) - : o(s) + : o(&s) , locale(std::localeconv()) , indent_char(ichar) , pretty_print(pretty_print_) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 48cf3c4b0..016a4dd27 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -1347,13 +1347,13 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec if (indent >= 0) { - serializer s(&string_adapter, indent_char, + serializer s(string_adapter, indent_char, true, ensure_ascii, static_cast(indent), error_handler); s.dump(*this); } else { - serializer s(&string_adapter, indent_char, + serializer s(string_adapter, indent_char, false, ensure_ascii, 0, error_handler); s.dump(*this); } @@ -4085,7 +4085,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(), pretty_print, false, static_cast(indentation)); s.dump(j); return o; diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index b3fcd6cdd..c94c477ed 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -21425,12 +21425,12 @@ class serializer being threaded through every call to @ref dump, @ref dump_internal and @ref dump_iteratively. */ - serializer(output_adapter_protocol* s, const char ichar, + serializer(output_adapter_protocol& s, const char ichar, const bool pretty_print_ = false, const bool ensure_ascii_ = false, const std::size_t indent_step_ = 0, error_handler_t error_handler_ = error_handler_t::strict) - : o(s) + : o(&s) , locale(std::localeconv()) , indent_char(ichar) , pretty_print(pretty_print_) @@ -24743,13 +24743,13 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec if (indent >= 0) { - serializer s(&string_adapter, indent_char, + serializer s(string_adapter, indent_char, true, ensure_ascii, static_cast(indent), error_handler); s.dump(*this); } else { - serializer s(&string_adapter, indent_char, + serializer s(string_adapter, indent_char, false, ensure_ascii, 0, error_handler); s.dump(*this); } @@ -27481,7 +27481,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(), pretty_print, false, static_cast(indentation)); s.dump(j); return o; diff --git a/tests/src/unit-convenience.cpp b/tests/src/unit-convenience.cpp index fcf6ba73c..0ba57d846 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, ' ', false, ensure_ascii); + json::serializer s(adapter, ' ', false, ensure_ascii); s.dump_escaped(original); s.flush(); // dump_escaped writes into the serializer's internal buffer CHECK(ss.str() == escaped);