23#include <nlohmann/detail/macro_scope.hpp>
25NLOHMANN_JSON_NAMESPACE_BEGIN
30template<
typename CharType>
struct output_adapter_protocol
32 virtual void write_character(CharType c) = 0;
33 virtual void write_characters(
const CharType* s, std::size_t length) = 0;
34 virtual ~output_adapter_protocol() =
default;
36 output_adapter_protocol() =
default;
37 output_adapter_protocol(
const output_adapter_protocol&) =
default;
38 output_adapter_protocol(output_adapter_protocol&&)
noexcept =
default;
39 output_adapter_protocol& operator=(
const output_adapter_protocol&) =
default;
40 output_adapter_protocol& operator=(output_adapter_protocol&&)
noexcept =
default;
44template<
typename CharType>
48template<
typename CharType,
typename AllocatorType = std::allocator<CharType>>
49class output_vector_adapter :
public output_adapter_protocol<CharType>
52 explicit output_vector_adapter(std::vector<CharType, AllocatorType>& vec) noexcept
56 void write_character(CharType c)
override
61 JSON_HEDLEY_NON_NULL(2)
62 void write_characters(
const CharType* s, std::size_t length)
override
64 v.insert(v.end(), s, s + length);
68 std::vector<CharType, AllocatorType>& v;
73template<
typename CharType>
74class output_stream_adapter :
public output_adapter_protocol<CharType>
77 explicit output_stream_adapter(std::basic_ostream<CharType>& s) noexcept
81 void write_character(CharType c)
override
86 JSON_HEDLEY_NON_NULL(2)
87 void write_characters(
const CharType* s, std::size_t length)
override
89 stream.write(s,
static_cast<std::streamsize
>(length));
93 std::basic_ostream<CharType>& stream;
98template<
typename CharType,
typename StringType = std::basic_
string<CharType>>
99class output_string_adapter :
public output_adapter_protocol<CharType>
102 explicit output_string_adapter(StringType& s) noexcept
106 void write_character(CharType c)
override
111 JSON_HEDLEY_NON_NULL(2)
112 void write_characters(
const CharType* s, std::size_t length)
override
114 str.append(s, length);
121template<
typename CharType,
typename StringType = std::basic_
string<CharType>>
125 template<
typename AllocatorType = std::allocator<CharType>>
126 output_adapter(std::vector<CharType, AllocatorType>& vec)
130 output_adapter(std::basic_ostream<CharType>& s)
134 output_adapter(StringType& s)
147NLOHMANN_JSON_NAMESPACE_END
output adapter for output streams
定义 output_adapters.hpp:75
output adapter for basic_string
定义 output_adapters.hpp:100
output adapter for byte vectors
定义 output_adapters.hpp:50
detail namespace with internal helper functions
定义 from_json.hpp:43
std::shared_ptr< output_adapter_protocol< CharType > > output_adapter_t
a type to simplify interfaces
定义 output_adapters.hpp:45