]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/json/impl/serialize.ipp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / json / impl / serialize.ipp
1 //
2 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/boostorg/json
8 //
9
10 #ifndef BOOST_JSON_IMPL_SERIALIZE_IPP
11 #define BOOST_JSON_IMPL_SERIALIZE_IPP
12
13 #include <boost/json/serialize.hpp>
14 #include <boost/json/serializer.hpp>
15 #include <ostream>
16
17 BOOST_JSON_NS_BEGIN
18
19 static
20 void
21 serialize_impl(
22 std::string& s,
23 serializer& sr)
24 {
25 // serialize to a small buffer to avoid
26 // the first few allocations in std::string
27 char buf[BOOST_JSON_STACK_BUFFER_SIZE];
28 string_view sv;
29 sv = sr.read(buf);
30 if(sr.done())
31 {
32 // fast path
33 s.append(
34 sv.data(), sv.size());
35 return;
36 }
37 std::size_t len = sv.size();
38 s.reserve(len * 2);
39 s.resize(s.capacity());
40 BOOST_ASSERT(
41 s.size() >= len * 2);
42 std::memcpy(&s[0],
43 sv.data(), sv.size());
44 for(;;)
45 {
46 sv = sr.read(
47 &s[0] + len,
48 s.size() - len);
49 len += sv.size();
50 if(sr.done())
51 break;
52 s.reserve(
53 s.capacity() + 1);
54 s.resize(
55 s.capacity());
56 }
57 s.resize(len);
58 }
59
60 std::string
61 serialize(
62 value const& jv)
63 {
64 std::string s;
65 serializer sr;
66 sr.reset(&jv);
67 serialize_impl(s, sr);
68 return s;
69 }
70
71 std::string
72 serialize(
73 array const& arr)
74 {
75 std::string s;
76 serializer sr;
77 sr.reset(&arr);
78 serialize_impl(s, sr);
79 return s;
80 }
81
82 std::string
83 serialize(
84 object const& obj)
85 {
86 std::string s;
87 serializer sr;
88 sr.reset(&obj);
89 serialize_impl(s, sr);
90 return s;
91 }
92
93 std::string
94 serialize(
95 string const& str)
96 {
97 std::string s;
98 serializer sr;
99 sr.reset(&str);
100 serialize_impl(s, sr);
101 return s;
102 }
103
104 // this is here for key_value_pair::key()
105 std::string
106 serialize(
107 string_view sv)
108 {
109 std::string s;
110 serializer sr;
111 sr.reset(sv);
112 serialize_impl(s, sr);
113 return s;
114 }
115
116 //----------------------------------------------------------
117
118 //[example_operator_lt__lt_
119 // Serialize a value into an output stream
120
121 std::ostream&
122 operator<<( std::ostream& os, value const& jv )
123 {
124 // Create a serializer
125 serializer sr;
126
127 // Set the serializer up for our value
128 sr.reset( &jv );
129
130 // Loop until all output is produced.
131 while( ! sr.done() )
132 {
133 // Use a local buffer to avoid allocation.
134 char buf[ BOOST_JSON_STACK_BUFFER_SIZE ];
135
136 // Fill our buffer with serialized characters and write it to the output stream.
137 os << sr.read( buf );
138 }
139
140 return os;
141 }
142 //]
143
144 static
145 void
146 to_ostream(
147 std::ostream& os,
148 serializer& sr)
149 {
150 while(! sr.done())
151 {
152 char buf[BOOST_JSON_STACK_BUFFER_SIZE];
153 auto s = sr.read(buf);
154 os.write(s.data(), s.size());
155 }
156 }
157
158 std::ostream&
159 operator<<(
160 std::ostream& os,
161 array const& arr)
162 {
163 serializer sr;
164 sr.reset(&arr);
165 to_ostream(os, sr);
166 return os;
167 }
168
169 std::ostream&
170 operator<<(
171 std::ostream& os,
172 object const& obj)
173 {
174 serializer sr;
175 sr.reset(&obj);
176 to_ostream(os, sr);
177 return os;
178 }
179
180 std::ostream&
181 operator<<(
182 std::ostream& os,
183 string const& str)
184 {
185 serializer sr;
186 sr.reset(&str);
187 to_ostream(os, sr);
188 return os;
189 }
190
191 BOOST_JSON_NS_END
192
193 #endif