// __ _____ _____ _____ // __| | __| | | | JSON for Modern C++ // | | |__ | | | | | | version 3.12.0 // |_____|_____|_____|_|___| https://github.com/nlohmann/json // // SPDX-FileCopyrightText: 2013-2026 Niels Lohmann // SPDX-License-Identifier: MIT #pragma once #include // copy #include // size_t #include // back_inserter #include // shared_ptr, make_shared #include // basic_string #include // move #include // vector #ifndef JSON_NO_IO #include // streamsize #include // basic_ostream #endif // JSON_NO_IO #include NLOHMANN_JSON_NAMESPACE_BEGIN namespace detail { /// abstract output adapter interface template struct output_adapter_protocol { virtual void write_character(CharType c) = 0; virtual void write_characters(const CharType* s, std::size_t length) = 0; virtual ~output_adapter_protocol() = default; output_adapter_protocol() = default; output_adapter_protocol(const output_adapter_protocol&) = default; output_adapter_protocol(output_adapter_protocol&&) noexcept = default; output_adapter_protocol& operator=(const output_adapter_protocol&) = default; output_adapter_protocol& operator=(output_adapter_protocol&&) noexcept = default; }; /// a type to simplify interfaces template using output_adapter_t = std::shared_ptr>; /// output adapter for byte vectors template> class output_vector_adapter : public output_adapter_protocol { public: explicit output_vector_adapter(std::vector& vec) noexcept : v(vec) {} void write_character(CharType c) override { v.push_back(c); } JSON_HEDLEY_NON_NULL(2) void write_characters(const CharType* s, std::size_t length) override { v.insert(v.end(), s, s + length); } private: std::vector& v; }; #ifndef JSON_NO_IO /// output adapter for output streams template class output_stream_adapter : public output_adapter_protocol { public: explicit output_stream_adapter(std::basic_ostream& s) noexcept : stream(s) {} void write_character(CharType c) override { stream.put(c); } JSON_HEDLEY_NON_NULL(2) void write_characters(const CharType* s, std::size_t length) override { stream.write(s, static_cast(length)); } private: std::basic_ostream& stream; }; #endif // JSON_NO_IO /// output adapter for basic_string template> class output_string_adapter : public output_adapter_protocol { public: explicit output_string_adapter(StringType& s) noexcept : str(s) {} void write_character(CharType c) override { str.push_back(c); } JSON_HEDLEY_NON_NULL(2) void write_characters(const CharType* s, std::size_t length) override { str.append(s, length); } private: StringType& str; }; /// @brief non-virtual output sink writing into a std::vector /// /// Unlike output_vector_adapter, this sink is not part of the virtual /// output_adapter_protocol hierarchy: it is passed to binary_writer by value as /// a template parameter, so write_character()/write_characters() are ordinary /// (inlinable) calls with no vtable lookup and no shared_ptr. It is used for the /// common `to_cbor`/`to_msgpack`/... into a std::vector. template> class output_vector_sink { public: explicit output_vector_sink(std::vector& vec) noexcept : v(vec) {} void write_character(CharType c) { v.push_back(c); } // no JSON_HEDLEY_NON_NULL here: binary_writer legitimately passes a null // pointer with length 0 for empty strings/binary values. Appending an empty // range is a no-op; the type-erased path tolerates this via the (unattributed) // virtual base, and the concrete sink must do the same. void write_characters(const CharType* s, std::size_t length) { v.insert(v.end(), s, s + length); } private: std::vector& v; }; /// @brief output sink forwarding to a type-erased output adapter /// /// Wraps the polymorphic output_adapter_t so the same binary_writer template can /// also target arbitrary adapters (output streams, strings, user-provided /// adapters) via the `output_adapter`-based overloads. Each write still goes /// through one virtual call, exactly as before; only the concrete sinks above /// avoid it. template class output_adapter_sink { public: explicit output_adapter_sink(output_adapter_t adapter) : oa(std::move(adapter)) { JSON_ASSERT(oa); } void write_character(CharType c) { oa->write_character(c); } // no JSON_HEDLEY_NON_NULL: forwards (null, 0) for empty payloads, exactly as // the type-erased path already did before this sink existed void write_characters(const CharType* s, std::size_t length) { oa->write_characters(s, length); } private: output_adapter_t oa = nullptr; }; template> class output_adapter { public: template> output_adapter(std::vector& vec) : oa(std::make_shared>(vec)) {} #ifndef JSON_NO_IO output_adapter(std::basic_ostream& s) : oa(std::make_shared>(s)) {} #endif // JSON_NO_IO output_adapter(StringType& s) : oa(std::make_shared>(s)) {} operator output_adapter_t() { return oa; } private: output_adapter_t oa = nullptr; }; } // namespace detail NLOHMANN_JSON_NAMESPACE_END