15#include <nlohmann/detail/macro_scope.hpp>
22template<
typename CharType>
struct output_adapter_protocol
24 virtual void write_character(CharType c) = 0;
25 virtual void write_characters(
const CharType* s, std::size_t length) = 0;
26 virtual ~output_adapter_protocol() =
default;
28 output_adapter_protocol() =
default;
29 output_adapter_protocol(
const output_adapter_protocol&) =
default;
30 output_adapter_protocol(output_adapter_protocol&&)
noexcept =
default;
31 output_adapter_protocol& operator=(
const output_adapter_protocol&) =
default;
32 output_adapter_protocol& operator=(output_adapter_protocol&&)
noexcept =
default;
36template<
typename CharType>
40template<
typename CharType,
typename AllocatorType = std::allocator<CharType>>
41class output_vector_adapter :
public output_adapter_protocol<CharType>
44 explicit output_vector_adapter(std::vector<CharType, AllocatorType>& vec) noexcept
48 void write_character(CharType c)
override
53 JSON_HEDLEY_NON_NULL(2)
54 void write_characters(
const CharType* s, std::size_t length)
override
56 std::copy(s, s + length, std::back_inserter(v));
60 std::vector<CharType, AllocatorType>& v;
65template<
typename CharType>
66class output_stream_adapter :
public output_adapter_protocol<CharType>
69 explicit output_stream_adapter(std::basic_ostream<CharType>& s) noexcept
73 void write_character(CharType c)
override
78 JSON_HEDLEY_NON_NULL(2)
79 void write_characters(
const CharType* s, std::size_t length)
override
81 stream.write(s,
static_cast<std::streamsize
>(length));
85 std::basic_ostream<CharType>& stream;
90template<
typename CharType,
typename StringType = std::basic_
string<CharType>>
91class output_string_adapter :
public output_adapter_protocol<CharType>
94 explicit output_string_adapter(StringType& s) noexcept
98 void write_character(CharType c)
override
103 JSON_HEDLEY_NON_NULL(2)
104 void write_characters(
const CharType* s, std::size_t length)
override
106 str.append(s, length);
113template<
typename CharType,
typename StringType = std::basic_
string<CharType>>
117 template<
typename AllocatorType = std::allocator<CharType>>
118 output_adapter(std::vector<CharType, AllocatorType>& vec)
122 output_adapter(std::basic_ostream<CharType>& s)
126 output_adapter(StringType& s)
output adapter for output streams
Definition output_adapters.hpp:67
output adapter for basic_string
Definition output_adapters.hpp:92
output adapter for byte vectors
Definition output_adapters.hpp:42
detail namespace with internal helper functions
Definition from_json.hpp:25
std::shared_ptr< output_adapter_protocol< CharType > > output_adapter_t
a type to simplify interfaces
Definition output_adapters.hpp:37
namespace for Niels Lohmann
Definition adl_serializer.hpp:12