EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
json.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file json.hpp
1 /*
2  __ _____ _____ _____
3  __| | __| | | | JSON for Modern C++
4 | | |__ | | | | | | version 3.7.3
5 |_____|_____|_____|_|___| https://github.com/nlohmann/json
6 
7 Licensed under the MIT License <http://opensource.org/licenses/MIT>.
8 SPDX-License-Identifier: MIT
9 Copyright (c) 2013-2019 Niels Lohmann <http://nlohmann.me>.
10 
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is
16 furnished to do so, subject to the following conditions:
17 
18 The above copyright notice and this permission notice shall be included in all
19 copies or substantial portions of the Software.
20 
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 SOFTWARE.
28 */
29 
30 #ifndef INCLUDE_NLOHMANN_JSON_HPP_
31 #define INCLUDE_NLOHMANN_JSON_HPP_
32 
33 #define NLOHMANN_JSON_VERSION_MAJOR 3
34 #define NLOHMANN_JSON_VERSION_MINOR 7
35 #define NLOHMANN_JSON_VERSION_PATCH 3
36 
37 #include <algorithm> // all_of, find, for_each
38 #include <cassert> // assert
39 #include <ciso646> // and, not, or
40 #include <cstddef> // nullptr_t, ptrdiff_t, size_t
41 #include <functional> // hash, less
42 #include <initializer_list> // initializer_list
43 #include <iosfwd> // istream, ostream
44 #include <iterator> // random_access_iterator_tag
45 #include <memory> // unique_ptr
46 #include <numeric> // accumulate
47 #include <string> // string, stoi, to_string
48 #include <utility> // declval, forward, move, pair, swap
49 #include <vector> // vector
50 
51 // #include <nlohmann/adl_serializer.hpp>
52 
53 
54 #include <utility>
55 
56 // #include <nlohmann/detail/conversions/from_json.hpp>
57 
58 
59 #include <algorithm> // transform
60 #include <array> // array
61 #include <ciso646> // and, not
62 #include <forward_list> // forward_list
63 #include <iterator> // inserter, front_inserter, end
64 #include <map> // map
65 #include <string> // string
66 #include <tuple> // tuple, make_tuple
67 #include <type_traits> // is_arithmetic, is_same, is_enum, underlying_type, is_convertible
68 #include <unordered_map> // unordered_map
69 #include <utility> // pair, declval
70 #include <valarray> // valarray
71 
72 // #include <nlohmann/detail/exceptions.hpp>
73 
74 
75 #include <exception> // exception
76 #include <stdexcept> // runtime_error
77 #include <string> // to_string
78 
79 // #include <nlohmann/detail/input/position_t.hpp>
80 
81 
82 #include <cstddef> // size_t
83 
84 namespace nlohmann
85 {
86 namespace detail
87 {
89 struct position_t
90 {
92  std::size_t chars_read_total = 0;
94  std::size_t chars_read_current_line = 0;
96  std::size_t lines_read = 0;
97 
99  constexpr operator size_t() const
100  {
101  return chars_read_total;
102  }
103 };
104 
105 } // namespace detail
106 } // namespace nlohmann
107 
108 // #include <nlohmann/detail/macro_scope.hpp>
109 
110 
111 #include <utility> // pair
112 // #include <nlohmann/thirdparty/hedley/hedley.hpp>
113 /* Hedley - https://nemequ.github.io/hedley
114  * Created by Evan Nemerson <evan@nemerson.com>
115  *
116  * To the extent possible under law, the author(s) have dedicated all
117  * copyright and related and neighboring rights to this software to
118  * the public domain worldwide. This software is distributed without
119  * any warranty.
120  *
121  * For details, see <http://creativecommons.org/publicdomain/zero/1.0/>.
122  * SPDX-License-Identifier: CC0-1.0
123  */
124 
125 #if !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < 13)
126 #if defined(JSON_HEDLEY_VERSION)
127  #undef JSON_HEDLEY_VERSION
128 #endif
129 #define JSON_HEDLEY_VERSION 13
130 
131 #if defined(JSON_HEDLEY_STRINGIFY_EX)
132  #undef JSON_HEDLEY_STRINGIFY_EX
133 #endif
134 #define JSON_HEDLEY_STRINGIFY_EX(x) #x
135 
136 #if defined(JSON_HEDLEY_STRINGIFY)
137  #undef JSON_HEDLEY_STRINGIFY
138 #endif
139 #define JSON_HEDLEY_STRINGIFY(x) JSON_HEDLEY_STRINGIFY_EX(x)
140 
141 #if defined(JSON_HEDLEY_CONCAT_EX)
142  #undef JSON_HEDLEY_CONCAT_EX
143 #endif
144 #define JSON_HEDLEY_CONCAT_EX(a,b) a##b
145 
146 #if defined(JSON_HEDLEY_CONCAT)
147  #undef JSON_HEDLEY_CONCAT
148 #endif
149 #define JSON_HEDLEY_CONCAT(a,b) JSON_HEDLEY_CONCAT_EX(a,b)
150 
151 #if defined(JSON_HEDLEY_CONCAT3_EX)
152  #undef JSON_HEDLEY_CONCAT3_EX
153 #endif
154 #define JSON_HEDLEY_CONCAT3_EX(a,b,c) a##b##c
155 
156 #if defined(JSON_HEDLEY_CONCAT3)
157  #undef JSON_HEDLEY_CONCAT3
158 #endif
159 #define JSON_HEDLEY_CONCAT3(a,b,c) JSON_HEDLEY_CONCAT3_EX(a,b,c)
160 
161 #if defined(JSON_HEDLEY_VERSION_ENCODE)
162  #undef JSON_HEDLEY_VERSION_ENCODE
163 #endif
164 #define JSON_HEDLEY_VERSION_ENCODE(major,minor,revision) (((major) * 1000000) + ((minor) * 1000) + (revision))
165 
166 #if defined(JSON_HEDLEY_VERSION_DECODE_MAJOR)
167  #undef JSON_HEDLEY_VERSION_DECODE_MAJOR
168 #endif
169 #define JSON_HEDLEY_VERSION_DECODE_MAJOR(version) ((version) / 1000000)
170 
171 #if defined(JSON_HEDLEY_VERSION_DECODE_MINOR)
172  #undef JSON_HEDLEY_VERSION_DECODE_MINOR
173 #endif
174 #define JSON_HEDLEY_VERSION_DECODE_MINOR(version) (((version) % 1000000) / 1000)
175 
176 #if defined(JSON_HEDLEY_VERSION_DECODE_REVISION)
177  #undef JSON_HEDLEY_VERSION_DECODE_REVISION
178 #endif
179 #define JSON_HEDLEY_VERSION_DECODE_REVISION(version) ((version) % 1000)
180 
181 #if defined(JSON_HEDLEY_GNUC_VERSION)
182  #undef JSON_HEDLEY_GNUC_VERSION
183 #endif
184 #if defined(__GNUC__) && defined(__GNUC_PATCHLEVEL__)
185  #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
186 #elif defined(__GNUC__)
187  #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, 0)
188 #endif
189 
190 #if defined(JSON_HEDLEY_GNUC_VERSION_CHECK)
191  #undef JSON_HEDLEY_GNUC_VERSION_CHECK
192 #endif
193 #if defined(JSON_HEDLEY_GNUC_VERSION)
194  #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GNUC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
195 #else
196  #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (0)
197 #endif
198 
199 #if defined(JSON_HEDLEY_MSVC_VERSION)
200  #undef JSON_HEDLEY_MSVC_VERSION
201 #endif
202 #if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 140000000)
203  #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 10000000, (_MSC_FULL_VER % 10000000) / 100000, (_MSC_FULL_VER % 100000) / 100)
204 #elif defined(_MSC_FULL_VER)
205  #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 1000000, (_MSC_FULL_VER % 1000000) / 10000, (_MSC_FULL_VER % 10000) / 10)
206 #elif defined(_MSC_VER)
207  #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_VER / 100, _MSC_VER % 100, 0)
208 #endif
209 
210 #if defined(JSON_HEDLEY_MSVC_VERSION_CHECK)
211  #undef JSON_HEDLEY_MSVC_VERSION_CHECK
212 #endif
213 #if !defined(_MSC_VER)
214  #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (0)
215 #elif defined(_MSC_VER) && (_MSC_VER >= 1400)
216  #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 10000000) + (minor * 100000) + (patch)))
217 #elif defined(_MSC_VER) && (_MSC_VER >= 1200)
218  #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 1000000) + (minor * 10000) + (patch)))
219 #else
220  #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_VER >= ((major * 100) + (minor)))
221 #endif
222 
223 #if defined(JSON_HEDLEY_INTEL_VERSION)
224  #undef JSON_HEDLEY_INTEL_VERSION
225 #endif
226 #if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE)
227  #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, __INTEL_COMPILER_UPDATE)
228 #elif defined(__INTEL_COMPILER)
229  #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, 0)
230 #endif
231 
232 #if defined(JSON_HEDLEY_INTEL_VERSION_CHECK)
233  #undef JSON_HEDLEY_INTEL_VERSION_CHECK
234 #endif
235 #if defined(JSON_HEDLEY_INTEL_VERSION)
236  #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
237 #else
238  #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (0)
239 #endif
240 
241 #if defined(JSON_HEDLEY_PGI_VERSION)
242  #undef JSON_HEDLEY_PGI_VERSION
243 #endif
244 #if defined(__PGI) && defined(__PGIC__) && defined(__PGIC_MINOR__) && defined(__PGIC_PATCHLEVEL__)
245  #define JSON_HEDLEY_PGI_VERSION JSON_HEDLEY_VERSION_ENCODE(__PGIC__, __PGIC_MINOR__, __PGIC_PATCHLEVEL__)
246 #endif
247 
248 #if defined(JSON_HEDLEY_PGI_VERSION_CHECK)
249  #undef JSON_HEDLEY_PGI_VERSION_CHECK
250 #endif
251 #if defined(JSON_HEDLEY_PGI_VERSION)
252  #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PGI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
253 #else
254  #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (0)
255 #endif
256 
257 #if defined(JSON_HEDLEY_SUNPRO_VERSION)
258  #undef JSON_HEDLEY_SUNPRO_VERSION
259 #endif
260 #if defined(__SUNPRO_C) && (__SUNPRO_C > 0x1000)
261  #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((((__SUNPRO_C >> 16) & 0xf) * 10) + ((__SUNPRO_C >> 12) & 0xf), (((__SUNPRO_C >> 8) & 0xf) * 10) + ((__SUNPRO_C >> 4) & 0xf), (__SUNPRO_C & 0xf) * 10)
262 #elif defined(__SUNPRO_C)
263  #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_C >> 8) & 0xf, (__SUNPRO_C >> 4) & 0xf, (__SUNPRO_C) & 0xf)
264 #elif defined(__SUNPRO_CC) && (__SUNPRO_CC > 0x1000)
265  #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((((__SUNPRO_CC >> 16) & 0xf) * 10) + ((__SUNPRO_CC >> 12) & 0xf), (((__SUNPRO_CC >> 8) & 0xf) * 10) + ((__SUNPRO_CC >> 4) & 0xf), (__SUNPRO_CC & 0xf) * 10)
266 #elif defined(__SUNPRO_CC)
267  #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_CC >> 8) & 0xf, (__SUNPRO_CC >> 4) & 0xf, (__SUNPRO_CC) & 0xf)
268 #endif
269 
270 #if defined(JSON_HEDLEY_SUNPRO_VERSION_CHECK)
271  #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK
272 #endif
273 #if defined(JSON_HEDLEY_SUNPRO_VERSION)
274  #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_SUNPRO_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
275 #else
276  #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (0)
277 #endif
278 
279 #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION)
280  #undef JSON_HEDLEY_EMSCRIPTEN_VERSION
281 #endif
282 #if defined(__EMSCRIPTEN__)
283  #define JSON_HEDLEY_EMSCRIPTEN_VERSION JSON_HEDLEY_VERSION_ENCODE(__EMSCRIPTEN_major__, __EMSCRIPTEN_minor__, __EMSCRIPTEN_tiny__)
284 #endif
285 
286 #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK)
287  #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK
288 #endif
289 #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION)
290  #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_EMSCRIPTEN_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
291 #else
292  #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (0)
293 #endif
294 
295 #if defined(JSON_HEDLEY_ARM_VERSION)
296  #undef JSON_HEDLEY_ARM_VERSION
297 #endif
298 #if defined(__CC_ARM) && defined(__ARMCOMPILER_VERSION)
299  #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCOMPILER_VERSION / 1000000, (__ARMCOMPILER_VERSION % 1000000) / 10000, (__ARMCOMPILER_VERSION % 10000) / 100)
300 #elif defined(__CC_ARM) && defined(__ARMCC_VERSION)
301  #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCC_VERSION / 1000000, (__ARMCC_VERSION % 1000000) / 10000, (__ARMCC_VERSION % 10000) / 100)
302 #endif
303 
304 #if defined(JSON_HEDLEY_ARM_VERSION_CHECK)
305  #undef JSON_HEDLEY_ARM_VERSION_CHECK
306 #endif
307 #if defined(JSON_HEDLEY_ARM_VERSION)
308  #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_ARM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
309 #else
310  #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (0)
311 #endif
312 
313 #if defined(JSON_HEDLEY_IBM_VERSION)
314  #undef JSON_HEDLEY_IBM_VERSION
315 #endif
316 #if defined(__ibmxl__)
317  #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ibmxl_version__, __ibmxl_release__, __ibmxl_modification__)
318 #elif defined(__xlC__) && defined(__xlC_ver__)
319  #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8, __xlC__ & 0xff, (__xlC_ver__ >> 8) & 0xff)
320 #elif defined(__xlC__)
321  #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8, __xlC__ & 0xff, 0)
322 #endif
323 
324 #if defined(JSON_HEDLEY_IBM_VERSION_CHECK)
325  #undef JSON_HEDLEY_IBM_VERSION_CHECK
326 #endif
327 #if defined(JSON_HEDLEY_IBM_VERSION)
328  #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IBM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
329 #else
330  #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (0)
331 #endif
332 
333 #if defined(JSON_HEDLEY_TI_VERSION)
334  #undef JSON_HEDLEY_TI_VERSION
335 #endif
336 #if \
337  defined(__TI_COMPILER_VERSION__) && \
338  ( \
339  defined(__TMS470__) || defined(__TI_ARM__) || \
340  defined(__MSP430__) || \
341  defined(__TMS320C2000__) \
342  )
343 #if (__TI_COMPILER_VERSION__ >= 16000000)
344  #define JSON_HEDLEY_TI_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
345 #endif
346 #endif
347 
348 #if defined(JSON_HEDLEY_TI_VERSION_CHECK)
349  #undef JSON_HEDLEY_TI_VERSION_CHECK
350 #endif
351 #if defined(JSON_HEDLEY_TI_VERSION)
352  #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
353 #else
354  #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (0)
355 #endif
356 
357 #if defined(JSON_HEDLEY_TI_CL2000_VERSION)
358  #undef JSON_HEDLEY_TI_CL2000_VERSION
359 #endif
360 #if defined(__TI_COMPILER_VERSION__) && defined(__TMS320C2000__)
361  #define JSON_HEDLEY_TI_CL2000_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
362 #endif
363 
364 #if defined(JSON_HEDLEY_TI_CL2000_VERSION_CHECK)
365  #undef JSON_HEDLEY_TI_CL2000_VERSION_CHECK
366 #endif
367 #if defined(JSON_HEDLEY_TI_CL2000_VERSION)
368  #define JSON_HEDLEY_TI_CL2000_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL2000_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
369 #else
370  #define JSON_HEDLEY_TI_CL2000_VERSION_CHECK(major,minor,patch) (0)
371 #endif
372 
373 #if defined(JSON_HEDLEY_TI_CL430_VERSION)
374  #undef JSON_HEDLEY_TI_CL430_VERSION
375 #endif
376 #if defined(__TI_COMPILER_VERSION__) && defined(__MSP430__)
377  #define JSON_HEDLEY_TI_CL430_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
378 #endif
379 
380 #if defined(JSON_HEDLEY_TI_CL430_VERSION_CHECK)
381  #undef JSON_HEDLEY_TI_CL430_VERSION_CHECK
382 #endif
383 #if defined(JSON_HEDLEY_TI_CL430_VERSION)
384  #define JSON_HEDLEY_TI_CL430_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL430_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
385 #else
386  #define JSON_HEDLEY_TI_CL430_VERSION_CHECK(major,minor,patch) (0)
387 #endif
388 
389 #if defined(JSON_HEDLEY_TI_ARMCL_VERSION)
390  #undef JSON_HEDLEY_TI_ARMCL_VERSION
391 #endif
392 #if defined(__TI_COMPILER_VERSION__) && (defined(__TMS470__) || defined(__TI_ARM__))
393  #define JSON_HEDLEY_TI_ARMCL_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
394 #endif
395 
396 #if defined(JSON_HEDLEY_TI_ARMCL_VERSION_CHECK)
397  #undef JSON_HEDLEY_TI_ARMCL_VERSION_CHECK
398 #endif
399 #if defined(JSON_HEDLEY_TI_ARMCL_VERSION)
400  #define JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_ARMCL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
401 #else
402  #define JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(major,minor,patch) (0)
403 #endif
404 
405 #if defined(JSON_HEDLEY_TI_CL6X_VERSION)
406  #undef JSON_HEDLEY_TI_CL6X_VERSION
407 #endif
408 #if defined(__TI_COMPILER_VERSION__) && defined(__TMS320C6X__)
409  #define JSON_HEDLEY_TI_CL6X_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
410 #endif
411 
412 #if defined(JSON_HEDLEY_TI_CL6X_VERSION_CHECK)
413  #undef JSON_HEDLEY_TI_CL6X_VERSION_CHECK
414 #endif
415 #if defined(JSON_HEDLEY_TI_CL6X_VERSION)
416  #define JSON_HEDLEY_TI_CL6X_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL6X_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
417 #else
418  #define JSON_HEDLEY_TI_CL6X_VERSION_CHECK(major,minor,patch) (0)
419 #endif
420 
421 #if defined(JSON_HEDLEY_TI_CL7X_VERSION)
422  #undef JSON_HEDLEY_TI_CL7X_VERSION
423 #endif
424 #if defined(__TI_COMPILER_VERSION__) && defined(__C7000__)
425  #define JSON_HEDLEY_TI_CL7X_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
426 #endif
427 
428 #if defined(JSON_HEDLEY_TI_CL7X_VERSION_CHECK)
429  #undef JSON_HEDLEY_TI_CL7X_VERSION_CHECK
430 #endif
431 #if defined(JSON_HEDLEY_TI_CL7X_VERSION)
432  #define JSON_HEDLEY_TI_CL7X_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL7X_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
433 #else
434  #define JSON_HEDLEY_TI_CL7X_VERSION_CHECK(major,minor,patch) (0)
435 #endif
436 
437 #if defined(JSON_HEDLEY_TI_CLPRU_VERSION)
438  #undef JSON_HEDLEY_TI_CLPRU_VERSION
439 #endif
440 #if defined(__TI_COMPILER_VERSION__) && defined(__PRU__)
441  #define JSON_HEDLEY_TI_CLPRU_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
442 #endif
443 
444 #if defined(JSON_HEDLEY_TI_CLPRU_VERSION_CHECK)
445  #undef JSON_HEDLEY_TI_CLPRU_VERSION_CHECK
446 #endif
447 #if defined(JSON_HEDLEY_TI_CLPRU_VERSION)
448  #define JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CLPRU_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
449 #else
450  #define JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(major,minor,patch) (0)
451 #endif
452 
453 #if defined(JSON_HEDLEY_CRAY_VERSION)
454  #undef JSON_HEDLEY_CRAY_VERSION
455 #endif
456 #if defined(_CRAYC)
457  #if defined(_RELEASE_PATCHLEVEL)
458  #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, _RELEASE_PATCHLEVEL)
459  #else
460  #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, 0)
461  #endif
462 #endif
463 
464 #if defined(JSON_HEDLEY_CRAY_VERSION_CHECK)
465  #undef JSON_HEDLEY_CRAY_VERSION_CHECK
466 #endif
467 #if defined(JSON_HEDLEY_CRAY_VERSION)
468  #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_CRAY_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
469 #else
470  #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (0)
471 #endif
472 
473 #if defined(JSON_HEDLEY_IAR_VERSION)
474  #undef JSON_HEDLEY_IAR_VERSION
475 #endif
476 #if defined(__IAR_SYSTEMS_ICC__)
477  #if __VER__ > 1000
478  #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE((__VER__ / 1000000), ((__VER__ / 1000) % 1000), (__VER__ % 1000))
479  #else
480  #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE(VER / 100, __VER__ % 100, 0)
481  #endif
482 #endif
483 
484 #if defined(JSON_HEDLEY_IAR_VERSION_CHECK)
485  #undef JSON_HEDLEY_IAR_VERSION_CHECK
486 #endif
487 #if defined(JSON_HEDLEY_IAR_VERSION)
488  #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IAR_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
489 #else
490  #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (0)
491 #endif
492 
493 #if defined(JSON_HEDLEY_TINYC_VERSION)
494  #undef JSON_HEDLEY_TINYC_VERSION
495 #endif
496 #if defined(__TINYC__)
497  #define JSON_HEDLEY_TINYC_VERSION JSON_HEDLEY_VERSION_ENCODE(__TINYC__ / 1000, (__TINYC__ / 100) % 10, __TINYC__ % 100)
498 #endif
499 
500 #if defined(JSON_HEDLEY_TINYC_VERSION_CHECK)
501  #undef JSON_HEDLEY_TINYC_VERSION_CHECK
502 #endif
503 #if defined(JSON_HEDLEY_TINYC_VERSION)
504  #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TINYC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
505 #else
506  #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (0)
507 #endif
508 
509 #if defined(JSON_HEDLEY_DMC_VERSION)
510  #undef JSON_HEDLEY_DMC_VERSION
511 #endif
512 #if defined(__DMC__)
513  #define JSON_HEDLEY_DMC_VERSION JSON_HEDLEY_VERSION_ENCODE(__DMC__ >> 8, (__DMC__ >> 4) & 0xf, __DMC__ & 0xf)
514 #endif
515 
516 #if defined(JSON_HEDLEY_DMC_VERSION_CHECK)
517  #undef JSON_HEDLEY_DMC_VERSION_CHECK
518 #endif
519 #if defined(JSON_HEDLEY_DMC_VERSION)
520  #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_DMC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
521 #else
522  #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (0)
523 #endif
524 
525 #if defined(JSON_HEDLEY_COMPCERT_VERSION)
526  #undef JSON_HEDLEY_COMPCERT_VERSION
527 #endif
528 #if defined(__COMPCERT_VERSION__)
529  #define JSON_HEDLEY_COMPCERT_VERSION JSON_HEDLEY_VERSION_ENCODE(__COMPCERT_VERSION__ / 10000, (__COMPCERT_VERSION__ / 100) % 100, __COMPCERT_VERSION__ % 100)
530 #endif
531 
532 #if defined(JSON_HEDLEY_COMPCERT_VERSION_CHECK)
533  #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK
534 #endif
535 #if defined(JSON_HEDLEY_COMPCERT_VERSION)
536  #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_COMPCERT_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
537 #else
538  #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (0)
539 #endif
540 
541 #if defined(JSON_HEDLEY_PELLES_VERSION)
542  #undef JSON_HEDLEY_PELLES_VERSION
543 #endif
544 #if defined(__POCC__)
545  #define JSON_HEDLEY_PELLES_VERSION JSON_HEDLEY_VERSION_ENCODE(__POCC__ / 100, __POCC__ % 100, 0)
546 #endif
547 
548 #if defined(JSON_HEDLEY_PELLES_VERSION_CHECK)
549  #undef JSON_HEDLEY_PELLES_VERSION_CHECK
550 #endif
551 #if defined(JSON_HEDLEY_PELLES_VERSION)
552  #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PELLES_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
553 #else
554  #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (0)
555 #endif
556 
557 #if defined(JSON_HEDLEY_GCC_VERSION)
558  #undef JSON_HEDLEY_GCC_VERSION
559 #endif
560 #if \
561  defined(JSON_HEDLEY_GNUC_VERSION) && \
562  !defined(__clang__) && \
563  !defined(JSON_HEDLEY_INTEL_VERSION) && \
564  !defined(JSON_HEDLEY_PGI_VERSION) && \
565  !defined(JSON_HEDLEY_ARM_VERSION) && \
566  !defined(JSON_HEDLEY_TI_VERSION) && \
567  !defined(JSON_HEDLEY_TI_ARMCL_VERSION) && \
568  !defined(JSON_HEDLEY_TI_CL430_VERSION) && \
569  !defined(JSON_HEDLEY_TI_CL2000_VERSION) && \
570  !defined(JSON_HEDLEY_TI_CL6X_VERSION) && \
571  !defined(JSON_HEDLEY_TI_CL7X_VERSION) && \
572  !defined(JSON_HEDLEY_TI_CLPRU_VERSION) && \
573  !defined(__COMPCERT__)
574  #define JSON_HEDLEY_GCC_VERSION JSON_HEDLEY_GNUC_VERSION
575 #endif
576 
577 #if defined(JSON_HEDLEY_GCC_VERSION_CHECK)
578  #undef JSON_HEDLEY_GCC_VERSION_CHECK
579 #endif
580 #if defined(JSON_HEDLEY_GCC_VERSION)
581  #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
582 #else
583  #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (0)
584 #endif
585 
586 #if defined(JSON_HEDLEY_HAS_ATTRIBUTE)
587  #undef JSON_HEDLEY_HAS_ATTRIBUTE
588 #endif
589 #if defined(__has_attribute)
590  #define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) __has_attribute(attribute)
591 #else
592  #define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) (0)
593 #endif
594 
595 #if defined(JSON_HEDLEY_GNUC_HAS_ATTRIBUTE)
596  #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE
597 #endif
598 #if defined(__has_attribute)
599  #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) __has_attribute(attribute)
600 #else
601  #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
602 #endif
603 
604 #if defined(JSON_HEDLEY_GCC_HAS_ATTRIBUTE)
605  #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE
606 #endif
607 #if defined(__has_attribute)
608  #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) __has_attribute(attribute)
609 #else
610  #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
611 #endif
612 
613 #if defined(JSON_HEDLEY_HAS_CPP_ATTRIBUTE)
614  #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE
615 #endif
616 #if \
617  defined(__has_cpp_attribute) && \
618  defined(__cplusplus) && \
619  (!defined(JSON_HEDLEY_SUNPRO_VERSION) || JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0))
620  #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) __has_cpp_attribute(attribute)
621 #else
622  #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) (0)
623 #endif
624 
625 #if defined(JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS)
626  #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS
627 #endif
628 #if !defined(__cplusplus) || !defined(__has_cpp_attribute)
629  #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) (0)
630 #elif \
631  !defined(JSON_HEDLEY_PGI_VERSION) && \
632  !defined(JSON_HEDLEY_IAR_VERSION) && \
633  (!defined(JSON_HEDLEY_SUNPRO_VERSION) || JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0)) && \
634  (!defined(JSON_HEDLEY_MSVC_VERSION) || JSON_HEDLEY_MSVC_VERSION_CHECK(19,20,0))
635  #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) JSON_HEDLEY_HAS_CPP_ATTRIBUTE(ns::attribute)
636 #else
637  #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) (0)
638 #endif
639 
640 #if defined(JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE)
641  #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE
642 #endif
643 #if defined(__has_cpp_attribute) && defined(__cplusplus)
644  #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute)
645 #else
646  #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
647 #endif
648 
649 #if defined(JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE)
650  #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE
651 #endif
652 #if defined(__has_cpp_attribute) && defined(__cplusplus)
653  #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute)
654 #else
655  #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
656 #endif
657 
658 #if defined(JSON_HEDLEY_HAS_BUILTIN)
659  #undef JSON_HEDLEY_HAS_BUILTIN
660 #endif
661 #if defined(__has_builtin)
662  #define JSON_HEDLEY_HAS_BUILTIN(builtin) __has_builtin(builtin)
663 #else
664  #define JSON_HEDLEY_HAS_BUILTIN(builtin) (0)
665 #endif
666 
667 #if defined(JSON_HEDLEY_GNUC_HAS_BUILTIN)
668  #undef JSON_HEDLEY_GNUC_HAS_BUILTIN
669 #endif
670 #if defined(__has_builtin)
671  #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin)
672 #else
673  #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
674 #endif
675 
676 #if defined(JSON_HEDLEY_GCC_HAS_BUILTIN)
677  #undef JSON_HEDLEY_GCC_HAS_BUILTIN
678 #endif
679 #if defined(__has_builtin)
680  #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin)
681 #else
682  #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
683 #endif
684 
685 #if defined(JSON_HEDLEY_HAS_FEATURE)
686  #undef JSON_HEDLEY_HAS_FEATURE
687 #endif
688 #if defined(__has_feature)
689  #define JSON_HEDLEY_HAS_FEATURE(feature) __has_feature(feature)
690 #else
691  #define JSON_HEDLEY_HAS_FEATURE(feature) (0)
692 #endif
693 
694 #if defined(JSON_HEDLEY_GNUC_HAS_FEATURE)
695  #undef JSON_HEDLEY_GNUC_HAS_FEATURE
696 #endif
697 #if defined(__has_feature)
698  #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature)
699 #else
700  #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
701 #endif
702 
703 #if defined(JSON_HEDLEY_GCC_HAS_FEATURE)
704  #undef JSON_HEDLEY_GCC_HAS_FEATURE
705 #endif
706 #if defined(__has_feature)
707  #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature)
708 #else
709  #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
710 #endif
711 
712 #if defined(JSON_HEDLEY_HAS_EXTENSION)
713  #undef JSON_HEDLEY_HAS_EXTENSION
714 #endif
715 #if defined(__has_extension)
716  #define JSON_HEDLEY_HAS_EXTENSION(extension) __has_extension(extension)
717 #else
718  #define JSON_HEDLEY_HAS_EXTENSION(extension) (0)
719 #endif
720 
721 #if defined(JSON_HEDLEY_GNUC_HAS_EXTENSION)
722  #undef JSON_HEDLEY_GNUC_HAS_EXTENSION
723 #endif
724 #if defined(__has_extension)
725  #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension)
726 #else
727  #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
728 #endif
729 
730 #if defined(JSON_HEDLEY_GCC_HAS_EXTENSION)
731  #undef JSON_HEDLEY_GCC_HAS_EXTENSION
732 #endif
733 #if defined(__has_extension)
734  #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension)
735 #else
736  #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
737 #endif
738 
739 #if defined(JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE)
740  #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE
741 #endif
742 #if defined(__has_declspec_attribute)
743  #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) __has_declspec_attribute(attribute)
744 #else
745  #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) (0)
746 #endif
747 
748 #if defined(JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE)
749  #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE
750 #endif
751 #if defined(__has_declspec_attribute)
752  #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute)
753 #else
754  #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
755 #endif
756 
757 #if defined(JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE)
758  #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE
759 #endif
760 #if defined(__has_declspec_attribute)
761  #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute)
762 #else
763  #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
764 #endif
765 
766 #if defined(JSON_HEDLEY_HAS_WARNING)
767  #undef JSON_HEDLEY_HAS_WARNING
768 #endif
769 #if defined(__has_warning)
770  #define JSON_HEDLEY_HAS_WARNING(warning) __has_warning(warning)
771 #else
772  #define JSON_HEDLEY_HAS_WARNING(warning) (0)
773 #endif
774 
775 #if defined(JSON_HEDLEY_GNUC_HAS_WARNING)
776  #undef JSON_HEDLEY_GNUC_HAS_WARNING
777 #endif
778 #if defined(__has_warning)
779  #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning)
780 #else
781  #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
782 #endif
783 
784 #if defined(JSON_HEDLEY_GCC_HAS_WARNING)
785  #undef JSON_HEDLEY_GCC_HAS_WARNING
786 #endif
787 #if defined(__has_warning)
788  #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning)
789 #else
790  #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
791 #endif
792 
793 /* JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_ is for
794  HEDLEY INTERNAL USE ONLY. API subject to change without notice. */
795 #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_)
796  #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_
797 #endif
798 #if defined(__cplusplus)
799 # if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat")
800 # if JSON_HEDLEY_HAS_WARNING("-Wc++17-extensions")
801 # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
802  JSON_HEDLEY_DIAGNOSTIC_PUSH \
803  _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
804  _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \
805  xpr \
806  JSON_HEDLEY_DIAGNOSTIC_POP
807 # else
808 # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
809  JSON_HEDLEY_DIAGNOSTIC_PUSH \
810  _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
811  xpr \
812  JSON_HEDLEY_DIAGNOSTIC_POP
813 # endif
814 # endif
815 #endif
816 #if !defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_)
817  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(x) x
818 #endif
819 
820 #if defined(JSON_HEDLEY_CONST_CAST)
821  #undef JSON_HEDLEY_CONST_CAST
822 #endif
823 #if defined(__cplusplus)
824 # define JSON_HEDLEY_CONST_CAST(T, expr) (const_cast<T>(expr))
825 #elif \
826  JSON_HEDLEY_HAS_WARNING("-Wcast-qual") || \
827  JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0) || \
828  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
829 # define JSON_HEDLEY_CONST_CAST(T, expr) (__extension__ ({ \
830  JSON_HEDLEY_DIAGNOSTIC_PUSH \
831  JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL \
832  ((T) (expr)); \
833  JSON_HEDLEY_DIAGNOSTIC_POP \
834  }))
835 #else
836 # define JSON_HEDLEY_CONST_CAST(T, expr) ((T) (expr))
837 #endif
838 
839 #if defined(JSON_HEDLEY_REINTERPRET_CAST)
840  #undef JSON_HEDLEY_REINTERPRET_CAST
841 #endif
842 #if defined(__cplusplus)
843  #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) (reinterpret_cast<T>(expr))
844 #else
845  #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) ((T) (expr))
846 #endif
847 
848 #if defined(JSON_HEDLEY_STATIC_CAST)
849  #undef JSON_HEDLEY_STATIC_CAST
850 #endif
851 #if defined(__cplusplus)
852  #define JSON_HEDLEY_STATIC_CAST(T, expr) (static_cast<T>(expr))
853 #else
854  #define JSON_HEDLEY_STATIC_CAST(T, expr) ((T) (expr))
855 #endif
856 
857 #if defined(JSON_HEDLEY_CPP_CAST)
858  #undef JSON_HEDLEY_CPP_CAST
859 #endif
860 #if defined(__cplusplus)
861 # if JSON_HEDLEY_HAS_WARNING("-Wold-style-cast")
862 # define JSON_HEDLEY_CPP_CAST(T, expr) \
863  JSON_HEDLEY_DIAGNOSTIC_PUSH \
864  _Pragma("clang diagnostic ignored \"-Wold-style-cast\"") \
865  ((T) (expr)) \
866  JSON_HEDLEY_DIAGNOSTIC_POP
867 # elif JSON_HEDLEY_IAR_VERSION_CHECK(8,3,0)
868 # define JSON_HEDLEY_CPP_CAST(T, expr) \
869  JSON_HEDLEY_DIAGNOSTIC_PUSH \
870  _Pragma("diag_suppress=Pe137") \
871  JSON_HEDLEY_DIAGNOSTIC_POP \
872 # else
873 # define JSON_HEDLEY_CPP_CAST(T, expr) ((T) (expr))
874 # endif
875 #else
876 # define JSON_HEDLEY_CPP_CAST(T, expr) (expr)
877 #endif
878 
879 #if \
880  (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
881  defined(__clang__) || \
882  JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \
883  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
884  JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \
885  JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \
886  JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
887  JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
888  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \
889  JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \
890  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \
891  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,0,0) || \
892  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
893  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
894  JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0) || \
895  JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,17) || \
896  JSON_HEDLEY_SUNPRO_VERSION_CHECK(8,0,0) || \
897  (JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) && defined(__C99_PRAGMA_OPERATOR))
898  #define JSON_HEDLEY_PRAGMA(value) _Pragma(#value)
899 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
900  #define JSON_HEDLEY_PRAGMA(value) __pragma(value)
901 #else
902  #define JSON_HEDLEY_PRAGMA(value)
903 #endif
904 
905 #if defined(JSON_HEDLEY_DIAGNOSTIC_PUSH)
906  #undef JSON_HEDLEY_DIAGNOSTIC_PUSH
907 #endif
908 #if defined(JSON_HEDLEY_DIAGNOSTIC_POP)
909  #undef JSON_HEDLEY_DIAGNOSTIC_POP
910 #endif
911 #if defined(__clang__)
912  #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("clang diagnostic push")
913  #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("clang diagnostic pop")
914 #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
915  #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)")
916  #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)")
917 #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0)
918  #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")
919  #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("GCC diagnostic pop")
920 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
921  #define JSON_HEDLEY_DIAGNOSTIC_PUSH __pragma(warning(push))
922  #define JSON_HEDLEY_DIAGNOSTIC_POP __pragma(warning(pop))
923 #elif JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0)
924  #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("push")
925  #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("pop")
926 #elif \
927  JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
928  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
929  JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,4,0) || \
930  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \
931  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
932  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0)
933  #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("diag_push")
934  #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("diag_pop")
935 #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0)
936  #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)")
937  #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)")
938 #else
939  #define JSON_HEDLEY_DIAGNOSTIC_PUSH
940  #define JSON_HEDLEY_DIAGNOSTIC_POP
941 #endif
942 
943 #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED)
944  #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
945 #endif
946 #if JSON_HEDLEY_HAS_WARNING("-Wdeprecated-declarations")
947  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")
948 #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
949  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warning(disable:1478 1786)")
950 #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
951  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444")
952 #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0)
953  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
954 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
955  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:4996))
956 #elif \
957  JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
958  (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
959  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
960  (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
961  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
962  (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
963  JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
964  (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
965  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
966  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
967  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0)
968  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1291,1718")
969 #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) && !defined(__cplusplus)
970  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,E_DEPRECATED_ATT,E_DEPRECATED_ATT_MESS)")
971 #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) && defined(__cplusplus)
972  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,symdeprecated,symdeprecated2)")
973 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
974  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress=Pe1444,Pe1215")
975 #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0)
976  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warn(disable:2241)")
977 #else
978  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
979 #endif
980 
981 #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS)
982  #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
983 #endif
984 #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
985  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("clang diagnostic ignored \"-Wunknown-pragmas\"")
986 #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
987  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("warning(disable:161)")
988 #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
989  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 1675")
990 #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0)
991  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("GCC diagnostic ignored \"-Wunknown-pragmas\"")
992 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
993  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:4068))
994 #elif \
995  JSON_HEDLEY_TI_VERSION_CHECK(16,9,0) || \
996  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \
997  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
998  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0)
999  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163")
1000 #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0)
1001  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163")
1002 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
1003  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress=Pe161")
1004 #else
1005  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
1006 #endif
1007 
1008 #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES)
1009  #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
1010 #endif
1011 #if JSON_HEDLEY_HAS_WARNING("-Wunknown-attributes")
1012  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("clang diagnostic ignored \"-Wunknown-attributes\"")
1013 #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0)
1014  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
1015 #elif JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0)
1016  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("warning(disable:1292)")
1017 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,0)
1018  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:5030))
1019 #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
1020  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097")
1021 #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus)
1022  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("error_messages(off,attrskipunsup)")
1023 #elif \
1024  JSON_HEDLEY_TI_VERSION_CHECK(18,1,0) || \
1025  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \
1026  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0)
1027  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1173")
1028 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
1029  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress=Pe1097")
1030 #else
1031  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
1032 #endif
1033 
1034 #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL)
1035  #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
1036 #endif
1037 #if JSON_HEDLEY_HAS_WARNING("-Wcast-qual")
1038  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("clang diagnostic ignored \"-Wcast-qual\"")
1039 #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
1040  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("warning(disable:2203 2331)")
1041 #elif JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0)
1042  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("GCC diagnostic ignored \"-Wcast-qual\"")
1043 #else
1044  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
1045 #endif
1046 
1047 #if defined(JSON_HEDLEY_DEPRECATED)
1048  #undef JSON_HEDLEY_DEPRECATED
1049 #endif
1050 #if defined(JSON_HEDLEY_DEPRECATED_FOR)
1051  #undef JSON_HEDLEY_DEPRECATED_FOR
1052 #endif
1053 #if JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0)
1054  #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated("Since " # since))
1055  #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated("Since " #since "; use " #replacement))
1056 #elif defined(__cplusplus) && (__cplusplus >= 201402L)
1057  #define JSON_HEDLEY_DEPRECATED(since) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since)]])
1058  #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since "; use " #replacement)]])
1059 #elif \
1060  JSON_HEDLEY_HAS_EXTENSION(attribute_deprecated_with_message) || \
1061  JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \
1062  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1063  JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \
1064  JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) || \
1065  JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
1066  JSON_HEDLEY_TI_VERSION_CHECK(18,1,0) || \
1067  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(18,1,0) || \
1068  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \
1069  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1070  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0)
1071  #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__("Since " #since)))
1072  #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__("Since " #since "; use " #replacement)))
1073 #elif \
1074  JSON_HEDLEY_HAS_ATTRIBUTE(deprecated) || \
1075  JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
1076  JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1077  JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1078  (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1079  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1080  (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1081  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1082  (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1083  JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1084  (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1085  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1086  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1087  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0)
1088  #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__))
1089  #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__))
1090 #elif \
1091  JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
1092  JSON_HEDLEY_PELLES_VERSION_CHECK(6,50,0)
1093  #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated)
1094  #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated)
1095 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
1096  #define JSON_HEDLEY_DEPRECATED(since) _Pragma("deprecated")
1097  #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) _Pragma("deprecated")
1098 #else
1099  #define JSON_HEDLEY_DEPRECATED(since)
1100  #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement)
1101 #endif
1102 
1103 #if defined(JSON_HEDLEY_UNAVAILABLE)
1104  #undef JSON_HEDLEY_UNAVAILABLE
1105 #endif
1106 #if \
1107  JSON_HEDLEY_HAS_ATTRIBUTE(warning) || \
1108  JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0) || \
1109  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
1110  #define JSON_HEDLEY_UNAVAILABLE(available_since) __attribute__((__warning__("Not available until " #available_since)))
1111 #else
1112  #define JSON_HEDLEY_UNAVAILABLE(available_since)
1113 #endif
1114 
1115 #if defined(JSON_HEDLEY_WARN_UNUSED_RESULT)
1116  #undef JSON_HEDLEY_WARN_UNUSED_RESULT
1117 #endif
1118 #if defined(JSON_HEDLEY_WARN_UNUSED_RESULT_MSG)
1119  #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG
1120 #endif
1121 #if (JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard) >= 201907L)
1122  #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
1123  #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard(msg)]])
1124 #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard)
1125  #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
1126  #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
1127 #elif \
1128  JSON_HEDLEY_HAS_ATTRIBUTE(warn_unused_result) || \
1129  JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
1130  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1131  JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1132  (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1133  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1134  (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1135  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1136  (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1137  JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1138  (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1139  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1140  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1141  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1142  (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \
1143  JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
1144  #define JSON_HEDLEY_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
1145  #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) __attribute__((__warn_unused_result__))
1146 #elif defined(_Check_return_) /* SAL */
1147  #define JSON_HEDLEY_WARN_UNUSED_RESULT _Check_return_
1148  #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) _Check_return_
1149 #else
1150  #define JSON_HEDLEY_WARN_UNUSED_RESULT
1151  #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg)
1152 #endif
1153 
1154 #if defined(JSON_HEDLEY_SENTINEL)
1155  #undef JSON_HEDLEY_SENTINEL
1156 #endif
1157 #if \
1158  JSON_HEDLEY_HAS_ATTRIBUTE(sentinel) || \
1159  JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
1160  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1161  JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0)
1162  #define JSON_HEDLEY_SENTINEL(position) __attribute__((__sentinel__(position)))
1163 #else
1164  #define JSON_HEDLEY_SENTINEL(position)
1165 #endif
1166 
1167 #if defined(JSON_HEDLEY_NO_RETURN)
1168  #undef JSON_HEDLEY_NO_RETURN
1169 #endif
1170 #if JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
1171  #define JSON_HEDLEY_NO_RETURN __noreturn
1172 #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
1173  #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__))
1174 #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
1175  #define JSON_HEDLEY_NO_RETURN _Noreturn
1176 #elif defined(__cplusplus) && (__cplusplus >= 201103L)
1177  #define JSON_HEDLEY_NO_RETURN JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[noreturn]])
1178 #elif \
1179  JSON_HEDLEY_HAS_ATTRIBUTE(noreturn) || \
1180  JSON_HEDLEY_GCC_VERSION_CHECK(3,2,0) || \
1181  JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
1182  JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1183  JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1184  JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1185  (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1186  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1187  (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1188  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1189  (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1190  JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1191  (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1192  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1193  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1194  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0)
1195  #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__))
1196 #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
1197  #define JSON_HEDLEY_NO_RETURN _Pragma("does_not_return")
1198 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0)
1199  #define JSON_HEDLEY_NO_RETURN __declspec(noreturn)
1200 #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus)
1201  #define JSON_HEDLEY_NO_RETURN _Pragma("FUNC_NEVER_RETURNS;")
1202 #elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3,2,0)
1203  #define JSON_HEDLEY_NO_RETURN __attribute((noreturn))
1204 #elif JSON_HEDLEY_PELLES_VERSION_CHECK(9,0,0)
1205  #define JSON_HEDLEY_NO_RETURN __declspec(noreturn)
1206 #else
1207  #define JSON_HEDLEY_NO_RETURN
1208 #endif
1209 
1210 #if defined(JSON_HEDLEY_NO_ESCAPE)
1211  #undef JSON_HEDLEY_NO_ESCAPE
1212 #endif
1213 #if JSON_HEDLEY_HAS_ATTRIBUTE(noescape)
1214  #define JSON_HEDLEY_NO_ESCAPE __attribute__((__noescape__))
1215 #else
1216  #define JSON_HEDLEY_NO_ESCAPE
1217 #endif
1218 
1219 #if defined(JSON_HEDLEY_UNREACHABLE)
1220  #undef JSON_HEDLEY_UNREACHABLE
1221 #endif
1222 #if defined(JSON_HEDLEY_UNREACHABLE_RETURN)
1223  #undef JSON_HEDLEY_UNREACHABLE_RETURN
1224 #endif
1225 #if defined(JSON_HEDLEY_ASSUME)
1226  #undef JSON_HEDLEY_ASSUME
1227 #endif
1228 #if \
1229  JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
1230  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
1231  #define JSON_HEDLEY_ASSUME(expr) __assume(expr)
1232 #elif JSON_HEDLEY_HAS_BUILTIN(__builtin_assume)
1233  #define JSON_HEDLEY_ASSUME(expr) __builtin_assume(expr)
1234 #elif \
1235  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
1236  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0)
1237  #if defined(__cplusplus)
1238  #define JSON_HEDLEY_ASSUME(expr) std::_nassert(expr)
1239  #else
1240  #define JSON_HEDLEY_ASSUME(expr) _nassert(expr)
1241  #endif
1242 #endif
1243 #if \
1244  (JSON_HEDLEY_HAS_BUILTIN(__builtin_unreachable) && (!defined(JSON_HEDLEY_ARM_VERSION))) || \
1245  JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \
1246  JSON_HEDLEY_PGI_VERSION_CHECK(18,10,0) || \
1247  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1248  JSON_HEDLEY_IBM_VERSION_CHECK(13,1,5)
1249  #define JSON_HEDLEY_UNREACHABLE() __builtin_unreachable()
1250 #elif defined(JSON_HEDLEY_ASSUME)
1251  #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0)
1252 #endif
1253 #if !defined(JSON_HEDLEY_ASSUME)
1254  #if defined(JSON_HEDLEY_UNREACHABLE)
1255  #define JSON_HEDLEY_ASSUME(expr) JSON_HEDLEY_STATIC_CAST(void, ((expr) ? 1 : (JSON_HEDLEY_UNREACHABLE(), 1)))
1256  #else
1257  #define JSON_HEDLEY_ASSUME(expr) JSON_HEDLEY_STATIC_CAST(void, expr)
1258  #endif
1259 #endif
1260 #if defined(JSON_HEDLEY_UNREACHABLE)
1261  #if \
1262  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
1263  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0)
1264  #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return (JSON_HEDLEY_STATIC_CAST(void, JSON_HEDLEY_ASSUME(0)), (value))
1265  #else
1266  #define JSON_HEDLEY_UNREACHABLE_RETURN(value) JSON_HEDLEY_UNREACHABLE()
1267  #endif
1268 #else
1269  #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return (value)
1270 #endif
1271 #if !defined(JSON_HEDLEY_UNREACHABLE)
1272  #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0)
1273 #endif
1274 
1276 #if JSON_HEDLEY_HAS_WARNING("-Wpedantic")
1277  #pragma clang diagnostic ignored "-Wpedantic"
1278 #endif
1279 #if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat-pedantic") && defined(__cplusplus)
1280  #pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
1281 #endif
1282 #if JSON_HEDLEY_GCC_HAS_WARNING("-Wvariadic-macros",4,0,0)
1283  #if defined(__clang__)
1284  #pragma clang diagnostic ignored "-Wvariadic-macros"
1285  #elif defined(JSON_HEDLEY_GCC_VERSION)
1286  #pragma GCC diagnostic ignored "-Wvariadic-macros"
1287  #endif
1288 #endif
1289 #if defined(JSON_HEDLEY_NON_NULL)
1290  #undef JSON_HEDLEY_NON_NULL
1291 #endif
1292 #if \
1293  JSON_HEDLEY_HAS_ATTRIBUTE(nonnull) || \
1294  JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
1295  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1296  JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0)
1297  #define JSON_HEDLEY_NON_NULL(...) __attribute__((__nonnull__(__VA_ARGS__)))
1298 #else
1299  #define JSON_HEDLEY_NON_NULL(...)
1300 #endif
1302 
1303 #if defined(JSON_HEDLEY_PRINTF_FORMAT)
1304  #undef JSON_HEDLEY_PRINTF_FORMAT
1305 #endif
1306 #if defined(__MINGW32__) && JSON_HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && !defined(__USE_MINGW_ANSI_STDIO)
1307  #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(ms_printf, string_idx, first_to_check)))
1308 #elif defined(__MINGW32__) && JSON_HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && defined(__USE_MINGW_ANSI_STDIO)
1309  #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(gnu_printf, string_idx, first_to_check)))
1310 #elif \
1311  JSON_HEDLEY_HAS_ATTRIBUTE(format) || \
1312  JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
1313  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1314  JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \
1315  JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1316  JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1317  (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1318  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1319  (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1320  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1321  (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1322  JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1323  (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1324  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1325  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1326  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0)
1327  #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(__printf__, string_idx, first_to_check)))
1328 #elif JSON_HEDLEY_PELLES_VERSION_CHECK(6,0,0)
1329  #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __declspec(vaformat(printf,string_idx,first_to_check))
1330 #else
1331  #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check)
1332 #endif
1333 
1334 #if defined(JSON_HEDLEY_CONSTEXPR)
1335  #undef JSON_HEDLEY_CONSTEXPR
1336 #endif
1337 #if defined(__cplusplus)
1338  #if __cplusplus >= 201103L
1339  #define JSON_HEDLEY_CONSTEXPR JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(constexpr)
1340  #endif
1341 #endif
1342 #if !defined(JSON_HEDLEY_CONSTEXPR)
1343  #define JSON_HEDLEY_CONSTEXPR
1344 #endif
1345 
1346 #if defined(JSON_HEDLEY_PREDICT)
1347  #undef JSON_HEDLEY_PREDICT
1348 #endif
1349 #if defined(JSON_HEDLEY_LIKELY)
1350  #undef JSON_HEDLEY_LIKELY
1351 #endif
1352 #if defined(JSON_HEDLEY_UNLIKELY)
1353  #undef JSON_HEDLEY_UNLIKELY
1354 #endif
1355 #if defined(JSON_HEDLEY_UNPREDICTABLE)
1356  #undef JSON_HEDLEY_UNPREDICTABLE
1357 #endif
1358 #if JSON_HEDLEY_HAS_BUILTIN(__builtin_unpredictable)
1359  #define JSON_HEDLEY_UNPREDICTABLE(expr) __builtin_unpredictable((expr))
1360 #endif
1361 #if \
1362  JSON_HEDLEY_HAS_BUILTIN(__builtin_expect_with_probability) || \
1363  JSON_HEDLEY_GCC_VERSION_CHECK(9,0,0)
1364 # define JSON_HEDLEY_PREDICT(expr, value, probability) __builtin_expect_with_probability( (expr), (value), (probability))
1365 # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) __builtin_expect_with_probability(!!(expr), 1 , (probability))
1366 # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) __builtin_expect_with_probability(!!(expr), 0 , (probability))
1367 # define JSON_HEDLEY_LIKELY(expr) __builtin_expect (!!(expr), 1 )
1368 # define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect (!!(expr), 0 )
1369 #elif \
1370  JSON_HEDLEY_HAS_BUILTIN(__builtin_expect) || \
1371  JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \
1372  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1373  (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \
1374  JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1375  JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1376  JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1377  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \
1378  JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \
1379  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \
1380  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
1381  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1382  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1383  JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,27) || \
1384  JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0)
1385 # define JSON_HEDLEY_PREDICT(expr, expected, probability) \
1386  (((probability) >= 0.9) ? __builtin_expect((expr), (expected)) : (JSON_HEDLEY_STATIC_CAST(void, expected), (expr)))
1387 # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) \
1388  (__extension__ ({ \
1389  double hedley_probability_ = (probability); \
1390  ((hedley_probability_ >= 0.9) ? __builtin_expect(!!(expr), 1) : ((hedley_probability_ <= 0.1) ? __builtin_expect(!!(expr), 0) : !!(expr))); \
1391  }))
1392 # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) \
1393  (__extension__ ({ \
1394  double hedley_probability_ = (probability); \
1395  ((hedley_probability_ >= 0.9) ? __builtin_expect(!!(expr), 0) : ((hedley_probability_ <= 0.1) ? __builtin_expect(!!(expr), 1) : !!(expr))); \
1396  }))
1397 # define JSON_HEDLEY_LIKELY(expr) __builtin_expect(!!(expr), 1)
1398 # define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect(!!(expr), 0)
1399 #else
1400 # define JSON_HEDLEY_PREDICT(expr, expected, probability) (JSON_HEDLEY_STATIC_CAST(void, expected), (expr))
1401 # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) (!!(expr))
1402 # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) (!!(expr))
1403 # define JSON_HEDLEY_LIKELY(expr) (!!(expr))
1404 # define JSON_HEDLEY_UNLIKELY(expr) (!!(expr))
1405 #endif
1406 #if !defined(JSON_HEDLEY_UNPREDICTABLE)
1407  #define JSON_HEDLEY_UNPREDICTABLE(expr) JSON_HEDLEY_PREDICT(expr, 1, 0.5)
1408 #endif
1409 
1410 #if defined(JSON_HEDLEY_MALLOC)
1411  #undef JSON_HEDLEY_MALLOC
1412 #endif
1413 #if \
1414  JSON_HEDLEY_HAS_ATTRIBUTE(malloc) || \
1415  JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
1416  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1417  JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
1418  JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1419  JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \
1420  JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1421  (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1422  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1423  (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1424  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1425  (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1426  JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1427  (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1428  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1429  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1430  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0)
1431  #define JSON_HEDLEY_MALLOC __attribute__((__malloc__))
1432 #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
1433  #define JSON_HEDLEY_MALLOC _Pragma("returns_new_memory")
1434 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(14, 0, 0)
1435  #define JSON_HEDLEY_MALLOC __declspec(restrict)
1436 #else
1437  #define JSON_HEDLEY_MALLOC
1438 #endif
1439 
1440 #if defined(JSON_HEDLEY_PURE)
1441  #undef JSON_HEDLEY_PURE
1442 #endif
1443 #if \
1444  JSON_HEDLEY_HAS_ATTRIBUTE(pure) || \
1445  JSON_HEDLEY_GCC_VERSION_CHECK(2,96,0) || \
1446  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1447  JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
1448  JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1449  JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1450  JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1451  (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1452  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1453  (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1454  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1455  (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1456  JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1457  (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1458  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1459  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1460  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1461  JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
1462 # define JSON_HEDLEY_PURE __attribute__((__pure__))
1463 #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
1464 # define JSON_HEDLEY_PURE _Pragma("does_not_write_global_data")
1465 #elif defined(__cplusplus) && \
1466  ( \
1467  JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \
1468  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0) || \
1469  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) \
1470  )
1471 # define JSON_HEDLEY_PURE _Pragma("FUNC_IS_PURE;")
1472 #else
1473 # define JSON_HEDLEY_PURE
1474 #endif
1475 
1476 #if defined(JSON_HEDLEY_CONST)
1477  #undef JSON_HEDLEY_CONST
1478 #endif
1479 #if \
1480  JSON_HEDLEY_HAS_ATTRIBUTE(const) || \
1481  JSON_HEDLEY_GCC_VERSION_CHECK(2,5,0) || \
1482  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1483  JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
1484  JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1485  JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1486  JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1487  (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1488  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1489  (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1490  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1491  (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1492  JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1493  (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1494  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1495  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1496  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1497  JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
1498  #define JSON_HEDLEY_CONST __attribute__((__const__))
1499 #elif \
1500  JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
1501  #define JSON_HEDLEY_CONST _Pragma("no_side_effect")
1502 #else
1503  #define JSON_HEDLEY_CONST JSON_HEDLEY_PURE
1504 #endif
1505 
1506 #if defined(JSON_HEDLEY_RESTRICT)
1507  #undef JSON_HEDLEY_RESTRICT
1508 #endif
1509 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && !defined(__cplusplus)
1510  #define JSON_HEDLEY_RESTRICT restrict
1511 #elif \
1512  JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
1513  JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
1514  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1515  JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1516  JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1517  JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
1518  JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1519  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,4) || \
1520  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \
1521  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1522  (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus)) || \
1523  JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \
1524  defined(__clang__)
1525  #define JSON_HEDLEY_RESTRICT __restrict
1526 #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,3,0) && !defined(__cplusplus)
1527  #define JSON_HEDLEY_RESTRICT _Restrict
1528 #else
1529  #define JSON_HEDLEY_RESTRICT
1530 #endif
1531 
1532 #if defined(JSON_HEDLEY_INLINE)
1533  #undef JSON_HEDLEY_INLINE
1534 #endif
1535 #if \
1536  (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
1537  (defined(__cplusplus) && (__cplusplus >= 199711L))
1538  #define JSON_HEDLEY_INLINE inline
1539 #elif \
1540  defined(JSON_HEDLEY_GCC_VERSION) || \
1541  JSON_HEDLEY_ARM_VERSION_CHECK(6,2,0)
1542  #define JSON_HEDLEY_INLINE __inline__
1543 #elif \
1544  JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \
1545  JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1546  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,1,0) || \
1547  JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \
1548  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
1549  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \
1550  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1551  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0)
1552  #define JSON_HEDLEY_INLINE __inline
1553 #else
1554  #define JSON_HEDLEY_INLINE
1555 #endif
1556 
1557 #if defined(JSON_HEDLEY_ALWAYS_INLINE)
1558  #undef JSON_HEDLEY_ALWAYS_INLINE
1559 #endif
1560 #if \
1561  JSON_HEDLEY_HAS_ATTRIBUTE(always_inline) || \
1562  JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
1563  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1564  JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
1565  JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1566  JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1567  JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1568  (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1569  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1570  (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1571  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1572  (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1573  JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1574  (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1575  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1576  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1577  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0)
1578 # define JSON_HEDLEY_ALWAYS_INLINE __attribute__((__always_inline__)) JSON_HEDLEY_INLINE
1579 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0)
1580 # define JSON_HEDLEY_ALWAYS_INLINE __forceinline
1581 #elif defined(__cplusplus) && \
1582  ( \
1583  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1584  JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1585  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1586  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
1587  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1588  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) \
1589  )
1590 # define JSON_HEDLEY_ALWAYS_INLINE _Pragma("FUNC_ALWAYS_INLINE;")
1591 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
1592 # define JSON_HEDLEY_ALWAYS_INLINE _Pragma("inline=forced")
1593 #else
1594 # define JSON_HEDLEY_ALWAYS_INLINE JSON_HEDLEY_INLINE
1595 #endif
1596 
1597 #if defined(JSON_HEDLEY_NEVER_INLINE)
1598  #undef JSON_HEDLEY_NEVER_INLINE
1599 #endif
1600 #if \
1601  JSON_HEDLEY_HAS_ATTRIBUTE(noinline) || \
1602  JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
1603  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1604  JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
1605  JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1606  JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1607  JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1608  (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1609  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1610  (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1611  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1612  (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1613  JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1614  (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1615  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1616  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1617  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0)
1618  #define JSON_HEDLEY_NEVER_INLINE __attribute__((__noinline__))
1619 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0)
1620  #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline)
1621 #elif JSON_HEDLEY_PGI_VERSION_CHECK(10,2,0)
1622  #define JSON_HEDLEY_NEVER_INLINE _Pragma("noinline")
1623 #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus)
1624  #define JSON_HEDLEY_NEVER_INLINE _Pragma("FUNC_CANNOT_INLINE;")
1625 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
1626  #define JSON_HEDLEY_NEVER_INLINE _Pragma("inline=never")
1627 #elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3,2,0)
1628  #define JSON_HEDLEY_NEVER_INLINE __attribute((noinline))
1629 #elif JSON_HEDLEY_PELLES_VERSION_CHECK(9,0,0)
1630  #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline)
1631 #else
1632  #define JSON_HEDLEY_NEVER_INLINE
1633 #endif
1634 
1635 #if defined(JSON_HEDLEY_PRIVATE)
1636  #undef JSON_HEDLEY_PRIVATE
1637 #endif
1638 #if defined(JSON_HEDLEY_PUBLIC)
1639  #undef JSON_HEDLEY_PUBLIC
1640 #endif
1641 #if defined(JSON_HEDLEY_IMPORT)
1642  #undef JSON_HEDLEY_IMPORT
1643 #endif
1644 #if defined(_WIN32) || defined(__CYGWIN__)
1645 # define JSON_HEDLEY_PRIVATE
1646 # define JSON_HEDLEY_PUBLIC __declspec(dllexport)
1647 # define JSON_HEDLEY_IMPORT __declspec(dllimport)
1648 #else
1649 # if \
1650  JSON_HEDLEY_HAS_ATTRIBUTE(visibility) || \
1651  JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
1652  JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
1653  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1654  JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1655  JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
1656  ( \
1657  defined(__TI_EABI__) && \
1658  ( \
1659  (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1660  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) \
1661  ) \
1662  )
1663 # define JSON_HEDLEY_PRIVATE __attribute__((__visibility__("hidden")))
1664 # define JSON_HEDLEY_PUBLIC __attribute__((__visibility__("default")))
1665 # else
1666 # define JSON_HEDLEY_PRIVATE
1667 # define JSON_HEDLEY_PUBLIC
1668 # endif
1669 # define JSON_HEDLEY_IMPORT extern
1670 #endif
1671 
1672 #if defined(JSON_HEDLEY_NO_THROW)
1673  #undef JSON_HEDLEY_NO_THROW
1674 #endif
1675 #if \
1676  JSON_HEDLEY_HAS_ATTRIBUTE(nothrow) || \
1677  JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
1678  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
1679  #define JSON_HEDLEY_NO_THROW __attribute__((__nothrow__))
1680 #elif \
1681  JSON_HEDLEY_MSVC_VERSION_CHECK(13,1,0) || \
1682  JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0)
1683  #define JSON_HEDLEY_NO_THROW __declspec(nothrow)
1684 #else
1685  #define JSON_HEDLEY_NO_THROW
1686 #endif
1687 
1688 #if defined(JSON_HEDLEY_FALL_THROUGH)
1689  #undef JSON_HEDLEY_FALL_THROUGH
1690 #endif
1691 #if \
1692  JSON_HEDLEY_HAS_ATTRIBUTE(fallthrough) || \
1693  JSON_HEDLEY_GCC_VERSION_CHECK(7,0,0)
1694  #define JSON_HEDLEY_FALL_THROUGH __attribute__((__fallthrough__))
1695 #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(clang,fallthrough)
1696  #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[clang::fallthrough]])
1697 #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(fallthrough)
1698  #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[fallthrough]])
1699 #elif defined(__fallthrough) /* SAL */
1700  #define JSON_HEDLEY_FALL_THROUGH __fallthrough
1701 #else
1702  #define JSON_HEDLEY_FALL_THROUGH
1703 #endif
1704 
1705 #if defined(JSON_HEDLEY_RETURNS_NON_NULL)
1706  #undef JSON_HEDLEY_RETURNS_NON_NULL
1707 #endif
1708 #if \
1709  JSON_HEDLEY_HAS_ATTRIBUTE(returns_nonnull) || \
1710  JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0)
1711  #define JSON_HEDLEY_RETURNS_NON_NULL __attribute__((__returns_nonnull__))
1712 #elif defined(_Ret_notnull_) /* SAL */
1713  #define JSON_HEDLEY_RETURNS_NON_NULL _Ret_notnull_
1714 #else
1715  #define JSON_HEDLEY_RETURNS_NON_NULL
1716 #endif
1717 
1718 #if defined(JSON_HEDLEY_ARRAY_PARAM)
1719  #undef JSON_HEDLEY_ARRAY_PARAM
1720 #endif
1721 #if \
1722  defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
1723  !defined(__STDC_NO_VLA__) && \
1724  !defined(__cplusplus) && \
1725  !defined(JSON_HEDLEY_PGI_VERSION) && \
1726  !defined(JSON_HEDLEY_TINYC_VERSION)
1727  #define JSON_HEDLEY_ARRAY_PARAM(name) (name)
1728 #else
1729  #define JSON_HEDLEY_ARRAY_PARAM(name)
1730 #endif
1731 
1732 #if defined(JSON_HEDLEY_IS_CONSTANT)
1733  #undef JSON_HEDLEY_IS_CONSTANT
1734 #endif
1735 #if defined(JSON_HEDLEY_REQUIRE_CONSTEXPR)
1736  #undef JSON_HEDLEY_REQUIRE_CONSTEXPR
1737 #endif
1738 /* JSON_HEDLEY_IS_CONSTEXPR_ is for
1739  HEDLEY INTERNAL USE ONLY. API subject to change without notice. */
1740 #if defined(JSON_HEDLEY_IS_CONSTEXPR_)
1741  #undef JSON_HEDLEY_IS_CONSTEXPR_
1742 #endif
1743 #if \
1744  JSON_HEDLEY_HAS_BUILTIN(__builtin_constant_p) || \
1745  JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
1746  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1747  JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,19) || \
1748  JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1749  JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
1750  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
1751  (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) && !defined(__cplusplus)) || \
1752  JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0)
1753  #define JSON_HEDLEY_IS_CONSTANT(expr) __builtin_constant_p(expr)
1754 #endif
1755 #if !defined(__cplusplus)
1756 # if \
1757  JSON_HEDLEY_HAS_BUILTIN(__builtin_types_compatible_p) || \
1758  JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
1759  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1760  JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
1761  JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
1762  JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) || \
1763  JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,24)
1764 #if defined(__INTPTR_TYPE__)
1765  #define JSON_HEDLEY_IS_CONSTEXPR_(expr) __builtin_types_compatible_p(__typeof__((1 ? (void*) ((__INTPTR_TYPE__) ((expr) * 0)) : (int*) 0)), int*)
1766 #else
1767  #include <stdint.h>
1768  #define JSON_HEDLEY_IS_CONSTEXPR_(expr) __builtin_types_compatible_p(__typeof__((1 ? (void*) ((intptr_t) ((expr) * 0)) : (int*) 0)), int*)
1769 #endif
1770 # elif \
1771  ( \
1772  defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) && \
1773  !defined(JSON_HEDLEY_SUNPRO_VERSION) && \
1774  !defined(JSON_HEDLEY_PGI_VERSION) && \
1775  !defined(JSON_HEDLEY_IAR_VERSION)) || \
1776  JSON_HEDLEY_HAS_EXTENSION(c_generic_selections) || \
1777  JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \
1778  JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0) || \
1779  JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \
1780  JSON_HEDLEY_ARM_VERSION_CHECK(5,3,0)
1781 #if defined(__INTPTR_TYPE__)
1782  #define JSON_HEDLEY_IS_CONSTEXPR_(expr) _Generic((1 ? (void*) ((__INTPTR_TYPE__) ((expr) * 0)) : (int*) 0), int*: 1, void*: 0)
1783 #else
1784  #include <stdint.h>
1785  #define JSON_HEDLEY_IS_CONSTEXPR_(expr) _Generic((1 ? (void*) ((intptr_t) * 0) : (int*) 0), int*: 1, void*: 0)
1786 #endif
1787 # elif \
1788  defined(JSON_HEDLEY_GCC_VERSION) || \
1789  defined(JSON_HEDLEY_INTEL_VERSION) || \
1790  defined(JSON_HEDLEY_TINYC_VERSION) || \
1791  defined(JSON_HEDLEY_TI_ARMCL_VERSION) || \
1792  JSON_HEDLEY_TI_CL430_VERSION_CHECK(18,12,0) || \
1793  defined(JSON_HEDLEY_TI_CL2000_VERSION) || \
1794  defined(JSON_HEDLEY_TI_CL6X_VERSION) || \
1795  defined(JSON_HEDLEY_TI_CL7X_VERSION) || \
1796  defined(JSON_HEDLEY_TI_CLPRU_VERSION) || \
1797  defined(__clang__)
1798 # define JSON_HEDLEY_IS_CONSTEXPR_(expr) ( \
1799  sizeof(void) != \
1800  sizeof(*( \
1801  1 ? \
1802  ((void*) ((expr) * 0L) ) : \
1803 ((struct { char v[sizeof(void) * 2]; } *) 1) \
1804  ) \
1805  ) \
1806  )
1807 # endif
1808 #endif
1809 #if defined(JSON_HEDLEY_IS_CONSTEXPR_)
1810  #if !defined(JSON_HEDLEY_IS_CONSTANT)
1811  #define JSON_HEDLEY_IS_CONSTANT(expr) JSON_HEDLEY_IS_CONSTEXPR_(expr)
1812  #endif
1813  #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (JSON_HEDLEY_IS_CONSTEXPR_(expr) ? (expr) : (-1))
1814 #else
1815  #if !defined(JSON_HEDLEY_IS_CONSTANT)
1816  #define JSON_HEDLEY_IS_CONSTANT(expr) (0)
1817  #endif
1818  #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (expr)
1819 #endif
1820 
1821 #if defined(JSON_HEDLEY_BEGIN_C_DECLS)
1822  #undef JSON_HEDLEY_BEGIN_C_DECLS
1823 #endif
1824 #if defined(JSON_HEDLEY_END_C_DECLS)
1825  #undef JSON_HEDLEY_END_C_DECLS
1826 #endif
1827 #if defined(JSON_HEDLEY_C_DECL)
1828  #undef JSON_HEDLEY_C_DECL
1829 #endif
1830 #if defined(__cplusplus)
1831  #define JSON_HEDLEY_BEGIN_C_DECLS extern "C" {
1832  #define JSON_HEDLEY_END_C_DECLS }
1833  #define JSON_HEDLEY_C_DECL extern "C"
1834 #else
1835  #define JSON_HEDLEY_BEGIN_C_DECLS
1836  #define JSON_HEDLEY_END_C_DECLS
1837  #define JSON_HEDLEY_C_DECL
1838 #endif
1839 
1840 #if defined(JSON_HEDLEY_STATIC_ASSERT)
1841  #undef JSON_HEDLEY_STATIC_ASSERT
1842 #endif
1843 #if \
1844  !defined(__cplusplus) && ( \
1845  (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)) || \
1846  JSON_HEDLEY_HAS_FEATURE(c_static_assert) || \
1847  JSON_HEDLEY_GCC_VERSION_CHECK(6,0,0) || \
1848  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1849  defined(_Static_assert) \
1850  )
1851 # define JSON_HEDLEY_STATIC_ASSERT(expr, message) _Static_assert(expr, message)
1852 #elif \
1853  (defined(__cplusplus) && (__cplusplus >= 201103L)) || \
1854  JSON_HEDLEY_MSVC_VERSION_CHECK(16,0,0)
1855 # define JSON_HEDLEY_STATIC_ASSERT(expr, message) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(static_assert(expr, message))
1856 #else
1857 # define JSON_HEDLEY_STATIC_ASSERT(expr, message)
1858 #endif
1859 
1860 #if defined(JSON_HEDLEY_NULL)
1861  #undef JSON_HEDLEY_NULL
1862 #endif
1863 #if defined(__cplusplus)
1864  #if __cplusplus >= 201103L
1865  #define JSON_HEDLEY_NULL JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(nullptr)
1866  #elif defined(NULL)
1867  #define JSON_HEDLEY_NULL NULL
1868  #else
1869  #define JSON_HEDLEY_NULL JSON_HEDLEY_STATIC_CAST(void*, 0)
1870  #endif
1871 #elif defined(NULL)
1872  #define JSON_HEDLEY_NULL NULL
1873 #else
1874  #define JSON_HEDLEY_NULL ((void*) 0)
1875 #endif
1876 
1877 #if defined(JSON_HEDLEY_MESSAGE)
1878  #undef JSON_HEDLEY_MESSAGE
1879 #endif
1880 #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
1881 # define JSON_HEDLEY_MESSAGE(msg) \
1882  JSON_HEDLEY_DIAGNOSTIC_PUSH \
1883  JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS \
1884  JSON_HEDLEY_PRAGMA(message msg) \
1885  JSON_HEDLEY_DIAGNOSTIC_POP
1886 #elif \
1887  JSON_HEDLEY_GCC_VERSION_CHECK(4,4,0) || \
1888  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
1889 # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message msg)
1890 #elif JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0)
1891 # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(_CRI message msg)
1892 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
1893 # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg))
1894 #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,0,0)
1895 # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg))
1896 #else
1897 # define JSON_HEDLEY_MESSAGE(msg)
1898 #endif
1899 
1900 #if defined(JSON_HEDLEY_WARNING)
1901  #undef JSON_HEDLEY_WARNING
1902 #endif
1903 #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
1904 # define JSON_HEDLEY_WARNING(msg) \
1905  JSON_HEDLEY_DIAGNOSTIC_PUSH \
1906  JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS \
1907  JSON_HEDLEY_PRAGMA(clang warning msg) \
1908  JSON_HEDLEY_DIAGNOSTIC_POP
1909 #elif \
1910  JSON_HEDLEY_GCC_VERSION_CHECK(4,8,0) || \
1911  JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \
1912  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
1913 # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(GCC warning msg)
1914 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
1915 # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(message(msg))
1916 #else
1917 # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_MESSAGE(msg)
1918 #endif
1919 
1920 #if defined(JSON_HEDLEY_REQUIRE)
1921  #undef JSON_HEDLEY_REQUIRE
1922 #endif
1923 #if defined(JSON_HEDLEY_REQUIRE_MSG)
1924  #undef JSON_HEDLEY_REQUIRE_MSG
1925 #endif
1926 #if JSON_HEDLEY_HAS_ATTRIBUTE(diagnose_if)
1927 # if JSON_HEDLEY_HAS_WARNING("-Wgcc-compat")
1928 # define JSON_HEDLEY_REQUIRE(expr) \
1929  JSON_HEDLEY_DIAGNOSTIC_PUSH \
1930  _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \
1931  __attribute__((diagnose_if(!(expr), #expr, "error"))) \
1932  JSON_HEDLEY_DIAGNOSTIC_POP
1933 # define JSON_HEDLEY_REQUIRE_MSG(expr,msg) \
1934  JSON_HEDLEY_DIAGNOSTIC_PUSH \
1935  _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \
1936  __attribute__((diagnose_if(!(expr), msg, "error"))) \
1937  JSON_HEDLEY_DIAGNOSTIC_POP
1938 # else
1939 # define JSON_HEDLEY_REQUIRE(expr) __attribute__((diagnose_if(!(expr), #expr, "error")))
1940 # define JSON_HEDLEY_REQUIRE_MSG(expr,msg) __attribute__((diagnose_if(!(expr), msg, "error")))
1941 # endif
1942 #else
1943 # define JSON_HEDLEY_REQUIRE(expr)
1944 # define JSON_HEDLEY_REQUIRE_MSG(expr,msg)
1945 #endif
1946 
1947 #if defined(JSON_HEDLEY_FLAGS)
1948  #undef JSON_HEDLEY_FLAGS
1949 #endif
1950 #if JSON_HEDLEY_HAS_ATTRIBUTE(flag_enum)
1951  #define JSON_HEDLEY_FLAGS __attribute__((__flag_enum__))
1952 #endif
1953 
1954 #if defined(JSON_HEDLEY_FLAGS_CAST)
1955  #undef JSON_HEDLEY_FLAGS_CAST
1956 #endif
1957 #if JSON_HEDLEY_INTEL_VERSION_CHECK(19,0,0)
1958 # define JSON_HEDLEY_FLAGS_CAST(T, expr) (__extension__ ({ \
1959  JSON_HEDLEY_DIAGNOSTIC_PUSH \
1960  _Pragma("warning(disable:188)") \
1961  ((T) (expr)); \
1962  JSON_HEDLEY_DIAGNOSTIC_POP \
1963  }))
1964 #else
1965 # define JSON_HEDLEY_FLAGS_CAST(T, expr) JSON_HEDLEY_STATIC_CAST(T, expr)
1966 #endif
1967 
1968 #if defined(JSON_HEDLEY_EMPTY_BASES)
1969  #undef JSON_HEDLEY_EMPTY_BASES
1970 #endif
1971 #if JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,23918) && !JSON_HEDLEY_MSVC_VERSION_CHECK(20,0,0)
1972  #define JSON_HEDLEY_EMPTY_BASES __declspec(empty_bases)
1973 #else
1974  #define JSON_HEDLEY_EMPTY_BASES
1975 #endif
1976 
1977 /* Remaining macros are deprecated. */
1978 
1979 #if defined(JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK)
1980  #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK
1981 #endif
1982 #if defined(__clang__)
1983  #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) (0)
1984 #else
1985  #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
1986 #endif
1987 
1988 #if defined(JSON_HEDLEY_CLANG_HAS_ATTRIBUTE)
1989  #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE
1990 #endif
1991 #define JSON_HEDLEY_CLANG_HAS_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
1992 
1993 #if defined(JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE)
1994  #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE
1995 #endif
1996 #define JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute)
1997 
1998 #if defined(JSON_HEDLEY_CLANG_HAS_BUILTIN)
1999  #undef JSON_HEDLEY_CLANG_HAS_BUILTIN
2000 #endif
2001 #define JSON_HEDLEY_CLANG_HAS_BUILTIN(builtin) JSON_HEDLEY_HAS_BUILTIN(builtin)
2002 
2003 #if defined(JSON_HEDLEY_CLANG_HAS_FEATURE)
2004  #undef JSON_HEDLEY_CLANG_HAS_FEATURE
2005 #endif
2006 #define JSON_HEDLEY_CLANG_HAS_FEATURE(feature) JSON_HEDLEY_HAS_FEATURE(feature)
2007 
2008 #if defined(JSON_HEDLEY_CLANG_HAS_EXTENSION)
2009  #undef JSON_HEDLEY_CLANG_HAS_EXTENSION
2010 #endif
2011 #define JSON_HEDLEY_CLANG_HAS_EXTENSION(extension) JSON_HEDLEY_HAS_EXTENSION(extension)
2012 
2013 #if defined(JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE)
2014  #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE
2015 #endif
2016 #define JSON_HEDLEY_CLANG_HAS_DECLSPEC_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute)
2017 
2018 #if defined(JSON_HEDLEY_CLANG_HAS_WARNING)
2019  #undef JSON_HEDLEY_CLANG_HAS_WARNING
2020 #endif
2021 #define JSON_HEDLEY_CLANG_HAS_WARNING(warning) JSON_HEDLEY_HAS_WARNING(warning)
2022 
2023 #endif /* !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < X) */
2024 
2025 
2026 // This file contains all internal macro definitions
2027 // You MUST include macro_unscope.hpp at the end of json.hpp to undef all of them
2028 
2029 // exclude unsupported compilers
2030 #if !defined(JSON_SKIP_UNSUPPORTED_COMPILER_CHECK)
2031  #if defined(__clang__)
2032  #if (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) < 30400
2033  #error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers"
2034  #endif
2035  #elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER))
2036  #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800
2037  #error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers"
2038  #endif
2039  #endif
2040 #endif
2041 
2042 // C++ language standard detection
2043 #if (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464
2044  #define JSON_HAS_CPP_17
2045  #define JSON_HAS_CPP_14
2046 #elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1)
2047  #define JSON_HAS_CPP_14
2048 #endif
2049 
2050 // disable float-equal warnings on GCC/clang
2051 #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
2052  #pragma GCC diagnostic push
2053  #pragma GCC diagnostic ignored "-Wfloat-equal"
2054 #endif
2055 
2056 // disable documentation warnings on clang
2057 #if defined(__clang__)
2058  #pragma GCC diagnostic push
2059  #pragma GCC diagnostic ignored "-Wdocumentation"
2060 #endif
2061 
2062 // allow to disable exceptions
2063 #if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION)
2064  #define JSON_THROW(exception) throw exception
2065  #define JSON_TRY try
2066  #define JSON_CATCH(exception) catch(exception)
2067  #define JSON_INTERNAL_CATCH(exception) catch(exception)
2068 #else
2069  #include <cstdlib>
2070  #define JSON_THROW(exception) std::abort()
2071  #define JSON_TRY if(true)
2072  #define JSON_CATCH(exception) if(false)
2073  #define JSON_INTERNAL_CATCH(exception) if(false)
2074 #endif
2075 
2076 // override exception macros
2077 #if defined(JSON_THROW_USER)
2078  #undef JSON_THROW
2079  #define JSON_THROW JSON_THROW_USER
2080 #endif
2081 #if defined(JSON_TRY_USER)
2082  #undef JSON_TRY
2083  #define JSON_TRY JSON_TRY_USER
2084 #endif
2085 #if defined(JSON_CATCH_USER)
2086  #undef JSON_CATCH
2087  #define JSON_CATCH JSON_CATCH_USER
2088  #undef JSON_INTERNAL_CATCH
2089  #define JSON_INTERNAL_CATCH JSON_CATCH_USER
2090 #endif
2091 #if defined(JSON_INTERNAL_CATCH_USER)
2092  #undef JSON_INTERNAL_CATCH
2093  #define JSON_INTERNAL_CATCH JSON_INTERNAL_CATCH_USER
2094 #endif
2095 
2101 #define NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...) \
2102  template<typename BasicJsonType> \
2103  inline void to_json(BasicJsonType& j, const ENUM_TYPE& e) \
2104  { \
2105  static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
2106  static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
2107  auto it = std::find_if(std::begin(m), std::end(m), \
2108  [e](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
2109  { \
2110  return ej_pair.first == e; \
2111  }); \
2112  j = ((it != std::end(m)) ? it : std::begin(m))->second; \
2113  } \
2114  template<typename BasicJsonType> \
2115  inline void from_json(const BasicJsonType& j, ENUM_TYPE& e) \
2116  { \
2117  static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
2118  static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
2119  auto it = std::find_if(std::begin(m), std::end(m), \
2120  [&j](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
2121  { \
2122  return ej_pair.second == j; \
2123  }); \
2124  e = ((it != std::end(m)) ? it : std::begin(m))->first; \
2125  }
2126 
2127 // Ugly macros to avoid uglier copy-paste when specializing basic_json. They
2128 // may be removed in the future once the class is split.
2129 
2130 #define NLOHMANN_BASIC_JSON_TPL_DECLARATION \
2131  template<template<typename, typename, typename...> class ObjectType, \
2132  template<typename, typename...> class ArrayType, \
2133  class StringType, class BooleanType, class NumberIntegerType, \
2134  class NumberUnsignedType, class NumberFloatType, \
2135  template<typename> class AllocatorType, \
2136  template<typename, typename = void> class JSONSerializer>
2137 
2138 #define NLOHMANN_BASIC_JSON_TPL \
2139  basic_json<ObjectType, ArrayType, StringType, BooleanType, \
2140  NumberIntegerType, NumberUnsignedType, NumberFloatType, \
2141  AllocatorType, JSONSerializer>
2142 
2143 
2144 namespace nlohmann
2145 {
2146 namespace detail
2147 {
2149 // exceptions //
2151 
2180 class exception : public std::exception
2181 {
2182  public:
2185  const char* what() const noexcept override
2186  {
2187  return m.what();
2188  }
2189 
2191  const int id;
2192 
2193  protected:
2195  exception(int id_, const char* what_arg) : id(id_), m(what_arg) {}
2196 
2197  static std::string name(const std::string& ename, int id_)
2198  {
2199  return "[json.exception." + ename + "." + std::to_string(id_) + "] ";
2200  }
2201 
2202  private:
2204  std::runtime_error m;
2205 };
2206 
2251 class parse_error : public exception
2252 {
2253  public:
2263  static parse_error create(int id_, const position_t& pos, const std::string& what_arg)
2264  {
2265  std::string w = exception::name("parse_error", id_) + "parse error" +
2266  position_string(pos) + ": " + what_arg;
2267  return parse_error(id_, pos.chars_read_total, w.c_str());
2268  }
2269 
2270  static parse_error create(int id_, std::size_t byte_, const std::string& what_arg)
2271  {
2272  std::string w = exception::name("parse_error", id_) + "parse error" +
2273  (byte_ != 0 ? (" at byte " + std::to_string(byte_)) : "") +
2274  ": " + what_arg;
2275  return parse_error(id_, byte_, w.c_str());
2276  }
2277 
2287  const std::size_t byte;
2288 
2289  private:
2290  parse_error(int id_, std::size_t byte_, const char* what_arg)
2291  : exception(id_, what_arg), byte(byte_) {}
2292 
2293  static std::string position_string(const position_t& pos)
2294  {
2295  return " at line " + std::to_string(pos.lines_read + 1) +
2296  ", column " + std::to_string(pos.chars_read_current_line);
2297  }
2298 };
2299 
2338 {
2339  public:
2340  static invalid_iterator create(int id_, const std::string& what_arg)
2341  {
2342  std::string w = exception::name("invalid_iterator", id_) + what_arg;
2343  return invalid_iterator(id_, w.c_str());
2344  }
2345 
2346  private:
2348  invalid_iterator(int id_, const char* what_arg)
2349  : exception(id_, what_arg) {}
2350 };
2351 
2391 class type_error : public exception
2392 {
2393  public:
2394  static type_error create(int id_, const std::string& what_arg)
2395  {
2396  std::string w = exception::name("type_error", id_) + what_arg;
2397  return type_error(id_, w.c_str());
2398  }
2399 
2400  private:
2402  type_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
2403 };
2404 
2438 class out_of_range : public exception
2439 {
2440  public:
2441  static out_of_range create(int id_, const std::string& what_arg)
2442  {
2443  std::string w = exception::name("out_of_range", id_) + what_arg;
2444  return out_of_range(id_, w.c_str());
2445  }
2446 
2447  private:
2449  out_of_range(int id_, const char* what_arg) : exception(id_, what_arg) {}
2450 };
2451 
2476 class other_error : public exception
2477 {
2478  public:
2479  static other_error create(int id_, const std::string& what_arg)
2480  {
2481  std::string w = exception::name("other_error", id_) + what_arg;
2482  return other_error(id_, w.c_str());
2483  }
2484 
2485  private:
2487  other_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
2488 };
2489 } // namespace detail
2490 } // namespace nlohmann
2491 
2492 // #include <nlohmann/detail/macro_scope.hpp>
2493 
2494 // #include <nlohmann/detail/meta/cpp_future.hpp>
2495 
2496 
2497 #include <ciso646> // not
2498 #include <cstddef> // size_t
2499 #include <type_traits> // conditional, enable_if, false_type, integral_constant, is_constructible, is_integral, is_same, remove_cv, remove_reference, true_type
2500 
2501 namespace nlohmann
2502 {
2503 namespace detail
2504 {
2505 // alias templates to reduce boilerplate
2506 template<bool B, typename T = void>
2507 using enable_if_t = typename std::enable_if<B, T>::type;
2508 
2509 template<typename T>
2510 using uncvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
2511 
2512 // implementation of C++14 index_sequence and affiliates
2513 // source: https://stackoverflow.com/a/32223343
2514 template<std::size_t... Ints>
2516 {
2518  using value_type = std::size_t;
2519  static constexpr std::size_t size() noexcept
2520  {
2521  return sizeof...(Ints);
2522  }
2523 };
2524 
2525 template<class Sequence1, class Sequence2>
2527 
2528 template<std::size_t... I1, std::size_t... I2>
2530  : index_sequence < I1..., (sizeof...(I1) + I2)... > {};
2531 
2532 template<std::size_t N>
2534  : merge_and_renumber < typename make_index_sequence < N / 2 >::type,
2535  typename make_index_sequence < N - N / 2 >::type > {};
2536 
2537 template<> struct make_index_sequence<0> : index_sequence<> {};
2538 template<> struct make_index_sequence<1> : index_sequence<0> {};
2539 
2540 template<typename... Ts>
2542 
2543 // dispatch utility (taken from ranges-v3)
2544 template<unsigned N> struct priority_tag : priority_tag < N - 1 > {};
2545 template<> struct priority_tag<0> {};
2546 
2547 // taken from ranges-v3
2548 template<typename T>
2550 {
2551  static constexpr T value{};
2552 };
2553 
2554 template<typename T>
2555 constexpr T static_const<T>::value;
2556 } // namespace detail
2557 } // namespace nlohmann
2558 
2559 // #include <nlohmann/detail/meta/type_traits.hpp>
2560 
2561 
2562 #include <ciso646> // not
2563 #include <limits> // numeric_limits
2564 #include <type_traits> // false_type, is_constructible, is_integral, is_same, true_type
2565 #include <utility> // declval
2566 
2567 // #include <nlohmann/detail/iterators/iterator_traits.hpp>
2568 
2569 
2570 #include <iterator> // random_access_iterator_tag
2571 
2572 // #include <nlohmann/detail/meta/void_t.hpp>
2573 
2574 
2575 namespace nlohmann
2576 {
2577 namespace detail
2578 {
2579 template <typename ...Ts> struct make_void
2580 {
2581  using type = void;
2582 };
2583 template <typename ...Ts> using void_t = typename make_void<Ts...>::type;
2584 } // namespace detail
2585 } // namespace nlohmann
2586 
2587 // #include <nlohmann/detail/meta/cpp_future.hpp>
2588 
2589 
2590 namespace nlohmann
2591 {
2592 namespace detail
2593 {
2594 template <typename It, typename = void>
2595 struct iterator_types {};
2596 
2597 template <typename It>
2599  It,
2600  void_t<typename It::difference_type, typename It::value_type, typename It::pointer,
2601  typename It::reference, typename It::iterator_category >>
2602 {
2603  using difference_type = typename It::difference_type;
2604  using value_type = typename It::value_type;
2605  using pointer = typename It::pointer;
2606  using reference = typename It::reference;
2607  using iterator_category = typename It::iterator_category;
2608 };
2609 
2610 // This is required as some compilers implement std::iterator_traits in a way that
2611 // doesn't work with SFINAE. See https://github.com/nlohmann/json/issues/1341.
2612 template <typename T, typename = void>
2614 {
2615 };
2616 
2617 template <typename T>
2618 struct iterator_traits < T, enable_if_t < !std::is_pointer<T>::value >>
2619  : iterator_types<T>
2620 {
2621 };
2622 
2623 template <typename T>
2625 {
2626  using iterator_category = std::random_access_iterator_tag;
2627  using value_type = T;
2628  using difference_type = ptrdiff_t;
2629  using pointer = T*;
2630  using reference = T&;
2631 };
2632 } // namespace detail
2633 } // namespace nlohmann
2634 
2635 // #include <nlohmann/detail/macro_scope.hpp>
2636 
2637 // #include <nlohmann/detail/meta/cpp_future.hpp>
2638 
2639 // #include <nlohmann/detail/meta/detected.hpp>
2640 
2641 
2642 #include <type_traits>
2643 
2644 // #include <nlohmann/detail/meta/void_t.hpp>
2645 
2646 
2647 // https://en.cppreference.com/w/cpp/experimental/is_detected
2648 namespace nlohmann
2649 {
2650 namespace detail
2651 {
2652 struct nonesuch
2653 {
2654  nonesuch() = delete;
2655  ~nonesuch() = delete;
2656  nonesuch(nonesuch const&) = delete;
2657  nonesuch(nonesuch const&&) = delete;
2658  void operator=(nonesuch const&) = delete;
2659  void operator=(nonesuch&&) = delete;
2660 };
2661 
2662 template <class Default,
2663  class AlwaysVoid,
2664  template <class...> class Op,
2665  class... Args>
2666 struct detector
2667 {
2668  using value_t = std::false_type;
2669  using type = Default;
2670 };
2671 
2672 template <class Default, template <class...> class Op, class... Args>
2673 struct detector<Default, void_t<Op<Args...>>, Op, Args...>
2674 {
2675  using value_t = std::true_type;
2676  using type = Op<Args...>;
2677 };
2678 
2679 template <template <class...> class Op, class... Args>
2680 using is_detected = typename detector<nonesuch, void, Op, Args...>::value_t;
2681 
2682 template <template <class...> class Op, class... Args>
2683 using detected_t = typename detector<nonesuch, void, Op, Args...>::type;
2684 
2685 template <class Default, template <class...> class Op, class... Args>
2686 using detected_or = detector<Default, void, Op, Args...>;
2687 
2688 template <class Default, template <class...> class Op, class... Args>
2689 using detected_or_t = typename detected_or<Default, Op, Args...>::type;
2690 
2691 template <class Expected, template <class...> class Op, class... Args>
2692 using is_detected_exact = std::is_same<Expected, detected_t<Op, Args...>>;
2693 
2694 template <class To, template <class...> class Op, class... Args>
2696  std::is_convertible<detected_t<Op, Args...>, To>;
2697 } // namespace detail
2698 } // namespace nlohmann
2699 
2700 // #include <nlohmann/json_fwd.hpp>
2701 #ifndef INCLUDE_NLOHMANN_JSON_FWD_HPP_
2702 #define INCLUDE_NLOHMANN_JSON_FWD_HPP_
2703 
2704 #include <cstdint> // int64_t, uint64_t
2705 #include <map> // map
2706 #include <memory> // allocator
2707 #include <string> // string
2708 #include <vector> // vector
2709 
2715 namespace nlohmann
2716 {
2724 template<typename T = void, typename SFINAE = void>
2726 
2727 template<template<typename U, typename V, typename... Args> class ObjectType =
2728  std::map,
2729  template<typename U, typename... Args> class ArrayType = std::vector,
2730  class StringType = std::string, class BooleanType = bool,
2731  class NumberIntegerType = std::int64_t,
2732  class NumberUnsignedType = std::uint64_t,
2733  class NumberFloatType = double,
2734  template<typename U> class AllocatorType = std::allocator,
2735  template<typename T, typename SFINAE = void> class JSONSerializer =
2738 
2750 template<typename BasicJsonType>
2752 
2762 } // namespace nlohmann
2763 
2764 #endif // INCLUDE_NLOHMANN_JSON_FWD_HPP_
2765 
2766 
2767 namespace nlohmann
2768 {
2777 namespace detail
2778 {
2780 // helpers //
2782 
2783 // Note to maintainers:
2784 //
2785 // Every trait in this file expects a non CV-qualified type.
2786 // The only exceptions are in the 'aliases for detected' section
2787 // (i.e. those of the form: decltype(T::member_function(std::declval<T>())))
2788 //
2789 // In this case, T has to be properly CV-qualified to constraint the function arguments
2790 // (e.g. to_json(BasicJsonType&, const T&))
2791 
2792 template<typename> struct is_basic_json : std::false_type {};
2793 
2795 struct is_basic_json<NLOHMANN_BASIC_JSON_TPL> : std::true_type {};
2796 
2798 // json_ref helpers //
2800 
2801 template <typename>
2802 class json_ref;
2803 
2804 template<typename>
2805 struct is_json_ref : std::false_type {};
2806 
2807 template <typename T>
2808 struct is_json_ref<json_ref<T>> : std::true_type {};
2809 
2811 // aliases for detected //
2813 
2814 template <typename T>
2815 using mapped_type_t = typename T::mapped_type;
2816 
2817 template <typename T>
2818 using key_type_t = typename T::key_type;
2819 
2820 template <typename T>
2821 using value_type_t = typename T::value_type;
2822 
2823 template <typename T>
2824 using difference_type_t = typename T::difference_type;
2825 
2826 template <typename T>
2827 using pointer_t = typename T::pointer;
2828 
2829 template <typename T>
2830 using reference_t = typename T::reference;
2831 
2832 template <typename T>
2833 using iterator_category_t = typename T::iterator_category;
2834 
2835 template <typename T>
2836 using iterator_t = typename T::iterator;
2837 
2838 template <typename T, typename... Args>
2839 using to_json_function = decltype(T::to_json(std::declval<Args>()...));
2840 
2841 template <typename T, typename... Args>
2842 using from_json_function = decltype(T::from_json(std::declval<Args>()...));
2843 
2844 template <typename T, typename U>
2845 using get_template_function = decltype(std::declval<T>().template get<U>());
2846 
2847 // trait checking if JSONSerializer<T>::from_json(json const&, udt&) exists
2848 template <typename BasicJsonType, typename T, typename = void>
2849 struct has_from_json : std::false_type {};
2850 
2851 template <typename BasicJsonType, typename T>
2852 struct has_from_json<BasicJsonType, T,
2854 {
2855  using serializer = typename BasicJsonType::template json_serializer<T, void>;
2856 
2857  static constexpr bool value =
2859  const BasicJsonType&, T&>::value;
2860 };
2861 
2862 // This trait checks if JSONSerializer<T>::from_json(json const&) exists
2863 // this overload is used for non-default-constructible user-defined-types
2864 template <typename BasicJsonType, typename T, typename = void>
2865 struct has_non_default_from_json : std::false_type {};
2866 
2867 template<typename BasicJsonType, typename T>
2869 {
2870  using serializer = typename BasicJsonType::template json_serializer<T, void>;
2871 
2872  static constexpr bool value =
2874  const BasicJsonType&>::value;
2875 };
2876 
2877 // This trait checks if BasicJsonType::json_serializer<T>::to_json exists
2878 // Do not evaluate the trait when T is a basic_json type, to avoid template instantiation infinite recursion.
2879 template <typename BasicJsonType, typename T, typename = void>
2880 struct has_to_json : std::false_type {};
2881 
2882 template <typename BasicJsonType, typename T>
2883 struct has_to_json<BasicJsonType, T, enable_if_t<not is_basic_json<T>::value>>
2884 {
2885  using serializer = typename BasicJsonType::template json_serializer<T, void>;
2886 
2887  static constexpr bool value =
2889  T>::value;
2890 };
2891 
2892 
2894 // is_ functions //
2896 
2897 template <typename T, typename = void>
2898 struct is_iterator_traits : std::false_type {};
2899 
2900 template <typename T>
2902 {
2903  private:
2905 
2906  public:
2907  static constexpr auto value =
2913 };
2914 
2915 // source: https://stackoverflow.com/a/37193089/4116453
2916 
2917 template <typename T, typename = void>
2918 struct is_complete_type : std::false_type {};
2919 
2920 template <typename T>
2921 struct is_complete_type<T, decltype(void(sizeof(T)))> : std::true_type {};
2922 
2923 template <typename BasicJsonType, typename CompatibleObjectType,
2924  typename = void>
2925 struct is_compatible_object_type_impl : std::false_type {};
2926 
2927 template <typename BasicJsonType, typename CompatibleObjectType>
2929  BasicJsonType, CompatibleObjectType,
2930  enable_if_t<is_detected<mapped_type_t, CompatibleObjectType>::value and
2931  is_detected<key_type_t, CompatibleObjectType>::value >>
2932 {
2933 
2934  using object_t = typename BasicJsonType::object_t;
2935 
2936  // macOS's is_constructible does not play well with nonesuch...
2937  static constexpr bool value =
2938  std::is_constructible<typename object_t::key_type,
2939  typename CompatibleObjectType::key_type>::value and
2940  std::is_constructible<typename object_t::mapped_type,
2941  typename CompatibleObjectType::mapped_type>::value;
2942 };
2943 
2944 template <typename BasicJsonType, typename CompatibleObjectType>
2946  : is_compatible_object_type_impl<BasicJsonType, CompatibleObjectType> {};
2947 
2948 template <typename BasicJsonType, typename ConstructibleObjectType,
2949  typename = void>
2950 struct is_constructible_object_type_impl : std::false_type {};
2951 
2952 template <typename BasicJsonType, typename ConstructibleObjectType>
2954  BasicJsonType, ConstructibleObjectType,
2955  enable_if_t<is_detected<mapped_type_t, ConstructibleObjectType>::value and
2956  is_detected<key_type_t, ConstructibleObjectType>::value >>
2957 {
2958  using object_t = typename BasicJsonType::object_t;
2959 
2960  static constexpr bool value =
2964  (std::is_constructible<typename ConstructibleObjectType::key_type,
2965  typename object_t::key_type>::value and
2966  std::is_same <
2967  typename object_t::mapped_type,
2968  typename ConstructibleObjectType::mapped_type >::value)) or
2969  (has_from_json<BasicJsonType,
2970  typename ConstructibleObjectType::mapped_type>::value or
2972  BasicJsonType,
2973  typename ConstructibleObjectType::mapped_type >::value);
2974 };
2975 
2976 template <typename BasicJsonType, typename ConstructibleObjectType>
2978  : is_constructible_object_type_impl<BasicJsonType,
2979  ConstructibleObjectType> {};
2980 
2981 template <typename BasicJsonType, typename CompatibleStringType,
2982  typename = void>
2983 struct is_compatible_string_type_impl : std::false_type {};
2984 
2985 template <typename BasicJsonType, typename CompatibleStringType>
2987  BasicJsonType, CompatibleStringType,
2988  enable_if_t<is_detected_exact<typename BasicJsonType::string_t::value_type,
2989  value_type_t, CompatibleStringType>::value >>
2990 {
2991  static constexpr auto value =
2993 };
2994 
2995 template <typename BasicJsonType, typename ConstructibleStringType>
2997  : is_compatible_string_type_impl<BasicJsonType, ConstructibleStringType> {};
2998 
2999 template <typename BasicJsonType, typename ConstructibleStringType,
3000  typename = void>
3001 struct is_constructible_string_type_impl : std::false_type {};
3002 
3003 template <typename BasicJsonType, typename ConstructibleStringType>
3005  BasicJsonType, ConstructibleStringType,
3006  enable_if_t<is_detected_exact<typename BasicJsonType::string_t::value_type,
3007  value_type_t, ConstructibleStringType>::value >>
3008 {
3009  static constexpr auto value =
3010  std::is_constructible<ConstructibleStringType,
3011  typename BasicJsonType::string_t>::value;
3012 };
3013 
3014 template <typename BasicJsonType, typename ConstructibleStringType>
3016  : is_constructible_string_type_impl<BasicJsonType, ConstructibleStringType> {};
3017 
3018 template <typename BasicJsonType, typename CompatibleArrayType, typename = void>
3019 struct is_compatible_array_type_impl : std::false_type {};
3020 
3021 template <typename BasicJsonType, typename CompatibleArrayType>
3023  BasicJsonType, CompatibleArrayType,
3024  enable_if_t<is_detected<value_type_t, CompatibleArrayType>::value and
3025  is_detected<iterator_t, CompatibleArrayType>::value and
3026 // This is needed because json_reverse_iterator has a ::iterator type...
3027 // Therefore it is detected as a CompatibleArrayType.
3028 // The real fix would be to have an Iterable concept.
3029  not is_iterator_traits<
3030  iterator_traits<CompatibleArrayType>>::value >>
3031 {
3032  static constexpr bool value =
3033  std::is_constructible<BasicJsonType,
3035 };
3036 
3037 template <typename BasicJsonType, typename CompatibleArrayType>
3039  : is_compatible_array_type_impl<BasicJsonType, CompatibleArrayType> {};
3040 
3041 template <typename BasicJsonType, typename ConstructibleArrayType, typename = void>
3042 struct is_constructible_array_type_impl : std::false_type {};
3043 
3044 template <typename BasicJsonType, typename ConstructibleArrayType>
3046  BasicJsonType, ConstructibleArrayType,
3047  enable_if_t<std::is_same<ConstructibleArrayType,
3048  typename BasicJsonType::value_type>::value >>
3049  : std::true_type {};
3050 
3051 template <typename BasicJsonType, typename ConstructibleArrayType>
3053  BasicJsonType, ConstructibleArrayType,
3054  enable_if_t<not std::is_same<ConstructibleArrayType,
3055  typename BasicJsonType::value_type>::value and
3056  std::is_default_constructible<ConstructibleArrayType>::value and
3057 (std::is_move_assignable<ConstructibleArrayType>::value or
3058  std::is_copy_assignable<ConstructibleArrayType>::value) and
3059 is_detected<value_type_t, ConstructibleArrayType>::value and
3060 is_detected<iterator_t, ConstructibleArrayType>::value and
3062 detected_t<value_type_t, ConstructibleArrayType>>::value >>
3063 {
3064  static constexpr bool value =
3065  // This is needed because json_reverse_iterator has a ::iterator type,
3066  // furthermore, std::back_insert_iterator (and other iterators) have a
3067  // base class `iterator`... Therefore it is detected as a
3068  // ConstructibleArrayType. The real fix would be to have an Iterable
3069  // concept.
3071 
3072  (std::is_same<typename ConstructibleArrayType::value_type,
3074  has_from_json<BasicJsonType,
3077  BasicJsonType, typename ConstructibleArrayType::value_type >::value);
3078 };
3079 
3080 template <typename BasicJsonType, typename ConstructibleArrayType>
3082  : is_constructible_array_type_impl<BasicJsonType, ConstructibleArrayType> {};
3083 
3084 template <typename RealIntegerType, typename CompatibleNumberIntegerType,
3085  typename = void>
3086 struct is_compatible_integer_type_impl : std::false_type {};
3087 
3088 template <typename RealIntegerType, typename CompatibleNumberIntegerType>
3090  RealIntegerType, CompatibleNumberIntegerType,
3091  enable_if_t<std::is_integral<RealIntegerType>::value and
3092  std::is_integral<CompatibleNumberIntegerType>::value and
3093  not std::is_same<bool, CompatibleNumberIntegerType>::value >>
3094 {
3095  // is there an assert somewhere on overflows?
3096  using RealLimits = std::numeric_limits<RealIntegerType>;
3097  using CompatibleLimits = std::numeric_limits<CompatibleNumberIntegerType>;
3098 
3099  static constexpr auto value =
3100  std::is_constructible<RealIntegerType,
3101  CompatibleNumberIntegerType>::value and
3102  CompatibleLimits::is_integer and
3103  RealLimits::is_signed == CompatibleLimits::is_signed;
3104 };
3105 
3106 template <typename RealIntegerType, typename CompatibleNumberIntegerType>
3108  : is_compatible_integer_type_impl<RealIntegerType,
3109  CompatibleNumberIntegerType> {};
3110 
3111 template <typename BasicJsonType, typename CompatibleType, typename = void>
3112 struct is_compatible_type_impl: std::false_type {};
3113 
3114 template <typename BasicJsonType, typename CompatibleType>
3116  BasicJsonType, CompatibleType,
3117  enable_if_t<is_complete_type<CompatibleType>::value >>
3118 {
3119  static constexpr bool value =
3121 };
3122 
3123 template <typename BasicJsonType, typename CompatibleType>
3125  : is_compatible_type_impl<BasicJsonType, CompatibleType> {};
3126 
3127 // https://en.cppreference.com/w/cpp/types/conjunction
3128 template<class...> struct conjunction : std::true_type { };
3129 template<class B1> struct conjunction<B1> : B1 { };
3130 template<class B1, class... Bn>
3131 struct conjunction<B1, Bn...>
3132 : std::conditional<bool(B1::value), conjunction<Bn...>, B1>::type {};
3133 
3134 template <typename T1, typename T2>
3135 struct is_constructible_tuple : std::false_type {};
3136 
3137 template <typename T1, typename... Args>
3138 struct is_constructible_tuple<T1, std::tuple<Args...>> : conjunction<std::is_constructible<T1, Args>...> {};
3139 } // namespace detail
3140 } // namespace nlohmann
3141 
3142 // #include <nlohmann/detail/value_t.hpp>
3143 
3144 
3145 #include <array> // array
3146 #include <ciso646> // and
3147 #include <cstddef> // size_t
3148 #include <cstdint> // uint8_t
3149 #include <string> // string
3150 
3151 namespace nlohmann
3152 {
3153 namespace detail
3154 {
3156 // JSON type enumeration //
3158 
3183 enum class value_t : std::uint8_t
3184 {
3185  null,
3186  object,
3187  array,
3188  string,
3189  boolean,
3190  number_integer,
3191  number_unsigned,
3192  number_float,
3193  discarded
3194 };
3195 
3206 inline bool operator<(const value_t lhs, const value_t rhs) noexcept
3207 {
3208  static constexpr std::array<std::uint8_t, 8> order = {{
3209  0 /* null */, 3 /* object */, 4 /* array */, 5 /* string */,
3210  1 /* boolean */, 2 /* integer */, 2 /* unsigned */, 2 /* float */
3211  }
3212  };
3213 
3214  const auto l_index = static_cast<std::size_t>(lhs);
3215  const auto r_index = static_cast<std::size_t>(rhs);
3216  return l_index < order.size() and r_index < order.size() and order[l_index] < order[r_index];
3217 }
3218 } // namespace detail
3219 } // namespace nlohmann
3220 
3221 
3222 namespace nlohmann
3223 {
3224 namespace detail
3225 {
3226 template<typename BasicJsonType>
3227 void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
3228 {
3229  if (JSON_HEDLEY_UNLIKELY(not j.is_null()))
3230  {
3231  JSON_THROW(type_error::create(302, "type must be null, but is " + std::string(j.type_name())));
3232  }
3233  n = nullptr;
3234 }
3235 
3236 // overloads for basic_json template parameters
3237 template<typename BasicJsonType, typename ArithmeticType,
3240  int> = 0>
3241 void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
3242 {
3243  switch (static_cast<value_t>(j))
3244  {
3245  case value_t::number_unsigned:
3246  {
3247  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
3248  break;
3249  }
3250  case value_t::number_integer:
3251  {
3252  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
3253  break;
3254  }
3255  case value_t::number_float:
3256  {
3257  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
3258  break;
3259  }
3260 
3261  default:
3262  JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name())));
3263  }
3264 }
3265 
3266 template<typename BasicJsonType>
3267 void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
3268 {
3269  if (JSON_HEDLEY_UNLIKELY(not j.is_boolean()))
3270  {
3271  JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(j.type_name())));
3272  }
3273  b = *j.template get_ptr<const typename BasicJsonType::boolean_t*>();
3274 }
3275 
3276 template<typename BasicJsonType>
3277 void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
3278 {
3279  if (JSON_HEDLEY_UNLIKELY(not j.is_string()))
3280  {
3281  JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name())));
3282  }
3283  s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
3284 }
3285 
3286 template <
3287  typename BasicJsonType, typename ConstructibleStringType,
3288  enable_if_t <
3290  not std::is_same<typename BasicJsonType::string_t,
3291  ConstructibleStringType>::value,
3292  int > = 0 >
3293 void from_json(const BasicJsonType& j, ConstructibleStringType& s)
3294 {
3295  if (JSON_HEDLEY_UNLIKELY(not j.is_string()))
3296  {
3297  JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name())));
3298  }
3299 
3300  s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
3301 }
3302 
3303 template<typename BasicJsonType>
3304 void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val)
3305 {
3306  get_arithmetic_value(j, val);
3307 }
3308 
3309 template<typename BasicJsonType>
3310 void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val)
3311 {
3312  get_arithmetic_value(j, val);
3313 }
3314 
3315 template<typename BasicJsonType>
3316 void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val)
3317 {
3318  get_arithmetic_value(j, val);
3319 }
3320 
3321 template<typename BasicJsonType, typename EnumType,
3323 void from_json(const BasicJsonType& j, EnumType& e)
3324 {
3325  typename std::underlying_type<EnumType>::type val;
3326  get_arithmetic_value(j, val);
3327  e = static_cast<EnumType>(val);
3328 }
3329 
3330 // forward_list doesn't have an insert method
3331 template<typename BasicJsonType, typename T, typename Allocator,
3333 void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
3334 {
3335  if (JSON_HEDLEY_UNLIKELY(not j.is_array()))
3336  {
3337  JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name())));
3338  }
3339  l.clear();
3340  std::transform(j.rbegin(), j.rend(),
3341  std::front_inserter(l), [](const BasicJsonType & i)
3342  {
3343  return i.template get<T>();
3344  });
3345 }
3346 
3347 // valarray doesn't have an insert method
3348 template<typename BasicJsonType, typename T,
3350 void from_json(const BasicJsonType& j, std::valarray<T>& l)
3351 {
3352  if (JSON_HEDLEY_UNLIKELY(not j.is_array()))
3353  {
3354  JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name())));
3355  }
3356  l.resize(j.size());
3357  std::copy(j.begin(), j.end(), std::begin(l));
3358 }
3359 
3360 template <typename BasicJsonType, typename T, std::size_t N>
3361 auto from_json(const BasicJsonType& j, T (&arr)[N])
3362 -> decltype(j.template get<T>(), void())
3363 {
3364  for (std::size_t i = 0; i < N; ++i)
3365  {
3366  arr[i] = j.at(i).template get<T>();
3367  }
3368 }
3369 
3370 template<typename BasicJsonType>
3371 void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType::array_t& arr, priority_tag<3> /*unused*/)
3372 {
3373  arr = *j.template get_ptr<const typename BasicJsonType::array_t*>();
3374 }
3375 
3376 template <typename BasicJsonType, typename T, std::size_t N>
3377 auto from_json_array_impl(const BasicJsonType& j, std::array<T, N>& arr,
3378  priority_tag<2> /*unused*/)
3379 -> decltype(j.template get<T>(), void())
3380 {
3381  for (std::size_t i = 0; i < N; ++i)
3382  {
3383  arr[i] = j.at(i).template get<T>();
3384  }
3385 }
3386 
3387 template<typename BasicJsonType, typename ConstructibleArrayType>
3388 auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<1> /*unused*/)
3389 -> decltype(
3390  arr.reserve(std::declval<typename ConstructibleArrayType::size_type>()),
3391  j.template get<typename ConstructibleArrayType::value_type>(),
3392  void())
3393 {
3394  using std::end;
3395 
3396  ConstructibleArrayType ret;
3397  ret.reserve(j.size());
3398  std::transform(j.begin(), j.end(),
3399  std::inserter(ret, end(ret)), [](const BasicJsonType & i)
3400  {
3401  // get<BasicJsonType>() returns *this, this won't call a from_json
3402  // method when value_type is BasicJsonType
3403  return i.template get<typename ConstructibleArrayType::value_type>();
3404  });
3405  arr = std::move(ret);
3406 }
3407 
3408 template <typename BasicJsonType, typename ConstructibleArrayType>
3409 void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr,
3410  priority_tag<0> /*unused*/)
3411 {
3412  using std::end;
3413 
3414  ConstructibleArrayType ret;
3416  j.begin(), j.end(), std::inserter(ret, end(ret)),
3417  [](const BasicJsonType & i)
3418  {
3419  // get<BasicJsonType>() returns *this, this won't call a from_json
3420  // method when value_type is BasicJsonType
3421  return i.template get<typename ConstructibleArrayType::value_type>();
3422  });
3423  arr = std::move(ret);
3424 }
3425 
3426 template <typename BasicJsonType, typename ConstructibleArrayType,
3427  enable_if_t <
3432  int > = 0 >
3433 
3434 auto from_json(const BasicJsonType& j, ConstructibleArrayType& arr)
3435 -> decltype(from_json_array_impl(j, arr, priority_tag<3> {}),
3436 j.template get<typename ConstructibleArrayType::value_type>(),
3437 void())
3438 {
3439  if (JSON_HEDLEY_UNLIKELY(not j.is_array()))
3440  {
3441  JSON_THROW(type_error::create(302, "type must be array, but is " +
3442  std::string(j.type_name())));
3443  }
3444 
3446 }
3447 
3448 template<typename BasicJsonType, typename ConstructibleObjectType,
3450 void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
3451 {
3452  if (JSON_HEDLEY_UNLIKELY(not j.is_object()))
3453  {
3454  JSON_THROW(type_error::create(302, "type must be object, but is " + std::string(j.type_name())));
3455  }
3456 
3457  ConstructibleObjectType ret;
3458  auto inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
3461  inner_object->begin(), inner_object->end(),
3462  std::inserter(ret, ret.begin()),
3463  [](typename BasicJsonType::object_t::value_type const & p)
3464  {
3465  return value_type(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
3466  });
3467  obj = std::move(ret);
3468 }
3469 
3470 // overload for arithmetic types, not chosen for basic_json template arguments
3471 // (BooleanType, etc..); note: Is it really necessary to provide explicit
3472 // overloads for boolean_t etc. in case of a custom BooleanType which is not
3473 // an arithmetic type?
3474 template<typename BasicJsonType, typename ArithmeticType,
3475  enable_if_t <
3481  int> = 0>
3482 void from_json(const BasicJsonType& j, ArithmeticType& val)
3483 {
3484  switch (static_cast<value_t>(j))
3485  {
3486  case value_t::number_unsigned:
3487  {
3488  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
3489  break;
3490  }
3491  case value_t::number_integer:
3492  {
3493  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
3494  break;
3495  }
3496  case value_t::number_float:
3497  {
3498  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
3499  break;
3500  }
3501  case value_t::boolean:
3502  {
3503  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
3504  break;
3505  }
3506 
3507  default:
3508  JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name())));
3509  }
3510 }
3511 
3512 template<typename BasicJsonType, typename A1, typename A2>
3513 void from_json(const BasicJsonType& j, std::pair<A1, A2>& p)
3514 {
3515  p = {j.at(0).template get<A1>(), j.at(1).template get<A2>()};
3516 }
3517 
3518 template<typename BasicJsonType, typename Tuple, std::size_t... Idx>
3519 void from_json_tuple_impl(const BasicJsonType& j, Tuple& t, index_sequence<Idx...> /*unused*/)
3520 {
3521  t = std::make_tuple(j.at(Idx).template get<typename std::tuple_element<Idx, Tuple>::type>()...);
3522 }
3523 
3524 template<typename BasicJsonType, typename... Args>
3525 void from_json(const BasicJsonType& j, std::tuple<Args...>& t)
3526 {
3528 }
3529 
3530 template <typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator,
3531  typename = enable_if_t<not std::is_constructible<
3532  typename BasicJsonType::string_t, Key>::value>>
3533 void from_json(const BasicJsonType& j, std::map<Key, Value, Compare, Allocator>& m)
3534 {
3535  if (JSON_HEDLEY_UNLIKELY(not j.is_array()))
3536  {
3537  JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name())));
3538  }
3539  m.clear();
3540  for (const auto& p : j)
3541  {
3542  if (JSON_HEDLEY_UNLIKELY(not p.is_array()))
3543  {
3544  JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name())));
3545  }
3546  m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
3547  }
3548 }
3549 
3550 template <typename BasicJsonType, typename Key, typename Value, typename Hash, typename KeyEqual, typename Allocator,
3551  typename = enable_if_t<not std::is_constructible<
3552  typename BasicJsonType::string_t, Key>::value>>
3553 void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>& m)
3554 {
3555  if (JSON_HEDLEY_UNLIKELY(not j.is_array()))
3556  {
3557  JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name())));
3558  }
3559  m.clear();
3560  for (const auto& p : j)
3561  {
3562  if (JSON_HEDLEY_UNLIKELY(not p.is_array()))
3563  {
3564  JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name())));
3565  }
3566  m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
3567  }
3568 }
3569 
3571 {
3572  template<typename BasicJsonType, typename T>
3573  auto operator()(const BasicJsonType& j, T& val) const
3574  noexcept(noexcept(from_json(j, val)))
3575  -> decltype(from_json(j, val), void())
3576  {
3577  return from_json(j, val);
3578  }
3579 };
3580 } // namespace detail
3581 
3585 namespace
3586 {
3588 } // namespace
3589 } // namespace nlohmann
3590 
3591 // #include <nlohmann/detail/conversions/to_json.hpp>
3592 
3593 
3594 #include <algorithm> // copy
3595 #include <ciso646> // or, and, not
3596 #include <iterator> // begin, end
3597 #include <string> // string
3598 #include <tuple> // tuple, get
3599 #include <type_traits> // is_same, is_constructible, is_floating_point, is_enum, underlying_type
3600 #include <utility> // move, forward, declval, pair
3601 #include <valarray> // valarray
3602 #include <vector> // vector
3603 
3604 // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
3605 
3606 
3607 #include <cstddef> // size_t
3608 #include <iterator> // input_iterator_tag
3609 #include <string> // string, to_string
3610 #include <tuple> // tuple_size, get, tuple_element
3611 
3612 // #include <nlohmann/detail/meta/type_traits.hpp>
3613 
3614 // #include <nlohmann/detail/value_t.hpp>
3615 
3616 
3617 namespace nlohmann
3618 {
3619 namespace detail
3620 {
3621 template<typename string_type>
3622 void int_to_string( string_type& target, std::size_t value )
3623 {
3624  target = std::to_string(value);
3625 }
3626 template <typename IteratorType> class iteration_proxy_value
3627 {
3628  public:
3629  using difference_type = std::ptrdiff_t;
3631  using pointer = value_type * ;
3632  using reference = value_type & ;
3633  using iterator_category = std::input_iterator_tag;
3634  using string_type = typename std::remove_cv< typename std::remove_reference<decltype( std::declval<IteratorType>().key() ) >::type >::type;
3635 
3636  private:
3638  IteratorType anchor;
3640  std::size_t array_index = 0;
3642  mutable std::size_t array_index_last = 0;
3644  mutable string_type array_index_str = "0";
3646  const string_type empty_str = "";
3647 
3648  public:
3649  explicit iteration_proxy_value(IteratorType it) noexcept : anchor(it) {}
3650 
3653  {
3654  return *this;
3655  }
3656 
3659  {
3660  ++anchor;
3661  ++array_index;
3662 
3663  return *this;
3664  }
3665 
3667  bool operator==(const iteration_proxy_value& o) const
3668  {
3669  return anchor == o.anchor;
3670  }
3671 
3673  bool operator!=(const iteration_proxy_value& o) const
3674  {
3675  return anchor != o.anchor;
3676  }
3677 
3679  const string_type& key() const
3680  {
3681  assert(anchor.m_object != nullptr);
3682 
3683  switch (anchor.m_object->type())
3684  {
3685  // use integer array index as key
3686  case value_t::array:
3687  {
3688  if (array_index != array_index_last)
3689  {
3690  int_to_string( array_index_str, array_index );
3691  array_index_last = array_index;
3692  }
3693  return array_index_str;
3694  }
3695 
3696  // use key from the object
3697  case value_t::object:
3698  return anchor.key();
3699 
3700  // use an empty key for all primitive types
3701  default:
3702  return empty_str;
3703  }
3704  }
3705 
3707  typename IteratorType::reference value() const
3708  {
3709  return anchor.value();
3710  }
3711 };
3712 
3714 template<typename IteratorType> class iteration_proxy
3715 {
3716  private:
3718  typename IteratorType::reference container;
3719 
3720  public:
3722  explicit iteration_proxy(typename IteratorType::reference cont) noexcept
3723  : container(cont) {}
3724 
3727  {
3728  return iteration_proxy_value<IteratorType>(container.begin());
3729  }
3730 
3733  {
3734  return iteration_proxy_value<IteratorType>(container.end());
3735  }
3736 };
3737 // Structured Bindings Support
3738 // For further reference see https://blog.tartanllama.xyz/structured-bindings/
3739 // And see https://github.com/nlohmann/json/pull/1391
3740 template <std::size_t N, typename IteratorType, enable_if_t<N == 0, int> = 0>
3741 auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.key())
3742 {
3743  return i.key();
3744 }
3745 // Structured Bindings Support
3746 // For further reference see https://blog.tartanllama.xyz/structured-bindings/
3747 // And see https://github.com/nlohmann/json/pull/1391
3748 template <std::size_t N, typename IteratorType, enable_if_t<N == 1, int> = 0>
3749 auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.value())
3750 {
3751  return i.value();
3752 }
3753 } // namespace detail
3754 } // namespace nlohmann
3755 
3756 // The Addition to the STD Namespace is required to add
3757 // Structured Bindings Support to the iteration_proxy_value class
3758 // For further reference see https://blog.tartanllama.xyz/structured-bindings/
3759 // And see https://github.com/nlohmann/json/pull/1391
3760 namespace std
3761 {
3762 #if defined(__clang__)
3763  // Fix: https://github.com/nlohmann/json/issues/1401
3764  #pragma clang diagnostic push
3765  #pragma clang diagnostic ignored "-Wmismatched-tags"
3766 #endif
3767 template <typename IteratorType>
3768 class tuple_size<::nlohmann::detail::iteration_proxy_value<IteratorType>>
3769  : public std::integral_constant<std::size_t, 2> {};
3770 
3771 template <std::size_t N, typename IteratorType>
3772 class tuple_element<N, ::nlohmann::detail::iteration_proxy_value<IteratorType >>
3773 {
3774  public:
3775  using type = decltype(
3776  get<N>(std::declval <
3778 };
3779 #if defined(__clang__)
3780  #pragma clang diagnostic pop
3781 #endif
3782 } // namespace std
3783 
3784 // #include <nlohmann/detail/meta/cpp_future.hpp>
3785 
3786 // #include <nlohmann/detail/meta/type_traits.hpp>
3787 
3788 // #include <nlohmann/detail/value_t.hpp>
3789 
3790 
3791 namespace nlohmann
3792 {
3793 namespace detail
3794 {
3796 // constructors //
3798 
3799 template<value_t> struct external_constructor;
3800 
3801 template<>
3803 {
3804  template<typename BasicJsonType>
3805  static void construct(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept
3806  {
3807  j.m_type = value_t::boolean;
3808  j.m_value = b;
3809  j.assert_invariant();
3810  }
3811 };
3812 
3813 template<>
3815 {
3816  template<typename BasicJsonType>
3817  static void construct(BasicJsonType& j, const typename BasicJsonType::string_t& s)
3818  {
3819  j.m_type = value_t::string;
3820  j.m_value = s;
3821  j.assert_invariant();
3822  }
3823 
3824  template<typename BasicJsonType>
3825  static void construct(BasicJsonType& j, typename BasicJsonType::string_t&& s)
3826  {
3827  j.m_type = value_t::string;
3828  j.m_value = std::move(s);
3829  j.assert_invariant();
3830  }
3831 
3832  template<typename BasicJsonType, typename CompatibleStringType,
3834  int> = 0>
3835  static void construct(BasicJsonType& j, const CompatibleStringType& str)
3836  {
3837  j.m_type = value_t::string;
3838  j.m_value.string = j.template create<typename BasicJsonType::string_t>(str);
3839  j.assert_invariant();
3840  }
3841 };
3842 
3843 template<>
3844 struct external_constructor<value_t::number_float>
3845 {
3846  template<typename BasicJsonType>
3847  static void construct(BasicJsonType& j, typename BasicJsonType::number_float_t val) noexcept
3848  {
3849  j.m_type = value_t::number_float;
3850  j.m_value = val;
3851  j.assert_invariant();
3852  }
3853 };
3854 
3855 template<>
3856 struct external_constructor<value_t::number_unsigned>
3857 {
3858  template<typename BasicJsonType>
3859  static void construct(BasicJsonType& j, typename BasicJsonType::number_unsigned_t val) noexcept
3860  {
3861  j.m_type = value_t::number_unsigned;
3862  j.m_value = val;
3863  j.assert_invariant();
3864  }
3865 };
3866 
3867 template<>
3868 struct external_constructor<value_t::number_integer>
3869 {
3870  template<typename BasicJsonType>
3871  static void construct(BasicJsonType& j, typename BasicJsonType::number_integer_t val) noexcept
3872  {
3873  j.m_type = value_t::number_integer;
3874  j.m_value = val;
3875  j.assert_invariant();
3876  }
3877 };
3878 
3879 template<>
3881 {
3882  template<typename BasicJsonType>
3883  static void construct(BasicJsonType& j, const typename BasicJsonType::array_t& arr)
3884  {
3885  j.m_type = value_t::array;
3886  j.m_value = arr;
3887  j.assert_invariant();
3888  }
3889 
3890  template<typename BasicJsonType>
3891  static void construct(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
3892  {
3893  j.m_type = value_t::array;
3894  j.m_value = std::move(arr);
3895  j.assert_invariant();
3896  }
3897 
3898  template<typename BasicJsonType, typename CompatibleArrayType,
3900  int> = 0>
3901  static void construct(BasicJsonType& j, const CompatibleArrayType& arr)
3902  {
3903  using std::begin;
3904  using std::end;
3905  j.m_type = value_t::array;
3906  j.m_value.array = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
3907  j.assert_invariant();
3908  }
3909 
3910  template<typename BasicJsonType>
3911  static void construct(BasicJsonType& j, const std::vector<bool>& arr)
3912  {
3913  j.m_type = value_t::array;
3914  j.m_value = value_t::array;
3915  j.m_value.array->reserve(arr.size());
3916  for (const bool x : arr)
3917  {
3918  j.m_value.array->push_back(x);
3919  }
3920  j.assert_invariant();
3921  }
3922 
3923  template<typename BasicJsonType, typename T,
3925  static void construct(BasicJsonType& j, const std::valarray<T>& arr)
3926  {
3927  j.m_type = value_t::array;
3928  j.m_value = value_t::array;
3929  j.m_value.array->resize(arr.size());
3930  if (arr.size() > 0)
3931  {
3932  std::copy(std::begin(arr), std::end(arr), j.m_value.array->begin());
3933  }
3934  j.assert_invariant();
3935  }
3936 };
3937 
3938 template<>
3940 {
3941  template<typename BasicJsonType>
3942  static void construct(BasicJsonType& j, const typename BasicJsonType::object_t& obj)
3943  {
3944  j.m_type = value_t::object;
3945  j.m_value = obj;
3946  j.assert_invariant();
3947  }
3948 
3949  template<typename BasicJsonType>
3950  static void construct(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
3951  {
3952  j.m_type = value_t::object;
3953  j.m_value = std::move(obj);
3954  j.assert_invariant();
3955  }
3956 
3957  template<typename BasicJsonType, typename CompatibleObjectType,
3959  static void construct(BasicJsonType& j, const CompatibleObjectType& obj)
3960  {
3961  using std::begin;
3962  using std::end;
3963 
3964  j.m_type = value_t::object;
3965  j.m_value.object = j.template create<typename BasicJsonType::object_t>(begin(obj), end(obj));
3966  j.assert_invariant();
3967  }
3968 };
3969 
3971 // to_json //
3973 
3974 template<typename BasicJsonType, typename T,
3976 void to_json(BasicJsonType& j, T b) noexcept
3977 {
3979 }
3980 
3981 template<typename BasicJsonType, typename CompatibleString,
3983 void to_json(BasicJsonType& j, const CompatibleString& s)
3984 {
3986 }
3987 
3988 template<typename BasicJsonType>
3989 void to_json(BasicJsonType& j, typename BasicJsonType::string_t&& s)
3990 {
3992 }
3993 
3994 template<typename BasicJsonType, typename FloatType,
3996 void to_json(BasicJsonType& j, FloatType val) noexcept
3997 {
3998  external_constructor<value_t::number_float>::construct(j, static_cast<typename BasicJsonType::number_float_t>(val));
3999 }
4000 
4001 template<typename BasicJsonType, typename CompatibleNumberUnsignedType,
4003 void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept
4004 {
4005  external_constructor<value_t::number_unsigned>::construct(j, static_cast<typename BasicJsonType::number_unsigned_t>(val));
4006 }
4007 
4008 template<typename BasicJsonType, typename CompatibleNumberIntegerType,
4010 void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept
4011 {
4012  external_constructor<value_t::number_integer>::construct(j, static_cast<typename BasicJsonType::number_integer_t>(val));
4013 }
4014 
4015 template<typename BasicJsonType, typename EnumType,
4017 void to_json(BasicJsonType& j, EnumType e) noexcept
4018 {
4019  using underlying_type = typename std::underlying_type<EnumType>::type;
4020  external_constructor<value_t::number_integer>::construct(j, static_cast<underlying_type>(e));
4021 }
4022 
4023 template<typename BasicJsonType>
4024 void to_json(BasicJsonType& j, const std::vector<bool>& e)
4025 {
4027 }
4028 
4029 template <typename BasicJsonType, typename CompatibleArrayType,
4030  enable_if_t<is_compatible_array_type<BasicJsonType,
4031  CompatibleArrayType>::value and
4032  not is_compatible_object_type<
4033  BasicJsonType, CompatibleArrayType>::value and
4036  int> = 0>
4037 void to_json(BasicJsonType& j, const CompatibleArrayType& arr)
4038 {
4040 }
4041 
4042 template<typename BasicJsonType, typename T,
4044 void to_json(BasicJsonType& j, const std::valarray<T>& arr)
4045 {
4047 }
4048 
4049 template<typename BasicJsonType>
4050 void to_json(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
4051 {
4053 }
4054 
4055 template<typename BasicJsonType, typename CompatibleObjectType,
4057 void to_json(BasicJsonType& j, const CompatibleObjectType& obj)
4058 {
4060 }
4061 
4062 template<typename BasicJsonType>
4063 void to_json(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
4064 {
4066 }
4067 
4068 template <
4069  typename BasicJsonType, typename T, std::size_t N,
4070  enable_if_t<not std::is_constructible<typename BasicJsonType::string_t,
4071  const T(&)[N]>::value,
4072  int> = 0 >
4073 void to_json(BasicJsonType& j, const T(&arr)[N])
4074 {
4076 }
4077 
4079 void to_json(BasicJsonType& j, const std::pair<T1, T2>& p)
4080 {
4081  j = { p.first, p.second };
4082 }
4083 
4084 // for https://github.com/nlohmann/json/pull/1134
4085 template < typename BasicJsonType, typename T,
4086  enable_if_t<std::is_same<T, iteration_proxy_value<typename BasicJsonType::iterator>>::value, int> = 0>
4087 void to_json(BasicJsonType& j, const T& b)
4088 {
4089  j = { {b.key(), b.value()} };
4090 }
4091 
4092 template<typename BasicJsonType, typename Tuple, std::size_t... Idx>
4093 void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence<Idx...> /*unused*/)
4094 {
4095  j = { std::get<Idx>(t)... };
4096 }
4097 
4099 void to_json(BasicJsonType& j, const T& t)
4100 {
4102 }
4103 
4105 {
4106  template<typename BasicJsonType, typename T>
4107  auto operator()(BasicJsonType& j, T&& val) const noexcept(noexcept(to_json(j, std::forward<T>(val))))
4108  -> decltype(to_json(j, std::forward<T>(val)), void())
4109  {
4110  return to_json(j, std::forward<T>(val));
4111  }
4112 };
4113 } // namespace detail
4114 
4116 namespace
4117 {
4119 } // namespace
4120 } // namespace nlohmann
4121 
4122 
4123 namespace nlohmann
4124 {
4125 
4126 template<typename, typename>
4128 {
4138  template<typename BasicJsonType, typename ValueType>
4139  static auto from_json(BasicJsonType&& j, ValueType& val) noexcept(
4140  noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), val)))
4141  -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), val), void())
4142  {
4143  ::nlohmann::from_json(std::forward<BasicJsonType>(j), val);
4144  }
4145 
4155  template <typename BasicJsonType, typename ValueType>
4156  static auto to_json(BasicJsonType& j, ValueType&& val) noexcept(
4157  noexcept(::nlohmann::to_json(j, std::forward<ValueType>(val))))
4158  -> decltype(::nlohmann::to_json(j, std::forward<ValueType>(val)), void())
4159  {
4160  ::nlohmann::to_json(j, std::forward<ValueType>(val));
4161  }
4162 };
4163 
4164 } // namespace nlohmann
4165 
4166 // #include <nlohmann/detail/conversions/from_json.hpp>
4167 
4168 // #include <nlohmann/detail/conversions/to_json.hpp>
4169 
4170 // #include <nlohmann/detail/exceptions.hpp>
4171 
4172 // #include <nlohmann/detail/input/binary_reader.hpp>
4173 
4174 
4175 #include <algorithm> // generate_n
4176 #include <array> // array
4177 #include <cassert> // assert
4178 #include <cmath> // ldexp
4179 #include <cstddef> // size_t
4180 #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
4181 #include <cstdio> // snprintf
4182 #include <cstring> // memcpy
4183 #include <iterator> // back_inserter
4184 #include <limits> // numeric_limits
4185 #include <string> // char_traits, string
4186 #include <utility> // make_pair, move
4187 
4188 // #include <nlohmann/detail/exceptions.hpp>
4189 
4190 // #include <nlohmann/detail/input/input_adapters.hpp>
4191 
4192 
4193 #include <array> // array
4194 #include <cassert> // assert
4195 #include <cstddef> // size_t
4196 #include <cstdio> //FILE *
4197 #include <cstring> // strlen
4198 #include <istream> // istream
4199 #include <iterator> // begin, end, iterator_traits, random_access_iterator_tag, distance, next
4200 #include <memory> // shared_ptr, make_shared, addressof
4201 #include <numeric> // accumulate
4202 #include <string> // string, char_traits
4203 #include <type_traits> // enable_if, is_base_of, is_pointer, is_integral, remove_pointer
4204 #include <utility> // pair, declval
4205 
4206 // #include <nlohmann/detail/iterators/iterator_traits.hpp>
4207 
4208 // #include <nlohmann/detail/macro_scope.hpp>
4209 
4210 
4211 namespace nlohmann
4212 {
4213 namespace detail
4214 {
4216 enum class input_format_t { json, cbor, msgpack, ubjson, bson };
4217 
4219 // input adapters //
4221 
4234 {
4236  virtual std::char_traits<char>::int_type get_character() = 0;
4237  virtual ~input_adapter_protocol() = default;
4238 };
4239 
4241 using input_adapter_t = std::shared_ptr<input_adapter_protocol>;
4242 
4248 {
4249  public:
4251  explicit file_input_adapter(std::FILE* f) noexcept
4252  : m_file(f)
4253  {}
4254 
4255  // make class move-only
4256  file_input_adapter(const file_input_adapter&) = delete;
4258  file_input_adapter& operator=(const file_input_adapter&) = delete;
4259  file_input_adapter& operator=(file_input_adapter&&) = default;
4260  ~file_input_adapter() override = default;
4261 
4262  std::char_traits<char>::int_type get_character() noexcept override
4263  {
4264  return std::fgetc(m_file);
4265  }
4266 
4267  private:
4269  std::FILE* m_file;
4270 };
4271 
4272 
4283 {
4284  public:
4286  {
4287  // clear stream flags; we use underlying streambuf I/O, do not
4288  // maintain ifstream flags, except eof
4289  is.clear(is.rdstate() & std::ios::eofbit);
4290  }
4291 
4292  explicit input_stream_adapter(std::istream& i)
4293  : is(i), sb(*i.rdbuf())
4294  {}
4295 
4296  // delete because of pointer members
4297  input_stream_adapter(const input_stream_adapter&) = delete;
4298  input_stream_adapter& operator=(input_stream_adapter&) = delete;
4300  input_stream_adapter& operator=(input_stream_adapter&&) = delete;
4301 
4302  // std::istream/std::streambuf use std::char_traits<char>::to_int_type, to
4303  // ensure that std::char_traits<char>::eof() and the character 0xFF do not
4304  // end up as the same value, eg. 0xFFFFFFFF.
4305  std::char_traits<char>::int_type get_character() override
4306  {
4307  auto res = sb.sbumpc();
4308  // set eof manually, as we don't use the istream interface.
4309  if (res == EOF)
4310  {
4311  is.clear(is.rdstate() | std::ios::eofbit);
4312  }
4313  return res;
4314  }
4315 
4316  private:
4318  std::istream& is;
4319  std::streambuf& sb;
4320 };
4321 
4324 {
4325  public:
4326  input_buffer_adapter(const char* b, const std::size_t l) noexcept
4327  : cursor(b), limit(b == nullptr ? nullptr : (b + l))
4328  {}
4329 
4330  // delete because of pointer members
4331  input_buffer_adapter(const input_buffer_adapter&) = delete;
4332  input_buffer_adapter& operator=(input_buffer_adapter&) = delete;
4334  input_buffer_adapter& operator=(input_buffer_adapter&&) = delete;
4335  ~input_buffer_adapter() override = default;
4336 
4337  std::char_traits<char>::int_type get_character() noexcept override
4338  {
4339  if (JSON_HEDLEY_LIKELY(cursor < limit))
4340  {
4341  assert(cursor != nullptr and limit != nullptr);
4342  return std::char_traits<char>::to_int_type(*(cursor++));
4343  }
4344 
4345  return std::char_traits<char>::eof();
4346  }
4347 
4348  private:
4350  const char* cursor;
4352  const char* const limit;
4353 };
4354 
4355 template<typename WideStringType, size_t T>
4357 {
4358  // UTF-32
4359  static void fill_buffer(const WideStringType& str,
4360  size_t& current_wchar,
4361  std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
4362  size_t& utf8_bytes_index,
4363  size_t& utf8_bytes_filled)
4364  {
4365  utf8_bytes_index = 0;
4366 
4367  if (current_wchar == str.size())
4368  {
4369  utf8_bytes[0] = std::char_traits<char>::eof();
4370  utf8_bytes_filled = 1;
4371  }
4372  else
4373  {
4374  // get the current character
4375  const auto wc = static_cast<unsigned int>(str[current_wchar++]);
4376 
4377  // UTF-32 to UTF-8 encoding
4378  if (wc < 0x80)
4379  {
4380  utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
4381  utf8_bytes_filled = 1;
4382  }
4383  else if (wc <= 0x7FF)
4384  {
4385  utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((wc >> 6u) & 0x1Fu));
4386  utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (wc & 0x3Fu));
4387  utf8_bytes_filled = 2;
4388  }
4389  else if (wc <= 0xFFFF)
4390  {
4391  utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((wc >> 12u) & 0x0Fu));
4392  utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((wc >> 6u) & 0x3Fu));
4393  utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (wc & 0x3Fu));
4394  utf8_bytes_filled = 3;
4395  }
4396  else if (wc <= 0x10FFFF)
4397  {
4398  utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | ((wc >> 18u) & 0x07u));
4399  utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((wc >> 12u) & 0x3Fu));
4400  utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((wc >> 6u) & 0x3Fu));
4401  utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (wc & 0x3Fu));
4402  utf8_bytes_filled = 4;
4403  }
4404  else
4405  {
4406  // unknown character
4407  utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
4408  utf8_bytes_filled = 1;
4409  }
4410  }
4411  }
4412 };
4413 
4414 template<typename WideStringType>
4415 struct wide_string_input_helper<WideStringType, 2>
4416 {
4417  // UTF-16
4418  static void fill_buffer(const WideStringType& str,
4419  size_t& current_wchar,
4420  std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
4421  size_t& utf8_bytes_index,
4422  size_t& utf8_bytes_filled)
4423  {
4424  utf8_bytes_index = 0;
4425 
4426  if (current_wchar == str.size())
4427  {
4428  utf8_bytes[0] = std::char_traits<char>::eof();
4429  utf8_bytes_filled = 1;
4430  }
4431  else
4432  {
4433  // get the current character
4434  const auto wc = static_cast<unsigned int>(str[current_wchar++]);
4435 
4436  // UTF-16 to UTF-8 encoding
4437  if (wc < 0x80)
4438  {
4439  utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
4440  utf8_bytes_filled = 1;
4441  }
4442  else if (wc <= 0x7FF)
4443  {
4444  utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((wc >> 6u)));
4445  utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (wc & 0x3Fu));
4446  utf8_bytes_filled = 2;
4447  }
4448  else if (0xD800 > wc or wc >= 0xE000)
4449  {
4450  utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((wc >> 12u)));
4451  utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((wc >> 6u) & 0x3Fu));
4452  utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (wc & 0x3Fu));
4453  utf8_bytes_filled = 3;
4454  }
4455  else
4456  {
4457  if (current_wchar < str.size())
4458  {
4459  const auto wc2 = static_cast<unsigned int>(str[current_wchar++]);
4460  const auto charcode = 0x10000u + (((wc & 0x3FFu) << 10u) | (wc2 & 0x3FFu));
4461  utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | (charcode >> 18u));
4462  utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 12u) & 0x3Fu));
4463  utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 6u) & 0x3Fu));
4464  utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (charcode & 0x3Fu));
4465  utf8_bytes_filled = 4;
4466  }
4467  else
4468  {
4469  // unknown character
4470  ++current_wchar;
4471  utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
4472  utf8_bytes_filled = 1;
4473  }
4474  }
4475  }
4476  }
4477 };
4478 
4479 template<typename WideStringType>
4481 {
4482  public:
4483  explicit wide_string_input_adapter(const WideStringType& w) noexcept
4484  : str(w)
4485  {}
4486 
4487  std::char_traits<char>::int_type get_character() noexcept override
4488  {
4489  // check if buffer needs to be filled
4490  if (utf8_bytes_index == utf8_bytes_filled)
4491  {
4492  fill_buffer<sizeof(typename WideStringType::value_type)>();
4493 
4494  assert(utf8_bytes_filled > 0);
4495  assert(utf8_bytes_index == 0);
4496  }
4497 
4498  // use buffer
4499  assert(utf8_bytes_filled > 0);
4500  assert(utf8_bytes_index < utf8_bytes_filled);
4501  return utf8_bytes[utf8_bytes_index++];
4502  }
4503 
4504  private:
4505  template<size_t T>
4506  void fill_buffer()
4507  {
4508  wide_string_input_helper<WideStringType, T>::fill_buffer(str, current_wchar, utf8_bytes, utf8_bytes_index, utf8_bytes_filled);
4509  }
4510 
4512  const WideStringType& str;
4513 
4515  std::size_t current_wchar = 0;
4516 
4518  std::array<std::char_traits<char>::int_type, 4> utf8_bytes = {{0, 0, 0, 0}};
4519 
4521  std::size_t utf8_bytes_index = 0;
4523  std::size_t utf8_bytes_filled = 0;
4524 };
4525 
4527 {
4528  public:
4529  // native support
4531  input_adapter(std::FILE* file)
4532  : ia(std::make_shared<file_input_adapter>(file)) {}
4534  input_adapter(std::istream& i)
4535  : ia(std::make_shared<input_stream_adapter>(i)) {}
4536 
4538  input_adapter(std::istream&& i)
4539  : ia(std::make_shared<input_stream_adapter>(i)) {}
4540 
4541  input_adapter(const std::wstring& ws)
4542  : ia(std::make_shared<wide_string_input_adapter<std::wstring>>(ws)) {}
4543 
4544  input_adapter(const std::u16string& ws)
4545  : ia(std::make_shared<wide_string_input_adapter<std::u16string>>(ws)) {}
4546 
4547  input_adapter(const std::u32string& ws)
4548  : ia(std::make_shared<wide_string_input_adapter<std::u32string>>(ws)) {}
4549 
4551  template<typename CharT,
4552  typename std::enable_if<
4554  std::is_integral<typename std::remove_pointer<CharT>::type>::value and
4555  sizeof(typename std::remove_pointer<CharT>::type) == 1,
4556  int>::type = 0>
4557  input_adapter(CharT b, std::size_t l)
4558  : ia(std::make_shared<input_buffer_adapter>(reinterpret_cast<const char*>(b), l)) {}
4559 
4560  // derived support
4561 
4563  template<typename CharT,
4564  typename std::enable_if<
4566  std::is_integral<typename std::remove_pointer<CharT>::type>::value and
4567  sizeof(typename std::remove_pointer<CharT>::type) == 1,
4568  int>::type = 0>
4569  input_adapter(CharT b)
4570  : input_adapter(reinterpret_cast<const char*>(b),
4571  std::strlen(reinterpret_cast<const char*>(b))) {}
4572 
4574  template<class IteratorType,
4575  typename std::enable_if<
4576  std::is_same<typename iterator_traits<IteratorType>::iterator_category, std::random_access_iterator_tag>::value,
4577  int>::type = 0>
4578  input_adapter(IteratorType first, IteratorType last)
4579  {
4580 #ifndef NDEBUG
4581  // assertion to check that the iterator range is indeed contiguous,
4582  // see https://stackoverflow.com/a/35008842/266378 for more discussion
4583  const auto is_contiguous = std::accumulate(
4584  first, last, std::pair<bool, int>(true, 0),
4585  [&first](std::pair<bool, int> res, decltype(*first) val)
4586  {
4587  res.first &= (val == *(std::next(std::addressof(*first), res.second++)));
4588  return res;
4589  }).first;
4590  assert(is_contiguous);
4591 #endif
4592 
4593  // assertion to check that each element is 1 byte long
4594  static_assert(
4595  sizeof(typename iterator_traits<IteratorType>::value_type) == 1,
4596  "each element in the iterator range must have the size of 1 byte");
4597 
4598  const auto len = static_cast<size_t>(std::distance(first, last));
4599  if (JSON_HEDLEY_LIKELY(len > 0))
4600  {
4601  // there is at least one element: use the address of first
4602  ia = std::make_shared<input_buffer_adapter>(reinterpret_cast<const char*>(&(*first)), len);
4603  }
4604  else
4605  {
4606  // the address of first cannot be used: use nullptr
4607  ia = std::make_shared<input_buffer_adapter>(nullptr, len);
4608  }
4609  }
4610 
4612  template<class T, std::size_t N>
4613  input_adapter(T (&array)[N])
4614  : input_adapter(std::begin(array), std::end(array)) {}
4615 
4617  template<class ContiguousContainer, typename
4619  std::is_base_of<std::random_access_iterator_tag, typename iterator_traits<decltype(std::begin(std::declval<ContiguousContainer const>()))>::iterator_category>::value,
4620  int>::type = 0>
4621  input_adapter(const ContiguousContainer& c)
4622  : input_adapter(std::begin(c), std::end(c)) {}
4623 
4624  operator input_adapter_t()
4625  {
4626  return ia;
4627  }
4628 
4629  private:
4631  input_adapter_t ia = nullptr;
4632 };
4633 } // namespace detail
4634 } // namespace nlohmann
4635 
4636 // #include <nlohmann/detail/input/json_sax.hpp>
4637 
4638 
4639 #include <cassert> // assert
4640 #include <cstddef>
4641 #include <string> // string
4642 #include <utility> // move
4643 #include <vector> // vector
4644 
4645 // #include <nlohmann/detail/exceptions.hpp>
4646 
4647 // #include <nlohmann/detail/macro_scope.hpp>
4648 
4649 
4650 namespace nlohmann
4651 {
4652 
4661 template<typename BasicJsonType>
4662 struct json_sax
4663 {
4665  using number_integer_t = typename BasicJsonType::number_integer_t;
4667  using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
4669  using number_float_t = typename BasicJsonType::number_float_t;
4671  using string_t = typename BasicJsonType::string_t;
4672 
4677  virtual bool null() = 0;
4678 
4684  virtual bool boolean(bool val) = 0;
4685 
4691  virtual bool number_integer(number_integer_t val) = 0;
4692 
4698  virtual bool number_unsigned(number_unsigned_t val) = 0;
4699 
4706  virtual bool number_float(number_float_t val, const string_t& s) = 0;
4707 
4714  virtual bool string(string_t& val) = 0;
4715 
4722  virtual bool start_object(std::size_t elements) = 0;
4723 
4730  virtual bool key(string_t& val) = 0;
4731 
4736  virtual bool end_object() = 0;
4737 
4744  virtual bool start_array(std::size_t elements) = 0;
4745 
4750  virtual bool end_array() = 0;
4751 
4759  virtual bool parse_error(std::size_t position,
4760  const std::string& last_token,
4761  const detail::exception& ex) = 0;
4762 
4763  virtual ~json_sax() = default;
4764 };
4765 
4766 
4767 namespace detail
4768 {
4782 template<typename BasicJsonType>
4784 {
4785  public:
4786  using number_integer_t = typename BasicJsonType::number_integer_t;
4787  using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
4788  using number_float_t = typename BasicJsonType::number_float_t;
4789  using string_t = typename BasicJsonType::string_t;
4790 
4796  explicit json_sax_dom_parser(BasicJsonType& r, const bool allow_exceptions_ = true)
4797  : root(r), allow_exceptions(allow_exceptions_)
4798  {}
4799 
4800  // make class move-only
4801  json_sax_dom_parser(const json_sax_dom_parser&) = delete;
4803  json_sax_dom_parser& operator=(const json_sax_dom_parser&) = delete;
4804  json_sax_dom_parser& operator=(json_sax_dom_parser&&) = default;
4805  ~json_sax_dom_parser() = default;
4806 
4807  bool null()
4808  {
4809  handle_value(nullptr);
4810  return true;
4811  }
4812 
4813  bool boolean(bool val)
4814  {
4815  handle_value(val);
4816  return true;
4817  }
4818 
4819  bool number_integer(number_integer_t val)
4820  {
4821  handle_value(val);
4822  return true;
4823  }
4824 
4825  bool number_unsigned(number_unsigned_t val)
4826  {
4827  handle_value(val);
4828  return true;
4829  }
4830 
4831  bool number_float(number_float_t val, const string_t& /*unused*/)
4832  {
4833  handle_value(val);
4834  return true;
4835  }
4836 
4837  bool string(string_t& val)
4838  {
4839  handle_value(val);
4840  return true;
4841  }
4842 
4843  bool start_object(std::size_t len)
4844  {
4845  ref_stack.push_back(handle_value(BasicJsonType::value_t::object));
4846 
4847  if (JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) and len > ref_stack.back()->max_size()))
4848  {
4849  JSON_THROW(out_of_range::create(408,
4850  "excessive object size: " + std::to_string(len)));
4851  }
4852 
4853  return true;
4854  }
4855 
4856  bool key(string_t& val)
4857  {
4858  // add null at given key and store the reference for later
4859  object_element = &(ref_stack.back()->m_value.object->operator[](val));
4860  return true;
4861  }
4862 
4863  bool end_object()
4864  {
4865  ref_stack.pop_back();
4866  return true;
4867  }
4868 
4869  bool start_array(std::size_t len)
4870  {
4871  ref_stack.push_back(handle_value(BasicJsonType::value_t::array));
4872 
4873  if (JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) and len > ref_stack.back()->max_size()))
4874  {
4875  JSON_THROW(out_of_range::create(408,
4876  "excessive array size: " + std::to_string(len)));
4877  }
4878 
4879  return true;
4880  }
4881 
4882  bool end_array()
4883  {
4884  ref_stack.pop_back();
4885  return true;
4886  }
4887 
4888  bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
4889  const detail::exception& ex)
4890  {
4891  errored = true;
4892  if (allow_exceptions)
4893  {
4894  // determine the proper exception type from the id
4895  switch ((ex.id / 100) % 100)
4896  {
4897  case 1:
4898  JSON_THROW(*static_cast<const detail::parse_error*>(&ex));
4899  case 4:
4900  JSON_THROW(*static_cast<const detail::out_of_range*>(&ex));
4901  // LCOV_EXCL_START
4902  case 2:
4903  JSON_THROW(*static_cast<const detail::invalid_iterator*>(&ex));
4904  case 3:
4905  JSON_THROW(*static_cast<const detail::type_error*>(&ex));
4906  case 5:
4907  JSON_THROW(*static_cast<const detail::other_error*>(&ex));
4908  default:
4909  assert(false);
4910  // LCOV_EXCL_STOP
4911  }
4912  }
4913  return false;
4914  }
4915 
4916  constexpr bool is_errored() const
4917  {
4918  return errored;
4919  }
4920 
4921  private:
4928  template<typename Value>
4930  BasicJsonType* handle_value(Value&& v)
4931  {
4932  if (ref_stack.empty())
4933  {
4934  root = BasicJsonType(std::forward<Value>(v));
4935  return &root;
4936  }
4937 
4938  assert(ref_stack.back()->is_array() or ref_stack.back()->is_object());
4939 
4940  if (ref_stack.back()->is_array())
4941  {
4942  ref_stack.back()->m_value.array->emplace_back(std::forward<Value>(v));
4943  return &(ref_stack.back()->m_value.array->back());
4944  }
4945 
4946  assert(ref_stack.back()->is_object());
4947  assert(object_element);
4948  *object_element = BasicJsonType(std::forward<Value>(v));
4949  return object_element;
4950  }
4951 
4953  BasicJsonType& root;
4955  std::vector<BasicJsonType*> ref_stack {};
4957  BasicJsonType* object_element = nullptr;
4959  bool errored = false;
4961  const bool allow_exceptions = true;
4962 };
4963 
4964 template<typename BasicJsonType>
4966 {
4967  public:
4968  using number_integer_t = typename BasicJsonType::number_integer_t;
4969  using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
4970  using number_float_t = typename BasicJsonType::number_float_t;
4971  using string_t = typename BasicJsonType::string_t;
4972  using parser_callback_t = typename BasicJsonType::parser_callback_t;
4973  using parse_event_t = typename BasicJsonType::parse_event_t;
4974 
4976  const parser_callback_t cb,
4977  const bool allow_exceptions_ = true)
4978  : root(r), callback(cb), allow_exceptions(allow_exceptions_)
4979  {
4980  keep_stack.push_back(true);
4981  }
4982 
4983  // make class move-only
4986  json_sax_dom_callback_parser& operator=(const json_sax_dom_callback_parser&) = delete;
4988  ~json_sax_dom_callback_parser() = default;
4989 
4990  bool null()
4991  {
4992  handle_value(nullptr);
4993  return true;
4994  }
4995 
4996  bool boolean(bool val)
4997  {
4998  handle_value(val);
4999  return true;
5000  }
5001 
5002  bool number_integer(number_integer_t val)
5003  {
5004  handle_value(val);
5005  return true;
5006  }
5007 
5008  bool number_unsigned(number_unsigned_t val)
5009  {
5010  handle_value(val);
5011  return true;
5012  }
5013 
5014  bool number_float(number_float_t val, const string_t& /*unused*/)
5015  {
5016  handle_value(val);
5017  return true;
5018  }
5019 
5020  bool string(string_t& val)
5021  {
5022  handle_value(val);
5023  return true;
5024  }
5025 
5026  bool start_object(std::size_t len)
5027  {
5028  // check callback for object start
5029  const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::object_start, discarded);
5030  keep_stack.push_back(keep);
5031 
5032  auto val = handle_value(BasicJsonType::value_t::object, true);
5033  ref_stack.push_back(val.second);
5034 
5035  // check object limit
5036  if (ref_stack.back() and JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) and len > ref_stack.back()->max_size()))
5037  {
5038  JSON_THROW(out_of_range::create(408, "excessive object size: " + std::to_string(len)));
5039  }
5040 
5041  return true;
5042  }
5043 
5044  bool key(string_t& val)
5045  {
5046  BasicJsonType k = BasicJsonType(val);
5047 
5048  // check callback for key
5049  const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::key, k);
5050  key_keep_stack.push_back(keep);
5051 
5052  // add discarded value at given key and store the reference for later
5053  if (keep and ref_stack.back())
5054  {
5055  object_element = &(ref_stack.back()->m_value.object->operator[](val) = discarded);
5056  }
5057 
5058  return true;
5059  }
5060 
5061  bool end_object()
5062  {
5063  if (ref_stack.back() and not callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back()))
5064  {
5065  // discard object
5066  *ref_stack.back() = discarded;
5067  }
5068 
5069  assert(not ref_stack.empty());
5070  assert(not keep_stack.empty());
5071  ref_stack.pop_back();
5072  keep_stack.pop_back();
5073 
5074  if (not ref_stack.empty() and ref_stack.back() and ref_stack.back()->is_object())
5075  {
5076  // remove discarded value
5077  for (auto it = ref_stack.back()->begin(); it != ref_stack.back()->end(); ++it)
5078  {
5079  if (it->is_discarded())
5080  {
5081  ref_stack.back()->erase(it);
5082  break;
5083  }
5084  }
5085  }
5086 
5087  return true;
5088  }
5089 
5090  bool start_array(std::size_t len)
5091  {
5092  const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::array_start, discarded);
5093  keep_stack.push_back(keep);
5094 
5095  auto val = handle_value(BasicJsonType::value_t::array, true);
5096  ref_stack.push_back(val.second);
5097 
5098  // check array limit
5099  if (ref_stack.back() and JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) and len > ref_stack.back()->max_size()))
5100  {
5101  JSON_THROW(out_of_range::create(408, "excessive array size: " + std::to_string(len)));
5102  }
5103 
5104  return true;
5105  }
5106 
5107  bool end_array()
5108  {
5109  bool keep = true;
5110 
5111  if (ref_stack.back())
5112  {
5113  keep = callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back());
5114  if (not keep)
5115  {
5116  // discard array
5117  *ref_stack.back() = discarded;
5118  }
5119  }
5120 
5121  assert(not ref_stack.empty());
5122  assert(not keep_stack.empty());
5123  ref_stack.pop_back();
5124  keep_stack.pop_back();
5125 
5126  // remove discarded value
5127  if (not keep and not ref_stack.empty() and ref_stack.back()->is_array())
5128  {
5129  ref_stack.back()->m_value.array->pop_back();
5130  }
5131 
5132  return true;
5133  }
5134 
5135  bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
5136  const detail::exception& ex)
5137  {
5138  errored = true;
5139  if (allow_exceptions)
5140  {
5141  // determine the proper exception type from the id
5142  switch ((ex.id / 100) % 100)
5143  {
5144  case 1:
5145  JSON_THROW(*static_cast<const detail::parse_error*>(&ex));
5146  case 4:
5147  JSON_THROW(*static_cast<const detail::out_of_range*>(&ex));
5148  // LCOV_EXCL_START
5149  case 2:
5150  JSON_THROW(*static_cast<const detail::invalid_iterator*>(&ex));
5151  case 3:
5152  JSON_THROW(*static_cast<const detail::type_error*>(&ex));
5153  case 5:
5154  JSON_THROW(*static_cast<const detail::other_error*>(&ex));
5155  default:
5156  assert(false);
5157  // LCOV_EXCL_STOP
5158  }
5159  }
5160  return false;
5161  }
5162 
5163  constexpr bool is_errored() const
5164  {
5165  return errored;
5166  }
5167 
5168  private:
5184  template<typename Value>
5185  std::pair<bool, BasicJsonType*> handle_value(Value&& v, const bool skip_callback = false)
5186  {
5187  assert(not keep_stack.empty());
5188 
5189  // do not handle this value if we know it would be added to a discarded
5190  // container
5191  if (not keep_stack.back())
5192  {
5193  return {false, nullptr};
5194  }
5195 
5196  // create value
5197  auto value = BasicJsonType(std::forward<Value>(v));
5198 
5199  // check callback
5200  const bool keep = skip_callback or callback(static_cast<int>(ref_stack.size()), parse_event_t::value, value);
5201 
5202  // do not handle this value if we just learnt it shall be discarded
5203  if (not keep)
5204  {
5205  return {false, nullptr};
5206  }
5207 
5208  if (ref_stack.empty())
5209  {
5210  root = std::move(value);
5211  return {true, &root};
5212  }
5213 
5214  // skip this value if we already decided to skip the parent
5215  // (https://github.com/nlohmann/json/issues/971#issuecomment-413678360)
5216  if (not ref_stack.back())
5217  {
5218  return {false, nullptr};
5219  }
5220 
5221  // we now only expect arrays and objects
5222  assert(ref_stack.back()->is_array() or ref_stack.back()->is_object());
5223 
5224  // array
5225  if (ref_stack.back()->is_array())
5226  {
5227  ref_stack.back()->m_value.array->push_back(std::move(value));
5228  return {true, &(ref_stack.back()->m_value.array->back())};
5229  }
5230 
5231  // object
5232  assert(ref_stack.back()->is_object());
5233  // check if we should store an element for the current key
5234  assert(not key_keep_stack.empty());
5235  const bool store_element = key_keep_stack.back();
5236  key_keep_stack.pop_back();
5237 
5238  if (not store_element)
5239  {
5240  return {false, nullptr};
5241  }
5242 
5243  assert(object_element);
5244  *object_element = std::move(value);
5245  return {true, object_element};
5246  }
5247 
5249  BasicJsonType& root;
5251  std::vector<BasicJsonType*> ref_stack {};
5253  std::vector<bool> keep_stack {};
5255  std::vector<bool> key_keep_stack {};
5257  BasicJsonType* object_element = nullptr;
5259  bool errored = false;
5261  const parser_callback_t callback = nullptr;
5263  const bool allow_exceptions = true;
5265  BasicJsonType discarded = BasicJsonType::value_t::discarded;
5266 };
5267 
5268 template<typename BasicJsonType>
5270 {
5271  public:
5272  using number_integer_t = typename BasicJsonType::number_integer_t;
5273  using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
5274  using number_float_t = typename BasicJsonType::number_float_t;
5275  using string_t = typename BasicJsonType::string_t;
5276 
5277  bool null()
5278  {
5279  return true;
5280  }
5281 
5282  bool boolean(bool /*unused*/)
5283  {
5284  return true;
5285  }
5286 
5287  bool number_integer(number_integer_t /*unused*/)
5288  {
5289  return true;
5290  }
5291 
5292  bool number_unsigned(number_unsigned_t /*unused*/)
5293  {
5294  return true;
5295  }
5296 
5297  bool number_float(number_float_t /*unused*/, const string_t& /*unused*/)
5298  {
5299  return true;
5300  }
5301 
5302  bool string(string_t& /*unused*/)
5303  {
5304  return true;
5305  }
5306 
5307  bool start_object(std::size_t /*unused*/ = std::size_t(-1))
5308  {
5309  return true;
5310  }
5311 
5312  bool key(string_t& /*unused*/)
5313  {
5314  return true;
5315  }
5316 
5317  bool end_object()
5318  {
5319  return true;
5320  }
5321 
5322  bool start_array(std::size_t /*unused*/ = std::size_t(-1))
5323  {
5324  return true;
5325  }
5326 
5327  bool end_array()
5328  {
5329  return true;
5330  }
5331 
5332  bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const detail::exception& /*unused*/)
5333  {
5334  return false;
5335  }
5336 };
5337 } // namespace detail
5338 
5339 } // namespace nlohmann
5340 
5341 // #include <nlohmann/detail/macro_scope.hpp>
5342 
5343 // #include <nlohmann/detail/meta/is_sax.hpp>
5344 
5345 
5346 #include <cstdint> // size_t
5347 #include <utility> // declval
5348 #include <string> // string
5349 
5350 // #include <nlohmann/detail/meta/detected.hpp>
5351 
5352 // #include <nlohmann/detail/meta/type_traits.hpp>
5353 
5354 
5355 namespace nlohmann
5356 {
5357 namespace detail
5358 {
5359 template <typename T>
5360 using null_function_t = decltype(std::declval<T&>().null());
5361 
5362 template <typename T>
5363 using boolean_function_t =
5364  decltype(std::declval<T&>().boolean(std::declval<bool>()));
5365 
5366 template <typename T, typename Integer>
5368  decltype(std::declval<T&>().number_integer(std::declval<Integer>()));
5369 
5370 template <typename T, typename Unsigned>
5372  decltype(std::declval<T&>().number_unsigned(std::declval<Unsigned>()));
5373 
5374 template <typename T, typename Float, typename String>
5375 using number_float_function_t = decltype(std::declval<T&>().number_float(
5376  std::declval<Float>(), std::declval<const String&>()));
5377 
5378 template <typename T, typename String>
5379 using string_function_t =
5380  decltype(std::declval<T&>().string(std::declval<String&>()));
5381 
5382 template <typename T>
5384  decltype(std::declval<T&>().start_object(std::declval<std::size_t>()));
5385 
5386 template <typename T, typename String>
5387 using key_function_t =
5388  decltype(std::declval<T&>().key(std::declval<String&>()));
5389 
5390 template <typename T>
5391 using end_object_function_t = decltype(std::declval<T&>().end_object());
5392 
5393 template <typename T>
5394 using start_array_function_t =
5395  decltype(std::declval<T&>().start_array(std::declval<std::size_t>()));
5396 
5397 template <typename T>
5398 using end_array_function_t = decltype(std::declval<T&>().end_array());
5399 
5400 template <typename T, typename Exception>
5401 using parse_error_function_t = decltype(std::declval<T&>().parse_error(
5402  std::declval<std::size_t>(), std::declval<const std::string&>(),
5403  std::declval<const Exception&>()));
5404 
5405 template <typename SAX, typename BasicJsonType>
5406 struct is_sax
5407 {
5408  private:
5410  "BasicJsonType must be of type basic_json<...>");
5411 
5412  using number_integer_t = typename BasicJsonType::number_integer_t;
5413  using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
5414  using number_float_t = typename BasicJsonType::number_float_t;
5415  using string_t = typename BasicJsonType::string_t;
5416  using exception_t = typename BasicJsonType::exception;
5417 
5418  public:
5419  static constexpr bool value =
5427  string_t>::value &&
5435 };
5436 
5437 template <typename SAX, typename BasicJsonType>
5439 {
5440  private:
5442  "BasicJsonType must be of type basic_json<...>");
5443 
5444  using number_integer_t = typename BasicJsonType::number_integer_t;
5445  using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
5446  using number_float_t = typename BasicJsonType::number_float_t;
5447  using string_t = typename BasicJsonType::string_t;
5448  using exception_t = typename BasicJsonType::exception;
5449 
5450  public:
5452  "Missing/invalid function: bool null()");
5454  "Missing/invalid function: bool boolean(bool)");
5456  "Missing/invalid function: bool boolean(bool)");
5457  static_assert(
5460  "Missing/invalid function: bool number_integer(number_integer_t)");
5461  static_assert(
5462  is_detected_exact<bool, number_unsigned_function_t, SAX,
5464  "Missing/invalid function: bool number_unsigned(number_unsigned_t)");
5465  static_assert(is_detected_exact<bool, number_float_function_t, SAX,
5467  "Missing/invalid function: bool number_float(number_float_t, const string_t&)");
5468  static_assert(
5470  "Missing/invalid function: bool string(string_t&)");
5472  "Missing/invalid function: bool start_object(std::size_t)");
5474  "Missing/invalid function: bool key(string_t&)");
5476  "Missing/invalid function: bool end_object()");
5478  "Missing/invalid function: bool start_array(std::size_t)");
5480  "Missing/invalid function: bool end_array()");
5481  static_assert(
5483  "Missing/invalid function: bool parse_error(std::size_t, const "
5484  "std::string&, const exception&)");
5485 };
5486 } // namespace detail
5487 } // namespace nlohmann
5488 
5489 // #include <nlohmann/detail/value_t.hpp>
5490 
5491 
5492 namespace nlohmann
5493 {
5494 namespace detail
5495 {
5497 // binary reader //
5499 
5503 template<typename BasicJsonType, typename SAX = json_sax_dom_parser<BasicJsonType>>
5505 {
5506  using number_integer_t = typename BasicJsonType::number_integer_t;
5507  using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
5508  using number_float_t = typename BasicJsonType::number_float_t;
5509  using string_t = typename BasicJsonType::string_t;
5510  using json_sax_t = SAX;
5511 
5512  public:
5518  explicit binary_reader(input_adapter_t adapter) : ia(std::move(adapter))
5519  {
5521  assert(ia);
5522  }
5523 
5524  // make class move-only
5525  binary_reader(const binary_reader&) = delete;
5526  binary_reader(binary_reader&&) = default;
5527  binary_reader& operator=(const binary_reader&) = delete;
5528  binary_reader& operator=(binary_reader&&) = default;
5529  ~binary_reader() = default;
5530 
5539  bool sax_parse(const input_format_t format,
5540  json_sax_t* sax_,
5541  const bool strict = true)
5542  {
5543  sax = sax_;
5544  bool result = false;
5545 
5546  switch (format)
5547  {
5548  case input_format_t::bson:
5549  result = parse_bson_internal();
5550  break;
5551 
5552  case input_format_t::cbor:
5553  result = parse_cbor_internal();
5554  break;
5555 
5556  case input_format_t::msgpack:
5557  result = parse_msgpack_internal();
5558  break;
5559 
5560  case input_format_t::ubjson:
5561  result = parse_ubjson_internal();
5562  break;
5563 
5564  default: // LCOV_EXCL_LINE
5565  assert(false); // LCOV_EXCL_LINE
5566  }
5567 
5568  // strict mode: next byte must be EOF
5569  if (result and strict)
5570  {
5571  if (format == input_format_t::ubjson)
5572  {
5573  get_ignore_noop();
5574  }
5575  else
5576  {
5577  get();
5578  }
5579 
5580  if (JSON_HEDLEY_UNLIKELY(current != std::char_traits<char>::eof()))
5581  {
5582  return sax->parse_error(chars_read, get_token_string(),
5583  parse_error::create(110, chars_read, exception_message(format, "expected end of input; last byte: 0x" + get_token_string(), "value")));
5584  }
5585  }
5586 
5587  return result;
5588  }
5589 
5597  static constexpr bool little_endianess(int num = 1) noexcept
5598  {
5599  return *reinterpret_cast<char*>(&num) == 1;
5600  }
5601 
5602  private:
5604  // BSON //
5606 
5611  bool parse_bson_internal()
5612  {
5613  std::int32_t document_size;
5614  get_number<std::int32_t, true>(input_format_t::bson, document_size);
5615 
5616  if (JSON_HEDLEY_UNLIKELY(not sax->start_object(std::size_t(-1))))
5617  {
5618  return false;
5619  }
5620 
5621  if (JSON_HEDLEY_UNLIKELY(not parse_bson_element_list(/*is_array*/false)))
5622  {
5623  return false;
5624  }
5625 
5626  return sax->end_object();
5627  }
5628 
5636  bool get_bson_cstr(string_t& result)
5637  {
5638  auto out = std::back_inserter(result);
5639  while (true)
5640  {
5641  get();
5642  if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::bson, "cstring")))
5643  {
5644  return false;
5645  }
5646  if (current == 0x00)
5647  {
5648  return true;
5649  }
5650  *out++ = static_cast<char>(current);
5651  }
5652 
5653  return true;
5654  }
5655 
5667  template<typename NumberType>
5668  bool get_bson_string(const NumberType len, string_t& result)
5669  {
5670  if (JSON_HEDLEY_UNLIKELY(len < 1))
5671  {
5672  auto last_token = get_token_string();
5673  return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "string length must be at least 1, is " + std::to_string(len), "string")));
5674  }
5675 
5676  return get_string(input_format_t::bson, len - static_cast<NumberType>(1), result) and get() != std::char_traits<char>::eof();
5677  }
5678 
5689  bool parse_bson_element_internal(const int element_type,
5690  const std::size_t element_type_parse_position)
5691  {
5692  switch (element_type)
5693  {
5694  case 0x01: // double
5695  {
5696  double number;
5697  return get_number<double, true>(input_format_t::bson, number) and sax->number_float(static_cast<number_float_t>(number), "");
5698  }
5699 
5700  case 0x02: // string
5701  {
5702  std::int32_t len;
5703  string_t value;
5704  return get_number<std::int32_t, true>(input_format_t::bson, len) and get_bson_string(len, value) and sax->string(value);
5705  }
5706 
5707  case 0x03: // object
5708  {
5709  return parse_bson_internal();
5710  }
5711 
5712  case 0x04: // array
5713  {
5714  return parse_bson_array();
5715  }
5716 
5717  case 0x08: // boolean
5718  {
5719  return sax->boolean(get() != 0);
5720  }
5721 
5722  case 0x0A: // null
5723  {
5724  return sax->null();
5725  }
5726 
5727  case 0x10: // int32
5728  {
5729  std::int32_t value;
5730  return get_number<std::int32_t, true>(input_format_t::bson, value) and sax->number_integer(value);
5731  }
5732 
5733  case 0x12: // int64
5734  {
5735  std::int64_t value;
5736  return get_number<std::int64_t, true>(input_format_t::bson, value) and sax->number_integer(value);
5737  }
5738 
5739  default: // anything else not supported (yet)
5740  {
5741  std::array<char, 3> cr{{}};
5742  (std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(element_type));
5743  return sax->parse_error(element_type_parse_position, std::string(cr.data()), parse_error::create(114, element_type_parse_position, "Unsupported BSON record type 0x" + std::string(cr.data())));
5744  }
5745  }
5746  }
5747 
5760  bool parse_bson_element_list(const bool is_array)
5761  {
5762  string_t key;
5763  while (int element_type = get())
5764  {
5765  if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::bson, "element list")))
5766  {
5767  return false;
5768  }
5769 
5770  const std::size_t element_type_parse_position = chars_read;
5771  if (JSON_HEDLEY_UNLIKELY(not get_bson_cstr(key)))
5772  {
5773  return false;
5774  }
5775 
5776  if (not is_array and not sax->key(key))
5777  {
5778  return false;
5779  }
5780 
5781  if (JSON_HEDLEY_UNLIKELY(not parse_bson_element_internal(element_type, element_type_parse_position)))
5782  {
5783  return false;
5784  }
5785 
5786  // get_bson_cstr only appends
5787  key.clear();
5788  }
5789 
5790  return true;
5791  }
5792 
5797  bool parse_bson_array()
5798  {
5799  std::int32_t document_size;
5800  get_number<std::int32_t, true>(input_format_t::bson, document_size);
5801 
5802  if (JSON_HEDLEY_UNLIKELY(not sax->start_array(std::size_t(-1))))
5803  {
5804  return false;
5805  }
5806 
5807  if (JSON_HEDLEY_UNLIKELY(not parse_bson_element_list(/*is_array*/true)))
5808  {
5809  return false;
5810  }
5811 
5812  return sax->end_array();
5813  }
5814 
5816  // CBOR //
5818 
5826  bool parse_cbor_internal(const bool get_char = true)
5827  {
5828  switch (get_char ? get() : current)
5829  {
5830  // EOF
5831  case std::char_traits<char>::eof():
5832  return unexpect_eof(input_format_t::cbor, "value");
5833 
5834  // Integer 0x00..0x17 (0..23)
5835  case 0x00:
5836  case 0x01:
5837  case 0x02:
5838  case 0x03:
5839  case 0x04:
5840  case 0x05:
5841  case 0x06:
5842  case 0x07:
5843  case 0x08:
5844  case 0x09:
5845  case 0x0A:
5846  case 0x0B:
5847  case 0x0C:
5848  case 0x0D:
5849  case 0x0E:
5850  case 0x0F:
5851  case 0x10:
5852  case 0x11:
5853  case 0x12:
5854  case 0x13:
5855  case 0x14:
5856  case 0x15:
5857  case 0x16:
5858  case 0x17:
5859  return sax->number_unsigned(static_cast<number_unsigned_t>(current));
5860 
5861  case 0x18: // Unsigned integer (one-byte uint8_t follows)
5862  {
5863  std::uint8_t number;
5864  return get_number(input_format_t::cbor, number) and sax->number_unsigned(number);
5865  }
5866 
5867  case 0x19: // Unsigned integer (two-byte uint16_t follows)
5868  {
5869  std::uint16_t number;
5870  return get_number(input_format_t::cbor, number) and sax->number_unsigned(number);
5871  }
5872 
5873  case 0x1A: // Unsigned integer (four-byte uint32_t follows)
5874  {
5875  std::uint32_t number;
5876  return get_number(input_format_t::cbor, number) and sax->number_unsigned(number);
5877  }
5878 
5879  case 0x1B: // Unsigned integer (eight-byte uint64_t follows)
5880  {
5881  std::uint64_t number;
5882  return get_number(input_format_t::cbor, number) and sax->number_unsigned(number);
5883  }
5884 
5885  // Negative integer -1-0x00..-1-0x17 (-1..-24)
5886  case 0x20:
5887  case 0x21:
5888  case 0x22:
5889  case 0x23:
5890  case 0x24:
5891  case 0x25:
5892  case 0x26:
5893  case 0x27:
5894  case 0x28:
5895  case 0x29:
5896  case 0x2A:
5897  case 0x2B:
5898  case 0x2C:
5899  case 0x2D:
5900  case 0x2E:
5901  case 0x2F:
5902  case 0x30:
5903  case 0x31:
5904  case 0x32:
5905  case 0x33:
5906  case 0x34:
5907  case 0x35:
5908  case 0x36:
5909  case 0x37:
5910  return sax->number_integer(static_cast<std::int8_t>(0x20 - 1 - current));
5911 
5912  case 0x38: // Negative integer (one-byte uint8_t follows)
5913  {
5914  std::uint8_t number;
5915  return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast<number_integer_t>(-1) - number);
5916  }
5917 
5918  case 0x39: // Negative integer -1-n (two-byte uint16_t follows)
5919  {
5920  std::uint16_t number;
5921  return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast<number_integer_t>(-1) - number);
5922  }
5923 
5924  case 0x3A: // Negative integer -1-n (four-byte uint32_t follows)
5925  {
5926  std::uint32_t number;
5927  return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast<number_integer_t>(-1) - number);
5928  }
5929 
5930  case 0x3B: // Negative integer -1-n (eight-byte uint64_t follows)
5931  {
5932  std::uint64_t number;
5933  return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast<number_integer_t>(-1)
5934  - static_cast<number_integer_t>(number));
5935  }
5936 
5937  // UTF-8 string (0x00..0x17 bytes follow)
5938  case 0x60:
5939  case 0x61:
5940  case 0x62:
5941  case 0x63:
5942  case 0x64:
5943  case 0x65:
5944  case 0x66:
5945  case 0x67:
5946  case 0x68:
5947  case 0x69:
5948  case 0x6A:
5949  case 0x6B:
5950  case 0x6C:
5951  case 0x6D:
5952  case 0x6E:
5953  case 0x6F:
5954  case 0x70:
5955  case 0x71:
5956  case 0x72:
5957  case 0x73:
5958  case 0x74:
5959  case 0x75:
5960  case 0x76:
5961  case 0x77:
5962  case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
5963  case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
5964  case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
5965  case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
5966  case 0x7F: // UTF-8 string (indefinite length)
5967  {
5968  string_t s;
5969  return get_cbor_string(s) and sax->string(s);
5970  }
5971 
5972  // array (0x00..0x17 data items follow)
5973  case 0x80:
5974  case 0x81:
5975  case 0x82:
5976  case 0x83:
5977  case 0x84:
5978  case 0x85:
5979  case 0x86:
5980  case 0x87:
5981  case 0x88:
5982  case 0x89:
5983  case 0x8A:
5984  case 0x8B:
5985  case 0x8C:
5986  case 0x8D:
5987  case 0x8E:
5988  case 0x8F:
5989  case 0x90:
5990  case 0x91:
5991  case 0x92:
5992  case 0x93:
5993  case 0x94:
5994  case 0x95:
5995  case 0x96:
5996  case 0x97:
5997  return get_cbor_array(static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu));
5998 
5999  case 0x98: // array (one-byte uint8_t for n follows)
6000  {
6001  std::uint8_t len;
6002  return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast<std::size_t>(len));
6003  }
6004 
6005  case 0x99: // array (two-byte uint16_t for n follow)
6006  {
6007  std::uint16_t len;
6008  return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast<std::size_t>(len));
6009  }
6010 
6011  case 0x9A: // array (four-byte uint32_t for n follow)
6012  {
6013  std::uint32_t len;
6014  return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast<std::size_t>(len));
6015  }
6016 
6017  case 0x9B: // array (eight-byte uint64_t for n follow)
6018  {
6019  std::uint64_t len;
6020  return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast<std::size_t>(len));
6021  }
6022 
6023  case 0x9F: // array (indefinite length)
6024  return get_cbor_array(std::size_t(-1));
6025 
6026  // map (0x00..0x17 pairs of data items follow)
6027  case 0xA0:
6028  case 0xA1:
6029  case 0xA2:
6030  case 0xA3:
6031  case 0xA4:
6032  case 0xA5:
6033  case 0xA6:
6034  case 0xA7:
6035  case 0xA8:
6036  case 0xA9:
6037  case 0xAA:
6038  case 0xAB:
6039  case 0xAC:
6040  case 0xAD:
6041  case 0xAE:
6042  case 0xAF:
6043  case 0xB0:
6044  case 0xB1:
6045  case 0xB2:
6046  case 0xB3:
6047  case 0xB4:
6048  case 0xB5:
6049  case 0xB6:
6050  case 0xB7:
6051  return get_cbor_object(static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu));
6052 
6053  case 0xB8: // map (one-byte uint8_t for n follows)
6054  {
6055  std::uint8_t len;
6056  return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast<std::size_t>(len));
6057  }
6058 
6059  case 0xB9: // map (two-byte uint16_t for n follow)
6060  {
6061  std::uint16_t len;
6062  return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast<std::size_t>(len));
6063  }
6064 
6065  case 0xBA: // map (four-byte uint32_t for n follow)
6066  {
6067  std::uint32_t len;
6068  return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast<std::size_t>(len));
6069  }
6070 
6071  case 0xBB: // map (eight-byte uint64_t for n follow)
6072  {
6073  std::uint64_t len;
6074  return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast<std::size_t>(len));
6075  }
6076 
6077  case 0xBF: // map (indefinite length)
6078  return get_cbor_object(std::size_t(-1));
6079 
6080  case 0xF4: // false
6081  return sax->boolean(false);
6082 
6083  case 0xF5: // true
6084  return sax->boolean(true);
6085 
6086  case 0xF6: // null
6087  return sax->null();
6088 
6089  case 0xF9: // Half-Precision Float (two-byte IEEE 754)
6090  {
6091  const int byte1_raw = get();
6092  if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::cbor, "number")))
6093  {
6094  return false;
6095  }
6096  const int byte2_raw = get();
6097  if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::cbor, "number")))
6098  {
6099  return false;
6100  }
6101 
6102  const auto byte1 = static_cast<unsigned char>(byte1_raw);
6103  const auto byte2 = static_cast<unsigned char>(byte2_raw);
6104 
6105  // code from RFC 7049, Appendix D, Figure 3:
6106  // As half-precision floating-point numbers were only added
6107  // to IEEE 754 in 2008, today's programming platforms often
6108  // still only have limited support for them. It is very
6109  // easy to include at least decoding support for them even
6110  // without such support. An example of a small decoder for
6111  // half-precision floating-point numbers in the C language
6112  // is shown in Fig. 3.
6113  const auto half = static_cast<unsigned int>((byte1 << 8u) + byte2);
6114  const double val = [&half]
6115  {
6116  const int exp = (half >> 10u) & 0x1Fu;
6117  const unsigned int mant = half & 0x3FFu;
6118  assert(0 <= exp and exp <= 32);
6119  assert(mant <= 1024);
6120  switch (exp)
6121  {
6122  case 0:
6123  return std::ldexp(mant, -24);
6124  case 31:
6125  return (mant == 0)
6126  ? std::numeric_limits<double>::infinity()
6127  : std::numeric_limits<double>::quiet_NaN();
6128  default:
6129  return std::ldexp(mant + 1024, exp - 25);
6130  }
6131  }();
6132  return sax->number_float((half & 0x8000u) != 0
6133  ? static_cast<number_float_t>(-val)
6134  : static_cast<number_float_t>(val), "");
6135  }
6136 
6137  case 0xFA: // Single-Precision Float (four-byte IEEE 754)
6138  {
6139  float number;
6140  return get_number(input_format_t::cbor, number) and sax->number_float(static_cast<number_float_t>(number), "");
6141  }
6142 
6143  case 0xFB: // Double-Precision Float (eight-byte IEEE 754)
6144  {
6145  double number;
6146  return get_number(input_format_t::cbor, number) and sax->number_float(static_cast<number_float_t>(number), "");
6147  }
6148 
6149  default: // anything else (0xFF is handled inside the other types)
6150  {
6151  auto last_token = get_token_string();
6152  return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value")));
6153  }
6154  }
6155  }
6156 
6168  bool get_cbor_string(string_t& result)
6169  {
6170  if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::cbor, "string")))
6171  {
6172  return false;
6173  }
6174 
6175  switch (current)
6176  {
6177  // UTF-8 string (0x00..0x17 bytes follow)
6178  case 0x60:
6179  case 0x61:
6180  case 0x62:
6181  case 0x63:
6182  case 0x64:
6183  case 0x65:
6184  case 0x66:
6185  case 0x67:
6186  case 0x68:
6187  case 0x69:
6188  case 0x6A:
6189  case 0x6B:
6190  case 0x6C:
6191  case 0x6D:
6192  case 0x6E:
6193  case 0x6F:
6194  case 0x70:
6195  case 0x71:
6196  case 0x72:
6197  case 0x73:
6198  case 0x74:
6199  case 0x75:
6200  case 0x76:
6201  case 0x77:
6202  {
6203  return get_string(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
6204  }
6205 
6206  case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
6207  {
6208  std::uint8_t len;
6209  return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result);
6210  }
6211 
6212  case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
6213  {
6214  std::uint16_t len;
6215  return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result);
6216  }
6217 
6218  case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
6219  {
6220  std::uint32_t len;
6221  return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result);
6222  }
6223 
6224  case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
6225  {
6226  std::uint64_t len;
6227  return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result);
6228  }
6229 
6230  case 0x7F: // UTF-8 string (indefinite length)
6231  {
6232  while (get() != 0xFF)
6233  {
6234  string_t chunk;
6235  if (not get_cbor_string(chunk))
6236  {
6237  return false;
6238  }
6239  result.append(chunk);
6240  }
6241  return true;
6242  }
6243 
6244  default:
6245  {
6246  auto last_token = get_token_string();
6247  return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x" + last_token, "string")));
6248  }
6249  }
6250  }
6251 
6257  bool get_cbor_array(const std::size_t len)
6258  {
6259  if (JSON_HEDLEY_UNLIKELY(not sax->start_array(len)))
6260  {
6261  return false;
6262  }
6263 
6264  if (len != std::size_t(-1))
6265  {
6266  for (std::size_t i = 0; i < len; ++i)
6267  {
6268  if (JSON_HEDLEY_UNLIKELY(not parse_cbor_internal()))
6269  {
6270  return false;
6271  }
6272  }
6273  }
6274  else
6275  {
6276  while (get() != 0xFF)
6277  {
6278  if (JSON_HEDLEY_UNLIKELY(not parse_cbor_internal(false)))
6279  {
6280  return false;
6281  }
6282  }
6283  }
6284 
6285  return sax->end_array();
6286  }
6287 
6293  bool get_cbor_object(const std::size_t len)
6294  {
6295  if (JSON_HEDLEY_UNLIKELY(not sax->start_object(len)))
6296  {
6297  return false;
6298  }
6299 
6300  string_t key;
6301  if (len != std::size_t(-1))
6302  {
6303  for (std::size_t i = 0; i < len; ++i)
6304  {
6305  get();
6306  if (JSON_HEDLEY_UNLIKELY(not get_cbor_string(key) or not sax->key(key)))
6307  {
6308  return false;
6309  }
6310 
6311  if (JSON_HEDLEY_UNLIKELY(not parse_cbor_internal()))
6312  {
6313  return false;
6314  }
6315  key.clear();
6316  }
6317  }
6318  else
6319  {
6320  while (get() != 0xFF)
6321  {
6322  if (JSON_HEDLEY_UNLIKELY(not get_cbor_string(key) or not sax->key(key)))
6323  {
6324  return false;
6325  }
6326 
6327  if (JSON_HEDLEY_UNLIKELY(not parse_cbor_internal()))
6328  {
6329  return false;
6330  }
6331  key.clear();
6332  }
6333  }
6334 
6335  return sax->end_object();
6336  }
6337 
6339  // MsgPack //
6341 
6345  bool parse_msgpack_internal()
6346  {
6347  switch (get())
6348  {
6349  // EOF
6350  case std::char_traits<char>::eof():
6351  return unexpect_eof(input_format_t::msgpack, "value");
6352 
6353  // positive fixint
6354  case 0x00:
6355  case 0x01:
6356  case 0x02:
6357  case 0x03:
6358  case 0x04:
6359  case 0x05:
6360  case 0x06:
6361  case 0x07:
6362  case 0x08:
6363  case 0x09:
6364  case 0x0A:
6365  case 0x0B:
6366  case 0x0C:
6367  case 0x0D:
6368  case 0x0E:
6369  case 0x0F:
6370  case 0x10:
6371  case 0x11:
6372  case 0x12:
6373  case 0x13:
6374  case 0x14:
6375  case 0x15:
6376  case 0x16:
6377  case 0x17:
6378  case 0x18:
6379  case 0x19:
6380  case 0x1A:
6381  case 0x1B:
6382  case 0x1C:
6383  case 0x1D:
6384  case 0x1E:
6385  case 0x1F:
6386  case 0x20:
6387  case 0x21:
6388  case 0x22:
6389  case 0x23:
6390  case 0x24:
6391  case 0x25:
6392  case 0x26:
6393  case 0x27:
6394  case 0x28:
6395  case 0x29:
6396  case 0x2A:
6397  case 0x2B:
6398  case 0x2C:
6399  case 0x2D:
6400  case 0x2E:
6401  case 0x2F:
6402  case 0x30:
6403  case 0x31:
6404  case 0x32:
6405  case 0x33:
6406  case 0x34:
6407  case 0x35:
6408  case 0x36:
6409  case 0x37:
6410  case 0x38:
6411  case 0x39:
6412  case 0x3A:
6413  case 0x3B:
6414  case 0x3C:
6415  case 0x3D:
6416  case 0x3E:
6417  case 0x3F:
6418  case 0x40:
6419  case 0x41:
6420  case 0x42:
6421  case 0x43:
6422  case 0x44:
6423  case 0x45:
6424  case 0x46:
6425  case 0x47:
6426  case 0x48:
6427  case 0x49:
6428  case 0x4A:
6429  case 0x4B:
6430  case 0x4C:
6431  case 0x4D:
6432  case 0x4E:
6433  case 0x4F:
6434  case 0x50:
6435  case 0x51:
6436  case 0x52:
6437  case 0x53:
6438  case 0x54:
6439  case 0x55:
6440  case 0x56:
6441  case 0x57:
6442  case 0x58:
6443  case 0x59:
6444  case 0x5A:
6445  case 0x5B:
6446  case 0x5C:
6447  case 0x5D:
6448  case 0x5E:
6449  case 0x5F:
6450  case 0x60:
6451  case 0x61:
6452  case 0x62:
6453  case 0x63:
6454  case 0x64:
6455  case 0x65:
6456  case 0x66:
6457  case 0x67:
6458  case 0x68:
6459  case 0x69:
6460  case 0x6A:
6461  case 0x6B:
6462  case 0x6C:
6463  case 0x6D:
6464  case 0x6E:
6465  case 0x6F:
6466  case 0x70:
6467  case 0x71:
6468  case 0x72:
6469  case 0x73:
6470  case 0x74:
6471  case 0x75:
6472  case 0x76:
6473  case 0x77:
6474  case 0x78:
6475  case 0x79:
6476  case 0x7A:
6477  case 0x7B:
6478  case 0x7C:
6479  case 0x7D:
6480  case 0x7E:
6481  case 0x7F:
6482  return sax->number_unsigned(static_cast<number_unsigned_t>(current));
6483 
6484  // fixmap
6485  case 0x80:
6486  case 0x81:
6487  case 0x82:
6488  case 0x83:
6489  case 0x84:
6490  case 0x85:
6491  case 0x86:
6492  case 0x87:
6493  case 0x88:
6494  case 0x89:
6495  case 0x8A:
6496  case 0x8B:
6497  case 0x8C:
6498  case 0x8D:
6499  case 0x8E:
6500  case 0x8F:
6501  return get_msgpack_object(static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
6502 
6503  // fixarray
6504  case 0x90:
6505  case 0x91:
6506  case 0x92:
6507  case 0x93:
6508  case 0x94:
6509  case 0x95:
6510  case 0x96:
6511  case 0x97:
6512  case 0x98:
6513  case 0x99:
6514  case 0x9A:
6515  case 0x9B:
6516  case 0x9C:
6517  case 0x9D:
6518  case 0x9E:
6519  case 0x9F:
6520  return get_msgpack_array(static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
6521 
6522  // fixstr
6523  case 0xA0:
6524  case 0xA1:
6525  case 0xA2:
6526  case 0xA3:
6527  case 0xA4:
6528  case 0xA5:
6529  case 0xA6:
6530  case 0xA7:
6531  case 0xA8:
6532  case 0xA9:
6533  case 0xAA:
6534  case 0xAB:
6535  case 0xAC:
6536  case 0xAD:
6537  case 0xAE:
6538  case 0xAF:
6539  case 0xB0:
6540  case 0xB1:
6541  case 0xB2:
6542  case 0xB3:
6543  case 0xB4:
6544  case 0xB5:
6545  case 0xB6:
6546  case 0xB7:
6547  case 0xB8:
6548  case 0xB9:
6549  case 0xBA:
6550  case 0xBB:
6551  case 0xBC:
6552  case 0xBD:
6553  case 0xBE:
6554  case 0xBF:
6555  case 0xD9: // str 8
6556  case 0xDA: // str 16
6557  case 0xDB: // str 32
6558  {
6559  string_t s;
6560  return get_msgpack_string(s) and sax->string(s);
6561  }
6562 
6563  case 0xC0: // nil
6564  return sax->null();
6565 
6566  case 0xC2: // false
6567  return sax->boolean(false);
6568 
6569  case 0xC3: // true
6570  return sax->boolean(true);
6571 
6572  case 0xCA: // float 32
6573  {
6574  float number;
6575  return get_number(input_format_t::msgpack, number) and sax->number_float(static_cast<number_float_t>(number), "");
6576  }
6577 
6578  case 0xCB: // float 64
6579  {
6580  double number;
6581  return get_number(input_format_t::msgpack, number) and sax->number_float(static_cast<number_float_t>(number), "");
6582  }
6583 
6584  case 0xCC: // uint 8
6585  {
6586  std::uint8_t number;
6587  return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number);
6588  }
6589 
6590  case 0xCD: // uint 16
6591  {
6592  std::uint16_t number;
6593  return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number);
6594  }
6595 
6596  case 0xCE: // uint 32
6597  {
6598  std::uint32_t number;
6599  return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number);
6600  }
6601 
6602  case 0xCF: // uint 64
6603  {
6604  std::uint64_t number;
6605  return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number);
6606  }
6607 
6608  case 0xD0: // int 8
6609  {
6610  std::int8_t number;
6611  return get_number(input_format_t::msgpack, number) and sax->number_integer(number);
6612  }
6613 
6614  case 0xD1: // int 16
6615  {
6616  std::int16_t number;
6617  return get_number(input_format_t::msgpack, number) and sax->number_integer(number);
6618  }
6619 
6620  case 0xD2: // int 32
6621  {
6622  std::int32_t number;
6623  return get_number(input_format_t::msgpack, number) and sax->number_integer(number);
6624  }
6625 
6626  case 0xD3: // int 64
6627  {
6628  std::int64_t number;
6629  return get_number(input_format_t::msgpack, number) and sax->number_integer(number);
6630  }
6631 
6632  case 0xDC: // array 16
6633  {
6634  std::uint16_t len;
6635  return get_number(input_format_t::msgpack, len) and get_msgpack_array(static_cast<std::size_t>(len));
6636  }
6637 
6638  case 0xDD: // array 32
6639  {
6640  std::uint32_t len;
6641  return get_number(input_format_t::msgpack, len) and get_msgpack_array(static_cast<std::size_t>(len));
6642  }
6643 
6644  case 0xDE: // map 16
6645  {
6646  std::uint16_t len;
6647  return get_number(input_format_t::msgpack, len) and get_msgpack_object(static_cast<std::size_t>(len));
6648  }
6649 
6650  case 0xDF: // map 32
6651  {
6652  std::uint32_t len;
6653  return get_number(input_format_t::msgpack, len) and get_msgpack_object(static_cast<std::size_t>(len));
6654  }
6655 
6656  // negative fixint
6657  case 0xE0:
6658  case 0xE1:
6659  case 0xE2:
6660  case 0xE3:
6661  case 0xE4:
6662  case 0xE5:
6663  case 0xE6:
6664  case 0xE7:
6665  case 0xE8:
6666  case 0xE9:
6667  case 0xEA:
6668  case 0xEB:
6669  case 0xEC:
6670  case 0xED:
6671  case 0xEE:
6672  case 0xEF:
6673  case 0xF0:
6674  case 0xF1:
6675  case 0xF2:
6676  case 0xF3:
6677  case 0xF4:
6678  case 0xF5:
6679  case 0xF6:
6680  case 0xF7:
6681  case 0xF8:
6682  case 0xF9:
6683  case 0xFA:
6684  case 0xFB:
6685  case 0xFC:
6686  case 0xFD:
6687  case 0xFE:
6688  case 0xFF:
6689  return sax->number_integer(static_cast<std::int8_t>(current));
6690 
6691  default: // anything else
6692  {
6693  auto last_token = get_token_string();
6694  return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::msgpack, "invalid byte: 0x" + last_token, "value")));
6695  }
6696  }
6697  }
6698 
6709  bool get_msgpack_string(string_t& result)
6710  {
6711  if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::msgpack, "string")))
6712  {
6713  return false;
6714  }
6715 
6716  switch (current)
6717  {
6718  // fixstr
6719  case 0xA0:
6720  case 0xA1:
6721  case 0xA2:
6722  case 0xA3:
6723  case 0xA4:
6724  case 0xA5:
6725  case 0xA6:
6726  case 0xA7:
6727  case 0xA8:
6728  case 0xA9:
6729  case 0xAA:
6730  case 0xAB:
6731  case 0xAC:
6732  case 0xAD:
6733  case 0xAE:
6734  case 0xAF:
6735  case 0xB0:
6736  case 0xB1:
6737  case 0xB2:
6738  case 0xB3:
6739  case 0xB4:
6740  case 0xB5:
6741  case 0xB6:
6742  case 0xB7:
6743  case 0xB8:
6744  case 0xB9:
6745  case 0xBA:
6746  case 0xBB:
6747  case 0xBC:
6748  case 0xBD:
6749  case 0xBE:
6750  case 0xBF:
6751  {
6752  return get_string(input_format_t::msgpack, static_cast<unsigned int>(current) & 0x1Fu, result);
6753  }
6754 
6755  case 0xD9: // str 8
6756  {
6757  std::uint8_t len;
6758  return get_number(input_format_t::msgpack, len) and get_string(input_format_t::msgpack, len, result);
6759  }
6760 
6761  case 0xDA: // str 16
6762  {
6763  std::uint16_t len;
6764  return get_number(input_format_t::msgpack, len) and get_string(input_format_t::msgpack, len, result);
6765  }
6766 
6767  case 0xDB: // str 32
6768  {
6769  std::uint32_t len;
6770  return get_number(input_format_t::msgpack, len) and get_string(input_format_t::msgpack, len, result);
6771  }
6772 
6773  default:
6774  {
6775  auto last_token = get_token_string();
6776  return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::msgpack, "expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x" + last_token, "string")));
6777  }
6778  }
6779  }
6780 
6785  bool get_msgpack_array(const std::size_t len)
6786  {
6787  if (JSON_HEDLEY_UNLIKELY(not sax->start_array(len)))
6788  {
6789  return false;
6790  }
6791 
6792  for (std::size_t i = 0; i < len; ++i)
6793  {
6794  if (JSON_HEDLEY_UNLIKELY(not parse_msgpack_internal()))
6795  {
6796  return false;
6797  }
6798  }
6799 
6800  return sax->end_array();
6801  }
6802 
6807  bool get_msgpack_object(const std::size_t len)
6808  {
6809  if (JSON_HEDLEY_UNLIKELY(not sax->start_object(len)))
6810  {
6811  return false;
6812  }
6813 
6814  string_t key;
6815  for (std::size_t i = 0; i < len; ++i)
6816  {
6817  get();
6818  if (JSON_HEDLEY_UNLIKELY(not get_msgpack_string(key) or not sax->key(key)))
6819  {
6820  return false;
6821  }
6822 
6823  if (JSON_HEDLEY_UNLIKELY(not parse_msgpack_internal()))
6824  {
6825  return false;
6826  }
6827  key.clear();
6828  }
6829 
6830  return sax->end_object();
6831  }
6832 
6834  // UBJSON //
6836 
6844  bool parse_ubjson_internal(const bool get_char = true)
6845  {
6846  return get_ubjson_value(get_char ? get_ignore_noop() : current);
6847  }
6848 
6863  bool get_ubjson_string(string_t& result, const bool get_char = true)
6864  {
6865  if (get_char)
6866  {
6867  get(); // TODO(niels): may we ignore N here?
6868  }
6869 
6870  if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::ubjson, "value")))
6871  {
6872  return false;
6873  }
6874 
6875  switch (current)
6876  {
6877  case 'U':
6878  {
6879  std::uint8_t len;
6880  return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result);
6881  }
6882 
6883  case 'i':
6884  {
6885  std::int8_t len;
6886  return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result);
6887  }
6888 
6889  case 'I':
6890  {
6891  std::int16_t len;
6892  return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result);
6893  }
6894 
6895  case 'l':
6896  {
6897  std::int32_t len;
6898  return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result);
6899  }
6900 
6901  case 'L':
6902  {
6903  std::int64_t len;
6904  return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result);
6905  }
6906 
6907  default:
6908  auto last_token = get_token_string();
6909  return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token, "string")));
6910  }
6911  }
6912 
6917  bool get_ubjson_size_value(std::size_t& result)
6918  {
6919  switch (get_ignore_noop())
6920  {
6921  case 'U':
6922  {
6923  std::uint8_t number;
6924  if (JSON_HEDLEY_UNLIKELY(not get_number(input_format_t::ubjson, number)))
6925  {
6926  return false;
6927  }
6928  result = static_cast<std::size_t>(number);
6929  return true;
6930  }
6931 
6932  case 'i':
6933  {
6934  std::int8_t number;
6935  if (JSON_HEDLEY_UNLIKELY(not get_number(input_format_t::ubjson, number)))
6936  {
6937  return false;
6938  }
6939  result = static_cast<std::size_t>(number);
6940  return true;
6941  }
6942 
6943  case 'I':
6944  {
6945  std::int16_t number;
6946  if (JSON_HEDLEY_UNLIKELY(not get_number(input_format_t::ubjson, number)))
6947  {
6948  return false;
6949  }
6950  result = static_cast<std::size_t>(number);
6951  return true;
6952  }
6953 
6954  case 'l':
6955  {
6956  std::int32_t number;
6957  if (JSON_HEDLEY_UNLIKELY(not get_number(input_format_t::ubjson, number)))
6958  {
6959  return false;
6960  }
6961  result = static_cast<std::size_t>(number);
6962  return true;
6963  }
6964 
6965  case 'L':
6966  {
6967  std::int64_t number;
6968  if (JSON_HEDLEY_UNLIKELY(not get_number(input_format_t::ubjson, number)))
6969  {
6970  return false;
6971  }
6972  result = static_cast<std::size_t>(number);
6973  return true;
6974  }
6975 
6976  default:
6977  {
6978  auto last_token = get_token_string();
6979  return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token, "size")));
6980  }
6981  }
6982  }
6983 
6994  bool get_ubjson_size_type(std::pair<std::size_t, int>& result)
6995  {
6996  result.first = string_t::npos; // size
6997  result.second = 0; // type
6998 
6999  get_ignore_noop();
7000 
7001  if (current == '$')
7002  {
7003  result.second = get(); // must not ignore 'N', because 'N' maybe the type
7004  if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::ubjson, "type")))
7005  {
7006  return false;
7007  }
7008 
7009  get_ignore_noop();
7010  if (JSON_HEDLEY_UNLIKELY(current != '#'))
7011  {
7012  if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::ubjson, "value")))
7013  {
7014  return false;
7015  }
7016  auto last_token = get_token_string();
7017  return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "expected '#' after type information; last byte: 0x" + last_token, "size")));
7018  }
7019 
7020  return get_ubjson_size_value(result.first);
7021  }
7022 
7023  if (current == '#')
7024  {
7025  return get_ubjson_size_value(result.first);
7026  }
7027 
7028  return true;
7029  }
7030 
7035  bool get_ubjson_value(const int prefix)
7036  {
7037  switch (prefix)
7038  {
7039  case std::char_traits<char>::eof(): // EOF
7040  return unexpect_eof(input_format_t::ubjson, "value");
7041 
7042  case 'T': // true
7043  return sax->boolean(true);
7044  case 'F': // false
7045  return sax->boolean(false);
7046 
7047  case 'Z': // null
7048  return sax->null();
7049 
7050  case 'U':
7051  {
7052  std::uint8_t number;
7053  return get_number(input_format_t::ubjson, number) and sax->number_unsigned(number);
7054  }
7055 
7056  case 'i':
7057  {
7058  std::int8_t number;
7059  return get_number(input_format_t::ubjson, number) and sax->number_integer(number);
7060  }
7061 
7062  case 'I':
7063  {
7064  std::int16_t number;
7065  return get_number(input_format_t::ubjson, number) and sax->number_integer(number);
7066  }
7067 
7068  case 'l':
7069  {
7070  std::int32_t number;
7071  return get_number(input_format_t::ubjson, number) and sax->number_integer(number);
7072  }
7073 
7074  case 'L':
7075  {
7076  std::int64_t number;
7077  return get_number(input_format_t::ubjson, number) and sax->number_integer(number);
7078  }
7079 
7080  case 'd':
7081  {
7082  float number;
7083  return get_number(input_format_t::ubjson, number) and sax->number_float(static_cast<number_float_t>(number), "");
7084  }
7085 
7086  case 'D':
7087  {
7088  double number;
7089  return get_number(input_format_t::ubjson, number) and sax->number_float(static_cast<number_float_t>(number), "");
7090  }
7091 
7092  case 'C': // char
7093  {
7094  get();
7095  if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(input_format_t::ubjson, "char")))
7096  {
7097  return false;
7098  }
7099  if (JSON_HEDLEY_UNLIKELY(current > 127))
7100  {
7101  auto last_token = get_token_string();
7102  return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "byte after 'C' must be in range 0x00..0x7F; last byte: 0x" + last_token, "char")));
7103  }
7104  string_t s(1, static_cast<char>(current));
7105  return sax->string(s);
7106  }
7107 
7108  case 'S': // string
7109  {
7110  string_t s;
7111  return get_ubjson_string(s) and sax->string(s);
7112  }
7113 
7114  case '[': // array
7115  return get_ubjson_array();
7116 
7117  case '{': // object
7118  return get_ubjson_object();
7119 
7120  default: // anything else
7121  {
7122  auto last_token = get_token_string();
7123  return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "invalid byte: 0x" + last_token, "value")));
7124  }
7125  }
7126  }
7127 
7131  bool get_ubjson_array()
7132  {
7133  std::pair<std::size_t, int> size_and_type;
7134  if (JSON_HEDLEY_UNLIKELY(not get_ubjson_size_type(size_and_type)))
7135  {
7136  return false;
7137  }
7138 
7139  if (size_and_type.first != string_t::npos)
7140  {
7141  if (JSON_HEDLEY_UNLIKELY(not sax->start_array(size_and_type.first)))
7142  {
7143  return false;
7144  }
7145 
7146  if (size_and_type.second != 0)
7147  {
7148  if (size_and_type.second != 'N')
7149  {
7150  for (std::size_t i = 0; i < size_and_type.first; ++i)
7151  {
7152  if (JSON_HEDLEY_UNLIKELY(not get_ubjson_value(size_and_type.second)))
7153  {
7154  return false;
7155  }
7156  }
7157  }
7158  }
7159  else
7160  {
7161  for (std::size_t i = 0; i < size_and_type.first; ++i)
7162  {
7163  if (JSON_HEDLEY_UNLIKELY(not parse_ubjson_internal()))
7164  {
7165  return false;
7166  }
7167  }
7168  }
7169  }
7170  else
7171  {
7172  if (JSON_HEDLEY_UNLIKELY(not sax->start_array(std::size_t(-1))))
7173  {
7174  return false;
7175  }
7176 
7177  while (current != ']')
7178  {
7179  if (JSON_HEDLEY_UNLIKELY(not parse_ubjson_internal(false)))
7180  {
7181  return false;
7182  }
7183  get_ignore_noop();
7184  }
7185  }
7186 
7187  return sax->end_array();
7188  }
7189 
7193  bool get_ubjson_object()
7194  {
7195  std::pair<std::size_t, int> size_and_type;
7196  if (JSON_HEDLEY_UNLIKELY(not get_ubjson_size_type(size_and_type)))
7197  {
7198  return false;
7199  }
7200 
7201  string_t key;
7202  if (size_and_type.first != string_t::npos)
7203  {
7204  if (JSON_HEDLEY_UNLIKELY(not sax->start_object(size_and_type.first)))
7205  {
7206  return false;
7207  }
7208 
7209  if (size_and_type.second != 0)
7210  {
7211  for (std::size_t i = 0; i < size_and_type.first; ++i)
7212  {
7213  if (JSON_HEDLEY_UNLIKELY(not get_ubjson_string(key) or not sax->key(key)))
7214  {
7215  return false;
7216  }
7217  if (JSON_HEDLEY_UNLIKELY(not get_ubjson_value(size_and_type.second)))
7218  {
7219  return false;
7220  }
7221  key.clear();
7222  }
7223  }
7224  else
7225  {
7226  for (std::size_t i = 0; i < size_and_type.first; ++i)
7227  {
7228  if (JSON_HEDLEY_UNLIKELY(not get_ubjson_string(key) or not sax->key(key)))
7229  {
7230  return false;
7231  }
7232  if (JSON_HEDLEY_UNLIKELY(not parse_ubjson_internal()))
7233  {
7234  return false;
7235  }
7236  key.clear();
7237  }
7238  }
7239  }
7240  else
7241  {
7242  if (JSON_HEDLEY_UNLIKELY(not sax->start_object(std::size_t(-1))))
7243  {
7244  return false;
7245  }
7246 
7247  while (current != '}')
7248  {
7249  if (JSON_HEDLEY_UNLIKELY(not get_ubjson_string(key, false) or not sax->key(key)))
7250  {
7251  return false;
7252  }
7253  if (JSON_HEDLEY_UNLIKELY(not parse_ubjson_internal()))
7254  {
7255  return false;
7256  }
7257  get_ignore_noop();
7258  key.clear();
7259  }
7260  }
7261 
7262  return sax->end_object();
7263  }
7264 
7266  // Utility functions //
7268 
7278  int get()
7279  {
7280  ++chars_read;
7281  return current = ia->get_character();
7282  }
7283 
7287  int get_ignore_noop()
7288  {
7289  do
7290  {
7291  get();
7292  }
7293  while (current == 'N');
7294 
7295  return current;
7296  }
7297 
7298  /*
7299  @brief read a number from the input
7300 
7301  @tparam NumberType the type of the number
7302  @param[in] format the current format (for diagnostics)
7303  @param[out] result number of type @a NumberType
7304 
7305  @return whether conversion completed
7306 
7307  @note This function needs to respect the system's endianess, because
7308  bytes in CBOR, MessagePack, and UBJSON are stored in network order
7309  (big endian) and therefore need reordering on little endian systems.
7310  */
7311  template<typename NumberType, bool InputIsLittleEndian = false>
7312  bool get_number(const input_format_t format, NumberType& result)
7313  {
7314  // step 1: read input into array with system's byte order
7315  std::array<std::uint8_t, sizeof(NumberType)> vec;
7316  for (std::size_t i = 0; i < sizeof(NumberType); ++i)
7317  {
7318  get();
7319  if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(format, "number")))
7320  {
7321  return false;
7322  }
7323 
7324  // reverse byte order prior to conversion if necessary
7325  if (is_little_endian != InputIsLittleEndian)
7326  {
7327  vec[sizeof(NumberType) - i - 1] = static_cast<std::uint8_t>(current);
7328  }
7329  else
7330  {
7331  vec[i] = static_cast<std::uint8_t>(current); // LCOV_EXCL_LINE
7332  }
7333  }
7334 
7335  // step 2: convert array into number of type T and return
7336  std::memcpy(&result, vec.data(), sizeof(NumberType));
7337  return true;
7338  }
7339 
7354  template<typename NumberType>
7355  bool get_string(const input_format_t format,
7356  const NumberType len,
7357  string_t& result)
7358  {
7359  bool success = true;
7360  std::generate_n(std::back_inserter(result), len, [this, &success, &format]()
7361  {
7362  get();
7363  if (JSON_HEDLEY_UNLIKELY(not unexpect_eof(format, "string")))
7364  {
7365  success = false;
7366  }
7367  return static_cast<char>(current);
7368  });
7369  return success;
7370  }
7371 
7378  bool unexpect_eof(const input_format_t format, const char* context) const
7379  {
7380  if (JSON_HEDLEY_UNLIKELY(current == std::char_traits<char>::eof()))
7381  {
7382  return sax->parse_error(chars_read, "<end of file>",
7383  parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context)));
7384  }
7385  return true;
7386  }
7387 
7391  std::string get_token_string() const
7392  {
7393  std::array<char, 3> cr{{}};
7394  (std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(current));
7395  return std::string{cr.data()};
7396  }
7397 
7404  std::string exception_message(const input_format_t format,
7405  const std::string& detail,
7406  const std::string& context) const
7407  {
7408  std::string error_msg = "syntax error while parsing ";
7409 
7410  switch (format)
7411  {
7412  case input_format_t::cbor:
7413  error_msg += "CBOR";
7414  break;
7415 
7416  case input_format_t::msgpack:
7417  error_msg += "MessagePack";
7418  break;
7419 
7420  case input_format_t::ubjson:
7421  error_msg += "UBJSON";
7422  break;
7423 
7424  case input_format_t::bson:
7425  error_msg += "BSON";
7426  break;
7427 
7428  default: // LCOV_EXCL_LINE
7429  assert(false); // LCOV_EXCL_LINE
7430  }
7431 
7432  return error_msg + " " + context + ": " + detail;
7433  }
7434 
7435  private:
7437  input_adapter_t ia = nullptr;
7438 
7440  int current = std::char_traits<char>::eof();
7441 
7443  std::size_t chars_read = 0;
7444 
7446  const bool is_little_endian = little_endianess();
7447 
7449  json_sax_t* sax = nullptr;
7450 };
7451 } // namespace detail
7452 } // namespace nlohmann
7453 
7454 // #include <nlohmann/detail/input/input_adapters.hpp>
7455 
7456 // #include <nlohmann/detail/input/lexer.hpp>
7457 
7458 
7459 #include <array> // array
7460 #include <clocale> // localeconv
7461 #include <cstddef> // size_t
7462 #include <cstdio> // snprintf
7463 #include <cstdlib> // strtof, strtod, strtold, strtoll, strtoull
7464 #include <initializer_list> // initializer_list
7465 #include <string> // char_traits, string
7466 #include <utility> // move
7467 #include <vector> // vector
7468 
7469 // #include <nlohmann/detail/input/input_adapters.hpp>
7470 
7471 // #include <nlohmann/detail/input/position_t.hpp>
7472 
7473 // #include <nlohmann/detail/macro_scope.hpp>
7474 
7475 
7476 namespace nlohmann
7477 {
7478 namespace detail
7479 {
7481 // lexer //
7483 
7489 template<typename BasicJsonType>
7490 class lexer
7491 {
7492  using number_integer_t = typename BasicJsonType::number_integer_t;
7493  using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
7494  using number_float_t = typename BasicJsonType::number_float_t;
7495  using string_t = typename BasicJsonType::string_t;
7496 
7497  public:
7499  enum class token_type
7500  {
7501  uninitialized,
7502  literal_true,
7503  literal_false,
7504  literal_null,
7505  value_string,
7506  value_unsigned,
7507  value_integer,
7508  value_float,
7509  begin_array,
7510  begin_object,
7511  end_array,
7512  end_object,
7513  name_separator,
7514  value_separator,
7515  parse_error,
7516  end_of_input,
7517  literal_or_value
7518  };
7519 
7523  static const char* token_type_name(const token_type t) noexcept
7524  {
7525  switch (t)
7526  {
7527  case token_type::uninitialized:
7528  return "<uninitialized>";
7529  case token_type::literal_true:
7530  return "true literal";
7531  case token_type::literal_false:
7532  return "false literal";
7533  case token_type::literal_null:
7534  return "null literal";
7535  case token_type::value_string:
7536  return "string literal";
7537  case lexer::token_type::value_unsigned:
7538  case lexer::token_type::value_integer:
7539  case lexer::token_type::value_float:
7540  return "number literal";
7541  case token_type::begin_array:
7542  return "'['";
7543  case token_type::begin_object:
7544  return "'{'";
7545  case token_type::end_array:
7546  return "']'";
7547  case token_type::end_object:
7548  return "'}'";
7549  case token_type::name_separator:
7550  return "':'";
7551  case token_type::value_separator:
7552  return "','";
7553  case token_type::parse_error:
7554  return "<parse error>";
7555  case token_type::end_of_input:
7556  return "end of input";
7557  case token_type::literal_or_value:
7558  return "'[', '{', or a literal";
7559  // LCOV_EXCL_START
7560  default: // catch non-enum values
7561  return "unknown token";
7562  // LCOV_EXCL_STOP
7563  }
7564  }
7565 
7566  explicit lexer(detail::input_adapter_t&& adapter)
7567  : ia(std::move(adapter)), decimal_point_char(get_decimal_point()) {}
7568 
7569  // delete because of pointer members
7570  lexer(const lexer&) = delete;
7571  lexer(lexer&&) = delete;
7572  lexer& operator=(lexer&) = delete;
7573  lexer& operator=(lexer&&) = delete;
7574  ~lexer() = default;
7575 
7576  private:
7578  // locales
7580 
7583  static char get_decimal_point() noexcept
7584  {
7585  const auto loc = localeconv();
7586  assert(loc != nullptr);
7587  return (loc->decimal_point == nullptr) ? '.' : *(loc->decimal_point);
7588  }
7589 
7591  // scan functions
7593 
7609  int get_codepoint()
7610  {
7611  // this function only makes sense after reading `\u`
7612  assert(current == 'u');
7613  int codepoint = 0;
7614 
7615  const auto factors = { 12u, 8u, 4u, 0u };
7616  for (const auto factor : factors)
7617  {
7618  get();
7619 
7620  if (current >= '0' and current <= '9')
7621  {
7622  codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x30u) << factor);
7623  }
7624  else if (current >= 'A' and current <= 'F')
7625  {
7626  codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x37u) << factor);
7627  }
7628  else if (current >= 'a' and current <= 'f')
7629  {
7630  codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x57u) << factor);
7631  }
7632  else
7633  {
7634  return -1;
7635  }
7636  }
7637 
7638  assert(0x0000 <= codepoint and codepoint <= 0xFFFF);
7639  return codepoint;
7640  }
7641 
7657  bool next_byte_in_range(std::initializer_list<int> ranges)
7658  {
7659  assert(ranges.size() == 2 or ranges.size() == 4 or ranges.size() == 6);
7660  add(current);
7661 
7662  for (auto range = ranges.begin(); range != ranges.end(); ++range)
7663  {
7664  get();
7665  if (JSON_HEDLEY_LIKELY(*range <= current and current <= *(++range)))
7666  {
7667  add(current);
7668  }
7669  else
7670  {
7671  error_message = "invalid string: ill-formed UTF-8 byte";
7672  return false;
7673  }
7674  }
7675 
7676  return true;
7677  }
7678 
7694  token_type scan_string()
7695  {
7696  // reset token_buffer (ignore opening quote)
7697  reset();
7698 
7699  // we entered the function by reading an open quote
7700  assert(current == '\"');
7701 
7702  while (true)
7703  {
7704  // get next character
7705  switch (get())
7706  {
7707  // end of file while parsing string
7708  case std::char_traits<char>::eof():
7709  {
7710  error_message = "invalid string: missing closing quote";
7711  return token_type::parse_error;
7712  }
7713 
7714  // closing quote
7715  case '\"':
7716  {
7717  return token_type::value_string;
7718  }
7719 
7720  // escapes
7721  case '\\':
7722  {
7723  switch (get())
7724  {
7725  // quotation mark
7726  case '\"':
7727  add('\"');
7728  break;
7729  // reverse solidus
7730  case '\\':
7731  add('\\');
7732  break;
7733  // solidus
7734  case '/':
7735  add('/');
7736  break;
7737  // backspace
7738  case 'b':
7739  add('\b');
7740  break;
7741  // form feed
7742  case 'f':
7743  add('\f');
7744  break;
7745  // line feed
7746  case 'n':
7747  add('\n');
7748  break;
7749  // carriage return
7750  case 'r':
7751  add('\r');
7752  break;
7753  // tab
7754  case 't':
7755  add('\t');
7756  break;
7757 
7758  // unicode escapes
7759  case 'u':
7760  {
7761  const int codepoint1 = get_codepoint();
7762  int codepoint = codepoint1; // start with codepoint1
7763 
7764  if (JSON_HEDLEY_UNLIKELY(codepoint1 == -1))
7765  {
7766  error_message = "invalid string: '\\u' must be followed by 4 hex digits";
7767  return token_type::parse_error;
7768  }
7769 
7770  // check if code point is a high surrogate
7771  if (0xD800 <= codepoint1 and codepoint1 <= 0xDBFF)
7772  {
7773  // expect next \uxxxx entry
7774  if (JSON_HEDLEY_LIKELY(get() == '\\' and get() == 'u'))
7775  {
7776  const int codepoint2 = get_codepoint();
7777 
7778  if (JSON_HEDLEY_UNLIKELY(codepoint2 == -1))
7779  {
7780  error_message = "invalid string: '\\u' must be followed by 4 hex digits";
7781  return token_type::parse_error;
7782  }
7783 
7784  // check if codepoint2 is a low surrogate
7785  if (JSON_HEDLEY_LIKELY(0xDC00 <= codepoint2 and codepoint2 <= 0xDFFF))
7786  {
7787  // overwrite codepoint
7788  codepoint = static_cast<int>(
7789  // high surrogate occupies the most significant 22 bits
7790  (static_cast<unsigned int>(codepoint1) << 10u)
7791  // low surrogate occupies the least significant 15 bits
7792  + static_cast<unsigned int>(codepoint2)
7793  // there is still the 0xD800, 0xDC00 and 0x10000 noise
7794  // in the result so we have to subtract with:
7795  // (0xD800 << 10) + DC00 - 0x10000 = 0x35FDC00
7796  - 0x35FDC00u);
7797  }
7798  else
7799  {
7800  error_message = "invalid string: surrogate U+DC00..U+DFFF must be followed by U+DC00..U+DFFF";
7801  return token_type::parse_error;
7802  }
7803  }
7804  else
7805  {
7806  error_message = "invalid string: surrogate U+DC00..U+DFFF must be followed by U+DC00..U+DFFF";
7807  return token_type::parse_error;
7808  }
7809  }
7810  else
7811  {
7812  if (JSON_HEDLEY_UNLIKELY(0xDC00 <= codepoint1 and codepoint1 <= 0xDFFF))
7813  {
7814  error_message = "invalid string: surrogate U+DC00..U+DFFF must follow U+D800..U+DBFF";
7815  return token_type::parse_error;
7816  }
7817  }
7818 
7819  // result of the above calculation yields a proper codepoint
7820  assert(0x00 <= codepoint and codepoint <= 0x10FFFF);
7821 
7822  // translate codepoint into bytes
7823  if (codepoint < 0x80)
7824  {
7825  // 1-byte characters: 0xxxxxxx (ASCII)
7826  add(codepoint);
7827  }
7828  else if (codepoint <= 0x7FF)
7829  {
7830  // 2-byte characters: 110xxxxx 10xxxxxx
7831  add(static_cast<int>(0xC0u | (static_cast<unsigned int>(codepoint) >> 6u)));
7832  add(static_cast<int>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
7833  }
7834  else if (codepoint <= 0xFFFF)
7835  {
7836  // 3-byte characters: 1110xxxx 10xxxxxx 10xxxxxx
7837  add(static_cast<int>(0xE0u | (static_cast<unsigned int>(codepoint) >> 12u)));
7838  add(static_cast<int>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
7839  add(static_cast<int>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
7840  }
7841  else
7842  {
7843  // 4-byte characters: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
7844  add(static_cast<int>(0xF0u | (static_cast<unsigned int>(codepoint) >> 18u)));
7845  add(static_cast<int>(0x80u | ((static_cast<unsigned int>(codepoint) >> 12u) & 0x3Fu)));
7846  add(static_cast<int>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
7847  add(static_cast<int>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
7848  }
7849 
7850  break;
7851  }
7852 
7853  // other characters after escape
7854  default:
7855  error_message = "invalid string: forbidden character after backslash";
7856  return token_type::parse_error;
7857  }
7858 
7859  break;
7860  }
7861 
7862  // invalid control characters
7863  case 0x00:
7864  {
7865  error_message = "invalid string: control character U+0000 (NUL) must be escaped to \\u0000";
7866  return token_type::parse_error;
7867  }
7868 
7869  case 0x01:
7870  {
7871  error_message = "invalid string: control character U+0001 (SOH) must be escaped to \\u0001";
7872  return token_type::parse_error;
7873  }
7874 
7875  case 0x02:
7876  {
7877  error_message = "invalid string: control character U+0002 (STX) must be escaped to \\u0002";
7878  return token_type::parse_error;
7879  }
7880 
7881  case 0x03:
7882  {
7883  error_message = "invalid string: control character U+0003 (ETX) must be escaped to \\u0003";
7884  return token_type::parse_error;
7885  }
7886 
7887  case 0x04:
7888  {
7889  error_message = "invalid string: control character U+0004 (EOT) must be escaped to \\u0004";
7890  return token_type::parse_error;
7891  }
7892 
7893  case 0x05:
7894  {
7895  error_message = "invalid string: control character U+0005 (ENQ) must be escaped to \\u0005";
7896  return token_type::parse_error;
7897  }
7898 
7899  case 0x06:
7900  {
7901  error_message = "invalid string: control character U+0006 (ACK) must be escaped to \\u0006";
7902  return token_type::parse_error;
7903  }
7904 
7905  case 0x07:
7906  {
7907  error_message = "invalid string: control character U+0007 (BEL) must be escaped to \\u0007";
7908  return token_type::parse_error;
7909  }
7910 
7911  case 0x08:
7912  {
7913  error_message = "invalid string: control character U+0008 (BS) must be escaped to \\u0008 or \\b";
7914  return token_type::parse_error;
7915  }
7916 
7917  case 0x09:
7918  {
7919  error_message = "invalid string: control character U+0009 (HT) must be escaped to \\u0009 or \\t";
7920  return token_type::parse_error;
7921  }
7922 
7923  case 0x0A:
7924  {
7925  error_message = "invalid string: control character U+000A (LF) must be escaped to \\u000A or \\n";
7926  return token_type::parse_error;
7927  }
7928 
7929  case 0x0B:
7930  {
7931  error_message = "invalid string: control character U+000B (VT) must be escaped to \\u000B";
7932  return token_type::parse_error;
7933  }
7934 
7935  case 0x0C:
7936  {
7937  error_message = "invalid string: control character U+000C (FF) must be escaped to \\u000C or \\f";
7938  return token_type::parse_error;
7939  }
7940 
7941  case 0x0D:
7942  {
7943  error_message = "invalid string: control character U+000D (CR) must be escaped to \\u000D or \\r";
7944  return token_type::parse_error;
7945  }
7946 
7947  case 0x0E:
7948  {
7949  error_message = "invalid string: control character U+000E (SO) must be escaped to \\u000E";
7950  return token_type::parse_error;
7951  }
7952 
7953  case 0x0F:
7954  {
7955  error_message = "invalid string: control character U+000F (SI) must be escaped to \\u000F";
7956  return token_type::parse_error;
7957  }
7958 
7959  case 0x10:
7960  {
7961  error_message = "invalid string: control character U+0010 (DLE) must be escaped to \\u0010";
7962  return token_type::parse_error;
7963  }
7964 
7965  case 0x11:
7966  {
7967  error_message = "invalid string: control character U+0011 (DC1) must be escaped to \\u0011";
7968  return token_type::parse_error;
7969  }
7970 
7971  case 0x12:
7972  {
7973  error_message = "invalid string: control character U+0012 (DC2) must be escaped to \\u0012";
7974  return token_type::parse_error;
7975  }
7976 
7977  case 0x13:
7978  {
7979  error_message = "invalid string: control character U+0013 (DC3) must be escaped to \\u0013";
7980  return token_type::parse_error;
7981  }
7982 
7983  case 0x14:
7984  {
7985  error_message = "invalid string: control character U+0014 (DC4) must be escaped to \\u0014";
7986  return token_type::parse_error;
7987  }
7988 
7989  case 0x15:
7990  {
7991  error_message = "invalid string: control character U+0015 (NAK) must be escaped to \\u0015";
7992  return token_type::parse_error;
7993  }
7994 
7995  case 0x16:
7996  {
7997  error_message = "invalid string: control character U+0016 (SYN) must be escaped to \\u0016";
7998  return token_type::parse_error;
7999  }
8000 
8001  case 0x17:
8002  {
8003  error_message = "invalid string: control character U+0017 (ETB) must be escaped to \\u0017";
8004  return token_type::parse_error;
8005  }
8006 
8007  case 0x18:
8008  {
8009  error_message = "invalid string: control character U+0018 (CAN) must be escaped to \\u0018";
8010  return token_type::parse_error;
8011  }
8012 
8013  case 0x19:
8014  {
8015  error_message = "invalid string: control character U+0019 (EM) must be escaped to \\u0019";
8016  return token_type::parse_error;
8017  }
8018 
8019  case 0x1A:
8020  {
8021  error_message = "invalid string: control character U+001A (SUB) must be escaped to \\u001A";
8022  return token_type::parse_error;
8023  }
8024 
8025  case 0x1B:
8026  {
8027  error_message = "invalid string: control character U+001B (ESC) must be escaped to \\u001B";
8028  return token_type::parse_error;
8029  }
8030 
8031  case 0x1C:
8032  {
8033  error_message = "invalid string: control character U+001C (FS) must be escaped to \\u001C";
8034  return token_type::parse_error;
8035  }
8036 
8037  case 0x1D:
8038  {
8039  error_message = "invalid string: control character U+001D (GS) must be escaped to \\u001D";
8040  return token_type::parse_error;
8041  }
8042 
8043  case 0x1E:
8044  {
8045  error_message = "invalid string: control character U+001E (RS) must be escaped to \\u001E";
8046  return token_type::parse_error;
8047  }
8048 
8049  case 0x1F:
8050  {
8051  error_message = "invalid string: control character U+001F (US) must be escaped to \\u001F";
8052  return token_type::parse_error;
8053  }
8054 
8055  // U+0020..U+007F (except U+0022 (quote) and U+005C (backspace))
8056  case 0x20:
8057  case 0x21:
8058  case 0x23:
8059  case 0x24:
8060  case 0x25:
8061  case 0x26:
8062  case 0x27:
8063  case 0x28:
8064  case 0x29:
8065  case 0x2A:
8066  case 0x2B:
8067  case 0x2C:
8068  case 0x2D:
8069  case 0x2E:
8070  case 0x2F:
8071  case 0x30:
8072  case 0x31:
8073  case 0x32:
8074  case 0x33:
8075  case 0x34:
8076  case 0x35:
8077  case 0x36:
8078  case 0x37:
8079  case 0x38:
8080  case 0x39:
8081  case 0x3A:
8082  case 0x3B:
8083  case 0x3C:
8084  case 0x3D:
8085  case 0x3E:
8086  case 0x3F:
8087  case 0x40:
8088  case 0x41:
8089  case 0x42:
8090  case 0x43:
8091  case 0x44:
8092  case 0x45:
8093  case 0x46:
8094  case 0x47:
8095  case 0x48:
8096  case 0x49:
8097  case 0x4A:
8098  case 0x4B:
8099  case 0x4C:
8100  case 0x4D:
8101  case 0x4E:
8102  case 0x4F:
8103  case 0x50:
8104  case 0x51:
8105  case 0x52:
8106  case 0x53:
8107  case 0x54:
8108  case 0x55:
8109  case 0x56:
8110  case 0x57:
8111  case 0x58:
8112  case 0x59:
8113  case 0x5A:
8114  case 0x5B:
8115  case 0x5D:
8116  case 0x5E:
8117  case 0x5F:
8118  case 0x60:
8119  case 0x61:
8120  case 0x62:
8121  case 0x63:
8122  case 0x64:
8123  case 0x65:
8124  case 0x66:
8125  case 0x67:
8126  case 0x68:
8127  case 0x69:
8128  case 0x6A:
8129  case 0x6B:
8130  case 0x6C:
8131  case 0x6D:
8132  case 0x6E:
8133  case 0x6F:
8134  case 0x70:
8135  case 0x71:
8136  case 0x72:
8137  case 0x73:
8138  case 0x74:
8139  case 0x75:
8140  case 0x76:
8141  case 0x77:
8142  case 0x78:
8143  case 0x79:
8144  case 0x7A:
8145  case 0x7B:
8146  case 0x7C:
8147  case 0x7D:
8148  case 0x7E:
8149  case 0x7F:
8150  {
8151  add(current);
8152  break;
8153  }
8154 
8155  // U+0080..U+07FF: bytes C2..DF 80..BF
8156  case 0xC2:
8157  case 0xC3:
8158  case 0xC4:
8159  case 0xC5:
8160  case 0xC6:
8161  case 0xC7:
8162  case 0xC8:
8163  case 0xC9:
8164  case 0xCA:
8165  case 0xCB:
8166  case 0xCC:
8167  case 0xCD:
8168  case 0xCE:
8169  case 0xCF:
8170  case 0xD0:
8171  case 0xD1:
8172  case 0xD2:
8173  case 0xD3:
8174  case 0xD4:
8175  case 0xD5:
8176  case 0xD6:
8177  case 0xD7:
8178  case 0xD8:
8179  case 0xD9:
8180  case 0xDA:
8181  case 0xDB:
8182  case 0xDC:
8183  case 0xDD:
8184  case 0xDE:
8185  case 0xDF:
8186  {
8187  if (JSON_HEDLEY_UNLIKELY(not next_byte_in_range({0x80, 0xBF})))
8188  {
8189  return token_type::parse_error;
8190  }
8191  break;
8192  }
8193 
8194  // U+0800..U+0FFF: bytes E0 A0..BF 80..BF
8195  case 0xE0:
8196  {
8197  if (JSON_HEDLEY_UNLIKELY(not (next_byte_in_range({0xA0, 0xBF, 0x80, 0xBF}))))
8198  {
8199  return token_type::parse_error;
8200  }
8201  break;
8202  }
8203 
8204  // U+1000..U+CFFF: bytes E1..EC 80..BF 80..BF
8205  // U+E000..U+FFFF: bytes EE..EF 80..BF 80..BF
8206  case 0xE1:
8207  case 0xE2:
8208  case 0xE3:
8209  case 0xE4:
8210  case 0xE5:
8211  case 0xE6:
8212  case 0xE7:
8213  case 0xE8:
8214  case 0xE9:
8215  case 0xEA:
8216  case 0xEB:
8217  case 0xEC:
8218  case 0xEE:
8219  case 0xEF:
8220  {
8221  if (JSON_HEDLEY_UNLIKELY(not (next_byte_in_range({0x80, 0xBF, 0x80, 0xBF}))))
8222  {
8223  return token_type::parse_error;
8224  }
8225  break;
8226  }
8227 
8228  // U+D000..U+D7FF: bytes ED 80..9F 80..BF
8229  case 0xED:
8230  {
8231  if (JSON_HEDLEY_UNLIKELY(not (next_byte_in_range({0x80, 0x9F, 0x80, 0xBF}))))
8232  {
8233  return token_type::parse_error;
8234  }
8235  break;
8236  }
8237 
8238  // U+10000..U+3FFFF F0 90..BF 80..BF 80..BF
8239  case 0xF0:
8240  {
8241  if (JSON_HEDLEY_UNLIKELY(not (next_byte_in_range({0x90, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
8242  {
8243  return token_type::parse_error;
8244  }
8245  break;
8246  }
8247 
8248  // U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF
8249  case 0xF1:
8250  case 0xF2:
8251  case 0xF3:
8252  {
8253  if (JSON_HEDLEY_UNLIKELY(not (next_byte_in_range({0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
8254  {
8255  return token_type::parse_error;
8256  }
8257  break;
8258  }
8259 
8260  // U+100000..U+10FFFF F4 80..8F 80..BF 80..BF
8261  case 0xF4:
8262  {
8263  if (JSON_HEDLEY_UNLIKELY(not (next_byte_in_range({0x80, 0x8F, 0x80, 0xBF, 0x80, 0xBF}))))
8264  {
8265  return token_type::parse_error;
8266  }
8267  break;
8268  }
8269 
8270  // remaining bytes (80..C1 and F5..FF) are ill-formed
8271  default:
8272  {
8273  error_message = "invalid string: ill-formed UTF-8 byte";
8274  return token_type::parse_error;
8275  }
8276  }
8277  }
8278  }
8279 
8281  static void strtof(float& f, const char* str, char** endptr) noexcept
8282  {
8283  f = std::strtof(str, endptr);
8284  }
8285 
8287  static void strtof(double& f, const char* str, char** endptr) noexcept
8288  {
8289  f = std::strtod(str, endptr);
8290  }
8291 
8293  static void strtof(long double& f, const char* str, char** endptr) noexcept
8294  {
8295  f = std::strtold(str, endptr);
8296  }
8297 
8338  token_type scan_number() // lgtm [cpp/use-of-goto]
8339  {
8340  // reset token_buffer to store the number's bytes
8341  reset();
8342 
8343  // the type of the parsed number; initially set to unsigned; will be
8344  // changed if minus sign, decimal point or exponent is read
8345  token_type number_type = token_type::value_unsigned;
8346 
8347  // state (init): we just found out we need to scan a number
8348  switch (current)
8349  {
8350  case '-':
8351  {
8352  add(current);
8353  goto scan_number_minus;
8354  }
8355 
8356  case '0':
8357  {
8358  add(current);
8359  goto scan_number_zero;
8360  }
8361 
8362  case '1':
8363  case '2':
8364  case '3':
8365  case '4':
8366  case '5':
8367  case '6':
8368  case '7':
8369  case '8':
8370  case '9':
8371  {
8372  add(current);
8373  goto scan_number_any1;
8374  }
8375 
8376  // all other characters are rejected outside scan_number()
8377  default: // LCOV_EXCL_LINE
8378  assert(false); // LCOV_EXCL_LINE
8379  }
8380 
8381 scan_number_minus:
8382  // state: we just parsed a leading minus sign
8383  number_type = token_type::value_integer;
8384  switch (get())
8385  {
8386  case '0':
8387  {
8388  add(current);
8389  goto scan_number_zero;
8390  }
8391 
8392  case '1':
8393  case '2':
8394  case '3':
8395  case '4':
8396  case '5':
8397  case '6':
8398  case '7':
8399  case '8':
8400  case '9':
8401  {
8402  add(current);
8403  goto scan_number_any1;
8404  }
8405 
8406  default:
8407  {
8408  error_message = "invalid number; expected digit after '-'";
8409  return token_type::parse_error;
8410  }
8411  }
8412 
8413 scan_number_zero:
8414  // state: we just parse a zero (maybe with a leading minus sign)
8415  switch (get())
8416  {
8417  case '.':
8418  {
8419  add(decimal_point_char);
8420  goto scan_number_decimal1;
8421  }
8422 
8423  case 'e':
8424  case 'E':
8425  {
8426  add(current);
8427  goto scan_number_exponent;
8428  }
8429 
8430  default:
8431  goto scan_number_done;
8432  }
8433 
8434 scan_number_any1:
8435  // state: we just parsed a number 0-9 (maybe with a leading minus sign)
8436  switch (get())
8437  {
8438  case '0':
8439  case '1':
8440  case '2':
8441  case '3':
8442  case '4':
8443  case '5':
8444  case '6':
8445  case '7':
8446  case '8':
8447  case '9':
8448  {
8449  add(current);
8450  goto scan_number_any1;
8451  }
8452 
8453  case '.':
8454  {
8455  add(decimal_point_char);
8456  goto scan_number_decimal1;
8457  }
8458 
8459  case 'e':
8460  case 'E':
8461  {
8462  add(current);
8463  goto scan_number_exponent;
8464  }
8465 
8466  default:
8467  goto scan_number_done;
8468  }
8469 
8470 scan_number_decimal1:
8471  // state: we just parsed a decimal point
8472  number_type = token_type::value_float;
8473  switch (get())
8474  {
8475  case '0':
8476  case '1':
8477  case '2':
8478  case '3':
8479  case '4':
8480  case '5':
8481  case '6':
8482  case '7':
8483  case '8':
8484  case '9':
8485  {
8486  add(current);
8487  goto scan_number_decimal2;
8488  }
8489 
8490  default:
8491  {
8492  error_message = "invalid number; expected digit after '.'";
8493  return token_type::parse_error;
8494  }
8495  }
8496 
8497 scan_number_decimal2:
8498  // we just parsed at least one number after a decimal point
8499  switch (get())
8500  {
8501  case '0':
8502  case '1':
8503  case '2':
8504  case '3':
8505  case '4':
8506  case '5':
8507  case '6':
8508  case '7':
8509  case '8':
8510  case '9':
8511  {
8512  add(current);
8513  goto scan_number_decimal2;
8514  }
8515 
8516  case 'e':
8517  case 'E':
8518  {
8519  add(current);
8520  goto scan_number_exponent;
8521  }
8522 
8523  default:
8524  goto scan_number_done;
8525  }
8526 
8527 scan_number_exponent:
8528  // we just parsed an exponent
8529  number_type = token_type::value_float;
8530  switch (get())
8531  {
8532  case '+':
8533  case '-':
8534  {
8535  add(current);
8536  goto scan_number_sign;
8537  }
8538 
8539  case '0':
8540  case '1':
8541  case '2':
8542  case '3':
8543  case '4':
8544  case '5':
8545  case '6':
8546  case '7':
8547  case '8':
8548  case '9':
8549  {
8550  add(current);
8551  goto scan_number_any2;
8552  }
8553 
8554  default:
8555  {
8556  error_message =
8557  "invalid number; expected '+', '-', or digit after exponent";
8558  return token_type::parse_error;
8559  }
8560  }
8561 
8562 scan_number_sign:
8563  // we just parsed an exponent sign
8564  switch (get())
8565  {
8566  case '0':
8567  case '1':
8568  case '2':
8569  case '3':
8570  case '4':
8571  case '5':
8572  case '6':
8573  case '7':
8574  case '8':
8575  case '9':
8576  {
8577  add(current);
8578  goto scan_number_any2;
8579  }
8580 
8581  default:
8582  {
8583  error_message = "invalid number; expected digit after exponent sign";
8584  return token_type::parse_error;
8585  }
8586  }
8587 
8588 scan_number_any2:
8589  // we just parsed a number after the exponent or exponent sign
8590  switch (get())
8591  {
8592  case '0':
8593  case '1':
8594  case '2':
8595  case '3':
8596  case '4':
8597  case '5':
8598  case '6':
8599  case '7':
8600  case '8':
8601  case '9':
8602  {
8603  add(current);
8604  goto scan_number_any2;
8605  }
8606 
8607  default:
8608  goto scan_number_done;
8609  }
8610 
8611 scan_number_done:
8612  // unget the character after the number (we only read it to know that
8613  // we are done scanning a number)
8614  unget();
8615 
8616  char* endptr = nullptr;
8617  errno = 0;
8618 
8619  // try to parse integers first and fall back to floats
8620  if (number_type == token_type::value_unsigned)
8621  {
8622  const auto x = std::strtoull(token_buffer.data(), &endptr, 10);
8623 
8624  // we checked the number format before
8625  assert(endptr == token_buffer.data() + token_buffer.size());
8626 
8627  if (errno == 0)
8628  {
8629  value_unsigned = static_cast<number_unsigned_t>(x);
8630  if (value_unsigned == x)
8631  {
8632  return token_type::value_unsigned;
8633  }
8634  }
8635  }
8636  else if (number_type == token_type::value_integer)
8637  {
8638  const auto x = std::strtoll(token_buffer.data(), &endptr, 10);
8639 
8640  // we checked the number format before
8641  assert(endptr == token_buffer.data() + token_buffer.size());
8642 
8643  if (errno == 0)
8644  {
8645  value_integer = static_cast<number_integer_t>(x);
8646  if (value_integer == x)
8647  {
8648  return token_type::value_integer;
8649  }
8650  }
8651  }
8652 
8653  // this code is reached if we parse a floating-point number or if an
8654  // integer conversion above failed
8655  strtof(value_float, token_buffer.data(), &endptr);
8656 
8657  // we checked the number format before
8658  assert(endptr == token_buffer.data() + token_buffer.size());
8659 
8660  return token_type::value_float;
8661  }
8662 
8669  token_type scan_literal(const char* literal_text, const std::size_t length,
8670  token_type return_type)
8671  {
8672  assert(current == literal_text[0]);
8673  for (std::size_t i = 1; i < length; ++i)
8674  {
8675  if (JSON_HEDLEY_UNLIKELY(get() != literal_text[i]))
8676  {
8677  error_message = "invalid literal";
8678  return token_type::parse_error;
8679  }
8680  }
8681  return return_type;
8682  }
8683 
8685  // input management
8687 
8689  void reset() noexcept
8690  {
8691  token_buffer.clear();
8692  token_string.clear();
8693  token_string.push_back(std::char_traits<char>::to_char_type(current));
8694  }
8695 
8696  /*
8697  @brief get next character from the input
8698 
8699  This function provides the interface to the used input adapter. It does
8700  not throw in case the input reached EOF, but returns a
8701  `std::char_traits<char>::eof()` in that case. Stores the scanned characters
8702  for use in error messages.
8703 
8704  @return character read from the input
8705  */
8706  std::char_traits<char>::int_type get()
8707  {
8708  ++position.chars_read_total;
8709  ++position.chars_read_current_line;
8710 
8711  if (next_unget)
8712  {
8713  // just reset the next_unget variable and work with current
8714  next_unget = false;
8715  }
8716  else
8717  {
8718  current = ia->get_character();
8719  }
8720 
8721  if (JSON_HEDLEY_LIKELY(current != std::char_traits<char>::eof()))
8722  {
8723  token_string.push_back(std::char_traits<char>::to_char_type(current));
8724  }
8725 
8726  if (current == '\n')
8727  {
8728  ++position.lines_read;
8729  position.chars_read_current_line = 0;
8730  }
8731 
8732  return current;
8733  }
8734 
8743  void unget()
8744  {
8745  next_unget = true;
8746 
8747  --position.chars_read_total;
8748 
8749  // in case we "unget" a newline, we have to also decrement the lines_read
8750  if (position.chars_read_current_line == 0)
8751  {
8752  if (position.lines_read > 0)
8753  {
8754  --position.lines_read;
8755  }
8756  }
8757  else
8758  {
8759  --position.chars_read_current_line;
8760  }
8761 
8762  if (JSON_HEDLEY_LIKELY(current != std::char_traits<char>::eof()))
8763  {
8764  assert(not token_string.empty());
8765  token_string.pop_back();
8766  }
8767  }
8768 
8770  void add(int c)
8771  {
8772  token_buffer.push_back(std::char_traits<char>::to_char_type(c));
8773  }
8774 
8775  public:
8777  // value getters
8779 
8781  constexpr number_integer_t get_number_integer() const noexcept
8782  {
8783  return value_integer;
8784  }
8785 
8787  constexpr number_unsigned_t get_number_unsigned() const noexcept
8788  {
8789  return value_unsigned;
8790  }
8791 
8793  constexpr number_float_t get_number_float() const noexcept
8794  {
8795  return value_float;
8796  }
8797 
8799  string_t& get_string()
8800  {
8801  return token_buffer;
8802  }
8803 
8805  // diagnostics
8807 
8809  constexpr position_t get_position() const noexcept
8810  {
8811  return position;
8812  }
8813 
8817  std::string get_token_string() const
8818  {
8819  // escape control characters
8820  std::string result;
8821  for (const auto c : token_string)
8822  {
8823  if ('\x00' <= c and c <= '\x1F')
8824  {
8825  // escape control characters
8826  std::array<char, 9> cs{{}};
8827  (std::snprintf)(cs.data(), cs.size(), "<U+%.4X>", static_cast<unsigned char>(c));
8828  result += cs.data();
8829  }
8830  else
8831  {
8832  // add character as is
8833  result.push_back(c);
8834  }
8835  }
8836 
8837  return result;
8838  }
8839 
8842  constexpr const char* get_error_message() const noexcept
8843  {
8844  return error_message;
8845  }
8846 
8848  // actual scanner
8850 
8855  bool skip_bom()
8856  {
8857  if (get() == 0xEF)
8858  {
8859  // check if we completely parse the BOM
8860  return get() == 0xBB and get() == 0xBF;
8861  }
8862 
8863  // the first character is not the beginning of the BOM; unget it to
8864  // process is later
8865  unget();
8866  return true;
8867  }
8868 
8870  {
8871  // initially, skip the BOM
8872  if (position.chars_read_total == 0 and not skip_bom())
8873  {
8874  error_message = "invalid BOM; must be 0xEF 0xBB 0xBF if given";
8875  return token_type::parse_error;
8876  }
8877 
8878  // read next character and ignore whitespace
8879  do
8880  {
8881  get();
8882  }
8883  while (current == ' ' or current == '\t' or current == '\n' or current == '\r');
8884 
8885  switch (current)
8886  {
8887  // structural characters
8888  case '[':
8889  return token_type::begin_array;
8890  case ']':
8891  return token_type::end_array;
8892  case '{':
8893  return token_type::begin_object;
8894  case '}':
8895  return token_type::end_object;
8896  case ':':
8897  return token_type::name_separator;
8898  case ',':
8899  return token_type::value_separator;
8900 
8901  // literals
8902  case 't':
8903  return scan_literal("true", 4, token_type::literal_true);
8904  case 'f':
8905  return scan_literal("false", 5, token_type::literal_false);
8906  case 'n':
8907  return scan_literal("null", 4, token_type::literal_null);
8908 
8909  // string
8910  case '\"':
8911  return scan_string();
8912 
8913  // number
8914  case '-':
8915  case '0':
8916  case '1':
8917  case '2':
8918  case '3':
8919  case '4':
8920  case '5':
8921  case '6':
8922  case '7':
8923  case '8':
8924  case '9':
8925  return scan_number();
8926 
8927  // end of input (the null byte is needed when parsing from
8928  // string literals)
8929  case '\0':
8930  case std::char_traits<char>::eof():
8931  return token_type::end_of_input;
8932 
8933  // error
8934  default:
8935  error_message = "invalid literal";
8936  return token_type::parse_error;
8937  }
8938  }
8939 
8940  private:
8943 
8945  std::char_traits<char>::int_type current = std::char_traits<char>::eof();
8946 
8948  bool next_unget = false;
8949 
8952 
8954  std::vector<char> token_string {};
8955 
8957  string_t token_buffer {};
8958 
8960  const char* error_message = "";
8961 
8962  // number values
8963  number_integer_t value_integer = 0;
8964  number_unsigned_t value_unsigned = 0;
8965  number_float_t value_float = 0;
8966 
8968  const char decimal_point_char = '.';
8969 };
8970 } // namespace detail
8971 } // namespace nlohmann
8972 
8973 // #include <nlohmann/detail/input/parser.hpp>
8974 
8975 
8976 #include <cassert> // assert
8977 #include <cmath> // isfinite
8978 #include <cstdint> // uint8_t
8979 #include <functional> // function
8980 #include <string> // string
8981 #include <utility> // move
8982 #include <vector> // vector
8983 
8984 // #include <nlohmann/detail/exceptions.hpp>
8985 
8986 // #include <nlohmann/detail/input/input_adapters.hpp>
8987 
8988 // #include <nlohmann/detail/input/json_sax.hpp>
8989 
8990 // #include <nlohmann/detail/input/lexer.hpp>
8991 
8992 // #include <nlohmann/detail/macro_scope.hpp>
8993 
8994 // #include <nlohmann/detail/meta/is_sax.hpp>
8995 
8996 // #include <nlohmann/detail/value_t.hpp>
8997 
8998 
8999 namespace nlohmann
9000 {
9001 namespace detail
9002 {
9004 // parser //
9006 
9012 template<typename BasicJsonType>
9013 class parser
9014 {
9015  using number_integer_t = typename BasicJsonType::number_integer_t;
9016  using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
9017  using number_float_t = typename BasicJsonType::number_float_t;
9018  using string_t = typename BasicJsonType::string_t;
9021 
9022  public:
9023  enum class parse_event_t : uint8_t
9024  {
9026  object_start,
9028  object_end,
9030  array_start,
9032  array_end,
9034  key,
9036  value
9037  };
9038 
9039  using parser_callback_t =
9040  std::function<bool(int depth, parse_event_t event, BasicJsonType& parsed)>;
9041 
9043  explicit parser(detail::input_adapter_t&& adapter,
9044  const parser_callback_t cb = nullptr,
9045  const bool allow_exceptions_ = true)
9046  : callback(cb), m_lexer(std::move(adapter)), allow_exceptions(allow_exceptions_)
9047  {
9048  // read first token
9049  get_token();
9050  }
9051 
9062  void parse(const bool strict, BasicJsonType& result)
9063  {
9064  if (callback)
9065  {
9066  json_sax_dom_callback_parser<BasicJsonType> sdp(result, callback, allow_exceptions);
9067  sax_parse_internal(&sdp);
9068  result.assert_invariant();
9069 
9070  // in strict mode, input must be completely read
9071  if (strict and (get_token() != token_type::end_of_input))
9072  {
9073  sdp.parse_error(m_lexer.get_position(),
9074  m_lexer.get_token_string(),
9075  parse_error::create(101, m_lexer.get_position(),
9076  exception_message(token_type::end_of_input, "value")));
9077  }
9078 
9079  // in case of an error, return discarded value
9080  if (sdp.is_errored())
9081  {
9082  result = value_t::discarded;
9083  return;
9084  }
9085 
9086  // set top-level value to null if it was discarded by the callback
9087  // function
9088  if (result.is_discarded())
9089  {
9090  result = nullptr;
9091  }
9092  }
9093  else
9094  {
9095  json_sax_dom_parser<BasicJsonType> sdp(result, allow_exceptions);
9096  sax_parse_internal(&sdp);
9097  result.assert_invariant();
9098 
9099  // in strict mode, input must be completely read
9100  if (strict and (get_token() != token_type::end_of_input))
9101  {
9102  sdp.parse_error(m_lexer.get_position(),
9103  m_lexer.get_token_string(),
9104  parse_error::create(101, m_lexer.get_position(),
9105  exception_message(token_type::end_of_input, "value")));
9106  }
9107 
9108  // in case of an error, return discarded value
9109  if (sdp.is_errored())
9110  {
9111  result = value_t::discarded;
9112  return;
9113  }
9114  }
9115  }
9116 
9123  bool accept(const bool strict = true)
9124  {
9125  json_sax_acceptor<BasicJsonType> sax_acceptor;
9126  return sax_parse(&sax_acceptor, strict);
9127  }
9128 
9129  template <typename SAX>
9131  bool sax_parse(SAX* sax, const bool strict = true)
9132  {
9134  const bool result = sax_parse_internal(sax);
9135 
9136  // strict mode: next byte must be EOF
9137  if (result and strict and (get_token() != token_type::end_of_input))
9138  {
9139  return sax->parse_error(m_lexer.get_position(),
9140  m_lexer.get_token_string(),
9141  parse_error::create(101, m_lexer.get_position(),
9142  exception_message(token_type::end_of_input, "value")));
9143  }
9144 
9145  return result;
9146  }
9147 
9148  private:
9149  template <typename SAX>
9151  bool sax_parse_internal(SAX* sax)
9152  {
9153  // stack to remember the hierarchy of structured values we are parsing
9154  // true = array; false = object
9155  std::vector<bool> states;
9156  // value to avoid a goto (see comment where set to true)
9157  bool skip_to_state_evaluation = false;
9158 
9159  while (true)
9160  {
9161  if (not skip_to_state_evaluation)
9162  {
9163  // invariant: get_token() was called before each iteration
9164  switch (last_token)
9165  {
9166  case token_type::begin_object:
9167  {
9168  if (JSON_HEDLEY_UNLIKELY(not sax->start_object(std::size_t(-1))))
9169  {
9170  return false;
9171  }
9172 
9173  // closing } -> we are done
9174  if (get_token() == token_type::end_object)
9175  {
9176  if (JSON_HEDLEY_UNLIKELY(not sax->end_object()))
9177  {
9178  return false;
9179  }
9180  break;
9181  }
9182 
9183  // parse key
9184  if (JSON_HEDLEY_UNLIKELY(last_token != token_type::value_string))
9185  {
9186  return sax->parse_error(m_lexer.get_position(),
9187  m_lexer.get_token_string(),
9188  parse_error::create(101, m_lexer.get_position(),
9189  exception_message(token_type::value_string, "object key")));
9190  }
9191  if (JSON_HEDLEY_UNLIKELY(not sax->key(m_lexer.get_string())))
9192  {
9193  return false;
9194  }
9195 
9196  // parse separator (:)
9197  if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
9198  {
9199  return sax->parse_error(m_lexer.get_position(),
9200  m_lexer.get_token_string(),
9201  parse_error::create(101, m_lexer.get_position(),
9202  exception_message(token_type::name_separator, "object separator")));
9203  }
9204 
9205  // remember we are now inside an object
9206  states.push_back(false);
9207 
9208  // parse values
9209  get_token();
9210  continue;
9211  }
9212 
9213  case token_type::begin_array:
9214  {
9215  if (JSON_HEDLEY_UNLIKELY(not sax->start_array(std::size_t(-1))))
9216  {
9217  return false;
9218  }
9219 
9220  // closing ] -> we are done
9221  if (get_token() == token_type::end_array)
9222  {
9223  if (JSON_HEDLEY_UNLIKELY(not sax->end_array()))
9224  {
9225  return false;
9226  }
9227  break;
9228  }
9229 
9230  // remember we are now inside an array
9231  states.push_back(true);
9232 
9233  // parse values (no need to call get_token)
9234  continue;
9235  }
9236 
9237  case token_type::value_float:
9238  {
9239  const auto res = m_lexer.get_number_float();
9240 
9241  if (JSON_HEDLEY_UNLIKELY(not std::isfinite(res)))
9242  {
9243  return sax->parse_error(m_lexer.get_position(),
9244  m_lexer.get_token_string(),
9245  out_of_range::create(406, "number overflow parsing '" + m_lexer.get_token_string() + "'"));
9246  }
9247 
9248  if (JSON_HEDLEY_UNLIKELY(not sax->number_float(res, m_lexer.get_string())))
9249  {
9250  return false;
9251  }
9252 
9253  break;
9254  }
9255 
9256  case token_type::literal_false:
9257  {
9258  if (JSON_HEDLEY_UNLIKELY(not sax->boolean(false)))
9259  {
9260  return false;
9261  }
9262  break;
9263  }
9264 
9265  case token_type::literal_null:
9266  {
9267  if (JSON_HEDLEY_UNLIKELY(not sax->null()))
9268  {
9269  return false;
9270  }
9271  break;
9272  }
9273 
9274  case token_type::literal_true:
9275  {
9276  if (JSON_HEDLEY_UNLIKELY(not sax->boolean(true)))
9277  {
9278  return false;
9279  }
9280  break;
9281  }
9282 
9283  case token_type::value_integer:
9284  {
9285  if (JSON_HEDLEY_UNLIKELY(not sax->number_integer(m_lexer.get_number_integer())))
9286  {
9287  return false;
9288  }
9289  break;
9290  }
9291 
9292  case token_type::value_string:
9293  {
9294  if (JSON_HEDLEY_UNLIKELY(not sax->string(m_lexer.get_string())))
9295  {
9296  return false;
9297  }
9298  break;
9299  }
9300 
9301  case token_type::value_unsigned:
9302  {
9303  if (JSON_HEDLEY_UNLIKELY(not sax->number_unsigned(m_lexer.get_number_unsigned())))
9304  {
9305  return false;
9306  }
9307  break;
9308  }
9309 
9310  case token_type::parse_error:
9311  {
9312  // using "uninitialized" to avoid "expected" message
9313  return sax->parse_error(m_lexer.get_position(),
9314  m_lexer.get_token_string(),
9315  parse_error::create(101, m_lexer.get_position(),
9316  exception_message(token_type::uninitialized, "value")));
9317  }
9318 
9319  default: // the last token was unexpected
9320  {
9321  return sax->parse_error(m_lexer.get_position(),
9322  m_lexer.get_token_string(),
9323  parse_error::create(101, m_lexer.get_position(),
9324  exception_message(token_type::literal_or_value, "value")));
9325  }
9326  }
9327  }
9328  else
9329  {
9330  skip_to_state_evaluation = false;
9331  }
9332 
9333  // we reached this line after we successfully parsed a value
9334  if (states.empty())
9335  {
9336  // empty stack: we reached the end of the hierarchy: done
9337  return true;
9338  }
9339 
9340  if (states.back()) // array
9341  {
9342  // comma -> next value
9343  if (get_token() == token_type::value_separator)
9344  {
9345  // parse a new value
9346  get_token();
9347  continue;
9348  }
9349 
9350  // closing ]
9351  if (JSON_HEDLEY_LIKELY(last_token == token_type::end_array))
9352  {
9353  if (JSON_HEDLEY_UNLIKELY(not sax->end_array()))
9354  {
9355  return false;
9356  }
9357 
9358  // We are done with this array. Before we can parse a
9359  // new value, we need to evaluate the new state first.
9360  // By setting skip_to_state_evaluation to false, we
9361  // are effectively jumping to the beginning of this if.
9362  assert(not states.empty());
9363  states.pop_back();
9364  skip_to_state_evaluation = true;
9365  continue;
9366  }
9367 
9368  return sax->parse_error(m_lexer.get_position(),
9369  m_lexer.get_token_string(),
9370  parse_error::create(101, m_lexer.get_position(),
9371  exception_message(token_type::end_array, "array")));
9372  }
9373  else // object
9374  {
9375  // comma -> next value
9376  if (get_token() == token_type::value_separator)
9377  {
9378  // parse key
9379  if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::value_string))
9380  {
9381  return sax->parse_error(m_lexer.get_position(),
9382  m_lexer.get_token_string(),
9383  parse_error::create(101, m_lexer.get_position(),
9384  exception_message(token_type::value_string, "object key")));
9385  }
9386 
9387  if (JSON_HEDLEY_UNLIKELY(not sax->key(m_lexer.get_string())))
9388  {
9389  return false;
9390  }
9391 
9392  // parse separator (:)
9393  if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
9394  {
9395  return sax->parse_error(m_lexer.get_position(),
9396  m_lexer.get_token_string(),
9397  parse_error::create(101, m_lexer.get_position(),
9398  exception_message(token_type::name_separator, "object separator")));
9399  }
9400 
9401  // parse values
9402  get_token();
9403  continue;
9404  }
9405 
9406  // closing }
9407  if (JSON_HEDLEY_LIKELY(last_token == token_type::end_object))
9408  {
9409  if (JSON_HEDLEY_UNLIKELY(not sax->end_object()))
9410  {
9411  return false;
9412  }
9413 
9414  // We are done with this object. Before we can parse a
9415  // new value, we need to evaluate the new state first.
9416  // By setting skip_to_state_evaluation to false, we
9417  // are effectively jumping to the beginning of this if.
9418  assert(not states.empty());
9419  states.pop_back();
9420  skip_to_state_evaluation = true;
9421  continue;
9422  }
9423 
9424  return sax->parse_error(m_lexer.get_position(),
9425  m_lexer.get_token_string(),
9426  parse_error::create(101, m_lexer.get_position(),
9427  exception_message(token_type::end_object, "object")));
9428  }
9429  }
9430  }
9431 
9433  token_type get_token()
9434  {
9435  return last_token = m_lexer.scan();
9436  }
9437 
9438  std::string exception_message(const token_type expected, const std::string& context)
9439  {
9440  std::string error_msg = "syntax error ";
9441 
9442  if (not context.empty())
9443  {
9444  error_msg += "while parsing " + context + " ";
9445  }
9446 
9447  error_msg += "- ";
9448 
9449  if (last_token == token_type::parse_error)
9450  {
9451  error_msg += std::string(m_lexer.get_error_message()) + "; last read: '" +
9452  m_lexer.get_token_string() + "'";
9453  }
9454  else
9455  {
9456  error_msg += "unexpected " + std::string(lexer_t::token_type_name(last_token));
9457  }
9458 
9459  if (expected != token_type::uninitialized)
9460  {
9461  error_msg += "; expected " + std::string(lexer_t::token_type_name(expected));
9462  }
9463 
9464  return error_msg;
9465  }
9466 
9467  private:
9469  const parser_callback_t callback = nullptr;
9471  token_type last_token = token_type::uninitialized;
9475  const bool allow_exceptions = true;
9476 };
9477 } // namespace detail
9478 } // namespace nlohmann
9479 
9480 // #include <nlohmann/detail/iterators/internal_iterator.hpp>
9481 
9482 
9483 // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
9484 
9485 
9486 #include <cstddef> // ptrdiff_t
9487 #include <limits> // numeric_limits
9488 
9489 namespace nlohmann
9490 {
9491 namespace detail
9492 {
9493 /*
9494 @brief an iterator for primitive JSON types
9495 
9496 This class models an iterator for primitive JSON types (boolean, number,
9497 string). It's only purpose is to allow the iterator/const_iterator classes
9498 to "iterate" over primitive values. Internally, the iterator is modeled by
9499 a `difference_type` variable. Value begin_value (`0`) models the begin,
9500 end_value (`1`) models past the end.
9501 */
9503 {
9504  private:
9505  using difference_type = std::ptrdiff_t;
9506  static constexpr difference_type begin_value = 0;
9507  static constexpr difference_type end_value = begin_value + 1;
9508 
9511 
9512  public:
9513  constexpr difference_type get_value() const noexcept
9514  {
9515  return m_it;
9516  }
9517 
9519  void set_begin() noexcept
9520  {
9521  m_it = begin_value;
9522  }
9523 
9525  void set_end() noexcept
9526  {
9527  m_it = end_value;
9528  }
9529 
9531  constexpr bool is_begin() const noexcept
9532  {
9533  return m_it == begin_value;
9534  }
9535 
9537  constexpr bool is_end() const noexcept
9538  {
9539  return m_it == end_value;
9540  }
9541 
9542  friend constexpr bool operator==(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
9543  {
9544  return lhs.m_it == rhs.m_it;
9545  }
9546 
9547  friend constexpr bool operator<(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
9548  {
9549  return lhs.m_it < rhs.m_it;
9550  }
9551 
9553  {
9554  auto result = *this;
9555  result += n;
9556  return result;
9557  }
9558 
9560  {
9561  return lhs.m_it - rhs.m_it;
9562  }
9563 
9564  primitive_iterator_t& operator++() noexcept
9565  {
9566  ++m_it;
9567  return *this;
9568  }
9569 
9570  primitive_iterator_t const operator++(int) noexcept
9571  {
9572  auto result = *this;
9573  ++m_it;
9574  return result;
9575  }
9576 
9577  primitive_iterator_t& operator--() noexcept
9578  {
9579  --m_it;
9580  return *this;
9581  }
9582 
9583  primitive_iterator_t const operator--(int) noexcept
9584  {
9585  auto result = *this;
9586  --m_it;
9587  return result;
9588  }
9589 
9591  {
9592  m_it += n;
9593  return *this;
9594  }
9595 
9597  {
9598  m_it -= n;
9599  return *this;
9600  }
9601 };
9602 } // namespace detail
9603 } // namespace nlohmann
9604 
9605 
9606 namespace nlohmann
9607 {
9608 namespace detail
9609 {
9616 template<typename BasicJsonType> struct internal_iterator
9617 {
9619  typename BasicJsonType::object_t::iterator object_iterator {};
9621  typename BasicJsonType::array_t::iterator array_iterator {};
9623  primitive_iterator_t primitive_iterator {};
9624 };
9625 } // namespace detail
9626 } // namespace nlohmann
9627 
9628 // #include <nlohmann/detail/iterators/iter_impl.hpp>
9629 
9630 
9631 #include <ciso646> // not
9632 #include <iterator> // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next
9633 #include <type_traits> // conditional, is_const, remove_const
9634 
9635 // #include <nlohmann/detail/exceptions.hpp>
9636 
9637 // #include <nlohmann/detail/iterators/internal_iterator.hpp>
9638 
9639 // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
9640 
9641 // #include <nlohmann/detail/macro_scope.hpp>
9642 
9643 // #include <nlohmann/detail/meta/cpp_future.hpp>
9644 
9645 // #include <nlohmann/detail/meta/type_traits.hpp>
9646 
9647 // #include <nlohmann/detail/value_t.hpp>
9648 
9649 
9650 namespace nlohmann
9651 {
9652 namespace detail
9653 {
9654 // forward declare, to be able to friend it later on
9655 template<typename IteratorType> class iteration_proxy;
9656 template<typename IteratorType> class iteration_proxy_value;
9657 
9674 template<typename BasicJsonType>
9676 {
9678  friend iter_impl<typename std::conditional<std::is_const<BasicJsonType>::value, typename std::remove_const<BasicJsonType>::type, const BasicJsonType>::type>;
9682 
9683  using object_t = typename BasicJsonType::object_t;
9684  using array_t = typename BasicJsonType::array_t;
9685  // make sure BasicJsonType is basic_json or const basic_json
9686  static_assert(is_basic_json<typename std::remove_const<BasicJsonType>::type>::value,
9687  "iter_impl only accepts (const) basic_json");
9688 
9689  public:
9690 
9696  using iterator_category = std::bidirectional_iterator_tag;
9697 
9701  using difference_type = typename BasicJsonType::difference_type;
9704  typename BasicJsonType::const_pointer,
9705  typename BasicJsonType::pointer>::type;
9707  using reference =
9709  typename BasicJsonType::const_reference,
9710  typename BasicJsonType::reference>::type;
9711 
9713  iter_impl() = default;
9714 
9721  explicit iter_impl(pointer object) noexcept : m_object(object)
9722  {
9723  assert(m_object != nullptr);
9724 
9725  switch (m_object->m_type)
9726  {
9727  case value_t::object:
9728  {
9729  m_it.object_iterator = typename object_t::iterator();
9730  break;
9731  }
9732 
9733  case value_t::array:
9734  {
9735  m_it.array_iterator = typename array_t::iterator();
9736  break;
9737  }
9738 
9739  default:
9740  {
9741  m_it.primitive_iterator = primitive_iterator_t();
9742  break;
9743  }
9744  }
9745  }
9746 
9764  : m_object(other.m_object), m_it(other.m_it)
9765  {}
9766 
9773  iter_impl& operator=(const iter_impl<const BasicJsonType>& other) noexcept
9774  {
9775  m_object = other.m_object;
9776  m_it = other.m_it;
9777  return *this;
9778  }
9779 
9785  iter_impl(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept
9786  : m_object(other.m_object), m_it(other.m_it)
9787  {}
9788 
9795  iter_impl& operator=(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept
9796  {
9797  m_object = other.m_object;
9798  m_it = other.m_it;
9799  return *this;
9800  }
9801 
9802  private:
9807  void set_begin() noexcept
9808  {
9809  assert(m_object != nullptr);
9810 
9811  switch (m_object->m_type)
9812  {
9813  case value_t::object:
9814  {
9815  m_it.object_iterator = m_object->m_value.object->begin();
9816  break;
9817  }
9818 
9819  case value_t::array:
9820  {
9821  m_it.array_iterator = m_object->m_value.array->begin();
9822  break;
9823  }
9824 
9825  case value_t::null:
9826  {
9827  // set to end so begin()==end() is true: null is empty
9828  m_it.primitive_iterator.set_end();
9829  break;
9830  }
9831 
9832  default:
9833  {
9834  m_it.primitive_iterator.set_begin();
9835  break;
9836  }
9837  }
9838  }
9839 
9844  void set_end() noexcept
9845  {
9846  assert(m_object != nullptr);
9847 
9848  switch (m_object->m_type)
9849  {
9850  case value_t::object:
9851  {
9852  m_it.object_iterator = m_object->m_value.object->end();
9853  break;
9854  }
9855 
9856  case value_t::array:
9857  {
9858  m_it.array_iterator = m_object->m_value.array->end();
9859  break;
9860  }
9861 
9862  default:
9863  {
9864  m_it.primitive_iterator.set_end();
9865  break;
9866  }
9867  }
9868  }
9869 
9870  public:
9876  {
9877  assert(m_object != nullptr);
9878 
9879  switch (m_object->m_type)
9880  {
9881  case value_t::object:
9882  {
9883  assert(m_it.object_iterator != m_object->m_value.object->end());
9884  return m_it.object_iterator->second;
9885  }
9886 
9887  case value_t::array:
9888  {
9889  assert(m_it.array_iterator != m_object->m_value.array->end());
9890  return *m_it.array_iterator;
9891  }
9892 
9893  case value_t::null:
9894  JSON_THROW(invalid_iterator::create(214, "cannot get value"));
9895 
9896  default:
9897  {
9898  if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
9899  {
9900  return *m_object;
9901  }
9902 
9903  JSON_THROW(invalid_iterator::create(214, "cannot get value"));
9904  }
9905  }
9906  }
9907 
9912  pointer operator->() const
9913  {
9914  assert(m_object != nullptr);
9915 
9916  switch (m_object->m_type)
9917  {
9918  case value_t::object:
9919  {
9920  assert(m_it.object_iterator != m_object->m_value.object->end());
9921  return &(m_it.object_iterator->second);
9922  }
9923 
9924  case value_t::array:
9925  {
9926  assert(m_it.array_iterator != m_object->m_value.array->end());
9927  return &*m_it.array_iterator;
9928  }
9929 
9930  default:
9931  {
9932  if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
9933  {
9934  return m_object;
9935  }
9936 
9937  JSON_THROW(invalid_iterator::create(214, "cannot get value"));
9938  }
9939  }
9940  }
9941 
9946  iter_impl const operator++(int)
9947  {
9948  auto result = *this;
9949  ++(*this);
9950  return result;
9951  }
9952 
9957  iter_impl& operator++()
9958  {
9959  assert(m_object != nullptr);
9960 
9961  switch (m_object->m_type)
9962  {
9963  case value_t::object:
9964  {
9965  std::advance(m_it.object_iterator, 1);
9966  break;
9967  }
9968 
9969  case value_t::array:
9970  {
9971  std::advance(m_it.array_iterator, 1);
9972  break;
9973  }
9974 
9975  default:
9976  {
9977  ++m_it.primitive_iterator;
9978  break;
9979  }
9980  }
9981 
9982  return *this;
9983  }
9984 
9989  iter_impl const operator--(int)
9990  {
9991  auto result = *this;
9992  --(*this);
9993  return result;
9994  }
9995 
10000  iter_impl& operator--()
10001  {
10002  assert(m_object != nullptr);
10003 
10004  switch (m_object->m_type)
10005  {
10006  case value_t::object:
10007  {
10008  std::advance(m_it.object_iterator, -1);
10009  break;
10010  }
10011 
10012  case value_t::array:
10013  {
10014  std::advance(m_it.array_iterator, -1);
10015  break;
10016  }
10017 
10018  default:
10019  {
10020  --m_it.primitive_iterator;
10021  break;
10022  }
10023  }
10024 
10025  return *this;
10026  }
10027 
10032  bool operator==(const iter_impl& other) const
10033  {
10034  // if objects are not the same, the comparison is undefined
10035  if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
10036  {
10037  JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers"));
10038  }
10039 
10040  assert(m_object != nullptr);
10041 
10042  switch (m_object->m_type)
10043  {
10044  case value_t::object:
10045  return (m_it.object_iterator == other.m_it.object_iterator);
10046 
10047  case value_t::array:
10048  return (m_it.array_iterator == other.m_it.array_iterator);
10049 
10050  default:
10051  return (m_it.primitive_iterator == other.m_it.primitive_iterator);
10052  }
10053  }
10054 
10059  bool operator!=(const iter_impl& other) const
10060  {
10061  return not operator==(other);
10062  }
10063 
10068  bool operator<(const iter_impl& other) const
10069  {
10070  // if objects are not the same, the comparison is undefined
10071  if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
10072  {
10073  JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers"));
10074  }
10075 
10076  assert(m_object != nullptr);
10077 
10078  switch (m_object->m_type)
10079  {
10080  case value_t::object:
10081  JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators"));
10082 
10083  case value_t::array:
10084  return (m_it.array_iterator < other.m_it.array_iterator);
10085 
10086  default:
10087  return (m_it.primitive_iterator < other.m_it.primitive_iterator);
10088  }
10089  }
10090 
10095  bool operator<=(const iter_impl& other) const
10096  {
10097  return not other.operator < (*this);
10098  }
10099 
10104  bool operator>(const iter_impl& other) const
10105  {
10106  return not operator<=(other);
10107  }
10108 
10113  bool operator>=(const iter_impl& other) const
10114  {
10115  return not operator<(other);
10116  }
10117 
10123  {
10124  assert(m_object != nullptr);
10125 
10126  switch (m_object->m_type)
10127  {
10128  case value_t::object:
10129  JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators"));
10130 
10131  case value_t::array:
10132  {
10133  std::advance(m_it.array_iterator, i);
10134  break;
10135  }
10136 
10137  default:
10138  {
10139  m_it.primitive_iterator += i;
10140  break;
10141  }
10142  }
10143 
10144  return *this;
10145  }
10146 
10152  {
10153  return operator+=(-i);
10154  }
10155 
10161  {
10162  auto result = *this;
10163  result += i;
10164  return result;
10165  }
10166 
10172  {
10173  auto result = it;
10174  result += i;
10175  return result;
10176  }
10177 
10183  {
10184  auto result = *this;
10185  result -= i;
10186  return result;
10187  }
10188 
10194  {
10195  assert(m_object != nullptr);
10196 
10197  switch (m_object->m_type)
10198  {
10199  case value_t::object:
10200  JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators"));
10201 
10202  case value_t::array:
10203  return m_it.array_iterator - other.m_it.array_iterator;
10204 
10205  default:
10206  return m_it.primitive_iterator - other.m_it.primitive_iterator;
10207  }
10208  }
10209 
10214  reference operator[](difference_type n) const
10215  {
10216  assert(m_object != nullptr);
10217 
10218  switch (m_object->m_type)
10219  {
10220  case value_t::object:
10221  JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators"));
10222 
10223  case value_t::array:
10224  return *std::next(m_it.array_iterator, n);
10225 
10226  case value_t::null:
10227  JSON_THROW(invalid_iterator::create(214, "cannot get value"));
10228 
10229  default:
10230  {
10231  if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.get_value() == -n))
10232  {
10233  return *m_object;
10234  }
10235 
10236  JSON_THROW(invalid_iterator::create(214, "cannot get value"));
10237  }
10238  }
10239  }
10240 
10245  const typename object_t::key_type& key() const
10246  {
10247  assert(m_object != nullptr);
10248 
10249  if (JSON_HEDLEY_LIKELY(m_object->is_object()))
10250  {
10251  return m_it.object_iterator->first;
10252  }
10253 
10254  JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators"));
10255  }
10256 
10262  {
10263  return operator*();
10264  }
10265 
10266  private:
10268  pointer m_object = nullptr;
10271 };
10272 } // namespace detail
10273 } // namespace nlohmann
10274 
10275 // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
10276 
10277 // #include <nlohmann/detail/iterators/json_reverse_iterator.hpp>
10278 
10279 
10280 #include <cstddef> // ptrdiff_t
10281 #include <iterator> // reverse_iterator
10282 #include <utility> // declval
10283 
10284 namespace nlohmann
10285 {
10286 namespace detail
10287 {
10289 // reverse_iterator //
10291 
10310 template<typename Base>
10311 class json_reverse_iterator : public std::reverse_iterator<Base>
10312 {
10313  public:
10314  using difference_type = std::ptrdiff_t;
10316  using base_iterator = std::reverse_iterator<Base>;
10318  using reference = typename Base::reference;
10319 
10321  explicit json_reverse_iterator(const typename base_iterator::iterator_type& it) noexcept
10322  : base_iterator(it) {}
10323 
10325  explicit json_reverse_iterator(const base_iterator& it) noexcept : base_iterator(it) {}
10326 
10328  json_reverse_iterator const operator++(int)
10329  {
10330  return static_cast<json_reverse_iterator>(base_iterator::operator++(1));
10331  }
10332 
10335  {
10336  return static_cast<json_reverse_iterator&>(base_iterator::operator++());
10337  }
10338 
10340  json_reverse_iterator const operator--(int)
10341  {
10342  return static_cast<json_reverse_iterator>(base_iterator::operator--(1));
10343  }
10344 
10347  {
10348  return static_cast<json_reverse_iterator&>(base_iterator::operator--());
10349  }
10350 
10353  {
10354  return static_cast<json_reverse_iterator&>(base_iterator::operator+=(i));
10355  }
10356 
10359  {
10360  return static_cast<json_reverse_iterator>(base_iterator::operator+(i));
10361  }
10362 
10365  {
10366  return static_cast<json_reverse_iterator>(base_iterator::operator-(i));
10367  }
10368 
10371  {
10372  return base_iterator(*this) - base_iterator(other);
10373  }
10374 
10376  reference operator[](difference_type n) const
10377  {
10378  return *(this->operator+(n));
10379  }
10380 
10382  auto key() const -> decltype(std::declval<Base>().key())
10383  {
10384  auto it = --this->base();
10385  return it.key();
10386  }
10387 
10390  {
10391  auto it = --this->base();
10392  return it.operator * ();
10393  }
10394 };
10395 } // namespace detail
10396 } // namespace nlohmann
10397 
10398 // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
10399 
10400 // #include <nlohmann/detail/json_pointer.hpp>
10401 
10402 
10403 #include <algorithm> // all_of
10404 #include <cassert> // assert
10405 #include <cctype> // isdigit
10406 #include <numeric> // accumulate
10407 #include <string> // string
10408 #include <utility> // move
10409 #include <vector> // vector
10410 
10411 // #include <nlohmann/detail/exceptions.hpp>
10412 
10413 // #include <nlohmann/detail/macro_scope.hpp>
10414 
10415 // #include <nlohmann/detail/value_t.hpp>
10416 
10417 
10418 namespace nlohmann
10419 {
10420 template<typename BasicJsonType>
10422 {
10423  // allow basic_json to access private members
10425  friend class basic_json;
10426 
10427  public:
10449  explicit json_pointer(const std::string& s = "")
10450  : reference_tokens(split(s))
10451  {}
10452 
10467  std::string to_string() const
10468  {
10469  return std::accumulate(reference_tokens.begin(), reference_tokens.end(),
10470  std::string{},
10471  [](const std::string & a, const std::string & b)
10472  {
10473  return a + "/" + escape(b);
10474  });
10475  }
10476 
10478  operator std::string() const
10479  {
10480  return to_string();
10481  }
10482 
10499  json_pointer& operator/=(const json_pointer& ptr)
10500  {
10501  reference_tokens.insert(reference_tokens.end(),
10502  ptr.reference_tokens.begin(),
10503  ptr.reference_tokens.end());
10504  return *this;
10505  }
10506 
10523  json_pointer& operator/=(std::string token)
10524  {
10525  push_back(std::move(token));
10526  return *this;
10527  }
10528 
10545  json_pointer& operator/=(std::size_t array_idx)
10546  {
10547  return *this /= std::to_string(array_idx);
10548  }
10549 
10566  const json_pointer& rhs)
10567  {
10568  return json_pointer(lhs) /= rhs;
10569  }
10570 
10586  friend json_pointer operator/(const json_pointer& ptr, std::string token)
10587  {
10588  return json_pointer(ptr) /= std::move(token);
10589  }
10590 
10606  friend json_pointer operator/(const json_pointer& ptr, std::size_t array_idx)
10607  {
10608  return json_pointer(ptr) /= array_idx;
10609  }
10610 
10624  json_pointer parent_pointer() const
10625  {
10626  if (empty())
10627  {
10628  return *this;
10629  }
10630 
10631  json_pointer res = *this;
10632  res.pop_back();
10633  return res;
10634  }
10635 
10649  void pop_back()
10650  {
10651  if (JSON_HEDLEY_UNLIKELY(empty()))
10652  {
10653  JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent"));
10654  }
10655 
10656  reference_tokens.pop_back();
10657  }
10658 
10673  const std::string& back() const
10674  {
10675  if (JSON_HEDLEY_UNLIKELY(empty()))
10676  {
10677  JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent"));
10678  }
10679 
10680  return reference_tokens.back();
10681  }
10682 
10695  void push_back(const std::string& token)
10696  {
10697  reference_tokens.push_back(token);
10698  }
10699 
10701  void push_back(std::string&& token)
10702  {
10703  reference_tokens.push_back(std::move(token));
10704  }
10705 
10720  bool empty() const noexcept
10721  {
10722  return reference_tokens.empty();
10723  }
10724 
10725  private:
10733  static int array_index(const std::string& s)
10734  {
10735  std::size_t processed_chars = 0;
10736  const int res = std::stoi(s, &processed_chars);
10737 
10738  // check if the string was completely read
10739  if (JSON_HEDLEY_UNLIKELY(processed_chars != s.size()))
10740  {
10741  JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'"));
10742  }
10743 
10744  return res;
10745  }
10746 
10747  json_pointer top() const
10748  {
10749  if (JSON_HEDLEY_UNLIKELY(empty()))
10750  {
10751  JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent"));
10752  }
10753 
10754  json_pointer result = *this;
10755  result.reference_tokens = {reference_tokens[0]};
10756  return result;
10757  }
10758 
10767  BasicJsonType& get_and_create(BasicJsonType& j) const
10768  {
10769  using size_type = typename BasicJsonType::size_type;
10770  auto result = &j;
10771 
10772  // in case no reference tokens exist, return a reference to the JSON value
10773  // j which will be overwritten by a primitive value
10774  for (const auto& reference_token : reference_tokens)
10775  {
10776  switch (result->type())
10777  {
10778  case detail::value_t::null:
10779  {
10780  if (reference_token == "0")
10781  {
10782  // start a new array if reference token is 0
10783  result = &result->operator[](0);
10784  }
10785  else
10786  {
10787  // start a new object otherwise
10788  result = &result->operator[](reference_token);
10789  }
10790  break;
10791  }
10792 
10793  case detail::value_t::object:
10794  {
10795  // create an entry in the object
10796  result = &result->operator[](reference_token);
10797  break;
10798  }
10799 
10800  case detail::value_t::array:
10801  {
10802  // create an entry in the array
10803  JSON_TRY
10804  {
10805  result = &result->operator[](static_cast<size_type>(array_index(reference_token)));
10806  }
10807  JSON_CATCH(std::invalid_argument&)
10808  {
10809  JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number"));
10810  }
10811  break;
10812  }
10813 
10814  /*
10815  The following code is only reached if there exists a reference
10816  token _and_ the current value is primitive. In this case, we have
10817  an error situation, because primitive values may only occur as
10818  single value; that is, with an empty list of reference tokens.
10819  */
10820  default:
10821  JSON_THROW(detail::type_error::create(313, "invalid value to unflatten"));
10822  }
10823  }
10824 
10825  return *result;
10826  }
10827 
10847  BasicJsonType& get_unchecked(BasicJsonType* ptr) const
10848  {
10849  using size_type = typename BasicJsonType::size_type;
10850  for (const auto& reference_token : reference_tokens)
10851  {
10852  // convert null values to arrays or objects before continuing
10853  if (ptr->is_null())
10854  {
10855  // check if reference token is a number
10856  const bool nums =
10857  std::all_of(reference_token.begin(), reference_token.end(),
10858  [](const unsigned char x)
10859  {
10860  return std::isdigit(x);
10861  });
10862 
10863  // change value to array for numbers or "-" or to object otherwise
10864  *ptr = (nums or reference_token == "-")
10865  ? detail::value_t::array
10866  : detail::value_t::object;
10867  }
10868 
10869  switch (ptr->type())
10870  {
10871  case detail::value_t::object:
10872  {
10873  // use unchecked object access
10874  ptr = &ptr->operator[](reference_token);
10875  break;
10876  }
10877 
10878  case detail::value_t::array:
10879  {
10880  // error condition (cf. RFC 6901, Sect. 4)
10881  if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
10882  {
10884  "array index '" + reference_token +
10885  "' must not begin with '0'"));
10886  }
10887 
10888  if (reference_token == "-")
10889  {
10890  // explicitly treat "-" as index beyond the end
10891  ptr = &ptr->operator[](ptr->m_value.array->size());
10892  }
10893  else
10894  {
10895  // convert array index to number; unchecked access
10896  JSON_TRY
10897  {
10898  ptr = &ptr->operator[](
10899  static_cast<size_type>(array_index(reference_token)));
10900  }
10901  JSON_CATCH(std::invalid_argument&)
10902  {
10903  JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number"));
10904  }
10905  }
10906  break;
10907  }
10908 
10909  default:
10910  JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'"));
10911  }
10912  }
10913 
10914  return *ptr;
10915  }
10916 
10923  BasicJsonType& get_checked(BasicJsonType* ptr) const
10924  {
10925  using size_type = typename BasicJsonType::size_type;
10926  for (const auto& reference_token : reference_tokens)
10927  {
10928  switch (ptr->type())
10929  {
10930  case detail::value_t::object:
10931  {
10932  // note: at performs range check
10933  ptr = &ptr->at(reference_token);
10934  break;
10935  }
10936 
10937  case detail::value_t::array:
10938  {
10939  if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
10940  {
10941  // "-" always fails the range check
10943  "array index '-' (" + std::to_string(ptr->m_value.array->size()) +
10944  ") is out of range"));
10945  }
10946 
10947  // error condition (cf. RFC 6901, Sect. 4)
10948  if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
10949  {
10951  "array index '" + reference_token +
10952  "' must not begin with '0'"));
10953  }
10954 
10955  // note: at performs range check
10956  JSON_TRY
10957  {
10958  ptr = &ptr->at(static_cast<size_type>(array_index(reference_token)));
10959  }
10960  JSON_CATCH(std::invalid_argument&)
10961  {
10962  JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number"));
10963  }
10964  break;
10965  }
10966 
10967  default:
10968  JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'"));
10969  }
10970  }
10971 
10972  return *ptr;
10973  }
10974 
10988  const BasicJsonType& get_unchecked(const BasicJsonType* ptr) const
10989  {
10990  using size_type = typename BasicJsonType::size_type;
10991  for (const auto& reference_token : reference_tokens)
10992  {
10993  switch (ptr->type())
10994  {
10995  case detail::value_t::object:
10996  {
10997  // use unchecked object access
10998  ptr = &ptr->operator[](reference_token);
10999  break;
11000  }
11001 
11002  case detail::value_t::array:
11003  {
11004  if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
11005  {
11006  // "-" cannot be used for const access
11008  "array index '-' (" + std::to_string(ptr->m_value.array->size()) +
11009  ") is out of range"));
11010  }
11011 
11012  // error condition (cf. RFC 6901, Sect. 4)
11013  if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
11014  {
11016  "array index '" + reference_token +
11017  "' must not begin with '0'"));
11018  }
11019 
11020  // use unchecked array access
11021  JSON_TRY
11022  {
11023  ptr = &ptr->operator[](
11024  static_cast<size_type>(array_index(reference_token)));
11025  }
11026  JSON_CATCH(std::invalid_argument&)
11027  {
11028  JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number"));
11029  }
11030  break;
11031  }
11032 
11033  default:
11034  JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'"));
11035  }
11036  }
11037 
11038  return *ptr;
11039  }
11040 
11047  const BasicJsonType& get_checked(const BasicJsonType* ptr) const
11048  {
11049  using size_type = typename BasicJsonType::size_type;
11050  for (const auto& reference_token : reference_tokens)
11051  {
11052  switch (ptr->type())
11053  {
11054  case detail::value_t::object:
11055  {
11056  // note: at performs range check
11057  ptr = &ptr->at(reference_token);
11058  break;
11059  }
11060 
11061  case detail::value_t::array:
11062  {
11063  if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
11064  {
11065  // "-" always fails the range check
11067  "array index '-' (" + std::to_string(ptr->m_value.array->size()) +
11068  ") is out of range"));
11069  }
11070 
11071  // error condition (cf. RFC 6901, Sect. 4)
11072  if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
11073  {
11075  "array index '" + reference_token +
11076  "' must not begin with '0'"));
11077  }
11078 
11079  // note: at performs range check
11080  JSON_TRY
11081  {
11082  ptr = &ptr->at(static_cast<size_type>(array_index(reference_token)));
11083  }
11084  JSON_CATCH(std::invalid_argument&)
11085  {
11086  JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number"));
11087  }
11088  break;
11089  }
11090 
11091  default:
11092  JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'"));
11093  }
11094  }
11095 
11096  return *ptr;
11097  }
11098 
11103  bool contains(const BasicJsonType* ptr) const
11104  {
11105  using size_type = typename BasicJsonType::size_type;
11106  for (const auto& reference_token : reference_tokens)
11107  {
11108  switch (ptr->type())
11109  {
11110  case detail::value_t::object:
11111  {
11112  if (not ptr->contains(reference_token))
11113  {
11114  // we did not find the key in the object
11115  return false;
11116  }
11117 
11118  ptr = &ptr->operator[](reference_token);
11119  break;
11120  }
11121 
11122  case detail::value_t::array:
11123  {
11124  if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
11125  {
11126  // "-" always fails the range check
11127  return false;
11128  }
11129 
11130  // error condition (cf. RFC 6901, Sect. 4)
11131  if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
11132  {
11134  "array index '" + reference_token +
11135  "' must not begin with '0'"));
11136  }
11137 
11138  JSON_TRY
11139  {
11140  const auto idx = static_cast<size_type>(array_index(reference_token));
11141  if (idx >= ptr->size())
11142  {
11143  // index out of range
11144  return false;
11145  }
11146 
11147  ptr = &ptr->operator[](idx);
11148  break;
11149  }
11150  JSON_CATCH(std::invalid_argument&)
11151  {
11152  JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number"));
11153  }
11154  break;
11155  }
11156 
11157  default:
11158  {
11159  // we do not expect primitive values if there is still a
11160  // reference token to process
11161  return false;
11162  }
11163  }
11164  }
11165 
11166  // no reference token left means we found a primitive value
11167  return true;
11168  }
11169 
11179  static std::vector<std::string> split(const std::string& reference_string)
11180  {
11181  std::vector<std::string> result;
11182 
11183  // special case: empty reference string -> no reference tokens
11184  if (reference_string.empty())
11185  {
11186  return result;
11187  }
11188 
11189  // check if nonempty reference string begins with slash
11190  if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/'))
11191  {
11193  "JSON pointer must be empty or begin with '/' - was: '" +
11194  reference_string + "'"));
11195  }
11196 
11197  // extract the reference tokens:
11198  // - slash: position of the last read slash (or end of string)
11199  // - start: position after the previous slash
11200  for (
11201  // search for the first slash after the first character
11202  std::size_t slash = reference_string.find_first_of('/', 1),
11203  // set the beginning of the first reference token
11204  start = 1;
11205  // we can stop if start == 0 (if slash == std::string::npos)
11206  start != 0;
11207  // set the beginning of the next reference token
11208  // (will eventually be 0 if slash == std::string::npos)
11209  start = (slash == std::string::npos) ? 0 : slash + 1,
11210  // find next slash
11211  slash = reference_string.find_first_of('/', start))
11212  {
11213  // use the text between the beginning of the reference token
11214  // (start) and the last slash (slash).
11215  auto reference_token = reference_string.substr(start, slash - start);
11216 
11217  // check reference tokens are properly escaped
11218  for (std::size_t pos = reference_token.find_first_of('~');
11219  pos != std::string::npos;
11220  pos = reference_token.find_first_of('~', pos + 1))
11221  {
11222  assert(reference_token[pos] == '~');
11223 
11224  // ~ must be followed by 0 or 1
11225  if (JSON_HEDLEY_UNLIKELY(pos == reference_token.size() - 1 or
11226  (reference_token[pos + 1] != '0' and
11227  reference_token[pos + 1] != '1')))
11228  {
11229  JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'"));
11230  }
11231  }
11232 
11233  // finally, store the reference token
11234  unescape(reference_token);
11235  result.push_back(reference_token);
11236  }
11237 
11238  return result;
11239  }
11240 
11254  static void replace_substring(std::string& s, const std::string& f,
11255  const std::string& t)
11256  {
11257  assert(not f.empty());
11258  for (auto pos = s.find(f); // find first occurrence of f
11259  pos != std::string::npos; // make sure f was found
11260  s.replace(pos, f.size(), t), // replace with t, and
11261  pos = s.find(f, pos + t.size())) // find next occurrence of f
11262  {}
11263  }
11264 
11266  static std::string escape(std::string s)
11267  {
11268  replace_substring(s, "~", "~0");
11269  replace_substring(s, "/", "~1");
11270  return s;
11271  }
11272 
11274  static void unescape(std::string& s)
11275  {
11276  replace_substring(s, "~1", "/");
11277  replace_substring(s, "~0", "~");
11278  }
11279 
11287  static void flatten(const std::string& reference_string,
11288  const BasicJsonType& value,
11289  BasicJsonType& result)
11290  {
11291  switch (value.type())
11292  {
11293  case detail::value_t::array:
11294  {
11295  if (value.m_value.array->empty())
11296  {
11297  // flatten empty array as null
11298  result[reference_string] = nullptr;
11299  }
11300  else
11301  {
11302  // iterate array and use index as reference string
11303  for (std::size_t i = 0; i < value.m_value.array->size(); ++i)
11304  {
11305  flatten(reference_string + "/" + std::to_string(i),
11306  value.m_value.array->operator[](i), result);
11307  }
11308  }
11309  break;
11310  }
11311 
11312  case detail::value_t::object:
11313  {
11314  if (value.m_value.object->empty())
11315  {
11316  // flatten empty object as null
11317  result[reference_string] = nullptr;
11318  }
11319  else
11320  {
11321  // iterate object and use keys as reference string
11322  for (const auto& element : *value.m_value.object)
11323  {
11324  flatten(reference_string + "/" + escape(element.first), element.second, result);
11325  }
11326  }
11327  break;
11328  }
11329 
11330  default:
11331  {
11332  // add primitive value with its reference string
11333  result[reference_string] = value;
11334  break;
11335  }
11336  }
11337  }
11338 
11349  static BasicJsonType
11350  unflatten(const BasicJsonType& value)
11351  {
11352  if (JSON_HEDLEY_UNLIKELY(not value.is_object()))
11353  {
11354  JSON_THROW(detail::type_error::create(314, "only objects can be unflattened"));
11355  }
11356 
11357  BasicJsonType result;
11358 
11359  // iterate the JSON object values
11360  for (const auto& element : *value.m_value.object)
11361  {
11362  if (JSON_HEDLEY_UNLIKELY(not element.second.is_primitive()))
11363  {
11364  JSON_THROW(detail::type_error::create(315, "values in object must be primitive"));
11365  }
11366 
11367  // assign value to reference pointed to by JSON pointer; Note that if
11368  // the JSON pointer is "" (i.e., points to the whole value), function
11369  // get_and_create returns a reference to result itself. An assignment
11370  // will then create a primitive value.
11371  json_pointer(element.first).get_and_create(result) = element.second;
11372  }
11373 
11374  return result;
11375  }
11376 
11388  friend bool operator==(json_pointer const& lhs,
11389  json_pointer const& rhs) noexcept
11390  {
11391  return lhs.reference_tokens == rhs.reference_tokens;
11392  }
11393 
11405  friend bool operator!=(json_pointer const& lhs,
11406  json_pointer const& rhs) noexcept
11407  {
11408  return not (lhs == rhs);
11409  }
11410 
11412  std::vector<std::string> reference_tokens;
11413 };
11414 } // namespace nlohmann
11415 
11416 // #include <nlohmann/detail/json_ref.hpp>
11417 
11418 
11419 #include <initializer_list>
11420 #include <utility>
11421 
11422 // #include <nlohmann/detail/meta/type_traits.hpp>
11423 
11424 
11425 namespace nlohmann
11426 {
11427 namespace detail
11428 {
11429 template<typename BasicJsonType>
11431 {
11432  public:
11433  using value_type = BasicJsonType;
11434 
11436  : owned_value(std::move(value)), value_ref(&owned_value), is_rvalue(true)
11437  {}
11438 
11440  : value_ref(const_cast<value_type*>(&value)), is_rvalue(false)
11441  {}
11442 
11443  json_ref(std::initializer_list<json_ref> init)
11444  : owned_value(init), value_ref(&owned_value), is_rvalue(true)
11445  {}
11446 
11447  template <
11448  class... Args,
11449  enable_if_t<std::is_constructible<value_type, Args...>::value, int> = 0 >
11450  json_ref(Args && ... args)
11451  : owned_value(std::forward<Args>(args)...), value_ref(&owned_value),
11452  is_rvalue(true) {}
11453 
11454  // class should be movable only
11455  json_ref(json_ref&&) = default;
11456  json_ref(const json_ref&) = delete;
11457  json_ref& operator=(const json_ref&) = delete;
11458  json_ref& operator=(json_ref&&) = delete;
11459  ~json_ref() = default;
11460 
11461  value_type moved_or_copied() const
11462  {
11463  if (is_rvalue)
11464  {
11465  return std::move(*value_ref);
11466  }
11467  return *value_ref;
11468  }
11469 
11470  value_type const& operator*() const
11471  {
11472  return *static_cast<value_type const*>(value_ref);
11473  }
11474 
11475  value_type const* operator->() const
11476  {
11477  return static_cast<value_type const*>(value_ref);
11478  }
11479 
11480  private:
11481  mutable value_type owned_value = nullptr;
11482  value_type* value_ref = nullptr;
11483  const bool is_rvalue;
11484 };
11485 } // namespace detail
11486 } // namespace nlohmann
11487 
11488 // #include <nlohmann/detail/macro_scope.hpp>
11489 
11490 // #include <nlohmann/detail/meta/cpp_future.hpp>
11491 
11492 // #include <nlohmann/detail/meta/type_traits.hpp>
11493 
11494 // #include <nlohmann/detail/output/binary_writer.hpp>
11495 
11496 
11497 #include <algorithm> // reverse
11498 #include <array> // array
11499 #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
11500 #include <cstring> // memcpy
11501 #include <limits> // numeric_limits
11502 #include <string> // string
11503 
11504 // #include <nlohmann/detail/input/binary_reader.hpp>
11505 
11506 // #include <nlohmann/detail/macro_scope.hpp>
11507 
11508 // #include <nlohmann/detail/output/output_adapters.hpp>
11509 
11510 
11511 #include <algorithm> // copy
11512 #include <cstddef> // size_t
11513 #include <ios> // streamsize
11514 #include <iterator> // back_inserter
11515 #include <memory> // shared_ptr, make_shared
11516 #include <ostream> // basic_ostream
11517 #include <string> // basic_string
11518 #include <vector> // vector
11519 // #include <nlohmann/detail/macro_scope.hpp>
11520 
11521 
11522 namespace nlohmann
11523 {
11524 namespace detail
11525 {
11527 template<typename CharType> struct output_adapter_protocol
11528 {
11529  virtual void write_character(CharType c) = 0;
11530  virtual void write_characters(const CharType* s, std::size_t length) = 0;
11531  virtual ~output_adapter_protocol() = default;
11532 };
11533 
11535 template<typename CharType>
11536 using output_adapter_t = std::shared_ptr<output_adapter_protocol<CharType>>;
11537 
11539 template<typename CharType>
11541 {
11542  public:
11543  explicit output_vector_adapter(std::vector<CharType>& vec) noexcept
11544  : v(vec)
11545  {}
11546 
11547  void write_character(CharType c) override
11548  {
11549  v.push_back(c);
11550  }
11551 
11553  void write_characters(const CharType* s, std::size_t length) override
11554  {
11555  std::copy(s, s + length, std::back_inserter(v));
11556  }
11557 
11558  private:
11559  std::vector<CharType>& v;
11560 };
11561 
11563 template<typename CharType>
11565 {
11566  public:
11567  explicit output_stream_adapter(std::basic_ostream<CharType>& s) noexcept
11568  : stream(s)
11569  {}
11570 
11571  void write_character(CharType c) override
11572  {
11573  stream.put(c);
11574  }
11575 
11577  void write_characters(const CharType* s, std::size_t length) override
11578  {
11579  stream.write(s, static_cast<std::streamsize>(length));
11580  }
11581 
11582  private:
11583  std::basic_ostream<CharType>& stream;
11584 };
11585 
11587 template<typename CharType, typename StringType = std::basic_string<CharType>>
11589 {
11590  public:
11591  explicit output_string_adapter(StringType& s) noexcept
11592  : str(s)
11593  {}
11594 
11595  void write_character(CharType c) override
11596  {
11597  str.push_back(c);
11598  }
11599 
11601  void write_characters(const CharType* s, std::size_t length) override
11602  {
11603  str.append(s, length);
11604  }
11605 
11606  private:
11607  StringType& str;
11608 };
11609 
11610 template<typename CharType, typename StringType = std::basic_string<CharType>>
11612 {
11613  public:
11614  output_adapter(std::vector<CharType>& vec)
11615  : oa(std::make_shared<output_vector_adapter<CharType>>(vec)) {}
11616 
11617  output_adapter(std::basic_ostream<CharType>& s)
11618  : oa(std::make_shared<output_stream_adapter<CharType>>(s)) {}
11619 
11620  output_adapter(StringType& s)
11621  : oa(std::make_shared<output_string_adapter<CharType, StringType>>(s)) {}
11622 
11624  {
11625  return oa;
11626  }
11627 
11628  private:
11630 };
11631 } // namespace detail
11632 } // namespace nlohmann
11633 
11634 
11635 namespace nlohmann
11636 {
11637 namespace detail
11638 {
11640 // binary writer //
11642 
11646 template<typename BasicJsonType, typename CharType>
11648 {
11649  using string_t = typename BasicJsonType::string_t;
11650 
11651  public:
11657  explicit binary_writer(output_adapter_t<CharType> adapter) : oa(adapter)
11658  {
11659  assert(oa);
11660  }
11661 
11666  void write_bson(const BasicJsonType& j)
11667  {
11668  switch (j.type())
11669  {
11670  case value_t::object:
11671  {
11672  write_bson_object(*j.m_value.object);
11673  break;
11674  }
11675 
11676  default:
11677  {
11678  JSON_THROW(type_error::create(317, "to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name())));
11679  }
11680  }
11681  }
11682 
11686  void write_cbor(const BasicJsonType& j)
11687  {
11688  switch (j.type())
11689  {
11690  case value_t::null:
11691  {
11692  oa->write_character(to_char_type(0xF6));
11693  break;
11694  }
11695 
11696  case value_t::boolean:
11697  {
11698  oa->write_character(j.m_value.boolean
11699  ? to_char_type(0xF5)
11700  : to_char_type(0xF4));
11701  break;
11702  }
11703 
11704  case value_t::number_integer:
11705  {
11706  if (j.m_value.number_integer >= 0)
11707  {
11708  // CBOR does not differentiate between positive signed
11709  // integers and unsigned integers. Therefore, we used the
11710  // code from the value_t::number_unsigned case here.
11711  if (j.m_value.number_integer <= 0x17)
11712  {
11713  write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
11714  }
11715  else if (j.m_value.number_integer <= (std::numeric_limits<std::uint8_t>::max)())
11716  {
11717  oa->write_character(to_char_type(0x18));
11718  write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
11719  }
11720  else if (j.m_value.number_integer <= (std::numeric_limits<std::uint16_t>::max)())
11721  {
11722  oa->write_character(to_char_type(0x19));
11723  write_number(static_cast<std::uint16_t>(j.m_value.number_integer));
11724  }
11725  else if (j.m_value.number_integer <= (std::numeric_limits<std::uint32_t>::max)())
11726  {
11727  oa->write_character(to_char_type(0x1A));
11728  write_number(static_cast<std::uint32_t>(j.m_value.number_integer));
11729  }
11730  else
11731  {
11732  oa->write_character(to_char_type(0x1B));
11733  write_number(static_cast<std::uint64_t>(j.m_value.number_integer));
11734  }
11735  }
11736  else
11737  {
11738  // The conversions below encode the sign in the first
11739  // byte, and the value is converted to a positive number.
11740  const auto positive_number = -1 - j.m_value.number_integer;
11741  if (j.m_value.number_integer >= -24)
11742  {
11743  write_number(static_cast<std::uint8_t>(0x20 + positive_number));
11744  }
11745  else if (positive_number <= (std::numeric_limits<std::uint8_t>::max)())
11746  {
11747  oa->write_character(to_char_type(0x38));
11748  write_number(static_cast<std::uint8_t>(positive_number));
11749  }
11750  else if (positive_number <= (std::numeric_limits<std::uint16_t>::max)())
11751  {
11752  oa->write_character(to_char_type(0x39));
11753  write_number(static_cast<std::uint16_t>(positive_number));
11754  }
11755  else if (positive_number <= (std::numeric_limits<std::uint32_t>::max)())
11756  {
11757  oa->write_character(to_char_type(0x3A));
11758  write_number(static_cast<std::uint32_t>(positive_number));
11759  }
11760  else
11761  {
11762  oa->write_character(to_char_type(0x3B));
11763  write_number(static_cast<std::uint64_t>(positive_number));
11764  }
11765  }
11766  break;
11767  }
11768 
11769  case value_t::number_unsigned:
11770  {
11771  if (j.m_value.number_unsigned <= 0x17)
11772  {
11773  write_number(static_cast<std::uint8_t>(j.m_value.number_unsigned));
11774  }
11775  else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
11776  {
11777  oa->write_character(to_char_type(0x18));
11778  write_number(static_cast<std::uint8_t>(j.m_value.number_unsigned));
11779  }
11780  else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
11781  {
11782  oa->write_character(to_char_type(0x19));
11783  write_number(static_cast<std::uint16_t>(j.m_value.number_unsigned));
11784  }
11785  else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
11786  {
11787  oa->write_character(to_char_type(0x1A));
11788  write_number(static_cast<std::uint32_t>(j.m_value.number_unsigned));
11789  }
11790  else
11791  {
11792  oa->write_character(to_char_type(0x1B));
11793  write_number(static_cast<std::uint64_t>(j.m_value.number_unsigned));
11794  }
11795  break;
11796  }
11797 
11798  case value_t::number_float:
11799  {
11800  oa->write_character(get_cbor_float_prefix(j.m_value.number_float));
11801  write_number(j.m_value.number_float);
11802  break;
11803  }
11804 
11805  case value_t::string:
11806  {
11807  // step 1: write control byte and the string length
11808  const auto N = j.m_value.string->size();
11809  if (N <= 0x17)
11810  {
11811  write_number(static_cast<std::uint8_t>(0x60 + N));
11812  }
11813  else if (N <= (std::numeric_limits<std::uint8_t>::max)())
11814  {
11815  oa->write_character(to_char_type(0x78));
11816  write_number(static_cast<std::uint8_t>(N));
11817  }
11819  {
11820  oa->write_character(to_char_type(0x79));
11821  write_number(static_cast<std::uint16_t>(N));
11822  }
11824  {
11825  oa->write_character(to_char_type(0x7A));
11826  write_number(static_cast<std::uint32_t>(N));
11827  }
11828  // LCOV_EXCL_START
11830  {
11831  oa->write_character(to_char_type(0x7B));
11832  write_number(static_cast<std::uint64_t>(N));
11833  }
11834  // LCOV_EXCL_STOP
11835 
11836  // step 2: write the string
11837  oa->write_characters(
11838  reinterpret_cast<const CharType*>(j.m_value.string->c_str()),
11839  j.m_value.string->size());
11840  break;
11841  }
11842 
11843  case value_t::array:
11844  {
11845  // step 1: write control byte and the array size
11846  const auto N = j.m_value.array->size();
11847  if (N <= 0x17)
11848  {
11849  write_number(static_cast<std::uint8_t>(0x80 + N));
11850  }
11851  else if (N <= (std::numeric_limits<std::uint8_t>::max)())
11852  {
11853  oa->write_character(to_char_type(0x98));
11854  write_number(static_cast<std::uint8_t>(N));
11855  }
11857  {
11858  oa->write_character(to_char_type(0x99));
11859  write_number(static_cast<std::uint16_t>(N));
11860  }
11862  {
11863  oa->write_character(to_char_type(0x9A));
11864  write_number(static_cast<std::uint32_t>(N));
11865  }
11866  // LCOV_EXCL_START
11868  {
11869  oa->write_character(to_char_type(0x9B));
11870  write_number(static_cast<std::uint64_t>(N));
11871  }
11872  // LCOV_EXCL_STOP
11873 
11874  // step 2: write each element
11875  for (const auto& el : *j.m_value.array)
11876  {
11877  write_cbor(el);
11878  }
11879  break;
11880  }
11881 
11882  case value_t::object:
11883  {
11884  // step 1: write control byte and the object size
11885  const auto N = j.m_value.object->size();
11886  if (N <= 0x17)
11887  {
11888  write_number(static_cast<std::uint8_t>(0xA0 + N));
11889  }
11890  else if (N <= (std::numeric_limits<std::uint8_t>::max)())
11891  {
11892  oa->write_character(to_char_type(0xB8));
11893  write_number(static_cast<std::uint8_t>(N));
11894  }
11896  {
11897  oa->write_character(to_char_type(0xB9));
11898  write_number(static_cast<std::uint16_t>(N));
11899  }
11901  {
11902  oa->write_character(to_char_type(0xBA));
11903  write_number(static_cast<std::uint32_t>(N));
11904  }
11905  // LCOV_EXCL_START
11907  {
11908  oa->write_character(to_char_type(0xBB));
11909  write_number(static_cast<std::uint64_t>(N));
11910  }
11911  // LCOV_EXCL_STOP
11912 
11913  // step 2: write each element
11914  for (const auto& el : *j.m_value.object)
11915  {
11916  write_cbor(el.first);
11917  write_cbor(el.second);
11918  }
11919  break;
11920  }
11921 
11922  default:
11923  break;
11924  }
11925  }
11926 
11930  void write_msgpack(const BasicJsonType& j)
11931  {
11932  switch (j.type())
11933  {
11934  case value_t::null: // nil
11935  {
11936  oa->write_character(to_char_type(0xC0));
11937  break;
11938  }
11939 
11940  case value_t::boolean: // true and false
11941  {
11942  oa->write_character(j.m_value.boolean
11943  ? to_char_type(0xC3)
11944  : to_char_type(0xC2));
11945  break;
11946  }
11947 
11948  case value_t::number_integer:
11949  {
11950  if (j.m_value.number_integer >= 0)
11951  {
11952  // MessagePack does not differentiate between positive
11953  // signed integers and unsigned integers. Therefore, we used
11954  // the code from the value_t::number_unsigned case here.
11955  if (j.m_value.number_unsigned < 128)
11956  {
11957  // positive fixnum
11958  write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
11959  }
11960  else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
11961  {
11962  // uint 8
11963  oa->write_character(to_char_type(0xCC));
11964  write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
11965  }
11966  else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
11967  {
11968  // uint 16
11969  oa->write_character(to_char_type(0xCD));
11970  write_number(static_cast<std::uint16_t>(j.m_value.number_integer));
11971  }
11972  else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
11973  {
11974  // uint 32
11975  oa->write_character(to_char_type(0xCE));
11976  write_number(static_cast<std::uint32_t>(j.m_value.number_integer));
11977  }
11978  else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
11979  {
11980  // uint 64
11981  oa->write_character(to_char_type(0xCF));
11982  write_number(static_cast<std::uint64_t>(j.m_value.number_integer));
11983  }
11984  }
11985  else
11986  {
11987  if (j.m_value.number_integer >= -32)
11988  {
11989  // negative fixnum
11990  write_number(static_cast<std::int8_t>(j.m_value.number_integer));
11991  }
11992  else if (j.m_value.number_integer >= (std::numeric_limits<std::int8_t>::min)() and
11993  j.m_value.number_integer <= (std::numeric_limits<std::int8_t>::max)())
11994  {
11995  // int 8
11996  oa->write_character(to_char_type(0xD0));
11997  write_number(static_cast<std::int8_t>(j.m_value.number_integer));
11998  }
11999  else if (j.m_value.number_integer >= (std::numeric_limits<std::int16_t>::min)() and
12000  j.m_value.number_integer <= (std::numeric_limits<std::int16_t>::max)())
12001  {
12002  // int 16
12003  oa->write_character(to_char_type(0xD1));
12004  write_number(static_cast<std::int16_t>(j.m_value.number_integer));
12005  }
12006  else if (j.m_value.number_integer >= (std::numeric_limits<std::int32_t>::min)() and
12007  j.m_value.number_integer <= (std::numeric_limits<std::int32_t>::max)())
12008  {
12009  // int 32
12010  oa->write_character(to_char_type(0xD2));
12011  write_number(static_cast<std::int32_t>(j.m_value.number_integer));
12012  }
12013  else if (j.m_value.number_integer >= (std::numeric_limits<std::int64_t>::min)() and
12014  j.m_value.number_integer <= (std::numeric_limits<std::int64_t>::max)())
12015  {
12016  // int 64
12017  oa->write_character(to_char_type(0xD3));
12018  write_number(static_cast<std::int64_t>(j.m_value.number_integer));
12019  }
12020  }
12021  break;
12022  }
12023 
12024  case value_t::number_unsigned:
12025  {
12026  if (j.m_value.number_unsigned < 128)
12027  {
12028  // positive fixnum
12029  write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
12030  }
12031  else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
12032  {
12033  // uint 8
12034  oa->write_character(to_char_type(0xCC));
12035  write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
12036  }
12037  else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
12038  {
12039  // uint 16
12040  oa->write_character(to_char_type(0xCD));
12041  write_number(static_cast<std::uint16_t>(j.m_value.number_integer));
12042  }
12043  else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
12044  {
12045  // uint 32
12046  oa->write_character(to_char_type(0xCE));
12047  write_number(static_cast<std::uint32_t>(j.m_value.number_integer));
12048  }
12049  else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
12050  {
12051  // uint 64
12052  oa->write_character(to_char_type(0xCF));
12053  write_number(static_cast<std::uint64_t>(j.m_value.number_integer));
12054  }
12055  break;
12056  }
12057 
12058  case value_t::number_float:
12059  {
12060  oa->write_character(get_msgpack_float_prefix(j.m_value.number_float));
12061  write_number(j.m_value.number_float);
12062  break;
12063  }
12064 
12065  case value_t::string:
12066  {
12067  // step 1: write control byte and the string length
12068  const auto N = j.m_value.string->size();
12069  if (N <= 31)
12070  {
12071  // fixstr
12072  write_number(static_cast<std::uint8_t>(0xA0 | N));
12073  }
12074  else if (N <= (std::numeric_limits<std::uint8_t>::max)())
12075  {
12076  // str 8
12077  oa->write_character(to_char_type(0xD9));
12078  write_number(static_cast<std::uint8_t>(N));
12079  }
12081  {
12082  // str 16
12083  oa->write_character(to_char_type(0xDA));
12084  write_number(static_cast<std::uint16_t>(N));
12085  }
12087  {
12088  // str 32
12089  oa->write_character(to_char_type(0xDB));
12090  write_number(static_cast<std::uint32_t>(N));
12091  }
12092 
12093  // step 2: write the string
12094  oa->write_characters(
12095  reinterpret_cast<const CharType*>(j.m_value.string->c_str()),
12096  j.m_value.string->size());
12097  break;
12098  }
12099 
12100  case value_t::array:
12101  {
12102  // step 1: write control byte and the array size
12103  const auto N = j.m_value.array->size();
12104  if (N <= 15)
12105  {
12106  // fixarray
12107  write_number(static_cast<std::uint8_t>(0x90 | N));
12108  }
12110  {
12111  // array 16
12112  oa->write_character(to_char_type(0xDC));
12113  write_number(static_cast<std::uint16_t>(N));
12114  }
12116  {
12117  // array 32
12118  oa->write_character(to_char_type(0xDD));
12119  write_number(static_cast<std::uint32_t>(N));
12120  }
12121 
12122  // step 2: write each element
12123  for (const auto& el : *j.m_value.array)
12124  {
12125  write_msgpack(el);
12126  }
12127  break;
12128  }
12129 
12130  case value_t::object:
12131  {
12132  // step 1: write control byte and the object size
12133  const auto N = j.m_value.object->size();
12134  if (N <= 15)
12135  {
12136  // fixmap
12137  write_number(static_cast<std::uint8_t>(0x80 | (N & 0xF)));
12138  }
12140  {
12141  // map 16
12142  oa->write_character(to_char_type(0xDE));
12143  write_number(static_cast<std::uint16_t>(N));
12144  }
12146  {
12147  // map 32
12148  oa->write_character(to_char_type(0xDF));
12149  write_number(static_cast<std::uint32_t>(N));
12150  }
12151 
12152  // step 2: write each element
12153  for (const auto& el : *j.m_value.object)
12154  {
12155  write_msgpack(el.first);
12156  write_msgpack(el.second);
12157  }
12158  break;
12159  }
12160 
12161  default:
12162  break;
12163  }
12164  }
12165 
12172  void write_ubjson(const BasicJsonType& j, const bool use_count,
12173  const bool use_type, const bool add_prefix = true)
12174  {
12175  switch (j.type())
12176  {
12177  case value_t::null:
12178  {
12179  if (add_prefix)
12180  {
12181  oa->write_character(to_char_type('Z'));
12182  }
12183  break;
12184  }
12185 
12186  case value_t::boolean:
12187  {
12188  if (add_prefix)
12189  {
12190  oa->write_character(j.m_value.boolean
12191  ? to_char_type('T')
12192  : to_char_type('F'));
12193  }
12194  break;
12195  }
12196 
12197  case value_t::number_integer:
12198  {
12199  write_number_with_ubjson_prefix(j.m_value.number_integer, add_prefix);
12200  break;
12201  }
12202 
12203  case value_t::number_unsigned:
12204  {
12205  write_number_with_ubjson_prefix(j.m_value.number_unsigned, add_prefix);
12206  break;
12207  }
12208 
12209  case value_t::number_float:
12210  {
12211  write_number_with_ubjson_prefix(j.m_value.number_float, add_prefix);
12212  break;
12213  }
12214 
12215  case value_t::string:
12216  {
12217  if (add_prefix)
12218  {
12219  oa->write_character(to_char_type('S'));
12220  }
12221  write_number_with_ubjson_prefix(j.m_value.string->size(), true);
12222  oa->write_characters(
12223  reinterpret_cast<const CharType*>(j.m_value.string->c_str()),
12224  j.m_value.string->size());
12225  break;
12226  }
12227 
12228  case value_t::array:
12229  {
12230  if (add_prefix)
12231  {
12232  oa->write_character(to_char_type('['));
12233  }
12234 
12235  bool prefix_required = true;
12236  if (use_type and not j.m_value.array->empty())
12237  {
12238  assert(use_count);
12239  const CharType first_prefix = ubjson_prefix(j.front());
12240  const bool same_prefix = std::all_of(j.begin() + 1, j.end(),
12241  [this, first_prefix](const BasicJsonType & v)
12242  {
12243  return ubjson_prefix(v) == first_prefix;
12244  });
12245 
12246  if (same_prefix)
12247  {
12248  prefix_required = false;
12249  oa->write_character(to_char_type('$'));
12250  oa->write_character(first_prefix);
12251  }
12252  }
12253 
12254  if (use_count)
12255  {
12256  oa->write_character(to_char_type('#'));
12257  write_number_with_ubjson_prefix(j.m_value.array->size(), true);
12258  }
12259 
12260  for (const auto& el : *j.m_value.array)
12261  {
12262  write_ubjson(el, use_count, use_type, prefix_required);
12263  }
12264 
12265  if (not use_count)
12266  {
12267  oa->write_character(to_char_type(']'));
12268  }
12269 
12270  break;
12271  }
12272 
12273  case value_t::object:
12274  {
12275  if (add_prefix)
12276  {
12277  oa->write_character(to_char_type('{'));
12278  }
12279 
12280  bool prefix_required = true;
12281  if (use_type and not j.m_value.object->empty())
12282  {
12283  assert(use_count);
12284  const CharType first_prefix = ubjson_prefix(j.front());
12285  const bool same_prefix = std::all_of(j.begin(), j.end(),
12286  [this, first_prefix](const BasicJsonType & v)
12287  {
12288  return ubjson_prefix(v) == first_prefix;
12289  });
12290 
12291  if (same_prefix)
12292  {
12293  prefix_required = false;
12294  oa->write_character(to_char_type('$'));
12295  oa->write_character(first_prefix);
12296  }
12297  }
12298 
12299  if (use_count)
12300  {
12301  oa->write_character(to_char_type('#'));
12302  write_number_with_ubjson_prefix(j.m_value.object->size(), true);
12303  }
12304 
12305  for (const auto& el : *j.m_value.object)
12306  {
12307  write_number_with_ubjson_prefix(el.first.size(), true);
12308  oa->write_characters(
12309  reinterpret_cast<const CharType*>(el.first.c_str()),
12310  el.first.size());
12311  write_ubjson(el.second, use_count, use_type, prefix_required);
12312  }
12313 
12314  if (not use_count)
12315  {
12316  oa->write_character(to_char_type('}'));
12317  }
12318 
12319  break;
12320  }
12321 
12322  default:
12323  break;
12324  }
12325  }
12326 
12327  private:
12329  // BSON //
12331 
12336  static std::size_t calc_bson_entry_header_size(const string_t& name)
12337  {
12338  const auto it = name.find(static_cast<typename string_t::value_type>(0));
12339  if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos))
12340  {
12341  JSON_THROW(out_of_range::create(409,
12342  "BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) + ")"));
12343  }
12344 
12345  return /*id*/ 1ul + name.size() + /*zero-terminator*/1u;
12346  }
12347 
12351  void write_bson_entry_header(const string_t& name,
12352  const std::uint8_t element_type)
12353  {
12354  oa->write_character(to_char_type(element_type)); // boolean
12355  oa->write_characters(
12356  reinterpret_cast<const CharType*>(name.c_str()),
12357  name.size() + 1u);
12358  }
12359 
12363  void write_bson_boolean(const string_t& name,
12364  const bool value)
12365  {
12366  write_bson_entry_header(name, 0x08);
12367  oa->write_character(value ? to_char_type(0x01) : to_char_type(0x00));
12368  }
12369 
12373  void write_bson_double(const string_t& name,
12374  const double value)
12375  {
12376  write_bson_entry_header(name, 0x01);
12377  write_number<double, true>(value);
12378  }
12379 
12383  static std::size_t calc_bson_string_size(const string_t& value)
12384  {
12385  return sizeof(std::int32_t) + value.size() + 1ul;
12386  }
12387 
12391  void write_bson_string(const string_t& name,
12392  const string_t& value)
12393  {
12394  write_bson_entry_header(name, 0x02);
12395 
12396  write_number<std::int32_t, true>(static_cast<std::int32_t>(value.size() + 1ul));
12397  oa->write_characters(
12398  reinterpret_cast<const CharType*>(value.c_str()),
12399  value.size() + 1);
12400  }
12401 
12405  void write_bson_null(const string_t& name)
12406  {
12407  write_bson_entry_header(name, 0x0A);
12408  }
12409 
12413  static std::size_t calc_bson_integer_size(const std::int64_t value)
12414  {
12416  ? sizeof(std::int32_t)
12417  : sizeof(std::int64_t);
12418  }
12419 
12423  void write_bson_integer(const string_t& name,
12424  const std::int64_t value)
12425  {
12427  {
12428  write_bson_entry_header(name, 0x10); // int32
12429  write_number<std::int32_t, true>(static_cast<std::int32_t>(value));
12430  }
12431  else
12432  {
12433  write_bson_entry_header(name, 0x12); // int64
12434  write_number<std::int64_t, true>(static_cast<std::int64_t>(value));
12435  }
12436  }
12437 
12441  static constexpr std::size_t calc_bson_unsigned_size(const std::uint64_t value) noexcept
12442  {
12443  return (value <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
12444  ? sizeof(std::int32_t)
12445  : sizeof(std::int64_t);
12446  }
12447 
12451  void write_bson_unsigned(const string_t& name,
12452  const std::uint64_t value)
12453  {
12454  if (value <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
12455  {
12456  write_bson_entry_header(name, 0x10 /* int32 */);
12457  write_number<std::int32_t, true>(static_cast<std::int32_t>(value));
12458  }
12459  else if (value <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
12460  {
12461  write_bson_entry_header(name, 0x12 /* int64 */);
12462  write_number<std::int64_t, true>(static_cast<std::int64_t>(value));
12463  }
12464  else
12465  {
12466  JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(value) + " cannot be represented by BSON as it does not fit int64"));
12467  }
12468  }
12469 
12473  void write_bson_object_entry(const string_t& name,
12474  const typename BasicJsonType::object_t& value)
12475  {
12476  write_bson_entry_header(name, 0x03); // object
12477  write_bson_object(value);
12478  }
12479 
12483  static std::size_t calc_bson_array_size(const typename BasicJsonType::array_t& value)
12484  {
12485  std::size_t array_index = 0ul;
12486 
12487  const std::size_t embedded_document_size = std::accumulate(std::begin(value), std::end(value), std::size_t(0), [&array_index](std::size_t result, const typename BasicJsonType::array_t::value_type & el)
12488  {
12489  return result + calc_bson_element_size(std::to_string(array_index++), el);
12490  });
12491 
12492  return sizeof(std::int32_t) + embedded_document_size + 1ul;
12493  }
12494 
12498  void write_bson_array(const string_t& name,
12499  const typename BasicJsonType::array_t& value)
12500  {
12501  write_bson_entry_header(name, 0x04); // array
12502  write_number<std::int32_t, true>(static_cast<std::int32_t>(calc_bson_array_size(value)));
12503 
12504  std::size_t array_index = 0ul;
12505 
12506  for (const auto& el : value)
12507  {
12508  write_bson_element(std::to_string(array_index++), el);
12509  }
12510 
12511  oa->write_character(to_char_type(0x00));
12512  }
12513 
12518  static std::size_t calc_bson_element_size(const string_t& name,
12519  const BasicJsonType& j)
12520  {
12521  const auto header_size = calc_bson_entry_header_size(name);
12522  switch (j.type())
12523  {
12524  case value_t::object:
12525  return header_size + calc_bson_object_size(*j.m_value.object);
12526 
12527  case value_t::array:
12528  return header_size + calc_bson_array_size(*j.m_value.array);
12529 
12530  case value_t::boolean:
12531  return header_size + 1ul;
12532 
12533  case value_t::number_float:
12534  return header_size + 8ul;
12535 
12536  case value_t::number_integer:
12537  return header_size + calc_bson_integer_size(j.m_value.number_integer);
12538 
12539  case value_t::number_unsigned:
12540  return header_size + calc_bson_unsigned_size(j.m_value.number_unsigned);
12541 
12542  case value_t::string:
12543  return header_size + calc_bson_string_size(*j.m_value.string);
12544 
12545  case value_t::null:
12546  return header_size + 0ul;
12547 
12548  // LCOV_EXCL_START
12549  default:
12550  assert(false);
12551  return 0ul;
12552  // LCOV_EXCL_STOP
12553  }
12554  }
12555 
12563  void write_bson_element(const string_t& name,
12564  const BasicJsonType& j)
12565  {
12566  switch (j.type())
12567  {
12568  case value_t::object:
12569  return write_bson_object_entry(name, *j.m_value.object);
12570 
12571  case value_t::array:
12572  return write_bson_array(name, *j.m_value.array);
12573 
12574  case value_t::boolean:
12575  return write_bson_boolean(name, j.m_value.boolean);
12576 
12577  case value_t::number_float:
12578  return write_bson_double(name, j.m_value.number_float);
12579 
12580  case value_t::number_integer:
12581  return write_bson_integer(name, j.m_value.number_integer);
12582 
12583  case value_t::number_unsigned:
12584  return write_bson_unsigned(name, j.m_value.number_unsigned);
12585 
12586  case value_t::string:
12587  return write_bson_string(name, *j.m_value.string);
12588 
12589  case value_t::null:
12590  return write_bson_null(name);
12591 
12592  // LCOV_EXCL_START
12593  default:
12594  assert(false);
12595  return;
12596  // LCOV_EXCL_STOP
12597  }
12598  }
12599 
12606  static std::size_t calc_bson_object_size(const typename BasicJsonType::object_t& value)
12607  {
12608  std::size_t document_size = std::accumulate(value.begin(), value.end(), std::size_t(0),
12609  [](size_t result, const typename BasicJsonType::object_t::value_type & el)
12610  {
12611  return result += calc_bson_element_size(el.first, el.second);
12612  });
12613 
12614  return sizeof(std::int32_t) + document_size + 1ul;
12615  }
12616 
12621  void write_bson_object(const typename BasicJsonType::object_t& value)
12622  {
12623  write_number<std::int32_t, true>(static_cast<std::int32_t>(calc_bson_object_size(value)));
12624 
12625  for (const auto& el : value)
12626  {
12627  write_bson_element(el.first, el.second);
12628  }
12629 
12630  oa->write_character(to_char_type(0x00));
12631  }
12632 
12634  // CBOR //
12636 
12637  static constexpr CharType get_cbor_float_prefix(float /*unused*/)
12638  {
12639  return to_char_type(0xFA); // Single-Precision Float
12640  }
12641 
12642  static constexpr CharType get_cbor_float_prefix(double /*unused*/)
12643  {
12644  return to_char_type(0xFB); // Double-Precision Float
12645  }
12646 
12648  // MsgPack //
12650 
12651  static constexpr CharType get_msgpack_float_prefix(float /*unused*/)
12652  {
12653  return to_char_type(0xCA); // float 32
12654  }
12655 
12656  static constexpr CharType get_msgpack_float_prefix(double /*unused*/)
12657  {
12658  return to_char_type(0xCB); // float 64
12659  }
12660 
12662  // UBJSON //
12664 
12665  // UBJSON: write number (floating point)
12666  template<typename NumberType, typename std::enable_if<
12668  void write_number_with_ubjson_prefix(const NumberType n,
12669  const bool add_prefix)
12670  {
12671  if (add_prefix)
12672  {
12673  oa->write_character(get_ubjson_float_prefix(n));
12674  }
12675  write_number(n);
12676  }
12677 
12678  // UBJSON: write number (unsigned integer)
12679  template<typename NumberType, typename std::enable_if<
12680  std::is_unsigned<NumberType>::value, int>::type = 0>
12681  void write_number_with_ubjson_prefix(const NumberType n,
12682  const bool add_prefix)
12683  {
12684  if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
12685  {
12686  if (add_prefix)
12687  {
12688  oa->write_character(to_char_type('i')); // int8
12689  }
12690  write_number(static_cast<std::uint8_t>(n));
12691  }
12692  else if (n <= (std::numeric_limits<std::uint8_t>::max)())
12693  {
12694  if (add_prefix)
12695  {
12696  oa->write_character(to_char_type('U')); // uint8
12697  }
12698  write_number(static_cast<std::uint8_t>(n));
12699  }
12700  else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
12701  {
12702  if (add_prefix)
12703  {
12704  oa->write_character(to_char_type('I')); // int16
12705  }
12706  write_number(static_cast<std::int16_t>(n));
12707  }
12708  else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
12709  {
12710  if (add_prefix)
12711  {
12712  oa->write_character(to_char_type('l')); // int32
12713  }
12714  write_number(static_cast<std::int32_t>(n));
12715  }
12716  else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
12717  {
12718  if (add_prefix)
12719  {
12720  oa->write_character(to_char_type('L')); // int64
12721  }
12722  write_number(static_cast<std::int64_t>(n));
12723  }
12724  else
12725  {
12726  JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(n) + " cannot be represented by UBJSON as it does not fit int64"));
12727  }
12728  }
12729 
12730  // UBJSON: write number (signed integer)
12731  template<typename NumberType, typename std::enable_if<
12733  not std::is_floating_point<NumberType>::value, int>::type = 0>
12734  void write_number_with_ubjson_prefix(const NumberType n,
12735  const bool add_prefix)
12736  {
12738  {
12739  if (add_prefix)
12740  {
12741  oa->write_character(to_char_type('i')); // int8
12742  }
12743  write_number(static_cast<std::int8_t>(n));
12744  }
12745  else if (static_cast<std::int64_t>((std::numeric_limits<std::uint8_t>::min)()) <= n and n <= static_cast<std::int64_t>((std::numeric_limits<std::uint8_t>::max)()))
12746  {
12747  if (add_prefix)
12748  {
12749  oa->write_character(to_char_type('U')); // uint8
12750  }
12751  write_number(static_cast<std::uint8_t>(n));
12752  }
12754  {
12755  if (add_prefix)
12756  {
12757  oa->write_character(to_char_type('I')); // int16
12758  }
12759  write_number(static_cast<std::int16_t>(n));
12760  }
12762  {
12763  if (add_prefix)
12764  {
12765  oa->write_character(to_char_type('l')); // int32
12766  }
12767  write_number(static_cast<std::int32_t>(n));
12768  }
12770  {
12771  if (add_prefix)
12772  {
12773  oa->write_character(to_char_type('L')); // int64
12774  }
12775  write_number(static_cast<std::int64_t>(n));
12776  }
12777  // LCOV_EXCL_START
12778  else
12779  {
12780  JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(n) + " cannot be represented by UBJSON as it does not fit int64"));
12781  }
12782  // LCOV_EXCL_STOP
12783  }
12784 
12794  CharType ubjson_prefix(const BasicJsonType& j) const noexcept
12795  {
12796  switch (j.type())
12797  {
12798  case value_t::null:
12799  return 'Z';
12800 
12801  case value_t::boolean:
12802  return j.m_value.boolean ? 'T' : 'F';
12803 
12804  case value_t::number_integer:
12805  {
12806  if ((std::numeric_limits<std::int8_t>::min)() <= j.m_value.number_integer and j.m_value.number_integer <= (std::numeric_limits<std::int8_t>::max)())
12807  {
12808  return 'i';
12809  }
12810  if ((std::numeric_limits<std::uint8_t>::min)() <= j.m_value.number_integer and j.m_value.number_integer <= (std::numeric_limits<std::uint8_t>::max)())
12811  {
12812  return 'U';
12813  }
12814  if ((std::numeric_limits<std::int16_t>::min)() <= j.m_value.number_integer and j.m_value.number_integer <= (std::numeric_limits<std::int16_t>::max)())
12815  {
12816  return 'I';
12817  }
12818  if ((std::numeric_limits<std::int32_t>::min)() <= j.m_value.number_integer and j.m_value.number_integer <= (std::numeric_limits<std::int32_t>::max)())
12819  {
12820  return 'l';
12821  }
12822  // no check and assume int64_t (see note above)
12823  return 'L';
12824  }
12825 
12826  case value_t::number_unsigned:
12827  {
12828  if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
12829  {
12830  return 'i';
12831  }
12832  if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint8_t>::max)()))
12833  {
12834  return 'U';
12835  }
12836  if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
12837  {
12838  return 'I';
12839  }
12840  if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
12841  {
12842  return 'l';
12843  }
12844  // no check and assume int64_t (see note above)
12845  return 'L';
12846  }
12847 
12848  case value_t::number_float:
12849  return get_ubjson_float_prefix(j.m_value.number_float);
12850 
12851  case value_t::string:
12852  return 'S';
12853 
12854  case value_t::array:
12855  return '[';
12856 
12857  case value_t::object:
12858  return '{';
12859 
12860  default: // discarded values
12861  return 'N';
12862  }
12863  }
12864 
12865  static constexpr CharType get_ubjson_float_prefix(float /*unused*/)
12866  {
12867  return 'd'; // float 32
12868  }
12869 
12870  static constexpr CharType get_ubjson_float_prefix(double /*unused*/)
12871  {
12872  return 'D'; // float 64
12873  }
12874 
12876  // Utility functions //
12878 
12879  /*
12880  @brief write a number to output input
12881  @param[in] n number of type @a NumberType
12882  @tparam NumberType the type of the number
12883  @tparam OutputIsLittleEndian Set to true if output data is
12884  required to be little endian
12885 
12886  @note This function needs to respect the system's endianess, because bytes
12887  in CBOR, MessagePack, and UBJSON are stored in network order (big
12888  endian) and therefore need reordering on little endian systems.
12889  */
12890  template<typename NumberType, bool OutputIsLittleEndian = false>
12891  void write_number(const NumberType n)
12892  {
12893  // step 1: write number to array of length NumberType
12894  std::array<CharType, sizeof(NumberType)> vec;
12895  std::memcpy(vec.data(), &n, sizeof(NumberType));
12896 
12897  // step 2: write array to output (with possible reordering)
12898  if (is_little_endian != OutputIsLittleEndian)
12899  {
12900  // reverse byte order prior to conversion if necessary
12901  std::reverse(vec.begin(), vec.end());
12902  }
12903 
12904  oa->write_characters(vec.data(), sizeof(NumberType));
12905  }
12906 
12907  public:
12908  // The following to_char_type functions are implement the conversion
12909  // between uint8_t and CharType. In case CharType is not unsigned,
12910  // such a conversion is required to allow values greater than 128.
12911  // See <https://github.com/nlohmann/json/issues/1286> for a discussion.
12912  template < typename C = CharType,
12914  static constexpr CharType to_char_type(std::uint8_t x) noexcept
12915  {
12916  return *reinterpret_cast<char*>(&x);
12917  }
12918 
12919  template < typename C = CharType,
12921  static CharType to_char_type(std::uint8_t x) noexcept
12922  {
12923  static_assert(sizeof(std::uint8_t) == sizeof(CharType), "size of CharType must be equal to std::uint8_t");
12924  static_assert(std::is_trivial<CharType>::value, "CharType must be trivial");
12925  CharType result;
12926  std::memcpy(&result, &x, sizeof(x));
12927  return result;
12928  }
12929 
12930  template<typename C = CharType,
12932  static constexpr CharType to_char_type(std::uint8_t x) noexcept
12933  {
12934  return x;
12935  }
12936 
12937  template < typename InputCharType, typename C = CharType,
12938  enable_if_t <
12941  std::is_same<char, typename std::remove_cv<InputCharType>::type>::value
12942  > * = nullptr >
12943  static constexpr CharType to_char_type(InputCharType x) noexcept
12944  {
12945  return x;
12946  }
12947 
12948  private:
12950  const bool is_little_endian = binary_reader<BasicJsonType>::little_endianess();
12951 
12954 };
12955 } // namespace detail
12956 } // namespace nlohmann
12957 
12958 // #include <nlohmann/detail/output/output_adapters.hpp>
12959 
12960 // #include <nlohmann/detail/output/serializer.hpp>
12961 
12962 
12963 #include <algorithm> // reverse, remove, fill, find, none_of
12964 #include <array> // array
12965 #include <cassert> // assert
12966 #include <ciso646> // and, or
12967 #include <clocale> // localeconv, lconv
12968 #include <cmath> // labs, isfinite, isnan, signbit
12969 #include <cstddef> // size_t, ptrdiff_t
12970 #include <cstdint> // uint8_t
12971 #include <cstdio> // snprintf
12972 #include <limits> // numeric_limits
12973 #include <string> // string
12974 #include <type_traits> // is_same
12975 #include <utility> // move
12976 
12977 // #include <nlohmann/detail/conversions/to_chars.hpp>
12978 
12979 
12980 #include <array> // array
12981 #include <cassert> // assert
12982 #include <ciso646> // or, and, not
12983 #include <cmath> // signbit, isfinite
12984 #include <cstdint> // intN_t, uintN_t
12985 #include <cstring> // memcpy, memmove
12986 #include <limits> // numeric_limits
12987 #include <type_traits> // conditional
12988 // #include <nlohmann/detail/macro_scope.hpp>
12989 
12990 
12991 namespace nlohmann
12992 {
12993 namespace detail
12994 {
12995 
13015 namespace dtoa_impl
13016 {
13017 
13018 template <typename Target, typename Source>
13020 {
13021  static_assert(sizeof(Target) == sizeof(Source), "size mismatch");
13022 
13023  Target target;
13024  std::memcpy(&target, &source, sizeof(Source));
13025  return target;
13026 }
13027 
13028 struct diyfp // f * 2^e
13029 {
13030  static constexpr int kPrecision = 64; // = q
13031 
13032  std::uint64_t f = 0;
13033  int e = 0;
13034 
13035  constexpr diyfp(std::uint64_t f_, int e_) noexcept : f(f_), e(e_) {}
13036 
13041  static diyfp sub(const diyfp& x, const diyfp& y) noexcept
13042  {
13043  assert(x.e == y.e);
13044  assert(x.f >= y.f);
13045 
13046  return {x.f - y.f, x.e};
13047  }
13048 
13053  static diyfp mul(const diyfp& x, const diyfp& y) noexcept
13054  {
13055  static_assert(kPrecision == 64, "internal error");
13056 
13057  // Computes:
13058  // f = round((x.f * y.f) / 2^q)
13059  // e = x.e + y.e + q
13060 
13061  // Emulate the 64-bit * 64-bit multiplication:
13062  //
13063  // p = u * v
13064  // = (u_lo + 2^32 u_hi) (v_lo + 2^32 v_hi)
13065  // = (u_lo v_lo ) + 2^32 ((u_lo v_hi ) + (u_hi v_lo )) + 2^64 (u_hi v_hi )
13066  // = (p0 ) + 2^32 ((p1 ) + (p2 )) + 2^64 (p3 )
13067  // = (p0_lo + 2^32 p0_hi) + 2^32 ((p1_lo + 2^32 p1_hi) + (p2_lo + 2^32 p2_hi)) + 2^64 (p3 )
13068  // = (p0_lo ) + 2^32 (p0_hi + p1_lo + p2_lo ) + 2^64 (p1_hi + p2_hi + p3)
13069  // = (p0_lo ) + 2^32 (Q ) + 2^64 (H )
13070  // = (p0_lo ) + 2^32 (Q_lo + 2^32 Q_hi ) + 2^64 (H )
13071  //
13072  // (Since Q might be larger than 2^32 - 1)
13073  //
13074  // = (p0_lo + 2^32 Q_lo) + 2^64 (Q_hi + H)
13075  //
13076  // (Q_hi + H does not overflow a 64-bit int)
13077  //
13078  // = p_lo + 2^64 p_hi
13079 
13080  const std::uint64_t u_lo = x.f & 0xFFFFFFFFu;
13081  const std::uint64_t u_hi = x.f >> 32u;
13082  const std::uint64_t v_lo = y.f & 0xFFFFFFFFu;
13083  const std::uint64_t v_hi = y.f >> 32u;
13084 
13085  const std::uint64_t p0 = u_lo * v_lo;
13086  const std::uint64_t p1 = u_lo * v_hi;
13087  const std::uint64_t p2 = u_hi * v_lo;
13088  const std::uint64_t p3 = u_hi * v_hi;
13089 
13090  const std::uint64_t p0_hi = p0 >> 32u;
13091  const std::uint64_t p1_lo = p1 & 0xFFFFFFFFu;
13092  const std::uint64_t p1_hi = p1 >> 32u;
13093  const std::uint64_t p2_lo = p2 & 0xFFFFFFFFu;
13094  const std::uint64_t p2_hi = p2 >> 32u;
13095 
13096  std::uint64_t Q = p0_hi + p1_lo + p2_lo;
13097 
13098  // The full product might now be computed as
13099  //
13100  // p_hi = p3 + p2_hi + p1_hi + (Q >> 32)
13101  // p_lo = p0_lo + (Q << 32)
13102  //
13103  // But in this particular case here, the full p_lo is not required.
13104  // Effectively we only need to add the highest bit in p_lo to p_hi (and
13105  // Q_hi + 1 does not overflow).
13106 
13107  Q += std::uint64_t{1} << (64u - 32u - 1u); // round, ties up
13108 
13109  const std::uint64_t h = p3 + p2_hi + p1_hi + (Q >> 32u);
13110 
13111  return {h, x.e + y.e + 64};
13112  }
13113 
13118  static diyfp normalize(diyfp x) noexcept
13119  {
13120  assert(x.f != 0);
13121 
13122  while ((x.f >> 63u) == 0)
13123  {
13124  x.f <<= 1u;
13125  x.e--;
13126  }
13127 
13128  return x;
13129  }
13130 
13135  static diyfp normalize_to(const diyfp& x, const int target_exponent) noexcept
13136  {
13137  const int delta = x.e - target_exponent;
13138 
13139  assert(delta >= 0);
13140  assert(((x.f << delta) >> delta) == x.f);
13141 
13142  return {x.f << delta, target_exponent};
13143  }
13144 };
13145 
13147 {
13151 };
13152 
13159 template <typename FloatType>
13161 {
13162  assert(std::isfinite(value));
13163  assert(value > 0);
13164 
13165  // Convert the IEEE representation into a diyfp.
13166  //
13167  // If v is denormal:
13168  // value = 0.F * 2^(1 - bias) = ( F) * 2^(1 - bias - (p-1))
13169  // If v is normalized:
13170  // value = 1.F * 2^(E - bias) = (2^(p-1) + F) * 2^(E - bias - (p-1))
13171 
13172  static_assert(std::numeric_limits<FloatType>::is_iec559,
13173  "internal error: dtoa_short requires an IEEE-754 floating-point implementation");
13174 
13175  constexpr int kPrecision = std::numeric_limits<FloatType>::digits; // = p (includes the hidden bit)
13176  constexpr int kBias = std::numeric_limits<FloatType>::max_exponent - 1 + (kPrecision - 1);
13177  constexpr int kMinExp = 1 - kBias;
13178  constexpr std::uint64_t kHiddenBit = std::uint64_t{1} << (kPrecision - 1); // = 2^(p-1)
13179 
13180  using bits_type = typename std::conditional<kPrecision == 24, std::uint32_t, std::uint64_t >::type;
13181 
13182  const std::uint64_t bits = reinterpret_bits<bits_type>(value);
13183  const std::uint64_t E = bits >> (kPrecision - 1);
13184  const std::uint64_t F = bits & (kHiddenBit - 1);
13185 
13186  const bool is_denormal = E == 0;
13187  const diyfp v = is_denormal
13188  ? diyfp(F, kMinExp)
13189  : diyfp(F + kHiddenBit, static_cast<int>(E) - kBias);
13190 
13191  // Compute the boundaries m- and m+ of the floating-point value
13192  // v = f * 2^e.
13193  //
13194  // Determine v- and v+, the floating-point predecessor and successor if v,
13195  // respectively.
13196  //
13197  // v- = v - 2^e if f != 2^(p-1) or e == e_min (A)
13198  // = v - 2^(e-1) if f == 2^(p-1) and e > e_min (B)
13199  //
13200  // v+ = v + 2^e
13201  //
13202  // Let m- = (v- + v) / 2 and m+ = (v + v+) / 2. All real numbers _strictly_
13203  // between m- and m+ round to v, regardless of how the input rounding
13204  // algorithm breaks ties.
13205  //
13206  // ---+-------------+-------------+-------------+-------------+--- (A)
13207  // v- m- v m+ v+
13208  //
13209  // -----------------+------+------+-------------+-------------+--- (B)
13210  // v- m- v m+ v+
13211 
13212  const bool lower_boundary_is_closer = F == 0 and E > 1;
13213  const diyfp m_plus = diyfp(2 * v.f + 1, v.e - 1);
13214  const diyfp m_minus = lower_boundary_is_closer
13215  ? diyfp(4 * v.f - 1, v.e - 2) // (B)
13216  : diyfp(2 * v.f - 1, v.e - 1); // (A)
13217 
13218  // Determine the normalized w+ = m+.
13219  const diyfp w_plus = diyfp::normalize(m_plus);
13220 
13221  // Determine w- = m- such that e_(w-) = e_(w+).
13222  const diyfp w_minus = diyfp::normalize_to(m_minus, w_plus.e);
13223 
13224  return {diyfp::normalize(v), w_minus, w_plus};
13225 }
13226 
13227 // Given normalized diyfp w, Grisu needs to find a (normalized) cached
13228 // power-of-ten c, such that the exponent of the product c * w = f * 2^e lies
13229 // within a certain range [alpha, gamma] (Definition 3.2 from [1])
13230 //
13231 // alpha <= e = e_c + e_w + q <= gamma
13232 //
13233 // or
13234 //
13235 // f_c * f_w * 2^alpha <= f_c 2^(e_c) * f_w 2^(e_w) * 2^q
13236 // <= f_c * f_w * 2^gamma
13237 //
13238 // Since c and w are normalized, i.e. 2^(q-1) <= f < 2^q, this implies
13239 //
13240 // 2^(q-1) * 2^(q-1) * 2^alpha <= c * w * 2^q < 2^q * 2^q * 2^gamma
13241 //
13242 // or
13243 //
13244 // 2^(q - 2 + alpha) <= c * w < 2^(q + gamma)
13245 //
13246 // The choice of (alpha,gamma) determines the size of the table and the form of
13247 // the digit generation procedure. Using (alpha,gamma)=(-60,-32) works out well
13248 // in practice:
13249 //
13250 // The idea is to cut the number c * w = f * 2^e into two parts, which can be
13251 // processed independently: An integral part p1, and a fractional part p2:
13252 //
13253 // f * 2^e = ( (f div 2^-e) * 2^-e + (f mod 2^-e) ) * 2^e
13254 // = (f div 2^-e) + (f mod 2^-e) * 2^e
13255 // = p1 + p2 * 2^e
13256 //
13257 // The conversion of p1 into decimal form requires a series of divisions and
13258 // modulos by (a power of) 10. These operations are faster for 32-bit than for
13259 // 64-bit integers, so p1 should ideally fit into a 32-bit integer. This can be
13260 // achieved by choosing
13261 //
13262 // -e >= 32 or e <= -32 := gamma
13263 //
13264 // In order to convert the fractional part
13265 //
13266 // p2 * 2^e = p2 / 2^-e = d[-1] / 10^1 + d[-2] / 10^2 + ...
13267 //
13268 // into decimal form, the fraction is repeatedly multiplied by 10 and the digits
13269 // d[-i] are extracted in order:
13270 //
13271 // (10 * p2) div 2^-e = d[-1]
13272 // (10 * p2) mod 2^-e = d[-2] / 10^1 + ...
13273 //
13274 // The multiplication by 10 must not overflow. It is sufficient to choose
13275 //
13276 // 10 * p2 < 16 * p2 = 2^4 * p2 <= 2^64.
13277 //
13278 // Since p2 = f mod 2^-e < 2^-e,
13279 //
13280 // -e <= 60 or e >= -60 := alpha
13281 
13282 constexpr int kAlpha = -60;
13283 constexpr int kGamma = -32;
13284 
13285 struct cached_power // c = f * 2^e ~= 10^k
13286 {
13287  std::uint64_t f;
13288  int e;
13289  int k;
13290 };
13291 
13300 {
13301  // Now
13302  //
13303  // alpha <= e_c + e + q <= gamma (1)
13304  // ==> f_c * 2^alpha <= c * 2^e * 2^q
13305  //
13306  // and since the c's are normalized, 2^(q-1) <= f_c,
13307  //
13308  // ==> 2^(q - 1 + alpha) <= c * 2^(e + q)
13309  // ==> 2^(alpha - e - 1) <= c
13310  //
13311  // If c were an exact power of ten, i.e. c = 10^k, one may determine k as
13312  //
13313  // k = ceil( log_10( 2^(alpha - e - 1) ) )
13314  // = ceil( (alpha - e - 1) * log_10(2) )
13315  //
13316  // From the paper:
13317  // "In theory the result of the procedure could be wrong since c is rounded,
13318  // and the computation itself is approximated [...]. In practice, however,
13319  // this simple function is sufficient."
13320  //
13321  // For IEEE double precision floating-point numbers converted into
13322  // normalized diyfp's w = f * 2^e, with q = 64,
13323  //
13324  // e >= -1022 (min IEEE exponent)
13325  // -52 (p - 1)
13326  // -52 (p - 1, possibly normalize denormal IEEE numbers)
13327  // -11 (normalize the diyfp)
13328  // = -1137
13329  //
13330  // and
13331  //
13332  // e <= +1023 (max IEEE exponent)
13333  // -52 (p - 1)
13334  // -11 (normalize the diyfp)
13335  // = 960
13336  //
13337  // This binary exponent range [-1137,960] results in a decimal exponent
13338  // range [-307,324]. One does not need to store a cached power for each
13339  // k in this range. For each such k it suffices to find a cached power
13340  // such that the exponent of the product lies in [alpha,gamma].
13341  // This implies that the difference of the decimal exponents of adjacent
13342  // table entries must be less than or equal to
13343  //
13344  // floor( (gamma - alpha) * log_10(2) ) = 8.
13345  //
13346  // (A smaller distance gamma-alpha would require a larger table.)
13347 
13348  // NB:
13349  // Actually this function returns c, such that -60 <= e_c + e + 64 <= -34.
13350 
13351  constexpr int kCachedPowersMinDecExp = -300;
13352  constexpr int kCachedPowersDecStep = 8;
13353 
13354  static constexpr std::array<cached_power, 79> kCachedPowers =
13355  {
13356  {
13357  { 0xAB70FE17C79AC6CA, -1060, -300 },
13358  { 0xFF77B1FCBEBCDC4F, -1034, -292 },
13359  { 0xBE5691EF416BD60C, -1007, -284 },
13360  { 0x8DD01FAD907FFC3C, -980, -276 },
13361  { 0xD3515C2831559A83, -954, -268 },
13362  { 0x9D71AC8FADA6C9B5, -927, -260 },
13363  { 0xEA9C227723EE8BCB, -901, -252 },
13364  { 0xAECC49914078536D, -874, -244 },
13365  { 0x823C12795DB6CE57, -847, -236 },
13366  { 0xC21094364DFB5637, -821, -228 },
13367  { 0x9096EA6F3848984F, -794, -220 },
13368  { 0xD77485CB25823AC7, -768, -212 },
13369  { 0xA086CFCD97BF97F4, -741, -204 },
13370  { 0xEF340A98172AACE5, -715, -196 },
13371  { 0xB23867FB2A35B28E, -688, -188 },
13372  { 0x84C8D4DFD2C63F3B, -661, -180 },
13373  { 0xC5DD44271AD3CDBA, -635, -172 },
13374  { 0x936B9FCEBB25C996, -608, -164 },
13375  { 0xDBAC6C247D62A584, -582, -156 },
13376  { 0xA3AB66580D5FDAF6, -555, -148 },
13377  { 0xF3E2F893DEC3F126, -529, -140 },
13378  { 0xB5B5ADA8AAFF80B8, -502, -132 },
13379  { 0x87625F056C7C4A8B, -475, -124 },
13380  { 0xC9BCFF6034C13053, -449, -116 },
13381  { 0x964E858C91BA2655, -422, -108 },
13382  { 0xDFF9772470297EBD, -396, -100 },
13383  { 0xA6DFBD9FB8E5B88F, -369, -92 },
13384  { 0xF8A95FCF88747D94, -343, -84 },
13385  { 0xB94470938FA89BCF, -316, -76 },
13386  { 0x8A08F0F8BF0F156B, -289, -68 },
13387  { 0xCDB02555653131B6, -263, -60 },
13388  { 0x993FE2C6D07B7FAC, -236, -52 },
13389  { 0xE45C10C42A2B3B06, -210, -44 },
13390  { 0xAA242499697392D3, -183, -36 },
13391  { 0xFD87B5F28300CA0E, -157, -28 },
13392  { 0xBCE5086492111AEB, -130, -20 },
13393  { 0x8CBCCC096F5088CC, -103, -12 },
13394  { 0xD1B71758E219652C, -77, -4 },
13395  { 0x9C40000000000000, -50, 4 },
13396  { 0xE8D4A51000000000, -24, 12 },
13397  { 0xAD78EBC5AC620000, 3, 20 },
13398  { 0x813F3978F8940984, 30, 28 },
13399  { 0xC097CE7BC90715B3, 56, 36 },
13400  { 0x8F7E32CE7BEA5C70, 83, 44 },
13401  { 0xD5D238A4ABE98068, 109, 52 },
13402  { 0x9F4F2726179A2245, 136, 60 },
13403  { 0xED63A231D4C4FB27, 162, 68 },
13404  { 0xB0DE65388CC8ADA8, 189, 76 },
13405  { 0x83C7088E1AAB65DB, 216, 84 },
13406  { 0xC45D1DF942711D9A, 242, 92 },
13407  { 0x924D692CA61BE758, 269, 100 },
13408  { 0xDA01EE641A708DEA, 295, 108 },
13409  { 0xA26DA3999AEF774A, 322, 116 },
13410  { 0xF209787BB47D6B85, 348, 124 },
13411  { 0xB454E4A179DD1877, 375, 132 },
13412  { 0x865B86925B9BC5C2, 402, 140 },
13413  { 0xC83553C5C8965D3D, 428, 148 },
13414  { 0x952AB45CFA97A0B3, 455, 156 },
13415  { 0xDE469FBD99A05FE3, 481, 164 },
13416  { 0xA59BC234DB398C25, 508, 172 },
13417  { 0xF6C69A72A3989F5C, 534, 180 },
13418  { 0xB7DCBF5354E9BECE, 561, 188 },
13419  { 0x88FCF317F22241E2, 588, 196 },
13420  { 0xCC20CE9BD35C78A5, 614, 204 },
13421  { 0x98165AF37B2153DF, 641, 212 },
13422  { 0xE2A0B5DC971F303A, 667, 220 },
13423  { 0xA8D9D1535CE3B396, 694, 228 },
13424  { 0xFB9B7CD9A4A7443C, 720, 236 },
13425  { 0xBB764C4CA7A44410, 747, 244 },
13426  { 0x8BAB8EEFB6409C1A, 774, 252 },
13427  { 0xD01FEF10A657842C, 800, 260 },
13428  { 0x9B10A4E5E9913129, 827, 268 },
13429  { 0xE7109BFBA19C0C9D, 853, 276 },
13430  { 0xAC2820D9623BF429, 880, 284 },
13431  { 0x80444B5E7AA7CF85, 907, 292 },
13432  { 0xBF21E44003ACDD2D, 933, 300 },
13433  { 0x8E679C2F5E44FF8F, 960, 308 },
13434  { 0xD433179D9C8CB841, 986, 316 },
13435  { 0x9E19DB92B4E31BA9, 1013, 324 },
13436  }
13437  };
13438 
13439  // This computation gives exactly the same results for k as
13440  // k = ceil((kAlpha - e - 1) * 0.30102999566398114)
13441  // for |e| <= 1500, but doesn't require floating-point operations.
13442  // NB: log_10(2) ~= 78913 / 2^18
13443  assert(e >= -1500);
13444  assert(e <= 1500);
13445  const int f = kAlpha - e - 1;
13446  const int k = (f * 78913) / (1 << 18) + static_cast<int>(f > 0);
13447 
13448  const int index = (-kCachedPowersMinDecExp + k + (kCachedPowersDecStep - 1)) / kCachedPowersDecStep;
13449  assert(index >= 0);
13450  assert(static_cast<std::size_t>(index) < kCachedPowers.size());
13451 
13452  const cached_power cached = kCachedPowers[static_cast<std::size_t>(index)];
13453  assert(kAlpha <= cached.e + e + 64);
13454  assert(kGamma >= cached.e + e + 64);
13455 
13456  return cached;
13457 }
13458 
13464 {
13465  // LCOV_EXCL_START
13466  if (n >= 1000000000)
13467  {
13468  pow10 = 1000000000;
13469  return 10;
13470  }
13471  // LCOV_EXCL_STOP
13472  else if (n >= 100000000)
13473  {
13474  pow10 = 100000000;
13475  return 9;
13476  }
13477  else if (n >= 10000000)
13478  {
13479  pow10 = 10000000;
13480  return 8;
13481  }
13482  else if (n >= 1000000)
13483  {
13484  pow10 = 1000000;
13485  return 7;
13486  }
13487  else if (n >= 100000)
13488  {
13489  pow10 = 100000;
13490  return 6;
13491  }
13492  else if (n >= 10000)
13493  {
13494  pow10 = 10000;
13495  return 5;
13496  }
13497  else if (n >= 1000)
13498  {
13499  pow10 = 1000;
13500  return 4;
13501  }
13502  else if (n >= 100)
13503  {
13504  pow10 = 100;
13505  return 3;
13506  }
13507  else if (n >= 10)
13508  {
13509  pow10 = 10;
13510  return 2;
13511  }
13512  else
13513  {
13514  pow10 = 1;
13515  return 1;
13516  }
13517 }
13518 
13519 inline void grisu2_round(char* buf, int len, std::uint64_t dist, std::uint64_t delta,
13520  std::uint64_t rest, std::uint64_t ten_k)
13521 {
13522  assert(len >= 1);
13523  assert(dist <= delta);
13524  assert(rest <= delta);
13525  assert(ten_k > 0);
13526 
13527  // <--------------------------- delta ---->
13528  // <---- dist --------->
13529  // --------------[------------------+-------------------]--------------
13530  // M- w M+
13531  //
13532  // ten_k
13533  // <------>
13534  // <---- rest ---->
13535  // --------------[------------------+----+--------------]--------------
13536  // w V
13537  // = buf * 10^k
13538  //
13539  // ten_k represents a unit-in-the-last-place in the decimal representation
13540  // stored in buf.
13541  // Decrement buf by ten_k while this takes buf closer to w.
13542 
13543  // The tests are written in this order to avoid overflow in unsigned
13544  // integer arithmetic.
13545 
13546  while (rest < dist
13547  and delta - rest >= ten_k
13548  and (rest + ten_k < dist or dist - rest > rest + ten_k - dist))
13549  {
13550  assert(buf[len - 1] != '0');
13551  buf[len - 1]--;
13552  rest += ten_k;
13553  }
13554 }
13555 
13560 inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent,
13561  diyfp M_minus, diyfp w, diyfp M_plus)
13562 {
13563  static_assert(kAlpha >= -60, "internal error");
13564  static_assert(kGamma <= -32, "internal error");
13565 
13566  // Generates the digits (and the exponent) of a decimal floating-point
13567  // number V = buffer * 10^decimal_exponent in the range [M-, M+]. The diyfp's
13568  // w, M- and M+ share the same exponent e, which satisfies alpha <= e <= gamma.
13569  //
13570  // <--------------------------- delta ---->
13571  // <---- dist --------->
13572  // --------------[------------------+-------------------]--------------
13573  // M- w M+
13574  //
13575  // Grisu2 generates the digits of M+ from left to right and stops as soon as
13576  // V is in [M-,M+].
13577 
13578  assert(M_plus.e >= kAlpha);
13579  assert(M_plus.e <= kGamma);
13580 
13581  std::uint64_t delta = diyfp::sub(M_plus, M_minus).f; // (significand of (M+ - M-), implicit exponent is e)
13582  std::uint64_t dist = diyfp::sub(M_plus, w ).f; // (significand of (M+ - w ), implicit exponent is e)
13583 
13584  // Split M+ = f * 2^e into two parts p1 and p2 (note: e < 0):
13585  //
13586  // M+ = f * 2^e
13587  // = ((f div 2^-e) * 2^-e + (f mod 2^-e)) * 2^e
13588  // = ((p1 ) * 2^-e + (p2 )) * 2^e
13589  // = p1 + p2 * 2^e
13590 
13591  const diyfp one(std::uint64_t{1} << -M_plus.e, M_plus.e);
13592 
13593  auto p1 = static_cast<std::uint32_t>(M_plus.f >> -one.e); // p1 = f div 2^-e (Since -e >= 32, p1 fits into a 32-bit int.)
13594  std::uint64_t p2 = M_plus.f & (one.f - 1); // p2 = f mod 2^-e
13595 
13596  // 1)
13597  //
13598  // Generate the digits of the integral part p1 = d[n-1]...d[1]d[0]
13599 
13600  assert(p1 > 0);
13601 
13602  std::uint32_t pow10;
13603  const int k = find_largest_pow10(p1, pow10);
13604 
13605  // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1)
13606  //
13607  // p1 = (p1 div 10^(k-1)) * 10^(k-1) + (p1 mod 10^(k-1))
13608  // = (d[k-1] ) * 10^(k-1) + (p1 mod 10^(k-1))
13609  //
13610  // M+ = p1 + p2 * 2^e
13611  // = d[k-1] * 10^(k-1) + (p1 mod 10^(k-1)) + p2 * 2^e
13612  // = d[k-1] * 10^(k-1) + ((p1 mod 10^(k-1)) * 2^-e + p2) * 2^e
13613  // = d[k-1] * 10^(k-1) + ( rest) * 2^e
13614  //
13615  // Now generate the digits d[n] of p1 from left to right (n = k-1,...,0)
13616  //
13617  // p1 = d[k-1]...d[n] * 10^n + d[n-1]...d[0]
13618  //
13619  // but stop as soon as
13620  //
13621  // rest * 2^e = (d[n-1]...d[0] * 2^-e + p2) * 2^e <= delta * 2^e
13622 
13623  int n = k;
13624  while (n > 0)
13625  {
13626  // Invariants:
13627  // M+ = buffer * 10^n + (p1 + p2 * 2^e) (buffer = 0 for n = k)
13628  // pow10 = 10^(n-1) <= p1 < 10^n
13629  //
13630  const std::uint32_t d = p1 / pow10; // d = p1 div 10^(n-1)
13631  const std::uint32_t r = p1 % pow10; // r = p1 mod 10^(n-1)
13632  //
13633  // M+ = buffer * 10^n + (d * 10^(n-1) + r) + p2 * 2^e
13634  // = (buffer * 10 + d) * 10^(n-1) + (r + p2 * 2^e)
13635  //
13636  assert(d <= 9);
13637  buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
13638  //
13639  // M+ = buffer * 10^(n-1) + (r + p2 * 2^e)
13640  //
13641  p1 = r;
13642  n--;
13643  //
13644  // M+ = buffer * 10^n + (p1 + p2 * 2^e)
13645  // pow10 = 10^n
13646  //
13647 
13648  // Now check if enough digits have been generated.
13649  // Compute
13650  //
13651  // p1 + p2 * 2^e = (p1 * 2^-e + p2) * 2^e = rest * 2^e
13652  //
13653  // Note:
13654  // Since rest and delta share the same exponent e, it suffices to
13655  // compare the significands.
13656  const std::uint64_t rest = (std::uint64_t{p1} << -one.e) + p2;
13657  if (rest <= delta)
13658  {
13659  // V = buffer * 10^n, with M- <= V <= M+.
13660 
13661  decimal_exponent += n;
13662 
13663  // We may now just stop. But instead look if the buffer could be
13664  // decremented to bring V closer to w.
13665  //
13666  // pow10 = 10^n is now 1 ulp in the decimal representation V.
13667  // The rounding procedure works with diyfp's with an implicit
13668  // exponent of e.
13669  //
13670  // 10^n = (10^n * 2^-e) * 2^e = ulp * 2^e
13671  //
13672  const std::uint64_t ten_n = std::uint64_t{pow10} << -one.e;
13673  grisu2_round(buffer, length, dist, delta, rest, ten_n);
13674 
13675  return;
13676  }
13677 
13678  pow10 /= 10;
13679  //
13680  // pow10 = 10^(n-1) <= p1 < 10^n
13681  // Invariants restored.
13682  }
13683 
13684  // 2)
13685  //
13686  // The digits of the integral part have been generated:
13687  //
13688  // M+ = d[k-1]...d[1]d[0] + p2 * 2^e
13689  // = buffer + p2 * 2^e
13690  //
13691  // Now generate the digits of the fractional part p2 * 2^e.
13692  //
13693  // Note:
13694  // No decimal point is generated: the exponent is adjusted instead.
13695  //
13696  // p2 actually represents the fraction
13697  //
13698  // p2 * 2^e
13699  // = p2 / 2^-e
13700  // = d[-1] / 10^1 + d[-2] / 10^2 + ...
13701  //
13702  // Now generate the digits d[-m] of p1 from left to right (m = 1,2,...)
13703  //
13704  // p2 * 2^e = d[-1]d[-2]...d[-m] * 10^-m
13705  // + 10^-m * (d[-m-1] / 10^1 + d[-m-2] / 10^2 + ...)
13706  //
13707  // using
13708  //
13709  // 10^m * p2 = ((10^m * p2) div 2^-e) * 2^-e + ((10^m * p2) mod 2^-e)
13710  // = ( d) * 2^-e + ( r)
13711  //
13712  // or
13713  // 10^m * p2 * 2^e = d + r * 2^e
13714  //
13715  // i.e.
13716  //
13717  // M+ = buffer + p2 * 2^e
13718  // = buffer + 10^-m * (d + r * 2^e)
13719  // = (buffer * 10^m + d) * 10^-m + 10^-m * r * 2^e
13720  //
13721  // and stop as soon as 10^-m * r * 2^e <= delta * 2^e
13722 
13723  assert(p2 > delta);
13724 
13725  int m = 0;
13726  for (;;)
13727  {
13728  // Invariant:
13729  // M+ = buffer * 10^-m + 10^-m * (d[-m-1] / 10 + d[-m-2] / 10^2 + ...) * 2^e
13730  // = buffer * 10^-m + 10^-m * (p2 ) * 2^e
13731  // = buffer * 10^-m + 10^-m * (1/10 * (10 * p2) ) * 2^e
13732  // = buffer * 10^-m + 10^-m * (1/10 * ((10*p2 div 2^-e) * 2^-e + (10*p2 mod 2^-e)) * 2^e
13733  //
13734  assert(p2 <= (std::numeric_limits<std::uint64_t>::max)() / 10);
13735  p2 *= 10;
13736  const std::uint64_t d = p2 >> -one.e; // d = (10 * p2) div 2^-e
13737  const std::uint64_t r = p2 & (one.f - 1); // r = (10 * p2) mod 2^-e
13738  //
13739  // M+ = buffer * 10^-m + 10^-m * (1/10 * (d * 2^-e + r) * 2^e
13740  // = buffer * 10^-m + 10^-m * (1/10 * (d + r * 2^e))
13741  // = (buffer * 10 + d) * 10^(-m-1) + 10^(-m-1) * r * 2^e
13742  //
13743  assert(d <= 9);
13744  buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
13745  //
13746  // M+ = buffer * 10^(-m-1) + 10^(-m-1) * r * 2^e
13747  //
13748  p2 = r;
13749  m++;
13750  //
13751  // M+ = buffer * 10^-m + 10^-m * p2 * 2^e
13752  // Invariant restored.
13753 
13754  // Check if enough digits have been generated.
13755  //
13756  // 10^-m * p2 * 2^e <= delta * 2^e
13757  // p2 * 2^e <= 10^m * delta * 2^e
13758  // p2 <= 10^m * delta
13759  delta *= 10;
13760  dist *= 10;
13761  if (p2 <= delta)
13762  {
13763  break;
13764  }
13765  }
13766 
13767  // V = buffer * 10^-m, with M- <= V <= M+.
13768 
13769  decimal_exponent -= m;
13770 
13771  // 1 ulp in the decimal representation is now 10^-m.
13772  // Since delta and dist are now scaled by 10^m, we need to do the
13773  // same with ulp in order to keep the units in sync.
13774  //
13775  // 10^m * 10^-m = 1 = 2^-e * 2^e = ten_m * 2^e
13776  //
13777  const std::uint64_t ten_m = one.f;
13778  grisu2_round(buffer, length, dist, delta, p2, ten_m);
13779 
13780  // By construction this algorithm generates the shortest possible decimal
13781  // number (Loitsch, Theorem 6.2) which rounds back to w.
13782  // For an input number of precision p, at least
13783  //
13784  // N = 1 + ceil(p * log_10(2))
13785  //
13786  // decimal digits are sufficient to identify all binary floating-point
13787  // numbers (Matula, "In-and-Out conversions").
13788  // This implies that the algorithm does not produce more than N decimal
13789  // digits.
13790  //
13791  // N = 17 for p = 53 (IEEE double precision)
13792  // N = 9 for p = 24 (IEEE single precision)
13793 }
13794 
13801 inline void grisu2(char* buf, int& len, int& decimal_exponent,
13802  diyfp m_minus, diyfp v, diyfp m_plus)
13803 {
13804  assert(m_plus.e == m_minus.e);
13805  assert(m_plus.e == v.e);
13806 
13807  // --------(-----------------------+-----------------------)-------- (A)
13808  // m- v m+
13809  //
13810  // --------------------(-----------+-----------------------)-------- (B)
13811  // m- v m+
13812  //
13813  // First scale v (and m- and m+) such that the exponent is in the range
13814  // [alpha, gamma].
13815 
13816  const cached_power cached = get_cached_power_for_binary_exponent(m_plus.e);
13817 
13818  const diyfp c_minus_k(cached.f, cached.e); // = c ~= 10^-k
13819 
13820  // The exponent of the products is = v.e + c_minus_k.e + q and is in the range [alpha,gamma]
13821  const diyfp w = diyfp::mul(v, c_minus_k);
13822  const diyfp w_minus = diyfp::mul(m_minus, c_minus_k);
13823  const diyfp w_plus = diyfp::mul(m_plus, c_minus_k);
13824 
13825  // ----(---+---)---------------(---+---)---------------(---+---)----
13826  // w- w w+
13827  // = c*m- = c*v = c*m+
13828  //
13829  // diyfp::mul rounds its result and c_minus_k is approximated too. w, w- and
13830  // w+ are now off by a small amount.
13831  // In fact:
13832  //
13833  // w - v * 10^k < 1 ulp
13834  //
13835  // To account for this inaccuracy, add resp. subtract 1 ulp.
13836  //
13837  // --------+---[---------------(---+---)---------------]---+--------
13838  // w- M- w M+ w+
13839  //
13840  // Now any number in [M-, M+] (bounds included) will round to w when input,
13841  // regardless of how the input rounding algorithm breaks ties.
13842  //
13843  // And digit_gen generates the shortest possible such number in [M-, M+].
13844  // Note that this does not mean that Grisu2 always generates the shortest
13845  // possible number in the interval (m-, m+).
13846  const diyfp M_minus(w_minus.f + 1, w_minus.e);
13847  const diyfp M_plus (w_plus.f - 1, w_plus.e );
13848 
13849  decimal_exponent = -cached.k; // = -(-k) = k
13850 
13851  grisu2_digit_gen(buf, len, decimal_exponent, M_minus, w, M_plus);
13852 }
13853 
13859 template <typename FloatType>
13861 void grisu2(char* buf, int& len, int& decimal_exponent, FloatType value)
13862 {
13863  static_assert(diyfp::kPrecision >= std::numeric_limits<FloatType>::digits + 3,
13864  "internal error: not enough precision");
13865 
13866  assert(std::isfinite(value));
13867  assert(value > 0);
13868 
13869  // If the neighbors (and boundaries) of 'value' are always computed for double-precision
13870  // numbers, all float's can be recovered using strtod (and strtof). However, the resulting
13871  // decimal representations are not exactly "short".
13872  //
13873  // The documentation for 'std::to_chars' (https://en.cppreference.com/w/cpp/utility/to_chars)
13874  // says "value is converted to a string as if by std::sprintf in the default ("C") locale"
13875  // and since sprintf promotes float's to double's, I think this is exactly what 'std::to_chars'
13876  // does.
13877  // On the other hand, the documentation for 'std::to_chars' requires that "parsing the
13878  // representation using the corresponding std::from_chars function recovers value exactly". That
13879  // indicates that single precision floating-point numbers should be recovered using
13880  // 'std::strtof'.
13881  //
13882  // NB: If the neighbors are computed for single-precision numbers, there is a single float
13883  // (7.0385307e-26f) which can't be recovered using strtod. The resulting double precision
13884  // value is off by 1 ulp.
13885 #if 0
13886  const boundaries w = compute_boundaries(static_cast<double>(value));
13887 #else
13888  const boundaries w = compute_boundaries(value);
13889 #endif
13890 
13891  grisu2(buf, len, decimal_exponent, w.minus, w.w, w.plus);
13892 }
13893 
13901 inline char* append_exponent(char* buf, int e)
13902 {
13903  assert(e > -1000);
13904  assert(e < 1000);
13905 
13906  if (e < 0)
13907  {
13908  e = -e;
13909  *buf++ = '-';
13910  }
13911  else
13912  {
13913  *buf++ = '+';
13914  }
13915 
13916  auto k = static_cast<std::uint32_t>(e);
13917  if (k < 10)
13918  {
13919  // Always print at least two digits in the exponent.
13920  // This is for compatibility with printf("%g").
13921  *buf++ = '0';
13922  *buf++ = static_cast<char>('0' + k);
13923  }
13924  else if (k < 100)
13925  {
13926  *buf++ = static_cast<char>('0' + k / 10);
13927  k %= 10;
13928  *buf++ = static_cast<char>('0' + k);
13929  }
13930  else
13931  {
13932  *buf++ = static_cast<char>('0' + k / 100);
13933  k %= 100;
13934  *buf++ = static_cast<char>('0' + k / 10);
13935  k %= 10;
13936  *buf++ = static_cast<char>('0' + k);
13937  }
13938 
13939  return buf;
13940 }
13941 
13953 inline char* format_buffer(char* buf, int len, int decimal_exponent,
13954  int min_exp, int max_exp)
13955 {
13956  assert(min_exp < 0);
13957  assert(max_exp > 0);
13958 
13959  const int k = len;
13960  const int n = len + decimal_exponent;
13961 
13962  // v = buf * 10^(n-k)
13963  // k is the length of the buffer (number of decimal digits)
13964  // n is the position of the decimal point relative to the start of the buffer.
13965 
13966  if (k <= n and n <= max_exp)
13967  {
13968  // digits[000]
13969  // len <= max_exp + 2
13970 
13971  std::memset(buf + k, '0', static_cast<size_t>(n - k));
13972  // Make it look like a floating-point number (#362, #378)
13973  buf[n + 0] = '.';
13974  buf[n + 1] = '0';
13975  return buf + (n + 2);
13976  }
13977 
13978  if (0 < n and n <= max_exp)
13979  {
13980  // dig.its
13981  // len <= max_digits10 + 1
13982 
13983  assert(k > n);
13984 
13985  std::memmove(buf + (n + 1), buf + n, static_cast<size_t>(k - n));
13986  buf[n] = '.';
13987  return buf + (k + 1);
13988  }
13989 
13990  if (min_exp < n and n <= 0)
13991  {
13992  // 0.[000]digits
13993  // len <= 2 + (-min_exp - 1) + max_digits10
13994 
13995  std::memmove(buf + (2 + -n), buf, static_cast<size_t>(k));
13996  buf[0] = '0';
13997  buf[1] = '.';
13998  std::memset(buf + 2, '0', static_cast<size_t>(-n));
13999  return buf + (2 + (-n) + k);
14000  }
14001 
14002  if (k == 1)
14003  {
14004  // dE+123
14005  // len <= 1 + 5
14006 
14007  buf += 1;
14008  }
14009  else
14010  {
14011  // d.igitsE+123
14012  // len <= max_digits10 + 1 + 5
14013 
14014  std::memmove(buf + 2, buf + 1, static_cast<size_t>(k - 1));
14015  buf[1] = '.';
14016  buf += 1 + k;
14017  }
14018 
14019  *buf++ = 'e';
14020  return append_exponent(buf, n - 1);
14021 }
14022 
14023 } // namespace dtoa_impl
14024 
14035 template <typename FloatType>
14038 char* to_chars(char* first, const char* last, FloatType value)
14039 {
14040  static_cast<void>(last); // maybe unused - fix warning
14041  assert(std::isfinite(value));
14042 
14043  // Use signbit(value) instead of (value < 0) since signbit works for -0.
14044  if (std::signbit(value))
14045  {
14046  value = -value;
14047  *first++ = '-';
14048  }
14049 
14050  if (value == 0) // +-0
14051  {
14052  *first++ = '0';
14053  // Make it look like a floating-point number (#362, #378)
14054  *first++ = '.';
14055  *first++ = '0';
14056  return first;
14057  }
14058 
14059  assert(last - first >= std::numeric_limits<FloatType>::max_digits10);
14060 
14061  // Compute v = buffer * 10^decimal_exponent.
14062  // The decimal digits are stored in the buffer, which needs to be interpreted
14063  // as an unsigned decimal integer.
14064  // len is the length of the buffer, i.e. the number of decimal digits.
14065  int len = 0;
14066  int decimal_exponent = 0;
14067  dtoa_impl::grisu2(first, len, decimal_exponent, value);
14068 
14069  assert(len <= std::numeric_limits<FloatType>::max_digits10);
14070 
14071  // Format the buffer like printf("%.*g", prec, value)
14072  constexpr int kMinExp = -4;
14073  // Use digits10 here to increase compatibility with version 2.
14074  constexpr int kMaxExp = std::numeric_limits<FloatType>::digits10;
14075 
14076  assert(last - first >= kMaxExp + 2);
14077  assert(last - first >= 2 + (-kMinExp - 1) + std::numeric_limits<FloatType>::max_digits10);
14078  assert(last - first >= std::numeric_limits<FloatType>::max_digits10 + 6);
14079 
14080  return dtoa_impl::format_buffer(first, len, decimal_exponent, kMinExp, kMaxExp);
14081 }
14082 
14083 } // namespace detail
14084 } // namespace nlohmann
14085 
14086 // #include <nlohmann/detail/exceptions.hpp>
14087 
14088 // #include <nlohmann/detail/macro_scope.hpp>
14089 
14090 // #include <nlohmann/detail/meta/cpp_future.hpp>
14091 
14092 // #include <nlohmann/detail/output/binary_writer.hpp>
14093 
14094 // #include <nlohmann/detail/output/output_adapters.hpp>
14095 
14096 // #include <nlohmann/detail/value_t.hpp>
14097 
14098 
14099 namespace nlohmann
14100 {
14101 namespace detail
14102 {
14104 // serialization //
14106 
14109 {
14110  strict,
14111  replace,
14112  ignore
14113 };
14114 
14115 template<typename BasicJsonType>
14117 {
14118  using string_t = typename BasicJsonType::string_t;
14119  using number_float_t = typename BasicJsonType::number_float_t;
14120  using number_integer_t = typename BasicJsonType::number_integer_t;
14121  using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
14122  static constexpr std::uint8_t UTF8_ACCEPT = 0;
14123  static constexpr std::uint8_t UTF8_REJECT = 1;
14124 
14125  public:
14132  error_handler_t error_handler_ = error_handler_t::strict)
14133  : o(std::move(s))
14134  , loc(std::localeconv())
14135  , thousands_sep(loc->thousands_sep == nullptr ? '\0' : * (loc->thousands_sep))
14136  , decimal_point(loc->decimal_point == nullptr ? '\0' : * (loc->decimal_point))
14137  , indent_char(ichar)
14138  , indent_string(512, indent_char)
14139  , error_handler(error_handler_)
14140  {}
14141 
14142  // delete because of pointer members
14143  serializer(const serializer&) = delete;
14144  serializer& operator=(const serializer&) = delete;
14145  serializer(serializer&&) = delete;
14146  serializer& operator=(serializer&&) = delete;
14147  ~serializer() = default;
14148 
14166  void dump(const BasicJsonType& val, const bool pretty_print,
14167  const bool ensure_ascii,
14168  const unsigned int indent_step,
14169  const unsigned int current_indent = 0)
14170  {
14171  switch (val.m_type)
14172  {
14173  case value_t::object:
14174  {
14175  if (val.m_value.object->empty())
14176  {
14177  o->write_characters("{}", 2);
14178  return;
14179  }
14180 
14181  if (pretty_print)
14182  {
14183  o->write_characters("{\n", 2);
14184 
14185  // variable to hold indentation for recursive calls
14186  const auto new_indent = current_indent + indent_step;
14187  if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
14188  {
14189  indent_string.resize(indent_string.size() * 2, ' ');
14190  }
14191 
14192  // first n-1 elements
14193  auto i = val.m_value.object->cbegin();
14194  for (std::size_t cnt = 0; cnt < val.m_value.object->size() - 1; ++cnt, ++i)
14195  {
14196  o->write_characters(indent_string.c_str(), new_indent);
14197  o->write_character('\"');
14198  dump_escaped(i->first, ensure_ascii);
14199  o->write_characters("\": ", 3);
14200  dump(i->second, true, ensure_ascii, indent_step, new_indent);
14201  o->write_characters(",\n", 2);
14202  }
14203 
14204  // last element
14205  assert(i != val.m_value.object->cend());
14206  assert(std::next(i) == val.m_value.object->cend());
14207  o->write_characters(indent_string.c_str(), new_indent);
14208  o->write_character('\"');
14209  dump_escaped(i->first, ensure_ascii);
14210  o->write_characters("\": ", 3);
14211  dump(i->second, true, ensure_ascii, indent_step, new_indent);
14212 
14213  o->write_character('\n');
14214  o->write_characters(indent_string.c_str(), current_indent);
14215  o->write_character('}');
14216  }
14217  else
14218  {
14219  o->write_character('{');
14220 
14221  // first n-1 elements
14222  auto i = val.m_value.object->cbegin();
14223  for (std::size_t cnt = 0; cnt < val.m_value.object->size() - 1; ++cnt, ++i)
14224  {
14225  o->write_character('\"');
14226  dump_escaped(i->first, ensure_ascii);
14227  o->write_characters("\":", 2);
14228  dump(i->second, false, ensure_ascii, indent_step, current_indent);
14229  o->write_character(',');
14230  }
14231 
14232  // last element
14233  assert(i != val.m_value.object->cend());
14234  assert(std::next(i) == val.m_value.object->cend());
14235  o->write_character('\"');
14236  dump_escaped(i->first, ensure_ascii);
14237  o->write_characters("\":", 2);
14238  dump(i->second, false, ensure_ascii, indent_step, current_indent);
14239 
14240  o->write_character('}');
14241  }
14242 
14243  return;
14244  }
14245 
14246  case value_t::array:
14247  {
14248  if (val.m_value.array->empty())
14249  {
14250  o->write_characters("[]", 2);
14251  return;
14252  }
14253 
14254  if (pretty_print)
14255  {
14256  o->write_characters("[\n", 2);
14257 
14258  // variable to hold indentation for recursive calls
14259  const auto new_indent = current_indent + indent_step;
14260  if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
14261  {
14262  indent_string.resize(indent_string.size() * 2, ' ');
14263  }
14264 
14265  // first n-1 elements
14266  for (auto i = val.m_value.array->cbegin();
14267  i != val.m_value.array->cend() - 1; ++i)
14268  {
14269  o->write_characters(indent_string.c_str(), new_indent);
14270  dump(*i, true, ensure_ascii, indent_step, new_indent);
14271  o->write_characters(",\n", 2);
14272  }
14273 
14274  // last element
14275  assert(not val.m_value.array->empty());
14276  o->write_characters(indent_string.c_str(), new_indent);
14277  dump(val.m_value.array->back(), true, ensure_ascii, indent_step, new_indent);
14278 
14279  o->write_character('\n');
14280  o->write_characters(indent_string.c_str(), current_indent);
14281  o->write_character(']');
14282  }
14283  else
14284  {
14285  o->write_character('[');
14286 
14287  // first n-1 elements
14288  for (auto i = val.m_value.array->cbegin();
14289  i != val.m_value.array->cend() - 1; ++i)
14290  {
14291  dump(*i, false, ensure_ascii, indent_step, current_indent);
14292  o->write_character(',');
14293  }
14294 
14295  // last element
14296  assert(not val.m_value.array->empty());
14297  dump(val.m_value.array->back(), false, ensure_ascii, indent_step, current_indent);
14298 
14299  o->write_character(']');
14300  }
14301 
14302  return;
14303  }
14304 
14305  case value_t::string:
14306  {
14307  o->write_character('\"');
14308  dump_escaped(*val.m_value.string, ensure_ascii);
14309  o->write_character('\"');
14310  return;
14311  }
14312 
14313  case value_t::boolean:
14314  {
14315  if (val.m_value.boolean)
14316  {
14317  o->write_characters("true", 4);
14318  }
14319  else
14320  {
14321  o->write_characters("false", 5);
14322  }
14323  return;
14324  }
14325 
14326  case value_t::number_integer:
14327  {
14328  dump_integer(val.m_value.number_integer);
14329  return;
14330  }
14331 
14332  case value_t::number_unsigned:
14333  {
14334  dump_integer(val.m_value.number_unsigned);
14335  return;
14336  }
14337 
14338  case value_t::number_float:
14339  {
14340  dump_float(val.m_value.number_float);
14341  return;
14342  }
14343 
14344  case value_t::discarded:
14345  {
14346  o->write_characters("<discarded>", 11);
14347  return;
14348  }
14349 
14350  case value_t::null:
14351  {
14352  o->write_characters("null", 4);
14353  return;
14354  }
14355 
14356  default: // LCOV_EXCL_LINE
14357  assert(false); // LCOV_EXCL_LINE
14358  }
14359  }
14360 
14361  private:
14376  void dump_escaped(const string_t& s, const bool ensure_ascii)
14377  {
14378  std::uint32_t codepoint;
14379  std::uint8_t state = UTF8_ACCEPT;
14380  std::size_t bytes = 0; // number of bytes written to string_buffer
14381 
14382  // number of bytes written at the point of the last valid byte
14383  std::size_t bytes_after_last_accept = 0;
14384  std::size_t undumped_chars = 0;
14385 
14386  for (std::size_t i = 0; i < s.size(); ++i)
14387  {
14388  const auto byte = static_cast<uint8_t>(s[i]);
14389 
14390  switch (decode(state, codepoint, byte))
14391  {
14392  case UTF8_ACCEPT: // decode found a new code point
14393  {
14394  switch (codepoint)
14395  {
14396  case 0x08: // backspace
14397  {
14398  string_buffer[bytes++] = '\\';
14399  string_buffer[bytes++] = 'b';
14400  break;
14401  }
14402 
14403  case 0x09: // horizontal tab
14404  {
14405  string_buffer[bytes++] = '\\';
14406  string_buffer[bytes++] = 't';
14407  break;
14408  }
14409 
14410  case 0x0A: // newline
14411  {
14412  string_buffer[bytes++] = '\\';
14413  string_buffer[bytes++] = 'n';
14414  break;
14415  }
14416 
14417  case 0x0C: // formfeed
14418  {
14419  string_buffer[bytes++] = '\\';
14420  string_buffer[bytes++] = 'f';
14421  break;
14422  }
14423 
14424  case 0x0D: // carriage return
14425  {
14426  string_buffer[bytes++] = '\\';
14427  string_buffer[bytes++] = 'r';
14428  break;
14429  }
14430 
14431  case 0x22: // quotation mark
14432  {
14433  string_buffer[bytes++] = '\\';
14434  string_buffer[bytes++] = '\"';
14435  break;
14436  }
14437 
14438  case 0x5C: // reverse solidus
14439  {
14440  string_buffer[bytes++] = '\\';
14441  string_buffer[bytes++] = '\\';
14442  break;
14443  }
14444 
14445  default:
14446  {
14447  // escape control characters (0x00..0x1F) or, if
14448  // ensure_ascii parameter is used, non-ASCII characters
14449  if ((codepoint <= 0x1F) or (ensure_ascii and (codepoint >= 0x7F)))
14450  {
14451  if (codepoint <= 0xFFFF)
14452  {
14453  (std::snprintf)(string_buffer.data() + bytes, 7, "\\u%04x",
14454  static_cast<std::uint16_t>(codepoint));
14455  bytes += 6;
14456  }
14457  else
14458  {
14459  (std::snprintf)(string_buffer.data() + bytes, 13, "\\u%04x\\u%04x",
14460  static_cast<std::uint16_t>(0xD7C0u + (codepoint >> 10u)),
14461  static_cast<std::uint16_t>(0xDC00u + (codepoint & 0x3FFu)));
14462  bytes += 12;
14463  }
14464  }
14465  else
14466  {
14467  // copy byte to buffer (all previous bytes
14468  // been copied have in default case above)
14469  string_buffer[bytes++] = s[i];
14470  }
14471  break;
14472  }
14473  }
14474 
14475  // write buffer and reset index; there must be 13 bytes
14476  // left, as this is the maximal number of bytes to be
14477  // written ("\uxxxx\uxxxx\0") for one code point
14478  if (string_buffer.size() - bytes < 13)
14479  {
14480  o->write_characters(string_buffer.data(), bytes);
14481  bytes = 0;
14482  }
14483 
14484  // remember the byte position of this accept
14485  bytes_after_last_accept = bytes;
14486  undumped_chars = 0;
14487  break;
14488  }
14489 
14490  case UTF8_REJECT: // decode found invalid UTF-8 byte
14491  {
14492  switch (error_handler)
14493  {
14494  case error_handler_t::strict:
14495  {
14496  std::string sn(3, '\0');
14497  (std::snprintf)(&sn[0], sn.size(), "%.2X", byte);
14498  JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn));
14499  }
14500 
14501  case error_handler_t::ignore:
14502  case error_handler_t::replace:
14503  {
14504  // in case we saw this character the first time, we
14505  // would like to read it again, because the byte
14506  // may be OK for itself, but just not OK for the
14507  // previous sequence
14508  if (undumped_chars > 0)
14509  {
14510  --i;
14511  }
14512 
14513  // reset length buffer to the last accepted index;
14514  // thus removing/ignoring the invalid characters
14515  bytes = bytes_after_last_accept;
14516 
14517  if (error_handler == error_handler_t::replace)
14518  {
14519  // add a replacement character
14520  if (ensure_ascii)
14521  {
14522  string_buffer[bytes++] = '\\';
14523  string_buffer[bytes++] = 'u';
14524  string_buffer[bytes++] = 'f';
14525  string_buffer[bytes++] = 'f';
14526  string_buffer[bytes++] = 'f';
14527  string_buffer[bytes++] = 'd';
14528  }
14529  else
14530  {
14531  string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xEF');
14532  string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBF');
14533  string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBD');
14534  }
14535 
14536  // write buffer and reset index; there must be 13 bytes
14537  // left, as this is the maximal number of bytes to be
14538  // written ("\uxxxx\uxxxx\0") for one code point
14539  if (string_buffer.size() - bytes < 13)
14540  {
14541  o->write_characters(string_buffer.data(), bytes);
14542  bytes = 0;
14543  }
14544 
14545  bytes_after_last_accept = bytes;
14546  }
14547 
14548  undumped_chars = 0;
14549 
14550  // continue processing the string
14551  state = UTF8_ACCEPT;
14552  break;
14553  }
14554 
14555  default: // LCOV_EXCL_LINE
14556  assert(false); // LCOV_EXCL_LINE
14557  }
14558  break;
14559  }
14560 
14561  default: // decode found yet incomplete multi-byte code point
14562  {
14563  if (not ensure_ascii)
14564  {
14565  // code point will not be escaped - copy byte to buffer
14566  string_buffer[bytes++] = s[i];
14567  }
14568  ++undumped_chars;
14569  break;
14570  }
14571  }
14572  }
14573 
14574  // we finished processing the string
14575  if (JSON_HEDLEY_LIKELY(state == UTF8_ACCEPT))
14576  {
14577  // write buffer
14578  if (bytes > 0)
14579  {
14580  o->write_characters(string_buffer.data(), bytes);
14581  }
14582  }
14583  else
14584  {
14585  // we finish reading, but do not accept: string was incomplete
14586  switch (error_handler)
14587  {
14588  case error_handler_t::strict:
14589  {
14590  std::string sn(3, '\0');
14591  (std::snprintf)(&sn[0], sn.size(), "%.2X", static_cast<std::uint8_t>(s.back()));
14592  JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + sn));
14593  }
14594 
14595  case error_handler_t::ignore:
14596  {
14597  // write all accepted bytes
14598  o->write_characters(string_buffer.data(), bytes_after_last_accept);
14599  break;
14600  }
14601 
14602  case error_handler_t::replace:
14603  {
14604  // write all accepted bytes
14605  o->write_characters(string_buffer.data(), bytes_after_last_accept);
14606  // add a replacement character
14607  if (ensure_ascii)
14608  {
14609  o->write_characters("\\ufffd", 6);
14610  }
14611  else
14612  {
14613  o->write_characters("\xEF\xBF\xBD", 3);
14614  }
14615  break;
14616  }
14617 
14618  default: // LCOV_EXCL_LINE
14619  assert(false); // LCOV_EXCL_LINE
14620  }
14621  }
14622  }
14623 
14632  inline unsigned int count_digits(number_unsigned_t x) noexcept
14633  {
14634  unsigned int n_digits = 1;
14635  for (;;)
14636  {
14637  if (x < 10)
14638  {
14639  return n_digits;
14640  }
14641  if (x < 100)
14642  {
14643  return n_digits + 1;
14644  }
14645  if (x < 1000)
14646  {
14647  return n_digits + 2;
14648  }
14649  if (x < 10000)
14650  {
14651  return n_digits + 3;
14652  }
14653  x = x / 10000u;
14654  n_digits += 4;
14655  }
14656  }
14657 
14667  template<typename NumberType, detail::enable_if_t<
14670  int> = 0>
14671  void dump_integer(NumberType x)
14672  {
14673  static constexpr std::array<std::array<char, 2>, 100> digits_to_99
14674  {
14675  {
14676  {{'0', '0'}}, {{'0', '1'}}, {{'0', '2'}}, {{'0', '3'}}, {{'0', '4'}}, {{'0', '5'}}, {{'0', '6'}}, {{'0', '7'}}, {{'0', '8'}}, {{'0', '9'}},
14677  {{'1', '0'}}, {{'1', '1'}}, {{'1', '2'}}, {{'1', '3'}}, {{'1', '4'}}, {{'1', '5'}}, {{'1', '6'}}, {{'1', '7'}}, {{'1', '8'}}, {{'1', '9'}},
14678  {{'2', '0'}}, {{'2', '1'}}, {{'2', '2'}}, {{'2', '3'}}, {{'2', '4'}}, {{'2', '5'}}, {{'2', '6'}}, {{'2', '7'}}, {{'2', '8'}}, {{'2', '9'}},
14679  {{'3', '0'}}, {{'3', '1'}}, {{'3', '2'}}, {{'3', '3'}}, {{'3', '4'}}, {{'3', '5'}}, {{'3', '6'}}, {{'3', '7'}}, {{'3', '8'}}, {{'3', '9'}},
14680  {{'4', '0'}}, {{'4', '1'}}, {{'4', '2'}}, {{'4', '3'}}, {{'4', '4'}}, {{'4', '5'}}, {{'4', '6'}}, {{'4', '7'}}, {{'4', '8'}}, {{'4', '9'}},
14681  {{'5', '0'}}, {{'5', '1'}}, {{'5', '2'}}, {{'5', '3'}}, {{'5', '4'}}, {{'5', '5'}}, {{'5', '6'}}, {{'5', '7'}}, {{'5', '8'}}, {{'5', '9'}},
14682  {{'6', '0'}}, {{'6', '1'}}, {{'6', '2'}}, {{'6', '3'}}, {{'6', '4'}}, {{'6', '5'}}, {{'6', '6'}}, {{'6', '7'}}, {{'6', '8'}}, {{'6', '9'}},
14683  {{'7', '0'}}, {{'7', '1'}}, {{'7', '2'}}, {{'7', '3'}}, {{'7', '4'}}, {{'7', '5'}}, {{'7', '6'}}, {{'7', '7'}}, {{'7', '8'}}, {{'7', '9'}},
14684  {{'8', '0'}}, {{'8', '1'}}, {{'8', '2'}}, {{'8', '3'}}, {{'8', '4'}}, {{'8', '5'}}, {{'8', '6'}}, {{'8', '7'}}, {{'8', '8'}}, {{'8', '9'}},
14685  {{'9', '0'}}, {{'9', '1'}}, {{'9', '2'}}, {{'9', '3'}}, {{'9', '4'}}, {{'9', '5'}}, {{'9', '6'}}, {{'9', '7'}}, {{'9', '8'}}, {{'9', '9'}},
14686  }
14687  };
14688 
14689  // special case for "0"
14690  if (x == 0)
14691  {
14692  o->write_character('0');
14693  return;
14694  }
14695 
14696  // use a pointer to fill the buffer
14697  auto buffer_ptr = number_buffer.begin();
14698 
14699  const bool is_negative = std::is_same<NumberType, number_integer_t>::value and not(x >= 0); // see issue #755
14700  number_unsigned_t abs_value;
14701 
14702  unsigned int n_chars;
14703 
14704  if (is_negative)
14705  {
14706  *buffer_ptr = '-';
14707  abs_value = remove_sign(x);
14708 
14709  // account one more byte for the minus sign
14710  n_chars = 1 + count_digits(abs_value);
14711  }
14712  else
14713  {
14714  abs_value = static_cast<number_unsigned_t>(x);
14715  n_chars = count_digits(abs_value);
14716  }
14717 
14718  // spare 1 byte for '\0'
14719  assert(n_chars < number_buffer.size() - 1);
14720 
14721  // jump to the end to generate the string from backward
14722  // so we later avoid reversing the result
14723  buffer_ptr += n_chars;
14724 
14725  // Fast int2ascii implementation inspired by "Fastware" talk by Andrei Alexandrescu
14726  // See: https://www.youtube.com/watch?v=o4-CwDo2zpg
14727  while (abs_value >= 100)
14728  {
14729  const auto digits_index = static_cast<unsigned>((abs_value % 100));
14730  abs_value /= 100;
14731  *(--buffer_ptr) = digits_to_99[digits_index][1];
14732  *(--buffer_ptr) = digits_to_99[digits_index][0];
14733  }
14734 
14735  if (abs_value >= 10)
14736  {
14737  const auto digits_index = static_cast<unsigned>(abs_value);
14738  *(--buffer_ptr) = digits_to_99[digits_index][1];
14739  *(--buffer_ptr) = digits_to_99[digits_index][0];
14740  }
14741  else
14742  {
14743  *(--buffer_ptr) = static_cast<char>('0' + abs_value);
14744  }
14745 
14746  o->write_characters(number_buffer.data(), n_chars);
14747  }
14748 
14757  void dump_float(number_float_t x)
14758  {
14759  // NaN / inf
14760  if (not std::isfinite(x))
14761  {
14762  o->write_characters("null", 4);
14763  return;
14764  }
14765 
14766  // If number_float_t is an IEEE-754 single or double precision number,
14767  // use the Grisu2 algorithm to produce short numbers which are
14768  // guaranteed to round-trip, using strtof and strtod, resp.
14769  //
14770  // NB: The test below works if <long double> == <double>.
14771  static constexpr bool is_ieee_single_or_double
14772  = (std::numeric_limits<number_float_t>::is_iec559 and std::numeric_limits<number_float_t>::digits == 24 and std::numeric_limits<number_float_t>::max_exponent == 128) or
14773  (std::numeric_limits<number_float_t>::is_iec559 and std::numeric_limits<number_float_t>::digits == 53 and std::numeric_limits<number_float_t>::max_exponent == 1024);
14774 
14775  dump_float(x, std::integral_constant<bool, is_ieee_single_or_double>());
14776  }
14777 
14778  void dump_float(number_float_t x, std::true_type /*is_ieee_single_or_double*/)
14779  {
14780  char* begin = number_buffer.data();
14781  char* end = ::nlohmann::detail::to_chars(begin, begin + number_buffer.size(), x);
14782 
14783  o->write_characters(begin, static_cast<size_t>(end - begin));
14784  }
14785 
14786  void dump_float(number_float_t x, std::false_type /*is_ieee_single_or_double*/)
14787  {
14788  // get number of digits for a float -> text -> float round-trip
14789  static constexpr auto d = std::numeric_limits<number_float_t>::max_digits10;
14790 
14791  // the actual conversion
14792  std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*g", d, x);
14793 
14794  // negative value indicates an error
14795  assert(len > 0);
14796  // check if buffer was large enough
14797  assert(static_cast<std::size_t>(len) < number_buffer.size());
14798 
14799  // erase thousands separator
14800  if (thousands_sep != '\0')
14801  {
14802  const auto end = std::remove(number_buffer.begin(),
14803  number_buffer.begin() + len, thousands_sep);
14804  std::fill(end, number_buffer.end(), '\0');
14805  assert((end - number_buffer.begin()) <= len);
14806  len = (end - number_buffer.begin());
14807  }
14808 
14809  // convert decimal point to '.'
14810  if (decimal_point != '\0' and decimal_point != '.')
14811  {
14812  const auto dec_pos = std::find(number_buffer.begin(), number_buffer.end(), decimal_point);
14813  if (dec_pos != number_buffer.end())
14814  {
14815  *dec_pos = '.';
14816  }
14817  }
14818 
14819  o->write_characters(number_buffer.data(), static_cast<std::size_t>(len));
14820 
14821  // determine if need to append ".0"
14822  const bool value_is_int_like =
14823  std::none_of(number_buffer.begin(), number_buffer.begin() + len + 1,
14824  [](char c)
14825  {
14826  return c == '.' or c == 'e';
14827  });
14828 
14829  if (value_is_int_like)
14830  {
14831  o->write_characters(".0", 2);
14832  }
14833  }
14834 
14856  static std::uint8_t decode(std::uint8_t& state, std::uint32_t& codep, const std::uint8_t byte) noexcept
14857  {
14858  static const std::array<std::uint8_t, 400> utf8d =
14859  {
14860  {
14861  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 00..1F
14862  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20..3F
14863  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 40..5F
14864  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 60..7F
14865  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // 80..9F
14866  7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // A0..BF
14867  8, 8, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // C0..DF
14868  0xA, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3, // E0..EF
14869  0xB, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, // F0..FF
14870  0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, 0x6, 0x1, 0x1, 0x1, 0x1, // s0..s0
14871  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, // s1..s2
14872  1, 2, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, // s3..s4
14873  1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, // s5..s6
14874  1, 3, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // s7..s8
14875  }
14876  };
14877 
14878  const std::uint8_t type = utf8d[byte];
14879 
14880  codep = (state != UTF8_ACCEPT)
14881  ? (byte & 0x3fu) | (codep << 6u)
14882  : (0xFFu >> type) & (byte);
14883 
14884  state = utf8d[256u + state * 16u + type];
14885  return state;
14886  }
14887 
14888  /*
14889  * Overload to make the compiler happy while it is instantiating
14890  * dump_integer for number_unsigned_t.
14891  * Must never be called.
14892  */
14894  {
14895  assert(false); // LCOV_EXCL_LINE
14896  return x; // LCOV_EXCL_LINE
14897  }
14898 
14899  /*
14900  * Helper function for dump_integer
14901  *
14902  * This function takes a negative signed integer and returns its absolute
14903  * value as unsigned integer. The plus/minus shuffling is necessary as we can
14904  * not directly remove the sign of an arbitrary signed integer as the
14905  * absolute values of INT_MIN and INT_MAX are usually not the same. See
14906  * #1708 for details.
14907  */
14908  inline number_unsigned_t remove_sign(number_integer_t x) noexcept
14909  {
14910  assert(x < 0 and x < (std::numeric_limits<number_integer_t>::max)());
14911  return static_cast<number_unsigned_t>(-(x + 1)) + 1;
14912  }
14913 
14914  private:
14917 
14919  std::array<char, 64> number_buffer{{}};
14920 
14922  const std::lconv* loc = nullptr;
14924  const char thousands_sep = '\0';
14926  const char decimal_point = '\0';
14927 
14929  std::array<char, 512> string_buffer{{}};
14930 
14932  const char indent_char;
14935 
14938 };
14939 } // namespace detail
14940 } // namespace nlohmann
14941 
14942 // #include <nlohmann/detail/value_t.hpp>
14943 
14944 // #include <nlohmann/json_fwd.hpp>
14945 
14946 
14952 namespace nlohmann
14953 {
14954 
15038 {
15039  private:
15040  template<detail::value_t> friend struct detail::external_constructor;
15041  friend ::nlohmann::json_pointer<basic_json>;
15042  friend ::nlohmann::detail::parser<basic_json>;
15043  friend ::nlohmann::detail::serializer<basic_json>;
15044  template<typename BasicJsonType>
15045  friend class ::nlohmann::detail::iter_impl;
15046  template<typename BasicJsonType, typename CharType>
15047  friend class ::nlohmann::detail::binary_writer;
15048  template<typename BasicJsonType, typename SAX>
15049  friend class ::nlohmann::detail::binary_reader;
15050  template<typename BasicJsonType>
15051  friend class ::nlohmann::detail::json_sax_dom_parser;
15052  template<typename BasicJsonType>
15053  friend class ::nlohmann::detail::json_sax_dom_callback_parser;
15054 
15057 
15058  // convenience aliases for types residing in namespace detail;
15061 
15063  template<typename BasicJsonType>
15065  template<typename BasicJsonType>
15067  template<typename Iterator>
15070 
15071  template<typename CharType>
15073 
15076 
15078 
15079  public:
15083  template<typename T, typename SFINAE>
15084  using json_serializer = JSONSerializer<T, SFINAE>;
15088  using initializer_list_t = std::initializer_list<detail::json_ref<basic_json>>;
15089 
15093 
15095  // exceptions //
15097 
15101 
15114 
15116 
15117 
15119  // container types //
15121 
15126 
15129 
15133  using const_reference = const value_type&;
15134 
15136  using difference_type = std::ptrdiff_t;
15138  using size_type = std::size_t;
15139 
15141  using allocator_type = AllocatorType<basic_json>;
15142 
15144  using pointer = typename std::allocator_traits<allocator_type>::pointer;
15146  using const_pointer = typename std::allocator_traits<allocator_type>::const_pointer;
15147 
15156 
15158 
15159 
15163  static allocator_type get_allocator()
15164  {
15165  return allocator_type();
15166  }
15167 
15195  static basic_json meta()
15196  {
15197  basic_json result;
15198 
15199  result["copyright"] = "(C) 2013-2017 Niels Lohmann";
15200  result["name"] = "JSON for Modern C++";
15201  result["url"] = "https://github.com/nlohmann/json";
15202  result["version"]["string"] =
15206  result["version"]["major"] = NLOHMANN_JSON_VERSION_MAJOR;
15207  result["version"]["minor"] = NLOHMANN_JSON_VERSION_MINOR;
15208  result["version"]["patch"] = NLOHMANN_JSON_VERSION_PATCH;
15209 
15210 #ifdef _WIN32
15211  result["platform"] = "win32";
15212 #elif defined __linux__
15213  result["platform"] = "linux";
15214 #elif defined __APPLE__
15215  result["platform"] = "apple";
15216 #elif defined __unix__
15217  result["platform"] = "unix";
15218 #else
15219  result["platform"] = "unknown";
15220 #endif
15221 
15222 #if defined(__ICC) || defined(__INTEL_COMPILER)
15223  result["compiler"] = {{"family", "icc"}, {"version", __INTEL_COMPILER}};
15224 #elif defined(__clang__)
15225  result["compiler"] = {{"family", "clang"}, {"version", __clang_version__}};
15226 #elif defined(__GNUC__) || defined(__GNUG__)
15227  result["compiler"] = {{"family", "gcc"}, {"version", std::to_string(__GNUC__) + "." + std::to_string(__GNUC_MINOR__) + "." + std::to_string(__GNUC_PATCHLEVEL__)}};
15228 #elif defined(__HP_cc) || defined(__HP_aCC)
15229  result["compiler"] = "hp"
15230 #elif defined(__IBMCPP__)
15231  result["compiler"] = {{"family", "ilecpp"}, {"version", __IBMCPP__}};
15232 #elif defined(_MSC_VER)
15233  result["compiler"] = {{"family", "msvc"}, {"version", _MSC_VER}};
15234 #elif defined(__PGI)
15235  result["compiler"] = {{"family", "pgcpp"}, {"version", __PGI}};
15236 #elif defined(__SUNPRO_CC)
15237  result["compiler"] = {{"family", "sunpro"}, {"version", __SUNPRO_CC}};
15238 #else
15239  result["compiler"] = {{"family", "unknown"}, {"version", "unknown"}};
15240 #endif
15241 
15242 #ifdef __cplusplus
15243  result["compiler"]["c++"] = std::to_string(__cplusplus);
15244 #else
15245  result["compiler"]["c++"] = "unknown";
15246 #endif
15247  return result;
15248  }
15249 
15250 
15252  // JSON value data types //
15254 
15259 
15260 #if defined(JSON_HAS_CPP_14)
15261  // Use transparent comparator if possible, combined with perfect forwarding
15262  // on find() and count() calls prevents unnecessary string construction.
15263  using object_comparator_t = std::less<>;
15264 #else
15265  using object_comparator_t = std::less<StringType>;
15266 #endif
15267 
15351  using object_t = ObjectType<StringType,
15352  basic_json,
15354  AllocatorType<std::pair<const StringType,
15356 
15401  using array_t = ArrayType<basic_json, AllocatorType<basic_json>>;
15402 
15454  using string_t = StringType;
15455 
15480  using boolean_t = BooleanType;
15481 
15552  using number_integer_t = NumberIntegerType;
15553 
15623  using number_unsigned_t = NumberUnsignedType;
15624 
15691  using number_float_t = NumberFloatType;
15692 
15694 
15695  private:
15696 
15698  template<typename T, typename... Args>
15700  static T* create(Args&& ... args)
15701  {
15702  AllocatorType<T> alloc;
15703  using AllocatorTraits = std::allocator_traits<AllocatorType<T>>;
15704 
15705  auto deleter = [&](T * object)
15706  {
15707  AllocatorTraits::deallocate(alloc, object, 1);
15708  };
15709  std::unique_ptr<T, decltype(deleter)> object(AllocatorTraits::allocate(alloc, 1), deleter);
15710  AllocatorTraits::construct(alloc, object.get(), std::forward<Args>(args)...);
15711  assert(object != nullptr);
15712  return object.release();
15713  }
15714 
15716  // JSON value storage //
15718 
15744  {
15759 
15761  json_value() = default;
15763  json_value(boolean_t v) noexcept : boolean(v) {}
15765  json_value(number_integer_t v) noexcept : number_integer(v) {}
15767  json_value(number_unsigned_t v) noexcept : number_unsigned(v) {}
15769  json_value(number_float_t v) noexcept : number_float(v) {}
15772  {
15773  switch (t)
15774  {
15775  case value_t::object:
15776  {
15777  object = create<object_t>();
15778  break;
15779  }
15780 
15781  case value_t::array:
15782  {
15783  array = create<array_t>();
15784  break;
15785  }
15786 
15787  case value_t::string:
15788  {
15789  string = create<string_t>("");
15790  break;
15791  }
15792 
15793  case value_t::boolean:
15794  {
15795  boolean = boolean_t(false);
15796  break;
15797  }
15798 
15799  case value_t::number_integer:
15800  {
15801  number_integer = number_integer_t(0);
15802  break;
15803  }
15804 
15805  case value_t::number_unsigned:
15806  {
15807  number_unsigned = number_unsigned_t(0);
15808  break;
15809  }
15810 
15811  case value_t::number_float:
15812  {
15813  number_float = number_float_t(0.0);
15814  break;
15815  }
15816 
15817  case value_t::null:
15818  {
15819  object = nullptr; // silence warning, see #821
15820  break;
15821  }
15822 
15823  default:
15824  {
15825  object = nullptr; // silence warning, see #821
15826  if (JSON_HEDLEY_UNLIKELY(t == value_t::null))
15827  {
15828  JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.7.3")); // LCOV_EXCL_LINE
15829  }
15830  break;
15831  }
15832  }
15833  }
15834 
15837  {
15838  string = create<string_t>(value);
15839  }
15840 
15843  {
15844  string = create<string_t>(std::move(value));
15845  }
15846 
15849  {
15850  object = create<object_t>(value);
15851  }
15852 
15855  {
15856  object = create<object_t>(std::move(value));
15857  }
15858 
15861  {
15862  array = create<array_t>(value);
15863  }
15864 
15867  {
15868  array = create<array_t>(std::move(value));
15869  }
15870 
15871  void destroy(value_t t) noexcept
15872  {
15873  // flatten the current json_value to a heap-allocated stack
15874  std::vector<basic_json> stack;
15875 
15876  // move the top-level items to stack
15877  if (t == value_t::array)
15878  {
15879  stack.reserve(array->size());
15880  std::move(array->begin(), array->end(), std::back_inserter(stack));
15881  }
15882  else if (t == value_t::object)
15883  {
15884  stack.reserve(object->size());
15885  for (auto&& it : *object)
15886  {
15887  stack.push_back(std::move(it.second));
15888  }
15889  }
15890 
15891  while (not stack.empty())
15892  {
15893  // move the last item to local variable to be processed
15894  basic_json current_item(std::move(stack.back()));
15895  stack.pop_back();
15896 
15897  // if current_item is array/object, move
15898  // its children to the stack to be processed later
15899  if (current_item.is_array())
15900  {
15901  std::move(current_item.m_value.array->begin(), current_item.m_value.array->end(),
15902  std::back_inserter(stack));
15903 
15904  current_item.m_value.array->clear();
15905  }
15906  else if (current_item.is_object())
15907  {
15908  for (auto&& it : *current_item.m_value.object)
15909  {
15910  stack.push_back(std::move(it.second));
15911  }
15912 
15913  current_item.m_value.object->clear();
15914  }
15915 
15916  // it's now safe that current_item get destructed
15917  // since it doesn't have any children
15918  }
15919 
15920  switch (t)
15921  {
15922  case value_t::object:
15923  {
15924  AllocatorType<object_t> alloc;
15925  std::allocator_traits<decltype(alloc)>::destroy(alloc, object);
15926  std::allocator_traits<decltype(alloc)>::deallocate(alloc, object, 1);
15927  break;
15928  }
15929 
15930  case value_t::array:
15931  {
15932  AllocatorType<array_t> alloc;
15933  std::allocator_traits<decltype(alloc)>::destroy(alloc, array);
15934  std::allocator_traits<decltype(alloc)>::deallocate(alloc, array, 1);
15935  break;
15936  }
15937 
15938  case value_t::string:
15939  {
15940  AllocatorType<string_t> alloc;
15941  std::allocator_traits<decltype(alloc)>::destroy(alloc, string);
15942  std::allocator_traits<decltype(alloc)>::deallocate(alloc, string, 1);
15943  break;
15944  }
15945 
15946  default:
15947  {
15948  break;
15949  }
15950  }
15951  }
15952  };
15953 
15963  void assert_invariant() const noexcept
15964  {
15965  assert(m_type != value_t::object or m_value.object != nullptr);
15966  assert(m_type != value_t::array or m_value.array != nullptr);
15967  assert(m_type != value_t::string or m_value.string != nullptr);
15968  }
15969 
15970  public:
15972  // JSON parser callback //
15974 
15991 
16042 
16044  // constructors //
16046 
16051 
16082  : m_type(v), m_value(v)
16083  {
16084  assert_invariant();
16085  }
16086 
16105  basic_json(std::nullptr_t = nullptr) noexcept
16106  : basic_json(value_t::null)
16107  {
16108  assert_invariant();
16109  }
16110 
16168  template <typename CompatibleType,
16169  typename U = detail::uncvref_t<CompatibleType>,
16172  basic_json(CompatibleType && val) noexcept(noexcept(
16173  JSONSerializer<U>::to_json(std::declval<basic_json_t&>(),
16174  std::forward<CompatibleType>(val))))
16175  {
16176  JSONSerializer<U>::to_json(*this, std::forward<CompatibleType>(val));
16177  assert_invariant();
16178  }
16179 
16206  template <typename BasicJsonType,
16209  basic_json(const BasicJsonType& val)
16210  {
16211  using other_boolean_t = typename BasicJsonType::boolean_t;
16212  using other_number_float_t = typename BasicJsonType::number_float_t;
16213  using other_number_integer_t = typename BasicJsonType::number_integer_t;
16214  using other_number_unsigned_t = typename BasicJsonType::number_unsigned_t;
16215  using other_string_t = typename BasicJsonType::string_t;
16216  using other_object_t = typename BasicJsonType::object_t;
16217  using other_array_t = typename BasicJsonType::array_t;
16218 
16219  switch (val.type())
16220  {
16221  case value_t::boolean:
16222  JSONSerializer<other_boolean_t>::to_json(*this, val.template get<other_boolean_t>());
16223  break;
16224  case value_t::number_float:
16225  JSONSerializer<other_number_float_t>::to_json(*this, val.template get<other_number_float_t>());
16226  break;
16227  case value_t::number_integer:
16228  JSONSerializer<other_number_integer_t>::to_json(*this, val.template get<other_number_integer_t>());
16229  break;
16230  case value_t::number_unsigned:
16231  JSONSerializer<other_number_unsigned_t>::to_json(*this, val.template get<other_number_unsigned_t>());
16232  break;
16233  case value_t::string:
16234  JSONSerializer<other_string_t>::to_json(*this, val.template get_ref<const other_string_t&>());
16235  break;
16236  case value_t::object:
16237  JSONSerializer<other_object_t>::to_json(*this, val.template get_ref<const other_object_t&>());
16238  break;
16239  case value_t::array:
16240  JSONSerializer<other_array_t>::to_json(*this, val.template get_ref<const other_array_t&>());
16241  break;
16242  case value_t::null:
16243  *this = nullptr;
16244  break;
16245  case value_t::discarded:
16246  m_type = value_t::discarded;
16247  break;
16248  default: // LCOV_EXCL_LINE
16249  assert(false); // LCOV_EXCL_LINE
16250  }
16251  assert_invariant();
16252  }
16253 
16329  bool type_deduction = true,
16330  value_t manual_type = value_t::array)
16331  {
16332  // check if each element is an array with two elements whose first
16333  // element is a string
16334  bool is_an_object = std::all_of(init.begin(), init.end(),
16335  [](const detail::json_ref<basic_json>& element_ref)
16336  {
16337  return element_ref->is_array() and element_ref->size() == 2 and (*element_ref)[0].is_string();
16338  });
16339 
16340  // adjust type if type deduction is not wanted
16341  if (not type_deduction)
16342  {
16343  // if array is wanted, do not create an object though possible
16344  if (manual_type == value_t::array)
16345  {
16346  is_an_object = false;
16347  }
16348 
16349  // if object is wanted but impossible, throw an exception
16350  if (JSON_HEDLEY_UNLIKELY(manual_type == value_t::object and not is_an_object))
16351  {
16352  JSON_THROW(type_error::create(301, "cannot create object from initializer list"));
16353  }
16354  }
16355 
16356  if (is_an_object)
16357  {
16358  // the initializer list is a list of pairs -> create object
16359  m_type = value_t::object;
16360  m_value = value_t::object;
16361 
16362  std::for_each(init.begin(), init.end(), [this](const detail::json_ref<basic_json>& element_ref)
16363  {
16364  auto element = element_ref.moved_or_copied();
16365  m_value.object->emplace(
16366  std::move(*((*element.m_value.array)[0].m_value.string)),
16367  std::move((*element.m_value.array)[1]));
16368  });
16369  }
16370  else
16371  {
16372  // the initializer list describes an array -> create array
16373  m_type = value_t::array;
16374  m_value.array = create<array_t>(init.begin(), init.end());
16375  }
16376 
16377  assert_invariant();
16378  }
16379 
16419  {
16420  return basic_json(init, false, value_t::array);
16421  }
16422 
16462  static basic_json object(initializer_list_t init = {})
16463  {
16464  return basic_json(init, false, value_t::object);
16465  }
16466 
16490  : m_type(value_t::array)
16491  {
16492  m_value.array = create<array_t>(cnt, val);
16493  assert_invariant();
16494  }
16495 
16551  template<class InputIT, typename std::enable_if<
16554  basic_json(InputIT first, InputIT last)
16555  {
16556  assert(first.m_object != nullptr);
16557  assert(last.m_object != nullptr);
16558 
16559  // make sure iterator fits the current value
16560  if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
16561  {
16562  JSON_THROW(invalid_iterator::create(201, "iterators are not compatible"));
16563  }
16564 
16565  // copy type from first iterator
16566  m_type = first.m_object->m_type;
16567 
16568  // check if iterator range is complete for primitive values
16569  switch (m_type)
16570  {
16571  case value_t::boolean:
16572  case value_t::number_float:
16573  case value_t::number_integer:
16574  case value_t::number_unsigned:
16575  case value_t::string:
16576  {
16577  if (JSON_HEDLEY_UNLIKELY(not first.m_it.primitive_iterator.is_begin()
16578  or not last.m_it.primitive_iterator.is_end()))
16579  {
16580  JSON_THROW(invalid_iterator::create(204, "iterators out of range"));
16581  }
16582  break;
16583  }
16584 
16585  default:
16586  break;
16587  }
16588 
16589  switch (m_type)
16590  {
16591  case value_t::number_integer:
16592  {
16593  m_value.number_integer = first.m_object->m_value.number_integer;
16594  break;
16595  }
16596 
16597  case value_t::number_unsigned:
16598  {
16599  m_value.number_unsigned = first.m_object->m_value.number_unsigned;
16600  break;
16601  }
16602 
16603  case value_t::number_float:
16604  {
16605  m_value.number_float = first.m_object->m_value.number_float;
16606  break;
16607  }
16608 
16609  case value_t::boolean:
16610  {
16611  m_value.boolean = first.m_object->m_value.boolean;
16612  break;
16613  }
16614 
16615  case value_t::string:
16616  {
16617  m_value = *first.m_object->m_value.string;
16618  break;
16619  }
16620 
16621  case value_t::object:
16622  {
16623  m_value.object = create<object_t>(first.m_it.object_iterator,
16624  last.m_it.object_iterator);
16625  break;
16626  }
16627 
16628  case value_t::array:
16629  {
16630  m_value.array = create<array_t>(first.m_it.array_iterator,
16631  last.m_it.array_iterator);
16632  break;
16633  }
16634 
16635  default:
16636  JSON_THROW(invalid_iterator::create(206, "cannot construct with iterators from " +
16637  std::string(first.m_object->type_name())));
16638  }
16639 
16640  assert_invariant();
16641  }
16642 
16643 
16645  // other constructors and destructor //
16647 
16648  template <typename JsonRef,
16650  std::is_same<typename JsonRef::value_type, basic_json>>::value, int> = 0 >
16651  basic_json(const JsonRef& ref) : basic_json(ref.moved_or_copied()) {}
16652 
16678  basic_json(const basic_json& other)
16679  : m_type(other.m_type)
16680  {
16681  // check of passed value is valid
16682  other.assert_invariant();
16683 
16684  switch (m_type)
16685  {
16686  case value_t::object:
16687  {
16688  m_value = *other.m_value.object;
16689  break;
16690  }
16691 
16692  case value_t::array:
16693  {
16694  m_value = *other.m_value.array;
16695  break;
16696  }
16697 
16698  case value_t::string:
16699  {
16700  m_value = *other.m_value.string;
16701  break;
16702  }
16703 
16704  case value_t::boolean:
16705  {
16706  m_value = other.m_value.boolean;
16707  break;
16708  }
16709 
16710  case value_t::number_integer:
16711  {
16712  m_value = other.m_value.number_integer;
16713  break;
16714  }
16715 
16716  case value_t::number_unsigned:
16717  {
16718  m_value = other.m_value.number_unsigned;
16719  break;
16720  }
16721 
16722  case value_t::number_float:
16723  {
16724  m_value = other.m_value.number_float;
16725  break;
16726  }
16727 
16728  default:
16729  break;
16730  }
16731 
16732  assert_invariant();
16733  }
16734 
16761  basic_json(basic_json&& other) noexcept
16762  : m_type(std::move(other.m_type)),
16763  m_value(std::move(other.m_value))
16764  {
16765  // check that passed value is valid
16766  other.assert_invariant();
16767 
16768  // invalidate payload
16769  other.m_type = value_t::null;
16770  other.m_value = {};
16771 
16772  assert_invariant();
16773  }
16774 
16798  basic_json& operator=(basic_json other) noexcept (
16803  )
16804  {
16805  // check that passed value is valid
16806  other.assert_invariant();
16807 
16808  using std::swap;
16809  swap(m_type, other.m_type);
16810  swap(m_value, other.m_value);
16811 
16812  assert_invariant();
16813  return *this;
16814  }
16815 
16831  ~basic_json() noexcept
16832  {
16833  assert_invariant();
16834  m_value.destroy(m_type);
16835  }
16836 
16838 
16839  public:
16841  // object inspection //
16843 
16847 
16889  string_t dump(const int indent = -1,
16890  const char indent_char = ' ',
16891  const bool ensure_ascii = false,
16892  const error_handler_t error_handler = error_handler_t::strict) const
16893  {
16894  string_t result;
16895  serializer s(detail::output_adapter<char, string_t>(result), indent_char, error_handler);
16896 
16897  if (indent >= 0)
16898  {
16899  s.dump(*this, true, ensure_ascii, static_cast<unsigned int>(indent));
16900  }
16901  else
16902  {
16903  s.dump(*this, false, ensure_ascii, 0);
16904  }
16905 
16906  return result;
16907  }
16908 
16941  constexpr value_t type() const noexcept
16942  {
16943  return m_type;
16944  }
16945 
16971  constexpr bool is_primitive() const noexcept
16972  {
16973  return is_null() or is_string() or is_boolean() or is_number();
16974  }
16975 
16998  constexpr bool is_structured() const noexcept
16999  {
17000  return is_array() or is_object();
17001  }
17002 
17020  constexpr bool is_null() const noexcept
17021  {
17022  return m_type == value_t::null;
17023  }
17024 
17042  constexpr bool is_boolean() const noexcept
17043  {
17044  return m_type == value_t::boolean;
17045  }
17046 
17072  constexpr bool is_number() const noexcept
17073  {
17074  return is_number_integer() or is_number_float();
17075  }
17076 
17101  constexpr bool is_number_integer() const noexcept
17102  {
17103  return m_type == value_t::number_integer or m_type == value_t::number_unsigned;
17104  }
17105 
17129  constexpr bool is_number_unsigned() const noexcept
17130  {
17131  return m_type == value_t::number_unsigned;
17132  }
17133 
17157  constexpr bool is_number_float() const noexcept
17158  {
17159  return m_type == value_t::number_float;
17160  }
17161 
17179  constexpr bool is_object() const noexcept
17180  {
17181  return m_type == value_t::object;
17182  }
17183 
17201  constexpr bool is_array() const noexcept
17202  {
17203  return m_type == value_t::array;
17204  }
17205 
17223  constexpr bool is_string() const noexcept
17224  {
17225  return m_type == value_t::string;
17226  }
17227 
17250  constexpr bool is_discarded() const noexcept
17251  {
17252  return m_type == value_t::discarded;
17253  }
17254 
17276  constexpr operator value_t() const noexcept
17277  {
17278  return m_type;
17279  }
17280 
17282 
17283  private:
17285  // value access //
17287 
17289  boolean_t get_impl(boolean_t* /*unused*/) const
17290  {
17291  if (JSON_HEDLEY_LIKELY(is_boolean()))
17292  {
17293  return m_value.boolean;
17294  }
17295 
17296  JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(type_name())));
17297  }
17298 
17300  object_t* get_impl_ptr(object_t* /*unused*/) noexcept
17301  {
17302  return is_object() ? m_value.object : nullptr;
17303  }
17304 
17306  constexpr const object_t* get_impl_ptr(const object_t* /*unused*/) const noexcept
17307  {
17308  return is_object() ? m_value.object : nullptr;
17309  }
17310 
17312  array_t* get_impl_ptr(array_t* /*unused*/) noexcept
17313  {
17314  return is_array() ? m_value.array : nullptr;
17315  }
17316 
17318  constexpr const array_t* get_impl_ptr(const array_t* /*unused*/) const noexcept
17319  {
17320  return is_array() ? m_value.array : nullptr;
17321  }
17322 
17324  string_t* get_impl_ptr(string_t* /*unused*/) noexcept
17325  {
17326  return is_string() ? m_value.string : nullptr;
17327  }
17328 
17330  constexpr const string_t* get_impl_ptr(const string_t* /*unused*/) const noexcept
17331  {
17332  return is_string() ? m_value.string : nullptr;
17333  }
17334 
17336  boolean_t* get_impl_ptr(boolean_t* /*unused*/) noexcept
17337  {
17338  return is_boolean() ? &m_value.boolean : nullptr;
17339  }
17340 
17342  constexpr const boolean_t* get_impl_ptr(const boolean_t* /*unused*/) const noexcept
17343  {
17344  return is_boolean() ? &m_value.boolean : nullptr;
17345  }
17346 
17348  number_integer_t* get_impl_ptr(number_integer_t* /*unused*/) noexcept
17349  {
17350  return is_number_integer() ? &m_value.number_integer : nullptr;
17351  }
17352 
17354  constexpr const number_integer_t* get_impl_ptr(const number_integer_t* /*unused*/) const noexcept
17355  {
17356  return is_number_integer() ? &m_value.number_integer : nullptr;
17357  }
17358 
17360  number_unsigned_t* get_impl_ptr(number_unsigned_t* /*unused*/) noexcept
17361  {
17362  return is_number_unsigned() ? &m_value.number_unsigned : nullptr;
17363  }
17364 
17366  constexpr const number_unsigned_t* get_impl_ptr(const number_unsigned_t* /*unused*/) const noexcept
17367  {
17368  return is_number_unsigned() ? &m_value.number_unsigned : nullptr;
17369  }
17370 
17372  number_float_t* get_impl_ptr(number_float_t* /*unused*/) noexcept
17373  {
17374  return is_number_float() ? &m_value.number_float : nullptr;
17375  }
17376 
17378  constexpr const number_float_t* get_impl_ptr(const number_float_t* /*unused*/) const noexcept
17379  {
17380  return is_number_float() ? &m_value.number_float : nullptr;
17381  }
17382 
17394  template<typename ReferenceType, typename ThisType>
17395  static ReferenceType get_ref_impl(ThisType& obj)
17396  {
17397  // delegate the call to get_ptr<>()
17398  auto ptr = obj.template get_ptr<typename std::add_pointer<ReferenceType>::type>();
17399 
17400  if (JSON_HEDLEY_LIKELY(ptr != nullptr))
17401  {
17402  return *ptr;
17403  }
17404 
17405  JSON_THROW(type_error::create(303, "incompatible ReferenceType for get_ref, actual type is " + std::string(obj.type_name())));
17406  }
17407 
17408  public:
17412 
17427  template<typename BasicJsonType, detail::enable_if_t<
17428  std::is_same<typename std::remove_const<BasicJsonType>::type, basic_json_t>::value,
17429  int> = 0>
17430  basic_json get() const
17431  {
17432  return *this;
17433  }
17434 
17450  template<typename BasicJsonType, detail::enable_if_t<
17453  BasicJsonType get() const
17454  {
17455  return *this;
17456  }
17457 
17497  template<typename ValueTypeCV, typename ValueType = detail::uncvref_t<ValueTypeCV>,
17498  detail::enable_if_t <
17499  not detail::is_basic_json<ValueType>::value and
17500  detail::has_from_json<basic_json_t, ValueType>::value and
17501  not detail::has_non_default_from_json<basic_json_t, ValueType>::value,
17502  int> = 0>
17503  ValueType get() const noexcept(noexcept(
17504  JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), std::declval<ValueType&>())))
17505  {
17506  // we cannot static_assert on ValueTypeCV being non-const, because
17507  // there is support for get<const basic_json_t>(), which is why we
17508  // still need the uncvref
17509  static_assert(not std::is_reference<ValueTypeCV>::value,
17510  "get() cannot be used with reference types, you might want to use get_ref()");
17512  "types must be DefaultConstructible when used with get()");
17513 
17514  ValueType ret;
17516  return ret;
17517  }
17518 
17550  template<typename ValueTypeCV, typename ValueType = detail::uncvref_t<ValueTypeCV>,
17551  detail::enable_if_t<not std::is_same<basic_json_t, ValueType>::value and
17552  detail::has_non_default_from_json<basic_json_t, ValueType>::value,
17553  int> = 0>
17554  ValueType get() const noexcept(noexcept(
17555  JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>())))
17556  {
17557  static_assert(not std::is_reference<ValueTypeCV>::value,
17558  "get() cannot be used with reference types, you might want to use get_ref()");
17560  }
17561 
17595  template<typename ValueType,
17599  int> = 0>
17600  ValueType & get_to(ValueType& v) const noexcept(noexcept(
17601  JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), v)))
17602  {
17604  return v;
17605  }
17606 
17607  template <
17608  typename T, std::size_t N,
17609  typename Array = T (&)[N],
17612  Array get_to(T (&v)[N]) const
17613  noexcept(noexcept(JSONSerializer<Array>::from_json(
17614  std::declval<const basic_json_t&>(), v)))
17615  {
17617  return v;
17618  }
17619 
17620 
17647  template<typename PointerType, typename std::enable_if<
17648  std::is_pointer<PointerType>::value, int>::type = 0>
17649  auto get_ptr() noexcept -> decltype(std::declval<basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
17650  {
17651  // delegate the call to get_impl_ptr<>()
17652  return get_impl_ptr(static_cast<PointerType>(nullptr));
17653  }
17654 
17659  template<typename PointerType, typename std::enable_if<
17661  std::is_const<typename std::remove_pointer<PointerType>::type>::value, int>::type = 0>
17662  constexpr auto get_ptr() const noexcept -> decltype(std::declval<const basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
17663  {
17664  // delegate the call to get_impl_ptr<>() const
17665  return get_impl_ptr(static_cast<PointerType>(nullptr));
17666  }
17667 
17695  template<typename PointerType, typename std::enable_if<
17696  std::is_pointer<PointerType>::value, int>::type = 0>
17697  auto get() noexcept -> decltype(std::declval<basic_json_t&>().template get_ptr<PointerType>())
17698  {
17699  // delegate the call to get_ptr
17700  return get_ptr<PointerType>();
17701  }
17702 
17707  template<typename PointerType, typename std::enable_if<
17708  std::is_pointer<PointerType>::value, int>::type = 0>
17709  constexpr auto get() const noexcept -> decltype(std::declval<const basic_json_t&>().template get_ptr<PointerType>())
17710  {
17711  // delegate the call to get_ptr
17712  return get_ptr<PointerType>();
17713  }
17714 
17741  template<typename ReferenceType, typename std::enable_if<
17743  ReferenceType get_ref()
17744  {
17745  // delegate call to get_ref_impl
17746  return get_ref_impl<ReferenceType>(*this);
17747  }
17748 
17753  template<typename ReferenceType, typename std::enable_if<
17755  std::is_const<typename std::remove_reference<ReferenceType>::type>::value, int>::type = 0>
17756  ReferenceType get_ref() const
17757  {
17758  // delegate call to get_ref_impl
17759  return get_ref_impl<ReferenceType>(*this);
17760  }
17761 
17791  template < typename ValueType, typename std::enable_if <
17793  not std::is_same<ValueType, detail::json_ref<basic_json>>::value and
17796  and not std::is_same<ValueType, std::initializer_list<typename string_t::value_type>>::value
17797 #if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) and _MSC_VER <= 1914))
17799 #endif
17801  , int >::type = 0 >
17802  operator ValueType() const
17803  {
17804  // delegate the call to get<>() const
17805  return get<ValueType>();
17806  }
17807 
17809 
17810 
17812  // element access //
17814 
17818 
17846  {
17847  // at only works for arrays
17848  if (JSON_HEDLEY_LIKELY(is_array()))
17849  {
17850  JSON_TRY
17851  {
17852  return m_value.array->at(idx);
17853  }
17854  JSON_CATCH (std::out_of_range&)
17855  {
17856  // create better exception explanation
17857  JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range"));
17858  }
17859  }
17860  else
17861  {
17862  JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name())));
17863  }
17864  }
17865 
17893  {
17894  // at only works for arrays
17895  if (JSON_HEDLEY_LIKELY(is_array()))
17896  {
17897  JSON_TRY
17898  {
17899  return m_value.array->at(idx);
17900  }
17901  JSON_CATCH (std::out_of_range&)
17902  {
17903  // create better exception explanation
17904  JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range"));
17905  }
17906  }
17907  else
17908  {
17909  JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name())));
17910  }
17911  }
17912 
17943  reference at(const typename object_t::key_type& key)
17944  {
17945  // at only works for objects
17946  if (JSON_HEDLEY_LIKELY(is_object()))
17947  {
17948  JSON_TRY
17949  {
17950  return m_value.object->at(key);
17951  }
17952  JSON_CATCH (std::out_of_range&)
17953  {
17954  // create better exception explanation
17955  JSON_THROW(out_of_range::create(403, "key '" + key + "' not found"));
17956  }
17957  }
17958  else
17959  {
17960  JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name())));
17961  }
17962  }
17963 
17994  const_reference at(const typename object_t::key_type& key) const
17995  {
17996  // at only works for objects
17997  if (JSON_HEDLEY_LIKELY(is_object()))
17998  {
17999  JSON_TRY
18000  {
18001  return m_value.object->at(key);
18002  }
18003  JSON_CATCH (std::out_of_range&)
18004  {
18005  // create better exception explanation
18006  JSON_THROW(out_of_range::create(403, "key '" + key + "' not found"));
18007  }
18008  }
18009  else
18010  {
18011  JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name())));
18012  }
18013  }
18014 
18040  reference operator[](size_type idx)
18041  {
18042  // implicitly convert null value to an empty array
18043  if (is_null())
18044  {
18045  m_type = value_t::array;
18046  m_value.array = create<array_t>();
18047  assert_invariant();
18048  }
18049 
18050  // operator[] only works for arrays
18051  if (JSON_HEDLEY_LIKELY(is_array()))
18052  {
18053  // fill up array with null values if given idx is outside range
18054  if (idx >= m_value.array->size())
18055  {
18056  m_value.array->insert(m_value.array->end(),
18057  idx - m_value.array->size() + 1,
18058  basic_json());
18059  }
18060 
18061  return m_value.array->operator[](idx);
18062  }
18063 
18064  JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name())));
18065  }
18066 
18086  const_reference operator[](size_type idx) const
18087  {
18088  // const operator[] only works for arrays
18089  if (JSON_HEDLEY_LIKELY(is_array()))
18090  {
18091  return m_value.array->operator[](idx);
18092  }
18093 
18094  JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name())));
18095  }
18096 
18124  reference operator[](const typename object_t::key_type& key)
18125  {
18126  // implicitly convert null value to an empty object
18127  if (is_null())
18128  {
18129  m_type = value_t::object;
18130  m_value.object = create<object_t>();
18131  assert_invariant();
18132  }
18133 
18134  // operator[] only works for objects
18135  if (JSON_HEDLEY_LIKELY(is_object()))
18136  {
18137  return m_value.object->operator[](key);
18138  }
18139 
18140  JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name())));
18141  }
18142 
18173  const_reference operator[](const typename object_t::key_type& key) const
18174  {
18175  // const operator[] only works for objects
18176  if (JSON_HEDLEY_LIKELY(is_object()))
18177  {
18178  assert(m_value.object->find(key) != m_value.object->end());
18179  return m_value.object->find(key)->second;
18180  }
18181 
18182  JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name())));
18183  }
18184 
18212  template<typename T>
18214  reference operator[](T* key)
18215  {
18216  // implicitly convert null to object
18217  if (is_null())
18218  {
18219  m_type = value_t::object;
18220  m_value = value_t::object;
18221  assert_invariant();
18222  }
18223 
18224  // at only works for objects
18225  if (JSON_HEDLEY_LIKELY(is_object()))
18226  {
18227  return m_value.object->operator[](key);
18228  }
18229 
18230  JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name())));
18231  }
18232 
18263  template<typename T>
18265  const_reference operator[](T* key) const
18266  {
18267  // at only works for objects
18268  if (JSON_HEDLEY_LIKELY(is_object()))
18269  {
18270  assert(m_value.object->find(key) != m_value.object->end());
18271  return m_value.object->find(key)->second;
18272  }
18273 
18274  JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name())));
18275  }
18276 
18327  template<class ValueType, typename std::enable_if<
18329  ValueType value(const typename object_t::key_type& key, const ValueType& default_value) const
18330  {
18331  // at only works for objects
18332  if (JSON_HEDLEY_LIKELY(is_object()))
18333  {
18334  // if key is found, return value and given default value otherwise
18335  const auto it = find(key);
18336  if (it != end())
18337  {
18338  return *it;
18339  }
18340 
18341  return default_value;
18342  }
18343 
18344  JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name())));
18345  }
18346 
18351  string_t value(const typename object_t::key_type& key, const char* default_value) const
18352  {
18353  return value(key, string_t(default_value));
18354  }
18355 
18399  template<class ValueType, typename std::enable_if<
18401  ValueType value(const json_pointer& ptr, const ValueType& default_value) const
18402  {
18403  // at only works for objects
18404  if (JSON_HEDLEY_LIKELY(is_object()))
18405  {
18406  // if pointer resolves a value, return it or use default value
18407  JSON_TRY
18408  {
18409  return ptr.get_checked(this);
18410  }
18412  {
18413  return default_value;
18414  }
18415  }
18416 
18417  JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name())));
18418  }
18419 
18425  string_t value(const json_pointer& ptr, const char* default_value) const
18426  {
18427  return value(ptr, string_t(default_value));
18428  }
18429 
18455  reference front()
18456  {
18457  return *begin();
18458  }
18459 
18463  const_reference front() const
18464  {
18465  return *cbegin();
18466  }
18467 
18499  reference back()
18500  {
18501  auto tmp = end();
18502  --tmp;
18503  return *tmp;
18504  }
18505 
18509  const_reference back() const
18510  {
18511  auto tmp = cend();
18512  --tmp;
18513  return *tmp;
18514  }
18515 
18562  template<class IteratorType, typename std::enable_if<
18565  = 0>
18566  IteratorType erase(IteratorType pos)
18567  {
18568  // make sure iterator fits the current value
18569  if (JSON_HEDLEY_UNLIKELY(this != pos.m_object))
18570  {
18571  JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value"));
18572  }
18573 
18574  IteratorType result = end();
18575 
18576  switch (m_type)
18577  {
18578  case value_t::boolean:
18579  case value_t::number_float:
18580  case value_t::number_integer:
18581  case value_t::number_unsigned:
18582  case value_t::string:
18583  {
18584  if (JSON_HEDLEY_UNLIKELY(not pos.m_it.primitive_iterator.is_begin()))
18585  {
18586  JSON_THROW(invalid_iterator::create(205, "iterator out of range"));
18587  }
18588 
18589  if (is_string())
18590  {
18591  AllocatorType<string_t> alloc;
18592  std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.string);
18593  std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.string, 1);
18594  m_value.string = nullptr;
18595  }
18596 
18597  m_type = value_t::null;
18598  assert_invariant();
18599  break;
18600  }
18601 
18602  case value_t::object:
18603  {
18604  result.m_it.object_iterator = m_value.object->erase(pos.m_it.object_iterator);
18605  break;
18606  }
18607 
18608  case value_t::array:
18609  {
18610  result.m_it.array_iterator = m_value.array->erase(pos.m_it.array_iterator);
18611  break;
18612  }
18613 
18614  default:
18615  JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name())));
18616  }
18617 
18618  return result;
18619  }
18620 
18667  template<class IteratorType, typename std::enable_if<
18670  = 0>
18671  IteratorType erase(IteratorType first, IteratorType last)
18672  {
18673  // make sure iterator fits the current value
18674  if (JSON_HEDLEY_UNLIKELY(this != first.m_object or this != last.m_object))
18675  {
18676  JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value"));
18677  }
18678 
18679  IteratorType result = end();
18680 
18681  switch (m_type)
18682  {
18683  case value_t::boolean:
18684  case value_t::number_float:
18685  case value_t::number_integer:
18686  case value_t::number_unsigned:
18687  case value_t::string:
18688  {
18689  if (JSON_HEDLEY_LIKELY(not first.m_it.primitive_iterator.is_begin()
18690  or not last.m_it.primitive_iterator.is_end()))
18691  {
18692  JSON_THROW(invalid_iterator::create(204, "iterators out of range"));
18693  }
18694 
18695  if (is_string())
18696  {
18697  AllocatorType<string_t> alloc;
18698  std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.string);
18699  std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.string, 1);
18700  m_value.string = nullptr;
18701  }
18702 
18703  m_type = value_t::null;
18704  assert_invariant();
18705  break;
18706  }
18707 
18708  case value_t::object:
18709  {
18710  result.m_it.object_iterator = m_value.object->erase(first.m_it.object_iterator,
18711  last.m_it.object_iterator);
18712  break;
18713  }
18714 
18715  case value_t::array:
18716  {
18717  result.m_it.array_iterator = m_value.array->erase(first.m_it.array_iterator,
18718  last.m_it.array_iterator);
18719  break;
18720  }
18721 
18722  default:
18723  JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name())));
18724  }
18725 
18726  return result;
18727  }
18728 
18758  size_type erase(const typename object_t::key_type& key)
18759  {
18760  // this erase only works for objects
18761  if (JSON_HEDLEY_LIKELY(is_object()))
18762  {
18763  return m_value.object->erase(key);
18764  }
18765 
18766  JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name())));
18767  }
18768 
18793  void erase(const size_type idx)
18794  {
18795  // this erase only works for arrays
18796  if (JSON_HEDLEY_LIKELY(is_array()))
18797  {
18798  if (JSON_HEDLEY_UNLIKELY(idx >= size()))
18799  {
18800  JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range"));
18801  }
18802 
18803  m_value.array->erase(m_value.array->begin() + static_cast<difference_type>(idx));
18804  }
18805  else
18806  {
18807  JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name())));
18808  }
18809  }
18810 
18812 
18813 
18815  // lookup //
18817 
18820 
18845  template<typename KeyT>
18846  iterator find(KeyT&& key)
18847  {
18848  auto result = end();
18849 
18850  if (is_object())
18851  {
18852  result.m_it.object_iterator = m_value.object->find(std::forward<KeyT>(key));
18853  }
18854 
18855  return result;
18856  }
18857 
18862  template<typename KeyT>
18863  const_iterator find(KeyT&& key) const
18864  {
18865  auto result = cend();
18866 
18867  if (is_object())
18868  {
18869  result.m_it.object_iterator = m_value.object->find(std::forward<KeyT>(key));
18870  }
18871 
18872  return result;
18873  }
18874 
18896  template<typename KeyT>
18897  size_type count(KeyT&& key) const
18898  {
18899  // return 0 for all nonobject types
18900  return is_object() ? m_value.object->count(std::forward<KeyT>(key)) : 0;
18901  }
18902 
18928  template<typename KeyT, typename std::enable_if<
18929  not std::is_same<typename std::decay<KeyT>::type, json_pointer>::value, int>::type = 0>
18930  bool contains(KeyT && key) const
18931  {
18932  return is_object() and m_value.object->find(std::forward<KeyT>(key)) != m_value.object->end();
18933  }
18934 
18961  bool contains(const json_pointer& ptr) const
18962  {
18963  return ptr.contains(this);
18964  }
18965 
18967 
18968 
18970  // iterators //
18972 
18975 
19000  iterator begin() noexcept
19001  {
19002  iterator result(this);
19003  result.set_begin();
19004  return result;
19005  }
19006 
19010  const_iterator begin() const noexcept
19011  {
19012  return cbegin();
19013  }
19014 
19040  const_iterator cbegin() const noexcept
19041  {
19042  const_iterator result(this);
19043  result.set_begin();
19044  return result;
19045  }
19046 
19071  iterator end() noexcept
19072  {
19073  iterator result(this);
19074  result.set_end();
19075  return result;
19076  }
19077 
19081  const_iterator end() const noexcept
19082  {
19083  return cend();
19084  }
19085 
19111  const_iterator cend() const noexcept
19112  {
19113  const_iterator result(this);
19114  result.set_end();
19115  return result;
19116  }
19117 
19141  reverse_iterator rbegin() noexcept
19142  {
19143  return reverse_iterator(end());
19144  }
19145 
19149  const_reverse_iterator rbegin() const noexcept
19150  {
19151  return crbegin();
19152  }
19153 
19178  reverse_iterator rend() noexcept
19179  {
19180  return reverse_iterator(begin());
19181  }
19182 
19186  const_reverse_iterator rend() const noexcept
19187  {
19188  return crend();
19189  }
19190 
19215  const_reverse_iterator crbegin() const noexcept
19216  {
19217  return const_reverse_iterator(cend());
19218  }
19219 
19244  const_reverse_iterator crend() const noexcept
19245  {
19246  return const_reverse_iterator(cbegin());
19247  }
19248 
19249  public:
19307  JSON_HEDLEY_DEPRECATED(3.1.0)
19308  static iteration_proxy<iterator> iterator_wrapper(reference ref) noexcept
19309  {
19310  return ref.items();
19311  }
19312 
19316  JSON_HEDLEY_DEPRECATED(3.1.0)
19317  static iteration_proxy<const_iterator> iterator_wrapper(const_reference ref) noexcept
19318  {
19319  return ref.items();
19320  }
19321 
19385  iteration_proxy<iterator> items() noexcept
19386  {
19387  return iteration_proxy<iterator>(*this);
19388  }
19389 
19393  iteration_proxy<const_iterator> items() const noexcept
19394  {
19395  return iteration_proxy<const_iterator>(*this);
19396  }
19397 
19399 
19400 
19402  // capacity //
19404 
19407 
19449  bool empty() const noexcept
19450  {
19451  switch (m_type)
19452  {
19453  case value_t::null:
19454  {
19455  // null values are empty
19456  return true;
19457  }
19458 
19459  case value_t::array:
19460  {
19461  // delegate call to array_t::empty()
19462  return m_value.array->empty();
19463  }
19464 
19465  case value_t::object:
19466  {
19467  // delegate call to object_t::empty()
19468  return m_value.object->empty();
19469  }
19470 
19471  default:
19472  {
19473  // all other types are nonempty
19474  return false;
19475  }
19476  }
19477  }
19478 
19521  size_type size() const noexcept
19522  {
19523  switch (m_type)
19524  {
19525  case value_t::null:
19526  {
19527  // null values are empty
19528  return 0;
19529  }
19530 
19531  case value_t::array:
19532  {
19533  // delegate call to array_t::size()
19534  return m_value.array->size();
19535  }
19536 
19537  case value_t::object:
19538  {
19539  // delegate call to object_t::size()
19540  return m_value.object->size();
19541  }
19542 
19543  default:
19544  {
19545  // all other types have size 1
19546  return 1;
19547  }
19548  }
19549  }
19550 
19591  size_type max_size() const noexcept
19592  {
19593  switch (m_type)
19594  {
19595  case value_t::array:
19596  {
19597  // delegate call to array_t::max_size()
19598  return m_value.array->max_size();
19599  }
19600 
19601  case value_t::object:
19602  {
19603  // delegate call to object_t::max_size()
19604  return m_value.object->max_size();
19605  }
19606 
19607  default:
19608  {
19609  // all other types have max_size() == size()
19610  return size();
19611  }
19612  }
19613  }
19614 
19616 
19617 
19619  // modifiers //
19621 
19624 
19661  void clear() noexcept
19662  {
19663  switch (m_type)
19664  {
19665  case value_t::number_integer:
19666  {
19667  m_value.number_integer = 0;
19668  break;
19669  }
19670 
19671  case value_t::number_unsigned:
19672  {
19673  m_value.number_unsigned = 0;
19674  break;
19675  }
19676 
19677  case value_t::number_float:
19678  {
19679  m_value.number_float = 0.0;
19680  break;
19681  }
19682 
19683  case value_t::boolean:
19684  {
19685  m_value.boolean = false;
19686  break;
19687  }
19688 
19689  case value_t::string:
19690  {
19691  m_value.string->clear();
19692  break;
19693  }
19694 
19695  case value_t::array:
19696  {
19697  m_value.array->clear();
19698  break;
19699  }
19700 
19701  case value_t::object:
19702  {
19703  m_value.object->clear();
19704  break;
19705  }
19706 
19707  default:
19708  break;
19709  }
19710  }
19711 
19732  void push_back(basic_json&& val)
19733  {
19734  // push_back only works for null objects or arrays
19735  if (JSON_HEDLEY_UNLIKELY(not(is_null() or is_array())))
19736  {
19737  JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name())));
19738  }
19739 
19740  // transform null object into an array
19741  if (is_null())
19742  {
19743  m_type = value_t::array;
19744  m_value = value_t::array;
19745  assert_invariant();
19746  }
19747 
19748  // add element to array (move semantics)
19749  m_value.array->push_back(std::move(val));
19750  // if val is moved from, basic_json move constructor marks it null so we do not call the destructor
19751  }
19752 
19757  reference operator+=(basic_json&& val)
19758  {
19759  push_back(std::move(val));
19760  return *this;
19761  }
19762 
19767  void push_back(const basic_json& val)
19768  {
19769  // push_back only works for null objects or arrays
19770  if (JSON_HEDLEY_UNLIKELY(not(is_null() or is_array())))
19771  {
19772  JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name())));
19773  }
19774 
19775  // transform null object into an array
19776  if (is_null())
19777  {
19778  m_type = value_t::array;
19779  m_value = value_t::array;
19780  assert_invariant();
19781  }
19782 
19783  // add element to array
19784  m_value.array->push_back(val);
19785  }
19786 
19791  reference operator+=(const basic_json& val)
19792  {
19793  push_back(val);
19794  return *this;
19795  }
19796 
19817  void push_back(const typename object_t::value_type& val)
19818  {
19819  // push_back only works for null objects or objects
19820  if (JSON_HEDLEY_UNLIKELY(not(is_null() or is_object())))
19821  {
19822  JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name())));
19823  }
19824 
19825  // transform null object into an object
19826  if (is_null())
19827  {
19828  m_type = value_t::object;
19829  m_value = value_t::object;
19830  assert_invariant();
19831  }
19832 
19833  // add element to array
19834  m_value.object->insert(val);
19835  }
19836 
19841  reference operator+=(const typename object_t::value_type& val)
19842  {
19843  push_back(val);
19844  return *this;
19845  }
19846 
19872  void push_back(initializer_list_t init)
19873  {
19874  if (is_object() and init.size() == 2 and (*init.begin())->is_string())
19875  {
19876  basic_json&& key = init.begin()->moved_or_copied();
19877  push_back(typename object_t::value_type(
19878  std::move(key.get_ref<string_t&>()), (init.begin() + 1)->moved_or_copied()));
19879  }
19880  else
19881  {
19882  push_back(basic_json(init));
19883  }
19884  }
19885 
19891  {
19892  push_back(init);
19893  return *this;
19894  }
19895 
19919  template<class... Args>
19920  reference emplace_back(Args&& ... args)
19921  {
19922  // emplace_back only works for null objects or arrays
19923  if (JSON_HEDLEY_UNLIKELY(not(is_null() or is_array())))
19924  {
19925  JSON_THROW(type_error::create(311, "cannot use emplace_back() with " + std::string(type_name())));
19926  }
19927 
19928  // transform null object into an array
19929  if (is_null())
19930  {
19931  m_type = value_t::array;
19932  m_value = value_t::array;
19933  assert_invariant();
19934  }
19935 
19936  // add element to array (perfect forwarding)
19937 #ifdef JSON_HAS_CPP_17
19938  return m_value.array->emplace_back(std::forward<Args>(args)...);
19939 #else
19940  m_value.array->emplace_back(std::forward<Args>(args)...);
19941  return m_value.array->back();
19942 #endif
19943  }
19944 
19972  template<class... Args>
19973  std::pair<iterator, bool> emplace(Args&& ... args)
19974  {
19975  // emplace only works for null objects or arrays
19976  if (JSON_HEDLEY_UNLIKELY(not(is_null() or is_object())))
19977  {
19978  JSON_THROW(type_error::create(311, "cannot use emplace() with " + std::string(type_name())));
19979  }
19980 
19981  // transform null object into an object
19982  if (is_null())
19983  {
19984  m_type = value_t::object;
19985  m_value = value_t::object;
19986  assert_invariant();
19987  }
19988 
19989  // add element to array (perfect forwarding)
19990  auto res = m_value.object->emplace(std::forward<Args>(args)...);
19991  // create result iterator and set iterator to the result of emplace
19992  auto it = begin();
19993  it.m_it.object_iterator = res.first;
19994 
19995  // return pair of iterator and boolean
19996  return {it, res.second};
19997  }
19998 
20002  template<typename... Args>
20003  iterator insert_iterator(const_iterator pos, Args&& ... args)
20004  {
20005  iterator result(this);
20006  assert(m_value.array != nullptr);
20007 
20008  auto insert_pos = std::distance(m_value.array->begin(), pos.m_it.array_iterator);
20009  m_value.array->insert(pos.m_it.array_iterator, std::forward<Args>(args)...);
20010  result.m_it.array_iterator = m_value.array->begin() + insert_pos;
20011 
20012  // This could have been written as:
20013  // result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, cnt, val);
20014  // but the return value of insert is missing in GCC 4.8, so it is written this way instead.
20015 
20016  return result;
20017  }
20018 
20042  {
20043  // insert only works for arrays
20044  if (JSON_HEDLEY_LIKELY(is_array()))
20045  {
20046  // check if iterator pos fits to this JSON value
20047  if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
20048  {
20049  JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value"));
20050  }
20051 
20052  // insert to array and return iterator
20053  return insert_iterator(pos, val);
20054  }
20055 
20056  JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name())));
20057  }
20058 
20064  {
20065  return insert(pos, val);
20066  }
20067 
20093  {
20094  // insert only works for arrays
20095  if (JSON_HEDLEY_LIKELY(is_array()))
20096  {
20097  // check if iterator pos fits to this JSON value
20098  if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
20099  {
20100  JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value"));
20101  }
20102 
20103  // insert to array and return iterator
20104  return insert_iterator(pos, cnt, val);
20105  }
20106 
20107  JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name())));
20108  }
20109 
20141  {
20142  // insert only works for arrays
20143  if (JSON_HEDLEY_UNLIKELY(not is_array()))
20144  {
20145  JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name())));
20146  }
20147 
20148  // check if iterator pos fits to this JSON value
20149  if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
20150  {
20151  JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value"));
20152  }
20153 
20154  // check if range iterators belong to the same JSON object
20155  if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
20156  {
20157  JSON_THROW(invalid_iterator::create(210, "iterators do not fit"));
20158  }
20159 
20160  if (JSON_HEDLEY_UNLIKELY(first.m_object == this))
20161  {
20162  JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container"));
20163  }
20164 
20165  // insert to array and return iterator
20166  return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator);
20167  }
20168 
20194  {
20195  // insert only works for arrays
20196  if (JSON_HEDLEY_UNLIKELY(not is_array()))
20197  {
20198  JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name())));
20199  }
20200 
20201  // check if iterator pos fits to this JSON value
20202  if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
20203  {
20204  JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value"));
20205  }
20206 
20207  // insert to array and return iterator
20208  return insert_iterator(pos, ilist.begin(), ilist.end());
20209  }
20210 
20234  void insert(const_iterator first, const_iterator last)
20235  {
20236  // insert only works for objects
20237  if (JSON_HEDLEY_UNLIKELY(not is_object()))
20238  {
20239  JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name())));
20240  }
20241 
20242  // check if range iterators belong to the same JSON object
20243  if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
20244  {
20245  JSON_THROW(invalid_iterator::create(210, "iterators do not fit"));
20246  }
20247 
20248  // passed iterators must belong to objects
20249  if (JSON_HEDLEY_UNLIKELY(not first.m_object->is_object()))
20250  {
20251  JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects"));
20252  }
20253 
20254  m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator);
20255  }
20256 
20277  {
20278  // implicitly convert null value to an empty object
20279  if (is_null())
20280  {
20281  m_type = value_t::object;
20282  m_value.object = create<object_t>();
20283  assert_invariant();
20284  }
20285 
20286  if (JSON_HEDLEY_UNLIKELY(not is_object()))
20287  {
20288  JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name())));
20289  }
20290  if (JSON_HEDLEY_UNLIKELY(not j.is_object()))
20291  {
20292  JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(j.type_name())));
20293  }
20294 
20295  for (auto it = j.cbegin(); it != j.cend(); ++it)
20296  {
20297  m_value.object->operator[](it.key()) = it.value();
20298  }
20299  }
20300 
20328  {
20329  // implicitly convert null value to an empty object
20330  if (is_null())
20331  {
20332  m_type = value_t::object;
20333  m_value.object = create<object_t>();
20334  assert_invariant();
20335  }
20336 
20337  if (JSON_HEDLEY_UNLIKELY(not is_object()))
20338  {
20339  JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name())));
20340  }
20341 
20342  // check if range iterators belong to the same JSON object
20343  if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
20344  {
20345  JSON_THROW(invalid_iterator::create(210, "iterators do not fit"));
20346  }
20347 
20348  // passed iterators must belong to objects
20349  if (JSON_HEDLEY_UNLIKELY(not first.m_object->is_object()
20350  or not last.m_object->is_object()))
20351  {
20352  JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects"));
20353  }
20354 
20355  for (auto it = first; it != last; ++it)
20356  {
20357  m_value.object->operator[](it.key()) = it.value();
20358  }
20359  }
20360 
20378  void swap(reference other) noexcept (
20383  )
20384  {
20385  std::swap(m_type, other.m_type);
20386  std::swap(m_value, other.m_value);
20387  assert_invariant();
20388  }
20389 
20410  void swap(array_t& other)
20411  {
20412  // swap only works for arrays
20413  if (JSON_HEDLEY_LIKELY(is_array()))
20414  {
20415  std::swap(*(m_value.array), other);
20416  }
20417  else
20418  {
20419  JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name())));
20420  }
20421  }
20422 
20443  void swap(object_t& other)
20444  {
20445  // swap only works for objects
20446  if (JSON_HEDLEY_LIKELY(is_object()))
20447  {
20448  std::swap(*(m_value.object), other);
20449  }
20450  else
20451  {
20452  JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name())));
20453  }
20454  }
20455 
20476  void swap(string_t& other)
20477  {
20478  // swap only works for strings
20479  if (JSON_HEDLEY_LIKELY(is_string()))
20480  {
20481  std::swap(*(m_value.string), other);
20482  }
20483  else
20484  {
20485  JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name())));
20486  }
20487  }
20488 
20490 
20491  public:
20493  // lexicographical comparison operators //
20495 
20498 
20554  friend bool operator==(const_reference lhs, const_reference rhs) noexcept
20555  {
20556  const auto lhs_type = lhs.type();
20557  const auto rhs_type = rhs.type();
20558 
20559  if (lhs_type == rhs_type)
20560  {
20561  switch (lhs_type)
20562  {
20563  case value_t::array:
20564  return *lhs.m_value.array == *rhs.m_value.array;
20565 
20566  case value_t::object:
20567  return *lhs.m_value.object == *rhs.m_value.object;
20568 
20569  case value_t::null:
20570  return true;
20571 
20572  case value_t::string:
20573  return *lhs.m_value.string == *rhs.m_value.string;
20574 
20575  case value_t::boolean:
20576  return lhs.m_value.boolean == rhs.m_value.boolean;
20577 
20578  case value_t::number_integer:
20579  return lhs.m_value.number_integer == rhs.m_value.number_integer;
20580 
20581  case value_t::number_unsigned:
20582  return lhs.m_value.number_unsigned == rhs.m_value.number_unsigned;
20583 
20584  case value_t::number_float:
20585  return lhs.m_value.number_float == rhs.m_value.number_float;
20586 
20587  default:
20588  return false;
20589  }
20590  }
20591  else if (lhs_type == value_t::number_integer and rhs_type == value_t::number_float)
20592  {
20593  return static_cast<number_float_t>(lhs.m_value.number_integer) == rhs.m_value.number_float;
20594  }
20595  else if (lhs_type == value_t::number_float and rhs_type == value_t::number_integer)
20596  {
20597  return lhs.m_value.number_float == static_cast<number_float_t>(rhs.m_value.number_integer);
20598  }
20599  else if (lhs_type == value_t::number_unsigned and rhs_type == value_t::number_float)
20600  {
20601  return static_cast<number_float_t>(lhs.m_value.number_unsigned) == rhs.m_value.number_float;
20602  }
20603  else if (lhs_type == value_t::number_float and rhs_type == value_t::number_unsigned)
20604  {
20605  return lhs.m_value.number_float == static_cast<number_float_t>(rhs.m_value.number_unsigned);
20606  }
20607  else if (lhs_type == value_t::number_unsigned and rhs_type == value_t::number_integer)
20608  {
20609  return static_cast<number_integer_t>(lhs.m_value.number_unsigned) == rhs.m_value.number_integer;
20610  }
20611  else if (lhs_type == value_t::number_integer and rhs_type == value_t::number_unsigned)
20612  {
20613  return lhs.m_value.number_integer == static_cast<number_integer_t>(rhs.m_value.number_unsigned);
20614  }
20615 
20616  return false;
20617  }
20618 
20623  template<typename ScalarType, typename std::enable_if<
20624  std::is_scalar<ScalarType>::value, int>::type = 0>
20625  friend bool operator==(const_reference lhs, const ScalarType rhs) noexcept
20626  {
20627  return lhs == basic_json(rhs);
20628  }
20629 
20634  template<typename ScalarType, typename std::enable_if<
20635  std::is_scalar<ScalarType>::value, int>::type = 0>
20636  friend bool operator==(const ScalarType lhs, const_reference rhs) noexcept
20637  {
20638  return basic_json(lhs) == rhs;
20639  }
20640 
20659  friend bool operator!=(const_reference lhs, const_reference rhs) noexcept
20660  {
20661  return not (lhs == rhs);
20662  }
20663 
20668  template<typename ScalarType, typename std::enable_if<
20669  std::is_scalar<ScalarType>::value, int>::type = 0>
20670  friend bool operator!=(const_reference lhs, const ScalarType rhs) noexcept
20671  {
20672  return lhs != basic_json(rhs);
20673  }
20674 
20679  template<typename ScalarType, typename std::enable_if<
20680  std::is_scalar<ScalarType>::value, int>::type = 0>
20681  friend bool operator!=(const ScalarType lhs, const_reference rhs) noexcept
20682  {
20683  return basic_json(lhs) != rhs;
20684  }
20685 
20712  friend bool operator<(const_reference lhs, const_reference rhs) noexcept
20713  {
20714  const auto lhs_type = lhs.type();
20715  const auto rhs_type = rhs.type();
20716 
20717  if (lhs_type == rhs_type)
20718  {
20719  switch (lhs_type)
20720  {
20721  case value_t::array:
20722  // note parentheses are necessary, see
20723  // https://github.com/nlohmann/json/issues/1530
20724  return (*lhs.m_value.array) < (*rhs.m_value.array);
20725 
20726  case value_t::object:
20727  return (*lhs.m_value.object) < (*rhs.m_value.object);
20728 
20729  case value_t::null:
20730  return false;
20731 
20732  case value_t::string:
20733  return (*lhs.m_value.string) < (*rhs.m_value.string);
20734 
20735  case value_t::boolean:
20736  return (lhs.m_value.boolean) < (rhs.m_value.boolean);
20737 
20738  case value_t::number_integer:
20739  return (lhs.m_value.number_integer) < (rhs.m_value.number_integer);
20740 
20741  case value_t::number_unsigned:
20742  return (lhs.m_value.number_unsigned) < (rhs.m_value.number_unsigned);
20743 
20744  case value_t::number_float:
20745  return (lhs.m_value.number_float) < (rhs.m_value.number_float);
20746 
20747  default:
20748  return false;
20749  }
20750  }
20751  else if (lhs_type == value_t::number_integer and rhs_type == value_t::number_float)
20752  {
20753  return static_cast<number_float_t>(lhs.m_value.number_integer) < rhs.m_value.number_float;
20754  }
20755  else if (lhs_type == value_t::number_float and rhs_type == value_t::number_integer)
20756  {
20757  return lhs.m_value.number_float < static_cast<number_float_t>(rhs.m_value.number_integer);
20758  }
20759  else if (lhs_type == value_t::number_unsigned and rhs_type == value_t::number_float)
20760  {
20761  return static_cast<number_float_t>(lhs.m_value.number_unsigned) < rhs.m_value.number_float;
20762  }
20763  else if (lhs_type == value_t::number_float and rhs_type == value_t::number_unsigned)
20764  {
20765  return lhs.m_value.number_float < static_cast<number_float_t>(rhs.m_value.number_unsigned);
20766  }
20767  else if (lhs_type == value_t::number_integer and rhs_type == value_t::number_unsigned)
20768  {
20769  return lhs.m_value.number_integer < static_cast<number_integer_t>(rhs.m_value.number_unsigned);
20770  }
20771  else if (lhs_type == value_t::number_unsigned and rhs_type == value_t::number_integer)
20772  {
20773  return static_cast<number_integer_t>(lhs.m_value.number_unsigned) < rhs.m_value.number_integer;
20774  }
20775 
20776  // We only reach this line if we cannot compare values. In that case,
20777  // we compare types. Note we have to call the operator explicitly,
20778  // because MSVC has problems otherwise.
20779  return operator<(lhs_type, rhs_type);
20780  }
20781 
20786  template<typename ScalarType, typename std::enable_if<
20787  std::is_scalar<ScalarType>::value, int>::type = 0>
20788  friend bool operator<(const_reference lhs, const ScalarType rhs) noexcept
20789  {
20790  return lhs < basic_json(rhs);
20791  }
20792 
20797  template<typename ScalarType, typename std::enable_if<
20798  std::is_scalar<ScalarType>::value, int>::type = 0>
20799  friend bool operator<(const ScalarType lhs, const_reference rhs) noexcept
20800  {
20801  return basic_json(lhs) < rhs;
20802  }
20803 
20823  friend bool operator<=(const_reference lhs, const_reference rhs) noexcept
20824  {
20825  return not (rhs < lhs);
20826  }
20827 
20832  template<typename ScalarType, typename std::enable_if<
20833  std::is_scalar<ScalarType>::value, int>::type = 0>
20834  friend bool operator<=(const_reference lhs, const ScalarType rhs) noexcept
20835  {
20836  return lhs <= basic_json(rhs);
20837  }
20838 
20843  template<typename ScalarType, typename std::enable_if<
20844  std::is_scalar<ScalarType>::value, int>::type = 0>
20845  friend bool operator<=(const ScalarType lhs, const_reference rhs) noexcept
20846  {
20847  return basic_json(lhs) <= rhs;
20848  }
20849 
20869  friend bool operator>(const_reference lhs, const_reference rhs) noexcept
20870  {
20871  return not (lhs <= rhs);
20872  }
20873 
20878  template<typename ScalarType, typename std::enable_if<
20879  std::is_scalar<ScalarType>::value, int>::type = 0>
20880  friend bool operator>(const_reference lhs, const ScalarType rhs) noexcept
20881  {
20882  return lhs > basic_json(rhs);
20883  }
20884 
20889  template<typename ScalarType, typename std::enable_if<
20890  std::is_scalar<ScalarType>::value, int>::type = 0>
20891  friend bool operator>(const ScalarType lhs, const_reference rhs) noexcept
20892  {
20893  return basic_json(lhs) > rhs;
20894  }
20895 
20915  friend bool operator>=(const_reference lhs, const_reference rhs) noexcept
20916  {
20917  return not (lhs < rhs);
20918  }
20919 
20924  template<typename ScalarType, typename std::enable_if<
20925  std::is_scalar<ScalarType>::value, int>::type = 0>
20926  friend bool operator>=(const_reference lhs, const ScalarType rhs) noexcept
20927  {
20928  return lhs >= basic_json(rhs);
20929  }
20930 
20935  template<typename ScalarType, typename std::enable_if<
20936  std::is_scalar<ScalarType>::value, int>::type = 0>
20937  friend bool operator>=(const ScalarType lhs, const_reference rhs) noexcept
20938  {
20939  return basic_json(lhs) >= rhs;
20940  }
20941 
20943 
20945  // serialization //
20947 
20950 
20982  friend std::ostream& operator<<(std::ostream& o, const basic_json& j)
20983  {
20984  // read width member and use it as indentation parameter if nonzero
20985  const bool pretty_print = o.width() > 0;
20986  const auto indentation = pretty_print ? o.width() : 0;
20987 
20988  // reset width to 0 for subsequent calls to this stream
20989  o.width(0);
20990 
20991  // do the actual serialization
20993  s.dump(j, pretty_print, false, static_cast<unsigned int>(indentation));
20994  return o;
20995  }
20996 
21005  JSON_HEDLEY_DEPRECATED(3.0.0)
21006  friend std::ostream& operator>>(const basic_json& j, std::ostream& o)
21007  {
21008  return o << j;
21009  }
21010 
21012 
21013 
21015  // deserialization //
21017 
21020 
21086  const parser_callback_t cb = nullptr,
21087  const bool allow_exceptions = true)
21088  {
21089  basic_json result;
21090  parser(i, cb, allow_exceptions).parse(true, result);
21091  return result;
21092  }
21093 
21094  static bool accept(detail::input_adapter&& i)
21095  {
21096  return parser(i).accept(true);
21097  }
21098 
21152  template <typename SAX>
21154  static bool sax_parse(detail::input_adapter&& i, SAX* sax,
21156  const bool strict = true)
21157  {
21158  assert(sax);
21159  return format == input_format_t::json
21160  ? parser(std::move(i)).sax_parse(sax, strict)
21161  : detail::binary_reader<basic_json, SAX>(std::move(i)).sax_parse(format, sax, strict);
21162  }
21163 
21213  template<class IteratorType, typename std::enable_if<
21214  std::is_base_of<
21215  std::random_access_iterator_tag,
21216  typename std::iterator_traits<IteratorType>::iterator_category>::value, int>::type = 0>
21217  static basic_json parse(IteratorType first, IteratorType last,
21218  const parser_callback_t cb = nullptr,
21219  const bool allow_exceptions = true)
21220  {
21221  basic_json result;
21222  parser(detail::input_adapter(first, last), cb, allow_exceptions).parse(true, result);
21223  return result;
21224  }
21225 
21226  template<class IteratorType, typename std::enable_if<
21227  std::is_base_of<
21228  std::random_access_iterator_tag,
21229  typename std::iterator_traits<IteratorType>::iterator_category>::value, int>::type = 0>
21230  static bool accept(IteratorType first, IteratorType last)
21231  {
21232  return parser(detail::input_adapter(first, last)).accept(true);
21233  }
21234 
21235  template<class IteratorType, class SAX, typename std::enable_if<
21236  std::is_base_of<
21237  std::random_access_iterator_tag,
21238  typename std::iterator_traits<IteratorType>::iterator_category>::value, int>::type = 0>
21240  static bool sax_parse(IteratorType first, IteratorType last, SAX* sax)
21241  {
21242  return parser(detail::input_adapter(first, last)).sax_parse(sax);
21243  }
21244 
21253  JSON_HEDLEY_DEPRECATED(3.0.0)
21254  friend std::istream& operator<<(basic_json& j, std::istream& i)
21255  {
21256  return operator>>(i, j);
21257  }
21258 
21284  friend std::istream& operator>>(std::istream& i, basic_json& j)
21285  {
21286  parser(detail::input_adapter(i)).parse(false, j);
21287  return i;
21288  }
21289 
21291 
21293  // convenience functions //
21295 
21327  const char* type_name() const noexcept
21328  {
21329  {
21330  switch (m_type)
21331  {
21332  case value_t::null:
21333  return "null";
21334  case value_t::object:
21335  return "object";
21336  case value_t::array:
21337  return "array";
21338  case value_t::string:
21339  return "string";
21340  case value_t::boolean:
21341  return "boolean";
21342  case value_t::discarded:
21343  return "discarded";
21344  default:
21345  return "number";
21346  }
21347  }
21348  }
21349 
21350 
21351  private:
21353  // member variables //
21355 
21357  value_t m_type = value_t::null;
21358 
21360  json_value m_value = {};
21361 
21363  // binary serialization/deserialization //
21365 
21368 
21369  public:
21458  static std::vector<uint8_t> to_cbor(const basic_json& j)
21459  {
21460  std::vector<uint8_t> result;
21461  to_cbor(j, result);
21462  return result;
21463  }
21464 
21465  static void to_cbor(const basic_json& j, detail::output_adapter<uint8_t> o)
21466  {
21467  binary_writer<uint8_t>(o).write_cbor(j);
21468  }
21469 
21470  static void to_cbor(const basic_json& j, detail::output_adapter<char> o)
21471  {
21472  binary_writer<char>(o).write_cbor(j);
21473  }
21474 
21554  static std::vector<uint8_t> to_msgpack(const basic_json& j)
21555  {
21556  std::vector<uint8_t> result;
21557  to_msgpack(j, result);
21558  return result;
21559  }
21560 
21561  static void to_msgpack(const basic_json& j, detail::output_adapter<uint8_t> o)
21562  {
21563  binary_writer<uint8_t>(o).write_msgpack(j);
21564  }
21565 
21566  static void to_msgpack(const basic_json& j, detail::output_adapter<char> o)
21567  {
21568  binary_writer<char>(o).write_msgpack(j);
21569  }
21570 
21651  static std::vector<uint8_t> to_ubjson(const basic_json& j,
21652  const bool use_size = false,
21653  const bool use_type = false)
21654  {
21655  std::vector<uint8_t> result;
21656  to_ubjson(j, result, use_size, use_type);
21657  return result;
21658  }
21659 
21660  static void to_ubjson(const basic_json& j, detail::output_adapter<uint8_t> o,
21661  const bool use_size = false, const bool use_type = false)
21662  {
21663  binary_writer<uint8_t>(o).write_ubjson(j, use_size, use_type);
21664  }
21665 
21666  static void to_ubjson(const basic_json& j, detail::output_adapter<char> o,
21667  const bool use_size = false, const bool use_type = false)
21668  {
21669  binary_writer<char>(o).write_ubjson(j, use_size, use_type);
21670  }
21671 
21672 
21728  static std::vector<uint8_t> to_bson(const basic_json& j)
21729  {
21730  std::vector<uint8_t> result;
21731  to_bson(j, result);
21732  return result;
21733  }
21734 
21743  static void to_bson(const basic_json& j, detail::output_adapter<uint8_t> o)
21744  {
21745  binary_writer<uint8_t>(o).write_bson(j);
21746  }
21747 
21751  static void to_bson(const basic_json& j, detail::output_adapter<char> o)
21752  {
21753  binary_writer<char>(o).write_bson(j);
21754  }
21755 
21756 
21857  static basic_json from_cbor(detail::input_adapter&& i,
21858  const bool strict = true,
21859  const bool allow_exceptions = true)
21860  {
21861  basic_json result;
21862  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
21863  const bool res = binary_reader(detail::input_adapter(i)).sax_parse(input_format_t::cbor, &sdp, strict);
21864  return res ? result : basic_json(value_t::discarded);
21865  }
21866 
21870  template<typename A1, typename A2,
21873  static basic_json from_cbor(A1 && a1, A2 && a2,
21874  const bool strict = true,
21875  const bool allow_exceptions = true)
21876  {
21877  basic_json result;
21878  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
21879  const bool res = binary_reader(detail::input_adapter(std::forward<A1>(a1), std::forward<A2>(a2))).sax_parse(input_format_t::cbor, &sdp, strict);
21880  return res ? result : basic_json(value_t::discarded);
21881  }
21882 
21966  static basic_json from_msgpack(detail::input_adapter&& i,
21967  const bool strict = true,
21968  const bool allow_exceptions = true)
21969  {
21970  basic_json result;
21971  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
21972  const bool res = binary_reader(detail::input_adapter(i)).sax_parse(input_format_t::msgpack, &sdp, strict);
21973  return res ? result : basic_json(value_t::discarded);
21974  }
21975 
21979  template<typename A1, typename A2,
21982  static basic_json from_msgpack(A1 && a1, A2 && a2,
21983  const bool strict = true,
21984  const bool allow_exceptions = true)
21985  {
21986  basic_json result;
21987  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
21988  const bool res = binary_reader(detail::input_adapter(std::forward<A1>(a1), std::forward<A2>(a2))).sax_parse(input_format_t::msgpack, &sdp, strict);
21989  return res ? result : basic_json(value_t::discarded);
21990  }
21991 
22054  static basic_json from_ubjson(detail::input_adapter&& i,
22055  const bool strict = true,
22056  const bool allow_exceptions = true)
22057  {
22058  basic_json result;
22059  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
22060  const bool res = binary_reader(detail::input_adapter(i)).sax_parse(input_format_t::ubjson, &sdp, strict);
22061  return res ? result : basic_json(value_t::discarded);
22062  }
22063 
22067  template<typename A1, typename A2,
22070  static basic_json from_ubjson(A1 && a1, A2 && a2,
22071  const bool strict = true,
22072  const bool allow_exceptions = true)
22073  {
22074  basic_json result;
22075  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
22076  const bool res = binary_reader(detail::input_adapter(std::forward<A1>(a1), std::forward<A2>(a2))).sax_parse(input_format_t::ubjson, &sdp, strict);
22077  return res ? result : basic_json(value_t::discarded);
22078  }
22079 
22141  static basic_json from_bson(detail::input_adapter&& i,
22142  const bool strict = true,
22143  const bool allow_exceptions = true)
22144  {
22145  basic_json result;
22146  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
22147  const bool res = binary_reader(detail::input_adapter(i)).sax_parse(input_format_t::bson, &sdp, strict);
22148  return res ? result : basic_json(value_t::discarded);
22149  }
22150 
22154  template<typename A1, typename A2,
22157  static basic_json from_bson(A1 && a1, A2 && a2,
22158  const bool strict = true,
22159  const bool allow_exceptions = true)
22160  {
22161  basic_json result;
22162  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
22163  const bool res = binary_reader(detail::input_adapter(std::forward<A1>(a1), std::forward<A2>(a2))).sax_parse(input_format_t::bson, &sdp, strict);
22164  return res ? result : basic_json(value_t::discarded);
22165  }
22166 
22167 
22168 
22170 
22172  // JSON Pointer support //
22174 
22177 
22211  reference operator[](const json_pointer& ptr)
22212  {
22213  return ptr.get_unchecked(this);
22214  }
22215 
22239  const_reference operator[](const json_pointer& ptr) const
22240  {
22241  return ptr.get_unchecked(this);
22242  }
22243 
22283  {
22284  return ptr.get_checked(this);
22285  }
22286 
22325  const_reference at(const json_pointer& ptr) const
22326  {
22327  return ptr.get_checked(this);
22328  }
22329 
22352  basic_json flatten() const
22353  {
22354  basic_json result(value_t::object);
22355  json_pointer::flatten("", *this, result);
22356  return result;
22357  }
22358 
22389  basic_json unflatten() const
22390  {
22391  return json_pointer::unflatten(*this);
22392  }
22393 
22395 
22397  // JSON Patch functions //
22399 
22402 
22450  basic_json patch(const basic_json& json_patch) const
22451  {
22452  // make a working copy to apply the patch to
22453  basic_json result = *this;
22454 
22455  // the valid JSON Patch operations
22456  enum class patch_operations {add, remove, replace, move, copy, test, invalid};
22457 
22458  const auto get_op = [](const std::string & op)
22459  {
22460  if (op == "add")
22461  {
22462  return patch_operations::add;
22463  }
22464  if (op == "remove")
22465  {
22466  return patch_operations::remove;
22467  }
22468  if (op == "replace")
22469  {
22470  return patch_operations::replace;
22471  }
22472  if (op == "move")
22473  {
22474  return patch_operations::move;
22475  }
22476  if (op == "copy")
22477  {
22478  return patch_operations::copy;
22479  }
22480  if (op == "test")
22481  {
22482  return patch_operations::test;
22483  }
22484 
22485  return patch_operations::invalid;
22486  };
22487 
22488  // wrapper for "add" operation; add value at ptr
22489  const auto operation_add = [&result](json_pointer & ptr, basic_json val)
22490  {
22491  // adding to the root of the target document means replacing it
22492  if (ptr.empty())
22493  {
22494  result = val;
22495  return;
22496  }
22497 
22498  // make sure the top element of the pointer exists
22499  json_pointer top_pointer = ptr.top();
22500  if (top_pointer != ptr)
22501  {
22502  result.at(top_pointer);
22503  }
22504 
22505  // get reference to parent of JSON pointer ptr
22506  const auto last_path = ptr.back();
22507  ptr.pop_back();
22508  basic_json& parent = result[ptr];
22509 
22510  switch (parent.m_type)
22511  {
22512  case value_t::null:
22513  case value_t::object:
22514  {
22515  // use operator[] to add value
22516  parent[last_path] = val;
22517  break;
22518  }
22519 
22520  case value_t::array:
22521  {
22522  if (last_path == "-")
22523  {
22524  // special case: append to back
22525  parent.push_back(val);
22526  }
22527  else
22528  {
22529  const auto idx = json_pointer::array_index(last_path);
22530  if (JSON_HEDLEY_UNLIKELY(static_cast<size_type>(idx) > parent.size()))
22531  {
22532  // avoid undefined behavior
22533  JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range"));
22534  }
22535 
22536  // default case: insert add offset
22537  parent.insert(parent.begin() + static_cast<difference_type>(idx), val);
22538  }
22539  break;
22540  }
22541 
22542  // if there exists a parent it cannot be primitive
22543  default: // LCOV_EXCL_LINE
22544  assert(false); // LCOV_EXCL_LINE
22545  }
22546  };
22547 
22548  // wrapper for "remove" operation; remove value at ptr
22549  const auto operation_remove = [&result](json_pointer & ptr)
22550  {
22551  // get reference to parent of JSON pointer ptr
22552  const auto last_path = ptr.back();
22553  ptr.pop_back();
22554  basic_json& parent = result.at(ptr);
22555 
22556  // remove child
22557  if (parent.is_object())
22558  {
22559  // perform range check
22560  auto it = parent.find(last_path);
22561  if (JSON_HEDLEY_LIKELY(it != parent.end()))
22562  {
22563  parent.erase(it);
22564  }
22565  else
22566  {
22567  JSON_THROW(out_of_range::create(403, "key '" + last_path + "' not found"));
22568  }
22569  }
22570  else if (parent.is_array())
22571  {
22572  // note erase performs range check
22573  parent.erase(static_cast<size_type>(json_pointer::array_index(last_path)));
22574  }
22575  };
22576 
22577  // type check: top level value must be an array
22578  if (JSON_HEDLEY_UNLIKELY(not json_patch.is_array()))
22579  {
22580  JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects"));
22581  }
22582 
22583  // iterate and apply the operations
22584  for (const auto& val : json_patch)
22585  {
22586  // wrapper to get a value for an operation
22587  const auto get_value = [&val](const std::string & op,
22588  const std::string & member,
22589  bool string_type) -> basic_json &
22590  {
22591  // find value
22592  auto it = val.m_value.object->find(member);
22593 
22594  // context-sensitive error message
22595  const auto error_msg = (op == "op") ? "operation" : "operation '" + op + "'";
22596 
22597  // check if desired value is present
22598  if (JSON_HEDLEY_UNLIKELY(it == val.m_value.object->end()))
22599  {
22600  JSON_THROW(parse_error::create(105, 0, error_msg + " must have member '" + member + "'"));
22601  }
22602 
22603  // check if result is of type string
22604  if (JSON_HEDLEY_UNLIKELY(string_type and not it->second.is_string()))
22605  {
22606  JSON_THROW(parse_error::create(105, 0, error_msg + " must have string member '" + member + "'"));
22607  }
22608 
22609  // no error: return value
22610  return it->second;
22611  };
22612 
22613  // type check: every element of the array must be an object
22614  if (JSON_HEDLEY_UNLIKELY(not val.is_object()))
22615  {
22616  JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects"));
22617  }
22618 
22619  // collect mandatory members
22620  const std::string op = get_value("op", "op", true);
22621  const std::string path = get_value(op, "path", true);
22622  json_pointer ptr(path);
22623 
22624  switch (get_op(op))
22625  {
22626  case patch_operations::add:
22627  {
22628  operation_add(ptr, get_value("add", "value", false));
22629  break;
22630  }
22631 
22632  case patch_operations::remove:
22633  {
22634  operation_remove(ptr);
22635  break;
22636  }
22637 
22638  case patch_operations::replace:
22639  {
22640  // the "path" location must exist - use at()
22641  result.at(ptr) = get_value("replace", "value", false);
22642  break;
22643  }
22644 
22645  case patch_operations::move:
22646  {
22647  const std::string from_path = get_value("move", "from", true);
22648  json_pointer from_ptr(from_path);
22649 
22650  // the "from" location must exist - use at()
22651  basic_json v = result.at(from_ptr);
22652 
22653  // The move operation is functionally identical to a
22654  // "remove" operation on the "from" location, followed
22655  // immediately by an "add" operation at the target
22656  // location with the value that was just removed.
22657  operation_remove(from_ptr);
22658  operation_add(ptr, v);
22659  break;
22660  }
22661 
22662  case patch_operations::copy:
22663  {
22664  const std::string from_path = get_value("copy", "from", true);
22665  const json_pointer from_ptr(from_path);
22666 
22667  // the "from" location must exist - use at()
22668  basic_json v = result.at(from_ptr);
22669 
22670  // The copy is functionally identical to an "add"
22671  // operation at the target location using the value
22672  // specified in the "from" member.
22673  operation_add(ptr, v);
22674  break;
22675  }
22676 
22677  case patch_operations::test:
22678  {
22679  bool success = false;
22680  JSON_TRY
22681  {
22682  // check if "value" matches the one at "path"
22683  // the "path" location must exist - use at()
22684  success = (result.at(ptr) == get_value("test", "value", false));
22685  }
22687  {
22688  // ignore out of range errors: success remains false
22689  }
22690 
22691  // throw an exception if test fails
22692  if (JSON_HEDLEY_UNLIKELY(not success))
22693  {
22694  JSON_THROW(other_error::create(501, "unsuccessful: " + val.dump()));
22695  }
22696 
22697  break;
22698  }
22699 
22700  default:
22701  {
22702  // op must be "add", "remove", "replace", "move", "copy", or
22703  // "test"
22704  JSON_THROW(parse_error::create(105, 0, "operation value '" + op + "' is invalid"));
22705  }
22706  }
22707  }
22708 
22709  return result;
22710  }
22711 
22746  static basic_json diff(const basic_json& source, const basic_json& target,
22747  const std::string& path = "")
22748  {
22749  // the patch
22750  basic_json result(value_t::array);
22751 
22752  // if the values are the same, return empty patch
22753  if (source == target)
22754  {
22755  return result;
22756  }
22757 
22758  if (source.type() != target.type())
22759  {
22760  // different types: replace value
22761  result.push_back(
22762  {
22763  {"op", "replace"}, {"path", path}, {"value", target}
22764  });
22765  return result;
22766  }
22767 
22768  switch (source.type())
22769  {
22770  case value_t::array:
22771  {
22772  // first pass: traverse common elements
22773  std::size_t i = 0;
22774  while (i < source.size() and i < target.size())
22775  {
22776  // recursive call to compare array values at index i
22777  auto temp_diff = diff(source[i], target[i], path + "/" + std::to_string(i));
22778  result.insert(result.end(), temp_diff.begin(), temp_diff.end());
22779  ++i;
22780  }
22781 
22782  // i now reached the end of at least one array
22783  // in a second pass, traverse the remaining elements
22784 
22785  // remove my remaining elements
22786  const auto end_index = static_cast<difference_type>(result.size());
22787  while (i < source.size())
22788  {
22789  // add operations in reverse order to avoid invalid
22790  // indices
22791  result.insert(result.begin() + end_index, object(
22792  {
22793  {"op", "remove"},
22794  {"path", path + "/" + std::to_string(i)}
22795  }));
22796  ++i;
22797  }
22798 
22799  // add other remaining elements
22800  while (i < target.size())
22801  {
22802  result.push_back(
22803  {
22804  {"op", "add"},
22805  {"path", path + "/" + std::to_string(i)},
22806  {"value", target[i]}
22807  });
22808  ++i;
22809  }
22810 
22811  break;
22812  }
22813 
22814  case value_t::object:
22815  {
22816  // first pass: traverse this object's elements
22817  for (auto it = source.cbegin(); it != source.cend(); ++it)
22818  {
22819  // escape the key name to be used in a JSON patch
22820  const auto key = json_pointer::escape(it.key());
22821 
22822  if (target.find(it.key()) != target.end())
22823  {
22824  // recursive call to compare object values at key it
22825  auto temp_diff = diff(it.value(), target[it.key()], path + "/" + key);
22826  result.insert(result.end(), temp_diff.begin(), temp_diff.end());
22827  }
22828  else
22829  {
22830  // found a key that is not in o -> remove it
22831  result.push_back(object(
22832  {
22833  {"op", "remove"}, {"path", path + "/" + key}
22834  }));
22835  }
22836  }
22837 
22838  // second pass: traverse other object's elements
22839  for (auto it = target.cbegin(); it != target.cend(); ++it)
22840  {
22841  if (source.find(it.key()) == source.end())
22842  {
22843  // found a key that is not in this -> add it
22844  const auto key = json_pointer::escape(it.key());
22845  result.push_back(
22846  {
22847  {"op", "add"}, {"path", path + "/" + key},
22848  {"value", it.value()}
22849  });
22850  }
22851  }
22852 
22853  break;
22854  }
22855 
22856  default:
22857  {
22858  // both primitive type: replace value
22859  result.push_back(
22860  {
22861  {"op", "replace"}, {"path", path}, {"value", target}
22862  });
22863  break;
22864  }
22865  }
22866 
22867  return result;
22868  }
22869 
22871 
22873  // JSON Merge Patch functions //
22875 
22878 
22921  void merge_patch(const basic_json& apply_patch)
22922  {
22923  if (apply_patch.is_object())
22924  {
22925  if (not is_object())
22926  {
22927  *this = object();
22928  }
22929  for (auto it = apply_patch.begin(); it != apply_patch.end(); ++it)
22930  {
22931  if (it.value().is_null())
22932  {
22933  erase(it.key());
22934  }
22935  else
22936  {
22937  operator[](it.key()).merge_patch(it.value());
22938  }
22939  }
22940  }
22941  else
22942  {
22943  *this = apply_patch;
22944  }
22945  }
22946 
22948 };
22949 
22960 std::string to_string(const NLOHMANN_BASIC_JSON_TPL& j)
22961 {
22962  return j.dump();
22963 }
22964 } // namespace nlohmann
22965 
22967 // nonmember support //
22969 
22970 // specialization of std::swap, and std::hash
22971 namespace std
22972 {
22973 
22975 template<>
22976 struct hash<nlohmann::json>
22977 {
22983  std::size_t operator()(const nlohmann::json& j) const
22984  {
22985  // a naive hashing via the string representation
22986  const auto& h = hash<nlohmann::json::string_t>();
22987  return h(j.dump());
22988  }
22989 };
22990 
22994 template<>
22996 {
23001  bool operator()(nlohmann::detail::value_t lhs,
23002  nlohmann::detail::value_t rhs) const noexcept
23003  {
23004  return nlohmann::detail::operator<(lhs, rhs);
23005  }
23006 };
23007 
23013 template<>
23014 inline void swap<nlohmann::json>(nlohmann::json& j1, nlohmann::json& j2) noexcept(
23017 )
23018 {
23019  j1.swap(j2);
23020 }
23021 
23022 } // namespace std
23023 
23038 inline nlohmann::json operator "" _json(const char* s, std::size_t n)
23039 {
23040  return nlohmann::json::parse(s, s + n);
23041 }
23042 
23057 inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std::size_t n)
23058 {
23059  return nlohmann::json::json_pointer(std::string(s, n));
23060 }
23061 
23062 // #include <nlohmann/detail/macro_unscope.hpp>
23063 
23064 
23065 // restore GCC/clang diagnostic settings
23066 #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
23067  #pragma GCC diagnostic pop
23068 #endif
23069 #if defined(__clang__)
23070  #pragma GCC diagnostic pop
23071 #endif
23072 
23073 // clean up
23074 #undef JSON_INTERNAL_CATCH
23075 #undef JSON_CATCH
23076 #undef JSON_THROW
23077 #undef JSON_TRY
23078 #undef JSON_HAS_CPP_14
23079 #undef JSON_HAS_CPP_17
23080 #undef NLOHMANN_BASIC_JSON_TPL_DECLARATION
23081 #undef NLOHMANN_BASIC_JSON_TPL
23082 
23083 // #include <nlohmann/thirdparty/hedley/hedley_undef.hpp>
23084 #undef JSON_HEDLEY_ALWAYS_INLINE
23085 #undef JSON_HEDLEY_ARM_VERSION
23086 #undef JSON_HEDLEY_ARM_VERSION_CHECK
23087 #undef JSON_HEDLEY_ARRAY_PARAM
23088 #undef JSON_HEDLEY_ASSUME
23089 #undef JSON_HEDLEY_BEGIN_C_DECLS
23090 #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE
23091 #undef JSON_HEDLEY_CLANG_HAS_BUILTIN
23092 #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE
23093 #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE
23094 #undef JSON_HEDLEY_CLANG_HAS_EXTENSION
23095 #undef JSON_HEDLEY_CLANG_HAS_FEATURE
23096 #undef JSON_HEDLEY_CLANG_HAS_WARNING
23097 #undef JSON_HEDLEY_COMPCERT_VERSION
23098 #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK
23099 #undef JSON_HEDLEY_CONCAT
23100 #undef JSON_HEDLEY_CONCAT3
23101 #undef JSON_HEDLEY_CONCAT3_EX
23102 #undef JSON_HEDLEY_CONCAT_EX
23103 #undef JSON_HEDLEY_CONST
23104 #undef JSON_HEDLEY_CONSTEXPR
23105 #undef JSON_HEDLEY_CONST_CAST
23106 #undef JSON_HEDLEY_CPP_CAST
23107 #undef JSON_HEDLEY_CRAY_VERSION
23108 #undef JSON_HEDLEY_CRAY_VERSION_CHECK
23109 #undef JSON_HEDLEY_C_DECL
23110 #undef JSON_HEDLEY_DEPRECATED
23111 #undef JSON_HEDLEY_DEPRECATED_FOR
23112 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
23113 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_
23114 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
23115 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
23116 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
23117 #undef JSON_HEDLEY_DIAGNOSTIC_POP
23118 #undef JSON_HEDLEY_DIAGNOSTIC_PUSH
23119 #undef JSON_HEDLEY_DMC_VERSION
23120 #undef JSON_HEDLEY_DMC_VERSION_CHECK
23121 #undef JSON_HEDLEY_EMPTY_BASES
23122 #undef JSON_HEDLEY_EMSCRIPTEN_VERSION
23123 #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK
23124 #undef JSON_HEDLEY_END_C_DECLS
23125 #undef JSON_HEDLEY_FLAGS
23126 #undef JSON_HEDLEY_FLAGS_CAST
23127 #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE
23128 #undef JSON_HEDLEY_GCC_HAS_BUILTIN
23129 #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE
23130 #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE
23131 #undef JSON_HEDLEY_GCC_HAS_EXTENSION
23132 #undef JSON_HEDLEY_GCC_HAS_FEATURE
23133 #undef JSON_HEDLEY_GCC_HAS_WARNING
23134 #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK
23135 #undef JSON_HEDLEY_GCC_VERSION
23136 #undef JSON_HEDLEY_GCC_VERSION_CHECK
23137 #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE
23138 #undef JSON_HEDLEY_GNUC_HAS_BUILTIN
23139 #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE
23140 #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE
23141 #undef JSON_HEDLEY_GNUC_HAS_EXTENSION
23142 #undef JSON_HEDLEY_GNUC_HAS_FEATURE
23143 #undef JSON_HEDLEY_GNUC_HAS_WARNING
23144 #undef JSON_HEDLEY_GNUC_VERSION
23145 #undef JSON_HEDLEY_GNUC_VERSION_CHECK
23146 #undef JSON_HEDLEY_HAS_ATTRIBUTE
23147 #undef JSON_HEDLEY_HAS_BUILTIN
23148 #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE
23149 #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS
23150 #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE
23151 #undef JSON_HEDLEY_HAS_EXTENSION
23152 #undef JSON_HEDLEY_HAS_FEATURE
23153 #undef JSON_HEDLEY_HAS_WARNING
23154 #undef JSON_HEDLEY_IAR_VERSION
23155 #undef JSON_HEDLEY_IAR_VERSION_CHECK
23156 #undef JSON_HEDLEY_IBM_VERSION
23157 #undef JSON_HEDLEY_IBM_VERSION_CHECK
23158 #undef JSON_HEDLEY_IMPORT
23159 #undef JSON_HEDLEY_INLINE
23160 #undef JSON_HEDLEY_INTEL_VERSION
23161 #undef JSON_HEDLEY_INTEL_VERSION_CHECK
23162 #undef JSON_HEDLEY_IS_CONSTANT
23163 #undef JSON_HEDLEY_IS_CONSTEXPR_
23164 #undef JSON_HEDLEY_LIKELY
23165 #undef JSON_HEDLEY_MALLOC
23166 #undef JSON_HEDLEY_MESSAGE
23167 #undef JSON_HEDLEY_MSVC_VERSION
23168 #undef JSON_HEDLEY_MSVC_VERSION_CHECK
23169 #undef JSON_HEDLEY_NEVER_INLINE
23170 #undef JSON_HEDLEY_NON_NULL
23171 #undef JSON_HEDLEY_NO_ESCAPE
23172 #undef JSON_HEDLEY_NO_RETURN
23173 #undef JSON_HEDLEY_NO_THROW
23174 #undef JSON_HEDLEY_NULL
23175 #undef JSON_HEDLEY_PELLES_VERSION
23176 #undef JSON_HEDLEY_PELLES_VERSION_CHECK
23177 #undef JSON_HEDLEY_PGI_VERSION
23178 #undef JSON_HEDLEY_PGI_VERSION_CHECK
23179 #undef JSON_HEDLEY_PREDICT
23180 #undef JSON_HEDLEY_PRINTF_FORMAT
23181 #undef JSON_HEDLEY_PRIVATE
23182 #undef JSON_HEDLEY_PUBLIC
23183 #undef JSON_HEDLEY_PURE
23184 #undef JSON_HEDLEY_REINTERPRET_CAST
23185 #undef JSON_HEDLEY_REQUIRE
23186 #undef JSON_HEDLEY_REQUIRE_CONSTEXPR
23187 #undef JSON_HEDLEY_REQUIRE_MSG
23188 #undef JSON_HEDLEY_RESTRICT
23189 #undef JSON_HEDLEY_RETURNS_NON_NULL
23190 #undef JSON_HEDLEY_SENTINEL
23191 #undef JSON_HEDLEY_STATIC_ASSERT
23192 #undef JSON_HEDLEY_STATIC_CAST
23193 #undef JSON_HEDLEY_STRINGIFY
23194 #undef JSON_HEDLEY_STRINGIFY_EX
23195 #undef JSON_HEDLEY_SUNPRO_VERSION
23196 #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK
23197 #undef JSON_HEDLEY_TINYC_VERSION
23198 #undef JSON_HEDLEY_TINYC_VERSION_CHECK
23199 #undef JSON_HEDLEY_TI_ARMCL_VERSION
23200 #undef JSON_HEDLEY_TI_ARMCL_VERSION_CHECK
23201 #undef JSON_HEDLEY_TI_CL2000_VERSION
23202 #undef JSON_HEDLEY_TI_CL2000_VERSION_CHECK
23203 #undef JSON_HEDLEY_TI_CL430_VERSION
23204 #undef JSON_HEDLEY_TI_CL430_VERSION_CHECK
23205 #undef JSON_HEDLEY_TI_CL6X_VERSION
23206 #undef JSON_HEDLEY_TI_CL6X_VERSION_CHECK
23207 #undef JSON_HEDLEY_TI_CL7X_VERSION
23208 #undef JSON_HEDLEY_TI_CL7X_VERSION_CHECK
23209 #undef JSON_HEDLEY_TI_CLPRU_VERSION
23210 #undef JSON_HEDLEY_TI_CLPRU_VERSION_CHECK
23211 #undef JSON_HEDLEY_TI_VERSION
23212 #undef JSON_HEDLEY_TI_VERSION_CHECK
23213 #undef JSON_HEDLEY_UNAVAILABLE
23214 #undef JSON_HEDLEY_UNLIKELY
23215 #undef JSON_HEDLEY_UNPREDICTABLE
23216 #undef JSON_HEDLEY_UNREACHABLE
23217 #undef JSON_HEDLEY_UNREACHABLE_RETURN
23218 #undef JSON_HEDLEY_VERSION
23219 #undef JSON_HEDLEY_VERSION_DECODE_MAJOR
23220 #undef JSON_HEDLEY_VERSION_DECODE_MINOR
23221 #undef JSON_HEDLEY_VERSION_DECODE_REVISION
23222 #undef JSON_HEDLEY_VERSION_ENCODE
23223 #undef JSON_HEDLEY_WARNING
23224 #undef JSON_HEDLEY_WARN_UNUSED_RESULT
23225 #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG
23226 #undef JSON_HEDLEY_FALL_THROUGH
23227 
23228 
23229 
23230 #endif // INCLUDE_NLOHMANN_JSON_HPP_