Debian Codegraph @

single_include/nlohmann/json.hpp

raw symbols ◀ ▶
Source reconstructed at patch state 4 of 6 — not the installed (fully patched) source.
6 Debian patches touch this file
1 // __ _____ _____ _____
2 // __| | __| | | | JSON for Modern C++
3 // | | |__ | | | | | | version 3.12.0
4 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
5 //
6 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
7 // SPDX-License-Identifier: MIT
8
9 /****************************************************************************\
10 * Note on documentation: The source files contain links to the online *
11 * documentation of the public API at https://json.nlohmann.me. This URL *
12 * contains the most recent documentation and should also be applicable to *
13 * previous versions; documentation for deprecated functions is not *
14 * removed, but marked deprecated. See "Generate documentation" section in *
15 * file docs/README.md. *
16 \****************************************************************************/
17
18 #ifndef INCLUDE_NLOHMANN_JSON_HPP_
19 #define INCLUDE_NLOHMANN_JSON_HPP_
20
21 #include <algorithm> // all_of, find, for_each
22 #include <cstddef> // nullptr_t, ptrdiff_t, size_t
23 #include <functional> // hash, less
24 #include <initializer_list> // initializer_list
25 #ifndef JSON_NO_IO
26 #include <iosfwd> // istream, ostream
27 #endif // JSON_NO_IO
28 #include <iterator> // random_access_iterator_tag
29 #include <memory> // unique_ptr
30 #include <string> // string, stoi, to_string
31 #include <utility> // declval, forward, move, pair, swap
32 #include <vector> // vector
33
34 // #include <nlohmann/adl_serializer.hpp>
35 // __ _____ _____ _____
36 // __| | __| | | | JSON for Modern C++
37 // | | |__ | | | | | | version 3.12.0
38 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
39 //
40 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
41 // SPDX-License-Identifier: MIT
42
43
44
45 #include <utility>
46
47 // #include <nlohmann/detail/abi_macros.hpp>
48 // __ _____ _____ _____
49 // __| | __| | | | JSON for Modern C++
50 // | | |__ | | | | | | version 3.12.0
51 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
52 //
53 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
54 // SPDX-License-Identifier: MIT
55
56
57
58 // This file contains all macro definitions affecting or depending on the ABI
59
60 #ifndef JSON_SKIP_LIBRARY_VERSION_CHECK
61 #if defined(NLOHMANN_JSON_VERSION_MAJOR) && defined(NLOHMANN_JSON_VERSION_MINOR) && defined(NLOHMANN_JSON_VERSION_PATCH)
62 #if NLOHMANN_JSON_VERSION_MAJOR != 3 || NLOHMANN_JSON_VERSION_MINOR != 12 || NLOHMANN_JSON_VERSION_PATCH != 0
63 #warning "Already included a different version of the library!"
64 #endif
65 #endif
66 #endif
67
68 #define NLOHMANN_JSON_VERSION_MAJOR 3 // NOLINT(modernize-macro-to-enum)
69 #define NLOHMANN_JSON_VERSION_MINOR 12 // NOLINT(modernize-macro-to-enum)
70 #define NLOHMANN_JSON_VERSION_PATCH 0 // NOLINT(modernize-macro-to-enum)
71
72 #ifndef JSON_DIAGNOSTICS
73 #define JSON_DIAGNOSTICS 0
74 #endif
75
76 #ifndef JSON_DIAGNOSTIC_POSITIONS
77 #define JSON_DIAGNOSTIC_POSITIONS 0
78 #endif
79
80 #ifndef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
81 #define JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON 0
82 #endif
83
84 #if JSON_DIAGNOSTICS
85 #define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS _diag
86 #else
87 #define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS
88 #endif
89
90 #if JSON_DIAGNOSTIC_POSITIONS
91 #define NLOHMANN_JSON_ABI_TAG_DIAGNOSTIC_POSITIONS _dp
92 #else
93 #define NLOHMANN_JSON_ABI_TAG_DIAGNOSTIC_POSITIONS
94 #endif
95
96 #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
97 #define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON _ldvcmp
98 #else
99 #define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON
100 #endif
101
102 #ifndef NLOHMANN_JSON_NAMESPACE_NO_VERSION
103 #define NLOHMANN_JSON_NAMESPACE_NO_VERSION 0
104 #endif
105
106 // Construct the namespace ABI tags component
107 #define NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b, c) json_abi ## a ## b ## c
108 #define NLOHMANN_JSON_ABI_TAGS_CONCAT(a, b, c) \
109 NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b, c)
110
111 #define NLOHMANN_JSON_ABI_TAGS \
112 NLOHMANN_JSON_ABI_TAGS_CONCAT( \
113 NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS, \
114 NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON, \
115 NLOHMANN_JSON_ABI_TAG_DIAGNOSTIC_POSITIONS)
116
117 // Construct the namespace version component
118 #define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch) \
119 _v ## major ## _ ## minor ## _ ## patch
120 #define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(major, minor, patch) \
121 NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch)
122
123 #if NLOHMANN_JSON_NAMESPACE_NO_VERSION
124 #define NLOHMANN_JSON_NAMESPACE_VERSION
125 #else
126 #define NLOHMANN_JSON_NAMESPACE_VERSION \
127 NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(NLOHMANN_JSON_VERSION_MAJOR, \
128 NLOHMANN_JSON_VERSION_MINOR, \
129 NLOHMANN_JSON_VERSION_PATCH)
130 #endif
131
132 // Combine namespace components
133 #define NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b) a ## b
134 #define NLOHMANN_JSON_NAMESPACE_CONCAT(a, b) \
135 NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b)
136
137 #ifndef NLOHMANN_JSON_NAMESPACE
138 #define NLOHMANN_JSON_NAMESPACE \
139 nlohmann::NLOHMANN_JSON_NAMESPACE_CONCAT( \
140 NLOHMANN_JSON_ABI_TAGS, \
141 NLOHMANN_JSON_NAMESPACE_VERSION)
142 #endif
143
144 #ifndef NLOHMANN_JSON_NAMESPACE_BEGIN
145 #define NLOHMANN_JSON_NAMESPACE_BEGIN \
146 namespace nlohmann \
147 { \
148 inline namespace NLOHMANN_JSON_NAMESPACE_CONCAT( \
149 NLOHMANN_JSON_ABI_TAGS, \
150 NLOHMANN_JSON_NAMESPACE_VERSION) \
151 {
152 #endif
153
154 #ifndef NLOHMANN_JSON_NAMESPACE_END
155 #define NLOHMANN_JSON_NAMESPACE_END \
156 } /* namespace (inline namespace) NOLINT(readability/namespace) */ \
157 } // namespace nlohmann
158 #endif
159
160 // #include <nlohmann/detail/conversions/from_json.hpp>
161 // __ _____ _____ _____
162 // __| | __| | | | JSON for Modern C++
163 // | | |__ | | | | | | version 3.12.0
164 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
165 //
166 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
167 // SPDX-License-Identifier: MIT
168
169
170
171 #include <algorithm> // transform
172 #include <array> // array
173 #include <forward_list> // forward_list
174 #include <iterator> // inserter, front_inserter, end
175 #include <map> // map
176 #include <string> // string
177 #include <tuple> // tuple, make_tuple
178 #include <type_traits> // is_arithmetic, is_same, is_enum, underlying_type, is_convertible
179 #include <unordered_map> // unordered_map
180 #include <utility> // pair, declval
181 #include <valarray> // valarray
182
183 // #include <nlohmann/detail/exceptions.hpp>
184 // __ _____ _____ _____
185 // __| | __| | | | JSON for Modern C++
186 // | | |__ | | | | | | version 3.12.0
187 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
188 //
189 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
190 // SPDX-License-Identifier: MIT
191
192
193
194 #include <cstddef> // nullptr_t
195 #include <exception> // exception
196 #if JSON_DIAGNOSTICS
197 #include <numeric> // accumulate
198 #endif
199 #include <stdexcept> // runtime_error
200 #include <string> // to_string
201 #include <vector> // vector
202
203 // #include <nlohmann/detail/value_t.hpp>
204 // __ _____ _____ _____
205 // __| | __| | | | JSON for Modern C++
206 // | | |__ | | | | | | version 3.12.0
207 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
208 //
209 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
210 // SPDX-License-Identifier: MIT
211
212
213
214 #include <array> // array
215 #include <cstddef> // size_t
216 #include <cstdint> // uint8_t
217 #include <string> // string
218
219 // #include <nlohmann/detail/macro_scope.hpp>
220 // __ _____ _____ _____
221 // __| | __| | | | JSON for Modern C++
222 // | | |__ | | | | | | version 3.12.0
223 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
224 //
225 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
226 // SPDX-License-Identifier: MIT
227
228
229
230 #include <utility> // declval, pair
231 // #include <nlohmann/detail/meta/detected.hpp>
232 // __ _____ _____ _____
233 // __| | __| | | | JSON for Modern C++
234 // | | |__ | | | | | | version 3.12.0
235 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
236 //
237 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
238 // SPDX-License-Identifier: MIT
239
240
241
242 #include <type_traits>
243
244 // #include <nlohmann/detail/meta/void_t.hpp>
245 // __ _____ _____ _____
246 // __| | __| | | | JSON for Modern C++
247 // | | |__ | | | | | | version 3.12.0
248 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
249 //
250 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
251 // SPDX-License-Identifier: MIT
252
253
254
255 // #include <nlohmann/detail/abi_macros.hpp>
256
257
258 NLOHMANN_JSON_NAMESPACE_BEGIN
259 namespace detail
260 {
261
262 template<typename ...Ts> struct make_void
263 {
264 using type = void;
265 };
266 template<typename ...Ts> using void_t = typename make_void<Ts...>::type;
267
268 } // namespace detail
269 NLOHMANN_JSON_NAMESPACE_END
270
271
272 NLOHMANN_JSON_NAMESPACE_BEGIN
273 namespace detail
274 {
275
276 // https://en.cppreference.com/w/cpp/experimental/is_detected
277 struct nonesuch
278 {
279 nonesuch() = delete;
280 ~nonesuch() = delete;
281 nonesuch(nonesuch const&) = delete;
282 nonesuch(nonesuch const&&) = delete;
283 void operator=(nonesuch const&) = delete;
284 void operator=(nonesuch&&) = delete;
285 };
286
287 template<class Default,
288 class AlwaysVoid,
289 template<class...> class Op,
290 class... Args>
291 struct detector
292 {
293 using value_t = std::false_type;
294 using type = Default;
295 };
296
297 template<class Default, template<class...> class Op, class... Args>
298 struct detector<Default, void_t<Op<Args...>>, Op, Args...>
299 {
300 using value_t = std::true_type;
301 using type = Op<Args...>;
302 };
303
304 template<template<class...> class Op, class... Args>
305 using is_detected = typename detector<nonesuch, void, Op, Args...>::value_t;
306
307 template<template<class...> class Op, class... Args>
308 struct is_detected_lazy : is_detected<Op, Args...> { };
309
310 template<template<class...> class Op, class... Args>
311 using detected_t = typename detector<nonesuch, void, Op, Args...>::type;
312
313 template<class Default, template<class...> class Op, class... Args>
314 using detected_or = detector<Default, void, Op, Args...>;
315
316 template<class Default, template<class...> class Op, class... Args>
317 using detected_or_t = typename detected_or<Default, Op, Args...>::type;
318
319 template<class Expected, template<class...> class Op, class... Args>
320 using is_detected_exact = std::is_same<Expected, detected_t<Op, Args...>>;
321
322 template<class To, template<class...> class Op, class... Args>
323 using is_detected_convertible =
324 std::is_convertible<detected_t<Op, Args...>, To>;
325
326 } // namespace detail
327 NLOHMANN_JSON_NAMESPACE_END
328
329 // #include <nlohmann/thirdparty/hedley/hedley.hpp>
330
331
332 // __ _____ _____ _____
333 // __| | __| | | | JSON for Modern C++
334 // | | |__ | | | | | | version 3.12.0
335 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
336 //
337 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
338 // SPDX-FileCopyrightText: 2016 - 2021 Evan Nemerson <[email protected]>
339 // SPDX-License-Identifier: MIT
340
341 /* Hedley - https://nemequ.github.io/hedley
342 * Created by Evan Nemerson <[email protected]>
343 */
344
345 #if !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < 15)
346 #if defined(JSON_HEDLEY_VERSION)
347 #undef JSON_HEDLEY_VERSION
348 #endif
349 #define JSON_HEDLEY_VERSION 15
350
351 #if defined(JSON_HEDLEY_STRINGIFY_EX)
352 #undef JSON_HEDLEY_STRINGIFY_EX
353 #endif
354 #define JSON_HEDLEY_STRINGIFY_EX(x) #x
355
356 #if defined(JSON_HEDLEY_STRINGIFY)
357 #undef JSON_HEDLEY_STRINGIFY
358 #endif
359 #define JSON_HEDLEY_STRINGIFY(x) JSON_HEDLEY_STRINGIFY_EX(x)
360
361 #if defined(JSON_HEDLEY_CONCAT_EX)
362 #undef JSON_HEDLEY_CONCAT_EX
363 #endif
364 #define JSON_HEDLEY_CONCAT_EX(a,b) a##b
365
366 #if defined(JSON_HEDLEY_CONCAT)
367 #undef JSON_HEDLEY_CONCAT
368 #endif
369 #define JSON_HEDLEY_CONCAT(a,b) JSON_HEDLEY_CONCAT_EX(a,b)
370
371 #if defined(JSON_HEDLEY_CONCAT3_EX)
372 #undef JSON_HEDLEY_CONCAT3_EX
373 #endif
374 #define JSON_HEDLEY_CONCAT3_EX(a,b,c) a##b##c
375
376 #if defined(JSON_HEDLEY_CONCAT3)
377 #undef JSON_HEDLEY_CONCAT3
378 #endif
379 #define JSON_HEDLEY_CONCAT3(a,b,c) JSON_HEDLEY_CONCAT3_EX(a,b,c)
380
381 #if defined(JSON_HEDLEY_VERSION_ENCODE)
382 #undef JSON_HEDLEY_VERSION_ENCODE
383 #endif
384 #define JSON_HEDLEY_VERSION_ENCODE(major,minor,revision) (((major) * 1000000) + ((minor) * 1000) + (revision))
385
386 #if defined(JSON_HEDLEY_VERSION_DECODE_MAJOR)
387 #undef JSON_HEDLEY_VERSION_DECODE_MAJOR
388 #endif
389 #define JSON_HEDLEY_VERSION_DECODE_MAJOR(version) ((version) / 1000000)
390
391 #if defined(JSON_HEDLEY_VERSION_DECODE_MINOR)
392 #undef JSON_HEDLEY_VERSION_DECODE_MINOR
393 #endif
394 #define JSON_HEDLEY_VERSION_DECODE_MINOR(version) (((version) % 1000000) / 1000)
395
396 #if defined(JSON_HEDLEY_VERSION_DECODE_REVISION)
397 #undef JSON_HEDLEY_VERSION_DECODE_REVISION
398 #endif
399 #define JSON_HEDLEY_VERSION_DECODE_REVISION(version) ((version) % 1000)
400
401 #if defined(JSON_HEDLEY_GNUC_VERSION)
402 #undef JSON_HEDLEY_GNUC_VERSION
403 #endif
404 #if defined(__GNUC__) && defined(__GNUC_PATCHLEVEL__)
405 #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
406 #elif defined(__GNUC__)
407 #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, 0)
408 #endif
409
410 #if defined(JSON_HEDLEY_GNUC_VERSION_CHECK)
411 #undef JSON_HEDLEY_GNUC_VERSION_CHECK
412 #endif
413 #if defined(JSON_HEDLEY_GNUC_VERSION)
414 #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GNUC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
415 #else
416 #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (0)
417 #endif
418
419 #if defined(JSON_HEDLEY_MSVC_VERSION)
420 #undef JSON_HEDLEY_MSVC_VERSION
421 #endif
422 #if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 140000000) && !defined(__ICL)
423 #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 10000000, (_MSC_FULL_VER % 10000000) / 100000, (_MSC_FULL_VER % 100000) / 100)
424 #elif defined(_MSC_FULL_VER) && !defined(__ICL)
425 #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 1000000, (_MSC_FULL_VER % 1000000) / 10000, (_MSC_FULL_VER % 10000) / 10)
426 #elif defined(_MSC_VER) && !defined(__ICL)
427 #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_VER / 100, _MSC_VER % 100, 0)
428 #endif
429
430 #if defined(JSON_HEDLEY_MSVC_VERSION_CHECK)
431 #undef JSON_HEDLEY_MSVC_VERSION_CHECK
432 #endif
433 #if !defined(JSON_HEDLEY_MSVC_VERSION)
434 #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (0)
435 #elif defined(_MSC_VER) && (_MSC_VER >= 1400)
436 #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 10000000) + (minor * 100000) + (patch)))
437 #elif defined(_MSC_VER) && (_MSC_VER >= 1200)
438 #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 1000000) + (minor * 10000) + (patch)))
439 #else
440 #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_VER >= ((major * 100) + (minor)))
441 #endif
442
443 #if defined(JSON_HEDLEY_INTEL_VERSION)
444 #undef JSON_HEDLEY_INTEL_VERSION
445 #endif
446 #if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && !defined(__ICL)
447 #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, __INTEL_COMPILER_UPDATE)
448 #elif defined(__INTEL_COMPILER) && !defined(__ICL)
449 #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, 0)
450 #endif
451
452 #if defined(JSON_HEDLEY_INTEL_VERSION_CHECK)
453 #undef JSON_HEDLEY_INTEL_VERSION_CHECK
454 #endif
455 #if defined(JSON_HEDLEY_INTEL_VERSION)
456 #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
457 #else
458 #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (0)
459 #endif
460
461 #if defined(JSON_HEDLEY_INTEL_CL_VERSION)
462 #undef JSON_HEDLEY_INTEL_CL_VERSION
463 #endif
464 #if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && defined(__ICL)
465 #define JSON_HEDLEY_INTEL_CL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER, __INTEL_COMPILER_UPDATE, 0)
466 #endif
467
468 #if defined(JSON_HEDLEY_INTEL_CL_VERSION_CHECK)
469 #undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK
470 #endif
471 #if defined(JSON_HEDLEY_INTEL_CL_VERSION)
472 #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_CL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
473 #else
474 #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (0)
475 #endif
476
477 #if defined(JSON_HEDLEY_PGI_VERSION)
478 #undef JSON_HEDLEY_PGI_VERSION
479 #endif
480 #if defined(__PGI) && defined(__PGIC__) && defined(__PGIC_MINOR__) && defined(__PGIC_PATCHLEVEL__)
481 #define JSON_HEDLEY_PGI_VERSION JSON_HEDLEY_VERSION_ENCODE(__PGIC__, __PGIC_MINOR__, __PGIC_PATCHLEVEL__)
482 #endif
483
484 #if defined(JSON_HEDLEY_PGI_VERSION_CHECK)
485 #undef JSON_HEDLEY_PGI_VERSION_CHECK
486 #endif
487 #if defined(JSON_HEDLEY_PGI_VERSION)
488 #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PGI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
489 #else
490 #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (0)
491 #endif
492
493 #if defined(JSON_HEDLEY_SUNPRO_VERSION)
494 #undef JSON_HEDLEY_SUNPRO_VERSION
495 #endif
496 #if defined(__SUNPRO_C) && (__SUNPRO_C > 0x1000)
497 #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)
498 #elif defined(__SUNPRO_C)
499 #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_C >> 8) & 0xf, (__SUNPRO_C >> 4) & 0xf, (__SUNPRO_C) & 0xf)
500 #elif defined(__SUNPRO_CC) && (__SUNPRO_CC > 0x1000)
501 #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)
502 #elif defined(__SUNPRO_CC)
503 #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_CC >> 8) & 0xf, (__SUNPRO_CC >> 4) & 0xf, (__SUNPRO_CC) & 0xf)
504 #endif
505
506 #if defined(JSON_HEDLEY_SUNPRO_VERSION_CHECK)
507 #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK
508 #endif
509 #if defined(JSON_HEDLEY_SUNPRO_VERSION)
510 #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_SUNPRO_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
511 #else
512 #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (0)
513 #endif
514
515 #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION)
516 #undef JSON_HEDLEY_EMSCRIPTEN_VERSION
517 #endif
518 #if defined(__EMSCRIPTEN__)
519 #define JSON_HEDLEY_EMSCRIPTEN_VERSION JSON_HEDLEY_VERSION_ENCODE(__EMSCRIPTEN_major__, __EMSCRIPTEN_minor__, __EMSCRIPTEN_tiny__)
520 #endif
521
522 #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK)
523 #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK
524 #endif
525 #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION)
526 #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_EMSCRIPTEN_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
527 #else
528 #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (0)
529 #endif
530
531 #if defined(JSON_HEDLEY_ARM_VERSION)
532 #undef JSON_HEDLEY_ARM_VERSION
533 #endif
534 #if defined(__CC_ARM) && defined(__ARMCOMPILER_VERSION)
535 #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCOMPILER_VERSION / 1000000, (__ARMCOMPILER_VERSION % 1000000) / 10000, (__ARMCOMPILER_VERSION % 10000) / 100)
536 #elif defined(__CC_ARM) && defined(__ARMCC_VERSION)
537 #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCC_VERSION / 1000000, (__ARMCC_VERSION % 1000000) / 10000, (__ARMCC_VERSION % 10000) / 100)
538 #endif
539
540 #if defined(JSON_HEDLEY_ARM_VERSION_CHECK)
541 #undef JSON_HEDLEY_ARM_VERSION_CHECK
542 #endif
543 #if defined(JSON_HEDLEY_ARM_VERSION)
544 #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_ARM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
545 #else
546 #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (0)
547 #endif
548
549 #if defined(JSON_HEDLEY_IBM_VERSION)
550 #undef JSON_HEDLEY_IBM_VERSION
551 #endif
552 #if defined(__ibmxl__)
553 #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ibmxl_version__, __ibmxl_release__, __ibmxl_modification__)
554 #elif defined(__xlC__) && defined(__xlC_ver__)
555 #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8, __xlC__ & 0xff, (__xlC_ver__ >> 8) & 0xff)
556 #elif defined(__xlC__)
557 #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8, __xlC__ & 0xff, 0)
558 #endif
559
560 #if defined(JSON_HEDLEY_IBM_VERSION_CHECK)
561 #undef JSON_HEDLEY_IBM_VERSION_CHECK
562 #endif
563 #if defined(JSON_HEDLEY_IBM_VERSION)
564 #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IBM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
565 #else
566 #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (0)
567 #endif
568
569 #if defined(JSON_HEDLEY_TI_VERSION)
570 #undef JSON_HEDLEY_TI_VERSION
571 #endif
572 #if \
573 defined(__TI_COMPILER_VERSION__) && \
574 ( \
575 defined(__TMS470__) || defined(__TI_ARM__) || \
576 defined(__MSP430__) || \
577 defined(__TMS320C2000__) \
578 )
579 #if (__TI_COMPILER_VERSION__ >= 16000000)
580 #define JSON_HEDLEY_TI_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
581 #endif
582 #endif
583
584 #if defined(JSON_HEDLEY_TI_VERSION_CHECK)
585 #undef JSON_HEDLEY_TI_VERSION_CHECK
586 #endif
587 #if defined(JSON_HEDLEY_TI_VERSION)
588 #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
589 #else
590 #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (0)
591 #endif
592
593 #if defined(JSON_HEDLEY_TI_CL2000_VERSION)
594 #undef JSON_HEDLEY_TI_CL2000_VERSION
595 #endif
596 #if defined(__TI_COMPILER_VERSION__) && defined(__TMS320C2000__)
597 #define JSON_HEDLEY_TI_CL2000_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
598 #endif
599
600 #if defined(JSON_HEDLEY_TI_CL2000_VERSION_CHECK)
601 #undef JSON_HEDLEY_TI_CL2000_VERSION_CHECK
602 #endif
603 #if defined(JSON_HEDLEY_TI_CL2000_VERSION)
604 #define JSON_HEDLEY_TI_CL2000_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL2000_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
605 #else
606 #define JSON_HEDLEY_TI_CL2000_VERSION_CHECK(major,minor,patch) (0)
607 #endif
608
609 #if defined(JSON_HEDLEY_TI_CL430_VERSION)
610 #undef JSON_HEDLEY_TI_CL430_VERSION
611 #endif
612 #if defined(__TI_COMPILER_VERSION__) && defined(__MSP430__)
613 #define JSON_HEDLEY_TI_CL430_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
614 #endif
615
616 #if defined(JSON_HEDLEY_TI_CL430_VERSION_CHECK)
617 #undef JSON_HEDLEY_TI_CL430_VERSION_CHECK
618 #endif
619 #if defined(JSON_HEDLEY_TI_CL430_VERSION)
620 #define JSON_HEDLEY_TI_CL430_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL430_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
621 #else
622 #define JSON_HEDLEY_TI_CL430_VERSION_CHECK(major,minor,patch) (0)
623 #endif
624
625 #if defined(JSON_HEDLEY_TI_ARMCL_VERSION)
626 #undef JSON_HEDLEY_TI_ARMCL_VERSION
627 #endif
628 #if defined(__TI_COMPILER_VERSION__) && (defined(__TMS470__) || defined(__TI_ARM__))
629 #define JSON_HEDLEY_TI_ARMCL_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
630 #endif
631
632 #if defined(JSON_HEDLEY_TI_ARMCL_VERSION_CHECK)
633 #undef JSON_HEDLEY_TI_ARMCL_VERSION_CHECK
634 #endif
635 #if defined(JSON_HEDLEY_TI_ARMCL_VERSION)
636 #define JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_ARMCL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
637 #else
638 #define JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(major,minor,patch) (0)
639 #endif
640
641 #if defined(JSON_HEDLEY_TI_CL6X_VERSION)
642 #undef JSON_HEDLEY_TI_CL6X_VERSION
643 #endif
644 #if defined(__TI_COMPILER_VERSION__) && defined(__TMS320C6X__)
645 #define JSON_HEDLEY_TI_CL6X_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
646 #endif
647
648 #if defined(JSON_HEDLEY_TI_CL6X_VERSION_CHECK)
649 #undef JSON_HEDLEY_TI_CL6X_VERSION_CHECK
650 #endif
651 #if defined(JSON_HEDLEY_TI_CL6X_VERSION)
652 #define JSON_HEDLEY_TI_CL6X_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL6X_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
653 #else
654 #define JSON_HEDLEY_TI_CL6X_VERSION_CHECK(major,minor,patch) (0)
655 #endif
656
657 #if defined(JSON_HEDLEY_TI_CL7X_VERSION)
658 #undef JSON_HEDLEY_TI_CL7X_VERSION
659 #endif
660 #if defined(__TI_COMPILER_VERSION__) && defined(__C7000__)
661 #define JSON_HEDLEY_TI_CL7X_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
662 #endif
663
664 #if defined(JSON_HEDLEY_TI_CL7X_VERSION_CHECK)
665 #undef JSON_HEDLEY_TI_CL7X_VERSION_CHECK
666 #endif
667 #if defined(JSON_HEDLEY_TI_CL7X_VERSION)
668 #define JSON_HEDLEY_TI_CL7X_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL7X_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
669 #else
670 #define JSON_HEDLEY_TI_CL7X_VERSION_CHECK(major,minor,patch) (0)
671 #endif
672
673 #if defined(JSON_HEDLEY_TI_CLPRU_VERSION)
674 #undef JSON_HEDLEY_TI_CLPRU_VERSION
675 #endif
676 #if defined(__TI_COMPILER_VERSION__) && defined(__PRU__)
677 #define JSON_HEDLEY_TI_CLPRU_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
678 #endif
679
680 #if defined(JSON_HEDLEY_TI_CLPRU_VERSION_CHECK)
681 #undef JSON_HEDLEY_TI_CLPRU_VERSION_CHECK
682 #endif
683 #if defined(JSON_HEDLEY_TI_CLPRU_VERSION)
684 #define JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CLPRU_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
685 #else
686 #define JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(major,minor,patch) (0)
687 #endif
688
689 #if defined(JSON_HEDLEY_CRAY_VERSION)
690 #undef JSON_HEDLEY_CRAY_VERSION
691 #endif
692 #if defined(_CRAYC)
693 #if defined(_RELEASE_PATCHLEVEL)
694 #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, _RELEASE_PATCHLEVEL)
695 #else
696 #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, 0)
697 #endif
698 #endif
699
700 #if defined(JSON_HEDLEY_CRAY_VERSION_CHECK)
701 #undef JSON_HEDLEY_CRAY_VERSION_CHECK
702 #endif
703 #if defined(JSON_HEDLEY_CRAY_VERSION)
704 #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_CRAY_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
705 #else
706 #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (0)
707 #endif
708
709 #if defined(JSON_HEDLEY_IAR_VERSION)
710 #undef JSON_HEDLEY_IAR_VERSION
711 #endif
712 #if defined(__IAR_SYSTEMS_ICC__)
713 #if __VER__ > 1000
714 #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE((__VER__ / 1000000), ((__VER__ / 1000) % 1000), (__VER__ % 1000))
715 #else
716 #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE(__VER__ / 100, __VER__ % 100, 0)
717 #endif
718 #endif
719
720 #if defined(JSON_HEDLEY_IAR_VERSION_CHECK)
721 #undef JSON_HEDLEY_IAR_VERSION_CHECK
722 #endif
723 #if defined(JSON_HEDLEY_IAR_VERSION)
724 #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IAR_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
725 #else
726 #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (0)
727 #endif
728
729 #if defined(JSON_HEDLEY_TINYC_VERSION)
730 #undef JSON_HEDLEY_TINYC_VERSION
731 #endif
732 #if defined(__TINYC__)
733 #define JSON_HEDLEY_TINYC_VERSION JSON_HEDLEY_VERSION_ENCODE(__TINYC__ / 1000, (__TINYC__ / 100) % 10, __TINYC__ % 100)
734 #endif
735
736 #if defined(JSON_HEDLEY_TINYC_VERSION_CHECK)
737 #undef JSON_HEDLEY_TINYC_VERSION_CHECK
738 #endif
739 #if defined(JSON_HEDLEY_TINYC_VERSION)
740 #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TINYC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
741 #else
742 #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (0)
743 #endif
744
745 #if defined(JSON_HEDLEY_DMC_VERSION)
746 #undef JSON_HEDLEY_DMC_VERSION
747 #endif
748 #if defined(__DMC__)
749 #define JSON_HEDLEY_DMC_VERSION JSON_HEDLEY_VERSION_ENCODE(__DMC__ >> 8, (__DMC__ >> 4) & 0xf, __DMC__ & 0xf)
750 #endif
751
752 #if defined(JSON_HEDLEY_DMC_VERSION_CHECK)
753 #undef JSON_HEDLEY_DMC_VERSION_CHECK
754 #endif
755 #if defined(JSON_HEDLEY_DMC_VERSION)
756 #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_DMC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
757 #else
758 #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (0)
759 #endif
760
761 #if defined(JSON_HEDLEY_COMPCERT_VERSION)
762 #undef JSON_HEDLEY_COMPCERT_VERSION
763 #endif
764 #if defined(__COMPCERT_VERSION__)
765 #define JSON_HEDLEY_COMPCERT_VERSION JSON_HEDLEY_VERSION_ENCODE(__COMPCERT_VERSION__ / 10000, (__COMPCERT_VERSION__ / 100) % 100, __COMPCERT_VERSION__ % 100)
766 #endif
767
768 #if defined(JSON_HEDLEY_COMPCERT_VERSION_CHECK)
769 #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK
770 #endif
771 #if defined(JSON_HEDLEY_COMPCERT_VERSION)
772 #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_COMPCERT_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
773 #else
774 #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (0)
775 #endif
776
777 #if defined(JSON_HEDLEY_PELLES_VERSION)
778 #undef JSON_HEDLEY_PELLES_VERSION
779 #endif
780 #if defined(__POCC__)
781 #define JSON_HEDLEY_PELLES_VERSION JSON_HEDLEY_VERSION_ENCODE(__POCC__ / 100, __POCC__ % 100, 0)
782 #endif
783
784 #if defined(JSON_HEDLEY_PELLES_VERSION_CHECK)
785 #undef JSON_HEDLEY_PELLES_VERSION_CHECK
786 #endif
787 #if defined(JSON_HEDLEY_PELLES_VERSION)
788 #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PELLES_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
789 #else
790 #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (0)
791 #endif
792
793 #if defined(JSON_HEDLEY_MCST_LCC_VERSION)
794 #undef JSON_HEDLEY_MCST_LCC_VERSION
795 #endif
796 #if defined(__LCC__) && defined(__LCC_MINOR__)
797 #define JSON_HEDLEY_MCST_LCC_VERSION JSON_HEDLEY_VERSION_ENCODE(__LCC__ / 100, __LCC__ % 100, __LCC_MINOR__)
798 #endif
799
800 #if defined(JSON_HEDLEY_MCST_LCC_VERSION_CHECK)
801 #undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK
802 #endif
803 #if defined(JSON_HEDLEY_MCST_LCC_VERSION)
804 #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_MCST_LCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
805 #else
806 #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (0)
807 #endif
808
809 #if defined(JSON_HEDLEY_GCC_VERSION)
810 #undef JSON_HEDLEY_GCC_VERSION
811 #endif
812 #if \
813 defined(JSON_HEDLEY_GNUC_VERSION) && \
814 !defined(__clang__) && \
815 !defined(JSON_HEDLEY_INTEL_VERSION) && \
816 !defined(JSON_HEDLEY_PGI_VERSION) && \
817 !defined(JSON_HEDLEY_ARM_VERSION) && \
818 !defined(JSON_HEDLEY_CRAY_VERSION) && \
819 !defined(JSON_HEDLEY_TI_VERSION) && \
820 !defined(JSON_HEDLEY_TI_ARMCL_VERSION) && \
821 !defined(JSON_HEDLEY_TI_CL430_VERSION) && \
822 !defined(JSON_HEDLEY_TI_CL2000_VERSION) && \
823 !defined(JSON_HEDLEY_TI_CL6X_VERSION) && \
824 !defined(JSON_HEDLEY_TI_CL7X_VERSION) && \
825 !defined(JSON_HEDLEY_TI_CLPRU_VERSION) && \
826 !defined(__COMPCERT__) && \
827 !defined(JSON_HEDLEY_MCST_LCC_VERSION)
828 #define JSON_HEDLEY_GCC_VERSION JSON_HEDLEY_GNUC_VERSION
829 #endif
830
831 #if defined(JSON_HEDLEY_GCC_VERSION_CHECK)
832 #undef JSON_HEDLEY_GCC_VERSION_CHECK
833 #endif
834 #if defined(JSON_HEDLEY_GCC_VERSION)
835 #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
836 #else
837 #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (0)
838 #endif
839
840 #if defined(JSON_HEDLEY_HAS_ATTRIBUTE)
841 #undef JSON_HEDLEY_HAS_ATTRIBUTE
842 #endif
843 #if \
844 defined(__has_attribute) && \
845 ( \
846 (!defined(JSON_HEDLEY_IAR_VERSION) || JSON_HEDLEY_IAR_VERSION_CHECK(8,5,9)) \
847 )
848 # define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) __has_attribute(attribute)
849 #else
850 # define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) (0)
851 #endif
852
853 #if defined(JSON_HEDLEY_GNUC_HAS_ATTRIBUTE)
854 #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE
855 #endif
856 #if defined(__has_attribute)
857 #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
858 #else
859 #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
860 #endif
861
862 #if defined(JSON_HEDLEY_GCC_HAS_ATTRIBUTE)
863 #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE
864 #endif
865 #if defined(__has_attribute)
866 #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
867 #else
868 #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
869 #endif
870
871 #if defined(JSON_HEDLEY_HAS_CPP_ATTRIBUTE)
872 #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE
873 #endif
874 #if \
875 defined(__has_cpp_attribute) && \
876 defined(__cplusplus) && \
877 (!defined(JSON_HEDLEY_SUNPRO_VERSION) || JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0))
878 #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) __has_cpp_attribute(attribute)
879 #else
880 #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) (0)
881 #endif
882
883 #if defined(JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS)
884 #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS
885 #endif
886 #if !defined(__cplusplus) || !defined(__has_cpp_attribute)
887 #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) (0)
888 #elif \
889 !defined(JSON_HEDLEY_PGI_VERSION) && \
890 !defined(JSON_HEDLEY_IAR_VERSION) && \
891 (!defined(JSON_HEDLEY_SUNPRO_VERSION) || JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0)) && \
892 (!defined(JSON_HEDLEY_MSVC_VERSION) || JSON_HEDLEY_MSVC_VERSION_CHECK(19,20,0))
893 #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) JSON_HEDLEY_HAS_CPP_ATTRIBUTE(ns::attribute)
894 #else
895 #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) (0)
896 #endif
897
898 #if defined(JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE)
899 #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE
900 #endif
901 #if defined(__has_cpp_attribute) && defined(__cplusplus)
902 #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute)
903 #else
904 #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
905 #endif
906
907 #if defined(JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE)
908 #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE
909 #endif
910 #if defined(__has_cpp_attribute) && defined(__cplusplus)
911 #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute)
912 #else
913 #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
914 #endif
915
916 #if defined(JSON_HEDLEY_HAS_BUILTIN)
917 #undef JSON_HEDLEY_HAS_BUILTIN
918 #endif
919 #if defined(__has_builtin)
920 #define JSON_HEDLEY_HAS_BUILTIN(builtin) __has_builtin(builtin)
921 #else
922 #define JSON_HEDLEY_HAS_BUILTIN(builtin) (0)
923 #endif
924
925 #if defined(JSON_HEDLEY_GNUC_HAS_BUILTIN)
926 #undef JSON_HEDLEY_GNUC_HAS_BUILTIN
927 #endif
928 #if defined(__has_builtin)
929 #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin)
930 #else
931 #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
932 #endif
933
934 #if defined(JSON_HEDLEY_GCC_HAS_BUILTIN)
935 #undef JSON_HEDLEY_GCC_HAS_BUILTIN
936 #endif
937 #if defined(__has_builtin)
938 #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin)
939 #else
940 #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
941 #endif
942
943 #if defined(JSON_HEDLEY_HAS_FEATURE)
944 #undef JSON_HEDLEY_HAS_FEATURE
945 #endif
946 #if defined(__has_feature)
947 #define JSON_HEDLEY_HAS_FEATURE(feature) __has_feature(feature)
948 #else
949 #define JSON_HEDLEY_HAS_FEATURE(feature) (0)
950 #endif
951
952 #if defined(JSON_HEDLEY_GNUC_HAS_FEATURE)
953 #undef JSON_HEDLEY_GNUC_HAS_FEATURE
954 #endif
955 #if defined(__has_feature)
956 #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature)
957 #else
958 #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
959 #endif
960
961 #if defined(JSON_HEDLEY_GCC_HAS_FEATURE)
962 #undef JSON_HEDLEY_GCC_HAS_FEATURE
963 #endif
964 #if defined(__has_feature)
965 #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature)
966 #else
967 #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
968 #endif
969
970 #if defined(JSON_HEDLEY_HAS_EXTENSION)
971 #undef JSON_HEDLEY_HAS_EXTENSION
972 #endif
973 #if defined(__has_extension)
974 #define JSON_HEDLEY_HAS_EXTENSION(extension) __has_extension(extension)
975 #else
976 #define JSON_HEDLEY_HAS_EXTENSION(extension) (0)
977 #endif
978
979 #if defined(JSON_HEDLEY_GNUC_HAS_EXTENSION)
980 #undef JSON_HEDLEY_GNUC_HAS_EXTENSION
981 #endif
982 #if defined(__has_extension)
983 #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension)
984 #else
985 #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
986 #endif
987
988 #if defined(JSON_HEDLEY_GCC_HAS_EXTENSION)
989 #undef JSON_HEDLEY_GCC_HAS_EXTENSION
990 #endif
991 #if defined(__has_extension)
992 #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension)
993 #else
994 #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
995 #endif
996
997 #if defined(JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE)
998 #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE
999 #endif
1000 #if defined(__has_declspec_attribute)
1001 #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) __has_declspec_attribute(attribute)
1002 #else
1003 #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) (0)
1004 #endif
1005
1006 #if defined(JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE)
1007 #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE
1008 #endif
1009 #if defined(__has_declspec_attribute)
1010 #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute)
1011 #else
1012 #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
1013 #endif
1014
1015 #if defined(JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE)
1016 #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE
1017 #endif
1018 #if defined(__has_declspec_attribute)
1019 #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute)
1020 #else
1021 #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
1022 #endif
1023
1024 #if defined(JSON_HEDLEY_HAS_WARNING)
1025 #undef JSON_HEDLEY_HAS_WARNING
1026 #endif
1027 #if defined(__has_warning)
1028 #define JSON_HEDLEY_HAS_WARNING(warning) __has_warning(warning)
1029 #else
1030 #define JSON_HEDLEY_HAS_WARNING(warning) (0)
1031 #endif
1032
1033 #if defined(JSON_HEDLEY_GNUC_HAS_WARNING)
1034 #undef JSON_HEDLEY_GNUC_HAS_WARNING
1035 #endif
1036 #if defined(__has_warning)
1037 #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning)
1038 #else
1039 #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
1040 #endif
1041
1042 #if defined(JSON_HEDLEY_GCC_HAS_WARNING)
1043 #undef JSON_HEDLEY_GCC_HAS_WARNING
1044 #endif
1045 #if defined(__has_warning)
1046 #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning)
1047 #else
1048 #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
1049 #endif
1050
1051 #if \
1052 (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
1053 defined(__clang__) || \
1054 JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \
1055 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1056 JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \
1057 JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \
1058 JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1059 JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1060 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \
1061 JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \
1062 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \
1063 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,0,0) || \
1064 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1065 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1066 JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0) || \
1067 JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,17) || \
1068 JSON_HEDLEY_SUNPRO_VERSION_CHECK(8,0,0) || \
1069 (JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) && defined(__C99_PRAGMA_OPERATOR))
1070 #define JSON_HEDLEY_PRAGMA(value) _Pragma(#value)
1071 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
1072 #define JSON_HEDLEY_PRAGMA(value) __pragma(value)
1073 #else
1074 #define JSON_HEDLEY_PRAGMA(value)
1075 #endif
1076
1077 #if defined(JSON_HEDLEY_DIAGNOSTIC_PUSH)
1078 #undef JSON_HEDLEY_DIAGNOSTIC_PUSH
1079 #endif
1080 #if defined(JSON_HEDLEY_DIAGNOSTIC_POP)
1081 #undef JSON_HEDLEY_DIAGNOSTIC_POP
1082 #endif
1083 #if defined(__clang__)
1084 #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("clang diagnostic push")
1085 #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("clang diagnostic pop")
1086 #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
1087 #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)")
1088 #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)")
1089 #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0)
1090 #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")
1091 #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("GCC diagnostic pop")
1092 #elif \
1093 JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \
1094 JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1095 #define JSON_HEDLEY_DIAGNOSTIC_PUSH __pragma(warning(push))
1096 #define JSON_HEDLEY_DIAGNOSTIC_POP __pragma(warning(pop))
1097 #elif JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0)
1098 #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("push")
1099 #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("pop")
1100 #elif \
1101 JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1102 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1103 JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,4,0) || \
1104 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \
1105 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1106 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0)
1107 #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("diag_push")
1108 #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("diag_pop")
1109 #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0)
1110 #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)")
1111 #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)")
1112 #else
1113 #define JSON_HEDLEY_DIAGNOSTIC_PUSH
1114 #define JSON_HEDLEY_DIAGNOSTIC_POP
1115 #endif
1116
1117 /* JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_ is for
1118 HEDLEY INTERNAL USE ONLY. API subject to change without notice. */
1119 #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_)
1120 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_
1121 #endif
1122 #if defined(__cplusplus)
1123 # if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat")
1124 # if JSON_HEDLEY_HAS_WARNING("-Wc++17-extensions")
1125 # if JSON_HEDLEY_HAS_WARNING("-Wc++1z-extensions")
1126 # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
1127 JSON_HEDLEY_DIAGNOSTIC_PUSH \
1128 _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
1129 _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \
1130 _Pragma("clang diagnostic ignored \"-Wc++1z-extensions\"") \
1131 xpr \
1132 JSON_HEDLEY_DIAGNOSTIC_POP
1133 # else
1134 # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
1135 JSON_HEDLEY_DIAGNOSTIC_PUSH \
1136 _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
1137 _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \
1138 xpr \
1139 JSON_HEDLEY_DIAGNOSTIC_POP
1140 # endif
1141 # else
1142 # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
1143 JSON_HEDLEY_DIAGNOSTIC_PUSH \
1144 _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
1145 xpr \
1146 JSON_HEDLEY_DIAGNOSTIC_POP
1147 # endif
1148 # endif
1149 #endif
1150 #if !defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_)
1151 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(x) x
1152 #endif
1153
1154 #if defined(JSON_HEDLEY_CONST_CAST)
1155 #undef JSON_HEDLEY_CONST_CAST
1156 #endif
1157 #if defined(__cplusplus)
1158 # define JSON_HEDLEY_CONST_CAST(T, expr) (const_cast<T>(expr))
1159 #elif \
1160 JSON_HEDLEY_HAS_WARNING("-Wcast-qual") || \
1161 JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0) || \
1162 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
1163 # define JSON_HEDLEY_CONST_CAST(T, expr) (__extension__ ({ \
1164 JSON_HEDLEY_DIAGNOSTIC_PUSH \
1165 JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL \
1166 ((T) (expr)); \
1167 JSON_HEDLEY_DIAGNOSTIC_POP \
1168 }))
1169 #else
1170 # define JSON_HEDLEY_CONST_CAST(T, expr) ((T) (expr))
1171 #endif
1172
1173 #if defined(JSON_HEDLEY_REINTERPRET_CAST)
1174 #undef JSON_HEDLEY_REINTERPRET_CAST
1175 #endif
1176 #if defined(__cplusplus)
1177 #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) (reinterpret_cast<T>(expr))
1178 #else
1179 #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) ((T) (expr))
1180 #endif
1181
1182 #if defined(JSON_HEDLEY_STATIC_CAST)
1183 #undef JSON_HEDLEY_STATIC_CAST
1184 #endif
1185 #if defined(__cplusplus)
1186 #define JSON_HEDLEY_STATIC_CAST(T, expr) (static_cast<T>(expr))
1187 #else
1188 #define JSON_HEDLEY_STATIC_CAST(T, expr) ((T) (expr))
1189 #endif
1190
1191 #if defined(JSON_HEDLEY_CPP_CAST)
1192 #undef JSON_HEDLEY_CPP_CAST
1193 #endif
1194 #if defined(__cplusplus)
1195 # if JSON_HEDLEY_HAS_WARNING("-Wold-style-cast")
1196 # define JSON_HEDLEY_CPP_CAST(T, expr) \
1197 JSON_HEDLEY_DIAGNOSTIC_PUSH \
1198 _Pragma("clang diagnostic ignored \"-Wold-style-cast\"") \
1199 ((T) (expr)) \
1200 JSON_HEDLEY_DIAGNOSTIC_POP
1201 # elif JSON_HEDLEY_IAR_VERSION_CHECK(8,3,0)
1202 # define JSON_HEDLEY_CPP_CAST(T, expr) \
1203 JSON_HEDLEY_DIAGNOSTIC_PUSH \
1204 _Pragma("diag_suppress=Pe137") \
1205 JSON_HEDLEY_DIAGNOSTIC_POP
1206 # else
1207 # define JSON_HEDLEY_CPP_CAST(T, expr) ((T) (expr))
1208 # endif
1209 #else
1210 # define JSON_HEDLEY_CPP_CAST(T, expr) (expr)
1211 #endif
1212
1213 #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED)
1214 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
1215 #endif
1216 #if JSON_HEDLEY_HAS_WARNING("-Wdeprecated-declarations")
1217 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")
1218 #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
1219 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warning(disable:1478 1786)")
1220 #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1221 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:1478 1786))
1222 #elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0)
1223 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1216,1444,1445")
1224 #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
1225 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444")
1226 #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0)
1227 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
1228 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
1229 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:4996))
1230 #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1231 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444")
1232 #elif \
1233 JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1234 (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1235 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1236 (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1237 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1238 (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1239 JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1240 (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1241 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1242 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1243 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0)
1244 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1291,1718")
1245 #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) && !defined(__cplusplus)
1246 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,E_DEPRECATED_ATT,E_DEPRECATED_ATT_MESS)")
1247 #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) && defined(__cplusplus)
1248 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,symdeprecated,symdeprecated2)")
1249 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
1250 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress=Pe1444,Pe1215")
1251 #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0)
1252 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warn(disable:2241)")
1253 #else
1254 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
1255 #endif
1256
1257 #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS)
1258 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
1259 #endif
1260 #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
1261 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("clang diagnostic ignored \"-Wunknown-pragmas\"")
1262 #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
1263 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("warning(disable:161)")
1264 #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1265 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:161))
1266 #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
1267 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 1675")
1268 #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0)
1269 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("GCC diagnostic ignored \"-Wunknown-pragmas\"")
1270 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
1271 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:4068))
1272 #elif \
1273 JSON_HEDLEY_TI_VERSION_CHECK(16,9,0) || \
1274 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \
1275 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1276 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0)
1277 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163")
1278 #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0)
1279 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163")
1280 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
1281 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress=Pe161")
1282 #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1283 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 161")
1284 #else
1285 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
1286 #endif
1287
1288 #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES)
1289 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
1290 #endif
1291 #if JSON_HEDLEY_HAS_WARNING("-Wunknown-attributes")
1292 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("clang diagnostic ignored \"-Wunknown-attributes\"")
1293 #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0)
1294 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
1295 #elif JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0)
1296 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("warning(disable:1292)")
1297 #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1298 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:1292))
1299 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,0)
1300 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:5030))
1301 #elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0)
1302 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097,1098")
1303 #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
1304 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097")
1305 #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus)
1306 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("error_messages(off,attrskipunsup)")
1307 #elif \
1308 JSON_HEDLEY_TI_VERSION_CHECK(18,1,0) || \
1309 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \
1310 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0)
1311 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1173")
1312 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
1313 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress=Pe1097")
1314 #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1315 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097")
1316 #else
1317 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
1318 #endif
1319
1320 #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL)
1321 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
1322 #endif
1323 #if JSON_HEDLEY_HAS_WARNING("-Wcast-qual")
1324 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("clang diagnostic ignored \"-Wcast-qual\"")
1325 #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
1326 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("warning(disable:2203 2331)")
1327 #elif JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0)
1328 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("GCC diagnostic ignored \"-Wcast-qual\"")
1329 #else
1330 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
1331 #endif
1332
1333 #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION)
1334 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
1335 #endif
1336 #if JSON_HEDLEY_HAS_WARNING("-Wunused-function")
1337 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("clang diagnostic ignored \"-Wunused-function\"")
1338 #elif JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0)
1339 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("GCC diagnostic ignored \"-Wunused-function\"")
1340 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(1,0,0)
1341 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION __pragma(warning(disable:4505))
1342 #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1343 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("diag_suppress 3142")
1344 #else
1345 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
1346 #endif
1347
1348 #if defined(JSON_HEDLEY_DEPRECATED)
1349 #undef JSON_HEDLEY_DEPRECATED
1350 #endif
1351 #if defined(JSON_HEDLEY_DEPRECATED_FOR)
1352 #undef JSON_HEDLEY_DEPRECATED_FOR
1353 #endif
1354 #if \
1355 JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
1356 JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1357 #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated("Since " # since))
1358 #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated("Since " #since "; use " #replacement))
1359 #elif \
1360 (JSON_HEDLEY_HAS_EXTENSION(attribute_deprecated_with_message) && !defined(JSON_HEDLEY_IAR_VERSION)) || \
1361 JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \
1362 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1363 JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \
1364 JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) || \
1365 JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
1366 JSON_HEDLEY_TI_VERSION_CHECK(18,1,0) || \
1367 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(18,1,0) || \
1368 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \
1369 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1370 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0) || \
1371 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1372 #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__("Since " #since)))
1373 #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__("Since " #since "; use " #replacement)))
1374 #elif defined(__cplusplus) && (__cplusplus >= 201402L)
1375 #define JSON_HEDLEY_DEPRECATED(since) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since)]])
1376 #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since "; use " #replacement)]])
1377 #elif \
1378 JSON_HEDLEY_HAS_ATTRIBUTE(deprecated) || \
1379 JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
1380 JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1381 JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1382 (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1383 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1384 (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1385 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1386 (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1387 JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1388 (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1389 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1390 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1391 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1392 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
1393 JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
1394 #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__))
1395 #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__))
1396 #elif \
1397 JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
1398 JSON_HEDLEY_PELLES_VERSION_CHECK(6,50,0) || \
1399 JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1400 #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated)
1401 #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated)
1402 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
1403 #define JSON_HEDLEY_DEPRECATED(since) _Pragma("deprecated")
1404 #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) _Pragma("deprecated")
1405 #else
1406 #define JSON_HEDLEY_DEPRECATED(since)
1407 #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement)
1408 #endif
1409
1410 #if defined(JSON_HEDLEY_UNAVAILABLE)
1411 #undef JSON_HEDLEY_UNAVAILABLE
1412 #endif
1413 #if \
1414 JSON_HEDLEY_HAS_ATTRIBUTE(warning) || \
1415 JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0) || \
1416 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1417 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1418 #define JSON_HEDLEY_UNAVAILABLE(available_since) __attribute__((__warning__("Not available until " #available_since)))
1419 #else
1420 #define JSON_HEDLEY_UNAVAILABLE(available_since)
1421 #endif
1422
1423 #if defined(JSON_HEDLEY_WARN_UNUSED_RESULT)
1424 #undef JSON_HEDLEY_WARN_UNUSED_RESULT
1425 #endif
1426 #if defined(JSON_HEDLEY_WARN_UNUSED_RESULT_MSG)
1427 #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG
1428 #endif
1429 #if \
1430 JSON_HEDLEY_HAS_ATTRIBUTE(warn_unused_result) || \
1431 JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
1432 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1433 JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1434 (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1435 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1436 (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1437 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1438 (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1439 JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1440 (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1441 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1442 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1443 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1444 (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \
1445 JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
1446 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1447 #define JSON_HEDLEY_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
1448 #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) __attribute__((__warn_unused_result__))
1449 #elif (JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard) >= 201907L)
1450 #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
1451 #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard(msg)]])
1452 #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard)
1453 #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
1454 #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
1455 #elif defined(_Check_return_) /* SAL */
1456 #define JSON_HEDLEY_WARN_UNUSED_RESULT _Check_return_
1457 #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) _Check_return_
1458 #else
1459 #define JSON_HEDLEY_WARN_UNUSED_RESULT
1460 #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg)
1461 #endif
1462
1463 #if defined(JSON_HEDLEY_SENTINEL)
1464 #undef JSON_HEDLEY_SENTINEL
1465 #endif
1466 #if \
1467 JSON_HEDLEY_HAS_ATTRIBUTE(sentinel) || \
1468 JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
1469 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1470 JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) || \
1471 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1472 #define JSON_HEDLEY_SENTINEL(position) __attribute__((__sentinel__(position)))
1473 #else
1474 #define JSON_HEDLEY_SENTINEL(position)
1475 #endif
1476
1477 #if defined(JSON_HEDLEY_NO_RETURN)
1478 #undef JSON_HEDLEY_NO_RETURN
1479 #endif
1480 #if JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
1481 #define JSON_HEDLEY_NO_RETURN __noreturn
1482 #elif \
1483 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1484 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1485 #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__))
1486 #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
1487 #define JSON_HEDLEY_NO_RETURN _Noreturn
1488 #elif defined(__cplusplus) && (__cplusplus >= 201103L)
1489 #define JSON_HEDLEY_NO_RETURN JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[noreturn]])
1490 #elif \
1491 JSON_HEDLEY_HAS_ATTRIBUTE(noreturn) || \
1492 JSON_HEDLEY_GCC_VERSION_CHECK(3,2,0) || \
1493 JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
1494 JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1495 JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1496 JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1497 (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1498 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1499 (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1500 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1501 (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1502 JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1503 (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1504 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1505 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1506 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1507 JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
1508 #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__))
1509 #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
1510 #define JSON_HEDLEY_NO_RETURN _Pragma("does_not_return")
1511 #elif \
1512 JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
1513 JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1514 #define JSON_HEDLEY_NO_RETURN __declspec(noreturn)
1515 #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus)
1516 #define JSON_HEDLEY_NO_RETURN _Pragma("FUNC_NEVER_RETURNS;")
1517 #elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3,2,0)
1518 #define JSON_HEDLEY_NO_RETURN __attribute((noreturn))
1519 #elif JSON_HEDLEY_PELLES_VERSION_CHECK(9,0,0)
1520 #define JSON_HEDLEY_NO_RETURN __declspec(noreturn)
1521 #else
1522 #define JSON_HEDLEY_NO_RETURN
1523 #endif
1524
1525 #if defined(JSON_HEDLEY_NO_ESCAPE)
1526 #undef JSON_HEDLEY_NO_ESCAPE
1527 #endif
1528 #if JSON_HEDLEY_HAS_ATTRIBUTE(noescape)
1529 #define JSON_HEDLEY_NO_ESCAPE __attribute__((__noescape__))
1530 #else
1531 #define JSON_HEDLEY_NO_ESCAPE
1532 #endif
1533
1534 #if defined(JSON_HEDLEY_UNREACHABLE)
1535 #undef JSON_HEDLEY_UNREACHABLE
1536 #endif
1537 #if defined(JSON_HEDLEY_UNREACHABLE_RETURN)
1538 #undef JSON_HEDLEY_UNREACHABLE_RETURN
1539 #endif
1540 #if defined(JSON_HEDLEY_ASSUME)
1541 #undef JSON_HEDLEY_ASSUME
1542 #endif
1543 #if \
1544 JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
1545 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1546 JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1547 #define JSON_HEDLEY_ASSUME(expr) __assume(expr)
1548 #elif JSON_HEDLEY_HAS_BUILTIN(__builtin_assume)
1549 #define JSON_HEDLEY_ASSUME(expr) __builtin_assume(expr)
1550 #elif \
1551 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
1552 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0)
1553 #if defined(__cplusplus)
1554 #define JSON_HEDLEY_ASSUME(expr) std::_nassert(expr)
1555 #else
1556 #define JSON_HEDLEY_ASSUME(expr) _nassert(expr)
1557 #endif
1558 #endif
1559 #if \
1560 (JSON_HEDLEY_HAS_BUILTIN(__builtin_unreachable) && (!defined(JSON_HEDLEY_ARM_VERSION))) || \
1561 JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \
1562 JSON_HEDLEY_PGI_VERSION_CHECK(18,10,0) || \
1563 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1564 JSON_HEDLEY_IBM_VERSION_CHECK(13,1,5) || \
1565 JSON_HEDLEY_CRAY_VERSION_CHECK(10,0,0) || \
1566 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1567 #define JSON_HEDLEY_UNREACHABLE() __builtin_unreachable()
1568 #elif defined(JSON_HEDLEY_ASSUME)
1569 #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0)
1570 #endif
1571 #if !defined(JSON_HEDLEY_ASSUME)
1572 #if defined(JSON_HEDLEY_UNREACHABLE)
1573 #define JSON_HEDLEY_ASSUME(expr) JSON_HEDLEY_STATIC_CAST(void, ((expr) ? 1 : (JSON_HEDLEY_UNREACHABLE(), 1)))
1574 #else
1575 #define JSON_HEDLEY_ASSUME(expr) JSON_HEDLEY_STATIC_CAST(void, expr)
1576 #endif
1577 #endif
1578 #if defined(JSON_HEDLEY_UNREACHABLE)
1579 #if \
1580 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
1581 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0)
1582 #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return (JSON_HEDLEY_STATIC_CAST(void, JSON_HEDLEY_ASSUME(0)), (value))
1583 #else
1584 #define JSON_HEDLEY_UNREACHABLE_RETURN(value) JSON_HEDLEY_UNREACHABLE()
1585 #endif
1586 #else
1587 #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return (value)
1588 #endif
1589 #if !defined(JSON_HEDLEY_UNREACHABLE)
1590 #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0)
1591 #endif
1592
1593 JSON_HEDLEY_DIAGNOSTIC_PUSH
1594 #if JSON_HEDLEY_HAS_WARNING("-Wpedantic")
1595 #pragma clang diagnostic ignored "-Wpedantic"
1596 #endif
1597 #if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat-pedantic") && defined(__cplusplus)
1598 #pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
1599 #endif
1600 #if JSON_HEDLEY_GCC_HAS_WARNING("-Wvariadic-macros",4,0,0)
1601 #if defined(__clang__)
1602 #pragma clang diagnostic ignored "-Wvariadic-macros"
1603 #elif defined(JSON_HEDLEY_GCC_VERSION)
1604 #pragma GCC diagnostic ignored "-Wvariadic-macros"
1605 #endif
1606 #endif
1607 #if defined(JSON_HEDLEY_NON_NULL)
1608 #undef JSON_HEDLEY_NON_NULL
1609 #endif
1610 #if \
1611 JSON_HEDLEY_HAS_ATTRIBUTE(nonnull) || \
1612 JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
1613 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1614 JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0)
1615 #define JSON_HEDLEY_NON_NULL(...) __attribute__((__nonnull__(__VA_ARGS__)))
1616 #else
1617 #define JSON_HEDLEY_NON_NULL(...)
1618 #endif
1619 JSON_HEDLEY_DIAGNOSTIC_POP
1620
1621 #if defined(JSON_HEDLEY_PRINTF_FORMAT)
1622 #undef JSON_HEDLEY_PRINTF_FORMAT
1623 #endif
1624 #if defined(__MINGW32__) && JSON_HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && !defined(__USE_MINGW_ANSI_STDIO)
1625 #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(ms_printf, string_idx, first_to_check)))
1626 #elif defined(__MINGW32__) && JSON_HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && defined(__USE_MINGW_ANSI_STDIO)
1627 #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(gnu_printf, string_idx, first_to_check)))
1628 #elif \
1629 JSON_HEDLEY_HAS_ATTRIBUTE(format) || \
1630 JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
1631 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1632 JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \
1633 JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1634 JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1635 (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1636 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1637 (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1638 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1639 (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1640 JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1641 (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1642 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1643 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1644 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1645 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1646 #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(__printf__, string_idx, first_to_check)))
1647 #elif JSON_HEDLEY_PELLES_VERSION_CHECK(6,0,0)
1648 #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __declspec(vaformat(printf,string_idx,first_to_check))
1649 #else
1650 #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check)
1651 #endif
1652
1653 #if defined(JSON_HEDLEY_CONSTEXPR)
1654 #undef JSON_HEDLEY_CONSTEXPR
1655 #endif
1656 #if defined(__cplusplus)
1657 #if __cplusplus >= 201103L
1658 #define JSON_HEDLEY_CONSTEXPR JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(constexpr)
1659 #endif
1660 #endif
1661 #if !defined(JSON_HEDLEY_CONSTEXPR)
1662 #define JSON_HEDLEY_CONSTEXPR
1663 #endif
1664
1665 #if defined(JSON_HEDLEY_PREDICT)
1666 #undef JSON_HEDLEY_PREDICT
1667 #endif
1668 #if defined(JSON_HEDLEY_LIKELY)
1669 #undef JSON_HEDLEY_LIKELY
1670 #endif
1671 #if defined(JSON_HEDLEY_UNLIKELY)
1672 #undef JSON_HEDLEY_UNLIKELY
1673 #endif
1674 #if defined(JSON_HEDLEY_UNPREDICTABLE)
1675 #undef JSON_HEDLEY_UNPREDICTABLE
1676 #endif
1677 #if JSON_HEDLEY_HAS_BUILTIN(__builtin_unpredictable)
1678 #define JSON_HEDLEY_UNPREDICTABLE(expr) __builtin_unpredictable((expr))
1679 #endif
1680 #if \
1681 (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect_with_probability) && !defined(JSON_HEDLEY_PGI_VERSION)) || \
1682 JSON_HEDLEY_GCC_VERSION_CHECK(9,0,0) || \
1683 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1684 # define JSON_HEDLEY_PREDICT(expr, value, probability) __builtin_expect_with_probability( (expr), (value), (probability))
1685 # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) __builtin_expect_with_probability(!!(expr), 1 , (probability))
1686 # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) __builtin_expect_with_probability(!!(expr), 0 , (probability))
1687 # define JSON_HEDLEY_LIKELY(expr) __builtin_expect (!!(expr), 1 )
1688 # define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect (!!(expr), 0 )
1689 #elif \
1690 (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \
1691 JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \
1692 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1693 (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \
1694 JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1695 JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1696 JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1697 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \
1698 JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \
1699 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \
1700 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
1701 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1702 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1703 JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,27) || \
1704 JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
1705 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1706 # define JSON_HEDLEY_PREDICT(expr, expected, probability) \
1707 (((probability) >= 0.9) ? __builtin_expect((expr), (expected)) : (JSON_HEDLEY_STATIC_CAST(void, expected), (expr)))
1708 # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) \
1709 (__extension__ ({ \
1710 double hedley_probability_ = (probability); \
1711 ((hedley_probability_ >= 0.9) ? __builtin_expect(!!(expr), 1) : ((hedley_probability_ <= 0.1) ? __builtin_expect(!!(expr), 0) : !!(expr))); \
1712 }))
1713 # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) \
1714 (__extension__ ({ \
1715 double hedley_probability_ = (probability); \
1716 ((hedley_probability_ >= 0.9) ? __builtin_expect(!!(expr), 0) : ((hedley_probability_ <= 0.1) ? __builtin_expect(!!(expr), 1) : !!(expr))); \
1717 }))
1718 # define JSON_HEDLEY_LIKELY(expr) __builtin_expect(!!(expr), 1)
1719 # define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect(!!(expr), 0)
1720 #else
1721 # define JSON_HEDLEY_PREDICT(expr, expected, probability) (JSON_HEDLEY_STATIC_CAST(void, expected), (expr))
1722 # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) (!!(expr))
1723 # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) (!!(expr))
1724 # define JSON_HEDLEY_LIKELY(expr) (!!(expr))
1725 # define JSON_HEDLEY_UNLIKELY(expr) (!!(expr))
1726 #endif
1727 #if !defined(JSON_HEDLEY_UNPREDICTABLE)
1728 #define JSON_HEDLEY_UNPREDICTABLE(expr) JSON_HEDLEY_PREDICT(expr, 1, 0.5)
1729 #endif
1730
1731 #if defined(JSON_HEDLEY_MALLOC)
1732 #undef JSON_HEDLEY_MALLOC
1733 #endif
1734 #if \
1735 JSON_HEDLEY_HAS_ATTRIBUTE(malloc) || \
1736 JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
1737 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1738 JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
1739 JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1740 JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \
1741 JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1742 (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1743 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1744 (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1745 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1746 (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1747 JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1748 (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1749 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1750 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1751 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1752 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1753 #define JSON_HEDLEY_MALLOC __attribute__((__malloc__))
1754 #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
1755 #define JSON_HEDLEY_MALLOC _Pragma("returns_new_memory")
1756 #elif \
1757 JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
1758 JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1759 #define JSON_HEDLEY_MALLOC __declspec(restrict)
1760 #else
1761 #define JSON_HEDLEY_MALLOC
1762 #endif
1763
1764 #if defined(JSON_HEDLEY_PURE)
1765 #undef JSON_HEDLEY_PURE
1766 #endif
1767 #if \
1768 JSON_HEDLEY_HAS_ATTRIBUTE(pure) || \
1769 JSON_HEDLEY_GCC_VERSION_CHECK(2,96,0) || \
1770 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1771 JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
1772 JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1773 JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1774 JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1775 (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1776 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1777 (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1778 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1779 (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1780 JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1781 (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1782 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1783 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1784 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1785 JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
1786 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1787 # define JSON_HEDLEY_PURE __attribute__((__pure__))
1788 #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
1789 # define JSON_HEDLEY_PURE _Pragma("does_not_write_global_data")
1790 #elif defined(__cplusplus) && \
1791 ( \
1792 JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \
1793 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0) || \
1794 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) \
1795 )
1796 # define JSON_HEDLEY_PURE _Pragma("FUNC_IS_PURE;")
1797 #else
1798 # define JSON_HEDLEY_PURE
1799 #endif
1800
1801 #if defined(JSON_HEDLEY_CONST)
1802 #undef JSON_HEDLEY_CONST
1803 #endif
1804 #if \
1805 JSON_HEDLEY_HAS_ATTRIBUTE(const) || \
1806 JSON_HEDLEY_GCC_VERSION_CHECK(2,5,0) || \
1807 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1808 JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
1809 JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1810 JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1811 JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1812 (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1813 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1814 (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1815 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1816 (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1817 JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1818 (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1819 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1820 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1821 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1822 JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
1823 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1824 #define JSON_HEDLEY_CONST __attribute__((__const__))
1825 #elif \
1826 JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
1827 #define JSON_HEDLEY_CONST _Pragma("no_side_effect")
1828 #else
1829 #define JSON_HEDLEY_CONST JSON_HEDLEY_PURE
1830 #endif
1831
1832 #if defined(JSON_HEDLEY_RESTRICT)
1833 #undef JSON_HEDLEY_RESTRICT
1834 #endif
1835 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && !defined(__cplusplus)
1836 #define JSON_HEDLEY_RESTRICT restrict
1837 #elif \
1838 JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
1839 JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
1840 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1841 JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
1842 JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1843 JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1844 JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
1845 JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1846 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,4) || \
1847 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \
1848 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1849 (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus)) || \
1850 JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \
1851 defined(__clang__) || \
1852 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1853 #define JSON_HEDLEY_RESTRICT __restrict
1854 #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,3,0) && !defined(__cplusplus)
1855 #define JSON_HEDLEY_RESTRICT _Restrict
1856 #else
1857 #define JSON_HEDLEY_RESTRICT
1858 #endif
1859
1860 #if defined(JSON_HEDLEY_INLINE)
1861 #undef JSON_HEDLEY_INLINE
1862 #endif
1863 #if \
1864 (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
1865 (defined(__cplusplus) && (__cplusplus >= 199711L))
1866 #define JSON_HEDLEY_INLINE inline
1867 #elif \
1868 defined(JSON_HEDLEY_GCC_VERSION) || \
1869 JSON_HEDLEY_ARM_VERSION_CHECK(6,2,0)
1870 #define JSON_HEDLEY_INLINE __inline__
1871 #elif \
1872 JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \
1873 JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
1874 JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1875 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,1,0) || \
1876 JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \
1877 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
1878 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \
1879 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1880 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1881 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1882 #define JSON_HEDLEY_INLINE __inline
1883 #else
1884 #define JSON_HEDLEY_INLINE
1885 #endif
1886
1887 #if defined(JSON_HEDLEY_ALWAYS_INLINE)
1888 #undef JSON_HEDLEY_ALWAYS_INLINE
1889 #endif
1890 #if \
1891 JSON_HEDLEY_HAS_ATTRIBUTE(always_inline) || \
1892 JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
1893 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1894 JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
1895 JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1896 JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1897 JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1898 (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1899 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1900 (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1901 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1902 (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1903 JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1904 (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1905 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1906 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1907 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1908 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
1909 JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
1910 # define JSON_HEDLEY_ALWAYS_INLINE __attribute__((__always_inline__)) JSON_HEDLEY_INLINE
1911 #elif \
1912 JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \
1913 JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1914 # define JSON_HEDLEY_ALWAYS_INLINE __forceinline
1915 #elif defined(__cplusplus) && \
1916 ( \
1917 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1918 JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1919 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1920 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
1921 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1922 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) \
1923 )
1924 # define JSON_HEDLEY_ALWAYS_INLINE _Pragma("FUNC_ALWAYS_INLINE;")
1925 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
1926 # define JSON_HEDLEY_ALWAYS_INLINE _Pragma("inline=forced")
1927 #else
1928 # define JSON_HEDLEY_ALWAYS_INLINE JSON_HEDLEY_INLINE
1929 #endif
1930
1931 #if defined(JSON_HEDLEY_NEVER_INLINE)
1932 #undef JSON_HEDLEY_NEVER_INLINE
1933 #endif
1934 #if \
1935 JSON_HEDLEY_HAS_ATTRIBUTE(noinline) || \
1936 JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
1937 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1938 JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
1939 JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1940 JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1941 JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1942 (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1943 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1944 (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1945 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1946 (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1947 JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1948 (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1949 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1950 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1951 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1952 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
1953 JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
1954 #define JSON_HEDLEY_NEVER_INLINE __attribute__((__noinline__))
1955 #elif \
1956 JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
1957 JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1958 #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline)
1959 #elif JSON_HEDLEY_PGI_VERSION_CHECK(10,2,0)
1960 #define JSON_HEDLEY_NEVER_INLINE _Pragma("noinline")
1961 #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus)
1962 #define JSON_HEDLEY_NEVER_INLINE _Pragma("FUNC_CANNOT_INLINE;")
1963 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
1964 #define JSON_HEDLEY_NEVER_INLINE _Pragma("inline=never")
1965 #elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3,2,0)
1966 #define JSON_HEDLEY_NEVER_INLINE __attribute((noinline))
1967 #elif JSON_HEDLEY_PELLES_VERSION_CHECK(9,0,0)
1968 #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline)
1969 #else
1970 #define JSON_HEDLEY_NEVER_INLINE
1971 #endif
1972
1973 #if defined(JSON_HEDLEY_PRIVATE)
1974 #undef JSON_HEDLEY_PRIVATE
1975 #endif
1976 #if defined(JSON_HEDLEY_PUBLIC)
1977 #undef JSON_HEDLEY_PUBLIC
1978 #endif
1979 #if defined(JSON_HEDLEY_IMPORT)
1980 #undef JSON_HEDLEY_IMPORT
1981 #endif
1982 #if defined(_WIN32) || defined(__CYGWIN__)
1983 # define JSON_HEDLEY_PRIVATE
1984 # define JSON_HEDLEY_PUBLIC __declspec(dllexport)
1985 # define JSON_HEDLEY_IMPORT __declspec(dllimport)
1986 #else
1987 # if \
1988 JSON_HEDLEY_HAS_ATTRIBUTE(visibility) || \
1989 JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
1990 JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
1991 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1992 JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1993 JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
1994 ( \
1995 defined(__TI_EABI__) && \
1996 ( \
1997 (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1998 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) \
1999 ) \
2000 ) || \
2001 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
2002 # define JSON_HEDLEY_PRIVATE __attribute__((__visibility__("hidden")))
2003 # define JSON_HEDLEY_PUBLIC __attribute__((__visibility__("default")))
2004 # else
2005 # define JSON_HEDLEY_PRIVATE
2006 # define JSON_HEDLEY_PUBLIC
2007 # endif
2008 # define JSON_HEDLEY_IMPORT extern
2009 #endif
2010
2011 #if defined(JSON_HEDLEY_NO_THROW)
2012 #undef JSON_HEDLEY_NO_THROW
2013 #endif
2014 #if \
2015 JSON_HEDLEY_HAS_ATTRIBUTE(nothrow) || \
2016 JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
2017 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
2018 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
2019 #define JSON_HEDLEY_NO_THROW __attribute__((__nothrow__))
2020 #elif \
2021 JSON_HEDLEY_MSVC_VERSION_CHECK(13,1,0) || \
2022 JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
2023 JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0)
2024 #define JSON_HEDLEY_NO_THROW __declspec(nothrow)
2025 #else
2026 #define JSON_HEDLEY_NO_THROW
2027 #endif
2028
2029 #if defined(JSON_HEDLEY_FALL_THROUGH)
2030 #undef JSON_HEDLEY_FALL_THROUGH
2031 #endif
2032 #if \
2033 JSON_HEDLEY_HAS_ATTRIBUTE(fallthrough) || \
2034 JSON_HEDLEY_GCC_VERSION_CHECK(7,0,0) || \
2035 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
2036 #define JSON_HEDLEY_FALL_THROUGH __attribute__((__fallthrough__))
2037 #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(clang,fallthrough)
2038 #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[clang::fallthrough]])
2039 #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(fallthrough)
2040 #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[fallthrough]])
2041 #elif defined(__fallthrough) /* SAL */
2042 #define JSON_HEDLEY_FALL_THROUGH __fallthrough
2043 #else
2044 #define JSON_HEDLEY_FALL_THROUGH
2045 #endif
2046
2047 #if defined(JSON_HEDLEY_RETURNS_NON_NULL)
2048 #undef JSON_HEDLEY_RETURNS_NON_NULL
2049 #endif
2050 #if \
2051 JSON_HEDLEY_HAS_ATTRIBUTE(returns_nonnull) || \
2052 JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \
2053 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
2054 #define JSON_HEDLEY_RETURNS_NON_NULL __attribute__((__returns_nonnull__))
2055 #elif defined(_Ret_notnull_) /* SAL */
2056 #define JSON_HEDLEY_RETURNS_NON_NULL _Ret_notnull_
2057 #else
2058 #define JSON_HEDLEY_RETURNS_NON_NULL
2059 #endif
2060
2061 #if defined(JSON_HEDLEY_ARRAY_PARAM)
2062 #undef JSON_HEDLEY_ARRAY_PARAM
2063 #endif
2064 #if \
2065 defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
2066 !defined(__STDC_NO_VLA__) && \
2067 !defined(__cplusplus) && \
2068 !defined(JSON_HEDLEY_PGI_VERSION) && \
2069 !defined(JSON_HEDLEY_TINYC_VERSION)
2070 #define JSON_HEDLEY_ARRAY_PARAM(name) (name)
2071 #else
2072 #define JSON_HEDLEY_ARRAY_PARAM(name)
2073 #endif
2074
2075 #if defined(JSON_HEDLEY_IS_CONSTANT)
2076 #undef JSON_HEDLEY_IS_CONSTANT
2077 #endif
2078 #if defined(JSON_HEDLEY_REQUIRE_CONSTEXPR)
2079 #undef JSON_HEDLEY_REQUIRE_CONSTEXPR
2080 #endif
2081 /* JSON_HEDLEY_IS_CONSTEXPR_ is for
2082 HEDLEY INTERNAL USE ONLY. API subject to change without notice. */
2083 #if defined(JSON_HEDLEY_IS_CONSTEXPR_)
2084 #undef JSON_HEDLEY_IS_CONSTEXPR_
2085 #endif
2086 #if \
2087 JSON_HEDLEY_HAS_BUILTIN(__builtin_constant_p) || \
2088 JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
2089 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
2090 JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,19) || \
2091 JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
2092 JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
2093 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
2094 (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) && !defined(__cplusplus)) || \
2095 JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
2096 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
2097 #define JSON_HEDLEY_IS_CONSTANT(expr) __builtin_constant_p(expr)
2098 #endif
2099 #if !defined(__cplusplus)
2100 # if \
2101 JSON_HEDLEY_HAS_BUILTIN(__builtin_types_compatible_p) || \
2102 JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
2103 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
2104 JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
2105 JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
2106 JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) || \
2107 JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,24)
2108 #if defined(__INTPTR_TYPE__)
2109 #define JSON_HEDLEY_IS_CONSTEXPR_(expr) __builtin_types_compatible_p(__typeof__((1 ? (void*) ((__INTPTR_TYPE__) ((expr) * 0)) : (int*) 0)), int*)
2110 #else
2111 #include <stdint.h>
2112 #define JSON_HEDLEY_IS_CONSTEXPR_(expr) __builtin_types_compatible_p(__typeof__((1 ? (void*) ((intptr_t) ((expr) * 0)) : (int*) 0)), int*)
2113 #endif
2114 # elif \
2115 ( \
2116 defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) && \
2117 !defined(JSON_HEDLEY_SUNPRO_VERSION) && \
2118 !defined(JSON_HEDLEY_PGI_VERSION) && \
2119 !defined(JSON_HEDLEY_IAR_VERSION)) || \
2120 (JSON_HEDLEY_HAS_EXTENSION(c_generic_selections) && !defined(JSON_HEDLEY_IAR_VERSION)) || \
2121 JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \
2122 JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0) || \
2123 JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \
2124 JSON_HEDLEY_ARM_VERSION_CHECK(5,3,0)
2125 #if defined(__INTPTR_TYPE__)
2126 #define JSON_HEDLEY_IS_CONSTEXPR_(expr) _Generic((1 ? (void*) ((__INTPTR_TYPE__) ((expr) * 0)) : (int*) 0), int*: 1, void*: 0)
2127 #else
2128 #include <stdint.h>
2129 #define JSON_HEDLEY_IS_CONSTEXPR_(expr) _Generic((1 ? (void*) ((intptr_t) * 0) : (int*) 0), int*: 1, void*: 0)
2130 #endif
2131 # elif \
2132 defined(JSON_HEDLEY_GCC_VERSION) || \
2133 defined(JSON_HEDLEY_INTEL_VERSION) || \
2134 defined(JSON_HEDLEY_TINYC_VERSION) || \
2135 defined(JSON_HEDLEY_TI_ARMCL_VERSION) || \
2136 JSON_HEDLEY_TI_CL430_VERSION_CHECK(18,12,0) || \
2137 defined(JSON_HEDLEY_TI_CL2000_VERSION) || \
2138 defined(JSON_HEDLEY_TI_CL6X_VERSION) || \
2139 defined(JSON_HEDLEY_TI_CL7X_VERSION) || \
2140 defined(JSON_HEDLEY_TI_CLPRU_VERSION) || \
2141 defined(__clang__)
2142 # define JSON_HEDLEY_IS_CONSTEXPR_(expr) ( \
2143 sizeof(void) != \
2144 sizeof(*( \
2145 1 ? \
2146 ((void*) ((expr) * 0L) ) : \
2147 ((struct { char v[sizeof(void) * 2]; } *) 1) \
2148 ) \
2149 ) \
2150 )
2151 # endif
2152 #endif
2153 #if defined(JSON_HEDLEY_IS_CONSTEXPR_)
2154 #if !defined(JSON_HEDLEY_IS_CONSTANT)
2155 #define JSON_HEDLEY_IS_CONSTANT(expr) JSON_HEDLEY_IS_CONSTEXPR_(expr)
2156 #endif
2157 #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (JSON_HEDLEY_IS_CONSTEXPR_(expr) ? (expr) : (-1))
2158 #else
2159 #if !defined(JSON_HEDLEY_IS_CONSTANT)
2160 #define JSON_HEDLEY_IS_CONSTANT(expr) (0)
2161 #endif
2162 #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (expr)
2163 #endif
2164
2165 #if defined(JSON_HEDLEY_BEGIN_C_DECLS)
2166 #undef JSON_HEDLEY_BEGIN_C_DECLS
2167 #endif
2168 #if defined(JSON_HEDLEY_END_C_DECLS)
2169 #undef JSON_HEDLEY_END_C_DECLS
2170 #endif
2171 #if defined(JSON_HEDLEY_C_DECL)
2172 #undef JSON_HEDLEY_C_DECL
2173 #endif
2174 #if defined(__cplusplus)
2175 #define JSON_HEDLEY_BEGIN_C_DECLS extern "C" {
2176 #define JSON_HEDLEY_END_C_DECLS }
2177 #define JSON_HEDLEY_C_DECL extern "C"
2178 #else
2179 #define JSON_HEDLEY_BEGIN_C_DECLS
2180 #define JSON_HEDLEY_END_C_DECLS
2181 #define JSON_HEDLEY_C_DECL
2182 #endif
2183
2184 #if defined(JSON_HEDLEY_STATIC_ASSERT)
2185 #undef JSON_HEDLEY_STATIC_ASSERT
2186 #endif
2187 #if \
2188 !defined(__cplusplus) && ( \
2189 (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)) || \
2190 (JSON_HEDLEY_HAS_FEATURE(c_static_assert) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \
2191 JSON_HEDLEY_GCC_VERSION_CHECK(6,0,0) || \
2192 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
2193 defined(_Static_assert) \
2194 )
2195 # define JSON_HEDLEY_STATIC_ASSERT(expr, message) _Static_assert(expr, message)
2196 #elif \
2197 (defined(__cplusplus) && (__cplusplus >= 201103L)) || \
2198 JSON_HEDLEY_MSVC_VERSION_CHECK(16,0,0) || \
2199 JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
2200 # define JSON_HEDLEY_STATIC_ASSERT(expr, message) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(static_assert(expr, message))
2201 #else
2202 # define JSON_HEDLEY_STATIC_ASSERT(expr, message)
2203 #endif
2204
2205 #if defined(JSON_HEDLEY_NULL)
2206 #undef JSON_HEDLEY_NULL
2207 #endif
2208 #if defined(__cplusplus)
2209 #if __cplusplus >= 201103L
2210 #define JSON_HEDLEY_NULL JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(nullptr)
2211 #elif defined(NULL)
2212 #define JSON_HEDLEY_NULL NULL
2213 #else
2214 #define JSON_HEDLEY_NULL JSON_HEDLEY_STATIC_CAST(void*, 0)
2215 #endif
2216 #elif defined(NULL)
2217 #define JSON_HEDLEY_NULL NULL
2218 #else
2219 #define JSON_HEDLEY_NULL ((void*) 0)
2220 #endif
2221
2222 #if defined(JSON_HEDLEY_MESSAGE)
2223 #undef JSON_HEDLEY_MESSAGE
2224 #endif
2225 #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
2226 # define JSON_HEDLEY_MESSAGE(msg) \
2227 JSON_HEDLEY_DIAGNOSTIC_PUSH \
2228 JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS \
2229 JSON_HEDLEY_PRAGMA(message msg) \
2230 JSON_HEDLEY_DIAGNOSTIC_POP
2231 #elif \
2232 JSON_HEDLEY_GCC_VERSION_CHECK(4,4,0) || \
2233 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
2234 # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message msg)
2235 #elif JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0)
2236 # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(_CRI message msg)
2237 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
2238 # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg))
2239 #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,0,0)
2240 # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg))
2241 #else
2242 # define JSON_HEDLEY_MESSAGE(msg)
2243 #endif
2244
2245 #if defined(JSON_HEDLEY_WARNING)
2246 #undef JSON_HEDLEY_WARNING
2247 #endif
2248 #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
2249 # define JSON_HEDLEY_WARNING(msg) \
2250 JSON_HEDLEY_DIAGNOSTIC_PUSH \
2251 JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS \
2252 JSON_HEDLEY_PRAGMA(clang warning msg) \
2253 JSON_HEDLEY_DIAGNOSTIC_POP
2254 #elif \
2255 JSON_HEDLEY_GCC_VERSION_CHECK(4,8,0) || \
2256 JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \
2257 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
2258 # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(GCC warning msg)
2259 #elif \
2260 JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \
2261 JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
2262 # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(message(msg))
2263 #else
2264 # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_MESSAGE(msg)
2265 #endif
2266
2267 #if defined(JSON_HEDLEY_REQUIRE)
2268 #undef JSON_HEDLEY_REQUIRE
2269 #endif
2270 #if defined(JSON_HEDLEY_REQUIRE_MSG)
2271 #undef JSON_HEDLEY_REQUIRE_MSG
2272 #endif
2273 #if JSON_HEDLEY_HAS_ATTRIBUTE(diagnose_if)
2274 # if JSON_HEDLEY_HAS_WARNING("-Wgcc-compat")
2275 # define JSON_HEDLEY_REQUIRE(expr) \
2276 JSON_HEDLEY_DIAGNOSTIC_PUSH \
2277 _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \
2278 __attribute__((diagnose_if(!(expr), #expr, "error"))) \
2279 JSON_HEDLEY_DIAGNOSTIC_POP
2280 # define JSON_HEDLEY_REQUIRE_MSG(expr,msg) \
2281 JSON_HEDLEY_DIAGNOSTIC_PUSH \
2282 _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \
2283 __attribute__((diagnose_if(!(expr), msg, "error"))) \
2284 JSON_HEDLEY_DIAGNOSTIC_POP
2285 # else
2286 # define JSON_HEDLEY_REQUIRE(expr) __attribute__((diagnose_if(!(expr), #expr, "error")))
2287 # define JSON_HEDLEY_REQUIRE_MSG(expr,msg) __attribute__((diagnose_if(!(expr), msg, "error")))
2288 # endif
2289 #else
2290 # define JSON_HEDLEY_REQUIRE(expr)
2291 # define JSON_HEDLEY_REQUIRE_MSG(expr,msg)
2292 #endif
2293
2294 #if defined(JSON_HEDLEY_FLAGS)
2295 #undef JSON_HEDLEY_FLAGS
2296 #endif
2297 #if JSON_HEDLEY_HAS_ATTRIBUTE(flag_enum) && (!defined(__cplusplus) || JSON_HEDLEY_HAS_WARNING("-Wbitfield-enum-conversion"))
2298 #define JSON_HEDLEY_FLAGS __attribute__((__flag_enum__))
2299 #else
2300 #define JSON_HEDLEY_FLAGS
2301 #endif
2302
2303 #if defined(JSON_HEDLEY_FLAGS_CAST)
2304 #undef JSON_HEDLEY_FLAGS_CAST
2305 #endif
2306 #if JSON_HEDLEY_INTEL_VERSION_CHECK(19,0,0)
2307 # define JSON_HEDLEY_FLAGS_CAST(T, expr) (__extension__ ({ \
2308 JSON_HEDLEY_DIAGNOSTIC_PUSH \
2309 _Pragma("warning(disable:188)") \
2310 ((T) (expr)); \
2311 JSON_HEDLEY_DIAGNOSTIC_POP \
2312 }))
2313 #else
2314 # define JSON_HEDLEY_FLAGS_CAST(T, expr) JSON_HEDLEY_STATIC_CAST(T, expr)
2315 #endif
2316
2317 #if defined(JSON_HEDLEY_EMPTY_BASES)
2318 #undef JSON_HEDLEY_EMPTY_BASES
2319 #endif
2320 #if \
2321 (JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,23918) && !JSON_HEDLEY_MSVC_VERSION_CHECK(20,0,0)) || \
2322 JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
2323 #define JSON_HEDLEY_EMPTY_BASES __declspec(empty_bases)
2324 #else
2325 #define JSON_HEDLEY_EMPTY_BASES
2326 #endif
2327
2328 /* Remaining macros are deprecated. */
2329
2330 #if defined(JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK)
2331 #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK
2332 #endif
2333 #if defined(__clang__)
2334 #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) (0)
2335 #else
2336 #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
2337 #endif
2338
2339 #if defined(JSON_HEDLEY_CLANG_HAS_ATTRIBUTE)
2340 #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE
2341 #endif
2342 #define JSON_HEDLEY_CLANG_HAS_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
2343
2344 #if defined(JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE)
2345 #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE
2346 #endif
2347 #define JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute)
2348
2349 #if defined(JSON_HEDLEY_CLANG_HAS_BUILTIN)
2350 #undef JSON_HEDLEY_CLANG_HAS_BUILTIN
2351 #endif
2352 #define JSON_HEDLEY_CLANG_HAS_BUILTIN(builtin) JSON_HEDLEY_HAS_BUILTIN(builtin)
2353
2354 #if defined(JSON_HEDLEY_CLANG_HAS_FEATURE)
2355 #undef JSON_HEDLEY_CLANG_HAS_FEATURE
2356 #endif
2357 #define JSON_HEDLEY_CLANG_HAS_FEATURE(feature) JSON_HEDLEY_HAS_FEATURE(feature)
2358
2359 #if defined(JSON_HEDLEY_CLANG_HAS_EXTENSION)
2360 #undef JSON_HEDLEY_CLANG_HAS_EXTENSION
2361 #endif
2362 #define JSON_HEDLEY_CLANG_HAS_EXTENSION(extension) JSON_HEDLEY_HAS_EXTENSION(extension)
2363
2364 #if defined(JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE)
2365 #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE
2366 #endif
2367 #define JSON_HEDLEY_CLANG_HAS_DECLSPEC_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute)
2368
2369 #if defined(JSON_HEDLEY_CLANG_HAS_WARNING)
2370 #undef JSON_HEDLEY_CLANG_HAS_WARNING
2371 #endif
2372 #define JSON_HEDLEY_CLANG_HAS_WARNING(warning) JSON_HEDLEY_HAS_WARNING(warning)
2373
2374 #endif /* !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < X) */
2375
2376
2377 // This file contains all internal macro definitions (except those affecting ABI)
2378 // You MUST include macro_unscope.hpp at the end of json.hpp to undef all of them
2379
2380 // #include <nlohmann/detail/abi_macros.hpp>
2381
2382
2383 // exclude unsupported compilers
2384 #if !defined(JSON_SKIP_UNSUPPORTED_COMPILER_CHECK)
2385 #if defined(__clang__)
2386 #if (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) < 30400
2387 #error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers"
2388 #endif
2389 #elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER))
2390 #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800
2391 #error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers"
2392 #endif
2393 #endif
2394 #endif
2395
2396 // C++ language standard detection
2397 // if the user manually specified the used c++ version this is skipped
2398 #if !defined(JSON_HAS_CPP_23) && !defined(JSON_HAS_CPP_20) && !defined(JSON_HAS_CPP_17) && !defined(JSON_HAS_CPP_14) && !defined(JSON_HAS_CPP_11)
2399 #if (defined(__cplusplus) && __cplusplus > 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG > 202002L)
2400 #define JSON_HAS_CPP_23
2401 #define JSON_HAS_CPP_20
2402 #define JSON_HAS_CPP_17
2403 #define JSON_HAS_CPP_14
2404 #elif (defined(__cplusplus) && __cplusplus > 201703L) || (defined(_MSVC_LANG) && _MSVC_LANG > 201703L)
2405 #define JSON_HAS_CPP_20
2406 #define JSON_HAS_CPP_17
2407 #define JSON_HAS_CPP_14
2408 #elif (defined(__cplusplus) && __cplusplus > 201402L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464
2409 #define JSON_HAS_CPP_17
2410 #define JSON_HAS_CPP_14
2411 #elif (defined(__cplusplus) && __cplusplus > 201103L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1)
2412 #define JSON_HAS_CPP_14
2413 #endif
2414 // the cpp 11 flag is always specified because it is the minimal required version
2415 #define JSON_HAS_CPP_11
2416 #endif
2417
2418 #ifdef __has_include
2419 #if __has_include(<version>)
2420 #include <version>
2421 #endif
2422 #endif
2423
2424 #if !defined(JSON_HAS_FILESYSTEM) && !defined(JSON_HAS_EXPERIMENTAL_FILESYSTEM)
2425 #ifdef JSON_HAS_CPP_17
2426 #if defined(__cpp_lib_filesystem)
2427 #define JSON_HAS_FILESYSTEM 1
2428 #elif defined(__cpp_lib_experimental_filesystem)
2429 #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
2430 #elif !defined(__has_include)
2431 #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
2432 #elif __has_include(<filesystem>)
2433 #define JSON_HAS_FILESYSTEM 1
2434 #elif __has_include(<experimental/filesystem>)
2435 #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
2436 #endif
2437
2438 // std::filesystem does not work on MinGW GCC 8: https://sourceforge.net/p/mingw-w64/bugs/737/
2439 #if defined(__MINGW32__) && defined(__GNUC__) && __GNUC__ == 8
2440 #undef JSON_HAS_FILESYSTEM
2441 #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
2442 #endif
2443
2444 // no filesystem support before GCC 8: https://en.cppreference.com/w/cpp/compiler_support
2445 #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 8
2446 #undef JSON_HAS_FILESYSTEM
2447 #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
2448 #endif
2449
2450 // no filesystem support before Clang 7: https://en.cppreference.com/w/cpp/compiler_support
2451 #if defined(__clang_major__) && __clang_major__ < 7
2452 #undef JSON_HAS_FILESYSTEM
2453 #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
2454 #endif
2455
2456 // no filesystem support before MSVC 19.14: https://en.cppreference.com/w/cpp/compiler_support
2457 #if defined(_MSC_VER) && _MSC_VER < 1914
2458 #undef JSON_HAS_FILESYSTEM
2459 #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
2460 #endif
2461
2462 // no filesystem support before iOS 13
2463 #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED < 130000
2464 #undef JSON_HAS_FILESYSTEM
2465 #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
2466 #endif
2467
2468 // no filesystem support before macOS Catalina
2469 #if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED < 101500
2470 #undef JSON_HAS_FILESYSTEM
2471 #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
2472 #endif
2473 #endif
2474 #endif
2475
2476 #ifndef JSON_HAS_EXPERIMENTAL_FILESYSTEM
2477 #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 0
2478 #endif
2479
2480 #ifndef JSON_HAS_FILESYSTEM
2481 #define JSON_HAS_FILESYSTEM 0
2482 #endif
2483
2484 #ifndef JSON_HAS_THREE_WAY_COMPARISON
2485 #if defined(__cpp_impl_three_way_comparison) && __cpp_impl_three_way_comparison >= 201907L \
2486 && defined(__cpp_lib_three_way_comparison) && __cpp_lib_three_way_comparison >= 201907L
2487 #define JSON_HAS_THREE_WAY_COMPARISON 1
2488 #else
2489 #define JSON_HAS_THREE_WAY_COMPARISON 0
2490 #endif
2491 #endif
2492
2493 #ifndef JSON_HAS_RANGES
2494 // ranges header shipping in GCC 11.1.0 (released 2021-04-27) has syntax error
2495 #if defined(__GLIBCXX__) && __GLIBCXX__ == 20210427
2496 #define JSON_HAS_RANGES 0
2497 #elif defined(__cpp_lib_ranges)
2498 #define JSON_HAS_RANGES 1
2499 #else
2500 #define JSON_HAS_RANGES 0
2501 #endif
2502 #endif
2503
2504 #ifndef JSON_HAS_STATIC_RTTI
2505 #if !defined(_HAS_STATIC_RTTI) || _HAS_STATIC_RTTI != 0
2506 #define JSON_HAS_STATIC_RTTI 1
2507 #else
2508 #define JSON_HAS_STATIC_RTTI 0
2509 #endif
2510 #endif
2511
2512 #ifdef JSON_HAS_CPP_17
2513 #define JSON_INLINE_VARIABLE inline
2514 #else
2515 #define JSON_INLINE_VARIABLE
2516 #endif
2517
2518 #if JSON_HEDLEY_HAS_ATTRIBUTE(no_unique_address)
2519 #define JSON_NO_UNIQUE_ADDRESS [[no_unique_address]]
2520 #else
2521 #define JSON_NO_UNIQUE_ADDRESS
2522 #endif
2523
2524 // disable documentation warnings on clang
2525 #if defined(__clang__)
2526 #pragma clang diagnostic push
2527 #pragma clang diagnostic ignored "-Wdocumentation"
2528 #pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
2529 #endif
2530
2531 // allow disabling exceptions
2532 #if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION)
2533 #define JSON_THROW(exception) throw exception
2534 #define JSON_TRY try
2535 #define JSON_CATCH(exception) catch(exception)
2536 #define JSON_INTERNAL_CATCH(exception) catch(exception)
2537 #else
2538 #include <cstdlib>
2539 #define JSON_THROW(exception) std::abort()
2540 #define JSON_TRY if(true)
2541 #define JSON_CATCH(exception) if(false)
2542 #define JSON_INTERNAL_CATCH(exception) if(false)
2543 #endif
2544
2545 // override exception macros
2546 #if defined(JSON_THROW_USER)
2547 #undef JSON_THROW
2548 #define JSON_THROW JSON_THROW_USER
2549 #endif
2550 #if defined(JSON_TRY_USER)
2551 #undef JSON_TRY
2552 #define JSON_TRY JSON_TRY_USER
2553 #endif
2554 #if defined(JSON_CATCH_USER)
2555 #undef JSON_CATCH
2556 #define JSON_CATCH JSON_CATCH_USER
2557 #undef JSON_INTERNAL_CATCH
2558 #define JSON_INTERNAL_CATCH JSON_CATCH_USER
2559 #endif
2560 #if defined(JSON_INTERNAL_CATCH_USER)
2561 #undef JSON_INTERNAL_CATCH
2562 #define JSON_INTERNAL_CATCH JSON_INTERNAL_CATCH_USER
2563 #endif
2564
2565 // allow overriding assert
2566 #if !defined(JSON_ASSERT)
2567 #include <cassert> // assert
2568 #define JSON_ASSERT(x) assert(x)
2569 #endif
2570
2571 // allow to access some private functions (needed by the test suite)
2572 #if defined(JSON_TESTS_PRIVATE)
2573 #define JSON_PRIVATE_UNLESS_TESTED public
2574 #else
2575 #define JSON_PRIVATE_UNLESS_TESTED private
2576 #endif
2577
2578 /*!
2579 @brief macro to briefly define a mapping between an enum and JSON
2580 @def NLOHMANN_JSON_SERIALIZE_ENUM
2581 @since version 3.4.0
2582 */
2583 #define NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...) \
2584 template<typename BasicJsonType> \
2585 inline void to_json(BasicJsonType& j, const ENUM_TYPE& e) \
2586 { \
2587 /* NOLINTNEXTLINE(modernize-type-traits) we use C++11 */ \
2588 static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
2589 /* NOLINTNEXTLINE(modernize-avoid-c-arrays) we don't want to depend on <array> */ \
2590 static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
2591 auto it = std::find_if(std::begin(m), std::end(m), \
2592 [e](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
2593 { \
2594 return ej_pair.first == e; \
2595 }); \
2596 j = ((it != std::end(m)) ? it : std::begin(m))->second; \
2597 } \
2598 template<typename BasicJsonType> \
2599 inline void from_json(const BasicJsonType& j, ENUM_TYPE& e) \
2600 { \
2601 /* NOLINTNEXTLINE(modernize-type-traits) we use C++11 */ \
2602 static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
2603 /* NOLINTNEXTLINE(modernize-avoid-c-arrays) we don't want to depend on <array> */ \
2604 static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
2605 auto it = std::find_if(std::begin(m), std::end(m), \
2606 [&j](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
2607 { \
2608 return ej_pair.second == j; \
2609 }); \
2610 e = ((it != std::end(m)) ? it : std::begin(m))->first; \
2611 }
2612
2613 // Ugly macros to avoid uglier copy-paste when specializing basic_json. They
2614 // may be removed in the future once the class is split.
2615
2616 #define NLOHMANN_BASIC_JSON_TPL_DECLARATION \
2617 template<template<typename, typename, typename...> class ObjectType, \
2618 template<typename, typename...> class ArrayType, \
2619 class StringType, class BooleanType, class NumberIntegerType, \
2620 class NumberUnsignedType, class NumberFloatType, \
2621 template<typename> class AllocatorType, \
2622 template<typename, typename = void> class JSONSerializer, \
2623 class BinaryType, \
2624 class CustomBaseClass>
2625
2626 #define NLOHMANN_BASIC_JSON_TPL \
2627 basic_json<ObjectType, ArrayType, StringType, BooleanType, \
2628 NumberIntegerType, NumberUnsignedType, NumberFloatType, \
2629 AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>
2630
2631 // Macros to simplify conversion from/to types
2632
2633 #define NLOHMANN_JSON_EXPAND( x ) x
2634 #define NLOHMANN_JSON_GET_MACRO(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, _62, _63, _64, NAME,...) NAME
2635 #define NLOHMANN_JSON_PASTE(...) NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_GET_MACRO(__VA_ARGS__, \
2636 NLOHMANN_JSON_PASTE64, \
2637 NLOHMANN_JSON_PASTE63, \
2638 NLOHMANN_JSON_PASTE62, \
2639 NLOHMANN_JSON_PASTE61, \
2640 NLOHMANN_JSON_PASTE60, \
2641 NLOHMANN_JSON_PASTE59, \
2642 NLOHMANN_JSON_PASTE58, \
2643 NLOHMANN_JSON_PASTE57, \
2644 NLOHMANN_JSON_PASTE56, \
2645 NLOHMANN_JSON_PASTE55, \
2646 NLOHMANN_JSON_PASTE54, \
2647 NLOHMANN_JSON_PASTE53, \
2648 NLOHMANN_JSON_PASTE52, \
2649 NLOHMANN_JSON_PASTE51, \
2650 NLOHMANN_JSON_PASTE50, \
2651 NLOHMANN_JSON_PASTE49, \
2652 NLOHMANN_JSON_PASTE48, \
2653 NLOHMANN_JSON_PASTE47, \
2654 NLOHMANN_JSON_PASTE46, \
2655 NLOHMANN_JSON_PASTE45, \
2656 NLOHMANN_JSON_PASTE44, \
2657 NLOHMANN_JSON_PASTE43, \
2658 NLOHMANN_JSON_PASTE42, \
2659 NLOHMANN_JSON_PASTE41, \
2660 NLOHMANN_JSON_PASTE40, \
2661 NLOHMANN_JSON_PASTE39, \
2662 NLOHMANN_JSON_PASTE38, \
2663 NLOHMANN_JSON_PASTE37, \
2664 NLOHMANN_JSON_PASTE36, \
2665 NLOHMANN_JSON_PASTE35, \
2666 NLOHMANN_JSON_PASTE34, \
2667 NLOHMANN_JSON_PASTE33, \
2668 NLOHMANN_JSON_PASTE32, \
2669 NLOHMANN_JSON_PASTE31, \
2670 NLOHMANN_JSON_PASTE30, \
2671 NLOHMANN_JSON_PASTE29, \
2672 NLOHMANN_JSON_PASTE28, \
2673 NLOHMANN_JSON_PASTE27, \
2674 NLOHMANN_JSON_PASTE26, \
2675 NLOHMANN_JSON_PASTE25, \
2676 NLOHMANN_JSON_PASTE24, \
2677 NLOHMANN_JSON_PASTE23, \
2678 NLOHMANN_JSON_PASTE22, \
2679 NLOHMANN_JSON_PASTE21, \
2680 NLOHMANN_JSON_PASTE20, \
2681 NLOHMANN_JSON_PASTE19, \
2682 NLOHMANN_JSON_PASTE18, \
2683 NLOHMANN_JSON_PASTE17, \
2684 NLOHMANN_JSON_PASTE16, \
2685 NLOHMANN_JSON_PASTE15, \
2686 NLOHMANN_JSON_PASTE14, \
2687 NLOHMANN_JSON_PASTE13, \
2688 NLOHMANN_JSON_PASTE12, \
2689 NLOHMANN_JSON_PASTE11, \
2690 NLOHMANN_JSON_PASTE10, \
2691 NLOHMANN_JSON_PASTE9, \
2692 NLOHMANN_JSON_PASTE8, \
2693 NLOHMANN_JSON_PASTE7, \
2694 NLOHMANN_JSON_PASTE6, \
2695 NLOHMANN_JSON_PASTE5, \
2696 NLOHMANN_JSON_PASTE4, \
2697 NLOHMANN_JSON_PASTE3, \
2698 NLOHMANN_JSON_PASTE2, \
2699 NLOHMANN_JSON_PASTE1)(__VA_ARGS__))
2700 #define NLOHMANN_JSON_PASTE2(func, v1) func(v1)
2701 #define NLOHMANN_JSON_PASTE3(func, v1, v2) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE2(func, v2)
2702 #define NLOHMANN_JSON_PASTE4(func, v1, v2, v3) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE3(func, v2, v3)
2703 #define NLOHMANN_JSON_PASTE5(func, v1, v2, v3, v4) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE4(func, v2, v3, v4)
2704 #define NLOHMANN_JSON_PASTE6(func, v1, v2, v3, v4, v5) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE5(func, v2, v3, v4, v5)
2705 #define NLOHMANN_JSON_PASTE7(func, v1, v2, v3, v4, v5, v6) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE6(func, v2, v3, v4, v5, v6)
2706 #define NLOHMANN_JSON_PASTE8(func, v1, v2, v3, v4, v5, v6, v7) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE7(func, v2, v3, v4, v5, v6, v7)
2707 #define NLOHMANN_JSON_PASTE9(func, v1, v2, v3, v4, v5, v6, v7, v8) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE8(func, v2, v3, v4, v5, v6, v7, v8)
2708 #define NLOHMANN_JSON_PASTE10(func, v1, v2, v3, v4, v5, v6, v7, v8, v9) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE9(func, v2, v3, v4, v5, v6, v7, v8, v9)
2709 #define NLOHMANN_JSON_PASTE11(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE10(func, v2, v3, v4, v5, v6, v7, v8, v9, v10)
2710 #define NLOHMANN_JSON_PASTE12(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE11(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11)
2711 #define NLOHMANN_JSON_PASTE13(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE12(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12)
2712 #define NLOHMANN_JSON_PASTE14(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE13(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13)
2713 #define NLOHMANN_JSON_PASTE15(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE14(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14)
2714 #define NLOHMANN_JSON_PASTE16(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE15(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15)
2715 #define NLOHMANN_JSON_PASTE17(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE16(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16)
2716 #define NLOHMANN_JSON_PASTE18(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE17(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17)
2717 #define NLOHMANN_JSON_PASTE19(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE18(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18)
2718 #define NLOHMANN_JSON_PASTE20(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE19(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19)
2719 #define NLOHMANN_JSON_PASTE21(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE20(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20)
2720 #define NLOHMANN_JSON_PASTE22(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE21(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21)
2721 #define NLOHMANN_JSON_PASTE23(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE22(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22)
2722 #define NLOHMANN_JSON_PASTE24(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE23(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23)
2723 #define NLOHMANN_JSON_PASTE25(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE24(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24)
2724 #define NLOHMANN_JSON_PASTE26(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE25(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25)
2725 #define NLOHMANN_JSON_PASTE27(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE26(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26)
2726 #define NLOHMANN_JSON_PASTE28(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE27(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27)
2727 #define NLOHMANN_JSON_PASTE29(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE28(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28)
2728 #define NLOHMANN_JSON_PASTE30(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE29(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29)
2729 #define NLOHMANN_JSON_PASTE31(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE30(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30)
2730 #define NLOHMANN_JSON_PASTE32(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE31(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31)
2731 #define NLOHMANN_JSON_PASTE33(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE32(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32)
2732 #define NLOHMANN_JSON_PASTE34(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE33(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33)
2733 #define NLOHMANN_JSON_PASTE35(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE34(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34)
2734 #define NLOHMANN_JSON_PASTE36(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE35(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35)
2735 #define NLOHMANN_JSON_PASTE37(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE36(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36)
2736 #define NLOHMANN_JSON_PASTE38(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE37(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37)
2737 #define NLOHMANN_JSON_PASTE39(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE38(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38)
2738 #define NLOHMANN_JSON_PASTE40(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE39(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39)
2739 #define NLOHMANN_JSON_PASTE41(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE40(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40)
2740 #define NLOHMANN_JSON_PASTE42(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE41(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41)
2741 #define NLOHMANN_JSON_PASTE43(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE42(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42)
2742 #define NLOHMANN_JSON_PASTE44(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE43(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43)
2743 #define NLOHMANN_JSON_PASTE45(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE44(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44)
2744 #define NLOHMANN_JSON_PASTE46(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE45(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45)
2745 #define NLOHMANN_JSON_PASTE47(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE46(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46)
2746 #define NLOHMANN_JSON_PASTE48(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE47(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47)
2747 #define NLOHMANN_JSON_PASTE49(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE48(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48)
2748 #define NLOHMANN_JSON_PASTE50(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE49(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49)
2749 #define NLOHMANN_JSON_PASTE51(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE50(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50)
2750 #define NLOHMANN_JSON_PASTE52(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE51(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51)
2751 #define NLOHMANN_JSON_PASTE53(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE52(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52)
2752 #define NLOHMANN_JSON_PASTE54(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE53(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53)
2753 #define NLOHMANN_JSON_PASTE55(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE54(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54)
2754 #define NLOHMANN_JSON_PASTE56(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE55(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55)
2755 #define NLOHMANN_JSON_PASTE57(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE56(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56)
2756 #define NLOHMANN_JSON_PASTE58(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE57(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57)
2757 #define NLOHMANN_JSON_PASTE59(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE58(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58)
2758 #define NLOHMANN_JSON_PASTE60(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE59(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59)
2759 #define NLOHMANN_JSON_PASTE61(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE60(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60)
2760 #define NLOHMANN_JSON_PASTE62(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE61(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61)
2761 #define NLOHMANN_JSON_PASTE63(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE62(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62)
2762 #define NLOHMANN_JSON_PASTE64(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62, v63) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE63(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62, v63)
2763
2764 #define NLOHMANN_JSON_TO(v1) nlohmann_json_j[#v1] = nlohmann_json_t.v1;
2765 #define NLOHMANN_JSON_FROM(v1) nlohmann_json_j.at(#v1).get_to(nlohmann_json_t.v1);
2766 #define NLOHMANN_JSON_FROM_WITH_DEFAULT(v1) nlohmann_json_t.v1 = !nlohmann_json_j.is_null() ? nlohmann_json_j.value(#v1, nlohmann_json_default_obj.v1) : nlohmann_json_default_obj.v1;
2767
2768 /*!
2769 @brief macro
2770 @def NLOHMANN_DEFINE_TYPE_INTRUSIVE
2771 @since version 3.9.0
2772 @sa https://json.nlohmann.me/api/macros/nlohmann_define_type_intrusive/
2773 */
2774 #define NLOHMANN_DEFINE_TYPE_INTRUSIVE(Type, ...) \
2775 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2776 friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
2777 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2778 friend void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }
2779
2780 /*!
2781 @brief macro
2782 @def NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT
2783 @since version 3.11.0
2784 @sa https://json.nlohmann.me/api/macros/nlohmann_define_type_intrusive/
2785 */
2786 #define NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Type, ...) \
2787 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2788 friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
2789 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2790 friend void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) { const Type nlohmann_json_default_obj{}; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) }
2791
2792 /*!
2793 @brief macro
2794 @def NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE
2795 @since version 3.11.3
2796 @sa https://json.nlohmann.me/api/macros/nlohmann_define_type_intrusive/
2797 */
2798 #define NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE(Type, ...) \
2799 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2800 friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) }
2801
2802 /*!
2803 @brief macro
2804 @def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE
2805 @since version 3.9.0
2806 @sa https://json.nlohmann.me/api/macros/nlohmann_define_type_non_intrusive/
2807 */
2808 #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Type, ...) \
2809 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2810 void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
2811 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2812 void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }
2813
2814 /*!
2815 @brief macro
2816 @def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT
2817 @since version 3.11.0
2818 @sa https://json.nlohmann.me/api/macros/nlohmann_define_type_non_intrusive/
2819 */
2820 #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Type, ...) \
2821 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2822 void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
2823 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2824 void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) { const Type nlohmann_json_default_obj{}; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) }
2825
2826 /*!
2827 @brief macro
2828 @def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE
2829 @since version 3.11.3
2830 @sa https://json.nlohmann.me/api/macros/nlohmann_define_type_non_intrusive/
2831 */
2832 #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE(Type, ...) \
2833 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2834 void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) }
2835
2836 /*!
2837 @brief macro
2838 @def NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE
2839 @since version 3.12.0
2840 @sa https://json.nlohmann.me/api/macros/nlohmann_define_derived_type/
2841 */
2842 #define NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE(Type, BaseType, ...) \
2843 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2844 friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { nlohmann::to_json(nlohmann_json_j, static_cast<const BaseType &>(nlohmann_json_t)); NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
2845 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2846 friend void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) { nlohmann::from_json(nlohmann_json_j, static_cast<BaseType&>(nlohmann_json_t)); NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }
2847
2848 /*!
2849 @brief macro
2850 @def NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT
2851 @since version 3.12.0
2852 @sa https://json.nlohmann.me/api/macros/nlohmann_define_derived_type/
2853 */
2854 #define NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT(Type, BaseType, ...) \
2855 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2856 friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { nlohmann::to_json(nlohmann_json_j, static_cast<const BaseType&>(nlohmann_json_t)); NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
2857 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2858 friend void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) { nlohmann::from_json(nlohmann_json_j, static_cast<BaseType&>(nlohmann_json_t)); const Type nlohmann_json_default_obj{}; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) }
2859
2860 /*!
2861 @brief macro
2862 @def NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE
2863 @since version 3.12.0
2864 @sa https://json.nlohmann.me/api/macros/nlohmann_define_derived_type/
2865 */
2866 #define NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE(Type, BaseType, ...) \
2867 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2868 friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { nlohmann::to_json(nlohmann_json_j, static_cast<const BaseType &>(nlohmann_json_t)); NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) }
2869
2870 /*!
2871 @brief macro
2872 @def NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE
2873 @since version 3.12.0
2874 @sa https://json.nlohmann.me/api/macros/nlohmann_define_derived_type/
2875 */
2876 #define NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE(Type, BaseType, ...) \
2877 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2878 void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { nlohmann::to_json(nlohmann_json_j, static_cast<const BaseType &>(nlohmann_json_t)); NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
2879 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2880 void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) { nlohmann::from_json(nlohmann_json_j, static_cast<BaseType&>(nlohmann_json_t)); NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }
2881
2882 /*!
2883 @brief macro
2884 @def NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT
2885 @since version 3.12.0
2886 @sa https://json.nlohmann.me/api/macros/nlohmann_define_derived_type/
2887 */
2888 #define NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Type, BaseType, ...) \
2889 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2890 void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { nlohmann::to_json(nlohmann_json_j, static_cast<const BaseType &>(nlohmann_json_t)); NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
2891 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2892 void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) { nlohmann::from_json(nlohmann_json_j, static_cast<BaseType&>(nlohmann_json_t)); const Type nlohmann_json_default_obj{}; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) }
2893
2894 /*!
2895 @brief macro
2896 @def NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE
2897 @since version 3.12.0
2898 @sa https://json.nlohmann.me/api/macros/nlohmann_define_derived_type/
2899 */
2900 #define NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE(Type, BaseType, ...) \
2901 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2902 void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { nlohmann::to_json(nlohmann_json_j, static_cast<const BaseType &>(nlohmann_json_t)); NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) }
2903
2904 // inspired from https://stackoverflow.com/a/26745591
2905 // allows calling any std function as if (e.g., with begin):
2906 // using std::begin; begin(x);
2907 //
2908 // it allows using the detected idiom to retrieve the return type
2909 // of such an expression
2910 #define NLOHMANN_CAN_CALL_STD_FUNC_IMPL(std_name) \
2911 namespace detail { \
2912 using std::std_name; \
2913 \
2914 template<typename... T> \
2915 using result_of_##std_name = decltype(std_name(std::declval<T>()...)); \
2916 } \
2917 \
2918 namespace detail2 { \
2919 struct std_name##_tag \
2920 { \
2921 }; \
2922 \
2923 template<typename... T> \
2924 std_name##_tag std_name(T&&...); \
2925 \
2926 template<typename... T> \
2927 using result_of_##std_name = decltype(std_name(std::declval<T>()...)); \
2928 \
2929 template<typename... T> \
2930 struct would_call_std_##std_name \
2931 { \
2932 static constexpr auto const value = ::nlohmann::detail:: \
2933 is_detected_exact<std_name##_tag, result_of_##std_name, T...>::value; \
2934 }; \
2935 } /* namespace detail2 */ \
2936 \
2937 template<typename... T> \
2938 struct would_call_std_##std_name : detail2::would_call_std_##std_name<T...> \
2939 { \
2940 }
2941
2942 #ifndef JSON_USE_IMPLICIT_CONVERSIONS
2943 #define JSON_USE_IMPLICIT_CONVERSIONS 1
2944 #endif
2945
2946 #if JSON_USE_IMPLICIT_CONVERSIONS
2947 #define JSON_EXPLICIT
2948 #else
2949 #define JSON_EXPLICIT explicit
2950 #endif
2951
2952 #ifndef JSON_DISABLE_ENUM_SERIALIZATION
2953 #define JSON_DISABLE_ENUM_SERIALIZATION 0
2954 #endif
2955
2956 #ifndef JSON_USE_GLOBAL_UDLS
2957 #define JSON_USE_GLOBAL_UDLS 1
2958 #endif
2959
2960 #if JSON_HAS_THREE_WAY_COMPARISON
2961 #include <compare> // partial_ordering
2962 #endif
2963
2964 NLOHMANN_JSON_NAMESPACE_BEGIN
2965 namespace detail
2966 {
2967
2968 ///////////////////////////
2969 // JSON type enumeration //
2970 ///////////////////////////
2971
2972 /*!
2973 @brief the JSON type enumeration
2974
2975 This enumeration collects the different JSON types. It is internally used to
2976 distinguish the stored values, and the functions @ref basic_json::is_null(),
2977 @ref basic_json::is_object(), @ref basic_json::is_array(),
2978 @ref basic_json::is_string(), @ref basic_json::is_boolean(),
2979 @ref basic_json::is_number() (with @ref basic_json::is_number_integer(),
2980 @ref basic_json::is_number_unsigned(), and @ref basic_json::is_number_float()),
2981 @ref basic_json::is_discarded(), @ref basic_json::is_primitive(), and
2982 @ref basic_json::is_structured() rely on it.
2983
2984 @note There are three enumeration entries (number_integer, number_unsigned, and
2985 number_float), because the library distinguishes these three types for numbers:
2986 @ref basic_json::number_unsigned_t is used for unsigned integers,
2987 @ref basic_json::number_integer_t is used for signed integers, and
2988 @ref basic_json::number_float_t is used for floating-point numbers or to
2989 approximate integers which do not fit in the limits of their respective type.
2990
2991 @sa see @ref basic_json::basic_json(const value_t value_type) -- create a JSON
2992 value with the default value for a given type
2993
2994 @since version 1.0.0
2995 */
2996 enum class value_t : std::uint8_t
2997 {
2998 null, ///< null value
2999 object, ///< object (unordered set of name/value pairs)
3000 array, ///< array (ordered collection of values)
3001 string, ///< string value
3002 boolean, ///< boolean value
3003 number_integer, ///< number value (signed integer)
3004 number_unsigned, ///< number value (unsigned integer)
3005 number_float, ///< number value (floating-point)
3006 binary, ///< binary array (ordered collection of bytes)
3007 discarded ///< discarded by the parser callback function
3008 };
3009
3010 /*!
3011 @brief comparison operator for JSON types
3012
3013 Returns an ordering that is similar to Python:
3014 - order: null < boolean < number < object < array < string < binary
3015 - furthermore, each type is not smaller than itself
3016 - discarded values are not comparable
3017 - binary is represented as a b"" string in python and directly comparable to a
3018 string; however, making a binary array directly comparable with a string would
3019 be surprising behavior in a JSON file.
3020
3021 @since version 1.0.0
3022 */
3023 #if JSON_HAS_THREE_WAY_COMPARISON
3024 inline std::partial_ordering operator<=>(const value_t lhs, const value_t rhs) noexcept // *NOPAD*
3025 #else
3026 inline bool operator<(const value_t lhs, const value_t rhs) noexcept
3027 #endif
3028 {
3029 static constexpr std::array<std::uint8_t, 9> order = {{
3030 0 /* null */, 3 /* object */, 4 /* array */, 5 /* string */,
3031 1 /* boolean */, 2 /* integer */, 2 /* unsigned */, 2 /* float */,
3032 6 /* binary */
3033 }
3034 };
3035
3036 const auto l_index = static_cast<std::size_t>(lhs);
3037 const auto r_index = static_cast<std::size_t>(rhs);
3038 #if JSON_HAS_THREE_WAY_COMPARISON
3039 if (l_index < order.size() && r_index < order.size())
3040 {
3041 return order[l_index] <=> order[r_index]; // *NOPAD*
3042 }
3043 return std::partial_ordering::unordered;
3044 #else
3045 return l_index < order.size() && r_index < order.size() && order[l_index] < order[r_index];
3046 #endif
3047 }
3048
3049 // GCC selects the built-in operator< over an operator rewritten from
3050 // a user-defined spaceship operator
3051 // Clang, MSVC, and ICC select the rewritten candidate
3052 // (see GCC bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105200)
3053 #if JSON_HAS_THREE_WAY_COMPARISON && defined(__GNUC__)
3054 inline bool operator<(const value_t lhs, const value_t rhs) noexcept
3055 {
3056 return std::is_lt(lhs <=> rhs); // *NOPAD*
3057 }
3058 #endif
3059
3060 } // namespace detail
3061 NLOHMANN_JSON_NAMESPACE_END
3062
3063 // #include <nlohmann/detail/string_escape.hpp>
3064 // __ _____ _____ _____
3065 // __| | __| | | | JSON for Modern C++
3066 // | | |__ | | | | | | version 3.12.0
3067 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
3068 //
3069 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
3070 // SPDX-License-Identifier: MIT
3071
3072
3073
3074 // #include <nlohmann/detail/abi_macros.hpp>
3075
3076
3077 NLOHMANN_JSON_NAMESPACE_BEGIN
3078 namespace detail
3079 {
3080
3081 /*!
3082 @brief replace all occurrences of a substring by another string
3083
3084 @param[in,out] s the string to manipulate; changed so that all
3085 occurrences of @a f are replaced with @a t
3086 @param[in] f the substring to replace with @a t
3087 @param[in] t the string to replace @a f
3088
3089 @pre The search string @a f must not be empty. **This precondition is
3090 enforced with an assertion.**
3091
3092 @since version 2.0.0
3093 */
3094 template<typename StringType>
3095 inline void replace_substring(StringType& s, const StringType& f,
3096 const StringType& t)
3097 {
3098 JSON_ASSERT(!f.empty());
3099 for (auto pos = s.find(f); // find first occurrence of f
3100 pos != StringType::npos; // make sure f was found
3101 s.replace(pos, f.size(), t), // replace with t, and
3102 pos = s.find(f, pos + t.size())) // find next occurrence of f
3103 {}
3104 }
3105
3106 /*!
3107 * @brief string escaping as described in RFC 6901 (Sect. 4)
3108 * @param[in] s string to escape
3109 * @return escaped string
3110 *
3111 * Note the order of escaping "~" to "~0" and "/" to "~1" is important.
3112 */
3113 template<typename StringType>
3114 inline StringType escape(StringType s)
3115 {
3116 replace_substring(s, StringType{"~"}, StringType{"~0"});
3117 replace_substring(s, StringType{"/"}, StringType{"~1"});
3118 return s;
3119 }
3120
3121 /*!
3122 * @brief string unescaping as described in RFC 6901 (Sect. 4)
3123 * @param[in] s string to unescape
3124 * @return unescaped string
3125 *
3126 * Note the order of escaping "~1" to "/" and "~0" to "~" is important.
3127 */
3128 template<typename StringType>
3129 static void unescape(StringType& s)
3130 {
3131 replace_substring(s, StringType{"~1"}, StringType{"/"});
3132 replace_substring(s, StringType{"~0"}, StringType{"~"});
3133 }
3134
3135 } // namespace detail
3136 NLOHMANN_JSON_NAMESPACE_END
3137
3138 // #include <nlohmann/detail/input/position_t.hpp>
3139 // __ _____ _____ _____
3140 // __| | __| | | | JSON for Modern C++
3141 // | | |__ | | | | | | version 3.12.0
3142 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
3143 //
3144 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
3145 // SPDX-License-Identifier: MIT
3146
3147
3148
3149 #include <cstddef> // size_t
3150
3151 // #include <nlohmann/detail/abi_macros.hpp>
3152
3153
3154 NLOHMANN_JSON_NAMESPACE_BEGIN
3155 namespace detail
3156 {
3157
3158 /// struct to capture the start position of the current token
3159 struct position_t
3160 {
3161 /// the total number of characters read
3162 std::size_t chars_read_total = 0;
3163 /// the number of characters read in the current line
3164 std::size_t chars_read_current_line = 0;
3165 /// the number of lines read
3166 std::size_t lines_read = 0;
3167
3168 /// conversion to size_t to preserve SAX interface
3169 constexpr operator size_t() const
3170 {
3171 return chars_read_total;
3172 }
3173 };
3174
3175 } // namespace detail
3176 NLOHMANN_JSON_NAMESPACE_END
3177
3178 // #include <nlohmann/detail/macro_scope.hpp>
3179
3180 // #include <nlohmann/detail/meta/cpp_future.hpp>
3181 // __ _____ _____ _____
3182 // __| | __| | | | JSON for Modern C++
3183 // | | |__ | | | | | | version 3.12.0
3184 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
3185 //
3186 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
3187 // SPDX-FileCopyrightText: 2018 The Abseil Authors
3188 // SPDX-License-Identifier: MIT
3189
3190
3191
3192 #include <array> // array
3193 #include <cstddef> // size_t
3194 #include <type_traits> // conditional, enable_if, false_type, integral_constant, is_constructible, is_integral, is_same, remove_cv, remove_reference, true_type
3195 #include <utility> // index_sequence, make_index_sequence, index_sequence_for
3196
3197 // #include <nlohmann/detail/macro_scope.hpp>
3198
3199
3200 NLOHMANN_JSON_NAMESPACE_BEGIN
3201 namespace detail
3202 {
3203
3204 template<typename T>
3205 using uncvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
3206
3207 #ifdef JSON_HAS_CPP_14
3208
3209 // the following utilities are natively available in C++14
3210 using std::enable_if_t;
3211 using std::index_sequence;
3212 using std::make_index_sequence;
3213 using std::index_sequence_for;
3214
3215 #else
3216
3217 // alias templates to reduce boilerplate
3218 template<bool B, typename T = void>
3219 using enable_if_t = typename std::enable_if<B, T>::type;
3220
3221 // The following code is taken from https://github.com/abseil/abseil-cpp/blob/10cb35e459f5ecca5b2ff107635da0bfa41011b4/absl/utility/utility.h
3222 // which is part of Google Abseil (https://github.com/abseil/abseil-cpp), licensed under the Apache License 2.0.
3223
3224 //// START OF CODE FROM GOOGLE ABSEIL
3225
3226 // integer_sequence
3227 //
3228 // Class template representing a compile-time integer sequence. An instantiation
3229 // of `integer_sequence<T, Ints...>` has a sequence of integers encoded in its
3230 // type through its template arguments (which is a common need when
3231 // working with C++11 variadic templates). `absl::integer_sequence` is designed
3232 // to be a drop-in replacement for C++14's `std::integer_sequence`.
3233 //
3234 // Example:
3235 //
3236 // template< class T, T... Ints >
3237 // void user_function(integer_sequence<T, Ints...>);
3238 //
3239 // int main()
3240 // {
3241 // // user_function's `T` will be deduced to `int` and `Ints...`
3242 // // will be deduced to `0, 1, 2, 3, 4`.
3243 // user_function(make_integer_sequence<int, 5>());
3244 // }
3245 template <typename T, T... Ints>
3246 struct integer_sequence
3247 {
3248 using value_type = T;
3249 static constexpr std::size_t size() noexcept
3250 {
3251 return sizeof...(Ints);
3252 }
3253 };
3254
3255 // index_sequence
3256 //
3257 // A helper template for an `integer_sequence` of `size_t`,
3258 // `absl::index_sequence` is designed to be a drop-in replacement for C++14's
3259 // `std::index_sequence`.
3260 template <size_t... Ints>
3261 using index_sequence = integer_sequence<size_t, Ints...>;
3262
3263 namespace utility_internal
3264 {
3265
3266 template <typename Seq, size_t SeqSize, size_t Rem>
3267 struct Extend;
3268
3269 // Note that SeqSize == sizeof...(Ints). It's passed explicitly for efficiency.
3270 template <typename T, T... Ints, size_t SeqSize>
3271 struct Extend<integer_sequence<T, Ints...>, SeqSize, 0>
3272 {
3273 using type = integer_sequence < T, Ints..., (Ints + SeqSize)... >;
3274 };
3275
3276 template <typename T, T... Ints, size_t SeqSize>
3277 struct Extend<integer_sequence<T, Ints...>, SeqSize, 1>
3278 {
3279 using type = integer_sequence < T, Ints..., (Ints + SeqSize)..., 2 * SeqSize >;
3280 };
3281
3282 // Recursion helper for 'make_integer_sequence<T, N>'.
3283 // 'Gen<T, N>::type' is an alias for 'integer_sequence<T, 0, 1, ... N-1>'.
3284 template <typename T, size_t N>
3285 struct Gen
3286 {
3287 using type =
3288 typename Extend < typename Gen < T, N / 2 >::type, N / 2, N % 2 >::type;
3289 };
3290
3291 template <typename T>
3292 struct Gen<T, 0>
3293 {
3294 using type = integer_sequence<T>;
3295 };
3296
3297 } // namespace utility_internal
3298
3299 // Compile-time sequences of integers
3300
3301 // make_integer_sequence
3302 //
3303 // This template alias is equivalent to
3304 // `integer_sequence<int, 0, 1, ..., N-1>`, and is designed to be a drop-in
3305 // replacement for C++14's `std::make_integer_sequence`.
3306 template <typename T, T N>
3307 using make_integer_sequence = typename utility_internal::Gen<T, N>::type;
3308
3309 // make_index_sequence
3310 //
3311 // This template alias is equivalent to `index_sequence<0, 1, ..., N-1>`,
3312 // and is designed to be a drop-in replacement for C++14's
3313 // `std::make_index_sequence`.
3314 template <size_t N>
3315 using make_index_sequence = make_integer_sequence<size_t, N>;
3316
3317 // index_sequence_for
3318 //
3319 // Converts a typename pack into an index sequence of the same length, and
3320 // is designed to be a drop-in replacement for C++14's
3321 // `std::index_sequence_for()`
3322 template <typename... Ts>
3323 using index_sequence_for = make_index_sequence<sizeof...(Ts)>;
3324
3325 //// END OF CODE FROM GOOGLE ABSEIL
3326
3327 #endif
3328
3329 // dispatch utility (taken from ranges-v3)
3330 template<unsigned N> struct priority_tag : priority_tag < N - 1 > {};
3331 template<> struct priority_tag<0> {};
3332
3333 // taken from ranges-v3
3334 template<typename T>
3335 struct static_const
3336 {
3337 static JSON_INLINE_VARIABLE constexpr T value{};
3338 };
3339
3340 #ifndef JSON_HAS_CPP_17
3341 template<typename T>
3342 constexpr T static_const<T>::value;
3343 #endif
3344
3345 template<typename T, typename... Args>
3346 constexpr std::array<T, sizeof...(Args)> make_array(Args&& ... args)
3347 {
3348 return std::array<T, sizeof...(Args)> {{static_cast<T>(std::forward<Args>(args))...}};
3349 }
3350
3351 } // namespace detail
3352 NLOHMANN_JSON_NAMESPACE_END
3353
3354 // #include <nlohmann/detail/meta/type_traits.hpp>
3355 // __ _____ _____ _____
3356 // __| | __| | | | JSON for Modern C++
3357 // | | |__ | | | | | | version 3.12.0
3358 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
3359 //
3360 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
3361 // SPDX-License-Identifier: MIT
3362
3363
3364
3365 #include <limits> // numeric_limits
3366 #include <string> // char_traits
3367 #include <tuple> // tuple
3368 #include <type_traits> // false_type, is_constructible, is_integral, is_same, true_type
3369 #include <utility> // declval
3370 #if defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603L
3371 #include <cstddef> // byte
3372 #endif
3373 // #include <nlohmann/detail/iterators/iterator_traits.hpp>
3374 // __ _____ _____ _____
3375 // __| | __| | | | JSON for Modern C++
3376 // | | |__ | | | | | | version 3.12.0
3377 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
3378 //
3379 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
3380 // SPDX-License-Identifier: MIT
3381
3382
3383
3384 #include <iterator> // random_access_iterator_tag
3385
3386 // #include <nlohmann/detail/abi_macros.hpp>
3387
3388 // #include <nlohmann/detail/meta/void_t.hpp>
3389
3390 // #include <nlohmann/detail/meta/cpp_future.hpp>
3391
3392
3393 NLOHMANN_JSON_NAMESPACE_BEGIN
3394 namespace detail
3395 {
3396
3397 template<typename It, typename = void>
3398 struct iterator_types {};
3399
3400 template<typename It>
3401 struct iterator_types <
3402 It,
3403 void_t<typename It::difference_type, typename It::value_type, typename It::pointer,
3404 typename It::reference, typename It::iterator_category >>
3405 {
3406 using difference_type = typename It::difference_type;
3407 using value_type = typename It::value_type;
3408 using pointer = typename It::pointer;
3409 using reference = typename It::reference;
3410 using iterator_category = typename It::iterator_category;
3411 };
3412
3413 // This is required as some compilers implement std::iterator_traits in a way that
3414 // doesn't work with SFINAE. See https://github.com/nlohmann/json/issues/1341.
3415 template<typename T, typename = void>
3416 struct iterator_traits
3417 {
3418 };
3419
3420 template<typename T>
3421 struct iterator_traits < T, enable_if_t < !std::is_pointer<T>::value >>
3422 : iterator_types<T>
3423 {
3424 };
3425
3426 template<typename T>
3427 struct iterator_traits<T*, enable_if_t<std::is_object<T>::value>>
3428 {
3429 using iterator_category = std::random_access_iterator_tag;
3430 using value_type = T;
3431 using difference_type = ptrdiff_t;
3432 using pointer = T*;
3433 using reference = T&;
3434 };
3435
3436 } // namespace detail
3437 NLOHMANN_JSON_NAMESPACE_END
3438
3439 // #include <nlohmann/detail/macro_scope.hpp>
3440
3441 // #include <nlohmann/detail/meta/call_std/begin.hpp>
3442 // __ _____ _____ _____
3443 // __| | __| | | | JSON for Modern C++
3444 // | | |__ | | | | | | version 3.12.0
3445 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
3446 //
3447 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
3448 // SPDX-License-Identifier: MIT
3449
3450
3451
3452 // #include <nlohmann/detail/macro_scope.hpp>
3453
3454
3455 NLOHMANN_JSON_NAMESPACE_BEGIN
3456
3457 NLOHMANN_CAN_CALL_STD_FUNC_IMPL(begin);
3458
3459 NLOHMANN_JSON_NAMESPACE_END
3460
3461 // #include <nlohmann/detail/meta/call_std/end.hpp>
3462 // __ _____ _____ _____
3463 // __| | __| | | | JSON for Modern C++
3464 // | | |__ | | | | | | version 3.12.0
3465 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
3466 //
3467 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
3468 // SPDX-License-Identifier: MIT
3469
3470
3471
3472 // #include <nlohmann/detail/macro_scope.hpp>
3473
3474
3475 NLOHMANN_JSON_NAMESPACE_BEGIN
3476
3477 NLOHMANN_CAN_CALL_STD_FUNC_IMPL(end);
3478
3479 NLOHMANN_JSON_NAMESPACE_END
3480
3481 // #include <nlohmann/detail/meta/cpp_future.hpp>
3482
3483 // #include <nlohmann/detail/meta/detected.hpp>
3484
3485 // #include <nlohmann/json_fwd.hpp>
3486 // __ _____ _____ _____
3487 // __| | __| | | | JSON for Modern C++
3488 // | | |__ | | | | | | version 3.12.0
3489 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
3490 //
3491 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
3492 // SPDX-License-Identifier: MIT
3493
3494 #ifndef INCLUDE_NLOHMANN_JSON_FWD_HPP_
3495 #define INCLUDE_NLOHMANN_JSON_FWD_HPP_
3496
3497 #include <cstdint> // int64_t, uint64_t
3498 #include <map> // map
3499 #include <memory> // allocator
3500 #include <string> // string
3501 #include <vector> // vector
3502
3503 // #include <nlohmann/detail/abi_macros.hpp>
3504
3505
3506 /*!
3507 @brief namespace for Niels Lohmann
3508 @see https://github.com/nlohmann
3509 @since version 1.0.0
3510 */
3511 NLOHMANN_JSON_NAMESPACE_BEGIN
3512
3513 /*!
3514 @brief default JSONSerializer template argument
3515
3516 This serializer ignores the template arguments and uses ADL
3517 ([argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl))
3518 for serialization.
3519 */
3520 template<typename T = void, typename SFINAE = void>
3521 struct adl_serializer;
3522
3523 /// a class to store JSON values
3524 /// @sa https://json.nlohmann.me/api/basic_json/
3525 template<template<typename U, typename V, typename... Args> class ObjectType =
3526 std::map,
3527 template<typename U, typename... Args> class ArrayType = std::vector,
3528 class StringType = std::string, class BooleanType = bool,
3529 class NumberIntegerType = std::int64_t,
3530 class NumberUnsignedType = std::uint64_t,
3531 class NumberFloatType = double,
3532 template<typename U> class AllocatorType = std::allocator,
3533 template<typename T, typename SFINAE = void> class JSONSerializer =
3534 adl_serializer,
3535 class BinaryType = std::vector<std::uint8_t>, // cppcheck-suppress syntaxError
3536 class CustomBaseClass = void>
3537 class basic_json;
3538
3539 /// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document
3540 /// @sa https://json.nlohmann.me/api/json_pointer/
3541 template<typename RefStringType>
3542 class json_pointer;
3543
3544 /*!
3545 @brief default specialization
3546 @sa https://json.nlohmann.me/api/json/
3547 */
3548 using json = basic_json<>;
3549
3550 /// @brief a minimal map-like container that preserves insertion order
3551 /// @sa https://json.nlohmann.me/api/ordered_map/
3552 template<class Key, class T, class IgnoredLess, class Allocator>
3553 struct ordered_map;
3554
3555 /// @brief specialization that maintains the insertion order of object keys
3556 /// @sa https://json.nlohmann.me/api/ordered_json/
3557 using ordered_json = basic_json<nlohmann::ordered_map>;
3558
3559 NLOHMANN_JSON_NAMESPACE_END
3560
3561 #endif // INCLUDE_NLOHMANN_JSON_FWD_HPP_
3562
3563
3564 NLOHMANN_JSON_NAMESPACE_BEGIN
3565 /*!
3566 @brief detail namespace with internal helper functions
3567
3568 This namespace collects functions that should not be exposed,
3569 implementations of some @ref basic_json methods, and meta-programming helpers.
3570
3571 @since version 2.1.0
3572 */
3573 namespace detail
3574 {
3575
3576 /////////////
3577 // helpers //
3578 /////////////
3579
3580 // Note to maintainers:
3581 //
3582 // Every trait in this file expects a non CV-qualified type.
3583 // The only exceptions are in the 'aliases for detected' section
3584 // (i.e. those of the form: decltype(T::member_function(std::declval<T>())))
3585 //
3586 // In this case, T has to be properly CV-qualified to constraint the function arguments
3587 // (e.g. to_json(BasicJsonType&, const T&))
3588
3589 template<typename> struct is_basic_json : std::false_type {};
3590
3591 NLOHMANN_BASIC_JSON_TPL_DECLARATION
3592 struct is_basic_json<NLOHMANN_BASIC_JSON_TPL> : std::true_type {};
3593
3594 // used by exceptions create() member functions
3595 // true_type for pointer to possibly cv-qualified basic_json or std::nullptr_t
3596 // false_type otherwise
3597 template<typename BasicJsonContext>
3598 struct is_basic_json_context :
3599 std::integral_constant < bool,
3600 is_basic_json<typename std::remove_cv<typename std::remove_pointer<BasicJsonContext>::type>::type>::value
3601 || std::is_same<BasicJsonContext, std::nullptr_t>::value >
3602 {};
3603
3604 //////////////////////
3605 // json_ref helpers //
3606 //////////////////////
3607
3608 template<typename>
3609 class json_ref;
3610
3611 template<typename>
3612 struct is_json_ref : std::false_type {};
3613
3614 template<typename T>
3615 struct is_json_ref<json_ref<T>> : std::true_type {};
3616
3617 //////////////////////////
3618 // aliases for detected //
3619 //////////////////////////
3620
3621 template<typename T>
3622 using mapped_type_t = typename T::mapped_type;
3623
3624 template<typename T>
3625 using key_type_t = typename T::key_type;
3626
3627 template<typename T>
3628 using value_type_t = typename T::value_type;
3629
3630 template<typename T>
3631 using difference_type_t = typename T::difference_type;
3632
3633 template<typename T>
3634 using pointer_t = typename T::pointer;
3635
3636 template<typename T>
3637 using reference_t = typename T::reference;
3638
3639 template<typename T>
3640 using iterator_category_t = typename T::iterator_category;
3641
3642 template<typename T, typename... Args>
3643 using to_json_function = decltype(T::to_json(std::declval<Args>()...));
3644
3645 template<typename T, typename... Args>
3646 using from_json_function = decltype(T::from_json(std::declval<Args>()...));
3647
3648 template<typename T, typename U>
3649 using get_template_function = decltype(std::declval<T>().template get<U>());
3650
3651 // trait checking if JSONSerializer<T>::from_json(json const&, udt&) exists
3652 template<typename BasicJsonType, typename T, typename = void>
3653 struct has_from_json : std::false_type {};
3654
3655 // trait checking if j.get<T> is valid
3656 // use this trait instead of std::is_constructible or std::is_convertible,
3657 // both rely on, or make use of implicit conversions, and thus fail when T
3658 // has several constructors/operator= (see https://github.com/nlohmann/json/issues/958)
3659 template <typename BasicJsonType, typename T>
3660 struct is_getable
3661 {
3662 static constexpr bool value = is_detected<get_template_function, const BasicJsonType&, T>::value;
3663 };
3664
3665 template<typename BasicJsonType, typename T>
3666 struct has_from_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
3667 {
3668 using serializer = typename BasicJsonType::template json_serializer<T, void>;
3669
3670 static constexpr bool value =
3671 is_detected_exact<void, from_json_function, serializer,
3672 const BasicJsonType&, T&>::value;
3673 };
3674
3675 // This trait checks if JSONSerializer<T>::from_json(json const&) exists
3676 // this overload is used for non-default-constructible user-defined-types
3677 template<typename BasicJsonType, typename T, typename = void>
3678 struct has_non_default_from_json : std::false_type {};
3679
3680 template<typename BasicJsonType, typename T>
3681 struct has_non_default_from_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
3682 {
3683 using serializer = typename BasicJsonType::template json_serializer<T, void>;
3684
3685 static constexpr bool value =
3686 is_detected_exact<T, from_json_function, serializer,
3687 const BasicJsonType&>::value;
3688 };
3689
3690 // This trait checks if BasicJsonType::json_serializer<T>::to_json exists
3691 // Do not evaluate the trait when T is a basic_json type, to avoid template instantiation infinite recursion.
3692 template<typename BasicJsonType, typename T, typename = void>
3693 struct has_to_json : std::false_type {};
3694
3695 template<typename BasicJsonType, typename T>
3696 struct has_to_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
3697 {
3698 using serializer = typename BasicJsonType::template json_serializer<T, void>;
3699
3700 static constexpr bool value =
3701 is_detected_exact<void, to_json_function, serializer, BasicJsonType&,
3702 T>::value;
3703 };
3704
3705 template<typename T>
3706 using detect_key_compare = typename T::key_compare;
3707
3708 template<typename T>
3709 struct has_key_compare : std::integral_constant<bool, is_detected<detect_key_compare, T>::value> {};
3710
3711 // obtains the actual object key comparator
3712 template<typename BasicJsonType>
3713 struct actual_object_comparator
3714 {
3715 using object_t = typename BasicJsonType::object_t;
3716 using object_comparator_t = typename BasicJsonType::default_object_comparator_t;
3717 using type = typename std::conditional < has_key_compare<object_t>::value,
3718 typename object_t::key_compare, object_comparator_t>::type;
3719 };
3720
3721 template<typename BasicJsonType>
3722 using actual_object_comparator_t = typename actual_object_comparator<BasicJsonType>::type;
3723
3724 /////////////////
3725 // char_traits //
3726 /////////////////
3727
3728 // Primary template of char_traits calls std char_traits
3729 template<typename T>
3730 struct char_traits : std::char_traits<T>
3731 {};
3732
3733 // Explicitly define char traits for unsigned char since it is not standard
3734 template<>
3735 struct char_traits<unsigned char> : std::char_traits<char>
3736 {
3737 using char_type = unsigned char;
3738 using int_type = uint64_t;
3739
3740 // Redefine to_int_type function
3741 static int_type to_int_type(char_type c) noexcept
3742 {
3743 return static_cast<int_type>(c);
3744 }
3745
3746 static char_type to_char_type(int_type i) noexcept
3747 {
3748 return static_cast<char_type>(i);
3749 }
3750
3751 static constexpr int_type eof() noexcept
3752 {
3753 return static_cast<int_type>(std::char_traits<char>::eof());
3754 }
3755 };
3756
3757 // Explicitly define char traits for signed char since it is not standard
3758 template<>
3759 struct char_traits<signed char> : std::char_traits<char>
3760 {
3761 using char_type = signed char;
3762 using int_type = uint64_t;
3763
3764 // Redefine to_int_type function
3765 static int_type to_int_type(char_type c) noexcept
3766 {
3767 return static_cast<int_type>(c);
3768 }
3769
3770 static char_type to_char_type(int_type i) noexcept
3771 {
3772 return static_cast<char_type>(i);
3773 }
3774
3775 static constexpr int_type eof() noexcept
3776 {
3777 return static_cast<int_type>(std::char_traits<char>::eof());
3778 }
3779 };
3780
3781 #if defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603L
3782 template<>
3783 struct char_traits<std::byte> : std::char_traits<char>
3784 {
3785 using char_type = std::byte;
3786 using int_type = uint64_t;
3787
3788 static int_type to_int_type(char_type c) noexcept
3789 {
3790 return static_cast<int_type>(std::to_integer<unsigned char>(c));
3791 }
3792
3793 static char_type to_char_type(int_type i) noexcept
3794 {
3795 return std::byte(static_cast<unsigned char>(i));
3796 }
3797
3798 static constexpr int_type eof() noexcept
3799 {
3800 return static_cast<int_type>(std::char_traits<char>::eof());
3801 }
3802 };
3803 #endif
3804
3805 ///////////////////
3806 // is_ functions //
3807 ///////////////////
3808
3809 // https://en.cppreference.com/w/cpp/types/conjunction
3810 template<class...> struct conjunction : std::true_type { };
3811 template<class B> struct conjunction<B> : B { };
3812 template<class B, class... Bn>
3813 struct conjunction<B, Bn...>
3814 : std::conditional<static_cast<bool>(B::value), conjunction<Bn...>, B>::type {};
3815
3816 // https://en.cppreference.com/w/cpp/types/negation
3817 template<class B> struct negation : std::integral_constant < bool, !B::value > { };
3818
3819 // Reimplementation of is_constructible and is_default_constructible, due to them being broken for
3820 // std::pair and std::tuple until LWG 2367 fix (see https://cplusplus.github.io/LWG/lwg-defects.html#2367).
3821 // This causes compile errors in e.g. clang 3.5 or gcc 4.9.
3822 template <typename T>
3823 struct is_default_constructible : std::is_default_constructible<T> {};
3824
3825 template <typename T1, typename T2>
3826 struct is_default_constructible<std::pair<T1, T2>>
3827 : conjunction<is_default_constructible<T1>, is_default_constructible<T2>> {};
3828
3829 template <typename T1, typename T2>
3830 struct is_default_constructible<const std::pair<T1, T2>>
3831 : conjunction<is_default_constructible<T1>, is_default_constructible<T2>> {};
3832
3833 template <typename... Ts>
3834 struct is_default_constructible<std::tuple<Ts...>>
3835 : conjunction<is_default_constructible<Ts>...> {};
3836
3837 template <typename... Ts>
3838 struct is_default_constructible<const std::tuple<Ts...>>
3839 : conjunction<is_default_constructible<Ts>...> {};
3840
3841 template <typename T, typename... Args>
3842 struct is_constructible : std::is_constructible<T, Args...> {};
3843
3844 template <typename T1, typename T2>
3845 struct is_constructible<std::pair<T1, T2>> : is_default_constructible<std::pair<T1, T2>> {};
3846
3847 template <typename T1, typename T2>
3848 struct is_constructible<const std::pair<T1, T2>> : is_default_constructible<const std::pair<T1, T2>> {};
3849
3850 template <typename... Ts>
3851 struct is_constructible<std::tuple<Ts...>> : is_default_constructible<std::tuple<Ts...>> {};
3852
3853 template <typename... Ts>
3854 struct is_constructible<const std::tuple<Ts...>> : is_default_constructible<const std::tuple<Ts...>> {};
3855
3856 template<typename T, typename = void>
3857 struct is_iterator_traits : std::false_type {};
3858
3859 template<typename T>
3860 struct is_iterator_traits<iterator_traits<T>>
3861 {
3862 private:
3863 using traits = iterator_traits<T>;
3864
3865 public:
3866 static constexpr auto value =
3867 is_detected<value_type_t, traits>::value &&
3868 is_detected<difference_type_t, traits>::value &&
3869 is_detected<pointer_t, traits>::value &&
3870 is_detected<iterator_category_t, traits>::value &&
3871 is_detected<reference_t, traits>::value;
3872 };
3873
3874 template<typename T>
3875 struct is_range
3876 {
3877 private:
3878 using t_ref = typename std::add_lvalue_reference<T>::type;
3879
3880 using iterator = detected_t<result_of_begin, t_ref>;
3881 using sentinel = detected_t<result_of_end, t_ref>;
3882
3883 // to be 100% correct, it should use https://en.cppreference.com/w/cpp/iterator/input_or_output_iterator
3884 // and https://en.cppreference.com/w/cpp/iterator/sentinel_for
3885 // but reimplementing these would be too much work, as a lot of other concepts are used underneath
3886 static constexpr auto is_iterator_begin =
3887 is_iterator_traits<iterator_traits<iterator>>::value;
3888
3889 public:
3890 static constexpr bool value = !std::is_same<iterator, nonesuch>::value && !std::is_same<sentinel, nonesuch>::value && is_iterator_begin;
3891 };
3892
3893 template<typename R>
3894 using iterator_t = enable_if_t<is_range<R>::value, result_of_begin<decltype(std::declval<R&>())>>;
3895
3896 template<typename T>
3897 using range_value_t = value_type_t<iterator_traits<iterator_t<T>>>;
3898
3899 // The following implementation of is_complete_type is taken from
3900 // https://blogs.msdn.microsoft.com/vcblog/2015/12/02/partial-support-for-expression-sfinae-in-vs-2015-update-1/
3901 // and is written by Xiang Fan who agreed to using it in this library.
3902
3903 template<typename T, typename = void>
3904 struct is_complete_type : std::false_type {};
3905
3906 template<typename T>
3907 struct is_complete_type<T, decltype(void(sizeof(T)))> : std::true_type {};
3908
3909 template<typename BasicJsonType, typename CompatibleObjectType,
3910 typename = void>
3911 struct is_compatible_object_type_impl : std::false_type {};
3912
3913 template<typename BasicJsonType, typename CompatibleObjectType>
3914 struct is_compatible_object_type_impl <
3915 BasicJsonType, CompatibleObjectType,
3916 enable_if_t < is_detected<mapped_type_t, CompatibleObjectType>::value&&
3917 is_detected<key_type_t, CompatibleObjectType>::value >>
3918 {
3919 using object_t = typename BasicJsonType::object_t;
3920
3921 // macOS's is_constructible does not play well with nonesuch...
3922 static constexpr bool value =
3923 is_constructible<typename object_t::key_type,
3924 typename CompatibleObjectType::key_type>::value &&
3925 is_constructible<typename object_t::mapped_type,
3926 typename CompatibleObjectType::mapped_type>::value;
3927 };
3928
3929 template<typename BasicJsonType, typename CompatibleObjectType>
3930 struct is_compatible_object_type
3931 : is_compatible_object_type_impl<BasicJsonType, CompatibleObjectType> {};
3932
3933 template<typename BasicJsonType, typename ConstructibleObjectType,
3934 typename = void>
3935 struct is_constructible_object_type_impl : std::false_type {};
3936
3937 template<typename BasicJsonType, typename ConstructibleObjectType>
3938 struct is_constructible_object_type_impl <
3939 BasicJsonType, ConstructibleObjectType,
3940 enable_if_t < is_detected<mapped_type_t, ConstructibleObjectType>::value&&
3941 is_detected<key_type_t, ConstructibleObjectType>::value >>
3942 {
3943 using object_t = typename BasicJsonType::object_t;
3944
3945 static constexpr bool value =
3946 (is_default_constructible<ConstructibleObjectType>::value &&
3947 (std::is_move_assignable<ConstructibleObjectType>::value ||
3948 std::is_copy_assignable<ConstructibleObjectType>::value) &&
3949 (is_constructible<typename ConstructibleObjectType::key_type,
3950 typename object_t::key_type>::value &&
3951 std::is_same <
3952 typename object_t::mapped_type,
3953 typename ConstructibleObjectType::mapped_type >::value)) ||
3954 (has_from_json<BasicJsonType,
3955 typename ConstructibleObjectType::mapped_type>::value ||
3956 has_non_default_from_json <
3957 BasicJsonType,
3958 typename ConstructibleObjectType::mapped_type >::value);
3959 };
3960
3961 template<typename BasicJsonType, typename ConstructibleObjectType>
3962 struct is_constructible_object_type
3963 : is_constructible_object_type_impl<BasicJsonType,
3964 ConstructibleObjectType> {};
3965
3966 template<typename BasicJsonType, typename CompatibleStringType>
3967 struct is_compatible_string_type
3968 {
3969 static constexpr auto value =
3970 is_constructible<typename BasicJsonType::string_t, CompatibleStringType>::value;
3971 };
3972
3973 template<typename BasicJsonType, typename ConstructibleStringType>
3974 struct is_constructible_string_type
3975 {
3976 // launder type through decltype() to fix compilation failure on ICPC
3977 #ifdef __INTEL_COMPILER
3978 using laundered_type = decltype(std::declval<ConstructibleStringType>());
3979 #else
3980 using laundered_type = ConstructibleStringType;
3981 #endif
3982
3983 static constexpr auto value =
3984 conjunction <
3985 is_constructible<laundered_type, typename BasicJsonType::string_t>,
3986 is_detected_exact<typename BasicJsonType::string_t::value_type,
3987 value_type_t, laundered_type >>::value;
3988 };
3989
3990 template<typename BasicJsonType, typename CompatibleArrayType, typename = void>
3991 struct is_compatible_array_type_impl : std::false_type {};
3992
3993 template<typename BasicJsonType, typename CompatibleArrayType>
3994 struct is_compatible_array_type_impl <
3995 BasicJsonType, CompatibleArrayType,
3996 enable_if_t <
3997 is_detected<iterator_t, CompatibleArrayType>::value&&
3998 is_iterator_traits<iterator_traits<detected_t<iterator_t, CompatibleArrayType>>>::value&&
3999 // special case for types like std::filesystem::path whose iterator's value_type are themselves
4000 // c.f. https://github.com/nlohmann/json/pull/3073
4001 !std::is_same<CompatibleArrayType, detected_t<range_value_t, CompatibleArrayType>>::value >>
4002 {
4003 static constexpr bool value =
4004 is_constructible<BasicJsonType,
4005 range_value_t<CompatibleArrayType>>::value;
4006 };
4007
4008 template<typename BasicJsonType, typename CompatibleArrayType>
4009 struct is_compatible_array_type
4010 : is_compatible_array_type_impl<BasicJsonType, CompatibleArrayType> {};
4011
4012 template<typename BasicJsonType, typename ConstructibleArrayType, typename = void>
4013 struct is_constructible_array_type_impl : std::false_type {};
4014
4015 template<typename BasicJsonType, typename ConstructibleArrayType>
4016 struct is_constructible_array_type_impl <
4017 BasicJsonType, ConstructibleArrayType,
4018 enable_if_t<std::is_same<ConstructibleArrayType,
4019 typename BasicJsonType::value_type>::value >>
4020 : std::true_type {};
4021
4022 template<typename BasicJsonType, typename ConstructibleArrayType>
4023 struct is_constructible_array_type_impl <
4024 BasicJsonType, ConstructibleArrayType,
4025 enable_if_t < !std::is_same<ConstructibleArrayType,
4026 typename BasicJsonType::value_type>::value&&
4027 !is_compatible_string_type<BasicJsonType, ConstructibleArrayType>::value&&
4028 is_default_constructible<ConstructibleArrayType>::value&&
4029 (std::is_move_assignable<ConstructibleArrayType>::value ||
4030 std::is_copy_assignable<ConstructibleArrayType>::value)&&
4031 is_detected<iterator_t, ConstructibleArrayType>::value&&
4032 is_iterator_traits<iterator_traits<detected_t<iterator_t, ConstructibleArrayType>>>::value&&
4033 is_detected<range_value_t, ConstructibleArrayType>::value&&
4034 // special case for types like std::filesystem::path whose iterator's value_type are themselves
4035 // c.f. https://github.com/nlohmann/json/pull/3073
4036 !std::is_same<ConstructibleArrayType, detected_t<range_value_t, ConstructibleArrayType>>::value&&
4037 is_complete_type <
4038 detected_t<range_value_t, ConstructibleArrayType >>::value >>
4039 {
4040 using value_type = range_value_t<ConstructibleArrayType>;
4041
4042 static constexpr bool value =
4043 std::is_same<value_type,
4044 typename BasicJsonType::array_t::value_type>::value ||
4045 has_from_json<BasicJsonType,
4046 value_type>::value ||
4047 has_non_default_from_json <
4048 BasicJsonType,
4049 value_type >::value;
4050 };
4051
4052 template<typename BasicJsonType, typename ConstructibleArrayType>
4053 struct is_constructible_array_type
4054 : is_constructible_array_type_impl<BasicJsonType, ConstructibleArrayType> {};
4055
4056 template<typename RealIntegerType, typename CompatibleNumberIntegerType,
4057 typename = void>
4058 struct is_compatible_integer_type_impl : std::false_type {};
4059
4060 template<typename RealIntegerType, typename CompatibleNumberIntegerType>
4061 struct is_compatible_integer_type_impl <
4062 RealIntegerType, CompatibleNumberIntegerType,
4063 enable_if_t < std::is_integral<RealIntegerType>::value&&
4064 std::is_integral<CompatibleNumberIntegerType>::value&&
4065 !std::is_same<bool, CompatibleNumberIntegerType>::value >>
4066 {
4067 // is there an assert somewhere on overflows?
4068 using RealLimits = std::numeric_limits<RealIntegerType>;
4069 using CompatibleLimits = std::numeric_limits<CompatibleNumberIntegerType>;
4070
4071 static constexpr auto value =
4072 is_constructible<RealIntegerType,
4073 CompatibleNumberIntegerType>::value &&
4074 CompatibleLimits::is_integer &&
4075 RealLimits::is_signed == CompatibleLimits::is_signed;
4076 };
4077
4078 template<typename RealIntegerType, typename CompatibleNumberIntegerType>
4079 struct is_compatible_integer_type
4080 : is_compatible_integer_type_impl<RealIntegerType,
4081 CompatibleNumberIntegerType> {};
4082
4083 template<typename BasicJsonType, typename CompatibleType, typename = void>
4084 struct is_compatible_type_impl: std::false_type {};
4085
4086 template<typename BasicJsonType, typename CompatibleType>
4087 struct is_compatible_type_impl <
4088 BasicJsonType, CompatibleType,
4089 enable_if_t<is_complete_type<CompatibleType>::value >>
4090 {
4091 static constexpr bool value =
4092 has_to_json<BasicJsonType, CompatibleType>::value;
4093 };
4094
4095 template<typename BasicJsonType, typename CompatibleType>
4096 struct is_compatible_type
4097 : is_compatible_type_impl<BasicJsonType, CompatibleType> {};
4098
4099 template<typename T1, typename T2>
4100 struct is_constructible_tuple : std::false_type {};
4101
4102 template<typename T1, typename... Args>
4103 struct is_constructible_tuple<T1, std::tuple<Args...>> : conjunction<is_constructible<T1, Args>...> {};
4104
4105 template<typename BasicJsonType, typename T>
4106 struct is_json_iterator_of : std::false_type {};
4107
4108 template<typename BasicJsonType>
4109 struct is_json_iterator_of<BasicJsonType, typename BasicJsonType::iterator> : std::true_type {};
4110
4111 template<typename BasicJsonType>
4112 struct is_json_iterator_of<BasicJsonType, typename BasicJsonType::const_iterator> : std::true_type
4113 {};
4114
4115 // checks if a given type T is a template specialization of Primary
4116 template<template <typename...> class Primary, typename T>
4117 struct is_specialization_of : std::false_type {};
4118
4119 template<template <typename...> class Primary, typename... Args>
4120 struct is_specialization_of<Primary, Primary<Args...>> : std::true_type {};
4121
4122 template<typename T>
4123 using is_json_pointer = is_specialization_of<::nlohmann::json_pointer, uncvref_t<T>>;
4124
4125 // checks if A and B are comparable using Compare functor
4126 template<typename Compare, typename A, typename B, typename = void>
4127 struct is_comparable : std::false_type {};
4128
4129 template<typename Compare, typename A, typename B>
4130 struct is_comparable<Compare, A, B, void_t<
4131 decltype(std::declval<Compare>()(std::declval<A>(), std::declval<B>())),
4132 decltype(std::declval<Compare>()(std::declval<B>(), std::declval<A>()))
4133 >> : std::true_type {};
4134
4135 template<typename T>
4136 using detect_is_transparent = typename T::is_transparent;
4137
4138 // type trait to check if KeyType can be used as object key (without a BasicJsonType)
4139 // see is_usable_as_basic_json_key_type below
4140 template<typename Comparator, typename ObjectKeyType, typename KeyTypeCVRef, bool RequireTransparentComparator = true,
4141 bool ExcludeObjectKeyType = RequireTransparentComparator, typename KeyType = uncvref_t<KeyTypeCVRef>>
4142 using is_usable_as_key_type = typename std::conditional <
4143 is_comparable<Comparator, ObjectKeyType, KeyTypeCVRef>::value
4144 && !(ExcludeObjectKeyType && std::is_same<KeyType,
4145 ObjectKeyType>::value)
4146 && (!RequireTransparentComparator
4147 || is_detected <detect_is_transparent, Comparator>::value)
4148 && !is_json_pointer<KeyType>::value,
4149 std::true_type,
4150 std::false_type >::type;
4151
4152 // type trait to check if KeyType can be used as object key
4153 // true if:
4154 // - KeyType is comparable with BasicJsonType::object_t::key_type
4155 // - if ExcludeObjectKeyType is true, KeyType is not BasicJsonType::object_t::key_type
4156 // - the comparator is transparent or RequireTransparentComparator is false
4157 // - KeyType is not a JSON iterator or json_pointer
4158 template<typename BasicJsonType, typename KeyTypeCVRef, bool RequireTransparentComparator = true,
4159 bool ExcludeObjectKeyType = RequireTransparentComparator, typename KeyType = uncvref_t<KeyTypeCVRef>>
4160 using is_usable_as_basic_json_key_type = typename std::conditional <
4161 is_usable_as_key_type<typename BasicJsonType::object_comparator_t,
4162 typename BasicJsonType::object_t::key_type, KeyTypeCVRef,
4163 RequireTransparentComparator, ExcludeObjectKeyType>::value
4164 && !is_json_iterator_of<BasicJsonType, KeyType>::value,
4165 std::true_type,
4166 std::false_type >::type;
4167
4168 template<typename ObjectType, typename KeyType>
4169 using detect_erase_with_key_type = decltype(std::declval<ObjectType&>().erase(std::declval<KeyType>()));
4170
4171 // type trait to check if object_t has an erase() member functions accepting KeyType
4172 template<typename BasicJsonType, typename KeyType>
4173 using has_erase_with_key_type = typename std::conditional <
4174 is_detected <
4175 detect_erase_with_key_type,
4176 typename BasicJsonType::object_t, KeyType >::value,
4177 std::true_type,
4178 std::false_type >::type;
4179
4180 // a naive helper to check if a type is an ordered_map (exploits the fact that
4181 // ordered_map inherits capacity() from std::vector)
4182 template <typename T>
4183 struct is_ordered_map
4184 {
4185 using one = char;
4186
4187 struct two
4188 {
4189 char x[2]; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
4190 };
4191
4192 template <typename C> static one test( decltype(&C::capacity) ) ;
4193 template <typename C> static two test(...);
4194
4195 enum { value = sizeof(test<T>(nullptr)) == sizeof(char) }; // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
4196 };
4197
4198 // to avoid useless casts (see https://github.com/nlohmann/json/issues/2893#issuecomment-889152324)
4199 template < typename T, typename U, enable_if_t < !std::is_same<T, U>::value, int > = 0 >
4200 T conditional_static_cast(U value)
4201 {
4202 return static_cast<T>(value);
4203 }
4204
4205 template<typename T, typename U, enable_if_t<std::is_same<T, U>::value, int> = 0>
4206 T conditional_static_cast(U value)
4207 {
4208 return value;
4209 }
4210
4211 template<typename... Types>
4212 using all_integral = conjunction<std::is_integral<Types>...>;
4213
4214 template<typename... Types>
4215 using all_signed = conjunction<std::is_signed<Types>...>;
4216
4217 template<typename... Types>
4218 using all_unsigned = conjunction<std::is_unsigned<Types>...>;
4219
4220 // there's a disjunction trait in another PR; replace when merged
4221 template<typename... Types>
4222 using same_sign = std::integral_constant < bool,
4223 all_signed<Types...>::value || all_unsigned<Types...>::value >;
4224
4225 template<typename OfType, typename T>
4226 using never_out_of_range = std::integral_constant < bool,
4227 (std::is_signed<OfType>::value && (sizeof(T) < sizeof(OfType)))
4228 || (same_sign<OfType, T>::value && sizeof(OfType) == sizeof(T)) >;
4229
4230 template<typename OfType, typename T,
4231 bool OfTypeSigned = std::is_signed<OfType>::value,
4232 bool TSigned = std::is_signed<T>::value>
4233 struct value_in_range_of_impl2;
4234
4235 template<typename OfType, typename T>
4236 struct value_in_range_of_impl2<OfType, T, false, false>
4237 {
4238 static constexpr bool test(T val)
4239 {
4240 using CommonType = typename std::common_type<OfType, T>::type;
4241 return static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
4242 }
4243 };
4244
4245 template<typename OfType, typename T>
4246 struct value_in_range_of_impl2<OfType, T, true, false>
4247 {
4248 static constexpr bool test(T val)
4249 {
4250 using CommonType = typename std::common_type<OfType, T>::type;
4251 return static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
4252 }
4253 };
4254
4255 template<typename OfType, typename T>
4256 struct value_in_range_of_impl2<OfType, T, false, true>
4257 {
4258 static constexpr bool test(T val)
4259 {
4260 using CommonType = typename std::common_type<OfType, T>::type;
4261 return val >= 0 && static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
4262 }
4263 };
4264
4265 template<typename OfType, typename T>
4266 struct value_in_range_of_impl2<OfType, T, true, true>
4267 {
4268 static constexpr bool test(T val)
4269 {
4270 using CommonType = typename std::common_type<OfType, T>::type;
4271 return static_cast<CommonType>(val) >= static_cast<CommonType>((std::numeric_limits<OfType>::min)())
4272 && static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
4273 }
4274 };
4275
4276 template<typename OfType, typename T,
4277 bool NeverOutOfRange = never_out_of_range<OfType, T>::value,
4278 typename = detail::enable_if_t<all_integral<OfType, T>::value>>
4279 struct value_in_range_of_impl1;
4280
4281 template<typename OfType, typename T>
4282 struct value_in_range_of_impl1<OfType, T, false>
4283 {
4284 static constexpr bool test(T val)
4285 {
4286 return value_in_range_of_impl2<OfType, T>::test(val);
4287 }
4288 };
4289
4290 template<typename OfType, typename T>
4291 struct value_in_range_of_impl1<OfType, T, true>
4292 {
4293 static constexpr bool test(T /*val*/)
4294 {
4295 return true;
4296 }
4297 };
4298
4299 template<typename OfType, typename T>
4300 constexpr bool value_in_range_of(T val)
4301 {
4302 return value_in_range_of_impl1<OfType, T>::test(val);
4303 }
4304
4305 template<bool Value>
4306 using bool_constant = std::integral_constant<bool, Value>;
4307
4308 ///////////////////////////////////////////////////////////////////////////////
4309 // is_c_string
4310 ///////////////////////////////////////////////////////////////////////////////
4311
4312 namespace impl
4313 {
4314
4315 template<typename T>
4316 constexpr bool is_c_string()
4317 {
4318 using TUnExt = typename std::remove_extent<T>::type;
4319 using TUnCVExt = typename std::remove_cv<TUnExt>::type;
4320 using TUnPtr = typename std::remove_pointer<T>::type;
4321 using TUnCVPtr = typename std::remove_cv<TUnPtr>::type;
4322 return
4323 (std::is_array<T>::value && std::is_same<TUnCVExt, char>::value)
4324 || (std::is_pointer<T>::value && std::is_same<TUnCVPtr, char>::value);
4325 }
4326
4327 } // namespace impl
4328
4329 // checks whether T is a [cv] char */[cv] char[] C string
4330 template<typename T>
4331 struct is_c_string : bool_constant<impl::is_c_string<T>()> {};
4332
4333 template<typename T>
4334 using is_c_string_uncvref = is_c_string<uncvref_t<T>>;
4335
4336 ///////////////////////////////////////////////////////////////////////////////
4337 // is_transparent
4338 ///////////////////////////////////////////////////////////////////////////////
4339
4340 namespace impl
4341 {
4342
4343 template<typename T>
4344 constexpr bool is_transparent()
4345 {
4346 return is_detected<detect_is_transparent, T>::value;
4347 }
4348
4349 } // namespace impl
4350
4351 // checks whether T has a member named is_transparent
4352 template<typename T>
4353 struct is_transparent : bool_constant<impl::is_transparent<T>()> {};
4354
4355 ///////////////////////////////////////////////////////////////////////////////
4356
4357 } // namespace detail
4358 NLOHMANN_JSON_NAMESPACE_END
4359
4360 // #include <nlohmann/detail/string_concat.hpp>
4361 // __ _____ _____ _____
4362 // __| | __| | | | JSON for Modern C++
4363 // | | |__ | | | | | | version 3.12.0
4364 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
4365 //
4366 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
4367 // SPDX-License-Identifier: MIT
4368
4369
4370
4371 #include <cstring> // strlen
4372 #include <string> // string
4373 #include <utility> // forward
4374
4375 // #include <nlohmann/detail/meta/cpp_future.hpp>
4376
4377 // #include <nlohmann/detail/meta/detected.hpp>
4378
4379
4380 NLOHMANN_JSON_NAMESPACE_BEGIN
4381 namespace detail
4382 {
4383
4384 inline std::size_t concat_length()
4385 {
4386 return 0;
4387 }
4388
4389 template<typename... Args>
4390 inline std::size_t concat_length(const char* cstr, const Args& ... rest);
4391
4392 template<typename StringType, typename... Args>
4393 inline std::size_t concat_length(const StringType& str, const Args& ... rest);
4394
4395 template<typename... Args>
4396 inline std::size_t concat_length(const char /*c*/, const Args& ... rest)
4397 {
4398 return 1 + concat_length(rest...);
4399 }
4400
4401 template<typename... Args>
4402 inline std::size_t concat_length(const char* cstr, const Args& ... rest)
4403 {
4404 // cppcheck-suppress ignoredReturnValue
4405 return ::strlen(cstr) + concat_length(rest...);
4406 }
4407
4408 template<typename StringType, typename... Args>
4409 inline std::size_t concat_length(const StringType& str, const Args& ... rest)
4410 {
4411 return str.size() + concat_length(rest...);
4412 }
4413
4414 template<typename OutStringType>
4415 inline void concat_into(OutStringType& /*out*/)
4416 {}
4417
4418 template<typename StringType, typename Arg>
4419 using string_can_append = decltype(std::declval<StringType&>().append(std::declval < Arg && > ()));
4420
4421 template<typename StringType, typename Arg>
4422 using detect_string_can_append = is_detected<string_can_append, StringType, Arg>;
4423
4424 template<typename StringType, typename Arg>
4425 using string_can_append_op = decltype(std::declval<StringType&>() += std::declval < Arg && > ());
4426
4427 template<typename StringType, typename Arg>
4428 using detect_string_can_append_op = is_detected<string_can_append_op, StringType, Arg>;
4429
4430 template<typename StringType, typename Arg>
4431 using string_can_append_iter = decltype(std::declval<StringType&>().append(std::declval<const Arg&>().begin(), std::declval<const Arg&>().end()));
4432
4433 template<typename StringType, typename Arg>
4434 using detect_string_can_append_iter = is_detected<string_can_append_iter, StringType, Arg>;
4435
4436 template<typename StringType, typename Arg>
4437 using string_can_append_data = decltype(std::declval<StringType&>().append(std::declval<const Arg&>().data(), std::declval<const Arg&>().size()));
4438
4439 template<typename StringType, typename Arg>
4440 using detect_string_can_append_data = is_detected<string_can_append_data, StringType, Arg>;
4441
4442 template < typename OutStringType, typename Arg, typename... Args,
4443 enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
4444 && detect_string_can_append_op<OutStringType, Arg>::value, int > = 0 >
4445 inline void concat_into(OutStringType& out, Arg && arg, Args && ... rest);
4446
4447 template < typename OutStringType, typename Arg, typename... Args,
4448 enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
4449 && !detect_string_can_append_op<OutStringType, Arg>::value
4450 && detect_string_can_append_iter<OutStringType, Arg>::value, int > = 0 >
4451 inline void concat_into(OutStringType& out, const Arg& arg, Args && ... rest);
4452
4453 template < typename OutStringType, typename Arg, typename... Args,
4454 enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
4455 && !detect_string_can_append_op<OutStringType, Arg>::value
4456 && !detect_string_can_append_iter<OutStringType, Arg>::value
4457 && detect_string_can_append_data<OutStringType, Arg>::value, int > = 0 >
4458 inline void concat_into(OutStringType& out, const Arg& arg, Args && ... rest);
4459
4460 template<typename OutStringType, typename Arg, typename... Args,
4461 enable_if_t<detect_string_can_append<OutStringType, Arg>::value, int> = 0>
4462 inline void concat_into(OutStringType& out, Arg && arg, Args && ... rest)
4463 {
4464 out.append(std::forward<Arg>(arg));
4465 concat_into(out, std::forward<Args>(rest)...);
4466 }
4467
4468 template < typename OutStringType, typename Arg, typename... Args,
4469 enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
4470 && detect_string_can_append_op<OutStringType, Arg>::value, int > >
4471 inline void concat_into(OutStringType& out, Arg&& arg, Args&& ... rest)
4472 {
4473 out += std::forward<Arg>(arg);
4474 concat_into(out, std::forward<Args>(rest)...);
4475 }
4476
4477 template < typename OutStringType, typename Arg, typename... Args,
4478 enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
4479 && !detect_string_can_append_op<OutStringType, Arg>::value
4480 && detect_string_can_append_iter<OutStringType, Arg>::value, int > >
4481 inline void concat_into(OutStringType& out, const Arg& arg, Args&& ... rest)
4482 {
4483 out.append(arg.begin(), arg.end());
4484 concat_into(out, std::forward<Args>(rest)...);
4485 }
4486
4487 template < typename OutStringType, typename Arg, typename... Args,
4488 enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
4489 && !detect_string_can_append_op<OutStringType, Arg>::value
4490 && !detect_string_can_append_iter<OutStringType, Arg>::value
4491 && detect_string_can_append_data<OutStringType, Arg>::value, int > >
4492 inline void concat_into(OutStringType& out, const Arg& arg, Args&& ... rest)
4493 {
4494 out.append(arg.data(), arg.size());
4495 concat_into(out, std::forward<Args>(rest)...);
4496 }
4497
4498 template<typename OutStringType = std::string, typename... Args>
4499 inline OutStringType concat(Args && ... args)
4500 {
4501 OutStringType str;
4502 str.reserve(concat_length(args...));
4503 concat_into(str, std::forward<Args>(args)...);
4504 return str;
4505 }
4506
4507 } // namespace detail
4508 NLOHMANN_JSON_NAMESPACE_END
4509
4510
4511 // With -Wweak-vtables, Clang will complain about the exception classes as they
4512 // have no out-of-line virtual method definitions and their vtable will be
4513 // emitted in every translation unit. This issue cannot be fixed with a
4514 // header-only library as there is no implementation file to move these
4515 // functions to. As a result, we suppress this warning here to avoid client
4516 // code to stumble over this. See https://github.com/nlohmann/json/issues/4087
4517 // for a discussion.
4518 #if defined(__clang__)
4519 #pragma clang diagnostic push
4520 #pragma clang diagnostic ignored "-Wweak-vtables"
4521 #endif
4522
4523 NLOHMANN_JSON_NAMESPACE_BEGIN
4524 namespace detail
4525 {
4526
4527 ////////////////
4528 // exceptions //
4529 ////////////////
4530
4531 /// @brief general exception of the @ref basic_json class
4532 /// @sa https://json.nlohmann.me/api/basic_json/exception/
4533 class exception : public std::exception
4534 {
4535 public:
4536 /// returns the explanatory string
4537 const char* what() const noexcept override
4538 {
4539 return m.what();
4540 }
4541
4542 /// the id of the exception
4543 const int id; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes)
4544
4545 protected:
4546 JSON_HEDLEY_NON_NULL(3)
4547 exception(int id_, const char* what_arg) : id(id_), m(what_arg) {} // NOLINT(bugprone-throw-keyword-missing)
4548
4549 static std::string name(const std::string& ename, int id_)
4550 {
4551 return concat("[json.exception.", ename, '.', std::to_string(id_), "] ");
4552 }
4553
4554 static std::string diagnostics(std::nullptr_t /*leaf_element*/)
4555 {
4556 return "";
4557 }
4558
4559 template<typename BasicJsonType>
4560 static std::string diagnostics(const BasicJsonType* leaf_element)
4561 {
4562 #if JSON_DIAGNOSTICS
4563 std::vector<std::string> tokens;
4564 for (const auto* current = leaf_element; current != nullptr && current->m_parent != nullptr; current = current->m_parent)
4565 {
4566 switch (current->m_parent->type())
4567 {
4568 case value_t::array:
4569 {
4570 for (std::size_t i = 0; i < current->m_parent->m_data.m_value.array->size(); ++i)
4571 {
4572 if (&current->m_parent->m_data.m_value.array->operator[](i) == current)
4573 {
4574 tokens.emplace_back(std::to_string(i));
4575 break;
4576 }
4577 }
4578 break;
4579 }
4580
4581 case value_t::object:
4582 {
4583 for (const auto& element : *current->m_parent->m_data.m_value.object)
4584 {
4585 if (&element.second == current)
4586 {
4587 tokens.emplace_back(element.first.c_str());
4588 break;
4589 }
4590 }
4591 break;
4592 }
4593
4594 case value_t::null: // LCOV_EXCL_LINE
4595 case value_t::string: // LCOV_EXCL_LINE
4596 case value_t::boolean: // LCOV_EXCL_LINE
4597 case value_t::number_integer: // LCOV_EXCL_LINE
4598 case value_t::number_unsigned: // LCOV_EXCL_LINE
4599 case value_t::number_float: // LCOV_EXCL_LINE
4600 case value_t::binary: // LCOV_EXCL_LINE
4601 case value_t::discarded: // LCOV_EXCL_LINE
4602 default: // LCOV_EXCL_LINE
4603 break; // LCOV_EXCL_LINE
4604 }
4605 }
4606
4607 if (tokens.empty())
4608 {
4609 return "";
4610 }
4611
4612 auto str = std::accumulate(tokens.rbegin(), tokens.rend(), std::string{},
4613 [](const std::string & a, const std::string & b)
4614 {
4615 return concat(a, '/', detail::escape(b));
4616 });
4617
4618 return concat('(', str, ") ", get_byte_positions(leaf_element));
4619 #else
4620 return get_byte_positions(leaf_element);
4621 #endif
4622 }
4623
4624 private:
4625 /// an exception object as storage for error messages
4626 std::runtime_error m;
4627 #if JSON_DIAGNOSTIC_POSITIONS
4628 template<typename BasicJsonType>
4629 static std::string get_byte_positions(const BasicJsonType* leaf_element)
4630 {
4631 if ((leaf_element->start_pos() != std::string::npos) && (leaf_element->end_pos() != std::string::npos))
4632 {
4633 return concat("(bytes ", std::to_string(leaf_element->start_pos()), "-", std::to_string(leaf_element->end_pos()), ") ");
4634 }
4635 return "";
4636 }
4637 #else
4638 template<typename BasicJsonType>
4639 static std::string get_byte_positions(const BasicJsonType* leaf_element)
4640 {
4641 static_cast<void>(leaf_element);
4642 return "";
4643 }
4644 #endif
4645 };
4646
4647 /// @brief exception indicating a parse error
4648 /// @sa https://json.nlohmann.me/api/basic_json/parse_error/
4649 class parse_error : public exception
4650 {
4651 public:
4652 /*!
4653 @brief create a parse error exception
4654 @param[in] id_ the id of the exception
4655 @param[in] pos the position where the error occurred (or with
4656 chars_read_total=0 if the position cannot be
4657 determined)
4658 @param[in] what_arg the explanatory string
4659 @return parse_error object
4660 */
4661 template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
4662 static parse_error create(int id_, const position_t& pos, const std::string& what_arg, BasicJsonContext context)
4663 {
4664 const std::string w = concat(exception::name("parse_error", id_), "parse error",
4665 position_string(pos), ": ", exception::diagnostics(context), what_arg);
4666 return {id_, pos.chars_read_total, w.c_str()};
4667 }
4668
4669 template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
4670 static parse_error create(int id_, std::size_t byte_, const std::string& what_arg, BasicJsonContext context)
4671 {
4672 const std::string w = concat(exception::name("parse_error", id_), "parse error",
4673 (byte_ != 0 ? (concat(" at byte ", std::to_string(byte_))) : ""),
4674 ": ", exception::diagnostics(context), what_arg);
4675 return {id_, byte_, w.c_str()};
4676 }
4677
4678 /*!
4679 @brief byte index of the parse error
4680
4681 The byte index of the last read character in the input file.
4682
4683 @note For an input with n bytes, 1 is the index of the first character and
4684 n+1 is the index of the terminating null byte or the end of file.
4685 This also holds true when reading a byte vector (CBOR or MessagePack).
4686 */
4687 const std::size_t byte;
4688
4689 private:
4690 parse_error(int id_, std::size_t byte_, const char* what_arg)
4691 : exception(id_, what_arg), byte(byte_) {}
4692
4693 static std::string position_string(const position_t& pos)
4694 {
4695 return concat(" at line ", std::to_string(pos.lines_read + 1),
4696 ", column ", std::to_string(pos.chars_read_current_line));
4697 }
4698 };
4699
4700 /// @brief exception indicating errors with iterators
4701 /// @sa https://json.nlohmann.me/api/basic_json/invalid_iterator/
4702 class invalid_iterator : public exception
4703 {
4704 public:
4705 template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
4706 static invalid_iterator create(int id_, const std::string& what_arg, BasicJsonContext context)
4707 {
4708 const std::string w = concat(exception::name("invalid_iterator", id_), exception::diagnostics(context), what_arg);
4709 return {id_, w.c_str()};
4710 }
4711
4712 private:
4713 JSON_HEDLEY_NON_NULL(3)
4714 invalid_iterator(int id_, const char* what_arg)
4715 : exception(id_, what_arg) {}
4716 };
4717
4718 /// @brief exception indicating executing a member function with a wrong type
4719 /// @sa https://json.nlohmann.me/api/basic_json/type_error/
4720 class type_error : public exception
4721 {
4722 public:
4723 template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
4724 static type_error create(int id_, const std::string& what_arg, BasicJsonContext context)
4725 {
4726 const std::string w = concat(exception::name("type_error", id_), exception::diagnostics(context), what_arg);
4727 return {id_, w.c_str()};
4728 }
4729
4730 private:
4731 JSON_HEDLEY_NON_NULL(3)
4732 type_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
4733 };
4734
4735 /// @brief exception indicating access out of the defined range
4736 /// @sa https://json.nlohmann.me/api/basic_json/out_of_range/
4737 class out_of_range : public exception
4738 {
4739 public:
4740 template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
4741 static out_of_range create(int id_, const std::string& what_arg, BasicJsonContext context)
4742 {
4743 const std::string w = concat(exception::name("out_of_range", id_), exception::diagnostics(context), what_arg);
4744 return {id_, w.c_str()};
4745 }
4746
4747 private:
4748 JSON_HEDLEY_NON_NULL(3)
4749 out_of_range(int id_, const char* what_arg) : exception(id_, what_arg) {}
4750 };
4751
4752 /// @brief exception indicating other library errors
4753 /// @sa https://json.nlohmann.me/api/basic_json/other_error/
4754 class other_error : public exception
4755 {
4756 public:
4757 template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
4758 static other_error create(int id_, const std::string& what_arg, BasicJsonContext context)
4759 {
4760 const std::string w = concat(exception::name("other_error", id_), exception::diagnostics(context), what_arg);
4761 return {id_, w.c_str()};
4762 }
4763
4764 private:
4765 JSON_HEDLEY_NON_NULL(3)
4766 other_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
4767 };
4768
4769 } // namespace detail
4770 NLOHMANN_JSON_NAMESPACE_END
4771
4772 #if defined(__clang__)
4773 #pragma clang diagnostic pop
4774 #endif
4775
4776 // #include <nlohmann/detail/macro_scope.hpp>
4777
4778 // #include <nlohmann/detail/meta/cpp_future.hpp>
4779
4780 // #include <nlohmann/detail/meta/identity_tag.hpp>
4781 // __ _____ _____ _____
4782 // __| | __| | | | JSON for Modern C++
4783 // | | |__ | | | | | | version 3.12.0
4784 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
4785 //
4786 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
4787 // SPDX-License-Identifier: MIT
4788
4789
4790
4791 // #include <nlohmann/detail/abi_macros.hpp>
4792
4793
4794 NLOHMANN_JSON_NAMESPACE_BEGIN
4795 namespace detail
4796 {
4797
4798 // dispatching helper struct
4799 template <class T> struct identity_tag {};
4800
4801 } // namespace detail
4802 NLOHMANN_JSON_NAMESPACE_END
4803
4804 // #include <nlohmann/detail/meta/std_fs.hpp>
4805 // __ _____ _____ _____
4806 // __| | __| | | | JSON for Modern C++
4807 // | | |__ | | | | | | version 3.12.0
4808 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
4809 //
4810 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
4811 // SPDX-License-Identifier: MIT
4812
4813
4814
4815 // #include <nlohmann/detail/macro_scope.hpp>
4816
4817
4818 #if JSON_HAS_EXPERIMENTAL_FILESYSTEM
4819 #include <experimental/filesystem>
4820 NLOHMANN_JSON_NAMESPACE_BEGIN
4821 namespace detail
4822 {
4823 namespace std_fs = std::experimental::filesystem;
4824 } // namespace detail
4825 NLOHMANN_JSON_NAMESPACE_END
4826 #elif JSON_HAS_FILESYSTEM
4827 #include <filesystem> // NOLINT(build/c++17)
4828 NLOHMANN_JSON_NAMESPACE_BEGIN
4829 namespace detail
4830 {
4831 namespace std_fs = std::filesystem;
4832 } // namespace detail
4833 NLOHMANN_JSON_NAMESPACE_END
4834 #endif
4835
4836 // #include <nlohmann/detail/meta/type_traits.hpp>
4837
4838 // #include <nlohmann/detail/string_concat.hpp>
4839
4840 // #include <nlohmann/detail/value_t.hpp>
4841
4842
4843 // include after macro_scope.hpp
4844 #ifdef JSON_HAS_CPP_17
4845 #include <optional> // optional
4846 #endif
4847
4848 #if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
4849 #include <string_view> // u8string_view
4850 #endif
4851
4852 NLOHMANN_JSON_NAMESPACE_BEGIN
4853 namespace detail
4854 {
4855
4856 template<typename BasicJsonType>
4857 inline void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
4858 {
4859 if (JSON_HEDLEY_UNLIKELY(!j.is_null()))
4860 {
4861 JSON_THROW(type_error::create(302, concat("type must be null, but is ", j.type_name()), &j));
4862 }
4863 n = nullptr;
4864 }
4865
4866 #ifdef JSON_HAS_CPP_17
4867 template<typename BasicJsonType, typename T>
4868 void from_json(const BasicJsonType& j, std::optional<T>& opt)
4869 {
4870 if (j.is_null())
4871 {
4872 opt = std::nullopt;
4873 }
4874 else
4875 {
4876 opt.emplace(j.template get<T>());
4877 }
4878 }
4879 #endif // JSON_HAS_CPP_17
4880
4881 // overloads for basic_json template parameters
4882 template < typename BasicJsonType, typename ArithmeticType,
4883 enable_if_t < std::is_arithmetic<ArithmeticType>::value&&
4884 !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
4885 int > = 0 >
4886 void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
4887 {
4888 switch (static_cast<value_t>(j))
4889 {
4890 case value_t::number_unsigned:
4891 {
4892 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
4893 break;
4894 }
4895 case value_t::number_integer:
4896 {
4897 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
4898 break;
4899 }
4900 case value_t::number_float:
4901 {
4902 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
4903 break;
4904 }
4905
4906 case value_t::null:
4907 case value_t::object:
4908 case value_t::array:
4909 case value_t::string:
4910 case value_t::boolean:
4911 case value_t::binary:
4912 case value_t::discarded:
4913 default:
4914 JSON_THROW(type_error::create(302, concat("type must be number, but is ", j.type_name()), &j));
4915 }
4916 }
4917
4918 template<typename BasicJsonType>
4919 inline void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
4920 {
4921 if (JSON_HEDLEY_UNLIKELY(!j.is_boolean()))
4922 {
4923 JSON_THROW(type_error::create(302, concat("type must be boolean, but is ", j.type_name()), &j));
4924 }
4925 b = *j.template get_ptr<const typename BasicJsonType::boolean_t*>();
4926 }
4927
4928 template<typename BasicJsonType>
4929 inline void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
4930 {
4931 if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
4932 {
4933 JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
4934 }
4935 s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
4936 }
4937
4938 template <
4939 typename BasicJsonType, typename StringType,
4940 enable_if_t <
4941 std::is_assignable<StringType&, const typename BasicJsonType::string_t>::value
4942 && is_detected_exact<typename BasicJsonType::string_t::value_type, value_type_t, StringType>::value
4943 && !std::is_same<typename BasicJsonType::string_t, StringType>::value
4944 && !is_json_ref<StringType>::value, int > = 0 >
4945 inline void from_json(const BasicJsonType& j, StringType& s)
4946 {
4947 if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
4948 {
4949 JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
4950 }
4951
4952 s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
4953 }
4954
4955 template<typename BasicJsonType>
4956 inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val)
4957 {
4958 get_arithmetic_value(j, val);
4959 }
4960
4961 template<typename BasicJsonType>
4962 inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val)
4963 {
4964 get_arithmetic_value(j, val);
4965 }
4966
4967 template<typename BasicJsonType>
4968 inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val)
4969 {
4970 get_arithmetic_value(j, val);
4971 }
4972
4973 #if !JSON_DISABLE_ENUM_SERIALIZATION
4974 template<typename BasicJsonType, typename EnumType,
4975 enable_if_t<std::is_enum<EnumType>::value, int> = 0>
4976 inline void from_json(const BasicJsonType& j, EnumType& e)
4977 {
4978 typename std::underlying_type<EnumType>::type val;
4979 get_arithmetic_value(j, val);
4980 e = static_cast<EnumType>(val);
4981 }
4982 #endif // JSON_DISABLE_ENUM_SERIALIZATION
4983
4984 // forward_list doesn't have an insert method
4985 template<typename BasicJsonType, typename T, typename Allocator,
4986 enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
4987 inline void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
4988 {
4989 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
4990 {
4991 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
4992 }
4993 l.clear();
4994 std::transform(j.rbegin(), j.rend(),
4995 std::front_inserter(l), [](const BasicJsonType & i)
4996 {
4997 return i.template get<T>();
4998 });
4999 }
5000
5001 // valarray doesn't have an insert method
5002 template<typename BasicJsonType, typename T,
5003 enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
5004 inline void from_json(const BasicJsonType& j, std::valarray<T>& l)
5005 {
5006 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
5007 {
5008 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
5009 }
5010 l.resize(j.size());
5011 std::transform(j.begin(), j.end(), std::begin(l),
5012 [](const BasicJsonType & elem)
5013 {
5014 return elem.template get<T>();
5015 });
5016 }
5017
5018 template<typename BasicJsonType, typename T, std::size_t N>
5019 auto from_json(const BasicJsonType& j, T (&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
5020 -> decltype(j.template get<T>(), void())
5021 {
5022 for (std::size_t i = 0; i < N; ++i)
5023 {
5024 arr[i] = j.at(i).template get<T>();
5025 }
5026 }
5027
5028 template<typename BasicJsonType, typename T, std::size_t N1, std::size_t N2>
5029 auto from_json(const BasicJsonType& j, T (&arr)[N1][N2]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
5030 -> decltype(j.template get<T>(), void())
5031 {
5032 for (std::size_t i1 = 0; i1 < N1; ++i1)
5033 {
5034 for (std::size_t i2 = 0; i2 < N2; ++i2)
5035 {
5036 arr[i1][i2] = j.at(i1).at(i2).template get<T>();
5037 }
5038 }
5039 }
5040
5041 template<typename BasicJsonType, typename T, std::size_t N1, std::size_t N2, std::size_t N3>
5042 auto from_json(const BasicJsonType& j, T (&arr)[N1][N2][N3]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
5043 -> decltype(j.template get<T>(), void())
5044 {
5045 for (std::size_t i1 = 0; i1 < N1; ++i1)
5046 {
5047 for (std::size_t i2 = 0; i2 < N2; ++i2)
5048 {
5049 for (std::size_t i3 = 0; i3 < N3; ++i3)
5050 {
5051 arr[i1][i2][i3] = j.at(i1).at(i2).at(i3).template get<T>();
5052 }
5053 }
5054 }
5055 }
5056
5057 template<typename BasicJsonType, typename T, std::size_t N1, std::size_t N2, std::size_t N3, std::size_t N4>
5058 auto from_json(const BasicJsonType& j, T (&arr)[N1][N2][N3][N4]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
5059 -> decltype(j.template get<T>(), void())
5060 {
5061 for (std::size_t i1 = 0; i1 < N1; ++i1)
5062 {
5063 for (std::size_t i2 = 0; i2 < N2; ++i2)
5064 {
5065 for (std::size_t i3 = 0; i3 < N3; ++i3)
5066 {
5067 for (std::size_t i4 = 0; i4 < N4; ++i4)
5068 {
5069 arr[i1][i2][i3][i4] = j.at(i1).at(i2).at(i3).at(i4).template get<T>();
5070 }
5071 }
5072 }
5073 }
5074 }
5075
5076 template<typename BasicJsonType>
5077 inline void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType::array_t& arr, priority_tag<3> /*unused*/)
5078 {
5079 arr = *j.template get_ptr<const typename BasicJsonType::array_t*>();
5080 }
5081
5082 template<typename BasicJsonType, typename T, std::size_t N>
5083 auto from_json_array_impl(const BasicJsonType& j, std::array<T, N>& arr,
5084 priority_tag<2> /*unused*/)
5085 -> decltype(j.template get<T>(), void())
5086 {
5087 for (std::size_t i = 0; i < N; ++i)
5088 {
5089 arr[i] = j.at(i).template get<T>();
5090 }
5091 }
5092
5093 template<typename BasicJsonType, typename ConstructibleArrayType,
5094 enable_if_t<
5095 std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
5096 int> = 0>
5097 auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<1> /*unused*/)
5098 -> decltype(
5099 arr.reserve(std::declval<typename ConstructibleArrayType::size_type>()),
5100 j.template get<typename ConstructibleArrayType::value_type>(),
5101 void())
5102 {
5103 using std::end;
5104
5105 ConstructibleArrayType ret;
5106 ret.reserve(j.size());
5107 std::transform(j.begin(), j.end(),
5108 std::inserter(ret, end(ret)), [](const BasicJsonType & i)
5109 {
5110 // get<BasicJsonType>() returns *this, this won't call a from_json
5111 // method when value_type is BasicJsonType
5112 return i.template get<typename ConstructibleArrayType::value_type>();
5113 });
5114 arr = std::move(ret);
5115 }
5116
5117 template<typename BasicJsonType, typename ConstructibleArrayType,
5118 enable_if_t<
5119 std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
5120 int> = 0>
5121 inline void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr,
5122 priority_tag<0> /*unused*/)
5123 {
5124 using std::end;
5125
5126 ConstructibleArrayType ret;
5127 std::transform(
5128 j.begin(), j.end(), std::inserter(ret, end(ret)),
5129 [](const BasicJsonType & i)
5130 {
5131 // get<BasicJsonType>() returns *this, this won't call a from_json
5132 // method when value_type is BasicJsonType
5133 return i.template get<typename ConstructibleArrayType::value_type>();
5134 });
5135 arr = std::move(ret);
5136 }
5137
5138 template < typename BasicJsonType, typename ConstructibleArrayType,
5139 enable_if_t <
5140 is_constructible_array_type<BasicJsonType, ConstructibleArrayType>::value&&
5141 !is_constructible_object_type<BasicJsonType, ConstructibleArrayType>::value&&
5142 !is_constructible_string_type<BasicJsonType, ConstructibleArrayType>::value&&
5143 !std::is_same<ConstructibleArrayType, typename BasicJsonType::binary_t>::value&&
5144 !is_basic_json<ConstructibleArrayType>::value,
5145 int > = 0 >
5146 auto from_json(const BasicJsonType& j, ConstructibleArrayType& arr)
5147 -> decltype(from_json_array_impl(j, arr, priority_tag<3> {}),
5148 j.template get<typename ConstructibleArrayType::value_type>(),
5149 void())
5150 {
5151 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
5152 {
5153 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
5154 }
5155
5156 from_json_array_impl(j, arr, priority_tag<3> {});
5157 }
5158
5159 template < typename BasicJsonType, typename T, std::size_t... Idx >
5160 std::array<T, sizeof...(Idx)> from_json_inplace_array_impl(BasicJsonType&& j,
5161 identity_tag<std::array<T, sizeof...(Idx)>> /*unused*/, index_sequence<Idx...> /*unused*/)
5162 {
5163 return { { std::forward<BasicJsonType>(j).at(Idx).template get<T>()... } };
5164 }
5165
5166 template < typename BasicJsonType, typename T, std::size_t N >
5167 auto from_json(BasicJsonType&& j, identity_tag<std::array<T, N>> tag)
5168 -> decltype(from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {}))
5169 {
5170 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
5171 {
5172 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
5173 }
5174
5175 return from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {});
5176 }
5177
5178 template<typename BasicJsonType>
5179 inline void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin)
5180 {
5181 if (JSON_HEDLEY_UNLIKELY(!j.is_binary()))
5182 {
5183 JSON_THROW(type_error::create(302, concat("type must be binary, but is ", j.type_name()), &j));
5184 }
5185
5186 bin = *j.template get_ptr<const typename BasicJsonType::binary_t*>();
5187 }
5188
5189 template<typename BasicJsonType, typename ConstructibleObjectType,
5190 enable_if_t<is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value, int> = 0>
5191 inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
5192 {
5193 if (JSON_HEDLEY_UNLIKELY(!j.is_object()))
5194 {
5195 JSON_THROW(type_error::create(302, concat("type must be object, but is ", j.type_name()), &j));
5196 }
5197
5198 ConstructibleObjectType ret;
5199 const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
5200 using value_type = typename ConstructibleObjectType::value_type;
5201 std::transform(
5202 inner_object->begin(), inner_object->end(),
5203 std::inserter(ret, ret.begin()),
5204 [](typename BasicJsonType::object_t::value_type const & p)
5205 {
5206 return value_type(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
5207 });
5208 obj = std::move(ret);
5209 }
5210
5211 // overload for arithmetic types, not chosen for basic_json template arguments
5212 // (BooleanType, etc..); note: Is it really necessary to provide explicit
5213 // overloads for boolean_t etc. in case of a custom BooleanType which is not
5214 // an arithmetic type?
5215 template < typename BasicJsonType, typename ArithmeticType,
5216 enable_if_t <
5217 std::is_arithmetic<ArithmeticType>::value&&
5218 !std::is_same<ArithmeticType, typename BasicJsonType::number_unsigned_t>::value&&
5219 !std::is_same<ArithmeticType, typename BasicJsonType::number_integer_t>::value&&
5220 !std::is_same<ArithmeticType, typename BasicJsonType::number_float_t>::value&&
5221 !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
5222 int > = 0 >
5223 inline void from_json(const BasicJsonType& j, ArithmeticType& val)
5224 {
5225 switch (static_cast<value_t>(j))
5226 {
5227 case value_t::number_unsigned:
5228 {
5229 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
5230 break;
5231 }
5232 case value_t::number_integer:
5233 {
5234 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
5235 break;
5236 }
5237 case value_t::number_float:
5238 {
5239 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
5240 break;
5241 }
5242 case value_t::boolean:
5243 {
5244 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
5245 break;
5246 }
5247
5248 case value_t::null:
5249 case value_t::object:
5250 case value_t::array:
5251 case value_t::string:
5252 case value_t::binary:
5253 case value_t::discarded:
5254 default:
5255 JSON_THROW(type_error::create(302, concat("type must be number, but is ", j.type_name()), &j));
5256 }
5257 }
5258
5259 template<typename BasicJsonType, typename... Args, std::size_t... Idx>
5260 std::tuple<Args...> from_json_tuple_impl_base(BasicJsonType&& j, index_sequence<Idx...> /*unused*/)
5261 {
5262 return std::make_tuple(std::forward<BasicJsonType>(j).at(Idx).template get<Args>()...);
5263 }
5264
5265 template<typename BasicJsonType>
5266 std::tuple<> from_json_tuple_impl_base(BasicJsonType& /*unused*/, index_sequence<> /*unused*/)
5267 {
5268 return {};
5269 }
5270
5271 template < typename BasicJsonType, class A1, class A2 >
5272 std::pair<A1, A2> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::pair<A1, A2>> /*unused*/, priority_tag<0> /*unused*/)
5273 {
5274 return {std::forward<BasicJsonType>(j).at(0).template get<A1>(),
5275 std::forward<BasicJsonType>(j).at(1).template get<A2>()};
5276 }
5277
5278 template<typename BasicJsonType, typename A1, typename A2>
5279 inline void from_json_tuple_impl(BasicJsonType&& j, std::pair<A1, A2>& p, priority_tag<1> /*unused*/)
5280 {
5281 p = from_json_tuple_impl(std::forward<BasicJsonType>(j), identity_tag<std::pair<A1, A2>> {}, priority_tag<0> {});
5282 }
5283
5284 template<typename BasicJsonType, typename... Args>
5285 std::tuple<Args...> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::tuple<Args...>> /*unused*/, priority_tag<2> /*unused*/)
5286 {
5287 return from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
5288 }
5289
5290 template<typename BasicJsonType, typename... Args>
5291 inline void from_json_tuple_impl(BasicJsonType&& j, std::tuple<Args...>& t, priority_tag<3> /*unused*/)
5292 {
5293 t = from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
5294 }
5295
5296 template<typename BasicJsonType, typename TupleRelated>
5297 auto from_json(BasicJsonType&& j, TupleRelated&& t)
5298 -> decltype(from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {}))
5299 {
5300 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
5301 {
5302 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
5303 }
5304
5305 return from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {});
5306 }
5307
5308 template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator,
5309 typename = enable_if_t < !std::is_constructible <
5310 typename BasicJsonType::string_t, Key >::value >>
5311 inline void from_json(const BasicJsonType& j, std::map<Key, Value, Compare, Allocator>& m)
5312 {
5313 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
5314 {
5315 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
5316 }
5317 m.clear();
5318 for (const auto& p : j)
5319 {
5320 if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
5321 {
5322 JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
5323 }
5324 m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
5325 }
5326 }
5327
5328 template < typename BasicJsonType, typename Key, typename Value, typename Hash, typename KeyEqual, typename Allocator,
5329 typename = enable_if_t < !std::is_constructible <
5330 typename BasicJsonType::string_t, Key >::value >>
5331 inline void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>& m)
5332 {
5333 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
5334 {
5335 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
5336 }
5337 m.clear();
5338 for (const auto& p : j)
5339 {
5340 if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
5341 {
5342 JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
5343 }
5344 m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
5345 }
5346 }
5347
5348 #if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
5349 template<typename BasicJsonType>
5350 inline void from_json(const BasicJsonType& j, std_fs::path& p)
5351 {
5352 if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
5353 {
5354 JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
5355 }
5356 const auto& s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
5357 // Checking for C++20 standard or later can be insufficient in case the
5358 // library support for char8_t is either incomplete or was disabled
5359 // altogether. Use the __cpp_lib_char8_t feature test instead.
5360 #if defined(__cpp_lib_char8_t) && (__cpp_lib_char8_t >= 201907L)
5361 p = std_fs::path(std::u8string_view(reinterpret_cast<const char8_t*>(s.data()), s.size()));
5362 #else
5363 p = std_fs::u8path(s); // accepts UTF-8 encoded std::string in C++17, deprecated in C++20
5364 #endif
5365 }
5366 #endif
5367
5368 struct from_json_fn
5369 {
5370 template<typename BasicJsonType, typename T>
5371 auto operator()(const BasicJsonType& j, T&& val) const
5372 noexcept(noexcept(from_json(j, std::forward<T>(val))))
5373 -> decltype(from_json(j, std::forward<T>(val)))
5374 {
5375 return from_json(j, std::forward<T>(val));
5376 }
5377 };
5378
5379 } // namespace detail
5380
5381 #ifndef JSON_HAS_CPP_17
5382 /// namespace to hold default `from_json` function
5383 /// to see why this is required:
5384 /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
5385 namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
5386 {
5387 #endif
5388 JSON_INLINE_VARIABLE constexpr const auto& from_json = // NOLINT(misc-definitions-in-headers)
5389 detail::static_const<detail::from_json_fn>::value;
5390 #ifndef JSON_HAS_CPP_17
5391 } // namespace
5392 #endif
5393
5394 NLOHMANN_JSON_NAMESPACE_END
5395
5396 // #include <nlohmann/detail/conversions/to_json.hpp>
5397 // __ _____ _____ _____
5398 // __| | __| | | | JSON for Modern C++
5399 // | | |__ | | | | | | version 3.12.0
5400 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
5401 //
5402 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
5403 // SPDX-License-Identifier: MIT
5404
5405
5406
5407 // #include <nlohmann/detail/macro_scope.hpp>
5408 // JSON_HAS_CPP_17
5409 #ifdef JSON_HAS_CPP_17
5410 #include <optional> // optional
5411 #endif
5412
5413 #include <algorithm> // copy
5414 #include <iterator> // begin, end
5415 #include <memory> // allocator_traits
5416 #include <string> // basic_string, char_traits
5417 #include <tuple> // tuple, get
5418 #include <type_traits> // is_same, is_constructible, is_floating_point, is_enum, underlying_type
5419 #include <utility> // move, forward, declval, pair
5420 #include <valarray> // valarray
5421 #include <vector> // vector
5422
5423 // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
5424 // __ _____ _____ _____
5425 // __| | __| | | | JSON for Modern C++
5426 // | | |__ | | | | | | version 3.12.0
5427 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
5428 //
5429 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
5430 // SPDX-License-Identifier: MIT
5431
5432
5433
5434 #include <cstddef> // size_t
5435 #include <iterator> // forward_iterator_tag
5436 #include <tuple> // tuple_size, get, tuple_element
5437 #include <utility> // move
5438
5439 #if JSON_HAS_RANGES
5440 #include <ranges> // enable_borrowed_range
5441 #endif
5442
5443 // #include <nlohmann/detail/abi_macros.hpp>
5444
5445 // #include <nlohmann/detail/meta/type_traits.hpp>
5446
5447 // #include <nlohmann/detail/string_utils.hpp>
5448 // __ _____ _____ _____
5449 // __| | __| | | | JSON for Modern C++
5450 // | | |__ | | | | | | version 3.12.0
5451 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
5452 //
5453 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
5454 // SPDX-License-Identifier: MIT
5455
5456
5457
5458 #include <cstddef> // size_t
5459 #include <string> // string, to_string
5460
5461 // #include <nlohmann/detail/abi_macros.hpp>
5462
5463
5464 NLOHMANN_JSON_NAMESPACE_BEGIN
5465 namespace detail
5466 {
5467
5468 template<typename StringType>
5469 void int_to_string(StringType& target, std::size_t value)
5470 {
5471 // For ADL
5472 using std::to_string;
5473 target = to_string(value);
5474 }
5475
5476 template<typename StringType>
5477 StringType to_string(std::size_t value)
5478 {
5479 StringType result;
5480 int_to_string(result, value);
5481 return result;
5482 }
5483
5484 } // namespace detail
5485 NLOHMANN_JSON_NAMESPACE_END
5486
5487 // #include <nlohmann/detail/value_t.hpp>
5488
5489
5490 NLOHMANN_JSON_NAMESPACE_BEGIN
5491 namespace detail
5492 {
5493
5494 template<typename IteratorType> class iteration_proxy_value
5495 {
5496 public:
5497 using difference_type = std::ptrdiff_t;
5498 using value_type = iteration_proxy_value;
5499 using pointer = value_type *;
5500 using reference = value_type &;
5501 using iterator_category = std::forward_iterator_tag;
5502 using string_type = typename std::remove_cv< typename std::remove_reference<decltype( std::declval<IteratorType>().key() ) >::type >::type;
5503
5504 private:
5505 /// the iterator
5506 IteratorType anchor{};
5507 /// an index for arrays (used to create key names)
5508 std::size_t array_index = 0;
5509 /// last stringified array index
5510 mutable std::size_t array_index_last = 0;
5511 /// a string representation of the array index
5512 mutable string_type array_index_str = "0";
5513 /// an empty string (to return a reference for primitive values)
5514 string_type empty_str{};
5515
5516 public:
5517 explicit iteration_proxy_value() = default;
5518 explicit iteration_proxy_value(IteratorType it, std::size_t array_index_ = 0)
5519 noexcept(std::is_nothrow_move_constructible<IteratorType>::value
5520 && std::is_nothrow_default_constructible<string_type>::value)
5521 : anchor(std::move(it))
5522 , array_index(array_index_)
5523 {}
5524
5525 iteration_proxy_value(iteration_proxy_value const&) = default;
5526 iteration_proxy_value& operator=(iteration_proxy_value const&) = default;
5527 // older GCCs are a bit fussy and require explicit noexcept specifiers on defaulted functions
5528 iteration_proxy_value(iteration_proxy_value&&)
5529 noexcept(std::is_nothrow_move_constructible<IteratorType>::value
5530 && std::is_nothrow_move_constructible<string_type>::value) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor,cppcoreguidelines-noexcept-move-operations)
5531 iteration_proxy_value& operator=(iteration_proxy_value&&)
5532 noexcept(std::is_nothrow_move_assignable<IteratorType>::value
5533 && std::is_nothrow_move_assignable<string_type>::value) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor,cppcoreguidelines-noexcept-move-operations)
5534 ~iteration_proxy_value() = default;
5535
5536 /// dereference operator (needed for range-based for)
5537 const iteration_proxy_value& operator*() const
5538 {
5539 return *this;
5540 }
5541
5542 /// increment operator (needed for range-based for)
5543 iteration_proxy_value& operator++()
5544 {
5545 ++anchor;
5546 ++array_index;
5547
5548 return *this;
5549 }
5550
5551 iteration_proxy_value operator++(int)& // NOLINT(cert-dcl21-cpp)
5552 {
5553 auto tmp = iteration_proxy_value(anchor, array_index);
5554 ++anchor;
5555 ++array_index;
5556 return tmp;
5557 }
5558
5559 /// equality operator (needed for InputIterator)
5560 bool operator==(const iteration_proxy_value& o) const
5561 {
5562 return anchor == o.anchor;
5563 }
5564
5565 /// inequality operator (needed for range-based for)
5566 bool operator!=(const iteration_proxy_value& o) const
5567 {
5568 return anchor != o.anchor;
5569 }
5570
5571 /// return key of the iterator
5572 const string_type& key() const
5573 {
5574 JSON_ASSERT(anchor.m_object != nullptr);
5575
5576 switch (anchor.m_object->type())
5577 {
5578 // use integer array index as key
5579 case value_t::array:
5580 {
5581 if (array_index != array_index_last)
5582 {
5583 int_to_string( array_index_str, array_index );
5584 array_index_last = array_index;
5585 }
5586 return array_index_str;
5587 }
5588
5589 // use key from the object
5590 case value_t::object:
5591 return anchor.key();
5592
5593 // use an empty key for all primitive types
5594 case value_t::null:
5595 case value_t::string:
5596 case value_t::boolean:
5597 case value_t::number_integer:
5598 case value_t::number_unsigned:
5599 case value_t::number_float:
5600 case value_t::binary:
5601 case value_t::discarded:
5602 default:
5603 return empty_str;
5604 }
5605 }
5606
5607 /// return value of the iterator
5608 typename IteratorType::reference value() const
5609 {
5610 return anchor.value();
5611 }
5612 };
5613
5614 /// proxy class for the items() function
5615 template<typename IteratorType> class iteration_proxy
5616 {
5617 private:
5618 /// the container to iterate
5619 typename IteratorType::pointer container = nullptr;
5620
5621 public:
5622 explicit iteration_proxy() = default;
5623
5624 /// construct iteration proxy from a container
5625 explicit iteration_proxy(typename IteratorType::reference cont) noexcept
5626 : container(&cont) {}
5627
5628 iteration_proxy(iteration_proxy const&) = default;
5629 iteration_proxy& operator=(iteration_proxy const&) = default;
5630 iteration_proxy(iteration_proxy&&) noexcept = default;
5631 iteration_proxy& operator=(iteration_proxy&&) noexcept = default;
5632 ~iteration_proxy() = default;
5633
5634 /// return iterator begin (needed for range-based for)
5635 iteration_proxy_value<IteratorType> begin() const noexcept
5636 {
5637 return iteration_proxy_value<IteratorType>(container->begin());
5638 }
5639
5640 /// return iterator end (needed for range-based for)
5641 iteration_proxy_value<IteratorType> end() const noexcept
5642 {
5643 return iteration_proxy_value<IteratorType>(container->end());
5644 }
5645 };
5646
5647 // Structured Bindings Support
5648 // For further reference see https://blog.tartanllama.xyz/structured-bindings/
5649 // And see https://github.com/nlohmann/json/pull/1391
5650 template<std::size_t N, typename IteratorType, enable_if_t<N == 0, int> = 0>
5651 auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.key())
5652 {
5653 return i.key();
5654 }
5655 // Structured Bindings Support
5656 // For further reference see https://blog.tartanllama.xyz/structured-bindings/
5657 // And see https://github.com/nlohmann/json/pull/1391
5658 template<std::size_t N, typename IteratorType, enable_if_t<N == 1, int> = 0>
5659 auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.value())
5660 {
5661 return i.value();
5662 }
5663
5664 } // namespace detail
5665 NLOHMANN_JSON_NAMESPACE_END
5666
5667 // The Addition to the STD Namespace is required to add
5668 // Structured Bindings Support to the iteration_proxy_value class
5669 // For further reference see https://blog.tartanllama.xyz/structured-bindings/
5670 // And see https://github.com/nlohmann/json/pull/1391
5671 namespace std
5672 {
5673
5674 #if defined(__clang__)
5675 // Fix: https://github.com/nlohmann/json/issues/1401
5676 #pragma clang diagnostic push
5677 #pragma clang diagnostic ignored "-Wmismatched-tags"
5678 #endif
5679 template<typename IteratorType>
5680 class tuple_size<::nlohmann::detail::iteration_proxy_value<IteratorType>> // NOLINT(cert-dcl58-cpp)
5681 : public std::integral_constant<std::size_t, 2> {};
5682
5683 template<std::size_t N, typename IteratorType>
5684 class tuple_element<N, ::nlohmann::detail::iteration_proxy_value<IteratorType >> // NOLINT(cert-dcl58-cpp)
5685 {
5686 public:
5687 using type = decltype(
5688 get<N>(std::declval <
5689 ::nlohmann::detail::iteration_proxy_value<IteratorType >> ()));
5690 };
5691 #if defined(__clang__)
5692 #pragma clang diagnostic pop
5693 #endif
5694
5695 } // namespace std
5696
5697 #if JSON_HAS_RANGES
5698 template <typename IteratorType>
5699 inline constexpr bool ::std::ranges::enable_borrowed_range<::nlohmann::detail::iteration_proxy<IteratorType>> = true;
5700 #endif
5701
5702 // #include <nlohmann/detail/meta/cpp_future.hpp>
5703
5704 // #include <nlohmann/detail/meta/std_fs.hpp>
5705
5706 // #include <nlohmann/detail/meta/type_traits.hpp>
5707
5708 // #include <nlohmann/detail/value_t.hpp>
5709
5710
5711 NLOHMANN_JSON_NAMESPACE_BEGIN
5712 namespace detail
5713 {
5714
5715 //////////////////
5716 // constructors //
5717 //////////////////
5718
5719 /*
5720 * Note all external_constructor<>::construct functions need to call
5721 * j.m_data.m_value.destroy(j.m_data.m_type) to avoid a memory leak in case j contains an
5722 * allocated value (e.g., a string). See bug issue
5723 * https://github.com/nlohmann/json/issues/2865 for more information.
5724 */
5725
5726 template<value_t> struct external_constructor;
5727
5728 template<>
5729 struct external_constructor<value_t::boolean>
5730 {
5731 template<typename BasicJsonType>
5732 static void construct(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept
5733 {
5734 j.m_data.m_value.destroy(j.m_data.m_type);
5735 j.m_data.m_type = value_t::boolean;
5736 j.m_data.m_value = b;
5737 j.assert_invariant();
5738 }
5739 };
5740
5741 template<>
5742 struct external_constructor<value_t::string>
5743 {
5744 template<typename BasicJsonType>
5745 static void construct(BasicJsonType& j, const typename BasicJsonType::string_t& s)
5746 {
5747 j.m_data.m_value.destroy(j.m_data.m_type);
5748 j.m_data.m_type = value_t::string;
5749 j.m_data.m_value = s;
5750 j.assert_invariant();
5751 }
5752
5753 template<typename BasicJsonType>
5754 static void construct(BasicJsonType& j, typename BasicJsonType::string_t&& s)
5755 {
5756 j.m_data.m_value.destroy(j.m_data.m_type);
5757 j.m_data.m_type = value_t::string;
5758 j.m_data.m_value = std::move(s);
5759 j.assert_invariant();
5760 }
5761
5762 template < typename BasicJsonType, typename CompatibleStringType,
5763 enable_if_t < !std::is_same<CompatibleStringType, typename BasicJsonType::string_t>::value,
5764 int > = 0 >
5765 static void construct(BasicJsonType& j, const CompatibleStringType& str)
5766 {
5767 j.m_data.m_value.destroy(j.m_data.m_type);
5768 j.m_data.m_type = value_t::string;
5769 j.m_data.m_value.string = j.template create<typename BasicJsonType::string_t>(str);
5770 j.assert_invariant();
5771 }
5772 };
5773
5774 template<>
5775 struct external_constructor<value_t::binary>
5776 {
5777 template<typename BasicJsonType>
5778 static void construct(BasicJsonType& j, const typename BasicJsonType::binary_t& b)
5779 {
5780 j.m_data.m_value.destroy(j.m_data.m_type);
5781 j.m_data.m_type = value_t::binary;
5782 j.m_data.m_value = typename BasicJsonType::binary_t(b);
5783 j.assert_invariant();
5784 }
5785
5786 template<typename BasicJsonType>
5787 static void construct(BasicJsonType& j, typename BasicJsonType::binary_t&& b)
5788 {
5789 j.m_data.m_value.destroy(j.m_data.m_type);
5790 j.m_data.m_type = value_t::binary;
5791 j.m_data.m_value = typename BasicJsonType::binary_t(std::move(b));
5792 j.assert_invariant();
5793 }
5794 };
5795
5796 template<>
5797 struct external_constructor<value_t::number_float>
5798 {
5799 template<typename BasicJsonType>
5800 static void construct(BasicJsonType& j, typename BasicJsonType::number_float_t val) noexcept
5801 {
5802 j.m_data.m_value.destroy(j.m_data.m_type);
5803 j.m_data.m_type = value_t::number_float;
5804 j.m_data.m_value = val;
5805 j.assert_invariant();
5806 }
5807 };
5808
5809 template<>
5810 struct external_constructor<value_t::number_unsigned>
5811 {
5812 template<typename BasicJsonType>
5813 static void construct(BasicJsonType& j, typename BasicJsonType::number_unsigned_t val) noexcept
5814 {
5815 j.m_data.m_value.destroy(j.m_data.m_type);
5816 j.m_data.m_type = value_t::number_unsigned;
5817 j.m_data.m_value = val;
5818 j.assert_invariant();
5819 }
5820 };
5821
5822 template<>
5823 struct external_constructor<value_t::number_integer>
5824 {
5825 template<typename BasicJsonType>
5826 static void construct(BasicJsonType& j, typename BasicJsonType::number_integer_t val) noexcept
5827 {
5828 j.m_data.m_value.destroy(j.m_data.m_type);
5829 j.m_data.m_type = value_t::number_integer;
5830 j.m_data.m_value = val;
5831 j.assert_invariant();
5832 }
5833 };
5834
5835 template<>
5836 struct external_constructor<value_t::array>
5837 {
5838 template<typename BasicJsonType>
5839 static void construct(BasicJsonType& j, const typename BasicJsonType::array_t& arr)
5840 {
5841 j.m_data.m_value.destroy(j.m_data.m_type);
5842 j.m_data.m_type = value_t::array;
5843 j.m_data.m_value = arr;
5844 j.set_parents();
5845 j.assert_invariant();
5846 }
5847
5848 template<typename BasicJsonType>
5849 static void construct(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
5850 {
5851 j.m_data.m_value.destroy(j.m_data.m_type);
5852 j.m_data.m_type = value_t::array;
5853 j.m_data.m_value = std::move(arr);
5854 j.set_parents();
5855 j.assert_invariant();
5856 }
5857
5858 template < typename BasicJsonType, typename CompatibleArrayType,
5859 enable_if_t < !std::is_same<CompatibleArrayType, typename BasicJsonType::array_t>::value,
5860 int > = 0 >
5861 static void construct(BasicJsonType& j, const CompatibleArrayType& arr)
5862 {
5863 using std::begin;
5864 using std::end;
5865
5866 j.m_data.m_value.destroy(j.m_data.m_type);
5867 j.m_data.m_type = value_t::array;
5868 j.m_data.m_value.array = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
5869 j.set_parents();
5870 j.assert_invariant();
5871 }
5872
5873 template<typename BasicJsonType>
5874 static void construct(BasicJsonType& j, const std::vector<bool>& arr)
5875 {
5876 j.m_data.m_value.destroy(j.m_data.m_type);
5877 j.m_data.m_type = value_t::array;
5878 j.m_data.m_value = value_t::array;
5879 j.m_data.m_value.array->reserve(arr.size());
5880 for (const bool x : arr)
5881 {
5882 j.m_data.m_value.array->push_back(x);
5883 j.set_parent(j.m_data.m_value.array->back());
5884 }
5885 j.assert_invariant();
5886 }
5887
5888 template<typename BasicJsonType, typename T,
5889 enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
5890 static void construct(BasicJsonType& j, const std::valarray<T>& arr)
5891 {
5892 j.m_data.m_value.destroy(j.m_data.m_type);
5893 j.m_data.m_type = value_t::array;
5894 j.m_data.m_value = value_t::array;
5895 j.m_data.m_value.array->resize(arr.size());
5896 if (arr.size() > 0)
5897 {
5898 std::copy(std::begin(arr), std::end(arr), j.m_data.m_value.array->begin());
5899 }
5900 j.set_parents();
5901 j.assert_invariant();
5902 }
5903 };
5904
5905 template<>
5906 struct external_constructor<value_t::object>
5907 {
5908 template<typename BasicJsonType>
5909 static void construct(BasicJsonType& j, const typename BasicJsonType::object_t& obj)
5910 {
5911 j.m_data.m_value.destroy(j.m_data.m_type);
5912 j.m_data.m_type = value_t::object;
5913 j.m_data.m_value = obj;
5914 j.set_parents();
5915 j.assert_invariant();
5916 }
5917
5918 template<typename BasicJsonType>
5919 static void construct(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
5920 {
5921 j.m_data.m_value.destroy(j.m_data.m_type);
5922 j.m_data.m_type = value_t::object;
5923 j.m_data.m_value = std::move(obj);
5924 j.set_parents();
5925 j.assert_invariant();
5926 }
5927
5928 template < typename BasicJsonType, typename CompatibleObjectType,
5929 enable_if_t < !std::is_same<CompatibleObjectType, typename BasicJsonType::object_t>::value, int > = 0 >
5930 static void construct(BasicJsonType& j, const CompatibleObjectType& obj)
5931 {
5932 using std::begin;
5933 using std::end;
5934
5935 j.m_data.m_value.destroy(j.m_data.m_type);
5936 j.m_data.m_type = value_t::object;
5937 j.m_data.m_value.object = j.template create<typename BasicJsonType::object_t>(begin(obj), end(obj));
5938 j.set_parents();
5939 j.assert_invariant();
5940 }
5941 };
5942
5943 /////////////
5944 // to_json //
5945 /////////////
5946
5947 #ifdef JSON_HAS_CPP_17
5948 template<typename BasicJsonType, typename T,
5949 enable_if_t<std::is_constructible<BasicJsonType, T>::value, int> = 0>
5950 void to_json(BasicJsonType& j, const std::optional<T>& opt) noexcept
5951 {
5952 if (opt.has_value())
5953 {
5954 j = *opt;
5955 }
5956 else
5957 {
5958 j = nullptr;
5959 }
5960 }
5961 #endif
5962
5963 template<typename BasicJsonType, typename T,
5964 enable_if_t<std::is_same<T, typename BasicJsonType::boolean_t>::value, int> = 0>
5965 inline void to_json(BasicJsonType& j, T b) noexcept
5966 {
5967 external_constructor<value_t::boolean>::construct(j, b);
5968 }
5969
5970 template < typename BasicJsonType, typename BoolRef,
5971 enable_if_t <
5972 ((std::is_same<std::vector<bool>::reference, BoolRef>::value
5973 && !std::is_same <std::vector<bool>::reference, typename BasicJsonType::boolean_t&>::value)
5974 || (std::is_same<std::vector<bool>::const_reference, BoolRef>::value
5975 && !std::is_same <detail::uncvref_t<std::vector<bool>::const_reference>,
5976 typename BasicJsonType::boolean_t >::value))
5977 && std::is_convertible<const BoolRef&, typename BasicJsonType::boolean_t>::value, int > = 0 >
5978 inline void to_json(BasicJsonType& j, const BoolRef& b) noexcept
5979 {
5980 external_constructor<value_t::boolean>::construct(j, static_cast<typename BasicJsonType::boolean_t>(b));
5981 }
5982
5983 template<typename BasicJsonType, typename CompatibleString,
5984 enable_if_t<std::is_constructible<typename BasicJsonType::string_t, CompatibleString>::value, int> = 0>
5985 inline void to_json(BasicJsonType& j, const CompatibleString& s)
5986 {
5987 external_constructor<value_t::string>::construct(j, s);
5988 }
5989
5990 template<typename BasicJsonType>
5991 inline void to_json(BasicJsonType& j, typename BasicJsonType::string_t&& s)
5992 {
5993 external_constructor<value_t::string>::construct(j, std::move(s));
5994 }
5995
5996 template<typename BasicJsonType, typename FloatType,
5997 enable_if_t<std::is_floating_point<FloatType>::value, int> = 0>
5998 inline void to_json(BasicJsonType& j, FloatType val) noexcept
5999 {
6000 external_constructor<value_t::number_float>::construct(j, static_cast<typename BasicJsonType::number_float_t>(val));
6001 }
6002
6003 template<typename BasicJsonType, typename CompatibleNumberUnsignedType,
6004 enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_unsigned_t, CompatibleNumberUnsignedType>::value, int> = 0>
6005 inline void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept
6006 {
6007 external_constructor<value_t::number_unsigned>::construct(j, static_cast<typename BasicJsonType::number_unsigned_t>(val));
6008 }
6009
6010 template<typename BasicJsonType, typename CompatibleNumberIntegerType,
6011 enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_integer_t, CompatibleNumberIntegerType>::value, int> = 0>
6012 inline void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept
6013 {
6014 external_constructor<value_t::number_integer>::construct(j, static_cast<typename BasicJsonType::number_integer_t>(val));
6015 }
6016
6017 #if !JSON_DISABLE_ENUM_SERIALIZATION
6018 template<typename BasicJsonType, typename EnumType,
6019 enable_if_t<std::is_enum<EnumType>::value, int> = 0>
6020 inline void to_json(BasicJsonType& j, EnumType e) noexcept
6021 {
6022 using underlying_type = typename std::underlying_type<EnumType>::type;
6023 static constexpr value_t integral_value_t = std::is_unsigned<underlying_type>::value ? value_t::number_unsigned : value_t::number_integer;
6024 external_constructor<integral_value_t>::construct(j, static_cast<underlying_type>(e));
6025 }
6026 #endif // JSON_DISABLE_ENUM_SERIALIZATION
6027
6028 template<typename BasicJsonType>
6029 inline void to_json(BasicJsonType& j, const std::vector<bool>& e)
6030 {
6031 external_constructor<value_t::array>::construct(j, e);
6032 }
6033
6034 template < typename BasicJsonType, typename CompatibleArrayType,
6035 enable_if_t < is_compatible_array_type<BasicJsonType,
6036 CompatibleArrayType>::value&&
6037 !is_compatible_object_type<BasicJsonType, CompatibleArrayType>::value&&
6038 !is_compatible_string_type<BasicJsonType, CompatibleArrayType>::value&&
6039 !std::is_same<typename BasicJsonType::binary_t, CompatibleArrayType>::value&&
6040 !is_basic_json<CompatibleArrayType>::value,
6041 int > = 0 >
6042 inline void to_json(BasicJsonType& j, const CompatibleArrayType& arr)
6043 {
6044 external_constructor<value_t::array>::construct(j, arr);
6045 }
6046
6047 template<typename BasicJsonType>
6048 inline void to_json(BasicJsonType& j, const typename BasicJsonType::binary_t& bin)
6049 {
6050 external_constructor<value_t::binary>::construct(j, bin);
6051 }
6052
6053 template<typename BasicJsonType, typename T,
6054 enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
6055 inline void to_json(BasicJsonType& j, const std::valarray<T>& arr)
6056 {
6057 external_constructor<value_t::array>::construct(j, std::move(arr));
6058 }
6059
6060 template<typename BasicJsonType>
6061 inline void to_json(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
6062 {
6063 external_constructor<value_t::array>::construct(j, std::move(arr));
6064 }
6065
6066 template < typename BasicJsonType, typename CompatibleObjectType,
6067 enable_if_t < is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value&& !is_basic_json<CompatibleObjectType>::value, int > = 0 >
6068 inline void to_json(BasicJsonType& j, const CompatibleObjectType& obj)
6069 {
6070 external_constructor<value_t::object>::construct(j, obj);
6071 }
6072
6073 template<typename BasicJsonType>
6074 inline void to_json(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
6075 {
6076 external_constructor<value_t::object>::construct(j, std::move(obj));
6077 }
6078
6079 template <
6080 typename BasicJsonType, typename T, std::size_t N,
6081 enable_if_t < !std::is_constructible<typename BasicJsonType::string_t,
6082 const T(&)[N]>::value, // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
6083 int > = 0 >
6084 inline void to_json(BasicJsonType& j, const T(&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
6085 {
6086 external_constructor<value_t::array>::construct(j, arr);
6087 }
6088
6089 template < typename BasicJsonType, typename T1, typename T2, enable_if_t < std::is_constructible<BasicJsonType, T1>::value&& std::is_constructible<BasicJsonType, T2>::value, int > = 0 >
6090 inline void to_json(BasicJsonType& j, const std::pair<T1, T2>& p)
6091 {
6092 j = { p.first, p.second };
6093 }
6094
6095 // for https://github.com/nlohmann/json/pull/1134
6096 template<typename BasicJsonType, typename T,
6097 enable_if_t<std::is_same<T, iteration_proxy_value<typename BasicJsonType::iterator>>::value, int> = 0>
6098 inline void to_json(BasicJsonType& j, const T& b)
6099 {
6100 j = { {b.key(), b.value()} };
6101 }
6102
6103 template<typename BasicJsonType, typename Tuple, std::size_t... Idx>
6104 inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence<Idx...> /*unused*/)
6105 {
6106 j = { std::get<Idx>(t)... };
6107 }
6108
6109 template<typename BasicJsonType, typename Tuple>
6110 inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& /*unused*/, index_sequence<> /*unused*/)
6111 {
6112 using array_t = typename BasicJsonType::array_t;
6113 j = array_t();
6114 }
6115
6116 template<typename BasicJsonType, typename T, enable_if_t<is_constructible_tuple<BasicJsonType, T>::value, int > = 0>
6117 inline void to_json(BasicJsonType& j, const T& t)
6118 {
6119 to_json_tuple_impl(j, t, make_index_sequence<std::tuple_size<T>::value> {});
6120 }
6121
6122 #if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
6123 #if defined(__cpp_lib_char8_t)
6124 template<typename BasicJsonType, typename Tr, typename Allocator>
6125 inline void to_json(BasicJsonType& j, const std::basic_string<char8_t, Tr, Allocator>& s)
6126 {
6127 using OtherAllocator = typename std::allocator_traits<Allocator>::template rebind_alloc<char>;
6128 j = std::basic_string<char, std::char_traits<char>, OtherAllocator>(s.begin(), s.end(), s.get_allocator());
6129 }
6130 #endif
6131
6132 template<typename BasicJsonType>
6133 inline void to_json(BasicJsonType& j, const std_fs::path& p)
6134 {
6135 // Returns either a std::string or a std::u8string depending whether library
6136 // support for char8_t is enabled.
6137 j = p.u8string();
6138 }
6139 #endif
6140
6141 struct to_json_fn
6142 {
6143 template<typename BasicJsonType, typename T>
6144 auto operator()(BasicJsonType& j, T&& val) const noexcept(noexcept(to_json(j, std::forward<T>(val))))
6145 -> decltype(to_json(j, std::forward<T>(val)), void())
6146 {
6147 return to_json(j, std::forward<T>(val));
6148 }
6149 };
6150 } // namespace detail
6151
6152 #ifndef JSON_HAS_CPP_17
6153 /// namespace to hold default `to_json` function
6154 /// to see why this is required:
6155 /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
6156 namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
6157 {
6158 #endif
6159 JSON_INLINE_VARIABLE constexpr const auto& to_json = // NOLINT(misc-definitions-in-headers)
6160 detail::static_const<detail::to_json_fn>::value;
6161 #ifndef JSON_HAS_CPP_17
6162 } // namespace
6163 #endif
6164
6165 NLOHMANN_JSON_NAMESPACE_END
6166
6167 // #include <nlohmann/detail/meta/identity_tag.hpp>
6168
6169
6170 NLOHMANN_JSON_NAMESPACE_BEGIN
6171
6172 /// @sa https://json.nlohmann.me/api/adl_serializer/
6173 template<typename ValueType, typename>
6174 struct adl_serializer
6175 {
6176 /// @brief convert a JSON value to any value type
6177 /// @sa https://json.nlohmann.me/api/adl_serializer/from_json/
6178 template<typename BasicJsonType, typename TargetType = ValueType>
6179 static auto from_json(BasicJsonType && j, TargetType& val) noexcept(
6180 noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), val)))
6181 -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), val), void())
6182 {
6183 ::nlohmann::from_json(std::forward<BasicJsonType>(j), val);
6184 }
6185
6186 /// @brief convert a JSON value to any value type
6187 /// @sa https://json.nlohmann.me/api/adl_serializer/from_json/
6188 template<typename BasicJsonType, typename TargetType = ValueType>
6189 static auto from_json(BasicJsonType && j) noexcept(
6190 noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {})))
6191 -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {}))
6192 {
6193 return ::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {});
6194 }
6195
6196 /// @brief convert any value type to a JSON value
6197 /// @sa https://json.nlohmann.me/api/adl_serializer/to_json/
6198 template<typename BasicJsonType, typename TargetType = ValueType>
6199 static auto to_json(BasicJsonType& j, TargetType && val) noexcept(
6200 noexcept(::nlohmann::to_json(j, std::forward<TargetType>(val))))
6201 -> decltype(::nlohmann::to_json(j, std::forward<TargetType>(val)), void())
6202 {
6203 ::nlohmann::to_json(j, std::forward<TargetType>(val));
6204 }
6205 };
6206
6207 NLOHMANN_JSON_NAMESPACE_END
6208
6209 // #include <nlohmann/byte_container_with_subtype.hpp>
6210 // __ _____ _____ _____
6211 // __| | __| | | | JSON for Modern C++
6212 // | | |__ | | | | | | version 3.12.0
6213 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
6214 //
6215 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
6216 // SPDX-License-Identifier: MIT
6217
6218
6219
6220 #include <cstdint> // uint8_t, uint64_t
6221 #include <tuple> // tie
6222 #include <utility> // move
6223
6224 // #include <nlohmann/detail/abi_macros.hpp>
6225
6226
6227 NLOHMANN_JSON_NAMESPACE_BEGIN
6228
6229 /// @brief an internal type for a backed binary type
6230 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/
6231 template<typename BinaryType>
6232 class byte_container_with_subtype : public BinaryType
6233 {
6234 public:
6235 using container_type = BinaryType;
6236 using subtype_type = std::uint64_t;
6237
6238 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
6239 byte_container_with_subtype() noexcept(noexcept(container_type()))
6240 : container_type()
6241 {}
6242
6243 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
6244 byte_container_with_subtype(const container_type& b) noexcept(noexcept(container_type(b)))
6245 : container_type(b)
6246 {}
6247
6248 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
6249 byte_container_with_subtype(container_type&& b) noexcept(noexcept(container_type(std::move(b))))
6250 : container_type(std::move(b))
6251 {}
6252
6253 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
6254 byte_container_with_subtype(const container_type& b, subtype_type subtype_) noexcept(noexcept(container_type(b)))
6255 : container_type(b)
6256 , m_subtype(subtype_)
6257 , m_has_subtype(true)
6258 {}
6259
6260 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
6261 byte_container_with_subtype(container_type&& b, subtype_type subtype_) noexcept(noexcept(container_type(std::move(b))))
6262 : container_type(std::move(b))
6263 , m_subtype(subtype_)
6264 , m_has_subtype(true)
6265 {}
6266
6267 bool operator==(const byte_container_with_subtype& rhs) const
6268 {
6269 return std::tie(static_cast<const BinaryType&>(*this), m_subtype, m_has_subtype) ==
6270 std::tie(static_cast<const BinaryType&>(rhs), rhs.m_subtype, rhs.m_has_subtype);
6271 }
6272
6273 bool operator!=(const byte_container_with_subtype& rhs) const
6274 {
6275 return !(rhs == *this);
6276 }
6277
6278 /// @brief sets the binary subtype
6279 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/set_subtype/
6280 void set_subtype(subtype_type subtype_) noexcept
6281 {
6282 m_subtype = subtype_;
6283 m_has_subtype = true;
6284 }
6285
6286 /// @brief return the binary subtype
6287 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/subtype/
6288 constexpr subtype_type subtype() const noexcept
6289 {
6290 return m_has_subtype ? m_subtype : static_cast<subtype_type>(-1);
6291 }
6292
6293 /// @brief return whether the value has a subtype
6294 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/has_subtype/
6295 constexpr bool has_subtype() const noexcept
6296 {
6297 return m_has_subtype;
6298 }
6299
6300 /// @brief clears the binary subtype
6301 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/clear_subtype/
6302 void clear_subtype() noexcept
6303 {
6304 m_subtype = 0;
6305 m_has_subtype = false;
6306 }
6307
6308 private:
6309 subtype_type m_subtype = 0;
6310 bool m_has_subtype = false;
6311 };
6312
6313 NLOHMANN_JSON_NAMESPACE_END
6314
6315 // #include <nlohmann/detail/conversions/from_json.hpp>
6316
6317 // #include <nlohmann/detail/conversions/to_json.hpp>
6318
6319 // #include <nlohmann/detail/exceptions.hpp>
6320
6321 // #include <nlohmann/detail/hash.hpp>
6322 // __ _____ _____ _____
6323 // __| | __| | | | JSON for Modern C++
6324 // | | |__ | | | | | | version 3.12.0
6325 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
6326 //
6327 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
6328 // SPDX-License-Identifier: MIT
6329
6330
6331
6332 #include <cstdint> // uint8_t
6333 #include <cstddef> // size_t
6334 #include <functional> // hash
6335
6336 // #include <nlohmann/detail/abi_macros.hpp>
6337
6338 // #include <nlohmann/detail/value_t.hpp>
6339
6340
6341 NLOHMANN_JSON_NAMESPACE_BEGIN
6342 namespace detail
6343 {
6344
6345 // boost::hash_combine
6346 inline std::size_t combine(std::size_t seed, std::size_t h) noexcept
6347 {
6348 seed ^= h + 0x9e3779b9 + (seed << 6U) + (seed >> 2U);
6349 return seed;
6350 }
6351
6352 /*!
6353 @brief hash a JSON value
6354
6355 The hash function tries to rely on std::hash where possible. Furthermore, the
6356 type of the JSON value is taken into account to have different hash values for
6357 null, 0, 0U, and false, etc.
6358
6359 @tparam BasicJsonType basic_json specialization
6360 @param j JSON value to hash
6361 @return hash value of j
6362 */
6363 template<typename BasicJsonType>
6364 std::size_t hash(const BasicJsonType& j)
6365 {
6366 using string_t = typename BasicJsonType::string_t;
6367 using number_integer_t = typename BasicJsonType::number_integer_t;
6368 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
6369 using number_float_t = typename BasicJsonType::number_float_t;
6370
6371 const auto type = static_cast<std::size_t>(j.type());
6372 switch (j.type())
6373 {
6374 case BasicJsonType::value_t::null:
6375 case BasicJsonType::value_t::discarded:
6376 {
6377 return combine(type, 0);
6378 }
6379
6380 case BasicJsonType::value_t::object:
6381 {
6382 auto seed = combine(type, j.size());
6383 for (const auto& element : j.items())
6384 {
6385 const auto h = std::hash<string_t> {}(element.key());
6386 seed = combine(seed, h);
6387 seed = combine(seed, hash(element.value()));
6388 }
6389 return seed;
6390 }
6391
6392 case BasicJsonType::value_t::array:
6393 {
6394 auto seed = combine(type, j.size());
6395 for (const auto& element : j)
6396 {
6397 seed = combine(seed, hash(element));
6398 }
6399 return seed;
6400 }
6401
6402 case BasicJsonType::value_t::string:
6403 {
6404 const auto h = std::hash<string_t> {}(j.template get_ref<const string_t&>());
6405 return combine(type, h);
6406 }
6407
6408 case BasicJsonType::value_t::boolean:
6409 {
6410 const auto h = std::hash<bool> {}(j.template get<bool>());
6411 return combine(type, h);
6412 }
6413
6414 case BasicJsonType::value_t::number_integer:
6415 {
6416 const auto h = std::hash<number_integer_t> {}(j.template get<number_integer_t>());
6417 return combine(type, h);
6418 }
6419
6420 case BasicJsonType::value_t::number_unsigned:
6421 {
6422 const auto h = std::hash<number_unsigned_t> {}(j.template get<number_unsigned_t>());
6423 return combine(type, h);
6424 }
6425
6426 case BasicJsonType::value_t::number_float:
6427 {
6428 const auto h = std::hash<number_float_t> {}(j.template get<number_float_t>());
6429 return combine(type, h);
6430 }
6431
6432 case BasicJsonType::value_t::binary:
6433 {
6434 auto seed = combine(type, j.get_binary().size());
6435 const auto h = std::hash<bool> {}(j.get_binary().has_subtype());
6436 seed = combine(seed, h);
6437 seed = combine(seed, static_cast<std::size_t>(j.get_binary().subtype()));
6438 for (const auto byte : j.get_binary())
6439 {
6440 seed = combine(seed, std::hash<std::uint8_t> {}(byte));
6441 }
6442 return seed;
6443 }
6444
6445 default: // LCOV_EXCL_LINE
6446 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
6447 return 0; // LCOV_EXCL_LINE
6448 }
6449 }
6450
6451 } // namespace detail
6452 NLOHMANN_JSON_NAMESPACE_END
6453
6454 // #include <nlohmann/detail/input/binary_reader.hpp>
6455 // __ _____ _____ _____
6456 // __| | __| | | | JSON for Modern C++
6457 // | | |__ | | | | | | version 3.12.0
6458 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
6459 //
6460 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
6461 // SPDX-License-Identifier: MIT
6462
6463
6464
6465 #include <algorithm> // generate_n
6466 #include <array> // array
6467 #include <cmath> // ldexp
6468 #include <cstddef> // size_t
6469 #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
6470 #include <cstdio> // snprintf
6471 #include <cstring> // memcpy
6472 #include <iterator> // back_inserter
6473 #include <limits> // numeric_limits
6474 #include <string> // char_traits, string
6475 #include <utility> // make_pair, move
6476 #include <vector> // vector
6477 #ifdef __cpp_lib_byteswap
6478 #include <bit> //byteswap
6479 #endif
6480
6481 // #include <nlohmann/detail/exceptions.hpp>
6482
6483 // #include <nlohmann/detail/input/input_adapters.hpp>
6484 // __ _____ _____ _____
6485 // __| | __| | | | JSON for Modern C++
6486 // | | |__ | | | | | | version 3.12.0
6487 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
6488 //
6489 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
6490 // SPDX-License-Identifier: MIT
6491
6492
6493
6494 #include <array> // array
6495 #include <cstddef> // size_t
6496 #include <cstring> // strlen
6497 #include <iterator> // begin, end, iterator_traits, random_access_iterator_tag, distance, next
6498 #include <memory> // shared_ptr, make_shared, addressof
6499 #include <numeric> // accumulate
6500 #include <string> // string, char_traits
6501 #include <type_traits> // enable_if, is_base_of, is_pointer, is_integral, remove_pointer
6502 #include <utility> // pair, declval
6503
6504 #ifndef JSON_NO_IO
6505 #include <cstdio> // FILE *
6506 #include <istream> // istream
6507 #endif // JSON_NO_IO
6508
6509 // #include <nlohmann/detail/exceptions.hpp>
6510
6511 // #include <nlohmann/detail/iterators/iterator_traits.hpp>
6512
6513 // #include <nlohmann/detail/macro_scope.hpp>
6514
6515 // #include <nlohmann/detail/meta/type_traits.hpp>
6516
6517
6518 NLOHMANN_JSON_NAMESPACE_BEGIN
6519 namespace detail
6520 {
6521
6522 /// the supported input formats
6523 enum class input_format_t { json, cbor, msgpack, ubjson, bson, bjdata };
6524
6525 ////////////////////
6526 // input adapters //
6527 ////////////////////
6528
6529 #ifndef JSON_NO_IO
6530 /*!
6531 Input adapter for stdio file access. This adapter read only 1 byte and do not use any
6532 buffer. This adapter is a very low level adapter.
6533 */
6534 class file_input_adapter
6535 {
6536 public:
6537 using char_type = char;
6538
6539 JSON_HEDLEY_NON_NULL(2)
6540 explicit file_input_adapter(std::FILE* f) noexcept
6541 : m_file(f)
6542 {
6543 JSON_ASSERT(m_file != nullptr);
6544 }
6545
6546 // make class move-only
6547 file_input_adapter(const file_input_adapter&) = delete;
6548 file_input_adapter(file_input_adapter&&) noexcept = default;
6549 file_input_adapter& operator=(const file_input_adapter&) = delete;
6550 file_input_adapter& operator=(file_input_adapter&&) = delete;
6551 ~file_input_adapter() = default;
6552
6553 std::char_traits<char>::int_type get_character() noexcept
6554 {
6555 return std::fgetc(m_file);
6556 }
6557
6558 // returns the number of characters successfully read
6559 template<class T>
6560 std::size_t get_elements(T* dest, std::size_t count = 1)
6561 {
6562 return fread(dest, 1, sizeof(T) * count, m_file);
6563 }
6564
6565 private:
6566 /// the file pointer to read from
6567 std::FILE* m_file;
6568 };
6569
6570 /*!
6571 Input adapter for a (caching) istream. Ignores a UFT Byte Order Mark at
6572 beginning of input. Does not support changing the underlying std::streambuf
6573 in mid-input. Maintains underlying std::istream and std::streambuf to support
6574 subsequent use of standard std::istream operations to process any input
6575 characters following those used in parsing the JSON input. Clears the
6576 std::istream flags; any input errors (e.g., EOF) will be detected by the first
6577 subsequent call for input from the std::istream.
6578 */
6579 class input_stream_adapter
6580 {
6581 public:
6582 using char_type = char;
6583
6584 ~input_stream_adapter()
6585 {
6586 // clear stream flags; we use underlying streambuf I/O, do not
6587 // maintain ifstream flags, except eof
6588 if (is != nullptr)
6589 {
6590 is->clear(is->rdstate() & std::ios::eofbit);
6591 }
6592 }
6593
6594 explicit input_stream_adapter(std::istream& i)
6595 : is(&i), sb(i.rdbuf())
6596 {}
6597
6598 // delete because of pointer members
6599 input_stream_adapter(const input_stream_adapter&) = delete;
6600 input_stream_adapter& operator=(input_stream_adapter&) = delete;
6601 input_stream_adapter& operator=(input_stream_adapter&&) = delete;
6602
6603 input_stream_adapter(input_stream_adapter&& rhs) noexcept
6604 : is(rhs.is), sb(rhs.sb)
6605 {
6606 rhs.is = nullptr;
6607 rhs.sb = nullptr;
6608 }
6609
6610 // std::istream/std::streambuf use std::char_traits<char>::to_int_type, to
6611 // ensure that std::char_traits<char>::eof() and the character 0xFF do not
6612 // end up as the same value, e.g. 0xFFFFFFFF.
6613 std::char_traits<char>::int_type get_character()
6614 {
6615 auto res = sb->sbumpc();
6616 // set eof manually, as we don't use the istream interface.
6617 if (JSON_HEDLEY_UNLIKELY(res == std::char_traits<char>::eof()))
6618 {
6619 is->clear(is->rdstate() | std::ios::eofbit);
6620 }
6621 return res;
6622 }
6623
6624 template<class T>
6625 std::size_t get_elements(T* dest, std::size_t count = 1)
6626 {
6627 auto res = static_cast<std::size_t>(sb->sgetn(reinterpret_cast<char*>(dest), static_cast<std::streamsize>(count * sizeof(T))));
6628 if (JSON_HEDLEY_UNLIKELY(res < count * sizeof(T)))
6629 {
6630 is->clear(is->rdstate() | std::ios::eofbit);
6631 }
6632 return res;
6633 }
6634
6635 private:
6636 /// the associated input stream
6637 std::istream* is = nullptr;
6638 std::streambuf* sb = nullptr;
6639 };
6640 #endif // JSON_NO_IO
6641
6642 // General-purpose iterator-based adapter. It might not be as fast as
6643 // theoretically possible for some containers, but it is extremely versatile.
6644 template<typename IteratorType>
6645 class iterator_input_adapter
6646 {
6647 public:
6648 using char_type = typename std::iterator_traits<IteratorType>::value_type;
6649
6650 iterator_input_adapter(IteratorType first, IteratorType last)
6651 : current(std::move(first)), end(std::move(last))
6652 {}
6653
6654 typename char_traits<char_type>::int_type get_character()
6655 {
6656 if (JSON_HEDLEY_LIKELY(current != end))
6657 {
6658 auto result = char_traits<char_type>::to_int_type(*current);
6659 std::advance(current, 1);
6660 return result;
6661 }
6662
6663 return char_traits<char_type>::eof();
6664 }
6665
6666 // for general iterators, we cannot really do something better than falling back to processing the range one-by-one
6667 template<class T>
6668 std::size_t get_elements(T* dest, std::size_t count = 1)
6669 {
6670 auto* ptr = reinterpret_cast<char*>(dest);
6671 for (std::size_t read_index = 0; read_index < count * sizeof(T); ++read_index)
6672 {
6673 if (JSON_HEDLEY_LIKELY(current != end))
6674 {
6675 ptr[read_index] = static_cast<char>(*current);
6676 std::advance(current, 1);
6677 }
6678 else
6679 {
6680 return read_index;
6681 }
6682 }
6683 return count * sizeof(T);
6684 }
6685
6686 private:
6687 IteratorType current;
6688 IteratorType end;
6689
6690 template<typename BaseInputAdapter, size_t T>
6691 friend struct wide_string_input_helper;
6692
6693 bool empty() const
6694 {
6695 return current == end;
6696 }
6697 };
6698
6699 template<typename BaseInputAdapter, size_t T>
6700 struct wide_string_input_helper;
6701
6702 template<typename BaseInputAdapter>
6703 struct wide_string_input_helper<BaseInputAdapter, 4>
6704 {
6705 // UTF-32
6706 static void fill_buffer(BaseInputAdapter& input,
6707 std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
6708 size_t& utf8_bytes_index,
6709 size_t& utf8_bytes_filled)
6710 {
6711 utf8_bytes_index = 0;
6712
6713 if (JSON_HEDLEY_UNLIKELY(input.empty()))
6714 {
6715 utf8_bytes[0] = std::char_traits<char>::eof();
6716 utf8_bytes_filled = 1;
6717 }
6718 else
6719 {
6720 // get the current character
6721 const auto wc = input.get_character();
6722
6723 // UTF-32 to UTF-8 encoding
6724 if (wc < 0x80)
6725 {
6726 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
6727 utf8_bytes_filled = 1;
6728 }
6729 else if (wc <= 0x7FF)
6730 {
6731 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((static_cast<unsigned int>(wc) >> 6u) & 0x1Fu));
6732 utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
6733 utf8_bytes_filled = 2;
6734 }
6735 else if (wc <= 0xFFFF)
6736 {
6737 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((static_cast<unsigned int>(wc) >> 12u) & 0x0Fu));
6738 utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
6739 utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
6740 utf8_bytes_filled = 3;
6741 }
6742 else if (wc <= 0x10FFFF)
6743 {
6744 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | ((static_cast<unsigned int>(wc) >> 18u) & 0x07u));
6745 utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 12u) & 0x3Fu));
6746 utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
6747 utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
6748 utf8_bytes_filled = 4;
6749 }
6750 else
6751 {
6752 // unknown character
6753 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
6754 utf8_bytes_filled = 1;
6755 }
6756 }
6757 }
6758 };
6759
6760 template<typename BaseInputAdapter>
6761 struct wide_string_input_helper<BaseInputAdapter, 2>
6762 {
6763 // UTF-16
6764 static void fill_buffer(BaseInputAdapter& input,
6765 std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
6766 size_t& utf8_bytes_index,
6767 size_t& utf8_bytes_filled)
6768 {
6769 utf8_bytes_index = 0;
6770
6771 if (JSON_HEDLEY_UNLIKELY(input.empty()))
6772 {
6773 utf8_bytes[0] = std::char_traits<char>::eof();
6774 utf8_bytes_filled = 1;
6775 }
6776 else
6777 {
6778 // get the current character
6779 const auto wc = input.get_character();
6780
6781 // UTF-16 to UTF-8 encoding
6782 if (wc < 0x80)
6783 {
6784 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
6785 utf8_bytes_filled = 1;
6786 }
6787 else if (wc <= 0x7FF)
6788 {
6789 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((static_cast<unsigned int>(wc) >> 6u)));
6790 utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
6791 utf8_bytes_filled = 2;
6792 }
6793 else if (0xD800 > wc || wc >= 0xE000)
6794 {
6795 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((static_cast<unsigned int>(wc) >> 12u)));
6796 utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
6797 utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
6798 utf8_bytes_filled = 3;
6799 }
6800 else
6801 {
6802 if (JSON_HEDLEY_UNLIKELY(!input.empty()))
6803 {
6804 const auto wc2 = static_cast<unsigned int>(input.get_character());
6805 const auto charcode = 0x10000u + (((static_cast<unsigned int>(wc) & 0x3FFu) << 10u) | (wc2 & 0x3FFu));
6806 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | (charcode >> 18u));
6807 utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 12u) & 0x3Fu));
6808 utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 6u) & 0x3Fu));
6809 utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (charcode & 0x3Fu));
6810 utf8_bytes_filled = 4;
6811 }
6812 else
6813 {
6814 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
6815 utf8_bytes_filled = 1;
6816 }
6817 }
6818 }
6819 }
6820 };
6821
6822 // Wraps another input adapter to convert wide character types into individual bytes.
6823 template<typename BaseInputAdapter, typename WideCharType>
6824 class wide_string_input_adapter
6825 {
6826 public:
6827 using char_type = char;
6828
6829 wide_string_input_adapter(BaseInputAdapter base)
6830 : base_adapter(base) {}
6831
6832 typename std::char_traits<char>::int_type get_character() noexcept
6833 {
6834 // check if buffer needs to be filled
6835 if (utf8_bytes_index == utf8_bytes_filled)
6836 {
6837 fill_buffer<sizeof(WideCharType)>();
6838
6839 JSON_ASSERT(utf8_bytes_filled > 0);
6840 JSON_ASSERT(utf8_bytes_index == 0);
6841 }
6842
6843 // use buffer
6844 JSON_ASSERT(utf8_bytes_filled > 0);
6845 JSON_ASSERT(utf8_bytes_index < utf8_bytes_filled);
6846 return utf8_bytes[utf8_bytes_index++];
6847 }
6848
6849 // parsing binary with wchar doesn't make sense, but since the parsing mode can be runtime, we need something here
6850 template<class T>
6851 std::size_t get_elements(T* /*dest*/, std::size_t /*count*/ = 1)
6852 {
6853 JSON_THROW(parse_error::create(112, 1, "wide string type cannot be interpreted as binary data", nullptr));
6854 }
6855
6856 private:
6857 BaseInputAdapter base_adapter;
6858
6859 template<size_t T>
6860 void fill_buffer()
6861 {
6862 wide_string_input_helper<BaseInputAdapter, T>::fill_buffer(base_adapter, utf8_bytes, utf8_bytes_index, utf8_bytes_filled);
6863 }
6864
6865 /// a buffer for UTF-8 bytes
6866 std::array<std::char_traits<char>::int_type, 4> utf8_bytes = {{0, 0, 0, 0}};
6867
6868 /// index to the utf8_codes array for the next valid byte
6869 std::size_t utf8_bytes_index = 0;
6870 /// number of valid bytes in the utf8_codes array
6871 std::size_t utf8_bytes_filled = 0;
6872 };
6873
6874 template<typename IteratorType, typename Enable = void>
6875 struct iterator_input_adapter_factory
6876 {
6877 using iterator_type = IteratorType;
6878 using char_type = typename std::iterator_traits<iterator_type>::value_type;
6879 using adapter_type = iterator_input_adapter<iterator_type>;
6880
6881 static adapter_type create(IteratorType first, IteratorType last)
6882 {
6883 return adapter_type(std::move(first), std::move(last));
6884 }
6885 };
6886
6887 template<typename T>
6888 struct is_iterator_of_multibyte
6889 {
6890 using value_type = typename std::iterator_traits<T>::value_type;
6891 enum
6892 {
6893 value = sizeof(value_type) > 1
6894 };
6895 };
6896
6897 template<typename IteratorType>
6898 struct iterator_input_adapter_factory<IteratorType, enable_if_t<is_iterator_of_multibyte<IteratorType>::value>>
6899 {
6900 using iterator_type = IteratorType;
6901 using char_type = typename std::iterator_traits<iterator_type>::value_type;
6902 using base_adapter_type = iterator_input_adapter<iterator_type>;
6903 using adapter_type = wide_string_input_adapter<base_adapter_type, char_type>;
6904
6905 static adapter_type create(IteratorType first, IteratorType last)
6906 {
6907 return adapter_type(base_adapter_type(std::move(first), std::move(last)));
6908 }
6909 };
6910
6911 // General purpose iterator-based input
6912 template<typename IteratorType>
6913 typename iterator_input_adapter_factory<IteratorType>::adapter_type input_adapter(IteratorType first, IteratorType last)
6914 {
6915 using factory_type = iterator_input_adapter_factory<IteratorType>;
6916 return factory_type::create(first, last);
6917 }
6918
6919 // Convenience shorthand from container to iterator
6920 // Enables ADL on begin(container) and end(container)
6921 // Encloses the using declarations in namespace for not to leak them to outside scope
6922
6923 namespace container_input_adapter_factory_impl
6924 {
6925
6926 using std::begin;
6927 using std::end;
6928
6929 template<typename ContainerType, typename Enable = void>
6930 struct container_input_adapter_factory {};
6931
6932 template<typename ContainerType>
6933 struct container_input_adapter_factory< ContainerType,
6934 void_t<decltype(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>()))>>
6935 {
6936 using adapter_type = decltype(input_adapter(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>())));
6937
6938 static adapter_type create(const ContainerType& container)
6939 {
6940 return input_adapter(begin(container), end(container));
6941 }
6942 };
6943
6944 } // namespace container_input_adapter_factory_impl
6945
6946 template<typename ContainerType>
6947 typename container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::adapter_type input_adapter(const ContainerType& container)
6948 {
6949 return container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::create(container);
6950 }
6951
6952 // specialization for std::string
6953 using string_input_adapter_type = decltype(input_adapter(std::declval<std::string>()));
6954
6955 #ifndef JSON_NO_IO
6956 // Special cases with fast paths
6957 inline file_input_adapter input_adapter(std::FILE* file)
6958 {
6959 if (file == nullptr)
6960 {
6961 JSON_THROW(parse_error::create(101, 0, "attempting to parse an empty input; check that your input string or stream contains the expected JSON", nullptr));
6962 }
6963 return file_input_adapter(file);
6964 }
6965
6966 inline input_stream_adapter input_adapter(std::istream& stream)
6967 {
6968 return input_stream_adapter(stream);
6969 }
6970
6971 inline input_stream_adapter input_adapter(std::istream&& stream)
6972 {
6973 return input_stream_adapter(stream);
6974 }
6975 #endif // JSON_NO_IO
6976
6977 using contiguous_bytes_input_adapter = decltype(input_adapter(std::declval<const char*>(), std::declval<const char*>()));
6978
6979 // Null-delimited strings, and the like.
6980 template < typename CharT,
6981 typename std::enable_if <
6982 std::is_pointer<CharT>::value&&
6983 !std::is_array<CharT>::value&&
6984 std::is_integral<typename std::remove_pointer<CharT>::type>::value&&
6985 sizeof(typename std::remove_pointer<CharT>::type) == 1,
6986 int >::type = 0 >
6987 contiguous_bytes_input_adapter input_adapter(CharT b)
6988 {
6989 if (b == nullptr)
6990 {
6991 JSON_THROW(parse_error::create(101, 0, "attempting to parse an empty input; check that your input string or stream contains the expected JSON", nullptr));
6992 }
6993 auto length = std::strlen(reinterpret_cast<const char*>(b));
6994 const auto* ptr = reinterpret_cast<const char*>(b);
6995 return input_adapter(ptr, ptr + length); // cppcheck-suppress[nullPointerArithmeticRedundantCheck]
6996 }
6997
6998 template<typename T, std::size_t N>
6999 auto input_adapter(T (&array)[N]) -> decltype(input_adapter(array, array + N)) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
7000 {
7001 return input_adapter(array, array + N);
7002 }
7003
7004 // This class only handles inputs of input_buffer_adapter type.
7005 // It's required so that expressions like {ptr, len} can be implicitly cast
7006 // to the correct adapter.
7007 class span_input_adapter
7008 {
7009 public:
7010 template < typename CharT,
7011 typename std::enable_if <
7012 std::is_pointer<CharT>::value&&
7013 std::is_integral<typename std::remove_pointer<CharT>::type>::value&&
7014 sizeof(typename std::remove_pointer<CharT>::type) == 1,
7015 int >::type = 0 >
7016 span_input_adapter(CharT b, std::size_t l)
7017 : ia(reinterpret_cast<const char*>(b), reinterpret_cast<const char*>(b) + l) {}
7018
7019 template<class IteratorType,
7020 typename std::enable_if<
7021 std::is_same<typename iterator_traits<IteratorType>::iterator_category, std::random_access_iterator_tag>::value,
7022 int>::type = 0>
7023 span_input_adapter(IteratorType first, IteratorType last)
7024 : ia(input_adapter(first, last)) {}
7025
7026 contiguous_bytes_input_adapter&& get()
7027 {
7028 return std::move(ia); // NOLINT(hicpp-move-const-arg,performance-move-const-arg)
7029 }
7030
7031 private:
7032 contiguous_bytes_input_adapter ia;
7033 };
7034
7035 } // namespace detail
7036 NLOHMANN_JSON_NAMESPACE_END
7037
7038 // #include <nlohmann/detail/input/json_sax.hpp>
7039 // __ _____ _____ _____
7040 // __| | __| | | | JSON for Modern C++
7041 // | | |__ | | | | | | version 3.12.0
7042 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
7043 //
7044 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
7045 // SPDX-License-Identifier: MIT
7046
7047
7048
7049 #include <cstddef>
7050 #include <string> // string
7051 #include <type_traits> // enable_if_t
7052 #include <utility> // move
7053 #include <vector> // vector
7054
7055 // #include <nlohmann/detail/exceptions.hpp>
7056
7057 // #include <nlohmann/detail/input/lexer.hpp>
7058 // __ _____ _____ _____
7059 // __| | __| | | | JSON for Modern C++
7060 // | | |__ | | | | | | version 3.12.0
7061 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
7062 //
7063 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
7064 // SPDX-License-Identifier: MIT
7065
7066
7067
7068 #include <array> // array
7069 #include <clocale> // localeconv
7070 #include <cstddef> // size_t
7071 #include <cstdio> // snprintf
7072 #include <cstdlib> // strtof, strtod, strtold, strtoll, strtoull
7073 #include <initializer_list> // initializer_list
7074 #include <string> // char_traits, string
7075 #include <utility> // move
7076 #include <vector> // vector
7077
7078 // #include <nlohmann/detail/input/input_adapters.hpp>
7079
7080 // #include <nlohmann/detail/input/position_t.hpp>
7081
7082 // #include <nlohmann/detail/macro_scope.hpp>
7083
7084 // #include <nlohmann/detail/meta/type_traits.hpp>
7085
7086
7087 NLOHMANN_JSON_NAMESPACE_BEGIN
7088 namespace detail
7089 {
7090
7091 ///////////
7092 // lexer //
7093 ///////////
7094
7095 template<typename BasicJsonType>
7096 class lexer_base
7097 {
7098 public:
7099 /// token types for the parser
7100 enum class token_type
7101 {
7102 uninitialized, ///< indicating the scanner is uninitialized
7103 literal_true, ///< the `true` literal
7104 literal_false, ///< the `false` literal
7105 literal_null, ///< the `null` literal
7106 value_string, ///< a string -- use get_string() for actual value
7107 value_unsigned, ///< an unsigned integer -- use get_number_unsigned() for actual value
7108 value_integer, ///< a signed integer -- use get_number_integer() for actual value
7109 value_float, ///< an floating point number -- use get_number_float() for actual value
7110 begin_array, ///< the character for array begin `[`
7111 begin_object, ///< the character for object begin `{`
7112 end_array, ///< the character for array end `]`
7113 end_object, ///< the character for object end `}`
7114 name_separator, ///< the name separator `:`
7115 value_separator, ///< the value separator `,`
7116 parse_error, ///< indicating a parse error
7117 end_of_input, ///< indicating the end of the input buffer
7118 literal_or_value ///< a literal or the begin of a value (only for diagnostics)
7119 };
7120
7121 /// return name of values of type token_type (only used for errors)
7122 JSON_HEDLEY_RETURNS_NON_NULL
7123 JSON_HEDLEY_CONST
7124 static const char* token_type_name(const token_type t) noexcept
7125 {
7126 switch (t)
7127 {
7128 case token_type::uninitialized:
7129 return "<uninitialized>";
7130 case token_type::literal_true:
7131 return "true literal";
7132 case token_type::literal_false:
7133 return "false literal";
7134 case token_type::literal_null:
7135 return "null literal";
7136 case token_type::value_string:
7137 return "string literal";
7138 case token_type::value_unsigned:
7139 case token_type::value_integer:
7140 case token_type::value_float:
7141 return "number literal";
7142 case token_type::begin_array:
7143 return "'['";
7144 case token_type::begin_object:
7145 return "'{'";
7146 case token_type::end_array:
7147 return "']'";
7148 case token_type::end_object:
7149 return "'}'";
7150 case token_type::name_separator:
7151 return "':'";
7152 case token_type::value_separator:
7153 return "','";
7154 case token_type::parse_error:
7155 return "<parse error>";
7156 case token_type::end_of_input:
7157 return "end of input";
7158 case token_type::literal_or_value:
7159 return "'[', '{', or a literal";
7160 // LCOV_EXCL_START
7161 default: // catch non-enum values
7162 return "unknown token";
7163 // LCOV_EXCL_STOP
7164 }
7165 }
7166 };
7167 /*!
7168 @brief lexical analysis
7169
7170 This class organizes the lexical analysis during JSON deserialization.
7171 */
7172 template<typename BasicJsonType, typename InputAdapterType>
7173 class lexer : public lexer_base<BasicJsonType>
7174 {
7175 using number_integer_t = typename BasicJsonType::number_integer_t;
7176 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
7177 using number_float_t = typename BasicJsonType::number_float_t;
7178 using string_t = typename BasicJsonType::string_t;
7179 using char_type = typename InputAdapterType::char_type;
7180 using char_int_type = typename char_traits<char_type>::int_type;
7181
7182 public:
7183 using token_type = typename lexer_base<BasicJsonType>::token_type;
7184
7185 explicit lexer(InputAdapterType&& adapter, bool ignore_comments_ = false) noexcept
7186 : ia(std::move(adapter))
7187 , ignore_comments(ignore_comments_)
7188 , decimal_point_char(static_cast<char_int_type>(get_decimal_point()))
7189 {}
7190
7191 // delete because of pointer members
7192 lexer(const lexer&) = delete;
7193 lexer(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
7194 lexer& operator=(lexer&) = delete;
7195 lexer& operator=(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
7196 ~lexer() = default;
7197
7198 private:
7199 /////////////////////
7200 // locales
7201 /////////////////////
7202
7203 /// return the locale-dependent decimal point
7204 JSON_HEDLEY_PURE
7205 static char get_decimal_point() noexcept
7206 {
7207 const auto* loc = localeconv();
7208 JSON_ASSERT(loc != nullptr);
7209 return (loc->decimal_point == nullptr) ? '.' : *(loc->decimal_point);
7210 }
7211
7212 /////////////////////
7213 // scan functions
7214 /////////////////////
7215
7216 /*!
7217 @brief get codepoint from 4 hex characters following `\u`
7218
7219 For input "\u c1 c2 c3 c4" the codepoint is:
7220 (c1 * 0x1000) + (c2 * 0x0100) + (c3 * 0x0010) + c4
7221 = (c1 << 12) + (c2 << 8) + (c3 << 4) + (c4 << 0)
7222
7223 Furthermore, the possible characters '0'..'9', 'A'..'F', and 'a'..'f'
7224 must be converted to the integers 0x0..0x9, 0xA..0xF, 0xA..0xF, resp. The
7225 conversion is done by subtracting the offset (0x30, 0x37, and 0x57)
7226 between the ASCII value of the character and the desired integer value.
7227
7228 @return codepoint (0x0000..0xFFFF) or -1 in case of an error (e.g. EOF or
7229 non-hex character)
7230 */
7231 int get_codepoint()
7232 {
7233 // this function only makes sense after reading `\u`
7234 JSON_ASSERT(current == 'u');
7235 int codepoint = 0;
7236
7237 const auto factors = { 12u, 8u, 4u, 0u };
7238 for (const auto factor : factors)
7239 {
7240 get();
7241
7242 if (current >= '0' && current <= '9')
7243 {
7244 codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x30u) << factor);
7245 }
7246 else if (current >= 'A' && current <= 'F')
7247 {
7248 codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x37u) << factor);
7249 }
7250 else if (current >= 'a' && current <= 'f')
7251 {
7252 codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x57u) << factor);
7253 }
7254 else
7255 {
7256 return -1;
7257 }
7258 }
7259
7260 JSON_ASSERT(0x0000 <= codepoint && codepoint <= 0xFFFF);
7261 return codepoint;
7262 }
7263
7264 /*!
7265 @brief check if the next byte(s) are inside a given range
7266
7267 Adds the current byte and, for each passed range, reads a new byte and
7268 checks if it is inside the range. If a violation was detected, set up an
7269 error message and return false. Otherwise, return true.
7270
7271 @param[in] ranges list of integers; interpreted as list of pairs of
7272 inclusive lower and upper bound, respectively
7273
7274 @pre The passed list @a ranges must have 2, 4, or 6 elements; that is,
7275 1, 2, or 3 pairs. This precondition is enforced by an assertion.
7276
7277 @return true if and only if no range violation was detected
7278 */
7279 bool next_byte_in_range(std::initializer_list<char_int_type> ranges)
7280 {
7281 JSON_ASSERT(ranges.size() == 2 || ranges.size() == 4 || ranges.size() == 6);
7282 add(current);
7283
7284 for (auto range = ranges.begin(); range != ranges.end(); ++range)
7285 {
7286 get();
7287 if (JSON_HEDLEY_LIKELY(*range <= current && current <= *(++range))) // NOLINT(bugprone-inc-dec-in-conditions)
7288 {
7289 add(current);
7290 }
7291 else
7292 {
7293 error_message = "invalid string: ill-formed UTF-8 byte";
7294 return false;
7295 }
7296 }
7297
7298 return true;
7299 }
7300
7301 /*!
7302 @brief scan a string literal
7303
7304 This function scans a string according to Sect. 7 of RFC 8259. While
7305 scanning, bytes are escaped and copied into buffer token_buffer. Then the
7306 function returns successfully, token_buffer is *not* null-terminated (as it
7307 may contain \0 bytes), and token_buffer.size() is the number of bytes in the
7308 string.
7309
7310 @return token_type::value_string if string could be successfully scanned,
7311 token_type::parse_error otherwise
7312
7313 @note In case of errors, variable error_message contains a textual
7314 description.
7315 */
7316 token_type scan_string()
7317 {
7318 // reset token_buffer (ignore opening quote)
7319 reset();
7320
7321 // we entered the function by reading an open quote
7322 JSON_ASSERT(current == '\"');
7323
7324 while (true)
7325 {
7326 // get next character
7327 switch (get())
7328 {
7329 // end of file while parsing string
7330 case char_traits<char_type>::eof():
7331 {
7332 error_message = "invalid string: missing closing quote";
7333 return token_type::parse_error;
7334 }
7335
7336 // closing quote
7337 case '\"':
7338 {
7339 return token_type::value_string;
7340 }
7341
7342 // escapes
7343 case '\\':
7344 {
7345 switch (get())
7346 {
7347 // quotation mark
7348 case '\"':
7349 add('\"');
7350 break;
7351 // reverse solidus
7352 case '\\':
7353 add('\\');
7354 break;
7355 // solidus
7356 case '/':
7357 add('/');
7358 break;
7359 // backspace
7360 case 'b':
7361 add('\b');
7362 break;
7363 // form feed
7364 case 'f':
7365 add('\f');
7366 break;
7367 // line feed
7368 case 'n':
7369 add('\n');
7370 break;
7371 // carriage return
7372 case 'r':
7373 add('\r');
7374 break;
7375 // tab
7376 case 't':
7377 add('\t');
7378 break;
7379
7380 // unicode escapes
7381 case 'u':
7382 {
7383 const int codepoint1 = get_codepoint();
7384 int codepoint = codepoint1; // start with codepoint1
7385
7386 if (JSON_HEDLEY_UNLIKELY(codepoint1 == -1))
7387 {
7388 error_message = "invalid string: '\\u' must be followed by 4 hex digits";
7389 return token_type::parse_error;
7390 }
7391
7392 // check if code point is a high surrogate
7393 if (0xD800 <= codepoint1 && codepoint1 <= 0xDBFF)
7394 {
7395 // expect next \uxxxx entry
7396 if (JSON_HEDLEY_LIKELY(get() == '\\' && get() == 'u'))
7397 {
7398 const int codepoint2 = get_codepoint();
7399
7400 if (JSON_HEDLEY_UNLIKELY(codepoint2 == -1))
7401 {
7402 error_message = "invalid string: '\\u' must be followed by 4 hex digits";
7403 return token_type::parse_error;
7404 }
7405
7406 // check if codepoint2 is a low surrogate
7407 if (JSON_HEDLEY_LIKELY(0xDC00 <= codepoint2 && codepoint2 <= 0xDFFF))
7408 {
7409 // overwrite codepoint
7410 codepoint = static_cast<int>(
7411 // high surrogate occupies the most significant 22 bits
7412 (static_cast<unsigned int>(codepoint1) << 10u)
7413 // low surrogate occupies the least significant 15 bits
7414 + static_cast<unsigned int>(codepoint2)
7415 // there is still the 0xD800, 0xDC00 and 0x10000 noise
7416 // in the result, so we have to subtract with:
7417 // (0xD800 << 10) + DC00 - 0x10000 = 0x35FDC00
7418 - 0x35FDC00u);
7419 }
7420 else
7421 {
7422 error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF";
7423 return token_type::parse_error;
7424 }
7425 }
7426 else
7427 {
7428 error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF";
7429 return token_type::parse_error;
7430 }
7431 }
7432 else
7433 {
7434 if (JSON_HEDLEY_UNLIKELY(0xDC00 <= codepoint1 && codepoint1 <= 0xDFFF))
7435 {
7436 error_message = "invalid string: surrogate U+DC00..U+DFFF must follow U+D800..U+DBFF";
7437 return token_type::parse_error;
7438 }
7439 }
7440
7441 // result of the above calculation yields a proper codepoint
7442 JSON_ASSERT(0x00 <= codepoint && codepoint <= 0x10FFFF);
7443
7444 // translate codepoint into bytes
7445 if (codepoint < 0x80)
7446 {
7447 // 1-byte characters: 0xxxxxxx (ASCII)
7448 add(static_cast<char_int_type>(codepoint));
7449 }
7450 else if (codepoint <= 0x7FF)
7451 {
7452 // 2-byte characters: 110xxxxx 10xxxxxx
7453 add(static_cast<char_int_type>(0xC0u | (static_cast<unsigned int>(codepoint) >> 6u)));
7454 add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
7455 }
7456 else if (codepoint <= 0xFFFF)
7457 {
7458 // 3-byte characters: 1110xxxx 10xxxxxx 10xxxxxx
7459 add(static_cast<char_int_type>(0xE0u | (static_cast<unsigned int>(codepoint) >> 12u)));
7460 add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
7461 add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
7462 }
7463 else
7464 {
7465 // 4-byte characters: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
7466 add(static_cast<char_int_type>(0xF0u | (static_cast<unsigned int>(codepoint) >> 18u)));
7467 add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 12u) & 0x3Fu)));
7468 add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
7469 add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
7470 }
7471
7472 break;
7473 }
7474
7475 // other characters after escape
7476 default:
7477 error_message = "invalid string: forbidden character after backslash";
7478 return token_type::parse_error;
7479 }
7480
7481 break;
7482 }
7483
7484 // invalid control characters
7485 case 0x00:
7486 {
7487 error_message = "invalid string: control character U+0000 (NUL) must be escaped to \\u0000";
7488 return token_type::parse_error;
7489 }
7490
7491 case 0x01:
7492 {
7493 error_message = "invalid string: control character U+0001 (SOH) must be escaped to \\u0001";
7494 return token_type::parse_error;
7495 }
7496
7497 case 0x02:
7498 {
7499 error_message = "invalid string: control character U+0002 (STX) must be escaped to \\u0002";
7500 return token_type::parse_error;
7501 }
7502
7503 case 0x03:
7504 {
7505 error_message = "invalid string: control character U+0003 (ETX) must be escaped to \\u0003";
7506 return token_type::parse_error;
7507 }
7508
7509 case 0x04:
7510 {
7511 error_message = "invalid string: control character U+0004 (EOT) must be escaped to \\u0004";
7512 return token_type::parse_error;
7513 }
7514
7515 case 0x05:
7516 {
7517 error_message = "invalid string: control character U+0005 (ENQ) must be escaped to \\u0005";
7518 return token_type::parse_error;
7519 }
7520
7521 case 0x06:
7522 {
7523 error_message = "invalid string: control character U+0006 (ACK) must be escaped to \\u0006";
7524 return token_type::parse_error;
7525 }
7526
7527 case 0x07:
7528 {
7529 error_message = "invalid string: control character U+0007 (BEL) must be escaped to \\u0007";
7530 return token_type::parse_error;
7531 }
7532
7533 case 0x08:
7534 {
7535 error_message = "invalid string: control character U+0008 (BS) must be escaped to \\u0008 or \\b";
7536 return token_type::parse_error;
7537 }
7538
7539 case 0x09:
7540 {
7541 error_message = "invalid string: control character U+0009 (HT) must be escaped to \\u0009 or \\t";
7542 return token_type::parse_error;
7543 }
7544
7545 case 0x0A:
7546 {
7547 error_message = "invalid string: control character U+000A (LF) must be escaped to \\u000A or \\n";
7548 return token_type::parse_error;
7549 }
7550
7551 case 0x0B:
7552 {
7553 error_message = "invalid string: control character U+000B (VT) must be escaped to \\u000B";
7554 return token_type::parse_error;
7555 }
7556
7557 case 0x0C:
7558 {
7559 error_message = "invalid string: control character U+000C (FF) must be escaped to \\u000C or \\f";
7560 return token_type::parse_error;
7561 }
7562
7563 case 0x0D:
7564 {
7565 error_message = "invalid string: control character U+000D (CR) must be escaped to \\u000D or \\r";
7566 return token_type::parse_error;
7567 }
7568
7569 case 0x0E:
7570 {
7571 error_message = "invalid string: control character U+000E (SO) must be escaped to \\u000E";
7572 return token_type::parse_error;
7573 }
7574
7575 case 0x0F:
7576 {
7577 error_message = "invalid string: control character U+000F (SI) must be escaped to \\u000F";
7578 return token_type::parse_error;
7579 }
7580
7581 case 0x10:
7582 {
7583 error_message = "invalid string: control character U+0010 (DLE) must be escaped to \\u0010";
7584 return token_type::parse_error;
7585 }
7586
7587 case 0x11:
7588 {
7589 error_message = "invalid string: control character U+0011 (DC1) must be escaped to \\u0011";
7590 return token_type::parse_error;
7591 }
7592
7593 case 0x12:
7594 {
7595 error_message = "invalid string: control character U+0012 (DC2) must be escaped to \\u0012";
7596 return token_type::parse_error;
7597 }
7598
7599 case 0x13:
7600 {
7601 error_message = "invalid string: control character U+0013 (DC3) must be escaped to \\u0013";
7602 return token_type::parse_error;
7603 }
7604
7605 case 0x14:
7606 {
7607 error_message = "invalid string: control character U+0014 (DC4) must be escaped to \\u0014";
7608 return token_type::parse_error;
7609 }
7610
7611 case 0x15:
7612 {
7613 error_message = "invalid string: control character U+0015 (NAK) must be escaped to \\u0015";
7614 return token_type::parse_error;
7615 }
7616
7617 case 0x16:
7618 {
7619 error_message = "invalid string: control character U+0016 (SYN) must be escaped to \\u0016";
7620 return token_type::parse_error;
7621 }
7622
7623 case 0x17:
7624 {
7625 error_message = "invalid string: control character U+0017 (ETB) must be escaped to \\u0017";
7626 return token_type::parse_error;
7627 }
7628
7629 case 0x18:
7630 {
7631 error_message = "invalid string: control character U+0018 (CAN) must be escaped to \\u0018";
7632 return token_type::parse_error;
7633 }
7634
7635 case 0x19:
7636 {
7637 error_message = "invalid string: control character U+0019 (EM) must be escaped to \\u0019";
7638 return token_type::parse_error;
7639 }
7640
7641 case 0x1A:
7642 {
7643 error_message = "invalid string: control character U+001A (SUB) must be escaped to \\u001A";
7644 return token_type::parse_error;
7645 }
7646
7647 case 0x1B:
7648 {
7649 error_message = "invalid string: control character U+001B (ESC) must be escaped to \\u001B";
7650 return token_type::parse_error;
7651 }
7652
7653 case 0x1C:
7654 {
7655 error_message = "invalid string: control character U+001C (FS) must be escaped to \\u001C";
7656 return token_type::parse_error;
7657 }
7658
7659 case 0x1D:
7660 {
7661 error_message = "invalid string: control character U+001D (GS) must be escaped to \\u001D";
7662 return token_type::parse_error;
7663 }
7664
7665 case 0x1E:
7666 {
7667 error_message = "invalid string: control character U+001E (RS) must be escaped to \\u001E";
7668 return token_type::parse_error;
7669 }
7670
7671 case 0x1F:
7672 {
7673 error_message = "invalid string: control character U+001F (US) must be escaped to \\u001F";
7674 return token_type::parse_error;
7675 }
7676
7677 // U+0020..U+007F (except U+0022 (quote) and U+005C (backspace))
7678 case 0x20:
7679 case 0x21:
7680 case 0x23:
7681 case 0x24:
7682 case 0x25:
7683 case 0x26:
7684 case 0x27:
7685 case 0x28:
7686 case 0x29:
7687 case 0x2A:
7688 case 0x2B:
7689 case 0x2C:
7690 case 0x2D:
7691 case 0x2E:
7692 case 0x2F:
7693 case 0x30:
7694 case 0x31:
7695 case 0x32:
7696 case 0x33:
7697 case 0x34:
7698 case 0x35:
7699 case 0x36:
7700 case 0x37:
7701 case 0x38:
7702 case 0x39:
7703 case 0x3A:
7704 case 0x3B:
7705 case 0x3C:
7706 case 0x3D:
7707 case 0x3E:
7708 case 0x3F:
7709 case 0x40:
7710 case 0x41:
7711 case 0x42:
7712 case 0x43:
7713 case 0x44:
7714 case 0x45:
7715 case 0x46:
7716 case 0x47:
7717 case 0x48:
7718 case 0x49:
7719 case 0x4A:
7720 case 0x4B:
7721 case 0x4C:
7722 case 0x4D:
7723 case 0x4E:
7724 case 0x4F:
7725 case 0x50:
7726 case 0x51:
7727 case 0x52:
7728 case 0x53:
7729 case 0x54:
7730 case 0x55:
7731 case 0x56:
7732 case 0x57:
7733 case 0x58:
7734 case 0x59:
7735 case 0x5A:
7736 case 0x5B:
7737 case 0x5D:
7738 case 0x5E:
7739 case 0x5F:
7740 case 0x60:
7741 case 0x61:
7742 case 0x62:
7743 case 0x63:
7744 case 0x64:
7745 case 0x65:
7746 case 0x66:
7747 case 0x67:
7748 case 0x68:
7749 case 0x69:
7750 case 0x6A:
7751 case 0x6B:
7752 case 0x6C:
7753 case 0x6D:
7754 case 0x6E:
7755 case 0x6F:
7756 case 0x70:
7757 case 0x71:
7758 case 0x72:
7759 case 0x73:
7760 case 0x74:
7761 case 0x75:
7762 case 0x76:
7763 case 0x77:
7764 case 0x78:
7765 case 0x79:
7766 case 0x7A:
7767 case 0x7B:
7768 case 0x7C:
7769 case 0x7D:
7770 case 0x7E:
7771 case 0x7F:
7772 {
7773 add(current);
7774 break;
7775 }
7776
7777 // U+0080..U+07FF: bytes C2..DF 80..BF
7778 case 0xC2:
7779 case 0xC3:
7780 case 0xC4:
7781 case 0xC5:
7782 case 0xC6:
7783 case 0xC7:
7784 case 0xC8:
7785 case 0xC9:
7786 case 0xCA:
7787 case 0xCB:
7788 case 0xCC:
7789 case 0xCD:
7790 case 0xCE:
7791 case 0xCF:
7792 case 0xD0:
7793 case 0xD1:
7794 case 0xD2:
7795 case 0xD3:
7796 case 0xD4:
7797 case 0xD5:
7798 case 0xD6:
7799 case 0xD7:
7800 case 0xD8:
7801 case 0xD9:
7802 case 0xDA:
7803 case 0xDB:
7804 case 0xDC:
7805 case 0xDD:
7806 case 0xDE:
7807 case 0xDF:
7808 {
7809 if (JSON_HEDLEY_UNLIKELY(!next_byte_in_range({0x80, 0xBF})))
7810 {
7811 return token_type::parse_error;
7812 }
7813 break;
7814 }
7815
7816 // U+0800..U+0FFF: bytes E0 A0..BF 80..BF
7817 case 0xE0:
7818 {
7819 if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0xA0, 0xBF, 0x80, 0xBF}))))
7820 {
7821 return token_type::parse_error;
7822 }
7823 break;
7824 }
7825
7826 // U+1000..U+CFFF: bytes E1..EC 80..BF 80..BF
7827 // U+E000..U+FFFF: bytes EE..EF 80..BF 80..BF
7828 case 0xE1:
7829 case 0xE2:
7830 case 0xE3:
7831 case 0xE4:
7832 case 0xE5:
7833 case 0xE6:
7834 case 0xE7:
7835 case 0xE8:
7836 case 0xE9:
7837 case 0xEA:
7838 case 0xEB:
7839 case 0xEC:
7840 case 0xEE:
7841 case 0xEF:
7842 {
7843 if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF}))))
7844 {
7845 return token_type::parse_error;
7846 }
7847 break;
7848 }
7849
7850 // U+D000..U+D7FF: bytes ED 80..9F 80..BF
7851 case 0xED:
7852 {
7853 if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x9F, 0x80, 0xBF}))))
7854 {
7855 return token_type::parse_error;
7856 }
7857 break;
7858 }
7859
7860 // U+10000..U+3FFFF F0 90..BF 80..BF 80..BF
7861 case 0xF0:
7862 {
7863 if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x90, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
7864 {
7865 return token_type::parse_error;
7866 }
7867 break;
7868 }
7869
7870 // U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF
7871 case 0xF1:
7872 case 0xF2:
7873 case 0xF3:
7874 {
7875 if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
7876 {
7877 return token_type::parse_error;
7878 }
7879 break;
7880 }
7881
7882 // U+100000..U+10FFFF F4 80..8F 80..BF 80..BF
7883 case 0xF4:
7884 {
7885 if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x8F, 0x80, 0xBF, 0x80, 0xBF}))))
7886 {
7887 return token_type::parse_error;
7888 }
7889 break;
7890 }
7891
7892 // remaining bytes (80..C1 and F5..FF) are ill-formed
7893 default:
7894 {
7895 error_message = "invalid string: ill-formed UTF-8 byte";
7896 return token_type::parse_error;
7897 }
7898 }
7899 }
7900 }
7901
7902 /*!
7903 * @brief scan a comment
7904 * @return whether comment could be scanned successfully
7905 */
7906 bool scan_comment()
7907 {
7908 switch (get())
7909 {
7910 // single-line comments skip input until a newline or EOF is read
7911 case '/':
7912 {
7913 while (true)
7914 {
7915 switch (get())
7916 {
7917 case '\n':
7918 case '\r':
7919 case char_traits<char_type>::eof():
7920 case '\0':
7921 return true;
7922
7923 default:
7924 break;
7925 }
7926 }
7927 }
7928
7929 // multi-line comments skip input until */ is read
7930 case '*':
7931 {
7932 while (true)
7933 {
7934 switch (get())
7935 {
7936 case char_traits<char_type>::eof():
7937 case '\0':
7938 {
7939 error_message = "invalid comment; missing closing '*/'";
7940 return false;
7941 }
7942
7943 case '*':
7944 {
7945 switch (get())
7946 {
7947 case '/':
7948 return true;
7949
7950 default:
7951 {
7952 unget();
7953 continue;
7954 }
7955 }
7956 }
7957
7958 default:
7959 continue;
7960 }
7961 }
7962 }
7963
7964 // unexpected character after reading '/'
7965 default:
7966 {
7967 error_message = "invalid comment; expecting '/' or '*' after '/'";
7968 return false;
7969 }
7970 }
7971 }
7972
7973 JSON_HEDLEY_NON_NULL(2)
7974 static void strtof(float& f, const char* str, char** endptr) noexcept
7975 {
7976 f = std::strtof(str, endptr);
7977 }
7978
7979 JSON_HEDLEY_NON_NULL(2)
7980 static void strtof(double& f, const char* str, char** endptr) noexcept
7981 {
7982 f = std::strtod(str, endptr);
7983 }
7984
7985 JSON_HEDLEY_NON_NULL(2)
7986 static void strtof(long double& f, const char* str, char** endptr) noexcept
7987 {
7988 f = std::strtold(str, endptr);
7989 }
7990
7991 /*!
7992 @brief scan a number literal
7993
7994 This function scans a string according to Sect. 6 of RFC 8259.
7995
7996 The function is realized with a deterministic finite state machine derived
7997 from the grammar described in RFC 8259. Starting in state "init", the
7998 input is read and used to determined the next state. Only state "done"
7999 accepts the number. State "error" is a trap state to model errors. In the
8000 table below, "anything" means any character but the ones listed before.
8001
8002 state | 0 | 1-9 | e E | + | - | . | anything
8003 ---------|----------|----------|----------|---------|---------|----------|-----------
8004 init | zero | any1 | [error] | [error] | minus | [error] | [error]
8005 minus | zero | any1 | [error] | [error] | [error] | [error] | [error]
8006 zero | done | done | exponent | done | done | decimal1 | done
8007 any1 | any1 | any1 | exponent | done | done | decimal1 | done
8008 decimal1 | decimal2 | decimal2 | [error] | [error] | [error] | [error] | [error]
8009 decimal2 | decimal2 | decimal2 | exponent | done | done | done | done
8010 exponent | any2 | any2 | [error] | sign | sign | [error] | [error]
8011 sign | any2 | any2 | [error] | [error] | [error] | [error] | [error]
8012 any2 | any2 | any2 | done | done | done | done | done
8013
8014 The state machine is realized with one label per state (prefixed with
8015 "scan_number_") and `goto` statements between them. The state machine
8016 contains cycles, but any cycle can be left when EOF is read. Therefore,
8017 the function is guaranteed to terminate.
8018
8019 During scanning, the read bytes are stored in token_buffer. This string is
8020 then converted to a signed integer, an unsigned integer, or a
8021 floating-point number.
8022
8023 @return token_type::value_unsigned, token_type::value_integer, or
8024 token_type::value_float if number could be successfully scanned,
8025 token_type::parse_error otherwise
8026
8027 @note The scanner is independent of the current locale. Internally, the
8028 locale's decimal point is used instead of `.` to work with the
8029 locale-dependent converters.
8030 */
8031 token_type scan_number() // lgtm [cpp/use-of-goto] `goto` is used in this function to implement the number-parsing state machine described above. By design, any finite input will eventually reach the "done" state or return token_type::parse_error. In each intermediate state, 1 byte of the input is appended to the token_buffer vector, and only the already initialized variables token_buffer, number_type, and error_message are manipulated.
8032 {
8033 // reset token_buffer to store the number's bytes
8034 reset();
8035
8036 // the type of the parsed number; initially set to unsigned; will be
8037 // changed if minus sign, decimal point or exponent is read
8038 token_type number_type = token_type::value_unsigned;
8039
8040 // state (init): we just found out we need to scan a number
8041 switch (current)
8042 {
8043 case '-':
8044 {
8045 add(current);
8046 goto scan_number_minus;
8047 }
8048
8049 case '0':
8050 {
8051 add(current);
8052 goto scan_number_zero;
8053 }
8054
8055 case '1':
8056 case '2':
8057 case '3':
8058 case '4':
8059 case '5':
8060 case '6':
8061 case '7':
8062 case '8':
8063 case '9':
8064 {
8065 add(current);
8066 goto scan_number_any1;
8067 }
8068
8069 // all other characters are rejected outside scan_number()
8070 default: // LCOV_EXCL_LINE
8071 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
8072 }
8073
8074 scan_number_minus:
8075 // state: we just parsed a leading minus sign
8076 number_type = token_type::value_integer;
8077 switch (get())
8078 {
8079 case '0':
8080 {
8081 add(current);
8082 goto scan_number_zero;
8083 }
8084
8085 case '1':
8086 case '2':
8087 case '3':
8088 case '4':
8089 case '5':
8090 case '6':
8091 case '7':
8092 case '8':
8093 case '9':
8094 {
8095 add(current);
8096 goto scan_number_any1;
8097 }
8098
8099 default:
8100 {
8101 error_message = "invalid number; expected digit after '-'";
8102 return token_type::parse_error;
8103 }
8104 }
8105
8106 scan_number_zero:
8107 // state: we just parse a zero (maybe with a leading minus sign)
8108 switch (get())
8109 {
8110 case '.':
8111 {
8112 add(decimal_point_char);
8113 decimal_point_position = token_buffer.size() - 1;
8114 goto scan_number_decimal1;
8115 }
8116
8117 case 'e':
8118 case 'E':
8119 {
8120 add(current);
8121 goto scan_number_exponent;
8122 }
8123
8124 default:
8125 goto scan_number_done;
8126 }
8127
8128 scan_number_any1:
8129 // state: we just parsed a number 0-9 (maybe with a leading minus sign)
8130 switch (get())
8131 {
8132 case '0':
8133 case '1':
8134 case '2':
8135 case '3':
8136 case '4':
8137 case '5':
8138 case '6':
8139 case '7':
8140 case '8':
8141 case '9':
8142 {
8143 add(current);
8144 goto scan_number_any1;
8145 }
8146
8147 case '.':
8148 {
8149 add(decimal_point_char);
8150 decimal_point_position = token_buffer.size() - 1;
8151 goto scan_number_decimal1;
8152 }
8153
8154 case 'e':
8155 case 'E':
8156 {
8157 add(current);
8158 goto scan_number_exponent;
8159 }
8160
8161 default:
8162 goto scan_number_done;
8163 }
8164
8165 scan_number_decimal1:
8166 // state: we just parsed a decimal point
8167 number_type = token_type::value_float;
8168 switch (get())
8169 {
8170 case '0':
8171 case '1':
8172 case '2':
8173 case '3':
8174 case '4':
8175 case '5':
8176 case '6':
8177 case '7':
8178 case '8':
8179 case '9':
8180 {
8181 add(current);
8182 goto scan_number_decimal2;
8183 }
8184
8185 default:
8186 {
8187 error_message = "invalid number; expected digit after '.'";
8188 return token_type::parse_error;
8189 }
8190 }
8191
8192 scan_number_decimal2:
8193 // we just parsed at least one number after a decimal point
8194 switch (get())
8195 {
8196 case '0':
8197 case '1':
8198 case '2':
8199 case '3':
8200 case '4':
8201 case '5':
8202 case '6':
8203 case '7':
8204 case '8':
8205 case '9':
8206 {
8207 add(current);
8208 goto scan_number_decimal2;
8209 }
8210
8211 case 'e':
8212 case 'E':
8213 {
8214 add(current);
8215 goto scan_number_exponent;
8216 }
8217
8218 default:
8219 goto scan_number_done;
8220 }
8221
8222 scan_number_exponent:
8223 // we just parsed an exponent
8224 number_type = token_type::value_float;
8225 switch (get())
8226 {
8227 case '+':
8228 case '-':
8229 {
8230 add(current);
8231 goto scan_number_sign;
8232 }
8233
8234 case '0':
8235 case '1':
8236 case '2':
8237 case '3':
8238 case '4':
8239 case '5':
8240 case '6':
8241 case '7':
8242 case '8':
8243 case '9':
8244 {
8245 add(current);
8246 goto scan_number_any2;
8247 }
8248
8249 default:
8250 {
8251 error_message =
8252 "invalid number; expected '+', '-', or digit after exponent";
8253 return token_type::parse_error;
8254 }
8255 }
8256
8257 scan_number_sign:
8258 // we just parsed an exponent sign
8259 switch (get())
8260 {
8261 case '0':
8262 case '1':
8263 case '2':
8264 case '3':
8265 case '4':
8266 case '5':
8267 case '6':
8268 case '7':
8269 case '8':
8270 case '9':
8271 {
8272 add(current);
8273 goto scan_number_any2;
8274 }
8275
8276 default:
8277 {
8278 error_message = "invalid number; expected digit after exponent sign";
8279 return token_type::parse_error;
8280 }
8281 }
8282
8283 scan_number_any2:
8284 // we just parsed a number after the exponent or exponent sign
8285 switch (get())
8286 {
8287 case '0':
8288 case '1':
8289 case '2':
8290 case '3':
8291 case '4':
8292 case '5':
8293 case '6':
8294 case '7':
8295 case '8':
8296 case '9':
8297 {
8298 add(current);
8299 goto scan_number_any2;
8300 }
8301
8302 default:
8303 goto scan_number_done;
8304 }
8305
8306 scan_number_done:
8307 // unget the character after the number (we only read it to know that
8308 // we are done scanning a number)
8309 unget();
8310
8311 char* endptr = nullptr; // NOLINT(misc-const-correctness,cppcoreguidelines-pro-type-vararg,hicpp-vararg)
8312 errno = 0;
8313
8314 // try to parse integers first and fall back to floats
8315 if (number_type == token_type::value_unsigned)
8316 {
8317 const auto x = std::strtoull(token_buffer.data(), &endptr, 10);
8318
8319 // we checked the number format before
8320 JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
8321
8322 if (errno != ERANGE)
8323 {
8324 value_unsigned = static_cast<number_unsigned_t>(x);
8325 if (value_unsigned == x)
8326 {
8327 return token_type::value_unsigned;
8328 }
8329 }
8330 }
8331 else if (number_type == token_type::value_integer)
8332 {
8333 const auto x = std::strtoll(token_buffer.data(), &endptr, 10);
8334
8335 // we checked the number format before
8336 JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
8337
8338 if (errno != ERANGE)
8339 {
8340 value_integer = static_cast<number_integer_t>(x);
8341 if (value_integer == x)
8342 {
8343 return token_type::value_integer;
8344 }
8345 }
8346 }
8347
8348 // this code is reached if we parse a floating-point number or if an
8349 // integer conversion above failed
8350 strtof(value_float, token_buffer.data(), &endptr);
8351
8352 // we checked the number format before
8353 JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
8354
8355 return token_type::value_float;
8356 }
8357
8358 /*!
8359 @param[in] literal_text the literal text to expect
8360 @param[in] length the length of the passed literal text
8361 @param[in] return_type the token type to return on success
8362 */
8363 JSON_HEDLEY_NON_NULL(2)
8364 token_type scan_literal(const char_type* literal_text, const std::size_t length,
8365 token_type return_type)
8366 {
8367 JSON_ASSERT(char_traits<char_type>::to_char_type(current) == literal_text[0]);
8368 for (std::size_t i = 1; i < length; ++i)
8369 {
8370 if (JSON_HEDLEY_UNLIKELY(char_traits<char_type>::to_char_type(get()) != literal_text[i]))
8371 {
8372 error_message = "invalid literal";
8373 return token_type::parse_error;
8374 }
8375 }
8376 return return_type;
8377 }
8378
8379 /////////////////////
8380 // input management
8381 /////////////////////
8382
8383 /// reset token_buffer; current character is beginning of token
8384 void reset() noexcept
8385 {
8386 token_buffer.clear();
8387 token_string.clear();
8388 decimal_point_position = std::string::npos;
8389 token_string.push_back(char_traits<char_type>::to_char_type(current));
8390 }
8391
8392 /*
8393 @brief get next character from the input
8394
8395 This function provides the interface to the used input adapter. It does
8396 not throw in case the input reached EOF, but returns a
8397 `char_traits<char>::eof()` in that case. Stores the scanned characters
8398 for use in error messages.
8399
8400 @return character read from the input
8401 */
8402 char_int_type get()
8403 {
8404 ++position.chars_read_total;
8405 ++position.chars_read_current_line;
8406
8407 if (next_unget)
8408 {
8409 // just reset the next_unget variable and work with current
8410 next_unget = false;
8411 }
8412 else
8413 {
8414 current = ia.get_character();
8415 }
8416
8417 if (JSON_HEDLEY_LIKELY(current != char_traits<char_type>::eof()))
8418 {
8419 token_string.push_back(char_traits<char_type>::to_char_type(current));
8420 }
8421
8422 if (current == '\n')
8423 {
8424 ++position.lines_read;
8425 position.chars_read_current_line = 0;
8426 }
8427
8428 return current;
8429 }
8430
8431 /*!
8432 @brief unget current character (read it again on next get)
8433
8434 We implement unget by setting variable next_unget to true. The input is not
8435 changed - we just simulate ungetting by modifying chars_read_total,
8436 chars_read_current_line, and token_string. The next call to get() will
8437 behave as if the unget character is read again.
8438 */
8439 void unget()
8440 {
8441 next_unget = true;
8442
8443 --position.chars_read_total;
8444
8445 // in case we "unget" a newline, we have to also decrement the lines_read
8446 if (position.chars_read_current_line == 0)
8447 {
8448 if (position.lines_read > 0)
8449 {
8450 --position.lines_read;
8451 }
8452 }
8453 else
8454 {
8455 --position.chars_read_current_line;
8456 }
8457
8458 if (JSON_HEDLEY_LIKELY(current != char_traits<char_type>::eof()))
8459 {
8460 JSON_ASSERT(!token_string.empty());
8461 token_string.pop_back();
8462 }
8463 }
8464
8465 /// add a character to token_buffer
8466 void add(char_int_type c)
8467 {
8468 token_buffer.push_back(static_cast<typename string_t::value_type>(c));
8469 }
8470
8471 public:
8472 /////////////////////
8473 // value getters
8474 /////////////////////
8475
8476 /// return integer value
8477 constexpr number_integer_t get_number_integer() const noexcept
8478 {
8479 return value_integer;
8480 }
8481
8482 /// return unsigned integer value
8483 constexpr number_unsigned_t get_number_unsigned() const noexcept
8484 {
8485 return value_unsigned;
8486 }
8487
8488 /// return floating-point value
8489 constexpr number_float_t get_number_float() const noexcept
8490 {
8491 return value_float;
8492 }
8493
8494 /// return current string value (implicitly resets the token; useful only once)
8495 string_t& get_string()
8496 {
8497 // translate decimal points from locale back to '.' (#4084)
8498 if (decimal_point_char != '.' && decimal_point_position != std::string::npos)
8499 {
8500 token_buffer[decimal_point_position] = '.';
8501 }
8502 return token_buffer;
8503 }
8504
8505 /////////////////////
8506 // diagnostics
8507 /////////////////////
8508
8509 /// return position of last read token
8510 constexpr position_t get_position() const noexcept
8511 {
8512 return position;
8513 }
8514
8515 /// return the last read token (for errors only). Will never contain EOF
8516 /// (an arbitrary value that is not a valid char value, often -1), because
8517 /// 255 may legitimately occur. May contain NUL, which should be escaped.
8518 std::string get_token_string() const
8519 {
8520 // escape control characters
8521 std::string result;
8522 for (const auto c : token_string)
8523 {
8524 if (static_cast<unsigned char>(c) <= '\x1F')
8525 {
8526 // escape control characters
8527 std::array<char, 9> cs{{}};
8528 static_cast<void>((std::snprintf)(cs.data(), cs.size(), "<U+%.4X>", static_cast<unsigned char>(c))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
8529 result += cs.data();
8530 }
8531 else
8532 {
8533 // add character as is
8534 result.push_back(static_cast<std::string::value_type>(c));
8535 }
8536 }
8537
8538 return result;
8539 }
8540
8541 /// return syntax error message
8542 JSON_HEDLEY_RETURNS_NON_NULL
8543 constexpr const char* get_error_message() const noexcept
8544 {
8545 return error_message;
8546 }
8547
8548 /////////////////////
8549 // actual scanner
8550 /////////////////////
8551
8552 /*!
8553 @brief skip the UTF-8 byte order mark
8554 @return true iff there is no BOM or the correct BOM has been skipped
8555 */
8556 bool skip_bom()
8557 {
8558 if (get() == 0xEF)
8559 {
8560 // check if we completely parse the BOM
8561 return get() == 0xBB && get() == 0xBF;
8562 }
8563
8564 // the first character is not the beginning of the BOM; unget it to
8565 // process is later
8566 unget();
8567 return true;
8568 }
8569
8570 void skip_whitespace()
8571 {
8572 do
8573 {
8574 get();
8575 }
8576 while (current == ' ' || current == '\t' || current == '\n' || current == '\r');
8577 }
8578
8579 token_type scan()
8580 {
8581 // initially, skip the BOM
8582 if (position.chars_read_total == 0 && !skip_bom())
8583 {
8584 error_message = "invalid BOM; must be 0xEF 0xBB 0xBF if given";
8585 return token_type::parse_error;
8586 }
8587
8588 // read next character and ignore whitespace
8589 skip_whitespace();
8590
8591 // ignore comments
8592 while (ignore_comments && current == '/')
8593 {
8594 if (!scan_comment())
8595 {
8596 return token_type::parse_error;
8597 }
8598
8599 // skip following whitespace
8600 skip_whitespace();
8601 }
8602
8603 switch (current)
8604 {
8605 // structural characters
8606 case '[':
8607 return token_type::begin_array;
8608 case ']':
8609 return token_type::end_array;
8610 case '{':
8611 return token_type::begin_object;
8612 case '}':
8613 return token_type::end_object;
8614 case ':':
8615 return token_type::name_separator;
8616 case ',':
8617 return token_type::value_separator;
8618
8619 // literals
8620 case 't':
8621 {
8622 std::array<char_type, 4> true_literal = {{static_cast<char_type>('t'), static_cast<char_type>('r'), static_cast<char_type>('u'), static_cast<char_type>('e')}};
8623 return scan_literal(true_literal.data(), true_literal.size(), token_type::literal_true);
8624 }
8625 case 'f':
8626 {
8627 std::array<char_type, 5> false_literal = {{static_cast<char_type>('f'), static_cast<char_type>('a'), static_cast<char_type>('l'), static_cast<char_type>('s'), static_cast<char_type>('e')}};
8628 return scan_literal(false_literal.data(), false_literal.size(), token_type::literal_false);
8629 }
8630 case 'n':
8631 {
8632 std::array<char_type, 4> null_literal = {{static_cast<char_type>('n'), static_cast<char_type>('u'), static_cast<char_type>('l'), static_cast<char_type>('l')}};
8633 return scan_literal(null_literal.data(), null_literal.size(), token_type::literal_null);
8634 }
8635
8636 // string
8637 case '\"':
8638 return scan_string();
8639
8640 // number
8641 case '-':
8642 case '0':
8643 case '1':
8644 case '2':
8645 case '3':
8646 case '4':
8647 case '5':
8648 case '6':
8649 case '7':
8650 case '8':
8651 case '9':
8652 return scan_number();
8653
8654 // end of input (the null byte is needed when parsing from
8655 // string literals)
8656 case '\0':
8657 case char_traits<char_type>::eof():
8658 return token_type::end_of_input;
8659
8660 // error
8661 default:
8662 error_message = "invalid literal";
8663 return token_type::parse_error;
8664 }
8665 }
8666
8667 private:
8668 /// input adapter
8669 InputAdapterType ia;
8670
8671 /// whether comments should be ignored (true) or signaled as errors (false)
8672 const bool ignore_comments = false;
8673
8674 /// the current character
8675 char_int_type current = char_traits<char_type>::eof();
8676
8677 /// whether the next get() call should just return current
8678 bool next_unget = false;
8679
8680 /// the start position of the current token
8681 position_t position {};
8682
8683 /// raw input token string (for error messages)
8684 std::vector<char_type> token_string {};
8685
8686 /// buffer for variable-length tokens (numbers, strings)
8687 string_t token_buffer {};
8688
8689 /// a description of occurred lexer errors
8690 const char* error_message = "";
8691
8692 // number values
8693 number_integer_t value_integer = 0;
8694 number_unsigned_t value_unsigned = 0;
8695 number_float_t value_float = 0;
8696
8697 /// the decimal point
8698 const char_int_type decimal_point_char = '.';
8699 /// the position of the decimal point in the input
8700 std::size_t decimal_point_position = std::string::npos;
8701 };
8702
8703 } // namespace detail
8704 NLOHMANN_JSON_NAMESPACE_END
8705
8706 // #include <nlohmann/detail/macro_scope.hpp>
8707
8708 // #include <nlohmann/detail/string_concat.hpp>
8709
8710 NLOHMANN_JSON_NAMESPACE_BEGIN
8711
8712 /*!
8713 @brief SAX interface
8714
8715 This class describes the SAX interface used by @ref nlohmann::json::sax_parse.
8716 Each function is called in different situations while the input is parsed. The
8717 boolean return value informs the parser whether to continue processing the
8718 input.
8719 */
8720 template<typename BasicJsonType>
8721 struct json_sax
8722 {
8723 using number_integer_t = typename BasicJsonType::number_integer_t;
8724 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
8725 using number_float_t = typename BasicJsonType::number_float_t;
8726 using string_t = typename BasicJsonType::string_t;
8727 using binary_t = typename BasicJsonType::binary_t;
8728
8729 /*!
8730 @brief a null value was read
8731 @return whether parsing should proceed
8732 */
8733 virtual bool null() = 0;
8734
8735 /*!
8736 @brief a boolean value was read
8737 @param[in] val boolean value
8738 @return whether parsing should proceed
8739 */
8740 virtual bool boolean(bool val) = 0;
8741
8742 /*!
8743 @brief an integer number was read
8744 @param[in] val integer value
8745 @return whether parsing should proceed
8746 */
8747 virtual bool number_integer(number_integer_t val) = 0;
8748
8749 /*!
8750 @brief an unsigned integer number was read
8751 @param[in] val unsigned integer value
8752 @return whether parsing should proceed
8753 */
8754 virtual bool number_unsigned(number_unsigned_t val) = 0;
8755
8756 /*!
8757 @brief a floating-point number was read
8758 @param[in] val floating-point value
8759 @param[in] s raw token value
8760 @return whether parsing should proceed
8761 */
8762 virtual bool number_float(number_float_t val, const string_t& s) = 0;
8763
8764 /*!
8765 @brief a string value was read
8766 @param[in] val string value
8767 @return whether parsing should proceed
8768 @note It is safe to move the passed string value.
8769 */
8770 virtual bool string(string_t& val) = 0;
8771
8772 /*!
8773 @brief a binary value was read
8774 @param[in] val binary value
8775 @return whether parsing should proceed
8776 @note It is safe to move the passed binary value.
8777 */
8778 virtual bool binary(binary_t& val) = 0;
8779
8780 /*!
8781 @brief the beginning of an object was read
8782 @param[in] elements number of object elements or -1 if unknown
8783 @return whether parsing should proceed
8784 @note binary formats may report the number of elements
8785 */
8786 virtual bool start_object(std::size_t elements) = 0;
8787
8788 /*!
8789 @brief an object key was read
8790 @param[in] val object key
8791 @return whether parsing should proceed
8792 @note It is safe to move the passed string.
8793 */
8794 virtual bool key(string_t& val) = 0;
8795
8796 /*!
8797 @brief the end of an object was read
8798 @return whether parsing should proceed
8799 */
8800 virtual bool end_object() = 0;
8801
8802 /*!
8803 @brief the beginning of an array was read
8804 @param[in] elements number of array elements or -1 if unknown
8805 @return whether parsing should proceed
8806 @note binary formats may report the number of elements
8807 */
8808 virtual bool start_array(std::size_t elements) = 0;
8809
8810 /*!
8811 @brief the end of an array was read
8812 @return whether parsing should proceed
8813 */
8814 virtual bool end_array() = 0;
8815
8816 /*!
8817 @brief a parse error occurred
8818 @param[in] position the position in the input where the error occurs
8819 @param[in] last_token the last read token
8820 @param[in] ex an exception object describing the error
8821 @return whether parsing should proceed (must return false)
8822 */
8823 virtual bool parse_error(std::size_t position,
8824 const std::string& last_token,
8825 const detail::exception& ex) = 0;
8826
8827 json_sax() = default;
8828 json_sax(const json_sax&) = default;
8829 json_sax(json_sax&&) noexcept = default;
8830 json_sax& operator=(const json_sax&) = default;
8831 json_sax& operator=(json_sax&&) noexcept = default;
8832 virtual ~json_sax() = default;
8833 };
8834
8835 namespace detail
8836 {
8837 constexpr std::size_t unknown_size()
8838 {
8839 return (std::numeric_limits<std::size_t>::max)();
8840 }
8841
8842 /*!
8843 @brief SAX implementation to create a JSON value from SAX events
8844
8845 This class implements the @ref json_sax interface and processes the SAX events
8846 to create a JSON value which makes it basically a DOM parser. The structure or
8847 hierarchy of the JSON value is managed by the stack `ref_stack` which contains
8848 a pointer to the respective array or object for each recursion depth.
8849
8850 After successful parsing, the value that is passed by reference to the
8851 constructor contains the parsed value.
8852
8853 @tparam BasicJsonType the JSON type
8854 */
8855 template<typename BasicJsonType, typename InputAdapterType>
8856 class json_sax_dom_parser
8857 {
8858 public:
8859 using number_integer_t = typename BasicJsonType::number_integer_t;
8860 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
8861 using number_float_t = typename BasicJsonType::number_float_t;
8862 using string_t = typename BasicJsonType::string_t;
8863 using binary_t = typename BasicJsonType::binary_t;
8864 using lexer_t = lexer<BasicJsonType, InputAdapterType>;
8865
8866 /*!
8867 @param[in,out] r reference to a JSON value that is manipulated while
8868 parsing
8869 @param[in] allow_exceptions_ whether parse errors yield exceptions
8870 */
8871 explicit json_sax_dom_parser(BasicJsonType& r, const bool allow_exceptions_ = true, lexer_t* lexer_ = nullptr)
8872 : root(r), allow_exceptions(allow_exceptions_), m_lexer_ref(lexer_)
8873 {}
8874
8875 // make class move-only
8876 json_sax_dom_parser(const json_sax_dom_parser&) = delete;
8877 json_sax_dom_parser(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
8878 json_sax_dom_parser& operator=(const json_sax_dom_parser&) = delete;
8879 json_sax_dom_parser& operator=(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
8880 ~json_sax_dom_parser() = default;
8881
8882 bool null()
8883 {
8884 handle_value(nullptr);
8885 return true;
8886 }
8887
8888 bool boolean(bool val)
8889 {
8890 handle_value(val);
8891 return true;
8892 }
8893
8894 bool number_integer(number_integer_t val)
8895 {
8896 handle_value(val);
8897 return true;
8898 }
8899
8900 bool number_unsigned(number_unsigned_t val)
8901 {
8902 handle_value(val);
8903 return true;
8904 }
8905
8906 bool number_float(number_float_t val, const string_t& /*unused*/)
8907 {
8908 handle_value(val);
8909 return true;
8910 }
8911
8912 bool string(string_t& val)
8913 {
8914 handle_value(val);
8915 return true;
8916 }
8917
8918 bool binary(binary_t& val)
8919 {
8920 handle_value(std::move(val));
8921 return true;
8922 }
8923
8924 bool start_object(std::size_t len)
8925 {
8926 ref_stack.push_back(handle_value(BasicJsonType::value_t::object));
8927
8928 #if JSON_DIAGNOSTIC_POSITIONS
8929 // Manually set the start position of the object here.
8930 // Ensure this is after the call to handle_value to ensure correct start position.
8931 if (m_lexer_ref)
8932 {
8933 // Lexer has read the first character of the object, so
8934 // subtract 1 from the position to get the correct start position.
8935 ref_stack.back()->start_position = m_lexer_ref->get_position() - 1;
8936 }
8937 #endif
8938
8939 if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
8940 {
8941 JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
8942 }
8943
8944 return true;
8945 }
8946
8947 bool key(string_t& val)
8948 {
8949 JSON_ASSERT(!ref_stack.empty());
8950 JSON_ASSERT(ref_stack.back()->is_object());
8951
8952 // add null at given key and store the reference for later
8953 object_element = &(ref_stack.back()->m_data.m_value.object->operator[](val));
8954 return true;
8955 }
8956
8957 bool end_object()
8958 {
8959 JSON_ASSERT(!ref_stack.empty());
8960 JSON_ASSERT(ref_stack.back()->is_object());
8961
8962 #if JSON_DIAGNOSTIC_POSITIONS
8963 if (m_lexer_ref)
8964 {
8965 // Lexer's position is past the closing brace, so set that as the end position.
8966 ref_stack.back()->end_position = m_lexer_ref->get_position();
8967 }
8968 #endif
8969
8970 ref_stack.back()->set_parents();
8971 ref_stack.pop_back();
8972 return true;
8973 }
8974
8975 bool start_array(std::size_t len)
8976 {
8977 ref_stack.push_back(handle_value(BasicJsonType::value_t::array));
8978
8979 #if JSON_DIAGNOSTIC_POSITIONS
8980 // Manually set the start position of the array here.
8981 // Ensure this is after the call to handle_value to ensure correct start position.
8982 if (m_lexer_ref)
8983 {
8984 ref_stack.back()->start_position = m_lexer_ref->get_position() - 1;
8985 }
8986 #endif
8987
8988 if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
8989 {
8990 JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
8991 }
8992
8993 return true;
8994 }
8995
8996 bool end_array()
8997 {
8998 JSON_ASSERT(!ref_stack.empty());
8999 JSON_ASSERT(ref_stack.back()->is_array());
9000
9001 #if JSON_DIAGNOSTIC_POSITIONS
9002 if (m_lexer_ref)
9003 {
9004 // Lexer's position is past the closing bracket, so set that as the end position.
9005 ref_stack.back()->end_position = m_lexer_ref->get_position();
9006 }
9007 #endif
9008
9009 ref_stack.back()->set_parents();
9010 ref_stack.pop_back();
9011 return true;
9012 }
9013
9014 template<class Exception>
9015 bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
9016 const Exception& ex)
9017 {
9018 errored = true;
9019 static_cast<void>(ex);
9020 if (allow_exceptions)
9021 {
9022 JSON_THROW(ex);
9023 }
9024 return false;
9025 }
9026
9027 constexpr bool is_errored() const
9028 {
9029 return errored;
9030 }
9031
9032 private:
9033
9034 #if JSON_DIAGNOSTIC_POSITIONS
9035 void handle_diagnostic_positions_for_json_value(BasicJsonType& v)
9036 {
9037 if (m_lexer_ref)
9038 {
9039 // Lexer has read past the current field value, so set the end position to the current position.
9040 // The start position will be set below based on the length of the string representation
9041 // of the value.
9042 v.end_position = m_lexer_ref->get_position();
9043
9044 switch (v.type())
9045 {
9046 case value_t::boolean:
9047 {
9048 // 4 and 5 are the string length of "true" and "false"
9049 v.start_position = v.end_position - (v.m_data.m_value.boolean ? 4 : 5);
9050 break;
9051 }
9052
9053 case value_t::null:
9054 {
9055 // 4 is the string length of "null"
9056 v.start_position = v.end_position - 4;
9057 break;
9058 }
9059
9060 case value_t::string:
9061 {
9062 // include the length of the quotes, which is 2
9063 v.start_position = v.end_position - v.m_data.m_value.string->size() - 2;
9064 break;
9065 }
9066
9067 // As we handle the start and end positions for values created during parsing,
9068 // we do not expect the following value type to be called. Regardless, set the positions
9069 // in case this is created manually or through a different constructor. Exclude from lcov
9070 // since the exact condition of this switch is esoteric.
9071 // LCOV_EXCL_START
9072 case value_t::discarded:
9073 {
9074 v.end_position = std::string::npos;
9075 v.start_position = v.end_position;
9076 break;
9077 }
9078 // LCOV_EXCL_STOP
9079 case value_t::binary:
9080 case value_t::number_integer:
9081 case value_t::number_unsigned:
9082 case value_t::number_float:
9083 {
9084 v.start_position = v.end_position - m_lexer_ref->get_string().size();
9085 break;
9086 }
9087 case value_t::object:
9088 case value_t::array:
9089 {
9090 // object and array are handled in start_object() and start_array() handlers
9091 // skip setting the values here.
9092 break;
9093 }
9094 default: // LCOV_EXCL_LINE
9095 // Handle all possible types discretely, default handler should never be reached.
9096 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert,-warnings-as-errors) LCOV_EXCL_LINE
9097 }
9098 }
9099 }
9100 #endif
9101
9102 /*!
9103 @invariant If the ref stack is empty, then the passed value will be the new
9104 root.
9105 @invariant If the ref stack contains a value, then it is an array or an
9106 object to which we can add elements
9107 */
9108 template<typename Value>
9109 JSON_HEDLEY_RETURNS_NON_NULL
9110 BasicJsonType* handle_value(Value&& v)
9111 {
9112 if (ref_stack.empty())
9113 {
9114 root = BasicJsonType(std::forward<Value>(v));
9115
9116 #if JSON_DIAGNOSTIC_POSITIONS
9117 handle_diagnostic_positions_for_json_value(root);
9118 #endif
9119
9120 return &root;
9121 }
9122
9123 JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
9124
9125 if (ref_stack.back()->is_array())
9126 {
9127 ref_stack.back()->m_data.m_value.array->emplace_back(std::forward<Value>(v));
9128
9129 #if JSON_DIAGNOSTIC_POSITIONS
9130 handle_diagnostic_positions_for_json_value(ref_stack.back()->m_data.m_value.array->back());
9131 #endif
9132
9133 return &(ref_stack.back()->m_data.m_value.array->back());
9134 }
9135
9136 JSON_ASSERT(ref_stack.back()->is_object());
9137 JSON_ASSERT(object_element);
9138 *object_element = BasicJsonType(std::forward<Value>(v));
9139
9140 #if JSON_DIAGNOSTIC_POSITIONS
9141 handle_diagnostic_positions_for_json_value(*object_element);
9142 #endif
9143
9144 return object_element;
9145 }
9146
9147 /// the parsed JSON value
9148 BasicJsonType& root;
9149 /// stack to model hierarchy of values
9150 std::vector<BasicJsonType*> ref_stack {};
9151 /// helper to hold the reference for the next object element
9152 BasicJsonType* object_element = nullptr;
9153 /// whether a syntax error occurred
9154 bool errored = false;
9155 /// whether to throw exceptions in case of errors
9156 const bool allow_exceptions = true;
9157 /// the lexer reference to obtain the current position
9158 lexer_t* m_lexer_ref = nullptr;
9159 };
9160
9161 template<typename BasicJsonType, typename InputAdapterType>
9162 class json_sax_dom_callback_parser
9163 {
9164 public:
9165 using number_integer_t = typename BasicJsonType::number_integer_t;
9166 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
9167 using number_float_t = typename BasicJsonType::number_float_t;
9168 using string_t = typename BasicJsonType::string_t;
9169 using binary_t = typename BasicJsonType::binary_t;
9170 using parser_callback_t = typename BasicJsonType::parser_callback_t;
9171 using parse_event_t = typename BasicJsonType::parse_event_t;
9172 using lexer_t = lexer<BasicJsonType, InputAdapterType>;
9173
9174 json_sax_dom_callback_parser(BasicJsonType& r,
9175 parser_callback_t cb,
9176 const bool allow_exceptions_ = true,
9177 lexer_t* lexer_ = nullptr)
9178 : root(r), callback(std::move(cb)), allow_exceptions(allow_exceptions_), m_lexer_ref(lexer_)
9179 {
9180 keep_stack.push_back(true);
9181 }
9182
9183 // make class move-only
9184 json_sax_dom_callback_parser(const json_sax_dom_callback_parser&) = delete;
9185 json_sax_dom_callback_parser(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
9186 json_sax_dom_callback_parser& operator=(const json_sax_dom_callback_parser&) = delete;
9187 json_sax_dom_callback_parser& operator=(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
9188 ~json_sax_dom_callback_parser() = default;
9189
9190 bool null()
9191 {
9192 handle_value(nullptr);
9193 return true;
9194 }
9195
9196 bool boolean(bool val)
9197 {
9198 handle_value(val);
9199 return true;
9200 }
9201
9202 bool number_integer(number_integer_t val)
9203 {
9204 handle_value(val);
9205 return true;
9206 }
9207
9208 bool number_unsigned(number_unsigned_t val)
9209 {
9210 handle_value(val);
9211 return true;
9212 }
9213
9214 bool number_float(number_float_t val, const string_t& /*unused*/)
9215 {
9216 handle_value(val);
9217 return true;
9218 }
9219
9220 bool string(string_t& val)
9221 {
9222 handle_value(val);
9223 return true;
9224 }
9225
9226 bool binary(binary_t& val)
9227 {
9228 handle_value(std::move(val));
9229 return true;
9230 }
9231
9232 bool start_object(std::size_t len)
9233 {
9234 // check callback for object start
9235 const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::object_start, discarded);
9236 keep_stack.push_back(keep);
9237
9238 auto val = handle_value(BasicJsonType::value_t::object, true);
9239 ref_stack.push_back(val.second);
9240
9241 if (ref_stack.back())
9242 {
9243
9244 #if JSON_DIAGNOSTIC_POSITIONS
9245 // Manually set the start position of the object here.
9246 // Ensure this is after the call to handle_value to ensure correct start position.
9247 if (m_lexer_ref)
9248 {
9249 // Lexer has read the first character of the object, so
9250 // subtract 1 from the position to get the correct start position.
9251 ref_stack.back()->start_position = m_lexer_ref->get_position() - 1;
9252 }
9253 #endif
9254
9255 // check object limit
9256 if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
9257 {
9258 JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
9259 }
9260 }
9261 return true;
9262 }
9263
9264 bool key(string_t& val)
9265 {
9266 BasicJsonType k = BasicJsonType(val);
9267
9268 // check callback for key
9269 const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::key, k);
9270 key_keep_stack.push_back(keep);
9271
9272 // add discarded value at given key and store the reference for later
9273 if (keep && ref_stack.back())
9274 {
9275 object_element = &(ref_stack.back()->m_data.m_value.object->operator[](val) = discarded);
9276 }
9277
9278 return true;
9279 }
9280
9281 bool end_object()
9282 {
9283 if (ref_stack.back())
9284 {
9285 if (!callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back()))
9286 {
9287 // discard object
9288 *ref_stack.back() = discarded;
9289
9290 #if JSON_DIAGNOSTIC_POSITIONS
9291 // Set start/end positions for discarded object.
9292 handle_diagnostic_positions_for_json_value(*ref_stack.back());
9293 #endif
9294 }
9295 else
9296 {
9297
9298 #if JSON_DIAGNOSTIC_POSITIONS
9299 if (m_lexer_ref)
9300 {
9301 // Lexer's position is past the closing brace, so set that as the end position.
9302 ref_stack.back()->end_position = m_lexer_ref->get_position();
9303 }
9304 #endif
9305
9306 ref_stack.back()->set_parents();
9307 }
9308 }
9309
9310 JSON_ASSERT(!ref_stack.empty());
9311 JSON_ASSERT(!keep_stack.empty());
9312 ref_stack.pop_back();
9313 keep_stack.pop_back();
9314
9315 if (!ref_stack.empty() && ref_stack.back() && ref_stack.back()->is_structured())
9316 {
9317 // remove discarded value
9318 for (auto it = ref_stack.back()->begin(); it != ref_stack.back()->end(); ++it)
9319 {
9320 if (it->is_discarded())
9321 {
9322 ref_stack.back()->erase(it);
9323 break;
9324 }
9325 }
9326 }
9327
9328 return true;
9329 }
9330
9331 bool start_array(std::size_t len)
9332 {
9333 const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::array_start, discarded);
9334 keep_stack.push_back(keep);
9335
9336 auto val = handle_value(BasicJsonType::value_t::array, true);
9337 ref_stack.push_back(val.second);
9338
9339 if (ref_stack.back())
9340 {
9341
9342 #if JSON_DIAGNOSTIC_POSITIONS
9343 // Manually set the start position of the array here.
9344 // Ensure this is after the call to handle_value to ensure correct start position.
9345 if (m_lexer_ref)
9346 {
9347 // Lexer has read the first character of the array, so
9348 // subtract 1 from the position to get the correct start position.
9349 ref_stack.back()->start_position = m_lexer_ref->get_position() - 1;
9350 }
9351 #endif
9352
9353 // check array limit
9354 if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
9355 {
9356 JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
9357 }
9358 }
9359
9360 return true;
9361 }
9362
9363 bool end_array()
9364 {
9365 bool keep = true;
9366
9367 if (ref_stack.back())
9368 {
9369 keep = callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back());
9370 if (keep)
9371 {
9372
9373 #if JSON_DIAGNOSTIC_POSITIONS
9374 if (m_lexer_ref)
9375 {
9376 // Lexer's position is past the closing bracket, so set that as the end position.
9377 ref_stack.back()->end_position = m_lexer_ref->get_position();
9378 }
9379 #endif
9380
9381 ref_stack.back()->set_parents();
9382 }
9383 else
9384 {
9385 // discard array
9386 *ref_stack.back() = discarded;
9387
9388 #if JSON_DIAGNOSTIC_POSITIONS
9389 // Set start/end positions for discarded array.
9390 handle_diagnostic_positions_for_json_value(*ref_stack.back());
9391 #endif
9392 }
9393 }
9394
9395 JSON_ASSERT(!ref_stack.empty());
9396 JSON_ASSERT(!keep_stack.empty());
9397 ref_stack.pop_back();
9398 keep_stack.pop_back();
9399
9400 // remove discarded value
9401 if (!keep && !ref_stack.empty() && ref_stack.back()->is_array())
9402 {
9403 ref_stack.back()->m_data.m_value.array->pop_back();
9404 }
9405
9406 return true;
9407 }
9408
9409 template<class Exception>
9410 bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
9411 const Exception& ex)
9412 {
9413 errored = true;
9414 static_cast<void>(ex);
9415 if (allow_exceptions)
9416 {
9417 JSON_THROW(ex);
9418 }
9419 return false;
9420 }
9421
9422 constexpr bool is_errored() const
9423 {
9424 return errored;
9425 }
9426
9427 private:
9428
9429 #if JSON_DIAGNOSTIC_POSITIONS
9430 void handle_diagnostic_positions_for_json_value(BasicJsonType& v)
9431 {
9432 if (m_lexer_ref)
9433 {
9434 // Lexer has read past the current field value, so set the end position to the current position.
9435 // The start position will be set below based on the length of the string representation
9436 // of the value.
9437 v.end_position = m_lexer_ref->get_position();
9438
9439 switch (v.type())
9440 {
9441 case value_t::boolean:
9442 {
9443 // 4 and 5 are the string length of "true" and "false"
9444 v.start_position = v.end_position - (v.m_data.m_value.boolean ? 4 : 5);
9445 break;
9446 }
9447
9448 case value_t::null:
9449 {
9450 // 4 is the string length of "null"
9451 v.start_position = v.end_position - 4;
9452 break;
9453 }
9454
9455 case value_t::string:
9456 {
9457 // include the length of the quotes, which is 2
9458 v.start_position = v.end_position - v.m_data.m_value.string->size() - 2;
9459 break;
9460 }
9461
9462 case value_t::discarded:
9463 {
9464 v.end_position = std::string::npos;
9465 v.start_position = v.end_position;
9466 break;
9467 }
9468
9469 case value_t::binary:
9470 case value_t::number_integer:
9471 case value_t::number_unsigned:
9472 case value_t::number_float:
9473 {
9474 v.start_position = v.end_position - m_lexer_ref->get_string().size();
9475 break;
9476 }
9477
9478 case value_t::object:
9479 case value_t::array:
9480 {
9481 // object and array are handled in start_object() and start_array() handlers
9482 // skip setting the values here.
9483 break;
9484 }
9485 default: // LCOV_EXCL_LINE
9486 // Handle all possible types discretely, default handler should never be reached.
9487 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert,-warnings-as-errors) LCOV_EXCL_LINE
9488 }
9489 }
9490 }
9491 #endif
9492
9493 /*!
9494 @param[in] v value to add to the JSON value we build during parsing
9495 @param[in] skip_callback whether we should skip calling the callback
9496 function; this is required after start_array() and
9497 start_object() SAX events, because otherwise we would call the
9498 callback function with an empty array or object, respectively.
9499
9500 @invariant If the ref stack is empty, then the passed value will be the new
9501 root.
9502 @invariant If the ref stack contains a value, then it is an array or an
9503 object to which we can add elements
9504
9505 @return pair of boolean (whether value should be kept) and pointer (to the
9506 passed value in the ref_stack hierarchy; nullptr if not kept)
9507 */
9508 template<typename Value>
9509 std::pair<bool, BasicJsonType*> handle_value(Value&& v, const bool skip_callback = false)
9510 {
9511 JSON_ASSERT(!keep_stack.empty());
9512
9513 // do not handle this value if we know it would be added to a discarded
9514 // container
9515 if (!keep_stack.back())
9516 {
9517 return {false, nullptr};
9518 }
9519
9520 // create value
9521 auto value = BasicJsonType(std::forward<Value>(v));
9522
9523 #if JSON_DIAGNOSTIC_POSITIONS
9524 handle_diagnostic_positions_for_json_value(value);
9525 #endif
9526
9527 // check callback
9528 const bool keep = skip_callback || callback(static_cast<int>(ref_stack.size()), parse_event_t::value, value);
9529
9530 // do not handle this value if we just learnt it shall be discarded
9531 if (!keep)
9532 {
9533 return {false, nullptr};
9534 }
9535
9536 if (ref_stack.empty())
9537 {
9538 root = std::move(value);
9539 return {true, & root};
9540 }
9541
9542 // skip this value if we already decided to skip the parent
9543 // (https://github.com/nlohmann/json/issues/971#issuecomment-413678360)
9544 if (!ref_stack.back())
9545 {
9546 return {false, nullptr};
9547 }
9548
9549 // we now only expect arrays and objects
9550 JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
9551
9552 // array
9553 if (ref_stack.back()->is_array())
9554 {
9555 ref_stack.back()->m_data.m_value.array->emplace_back(std::move(value));
9556 return {true, & (ref_stack.back()->m_data.m_value.array->back())};
9557 }
9558
9559 // object
9560 JSON_ASSERT(ref_stack.back()->is_object());
9561 // check if we should store an element for the current key
9562 JSON_ASSERT(!key_keep_stack.empty());
9563 const bool store_element = key_keep_stack.back();
9564 key_keep_stack.pop_back();
9565
9566 if (!store_element)
9567 {
9568 return {false, nullptr};
9569 }
9570
9571 JSON_ASSERT(object_element);
9572 *object_element = std::move(value);
9573 return {true, object_element};
9574 }
9575
9576 /// the parsed JSON value
9577 BasicJsonType& root;
9578 /// stack to model hierarchy of values
9579 std::vector<BasicJsonType*> ref_stack {};
9580 /// stack to manage which values to keep
9581 std::vector<bool> keep_stack {}; // NOLINT(readability-redundant-member-init)
9582 /// stack to manage which object keys to keep
9583 std::vector<bool> key_keep_stack {}; // NOLINT(readability-redundant-member-init)
9584 /// helper to hold the reference for the next object element
9585 BasicJsonType* object_element = nullptr;
9586 /// whether a syntax error occurred
9587 bool errored = false;
9588 /// callback function
9589 const parser_callback_t callback = nullptr;
9590 /// whether to throw exceptions in case of errors
9591 const bool allow_exceptions = true;
9592 /// a discarded value for the callback
9593 BasicJsonType discarded = BasicJsonType::value_t::discarded;
9594 /// the lexer reference to obtain the current position
9595 lexer_t* m_lexer_ref = nullptr;
9596 };
9597
9598 template<typename BasicJsonType>
9599 class json_sax_acceptor
9600 {
9601 public:
9602 using number_integer_t = typename BasicJsonType::number_integer_t;
9603 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
9604 using number_float_t = typename BasicJsonType::number_float_t;
9605 using string_t = typename BasicJsonType::string_t;
9606 using binary_t = typename BasicJsonType::binary_t;
9607
9608 bool null()
9609 {
9610 return true;
9611 }
9612
9613 bool boolean(bool /*unused*/)
9614 {
9615 return true;
9616 }
9617
9618 bool number_integer(number_integer_t /*unused*/)
9619 {
9620 return true;
9621 }
9622
9623 bool number_unsigned(number_unsigned_t /*unused*/)
9624 {
9625 return true;
9626 }
9627
9628 bool number_float(number_float_t /*unused*/, const string_t& /*unused*/)
9629 {
9630 return true;
9631 }
9632
9633 bool string(string_t& /*unused*/)
9634 {
9635 return true;
9636 }
9637
9638 bool binary(binary_t& /*unused*/)
9639 {
9640 return true;
9641 }
9642
9643 bool start_object(std::size_t /*unused*/ = detail::unknown_size())
9644 {
9645 return true;
9646 }
9647
9648 bool key(string_t& /*unused*/)
9649 {
9650 return true;
9651 }
9652
9653 bool end_object()
9654 {
9655 return true;
9656 }
9657
9658 bool start_array(std::size_t /*unused*/ = detail::unknown_size())
9659 {
9660 return true;
9661 }
9662
9663 bool end_array()
9664 {
9665 return true;
9666 }
9667
9668 bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const detail::exception& /*unused*/)
9669 {
9670 return false;
9671 }
9672 };
9673
9674 } // namespace detail
9675 NLOHMANN_JSON_NAMESPACE_END
9676
9677 // #include <nlohmann/detail/input/lexer.hpp>
9678
9679 // #include <nlohmann/detail/macro_scope.hpp>
9680
9681 // #include <nlohmann/detail/meta/is_sax.hpp>
9682 // __ _____ _____ _____
9683 // __| | __| | | | JSON for Modern C++
9684 // | | |__ | | | | | | version 3.12.0
9685 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
9686 //
9687 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
9688 // SPDX-License-Identifier: MIT
9689
9690
9691
9692 #include <cstdint> // size_t
9693 #include <utility> // declval
9694 #include <string> // string
9695
9696 // #include <nlohmann/detail/abi_macros.hpp>
9697
9698 // #include <nlohmann/detail/meta/detected.hpp>
9699
9700 // #include <nlohmann/detail/meta/type_traits.hpp>
9701
9702
9703 NLOHMANN_JSON_NAMESPACE_BEGIN
9704 namespace detail
9705 {
9706
9707 template<typename T>
9708 using null_function_t = decltype(std::declval<T&>().null());
9709
9710 template<typename T>
9711 using boolean_function_t =
9712 decltype(std::declval<T&>().boolean(std::declval<bool>()));
9713
9714 template<typename T, typename Integer>
9715 using number_integer_function_t =
9716 decltype(std::declval<T&>().number_integer(std::declval<Integer>()));
9717
9718 template<typename T, typename Unsigned>
9719 using number_unsigned_function_t =
9720 decltype(std::declval<T&>().number_unsigned(std::declval<Unsigned>()));
9721
9722 template<typename T, typename Float, typename String>
9723 using number_float_function_t = decltype(std::declval<T&>().number_float(
9724 std::declval<Float>(), std::declval<const String&>()));
9725
9726 template<typename T, typename String>
9727 using string_function_t =
9728 decltype(std::declval<T&>().string(std::declval<String&>()));
9729
9730 template<typename T, typename Binary>
9731 using binary_function_t =
9732 decltype(std::declval<T&>().binary(std::declval<Binary&>()));
9733
9734 template<typename T>
9735 using start_object_function_t =
9736 decltype(std::declval<T&>().start_object(std::declval<std::size_t>()));
9737
9738 template<typename T, typename String>
9739 using key_function_t =
9740 decltype(std::declval<T&>().key(std::declval<String&>()));
9741
9742 template<typename T>
9743 using end_object_function_t = decltype(std::declval<T&>().end_object());
9744
9745 template<typename T>
9746 using start_array_function_t =
9747 decltype(std::declval<T&>().start_array(std::declval<std::size_t>()));
9748
9749 template<typename T>
9750 using end_array_function_t = decltype(std::declval<T&>().end_array());
9751
9752 template<typename T, typename Exception>
9753 using parse_error_function_t = decltype(std::declval<T&>().parse_error(
9754 std::declval<std::size_t>(), std::declval<const std::string&>(),
9755 std::declval<const Exception&>()));
9756
9757 template<typename SAX, typename BasicJsonType>
9758 struct is_sax
9759 {
9760 private:
9761 static_assert(is_basic_json<BasicJsonType>::value,
9762 "BasicJsonType must be of type basic_json<...>");
9763
9764 using number_integer_t = typename BasicJsonType::number_integer_t;
9765 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
9766 using number_float_t = typename BasicJsonType::number_float_t;
9767 using string_t = typename BasicJsonType::string_t;
9768 using binary_t = typename BasicJsonType::binary_t;
9769 using exception_t = typename BasicJsonType::exception;
9770
9771 public:
9772 static constexpr bool value =
9773 is_detected_exact<bool, null_function_t, SAX>::value &&
9774 is_detected_exact<bool, boolean_function_t, SAX>::value &&
9775 is_detected_exact<bool, number_integer_function_t, SAX, number_integer_t>::value &&
9776 is_detected_exact<bool, number_unsigned_function_t, SAX, number_unsigned_t>::value &&
9777 is_detected_exact<bool, number_float_function_t, SAX, number_float_t, string_t>::value &&
9778 is_detected_exact<bool, string_function_t, SAX, string_t>::value &&
9779 is_detected_exact<bool, binary_function_t, SAX, binary_t>::value &&
9780 is_detected_exact<bool, start_object_function_t, SAX>::value &&
9781 is_detected_exact<bool, key_function_t, SAX, string_t>::value &&
9782 is_detected_exact<bool, end_object_function_t, SAX>::value &&
9783 is_detected_exact<bool, start_array_function_t, SAX>::value &&
9784 is_detected_exact<bool, end_array_function_t, SAX>::value &&
9785 is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value;
9786 };
9787
9788 template<typename SAX, typename BasicJsonType>
9789 struct is_sax_static_asserts
9790 {
9791 private:
9792 static_assert(is_basic_json<BasicJsonType>::value,
9793 "BasicJsonType must be of type basic_json<...>");
9794
9795 using number_integer_t = typename BasicJsonType::number_integer_t;
9796 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
9797 using number_float_t = typename BasicJsonType::number_float_t;
9798 using string_t = typename BasicJsonType::string_t;
9799 using binary_t = typename BasicJsonType::binary_t;
9800 using exception_t = typename BasicJsonType::exception;
9801
9802 public:
9803 static_assert(is_detected_exact<bool, null_function_t, SAX>::value,
9804 "Missing/invalid function: bool null()");
9805 static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
9806 "Missing/invalid function: bool boolean(bool)");
9807 static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
9808 "Missing/invalid function: bool boolean(bool)");
9809 static_assert(
9810 is_detected_exact<bool, number_integer_function_t, SAX,
9811 number_integer_t>::value,
9812 "Missing/invalid function: bool number_integer(number_integer_t)");
9813 static_assert(
9814 is_detected_exact<bool, number_unsigned_function_t, SAX,
9815 number_unsigned_t>::value,
9816 "Missing/invalid function: bool number_unsigned(number_unsigned_t)");
9817 static_assert(is_detected_exact<bool, number_float_function_t, SAX,
9818 number_float_t, string_t>::value,
9819 "Missing/invalid function: bool number_float(number_float_t, const string_t&)");
9820 static_assert(
9821 is_detected_exact<bool, string_function_t, SAX, string_t>::value,
9822 "Missing/invalid function: bool string(string_t&)");
9823 static_assert(
9824 is_detected_exact<bool, binary_function_t, SAX, binary_t>::value,
9825 "Missing/invalid function: bool binary(binary_t&)");
9826 static_assert(is_detected_exact<bool, start_object_function_t, SAX>::value,
9827 "Missing/invalid function: bool start_object(std::size_t)");
9828 static_assert(is_detected_exact<bool, key_function_t, SAX, string_t>::value,
9829 "Missing/invalid function: bool key(string_t&)");
9830 static_assert(is_detected_exact<bool, end_object_function_t, SAX>::value,
9831 "Missing/invalid function: bool end_object()");
9832 static_assert(is_detected_exact<bool, start_array_function_t, SAX>::value,
9833 "Missing/invalid function: bool start_array(std::size_t)");
9834 static_assert(is_detected_exact<bool, end_array_function_t, SAX>::value,
9835 "Missing/invalid function: bool end_array()");
9836 static_assert(
9837 is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value,
9838 "Missing/invalid function: bool parse_error(std::size_t, const "
9839 "std::string&, const exception&)");
9840 };
9841
9842 } // namespace detail
9843 NLOHMANN_JSON_NAMESPACE_END
9844
9845 // #include <nlohmann/detail/meta/type_traits.hpp>
9846
9847 // #include <nlohmann/detail/string_concat.hpp>
9848
9849 // #include <nlohmann/detail/value_t.hpp>
9850
9851
9852 NLOHMANN_JSON_NAMESPACE_BEGIN
9853 namespace detail
9854 {
9855
9856 /// how to treat CBOR tags
9857 enum class cbor_tag_handler_t
9858 {
9859 error, ///< throw a parse_error exception in case of a tag
9860 ignore, ///< ignore tags
9861 store ///< store tags as binary type
9862 };
9863
9864 /*!
9865 @brief determine system byte order
9866
9867 @return true if and only if system's byte order is little endian
9868
9869 @note from https://stackoverflow.com/a/1001328/266378
9870 */
9871 static inline bool little_endianness(int num = 1) noexcept
9872 {
9873 return *reinterpret_cast<char*>(&num) == 1;
9874 }
9875
9876 ///////////////////
9877 // binary reader //
9878 ///////////////////
9879
9880 /*!
9881 @brief deserialization of CBOR, MessagePack, and UBJSON values
9882 */
9883 template<typename BasicJsonType, typename InputAdapterType, typename SAX = json_sax_dom_parser<BasicJsonType, InputAdapterType>>
9884 class binary_reader
9885 {
9886 using number_integer_t = typename BasicJsonType::number_integer_t;
9887 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
9888 using number_float_t = typename BasicJsonType::number_float_t;
9889 using string_t = typename BasicJsonType::string_t;
9890 using binary_t = typename BasicJsonType::binary_t;
9891 using json_sax_t = SAX;
9892 using char_type = typename InputAdapterType::char_type;
9893 using char_int_type = typename char_traits<char_type>::int_type;
9894
9895 public:
9896 /*!
9897 @brief create a binary reader
9898
9899 @param[in] adapter input adapter to read from
9900 */
9901 explicit binary_reader(InputAdapterType&& adapter, const input_format_t format = input_format_t::json) noexcept : ia(std::move(adapter)), input_format(format)
9902 {
9903 (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
9904 }
9905
9906 // make class move-only
9907 binary_reader(const binary_reader&) = delete;
9908 binary_reader(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
9909 binary_reader& operator=(const binary_reader&) = delete;
9910 binary_reader& operator=(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
9911 ~binary_reader() = default;
9912
9913 /*!
9914 @param[in] format the binary format to parse
9915 @param[in] sax_ a SAX event processor
9916 @param[in] strict whether to expect the input to be consumed completed
9917 @param[in] tag_handler how to treat CBOR tags
9918
9919 @return whether parsing was successful
9920 */
9921 JSON_HEDLEY_NON_NULL(3)
9922 bool sax_parse(const input_format_t format,
9923 json_sax_t* sax_,
9924 const bool strict = true,
9925 const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
9926 {
9927 sax = sax_;
9928 bool result = false;
9929
9930 switch (format)
9931 {
9932 case input_format_t::bson:
9933 result = parse_bson_internal();
9934 break;
9935
9936 case input_format_t::cbor:
9937 result = parse_cbor_internal(true, tag_handler);
9938 break;
9939
9940 case input_format_t::msgpack:
9941 result = parse_msgpack_internal();
9942 break;
9943
9944 case input_format_t::ubjson:
9945 case input_format_t::bjdata:
9946 result = parse_ubjson_internal();
9947 break;
9948
9949 case input_format_t::json: // LCOV_EXCL_LINE
9950 default: // LCOV_EXCL_LINE
9951 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
9952 }
9953
9954 // strict mode: next byte must be EOF
9955 if (result && strict)
9956 {
9957 if (input_format == input_format_t::ubjson || input_format == input_format_t::bjdata)
9958 {
9959 get_ignore_noop();
9960 }
9961 else
9962 {
9963 get();
9964 }
9965
9966 if (JSON_HEDLEY_UNLIKELY(current != char_traits<char_type>::eof()))
9967 {
9968 return sax->parse_error(chars_read, get_token_string(), parse_error::create(110, chars_read,
9969 exception_message(input_format, concat("expected end of input; last byte: 0x", get_token_string()), "value"), nullptr));
9970 }
9971 }
9972
9973 return result;
9974 }
9975
9976 private:
9977 //////////
9978 // BSON //
9979 //////////
9980
9981 /*!
9982 @brief Reads in a BSON-object and passes it to the SAX-parser.
9983 @return whether a valid BSON-value was passed to the SAX parser
9984 */
9985 bool parse_bson_internal()
9986 {
9987 std::int32_t document_size{};
9988 get_number<std::int32_t, true>(input_format_t::bson, document_size);
9989
9990 if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
9991 {
9992 return false;
9993 }
9994
9995 if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/false)))
9996 {
9997 return false;
9998 }
9999
10000 return sax->end_object();
10001 }
10002
10003 /*!
10004 @brief Parses a C-style string from the BSON input.
10005 @param[in,out] result A reference to the string variable where the read
10006 string is to be stored.
10007 @return `true` if the \x00-byte indicating the end of the string was
10008 encountered before the EOF; false` indicates an unexpected EOF.
10009 */
10010 bool get_bson_cstr(string_t& result)
10011 {
10012 auto out = std::back_inserter(result);
10013 while (true)
10014 {
10015 get();
10016 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "cstring")))
10017 {
10018 return false;
10019 }
10020 if (current == 0x00)
10021 {
10022 return true;
10023 }
10024 *out++ = static_cast<typename string_t::value_type>(current);
10025 }
10026 }
10027
10028 /*!
10029 @brief Parses a zero-terminated string of length @a len from the BSON
10030 input.
10031 @param[in] len The length (including the zero-byte at the end) of the
10032 string to be read.
10033 @param[in,out] result A reference to the string variable where the read
10034 string is to be stored.
10035 @tparam NumberType The type of the length @a len
10036 @pre len >= 1
10037 @return `true` if the string was successfully parsed
10038 */
10039 template<typename NumberType>
10040 bool get_bson_string(const NumberType len, string_t& result)
10041 {
10042 if (JSON_HEDLEY_UNLIKELY(len < 1))
10043 {
10044 auto last_token = get_token_string();
10045 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
10046 exception_message(input_format_t::bson, concat("string length must be at least 1, is ", std::to_string(len)), "string"), nullptr));
10047 }
10048
10049 return get_string(input_format_t::bson, len - static_cast<NumberType>(1), result) && get() != char_traits<char_type>::eof();
10050 }
10051
10052 /*!
10053 @brief Parses a byte array input of length @a len from the BSON input.
10054 @param[in] len The length of the byte array to be read.
10055 @param[in,out] result A reference to the binary variable where the read
10056 array is to be stored.
10057 @tparam NumberType The type of the length @a len
10058 @pre len >= 0
10059 @return `true` if the byte array was successfully parsed
10060 */
10061 template<typename NumberType>
10062 bool get_bson_binary(const NumberType len, binary_t& result)
10063 {
10064 if (JSON_HEDLEY_UNLIKELY(len < 0))
10065 {
10066 auto last_token = get_token_string();
10067 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
10068 exception_message(input_format_t::bson, concat("byte array length cannot be negative, is ", std::to_string(len)), "binary"), nullptr));
10069 }
10070
10071 // All BSON binary values have a subtype
10072 std::uint8_t subtype{};
10073 get_number<std::uint8_t>(input_format_t::bson, subtype);
10074 result.set_subtype(subtype);
10075
10076 return get_binary(input_format_t::bson, len, result);
10077 }
10078
10079 /*!
10080 @brief Read a BSON document element of the given @a element_type.
10081 @param[in] element_type The BSON element type, c.f. http://bsonspec.org/spec.html
10082 @param[in] element_type_parse_position The position in the input stream,
10083 where the `element_type` was read.
10084 @warning Not all BSON element types are supported yet. An unsupported
10085 @a element_type will give rise to a parse_error.114:
10086 Unsupported BSON record type 0x...
10087 @return whether a valid BSON-object/array was passed to the SAX parser
10088 */
10089 bool parse_bson_element_internal(const char_int_type element_type,
10090 const std::size_t element_type_parse_position)
10091 {
10092 switch (element_type)
10093 {
10094 case 0x01: // double
10095 {
10096 double number{};
10097 return get_number<double, true>(input_format_t::bson, number) && sax->number_float(static_cast<number_float_t>(number), "");
10098 }
10099
10100 case 0x02: // string
10101 {
10102 std::int32_t len{};
10103 string_t value;
10104 return get_number<std::int32_t, true>(input_format_t::bson, len) && get_bson_string(len, value) && sax->string(value);
10105 }
10106
10107 case 0x03: // object
10108 {
10109 return parse_bson_internal();
10110 }
10111
10112 case 0x04: // array
10113 {
10114 return parse_bson_array();
10115 }
10116
10117 case 0x05: // binary
10118 {
10119 std::int32_t len{};
10120 binary_t value;
10121 return get_number<std::int32_t, true>(input_format_t::bson, len) && get_bson_binary(len, value) && sax->binary(value);
10122 }
10123
10124 case 0x08: // boolean
10125 {
10126 return sax->boolean(get() != 0);
10127 }
10128
10129 case 0x0A: // null
10130 {
10131 return sax->null();
10132 }
10133
10134 case 0x10: // int32
10135 {
10136 std::int32_t value{};
10137 return get_number<std::int32_t, true>(input_format_t::bson, value) && sax->number_integer(value);
10138 }
10139
10140 case 0x12: // int64
10141 {
10142 std::int64_t value{};
10143 return get_number<std::int64_t, true>(input_format_t::bson, value) && sax->number_integer(value);
10144 }
10145
10146 case 0x11: // uint64
10147 {
10148 std::uint64_t value{};
10149 return get_number<std::uint64_t, true>(input_format_t::bson, value) && sax->number_unsigned(value);
10150 }
10151
10152 default: // anything else not supported (yet)
10153 {
10154 std::array<char, 3> cr{{}};
10155 static_cast<void>((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(element_type))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
10156 const std::string cr_str{cr.data()};
10157 return sax->parse_error(element_type_parse_position, cr_str,
10158 parse_error::create(114, element_type_parse_position, concat("Unsupported BSON record type 0x", cr_str), nullptr));
10159 }
10160 }
10161 }
10162
10163 /*!
10164 @brief Read a BSON element list (as specified in the BSON-spec)
10165
10166 The same binary layout is used for objects and arrays, hence it must be
10167 indicated with the argument @a is_array which one is expected
10168 (true --> array, false --> object).
10169
10170 @param[in] is_array Determines if the element list being read is to be
10171 treated as an object (@a is_array == false), or as an
10172 array (@a is_array == true).
10173 @return whether a valid BSON-object/array was passed to the SAX parser
10174 */
10175 bool parse_bson_element_list(const bool is_array)
10176 {
10177 string_t key;
10178
10179 while (auto element_type = get())
10180 {
10181 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "element list")))
10182 {
10183 return false;
10184 }
10185
10186 const std::size_t element_type_parse_position = chars_read;
10187 if (JSON_HEDLEY_UNLIKELY(!get_bson_cstr(key)))
10188 {
10189 return false;
10190 }
10191
10192 if (!is_array && !sax->key(key))
10193 {
10194 return false;
10195 }
10196
10197 if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_internal(element_type, element_type_parse_position)))
10198 {
10199 return false;
10200 }
10201
10202 // get_bson_cstr only appends
10203 key.clear();
10204 }
10205
10206 return true;
10207 }
10208
10209 /*!
10210 @brief Reads an array from the BSON input and passes it to the SAX-parser.
10211 @return whether a valid BSON-array was passed to the SAX parser
10212 */
10213 bool parse_bson_array()
10214 {
10215 std::int32_t document_size{};
10216 get_number<std::int32_t, true>(input_format_t::bson, document_size);
10217
10218 if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
10219 {
10220 return false;
10221 }
10222
10223 if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/true)))
10224 {
10225 return false;
10226 }
10227
10228 return sax->end_array();
10229 }
10230
10231 //////////
10232 // CBOR //
10233 //////////
10234
10235 /*!
10236 @param[in] get_char whether a new character should be retrieved from the
10237 input (true) or whether the last read character should
10238 be considered instead (false)
10239 @param[in] tag_handler how CBOR tags should be treated
10240
10241 @return whether a valid CBOR value was passed to the SAX parser
10242 */
10243 bool parse_cbor_internal(const bool get_char,
10244 const cbor_tag_handler_t tag_handler)
10245 {
10246 switch (get_char ? get() : current)
10247 {
10248 // EOF
10249 case char_traits<char_type>::eof():
10250 return unexpect_eof(input_format_t::cbor, "value");
10251
10252 // Integer 0x00..0x17 (0..23)
10253 case 0x00:
10254 case 0x01:
10255 case 0x02:
10256 case 0x03:
10257 case 0x04:
10258 case 0x05:
10259 case 0x06:
10260 case 0x07:
10261 case 0x08:
10262 case 0x09:
10263 case 0x0A:
10264 case 0x0B:
10265 case 0x0C:
10266 case 0x0D:
10267 case 0x0E:
10268 case 0x0F:
10269 case 0x10:
10270 case 0x11:
10271 case 0x12:
10272 case 0x13:
10273 case 0x14:
10274 case 0x15:
10275 case 0x16:
10276 case 0x17:
10277 return sax->number_unsigned(static_cast<number_unsigned_t>(current));
10278
10279 case 0x18: // Unsigned integer (one-byte uint8_t follows)
10280 {
10281 std::uint8_t number{};
10282 return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
10283 }
10284
10285 case 0x19: // Unsigned integer (two-byte uint16_t follows)
10286 {
10287 std::uint16_t number{};
10288 return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
10289 }
10290
10291 case 0x1A: // Unsigned integer (four-byte uint32_t follows)
10292 {
10293 std::uint32_t number{};
10294 return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
10295 }
10296
10297 case 0x1B: // Unsigned integer (eight-byte uint64_t follows)
10298 {
10299 std::uint64_t number{};
10300 return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
10301 }
10302
10303 // Negative integer -1-0x00..-1-0x17 (-1..-24)
10304 case 0x20:
10305 case 0x21:
10306 case 0x22:
10307 case 0x23:
10308 case 0x24:
10309 case 0x25:
10310 case 0x26:
10311 case 0x27:
10312 case 0x28:
10313 case 0x29:
10314 case 0x2A:
10315 case 0x2B:
10316 case 0x2C:
10317 case 0x2D:
10318 case 0x2E:
10319 case 0x2F:
10320 case 0x30:
10321 case 0x31:
10322 case 0x32:
10323 case 0x33:
10324 case 0x34:
10325 case 0x35:
10326 case 0x36:
10327 case 0x37:
10328 return sax->number_integer(static_cast<std::int8_t>(0x20 - 1 - current));
10329
10330 case 0x38: // Negative integer (one-byte uint8_t follows)
10331 {
10332 std::uint8_t number{};
10333 return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
10334 }
10335
10336 case 0x39: // Negative integer -1-n (two-byte uint16_t follows)
10337 {
10338 std::uint16_t number{};
10339 return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
10340 }
10341
10342 case 0x3A: // Negative integer -1-n (four-byte uint32_t follows)
10343 {
10344 std::uint32_t number{};
10345 return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
10346 }
10347
10348 case 0x3B: // Negative integer -1-n (eight-byte uint64_t follows)
10349 {
10350 std::uint64_t number{};
10351 return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1)
10352 - static_cast<number_integer_t>(number));
10353 }
10354
10355 // Binary data (0x00..0x17 bytes follow)
10356 case 0x40:
10357 case 0x41:
10358 case 0x42:
10359 case 0x43:
10360 case 0x44:
10361 case 0x45:
10362 case 0x46:
10363 case 0x47:
10364 case 0x48:
10365 case 0x49:
10366 case 0x4A:
10367 case 0x4B:
10368 case 0x4C:
10369 case 0x4D:
10370 case 0x4E:
10371 case 0x4F:
10372 case 0x50:
10373 case 0x51:
10374 case 0x52:
10375 case 0x53:
10376 case 0x54:
10377 case 0x55:
10378 case 0x56:
10379 case 0x57:
10380 case 0x58: // Binary data (one-byte uint8_t for n follows)
10381 case 0x59: // Binary data (two-byte uint16_t for n follow)
10382 case 0x5A: // Binary data (four-byte uint32_t for n follow)
10383 case 0x5B: // Binary data (eight-byte uint64_t for n follow)
10384 case 0x5F: // Binary data (indefinite length)
10385 {
10386 binary_t b;
10387 return get_cbor_binary(b) && sax->binary(b);
10388 }
10389
10390 // UTF-8 string (0x00..0x17 bytes follow)
10391 case 0x60:
10392 case 0x61:
10393 case 0x62:
10394 case 0x63:
10395 case 0x64:
10396 case 0x65:
10397 case 0x66:
10398 case 0x67:
10399 case 0x68:
10400 case 0x69:
10401 case 0x6A:
10402 case 0x6B:
10403 case 0x6C:
10404 case 0x6D:
10405 case 0x6E:
10406 case 0x6F:
10407 case 0x70:
10408 case 0x71:
10409 case 0x72:
10410 case 0x73:
10411 case 0x74:
10412 case 0x75:
10413 case 0x76:
10414 case 0x77:
10415 case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
10416 case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
10417 case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
10418 case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
10419 case 0x7F: // UTF-8 string (indefinite length)
10420 {
10421 string_t s;
10422 return get_cbor_string(s) && sax->string(s);
10423 }
10424
10425 // array (0x00..0x17 data items follow)
10426 case 0x80:
10427 case 0x81:
10428 case 0x82:
10429 case 0x83:
10430 case 0x84:
10431 case 0x85:
10432 case 0x86:
10433 case 0x87:
10434 case 0x88:
10435 case 0x89:
10436 case 0x8A:
10437 case 0x8B:
10438 case 0x8C:
10439 case 0x8D:
10440 case 0x8E:
10441 case 0x8F:
10442 case 0x90:
10443 case 0x91:
10444 case 0x92:
10445 case 0x93:
10446 case 0x94:
10447 case 0x95:
10448 case 0x96:
10449 case 0x97:
10450 return get_cbor_array(
10451 conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
10452
10453 case 0x98: // array (one-byte uint8_t for n follows)
10454 {
10455 std::uint8_t len{};
10456 return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
10457 }
10458
10459 case 0x99: // array (two-byte uint16_t for n follow)
10460 {
10461 std::uint16_t len{};
10462 return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
10463 }
10464
10465 case 0x9A: // array (four-byte uint32_t for n follow)
10466 {
10467 std::uint32_t len{};
10468 return get_number(input_format_t::cbor, len) && get_cbor_array(conditional_static_cast<std::size_t>(len), tag_handler);
10469 }
10470
10471 case 0x9B: // array (eight-byte uint64_t for n follow)
10472 {
10473 std::uint64_t len{};
10474 return get_number(input_format_t::cbor, len) && get_cbor_array(conditional_static_cast<std::size_t>(len), tag_handler);
10475 }
10476
10477 case 0x9F: // array (indefinite length)
10478 return get_cbor_array(detail::unknown_size(), tag_handler);
10479
10480 // map (0x00..0x17 pairs of data items follow)
10481 case 0xA0:
10482 case 0xA1:
10483 case 0xA2:
10484 case 0xA3:
10485 case 0xA4:
10486 case 0xA5:
10487 case 0xA6:
10488 case 0xA7:
10489 case 0xA8:
10490 case 0xA9:
10491 case 0xAA:
10492 case 0xAB:
10493 case 0xAC:
10494 case 0xAD:
10495 case 0xAE:
10496 case 0xAF:
10497 case 0xB0:
10498 case 0xB1:
10499 case 0xB2:
10500 case 0xB3:
10501 case 0xB4:
10502 case 0xB5:
10503 case 0xB6:
10504 case 0xB7:
10505 return get_cbor_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
10506
10507 case 0xB8: // map (one-byte uint8_t for n follows)
10508 {
10509 std::uint8_t len{};
10510 return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
10511 }
10512
10513 case 0xB9: // map (two-byte uint16_t for n follow)
10514 {
10515 std::uint16_t len{};
10516 return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
10517 }
10518
10519 case 0xBA: // map (four-byte uint32_t for n follow)
10520 {
10521 std::uint32_t len{};
10522 return get_number(input_format_t::cbor, len) && get_cbor_object(conditional_static_cast<std::size_t>(len), tag_handler);
10523 }
10524
10525 case 0xBB: // map (eight-byte uint64_t for n follow)
10526 {
10527 std::uint64_t len{};
10528 return get_number(input_format_t::cbor, len) && get_cbor_object(conditional_static_cast<std::size_t>(len), tag_handler);
10529 }
10530
10531 case 0xBF: // map (indefinite length)
10532 return get_cbor_object(detail::unknown_size(), tag_handler);
10533
10534 case 0xC6: // tagged item
10535 case 0xC7:
10536 case 0xC8:
10537 case 0xC9:
10538 case 0xCA:
10539 case 0xCB:
10540 case 0xCC:
10541 case 0xCD:
10542 case 0xCE:
10543 case 0xCF:
10544 case 0xD0:
10545 case 0xD1:
10546 case 0xD2:
10547 case 0xD3:
10548 case 0xD4:
10549 case 0xD8: // tagged item (1 bytes follow)
10550 case 0xD9: // tagged item (2 bytes follow)
10551 case 0xDA: // tagged item (4 bytes follow)
10552 case 0xDB: // tagged item (8 bytes follow)
10553 {
10554 switch (tag_handler)
10555 {
10556 case cbor_tag_handler_t::error:
10557 {
10558 auto last_token = get_token_string();
10559 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
10560 exception_message(input_format_t::cbor, concat("invalid byte: 0x", last_token), "value"), nullptr));
10561 }
10562
10563 case cbor_tag_handler_t::ignore:
10564 {
10565 // ignore binary subtype
10566 switch (current)
10567 {
10568 case 0xD8:
10569 {
10570 std::uint8_t subtype_to_ignore{};
10571 get_number(input_format_t::cbor, subtype_to_ignore);
10572 break;
10573 }
10574 case 0xD9:
10575 {
10576 std::uint16_t subtype_to_ignore{};
10577 get_number(input_format_t::cbor, subtype_to_ignore);
10578 break;
10579 }
10580 case 0xDA:
10581 {
10582 std::uint32_t subtype_to_ignore{};
10583 get_number(input_format_t::cbor, subtype_to_ignore);
10584 break;
10585 }
10586 case 0xDB:
10587 {
10588 std::uint64_t subtype_to_ignore{};
10589 get_number(input_format_t::cbor, subtype_to_ignore);
10590 break;
10591 }
10592 default:
10593 break;
10594 }
10595 return parse_cbor_internal(true, tag_handler);
10596 }
10597
10598 case cbor_tag_handler_t::store:
10599 {
10600 binary_t b;
10601 // use binary subtype and store in binary container
10602 switch (current)
10603 {
10604 case 0xD8:
10605 {
10606 std::uint8_t subtype{};
10607 get_number(input_format_t::cbor, subtype);
10608 b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
10609 break;
10610 }
10611 case 0xD9:
10612 {
10613 std::uint16_t subtype{};
10614 get_number(input_format_t::cbor, subtype);
10615 b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
10616 break;
10617 }
10618 case 0xDA:
10619 {
10620 std::uint32_t subtype{};
10621 get_number(input_format_t::cbor, subtype);
10622 b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
10623 break;
10624 }
10625 case 0xDB:
10626 {
10627 std::uint64_t subtype{};
10628 get_number(input_format_t::cbor, subtype);
10629 b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
10630 break;
10631 }
10632 default:
10633 return parse_cbor_internal(true, tag_handler);
10634 }
10635 get();
10636 return get_cbor_binary(b) && sax->binary(b);
10637 }
10638
10639 default: // LCOV_EXCL_LINE
10640 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
10641 return false; // LCOV_EXCL_LINE
10642 }
10643 }
10644
10645 case 0xF4: // false
10646 return sax->boolean(false);
10647
10648 case 0xF5: // true
10649 return sax->boolean(true);
10650
10651 case 0xF6: // null
10652 return sax->null();
10653
10654 case 0xF9: // Half-Precision Float (two-byte IEEE 754)
10655 {
10656 const auto byte1_raw = get();
10657 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
10658 {
10659 return false;
10660 }
10661 const auto byte2_raw = get();
10662 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
10663 {
10664 return false;
10665 }
10666
10667 const auto byte1 = static_cast<unsigned char>(byte1_raw);
10668 const auto byte2 = static_cast<unsigned char>(byte2_raw);
10669
10670 // code from RFC 7049, Appendix D, Figure 3:
10671 // As half-precision floating-point numbers were only added
10672 // to IEEE 754 in 2008, today's programming platforms often
10673 // still only have limited support for them. It is very
10674 // easy to include at least decoding support for them even
10675 // without such support. An example of a small decoder for
10676 // half-precision floating-point numbers in the C language
10677 // is shown in Fig. 3.
10678 const auto half = static_cast<unsigned int>((byte1 << 8u) + byte2);
10679 const double val = [&half]
10680 {
10681 const int exp = (half >> 10u) & 0x1Fu;
10682 const unsigned int mant = half & 0x3FFu;
10683 JSON_ASSERT(0 <= exp&& exp <= 32);
10684 JSON_ASSERT(mant <= 1024);
10685 switch (exp)
10686 {
10687 case 0:
10688 return std::ldexp(mant, -24);
10689 case 31:
10690 return (mant == 0)
10691 ? std::numeric_limits<double>::infinity()
10692 : std::numeric_limits<double>::quiet_NaN();
10693 default:
10694 return std::ldexp(mant + 1024, exp - 25);
10695 }
10696 }();
10697 return sax->number_float((half & 0x8000u) != 0
10698 ? static_cast<number_float_t>(-val)
10699 : static_cast<number_float_t>(val), "");
10700 }
10701
10702 case 0xFA: // Single-Precision Float (four-byte IEEE 754)
10703 {
10704 float number{};
10705 return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
10706 }
10707
10708 case 0xFB: // Double-Precision Float (eight-byte IEEE 754)
10709 {
10710 double number{};
10711 return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
10712 }
10713
10714 default: // anything else (0xFF is handled inside the other types)
10715 {
10716 auto last_token = get_token_string();
10717 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
10718 exception_message(input_format_t::cbor, concat("invalid byte: 0x", last_token), "value"), nullptr));
10719 }
10720 }
10721 }
10722
10723 /*!
10724 @brief reads a CBOR string
10725
10726 This function first reads starting bytes to determine the expected
10727 string length and then copies this number of bytes into a string.
10728 Additionally, CBOR's strings with indefinite lengths are supported.
10729
10730 @param[out] result created string
10731
10732 @return whether string creation completed
10733 */
10734 bool get_cbor_string(string_t& result)
10735 {
10736 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "string")))
10737 {
10738 return false;
10739 }
10740
10741 switch (current)
10742 {
10743 // UTF-8 string (0x00..0x17 bytes follow)
10744 case 0x60:
10745 case 0x61:
10746 case 0x62:
10747 case 0x63:
10748 case 0x64:
10749 case 0x65:
10750 case 0x66:
10751 case 0x67:
10752 case 0x68:
10753 case 0x69:
10754 case 0x6A:
10755 case 0x6B:
10756 case 0x6C:
10757 case 0x6D:
10758 case 0x6E:
10759 case 0x6F:
10760 case 0x70:
10761 case 0x71:
10762 case 0x72:
10763 case 0x73:
10764 case 0x74:
10765 case 0x75:
10766 case 0x76:
10767 case 0x77:
10768 {
10769 return get_string(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
10770 }
10771
10772 case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
10773 {
10774 std::uint8_t len{};
10775 return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
10776 }
10777
10778 case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
10779 {
10780 std::uint16_t len{};
10781 return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
10782 }
10783
10784 case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
10785 {
10786 std::uint32_t len{};
10787 return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
10788 }
10789
10790 case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
10791 {
10792 std::uint64_t len{};
10793 return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
10794 }
10795
10796 case 0x7F: // UTF-8 string (indefinite length)
10797 {
10798 while (get() != 0xFF)
10799 {
10800 string_t chunk;
10801 if (!get_cbor_string(chunk))
10802 {
10803 return false;
10804 }
10805 result.append(chunk);
10806 }
10807 return true;
10808 }
10809
10810 default:
10811 {
10812 auto last_token = get_token_string();
10813 return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
10814 exception_message(input_format_t::cbor, concat("expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x", last_token), "string"), nullptr));
10815 }
10816 }
10817 }
10818
10819 /*!
10820 @brief reads a CBOR byte array
10821
10822 This function first reads starting bytes to determine the expected
10823 byte array length and then copies this number of bytes into the byte array.
10824 Additionally, CBOR's byte arrays with indefinite lengths are supported.
10825
10826 @param[out] result created byte array
10827
10828 @return whether byte array creation completed
10829 */
10830 bool get_cbor_binary(binary_t& result)
10831 {
10832 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "binary")))
10833 {
10834 return false;
10835 }
10836
10837 switch (current)
10838 {
10839 // Binary data (0x00..0x17 bytes follow)
10840 case 0x40:
10841 case 0x41:
10842 case 0x42:
10843 case 0x43:
10844 case 0x44:
10845 case 0x45:
10846 case 0x46:
10847 case 0x47:
10848 case 0x48:
10849 case 0x49:
10850 case 0x4A:
10851 case 0x4B:
10852 case 0x4C:
10853 case 0x4D:
10854 case 0x4E:
10855 case 0x4F:
10856 case 0x50:
10857 case 0x51:
10858 case 0x52:
10859 case 0x53:
10860 case 0x54:
10861 case 0x55:
10862 case 0x56:
10863 case 0x57:
10864 {
10865 return get_binary(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
10866 }
10867
10868 case 0x58: // Binary data (one-byte uint8_t for n follows)
10869 {
10870 std::uint8_t len{};
10871 return get_number(input_format_t::cbor, len) &&
10872 get_binary(input_format_t::cbor, len, result);
10873 }
10874
10875 case 0x59: // Binary data (two-byte uint16_t for n follow)
10876 {
10877 std::uint16_t len{};
10878 return get_number(input_format_t::cbor, len) &&
10879 get_binary(input_format_t::cbor, len, result);
10880 }
10881
10882 case 0x5A: // Binary data (four-byte uint32_t for n follow)
10883 {
10884 std::uint32_t len{};
10885 return get_number(input_format_t::cbor, len) &&
10886 get_binary(input_format_t::cbor, len, result);
10887 }
10888
10889 case 0x5B: // Binary data (eight-byte uint64_t for n follow)
10890 {
10891 std::uint64_t len{};
10892 return get_number(input_format_t::cbor, len) &&
10893 get_binary(input_format_t::cbor, len, result);
10894 }
10895
10896 case 0x5F: // Binary data (indefinite length)
10897 {
10898 while (get() != 0xFF)
10899 {
10900 binary_t chunk;
10901 if (!get_cbor_binary(chunk))
10902 {
10903 return false;
10904 }
10905 result.insert(result.end(), chunk.begin(), chunk.end());
10906 }
10907 return true;
10908 }
10909
10910 default:
10911 {
10912 auto last_token = get_token_string();
10913 return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
10914 exception_message(input_format_t::cbor, concat("expected length specification (0x40-0x5B) or indefinite binary array type (0x5F); last byte: 0x", last_token), "binary"), nullptr));
10915 }
10916 }
10917 }
10918
10919 /*!
10920 @param[in] len the length of the array or detail::unknown_size() for an
10921 array of indefinite size
10922 @param[in] tag_handler how CBOR tags should be treated
10923 @return whether array creation completed
10924 */
10925 bool get_cbor_array(const std::size_t len,
10926 const cbor_tag_handler_t tag_handler)
10927 {
10928 if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
10929 {
10930 return false;
10931 }
10932
10933 if (len != detail::unknown_size())
10934 {
10935 for (std::size_t i = 0; i < len; ++i)
10936 {
10937 if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
10938 {
10939 return false;
10940 }
10941 }
10942 }
10943 else
10944 {
10945 while (get() != 0xFF)
10946 {
10947 if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(false, tag_handler)))
10948 {
10949 return false;
10950 }
10951 }
10952 }
10953
10954 return sax->end_array();
10955 }
10956
10957 /*!
10958 @param[in] len the length of the object or detail::unknown_size() for an
10959 object of indefinite size
10960 @param[in] tag_handler how CBOR tags should be treated
10961 @return whether object creation completed
10962 */
10963 bool get_cbor_object(const std::size_t len,
10964 const cbor_tag_handler_t tag_handler)
10965 {
10966 if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
10967 {
10968 return false;
10969 }
10970
10971 if (len != 0)
10972 {
10973 string_t key;
10974 if (len != detail::unknown_size())
10975 {
10976 for (std::size_t i = 0; i < len; ++i)
10977 {
10978 get();
10979 if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
10980 {
10981 return false;
10982 }
10983
10984 if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
10985 {
10986 return false;
10987 }
10988 key.clear();
10989 }
10990 }
10991 else
10992 {
10993 while (get() != 0xFF)
10994 {
10995 if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
10996 {
10997 return false;
10998 }
10999
11000 if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
11001 {
11002 return false;
11003 }
11004 key.clear();
11005 }
11006 }
11007 }
11008
11009 return sax->end_object();
11010 }
11011
11012 /////////////
11013 // MsgPack //
11014 /////////////
11015
11016 /*!
11017 @return whether a valid MessagePack value was passed to the SAX parser
11018 */
11019 bool parse_msgpack_internal()
11020 {
11021 switch (get())
11022 {
11023 // EOF
11024 case char_traits<char_type>::eof():
11025 return unexpect_eof(input_format_t::msgpack, "value");
11026
11027 // positive fixint
11028 case 0x00:
11029 case 0x01:
11030 case 0x02:
11031 case 0x03:
11032 case 0x04:
11033 case 0x05:
11034 case 0x06:
11035 case 0x07:
11036 case 0x08:
11037 case 0x09:
11038 case 0x0A:
11039 case 0x0B:
11040 case 0x0C:
11041 case 0x0D:
11042 case 0x0E:
11043 case 0x0F:
11044 case 0x10:
11045 case 0x11:
11046 case 0x12:
11047 case 0x13:
11048 case 0x14:
11049 case 0x15:
11050 case 0x16:
11051 case 0x17:
11052 case 0x18:
11053 case 0x19:
11054 case 0x1A:
11055 case 0x1B:
11056 case 0x1C:
11057 case 0x1D:
11058 case 0x1E:
11059 case 0x1F:
11060 case 0x20:
11061 case 0x21:
11062 case 0x22:
11063 case 0x23:
11064 case 0x24:
11065 case 0x25:
11066 case 0x26:
11067 case 0x27:
11068 case 0x28:
11069 case 0x29:
11070 case 0x2A:
11071 case 0x2B:
11072 case 0x2C:
11073 case 0x2D:
11074 case 0x2E:
11075 case 0x2F:
11076 case 0x30:
11077 case 0x31:
11078 case 0x32:
11079 case 0x33:
11080 case 0x34:
11081 case 0x35:
11082 case 0x36:
11083 case 0x37:
11084 case 0x38:
11085 case 0x39:
11086 case 0x3A:
11087 case 0x3B:
11088 case 0x3C:
11089 case 0x3D:
11090 case 0x3E:
11091 case 0x3F:
11092 case 0x40:
11093 case 0x41:
11094 case 0x42:
11095 case 0x43:
11096 case 0x44:
11097 case 0x45:
11098 case 0x46:
11099 case 0x47:
11100 case 0x48:
11101 case 0x49:
11102 case 0x4A:
11103 case 0x4B:
11104 case 0x4C:
11105 case 0x4D:
11106 case 0x4E:
11107 case 0x4F:
11108 case 0x50:
11109 case 0x51:
11110 case 0x52:
11111 case 0x53:
11112 case 0x54:
11113 case 0x55:
11114 case 0x56:
11115 case 0x57:
11116 case 0x58:
11117 case 0x59:
11118 case 0x5A:
11119 case 0x5B:
11120 case 0x5C:
11121 case 0x5D:
11122 case 0x5E:
11123 case 0x5F:
11124 case 0x60:
11125 case 0x61:
11126 case 0x62:
11127 case 0x63:
11128 case 0x64:
11129 case 0x65:
11130 case 0x66:
11131 case 0x67:
11132 case 0x68:
11133 case 0x69:
11134 case 0x6A:
11135 case 0x6B:
11136 case 0x6C:
11137 case 0x6D:
11138 case 0x6E:
11139 case 0x6F:
11140 case 0x70:
11141 case 0x71:
11142 case 0x72:
11143 case 0x73:
11144 case 0x74:
11145 case 0x75:
11146 case 0x76:
11147 case 0x77:
11148 case 0x78:
11149 case 0x79:
11150 case 0x7A:
11151 case 0x7B:
11152 case 0x7C:
11153 case 0x7D:
11154 case 0x7E:
11155 case 0x7F:
11156 return sax->number_unsigned(static_cast<number_unsigned_t>(current));
11157
11158 // fixmap
11159 case 0x80:
11160 case 0x81:
11161 case 0x82:
11162 case 0x83:
11163 case 0x84:
11164 case 0x85:
11165 case 0x86:
11166 case 0x87:
11167 case 0x88:
11168 case 0x89:
11169 case 0x8A:
11170 case 0x8B:
11171 case 0x8C:
11172 case 0x8D:
11173 case 0x8E:
11174 case 0x8F:
11175 return get_msgpack_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
11176
11177 // fixarray
11178 case 0x90:
11179 case 0x91:
11180 case 0x92:
11181 case 0x93:
11182 case 0x94:
11183 case 0x95:
11184 case 0x96:
11185 case 0x97:
11186 case 0x98:
11187 case 0x99:
11188 case 0x9A:
11189 case 0x9B:
11190 case 0x9C:
11191 case 0x9D:
11192 case 0x9E:
11193 case 0x9F:
11194 return get_msgpack_array(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
11195
11196 // fixstr
11197 case 0xA0:
11198 case 0xA1:
11199 case 0xA2:
11200 case 0xA3:
11201 case 0xA4:
11202 case 0xA5:
11203 case 0xA6:
11204 case 0xA7:
11205 case 0xA8:
11206 case 0xA9:
11207 case 0xAA:
11208 case 0xAB:
11209 case 0xAC:
11210 case 0xAD:
11211 case 0xAE:
11212 case 0xAF:
11213 case 0xB0:
11214 case 0xB1:
11215 case 0xB2:
11216 case 0xB3:
11217 case 0xB4:
11218 case 0xB5:
11219 case 0xB6:
11220 case 0xB7:
11221 case 0xB8:
11222 case 0xB9:
11223 case 0xBA:
11224 case 0xBB:
11225 case 0xBC:
11226 case 0xBD:
11227 case 0xBE:
11228 case 0xBF:
11229 case 0xD9: // str 8
11230 case 0xDA: // str 16
11231 case 0xDB: // str 32
11232 {
11233 string_t s;
11234 return get_msgpack_string(s) && sax->string(s);
11235 }
11236
11237 case 0xC0: // nil
11238 return sax->null();
11239
11240 case 0xC2: // false
11241 return sax->boolean(false);
11242
11243 case 0xC3: // true
11244 return sax->boolean(true);
11245
11246 case 0xC4: // bin 8
11247 case 0xC5: // bin 16
11248 case 0xC6: // bin 32
11249 case 0xC7: // ext 8
11250 case 0xC8: // ext 16
11251 case 0xC9: // ext 32
11252 case 0xD4: // fixext 1
11253 case 0xD5: // fixext 2
11254 case 0xD6: // fixext 4
11255 case 0xD7: // fixext 8
11256 case 0xD8: // fixext 16
11257 {
11258 binary_t b;
11259 return get_msgpack_binary(b) && sax->binary(b);
11260 }
11261
11262 case 0xCA: // float 32
11263 {
11264 float number{};
11265 return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast<number_float_t>(number), "");
11266 }
11267
11268 case 0xCB: // float 64
11269 {
11270 double number{};
11271 return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast<number_float_t>(number), "");
11272 }
11273
11274 case 0xCC: // uint 8
11275 {
11276 std::uint8_t number{};
11277 return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
11278 }
11279
11280 case 0xCD: // uint 16
11281 {
11282 std::uint16_t number{};
11283 return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
11284 }
11285
11286 case 0xCE: // uint 32
11287 {
11288 std::uint32_t number{};
11289 return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
11290 }
11291
11292 case 0xCF: // uint 64
11293 {
11294 std::uint64_t number{};
11295 return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
11296 }
11297
11298 case 0xD0: // int 8
11299 {
11300 std::int8_t number{};
11301 return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
11302 }
11303
11304 case 0xD1: // int 16
11305 {
11306 std::int16_t number{};
11307 return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
11308 }
11309
11310 case 0xD2: // int 32
11311 {
11312 std::int32_t number{};
11313 return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
11314 }
11315
11316 case 0xD3: // int 64
11317 {
11318 std::int64_t number{};
11319 return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
11320 }
11321
11322 case 0xDC: // array 16
11323 {
11324 std::uint16_t len{};
11325 return get_number(input_format_t::msgpack, len) && get_msgpack_array(static_cast<std::size_t>(len));
11326 }
11327
11328 case 0xDD: // array 32
11329 {
11330 std::uint32_t len{};
11331 return get_number(input_format_t::msgpack, len) && get_msgpack_array(conditional_static_cast<std::size_t>(len));
11332 }
11333
11334 case 0xDE: // map 16
11335 {
11336 std::uint16_t len{};
11337 return get_number(input_format_t::msgpack, len) && get_msgpack_object(static_cast<std::size_t>(len));
11338 }
11339
11340 case 0xDF: // map 32
11341 {
11342 std::uint32_t len{};
11343 return get_number(input_format_t::msgpack, len) && get_msgpack_object(conditional_static_cast<std::size_t>(len));
11344 }
11345
11346 // negative fixint
11347 case 0xE0:
11348 case 0xE1:
11349 case 0xE2:
11350 case 0xE3:
11351 case 0xE4:
11352 case 0xE5:
11353 case 0xE6:
11354 case 0xE7:
11355 case 0xE8:
11356 case 0xE9:
11357 case 0xEA:
11358 case 0xEB:
11359 case 0xEC:
11360 case 0xED:
11361 case 0xEE:
11362 case 0xEF:
11363 case 0xF0:
11364 case 0xF1:
11365 case 0xF2:
11366 case 0xF3:
11367 case 0xF4:
11368 case 0xF5:
11369 case 0xF6:
11370 case 0xF7:
11371 case 0xF8:
11372 case 0xF9:
11373 case 0xFA:
11374 case 0xFB:
11375 case 0xFC:
11376 case 0xFD:
11377 case 0xFE:
11378 case 0xFF:
11379 return sax->number_integer(static_cast<std::int8_t>(current));
11380
11381 default: // anything else
11382 {
11383 auto last_token = get_token_string();
11384 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
11385 exception_message(input_format_t::msgpack, concat("invalid byte: 0x", last_token), "value"), nullptr));
11386 }
11387 }
11388 }
11389
11390 /*!
11391 @brief reads a MessagePack string
11392
11393 This function first reads starting bytes to determine the expected
11394 string length and then copies this number of bytes into a string.
11395
11396 @param[out] result created string
11397
11398 @return whether string creation completed
11399 */
11400 bool get_msgpack_string(string_t& result)
11401 {
11402 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::msgpack, "string")))
11403 {
11404 return false;
11405 }
11406
11407 switch (current)
11408 {
11409 // fixstr
11410 case 0xA0:
11411 case 0xA1:
11412 case 0xA2:
11413 case 0xA3:
11414 case 0xA4:
11415 case 0xA5:
11416 case 0xA6:
11417 case 0xA7:
11418 case 0xA8:
11419 case 0xA9:
11420 case 0xAA:
11421 case 0xAB:
11422 case 0xAC:
11423 case 0xAD:
11424 case 0xAE:
11425 case 0xAF:
11426 case 0xB0:
11427 case 0xB1:
11428 case 0xB2:
11429 case 0xB3:
11430 case 0xB4:
11431 case 0xB5:
11432 case 0xB6:
11433 case 0xB7:
11434 case 0xB8:
11435 case 0xB9:
11436 case 0xBA:
11437 case 0xBB:
11438 case 0xBC:
11439 case 0xBD:
11440 case 0xBE:
11441 case 0xBF:
11442 {
11443 return get_string(input_format_t::msgpack, static_cast<unsigned int>(current) & 0x1Fu, result);
11444 }
11445
11446 case 0xD9: // str 8
11447 {
11448 std::uint8_t len{};
11449 return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
11450 }
11451
11452 case 0xDA: // str 16
11453 {
11454 std::uint16_t len{};
11455 return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
11456 }
11457
11458 case 0xDB: // str 32
11459 {
11460 std::uint32_t len{};
11461 return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
11462 }
11463
11464 default:
11465 {
11466 auto last_token = get_token_string();
11467 return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
11468 exception_message(input_format_t::msgpack, concat("expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x", last_token), "string"), nullptr));
11469 }
11470 }
11471 }
11472
11473 /*!
11474 @brief reads a MessagePack byte array
11475
11476 This function first reads starting bytes to determine the expected
11477 byte array length and then copies this number of bytes into a byte array.
11478
11479 @param[out] result created byte array
11480
11481 @return whether byte array creation completed
11482 */
11483 bool get_msgpack_binary(binary_t& result)
11484 {
11485 // helper function to set the subtype
11486 auto assign_and_return_true = [&result](std::int8_t subtype)
11487 {
11488 result.set_subtype(static_cast<std::uint8_t>(subtype));
11489 return true;
11490 };
11491
11492 switch (current)
11493 {
11494 case 0xC4: // bin 8
11495 {
11496 std::uint8_t len{};
11497 return get_number(input_format_t::msgpack, len) &&
11498 get_binary(input_format_t::msgpack, len, result);
11499 }
11500
11501 case 0xC5: // bin 16
11502 {
11503 std::uint16_t len{};
11504 return get_number(input_format_t::msgpack, len) &&
11505 get_binary(input_format_t::msgpack, len, result);
11506 }
11507
11508 case 0xC6: // bin 32
11509 {
11510 std::uint32_t len{};
11511 return get_number(input_format_t::msgpack, len) &&
11512 get_binary(input_format_t::msgpack, len, result);
11513 }
11514
11515 case 0xC7: // ext 8
11516 {
11517 std::uint8_t len{};
11518 std::int8_t subtype{};
11519 return get_number(input_format_t::msgpack, len) &&
11520 get_number(input_format_t::msgpack, subtype) &&
11521 get_binary(input_format_t::msgpack, len, result) &&
11522 assign_and_return_true(subtype);
11523 }
11524
11525 case 0xC8: // ext 16
11526 {
11527 std::uint16_t len{};
11528 std::int8_t subtype{};
11529 return get_number(input_format_t::msgpack, len) &&
11530 get_number(input_format_t::msgpack, subtype) &&
11531 get_binary(input_format_t::msgpack, len, result) &&
11532 assign_and_return_true(subtype);
11533 }
11534
11535 case 0xC9: // ext 32
11536 {
11537 std::uint32_t len{};
11538 std::int8_t subtype{};
11539 return get_number(input_format_t::msgpack, len) &&
11540 get_number(input_format_t::msgpack, subtype) &&
11541 get_binary(input_format_t::msgpack, len, result) &&
11542 assign_and_return_true(subtype);
11543 }
11544
11545 case 0xD4: // fixext 1
11546 {
11547 std::int8_t subtype{};
11548 return get_number(input_format_t::msgpack, subtype) &&
11549 get_binary(input_format_t::msgpack, 1, result) &&
11550 assign_and_return_true(subtype);
11551 }
11552
11553 case 0xD5: // fixext 2
11554 {
11555 std::int8_t subtype{};
11556 return get_number(input_format_t::msgpack, subtype) &&
11557 get_binary(input_format_t::msgpack, 2, result) &&
11558 assign_and_return_true(subtype);
11559 }
11560
11561 case 0xD6: // fixext 4
11562 {
11563 std::int8_t subtype{};
11564 return get_number(input_format_t::msgpack, subtype) &&
11565 get_binary(input_format_t::msgpack, 4, result) &&
11566 assign_and_return_true(subtype);
11567 }
11568
11569 case 0xD7: // fixext 8
11570 {
11571 std::int8_t subtype{};
11572 return get_number(input_format_t::msgpack, subtype) &&
11573 get_binary(input_format_t::msgpack, 8, result) &&
11574 assign_and_return_true(subtype);
11575 }
11576
11577 case 0xD8: // fixext 16
11578 {
11579 std::int8_t subtype{};
11580 return get_number(input_format_t::msgpack, subtype) &&
11581 get_binary(input_format_t::msgpack, 16, result) &&
11582 assign_and_return_true(subtype);
11583 }
11584
11585 default: // LCOV_EXCL_LINE
11586 return false; // LCOV_EXCL_LINE
11587 }
11588 }
11589
11590 /*!
11591 @param[in] len the length of the array
11592 @return whether array creation completed
11593 */
11594 bool get_msgpack_array(const std::size_t len)
11595 {
11596 if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
11597 {
11598 return false;
11599 }
11600
11601 for (std::size_t i = 0; i < len; ++i)
11602 {
11603 if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
11604 {
11605 return false;
11606 }
11607 }
11608
11609 return sax->end_array();
11610 }
11611
11612 /*!
11613 @param[in] len the length of the object
11614 @return whether object creation completed
11615 */
11616 bool get_msgpack_object(const std::size_t len)
11617 {
11618 if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
11619 {
11620 return false;
11621 }
11622
11623 string_t key;
11624 for (std::size_t i = 0; i < len; ++i)
11625 {
11626 get();
11627 if (JSON_HEDLEY_UNLIKELY(!get_msgpack_string(key) || !sax->key(key)))
11628 {
11629 return false;
11630 }
11631
11632 if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
11633 {
11634 return false;
11635 }
11636 key.clear();
11637 }
11638
11639 return sax->end_object();
11640 }
11641
11642 ////////////
11643 // UBJSON //
11644 ////////////
11645
11646 /*!
11647 @param[in] get_char whether a new character should be retrieved from the
11648 input (true, default) or whether the last read
11649 character should be considered instead
11650
11651 @return whether a valid UBJSON value was passed to the SAX parser
11652 */
11653 bool parse_ubjson_internal(const bool get_char = true)
11654 {
11655 return get_ubjson_value(get_char ? get_ignore_noop() : current);
11656 }
11657
11658 /*!
11659 @brief reads a UBJSON string
11660
11661 This function is either called after reading the 'S' byte explicitly
11662 indicating a string, or in case of an object key where the 'S' byte can be
11663 left out.
11664
11665 @param[out] result created string
11666 @param[in] get_char whether a new character should be retrieved from the
11667 input (true, default) or whether the last read
11668 character should be considered instead
11669
11670 @return whether string creation completed
11671 */
11672 bool get_ubjson_string(string_t& result, const bool get_char = true)
11673 {
11674 if (get_char)
11675 {
11676 get(); // TODO(niels): may we ignore N here?
11677 }
11678
11679 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "value")))
11680 {
11681 return false;
11682 }
11683
11684 switch (current)
11685 {
11686 case 'U':
11687 {
11688 std::uint8_t len{};
11689 return get_number(input_format, len) && get_string(input_format, len, result);
11690 }
11691
11692 case 'i':
11693 {
11694 std::int8_t len{};
11695 return get_number(input_format, len) && get_string(input_format, len, result);
11696 }
11697
11698 case 'I':
11699 {
11700 std::int16_t len{};
11701 return get_number(input_format, len) && get_string(input_format, len, result);
11702 }
11703
11704 case 'l':
11705 {
11706 std::int32_t len{};
11707 return get_number(input_format, len) && get_string(input_format, len, result);
11708 }
11709
11710 case 'L':
11711 {
11712 std::int64_t len{};
11713 return get_number(input_format, len) && get_string(input_format, len, result);
11714 }
11715
11716 case 'u':
11717 {
11718 if (input_format != input_format_t::bjdata)
11719 {
11720 break;
11721 }
11722 std::uint16_t len{};
11723 return get_number(input_format, len) && get_string(input_format, len, result);
11724 }
11725
11726 case 'm':
11727 {
11728 if (input_format != input_format_t::bjdata)
11729 {
11730 break;
11731 }
11732 std::uint32_t len{};
11733 return get_number(input_format, len) && get_string(input_format, len, result);
11734 }
11735
11736 case 'M':
11737 {
11738 if (input_format != input_format_t::bjdata)
11739 {
11740 break;
11741 }
11742 std::uint64_t len{};
11743 return get_number(input_format, len) && get_string(input_format, len, result);
11744 }
11745
11746 default:
11747 break;
11748 }
11749 auto last_token = get_token_string();
11750 std::string message;
11751
11752 if (input_format != input_format_t::bjdata)
11753 {
11754 message = "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token;
11755 }
11756 else
11757 {
11758 message = "expected length type specification (U, i, u, I, m, l, M, L); last byte: 0x" + last_token;
11759 }
11760 return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "string"), nullptr));
11761 }
11762
11763 /*!
11764 @param[out] dim an integer vector storing the ND array dimensions
11765 @return whether reading ND array size vector is successful
11766 */
11767 bool get_ubjson_ndarray_size(std::vector<size_t>& dim)
11768 {
11769 std::pair<std::size_t, char_int_type> size_and_type;
11770 size_t dimlen = 0;
11771 bool no_ndarray = true;
11772
11773 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type, no_ndarray)))
11774 {
11775 return false;
11776 }
11777
11778 if (size_and_type.first != npos)
11779 {
11780 if (size_and_type.second != 0)
11781 {
11782 if (size_and_type.second != 'N')
11783 {
11784 for (std::size_t i = 0; i < size_and_type.first; ++i)
11785 {
11786 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, size_and_type.second)))
11787 {
11788 return false;
11789 }
11790 dim.push_back(dimlen);
11791 }
11792 }
11793 }
11794 else
11795 {
11796 for (std::size_t i = 0; i < size_and_type.first; ++i)
11797 {
11798 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray)))
11799 {
11800 return false;
11801 }
11802 dim.push_back(dimlen);
11803 }
11804 }
11805 }
11806 else
11807 {
11808 while (current != ']')
11809 {
11810 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, current)))
11811 {
11812 return false;
11813 }
11814 dim.push_back(dimlen);
11815 get_ignore_noop();
11816 }
11817 }
11818 return true;
11819 }
11820
11821 /*!
11822 @param[out] result determined size
11823 @param[in,out] is_ndarray for input, `true` means already inside an ndarray vector
11824 or ndarray dimension is not allowed; `false` means ndarray
11825 is allowed; for output, `true` means an ndarray is found;
11826 is_ndarray can only return `true` when its initial value
11827 is `false`
11828 @param[in] prefix type marker if already read, otherwise set to 0
11829
11830 @return whether size determination completed
11831 */
11832 bool get_ubjson_size_value(std::size_t& result, bool& is_ndarray, char_int_type prefix = 0)
11833 {
11834 if (prefix == 0)
11835 {
11836 prefix = get_ignore_noop();
11837 }
11838
11839 switch (prefix)
11840 {
11841 case 'U':
11842 {
11843 std::uint8_t number{};
11844 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11845 {
11846 return false;
11847 }
11848 result = static_cast<std::size_t>(number);
11849 return true;
11850 }
11851
11852 case 'i':
11853 {
11854 std::int8_t number{};
11855 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11856 {
11857 return false;
11858 }
11859 if (number < 0)
11860 {
11861 return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
11862 exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
11863 }
11864 result = static_cast<std::size_t>(number); // NOLINT(bugprone-signed-char-misuse,cert-str34-c): number is not a char
11865 return true;
11866 }
11867
11868 case 'I':
11869 {
11870 std::int16_t number{};
11871 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11872 {
11873 return false;
11874 }
11875 if (number < 0)
11876 {
11877 return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
11878 exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
11879 }
11880 result = static_cast<std::size_t>(number);
11881 return true;
11882 }
11883
11884 case 'l':
11885 {
11886 std::int32_t number{};
11887 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11888 {
11889 return false;
11890 }
11891 if (number < 0)
11892 {
11893 return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
11894 exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
11895 }
11896 result = static_cast<std::size_t>(number);
11897 return true;
11898 }
11899
11900 case 'L':
11901 {
11902 std::int64_t number{};
11903 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11904 {
11905 return false;
11906 }
11907 if (number < 0)
11908 {
11909 return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
11910 exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
11911 }
11912 if (!value_in_range_of<std::size_t>(number))
11913 {
11914 return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408,
11915 exception_message(input_format, "integer value overflow", "size"), nullptr));
11916 }
11917 result = static_cast<std::size_t>(number);
11918 return true;
11919 }
11920
11921 case 'u':
11922 {
11923 if (input_format != input_format_t::bjdata)
11924 {
11925 break;
11926 }
11927 std::uint16_t number{};
11928 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11929 {
11930 return false;
11931 }
11932 result = static_cast<std::size_t>(number);
11933 return true;
11934 }
11935
11936 case 'm':
11937 {
11938 if (input_format != input_format_t::bjdata)
11939 {
11940 break;
11941 }
11942 std::uint32_t number{};
11943 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11944 {
11945 return false;
11946 }
11947 result = conditional_static_cast<std::size_t>(number);
11948 return true;
11949 }
11950
11951 case 'M':
11952 {
11953 if (input_format != input_format_t::bjdata)
11954 {
11955 break;
11956 }
11957 std::uint64_t number{};
11958 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11959 {
11960 return false;
11961 }
11962 if (!value_in_range_of<std::size_t>(number))
11963 {
11964 return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408,
11965 exception_message(input_format, "integer value overflow", "size"), nullptr));
11966 }
11967 result = detail::conditional_static_cast<std::size_t>(number);
11968 return true;
11969 }
11970
11971 case '[':
11972 {
11973 if (input_format != input_format_t::bjdata)
11974 {
11975 break;
11976 }
11977 if (is_ndarray) // ndarray dimensional vector can only contain integers, and can not embed another array
11978 {
11979 return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read, exception_message(input_format, "ndarray dimensional vector is not allowed", "size"), nullptr));
11980 }
11981 std::vector<size_t> dim;
11982 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_ndarray_size(dim)))
11983 {
11984 return false;
11985 }
11986 if (dim.size() == 1 || (dim.size() == 2 && dim.at(0) == 1)) // return normal array size if 1D row vector
11987 {
11988 result = dim.at(dim.size() - 1);
11989 return true;
11990 }
11991 if (!dim.empty()) // if ndarray, convert to an object in JData annotated array format
11992 {
11993 for (auto i : dim) // test if any dimension in an ndarray is 0, if so, return a 1D empty container
11994 {
11995 if ( i == 0 )
11996 {
11997 result = 0;
11998 return true;
11999 }
12000 }
12001
12002 string_t key = "_ArraySize_";
12003 if (JSON_HEDLEY_UNLIKELY(!sax->start_object(3) || !sax->key(key) || !sax->start_array(dim.size())))
12004 {
12005 return false;
12006 }
12007 result = 1;
12008 for (auto i : dim)
12009 {
12010 result *= i;
12011 if (result == 0 || result == npos) // because dim elements shall not have zeros, result = 0 means overflow happened; it also can't be npos as it is used to initialize size in get_ubjson_size_type()
12012 {
12013 return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408, exception_message(input_format, "excessive ndarray size caused overflow", "size"), nullptr));
12014 }
12015 if (JSON_HEDLEY_UNLIKELY(!sax->number_unsigned(static_cast<number_unsigned_t>(i))))
12016 {
12017 return false;
12018 }
12019 }
12020 is_ndarray = true;
12021 return sax->end_array();
12022 }
12023 result = 0;
12024 return true;
12025 }
12026
12027 default:
12028 break;
12029 }
12030 auto last_token = get_token_string();
12031 std::string message;
12032
12033 if (input_format != input_format_t::bjdata)
12034 {
12035 message = "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token;
12036 }
12037 else
12038 {
12039 message = "expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x" + last_token;
12040 }
12041 return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "size"), nullptr));
12042 }
12043
12044 /*!
12045 @brief determine the type and size for a container
12046
12047 In the optimized UBJSON format, a type and a size can be provided to allow
12048 for a more compact representation.
12049
12050 @param[out] result pair of the size and the type
12051 @param[in] inside_ndarray whether the parser is parsing an ND array dimensional vector
12052
12053 @return whether pair creation completed
12054 */
12055 bool get_ubjson_size_type(std::pair<std::size_t, char_int_type>& result, bool inside_ndarray = false)
12056 {
12057 result.first = npos; // size
12058 result.second = 0; // type
12059 bool is_ndarray = false;
12060
12061 get_ignore_noop();
12062
12063 if (current == '$')
12064 {
12065 result.second = get(); // must not ignore 'N', because 'N' maybe the type
12066 if (input_format == input_format_t::bjdata
12067 && JSON_HEDLEY_UNLIKELY(std::binary_search(bjd_optimized_type_markers.begin(), bjd_optimized_type_markers.end(), result.second)))
12068 {
12069 auto last_token = get_token_string();
12070 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
12071 exception_message(input_format, concat("marker 0x", last_token, " is not a permitted optimized array type"), "type"), nullptr));
12072 }
12073
12074 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "type")))
12075 {
12076 return false;
12077 }
12078
12079 get_ignore_noop();
12080 if (JSON_HEDLEY_UNLIKELY(current != '#'))
12081 {
12082 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "value")))
12083 {
12084 return false;
12085 }
12086 auto last_token = get_token_string();
12087 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
12088 exception_message(input_format, concat("expected '#' after type information; last byte: 0x", last_token), "size"), nullptr));
12089 }
12090
12091 const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
12092 if (input_format == input_format_t::bjdata && is_ndarray)
12093 {
12094 if (inside_ndarray)
12095 {
12096 return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
12097 exception_message(input_format, "ndarray can not be recursive", "size"), nullptr));
12098 }
12099 result.second |= (1 << 8); // use bit 8 to indicate ndarray, all UBJSON and BJData markers should be ASCII letters
12100 }
12101 return is_error;
12102 }
12103
12104 if (current == '#')
12105 {
12106 const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
12107 if (input_format == input_format_t::bjdata && is_ndarray)
12108 {
12109 return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
12110 exception_message(input_format, "ndarray requires both type and size", "size"), nullptr));
12111 }
12112 return is_error;
12113 }
12114
12115 return true;
12116 }
12117
12118 /*!
12119 @param prefix the previously read or set type prefix
12120 @return whether value creation completed
12121 */
12122 bool get_ubjson_value(const char_int_type prefix)
12123 {
12124 switch (prefix)
12125 {
12126 case char_traits<char_type>::eof(): // EOF
12127 return unexpect_eof(input_format, "value");
12128
12129 case 'T': // true
12130 return sax->boolean(true);
12131 case 'F': // false
12132 return sax->boolean(false);
12133
12134 case 'Z': // null
12135 return sax->null();
12136
12137 case 'B': // byte
12138 {
12139 if (input_format != input_format_t::bjdata)
12140 {
12141 break;
12142 }
12143 std::uint8_t number{};
12144 return get_number(input_format, number) && sax->number_unsigned(number);
12145 }
12146
12147 case 'U':
12148 {
12149 std::uint8_t number{};
12150 return get_number(input_format, number) && sax->number_unsigned(number);
12151 }
12152
12153 case 'i':
12154 {
12155 std::int8_t number{};
12156 return get_number(input_format, number) && sax->number_integer(number);
12157 }
12158
12159 case 'I':
12160 {
12161 std::int16_t number{};
12162 return get_number(input_format, number) && sax->number_integer(number);
12163 }
12164
12165 case 'l':
12166 {
12167 std::int32_t number{};
12168 return get_number(input_format, number) && sax->number_integer(number);
12169 }
12170
12171 case 'L':
12172 {
12173 std::int64_t number{};
12174 return get_number(input_format, number) && sax->number_integer(number);
12175 }
12176
12177 case 'u':
12178 {
12179 if (input_format != input_format_t::bjdata)
12180 {
12181 break;
12182 }
12183 std::uint16_t number{};
12184 return get_number(input_format, number) && sax->number_unsigned(number);
12185 }
12186
12187 case 'm':
12188 {
12189 if (input_format != input_format_t::bjdata)
12190 {
12191 break;
12192 }
12193 std::uint32_t number{};
12194 return get_number(input_format, number) && sax->number_unsigned(number);
12195 }
12196
12197 case 'M':
12198 {
12199 if (input_format != input_format_t::bjdata)
12200 {
12201 break;
12202 }
12203 std::uint64_t number{};
12204 return get_number(input_format, number) && sax->number_unsigned(number);
12205 }
12206
12207 case 'h':
12208 {
12209 if (input_format != input_format_t::bjdata)
12210 {
12211 break;
12212 }
12213 const auto byte1_raw = get();
12214 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
12215 {
12216 return false;
12217 }
12218 const auto byte2_raw = get();
12219 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
12220 {
12221 return false;
12222 }
12223
12224 const auto byte1 = static_cast<unsigned char>(byte1_raw);
12225 const auto byte2 = static_cast<unsigned char>(byte2_raw);
12226
12227 // code from RFC 7049, Appendix D, Figure 3:
12228 // As half-precision floating-point numbers were only added
12229 // to IEEE 754 in 2008, today's programming platforms often
12230 // still only have limited support for them. It is very
12231 // easy to include at least decoding support for them even
12232 // without such support. An example of a small decoder for
12233 // half-precision floating-point numbers in the C language
12234 // is shown in Fig. 3.
12235 const auto half = static_cast<unsigned int>((byte2 << 8u) + byte1);
12236 const double val = [&half]
12237 {
12238 const int exp = (half >> 10u) & 0x1Fu;
12239 const unsigned int mant = half & 0x3FFu;
12240 JSON_ASSERT(0 <= exp&& exp <= 32);
12241 JSON_ASSERT(mant <= 1024);
12242 switch (exp)
12243 {
12244 case 0:
12245 return std::ldexp(mant, -24);
12246 case 31:
12247 return (mant == 0)
12248 ? std::numeric_limits<double>::infinity()
12249 : std::numeric_limits<double>::quiet_NaN();
12250 default:
12251 return std::ldexp(mant + 1024, exp - 25);
12252 }
12253 }();
12254 return sax->number_float((half & 0x8000u) != 0
12255 ? static_cast<number_float_t>(-val)
12256 : static_cast<number_float_t>(val), "");
12257 }
12258
12259 case 'd':
12260 {
12261 float number{};
12262 return get_number(input_format, number) && sax->number_float(static_cast<number_float_t>(number), "");
12263 }
12264
12265 case 'D':
12266 {
12267 double number{};
12268 return get_number(input_format, number) && sax->number_float(static_cast<number_float_t>(number), "");
12269 }
12270
12271 case 'H':
12272 {
12273 return get_ubjson_high_precision_number();
12274 }
12275
12276 case 'C': // char
12277 {
12278 get();
12279 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "char")))
12280 {
12281 return false;
12282 }
12283 if (JSON_HEDLEY_UNLIKELY(current > 127))
12284 {
12285 auto last_token = get_token_string();
12286 return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
12287 exception_message(input_format, concat("byte after 'C' must be in range 0x00..0x7F; last byte: 0x", last_token), "char"), nullptr));
12288 }
12289 string_t s(1, static_cast<typename string_t::value_type>(current));
12290 return sax->string(s);
12291 }
12292
12293 case 'S': // string
12294 {
12295 string_t s;
12296 return get_ubjson_string(s) && sax->string(s);
12297 }
12298
12299 case '[': // array
12300 return get_ubjson_array();
12301
12302 case '{': // object
12303 return get_ubjson_object();
12304
12305 default: // anything else
12306 break;
12307 }
12308 auto last_token = get_token_string();
12309 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format, "invalid byte: 0x" + last_token, "value"), nullptr));
12310 }
12311
12312 /*!
12313 @return whether array creation completed
12314 */
12315 bool get_ubjson_array()
12316 {
12317 std::pair<std::size_t, char_int_type> size_and_type;
12318 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type)))
12319 {
12320 return false;
12321 }
12322
12323 // if bit-8 of size_and_type.second is set to 1, encode bjdata ndarray as an object in JData annotated array format (https://github.com/NeuroJSON/jdata):
12324 // {"_ArrayType_" : "typeid", "_ArraySize_" : [n1, n2, ...], "_ArrayData_" : [v1, v2, ...]}
12325
12326 if (input_format == input_format_t::bjdata && size_and_type.first != npos && (size_and_type.second & (1 << 8)) != 0)
12327 {
12328 size_and_type.second &= ~(static_cast<char_int_type>(1) << 8); // use bit 8 to indicate ndarray, here we remove the bit to restore the type marker
12329 auto it = std::lower_bound(bjd_types_map.begin(), bjd_types_map.end(), size_and_type.second, [](const bjd_type & p, char_int_type t)
12330 {
12331 return p.first < t;
12332 });
12333 string_t key = "_ArrayType_";
12334 if (JSON_HEDLEY_UNLIKELY(it == bjd_types_map.end() || it->first != size_and_type.second))
12335 {
12336 auto last_token = get_token_string();
12337 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
12338 exception_message(input_format, "invalid byte: 0x" + last_token, "type"), nullptr));
12339 }
12340
12341 string_t type = it->second; // sax->string() takes a reference
12342 if (JSON_HEDLEY_UNLIKELY(!sax->key(key) || !sax->string(type)))
12343 {
12344 return false;
12345 }
12346
12347 if (size_and_type.second == 'C' || size_and_type.second == 'B')
12348 {
12349 size_and_type.second = 'U';
12350 }
12351
12352 key = "_ArrayData_";
12353 if (JSON_HEDLEY_UNLIKELY(!sax->key(key) || !sax->start_array(size_and_type.first) ))
12354 {
12355 return false;
12356 }
12357
12358 for (std::size_t i = 0; i < size_and_type.first; ++i)
12359 {
12360 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
12361 {
12362 return false;
12363 }
12364 }
12365
12366 return (sax->end_array() && sax->end_object());
12367 }
12368
12369 // If BJData type marker is 'B' decode as binary
12370 if (input_format == input_format_t::bjdata && size_and_type.first != npos && size_and_type.second == 'B')
12371 {
12372 binary_t result;
12373 return get_binary(input_format, size_and_type.first, result) && sax->binary(result);
12374 }
12375
12376 if (size_and_type.first != npos)
12377 {
12378 if (JSON_HEDLEY_UNLIKELY(!sax->start_array(size_and_type.first)))
12379 {
12380 return false;
12381 }
12382
12383 if (size_and_type.second != 0)
12384 {
12385 if (size_and_type.second != 'N')
12386 {
12387 for (std::size_t i = 0; i < size_and_type.first; ++i)
12388 {
12389 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
12390 {
12391 return false;
12392 }
12393 }
12394 }
12395 }
12396 else
12397 {
12398 for (std::size_t i = 0; i < size_and_type.first; ++i)
12399 {
12400 if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
12401 {
12402 return false;
12403 }
12404 }
12405 }
12406 }
12407 else
12408 {
12409 if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
12410 {
12411 return false;
12412 }
12413
12414 while (current != ']')
12415 {
12416 if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal(false)))
12417 {
12418 return false;
12419 }
12420 get_ignore_noop();
12421 }
12422 }
12423
12424 return sax->end_array();
12425 }
12426
12427 /*!
12428 @return whether object creation completed
12429 */
12430 bool get_ubjson_object()
12431 {
12432 std::pair<std::size_t, char_int_type> size_and_type;
12433 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type)))
12434 {
12435 return false;
12436 }
12437
12438 // do not accept ND-array size in objects in BJData
12439 if (input_format == input_format_t::bjdata && size_and_type.first != npos && (size_and_type.second & (1 << 8)) != 0)
12440 {
12441 auto last_token = get_token_string();
12442 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
12443 exception_message(input_format, "BJData object does not support ND-array size in optimized format", "object"), nullptr));
12444 }
12445
12446 string_t key;
12447 if (size_and_type.first != npos)
12448 {
12449 if (JSON_HEDLEY_UNLIKELY(!sax->start_object(size_and_type.first)))
12450 {
12451 return false;
12452 }
12453
12454 if (size_and_type.second != 0)
12455 {
12456 for (std::size_t i = 0; i < size_and_type.first; ++i)
12457 {
12458 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
12459 {
12460 return false;
12461 }
12462 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
12463 {
12464 return false;
12465 }
12466 key.clear();
12467 }
12468 }
12469 else
12470 {
12471 for (std::size_t i = 0; i < size_and_type.first; ++i)
12472 {
12473 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
12474 {
12475 return false;
12476 }
12477 if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
12478 {
12479 return false;
12480 }
12481 key.clear();
12482 }
12483 }
12484 }
12485 else
12486 {
12487 if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
12488 {
12489 return false;
12490 }
12491
12492 while (current != '}')
12493 {
12494 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key, false) || !sax->key(key)))
12495 {
12496 return false;
12497 }
12498 if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
12499 {
12500 return false;
12501 }
12502 get_ignore_noop();
12503 key.clear();
12504 }
12505 }
12506
12507 return sax->end_object();
12508 }
12509
12510 // Note, no reader for UBJSON binary types is implemented because they do
12511 // not exist
12512
12513 bool get_ubjson_high_precision_number()
12514 {
12515 // get size of following number string
12516 std::size_t size{};
12517 bool no_ndarray = true;
12518 auto res = get_ubjson_size_value(size, no_ndarray);
12519 if (JSON_HEDLEY_UNLIKELY(!res))
12520 {
12521 return res;
12522 }
12523
12524 // get number string
12525 std::vector<char> number_vector;
12526 for (std::size_t i = 0; i < size; ++i)
12527 {
12528 get();
12529 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
12530 {
12531 return false;
12532 }
12533 number_vector.push_back(static_cast<char>(current));
12534 }
12535
12536 // parse number string
12537 using ia_type = decltype(detail::input_adapter(number_vector));
12538 auto number_lexer = detail::lexer<BasicJsonType, ia_type>(detail::input_adapter(number_vector), false);
12539 const auto result_number = number_lexer.scan();
12540 const auto number_string = number_lexer.get_token_string();
12541 const auto result_remainder = number_lexer.scan();
12542
12543 using token_type = typename detail::lexer_base<BasicJsonType>::token_type;
12544
12545 if (JSON_HEDLEY_UNLIKELY(result_remainder != token_type::end_of_input))
12546 {
12547 return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read,
12548 exception_message(input_format, concat("invalid number text: ", number_lexer.get_token_string()), "high-precision number"), nullptr));
12549 }
12550
12551 switch (result_number)
12552 {
12553 case token_type::value_integer:
12554 return sax->number_integer(number_lexer.get_number_integer());
12555 case token_type::value_unsigned:
12556 return sax->number_unsigned(number_lexer.get_number_unsigned());
12557 case token_type::value_float:
12558 return sax->number_float(number_lexer.get_number_float(), std::move(number_string));
12559 case token_type::uninitialized:
12560 case token_type::literal_true:
12561 case token_type::literal_false:
12562 case token_type::literal_null:
12563 case token_type::value_string:
12564 case token_type::begin_array:
12565 case token_type::begin_object:
12566 case token_type::end_array:
12567 case token_type::end_object:
12568 case token_type::name_separator:
12569 case token_type::value_separator:
12570 case token_type::parse_error:
12571 case token_type::end_of_input:
12572 case token_type::literal_or_value:
12573 default:
12574 return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read,
12575 exception_message(input_format, concat("invalid number text: ", number_lexer.get_token_string()), "high-precision number"), nullptr));
12576 }
12577 }
12578
12579 ///////////////////////
12580 // Utility functions //
12581 ///////////////////////
12582
12583 /*!
12584 @brief get next character from the input
12585
12586 This function provides the interface to the used input adapter. It does
12587 not throw in case the input reached EOF, but returns a -'ve valued
12588 `char_traits<char_type>::eof()` in that case.
12589
12590 @return character read from the input
12591 */
12592 char_int_type get()
12593 {
12594 ++chars_read;
12595 return current = ia.get_character();
12596 }
12597
12598 /*!
12599 @brief get_to read into a primitive type
12600
12601 This function provides the interface to the used input adapter. It does
12602 not throw in case the input reached EOF, but returns false instead
12603
12604 @return bool, whether the read was successful
12605 */
12606 template<class T>
12607 bool get_to(T& dest, const input_format_t format, const char* context)
12608 {
12609 auto new_chars_read = ia.get_elements(&dest);
12610 chars_read += new_chars_read;
12611 if (JSON_HEDLEY_UNLIKELY(new_chars_read < sizeof(T)))
12612 {
12613 // in case of failure, advance position by 1 to report failing location
12614 ++chars_read;
12615 sax->parse_error(chars_read, "<end of file>", parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), nullptr));
12616 return false;
12617 }
12618 return true;
12619 }
12620
12621 /*!
12622 @return character read from the input after ignoring all 'N' entries
12623 */
12624 char_int_type get_ignore_noop()
12625 {
12626 do
12627 {
12628 get();
12629 }
12630 while (current == 'N');
12631
12632 return current;
12633 }
12634
12635 template<class NumberType>
12636 static void byte_swap(NumberType& number)
12637 {
12638 constexpr std::size_t sz = sizeof(number);
12639 #ifdef __cpp_lib_byteswap
12640 if constexpr (sz == 1)
12641 {
12642 return;
12643 }
12644 if constexpr(std::is_integral_v<NumberType>)
12645 {
12646 number = std::byteswap(number);
12647 return;
12648 }
12649 #endif
12650 auto* ptr = reinterpret_cast<std::uint8_t*>(&number);
12651 for (std::size_t i = 0; i < sz / 2; ++i)
12652 {
12653 std::swap(ptr[i], ptr[sz - i - 1]);
12654 }
12655 }
12656
12657 /*
12658 @brief read a number from the input
12659
12660 @tparam NumberType the type of the number
12661 @param[in] format the current format (for diagnostics)
12662 @param[out] result number of type @a NumberType
12663
12664 @return whether conversion completed
12665
12666 @note This function needs to respect the system's endianness, because
12667 bytes in CBOR, MessagePack, and UBJSON are stored in network order
12668 (big endian) and therefore need reordering on little endian systems.
12669 On the other hand, BSON and BJData use little endian and should reorder
12670 on big endian systems.
12671 */
12672 template<typename NumberType, bool InputIsLittleEndian = false>
12673 bool get_number(const input_format_t format, NumberType& result)
12674 {
12675 // read in the original format
12676
12677 if (JSON_HEDLEY_UNLIKELY(!get_to(result, format, "number")))
12678 {
12679 return false;
12680 }
12681 if (is_little_endian != (InputIsLittleEndian || format == input_format_t::bjdata))
12682 {
12683 byte_swap(result);
12684 }
12685 return true;
12686 }
12687
12688 /*!
12689 @brief create a string by reading characters from the input
12690
12691 @tparam NumberType the type of the number
12692 @param[in] format the current format (for diagnostics)
12693 @param[in] len number of characters to read
12694 @param[out] result string created by reading @a len bytes
12695
12696 @return whether string creation completed
12697
12698 @note We can not reserve @a len bytes for the result, because @a len
12699 may be too large. Usually, @ref unexpect_eof() detects the end of
12700 the input before we run out of string memory.
12701 */
12702 template<typename NumberType>
12703 bool get_string(const input_format_t format,
12704 const NumberType len,
12705 string_t& result)
12706 {
12707 bool success = true;
12708 for (NumberType i = 0; i < len; i++)
12709 {
12710 get();
12711 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "string")))
12712 {
12713 success = false;
12714 break;
12715 }
12716 result.push_back(static_cast<typename string_t::value_type>(current));
12717 }
12718 return success;
12719 }
12720
12721 /*!
12722 @brief create a byte array by reading bytes from the input
12723
12724 @tparam NumberType the type of the number
12725 @param[in] format the current format (for diagnostics)
12726 @param[in] len number of bytes to read
12727 @param[out] result byte array created by reading @a len bytes
12728
12729 @return whether byte array creation completed
12730
12731 @note We can not reserve @a len bytes for the result, because @a len
12732 may be too large. Usually, @ref unexpect_eof() detects the end of
12733 the input before we run out of memory.
12734 */
12735 template<typename NumberType>
12736 bool get_binary(const input_format_t format,
12737 const NumberType len,
12738 binary_t& result)
12739 {
12740 bool success = true;
12741 for (NumberType i = 0; i < len; i++)
12742 {
12743 get();
12744 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "binary")))
12745 {
12746 success = false;
12747 break;
12748 }
12749 result.push_back(static_cast<std::uint8_t>(current));
12750 }
12751 return success;
12752 }
12753
12754 /*!
12755 @param[in] format the current format (for diagnostics)
12756 @param[in] context further context information (for diagnostics)
12757 @return whether the last read character is not EOF
12758 */
12759 JSON_HEDLEY_NON_NULL(3)
12760 bool unexpect_eof(const input_format_t format, const char* context) const
12761 {
12762 if (JSON_HEDLEY_UNLIKELY(current == char_traits<char_type>::eof()))
12763 {
12764 return sax->parse_error(chars_read, "<end of file>",
12765 parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), nullptr));
12766 }
12767 return true;
12768 }
12769
12770 /*!
12771 @return a string representation of the last read byte
12772 */
12773 std::string get_token_string() const
12774 {
12775 std::array<char, 3> cr{{}};
12776 static_cast<void>((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(current))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
12777 return std::string{cr.data()};
12778 }
12779
12780 /*!
12781 @param[in] format the current format
12782 @param[in] detail a detailed error message
12783 @param[in] context further context information
12784 @return a message string to use in the parse_error exceptions
12785 */
12786 std::string exception_message(const input_format_t format,
12787 const std::string& detail,
12788 const std::string& context) const
12789 {
12790 std::string error_msg = "syntax error while parsing ";
12791
12792 switch (format)
12793 {
12794 case input_format_t::cbor:
12795 error_msg += "CBOR";
12796 break;
12797
12798 case input_format_t::msgpack:
12799 error_msg += "MessagePack";
12800 break;
12801
12802 case input_format_t::ubjson:
12803 error_msg += "UBJSON";
12804 break;
12805
12806 case input_format_t::bson:
12807 error_msg += "BSON";
12808 break;
12809
12810 case input_format_t::bjdata:
12811 error_msg += "BJData";
12812 break;
12813
12814 case input_format_t::json: // LCOV_EXCL_LINE
12815 default: // LCOV_EXCL_LINE
12816 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
12817 }
12818
12819 return concat(error_msg, ' ', context, ": ", detail);
12820 }
12821
12822 private:
12823 static JSON_INLINE_VARIABLE constexpr std::size_t npos = detail::unknown_size();
12824
12825 /// input adapter
12826 InputAdapterType ia;
12827
12828 /// the current character
12829 char_int_type current = char_traits<char_type>::eof();
12830
12831 /// the number of characters read
12832 std::size_t chars_read = 0;
12833
12834 /// whether we can assume little endianness
12835 const bool is_little_endian = little_endianness();
12836
12837 /// input format
12838 const input_format_t input_format = input_format_t::json;
12839
12840 /// the SAX parser
12841 json_sax_t* sax = nullptr;
12842
12843 // excluded markers in bjdata optimized type
12844 #define JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_ \
12845 make_array<char_int_type>('F', 'H', 'N', 'S', 'T', 'Z', '[', '{')
12846
12847 #define JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_ \
12848 make_array<bjd_type>( \
12849 bjd_type{'B', "byte"}, \
12850 bjd_type{'C', "char"}, \
12851 bjd_type{'D', "double"}, \
12852 bjd_type{'I', "int16"}, \
12853 bjd_type{'L', "int64"}, \
12854 bjd_type{'M', "uint64"}, \
12855 bjd_type{'U', "uint8"}, \
12856 bjd_type{'d', "single"}, \
12857 bjd_type{'i', "int8"}, \
12858 bjd_type{'l', "int32"}, \
12859 bjd_type{'m', "uint32"}, \
12860 bjd_type{'u', "uint16"})
12861
12862 JSON_PRIVATE_UNLESS_TESTED:
12863 // lookup tables
12864 // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
12865 const decltype(JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_) bjd_optimized_type_markers =
12866 JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_;
12867
12868 using bjd_type = std::pair<char_int_type, string_t>;
12869 // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
12870 const decltype(JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_) bjd_types_map =
12871 JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_;
12872
12873 #undef JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_
12874 #undef JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_
12875 };
12876
12877 #ifndef JSON_HAS_CPP_17
12878 template<typename BasicJsonType, typename InputAdapterType, typename SAX>
12879 constexpr std::size_t binary_reader<BasicJsonType, InputAdapterType, SAX>::npos;
12880 #endif
12881
12882 } // namespace detail
12883 NLOHMANN_JSON_NAMESPACE_END
12884
12885 // #include <nlohmann/detail/input/input_adapters.hpp>
12886
12887 // #include <nlohmann/detail/input/lexer.hpp>
12888
12889 // #include <nlohmann/detail/input/parser.hpp>
12890 // __ _____ _____ _____
12891 // __| | __| | | | JSON for Modern C++
12892 // | | |__ | | | | | | version 3.12.0
12893 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
12894 //
12895 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
12896 // SPDX-License-Identifier: MIT
12897
12898
12899
12900 #include <cmath> // isfinite
12901 #include <cstdint> // uint8_t
12902 #include <functional> // function
12903 #include <string> // string
12904 #include <utility> // move
12905 #include <vector> // vector
12906
12907 // #include <nlohmann/detail/exceptions.hpp>
12908
12909 // #include <nlohmann/detail/input/input_adapters.hpp>
12910
12911 // #include <nlohmann/detail/input/json_sax.hpp>
12912
12913 // #include <nlohmann/detail/input/lexer.hpp>
12914
12915 // #include <nlohmann/detail/macro_scope.hpp>
12916
12917 // #include <nlohmann/detail/meta/is_sax.hpp>
12918
12919 // #include <nlohmann/detail/string_concat.hpp>
12920
12921 // #include <nlohmann/detail/value_t.hpp>
12922
12923
12924 NLOHMANN_JSON_NAMESPACE_BEGIN
12925 namespace detail
12926 {
12927 ////////////
12928 // parser //
12929 ////////////
12930
12931 enum class parse_event_t : std::uint8_t
12932 {
12933 /// the parser read `{` and started to process a JSON object
12934 object_start,
12935 /// the parser read `}` and finished processing a JSON object
12936 object_end,
12937 /// the parser read `[` and started to process a JSON array
12938 array_start,
12939 /// the parser read `]` and finished processing a JSON array
12940 array_end,
12941 /// the parser read a key of a value in an object
12942 key,
12943 /// the parser finished reading a JSON value
12944 value
12945 };
12946
12947 template<typename BasicJsonType>
12948 using parser_callback_t =
12949 std::function<bool(int /*depth*/, parse_event_t /*event*/, BasicJsonType& /*parsed*/)>;
12950
12951 /*!
12952 @brief syntax analysis
12953
12954 This class implements a recursive descent parser.
12955 */
12956 template<typename BasicJsonType, typename InputAdapterType>
12957 class parser
12958 {
12959 using number_integer_t = typename BasicJsonType::number_integer_t;
12960 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
12961 using number_float_t = typename BasicJsonType::number_float_t;
12962 using string_t = typename BasicJsonType::string_t;
12963 using lexer_t = lexer<BasicJsonType, InputAdapterType>;
12964 using token_type = typename lexer_t::token_type;
12965
12966 public:
12967 /// a parser reading from an input adapter
12968 explicit parser(InputAdapterType&& adapter,
12969 parser_callback_t<BasicJsonType> cb = nullptr,
12970 const bool allow_exceptions_ = true,
12971 const bool skip_comments = false)
12972 : callback(std::move(cb))
12973 , m_lexer(std::move(adapter), skip_comments)
12974 , allow_exceptions(allow_exceptions_)
12975 {
12976 // read first token
12977 get_token();
12978 }
12979
12980 /*!
12981 @brief public parser interface
12982
12983 @param[in] strict whether to expect the last token to be EOF
12984 @param[in,out] result parsed JSON value
12985
12986 @throw parse_error.101 in case of an unexpected token
12987 @throw parse_error.102 if to_unicode fails or surrogate error
12988 @throw parse_error.103 if to_unicode fails
12989 */
12990 void parse(const bool strict, BasicJsonType& result)
12991 {
12992 if (callback)
12993 {
12994 json_sax_dom_callback_parser<BasicJsonType, InputAdapterType> sdp(result, callback, allow_exceptions, &m_lexer);
12995 sax_parse_internal(&sdp);
12996
12997 // in strict mode, input must be completely read
12998 if (strict && (get_token() != token_type::end_of_input))
12999 {
13000 sdp.parse_error(m_lexer.get_position(),
13001 m_lexer.get_token_string(),
13002 parse_error::create(101, m_lexer.get_position(),
13003 exception_message(token_type::end_of_input, "value"), nullptr));
13004 }
13005
13006 // in case of an error, return discarded value
13007 if (sdp.is_errored())
13008 {
13009 result = value_t::discarded;
13010 return;
13011 }
13012
13013 // set top-level value to null if it was discarded by the callback
13014 // function
13015 if (result.is_discarded())
13016 {
13017 result = nullptr;
13018 }
13019 }
13020 else
13021 {
13022 json_sax_dom_parser<BasicJsonType, InputAdapterType> sdp(result, allow_exceptions, &m_lexer);
13023 sax_parse_internal(&sdp);
13024
13025 // in strict mode, input must be completely read
13026 if (strict && (get_token() != token_type::end_of_input))
13027 {
13028 sdp.parse_error(m_lexer.get_position(),
13029 m_lexer.get_token_string(),
13030 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), nullptr));
13031 }
13032
13033 // in case of an error, return discarded value
13034 if (sdp.is_errored())
13035 {
13036 result = value_t::discarded;
13037 return;
13038 }
13039 }
13040
13041 result.assert_invariant();
13042 }
13043
13044 /*!
13045 @brief public accept interface
13046
13047 @param[in] strict whether to expect the last token to be EOF
13048 @return whether the input is a proper JSON text
13049 */
13050 bool accept(const bool strict = true)
13051 {
13052 json_sax_acceptor<BasicJsonType> sax_acceptor;
13053 return sax_parse(&sax_acceptor, strict);
13054 }
13055
13056 template<typename SAX>
13057 JSON_HEDLEY_NON_NULL(2)
13058 bool sax_parse(SAX* sax, const bool strict = true)
13059 {
13060 (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
13061 const bool result = sax_parse_internal(sax);
13062
13063 // strict mode: next byte must be EOF
13064 if (result && strict && (get_token() != token_type::end_of_input))
13065 {
13066 return sax->parse_error(m_lexer.get_position(),
13067 m_lexer.get_token_string(),
13068 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), nullptr));
13069 }
13070
13071 return result;
13072 }
13073
13074 private:
13075 template<typename SAX>
13076 JSON_HEDLEY_NON_NULL(2)
13077 bool sax_parse_internal(SAX* sax)
13078 {
13079 // stack to remember the hierarchy of structured values we are parsing
13080 // true = array; false = object
13081 std::vector<bool> states;
13082 // value to avoid a goto (see comment where set to true)
13083 bool skip_to_state_evaluation = false;
13084
13085 while (true)
13086 {
13087 if (!skip_to_state_evaluation)
13088 {
13089 // invariant: get_token() was called before each iteration
13090 switch (last_token)
13091 {
13092 case token_type::begin_object:
13093 {
13094 if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
13095 {
13096 return false;
13097 }
13098
13099 // closing } -> we are done
13100 if (get_token() == token_type::end_object)
13101 {
13102 if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
13103 {
13104 return false;
13105 }
13106 break;
13107 }
13108
13109 // parse key
13110 if (JSON_HEDLEY_UNLIKELY(last_token != token_type::value_string))
13111 {
13112 return sax->parse_error(m_lexer.get_position(),
13113 m_lexer.get_token_string(),
13114 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), nullptr));
13115 }
13116 if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
13117 {
13118 return false;
13119 }
13120
13121 // parse separator (:)
13122 if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
13123 {
13124 return sax->parse_error(m_lexer.get_position(),
13125 m_lexer.get_token_string(),
13126 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), nullptr));
13127 }
13128
13129 // remember we are now inside an object
13130 states.push_back(false);
13131
13132 // parse values
13133 get_token();
13134 continue;
13135 }
13136
13137 case token_type::begin_array:
13138 {
13139 if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
13140 {
13141 return false;
13142 }
13143
13144 // closing ] -> we are done
13145 if (get_token() == token_type::end_array)
13146 {
13147 if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
13148 {
13149 return false;
13150 }
13151 break;
13152 }
13153
13154 // remember we are now inside an array
13155 states.push_back(true);
13156
13157 // parse values (no need to call get_token)
13158 continue;
13159 }
13160
13161 case token_type::value_float:
13162 {
13163 const auto res = m_lexer.get_number_float();
13164
13165 if (JSON_HEDLEY_UNLIKELY(!std::isfinite(res)))
13166 {
13167 return sax->parse_error(m_lexer.get_position(),
13168 m_lexer.get_token_string(),
13169 out_of_range::create(406, concat("number overflow parsing '", m_lexer.get_token_string(), '\''), nullptr));
13170 }
13171
13172 if (JSON_HEDLEY_UNLIKELY(!sax->number_float(res, m_lexer.get_string())))
13173 {
13174 return false;
13175 }
13176
13177 break;
13178 }
13179
13180 case token_type::literal_false:
13181 {
13182 if (JSON_HEDLEY_UNLIKELY(!sax->boolean(false)))
13183 {
13184 return false;
13185 }
13186 break;
13187 }
13188
13189 case token_type::literal_null:
13190 {
13191 if (JSON_HEDLEY_UNLIKELY(!sax->null()))
13192 {
13193 return false;
13194 }
13195 break;
13196 }
13197
13198 case token_type::literal_true:
13199 {
13200 if (JSON_HEDLEY_UNLIKELY(!sax->boolean(true)))
13201 {
13202 return false;
13203 }
13204 break;
13205 }
13206
13207 case token_type::value_integer:
13208 {
13209 if (JSON_HEDLEY_UNLIKELY(!sax->number_integer(m_lexer.get_number_integer())))
13210 {
13211 return false;
13212 }
13213 break;
13214 }
13215
13216 case token_type::value_string:
13217 {
13218 if (JSON_HEDLEY_UNLIKELY(!sax->string(m_lexer.get_string())))
13219 {
13220 return false;
13221 }
13222 break;
13223 }
13224
13225 case token_type::value_unsigned:
13226 {
13227 if (JSON_HEDLEY_UNLIKELY(!sax->number_unsigned(m_lexer.get_number_unsigned())))
13228 {
13229 return false;
13230 }
13231 break;
13232 }
13233
13234 case token_type::parse_error:
13235 {
13236 // using "uninitialized" to avoid "expected" message
13237 return sax->parse_error(m_lexer.get_position(),
13238 m_lexer.get_token_string(),
13239 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::uninitialized, "value"), nullptr));
13240 }
13241 case token_type::end_of_input:
13242 {
13243 if (JSON_HEDLEY_UNLIKELY(m_lexer.get_position().chars_read_total == 1))
13244 {
13245 return sax->parse_error(m_lexer.get_position(),
13246 m_lexer.get_token_string(),
13247 parse_error::create(101, m_lexer.get_position(),
13248 "attempting to parse an empty input; check that your input string or stream contains the expected JSON", nullptr));
13249 }
13250
13251 return sax->parse_error(m_lexer.get_position(),
13252 m_lexer.get_token_string(),
13253 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::literal_or_value, "value"), nullptr));
13254 }
13255 case token_type::uninitialized:
13256 case token_type::end_array:
13257 case token_type::end_object:
13258 case token_type::name_separator:
13259 case token_type::value_separator:
13260 case token_type::literal_or_value:
13261 default: // the last token was unexpected
13262 {
13263 return sax->parse_error(m_lexer.get_position(),
13264 m_lexer.get_token_string(),
13265 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::literal_or_value, "value"), nullptr));
13266 }
13267 }
13268 }
13269 else
13270 {
13271 skip_to_state_evaluation = false;
13272 }
13273
13274 // we reached this line after we successfully parsed a value
13275 if (states.empty())
13276 {
13277 // empty stack: we reached the end of the hierarchy: done
13278 return true;
13279 }
13280
13281 if (states.back()) // array
13282 {
13283 // comma -> next value
13284 if (get_token() == token_type::value_separator)
13285 {
13286 // parse a new value
13287 get_token();
13288 continue;
13289 }
13290
13291 // closing ]
13292 if (JSON_HEDLEY_LIKELY(last_token == token_type::end_array))
13293 {
13294 if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
13295 {
13296 return false;
13297 }
13298
13299 // We are done with this array. Before we can parse a
13300 // new value, we need to evaluate the new state first.
13301 // By setting skip_to_state_evaluation to false, we
13302 // are effectively jumping to the beginning of this if.
13303 JSON_ASSERT(!states.empty());
13304 states.pop_back();
13305 skip_to_state_evaluation = true;
13306 continue;
13307 }
13308
13309 return sax->parse_error(m_lexer.get_position(),
13310 m_lexer.get_token_string(),
13311 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_array, "array"), nullptr));
13312 }
13313
13314 // states.back() is false -> object
13315
13316 // comma -> next value
13317 if (get_token() == token_type::value_separator)
13318 {
13319 // parse key
13320 if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::value_string))
13321 {
13322 return sax->parse_error(m_lexer.get_position(),
13323 m_lexer.get_token_string(),
13324 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), nullptr));
13325 }
13326
13327 if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
13328 {
13329 return false;
13330 }
13331
13332 // parse separator (:)
13333 if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
13334 {
13335 return sax->parse_error(m_lexer.get_position(),
13336 m_lexer.get_token_string(),
13337 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), nullptr));
13338 }
13339
13340 // parse values
13341 get_token();
13342 continue;
13343 }
13344
13345 // closing }
13346 if (JSON_HEDLEY_LIKELY(last_token == token_type::end_object))
13347 {
13348 if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
13349 {
13350 return false;
13351 }
13352
13353 // We are done with this object. Before we can parse a
13354 // new value, we need to evaluate the new state first.
13355 // By setting skip_to_state_evaluation to false, we
13356 // are effectively jumping to the beginning of this if.
13357 JSON_ASSERT(!states.empty());
13358 states.pop_back();
13359 skip_to_state_evaluation = true;
13360 continue;
13361 }
13362
13363 return sax->parse_error(m_lexer.get_position(),
13364 m_lexer.get_token_string(),
13365 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_object, "object"), nullptr));
13366 }
13367 }
13368
13369 /// get next token from lexer
13370 token_type get_token()
13371 {
13372 return last_token = m_lexer.scan();
13373 }
13374
13375 std::string exception_message(const token_type expected, const std::string& context)
13376 {
13377 std::string error_msg = "syntax error ";
13378
13379 if (!context.empty())
13380 {
13381 error_msg += concat("while parsing ", context, ' ');
13382 }
13383
13384 error_msg += "- ";
13385
13386 if (last_token == token_type::parse_error)
13387 {
13388 error_msg += concat(m_lexer.get_error_message(), "; last read: '",
13389 m_lexer.get_token_string(), '\'');
13390 }
13391 else
13392 {
13393 error_msg += concat("unexpected ", lexer_t::token_type_name(last_token));
13394 }
13395
13396 if (expected != token_type::uninitialized)
13397 {
13398 error_msg += concat("; expected ", lexer_t::token_type_name(expected));
13399 }
13400
13401 return error_msg;
13402 }
13403
13404 private:
13405 /// callback function
13406 const parser_callback_t<BasicJsonType> callback = nullptr;
13407 /// the type of the last read token
13408 token_type last_token = token_type::uninitialized;
13409 /// the lexer
13410 lexer_t m_lexer;
13411 /// whether to throw exceptions in case of errors
13412 const bool allow_exceptions = true;
13413 };
13414
13415 } // namespace detail
13416 NLOHMANN_JSON_NAMESPACE_END
13417
13418 // #include <nlohmann/detail/iterators/internal_iterator.hpp>
13419 // __ _____ _____ _____
13420 // __| | __| | | | JSON for Modern C++
13421 // | | |__ | | | | | | version 3.12.0
13422 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
13423 //
13424 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
13425 // SPDX-License-Identifier: MIT
13426
13427
13428
13429 // #include <nlohmann/detail/abi_macros.hpp>
13430
13431 // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
13432 // __ _____ _____ _____
13433 // __| | __| | | | JSON for Modern C++
13434 // | | |__ | | | | | | version 3.12.0
13435 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
13436 //
13437 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
13438 // SPDX-License-Identifier: MIT
13439
13440
13441
13442 #include <cstddef> // ptrdiff_t
13443 #include <limits> // numeric_limits
13444
13445 // #include <nlohmann/detail/macro_scope.hpp>
13446
13447
13448 NLOHMANN_JSON_NAMESPACE_BEGIN
13449 namespace detail
13450 {
13451
13452 /*
13453 @brief an iterator for primitive JSON types
13454
13455 This class models an iterator for primitive JSON types (boolean, number,
13456 string). It's only purpose is to allow the iterator/const_iterator classes
13457 to "iterate" over primitive values. Internally, the iterator is modeled by
13458 a `difference_type` variable. Value begin_value (`0`) models the begin,
13459 end_value (`1`) models past the end.
13460 */
13461 class primitive_iterator_t
13462 {
13463 private:
13464 using difference_type = std::ptrdiff_t;
13465 static constexpr difference_type begin_value = 0;
13466 static constexpr difference_type end_value = begin_value + 1;
13467
13468 JSON_PRIVATE_UNLESS_TESTED:
13469 /// iterator as signed integer type
13470 difference_type m_it = (std::numeric_limits<std::ptrdiff_t>::min)();
13471
13472 public:
13473 constexpr difference_type get_value() const noexcept
13474 {
13475 return m_it;
13476 }
13477
13478 /// set iterator to a defined beginning
13479 void set_begin() noexcept
13480 {
13481 m_it = begin_value;
13482 }
13483
13484 /// set iterator to a defined past the end
13485 void set_end() noexcept
13486 {
13487 m_it = end_value;
13488 }
13489
13490 /// return whether the iterator can be dereferenced
13491 constexpr bool is_begin() const noexcept
13492 {
13493 return m_it == begin_value;
13494 }
13495
13496 /// return whether the iterator is at end
13497 constexpr bool is_end() const noexcept
13498 {
13499 return m_it == end_value;
13500 }
13501
13502 friend constexpr bool operator==(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
13503 {
13504 return lhs.m_it == rhs.m_it;
13505 }
13506
13507 friend constexpr bool operator<(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
13508 {
13509 return lhs.m_it < rhs.m_it;
13510 }
13511
13512 primitive_iterator_t operator+(difference_type n) noexcept
13513 {
13514 auto result = *this;
13515 result += n;
13516 return result;
13517 }
13518
13519 friend constexpr difference_type operator-(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
13520 {
13521 return lhs.m_it - rhs.m_it;
13522 }
13523
13524 primitive_iterator_t& operator++() noexcept
13525 {
13526 ++m_it;
13527 return *this;
13528 }
13529
13530 primitive_iterator_t operator++(int)& noexcept // NOLINT(cert-dcl21-cpp)
13531 {
13532 auto result = *this;
13533 ++m_it;
13534 return result;
13535 }
13536
13537 primitive_iterator_t& operator--() noexcept
13538 {
13539 --m_it;
13540 return *this;
13541 }
13542
13543 primitive_iterator_t operator--(int)& noexcept // NOLINT(cert-dcl21-cpp)
13544 {
13545 auto result = *this;
13546 --m_it;
13547 return result;
13548 }
13549
13550 primitive_iterator_t& operator+=(difference_type n) noexcept
13551 {
13552 m_it += n;
13553 return *this;
13554 }
13555
13556 primitive_iterator_t& operator-=(difference_type n) noexcept
13557 {
13558 m_it -= n;
13559 return *this;
13560 }
13561 };
13562
13563 } // namespace detail
13564 NLOHMANN_JSON_NAMESPACE_END
13565
13566
13567 NLOHMANN_JSON_NAMESPACE_BEGIN
13568 namespace detail
13569 {
13570
13571 /*!
13572 @brief an iterator value
13573
13574 @note This structure could easily be a union, but MSVC currently does not allow
13575 unions members with complex constructors, see https://github.com/nlohmann/json/pull/105.
13576 */
13577 template<typename BasicJsonType> struct internal_iterator
13578 {
13579 /// iterator for JSON objects
13580 typename BasicJsonType::object_t::iterator object_iterator {};
13581 /// iterator for JSON arrays
13582 typename BasicJsonType::array_t::iterator array_iterator {};
13583 /// generic iterator for all other types
13584 primitive_iterator_t primitive_iterator {};
13585 };
13586
13587 } // namespace detail
13588 NLOHMANN_JSON_NAMESPACE_END
13589
13590 // #include <nlohmann/detail/iterators/iter_impl.hpp>
13591 // __ _____ _____ _____
13592 // __| | __| | | | JSON for Modern C++
13593 // | | |__ | | | | | | version 3.12.0
13594 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
13595 //
13596 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
13597 // SPDX-License-Identifier: MIT
13598
13599
13600
13601 #include <iterator> // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next
13602 #include <type_traits> // conditional, is_const, remove_const
13603
13604 // #include <nlohmann/detail/exceptions.hpp>
13605
13606 // #include <nlohmann/detail/iterators/internal_iterator.hpp>
13607
13608 // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
13609
13610 // #include <nlohmann/detail/macro_scope.hpp>
13611
13612 // #include <nlohmann/detail/meta/cpp_future.hpp>
13613
13614 // #include <nlohmann/detail/meta/type_traits.hpp>
13615
13616 // #include <nlohmann/detail/value_t.hpp>
13617
13618
13619 NLOHMANN_JSON_NAMESPACE_BEGIN
13620 namespace detail
13621 {
13622
13623 // forward declare, to be able to friend it later on
13624 template<typename IteratorType> class iteration_proxy;
13625 template<typename IteratorType> class iteration_proxy_value;
13626
13627 /*!
13628 @brief a template for a bidirectional iterator for the @ref basic_json class
13629 This class implements a both iterators (iterator and const_iterator) for the
13630 @ref basic_json class.
13631 @note An iterator is called *initialized* when a pointer to a JSON value has
13632 been set (e.g., by a constructor or a copy assignment). If the iterator is
13633 default-constructed, it is *uninitialized* and most methods are undefined.
13634 **The library uses assertions to detect calls on uninitialized iterators.**
13635 @requirement The class satisfies the following concept requirements:
13636 -
13637 [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
13638 The iterator that can be moved can be moved in both directions (i.e.
13639 incremented and decremented).
13640 @since version 1.0.0, simplified in version 2.0.9, change to bidirectional
13641 iterators in version 3.0.0 (see https://github.com/nlohmann/json/issues/593)
13642 */
13643 template<typename BasicJsonType>
13644 class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions)
13645 {
13646 /// the iterator with BasicJsonType of different const-ness
13647 using other_iter_impl = iter_impl<typename std::conditional<std::is_const<BasicJsonType>::value, typename std::remove_const<BasicJsonType>::type, const BasicJsonType>::type>;
13648 /// allow basic_json to access private members
13649 friend other_iter_impl;
13650 friend BasicJsonType;
13651 friend iteration_proxy<iter_impl>;
13652 friend iteration_proxy_value<iter_impl>;
13653
13654 using object_t = typename BasicJsonType::object_t;
13655 using array_t = typename BasicJsonType::array_t;
13656 // make sure BasicJsonType is basic_json or const basic_json
13657 static_assert(is_basic_json<typename std::remove_const<BasicJsonType>::type>::value,
13658 "iter_impl only accepts (const) basic_json");
13659 // superficial check for the LegacyBidirectionalIterator named requirement
13660 static_assert(std::is_base_of<std::bidirectional_iterator_tag, std::bidirectional_iterator_tag>::value
13661 && std::is_base_of<std::bidirectional_iterator_tag, typename std::iterator_traits<typename array_t::iterator>::iterator_category>::value,
13662 "basic_json iterator assumes array and object type iterators satisfy the LegacyBidirectionalIterator named requirement.");
13663
13664 public:
13665 /// The std::iterator class template (used as a base class to provide typedefs) is deprecated in C++17.
13666 /// The C++ Standard has never required user-defined iterators to derive from std::iterator.
13667 /// A user-defined iterator should provide publicly accessible typedefs named
13668 /// iterator_category, value_type, difference_type, pointer, and reference.
13669 /// Note that value_type is required to be non-const, even for constant iterators.
13670 using iterator_category = std::bidirectional_iterator_tag;
13671
13672 /// the type of the values when the iterator is dereferenced
13673 using value_type = typename BasicJsonType::value_type;
13674 /// a type to represent differences between iterators
13675 using difference_type = typename BasicJsonType::difference_type;
13676 /// defines a pointer to the type iterated over (value_type)
13677 using pointer = typename std::conditional<std::is_const<BasicJsonType>::value,
13678 typename BasicJsonType::const_pointer,
13679 typename BasicJsonType::pointer>::type;
13680 /// defines a reference to the type iterated over (value_type)
13681 using reference =
13682 typename std::conditional<std::is_const<BasicJsonType>::value,
13683 typename BasicJsonType::const_reference,
13684 typename BasicJsonType::reference>::type;
13685
13686 iter_impl() = default;
13687 ~iter_impl() = default;
13688 iter_impl(iter_impl&&) noexcept = default;
13689 iter_impl& operator=(iter_impl&&) noexcept = default;
13690
13691 /*!
13692 @brief constructor for a given JSON instance
13693 @param[in] object pointer to a JSON object for this iterator
13694 @pre object != nullptr
13695 @post The iterator is initialized; i.e. `m_object != nullptr`.
13696 */
13697 explicit iter_impl(pointer object) noexcept : m_object(object)
13698 {
13699 JSON_ASSERT(m_object != nullptr);
13700
13701 switch (m_object->m_data.m_type)
13702 {
13703 case value_t::object:
13704 {
13705 m_it.object_iterator = typename object_t::iterator();
13706 break;
13707 }
13708
13709 case value_t::array:
13710 {
13711 m_it.array_iterator = typename array_t::iterator();
13712 break;
13713 }
13714
13715 case value_t::null:
13716 case value_t::string:
13717 case value_t::boolean:
13718 case value_t::number_integer:
13719 case value_t::number_unsigned:
13720 case value_t::number_float:
13721 case value_t::binary:
13722 case value_t::discarded:
13723 default:
13724 {
13725 m_it.primitive_iterator = primitive_iterator_t();
13726 break;
13727 }
13728 }
13729 }
13730
13731 /*!
13732 @note The conventional copy constructor and copy assignment are implicitly
13733 defined. Combined with the following converting constructor and
13734 assignment, they support: (1) copy from iterator to iterator, (2)
13735 copy from const iterator to const iterator, and (3) conversion from
13736 iterator to const iterator. However conversion from const iterator
13737 to iterator is not defined.
13738 */
13739
13740 /*!
13741 @brief const copy constructor
13742 @param[in] other const iterator to copy from
13743 @note This copy constructor had to be defined explicitly to circumvent a bug
13744 occurring on msvc v19.0 compiler (VS 2015) debug build. For more
13745 information refer to: https://github.com/nlohmann/json/issues/1608
13746 */
13747 iter_impl(const iter_impl<const BasicJsonType>& other) noexcept
13748 : m_object(other.m_object), m_it(other.m_it)
13749 {}
13750
13751 /*!
13752 @brief converting assignment
13753 @param[in] other const iterator to copy from
13754 @return const/non-const iterator
13755 @note It is not checked whether @a other is initialized.
13756 */
13757 iter_impl& operator=(const iter_impl<const BasicJsonType>& other) noexcept
13758 {
13759 if (&other != this)
13760 {
13761 m_object = other.m_object;
13762 m_it = other.m_it;
13763 }
13764 return *this;
13765 }
13766
13767 /*!
13768 @brief converting constructor
13769 @param[in] other non-const iterator to copy from
13770 @note It is not checked whether @a other is initialized.
13771 */
13772 iter_impl(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept
13773 : m_object(other.m_object), m_it(other.m_it)
13774 {}
13775
13776 /*!
13777 @brief converting assignment
13778 @param[in] other non-const iterator to copy from
13779 @return const/non-const iterator
13780 @note It is not checked whether @a other is initialized.
13781 */
13782 iter_impl& operator=(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept // NOLINT(cert-oop54-cpp)
13783 {
13784 m_object = other.m_object;
13785 m_it = other.m_it;
13786 return *this;
13787 }
13788
13789 JSON_PRIVATE_UNLESS_TESTED:
13790 /*!
13791 @brief set the iterator to the first value
13792 @pre The iterator is initialized; i.e. `m_object != nullptr`.
13793 */
13794 void set_begin() noexcept
13795 {
13796 JSON_ASSERT(m_object != nullptr);
13797
13798 switch (m_object->m_data.m_type)
13799 {
13800 case value_t::object:
13801 {
13802 m_it.object_iterator = m_object->m_data.m_value.object->begin();
13803 break;
13804 }
13805
13806 case value_t::array:
13807 {
13808 m_it.array_iterator = m_object->m_data.m_value.array->begin();
13809 break;
13810 }
13811
13812 case value_t::null:
13813 {
13814 // set to end so begin()==end() is true: null is empty
13815 m_it.primitive_iterator.set_end();
13816 break;
13817 }
13818
13819 case value_t::string:
13820 case value_t::boolean:
13821 case value_t::number_integer:
13822 case value_t::number_unsigned:
13823 case value_t::number_float:
13824 case value_t::binary:
13825 case value_t::discarded:
13826 default:
13827 {
13828 m_it.primitive_iterator.set_begin();
13829 break;
13830 }
13831 }
13832 }
13833
13834 /*!
13835 @brief set the iterator past the last value
13836 @pre The iterator is initialized; i.e. `m_object != nullptr`.
13837 */
13838 void set_end() noexcept
13839 {
13840 JSON_ASSERT(m_object != nullptr);
13841
13842 switch (m_object->m_data.m_type)
13843 {
13844 case value_t::object:
13845 {
13846 m_it.object_iterator = m_object->m_data.m_value.object->end();
13847 break;
13848 }
13849
13850 case value_t::array:
13851 {
13852 m_it.array_iterator = m_object->m_data.m_value.array->end();
13853 break;
13854 }
13855
13856 case value_t::null:
13857 case value_t::string:
13858 case value_t::boolean:
13859 case value_t::number_integer:
13860 case value_t::number_unsigned:
13861 case value_t::number_float:
13862 case value_t::binary:
13863 case value_t::discarded:
13864 default:
13865 {
13866 m_it.primitive_iterator.set_end();
13867 break;
13868 }
13869 }
13870 }
13871
13872 public:
13873 /*!
13874 @brief return a reference to the value pointed to by the iterator
13875 @pre The iterator is initialized; i.e. `m_object != nullptr`.
13876 */
13877 reference operator*() const
13878 {
13879 JSON_ASSERT(m_object != nullptr);
13880
13881 switch (m_object->m_data.m_type)
13882 {
13883 case value_t::object:
13884 {
13885 JSON_ASSERT(m_it.object_iterator != m_object->m_data.m_value.object->end());
13886 return m_it.object_iterator->second;
13887 }
13888
13889 case value_t::array:
13890 {
13891 JSON_ASSERT(m_it.array_iterator != m_object->m_data.m_value.array->end());
13892 return *m_it.array_iterator;
13893 }
13894
13895 case value_t::null:
13896 JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
13897
13898 case value_t::string:
13899 case value_t::boolean:
13900 case value_t::number_integer:
13901 case value_t::number_unsigned:
13902 case value_t::number_float:
13903 case value_t::binary:
13904 case value_t::discarded:
13905 default:
13906 {
13907 if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
13908 {
13909 return *m_object;
13910 }
13911
13912 JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
13913 }
13914 }
13915 }
13916
13917 /*!
13918 @brief dereference the iterator
13919 @pre The iterator is initialized; i.e. `m_object != nullptr`.
13920 */
13921 pointer operator->() const
13922 {
13923 JSON_ASSERT(m_object != nullptr);
13924
13925 switch (m_object->m_data.m_type)
13926 {
13927 case value_t::object:
13928 {
13929 JSON_ASSERT(m_it.object_iterator != m_object->m_data.m_value.object->end());
13930 return &(m_it.object_iterator->second);
13931 }
13932
13933 case value_t::array:
13934 {
13935 JSON_ASSERT(m_it.array_iterator != m_object->m_data.m_value.array->end());
13936 return &*m_it.array_iterator;
13937 }
13938
13939 case value_t::null:
13940 case value_t::string:
13941 case value_t::boolean:
13942 case value_t::number_integer:
13943 case value_t::number_unsigned:
13944 case value_t::number_float:
13945 case value_t::binary:
13946 case value_t::discarded:
13947 default:
13948 {
13949 if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
13950 {
13951 return m_object;
13952 }
13953
13954 JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
13955 }
13956 }
13957 }
13958
13959 /*!
13960 @brief post-increment (it++)
13961 @pre The iterator is initialized; i.e. `m_object != nullptr`.
13962 */
13963 iter_impl operator++(int)& // NOLINT(cert-dcl21-cpp)
13964 {
13965 auto result = *this;
13966 ++(*this);
13967 return result;
13968 }
13969
13970 /*!
13971 @brief pre-increment (++it)
13972 @pre The iterator is initialized; i.e. `m_object != nullptr`.
13973 */
13974 iter_impl& operator++()
13975 {
13976 JSON_ASSERT(m_object != nullptr);
13977
13978 switch (m_object->m_data.m_type)
13979 {
13980 case value_t::object:
13981 {
13982 std::advance(m_it.object_iterator, 1);
13983 break;
13984 }
13985
13986 case value_t::array:
13987 {
13988 std::advance(m_it.array_iterator, 1);
13989 break;
13990 }
13991
13992 case value_t::null:
13993 case value_t::string:
13994 case value_t::boolean:
13995 case value_t::number_integer:
13996 case value_t::number_unsigned:
13997 case value_t::number_float:
13998 case value_t::binary:
13999 case value_t::discarded:
14000 default:
14001 {
14002 ++m_it.primitive_iterator;
14003 break;
14004 }
14005 }
14006
14007 return *this;
14008 }
14009
14010 /*!
14011 @brief post-decrement (it--)
14012 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14013 */
14014 iter_impl operator--(int)& // NOLINT(cert-dcl21-cpp)
14015 {
14016 auto result = *this;
14017 --(*this);
14018 return result;
14019 }
14020
14021 /*!
14022 @brief pre-decrement (--it)
14023 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14024 */
14025 iter_impl& operator--()
14026 {
14027 JSON_ASSERT(m_object != nullptr);
14028
14029 switch (m_object->m_data.m_type)
14030 {
14031 case value_t::object:
14032 {
14033 std::advance(m_it.object_iterator, -1);
14034 break;
14035 }
14036
14037 case value_t::array:
14038 {
14039 std::advance(m_it.array_iterator, -1);
14040 break;
14041 }
14042
14043 case value_t::null:
14044 case value_t::string:
14045 case value_t::boolean:
14046 case value_t::number_integer:
14047 case value_t::number_unsigned:
14048 case value_t::number_float:
14049 case value_t::binary:
14050 case value_t::discarded:
14051 default:
14052 {
14053 --m_it.primitive_iterator;
14054 break;
14055 }
14056 }
14057
14058 return *this;
14059 }
14060
14061 /*!
14062 @brief comparison: equal
14063 @pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
14064 */
14065 template < typename IterImpl, detail::enable_if_t < (std::is_same<IterImpl, iter_impl>::value || std::is_same<IterImpl, other_iter_impl>::value), std::nullptr_t > = nullptr >
14066 bool operator==(const IterImpl& other) const
14067 {
14068 // if objects are not the same, the comparison is undefined
14069 if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
14070 {
14071 JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object));
14072 }
14073
14074 // value-initialized forward iterators can be compared, and must compare equal to other value-initialized iterators of the same type #4493
14075 if (m_object == nullptr)
14076 {
14077 return true;
14078 }
14079
14080 switch (m_object->m_data.m_type)
14081 {
14082 case value_t::object:
14083 return (m_it.object_iterator == other.m_it.object_iterator);
14084
14085 case value_t::array:
14086 return (m_it.array_iterator == other.m_it.array_iterator);
14087
14088 case value_t::null:
14089 case value_t::string:
14090 case value_t::boolean:
14091 case value_t::number_integer:
14092 case value_t::number_unsigned:
14093 case value_t::number_float:
14094 case value_t::binary:
14095 case value_t::discarded:
14096 default:
14097 return (m_it.primitive_iterator == other.m_it.primitive_iterator);
14098 }
14099 }
14100
14101 /*!
14102 @brief comparison: not equal
14103 @pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
14104 */
14105 template < typename IterImpl, detail::enable_if_t < (std::is_same<IterImpl, iter_impl>::value || std::is_same<IterImpl, other_iter_impl>::value), std::nullptr_t > = nullptr >
14106 bool operator!=(const IterImpl& other) const
14107 {
14108 return !operator==(other);
14109 }
14110
14111 /*!
14112 @brief comparison: smaller
14113 @pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
14114 */
14115 bool operator<(const iter_impl& other) const
14116 {
14117 // if objects are not the same, the comparison is undefined
14118 if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
14119 {
14120 JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object));
14121 }
14122
14123 // value-initialized forward iterators can be compared, and must compare equal to other value-initialized iterators of the same type #4493
14124 if (m_object == nullptr)
14125 {
14126 // the iterators are both value-initialized and are to be considered equal, but this function checks for smaller, so we return false
14127 return false;
14128 }
14129
14130 switch (m_object->m_data.m_type)
14131 {
14132 case value_t::object:
14133 JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators", m_object));
14134
14135 case value_t::array:
14136 return (m_it.array_iterator < other.m_it.array_iterator);
14137
14138 case value_t::null:
14139 case value_t::string:
14140 case value_t::boolean:
14141 case value_t::number_integer:
14142 case value_t::number_unsigned:
14143 case value_t::number_float:
14144 case value_t::binary:
14145 case value_t::discarded:
14146 default:
14147 return (m_it.primitive_iterator < other.m_it.primitive_iterator);
14148 }
14149 }
14150
14151 /*!
14152 @brief comparison: less than or equal
14153 @pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
14154 */
14155 bool operator<=(const iter_impl& other) const
14156 {
14157 return !other.operator < (*this);
14158 }
14159
14160 /*!
14161 @brief comparison: greater than
14162 @pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
14163 */
14164 bool operator>(const iter_impl& other) const
14165 {
14166 return !operator<=(other);
14167 }
14168
14169 /*!
14170 @brief comparison: greater than or equal
14171 @pre (1) The iterator is initialized; i.e. `m_object != nullptr`, or (2) both iterators are value-initialized.
14172 */
14173 bool operator>=(const iter_impl& other) const
14174 {
14175 return !operator<(other);
14176 }
14177
14178 /*!
14179 @brief add to iterator
14180 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14181 */
14182 iter_impl& operator+=(difference_type i)
14183 {
14184 JSON_ASSERT(m_object != nullptr);
14185
14186 switch (m_object->m_data.m_type)
14187 {
14188 case value_t::object:
14189 JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", m_object));
14190
14191 case value_t::array:
14192 {
14193 std::advance(m_it.array_iterator, i);
14194 break;
14195 }
14196
14197 case value_t::null:
14198 case value_t::string:
14199 case value_t::boolean:
14200 case value_t::number_integer:
14201 case value_t::number_unsigned:
14202 case value_t::number_float:
14203 case value_t::binary:
14204 case value_t::discarded:
14205 default:
14206 {
14207 m_it.primitive_iterator += i;
14208 break;
14209 }
14210 }
14211
14212 return *this;
14213 }
14214
14215 /*!
14216 @brief subtract from iterator
14217 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14218 */
14219 iter_impl& operator-=(difference_type i)
14220 {
14221 return operator+=(-i);
14222 }
14223
14224 /*!
14225 @brief add to iterator
14226 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14227 */
14228 iter_impl operator+(difference_type i) const
14229 {
14230 auto result = *this;
14231 result += i;
14232 return result;
14233 }
14234
14235 /*!
14236 @brief addition of distance and iterator
14237 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14238 */
14239 friend iter_impl operator+(difference_type i, const iter_impl& it)
14240 {
14241 auto result = it;
14242 result += i;
14243 return result;
14244 }
14245
14246 /*!
14247 @brief subtract from iterator
14248 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14249 */
14250 iter_impl operator-(difference_type i) const
14251 {
14252 auto result = *this;
14253 result -= i;
14254 return result;
14255 }
14256
14257 /*!
14258 @brief return difference
14259 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14260 */
14261 difference_type operator-(const iter_impl& other) const
14262 {
14263 JSON_ASSERT(m_object != nullptr);
14264
14265 switch (m_object->m_data.m_type)
14266 {
14267 case value_t::object:
14268 JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", m_object));
14269
14270 case value_t::array:
14271 return m_it.array_iterator - other.m_it.array_iterator;
14272
14273 case value_t::null:
14274 case value_t::string:
14275 case value_t::boolean:
14276 case value_t::number_integer:
14277 case value_t::number_unsigned:
14278 case value_t::number_float:
14279 case value_t::binary:
14280 case value_t::discarded:
14281 default:
14282 return m_it.primitive_iterator - other.m_it.primitive_iterator;
14283 }
14284 }
14285
14286 /*!
14287 @brief access to successor
14288 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14289 */
14290 reference operator[](difference_type n) const
14291 {
14292 JSON_ASSERT(m_object != nullptr);
14293
14294 switch (m_object->m_data.m_type)
14295 {
14296 case value_t::object:
14297 JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators", m_object));
14298
14299 case value_t::array:
14300 return *std::next(m_it.array_iterator, n);
14301
14302 case value_t::null:
14303 JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
14304
14305 case value_t::string:
14306 case value_t::boolean:
14307 case value_t::number_integer:
14308 case value_t::number_unsigned:
14309 case value_t::number_float:
14310 case value_t::binary:
14311 case value_t::discarded:
14312 default:
14313 {
14314 if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.get_value() == -n))
14315 {
14316 return *m_object;
14317 }
14318
14319 JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
14320 }
14321 }
14322 }
14323
14324 /*!
14325 @brief return the key of an object iterator
14326 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14327 */
14328 const typename object_t::key_type& key() const
14329 {
14330 JSON_ASSERT(m_object != nullptr);
14331
14332 if (JSON_HEDLEY_LIKELY(m_object->is_object()))
14333 {
14334 return m_it.object_iterator->first;
14335 }
14336
14337 JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators", m_object));
14338 }
14339
14340 /*!
14341 @brief return the value of an iterator
14342 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14343 */
14344 reference value() const
14345 {
14346 return operator*();
14347 }
14348
14349 JSON_PRIVATE_UNLESS_TESTED:
14350 /// associated JSON instance
14351 pointer m_object = nullptr;
14352 /// the actual iterator of the associated instance
14353 internal_iterator<typename std::remove_const<BasicJsonType>::type> m_it {};
14354 };
14355
14356 } // namespace detail
14357 NLOHMANN_JSON_NAMESPACE_END
14358
14359 // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
14360
14361 // #include <nlohmann/detail/iterators/json_reverse_iterator.hpp>
14362 // __ _____ _____ _____
14363 // __| | __| | | | JSON for Modern C++
14364 // | | |__ | | | | | | version 3.12.0
14365 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
14366 //
14367 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
14368 // SPDX-License-Identifier: MIT
14369
14370
14371
14372 #include <cstddef> // ptrdiff_t
14373 #include <iterator> // reverse_iterator
14374 #include <utility> // declval
14375
14376 // #include <nlohmann/detail/abi_macros.hpp>
14377
14378
14379 NLOHMANN_JSON_NAMESPACE_BEGIN
14380 namespace detail
14381 {
14382
14383 //////////////////////
14384 // reverse_iterator //
14385 //////////////////////
14386
14387 /*!
14388 @brief a template for a reverse iterator class
14389
14390 @tparam Base the base iterator type to reverse. Valid types are @ref
14391 iterator (to create @ref reverse_iterator) and @ref const_iterator (to
14392 create @ref const_reverse_iterator).
14393
14394 @requirement The class satisfies the following concept requirements:
14395 -
14396 [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
14397 The iterator that can be moved can be moved in both directions (i.e.
14398 incremented and decremented).
14399 - [OutputIterator](https://en.cppreference.com/w/cpp/named_req/OutputIterator):
14400 It is possible to write to the pointed-to element (only if @a Base is
14401 @ref iterator).
14402
14403 @since version 1.0.0
14404 */
14405 template<typename Base>
14406 class json_reverse_iterator : public std::reverse_iterator<Base>
14407 {
14408 public:
14409 using difference_type = std::ptrdiff_t;
14410 /// shortcut to the reverse iterator adapter
14411 using base_iterator = std::reverse_iterator<Base>;
14412 /// the reference type for the pointed-to element
14413 using reference = typename Base::reference;
14414
14415 /// create reverse iterator from iterator
14416 explicit json_reverse_iterator(const typename base_iterator::iterator_type& it) noexcept
14417 : base_iterator(it) {}
14418
14419 /// create reverse iterator from base class
14420 explicit json_reverse_iterator(const base_iterator& it) noexcept : base_iterator(it) {}
14421
14422 /// post-increment (it++)
14423 json_reverse_iterator operator++(int)& // NOLINT(cert-dcl21-cpp)
14424 {
14425 return static_cast<json_reverse_iterator>(base_iterator::operator++(1));
14426 }
14427
14428 /// pre-increment (++it)
14429 json_reverse_iterator& operator++()
14430 {
14431 return static_cast<json_reverse_iterator&>(base_iterator::operator++());
14432 }
14433
14434 /// post-decrement (it--)
14435 json_reverse_iterator operator--(int)& // NOLINT(cert-dcl21-cpp)
14436 {
14437 return static_cast<json_reverse_iterator>(base_iterator::operator--(1));
14438 }
14439
14440 /// pre-decrement (--it)
14441 json_reverse_iterator& operator--()
14442 {
14443 return static_cast<json_reverse_iterator&>(base_iterator::operator--());
14444 }
14445
14446 /// add to iterator
14447 json_reverse_iterator& operator+=(difference_type i)
14448 {
14449 return static_cast<json_reverse_iterator&>(base_iterator::operator+=(i));
14450 }
14451
14452 /// add to iterator
14453 json_reverse_iterator operator+(difference_type i) const
14454 {
14455 return static_cast<json_reverse_iterator>(base_iterator::operator+(i));
14456 }
14457
14458 /// subtract from iterator
14459 json_reverse_iterator operator-(difference_type i) const
14460 {
14461 return static_cast<json_reverse_iterator>(base_iterator::operator-(i));
14462 }
14463
14464 /// return difference
14465 difference_type operator-(const json_reverse_iterator& other) const
14466 {
14467 return base_iterator(*this) - base_iterator(other);
14468 }
14469
14470 /// access to successor
14471 reference operator[](difference_type n) const
14472 {
14473 return *(this->operator+(n));
14474 }
14475
14476 /// return the key of an object iterator
14477 auto key() const -> decltype(std::declval<Base>().key())
14478 {
14479 auto it = --this->base();
14480 return it.key();
14481 }
14482
14483 /// return the value of an iterator
14484 reference value() const
14485 {
14486 auto it = --this->base();
14487 return it.operator * ();
14488 }
14489 };
14490
14491 } // namespace detail
14492 NLOHMANN_JSON_NAMESPACE_END
14493
14494 // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
14495
14496 // #include <nlohmann/detail/json_custom_base_class.hpp>
14497 // __ _____ _____ _____
14498 // __| | __| | | | JSON for Modern C++
14499 // | | |__ | | | | | | version 3.12.0
14500 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
14501 //
14502 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
14503 // SPDX-License-Identifier: MIT
14504
14505
14506
14507 #include <type_traits> // conditional, is_same
14508
14509 // #include <nlohmann/detail/abi_macros.hpp>
14510
14511
14512 NLOHMANN_JSON_NAMESPACE_BEGIN
14513 namespace detail
14514 {
14515
14516 /*!
14517 @brief Default base class of the @ref basic_json class.
14518
14519 So that the correct implementations of the copy / move ctors / assign operators
14520 of @ref basic_json do not require complex case distinctions
14521 (no base class / custom base class used as customization point),
14522 @ref basic_json always has a base class.
14523 By default, this class is used because it is empty and thus has no effect
14524 on the behavior of @ref basic_json.
14525 */
14526 struct json_default_base {};
14527
14528 template<class T>
14529 using json_base_class = typename std::conditional <
14530 std::is_same<T, void>::value,
14531 json_default_base,
14532 T
14533 >::type;
14534
14535 } // namespace detail
14536 NLOHMANN_JSON_NAMESPACE_END
14537
14538 // #include <nlohmann/detail/json_pointer.hpp>
14539 // __ _____ _____ _____
14540 // __| | __| | | | JSON for Modern C++
14541 // | | |__ | | | | | | version 3.12.0
14542 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
14543 //
14544 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
14545 // SPDX-License-Identifier: MIT
14546
14547
14548
14549 #include <algorithm> // all_of
14550 #include <cctype> // isdigit
14551 #include <cerrno> // errno, ERANGE
14552 #include <cstdlib> // strtoull
14553 #ifndef JSON_NO_IO
14554 #include <iosfwd> // ostream
14555 #endif // JSON_NO_IO
14556 #include <limits> // max
14557 #include <numeric> // accumulate
14558 #include <string> // string
14559 #include <utility> // move
14560 #include <vector> // vector
14561
14562 // #include <nlohmann/detail/exceptions.hpp>
14563
14564 // #include <nlohmann/detail/macro_scope.hpp>
14565
14566 // #include <nlohmann/detail/string_concat.hpp>
14567
14568 // #include <nlohmann/detail/string_escape.hpp>
14569
14570 // #include <nlohmann/detail/value_t.hpp>
14571
14572
14573 NLOHMANN_JSON_NAMESPACE_BEGIN
14574
14575 /// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document
14576 /// @sa https://json.nlohmann.me/api/json_pointer/
14577 template<typename RefStringType>
14578 class json_pointer
14579 {
14580 // allow basic_json to access private members
14581 NLOHMANN_BASIC_JSON_TPL_DECLARATION
14582 friend class basic_json;
14583
14584 template<typename>
14585 friend class json_pointer;
14586
14587 template<typename T>
14588 struct string_t_helper
14589 {
14590 using type = T;
14591 };
14592
14593 NLOHMANN_BASIC_JSON_TPL_DECLARATION
14594 struct string_t_helper<NLOHMANN_BASIC_JSON_TPL>
14595 {
14596 using type = StringType;
14597 };
14598
14599 public:
14600 // for backwards compatibility accept BasicJsonType
14601 using string_t = typename string_t_helper<RefStringType>::type;
14602
14603 /// @brief create JSON pointer
14604 /// @sa https://json.nlohmann.me/api/json_pointer/json_pointer/
14605 explicit json_pointer(const string_t& s = "")
14606 : reference_tokens(split(s))
14607 {}
14608
14609 /// @brief return a string representation of the JSON pointer
14610 /// @sa https://json.nlohmann.me/api/json_pointer/to_string/
14611 string_t to_string() const
14612 {
14613 return std::accumulate(reference_tokens.begin(), reference_tokens.end(),
14614 string_t{},
14615 [](const string_t& a, const string_t& b)
14616 {
14617 return detail::concat(a, '/', detail::escape(b));
14618 });
14619 }
14620
14621 /// @brief return a string representation of the JSON pointer
14622 /// @sa https://json.nlohmann.me/api/json_pointer/operator_string/
14623 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, to_string())
14624 operator string_t() const
14625 {
14626 return to_string();
14627 }
14628
14629 #ifndef JSON_NO_IO
14630 /// @brief write string representation of the JSON pointer to stream
14631 /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
14632 friend std::ostream& operator<<(std::ostream& o, const json_pointer& ptr)
14633 {
14634 o << ptr.to_string();
14635 return o;
14636 }
14637 #endif
14638
14639 /// @brief append another JSON pointer at the end of this JSON pointer
14640 /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
14641 json_pointer& operator/=(const json_pointer& ptr)
14642 {
14643 reference_tokens.insert(reference_tokens.end(),
14644 ptr.reference_tokens.begin(),
14645 ptr.reference_tokens.end());
14646 return *this;
14647 }
14648
14649 /// @brief append an unescaped reference token at the end of this JSON pointer
14650 /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
14651 json_pointer& operator/=(string_t token)
14652 {
14653 push_back(std::move(token));
14654 return *this;
14655 }
14656
14657 /// @brief append an array index at the end of this JSON pointer
14658 /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
14659 json_pointer& operator/=(std::size_t array_idx)
14660 {
14661 return *this /= std::to_string(array_idx);
14662 }
14663
14664 /// @brief create a new JSON pointer by appending the right JSON pointer at the end of the left JSON pointer
14665 /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
14666 friend json_pointer operator/(const json_pointer& lhs,
14667 const json_pointer& rhs)
14668 {
14669 return json_pointer(lhs) /= rhs;
14670 }
14671
14672 /// @brief create a new JSON pointer by appending the unescaped token at the end of the JSON pointer
14673 /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
14674 friend json_pointer operator/(const json_pointer& lhs, string_t token) // NOLINT(performance-unnecessary-value-param)
14675 {
14676 return json_pointer(lhs) /= std::move(token);
14677 }
14678
14679 /// @brief create a new JSON pointer by appending the array-index-token at the end of the JSON pointer
14680 /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
14681 friend json_pointer operator/(const json_pointer& lhs, std::size_t array_idx)
14682 {
14683 return json_pointer(lhs) /= array_idx;
14684 }
14685
14686 /// @brief returns the parent of this JSON pointer
14687 /// @sa https://json.nlohmann.me/api/json_pointer/parent_pointer/
14688 json_pointer parent_pointer() const
14689 {
14690 if (empty())
14691 {
14692 return *this;
14693 }
14694
14695 json_pointer res = *this;
14696 res.pop_back();
14697 return res;
14698 }
14699
14700 /// @brief remove last reference token
14701 /// @sa https://json.nlohmann.me/api/json_pointer/pop_back/
14702 void pop_back()
14703 {
14704 if (JSON_HEDLEY_UNLIKELY(empty()))
14705 {
14706 JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
14707 }
14708
14709 reference_tokens.pop_back();
14710 }
14711
14712 /// @brief return last reference token
14713 /// @sa https://json.nlohmann.me/api/json_pointer/back/
14714 const string_t& back() const
14715 {
14716 if (JSON_HEDLEY_UNLIKELY(empty()))
14717 {
14718 JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
14719 }
14720
14721 return reference_tokens.back();
14722 }
14723
14724 /// @brief append an unescaped token at the end of the reference pointer
14725 /// @sa https://json.nlohmann.me/api/json_pointer/push_back/
14726 void push_back(const string_t& token)
14727 {
14728 reference_tokens.push_back(token);
14729 }
14730
14731 /// @brief append an unescaped token at the end of the reference pointer
14732 /// @sa https://json.nlohmann.me/api/json_pointer/push_back/
14733 void push_back(string_t&& token)
14734 {
14735 reference_tokens.push_back(std::move(token));
14736 }
14737
14738 /// @brief return whether pointer points to the root document
14739 /// @sa https://json.nlohmann.me/api/json_pointer/empty/
14740 bool empty() const noexcept
14741 {
14742 return reference_tokens.empty();
14743 }
14744
14745 private:
14746 /*!
14747 @param[in] s reference token to be converted into an array index
14748
14749 @return integer representation of @a s
14750
14751 @throw parse_error.106 if an array index begins with '0'
14752 @throw parse_error.109 if an array index begins not with a digit
14753 @throw out_of_range.404 if string @a s could not be converted to an integer
14754 @throw out_of_range.410 if an array index exceeds size_type
14755 */
14756 template<typename BasicJsonType>
14757 static typename BasicJsonType::size_type array_index(const string_t& s)
14758 {
14759 using size_type = typename BasicJsonType::size_type;
14760
14761 // error condition (cf. RFC 6901, Sect. 4)
14762 if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && s[0] == '0'))
14763 {
14764 JSON_THROW(detail::parse_error::create(106, 0, detail::concat("array index '", s, "' must not begin with '0'"), nullptr));
14765 }
14766
14767 // error condition (cf. RFC 6901, Sect. 4)
14768 if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && !(s[0] >= '1' && s[0] <= '9')))
14769 {
14770 JSON_THROW(detail::parse_error::create(109, 0, detail::concat("array index '", s, "' is not a number"), nullptr));
14771 }
14772
14773 const char* p = s.c_str();
14774 char* p_end = nullptr; // NOLINT(misc-const-correctness)
14775 errno = 0; // strtoull doesn't reset errno
14776 const unsigned long long res = std::strtoull(p, &p_end, 10); // NOLINT(runtime/int)
14777 if (p == p_end // invalid input or empty string
14778 || errno == ERANGE // out of range
14779 || JSON_HEDLEY_UNLIKELY(static_cast<std::size_t>(p_end - p) != s.size())) // incomplete read
14780 {
14781 JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", s, "'"), nullptr));
14782 }
14783
14784 // only triggered on special platforms (like 32bit), see also
14785 // https://github.com/nlohmann/json/pull/2203
14786 if (res >= static_cast<unsigned long long>((std::numeric_limits<size_type>::max)())) // NOLINT(runtime/int)
14787 {
14788 JSON_THROW(detail::out_of_range::create(410, detail::concat("array index ", s, " exceeds size_type"), nullptr)); // LCOV_EXCL_LINE
14789 }
14790
14791 return static_cast<size_type>(res);
14792 }
14793
14794 JSON_PRIVATE_UNLESS_TESTED:
14795 json_pointer top() const
14796 {
14797 if (JSON_HEDLEY_UNLIKELY(empty()))
14798 {
14799 JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
14800 }
14801
14802 json_pointer result = *this;
14803 result.reference_tokens = {reference_tokens[0]};
14804 return result;
14805 }
14806
14807 private:
14808 /*!
14809 @brief create and return a reference to the pointed to value
14810
14811 @complexity Linear in the number of reference tokens.
14812
14813 @throw parse_error.109 if array index is not a number
14814 @throw type_error.313 if value cannot be unflattened
14815 */
14816 template<typename BasicJsonType>
14817 BasicJsonType& get_and_create(BasicJsonType& j) const
14818 {
14819 auto* result = &j;
14820
14821 // in case no reference tokens exist, return a reference to the JSON value
14822 // j which will be overwritten by a primitive value
14823 for (const auto& reference_token : reference_tokens)
14824 {
14825 switch (result->type())
14826 {
14827 case detail::value_t::null:
14828 {
14829 if (reference_token == "0")
14830 {
14831 // start a new array if reference token is 0
14832 result = &result->operator[](0);
14833 }
14834 else
14835 {
14836 // start a new object otherwise
14837 result = &result->operator[](reference_token);
14838 }
14839 break;
14840 }
14841
14842 case detail::value_t::object:
14843 {
14844 // create an entry in the object
14845 result = &result->operator[](reference_token);
14846 break;
14847 }
14848
14849 case detail::value_t::array:
14850 {
14851 // create an entry in the array
14852 result = &result->operator[](array_index<BasicJsonType>(reference_token));
14853 break;
14854 }
14855
14856 /*
14857 The following code is only reached if there exists a reference
14858 token _and_ the current value is primitive. In this case, we have
14859 an error situation, because primitive values may only occur as
14860 single value; that is, with an empty list of reference tokens.
14861 */
14862 case detail::value_t::string:
14863 case detail::value_t::boolean:
14864 case detail::value_t::number_integer:
14865 case detail::value_t::number_unsigned:
14866 case detail::value_t::number_float:
14867 case detail::value_t::binary:
14868 case detail::value_t::discarded:
14869 default:
14870 JSON_THROW(detail::type_error::create(313, "invalid value to unflatten", &j));
14871 }
14872 }
14873
14874 return *result;
14875 }
14876
14877 /*!
14878 @brief return a reference to the pointed to value
14879
14880 @note This version does not throw if a value is not present, but tries to
14881 create nested values instead. For instance, calling this function
14882 with pointer `"/this/that"` on a null value is equivalent to calling
14883 `operator[]("this").operator[]("that")` on that value, effectively
14884 changing the null value to an object.
14885
14886 @param[in] ptr a JSON value
14887
14888 @return reference to the JSON value pointed to by the JSON pointer
14889
14890 @complexity Linear in the length of the JSON pointer.
14891
14892 @throw parse_error.106 if an array index begins with '0'
14893 @throw parse_error.109 if an array index was not a number
14894 @throw out_of_range.404 if the JSON pointer can not be resolved
14895 */
14896 template<typename BasicJsonType>
14897 BasicJsonType& get_unchecked(BasicJsonType* ptr) const
14898 {
14899 for (const auto& reference_token : reference_tokens)
14900 {
14901 // convert null values to arrays or objects before continuing
14902 if (ptr->is_null())
14903 {
14904 // check if reference token is a number
14905 const bool nums =
14906 std::all_of(reference_token.begin(), reference_token.end(),
14907 [](const unsigned char x)
14908 {
14909 return std::isdigit(x);
14910 });
14911
14912 // change value to array for numbers or "-" or to object otherwise
14913 *ptr = (nums || reference_token == "-")
14914 ? detail::value_t::array
14915 : detail::value_t::object;
14916 }
14917
14918 switch (ptr->type())
14919 {
14920 case detail::value_t::object:
14921 {
14922 // use unchecked object access
14923 ptr = &ptr->operator[](reference_token);
14924 break;
14925 }
14926
14927 case detail::value_t::array:
14928 {
14929 if (reference_token == "-")
14930 {
14931 // explicitly treat "-" as index beyond the end
14932 ptr = &ptr->operator[](ptr->m_data.m_value.array->size());
14933 }
14934 else
14935 {
14936 // convert array index to number; unchecked access
14937 ptr = &ptr->operator[](array_index<BasicJsonType>(reference_token));
14938 }
14939 break;
14940 }
14941
14942 case detail::value_t::null:
14943 case detail::value_t::string:
14944 case detail::value_t::boolean:
14945 case detail::value_t::number_integer:
14946 case detail::value_t::number_unsigned:
14947 case detail::value_t::number_float:
14948 case detail::value_t::binary:
14949 case detail::value_t::discarded:
14950 default:
14951 JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
14952 }
14953 }
14954
14955 return *ptr;
14956 }
14957
14958 /*!
14959 @throw parse_error.106 if an array index begins with '0'
14960 @throw parse_error.109 if an array index was not a number
14961 @throw out_of_range.402 if the array index '-' is used
14962 @throw out_of_range.404 if the JSON pointer can not be resolved
14963 */
14964 template<typename BasicJsonType>
14965 BasicJsonType& get_checked(BasicJsonType* ptr) const
14966 {
14967 for (const auto& reference_token : reference_tokens)
14968 {
14969 switch (ptr->type())
14970 {
14971 case detail::value_t::object:
14972 {
14973 // note: at performs range check
14974 ptr = &ptr->at(reference_token);
14975 break;
14976 }
14977
14978 case detail::value_t::array:
14979 {
14980 if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
14981 {
14982 // "-" always fails the range check
14983 JSON_THROW(detail::out_of_range::create(402, detail::concat(
14984 "array index '-' (", std::to_string(ptr->m_data.m_value.array->size()),
14985 ") is out of range"), ptr));
14986 }
14987
14988 // note: at performs range check
14989 ptr = &ptr->at(array_index<BasicJsonType>(reference_token));
14990 break;
14991 }
14992
14993 case detail::value_t::null:
14994 case detail::value_t::string:
14995 case detail::value_t::boolean:
14996 case detail::value_t::number_integer:
14997 case detail::value_t::number_unsigned:
14998 case detail::value_t::number_float:
14999 case detail::value_t::binary:
15000 case detail::value_t::discarded:
15001 default:
15002 JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
15003 }
15004 }
15005
15006 return *ptr;
15007 }
15008
15009 /*!
15010 @brief return a const reference to the pointed to value
15011
15012 @param[in] ptr a JSON value
15013
15014 @return const reference to the JSON value pointed to by the JSON
15015 pointer
15016
15017 @throw parse_error.106 if an array index begins with '0'
15018 @throw parse_error.109 if an array index was not a number
15019 @throw out_of_range.402 if the array index '-' is used
15020 @throw out_of_range.404 if the JSON pointer can not be resolved
15021 */
15022 template<typename BasicJsonType>
15023 const BasicJsonType& get_unchecked(const BasicJsonType* ptr) const
15024 {
15025 for (const auto& reference_token : reference_tokens)
15026 {
15027 switch (ptr->type())
15028 {
15029 case detail::value_t::object:
15030 {
15031 // use unchecked object access
15032 ptr = &ptr->operator[](reference_token);
15033 break;
15034 }
15035
15036 case detail::value_t::array:
15037 {
15038 if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
15039 {
15040 // "-" cannot be used for const access
15041 JSON_THROW(detail::out_of_range::create(402, detail::concat("array index '-' (", std::to_string(ptr->m_data.m_value.array->size()), ") is out of range"), ptr));
15042 }
15043
15044 // use unchecked array access
15045 ptr = &ptr->operator[](array_index<BasicJsonType>(reference_token));
15046 break;
15047 }
15048
15049 case detail::value_t::null:
15050 case detail::value_t::string:
15051 case detail::value_t::boolean:
15052 case detail::value_t::number_integer:
15053 case detail::value_t::number_unsigned:
15054 case detail::value_t::number_float:
15055 case detail::value_t::binary:
15056 case detail::value_t::discarded:
15057 default:
15058 JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
15059 }
15060 }
15061
15062 return *ptr;
15063 }
15064
15065 /*!
15066 @throw parse_error.106 if an array index begins with '0'
15067 @throw parse_error.109 if an array index was not a number
15068 @throw out_of_range.402 if the array index '-' is used
15069 @throw out_of_range.404 if the JSON pointer can not be resolved
15070 */
15071 template<typename BasicJsonType>
15072 const BasicJsonType& get_checked(const BasicJsonType* ptr) const
15073 {
15074 for (const auto& reference_token : reference_tokens)
15075 {
15076 switch (ptr->type())
15077 {
15078 case detail::value_t::object:
15079 {
15080 // note: at performs range check
15081 ptr = &ptr->at(reference_token);
15082 break;
15083 }
15084
15085 case detail::value_t::array:
15086 {
15087 if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
15088 {
15089 // "-" always fails the range check
15090 JSON_THROW(detail::out_of_range::create(402, detail::concat(
15091 "array index '-' (", std::to_string(ptr->m_data.m_value.array->size()),
15092 ") is out of range"), ptr));
15093 }
15094
15095 // note: at performs range check
15096 ptr = &ptr->at(array_index<BasicJsonType>(reference_token));
15097 break;
15098 }
15099
15100 case detail::value_t::null:
15101 case detail::value_t::string:
15102 case detail::value_t::boolean:
15103 case detail::value_t::number_integer:
15104 case detail::value_t::number_unsigned:
15105 case detail::value_t::number_float:
15106 case detail::value_t::binary:
15107 case detail::value_t::discarded:
15108 default:
15109 JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
15110 }
15111 }
15112
15113 return *ptr;
15114 }
15115
15116 /*!
15117 @throw parse_error.106 if an array index begins with '0'
15118 @throw parse_error.109 if an array index was not a number
15119 */
15120 template<typename BasicJsonType>
15121 bool contains(const BasicJsonType* ptr) const
15122 {
15123 for (const auto& reference_token : reference_tokens)
15124 {
15125 switch (ptr->type())
15126 {
15127 case detail::value_t::object:
15128 {
15129 if (!ptr->contains(reference_token))
15130 {
15131 // we did not find the key in the object
15132 return false;
15133 }
15134
15135 ptr = &ptr->operator[](reference_token);
15136 break;
15137 }
15138
15139 case detail::value_t::array:
15140 {
15141 if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
15142 {
15143 // "-" always fails the range check
15144 return false;
15145 }
15146 if (JSON_HEDLEY_UNLIKELY(reference_token.size() == 1 && !("0" <= reference_token && reference_token <= "9")))
15147 {
15148 // invalid char
15149 return false;
15150 }
15151 if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1))
15152 {
15153 if (JSON_HEDLEY_UNLIKELY(!('1' <= reference_token[0] && reference_token[0] <= '9')))
15154 {
15155 // first char should be between '1' and '9'
15156 return false;
15157 }
15158 for (std::size_t i = 1; i < reference_token.size(); i++)
15159 {
15160 if (JSON_HEDLEY_UNLIKELY(!('0' <= reference_token[i] && reference_token[i] <= '9')))
15161 {
15162 // other char should be between '0' and '9'
15163 return false;
15164 }
15165 }
15166 }
15167
15168 const auto idx = array_index<BasicJsonType>(reference_token);
15169 if (idx >= ptr->size())
15170 {
15171 // index out of range
15172 return false;
15173 }
15174
15175 ptr = &ptr->operator[](idx);
15176 break;
15177 }
15178
15179 case detail::value_t::null:
15180 case detail::value_t::string:
15181 case detail::value_t::boolean:
15182 case detail::value_t::number_integer:
15183 case detail::value_t::number_unsigned:
15184 case detail::value_t::number_float:
15185 case detail::value_t::binary:
15186 case detail::value_t::discarded:
15187 default:
15188 {
15189 // we do not expect primitive values if there is still a
15190 // reference token to process
15191 return false;
15192 }
15193 }
15194 }
15195
15196 // no reference token left means we found a primitive value
15197 return true;
15198 }
15199
15200 /*!
15201 @brief split the string input to reference tokens
15202
15203 @note This function is only called by the json_pointer constructor.
15204 All exceptions below are documented there.
15205
15206 @throw parse_error.107 if the pointer is not empty or begins with '/'
15207 @throw parse_error.108 if character '~' is not followed by '0' or '1'
15208 */
15209 static std::vector<string_t> split(const string_t& reference_string)
15210 {
15211 std::vector<string_t> result;
15212
15213 // special case: empty reference string -> no reference tokens
15214 if (reference_string.empty())
15215 {
15216 return result;
15217 }
15218
15219 // check if nonempty reference string begins with slash
15220 if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/'))
15221 {
15222 JSON_THROW(detail::parse_error::create(107, 1, detail::concat("JSON pointer must be empty or begin with '/' - was: '", reference_string, "'"), nullptr));
15223 }
15224
15225 // extract the reference tokens:
15226 // - slash: position of the last read slash (or end of string)
15227 // - start: position after the previous slash
15228 for (
15229 // search for the first slash after the first character
15230 std::size_t slash = reference_string.find_first_of('/', 1),
15231 // set the beginning of the first reference token
15232 start = 1;
15233 // we can stop if start == 0 (if slash == string_t::npos)
15234 start != 0;
15235 // set the beginning of the next reference token
15236 // (will eventually be 0 if slash == string_t::npos)
15237 start = (slash == string_t::npos) ? 0 : slash + 1,
15238 // find next slash
15239 slash = reference_string.find_first_of('/', start))
15240 {
15241 // use the text between the beginning of the reference token
15242 // (start) and the last slash (slash).
15243 auto reference_token = reference_string.substr(start, slash - start);
15244
15245 // check reference tokens are properly escaped
15246 for (std::size_t pos = reference_token.find_first_of('~');
15247 pos != string_t::npos;
15248 pos = reference_token.find_first_of('~', pos + 1))
15249 {
15250 JSON_ASSERT(reference_token[pos] == '~');
15251
15252 // ~ must be followed by 0 or 1
15253 if (JSON_HEDLEY_UNLIKELY(pos == reference_token.size() - 1 ||
15254 (reference_token[pos + 1] != '0' &&
15255 reference_token[pos + 1] != '1')))
15256 {
15257 JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'", nullptr));
15258 }
15259 }
15260
15261 // finally, store the reference token
15262 detail::unescape(reference_token);
15263 result.push_back(reference_token);
15264 }
15265
15266 return result;
15267 }
15268
15269 private:
15270 /*!
15271 @param[in] reference_string the reference string to the current value
15272 @param[in] value the value to consider
15273 @param[in,out] result the result object to insert values to
15274
15275 @note Empty objects or arrays are flattened to `null`.
15276 */
15277 template<typename BasicJsonType>
15278 static void flatten(const string_t& reference_string,
15279 const BasicJsonType& value,
15280 BasicJsonType& result)
15281 {
15282 switch (value.type())
15283 {
15284 case detail::value_t::array:
15285 {
15286 if (value.m_data.m_value.array->empty())
15287 {
15288 // flatten empty array as null
15289 result[reference_string] = nullptr;
15290 }
15291 else
15292 {
15293 // iterate array and use index as reference string
15294 for (std::size_t i = 0; i < value.m_data.m_value.array->size(); ++i)
15295 {
15296 flatten(detail::concat<string_t>(reference_string, '/', std::to_string(i)),
15297 value.m_data.m_value.array->operator[](i), result);
15298 }
15299 }
15300 break;
15301 }
15302
15303 case detail::value_t::object:
15304 {
15305 if (value.m_data.m_value.object->empty())
15306 {
15307 // flatten empty object as null
15308 result[reference_string] = nullptr;
15309 }
15310 else
15311 {
15312 // iterate object and use keys as reference string
15313 for (const auto& element : *value.m_data.m_value.object)
15314 {
15315 flatten(detail::concat<string_t>(reference_string, '/', detail::escape(element.first)), element.second, result);
15316 }
15317 }
15318 break;
15319 }
15320
15321 case detail::value_t::null:
15322 case detail::value_t::string:
15323 case detail::value_t::boolean:
15324 case detail::value_t::number_integer:
15325 case detail::value_t::number_unsigned:
15326 case detail::value_t::number_float:
15327 case detail::value_t::binary:
15328 case detail::value_t::discarded:
15329 default:
15330 {
15331 // add primitive value with its reference string
15332 result[reference_string] = value;
15333 break;
15334 }
15335 }
15336 }
15337
15338 /*!
15339 @param[in] value flattened JSON
15340
15341 @return unflattened JSON
15342
15343 @throw parse_error.109 if array index is not a number
15344 @throw type_error.314 if value is not an object
15345 @throw type_error.315 if object values are not primitive
15346 @throw type_error.313 if value cannot be unflattened
15347 */
15348 template<typename BasicJsonType>
15349 static BasicJsonType
15350 unflatten(const BasicJsonType& value)
15351 {
15352 if (JSON_HEDLEY_UNLIKELY(!value.is_object()))
15353 {
15354 JSON_THROW(detail::type_error::create(314, "only objects can be unflattened", &value));
15355 }
15356
15357 BasicJsonType result;
15358
15359 // iterate the JSON object values
15360 for (const auto& element : *value.m_data.m_value.object)
15361 {
15362 if (JSON_HEDLEY_UNLIKELY(!element.second.is_primitive()))
15363 {
15364 JSON_THROW(detail::type_error::create(315, "values in object must be primitive", &element.second));
15365 }
15366
15367 // assign value to reference pointed to by JSON pointer; Note that if
15368 // the JSON pointer is "" (i.e., points to the whole value), function
15369 // get_and_create returns a reference to result itself. An assignment
15370 // will then create a primitive value.
15371 json_pointer(element.first).get_and_create(result) = element.second;
15372 }
15373
15374 return result;
15375 }
15376
15377 // can't use conversion operator because of ambiguity
15378 json_pointer<string_t> convert() const&
15379 {
15380 json_pointer<string_t> result;
15381 result.reference_tokens = reference_tokens;
15382 return result;
15383 }
15384
15385 json_pointer<string_t> convert()&&
15386 {
15387 json_pointer<string_t> result;
15388 result.reference_tokens = std::move(reference_tokens);
15389 return result;
15390 }
15391
15392 public:
15393 #if JSON_HAS_THREE_WAY_COMPARISON
15394 /// @brief compares two JSON pointers for equality
15395 /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
15396 template<typename RefStringTypeRhs>
15397 bool operator==(const json_pointer<RefStringTypeRhs>& rhs) const noexcept
15398 {
15399 return reference_tokens == rhs.reference_tokens;
15400 }
15401
15402 /// @brief compares JSON pointer and string for equality
15403 /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
15404 JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer))
15405 bool operator==(const string_t& rhs) const
15406 {
15407 return *this == json_pointer(rhs);
15408 }
15409
15410 /// @brief 3-way compares two JSON pointers
15411 template<typename RefStringTypeRhs>
15412 std::strong_ordering operator<=>(const json_pointer<RefStringTypeRhs>& rhs) const noexcept // *NOPAD*
15413 {
15414 return reference_tokens <=> rhs.reference_tokens; // *NOPAD*
15415 }
15416 #else
15417 /// @brief compares two JSON pointers for equality
15418 /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
15419 template<typename RefStringTypeLhs, typename RefStringTypeRhs>
15420 // NOLINTNEXTLINE(readability-redundant-declaration)
15421 friend bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
15422 const json_pointer<RefStringTypeRhs>& rhs) noexcept;
15423
15424 /// @brief compares JSON pointer and string for equality
15425 /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
15426 template<typename RefStringTypeLhs, typename StringType>
15427 // NOLINTNEXTLINE(readability-redundant-declaration)
15428 friend bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
15429 const StringType& rhs);
15430
15431 /// @brief compares string and JSON pointer for equality
15432 /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
15433 template<typename RefStringTypeRhs, typename StringType>
15434 // NOLINTNEXTLINE(readability-redundant-declaration)
15435 friend bool operator==(const StringType& lhs,
15436 const json_pointer<RefStringTypeRhs>& rhs);
15437
15438 /// @brief compares two JSON pointers for inequality
15439 /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
15440 template<typename RefStringTypeLhs, typename RefStringTypeRhs>
15441 // NOLINTNEXTLINE(readability-redundant-declaration)
15442 friend bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
15443 const json_pointer<RefStringTypeRhs>& rhs) noexcept;
15444
15445 /// @brief compares JSON pointer and string for inequality
15446 /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
15447 template<typename RefStringTypeLhs, typename StringType>
15448 // NOLINTNEXTLINE(readability-redundant-declaration)
15449 friend bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
15450 const StringType& rhs);
15451
15452 /// @brief compares string and JSON pointer for inequality
15453 /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
15454 template<typename RefStringTypeRhs, typename StringType>
15455 // NOLINTNEXTLINE(readability-redundant-declaration)
15456 friend bool operator!=(const StringType& lhs,
15457 const json_pointer<RefStringTypeRhs>& rhs);
15458
15459 /// @brief compares two JSON pointer for less-than
15460 template<typename RefStringTypeLhs, typename RefStringTypeRhs>
15461 // NOLINTNEXTLINE(readability-redundant-declaration)
15462 friend bool operator<(const json_pointer<RefStringTypeLhs>& lhs,
15463 const json_pointer<RefStringTypeRhs>& rhs) noexcept;
15464 #endif
15465
15466 private:
15467 /// the reference tokens
15468 std::vector<string_t> reference_tokens;
15469 };
15470
15471 #if !JSON_HAS_THREE_WAY_COMPARISON
15472 // functions cannot be defined inside class due to ODR violations
15473 template<typename RefStringTypeLhs, typename RefStringTypeRhs>
15474 inline bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
15475 const json_pointer<RefStringTypeRhs>& rhs) noexcept
15476 {
15477 return lhs.reference_tokens == rhs.reference_tokens;
15478 }
15479
15480 template<typename RefStringTypeLhs,
15481 typename StringType = typename json_pointer<RefStringTypeLhs>::string_t>
15482 JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer, json_pointer))
15483 inline bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
15484 const StringType& rhs)
15485 {
15486 return lhs == json_pointer<RefStringTypeLhs>(rhs);
15487 }
15488
15489 template<typename RefStringTypeRhs,
15490 typename StringType = typename json_pointer<RefStringTypeRhs>::string_t>
15491 JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer, json_pointer))
15492 inline bool operator==(const StringType& lhs,
15493 const json_pointer<RefStringTypeRhs>& rhs)
15494 {
15495 return json_pointer<RefStringTypeRhs>(lhs) == rhs;
15496 }
15497
15498 template<typename RefStringTypeLhs, typename RefStringTypeRhs>
15499 inline bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
15500 const json_pointer<RefStringTypeRhs>& rhs) noexcept
15501 {
15502 return !(lhs == rhs);
15503 }
15504
15505 template<typename RefStringTypeLhs,
15506 typename StringType = typename json_pointer<RefStringTypeLhs>::string_t>
15507 JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator!=(json_pointer, json_pointer))
15508 inline bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
15509 const StringType& rhs)
15510 {
15511 return !(lhs == rhs);
15512 }
15513
15514 template<typename RefStringTypeRhs,
15515 typename StringType = typename json_pointer<RefStringTypeRhs>::string_t>
15516 JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator!=(json_pointer, json_pointer))
15517 inline bool operator!=(const StringType& lhs,
15518 const json_pointer<RefStringTypeRhs>& rhs)
15519 {
15520 return !(lhs == rhs);
15521 }
15522
15523 template<typename RefStringTypeLhs, typename RefStringTypeRhs>
15524 inline bool operator<(const json_pointer<RefStringTypeLhs>& lhs,
15525 const json_pointer<RefStringTypeRhs>& rhs) noexcept
15526 {
15527 return lhs.reference_tokens < rhs.reference_tokens;
15528 }
15529 #endif
15530
15531 NLOHMANN_JSON_NAMESPACE_END
15532
15533 // #include <nlohmann/detail/json_ref.hpp>
15534 // __ _____ _____ _____
15535 // __| | __| | | | JSON for Modern C++
15536 // | | |__ | | | | | | version 3.12.0
15537 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
15538 //
15539 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
15540 // SPDX-License-Identifier: MIT
15541
15542
15543
15544 #include <initializer_list>
15545 #include <utility>
15546
15547 // #include <nlohmann/detail/abi_macros.hpp>
15548
15549 // #include <nlohmann/detail/meta/type_traits.hpp>
15550
15551
15552 NLOHMANN_JSON_NAMESPACE_BEGIN
15553 namespace detail
15554 {
15555
15556 template<typename BasicJsonType>
15557 class json_ref
15558 {
15559 public:
15560 using value_type = BasicJsonType;
15561
15562 json_ref(value_type&& value)
15563 : owned_value(std::move(value))
15564 {}
15565
15566 json_ref(const value_type& value)
15567 : value_ref(&value)
15568 {}
15569
15570 json_ref(std::initializer_list<json_ref> init)
15571 : owned_value(init)
15572 {}
15573
15574 template <
15575 class... Args,
15576 enable_if_t<std::is_constructible<value_type, Args...>::value, int> = 0 >
15577 json_ref(Args && ... args)
15578 : owned_value(std::forward<Args>(args)...)
15579 {}
15580
15581 // class should be movable only
15582 json_ref(json_ref&&) noexcept = default;
15583 json_ref(const json_ref&) = delete;
15584 json_ref& operator=(const json_ref&) = delete;
15585 json_ref& operator=(json_ref&&) = delete;
15586 ~json_ref() = default;
15587
15588 value_type moved_or_copied() const
15589 {
15590 if (value_ref == nullptr)
15591 {
15592 return std::move(owned_value);
15593 }
15594 return *value_ref;
15595 }
15596
15597 value_type const& operator*() const
15598 {
15599 return value_ref ? *value_ref : owned_value;
15600 }
15601
15602 value_type const* operator->() const
15603 {
15604 return &** this;
15605 }
15606
15607 private:
15608 mutable value_type owned_value = nullptr;
15609 value_type const* value_ref = nullptr;
15610 };
15611
15612 } // namespace detail
15613 NLOHMANN_JSON_NAMESPACE_END
15614
15615 // #include <nlohmann/detail/macro_scope.hpp>
15616
15617 // #include <nlohmann/detail/string_concat.hpp>
15618
15619 // #include <nlohmann/detail/string_escape.hpp>
15620
15621 // #include <nlohmann/detail/string_utils.hpp>
15622
15623 // #include <nlohmann/detail/meta/cpp_future.hpp>
15624
15625 // #include <nlohmann/detail/meta/type_traits.hpp>
15626
15627 // #include <nlohmann/detail/output/binary_writer.hpp>
15628 // __ _____ _____ _____
15629 // __| | __| | | | JSON for Modern C++
15630 // | | |__ | | | | | | version 3.12.0
15631 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
15632 //
15633 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
15634 // SPDX-License-Identifier: MIT
15635
15636
15637
15638 #include <algorithm> // reverse
15639 #include <array> // array
15640 #include <map> // map
15641 #include <cmath> // isnan, isinf
15642 #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
15643 #include <cstring> // memcpy
15644 #include <limits> // numeric_limits
15645 #include <string> // string
15646 #include <utility> // move
15647 #include <vector> // vector
15648
15649 // #include <nlohmann/detail/input/binary_reader.hpp>
15650
15651 // #include <nlohmann/detail/macro_scope.hpp>
15652
15653 // #include <nlohmann/detail/output/output_adapters.hpp>
15654 // __ _____ _____ _____
15655 // __| | __| | | | JSON for Modern C++
15656 // | | |__ | | | | | | version 3.12.0
15657 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
15658 //
15659 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
15660 // SPDX-License-Identifier: MIT
15661
15662
15663
15664 #include <algorithm> // copy
15665 #include <cstddef> // size_t
15666 #include <iterator> // back_inserter
15667 #include <memory> // shared_ptr, make_shared
15668 #include <string> // basic_string
15669 #include <vector> // vector
15670
15671 #ifndef JSON_NO_IO
15672 #include <ios> // streamsize
15673 #include <ostream> // basic_ostream
15674 #endif // JSON_NO_IO
15675
15676 // #include <nlohmann/detail/macro_scope.hpp>
15677
15678
15679 NLOHMANN_JSON_NAMESPACE_BEGIN
15680 namespace detail
15681 {
15682
15683 /// abstract output adapter interface
15684 template<typename CharType> struct output_adapter_protocol
15685 {
15686 virtual void write_character(CharType c) = 0;
15687 virtual void write_characters(const CharType* s, std::size_t length) = 0;
15688 virtual ~output_adapter_protocol() = default;
15689
15690 output_adapter_protocol() = default;
15691 output_adapter_protocol(const output_adapter_protocol&) = default;
15692 output_adapter_protocol(output_adapter_protocol&&) noexcept = default;
15693 output_adapter_protocol& operator=(const output_adapter_protocol&) = default;
15694 output_adapter_protocol& operator=(output_adapter_protocol&&) noexcept = default;
15695 };
15696
15697 /// a type to simplify interfaces
15698 template<typename CharType>
15699 using output_adapter_t = std::shared_ptr<output_adapter_protocol<CharType>>;
15700
15701 /// output adapter for byte vectors
15702 template<typename CharType, typename AllocatorType = std::allocator<CharType>>
15703 class output_vector_adapter : public output_adapter_protocol<CharType>
15704 {
15705 public:
15706 explicit output_vector_adapter(std::vector<CharType, AllocatorType>& vec) noexcept
15707 : v(vec)
15708 {}
15709
15710 void write_character(CharType c) override
15711 {
15712 v.push_back(c);
15713 }
15714
15715 JSON_HEDLEY_NON_NULL(2)
15716 void write_characters(const CharType* s, std::size_t length) override
15717 {
15718 v.insert(v.end(), s, s + length);
15719 }
15720
15721 private:
15722 std::vector<CharType, AllocatorType>& v;
15723 };
15724
15725 #ifndef JSON_NO_IO
15726 /// output adapter for output streams
15727 template<typename CharType>
15728 class output_stream_adapter : public output_adapter_protocol<CharType>
15729 {
15730 public:
15731 explicit output_stream_adapter(std::basic_ostream<CharType>& s) noexcept
15732 : stream(s)
15733 {}
15734
15735 void write_character(CharType c) override
15736 {
15737 stream.put(c);
15738 }
15739
15740 JSON_HEDLEY_NON_NULL(2)
15741 void write_characters(const CharType* s, std::size_t length) override
15742 {
15743 stream.write(s, static_cast<std::streamsize>(length));
15744 }
15745
15746 private:
15747 std::basic_ostream<CharType>& stream;
15748 };
15749 #endif // JSON_NO_IO
15750
15751 /// output adapter for basic_string
15752 template<typename CharType, typename StringType = std::basic_string<CharType>>
15753 class output_string_adapter : public output_adapter_protocol<CharType>
15754 {
15755 public:
15756 explicit output_string_adapter(StringType& s) noexcept
15757 : str(s)
15758 {}
15759
15760 void write_character(CharType c) override
15761 {
15762 str.push_back(c);
15763 }
15764
15765 JSON_HEDLEY_NON_NULL(2)
15766 void write_characters(const CharType* s, std::size_t length) override
15767 {
15768 str.append(s, length);
15769 }
15770
15771 private:
15772 StringType& str;
15773 };
15774
15775 template<typename CharType, typename StringType = std::basic_string<CharType>>
15776 class output_adapter
15777 {
15778 public:
15779 template<typename AllocatorType = std::allocator<CharType>>
15780 output_adapter(std::vector<CharType, AllocatorType>& vec)
15781 : oa(std::make_shared<output_vector_adapter<CharType, AllocatorType>>(vec)) {}
15782
15783 #ifndef JSON_NO_IO
15784 output_adapter(std::basic_ostream<CharType>& s)
15785 : oa(std::make_shared<output_stream_adapter<CharType>>(s)) {}
15786 #endif // JSON_NO_IO
15787
15788 output_adapter(StringType& s)
15789 : oa(std::make_shared<output_string_adapter<CharType, StringType>>(s)) {}
15790
15791 operator output_adapter_t<CharType>()
15792 {
15793 return oa;
15794 }
15795
15796 private:
15797 output_adapter_t<CharType> oa = nullptr;
15798 };
15799
15800 } // namespace detail
15801 NLOHMANN_JSON_NAMESPACE_END
15802
15803 // #include <nlohmann/detail/string_concat.hpp>
15804
15805
15806 NLOHMANN_JSON_NAMESPACE_BEGIN
15807 namespace detail
15808 {
15809
15810 /// how to encode BJData
15811 enum class bjdata_version_t
15812 {
15813 draft2,
15814 draft3,
15815 };
15816
15817 ///////////////////
15818 // binary writer //
15819 ///////////////////
15820
15821 /*!
15822 @brief serialization to CBOR and MessagePack values
15823 */
15824 template<typename BasicJsonType, typename CharType>
15825 class binary_writer
15826 {
15827 using string_t = typename BasicJsonType::string_t;
15828 using binary_t = typename BasicJsonType::binary_t;
15829 using number_float_t = typename BasicJsonType::number_float_t;
15830
15831 public:
15832 /*!
15833 @brief create a binary writer
15834
15835 @param[in] adapter output adapter to write to
15836 */
15837 explicit binary_writer(output_adapter_t<CharType> adapter) : oa(std::move(adapter))
15838 {
15839 JSON_ASSERT(oa);
15840 }
15841
15842 /*!
15843 @param[in] j JSON value to serialize
15844 @pre j.type() == value_t::object
15845 */
15846 void write_bson(const BasicJsonType& j)
15847 {
15848 switch (j.type())
15849 {
15850 case value_t::object:
15851 {
15852 write_bson_object(*j.m_data.m_value.object);
15853 break;
15854 }
15855
15856 case value_t::null:
15857 case value_t::array:
15858 case value_t::string:
15859 case value_t::boolean:
15860 case value_t::number_integer:
15861 case value_t::number_unsigned:
15862 case value_t::number_float:
15863 case value_t::binary:
15864 case value_t::discarded:
15865 default:
15866 {
15867 JSON_THROW(type_error::create(317, concat("to serialize to BSON, top-level type must be object, but is ", j.type_name()), &j));
15868 }
15869 }
15870 }
15871
15872 /*!
15873 @param[in] j JSON value to serialize
15874 */
15875 void write_cbor(const BasicJsonType& j)
15876 {
15877 switch (j.type())
15878 {
15879 case value_t::null:
15880 {
15881 oa->write_character(to_char_type(0xF6));
15882 break;
15883 }
15884
15885 case value_t::boolean:
15886 {
15887 oa->write_character(j.m_data.m_value.boolean
15888 ? to_char_type(0xF5)
15889 : to_char_type(0xF4));
15890 break;
15891 }
15892
15893 case value_t::number_integer:
15894 {
15895 if (j.m_data.m_value.number_integer >= 0)
15896 {
15897 // CBOR does not differentiate between positive signed
15898 // integers and unsigned integers. Therefore, we used the
15899 // code from the value_t::number_unsigned case here.
15900 if (j.m_data.m_value.number_integer <= 0x17)
15901 {
15902 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
15903 }
15904 else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint8_t>::max)())
15905 {
15906 oa->write_character(to_char_type(0x18));
15907 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
15908 }
15909 else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint16_t>::max)())
15910 {
15911 oa->write_character(to_char_type(0x19));
15912 write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_integer));
15913 }
15914 else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint32_t>::max)())
15915 {
15916 oa->write_character(to_char_type(0x1A));
15917 write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_integer));
15918 }
15919 else
15920 {
15921 oa->write_character(to_char_type(0x1B));
15922 write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_integer));
15923 }
15924 }
15925 else
15926 {
15927 // The conversions below encode the sign in the first
15928 // byte, and the value is converted to a positive number.
15929 const auto positive_number = -1 - j.m_data.m_value.number_integer;
15930 if (j.m_data.m_value.number_integer >= -24)
15931 {
15932 write_number(static_cast<std::uint8_t>(0x20 + positive_number));
15933 }
15934 else if (positive_number <= (std::numeric_limits<std::uint8_t>::max)())
15935 {
15936 oa->write_character(to_char_type(0x38));
15937 write_number(static_cast<std::uint8_t>(positive_number));
15938 }
15939 else if (positive_number <= (std::numeric_limits<std::uint16_t>::max)())
15940 {
15941 oa->write_character(to_char_type(0x39));
15942 write_number(static_cast<std::uint16_t>(positive_number));
15943 }
15944 else if (positive_number <= (std::numeric_limits<std::uint32_t>::max)())
15945 {
15946 oa->write_character(to_char_type(0x3A));
15947 write_number(static_cast<std::uint32_t>(positive_number));
15948 }
15949 else
15950 {
15951 oa->write_character(to_char_type(0x3B));
15952 write_number(static_cast<std::uint64_t>(positive_number));
15953 }
15954 }
15955 break;
15956 }
15957
15958 case value_t::number_unsigned:
15959 {
15960 if (j.m_data.m_value.number_unsigned <= 0x17)
15961 {
15962 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_unsigned));
15963 }
15964 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
15965 {
15966 oa->write_character(to_char_type(0x18));
15967 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_unsigned));
15968 }
15969 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
15970 {
15971 oa->write_character(to_char_type(0x19));
15972 write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_unsigned));
15973 }
15974 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
15975 {
15976 oa->write_character(to_char_type(0x1A));
15977 write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_unsigned));
15978 }
15979 else
15980 {
15981 oa->write_character(to_char_type(0x1B));
15982 write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_unsigned));
15983 }
15984 break;
15985 }
15986
15987 case value_t::number_float:
15988 {
15989 if (std::isnan(j.m_data.m_value.number_float))
15990 {
15991 // NaN is 0xf97e00 in CBOR
15992 oa->write_character(to_char_type(0xF9));
15993 oa->write_character(to_char_type(0x7E));
15994 oa->write_character(to_char_type(0x00));
15995 }
15996 else if (std::isinf(j.m_data.m_value.number_float))
15997 {
15998 // Infinity is 0xf97c00, -Infinity is 0xf9fc00
15999 oa->write_character(to_char_type(0xf9));
16000 oa->write_character(j.m_data.m_value.number_float > 0 ? to_char_type(0x7C) : to_char_type(0xFC));
16001 oa->write_character(to_char_type(0x00));
16002 }
16003 else
16004 {
16005 write_compact_float(j.m_data.m_value.number_float, detail::input_format_t::cbor);
16006 }
16007 break;
16008 }
16009
16010 case value_t::string:
16011 {
16012 // step 1: write control byte and the string length
16013 const auto N = j.m_data.m_value.string->size();
16014 if (N <= 0x17)
16015 {
16016 write_number(static_cast<std::uint8_t>(0x60 + N));
16017 }
16018 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
16019 {
16020 oa->write_character(to_char_type(0x78));
16021 write_number(static_cast<std::uint8_t>(N));
16022 }
16023 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
16024 {
16025 oa->write_character(to_char_type(0x79));
16026 write_number(static_cast<std::uint16_t>(N));
16027 }
16028 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
16029 {
16030 oa->write_character(to_char_type(0x7A));
16031 write_number(static_cast<std::uint32_t>(N));
16032 }
16033 // LCOV_EXCL_START
16034 else if (N <= (std::numeric_limits<std::uint64_t>::max)())
16035 {
16036 oa->write_character(to_char_type(0x7B));
16037 write_number(static_cast<std::uint64_t>(N));
16038 }
16039 // LCOV_EXCL_STOP
16040
16041 // step 2: write the string
16042 oa->write_characters(
16043 reinterpret_cast<const CharType*>(j.m_data.m_value.string->c_str()),
16044 j.m_data.m_value.string->size());
16045 break;
16046 }
16047
16048 case value_t::array:
16049 {
16050 // step 1: write control byte and the array size
16051 const auto N = j.m_data.m_value.array->size();
16052 if (N <= 0x17)
16053 {
16054 write_number(static_cast<std::uint8_t>(0x80 + N));
16055 }
16056 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
16057 {
16058 oa->write_character(to_char_type(0x98));
16059 write_number(static_cast<std::uint8_t>(N));
16060 }
16061 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
16062 {
16063 oa->write_character(to_char_type(0x99));
16064 write_number(static_cast<std::uint16_t>(N));
16065 }
16066 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
16067 {
16068 oa->write_character(to_char_type(0x9A));
16069 write_number(static_cast<std::uint32_t>(N));
16070 }
16071 // LCOV_EXCL_START
16072 else if (N <= (std::numeric_limits<std::uint64_t>::max)())
16073 {
16074 oa->write_character(to_char_type(0x9B));
16075 write_number(static_cast<std::uint64_t>(N));
16076 }
16077 // LCOV_EXCL_STOP
16078
16079 // step 2: write each element
16080 for (const auto& el : *j.m_data.m_value.array)
16081 {
16082 write_cbor(el);
16083 }
16084 break;
16085 }
16086
16087 case value_t::binary:
16088 {
16089 if (j.m_data.m_value.binary->has_subtype())
16090 {
16091 if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint8_t>::max)())
16092 {
16093 write_number(static_cast<std::uint8_t>(0xd8));
16094 write_number(static_cast<std::uint8_t>(j.m_data.m_value.binary->subtype()));
16095 }
16096 else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint16_t>::max)())
16097 {
16098 write_number(static_cast<std::uint8_t>(0xd9));
16099 write_number(static_cast<std::uint16_t>(j.m_data.m_value.binary->subtype()));
16100 }
16101 else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint32_t>::max)())
16102 {
16103 write_number(static_cast<std::uint8_t>(0xda));
16104 write_number(static_cast<std::uint32_t>(j.m_data.m_value.binary->subtype()));
16105 }
16106 else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint64_t>::max)())
16107 {
16108 write_number(static_cast<std::uint8_t>(0xdb));
16109 write_number(static_cast<std::uint64_t>(j.m_data.m_value.binary->subtype()));
16110 }
16111 }
16112
16113 // step 1: write control byte and the binary array size
16114 const auto N = j.m_data.m_value.binary->size();
16115 if (N <= 0x17)
16116 {
16117 write_number(static_cast<std::uint8_t>(0x40 + N));
16118 }
16119 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
16120 {
16121 oa->write_character(to_char_type(0x58));
16122 write_number(static_cast<std::uint8_t>(N));
16123 }
16124 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
16125 {
16126 oa->write_character(to_char_type(0x59));
16127 write_number(static_cast<std::uint16_t>(N));
16128 }
16129 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
16130 {
16131 oa->write_character(to_char_type(0x5A));
16132 write_number(static_cast<std::uint32_t>(N));
16133 }
16134 // LCOV_EXCL_START
16135 else if (N <= (std::numeric_limits<std::uint64_t>::max)())
16136 {
16137 oa->write_character(to_char_type(0x5B));
16138 write_number(static_cast<std::uint64_t>(N));
16139 }
16140 // LCOV_EXCL_STOP
16141
16142 // step 2: write each element
16143 oa->write_characters(
16144 reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
16145 N);
16146
16147 break;
16148 }
16149
16150 case value_t::object:
16151 {
16152 // step 1: write control byte and the object size
16153 const auto N = j.m_data.m_value.object->size();
16154 if (N <= 0x17)
16155 {
16156 write_number(static_cast<std::uint8_t>(0xA0 + N));
16157 }
16158 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
16159 {
16160 oa->write_character(to_char_type(0xB8));
16161 write_number(static_cast<std::uint8_t>(N));
16162 }
16163 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
16164 {
16165 oa->write_character(to_char_type(0xB9));
16166 write_number(static_cast<std::uint16_t>(N));
16167 }
16168 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
16169 {
16170 oa->write_character(to_char_type(0xBA));
16171 write_number(static_cast<std::uint32_t>(N));
16172 }
16173 // LCOV_EXCL_START
16174 else if (N <= (std::numeric_limits<std::uint64_t>::max)())
16175 {
16176 oa->write_character(to_char_type(0xBB));
16177 write_number(static_cast<std::uint64_t>(N));
16178 }
16179 // LCOV_EXCL_STOP
16180
16181 // step 2: write each element
16182 for (const auto& el : *j.m_data.m_value.object)
16183 {
16184 write_cbor(el.first);
16185 write_cbor(el.second);
16186 }
16187 break;
16188 }
16189
16190 case value_t::discarded:
16191 default:
16192 break;
16193 }
16194 }
16195
16196 /*!
16197 @param[in] j JSON value to serialize
16198 */
16199 void write_msgpack(const BasicJsonType& j)
16200 {
16201 switch (j.type())
16202 {
16203 case value_t::null: // nil
16204 {
16205 oa->write_character(to_char_type(0xC0));
16206 break;
16207 }
16208
16209 case value_t::boolean: // true and false
16210 {
16211 oa->write_character(j.m_data.m_value.boolean
16212 ? to_char_type(0xC3)
16213 : to_char_type(0xC2));
16214 break;
16215 }
16216
16217 case value_t::number_integer:
16218 {
16219 if (j.m_data.m_value.number_integer >= 0)
16220 {
16221 // MessagePack does not differentiate between positive
16222 // signed integers and unsigned integers. Therefore, we used
16223 // the code from the value_t::number_unsigned case here.
16224 if (j.m_data.m_value.number_unsigned < 128)
16225 {
16226 // positive fixnum
16227 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
16228 }
16229 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
16230 {
16231 // uint 8
16232 oa->write_character(to_char_type(0xCC));
16233 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
16234 }
16235 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
16236 {
16237 // uint 16
16238 oa->write_character(to_char_type(0xCD));
16239 write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_integer));
16240 }
16241 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
16242 {
16243 // uint 32
16244 oa->write_character(to_char_type(0xCE));
16245 write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_integer));
16246 }
16247 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
16248 {
16249 // uint 64
16250 oa->write_character(to_char_type(0xCF));
16251 write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_integer));
16252 }
16253 }
16254 else
16255 {
16256 if (j.m_data.m_value.number_integer >= -32)
16257 {
16258 // negative fixnum
16259 write_number(static_cast<std::int8_t>(j.m_data.m_value.number_integer));
16260 }
16261 else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int8_t>::min)() &&
16262 j.m_data.m_value.number_integer <= (std::numeric_limits<std::int8_t>::max)())
16263 {
16264 // int 8
16265 oa->write_character(to_char_type(0xD0));
16266 write_number(static_cast<std::int8_t>(j.m_data.m_value.number_integer));
16267 }
16268 else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int16_t>::min)() &&
16269 j.m_data.m_value.number_integer <= (std::numeric_limits<std::int16_t>::max)())
16270 {
16271 // int 16
16272 oa->write_character(to_char_type(0xD1));
16273 write_number(static_cast<std::int16_t>(j.m_data.m_value.number_integer));
16274 }
16275 else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int32_t>::min)() &&
16276 j.m_data.m_value.number_integer <= (std::numeric_limits<std::int32_t>::max)())
16277 {
16278 // int 32
16279 oa->write_character(to_char_type(0xD2));
16280 write_number(static_cast<std::int32_t>(j.m_data.m_value.number_integer));
16281 }
16282 else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int64_t>::min)() &&
16283 j.m_data.m_value.number_integer <= (std::numeric_limits<std::int64_t>::max)())
16284 {
16285 // int 64
16286 oa->write_character(to_char_type(0xD3));
16287 write_number(static_cast<std::int64_t>(j.m_data.m_value.number_integer));
16288 }
16289 }
16290 break;
16291 }
16292
16293 case value_t::number_unsigned:
16294 {
16295 if (j.m_data.m_value.number_unsigned < 128)
16296 {
16297 // positive fixnum
16298 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
16299 }
16300 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
16301 {
16302 // uint 8
16303 oa->write_character(to_char_type(0xCC));
16304 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
16305 }
16306 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
16307 {
16308 // uint 16
16309 oa->write_character(to_char_type(0xCD));
16310 write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_integer));
16311 }
16312 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
16313 {
16314 // uint 32
16315 oa->write_character(to_char_type(0xCE));
16316 write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_integer));
16317 }
16318 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
16319 {
16320 // uint 64
16321 oa->write_character(to_char_type(0xCF));
16322 write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_integer));
16323 }
16324 break;
16325 }
16326
16327 case value_t::number_float:
16328 {
16329 write_compact_float(j.m_data.m_value.number_float, detail::input_format_t::msgpack);
16330 break;
16331 }
16332
16333 case value_t::string:
16334 {
16335 // step 1: write control byte and the string length
16336 const auto N = j.m_data.m_value.string->size();
16337 if (N <= 31)
16338 {
16339 // fixstr
16340 write_number(static_cast<std::uint8_t>(0xA0 | N));
16341 }
16342 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
16343 {
16344 // str 8
16345 oa->write_character(to_char_type(0xD9));
16346 write_number(static_cast<std::uint8_t>(N));
16347 }
16348 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
16349 {
16350 // str 16
16351 oa->write_character(to_char_type(0xDA));
16352 write_number(static_cast<std::uint16_t>(N));
16353 }
16354 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
16355 {
16356 // str 32
16357 oa->write_character(to_char_type(0xDB));
16358 write_number(static_cast<std::uint32_t>(N));
16359 }
16360
16361 // step 2: write the string
16362 oa->write_characters(
16363 reinterpret_cast<const CharType*>(j.m_data.m_value.string->c_str()),
16364 j.m_data.m_value.string->size());
16365 break;
16366 }
16367
16368 case value_t::array:
16369 {
16370 // step 1: write control byte and the array size
16371 const auto N = j.m_data.m_value.array->size();
16372 if (N <= 15)
16373 {
16374 // fixarray
16375 write_number(static_cast<std::uint8_t>(0x90 | N));
16376 }
16377 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
16378 {
16379 // array 16
16380 oa->write_character(to_char_type(0xDC));
16381 write_number(static_cast<std::uint16_t>(N));
16382 }
16383 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
16384 {
16385 // array 32
16386 oa->write_character(to_char_type(0xDD));
16387 write_number(static_cast<std::uint32_t>(N));
16388 }
16389
16390 // step 2: write each element
16391 for (const auto& el : *j.m_data.m_value.array)
16392 {
16393 write_msgpack(el);
16394 }
16395 break;
16396 }
16397
16398 case value_t::binary:
16399 {
16400 // step 0: determine if the binary type has a set subtype to
16401 // determine whether to use the ext or fixext types
16402 const bool use_ext = j.m_data.m_value.binary->has_subtype();
16403
16404 // step 1: write control byte and the byte string length
16405 const auto N = j.m_data.m_value.binary->size();
16406 if (N <= (std::numeric_limits<std::uint8_t>::max)())
16407 {
16408 std::uint8_t output_type{};
16409 bool fixed = true;
16410 if (use_ext)
16411 {
16412 switch (N)
16413 {
16414 case 1:
16415 output_type = 0xD4; // fixext 1
16416 break;
16417 case 2:
16418 output_type = 0xD5; // fixext 2
16419 break;
16420 case 4:
16421 output_type = 0xD6; // fixext 4
16422 break;
16423 case 8:
16424 output_type = 0xD7; // fixext 8
16425 break;
16426 case 16:
16427 output_type = 0xD8; // fixext 16
16428 break;
16429 default:
16430 output_type = 0xC7; // ext 8
16431 fixed = false;
16432 break;
16433 }
16434
16435 }
16436 else
16437 {
16438 output_type = 0xC4; // bin 8
16439 fixed = false;
16440 }
16441
16442 oa->write_character(to_char_type(output_type));
16443 if (!fixed)
16444 {
16445 write_number(static_cast<std::uint8_t>(N));
16446 }
16447 }
16448 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
16449 {
16450 const std::uint8_t output_type = use_ext
16451 ? 0xC8 // ext 16
16452 : 0xC5; // bin 16
16453
16454 oa->write_character(to_char_type(output_type));
16455 write_number(static_cast<std::uint16_t>(N));
16456 }
16457 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
16458 {
16459 const std::uint8_t output_type = use_ext
16460 ? 0xC9 // ext 32
16461 : 0xC6; // bin 32
16462
16463 oa->write_character(to_char_type(output_type));
16464 write_number(static_cast<std::uint32_t>(N));
16465 }
16466
16467 // step 1.5: if this is an ext type, write the subtype
16468 if (use_ext)
16469 {
16470 write_number(static_cast<std::int8_t>(j.m_data.m_value.binary->subtype()));
16471 }
16472
16473 // step 2: write the byte string
16474 oa->write_characters(
16475 reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
16476 N);
16477
16478 break;
16479 }
16480
16481 case value_t::object:
16482 {
16483 // step 1: write control byte and the object size
16484 const auto N = j.m_data.m_value.object->size();
16485 if (N <= 15)
16486 {
16487 // fixmap
16488 write_number(static_cast<std::uint8_t>(0x80 | (N & 0xF)));
16489 }
16490 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
16491 {
16492 // map 16
16493 oa->write_character(to_char_type(0xDE));
16494 write_number(static_cast<std::uint16_t>(N));
16495 }
16496 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
16497 {
16498 // map 32
16499 oa->write_character(to_char_type(0xDF));
16500 write_number(static_cast<std::uint32_t>(N));
16501 }
16502
16503 // step 2: write each element
16504 for (const auto& el : *j.m_data.m_value.object)
16505 {
16506 write_msgpack(el.first);
16507 write_msgpack(el.second);
16508 }
16509 break;
16510 }
16511
16512 case value_t::discarded:
16513 default:
16514 break;
16515 }
16516 }
16517
16518 /*!
16519 @param[in] j JSON value to serialize
16520 @param[in] use_count whether to use '#' prefixes (optimized format)
16521 @param[in] use_type whether to use '$' prefixes (optimized format)
16522 @param[in] add_prefix whether prefixes need to be used for this value
16523 @param[in] use_bjdata whether write in BJData format, default is false
16524 @param[in] bjdata_version which BJData version to use, default is draft2
16525 */
16526 void write_ubjson(const BasicJsonType& j, const bool use_count,
16527 const bool use_type, const bool add_prefix = true,
16528 const bool use_bjdata = false, const bjdata_version_t bjdata_version = bjdata_version_t::draft2)
16529 {
16530 const bool bjdata_draft3 = use_bjdata && bjdata_version == bjdata_version_t::draft3;
16531
16532 switch (j.type())
16533 {
16534 case value_t::null:
16535 {
16536 if (add_prefix)
16537 {
16538 oa->write_character(to_char_type('Z'));
16539 }
16540 break;
16541 }
16542
16543 case value_t::boolean:
16544 {
16545 if (add_prefix)
16546 {
16547 oa->write_character(j.m_data.m_value.boolean
16548 ? to_char_type('T')
16549 : to_char_type('F'));
16550 }
16551 break;
16552 }
16553
16554 case value_t::number_integer:
16555 {
16556 write_number_with_ubjson_prefix(j.m_data.m_value.number_integer, add_prefix, use_bjdata);
16557 break;
16558 }
16559
16560 case value_t::number_unsigned:
16561 {
16562 write_number_with_ubjson_prefix(j.m_data.m_value.number_unsigned, add_prefix, use_bjdata);
16563 break;
16564 }
16565
16566 case value_t::number_float:
16567 {
16568 write_number_with_ubjson_prefix(j.m_data.m_value.number_float, add_prefix, use_bjdata);
16569 break;
16570 }
16571
16572 case value_t::string:
16573 {
16574 if (add_prefix)
16575 {
16576 oa->write_character(to_char_type('S'));
16577 }
16578 write_number_with_ubjson_prefix(j.m_data.m_value.string->size(), true, use_bjdata);
16579 oa->write_characters(
16580 reinterpret_cast<const CharType*>(j.m_data.m_value.string->c_str()),
16581 j.m_data.m_value.string->size());
16582 break;
16583 }
16584
16585 case value_t::array:
16586 {
16587 if (add_prefix)
16588 {
16589 oa->write_character(to_char_type('['));
16590 }
16591
16592 bool prefix_required = true;
16593 if (use_type && !j.m_data.m_value.array->empty())
16594 {
16595 JSON_ASSERT(use_count);
16596 const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata);
16597 const bool same_prefix = std::all_of(j.begin() + 1, j.end(),
16598 [this, first_prefix, use_bjdata](const BasicJsonType & v)
16599 {
16600 return ubjson_prefix(v, use_bjdata) == first_prefix;
16601 });
16602
16603 std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
16604
16605 if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
16606 {
16607 prefix_required = false;
16608 oa->write_character(to_char_type('$'));
16609 oa->write_character(first_prefix);
16610 }
16611 }
16612
16613 if (use_count)
16614 {
16615 oa->write_character(to_char_type('#'));
16616 write_number_with_ubjson_prefix(j.m_data.m_value.array->size(), true, use_bjdata);
16617 }
16618
16619 for (const auto& el : *j.m_data.m_value.array)
16620 {
16621 write_ubjson(el, use_count, use_type, prefix_required, use_bjdata, bjdata_version);
16622 }
16623
16624 if (!use_count)
16625 {
16626 oa->write_character(to_char_type(']'));
16627 }
16628
16629 break;
16630 }
16631
16632 case value_t::binary:
16633 {
16634 if (add_prefix)
16635 {
16636 oa->write_character(to_char_type('['));
16637 }
16638
16639 if (use_type && (bjdata_draft3 || !j.m_data.m_value.binary->empty()))
16640 {
16641 JSON_ASSERT(use_count);
16642 oa->write_character(to_char_type('$'));
16643 oa->write_character(bjdata_draft3 ? 'B' : 'U');
16644 }
16645
16646 if (use_count)
16647 {
16648 oa->write_character(to_char_type('#'));
16649 write_number_with_ubjson_prefix(j.m_data.m_value.binary->size(), true, use_bjdata);
16650 }
16651
16652 if (use_type)
16653 {
16654 oa->write_characters(
16655 reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
16656 j.m_data.m_value.binary->size());
16657 }
16658 else
16659 {
16660 for (size_t i = 0; i < j.m_data.m_value.binary->size(); ++i)
16661 {
16662 oa->write_character(to_char_type(bjdata_draft3 ? 'B' : 'U'));
16663 oa->write_character(j.m_data.m_value.binary->data()[i]);
16664 }
16665 }
16666
16667 if (!use_count)
16668 {
16669 oa->write_character(to_char_type(']'));
16670 }
16671
16672 break;
16673 }
16674
16675 case value_t::object:
16676 {
16677 if (use_bjdata && j.m_data.m_value.object->size() == 3 && j.m_data.m_value.object->find("_ArrayType_") != j.m_data.m_value.object->end() && j.m_data.m_value.object->find("_ArraySize_") != j.m_data.m_value.object->end() && j.m_data.m_value.object->find("_ArrayData_") != j.m_data.m_value.object->end())
16678 {
16679 if (!write_bjdata_ndarray(*j.m_data.m_value.object, use_count, use_type, bjdata_version)) // decode bjdata ndarray in the JData format (https://github.com/NeuroJSON/jdata)
16680 {
16681 break;
16682 }
16683 }
16684
16685 if (add_prefix)
16686 {
16687 oa->write_character(to_char_type('{'));
16688 }
16689
16690 bool prefix_required = true;
16691 if (use_type && !j.m_data.m_value.object->empty())
16692 {
16693 JSON_ASSERT(use_count);
16694 const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata);
16695 const bool same_prefix = std::all_of(j.begin(), j.end(),
16696 [this, first_prefix, use_bjdata](const BasicJsonType & v)
16697 {
16698 return ubjson_prefix(v, use_bjdata) == first_prefix;
16699 });
16700
16701 std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
16702
16703 if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
16704 {
16705 prefix_required = false;
16706 oa->write_character(to_char_type('$'));
16707 oa->write_character(first_prefix);
16708 }
16709 }
16710
16711 if (use_count)
16712 {
16713 oa->write_character(to_char_type('#'));
16714 write_number_with_ubjson_prefix(j.m_data.m_value.object->size(), true, use_bjdata);
16715 }
16716
16717 for (const auto& el : *j.m_data.m_value.object)
16718 {
16719 write_number_with_ubjson_prefix(el.first.size(), true, use_bjdata);
16720 oa->write_characters(
16721 reinterpret_cast<const CharType*>(el.first.c_str()),
16722 el.first.size());
16723 write_ubjson(el.second, use_count, use_type, prefix_required, use_bjdata, bjdata_version);
16724 }
16725
16726 if (!use_count)
16727 {
16728 oa->write_character(to_char_type('}'));
16729 }
16730
16731 break;
16732 }
16733
16734 case value_t::discarded:
16735 default:
16736 break;
16737 }
16738 }
16739
16740 private:
16741 //////////
16742 // BSON //
16743 //////////
16744
16745 /*!
16746 @return The size of a BSON document entry header, including the id marker
16747 and the entry name size (and its null-terminator).
16748 */
16749 static std::size_t calc_bson_entry_header_size(const string_t& name, const BasicJsonType& j)
16750 {
16751 const auto it = name.find(static_cast<typename string_t::value_type>(0));
16752 if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos))
16753 {
16754 JSON_THROW(out_of_range::create(409, concat("BSON key cannot contain code point U+0000 (at byte ", std::to_string(it), ")"), &j));
16755 static_cast<void>(j);
16756 }
16757
16758 return /*id*/ 1ul + name.size() + /*zero-terminator*/1u;
16759 }
16760
16761 /*!
16762 @brief Writes the given @a element_type and @a name to the output adapter
16763 */
16764 void write_bson_entry_header(const string_t& name,
16765 const std::uint8_t element_type)
16766 {
16767 oa->write_character(to_char_type(element_type)); // boolean
16768 oa->write_characters(
16769 reinterpret_cast<const CharType*>(name.c_str()),
16770 name.size() + 1u);
16771 }
16772
16773 /*!
16774 @brief Writes a BSON element with key @a name and boolean value @a value
16775 */
16776 void write_bson_boolean(const string_t& name,
16777 const bool value)
16778 {
16779 write_bson_entry_header(name, 0x08);
16780 oa->write_character(value ? to_char_type(0x01) : to_char_type(0x00));
16781 }
16782
16783 /*!
16784 @brief Writes a BSON element with key @a name and double value @a value
16785 */
16786 void write_bson_double(const string_t& name,
16787 const double value)
16788 {
16789 write_bson_entry_header(name, 0x01);
16790 write_number<double>(value, true);
16791 }
16792
16793 /*!
16794 @return The size of the BSON-encoded string in @a value
16795 */
16796 static std::size_t calc_bson_string_size(const string_t& value)
16797 {
16798 return sizeof(std::int32_t) + value.size() + 1ul;
16799 }
16800
16801 /*!
16802 @brief Writes a BSON element with key @a name and string value @a value
16803 */
16804 void write_bson_string(const string_t& name,
16805 const string_t& value)
16806 {
16807 write_bson_entry_header(name, 0x02);
16808
16809 write_number<std::int32_t>(static_cast<std::int32_t>(value.size() + 1ul), true);
16810 oa->write_characters(
16811 reinterpret_cast<const CharType*>(value.c_str()),
16812 value.size() + 1);
16813 }
16814
16815 /*!
16816 @brief Writes a BSON element with key @a name and null value
16817 */
16818 void write_bson_null(const string_t& name)
16819 {
16820 write_bson_entry_header(name, 0x0A);
16821 }
16822
16823 /*!
16824 @return The size of the BSON-encoded integer @a value
16825 */
16826 static std::size_t calc_bson_integer_size(const std::int64_t value)
16827 {
16828 return (std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)()
16829 ? sizeof(std::int32_t)
16830 : sizeof(std::int64_t);
16831 }
16832
16833 /*!
16834 @brief Writes a BSON element with key @a name and integer @a value
16835 */
16836 void write_bson_integer(const string_t& name,
16837 const std::int64_t value)
16838 {
16839 if ((std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)())
16840 {
16841 write_bson_entry_header(name, 0x10); // int32
16842 write_number<std::int32_t>(static_cast<std::int32_t>(value), true);
16843 }
16844 else
16845 {
16846 write_bson_entry_header(name, 0x12); // int64
16847 write_number<std::int64_t>(static_cast<std::int64_t>(value), true);
16848 }
16849 }
16850
16851 /*!
16852 @return The size of the BSON-encoded unsigned integer in @a j
16853 */
16854 static constexpr std::size_t calc_bson_unsigned_size(const std::uint64_t value) noexcept
16855 {
16856 return (value <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
16857 ? sizeof(std::int32_t)
16858 : sizeof(std::int64_t);
16859 }
16860
16861 /*!
16862 @brief Writes a BSON element with key @a name and unsigned @a value
16863 */
16864 void write_bson_unsigned(const string_t& name,
16865 const BasicJsonType& j)
16866 {
16867 if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
16868 {
16869 write_bson_entry_header(name, 0x10 /* int32 */);
16870 write_number<std::int32_t>(static_cast<std::int32_t>(j.m_data.m_value.number_unsigned), true);
16871 }
16872 else if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
16873 {
16874 write_bson_entry_header(name, 0x12 /* int64 */);
16875 write_number<std::int64_t>(static_cast<std::int64_t>(j.m_data.m_value.number_unsigned), true);
16876 }
16877 else
16878 {
16879 write_bson_entry_header(name, 0x11 /* uint64 */);
16880 write_number<std::uint64_t>(static_cast<std::uint64_t>(j.m_data.m_value.number_unsigned), true);
16881 }
16882 }
16883
16884 /*!
16885 @brief Writes a BSON element with key @a name and object @a value
16886 */
16887 void write_bson_object_entry(const string_t& name,
16888 const typename BasicJsonType::object_t& value)
16889 {
16890 write_bson_entry_header(name, 0x03); // object
16891 write_bson_object(value);
16892 }
16893
16894 /*!
16895 @return The size of the BSON-encoded array @a value
16896 */
16897 static std::size_t calc_bson_array_size(const typename BasicJsonType::array_t& value)
16898 {
16899 std::size_t array_index = 0ul;
16900
16901 const std::size_t embedded_document_size = std::accumulate(std::begin(value), std::end(value), static_cast<std::size_t>(0), [&array_index](std::size_t result, const typename BasicJsonType::array_t::value_type & el)
16902 {
16903 return result + calc_bson_element_size(std::to_string(array_index++), el);
16904 });
16905
16906 return sizeof(std::int32_t) + embedded_document_size + 1ul;
16907 }
16908
16909 /*!
16910 @return The size of the BSON-encoded binary array @a value
16911 */
16912 static std::size_t calc_bson_binary_size(const typename BasicJsonType::binary_t& value)
16913 {
16914 return sizeof(std::int32_t) + value.size() + 1ul;
16915 }
16916
16917 /*!
16918 @brief Writes a BSON element with key @a name and array @a value
16919 */
16920 void write_bson_array(const string_t& name,
16921 const typename BasicJsonType::array_t& value)
16922 {
16923 write_bson_entry_header(name, 0x04); // array
16924 write_number<std::int32_t>(static_cast<std::int32_t>(calc_bson_array_size(value)), true);
16925
16926 std::size_t array_index = 0ul;
16927
16928 for (const auto& el : value)
16929 {
16930 write_bson_element(std::to_string(array_index++), el);
16931 }
16932
16933 oa->write_character(to_char_type(0x00));
16934 }
16935
16936 /*!
16937 @brief Writes a BSON element with key @a name and binary value @a value
16938 */
16939 void write_bson_binary(const string_t& name,
16940 const binary_t& value)
16941 {
16942 write_bson_entry_header(name, 0x05);
16943
16944 write_number<std::int32_t>(static_cast<std::int32_t>(value.size()), true);
16945 write_number(value.has_subtype() ? static_cast<std::uint8_t>(value.subtype()) : static_cast<std::uint8_t>(0x00));
16946
16947 oa->write_characters(reinterpret_cast<const CharType*>(value.data()), value.size());
16948 }
16949
16950 /*!
16951 @brief Calculates the size necessary to serialize the JSON value @a j with its @a name
16952 @return The calculated size for the BSON document entry for @a j with the given @a name.
16953 */
16954 static std::size_t calc_bson_element_size(const string_t& name,
16955 const BasicJsonType& j)
16956 {
16957 const auto header_size = calc_bson_entry_header_size(name, j);
16958 switch (j.type())
16959 {
16960 case value_t::object:
16961 return header_size + calc_bson_object_size(*j.m_data.m_value.object);
16962
16963 case value_t::array:
16964 return header_size + calc_bson_array_size(*j.m_data.m_value.array);
16965
16966 case value_t::binary:
16967 return header_size + calc_bson_binary_size(*j.m_data.m_value.binary);
16968
16969 case value_t::boolean:
16970 return header_size + 1ul;
16971
16972 case value_t::number_float:
16973 return header_size + 8ul;
16974
16975 case value_t::number_integer:
16976 return header_size + calc_bson_integer_size(j.m_data.m_value.number_integer);
16977
16978 case value_t::number_unsigned:
16979 return header_size + calc_bson_unsigned_size(j.m_data.m_value.number_unsigned);
16980
16981 case value_t::string:
16982 return header_size + calc_bson_string_size(*j.m_data.m_value.string);
16983
16984 case value_t::null:
16985 return header_size + 0ul;
16986
16987 // LCOV_EXCL_START
16988 case value_t::discarded:
16989 default:
16990 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert)
16991 return 0ul;
16992 // LCOV_EXCL_STOP
16993 }
16994 }
16995
16996 /*!
16997 @brief Serializes the JSON value @a j to BSON and associates it with the
16998 key @a name.
16999 @param name The name to associate with the JSON entity @a j within the
17000 current BSON document
17001 */
17002 void write_bson_element(const string_t& name,
17003 const BasicJsonType& j)
17004 {
17005 switch (j.type())
17006 {
17007 case value_t::object:
17008 return write_bson_object_entry(name, *j.m_data.m_value.object);
17009
17010 case value_t::array:
17011 return write_bson_array(name, *j.m_data.m_value.array);
17012
17013 case value_t::binary:
17014 return write_bson_binary(name, *j.m_data.m_value.binary);
17015
17016 case value_t::boolean:
17017 return write_bson_boolean(name, j.m_data.m_value.boolean);
17018
17019 case value_t::number_float:
17020 return write_bson_double(name, j.m_data.m_value.number_float);
17021
17022 case value_t::number_integer:
17023 return write_bson_integer(name, j.m_data.m_value.number_integer);
17024
17025 case value_t::number_unsigned:
17026 return write_bson_unsigned(name, j);
17027
17028 case value_t::string:
17029 return write_bson_string(name, *j.m_data.m_value.string);
17030
17031 case value_t::null:
17032 return write_bson_null(name);
17033
17034 // LCOV_EXCL_START
17035 case value_t::discarded:
17036 default:
17037 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert)
17038 return;
17039 // LCOV_EXCL_STOP
17040 }
17041 }
17042
17043 /*!
17044 @brief Calculates the size of the BSON serialization of the given
17045 JSON-object @a j.
17046 @param[in] value JSON value to serialize
17047 @pre value.type() == value_t::object
17048 */
17049 static std::size_t calc_bson_object_size(const typename BasicJsonType::object_t& value)
17050 {
17051 const std::size_t document_size = std::accumulate(value.begin(), value.end(), static_cast<std::size_t>(0),
17052 [](size_t result, const typename BasicJsonType::object_t::value_type & el)
17053 {
17054 return result += calc_bson_element_size(el.first, el.second);
17055 });
17056
17057 return sizeof(std::int32_t) + document_size + 1ul;
17058 }
17059
17060 /*!
17061 @param[in] value JSON value to serialize
17062 @pre value.type() == value_t::object
17063 */
17064 void write_bson_object(const typename BasicJsonType::object_t& value)
17065 {
17066 write_number<std::int32_t>(static_cast<std::int32_t>(calc_bson_object_size(value)), true);
17067
17068 for (const auto& el : value)
17069 {
17070 write_bson_element(el.first, el.second);
17071 }
17072
17073 oa->write_character(to_char_type(0x00));
17074 }
17075
17076 //////////
17077 // CBOR //
17078 //////////
17079
17080 static constexpr CharType get_cbor_float_prefix(float /*unused*/)
17081 {
17082 return to_char_type(0xFA); // Single-Precision Float
17083 }
17084
17085 static constexpr CharType get_cbor_float_prefix(double /*unused*/)
17086 {
17087 return to_char_type(0xFB); // Double-Precision Float
17088 }
17089
17090 /////////////
17091 // MsgPack //
17092 /////////////
17093
17094 static constexpr CharType get_msgpack_float_prefix(float /*unused*/)
17095 {
17096 return to_char_type(0xCA); // float 32
17097 }
17098
17099 static constexpr CharType get_msgpack_float_prefix(double /*unused*/)
17100 {
17101 return to_char_type(0xCB); // float 64
17102 }
17103
17104 ////////////
17105 // UBJSON //
17106 ////////////
17107
17108 // UBJSON: write number (floating point)
17109 template<typename NumberType, typename std::enable_if<
17110 std::is_floating_point<NumberType>::value, int>::type = 0>
17111 void write_number_with_ubjson_prefix(const NumberType n,
17112 const bool add_prefix,
17113 const bool use_bjdata)
17114 {
17115 if (add_prefix)
17116 {
17117 oa->write_character(get_ubjson_float_prefix(n));
17118 }
17119 write_number(n, use_bjdata);
17120 }
17121
17122 // UBJSON: write number (unsigned integer)
17123 template<typename NumberType, typename std::enable_if<
17124 std::is_unsigned<NumberType>::value, int>::type = 0>
17125 void write_number_with_ubjson_prefix(const NumberType n,
17126 const bool add_prefix,
17127 const bool use_bjdata)
17128 {
17129 if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
17130 {
17131 if (add_prefix)
17132 {
17133 oa->write_character(to_char_type('i')); // int8
17134 }
17135 write_number(static_cast<std::uint8_t>(n), use_bjdata);
17136 }
17137 else if (n <= (std::numeric_limits<std::uint8_t>::max)())
17138 {
17139 if (add_prefix)
17140 {
17141 oa->write_character(to_char_type('U')); // uint8
17142 }
17143 write_number(static_cast<std::uint8_t>(n), use_bjdata);
17144 }
17145 else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
17146 {
17147 if (add_prefix)
17148 {
17149 oa->write_character(to_char_type('I')); // int16
17150 }
17151 write_number(static_cast<std::int16_t>(n), use_bjdata);
17152 }
17153 else if (use_bjdata && n <= static_cast<uint64_t>((std::numeric_limits<uint16_t>::max)()))
17154 {
17155 if (add_prefix)
17156 {
17157 oa->write_character(to_char_type('u')); // uint16 - bjdata only
17158 }
17159 write_number(static_cast<std::uint16_t>(n), use_bjdata);
17160 }
17161 else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
17162 {
17163 if (add_prefix)
17164 {
17165 oa->write_character(to_char_type('l')); // int32
17166 }
17167 write_number(static_cast<std::int32_t>(n), use_bjdata);
17168 }
17169 else if (use_bjdata && n <= static_cast<uint64_t>((std::numeric_limits<uint32_t>::max)()))
17170 {
17171 if (add_prefix)
17172 {
17173 oa->write_character(to_char_type('m')); // uint32 - bjdata only
17174 }
17175 write_number(static_cast<std::uint32_t>(n), use_bjdata);
17176 }
17177 else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
17178 {
17179 if (add_prefix)
17180 {
17181 oa->write_character(to_char_type('L')); // int64
17182 }
17183 write_number(static_cast<std::int64_t>(n), use_bjdata);
17184 }
17185 else if (use_bjdata && n <= (std::numeric_limits<uint64_t>::max)())
17186 {
17187 if (add_prefix)
17188 {
17189 oa->write_character(to_char_type('M')); // uint64 - bjdata only
17190 }
17191 write_number(static_cast<std::uint64_t>(n), use_bjdata);
17192 }
17193 else
17194 {
17195 if (add_prefix)
17196 {
17197 oa->write_character(to_char_type('H')); // high-precision number
17198 }
17199
17200 const auto number = BasicJsonType(n).dump();
17201 write_number_with_ubjson_prefix(number.size(), true, use_bjdata);
17202 for (std::size_t i = 0; i < number.size(); ++i)
17203 {
17204 oa->write_character(to_char_type(static_cast<std::uint8_t>(number[i])));
17205 }
17206 }
17207 }
17208
17209 // UBJSON: write number (signed integer)
17210 template < typename NumberType, typename std::enable_if <
17211 std::is_signed<NumberType>::value&&
17212 !std::is_floating_point<NumberType>::value, int >::type = 0 >
17213 void write_number_with_ubjson_prefix(const NumberType n,
17214 const bool add_prefix,
17215 const bool use_bjdata)
17216 {
17217 if ((std::numeric_limits<std::int8_t>::min)() <= n && n <= (std::numeric_limits<std::int8_t>::max)())
17218 {
17219 if (add_prefix)
17220 {
17221 oa->write_character(to_char_type('i')); // int8
17222 }
17223 write_number(static_cast<std::int8_t>(n), use_bjdata);
17224 }
17225 else if (static_cast<std::int64_t>((std::numeric_limits<std::uint8_t>::min)()) <= n && n <= static_cast<std::int64_t>((std::numeric_limits<std::uint8_t>::max)()))
17226 {
17227 if (add_prefix)
17228 {
17229 oa->write_character(to_char_type('U')); // uint8
17230 }
17231 write_number(static_cast<std::uint8_t>(n), use_bjdata);
17232 }
17233 else if ((std::numeric_limits<std::int16_t>::min)() <= n && n <= (std::numeric_limits<std::int16_t>::max)())
17234 {
17235 if (add_prefix)
17236 {
17237 oa->write_character(to_char_type('I')); // int16
17238 }
17239 write_number(static_cast<std::int16_t>(n), use_bjdata);
17240 }
17241 else if (use_bjdata && (static_cast<std::int64_t>((std::numeric_limits<std::uint16_t>::min)()) <= n && n <= static_cast<std::int64_t>((std::numeric_limits<std::uint16_t>::max)())))
17242 {
17243 if (add_prefix)
17244 {
17245 oa->write_character(to_char_type('u')); // uint16 - bjdata only
17246 }
17247 write_number(static_cast<uint16_t>(n), use_bjdata);
17248 }
17249 else if ((std::numeric_limits<std::int32_t>::min)() <= n && n <= (std::numeric_limits<std::int32_t>::max)())
17250 {
17251 if (add_prefix)
17252 {
17253 oa->write_character(to_char_type('l')); // int32
17254 }
17255 write_number(static_cast<std::int32_t>(n), use_bjdata);
17256 }
17257 else if (use_bjdata && (static_cast<std::int64_t>((std::numeric_limits<std::uint32_t>::min)()) <= n && n <= static_cast<std::int64_t>((std::numeric_limits<std::uint32_t>::max)())))
17258 {
17259 if (add_prefix)
17260 {
17261 oa->write_character(to_char_type('m')); // uint32 - bjdata only
17262 }
17263 write_number(static_cast<uint32_t>(n), use_bjdata);
17264 }
17265 else if ((std::numeric_limits<std::int64_t>::min)() <= n && n <= (std::numeric_limits<std::int64_t>::max)())
17266 {
17267 if (add_prefix)
17268 {
17269 oa->write_character(to_char_type('L')); // int64
17270 }
17271 write_number(static_cast<std::int64_t>(n), use_bjdata);
17272 }
17273 // LCOV_EXCL_START
17274 else
17275 {
17276 if (add_prefix)
17277 {
17278 oa->write_character(to_char_type('H')); // high-precision number
17279 }
17280
17281 const auto number = BasicJsonType(n).dump();
17282 write_number_with_ubjson_prefix(number.size(), true, use_bjdata);
17283 for (std::size_t i = 0; i < number.size(); ++i)
17284 {
17285 oa->write_character(to_char_type(static_cast<std::uint8_t>(number[i])));
17286 }
17287 }
17288 // LCOV_EXCL_STOP
17289 }
17290
17291 /*!
17292 @brief determine the type prefix of container values
17293 */
17294 CharType ubjson_prefix(const BasicJsonType& j, const bool use_bjdata) const noexcept
17295 {
17296 switch (j.type())
17297 {
17298 case value_t::null:
17299 return 'Z';
17300
17301 case value_t::boolean:
17302 return j.m_data.m_value.boolean ? 'T' : 'F';
17303
17304 case value_t::number_integer:
17305 {
17306 if ((std::numeric_limits<std::int8_t>::min)() <= j.m_data.m_value.number_integer && j.m_data.m_value.number_integer <= (std::numeric_limits<std::int8_t>::max)())
17307 {
17308 return 'i';
17309 }
17310 if ((std::numeric_limits<std::uint8_t>::min)() <= j.m_data.m_value.number_integer && j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint8_t>::max)())
17311 {
17312 return 'U';
17313 }
17314 if ((std::numeric_limits<std::int16_t>::min)() <= j.m_data.m_value.number_integer && j.m_data.m_value.number_integer <= (std::numeric_limits<std::int16_t>::max)())
17315 {
17316 return 'I';
17317 }
17318 if (use_bjdata && ((std::numeric_limits<std::uint16_t>::min)() <= j.m_data.m_value.number_integer && j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint16_t>::max)()))
17319 {
17320 return 'u';
17321 }
17322 if ((std::numeric_limits<std::int32_t>::min)() <= j.m_data.m_value.number_integer && j.m_data.m_value.number_integer <= (std::numeric_limits<std::int32_t>::max)())
17323 {
17324 return 'l';
17325 }
17326 if (use_bjdata && ((std::numeric_limits<std::uint32_t>::min)() <= j.m_data.m_value.number_integer && j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint32_t>::max)()))
17327 {
17328 return 'm';
17329 }
17330 if ((std::numeric_limits<std::int64_t>::min)() <= j.m_data.m_value.number_integer && j.m_data.m_value.number_integer <= (std::numeric_limits<std::int64_t>::max)())
17331 {
17332 return 'L';
17333 }
17334 // anything else is treated as high-precision number
17335 return 'H'; // LCOV_EXCL_LINE
17336 }
17337
17338 case value_t::number_unsigned:
17339 {
17340 if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
17341 {
17342 return 'i';
17343 }
17344 if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint8_t>::max)()))
17345 {
17346 return 'U';
17347 }
17348 if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
17349 {
17350 return 'I';
17351 }
17352 if (use_bjdata && j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint16_t>::max)()))
17353 {
17354 return 'u';
17355 }
17356 if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
17357 {
17358 return 'l';
17359 }
17360 if (use_bjdata && j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint32_t>::max)()))
17361 {
17362 return 'm';
17363 }
17364 if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
17365 {
17366 return 'L';
17367 }
17368 if (use_bjdata && j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
17369 {
17370 return 'M';
17371 }
17372 // anything else is treated as high-precision number
17373 return 'H'; // LCOV_EXCL_LINE
17374 }
17375
17376 case value_t::number_float:
17377 return get_ubjson_float_prefix(j.m_data.m_value.number_float);
17378
17379 case value_t::string:
17380 return 'S';
17381
17382 case value_t::array: // fallthrough
17383 case value_t::binary:
17384 return '[';
17385
17386 case value_t::object:
17387 return '{';
17388
17389 case value_t::discarded:
17390 default: // discarded values
17391 return 'N';
17392 }
17393 }
17394
17395 static constexpr CharType get_ubjson_float_prefix(float /*unused*/)
17396 {
17397 return 'd'; // float 32
17398 }
17399
17400 static constexpr CharType get_ubjson_float_prefix(double /*unused*/)
17401 {
17402 return 'D'; // float 64
17403 }
17404
17405 /*!
17406 @return false if the object is successfully converted to a bjdata ndarray, true if the type or size is invalid
17407 */
17408 bool write_bjdata_ndarray(const typename BasicJsonType::object_t& value, const bool use_count, const bool use_type, const bjdata_version_t bjdata_version)
17409 {
17410 std::map<string_t, CharType> bjdtype = {{"uint8", 'U'}, {"int8", 'i'}, {"uint16", 'u'}, {"int16", 'I'},
17411 {"uint32", 'm'}, {"int32", 'l'}, {"uint64", 'M'}, {"int64", 'L'}, {"single", 'd'}, {"double", 'D'},
17412 {"char", 'C'}, {"byte", 'B'}
17413 };
17414
17415 string_t key = "_ArrayType_";
17416 auto it = bjdtype.find(static_cast<string_t>(value.at(key)));
17417 if (it == bjdtype.end())
17418 {
17419 return true;
17420 }
17421 CharType dtype = it->second;
17422
17423 key = "_ArraySize_";
17424 std::size_t len = (value.at(key).empty() ? 0 : 1);
17425 for (const auto& el : value.at(key))
17426 {
17427 len *= static_cast<std::size_t>(el.m_data.m_value.number_unsigned);
17428 }
17429
17430 key = "_ArrayData_";
17431 if (value.at(key).size() != len)
17432 {
17433 return true;
17434 }
17435
17436 oa->write_character('[');
17437 oa->write_character('$');
17438 oa->write_character(dtype);
17439 oa->write_character('#');
17440
17441 key = "_ArraySize_";
17442 write_ubjson(value.at(key), use_count, use_type, true, true, bjdata_version);
17443
17444 key = "_ArrayData_";
17445 if (dtype == 'U' || dtype == 'C' || dtype == 'B')
17446 {
17447 for (const auto& el : value.at(key))
17448 {
17449 write_number(static_cast<std::uint8_t>(el.m_data.m_value.number_unsigned), true);
17450 }
17451 }
17452 else if (dtype == 'i')
17453 {
17454 for (const auto& el : value.at(key))
17455 {
17456 write_number(static_cast<std::int8_t>(el.m_data.m_value.number_integer), true);
17457 }
17458 }
17459 else if (dtype == 'u')
17460 {
17461 for (const auto& el : value.at(key))
17462 {
17463 write_number(static_cast<std::uint16_t>(el.m_data.m_value.number_unsigned), true);
17464 }
17465 }
17466 else if (dtype == 'I')
17467 {
17468 for (const auto& el : value.at(key))
17469 {
17470 write_number(static_cast<std::int16_t>(el.m_data.m_value.number_integer), true);
17471 }
17472 }
17473 else if (dtype == 'm')
17474 {
17475 for (const auto& el : value.at(key))
17476 {
17477 write_number(static_cast<std::uint32_t>(el.m_data.m_value.number_unsigned), true);
17478 }
17479 }
17480 else if (dtype == 'l')
17481 {
17482 for (const auto& el : value.at(key))
17483 {
17484 write_number(static_cast<std::int32_t>(el.m_data.m_value.number_integer), true);
17485 }
17486 }
17487 else if (dtype == 'M')
17488 {
17489 for (const auto& el : value.at(key))
17490 {
17491 write_number(static_cast<std::uint64_t>(el.m_data.m_value.number_unsigned), true);
17492 }
17493 }
17494 else if (dtype == 'L')
17495 {
17496 for (const auto& el : value.at(key))
17497 {
17498 write_number(static_cast<std::int64_t>(el.m_data.m_value.number_integer), true);
17499 }
17500 }
17501 else if (dtype == 'd')
17502 {
17503 for (const auto& el : value.at(key))
17504 {
17505 write_number(static_cast<float>(el.m_data.m_value.number_float), true);
17506 }
17507 }
17508 else if (dtype == 'D')
17509 {
17510 for (const auto& el : value.at(key))
17511 {
17512 write_number(static_cast<double>(el.m_data.m_value.number_float), true);
17513 }
17514 }
17515 return false;
17516 }
17517
17518 ///////////////////////
17519 // Utility functions //
17520 ///////////////////////
17521
17522 /*
17523 @brief write a number to output input
17524 @param[in] n number of type @a NumberType
17525 @param[in] OutputIsLittleEndian Set to true if output data is
17526 required to be little endian
17527 @tparam NumberType the type of the number
17528
17529 @note This function needs to respect the system's endianness, because bytes
17530 in CBOR, MessagePack, and UBJSON are stored in network order (big
17531 endian) and therefore need reordering on little endian systems.
17532 On the other hand, BSON and BJData use little endian and should reorder
17533 on big endian systems.
17534 */
17535 template<typename NumberType>
17536 void write_number(const NumberType n, const bool OutputIsLittleEndian = false)
17537 {
17538 // step 1: write number to array of length NumberType
17539 std::array<CharType, sizeof(NumberType)> vec{};
17540 std::memcpy(vec.data(), &n, sizeof(NumberType));
17541
17542 // step 2: write array to output (with possible reordering)
17543 if (is_little_endian != OutputIsLittleEndian)
17544 {
17545 // reverse byte order prior to conversion if necessary
17546 std::reverse(vec.begin(), vec.end());
17547 }
17548
17549 oa->write_characters(vec.data(), sizeof(NumberType));
17550 }
17551
17552 void write_compact_float(const number_float_t n, detail::input_format_t format)
17553 {
17554 #ifdef __GNUC__
17555 #pragma GCC diagnostic push
17556 #pragma GCC diagnostic ignored "-Wfloat-equal"
17557 #endif
17558 if (static_cast<double>(n) >= static_cast<double>(std::numeric_limits<float>::lowest()) &&
17559 static_cast<double>(n) <= static_cast<double>((std::numeric_limits<float>::max)()) &&
17560 static_cast<double>(static_cast<float>(n)) == static_cast<double>(n))
17561 {
17562 oa->write_character(format == detail::input_format_t::cbor
17563 ? get_cbor_float_prefix(static_cast<float>(n))
17564 : get_msgpack_float_prefix(static_cast<float>(n)));
17565 write_number(static_cast<float>(n));
17566 }
17567 else
17568 {
17569 oa->write_character(format == detail::input_format_t::cbor
17570 ? get_cbor_float_prefix(n)
17571 : get_msgpack_float_prefix(n));
17572 write_number(n);
17573 }
17574 #ifdef __GNUC__
17575 #pragma GCC diagnostic pop
17576 #endif
17577 }
17578
17579 public:
17580 // The following to_char_type functions are implement the conversion
17581 // between uint8_t and CharType. In case CharType is not unsigned,
17582 // such a conversion is required to allow values greater than 128.
17583 // See <https://github.com/nlohmann/json/issues/1286> for a discussion.
17584 template < typename C = CharType,
17585 enable_if_t < std::is_signed<C>::value && std::is_signed<char>::value > * = nullptr >
17586 static constexpr CharType to_char_type(std::uint8_t x) noexcept
17587 {
17588 return *reinterpret_cast<char*>(&x);
17589 }
17590
17591 template < typename C = CharType,
17592 enable_if_t < std::is_signed<C>::value && std::is_unsigned<char>::value > * = nullptr >
17593 static CharType to_char_type(std::uint8_t x) noexcept
17594 {
17595 static_assert(sizeof(std::uint8_t) == sizeof(CharType), "size of CharType must be equal to std::uint8_t");
17596 static_assert(std::is_trivial<CharType>::value, "CharType must be trivial");
17597 CharType result;
17598 std::memcpy(&result, &x, sizeof(x));
17599 return result;
17600 }
17601
17602 template<typename C = CharType,
17603 enable_if_t<std::is_unsigned<C>::value>* = nullptr>
17604 static constexpr CharType to_char_type(std::uint8_t x) noexcept
17605 {
17606 return x;
17607 }
17608
17609 template < typename InputCharType, typename C = CharType,
17610 enable_if_t <
17611 std::is_signed<C>::value &&
17612 std::is_signed<char>::value &&
17613 std::is_same<char, typename std::remove_cv<InputCharType>::type>::value
17614 > * = nullptr >
17615 static constexpr CharType to_char_type(InputCharType x) noexcept
17616 {
17617 return x;
17618 }
17619
17620 private:
17621 /// whether we can assume little endianness
17622 const bool is_little_endian = little_endianness();
17623
17624 /// the output
17625 output_adapter_t<CharType> oa = nullptr;
17626 };
17627
17628 } // namespace detail
17629 NLOHMANN_JSON_NAMESPACE_END
17630
17631 // #include <nlohmann/detail/output/output_adapters.hpp>
17632
17633 // #include <nlohmann/detail/output/serializer.hpp>
17634 // __ _____ _____ _____
17635 // __| | __| | | | JSON for Modern C++
17636 // | | |__ | | | | | | version 3.12.0
17637 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
17638 //
17639 // SPDX-FileCopyrightText: 2008 - 2009 Björn Hoehrmann <[email protected]>
17640 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
17641 // SPDX-License-Identifier: MIT
17642
17643
17644
17645 #include <algorithm> // reverse, remove, fill, find, none_of
17646 #include <array> // array
17647 #include <clocale> // localeconv, lconv
17648 #include <cmath> // labs, isfinite, isnan, signbit
17649 #include <cstddef> // size_t, ptrdiff_t
17650 #include <cstdint> // uint8_t
17651 #include <cstdio> // snprintf
17652 #include <limits> // numeric_limits
17653 #include <string> // string, char_traits
17654 #include <iomanip> // setfill, setw
17655 #include <type_traits> // is_same
17656 #include <utility> // move
17657
17658 // #include <nlohmann/detail/conversions/to_chars.hpp>
17659 // __ _____ _____ _____
17660 // __| | __| | | | JSON for Modern C++
17661 // | | |__ | | | | | | version 3.12.0
17662 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
17663 //
17664 // SPDX-FileCopyrightText: 2009 Florian Loitsch <https://florian.loitsch.com/>
17665 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
17666 // SPDX-License-Identifier: MIT
17667
17668
17669
17670 #include <array> // array
17671 #include <cmath> // signbit, isfinite
17672 #include <cstdint> // intN_t, uintN_t
17673 #include <cstring> // memcpy, memmove
17674 #include <limits> // numeric_limits
17675 #include <type_traits> // conditional
17676
17677 // #include <nlohmann/detail/macro_scope.hpp>
17678
17679
17680 NLOHMANN_JSON_NAMESPACE_BEGIN
17681 namespace detail
17682 {
17683
17684 /*!
17685 @brief implements the Grisu2 algorithm for binary to decimal floating-point
17686 conversion.
17687
17688 This implementation is a slightly modified version of the reference
17689 implementation which may be obtained from
17690 http://florian.loitsch.com/publications (bench.tar.gz).
17691
17692 The code is distributed under the MIT license, Copyright (c) 2009 Florian Loitsch.
17693
17694 For a detailed description of the algorithm see:
17695
17696 [1] Loitsch, "Printing Floating-Point Numbers Quickly and Accurately with
17697 Integers", Proceedings of the ACM SIGPLAN 2010 Conference on Programming
17698 Language Design and Implementation, PLDI 2010
17699 [2] Burger, Dybvig, "Printing Floating-Point Numbers Quickly and Accurately",
17700 Proceedings of the ACM SIGPLAN 1996 Conference on Programming Language
17701 Design and Implementation, PLDI 1996
17702 */
17703 namespace dtoa_impl
17704 {
17705
17706 template<typename Target, typename Source>
17707 Target reinterpret_bits(const Source source)
17708 {
17709 static_assert(sizeof(Target) == sizeof(Source), "size mismatch");
17710
17711 Target target;
17712 std::memcpy(&target, &source, sizeof(Source));
17713 return target;
17714 }
17715
17716 struct diyfp // f * 2^e
17717 {
17718 static constexpr int kPrecision = 64; // = q
17719
17720 std::uint64_t f = 0;
17721 int e = 0;
17722
17723 constexpr diyfp(std::uint64_t f_, int e_) noexcept : f(f_), e(e_) {}
17724
17725 /*!
17726 @brief returns x - y
17727 @pre x.e == y.e and x.f >= y.f
17728 */
17729 static diyfp sub(const diyfp& x, const diyfp& y) noexcept
17730 {
17731 JSON_ASSERT(x.e == y.e);
17732 JSON_ASSERT(x.f >= y.f);
17733
17734 return {x.f - y.f, x.e};
17735 }
17736
17737 /*!
17738 @brief returns x * y
17739 @note The result is rounded. (Only the upper q bits are returned.)
17740 */
17741 static diyfp mul(const diyfp& x, const diyfp& y) noexcept
17742 {
17743 static_assert(kPrecision == 64, "internal error");
17744
17745 // Computes:
17746 // f = round((x.f * y.f) / 2^q)
17747 // e = x.e + y.e + q
17748
17749 // Emulate the 64-bit * 64-bit multiplication:
17750 //
17751 // p = u * v
17752 // = (u_lo + 2^32 u_hi) (v_lo + 2^32 v_hi)
17753 // = (u_lo v_lo ) + 2^32 ((u_lo v_hi ) + (u_hi v_lo )) + 2^64 (u_hi v_hi )
17754 // = (p0 ) + 2^32 ((p1 ) + (p2 )) + 2^64 (p3 )
17755 // = (p0_lo + 2^32 p0_hi) + 2^32 ((p1_lo + 2^32 p1_hi) + (p2_lo + 2^32 p2_hi)) + 2^64 (p3 )
17756 // = (p0_lo ) + 2^32 (p0_hi + p1_lo + p2_lo ) + 2^64 (p1_hi + p2_hi + p3)
17757 // = (p0_lo ) + 2^32 (Q ) + 2^64 (H )
17758 // = (p0_lo ) + 2^32 (Q_lo + 2^32 Q_hi ) + 2^64 (H )
17759 //
17760 // (Since Q might be larger than 2^32 - 1)
17761 //
17762 // = (p0_lo + 2^32 Q_lo) + 2^64 (Q_hi + H)
17763 //
17764 // (Q_hi + H does not overflow a 64-bit int)
17765 //
17766 // = p_lo + 2^64 p_hi
17767
17768 const std::uint64_t u_lo = x.f & 0xFFFFFFFFu;
17769 const std::uint64_t u_hi = x.f >> 32u;
17770 const std::uint64_t v_lo = y.f & 0xFFFFFFFFu;
17771 const std::uint64_t v_hi = y.f >> 32u;
17772
17773 const std::uint64_t p0 = u_lo * v_lo;
17774 const std::uint64_t p1 = u_lo * v_hi;
17775 const std::uint64_t p2 = u_hi * v_lo;
17776 const std::uint64_t p3 = u_hi * v_hi;
17777
17778 const std::uint64_t p0_hi = p0 >> 32u;
17779 const std::uint64_t p1_lo = p1 & 0xFFFFFFFFu;
17780 const std::uint64_t p1_hi = p1 >> 32u;
17781 const std::uint64_t p2_lo = p2 & 0xFFFFFFFFu;
17782 const std::uint64_t p2_hi = p2 >> 32u;
17783
17784 std::uint64_t Q = p0_hi + p1_lo + p2_lo;
17785
17786 // The full product might now be computed as
17787 //
17788 // p_hi = p3 + p2_hi + p1_hi + (Q >> 32)
17789 // p_lo = p0_lo + (Q << 32)
17790 //
17791 // But in this particular case here, the full p_lo is not required.
17792 // Effectively we only need to add the highest bit in p_lo to p_hi (and
17793 // Q_hi + 1 does not overflow).
17794
17795 Q += std::uint64_t{1} << (64u - 32u - 1u); // round, ties up
17796
17797 const std::uint64_t h = p3 + p2_hi + p1_hi + (Q >> 32u);
17798
17799 return {h, x.e + y.e + 64};
17800 }
17801
17802 /*!
17803 @brief normalize x such that the significand is >= 2^(q-1)
17804 @pre x.f != 0
17805 */
17806 static diyfp normalize(diyfp x) noexcept
17807 {
17808 JSON_ASSERT(x.f != 0);
17809
17810 while ((x.f >> 63u) == 0)
17811 {
17812 x.f <<= 1u;
17813 x.e--;
17814 }
17815
17816 return x;
17817 }
17818
17819 /*!
17820 @brief normalize x such that the result has the exponent E
17821 @pre e >= x.e and the upper e - x.e bits of x.f must be zero.
17822 */
17823 static diyfp normalize_to(const diyfp& x, const int target_exponent) noexcept
17824 {
17825 const int delta = x.e - target_exponent;
17826
17827 JSON_ASSERT(delta >= 0);
17828 JSON_ASSERT(((x.f << delta) >> delta) == x.f);
17829
17830 return {x.f << delta, target_exponent};
17831 }
17832 };
17833
17834 struct boundaries
17835 {
17836 diyfp w;
17837 diyfp minus;
17838 diyfp plus;
17839 };
17840
17841 /*!
17842 Compute the (normalized) diyfp representing the input number 'value' and its
17843 boundaries.
17844
17845 @pre value must be finite and positive
17846 */
17847 template<typename FloatType>
17848 boundaries compute_boundaries(FloatType value)
17849 {
17850 JSON_ASSERT(std::isfinite(value));
17851 JSON_ASSERT(value > 0);
17852
17853 // Convert the IEEE representation into a diyfp.
17854 //
17855 // If v is denormal:
17856 // value = 0.F * 2^(1 - bias) = ( F) * 2^(1 - bias - (p-1))
17857 // If v is normalized:
17858 // value = 1.F * 2^(E - bias) = (2^(p-1) + F) * 2^(E - bias - (p-1))
17859
17860 static_assert(std::numeric_limits<FloatType>::is_iec559,
17861 "internal error: dtoa_short requires an IEEE-754 floating-point implementation");
17862
17863 constexpr int kPrecision = std::numeric_limits<FloatType>::digits; // = p (includes the hidden bit)
17864 constexpr int kBias = std::numeric_limits<FloatType>::max_exponent - 1 + (kPrecision - 1);
17865 constexpr int kMinExp = 1 - kBias;
17866 constexpr std::uint64_t kHiddenBit = std::uint64_t{1} << (kPrecision - 1); // = 2^(p-1)
17867
17868 using bits_type = typename std::conditional<kPrecision == 24, std::uint32_t, std::uint64_t >::type;
17869
17870 const auto bits = static_cast<std::uint64_t>(reinterpret_bits<bits_type>(value));
17871 const std::uint64_t E = bits >> (kPrecision - 1);
17872 const std::uint64_t F = bits & (kHiddenBit - 1);
17873
17874 const bool is_denormal = E == 0;
17875 const diyfp v = is_denormal
17876 ? diyfp(F, kMinExp)
17877 : diyfp(F + kHiddenBit, static_cast<int>(E) - kBias);
17878
17879 // Compute the boundaries m- and m+ of the floating-point value
17880 // v = f * 2^e.
17881 //
17882 // Determine v- and v+, the floating-point predecessor and successor if v,
17883 // respectively.
17884 //
17885 // v- = v - 2^e if f != 2^(p-1) or e == e_min (A)
17886 // = v - 2^(e-1) if f == 2^(p-1) and e > e_min (B)
17887 //
17888 // v+ = v + 2^e
17889 //
17890 // Let m- = (v- + v) / 2 and m+ = (v + v+) / 2. All real numbers _strictly_
17891 // between m- and m+ round to v, regardless of how the input rounding
17892 // algorithm breaks ties.
17893 //
17894 // ---+-------------+-------------+-------------+-------------+--- (A)
17895 // v- m- v m+ v+
17896 //
17897 // -----------------+------+------+-------------+-------------+--- (B)
17898 // v- m- v m+ v+
17899
17900 const bool lower_boundary_is_closer = F == 0 && E > 1;
17901 const diyfp m_plus = diyfp((2 * v.f) + 1, v.e - 1);
17902 const diyfp m_minus = lower_boundary_is_closer
17903 ? diyfp((4 * v.f) - 1, v.e - 2) // (B)
17904 : diyfp((2 * v.f) - 1, v.e - 1); // (A)
17905
17906 // Determine the normalized w+ = m+.
17907 const diyfp w_plus = diyfp::normalize(m_plus);
17908
17909 // Determine w- = m- such that e_(w-) = e_(w+).
17910 const diyfp w_minus = diyfp::normalize_to(m_minus, w_plus.e);
17911
17912 return {diyfp::normalize(v), w_minus, w_plus};
17913 }
17914
17915 // Given normalized diyfp w, Grisu needs to find a (normalized) cached
17916 // power-of-ten c, such that the exponent of the product c * w = f * 2^e lies
17917 // within a certain range [alpha, gamma] (Definition 3.2 from [1])
17918 //
17919 // alpha <= e = e_c + e_w + q <= gamma
17920 //
17921 // or
17922 //
17923 // f_c * f_w * 2^alpha <= f_c 2^(e_c) * f_w 2^(e_w) * 2^q
17924 // <= f_c * f_w * 2^gamma
17925 //
17926 // Since c and w are normalized, i.e. 2^(q-1) <= f < 2^q, this implies
17927 //
17928 // 2^(q-1) * 2^(q-1) * 2^alpha <= c * w * 2^q < 2^q * 2^q * 2^gamma
17929 //
17930 // or
17931 //
17932 // 2^(q - 2 + alpha) <= c * w < 2^(q + gamma)
17933 //
17934 // The choice of (alpha,gamma) determines the size of the table and the form of
17935 // the digit generation procedure. Using (alpha,gamma)=(-60,-32) works out well
17936 // in practice:
17937 //
17938 // The idea is to cut the number c * w = f * 2^e into two parts, which can be
17939 // processed independently: An integral part p1, and a fractional part p2:
17940 //
17941 // f * 2^e = ( (f div 2^-e) * 2^-e + (f mod 2^-e) ) * 2^e
17942 // = (f div 2^-e) + (f mod 2^-e) * 2^e
17943 // = p1 + p2 * 2^e
17944 //
17945 // The conversion of p1 into decimal form requires a series of divisions and
17946 // modulos by (a power of) 10. These operations are faster for 32-bit than for
17947 // 64-bit integers, so p1 should ideally fit into a 32-bit integer. This can be
17948 // achieved by choosing
17949 //
17950 // -e >= 32 or e <= -32 := gamma
17951 //
17952 // In order to convert the fractional part
17953 //
17954 // p2 * 2^e = p2 / 2^-e = d[-1] / 10^1 + d[-2] / 10^2 + ...
17955 //
17956 // into decimal form, the fraction is repeatedly multiplied by 10 and the digits
17957 // d[-i] are extracted in order:
17958 //
17959 // (10 * p2) div 2^-e = d[-1]
17960 // (10 * p2) mod 2^-e = d[-2] / 10^1 + ...
17961 //
17962 // The multiplication by 10 must not overflow. It is sufficient to choose
17963 //
17964 // 10 * p2 < 16 * p2 = 2^4 * p2 <= 2^64.
17965 //
17966 // Since p2 = f mod 2^-e < 2^-e,
17967 //
17968 // -e <= 60 or e >= -60 := alpha
17969
17970 constexpr int kAlpha = -60;
17971 constexpr int kGamma = -32;
17972
17973 struct cached_power // c = f * 2^e ~= 10^k
17974 {
17975 std::uint64_t f;
17976 int e;
17977 int k;
17978 };
17979
17980 /*!
17981 For a normalized diyfp w = f * 2^e, this function returns a (normalized) cached
17982 power-of-ten c = f_c * 2^e_c, such that the exponent of the product w * c
17983 satisfies (Definition 3.2 from [1])
17984
17985 alpha <= e_c + e + q <= gamma.
17986 */
17987 inline cached_power get_cached_power_for_binary_exponent(int e)
17988 {
17989 // Now
17990 //
17991 // alpha <= e_c + e + q <= gamma (1)
17992 // ==> f_c * 2^alpha <= c * 2^e * 2^q
17993 //
17994 // and since the c's are normalized, 2^(q-1) <= f_c,
17995 //
17996 // ==> 2^(q - 1 + alpha) <= c * 2^(e + q)
17997 // ==> 2^(alpha - e - 1) <= c
17998 //
17999 // If c were an exact power of ten, i.e. c = 10^k, one may determine k as
18000 //
18001 // k = ceil( log_10( 2^(alpha - e - 1) ) )
18002 // = ceil( (alpha - e - 1) * log_10(2) )
18003 //
18004 // From the paper:
18005 // "In theory the result of the procedure could be wrong since c is rounded,
18006 // and the computation itself is approximated [...]. In practice, however,
18007 // this simple function is sufficient."
18008 //
18009 // For IEEE double precision floating-point numbers converted into
18010 // normalized diyfp's w = f * 2^e, with q = 64,
18011 //
18012 // e >= -1022 (min IEEE exponent)
18013 // -52 (p - 1)
18014 // -52 (p - 1, possibly normalize denormal IEEE numbers)
18015 // -11 (normalize the diyfp)
18016 // = -1137
18017 //
18018 // and
18019 //
18020 // e <= +1023 (max IEEE exponent)
18021 // -52 (p - 1)
18022 // -11 (normalize the diyfp)
18023 // = 960
18024 //
18025 // This binary exponent range [-1137,960] results in a decimal exponent
18026 // range [-307,324]. One does not need to store a cached power for each
18027 // k in this range. For each such k it suffices to find a cached power
18028 // such that the exponent of the product lies in [alpha,gamma].
18029 // This implies that the difference of the decimal exponents of adjacent
18030 // table entries must be less than or equal to
18031 //
18032 // floor( (gamma - alpha) * log_10(2) ) = 8.
18033 //
18034 // (A smaller distance gamma-alpha would require a larger table.)
18035
18036 // NB:
18037 // Actually this function returns c, such that -60 <= e_c + e + 64 <= -34.
18038
18039 constexpr int kCachedPowersMinDecExp = -300;
18040 constexpr int kCachedPowersDecStep = 8;
18041
18042 static constexpr std::array<cached_power, 79> kCachedPowers =
18043 {
18044 {
18045 { 0xAB70FE17C79AC6CA, -1060, -300 },
18046 { 0xFF77B1FCBEBCDC4F, -1034, -292 },
18047 { 0xBE5691EF416BD60C, -1007, -284 },
18048 { 0x8DD01FAD907FFC3C, -980, -276 },
18049 { 0xD3515C2831559A83, -954, -268 },
18050 { 0x9D71AC8FADA6C9B5, -927, -260 },
18051 { 0xEA9C227723EE8BCB, -901, -252 },
18052 { 0xAECC49914078536D, -874, -244 },
18053 { 0x823C12795DB6CE57, -847, -236 },
18054 { 0xC21094364DFB5637, -821, -228 },
18055 { 0x9096EA6F3848984F, -794, -220 },
18056 { 0xD77485CB25823AC7, -768, -212 },
18057 { 0xA086CFCD97BF97F4, -741, -204 },
18058 { 0xEF340A98172AACE5, -715, -196 },
18059 { 0xB23867FB2A35B28E, -688, -188 },
18060 { 0x84C8D4DFD2C63F3B, -661, -180 },
18061 { 0xC5DD44271AD3CDBA, -635, -172 },
18062 { 0x936B9FCEBB25C996, -608, -164 },
18063 { 0xDBAC6C247D62A584, -582, -156 },
18064 { 0xA3AB66580D5FDAF6, -555, -148 },
18065 { 0xF3E2F893DEC3F126, -529, -140 },
18066 { 0xB5B5ADA8AAFF80B8, -502, -132 },
18067 { 0x87625F056C7C4A8B, -475, -124 },
18068 { 0xC9BCFF6034C13053, -449, -116 },
18069 { 0x964E858C91BA2655, -422, -108 },
18070 { 0xDFF9772470297EBD, -396, -100 },
18071 { 0xA6DFBD9FB8E5B88F, -369, -92 },
18072 { 0xF8A95FCF88747D94, -343, -84 },
18073 { 0xB94470938FA89BCF, -316, -76 },
18074 { 0x8A08F0F8BF0F156B, -289, -68 },
18075 { 0xCDB02555653131B6, -263, -60 },
18076 { 0x993FE2C6D07B7FAC, -236, -52 },
18077 { 0xE45C10C42A2B3B06, -210, -44 },
18078 { 0xAA242499697392D3, -183, -36 },
18079 { 0xFD87B5F28300CA0E, -157, -28 },
18080 { 0xBCE5086492111AEB, -130, -20 },
18081 { 0x8CBCCC096F5088CC, -103, -12 },
18082 { 0xD1B71758E219652C, -77, -4 },
18083 { 0x9C40000000000000, -50, 4 },
18084 { 0xE8D4A51000000000, -24, 12 },
18085 { 0xAD78EBC5AC620000, 3, 20 },
18086 { 0x813F3978F8940984, 30, 28 },
18087 { 0xC097CE7BC90715B3, 56, 36 },
18088 { 0x8F7E32CE7BEA5C70, 83, 44 },
18089 { 0xD5D238A4ABE98068, 109, 52 },
18090 { 0x9F4F2726179A2245, 136, 60 },
18091 { 0xED63A231D4C4FB27, 162, 68 },
18092 { 0xB0DE65388CC8ADA8, 189, 76 },
18093 { 0x83C7088E1AAB65DB, 216, 84 },
18094 { 0xC45D1DF942711D9A, 242, 92 },
18095 { 0x924D692CA61BE758, 269, 100 },
18096 { 0xDA01EE641A708DEA, 295, 108 },
18097 { 0xA26DA3999AEF774A, 322, 116 },
18098 { 0xF209787BB47D6B85, 348, 124 },
18099 { 0xB454E4A179DD1877, 375, 132 },
18100 { 0x865B86925B9BC5C2, 402, 140 },
18101 { 0xC83553C5C8965D3D, 428, 148 },
18102 { 0x952AB45CFA97A0B3, 455, 156 },
18103 { 0xDE469FBD99A05FE3, 481, 164 },
18104 { 0xA59BC234DB398C25, 508, 172 },
18105 { 0xF6C69A72A3989F5C, 534, 180 },
18106 { 0xB7DCBF5354E9BECE, 561, 188 },
18107 { 0x88FCF317F22241E2, 588, 196 },
18108 { 0xCC20CE9BD35C78A5, 614, 204 },
18109 { 0x98165AF37B2153DF, 641, 212 },
18110 { 0xE2A0B5DC971F303A, 667, 220 },
18111 { 0xA8D9D1535CE3B396, 694, 228 },
18112 { 0xFB9B7CD9A4A7443C, 720, 236 },
18113 { 0xBB764C4CA7A44410, 747, 244 },
18114 { 0x8BAB8EEFB6409C1A, 774, 252 },
18115 { 0xD01FEF10A657842C, 800, 260 },
18116 { 0x9B10A4E5E9913129, 827, 268 },
18117 { 0xE7109BFBA19C0C9D, 853, 276 },
18118 { 0xAC2820D9623BF429, 880, 284 },
18119 { 0x80444B5E7AA7CF85, 907, 292 },
18120 { 0xBF21E44003ACDD2D, 933, 300 },
18121 { 0x8E679C2F5E44FF8F, 960, 308 },
18122 { 0xD433179D9C8CB841, 986, 316 },
18123 { 0x9E19DB92B4E31BA9, 1013, 324 },
18124 }
18125 };
18126
18127 // This computation gives exactly the same results for k as
18128 // k = ceil((kAlpha - e - 1) * 0.30102999566398114)
18129 // for |e| <= 1500, but doesn't require floating-point operations.
18130 // NB: log_10(2) ~= 78913 / 2^18
18131 JSON_ASSERT(e >= -1500);
18132 JSON_ASSERT(e <= 1500);
18133 const int f = kAlpha - e - 1;
18134 const int k = ((f * 78913) / (1 << 18)) + static_cast<int>(f > 0);
18135
18136 const int index = (-kCachedPowersMinDecExp + k + (kCachedPowersDecStep - 1)) / kCachedPowersDecStep;
18137 JSON_ASSERT(index >= 0);
18138 JSON_ASSERT(static_cast<std::size_t>(index) < kCachedPowers.size());
18139
18140 const cached_power cached = kCachedPowers[static_cast<std::size_t>(index)];
18141 JSON_ASSERT(kAlpha <= cached.e + e + 64);
18142 JSON_ASSERT(kGamma >= cached.e + e + 64);
18143
18144 return cached;
18145 }
18146
18147 /*!
18148 For n != 0, returns k, such that pow10 := 10^(k-1) <= n < 10^k.
18149 For n == 0, returns 1 and sets pow10 := 1.
18150 */
18151 inline int find_largest_pow10(const std::uint32_t n, std::uint32_t& pow10)
18152 {
18153 // LCOV_EXCL_START
18154 if (n >= 1000000000)
18155 {
18156 pow10 = 1000000000;
18157 return 10;
18158 }
18159 // LCOV_EXCL_STOP
18160 if (n >= 100000000)
18161 {
18162 pow10 = 100000000;
18163 return 9;
18164 }
18165 if (n >= 10000000)
18166 {
18167 pow10 = 10000000;
18168 return 8;
18169 }
18170 if (n >= 1000000)
18171 {
18172 pow10 = 1000000;
18173 return 7;
18174 }
18175 if (n >= 100000)
18176 {
18177 pow10 = 100000;
18178 return 6;
18179 }
18180 if (n >= 10000)
18181 {
18182 pow10 = 10000;
18183 return 5;
18184 }
18185 if (n >= 1000)
18186 {
18187 pow10 = 1000;
18188 return 4;
18189 }
18190 if (n >= 100)
18191 {
18192 pow10 = 100;
18193 return 3;
18194 }
18195 if (n >= 10)
18196 {
18197 pow10 = 10;
18198 return 2;
18199 }
18200
18201 pow10 = 1;
18202 return 1;
18203 }
18204
18205 inline void grisu2_round(char* buf, int len, std::uint64_t dist, std::uint64_t delta,
18206 std::uint64_t rest, std::uint64_t ten_k)
18207 {
18208 JSON_ASSERT(len >= 1);
18209 JSON_ASSERT(dist <= delta);
18210 JSON_ASSERT(rest <= delta);
18211 JSON_ASSERT(ten_k > 0);
18212
18213 // <--------------------------- delta ---->
18214 // <---- dist --------->
18215 // --------------[------------------+-------------------]--------------
18216 // M- w M+
18217 //
18218 // ten_k
18219 // <------>
18220 // <---- rest ---->
18221 // --------------[------------------+----+--------------]--------------
18222 // w V
18223 // = buf * 10^k
18224 //
18225 // ten_k represents a unit-in-the-last-place in the decimal representation
18226 // stored in buf.
18227 // Decrement buf by ten_k while this takes buf closer to w.
18228
18229 // The tests are written in this order to avoid overflow in unsigned
18230 // integer arithmetic.
18231
18232 while (rest < dist
18233 && delta - rest >= ten_k
18234 && (rest + ten_k < dist || dist - rest > rest + ten_k - dist))
18235 {
18236 JSON_ASSERT(buf[len - 1] != '0');
18237 buf[len - 1]--;
18238 rest += ten_k;
18239 }
18240 }
18241
18242 /*!
18243 Generates V = buffer * 10^decimal_exponent, such that M- <= V <= M+.
18244 M- and M+ must be normalized and share the same exponent -60 <= e <= -32.
18245 */
18246 inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent,
18247 diyfp M_minus, diyfp w, diyfp M_plus)
18248 {
18249 static_assert(kAlpha >= -60, "internal error");
18250 static_assert(kGamma <= -32, "internal error");
18251
18252 // Generates the digits (and the exponent) of a decimal floating-point
18253 // number V = buffer * 10^decimal_exponent in the range [M-, M+]. The diyfp's
18254 // w, M- and M+ share the same exponent e, which satisfies alpha <= e <= gamma.
18255 //
18256 // <--------------------------- delta ---->
18257 // <---- dist --------->
18258 // --------------[------------------+-------------------]--------------
18259 // M- w M+
18260 //
18261 // Grisu2 generates the digits of M+ from left to right and stops as soon as
18262 // V is in [M-,M+].
18263
18264 JSON_ASSERT(M_plus.e >= kAlpha);
18265 JSON_ASSERT(M_plus.e <= kGamma);
18266
18267 std::uint64_t delta = diyfp::sub(M_plus, M_minus).f; // (significand of (M+ - M-), implicit exponent is e)
18268 std::uint64_t dist = diyfp::sub(M_plus, w ).f; // (significand of (M+ - w ), implicit exponent is e)
18269
18270 // Split M+ = f * 2^e into two parts p1 and p2 (note: e < 0):
18271 //
18272 // M+ = f * 2^e
18273 // = ((f div 2^-e) * 2^-e + (f mod 2^-e)) * 2^e
18274 // = ((p1 ) * 2^-e + (p2 )) * 2^e
18275 // = p1 + p2 * 2^e
18276
18277 const diyfp one(std::uint64_t{1} << -M_plus.e, M_plus.e);
18278
18279 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.)
18280 std::uint64_t p2 = M_plus.f & (one.f - 1); // p2 = f mod 2^-e
18281
18282 // 1)
18283 //
18284 // Generate the digits of the integral part p1 = d[n-1]...d[1]d[0]
18285
18286 JSON_ASSERT(p1 > 0);
18287
18288 std::uint32_t pow10{};
18289 const int k = find_largest_pow10(p1, pow10);
18290
18291 // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1)
18292 //
18293 // p1 = (p1 div 10^(k-1)) * 10^(k-1) + (p1 mod 10^(k-1))
18294 // = (d[k-1] ) * 10^(k-1) + (p1 mod 10^(k-1))
18295 //
18296 // M+ = p1 + p2 * 2^e
18297 // = d[k-1] * 10^(k-1) + (p1 mod 10^(k-1)) + p2 * 2^e
18298 // = d[k-1] * 10^(k-1) + ((p1 mod 10^(k-1)) * 2^-e + p2) * 2^e
18299 // = d[k-1] * 10^(k-1) + ( rest) * 2^e
18300 //
18301 // Now generate the digits d[n] of p1 from left to right (n = k-1,...,0)
18302 //
18303 // p1 = d[k-1]...d[n] * 10^n + d[n-1]...d[0]
18304 //
18305 // but stop as soon as
18306 //
18307 // rest * 2^e = (d[n-1]...d[0] * 2^-e + p2) * 2^e <= delta * 2^e
18308
18309 int n = k;
18310 while (n > 0)
18311 {
18312 // Invariants:
18313 // M+ = buffer * 10^n + (p1 + p2 * 2^e) (buffer = 0 for n = k)
18314 // pow10 = 10^(n-1) <= p1 < 10^n
18315 //
18316 const std::uint32_t d = p1 / pow10; // d = p1 div 10^(n-1)
18317 const std::uint32_t r = p1 % pow10; // r = p1 mod 10^(n-1)
18318 //
18319 // M+ = buffer * 10^n + (d * 10^(n-1) + r) + p2 * 2^e
18320 // = (buffer * 10 + d) * 10^(n-1) + (r + p2 * 2^e)
18321 //
18322 JSON_ASSERT(d <= 9);
18323 buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
18324 //
18325 // M+ = buffer * 10^(n-1) + (r + p2 * 2^e)
18326 //
18327 p1 = r;
18328 n--;
18329 //
18330 // M+ = buffer * 10^n + (p1 + p2 * 2^e)
18331 // pow10 = 10^n
18332 //
18333
18334 // Now check if enough digits have been generated.
18335 // Compute
18336 //
18337 // p1 + p2 * 2^e = (p1 * 2^-e + p2) * 2^e = rest * 2^e
18338 //
18339 // Note:
18340 // Since rest and delta share the same exponent e, it suffices to
18341 // compare the significands.
18342 const std::uint64_t rest = (std::uint64_t{p1} << -one.e) + p2;
18343 if (rest <= delta)
18344 {
18345 // V = buffer * 10^n, with M- <= V <= M+.
18346
18347 decimal_exponent += n;
18348
18349 // We may now just stop. But instead look if the buffer could be
18350 // decremented to bring V closer to w.
18351 //
18352 // pow10 = 10^n is now 1 ulp in the decimal representation V.
18353 // The rounding procedure works with diyfp's with an implicit
18354 // exponent of e.
18355 //
18356 // 10^n = (10^n * 2^-e) * 2^e = ulp * 2^e
18357 //
18358 const std::uint64_t ten_n = std::uint64_t{pow10} << -one.e;
18359 grisu2_round(buffer, length, dist, delta, rest, ten_n);
18360
18361 return;
18362 }
18363
18364 pow10 /= 10;
18365 //
18366 // pow10 = 10^(n-1) <= p1 < 10^n
18367 // Invariants restored.
18368 }
18369
18370 // 2)
18371 //
18372 // The digits of the integral part have been generated:
18373 //
18374 // M+ = d[k-1]...d[1]d[0] + p2 * 2^e
18375 // = buffer + p2 * 2^e
18376 //
18377 // Now generate the digits of the fractional part p2 * 2^e.
18378 //
18379 // Note:
18380 // No decimal point is generated: the exponent is adjusted instead.
18381 //
18382 // p2 actually represents the fraction
18383 //
18384 // p2 * 2^e
18385 // = p2 / 2^-e
18386 // = d[-1] / 10^1 + d[-2] / 10^2 + ...
18387 //
18388 // Now generate the digits d[-m] of p1 from left to right (m = 1,2,...)
18389 //
18390 // p2 * 2^e = d[-1]d[-2]...d[-m] * 10^-m
18391 // + 10^-m * (d[-m-1] / 10^1 + d[-m-2] / 10^2 + ...)
18392 //
18393 // using
18394 //
18395 // 10^m * p2 = ((10^m * p2) div 2^-e) * 2^-e + ((10^m * p2) mod 2^-e)
18396 // = ( d) * 2^-e + ( r)
18397 //
18398 // or
18399 // 10^m * p2 * 2^e = d + r * 2^e
18400 //
18401 // i.e.
18402 //
18403 // M+ = buffer + p2 * 2^e
18404 // = buffer + 10^-m * (d + r * 2^e)
18405 // = (buffer * 10^m + d) * 10^-m + 10^-m * r * 2^e
18406 //
18407 // and stop as soon as 10^-m * r * 2^e <= delta * 2^e
18408
18409 JSON_ASSERT(p2 > delta);
18410
18411 int m = 0;
18412 for (;;)
18413 {
18414 // Invariant:
18415 // M+ = buffer * 10^-m + 10^-m * (d[-m-1] / 10 + d[-m-2] / 10^2 + ...) * 2^e
18416 // = buffer * 10^-m + 10^-m * (p2 ) * 2^e
18417 // = buffer * 10^-m + 10^-m * (1/10 * (10 * p2) ) * 2^e
18418 // = buffer * 10^-m + 10^-m * (1/10 * ((10*p2 div 2^-e) * 2^-e + (10*p2 mod 2^-e)) * 2^e
18419 //
18420 JSON_ASSERT(p2 <= (std::numeric_limits<std::uint64_t>::max)() / 10);
18421 p2 *= 10;
18422 const std::uint64_t d = p2 >> -one.e; // d = (10 * p2) div 2^-e
18423 const std::uint64_t r = p2 & (one.f - 1); // r = (10 * p2) mod 2^-e
18424 //
18425 // M+ = buffer * 10^-m + 10^-m * (1/10 * (d * 2^-e + r) * 2^e
18426 // = buffer * 10^-m + 10^-m * (1/10 * (d + r * 2^e))
18427 // = (buffer * 10 + d) * 10^(-m-1) + 10^(-m-1) * r * 2^e
18428 //
18429 JSON_ASSERT(d <= 9);
18430 buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
18431 //
18432 // M+ = buffer * 10^(-m-1) + 10^(-m-1) * r * 2^e
18433 //
18434 p2 = r;
18435 m++;
18436 //
18437 // M+ = buffer * 10^-m + 10^-m * p2 * 2^e
18438 // Invariant restored.
18439
18440 // Check if enough digits have been generated.
18441 //
18442 // 10^-m * p2 * 2^e <= delta * 2^e
18443 // p2 * 2^e <= 10^m * delta * 2^e
18444 // p2 <= 10^m * delta
18445 delta *= 10;
18446 dist *= 10;
18447 if (p2 <= delta)
18448 {
18449 break;
18450 }
18451 }
18452
18453 // V = buffer * 10^-m, with M- <= V <= M+.
18454
18455 decimal_exponent -= m;
18456
18457 // 1 ulp in the decimal representation is now 10^-m.
18458 // Since delta and dist are now scaled by 10^m, we need to do the
18459 // same with ulp in order to keep the units in sync.
18460 //
18461 // 10^m * 10^-m = 1 = 2^-e * 2^e = ten_m * 2^e
18462 //
18463 const std::uint64_t ten_m = one.f;
18464 grisu2_round(buffer, length, dist, delta, p2, ten_m);
18465
18466 // By construction this algorithm generates the shortest possible decimal
18467 // number (Loitsch, Theorem 6.2) which rounds back to w.
18468 // For an input number of precision p, at least
18469 //
18470 // N = 1 + ceil(p * log_10(2))
18471 //
18472 // decimal digits are sufficient to identify all binary floating-point
18473 // numbers (Matula, "In-and-Out conversions").
18474 // This implies that the algorithm does not produce more than N decimal
18475 // digits.
18476 //
18477 // N = 17 for p = 53 (IEEE double precision)
18478 // N = 9 for p = 24 (IEEE single precision)
18479 }
18480
18481 /*!
18482 v = buf * 10^decimal_exponent
18483 len is the length of the buffer (number of decimal digits)
18484 The buffer must be large enough, i.e. >= max_digits10.
18485 */
18486 JSON_HEDLEY_NON_NULL(1)
18487 inline void grisu2(char* buf, int& len, int& decimal_exponent,
18488 diyfp m_minus, diyfp v, diyfp m_plus)
18489 {
18490 JSON_ASSERT(m_plus.e == m_minus.e);
18491 JSON_ASSERT(m_plus.e == v.e);
18492
18493 // --------(-----------------------+-----------------------)-------- (A)
18494 // m- v m+
18495 //
18496 // --------------------(-----------+-----------------------)-------- (B)
18497 // m- v m+
18498 //
18499 // First scale v (and m- and m+) such that the exponent is in the range
18500 // [alpha, gamma].
18501
18502 const cached_power cached = get_cached_power_for_binary_exponent(m_plus.e);
18503
18504 const diyfp c_minus_k(cached.f, cached.e); // = c ~= 10^-k
18505
18506 // The exponent of the products is = v.e + c_minus_k.e + q and is in the range [alpha,gamma]
18507 const diyfp w = diyfp::mul(v, c_minus_k);
18508 const diyfp w_minus = diyfp::mul(m_minus, c_minus_k);
18509 const diyfp w_plus = diyfp::mul(m_plus, c_minus_k);
18510
18511 // ----(---+---)---------------(---+---)---------------(---+---)----
18512 // w- w w+
18513 // = c*m- = c*v = c*m+
18514 //
18515 // diyfp::mul rounds its result and c_minus_k is approximated too. w, w- and
18516 // w+ are now off by a small amount.
18517 // In fact:
18518 //
18519 // w - v * 10^k < 1 ulp
18520 //
18521 // To account for this inaccuracy, add resp. subtract 1 ulp.
18522 //
18523 // --------+---[---------------(---+---)---------------]---+--------
18524 // w- M- w M+ w+
18525 //
18526 // Now any number in [M-, M+] (bounds included) will round to w when input,
18527 // regardless of how the input rounding algorithm breaks ties.
18528 //
18529 // And digit_gen generates the shortest possible such number in [M-, M+].
18530 // Note that this does not mean that Grisu2 always generates the shortest
18531 // possible number in the interval (m-, m+).
18532 const diyfp M_minus(w_minus.f + 1, w_minus.e);
18533 const diyfp M_plus (w_plus.f - 1, w_plus.e );
18534
18535 decimal_exponent = -cached.k; // = -(-k) = k
18536
18537 grisu2_digit_gen(buf, len, decimal_exponent, M_minus, w, M_plus);
18538 }
18539
18540 /*!
18541 v = buf * 10^decimal_exponent
18542 len is the length of the buffer (number of decimal digits)
18543 The buffer must be large enough, i.e. >= max_digits10.
18544 */
18545 template<typename FloatType>
18546 JSON_HEDLEY_NON_NULL(1)
18547 void grisu2(char* buf, int& len, int& decimal_exponent, FloatType value)
18548 {
18549 static_assert(diyfp::kPrecision >= std::numeric_limits<FloatType>::digits + 3,
18550 "internal error: not enough precision");
18551
18552 JSON_ASSERT(std::isfinite(value));
18553 JSON_ASSERT(value > 0);
18554
18555 // If the neighbors (and boundaries) of 'value' are always computed for double-precision
18556 // numbers, all float's can be recovered using strtod (and strtof). However, the resulting
18557 // decimal representations are not exactly "short".
18558 //
18559 // The documentation for 'std::to_chars' (https://en.cppreference.com/w/cpp/utility/to_chars)
18560 // says "value is converted to a string as if by std::sprintf in the default ("C") locale"
18561 // and since sprintf promotes floats to doubles, I think this is exactly what 'std::to_chars'
18562 // does.
18563 // On the other hand, the documentation for 'std::to_chars' requires that "parsing the
18564 // representation using the corresponding std::from_chars function recovers value exactly". That
18565 // indicates that single precision floating-point numbers should be recovered using
18566 // 'std::strtof'.
18567 //
18568 // NB: If the neighbors are computed for single-precision numbers, there is a single float
18569 // (7.0385307e-26f) which can't be recovered using strtod. The resulting double precision
18570 // value is off by 1 ulp.
18571 #if 0 // NOLINT(readability-avoid-unconditional-preprocessor-if)
18572 const boundaries w = compute_boundaries(static_cast<double>(value));
18573 #else
18574 const boundaries w = compute_boundaries(value);
18575 #endif
18576
18577 grisu2(buf, len, decimal_exponent, w.minus, w.w, w.plus);
18578 }
18579
18580 /*!
18581 @brief appends a decimal representation of e to buf
18582 @return a pointer to the element following the exponent.
18583 @pre -1000 < e < 1000
18584 */
18585 JSON_HEDLEY_NON_NULL(1)
18586 JSON_HEDLEY_RETURNS_NON_NULL
18587 inline char* append_exponent(char* buf, int e)
18588 {
18589 JSON_ASSERT(e > -1000);
18590 JSON_ASSERT(e < 1000);
18591
18592 if (e < 0)
18593 {
18594 e = -e;
18595 *buf++ = '-';
18596 }
18597 else
18598 {
18599 *buf++ = '+';
18600 }
18601
18602 auto k = static_cast<std::uint32_t>(e);
18603 if (k < 10)
18604 {
18605 // Always print at least two digits in the exponent.
18606 // This is for compatibility with printf("%g").
18607 *buf++ = '0';
18608 *buf++ = static_cast<char>('0' + k);
18609 }
18610 else if (k < 100)
18611 {
18612 *buf++ = static_cast<char>('0' + (k / 10));
18613 k %= 10;
18614 *buf++ = static_cast<char>('0' + k);
18615 }
18616 else
18617 {
18618 *buf++ = static_cast<char>('0' + (k / 100));
18619 k %= 100;
18620 *buf++ = static_cast<char>('0' + (k / 10));
18621 k %= 10;
18622 *buf++ = static_cast<char>('0' + k);
18623 }
18624
18625 return buf;
18626 }
18627
18628 /*!
18629 @brief prettify v = buf * 10^decimal_exponent
18630
18631 If v is in the range [10^min_exp, 10^max_exp) it will be printed in fixed-point
18632 notation. Otherwise it will be printed in exponential notation.
18633
18634 @pre min_exp < 0
18635 @pre max_exp > 0
18636 */
18637 JSON_HEDLEY_NON_NULL(1)
18638 JSON_HEDLEY_RETURNS_NON_NULL
18639 inline char* format_buffer(char* buf, int len, int decimal_exponent,
18640 int min_exp, int max_exp)
18641 {
18642 JSON_ASSERT(min_exp < 0);
18643 JSON_ASSERT(max_exp > 0);
18644
18645 const int k = len;
18646 const int n = len + decimal_exponent;
18647
18648 // v = buf * 10^(n-k)
18649 // k is the length of the buffer (number of decimal digits)
18650 // n is the position of the decimal point relative to the start of the buffer.
18651
18652 if (k <= n && n <= max_exp)
18653 {
18654 // digits[000]
18655 // len <= max_exp + 2
18656
18657 std::memset(buf + k, '0', static_cast<size_t>(n) - static_cast<size_t>(k));
18658 // Make it look like a floating-point number (#362, #378)
18659 buf[n + 0] = '.';
18660 buf[n + 1] = '0';
18661 return buf + (static_cast<size_t>(n) + 2);
18662 }
18663
18664 if (0 < n && n <= max_exp)
18665 {
18666 // dig.its
18667 // len <= max_digits10 + 1
18668
18669 JSON_ASSERT(k > n);
18670
18671 std::memmove(buf + (static_cast<size_t>(n) + 1), buf + n, static_cast<size_t>(k) - static_cast<size_t>(n));
18672 buf[n] = '.';
18673 return buf + (static_cast<size_t>(k) + 1U);
18674 }
18675
18676 if (min_exp < n && n <= 0)
18677 {
18678 // 0.[000]digits
18679 // len <= 2 + (-min_exp - 1) + max_digits10
18680
18681 std::memmove(buf + (2 + static_cast<size_t>(-n)), buf, static_cast<size_t>(k));
18682 buf[0] = '0';
18683 buf[1] = '.';
18684 std::memset(buf + 2, '0', static_cast<size_t>(-n));
18685 return buf + (2U + static_cast<size_t>(-n) + static_cast<size_t>(k));
18686 }
18687
18688 if (k == 1)
18689 {
18690 // dE+123
18691 // len <= 1 + 5
18692
18693 buf += 1;
18694 }
18695 else
18696 {
18697 // d.igitsE+123
18698 // len <= max_digits10 + 1 + 5
18699
18700 std::memmove(buf + 2, buf + 1, static_cast<size_t>(k) - 1);
18701 buf[1] = '.';
18702 buf += 1 + static_cast<size_t>(k);
18703 }
18704
18705 *buf++ = 'e';
18706 return append_exponent(buf, n - 1);
18707 }
18708
18709 } // namespace dtoa_impl
18710
18711 /*!
18712 @brief generates a decimal representation of the floating-point number value in [first, last).
18713
18714 The format of the resulting decimal representation is similar to printf's %g
18715 format. Returns an iterator pointing past-the-end of the decimal representation.
18716
18717 @note The input number must be finite, i.e. NaN's and Inf's are not supported.
18718 @note The buffer must be large enough.
18719 @note The result is NOT null-terminated.
18720 */
18721 template<typename FloatType>
18722 JSON_HEDLEY_NON_NULL(1, 2)
18723 JSON_HEDLEY_RETURNS_NON_NULL
18724 char* to_chars(char* first, const char* last, FloatType value)
18725 {
18726 static_cast<void>(last); // maybe unused - fix warning
18727 JSON_ASSERT(std::isfinite(value));
18728
18729 // Use signbit(value) instead of (value < 0) since signbit works for -0.
18730 if (std::signbit(value))
18731 {
18732 value = -value;
18733 *first++ = '-';
18734 }
18735
18736 #ifdef __GNUC__
18737 #pragma GCC diagnostic push
18738 #pragma GCC diagnostic ignored "-Wfloat-equal"
18739 #endif
18740 if (value == 0) // +-0
18741 {
18742 *first++ = '0';
18743 // Make it look like a floating-point number (#362, #378)
18744 *first++ = '.';
18745 *first++ = '0';
18746 return first;
18747 }
18748 #ifdef __GNUC__
18749 #pragma GCC diagnostic pop
18750 #endif
18751
18752 JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10);
18753
18754 // Compute v = buffer * 10^decimal_exponent.
18755 // The decimal digits are stored in the buffer, which needs to be interpreted
18756 // as an unsigned decimal integer.
18757 // len is the length of the buffer, i.e. the number of decimal digits.
18758 int len = 0;
18759 int decimal_exponent = 0;
18760 dtoa_impl::grisu2(first, len, decimal_exponent, value);
18761
18762 JSON_ASSERT(len <= std::numeric_limits<FloatType>::max_digits10);
18763
18764 // Format the buffer like printf("%.*g", prec, value)
18765 constexpr int kMinExp = -4;
18766 // Use digits10 here to increase compatibility with version 2.
18767 constexpr int kMaxExp = std::numeric_limits<FloatType>::digits10;
18768
18769 JSON_ASSERT(last - first >= kMaxExp + 2);
18770 JSON_ASSERT(last - first >= 2 + (-kMinExp - 1) + std::numeric_limits<FloatType>::max_digits10);
18771 JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10 + 6);
18772
18773 return dtoa_impl::format_buffer(first, len, decimal_exponent, kMinExp, kMaxExp);
18774 }
18775
18776 } // namespace detail
18777 NLOHMANN_JSON_NAMESPACE_END
18778
18779 // #include <nlohmann/detail/exceptions.hpp>
18780
18781 // #include <nlohmann/detail/macro_scope.hpp>
18782
18783 // #include <nlohmann/detail/meta/cpp_future.hpp>
18784
18785 // #include <nlohmann/detail/output/binary_writer.hpp>
18786
18787 // #include <nlohmann/detail/output/output_adapters.hpp>
18788
18789 // #include <nlohmann/detail/string_concat.hpp>
18790
18791 // #include <nlohmann/detail/value_t.hpp>
18792
18793
18794 NLOHMANN_JSON_NAMESPACE_BEGIN
18795 namespace detail
18796 {
18797
18798 ///////////////////
18799 // serialization //
18800 ///////////////////
18801
18802 /// how to treat decoding errors
18803 enum class error_handler_t
18804 {
18805 strict, ///< throw a type_error exception in case of invalid UTF-8
18806 replace, ///< replace invalid UTF-8 sequences with U+FFFD
18807 ignore ///< ignore invalid UTF-8 sequences
18808 };
18809
18810 template<typename BasicJsonType>
18811 class serializer
18812 {
18813 using string_t = typename BasicJsonType::string_t;
18814 using number_float_t = typename BasicJsonType::number_float_t;
18815 using number_integer_t = typename BasicJsonType::number_integer_t;
18816 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
18817 using binary_char_t = typename BasicJsonType::binary_t::value_type;
18818 static constexpr std::uint8_t UTF8_ACCEPT = 0;
18819 static constexpr std::uint8_t UTF8_REJECT = 1;
18820
18821 public:
18822 /*!
18823 @param[in] s output stream to serialize to
18824 @param[in] ichar indentation character to use
18825 @param[in] error_handler_ how to react on decoding errors
18826 */
18827 serializer(output_adapter_t<char> s, const char ichar,
18828 error_handler_t error_handler_ = error_handler_t::strict)
18829 : o(std::move(s))
18830 , loc(std::localeconv())
18831 , thousands_sep(loc->thousands_sep == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->thousands_sep)))
18832 , decimal_point(loc->decimal_point == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->decimal_point)))
18833 , indent_char(ichar)
18834 , indent_string(512, indent_char)
18835 , error_handler(error_handler_)
18836 {}
18837
18838 // delete because of pointer members
18839 serializer(const serializer&) = delete;
18840 serializer& operator=(const serializer&) = delete;
18841 serializer(serializer&&) = delete;
18842 serializer& operator=(serializer&&) = delete;
18843 ~serializer() = default;
18844
18845 /*!
18846 @brief internal implementation of the serialization function
18847
18848 This function is called by the public member function dump and organizes
18849 the serialization internally. The indentation level is propagated as
18850 additional parameter. In case of arrays and objects, the function is
18851 called recursively.
18852
18853 - strings and object keys are escaped using `escape_string()`
18854 - integer numbers are converted implicitly via `operator<<`
18855 - floating-point numbers are converted to a string using `"%g"` format
18856 - binary values are serialized as objects containing the subtype and the
18857 byte array
18858
18859 @param[in] val value to serialize
18860 @param[in] pretty_print whether the output shall be pretty-printed
18861 @param[in] ensure_ascii If @a ensure_ascii is true, all non-ASCII characters
18862 in the output are escaped with `\uXXXX` sequences, and the result consists
18863 of ASCII characters only.
18864 @param[in] indent_step the indent level
18865 @param[in] current_indent the current indent level (only used internally)
18866 */
18867 void dump(const BasicJsonType& val,
18868 const bool pretty_print,
18869 const bool ensure_ascii,
18870 const unsigned int indent_step,
18871 const unsigned int current_indent = 0)
18872 {
18873 switch (val.m_data.m_type)
18874 {
18875 case value_t::object:
18876 {
18877 if (val.m_data.m_value.object->empty())
18878 {
18879 o->write_characters("{}", 2);
18880 return;
18881 }
18882
18883 if (pretty_print)
18884 {
18885 o->write_characters("{\n", 2);
18886
18887 // variable to hold indentation for recursive calls
18888 const auto new_indent = current_indent + indent_step;
18889 if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
18890 {
18891 indent_string.resize(indent_string.size() * 2, ' ');
18892 }
18893
18894 // first n-1 elements
18895 auto i = val.m_data.m_value.object->cbegin();
18896 for (std::size_t cnt = 0; cnt < val.m_data.m_value.object->size() - 1; ++cnt, ++i)
18897 {
18898 o->write_characters(indent_string.c_str(), new_indent);
18899 o->write_character('\"');
18900 dump_escaped(i->first, ensure_ascii);
18901 o->write_characters("\": ", 3);
18902 dump(i->second, true, ensure_ascii, indent_step, new_indent);
18903 o->write_characters(",\n", 2);
18904 }
18905
18906 // last element
18907 JSON_ASSERT(i != val.m_data.m_value.object->cend());
18908 JSON_ASSERT(std::next(i) == val.m_data.m_value.object->cend());
18909 o->write_characters(indent_string.c_str(), new_indent);
18910 o->write_character('\"');
18911 dump_escaped(i->first, ensure_ascii);
18912 o->write_characters("\": ", 3);
18913 dump(i->second, true, ensure_ascii, indent_step, new_indent);
18914
18915 o->write_character('\n');
18916 o->write_characters(indent_string.c_str(), current_indent);
18917 o->write_character('}');
18918 }
18919 else
18920 {
18921 o->write_character('{');
18922
18923 // first n-1 elements
18924 auto i = val.m_data.m_value.object->cbegin();
18925 for (std::size_t cnt = 0; cnt < val.m_data.m_value.object->size() - 1; ++cnt, ++i)
18926 {
18927 o->write_character('\"');
18928 dump_escaped(i->first, ensure_ascii);
18929 o->write_characters("\":", 2);
18930 dump(i->second, false, ensure_ascii, indent_step, current_indent);
18931 o->write_character(',');
18932 }
18933
18934 // last element
18935 JSON_ASSERT(i != val.m_data.m_value.object->cend());
18936 JSON_ASSERT(std::next(i) == val.m_data.m_value.object->cend());
18937 o->write_character('\"');
18938 dump_escaped(i->first, ensure_ascii);
18939 o->write_characters("\":", 2);
18940 dump(i->second, false, ensure_ascii, indent_step, current_indent);
18941
18942 o->write_character('}');
18943 }
18944
18945 return;
18946 }
18947
18948 case value_t::array:
18949 {
18950 if (val.m_data.m_value.array->empty())
18951 {
18952 o->write_characters("[]", 2);
18953 return;
18954 }
18955
18956 if (pretty_print)
18957 {
18958 o->write_characters("[\n", 2);
18959
18960 // variable to hold indentation for recursive calls
18961 const auto new_indent = current_indent + indent_step;
18962 if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
18963 {
18964 indent_string.resize(indent_string.size() * 2, ' ');
18965 }
18966
18967 // first n-1 elements
18968 for (auto i = val.m_data.m_value.array->cbegin();
18969 i != val.m_data.m_value.array->cend() - 1; ++i)
18970 {
18971 o->write_characters(indent_string.c_str(), new_indent);
18972 dump(*i, true, ensure_ascii, indent_step, new_indent);
18973 o->write_characters(",\n", 2);
18974 }
18975
18976 // last element
18977 JSON_ASSERT(!val.m_data.m_value.array->empty());
18978 o->write_characters(indent_string.c_str(), new_indent);
18979 dump(val.m_data.m_value.array->back(), true, ensure_ascii, indent_step, new_indent);
18980
18981 o->write_character('\n');
18982 o->write_characters(indent_string.c_str(), current_indent);
18983 o->write_character(']');
18984 }
18985 else
18986 {
18987 o->write_character('[');
18988
18989 // first n-1 elements
18990 for (auto i = val.m_data.m_value.array->cbegin();
18991 i != val.m_data.m_value.array->cend() - 1; ++i)
18992 {
18993 dump(*i, false, ensure_ascii, indent_step, current_indent);
18994 o->write_character(',');
18995 }
18996
18997 // last element
18998 JSON_ASSERT(!val.m_data.m_value.array->empty());
18999 dump(val.m_data.m_value.array->back(), false, ensure_ascii, indent_step, current_indent);
19000
19001 o->write_character(']');
19002 }
19003
19004 return;
19005 }
19006
19007 case value_t::string:
19008 {
19009 o->write_character('\"');
19010 dump_escaped(*val.m_data.m_value.string, ensure_ascii);
19011 o->write_character('\"');
19012 return;
19013 }
19014
19015 case value_t::binary:
19016 {
19017 if (pretty_print)
19018 {
19019 o->write_characters("{\n", 2);
19020
19021 // variable to hold indentation for recursive calls
19022 const auto new_indent = current_indent + indent_step;
19023 if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
19024 {
19025 indent_string.resize(indent_string.size() * 2, ' ');
19026 }
19027
19028 o->write_characters(indent_string.c_str(), new_indent);
19029
19030 o->write_characters("\"bytes\": [", 10);
19031
19032 if (!val.m_data.m_value.binary->empty())
19033 {
19034 for (auto i = val.m_data.m_value.binary->cbegin();
19035 i != val.m_data.m_value.binary->cend() - 1; ++i)
19036 {
19037 dump_integer(*i);
19038 o->write_characters(", ", 2);
19039 }
19040 dump_integer(val.m_data.m_value.binary->back());
19041 }
19042
19043 o->write_characters("],\n", 3);
19044 o->write_characters(indent_string.c_str(), new_indent);
19045
19046 o->write_characters("\"subtype\": ", 11);
19047 if (val.m_data.m_value.binary->has_subtype())
19048 {
19049 dump_integer(val.m_data.m_value.binary->subtype());
19050 }
19051 else
19052 {
19053 o->write_characters("null", 4);
19054 }
19055 o->write_character('\n');
19056 o->write_characters(indent_string.c_str(), current_indent);
19057 o->write_character('}');
19058 }
19059 else
19060 {
19061 o->write_characters("{\"bytes\":[", 10);
19062
19063 if (!val.m_data.m_value.binary->empty())
19064 {
19065 for (auto i = val.m_data.m_value.binary->cbegin();
19066 i != val.m_data.m_value.binary->cend() - 1; ++i)
19067 {
19068 dump_integer(*i);
19069 o->write_character(',');
19070 }
19071 dump_integer(val.m_data.m_value.binary->back());
19072 }
19073
19074 o->write_characters("],\"subtype\":", 12);
19075 if (val.m_data.m_value.binary->has_subtype())
19076 {
19077 dump_integer(val.m_data.m_value.binary->subtype());
19078 o->write_character('}');
19079 }
19080 else
19081 {
19082 o->write_characters("null}", 5);
19083 }
19084 }
19085 return;
19086 }
19087
19088 case value_t::boolean:
19089 {
19090 if (val.m_data.m_value.boolean)
19091 {
19092 o->write_characters("true", 4);
19093 }
19094 else
19095 {
19096 o->write_characters("false", 5);
19097 }
19098 return;
19099 }
19100
19101 case value_t::number_integer:
19102 {
19103 dump_integer(val.m_data.m_value.number_integer);
19104 return;
19105 }
19106
19107 case value_t::number_unsigned:
19108 {
19109 dump_integer(val.m_data.m_value.number_unsigned);
19110 return;
19111 }
19112
19113 case value_t::number_float:
19114 {
19115 dump_float(val.m_data.m_value.number_float);
19116 return;
19117 }
19118
19119 case value_t::discarded:
19120 {
19121 o->write_characters("<discarded>", 11);
19122 return;
19123 }
19124
19125 case value_t::null:
19126 {
19127 o->write_characters("null", 4);
19128 return;
19129 }
19130
19131 default: // LCOV_EXCL_LINE
19132 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
19133 }
19134 }
19135
19136 JSON_PRIVATE_UNLESS_TESTED:
19137 /*!
19138 @brief dump escaped string
19139
19140 Escape a string by replacing certain special characters by a sequence of an
19141 escape character (backslash) and another character and other control
19142 characters by a sequence of "\u" followed by a four-digit hex
19143 representation. The escaped string is written to output stream @a o.
19144
19145 @param[in] s the string to escape
19146 @param[in] ensure_ascii whether to escape non-ASCII characters with
19147 \uXXXX sequences
19148
19149 @complexity Linear in the length of string @a s.
19150 */
19151 void dump_escaped(const string_t& s, const bool ensure_ascii)
19152 {
19153 std::uint32_t codepoint{};
19154 std::uint8_t state = UTF8_ACCEPT;
19155 std::size_t bytes = 0; // number of bytes written to string_buffer
19156
19157 // number of bytes written at the point of the last valid byte
19158 std::size_t bytes_after_last_accept = 0;
19159 std::size_t undumped_chars = 0;
19160
19161 for (std::size_t i = 0; i < s.size(); ++i)
19162 {
19163 const auto byte = static_cast<std::uint8_t>(s[i]);
19164
19165 switch (decode(state, codepoint, byte))
19166 {
19167 case UTF8_ACCEPT: // decode found a new code point
19168 {
19169 switch (codepoint)
19170 {
19171 case 0x08: // backspace
19172 {
19173 string_buffer[bytes++] = '\\';
19174 string_buffer[bytes++] = 'b';
19175 break;
19176 }
19177
19178 case 0x09: // horizontal tab
19179 {
19180 string_buffer[bytes++] = '\\';
19181 string_buffer[bytes++] = 't';
19182 break;
19183 }
19184
19185 case 0x0A: // newline
19186 {
19187 string_buffer[bytes++] = '\\';
19188 string_buffer[bytes++] = 'n';
19189 break;
19190 }
19191
19192 case 0x0C: // formfeed
19193 {
19194 string_buffer[bytes++] = '\\';
19195 string_buffer[bytes++] = 'f';
19196 break;
19197 }
19198
19199 case 0x0D: // carriage return
19200 {
19201 string_buffer[bytes++] = '\\';
19202 string_buffer[bytes++] = 'r';
19203 break;
19204 }
19205
19206 case 0x22: // quotation mark
19207 {
19208 string_buffer[bytes++] = '\\';
19209 string_buffer[bytes++] = '\"';
19210 break;
19211 }
19212
19213 case 0x5C: // reverse solidus
19214 {
19215 string_buffer[bytes++] = '\\';
19216 string_buffer[bytes++] = '\\';
19217 break;
19218 }
19219
19220 default:
19221 {
19222 // escape control characters (0x00..0x1F) or, if
19223 // ensure_ascii parameter is used, non-ASCII characters
19224 if ((codepoint <= 0x1F) || (ensure_ascii && (codepoint >= 0x7F)))
19225 {
19226 if (codepoint <= 0xFFFF)
19227 {
19228 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
19229 static_cast<void>((std::snprintf)(string_buffer.data() + bytes, 7, "\\u%04x",
19230 static_cast<std::uint16_t>(codepoint)));
19231 bytes += 6;
19232 }
19233 else
19234 {
19235 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
19236 static_cast<void>((std::snprintf)(string_buffer.data() + bytes, 13, "\\u%04x\\u%04x",
19237 static_cast<std::uint16_t>(0xD7C0u + (codepoint >> 10u)),
19238 static_cast<std::uint16_t>(0xDC00u + (codepoint & 0x3FFu))));
19239 bytes += 12;
19240 }
19241 }
19242 else
19243 {
19244 // copy byte to buffer (all previous bytes
19245 // been copied have in default case above)
19246 string_buffer[bytes++] = s[i];
19247 }
19248 break;
19249 }
19250 }
19251
19252 // write buffer and reset index; there must be 13 bytes
19253 // left, as this is the maximal number of bytes to be
19254 // written ("\uxxxx\uxxxx\0") for one code point
19255 if (string_buffer.size() - bytes < 13)
19256 {
19257 o->write_characters(string_buffer.data(), bytes);
19258 bytes = 0;
19259 }
19260
19261 // remember the byte position of this accept
19262 bytes_after_last_accept = bytes;
19263 undumped_chars = 0;
19264 break;
19265 }
19266
19267 case UTF8_REJECT: // decode found invalid UTF-8 byte
19268 {
19269 switch (error_handler)
19270 {
19271 case error_handler_t::strict:
19272 {
19273 JSON_THROW(type_error::create(316, concat("invalid UTF-8 byte at index ", std::to_string(i), ": 0x", hex_bytes(byte | 0)), nullptr));
19274 }
19275
19276 case error_handler_t::ignore:
19277 case error_handler_t::replace:
19278 {
19279 // in case we saw this character the first time, we
19280 // would like to read it again, because the byte
19281 // may be OK for itself, but just not OK for the
19282 // previous sequence
19283 if (undumped_chars > 0)
19284 {
19285 --i;
19286 }
19287
19288 // reset length buffer to the last accepted index;
19289 // thus removing/ignoring the invalid characters
19290 bytes = bytes_after_last_accept;
19291
19292 if (error_handler == error_handler_t::replace)
19293 {
19294 // add a replacement character
19295 if (ensure_ascii)
19296 {
19297 string_buffer[bytes++] = '\\';
19298 string_buffer[bytes++] = 'u';
19299 string_buffer[bytes++] = 'f';
19300 string_buffer[bytes++] = 'f';
19301 string_buffer[bytes++] = 'f';
19302 string_buffer[bytes++] = 'd';
19303 }
19304 else
19305 {
19306 string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xEF');
19307 string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBF');
19308 string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBD');
19309 }
19310
19311 // write buffer and reset index; there must be 13 bytes
19312 // left, as this is the maximal number of bytes to be
19313 // written ("\uxxxx\uxxxx\0") for one code point
19314 if (string_buffer.size() - bytes < 13)
19315 {
19316 o->write_characters(string_buffer.data(), bytes);
19317 bytes = 0;
19318 }
19319
19320 bytes_after_last_accept = bytes;
19321 }
19322
19323 undumped_chars = 0;
19324
19325 // continue processing the string
19326 state = UTF8_ACCEPT;
19327 break;
19328 }
19329
19330 default: // LCOV_EXCL_LINE
19331 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
19332 }
19333 break;
19334 }
19335
19336 default: // decode found yet incomplete multi-byte code point
19337 {
19338 if (!ensure_ascii)
19339 {
19340 // code point will not be escaped - copy byte to buffer
19341 string_buffer[bytes++] = s[i];
19342 }
19343 ++undumped_chars;
19344 break;
19345 }
19346 }
19347 }
19348
19349 // we finished processing the string
19350 if (JSON_HEDLEY_LIKELY(state == UTF8_ACCEPT))
19351 {
19352 // write buffer
19353 if (bytes > 0)
19354 {
19355 o->write_characters(string_buffer.data(), bytes);
19356 }
19357 }
19358 else
19359 {
19360 // we finish reading, but do not accept: string was incomplete
19361 switch (error_handler)
19362 {
19363 case error_handler_t::strict:
19364 {
19365 JSON_THROW(type_error::create(316, concat("incomplete UTF-8 string; last byte: 0x", hex_bytes(static_cast<std::uint8_t>(s.back() | 0))), nullptr));
19366 }
19367
19368 case error_handler_t::ignore:
19369 {
19370 // write all accepted bytes
19371 o->write_characters(string_buffer.data(), bytes_after_last_accept);
19372 break;
19373 }
19374
19375 case error_handler_t::replace:
19376 {
19377 // write all accepted bytes
19378 o->write_characters(string_buffer.data(), bytes_after_last_accept);
19379 // add a replacement character
19380 if (ensure_ascii)
19381 {
19382 o->write_characters("\\ufffd", 6);
19383 }
19384 else
19385 {
19386 o->write_characters("\xEF\xBF\xBD", 3);
19387 }
19388 break;
19389 }
19390
19391 default: // LCOV_EXCL_LINE
19392 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
19393 }
19394 }
19395 }
19396
19397 private:
19398 /*!
19399 @brief count digits
19400
19401 Count the number of decimal (base 10) digits for an input unsigned integer.
19402
19403 @param[in] x unsigned integer number to count its digits
19404 @return number of decimal digits
19405 */
19406 unsigned int count_digits(number_unsigned_t x) noexcept
19407 {
19408 unsigned int n_digits = 1;
19409 for (;;)
19410 {
19411 if (x < 10)
19412 {
19413 return n_digits;
19414 }
19415 if (x < 100)
19416 {
19417 return n_digits + 1;
19418 }
19419 if (x < 1000)
19420 {
19421 return n_digits + 2;
19422 }
19423 if (x < 10000)
19424 {
19425 return n_digits + 3;
19426 }
19427 x = x / 10000u;
19428 n_digits += 4;
19429 }
19430 }
19431
19432 /*!
19433 * @brief convert a byte to a uppercase hex representation
19434 * @param[in] byte byte to represent
19435 * @return representation ("00".."FF")
19436 */
19437 static std::string hex_bytes(std::uint8_t byte)
19438 {
19439 std::string result = "FF";
19440 constexpr const char* nibble_to_hex = "0123456789ABCDEF";
19441 result[0] = nibble_to_hex[byte / 16];
19442 result[1] = nibble_to_hex[byte % 16];
19443 return result;
19444 }
19445
19446 // templates to avoid warnings about useless casts
19447 template <typename NumberType, enable_if_t<std::is_signed<NumberType>::value, int> = 0>
19448 bool is_negative_number(NumberType x)
19449 {
19450 return x < 0;
19451 }
19452
19453 template < typename NumberType, enable_if_t <std::is_unsigned<NumberType>::value, int > = 0 >
19454 bool is_negative_number(NumberType /*unused*/)
19455 {
19456 return false;
19457 }
19458
19459 /*!
19460 @brief dump an integer
19461
19462 Dump a given integer to output stream @a o. Works internally with
19463 @a number_buffer.
19464
19465 @param[in] x integer number (signed or unsigned) to dump
19466 @tparam NumberType either @a number_integer_t or @a number_unsigned_t
19467 */
19468 template < typename NumberType, detail::enable_if_t <
19469 std::is_integral<NumberType>::value ||
19470 std::is_same<NumberType, number_unsigned_t>::value ||
19471 std::is_same<NumberType, number_integer_t>::value ||
19472 std::is_same<NumberType, binary_char_t>::value,
19473 int > = 0 >
19474 void dump_integer(NumberType x)
19475 {
19476 static constexpr std::array<std::array<char, 2>, 100> digits_to_99
19477 {
19478 {
19479 {{'0', '0'}}, {{'0', '1'}}, {{'0', '2'}}, {{'0', '3'}}, {{'0', '4'}}, {{'0', '5'}}, {{'0', '6'}}, {{'0', '7'}}, {{'0', '8'}}, {{'0', '9'}},
19480 {{'1', '0'}}, {{'1', '1'}}, {{'1', '2'}}, {{'1', '3'}}, {{'1', '4'}}, {{'1', '5'}}, {{'1', '6'}}, {{'1', '7'}}, {{'1', '8'}}, {{'1', '9'}},
19481 {{'2', '0'}}, {{'2', '1'}}, {{'2', '2'}}, {{'2', '3'}}, {{'2', '4'}}, {{'2', '5'}}, {{'2', '6'}}, {{'2', '7'}}, {{'2', '8'}}, {{'2', '9'}},
19482 {{'3', '0'}}, {{'3', '1'}}, {{'3', '2'}}, {{'3', '3'}}, {{'3', '4'}}, {{'3', '5'}}, {{'3', '6'}}, {{'3', '7'}}, {{'3', '8'}}, {{'3', '9'}},
19483 {{'4', '0'}}, {{'4', '1'}}, {{'4', '2'}}, {{'4', '3'}}, {{'4', '4'}}, {{'4', '5'}}, {{'4', '6'}}, {{'4', '7'}}, {{'4', '8'}}, {{'4', '9'}},
19484 {{'5', '0'}}, {{'5', '1'}}, {{'5', '2'}}, {{'5', '3'}}, {{'5', '4'}}, {{'5', '5'}}, {{'5', '6'}}, {{'5', '7'}}, {{'5', '8'}}, {{'5', '9'}},
19485 {{'6', '0'}}, {{'6', '1'}}, {{'6', '2'}}, {{'6', '3'}}, {{'6', '4'}}, {{'6', '5'}}, {{'6', '6'}}, {{'6', '7'}}, {{'6', '8'}}, {{'6', '9'}},
19486 {{'7', '0'}}, {{'7', '1'}}, {{'7', '2'}}, {{'7', '3'}}, {{'7', '4'}}, {{'7', '5'}}, {{'7', '6'}}, {{'7', '7'}}, {{'7', '8'}}, {{'7', '9'}},
19487 {{'8', '0'}}, {{'8', '1'}}, {{'8', '2'}}, {{'8', '3'}}, {{'8', '4'}}, {{'8', '5'}}, {{'8', '6'}}, {{'8', '7'}}, {{'8', '8'}}, {{'8', '9'}},
19488 {{'9', '0'}}, {{'9', '1'}}, {{'9', '2'}}, {{'9', '3'}}, {{'9', '4'}}, {{'9', '5'}}, {{'9', '6'}}, {{'9', '7'}}, {{'9', '8'}}, {{'9', '9'}},
19489 }
19490 };
19491
19492 // special case for "0"
19493 if (x == 0)
19494 {
19495 o->write_character('0');
19496 return;
19497 }
19498
19499 // use a pointer to fill the buffer
19500 auto buffer_ptr = number_buffer.begin(); // NOLINT(llvm-qualified-auto,readability-qualified-auto,cppcoreguidelines-pro-type-vararg,hicpp-vararg)
19501
19502 number_unsigned_t abs_value;
19503
19504 unsigned int n_chars{};
19505
19506 if (is_negative_number(x))
19507 {
19508 *buffer_ptr = '-';
19509 abs_value = remove_sign(static_cast<number_integer_t>(x));
19510
19511 // account one more byte for the minus sign
19512 n_chars = 1 + count_digits(abs_value);
19513 }
19514 else
19515 {
19516 abs_value = static_cast<number_unsigned_t>(x);
19517 n_chars = count_digits(abs_value);
19518 }
19519
19520 // spare 1 byte for '\0'
19521 JSON_ASSERT(n_chars < number_buffer.size() - 1);
19522
19523 // jump to the end to generate the string from backward,
19524 // so we later avoid reversing the result
19525 buffer_ptr += n_chars;
19526
19527 // Fast int2ascii implementation inspired by "Fastware" talk by Andrei Alexandrescu
19528 // See: https://www.youtube.com/watch?v=o4-CwDo2zpg
19529 while (abs_value >= 100)
19530 {
19531 const auto digits_index = static_cast<unsigned>((abs_value % 100));
19532 abs_value /= 100;
19533 *(--buffer_ptr) = digits_to_99[digits_index][1];
19534 *(--buffer_ptr) = digits_to_99[digits_index][0];
19535 }
19536
19537 if (abs_value >= 10)
19538 {
19539 const auto digits_index = static_cast<unsigned>(abs_value);
19540 *(--buffer_ptr) = digits_to_99[digits_index][1];
19541 *(--buffer_ptr) = digits_to_99[digits_index][0];
19542 }
19543 else
19544 {
19545 *(--buffer_ptr) = static_cast<char>('0' + abs_value);
19546 }
19547
19548 o->write_characters(number_buffer.data(), n_chars);
19549 }
19550
19551 /*!
19552 @brief dump a floating-point number
19553
19554 Dump a given floating-point number to output stream @a o. Works internally
19555 with @a number_buffer.
19556
19557 @param[in] x floating-point number to dump
19558 */
19559 void dump_float(number_float_t x)
19560 {
19561 // NaN / inf
19562 if (!std::isfinite(x))
19563 {
19564 o->write_characters("null", 4);
19565 return;
19566 }
19567
19568 // If number_float_t is an IEEE-754 single or double precision number,
19569 // use the Grisu2 algorithm to produce short numbers which are
19570 // guaranteed to round-trip, using strtof and strtod, resp.
19571 //
19572 // NB: The test below works if <long double> == <double>.
19573 static constexpr bool is_ieee_single_or_double
19574 = (std::numeric_limits<number_float_t>::is_iec559 && std::numeric_limits<number_float_t>::digits == 24 && std::numeric_limits<number_float_t>::max_exponent == 128) ||
19575 (std::numeric_limits<number_float_t>::is_iec559 && std::numeric_limits<number_float_t>::digits == 53 && std::numeric_limits<number_float_t>::max_exponent == 1024);
19576
19577 dump_float(x, std::integral_constant<bool, is_ieee_single_or_double>());
19578 }
19579
19580 void dump_float(number_float_t x, std::true_type /*is_ieee_single_or_double*/)
19581 {
19582 auto* begin = number_buffer.data();
19583 auto* end = ::nlohmann::detail::to_chars(begin, begin + number_buffer.size(), x);
19584
19585 o->write_characters(begin, static_cast<size_t>(end - begin));
19586 }
19587
19588 void dump_float(number_float_t x, std::false_type /*is_ieee_single_or_double*/)
19589 {
19590 // get number of digits for a float -> text -> float round-trip
19591 static constexpr auto d = std::numeric_limits<number_float_t>::max_digits10;
19592
19593 // the actual conversion
19594 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
19595 std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*g", d, x);
19596
19597 // negative value indicates an error
19598 JSON_ASSERT(len > 0);
19599 // check if buffer was large enough
19600 JSON_ASSERT(static_cast<std::size_t>(len) < number_buffer.size());
19601
19602 // erase thousands separator
19603 if (thousands_sep != '\0')
19604 {
19605 // NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::remove returns an iterator, see https://github.com/nlohmann/json/issues/3081
19606 const auto end = std::remove(number_buffer.begin(), number_buffer.begin() + len, thousands_sep);
19607 std::fill(end, number_buffer.end(), '\0');
19608 JSON_ASSERT((end - number_buffer.begin()) <= len);
19609 len = (end - number_buffer.begin());
19610 }
19611
19612 // convert decimal point to '.'
19613 if (decimal_point != '\0' && decimal_point != '.')
19614 {
19615 // NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::find returns an iterator, see https://github.com/nlohmann/json/issues/3081
19616 const auto dec_pos = std::find(number_buffer.begin(), number_buffer.end(), decimal_point);
19617 if (dec_pos != number_buffer.end())
19618 {
19619 *dec_pos = '.';
19620 }
19621 }
19622
19623 o->write_characters(number_buffer.data(), static_cast<std::size_t>(len));
19624
19625 // determine if we need to append ".0"
19626 const bool value_is_int_like =
19627 std::none_of(number_buffer.begin(), number_buffer.begin() + len + 1,
19628 [](char c)
19629 {
19630 return c == '.' || c == 'e';
19631 });
19632
19633 if (value_is_int_like)
19634 {
19635 o->write_characters(".0", 2);
19636 }
19637 }
19638
19639 /*!
19640 @brief check whether a string is UTF-8 encoded
19641
19642 The function checks each byte of a string whether it is UTF-8 encoded. The
19643 result of the check is stored in the @a state parameter. The function must
19644 be called initially with state 0 (accept). State 1 means the string must
19645 be rejected, because the current byte is not allowed. If the string is
19646 completely processed, but the state is non-zero, the string ended
19647 prematurely; that is, the last byte indicated more bytes should have
19648 followed.
19649
19650 @param[in,out] state the state of the decoding
19651 @param[in,out] codep codepoint (valid only if resulting state is UTF8_ACCEPT)
19652 @param[in] byte next byte to decode
19653 @return new state
19654
19655 @note The function has been edited: a std::array is used.
19656
19657 @copyright Copyright (c) 2008-2009 Bjoern Hoehrmann <[email protected]>
19658 @sa http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
19659 */
19660 static std::uint8_t decode(std::uint8_t& state, std::uint32_t& codep, const std::uint8_t byte) noexcept
19661 {
19662 static const std::array<std::uint8_t, 400> utf8d =
19663 {
19664 {
19665 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
19666 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
19667 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
19668 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
19669 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
19670 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
19671 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
19672 0xA, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3, // E0..EF
19673 0xB, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, // F0..FF
19674 0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, 0x6, 0x1, 0x1, 0x1, 0x1, // s0..s0
19675 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
19676 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
19677 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
19678 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
19679 }
19680 };
19681
19682 JSON_ASSERT(byte < utf8d.size());
19683 const std::uint8_t type = utf8d[byte];
19684
19685 codep = (state != UTF8_ACCEPT)
19686 ? (byte & 0x3fu) | (codep << 6u)
19687 : (0xFFu >> type) & (byte);
19688
19689 const std::size_t index = 256u + (static_cast<size_t>(state) * 16u) + static_cast<size_t>(type);
19690 JSON_ASSERT(index < utf8d.size());
19691 state = utf8d[index];
19692 return state;
19693 }
19694
19695 /*
19696 * Overload to make the compiler happy while it is instantiating
19697 * dump_integer for number_unsigned_t.
19698 * Must never be called.
19699 */
19700 number_unsigned_t remove_sign(number_unsigned_t x)
19701 {
19702 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
19703 return x; // LCOV_EXCL_LINE
19704 }
19705
19706 /*
19707 * Helper function for dump_integer
19708 *
19709 * This function takes a negative signed integer and returns its absolute
19710 * value as unsigned integer. The plus/minus shuffling is necessary as we can
19711 * not directly remove the sign of an arbitrary signed integer as the
19712 * absolute values of INT_MIN and INT_MAX are usually not the same. See
19713 * #1708 for details.
19714 */
19715 number_unsigned_t remove_sign(number_integer_t x) noexcept
19716 {
19717 JSON_ASSERT(x < 0 && x < (std::numeric_limits<number_integer_t>::max)()); // NOLINT(misc-redundant-expression)
19718 return static_cast<number_unsigned_t>(-(x + 1)) + 1;
19719 }
19720
19721 private:
19722 /// the output of the serializer
19723 output_adapter_t<char> o = nullptr;
19724
19725 /// a (hopefully) large enough character buffer
19726 std::array<char, 64> number_buffer{{}};
19727
19728 /// the locale
19729 const std::lconv* loc = nullptr;
19730 /// the locale's thousand separator character
19731 const char thousands_sep = '\0';
19732 /// the locale's decimal point character
19733 const char decimal_point = '\0';
19734
19735 /// string buffer
19736 std::array<char, 512> string_buffer{{}};
19737
19738 /// the indentation character
19739 const char indent_char;
19740 /// the indentation string
19741 string_t indent_string;
19742
19743 /// error_handler how to react on decoding errors
19744 const error_handler_t error_handler;
19745 };
19746
19747 } // namespace detail
19748 NLOHMANN_JSON_NAMESPACE_END
19749
19750 // #include <nlohmann/detail/value_t.hpp>
19751
19752 // #include <nlohmann/json_fwd.hpp>
19753
19754 // #include <nlohmann/ordered_map.hpp>
19755 // __ _____ _____ _____
19756 // __| | __| | | | JSON for Modern C++
19757 // | | |__ | | | | | | version 3.12.0
19758 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
19759 //
19760 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
19761 // SPDX-License-Identifier: MIT
19762
19763
19764
19765 #include <functional> // equal_to, less
19766 #include <initializer_list> // initializer_list
19767 #include <iterator> // input_iterator_tag, iterator_traits
19768 #include <memory> // allocator
19769 #include <stdexcept> // for out_of_range
19770 #include <type_traits> // enable_if, is_convertible
19771 #include <utility> // pair
19772 #include <vector> // vector
19773
19774 // #include <nlohmann/detail/macro_scope.hpp>
19775
19776 // #include <nlohmann/detail/meta/type_traits.hpp>
19777
19778
19779 NLOHMANN_JSON_NAMESPACE_BEGIN
19780
19781 /// ordered_map: a minimal map-like container that preserves insertion order
19782 /// for use within nlohmann::basic_json<ordered_map>
19783 template <class Key, class T, class IgnoredLess = std::less<Key>,
19784 class Allocator = std::allocator<std::pair<const Key, T>>>
19785 struct ordered_map : std::vector<std::pair<const Key, T>, Allocator>
19786 {
19787 using key_type = Key;
19788 using mapped_type = T;
19789 using Container = std::vector<std::pair<const Key, T>, Allocator>;
19790 using iterator = typename Container::iterator;
19791 using const_iterator = typename Container::const_iterator;
19792 using size_type = typename Container::size_type;
19793 using value_type = typename Container::value_type;
19794 #ifdef JSON_HAS_CPP_14
19795 using key_compare = std::equal_to<>;
19796 #else
19797 using key_compare = std::equal_to<Key>;
19798 #endif
19799
19800 // Explicit constructors instead of `using Container::Container`
19801 // otherwise older compilers choke on it (GCC <= 5.5, xcode <= 9.4)
19802 ordered_map() noexcept(noexcept(Container())) : Container{} {}
19803 explicit ordered_map(const Allocator& alloc) noexcept(noexcept(Container(alloc))) : Container{alloc} {}
19804 template <class It>
19805 ordered_map(It first, It last, const Allocator& alloc = Allocator())
19806 : Container{first, last, alloc} {}
19807 ordered_map(std::initializer_list<value_type> init, const Allocator& alloc = Allocator() )
19808 : Container{init, alloc} {}
19809
19810 std::pair<iterator, bool> emplace(const key_type& key, T&& t)
19811 {
19812 for (auto it = this->begin(); it != this->end(); ++it)
19813 {
19814 if (m_compare(it->first, key))
19815 {
19816 return {it, false};
19817 }
19818 }
19819 Container::emplace_back(key, std::forward<T>(t));
19820 return {std::prev(this->end()), true};
19821 }
19822
19823 template<class KeyType, detail::enable_if_t<
19824 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
19825 std::pair<iterator, bool> emplace(KeyType && key, T && t)
19826 {
19827 for (auto it = this->begin(); it != this->end(); ++it)
19828 {
19829 if (m_compare(it->first, key))
19830 {
19831 return {it, false};
19832 }
19833 }
19834 Container::emplace_back(std::forward<KeyType>(key), std::forward<T>(t));
19835 return {std::prev(this->end()), true};
19836 }
19837
19838 T& operator[](const key_type& key)
19839 {
19840 return emplace(key, T{}).first->second;
19841 }
19842
19843 template<class KeyType, detail::enable_if_t<
19844 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
19845 T & operator[](KeyType && key)
19846 {
19847 return emplace(std::forward<KeyType>(key), T{}).first->second;
19848 }
19849
19850 const T& operator[](const key_type& key) const
19851 {
19852 return at(key);
19853 }
19854
19855 template<class KeyType, detail::enable_if_t<
19856 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
19857 const T & operator[](KeyType && key) const
19858 {
19859 return at(std::forward<KeyType>(key));
19860 }
19861
19862 T& at(const key_type& key)
19863 {
19864 for (auto it = this->begin(); it != this->end(); ++it)
19865 {
19866 if (m_compare(it->first, key))
19867 {
19868 return it->second;
19869 }
19870 }
19871
19872 JSON_THROW(std::out_of_range("key not found"));
19873 }
19874
19875 template<class KeyType, detail::enable_if_t<
19876 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
19877 T & at(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
19878 {
19879 for (auto it = this->begin(); it != this->end(); ++it)
19880 {
19881 if (m_compare(it->first, key))
19882 {
19883 return it->second;
19884 }
19885 }
19886
19887 JSON_THROW(std::out_of_range("key not found"));
19888 }
19889
19890 const T& at(const key_type& key) const
19891 {
19892 for (auto it = this->begin(); it != this->end(); ++it)
19893 {
19894 if (m_compare(it->first, key))
19895 {
19896 return it->second;
19897 }
19898 }
19899
19900 JSON_THROW(std::out_of_range("key not found"));
19901 }
19902
19903 template<class KeyType, detail::enable_if_t<
19904 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
19905 const T & at(KeyType && key) const // NOLINT(cppcoreguidelines-missing-std-forward)
19906 {
19907 for (auto it = this->begin(); it != this->end(); ++it)
19908 {
19909 if (m_compare(it->first, key))
19910 {
19911 return it->second;
19912 }
19913 }
19914
19915 JSON_THROW(std::out_of_range("key not found"));
19916 }
19917
19918 size_type erase(const key_type& key)
19919 {
19920 for (auto it = this->begin(); it != this->end(); ++it)
19921 {
19922 if (m_compare(it->first, key))
19923 {
19924 // Since we cannot move const Keys, re-construct them in place
19925 for (auto next = it; ++next != this->end(); ++it)
19926 {
19927 it->~value_type(); // Destroy but keep allocation
19928 new (&*it) value_type{std::move(*next)};
19929 }
19930 Container::pop_back();
19931 return 1;
19932 }
19933 }
19934 return 0;
19935 }
19936
19937 template<class KeyType, detail::enable_if_t<
19938 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
19939 size_type erase(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
19940 {
19941 for (auto it = this->begin(); it != this->end(); ++it)
19942 {
19943 if (m_compare(it->first, key))
19944 {
19945 // Since we cannot move const Keys, re-construct them in place
19946 for (auto next = it; ++next != this->end(); ++it)
19947 {
19948 it->~value_type(); // Destroy but keep allocation
19949 new (&*it) value_type{std::move(*next)};
19950 }
19951 Container::pop_back();
19952 return 1;
19953 }
19954 }
19955 return 0;
19956 }
19957
19958 iterator erase(iterator pos)
19959 {
19960 return erase(pos, std::next(pos));
19961 }
19962
19963 iterator erase(iterator first, iterator last)
19964 {
19965 if (first == last)
19966 {
19967 return first;
19968 }
19969
19970 const auto elements_affected = std::distance(first, last);
19971 const auto offset = std::distance(Container::begin(), first);
19972
19973 // This is the start situation. We need to delete elements_affected
19974 // elements (3 in this example: e, f, g), and need to return an
19975 // iterator past the last deleted element (h in this example).
19976 // Note that offset is the distance from the start of the vector
19977 // to first. We will need this later.
19978
19979 // [ a, b, c, d, e, f, g, h, i, j ]
19980 // ^ ^
19981 // first last
19982
19983 // Since we cannot move const Keys, we re-construct them in place.
19984 // We start at first and re-construct (viz. copy) the elements from
19985 // the back of the vector. Example for first iteration:
19986
19987 // ,--------.
19988 // v | destroy e and re-construct with h
19989 // [ a, b, c, d, e, f, g, h, i, j ]
19990 // ^ ^
19991 // it it + elements_affected
19992
19993 for (auto it = first; std::next(it, elements_affected) != Container::end(); ++it)
19994 {
19995 it->~value_type(); // destroy but keep allocation
19996 new (&*it) value_type{std::move(*std::next(it, elements_affected))}; // "move" next element to it
19997 }
19998
19999 // [ a, b, c, d, h, i, j, h, i, j ]
20000 // ^ ^
20001 // first last
20002
20003 // remove the unneeded elements at the end of the vector
20004 Container::resize(this->size() - static_cast<size_type>(elements_affected));
20005
20006 // [ a, b, c, d, h, i, j ]
20007 // ^ ^
20008 // first last
20009
20010 // first is now pointing past the last deleted element, but we cannot
20011 // use this iterator, because it may have been invalidated by the
20012 // resize call. Instead, we can return begin() + offset.
20013 return Container::begin() + offset;
20014 }
20015
20016 size_type count(const key_type& key) const
20017 {
20018 for (auto it = this->begin(); it != this->end(); ++it)
20019 {
20020 if (m_compare(it->first, key))
20021 {
20022 return 1;
20023 }
20024 }
20025 return 0;
20026 }
20027
20028 template<class KeyType, detail::enable_if_t<
20029 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
20030 size_type count(KeyType && key) const // NOLINT(cppcoreguidelines-missing-std-forward)
20031 {
20032 for (auto it = this->begin(); it != this->end(); ++it)
20033 {
20034 if (m_compare(it->first, key))
20035 {
20036 return 1;
20037 }
20038 }
20039 return 0;
20040 }
20041
20042 iterator find(const key_type& key)
20043 {
20044 for (auto it = this->begin(); it != this->end(); ++it)
20045 {
20046 if (m_compare(it->first, key))
20047 {
20048 return it;
20049 }
20050 }
20051 return Container::end();
20052 }
20053
20054 template<class KeyType, detail::enable_if_t<
20055 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
20056 iterator find(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
20057 {
20058 for (auto it = this->begin(); it != this->end(); ++it)
20059 {
20060 if (m_compare(it->first, key))
20061 {
20062 return it;
20063 }
20064 }
20065 return Container::end();
20066 }
20067
20068 const_iterator find(const key_type& key) const
20069 {
20070 for (auto it = this->begin(); it != this->end(); ++it)
20071 {
20072 if (m_compare(it->first, key))
20073 {
20074 return it;
20075 }
20076 }
20077 return Container::end();
20078 }
20079
20080 std::pair<iterator, bool> insert( value_type&& value )
20081 {
20082 return emplace(value.first, std::move(value.second));
20083 }
20084
20085 std::pair<iterator, bool> insert( const value_type& value )
20086 {
20087 for (auto it = this->begin(); it != this->end(); ++it)
20088 {
20089 if (m_compare(it->first, value.first))
20090 {
20091 return {it, false};
20092 }
20093 }
20094 Container::push_back(value);
20095 return {--this->end(), true};
20096 }
20097
20098 template<typename InputIt>
20099 using require_input_iter = typename std::enable_if<std::is_convertible<typename std::iterator_traits<InputIt>::iterator_category,
20100 std::input_iterator_tag>::value>::type;
20101
20102 template<typename InputIt, typename = require_input_iter<InputIt>>
20103 void insert(InputIt first, InputIt last)
20104 {
20105 for (auto it = first; it != last; ++it)
20106 {
20107 insert(*it);
20108 }
20109 }
20110
20111 private:
20112 JSON_NO_UNIQUE_ADDRESS key_compare m_compare = key_compare();
20113 };
20114
20115 NLOHMANN_JSON_NAMESPACE_END
20116
20117
20118 #if defined(JSON_HAS_CPP_17)
20119 #if JSON_HAS_STATIC_RTTI
20120 #include <any>
20121 #endif
20122 #include <string_view>
20123 #endif
20124
20125 /*!
20126 @brief namespace for Niels Lohmann
20127 @see https://github.com/nlohmann
20128 @since version 1.0.0
20129 */
20130 NLOHMANN_JSON_NAMESPACE_BEGIN
20131
20132 /*!
20133 @brief a class to store JSON values
20134
20135 @internal
20136 @invariant The member variables @a m_value and @a m_type have the following
20137 relationship:
20138 - If `m_type == value_t::object`, then `m_value.object != nullptr`.
20139 - If `m_type == value_t::array`, then `m_value.array != nullptr`.
20140 - If `m_type == value_t::string`, then `m_value.string != nullptr`.
20141 The invariants are checked by member function assert_invariant().
20142
20143 @note ObjectType trick from https://stackoverflow.com/a/9860911
20144 @endinternal
20145
20146 @since version 1.0.0
20147
20148 @nosubgrouping
20149 */
20150 NLOHMANN_BASIC_JSON_TPL_DECLARATION
20151 class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions)
20152 : public ::nlohmann::detail::json_base_class<CustomBaseClass>
20153 {
20154 private:
20155 template<detail::value_t> friend struct detail::external_constructor;
20156
20157 template<typename>
20158 friend class ::nlohmann::json_pointer;
20159 // can be restored when json_pointer backwards compatibility is removed
20160 // friend ::nlohmann::json_pointer<StringType>;
20161
20162 template<typename BasicJsonType, typename InputType>
20163 friend class ::nlohmann::detail::parser;
20164 friend ::nlohmann::detail::serializer<basic_json>;
20165 template<typename BasicJsonType>
20166 friend class ::nlohmann::detail::iter_impl;
20167 template<typename BasicJsonType, typename CharType>
20168 friend class ::nlohmann::detail::binary_writer;
20169 template<typename BasicJsonType, typename InputType, typename SAX>
20170 friend class ::nlohmann::detail::binary_reader;
20171 template<typename BasicJsonType, typename InputAdapterType>
20172 friend class ::nlohmann::detail::json_sax_dom_parser;
20173 template<typename BasicJsonType, typename InputAdapterType>
20174 friend class ::nlohmann::detail::json_sax_dom_callback_parser;
20175 friend class ::nlohmann::detail::exception;
20176
20177 /// workaround type for MSVC
20178 using basic_json_t = NLOHMANN_BASIC_JSON_TPL;
20179 using json_base_class_t = ::nlohmann::detail::json_base_class<CustomBaseClass>;
20180
20181 JSON_PRIVATE_UNLESS_TESTED:
20182 // convenience aliases for types residing in namespace detail;
20183 using lexer = ::nlohmann::detail::lexer_base<basic_json>;
20184
20185 template<typename InputAdapterType>
20186 static ::nlohmann::detail::parser<basic_json, InputAdapterType> parser(
20187 InputAdapterType adapter,
20188 detail::parser_callback_t<basic_json>cb = nullptr,
20189 const bool allow_exceptions = true,
20190 const bool ignore_comments = false
20191 )
20192 {
20193 return ::nlohmann::detail::parser<basic_json, InputAdapterType>(std::move(adapter),
20194 std::move(cb), allow_exceptions, ignore_comments);
20195 }
20196
20197 private:
20198 using primitive_iterator_t = ::nlohmann::detail::primitive_iterator_t;
20199 template<typename BasicJsonType>
20200 using internal_iterator = ::nlohmann::detail::internal_iterator<BasicJsonType>;
20201 template<typename BasicJsonType>
20202 using iter_impl = ::nlohmann::detail::iter_impl<BasicJsonType>;
20203 template<typename Iterator>
20204 using iteration_proxy = ::nlohmann::detail::iteration_proxy<Iterator>;
20205 template<typename Base> using json_reverse_iterator = ::nlohmann::detail::json_reverse_iterator<Base>;
20206
20207 template<typename CharType>
20208 using output_adapter_t = ::nlohmann::detail::output_adapter_t<CharType>;
20209
20210 template<typename InputType>
20211 using binary_reader = ::nlohmann::detail::binary_reader<basic_json, InputType>;
20212 template<typename CharType> using binary_writer = ::nlohmann::detail::binary_writer<basic_json, CharType>;
20213
20214 JSON_PRIVATE_UNLESS_TESTED:
20215 using serializer = ::nlohmann::detail::serializer<basic_json>;
20216
20217 public:
20218 using value_t = detail::value_t;
20219 /// JSON Pointer, see @ref nlohmann::json_pointer
20220 using json_pointer = ::nlohmann::json_pointer<StringType>;
20221 template<typename T, typename SFINAE>
20222 using json_serializer = JSONSerializer<T, SFINAE>;
20223 /// how to treat decoding errors
20224 using error_handler_t = detail::error_handler_t;
20225 /// how to treat CBOR tags
20226 using cbor_tag_handler_t = detail::cbor_tag_handler_t;
20227 /// how to encode BJData
20228 using bjdata_version_t = detail::bjdata_version_t;
20229 /// helper type for initializer lists of basic_json values
20230 using initializer_list_t = std::initializer_list<detail::json_ref<basic_json>>;
20231
20232 using input_format_t = detail::input_format_t;
20233 /// SAX interface type, see @ref nlohmann::json_sax
20234 using json_sax_t = json_sax<basic_json>;
20235
20236 ////////////////
20237 // exceptions //
20238 ////////////////
20239
20240 /// @name exceptions
20241 /// Classes to implement user-defined exceptions.
20242 /// @{
20243
20244 using exception = detail::exception;
20245 using parse_error = detail::parse_error;
20246 using invalid_iterator = detail::invalid_iterator;
20247 using type_error = detail::type_error;
20248 using out_of_range = detail::out_of_range;
20249 using other_error = detail::other_error;
20250
20251 /// @}
20252
20253 /////////////////////
20254 // container types //
20255 /////////////////////
20256
20257 /// @name container types
20258 /// The canonic container types to use @ref basic_json like any other STL
20259 /// container.
20260 /// @{
20261
20262 /// the type of elements in a basic_json container
20263 using value_type = basic_json;
20264
20265 /// the type of an element reference
20266 using reference = value_type&;
20267 /// the type of an element const reference
20268 using const_reference = const value_type&;
20269
20270 /// a type to represent differences between iterators
20271 using difference_type = std::ptrdiff_t;
20272 /// a type to represent container sizes
20273 using size_type = std::size_t;
20274
20275 /// the allocator type
20276 using allocator_type = AllocatorType<basic_json>;
20277
20278 /// the type of an element pointer
20279 using pointer = typename std::allocator_traits<allocator_type>::pointer;
20280 /// the type of an element const pointer
20281 using const_pointer = typename std::allocator_traits<allocator_type>::const_pointer;
20282
20283 /// an iterator for a basic_json container
20284 using iterator = iter_impl<basic_json>;
20285 /// a const iterator for a basic_json container
20286 using const_iterator = iter_impl<const basic_json>;
20287 /// a reverse iterator for a basic_json container
20288 using reverse_iterator = json_reverse_iterator<typename basic_json::iterator>;
20289 /// a const reverse iterator for a basic_json container
20290 using const_reverse_iterator = json_reverse_iterator<typename basic_json::const_iterator>;
20291
20292 /// @}
20293
20294 /// @brief returns the allocator associated with the container
20295 /// @sa https://json.nlohmann.me/api/basic_json/get_allocator/
20296 static allocator_type get_allocator()
20297 {
20298 return allocator_type();
20299 }
20300
20301 /// @brief returns version information on the library
20302 /// @sa https://json.nlohmann.me/api/basic_json/meta/
20303 JSON_HEDLEY_WARN_UNUSED_RESULT
20304 static basic_json meta()
20305 {
20306 basic_json result;
20307
20308 result["copyright"] = "(C) 2013-2025 Niels Lohmann";
20309 result["name"] = "JSON for Modern C++";
20310 result["url"] = "https://github.com/nlohmann/json";
20311 result["version"]["string"] =
20312 detail::concat(std::to_string(NLOHMANN_JSON_VERSION_MAJOR), '.',
20313 std::to_string(NLOHMANN_JSON_VERSION_MINOR), '.',
20314 std::to_string(NLOHMANN_JSON_VERSION_PATCH));
20315 result["version"]["major"] = NLOHMANN_JSON_VERSION_MAJOR;
20316 result["version"]["minor"] = NLOHMANN_JSON_VERSION_MINOR;
20317 result["version"]["patch"] = NLOHMANN_JSON_VERSION_PATCH;
20318
20319 #ifdef _WIN32
20320 result["platform"] = "win32";
20321 #elif defined __linux__
20322 result["platform"] = "linux";
20323 #elif defined __APPLE__
20324 result["platform"] = "apple";
20325 #elif defined __unix__
20326 result["platform"] = "unix";
20327 #else
20328 result["platform"] = "unknown";
20329 #endif
20330
20331 #if defined(__ICC) || defined(__INTEL_COMPILER)
20332 result["compiler"] = {{"family", "icc"}, {"version", __INTEL_COMPILER}};
20333 #elif defined(__clang__)
20334 result["compiler"] = {{"family", "clang"}, {"version", __clang_version__}};
20335 #elif defined(__GNUC__) || defined(__GNUG__)
20336 result["compiler"] = {{"family", "gcc"}, {"version", detail::concat(
20337 std::to_string(__GNUC__), '.',
20338 std::to_string(__GNUC_MINOR__), '.',
20339 std::to_string(__GNUC_PATCHLEVEL__))
20340 }
20341 };
20342 #elif defined(__HP_cc) || defined(__HP_aCC)
20343 result["compiler"] = "hp"
20344 #elif defined(__IBMCPP__)
20345 result["compiler"] = {{"family", "ilecpp"}, {"version", __IBMCPP__}};
20346 #elif defined(_MSC_VER)
20347 result["compiler"] = {{"family", "msvc"}, {"version", _MSC_VER}};
20348 #elif defined(__PGI)
20349 result["compiler"] = {{"family", "pgcpp"}, {"version", __PGI}};
20350 #elif defined(__SUNPRO_CC)
20351 result["compiler"] = {{"family", "sunpro"}, {"version", __SUNPRO_CC}};
20352 #else
20353 result["compiler"] = {{"family", "unknown"}, {"version", "unknown"}};
20354 #endif
20355
20356 #if defined(_MSVC_LANG)
20357 result["compiler"]["c++"] = std::to_string(_MSVC_LANG);
20358 #elif defined(__cplusplus)
20359 result["compiler"]["c++"] = std::to_string(__cplusplus);
20360 #else
20361 result["compiler"]["c++"] = "unknown";
20362 #endif
20363 return result;
20364 }
20365
20366 ///////////////////////////
20367 // JSON value data types //
20368 ///////////////////////////
20369
20370 /// @name JSON value data types
20371 /// The data types to store a JSON value. These types are derived from
20372 /// the template arguments passed to class @ref basic_json.
20373 /// @{
20374
20375 /// @brief default object key comparator type
20376 /// The actual object key comparator type (@ref object_comparator_t) may be
20377 /// different.
20378 /// @sa https://json.nlohmann.me/api/basic_json/default_object_comparator_t/
20379 #if defined(JSON_HAS_CPP_14)
20380 // use of transparent comparator avoids unnecessary repeated construction of temporaries
20381 // in functions involving lookup by key with types other than object_t::key_type (aka. StringType)
20382 using default_object_comparator_t = std::less<>;
20383 #else
20384 using default_object_comparator_t = std::less<StringType>;
20385 #endif
20386
20387 /// @brief a type for an object
20388 /// @sa https://json.nlohmann.me/api/basic_json/object_t/
20389 using object_t = ObjectType<StringType,
20390 basic_json,
20391 default_object_comparator_t,
20392 AllocatorType<std::pair<const StringType,
20393 basic_json>>>;
20394
20395 /// @brief a type for an array
20396 /// @sa https://json.nlohmann.me/api/basic_json/array_t/
20397 using array_t = ArrayType<basic_json, AllocatorType<basic_json>>;
20398
20399 /// @brief a type for a string
20400 /// @sa https://json.nlohmann.me/api/basic_json/string_t/
20401 using string_t = StringType;
20402
20403 /// @brief a type for a boolean
20404 /// @sa https://json.nlohmann.me/api/basic_json/boolean_t/
20405 using boolean_t = BooleanType;
20406
20407 /// @brief a type for a number (integer)
20408 /// @sa https://json.nlohmann.me/api/basic_json/number_integer_t/
20409 using number_integer_t = NumberIntegerType;
20410
20411 /// @brief a type for a number (unsigned)
20412 /// @sa https://json.nlohmann.me/api/basic_json/number_unsigned_t/
20413 using number_unsigned_t = NumberUnsignedType;
20414
20415 /// @brief a type for a number (floating-point)
20416 /// @sa https://json.nlohmann.me/api/basic_json/number_float_t/
20417 using number_float_t = NumberFloatType;
20418
20419 /// @brief a type for a packed binary type
20420 /// @sa https://json.nlohmann.me/api/basic_json/binary_t/
20421 using binary_t = nlohmann::byte_container_with_subtype<BinaryType>;
20422
20423 /// @brief object key comparator type
20424 /// @sa https://json.nlohmann.me/api/basic_json/object_comparator_t/
20425 using object_comparator_t = detail::actual_object_comparator_t<basic_json>;
20426
20427 /// @}
20428
20429 private:
20430
20431 /// helper for exception-safe object creation
20432 template<typename T, typename... Args>
20433 JSON_HEDLEY_RETURNS_NON_NULL
20434 static T* create(Args&& ... args)
20435 {
20436 AllocatorType<T> alloc;
20437 using AllocatorTraits = std::allocator_traits<AllocatorType<T>>;
20438
20439 auto deleter = [&](T * obj)
20440 {
20441 AllocatorTraits::deallocate(alloc, obj, 1);
20442 };
20443 std::unique_ptr<T, decltype(deleter)> obj(AllocatorTraits::allocate(alloc, 1), deleter);
20444 AllocatorTraits::construct(alloc, obj.get(), std::forward<Args>(args)...);
20445 JSON_ASSERT(obj != nullptr);
20446 return obj.release();
20447 }
20448
20449 ////////////////////////
20450 // JSON value storage //
20451 ////////////////////////
20452
20453 JSON_PRIVATE_UNLESS_TESTED:
20454 /*!
20455 @brief a JSON value
20456
20457 The actual storage for a JSON value of the @ref basic_json class. This
20458 union combines the different storage types for the JSON value types
20459 defined in @ref value_t.
20460
20461 JSON type | value_t type | used type
20462 --------- | --------------- | ------------------------
20463 object | object | pointer to @ref object_t
20464 array | array | pointer to @ref array_t
20465 string | string | pointer to @ref string_t
20466 boolean | boolean | @ref boolean_t
20467 number | number_integer | @ref number_integer_t
20468 number | number_unsigned | @ref number_unsigned_t
20469 number | number_float | @ref number_float_t
20470 binary | binary | pointer to @ref binary_t
20471 null | null | *no value is stored*
20472
20473 @note Variable-length types (objects, arrays, and strings) are stored as
20474 pointers. The size of the union should not exceed 64 bits if the default
20475 value types are used.
20476
20477 @since version 1.0.0
20478 */
20479 union json_value
20480 {
20481 /// object (stored with pointer to save storage)
20482 object_t* object;
20483 /// array (stored with pointer to save storage)
20484 array_t* array;
20485 /// string (stored with pointer to save storage)
20486 string_t* string;
20487 /// binary (stored with pointer to save storage)
20488 binary_t* binary;
20489 /// boolean
20490 boolean_t boolean;
20491 /// number (integer)
20492 number_integer_t number_integer;
20493 /// number (unsigned integer)
20494 number_unsigned_t number_unsigned;
20495 /// number (floating-point)
20496 number_float_t number_float;
20497
20498 /// default constructor (for null values)
20499 json_value() = default;
20500 /// constructor for booleans
20501 json_value(boolean_t v) noexcept : boolean(v) {}
20502 /// constructor for numbers (integer)
20503 json_value(number_integer_t v) noexcept : number_integer(v) {}
20504 /// constructor for numbers (unsigned)
20505 json_value(number_unsigned_t v) noexcept : number_unsigned(v) {}
20506 /// constructor for numbers (floating-point)
20507 json_value(number_float_t v) noexcept : number_float(v) {}
20508 /// constructor for empty values of a given type
20509 json_value(value_t t)
20510 {
20511 switch (t)
20512 {
20513 case value_t::object:
20514 {
20515 object = create<object_t>();
20516 break;
20517 }
20518
20519 case value_t::array:
20520 {
20521 array = create<array_t>();
20522 break;
20523 }
20524
20525 case value_t::string:
20526 {
20527 string = create<string_t>("");
20528 break;
20529 }
20530
20531 case value_t::binary:
20532 {
20533 binary = create<binary_t>();
20534 break;
20535 }
20536
20537 case value_t::boolean:
20538 {
20539 boolean = static_cast<boolean_t>(false);
20540 break;
20541 }
20542
20543 case value_t::number_integer:
20544 {
20545 number_integer = static_cast<number_integer_t>(0);
20546 break;
20547 }
20548
20549 case value_t::number_unsigned:
20550 {
20551 number_unsigned = static_cast<number_unsigned_t>(0);
20552 break;
20553 }
20554
20555 case value_t::number_float:
20556 {
20557 number_float = static_cast<number_float_t>(0.0);
20558 break;
20559 }
20560
20561 case value_t::null:
20562 {
20563 object = nullptr; // silence warning, see #821
20564 break;
20565 }
20566
20567 case value_t::discarded:
20568 default:
20569 {
20570 object = nullptr; // silence warning, see #821
20571 if (JSON_HEDLEY_UNLIKELY(t == value_t::null))
20572 {
20573 JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.12.0", nullptr)); // LCOV_EXCL_LINE
20574 }
20575 break;
20576 }
20577 }
20578 }
20579
20580 /// constructor for strings
20581 json_value(const string_t& value) : string(create<string_t>(value)) {}
20582
20583 /// constructor for rvalue strings
20584 json_value(string_t&& value) : string(create<string_t>(std::move(value))) {}
20585
20586 /// constructor for objects
20587 json_value(const object_t& value) : object(create<object_t>(value)) {}
20588
20589 /// constructor for rvalue objects
20590 json_value(object_t&& value) : object(create<object_t>(std::move(value))) {}
20591
20592 /// constructor for arrays
20593 json_value(const array_t& value) : array(create<array_t>(value)) {}
20594
20595 /// constructor for rvalue arrays
20596 json_value(array_t&& value) : array(create<array_t>(std::move(value))) {}
20597
20598 /// constructor for binary arrays
20599 json_value(const typename binary_t::container_type& value) : binary(create<binary_t>(value)) {}
20600
20601 /// constructor for rvalue binary arrays
20602 json_value(typename binary_t::container_type&& value) : binary(create<binary_t>(std::move(value))) {}
20603
20604 /// constructor for binary arrays (internal type)
20605 json_value(const binary_t& value) : binary(create<binary_t>(value)) {}
20606
20607 /// constructor for rvalue binary arrays (internal type)
20608 json_value(binary_t&& value) : binary(create<binary_t>(std::move(value))) {}
20609
20610 void destroy(value_t t)
20611 {
20612 if (
20613 (t == value_t::object && object == nullptr) ||
20614 (t == value_t::array && array == nullptr) ||
20615 (t == value_t::string && string == nullptr) ||
20616 (t == value_t::binary && binary == nullptr)
20617 )
20618 {
20619 //not initialized (e.g. due to exception in the ctor)
20620 return;
20621 }
20622 if (t == value_t::array || t == value_t::object)
20623 {
20624 // flatten the current json_value to a heap-allocated stack
20625 std::vector<basic_json> stack;
20626
20627 // move the top-level items to stack
20628 if (t == value_t::array)
20629 {
20630 stack.reserve(array->size());
20631 std::move(array->begin(), array->end(), std::back_inserter(stack));
20632 }
20633 else
20634 {
20635 stack.reserve(object->size());
20636 for (auto&& it : *object)
20637 {
20638 stack.push_back(std::move(it.second));
20639 }
20640 }
20641
20642 while (!stack.empty())
20643 {
20644 // move the last item to local variable to be processed
20645 basic_json current_item(std::move(stack.back()));
20646 stack.pop_back();
20647
20648 // if current_item is array/object, move
20649 // its children to the stack to be processed later
20650 if (current_item.is_array())
20651 {
20652 std::move(current_item.m_data.m_value.array->begin(), current_item.m_data.m_value.array->end(), std::back_inserter(stack));
20653
20654 current_item.m_data.m_value.array->clear();
20655 }
20656 else if (current_item.is_object())
20657 {
20658 for (auto&& it : *current_item.m_data.m_value.object)
20659 {
20660 stack.push_back(std::move(it.second));
20661 }
20662
20663 current_item.m_data.m_value.object->clear();
20664 }
20665
20666 // it's now safe that current_item get destructed
20667 // since it doesn't have any children
20668 }
20669 }
20670
20671 switch (t)
20672 {
20673 case value_t::object:
20674 {
20675 AllocatorType<object_t> alloc;
20676 std::allocator_traits<decltype(alloc)>::destroy(alloc, object);
20677 std::allocator_traits<decltype(alloc)>::deallocate(alloc, object, 1);
20678 break;
20679 }
20680
20681 case value_t::array:
20682 {
20683 AllocatorType<array_t> alloc;
20684 std::allocator_traits<decltype(alloc)>::destroy(alloc, array);
20685 std::allocator_traits<decltype(alloc)>::deallocate(alloc, array, 1);
20686 break;
20687 }
20688
20689 case value_t::string:
20690 {
20691 AllocatorType<string_t> alloc;
20692 std::allocator_traits<decltype(alloc)>::destroy(alloc, string);
20693 std::allocator_traits<decltype(alloc)>::deallocate(alloc, string, 1);
20694 break;
20695 }
20696
20697 case value_t::binary:
20698 {
20699 AllocatorType<binary_t> alloc;
20700 std::allocator_traits<decltype(alloc)>::destroy(alloc, binary);
20701 std::allocator_traits<decltype(alloc)>::deallocate(alloc, binary, 1);
20702 break;
20703 }
20704
20705 case value_t::null:
20706 case value_t::boolean:
20707 case value_t::number_integer:
20708 case value_t::number_unsigned:
20709 case value_t::number_float:
20710 case value_t::discarded:
20711 default:
20712 {
20713 break;
20714 }
20715 }
20716 }
20717 };
20718
20719 private:
20720 /*!
20721 @brief checks the class invariants
20722
20723 This function asserts the class invariants. It needs to be called at the
20724 end of every constructor to make sure that created objects respect the
20725 invariant. Furthermore, it has to be called each time the type of a JSON
20726 value is changed, because the invariant expresses a relationship between
20727 @a m_type and @a m_value.
20728
20729 Furthermore, the parent relation is checked for arrays and objects: If
20730 @a check_parents true and the value is an array or object, then the
20731 container's elements must have the current value as parent.
20732
20733 @param[in] check_parents whether the parent relation should be checked.
20734 The value is true by default and should only be set to false
20735 during destruction of objects when the invariant does not
20736 need to hold.
20737 */
20738 void assert_invariant(bool check_parents = true) const noexcept
20739 {
20740 JSON_ASSERT(m_data.m_type != value_t::object || m_data.m_value.object != nullptr);
20741 JSON_ASSERT(m_data.m_type != value_t::array || m_data.m_value.array != nullptr);
20742 JSON_ASSERT(m_data.m_type != value_t::string || m_data.m_value.string != nullptr);
20743 JSON_ASSERT(m_data.m_type != value_t::binary || m_data.m_value.binary != nullptr);
20744
20745 #if JSON_DIAGNOSTICS
20746 JSON_TRY
20747 {
20748 // cppcheck-suppress assertWithSideEffect
20749 JSON_ASSERT(!check_parents || !is_structured() || std::all_of(begin(), end(), [this](const basic_json & j)
20750 {
20751 return j.m_parent == this;
20752 }));
20753 }
20754 JSON_CATCH(...) {} // LCOV_EXCL_LINE
20755 #endif
20756 static_cast<void>(check_parents);
20757 }
20758
20759 void set_parents()
20760 {
20761 #if JSON_DIAGNOSTICS
20762 switch (m_data.m_type)
20763 {
20764 case value_t::array:
20765 {
20766 for (auto& element : *m_data.m_value.array)
20767 {
20768 element.m_parent = this;
20769 }
20770 break;
20771 }
20772
20773 case value_t::object:
20774 {
20775 for (auto& element : *m_data.m_value.object)
20776 {
20777 element.second.m_parent = this;
20778 }
20779 break;
20780 }
20781
20782 case value_t::null:
20783 case value_t::string:
20784 case value_t::boolean:
20785 case value_t::number_integer:
20786 case value_t::number_unsigned:
20787 case value_t::number_float:
20788 case value_t::binary:
20789 case value_t::discarded:
20790 default:
20791 break;
20792 }
20793 #endif
20794 }
20795
20796 iterator set_parents(iterator it, typename iterator::difference_type count_set_parents)
20797 {
20798 #if JSON_DIAGNOSTICS
20799 for (typename iterator::difference_type i = 0; i < count_set_parents; ++i)
20800 {
20801 (it + i)->m_parent = this;
20802 }
20803 #else
20804 static_cast<void>(count_set_parents);
20805 #endif
20806 return it;
20807 }
20808
20809 reference set_parent(reference j, std::size_t old_capacity = detail::unknown_size())
20810 {
20811 #if JSON_DIAGNOSTICS
20812 if (old_capacity != detail::unknown_size())
20813 {
20814 // see https://github.com/nlohmann/json/issues/2838
20815 JSON_ASSERT(type() == value_t::array);
20816 if (JSON_HEDLEY_UNLIKELY(m_data.m_value.array->capacity() != old_capacity))
20817 {
20818 // capacity has changed: update all parents
20819 set_parents();
20820 return j;
20821 }
20822 }
20823
20824 // ordered_json uses a vector internally, so pointers could have
20825 // been invalidated; see https://github.com/nlohmann/json/issues/2962
20826 #ifdef JSON_HEDLEY_MSVC_VERSION
20827 #pragma warning(push )
20828 #pragma warning(disable : 4127) // ignore warning to replace if with if constexpr
20829 #endif
20830 if (detail::is_ordered_map<object_t>::value)
20831 {
20832 set_parents();
20833 return j;
20834 }
20835 #ifdef JSON_HEDLEY_MSVC_VERSION
20836 #pragma warning( pop )
20837 #endif
20838
20839 j.m_parent = this;
20840 #else
20841 static_cast<void>(j);
20842 static_cast<void>(old_capacity);
20843 #endif
20844 return j;
20845 }
20846
20847 public:
20848 //////////////////////////
20849 // JSON parser callback //
20850 //////////////////////////
20851
20852 /// @brief parser event types
20853 /// @sa https://json.nlohmann.me/api/basic_json/parse_event_t/
20854 using parse_event_t = detail::parse_event_t;
20855
20856 /// @brief per-element parser callback type
20857 /// @sa https://json.nlohmann.me/api/basic_json/parser_callback_t/
20858 using parser_callback_t = detail::parser_callback_t<basic_json>;
20859
20860 //////////////////
20861 // constructors //
20862 //////////////////
20863
20864 /// @name constructors and destructors
20865 /// Constructors of class @ref basic_json, copy/move constructor, copy
20866 /// assignment, static functions creating objects, and the destructor.
20867 /// @{
20868
20869 /// @brief create an empty value with a given type
20870 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
20871 basic_json(const value_t v)
20872 : m_data(v)
20873 {
20874 assert_invariant();
20875 }
20876
20877 /// @brief create a null object
20878 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
20879 basic_json(std::nullptr_t = nullptr) noexcept // NOLINT(bugprone-exception-escape)
20880 : basic_json(value_t::null)
20881 {
20882 assert_invariant();
20883 }
20884
20885 /// @brief create a JSON value from compatible types
20886 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
20887 template < typename CompatibleType,
20888 typename U = detail::uncvref_t<CompatibleType>,
20889 detail::enable_if_t <
20890 !detail::is_basic_json<U>::value && detail::is_compatible_type<basic_json_t, U>::value, int > = 0 >
20891 basic_json(CompatibleType && val) noexcept(noexcept( // NOLINT(bugprone-forwarding-reference-overload,bugprone-exception-escape)
20892 JSONSerializer<U>::to_json(std::declval<basic_json_t&>(),
20893 std::forward<CompatibleType>(val))))
20894 {
20895 JSONSerializer<U>::to_json(*this, std::forward<CompatibleType>(val));
20896 set_parents();
20897 assert_invariant();
20898 }
20899
20900 /// @brief create a JSON value from an existing one
20901 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
20902 template < typename BasicJsonType,
20903 detail::enable_if_t <
20904 detail::is_basic_json<BasicJsonType>::value&& !std::is_same<basic_json, BasicJsonType>::value, int > = 0 >
20905 basic_json(const BasicJsonType& val)
20906 #if JSON_DIAGNOSTIC_POSITIONS
20907 : start_position(val.start_pos()),
20908 end_position(val.end_pos())
20909 #endif
20910 {
20911 using other_boolean_t = typename BasicJsonType::boolean_t;
20912 using other_number_float_t = typename BasicJsonType::number_float_t;
20913 using other_number_integer_t = typename BasicJsonType::number_integer_t;
20914 using other_number_unsigned_t = typename BasicJsonType::number_unsigned_t;
20915 using other_string_t = typename BasicJsonType::string_t;
20916 using other_object_t = typename BasicJsonType::object_t;
20917 using other_array_t = typename BasicJsonType::array_t;
20918 using other_binary_t = typename BasicJsonType::binary_t;
20919
20920 switch (val.type())
20921 {
20922 case value_t::boolean:
20923 JSONSerializer<other_boolean_t>::to_json(*this, val.template get<other_boolean_t>());
20924 break;
20925 case value_t::number_float:
20926 JSONSerializer<other_number_float_t>::to_json(*this, val.template get<other_number_float_t>());
20927 break;
20928 case value_t::number_integer:
20929 JSONSerializer<other_number_integer_t>::to_json(*this, val.template get<other_number_integer_t>());
20930 break;
20931 case value_t::number_unsigned:
20932 JSONSerializer<other_number_unsigned_t>::to_json(*this, val.template get<other_number_unsigned_t>());
20933 break;
20934 case value_t::string:
20935 JSONSerializer<other_string_t>::to_json(*this, val.template get_ref<const other_string_t&>());
20936 break;
20937 case value_t::object:
20938 JSONSerializer<other_object_t>::to_json(*this, val.template get_ref<const other_object_t&>());
20939 break;
20940 case value_t::array:
20941 JSONSerializer<other_array_t>::to_json(*this, val.template get_ref<const other_array_t&>());
20942 break;
20943 case value_t::binary:
20944 JSONSerializer<other_binary_t>::to_json(*this, val.template get_ref<const other_binary_t&>());
20945 break;
20946 case value_t::null:
20947 *this = nullptr;
20948 break;
20949 case value_t::discarded:
20950 m_data.m_type = value_t::discarded;
20951 break;
20952 default: // LCOV_EXCL_LINE
20953 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
20954 }
20955 JSON_ASSERT(m_data.m_type == val.type());
20956
20957 set_parents();
20958 assert_invariant();
20959 }
20960
20961 /// @brief create a container (array or object) from an initializer list
20962 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
20963 basic_json(initializer_list_t init,
20964 bool type_deduction = true,
20965 value_t manual_type = value_t::array)
20966 {
20967 // check if each element is an array with two elements whose first
20968 // element is a string
20969 bool is_an_object = std::all_of(init.begin(), init.end(),
20970 [](const detail::json_ref<basic_json>& element_ref)
20971 {
20972 // The cast is to ensure op[size_type] is called, bearing in mind size_type may not be int;
20973 // (many string types can be constructed from 0 via its null-pointer guise, so we get a
20974 // broken call to op[key_type], the wrong semantics and a 4804 warning on Windows)
20975 return element_ref->is_array() && element_ref->size() == 2 && (*element_ref)[static_cast<size_type>(0)].is_string();
20976 });
20977
20978 // adjust type if type deduction is not wanted
20979 if (!type_deduction)
20980 {
20981 // if array is wanted, do not create an object though possible
20982 if (manual_type == value_t::array)
20983 {
20984 is_an_object = false;
20985 }
20986
20987 // if object is wanted but impossible, throw an exception
20988 if (JSON_HEDLEY_UNLIKELY(manual_type == value_t::object && !is_an_object))
20989 {
20990 JSON_THROW(type_error::create(301, "cannot create object from initializer list", nullptr));
20991 }
20992 }
20993
20994 if (is_an_object)
20995 {
20996 // the initializer list is a list of pairs -> create object
20997 m_data.m_type = value_t::object;
20998 m_data.m_value = value_t::object;
20999
21000 for (auto& element_ref : init)
21001 {
21002 auto element = element_ref.moved_or_copied();
21003 m_data.m_value.object->emplace(
21004 std::move(*((*element.m_data.m_value.array)[0].m_data.m_value.string)),
21005 std::move((*element.m_data.m_value.array)[1]));
21006 }
21007 }
21008 else
21009 {
21010 // the initializer list describes an array -> create array
21011 m_data.m_type = value_t::array;
21012 m_data.m_value.array = create<array_t>(init.begin(), init.end());
21013 }
21014
21015 set_parents();
21016 assert_invariant();
21017 }
21018
21019 /// @brief explicitly create a binary array (without subtype)
21020 /// @sa https://json.nlohmann.me/api/basic_json/binary/
21021 JSON_HEDLEY_WARN_UNUSED_RESULT
21022 static basic_json binary(const typename binary_t::container_type& init)
21023 {
21024 auto res = basic_json();
21025 res.m_data.m_type = value_t::binary;
21026 res.m_data.m_value = init;
21027 return res;
21028 }
21029
21030 /// @brief explicitly create a binary array (with subtype)
21031 /// @sa https://json.nlohmann.me/api/basic_json/binary/
21032 JSON_HEDLEY_WARN_UNUSED_RESULT
21033 static basic_json binary(const typename binary_t::container_type& init, typename binary_t::subtype_type subtype)
21034 {
21035 auto res = basic_json();
21036 res.m_data.m_type = value_t::binary;
21037 res.m_data.m_value = binary_t(init, subtype);
21038 return res;
21039 }
21040
21041 /// @brief explicitly create a binary array
21042 /// @sa https://json.nlohmann.me/api/basic_json/binary/
21043 JSON_HEDLEY_WARN_UNUSED_RESULT
21044 static basic_json binary(typename binary_t::container_type&& init)
21045 {
21046 auto res = basic_json();
21047 res.m_data.m_type = value_t::binary;
21048 res.m_data.m_value = std::move(init);
21049 return res;
21050 }
21051
21052 /// @brief explicitly create a binary array (with subtype)
21053 /// @sa https://json.nlohmann.me/api/basic_json/binary/
21054 JSON_HEDLEY_WARN_UNUSED_RESULT
21055 static basic_json binary(typename binary_t::container_type&& init, typename binary_t::subtype_type subtype)
21056 {
21057 auto res = basic_json();
21058 res.m_data.m_type = value_t::binary;
21059 res.m_data.m_value = binary_t(std::move(init), subtype);
21060 return res;
21061 }
21062
21063 /// @brief explicitly create an array from an initializer list
21064 /// @sa https://json.nlohmann.me/api/basic_json/array/
21065 JSON_HEDLEY_WARN_UNUSED_RESULT
21066 static basic_json array(initializer_list_t init = {})
21067 {
21068 return basic_json(init, false, value_t::array);
21069 }
21070
21071 /// @brief explicitly create an object from an initializer list
21072 /// @sa https://json.nlohmann.me/api/basic_json/object/
21073 JSON_HEDLEY_WARN_UNUSED_RESULT
21074 static basic_json object(initializer_list_t init = {})
21075 {
21076 return basic_json(init, false, value_t::object);
21077 }
21078
21079 /// @brief construct an array with count copies of given value
21080 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
21081 basic_json(size_type cnt, const basic_json& val):
21082 m_data{cnt, val}
21083 {
21084 set_parents();
21085 assert_invariant();
21086 }
21087
21088 /// @brief construct a JSON container given an iterator range
21089 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
21090 template < class InputIT, typename std::enable_if <
21091 std::is_same<InputIT, typename basic_json_t::iterator>::value ||
21092 std::is_same<InputIT, typename basic_json_t::const_iterator>::value, int >::type = 0 >
21093 basic_json(InputIT first, InputIT last) // NOLINT(performance-unnecessary-value-param)
21094 {
21095 JSON_ASSERT(first.m_object != nullptr);
21096 JSON_ASSERT(last.m_object != nullptr);
21097
21098 // make sure iterator fits the current value
21099 if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
21100 {
21101 JSON_THROW(invalid_iterator::create(201, "iterators are not compatible", nullptr));
21102 }
21103
21104 // copy type from first iterator
21105 m_data.m_type = first.m_object->m_data.m_type;
21106
21107 // check if iterator range is complete for primitive values
21108 switch (m_data.m_type)
21109 {
21110 case value_t::boolean:
21111 case value_t::number_float:
21112 case value_t::number_integer:
21113 case value_t::number_unsigned:
21114 case value_t::string:
21115 {
21116 if (JSON_HEDLEY_UNLIKELY(!first.m_it.primitive_iterator.is_begin()
21117 || !last.m_it.primitive_iterator.is_end()))
21118 {
21119 JSON_THROW(invalid_iterator::create(204, "iterators out of range", first.m_object));
21120 }
21121 break;
21122 }
21123
21124 case value_t::null:
21125 case value_t::object:
21126 case value_t::array:
21127 case value_t::binary:
21128 case value_t::discarded:
21129 default:
21130 break;
21131 }
21132
21133 switch (m_data.m_type)
21134 {
21135 case value_t::number_integer:
21136 {
21137 m_data.m_value.number_integer = first.m_object->m_data.m_value.number_integer;
21138 break;
21139 }
21140
21141 case value_t::number_unsigned:
21142 {
21143 m_data.m_value.number_unsigned = first.m_object->m_data.m_value.number_unsigned;
21144 break;
21145 }
21146
21147 case value_t::number_float:
21148 {
21149 m_data.m_value.number_float = first.m_object->m_data.m_value.number_float;
21150 break;
21151 }
21152
21153 case value_t::boolean:
21154 {
21155 m_data.m_value.boolean = first.m_object->m_data.m_value.boolean;
21156 break;
21157 }
21158
21159 case value_t::string:
21160 {
21161 m_data.m_value = *first.m_object->m_data.m_value.string;
21162 break;
21163 }
21164
21165 case value_t::object:
21166 {
21167 m_data.m_value.object = create<object_t>(first.m_it.object_iterator,
21168 last.m_it.object_iterator);
21169 break;
21170 }
21171
21172 case value_t::array:
21173 {
21174 m_data.m_value.array = create<array_t>(first.m_it.array_iterator,
21175 last.m_it.array_iterator);
21176 break;
21177 }
21178
21179 case value_t::binary:
21180 {
21181 m_data.m_value = *first.m_object->m_data.m_value.binary;
21182 break;
21183 }
21184
21185 case value_t::null:
21186 case value_t::discarded:
21187 default:
21188 JSON_THROW(invalid_iterator::create(206, detail::concat("cannot construct with iterators from ", first.m_object->type_name()), first.m_object));
21189 }
21190
21191 set_parents();
21192 assert_invariant();
21193 }
21194
21195 ///////////////////////////////////////
21196 // other constructors and destructor //
21197 ///////////////////////////////////////
21198
21199 template<typename JsonRef,
21200 detail::enable_if_t<detail::conjunction<detail::is_json_ref<JsonRef>,
21201 std::is_same<typename JsonRef::value_type, basic_json>>::value, int> = 0 >
21202 basic_json(const JsonRef& ref) : basic_json(ref.moved_or_copied()) {}
21203
21204 /// @brief copy constructor
21205 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
21206 basic_json(const basic_json& other)
21207 : json_base_class_t(other)
21208 #if JSON_DIAGNOSTIC_POSITIONS
21209 , start_position(other.start_position)
21210 , end_position(other.end_position)
21211 #endif
21212 {
21213 m_data.m_type = other.m_data.m_type;
21214 // check of passed value is valid
21215 other.assert_invariant();
21216
21217 switch (m_data.m_type)
21218 {
21219 case value_t::object:
21220 {
21221 m_data.m_value = *other.m_data.m_value.object;
21222 break;
21223 }
21224
21225 case value_t::array:
21226 {
21227 m_data.m_value = *other.m_data.m_value.array;
21228 break;
21229 }
21230
21231 case value_t::string:
21232 {
21233 m_data.m_value = *other.m_data.m_value.string;
21234 break;
21235 }
21236
21237 case value_t::boolean:
21238 {
21239 m_data.m_value = other.m_data.m_value.boolean;
21240 break;
21241 }
21242
21243 case value_t::number_integer:
21244 {
21245 m_data.m_value = other.m_data.m_value.number_integer;
21246 break;
21247 }
21248
21249 case value_t::number_unsigned:
21250 {
21251 m_data.m_value = other.m_data.m_value.number_unsigned;
21252 break;
21253 }
21254
21255 case value_t::number_float:
21256 {
21257 m_data.m_value = other.m_data.m_value.number_float;
21258 break;
21259 }
21260
21261 case value_t::binary:
21262 {
21263 m_data.m_value = *other.m_data.m_value.binary;
21264 break;
21265 }
21266
21267 case value_t::null:
21268 case value_t::discarded:
21269 default:
21270 break;
21271 }
21272
21273 set_parents();
21274 assert_invariant();
21275 }
21276
21277 /// @brief move constructor
21278 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
21279 basic_json(basic_json&& other) noexcept
21280 : json_base_class_t(std::forward<json_base_class_t>(other)),
21281 m_data(std::move(other.m_data)) // cppcheck-suppress[accessForwarded] TODO check
21282 #if JSON_DIAGNOSTIC_POSITIONS
21283 , start_position(other.start_position) // cppcheck-suppress[accessForwarded] TODO check
21284 , end_position(other.end_position) // cppcheck-suppress[accessForwarded] TODO check
21285 #endif
21286 {
21287 // check that passed value is valid
21288 other.assert_invariant(false); // cppcheck-suppress[accessForwarded]
21289
21290 // invalidate payload
21291 other.m_data.m_type = value_t::null;
21292 other.m_data.m_value = {};
21293
21294 #if JSON_DIAGNOSTIC_POSITIONS
21295 other.start_position = std::string::npos;
21296 other.end_position = std::string::npos;
21297 #endif
21298
21299 set_parents();
21300 assert_invariant();
21301 }
21302
21303 /// @brief copy assignment
21304 /// @sa https://json.nlohmann.me/api/basic_json/operator=/
21305 basic_json& operator=(basic_json other) noexcept (
21306 std::is_nothrow_move_constructible<value_t>::value&&
21307 std::is_nothrow_move_assignable<value_t>::value&&
21308 std::is_nothrow_move_constructible<json_value>::value&&
21309 std::is_nothrow_move_assignable<json_value>::value&&
21310 std::is_nothrow_move_assignable<json_base_class_t>::value
21311 )
21312 {
21313 // check that passed value is valid
21314 other.assert_invariant();
21315
21316 using std::swap;
21317 swap(m_data.m_type, other.m_data.m_type);
21318 swap(m_data.m_value, other.m_data.m_value);
21319
21320 #if JSON_DIAGNOSTIC_POSITIONS
21321 swap(start_position, other.start_position);
21322 swap(end_position, other.end_position);
21323 #endif
21324
21325 json_base_class_t::operator=(std::move(other));
21326
21327 set_parents();
21328 assert_invariant();
21329 return *this;
21330 }
21331
21332 /// @brief destructor
21333 /// @sa https://json.nlohmann.me/api/basic_json/~basic_json/
21334 ~basic_json() noexcept
21335 {
21336 assert_invariant(false);
21337 }
21338
21339 /// @}
21340
21341 public:
21342 ///////////////////////
21343 // object inspection //
21344 ///////////////////////
21345
21346 /// @name object inspection
21347 /// Functions to inspect the type of a JSON value.
21348 /// @{
21349
21350 /// @brief serialization
21351 /// @sa https://json.nlohmann.me/api/basic_json/dump/
21352 string_t dump(const int indent = -1,
21353 const char indent_char = ' ',
21354 const bool ensure_ascii = false,
21355 const error_handler_t error_handler = error_handler_t::strict) const
21356 {
21357 string_t result;
21358 serializer s(detail::output_adapter<char, string_t>(result), indent_char, error_handler);
21359
21360 if (indent >= 0)
21361 {
21362 s.dump(*this, true, ensure_ascii, static_cast<unsigned int>(indent));
21363 }
21364 else
21365 {
21366 s.dump(*this, false, ensure_ascii, 0);
21367 }
21368
21369 return result;
21370 }
21371
21372 /// @brief return the type of the JSON value (explicit)
21373 /// @sa https://json.nlohmann.me/api/basic_json/type/
21374 constexpr value_t type() const noexcept
21375 {
21376 return m_data.m_type;
21377 }
21378
21379 /// @brief return whether type is primitive
21380 /// @sa https://json.nlohmann.me/api/basic_json/is_primitive/
21381 constexpr bool is_primitive() const noexcept
21382 {
21383 return is_null() || is_string() || is_boolean() || is_number() || is_binary();
21384 }
21385
21386 /// @brief return whether type is structured
21387 /// @sa https://json.nlohmann.me/api/basic_json/is_structured/
21388 constexpr bool is_structured() const noexcept
21389 {
21390 return is_array() || is_object();
21391 }
21392
21393 /// @brief return whether value is null
21394 /// @sa https://json.nlohmann.me/api/basic_json/is_null/
21395 constexpr bool is_null() const noexcept
21396 {
21397 return m_data.m_type == value_t::null;
21398 }
21399
21400 /// @brief return whether value is a boolean
21401 /// @sa https://json.nlohmann.me/api/basic_json/is_boolean/
21402 constexpr bool is_boolean() const noexcept
21403 {
21404 return m_data.m_type == value_t::boolean;
21405 }
21406
21407 /// @brief return whether value is a number
21408 /// @sa https://json.nlohmann.me/api/basic_json/is_number/
21409 constexpr bool is_number() const noexcept
21410 {
21411 return is_number_integer() || is_number_float();
21412 }
21413
21414 /// @brief return whether value is an integer number
21415 /// @sa https://json.nlohmann.me/api/basic_json/is_number_integer/
21416 constexpr bool is_number_integer() const noexcept
21417 {
21418 return m_data.m_type == value_t::number_integer || m_data.m_type == value_t::number_unsigned;
21419 }
21420
21421 /// @brief return whether value is an unsigned integer number
21422 /// @sa https://json.nlohmann.me/api/basic_json/is_number_unsigned/
21423 constexpr bool is_number_unsigned() const noexcept
21424 {
21425 return m_data.m_type == value_t::number_unsigned;
21426 }
21427
21428 /// @brief return whether value is a floating-point number
21429 /// @sa https://json.nlohmann.me/api/basic_json/is_number_float/
21430 constexpr bool is_number_float() const noexcept
21431 {
21432 return m_data.m_type == value_t::number_float;
21433 }
21434
21435 /// @brief return whether value is an object
21436 /// @sa https://json.nlohmann.me/api/basic_json/is_object/
21437 constexpr bool is_object() const noexcept
21438 {
21439 return m_data.m_type == value_t::object;
21440 }
21441
21442 /// @brief return whether value is an array
21443 /// @sa https://json.nlohmann.me/api/basic_json/is_array/
21444 constexpr bool is_array() const noexcept
21445 {
21446 return m_data.m_type == value_t::array;
21447 }
21448
21449 /// @brief return whether value is a string
21450 /// @sa https://json.nlohmann.me/api/basic_json/is_string/
21451 constexpr bool is_string() const noexcept
21452 {
21453 return m_data.m_type == value_t::string;
21454 }
21455
21456 /// @brief return whether value is a binary array
21457 /// @sa https://json.nlohmann.me/api/basic_json/is_binary/
21458 constexpr bool is_binary() const noexcept
21459 {
21460 return m_data.m_type == value_t::binary;
21461 }
21462
21463 /// @brief return whether value is discarded
21464 /// @sa https://json.nlohmann.me/api/basic_json/is_discarded/
21465 constexpr bool is_discarded() const noexcept
21466 {
21467 return m_data.m_type == value_t::discarded;
21468 }
21469
21470 /// @brief return the type of the JSON value (implicit)
21471 /// @sa https://json.nlohmann.me/api/basic_json/operator_value_t/
21472 constexpr operator value_t() const noexcept
21473 {
21474 return m_data.m_type;
21475 }
21476
21477 /// @}
21478
21479 private:
21480 //////////////////
21481 // value access //
21482 //////////////////
21483
21484 /// get a boolean (explicit)
21485 boolean_t get_impl(boolean_t* /*unused*/) const
21486 {
21487 if (JSON_HEDLEY_LIKELY(is_boolean()))
21488 {
21489 return m_data.m_value.boolean;
21490 }
21491
21492 JSON_THROW(type_error::create(302, detail::concat("type must be boolean, but is ", type_name()), this));
21493 }
21494
21495 /// get a pointer to the value (object)
21496 object_t* get_impl_ptr(object_t* /*unused*/) noexcept
21497 {
21498 return is_object() ? m_data.m_value.object : nullptr;
21499 }
21500
21501 /// get a pointer to the value (object)
21502 constexpr const object_t* get_impl_ptr(const object_t* /*unused*/) const noexcept
21503 {
21504 return is_object() ? m_data.m_value.object : nullptr;
21505 }
21506
21507 /// get a pointer to the value (array)
21508 array_t* get_impl_ptr(array_t* /*unused*/) noexcept
21509 {
21510 return is_array() ? m_data.m_value.array : nullptr;
21511 }
21512
21513 /// get a pointer to the value (array)
21514 constexpr const array_t* get_impl_ptr(const array_t* /*unused*/) const noexcept
21515 {
21516 return is_array() ? m_data.m_value.array : nullptr;
21517 }
21518
21519 /// get a pointer to the value (string)
21520 string_t* get_impl_ptr(string_t* /*unused*/) noexcept
21521 {
21522 return is_string() ? m_data.m_value.string : nullptr;
21523 }
21524
21525 /// get a pointer to the value (string)
21526 constexpr const string_t* get_impl_ptr(const string_t* /*unused*/) const noexcept
21527 {
21528 return is_string() ? m_data.m_value.string : nullptr;
21529 }
21530
21531 /// get a pointer to the value (boolean)
21532 boolean_t* get_impl_ptr(boolean_t* /*unused*/) noexcept
21533 {
21534 return is_boolean() ? &m_data.m_value.boolean : nullptr;
21535 }
21536
21537 /// get a pointer to the value (boolean)
21538 constexpr const boolean_t* get_impl_ptr(const boolean_t* /*unused*/) const noexcept
21539 {
21540 return is_boolean() ? &m_data.m_value.boolean : nullptr;
21541 }
21542
21543 /// get a pointer to the value (integer number)
21544 number_integer_t* get_impl_ptr(number_integer_t* /*unused*/) noexcept
21545 {
21546 return m_data.m_type == value_t::number_integer ? &m_data.m_value.number_integer : nullptr;
21547 }
21548
21549 /// get a pointer to the value (integer number)
21550 constexpr const number_integer_t* get_impl_ptr(const number_integer_t* /*unused*/) const noexcept
21551 {
21552 return m_data.m_type == value_t::number_integer ? &m_data.m_value.number_integer : nullptr;
21553 }
21554
21555 /// get a pointer to the value (unsigned number)
21556 number_unsigned_t* get_impl_ptr(number_unsigned_t* /*unused*/) noexcept
21557 {
21558 return is_number_unsigned() ? &m_data.m_value.number_unsigned : nullptr;
21559 }
21560
21561 /// get a pointer to the value (unsigned number)
21562 constexpr const number_unsigned_t* get_impl_ptr(const number_unsigned_t* /*unused*/) const noexcept
21563 {
21564 return is_number_unsigned() ? &m_data.m_value.number_unsigned : nullptr;
21565 }
21566
21567 /// get a pointer to the value (floating-point number)
21568 number_float_t* get_impl_ptr(number_float_t* /*unused*/) noexcept
21569 {
21570 return is_number_float() ? &m_data.m_value.number_float : nullptr;
21571 }
21572
21573 /// get a pointer to the value (floating-point number)
21574 constexpr const number_float_t* get_impl_ptr(const number_float_t* /*unused*/) const noexcept
21575 {
21576 return is_number_float() ? &m_data.m_value.number_float : nullptr;
21577 }
21578
21579 /// get a pointer to the value (binary)
21580 binary_t* get_impl_ptr(binary_t* /*unused*/) noexcept
21581 {
21582 return is_binary() ? m_data.m_value.binary : nullptr;
21583 }
21584
21585 /// get a pointer to the value (binary)
21586 constexpr const binary_t* get_impl_ptr(const binary_t* /*unused*/) const noexcept
21587 {
21588 return is_binary() ? m_data.m_value.binary : nullptr;
21589 }
21590
21591 /*!
21592 @brief helper function to implement get_ref()
21593
21594 This function helps to implement get_ref() without code duplication for
21595 const and non-const overloads
21596
21597 @tparam ThisType will be deduced as `basic_json` or `const basic_json`
21598
21599 @throw type_error.303 if ReferenceType does not match underlying value
21600 type of the current JSON
21601 */
21602 template<typename ReferenceType, typename ThisType>
21603 static ReferenceType get_ref_impl(ThisType& obj)
21604 {
21605 // delegate the call to get_ptr<>()
21606 auto* ptr = obj.template get_ptr<typename std::add_pointer<ReferenceType>::type>();
21607
21608 if (JSON_HEDLEY_LIKELY(ptr != nullptr))
21609 {
21610 return *ptr;
21611 }
21612
21613 JSON_THROW(type_error::create(303, detail::concat("incompatible ReferenceType for get_ref, actual type is ", obj.type_name()), &obj));
21614 }
21615
21616 public:
21617 /// @name value access
21618 /// Direct access to the stored value of a JSON value.
21619 /// @{
21620
21621 /// @brief get a pointer value (implicit)
21622 /// @sa https://json.nlohmann.me/api/basic_json/get_ptr/
21623 template<typename PointerType, typename std::enable_if<
21624 std::is_pointer<PointerType>::value, int>::type = 0>
21625 auto get_ptr() noexcept -> decltype(std::declval<basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
21626 {
21627 // delegate the call to get_impl_ptr<>()
21628 return get_impl_ptr(static_cast<PointerType>(nullptr));
21629 }
21630
21631 /// @brief get a pointer value (implicit)
21632 /// @sa https://json.nlohmann.me/api/basic_json/get_ptr/
21633 template < typename PointerType, typename std::enable_if <
21634 std::is_pointer<PointerType>::value&&
21635 std::is_const<typename std::remove_pointer<PointerType>::type>::value, int >::type = 0 >
21636 constexpr auto get_ptr() const noexcept -> decltype(std::declval<const basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
21637 {
21638 // delegate the call to get_impl_ptr<>() const
21639 return get_impl_ptr(static_cast<PointerType>(nullptr));
21640 }
21641
21642 private:
21643 /*!
21644 @brief get a value (explicit)
21645
21646 Explicit type conversion between the JSON value and a compatible value
21647 which is [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
21648 and [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
21649 The value is converted by calling the @ref json_serializer<ValueType>
21650 `from_json()` method.
21651
21652 The function is equivalent to executing
21653 @code {.cpp}
21654 ValueType ret;
21655 JSONSerializer<ValueType>::from_json(*this, ret);
21656 return ret;
21657 @endcode
21658
21659 This overloads is chosen if:
21660 - @a ValueType is not @ref basic_json,
21661 - @ref json_serializer<ValueType> has a `from_json()` method of the form
21662 `void from_json(const basic_json&, ValueType&)`, and
21663 - @ref json_serializer<ValueType> does not have a `from_json()` method of
21664 the form `ValueType from_json(const basic_json&)`
21665
21666 @tparam ValueType the returned value type
21667
21668 @return copy of the JSON value, converted to @a ValueType
21669
21670 @throw what @ref json_serializer<ValueType> `from_json()` method throws
21671
21672 @liveexample{The example below shows several conversions from JSON values
21673 to other types. There a few things to note: (1) Floating-point numbers can
21674 be converted to integers\, (2) A JSON array can be converted to a standard
21675 `std::vector<short>`\, (3) A JSON object can be converted to C++
21676 associative containers such as `std::unordered_map<std::string\,
21677 json>`.,get__ValueType_const}
21678
21679 @since version 2.1.0
21680 */
21681 template < typename ValueType,
21682 detail::enable_if_t <
21683 detail::is_default_constructible<ValueType>::value&&
21684 detail::has_from_json<basic_json_t, ValueType>::value,
21685 int > = 0 >
21686 ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept(
21687 JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), std::declval<ValueType&>())))
21688 {
21689 auto ret = ValueType();
21690 JSONSerializer<ValueType>::from_json(*this, ret);
21691 return ret;
21692 }
21693
21694 /*!
21695 @brief get a value (explicit); special case
21696
21697 Explicit type conversion between the JSON value and a compatible value
21698 which is **not** [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
21699 and **not** [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
21700 The value is converted by calling the @ref json_serializer<ValueType>
21701 `from_json()` method.
21702
21703 The function is equivalent to executing
21704 @code {.cpp}
21705 return JSONSerializer<ValueType>::from_json(*this);
21706 @endcode
21707
21708 This overloads is chosen if:
21709 - @a ValueType is not @ref basic_json and
21710 - @ref json_serializer<ValueType> has a `from_json()` method of the form
21711 `ValueType from_json(const basic_json&)`
21712
21713 @note If @ref json_serializer<ValueType> has both overloads of
21714 `from_json()`, this one is chosen.
21715
21716 @tparam ValueType the returned value type
21717
21718 @return copy of the JSON value, converted to @a ValueType
21719
21720 @throw what @ref json_serializer<ValueType> `from_json()` method throws
21721
21722 @since version 2.1.0
21723 */
21724 template < typename ValueType,
21725 detail::enable_if_t <
21726 detail::has_non_default_from_json<basic_json_t, ValueType>::value,
21727 int > = 0 >
21728 ValueType get_impl(detail::priority_tag<1> /*unused*/) const noexcept(noexcept(
21729 JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>())))
21730 {
21731 return JSONSerializer<ValueType>::from_json(*this);
21732 }
21733
21734 /*!
21735 @brief get special-case overload
21736
21737 This overloads converts the current @ref basic_json in a different
21738 @ref basic_json type
21739
21740 @tparam BasicJsonType == @ref basic_json
21741
21742 @return a copy of *this, converted into @a BasicJsonType
21743
21744 @complexity Depending on the implementation of the called `from_json()`
21745 method.
21746
21747 @since version 3.2.0
21748 */
21749 template < typename BasicJsonType,
21750 detail::enable_if_t <
21751 detail::is_basic_json<BasicJsonType>::value,
21752 int > = 0 >
21753 BasicJsonType get_impl(detail::priority_tag<2> /*unused*/) const
21754 {
21755 return *this;
21756 }
21757
21758 /*!
21759 @brief get special-case overload
21760
21761 This overloads avoids a lot of template boilerplate, it can be seen as the
21762 identity method
21763
21764 @tparam BasicJsonType == @ref basic_json
21765
21766 @return a copy of *this
21767
21768 @complexity Constant.
21769
21770 @since version 2.1.0
21771 */
21772 template<typename BasicJsonType,
21773 detail::enable_if_t<
21774 std::is_same<BasicJsonType, basic_json_t>::value,
21775 int> = 0>
21776 basic_json get_impl(detail::priority_tag<3> /*unused*/) const
21777 {
21778 return *this;
21779 }
21780
21781 /*!
21782 @brief get a pointer value (explicit)
21783 @copydoc get()
21784 */
21785 template<typename PointerType,
21786 detail::enable_if_t<
21787 std::is_pointer<PointerType>::value,
21788 int> = 0>
21789 constexpr auto get_impl(detail::priority_tag<4> /*unused*/) const noexcept
21790 -> decltype(std::declval<const basic_json_t&>().template get_ptr<PointerType>())
21791 {
21792 // delegate the call to get_ptr
21793 return get_ptr<PointerType>();
21794 }
21795
21796 public:
21797 /*!
21798 @brief get a (pointer) value (explicit)
21799
21800 Performs explicit type conversion between the JSON value and a compatible value if required.
21801
21802 - If the requested type is a pointer to the internally stored JSON value that pointer is returned.
21803 No copies are made.
21804
21805 - If the requested type is the current @ref basic_json, or a different @ref basic_json convertible
21806 from the current @ref basic_json.
21807
21808 - Otherwise the value is converted by calling the @ref json_serializer<ValueType> `from_json()`
21809 method.
21810
21811 @tparam ValueTypeCV the provided value type
21812 @tparam ValueType the returned value type
21813
21814 @return copy of the JSON value, converted to @tparam ValueType if necessary
21815
21816 @throw what @ref json_serializer<ValueType> `from_json()` method throws if conversion is required
21817
21818 @since version 2.1.0
21819 */
21820 template < typename ValueTypeCV, typename ValueType = detail::uncvref_t<ValueTypeCV>>
21821 #if defined(JSON_HAS_CPP_14)
21822 constexpr
21823 #endif
21824 auto get() const noexcept(
21825 noexcept(std::declval<const basic_json_t&>().template get_impl<ValueType>(detail::priority_tag<4> {})))
21826 -> decltype(std::declval<const basic_json_t&>().template get_impl<ValueType>(detail::priority_tag<4> {}))
21827 {
21828 // we cannot static_assert on ValueTypeCV being non-const, because
21829 // there is support for get<const basic_json_t>(), which is why we
21830 // still need the uncvref
21831 static_assert(!std::is_reference<ValueTypeCV>::value,
21832 "get() cannot be used with reference types, you might want to use get_ref()");
21833 return get_impl<ValueType>(detail::priority_tag<4> {});
21834 }
21835
21836 /*!
21837 @brief get a pointer value (explicit)
21838
21839 Explicit pointer access to the internally stored JSON value. No copies are
21840 made.
21841
21842 @warning The pointer becomes invalid if the underlying JSON object
21843 changes.
21844
21845 @tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
21846 object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
21847 @ref number_unsigned_t, or @ref number_float_t.
21848
21849 @return pointer to the internally stored JSON value if the requested
21850 pointer type @a PointerType fits to the JSON value; `nullptr` otherwise
21851
21852 @complexity Constant.
21853
21854 @liveexample{The example below shows how pointers to internal values of a
21855 JSON value can be requested. Note that no type conversions are made and a
21856 `nullptr` is returned if the value and the requested pointer type does not
21857 match.,get__PointerType}
21858
21859 @sa see @ref get_ptr() for explicit pointer-member access
21860
21861 @since version 1.0.0
21862 */
21863 template<typename PointerType, typename std::enable_if<
21864 std::is_pointer<PointerType>::value, int>::type = 0>
21865 auto get() noexcept -> decltype(std::declval<basic_json_t&>().template get_ptr<PointerType>())
21866 {
21867 // delegate the call to get_ptr
21868 return get_ptr<PointerType>();
21869 }
21870
21871 /// @brief get a value (explicit)
21872 /// @sa https://json.nlohmann.me/api/basic_json/get_to/
21873 template < typename ValueType,
21874 detail::enable_if_t <
21875 !detail::is_basic_json<ValueType>::value&&
21876 detail::has_from_json<basic_json_t, ValueType>::value,
21877 int > = 0 >
21878 ValueType & get_to(ValueType& v) const noexcept(noexcept(
21879 JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), v)))
21880 {
21881 JSONSerializer<ValueType>::from_json(*this, v);
21882 return v;
21883 }
21884
21885 // specialization to allow calling get_to with a basic_json value
21886 // see https://github.com/nlohmann/json/issues/2175
21887 template<typename ValueType,
21888 detail::enable_if_t <
21889 detail::is_basic_json<ValueType>::value,
21890 int> = 0>
21891 ValueType & get_to(ValueType& v) const
21892 {
21893 v = *this;
21894 return v;
21895 }
21896
21897 template <
21898 typename T, std::size_t N,
21899 typename Array = T (&)[N], // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
21900 detail::enable_if_t <
21901 detail::has_from_json<basic_json_t, Array>::value, int > = 0 >
21902 Array get_to(T (&v)[N]) const // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
21903 noexcept(noexcept(JSONSerializer<Array>::from_json(
21904 std::declval<const basic_json_t&>(), v)))
21905 {
21906 JSONSerializer<Array>::from_json(*this, v);
21907 return v;
21908 }
21909
21910 /// @brief get a reference value (implicit)
21911 /// @sa https://json.nlohmann.me/api/basic_json/get_ref/
21912 template<typename ReferenceType, typename std::enable_if<
21913 std::is_reference<ReferenceType>::value, int>::type = 0>
21914 ReferenceType get_ref()
21915 {
21916 // delegate call to get_ref_impl
21917 return get_ref_impl<ReferenceType>(*this);
21918 }
21919
21920 /// @brief get a reference value (implicit)
21921 /// @sa https://json.nlohmann.me/api/basic_json/get_ref/
21922 template < typename ReferenceType, typename std::enable_if <
21923 std::is_reference<ReferenceType>::value&&
21924 std::is_const<typename std::remove_reference<ReferenceType>::type>::value, int >::type = 0 >
21925 ReferenceType get_ref() const
21926 {
21927 // delegate call to get_ref_impl
21928 return get_ref_impl<ReferenceType>(*this);
21929 }
21930
21931 /*!
21932 @brief get a value (implicit)
21933
21934 Implicit type conversion between the JSON value and a compatible value.
21935 The call is realized by calling @ref get() const.
21936
21937 @tparam ValueType non-pointer type compatible to the JSON value, for
21938 instance `int` for JSON integer numbers, `bool` for JSON booleans, or
21939 `std::vector` types for JSON arrays. The character type of @ref string_t
21940 as well as an initializer list of this type is excluded to avoid
21941 ambiguities as these types implicitly convert to `std::string`.
21942
21943 @return copy of the JSON value, converted to type @a ValueType
21944
21945 @throw type_error.302 in case passed type @a ValueType is incompatible
21946 to the JSON value type (e.g., the JSON value is of type boolean, but a
21947 string is requested); see example below
21948
21949 @complexity Linear in the size of the JSON value.
21950
21951 @liveexample{The example below shows several conversions from JSON values
21952 to other types. There a few things to note: (1) Floating-point numbers can
21953 be converted to integers\, (2) A JSON array can be converted to a standard
21954 `std::vector<short>`\, (3) A JSON object can be converted to C++
21955 associative containers such as `std::unordered_map<std::string\,
21956 json>`.,operator__ValueType}
21957
21958 @since version 1.0.0
21959 */
21960 template < typename ValueType, typename std::enable_if <
21961 detail::conjunction <
21962 detail::negation<std::is_pointer<ValueType>>,
21963 detail::negation<std::is_same<ValueType, std::nullptr_t>>,
21964 detail::negation<std::is_same<ValueType, detail::json_ref<basic_json>>>,
21965 detail::negation<std::is_same<ValueType, typename string_t::value_type>>,
21966 detail::negation<detail::is_basic_json<ValueType>>,
21967 detail::negation<std::is_same<ValueType, std::initializer_list<typename string_t::value_type>>>,
21968 #if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER <= 1914))
21969 detail::negation<std::is_same<ValueType, std::string_view>>,
21970 #endif
21971 #if defined(JSON_HAS_CPP_17) && JSON_HAS_STATIC_RTTI
21972 detail::negation<std::is_same<ValueType, std::any>>,
21973 #endif
21974 detail::is_detected_lazy<detail::get_template_function, const basic_json_t&, ValueType>
21975 >::value, int >::type = 0 >
21976 JSON_EXPLICIT operator ValueType() const
21977 {
21978 // delegate the call to get<>() const
21979 return get<ValueType>();
21980 }
21981
21982 /// @brief get a binary value
21983 /// @sa https://json.nlohmann.me/api/basic_json/get_binary/
21984 binary_t& get_binary()
21985 {
21986 if (!is_binary())
21987 {
21988 JSON_THROW(type_error::create(302, detail::concat("type must be binary, but is ", type_name()), this));
21989 }
21990
21991 return *get_ptr<binary_t*>();
21992 }
21993
21994 /// @brief get a binary value
21995 /// @sa https://json.nlohmann.me/api/basic_json/get_binary/
21996 const binary_t& get_binary() const
21997 {
21998 if (!is_binary())
21999 {
22000 JSON_THROW(type_error::create(302, detail::concat("type must be binary, but is ", type_name()), this));
22001 }
22002
22003 return *get_ptr<const binary_t*>();
22004 }
22005
22006 /// @}
22007
22008 ////////////////////
22009 // element access //
22010 ////////////////////
22011
22012 /// @name element access
22013 /// Access to the JSON value.
22014 /// @{
22015
22016 /// @brief access specified array element with bounds checking
22017 /// @sa https://json.nlohmann.me/api/basic_json/at/
22018 reference at(size_type idx)
22019 {
22020 // at only works for arrays
22021 if (JSON_HEDLEY_LIKELY(is_array()))
22022 {
22023 JSON_TRY
22024 {
22025 return set_parent(m_data.m_value.array->at(idx));
22026 }
22027 JSON_CATCH (std::out_of_range&)
22028 {
22029 // create better exception explanation
22030 JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
22031 } // cppcheck-suppress[missingReturn]
22032 }
22033 else
22034 {
22035 JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
22036 }
22037 }
22038
22039 /// @brief access specified array element with bounds checking
22040 /// @sa https://json.nlohmann.me/api/basic_json/at/
22041 const_reference at(size_type idx) const
22042 {
22043 // at only works for arrays
22044 if (JSON_HEDLEY_LIKELY(is_array()))
22045 {
22046 JSON_TRY
22047 {
22048 return m_data.m_value.array->at(idx);
22049 }
22050 JSON_CATCH (std::out_of_range&)
22051 {
22052 // create better exception explanation
22053 JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
22054 } // cppcheck-suppress[missingReturn]
22055 }
22056 else
22057 {
22058 JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
22059 }
22060 }
22061
22062 /// @brief access specified object element with bounds checking
22063 /// @sa https://json.nlohmann.me/api/basic_json/at/
22064 reference at(const typename object_t::key_type& key)
22065 {
22066 // at only works for objects
22067 if (JSON_HEDLEY_UNLIKELY(!is_object()))
22068 {
22069 JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
22070 }
22071
22072 auto it = m_data.m_value.object->find(key);
22073 if (it == m_data.m_value.object->end())
22074 {
22075 JSON_THROW(out_of_range::create(403, detail::concat("key '", key, "' not found"), this));
22076 }
22077 return set_parent(it->second);
22078 }
22079
22080 /// @brief access specified object element with bounds checking
22081 /// @sa https://json.nlohmann.me/api/basic_json/at/
22082 template<class KeyType, detail::enable_if_t<
22083 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
22084 reference at(KeyType && key)
22085 {
22086 // at only works for objects
22087 if (JSON_HEDLEY_UNLIKELY(!is_object()))
22088 {
22089 JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
22090 }
22091
22092 auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
22093 if (it == m_data.m_value.object->end())
22094 {
22095 JSON_THROW(out_of_range::create(403, detail::concat("key '", string_t(std::forward<KeyType>(key)), "' not found"), this));
22096 }
22097 return set_parent(it->second);
22098 }
22099
22100 /// @brief access specified object element with bounds checking
22101 /// @sa https://json.nlohmann.me/api/basic_json/at/
22102 const_reference at(const typename object_t::key_type& key) const
22103 {
22104 // at only works for objects
22105 if (JSON_HEDLEY_UNLIKELY(!is_object()))
22106 {
22107 JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
22108 }
22109
22110 auto it = m_data.m_value.object->find(key);
22111 if (it == m_data.m_value.object->end())
22112 {
22113 JSON_THROW(out_of_range::create(403, detail::concat("key '", key, "' not found"), this));
22114 }
22115 return it->second;
22116 }
22117
22118 /// @brief access specified object element with bounds checking
22119 /// @sa https://json.nlohmann.me/api/basic_json/at/
22120 template<class KeyType, detail::enable_if_t<
22121 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
22122 const_reference at(KeyType && key) const
22123 {
22124 // at only works for objects
22125 if (JSON_HEDLEY_UNLIKELY(!is_object()))
22126 {
22127 JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
22128 }
22129
22130 auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
22131 if (it == m_data.m_value.object->end())
22132 {
22133 JSON_THROW(out_of_range::create(403, detail::concat("key '", string_t(std::forward<KeyType>(key)), "' not found"), this));
22134 }
22135 return it->second;
22136 }
22137
22138 /// @brief access specified array element
22139 /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
22140 reference operator[](size_type idx)
22141 {
22142 // implicitly convert null value to an empty array
22143 if (is_null())
22144 {
22145 m_data.m_type = value_t::array;
22146 m_data.m_value.array = create<array_t>();
22147 assert_invariant();
22148 }
22149
22150 // operator[] only works for arrays
22151 if (JSON_HEDLEY_LIKELY(is_array()))
22152 {
22153 // fill up array with null values if given idx is outside range
22154 if (idx >= m_data.m_value.array->size())
22155 {
22156 #if JSON_DIAGNOSTICS
22157 // remember array size & capacity before resizing
22158 const auto old_size = m_data.m_value.array->size();
22159 const auto old_capacity = m_data.m_value.array->capacity();
22160 #endif
22161 m_data.m_value.array->resize(idx + 1);
22162
22163 #if JSON_DIAGNOSTICS
22164 if (JSON_HEDLEY_UNLIKELY(m_data.m_value.array->capacity() != old_capacity))
22165 {
22166 // capacity has changed: update all parents
22167 set_parents();
22168 }
22169 else
22170 {
22171 // set parent for values added above
22172 set_parents(begin() + static_cast<typename iterator::difference_type>(old_size), static_cast<typename iterator::difference_type>(idx + 1 - old_size));
22173 }
22174 #endif
22175 assert_invariant();
22176 }
22177
22178 return m_data.m_value.array->operator[](idx);
22179 }
22180
22181 JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a numeric argument with ", type_name()), this));
22182 }
22183
22184 /// @brief access specified array element
22185 /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
22186 const_reference operator[](size_type idx) const
22187 {
22188 // const operator[] only works for arrays
22189 if (JSON_HEDLEY_LIKELY(is_array()))
22190 {
22191 return m_data.m_value.array->operator[](idx);
22192 }
22193
22194 JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a numeric argument with ", type_name()), this));
22195 }
22196
22197 /// @brief access specified object element
22198 /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
22199 reference operator[](typename object_t::key_type key) // NOLINT(performance-unnecessary-value-param)
22200 {
22201 // implicitly convert null value to an empty object
22202 if (is_null())
22203 {
22204 m_data.m_type = value_t::object;
22205 m_data.m_value.object = create<object_t>();
22206 assert_invariant();
22207 }
22208
22209 // operator[] only works for objects
22210 if (JSON_HEDLEY_LIKELY(is_object()))
22211 {
22212 auto result = m_data.m_value.object->emplace(std::move(key), nullptr);
22213 return set_parent(result.first->second);
22214 }
22215
22216 JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
22217 }
22218
22219 /// @brief access specified object element
22220 /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
22221 const_reference operator[](const typename object_t::key_type& key) const
22222 {
22223 // const operator[] only works for objects
22224 if (JSON_HEDLEY_LIKELY(is_object()))
22225 {
22226 auto it = m_data.m_value.object->find(key);
22227 JSON_ASSERT(it != m_data.m_value.object->end());
22228 return it->second;
22229 }
22230
22231 JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
22232 }
22233
22234 // these two functions resolve a (const) char * ambiguity affecting Clang and MSVC
22235 // (they seemingly cannot be constrained to resolve the ambiguity)
22236 template<typename T>
22237 reference operator[](T* key)
22238 {
22239 return operator[](typename object_t::key_type(key));
22240 }
22241
22242 template<typename T>
22243 const_reference operator[](T* key) const
22244 {
22245 return operator[](typename object_t::key_type(key));
22246 }
22247
22248 /// @brief access specified object element
22249 /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
22250 template<class KeyType, detail::enable_if_t<
22251 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int > = 0 >
22252 reference operator[](KeyType && key)
22253 {
22254 // implicitly convert null value to an empty object
22255 if (is_null())
22256 {
22257 m_data.m_type = value_t::object;
22258 m_data.m_value.object = create<object_t>();
22259 assert_invariant();
22260 }
22261
22262 // operator[] only works for objects
22263 if (JSON_HEDLEY_LIKELY(is_object()))
22264 {
22265 auto result = m_data.m_value.object->emplace(std::forward<KeyType>(key), nullptr);
22266 return set_parent(result.first->second);
22267 }
22268
22269 JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
22270 }
22271
22272 /// @brief access specified object element
22273 /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
22274 template<class KeyType, detail::enable_if_t<
22275 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int > = 0 >
22276 const_reference operator[](KeyType && key) const
22277 {
22278 // const operator[] only works for objects
22279 if (JSON_HEDLEY_LIKELY(is_object()))
22280 {
22281 auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
22282 JSON_ASSERT(it != m_data.m_value.object->end());
22283 return it->second;
22284 }
22285
22286 JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
22287 }
22288
22289 private:
22290 template<typename KeyType>
22291 using is_comparable_with_object_key = detail::is_comparable <
22292 object_comparator_t, const typename object_t::key_type&, KeyType >;
22293
22294 template<typename ValueType>
22295 using value_return_type = std::conditional <
22296 detail::is_c_string_uncvref<ValueType>::value,
22297 string_t, typename std::decay<ValueType>::type >;
22298
22299 public:
22300 /// @brief access specified object element with default value
22301 /// @sa https://json.nlohmann.me/api/basic_json/value/
22302 template < class ValueType, detail::enable_if_t <
22303 !detail::is_transparent<object_comparator_t>::value
22304 && detail::is_getable<basic_json_t, ValueType>::value
22305 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22306 ValueType value(const typename object_t::key_type& key, const ValueType& default_value) const
22307 {
22308 // value only works for objects
22309 if (JSON_HEDLEY_LIKELY(is_object()))
22310 {
22311 // if key is found, return value and given default value otherwise
22312 const auto it = find(key);
22313 if (it != end())
22314 {
22315 return it->template get<ValueType>();
22316 }
22317
22318 return default_value;
22319 }
22320
22321 JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
22322 }
22323
22324 /// @brief access specified object element with default value
22325 /// @sa https://json.nlohmann.me/api/basic_json/value/
22326 template < class ValueType, class ReturnType = typename value_return_type<ValueType>::type,
22327 detail::enable_if_t <
22328 !detail::is_transparent<object_comparator_t>::value
22329 && detail::is_getable<basic_json_t, ReturnType>::value
22330 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22331 ReturnType value(const typename object_t::key_type& key, ValueType && default_value) const
22332 {
22333 // value only works for objects
22334 if (JSON_HEDLEY_LIKELY(is_object()))
22335 {
22336 // if key is found, return value and given default value otherwise
22337 const auto it = find(key);
22338 if (it != end())
22339 {
22340 return it->template get<ReturnType>();
22341 }
22342
22343 return std::forward<ValueType>(default_value);
22344 }
22345
22346 JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
22347 }
22348
22349 /// @brief access specified object element with default value
22350 /// @sa https://json.nlohmann.me/api/basic_json/value/
22351 template < class ValueType, class KeyType, detail::enable_if_t <
22352 detail::is_transparent<object_comparator_t>::value
22353 && !detail::is_json_pointer<KeyType>::value
22354 && is_comparable_with_object_key<KeyType>::value
22355 && detail::is_getable<basic_json_t, ValueType>::value
22356 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22357 ValueType value(KeyType && key, const ValueType& default_value) const
22358 {
22359 // value only works for objects
22360 if (JSON_HEDLEY_LIKELY(is_object()))
22361 {
22362 // if key is found, return value and given default value otherwise
22363 const auto it = find(std::forward<KeyType>(key));
22364 if (it != end())
22365 {
22366 return it->template get<ValueType>();
22367 }
22368
22369 return default_value;
22370 }
22371
22372 JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
22373 }
22374
22375 /// @brief access specified object element via JSON Pointer with default value
22376 /// @sa https://json.nlohmann.me/api/basic_json/value/
22377 template < class ValueType, class KeyType, class ReturnType = typename value_return_type<ValueType>::type,
22378 detail::enable_if_t <
22379 detail::is_transparent<object_comparator_t>::value
22380 && !detail::is_json_pointer<KeyType>::value
22381 && is_comparable_with_object_key<KeyType>::value
22382 && detail::is_getable<basic_json_t, ReturnType>::value
22383 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22384 ReturnType value(KeyType && key, ValueType && default_value) const
22385 {
22386 // value only works for objects
22387 if (JSON_HEDLEY_LIKELY(is_object()))
22388 {
22389 // if key is found, return value and given default value otherwise
22390 const auto it = find(std::forward<KeyType>(key));
22391 if (it != end())
22392 {
22393 return it->template get<ReturnType>();
22394 }
22395
22396 return std::forward<ValueType>(default_value);
22397 }
22398
22399 JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
22400 }
22401
22402 /// @brief access specified object element via JSON Pointer with default value
22403 /// @sa https://json.nlohmann.me/api/basic_json/value/
22404 template < class ValueType, detail::enable_if_t <
22405 detail::is_getable<basic_json_t, ValueType>::value
22406 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22407 ValueType value(const json_pointer& ptr, const ValueType& default_value) const
22408 {
22409 // value only works for objects
22410 if (JSON_HEDLEY_LIKELY(is_object()))
22411 {
22412 // if pointer resolves a value, return it or use default value
22413 JSON_TRY
22414 {
22415 return ptr.get_checked(this).template get<ValueType>();
22416 }
22417 JSON_INTERNAL_CATCH (out_of_range&)
22418 {
22419 return default_value;
22420 }
22421 }
22422
22423 JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
22424 }
22425
22426 /// @brief access specified object element via JSON Pointer with default value
22427 /// @sa https://json.nlohmann.me/api/basic_json/value/
22428 template < class ValueType, class ReturnType = typename value_return_type<ValueType>::type,
22429 detail::enable_if_t <
22430 detail::is_getable<basic_json_t, ReturnType>::value
22431 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22432 ReturnType value(const json_pointer& ptr, ValueType && default_value) const
22433 {
22434 // value only works for objects
22435 if (JSON_HEDLEY_LIKELY(is_object()))
22436 {
22437 // if pointer resolves a value, return it or use default value
22438 JSON_TRY
22439 {
22440 return ptr.get_checked(this).template get<ReturnType>();
22441 }
22442 JSON_INTERNAL_CATCH (out_of_range&)
22443 {
22444 return std::forward<ValueType>(default_value);
22445 }
22446 }
22447
22448 JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
22449 }
22450
22451 template < class ValueType, class BasicJsonType, detail::enable_if_t <
22452 detail::is_basic_json<BasicJsonType>::value
22453 && detail::is_getable<basic_json_t, ValueType>::value
22454 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22455 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
22456 ValueType value(const ::nlohmann::json_pointer<BasicJsonType>& ptr, const ValueType& default_value) const
22457 {
22458 return value(ptr.convert(), default_value);
22459 }
22460
22461 template < class ValueType, class BasicJsonType, class ReturnType = typename value_return_type<ValueType>::type,
22462 detail::enable_if_t <
22463 detail::is_basic_json<BasicJsonType>::value
22464 && detail::is_getable<basic_json_t, ReturnType>::value
22465 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22466 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
22467 ReturnType value(const ::nlohmann::json_pointer<BasicJsonType>& ptr, ValueType && default_value) const
22468 {
22469 return value(ptr.convert(), std::forward<ValueType>(default_value));
22470 }
22471
22472 /// @brief access the first element
22473 /// @sa https://json.nlohmann.me/api/basic_json/front/
22474 reference front()
22475 {
22476 return *begin();
22477 }
22478
22479 /// @brief access the first element
22480 /// @sa https://json.nlohmann.me/api/basic_json/front/
22481 const_reference front() const
22482 {
22483 return *cbegin();
22484 }
22485
22486 /// @brief access the last element
22487 /// @sa https://json.nlohmann.me/api/basic_json/back/
22488 reference back()
22489 {
22490 auto tmp = end();
22491 --tmp;
22492 return *tmp;
22493 }
22494
22495 /// @brief access the last element
22496 /// @sa https://json.nlohmann.me/api/basic_json/back/
22497 const_reference back() const
22498 {
22499 auto tmp = cend();
22500 --tmp;
22501 return *tmp;
22502 }
22503
22504 /// @brief remove element given an iterator
22505 /// @sa https://json.nlohmann.me/api/basic_json/erase/
22506 template < class IteratorType, detail::enable_if_t <
22507 std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
22508 std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int > = 0 >
22509 IteratorType erase(IteratorType pos) // NOLINT(performance-unnecessary-value-param)
22510 {
22511 // make sure iterator fits the current value
22512 if (JSON_HEDLEY_UNLIKELY(this != pos.m_object))
22513 {
22514 JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
22515 }
22516
22517 IteratorType result = end();
22518
22519 switch (m_data.m_type)
22520 {
22521 case value_t::boolean:
22522 case value_t::number_float:
22523 case value_t::number_integer:
22524 case value_t::number_unsigned:
22525 case value_t::string:
22526 case value_t::binary:
22527 {
22528 if (JSON_HEDLEY_UNLIKELY(!pos.m_it.primitive_iterator.is_begin()))
22529 {
22530 JSON_THROW(invalid_iterator::create(205, "iterator out of range", this));
22531 }
22532
22533 if (is_string())
22534 {
22535 AllocatorType<string_t> alloc;
22536 std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.string);
22537 std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.string, 1);
22538 m_data.m_value.string = nullptr;
22539 }
22540 else if (is_binary())
22541 {
22542 AllocatorType<binary_t> alloc;
22543 std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.binary);
22544 std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.binary, 1);
22545 m_data.m_value.binary = nullptr;
22546 }
22547
22548 m_data.m_type = value_t::null;
22549 assert_invariant();
22550 break;
22551 }
22552
22553 case value_t::object:
22554 {
22555 result.m_it.object_iterator = m_data.m_value.object->erase(pos.m_it.object_iterator);
22556 break;
22557 }
22558
22559 case value_t::array:
22560 {
22561 result.m_it.array_iterator = m_data.m_value.array->erase(pos.m_it.array_iterator);
22562 break;
22563 }
22564
22565 case value_t::null:
22566 case value_t::discarded:
22567 default:
22568 JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
22569 }
22570
22571 return result;
22572 }
22573
22574 /// @brief remove elements given an iterator range
22575 /// @sa https://json.nlohmann.me/api/basic_json/erase/
22576 template < class IteratorType, detail::enable_if_t <
22577 std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
22578 std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int > = 0 >
22579 IteratorType erase(IteratorType first, IteratorType last) // NOLINT(performance-unnecessary-value-param)
22580 {
22581 // make sure iterator fits the current value
22582 if (JSON_HEDLEY_UNLIKELY(this != first.m_object || this != last.m_object))
22583 {
22584 JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value", this));
22585 }
22586
22587 IteratorType result = end();
22588
22589 switch (m_data.m_type)
22590 {
22591 case value_t::boolean:
22592 case value_t::number_float:
22593 case value_t::number_integer:
22594 case value_t::number_unsigned:
22595 case value_t::string:
22596 case value_t::binary:
22597 {
22598 if (JSON_HEDLEY_LIKELY(!first.m_it.primitive_iterator.is_begin()
22599 || !last.m_it.primitive_iterator.is_end()))
22600 {
22601 JSON_THROW(invalid_iterator::create(204, "iterators out of range", this));
22602 }
22603
22604 if (is_string())
22605 {
22606 AllocatorType<string_t> alloc;
22607 std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.string);
22608 std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.string, 1);
22609 m_data.m_value.string = nullptr;
22610 }
22611 else if (is_binary())
22612 {
22613 AllocatorType<binary_t> alloc;
22614 std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.binary);
22615 std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.binary, 1);
22616 m_data.m_value.binary = nullptr;
22617 }
22618
22619 m_data.m_type = value_t::null;
22620 assert_invariant();
22621 break;
22622 }
22623
22624 case value_t::object:
22625 {
22626 result.m_it.object_iterator = m_data.m_value.object->erase(first.m_it.object_iterator,
22627 last.m_it.object_iterator);
22628 break;
22629 }
22630
22631 case value_t::array:
22632 {
22633 result.m_it.array_iterator = m_data.m_value.array->erase(first.m_it.array_iterator,
22634 last.m_it.array_iterator);
22635 break;
22636 }
22637
22638 case value_t::null:
22639 case value_t::discarded:
22640 default:
22641 JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
22642 }
22643
22644 return result;
22645 }
22646
22647 private:
22648 template < typename KeyType, detail::enable_if_t <
22649 detail::has_erase_with_key_type<basic_json_t, KeyType>::value, int > = 0 >
22650 size_type erase_internal(KeyType && key)
22651 {
22652 // this erase only works for objects
22653 if (JSON_HEDLEY_UNLIKELY(!is_object()))
22654 {
22655 JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
22656 }
22657
22658 return m_data.m_value.object->erase(std::forward<KeyType>(key));
22659 }
22660
22661 template < typename KeyType, detail::enable_if_t <
22662 !detail::has_erase_with_key_type<basic_json_t, KeyType>::value, int > = 0 >
22663 size_type erase_internal(KeyType && key)
22664 {
22665 // this erase only works for objects
22666 if (JSON_HEDLEY_UNLIKELY(!is_object()))
22667 {
22668 JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
22669 }
22670
22671 const auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
22672 if (it != m_data.m_value.object->end())
22673 {
22674 m_data.m_value.object->erase(it);
22675 return 1;
22676 }
22677 return 0;
22678 }
22679
22680 public:
22681
22682 /// @brief remove element from a JSON object given a key
22683 /// @sa https://json.nlohmann.me/api/basic_json/erase/
22684 size_type erase(const typename object_t::key_type& key)
22685 {
22686 // the indirection via erase_internal() is added to avoid making this
22687 // function a template and thus de-rank it during overload resolution
22688 return erase_internal(key);
22689 }
22690
22691 /// @brief remove element from a JSON object given a key
22692 /// @sa https://json.nlohmann.me/api/basic_json/erase/
22693 template<class KeyType, detail::enable_if_t<
22694 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
22695 size_type erase(KeyType && key)
22696 {
22697 return erase_internal(std::forward<KeyType>(key));
22698 }
22699
22700 /// @brief remove element from a JSON array given an index
22701 /// @sa https://json.nlohmann.me/api/basic_json/erase/
22702 void erase(const size_type idx)
22703 {
22704 // this erase only works for arrays
22705 if (JSON_HEDLEY_LIKELY(is_array()))
22706 {
22707 if (JSON_HEDLEY_UNLIKELY(idx >= size()))
22708 {
22709 JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
22710 }
22711
22712 m_data.m_value.array->erase(m_data.m_value.array->begin() + static_cast<difference_type>(idx));
22713 }
22714 else
22715 {
22716 JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
22717 }
22718 }
22719
22720 /// @}
22721
22722 ////////////
22723 // lookup //
22724 ////////////
22725
22726 /// @name lookup
22727 /// @{
22728
22729 /// @brief find an element in a JSON object
22730 /// @sa https://json.nlohmann.me/api/basic_json/find/
22731 iterator find(const typename object_t::key_type& key)
22732 {
22733 auto result = end();
22734
22735 if (is_object())
22736 {
22737 result.m_it.object_iterator = m_data.m_value.object->find(key);
22738 }
22739
22740 return result;
22741 }
22742
22743 /// @brief find an element in a JSON object
22744 /// @sa https://json.nlohmann.me/api/basic_json/find/
22745 const_iterator find(const typename object_t::key_type& key) const
22746 {
22747 auto result = cend();
22748
22749 if (is_object())
22750 {
22751 result.m_it.object_iterator = m_data.m_value.object->find(key);
22752 }
22753
22754 return result;
22755 }
22756
22757 /// @brief find an element in a JSON object
22758 /// @sa https://json.nlohmann.me/api/basic_json/find/
22759 template<class KeyType, detail::enable_if_t<
22760 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
22761 iterator find(KeyType && key)
22762 {
22763 auto result = end();
22764
22765 if (is_object())
22766 {
22767 result.m_it.object_iterator = m_data.m_value.object->find(std::forward<KeyType>(key));
22768 }
22769
22770 return result;
22771 }
22772
22773 /// @brief find an element in a JSON object
22774 /// @sa https://json.nlohmann.me/api/basic_json/find/
22775 template<class KeyType, detail::enable_if_t<
22776 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
22777 const_iterator find(KeyType && key) const
22778 {
22779 auto result = cend();
22780
22781 if (is_object())
22782 {
22783 result.m_it.object_iterator = m_data.m_value.object->find(std::forward<KeyType>(key));
22784 }
22785
22786 return result;
22787 }
22788
22789 /// @brief returns the number of occurrences of a key in a JSON object
22790 /// @sa https://json.nlohmann.me/api/basic_json/count/
22791 size_type count(const typename object_t::key_type& key) const
22792 {
22793 // return 0 for all nonobject types
22794 return is_object() ? m_data.m_value.object->count(key) : 0;
22795 }
22796
22797 /// @brief returns the number of occurrences of a key in a JSON object
22798 /// @sa https://json.nlohmann.me/api/basic_json/count/
22799 template<class KeyType, detail::enable_if_t<
22800 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
22801 size_type count(KeyType && key) const
22802 {
22803 // return 0 for all nonobject types
22804 return is_object() ? m_data.m_value.object->count(std::forward<KeyType>(key)) : 0;
22805 }
22806
22807 /// @brief check the existence of an element in a JSON object
22808 /// @sa https://json.nlohmann.me/api/basic_json/contains/
22809 bool contains(const typename object_t::key_type& key) const
22810 {
22811 return is_object() && m_data.m_value.object->find(key) != m_data.m_value.object->end();
22812 }
22813
22814 /// @brief check the existence of an element in a JSON object
22815 /// @sa https://json.nlohmann.me/api/basic_json/contains/
22816 template<class KeyType, detail::enable_if_t<
22817 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
22818 bool contains(KeyType && key) const
22819 {
22820 return is_object() && m_data.m_value.object->find(std::forward<KeyType>(key)) != m_data.m_value.object->end();
22821 }
22822
22823 /// @brief check the existence of an element in a JSON object given a JSON pointer
22824 /// @sa https://json.nlohmann.me/api/basic_json/contains/
22825 bool contains(const json_pointer& ptr) const
22826 {
22827 return ptr.contains(this);
22828 }
22829
22830 template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
22831 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
22832 bool contains(const typename ::nlohmann::json_pointer<BasicJsonType>& ptr) const
22833 {
22834 return ptr.contains(this);
22835 }
22836
22837 /// @}
22838
22839 ///////////////
22840 // iterators //
22841 ///////////////
22842
22843 /// @name iterators
22844 /// @{
22845
22846 /// @brief returns an iterator to the first element
22847 /// @sa https://json.nlohmann.me/api/basic_json/begin/
22848 iterator begin() noexcept
22849 {
22850 iterator result(this);
22851 result.set_begin();
22852 return result;
22853 }
22854
22855 /// @brief returns an iterator to the first element
22856 /// @sa https://json.nlohmann.me/api/basic_json/begin/
22857 const_iterator begin() const noexcept
22858 {
22859 return cbegin();
22860 }
22861
22862 /// @brief returns a const iterator to the first element
22863 /// @sa https://json.nlohmann.me/api/basic_json/cbegin/
22864 const_iterator cbegin() const noexcept
22865 {
22866 const_iterator result(this);
22867 result.set_begin();
22868 return result;
22869 }
22870
22871 /// @brief returns an iterator to one past the last element
22872 /// @sa https://json.nlohmann.me/api/basic_json/end/
22873 iterator end() noexcept
22874 {
22875 iterator result(this);
22876 result.set_end();
22877 return result;
22878 }
22879
22880 /// @brief returns an iterator to one past the last element
22881 /// @sa https://json.nlohmann.me/api/basic_json/end/
22882 const_iterator end() const noexcept
22883 {
22884 return cend();
22885 }
22886
22887 /// @brief returns an iterator to one past the last element
22888 /// @sa https://json.nlohmann.me/api/basic_json/cend/
22889 const_iterator cend() const noexcept
22890 {
22891 const_iterator result(this);
22892 result.set_end();
22893 return result;
22894 }
22895
22896 /// @brief returns an iterator to the reverse-beginning
22897 /// @sa https://json.nlohmann.me/api/basic_json/rbegin/
22898 reverse_iterator rbegin() noexcept
22899 {
22900 return reverse_iterator(end());
22901 }
22902
22903 /// @brief returns an iterator to the reverse-beginning
22904 /// @sa https://json.nlohmann.me/api/basic_json/rbegin/
22905 const_reverse_iterator rbegin() const noexcept
22906 {
22907 return crbegin();
22908 }
22909
22910 /// @brief returns an iterator to the reverse-end
22911 /// @sa https://json.nlohmann.me/api/basic_json/rend/
22912 reverse_iterator rend() noexcept
22913 {
22914 return reverse_iterator(begin());
22915 }
22916
22917 /// @brief returns an iterator to the reverse-end
22918 /// @sa https://json.nlohmann.me/api/basic_json/rend/
22919 const_reverse_iterator rend() const noexcept
22920 {
22921 return crend();
22922 }
22923
22924 /// @brief returns a const reverse iterator to the last element
22925 /// @sa https://json.nlohmann.me/api/basic_json/crbegin/
22926 const_reverse_iterator crbegin() const noexcept
22927 {
22928 return const_reverse_iterator(cend());
22929 }
22930
22931 /// @brief returns a const reverse iterator to one before the first
22932 /// @sa https://json.nlohmann.me/api/basic_json/crend/
22933 const_reverse_iterator crend() const noexcept
22934 {
22935 return const_reverse_iterator(cbegin());
22936 }
22937
22938 public:
22939 /// @brief wrapper to access iterator member functions in range-based for
22940 /// @sa https://json.nlohmann.me/api/basic_json/items/
22941 /// @deprecated This function is deprecated since 3.1.0 and will be removed in
22942 /// version 4.0.0 of the library. Please use @ref items() instead;
22943 /// that is, replace `json::iterator_wrapper(j)` with `j.items()`.
22944 JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items())
22945 static iteration_proxy<iterator> iterator_wrapper(reference ref) noexcept
22946 {
22947 return ref.items();
22948 }
22949
22950 /// @brief wrapper to access iterator member functions in range-based for
22951 /// @sa https://json.nlohmann.me/api/basic_json/items/
22952 /// @deprecated This function is deprecated since 3.1.0 and will be removed in
22953 /// version 4.0.0 of the library. Please use @ref items() instead;
22954 /// that is, replace `json::iterator_wrapper(j)` with `j.items()`.
22955 JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items())
22956 static iteration_proxy<const_iterator> iterator_wrapper(const_reference ref) noexcept
22957 {
22958 return ref.items();
22959 }
22960
22961 /// @brief helper to access iterator member functions in range-based for
22962 /// @sa https://json.nlohmann.me/api/basic_json/items/
22963 iteration_proxy<iterator> items() noexcept
22964 {
22965 return iteration_proxy<iterator>(*this);
22966 }
22967
22968 /// @brief helper to access iterator member functions in range-based for
22969 /// @sa https://json.nlohmann.me/api/basic_json/items/
22970 iteration_proxy<const_iterator> items() const noexcept
22971 {
22972 return iteration_proxy<const_iterator>(*this);
22973 }
22974
22975 /// @}
22976
22977 //////////////
22978 // capacity //
22979 //////////////
22980
22981 /// @name capacity
22982 /// @{
22983
22984 /// @brief checks whether the container is empty.
22985 /// @sa https://json.nlohmann.me/api/basic_json/empty/
22986 bool empty() const noexcept
22987 {
22988 switch (m_data.m_type)
22989 {
22990 case value_t::null:
22991 {
22992 // null values are empty
22993 return true;
22994 }
22995
22996 case value_t::array:
22997 {
22998 // delegate call to array_t::empty()
22999 return m_data.m_value.array->empty();
23000 }
23001
23002 case value_t::object:
23003 {
23004 // delegate call to object_t::empty()
23005 return m_data.m_value.object->empty();
23006 }
23007
23008 case value_t::string:
23009 case value_t::boolean:
23010 case value_t::number_integer:
23011 case value_t::number_unsigned:
23012 case value_t::number_float:
23013 case value_t::binary:
23014 case value_t::discarded:
23015 default:
23016 {
23017 // all other types are nonempty
23018 return false;
23019 }
23020 }
23021 }
23022
23023 /// @brief returns the number of elements
23024 /// @sa https://json.nlohmann.me/api/basic_json/size/
23025 size_type size() const noexcept
23026 {
23027 switch (m_data.m_type)
23028 {
23029 case value_t::null:
23030 {
23031 // null values are empty
23032 return 0;
23033 }
23034
23035 case value_t::array:
23036 {
23037 // delegate call to array_t::size()
23038 return m_data.m_value.array->size();
23039 }
23040
23041 case value_t::object:
23042 {
23043 // delegate call to object_t::size()
23044 return m_data.m_value.object->size();
23045 }
23046
23047 case value_t::string:
23048 case value_t::boolean:
23049 case value_t::number_integer:
23050 case value_t::number_unsigned:
23051 case value_t::number_float:
23052 case value_t::binary:
23053 case value_t::discarded:
23054 default:
23055 {
23056 // all other types have size 1
23057 return 1;
23058 }
23059 }
23060 }
23061
23062 /// @brief returns the maximum possible number of elements
23063 /// @sa https://json.nlohmann.me/api/basic_json/max_size/
23064 size_type max_size() const noexcept
23065 {
23066 switch (m_data.m_type)
23067 {
23068 case value_t::array:
23069 {
23070 // delegate call to array_t::max_size()
23071 return m_data.m_value.array->max_size();
23072 }
23073
23074 case value_t::object:
23075 {
23076 // delegate call to object_t::max_size()
23077 return m_data.m_value.object->max_size();
23078 }
23079
23080 case value_t::null:
23081 case value_t::string:
23082 case value_t::boolean:
23083 case value_t::number_integer:
23084 case value_t::number_unsigned:
23085 case value_t::number_float:
23086 case value_t::binary:
23087 case value_t::discarded:
23088 default:
23089 {
23090 // all other types have max_size() == size()
23091 return size();
23092 }
23093 }
23094 }
23095
23096 /// @}
23097
23098 ///////////////
23099 // modifiers //
23100 ///////////////
23101
23102 /// @name modifiers
23103 /// @{
23104
23105 /// @brief clears the contents
23106 /// @sa https://json.nlohmann.me/api/basic_json/clear/
23107 void clear() noexcept
23108 {
23109 switch (m_data.m_type)
23110 {
23111 case value_t::number_integer:
23112 {
23113 m_data.m_value.number_integer = 0;
23114 break;
23115 }
23116
23117 case value_t::number_unsigned:
23118 {
23119 m_data.m_value.number_unsigned = 0;
23120 break;
23121 }
23122
23123 case value_t::number_float:
23124 {
23125 m_data.m_value.number_float = 0.0;
23126 break;
23127 }
23128
23129 case value_t::boolean:
23130 {
23131 m_data.m_value.boolean = false;
23132 break;
23133 }
23134
23135 case value_t::string:
23136 {
23137 m_data.m_value.string->clear();
23138 break;
23139 }
23140
23141 case value_t::binary:
23142 {
23143 m_data.m_value.binary->clear();
23144 break;
23145 }
23146
23147 case value_t::array:
23148 {
23149 m_data.m_value.array->clear();
23150 break;
23151 }
23152
23153 case value_t::object:
23154 {
23155 m_data.m_value.object->clear();
23156 break;
23157 }
23158
23159 case value_t::null:
23160 case value_t::discarded:
23161 default:
23162 break;
23163 }
23164 }
23165
23166 /// @brief add an object to an array
23167 /// @sa https://json.nlohmann.me/api/basic_json/push_back/
23168 void push_back(basic_json&& val)
23169 {
23170 // push_back only works for null objects or arrays
23171 if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
23172 {
23173 JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
23174 }
23175
23176 // transform null object into an array
23177 if (is_null())
23178 {
23179 m_data.m_type = value_t::array;
23180 m_data.m_value = value_t::array;
23181 assert_invariant();
23182 }
23183
23184 // add element to array (move semantics)
23185 const auto old_capacity = m_data.m_value.array->capacity();
23186 m_data.m_value.array->push_back(std::move(val));
23187 set_parent(m_data.m_value.array->back(), old_capacity);
23188 // if val is moved from, basic_json move constructor marks it null, so we do not call the destructor
23189 }
23190
23191 /// @brief add an object to an array
23192 /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
23193 reference operator+=(basic_json&& val)
23194 {
23195 push_back(std::move(val));
23196 return *this;
23197 }
23198
23199 /// @brief add an object to an array
23200 /// @sa https://json.nlohmann.me/api/basic_json/push_back/
23201 void push_back(const basic_json& val)
23202 {
23203 // push_back only works for null objects or arrays
23204 if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
23205 {
23206 JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
23207 }
23208
23209 // transform null object into an array
23210 if (is_null())
23211 {
23212 m_data.m_type = value_t::array;
23213 m_data.m_value = value_t::array;
23214 assert_invariant();
23215 }
23216
23217 // add element to array
23218 const auto old_capacity = m_data.m_value.array->capacity();
23219 m_data.m_value.array->push_back(val);
23220 set_parent(m_data.m_value.array->back(), old_capacity);
23221 }
23222
23223 /// @brief add an object to an array
23224 /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
23225 reference operator+=(const basic_json& val)
23226 {
23227 push_back(val);
23228 return *this;
23229 }
23230
23231 /// @brief add an object to an object
23232 /// @sa https://json.nlohmann.me/api/basic_json/push_back/
23233 void push_back(const typename object_t::value_type& val)
23234 {
23235 // push_back only works for null objects or objects
23236 if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
23237 {
23238 JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
23239 }
23240
23241 // transform null object into an object
23242 if (is_null())
23243 {
23244 m_data.m_type = value_t::object;
23245 m_data.m_value = value_t::object;
23246 assert_invariant();
23247 }
23248
23249 // add element to object
23250 auto res = m_data.m_value.object->insert(val);
23251 set_parent(res.first->second);
23252 }
23253
23254 /// @brief add an object to an object
23255 /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
23256 reference operator+=(const typename object_t::value_type& val)
23257 {
23258 push_back(val);
23259 return *this;
23260 }
23261
23262 /// @brief add an object to an object
23263 /// @sa https://json.nlohmann.me/api/basic_json/push_back/
23264 void push_back(initializer_list_t init)
23265 {
23266 if (is_object() && init.size() == 2 && (*init.begin())->is_string())
23267 {
23268 basic_json&& key = init.begin()->moved_or_copied();
23269 push_back(typename object_t::value_type(
23270 std::move(key.get_ref<string_t&>()), (init.begin() + 1)->moved_or_copied()));
23271 }
23272 else
23273 {
23274 push_back(basic_json(init));
23275 }
23276 }
23277
23278 /// @brief add an object to an object
23279 /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
23280 reference operator+=(initializer_list_t init)
23281 {
23282 push_back(init);
23283 return *this;
23284 }
23285
23286 /// @brief add an object to an array
23287 /// @sa https://json.nlohmann.me/api/basic_json/emplace_back/
23288 template<class... Args>
23289 reference emplace_back(Args&& ... args)
23290 {
23291 // emplace_back only works for null objects or arrays
23292 if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
23293 {
23294 JSON_THROW(type_error::create(311, detail::concat("cannot use emplace_back() with ", type_name()), this));
23295 }
23296
23297 // transform null object into an array
23298 if (is_null())
23299 {
23300 m_data.m_type = value_t::array;
23301 m_data.m_value = value_t::array;
23302 assert_invariant();
23303 }
23304
23305 // add element to array (perfect forwarding)
23306 const auto old_capacity = m_data.m_value.array->capacity();
23307 m_data.m_value.array->emplace_back(std::forward<Args>(args)...);
23308 return set_parent(m_data.m_value.array->back(), old_capacity);
23309 }
23310
23311 /// @brief add an object to an object if key does not exist
23312 /// @sa https://json.nlohmann.me/api/basic_json/emplace/
23313 template<class... Args>
23314 std::pair<iterator, bool> emplace(Args&& ... args)
23315 {
23316 // emplace only works for null objects or arrays
23317 if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
23318 {
23319 JSON_THROW(type_error::create(311, detail::concat("cannot use emplace() with ", type_name()), this));
23320 }
23321
23322 // transform null object into an object
23323 if (is_null())
23324 {
23325 m_data.m_type = value_t::object;
23326 m_data.m_value = value_t::object;
23327 assert_invariant();
23328 }
23329
23330 // add element to array (perfect forwarding)
23331 auto res = m_data.m_value.object->emplace(std::forward<Args>(args)...);
23332 set_parent(res.first->second);
23333
23334 // create result iterator and set iterator to the result of emplace
23335 auto it = begin();
23336 it.m_it.object_iterator = res.first;
23337
23338 // return pair of iterator and boolean
23339 return {it, res.second};
23340 }
23341
23342 /// Helper for insertion of an iterator
23343 /// @note: This uses std::distance to support GCC 4.8,
23344 /// see https://github.com/nlohmann/json/pull/1257
23345 template<typename... Args>
23346 iterator insert_iterator(const_iterator pos, Args&& ... args) // NOLINT(performance-unnecessary-value-param)
23347 {
23348 iterator result(this);
23349 JSON_ASSERT(m_data.m_value.array != nullptr);
23350
23351 auto insert_pos = std::distance(m_data.m_value.array->begin(), pos.m_it.array_iterator);
23352 m_data.m_value.array->insert(pos.m_it.array_iterator, std::forward<Args>(args)...);
23353 result.m_it.array_iterator = m_data.m_value.array->begin() + insert_pos;
23354
23355 // This could have been written as:
23356 // result.m_it.array_iterator = m_data.m_value.array->insert(pos.m_it.array_iterator, cnt, val);
23357 // but the return value of insert is missing in GCC 4.8, so it is written this way instead.
23358
23359 set_parents();
23360 return result;
23361 }
23362
23363 /// @brief inserts element into array
23364 /// @sa https://json.nlohmann.me/api/basic_json/insert/
23365 iterator insert(const_iterator pos, const basic_json& val) // NOLINT(performance-unnecessary-value-param)
23366 {
23367 // insert only works for arrays
23368 if (JSON_HEDLEY_LIKELY(is_array()))
23369 {
23370 // check if iterator pos fits to this JSON value
23371 if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
23372 {
23373 JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
23374 }
23375
23376 // insert to array and return iterator
23377 return insert_iterator(pos, val);
23378 }
23379
23380 JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
23381 }
23382
23383 /// @brief inserts element into array
23384 /// @sa https://json.nlohmann.me/api/basic_json/insert/
23385 iterator insert(const_iterator pos, basic_json&& val) // NOLINT(performance-unnecessary-value-param)
23386 {
23387 return insert(pos, val);
23388 }
23389
23390 /// @brief inserts copies of element into array
23391 /// @sa https://json.nlohmann.me/api/basic_json/insert/
23392 iterator insert(const_iterator pos, size_type cnt, const basic_json& val) // NOLINT(performance-unnecessary-value-param)
23393 {
23394 // insert only works for arrays
23395 if (JSON_HEDLEY_LIKELY(is_array()))
23396 {
23397 // check if iterator pos fits to this JSON value
23398 if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
23399 {
23400 JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
23401 }
23402
23403 // insert to array and return iterator
23404 return insert_iterator(pos, cnt, val);
23405 }
23406
23407 JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
23408 }
23409
23410 /// @brief inserts range of elements into array
23411 /// @sa https://json.nlohmann.me/api/basic_json/insert/
23412 iterator insert(const_iterator pos, const_iterator first, const_iterator last) // NOLINT(performance-unnecessary-value-param)
23413 {
23414 // insert only works for arrays
23415 if (JSON_HEDLEY_UNLIKELY(!is_array()))
23416 {
23417 JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
23418 }
23419
23420 // check if iterator pos fits to this JSON value
23421 if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
23422 {
23423 JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
23424 }
23425
23426 // check if range iterators belong to the same JSON object
23427 if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
23428 {
23429 JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
23430 }
23431
23432 if (JSON_HEDLEY_UNLIKELY(first.m_object == this))
23433 {
23434 JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container", this));
23435 }
23436
23437 // insert to array and return iterator
23438 return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator);
23439 }
23440
23441 /// @brief inserts elements from initializer list into array
23442 /// @sa https://json.nlohmann.me/api/basic_json/insert/
23443 iterator insert(const_iterator pos, initializer_list_t ilist) // NOLINT(performance-unnecessary-value-param)
23444 {
23445 // insert only works for arrays
23446 if (JSON_HEDLEY_UNLIKELY(!is_array()))
23447 {
23448 JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
23449 }
23450
23451 // check if iterator pos fits to this JSON value
23452 if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
23453 {
23454 JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
23455 }
23456
23457 // insert to array and return iterator
23458 return insert_iterator(pos, ilist.begin(), ilist.end());
23459 }
23460
23461 /// @brief inserts range of elements into object
23462 /// @sa https://json.nlohmann.me/api/basic_json/insert/
23463 void insert(const_iterator first, const_iterator last) // NOLINT(performance-unnecessary-value-param)
23464 {
23465 // insert only works for objects
23466 if (JSON_HEDLEY_UNLIKELY(!is_object()))
23467 {
23468 JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
23469 }
23470
23471 // check if range iterators belong to the same JSON object
23472 if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
23473 {
23474 JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
23475 }
23476
23477 // passed iterators must belong to objects
23478 if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()))
23479 {
23480 JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", this));
23481 }
23482
23483 m_data.m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator);
23484 set_parents();
23485 }
23486
23487 /// @brief updates a JSON object from another object, overwriting existing keys
23488 /// @sa https://json.nlohmann.me/api/basic_json/update/
23489 void update(const_reference j, bool merge_objects = false)
23490 {
23491 update(j.begin(), j.end(), merge_objects);
23492 }
23493
23494 /// @brief updates a JSON object from another object, overwriting existing keys
23495 /// @sa https://json.nlohmann.me/api/basic_json/update/
23496 void update(const_iterator first, const_iterator last, bool merge_objects = false) // NOLINT(performance-unnecessary-value-param)
23497 {
23498 // implicitly convert null value to an empty object
23499 if (is_null())
23500 {
23501 m_data.m_type = value_t::object;
23502 m_data.m_value.object = create<object_t>();
23503 assert_invariant();
23504 }
23505
23506 if (JSON_HEDLEY_UNLIKELY(!is_object()))
23507 {
23508 JSON_THROW(type_error::create(312, detail::concat("cannot use update() with ", type_name()), this));
23509 }
23510
23511 // check if range iterators belong to the same JSON object
23512 if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
23513 {
23514 JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
23515 }
23516
23517 // passed iterators must belong to objects
23518 if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()))
23519 {
23520 JSON_THROW(type_error::create(312, detail::concat("cannot use update() with ", first.m_object->type_name()), first.m_object));
23521 }
23522
23523 for (auto it = first; it != last; ++it)
23524 {
23525 if (merge_objects && it.value().is_object())
23526 {
23527 auto it2 = m_data.m_value.object->find(it.key());
23528 if (it2 != m_data.m_value.object->end())
23529 {
23530 it2->second.update(it.value(), true);
23531 continue;
23532 }
23533 }
23534 m_data.m_value.object->operator[](it.key()) = it.value();
23535 #if JSON_DIAGNOSTICS
23536 m_data.m_value.object->operator[](it.key()).m_parent = this;
23537 #endif
23538 }
23539 }
23540
23541 /// @brief exchanges the values
23542 /// @sa https://json.nlohmann.me/api/basic_json/swap/
23543 void swap(reference other) noexcept (
23544 std::is_nothrow_move_constructible<value_t>::value&&
23545 std::is_nothrow_move_assignable<value_t>::value&&
23546 std::is_nothrow_move_constructible<json_value>::value&& // NOLINT(cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
23547 std::is_nothrow_move_assignable<json_value>::value
23548 )
23549 {
23550 std::swap(m_data.m_type, other.m_data.m_type);
23551 std::swap(m_data.m_value, other.m_data.m_value);
23552
23553 set_parents();
23554 other.set_parents();
23555 assert_invariant();
23556 }
23557
23558 /// @brief exchanges the values
23559 /// @sa https://json.nlohmann.me/api/basic_json/swap/
23560 friend void swap(reference left, reference right) noexcept (
23561 std::is_nothrow_move_constructible<value_t>::value&&
23562 std::is_nothrow_move_assignable<value_t>::value&&
23563 std::is_nothrow_move_constructible<json_value>::value&& // NOLINT(cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
23564 std::is_nothrow_move_assignable<json_value>::value
23565 )
23566 {
23567 left.swap(right);
23568 }
23569
23570 /// @brief exchanges the values
23571 /// @sa https://json.nlohmann.me/api/basic_json/swap/
23572 void swap(array_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
23573 {
23574 // swap only works for arrays
23575 if (JSON_HEDLEY_LIKELY(is_array()))
23576 {
23577 using std::swap;
23578 swap(*(m_data.m_value.array), other);
23579 }
23580 else
23581 {
23582 JSON_THROW(type_error::create(310, detail::concat("cannot use swap(array_t&) with ", type_name()), this));
23583 }
23584 }
23585
23586 /// @brief exchanges the values
23587 /// @sa https://json.nlohmann.me/api/basic_json/swap/
23588 void swap(object_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
23589 {
23590 // swap only works for objects
23591 if (JSON_HEDLEY_LIKELY(is_object()))
23592 {
23593 using std::swap;
23594 swap(*(m_data.m_value.object), other);
23595 }
23596 else
23597 {
23598 JSON_THROW(type_error::create(310, detail::concat("cannot use swap(object_t&) with ", type_name()), this));
23599 }
23600 }
23601
23602 /// @brief exchanges the values
23603 /// @sa https://json.nlohmann.me/api/basic_json/swap/
23604 void swap(string_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
23605 {
23606 // swap only works for strings
23607 if (JSON_HEDLEY_LIKELY(is_string()))
23608 {
23609 using std::swap;
23610 swap(*(m_data.m_value.string), other);
23611 }
23612 else
23613 {
23614 JSON_THROW(type_error::create(310, detail::concat("cannot use swap(string_t&) with ", type_name()), this));
23615 }
23616 }
23617
23618 /// @brief exchanges the values
23619 /// @sa https://json.nlohmann.me/api/basic_json/swap/
23620 void swap(binary_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
23621 {
23622 // swap only works for strings
23623 if (JSON_HEDLEY_LIKELY(is_binary()))
23624 {
23625 using std::swap;
23626 swap(*(m_data.m_value.binary), other);
23627 }
23628 else
23629 {
23630 JSON_THROW(type_error::create(310, detail::concat("cannot use swap(binary_t&) with ", type_name()), this));
23631 }
23632 }
23633
23634 /// @brief exchanges the values
23635 /// @sa https://json.nlohmann.me/api/basic_json/swap/
23636 void swap(typename binary_t::container_type& other) // NOLINT(bugprone-exception-escape)
23637 {
23638 // swap only works for strings
23639 if (JSON_HEDLEY_LIKELY(is_binary()))
23640 {
23641 using std::swap;
23642 swap(*(m_data.m_value.binary), other);
23643 }
23644 else
23645 {
23646 JSON_THROW(type_error::create(310, detail::concat("cannot use swap(binary_t::container_type&) with ", type_name()), this));
23647 }
23648 }
23649
23650 /// @}
23651
23652 //////////////////////////////////////////
23653 // lexicographical comparison operators //
23654 //////////////////////////////////////////
23655
23656 /// @name lexicographical comparison operators
23657 /// @{
23658
23659 // note parentheses around operands are necessary; see
23660 // https://github.com/nlohmann/json/issues/1530
23661 #define JSON_IMPLEMENT_OPERATOR(op, null_result, unordered_result, default_result) \
23662 const auto lhs_type = lhs.type(); \
23663 const auto rhs_type = rhs.type(); \
23664 \
23665 if (lhs_type == rhs_type) /* NOLINT(readability/braces) */ \
23666 { \
23667 switch (lhs_type) \
23668 { \
23669 case value_t::array: \
23670 return (*lhs.m_data.m_value.array) op (*rhs.m_data.m_value.array); \
23671 \
23672 case value_t::object: \
23673 return (*lhs.m_data.m_value.object) op (*rhs.m_data.m_value.object); \
23674 \
23675 case value_t::null: \
23676 return (null_result); \
23677 \
23678 case value_t::string: \
23679 return (*lhs.m_data.m_value.string) op (*rhs.m_data.m_value.string); \
23680 \
23681 case value_t::boolean: \
23682 return (lhs.m_data.m_value.boolean) op (rhs.m_data.m_value.boolean); \
23683 \
23684 case value_t::number_integer: \
23685 return (lhs.m_data.m_value.number_integer) op (rhs.m_data.m_value.number_integer); \
23686 \
23687 case value_t::number_unsigned: \
23688 return (lhs.m_data.m_value.number_unsigned) op (rhs.m_data.m_value.number_unsigned); \
23689 \
23690 case value_t::number_float: \
23691 return (lhs.m_data.m_value.number_float) op (rhs.m_data.m_value.number_float); \
23692 \
23693 case value_t::binary: \
23694 return (*lhs.m_data.m_value.binary) op (*rhs.m_data.m_value.binary); \
23695 \
23696 case value_t::discarded: \
23697 default: \
23698 return (unordered_result); \
23699 } \
23700 } \
23701 else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_float) \
23702 { \
23703 return static_cast<number_float_t>(lhs.m_data.m_value.number_integer) op rhs.m_data.m_value.number_float; \
23704 } \
23705 else if (lhs_type == value_t::number_float && rhs_type == value_t::number_integer) \
23706 { \
23707 return lhs.m_data.m_value.number_float op static_cast<number_float_t>(rhs.m_data.m_value.number_integer); \
23708 } \
23709 else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_float) \
23710 { \
23711 return static_cast<number_float_t>(lhs.m_data.m_value.number_unsigned) op rhs.m_data.m_value.number_float; \
23712 } \
23713 else if (lhs_type == value_t::number_float && rhs_type == value_t::number_unsigned) \
23714 { \
23715 return lhs.m_data.m_value.number_float op static_cast<number_float_t>(rhs.m_data.m_value.number_unsigned); \
23716 } \
23717 else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_integer) \
23718 { \
23719 return static_cast<number_integer_t>(lhs.m_data.m_value.number_unsigned) op rhs.m_data.m_value.number_integer; \
23720 } \
23721 else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_unsigned) \
23722 { \
23723 return lhs.m_data.m_value.number_integer op static_cast<number_integer_t>(rhs.m_data.m_value.number_unsigned); \
23724 } \
23725 else if(compares_unordered(lhs, rhs))\
23726 {\
23727 return (unordered_result);\
23728 }\
23729 \
23730 return (default_result);
23731
23732 JSON_PRIVATE_UNLESS_TESTED:
23733 // returns true if:
23734 // - any operand is NaN and the other operand is of number type
23735 // - any operand is discarded
23736 // in legacy mode, discarded values are considered ordered if
23737 // an operation is computed as an odd number of inverses of others
23738 static bool compares_unordered(const_reference lhs, const_reference rhs, bool inverse = false) noexcept
23739 {
23740 if ((lhs.is_number_float() && std::isnan(lhs.m_data.m_value.number_float) && rhs.is_number())
23741 || (rhs.is_number_float() && std::isnan(rhs.m_data.m_value.number_float) && lhs.is_number()))
23742 {
23743 return true;
23744 }
23745 #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
23746 return (lhs.is_discarded() || rhs.is_discarded()) && !inverse;
23747 #else
23748 static_cast<void>(inverse);
23749 return lhs.is_discarded() || rhs.is_discarded();
23750 #endif
23751 }
23752
23753 private:
23754 bool compares_unordered(const_reference rhs, bool inverse = false) const noexcept
23755 {
23756 return compares_unordered(*this, rhs, inverse);
23757 }
23758
23759 public:
23760 #if JSON_HAS_THREE_WAY_COMPARISON
23761 /// @brief comparison: equal
23762 /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
23763 bool operator==(const_reference rhs) const noexcept
23764 {
23765 #ifdef __GNUC__
23766 #pragma GCC diagnostic push
23767 #pragma GCC diagnostic ignored "-Wfloat-equal"
23768 #endif
23769 const_reference lhs = *this;
23770 JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
23771 #ifdef __GNUC__
23772 #pragma GCC diagnostic pop
23773 #endif
23774 }
23775
23776 /// @brief comparison: equal
23777 /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
23778 template<typename ScalarType>
23779 requires std::is_scalar_v<ScalarType>
23780 bool operator==(ScalarType rhs) const noexcept
23781 {
23782 return *this == basic_json(rhs);
23783 }
23784
23785 /// @brief comparison: not equal
23786 /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
23787 bool operator!=(const_reference rhs) const noexcept
23788 {
23789 if (compares_unordered(rhs, true))
23790 {
23791 return false;
23792 }
23793 return !operator==(rhs);
23794 }
23795
23796 /// @brief comparison: 3-way
23797 /// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
23798 std::partial_ordering operator<=>(const_reference rhs) const noexcept // *NOPAD*
23799 {
23800 const_reference lhs = *this;
23801 // default_result is used if we cannot compare values. In that case,
23802 // we compare types.
23803 JSON_IMPLEMENT_OPERATOR(<=>, // *NOPAD*
23804 std::partial_ordering::equivalent,
23805 std::partial_ordering::unordered,
23806 lhs_type <=> rhs_type) // *NOPAD*
23807 }
23808
23809 /// @brief comparison: 3-way
23810 /// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
23811 template<typename ScalarType>
23812 requires std::is_scalar_v<ScalarType>
23813 std::partial_ordering operator<=>(ScalarType rhs) const noexcept // *NOPAD*
23814 {
23815 return *this <=> basic_json(rhs); // *NOPAD*
23816 }
23817
23818 #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
23819 // all operators that are computed as an odd number of inverses of others
23820 // need to be overloaded to emulate the legacy comparison behavior
23821
23822 /// @brief comparison: less than or equal
23823 /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
23824 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON)
23825 bool operator<=(const_reference rhs) const noexcept
23826 {
23827 if (compares_unordered(rhs, true))
23828 {
23829 return false;
23830 }
23831 return !(rhs < *this);
23832 }
23833
23834 /// @brief comparison: less than or equal
23835 /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
23836 template<typename ScalarType>
23837 requires std::is_scalar_v<ScalarType>
23838 bool operator<=(ScalarType rhs) const noexcept
23839 {
23840 return *this <= basic_json(rhs);
23841 }
23842
23843 /// @brief comparison: greater than or equal
23844 /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
23845 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON)
23846 bool operator>=(const_reference rhs) const noexcept
23847 {
23848 if (compares_unordered(rhs, true))
23849 {
23850 return false;
23851 }
23852 return !(*this < rhs);
23853 }
23854
23855 /// @brief comparison: greater than or equal
23856 /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
23857 template<typename ScalarType>
23858 requires std::is_scalar_v<ScalarType>
23859 bool operator>=(ScalarType rhs) const noexcept
23860 {
23861 return *this >= basic_json(rhs);
23862 }
23863 #endif
23864 #else
23865 /// @brief comparison: equal
23866 /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
23867 friend bool operator==(const_reference lhs, const_reference rhs) noexcept
23868 {
23869 #ifdef __GNUC__
23870 #pragma GCC diagnostic push
23871 #pragma GCC diagnostic ignored "-Wfloat-equal"
23872 #endif
23873 JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
23874 #ifdef __GNUC__
23875 #pragma GCC diagnostic pop
23876 #endif
23877 }
23878
23879 /// @brief comparison: equal
23880 /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
23881 template<typename ScalarType, typename std::enable_if<
23882 std::is_scalar<ScalarType>::value, int>::type = 0>
23883 friend bool operator==(const_reference lhs, ScalarType rhs) noexcept
23884 {
23885 return lhs == basic_json(rhs);
23886 }
23887
23888 /// @brief comparison: equal
23889 /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
23890 template<typename ScalarType, typename std::enable_if<
23891 std::is_scalar<ScalarType>::value, int>::type = 0>
23892 friend bool operator==(ScalarType lhs, const_reference rhs) noexcept
23893 {
23894 return basic_json(lhs) == rhs;
23895 }
23896
23897 /// @brief comparison: not equal
23898 /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
23899 friend bool operator!=(const_reference lhs, const_reference rhs) noexcept
23900 {
23901 if (compares_unordered(lhs, rhs, true))
23902 {
23903 return false;
23904 }
23905 return !(lhs == rhs);
23906 }
23907
23908 /// @brief comparison: not equal
23909 /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
23910 template<typename ScalarType, typename std::enable_if<
23911 std::is_scalar<ScalarType>::value, int>::type = 0>
23912 friend bool operator!=(const_reference lhs, ScalarType rhs) noexcept
23913 {
23914 return lhs != basic_json(rhs);
23915 }
23916
23917 /// @brief comparison: not equal
23918 /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
23919 template<typename ScalarType, typename std::enable_if<
23920 std::is_scalar<ScalarType>::value, int>::type = 0>
23921 friend bool operator!=(ScalarType lhs, const_reference rhs) noexcept
23922 {
23923 return basic_json(lhs) != rhs;
23924 }
23925
23926 /// @brief comparison: less than
23927 /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
23928 friend bool operator<(const_reference lhs, const_reference rhs) noexcept
23929 {
23930 // default_result is used if we cannot compare values. In that case,
23931 // we compare types. Note we have to call the operator explicitly,
23932 // because MSVC has problems otherwise.
23933 JSON_IMPLEMENT_OPERATOR( <, false, false, operator<(lhs_type, rhs_type))
23934 }
23935
23936 /// @brief comparison: less than
23937 /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
23938 template<typename ScalarType, typename std::enable_if<
23939 std::is_scalar<ScalarType>::value, int>::type = 0>
23940 friend bool operator<(const_reference lhs, ScalarType rhs) noexcept
23941 {
23942 return lhs < basic_json(rhs);
23943 }
23944
23945 /// @brief comparison: less than
23946 /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
23947 template<typename ScalarType, typename std::enable_if<
23948 std::is_scalar<ScalarType>::value, int>::type = 0>
23949 friend bool operator<(ScalarType lhs, const_reference rhs) noexcept
23950 {
23951 return basic_json(lhs) < rhs;
23952 }
23953
23954 /// @brief comparison: less than or equal
23955 /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
23956 friend bool operator<=(const_reference lhs, const_reference rhs) noexcept
23957 {
23958 if (compares_unordered(lhs, rhs, true))
23959 {
23960 return false;
23961 }
23962 return !(rhs < lhs);
23963 }
23964
23965 /// @brief comparison: less than or equal
23966 /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
23967 template<typename ScalarType, typename std::enable_if<
23968 std::is_scalar<ScalarType>::value, int>::type = 0>
23969 friend bool operator<=(const_reference lhs, ScalarType rhs) noexcept
23970 {
23971 return lhs <= basic_json(rhs);
23972 }
23973
23974 /// @brief comparison: less than or equal
23975 /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
23976 template<typename ScalarType, typename std::enable_if<
23977 std::is_scalar<ScalarType>::value, int>::type = 0>
23978 friend bool operator<=(ScalarType lhs, const_reference rhs) noexcept
23979 {
23980 return basic_json(lhs) <= rhs;
23981 }
23982
23983 /// @brief comparison: greater than
23984 /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
23985 friend bool operator>(const_reference lhs, const_reference rhs) noexcept
23986 {
23987 // double inverse
23988 if (compares_unordered(lhs, rhs))
23989 {
23990 return false;
23991 }
23992 return !(lhs <= rhs);
23993 }
23994
23995 /// @brief comparison: greater than
23996 /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
23997 template<typename ScalarType, typename std::enable_if<
23998 std::is_scalar<ScalarType>::value, int>::type = 0>
23999 friend bool operator>(const_reference lhs, ScalarType rhs) noexcept
24000 {
24001 return lhs > basic_json(rhs);
24002 }
24003
24004 /// @brief comparison: greater than
24005 /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
24006 template<typename ScalarType, typename std::enable_if<
24007 std::is_scalar<ScalarType>::value, int>::type = 0>
24008 friend bool operator>(ScalarType lhs, const_reference rhs) noexcept
24009 {
24010 return basic_json(lhs) > rhs;
24011 }
24012
24013 /// @brief comparison: greater than or equal
24014 /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
24015 friend bool operator>=(const_reference lhs, const_reference rhs) noexcept
24016 {
24017 if (compares_unordered(lhs, rhs, true))
24018 {
24019 return false;
24020 }
24021 return !(lhs < rhs);
24022 }
24023
24024 /// @brief comparison: greater than or equal
24025 /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
24026 template<typename ScalarType, typename std::enable_if<
24027 std::is_scalar<ScalarType>::value, int>::type = 0>
24028 friend bool operator>=(const_reference lhs, ScalarType rhs) noexcept
24029 {
24030 return lhs >= basic_json(rhs);
24031 }
24032
24033 /// @brief comparison: greater than or equal
24034 /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
24035 template<typename ScalarType, typename std::enable_if<
24036 std::is_scalar<ScalarType>::value, int>::type = 0>
24037 friend bool operator>=(ScalarType lhs, const_reference rhs) noexcept
24038 {
24039 return basic_json(lhs) >= rhs;
24040 }
24041 #endif
24042
24043 #undef JSON_IMPLEMENT_OPERATOR
24044
24045 /// @}
24046
24047 ///////////////////
24048 // serialization //
24049 ///////////////////
24050
24051 /// @name serialization
24052 /// @{
24053 #ifndef JSON_NO_IO
24054 /// @brief serialize to stream
24055 /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
24056 friend std::ostream& operator<<(std::ostream& o, const basic_json& j)
24057 {
24058 // read width member and use it as indentation parameter if nonzero
24059 const bool pretty_print = o.width() > 0;
24060 const auto indentation = pretty_print ? o.width() : 0;
24061
24062 // reset width to 0 for subsequent calls to this stream
24063 o.width(0);
24064
24065 // do the actual serialization
24066 serializer s(detail::output_adapter<char>(o), o.fill());
24067 s.dump(j, pretty_print, false, static_cast<unsigned int>(indentation));
24068 return o;
24069 }
24070
24071 /// @brief serialize to stream
24072 /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
24073 /// @deprecated This function is deprecated since 3.0.0 and will be removed in
24074 /// version 4.0.0 of the library. Please use
24075 /// operator<<(std::ostream&, const basic_json&) instead; that is,
24076 /// replace calls like `j >> o;` with `o << j;`.
24077 JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator<<(std::ostream&, const basic_json&))
24078 friend std::ostream& operator>>(const basic_json& j, std::ostream& o)
24079 {
24080 return o << j;
24081 }
24082 #endif // JSON_NO_IO
24083 /// @}
24084
24085 /////////////////////
24086 // deserialization //
24087 /////////////////////
24088
24089 /// @name deserialization
24090 /// @{
24091
24092 /// @brief deserialize from a compatible input
24093 /// @sa https://json.nlohmann.me/api/basic_json/parse/
24094 template<typename InputType>
24095 JSON_HEDLEY_WARN_UNUSED_RESULT
24096 static basic_json parse(InputType&& i,
24097 parser_callback_t cb = nullptr,
24098 const bool allow_exceptions = true,
24099 const bool ignore_comments = false)
24100 {
24101 basic_json result;
24102 parser(detail::input_adapter(std::forward<InputType>(i)), std::move(cb), allow_exceptions, ignore_comments).parse(true, result); // cppcheck-suppress[accessMoved,accessForwarded]
24103 return result;
24104 }
24105
24106 /// @brief deserialize from a pair of character iterators
24107 /// @sa https://json.nlohmann.me/api/basic_json/parse/
24108 template<typename IteratorType>
24109 JSON_HEDLEY_WARN_UNUSED_RESULT
24110 static basic_json parse(IteratorType first,
24111 IteratorType last,
24112 parser_callback_t cb = nullptr,
24113 const bool allow_exceptions = true,
24114 const bool ignore_comments = false)
24115 {
24116 basic_json result;
24117 parser(detail::input_adapter(std::move(first), std::move(last)), std::move(cb), allow_exceptions, ignore_comments).parse(true, result); // cppcheck-suppress[accessMoved]
24118 return result;
24119 }
24120
24121 JSON_HEDLEY_WARN_UNUSED_RESULT
24122 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, parse(ptr, ptr + len))
24123 static basic_json parse(detail::span_input_adapter&& i,
24124 parser_callback_t cb = nullptr,
24125 const bool allow_exceptions = true,
24126 const bool ignore_comments = false)
24127 {
24128 basic_json result;
24129 parser(i.get(), std::move(cb), allow_exceptions, ignore_comments).parse(true, result); // cppcheck-suppress[accessMoved]
24130 return result;
24131 }
24132
24133 /// @brief check if the input is valid JSON
24134 /// @sa https://json.nlohmann.me/api/basic_json/accept/
24135 template<typename InputType>
24136 static bool accept(InputType&& i,
24137 const bool ignore_comments = false)
24138 {
24139 return parser(detail::input_adapter(std::forward<InputType>(i)), nullptr, false, ignore_comments).accept(true);
24140 }
24141
24142 /// @brief check if the input is valid JSON
24143 /// @sa https://json.nlohmann.me/api/basic_json/accept/
24144 template<typename IteratorType>
24145 static bool accept(IteratorType first, IteratorType last,
24146 const bool ignore_comments = false)
24147 {
24148 return parser(detail::input_adapter(std::move(first), std::move(last)), nullptr, false, ignore_comments).accept(true);
24149 }
24150
24151 JSON_HEDLEY_WARN_UNUSED_RESULT
24152 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, accept(ptr, ptr + len))
24153 static bool accept(detail::span_input_adapter&& i,
24154 const bool ignore_comments = false)
24155 {
24156 return parser(i.get(), nullptr, false, ignore_comments).accept(true);
24157 }
24158
24159 /// @brief generate SAX events
24160 /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
24161 template <typename InputType, typename SAX>
24162 JSON_HEDLEY_NON_NULL(2)
24163 static bool sax_parse(InputType&& i, SAX* sax,
24164 input_format_t format = input_format_t::json,
24165 const bool strict = true,
24166 const bool ignore_comments = false)
24167 {
24168 auto ia = detail::input_adapter(std::forward<InputType>(i));
24169 return format == input_format_t::json
24170 ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
24171 : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
24172 }
24173
24174 /// @brief generate SAX events
24175 /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
24176 template<class IteratorType, class SAX>
24177 JSON_HEDLEY_NON_NULL(3)
24178 static bool sax_parse(IteratorType first, IteratorType last, SAX* sax,
24179 input_format_t format = input_format_t::json,
24180 const bool strict = true,
24181 const bool ignore_comments = false)
24182 {
24183 auto ia = detail::input_adapter(std::move(first), std::move(last));
24184 return format == input_format_t::json
24185 ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
24186 : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
24187 }
24188
24189 /// @brief generate SAX events
24190 /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
24191 /// @deprecated This function is deprecated since 3.8.0 and will be removed in
24192 /// version 4.0.0 of the library. Please use
24193 /// sax_parse(ptr, ptr + len) instead.
24194 template <typename SAX>
24195 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, sax_parse(ptr, ptr + len, ...))
24196 JSON_HEDLEY_NON_NULL(2)
24197 static bool sax_parse(detail::span_input_adapter&& i, SAX* sax,
24198 input_format_t format = input_format_t::json,
24199 const bool strict = true,
24200 const bool ignore_comments = false)
24201 {
24202 auto ia = i.get();
24203 return format == input_format_t::json
24204 // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
24205 ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
24206 // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
24207 : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
24208 }
24209 #ifndef JSON_NO_IO
24210 /// @brief deserialize from stream
24211 /// @sa https://json.nlohmann.me/api/basic_json/operator_gtgt/
24212 /// @deprecated This stream operator is deprecated since 3.0.0 and will be removed in
24213 /// version 4.0.0 of the library. Please use
24214 /// operator>>(std::istream&, basic_json&) instead; that is,
24215 /// replace calls like `j << i;` with `i >> j;`.
24216 JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator>>(std::istream&, basic_json&))
24217 friend std::istream& operator<<(basic_json& j, std::istream& i)
24218 {
24219 return operator>>(i, j);
24220 }
24221
24222 /// @brief deserialize from stream
24223 /// @sa https://json.nlohmann.me/api/basic_json/operator_gtgt/
24224 friend std::istream& operator>>(std::istream& i, basic_json& j)
24225 {
24226 parser(detail::input_adapter(i)).parse(false, j);
24227 return i;
24228 }
24229 #endif // JSON_NO_IO
24230 /// @}
24231
24232 ///////////////////////////
24233 // convenience functions //
24234 ///////////////////////////
24235
24236 /// @brief return the type as string
24237 /// @sa https://json.nlohmann.me/api/basic_json/type_name/
24238 JSON_HEDLEY_RETURNS_NON_NULL
24239 const char* type_name() const noexcept
24240 {
24241 switch (m_data.m_type)
24242 {
24243 case value_t::null:
24244 return "null";
24245 case value_t::object:
24246 return "object";
24247 case value_t::array:
24248 return "array";
24249 case value_t::string:
24250 return "string";
24251 case value_t::boolean:
24252 return "boolean";
24253 case value_t::binary:
24254 return "binary";
24255 case value_t::discarded:
24256 return "discarded";
24257 case value_t::number_integer:
24258 case value_t::number_unsigned:
24259 case value_t::number_float:
24260 default:
24261 return "number";
24262 }
24263 }
24264
24265 JSON_PRIVATE_UNLESS_TESTED:
24266 //////////////////////
24267 // member variables //
24268 //////////////////////
24269
24270 struct data
24271 {
24272 /// the type of the current element
24273 value_t m_type = value_t::null;
24274
24275 /// the value of the current element
24276 json_value m_value = {};
24277
24278 data(const value_t v)
24279 : m_type(v), m_value(v)
24280 {
24281 }
24282
24283 data(size_type cnt, const basic_json& val)
24284 : m_type(value_t::array)
24285 {
24286 m_value.array = create<array_t>(cnt, val);
24287 }
24288
24289 data() noexcept = default;
24290 data(data&&) noexcept = default;
24291 data(const data&) noexcept = delete;
24292 data& operator=(data&&) noexcept = delete;
24293 data& operator=(const data&) noexcept = delete;
24294
24295 ~data() noexcept
24296 {
24297 m_value.destroy(m_type);
24298 }
24299 };
24300
24301 data m_data = {};
24302
24303 #if JSON_DIAGNOSTICS
24304 /// a pointer to a parent value (for debugging purposes)
24305 basic_json* m_parent = nullptr;
24306 #endif
24307
24308 #if JSON_DIAGNOSTIC_POSITIONS
24309 /// the start position of the value
24310 std::size_t start_position = std::string::npos;
24311 /// the end position of the value
24312 std::size_t end_position = std::string::npos;
24313 public:
24314 constexpr std::size_t start_pos() const noexcept
24315 {
24316 return start_position;
24317 }
24318
24319 constexpr std::size_t end_pos() const noexcept
24320 {
24321 return end_position;
24322 }
24323 #endif
24324
24325 //////////////////////////////////////////
24326 // binary serialization/deserialization //
24327 //////////////////////////////////////////
24328
24329 /// @name binary serialization/deserialization support
24330 /// @{
24331
24332 public:
24333 /// @brief create a CBOR serialization of a given JSON value
24334 /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
24335 static std::vector<std::uint8_t> to_cbor(const basic_json& j)
24336 {
24337 std::vector<std::uint8_t> result;
24338 to_cbor(j, result);
24339 return result;
24340 }
24341
24342 /// @brief create a CBOR serialization of a given JSON value
24343 /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
24344 static void to_cbor(const basic_json& j, detail::output_adapter<std::uint8_t> o)
24345 {
24346 binary_writer<std::uint8_t>(o).write_cbor(j);
24347 }
24348
24349 /// @brief create a CBOR serialization of a given JSON value
24350 /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
24351 static void to_cbor(const basic_json& j, detail::output_adapter<char> o)
24352 {
24353 binary_writer<char>(o).write_cbor(j);
24354 }
24355
24356 /// @brief create a MessagePack serialization of a given JSON value
24357 /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
24358 static std::vector<std::uint8_t> to_msgpack(const basic_json& j)
24359 {
24360 std::vector<std::uint8_t> result;
24361 to_msgpack(j, result);
24362 return result;
24363 }
24364
24365 /// @brief create a MessagePack serialization of a given JSON value
24366 /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
24367 static void to_msgpack(const basic_json& j, detail::output_adapter<std::uint8_t> o)
24368 {
24369 binary_writer<std::uint8_t>(o).write_msgpack(j);
24370 }
24371
24372 /// @brief create a MessagePack serialization of a given JSON value
24373 /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
24374 static void to_msgpack(const basic_json& j, detail::output_adapter<char> o)
24375 {
24376 binary_writer<char>(o).write_msgpack(j);
24377 }
24378
24379 /// @brief create a UBJSON serialization of a given JSON value
24380 /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
24381 static std::vector<std::uint8_t> to_ubjson(const basic_json& j,
24382 const bool use_size = false,
24383 const bool use_type = false)
24384 {
24385 std::vector<std::uint8_t> result;
24386 to_ubjson(j, result, use_size, use_type);
24387 return result;
24388 }
24389
24390 /// @brief create a UBJSON serialization of a given JSON value
24391 /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
24392 static void to_ubjson(const basic_json& j, detail::output_adapter<std::uint8_t> o,
24393 const bool use_size = false, const bool use_type = false)
24394 {
24395 binary_writer<std::uint8_t>(o).write_ubjson(j, use_size, use_type);
24396 }
24397
24398 /// @brief create a UBJSON serialization of a given JSON value
24399 /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
24400 static void to_ubjson(const basic_json& j, detail::output_adapter<char> o,
24401 const bool use_size = false, const bool use_type = false)
24402 {
24403 binary_writer<char>(o).write_ubjson(j, use_size, use_type);
24404 }
24405
24406 /// @brief create a BJData serialization of a given JSON value
24407 /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
24408 static std::vector<std::uint8_t> to_bjdata(const basic_json& j,
24409 const bool use_size = false,
24410 const bool use_type = false,
24411 const bjdata_version_t version = bjdata_version_t::draft2)
24412 {
24413 std::vector<std::uint8_t> result;
24414 to_bjdata(j, result, use_size, use_type, version);
24415 return result;
24416 }
24417
24418 /// @brief create a BJData serialization of a given JSON value
24419 /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
24420 static void to_bjdata(const basic_json& j, detail::output_adapter<std::uint8_t> o,
24421 const bool use_size = false, const bool use_type = false,
24422 const bjdata_version_t version = bjdata_version_t::draft2)
24423 {
24424 binary_writer<std::uint8_t>(o).write_ubjson(j, use_size, use_type, true, true, version);
24425 }
24426
24427 /// @brief create a BJData serialization of a given JSON value
24428 /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
24429 static void to_bjdata(const basic_json& j, detail::output_adapter<char> o,
24430 const bool use_size = false, const bool use_type = false,
24431 const bjdata_version_t version = bjdata_version_t::draft2)
24432 {
24433 binary_writer<char>(o).write_ubjson(j, use_size, use_type, true, true, version);
24434 }
24435
24436 /// @brief create a BSON serialization of a given JSON value
24437 /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
24438 static std::vector<std::uint8_t> to_bson(const basic_json& j)
24439 {
24440 std::vector<std::uint8_t> result;
24441 to_bson(j, result);
24442 return result;
24443 }
24444
24445 /// @brief create a BSON serialization of a given JSON value
24446 /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
24447 static void to_bson(const basic_json& j, detail::output_adapter<std::uint8_t> o)
24448 {
24449 binary_writer<std::uint8_t>(o).write_bson(j);
24450 }
24451
24452 /// @brief create a BSON serialization of a given JSON value
24453 /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
24454 static void to_bson(const basic_json& j, detail::output_adapter<char> o)
24455 {
24456 binary_writer<char>(o).write_bson(j);
24457 }
24458
24459 /// @brief create a JSON value from an input in CBOR format
24460 /// @sa https://json.nlohmann.me/api/basic_json/from_cbor/
24461 template<typename InputType>
24462 JSON_HEDLEY_WARN_UNUSED_RESULT
24463 static basic_json from_cbor(InputType&& i,
24464 const bool strict = true,
24465 const bool allow_exceptions = true,
24466 const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
24467 {
24468 basic_json result;
24469 auto ia = detail::input_adapter(std::forward<InputType>(i));
24470 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24471 const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler); // cppcheck-suppress[accessMoved]
24472 return res ? result : basic_json(value_t::discarded);
24473 }
24474
24475 /// @brief create a JSON value from an input in CBOR format
24476 /// @sa https://json.nlohmann.me/api/basic_json/from_cbor/
24477 template<typename IteratorType>
24478 JSON_HEDLEY_WARN_UNUSED_RESULT
24479 static basic_json from_cbor(IteratorType first, IteratorType last,
24480 const bool strict = true,
24481 const bool allow_exceptions = true,
24482 const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
24483 {
24484 basic_json result;
24485 auto ia = detail::input_adapter(std::move(first), std::move(last));
24486 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24487 const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler); // cppcheck-suppress[accessMoved]
24488 return res ? result : basic_json(value_t::discarded);
24489 }
24490
24491 template<typename T>
24492 JSON_HEDLEY_WARN_UNUSED_RESULT
24493 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len))
24494 static basic_json from_cbor(const T* ptr, std::size_t len,
24495 const bool strict = true,
24496 const bool allow_exceptions = true,
24497 const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
24498 {
24499 return from_cbor(ptr, ptr + len, strict, allow_exceptions, tag_handler);
24500 }
24501
24502 JSON_HEDLEY_WARN_UNUSED_RESULT
24503 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len))
24504 static basic_json from_cbor(detail::span_input_adapter&& i,
24505 const bool strict = true,
24506 const bool allow_exceptions = true,
24507 const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
24508 {
24509 basic_json result;
24510 auto ia = i.get();
24511 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24512 // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
24513 const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler); // cppcheck-suppress[accessMoved]
24514 return res ? result : basic_json(value_t::discarded);
24515 }
24516
24517 /// @brief create a JSON value from an input in MessagePack format
24518 /// @sa https://json.nlohmann.me/api/basic_json/from_msgpack/
24519 template<typename InputType>
24520 JSON_HEDLEY_WARN_UNUSED_RESULT
24521 static basic_json from_msgpack(InputType&& i,
24522 const bool strict = true,
24523 const bool allow_exceptions = true)
24524 {
24525 basic_json result;
24526 auto ia = detail::input_adapter(std::forward<InputType>(i));
24527 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24528 const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict); // cppcheck-suppress[accessMoved]
24529 return res ? result : basic_json(value_t::discarded);
24530 }
24531
24532 /// @brief create a JSON value from an input in MessagePack format
24533 /// @sa https://json.nlohmann.me/api/basic_json/from_msgpack/
24534 template<typename IteratorType>
24535 JSON_HEDLEY_WARN_UNUSED_RESULT
24536 static basic_json from_msgpack(IteratorType first, IteratorType last,
24537 const bool strict = true,
24538 const bool allow_exceptions = true)
24539 {
24540 basic_json result;
24541 auto ia = detail::input_adapter(std::move(first), std::move(last));
24542 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24543 const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict); // cppcheck-suppress[accessMoved]
24544 return res ? result : basic_json(value_t::discarded);
24545 }
24546
24547 template<typename T>
24548 JSON_HEDLEY_WARN_UNUSED_RESULT
24549 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len))
24550 static basic_json from_msgpack(const T* ptr, std::size_t len,
24551 const bool strict = true,
24552 const bool allow_exceptions = true)
24553 {
24554 return from_msgpack(ptr, ptr + len, strict, allow_exceptions);
24555 }
24556
24557 JSON_HEDLEY_WARN_UNUSED_RESULT
24558 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len))
24559 static basic_json from_msgpack(detail::span_input_adapter&& i,
24560 const bool strict = true,
24561 const bool allow_exceptions = true)
24562 {
24563 basic_json result;
24564 auto ia = i.get();
24565 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24566 // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
24567 const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict); // cppcheck-suppress[accessMoved]
24568 return res ? result : basic_json(value_t::discarded);
24569 }
24570
24571 /// @brief create a JSON value from an input in UBJSON format
24572 /// @sa https://json.nlohmann.me/api/basic_json/from_ubjson/
24573 template<typename InputType>
24574 JSON_HEDLEY_WARN_UNUSED_RESULT
24575 static basic_json from_ubjson(InputType&& i,
24576 const bool strict = true,
24577 const bool allow_exceptions = true)
24578 {
24579 basic_json result;
24580 auto ia = detail::input_adapter(std::forward<InputType>(i));
24581 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24582 const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict); // cppcheck-suppress[accessMoved]
24583 return res ? result : basic_json(value_t::discarded);
24584 }
24585
24586 /// @brief create a JSON value from an input in UBJSON format
24587 /// @sa https://json.nlohmann.me/api/basic_json/from_ubjson/
24588 template<typename IteratorType>
24589 JSON_HEDLEY_WARN_UNUSED_RESULT
24590 static basic_json from_ubjson(IteratorType first, IteratorType last,
24591 const bool strict = true,
24592 const bool allow_exceptions = true)
24593 {
24594 basic_json result;
24595 auto ia = detail::input_adapter(std::move(first), std::move(last));
24596 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24597 const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict); // cppcheck-suppress[accessMoved]
24598 return res ? result : basic_json(value_t::discarded);
24599 }
24600
24601 template<typename T>
24602 JSON_HEDLEY_WARN_UNUSED_RESULT
24603 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len))
24604 static basic_json from_ubjson(const T* ptr, std::size_t len,
24605 const bool strict = true,
24606 const bool allow_exceptions = true)
24607 {
24608 return from_ubjson(ptr, ptr + len, strict, allow_exceptions);
24609 }
24610
24611 JSON_HEDLEY_WARN_UNUSED_RESULT
24612 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len))
24613 static basic_json from_ubjson(detail::span_input_adapter&& i,
24614 const bool strict = true,
24615 const bool allow_exceptions = true)
24616 {
24617 basic_json result;
24618 auto ia = i.get();
24619 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24620 // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
24621 const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict); // cppcheck-suppress[accessMoved]
24622 return res ? result : basic_json(value_t::discarded);
24623 }
24624
24625 /// @brief create a JSON value from an input in BJData format
24626 /// @sa https://json.nlohmann.me/api/basic_json/from_bjdata/
24627 template<typename InputType>
24628 JSON_HEDLEY_WARN_UNUSED_RESULT
24629 static basic_json from_bjdata(InputType&& i,
24630 const bool strict = true,
24631 const bool allow_exceptions = true)
24632 {
24633 basic_json result;
24634 auto ia = detail::input_adapter(std::forward<InputType>(i));
24635 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24636 const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict); // cppcheck-suppress[accessMoved]
24637 return res ? result : basic_json(value_t::discarded);
24638 }
24639
24640 /// @brief create a JSON value from an input in BJData format
24641 /// @sa https://json.nlohmann.me/api/basic_json/from_bjdata/
24642 template<typename IteratorType>
24643 JSON_HEDLEY_WARN_UNUSED_RESULT
24644 static basic_json from_bjdata(IteratorType first, IteratorType last,
24645 const bool strict = true,
24646 const bool allow_exceptions = true)
24647 {
24648 basic_json result;
24649 auto ia = detail::input_adapter(std::move(first), std::move(last));
24650 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24651 const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict); // cppcheck-suppress[accessMoved]
24652 return res ? result : basic_json(value_t::discarded);
24653 }
24654
24655 /// @brief create a JSON value from an input in BSON format
24656 /// @sa https://json.nlohmann.me/api/basic_json/from_bson/
24657 template<typename InputType>
24658 JSON_HEDLEY_WARN_UNUSED_RESULT
24659 static basic_json from_bson(InputType&& i,
24660 const bool strict = true,
24661 const bool allow_exceptions = true)
24662 {
24663 basic_json result;
24664 auto ia = detail::input_adapter(std::forward<InputType>(i));
24665 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24666 const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict); // cppcheck-suppress[accessMoved]
24667 return res ? result : basic_json(value_t::discarded);
24668 }
24669
24670 /// @brief create a JSON value from an input in BSON format
24671 /// @sa https://json.nlohmann.me/api/basic_json/from_bson/
24672 template<typename IteratorType>
24673 JSON_HEDLEY_WARN_UNUSED_RESULT
24674 static basic_json from_bson(IteratorType first, IteratorType last,
24675 const bool strict = true,
24676 const bool allow_exceptions = true)
24677 {
24678 basic_json result;
24679 auto ia = detail::input_adapter(std::move(first), std::move(last));
24680 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24681 const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict); // cppcheck-suppress[accessMoved]
24682 return res ? result : basic_json(value_t::discarded);
24683 }
24684
24685 template<typename T>
24686 JSON_HEDLEY_WARN_UNUSED_RESULT
24687 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len))
24688 static basic_json from_bson(const T* ptr, std::size_t len,
24689 const bool strict = true,
24690 const bool allow_exceptions = true)
24691 {
24692 return from_bson(ptr, ptr + len, strict, allow_exceptions);
24693 }
24694
24695 JSON_HEDLEY_WARN_UNUSED_RESULT
24696 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len))
24697 static basic_json from_bson(detail::span_input_adapter&& i,
24698 const bool strict = true,
24699 const bool allow_exceptions = true)
24700 {
24701 basic_json result;
24702 auto ia = i.get();
24703 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24704 // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
24705 const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict); // cppcheck-suppress[accessMoved]
24706 return res ? result : basic_json(value_t::discarded);
24707 }
24708 /// @}
24709
24710 //////////////////////////
24711 // JSON Pointer support //
24712 //////////////////////////
24713
24714 /// @name JSON Pointer functions
24715 /// @{
24716
24717 /// @brief access specified element via JSON Pointer
24718 /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
24719 reference operator[](const json_pointer& ptr)
24720 {
24721 return ptr.get_unchecked(this);
24722 }
24723
24724 template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
24725 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
24726 reference operator[](const ::nlohmann::json_pointer<BasicJsonType>& ptr)
24727 {
24728 return ptr.get_unchecked(this);
24729 }
24730
24731 /// @brief access specified element via JSON Pointer
24732 /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
24733 const_reference operator[](const json_pointer& ptr) const
24734 {
24735 return ptr.get_unchecked(this);
24736 }
24737
24738 template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
24739 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
24740 const_reference operator[](const ::nlohmann::json_pointer<BasicJsonType>& ptr) const
24741 {
24742 return ptr.get_unchecked(this);
24743 }
24744
24745 /// @brief access specified element via JSON Pointer
24746 /// @sa https://json.nlohmann.me/api/basic_json/at/
24747 reference at(const json_pointer& ptr)
24748 {
24749 return ptr.get_checked(this);
24750 }
24751
24752 template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
24753 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
24754 reference at(const ::nlohmann::json_pointer<BasicJsonType>& ptr)
24755 {
24756 return ptr.get_checked(this);
24757 }
24758
24759 /// @brief access specified element via JSON Pointer
24760 /// @sa https://json.nlohmann.me/api/basic_json/at/
24761 const_reference at(const json_pointer& ptr) const
24762 {
24763 return ptr.get_checked(this);
24764 }
24765
24766 template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
24767 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
24768 const_reference at(const ::nlohmann::json_pointer<BasicJsonType>& ptr) const
24769 {
24770 return ptr.get_checked(this);
24771 }
24772
24773 /// @brief return flattened JSON value
24774 /// @sa https://json.nlohmann.me/api/basic_json/flatten/
24775 basic_json flatten() const
24776 {
24777 basic_json result(value_t::object);
24778 json_pointer::flatten("", *this, result);
24779 return result;
24780 }
24781
24782 /// @brief unflatten a previously flattened JSON value
24783 /// @sa https://json.nlohmann.me/api/basic_json/unflatten/
24784 basic_json unflatten() const
24785 {
24786 return json_pointer::unflatten(*this);
24787 }
24788
24789 /// @}
24790
24791 //////////////////////////
24792 // JSON Patch functions //
24793 //////////////////////////
24794
24795 /// @name JSON Patch functions
24796 /// @{
24797
24798 /// @brief applies a JSON patch in-place without copying the object
24799 /// @sa https://json.nlohmann.me/api/basic_json/patch/
24800 void patch_inplace(const basic_json& json_patch)
24801 {
24802 basic_json& result = *this;
24803 // the valid JSON Patch operations
24804 enum class patch_operations {add, remove, replace, move, copy, test, invalid};
24805
24806 const auto get_op = [](const string_t& op)
24807 {
24808 if (op == "add")
24809 {
24810 return patch_operations::add;
24811 }
24812 if (op == "remove")
24813 {
24814 return patch_operations::remove;
24815 }
24816 if (op == "replace")
24817 {
24818 return patch_operations::replace;
24819 }
24820 if (op == "move")
24821 {
24822 return patch_operations::move;
24823 }
24824 if (op == "copy")
24825 {
24826 return patch_operations::copy;
24827 }
24828 if (op == "test")
24829 {
24830 return patch_operations::test;
24831 }
24832
24833 return patch_operations::invalid;
24834 };
24835
24836 // wrapper for "add" operation; add value at ptr
24837 const auto operation_add = [&result](json_pointer & ptr, const basic_json & val)
24838 {
24839 // adding to the root of the target document means replacing it
24840 if (ptr.empty())
24841 {
24842 result = val;
24843 return;
24844 }
24845
24846 // make sure the top element of the pointer exists
24847 json_pointer const top_pointer = ptr.top();
24848 if (top_pointer != ptr)
24849 {
24850 result.at(top_pointer);
24851 }
24852
24853 // get reference to parent of JSON pointer ptr
24854 const auto last_path = ptr.back();
24855 ptr.pop_back();
24856 // parent must exist when performing patch add per RFC6902 specs
24857 basic_json& parent = result.at(ptr);
24858
24859 switch (parent.m_data.m_type)
24860 {
24861 case value_t::null:
24862 case value_t::object:
24863 {
24864 // use operator[] to add value
24865 parent[last_path] = val;
24866 break;
24867 }
24868
24869 case value_t::array:
24870 {
24871 if (last_path == "-")
24872 {
24873 // special case: append to back
24874 parent.push_back(val);
24875 }
24876 else
24877 {
24878 const auto idx = json_pointer::template array_index<basic_json_t>(last_path);
24879 if (JSON_HEDLEY_UNLIKELY(idx > parent.size()))
24880 {
24881 // avoid undefined behavior
24882 JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), &parent));
24883 }
24884
24885 // default case: insert add offset
24886 parent.insert(parent.begin() + static_cast<difference_type>(idx), val);
24887 }
24888 break;
24889 }
24890
24891 // if there exists a parent it cannot be primitive
24892 case value_t::string: // LCOV_EXCL_LINE
24893 case value_t::boolean: // LCOV_EXCL_LINE
24894 case value_t::number_integer: // LCOV_EXCL_LINE
24895 case value_t::number_unsigned: // LCOV_EXCL_LINE
24896 case value_t::number_float: // LCOV_EXCL_LINE
24897 case value_t::binary: // LCOV_EXCL_LINE
24898 case value_t::discarded: // LCOV_EXCL_LINE
24899 default: // LCOV_EXCL_LINE
24900 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
24901 }
24902 };
24903
24904 // wrapper for "remove" operation; remove value at ptr
24905 const auto operation_remove = [this, & result](json_pointer & ptr)
24906 {
24907 // get reference to parent of JSON pointer ptr
24908 const auto last_path = ptr.back();
24909 ptr.pop_back();
24910 basic_json& parent = result.at(ptr);
24911
24912 // remove child
24913 if (parent.is_object())
24914 {
24915 // perform range check
24916 auto it = parent.find(last_path);
24917 if (JSON_HEDLEY_LIKELY(it != parent.end()))
24918 {
24919 parent.erase(it);
24920 }
24921 else
24922 {
24923 JSON_THROW(out_of_range::create(403, detail::concat("key '", last_path, "' not found"), this));
24924 }
24925 }
24926 else if (parent.is_array())
24927 {
24928 // note erase performs range check
24929 parent.erase(json_pointer::template array_index<basic_json_t>(last_path));
24930 }
24931 };
24932
24933 // type check: top level value must be an array
24934 if (JSON_HEDLEY_UNLIKELY(!json_patch.is_array()))
24935 {
24936 JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", &json_patch));
24937 }
24938
24939 // iterate and apply the operations
24940 for (const auto& val : json_patch)
24941 {
24942 // wrapper to get a value for an operation
24943 const auto get_value = [&val](const string_t& op,
24944 const string_t& member,
24945 bool string_type) -> basic_json &
24946 {
24947 // find value
24948 auto it = val.m_data.m_value.object->find(member);
24949
24950 // context-sensitive error message
24951 const auto error_msg = (op == "op") ? "operation" : detail::concat("operation '", op, '\''); // NOLINT(bugprone-unused-local-non-trivial-variable)
24952
24953 // check if desired value is present
24954 if (JSON_HEDLEY_UNLIKELY(it == val.m_data.m_value.object->end()))
24955 {
24956 // NOLINTNEXTLINE(performance-inefficient-string-concatenation)
24957 JSON_THROW(parse_error::create(105, 0, detail::concat(error_msg, " must have member '", member, "'"), &val));
24958 }
24959
24960 // check if result is of type string
24961 if (JSON_HEDLEY_UNLIKELY(string_type && !it->second.is_string()))
24962 {
24963 // NOLINTNEXTLINE(performance-inefficient-string-concatenation)
24964 JSON_THROW(parse_error::create(105, 0, detail::concat(error_msg, " must have string member '", member, "'"), &val));
24965 }
24966
24967 // no error: return value
24968 return it->second;
24969 };
24970
24971 // type check: every element of the array must be an object
24972 if (JSON_HEDLEY_UNLIKELY(!val.is_object()))
24973 {
24974 JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", &val));
24975 }
24976
24977 // collect mandatory members
24978 const auto op = get_value("op", "op", true).template get<string_t>();
24979 const auto path = get_value(op, "path", true).template get<string_t>();
24980 json_pointer ptr(path);
24981
24982 switch (get_op(op))
24983 {
24984 case patch_operations::add:
24985 {
24986 operation_add(ptr, get_value("add", "value", false));
24987 break;
24988 }
24989
24990 case patch_operations::remove:
24991 {
24992 operation_remove(ptr);
24993 break;
24994 }
24995
24996 case patch_operations::replace:
24997 {
24998 // the "path" location must exist - use at()
24999 result.at(ptr) = get_value("replace", "value", false);
25000 break;
25001 }
25002
25003 case patch_operations::move:
25004 {
25005 const auto from_path = get_value("move", "from", true).template get<string_t>();
25006 json_pointer from_ptr(from_path);
25007
25008 // the "from" location must exist - use at()
25009 basic_json const v = result.at(from_ptr);
25010
25011 // The move operation is functionally identical to a
25012 // "remove" operation on the "from" location, followed
25013 // immediately by an "add" operation at the target
25014 // location with the value that was just removed.
25015 operation_remove(from_ptr);
25016 operation_add(ptr, v);
25017 break;
25018 }
25019
25020 case patch_operations::copy:
25021 {
25022 const auto from_path = get_value("copy", "from", true).template get<string_t>();
25023 const json_pointer from_ptr(from_path);
25024
25025 // the "from" location must exist - use at()
25026 basic_json const v = result.at(from_ptr);
25027
25028 // The copy is functionally identical to an "add"
25029 // operation at the target location using the value
25030 // specified in the "from" member.
25031 operation_add(ptr, v);
25032 break;
25033 }
25034
25035 case patch_operations::test:
25036 {
25037 bool success = false;
25038 JSON_TRY
25039 {
25040 // check if "value" matches the one at "path"
25041 // the "path" location must exist - use at()
25042 success = (result.at(ptr) == get_value("test", "value", false));
25043 }
25044 JSON_INTERNAL_CATCH (out_of_range&)
25045 {
25046 // ignore out of range errors: success remains false
25047 }
25048
25049 // throw an exception if test fails
25050 if (JSON_HEDLEY_UNLIKELY(!success))
25051 {
25052 JSON_THROW(other_error::create(501, detail::concat("unsuccessful: ", val.dump()), &val));
25053 }
25054
25055 break;
25056 }
25057
25058 case patch_operations::invalid:
25059 default:
25060 {
25061 // op must be "add", "remove", "replace", "move", "copy", or
25062 // "test"
25063 JSON_THROW(parse_error::create(105, 0, detail::concat("operation value '", op, "' is invalid"), &val));
25064 }
25065 }
25066 }
25067 }
25068
25069 /// @brief applies a JSON patch to a copy of the current object
25070 /// @sa https://json.nlohmann.me/api/basic_json/patch/
25071 basic_json patch(const basic_json& json_patch) const
25072 {
25073 basic_json result = *this;
25074 result.patch_inplace(json_patch);
25075 return result;
25076 }
25077
25078 /// @brief creates a diff as a JSON patch
25079 /// @sa https://json.nlohmann.me/api/basic_json/diff/
25080 JSON_HEDLEY_WARN_UNUSED_RESULT
25081 static basic_json diff(const basic_json& source, const basic_json& target,
25082 const string_t& path = "")
25083 {
25084 // the patch
25085 basic_json result(value_t::array);
25086
25087 // if the values are the same, return empty patch
25088 if (source == target)
25089 {
25090 return result;
25091 }
25092
25093 if (source.type() != target.type())
25094 {
25095 // different types: replace value
25096 result.push_back(
25097 {
25098 {"op", "replace"}, {"path", path}, {"value", target}
25099 });
25100 return result;
25101 }
25102
25103 switch (source.type())
25104 {
25105 case value_t::array:
25106 {
25107 // first pass: traverse common elements
25108 std::size_t i = 0;
25109 while (i < source.size() && i < target.size())
25110 {
25111 // recursive call to compare array values at index i
25112 auto temp_diff = diff(source[i], target[i], detail::concat<string_t>(path, '/', detail::to_string<string_t>(i)));
25113 result.insert(result.end(), temp_diff.begin(), temp_diff.end());
25114 ++i;
25115 }
25116
25117 // We now reached the end of at least one array
25118 // in a second pass, traverse the remaining elements
25119
25120 // remove my remaining elements
25121 const auto end_index = static_cast<difference_type>(result.size());
25122 while (i < source.size())
25123 {
25124 // add operations in reverse order to avoid invalid
25125 // indices
25126 result.insert(result.begin() + end_index, object(
25127 {
25128 {"op", "remove"},
25129 {"path", detail::concat<string_t>(path, '/', detail::to_string<string_t>(i))}
25130 }));
25131 ++i;
25132 }
25133
25134 // add other remaining elements
25135 while (i < target.size())
25136 {
25137 result.push_back(
25138 {
25139 {"op", "add"},
25140 {"path", detail::concat<string_t>(path, "/-")},
25141 {"value", target[i]}
25142 });
25143 ++i;
25144 }
25145
25146 break;
25147 }
25148
25149 case value_t::object:
25150 {
25151 // first pass: traverse this object's elements
25152 for (auto it = source.cbegin(); it != source.cend(); ++it)
25153 {
25154 // escape the key name to be used in a JSON patch
25155 const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
25156
25157 if (target.find(it.key()) != target.end())
25158 {
25159 // recursive call to compare object values at key it
25160 auto temp_diff = diff(it.value(), target[it.key()], path_key);
25161 result.insert(result.end(), temp_diff.begin(), temp_diff.end());
25162 }
25163 else
25164 {
25165 // found a key that is not in o -> remove it
25166 result.push_back(object(
25167 {
25168 {"op", "remove"}, {"path", path_key}
25169 }));
25170 }
25171 }
25172
25173 // second pass: traverse other object's elements
25174 for (auto it = target.cbegin(); it != target.cend(); ++it)
25175 {
25176 if (source.find(it.key()) == source.end())
25177 {
25178 // found a key that is not in this -> add it
25179 const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
25180 result.push_back(
25181 {
25182 {"op", "add"}, {"path", path_key},
25183 {"value", it.value()}
25184 });
25185 }
25186 }
25187
25188 break;
25189 }
25190
25191 case value_t::null:
25192 case value_t::string:
25193 case value_t::boolean:
25194 case value_t::number_integer:
25195 case value_t::number_unsigned:
25196 case value_t::number_float:
25197 case value_t::binary:
25198 case value_t::discarded:
25199 default:
25200 {
25201 // both primitive type: replace value
25202 result.push_back(
25203 {
25204 {"op", "replace"}, {"path", path}, {"value", target}
25205 });
25206 break;
25207 }
25208 }
25209
25210 return result;
25211 }
25212 /// @}
25213
25214 ////////////////////////////////
25215 // JSON Merge Patch functions //
25216 ////////////////////////////////
25217
25218 /// @name JSON Merge Patch functions
25219 /// @{
25220
25221 /// @brief applies a JSON Merge Patch
25222 /// @sa https://json.nlohmann.me/api/basic_json/merge_patch/
25223 void merge_patch(const basic_json& apply_patch)
25224 {
25225 if (apply_patch.is_object())
25226 {
25227 if (!is_object())
25228 {
25229 *this = object();
25230 }
25231 for (auto it = apply_patch.begin(); it != apply_patch.end(); ++it)
25232 {
25233 if (it.value().is_null())
25234 {
25235 erase(it.key());
25236 }
25237 else
25238 {
25239 operator[](it.key()).merge_patch(it.value());
25240 }
25241 }
25242 }
25243 else
25244 {
25245 *this = apply_patch;
25246 }
25247 }
25248
25249 /// @}
25250 };
25251
25252 /// @brief user-defined to_string function for JSON values
25253 /// @sa https://json.nlohmann.me/api/basic_json/to_string/
25254 NLOHMANN_BASIC_JSON_TPL_DECLARATION
25255 std::string to_string(const NLOHMANN_BASIC_JSON_TPL& j)
25256 {
25257 return j.dump();
25258 }
25259
25260 inline namespace literals
25261 {
25262 inline namespace json_literals
25263 {
25264
25265 /// @brief user-defined string literal for JSON values
25266 /// @sa https://json.nlohmann.me/api/basic_json/operator_literal_json/
25267 JSON_HEDLEY_NON_NULL(1)
25268 #if !defined(JSON_HEDLEY_GCC_VERSION) || JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0)
25269 inline nlohmann::json operator ""_json(const char* s, std::size_t n)
25270 #else
25271 inline nlohmann::json operator "" _json(const char* s, std::size_t n)
25272 #endif
25273 {
25274 return nlohmann::json::parse(s, s + n);
25275 }
25276
25277 /// @brief user-defined string literal for JSON pointer
25278 /// @sa https://json.nlohmann.me/api/basic_json/operator_literal_json_pointer/
25279 JSON_HEDLEY_NON_NULL(1)
25280 #if !defined(JSON_HEDLEY_GCC_VERSION) || JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0)
25281 inline nlohmann::json::json_pointer operator ""_json_pointer(const char* s, std::size_t n)
25282 #else
25283 inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std::size_t n)
25284 #endif
25285 {
25286 return nlohmann::json::json_pointer(std::string(s, n));
25287 }
25288
25289 } // namespace json_literals
25290 } // namespace literals
25291 NLOHMANN_JSON_NAMESPACE_END
25292
25293 ///////////////////////
25294 // nonmember support //
25295 ///////////////////////
25296
25297 namespace std // NOLINT(cert-dcl58-cpp)
25298 {
25299
25300 /// @brief hash value for JSON objects
25301 /// @sa https://json.nlohmann.me/api/basic_json/std_hash/
25302 NLOHMANN_BASIC_JSON_TPL_DECLARATION
25303 struct hash<nlohmann::NLOHMANN_BASIC_JSON_TPL> // NOLINT(cert-dcl58-cpp)
25304 {
25305 std::size_t operator()(const nlohmann::NLOHMANN_BASIC_JSON_TPL& j) const
25306 {
25307 return nlohmann::detail::hash(j);
25308 }
25309 };
25310
25311 // specialization for std::less<value_t>
25312 template<>
25313 struct less< ::nlohmann::detail::value_t> // do not remove the space after '<', see https://github.com/nlohmann/json/pull/679
25314 {
25315 /*!
25316 @brief compare two value_t enum values
25317 @since version 3.0.0
25318 */
25319 bool operator()(::nlohmann::detail::value_t lhs,
25320 ::nlohmann::detail::value_t rhs) const noexcept
25321 {
25322 #if JSON_HAS_THREE_WAY_COMPARISON
25323 return std::is_lt(lhs <=> rhs); // *NOPAD*
25324 #else
25325 return ::nlohmann::detail::operator<(lhs, rhs);
25326 #endif
25327 }
25328 };
25329
25330 // C++20 prohibit function specialization in the std namespace.
25331 #ifndef JSON_HAS_CPP_20
25332
25333 /// @brief exchanges the values of two JSON objects
25334 /// @sa https://json.nlohmann.me/api/basic_json/std_swap/
25335 NLOHMANN_BASIC_JSON_TPL_DECLARATION
25336 inline void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL& j1, nlohmann::NLOHMANN_BASIC_JSON_TPL& j2) noexcept( // NOLINT(readability-inconsistent-declaration-parameter-name, cert-dcl58-cpp)
25337 is_nothrow_move_constructible<nlohmann::NLOHMANN_BASIC_JSON_TPL>::value&& // NOLINT(misc-redundant-expression,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
25338 is_nothrow_move_assignable<nlohmann::NLOHMANN_BASIC_JSON_TPL>::value)
25339 {
25340 j1.swap(j2);
25341 }
25342
25343 #endif
25344
25345 } // namespace std
25346
25347 #if JSON_USE_GLOBAL_UDLS
25348 #if !defined(JSON_HEDLEY_GCC_VERSION) || JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0)
25349 using nlohmann::literals::json_literals::operator ""_json; // NOLINT(misc-unused-using-decls,google-global-names-in-headers)
25350 using nlohmann::literals::json_literals::operator ""_json_pointer; //NOLINT(misc-unused-using-decls,google-global-names-in-headers)
25351 #else
25352 using nlohmann::literals::json_literals::operator "" _json; // NOLINT(misc-unused-using-decls,google-global-names-in-headers)
25353 using nlohmann::literals::json_literals::operator "" _json_pointer; //NOLINT(misc-unused-using-decls,google-global-names-in-headers)
25354 #endif
25355 #endif
25356
25357 // #include <nlohmann/detail/macro_unscope.hpp>
25358 // __ _____ _____ _____
25359 // __| | __| | | | JSON for Modern C++
25360 // | | |__ | | | | | | version 3.12.0
25361 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
25362 //
25363 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
25364 // SPDX-License-Identifier: MIT
25365
25366
25367
25368 // restore clang diagnostic settings
25369 #if defined(__clang__)
25370 #pragma clang diagnostic pop
25371 #endif
25372
25373 // clean up
25374 #undef JSON_ASSERT
25375 #undef JSON_INTERNAL_CATCH
25376 #undef JSON_THROW
25377 #undef JSON_PRIVATE_UNLESS_TESTED
25378 #undef NLOHMANN_BASIC_JSON_TPL_DECLARATION
25379 #undef NLOHMANN_BASIC_JSON_TPL
25380 #undef JSON_EXPLICIT
25381 #undef NLOHMANN_CAN_CALL_STD_FUNC_IMPL
25382 #undef JSON_INLINE_VARIABLE
25383 #undef JSON_NO_UNIQUE_ADDRESS
25384 #undef JSON_DISABLE_ENUM_SERIALIZATION
25385 #undef JSON_USE_GLOBAL_UDLS
25386
25387 #ifndef JSON_TEST_KEEP_MACROS
25388 #undef JSON_CATCH
25389 #undef JSON_TRY
25390 #undef JSON_HAS_CPP_11
25391 #undef JSON_HAS_CPP_14
25392 #undef JSON_HAS_CPP_17
25393 #undef JSON_HAS_CPP_20
25394 #undef JSON_HAS_CPP_23
25395 #undef JSON_HAS_FILESYSTEM
25396 #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
25397 #undef JSON_HAS_THREE_WAY_COMPARISON
25398 #undef JSON_HAS_RANGES
25399 #undef JSON_HAS_STATIC_RTTI
25400 #undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
25401 #endif
25402
25403 // #include <nlohmann/thirdparty/hedley/hedley_undef.hpp>
25404 // __ _____ _____ _____
25405 // __| | __| | | | JSON for Modern C++
25406 // | | |__ | | | | | | version 3.12.0
25407 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
25408 //
25409 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
25410 // SPDX-License-Identifier: MIT
25411
25412
25413
25414 #undef JSON_HEDLEY_ALWAYS_INLINE
25415 #undef JSON_HEDLEY_ARM_VERSION
25416 #undef JSON_HEDLEY_ARM_VERSION_CHECK
25417 #undef JSON_HEDLEY_ARRAY_PARAM
25418 #undef JSON_HEDLEY_ASSUME
25419 #undef JSON_HEDLEY_BEGIN_C_DECLS
25420 #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE
25421 #undef JSON_HEDLEY_CLANG_HAS_BUILTIN
25422 #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE
25423 #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE
25424 #undef JSON_HEDLEY_CLANG_HAS_EXTENSION
25425 #undef JSON_HEDLEY_CLANG_HAS_FEATURE
25426 #undef JSON_HEDLEY_CLANG_HAS_WARNING
25427 #undef JSON_HEDLEY_COMPCERT_VERSION
25428 #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK
25429 #undef JSON_HEDLEY_CONCAT
25430 #undef JSON_HEDLEY_CONCAT3
25431 #undef JSON_HEDLEY_CONCAT3_EX
25432 #undef JSON_HEDLEY_CONCAT_EX
25433 #undef JSON_HEDLEY_CONST
25434 #undef JSON_HEDLEY_CONSTEXPR
25435 #undef JSON_HEDLEY_CONST_CAST
25436 #undef JSON_HEDLEY_CPP_CAST
25437 #undef JSON_HEDLEY_CRAY_VERSION
25438 #undef JSON_HEDLEY_CRAY_VERSION_CHECK
25439 #undef JSON_HEDLEY_C_DECL
25440 #undef JSON_HEDLEY_DEPRECATED
25441 #undef JSON_HEDLEY_DEPRECATED_FOR
25442 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
25443 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_
25444 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
25445 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
25446 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
25447 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
25448 #undef JSON_HEDLEY_DIAGNOSTIC_POP
25449 #undef JSON_HEDLEY_DIAGNOSTIC_PUSH
25450 #undef JSON_HEDLEY_DMC_VERSION
25451 #undef JSON_HEDLEY_DMC_VERSION_CHECK
25452 #undef JSON_HEDLEY_EMPTY_BASES
25453 #undef JSON_HEDLEY_EMSCRIPTEN_VERSION
25454 #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK
25455 #undef JSON_HEDLEY_END_C_DECLS
25456 #undef JSON_HEDLEY_FLAGS
25457 #undef JSON_HEDLEY_FLAGS_CAST
25458 #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE
25459 #undef JSON_HEDLEY_GCC_HAS_BUILTIN
25460 #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE
25461 #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE
25462 #undef JSON_HEDLEY_GCC_HAS_EXTENSION
25463 #undef JSON_HEDLEY_GCC_HAS_FEATURE
25464 #undef JSON_HEDLEY_GCC_HAS_WARNING
25465 #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK
25466 #undef JSON_HEDLEY_GCC_VERSION
25467 #undef JSON_HEDLEY_GCC_VERSION_CHECK
25468 #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE
25469 #undef JSON_HEDLEY_GNUC_HAS_BUILTIN
25470 #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE
25471 #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE
25472 #undef JSON_HEDLEY_GNUC_HAS_EXTENSION
25473 #undef JSON_HEDLEY_GNUC_HAS_FEATURE
25474 #undef JSON_HEDLEY_GNUC_HAS_WARNING
25475 #undef JSON_HEDLEY_GNUC_VERSION
25476 #undef JSON_HEDLEY_GNUC_VERSION_CHECK
25477 #undef JSON_HEDLEY_HAS_ATTRIBUTE
25478 #undef JSON_HEDLEY_HAS_BUILTIN
25479 #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE
25480 #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS
25481 #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE
25482 #undef JSON_HEDLEY_HAS_EXTENSION
25483 #undef JSON_HEDLEY_HAS_FEATURE
25484 #undef JSON_HEDLEY_HAS_WARNING
25485 #undef JSON_HEDLEY_IAR_VERSION
25486 #undef JSON_HEDLEY_IAR_VERSION_CHECK
25487 #undef JSON_HEDLEY_IBM_VERSION
25488 #undef JSON_HEDLEY_IBM_VERSION_CHECK
25489 #undef JSON_HEDLEY_IMPORT
25490 #undef JSON_HEDLEY_INLINE
25491 #undef JSON_HEDLEY_INTEL_CL_VERSION
25492 #undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK
25493 #undef JSON_HEDLEY_INTEL_VERSION
25494 #undef JSON_HEDLEY_INTEL_VERSION_CHECK
25495 #undef JSON_HEDLEY_IS_CONSTANT
25496 #undef JSON_HEDLEY_IS_CONSTEXPR_
25497 #undef JSON_HEDLEY_LIKELY
25498 #undef JSON_HEDLEY_MALLOC
25499 #undef JSON_HEDLEY_MCST_LCC_VERSION
25500 #undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK
25501 #undef JSON_HEDLEY_MESSAGE
25502 #undef JSON_HEDLEY_MSVC_VERSION
25503 #undef JSON_HEDLEY_MSVC_VERSION_CHECK
25504 #undef JSON_HEDLEY_NEVER_INLINE
25505 #undef JSON_HEDLEY_NON_NULL
25506 #undef JSON_HEDLEY_NO_ESCAPE
25507 #undef JSON_HEDLEY_NO_RETURN
25508 #undef JSON_HEDLEY_NO_THROW
25509 #undef JSON_HEDLEY_NULL
25510 #undef JSON_HEDLEY_PELLES_VERSION
25511 #undef JSON_HEDLEY_PELLES_VERSION_CHECK
25512 #undef JSON_HEDLEY_PGI_VERSION
25513 #undef JSON_HEDLEY_PGI_VERSION_CHECK
25514 #undef JSON_HEDLEY_PREDICT
25515 #undef JSON_HEDLEY_PRINTF_FORMAT
25516 #undef JSON_HEDLEY_PRIVATE
25517 #undef JSON_HEDLEY_PUBLIC
25518 #undef JSON_HEDLEY_PURE
25519 #undef JSON_HEDLEY_REINTERPRET_CAST
25520 #undef JSON_HEDLEY_REQUIRE
25521 #undef JSON_HEDLEY_REQUIRE_CONSTEXPR
25522 #undef JSON_HEDLEY_REQUIRE_MSG
25523 #undef JSON_HEDLEY_RESTRICT
25524 #undef JSON_HEDLEY_RETURNS_NON_NULL
25525 #undef JSON_HEDLEY_SENTINEL
25526 #undef JSON_HEDLEY_STATIC_ASSERT
25527 #undef JSON_HEDLEY_STATIC_CAST
25528 #undef JSON_HEDLEY_STRINGIFY
25529 #undef JSON_HEDLEY_STRINGIFY_EX
25530 #undef JSON_HEDLEY_SUNPRO_VERSION
25531 #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK
25532 #undef JSON_HEDLEY_TINYC_VERSION
25533 #undef JSON_HEDLEY_TINYC_VERSION_CHECK
25534 #undef JSON_HEDLEY_TI_ARMCL_VERSION
25535 #undef JSON_HEDLEY_TI_ARMCL_VERSION_CHECK
25536 #undef JSON_HEDLEY_TI_CL2000_VERSION
25537 #undef JSON_HEDLEY_TI_CL2000_VERSION_CHECK
25538 #undef JSON_HEDLEY_TI_CL430_VERSION
25539 #undef JSON_HEDLEY_TI_CL430_VERSION_CHECK
25540 #undef JSON_HEDLEY_TI_CL6X_VERSION
25541 #undef JSON_HEDLEY_TI_CL6X_VERSION_CHECK
25542 #undef JSON_HEDLEY_TI_CL7X_VERSION
25543 #undef JSON_HEDLEY_TI_CL7X_VERSION_CHECK
25544 #undef JSON_HEDLEY_TI_CLPRU_VERSION
25545 #undef JSON_HEDLEY_TI_CLPRU_VERSION_CHECK
25546 #undef JSON_HEDLEY_TI_VERSION
25547 #undef JSON_HEDLEY_TI_VERSION_CHECK
25548 #undef JSON_HEDLEY_UNAVAILABLE
25549 #undef JSON_HEDLEY_UNLIKELY
25550 #undef JSON_HEDLEY_UNPREDICTABLE
25551 #undef JSON_HEDLEY_UNREACHABLE
25552 #undef JSON_HEDLEY_UNREACHABLE_RETURN
25553 #undef JSON_HEDLEY_VERSION
25554 #undef JSON_HEDLEY_VERSION_DECODE_MAJOR
25555 #undef JSON_HEDLEY_VERSION_DECODE_MINOR
25556 #undef JSON_HEDLEY_VERSION_DECODE_REVISION
25557 #undef JSON_HEDLEY_VERSION_ENCODE
25558 #undef JSON_HEDLEY_WARNING
25559 #undef JSON_HEDLEY_WARN_UNUSED_RESULT
25560 #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG
25561 #undef JSON_HEDLEY_FALL_THROUGH
25562
25563
25564
25565 #endif // INCLUDE_NLOHMANN_JSON_HPP_