| 1 |
|
|
1 |
|
|
| 2 |
|
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
|
2 |
|
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
|
| 3 |
|
|
3 |
|
|
| 4 |
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
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)
|
5 |
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
| 6 |
|
|
6 |
|
|
| 7 |
|
// Official repository: https://github.com/boostorg/json
|
7 |
|
// Official repository: https://github.com/boostorg/json
|
| 8 |
|
|
8 |
|
|
| 9 |
|
|
9 |
|
|
| 10 |
|
#ifndef BOOST_JSON_SERIALIZER_HPP
|
10 |
|
#ifndef BOOST_JSON_SERIALIZER_HPP
|
| 11 |
|
#define BOOST_JSON_SERIALIZER_HPP
|
11 |
|
#define BOOST_JSON_SERIALIZER_HPP
|
| 12 |
|
|
12 |
|
|
| 13 |
|
#include <boost/json/detail/config.hpp>
|
13 |
|
#include <boost/json/detail/config.hpp>
|
| 14 |
|
#include <boost/json/detail/format.hpp>
|
14 |
|
#include <boost/json/detail/format.hpp>
|
| 15 |
|
#include <boost/json/detail/stream.hpp>
|
15 |
|
#include <boost/json/detail/stream.hpp>
|
| 16 |
|
#include <boost/json/detail/writer.hpp>
|
16 |
|
#include <boost/json/detail/writer.hpp>
|
| 17 |
|
#include <boost/json/serialize_options.hpp>
|
17 |
|
#include <boost/json/serialize_options.hpp>
|
| 18 |
|
#include <boost/json/value.hpp>
|
18 |
|
#include <boost/json/value.hpp>
|
| 19 |
|
|
19 |
|
|
| 20 |
|
|
20 |
|
|
| 21 |
|
|
21 |
|
|
| 22 |
|
|
22 |
|
|
| 23 |
|
/** A serializer for JSON.
|
23 |
|
/** A serializer for JSON.
|
| 24 |
|
|
24 |
|
|
| 25 |
|
This class traverses an instance of a library type and emits serialized
|
25 |
|
This class traverses an instance of a library type and emits serialized
|
| 26 |
|
JSON text by filling in one or more caller-provided buffers. To use,
|
26 |
|
JSON text by filling in one or more caller-provided buffers. To use,
|
| 27 |
|
declare a variable and call @ref reset with a pointer to the variable you
|
27 |
|
declare a variable and call @ref reset with a pointer to the variable you
|
| 28 |
|
want to serialize. Then call @ref read over and over until @ref done
|
28 |
|
want to serialize. Then call @ref read over and over until @ref done
|
| 29 |
|
|
29 |
|
|
| 30 |
|
|
30 |
|
|
| 31 |
|
|
31 |
|
|
| 32 |
|
This demonstrates how the serializer may be used to print a JSON value to
|
32 |
|
This demonstrates how the serializer may be used to print a JSON value to
|
| 33 |
|
|
33 |
|
|
| 34 |
|
|
34 |
|
|
| 35 |
|
|
35 |
|
|
| 36 |
|
void print( std::ostream& os, value const& jv)
|
36 |
|
void print( std::ostream& os, value const& jv)
|
| 37 |
|
|
37 |
|
|
| 38 |
|
|
38 |
|
|
| 39 |
|
|
39 |
|
|
| 40 |
|
|
40 |
|
|
| 41 |
|
|
41 |
|
|
| 42 |
|
|
42 |
|
|
| 43 |
|
|
43 |
|
|
| 44 |
|
|
44 |
|
|
| 45 |
|
|
45 |
|
|
| 46 |
|
|
46 |
|
|
| 47 |
|
|
47 |
|
|
| 48 |
|
|
48 |
|
|
| 49 |
|
The same instance may not be accessed concurrently.
|
49 |
|
The same instance may not be accessed concurrently.
|
| 50 |
|
|
50 |
|
|
| 51 |
|
|
51 |
|
|
| 52 |
|
The @ref serialize_options structure optionally provided upon construction
|
52 |
|
The @ref serialize_options structure optionally provided upon construction
|
| 53 |
|
is used to enable non-standard JSON extensions. A default-constructed
|
53 |
|
is used to enable non-standard JSON extensions. A default-constructed
|
| 54 |
|
`serialize_options` doesn't enable any extensions.
|
54 |
|
`serialize_options` doesn't enable any extensions.
|
| 55 |
|
|
55 |
|
|
| 56 |
|
|
56 |
|
|
| 57 |
|
|
57 |
|
|
| 58 |
|
|
58 |
|
|
| 59 |
|
|
59 |
|
|
| 60 |
|
|
60 |
|
|
| 61 |
|
using fn_t = bool (*)(writer&, detail::stream&);
|
61 |
|
using fn_t = bool (*)(writer&, detail::stream&);
|
| 62 |
|
|
62 |
|
|
| 63 |
|
|
63 |
|
|
| 64 |
|
|
64 |
|
|
| 65 |
|
|
65 |
|
|
| 66 |
|
|
66 |
|
|
| 67 |
|
|
67 |
|
|
| 68 |
|
|
68 |
|
|
| 69 |
|
|
69 |
|
|
| 70 |
|
All temporary storage is deallocated.
|
70 |
|
All temporary storage is deallocated.
|
| 71 |
|
|
71 |
|
|
| 72 |
|
|
72 |
|
|
| 73 |
|
|
73 |
|
|
| 74 |
|
|
74 |
|
|
| 75 |
|
|
75 |
|
|
| 76 |
|
|
76 |
|
|
| 77 |
|
|
77 |
|
|
| 78 |
|
|
78 |
|
|
| 79 |
|
|
79 |
|
|
| 80 |
|
|
80 |
|
|
| 81 |
|
#endif // BOOST_JSON_DOCS
|
81 |
|
#endif // BOOST_JSON_DOCS
|
| 82 |
|
|
82 |
|
|
| 83 |
|
|
83 |
|
|
| 84 |
|
|
84 |
|
|
| 85 |
|
The serializer is constructed with no value to serialize The value may
|
85 |
|
The serializer is constructed with no value to serialize The value may
|
| 86 |
|
be set later by calling @ref reset. If serialization is attempted with
|
86 |
|
be set later by calling @ref reset. If serialization is attempted with
|
| 87 |
|
no value, the output is as if a null value is serialized.
|
87 |
|
no value, the output is as if a null value is serialized.
|
| 88 |
|
|
88 |
|
|
| 89 |
|
Overload **(3)** is a move constructor. The type is neither copyable
|
89 |
|
Overload **(3)** is a move constructor. The type is neither copyable
|
| 90 |
|
nor movable, so this constructor is deleted.
|
90 |
|
nor movable, so this constructor is deleted.
|
| 91 |
|
|
91 |
|
|
| 92 |
|
|
92 |
|
|
| 93 |
|
|
93 |
|
|
| 94 |
|
|
94 |
|
|
| 95 |
|
|
95 |
|
|
| 96 |
|
|
96 |
|
|
| 97 |
|
|
97 |
|
|
| 98 |
|
@param opts The options for the serializer. If this parameter is
|
98 |
|
@param opts The options for the serializer. If this parameter is
|
| 99 |
|
omitted, the serializer will output only standard JSON.
|
99 |
|
omitted, the serializer will output only standard JSON.
|
| 100 |
|
|
100 |
|
|
| 101 |
|
|
101 |
|
|
| 102 |
|
|
102 |
|
|
| 103 |
|
|
103 |
|
|
| 104 |
|
serializer( serialize_options const& opts = {} ) noexcept;
|
104 |
|
serializer( serialize_options const& opts = {} ) noexcept;
|
| 105 |
|
|
105 |
|
|
| 106 |
|
|
106 |
|
|
| 107 |
|
|
107 |
|
|
| 108 |
|
@param sp A pointer to the @ref boost::container::pmr::memory_resource
|
108 |
|
@param sp A pointer to the @ref boost::container::pmr::memory_resource
|
| 109 |
|
to use when producing partial output. Shared ownership of the memory
|
109 |
|
to use when producing partial output. Shared ownership of the memory
|
| 110 |
|
resource is retained until the serializer is destroyed.
|
110 |
|
resource is retained until the serializer is destroyed.
|
| 111 |
|
|
111 |
|
|
| 112 |
|
@param buf An optional static buffer to use for temporary storage when
|
112 |
|
@param buf An optional static buffer to use for temporary storage when
|
| 113 |
|
producing partial output.
|
113 |
|
producing partial output.
|
| 114 |
|
|
114 |
|
|
| 115 |
|
@param size The number of bytes of valid memory pointed to by
|
115 |
|
@param size The number of bytes of valid memory pointed to by
|
| 116 |
|
|
116 |
|
|
| 117 |
|
|
117 |
|
|
| 118 |
|
|
118 |
|
|
| 119 |
|
|
119 |
|
|
| 120 |
|
|
120 |
|
|
| 121 |
|
|
121 |
|
|
| 122 |
|
|
122 |
|
|
| 123 |
|
unsigned char* buf = nullptr,
|
123 |
|
unsigned char* buf = nullptr,
|
| 124 |
|
|
124 |
|
|
| 125 |
|
serialize_options const& opts = {}) noexcept;
|
125 |
|
serialize_options const& opts = {}) noexcept;
|
| 126 |
|
|
126 |
|
|
| 127 |
|
|
127 |
|
|
| 128 |
|
serializer(serializer&&) = delete;
|
128 |
|
serializer(serializer&&) = delete;
|
| 129 |
|
|
129 |
|
|
| 130 |
|
|
130 |
|
|
| 131 |
|
/** Check if the serialization is complete.
|
131 |
|
/** Check if the serialization is complete.
|
| 132 |
|
|
132 |
|
|
| 133 |
|
This function returns `true` when all of the characters in the
|
133 |
|
This function returns `true` when all of the characters in the
|
| 134 |
|
serialized representation of the value have been read.
|
134 |
|
serialized representation of the value have been read.
|
| 135 |
|
|
135 |
|
|
| 136 |
|
|
136 |
|
|
| 137 |
|
|
137 |
|
|
| 138 |
|
|
138 |
|
|
| 139 |
|
|
139 |
|
|
| 140 |
|
|
140 |
|
|
| 141 |
|
|
141 |
|
|
| 142 |
|
|
142 |
|
|
| 143 |
|
|
143 |
|
|
| 144 |
|
|
144 |
|
|
| 145 |
|
|
145 |
|
|
| 146 |
|
|
146 |
|
|
| 147 |
|
|
147 |
|
|
| 148 |
|
/** Reset the serializer for a new element.
|
148 |
|
/** Reset the serializer for a new element.
|
| 149 |
|
|
149 |
|
|
| 150 |
|
This function prepares the serializer to emit a new serialized JSON
|
150 |
|
This function prepares the serializer to emit a new serialized JSON
|
| 151 |
|
representing its argument: `*p` **(1)**--**(5)**, `sv` **(6)**, or
|
151 |
|
representing its argument: `*p` **(1)**--**(5)**, `sv` **(6)**, or
|
| 152 |
|
`np` **(7)**. Ownership is not transferred. The caller is responsible
|
152 |
|
`np` **(7)**. Ownership is not transferred. The caller is responsible
|
| 153 |
|
for ensuring that the lifetime of the object pointed to by the argument
|
153 |
|
for ensuring that the lifetime of the object pointed to by the argument
|
| 154 |
|
extends until it is no longer needed.
|
154 |
|
extends until it is no longer needed.
|
| 155 |
|
|
155 |
|
|
| 156 |
|
Any memory internally allocated for previous uses of this `serializer`
|
156 |
|
Any memory internally allocated for previous uses of this `serializer`
|
| 157 |
|
object is preserved and re-used for the new output.
|
157 |
|
object is preserved and re-used for the new output.
|
| 158 |
|
|
158 |
|
|
| 159 |
|
Overload **(5)** uses \<\<direct_conversion,direct serialization\>\>.
|
159 |
|
Overload **(5)** uses \<\<direct_conversion,direct serialization\>\>.
|
| 160 |
|
|
160 |
|
|
| 161 |
|
@param p A pointer to the element to serialize.
|
161 |
|
@param p A pointer to the element to serialize.
|
| 162 |
|
|
162 |
|
|
| 163 |
|
|
163 |
|
|
| 164 |
|
|
164 |
|
|
| 165 |
|
|
165 |
|
|
| 166 |
|
|
166 |
|
|
| 167 |
|
reset(value const* p) noexcept;
|
167 |
|
reset(value const* p) noexcept;
|
| 168 |
|
|
168 |
|
|
| 169 |
|
|
169 |
|
|
| 170 |
|
|
170 |
|
|
| 171 |
|
reset(array const* p) noexcept;
|
171 |
|
reset(array const* p) noexcept;
|
| 172 |
|
|
172 |
|
|
| 173 |
|
|
173 |
|
|
| 174 |
|
|
174 |
|
|
| 175 |
|
reset(object const* p) noexcept;
|
175 |
|
reset(object const* p) noexcept;
|
| 176 |
|
|
176 |
|
|
| 177 |
|
|
177 |
|
|
| 178 |
|
|
178 |
|
|
| 179 |
|
reset(string const* p) noexcept;
|
179 |
|
reset(string const* p) noexcept;
|
| 180 |
|
|
180 |
|
|
| 181 |
|
|
181 |
|
|
| 182 |
|
|
182 |
|
|
| 183 |
|
reset(T const* p) noexcept;
|
183 |
|
reset(T const* p) noexcept;
|
| 184 |
|
|
184 |
|
|
| 185 |
|
|
185 |
|
|
| 186 |
|
|
186 |
|
|
| 187 |
|
@param sv The characters representing a string.
|
187 |
|
@param sv The characters representing a string.
|
| 188 |
|
|
188 |
|
|
| 189 |
|
|
189 |
|
|
| 190 |
|
|
190 |
|
|
| 191 |
|
reset(string_view sv) noexcept;
|
191 |
|
reset(string_view sv) noexcept;
|
| 192 |
|
|
192 |
|
|
| 193 |
|
|
193 |
|
|
| 194 |
|
|
194 |
|
|
| 195 |
|
@param np Represents a null value.
|
195 |
|
@param np Represents a null value.
|
| 196 |
|
|
196 |
|
|
| 197 |
|
|
197 |
|
|
| 198 |
|
|
198 |
|
|
| 199 |
|
reset(std::nullptr_t np) noexcept;
|
199 |
|
reset(std::nullptr_t np) noexcept;
|
| 200 |
|
|
200 |
|
|
| 201 |
|
|
201 |
|
|
| 202 |
|
/** Read the next buffer of serialized JSON.
|
202 |
|
/** Read the next buffer of serialized JSON.
|
| 203 |
|
|
203 |
|
|
| 204 |
|
This function attempts to fill the caller provided buffer starting at
|
204 |
|
This function attempts to fill the caller provided buffer starting at
|
| 205 |
|
`dest` with up to `size` characters of the serialized JSON that
|
205 |
|
`dest` with up to `size` characters of the serialized JSON that
|
| 206 |
|
represents the value. If the buffer is not large enough, multiple calls
|
206 |
|
represents the value. If the buffer is not large enough, multiple calls
|
| 207 |
|
|
207 |
|
|
| 208 |
|
|
208 |
|
|
| 209 |
|
If serialization completes during this call; that is, that all of the
|
209 |
|
If serialization completes during this call; that is, that all of the
|
| 210 |
|
characters belonging to the serialized value have been written to
|
210 |
|
characters belonging to the serialized value have been written to
|
| 211 |
|
caller-provided buffers, the function @ref done will return `true`.
|
211 |
|
caller-provided buffers, the function @ref done will return `true`.
|
| 212 |
|
|
212 |
|
|
| 213 |
|
|
213 |
|
|
| 214 |
|
|
214 |
|
|
| 215 |
|
|
215 |
|
|
| 216 |
|
|
216 |
|
|
| 217 |
|
|
217 |
|
|
| 218 |
|
|
218 |
|
|
| 219 |
|
@li **(1)** linear in `size`.
|
219 |
|
@li **(1)** linear in `size`.
|
| 220 |
|
@li **(2)** linear in `N`.
|
220 |
|
@li **(2)** linear in `N`.
|
| 221 |
|
|
221 |
|
|
| 222 |
|
|
222 |
|
|
| 223 |
|
Basic guarantee. Calls to `memory_resource::allocate` may throw.
|
223 |
|
Basic guarantee. Calls to `memory_resource::allocate` may throw.
|
| 224 |
|
|
224 |
|
|
| 225 |
|
@return A @ref string_view containing the characters written, which may
|
225 |
|
@return A @ref string_view containing the characters written, which may
|
| 226 |
|
be less than `size` or `N`.
|
226 |
|
be less than `size` or `N`.
|
| 227 |
|
|
227 |
|
|
| 228 |
|
@param dest A pointer to storage to write into.
|
228 |
|
@param dest A pointer to storage to write into.
|
| 229 |
|
|
229 |
|
|
| 230 |
|
@param size The maximum number of characters to write to the memory
|
230 |
|
@param size The maximum number of characters to write to the memory
|
| 231 |
|
|
231 |
|
|
| 232 |
|
|
232 |
|
|
| 233 |
|
|
233 |
|
|
| 234 |
|
|
234 |
|
|
| 235 |
|
|
235 |
|
|
| 236 |
|
|
236 |
|
|
| 237 |
|
read(char* dest, std::size_t size);
|
237 |
|
read(char* dest, std::size_t size);
|
| 238 |
|
|
238 |
|
|
| 239 |
|
|
239 |
|
|
| 240 |
|
|
240 |
|
|
| 241 |
|
@tparam N The size of the array `dest`.
|
241 |
|
@tparam N The size of the array `dest`.
|
| 242 |
|
|
242 |
|
|
| 243 |
|
|
243 |
|
|
| 244 |
|
|
244 |
|
|
| 245 |
|
|
245 |
|
|
| 246 |
|
|
246 |
|
|
| 247 |
|
|
247 |
|
|
| 248 |
|
|
248 |
|
|
| 249 |
|
|
249 |
|
|
| 250 |
|
|
250 |
|
|
| 251 |
|
|
251 |
|
|
| 252 |
|
|
252 |
|
|
| 253 |
|
// Safety net for accidental buffer overflows
|
253 |
|
// Safety net for accidental buffer overflows
|
| 254 |
|
|
254 |
|
|
| 255 |
|
|
255 |
|
|
| 256 |
|
read(char(&dest)[N], std::size_t n)
|
256 |
|
read(char(&dest)[N], std::size_t n)
|
| 257 |
|
|
257 |
|
|
| 258 |
|
// If this goes off, check your parameters
|
258 |
|
// If this goes off, check your parameters
|
| 259 |
|
// closely, chances are you passed an array
|
259 |
|
// closely, chances are you passed an array
|
| 260 |
|
// thinking it was a pointer.
|
260 |
|
// thinking it was a pointer.
|
| 261 |
|
|
261 |
|
|
| 262 |
|
|
262 |
|
|
| 263 |
|
|
263 |
|
|
| 264 |
|
|
264 |
|
|
| 265 |
|
|
265 |
|
|
| 266 |
|
|
266 |
|
|
| 267 |
|
|
267 |
|
|
| 268 |
|
|
268 |
|
|
| 269 |
|
|
269 |
|
|
| 270 |
|
#include <boost/json/impl/serializer.hpp>
|
270 |
|
#include <boost/json/impl/serializer.hpp>
|
| 271 |
|
|
271 |
|
|
| 272 |
|
|
272 |
|
|