Debian Codegraph @

single_include/nlohmann/json.hpp

raw symbols ◀ ▶
Source reconstructed at patch state 1 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
3371 // #include <nlohmann/detail/iterators/iterator_traits.hpp>
3372 // __ _____ _____ _____
3373 // __| | __| | | | JSON for Modern C++
3374 // | | |__ | | | | | | version 3.12.0
3375 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
3376 //
3377 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
3378 // SPDX-License-Identifier: MIT
3379
3380
3381
3382 #include <iterator> // random_access_iterator_tag
3383
3384 // #include <nlohmann/detail/abi_macros.hpp>
3385
3386 // #include <nlohmann/detail/meta/void_t.hpp>
3387
3388 // #include <nlohmann/detail/meta/cpp_future.hpp>
3389
3390
3391 NLOHMANN_JSON_NAMESPACE_BEGIN
3392 namespace detail
3393 {
3394
3395 template<typename It, typename = void>
3396 struct iterator_types {};
3397
3398 template<typename It>
3399 struct iterator_types <
3400 It,
3401 void_t<typename It::difference_type, typename It::value_type, typename It::pointer,
3402 typename It::reference, typename It::iterator_category >>
3403 {
3404 using difference_type = typename It::difference_type;
3405 using value_type = typename It::value_type;
3406 using pointer = typename It::pointer;
3407 using reference = typename It::reference;
3408 using iterator_category = typename It::iterator_category;
3409 };
3410
3411 // This is required as some compilers implement std::iterator_traits in a way that
3412 // doesn't work with SFINAE. See https://github.com/nlohmann/json/issues/1341.
3413 template<typename T, typename = void>
3414 struct iterator_traits
3415 {
3416 };
3417
3418 template<typename T>
3419 struct iterator_traits < T, enable_if_t < !std::is_pointer<T>::value >>
3420 : iterator_types<T>
3421 {
3422 };
3423
3424 template<typename T>
3425 struct iterator_traits<T*, enable_if_t<std::is_object<T>::value>>
3426 {
3427 using iterator_category = std::random_access_iterator_tag;
3428 using value_type = T;
3429 using difference_type = ptrdiff_t;
3430 using pointer = T*;
3431 using reference = T&;
3432 };
3433
3434 } // namespace detail
3435 NLOHMANN_JSON_NAMESPACE_END
3436
3437 // #include <nlohmann/detail/macro_scope.hpp>
3438
3439 // #include <nlohmann/detail/meta/call_std/begin.hpp>
3440 // __ _____ _____ _____
3441 // __| | __| | | | JSON for Modern C++
3442 // | | |__ | | | | | | version 3.12.0
3443 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
3444 //
3445 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
3446 // SPDX-License-Identifier: MIT
3447
3448
3449
3450 // #include <nlohmann/detail/macro_scope.hpp>
3451
3452
3453 NLOHMANN_JSON_NAMESPACE_BEGIN
3454
3455 NLOHMANN_CAN_CALL_STD_FUNC_IMPL(begin);
3456
3457 NLOHMANN_JSON_NAMESPACE_END
3458
3459 // #include <nlohmann/detail/meta/call_std/end.hpp>
3460 // __ _____ _____ _____
3461 // __| | __| | | | JSON for Modern C++
3462 // | | |__ | | | | | | version 3.12.0
3463 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
3464 //
3465 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
3466 // SPDX-License-Identifier: MIT
3467
3468
3469
3470 // #include <nlohmann/detail/macro_scope.hpp>
3471
3472
3473 NLOHMANN_JSON_NAMESPACE_BEGIN
3474
3475 NLOHMANN_CAN_CALL_STD_FUNC_IMPL(end);
3476
3477 NLOHMANN_JSON_NAMESPACE_END
3478
3479 // #include <nlohmann/detail/meta/cpp_future.hpp>
3480
3481 // #include <nlohmann/detail/meta/detected.hpp>
3482
3483 // #include <nlohmann/json_fwd.hpp>
3484 // __ _____ _____ _____
3485 // __| | __| | | | JSON for Modern C++
3486 // | | |__ | | | | | | version 3.12.0
3487 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
3488 //
3489 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
3490 // SPDX-License-Identifier: MIT
3491
3492 #ifndef INCLUDE_NLOHMANN_JSON_FWD_HPP_
3493 #define INCLUDE_NLOHMANN_JSON_FWD_HPP_
3494
3495 #include <cstdint> // int64_t, uint64_t
3496 #include <map> // map
3497 #include <memory> // allocator
3498 #include <string> // string
3499 #include <vector> // vector
3500
3501 // #include <nlohmann/detail/abi_macros.hpp>
3502
3503
3504 /*!
3505 @brief namespace for Niels Lohmann
3506 @see https://github.com/nlohmann
3507 @since version 1.0.0
3508 */
3509 NLOHMANN_JSON_NAMESPACE_BEGIN
3510
3511 /*!
3512 @brief default JSONSerializer template argument
3513
3514 This serializer ignores the template arguments and uses ADL
3515 ([argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl))
3516 for serialization.
3517 */
3518 template<typename T = void, typename SFINAE = void>
3519 struct adl_serializer;
3520
3521 /// a class to store JSON values
3522 /// @sa https://json.nlohmann.me/api/basic_json/
3523 template<template<typename U, typename V, typename... Args> class ObjectType =
3524 std::map,
3525 template<typename U, typename... Args> class ArrayType = std::vector,
3526 class StringType = std::string, class BooleanType = bool,
3527 class NumberIntegerType = std::int64_t,
3528 class NumberUnsignedType = std::uint64_t,
3529 class NumberFloatType = double,
3530 template<typename U> class AllocatorType = std::allocator,
3531 template<typename T, typename SFINAE = void> class JSONSerializer =
3532 adl_serializer,
3533 class BinaryType = std::vector<std::uint8_t>, // cppcheck-suppress syntaxError
3534 class CustomBaseClass = void>
3535 class basic_json;
3536
3537 /// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document
3538 /// @sa https://json.nlohmann.me/api/json_pointer/
3539 template<typename RefStringType>
3540 class json_pointer;
3541
3542 /*!
3543 @brief default specialization
3544 @sa https://json.nlohmann.me/api/json/
3545 */
3546 using json = basic_json<>;
3547
3548 /// @brief a minimal map-like container that preserves insertion order
3549 /// @sa https://json.nlohmann.me/api/ordered_map/
3550 template<class Key, class T, class IgnoredLess, class Allocator>
3551 struct ordered_map;
3552
3553 /// @brief specialization that maintains the insertion order of object keys
3554 /// @sa https://json.nlohmann.me/api/ordered_json/
3555 using ordered_json = basic_json<nlohmann::ordered_map>;
3556
3557 NLOHMANN_JSON_NAMESPACE_END
3558
3559 #endif // INCLUDE_NLOHMANN_JSON_FWD_HPP_
3560
3561
3562 NLOHMANN_JSON_NAMESPACE_BEGIN
3563 /*!
3564 @brief detail namespace with internal helper functions
3565
3566 This namespace collects functions that should not be exposed,
3567 implementations of some @ref basic_json methods, and meta-programming helpers.
3568
3569 @since version 2.1.0
3570 */
3571 namespace detail
3572 {
3573
3574 /////////////
3575 // helpers //
3576 /////////////
3577
3578 // Note to maintainers:
3579 //
3580 // Every trait in this file expects a non CV-qualified type.
3581 // The only exceptions are in the 'aliases for detected' section
3582 // (i.e. those of the form: decltype(T::member_function(std::declval<T>())))
3583 //
3584 // In this case, T has to be properly CV-qualified to constraint the function arguments
3585 // (e.g. to_json(BasicJsonType&, const T&))
3586
3587 template<typename> struct is_basic_json : std::false_type {};
3588
3589 NLOHMANN_BASIC_JSON_TPL_DECLARATION
3590 struct is_basic_json<NLOHMANN_BASIC_JSON_TPL> : std::true_type {};
3591
3592 // used by exceptions create() member functions
3593 // true_type for pointer to possibly cv-qualified basic_json or std::nullptr_t
3594 // false_type otherwise
3595 template<typename BasicJsonContext>
3596 struct is_basic_json_context :
3597 std::integral_constant < bool,
3598 is_basic_json<typename std::remove_cv<typename std::remove_pointer<BasicJsonContext>::type>::type>::value
3599 || std::is_same<BasicJsonContext, std::nullptr_t>::value >
3600 {};
3601
3602 //////////////////////
3603 // json_ref helpers //
3604 //////////////////////
3605
3606 template<typename>
3607 class json_ref;
3608
3609 template<typename>
3610 struct is_json_ref : std::false_type {};
3611
3612 template<typename T>
3613 struct is_json_ref<json_ref<T>> : std::true_type {};
3614
3615 //////////////////////////
3616 // aliases for detected //
3617 //////////////////////////
3618
3619 template<typename T>
3620 using mapped_type_t = typename T::mapped_type;
3621
3622 template<typename T>
3623 using key_type_t = typename T::key_type;
3624
3625 template<typename T>
3626 using value_type_t = typename T::value_type;
3627
3628 template<typename T>
3629 using difference_type_t = typename T::difference_type;
3630
3631 template<typename T>
3632 using pointer_t = typename T::pointer;
3633
3634 template<typename T>
3635 using reference_t = typename T::reference;
3636
3637 template<typename T>
3638 using iterator_category_t = typename T::iterator_category;
3639
3640 template<typename T, typename... Args>
3641 using to_json_function = decltype(T::to_json(std::declval<Args>()...));
3642
3643 template<typename T, typename... Args>
3644 using from_json_function = decltype(T::from_json(std::declval<Args>()...));
3645
3646 template<typename T, typename U>
3647 using get_template_function = decltype(std::declval<T>().template get<U>());
3648
3649 // trait checking if JSONSerializer<T>::from_json(json const&, udt&) exists
3650 template<typename BasicJsonType, typename T, typename = void>
3651 struct has_from_json : std::false_type {};
3652
3653 // trait checking if j.get<T> is valid
3654 // use this trait instead of std::is_constructible or std::is_convertible,
3655 // both rely on, or make use of implicit conversions, and thus fail when T
3656 // has several constructors/operator= (see https://github.com/nlohmann/json/issues/958)
3657 template <typename BasicJsonType, typename T>
3658 struct is_getable
3659 {
3660 static constexpr bool value = is_detected<get_template_function, const BasicJsonType&, T>::value;
3661 };
3662
3663 template<typename BasicJsonType, typename T>
3664 struct has_from_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
3665 {
3666 using serializer = typename BasicJsonType::template json_serializer<T, void>;
3667
3668 static constexpr bool value =
3669 is_detected_exact<void, from_json_function, serializer,
3670 const BasicJsonType&, T&>::value;
3671 };
3672
3673 // This trait checks if JSONSerializer<T>::from_json(json const&) exists
3674 // this overload is used for non-default-constructible user-defined-types
3675 template<typename BasicJsonType, typename T, typename = void>
3676 struct has_non_default_from_json : std::false_type {};
3677
3678 template<typename BasicJsonType, typename T>
3679 struct has_non_default_from_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
3680 {
3681 using serializer = typename BasicJsonType::template json_serializer<T, void>;
3682
3683 static constexpr bool value =
3684 is_detected_exact<T, from_json_function, serializer,
3685 const BasicJsonType&>::value;
3686 };
3687
3688 // This trait checks if BasicJsonType::json_serializer<T>::to_json exists
3689 // Do not evaluate the trait when T is a basic_json type, to avoid template instantiation infinite recursion.
3690 template<typename BasicJsonType, typename T, typename = void>
3691 struct has_to_json : std::false_type {};
3692
3693 template<typename BasicJsonType, typename T>
3694 struct has_to_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
3695 {
3696 using serializer = typename BasicJsonType::template json_serializer<T, void>;
3697
3698 static constexpr bool value =
3699 is_detected_exact<void, to_json_function, serializer, BasicJsonType&,
3700 T>::value;
3701 };
3702
3703 template<typename T>
3704 using detect_key_compare = typename T::key_compare;
3705
3706 template<typename T>
3707 struct has_key_compare : std::integral_constant<bool, is_detected<detect_key_compare, T>::value> {};
3708
3709 // obtains the actual object key comparator
3710 template<typename BasicJsonType>
3711 struct actual_object_comparator
3712 {
3713 using object_t = typename BasicJsonType::object_t;
3714 using object_comparator_t = typename BasicJsonType::default_object_comparator_t;
3715 using type = typename std::conditional < has_key_compare<object_t>::value,
3716 typename object_t::key_compare, object_comparator_t>::type;
3717 };
3718
3719 template<typename BasicJsonType>
3720 using actual_object_comparator_t = typename actual_object_comparator<BasicJsonType>::type;
3721
3722 /////////////////
3723 // char_traits //
3724 /////////////////
3725
3726 // Primary template of char_traits calls std char_traits
3727 template<typename T>
3728 struct char_traits : std::char_traits<T>
3729 {};
3730
3731 // Explicitly define char traits for unsigned char since it is not standard
3732 template<>
3733 struct char_traits<unsigned char> : std::char_traits<char>
3734 {
3735 using char_type = unsigned char;
3736 using int_type = uint64_t;
3737
3738 // Redefine to_int_type function
3739 static int_type to_int_type(char_type c) noexcept
3740 {
3741 return static_cast<int_type>(c);
3742 }
3743
3744 static char_type to_char_type(int_type i) noexcept
3745 {
3746 return static_cast<char_type>(i);
3747 }
3748
3749 static constexpr int_type eof() noexcept
3750 {
3751 return static_cast<int_type>(std::char_traits<char>::eof());
3752 }
3753 };
3754
3755 // Explicitly define char traits for signed char since it is not standard
3756 template<>
3757 struct char_traits<signed char> : std::char_traits<char>
3758 {
3759 using char_type = signed char;
3760 using int_type = uint64_t;
3761
3762 // Redefine to_int_type function
3763 static int_type to_int_type(char_type c) noexcept
3764 {
3765 return static_cast<int_type>(c);
3766 }
3767
3768 static char_type to_char_type(int_type i) noexcept
3769 {
3770 return static_cast<char_type>(i);
3771 }
3772
3773 static constexpr int_type eof() noexcept
3774 {
3775 return static_cast<int_type>(std::char_traits<char>::eof());
3776 }
3777 };
3778
3779 ///////////////////
3780 // is_ functions //
3781 ///////////////////
3782
3783 // https://en.cppreference.com/w/cpp/types/conjunction
3784 template<class...> struct conjunction : std::true_type { };
3785 template<class B> struct conjunction<B> : B { };
3786 template<class B, class... Bn>
3787 struct conjunction<B, Bn...>
3788 : std::conditional<static_cast<bool>(B::value), conjunction<Bn...>, B>::type {};
3789
3790 // https://en.cppreference.com/w/cpp/types/negation
3791 template<class B> struct negation : std::integral_constant < bool, !B::value > { };
3792
3793 // Reimplementation of is_constructible and is_default_constructible, due to them being broken for
3794 // std::pair and std::tuple until LWG 2367 fix (see https://cplusplus.github.io/LWG/lwg-defects.html#2367).
3795 // This causes compile errors in e.g. clang 3.5 or gcc 4.9.
3796 template <typename T>
3797 struct is_default_constructible : std::is_default_constructible<T> {};
3798
3799 template <typename T1, typename T2>
3800 struct is_default_constructible<std::pair<T1, T2>>
3801 : conjunction<is_default_constructible<T1>, is_default_constructible<T2>> {};
3802
3803 template <typename T1, typename T2>
3804 struct is_default_constructible<const std::pair<T1, T2>>
3805 : conjunction<is_default_constructible<T1>, is_default_constructible<T2>> {};
3806
3807 template <typename... Ts>
3808 struct is_default_constructible<std::tuple<Ts...>>
3809 : conjunction<is_default_constructible<Ts>...> {};
3810
3811 template <typename... Ts>
3812 struct is_default_constructible<const std::tuple<Ts...>>
3813 : conjunction<is_default_constructible<Ts>...> {};
3814
3815 template <typename T, typename... Args>
3816 struct is_constructible : std::is_constructible<T, Args...> {};
3817
3818 template <typename T1, typename T2>
3819 struct is_constructible<std::pair<T1, T2>> : is_default_constructible<std::pair<T1, T2>> {};
3820
3821 template <typename T1, typename T2>
3822 struct is_constructible<const std::pair<T1, T2>> : is_default_constructible<const std::pair<T1, T2>> {};
3823
3824 template <typename... Ts>
3825 struct is_constructible<std::tuple<Ts...>> : is_default_constructible<std::tuple<Ts...>> {};
3826
3827 template <typename... Ts>
3828 struct is_constructible<const std::tuple<Ts...>> : is_default_constructible<const std::tuple<Ts...>> {};
3829
3830 template<typename T, typename = void>
3831 struct is_iterator_traits : std::false_type {};
3832
3833 template<typename T>
3834 struct is_iterator_traits<iterator_traits<T>>
3835 {
3836 private:
3837 using traits = iterator_traits<T>;
3838
3839 public:
3840 static constexpr auto value =
3841 is_detected<value_type_t, traits>::value &&
3842 is_detected<difference_type_t, traits>::value &&
3843 is_detected<pointer_t, traits>::value &&
3844 is_detected<iterator_category_t, traits>::value &&
3845 is_detected<reference_t, traits>::value;
3846 };
3847
3848 template<typename T>
3849 struct is_range
3850 {
3851 private:
3852 using t_ref = typename std::add_lvalue_reference<T>::type;
3853
3854 using iterator = detected_t<result_of_begin, t_ref>;
3855 using sentinel = detected_t<result_of_end, t_ref>;
3856
3857 // to be 100% correct, it should use https://en.cppreference.com/w/cpp/iterator/input_or_output_iterator
3858 // and https://en.cppreference.com/w/cpp/iterator/sentinel_for
3859 // but reimplementing these would be too much work, as a lot of other concepts are used underneath
3860 static constexpr auto is_iterator_begin =
3861 is_iterator_traits<iterator_traits<iterator>>::value;
3862
3863 public:
3864 static constexpr bool value = !std::is_same<iterator, nonesuch>::value && !std::is_same<sentinel, nonesuch>::value && is_iterator_begin;
3865 };
3866
3867 template<typename R>
3868 using iterator_t = enable_if_t<is_range<R>::value, result_of_begin<decltype(std::declval<R&>())>>;
3869
3870 template<typename T>
3871 using range_value_t = value_type_t<iterator_traits<iterator_t<T>>>;
3872
3873 // The following implementation of is_complete_type is taken from
3874 // https://blogs.msdn.microsoft.com/vcblog/2015/12/02/partial-support-for-expression-sfinae-in-vs-2015-update-1/
3875 // and is written by Xiang Fan who agreed to using it in this library.
3876
3877 template<typename T, typename = void>
3878 struct is_complete_type : std::false_type {};
3879
3880 template<typename T>
3881 struct is_complete_type<T, decltype(void(sizeof(T)))> : std::true_type {};
3882
3883 template<typename BasicJsonType, typename CompatibleObjectType,
3884 typename = void>
3885 struct is_compatible_object_type_impl : std::false_type {};
3886
3887 template<typename BasicJsonType, typename CompatibleObjectType>
3888 struct is_compatible_object_type_impl <
3889 BasicJsonType, CompatibleObjectType,
3890 enable_if_t < is_detected<mapped_type_t, CompatibleObjectType>::value&&
3891 is_detected<key_type_t, CompatibleObjectType>::value >>
3892 {
3893 using object_t = typename BasicJsonType::object_t;
3894
3895 // macOS's is_constructible does not play well with nonesuch...
3896 static constexpr bool value =
3897 is_constructible<typename object_t::key_type,
3898 typename CompatibleObjectType::key_type>::value &&
3899 is_constructible<typename object_t::mapped_type,
3900 typename CompatibleObjectType::mapped_type>::value;
3901 };
3902
3903 template<typename BasicJsonType, typename CompatibleObjectType>
3904 struct is_compatible_object_type
3905 : is_compatible_object_type_impl<BasicJsonType, CompatibleObjectType> {};
3906
3907 template<typename BasicJsonType, typename ConstructibleObjectType,
3908 typename = void>
3909 struct is_constructible_object_type_impl : std::false_type {};
3910
3911 template<typename BasicJsonType, typename ConstructibleObjectType>
3912 struct is_constructible_object_type_impl <
3913 BasicJsonType, ConstructibleObjectType,
3914 enable_if_t < is_detected<mapped_type_t, ConstructibleObjectType>::value&&
3915 is_detected<key_type_t, ConstructibleObjectType>::value >>
3916 {
3917 using object_t = typename BasicJsonType::object_t;
3918
3919 static constexpr bool value =
3920 (is_default_constructible<ConstructibleObjectType>::value &&
3921 (std::is_move_assignable<ConstructibleObjectType>::value ||
3922 std::is_copy_assignable<ConstructibleObjectType>::value) &&
3923 (is_constructible<typename ConstructibleObjectType::key_type,
3924 typename object_t::key_type>::value &&
3925 std::is_same <
3926 typename object_t::mapped_type,
3927 typename ConstructibleObjectType::mapped_type >::value)) ||
3928 (has_from_json<BasicJsonType,
3929 typename ConstructibleObjectType::mapped_type>::value ||
3930 has_non_default_from_json <
3931 BasicJsonType,
3932 typename ConstructibleObjectType::mapped_type >::value);
3933 };
3934
3935 template<typename BasicJsonType, typename ConstructibleObjectType>
3936 struct is_constructible_object_type
3937 : is_constructible_object_type_impl<BasicJsonType,
3938 ConstructibleObjectType> {};
3939
3940 template<typename BasicJsonType, typename CompatibleStringType>
3941 struct is_compatible_string_type
3942 {
3943 static constexpr auto value =
3944 is_constructible<typename BasicJsonType::string_t, CompatibleStringType>::value;
3945 };
3946
3947 template<typename BasicJsonType, typename ConstructibleStringType>
3948 struct is_constructible_string_type
3949 {
3950 // launder type through decltype() to fix compilation failure on ICPC
3951 #ifdef __INTEL_COMPILER
3952 using laundered_type = decltype(std::declval<ConstructibleStringType>());
3953 #else
3954 using laundered_type = ConstructibleStringType;
3955 #endif
3956
3957 static constexpr auto value =
3958 conjunction <
3959 is_constructible<laundered_type, typename BasicJsonType::string_t>,
3960 is_detected_exact<typename BasicJsonType::string_t::value_type,
3961 value_type_t, laundered_type >>::value;
3962 };
3963
3964 template<typename BasicJsonType, typename CompatibleArrayType, typename = void>
3965 struct is_compatible_array_type_impl : std::false_type {};
3966
3967 template<typename BasicJsonType, typename CompatibleArrayType>
3968 struct is_compatible_array_type_impl <
3969 BasicJsonType, CompatibleArrayType,
3970 enable_if_t <
3971 is_detected<iterator_t, CompatibleArrayType>::value&&
3972 is_iterator_traits<iterator_traits<detected_t<iterator_t, CompatibleArrayType>>>::value&&
3973 // special case for types like std::filesystem::path whose iterator's value_type are themselves
3974 // c.f. https://github.com/nlohmann/json/pull/3073
3975 !std::is_same<CompatibleArrayType, detected_t<range_value_t, CompatibleArrayType>>::value >>
3976 {
3977 static constexpr bool value =
3978 is_constructible<BasicJsonType,
3979 range_value_t<CompatibleArrayType>>::value;
3980 };
3981
3982 template<typename BasicJsonType, typename CompatibleArrayType>
3983 struct is_compatible_array_type
3984 : is_compatible_array_type_impl<BasicJsonType, CompatibleArrayType> {};
3985
3986 template<typename BasicJsonType, typename ConstructibleArrayType, typename = void>
3987 struct is_constructible_array_type_impl : std::false_type {};
3988
3989 template<typename BasicJsonType, typename ConstructibleArrayType>
3990 struct is_constructible_array_type_impl <
3991 BasicJsonType, ConstructibleArrayType,
3992 enable_if_t<std::is_same<ConstructibleArrayType,
3993 typename BasicJsonType::value_type>::value >>
3994 : std::true_type {};
3995
3996 template<typename BasicJsonType, typename ConstructibleArrayType>
3997 struct is_constructible_array_type_impl <
3998 BasicJsonType, ConstructibleArrayType,
3999 enable_if_t < !std::is_same<ConstructibleArrayType,
4000 typename BasicJsonType::value_type>::value&&
4001 !is_compatible_string_type<BasicJsonType, ConstructibleArrayType>::value&&
4002 is_default_constructible<ConstructibleArrayType>::value&&
4003 (std::is_move_assignable<ConstructibleArrayType>::value ||
4004 std::is_copy_assignable<ConstructibleArrayType>::value)&&
4005 is_detected<iterator_t, ConstructibleArrayType>::value&&
4006 is_iterator_traits<iterator_traits<detected_t<iterator_t, ConstructibleArrayType>>>::value&&
4007 is_detected<range_value_t, ConstructibleArrayType>::value&&
4008 // special case for types like std::filesystem::path whose iterator's value_type are themselves
4009 // c.f. https://github.com/nlohmann/json/pull/3073
4010 !std::is_same<ConstructibleArrayType, detected_t<range_value_t, ConstructibleArrayType>>::value&&
4011 is_complete_type <
4012 detected_t<range_value_t, ConstructibleArrayType >>::value >>
4013 {
4014 using value_type = range_value_t<ConstructibleArrayType>;
4015
4016 static constexpr bool value =
4017 std::is_same<value_type,
4018 typename BasicJsonType::array_t::value_type>::value ||
4019 has_from_json<BasicJsonType,
4020 value_type>::value ||
4021 has_non_default_from_json <
4022 BasicJsonType,
4023 value_type >::value;
4024 };
4025
4026 template<typename BasicJsonType, typename ConstructibleArrayType>
4027 struct is_constructible_array_type
4028 : is_constructible_array_type_impl<BasicJsonType, ConstructibleArrayType> {};
4029
4030 template<typename RealIntegerType, typename CompatibleNumberIntegerType,
4031 typename = void>
4032 struct is_compatible_integer_type_impl : std::false_type {};
4033
4034 template<typename RealIntegerType, typename CompatibleNumberIntegerType>
4035 struct is_compatible_integer_type_impl <
4036 RealIntegerType, CompatibleNumberIntegerType,
4037 enable_if_t < std::is_integral<RealIntegerType>::value&&
4038 std::is_integral<CompatibleNumberIntegerType>::value&&
4039 !std::is_same<bool, CompatibleNumberIntegerType>::value >>
4040 {
4041 // is there an assert somewhere on overflows?
4042 using RealLimits = std::numeric_limits<RealIntegerType>;
4043 using CompatibleLimits = std::numeric_limits<CompatibleNumberIntegerType>;
4044
4045 static constexpr auto value =
4046 is_constructible<RealIntegerType,
4047 CompatibleNumberIntegerType>::value &&
4048 CompatibleLimits::is_integer &&
4049 RealLimits::is_signed == CompatibleLimits::is_signed;
4050 };
4051
4052 template<typename RealIntegerType, typename CompatibleNumberIntegerType>
4053 struct is_compatible_integer_type
4054 : is_compatible_integer_type_impl<RealIntegerType,
4055 CompatibleNumberIntegerType> {};
4056
4057 template<typename BasicJsonType, typename CompatibleType, typename = void>
4058 struct is_compatible_type_impl: std::false_type {};
4059
4060 template<typename BasicJsonType, typename CompatibleType>
4061 struct is_compatible_type_impl <
4062 BasicJsonType, CompatibleType,
4063 enable_if_t<is_complete_type<CompatibleType>::value >>
4064 {
4065 static constexpr bool value =
4066 has_to_json<BasicJsonType, CompatibleType>::value;
4067 };
4068
4069 template<typename BasicJsonType, typename CompatibleType>
4070 struct is_compatible_type
4071 : is_compatible_type_impl<BasicJsonType, CompatibleType> {};
4072
4073 template<typename T1, typename T2>
4074 struct is_constructible_tuple : std::false_type {};
4075
4076 template<typename T1, typename... Args>
4077 struct is_constructible_tuple<T1, std::tuple<Args...>> : conjunction<is_constructible<T1, Args>...> {};
4078
4079 template<typename BasicJsonType, typename T>
4080 struct is_json_iterator_of : std::false_type {};
4081
4082 template<typename BasicJsonType>
4083 struct is_json_iterator_of<BasicJsonType, typename BasicJsonType::iterator> : std::true_type {};
4084
4085 template<typename BasicJsonType>
4086 struct is_json_iterator_of<BasicJsonType, typename BasicJsonType::const_iterator> : std::true_type
4087 {};
4088
4089 // checks if a given type T is a template specialization of Primary
4090 template<template <typename...> class Primary, typename T>
4091 struct is_specialization_of : std::false_type {};
4092
4093 template<template <typename...> class Primary, typename... Args>
4094 struct is_specialization_of<Primary, Primary<Args...>> : std::true_type {};
4095
4096 template<typename T>
4097 using is_json_pointer = is_specialization_of<::nlohmann::json_pointer, uncvref_t<T>>;
4098
4099 // checks if A and B are comparable using Compare functor
4100 template<typename Compare, typename A, typename B, typename = void>
4101 struct is_comparable : std::false_type {};
4102
4103 template<typename Compare, typename A, typename B>
4104 struct is_comparable<Compare, A, B, void_t<
4105 decltype(std::declval<Compare>()(std::declval<A>(), std::declval<B>())),
4106 decltype(std::declval<Compare>()(std::declval<B>(), std::declval<A>()))
4107 >> : std::true_type {};
4108
4109 template<typename T>
4110 using detect_is_transparent = typename T::is_transparent;
4111
4112 // type trait to check if KeyType can be used as object key (without a BasicJsonType)
4113 // see is_usable_as_basic_json_key_type below
4114 template<typename Comparator, typename ObjectKeyType, typename KeyTypeCVRef, bool RequireTransparentComparator = true,
4115 bool ExcludeObjectKeyType = RequireTransparentComparator, typename KeyType = uncvref_t<KeyTypeCVRef>>
4116 using is_usable_as_key_type = typename std::conditional <
4117 is_comparable<Comparator, ObjectKeyType, KeyTypeCVRef>::value
4118 && !(ExcludeObjectKeyType && std::is_same<KeyType,
4119 ObjectKeyType>::value)
4120 && (!RequireTransparentComparator
4121 || is_detected <detect_is_transparent, Comparator>::value)
4122 && !is_json_pointer<KeyType>::value,
4123 std::true_type,
4124 std::false_type >::type;
4125
4126 // type trait to check if KeyType can be used as object key
4127 // true if:
4128 // - KeyType is comparable with BasicJsonType::object_t::key_type
4129 // - if ExcludeObjectKeyType is true, KeyType is not BasicJsonType::object_t::key_type
4130 // - the comparator is transparent or RequireTransparentComparator is false
4131 // - KeyType is not a JSON iterator or json_pointer
4132 template<typename BasicJsonType, typename KeyTypeCVRef, bool RequireTransparentComparator = true,
4133 bool ExcludeObjectKeyType = RequireTransparentComparator, typename KeyType = uncvref_t<KeyTypeCVRef>>
4134 using is_usable_as_basic_json_key_type = typename std::conditional <
4135 is_usable_as_key_type<typename BasicJsonType::object_comparator_t,
4136 typename BasicJsonType::object_t::key_type, KeyTypeCVRef,
4137 RequireTransparentComparator, ExcludeObjectKeyType>::value
4138 && !is_json_iterator_of<BasicJsonType, KeyType>::value,
4139 std::true_type,
4140 std::false_type >::type;
4141
4142 template<typename ObjectType, typename KeyType>
4143 using detect_erase_with_key_type = decltype(std::declval<ObjectType&>().erase(std::declval<KeyType>()));
4144
4145 // type trait to check if object_t has an erase() member functions accepting KeyType
4146 template<typename BasicJsonType, typename KeyType>
4147 using has_erase_with_key_type = typename std::conditional <
4148 is_detected <
4149 detect_erase_with_key_type,
4150 typename BasicJsonType::object_t, KeyType >::value,
4151 std::true_type,
4152 std::false_type >::type;
4153
4154 // a naive helper to check if a type is an ordered_map (exploits the fact that
4155 // ordered_map inherits capacity() from std::vector)
4156 template <typename T>
4157 struct is_ordered_map
4158 {
4159 using one = char;
4160
4161 struct two
4162 {
4163 char x[2]; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
4164 };
4165
4166 template <typename C> static one test( decltype(&C::capacity) ) ;
4167 template <typename C> static two test(...);
4168
4169 enum { value = sizeof(test<T>(nullptr)) == sizeof(char) }; // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
4170 };
4171
4172 // to avoid useless casts (see https://github.com/nlohmann/json/issues/2893#issuecomment-889152324)
4173 template < typename T, typename U, enable_if_t < !std::is_same<T, U>::value, int > = 0 >
4174 T conditional_static_cast(U value)
4175 {
4176 return static_cast<T>(value);
4177 }
4178
4179 template<typename T, typename U, enable_if_t<std::is_same<T, U>::value, int> = 0>
4180 T conditional_static_cast(U value)
4181 {
4182 return value;
4183 }
4184
4185 template<typename... Types>
4186 using all_integral = conjunction<std::is_integral<Types>...>;
4187
4188 template<typename... Types>
4189 using all_signed = conjunction<std::is_signed<Types>...>;
4190
4191 template<typename... Types>
4192 using all_unsigned = conjunction<std::is_unsigned<Types>...>;
4193
4194 // there's a disjunction trait in another PR; replace when merged
4195 template<typename... Types>
4196 using same_sign = std::integral_constant < bool,
4197 all_signed<Types...>::value || all_unsigned<Types...>::value >;
4198
4199 template<typename OfType, typename T>
4200 using never_out_of_range = std::integral_constant < bool,
4201 (std::is_signed<OfType>::value && (sizeof(T) < sizeof(OfType)))
4202 || (same_sign<OfType, T>::value && sizeof(OfType) == sizeof(T)) >;
4203
4204 template<typename OfType, typename T,
4205 bool OfTypeSigned = std::is_signed<OfType>::value,
4206 bool TSigned = std::is_signed<T>::value>
4207 struct value_in_range_of_impl2;
4208
4209 template<typename OfType, typename T>
4210 struct value_in_range_of_impl2<OfType, T, false, false>
4211 {
4212 static constexpr bool test(T val)
4213 {
4214 using CommonType = typename std::common_type<OfType, T>::type;
4215 return static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
4216 }
4217 };
4218
4219 template<typename OfType, typename T>
4220 struct value_in_range_of_impl2<OfType, T, true, false>
4221 {
4222 static constexpr bool test(T val)
4223 {
4224 using CommonType = typename std::common_type<OfType, T>::type;
4225 return static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
4226 }
4227 };
4228
4229 template<typename OfType, typename T>
4230 struct value_in_range_of_impl2<OfType, T, false, true>
4231 {
4232 static constexpr bool test(T val)
4233 {
4234 using CommonType = typename std::common_type<OfType, T>::type;
4235 return val >= 0 && static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
4236 }
4237 };
4238
4239 template<typename OfType, typename T>
4240 struct value_in_range_of_impl2<OfType, T, true, true>
4241 {
4242 static constexpr bool test(T val)
4243 {
4244 using CommonType = typename std::common_type<OfType, T>::type;
4245 return static_cast<CommonType>(val) >= static_cast<CommonType>((std::numeric_limits<OfType>::min)())
4246 && static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
4247 }
4248 };
4249
4250 template<typename OfType, typename T,
4251 bool NeverOutOfRange = never_out_of_range<OfType, T>::value,
4252 typename = detail::enable_if_t<all_integral<OfType, T>::value>>
4253 struct value_in_range_of_impl1;
4254
4255 template<typename OfType, typename T>
4256 struct value_in_range_of_impl1<OfType, T, false>
4257 {
4258 static constexpr bool test(T val)
4259 {
4260 return value_in_range_of_impl2<OfType, T>::test(val);
4261 }
4262 };
4263
4264 template<typename OfType, typename T>
4265 struct value_in_range_of_impl1<OfType, T, true>
4266 {
4267 static constexpr bool test(T /*val*/)
4268 {
4269 return true;
4270 }
4271 };
4272
4273 template<typename OfType, typename T>
4274 constexpr bool value_in_range_of(T val)
4275 {
4276 return value_in_range_of_impl1<OfType, T>::test(val);
4277 }
4278
4279 template<bool Value>
4280 using bool_constant = std::integral_constant<bool, Value>;
4281
4282 ///////////////////////////////////////////////////////////////////////////////
4283 // is_c_string
4284 ///////////////////////////////////////////////////////////////////////////////
4285
4286 namespace impl
4287 {
4288
4289 template<typename T>
4290 constexpr bool is_c_string()
4291 {
4292 using TUnExt = typename std::remove_extent<T>::type;
4293 using TUnCVExt = typename std::remove_cv<TUnExt>::type;
4294 using TUnPtr = typename std::remove_pointer<T>::type;
4295 using TUnCVPtr = typename std::remove_cv<TUnPtr>::type;
4296 return
4297 (std::is_array<T>::value && std::is_same<TUnCVExt, char>::value)
4298 || (std::is_pointer<T>::value && std::is_same<TUnCVPtr, char>::value);
4299 }
4300
4301 } // namespace impl
4302
4303 // checks whether T is a [cv] char */[cv] char[] C string
4304 template<typename T>
4305 struct is_c_string : bool_constant<impl::is_c_string<T>()> {};
4306
4307 template<typename T>
4308 using is_c_string_uncvref = is_c_string<uncvref_t<T>>;
4309
4310 ///////////////////////////////////////////////////////////////////////////////
4311 // is_transparent
4312 ///////////////////////////////////////////////////////////////////////////////
4313
4314 namespace impl
4315 {
4316
4317 template<typename T>
4318 constexpr bool is_transparent()
4319 {
4320 return is_detected<detect_is_transparent, T>::value;
4321 }
4322
4323 } // namespace impl
4324
4325 // checks whether T has a member named is_transparent
4326 template<typename T>
4327 struct is_transparent : bool_constant<impl::is_transparent<T>()> {};
4328
4329 ///////////////////////////////////////////////////////////////////////////////
4330
4331 } // namespace detail
4332 NLOHMANN_JSON_NAMESPACE_END
4333
4334 // #include <nlohmann/detail/string_concat.hpp>
4335 // __ _____ _____ _____
4336 // __| | __| | | | JSON for Modern C++
4337 // | | |__ | | | | | | version 3.12.0
4338 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
4339 //
4340 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
4341 // SPDX-License-Identifier: MIT
4342
4343
4344
4345 #include <cstring> // strlen
4346 #include <string> // string
4347 #include <utility> // forward
4348
4349 // #include <nlohmann/detail/meta/cpp_future.hpp>
4350
4351 // #include <nlohmann/detail/meta/detected.hpp>
4352
4353
4354 NLOHMANN_JSON_NAMESPACE_BEGIN
4355 namespace detail
4356 {
4357
4358 inline std::size_t concat_length()
4359 {
4360 return 0;
4361 }
4362
4363 template<typename... Args>
4364 inline std::size_t concat_length(const char* cstr, const Args& ... rest);
4365
4366 template<typename StringType, typename... Args>
4367 inline std::size_t concat_length(const StringType& str, const Args& ... rest);
4368
4369 template<typename... Args>
4370 inline std::size_t concat_length(const char /*c*/, const Args& ... rest)
4371 {
4372 return 1 + concat_length(rest...);
4373 }
4374
4375 template<typename... Args>
4376 inline std::size_t concat_length(const char* cstr, const Args& ... rest)
4377 {
4378 // cppcheck-suppress ignoredReturnValue
4379 return ::strlen(cstr) + concat_length(rest...);
4380 }
4381
4382 template<typename StringType, typename... Args>
4383 inline std::size_t concat_length(const StringType& str, const Args& ... rest)
4384 {
4385 return str.size() + concat_length(rest...);
4386 }
4387
4388 template<typename OutStringType>
4389 inline void concat_into(OutStringType& /*out*/)
4390 {}
4391
4392 template<typename StringType, typename Arg>
4393 using string_can_append = decltype(std::declval<StringType&>().append(std::declval < Arg && > ()));
4394
4395 template<typename StringType, typename Arg>
4396 using detect_string_can_append = is_detected<string_can_append, StringType, Arg>;
4397
4398 template<typename StringType, typename Arg>
4399 using string_can_append_op = decltype(std::declval<StringType&>() += std::declval < Arg && > ());
4400
4401 template<typename StringType, typename Arg>
4402 using detect_string_can_append_op = is_detected<string_can_append_op, StringType, Arg>;
4403
4404 template<typename StringType, typename Arg>
4405 using string_can_append_iter = decltype(std::declval<StringType&>().append(std::declval<const Arg&>().begin(), std::declval<const Arg&>().end()));
4406
4407 template<typename StringType, typename Arg>
4408 using detect_string_can_append_iter = is_detected<string_can_append_iter, StringType, Arg>;
4409
4410 template<typename StringType, typename Arg>
4411 using string_can_append_data = decltype(std::declval<StringType&>().append(std::declval<const Arg&>().data(), std::declval<const Arg&>().size()));
4412
4413 template<typename StringType, typename Arg>
4414 using detect_string_can_append_data = is_detected<string_can_append_data, StringType, Arg>;
4415
4416 template < typename OutStringType, typename Arg, typename... Args,
4417 enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
4418 && detect_string_can_append_op<OutStringType, Arg>::value, int > = 0 >
4419 inline void concat_into(OutStringType& out, Arg && arg, Args && ... rest);
4420
4421 template < typename OutStringType, typename Arg, typename... Args,
4422 enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
4423 && !detect_string_can_append_op<OutStringType, Arg>::value
4424 && detect_string_can_append_iter<OutStringType, Arg>::value, int > = 0 >
4425 inline void concat_into(OutStringType& out, const Arg& arg, Args && ... rest);
4426
4427 template < typename OutStringType, typename Arg, typename... Args,
4428 enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
4429 && !detect_string_can_append_op<OutStringType, Arg>::value
4430 && !detect_string_can_append_iter<OutStringType, Arg>::value
4431 && detect_string_can_append_data<OutStringType, Arg>::value, int > = 0 >
4432 inline void concat_into(OutStringType& out, const Arg& arg, Args && ... rest);
4433
4434 template<typename OutStringType, typename Arg, typename... Args,
4435 enable_if_t<detect_string_can_append<OutStringType, Arg>::value, int> = 0>
4436 inline void concat_into(OutStringType& out, Arg && arg, Args && ... rest)
4437 {
4438 out.append(std::forward<Arg>(arg));
4439 concat_into(out, std::forward<Args>(rest)...);
4440 }
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 > >
4445 inline void concat_into(OutStringType& out, Arg&& arg, Args&& ... rest)
4446 {
4447 out += std::forward<Arg>(arg);
4448 concat_into(out, std::forward<Args>(rest)...);
4449 }
4450
4451 template < typename OutStringType, typename Arg, typename... Args,
4452 enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
4453 && !detect_string_can_append_op<OutStringType, Arg>::value
4454 && detect_string_can_append_iter<OutStringType, Arg>::value, int > >
4455 inline void concat_into(OutStringType& out, const Arg& arg, Args&& ... rest)
4456 {
4457 out.append(arg.begin(), arg.end());
4458 concat_into(out, std::forward<Args>(rest)...);
4459 }
4460
4461 template < typename OutStringType, typename Arg, typename... Args,
4462 enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
4463 && !detect_string_can_append_op<OutStringType, Arg>::value
4464 && !detect_string_can_append_iter<OutStringType, Arg>::value
4465 && detect_string_can_append_data<OutStringType, Arg>::value, int > >
4466 inline void concat_into(OutStringType& out, const Arg& arg, Args&& ... rest)
4467 {
4468 out.append(arg.data(), arg.size());
4469 concat_into(out, std::forward<Args>(rest)...);
4470 }
4471
4472 template<typename OutStringType = std::string, typename... Args>
4473 inline OutStringType concat(Args && ... args)
4474 {
4475 OutStringType str;
4476 str.reserve(concat_length(args...));
4477 concat_into(str, std::forward<Args>(args)...);
4478 return str;
4479 }
4480
4481 } // namespace detail
4482 NLOHMANN_JSON_NAMESPACE_END
4483
4484
4485 // With -Wweak-vtables, Clang will complain about the exception classes as they
4486 // have no out-of-line virtual method definitions and their vtable will be
4487 // emitted in every translation unit. This issue cannot be fixed with a
4488 // header-only library as there is no implementation file to move these
4489 // functions to. As a result, we suppress this warning here to avoid client
4490 // code to stumble over this. See https://github.com/nlohmann/json/issues/4087
4491 // for a discussion.
4492 #if defined(__clang__)
4493 #pragma clang diagnostic push
4494 #pragma clang diagnostic ignored "-Wweak-vtables"
4495 #endif
4496
4497 NLOHMANN_JSON_NAMESPACE_BEGIN
4498 namespace detail
4499 {
4500
4501 ////////////////
4502 // exceptions //
4503 ////////////////
4504
4505 /// @brief general exception of the @ref basic_json class
4506 /// @sa https://json.nlohmann.me/api/basic_json/exception/
4507 class exception : public std::exception
4508 {
4509 public:
4510 /// returns the explanatory string
4511 const char* what() const noexcept override
4512 {
4513 return m.what();
4514 }
4515
4516 /// the id of the exception
4517 const int id; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes)
4518
4519 protected:
4520 JSON_HEDLEY_NON_NULL(3)
4521 exception(int id_, const char* what_arg) : id(id_), m(what_arg) {} // NOLINT(bugprone-throw-keyword-missing)
4522
4523 static std::string name(const std::string& ename, int id_)
4524 {
4525 return concat("[json.exception.", ename, '.', std::to_string(id_), "] ");
4526 }
4527
4528 static std::string diagnostics(std::nullptr_t /*leaf_element*/)
4529 {
4530 return "";
4531 }
4532
4533 template<typename BasicJsonType>
4534 static std::string diagnostics(const BasicJsonType* leaf_element)
4535 {
4536 #if JSON_DIAGNOSTICS
4537 std::vector<std::string> tokens;
4538 for (const auto* current = leaf_element; current != nullptr && current->m_parent != nullptr; current = current->m_parent)
4539 {
4540 switch (current->m_parent->type())
4541 {
4542 case value_t::array:
4543 {
4544 for (std::size_t i = 0; i < current->m_parent->m_data.m_value.array->size(); ++i)
4545 {
4546 if (&current->m_parent->m_data.m_value.array->operator[](i) == current)
4547 {
4548 tokens.emplace_back(std::to_string(i));
4549 break;
4550 }
4551 }
4552 break;
4553 }
4554
4555 case value_t::object:
4556 {
4557 for (const auto& element : *current->m_parent->m_data.m_value.object)
4558 {
4559 if (&element.second == current)
4560 {
4561 tokens.emplace_back(element.first.c_str());
4562 break;
4563 }
4564 }
4565 break;
4566 }
4567
4568 case value_t::null: // LCOV_EXCL_LINE
4569 case value_t::string: // LCOV_EXCL_LINE
4570 case value_t::boolean: // LCOV_EXCL_LINE
4571 case value_t::number_integer: // LCOV_EXCL_LINE
4572 case value_t::number_unsigned: // LCOV_EXCL_LINE
4573 case value_t::number_float: // LCOV_EXCL_LINE
4574 case value_t::binary: // LCOV_EXCL_LINE
4575 case value_t::discarded: // LCOV_EXCL_LINE
4576 default: // LCOV_EXCL_LINE
4577 break; // LCOV_EXCL_LINE
4578 }
4579 }
4580
4581 if (tokens.empty())
4582 {
4583 return "";
4584 }
4585
4586 auto str = std::accumulate(tokens.rbegin(), tokens.rend(), std::string{},
4587 [](const std::string & a, const std::string & b)
4588 {
4589 return concat(a, '/', detail::escape(b));
4590 });
4591
4592 return concat('(', str, ") ", get_byte_positions(leaf_element));
4593 #else
4594 return get_byte_positions(leaf_element);
4595 #endif
4596 }
4597
4598 private:
4599 /// an exception object as storage for error messages
4600 std::runtime_error m;
4601 #if JSON_DIAGNOSTIC_POSITIONS
4602 template<typename BasicJsonType>
4603 static std::string get_byte_positions(const BasicJsonType* leaf_element)
4604 {
4605 if ((leaf_element->start_pos() != std::string::npos) && (leaf_element->end_pos() != std::string::npos))
4606 {
4607 return concat("(bytes ", std::to_string(leaf_element->start_pos()), "-", std::to_string(leaf_element->end_pos()), ") ");
4608 }
4609 return "";
4610 }
4611 #else
4612 template<typename BasicJsonType>
4613 static std::string get_byte_positions(const BasicJsonType* leaf_element)
4614 {
4615 static_cast<void>(leaf_element);
4616 return "";
4617 }
4618 #endif
4619 };
4620
4621 /// @brief exception indicating a parse error
4622 /// @sa https://json.nlohmann.me/api/basic_json/parse_error/
4623 class parse_error : public exception
4624 {
4625 public:
4626 /*!
4627 @brief create a parse error exception
4628 @param[in] id_ the id of the exception
4629 @param[in] pos the position where the error occurred (or with
4630 chars_read_total=0 if the position cannot be
4631 determined)
4632 @param[in] what_arg the explanatory string
4633 @return parse_error object
4634 */
4635 template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
4636 static parse_error create(int id_, const position_t& pos, const std::string& what_arg, BasicJsonContext context)
4637 {
4638 const std::string w = concat(exception::name("parse_error", id_), "parse error",
4639 position_string(pos), ": ", exception::diagnostics(context), what_arg);
4640 return {id_, pos.chars_read_total, w.c_str()};
4641 }
4642
4643 template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
4644 static parse_error create(int id_, std::size_t byte_, const std::string& what_arg, BasicJsonContext context)
4645 {
4646 const std::string w = concat(exception::name("parse_error", id_), "parse error",
4647 (byte_ != 0 ? (concat(" at byte ", std::to_string(byte_))) : ""),
4648 ": ", exception::diagnostics(context), what_arg);
4649 return {id_, byte_, w.c_str()};
4650 }
4651
4652 /*!
4653 @brief byte index of the parse error
4654
4655 The byte index of the last read character in the input file.
4656
4657 @note For an input with n bytes, 1 is the index of the first character and
4658 n+1 is the index of the terminating null byte or the end of file.
4659 This also holds true when reading a byte vector (CBOR or MessagePack).
4660 */
4661 const std::size_t byte;
4662
4663 private:
4664 parse_error(int id_, std::size_t byte_, const char* what_arg)
4665 : exception(id_, what_arg), byte(byte_) {}
4666
4667 static std::string position_string(const position_t& pos)
4668 {
4669 return concat(" at line ", std::to_string(pos.lines_read + 1),
4670 ", column ", std::to_string(pos.chars_read_current_line));
4671 }
4672 };
4673
4674 /// @brief exception indicating errors with iterators
4675 /// @sa https://json.nlohmann.me/api/basic_json/invalid_iterator/
4676 class invalid_iterator : public exception
4677 {
4678 public:
4679 template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
4680 static invalid_iterator create(int id_, const std::string& what_arg, BasicJsonContext context)
4681 {
4682 const std::string w = concat(exception::name("invalid_iterator", id_), exception::diagnostics(context), what_arg);
4683 return {id_, w.c_str()};
4684 }
4685
4686 private:
4687 JSON_HEDLEY_NON_NULL(3)
4688 invalid_iterator(int id_, const char* what_arg)
4689 : exception(id_, what_arg) {}
4690 };
4691
4692 /// @brief exception indicating executing a member function with a wrong type
4693 /// @sa https://json.nlohmann.me/api/basic_json/type_error/
4694 class type_error : public exception
4695 {
4696 public:
4697 template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
4698 static type_error create(int id_, const std::string& what_arg, BasicJsonContext context)
4699 {
4700 const std::string w = concat(exception::name("type_error", id_), exception::diagnostics(context), what_arg);
4701 return {id_, w.c_str()};
4702 }
4703
4704 private:
4705 JSON_HEDLEY_NON_NULL(3)
4706 type_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
4707 };
4708
4709 /// @brief exception indicating access out of the defined range
4710 /// @sa https://json.nlohmann.me/api/basic_json/out_of_range/
4711 class out_of_range : public exception
4712 {
4713 public:
4714 template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
4715 static out_of_range create(int id_, const std::string& what_arg, BasicJsonContext context)
4716 {
4717 const std::string w = concat(exception::name("out_of_range", id_), exception::diagnostics(context), what_arg);
4718 return {id_, w.c_str()};
4719 }
4720
4721 private:
4722 JSON_HEDLEY_NON_NULL(3)
4723 out_of_range(int id_, const char* what_arg) : exception(id_, what_arg) {}
4724 };
4725
4726 /// @brief exception indicating other library errors
4727 /// @sa https://json.nlohmann.me/api/basic_json/other_error/
4728 class other_error : public exception
4729 {
4730 public:
4731 template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
4732 static other_error create(int id_, const std::string& what_arg, BasicJsonContext context)
4733 {
4734 const std::string w = concat(exception::name("other_error", id_), exception::diagnostics(context), what_arg);
4735 return {id_, w.c_str()};
4736 }
4737
4738 private:
4739 JSON_HEDLEY_NON_NULL(3)
4740 other_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
4741 };
4742
4743 } // namespace detail
4744 NLOHMANN_JSON_NAMESPACE_END
4745
4746 #if defined(__clang__)
4747 #pragma clang diagnostic pop
4748 #endif
4749
4750 // #include <nlohmann/detail/macro_scope.hpp>
4751
4752 // #include <nlohmann/detail/meta/cpp_future.hpp>
4753
4754 // #include <nlohmann/detail/meta/identity_tag.hpp>
4755 // __ _____ _____ _____
4756 // __| | __| | | | JSON for Modern C++
4757 // | | |__ | | | | | | version 3.12.0
4758 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
4759 //
4760 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
4761 // SPDX-License-Identifier: MIT
4762
4763
4764
4765 // #include <nlohmann/detail/abi_macros.hpp>
4766
4767
4768 NLOHMANN_JSON_NAMESPACE_BEGIN
4769 namespace detail
4770 {
4771
4772 // dispatching helper struct
4773 template <class T> struct identity_tag {};
4774
4775 } // namespace detail
4776 NLOHMANN_JSON_NAMESPACE_END
4777
4778 // #include <nlohmann/detail/meta/std_fs.hpp>
4779 // __ _____ _____ _____
4780 // __| | __| | | | JSON for Modern C++
4781 // | | |__ | | | | | | version 3.12.0
4782 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
4783 //
4784 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
4785 // SPDX-License-Identifier: MIT
4786
4787
4788
4789 // #include <nlohmann/detail/macro_scope.hpp>
4790
4791
4792 #if JSON_HAS_EXPERIMENTAL_FILESYSTEM
4793 #include <experimental/filesystem>
4794 NLOHMANN_JSON_NAMESPACE_BEGIN
4795 namespace detail
4796 {
4797 namespace std_fs = std::experimental::filesystem;
4798 } // namespace detail
4799 NLOHMANN_JSON_NAMESPACE_END
4800 #elif JSON_HAS_FILESYSTEM
4801 #include <filesystem> // NOLINT(build/c++17)
4802 NLOHMANN_JSON_NAMESPACE_BEGIN
4803 namespace detail
4804 {
4805 namespace std_fs = std::filesystem;
4806 } // namespace detail
4807 NLOHMANN_JSON_NAMESPACE_END
4808 #endif
4809
4810 // #include <nlohmann/detail/meta/type_traits.hpp>
4811
4812 // #include <nlohmann/detail/string_concat.hpp>
4813
4814 // #include <nlohmann/detail/value_t.hpp>
4815
4816
4817 // include after macro_scope.hpp
4818 #ifdef JSON_HAS_CPP_17
4819 #include <optional> // optional
4820 #endif
4821
4822 NLOHMANN_JSON_NAMESPACE_BEGIN
4823 namespace detail
4824 {
4825
4826 template<typename BasicJsonType>
4827 inline void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
4828 {
4829 if (JSON_HEDLEY_UNLIKELY(!j.is_null()))
4830 {
4831 JSON_THROW(type_error::create(302, concat("type must be null, but is ", j.type_name()), &j));
4832 }
4833 n = nullptr;
4834 }
4835
4836 #ifdef JSON_HAS_CPP_17
4837 template<typename BasicJsonType, typename T>
4838 void from_json(const BasicJsonType& j, std::optional<T>& opt)
4839 {
4840 if (j.is_null())
4841 {
4842 opt = std::nullopt;
4843 }
4844 else
4845 {
4846 opt.emplace(j.template get<T>());
4847 }
4848 }
4849 #endif // JSON_HAS_CPP_17
4850
4851 // overloads for basic_json template parameters
4852 template < typename BasicJsonType, typename ArithmeticType,
4853 enable_if_t < std::is_arithmetic<ArithmeticType>::value&&
4854 !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
4855 int > = 0 >
4856 void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
4857 {
4858 switch (static_cast<value_t>(j))
4859 {
4860 case value_t::number_unsigned:
4861 {
4862 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
4863 break;
4864 }
4865 case value_t::number_integer:
4866 {
4867 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
4868 break;
4869 }
4870 case value_t::number_float:
4871 {
4872 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
4873 break;
4874 }
4875
4876 case value_t::null:
4877 case value_t::object:
4878 case value_t::array:
4879 case value_t::string:
4880 case value_t::boolean:
4881 case value_t::binary:
4882 case value_t::discarded:
4883 default:
4884 JSON_THROW(type_error::create(302, concat("type must be number, but is ", j.type_name()), &j));
4885 }
4886 }
4887
4888 template<typename BasicJsonType>
4889 inline void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
4890 {
4891 if (JSON_HEDLEY_UNLIKELY(!j.is_boolean()))
4892 {
4893 JSON_THROW(type_error::create(302, concat("type must be boolean, but is ", j.type_name()), &j));
4894 }
4895 b = *j.template get_ptr<const typename BasicJsonType::boolean_t*>();
4896 }
4897
4898 template<typename BasicJsonType>
4899 inline void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
4900 {
4901 if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
4902 {
4903 JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
4904 }
4905 s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
4906 }
4907
4908 template <
4909 typename BasicJsonType, typename StringType,
4910 enable_if_t <
4911 std::is_assignable<StringType&, const typename BasicJsonType::string_t>::value
4912 && is_detected_exact<typename BasicJsonType::string_t::value_type, value_type_t, StringType>::value
4913 && !std::is_same<typename BasicJsonType::string_t, StringType>::value
4914 && !is_json_ref<StringType>::value, int > = 0 >
4915 inline void from_json(const BasicJsonType& j, StringType& s)
4916 {
4917 if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
4918 {
4919 JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
4920 }
4921
4922 s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
4923 }
4924
4925 template<typename BasicJsonType>
4926 inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val)
4927 {
4928 get_arithmetic_value(j, val);
4929 }
4930
4931 template<typename BasicJsonType>
4932 inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val)
4933 {
4934 get_arithmetic_value(j, val);
4935 }
4936
4937 template<typename BasicJsonType>
4938 inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val)
4939 {
4940 get_arithmetic_value(j, val);
4941 }
4942
4943 #if !JSON_DISABLE_ENUM_SERIALIZATION
4944 template<typename BasicJsonType, typename EnumType,
4945 enable_if_t<std::is_enum<EnumType>::value, int> = 0>
4946 inline void from_json(const BasicJsonType& j, EnumType& e)
4947 {
4948 typename std::underlying_type<EnumType>::type val;
4949 get_arithmetic_value(j, val);
4950 e = static_cast<EnumType>(val);
4951 }
4952 #endif // JSON_DISABLE_ENUM_SERIALIZATION
4953
4954 // forward_list doesn't have an insert method
4955 template<typename BasicJsonType, typename T, typename Allocator,
4956 enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
4957 inline void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
4958 {
4959 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
4960 {
4961 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
4962 }
4963 l.clear();
4964 std::transform(j.rbegin(), j.rend(),
4965 std::front_inserter(l), [](const BasicJsonType & i)
4966 {
4967 return i.template get<T>();
4968 });
4969 }
4970
4971 // valarray doesn't have an insert method
4972 template<typename BasicJsonType, typename T,
4973 enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
4974 inline void from_json(const BasicJsonType& j, std::valarray<T>& l)
4975 {
4976 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
4977 {
4978 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
4979 }
4980 l.resize(j.size());
4981 std::transform(j.begin(), j.end(), std::begin(l),
4982 [](const BasicJsonType & elem)
4983 {
4984 return elem.template get<T>();
4985 });
4986 }
4987
4988 template<typename BasicJsonType, typename T, std::size_t N>
4989 auto from_json(const BasicJsonType& j, T (&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
4990 -> decltype(j.template get<T>(), void())
4991 {
4992 for (std::size_t i = 0; i < N; ++i)
4993 {
4994 arr[i] = j.at(i).template get<T>();
4995 }
4996 }
4997
4998 template<typename BasicJsonType, typename T, std::size_t N1, std::size_t N2>
4999 auto from_json(const BasicJsonType& j, T (&arr)[N1][N2]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
5000 -> decltype(j.template get<T>(), void())
5001 {
5002 for (std::size_t i1 = 0; i1 < N1; ++i1)
5003 {
5004 for (std::size_t i2 = 0; i2 < N2; ++i2)
5005 {
5006 arr[i1][i2] = j.at(i1).at(i2).template get<T>();
5007 }
5008 }
5009 }
5010
5011 template<typename BasicJsonType, typename T, std::size_t N1, std::size_t N2, std::size_t N3>
5012 auto from_json(const BasicJsonType& j, T (&arr)[N1][N2][N3]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
5013 -> decltype(j.template get<T>(), void())
5014 {
5015 for (std::size_t i1 = 0; i1 < N1; ++i1)
5016 {
5017 for (std::size_t i2 = 0; i2 < N2; ++i2)
5018 {
5019 for (std::size_t i3 = 0; i3 < N3; ++i3)
5020 {
5021 arr[i1][i2][i3] = j.at(i1).at(i2).at(i3).template get<T>();
5022 }
5023 }
5024 }
5025 }
5026
5027 template<typename BasicJsonType, typename T, std::size_t N1, std::size_t N2, std::size_t N3, std::size_t N4>
5028 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)
5029 -> decltype(j.template get<T>(), void())
5030 {
5031 for (std::size_t i1 = 0; i1 < N1; ++i1)
5032 {
5033 for (std::size_t i2 = 0; i2 < N2; ++i2)
5034 {
5035 for (std::size_t i3 = 0; i3 < N3; ++i3)
5036 {
5037 for (std::size_t i4 = 0; i4 < N4; ++i4)
5038 {
5039 arr[i1][i2][i3][i4] = j.at(i1).at(i2).at(i3).at(i4).template get<T>();
5040 }
5041 }
5042 }
5043 }
5044 }
5045
5046 template<typename BasicJsonType>
5047 inline void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType::array_t& arr, priority_tag<3> /*unused*/)
5048 {
5049 arr = *j.template get_ptr<const typename BasicJsonType::array_t*>();
5050 }
5051
5052 template<typename BasicJsonType, typename T, std::size_t N>
5053 auto from_json_array_impl(const BasicJsonType& j, std::array<T, N>& arr,
5054 priority_tag<2> /*unused*/)
5055 -> decltype(j.template get<T>(), void())
5056 {
5057 for (std::size_t i = 0; i < N; ++i)
5058 {
5059 arr[i] = j.at(i).template get<T>();
5060 }
5061 }
5062
5063 template<typename BasicJsonType, typename ConstructibleArrayType,
5064 enable_if_t<
5065 std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
5066 int> = 0>
5067 auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<1> /*unused*/)
5068 -> decltype(
5069 arr.reserve(std::declval<typename ConstructibleArrayType::size_type>()),
5070 j.template get<typename ConstructibleArrayType::value_type>(),
5071 void())
5072 {
5073 using std::end;
5074
5075 ConstructibleArrayType ret;
5076 ret.reserve(j.size());
5077 std::transform(j.begin(), j.end(),
5078 std::inserter(ret, end(ret)), [](const BasicJsonType & i)
5079 {
5080 // get<BasicJsonType>() returns *this, this won't call a from_json
5081 // method when value_type is BasicJsonType
5082 return i.template get<typename ConstructibleArrayType::value_type>();
5083 });
5084 arr = std::move(ret);
5085 }
5086
5087 template<typename BasicJsonType, typename ConstructibleArrayType,
5088 enable_if_t<
5089 std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
5090 int> = 0>
5091 inline void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr,
5092 priority_tag<0> /*unused*/)
5093 {
5094 using std::end;
5095
5096 ConstructibleArrayType ret;
5097 std::transform(
5098 j.begin(), j.end(), std::inserter(ret, end(ret)),
5099 [](const BasicJsonType & i)
5100 {
5101 // get<BasicJsonType>() returns *this, this won't call a from_json
5102 // method when value_type is BasicJsonType
5103 return i.template get<typename ConstructibleArrayType::value_type>();
5104 });
5105 arr = std::move(ret);
5106 }
5107
5108 template < typename BasicJsonType, typename ConstructibleArrayType,
5109 enable_if_t <
5110 is_constructible_array_type<BasicJsonType, ConstructibleArrayType>::value&&
5111 !is_constructible_object_type<BasicJsonType, ConstructibleArrayType>::value&&
5112 !is_constructible_string_type<BasicJsonType, ConstructibleArrayType>::value&&
5113 !std::is_same<ConstructibleArrayType, typename BasicJsonType::binary_t>::value&&
5114 !is_basic_json<ConstructibleArrayType>::value,
5115 int > = 0 >
5116 auto from_json(const BasicJsonType& j, ConstructibleArrayType& arr)
5117 -> decltype(from_json_array_impl(j, arr, priority_tag<3> {}),
5118 j.template get<typename ConstructibleArrayType::value_type>(),
5119 void())
5120 {
5121 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
5122 {
5123 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
5124 }
5125
5126 from_json_array_impl(j, arr, priority_tag<3> {});
5127 }
5128
5129 template < typename BasicJsonType, typename T, std::size_t... Idx >
5130 std::array<T, sizeof...(Idx)> from_json_inplace_array_impl(BasicJsonType&& j,
5131 identity_tag<std::array<T, sizeof...(Idx)>> /*unused*/, index_sequence<Idx...> /*unused*/)
5132 {
5133 return { { std::forward<BasicJsonType>(j).at(Idx).template get<T>()... } };
5134 }
5135
5136 template < typename BasicJsonType, typename T, std::size_t N >
5137 auto from_json(BasicJsonType&& j, identity_tag<std::array<T, N>> tag)
5138 -> decltype(from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {}))
5139 {
5140 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
5141 {
5142 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
5143 }
5144
5145 return from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {});
5146 }
5147
5148 template<typename BasicJsonType>
5149 inline void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin)
5150 {
5151 if (JSON_HEDLEY_UNLIKELY(!j.is_binary()))
5152 {
5153 JSON_THROW(type_error::create(302, concat("type must be binary, but is ", j.type_name()), &j));
5154 }
5155
5156 bin = *j.template get_ptr<const typename BasicJsonType::binary_t*>();
5157 }
5158
5159 template<typename BasicJsonType, typename ConstructibleObjectType,
5160 enable_if_t<is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value, int> = 0>
5161 inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
5162 {
5163 if (JSON_HEDLEY_UNLIKELY(!j.is_object()))
5164 {
5165 JSON_THROW(type_error::create(302, concat("type must be object, but is ", j.type_name()), &j));
5166 }
5167
5168 ConstructibleObjectType ret;
5169 const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
5170 using value_type = typename ConstructibleObjectType::value_type;
5171 std::transform(
5172 inner_object->begin(), inner_object->end(),
5173 std::inserter(ret, ret.begin()),
5174 [](typename BasicJsonType::object_t::value_type const & p)
5175 {
5176 return value_type(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
5177 });
5178 obj = std::move(ret);
5179 }
5180
5181 // overload for arithmetic types, not chosen for basic_json template arguments
5182 // (BooleanType, etc..); note: Is it really necessary to provide explicit
5183 // overloads for boolean_t etc. in case of a custom BooleanType which is not
5184 // an arithmetic type?
5185 template < typename BasicJsonType, typename ArithmeticType,
5186 enable_if_t <
5187 std::is_arithmetic<ArithmeticType>::value&&
5188 !std::is_same<ArithmeticType, typename BasicJsonType::number_unsigned_t>::value&&
5189 !std::is_same<ArithmeticType, typename BasicJsonType::number_integer_t>::value&&
5190 !std::is_same<ArithmeticType, typename BasicJsonType::number_float_t>::value&&
5191 !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
5192 int > = 0 >
5193 inline void from_json(const BasicJsonType& j, ArithmeticType& val)
5194 {
5195 switch (static_cast<value_t>(j))
5196 {
5197 case value_t::number_unsigned:
5198 {
5199 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
5200 break;
5201 }
5202 case value_t::number_integer:
5203 {
5204 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
5205 break;
5206 }
5207 case value_t::number_float:
5208 {
5209 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
5210 break;
5211 }
5212 case value_t::boolean:
5213 {
5214 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
5215 break;
5216 }
5217
5218 case value_t::null:
5219 case value_t::object:
5220 case value_t::array:
5221 case value_t::string:
5222 case value_t::binary:
5223 case value_t::discarded:
5224 default:
5225 JSON_THROW(type_error::create(302, concat("type must be number, but is ", j.type_name()), &j));
5226 }
5227 }
5228
5229 template<typename BasicJsonType, typename... Args, std::size_t... Idx>
5230 std::tuple<Args...> from_json_tuple_impl_base(BasicJsonType&& j, index_sequence<Idx...> /*unused*/)
5231 {
5232 return std::make_tuple(std::forward<BasicJsonType>(j).at(Idx).template get<Args>()...);
5233 }
5234
5235 template<typename BasicJsonType>
5236 std::tuple<> from_json_tuple_impl_base(BasicJsonType& /*unused*/, index_sequence<> /*unused*/)
5237 {
5238 return {};
5239 }
5240
5241 template < typename BasicJsonType, class A1, class A2 >
5242 std::pair<A1, A2> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::pair<A1, A2>> /*unused*/, priority_tag<0> /*unused*/)
5243 {
5244 return {std::forward<BasicJsonType>(j).at(0).template get<A1>(),
5245 std::forward<BasicJsonType>(j).at(1).template get<A2>()};
5246 }
5247
5248 template<typename BasicJsonType, typename A1, typename A2>
5249 inline void from_json_tuple_impl(BasicJsonType&& j, std::pair<A1, A2>& p, priority_tag<1> /*unused*/)
5250 {
5251 p = from_json_tuple_impl(std::forward<BasicJsonType>(j), identity_tag<std::pair<A1, A2>> {}, priority_tag<0> {});
5252 }
5253
5254 template<typename BasicJsonType, typename... Args>
5255 std::tuple<Args...> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::tuple<Args...>> /*unused*/, priority_tag<2> /*unused*/)
5256 {
5257 return from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
5258 }
5259
5260 template<typename BasicJsonType, typename... Args>
5261 inline void from_json_tuple_impl(BasicJsonType&& j, std::tuple<Args...>& t, priority_tag<3> /*unused*/)
5262 {
5263 t = from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
5264 }
5265
5266 template<typename BasicJsonType, typename TupleRelated>
5267 auto from_json(BasicJsonType&& j, TupleRelated&& t)
5268 -> decltype(from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {}))
5269 {
5270 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
5271 {
5272 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
5273 }
5274
5275 return from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {});
5276 }
5277
5278 template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator,
5279 typename = enable_if_t < !std::is_constructible <
5280 typename BasicJsonType::string_t, Key >::value >>
5281 inline void from_json(const BasicJsonType& j, std::map<Key, Value, Compare, Allocator>& m)
5282 {
5283 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
5284 {
5285 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
5286 }
5287 m.clear();
5288 for (const auto& p : j)
5289 {
5290 if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
5291 {
5292 JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
5293 }
5294 m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
5295 }
5296 }
5297
5298 template < typename BasicJsonType, typename Key, typename Value, typename Hash, typename KeyEqual, typename Allocator,
5299 typename = enable_if_t < !std::is_constructible <
5300 typename BasicJsonType::string_t, Key >::value >>
5301 inline void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>& m)
5302 {
5303 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
5304 {
5305 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
5306 }
5307 m.clear();
5308 for (const auto& p : j)
5309 {
5310 if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
5311 {
5312 JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
5313 }
5314 m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
5315 }
5316 }
5317
5318 #if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
5319 template<typename BasicJsonType>
5320 inline void from_json(const BasicJsonType& j, std_fs::path& p)
5321 {
5322 if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
5323 {
5324 JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
5325 }
5326 const auto& s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
5327 #ifdef JSON_HAS_CPP_20
5328 p = std_fs::path(std::u8string_view(reinterpret_cast<const char8_t*>(s.data()), s.size()));
5329 #else
5330 p = std_fs::u8path(s); // accepts UTF-8 encoded std::string in C++17, deprecated in C++20
5331 #endif
5332 }
5333 #endif
5334
5335 struct from_json_fn
5336 {
5337 template<typename BasicJsonType, typename T>
5338 auto operator()(const BasicJsonType& j, T&& val) const
5339 noexcept(noexcept(from_json(j, std::forward<T>(val))))
5340 -> decltype(from_json(j, std::forward<T>(val)))
5341 {
5342 return from_json(j, std::forward<T>(val));
5343 }
5344 };
5345
5346 } // namespace detail
5347
5348 #ifndef JSON_HAS_CPP_17
5349 /// namespace to hold default `from_json` function
5350 /// to see why this is required:
5351 /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
5352 namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
5353 {
5354 #endif
5355 JSON_INLINE_VARIABLE constexpr const auto& from_json = // NOLINT(misc-definitions-in-headers)
5356 detail::static_const<detail::from_json_fn>::value;
5357 #ifndef JSON_HAS_CPP_17
5358 } // namespace
5359 #endif
5360
5361 NLOHMANN_JSON_NAMESPACE_END
5362
5363 // #include <nlohmann/detail/conversions/to_json.hpp>
5364 // __ _____ _____ _____
5365 // __| | __| | | | JSON for Modern C++
5366 // | | |__ | | | | | | version 3.12.0
5367 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
5368 //
5369 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
5370 // SPDX-License-Identifier: MIT
5371
5372
5373
5374 // #include <nlohmann/detail/macro_scope.hpp>
5375 // JSON_HAS_CPP_17
5376 #ifdef JSON_HAS_CPP_17
5377 #include <optional> // optional
5378 #endif
5379
5380 #include <algorithm> // copy
5381 #include <iterator> // begin, end
5382 #include <string> // string
5383 #include <tuple> // tuple, get
5384 #include <type_traits> // is_same, is_constructible, is_floating_point, is_enum, underlying_type
5385 #include <utility> // move, forward, declval, pair
5386 #include <valarray> // valarray
5387 #include <vector> // vector
5388
5389 // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
5390 // __ _____ _____ _____
5391 // __| | __| | | | JSON for Modern C++
5392 // | | |__ | | | | | | version 3.12.0
5393 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
5394 //
5395 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
5396 // SPDX-License-Identifier: MIT
5397
5398
5399
5400 #include <cstddef> // size_t
5401 #include <iterator> // forward_iterator_tag
5402 #include <tuple> // tuple_size, get, tuple_element
5403 #include <utility> // move
5404
5405 #if JSON_HAS_RANGES
5406 #include <ranges> // enable_borrowed_range
5407 #endif
5408
5409 // #include <nlohmann/detail/abi_macros.hpp>
5410
5411 // #include <nlohmann/detail/meta/type_traits.hpp>
5412
5413 // #include <nlohmann/detail/string_utils.hpp>
5414 // __ _____ _____ _____
5415 // __| | __| | | | JSON for Modern C++
5416 // | | |__ | | | | | | version 3.12.0
5417 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
5418 //
5419 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
5420 // SPDX-License-Identifier: MIT
5421
5422
5423
5424 #include <cstddef> // size_t
5425 #include <string> // string, to_string
5426
5427 // #include <nlohmann/detail/abi_macros.hpp>
5428
5429
5430 NLOHMANN_JSON_NAMESPACE_BEGIN
5431 namespace detail
5432 {
5433
5434 template<typename StringType>
5435 void int_to_string(StringType& target, std::size_t value)
5436 {
5437 // For ADL
5438 using std::to_string;
5439 target = to_string(value);
5440 }
5441
5442 template<typename StringType>
5443 StringType to_string(std::size_t value)
5444 {
5445 StringType result;
5446 int_to_string(result, value);
5447 return result;
5448 }
5449
5450 } // namespace detail
5451 NLOHMANN_JSON_NAMESPACE_END
5452
5453 // #include <nlohmann/detail/value_t.hpp>
5454
5455
5456 NLOHMANN_JSON_NAMESPACE_BEGIN
5457 namespace detail
5458 {
5459
5460 template<typename IteratorType> class iteration_proxy_value
5461 {
5462 public:
5463 using difference_type = std::ptrdiff_t;
5464 using value_type = iteration_proxy_value;
5465 using pointer = value_type *;
5466 using reference = value_type &;
5467 using iterator_category = std::forward_iterator_tag;
5468 using string_type = typename std::remove_cv< typename std::remove_reference<decltype( std::declval<IteratorType>().key() ) >::type >::type;
5469
5470 private:
5471 /// the iterator
5472 IteratorType anchor{};
5473 /// an index for arrays (used to create key names)
5474 std::size_t array_index = 0;
5475 /// last stringified array index
5476 mutable std::size_t array_index_last = 0;
5477 /// a string representation of the array index
5478 mutable string_type array_index_str = "0";
5479 /// an empty string (to return a reference for primitive values)
5480 string_type empty_str{};
5481
5482 public:
5483 explicit iteration_proxy_value() = default;
5484 explicit iteration_proxy_value(IteratorType it, std::size_t array_index_ = 0)
5485 noexcept(std::is_nothrow_move_constructible<IteratorType>::value
5486 && std::is_nothrow_default_constructible<string_type>::value)
5487 : anchor(std::move(it))
5488 , array_index(array_index_)
5489 {}
5490
5491 iteration_proxy_value(iteration_proxy_value const&) = default;
5492 iteration_proxy_value& operator=(iteration_proxy_value const&) = default;
5493 // older GCCs are a bit fussy and require explicit noexcept specifiers on defaulted functions
5494 iteration_proxy_value(iteration_proxy_value&&)
5495 noexcept(std::is_nothrow_move_constructible<IteratorType>::value
5496 && std::is_nothrow_move_constructible<string_type>::value) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor,cppcoreguidelines-noexcept-move-operations)
5497 iteration_proxy_value& operator=(iteration_proxy_value&&)
5498 noexcept(std::is_nothrow_move_assignable<IteratorType>::value
5499 && std::is_nothrow_move_assignable<string_type>::value) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor,cppcoreguidelines-noexcept-move-operations)
5500 ~iteration_proxy_value() = default;
5501
5502 /// dereference operator (needed for range-based for)
5503 const iteration_proxy_value& operator*() const
5504 {
5505 return *this;
5506 }
5507
5508 /// increment operator (needed for range-based for)
5509 iteration_proxy_value& operator++()
5510 {
5511 ++anchor;
5512 ++array_index;
5513
5514 return *this;
5515 }
5516
5517 iteration_proxy_value operator++(int)& // NOLINT(cert-dcl21-cpp)
5518 {
5519 auto tmp = iteration_proxy_value(anchor, array_index);
5520 ++anchor;
5521 ++array_index;
5522 return tmp;
5523 }
5524
5525 /// equality operator (needed for InputIterator)
5526 bool operator==(const iteration_proxy_value& o) const
5527 {
5528 return anchor == o.anchor;
5529 }
5530
5531 /// inequality operator (needed for range-based for)
5532 bool operator!=(const iteration_proxy_value& o) const
5533 {
5534 return anchor != o.anchor;
5535 }
5536
5537 /// return key of the iterator
5538 const string_type& key() const
5539 {
5540 JSON_ASSERT(anchor.m_object != nullptr);
5541
5542 switch (anchor.m_object->type())
5543 {
5544 // use integer array index as key
5545 case value_t::array:
5546 {
5547 if (array_index != array_index_last)
5548 {
5549 int_to_string( array_index_str, array_index );
5550 array_index_last = array_index;
5551 }
5552 return array_index_str;
5553 }
5554
5555 // use key from the object
5556 case value_t::object:
5557 return anchor.key();
5558
5559 // use an empty key for all primitive types
5560 case value_t::null:
5561 case value_t::string:
5562 case value_t::boolean:
5563 case value_t::number_integer:
5564 case value_t::number_unsigned:
5565 case value_t::number_float:
5566 case value_t::binary:
5567 case value_t::discarded:
5568 default:
5569 return empty_str;
5570 }
5571 }
5572
5573 /// return value of the iterator
5574 typename IteratorType::reference value() const
5575 {
5576 return anchor.value();
5577 }
5578 };
5579
5580 /// proxy class for the items() function
5581 template<typename IteratorType> class iteration_proxy
5582 {
5583 private:
5584 /// the container to iterate
5585 typename IteratorType::pointer container = nullptr;
5586
5587 public:
5588 explicit iteration_proxy() = default;
5589
5590 /// construct iteration proxy from a container
5591 explicit iteration_proxy(typename IteratorType::reference cont) noexcept
5592 : container(&cont) {}
5593
5594 iteration_proxy(iteration_proxy const&) = default;
5595 iteration_proxy& operator=(iteration_proxy const&) = default;
5596 iteration_proxy(iteration_proxy&&) noexcept = default;
5597 iteration_proxy& operator=(iteration_proxy&&) noexcept = default;
5598 ~iteration_proxy() = default;
5599
5600 /// return iterator begin (needed for range-based for)
5601 iteration_proxy_value<IteratorType> begin() const noexcept
5602 {
5603 return iteration_proxy_value<IteratorType>(container->begin());
5604 }
5605
5606 /// return iterator end (needed for range-based for)
5607 iteration_proxy_value<IteratorType> end() const noexcept
5608 {
5609 return iteration_proxy_value<IteratorType>(container->end());
5610 }
5611 };
5612
5613 // Structured Bindings Support
5614 // For further reference see https://blog.tartanllama.xyz/structured-bindings/
5615 // And see https://github.com/nlohmann/json/pull/1391
5616 template<std::size_t N, typename IteratorType, enable_if_t<N == 0, int> = 0>
5617 auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.key())
5618 {
5619 return i.key();
5620 }
5621 // Structured Bindings Support
5622 // For further reference see https://blog.tartanllama.xyz/structured-bindings/
5623 // And see https://github.com/nlohmann/json/pull/1391
5624 template<std::size_t N, typename IteratorType, enable_if_t<N == 1, int> = 0>
5625 auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.value())
5626 {
5627 return i.value();
5628 }
5629
5630 } // namespace detail
5631 NLOHMANN_JSON_NAMESPACE_END
5632
5633 // The Addition to the STD Namespace is required to add
5634 // Structured Bindings Support to the iteration_proxy_value class
5635 // For further reference see https://blog.tartanllama.xyz/structured-bindings/
5636 // And see https://github.com/nlohmann/json/pull/1391
5637 namespace std
5638 {
5639
5640 #if defined(__clang__)
5641 // Fix: https://github.com/nlohmann/json/issues/1401
5642 #pragma clang diagnostic push
5643 #pragma clang diagnostic ignored "-Wmismatched-tags"
5644 #endif
5645 template<typename IteratorType>
5646 class tuple_size<::nlohmann::detail::iteration_proxy_value<IteratorType>> // NOLINT(cert-dcl58-cpp)
5647 : public std::integral_constant<std::size_t, 2> {};
5648
5649 template<std::size_t N, typename IteratorType>
5650 class tuple_element<N, ::nlohmann::detail::iteration_proxy_value<IteratorType >> // NOLINT(cert-dcl58-cpp)
5651 {
5652 public:
5653 using type = decltype(
5654 get<N>(std::declval <
5655 ::nlohmann::detail::iteration_proxy_value<IteratorType >> ()));
5656 };
5657 #if defined(__clang__)
5658 #pragma clang diagnostic pop
5659 #endif
5660
5661 } // namespace std
5662
5663 #if JSON_HAS_RANGES
5664 template <typename IteratorType>
5665 inline constexpr bool ::std::ranges::enable_borrowed_range<::nlohmann::detail::iteration_proxy<IteratorType>> = true;
5666 #endif
5667
5668 // #include <nlohmann/detail/meta/cpp_future.hpp>
5669
5670 // #include <nlohmann/detail/meta/std_fs.hpp>
5671
5672 // #include <nlohmann/detail/meta/type_traits.hpp>
5673
5674 // #include <nlohmann/detail/value_t.hpp>
5675
5676
5677 NLOHMANN_JSON_NAMESPACE_BEGIN
5678 namespace detail
5679 {
5680
5681 //////////////////
5682 // constructors //
5683 //////////////////
5684
5685 /*
5686 * Note all external_constructor<>::construct functions need to call
5687 * j.m_data.m_value.destroy(j.m_data.m_type) to avoid a memory leak in case j contains an
5688 * allocated value (e.g., a string). See bug issue
5689 * https://github.com/nlohmann/json/issues/2865 for more information.
5690 */
5691
5692 template<value_t> struct external_constructor;
5693
5694 template<>
5695 struct external_constructor<value_t::boolean>
5696 {
5697 template<typename BasicJsonType>
5698 static void construct(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept
5699 {
5700 j.m_data.m_value.destroy(j.m_data.m_type);
5701 j.m_data.m_type = value_t::boolean;
5702 j.m_data.m_value = b;
5703 j.assert_invariant();
5704 }
5705 };
5706
5707 template<>
5708 struct external_constructor<value_t::string>
5709 {
5710 template<typename BasicJsonType>
5711 static void construct(BasicJsonType& j, const typename BasicJsonType::string_t& s)
5712 {
5713 j.m_data.m_value.destroy(j.m_data.m_type);
5714 j.m_data.m_type = value_t::string;
5715 j.m_data.m_value = s;
5716 j.assert_invariant();
5717 }
5718
5719 template<typename BasicJsonType>
5720 static void construct(BasicJsonType& j, typename BasicJsonType::string_t&& s)
5721 {
5722 j.m_data.m_value.destroy(j.m_data.m_type);
5723 j.m_data.m_type = value_t::string;
5724 j.m_data.m_value = std::move(s);
5725 j.assert_invariant();
5726 }
5727
5728 template < typename BasicJsonType, typename CompatibleStringType,
5729 enable_if_t < !std::is_same<CompatibleStringType, typename BasicJsonType::string_t>::value,
5730 int > = 0 >
5731 static void construct(BasicJsonType& j, const CompatibleStringType& str)
5732 {
5733 j.m_data.m_value.destroy(j.m_data.m_type);
5734 j.m_data.m_type = value_t::string;
5735 j.m_data.m_value.string = j.template create<typename BasicJsonType::string_t>(str);
5736 j.assert_invariant();
5737 }
5738 };
5739
5740 template<>
5741 struct external_constructor<value_t::binary>
5742 {
5743 template<typename BasicJsonType>
5744 static void construct(BasicJsonType& j, const typename BasicJsonType::binary_t& b)
5745 {
5746 j.m_data.m_value.destroy(j.m_data.m_type);
5747 j.m_data.m_type = value_t::binary;
5748 j.m_data.m_value = typename BasicJsonType::binary_t(b);
5749 j.assert_invariant();
5750 }
5751
5752 template<typename BasicJsonType>
5753 static void construct(BasicJsonType& j, typename BasicJsonType::binary_t&& b)
5754 {
5755 j.m_data.m_value.destroy(j.m_data.m_type);
5756 j.m_data.m_type = value_t::binary;
5757 j.m_data.m_value = typename BasicJsonType::binary_t(std::move(b));
5758 j.assert_invariant();
5759 }
5760 };
5761
5762 template<>
5763 struct external_constructor<value_t::number_float>
5764 {
5765 template<typename BasicJsonType>
5766 static void construct(BasicJsonType& j, typename BasicJsonType::number_float_t val) noexcept
5767 {
5768 j.m_data.m_value.destroy(j.m_data.m_type);
5769 j.m_data.m_type = value_t::number_float;
5770 j.m_data.m_value = val;
5771 j.assert_invariant();
5772 }
5773 };
5774
5775 template<>
5776 struct external_constructor<value_t::number_unsigned>
5777 {
5778 template<typename BasicJsonType>
5779 static void construct(BasicJsonType& j, typename BasicJsonType::number_unsigned_t val) noexcept
5780 {
5781 j.m_data.m_value.destroy(j.m_data.m_type);
5782 j.m_data.m_type = value_t::number_unsigned;
5783 j.m_data.m_value = val;
5784 j.assert_invariant();
5785 }
5786 };
5787
5788 template<>
5789 struct external_constructor<value_t::number_integer>
5790 {
5791 template<typename BasicJsonType>
5792 static void construct(BasicJsonType& j, typename BasicJsonType::number_integer_t val) noexcept
5793 {
5794 j.m_data.m_value.destroy(j.m_data.m_type);
5795 j.m_data.m_type = value_t::number_integer;
5796 j.m_data.m_value = val;
5797 j.assert_invariant();
5798 }
5799 };
5800
5801 template<>
5802 struct external_constructor<value_t::array>
5803 {
5804 template<typename BasicJsonType>
5805 static void construct(BasicJsonType& j, const typename BasicJsonType::array_t& arr)
5806 {
5807 j.m_data.m_value.destroy(j.m_data.m_type);
5808 j.m_data.m_type = value_t::array;
5809 j.m_data.m_value = arr;
5810 j.set_parents();
5811 j.assert_invariant();
5812 }
5813
5814 template<typename BasicJsonType>
5815 static void construct(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
5816 {
5817 j.m_data.m_value.destroy(j.m_data.m_type);
5818 j.m_data.m_type = value_t::array;
5819 j.m_data.m_value = std::move(arr);
5820 j.set_parents();
5821 j.assert_invariant();
5822 }
5823
5824 template < typename BasicJsonType, typename CompatibleArrayType,
5825 enable_if_t < !std::is_same<CompatibleArrayType, typename BasicJsonType::array_t>::value,
5826 int > = 0 >
5827 static void construct(BasicJsonType& j, const CompatibleArrayType& arr)
5828 {
5829 using std::begin;
5830 using std::end;
5831
5832 j.m_data.m_value.destroy(j.m_data.m_type);
5833 j.m_data.m_type = value_t::array;
5834 j.m_data.m_value.array = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
5835 j.set_parents();
5836 j.assert_invariant();
5837 }
5838
5839 template<typename BasicJsonType>
5840 static void construct(BasicJsonType& j, const std::vector<bool>& arr)
5841 {
5842 j.m_data.m_value.destroy(j.m_data.m_type);
5843 j.m_data.m_type = value_t::array;
5844 j.m_data.m_value = value_t::array;
5845 j.m_data.m_value.array->reserve(arr.size());
5846 for (const bool x : arr)
5847 {
5848 j.m_data.m_value.array->push_back(x);
5849 j.set_parent(j.m_data.m_value.array->back());
5850 }
5851 j.assert_invariant();
5852 }
5853
5854 template<typename BasicJsonType, typename T,
5855 enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
5856 static void construct(BasicJsonType& j, const std::valarray<T>& arr)
5857 {
5858 j.m_data.m_value.destroy(j.m_data.m_type);
5859 j.m_data.m_type = value_t::array;
5860 j.m_data.m_value = value_t::array;
5861 j.m_data.m_value.array->resize(arr.size());
5862 if (arr.size() > 0)
5863 {
5864 std::copy(std::begin(arr), std::end(arr), j.m_data.m_value.array->begin());
5865 }
5866 j.set_parents();
5867 j.assert_invariant();
5868 }
5869 };
5870
5871 template<>
5872 struct external_constructor<value_t::object>
5873 {
5874 template<typename BasicJsonType>
5875 static void construct(BasicJsonType& j, const typename BasicJsonType::object_t& obj)
5876 {
5877 j.m_data.m_value.destroy(j.m_data.m_type);
5878 j.m_data.m_type = value_t::object;
5879 j.m_data.m_value = obj;
5880 j.set_parents();
5881 j.assert_invariant();
5882 }
5883
5884 template<typename BasicJsonType>
5885 static void construct(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
5886 {
5887 j.m_data.m_value.destroy(j.m_data.m_type);
5888 j.m_data.m_type = value_t::object;
5889 j.m_data.m_value = std::move(obj);
5890 j.set_parents();
5891 j.assert_invariant();
5892 }
5893
5894 template < typename BasicJsonType, typename CompatibleObjectType,
5895 enable_if_t < !std::is_same<CompatibleObjectType, typename BasicJsonType::object_t>::value, int > = 0 >
5896 static void construct(BasicJsonType& j, const CompatibleObjectType& obj)
5897 {
5898 using std::begin;
5899 using std::end;
5900
5901 j.m_data.m_value.destroy(j.m_data.m_type);
5902 j.m_data.m_type = value_t::object;
5903 j.m_data.m_value.object = j.template create<typename BasicJsonType::object_t>(begin(obj), end(obj));
5904 j.set_parents();
5905 j.assert_invariant();
5906 }
5907 };
5908
5909 /////////////
5910 // to_json //
5911 /////////////
5912
5913 #ifdef JSON_HAS_CPP_17
5914 template<typename BasicJsonType, typename T,
5915 enable_if_t<std::is_constructible<BasicJsonType, T>::value, int> = 0>
5916 void to_json(BasicJsonType& j, const std::optional<T>& opt) noexcept
5917 {
5918 if (opt.has_value())
5919 {
5920 j = *opt;
5921 }
5922 else
5923 {
5924 j = nullptr;
5925 }
5926 }
5927 #endif
5928
5929 template<typename BasicJsonType, typename T,
5930 enable_if_t<std::is_same<T, typename BasicJsonType::boolean_t>::value, int> = 0>
5931 inline void to_json(BasicJsonType& j, T b) noexcept
5932 {
5933 external_constructor<value_t::boolean>::construct(j, b);
5934 }
5935
5936 template < typename BasicJsonType, typename BoolRef,
5937 enable_if_t <
5938 ((std::is_same<std::vector<bool>::reference, BoolRef>::value
5939 && !std::is_same <std::vector<bool>::reference, typename BasicJsonType::boolean_t&>::value)
5940 || (std::is_same<std::vector<bool>::const_reference, BoolRef>::value
5941 && !std::is_same <detail::uncvref_t<std::vector<bool>::const_reference>,
5942 typename BasicJsonType::boolean_t >::value))
5943 && std::is_convertible<const BoolRef&, typename BasicJsonType::boolean_t>::value, int > = 0 >
5944 inline void to_json(BasicJsonType& j, const BoolRef& b) noexcept
5945 {
5946 external_constructor<value_t::boolean>::construct(j, static_cast<typename BasicJsonType::boolean_t>(b));
5947 }
5948
5949 template<typename BasicJsonType, typename CompatibleString,
5950 enable_if_t<std::is_constructible<typename BasicJsonType::string_t, CompatibleString>::value, int> = 0>
5951 inline void to_json(BasicJsonType& j, const CompatibleString& s)
5952 {
5953 external_constructor<value_t::string>::construct(j, s);
5954 }
5955
5956 template<typename BasicJsonType>
5957 inline void to_json(BasicJsonType& j, typename BasicJsonType::string_t&& s)
5958 {
5959 external_constructor<value_t::string>::construct(j, std::move(s));
5960 }
5961
5962 template<typename BasicJsonType, typename FloatType,
5963 enable_if_t<std::is_floating_point<FloatType>::value, int> = 0>
5964 inline void to_json(BasicJsonType& j, FloatType val) noexcept
5965 {
5966 external_constructor<value_t::number_float>::construct(j, static_cast<typename BasicJsonType::number_float_t>(val));
5967 }
5968
5969 template<typename BasicJsonType, typename CompatibleNumberUnsignedType,
5970 enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_unsigned_t, CompatibleNumberUnsignedType>::value, int> = 0>
5971 inline void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept
5972 {
5973 external_constructor<value_t::number_unsigned>::construct(j, static_cast<typename BasicJsonType::number_unsigned_t>(val));
5974 }
5975
5976 template<typename BasicJsonType, typename CompatibleNumberIntegerType,
5977 enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_integer_t, CompatibleNumberIntegerType>::value, int> = 0>
5978 inline void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept
5979 {
5980 external_constructor<value_t::number_integer>::construct(j, static_cast<typename BasicJsonType::number_integer_t>(val));
5981 }
5982
5983 #if !JSON_DISABLE_ENUM_SERIALIZATION
5984 template<typename BasicJsonType, typename EnumType,
5985 enable_if_t<std::is_enum<EnumType>::value, int> = 0>
5986 inline void to_json(BasicJsonType& j, EnumType e) noexcept
5987 {
5988 using underlying_type = typename std::underlying_type<EnumType>::type;
5989 static constexpr value_t integral_value_t = std::is_unsigned<underlying_type>::value ? value_t::number_unsigned : value_t::number_integer;
5990 external_constructor<integral_value_t>::construct(j, static_cast<underlying_type>(e));
5991 }
5992 #endif // JSON_DISABLE_ENUM_SERIALIZATION
5993
5994 template<typename BasicJsonType>
5995 inline void to_json(BasicJsonType& j, const std::vector<bool>& e)
5996 {
5997 external_constructor<value_t::array>::construct(j, e);
5998 }
5999
6000 template < typename BasicJsonType, typename CompatibleArrayType,
6001 enable_if_t < is_compatible_array_type<BasicJsonType,
6002 CompatibleArrayType>::value&&
6003 !is_compatible_object_type<BasicJsonType, CompatibleArrayType>::value&&
6004 !is_compatible_string_type<BasicJsonType, CompatibleArrayType>::value&&
6005 !std::is_same<typename BasicJsonType::binary_t, CompatibleArrayType>::value&&
6006 !is_basic_json<CompatibleArrayType>::value,
6007 int > = 0 >
6008 inline void to_json(BasicJsonType& j, const CompatibleArrayType& arr)
6009 {
6010 external_constructor<value_t::array>::construct(j, arr);
6011 }
6012
6013 template<typename BasicJsonType>
6014 inline void to_json(BasicJsonType& j, const typename BasicJsonType::binary_t& bin)
6015 {
6016 external_constructor<value_t::binary>::construct(j, bin);
6017 }
6018
6019 template<typename BasicJsonType, typename T,
6020 enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
6021 inline void to_json(BasicJsonType& j, const std::valarray<T>& arr)
6022 {
6023 external_constructor<value_t::array>::construct(j, std::move(arr));
6024 }
6025
6026 template<typename BasicJsonType>
6027 inline void to_json(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
6028 {
6029 external_constructor<value_t::array>::construct(j, std::move(arr));
6030 }
6031
6032 template < typename BasicJsonType, typename CompatibleObjectType,
6033 enable_if_t < is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value&& !is_basic_json<CompatibleObjectType>::value, int > = 0 >
6034 inline void to_json(BasicJsonType& j, const CompatibleObjectType& obj)
6035 {
6036 external_constructor<value_t::object>::construct(j, obj);
6037 }
6038
6039 template<typename BasicJsonType>
6040 inline void to_json(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
6041 {
6042 external_constructor<value_t::object>::construct(j, std::move(obj));
6043 }
6044
6045 template <
6046 typename BasicJsonType, typename T, std::size_t N,
6047 enable_if_t < !std::is_constructible<typename BasicJsonType::string_t,
6048 const T(&)[N]>::value, // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
6049 int > = 0 >
6050 inline void to_json(BasicJsonType& j, const T(&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
6051 {
6052 external_constructor<value_t::array>::construct(j, arr);
6053 }
6054
6055 template < typename BasicJsonType, typename T1, typename T2, enable_if_t < std::is_constructible<BasicJsonType, T1>::value&& std::is_constructible<BasicJsonType, T2>::value, int > = 0 >
6056 inline void to_json(BasicJsonType& j, const std::pair<T1, T2>& p)
6057 {
6058 j = { p.first, p.second };
6059 }
6060
6061 // for https://github.com/nlohmann/json/pull/1134
6062 template<typename BasicJsonType, typename T,
6063 enable_if_t<std::is_same<T, iteration_proxy_value<typename BasicJsonType::iterator>>::value, int> = 0>
6064 inline void to_json(BasicJsonType& j, const T& b)
6065 {
6066 j = { {b.key(), b.value()} };
6067 }
6068
6069 template<typename BasicJsonType, typename Tuple, std::size_t... Idx>
6070 inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence<Idx...> /*unused*/)
6071 {
6072 j = { std::get<Idx>(t)... };
6073 }
6074
6075 template<typename BasicJsonType, typename Tuple>
6076 inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& /*unused*/, index_sequence<> /*unused*/)
6077 {
6078 using array_t = typename BasicJsonType::array_t;
6079 j = array_t();
6080 }
6081
6082 template<typename BasicJsonType, typename T, enable_if_t<is_constructible_tuple<BasicJsonType, T>::value, int > = 0>
6083 inline void to_json(BasicJsonType& j, const T& t)
6084 {
6085 to_json_tuple_impl(j, t, make_index_sequence<std::tuple_size<T>::value> {});
6086 }
6087
6088 #if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
6089 template<typename BasicJsonType>
6090 inline void to_json(BasicJsonType& j, const std_fs::path& p)
6091 {
6092 #ifdef JSON_HAS_CPP_20
6093 const std::u8string s = p.u8string();
6094 j = std::string(s.begin(), s.end());
6095 #else
6096 j = p.u8string(); // returns std::string in C++17
6097 #endif
6098 }
6099 #endif
6100
6101 struct to_json_fn
6102 {
6103 template<typename BasicJsonType, typename T>
6104 auto operator()(BasicJsonType& j, T&& val) const noexcept(noexcept(to_json(j, std::forward<T>(val))))
6105 -> decltype(to_json(j, std::forward<T>(val)), void())
6106 {
6107 return to_json(j, std::forward<T>(val));
6108 }
6109 };
6110 } // namespace detail
6111
6112 #ifndef JSON_HAS_CPP_17
6113 /// namespace to hold default `to_json` function
6114 /// to see why this is required:
6115 /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
6116 namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
6117 {
6118 #endif
6119 JSON_INLINE_VARIABLE constexpr const auto& to_json = // NOLINT(misc-definitions-in-headers)
6120 detail::static_const<detail::to_json_fn>::value;
6121 #ifndef JSON_HAS_CPP_17
6122 } // namespace
6123 #endif
6124
6125 NLOHMANN_JSON_NAMESPACE_END
6126
6127 // #include <nlohmann/detail/meta/identity_tag.hpp>
6128
6129
6130 NLOHMANN_JSON_NAMESPACE_BEGIN
6131
6132 /// @sa https://json.nlohmann.me/api/adl_serializer/
6133 template<typename ValueType, typename>
6134 struct adl_serializer
6135 {
6136 /// @brief convert a JSON value to any value type
6137 /// @sa https://json.nlohmann.me/api/adl_serializer/from_json/
6138 template<typename BasicJsonType, typename TargetType = ValueType>
6139 static auto from_json(BasicJsonType && j, TargetType& val) noexcept(
6140 noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), val)))
6141 -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), val), void())
6142 {
6143 ::nlohmann::from_json(std::forward<BasicJsonType>(j), val);
6144 }
6145
6146 /// @brief convert a JSON value to any value type
6147 /// @sa https://json.nlohmann.me/api/adl_serializer/from_json/
6148 template<typename BasicJsonType, typename TargetType = ValueType>
6149 static auto from_json(BasicJsonType && j) noexcept(
6150 noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {})))
6151 -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {}))
6152 {
6153 return ::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {});
6154 }
6155
6156 /// @brief convert any value type to a JSON value
6157 /// @sa https://json.nlohmann.me/api/adl_serializer/to_json/
6158 template<typename BasicJsonType, typename TargetType = ValueType>
6159 static auto to_json(BasicJsonType& j, TargetType && val) noexcept(
6160 noexcept(::nlohmann::to_json(j, std::forward<TargetType>(val))))
6161 -> decltype(::nlohmann::to_json(j, std::forward<TargetType>(val)), void())
6162 {
6163 ::nlohmann::to_json(j, std::forward<TargetType>(val));
6164 }
6165 };
6166
6167 NLOHMANN_JSON_NAMESPACE_END
6168
6169 // #include <nlohmann/byte_container_with_subtype.hpp>
6170 // __ _____ _____ _____
6171 // __| | __| | | | JSON for Modern C++
6172 // | | |__ | | | | | | version 3.12.0
6173 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
6174 //
6175 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
6176 // SPDX-License-Identifier: MIT
6177
6178
6179
6180 #include <cstdint> // uint8_t, uint64_t
6181 #include <tuple> // tie
6182 #include <utility> // move
6183
6184 // #include <nlohmann/detail/abi_macros.hpp>
6185
6186
6187 NLOHMANN_JSON_NAMESPACE_BEGIN
6188
6189 /// @brief an internal type for a backed binary type
6190 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/
6191 template<typename BinaryType>
6192 class byte_container_with_subtype : public BinaryType
6193 {
6194 public:
6195 using container_type = BinaryType;
6196 using subtype_type = std::uint64_t;
6197
6198 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
6199 byte_container_with_subtype() noexcept(noexcept(container_type()))
6200 : container_type()
6201 {}
6202
6203 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
6204 byte_container_with_subtype(const container_type& b) noexcept(noexcept(container_type(b)))
6205 : container_type(b)
6206 {}
6207
6208 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
6209 byte_container_with_subtype(container_type&& b) noexcept(noexcept(container_type(std::move(b))))
6210 : container_type(std::move(b))
6211 {}
6212
6213 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
6214 byte_container_with_subtype(const container_type& b, subtype_type subtype_) noexcept(noexcept(container_type(b)))
6215 : container_type(b)
6216 , m_subtype(subtype_)
6217 , m_has_subtype(true)
6218 {}
6219
6220 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
6221 byte_container_with_subtype(container_type&& b, subtype_type subtype_) noexcept(noexcept(container_type(std::move(b))))
6222 : container_type(std::move(b))
6223 , m_subtype(subtype_)
6224 , m_has_subtype(true)
6225 {}
6226
6227 bool operator==(const byte_container_with_subtype& rhs) const
6228 {
6229 return std::tie(static_cast<const BinaryType&>(*this), m_subtype, m_has_subtype) ==
6230 std::tie(static_cast<const BinaryType&>(rhs), rhs.m_subtype, rhs.m_has_subtype);
6231 }
6232
6233 bool operator!=(const byte_container_with_subtype& rhs) const
6234 {
6235 return !(rhs == *this);
6236 }
6237
6238 /// @brief sets the binary subtype
6239 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/set_subtype/
6240 void set_subtype(subtype_type subtype_) noexcept
6241 {
6242 m_subtype = subtype_;
6243 m_has_subtype = true;
6244 }
6245
6246 /// @brief return the binary subtype
6247 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/subtype/
6248 constexpr subtype_type subtype() const noexcept
6249 {
6250 return m_has_subtype ? m_subtype : static_cast<subtype_type>(-1);
6251 }
6252
6253 /// @brief return whether the value has a subtype
6254 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/has_subtype/
6255 constexpr bool has_subtype() const noexcept
6256 {
6257 return m_has_subtype;
6258 }
6259
6260 /// @brief clears the binary subtype
6261 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/clear_subtype/
6262 void clear_subtype() noexcept
6263 {
6264 m_subtype = 0;
6265 m_has_subtype = false;
6266 }
6267
6268 private:
6269 subtype_type m_subtype = 0;
6270 bool m_has_subtype = false;
6271 };
6272
6273 NLOHMANN_JSON_NAMESPACE_END
6274
6275 // #include <nlohmann/detail/conversions/from_json.hpp>
6276
6277 // #include <nlohmann/detail/conversions/to_json.hpp>
6278
6279 // #include <nlohmann/detail/exceptions.hpp>
6280
6281 // #include <nlohmann/detail/hash.hpp>
6282 // __ _____ _____ _____
6283 // __| | __| | | | JSON for Modern C++
6284 // | | |__ | | | | | | version 3.12.0
6285 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
6286 //
6287 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
6288 // SPDX-License-Identifier: MIT
6289
6290
6291
6292 #include <cstdint> // uint8_t
6293 #include <cstddef> // size_t
6294 #include <functional> // hash
6295
6296 // #include <nlohmann/detail/abi_macros.hpp>
6297
6298 // #include <nlohmann/detail/value_t.hpp>
6299
6300
6301 NLOHMANN_JSON_NAMESPACE_BEGIN
6302 namespace detail
6303 {
6304
6305 // boost::hash_combine
6306 inline std::size_t combine(std::size_t seed, std::size_t h) noexcept
6307 {
6308 seed ^= h + 0x9e3779b9 + (seed << 6U) + (seed >> 2U);
6309 return seed;
6310 }
6311
6312 /*!
6313 @brief hash a JSON value
6314
6315 The hash function tries to rely on std::hash where possible. Furthermore, the
6316 type of the JSON value is taken into account to have different hash values for
6317 null, 0, 0U, and false, etc.
6318
6319 @tparam BasicJsonType basic_json specialization
6320 @param j JSON value to hash
6321 @return hash value of j
6322 */
6323 template<typename BasicJsonType>
6324 std::size_t hash(const BasicJsonType& j)
6325 {
6326 using string_t = typename BasicJsonType::string_t;
6327 using number_integer_t = typename BasicJsonType::number_integer_t;
6328 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
6329 using number_float_t = typename BasicJsonType::number_float_t;
6330
6331 const auto type = static_cast<std::size_t>(j.type());
6332 switch (j.type())
6333 {
6334 case BasicJsonType::value_t::null:
6335 case BasicJsonType::value_t::discarded:
6336 {
6337 return combine(type, 0);
6338 }
6339
6340 case BasicJsonType::value_t::object:
6341 {
6342 auto seed = combine(type, j.size());
6343 for (const auto& element : j.items())
6344 {
6345 const auto h = std::hash<string_t> {}(element.key());
6346 seed = combine(seed, h);
6347 seed = combine(seed, hash(element.value()));
6348 }
6349 return seed;
6350 }
6351
6352 case BasicJsonType::value_t::array:
6353 {
6354 auto seed = combine(type, j.size());
6355 for (const auto& element : j)
6356 {
6357 seed = combine(seed, hash(element));
6358 }
6359 return seed;
6360 }
6361
6362 case BasicJsonType::value_t::string:
6363 {
6364 const auto h = std::hash<string_t> {}(j.template get_ref<const string_t&>());
6365 return combine(type, h);
6366 }
6367
6368 case BasicJsonType::value_t::boolean:
6369 {
6370 const auto h = std::hash<bool> {}(j.template get<bool>());
6371 return combine(type, h);
6372 }
6373
6374 case BasicJsonType::value_t::number_integer:
6375 {
6376 const auto h = std::hash<number_integer_t> {}(j.template get<number_integer_t>());
6377 return combine(type, h);
6378 }
6379
6380 case BasicJsonType::value_t::number_unsigned:
6381 {
6382 const auto h = std::hash<number_unsigned_t> {}(j.template get<number_unsigned_t>());
6383 return combine(type, h);
6384 }
6385
6386 case BasicJsonType::value_t::number_float:
6387 {
6388 const auto h = std::hash<number_float_t> {}(j.template get<number_float_t>());
6389 return combine(type, h);
6390 }
6391
6392 case BasicJsonType::value_t::binary:
6393 {
6394 auto seed = combine(type, j.get_binary().size());
6395 const auto h = std::hash<bool> {}(j.get_binary().has_subtype());
6396 seed = combine(seed, h);
6397 seed = combine(seed, static_cast<std::size_t>(j.get_binary().subtype()));
6398 for (const auto byte : j.get_binary())
6399 {
6400 seed = combine(seed, std::hash<std::uint8_t> {}(byte));
6401 }
6402 return seed;
6403 }
6404
6405 default: // LCOV_EXCL_LINE
6406 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
6407 return 0; // LCOV_EXCL_LINE
6408 }
6409 }
6410
6411 } // namespace detail
6412 NLOHMANN_JSON_NAMESPACE_END
6413
6414 // #include <nlohmann/detail/input/binary_reader.hpp>
6415 // __ _____ _____ _____
6416 // __| | __| | | | JSON for Modern C++
6417 // | | |__ | | | | | | version 3.12.0
6418 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
6419 //
6420 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
6421 // SPDX-License-Identifier: MIT
6422
6423
6424
6425 #include <algorithm> // generate_n
6426 #include <array> // array
6427 #include <cmath> // ldexp
6428 #include <cstddef> // size_t
6429 #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
6430 #include <cstdio> // snprintf
6431 #include <cstring> // memcpy
6432 #include <iterator> // back_inserter
6433 #include <limits> // numeric_limits
6434 #include <string> // char_traits, string
6435 #include <utility> // make_pair, move
6436 #include <vector> // vector
6437 #ifdef __cpp_lib_byteswap
6438 #include <bit> //byteswap
6439 #endif
6440
6441 // #include <nlohmann/detail/exceptions.hpp>
6442
6443 // #include <nlohmann/detail/input/input_adapters.hpp>
6444 // __ _____ _____ _____
6445 // __| | __| | | | JSON for Modern C++
6446 // | | |__ | | | | | | version 3.12.0
6447 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
6448 //
6449 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
6450 // SPDX-License-Identifier: MIT
6451
6452
6453
6454 #include <array> // array
6455 #include <cstddef> // size_t
6456 #include <cstring> // strlen
6457 #include <iterator> // begin, end, iterator_traits, random_access_iterator_tag, distance, next
6458 #include <memory> // shared_ptr, make_shared, addressof
6459 #include <numeric> // accumulate
6460 #include <string> // string, char_traits
6461 #include <type_traits> // enable_if, is_base_of, is_pointer, is_integral, remove_pointer
6462 #include <utility> // pair, declval
6463
6464 #ifndef JSON_NO_IO
6465 #include <cstdio> // FILE *
6466 #include <istream> // istream
6467 #endif // JSON_NO_IO
6468
6469 // #include <nlohmann/detail/exceptions.hpp>
6470
6471 // #include <nlohmann/detail/iterators/iterator_traits.hpp>
6472
6473 // #include <nlohmann/detail/macro_scope.hpp>
6474
6475 // #include <nlohmann/detail/meta/type_traits.hpp>
6476
6477
6478 NLOHMANN_JSON_NAMESPACE_BEGIN
6479 namespace detail
6480 {
6481
6482 /// the supported input formats
6483 enum class input_format_t { json, cbor, msgpack, ubjson, bson, bjdata };
6484
6485 ////////////////////
6486 // input adapters //
6487 ////////////////////
6488
6489 #ifndef JSON_NO_IO
6490 /*!
6491 Input adapter for stdio file access. This adapter read only 1 byte and do not use any
6492 buffer. This adapter is a very low level adapter.
6493 */
6494 class file_input_adapter
6495 {
6496 public:
6497 using char_type = char;
6498
6499 JSON_HEDLEY_NON_NULL(2)
6500 explicit file_input_adapter(std::FILE* f) noexcept
6501 : m_file(f)
6502 {
6503 JSON_ASSERT(m_file != nullptr);
6504 }
6505
6506 // make class move-only
6507 file_input_adapter(const file_input_adapter&) = delete;
6508 file_input_adapter(file_input_adapter&&) noexcept = default;
6509 file_input_adapter& operator=(const file_input_adapter&) = delete;
6510 file_input_adapter& operator=(file_input_adapter&&) = delete;
6511 ~file_input_adapter() = default;
6512
6513 std::char_traits<char>::int_type get_character() noexcept
6514 {
6515 return std::fgetc(m_file);
6516 }
6517
6518 // returns the number of characters successfully read
6519 template<class T>
6520 std::size_t get_elements(T* dest, std::size_t count = 1)
6521 {
6522 return fread(dest, 1, sizeof(T) * count, m_file);
6523 }
6524
6525 private:
6526 /// the file pointer to read from
6527 std::FILE* m_file;
6528 };
6529
6530 /*!
6531 Input adapter for a (caching) istream. Ignores a UFT Byte Order Mark at
6532 beginning of input. Does not support changing the underlying std::streambuf
6533 in mid-input. Maintains underlying std::istream and std::streambuf to support
6534 subsequent use of standard std::istream operations to process any input
6535 characters following those used in parsing the JSON input. Clears the
6536 std::istream flags; any input errors (e.g., EOF) will be detected by the first
6537 subsequent call for input from the std::istream.
6538 */
6539 class input_stream_adapter
6540 {
6541 public:
6542 using char_type = char;
6543
6544 ~input_stream_adapter()
6545 {
6546 // clear stream flags; we use underlying streambuf I/O, do not
6547 // maintain ifstream flags, except eof
6548 if (is != nullptr)
6549 {
6550 is->clear(is->rdstate() & std::ios::eofbit);
6551 }
6552 }
6553
6554 explicit input_stream_adapter(std::istream& i)
6555 : is(&i), sb(i.rdbuf())
6556 {}
6557
6558 // delete because of pointer members
6559 input_stream_adapter(const input_stream_adapter&) = delete;
6560 input_stream_adapter& operator=(input_stream_adapter&) = delete;
6561 input_stream_adapter& operator=(input_stream_adapter&&) = delete;
6562
6563 input_stream_adapter(input_stream_adapter&& rhs) noexcept
6564 : is(rhs.is), sb(rhs.sb)
6565 {
6566 rhs.is = nullptr;
6567 rhs.sb = nullptr;
6568 }
6569
6570 // std::istream/std::streambuf use std::char_traits<char>::to_int_type, to
6571 // ensure that std::char_traits<char>::eof() and the character 0xFF do not
6572 // end up as the same value, e.g. 0xFFFFFFFF.
6573 std::char_traits<char>::int_type get_character()
6574 {
6575 auto res = sb->sbumpc();
6576 // set eof manually, as we don't use the istream interface.
6577 if (JSON_HEDLEY_UNLIKELY(res == std::char_traits<char>::eof()))
6578 {
6579 is->clear(is->rdstate() | std::ios::eofbit);
6580 }
6581 return res;
6582 }
6583
6584 template<class T>
6585 std::size_t get_elements(T* dest, std::size_t count = 1)
6586 {
6587 auto res = static_cast<std::size_t>(sb->sgetn(reinterpret_cast<char*>(dest), static_cast<std::streamsize>(count * sizeof(T))));
6588 if (JSON_HEDLEY_UNLIKELY(res < count * sizeof(T)))
6589 {
6590 is->clear(is->rdstate() | std::ios::eofbit);
6591 }
6592 return res;
6593 }
6594
6595 private:
6596 /// the associated input stream
6597 std::istream* is = nullptr;
6598 std::streambuf* sb = nullptr;
6599 };
6600 #endif // JSON_NO_IO
6601
6602 // General-purpose iterator-based adapter. It might not be as fast as
6603 // theoretically possible for some containers, but it is extremely versatile.
6604 template<typename IteratorType>
6605 class iterator_input_adapter
6606 {
6607 public:
6608 using char_type = typename std::iterator_traits<IteratorType>::value_type;
6609
6610 iterator_input_adapter(IteratorType first, IteratorType last)
6611 : current(std::move(first)), end(std::move(last))
6612 {}
6613
6614 typename char_traits<char_type>::int_type get_character()
6615 {
6616 if (JSON_HEDLEY_LIKELY(current != end))
6617 {
6618 auto result = char_traits<char_type>::to_int_type(*current);
6619 std::advance(current, 1);
6620 return result;
6621 }
6622
6623 return char_traits<char_type>::eof();
6624 }
6625
6626 // for general iterators, we cannot really do something better than falling back to processing the range one-by-one
6627 template<class T>
6628 std::size_t get_elements(T* dest, std::size_t count = 1)
6629 {
6630 auto* ptr = reinterpret_cast<char*>(dest);
6631 for (std::size_t read_index = 0; read_index < count * sizeof(T); ++read_index)
6632 {
6633 if (JSON_HEDLEY_LIKELY(current != end))
6634 {
6635 ptr[read_index] = static_cast<char>(*current);
6636 std::advance(current, 1);
6637 }
6638 else
6639 {
6640 return read_index;
6641 }
6642 }
6643 return count * sizeof(T);
6644 }
6645
6646 private:
6647 IteratorType current;
6648 IteratorType end;
6649
6650 template<typename BaseInputAdapter, size_t T>
6651 friend struct wide_string_input_helper;
6652
6653 bool empty() const
6654 {
6655 return current == end;
6656 }
6657 };
6658
6659 template<typename BaseInputAdapter, size_t T>
6660 struct wide_string_input_helper;
6661
6662 template<typename BaseInputAdapter>
6663 struct wide_string_input_helper<BaseInputAdapter, 4>
6664 {
6665 // UTF-32
6666 static void fill_buffer(BaseInputAdapter& input,
6667 std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
6668 size_t& utf8_bytes_index,
6669 size_t& utf8_bytes_filled)
6670 {
6671 utf8_bytes_index = 0;
6672
6673 if (JSON_HEDLEY_UNLIKELY(input.empty()))
6674 {
6675 utf8_bytes[0] = std::char_traits<char>::eof();
6676 utf8_bytes_filled = 1;
6677 }
6678 else
6679 {
6680 // get the current character
6681 const auto wc = input.get_character();
6682
6683 // UTF-32 to UTF-8 encoding
6684 if (wc < 0x80)
6685 {
6686 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
6687 utf8_bytes_filled = 1;
6688 }
6689 else if (wc <= 0x7FF)
6690 {
6691 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((static_cast<unsigned int>(wc) >> 6u) & 0x1Fu));
6692 utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
6693 utf8_bytes_filled = 2;
6694 }
6695 else if (wc <= 0xFFFF)
6696 {
6697 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((static_cast<unsigned int>(wc) >> 12u) & 0x0Fu));
6698 utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
6699 utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
6700 utf8_bytes_filled = 3;
6701 }
6702 else if (wc <= 0x10FFFF)
6703 {
6704 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | ((static_cast<unsigned int>(wc) >> 18u) & 0x07u));
6705 utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 12u) & 0x3Fu));
6706 utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
6707 utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
6708 utf8_bytes_filled = 4;
6709 }
6710 else
6711 {
6712 // unknown character
6713 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
6714 utf8_bytes_filled = 1;
6715 }
6716 }
6717 }
6718 };
6719
6720 template<typename BaseInputAdapter>
6721 struct wide_string_input_helper<BaseInputAdapter, 2>
6722 {
6723 // UTF-16
6724 static void fill_buffer(BaseInputAdapter& input,
6725 std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
6726 size_t& utf8_bytes_index,
6727 size_t& utf8_bytes_filled)
6728 {
6729 utf8_bytes_index = 0;
6730
6731 if (JSON_HEDLEY_UNLIKELY(input.empty()))
6732 {
6733 utf8_bytes[0] = std::char_traits<char>::eof();
6734 utf8_bytes_filled = 1;
6735 }
6736 else
6737 {
6738 // get the current character
6739 const auto wc = input.get_character();
6740
6741 // UTF-16 to UTF-8 encoding
6742 if (wc < 0x80)
6743 {
6744 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
6745 utf8_bytes_filled = 1;
6746 }
6747 else if (wc <= 0x7FF)
6748 {
6749 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((static_cast<unsigned int>(wc) >> 6u)));
6750 utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
6751 utf8_bytes_filled = 2;
6752 }
6753 else if (0xD800 > wc || wc >= 0xE000)
6754 {
6755 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((static_cast<unsigned int>(wc) >> 12u)));
6756 utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
6757 utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
6758 utf8_bytes_filled = 3;
6759 }
6760 else
6761 {
6762 if (JSON_HEDLEY_UNLIKELY(!input.empty()))
6763 {
6764 const auto wc2 = static_cast<unsigned int>(input.get_character());
6765 const auto charcode = 0x10000u + (((static_cast<unsigned int>(wc) & 0x3FFu) << 10u) | (wc2 & 0x3FFu));
6766 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | (charcode >> 18u));
6767 utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 12u) & 0x3Fu));
6768 utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 6u) & 0x3Fu));
6769 utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (charcode & 0x3Fu));
6770 utf8_bytes_filled = 4;
6771 }
6772 else
6773 {
6774 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
6775 utf8_bytes_filled = 1;
6776 }
6777 }
6778 }
6779 }
6780 };
6781
6782 // Wraps another input adapter to convert wide character types into individual bytes.
6783 template<typename BaseInputAdapter, typename WideCharType>
6784 class wide_string_input_adapter
6785 {
6786 public:
6787 using char_type = char;
6788
6789 wide_string_input_adapter(BaseInputAdapter base)
6790 : base_adapter(base) {}
6791
6792 typename std::char_traits<char>::int_type get_character() noexcept
6793 {
6794 // check if buffer needs to be filled
6795 if (utf8_bytes_index == utf8_bytes_filled)
6796 {
6797 fill_buffer<sizeof(WideCharType)>();
6798
6799 JSON_ASSERT(utf8_bytes_filled > 0);
6800 JSON_ASSERT(utf8_bytes_index == 0);
6801 }
6802
6803 // use buffer
6804 JSON_ASSERT(utf8_bytes_filled > 0);
6805 JSON_ASSERT(utf8_bytes_index < utf8_bytes_filled);
6806 return utf8_bytes[utf8_bytes_index++];
6807 }
6808
6809 // parsing binary with wchar doesn't make sense, but since the parsing mode can be runtime, we need something here
6810 template<class T>
6811 std::size_t get_elements(T* /*dest*/, std::size_t /*count*/ = 1)
6812 {
6813 JSON_THROW(parse_error::create(112, 1, "wide string type cannot be interpreted as binary data", nullptr));
6814 }
6815
6816 private:
6817 BaseInputAdapter base_adapter;
6818
6819 template<size_t T>
6820 void fill_buffer()
6821 {
6822 wide_string_input_helper<BaseInputAdapter, T>::fill_buffer(base_adapter, utf8_bytes, utf8_bytes_index, utf8_bytes_filled);
6823 }
6824
6825 /// a buffer for UTF-8 bytes
6826 std::array<std::char_traits<char>::int_type, 4> utf8_bytes = {{0, 0, 0, 0}};
6827
6828 /// index to the utf8_codes array for the next valid byte
6829 std::size_t utf8_bytes_index = 0;
6830 /// number of valid bytes in the utf8_codes array
6831 std::size_t utf8_bytes_filled = 0;
6832 };
6833
6834 template<typename IteratorType, typename Enable = void>
6835 struct iterator_input_adapter_factory
6836 {
6837 using iterator_type = IteratorType;
6838 using char_type = typename std::iterator_traits<iterator_type>::value_type;
6839 using adapter_type = iterator_input_adapter<iterator_type>;
6840
6841 static adapter_type create(IteratorType first, IteratorType last)
6842 {
6843 return adapter_type(std::move(first), std::move(last));
6844 }
6845 };
6846
6847 template<typename T>
6848 struct is_iterator_of_multibyte
6849 {
6850 using value_type = typename std::iterator_traits<T>::value_type;
6851 enum
6852 {
6853 value = sizeof(value_type) > 1
6854 };
6855 };
6856
6857 template<typename IteratorType>
6858 struct iterator_input_adapter_factory<IteratorType, enable_if_t<is_iterator_of_multibyte<IteratorType>::value>>
6859 {
6860 using iterator_type = IteratorType;
6861 using char_type = typename std::iterator_traits<iterator_type>::value_type;
6862 using base_adapter_type = iterator_input_adapter<iterator_type>;
6863 using adapter_type = wide_string_input_adapter<base_adapter_type, char_type>;
6864
6865 static adapter_type create(IteratorType first, IteratorType last)
6866 {
6867 return adapter_type(base_adapter_type(std::move(first), std::move(last)));
6868 }
6869 };
6870
6871 // General purpose iterator-based input
6872 template<typename IteratorType>
6873 typename iterator_input_adapter_factory<IteratorType>::adapter_type input_adapter(IteratorType first, IteratorType last)
6874 {
6875 using factory_type = iterator_input_adapter_factory<IteratorType>;
6876 return factory_type::create(first, last);
6877 }
6878
6879 // Convenience shorthand from container to iterator
6880 // Enables ADL on begin(container) and end(container)
6881 // Encloses the using declarations in namespace for not to leak them to outside scope
6882
6883 namespace container_input_adapter_factory_impl
6884 {
6885
6886 using std::begin;
6887 using std::end;
6888
6889 template<typename ContainerType, typename Enable = void>
6890 struct container_input_adapter_factory {};
6891
6892 template<typename ContainerType>
6893 struct container_input_adapter_factory< ContainerType,
6894 void_t<decltype(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>()))>>
6895 {
6896 using adapter_type = decltype(input_adapter(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>())));
6897
6898 static adapter_type create(const ContainerType& container)
6899 {
6900 return input_adapter(begin(container), end(container));
6901 }
6902 };
6903
6904 } // namespace container_input_adapter_factory_impl
6905
6906 template<typename ContainerType>
6907 typename container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::adapter_type input_adapter(const ContainerType& container)
6908 {
6909 return container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::create(container);
6910 }
6911
6912 // specialization for std::string
6913 using string_input_adapter_type = decltype(input_adapter(std::declval<std::string>()));
6914
6915 #ifndef JSON_NO_IO
6916 // Special cases with fast paths
6917 inline file_input_adapter input_adapter(std::FILE* file)
6918 {
6919 if (file == nullptr)
6920 {
6921 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));
6922 }
6923 return file_input_adapter(file);
6924 }
6925
6926 inline input_stream_adapter input_adapter(std::istream& stream)
6927 {
6928 return input_stream_adapter(stream);
6929 }
6930
6931 inline input_stream_adapter input_adapter(std::istream&& stream)
6932 {
6933 return input_stream_adapter(stream);
6934 }
6935 #endif // JSON_NO_IO
6936
6937 using contiguous_bytes_input_adapter = decltype(input_adapter(std::declval<const char*>(), std::declval<const char*>()));
6938
6939 // Null-delimited strings, and the like.
6940 template < typename CharT,
6941 typename std::enable_if <
6942 std::is_pointer<CharT>::value&&
6943 !std::is_array<CharT>::value&&
6944 std::is_integral<typename std::remove_pointer<CharT>::type>::value&&
6945 sizeof(typename std::remove_pointer<CharT>::type) == 1,
6946 int >::type = 0 >
6947 contiguous_bytes_input_adapter input_adapter(CharT b)
6948 {
6949 if (b == nullptr)
6950 {
6951 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));
6952 }
6953 auto length = std::strlen(reinterpret_cast<const char*>(b));
6954 const auto* ptr = reinterpret_cast<const char*>(b);
6955 return input_adapter(ptr, ptr + length); // cppcheck-suppress[nullPointerArithmeticRedundantCheck]
6956 }
6957
6958 template<typename T, std::size_t N>
6959 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)
6960 {
6961 return input_adapter(array, array + N);
6962 }
6963
6964 // This class only handles inputs of input_buffer_adapter type.
6965 // It's required so that expressions like {ptr, len} can be implicitly cast
6966 // to the correct adapter.
6967 class span_input_adapter
6968 {
6969 public:
6970 template < typename CharT,
6971 typename std::enable_if <
6972 std::is_pointer<CharT>::value&&
6973 std::is_integral<typename std::remove_pointer<CharT>::type>::value&&
6974 sizeof(typename std::remove_pointer<CharT>::type) == 1,
6975 int >::type = 0 >
6976 span_input_adapter(CharT b, std::size_t l)
6977 : ia(reinterpret_cast<const char*>(b), reinterpret_cast<const char*>(b) + l) {}
6978
6979 template<class IteratorType,
6980 typename std::enable_if<
6981 std::is_same<typename iterator_traits<IteratorType>::iterator_category, std::random_access_iterator_tag>::value,
6982 int>::type = 0>
6983 span_input_adapter(IteratorType first, IteratorType last)
6984 : ia(input_adapter(first, last)) {}
6985
6986 contiguous_bytes_input_adapter&& get()
6987 {
6988 return std::move(ia); // NOLINT(hicpp-move-const-arg,performance-move-const-arg)
6989 }
6990
6991 private:
6992 contiguous_bytes_input_adapter ia;
6993 };
6994
6995 } // namespace detail
6996 NLOHMANN_JSON_NAMESPACE_END
6997
6998 // #include <nlohmann/detail/input/json_sax.hpp>
6999 // __ _____ _____ _____
7000 // __| | __| | | | JSON for Modern C++
7001 // | | |__ | | | | | | version 3.12.0
7002 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
7003 //
7004 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
7005 // SPDX-License-Identifier: MIT
7006
7007
7008
7009 #include <cstddef>
7010 #include <string> // string
7011 #include <type_traits> // enable_if_t
7012 #include <utility> // move
7013 #include <vector> // vector
7014
7015 // #include <nlohmann/detail/exceptions.hpp>
7016
7017 // #include <nlohmann/detail/input/lexer.hpp>
7018 // __ _____ _____ _____
7019 // __| | __| | | | JSON for Modern C++
7020 // | | |__ | | | | | | version 3.12.0
7021 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
7022 //
7023 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
7024 // SPDX-License-Identifier: MIT
7025
7026
7027
7028 #include <array> // array
7029 #include <clocale> // localeconv
7030 #include <cstddef> // size_t
7031 #include <cstdio> // snprintf
7032 #include <cstdlib> // strtof, strtod, strtold, strtoll, strtoull
7033 #include <initializer_list> // initializer_list
7034 #include <string> // char_traits, string
7035 #include <utility> // move
7036 #include <vector> // vector
7037
7038 // #include <nlohmann/detail/input/input_adapters.hpp>
7039
7040 // #include <nlohmann/detail/input/position_t.hpp>
7041
7042 // #include <nlohmann/detail/macro_scope.hpp>
7043
7044 // #include <nlohmann/detail/meta/type_traits.hpp>
7045
7046
7047 NLOHMANN_JSON_NAMESPACE_BEGIN
7048 namespace detail
7049 {
7050
7051 ///////////
7052 // lexer //
7053 ///////////
7054
7055 template<typename BasicJsonType>
7056 class lexer_base
7057 {
7058 public:
7059 /// token types for the parser
7060 enum class token_type
7061 {
7062 uninitialized, ///< indicating the scanner is uninitialized
7063 literal_true, ///< the `true` literal
7064 literal_false, ///< the `false` literal
7065 literal_null, ///< the `null` literal
7066 value_string, ///< a string -- use get_string() for actual value
7067 value_unsigned, ///< an unsigned integer -- use get_number_unsigned() for actual value
7068 value_integer, ///< a signed integer -- use get_number_integer() for actual value
7069 value_float, ///< an floating point number -- use get_number_float() for actual value
7070 begin_array, ///< the character for array begin `[`
7071 begin_object, ///< the character for object begin `{`
7072 end_array, ///< the character for array end `]`
7073 end_object, ///< the character for object end `}`
7074 name_separator, ///< the name separator `:`
7075 value_separator, ///< the value separator `,`
7076 parse_error, ///< indicating a parse error
7077 end_of_input, ///< indicating the end of the input buffer
7078 literal_or_value ///< a literal or the begin of a value (only for diagnostics)
7079 };
7080
7081 /// return name of values of type token_type (only used for errors)
7082 JSON_HEDLEY_RETURNS_NON_NULL
7083 JSON_HEDLEY_CONST
7084 static const char* token_type_name(const token_type t) noexcept
7085 {
7086 switch (t)
7087 {
7088 case token_type::uninitialized:
7089 return "<uninitialized>";
7090 case token_type::literal_true:
7091 return "true literal";
7092 case token_type::literal_false:
7093 return "false literal";
7094 case token_type::literal_null:
7095 return "null literal";
7096 case token_type::value_string:
7097 return "string literal";
7098 case token_type::value_unsigned:
7099 case token_type::value_integer:
7100 case token_type::value_float:
7101 return "number literal";
7102 case token_type::begin_array:
7103 return "'['";
7104 case token_type::begin_object:
7105 return "'{'";
7106 case token_type::end_array:
7107 return "']'";
7108 case token_type::end_object:
7109 return "'}'";
7110 case token_type::name_separator:
7111 return "':'";
7112 case token_type::value_separator:
7113 return "','";
7114 case token_type::parse_error:
7115 return "<parse error>";
7116 case token_type::end_of_input:
7117 return "end of input";
7118 case token_type::literal_or_value:
7119 return "'[', '{', or a literal";
7120 // LCOV_EXCL_START
7121 default: // catch non-enum values
7122 return "unknown token";
7123 // LCOV_EXCL_STOP
7124 }
7125 }
7126 };
7127 /*!
7128 @brief lexical analysis
7129
7130 This class organizes the lexical analysis during JSON deserialization.
7131 */
7132 template<typename BasicJsonType, typename InputAdapterType>
7133 class lexer : public lexer_base<BasicJsonType>
7134 {
7135 using number_integer_t = typename BasicJsonType::number_integer_t;
7136 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
7137 using number_float_t = typename BasicJsonType::number_float_t;
7138 using string_t = typename BasicJsonType::string_t;
7139 using char_type = typename InputAdapterType::char_type;
7140 using char_int_type = typename char_traits<char_type>::int_type;
7141
7142 public:
7143 using token_type = typename lexer_base<BasicJsonType>::token_type;
7144
7145 explicit lexer(InputAdapterType&& adapter, bool ignore_comments_ = false) noexcept
7146 : ia(std::move(adapter))
7147 , ignore_comments(ignore_comments_)
7148 , decimal_point_char(static_cast<char_int_type>(get_decimal_point()))
7149 {}
7150
7151 // delete because of pointer members
7152 lexer(const lexer&) = delete;
7153 lexer(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
7154 lexer& operator=(lexer&) = delete;
7155 lexer& operator=(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
7156 ~lexer() = default;
7157
7158 private:
7159 /////////////////////
7160 // locales
7161 /////////////////////
7162
7163 /// return the locale-dependent decimal point
7164 JSON_HEDLEY_PURE
7165 static char get_decimal_point() noexcept
7166 {
7167 const auto* loc = localeconv();
7168 JSON_ASSERT(loc != nullptr);
7169 return (loc->decimal_point == nullptr) ? '.' : *(loc->decimal_point);
7170 }
7171
7172 /////////////////////
7173 // scan functions
7174 /////////////////////
7175
7176 /*!
7177 @brief get codepoint from 4 hex characters following `\u`
7178
7179 For input "\u c1 c2 c3 c4" the codepoint is:
7180 (c1 * 0x1000) + (c2 * 0x0100) + (c3 * 0x0010) + c4
7181 = (c1 << 12) + (c2 << 8) + (c3 << 4) + (c4 << 0)
7182
7183 Furthermore, the possible characters '0'..'9', 'A'..'F', and 'a'..'f'
7184 must be converted to the integers 0x0..0x9, 0xA..0xF, 0xA..0xF, resp. The
7185 conversion is done by subtracting the offset (0x30, 0x37, and 0x57)
7186 between the ASCII value of the character and the desired integer value.
7187
7188 @return codepoint (0x0000..0xFFFF) or -1 in case of an error (e.g. EOF or
7189 non-hex character)
7190 */
7191 int get_codepoint()
7192 {
7193 // this function only makes sense after reading `\u`
7194 JSON_ASSERT(current == 'u');
7195 int codepoint = 0;
7196
7197 const auto factors = { 12u, 8u, 4u, 0u };
7198 for (const auto factor : factors)
7199 {
7200 get();
7201
7202 if (current >= '0' && current <= '9')
7203 {
7204 codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x30u) << factor);
7205 }
7206 else if (current >= 'A' && current <= 'F')
7207 {
7208 codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x37u) << factor);
7209 }
7210 else if (current >= 'a' && current <= 'f')
7211 {
7212 codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x57u) << factor);
7213 }
7214 else
7215 {
7216 return -1;
7217 }
7218 }
7219
7220 JSON_ASSERT(0x0000 <= codepoint && codepoint <= 0xFFFF);
7221 return codepoint;
7222 }
7223
7224 /*!
7225 @brief check if the next byte(s) are inside a given range
7226
7227 Adds the current byte and, for each passed range, reads a new byte and
7228 checks if it is inside the range. If a violation was detected, set up an
7229 error message and return false. Otherwise, return true.
7230
7231 @param[in] ranges list of integers; interpreted as list of pairs of
7232 inclusive lower and upper bound, respectively
7233
7234 @pre The passed list @a ranges must have 2, 4, or 6 elements; that is,
7235 1, 2, or 3 pairs. This precondition is enforced by an assertion.
7236
7237 @return true if and only if no range violation was detected
7238 */
7239 bool next_byte_in_range(std::initializer_list<char_int_type> ranges)
7240 {
7241 JSON_ASSERT(ranges.size() == 2 || ranges.size() == 4 || ranges.size() == 6);
7242 add(current);
7243
7244 for (auto range = ranges.begin(); range != ranges.end(); ++range)
7245 {
7246 get();
7247 if (JSON_HEDLEY_LIKELY(*range <= current && current <= *(++range))) // NOLINT(bugprone-inc-dec-in-conditions)
7248 {
7249 add(current);
7250 }
7251 else
7252 {
7253 error_message = "invalid string: ill-formed UTF-8 byte";
7254 return false;
7255 }
7256 }
7257
7258 return true;
7259 }
7260
7261 /*!
7262 @brief scan a string literal
7263
7264 This function scans a string according to Sect. 7 of RFC 8259. While
7265 scanning, bytes are escaped and copied into buffer token_buffer. Then the
7266 function returns successfully, token_buffer is *not* null-terminated (as it
7267 may contain \0 bytes), and token_buffer.size() is the number of bytes in the
7268 string.
7269
7270 @return token_type::value_string if string could be successfully scanned,
7271 token_type::parse_error otherwise
7272
7273 @note In case of errors, variable error_message contains a textual
7274 description.
7275 */
7276 token_type scan_string()
7277 {
7278 // reset token_buffer (ignore opening quote)
7279 reset();
7280
7281 // we entered the function by reading an open quote
7282 JSON_ASSERT(current == '\"');
7283
7284 while (true)
7285 {
7286 // get next character
7287 switch (get())
7288 {
7289 // end of file while parsing string
7290 case char_traits<char_type>::eof():
7291 {
7292 error_message = "invalid string: missing closing quote";
7293 return token_type::parse_error;
7294 }
7295
7296 // closing quote
7297 case '\"':
7298 {
7299 return token_type::value_string;
7300 }
7301
7302 // escapes
7303 case '\\':
7304 {
7305 switch (get())
7306 {
7307 // quotation mark
7308 case '\"':
7309 add('\"');
7310 break;
7311 // reverse solidus
7312 case '\\':
7313 add('\\');
7314 break;
7315 // solidus
7316 case '/':
7317 add('/');
7318 break;
7319 // backspace
7320 case 'b':
7321 add('\b');
7322 break;
7323 // form feed
7324 case 'f':
7325 add('\f');
7326 break;
7327 // line feed
7328 case 'n':
7329 add('\n');
7330 break;
7331 // carriage return
7332 case 'r':
7333 add('\r');
7334 break;
7335 // tab
7336 case 't':
7337 add('\t');
7338 break;
7339
7340 // unicode escapes
7341 case 'u':
7342 {
7343 const int codepoint1 = get_codepoint();
7344 int codepoint = codepoint1; // start with codepoint1
7345
7346 if (JSON_HEDLEY_UNLIKELY(codepoint1 == -1))
7347 {
7348 error_message = "invalid string: '\\u' must be followed by 4 hex digits";
7349 return token_type::parse_error;
7350 }
7351
7352 // check if code point is a high surrogate
7353 if (0xD800 <= codepoint1 && codepoint1 <= 0xDBFF)
7354 {
7355 // expect next \uxxxx entry
7356 if (JSON_HEDLEY_LIKELY(get() == '\\' && get() == 'u'))
7357 {
7358 const int codepoint2 = get_codepoint();
7359
7360 if (JSON_HEDLEY_UNLIKELY(codepoint2 == -1))
7361 {
7362 error_message = "invalid string: '\\u' must be followed by 4 hex digits";
7363 return token_type::parse_error;
7364 }
7365
7366 // check if codepoint2 is a low surrogate
7367 if (JSON_HEDLEY_LIKELY(0xDC00 <= codepoint2 && codepoint2 <= 0xDFFF))
7368 {
7369 // overwrite codepoint
7370 codepoint = static_cast<int>(
7371 // high surrogate occupies the most significant 22 bits
7372 (static_cast<unsigned int>(codepoint1) << 10u)
7373 // low surrogate occupies the least significant 15 bits
7374 + static_cast<unsigned int>(codepoint2)
7375 // there is still the 0xD800, 0xDC00 and 0x10000 noise
7376 // in the result, so we have to subtract with:
7377 // (0xD800 << 10) + DC00 - 0x10000 = 0x35FDC00
7378 - 0x35FDC00u);
7379 }
7380 else
7381 {
7382 error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF";
7383 return token_type::parse_error;
7384 }
7385 }
7386 else
7387 {
7388 error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF";
7389 return token_type::parse_error;
7390 }
7391 }
7392 else
7393 {
7394 if (JSON_HEDLEY_UNLIKELY(0xDC00 <= codepoint1 && codepoint1 <= 0xDFFF))
7395 {
7396 error_message = "invalid string: surrogate U+DC00..U+DFFF must follow U+D800..U+DBFF";
7397 return token_type::parse_error;
7398 }
7399 }
7400
7401 // result of the above calculation yields a proper codepoint
7402 JSON_ASSERT(0x00 <= codepoint && codepoint <= 0x10FFFF);
7403
7404 // translate codepoint into bytes
7405 if (codepoint < 0x80)
7406 {
7407 // 1-byte characters: 0xxxxxxx (ASCII)
7408 add(static_cast<char_int_type>(codepoint));
7409 }
7410 else if (codepoint <= 0x7FF)
7411 {
7412 // 2-byte characters: 110xxxxx 10xxxxxx
7413 add(static_cast<char_int_type>(0xC0u | (static_cast<unsigned int>(codepoint) >> 6u)));
7414 add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
7415 }
7416 else if (codepoint <= 0xFFFF)
7417 {
7418 // 3-byte characters: 1110xxxx 10xxxxxx 10xxxxxx
7419 add(static_cast<char_int_type>(0xE0u | (static_cast<unsigned int>(codepoint) >> 12u)));
7420 add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
7421 add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
7422 }
7423 else
7424 {
7425 // 4-byte characters: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
7426 add(static_cast<char_int_type>(0xF0u | (static_cast<unsigned int>(codepoint) >> 18u)));
7427 add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 12u) & 0x3Fu)));
7428 add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
7429 add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
7430 }
7431
7432 break;
7433 }
7434
7435 // other characters after escape
7436 default:
7437 error_message = "invalid string: forbidden character after backslash";
7438 return token_type::parse_error;
7439 }
7440
7441 break;
7442 }
7443
7444 // invalid control characters
7445 case 0x00:
7446 {
7447 error_message = "invalid string: control character U+0000 (NUL) must be escaped to \\u0000";
7448 return token_type::parse_error;
7449 }
7450
7451 case 0x01:
7452 {
7453 error_message = "invalid string: control character U+0001 (SOH) must be escaped to \\u0001";
7454 return token_type::parse_error;
7455 }
7456
7457 case 0x02:
7458 {
7459 error_message = "invalid string: control character U+0002 (STX) must be escaped to \\u0002";
7460 return token_type::parse_error;
7461 }
7462
7463 case 0x03:
7464 {
7465 error_message = "invalid string: control character U+0003 (ETX) must be escaped to \\u0003";
7466 return token_type::parse_error;
7467 }
7468
7469 case 0x04:
7470 {
7471 error_message = "invalid string: control character U+0004 (EOT) must be escaped to \\u0004";
7472 return token_type::parse_error;
7473 }
7474
7475 case 0x05:
7476 {
7477 error_message = "invalid string: control character U+0005 (ENQ) must be escaped to \\u0005";
7478 return token_type::parse_error;
7479 }
7480
7481 case 0x06:
7482 {
7483 error_message = "invalid string: control character U+0006 (ACK) must be escaped to \\u0006";
7484 return token_type::parse_error;
7485 }
7486
7487 case 0x07:
7488 {
7489 error_message = "invalid string: control character U+0007 (BEL) must be escaped to \\u0007";
7490 return token_type::parse_error;
7491 }
7492
7493 case 0x08:
7494 {
7495 error_message = "invalid string: control character U+0008 (BS) must be escaped to \\u0008 or \\b";
7496 return token_type::parse_error;
7497 }
7498
7499 case 0x09:
7500 {
7501 error_message = "invalid string: control character U+0009 (HT) must be escaped to \\u0009 or \\t";
7502 return token_type::parse_error;
7503 }
7504
7505 case 0x0A:
7506 {
7507 error_message = "invalid string: control character U+000A (LF) must be escaped to \\u000A or \\n";
7508 return token_type::parse_error;
7509 }
7510
7511 case 0x0B:
7512 {
7513 error_message = "invalid string: control character U+000B (VT) must be escaped to \\u000B";
7514 return token_type::parse_error;
7515 }
7516
7517 case 0x0C:
7518 {
7519 error_message = "invalid string: control character U+000C (FF) must be escaped to \\u000C or \\f";
7520 return token_type::parse_error;
7521 }
7522
7523 case 0x0D:
7524 {
7525 error_message = "invalid string: control character U+000D (CR) must be escaped to \\u000D or \\r";
7526 return token_type::parse_error;
7527 }
7528
7529 case 0x0E:
7530 {
7531 error_message = "invalid string: control character U+000E (SO) must be escaped to \\u000E";
7532 return token_type::parse_error;
7533 }
7534
7535 case 0x0F:
7536 {
7537 error_message = "invalid string: control character U+000F (SI) must be escaped to \\u000F";
7538 return token_type::parse_error;
7539 }
7540
7541 case 0x10:
7542 {
7543 error_message = "invalid string: control character U+0010 (DLE) must be escaped to \\u0010";
7544 return token_type::parse_error;
7545 }
7546
7547 case 0x11:
7548 {
7549 error_message = "invalid string: control character U+0011 (DC1) must be escaped to \\u0011";
7550 return token_type::parse_error;
7551 }
7552
7553 case 0x12:
7554 {
7555 error_message = "invalid string: control character U+0012 (DC2) must be escaped to \\u0012";
7556 return token_type::parse_error;
7557 }
7558
7559 case 0x13:
7560 {
7561 error_message = "invalid string: control character U+0013 (DC3) must be escaped to \\u0013";
7562 return token_type::parse_error;
7563 }
7564
7565 case 0x14:
7566 {
7567 error_message = "invalid string: control character U+0014 (DC4) must be escaped to \\u0014";
7568 return token_type::parse_error;
7569 }
7570
7571 case 0x15:
7572 {
7573 error_message = "invalid string: control character U+0015 (NAK) must be escaped to \\u0015";
7574 return token_type::parse_error;
7575 }
7576
7577 case 0x16:
7578 {
7579 error_message = "invalid string: control character U+0016 (SYN) must be escaped to \\u0016";
7580 return token_type::parse_error;
7581 }
7582
7583 case 0x17:
7584 {
7585 error_message = "invalid string: control character U+0017 (ETB) must be escaped to \\u0017";
7586 return token_type::parse_error;
7587 }
7588
7589 case 0x18:
7590 {
7591 error_message = "invalid string: control character U+0018 (CAN) must be escaped to \\u0018";
7592 return token_type::parse_error;
7593 }
7594
7595 case 0x19:
7596 {
7597 error_message = "invalid string: control character U+0019 (EM) must be escaped to \\u0019";
7598 return token_type::parse_error;
7599 }
7600
7601 case 0x1A:
7602 {
7603 error_message = "invalid string: control character U+001A (SUB) must be escaped to \\u001A";
7604 return token_type::parse_error;
7605 }
7606
7607 case 0x1B:
7608 {
7609 error_message = "invalid string: control character U+001B (ESC) must be escaped to \\u001B";
7610 return token_type::parse_error;
7611 }
7612
7613 case 0x1C:
7614 {
7615 error_message = "invalid string: control character U+001C (FS) must be escaped to \\u001C";
7616 return token_type::parse_error;
7617 }
7618
7619 case 0x1D:
7620 {
7621 error_message = "invalid string: control character U+001D (GS) must be escaped to \\u001D";
7622 return token_type::parse_error;
7623 }
7624
7625 case 0x1E:
7626 {
7627 error_message = "invalid string: control character U+001E (RS) must be escaped to \\u001E";
7628 return token_type::parse_error;
7629 }
7630
7631 case 0x1F:
7632 {
7633 error_message = "invalid string: control character U+001F (US) must be escaped to \\u001F";
7634 return token_type::parse_error;
7635 }
7636
7637 // U+0020..U+007F (except U+0022 (quote) and U+005C (backspace))
7638 case 0x20:
7639 case 0x21:
7640 case 0x23:
7641 case 0x24:
7642 case 0x25:
7643 case 0x26:
7644 case 0x27:
7645 case 0x28:
7646 case 0x29:
7647 case 0x2A:
7648 case 0x2B:
7649 case 0x2C:
7650 case 0x2D:
7651 case 0x2E:
7652 case 0x2F:
7653 case 0x30:
7654 case 0x31:
7655 case 0x32:
7656 case 0x33:
7657 case 0x34:
7658 case 0x35:
7659 case 0x36:
7660 case 0x37:
7661 case 0x38:
7662 case 0x39:
7663 case 0x3A:
7664 case 0x3B:
7665 case 0x3C:
7666 case 0x3D:
7667 case 0x3E:
7668 case 0x3F:
7669 case 0x40:
7670 case 0x41:
7671 case 0x42:
7672 case 0x43:
7673 case 0x44:
7674 case 0x45:
7675 case 0x46:
7676 case 0x47:
7677 case 0x48:
7678 case 0x49:
7679 case 0x4A:
7680 case 0x4B:
7681 case 0x4C:
7682 case 0x4D:
7683 case 0x4E:
7684 case 0x4F:
7685 case 0x50:
7686 case 0x51:
7687 case 0x52:
7688 case 0x53:
7689 case 0x54:
7690 case 0x55:
7691 case 0x56:
7692 case 0x57:
7693 case 0x58:
7694 case 0x59:
7695 case 0x5A:
7696 case 0x5B:
7697 case 0x5D:
7698 case 0x5E:
7699 case 0x5F:
7700 case 0x60:
7701 case 0x61:
7702 case 0x62:
7703 case 0x63:
7704 case 0x64:
7705 case 0x65:
7706 case 0x66:
7707 case 0x67:
7708 case 0x68:
7709 case 0x69:
7710 case 0x6A:
7711 case 0x6B:
7712 case 0x6C:
7713 case 0x6D:
7714 case 0x6E:
7715 case 0x6F:
7716 case 0x70:
7717 case 0x71:
7718 case 0x72:
7719 case 0x73:
7720 case 0x74:
7721 case 0x75:
7722 case 0x76:
7723 case 0x77:
7724 case 0x78:
7725 case 0x79:
7726 case 0x7A:
7727 case 0x7B:
7728 case 0x7C:
7729 case 0x7D:
7730 case 0x7E:
7731 case 0x7F:
7732 {
7733 add(current);
7734 break;
7735 }
7736
7737 // U+0080..U+07FF: bytes C2..DF 80..BF
7738 case 0xC2:
7739 case 0xC3:
7740 case 0xC4:
7741 case 0xC5:
7742 case 0xC6:
7743 case 0xC7:
7744 case 0xC8:
7745 case 0xC9:
7746 case 0xCA:
7747 case 0xCB:
7748 case 0xCC:
7749 case 0xCD:
7750 case 0xCE:
7751 case 0xCF:
7752 case 0xD0:
7753 case 0xD1:
7754 case 0xD2:
7755 case 0xD3:
7756 case 0xD4:
7757 case 0xD5:
7758 case 0xD6:
7759 case 0xD7:
7760 case 0xD8:
7761 case 0xD9:
7762 case 0xDA:
7763 case 0xDB:
7764 case 0xDC:
7765 case 0xDD:
7766 case 0xDE:
7767 case 0xDF:
7768 {
7769 if (JSON_HEDLEY_UNLIKELY(!next_byte_in_range({0x80, 0xBF})))
7770 {
7771 return token_type::parse_error;
7772 }
7773 break;
7774 }
7775
7776 // U+0800..U+0FFF: bytes E0 A0..BF 80..BF
7777 case 0xE0:
7778 {
7779 if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0xA0, 0xBF, 0x80, 0xBF}))))
7780 {
7781 return token_type::parse_error;
7782 }
7783 break;
7784 }
7785
7786 // U+1000..U+CFFF: bytes E1..EC 80..BF 80..BF
7787 // U+E000..U+FFFF: bytes EE..EF 80..BF 80..BF
7788 case 0xE1:
7789 case 0xE2:
7790 case 0xE3:
7791 case 0xE4:
7792 case 0xE5:
7793 case 0xE6:
7794 case 0xE7:
7795 case 0xE8:
7796 case 0xE9:
7797 case 0xEA:
7798 case 0xEB:
7799 case 0xEC:
7800 case 0xEE:
7801 case 0xEF:
7802 {
7803 if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF}))))
7804 {
7805 return token_type::parse_error;
7806 }
7807 break;
7808 }
7809
7810 // U+D000..U+D7FF: bytes ED 80..9F 80..BF
7811 case 0xED:
7812 {
7813 if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x9F, 0x80, 0xBF}))))
7814 {
7815 return token_type::parse_error;
7816 }
7817 break;
7818 }
7819
7820 // U+10000..U+3FFFF F0 90..BF 80..BF 80..BF
7821 case 0xF0:
7822 {
7823 if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x90, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
7824 {
7825 return token_type::parse_error;
7826 }
7827 break;
7828 }
7829
7830 // U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF
7831 case 0xF1:
7832 case 0xF2:
7833 case 0xF3:
7834 {
7835 if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
7836 {
7837 return token_type::parse_error;
7838 }
7839 break;
7840 }
7841
7842 // U+100000..U+10FFFF F4 80..8F 80..BF 80..BF
7843 case 0xF4:
7844 {
7845 if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x8F, 0x80, 0xBF, 0x80, 0xBF}))))
7846 {
7847 return token_type::parse_error;
7848 }
7849 break;
7850 }
7851
7852 // remaining bytes (80..C1 and F5..FF) are ill-formed
7853 default:
7854 {
7855 error_message = "invalid string: ill-formed UTF-8 byte";
7856 return token_type::parse_error;
7857 }
7858 }
7859 }
7860 }
7861
7862 /*!
7863 * @brief scan a comment
7864 * @return whether comment could be scanned successfully
7865 */
7866 bool scan_comment()
7867 {
7868 switch (get())
7869 {
7870 // single-line comments skip input until a newline or EOF is read
7871 case '/':
7872 {
7873 while (true)
7874 {
7875 switch (get())
7876 {
7877 case '\n':
7878 case '\r':
7879 case char_traits<char_type>::eof():
7880 case '\0':
7881 return true;
7882
7883 default:
7884 break;
7885 }
7886 }
7887 }
7888
7889 // multi-line comments skip input until */ is read
7890 case '*':
7891 {
7892 while (true)
7893 {
7894 switch (get())
7895 {
7896 case char_traits<char_type>::eof():
7897 case '\0':
7898 {
7899 error_message = "invalid comment; missing closing '*/'";
7900 return false;
7901 }
7902
7903 case '*':
7904 {
7905 switch (get())
7906 {
7907 case '/':
7908 return true;
7909
7910 default:
7911 {
7912 unget();
7913 continue;
7914 }
7915 }
7916 }
7917
7918 default:
7919 continue;
7920 }
7921 }
7922 }
7923
7924 // unexpected character after reading '/'
7925 default:
7926 {
7927 error_message = "invalid comment; expecting '/' or '*' after '/'";
7928 return false;
7929 }
7930 }
7931 }
7932
7933 JSON_HEDLEY_NON_NULL(2)
7934 static void strtof(float& f, const char* str, char** endptr) noexcept
7935 {
7936 f = std::strtof(str, endptr);
7937 }
7938
7939 JSON_HEDLEY_NON_NULL(2)
7940 static void strtof(double& f, const char* str, char** endptr) noexcept
7941 {
7942 f = std::strtod(str, endptr);
7943 }
7944
7945 JSON_HEDLEY_NON_NULL(2)
7946 static void strtof(long double& f, const char* str, char** endptr) noexcept
7947 {
7948 f = std::strtold(str, endptr);
7949 }
7950
7951 /*!
7952 @brief scan a number literal
7953
7954 This function scans a string according to Sect. 6 of RFC 8259.
7955
7956 The function is realized with a deterministic finite state machine derived
7957 from the grammar described in RFC 8259. Starting in state "init", the
7958 input is read and used to determined the next state. Only state "done"
7959 accepts the number. State "error" is a trap state to model errors. In the
7960 table below, "anything" means any character but the ones listed before.
7961
7962 state | 0 | 1-9 | e E | + | - | . | anything
7963 ---------|----------|----------|----------|---------|---------|----------|-----------
7964 init | zero | any1 | [error] | [error] | minus | [error] | [error]
7965 minus | zero | any1 | [error] | [error] | [error] | [error] | [error]
7966 zero | done | done | exponent | done | done | decimal1 | done
7967 any1 | any1 | any1 | exponent | done | done | decimal1 | done
7968 decimal1 | decimal2 | decimal2 | [error] | [error] | [error] | [error] | [error]
7969 decimal2 | decimal2 | decimal2 | exponent | done | done | done | done
7970 exponent | any2 | any2 | [error] | sign | sign | [error] | [error]
7971 sign | any2 | any2 | [error] | [error] | [error] | [error] | [error]
7972 any2 | any2 | any2 | done | done | done | done | done
7973
7974 The state machine is realized with one label per state (prefixed with
7975 "scan_number_") and `goto` statements between them. The state machine
7976 contains cycles, but any cycle can be left when EOF is read. Therefore,
7977 the function is guaranteed to terminate.
7978
7979 During scanning, the read bytes are stored in token_buffer. This string is
7980 then converted to a signed integer, an unsigned integer, or a
7981 floating-point number.
7982
7983 @return token_type::value_unsigned, token_type::value_integer, or
7984 token_type::value_float if number could be successfully scanned,
7985 token_type::parse_error otherwise
7986
7987 @note The scanner is independent of the current locale. Internally, the
7988 locale's decimal point is used instead of `.` to work with the
7989 locale-dependent converters.
7990 */
7991 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.
7992 {
7993 // reset token_buffer to store the number's bytes
7994 reset();
7995
7996 // the type of the parsed number; initially set to unsigned; will be
7997 // changed if minus sign, decimal point or exponent is read
7998 token_type number_type = token_type::value_unsigned;
7999
8000 // state (init): we just found out we need to scan a number
8001 switch (current)
8002 {
8003 case '-':
8004 {
8005 add(current);
8006 goto scan_number_minus;
8007 }
8008
8009 case '0':
8010 {
8011 add(current);
8012 goto scan_number_zero;
8013 }
8014
8015 case '1':
8016 case '2':
8017 case '3':
8018 case '4':
8019 case '5':
8020 case '6':
8021 case '7':
8022 case '8':
8023 case '9':
8024 {
8025 add(current);
8026 goto scan_number_any1;
8027 }
8028
8029 // all other characters are rejected outside scan_number()
8030 default: // LCOV_EXCL_LINE
8031 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
8032 }
8033
8034 scan_number_minus:
8035 // state: we just parsed a leading minus sign
8036 number_type = token_type::value_integer;
8037 switch (get())
8038 {
8039 case '0':
8040 {
8041 add(current);
8042 goto scan_number_zero;
8043 }
8044
8045 case '1':
8046 case '2':
8047 case '3':
8048 case '4':
8049 case '5':
8050 case '6':
8051 case '7':
8052 case '8':
8053 case '9':
8054 {
8055 add(current);
8056 goto scan_number_any1;
8057 }
8058
8059 default:
8060 {
8061 error_message = "invalid number; expected digit after '-'";
8062 return token_type::parse_error;
8063 }
8064 }
8065
8066 scan_number_zero:
8067 // state: we just parse a zero (maybe with a leading minus sign)
8068 switch (get())
8069 {
8070 case '.':
8071 {
8072 add(decimal_point_char);
8073 decimal_point_position = token_buffer.size() - 1;
8074 goto scan_number_decimal1;
8075 }
8076
8077 case 'e':
8078 case 'E':
8079 {
8080 add(current);
8081 goto scan_number_exponent;
8082 }
8083
8084 default:
8085 goto scan_number_done;
8086 }
8087
8088 scan_number_any1:
8089 // state: we just parsed a number 0-9 (maybe with a leading minus sign)
8090 switch (get())
8091 {
8092 case '0':
8093 case '1':
8094 case '2':
8095 case '3':
8096 case '4':
8097 case '5':
8098 case '6':
8099 case '7':
8100 case '8':
8101 case '9':
8102 {
8103 add(current);
8104 goto scan_number_any1;
8105 }
8106
8107 case '.':
8108 {
8109 add(decimal_point_char);
8110 decimal_point_position = token_buffer.size() - 1;
8111 goto scan_number_decimal1;
8112 }
8113
8114 case 'e':
8115 case 'E':
8116 {
8117 add(current);
8118 goto scan_number_exponent;
8119 }
8120
8121 default:
8122 goto scan_number_done;
8123 }
8124
8125 scan_number_decimal1:
8126 // state: we just parsed a decimal point
8127 number_type = token_type::value_float;
8128 switch (get())
8129 {
8130 case '0':
8131 case '1':
8132 case '2':
8133 case '3':
8134 case '4':
8135 case '5':
8136 case '6':
8137 case '7':
8138 case '8':
8139 case '9':
8140 {
8141 add(current);
8142 goto scan_number_decimal2;
8143 }
8144
8145 default:
8146 {
8147 error_message = "invalid number; expected digit after '.'";
8148 return token_type::parse_error;
8149 }
8150 }
8151
8152 scan_number_decimal2:
8153 // we just parsed at least one number after a decimal point
8154 switch (get())
8155 {
8156 case '0':
8157 case '1':
8158 case '2':
8159 case '3':
8160 case '4':
8161 case '5':
8162 case '6':
8163 case '7':
8164 case '8':
8165 case '9':
8166 {
8167 add(current);
8168 goto scan_number_decimal2;
8169 }
8170
8171 case 'e':
8172 case 'E':
8173 {
8174 add(current);
8175 goto scan_number_exponent;
8176 }
8177
8178 default:
8179 goto scan_number_done;
8180 }
8181
8182 scan_number_exponent:
8183 // we just parsed an exponent
8184 number_type = token_type::value_float;
8185 switch (get())
8186 {
8187 case '+':
8188 case '-':
8189 {
8190 add(current);
8191 goto scan_number_sign;
8192 }
8193
8194 case '0':
8195 case '1':
8196 case '2':
8197 case '3':
8198 case '4':
8199 case '5':
8200 case '6':
8201 case '7':
8202 case '8':
8203 case '9':
8204 {
8205 add(current);
8206 goto scan_number_any2;
8207 }
8208
8209 default:
8210 {
8211 error_message =
8212 "invalid number; expected '+', '-', or digit after exponent";
8213 return token_type::parse_error;
8214 }
8215 }
8216
8217 scan_number_sign:
8218 // we just parsed an exponent sign
8219 switch (get())
8220 {
8221 case '0':
8222 case '1':
8223 case '2':
8224 case '3':
8225 case '4':
8226 case '5':
8227 case '6':
8228 case '7':
8229 case '8':
8230 case '9':
8231 {
8232 add(current);
8233 goto scan_number_any2;
8234 }
8235
8236 default:
8237 {
8238 error_message = "invalid number; expected digit after exponent sign";
8239 return token_type::parse_error;
8240 }
8241 }
8242
8243 scan_number_any2:
8244 // we just parsed a number after the exponent or exponent sign
8245 switch (get())
8246 {
8247 case '0':
8248 case '1':
8249 case '2':
8250 case '3':
8251 case '4':
8252 case '5':
8253 case '6':
8254 case '7':
8255 case '8':
8256 case '9':
8257 {
8258 add(current);
8259 goto scan_number_any2;
8260 }
8261
8262 default:
8263 goto scan_number_done;
8264 }
8265
8266 scan_number_done:
8267 // unget the character after the number (we only read it to know that
8268 // we are done scanning a number)
8269 unget();
8270
8271 char* endptr = nullptr; // NOLINT(misc-const-correctness,cppcoreguidelines-pro-type-vararg,hicpp-vararg)
8272 errno = 0;
8273
8274 // try to parse integers first and fall back to floats
8275 if (number_type == token_type::value_unsigned)
8276 {
8277 const auto x = std::strtoull(token_buffer.data(), &endptr, 10);
8278
8279 // we checked the number format before
8280 JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
8281
8282 if (errno != ERANGE)
8283 {
8284 value_unsigned = static_cast<number_unsigned_t>(x);
8285 if (value_unsigned == x)
8286 {
8287 return token_type::value_unsigned;
8288 }
8289 }
8290 }
8291 else if (number_type == token_type::value_integer)
8292 {
8293 const auto x = std::strtoll(token_buffer.data(), &endptr, 10);
8294
8295 // we checked the number format before
8296 JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
8297
8298 if (errno != ERANGE)
8299 {
8300 value_integer = static_cast<number_integer_t>(x);
8301 if (value_integer == x)
8302 {
8303 return token_type::value_integer;
8304 }
8305 }
8306 }
8307
8308 // this code is reached if we parse a floating-point number or if an
8309 // integer conversion above failed
8310 strtof(value_float, token_buffer.data(), &endptr);
8311
8312 // we checked the number format before
8313 JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
8314
8315 return token_type::value_float;
8316 }
8317
8318 /*!
8319 @param[in] literal_text the literal text to expect
8320 @param[in] length the length of the passed literal text
8321 @param[in] return_type the token type to return on success
8322 */
8323 JSON_HEDLEY_NON_NULL(2)
8324 token_type scan_literal(const char_type* literal_text, const std::size_t length,
8325 token_type return_type)
8326 {
8327 JSON_ASSERT(char_traits<char_type>::to_char_type(current) == literal_text[0]);
8328 for (std::size_t i = 1; i < length; ++i)
8329 {
8330 if (JSON_HEDLEY_UNLIKELY(char_traits<char_type>::to_char_type(get()) != literal_text[i]))
8331 {
8332 error_message = "invalid literal";
8333 return token_type::parse_error;
8334 }
8335 }
8336 return return_type;
8337 }
8338
8339 /////////////////////
8340 // input management
8341 /////////////////////
8342
8343 /// reset token_buffer; current character is beginning of token
8344 void reset() noexcept
8345 {
8346 token_buffer.clear();
8347 token_string.clear();
8348 decimal_point_position = std::string::npos;
8349 token_string.push_back(char_traits<char_type>::to_char_type(current));
8350 }
8351
8352 /*
8353 @brief get next character from the input
8354
8355 This function provides the interface to the used input adapter. It does
8356 not throw in case the input reached EOF, but returns a
8357 `char_traits<char>::eof()` in that case. Stores the scanned characters
8358 for use in error messages.
8359
8360 @return character read from the input
8361 */
8362 char_int_type get()
8363 {
8364 ++position.chars_read_total;
8365 ++position.chars_read_current_line;
8366
8367 if (next_unget)
8368 {
8369 // just reset the next_unget variable and work with current
8370 next_unget = false;
8371 }
8372 else
8373 {
8374 current = ia.get_character();
8375 }
8376
8377 if (JSON_HEDLEY_LIKELY(current != char_traits<char_type>::eof()))
8378 {
8379 token_string.push_back(char_traits<char_type>::to_char_type(current));
8380 }
8381
8382 if (current == '\n')
8383 {
8384 ++position.lines_read;
8385 position.chars_read_current_line = 0;
8386 }
8387
8388 return current;
8389 }
8390
8391 /*!
8392 @brief unget current character (read it again on next get)
8393
8394 We implement unget by setting variable next_unget to true. The input is not
8395 changed - we just simulate ungetting by modifying chars_read_total,
8396 chars_read_current_line, and token_string. The next call to get() will
8397 behave as if the unget character is read again.
8398 */
8399 void unget()
8400 {
8401 next_unget = true;
8402
8403 --position.chars_read_total;
8404
8405 // in case we "unget" a newline, we have to also decrement the lines_read
8406 if (position.chars_read_current_line == 0)
8407 {
8408 if (position.lines_read > 0)
8409 {
8410 --position.lines_read;
8411 }
8412 }
8413 else
8414 {
8415 --position.chars_read_current_line;
8416 }
8417
8418 if (JSON_HEDLEY_LIKELY(current != char_traits<char_type>::eof()))
8419 {
8420 JSON_ASSERT(!token_string.empty());
8421 token_string.pop_back();
8422 }
8423 }
8424
8425 /// add a character to token_buffer
8426 void add(char_int_type c)
8427 {
8428 token_buffer.push_back(static_cast<typename string_t::value_type>(c));
8429 }
8430
8431 public:
8432 /////////////////////
8433 // value getters
8434 /////////////////////
8435
8436 /// return integer value
8437 constexpr number_integer_t get_number_integer() const noexcept
8438 {
8439 return value_integer;
8440 }
8441
8442 /// return unsigned integer value
8443 constexpr number_unsigned_t get_number_unsigned() const noexcept
8444 {
8445 return value_unsigned;
8446 }
8447
8448 /// return floating-point value
8449 constexpr number_float_t get_number_float() const noexcept
8450 {
8451 return value_float;
8452 }
8453
8454 /// return current string value (implicitly resets the token; useful only once)
8455 string_t& get_string()
8456 {
8457 // translate decimal points from locale back to '.' (#4084)
8458 if (decimal_point_char != '.' && decimal_point_position != std::string::npos)
8459 {
8460 token_buffer[decimal_point_position] = '.';
8461 }
8462 return token_buffer;
8463 }
8464
8465 /////////////////////
8466 // diagnostics
8467 /////////////////////
8468
8469 /// return position of last read token
8470 constexpr position_t get_position() const noexcept
8471 {
8472 return position;
8473 }
8474
8475 /// return the last read token (for errors only). Will never contain EOF
8476 /// (an arbitrary value that is not a valid char value, often -1), because
8477 /// 255 may legitimately occur. May contain NUL, which should be escaped.
8478 std::string get_token_string() const
8479 {
8480 // escape control characters
8481 std::string result;
8482 for (const auto c : token_string)
8483 {
8484 if (static_cast<unsigned char>(c) <= '\x1F')
8485 {
8486 // escape control characters
8487 std::array<char, 9> cs{{}};
8488 static_cast<void>((std::snprintf)(cs.data(), cs.size(), "<U+%.4X>", static_cast<unsigned char>(c))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
8489 result += cs.data();
8490 }
8491 else
8492 {
8493 // add character as is
8494 result.push_back(static_cast<std::string::value_type>(c));
8495 }
8496 }
8497
8498 return result;
8499 }
8500
8501 /// return syntax error message
8502 JSON_HEDLEY_RETURNS_NON_NULL
8503 constexpr const char* get_error_message() const noexcept
8504 {
8505 return error_message;
8506 }
8507
8508 /////////////////////
8509 // actual scanner
8510 /////////////////////
8511
8512 /*!
8513 @brief skip the UTF-8 byte order mark
8514 @return true iff there is no BOM or the correct BOM has been skipped
8515 */
8516 bool skip_bom()
8517 {
8518 if (get() == 0xEF)
8519 {
8520 // check if we completely parse the BOM
8521 return get() == 0xBB && get() == 0xBF;
8522 }
8523
8524 // the first character is not the beginning of the BOM; unget it to
8525 // process is later
8526 unget();
8527 return true;
8528 }
8529
8530 void skip_whitespace()
8531 {
8532 do
8533 {
8534 get();
8535 }
8536 while (current == ' ' || current == '\t' || current == '\n' || current == '\r');
8537 }
8538
8539 token_type scan()
8540 {
8541 // initially, skip the BOM
8542 if (position.chars_read_total == 0 && !skip_bom())
8543 {
8544 error_message = "invalid BOM; must be 0xEF 0xBB 0xBF if given";
8545 return token_type::parse_error;
8546 }
8547
8548 // read next character and ignore whitespace
8549 skip_whitespace();
8550
8551 // ignore comments
8552 while (ignore_comments && current == '/')
8553 {
8554 if (!scan_comment())
8555 {
8556 return token_type::parse_error;
8557 }
8558
8559 // skip following whitespace
8560 skip_whitespace();
8561 }
8562
8563 switch (current)
8564 {
8565 // structural characters
8566 case '[':
8567 return token_type::begin_array;
8568 case ']':
8569 return token_type::end_array;
8570 case '{':
8571 return token_type::begin_object;
8572 case '}':
8573 return token_type::end_object;
8574 case ':':
8575 return token_type::name_separator;
8576 case ',':
8577 return token_type::value_separator;
8578
8579 // literals
8580 case 't':
8581 {
8582 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')}};
8583 return scan_literal(true_literal.data(), true_literal.size(), token_type::literal_true);
8584 }
8585 case 'f':
8586 {
8587 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')}};
8588 return scan_literal(false_literal.data(), false_literal.size(), token_type::literal_false);
8589 }
8590 case 'n':
8591 {
8592 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')}};
8593 return scan_literal(null_literal.data(), null_literal.size(), token_type::literal_null);
8594 }
8595
8596 // string
8597 case '\"':
8598 return scan_string();
8599
8600 // number
8601 case '-':
8602 case '0':
8603 case '1':
8604 case '2':
8605 case '3':
8606 case '4':
8607 case '5':
8608 case '6':
8609 case '7':
8610 case '8':
8611 case '9':
8612 return scan_number();
8613
8614 // end of input (the null byte is needed when parsing from
8615 // string literals)
8616 case '\0':
8617 case char_traits<char_type>::eof():
8618 return token_type::end_of_input;
8619
8620 // error
8621 default:
8622 error_message = "invalid literal";
8623 return token_type::parse_error;
8624 }
8625 }
8626
8627 private:
8628 /// input adapter
8629 InputAdapterType ia;
8630
8631 /// whether comments should be ignored (true) or signaled as errors (false)
8632 const bool ignore_comments = false;
8633
8634 /// the current character
8635 char_int_type current = char_traits<char_type>::eof();
8636
8637 /// whether the next get() call should just return current
8638 bool next_unget = false;
8639
8640 /// the start position of the current token
8641 position_t position {};
8642
8643 /// raw input token string (for error messages)
8644 std::vector<char_type> token_string {};
8645
8646 /// buffer for variable-length tokens (numbers, strings)
8647 string_t token_buffer {};
8648
8649 /// a description of occurred lexer errors
8650 const char* error_message = "";
8651
8652 // number values
8653 number_integer_t value_integer = 0;
8654 number_unsigned_t value_unsigned = 0;
8655 number_float_t value_float = 0;
8656
8657 /// the decimal point
8658 const char_int_type decimal_point_char = '.';
8659 /// the position of the decimal point in the input
8660 std::size_t decimal_point_position = std::string::npos;
8661 };
8662
8663 } // namespace detail
8664 NLOHMANN_JSON_NAMESPACE_END
8665
8666 // #include <nlohmann/detail/macro_scope.hpp>
8667
8668 // #include <nlohmann/detail/string_concat.hpp>
8669
8670 NLOHMANN_JSON_NAMESPACE_BEGIN
8671
8672 /*!
8673 @brief SAX interface
8674
8675 This class describes the SAX interface used by @ref nlohmann::json::sax_parse.
8676 Each function is called in different situations while the input is parsed. The
8677 boolean return value informs the parser whether to continue processing the
8678 input.
8679 */
8680 template<typename BasicJsonType>
8681 struct json_sax
8682 {
8683 using number_integer_t = typename BasicJsonType::number_integer_t;
8684 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
8685 using number_float_t = typename BasicJsonType::number_float_t;
8686 using string_t = typename BasicJsonType::string_t;
8687 using binary_t = typename BasicJsonType::binary_t;
8688
8689 /*!
8690 @brief a null value was read
8691 @return whether parsing should proceed
8692 */
8693 virtual bool null() = 0;
8694
8695 /*!
8696 @brief a boolean value was read
8697 @param[in] val boolean value
8698 @return whether parsing should proceed
8699 */
8700 virtual bool boolean(bool val) = 0;
8701
8702 /*!
8703 @brief an integer number was read
8704 @param[in] val integer value
8705 @return whether parsing should proceed
8706 */
8707 virtual bool number_integer(number_integer_t val) = 0;
8708
8709 /*!
8710 @brief an unsigned integer number was read
8711 @param[in] val unsigned integer value
8712 @return whether parsing should proceed
8713 */
8714 virtual bool number_unsigned(number_unsigned_t val) = 0;
8715
8716 /*!
8717 @brief a floating-point number was read
8718 @param[in] val floating-point value
8719 @param[in] s raw token value
8720 @return whether parsing should proceed
8721 */
8722 virtual bool number_float(number_float_t val, const string_t& s) = 0;
8723
8724 /*!
8725 @brief a string value was read
8726 @param[in] val string value
8727 @return whether parsing should proceed
8728 @note It is safe to move the passed string value.
8729 */
8730 virtual bool string(string_t& val) = 0;
8731
8732 /*!
8733 @brief a binary value was read
8734 @param[in] val binary value
8735 @return whether parsing should proceed
8736 @note It is safe to move the passed binary value.
8737 */
8738 virtual bool binary(binary_t& val) = 0;
8739
8740 /*!
8741 @brief the beginning of an object was read
8742 @param[in] elements number of object elements or -1 if unknown
8743 @return whether parsing should proceed
8744 @note binary formats may report the number of elements
8745 */
8746 virtual bool start_object(std::size_t elements) = 0;
8747
8748 /*!
8749 @brief an object key was read
8750 @param[in] val object key
8751 @return whether parsing should proceed
8752 @note It is safe to move the passed string.
8753 */
8754 virtual bool key(string_t& val) = 0;
8755
8756 /*!
8757 @brief the end of an object was read
8758 @return whether parsing should proceed
8759 */
8760 virtual bool end_object() = 0;
8761
8762 /*!
8763 @brief the beginning of an array was read
8764 @param[in] elements number of array elements or -1 if unknown
8765 @return whether parsing should proceed
8766 @note binary formats may report the number of elements
8767 */
8768 virtual bool start_array(std::size_t elements) = 0;
8769
8770 /*!
8771 @brief the end of an array was read
8772 @return whether parsing should proceed
8773 */
8774 virtual bool end_array() = 0;
8775
8776 /*!
8777 @brief a parse error occurred
8778 @param[in] position the position in the input where the error occurs
8779 @param[in] last_token the last read token
8780 @param[in] ex an exception object describing the error
8781 @return whether parsing should proceed (must return false)
8782 */
8783 virtual bool parse_error(std::size_t position,
8784 const std::string& last_token,
8785 const detail::exception& ex) = 0;
8786
8787 json_sax() = default;
8788 json_sax(const json_sax&) = default;
8789 json_sax(json_sax&&) noexcept = default;
8790 json_sax& operator=(const json_sax&) = default;
8791 json_sax& operator=(json_sax&&) noexcept = default;
8792 virtual ~json_sax() = default;
8793 };
8794
8795 namespace detail
8796 {
8797 constexpr std::size_t unknown_size()
8798 {
8799 return (std::numeric_limits<std::size_t>::max)();
8800 }
8801
8802 /*!
8803 @brief SAX implementation to create a JSON value from SAX events
8804
8805 This class implements the @ref json_sax interface and processes the SAX events
8806 to create a JSON value which makes it basically a DOM parser. The structure or
8807 hierarchy of the JSON value is managed by the stack `ref_stack` which contains
8808 a pointer to the respective array or object for each recursion depth.
8809
8810 After successful parsing, the value that is passed by reference to the
8811 constructor contains the parsed value.
8812
8813 @tparam BasicJsonType the JSON type
8814 */
8815 template<typename BasicJsonType, typename InputAdapterType>
8816 class json_sax_dom_parser
8817 {
8818 public:
8819 using number_integer_t = typename BasicJsonType::number_integer_t;
8820 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
8821 using number_float_t = typename BasicJsonType::number_float_t;
8822 using string_t = typename BasicJsonType::string_t;
8823 using binary_t = typename BasicJsonType::binary_t;
8824 using lexer_t = lexer<BasicJsonType, InputAdapterType>;
8825
8826 /*!
8827 @param[in,out] r reference to a JSON value that is manipulated while
8828 parsing
8829 @param[in] allow_exceptions_ whether parse errors yield exceptions
8830 */
8831 explicit json_sax_dom_parser(BasicJsonType& r, const bool allow_exceptions_ = true, lexer_t* lexer_ = nullptr)
8832 : root(r), allow_exceptions(allow_exceptions_), m_lexer_ref(lexer_)
8833 {}
8834
8835 // make class move-only
8836 json_sax_dom_parser(const json_sax_dom_parser&) = delete;
8837 json_sax_dom_parser(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
8838 json_sax_dom_parser& operator=(const json_sax_dom_parser&) = delete;
8839 json_sax_dom_parser& operator=(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
8840 ~json_sax_dom_parser() = default;
8841
8842 bool null()
8843 {
8844 handle_value(nullptr);
8845 return true;
8846 }
8847
8848 bool boolean(bool val)
8849 {
8850 handle_value(val);
8851 return true;
8852 }
8853
8854 bool number_integer(number_integer_t val)
8855 {
8856 handle_value(val);
8857 return true;
8858 }
8859
8860 bool number_unsigned(number_unsigned_t val)
8861 {
8862 handle_value(val);
8863 return true;
8864 }
8865
8866 bool number_float(number_float_t val, const string_t& /*unused*/)
8867 {
8868 handle_value(val);
8869 return true;
8870 }
8871
8872 bool string(string_t& val)
8873 {
8874 handle_value(val);
8875 return true;
8876 }
8877
8878 bool binary(binary_t& val)
8879 {
8880 handle_value(std::move(val));
8881 return true;
8882 }
8883
8884 bool start_object(std::size_t len)
8885 {
8886 ref_stack.push_back(handle_value(BasicJsonType::value_t::object));
8887
8888 #if JSON_DIAGNOSTIC_POSITIONS
8889 // Manually set the start position of the object here.
8890 // Ensure this is after the call to handle_value to ensure correct start position.
8891 if (m_lexer_ref)
8892 {
8893 // Lexer has read the first character of the object, so
8894 // subtract 1 from the position to get the correct start position.
8895 ref_stack.back()->start_position = m_lexer_ref->get_position() - 1;
8896 }
8897 #endif
8898
8899 if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
8900 {
8901 JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
8902 }
8903
8904 return true;
8905 }
8906
8907 bool key(string_t& val)
8908 {
8909 JSON_ASSERT(!ref_stack.empty());
8910 JSON_ASSERT(ref_stack.back()->is_object());
8911
8912 // add null at given key and store the reference for later
8913 object_element = &(ref_stack.back()->m_data.m_value.object->operator[](val));
8914 return true;
8915 }
8916
8917 bool end_object()
8918 {
8919 JSON_ASSERT(!ref_stack.empty());
8920 JSON_ASSERT(ref_stack.back()->is_object());
8921
8922 #if JSON_DIAGNOSTIC_POSITIONS
8923 if (m_lexer_ref)
8924 {
8925 // Lexer's position is past the closing brace, so set that as the end position.
8926 ref_stack.back()->end_position = m_lexer_ref->get_position();
8927 }
8928 #endif
8929
8930 ref_stack.back()->set_parents();
8931 ref_stack.pop_back();
8932 return true;
8933 }
8934
8935 bool start_array(std::size_t len)
8936 {
8937 ref_stack.push_back(handle_value(BasicJsonType::value_t::array));
8938
8939 #if JSON_DIAGNOSTIC_POSITIONS
8940 // Manually set the start position of the array here.
8941 // Ensure this is after the call to handle_value to ensure correct start position.
8942 if (m_lexer_ref)
8943 {
8944 ref_stack.back()->start_position = m_lexer_ref->get_position() - 1;
8945 }
8946 #endif
8947
8948 if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
8949 {
8950 JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
8951 }
8952
8953 return true;
8954 }
8955
8956 bool end_array()
8957 {
8958 JSON_ASSERT(!ref_stack.empty());
8959 JSON_ASSERT(ref_stack.back()->is_array());
8960
8961 #if JSON_DIAGNOSTIC_POSITIONS
8962 if (m_lexer_ref)
8963 {
8964 // Lexer's position is past the closing bracket, so set that as the end position.
8965 ref_stack.back()->end_position = m_lexer_ref->get_position();
8966 }
8967 #endif
8968
8969 ref_stack.back()->set_parents();
8970 ref_stack.pop_back();
8971 return true;
8972 }
8973
8974 template<class Exception>
8975 bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
8976 const Exception& ex)
8977 {
8978 errored = true;
8979 static_cast<void>(ex);
8980 if (allow_exceptions)
8981 {
8982 JSON_THROW(ex);
8983 }
8984 return false;
8985 }
8986
8987 constexpr bool is_errored() const
8988 {
8989 return errored;
8990 }
8991
8992 private:
8993
8994 #if JSON_DIAGNOSTIC_POSITIONS
8995 void handle_diagnostic_positions_for_json_value(BasicJsonType& v)
8996 {
8997 if (m_lexer_ref)
8998 {
8999 // Lexer has read past the current field value, so set the end position to the current position.
9000 // The start position will be set below based on the length of the string representation
9001 // of the value.
9002 v.end_position = m_lexer_ref->get_position();
9003
9004 switch (v.type())
9005 {
9006 case value_t::boolean:
9007 {
9008 // 4 and 5 are the string length of "true" and "false"
9009 v.start_position = v.end_position - (v.m_data.m_value.boolean ? 4 : 5);
9010 break;
9011 }
9012
9013 case value_t::null:
9014 {
9015 // 4 is the string length of "null"
9016 v.start_position = v.end_position - 4;
9017 break;
9018 }
9019
9020 case value_t::string:
9021 {
9022 // include the length of the quotes, which is 2
9023 v.start_position = v.end_position - v.m_data.m_value.string->size() - 2;
9024 break;
9025 }
9026
9027 // As we handle the start and end positions for values created during parsing,
9028 // we do not expect the following value type to be called. Regardless, set the positions
9029 // in case this is created manually or through a different constructor. Exclude from lcov
9030 // since the exact condition of this switch is esoteric.
9031 // LCOV_EXCL_START
9032 case value_t::discarded:
9033 {
9034 v.end_position = std::string::npos;
9035 v.start_position = v.end_position;
9036 break;
9037 }
9038 // LCOV_EXCL_STOP
9039 case value_t::binary:
9040 case value_t::number_integer:
9041 case value_t::number_unsigned:
9042 case value_t::number_float:
9043 {
9044 v.start_position = v.end_position - m_lexer_ref->get_string().size();
9045 break;
9046 }
9047 case value_t::object:
9048 case value_t::array:
9049 {
9050 // object and array are handled in start_object() and start_array() handlers
9051 // skip setting the values here.
9052 break;
9053 }
9054 default: // LCOV_EXCL_LINE
9055 // Handle all possible types discretely, default handler should never be reached.
9056 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert,-warnings-as-errors) LCOV_EXCL_LINE
9057 }
9058 }
9059 }
9060 #endif
9061
9062 /*!
9063 @invariant If the ref stack is empty, then the passed value will be the new
9064 root.
9065 @invariant If the ref stack contains a value, then it is an array or an
9066 object to which we can add elements
9067 */
9068 template<typename Value>
9069 JSON_HEDLEY_RETURNS_NON_NULL
9070 BasicJsonType* handle_value(Value&& v)
9071 {
9072 if (ref_stack.empty())
9073 {
9074 root = BasicJsonType(std::forward<Value>(v));
9075
9076 #if JSON_DIAGNOSTIC_POSITIONS
9077 handle_diagnostic_positions_for_json_value(root);
9078 #endif
9079
9080 return &root;
9081 }
9082
9083 JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
9084
9085 if (ref_stack.back()->is_array())
9086 {
9087 ref_stack.back()->m_data.m_value.array->emplace_back(std::forward<Value>(v));
9088
9089 #if JSON_DIAGNOSTIC_POSITIONS
9090 handle_diagnostic_positions_for_json_value(ref_stack.back()->m_data.m_value.array->back());
9091 #endif
9092
9093 return &(ref_stack.back()->m_data.m_value.array->back());
9094 }
9095
9096 JSON_ASSERT(ref_stack.back()->is_object());
9097 JSON_ASSERT(object_element);
9098 *object_element = BasicJsonType(std::forward<Value>(v));
9099
9100 #if JSON_DIAGNOSTIC_POSITIONS
9101 handle_diagnostic_positions_for_json_value(*object_element);
9102 #endif
9103
9104 return object_element;
9105 }
9106
9107 /// the parsed JSON value
9108 BasicJsonType& root;
9109 /// stack to model hierarchy of values
9110 std::vector<BasicJsonType*> ref_stack {};
9111 /// helper to hold the reference for the next object element
9112 BasicJsonType* object_element = nullptr;
9113 /// whether a syntax error occurred
9114 bool errored = false;
9115 /// whether to throw exceptions in case of errors
9116 const bool allow_exceptions = true;
9117 /// the lexer reference to obtain the current position
9118 lexer_t* m_lexer_ref = nullptr;
9119 };
9120
9121 template<typename BasicJsonType, typename InputAdapterType>
9122 class json_sax_dom_callback_parser
9123 {
9124 public:
9125 using number_integer_t = typename BasicJsonType::number_integer_t;
9126 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
9127 using number_float_t = typename BasicJsonType::number_float_t;
9128 using string_t = typename BasicJsonType::string_t;
9129 using binary_t = typename BasicJsonType::binary_t;
9130 using parser_callback_t = typename BasicJsonType::parser_callback_t;
9131 using parse_event_t = typename BasicJsonType::parse_event_t;
9132 using lexer_t = lexer<BasicJsonType, InputAdapterType>;
9133
9134 json_sax_dom_callback_parser(BasicJsonType& r,
9135 parser_callback_t cb,
9136 const bool allow_exceptions_ = true,
9137 lexer_t* lexer_ = nullptr)
9138 : root(r), callback(std::move(cb)), allow_exceptions(allow_exceptions_), m_lexer_ref(lexer_)
9139 {
9140 keep_stack.push_back(true);
9141 }
9142
9143 // make class move-only
9144 json_sax_dom_callback_parser(const json_sax_dom_callback_parser&) = delete;
9145 json_sax_dom_callback_parser(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
9146 json_sax_dom_callback_parser& operator=(const json_sax_dom_callback_parser&) = delete;
9147 json_sax_dom_callback_parser& operator=(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
9148 ~json_sax_dom_callback_parser() = default;
9149
9150 bool null()
9151 {
9152 handle_value(nullptr);
9153 return true;
9154 }
9155
9156 bool boolean(bool val)
9157 {
9158 handle_value(val);
9159 return true;
9160 }
9161
9162 bool number_integer(number_integer_t val)
9163 {
9164 handle_value(val);
9165 return true;
9166 }
9167
9168 bool number_unsigned(number_unsigned_t val)
9169 {
9170 handle_value(val);
9171 return true;
9172 }
9173
9174 bool number_float(number_float_t val, const string_t& /*unused*/)
9175 {
9176 handle_value(val);
9177 return true;
9178 }
9179
9180 bool string(string_t& val)
9181 {
9182 handle_value(val);
9183 return true;
9184 }
9185
9186 bool binary(binary_t& val)
9187 {
9188 handle_value(std::move(val));
9189 return true;
9190 }
9191
9192 bool start_object(std::size_t len)
9193 {
9194 // check callback for object start
9195 const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::object_start, discarded);
9196 keep_stack.push_back(keep);
9197
9198 auto val = handle_value(BasicJsonType::value_t::object, true);
9199 ref_stack.push_back(val.second);
9200
9201 if (ref_stack.back())
9202 {
9203
9204 #if JSON_DIAGNOSTIC_POSITIONS
9205 // Manually set the start position of the object here.
9206 // Ensure this is after the call to handle_value to ensure correct start position.
9207 if (m_lexer_ref)
9208 {
9209 // Lexer has read the first character of the object, so
9210 // subtract 1 from the position to get the correct start position.
9211 ref_stack.back()->start_position = m_lexer_ref->get_position() - 1;
9212 }
9213 #endif
9214
9215 // check object limit
9216 if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
9217 {
9218 JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
9219 }
9220 }
9221 return true;
9222 }
9223
9224 bool key(string_t& val)
9225 {
9226 BasicJsonType k = BasicJsonType(val);
9227
9228 // check callback for key
9229 const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::key, k);
9230 key_keep_stack.push_back(keep);
9231
9232 // add discarded value at given key and store the reference for later
9233 if (keep && ref_stack.back())
9234 {
9235 object_element = &(ref_stack.back()->m_data.m_value.object->operator[](val) = discarded);
9236 }
9237
9238 return true;
9239 }
9240
9241 bool end_object()
9242 {
9243 if (ref_stack.back())
9244 {
9245 if (!callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back()))
9246 {
9247 // discard object
9248 *ref_stack.back() = discarded;
9249
9250 #if JSON_DIAGNOSTIC_POSITIONS
9251 // Set start/end positions for discarded object.
9252 handle_diagnostic_positions_for_json_value(*ref_stack.back());
9253 #endif
9254 }
9255 else
9256 {
9257
9258 #if JSON_DIAGNOSTIC_POSITIONS
9259 if (m_lexer_ref)
9260 {
9261 // Lexer's position is past the closing brace, so set that as the end position.
9262 ref_stack.back()->end_position = m_lexer_ref->get_position();
9263 }
9264 #endif
9265
9266 ref_stack.back()->set_parents();
9267 }
9268 }
9269
9270 JSON_ASSERT(!ref_stack.empty());
9271 JSON_ASSERT(!keep_stack.empty());
9272 ref_stack.pop_back();
9273 keep_stack.pop_back();
9274
9275 if (!ref_stack.empty() && ref_stack.back() && ref_stack.back()->is_structured())
9276 {
9277 // remove discarded value
9278 for (auto it = ref_stack.back()->begin(); it != ref_stack.back()->end(); ++it)
9279 {
9280 if (it->is_discarded())
9281 {
9282 ref_stack.back()->erase(it);
9283 break;
9284 }
9285 }
9286 }
9287
9288 return true;
9289 }
9290
9291 bool start_array(std::size_t len)
9292 {
9293 const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::array_start, discarded);
9294 keep_stack.push_back(keep);
9295
9296 auto val = handle_value(BasicJsonType::value_t::array, true);
9297 ref_stack.push_back(val.second);
9298
9299 if (ref_stack.back())
9300 {
9301
9302 #if JSON_DIAGNOSTIC_POSITIONS
9303 // Manually set the start position of the array here.
9304 // Ensure this is after the call to handle_value to ensure correct start position.
9305 if (m_lexer_ref)
9306 {
9307 // Lexer has read the first character of the array, so
9308 // subtract 1 from the position to get the correct start position.
9309 ref_stack.back()->start_position = m_lexer_ref->get_position() - 1;
9310 }
9311 #endif
9312
9313 // check array limit
9314 if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
9315 {
9316 JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
9317 }
9318 }
9319
9320 return true;
9321 }
9322
9323 bool end_array()
9324 {
9325 bool keep = true;
9326
9327 if (ref_stack.back())
9328 {
9329 keep = callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back());
9330 if (keep)
9331 {
9332
9333 #if JSON_DIAGNOSTIC_POSITIONS
9334 if (m_lexer_ref)
9335 {
9336 // Lexer's position is past the closing bracket, so set that as the end position.
9337 ref_stack.back()->end_position = m_lexer_ref->get_position();
9338 }
9339 #endif
9340
9341 ref_stack.back()->set_parents();
9342 }
9343 else
9344 {
9345 // discard array
9346 *ref_stack.back() = discarded;
9347
9348 #if JSON_DIAGNOSTIC_POSITIONS
9349 // Set start/end positions for discarded array.
9350 handle_diagnostic_positions_for_json_value(*ref_stack.back());
9351 #endif
9352 }
9353 }
9354
9355 JSON_ASSERT(!ref_stack.empty());
9356 JSON_ASSERT(!keep_stack.empty());
9357 ref_stack.pop_back();
9358 keep_stack.pop_back();
9359
9360 // remove discarded value
9361 if (!keep && !ref_stack.empty() && ref_stack.back()->is_array())
9362 {
9363 ref_stack.back()->m_data.m_value.array->pop_back();
9364 }
9365
9366 return true;
9367 }
9368
9369 template<class Exception>
9370 bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
9371 const Exception& ex)
9372 {
9373 errored = true;
9374 static_cast<void>(ex);
9375 if (allow_exceptions)
9376 {
9377 JSON_THROW(ex);
9378 }
9379 return false;
9380 }
9381
9382 constexpr bool is_errored() const
9383 {
9384 return errored;
9385 }
9386
9387 private:
9388
9389 #if JSON_DIAGNOSTIC_POSITIONS
9390 void handle_diagnostic_positions_for_json_value(BasicJsonType& v)
9391 {
9392 if (m_lexer_ref)
9393 {
9394 // Lexer has read past the current field value, so set the end position to the current position.
9395 // The start position will be set below based on the length of the string representation
9396 // of the value.
9397 v.end_position = m_lexer_ref->get_position();
9398
9399 switch (v.type())
9400 {
9401 case value_t::boolean:
9402 {
9403 // 4 and 5 are the string length of "true" and "false"
9404 v.start_position = v.end_position - (v.m_data.m_value.boolean ? 4 : 5);
9405 break;
9406 }
9407
9408 case value_t::null:
9409 {
9410 // 4 is the string length of "null"
9411 v.start_position = v.end_position - 4;
9412 break;
9413 }
9414
9415 case value_t::string:
9416 {
9417 // include the length of the quotes, which is 2
9418 v.start_position = v.end_position - v.m_data.m_value.string->size() - 2;
9419 break;
9420 }
9421
9422 case value_t::discarded:
9423 {
9424 v.end_position = std::string::npos;
9425 v.start_position = v.end_position;
9426 break;
9427 }
9428
9429 case value_t::binary:
9430 case value_t::number_integer:
9431 case value_t::number_unsigned:
9432 case value_t::number_float:
9433 {
9434 v.start_position = v.end_position - m_lexer_ref->get_string().size();
9435 break;
9436 }
9437
9438 case value_t::object:
9439 case value_t::array:
9440 {
9441 // object and array are handled in start_object() and start_array() handlers
9442 // skip setting the values here.
9443 break;
9444 }
9445 default: // LCOV_EXCL_LINE
9446 // Handle all possible types discretely, default handler should never be reached.
9447 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert,-warnings-as-errors) LCOV_EXCL_LINE
9448 }
9449 }
9450 }
9451 #endif
9452
9453 /*!
9454 @param[in] v value to add to the JSON value we build during parsing
9455 @param[in] skip_callback whether we should skip calling the callback
9456 function; this is required after start_array() and
9457 start_object() SAX events, because otherwise we would call the
9458 callback function with an empty array or object, respectively.
9459
9460 @invariant If the ref stack is empty, then the passed value will be the new
9461 root.
9462 @invariant If the ref stack contains a value, then it is an array or an
9463 object to which we can add elements
9464
9465 @return pair of boolean (whether value should be kept) and pointer (to the
9466 passed value in the ref_stack hierarchy; nullptr if not kept)
9467 */
9468 template<typename Value>
9469 std::pair<bool, BasicJsonType*> handle_value(Value&& v, const bool skip_callback = false)
9470 {
9471 JSON_ASSERT(!keep_stack.empty());
9472
9473 // do not handle this value if we know it would be added to a discarded
9474 // container
9475 if (!keep_stack.back())
9476 {
9477 return {false, nullptr};
9478 }
9479
9480 // create value
9481 auto value = BasicJsonType(std::forward<Value>(v));
9482
9483 #if JSON_DIAGNOSTIC_POSITIONS
9484 handle_diagnostic_positions_for_json_value(value);
9485 #endif
9486
9487 // check callback
9488 const bool keep = skip_callback || callback(static_cast<int>(ref_stack.size()), parse_event_t::value, value);
9489
9490 // do not handle this value if we just learnt it shall be discarded
9491 if (!keep)
9492 {
9493 return {false, nullptr};
9494 }
9495
9496 if (ref_stack.empty())
9497 {
9498 root = std::move(value);
9499 return {true, & root};
9500 }
9501
9502 // skip this value if we already decided to skip the parent
9503 // (https://github.com/nlohmann/json/issues/971#issuecomment-413678360)
9504 if (!ref_stack.back())
9505 {
9506 return {false, nullptr};
9507 }
9508
9509 // we now only expect arrays and objects
9510 JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
9511
9512 // array
9513 if (ref_stack.back()->is_array())
9514 {
9515 ref_stack.back()->m_data.m_value.array->emplace_back(std::move(value));
9516 return {true, & (ref_stack.back()->m_data.m_value.array->back())};
9517 }
9518
9519 // object
9520 JSON_ASSERT(ref_stack.back()->is_object());
9521 // check if we should store an element for the current key
9522 JSON_ASSERT(!key_keep_stack.empty());
9523 const bool store_element = key_keep_stack.back();
9524 key_keep_stack.pop_back();
9525
9526 if (!store_element)
9527 {
9528 return {false, nullptr};
9529 }
9530
9531 JSON_ASSERT(object_element);
9532 *object_element = std::move(value);
9533 return {true, object_element};
9534 }
9535
9536 /// the parsed JSON value
9537 BasicJsonType& root;
9538 /// stack to model hierarchy of values
9539 std::vector<BasicJsonType*> ref_stack {};
9540 /// stack to manage which values to keep
9541 std::vector<bool> keep_stack {}; // NOLINT(readability-redundant-member-init)
9542 /// stack to manage which object keys to keep
9543 std::vector<bool> key_keep_stack {}; // NOLINT(readability-redundant-member-init)
9544 /// helper to hold the reference for the next object element
9545 BasicJsonType* object_element = nullptr;
9546 /// whether a syntax error occurred
9547 bool errored = false;
9548 /// callback function
9549 const parser_callback_t callback = nullptr;
9550 /// whether to throw exceptions in case of errors
9551 const bool allow_exceptions = true;
9552 /// a discarded value for the callback
9553 BasicJsonType discarded = BasicJsonType::value_t::discarded;
9554 /// the lexer reference to obtain the current position
9555 lexer_t* m_lexer_ref = nullptr;
9556 };
9557
9558 template<typename BasicJsonType>
9559 class json_sax_acceptor
9560 {
9561 public:
9562 using number_integer_t = typename BasicJsonType::number_integer_t;
9563 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
9564 using number_float_t = typename BasicJsonType::number_float_t;
9565 using string_t = typename BasicJsonType::string_t;
9566 using binary_t = typename BasicJsonType::binary_t;
9567
9568 bool null()
9569 {
9570 return true;
9571 }
9572
9573 bool boolean(bool /*unused*/)
9574 {
9575 return true;
9576 }
9577
9578 bool number_integer(number_integer_t /*unused*/)
9579 {
9580 return true;
9581 }
9582
9583 bool number_unsigned(number_unsigned_t /*unused*/)
9584 {
9585 return true;
9586 }
9587
9588 bool number_float(number_float_t /*unused*/, const string_t& /*unused*/)
9589 {
9590 return true;
9591 }
9592
9593 bool string(string_t& /*unused*/)
9594 {
9595 return true;
9596 }
9597
9598 bool binary(binary_t& /*unused*/)
9599 {
9600 return true;
9601 }
9602
9603 bool start_object(std::size_t /*unused*/ = detail::unknown_size())
9604 {
9605 return true;
9606 }
9607
9608 bool key(string_t& /*unused*/)
9609 {
9610 return true;
9611 }
9612
9613 bool end_object()
9614 {
9615 return true;
9616 }
9617
9618 bool start_array(std::size_t /*unused*/ = detail::unknown_size())
9619 {
9620 return true;
9621 }
9622
9623 bool end_array()
9624 {
9625 return true;
9626 }
9627
9628 bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const detail::exception& /*unused*/)
9629 {
9630 return false;
9631 }
9632 };
9633
9634 } // namespace detail
9635 NLOHMANN_JSON_NAMESPACE_END
9636
9637 // #include <nlohmann/detail/input/lexer.hpp>
9638
9639 // #include <nlohmann/detail/macro_scope.hpp>
9640
9641 // #include <nlohmann/detail/meta/is_sax.hpp>
9642 // __ _____ _____ _____
9643 // __| | __| | | | JSON for Modern C++
9644 // | | |__ | | | | | | version 3.12.0
9645 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
9646 //
9647 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
9648 // SPDX-License-Identifier: MIT
9649
9650
9651
9652 #include <cstdint> // size_t
9653 #include <utility> // declval
9654 #include <string> // string
9655
9656 // #include <nlohmann/detail/abi_macros.hpp>
9657
9658 // #include <nlohmann/detail/meta/detected.hpp>
9659
9660 // #include <nlohmann/detail/meta/type_traits.hpp>
9661
9662
9663 NLOHMANN_JSON_NAMESPACE_BEGIN
9664 namespace detail
9665 {
9666
9667 template<typename T>
9668 using null_function_t = decltype(std::declval<T&>().null());
9669
9670 template<typename T>
9671 using boolean_function_t =
9672 decltype(std::declval<T&>().boolean(std::declval<bool>()));
9673
9674 template<typename T, typename Integer>
9675 using number_integer_function_t =
9676 decltype(std::declval<T&>().number_integer(std::declval<Integer>()));
9677
9678 template<typename T, typename Unsigned>
9679 using number_unsigned_function_t =
9680 decltype(std::declval<T&>().number_unsigned(std::declval<Unsigned>()));
9681
9682 template<typename T, typename Float, typename String>
9683 using number_float_function_t = decltype(std::declval<T&>().number_float(
9684 std::declval<Float>(), std::declval<const String&>()));
9685
9686 template<typename T, typename String>
9687 using string_function_t =
9688 decltype(std::declval<T&>().string(std::declval<String&>()));
9689
9690 template<typename T, typename Binary>
9691 using binary_function_t =
9692 decltype(std::declval<T&>().binary(std::declval<Binary&>()));
9693
9694 template<typename T>
9695 using start_object_function_t =
9696 decltype(std::declval<T&>().start_object(std::declval<std::size_t>()));
9697
9698 template<typename T, typename String>
9699 using key_function_t =
9700 decltype(std::declval<T&>().key(std::declval<String&>()));
9701
9702 template<typename T>
9703 using end_object_function_t = decltype(std::declval<T&>().end_object());
9704
9705 template<typename T>
9706 using start_array_function_t =
9707 decltype(std::declval<T&>().start_array(std::declval<std::size_t>()));
9708
9709 template<typename T>
9710 using end_array_function_t = decltype(std::declval<T&>().end_array());
9711
9712 template<typename T, typename Exception>
9713 using parse_error_function_t = decltype(std::declval<T&>().parse_error(
9714 std::declval<std::size_t>(), std::declval<const std::string&>(),
9715 std::declval<const Exception&>()));
9716
9717 template<typename SAX, typename BasicJsonType>
9718 struct is_sax
9719 {
9720 private:
9721 static_assert(is_basic_json<BasicJsonType>::value,
9722 "BasicJsonType must be of type basic_json<...>");
9723
9724 using number_integer_t = typename BasicJsonType::number_integer_t;
9725 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
9726 using number_float_t = typename BasicJsonType::number_float_t;
9727 using string_t = typename BasicJsonType::string_t;
9728 using binary_t = typename BasicJsonType::binary_t;
9729 using exception_t = typename BasicJsonType::exception;
9730
9731 public:
9732 static constexpr bool value =
9733 is_detected_exact<bool, null_function_t, SAX>::value &&
9734 is_detected_exact<bool, boolean_function_t, SAX>::value &&
9735 is_detected_exact<bool, number_integer_function_t, SAX, number_integer_t>::value &&
9736 is_detected_exact<bool, number_unsigned_function_t, SAX, number_unsigned_t>::value &&
9737 is_detected_exact<bool, number_float_function_t, SAX, number_float_t, string_t>::value &&
9738 is_detected_exact<bool, string_function_t, SAX, string_t>::value &&
9739 is_detected_exact<bool, binary_function_t, SAX, binary_t>::value &&
9740 is_detected_exact<bool, start_object_function_t, SAX>::value &&
9741 is_detected_exact<bool, key_function_t, SAX, string_t>::value &&
9742 is_detected_exact<bool, end_object_function_t, SAX>::value &&
9743 is_detected_exact<bool, start_array_function_t, SAX>::value &&
9744 is_detected_exact<bool, end_array_function_t, SAX>::value &&
9745 is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value;
9746 };
9747
9748 template<typename SAX, typename BasicJsonType>
9749 struct is_sax_static_asserts
9750 {
9751 private:
9752 static_assert(is_basic_json<BasicJsonType>::value,
9753 "BasicJsonType must be of type basic_json<...>");
9754
9755 using number_integer_t = typename BasicJsonType::number_integer_t;
9756 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
9757 using number_float_t = typename BasicJsonType::number_float_t;
9758 using string_t = typename BasicJsonType::string_t;
9759 using binary_t = typename BasicJsonType::binary_t;
9760 using exception_t = typename BasicJsonType::exception;
9761
9762 public:
9763 static_assert(is_detected_exact<bool, null_function_t, SAX>::value,
9764 "Missing/invalid function: bool null()");
9765 static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
9766 "Missing/invalid function: bool boolean(bool)");
9767 static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
9768 "Missing/invalid function: bool boolean(bool)");
9769 static_assert(
9770 is_detected_exact<bool, number_integer_function_t, SAX,
9771 number_integer_t>::value,
9772 "Missing/invalid function: bool number_integer(number_integer_t)");
9773 static_assert(
9774 is_detected_exact<bool, number_unsigned_function_t, SAX,
9775 number_unsigned_t>::value,
9776 "Missing/invalid function: bool number_unsigned(number_unsigned_t)");
9777 static_assert(is_detected_exact<bool, number_float_function_t, SAX,
9778 number_float_t, string_t>::value,
9779 "Missing/invalid function: bool number_float(number_float_t, const string_t&)");
9780 static_assert(
9781 is_detected_exact<bool, string_function_t, SAX, string_t>::value,
9782 "Missing/invalid function: bool string(string_t&)");
9783 static_assert(
9784 is_detected_exact<bool, binary_function_t, SAX, binary_t>::value,
9785 "Missing/invalid function: bool binary(binary_t&)");
9786 static_assert(is_detected_exact<bool, start_object_function_t, SAX>::value,
9787 "Missing/invalid function: bool start_object(std::size_t)");
9788 static_assert(is_detected_exact<bool, key_function_t, SAX, string_t>::value,
9789 "Missing/invalid function: bool key(string_t&)");
9790 static_assert(is_detected_exact<bool, end_object_function_t, SAX>::value,
9791 "Missing/invalid function: bool end_object()");
9792 static_assert(is_detected_exact<bool, start_array_function_t, SAX>::value,
9793 "Missing/invalid function: bool start_array(std::size_t)");
9794 static_assert(is_detected_exact<bool, end_array_function_t, SAX>::value,
9795 "Missing/invalid function: bool end_array()");
9796 static_assert(
9797 is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value,
9798 "Missing/invalid function: bool parse_error(std::size_t, const "
9799 "std::string&, const exception&)");
9800 };
9801
9802 } // namespace detail
9803 NLOHMANN_JSON_NAMESPACE_END
9804
9805 // #include <nlohmann/detail/meta/type_traits.hpp>
9806
9807 // #include <nlohmann/detail/string_concat.hpp>
9808
9809 // #include <nlohmann/detail/value_t.hpp>
9810
9811
9812 NLOHMANN_JSON_NAMESPACE_BEGIN
9813 namespace detail
9814 {
9815
9816 /// how to treat CBOR tags
9817 enum class cbor_tag_handler_t
9818 {
9819 error, ///< throw a parse_error exception in case of a tag
9820 ignore, ///< ignore tags
9821 store ///< store tags as binary type
9822 };
9823
9824 /*!
9825 @brief determine system byte order
9826
9827 @return true if and only if system's byte order is little endian
9828
9829 @note from https://stackoverflow.com/a/1001328/266378
9830 */
9831 static inline bool little_endianness(int num = 1) noexcept
9832 {
9833 return *reinterpret_cast<char*>(&num) == 1;
9834 }
9835
9836 ///////////////////
9837 // binary reader //
9838 ///////////////////
9839
9840 /*!
9841 @brief deserialization of CBOR, MessagePack, and UBJSON values
9842 */
9843 template<typename BasicJsonType, typename InputAdapterType, typename SAX = json_sax_dom_parser<BasicJsonType, InputAdapterType>>
9844 class binary_reader
9845 {
9846 using number_integer_t = typename BasicJsonType::number_integer_t;
9847 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
9848 using number_float_t = typename BasicJsonType::number_float_t;
9849 using string_t = typename BasicJsonType::string_t;
9850 using binary_t = typename BasicJsonType::binary_t;
9851 using json_sax_t = SAX;
9852 using char_type = typename InputAdapterType::char_type;
9853 using char_int_type = typename char_traits<char_type>::int_type;
9854
9855 public:
9856 /*!
9857 @brief create a binary reader
9858
9859 @param[in] adapter input adapter to read from
9860 */
9861 explicit binary_reader(InputAdapterType&& adapter, const input_format_t format = input_format_t::json) noexcept : ia(std::move(adapter)), input_format(format)
9862 {
9863 (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
9864 }
9865
9866 // make class move-only
9867 binary_reader(const binary_reader&) = delete;
9868 binary_reader(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
9869 binary_reader& operator=(const binary_reader&) = delete;
9870 binary_reader& operator=(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
9871 ~binary_reader() = default;
9872
9873 /*!
9874 @param[in] format the binary format to parse
9875 @param[in] sax_ a SAX event processor
9876 @param[in] strict whether to expect the input to be consumed completed
9877 @param[in] tag_handler how to treat CBOR tags
9878
9879 @return whether parsing was successful
9880 */
9881 JSON_HEDLEY_NON_NULL(3)
9882 bool sax_parse(const input_format_t format,
9883 json_sax_t* sax_,
9884 const bool strict = true,
9885 const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
9886 {
9887 sax = sax_;
9888 bool result = false;
9889
9890 switch (format)
9891 {
9892 case input_format_t::bson:
9893 result = parse_bson_internal();
9894 break;
9895
9896 case input_format_t::cbor:
9897 result = parse_cbor_internal(true, tag_handler);
9898 break;
9899
9900 case input_format_t::msgpack:
9901 result = parse_msgpack_internal();
9902 break;
9903
9904 case input_format_t::ubjson:
9905 case input_format_t::bjdata:
9906 result = parse_ubjson_internal();
9907 break;
9908
9909 case input_format_t::json: // LCOV_EXCL_LINE
9910 default: // LCOV_EXCL_LINE
9911 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
9912 }
9913
9914 // strict mode: next byte must be EOF
9915 if (result && strict)
9916 {
9917 if (input_format == input_format_t::ubjson || input_format == input_format_t::bjdata)
9918 {
9919 get_ignore_noop();
9920 }
9921 else
9922 {
9923 get();
9924 }
9925
9926 if (JSON_HEDLEY_UNLIKELY(current != char_traits<char_type>::eof()))
9927 {
9928 return sax->parse_error(chars_read, get_token_string(), parse_error::create(110, chars_read,
9929 exception_message(input_format, concat("expected end of input; last byte: 0x", get_token_string()), "value"), nullptr));
9930 }
9931 }
9932
9933 return result;
9934 }
9935
9936 private:
9937 //////////
9938 // BSON //
9939 //////////
9940
9941 /*!
9942 @brief Reads in a BSON-object and passes it to the SAX-parser.
9943 @return whether a valid BSON-value was passed to the SAX parser
9944 */
9945 bool parse_bson_internal()
9946 {
9947 std::int32_t document_size{};
9948 get_number<std::int32_t, true>(input_format_t::bson, document_size);
9949
9950 if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
9951 {
9952 return false;
9953 }
9954
9955 if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/false)))
9956 {
9957 return false;
9958 }
9959
9960 return sax->end_object();
9961 }
9962
9963 /*!
9964 @brief Parses a C-style string from the BSON input.
9965 @param[in,out] result A reference to the string variable where the read
9966 string is to be stored.
9967 @return `true` if the \x00-byte indicating the end of the string was
9968 encountered before the EOF; false` indicates an unexpected EOF.
9969 */
9970 bool get_bson_cstr(string_t& result)
9971 {
9972 auto out = std::back_inserter(result);
9973 while (true)
9974 {
9975 get();
9976 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "cstring")))
9977 {
9978 return false;
9979 }
9980 if (current == 0x00)
9981 {
9982 return true;
9983 }
9984 *out++ = static_cast<typename string_t::value_type>(current);
9985 }
9986 }
9987
9988 /*!
9989 @brief Parses a zero-terminated string of length @a len from the BSON
9990 input.
9991 @param[in] len The length (including the zero-byte at the end) of the
9992 string to be read.
9993 @param[in,out] result A reference to the string variable where the read
9994 string is to be stored.
9995 @tparam NumberType The type of the length @a len
9996 @pre len >= 1
9997 @return `true` if the string was successfully parsed
9998 */
9999 template<typename NumberType>
10000 bool get_bson_string(const NumberType len, string_t& result)
10001 {
10002 if (JSON_HEDLEY_UNLIKELY(len < 1))
10003 {
10004 auto last_token = get_token_string();
10005 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
10006 exception_message(input_format_t::bson, concat("string length must be at least 1, is ", std::to_string(len)), "string"), nullptr));
10007 }
10008
10009 return get_string(input_format_t::bson, len - static_cast<NumberType>(1), result) && get() != char_traits<char_type>::eof();
10010 }
10011
10012 /*!
10013 @brief Parses a byte array input of length @a len from the BSON input.
10014 @param[in] len The length of the byte array to be read.
10015 @param[in,out] result A reference to the binary variable where the read
10016 array is to be stored.
10017 @tparam NumberType The type of the length @a len
10018 @pre len >= 0
10019 @return `true` if the byte array was successfully parsed
10020 */
10021 template<typename NumberType>
10022 bool get_bson_binary(const NumberType len, binary_t& result)
10023 {
10024 if (JSON_HEDLEY_UNLIKELY(len < 0))
10025 {
10026 auto last_token = get_token_string();
10027 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
10028 exception_message(input_format_t::bson, concat("byte array length cannot be negative, is ", std::to_string(len)), "binary"), nullptr));
10029 }
10030
10031 // All BSON binary values have a subtype
10032 std::uint8_t subtype{};
10033 get_number<std::uint8_t>(input_format_t::bson, subtype);
10034 result.set_subtype(subtype);
10035
10036 return get_binary(input_format_t::bson, len, result);
10037 }
10038
10039 /*!
10040 @brief Read a BSON document element of the given @a element_type.
10041 @param[in] element_type The BSON element type, c.f. http://bsonspec.org/spec.html
10042 @param[in] element_type_parse_position The position in the input stream,
10043 where the `element_type` was read.
10044 @warning Not all BSON element types are supported yet. An unsupported
10045 @a element_type will give rise to a parse_error.114:
10046 Unsupported BSON record type 0x...
10047 @return whether a valid BSON-object/array was passed to the SAX parser
10048 */
10049 bool parse_bson_element_internal(const char_int_type element_type,
10050 const std::size_t element_type_parse_position)
10051 {
10052 switch (element_type)
10053 {
10054 case 0x01: // double
10055 {
10056 double number{};
10057 return get_number<double, true>(input_format_t::bson, number) && sax->number_float(static_cast<number_float_t>(number), "");
10058 }
10059
10060 case 0x02: // string
10061 {
10062 std::int32_t len{};
10063 string_t value;
10064 return get_number<std::int32_t, true>(input_format_t::bson, len) && get_bson_string(len, value) && sax->string(value);
10065 }
10066
10067 case 0x03: // object
10068 {
10069 return parse_bson_internal();
10070 }
10071
10072 case 0x04: // array
10073 {
10074 return parse_bson_array();
10075 }
10076
10077 case 0x05: // binary
10078 {
10079 std::int32_t len{};
10080 binary_t value;
10081 return get_number<std::int32_t, true>(input_format_t::bson, len) && get_bson_binary(len, value) && sax->binary(value);
10082 }
10083
10084 case 0x08: // boolean
10085 {
10086 return sax->boolean(get() != 0);
10087 }
10088
10089 case 0x0A: // null
10090 {
10091 return sax->null();
10092 }
10093
10094 case 0x10: // int32
10095 {
10096 std::int32_t value{};
10097 return get_number<std::int32_t, true>(input_format_t::bson, value) && sax->number_integer(value);
10098 }
10099
10100 case 0x12: // int64
10101 {
10102 std::int64_t value{};
10103 return get_number<std::int64_t, true>(input_format_t::bson, value) && sax->number_integer(value);
10104 }
10105
10106 case 0x11: // uint64
10107 {
10108 std::uint64_t value{};
10109 return get_number<std::uint64_t, true>(input_format_t::bson, value) && sax->number_unsigned(value);
10110 }
10111
10112 default: // anything else not supported (yet)
10113 {
10114 std::array<char, 3> cr{{}};
10115 static_cast<void>((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(element_type))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
10116 const std::string cr_str{cr.data()};
10117 return sax->parse_error(element_type_parse_position, cr_str,
10118 parse_error::create(114, element_type_parse_position, concat("Unsupported BSON record type 0x", cr_str), nullptr));
10119 }
10120 }
10121 }
10122
10123 /*!
10124 @brief Read a BSON element list (as specified in the BSON-spec)
10125
10126 The same binary layout is used for objects and arrays, hence it must be
10127 indicated with the argument @a is_array which one is expected
10128 (true --> array, false --> object).
10129
10130 @param[in] is_array Determines if the element list being read is to be
10131 treated as an object (@a is_array == false), or as an
10132 array (@a is_array == true).
10133 @return whether a valid BSON-object/array was passed to the SAX parser
10134 */
10135 bool parse_bson_element_list(const bool is_array)
10136 {
10137 string_t key;
10138
10139 while (auto element_type = get())
10140 {
10141 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "element list")))
10142 {
10143 return false;
10144 }
10145
10146 const std::size_t element_type_parse_position = chars_read;
10147 if (JSON_HEDLEY_UNLIKELY(!get_bson_cstr(key)))
10148 {
10149 return false;
10150 }
10151
10152 if (!is_array && !sax->key(key))
10153 {
10154 return false;
10155 }
10156
10157 if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_internal(element_type, element_type_parse_position)))
10158 {
10159 return false;
10160 }
10161
10162 // get_bson_cstr only appends
10163 key.clear();
10164 }
10165
10166 return true;
10167 }
10168
10169 /*!
10170 @brief Reads an array from the BSON input and passes it to the SAX-parser.
10171 @return whether a valid BSON-array was passed to the SAX parser
10172 */
10173 bool parse_bson_array()
10174 {
10175 std::int32_t document_size{};
10176 get_number<std::int32_t, true>(input_format_t::bson, document_size);
10177
10178 if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
10179 {
10180 return false;
10181 }
10182
10183 if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/true)))
10184 {
10185 return false;
10186 }
10187
10188 return sax->end_array();
10189 }
10190
10191 //////////
10192 // CBOR //
10193 //////////
10194
10195 /*!
10196 @param[in] get_char whether a new character should be retrieved from the
10197 input (true) or whether the last read character should
10198 be considered instead (false)
10199 @param[in] tag_handler how CBOR tags should be treated
10200
10201 @return whether a valid CBOR value was passed to the SAX parser
10202 */
10203 bool parse_cbor_internal(const bool get_char,
10204 const cbor_tag_handler_t tag_handler)
10205 {
10206 switch (get_char ? get() : current)
10207 {
10208 // EOF
10209 case char_traits<char_type>::eof():
10210 return unexpect_eof(input_format_t::cbor, "value");
10211
10212 // Integer 0x00..0x17 (0..23)
10213 case 0x00:
10214 case 0x01:
10215 case 0x02:
10216 case 0x03:
10217 case 0x04:
10218 case 0x05:
10219 case 0x06:
10220 case 0x07:
10221 case 0x08:
10222 case 0x09:
10223 case 0x0A:
10224 case 0x0B:
10225 case 0x0C:
10226 case 0x0D:
10227 case 0x0E:
10228 case 0x0F:
10229 case 0x10:
10230 case 0x11:
10231 case 0x12:
10232 case 0x13:
10233 case 0x14:
10234 case 0x15:
10235 case 0x16:
10236 case 0x17:
10237 return sax->number_unsigned(static_cast<number_unsigned_t>(current));
10238
10239 case 0x18: // Unsigned integer (one-byte uint8_t follows)
10240 {
10241 std::uint8_t number{};
10242 return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
10243 }
10244
10245 case 0x19: // Unsigned integer (two-byte uint16_t follows)
10246 {
10247 std::uint16_t number{};
10248 return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
10249 }
10250
10251 case 0x1A: // Unsigned integer (four-byte uint32_t follows)
10252 {
10253 std::uint32_t number{};
10254 return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
10255 }
10256
10257 case 0x1B: // Unsigned integer (eight-byte uint64_t follows)
10258 {
10259 std::uint64_t number{};
10260 return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
10261 }
10262
10263 // Negative integer -1-0x00..-1-0x17 (-1..-24)
10264 case 0x20:
10265 case 0x21:
10266 case 0x22:
10267 case 0x23:
10268 case 0x24:
10269 case 0x25:
10270 case 0x26:
10271 case 0x27:
10272 case 0x28:
10273 case 0x29:
10274 case 0x2A:
10275 case 0x2B:
10276 case 0x2C:
10277 case 0x2D:
10278 case 0x2E:
10279 case 0x2F:
10280 case 0x30:
10281 case 0x31:
10282 case 0x32:
10283 case 0x33:
10284 case 0x34:
10285 case 0x35:
10286 case 0x36:
10287 case 0x37:
10288 return sax->number_integer(static_cast<std::int8_t>(0x20 - 1 - current));
10289
10290 case 0x38: // Negative integer (one-byte uint8_t follows)
10291 {
10292 std::uint8_t number{};
10293 return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
10294 }
10295
10296 case 0x39: // Negative integer -1-n (two-byte uint16_t follows)
10297 {
10298 std::uint16_t number{};
10299 return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
10300 }
10301
10302 case 0x3A: // Negative integer -1-n (four-byte uint32_t follows)
10303 {
10304 std::uint32_t number{};
10305 return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
10306 }
10307
10308 case 0x3B: // Negative integer -1-n (eight-byte uint64_t follows)
10309 {
10310 std::uint64_t number{};
10311 return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1)
10312 - static_cast<number_integer_t>(number));
10313 }
10314
10315 // Binary data (0x00..0x17 bytes follow)
10316 case 0x40:
10317 case 0x41:
10318 case 0x42:
10319 case 0x43:
10320 case 0x44:
10321 case 0x45:
10322 case 0x46:
10323 case 0x47:
10324 case 0x48:
10325 case 0x49:
10326 case 0x4A:
10327 case 0x4B:
10328 case 0x4C:
10329 case 0x4D:
10330 case 0x4E:
10331 case 0x4F:
10332 case 0x50:
10333 case 0x51:
10334 case 0x52:
10335 case 0x53:
10336 case 0x54:
10337 case 0x55:
10338 case 0x56:
10339 case 0x57:
10340 case 0x58: // Binary data (one-byte uint8_t for n follows)
10341 case 0x59: // Binary data (two-byte uint16_t for n follow)
10342 case 0x5A: // Binary data (four-byte uint32_t for n follow)
10343 case 0x5B: // Binary data (eight-byte uint64_t for n follow)
10344 case 0x5F: // Binary data (indefinite length)
10345 {
10346 binary_t b;
10347 return get_cbor_binary(b) && sax->binary(b);
10348 }
10349
10350 // UTF-8 string (0x00..0x17 bytes follow)
10351 case 0x60:
10352 case 0x61:
10353 case 0x62:
10354 case 0x63:
10355 case 0x64:
10356 case 0x65:
10357 case 0x66:
10358 case 0x67:
10359 case 0x68:
10360 case 0x69:
10361 case 0x6A:
10362 case 0x6B:
10363 case 0x6C:
10364 case 0x6D:
10365 case 0x6E:
10366 case 0x6F:
10367 case 0x70:
10368 case 0x71:
10369 case 0x72:
10370 case 0x73:
10371 case 0x74:
10372 case 0x75:
10373 case 0x76:
10374 case 0x77:
10375 case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
10376 case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
10377 case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
10378 case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
10379 case 0x7F: // UTF-8 string (indefinite length)
10380 {
10381 string_t s;
10382 return get_cbor_string(s) && sax->string(s);
10383 }
10384
10385 // array (0x00..0x17 data items follow)
10386 case 0x80:
10387 case 0x81:
10388 case 0x82:
10389 case 0x83:
10390 case 0x84:
10391 case 0x85:
10392 case 0x86:
10393 case 0x87:
10394 case 0x88:
10395 case 0x89:
10396 case 0x8A:
10397 case 0x8B:
10398 case 0x8C:
10399 case 0x8D:
10400 case 0x8E:
10401 case 0x8F:
10402 case 0x90:
10403 case 0x91:
10404 case 0x92:
10405 case 0x93:
10406 case 0x94:
10407 case 0x95:
10408 case 0x96:
10409 case 0x97:
10410 return get_cbor_array(
10411 conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
10412
10413 case 0x98: // array (one-byte uint8_t for n follows)
10414 {
10415 std::uint8_t len{};
10416 return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
10417 }
10418
10419 case 0x99: // array (two-byte uint16_t for n follow)
10420 {
10421 std::uint16_t len{};
10422 return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
10423 }
10424
10425 case 0x9A: // array (four-byte uint32_t for n follow)
10426 {
10427 std::uint32_t len{};
10428 return get_number(input_format_t::cbor, len) && get_cbor_array(conditional_static_cast<std::size_t>(len), tag_handler);
10429 }
10430
10431 case 0x9B: // array (eight-byte uint64_t for n follow)
10432 {
10433 std::uint64_t len{};
10434 return get_number(input_format_t::cbor, len) && get_cbor_array(conditional_static_cast<std::size_t>(len), tag_handler);
10435 }
10436
10437 case 0x9F: // array (indefinite length)
10438 return get_cbor_array(detail::unknown_size(), tag_handler);
10439
10440 // map (0x00..0x17 pairs of data items follow)
10441 case 0xA0:
10442 case 0xA1:
10443 case 0xA2:
10444 case 0xA3:
10445 case 0xA4:
10446 case 0xA5:
10447 case 0xA6:
10448 case 0xA7:
10449 case 0xA8:
10450 case 0xA9:
10451 case 0xAA:
10452 case 0xAB:
10453 case 0xAC:
10454 case 0xAD:
10455 case 0xAE:
10456 case 0xAF:
10457 case 0xB0:
10458 case 0xB1:
10459 case 0xB2:
10460 case 0xB3:
10461 case 0xB4:
10462 case 0xB5:
10463 case 0xB6:
10464 case 0xB7:
10465 return get_cbor_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
10466
10467 case 0xB8: // map (one-byte uint8_t for n follows)
10468 {
10469 std::uint8_t len{};
10470 return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
10471 }
10472
10473 case 0xB9: // map (two-byte uint16_t for n follow)
10474 {
10475 std::uint16_t len{};
10476 return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
10477 }
10478
10479 case 0xBA: // map (four-byte uint32_t for n follow)
10480 {
10481 std::uint32_t len{};
10482 return get_number(input_format_t::cbor, len) && get_cbor_object(conditional_static_cast<std::size_t>(len), tag_handler);
10483 }
10484
10485 case 0xBB: // map (eight-byte uint64_t for n follow)
10486 {
10487 std::uint64_t len{};
10488 return get_number(input_format_t::cbor, len) && get_cbor_object(conditional_static_cast<std::size_t>(len), tag_handler);
10489 }
10490
10491 case 0xBF: // map (indefinite length)
10492 return get_cbor_object(detail::unknown_size(), tag_handler);
10493
10494 case 0xC6: // tagged item
10495 case 0xC7:
10496 case 0xC8:
10497 case 0xC9:
10498 case 0xCA:
10499 case 0xCB:
10500 case 0xCC:
10501 case 0xCD:
10502 case 0xCE:
10503 case 0xCF:
10504 case 0xD0:
10505 case 0xD1:
10506 case 0xD2:
10507 case 0xD3:
10508 case 0xD4:
10509 case 0xD8: // tagged item (1 bytes follow)
10510 case 0xD9: // tagged item (2 bytes follow)
10511 case 0xDA: // tagged item (4 bytes follow)
10512 case 0xDB: // tagged item (8 bytes follow)
10513 {
10514 switch (tag_handler)
10515 {
10516 case cbor_tag_handler_t::error:
10517 {
10518 auto last_token = get_token_string();
10519 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
10520 exception_message(input_format_t::cbor, concat("invalid byte: 0x", last_token), "value"), nullptr));
10521 }
10522
10523 case cbor_tag_handler_t::ignore:
10524 {
10525 // ignore binary subtype
10526 switch (current)
10527 {
10528 case 0xD8:
10529 {
10530 std::uint8_t subtype_to_ignore{};
10531 get_number(input_format_t::cbor, subtype_to_ignore);
10532 break;
10533 }
10534 case 0xD9:
10535 {
10536 std::uint16_t subtype_to_ignore{};
10537 get_number(input_format_t::cbor, subtype_to_ignore);
10538 break;
10539 }
10540 case 0xDA:
10541 {
10542 std::uint32_t subtype_to_ignore{};
10543 get_number(input_format_t::cbor, subtype_to_ignore);
10544 break;
10545 }
10546 case 0xDB:
10547 {
10548 std::uint64_t subtype_to_ignore{};
10549 get_number(input_format_t::cbor, subtype_to_ignore);
10550 break;
10551 }
10552 default:
10553 break;
10554 }
10555 return parse_cbor_internal(true, tag_handler);
10556 }
10557
10558 case cbor_tag_handler_t::store:
10559 {
10560 binary_t b;
10561 // use binary subtype and store in binary container
10562 switch (current)
10563 {
10564 case 0xD8:
10565 {
10566 std::uint8_t subtype{};
10567 get_number(input_format_t::cbor, subtype);
10568 b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
10569 break;
10570 }
10571 case 0xD9:
10572 {
10573 std::uint16_t subtype{};
10574 get_number(input_format_t::cbor, subtype);
10575 b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
10576 break;
10577 }
10578 case 0xDA:
10579 {
10580 std::uint32_t subtype{};
10581 get_number(input_format_t::cbor, subtype);
10582 b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
10583 break;
10584 }
10585 case 0xDB:
10586 {
10587 std::uint64_t subtype{};
10588 get_number(input_format_t::cbor, subtype);
10589 b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
10590 break;
10591 }
10592 default:
10593 return parse_cbor_internal(true, tag_handler);
10594 }
10595 get();
10596 return get_cbor_binary(b) && sax->binary(b);
10597 }
10598
10599 default: // LCOV_EXCL_LINE
10600 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
10601 return false; // LCOV_EXCL_LINE
10602 }
10603 }
10604
10605 case 0xF4: // false
10606 return sax->boolean(false);
10607
10608 case 0xF5: // true
10609 return sax->boolean(true);
10610
10611 case 0xF6: // null
10612 return sax->null();
10613
10614 case 0xF9: // Half-Precision Float (two-byte IEEE 754)
10615 {
10616 const auto byte1_raw = get();
10617 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
10618 {
10619 return false;
10620 }
10621 const auto byte2_raw = get();
10622 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
10623 {
10624 return false;
10625 }
10626
10627 const auto byte1 = static_cast<unsigned char>(byte1_raw);
10628 const auto byte2 = static_cast<unsigned char>(byte2_raw);
10629
10630 // code from RFC 7049, Appendix D, Figure 3:
10631 // As half-precision floating-point numbers were only added
10632 // to IEEE 754 in 2008, today's programming platforms often
10633 // still only have limited support for them. It is very
10634 // easy to include at least decoding support for them even
10635 // without such support. An example of a small decoder for
10636 // half-precision floating-point numbers in the C language
10637 // is shown in Fig. 3.
10638 const auto half = static_cast<unsigned int>((byte1 << 8u) + byte2);
10639 const double val = [&half]
10640 {
10641 const int exp = (half >> 10u) & 0x1Fu;
10642 const unsigned int mant = half & 0x3FFu;
10643 JSON_ASSERT(0 <= exp&& exp <= 32);
10644 JSON_ASSERT(mant <= 1024);
10645 switch (exp)
10646 {
10647 case 0:
10648 return std::ldexp(mant, -24);
10649 case 31:
10650 return (mant == 0)
10651 ? std::numeric_limits<double>::infinity()
10652 : std::numeric_limits<double>::quiet_NaN();
10653 default:
10654 return std::ldexp(mant + 1024, exp - 25);
10655 }
10656 }();
10657 return sax->number_float((half & 0x8000u) != 0
10658 ? static_cast<number_float_t>(-val)
10659 : static_cast<number_float_t>(val), "");
10660 }
10661
10662 case 0xFA: // Single-Precision Float (four-byte IEEE 754)
10663 {
10664 float number{};
10665 return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
10666 }
10667
10668 case 0xFB: // Double-Precision Float (eight-byte IEEE 754)
10669 {
10670 double number{};
10671 return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
10672 }
10673
10674 default: // anything else (0xFF is handled inside the other types)
10675 {
10676 auto last_token = get_token_string();
10677 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
10678 exception_message(input_format_t::cbor, concat("invalid byte: 0x", last_token), "value"), nullptr));
10679 }
10680 }
10681 }
10682
10683 /*!
10684 @brief reads a CBOR string
10685
10686 This function first reads starting bytes to determine the expected
10687 string length and then copies this number of bytes into a string.
10688 Additionally, CBOR's strings with indefinite lengths are supported.
10689
10690 @param[out] result created string
10691
10692 @return whether string creation completed
10693 */
10694 bool get_cbor_string(string_t& result)
10695 {
10696 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "string")))
10697 {
10698 return false;
10699 }
10700
10701 switch (current)
10702 {
10703 // UTF-8 string (0x00..0x17 bytes follow)
10704 case 0x60:
10705 case 0x61:
10706 case 0x62:
10707 case 0x63:
10708 case 0x64:
10709 case 0x65:
10710 case 0x66:
10711 case 0x67:
10712 case 0x68:
10713 case 0x69:
10714 case 0x6A:
10715 case 0x6B:
10716 case 0x6C:
10717 case 0x6D:
10718 case 0x6E:
10719 case 0x6F:
10720 case 0x70:
10721 case 0x71:
10722 case 0x72:
10723 case 0x73:
10724 case 0x74:
10725 case 0x75:
10726 case 0x76:
10727 case 0x77:
10728 {
10729 return get_string(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
10730 }
10731
10732 case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
10733 {
10734 std::uint8_t len{};
10735 return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
10736 }
10737
10738 case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
10739 {
10740 std::uint16_t len{};
10741 return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
10742 }
10743
10744 case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
10745 {
10746 std::uint32_t len{};
10747 return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
10748 }
10749
10750 case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
10751 {
10752 std::uint64_t len{};
10753 return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
10754 }
10755
10756 case 0x7F: // UTF-8 string (indefinite length)
10757 {
10758 while (get() != 0xFF)
10759 {
10760 string_t chunk;
10761 if (!get_cbor_string(chunk))
10762 {
10763 return false;
10764 }
10765 result.append(chunk);
10766 }
10767 return true;
10768 }
10769
10770 default:
10771 {
10772 auto last_token = get_token_string();
10773 return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
10774 exception_message(input_format_t::cbor, concat("expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x", last_token), "string"), nullptr));
10775 }
10776 }
10777 }
10778
10779 /*!
10780 @brief reads a CBOR byte array
10781
10782 This function first reads starting bytes to determine the expected
10783 byte array length and then copies this number of bytes into the byte array.
10784 Additionally, CBOR's byte arrays with indefinite lengths are supported.
10785
10786 @param[out] result created byte array
10787
10788 @return whether byte array creation completed
10789 */
10790 bool get_cbor_binary(binary_t& result)
10791 {
10792 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "binary")))
10793 {
10794 return false;
10795 }
10796
10797 switch (current)
10798 {
10799 // Binary data (0x00..0x17 bytes follow)
10800 case 0x40:
10801 case 0x41:
10802 case 0x42:
10803 case 0x43:
10804 case 0x44:
10805 case 0x45:
10806 case 0x46:
10807 case 0x47:
10808 case 0x48:
10809 case 0x49:
10810 case 0x4A:
10811 case 0x4B:
10812 case 0x4C:
10813 case 0x4D:
10814 case 0x4E:
10815 case 0x4F:
10816 case 0x50:
10817 case 0x51:
10818 case 0x52:
10819 case 0x53:
10820 case 0x54:
10821 case 0x55:
10822 case 0x56:
10823 case 0x57:
10824 {
10825 return get_binary(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
10826 }
10827
10828 case 0x58: // Binary data (one-byte uint8_t for n follows)
10829 {
10830 std::uint8_t len{};
10831 return get_number(input_format_t::cbor, len) &&
10832 get_binary(input_format_t::cbor, len, result);
10833 }
10834
10835 case 0x59: // Binary data (two-byte uint16_t for n follow)
10836 {
10837 std::uint16_t len{};
10838 return get_number(input_format_t::cbor, len) &&
10839 get_binary(input_format_t::cbor, len, result);
10840 }
10841
10842 case 0x5A: // Binary data (four-byte uint32_t for n follow)
10843 {
10844 std::uint32_t len{};
10845 return get_number(input_format_t::cbor, len) &&
10846 get_binary(input_format_t::cbor, len, result);
10847 }
10848
10849 case 0x5B: // Binary data (eight-byte uint64_t for n follow)
10850 {
10851 std::uint64_t len{};
10852 return get_number(input_format_t::cbor, len) &&
10853 get_binary(input_format_t::cbor, len, result);
10854 }
10855
10856 case 0x5F: // Binary data (indefinite length)
10857 {
10858 while (get() != 0xFF)
10859 {
10860 binary_t chunk;
10861 if (!get_cbor_binary(chunk))
10862 {
10863 return false;
10864 }
10865 result.insert(result.end(), chunk.begin(), chunk.end());
10866 }
10867 return true;
10868 }
10869
10870 default:
10871 {
10872 auto last_token = get_token_string();
10873 return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
10874 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));
10875 }
10876 }
10877 }
10878
10879 /*!
10880 @param[in] len the length of the array or detail::unknown_size() for an
10881 array of indefinite size
10882 @param[in] tag_handler how CBOR tags should be treated
10883 @return whether array creation completed
10884 */
10885 bool get_cbor_array(const std::size_t len,
10886 const cbor_tag_handler_t tag_handler)
10887 {
10888 if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
10889 {
10890 return false;
10891 }
10892
10893 if (len != detail::unknown_size())
10894 {
10895 for (std::size_t i = 0; i < len; ++i)
10896 {
10897 if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
10898 {
10899 return false;
10900 }
10901 }
10902 }
10903 else
10904 {
10905 while (get() != 0xFF)
10906 {
10907 if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(false, tag_handler)))
10908 {
10909 return false;
10910 }
10911 }
10912 }
10913
10914 return sax->end_array();
10915 }
10916
10917 /*!
10918 @param[in] len the length of the object or detail::unknown_size() for an
10919 object of indefinite size
10920 @param[in] tag_handler how CBOR tags should be treated
10921 @return whether object creation completed
10922 */
10923 bool get_cbor_object(const std::size_t len,
10924 const cbor_tag_handler_t tag_handler)
10925 {
10926 if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
10927 {
10928 return false;
10929 }
10930
10931 if (len != 0)
10932 {
10933 string_t key;
10934 if (len != detail::unknown_size())
10935 {
10936 for (std::size_t i = 0; i < len; ++i)
10937 {
10938 get();
10939 if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
10940 {
10941 return false;
10942 }
10943
10944 if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
10945 {
10946 return false;
10947 }
10948 key.clear();
10949 }
10950 }
10951 else
10952 {
10953 while (get() != 0xFF)
10954 {
10955 if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
10956 {
10957 return false;
10958 }
10959
10960 if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
10961 {
10962 return false;
10963 }
10964 key.clear();
10965 }
10966 }
10967 }
10968
10969 return sax->end_object();
10970 }
10971
10972 /////////////
10973 // MsgPack //
10974 /////////////
10975
10976 /*!
10977 @return whether a valid MessagePack value was passed to the SAX parser
10978 */
10979 bool parse_msgpack_internal()
10980 {
10981 switch (get())
10982 {
10983 // EOF
10984 case char_traits<char_type>::eof():
10985 return unexpect_eof(input_format_t::msgpack, "value");
10986
10987 // positive fixint
10988 case 0x00:
10989 case 0x01:
10990 case 0x02:
10991 case 0x03:
10992 case 0x04:
10993 case 0x05:
10994 case 0x06:
10995 case 0x07:
10996 case 0x08:
10997 case 0x09:
10998 case 0x0A:
10999 case 0x0B:
11000 case 0x0C:
11001 case 0x0D:
11002 case 0x0E:
11003 case 0x0F:
11004 case 0x10:
11005 case 0x11:
11006 case 0x12:
11007 case 0x13:
11008 case 0x14:
11009 case 0x15:
11010 case 0x16:
11011 case 0x17:
11012 case 0x18:
11013 case 0x19:
11014 case 0x1A:
11015 case 0x1B:
11016 case 0x1C:
11017 case 0x1D:
11018 case 0x1E:
11019 case 0x1F:
11020 case 0x20:
11021 case 0x21:
11022 case 0x22:
11023 case 0x23:
11024 case 0x24:
11025 case 0x25:
11026 case 0x26:
11027 case 0x27:
11028 case 0x28:
11029 case 0x29:
11030 case 0x2A:
11031 case 0x2B:
11032 case 0x2C:
11033 case 0x2D:
11034 case 0x2E:
11035 case 0x2F:
11036 case 0x30:
11037 case 0x31:
11038 case 0x32:
11039 case 0x33:
11040 case 0x34:
11041 case 0x35:
11042 case 0x36:
11043 case 0x37:
11044 case 0x38:
11045 case 0x39:
11046 case 0x3A:
11047 case 0x3B:
11048 case 0x3C:
11049 case 0x3D:
11050 case 0x3E:
11051 case 0x3F:
11052 case 0x40:
11053 case 0x41:
11054 case 0x42:
11055 case 0x43:
11056 case 0x44:
11057 case 0x45:
11058 case 0x46:
11059 case 0x47:
11060 case 0x48:
11061 case 0x49:
11062 case 0x4A:
11063 case 0x4B:
11064 case 0x4C:
11065 case 0x4D:
11066 case 0x4E:
11067 case 0x4F:
11068 case 0x50:
11069 case 0x51:
11070 case 0x52:
11071 case 0x53:
11072 case 0x54:
11073 case 0x55:
11074 case 0x56:
11075 case 0x57:
11076 case 0x58:
11077 case 0x59:
11078 case 0x5A:
11079 case 0x5B:
11080 case 0x5C:
11081 case 0x5D:
11082 case 0x5E:
11083 case 0x5F:
11084 case 0x60:
11085 case 0x61:
11086 case 0x62:
11087 case 0x63:
11088 case 0x64:
11089 case 0x65:
11090 case 0x66:
11091 case 0x67:
11092 case 0x68:
11093 case 0x69:
11094 case 0x6A:
11095 case 0x6B:
11096 case 0x6C:
11097 case 0x6D:
11098 case 0x6E:
11099 case 0x6F:
11100 case 0x70:
11101 case 0x71:
11102 case 0x72:
11103 case 0x73:
11104 case 0x74:
11105 case 0x75:
11106 case 0x76:
11107 case 0x77:
11108 case 0x78:
11109 case 0x79:
11110 case 0x7A:
11111 case 0x7B:
11112 case 0x7C:
11113 case 0x7D:
11114 case 0x7E:
11115 case 0x7F:
11116 return sax->number_unsigned(static_cast<number_unsigned_t>(current));
11117
11118 // fixmap
11119 case 0x80:
11120 case 0x81:
11121 case 0x82:
11122 case 0x83:
11123 case 0x84:
11124 case 0x85:
11125 case 0x86:
11126 case 0x87:
11127 case 0x88:
11128 case 0x89:
11129 case 0x8A:
11130 case 0x8B:
11131 case 0x8C:
11132 case 0x8D:
11133 case 0x8E:
11134 case 0x8F:
11135 return get_msgpack_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
11136
11137 // fixarray
11138 case 0x90:
11139 case 0x91:
11140 case 0x92:
11141 case 0x93:
11142 case 0x94:
11143 case 0x95:
11144 case 0x96:
11145 case 0x97:
11146 case 0x98:
11147 case 0x99:
11148 case 0x9A:
11149 case 0x9B:
11150 case 0x9C:
11151 case 0x9D:
11152 case 0x9E:
11153 case 0x9F:
11154 return get_msgpack_array(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
11155
11156 // fixstr
11157 case 0xA0:
11158 case 0xA1:
11159 case 0xA2:
11160 case 0xA3:
11161 case 0xA4:
11162 case 0xA5:
11163 case 0xA6:
11164 case 0xA7:
11165 case 0xA8:
11166 case 0xA9:
11167 case 0xAA:
11168 case 0xAB:
11169 case 0xAC:
11170 case 0xAD:
11171 case 0xAE:
11172 case 0xAF:
11173 case 0xB0:
11174 case 0xB1:
11175 case 0xB2:
11176 case 0xB3:
11177 case 0xB4:
11178 case 0xB5:
11179 case 0xB6:
11180 case 0xB7:
11181 case 0xB8:
11182 case 0xB9:
11183 case 0xBA:
11184 case 0xBB:
11185 case 0xBC:
11186 case 0xBD:
11187 case 0xBE:
11188 case 0xBF:
11189 case 0xD9: // str 8
11190 case 0xDA: // str 16
11191 case 0xDB: // str 32
11192 {
11193 string_t s;
11194 return get_msgpack_string(s) && sax->string(s);
11195 }
11196
11197 case 0xC0: // nil
11198 return sax->null();
11199
11200 case 0xC2: // false
11201 return sax->boolean(false);
11202
11203 case 0xC3: // true
11204 return sax->boolean(true);
11205
11206 case 0xC4: // bin 8
11207 case 0xC5: // bin 16
11208 case 0xC6: // bin 32
11209 case 0xC7: // ext 8
11210 case 0xC8: // ext 16
11211 case 0xC9: // ext 32
11212 case 0xD4: // fixext 1
11213 case 0xD5: // fixext 2
11214 case 0xD6: // fixext 4
11215 case 0xD7: // fixext 8
11216 case 0xD8: // fixext 16
11217 {
11218 binary_t b;
11219 return get_msgpack_binary(b) && sax->binary(b);
11220 }
11221
11222 case 0xCA: // float 32
11223 {
11224 float number{};
11225 return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast<number_float_t>(number), "");
11226 }
11227
11228 case 0xCB: // float 64
11229 {
11230 double number{};
11231 return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast<number_float_t>(number), "");
11232 }
11233
11234 case 0xCC: // uint 8
11235 {
11236 std::uint8_t number{};
11237 return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
11238 }
11239
11240 case 0xCD: // uint 16
11241 {
11242 std::uint16_t number{};
11243 return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
11244 }
11245
11246 case 0xCE: // uint 32
11247 {
11248 std::uint32_t number{};
11249 return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
11250 }
11251
11252 case 0xCF: // uint 64
11253 {
11254 std::uint64_t number{};
11255 return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
11256 }
11257
11258 case 0xD0: // int 8
11259 {
11260 std::int8_t number{};
11261 return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
11262 }
11263
11264 case 0xD1: // int 16
11265 {
11266 std::int16_t number{};
11267 return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
11268 }
11269
11270 case 0xD2: // int 32
11271 {
11272 std::int32_t number{};
11273 return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
11274 }
11275
11276 case 0xD3: // int 64
11277 {
11278 std::int64_t number{};
11279 return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
11280 }
11281
11282 case 0xDC: // array 16
11283 {
11284 std::uint16_t len{};
11285 return get_number(input_format_t::msgpack, len) && get_msgpack_array(static_cast<std::size_t>(len));
11286 }
11287
11288 case 0xDD: // array 32
11289 {
11290 std::uint32_t len{};
11291 return get_number(input_format_t::msgpack, len) && get_msgpack_array(conditional_static_cast<std::size_t>(len));
11292 }
11293
11294 case 0xDE: // map 16
11295 {
11296 std::uint16_t len{};
11297 return get_number(input_format_t::msgpack, len) && get_msgpack_object(static_cast<std::size_t>(len));
11298 }
11299
11300 case 0xDF: // map 32
11301 {
11302 std::uint32_t len{};
11303 return get_number(input_format_t::msgpack, len) && get_msgpack_object(conditional_static_cast<std::size_t>(len));
11304 }
11305
11306 // negative fixint
11307 case 0xE0:
11308 case 0xE1:
11309 case 0xE2:
11310 case 0xE3:
11311 case 0xE4:
11312 case 0xE5:
11313 case 0xE6:
11314 case 0xE7:
11315 case 0xE8:
11316 case 0xE9:
11317 case 0xEA:
11318 case 0xEB:
11319 case 0xEC:
11320 case 0xED:
11321 case 0xEE:
11322 case 0xEF:
11323 case 0xF0:
11324 case 0xF1:
11325 case 0xF2:
11326 case 0xF3:
11327 case 0xF4:
11328 case 0xF5:
11329 case 0xF6:
11330 case 0xF7:
11331 case 0xF8:
11332 case 0xF9:
11333 case 0xFA:
11334 case 0xFB:
11335 case 0xFC:
11336 case 0xFD:
11337 case 0xFE:
11338 case 0xFF:
11339 return sax->number_integer(static_cast<std::int8_t>(current));
11340
11341 default: // anything else
11342 {
11343 auto last_token = get_token_string();
11344 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
11345 exception_message(input_format_t::msgpack, concat("invalid byte: 0x", last_token), "value"), nullptr));
11346 }
11347 }
11348 }
11349
11350 /*!
11351 @brief reads a MessagePack string
11352
11353 This function first reads starting bytes to determine the expected
11354 string length and then copies this number of bytes into a string.
11355
11356 @param[out] result created string
11357
11358 @return whether string creation completed
11359 */
11360 bool get_msgpack_string(string_t& result)
11361 {
11362 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::msgpack, "string")))
11363 {
11364 return false;
11365 }
11366
11367 switch (current)
11368 {
11369 // fixstr
11370 case 0xA0:
11371 case 0xA1:
11372 case 0xA2:
11373 case 0xA3:
11374 case 0xA4:
11375 case 0xA5:
11376 case 0xA6:
11377 case 0xA7:
11378 case 0xA8:
11379 case 0xA9:
11380 case 0xAA:
11381 case 0xAB:
11382 case 0xAC:
11383 case 0xAD:
11384 case 0xAE:
11385 case 0xAF:
11386 case 0xB0:
11387 case 0xB1:
11388 case 0xB2:
11389 case 0xB3:
11390 case 0xB4:
11391 case 0xB5:
11392 case 0xB6:
11393 case 0xB7:
11394 case 0xB8:
11395 case 0xB9:
11396 case 0xBA:
11397 case 0xBB:
11398 case 0xBC:
11399 case 0xBD:
11400 case 0xBE:
11401 case 0xBF:
11402 {
11403 return get_string(input_format_t::msgpack, static_cast<unsigned int>(current) & 0x1Fu, result);
11404 }
11405
11406 case 0xD9: // str 8
11407 {
11408 std::uint8_t len{};
11409 return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
11410 }
11411
11412 case 0xDA: // str 16
11413 {
11414 std::uint16_t len{};
11415 return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
11416 }
11417
11418 case 0xDB: // str 32
11419 {
11420 std::uint32_t len{};
11421 return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
11422 }
11423
11424 default:
11425 {
11426 auto last_token = get_token_string();
11427 return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
11428 exception_message(input_format_t::msgpack, concat("expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x", last_token), "string"), nullptr));
11429 }
11430 }
11431 }
11432
11433 /*!
11434 @brief reads a MessagePack byte array
11435
11436 This function first reads starting bytes to determine the expected
11437 byte array length and then copies this number of bytes into a byte array.
11438
11439 @param[out] result created byte array
11440
11441 @return whether byte array creation completed
11442 */
11443 bool get_msgpack_binary(binary_t& result)
11444 {
11445 // helper function to set the subtype
11446 auto assign_and_return_true = [&result](std::int8_t subtype)
11447 {
11448 result.set_subtype(static_cast<std::uint8_t>(subtype));
11449 return true;
11450 };
11451
11452 switch (current)
11453 {
11454 case 0xC4: // bin 8
11455 {
11456 std::uint8_t len{};
11457 return get_number(input_format_t::msgpack, len) &&
11458 get_binary(input_format_t::msgpack, len, result);
11459 }
11460
11461 case 0xC5: // bin 16
11462 {
11463 std::uint16_t len{};
11464 return get_number(input_format_t::msgpack, len) &&
11465 get_binary(input_format_t::msgpack, len, result);
11466 }
11467
11468 case 0xC6: // bin 32
11469 {
11470 std::uint32_t len{};
11471 return get_number(input_format_t::msgpack, len) &&
11472 get_binary(input_format_t::msgpack, len, result);
11473 }
11474
11475 case 0xC7: // ext 8
11476 {
11477 std::uint8_t len{};
11478 std::int8_t subtype{};
11479 return get_number(input_format_t::msgpack, len) &&
11480 get_number(input_format_t::msgpack, subtype) &&
11481 get_binary(input_format_t::msgpack, len, result) &&
11482 assign_and_return_true(subtype);
11483 }
11484
11485 case 0xC8: // ext 16
11486 {
11487 std::uint16_t len{};
11488 std::int8_t subtype{};
11489 return get_number(input_format_t::msgpack, len) &&
11490 get_number(input_format_t::msgpack, subtype) &&
11491 get_binary(input_format_t::msgpack, len, result) &&
11492 assign_and_return_true(subtype);
11493 }
11494
11495 case 0xC9: // ext 32
11496 {
11497 std::uint32_t len{};
11498 std::int8_t subtype{};
11499 return get_number(input_format_t::msgpack, len) &&
11500 get_number(input_format_t::msgpack, subtype) &&
11501 get_binary(input_format_t::msgpack, len, result) &&
11502 assign_and_return_true(subtype);
11503 }
11504
11505 case 0xD4: // fixext 1
11506 {
11507 std::int8_t subtype{};
11508 return get_number(input_format_t::msgpack, subtype) &&
11509 get_binary(input_format_t::msgpack, 1, result) &&
11510 assign_and_return_true(subtype);
11511 }
11512
11513 case 0xD5: // fixext 2
11514 {
11515 std::int8_t subtype{};
11516 return get_number(input_format_t::msgpack, subtype) &&
11517 get_binary(input_format_t::msgpack, 2, result) &&
11518 assign_and_return_true(subtype);
11519 }
11520
11521 case 0xD6: // fixext 4
11522 {
11523 std::int8_t subtype{};
11524 return get_number(input_format_t::msgpack, subtype) &&
11525 get_binary(input_format_t::msgpack, 4, result) &&
11526 assign_and_return_true(subtype);
11527 }
11528
11529 case 0xD7: // fixext 8
11530 {
11531 std::int8_t subtype{};
11532 return get_number(input_format_t::msgpack, subtype) &&
11533 get_binary(input_format_t::msgpack, 8, result) &&
11534 assign_and_return_true(subtype);
11535 }
11536
11537 case 0xD8: // fixext 16
11538 {
11539 std::int8_t subtype{};
11540 return get_number(input_format_t::msgpack, subtype) &&
11541 get_binary(input_format_t::msgpack, 16, result) &&
11542 assign_and_return_true(subtype);
11543 }
11544
11545 default: // LCOV_EXCL_LINE
11546 return false; // LCOV_EXCL_LINE
11547 }
11548 }
11549
11550 /*!
11551 @param[in] len the length of the array
11552 @return whether array creation completed
11553 */
11554 bool get_msgpack_array(const std::size_t len)
11555 {
11556 if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
11557 {
11558 return false;
11559 }
11560
11561 for (std::size_t i = 0; i < len; ++i)
11562 {
11563 if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
11564 {
11565 return false;
11566 }
11567 }
11568
11569 return sax->end_array();
11570 }
11571
11572 /*!
11573 @param[in] len the length of the object
11574 @return whether object creation completed
11575 */
11576 bool get_msgpack_object(const std::size_t len)
11577 {
11578 if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
11579 {
11580 return false;
11581 }
11582
11583 string_t key;
11584 for (std::size_t i = 0; i < len; ++i)
11585 {
11586 get();
11587 if (JSON_HEDLEY_UNLIKELY(!get_msgpack_string(key) || !sax->key(key)))
11588 {
11589 return false;
11590 }
11591
11592 if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
11593 {
11594 return false;
11595 }
11596 key.clear();
11597 }
11598
11599 return sax->end_object();
11600 }
11601
11602 ////////////
11603 // UBJSON //
11604 ////////////
11605
11606 /*!
11607 @param[in] get_char whether a new character should be retrieved from the
11608 input (true, default) or whether the last read
11609 character should be considered instead
11610
11611 @return whether a valid UBJSON value was passed to the SAX parser
11612 */
11613 bool parse_ubjson_internal(const bool get_char = true)
11614 {
11615 return get_ubjson_value(get_char ? get_ignore_noop() : current);
11616 }
11617
11618 /*!
11619 @brief reads a UBJSON string
11620
11621 This function is either called after reading the 'S' byte explicitly
11622 indicating a string, or in case of an object key where the 'S' byte can be
11623 left out.
11624
11625 @param[out] result created string
11626 @param[in] get_char whether a new character should be retrieved from the
11627 input (true, default) or whether the last read
11628 character should be considered instead
11629
11630 @return whether string creation completed
11631 */
11632 bool get_ubjson_string(string_t& result, const bool get_char = true)
11633 {
11634 if (get_char)
11635 {
11636 get(); // TODO(niels): may we ignore N here?
11637 }
11638
11639 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "value")))
11640 {
11641 return false;
11642 }
11643
11644 switch (current)
11645 {
11646 case 'U':
11647 {
11648 std::uint8_t len{};
11649 return get_number(input_format, len) && get_string(input_format, len, result);
11650 }
11651
11652 case 'i':
11653 {
11654 std::int8_t len{};
11655 return get_number(input_format, len) && get_string(input_format, len, result);
11656 }
11657
11658 case 'I':
11659 {
11660 std::int16_t len{};
11661 return get_number(input_format, len) && get_string(input_format, len, result);
11662 }
11663
11664 case 'l':
11665 {
11666 std::int32_t len{};
11667 return get_number(input_format, len) && get_string(input_format, len, result);
11668 }
11669
11670 case 'L':
11671 {
11672 std::int64_t len{};
11673 return get_number(input_format, len) && get_string(input_format, len, result);
11674 }
11675
11676 case 'u':
11677 {
11678 if (input_format != input_format_t::bjdata)
11679 {
11680 break;
11681 }
11682 std::uint16_t len{};
11683 return get_number(input_format, len) && get_string(input_format, len, result);
11684 }
11685
11686 case 'm':
11687 {
11688 if (input_format != input_format_t::bjdata)
11689 {
11690 break;
11691 }
11692 std::uint32_t len{};
11693 return get_number(input_format, len) && get_string(input_format, len, result);
11694 }
11695
11696 case 'M':
11697 {
11698 if (input_format != input_format_t::bjdata)
11699 {
11700 break;
11701 }
11702 std::uint64_t len{};
11703 return get_number(input_format, len) && get_string(input_format, len, result);
11704 }
11705
11706 default:
11707 break;
11708 }
11709 auto last_token = get_token_string();
11710 std::string message;
11711
11712 if (input_format != input_format_t::bjdata)
11713 {
11714 message = "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token;
11715 }
11716 else
11717 {
11718 message = "expected length type specification (U, i, u, I, m, l, M, L); last byte: 0x" + last_token;
11719 }
11720 return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "string"), nullptr));
11721 }
11722
11723 /*!
11724 @param[out] dim an integer vector storing the ND array dimensions
11725 @return whether reading ND array size vector is successful
11726 */
11727 bool get_ubjson_ndarray_size(std::vector<size_t>& dim)
11728 {
11729 std::pair<std::size_t, char_int_type> size_and_type;
11730 size_t dimlen = 0;
11731 bool no_ndarray = true;
11732
11733 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type, no_ndarray)))
11734 {
11735 return false;
11736 }
11737
11738 if (size_and_type.first != npos)
11739 {
11740 if (size_and_type.second != 0)
11741 {
11742 if (size_and_type.second != 'N')
11743 {
11744 for (std::size_t i = 0; i < size_and_type.first; ++i)
11745 {
11746 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, size_and_type.second)))
11747 {
11748 return false;
11749 }
11750 dim.push_back(dimlen);
11751 }
11752 }
11753 }
11754 else
11755 {
11756 for (std::size_t i = 0; i < size_and_type.first; ++i)
11757 {
11758 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray)))
11759 {
11760 return false;
11761 }
11762 dim.push_back(dimlen);
11763 }
11764 }
11765 }
11766 else
11767 {
11768 while (current != ']')
11769 {
11770 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, current)))
11771 {
11772 return false;
11773 }
11774 dim.push_back(dimlen);
11775 get_ignore_noop();
11776 }
11777 }
11778 return true;
11779 }
11780
11781 /*!
11782 @param[out] result determined size
11783 @param[in,out] is_ndarray for input, `true` means already inside an ndarray vector
11784 or ndarray dimension is not allowed; `false` means ndarray
11785 is allowed; for output, `true` means an ndarray is found;
11786 is_ndarray can only return `true` when its initial value
11787 is `false`
11788 @param[in] prefix type marker if already read, otherwise set to 0
11789
11790 @return whether size determination completed
11791 */
11792 bool get_ubjson_size_value(std::size_t& result, bool& is_ndarray, char_int_type prefix = 0)
11793 {
11794 if (prefix == 0)
11795 {
11796 prefix = get_ignore_noop();
11797 }
11798
11799 switch (prefix)
11800 {
11801 case 'U':
11802 {
11803 std::uint8_t number{};
11804 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11805 {
11806 return false;
11807 }
11808 result = static_cast<std::size_t>(number);
11809 return true;
11810 }
11811
11812 case 'i':
11813 {
11814 std::int8_t number{};
11815 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11816 {
11817 return false;
11818 }
11819 if (number < 0)
11820 {
11821 return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
11822 exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
11823 }
11824 result = static_cast<std::size_t>(number); // NOLINT(bugprone-signed-char-misuse,cert-str34-c): number is not a char
11825 return true;
11826 }
11827
11828 case 'I':
11829 {
11830 std::int16_t number{};
11831 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11832 {
11833 return false;
11834 }
11835 if (number < 0)
11836 {
11837 return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
11838 exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
11839 }
11840 result = static_cast<std::size_t>(number);
11841 return true;
11842 }
11843
11844 case 'l':
11845 {
11846 std::int32_t number{};
11847 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11848 {
11849 return false;
11850 }
11851 if (number < 0)
11852 {
11853 return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
11854 exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
11855 }
11856 result = static_cast<std::size_t>(number);
11857 return true;
11858 }
11859
11860 case 'L':
11861 {
11862 std::int64_t number{};
11863 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11864 {
11865 return false;
11866 }
11867 if (number < 0)
11868 {
11869 return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
11870 exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
11871 }
11872 if (!value_in_range_of<std::size_t>(number))
11873 {
11874 return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408,
11875 exception_message(input_format, "integer value overflow", "size"), nullptr));
11876 }
11877 result = static_cast<std::size_t>(number);
11878 return true;
11879 }
11880
11881 case 'u':
11882 {
11883 if (input_format != input_format_t::bjdata)
11884 {
11885 break;
11886 }
11887 std::uint16_t number{};
11888 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11889 {
11890 return false;
11891 }
11892 result = static_cast<std::size_t>(number);
11893 return true;
11894 }
11895
11896 case 'm':
11897 {
11898 if (input_format != input_format_t::bjdata)
11899 {
11900 break;
11901 }
11902 std::uint32_t number{};
11903 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11904 {
11905 return false;
11906 }
11907 result = conditional_static_cast<std::size_t>(number);
11908 return true;
11909 }
11910
11911 case 'M':
11912 {
11913 if (input_format != input_format_t::bjdata)
11914 {
11915 break;
11916 }
11917 std::uint64_t number{};
11918 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11919 {
11920 return false;
11921 }
11922 if (!value_in_range_of<std::size_t>(number))
11923 {
11924 return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408,
11925 exception_message(input_format, "integer value overflow", "size"), nullptr));
11926 }
11927 result = detail::conditional_static_cast<std::size_t>(number);
11928 return true;
11929 }
11930
11931 case '[':
11932 {
11933 if (input_format != input_format_t::bjdata)
11934 {
11935 break;
11936 }
11937 if (is_ndarray) // ndarray dimensional vector can only contain integers, and can not embed another array
11938 {
11939 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));
11940 }
11941 std::vector<size_t> dim;
11942 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_ndarray_size(dim)))
11943 {
11944 return false;
11945 }
11946 if (dim.size() == 1 || (dim.size() == 2 && dim.at(0) == 1)) // return normal array size if 1D row vector
11947 {
11948 result = dim.at(dim.size() - 1);
11949 return true;
11950 }
11951 if (!dim.empty()) // if ndarray, convert to an object in JData annotated array format
11952 {
11953 for (auto i : dim) // test if any dimension in an ndarray is 0, if so, return a 1D empty container
11954 {
11955 if ( i == 0 )
11956 {
11957 result = 0;
11958 return true;
11959 }
11960 }
11961
11962 string_t key = "_ArraySize_";
11963 if (JSON_HEDLEY_UNLIKELY(!sax->start_object(3) || !sax->key(key) || !sax->start_array(dim.size())))
11964 {
11965 return false;
11966 }
11967 result = 1;
11968 for (auto i : dim)
11969 {
11970 result *= i;
11971 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()
11972 {
11973 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));
11974 }
11975 if (JSON_HEDLEY_UNLIKELY(!sax->number_unsigned(static_cast<number_unsigned_t>(i))))
11976 {
11977 return false;
11978 }
11979 }
11980 is_ndarray = true;
11981 return sax->end_array();
11982 }
11983 result = 0;
11984 return true;
11985 }
11986
11987 default:
11988 break;
11989 }
11990 auto last_token = get_token_string();
11991 std::string message;
11992
11993 if (input_format != input_format_t::bjdata)
11994 {
11995 message = "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token;
11996 }
11997 else
11998 {
11999 message = "expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x" + last_token;
12000 }
12001 return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "size"), nullptr));
12002 }
12003
12004 /*!
12005 @brief determine the type and size for a container
12006
12007 In the optimized UBJSON format, a type and a size can be provided to allow
12008 for a more compact representation.
12009
12010 @param[out] result pair of the size and the type
12011 @param[in] inside_ndarray whether the parser is parsing an ND array dimensional vector
12012
12013 @return whether pair creation completed
12014 */
12015 bool get_ubjson_size_type(std::pair<std::size_t, char_int_type>& result, bool inside_ndarray = false)
12016 {
12017 result.first = npos; // size
12018 result.second = 0; // type
12019 bool is_ndarray = false;
12020
12021 get_ignore_noop();
12022
12023 if (current == '$')
12024 {
12025 result.second = get(); // must not ignore 'N', because 'N' maybe the type
12026 if (input_format == input_format_t::bjdata
12027 && JSON_HEDLEY_UNLIKELY(std::binary_search(bjd_optimized_type_markers.begin(), bjd_optimized_type_markers.end(), result.second)))
12028 {
12029 auto last_token = get_token_string();
12030 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
12031 exception_message(input_format, concat("marker 0x", last_token, " is not a permitted optimized array type"), "type"), nullptr));
12032 }
12033
12034 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "type")))
12035 {
12036 return false;
12037 }
12038
12039 get_ignore_noop();
12040 if (JSON_HEDLEY_UNLIKELY(current != '#'))
12041 {
12042 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "value")))
12043 {
12044 return false;
12045 }
12046 auto last_token = get_token_string();
12047 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
12048 exception_message(input_format, concat("expected '#' after type information; last byte: 0x", last_token), "size"), nullptr));
12049 }
12050
12051 const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
12052 if (input_format == input_format_t::bjdata && is_ndarray)
12053 {
12054 if (inside_ndarray)
12055 {
12056 return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
12057 exception_message(input_format, "ndarray can not be recursive", "size"), nullptr));
12058 }
12059 result.second |= (1 << 8); // use bit 8 to indicate ndarray, all UBJSON and BJData markers should be ASCII letters
12060 }
12061 return is_error;
12062 }
12063
12064 if (current == '#')
12065 {
12066 const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
12067 if (input_format == input_format_t::bjdata && is_ndarray)
12068 {
12069 return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
12070 exception_message(input_format, "ndarray requires both type and size", "size"), nullptr));
12071 }
12072 return is_error;
12073 }
12074
12075 return true;
12076 }
12077
12078 /*!
12079 @param prefix the previously read or set type prefix
12080 @return whether value creation completed
12081 */
12082 bool get_ubjson_value(const char_int_type prefix)
12083 {
12084 switch (prefix)
12085 {
12086 case char_traits<char_type>::eof(): // EOF
12087 return unexpect_eof(input_format, "value");
12088
12089 case 'T': // true
12090 return sax->boolean(true);
12091 case 'F': // false
12092 return sax->boolean(false);
12093
12094 case 'Z': // null
12095 return sax->null();
12096
12097 case 'B': // byte
12098 {
12099 if (input_format != input_format_t::bjdata)
12100 {
12101 break;
12102 }
12103 std::uint8_t number{};
12104 return get_number(input_format, number) && sax->number_unsigned(number);
12105 }
12106
12107 case 'U':
12108 {
12109 std::uint8_t number{};
12110 return get_number(input_format, number) && sax->number_unsigned(number);
12111 }
12112
12113 case 'i':
12114 {
12115 std::int8_t number{};
12116 return get_number(input_format, number) && sax->number_integer(number);
12117 }
12118
12119 case 'I':
12120 {
12121 std::int16_t number{};
12122 return get_number(input_format, number) && sax->number_integer(number);
12123 }
12124
12125 case 'l':
12126 {
12127 std::int32_t number{};
12128 return get_number(input_format, number) && sax->number_integer(number);
12129 }
12130
12131 case 'L':
12132 {
12133 std::int64_t number{};
12134 return get_number(input_format, number) && sax->number_integer(number);
12135 }
12136
12137 case 'u':
12138 {
12139 if (input_format != input_format_t::bjdata)
12140 {
12141 break;
12142 }
12143 std::uint16_t number{};
12144 return get_number(input_format, number) && sax->number_unsigned(number);
12145 }
12146
12147 case 'm':
12148 {
12149 if (input_format != input_format_t::bjdata)
12150 {
12151 break;
12152 }
12153 std::uint32_t number{};
12154 return get_number(input_format, number) && sax->number_unsigned(number);
12155 }
12156
12157 case 'M':
12158 {
12159 if (input_format != input_format_t::bjdata)
12160 {
12161 break;
12162 }
12163 std::uint64_t number{};
12164 return get_number(input_format, number) && sax->number_unsigned(number);
12165 }
12166
12167 case 'h':
12168 {
12169 if (input_format != input_format_t::bjdata)
12170 {
12171 break;
12172 }
12173 const auto byte1_raw = get();
12174 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
12175 {
12176 return false;
12177 }
12178 const auto byte2_raw = get();
12179 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
12180 {
12181 return false;
12182 }
12183
12184 const auto byte1 = static_cast<unsigned char>(byte1_raw);
12185 const auto byte2 = static_cast<unsigned char>(byte2_raw);
12186
12187 // code from RFC 7049, Appendix D, Figure 3:
12188 // As half-precision floating-point numbers were only added
12189 // to IEEE 754 in 2008, today's programming platforms often
12190 // still only have limited support for them. It is very
12191 // easy to include at least decoding support for them even
12192 // without such support. An example of a small decoder for
12193 // half-precision floating-point numbers in the C language
12194 // is shown in Fig. 3.
12195 const auto half = static_cast<unsigned int>((byte2 << 8u) + byte1);
12196 const double val = [&half]
12197 {
12198 const int exp = (half >> 10u) & 0x1Fu;
12199 const unsigned int mant = half & 0x3FFu;
12200 JSON_ASSERT(0 <= exp&& exp <= 32);
12201 JSON_ASSERT(mant <= 1024);
12202 switch (exp)
12203 {
12204 case 0:
12205 return std::ldexp(mant, -24);
12206 case 31:
12207 return (mant == 0)
12208 ? std::numeric_limits<double>::infinity()
12209 : std::numeric_limits<double>::quiet_NaN();
12210 default:
12211 return std::ldexp(mant + 1024, exp - 25);
12212 }
12213 }();
12214 return sax->number_float((half & 0x8000u) != 0
12215 ? static_cast<number_float_t>(-val)
12216 : static_cast<number_float_t>(val), "");
12217 }
12218
12219 case 'd':
12220 {
12221 float number{};
12222 return get_number(input_format, number) && sax->number_float(static_cast<number_float_t>(number), "");
12223 }
12224
12225 case 'D':
12226 {
12227 double number{};
12228 return get_number(input_format, number) && sax->number_float(static_cast<number_float_t>(number), "");
12229 }
12230
12231 case 'H':
12232 {
12233 return get_ubjson_high_precision_number();
12234 }
12235
12236 case 'C': // char
12237 {
12238 get();
12239 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "char")))
12240 {
12241 return false;
12242 }
12243 if (JSON_HEDLEY_UNLIKELY(current > 127))
12244 {
12245 auto last_token = get_token_string();
12246 return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
12247 exception_message(input_format, concat("byte after 'C' must be in range 0x00..0x7F; last byte: 0x", last_token), "char"), nullptr));
12248 }
12249 string_t s(1, static_cast<typename string_t::value_type>(current));
12250 return sax->string(s);
12251 }
12252
12253 case 'S': // string
12254 {
12255 string_t s;
12256 return get_ubjson_string(s) && sax->string(s);
12257 }
12258
12259 case '[': // array
12260 return get_ubjson_array();
12261
12262 case '{': // object
12263 return get_ubjson_object();
12264
12265 default: // anything else
12266 break;
12267 }
12268 auto last_token = get_token_string();
12269 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));
12270 }
12271
12272 /*!
12273 @return whether array creation completed
12274 */
12275 bool get_ubjson_array()
12276 {
12277 std::pair<std::size_t, char_int_type> size_and_type;
12278 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type)))
12279 {
12280 return false;
12281 }
12282
12283 // 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):
12284 // {"_ArrayType_" : "typeid", "_ArraySize_" : [n1, n2, ...], "_ArrayData_" : [v1, v2, ...]}
12285
12286 if (input_format == input_format_t::bjdata && size_and_type.first != npos && (size_and_type.second & (1 << 8)) != 0)
12287 {
12288 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
12289 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)
12290 {
12291 return p.first < t;
12292 });
12293 string_t key = "_ArrayType_";
12294 if (JSON_HEDLEY_UNLIKELY(it == bjd_types_map.end() || it->first != size_and_type.second))
12295 {
12296 auto last_token = get_token_string();
12297 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
12298 exception_message(input_format, "invalid byte: 0x" + last_token, "type"), nullptr));
12299 }
12300
12301 string_t type = it->second; // sax->string() takes a reference
12302 if (JSON_HEDLEY_UNLIKELY(!sax->key(key) || !sax->string(type)))
12303 {
12304 return false;
12305 }
12306
12307 if (size_and_type.second == 'C' || size_and_type.second == 'B')
12308 {
12309 size_and_type.second = 'U';
12310 }
12311
12312 key = "_ArrayData_";
12313 if (JSON_HEDLEY_UNLIKELY(!sax->key(key) || !sax->start_array(size_and_type.first) ))
12314 {
12315 return false;
12316 }
12317
12318 for (std::size_t i = 0; i < size_and_type.first; ++i)
12319 {
12320 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
12321 {
12322 return false;
12323 }
12324 }
12325
12326 return (sax->end_array() && sax->end_object());
12327 }
12328
12329 // If BJData type marker is 'B' decode as binary
12330 if (input_format == input_format_t::bjdata && size_and_type.first != npos && size_and_type.second == 'B')
12331 {
12332 binary_t result;
12333 return get_binary(input_format, size_and_type.first, result) && sax->binary(result);
12334 }
12335
12336 if (size_and_type.first != npos)
12337 {
12338 if (JSON_HEDLEY_UNLIKELY(!sax->start_array(size_and_type.first)))
12339 {
12340 return false;
12341 }
12342
12343 if (size_and_type.second != 0)
12344 {
12345 if (size_and_type.second != 'N')
12346 {
12347 for (std::size_t i = 0; i < size_and_type.first; ++i)
12348 {
12349 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
12350 {
12351 return false;
12352 }
12353 }
12354 }
12355 }
12356 else
12357 {
12358 for (std::size_t i = 0; i < size_and_type.first; ++i)
12359 {
12360 if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
12361 {
12362 return false;
12363 }
12364 }
12365 }
12366 }
12367 else
12368 {
12369 if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
12370 {
12371 return false;
12372 }
12373
12374 while (current != ']')
12375 {
12376 if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal(false)))
12377 {
12378 return false;
12379 }
12380 get_ignore_noop();
12381 }
12382 }
12383
12384 return sax->end_array();
12385 }
12386
12387 /*!
12388 @return whether object creation completed
12389 */
12390 bool get_ubjson_object()
12391 {
12392 std::pair<std::size_t, char_int_type> size_and_type;
12393 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type)))
12394 {
12395 return false;
12396 }
12397
12398 // do not accept ND-array size in objects in BJData
12399 if (input_format == input_format_t::bjdata && size_and_type.first != npos && (size_and_type.second & (1 << 8)) != 0)
12400 {
12401 auto last_token = get_token_string();
12402 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
12403 exception_message(input_format, "BJData object does not support ND-array size in optimized format", "object"), nullptr));
12404 }
12405
12406 string_t key;
12407 if (size_and_type.first != npos)
12408 {
12409 if (JSON_HEDLEY_UNLIKELY(!sax->start_object(size_and_type.first)))
12410 {
12411 return false;
12412 }
12413
12414 if (size_and_type.second != 0)
12415 {
12416 for (std::size_t i = 0; i < size_and_type.first; ++i)
12417 {
12418 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
12419 {
12420 return false;
12421 }
12422 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
12423 {
12424 return false;
12425 }
12426 key.clear();
12427 }
12428 }
12429 else
12430 {
12431 for (std::size_t i = 0; i < size_and_type.first; ++i)
12432 {
12433 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
12434 {
12435 return false;
12436 }
12437 if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
12438 {
12439 return false;
12440 }
12441 key.clear();
12442 }
12443 }
12444 }
12445 else
12446 {
12447 if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
12448 {
12449 return false;
12450 }
12451
12452 while (current != '}')
12453 {
12454 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key, false) || !sax->key(key)))
12455 {
12456 return false;
12457 }
12458 if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
12459 {
12460 return false;
12461 }
12462 get_ignore_noop();
12463 key.clear();
12464 }
12465 }
12466
12467 return sax->end_object();
12468 }
12469
12470 // Note, no reader for UBJSON binary types is implemented because they do
12471 // not exist
12472
12473 bool get_ubjson_high_precision_number()
12474 {
12475 // get size of following number string
12476 std::size_t size{};
12477 bool no_ndarray = true;
12478 auto res = get_ubjson_size_value(size, no_ndarray);
12479 if (JSON_HEDLEY_UNLIKELY(!res))
12480 {
12481 return res;
12482 }
12483
12484 // get number string
12485 std::vector<char> number_vector;
12486 for (std::size_t i = 0; i < size; ++i)
12487 {
12488 get();
12489 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
12490 {
12491 return false;
12492 }
12493 number_vector.push_back(static_cast<char>(current));
12494 }
12495
12496 // parse number string
12497 using ia_type = decltype(detail::input_adapter(number_vector));
12498 auto number_lexer = detail::lexer<BasicJsonType, ia_type>(detail::input_adapter(number_vector), false);
12499 const auto result_number = number_lexer.scan();
12500 const auto number_string = number_lexer.get_token_string();
12501 const auto result_remainder = number_lexer.scan();
12502
12503 using token_type = typename detail::lexer_base<BasicJsonType>::token_type;
12504
12505 if (JSON_HEDLEY_UNLIKELY(result_remainder != token_type::end_of_input))
12506 {
12507 return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read,
12508 exception_message(input_format, concat("invalid number text: ", number_lexer.get_token_string()), "high-precision number"), nullptr));
12509 }
12510
12511 switch (result_number)
12512 {
12513 case token_type::value_integer:
12514 return sax->number_integer(number_lexer.get_number_integer());
12515 case token_type::value_unsigned:
12516 return sax->number_unsigned(number_lexer.get_number_unsigned());
12517 case token_type::value_float:
12518 return sax->number_float(number_lexer.get_number_float(), std::move(number_string));
12519 case token_type::uninitialized:
12520 case token_type::literal_true:
12521 case token_type::literal_false:
12522 case token_type::literal_null:
12523 case token_type::value_string:
12524 case token_type::begin_array:
12525 case token_type::begin_object:
12526 case token_type::end_array:
12527 case token_type::end_object:
12528 case token_type::name_separator:
12529 case token_type::value_separator:
12530 case token_type::parse_error:
12531 case token_type::end_of_input:
12532 case token_type::literal_or_value:
12533 default:
12534 return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read,
12535 exception_message(input_format, concat("invalid number text: ", number_lexer.get_token_string()), "high-precision number"), nullptr));
12536 }
12537 }
12538
12539 ///////////////////////
12540 // Utility functions //
12541 ///////////////////////
12542
12543 /*!
12544 @brief get next character from the input
12545
12546 This function provides the interface to the used input adapter. It does
12547 not throw in case the input reached EOF, but returns a -'ve valued
12548 `char_traits<char_type>::eof()` in that case.
12549
12550 @return character read from the input
12551 */
12552 char_int_type get()
12553 {
12554 ++chars_read;
12555 return current = ia.get_character();
12556 }
12557
12558 /*!
12559 @brief get_to read into a primitive type
12560
12561 This function provides the interface to the used input adapter. It does
12562 not throw in case the input reached EOF, but returns false instead
12563
12564 @return bool, whether the read was successful
12565 */
12566 template<class T>
12567 bool get_to(T& dest, const input_format_t format, const char* context)
12568 {
12569 auto new_chars_read = ia.get_elements(&dest);
12570 chars_read += new_chars_read;
12571 if (JSON_HEDLEY_UNLIKELY(new_chars_read < sizeof(T)))
12572 {
12573 // in case of failure, advance position by 1 to report failing location
12574 ++chars_read;
12575 sax->parse_error(chars_read, "<end of file>", parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), nullptr));
12576 return false;
12577 }
12578 return true;
12579 }
12580
12581 /*!
12582 @return character read from the input after ignoring all 'N' entries
12583 */
12584 char_int_type get_ignore_noop()
12585 {
12586 do
12587 {
12588 get();
12589 }
12590 while (current == 'N');
12591
12592 return current;
12593 }
12594
12595 template<class NumberType>
12596 static void byte_swap(NumberType& number)
12597 {
12598 constexpr std::size_t sz = sizeof(number);
12599 #ifdef __cpp_lib_byteswap
12600 if constexpr (sz == 1)
12601 {
12602 return;
12603 }
12604 if constexpr(std::is_integral_v<NumberType>)
12605 {
12606 number = std::byteswap(number);
12607 return;
12608 }
12609 #endif
12610 auto* ptr = reinterpret_cast<std::uint8_t*>(&number);
12611 for (std::size_t i = 0; i < sz / 2; ++i)
12612 {
12613 std::swap(ptr[i], ptr[sz - i - 1]);
12614 }
12615 }
12616
12617 /*
12618 @brief read a number from the input
12619
12620 @tparam NumberType the type of the number
12621 @param[in] format the current format (for diagnostics)
12622 @param[out] result number of type @a NumberType
12623
12624 @return whether conversion completed
12625
12626 @note This function needs to respect the system's endianness, because
12627 bytes in CBOR, MessagePack, and UBJSON are stored in network order
12628 (big endian) and therefore need reordering on little endian systems.
12629 On the other hand, BSON and BJData use little endian and should reorder
12630 on big endian systems.
12631 */
12632 template<typename NumberType, bool InputIsLittleEndian = false>
12633 bool get_number(const input_format_t format, NumberType& result)
12634 {
12635 // read in the original format
12636
12637 if (JSON_HEDLEY_UNLIKELY(!get_to(result, format, "number")))
12638 {
12639 return false;
12640 }
12641 if (is_little_endian != (InputIsLittleEndian || format == input_format_t::bjdata))
12642 {
12643 byte_swap(result);
12644 }
12645 return true;
12646 }
12647
12648 /*!
12649 @brief create a string by reading characters from the input
12650
12651 @tparam NumberType the type of the number
12652 @param[in] format the current format (for diagnostics)
12653 @param[in] len number of characters to read
12654 @param[out] result string created by reading @a len bytes
12655
12656 @return whether string creation completed
12657
12658 @note We can not reserve @a len bytes for the result, because @a len
12659 may be too large. Usually, @ref unexpect_eof() detects the end of
12660 the input before we run out of string memory.
12661 */
12662 template<typename NumberType>
12663 bool get_string(const input_format_t format,
12664 const NumberType len,
12665 string_t& result)
12666 {
12667 bool success = true;
12668 for (NumberType i = 0; i < len; i++)
12669 {
12670 get();
12671 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "string")))
12672 {
12673 success = false;
12674 break;
12675 }
12676 result.push_back(static_cast<typename string_t::value_type>(current));
12677 }
12678 return success;
12679 }
12680
12681 /*!
12682 @brief create a byte array by reading bytes from the input
12683
12684 @tparam NumberType the type of the number
12685 @param[in] format the current format (for diagnostics)
12686 @param[in] len number of bytes to read
12687 @param[out] result byte array created by reading @a len bytes
12688
12689 @return whether byte array creation completed
12690
12691 @note We can not reserve @a len bytes for the result, because @a len
12692 may be too large. Usually, @ref unexpect_eof() detects the end of
12693 the input before we run out of memory.
12694 */
12695 template<typename NumberType>
12696 bool get_binary(const input_format_t format,
12697 const NumberType len,
12698 binary_t& result)
12699 {
12700 bool success = true;
12701 for (NumberType i = 0; i < len; i++)
12702 {
12703 get();
12704 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "binary")))
12705 {
12706 success = false;
12707 break;
12708 }
12709 result.push_back(static_cast<std::uint8_t>(current));
12710 }
12711 return success;
12712 }
12713
12714 /*!
12715 @param[in] format the current format (for diagnostics)
12716 @param[in] context further context information (for diagnostics)
12717 @return whether the last read character is not EOF
12718 */
12719 JSON_HEDLEY_NON_NULL(3)
12720 bool unexpect_eof(const input_format_t format, const char* context) const
12721 {
12722 if (JSON_HEDLEY_UNLIKELY(current == char_traits<char_type>::eof()))
12723 {
12724 return sax->parse_error(chars_read, "<end of file>",
12725 parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), nullptr));
12726 }
12727 return true;
12728 }
12729
12730 /*!
12731 @return a string representation of the last read byte
12732 */
12733 std::string get_token_string() const
12734 {
12735 std::array<char, 3> cr{{}};
12736 static_cast<void>((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(current))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
12737 return std::string{cr.data()};
12738 }
12739
12740 /*!
12741 @param[in] format the current format
12742 @param[in] detail a detailed error message
12743 @param[in] context further context information
12744 @return a message string to use in the parse_error exceptions
12745 */
12746 std::string exception_message(const input_format_t format,
12747 const std::string& detail,
12748 const std::string& context) const
12749 {
12750 std::string error_msg = "syntax error while parsing ";
12751
12752 switch (format)
12753 {
12754 case input_format_t::cbor:
12755 error_msg += "CBOR";
12756 break;
12757
12758 case input_format_t::msgpack:
12759 error_msg += "MessagePack";
12760 break;
12761
12762 case input_format_t::ubjson:
12763 error_msg += "UBJSON";
12764 break;
12765
12766 case input_format_t::bson:
12767 error_msg += "BSON";
12768 break;
12769
12770 case input_format_t::bjdata:
12771 error_msg += "BJData";
12772 break;
12773
12774 case input_format_t::json: // LCOV_EXCL_LINE
12775 default: // LCOV_EXCL_LINE
12776 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
12777 }
12778
12779 return concat(error_msg, ' ', context, ": ", detail);
12780 }
12781
12782 private:
12783 static JSON_INLINE_VARIABLE constexpr std::size_t npos = detail::unknown_size();
12784
12785 /// input adapter
12786 InputAdapterType ia;
12787
12788 /// the current character
12789 char_int_type current = char_traits<char_type>::eof();
12790
12791 /// the number of characters read
12792 std::size_t chars_read = 0;
12793
12794 /// whether we can assume little endianness
12795 const bool is_little_endian = little_endianness();
12796
12797 /// input format
12798 const input_format_t input_format = input_format_t::json;
12799
12800 /// the SAX parser
12801 json_sax_t* sax = nullptr;
12802
12803 // excluded markers in bjdata optimized type
12804 #define JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_ \
12805 make_array<char_int_type>('F', 'H', 'N', 'S', 'T', 'Z', '[', '{')
12806
12807 #define JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_ \
12808 make_array<bjd_type>( \
12809 bjd_type{'B', "byte"}, \
12810 bjd_type{'C', "char"}, \
12811 bjd_type{'D', "double"}, \
12812 bjd_type{'I', "int16"}, \
12813 bjd_type{'L', "int64"}, \
12814 bjd_type{'M', "uint64"}, \
12815 bjd_type{'U', "uint8"}, \
12816 bjd_type{'d', "single"}, \
12817 bjd_type{'i', "int8"}, \
12818 bjd_type{'l', "int32"}, \
12819 bjd_type{'m', "uint32"}, \
12820 bjd_type{'u', "uint16"})
12821
12822 JSON_PRIVATE_UNLESS_TESTED:
12823 // lookup tables
12824 // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
12825 const decltype(JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_) bjd_optimized_type_markers =
12826 JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_;
12827
12828 using bjd_type = std::pair<char_int_type, string_t>;
12829 // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
12830 const decltype(JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_) bjd_types_map =
12831 JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_;
12832
12833 #undef JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_
12834 #undef JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_
12835 };
12836
12837 #ifndef JSON_HAS_CPP_17
12838 template<typename BasicJsonType, typename InputAdapterType, typename SAX>
12839 constexpr std::size_t binary_reader<BasicJsonType, InputAdapterType, SAX>::npos;
12840 #endif
12841
12842 } // namespace detail
12843 NLOHMANN_JSON_NAMESPACE_END
12844
12845 // #include <nlohmann/detail/input/input_adapters.hpp>
12846
12847 // #include <nlohmann/detail/input/lexer.hpp>
12848
12849 // #include <nlohmann/detail/input/parser.hpp>
12850 // __ _____ _____ _____
12851 // __| | __| | | | JSON for Modern C++
12852 // | | |__ | | | | | | version 3.12.0
12853 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
12854 //
12855 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
12856 // SPDX-License-Identifier: MIT
12857
12858
12859
12860 #include <cmath> // isfinite
12861 #include <cstdint> // uint8_t
12862 #include <functional> // function
12863 #include <string> // string
12864 #include <utility> // move
12865 #include <vector> // vector
12866
12867 // #include <nlohmann/detail/exceptions.hpp>
12868
12869 // #include <nlohmann/detail/input/input_adapters.hpp>
12870
12871 // #include <nlohmann/detail/input/json_sax.hpp>
12872
12873 // #include <nlohmann/detail/input/lexer.hpp>
12874
12875 // #include <nlohmann/detail/macro_scope.hpp>
12876
12877 // #include <nlohmann/detail/meta/is_sax.hpp>
12878
12879 // #include <nlohmann/detail/string_concat.hpp>
12880
12881 // #include <nlohmann/detail/value_t.hpp>
12882
12883
12884 NLOHMANN_JSON_NAMESPACE_BEGIN
12885 namespace detail
12886 {
12887 ////////////
12888 // parser //
12889 ////////////
12890
12891 enum class parse_event_t : std::uint8_t
12892 {
12893 /// the parser read `{` and started to process a JSON object
12894 object_start,
12895 /// the parser read `}` and finished processing a JSON object
12896 object_end,
12897 /// the parser read `[` and started to process a JSON array
12898 array_start,
12899 /// the parser read `]` and finished processing a JSON array
12900 array_end,
12901 /// the parser read a key of a value in an object
12902 key,
12903 /// the parser finished reading a JSON value
12904 value
12905 };
12906
12907 template<typename BasicJsonType>
12908 using parser_callback_t =
12909 std::function<bool(int /*depth*/, parse_event_t /*event*/, BasicJsonType& /*parsed*/)>;
12910
12911 /*!
12912 @brief syntax analysis
12913
12914 This class implements a recursive descent parser.
12915 */
12916 template<typename BasicJsonType, typename InputAdapterType>
12917 class parser
12918 {
12919 using number_integer_t = typename BasicJsonType::number_integer_t;
12920 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
12921 using number_float_t = typename BasicJsonType::number_float_t;
12922 using string_t = typename BasicJsonType::string_t;
12923 using lexer_t = lexer<BasicJsonType, InputAdapterType>;
12924 using token_type = typename lexer_t::token_type;
12925
12926 public:
12927 /// a parser reading from an input adapter
12928 explicit parser(InputAdapterType&& adapter,
12929 parser_callback_t<BasicJsonType> cb = nullptr,
12930 const bool allow_exceptions_ = true,
12931 const bool skip_comments = false)
12932 : callback(std::move(cb))
12933 , m_lexer(std::move(adapter), skip_comments)
12934 , allow_exceptions(allow_exceptions_)
12935 {
12936 // read first token
12937 get_token();
12938 }
12939
12940 /*!
12941 @brief public parser interface
12942
12943 @param[in] strict whether to expect the last token to be EOF
12944 @param[in,out] result parsed JSON value
12945
12946 @throw parse_error.101 in case of an unexpected token
12947 @throw parse_error.102 if to_unicode fails or surrogate error
12948 @throw parse_error.103 if to_unicode fails
12949 */
12950 void parse(const bool strict, BasicJsonType& result)
12951 {
12952 if (callback)
12953 {
12954 json_sax_dom_callback_parser<BasicJsonType, InputAdapterType> sdp(result, callback, allow_exceptions, &m_lexer);
12955 sax_parse_internal(&sdp);
12956
12957 // in strict mode, input must be completely read
12958 if (strict && (get_token() != token_type::end_of_input))
12959 {
12960 sdp.parse_error(m_lexer.get_position(),
12961 m_lexer.get_token_string(),
12962 parse_error::create(101, m_lexer.get_position(),
12963 exception_message(token_type::end_of_input, "value"), nullptr));
12964 }
12965
12966 // in case of an error, return discarded value
12967 if (sdp.is_errored())
12968 {
12969 result = value_t::discarded;
12970 return;
12971 }
12972
12973 // set top-level value to null if it was discarded by the callback
12974 // function
12975 if (result.is_discarded())
12976 {
12977 result = nullptr;
12978 }
12979 }
12980 else
12981 {
12982 json_sax_dom_parser<BasicJsonType, InputAdapterType> sdp(result, allow_exceptions, &m_lexer);
12983 sax_parse_internal(&sdp);
12984
12985 // in strict mode, input must be completely read
12986 if (strict && (get_token() != token_type::end_of_input))
12987 {
12988 sdp.parse_error(m_lexer.get_position(),
12989 m_lexer.get_token_string(),
12990 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), nullptr));
12991 }
12992
12993 // in case of an error, return discarded value
12994 if (sdp.is_errored())
12995 {
12996 result = value_t::discarded;
12997 return;
12998 }
12999 }
13000
13001 result.assert_invariant();
13002 }
13003
13004 /*!
13005 @brief public accept interface
13006
13007 @param[in] strict whether to expect the last token to be EOF
13008 @return whether the input is a proper JSON text
13009 */
13010 bool accept(const bool strict = true)
13011 {
13012 json_sax_acceptor<BasicJsonType> sax_acceptor;
13013 return sax_parse(&sax_acceptor, strict);
13014 }
13015
13016 template<typename SAX>
13017 JSON_HEDLEY_NON_NULL(2)
13018 bool sax_parse(SAX* sax, const bool strict = true)
13019 {
13020 (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
13021 const bool result = sax_parse_internal(sax);
13022
13023 // strict mode: next byte must be EOF
13024 if (result && strict && (get_token() != token_type::end_of_input))
13025 {
13026 return sax->parse_error(m_lexer.get_position(),
13027 m_lexer.get_token_string(),
13028 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), nullptr));
13029 }
13030
13031 return result;
13032 }
13033
13034 private:
13035 template<typename SAX>
13036 JSON_HEDLEY_NON_NULL(2)
13037 bool sax_parse_internal(SAX* sax)
13038 {
13039 // stack to remember the hierarchy of structured values we are parsing
13040 // true = array; false = object
13041 std::vector<bool> states;
13042 // value to avoid a goto (see comment where set to true)
13043 bool skip_to_state_evaluation = false;
13044
13045 while (true)
13046 {
13047 if (!skip_to_state_evaluation)
13048 {
13049 // invariant: get_token() was called before each iteration
13050 switch (last_token)
13051 {
13052 case token_type::begin_object:
13053 {
13054 if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
13055 {
13056 return false;
13057 }
13058
13059 // closing } -> we are done
13060 if (get_token() == token_type::end_object)
13061 {
13062 if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
13063 {
13064 return false;
13065 }
13066 break;
13067 }
13068
13069 // parse key
13070 if (JSON_HEDLEY_UNLIKELY(last_token != token_type::value_string))
13071 {
13072 return sax->parse_error(m_lexer.get_position(),
13073 m_lexer.get_token_string(),
13074 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), nullptr));
13075 }
13076 if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
13077 {
13078 return false;
13079 }
13080
13081 // parse separator (:)
13082 if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
13083 {
13084 return sax->parse_error(m_lexer.get_position(),
13085 m_lexer.get_token_string(),
13086 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), nullptr));
13087 }
13088
13089 // remember we are now inside an object
13090 states.push_back(false);
13091
13092 // parse values
13093 get_token();
13094 continue;
13095 }
13096
13097 case token_type::begin_array:
13098 {
13099 if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
13100 {
13101 return false;
13102 }
13103
13104 // closing ] -> we are done
13105 if (get_token() == token_type::end_array)
13106 {
13107 if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
13108 {
13109 return false;
13110 }
13111 break;
13112 }
13113
13114 // remember we are now inside an array
13115 states.push_back(true);
13116
13117 // parse values (no need to call get_token)
13118 continue;
13119 }
13120
13121 case token_type::value_float:
13122 {
13123 const auto res = m_lexer.get_number_float();
13124
13125 if (JSON_HEDLEY_UNLIKELY(!std::isfinite(res)))
13126 {
13127 return sax->parse_error(m_lexer.get_position(),
13128 m_lexer.get_token_string(),
13129 out_of_range::create(406, concat("number overflow parsing '", m_lexer.get_token_string(), '\''), nullptr));
13130 }
13131
13132 if (JSON_HEDLEY_UNLIKELY(!sax->number_float(res, m_lexer.get_string())))
13133 {
13134 return false;
13135 }
13136
13137 break;
13138 }
13139
13140 case token_type::literal_false:
13141 {
13142 if (JSON_HEDLEY_UNLIKELY(!sax->boolean(false)))
13143 {
13144 return false;
13145 }
13146 break;
13147 }
13148
13149 case token_type::literal_null:
13150 {
13151 if (JSON_HEDLEY_UNLIKELY(!sax->null()))
13152 {
13153 return false;
13154 }
13155 break;
13156 }
13157
13158 case token_type::literal_true:
13159 {
13160 if (JSON_HEDLEY_UNLIKELY(!sax->boolean(true)))
13161 {
13162 return false;
13163 }
13164 break;
13165 }
13166
13167 case token_type::value_integer:
13168 {
13169 if (JSON_HEDLEY_UNLIKELY(!sax->number_integer(m_lexer.get_number_integer())))
13170 {
13171 return false;
13172 }
13173 break;
13174 }
13175
13176 case token_type::value_string:
13177 {
13178 if (JSON_HEDLEY_UNLIKELY(!sax->string(m_lexer.get_string())))
13179 {
13180 return false;
13181 }
13182 break;
13183 }
13184
13185 case token_type::value_unsigned:
13186 {
13187 if (JSON_HEDLEY_UNLIKELY(!sax->number_unsigned(m_lexer.get_number_unsigned())))
13188 {
13189 return false;
13190 }
13191 break;
13192 }
13193
13194 case token_type::parse_error:
13195 {
13196 // using "uninitialized" to avoid "expected" message
13197 return sax->parse_error(m_lexer.get_position(),
13198 m_lexer.get_token_string(),
13199 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::uninitialized, "value"), nullptr));
13200 }
13201 case token_type::end_of_input:
13202 {
13203 if (JSON_HEDLEY_UNLIKELY(m_lexer.get_position().chars_read_total == 1))
13204 {
13205 return sax->parse_error(m_lexer.get_position(),
13206 m_lexer.get_token_string(),
13207 parse_error::create(101, m_lexer.get_position(),
13208 "attempting to parse an empty input; check that your input string or stream contains the expected JSON", nullptr));
13209 }
13210
13211 return sax->parse_error(m_lexer.get_position(),
13212 m_lexer.get_token_string(),
13213 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::literal_or_value, "value"), nullptr));
13214 }
13215 case token_type::uninitialized:
13216 case token_type::end_array:
13217 case token_type::end_object:
13218 case token_type::name_separator:
13219 case token_type::value_separator:
13220 case token_type::literal_or_value:
13221 default: // the last token was unexpected
13222 {
13223 return sax->parse_error(m_lexer.get_position(),
13224 m_lexer.get_token_string(),
13225 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::literal_or_value, "value"), nullptr));
13226 }
13227 }
13228 }
13229 else
13230 {
13231 skip_to_state_evaluation = false;
13232 }
13233
13234 // we reached this line after we successfully parsed a value
13235 if (states.empty())
13236 {
13237 // empty stack: we reached the end of the hierarchy: done
13238 return true;
13239 }
13240
13241 if (states.back()) // array
13242 {
13243 // comma -> next value
13244 if (get_token() == token_type::value_separator)
13245 {
13246 // parse a new value
13247 get_token();
13248 continue;
13249 }
13250
13251 // closing ]
13252 if (JSON_HEDLEY_LIKELY(last_token == token_type::end_array))
13253 {
13254 if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
13255 {
13256 return false;
13257 }
13258
13259 // We are done with this array. Before we can parse a
13260 // new value, we need to evaluate the new state first.
13261 // By setting skip_to_state_evaluation to false, we
13262 // are effectively jumping to the beginning of this if.
13263 JSON_ASSERT(!states.empty());
13264 states.pop_back();
13265 skip_to_state_evaluation = true;
13266 continue;
13267 }
13268
13269 return sax->parse_error(m_lexer.get_position(),
13270 m_lexer.get_token_string(),
13271 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_array, "array"), nullptr));
13272 }
13273
13274 // states.back() is false -> object
13275
13276 // comma -> next value
13277 if (get_token() == token_type::value_separator)
13278 {
13279 // parse key
13280 if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::value_string))
13281 {
13282 return sax->parse_error(m_lexer.get_position(),
13283 m_lexer.get_token_string(),
13284 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), nullptr));
13285 }
13286
13287 if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
13288 {
13289 return false;
13290 }
13291
13292 // parse separator (:)
13293 if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
13294 {
13295 return sax->parse_error(m_lexer.get_position(),
13296 m_lexer.get_token_string(),
13297 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), nullptr));
13298 }
13299
13300 // parse values
13301 get_token();
13302 continue;
13303 }
13304
13305 // closing }
13306 if (JSON_HEDLEY_LIKELY(last_token == token_type::end_object))
13307 {
13308 if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
13309 {
13310 return false;
13311 }
13312
13313 // We are done with this object. Before we can parse a
13314 // new value, we need to evaluate the new state first.
13315 // By setting skip_to_state_evaluation to false, we
13316 // are effectively jumping to the beginning of this if.
13317 JSON_ASSERT(!states.empty());
13318 states.pop_back();
13319 skip_to_state_evaluation = true;
13320 continue;
13321 }
13322
13323 return sax->parse_error(m_lexer.get_position(),
13324 m_lexer.get_token_string(),
13325 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_object, "object"), nullptr));
13326 }
13327 }
13328
13329 /// get next token from lexer
13330 token_type get_token()
13331 {
13332 return last_token = m_lexer.scan();
13333 }
13334
13335 std::string exception_message(const token_type expected, const std::string& context)
13336 {
13337 std::string error_msg = "syntax error ";
13338
13339 if (!context.empty())
13340 {
13341 error_msg += concat("while parsing ", context, ' ');
13342 }
13343
13344 error_msg += "- ";
13345
13346 if (last_token == token_type::parse_error)
13347 {
13348 error_msg += concat(m_lexer.get_error_message(), "; last read: '",
13349 m_lexer.get_token_string(), '\'');
13350 }
13351 else
13352 {
13353 error_msg += concat("unexpected ", lexer_t::token_type_name(last_token));
13354 }
13355
13356 if (expected != token_type::uninitialized)
13357 {
13358 error_msg += concat("; expected ", lexer_t::token_type_name(expected));
13359 }
13360
13361 return error_msg;
13362 }
13363
13364 private:
13365 /// callback function
13366 const parser_callback_t<BasicJsonType> callback = nullptr;
13367 /// the type of the last read token
13368 token_type last_token = token_type::uninitialized;
13369 /// the lexer
13370 lexer_t m_lexer;
13371 /// whether to throw exceptions in case of errors
13372 const bool allow_exceptions = true;
13373 };
13374
13375 } // namespace detail
13376 NLOHMANN_JSON_NAMESPACE_END
13377
13378 // #include <nlohmann/detail/iterators/internal_iterator.hpp>
13379 // __ _____ _____ _____
13380 // __| | __| | | | JSON for Modern C++
13381 // | | |__ | | | | | | version 3.12.0
13382 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
13383 //
13384 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
13385 // SPDX-License-Identifier: MIT
13386
13387
13388
13389 // #include <nlohmann/detail/abi_macros.hpp>
13390
13391 // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
13392 // __ _____ _____ _____
13393 // __| | __| | | | JSON for Modern C++
13394 // | | |__ | | | | | | version 3.12.0
13395 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
13396 //
13397 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
13398 // SPDX-License-Identifier: MIT
13399
13400
13401
13402 #include <cstddef> // ptrdiff_t
13403 #include <limits> // numeric_limits
13404
13405 // #include <nlohmann/detail/macro_scope.hpp>
13406
13407
13408 NLOHMANN_JSON_NAMESPACE_BEGIN
13409 namespace detail
13410 {
13411
13412 /*
13413 @brief an iterator for primitive JSON types
13414
13415 This class models an iterator for primitive JSON types (boolean, number,
13416 string). It's only purpose is to allow the iterator/const_iterator classes
13417 to "iterate" over primitive values. Internally, the iterator is modeled by
13418 a `difference_type` variable. Value begin_value (`0`) models the begin,
13419 end_value (`1`) models past the end.
13420 */
13421 class primitive_iterator_t
13422 {
13423 private:
13424 using difference_type = std::ptrdiff_t;
13425 static constexpr difference_type begin_value = 0;
13426 static constexpr difference_type end_value = begin_value + 1;
13427
13428 JSON_PRIVATE_UNLESS_TESTED:
13429 /// iterator as signed integer type
13430 difference_type m_it = (std::numeric_limits<std::ptrdiff_t>::min)();
13431
13432 public:
13433 constexpr difference_type get_value() const noexcept
13434 {
13435 return m_it;
13436 }
13437
13438 /// set iterator to a defined beginning
13439 void set_begin() noexcept
13440 {
13441 m_it = begin_value;
13442 }
13443
13444 /// set iterator to a defined past the end
13445 void set_end() noexcept
13446 {
13447 m_it = end_value;
13448 }
13449
13450 /// return whether the iterator can be dereferenced
13451 constexpr bool is_begin() const noexcept
13452 {
13453 return m_it == begin_value;
13454 }
13455
13456 /// return whether the iterator is at end
13457 constexpr bool is_end() const noexcept
13458 {
13459 return m_it == end_value;
13460 }
13461
13462 friend constexpr bool operator==(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
13463 {
13464 return lhs.m_it == rhs.m_it;
13465 }
13466
13467 friend constexpr bool operator<(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
13468 {
13469 return lhs.m_it < rhs.m_it;
13470 }
13471
13472 primitive_iterator_t operator+(difference_type n) noexcept
13473 {
13474 auto result = *this;
13475 result += n;
13476 return result;
13477 }
13478
13479 friend constexpr difference_type operator-(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
13480 {
13481 return lhs.m_it - rhs.m_it;
13482 }
13483
13484 primitive_iterator_t& operator++() noexcept
13485 {
13486 ++m_it;
13487 return *this;
13488 }
13489
13490 primitive_iterator_t operator++(int)& noexcept // NOLINT(cert-dcl21-cpp)
13491 {
13492 auto result = *this;
13493 ++m_it;
13494 return result;
13495 }
13496
13497 primitive_iterator_t& operator--() noexcept
13498 {
13499 --m_it;
13500 return *this;
13501 }
13502
13503 primitive_iterator_t operator--(int)& noexcept // NOLINT(cert-dcl21-cpp)
13504 {
13505 auto result = *this;
13506 --m_it;
13507 return result;
13508 }
13509
13510 primitive_iterator_t& operator+=(difference_type n) noexcept
13511 {
13512 m_it += n;
13513 return *this;
13514 }
13515
13516 primitive_iterator_t& operator-=(difference_type n) noexcept
13517 {
13518 m_it -= n;
13519 return *this;
13520 }
13521 };
13522
13523 } // namespace detail
13524 NLOHMANN_JSON_NAMESPACE_END
13525
13526
13527 NLOHMANN_JSON_NAMESPACE_BEGIN
13528 namespace detail
13529 {
13530
13531 /*!
13532 @brief an iterator value
13533
13534 @note This structure could easily be a union, but MSVC currently does not allow
13535 unions members with complex constructors, see https://github.com/nlohmann/json/pull/105.
13536 */
13537 template<typename BasicJsonType> struct internal_iterator
13538 {
13539 /// iterator for JSON objects
13540 typename BasicJsonType::object_t::iterator object_iterator {};
13541 /// iterator for JSON arrays
13542 typename BasicJsonType::array_t::iterator array_iterator {};
13543 /// generic iterator for all other types
13544 primitive_iterator_t primitive_iterator {};
13545 };
13546
13547 } // namespace detail
13548 NLOHMANN_JSON_NAMESPACE_END
13549
13550 // #include <nlohmann/detail/iterators/iter_impl.hpp>
13551 // __ _____ _____ _____
13552 // __| | __| | | | JSON for Modern C++
13553 // | | |__ | | | | | | version 3.12.0
13554 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
13555 //
13556 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
13557 // SPDX-License-Identifier: MIT
13558
13559
13560
13561 #include <iterator> // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next
13562 #include <type_traits> // conditional, is_const, remove_const
13563
13564 // #include <nlohmann/detail/exceptions.hpp>
13565
13566 // #include <nlohmann/detail/iterators/internal_iterator.hpp>
13567
13568 // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
13569
13570 // #include <nlohmann/detail/macro_scope.hpp>
13571
13572 // #include <nlohmann/detail/meta/cpp_future.hpp>
13573
13574 // #include <nlohmann/detail/meta/type_traits.hpp>
13575
13576 // #include <nlohmann/detail/value_t.hpp>
13577
13578
13579 NLOHMANN_JSON_NAMESPACE_BEGIN
13580 namespace detail
13581 {
13582
13583 // forward declare, to be able to friend it later on
13584 template<typename IteratorType> class iteration_proxy;
13585 template<typename IteratorType> class iteration_proxy_value;
13586
13587 /*!
13588 @brief a template for a bidirectional iterator for the @ref basic_json class
13589 This class implements a both iterators (iterator and const_iterator) for the
13590 @ref basic_json class.
13591 @note An iterator is called *initialized* when a pointer to a JSON value has
13592 been set (e.g., by a constructor or a copy assignment). If the iterator is
13593 default-constructed, it is *uninitialized* and most methods are undefined.
13594 **The library uses assertions to detect calls on uninitialized iterators.**
13595 @requirement The class satisfies the following concept requirements:
13596 -
13597 [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
13598 The iterator that can be moved can be moved in both directions (i.e.
13599 incremented and decremented).
13600 @since version 1.0.0, simplified in version 2.0.9, change to bidirectional
13601 iterators in version 3.0.0 (see https://github.com/nlohmann/json/issues/593)
13602 */
13603 template<typename BasicJsonType>
13604 class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions)
13605 {
13606 /// the iterator with BasicJsonType of different const-ness
13607 using other_iter_impl = iter_impl<typename std::conditional<std::is_const<BasicJsonType>::value, typename std::remove_const<BasicJsonType>::type, const BasicJsonType>::type>;
13608 /// allow basic_json to access private members
13609 friend other_iter_impl;
13610 friend BasicJsonType;
13611 friend iteration_proxy<iter_impl>;
13612 friend iteration_proxy_value<iter_impl>;
13613
13614 using object_t = typename BasicJsonType::object_t;
13615 using array_t = typename BasicJsonType::array_t;
13616 // make sure BasicJsonType is basic_json or const basic_json
13617 static_assert(is_basic_json<typename std::remove_const<BasicJsonType>::type>::value,
13618 "iter_impl only accepts (const) basic_json");
13619 // superficial check for the LegacyBidirectionalIterator named requirement
13620 static_assert(std::is_base_of<std::bidirectional_iterator_tag, std::bidirectional_iterator_tag>::value
13621 && std::is_base_of<std::bidirectional_iterator_tag, typename std::iterator_traits<typename array_t::iterator>::iterator_category>::value,
13622 "basic_json iterator assumes array and object type iterators satisfy the LegacyBidirectionalIterator named requirement.");
13623
13624 public:
13625 /// The std::iterator class template (used as a base class to provide typedefs) is deprecated in C++17.
13626 /// The C++ Standard has never required user-defined iterators to derive from std::iterator.
13627 /// A user-defined iterator should provide publicly accessible typedefs named
13628 /// iterator_category, value_type, difference_type, pointer, and reference.
13629 /// Note that value_type is required to be non-const, even for constant iterators.
13630 using iterator_category = std::bidirectional_iterator_tag;
13631
13632 /// the type of the values when the iterator is dereferenced
13633 using value_type = typename BasicJsonType::value_type;
13634 /// a type to represent differences between iterators
13635 using difference_type = typename BasicJsonType::difference_type;
13636 /// defines a pointer to the type iterated over (value_type)
13637 using pointer = typename std::conditional<std::is_const<BasicJsonType>::value,
13638 typename BasicJsonType::const_pointer,
13639 typename BasicJsonType::pointer>::type;
13640 /// defines a reference to the type iterated over (value_type)
13641 using reference =
13642 typename std::conditional<std::is_const<BasicJsonType>::value,
13643 typename BasicJsonType::const_reference,
13644 typename BasicJsonType::reference>::type;
13645
13646 iter_impl() = default;
13647 ~iter_impl() = default;
13648 iter_impl(iter_impl&&) noexcept = default;
13649 iter_impl& operator=(iter_impl&&) noexcept = default;
13650
13651 /*!
13652 @brief constructor for a given JSON instance
13653 @param[in] object pointer to a JSON object for this iterator
13654 @pre object != nullptr
13655 @post The iterator is initialized; i.e. `m_object != nullptr`.
13656 */
13657 explicit iter_impl(pointer object) noexcept : m_object(object)
13658 {
13659 JSON_ASSERT(m_object != nullptr);
13660
13661 switch (m_object->m_data.m_type)
13662 {
13663 case value_t::object:
13664 {
13665 m_it.object_iterator = typename object_t::iterator();
13666 break;
13667 }
13668
13669 case value_t::array:
13670 {
13671 m_it.array_iterator = typename array_t::iterator();
13672 break;
13673 }
13674
13675 case value_t::null:
13676 case value_t::string:
13677 case value_t::boolean:
13678 case value_t::number_integer:
13679 case value_t::number_unsigned:
13680 case value_t::number_float:
13681 case value_t::binary:
13682 case value_t::discarded:
13683 default:
13684 {
13685 m_it.primitive_iterator = primitive_iterator_t();
13686 break;
13687 }
13688 }
13689 }
13690
13691 /*!
13692 @note The conventional copy constructor and copy assignment are implicitly
13693 defined. Combined with the following converting constructor and
13694 assignment, they support: (1) copy from iterator to iterator, (2)
13695 copy from const iterator to const iterator, and (3) conversion from
13696 iterator to const iterator. However conversion from const iterator
13697 to iterator is not defined.
13698 */
13699
13700 /*!
13701 @brief const copy constructor
13702 @param[in] other const iterator to copy from
13703 @note This copy constructor had to be defined explicitly to circumvent a bug
13704 occurring on msvc v19.0 compiler (VS 2015) debug build. For more
13705 information refer to: https://github.com/nlohmann/json/issues/1608
13706 */
13707 iter_impl(const iter_impl<const BasicJsonType>& other) noexcept
13708 : m_object(other.m_object), m_it(other.m_it)
13709 {}
13710
13711 /*!
13712 @brief converting assignment
13713 @param[in] other const iterator to copy from
13714 @return const/non-const iterator
13715 @note It is not checked whether @a other is initialized.
13716 */
13717 iter_impl& operator=(const iter_impl<const BasicJsonType>& other) noexcept
13718 {
13719 if (&other != this)
13720 {
13721 m_object = other.m_object;
13722 m_it = other.m_it;
13723 }
13724 return *this;
13725 }
13726
13727 /*!
13728 @brief converting constructor
13729 @param[in] other non-const iterator to copy from
13730 @note It is not checked whether @a other is initialized.
13731 */
13732 iter_impl(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept
13733 : m_object(other.m_object), m_it(other.m_it)
13734 {}
13735
13736 /*!
13737 @brief converting assignment
13738 @param[in] other non-const iterator to copy from
13739 @return const/non-const iterator
13740 @note It is not checked whether @a other is initialized.
13741 */
13742 iter_impl& operator=(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept // NOLINT(cert-oop54-cpp)
13743 {
13744 m_object = other.m_object;
13745 m_it = other.m_it;
13746 return *this;
13747 }
13748
13749 JSON_PRIVATE_UNLESS_TESTED:
13750 /*!
13751 @brief set the iterator to the first value
13752 @pre The iterator is initialized; i.e. `m_object != nullptr`.
13753 */
13754 void set_begin() noexcept
13755 {
13756 JSON_ASSERT(m_object != nullptr);
13757
13758 switch (m_object->m_data.m_type)
13759 {
13760 case value_t::object:
13761 {
13762 m_it.object_iterator = m_object->m_data.m_value.object->begin();
13763 break;
13764 }
13765
13766 case value_t::array:
13767 {
13768 m_it.array_iterator = m_object->m_data.m_value.array->begin();
13769 break;
13770 }
13771
13772 case value_t::null:
13773 {
13774 // set to end so begin()==end() is true: null is empty
13775 m_it.primitive_iterator.set_end();
13776 break;
13777 }
13778
13779 case value_t::string:
13780 case value_t::boolean:
13781 case value_t::number_integer:
13782 case value_t::number_unsigned:
13783 case value_t::number_float:
13784 case value_t::binary:
13785 case value_t::discarded:
13786 default:
13787 {
13788 m_it.primitive_iterator.set_begin();
13789 break;
13790 }
13791 }
13792 }
13793
13794 /*!
13795 @brief set the iterator past the last value
13796 @pre The iterator is initialized; i.e. `m_object != nullptr`.
13797 */
13798 void set_end() noexcept
13799 {
13800 JSON_ASSERT(m_object != nullptr);
13801
13802 switch (m_object->m_data.m_type)
13803 {
13804 case value_t::object:
13805 {
13806 m_it.object_iterator = m_object->m_data.m_value.object->end();
13807 break;
13808 }
13809
13810 case value_t::array:
13811 {
13812 m_it.array_iterator = m_object->m_data.m_value.array->end();
13813 break;
13814 }
13815
13816 case value_t::null:
13817 case value_t::string:
13818 case value_t::boolean:
13819 case value_t::number_integer:
13820 case value_t::number_unsigned:
13821 case value_t::number_float:
13822 case value_t::binary:
13823 case value_t::discarded:
13824 default:
13825 {
13826 m_it.primitive_iterator.set_end();
13827 break;
13828 }
13829 }
13830 }
13831
13832 public:
13833 /*!
13834 @brief return a reference to the value pointed to by the iterator
13835 @pre The iterator is initialized; i.e. `m_object != nullptr`.
13836 */
13837 reference operator*() const
13838 {
13839 JSON_ASSERT(m_object != nullptr);
13840
13841 switch (m_object->m_data.m_type)
13842 {
13843 case value_t::object:
13844 {
13845 JSON_ASSERT(m_it.object_iterator != m_object->m_data.m_value.object->end());
13846 return m_it.object_iterator->second;
13847 }
13848
13849 case value_t::array:
13850 {
13851 JSON_ASSERT(m_it.array_iterator != m_object->m_data.m_value.array->end());
13852 return *m_it.array_iterator;
13853 }
13854
13855 case value_t::null:
13856 JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
13857
13858 case value_t::string:
13859 case value_t::boolean:
13860 case value_t::number_integer:
13861 case value_t::number_unsigned:
13862 case value_t::number_float:
13863 case value_t::binary:
13864 case value_t::discarded:
13865 default:
13866 {
13867 if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
13868 {
13869 return *m_object;
13870 }
13871
13872 JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
13873 }
13874 }
13875 }
13876
13877 /*!
13878 @brief dereference the iterator
13879 @pre The iterator is initialized; i.e. `m_object != nullptr`.
13880 */
13881 pointer operator->() const
13882 {
13883 JSON_ASSERT(m_object != nullptr);
13884
13885 switch (m_object->m_data.m_type)
13886 {
13887 case value_t::object:
13888 {
13889 JSON_ASSERT(m_it.object_iterator != m_object->m_data.m_value.object->end());
13890 return &(m_it.object_iterator->second);
13891 }
13892
13893 case value_t::array:
13894 {
13895 JSON_ASSERT(m_it.array_iterator != m_object->m_data.m_value.array->end());
13896 return &*m_it.array_iterator;
13897 }
13898
13899 case value_t::null:
13900 case value_t::string:
13901 case value_t::boolean:
13902 case value_t::number_integer:
13903 case value_t::number_unsigned:
13904 case value_t::number_float:
13905 case value_t::binary:
13906 case value_t::discarded:
13907 default:
13908 {
13909 if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
13910 {
13911 return m_object;
13912 }
13913
13914 JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
13915 }
13916 }
13917 }
13918
13919 /*!
13920 @brief post-increment (it++)
13921 @pre The iterator is initialized; i.e. `m_object != nullptr`.
13922 */
13923 iter_impl operator++(int)& // NOLINT(cert-dcl21-cpp)
13924 {
13925 auto result = *this;
13926 ++(*this);
13927 return result;
13928 }
13929
13930 /*!
13931 @brief pre-increment (++it)
13932 @pre The iterator is initialized; i.e. `m_object != nullptr`.
13933 */
13934 iter_impl& operator++()
13935 {
13936 JSON_ASSERT(m_object != nullptr);
13937
13938 switch (m_object->m_data.m_type)
13939 {
13940 case value_t::object:
13941 {
13942 std::advance(m_it.object_iterator, 1);
13943 break;
13944 }
13945
13946 case value_t::array:
13947 {
13948 std::advance(m_it.array_iterator, 1);
13949 break;
13950 }
13951
13952 case value_t::null:
13953 case value_t::string:
13954 case value_t::boolean:
13955 case value_t::number_integer:
13956 case value_t::number_unsigned:
13957 case value_t::number_float:
13958 case value_t::binary:
13959 case value_t::discarded:
13960 default:
13961 {
13962 ++m_it.primitive_iterator;
13963 break;
13964 }
13965 }
13966
13967 return *this;
13968 }
13969
13970 /*!
13971 @brief post-decrement (it--)
13972 @pre The iterator is initialized; i.e. `m_object != nullptr`.
13973 */
13974 iter_impl operator--(int)& // NOLINT(cert-dcl21-cpp)
13975 {
13976 auto result = *this;
13977 --(*this);
13978 return result;
13979 }
13980
13981 /*!
13982 @brief pre-decrement (--it)
13983 @pre The iterator is initialized; i.e. `m_object != nullptr`.
13984 */
13985 iter_impl& operator--()
13986 {
13987 JSON_ASSERT(m_object != nullptr);
13988
13989 switch (m_object->m_data.m_type)
13990 {
13991 case value_t::object:
13992 {
13993 std::advance(m_it.object_iterator, -1);
13994 break;
13995 }
13996
13997 case value_t::array:
13998 {
13999 std::advance(m_it.array_iterator, -1);
14000 break;
14001 }
14002
14003 case value_t::null:
14004 case value_t::string:
14005 case value_t::boolean:
14006 case value_t::number_integer:
14007 case value_t::number_unsigned:
14008 case value_t::number_float:
14009 case value_t::binary:
14010 case value_t::discarded:
14011 default:
14012 {
14013 --m_it.primitive_iterator;
14014 break;
14015 }
14016 }
14017
14018 return *this;
14019 }
14020
14021 /*!
14022 @brief comparison: equal
14023 @pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
14024 */
14025 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 >
14026 bool operator==(const IterImpl& other) const
14027 {
14028 // if objects are not the same, the comparison is undefined
14029 if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
14030 {
14031 JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object));
14032 }
14033
14034 // value-initialized forward iterators can be compared, and must compare equal to other value-initialized iterators of the same type #4493
14035 if (m_object == nullptr)
14036 {
14037 return true;
14038 }
14039
14040 switch (m_object->m_data.m_type)
14041 {
14042 case value_t::object:
14043 return (m_it.object_iterator == other.m_it.object_iterator);
14044
14045 case value_t::array:
14046 return (m_it.array_iterator == other.m_it.array_iterator);
14047
14048 case value_t::null:
14049 case value_t::string:
14050 case value_t::boolean:
14051 case value_t::number_integer:
14052 case value_t::number_unsigned:
14053 case value_t::number_float:
14054 case value_t::binary:
14055 case value_t::discarded:
14056 default:
14057 return (m_it.primitive_iterator == other.m_it.primitive_iterator);
14058 }
14059 }
14060
14061 /*!
14062 @brief comparison: not 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 return !operator==(other);
14069 }
14070
14071 /*!
14072 @brief comparison: smaller
14073 @pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
14074 */
14075 bool operator<(const iter_impl& other) const
14076 {
14077 // if objects are not the same, the comparison is undefined
14078 if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
14079 {
14080 JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object));
14081 }
14082
14083 // value-initialized forward iterators can be compared, and must compare equal to other value-initialized iterators of the same type #4493
14084 if (m_object == nullptr)
14085 {
14086 // the iterators are both value-initialized and are to be considered equal, but this function checks for smaller, so we return false
14087 return false;
14088 }
14089
14090 switch (m_object->m_data.m_type)
14091 {
14092 case value_t::object:
14093 JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators", m_object));
14094
14095 case value_t::array:
14096 return (m_it.array_iterator < other.m_it.array_iterator);
14097
14098 case value_t::null:
14099 case value_t::string:
14100 case value_t::boolean:
14101 case value_t::number_integer:
14102 case value_t::number_unsigned:
14103 case value_t::number_float:
14104 case value_t::binary:
14105 case value_t::discarded:
14106 default:
14107 return (m_it.primitive_iterator < other.m_it.primitive_iterator);
14108 }
14109 }
14110
14111 /*!
14112 @brief comparison: less than or equal
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 return !other.operator < (*this);
14118 }
14119
14120 /*!
14121 @brief comparison: greater than
14122 @pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
14123 */
14124 bool operator>(const iter_impl& other) const
14125 {
14126 return !operator<=(other);
14127 }
14128
14129 /*!
14130 @brief comparison: greater than or equal
14131 @pre (1) The iterator is initialized; i.e. `m_object != nullptr`, or (2) both iterators are value-initialized.
14132 */
14133 bool operator>=(const iter_impl& other) const
14134 {
14135 return !operator<(other);
14136 }
14137
14138 /*!
14139 @brief add to iterator
14140 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14141 */
14142 iter_impl& operator+=(difference_type i)
14143 {
14144 JSON_ASSERT(m_object != nullptr);
14145
14146 switch (m_object->m_data.m_type)
14147 {
14148 case value_t::object:
14149 JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", m_object));
14150
14151 case value_t::array:
14152 {
14153 std::advance(m_it.array_iterator, i);
14154 break;
14155 }
14156
14157 case value_t::null:
14158 case value_t::string:
14159 case value_t::boolean:
14160 case value_t::number_integer:
14161 case value_t::number_unsigned:
14162 case value_t::number_float:
14163 case value_t::binary:
14164 case value_t::discarded:
14165 default:
14166 {
14167 m_it.primitive_iterator += i;
14168 break;
14169 }
14170 }
14171
14172 return *this;
14173 }
14174
14175 /*!
14176 @brief subtract from iterator
14177 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14178 */
14179 iter_impl& operator-=(difference_type i)
14180 {
14181 return operator+=(-i);
14182 }
14183
14184 /*!
14185 @brief add to iterator
14186 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14187 */
14188 iter_impl operator+(difference_type i) const
14189 {
14190 auto result = *this;
14191 result += i;
14192 return result;
14193 }
14194
14195 /*!
14196 @brief addition of distance and iterator
14197 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14198 */
14199 friend iter_impl operator+(difference_type i, const iter_impl& it)
14200 {
14201 auto result = it;
14202 result += i;
14203 return result;
14204 }
14205
14206 /*!
14207 @brief subtract from iterator
14208 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14209 */
14210 iter_impl operator-(difference_type i) const
14211 {
14212 auto result = *this;
14213 result -= i;
14214 return result;
14215 }
14216
14217 /*!
14218 @brief return difference
14219 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14220 */
14221 difference_type operator-(const iter_impl& other) const
14222 {
14223 JSON_ASSERT(m_object != nullptr);
14224
14225 switch (m_object->m_data.m_type)
14226 {
14227 case value_t::object:
14228 JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", m_object));
14229
14230 case value_t::array:
14231 return m_it.array_iterator - other.m_it.array_iterator;
14232
14233 case value_t::null:
14234 case value_t::string:
14235 case value_t::boolean:
14236 case value_t::number_integer:
14237 case value_t::number_unsigned:
14238 case value_t::number_float:
14239 case value_t::binary:
14240 case value_t::discarded:
14241 default:
14242 return m_it.primitive_iterator - other.m_it.primitive_iterator;
14243 }
14244 }
14245
14246 /*!
14247 @brief access to successor
14248 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14249 */
14250 reference operator[](difference_type n) const
14251 {
14252 JSON_ASSERT(m_object != nullptr);
14253
14254 switch (m_object->m_data.m_type)
14255 {
14256 case value_t::object:
14257 JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators", m_object));
14258
14259 case value_t::array:
14260 return *std::next(m_it.array_iterator, n);
14261
14262 case value_t::null:
14263 JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
14264
14265 case value_t::string:
14266 case value_t::boolean:
14267 case value_t::number_integer:
14268 case value_t::number_unsigned:
14269 case value_t::number_float:
14270 case value_t::binary:
14271 case value_t::discarded:
14272 default:
14273 {
14274 if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.get_value() == -n))
14275 {
14276 return *m_object;
14277 }
14278
14279 JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
14280 }
14281 }
14282 }
14283
14284 /*!
14285 @brief return the key of an object iterator
14286 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14287 */
14288 const typename object_t::key_type& key() const
14289 {
14290 JSON_ASSERT(m_object != nullptr);
14291
14292 if (JSON_HEDLEY_LIKELY(m_object->is_object()))
14293 {
14294 return m_it.object_iterator->first;
14295 }
14296
14297 JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators", m_object));
14298 }
14299
14300 /*!
14301 @brief return the value of an iterator
14302 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14303 */
14304 reference value() const
14305 {
14306 return operator*();
14307 }
14308
14309 JSON_PRIVATE_UNLESS_TESTED:
14310 /// associated JSON instance
14311 pointer m_object = nullptr;
14312 /// the actual iterator of the associated instance
14313 internal_iterator<typename std::remove_const<BasicJsonType>::type> m_it {};
14314 };
14315
14316 } // namespace detail
14317 NLOHMANN_JSON_NAMESPACE_END
14318
14319 // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
14320
14321 // #include <nlohmann/detail/iterators/json_reverse_iterator.hpp>
14322 // __ _____ _____ _____
14323 // __| | __| | | | JSON for Modern C++
14324 // | | |__ | | | | | | version 3.12.0
14325 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
14326 //
14327 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
14328 // SPDX-License-Identifier: MIT
14329
14330
14331
14332 #include <cstddef> // ptrdiff_t
14333 #include <iterator> // reverse_iterator
14334 #include <utility> // declval
14335
14336 // #include <nlohmann/detail/abi_macros.hpp>
14337
14338
14339 NLOHMANN_JSON_NAMESPACE_BEGIN
14340 namespace detail
14341 {
14342
14343 //////////////////////
14344 // reverse_iterator //
14345 //////////////////////
14346
14347 /*!
14348 @brief a template for a reverse iterator class
14349
14350 @tparam Base the base iterator type to reverse. Valid types are @ref
14351 iterator (to create @ref reverse_iterator) and @ref const_iterator (to
14352 create @ref const_reverse_iterator).
14353
14354 @requirement The class satisfies the following concept requirements:
14355 -
14356 [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
14357 The iterator that can be moved can be moved in both directions (i.e.
14358 incremented and decremented).
14359 - [OutputIterator](https://en.cppreference.com/w/cpp/named_req/OutputIterator):
14360 It is possible to write to the pointed-to element (only if @a Base is
14361 @ref iterator).
14362
14363 @since version 1.0.0
14364 */
14365 template<typename Base>
14366 class json_reverse_iterator : public std::reverse_iterator<Base>
14367 {
14368 public:
14369 using difference_type = std::ptrdiff_t;
14370 /// shortcut to the reverse iterator adapter
14371 using base_iterator = std::reverse_iterator<Base>;
14372 /// the reference type for the pointed-to element
14373 using reference = typename Base::reference;
14374
14375 /// create reverse iterator from iterator
14376 explicit json_reverse_iterator(const typename base_iterator::iterator_type& it) noexcept
14377 : base_iterator(it) {}
14378
14379 /// create reverse iterator from base class
14380 explicit json_reverse_iterator(const base_iterator& it) noexcept : base_iterator(it) {}
14381
14382 /// post-increment (it++)
14383 json_reverse_iterator operator++(int)& // NOLINT(cert-dcl21-cpp)
14384 {
14385 return static_cast<json_reverse_iterator>(base_iterator::operator++(1));
14386 }
14387
14388 /// pre-increment (++it)
14389 json_reverse_iterator& operator++()
14390 {
14391 return static_cast<json_reverse_iterator&>(base_iterator::operator++());
14392 }
14393
14394 /// post-decrement (it--)
14395 json_reverse_iterator operator--(int)& // NOLINT(cert-dcl21-cpp)
14396 {
14397 return static_cast<json_reverse_iterator>(base_iterator::operator--(1));
14398 }
14399
14400 /// pre-decrement (--it)
14401 json_reverse_iterator& operator--()
14402 {
14403 return static_cast<json_reverse_iterator&>(base_iterator::operator--());
14404 }
14405
14406 /// add to iterator
14407 json_reverse_iterator& operator+=(difference_type i)
14408 {
14409 return static_cast<json_reverse_iterator&>(base_iterator::operator+=(i));
14410 }
14411
14412 /// add to iterator
14413 json_reverse_iterator operator+(difference_type i) const
14414 {
14415 return static_cast<json_reverse_iterator>(base_iterator::operator+(i));
14416 }
14417
14418 /// subtract from iterator
14419 json_reverse_iterator operator-(difference_type i) const
14420 {
14421 return static_cast<json_reverse_iterator>(base_iterator::operator-(i));
14422 }
14423
14424 /// return difference
14425 difference_type operator-(const json_reverse_iterator& other) const
14426 {
14427 return base_iterator(*this) - base_iterator(other);
14428 }
14429
14430 /// access to successor
14431 reference operator[](difference_type n) const
14432 {
14433 return *(this->operator+(n));
14434 }
14435
14436 /// return the key of an object iterator
14437 auto key() const -> decltype(std::declval<Base>().key())
14438 {
14439 auto it = --this->base();
14440 return it.key();
14441 }
14442
14443 /// return the value of an iterator
14444 reference value() const
14445 {
14446 auto it = --this->base();
14447 return it.operator * ();
14448 }
14449 };
14450
14451 } // namespace detail
14452 NLOHMANN_JSON_NAMESPACE_END
14453
14454 // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
14455
14456 // #include <nlohmann/detail/json_custom_base_class.hpp>
14457 // __ _____ _____ _____
14458 // __| | __| | | | JSON for Modern C++
14459 // | | |__ | | | | | | version 3.12.0
14460 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
14461 //
14462 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
14463 // SPDX-License-Identifier: MIT
14464
14465
14466
14467 #include <type_traits> // conditional, is_same
14468
14469 // #include <nlohmann/detail/abi_macros.hpp>
14470
14471
14472 NLOHMANN_JSON_NAMESPACE_BEGIN
14473 namespace detail
14474 {
14475
14476 /*!
14477 @brief Default base class of the @ref basic_json class.
14478
14479 So that the correct implementations of the copy / move ctors / assign operators
14480 of @ref basic_json do not require complex case distinctions
14481 (no base class / custom base class used as customization point),
14482 @ref basic_json always has a base class.
14483 By default, this class is used because it is empty and thus has no effect
14484 on the behavior of @ref basic_json.
14485 */
14486 struct json_default_base {};
14487
14488 template<class T>
14489 using json_base_class = typename std::conditional <
14490 std::is_same<T, void>::value,
14491 json_default_base,
14492 T
14493 >::type;
14494
14495 } // namespace detail
14496 NLOHMANN_JSON_NAMESPACE_END
14497
14498 // #include <nlohmann/detail/json_pointer.hpp>
14499 // __ _____ _____ _____
14500 // __| | __| | | | JSON for Modern C++
14501 // | | |__ | | | | | | version 3.12.0
14502 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
14503 //
14504 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
14505 // SPDX-License-Identifier: MIT
14506
14507
14508
14509 #include <algorithm> // all_of
14510 #include <cctype> // isdigit
14511 #include <cerrno> // errno, ERANGE
14512 #include <cstdlib> // strtoull
14513 #ifndef JSON_NO_IO
14514 #include <iosfwd> // ostream
14515 #endif // JSON_NO_IO
14516 #include <limits> // max
14517 #include <numeric> // accumulate
14518 #include <string> // string
14519 #include <utility> // move
14520 #include <vector> // vector
14521
14522 // #include <nlohmann/detail/exceptions.hpp>
14523
14524 // #include <nlohmann/detail/macro_scope.hpp>
14525
14526 // #include <nlohmann/detail/string_concat.hpp>
14527
14528 // #include <nlohmann/detail/string_escape.hpp>
14529
14530 // #include <nlohmann/detail/value_t.hpp>
14531
14532
14533 NLOHMANN_JSON_NAMESPACE_BEGIN
14534
14535 /// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document
14536 /// @sa https://json.nlohmann.me/api/json_pointer/
14537 template<typename RefStringType>
14538 class json_pointer
14539 {
14540 // allow basic_json to access private members
14541 NLOHMANN_BASIC_JSON_TPL_DECLARATION
14542 friend class basic_json;
14543
14544 template<typename>
14545 friend class json_pointer;
14546
14547 template<typename T>
14548 struct string_t_helper
14549 {
14550 using type = T;
14551 };
14552
14553 NLOHMANN_BASIC_JSON_TPL_DECLARATION
14554 struct string_t_helper<NLOHMANN_BASIC_JSON_TPL>
14555 {
14556 using type = StringType;
14557 };
14558
14559 public:
14560 // for backwards compatibility accept BasicJsonType
14561 using string_t = typename string_t_helper<RefStringType>::type;
14562
14563 /// @brief create JSON pointer
14564 /// @sa https://json.nlohmann.me/api/json_pointer/json_pointer/
14565 explicit json_pointer(const string_t& s = "")
14566 : reference_tokens(split(s))
14567 {}
14568
14569 /// @brief return a string representation of the JSON pointer
14570 /// @sa https://json.nlohmann.me/api/json_pointer/to_string/
14571 string_t to_string() const
14572 {
14573 return std::accumulate(reference_tokens.begin(), reference_tokens.end(),
14574 string_t{},
14575 [](const string_t& a, const string_t& b)
14576 {
14577 return detail::concat(a, '/', detail::escape(b));
14578 });
14579 }
14580
14581 /// @brief return a string representation of the JSON pointer
14582 /// @sa https://json.nlohmann.me/api/json_pointer/operator_string/
14583 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, to_string())
14584 operator string_t() const
14585 {
14586 return to_string();
14587 }
14588
14589 #ifndef JSON_NO_IO
14590 /// @brief write string representation of the JSON pointer to stream
14591 /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
14592 friend std::ostream& operator<<(std::ostream& o, const json_pointer& ptr)
14593 {
14594 o << ptr.to_string();
14595 return o;
14596 }
14597 #endif
14598
14599 /// @brief append another JSON pointer at the end of this JSON pointer
14600 /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
14601 json_pointer& operator/=(const json_pointer& ptr)
14602 {
14603 reference_tokens.insert(reference_tokens.end(),
14604 ptr.reference_tokens.begin(),
14605 ptr.reference_tokens.end());
14606 return *this;
14607 }
14608
14609 /// @brief append an unescaped reference token at the end of this JSON pointer
14610 /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
14611 json_pointer& operator/=(string_t token)
14612 {
14613 push_back(std::move(token));
14614 return *this;
14615 }
14616
14617 /// @brief append an array index at the end of this JSON pointer
14618 /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
14619 json_pointer& operator/=(std::size_t array_idx)
14620 {
14621 return *this /= std::to_string(array_idx);
14622 }
14623
14624 /// @brief create a new JSON pointer by appending the right JSON pointer at the end of the left JSON pointer
14625 /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
14626 friend json_pointer operator/(const json_pointer& lhs,
14627 const json_pointer& rhs)
14628 {
14629 return json_pointer(lhs) /= rhs;
14630 }
14631
14632 /// @brief create a new JSON pointer by appending the unescaped token at the end of the JSON pointer
14633 /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
14634 friend json_pointer operator/(const json_pointer& lhs, string_t token) // NOLINT(performance-unnecessary-value-param)
14635 {
14636 return json_pointer(lhs) /= std::move(token);
14637 }
14638
14639 /// @brief create a new JSON pointer by appending the array-index-token at the end of the JSON pointer
14640 /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
14641 friend json_pointer operator/(const json_pointer& lhs, std::size_t array_idx)
14642 {
14643 return json_pointer(lhs) /= array_idx;
14644 }
14645
14646 /// @brief returns the parent of this JSON pointer
14647 /// @sa https://json.nlohmann.me/api/json_pointer/parent_pointer/
14648 json_pointer parent_pointer() const
14649 {
14650 if (empty())
14651 {
14652 return *this;
14653 }
14654
14655 json_pointer res = *this;
14656 res.pop_back();
14657 return res;
14658 }
14659
14660 /// @brief remove last reference token
14661 /// @sa https://json.nlohmann.me/api/json_pointer/pop_back/
14662 void pop_back()
14663 {
14664 if (JSON_HEDLEY_UNLIKELY(empty()))
14665 {
14666 JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
14667 }
14668
14669 reference_tokens.pop_back();
14670 }
14671
14672 /// @brief return last reference token
14673 /// @sa https://json.nlohmann.me/api/json_pointer/back/
14674 const string_t& back() const
14675 {
14676 if (JSON_HEDLEY_UNLIKELY(empty()))
14677 {
14678 JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
14679 }
14680
14681 return reference_tokens.back();
14682 }
14683
14684 /// @brief append an unescaped token at the end of the reference pointer
14685 /// @sa https://json.nlohmann.me/api/json_pointer/push_back/
14686 void push_back(const string_t& token)
14687 {
14688 reference_tokens.push_back(token);
14689 }
14690
14691 /// @brief append an unescaped token at the end of the reference pointer
14692 /// @sa https://json.nlohmann.me/api/json_pointer/push_back/
14693 void push_back(string_t&& token)
14694 {
14695 reference_tokens.push_back(std::move(token));
14696 }
14697
14698 /// @brief return whether pointer points to the root document
14699 /// @sa https://json.nlohmann.me/api/json_pointer/empty/
14700 bool empty() const noexcept
14701 {
14702 return reference_tokens.empty();
14703 }
14704
14705 private:
14706 /*!
14707 @param[in] s reference token to be converted into an array index
14708
14709 @return integer representation of @a s
14710
14711 @throw parse_error.106 if an array index begins with '0'
14712 @throw parse_error.109 if an array index begins not with a digit
14713 @throw out_of_range.404 if string @a s could not be converted to an integer
14714 @throw out_of_range.410 if an array index exceeds size_type
14715 */
14716 template<typename BasicJsonType>
14717 static typename BasicJsonType::size_type array_index(const string_t& s)
14718 {
14719 using size_type = typename BasicJsonType::size_type;
14720
14721 // error condition (cf. RFC 6901, Sect. 4)
14722 if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && s[0] == '0'))
14723 {
14724 JSON_THROW(detail::parse_error::create(106, 0, detail::concat("array index '", s, "' must not begin with '0'"), nullptr));
14725 }
14726
14727 // error condition (cf. RFC 6901, Sect. 4)
14728 if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && !(s[0] >= '1' && s[0] <= '9')))
14729 {
14730 JSON_THROW(detail::parse_error::create(109, 0, detail::concat("array index '", s, "' is not a number"), nullptr));
14731 }
14732
14733 const char* p = s.c_str();
14734 char* p_end = nullptr; // NOLINT(misc-const-correctness)
14735 errno = 0; // strtoull doesn't reset errno
14736 const unsigned long long res = std::strtoull(p, &p_end, 10); // NOLINT(runtime/int)
14737 if (p == p_end // invalid input or empty string
14738 || errno == ERANGE // out of range
14739 || JSON_HEDLEY_UNLIKELY(static_cast<std::size_t>(p_end - p) != s.size())) // incomplete read
14740 {
14741 JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", s, "'"), nullptr));
14742 }
14743
14744 // only triggered on special platforms (like 32bit), see also
14745 // https://github.com/nlohmann/json/pull/2203
14746 if (res >= static_cast<unsigned long long>((std::numeric_limits<size_type>::max)())) // NOLINT(runtime/int)
14747 {
14748 JSON_THROW(detail::out_of_range::create(410, detail::concat("array index ", s, " exceeds size_type"), nullptr)); // LCOV_EXCL_LINE
14749 }
14750
14751 return static_cast<size_type>(res);
14752 }
14753
14754 JSON_PRIVATE_UNLESS_TESTED:
14755 json_pointer top() const
14756 {
14757 if (JSON_HEDLEY_UNLIKELY(empty()))
14758 {
14759 JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
14760 }
14761
14762 json_pointer result = *this;
14763 result.reference_tokens = {reference_tokens[0]};
14764 return result;
14765 }
14766
14767 private:
14768 /*!
14769 @brief create and return a reference to the pointed to value
14770
14771 @complexity Linear in the number of reference tokens.
14772
14773 @throw parse_error.109 if array index is not a number
14774 @throw type_error.313 if value cannot be unflattened
14775 */
14776 template<typename BasicJsonType>
14777 BasicJsonType& get_and_create(BasicJsonType& j) const
14778 {
14779 auto* result = &j;
14780
14781 // in case no reference tokens exist, return a reference to the JSON value
14782 // j which will be overwritten by a primitive value
14783 for (const auto& reference_token : reference_tokens)
14784 {
14785 switch (result->type())
14786 {
14787 case detail::value_t::null:
14788 {
14789 if (reference_token == "0")
14790 {
14791 // start a new array if reference token is 0
14792 result = &result->operator[](0);
14793 }
14794 else
14795 {
14796 // start a new object otherwise
14797 result = &result->operator[](reference_token);
14798 }
14799 break;
14800 }
14801
14802 case detail::value_t::object:
14803 {
14804 // create an entry in the object
14805 result = &result->operator[](reference_token);
14806 break;
14807 }
14808
14809 case detail::value_t::array:
14810 {
14811 // create an entry in the array
14812 result = &result->operator[](array_index<BasicJsonType>(reference_token));
14813 break;
14814 }
14815
14816 /*
14817 The following code is only reached if there exists a reference
14818 token _and_ the current value is primitive. In this case, we have
14819 an error situation, because primitive values may only occur as
14820 single value; that is, with an empty list of reference tokens.
14821 */
14822 case detail::value_t::string:
14823 case detail::value_t::boolean:
14824 case detail::value_t::number_integer:
14825 case detail::value_t::number_unsigned:
14826 case detail::value_t::number_float:
14827 case detail::value_t::binary:
14828 case detail::value_t::discarded:
14829 default:
14830 JSON_THROW(detail::type_error::create(313, "invalid value to unflatten", &j));
14831 }
14832 }
14833
14834 return *result;
14835 }
14836
14837 /*!
14838 @brief return a reference to the pointed to value
14839
14840 @note This version does not throw if a value is not present, but tries to
14841 create nested values instead. For instance, calling this function
14842 with pointer `"/this/that"` on a null value is equivalent to calling
14843 `operator[]("this").operator[]("that")` on that value, effectively
14844 changing the null value to an object.
14845
14846 @param[in] ptr a JSON value
14847
14848 @return reference to the JSON value pointed to by the JSON pointer
14849
14850 @complexity Linear in the length of the JSON pointer.
14851
14852 @throw parse_error.106 if an array index begins with '0'
14853 @throw parse_error.109 if an array index was not a number
14854 @throw out_of_range.404 if the JSON pointer can not be resolved
14855 */
14856 template<typename BasicJsonType>
14857 BasicJsonType& get_unchecked(BasicJsonType* ptr) const
14858 {
14859 for (const auto& reference_token : reference_tokens)
14860 {
14861 // convert null values to arrays or objects before continuing
14862 if (ptr->is_null())
14863 {
14864 // check if reference token is a number
14865 const bool nums =
14866 std::all_of(reference_token.begin(), reference_token.end(),
14867 [](const unsigned char x)
14868 {
14869 return std::isdigit(x);
14870 });
14871
14872 // change value to array for numbers or "-" or to object otherwise
14873 *ptr = (nums || reference_token == "-")
14874 ? detail::value_t::array
14875 : detail::value_t::object;
14876 }
14877
14878 switch (ptr->type())
14879 {
14880 case detail::value_t::object:
14881 {
14882 // use unchecked object access
14883 ptr = &ptr->operator[](reference_token);
14884 break;
14885 }
14886
14887 case detail::value_t::array:
14888 {
14889 if (reference_token == "-")
14890 {
14891 // explicitly treat "-" as index beyond the end
14892 ptr = &ptr->operator[](ptr->m_data.m_value.array->size());
14893 }
14894 else
14895 {
14896 // convert array index to number; unchecked access
14897 ptr = &ptr->operator[](array_index<BasicJsonType>(reference_token));
14898 }
14899 break;
14900 }
14901
14902 case detail::value_t::null:
14903 case detail::value_t::string:
14904 case detail::value_t::boolean:
14905 case detail::value_t::number_integer:
14906 case detail::value_t::number_unsigned:
14907 case detail::value_t::number_float:
14908 case detail::value_t::binary:
14909 case detail::value_t::discarded:
14910 default:
14911 JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
14912 }
14913 }
14914
14915 return *ptr;
14916 }
14917
14918 /*!
14919 @throw parse_error.106 if an array index begins with '0'
14920 @throw parse_error.109 if an array index was not a number
14921 @throw out_of_range.402 if the array index '-' is used
14922 @throw out_of_range.404 if the JSON pointer can not be resolved
14923 */
14924 template<typename BasicJsonType>
14925 BasicJsonType& get_checked(BasicJsonType* ptr) const
14926 {
14927 for (const auto& reference_token : reference_tokens)
14928 {
14929 switch (ptr->type())
14930 {
14931 case detail::value_t::object:
14932 {
14933 // note: at performs range check
14934 ptr = &ptr->at(reference_token);
14935 break;
14936 }
14937
14938 case detail::value_t::array:
14939 {
14940 if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
14941 {
14942 // "-" always fails the range check
14943 JSON_THROW(detail::out_of_range::create(402, detail::concat(
14944 "array index '-' (", std::to_string(ptr->m_data.m_value.array->size()),
14945 ") is out of range"), ptr));
14946 }
14947
14948 // note: at performs range check
14949 ptr = &ptr->at(array_index<BasicJsonType>(reference_token));
14950 break;
14951 }
14952
14953 case detail::value_t::null:
14954 case detail::value_t::string:
14955 case detail::value_t::boolean:
14956 case detail::value_t::number_integer:
14957 case detail::value_t::number_unsigned:
14958 case detail::value_t::number_float:
14959 case detail::value_t::binary:
14960 case detail::value_t::discarded:
14961 default:
14962 JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
14963 }
14964 }
14965
14966 return *ptr;
14967 }
14968
14969 /*!
14970 @brief return a const reference to the pointed to value
14971
14972 @param[in] ptr a JSON value
14973
14974 @return const reference to the JSON value pointed to by the JSON
14975 pointer
14976
14977 @throw parse_error.106 if an array index begins with '0'
14978 @throw parse_error.109 if an array index was not a number
14979 @throw out_of_range.402 if the array index '-' is used
14980 @throw out_of_range.404 if the JSON pointer can not be resolved
14981 */
14982 template<typename BasicJsonType>
14983 const BasicJsonType& get_unchecked(const BasicJsonType* ptr) const
14984 {
14985 for (const auto& reference_token : reference_tokens)
14986 {
14987 switch (ptr->type())
14988 {
14989 case detail::value_t::object:
14990 {
14991 // use unchecked object access
14992 ptr = &ptr->operator[](reference_token);
14993 break;
14994 }
14995
14996 case detail::value_t::array:
14997 {
14998 if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
14999 {
15000 // "-" cannot be used for const access
15001 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));
15002 }
15003
15004 // use unchecked array access
15005 ptr = &ptr->operator[](array_index<BasicJsonType>(reference_token));
15006 break;
15007 }
15008
15009 case detail::value_t::null:
15010 case detail::value_t::string:
15011 case detail::value_t::boolean:
15012 case detail::value_t::number_integer:
15013 case detail::value_t::number_unsigned:
15014 case detail::value_t::number_float:
15015 case detail::value_t::binary:
15016 case detail::value_t::discarded:
15017 default:
15018 JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
15019 }
15020 }
15021
15022 return *ptr;
15023 }
15024
15025 /*!
15026 @throw parse_error.106 if an array index begins with '0'
15027 @throw parse_error.109 if an array index was not a number
15028 @throw out_of_range.402 if the array index '-' is used
15029 @throw out_of_range.404 if the JSON pointer can not be resolved
15030 */
15031 template<typename BasicJsonType>
15032 const BasicJsonType& get_checked(const BasicJsonType* ptr) const
15033 {
15034 for (const auto& reference_token : reference_tokens)
15035 {
15036 switch (ptr->type())
15037 {
15038 case detail::value_t::object:
15039 {
15040 // note: at performs range check
15041 ptr = &ptr->at(reference_token);
15042 break;
15043 }
15044
15045 case detail::value_t::array:
15046 {
15047 if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
15048 {
15049 // "-" always fails the range check
15050 JSON_THROW(detail::out_of_range::create(402, detail::concat(
15051 "array index '-' (", std::to_string(ptr->m_data.m_value.array->size()),
15052 ") is out of range"), ptr));
15053 }
15054
15055 // note: at performs range check
15056 ptr = &ptr->at(array_index<BasicJsonType>(reference_token));
15057 break;
15058 }
15059
15060 case detail::value_t::null:
15061 case detail::value_t::string:
15062 case detail::value_t::boolean:
15063 case detail::value_t::number_integer:
15064 case detail::value_t::number_unsigned:
15065 case detail::value_t::number_float:
15066 case detail::value_t::binary:
15067 case detail::value_t::discarded:
15068 default:
15069 JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
15070 }
15071 }
15072
15073 return *ptr;
15074 }
15075
15076 /*!
15077 @throw parse_error.106 if an array index begins with '0'
15078 @throw parse_error.109 if an array index was not a number
15079 */
15080 template<typename BasicJsonType>
15081 bool contains(const BasicJsonType* ptr) const
15082 {
15083 for (const auto& reference_token : reference_tokens)
15084 {
15085 switch (ptr->type())
15086 {
15087 case detail::value_t::object:
15088 {
15089 if (!ptr->contains(reference_token))
15090 {
15091 // we did not find the key in the object
15092 return false;
15093 }
15094
15095 ptr = &ptr->operator[](reference_token);
15096 break;
15097 }
15098
15099 case detail::value_t::array:
15100 {
15101 if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
15102 {
15103 // "-" always fails the range check
15104 return false;
15105 }
15106 if (JSON_HEDLEY_UNLIKELY(reference_token.size() == 1 && !("0" <= reference_token && reference_token <= "9")))
15107 {
15108 // invalid char
15109 return false;
15110 }
15111 if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1))
15112 {
15113 if (JSON_HEDLEY_UNLIKELY(!('1' <= reference_token[0] && reference_token[0] <= '9')))
15114 {
15115 // first char should be between '1' and '9'
15116 return false;
15117 }
15118 for (std::size_t i = 1; i < reference_token.size(); i++)
15119 {
15120 if (JSON_HEDLEY_UNLIKELY(!('0' <= reference_token[i] && reference_token[i] <= '9')))
15121 {
15122 // other char should be between '0' and '9'
15123 return false;
15124 }
15125 }
15126 }
15127
15128 const auto idx = array_index<BasicJsonType>(reference_token);
15129 if (idx >= ptr->size())
15130 {
15131 // index out of range
15132 return false;
15133 }
15134
15135 ptr = &ptr->operator[](idx);
15136 break;
15137 }
15138
15139 case detail::value_t::null:
15140 case detail::value_t::string:
15141 case detail::value_t::boolean:
15142 case detail::value_t::number_integer:
15143 case detail::value_t::number_unsigned:
15144 case detail::value_t::number_float:
15145 case detail::value_t::binary:
15146 case detail::value_t::discarded:
15147 default:
15148 {
15149 // we do not expect primitive values if there is still a
15150 // reference token to process
15151 return false;
15152 }
15153 }
15154 }
15155
15156 // no reference token left means we found a primitive value
15157 return true;
15158 }
15159
15160 /*!
15161 @brief split the string input to reference tokens
15162
15163 @note This function is only called by the json_pointer constructor.
15164 All exceptions below are documented there.
15165
15166 @throw parse_error.107 if the pointer is not empty or begins with '/'
15167 @throw parse_error.108 if character '~' is not followed by '0' or '1'
15168 */
15169 static std::vector<string_t> split(const string_t& reference_string)
15170 {
15171 std::vector<string_t> result;
15172
15173 // special case: empty reference string -> no reference tokens
15174 if (reference_string.empty())
15175 {
15176 return result;
15177 }
15178
15179 // check if nonempty reference string begins with slash
15180 if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/'))
15181 {
15182 JSON_THROW(detail::parse_error::create(107, 1, detail::concat("JSON pointer must be empty or begin with '/' - was: '", reference_string, "'"), nullptr));
15183 }
15184
15185 // extract the reference tokens:
15186 // - slash: position of the last read slash (or end of string)
15187 // - start: position after the previous slash
15188 for (
15189 // search for the first slash after the first character
15190 std::size_t slash = reference_string.find_first_of('/', 1),
15191 // set the beginning of the first reference token
15192 start = 1;
15193 // we can stop if start == 0 (if slash == string_t::npos)
15194 start != 0;
15195 // set the beginning of the next reference token
15196 // (will eventually be 0 if slash == string_t::npos)
15197 start = (slash == string_t::npos) ? 0 : slash + 1,
15198 // find next slash
15199 slash = reference_string.find_first_of('/', start))
15200 {
15201 // use the text between the beginning of the reference token
15202 // (start) and the last slash (slash).
15203 auto reference_token = reference_string.substr(start, slash - start);
15204
15205 // check reference tokens are properly escaped
15206 for (std::size_t pos = reference_token.find_first_of('~');
15207 pos != string_t::npos;
15208 pos = reference_token.find_first_of('~', pos + 1))
15209 {
15210 JSON_ASSERT(reference_token[pos] == '~');
15211
15212 // ~ must be followed by 0 or 1
15213 if (JSON_HEDLEY_UNLIKELY(pos == reference_token.size() - 1 ||
15214 (reference_token[pos + 1] != '0' &&
15215 reference_token[pos + 1] != '1')))
15216 {
15217 JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'", nullptr));
15218 }
15219 }
15220
15221 // finally, store the reference token
15222 detail::unescape(reference_token);
15223 result.push_back(reference_token);
15224 }
15225
15226 return result;
15227 }
15228
15229 private:
15230 /*!
15231 @param[in] reference_string the reference string to the current value
15232 @param[in] value the value to consider
15233 @param[in,out] result the result object to insert values to
15234
15235 @note Empty objects or arrays are flattened to `null`.
15236 */
15237 template<typename BasicJsonType>
15238 static void flatten(const string_t& reference_string,
15239 const BasicJsonType& value,
15240 BasicJsonType& result)
15241 {
15242 switch (value.type())
15243 {
15244 case detail::value_t::array:
15245 {
15246 if (value.m_data.m_value.array->empty())
15247 {
15248 // flatten empty array as null
15249 result[reference_string] = nullptr;
15250 }
15251 else
15252 {
15253 // iterate array and use index as reference string
15254 for (std::size_t i = 0; i < value.m_data.m_value.array->size(); ++i)
15255 {
15256 flatten(detail::concat<string_t>(reference_string, '/', std::to_string(i)),
15257 value.m_data.m_value.array->operator[](i), result);
15258 }
15259 }
15260 break;
15261 }
15262
15263 case detail::value_t::object:
15264 {
15265 if (value.m_data.m_value.object->empty())
15266 {
15267 // flatten empty object as null
15268 result[reference_string] = nullptr;
15269 }
15270 else
15271 {
15272 // iterate object and use keys as reference string
15273 for (const auto& element : *value.m_data.m_value.object)
15274 {
15275 flatten(detail::concat<string_t>(reference_string, '/', detail::escape(element.first)), element.second, result);
15276 }
15277 }
15278 break;
15279 }
15280
15281 case detail::value_t::null:
15282 case detail::value_t::string:
15283 case detail::value_t::boolean:
15284 case detail::value_t::number_integer:
15285 case detail::value_t::number_unsigned:
15286 case detail::value_t::number_float:
15287 case detail::value_t::binary:
15288 case detail::value_t::discarded:
15289 default:
15290 {
15291 // add primitive value with its reference string
15292 result[reference_string] = value;
15293 break;
15294 }
15295 }
15296 }
15297
15298 /*!
15299 @param[in] value flattened JSON
15300
15301 @return unflattened JSON
15302
15303 @throw parse_error.109 if array index is not a number
15304 @throw type_error.314 if value is not an object
15305 @throw type_error.315 if object values are not primitive
15306 @throw type_error.313 if value cannot be unflattened
15307 */
15308 template<typename BasicJsonType>
15309 static BasicJsonType
15310 unflatten(const BasicJsonType& value)
15311 {
15312 if (JSON_HEDLEY_UNLIKELY(!value.is_object()))
15313 {
15314 JSON_THROW(detail::type_error::create(314, "only objects can be unflattened", &value));
15315 }
15316
15317 BasicJsonType result;
15318
15319 // iterate the JSON object values
15320 for (const auto& element : *value.m_data.m_value.object)
15321 {
15322 if (JSON_HEDLEY_UNLIKELY(!element.second.is_primitive()))
15323 {
15324 JSON_THROW(detail::type_error::create(315, "values in object must be primitive", &element.second));
15325 }
15326
15327 // assign value to reference pointed to by JSON pointer; Note that if
15328 // the JSON pointer is "" (i.e., points to the whole value), function
15329 // get_and_create returns a reference to result itself. An assignment
15330 // will then create a primitive value.
15331 json_pointer(element.first).get_and_create(result) = element.second;
15332 }
15333
15334 return result;
15335 }
15336
15337 // can't use conversion operator because of ambiguity
15338 json_pointer<string_t> convert() const&
15339 {
15340 json_pointer<string_t> result;
15341 result.reference_tokens = reference_tokens;
15342 return result;
15343 }
15344
15345 json_pointer<string_t> convert()&&
15346 {
15347 json_pointer<string_t> result;
15348 result.reference_tokens = std::move(reference_tokens);
15349 return result;
15350 }
15351
15352 public:
15353 #if JSON_HAS_THREE_WAY_COMPARISON
15354 /// @brief compares two JSON pointers for equality
15355 /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
15356 template<typename RefStringTypeRhs>
15357 bool operator==(const json_pointer<RefStringTypeRhs>& rhs) const noexcept
15358 {
15359 return reference_tokens == rhs.reference_tokens;
15360 }
15361
15362 /// @brief compares JSON pointer and string for equality
15363 /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
15364 JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer))
15365 bool operator==(const string_t& rhs) const
15366 {
15367 return *this == json_pointer(rhs);
15368 }
15369
15370 /// @brief 3-way compares two JSON pointers
15371 template<typename RefStringTypeRhs>
15372 std::strong_ordering operator<=>(const json_pointer<RefStringTypeRhs>& rhs) const noexcept // *NOPAD*
15373 {
15374 return reference_tokens <=> rhs.reference_tokens; // *NOPAD*
15375 }
15376 #else
15377 /// @brief compares two JSON pointers for equality
15378 /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
15379 template<typename RefStringTypeLhs, typename RefStringTypeRhs>
15380 // NOLINTNEXTLINE(readability-redundant-declaration)
15381 friend bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
15382 const json_pointer<RefStringTypeRhs>& rhs) noexcept;
15383
15384 /// @brief compares JSON pointer and string for equality
15385 /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
15386 template<typename RefStringTypeLhs, typename StringType>
15387 // NOLINTNEXTLINE(readability-redundant-declaration)
15388 friend bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
15389 const StringType& rhs);
15390
15391 /// @brief compares string and JSON pointer for equality
15392 /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
15393 template<typename RefStringTypeRhs, typename StringType>
15394 // NOLINTNEXTLINE(readability-redundant-declaration)
15395 friend bool operator==(const StringType& lhs,
15396 const json_pointer<RefStringTypeRhs>& rhs);
15397
15398 /// @brief compares two JSON pointers for inequality
15399 /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
15400 template<typename RefStringTypeLhs, typename RefStringTypeRhs>
15401 // NOLINTNEXTLINE(readability-redundant-declaration)
15402 friend bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
15403 const json_pointer<RefStringTypeRhs>& rhs) noexcept;
15404
15405 /// @brief compares JSON pointer and string for inequality
15406 /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
15407 template<typename RefStringTypeLhs, typename StringType>
15408 // NOLINTNEXTLINE(readability-redundant-declaration)
15409 friend bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
15410 const StringType& rhs);
15411
15412 /// @brief compares string and JSON pointer for inequality
15413 /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
15414 template<typename RefStringTypeRhs, typename StringType>
15415 // NOLINTNEXTLINE(readability-redundant-declaration)
15416 friend bool operator!=(const StringType& lhs,
15417 const json_pointer<RefStringTypeRhs>& rhs);
15418
15419 /// @brief compares two JSON pointer for less-than
15420 template<typename RefStringTypeLhs, typename RefStringTypeRhs>
15421 // NOLINTNEXTLINE(readability-redundant-declaration)
15422 friend bool operator<(const json_pointer<RefStringTypeLhs>& lhs,
15423 const json_pointer<RefStringTypeRhs>& rhs) noexcept;
15424 #endif
15425
15426 private:
15427 /// the reference tokens
15428 std::vector<string_t> reference_tokens;
15429 };
15430
15431 #if !JSON_HAS_THREE_WAY_COMPARISON
15432 // functions cannot be defined inside class due to ODR violations
15433 template<typename RefStringTypeLhs, typename RefStringTypeRhs>
15434 inline bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
15435 const json_pointer<RefStringTypeRhs>& rhs) noexcept
15436 {
15437 return lhs.reference_tokens == rhs.reference_tokens;
15438 }
15439
15440 template<typename RefStringTypeLhs,
15441 typename StringType = typename json_pointer<RefStringTypeLhs>::string_t>
15442 JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer, json_pointer))
15443 inline bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
15444 const StringType& rhs)
15445 {
15446 return lhs == json_pointer<RefStringTypeLhs>(rhs);
15447 }
15448
15449 template<typename RefStringTypeRhs,
15450 typename StringType = typename json_pointer<RefStringTypeRhs>::string_t>
15451 JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer, json_pointer))
15452 inline bool operator==(const StringType& lhs,
15453 const json_pointer<RefStringTypeRhs>& rhs)
15454 {
15455 return json_pointer<RefStringTypeRhs>(lhs) == rhs;
15456 }
15457
15458 template<typename RefStringTypeLhs, typename RefStringTypeRhs>
15459 inline bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
15460 const json_pointer<RefStringTypeRhs>& rhs) noexcept
15461 {
15462 return !(lhs == rhs);
15463 }
15464
15465 template<typename RefStringTypeLhs,
15466 typename StringType = typename json_pointer<RefStringTypeLhs>::string_t>
15467 JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator!=(json_pointer, json_pointer))
15468 inline bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
15469 const StringType& rhs)
15470 {
15471 return !(lhs == rhs);
15472 }
15473
15474 template<typename RefStringTypeRhs,
15475 typename StringType = typename json_pointer<RefStringTypeRhs>::string_t>
15476 JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator!=(json_pointer, json_pointer))
15477 inline bool operator!=(const StringType& lhs,
15478 const json_pointer<RefStringTypeRhs>& rhs)
15479 {
15480 return !(lhs == rhs);
15481 }
15482
15483 template<typename RefStringTypeLhs, typename RefStringTypeRhs>
15484 inline bool operator<(const json_pointer<RefStringTypeLhs>& lhs,
15485 const json_pointer<RefStringTypeRhs>& rhs) noexcept
15486 {
15487 return lhs.reference_tokens < rhs.reference_tokens;
15488 }
15489 #endif
15490
15491 NLOHMANN_JSON_NAMESPACE_END
15492
15493 // #include <nlohmann/detail/json_ref.hpp>
15494 // __ _____ _____ _____
15495 // __| | __| | | | JSON for Modern C++
15496 // | | |__ | | | | | | version 3.12.0
15497 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
15498 //
15499 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
15500 // SPDX-License-Identifier: MIT
15501
15502
15503
15504 #include <initializer_list>
15505 #include <utility>
15506
15507 // #include <nlohmann/detail/abi_macros.hpp>
15508
15509 // #include <nlohmann/detail/meta/type_traits.hpp>
15510
15511
15512 NLOHMANN_JSON_NAMESPACE_BEGIN
15513 namespace detail
15514 {
15515
15516 template<typename BasicJsonType>
15517 class json_ref
15518 {
15519 public:
15520 using value_type = BasicJsonType;
15521
15522 json_ref(value_type&& value)
15523 : owned_value(std::move(value))
15524 {}
15525
15526 json_ref(const value_type& value)
15527 : value_ref(&value)
15528 {}
15529
15530 json_ref(std::initializer_list<json_ref> init)
15531 : owned_value(init)
15532 {}
15533
15534 template <
15535 class... Args,
15536 enable_if_t<std::is_constructible<value_type, Args...>::value, int> = 0 >
15537 json_ref(Args && ... args)
15538 : owned_value(std::forward<Args>(args)...)
15539 {}
15540
15541 // class should be movable only
15542 json_ref(json_ref&&) noexcept = default;
15543 json_ref(const json_ref&) = delete;
15544 json_ref& operator=(const json_ref&) = delete;
15545 json_ref& operator=(json_ref&&) = delete;
15546 ~json_ref() = default;
15547
15548 value_type moved_or_copied() const
15549 {
15550 if (value_ref == nullptr)
15551 {
15552 return std::move(owned_value);
15553 }
15554 return *value_ref;
15555 }
15556
15557 value_type const& operator*() const
15558 {
15559 return value_ref ? *value_ref : owned_value;
15560 }
15561
15562 value_type const* operator->() const
15563 {
15564 return &** this;
15565 }
15566
15567 private:
15568 mutable value_type owned_value = nullptr;
15569 value_type const* value_ref = nullptr;
15570 };
15571
15572 } // namespace detail
15573 NLOHMANN_JSON_NAMESPACE_END
15574
15575 // #include <nlohmann/detail/macro_scope.hpp>
15576
15577 // #include <nlohmann/detail/string_concat.hpp>
15578
15579 // #include <nlohmann/detail/string_escape.hpp>
15580
15581 // #include <nlohmann/detail/string_utils.hpp>
15582
15583 // #include <nlohmann/detail/meta/cpp_future.hpp>
15584
15585 // #include <nlohmann/detail/meta/type_traits.hpp>
15586
15587 // #include <nlohmann/detail/output/binary_writer.hpp>
15588 // __ _____ _____ _____
15589 // __| | __| | | | JSON for Modern C++
15590 // | | |__ | | | | | | version 3.12.0
15591 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
15592 //
15593 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
15594 // SPDX-License-Identifier: MIT
15595
15596
15597
15598 #include <algorithm> // reverse
15599 #include <array> // array
15600 #include <map> // map
15601 #include <cmath> // isnan, isinf
15602 #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
15603 #include <cstring> // memcpy
15604 #include <limits> // numeric_limits
15605 #include <string> // string
15606 #include <utility> // move
15607 #include <vector> // vector
15608
15609 // #include <nlohmann/detail/input/binary_reader.hpp>
15610
15611 // #include <nlohmann/detail/macro_scope.hpp>
15612
15613 // #include <nlohmann/detail/output/output_adapters.hpp>
15614 // __ _____ _____ _____
15615 // __| | __| | | | JSON for Modern C++
15616 // | | |__ | | | | | | version 3.12.0
15617 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
15618 //
15619 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
15620 // SPDX-License-Identifier: MIT
15621
15622
15623
15624 #include <algorithm> // copy
15625 #include <cstddef> // size_t
15626 #include <iterator> // back_inserter
15627 #include <memory> // shared_ptr, make_shared
15628 #include <string> // basic_string
15629 #include <vector> // vector
15630
15631 #ifndef JSON_NO_IO
15632 #include <ios> // streamsize
15633 #include <ostream> // basic_ostream
15634 #endif // JSON_NO_IO
15635
15636 // #include <nlohmann/detail/macro_scope.hpp>
15637
15638
15639 NLOHMANN_JSON_NAMESPACE_BEGIN
15640 namespace detail
15641 {
15642
15643 /// abstract output adapter interface
15644 template<typename CharType> struct output_adapter_protocol
15645 {
15646 virtual void write_character(CharType c) = 0;
15647 virtual void write_characters(const CharType* s, std::size_t length) = 0;
15648 virtual ~output_adapter_protocol() = default;
15649
15650 output_adapter_protocol() = default;
15651 output_adapter_protocol(const output_adapter_protocol&) = default;
15652 output_adapter_protocol(output_adapter_protocol&&) noexcept = default;
15653 output_adapter_protocol& operator=(const output_adapter_protocol&) = default;
15654 output_adapter_protocol& operator=(output_adapter_protocol&&) noexcept = default;
15655 };
15656
15657 /// a type to simplify interfaces
15658 template<typename CharType>
15659 using output_adapter_t = std::shared_ptr<output_adapter_protocol<CharType>>;
15660
15661 /// output adapter for byte vectors
15662 template<typename CharType, typename AllocatorType = std::allocator<CharType>>
15663 class output_vector_adapter : public output_adapter_protocol<CharType>
15664 {
15665 public:
15666 explicit output_vector_adapter(std::vector<CharType, AllocatorType>& vec) noexcept
15667 : v(vec)
15668 {}
15669
15670 void write_character(CharType c) override
15671 {
15672 v.push_back(c);
15673 }
15674
15675 JSON_HEDLEY_NON_NULL(2)
15676 void write_characters(const CharType* s, std::size_t length) override
15677 {
15678 v.insert(v.end(), s, s + length);
15679 }
15680
15681 private:
15682 std::vector<CharType, AllocatorType>& v;
15683 };
15684
15685 #ifndef JSON_NO_IO
15686 /// output adapter for output streams
15687 template<typename CharType>
15688 class output_stream_adapter : public output_adapter_protocol<CharType>
15689 {
15690 public:
15691 explicit output_stream_adapter(std::basic_ostream<CharType>& s) noexcept
15692 : stream(s)
15693 {}
15694
15695 void write_character(CharType c) override
15696 {
15697 stream.put(c);
15698 }
15699
15700 JSON_HEDLEY_NON_NULL(2)
15701 void write_characters(const CharType* s, std::size_t length) override
15702 {
15703 stream.write(s, static_cast<std::streamsize>(length));
15704 }
15705
15706 private:
15707 std::basic_ostream<CharType>& stream;
15708 };
15709 #endif // JSON_NO_IO
15710
15711 /// output adapter for basic_string
15712 template<typename CharType, typename StringType = std::basic_string<CharType>>
15713 class output_string_adapter : public output_adapter_protocol<CharType>
15714 {
15715 public:
15716 explicit output_string_adapter(StringType& s) noexcept
15717 : str(s)
15718 {}
15719
15720 void write_character(CharType c) override
15721 {
15722 str.push_back(c);
15723 }
15724
15725 JSON_HEDLEY_NON_NULL(2)
15726 void write_characters(const CharType* s, std::size_t length) override
15727 {
15728 str.append(s, length);
15729 }
15730
15731 private:
15732 StringType& str;
15733 };
15734
15735 template<typename CharType, typename StringType = std::basic_string<CharType>>
15736 class output_adapter
15737 {
15738 public:
15739 template<typename AllocatorType = std::allocator<CharType>>
15740 output_adapter(std::vector<CharType, AllocatorType>& vec)
15741 : oa(std::make_shared<output_vector_adapter<CharType, AllocatorType>>(vec)) {}
15742
15743 #ifndef JSON_NO_IO
15744 output_adapter(std::basic_ostream<CharType>& s)
15745 : oa(std::make_shared<output_stream_adapter<CharType>>(s)) {}
15746 #endif // JSON_NO_IO
15747
15748 output_adapter(StringType& s)
15749 : oa(std::make_shared<output_string_adapter<CharType, StringType>>(s)) {}
15750
15751 operator output_adapter_t<CharType>()
15752 {
15753 return oa;
15754 }
15755
15756 private:
15757 output_adapter_t<CharType> oa = nullptr;
15758 };
15759
15760 } // namespace detail
15761 NLOHMANN_JSON_NAMESPACE_END
15762
15763 // #include <nlohmann/detail/string_concat.hpp>
15764
15765
15766 NLOHMANN_JSON_NAMESPACE_BEGIN
15767 namespace detail
15768 {
15769
15770 /// how to encode BJData
15771 enum class bjdata_version_t
15772 {
15773 draft2,
15774 draft3,
15775 };
15776
15777 ///////////////////
15778 // binary writer //
15779 ///////////////////
15780
15781 /*!
15782 @brief serialization to CBOR and MessagePack values
15783 */
15784 template<typename BasicJsonType, typename CharType>
15785 class binary_writer
15786 {
15787 using string_t = typename BasicJsonType::string_t;
15788 using binary_t = typename BasicJsonType::binary_t;
15789 using number_float_t = typename BasicJsonType::number_float_t;
15790
15791 public:
15792 /*!
15793 @brief create a binary writer
15794
15795 @param[in] adapter output adapter to write to
15796 */
15797 explicit binary_writer(output_adapter_t<CharType> adapter) : oa(std::move(adapter))
15798 {
15799 JSON_ASSERT(oa);
15800 }
15801
15802 /*!
15803 @param[in] j JSON value to serialize
15804 @pre j.type() == value_t::object
15805 */
15806 void write_bson(const BasicJsonType& j)
15807 {
15808 switch (j.type())
15809 {
15810 case value_t::object:
15811 {
15812 write_bson_object(*j.m_data.m_value.object);
15813 break;
15814 }
15815
15816 case value_t::null:
15817 case value_t::array:
15818 case value_t::string:
15819 case value_t::boolean:
15820 case value_t::number_integer:
15821 case value_t::number_unsigned:
15822 case value_t::number_float:
15823 case value_t::binary:
15824 case value_t::discarded:
15825 default:
15826 {
15827 JSON_THROW(type_error::create(317, concat("to serialize to BSON, top-level type must be object, but is ", j.type_name()), &j));
15828 }
15829 }
15830 }
15831
15832 /*!
15833 @param[in] j JSON value to serialize
15834 */
15835 void write_cbor(const BasicJsonType& j)
15836 {
15837 switch (j.type())
15838 {
15839 case value_t::null:
15840 {
15841 oa->write_character(to_char_type(0xF6));
15842 break;
15843 }
15844
15845 case value_t::boolean:
15846 {
15847 oa->write_character(j.m_data.m_value.boolean
15848 ? to_char_type(0xF5)
15849 : to_char_type(0xF4));
15850 break;
15851 }
15852
15853 case value_t::number_integer:
15854 {
15855 if (j.m_data.m_value.number_integer >= 0)
15856 {
15857 // CBOR does not differentiate between positive signed
15858 // integers and unsigned integers. Therefore, we used the
15859 // code from the value_t::number_unsigned case here.
15860 if (j.m_data.m_value.number_integer <= 0x17)
15861 {
15862 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
15863 }
15864 else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint8_t>::max)())
15865 {
15866 oa->write_character(to_char_type(0x18));
15867 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
15868 }
15869 else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint16_t>::max)())
15870 {
15871 oa->write_character(to_char_type(0x19));
15872 write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_integer));
15873 }
15874 else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint32_t>::max)())
15875 {
15876 oa->write_character(to_char_type(0x1A));
15877 write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_integer));
15878 }
15879 else
15880 {
15881 oa->write_character(to_char_type(0x1B));
15882 write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_integer));
15883 }
15884 }
15885 else
15886 {
15887 // The conversions below encode the sign in the first
15888 // byte, and the value is converted to a positive number.
15889 const auto positive_number = -1 - j.m_data.m_value.number_integer;
15890 if (j.m_data.m_value.number_integer >= -24)
15891 {
15892 write_number(static_cast<std::uint8_t>(0x20 + positive_number));
15893 }
15894 else if (positive_number <= (std::numeric_limits<std::uint8_t>::max)())
15895 {
15896 oa->write_character(to_char_type(0x38));
15897 write_number(static_cast<std::uint8_t>(positive_number));
15898 }
15899 else if (positive_number <= (std::numeric_limits<std::uint16_t>::max)())
15900 {
15901 oa->write_character(to_char_type(0x39));
15902 write_number(static_cast<std::uint16_t>(positive_number));
15903 }
15904 else if (positive_number <= (std::numeric_limits<std::uint32_t>::max)())
15905 {
15906 oa->write_character(to_char_type(0x3A));
15907 write_number(static_cast<std::uint32_t>(positive_number));
15908 }
15909 else
15910 {
15911 oa->write_character(to_char_type(0x3B));
15912 write_number(static_cast<std::uint64_t>(positive_number));
15913 }
15914 }
15915 break;
15916 }
15917
15918 case value_t::number_unsigned:
15919 {
15920 if (j.m_data.m_value.number_unsigned <= 0x17)
15921 {
15922 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_unsigned));
15923 }
15924 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
15925 {
15926 oa->write_character(to_char_type(0x18));
15927 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_unsigned));
15928 }
15929 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
15930 {
15931 oa->write_character(to_char_type(0x19));
15932 write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_unsigned));
15933 }
15934 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
15935 {
15936 oa->write_character(to_char_type(0x1A));
15937 write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_unsigned));
15938 }
15939 else
15940 {
15941 oa->write_character(to_char_type(0x1B));
15942 write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_unsigned));
15943 }
15944 break;
15945 }
15946
15947 case value_t::number_float:
15948 {
15949 if (std::isnan(j.m_data.m_value.number_float))
15950 {
15951 // NaN is 0xf97e00 in CBOR
15952 oa->write_character(to_char_type(0xF9));
15953 oa->write_character(to_char_type(0x7E));
15954 oa->write_character(to_char_type(0x00));
15955 }
15956 else if (std::isinf(j.m_data.m_value.number_float))
15957 {
15958 // Infinity is 0xf97c00, -Infinity is 0xf9fc00
15959 oa->write_character(to_char_type(0xf9));
15960 oa->write_character(j.m_data.m_value.number_float > 0 ? to_char_type(0x7C) : to_char_type(0xFC));
15961 oa->write_character(to_char_type(0x00));
15962 }
15963 else
15964 {
15965 write_compact_float(j.m_data.m_value.number_float, detail::input_format_t::cbor);
15966 }
15967 break;
15968 }
15969
15970 case value_t::string:
15971 {
15972 // step 1: write control byte and the string length
15973 const auto N = j.m_data.m_value.string->size();
15974 if (N <= 0x17)
15975 {
15976 write_number(static_cast<std::uint8_t>(0x60 + N));
15977 }
15978 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
15979 {
15980 oa->write_character(to_char_type(0x78));
15981 write_number(static_cast<std::uint8_t>(N));
15982 }
15983 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
15984 {
15985 oa->write_character(to_char_type(0x79));
15986 write_number(static_cast<std::uint16_t>(N));
15987 }
15988 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
15989 {
15990 oa->write_character(to_char_type(0x7A));
15991 write_number(static_cast<std::uint32_t>(N));
15992 }
15993 // LCOV_EXCL_START
15994 else if (N <= (std::numeric_limits<std::uint64_t>::max)())
15995 {
15996 oa->write_character(to_char_type(0x7B));
15997 write_number(static_cast<std::uint64_t>(N));
15998 }
15999 // LCOV_EXCL_STOP
16000
16001 // step 2: write the string
16002 oa->write_characters(
16003 reinterpret_cast<const CharType*>(j.m_data.m_value.string->c_str()),
16004 j.m_data.m_value.string->size());
16005 break;
16006 }
16007
16008 case value_t::array:
16009 {
16010 // step 1: write control byte and the array size
16011 const auto N = j.m_data.m_value.array->size();
16012 if (N <= 0x17)
16013 {
16014 write_number(static_cast<std::uint8_t>(0x80 + N));
16015 }
16016 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
16017 {
16018 oa->write_character(to_char_type(0x98));
16019 write_number(static_cast<std::uint8_t>(N));
16020 }
16021 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
16022 {
16023 oa->write_character(to_char_type(0x99));
16024 write_number(static_cast<std::uint16_t>(N));
16025 }
16026 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
16027 {
16028 oa->write_character(to_char_type(0x9A));
16029 write_number(static_cast<std::uint32_t>(N));
16030 }
16031 // LCOV_EXCL_START
16032 else if (N <= (std::numeric_limits<std::uint64_t>::max)())
16033 {
16034 oa->write_character(to_char_type(0x9B));
16035 write_number(static_cast<std::uint64_t>(N));
16036 }
16037 // LCOV_EXCL_STOP
16038
16039 // step 2: write each element
16040 for (const auto& el : *j.m_data.m_value.array)
16041 {
16042 write_cbor(el);
16043 }
16044 break;
16045 }
16046
16047 case value_t::binary:
16048 {
16049 if (j.m_data.m_value.binary->has_subtype())
16050 {
16051 if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint8_t>::max)())
16052 {
16053 write_number(static_cast<std::uint8_t>(0xd8));
16054 write_number(static_cast<std::uint8_t>(j.m_data.m_value.binary->subtype()));
16055 }
16056 else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint16_t>::max)())
16057 {
16058 write_number(static_cast<std::uint8_t>(0xd9));
16059 write_number(static_cast<std::uint16_t>(j.m_data.m_value.binary->subtype()));
16060 }
16061 else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint32_t>::max)())
16062 {
16063 write_number(static_cast<std::uint8_t>(0xda));
16064 write_number(static_cast<std::uint32_t>(j.m_data.m_value.binary->subtype()));
16065 }
16066 else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint64_t>::max)())
16067 {
16068 write_number(static_cast<std::uint8_t>(0xdb));
16069 write_number(static_cast<std::uint64_t>(j.m_data.m_value.binary->subtype()));
16070 }
16071 }
16072
16073 // step 1: write control byte and the binary array size
16074 const auto N = j.m_data.m_value.binary->size();
16075 if (N <= 0x17)
16076 {
16077 write_number(static_cast<std::uint8_t>(0x40 + N));
16078 }
16079 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
16080 {
16081 oa->write_character(to_char_type(0x58));
16082 write_number(static_cast<std::uint8_t>(N));
16083 }
16084 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
16085 {
16086 oa->write_character(to_char_type(0x59));
16087 write_number(static_cast<std::uint16_t>(N));
16088 }
16089 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
16090 {
16091 oa->write_character(to_char_type(0x5A));
16092 write_number(static_cast<std::uint32_t>(N));
16093 }
16094 // LCOV_EXCL_START
16095 else if (N <= (std::numeric_limits<std::uint64_t>::max)())
16096 {
16097 oa->write_character(to_char_type(0x5B));
16098 write_number(static_cast<std::uint64_t>(N));
16099 }
16100 // LCOV_EXCL_STOP
16101
16102 // step 2: write each element
16103 oa->write_characters(
16104 reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
16105 N);
16106
16107 break;
16108 }
16109
16110 case value_t::object:
16111 {
16112 // step 1: write control byte and the object size
16113 const auto N = j.m_data.m_value.object->size();
16114 if (N <= 0x17)
16115 {
16116 write_number(static_cast<std::uint8_t>(0xA0 + N));
16117 }
16118 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
16119 {
16120 oa->write_character(to_char_type(0xB8));
16121 write_number(static_cast<std::uint8_t>(N));
16122 }
16123 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
16124 {
16125 oa->write_character(to_char_type(0xB9));
16126 write_number(static_cast<std::uint16_t>(N));
16127 }
16128 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
16129 {
16130 oa->write_character(to_char_type(0xBA));
16131 write_number(static_cast<std::uint32_t>(N));
16132 }
16133 // LCOV_EXCL_START
16134 else if (N <= (std::numeric_limits<std::uint64_t>::max)())
16135 {
16136 oa->write_character(to_char_type(0xBB));
16137 write_number(static_cast<std::uint64_t>(N));
16138 }
16139 // LCOV_EXCL_STOP
16140
16141 // step 2: write each element
16142 for (const auto& el : *j.m_data.m_value.object)
16143 {
16144 write_cbor(el.first);
16145 write_cbor(el.second);
16146 }
16147 break;
16148 }
16149
16150 case value_t::discarded:
16151 default:
16152 break;
16153 }
16154 }
16155
16156 /*!
16157 @param[in] j JSON value to serialize
16158 */
16159 void write_msgpack(const BasicJsonType& j)
16160 {
16161 switch (j.type())
16162 {
16163 case value_t::null: // nil
16164 {
16165 oa->write_character(to_char_type(0xC0));
16166 break;
16167 }
16168
16169 case value_t::boolean: // true and false
16170 {
16171 oa->write_character(j.m_data.m_value.boolean
16172 ? to_char_type(0xC3)
16173 : to_char_type(0xC2));
16174 break;
16175 }
16176
16177 case value_t::number_integer:
16178 {
16179 if (j.m_data.m_value.number_integer >= 0)
16180 {
16181 // MessagePack does not differentiate between positive
16182 // signed integers and unsigned integers. Therefore, we used
16183 // the code from the value_t::number_unsigned case here.
16184 if (j.m_data.m_value.number_unsigned < 128)
16185 {
16186 // positive fixnum
16187 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
16188 }
16189 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
16190 {
16191 // uint 8
16192 oa->write_character(to_char_type(0xCC));
16193 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
16194 }
16195 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
16196 {
16197 // uint 16
16198 oa->write_character(to_char_type(0xCD));
16199 write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_integer));
16200 }
16201 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
16202 {
16203 // uint 32
16204 oa->write_character(to_char_type(0xCE));
16205 write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_integer));
16206 }
16207 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
16208 {
16209 // uint 64
16210 oa->write_character(to_char_type(0xCF));
16211 write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_integer));
16212 }
16213 }
16214 else
16215 {
16216 if (j.m_data.m_value.number_integer >= -32)
16217 {
16218 // negative fixnum
16219 write_number(static_cast<std::int8_t>(j.m_data.m_value.number_integer));
16220 }
16221 else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int8_t>::min)() &&
16222 j.m_data.m_value.number_integer <= (std::numeric_limits<std::int8_t>::max)())
16223 {
16224 // int 8
16225 oa->write_character(to_char_type(0xD0));
16226 write_number(static_cast<std::int8_t>(j.m_data.m_value.number_integer));
16227 }
16228 else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int16_t>::min)() &&
16229 j.m_data.m_value.number_integer <= (std::numeric_limits<std::int16_t>::max)())
16230 {
16231 // int 16
16232 oa->write_character(to_char_type(0xD1));
16233 write_number(static_cast<std::int16_t>(j.m_data.m_value.number_integer));
16234 }
16235 else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int32_t>::min)() &&
16236 j.m_data.m_value.number_integer <= (std::numeric_limits<std::int32_t>::max)())
16237 {
16238 // int 32
16239 oa->write_character(to_char_type(0xD2));
16240 write_number(static_cast<std::int32_t>(j.m_data.m_value.number_integer));
16241 }
16242 else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int64_t>::min)() &&
16243 j.m_data.m_value.number_integer <= (std::numeric_limits<std::int64_t>::max)())
16244 {
16245 // int 64
16246 oa->write_character(to_char_type(0xD3));
16247 write_number(static_cast<std::int64_t>(j.m_data.m_value.number_integer));
16248 }
16249 }
16250 break;
16251 }
16252
16253 case value_t::number_unsigned:
16254 {
16255 if (j.m_data.m_value.number_unsigned < 128)
16256 {
16257 // positive fixnum
16258 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
16259 }
16260 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
16261 {
16262 // uint 8
16263 oa->write_character(to_char_type(0xCC));
16264 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
16265 }
16266 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
16267 {
16268 // uint 16
16269 oa->write_character(to_char_type(0xCD));
16270 write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_integer));
16271 }
16272 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
16273 {
16274 // uint 32
16275 oa->write_character(to_char_type(0xCE));
16276 write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_integer));
16277 }
16278 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
16279 {
16280 // uint 64
16281 oa->write_character(to_char_type(0xCF));
16282 write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_integer));
16283 }
16284 break;
16285 }
16286
16287 case value_t::number_float:
16288 {
16289 write_compact_float(j.m_data.m_value.number_float, detail::input_format_t::msgpack);
16290 break;
16291 }
16292
16293 case value_t::string:
16294 {
16295 // step 1: write control byte and the string length
16296 const auto N = j.m_data.m_value.string->size();
16297 if (N <= 31)
16298 {
16299 // fixstr
16300 write_number(static_cast<std::uint8_t>(0xA0 | N));
16301 }
16302 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
16303 {
16304 // str 8
16305 oa->write_character(to_char_type(0xD9));
16306 write_number(static_cast<std::uint8_t>(N));
16307 }
16308 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
16309 {
16310 // str 16
16311 oa->write_character(to_char_type(0xDA));
16312 write_number(static_cast<std::uint16_t>(N));
16313 }
16314 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
16315 {
16316 // str 32
16317 oa->write_character(to_char_type(0xDB));
16318 write_number(static_cast<std::uint32_t>(N));
16319 }
16320
16321 // step 2: write the string
16322 oa->write_characters(
16323 reinterpret_cast<const CharType*>(j.m_data.m_value.string->c_str()),
16324 j.m_data.m_value.string->size());
16325 break;
16326 }
16327
16328 case value_t::array:
16329 {
16330 // step 1: write control byte and the array size
16331 const auto N = j.m_data.m_value.array->size();
16332 if (N <= 15)
16333 {
16334 // fixarray
16335 write_number(static_cast<std::uint8_t>(0x90 | N));
16336 }
16337 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
16338 {
16339 // array 16
16340 oa->write_character(to_char_type(0xDC));
16341 write_number(static_cast<std::uint16_t>(N));
16342 }
16343 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
16344 {
16345 // array 32
16346 oa->write_character(to_char_type(0xDD));
16347 write_number(static_cast<std::uint32_t>(N));
16348 }
16349
16350 // step 2: write each element
16351 for (const auto& el : *j.m_data.m_value.array)
16352 {
16353 write_msgpack(el);
16354 }
16355 break;
16356 }
16357
16358 case value_t::binary:
16359 {
16360 // step 0: determine if the binary type has a set subtype to
16361 // determine whether to use the ext or fixext types
16362 const bool use_ext = j.m_data.m_value.binary->has_subtype();
16363
16364 // step 1: write control byte and the byte string length
16365 const auto N = j.m_data.m_value.binary->size();
16366 if (N <= (std::numeric_limits<std::uint8_t>::max)())
16367 {
16368 std::uint8_t output_type{};
16369 bool fixed = true;
16370 if (use_ext)
16371 {
16372 switch (N)
16373 {
16374 case 1:
16375 output_type = 0xD4; // fixext 1
16376 break;
16377 case 2:
16378 output_type = 0xD5; // fixext 2
16379 break;
16380 case 4:
16381 output_type = 0xD6; // fixext 4
16382 break;
16383 case 8:
16384 output_type = 0xD7; // fixext 8
16385 break;
16386 case 16:
16387 output_type = 0xD8; // fixext 16
16388 break;
16389 default:
16390 output_type = 0xC7; // ext 8
16391 fixed = false;
16392 break;
16393 }
16394
16395 }
16396 else
16397 {
16398 output_type = 0xC4; // bin 8
16399 fixed = false;
16400 }
16401
16402 oa->write_character(to_char_type(output_type));
16403 if (!fixed)
16404 {
16405 write_number(static_cast<std::uint8_t>(N));
16406 }
16407 }
16408 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
16409 {
16410 const std::uint8_t output_type = use_ext
16411 ? 0xC8 // ext 16
16412 : 0xC5; // bin 16
16413
16414 oa->write_character(to_char_type(output_type));
16415 write_number(static_cast<std::uint16_t>(N));
16416 }
16417 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
16418 {
16419 const std::uint8_t output_type = use_ext
16420 ? 0xC9 // ext 32
16421 : 0xC6; // bin 32
16422
16423 oa->write_character(to_char_type(output_type));
16424 write_number(static_cast<std::uint32_t>(N));
16425 }
16426
16427 // step 1.5: if this is an ext type, write the subtype
16428 if (use_ext)
16429 {
16430 write_number(static_cast<std::int8_t>(j.m_data.m_value.binary->subtype()));
16431 }
16432
16433 // step 2: write the byte string
16434 oa->write_characters(
16435 reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
16436 N);
16437
16438 break;
16439 }
16440
16441 case value_t::object:
16442 {
16443 // step 1: write control byte and the object size
16444 const auto N = j.m_data.m_value.object->size();
16445 if (N <= 15)
16446 {
16447 // fixmap
16448 write_number(static_cast<std::uint8_t>(0x80 | (N & 0xF)));
16449 }
16450 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
16451 {
16452 // map 16
16453 oa->write_character(to_char_type(0xDE));
16454 write_number(static_cast<std::uint16_t>(N));
16455 }
16456 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
16457 {
16458 // map 32
16459 oa->write_character(to_char_type(0xDF));
16460 write_number(static_cast<std::uint32_t>(N));
16461 }
16462
16463 // step 2: write each element
16464 for (const auto& el : *j.m_data.m_value.object)
16465 {
16466 write_msgpack(el.first);
16467 write_msgpack(el.second);
16468 }
16469 break;
16470 }
16471
16472 case value_t::discarded:
16473 default:
16474 break;
16475 }
16476 }
16477
16478 /*!
16479 @param[in] j JSON value to serialize
16480 @param[in] use_count whether to use '#' prefixes (optimized format)
16481 @param[in] use_type whether to use '$' prefixes (optimized format)
16482 @param[in] add_prefix whether prefixes need to be used for this value
16483 @param[in] use_bjdata whether write in BJData format, default is false
16484 @param[in] bjdata_version which BJData version to use, default is draft2
16485 */
16486 void write_ubjson(const BasicJsonType& j, const bool use_count,
16487 const bool use_type, const bool add_prefix = true,
16488 const bool use_bjdata = false, const bjdata_version_t bjdata_version = bjdata_version_t::draft2)
16489 {
16490 const bool bjdata_draft3 = use_bjdata && bjdata_version == bjdata_version_t::draft3;
16491
16492 switch (j.type())
16493 {
16494 case value_t::null:
16495 {
16496 if (add_prefix)
16497 {
16498 oa->write_character(to_char_type('Z'));
16499 }
16500 break;
16501 }
16502
16503 case value_t::boolean:
16504 {
16505 if (add_prefix)
16506 {
16507 oa->write_character(j.m_data.m_value.boolean
16508 ? to_char_type('T')
16509 : to_char_type('F'));
16510 }
16511 break;
16512 }
16513
16514 case value_t::number_integer:
16515 {
16516 write_number_with_ubjson_prefix(j.m_data.m_value.number_integer, add_prefix, use_bjdata);
16517 break;
16518 }
16519
16520 case value_t::number_unsigned:
16521 {
16522 write_number_with_ubjson_prefix(j.m_data.m_value.number_unsigned, add_prefix, use_bjdata);
16523 break;
16524 }
16525
16526 case value_t::number_float:
16527 {
16528 write_number_with_ubjson_prefix(j.m_data.m_value.number_float, add_prefix, use_bjdata);
16529 break;
16530 }
16531
16532 case value_t::string:
16533 {
16534 if (add_prefix)
16535 {
16536 oa->write_character(to_char_type('S'));
16537 }
16538 write_number_with_ubjson_prefix(j.m_data.m_value.string->size(), true, use_bjdata);
16539 oa->write_characters(
16540 reinterpret_cast<const CharType*>(j.m_data.m_value.string->c_str()),
16541 j.m_data.m_value.string->size());
16542 break;
16543 }
16544
16545 case value_t::array:
16546 {
16547 if (add_prefix)
16548 {
16549 oa->write_character(to_char_type('['));
16550 }
16551
16552 bool prefix_required = true;
16553 if (use_type && !j.m_data.m_value.array->empty())
16554 {
16555 JSON_ASSERT(use_count);
16556 const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata);
16557 const bool same_prefix = std::all_of(j.begin() + 1, j.end(),
16558 [this, first_prefix, use_bjdata](const BasicJsonType & v)
16559 {
16560 return ubjson_prefix(v, use_bjdata) == first_prefix;
16561 });
16562
16563 std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
16564
16565 if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
16566 {
16567 prefix_required = false;
16568 oa->write_character(to_char_type('$'));
16569 oa->write_character(first_prefix);
16570 }
16571 }
16572
16573 if (use_count)
16574 {
16575 oa->write_character(to_char_type('#'));
16576 write_number_with_ubjson_prefix(j.m_data.m_value.array->size(), true, use_bjdata);
16577 }
16578
16579 for (const auto& el : *j.m_data.m_value.array)
16580 {
16581 write_ubjson(el, use_count, use_type, prefix_required, use_bjdata, bjdata_version);
16582 }
16583
16584 if (!use_count)
16585 {
16586 oa->write_character(to_char_type(']'));
16587 }
16588
16589 break;
16590 }
16591
16592 case value_t::binary:
16593 {
16594 if (add_prefix)
16595 {
16596 oa->write_character(to_char_type('['));
16597 }
16598
16599 if (use_type && (bjdata_draft3 || !j.m_data.m_value.binary->empty()))
16600 {
16601 JSON_ASSERT(use_count);
16602 oa->write_character(to_char_type('$'));
16603 oa->write_character(bjdata_draft3 ? 'B' : 'U');
16604 }
16605
16606 if (use_count)
16607 {
16608 oa->write_character(to_char_type('#'));
16609 write_number_with_ubjson_prefix(j.m_data.m_value.binary->size(), true, use_bjdata);
16610 }
16611
16612 if (use_type)
16613 {
16614 oa->write_characters(
16615 reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
16616 j.m_data.m_value.binary->size());
16617 }
16618 else
16619 {
16620 for (size_t i = 0; i < j.m_data.m_value.binary->size(); ++i)
16621 {
16622 oa->write_character(to_char_type(bjdata_draft3 ? 'B' : 'U'));
16623 oa->write_character(j.m_data.m_value.binary->data()[i]);
16624 }
16625 }
16626
16627 if (!use_count)
16628 {
16629 oa->write_character(to_char_type(']'));
16630 }
16631
16632 break;
16633 }
16634
16635 case value_t::object:
16636 {
16637 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())
16638 {
16639 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)
16640 {
16641 break;
16642 }
16643 }
16644
16645 if (add_prefix)
16646 {
16647 oa->write_character(to_char_type('{'));
16648 }
16649
16650 bool prefix_required = true;
16651 if (use_type && !j.m_data.m_value.object->empty())
16652 {
16653 JSON_ASSERT(use_count);
16654 const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata);
16655 const bool same_prefix = std::all_of(j.begin(), j.end(),
16656 [this, first_prefix, use_bjdata](const BasicJsonType & v)
16657 {
16658 return ubjson_prefix(v, use_bjdata) == first_prefix;
16659 });
16660
16661 std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
16662
16663 if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
16664 {
16665 prefix_required = false;
16666 oa->write_character(to_char_type('$'));
16667 oa->write_character(first_prefix);
16668 }
16669 }
16670
16671 if (use_count)
16672 {
16673 oa->write_character(to_char_type('#'));
16674 write_number_with_ubjson_prefix(j.m_data.m_value.object->size(), true, use_bjdata);
16675 }
16676
16677 for (const auto& el : *j.m_data.m_value.object)
16678 {
16679 write_number_with_ubjson_prefix(el.first.size(), true, use_bjdata);
16680 oa->write_characters(
16681 reinterpret_cast<const CharType*>(el.first.c_str()),
16682 el.first.size());
16683 write_ubjson(el.second, use_count, use_type, prefix_required, use_bjdata, bjdata_version);
16684 }
16685
16686 if (!use_count)
16687 {
16688 oa->write_character(to_char_type('}'));
16689 }
16690
16691 break;
16692 }
16693
16694 case value_t::discarded:
16695 default:
16696 break;
16697 }
16698 }
16699
16700 private:
16701 //////////
16702 // BSON //
16703 //////////
16704
16705 /*!
16706 @return The size of a BSON document entry header, including the id marker
16707 and the entry name size (and its null-terminator).
16708 */
16709 static std::size_t calc_bson_entry_header_size(const string_t& name, const BasicJsonType& j)
16710 {
16711 const auto it = name.find(static_cast<typename string_t::value_type>(0));
16712 if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos))
16713 {
16714 JSON_THROW(out_of_range::create(409, concat("BSON key cannot contain code point U+0000 (at byte ", std::to_string(it), ")"), &j));
16715 static_cast<void>(j);
16716 }
16717
16718 return /*id*/ 1ul + name.size() + /*zero-terminator*/1u;
16719 }
16720
16721 /*!
16722 @brief Writes the given @a element_type and @a name to the output adapter
16723 */
16724 void write_bson_entry_header(const string_t& name,
16725 const std::uint8_t element_type)
16726 {
16727 oa->write_character(to_char_type(element_type)); // boolean
16728 oa->write_characters(
16729 reinterpret_cast<const CharType*>(name.c_str()),
16730 name.size() + 1u);
16731 }
16732
16733 /*!
16734 @brief Writes a BSON element with key @a name and boolean value @a value
16735 */
16736 void write_bson_boolean(const string_t& name,
16737 const bool value)
16738 {
16739 write_bson_entry_header(name, 0x08);
16740 oa->write_character(value ? to_char_type(0x01) : to_char_type(0x00));
16741 }
16742
16743 /*!
16744 @brief Writes a BSON element with key @a name and double value @a value
16745 */
16746 void write_bson_double(const string_t& name,
16747 const double value)
16748 {
16749 write_bson_entry_header(name, 0x01);
16750 write_number<double>(value, true);
16751 }
16752
16753 /*!
16754 @return The size of the BSON-encoded string in @a value
16755 */
16756 static std::size_t calc_bson_string_size(const string_t& value)
16757 {
16758 return sizeof(std::int32_t) + value.size() + 1ul;
16759 }
16760
16761 /*!
16762 @brief Writes a BSON element with key @a name and string value @a value
16763 */
16764 void write_bson_string(const string_t& name,
16765 const string_t& value)
16766 {
16767 write_bson_entry_header(name, 0x02);
16768
16769 write_number<std::int32_t>(static_cast<std::int32_t>(value.size() + 1ul), true);
16770 oa->write_characters(
16771 reinterpret_cast<const CharType*>(value.c_str()),
16772 value.size() + 1);
16773 }
16774
16775 /*!
16776 @brief Writes a BSON element with key @a name and null value
16777 */
16778 void write_bson_null(const string_t& name)
16779 {
16780 write_bson_entry_header(name, 0x0A);
16781 }
16782
16783 /*!
16784 @return The size of the BSON-encoded integer @a value
16785 */
16786 static std::size_t calc_bson_integer_size(const std::int64_t value)
16787 {
16788 return (std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)()
16789 ? sizeof(std::int32_t)
16790 : sizeof(std::int64_t);
16791 }
16792
16793 /*!
16794 @brief Writes a BSON element with key @a name and integer @a value
16795 */
16796 void write_bson_integer(const string_t& name,
16797 const std::int64_t value)
16798 {
16799 if ((std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)())
16800 {
16801 write_bson_entry_header(name, 0x10); // int32
16802 write_number<std::int32_t>(static_cast<std::int32_t>(value), true);
16803 }
16804 else
16805 {
16806 write_bson_entry_header(name, 0x12); // int64
16807 write_number<std::int64_t>(static_cast<std::int64_t>(value), true);
16808 }
16809 }
16810
16811 /*!
16812 @return The size of the BSON-encoded unsigned integer in @a j
16813 */
16814 static constexpr std::size_t calc_bson_unsigned_size(const std::uint64_t value) noexcept
16815 {
16816 return (value <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
16817 ? sizeof(std::int32_t)
16818 : sizeof(std::int64_t);
16819 }
16820
16821 /*!
16822 @brief Writes a BSON element with key @a name and unsigned @a value
16823 */
16824 void write_bson_unsigned(const string_t& name,
16825 const BasicJsonType& j)
16826 {
16827 if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
16828 {
16829 write_bson_entry_header(name, 0x10 /* int32 */);
16830 write_number<std::int32_t>(static_cast<std::int32_t>(j.m_data.m_value.number_unsigned), true);
16831 }
16832 else if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
16833 {
16834 write_bson_entry_header(name, 0x12 /* int64 */);
16835 write_number<std::int64_t>(static_cast<std::int64_t>(j.m_data.m_value.number_unsigned), true);
16836 }
16837 else
16838 {
16839 write_bson_entry_header(name, 0x11 /* uint64 */);
16840 write_number<std::uint64_t>(static_cast<std::uint64_t>(j.m_data.m_value.number_unsigned), true);
16841 }
16842 }
16843
16844 /*!
16845 @brief Writes a BSON element with key @a name and object @a value
16846 */
16847 void write_bson_object_entry(const string_t& name,
16848 const typename BasicJsonType::object_t& value)
16849 {
16850 write_bson_entry_header(name, 0x03); // object
16851 write_bson_object(value);
16852 }
16853
16854 /*!
16855 @return The size of the BSON-encoded array @a value
16856 */
16857 static std::size_t calc_bson_array_size(const typename BasicJsonType::array_t& value)
16858 {
16859 std::size_t array_index = 0ul;
16860
16861 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)
16862 {
16863 return result + calc_bson_element_size(std::to_string(array_index++), el);
16864 });
16865
16866 return sizeof(std::int32_t) + embedded_document_size + 1ul;
16867 }
16868
16869 /*!
16870 @return The size of the BSON-encoded binary array @a value
16871 */
16872 static std::size_t calc_bson_binary_size(const typename BasicJsonType::binary_t& value)
16873 {
16874 return sizeof(std::int32_t) + value.size() + 1ul;
16875 }
16876
16877 /*!
16878 @brief Writes a BSON element with key @a name and array @a value
16879 */
16880 void write_bson_array(const string_t& name,
16881 const typename BasicJsonType::array_t& value)
16882 {
16883 write_bson_entry_header(name, 0x04); // array
16884 write_number<std::int32_t>(static_cast<std::int32_t>(calc_bson_array_size(value)), true);
16885
16886 std::size_t array_index = 0ul;
16887
16888 for (const auto& el : value)
16889 {
16890 write_bson_element(std::to_string(array_index++), el);
16891 }
16892
16893 oa->write_character(to_char_type(0x00));
16894 }
16895
16896 /*!
16897 @brief Writes a BSON element with key @a name and binary value @a value
16898 */
16899 void write_bson_binary(const string_t& name,
16900 const binary_t& value)
16901 {
16902 write_bson_entry_header(name, 0x05);
16903
16904 write_number<std::int32_t>(static_cast<std::int32_t>(value.size()), true);
16905 write_number(value.has_subtype() ? static_cast<std::uint8_t>(value.subtype()) : static_cast<std::uint8_t>(0x00));
16906
16907 oa->write_characters(reinterpret_cast<const CharType*>(value.data()), value.size());
16908 }
16909
16910 /*!
16911 @brief Calculates the size necessary to serialize the JSON value @a j with its @a name
16912 @return The calculated size for the BSON document entry for @a j with the given @a name.
16913 */
16914 static std::size_t calc_bson_element_size(const string_t& name,
16915 const BasicJsonType& j)
16916 {
16917 const auto header_size = calc_bson_entry_header_size(name, j);
16918 switch (j.type())
16919 {
16920 case value_t::object:
16921 return header_size + calc_bson_object_size(*j.m_data.m_value.object);
16922
16923 case value_t::array:
16924 return header_size + calc_bson_array_size(*j.m_data.m_value.array);
16925
16926 case value_t::binary:
16927 return header_size + calc_bson_binary_size(*j.m_data.m_value.binary);
16928
16929 case value_t::boolean:
16930 return header_size + 1ul;
16931
16932 case value_t::number_float:
16933 return header_size + 8ul;
16934
16935 case value_t::number_integer:
16936 return header_size + calc_bson_integer_size(j.m_data.m_value.number_integer);
16937
16938 case value_t::number_unsigned:
16939 return header_size + calc_bson_unsigned_size(j.m_data.m_value.number_unsigned);
16940
16941 case value_t::string:
16942 return header_size + calc_bson_string_size(*j.m_data.m_value.string);
16943
16944 case value_t::null:
16945 return header_size + 0ul;
16946
16947 // LCOV_EXCL_START
16948 case value_t::discarded:
16949 default:
16950 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert)
16951 return 0ul;
16952 // LCOV_EXCL_STOP
16953 }
16954 }
16955
16956 /*!
16957 @brief Serializes the JSON value @a j to BSON and associates it with the
16958 key @a name.
16959 @param name The name to associate with the JSON entity @a j within the
16960 current BSON document
16961 */
16962 void write_bson_element(const string_t& name,
16963 const BasicJsonType& j)
16964 {
16965 switch (j.type())
16966 {
16967 case value_t::object:
16968 return write_bson_object_entry(name, *j.m_data.m_value.object);
16969
16970 case value_t::array:
16971 return write_bson_array(name, *j.m_data.m_value.array);
16972
16973 case value_t::binary:
16974 return write_bson_binary(name, *j.m_data.m_value.binary);
16975
16976 case value_t::boolean:
16977 return write_bson_boolean(name, j.m_data.m_value.boolean);
16978
16979 case value_t::number_float:
16980 return write_bson_double(name, j.m_data.m_value.number_float);
16981
16982 case value_t::number_integer:
16983 return write_bson_integer(name, j.m_data.m_value.number_integer);
16984
16985 case value_t::number_unsigned:
16986 return write_bson_unsigned(name, j);
16987
16988 case value_t::string:
16989 return write_bson_string(name, *j.m_data.m_value.string);
16990
16991 case value_t::null:
16992 return write_bson_null(name);
16993
16994 // LCOV_EXCL_START
16995 case value_t::discarded:
16996 default:
16997 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert)
16998 return;
16999 // LCOV_EXCL_STOP
17000 }
17001 }
17002
17003 /*!
17004 @brief Calculates the size of the BSON serialization of the given
17005 JSON-object @a j.
17006 @param[in] value JSON value to serialize
17007 @pre value.type() == value_t::object
17008 */
17009 static std::size_t calc_bson_object_size(const typename BasicJsonType::object_t& value)
17010 {
17011 const std::size_t document_size = std::accumulate(value.begin(), value.end(), static_cast<std::size_t>(0),
17012 [](size_t result, const typename BasicJsonType::object_t::value_type & el)
17013 {
17014 return result += calc_bson_element_size(el.first, el.second);
17015 });
17016
17017 return sizeof(std::int32_t) + document_size + 1ul;
17018 }
17019
17020 /*!
17021 @param[in] value JSON value to serialize
17022 @pre value.type() == value_t::object
17023 */
17024 void write_bson_object(const typename BasicJsonType::object_t& value)
17025 {
17026 write_number<std::int32_t>(static_cast<std::int32_t>(calc_bson_object_size(value)), true);
17027
17028 for (const auto& el : value)
17029 {
17030 write_bson_element(el.first, el.second);
17031 }
17032
17033 oa->write_character(to_char_type(0x00));
17034 }
17035
17036 //////////
17037 // CBOR //
17038 //////////
17039
17040 static constexpr CharType get_cbor_float_prefix(float /*unused*/)
17041 {
17042 return to_char_type(0xFA); // Single-Precision Float
17043 }
17044
17045 static constexpr CharType get_cbor_float_prefix(double /*unused*/)
17046 {
17047 return to_char_type(0xFB); // Double-Precision Float
17048 }
17049
17050 /////////////
17051 // MsgPack //
17052 /////////////
17053
17054 static constexpr CharType get_msgpack_float_prefix(float /*unused*/)
17055 {
17056 return to_char_type(0xCA); // float 32
17057 }
17058
17059 static constexpr CharType get_msgpack_float_prefix(double /*unused*/)
17060 {
17061 return to_char_type(0xCB); // float 64
17062 }
17063
17064 ////////////
17065 // UBJSON //
17066 ////////////
17067
17068 // UBJSON: write number (floating point)
17069 template<typename NumberType, typename std::enable_if<
17070 std::is_floating_point<NumberType>::value, int>::type = 0>
17071 void write_number_with_ubjson_prefix(const NumberType n,
17072 const bool add_prefix,
17073 const bool use_bjdata)
17074 {
17075 if (add_prefix)
17076 {
17077 oa->write_character(get_ubjson_float_prefix(n));
17078 }
17079 write_number(n, use_bjdata);
17080 }
17081
17082 // UBJSON: write number (unsigned integer)
17083 template<typename NumberType, typename std::enable_if<
17084 std::is_unsigned<NumberType>::value, int>::type = 0>
17085 void write_number_with_ubjson_prefix(const NumberType n,
17086 const bool add_prefix,
17087 const bool use_bjdata)
17088 {
17089 if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
17090 {
17091 if (add_prefix)
17092 {
17093 oa->write_character(to_char_type('i')); // int8
17094 }
17095 write_number(static_cast<std::uint8_t>(n), use_bjdata);
17096 }
17097 else if (n <= (std::numeric_limits<std::uint8_t>::max)())
17098 {
17099 if (add_prefix)
17100 {
17101 oa->write_character(to_char_type('U')); // uint8
17102 }
17103 write_number(static_cast<std::uint8_t>(n), use_bjdata);
17104 }
17105 else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
17106 {
17107 if (add_prefix)
17108 {
17109 oa->write_character(to_char_type('I')); // int16
17110 }
17111 write_number(static_cast<std::int16_t>(n), use_bjdata);
17112 }
17113 else if (use_bjdata && n <= static_cast<uint64_t>((std::numeric_limits<uint16_t>::max)()))
17114 {
17115 if (add_prefix)
17116 {
17117 oa->write_character(to_char_type('u')); // uint16 - bjdata only
17118 }
17119 write_number(static_cast<std::uint16_t>(n), use_bjdata);
17120 }
17121 else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
17122 {
17123 if (add_prefix)
17124 {
17125 oa->write_character(to_char_type('l')); // int32
17126 }
17127 write_number(static_cast<std::int32_t>(n), use_bjdata);
17128 }
17129 else if (use_bjdata && n <= static_cast<uint64_t>((std::numeric_limits<uint32_t>::max)()))
17130 {
17131 if (add_prefix)
17132 {
17133 oa->write_character(to_char_type('m')); // uint32 - bjdata only
17134 }
17135 write_number(static_cast<std::uint32_t>(n), use_bjdata);
17136 }
17137 else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
17138 {
17139 if (add_prefix)
17140 {
17141 oa->write_character(to_char_type('L')); // int64
17142 }
17143 write_number(static_cast<std::int64_t>(n), use_bjdata);
17144 }
17145 else if (use_bjdata && n <= (std::numeric_limits<uint64_t>::max)())
17146 {
17147 if (add_prefix)
17148 {
17149 oa->write_character(to_char_type('M')); // uint64 - bjdata only
17150 }
17151 write_number(static_cast<std::uint64_t>(n), use_bjdata);
17152 }
17153 else
17154 {
17155 if (add_prefix)
17156 {
17157 oa->write_character(to_char_type('H')); // high-precision number
17158 }
17159
17160 const auto number = BasicJsonType(n).dump();
17161 write_number_with_ubjson_prefix(number.size(), true, use_bjdata);
17162 for (std::size_t i = 0; i < number.size(); ++i)
17163 {
17164 oa->write_character(to_char_type(static_cast<std::uint8_t>(number[i])));
17165 }
17166 }
17167 }
17168
17169 // UBJSON: write number (signed integer)
17170 template < typename NumberType, typename std::enable_if <
17171 std::is_signed<NumberType>::value&&
17172 !std::is_floating_point<NumberType>::value, int >::type = 0 >
17173 void write_number_with_ubjson_prefix(const NumberType n,
17174 const bool add_prefix,
17175 const bool use_bjdata)
17176 {
17177 if ((std::numeric_limits<std::int8_t>::min)() <= n && n <= (std::numeric_limits<std::int8_t>::max)())
17178 {
17179 if (add_prefix)
17180 {
17181 oa->write_character(to_char_type('i')); // int8
17182 }
17183 write_number(static_cast<std::int8_t>(n), use_bjdata);
17184 }
17185 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)()))
17186 {
17187 if (add_prefix)
17188 {
17189 oa->write_character(to_char_type('U')); // uint8
17190 }
17191 write_number(static_cast<std::uint8_t>(n), use_bjdata);
17192 }
17193 else if ((std::numeric_limits<std::int16_t>::min)() <= n && n <= (std::numeric_limits<std::int16_t>::max)())
17194 {
17195 if (add_prefix)
17196 {
17197 oa->write_character(to_char_type('I')); // int16
17198 }
17199 write_number(static_cast<std::int16_t>(n), use_bjdata);
17200 }
17201 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)())))
17202 {
17203 if (add_prefix)
17204 {
17205 oa->write_character(to_char_type('u')); // uint16 - bjdata only
17206 }
17207 write_number(static_cast<uint16_t>(n), use_bjdata);
17208 }
17209 else if ((std::numeric_limits<std::int32_t>::min)() <= n && n <= (std::numeric_limits<std::int32_t>::max)())
17210 {
17211 if (add_prefix)
17212 {
17213 oa->write_character(to_char_type('l')); // int32
17214 }
17215 write_number(static_cast<std::int32_t>(n), use_bjdata);
17216 }
17217 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)())))
17218 {
17219 if (add_prefix)
17220 {
17221 oa->write_character(to_char_type('m')); // uint32 - bjdata only
17222 }
17223 write_number(static_cast<uint32_t>(n), use_bjdata);
17224 }
17225 else if ((std::numeric_limits<std::int64_t>::min)() <= n && n <= (std::numeric_limits<std::int64_t>::max)())
17226 {
17227 if (add_prefix)
17228 {
17229 oa->write_character(to_char_type('L')); // int64
17230 }
17231 write_number(static_cast<std::int64_t>(n), use_bjdata);
17232 }
17233 // LCOV_EXCL_START
17234 else
17235 {
17236 if (add_prefix)
17237 {
17238 oa->write_character(to_char_type('H')); // high-precision number
17239 }
17240
17241 const auto number = BasicJsonType(n).dump();
17242 write_number_with_ubjson_prefix(number.size(), true, use_bjdata);
17243 for (std::size_t i = 0; i < number.size(); ++i)
17244 {
17245 oa->write_character(to_char_type(static_cast<std::uint8_t>(number[i])));
17246 }
17247 }
17248 // LCOV_EXCL_STOP
17249 }
17250
17251 /*!
17252 @brief determine the type prefix of container values
17253 */
17254 CharType ubjson_prefix(const BasicJsonType& j, const bool use_bjdata) const noexcept
17255 {
17256 switch (j.type())
17257 {
17258 case value_t::null:
17259 return 'Z';
17260
17261 case value_t::boolean:
17262 return j.m_data.m_value.boolean ? 'T' : 'F';
17263
17264 case value_t::number_integer:
17265 {
17266 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)())
17267 {
17268 return 'i';
17269 }
17270 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)())
17271 {
17272 return 'U';
17273 }
17274 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)())
17275 {
17276 return 'I';
17277 }
17278 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)()))
17279 {
17280 return 'u';
17281 }
17282 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)())
17283 {
17284 return 'l';
17285 }
17286 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)()))
17287 {
17288 return 'm';
17289 }
17290 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)())
17291 {
17292 return 'L';
17293 }
17294 // anything else is treated as high-precision number
17295 return 'H'; // LCOV_EXCL_LINE
17296 }
17297
17298 case value_t::number_unsigned:
17299 {
17300 if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
17301 {
17302 return 'i';
17303 }
17304 if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint8_t>::max)()))
17305 {
17306 return 'U';
17307 }
17308 if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
17309 {
17310 return 'I';
17311 }
17312 if (use_bjdata && j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint16_t>::max)()))
17313 {
17314 return 'u';
17315 }
17316 if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
17317 {
17318 return 'l';
17319 }
17320 if (use_bjdata && j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint32_t>::max)()))
17321 {
17322 return 'm';
17323 }
17324 if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
17325 {
17326 return 'L';
17327 }
17328 if (use_bjdata && j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
17329 {
17330 return 'M';
17331 }
17332 // anything else is treated as high-precision number
17333 return 'H'; // LCOV_EXCL_LINE
17334 }
17335
17336 case value_t::number_float:
17337 return get_ubjson_float_prefix(j.m_data.m_value.number_float);
17338
17339 case value_t::string:
17340 return 'S';
17341
17342 case value_t::array: // fallthrough
17343 case value_t::binary:
17344 return '[';
17345
17346 case value_t::object:
17347 return '{';
17348
17349 case value_t::discarded:
17350 default: // discarded values
17351 return 'N';
17352 }
17353 }
17354
17355 static constexpr CharType get_ubjson_float_prefix(float /*unused*/)
17356 {
17357 return 'd'; // float 32
17358 }
17359
17360 static constexpr CharType get_ubjson_float_prefix(double /*unused*/)
17361 {
17362 return 'D'; // float 64
17363 }
17364
17365 /*!
17366 @return false if the object is successfully converted to a bjdata ndarray, true if the type or size is invalid
17367 */
17368 bool write_bjdata_ndarray(const typename BasicJsonType::object_t& value, const bool use_count, const bool use_type, const bjdata_version_t bjdata_version)
17369 {
17370 std::map<string_t, CharType> bjdtype = {{"uint8", 'U'}, {"int8", 'i'}, {"uint16", 'u'}, {"int16", 'I'},
17371 {"uint32", 'm'}, {"int32", 'l'}, {"uint64", 'M'}, {"int64", 'L'}, {"single", 'd'}, {"double", 'D'},
17372 {"char", 'C'}, {"byte", 'B'}
17373 };
17374
17375 string_t key = "_ArrayType_";
17376 auto it = bjdtype.find(static_cast<string_t>(value.at(key)));
17377 if (it == bjdtype.end())
17378 {
17379 return true;
17380 }
17381 CharType dtype = it->second;
17382
17383 key = "_ArraySize_";
17384 std::size_t len = (value.at(key).empty() ? 0 : 1);
17385 for (const auto& el : value.at(key))
17386 {
17387 len *= static_cast<std::size_t>(el.m_data.m_value.number_unsigned);
17388 }
17389
17390 key = "_ArrayData_";
17391 if (value.at(key).size() != len)
17392 {
17393 return true;
17394 }
17395
17396 oa->write_character('[');
17397 oa->write_character('$');
17398 oa->write_character(dtype);
17399 oa->write_character('#');
17400
17401 key = "_ArraySize_";
17402 write_ubjson(value.at(key), use_count, use_type, true, true, bjdata_version);
17403
17404 key = "_ArrayData_";
17405 if (dtype == 'U' || dtype == 'C' || dtype == 'B')
17406 {
17407 for (const auto& el : value.at(key))
17408 {
17409 write_number(static_cast<std::uint8_t>(el.m_data.m_value.number_unsigned), true);
17410 }
17411 }
17412 else if (dtype == 'i')
17413 {
17414 for (const auto& el : value.at(key))
17415 {
17416 write_number(static_cast<std::int8_t>(el.m_data.m_value.number_integer), true);
17417 }
17418 }
17419 else if (dtype == 'u')
17420 {
17421 for (const auto& el : value.at(key))
17422 {
17423 write_number(static_cast<std::uint16_t>(el.m_data.m_value.number_unsigned), true);
17424 }
17425 }
17426 else if (dtype == 'I')
17427 {
17428 for (const auto& el : value.at(key))
17429 {
17430 write_number(static_cast<std::int16_t>(el.m_data.m_value.number_integer), true);
17431 }
17432 }
17433 else if (dtype == 'm')
17434 {
17435 for (const auto& el : value.at(key))
17436 {
17437 write_number(static_cast<std::uint32_t>(el.m_data.m_value.number_unsigned), true);
17438 }
17439 }
17440 else if (dtype == 'l')
17441 {
17442 for (const auto& el : value.at(key))
17443 {
17444 write_number(static_cast<std::int32_t>(el.m_data.m_value.number_integer), true);
17445 }
17446 }
17447 else if (dtype == 'M')
17448 {
17449 for (const auto& el : value.at(key))
17450 {
17451 write_number(static_cast<std::uint64_t>(el.m_data.m_value.number_unsigned), true);
17452 }
17453 }
17454 else if (dtype == 'L')
17455 {
17456 for (const auto& el : value.at(key))
17457 {
17458 write_number(static_cast<std::int64_t>(el.m_data.m_value.number_integer), true);
17459 }
17460 }
17461 else if (dtype == 'd')
17462 {
17463 for (const auto& el : value.at(key))
17464 {
17465 write_number(static_cast<float>(el.m_data.m_value.number_float), true);
17466 }
17467 }
17468 else if (dtype == 'D')
17469 {
17470 for (const auto& el : value.at(key))
17471 {
17472 write_number(static_cast<double>(el.m_data.m_value.number_float), true);
17473 }
17474 }
17475 return false;
17476 }
17477
17478 ///////////////////////
17479 // Utility functions //
17480 ///////////////////////
17481
17482 /*
17483 @brief write a number to output input
17484 @param[in] n number of type @a NumberType
17485 @param[in] OutputIsLittleEndian Set to true if output data is
17486 required to be little endian
17487 @tparam NumberType the type of the number
17488
17489 @note This function needs to respect the system's endianness, because bytes
17490 in CBOR, MessagePack, and UBJSON are stored in network order (big
17491 endian) and therefore need reordering on little endian systems.
17492 On the other hand, BSON and BJData use little endian and should reorder
17493 on big endian systems.
17494 */
17495 template<typename NumberType>
17496 void write_number(const NumberType n, const bool OutputIsLittleEndian = false)
17497 {
17498 // step 1: write number to array of length NumberType
17499 std::array<CharType, sizeof(NumberType)> vec{};
17500 std::memcpy(vec.data(), &n, sizeof(NumberType));
17501
17502 // step 2: write array to output (with possible reordering)
17503 if (is_little_endian != OutputIsLittleEndian)
17504 {
17505 // reverse byte order prior to conversion if necessary
17506 std::reverse(vec.begin(), vec.end());
17507 }
17508
17509 oa->write_characters(vec.data(), sizeof(NumberType));
17510 }
17511
17512 void write_compact_float(const number_float_t n, detail::input_format_t format)
17513 {
17514 #ifdef __GNUC__
17515 #pragma GCC diagnostic push
17516 #pragma GCC diagnostic ignored "-Wfloat-equal"
17517 #endif
17518 if (static_cast<double>(n) >= static_cast<double>(std::numeric_limits<float>::lowest()) &&
17519 static_cast<double>(n) <= static_cast<double>((std::numeric_limits<float>::max)()) &&
17520 static_cast<double>(static_cast<float>(n)) == static_cast<double>(n))
17521 {
17522 oa->write_character(format == detail::input_format_t::cbor
17523 ? get_cbor_float_prefix(static_cast<float>(n))
17524 : get_msgpack_float_prefix(static_cast<float>(n)));
17525 write_number(static_cast<float>(n));
17526 }
17527 else
17528 {
17529 oa->write_character(format == detail::input_format_t::cbor
17530 ? get_cbor_float_prefix(n)
17531 : get_msgpack_float_prefix(n));
17532 write_number(n);
17533 }
17534 #ifdef __GNUC__
17535 #pragma GCC diagnostic pop
17536 #endif
17537 }
17538
17539 public:
17540 // The following to_char_type functions are implement the conversion
17541 // between uint8_t and CharType. In case CharType is not unsigned,
17542 // such a conversion is required to allow values greater than 128.
17543 // See <https://github.com/nlohmann/json/issues/1286> for a discussion.
17544 template < typename C = CharType,
17545 enable_if_t < std::is_signed<C>::value && std::is_signed<char>::value > * = nullptr >
17546 static constexpr CharType to_char_type(std::uint8_t x) noexcept
17547 {
17548 return *reinterpret_cast<char*>(&x);
17549 }
17550
17551 template < typename C = CharType,
17552 enable_if_t < std::is_signed<C>::value && std::is_unsigned<char>::value > * = nullptr >
17553 static CharType to_char_type(std::uint8_t x) noexcept
17554 {
17555 static_assert(sizeof(std::uint8_t) == sizeof(CharType), "size of CharType must be equal to std::uint8_t");
17556 static_assert(std::is_trivial<CharType>::value, "CharType must be trivial");
17557 CharType result;
17558 std::memcpy(&result, &x, sizeof(x));
17559 return result;
17560 }
17561
17562 template<typename C = CharType,
17563 enable_if_t<std::is_unsigned<C>::value>* = nullptr>
17564 static constexpr CharType to_char_type(std::uint8_t x) noexcept
17565 {
17566 return x;
17567 }
17568
17569 template < typename InputCharType, typename C = CharType,
17570 enable_if_t <
17571 std::is_signed<C>::value &&
17572 std::is_signed<char>::value &&
17573 std::is_same<char, typename std::remove_cv<InputCharType>::type>::value
17574 > * = nullptr >
17575 static constexpr CharType to_char_type(InputCharType x) noexcept
17576 {
17577 return x;
17578 }
17579
17580 private:
17581 /// whether we can assume little endianness
17582 const bool is_little_endian = little_endianness();
17583
17584 /// the output
17585 output_adapter_t<CharType> oa = nullptr;
17586 };
17587
17588 } // namespace detail
17589 NLOHMANN_JSON_NAMESPACE_END
17590
17591 // #include <nlohmann/detail/output/output_adapters.hpp>
17592
17593 // #include <nlohmann/detail/output/serializer.hpp>
17594 // __ _____ _____ _____
17595 // __| | __| | | | JSON for Modern C++
17596 // | | |__ | | | | | | version 3.12.0
17597 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
17598 //
17599 // SPDX-FileCopyrightText: 2008 - 2009 Björn Hoehrmann <[email protected]>
17600 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
17601 // SPDX-License-Identifier: MIT
17602
17603
17604
17605 #include <algorithm> // reverse, remove, fill, find, none_of
17606 #include <array> // array
17607 #include <clocale> // localeconv, lconv
17608 #include <cmath> // labs, isfinite, isnan, signbit
17609 #include <cstddef> // size_t, ptrdiff_t
17610 #include <cstdint> // uint8_t
17611 #include <cstdio> // snprintf
17612 #include <limits> // numeric_limits
17613 #include <string> // string, char_traits
17614 #include <iomanip> // setfill, setw
17615 #include <type_traits> // is_same
17616 #include <utility> // move
17617
17618 // #include <nlohmann/detail/conversions/to_chars.hpp>
17619 // __ _____ _____ _____
17620 // __| | __| | | | JSON for Modern C++
17621 // | | |__ | | | | | | version 3.12.0
17622 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
17623 //
17624 // SPDX-FileCopyrightText: 2009 Florian Loitsch <https://florian.loitsch.com/>
17625 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
17626 // SPDX-License-Identifier: MIT
17627
17628
17629
17630 #include <array> // array
17631 #include <cmath> // signbit, isfinite
17632 #include <cstdint> // intN_t, uintN_t
17633 #include <cstring> // memcpy, memmove
17634 #include <limits> // numeric_limits
17635 #include <type_traits> // conditional
17636
17637 // #include <nlohmann/detail/macro_scope.hpp>
17638
17639
17640 NLOHMANN_JSON_NAMESPACE_BEGIN
17641 namespace detail
17642 {
17643
17644 /*!
17645 @brief implements the Grisu2 algorithm for binary to decimal floating-point
17646 conversion.
17647
17648 This implementation is a slightly modified version of the reference
17649 implementation which may be obtained from
17650 http://florian.loitsch.com/publications (bench.tar.gz).
17651
17652 The code is distributed under the MIT license, Copyright (c) 2009 Florian Loitsch.
17653
17654 For a detailed description of the algorithm see:
17655
17656 [1] Loitsch, "Printing Floating-Point Numbers Quickly and Accurately with
17657 Integers", Proceedings of the ACM SIGPLAN 2010 Conference on Programming
17658 Language Design and Implementation, PLDI 2010
17659 [2] Burger, Dybvig, "Printing Floating-Point Numbers Quickly and Accurately",
17660 Proceedings of the ACM SIGPLAN 1996 Conference on Programming Language
17661 Design and Implementation, PLDI 1996
17662 */
17663 namespace dtoa_impl
17664 {
17665
17666 template<typename Target, typename Source>
17667 Target reinterpret_bits(const Source source)
17668 {
17669 static_assert(sizeof(Target) == sizeof(Source), "size mismatch");
17670
17671 Target target;
17672 std::memcpy(&target, &source, sizeof(Source));
17673 return target;
17674 }
17675
17676 struct diyfp // f * 2^e
17677 {
17678 static constexpr int kPrecision = 64; // = q
17679
17680 std::uint64_t f = 0;
17681 int e = 0;
17682
17683 constexpr diyfp(std::uint64_t f_, int e_) noexcept : f(f_), e(e_) {}
17684
17685 /*!
17686 @brief returns x - y
17687 @pre x.e == y.e and x.f >= y.f
17688 */
17689 static diyfp sub(const diyfp& x, const diyfp& y) noexcept
17690 {
17691 JSON_ASSERT(x.e == y.e);
17692 JSON_ASSERT(x.f >= y.f);
17693
17694 return {x.f - y.f, x.e};
17695 }
17696
17697 /*!
17698 @brief returns x * y
17699 @note The result is rounded. (Only the upper q bits are returned.)
17700 */
17701 static diyfp mul(const diyfp& x, const diyfp& y) noexcept
17702 {
17703 static_assert(kPrecision == 64, "internal error");
17704
17705 // Computes:
17706 // f = round((x.f * y.f) / 2^q)
17707 // e = x.e + y.e + q
17708
17709 // Emulate the 64-bit * 64-bit multiplication:
17710 //
17711 // p = u * v
17712 // = (u_lo + 2^32 u_hi) (v_lo + 2^32 v_hi)
17713 // = (u_lo v_lo ) + 2^32 ((u_lo v_hi ) + (u_hi v_lo )) + 2^64 (u_hi v_hi )
17714 // = (p0 ) + 2^32 ((p1 ) + (p2 )) + 2^64 (p3 )
17715 // = (p0_lo + 2^32 p0_hi) + 2^32 ((p1_lo + 2^32 p1_hi) + (p2_lo + 2^32 p2_hi)) + 2^64 (p3 )
17716 // = (p0_lo ) + 2^32 (p0_hi + p1_lo + p2_lo ) + 2^64 (p1_hi + p2_hi + p3)
17717 // = (p0_lo ) + 2^32 (Q ) + 2^64 (H )
17718 // = (p0_lo ) + 2^32 (Q_lo + 2^32 Q_hi ) + 2^64 (H )
17719 //
17720 // (Since Q might be larger than 2^32 - 1)
17721 //
17722 // = (p0_lo + 2^32 Q_lo) + 2^64 (Q_hi + H)
17723 //
17724 // (Q_hi + H does not overflow a 64-bit int)
17725 //
17726 // = p_lo + 2^64 p_hi
17727
17728 const std::uint64_t u_lo = x.f & 0xFFFFFFFFu;
17729 const std::uint64_t u_hi = x.f >> 32u;
17730 const std::uint64_t v_lo = y.f & 0xFFFFFFFFu;
17731 const std::uint64_t v_hi = y.f >> 32u;
17732
17733 const std::uint64_t p0 = u_lo * v_lo;
17734 const std::uint64_t p1 = u_lo * v_hi;
17735 const std::uint64_t p2 = u_hi * v_lo;
17736 const std::uint64_t p3 = u_hi * v_hi;
17737
17738 const std::uint64_t p0_hi = p0 >> 32u;
17739 const std::uint64_t p1_lo = p1 & 0xFFFFFFFFu;
17740 const std::uint64_t p1_hi = p1 >> 32u;
17741 const std::uint64_t p2_lo = p2 & 0xFFFFFFFFu;
17742 const std::uint64_t p2_hi = p2 >> 32u;
17743
17744 std::uint64_t Q = p0_hi + p1_lo + p2_lo;
17745
17746 // The full product might now be computed as
17747 //
17748 // p_hi = p3 + p2_hi + p1_hi + (Q >> 32)
17749 // p_lo = p0_lo + (Q << 32)
17750 //
17751 // But in this particular case here, the full p_lo is not required.
17752 // Effectively we only need to add the highest bit in p_lo to p_hi (and
17753 // Q_hi + 1 does not overflow).
17754
17755 Q += std::uint64_t{1} << (64u - 32u - 1u); // round, ties up
17756
17757 const std::uint64_t h = p3 + p2_hi + p1_hi + (Q >> 32u);
17758
17759 return {h, x.e + y.e + 64};
17760 }
17761
17762 /*!
17763 @brief normalize x such that the significand is >= 2^(q-1)
17764 @pre x.f != 0
17765 */
17766 static diyfp normalize(diyfp x) noexcept
17767 {
17768 JSON_ASSERT(x.f != 0);
17769
17770 while ((x.f >> 63u) == 0)
17771 {
17772 x.f <<= 1u;
17773 x.e--;
17774 }
17775
17776 return x;
17777 }
17778
17779 /*!
17780 @brief normalize x such that the result has the exponent E
17781 @pre e >= x.e and the upper e - x.e bits of x.f must be zero.
17782 */
17783 static diyfp normalize_to(const diyfp& x, const int target_exponent) noexcept
17784 {
17785 const int delta = x.e - target_exponent;
17786
17787 JSON_ASSERT(delta >= 0);
17788 JSON_ASSERT(((x.f << delta) >> delta) == x.f);
17789
17790 return {x.f << delta, target_exponent};
17791 }
17792 };
17793
17794 struct boundaries
17795 {
17796 diyfp w;
17797 diyfp minus;
17798 diyfp plus;
17799 };
17800
17801 /*!
17802 Compute the (normalized) diyfp representing the input number 'value' and its
17803 boundaries.
17804
17805 @pre value must be finite and positive
17806 */
17807 template<typename FloatType>
17808 boundaries compute_boundaries(FloatType value)
17809 {
17810 JSON_ASSERT(std::isfinite(value));
17811 JSON_ASSERT(value > 0);
17812
17813 // Convert the IEEE representation into a diyfp.
17814 //
17815 // If v is denormal:
17816 // value = 0.F * 2^(1 - bias) = ( F) * 2^(1 - bias - (p-1))
17817 // If v is normalized:
17818 // value = 1.F * 2^(E - bias) = (2^(p-1) + F) * 2^(E - bias - (p-1))
17819
17820 static_assert(std::numeric_limits<FloatType>::is_iec559,
17821 "internal error: dtoa_short requires an IEEE-754 floating-point implementation");
17822
17823 constexpr int kPrecision = std::numeric_limits<FloatType>::digits; // = p (includes the hidden bit)
17824 constexpr int kBias = std::numeric_limits<FloatType>::max_exponent - 1 + (kPrecision - 1);
17825 constexpr int kMinExp = 1 - kBias;
17826 constexpr std::uint64_t kHiddenBit = std::uint64_t{1} << (kPrecision - 1); // = 2^(p-1)
17827
17828 using bits_type = typename std::conditional<kPrecision == 24, std::uint32_t, std::uint64_t >::type;
17829
17830 const auto bits = static_cast<std::uint64_t>(reinterpret_bits<bits_type>(value));
17831 const std::uint64_t E = bits >> (kPrecision - 1);
17832 const std::uint64_t F = bits & (kHiddenBit - 1);
17833
17834 const bool is_denormal = E == 0;
17835 const diyfp v = is_denormal
17836 ? diyfp(F, kMinExp)
17837 : diyfp(F + kHiddenBit, static_cast<int>(E) - kBias);
17838
17839 // Compute the boundaries m- and m+ of the floating-point value
17840 // v = f * 2^e.
17841 //
17842 // Determine v- and v+, the floating-point predecessor and successor if v,
17843 // respectively.
17844 //
17845 // v- = v - 2^e if f != 2^(p-1) or e == e_min (A)
17846 // = v - 2^(e-1) if f == 2^(p-1) and e > e_min (B)
17847 //
17848 // v+ = v + 2^e
17849 //
17850 // Let m- = (v- + v) / 2 and m+ = (v + v+) / 2. All real numbers _strictly_
17851 // between m- and m+ round to v, regardless of how the input rounding
17852 // algorithm breaks ties.
17853 //
17854 // ---+-------------+-------------+-------------+-------------+--- (A)
17855 // v- m- v m+ v+
17856 //
17857 // -----------------+------+------+-------------+-------------+--- (B)
17858 // v- m- v m+ v+
17859
17860 const bool lower_boundary_is_closer = F == 0 && E > 1;
17861 const diyfp m_plus = diyfp((2 * v.f) + 1, v.e - 1);
17862 const diyfp m_minus = lower_boundary_is_closer
17863 ? diyfp((4 * v.f) - 1, v.e - 2) // (B)
17864 : diyfp((2 * v.f) - 1, v.e - 1); // (A)
17865
17866 // Determine the normalized w+ = m+.
17867 const diyfp w_plus = diyfp::normalize(m_plus);
17868
17869 // Determine w- = m- such that e_(w-) = e_(w+).
17870 const diyfp w_minus = diyfp::normalize_to(m_minus, w_plus.e);
17871
17872 return {diyfp::normalize(v), w_minus, w_plus};
17873 }
17874
17875 // Given normalized diyfp w, Grisu needs to find a (normalized) cached
17876 // power-of-ten c, such that the exponent of the product c * w = f * 2^e lies
17877 // within a certain range [alpha, gamma] (Definition 3.2 from [1])
17878 //
17879 // alpha <= e = e_c + e_w + q <= gamma
17880 //
17881 // or
17882 //
17883 // f_c * f_w * 2^alpha <= f_c 2^(e_c) * f_w 2^(e_w) * 2^q
17884 // <= f_c * f_w * 2^gamma
17885 //
17886 // Since c and w are normalized, i.e. 2^(q-1) <= f < 2^q, this implies
17887 //
17888 // 2^(q-1) * 2^(q-1) * 2^alpha <= c * w * 2^q < 2^q * 2^q * 2^gamma
17889 //
17890 // or
17891 //
17892 // 2^(q - 2 + alpha) <= c * w < 2^(q + gamma)
17893 //
17894 // The choice of (alpha,gamma) determines the size of the table and the form of
17895 // the digit generation procedure. Using (alpha,gamma)=(-60,-32) works out well
17896 // in practice:
17897 //
17898 // The idea is to cut the number c * w = f * 2^e into two parts, which can be
17899 // processed independently: An integral part p1, and a fractional part p2:
17900 //
17901 // f * 2^e = ( (f div 2^-e) * 2^-e + (f mod 2^-e) ) * 2^e
17902 // = (f div 2^-e) + (f mod 2^-e) * 2^e
17903 // = p1 + p2 * 2^e
17904 //
17905 // The conversion of p1 into decimal form requires a series of divisions and
17906 // modulos by (a power of) 10. These operations are faster for 32-bit than for
17907 // 64-bit integers, so p1 should ideally fit into a 32-bit integer. This can be
17908 // achieved by choosing
17909 //
17910 // -e >= 32 or e <= -32 := gamma
17911 //
17912 // In order to convert the fractional part
17913 //
17914 // p2 * 2^e = p2 / 2^-e = d[-1] / 10^1 + d[-2] / 10^2 + ...
17915 //
17916 // into decimal form, the fraction is repeatedly multiplied by 10 and the digits
17917 // d[-i] are extracted in order:
17918 //
17919 // (10 * p2) div 2^-e = d[-1]
17920 // (10 * p2) mod 2^-e = d[-2] / 10^1 + ...
17921 //
17922 // The multiplication by 10 must not overflow. It is sufficient to choose
17923 //
17924 // 10 * p2 < 16 * p2 = 2^4 * p2 <= 2^64.
17925 //
17926 // Since p2 = f mod 2^-e < 2^-e,
17927 //
17928 // -e <= 60 or e >= -60 := alpha
17929
17930 constexpr int kAlpha = -60;
17931 constexpr int kGamma = -32;
17932
17933 struct cached_power // c = f * 2^e ~= 10^k
17934 {
17935 std::uint64_t f;
17936 int e;
17937 int k;
17938 };
17939
17940 /*!
17941 For a normalized diyfp w = f * 2^e, this function returns a (normalized) cached
17942 power-of-ten c = f_c * 2^e_c, such that the exponent of the product w * c
17943 satisfies (Definition 3.2 from [1])
17944
17945 alpha <= e_c + e + q <= gamma.
17946 */
17947 inline cached_power get_cached_power_for_binary_exponent(int e)
17948 {
17949 // Now
17950 //
17951 // alpha <= e_c + e + q <= gamma (1)
17952 // ==> f_c * 2^alpha <= c * 2^e * 2^q
17953 //
17954 // and since the c's are normalized, 2^(q-1) <= f_c,
17955 //
17956 // ==> 2^(q - 1 + alpha) <= c * 2^(e + q)
17957 // ==> 2^(alpha - e - 1) <= c
17958 //
17959 // If c were an exact power of ten, i.e. c = 10^k, one may determine k as
17960 //
17961 // k = ceil( log_10( 2^(alpha - e - 1) ) )
17962 // = ceil( (alpha - e - 1) * log_10(2) )
17963 //
17964 // From the paper:
17965 // "In theory the result of the procedure could be wrong since c is rounded,
17966 // and the computation itself is approximated [...]. In practice, however,
17967 // this simple function is sufficient."
17968 //
17969 // For IEEE double precision floating-point numbers converted into
17970 // normalized diyfp's w = f * 2^e, with q = 64,
17971 //
17972 // e >= -1022 (min IEEE exponent)
17973 // -52 (p - 1)
17974 // -52 (p - 1, possibly normalize denormal IEEE numbers)
17975 // -11 (normalize the diyfp)
17976 // = -1137
17977 //
17978 // and
17979 //
17980 // e <= +1023 (max IEEE exponent)
17981 // -52 (p - 1)
17982 // -11 (normalize the diyfp)
17983 // = 960
17984 //
17985 // This binary exponent range [-1137,960] results in a decimal exponent
17986 // range [-307,324]. One does not need to store a cached power for each
17987 // k in this range. For each such k it suffices to find a cached power
17988 // such that the exponent of the product lies in [alpha,gamma].
17989 // This implies that the difference of the decimal exponents of adjacent
17990 // table entries must be less than or equal to
17991 //
17992 // floor( (gamma - alpha) * log_10(2) ) = 8.
17993 //
17994 // (A smaller distance gamma-alpha would require a larger table.)
17995
17996 // NB:
17997 // Actually this function returns c, such that -60 <= e_c + e + 64 <= -34.
17998
17999 constexpr int kCachedPowersMinDecExp = -300;
18000 constexpr int kCachedPowersDecStep = 8;
18001
18002 static constexpr std::array<cached_power, 79> kCachedPowers =
18003 {
18004 {
18005 { 0xAB70FE17C79AC6CA, -1060, -300 },
18006 { 0xFF77B1FCBEBCDC4F, -1034, -292 },
18007 { 0xBE5691EF416BD60C, -1007, -284 },
18008 { 0x8DD01FAD907FFC3C, -980, -276 },
18009 { 0xD3515C2831559A83, -954, -268 },
18010 { 0x9D71AC8FADA6C9B5, -927, -260 },
18011 { 0xEA9C227723EE8BCB, -901, -252 },
18012 { 0xAECC49914078536D, -874, -244 },
18013 { 0x823C12795DB6CE57, -847, -236 },
18014 { 0xC21094364DFB5637, -821, -228 },
18015 { 0x9096EA6F3848984F, -794, -220 },
18016 { 0xD77485CB25823AC7, -768, -212 },
18017 { 0xA086CFCD97BF97F4, -741, -204 },
18018 { 0xEF340A98172AACE5, -715, -196 },
18019 { 0xB23867FB2A35B28E, -688, -188 },
18020 { 0x84C8D4DFD2C63F3B, -661, -180 },
18021 { 0xC5DD44271AD3CDBA, -635, -172 },
18022 { 0x936B9FCEBB25C996, -608, -164 },
18023 { 0xDBAC6C247D62A584, -582, -156 },
18024 { 0xA3AB66580D5FDAF6, -555, -148 },
18025 { 0xF3E2F893DEC3F126, -529, -140 },
18026 { 0xB5B5ADA8AAFF80B8, -502, -132 },
18027 { 0x87625F056C7C4A8B, -475, -124 },
18028 { 0xC9BCFF6034C13053, -449, -116 },
18029 { 0x964E858C91BA2655, -422, -108 },
18030 { 0xDFF9772470297EBD, -396, -100 },
18031 { 0xA6DFBD9FB8E5B88F, -369, -92 },
18032 { 0xF8A95FCF88747D94, -343, -84 },
18033 { 0xB94470938FA89BCF, -316, -76 },
18034 { 0x8A08F0F8BF0F156B, -289, -68 },
18035 { 0xCDB02555653131B6, -263, -60 },
18036 { 0x993FE2C6D07B7FAC, -236, -52 },
18037 { 0xE45C10C42A2B3B06, -210, -44 },
18038 { 0xAA242499697392D3, -183, -36 },
18039 { 0xFD87B5F28300CA0E, -157, -28 },
18040 { 0xBCE5086492111AEB, -130, -20 },
18041 { 0x8CBCCC096F5088CC, -103, -12 },
18042 { 0xD1B71758E219652C, -77, -4 },
18043 { 0x9C40000000000000, -50, 4 },
18044 { 0xE8D4A51000000000, -24, 12 },
18045 { 0xAD78EBC5AC620000, 3, 20 },
18046 { 0x813F3978F8940984, 30, 28 },
18047 { 0xC097CE7BC90715B3, 56, 36 },
18048 { 0x8F7E32CE7BEA5C70, 83, 44 },
18049 { 0xD5D238A4ABE98068, 109, 52 },
18050 { 0x9F4F2726179A2245, 136, 60 },
18051 { 0xED63A231D4C4FB27, 162, 68 },
18052 { 0xB0DE65388CC8ADA8, 189, 76 },
18053 { 0x83C7088E1AAB65DB, 216, 84 },
18054 { 0xC45D1DF942711D9A, 242, 92 },
18055 { 0x924D692CA61BE758, 269, 100 },
18056 { 0xDA01EE641A708DEA, 295, 108 },
18057 { 0xA26DA3999AEF774A, 322, 116 },
18058 { 0xF209787BB47D6B85, 348, 124 },
18059 { 0xB454E4A179DD1877, 375, 132 },
18060 { 0x865B86925B9BC5C2, 402, 140 },
18061 { 0xC83553C5C8965D3D, 428, 148 },
18062 { 0x952AB45CFA97A0B3, 455, 156 },
18063 { 0xDE469FBD99A05FE3, 481, 164 },
18064 { 0xA59BC234DB398C25, 508, 172 },
18065 { 0xF6C69A72A3989F5C, 534, 180 },
18066 { 0xB7DCBF5354E9BECE, 561, 188 },
18067 { 0x88FCF317F22241E2, 588, 196 },
18068 { 0xCC20CE9BD35C78A5, 614, 204 },
18069 { 0x98165AF37B2153DF, 641, 212 },
18070 { 0xE2A0B5DC971F303A, 667, 220 },
18071 { 0xA8D9D1535CE3B396, 694, 228 },
18072 { 0xFB9B7CD9A4A7443C, 720, 236 },
18073 { 0xBB764C4CA7A44410, 747, 244 },
18074 { 0x8BAB8EEFB6409C1A, 774, 252 },
18075 { 0xD01FEF10A657842C, 800, 260 },
18076 { 0x9B10A4E5E9913129, 827, 268 },
18077 { 0xE7109BFBA19C0C9D, 853, 276 },
18078 { 0xAC2820D9623BF429, 880, 284 },
18079 { 0x80444B5E7AA7CF85, 907, 292 },
18080 { 0xBF21E44003ACDD2D, 933, 300 },
18081 { 0x8E679C2F5E44FF8F, 960, 308 },
18082 { 0xD433179D9C8CB841, 986, 316 },
18083 { 0x9E19DB92B4E31BA9, 1013, 324 },
18084 }
18085 };
18086
18087 // This computation gives exactly the same results for k as
18088 // k = ceil((kAlpha - e - 1) * 0.30102999566398114)
18089 // for |e| <= 1500, but doesn't require floating-point operations.
18090 // NB: log_10(2) ~= 78913 / 2^18
18091 JSON_ASSERT(e >= -1500);
18092 JSON_ASSERT(e <= 1500);
18093 const int f = kAlpha - e - 1;
18094 const int k = ((f * 78913) / (1 << 18)) + static_cast<int>(f > 0);
18095
18096 const int index = (-kCachedPowersMinDecExp + k + (kCachedPowersDecStep - 1)) / kCachedPowersDecStep;
18097 JSON_ASSERT(index >= 0);
18098 JSON_ASSERT(static_cast<std::size_t>(index) < kCachedPowers.size());
18099
18100 const cached_power cached = kCachedPowers[static_cast<std::size_t>(index)];
18101 JSON_ASSERT(kAlpha <= cached.e + e + 64);
18102 JSON_ASSERT(kGamma >= cached.e + e + 64);
18103
18104 return cached;
18105 }
18106
18107 /*!
18108 For n != 0, returns k, such that pow10 := 10^(k-1) <= n < 10^k.
18109 For n == 0, returns 1 and sets pow10 := 1.
18110 */
18111 inline int find_largest_pow10(const std::uint32_t n, std::uint32_t& pow10)
18112 {
18113 // LCOV_EXCL_START
18114 if (n >= 1000000000)
18115 {
18116 pow10 = 1000000000;
18117 return 10;
18118 }
18119 // LCOV_EXCL_STOP
18120 if (n >= 100000000)
18121 {
18122 pow10 = 100000000;
18123 return 9;
18124 }
18125 if (n >= 10000000)
18126 {
18127 pow10 = 10000000;
18128 return 8;
18129 }
18130 if (n >= 1000000)
18131 {
18132 pow10 = 1000000;
18133 return 7;
18134 }
18135 if (n >= 100000)
18136 {
18137 pow10 = 100000;
18138 return 6;
18139 }
18140 if (n >= 10000)
18141 {
18142 pow10 = 10000;
18143 return 5;
18144 }
18145 if (n >= 1000)
18146 {
18147 pow10 = 1000;
18148 return 4;
18149 }
18150 if (n >= 100)
18151 {
18152 pow10 = 100;
18153 return 3;
18154 }
18155 if (n >= 10)
18156 {
18157 pow10 = 10;
18158 return 2;
18159 }
18160
18161 pow10 = 1;
18162 return 1;
18163 }
18164
18165 inline void grisu2_round(char* buf, int len, std::uint64_t dist, std::uint64_t delta,
18166 std::uint64_t rest, std::uint64_t ten_k)
18167 {
18168 JSON_ASSERT(len >= 1);
18169 JSON_ASSERT(dist <= delta);
18170 JSON_ASSERT(rest <= delta);
18171 JSON_ASSERT(ten_k > 0);
18172
18173 // <--------------------------- delta ---->
18174 // <---- dist --------->
18175 // --------------[------------------+-------------------]--------------
18176 // M- w M+
18177 //
18178 // ten_k
18179 // <------>
18180 // <---- rest ---->
18181 // --------------[------------------+----+--------------]--------------
18182 // w V
18183 // = buf * 10^k
18184 //
18185 // ten_k represents a unit-in-the-last-place in the decimal representation
18186 // stored in buf.
18187 // Decrement buf by ten_k while this takes buf closer to w.
18188
18189 // The tests are written in this order to avoid overflow in unsigned
18190 // integer arithmetic.
18191
18192 while (rest < dist
18193 && delta - rest >= ten_k
18194 && (rest + ten_k < dist || dist - rest > rest + ten_k - dist))
18195 {
18196 JSON_ASSERT(buf[len - 1] != '0');
18197 buf[len - 1]--;
18198 rest += ten_k;
18199 }
18200 }
18201
18202 /*!
18203 Generates V = buffer * 10^decimal_exponent, such that M- <= V <= M+.
18204 M- and M+ must be normalized and share the same exponent -60 <= e <= -32.
18205 */
18206 inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent,
18207 diyfp M_minus, diyfp w, diyfp M_plus)
18208 {
18209 static_assert(kAlpha >= -60, "internal error");
18210 static_assert(kGamma <= -32, "internal error");
18211
18212 // Generates the digits (and the exponent) of a decimal floating-point
18213 // number V = buffer * 10^decimal_exponent in the range [M-, M+]. The diyfp's
18214 // w, M- and M+ share the same exponent e, which satisfies alpha <= e <= gamma.
18215 //
18216 // <--------------------------- delta ---->
18217 // <---- dist --------->
18218 // --------------[------------------+-------------------]--------------
18219 // M- w M+
18220 //
18221 // Grisu2 generates the digits of M+ from left to right and stops as soon as
18222 // V is in [M-,M+].
18223
18224 JSON_ASSERT(M_plus.e >= kAlpha);
18225 JSON_ASSERT(M_plus.e <= kGamma);
18226
18227 std::uint64_t delta = diyfp::sub(M_plus, M_minus).f; // (significand of (M+ - M-), implicit exponent is e)
18228 std::uint64_t dist = diyfp::sub(M_plus, w ).f; // (significand of (M+ - w ), implicit exponent is e)
18229
18230 // Split M+ = f * 2^e into two parts p1 and p2 (note: e < 0):
18231 //
18232 // M+ = f * 2^e
18233 // = ((f div 2^-e) * 2^-e + (f mod 2^-e)) * 2^e
18234 // = ((p1 ) * 2^-e + (p2 )) * 2^e
18235 // = p1 + p2 * 2^e
18236
18237 const diyfp one(std::uint64_t{1} << -M_plus.e, M_plus.e);
18238
18239 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.)
18240 std::uint64_t p2 = M_plus.f & (one.f - 1); // p2 = f mod 2^-e
18241
18242 // 1)
18243 //
18244 // Generate the digits of the integral part p1 = d[n-1]...d[1]d[0]
18245
18246 JSON_ASSERT(p1 > 0);
18247
18248 std::uint32_t pow10{};
18249 const int k = find_largest_pow10(p1, pow10);
18250
18251 // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1)
18252 //
18253 // p1 = (p1 div 10^(k-1)) * 10^(k-1) + (p1 mod 10^(k-1))
18254 // = (d[k-1] ) * 10^(k-1) + (p1 mod 10^(k-1))
18255 //
18256 // M+ = p1 + p2 * 2^e
18257 // = d[k-1] * 10^(k-1) + (p1 mod 10^(k-1)) + p2 * 2^e
18258 // = d[k-1] * 10^(k-1) + ((p1 mod 10^(k-1)) * 2^-e + p2) * 2^e
18259 // = d[k-1] * 10^(k-1) + ( rest) * 2^e
18260 //
18261 // Now generate the digits d[n] of p1 from left to right (n = k-1,...,0)
18262 //
18263 // p1 = d[k-1]...d[n] * 10^n + d[n-1]...d[0]
18264 //
18265 // but stop as soon as
18266 //
18267 // rest * 2^e = (d[n-1]...d[0] * 2^-e + p2) * 2^e <= delta * 2^e
18268
18269 int n = k;
18270 while (n > 0)
18271 {
18272 // Invariants:
18273 // M+ = buffer * 10^n + (p1 + p2 * 2^e) (buffer = 0 for n = k)
18274 // pow10 = 10^(n-1) <= p1 < 10^n
18275 //
18276 const std::uint32_t d = p1 / pow10; // d = p1 div 10^(n-1)
18277 const std::uint32_t r = p1 % pow10; // r = p1 mod 10^(n-1)
18278 //
18279 // M+ = buffer * 10^n + (d * 10^(n-1) + r) + p2 * 2^e
18280 // = (buffer * 10 + d) * 10^(n-1) + (r + p2 * 2^e)
18281 //
18282 JSON_ASSERT(d <= 9);
18283 buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
18284 //
18285 // M+ = buffer * 10^(n-1) + (r + p2 * 2^e)
18286 //
18287 p1 = r;
18288 n--;
18289 //
18290 // M+ = buffer * 10^n + (p1 + p2 * 2^e)
18291 // pow10 = 10^n
18292 //
18293
18294 // Now check if enough digits have been generated.
18295 // Compute
18296 //
18297 // p1 + p2 * 2^e = (p1 * 2^-e + p2) * 2^e = rest * 2^e
18298 //
18299 // Note:
18300 // Since rest and delta share the same exponent e, it suffices to
18301 // compare the significands.
18302 const std::uint64_t rest = (std::uint64_t{p1} << -one.e) + p2;
18303 if (rest <= delta)
18304 {
18305 // V = buffer * 10^n, with M- <= V <= M+.
18306
18307 decimal_exponent += n;
18308
18309 // We may now just stop. But instead look if the buffer could be
18310 // decremented to bring V closer to w.
18311 //
18312 // pow10 = 10^n is now 1 ulp in the decimal representation V.
18313 // The rounding procedure works with diyfp's with an implicit
18314 // exponent of e.
18315 //
18316 // 10^n = (10^n * 2^-e) * 2^e = ulp * 2^e
18317 //
18318 const std::uint64_t ten_n = std::uint64_t{pow10} << -one.e;
18319 grisu2_round(buffer, length, dist, delta, rest, ten_n);
18320
18321 return;
18322 }
18323
18324 pow10 /= 10;
18325 //
18326 // pow10 = 10^(n-1) <= p1 < 10^n
18327 // Invariants restored.
18328 }
18329
18330 // 2)
18331 //
18332 // The digits of the integral part have been generated:
18333 //
18334 // M+ = d[k-1]...d[1]d[0] + p2 * 2^e
18335 // = buffer + p2 * 2^e
18336 //
18337 // Now generate the digits of the fractional part p2 * 2^e.
18338 //
18339 // Note:
18340 // No decimal point is generated: the exponent is adjusted instead.
18341 //
18342 // p2 actually represents the fraction
18343 //
18344 // p2 * 2^e
18345 // = p2 / 2^-e
18346 // = d[-1] / 10^1 + d[-2] / 10^2 + ...
18347 //
18348 // Now generate the digits d[-m] of p1 from left to right (m = 1,2,...)
18349 //
18350 // p2 * 2^e = d[-1]d[-2]...d[-m] * 10^-m
18351 // + 10^-m * (d[-m-1] / 10^1 + d[-m-2] / 10^2 + ...)
18352 //
18353 // using
18354 //
18355 // 10^m * p2 = ((10^m * p2) div 2^-e) * 2^-e + ((10^m * p2) mod 2^-e)
18356 // = ( d) * 2^-e + ( r)
18357 //
18358 // or
18359 // 10^m * p2 * 2^e = d + r * 2^e
18360 //
18361 // i.e.
18362 //
18363 // M+ = buffer + p2 * 2^e
18364 // = buffer + 10^-m * (d + r * 2^e)
18365 // = (buffer * 10^m + d) * 10^-m + 10^-m * r * 2^e
18366 //
18367 // and stop as soon as 10^-m * r * 2^e <= delta * 2^e
18368
18369 JSON_ASSERT(p2 > delta);
18370
18371 int m = 0;
18372 for (;;)
18373 {
18374 // Invariant:
18375 // M+ = buffer * 10^-m + 10^-m * (d[-m-1] / 10 + d[-m-2] / 10^2 + ...) * 2^e
18376 // = buffer * 10^-m + 10^-m * (p2 ) * 2^e
18377 // = buffer * 10^-m + 10^-m * (1/10 * (10 * p2) ) * 2^e
18378 // = buffer * 10^-m + 10^-m * (1/10 * ((10*p2 div 2^-e) * 2^-e + (10*p2 mod 2^-e)) * 2^e
18379 //
18380 JSON_ASSERT(p2 <= (std::numeric_limits<std::uint64_t>::max)() / 10);
18381 p2 *= 10;
18382 const std::uint64_t d = p2 >> -one.e; // d = (10 * p2) div 2^-e
18383 const std::uint64_t r = p2 & (one.f - 1); // r = (10 * p2) mod 2^-e
18384 //
18385 // M+ = buffer * 10^-m + 10^-m * (1/10 * (d * 2^-e + r) * 2^e
18386 // = buffer * 10^-m + 10^-m * (1/10 * (d + r * 2^e))
18387 // = (buffer * 10 + d) * 10^(-m-1) + 10^(-m-1) * r * 2^e
18388 //
18389 JSON_ASSERT(d <= 9);
18390 buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
18391 //
18392 // M+ = buffer * 10^(-m-1) + 10^(-m-1) * r * 2^e
18393 //
18394 p2 = r;
18395 m++;
18396 //
18397 // M+ = buffer * 10^-m + 10^-m * p2 * 2^e
18398 // Invariant restored.
18399
18400 // Check if enough digits have been generated.
18401 //
18402 // 10^-m * p2 * 2^e <= delta * 2^e
18403 // p2 * 2^e <= 10^m * delta * 2^e
18404 // p2 <= 10^m * delta
18405 delta *= 10;
18406 dist *= 10;
18407 if (p2 <= delta)
18408 {
18409 break;
18410 }
18411 }
18412
18413 // V = buffer * 10^-m, with M- <= V <= M+.
18414
18415 decimal_exponent -= m;
18416
18417 // 1 ulp in the decimal representation is now 10^-m.
18418 // Since delta and dist are now scaled by 10^m, we need to do the
18419 // same with ulp in order to keep the units in sync.
18420 //
18421 // 10^m * 10^-m = 1 = 2^-e * 2^e = ten_m * 2^e
18422 //
18423 const std::uint64_t ten_m = one.f;
18424 grisu2_round(buffer, length, dist, delta, p2, ten_m);
18425
18426 // By construction this algorithm generates the shortest possible decimal
18427 // number (Loitsch, Theorem 6.2) which rounds back to w.
18428 // For an input number of precision p, at least
18429 //
18430 // N = 1 + ceil(p * log_10(2))
18431 //
18432 // decimal digits are sufficient to identify all binary floating-point
18433 // numbers (Matula, "In-and-Out conversions").
18434 // This implies that the algorithm does not produce more than N decimal
18435 // digits.
18436 //
18437 // N = 17 for p = 53 (IEEE double precision)
18438 // N = 9 for p = 24 (IEEE single precision)
18439 }
18440
18441 /*!
18442 v = buf * 10^decimal_exponent
18443 len is the length of the buffer (number of decimal digits)
18444 The buffer must be large enough, i.e. >= max_digits10.
18445 */
18446 JSON_HEDLEY_NON_NULL(1)
18447 inline void grisu2(char* buf, int& len, int& decimal_exponent,
18448 diyfp m_minus, diyfp v, diyfp m_plus)
18449 {
18450 JSON_ASSERT(m_plus.e == m_minus.e);
18451 JSON_ASSERT(m_plus.e == v.e);
18452
18453 // --------(-----------------------+-----------------------)-------- (A)
18454 // m- v m+
18455 //
18456 // --------------------(-----------+-----------------------)-------- (B)
18457 // m- v m+
18458 //
18459 // First scale v (and m- and m+) such that the exponent is in the range
18460 // [alpha, gamma].
18461
18462 const cached_power cached = get_cached_power_for_binary_exponent(m_plus.e);
18463
18464 const diyfp c_minus_k(cached.f, cached.e); // = c ~= 10^-k
18465
18466 // The exponent of the products is = v.e + c_minus_k.e + q and is in the range [alpha,gamma]
18467 const diyfp w = diyfp::mul(v, c_minus_k);
18468 const diyfp w_minus = diyfp::mul(m_minus, c_minus_k);
18469 const diyfp w_plus = diyfp::mul(m_plus, c_minus_k);
18470
18471 // ----(---+---)---------------(---+---)---------------(---+---)----
18472 // w- w w+
18473 // = c*m- = c*v = c*m+
18474 //
18475 // diyfp::mul rounds its result and c_minus_k is approximated too. w, w- and
18476 // w+ are now off by a small amount.
18477 // In fact:
18478 //
18479 // w - v * 10^k < 1 ulp
18480 //
18481 // To account for this inaccuracy, add resp. subtract 1 ulp.
18482 //
18483 // --------+---[---------------(---+---)---------------]---+--------
18484 // w- M- w M+ w+
18485 //
18486 // Now any number in [M-, M+] (bounds included) will round to w when input,
18487 // regardless of how the input rounding algorithm breaks ties.
18488 //
18489 // And digit_gen generates the shortest possible such number in [M-, M+].
18490 // Note that this does not mean that Grisu2 always generates the shortest
18491 // possible number in the interval (m-, m+).
18492 const diyfp M_minus(w_minus.f + 1, w_minus.e);
18493 const diyfp M_plus (w_plus.f - 1, w_plus.e );
18494
18495 decimal_exponent = -cached.k; // = -(-k) = k
18496
18497 grisu2_digit_gen(buf, len, decimal_exponent, M_minus, w, M_plus);
18498 }
18499
18500 /*!
18501 v = buf * 10^decimal_exponent
18502 len is the length of the buffer (number of decimal digits)
18503 The buffer must be large enough, i.e. >= max_digits10.
18504 */
18505 template<typename FloatType>
18506 JSON_HEDLEY_NON_NULL(1)
18507 void grisu2(char* buf, int& len, int& decimal_exponent, FloatType value)
18508 {
18509 static_assert(diyfp::kPrecision >= std::numeric_limits<FloatType>::digits + 3,
18510 "internal error: not enough precision");
18511
18512 JSON_ASSERT(std::isfinite(value));
18513 JSON_ASSERT(value > 0);
18514
18515 // If the neighbors (and boundaries) of 'value' are always computed for double-precision
18516 // numbers, all float's can be recovered using strtod (and strtof). However, the resulting
18517 // decimal representations are not exactly "short".
18518 //
18519 // The documentation for 'std::to_chars' (https://en.cppreference.com/w/cpp/utility/to_chars)
18520 // says "value is converted to a string as if by std::sprintf in the default ("C") locale"
18521 // and since sprintf promotes floats to doubles, I think this is exactly what 'std::to_chars'
18522 // does.
18523 // On the other hand, the documentation for 'std::to_chars' requires that "parsing the
18524 // representation using the corresponding std::from_chars function recovers value exactly". That
18525 // indicates that single precision floating-point numbers should be recovered using
18526 // 'std::strtof'.
18527 //
18528 // NB: If the neighbors are computed for single-precision numbers, there is a single float
18529 // (7.0385307e-26f) which can't be recovered using strtod. The resulting double precision
18530 // value is off by 1 ulp.
18531 #if 0 // NOLINT(readability-avoid-unconditional-preprocessor-if)
18532 const boundaries w = compute_boundaries(static_cast<double>(value));
18533 #else
18534 const boundaries w = compute_boundaries(value);
18535 #endif
18536
18537 grisu2(buf, len, decimal_exponent, w.minus, w.w, w.plus);
18538 }
18539
18540 /*!
18541 @brief appends a decimal representation of e to buf
18542 @return a pointer to the element following the exponent.
18543 @pre -1000 < e < 1000
18544 */
18545 JSON_HEDLEY_NON_NULL(1)
18546 JSON_HEDLEY_RETURNS_NON_NULL
18547 inline char* append_exponent(char* buf, int e)
18548 {
18549 JSON_ASSERT(e > -1000);
18550 JSON_ASSERT(e < 1000);
18551
18552 if (e < 0)
18553 {
18554 e = -e;
18555 *buf++ = '-';
18556 }
18557 else
18558 {
18559 *buf++ = '+';
18560 }
18561
18562 auto k = static_cast<std::uint32_t>(e);
18563 if (k < 10)
18564 {
18565 // Always print at least two digits in the exponent.
18566 // This is for compatibility with printf("%g").
18567 *buf++ = '0';
18568 *buf++ = static_cast<char>('0' + k);
18569 }
18570 else if (k < 100)
18571 {
18572 *buf++ = static_cast<char>('0' + (k / 10));
18573 k %= 10;
18574 *buf++ = static_cast<char>('0' + k);
18575 }
18576 else
18577 {
18578 *buf++ = static_cast<char>('0' + (k / 100));
18579 k %= 100;
18580 *buf++ = static_cast<char>('0' + (k / 10));
18581 k %= 10;
18582 *buf++ = static_cast<char>('0' + k);
18583 }
18584
18585 return buf;
18586 }
18587
18588 /*!
18589 @brief prettify v = buf * 10^decimal_exponent
18590
18591 If v is in the range [10^min_exp, 10^max_exp) it will be printed in fixed-point
18592 notation. Otherwise it will be printed in exponential notation.
18593
18594 @pre min_exp < 0
18595 @pre max_exp > 0
18596 */
18597 JSON_HEDLEY_NON_NULL(1)
18598 JSON_HEDLEY_RETURNS_NON_NULL
18599 inline char* format_buffer(char* buf, int len, int decimal_exponent,
18600 int min_exp, int max_exp)
18601 {
18602 JSON_ASSERT(min_exp < 0);
18603 JSON_ASSERT(max_exp > 0);
18604
18605 const int k = len;
18606 const int n = len + decimal_exponent;
18607
18608 // v = buf * 10^(n-k)
18609 // k is the length of the buffer (number of decimal digits)
18610 // n is the position of the decimal point relative to the start of the buffer.
18611
18612 if (k <= n && n <= max_exp)
18613 {
18614 // digits[000]
18615 // len <= max_exp + 2
18616
18617 std::memset(buf + k, '0', static_cast<size_t>(n) - static_cast<size_t>(k));
18618 // Make it look like a floating-point number (#362, #378)
18619 buf[n + 0] = '.';
18620 buf[n + 1] = '0';
18621 return buf + (static_cast<size_t>(n) + 2);
18622 }
18623
18624 if (0 < n && n <= max_exp)
18625 {
18626 // dig.its
18627 // len <= max_digits10 + 1
18628
18629 JSON_ASSERT(k > n);
18630
18631 std::memmove(buf + (static_cast<size_t>(n) + 1), buf + n, static_cast<size_t>(k) - static_cast<size_t>(n));
18632 buf[n] = '.';
18633 return buf + (static_cast<size_t>(k) + 1U);
18634 }
18635
18636 if (min_exp < n && n <= 0)
18637 {
18638 // 0.[000]digits
18639 // len <= 2 + (-min_exp - 1) + max_digits10
18640
18641 std::memmove(buf + (2 + static_cast<size_t>(-n)), buf, static_cast<size_t>(k));
18642 buf[0] = '0';
18643 buf[1] = '.';
18644 std::memset(buf + 2, '0', static_cast<size_t>(-n));
18645 return buf + (2U + static_cast<size_t>(-n) + static_cast<size_t>(k));
18646 }
18647
18648 if (k == 1)
18649 {
18650 // dE+123
18651 // len <= 1 + 5
18652
18653 buf += 1;
18654 }
18655 else
18656 {
18657 // d.igitsE+123
18658 // len <= max_digits10 + 1 + 5
18659
18660 std::memmove(buf + 2, buf + 1, static_cast<size_t>(k) - 1);
18661 buf[1] = '.';
18662 buf += 1 + static_cast<size_t>(k);
18663 }
18664
18665 *buf++ = 'e';
18666 return append_exponent(buf, n - 1);
18667 }
18668
18669 } // namespace dtoa_impl
18670
18671 /*!
18672 @brief generates a decimal representation of the floating-point number value in [first, last).
18673
18674 The format of the resulting decimal representation is similar to printf's %g
18675 format. Returns an iterator pointing past-the-end of the decimal representation.
18676
18677 @note The input number must be finite, i.e. NaN's and Inf's are not supported.
18678 @note The buffer must be large enough.
18679 @note The result is NOT null-terminated.
18680 */
18681 template<typename FloatType>
18682 JSON_HEDLEY_NON_NULL(1, 2)
18683 JSON_HEDLEY_RETURNS_NON_NULL
18684 char* to_chars(char* first, const char* last, FloatType value)
18685 {
18686 static_cast<void>(last); // maybe unused - fix warning
18687 JSON_ASSERT(std::isfinite(value));
18688
18689 // Use signbit(value) instead of (value < 0) since signbit works for -0.
18690 if (std::signbit(value))
18691 {
18692 value = -value;
18693 *first++ = '-';
18694 }
18695
18696 #ifdef __GNUC__
18697 #pragma GCC diagnostic push
18698 #pragma GCC diagnostic ignored "-Wfloat-equal"
18699 #endif
18700 if (value == 0) // +-0
18701 {
18702 *first++ = '0';
18703 // Make it look like a floating-point number (#362, #378)
18704 *first++ = '.';
18705 *first++ = '0';
18706 return first;
18707 }
18708 #ifdef __GNUC__
18709 #pragma GCC diagnostic pop
18710 #endif
18711
18712 JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10);
18713
18714 // Compute v = buffer * 10^decimal_exponent.
18715 // The decimal digits are stored in the buffer, which needs to be interpreted
18716 // as an unsigned decimal integer.
18717 // len is the length of the buffer, i.e. the number of decimal digits.
18718 int len = 0;
18719 int decimal_exponent = 0;
18720 dtoa_impl::grisu2(first, len, decimal_exponent, value);
18721
18722 JSON_ASSERT(len <= std::numeric_limits<FloatType>::max_digits10);
18723
18724 // Format the buffer like printf("%.*g", prec, value)
18725 constexpr int kMinExp = -4;
18726 // Use digits10 here to increase compatibility with version 2.
18727 constexpr int kMaxExp = std::numeric_limits<FloatType>::digits10;
18728
18729 JSON_ASSERT(last - first >= kMaxExp + 2);
18730 JSON_ASSERT(last - first >= 2 + (-kMinExp - 1) + std::numeric_limits<FloatType>::max_digits10);
18731 JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10 + 6);
18732
18733 return dtoa_impl::format_buffer(first, len, decimal_exponent, kMinExp, kMaxExp);
18734 }
18735
18736 } // namespace detail
18737 NLOHMANN_JSON_NAMESPACE_END
18738
18739 // #include <nlohmann/detail/exceptions.hpp>
18740
18741 // #include <nlohmann/detail/macro_scope.hpp>
18742
18743 // #include <nlohmann/detail/meta/cpp_future.hpp>
18744
18745 // #include <nlohmann/detail/output/binary_writer.hpp>
18746
18747 // #include <nlohmann/detail/output/output_adapters.hpp>
18748
18749 // #include <nlohmann/detail/string_concat.hpp>
18750
18751 // #include <nlohmann/detail/value_t.hpp>
18752
18753
18754 NLOHMANN_JSON_NAMESPACE_BEGIN
18755 namespace detail
18756 {
18757
18758 ///////////////////
18759 // serialization //
18760 ///////////////////
18761
18762 /// how to treat decoding errors
18763 enum class error_handler_t
18764 {
18765 strict, ///< throw a type_error exception in case of invalid UTF-8
18766 replace, ///< replace invalid UTF-8 sequences with U+FFFD
18767 ignore ///< ignore invalid UTF-8 sequences
18768 };
18769
18770 template<typename BasicJsonType>
18771 class serializer
18772 {
18773 using string_t = typename BasicJsonType::string_t;
18774 using number_float_t = typename BasicJsonType::number_float_t;
18775 using number_integer_t = typename BasicJsonType::number_integer_t;
18776 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
18777 using binary_char_t = typename BasicJsonType::binary_t::value_type;
18778 static constexpr std::uint8_t UTF8_ACCEPT = 0;
18779 static constexpr std::uint8_t UTF8_REJECT = 1;
18780
18781 public:
18782 /*!
18783 @param[in] s output stream to serialize to
18784 @param[in] ichar indentation character to use
18785 @param[in] error_handler_ how to react on decoding errors
18786 */
18787 serializer(output_adapter_t<char> s, const char ichar,
18788 error_handler_t error_handler_ = error_handler_t::strict)
18789 : o(std::move(s))
18790 , loc(std::localeconv())
18791 , thousands_sep(loc->thousands_sep == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->thousands_sep)))
18792 , decimal_point(loc->decimal_point == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->decimal_point)))
18793 , indent_char(ichar)
18794 , indent_string(512, indent_char)
18795 , error_handler(error_handler_)
18796 {}
18797
18798 // delete because of pointer members
18799 serializer(const serializer&) = delete;
18800 serializer& operator=(const serializer&) = delete;
18801 serializer(serializer&&) = delete;
18802 serializer& operator=(serializer&&) = delete;
18803 ~serializer() = default;
18804
18805 /*!
18806 @brief internal implementation of the serialization function
18807
18808 This function is called by the public member function dump and organizes
18809 the serialization internally. The indentation level is propagated as
18810 additional parameter. In case of arrays and objects, the function is
18811 called recursively.
18812
18813 - strings and object keys are escaped using `escape_string()`
18814 - integer numbers are converted implicitly via `operator<<`
18815 - floating-point numbers are converted to a string using `"%g"` format
18816 - binary values are serialized as objects containing the subtype and the
18817 byte array
18818
18819 @param[in] val value to serialize
18820 @param[in] pretty_print whether the output shall be pretty-printed
18821 @param[in] ensure_ascii If @a ensure_ascii is true, all non-ASCII characters
18822 in the output are escaped with `\uXXXX` sequences, and the result consists
18823 of ASCII characters only.
18824 @param[in] indent_step the indent level
18825 @param[in] current_indent the current indent level (only used internally)
18826 */
18827 void dump(const BasicJsonType& val,
18828 const bool pretty_print,
18829 const bool ensure_ascii,
18830 const unsigned int indent_step,
18831 const unsigned int current_indent = 0)
18832 {
18833 switch (val.m_data.m_type)
18834 {
18835 case value_t::object:
18836 {
18837 if (val.m_data.m_value.object->empty())
18838 {
18839 o->write_characters("{}", 2);
18840 return;
18841 }
18842
18843 if (pretty_print)
18844 {
18845 o->write_characters("{\n", 2);
18846
18847 // variable to hold indentation for recursive calls
18848 const auto new_indent = current_indent + indent_step;
18849 if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
18850 {
18851 indent_string.resize(indent_string.size() * 2, ' ');
18852 }
18853
18854 // first n-1 elements
18855 auto i = val.m_data.m_value.object->cbegin();
18856 for (std::size_t cnt = 0; cnt < val.m_data.m_value.object->size() - 1; ++cnt, ++i)
18857 {
18858 o->write_characters(indent_string.c_str(), new_indent);
18859 o->write_character('\"');
18860 dump_escaped(i->first, ensure_ascii);
18861 o->write_characters("\": ", 3);
18862 dump(i->second, true, ensure_ascii, indent_step, new_indent);
18863 o->write_characters(",\n", 2);
18864 }
18865
18866 // last element
18867 JSON_ASSERT(i != val.m_data.m_value.object->cend());
18868 JSON_ASSERT(std::next(i) == val.m_data.m_value.object->cend());
18869 o->write_characters(indent_string.c_str(), new_indent);
18870 o->write_character('\"');
18871 dump_escaped(i->first, ensure_ascii);
18872 o->write_characters("\": ", 3);
18873 dump(i->second, true, ensure_ascii, indent_step, new_indent);
18874
18875 o->write_character('\n');
18876 o->write_characters(indent_string.c_str(), current_indent);
18877 o->write_character('}');
18878 }
18879 else
18880 {
18881 o->write_character('{');
18882
18883 // first n-1 elements
18884 auto i = val.m_data.m_value.object->cbegin();
18885 for (std::size_t cnt = 0; cnt < val.m_data.m_value.object->size() - 1; ++cnt, ++i)
18886 {
18887 o->write_character('\"');
18888 dump_escaped(i->first, ensure_ascii);
18889 o->write_characters("\":", 2);
18890 dump(i->second, false, ensure_ascii, indent_step, current_indent);
18891 o->write_character(',');
18892 }
18893
18894 // last element
18895 JSON_ASSERT(i != val.m_data.m_value.object->cend());
18896 JSON_ASSERT(std::next(i) == val.m_data.m_value.object->cend());
18897 o->write_character('\"');
18898 dump_escaped(i->first, ensure_ascii);
18899 o->write_characters("\":", 2);
18900 dump(i->second, false, ensure_ascii, indent_step, current_indent);
18901
18902 o->write_character('}');
18903 }
18904
18905 return;
18906 }
18907
18908 case value_t::array:
18909 {
18910 if (val.m_data.m_value.array->empty())
18911 {
18912 o->write_characters("[]", 2);
18913 return;
18914 }
18915
18916 if (pretty_print)
18917 {
18918 o->write_characters("[\n", 2);
18919
18920 // variable to hold indentation for recursive calls
18921 const auto new_indent = current_indent + indent_step;
18922 if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
18923 {
18924 indent_string.resize(indent_string.size() * 2, ' ');
18925 }
18926
18927 // first n-1 elements
18928 for (auto i = val.m_data.m_value.array->cbegin();
18929 i != val.m_data.m_value.array->cend() - 1; ++i)
18930 {
18931 o->write_characters(indent_string.c_str(), new_indent);
18932 dump(*i, true, ensure_ascii, indent_step, new_indent);
18933 o->write_characters(",\n", 2);
18934 }
18935
18936 // last element
18937 JSON_ASSERT(!val.m_data.m_value.array->empty());
18938 o->write_characters(indent_string.c_str(), new_indent);
18939 dump(val.m_data.m_value.array->back(), true, ensure_ascii, indent_step, new_indent);
18940
18941 o->write_character('\n');
18942 o->write_characters(indent_string.c_str(), current_indent);
18943 o->write_character(']');
18944 }
18945 else
18946 {
18947 o->write_character('[');
18948
18949 // first n-1 elements
18950 for (auto i = val.m_data.m_value.array->cbegin();
18951 i != val.m_data.m_value.array->cend() - 1; ++i)
18952 {
18953 dump(*i, false, ensure_ascii, indent_step, current_indent);
18954 o->write_character(',');
18955 }
18956
18957 // last element
18958 JSON_ASSERT(!val.m_data.m_value.array->empty());
18959 dump(val.m_data.m_value.array->back(), false, ensure_ascii, indent_step, current_indent);
18960
18961 o->write_character(']');
18962 }
18963
18964 return;
18965 }
18966
18967 case value_t::string:
18968 {
18969 o->write_character('\"');
18970 dump_escaped(*val.m_data.m_value.string, ensure_ascii);
18971 o->write_character('\"');
18972 return;
18973 }
18974
18975 case value_t::binary:
18976 {
18977 if (pretty_print)
18978 {
18979 o->write_characters("{\n", 2);
18980
18981 // variable to hold indentation for recursive calls
18982 const auto new_indent = current_indent + indent_step;
18983 if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
18984 {
18985 indent_string.resize(indent_string.size() * 2, ' ');
18986 }
18987
18988 o->write_characters(indent_string.c_str(), new_indent);
18989
18990 o->write_characters("\"bytes\": [", 10);
18991
18992 if (!val.m_data.m_value.binary->empty())
18993 {
18994 for (auto i = val.m_data.m_value.binary->cbegin();
18995 i != val.m_data.m_value.binary->cend() - 1; ++i)
18996 {
18997 dump_integer(*i);
18998 o->write_characters(", ", 2);
18999 }
19000 dump_integer(val.m_data.m_value.binary->back());
19001 }
19002
19003 o->write_characters("],\n", 3);
19004 o->write_characters(indent_string.c_str(), new_indent);
19005
19006 o->write_characters("\"subtype\": ", 11);
19007 if (val.m_data.m_value.binary->has_subtype())
19008 {
19009 dump_integer(val.m_data.m_value.binary->subtype());
19010 }
19011 else
19012 {
19013 o->write_characters("null", 4);
19014 }
19015 o->write_character('\n');
19016 o->write_characters(indent_string.c_str(), current_indent);
19017 o->write_character('}');
19018 }
19019 else
19020 {
19021 o->write_characters("{\"bytes\":[", 10);
19022
19023 if (!val.m_data.m_value.binary->empty())
19024 {
19025 for (auto i = val.m_data.m_value.binary->cbegin();
19026 i != val.m_data.m_value.binary->cend() - 1; ++i)
19027 {
19028 dump_integer(*i);
19029 o->write_character(',');
19030 }
19031 dump_integer(val.m_data.m_value.binary->back());
19032 }
19033
19034 o->write_characters("],\"subtype\":", 12);
19035 if (val.m_data.m_value.binary->has_subtype())
19036 {
19037 dump_integer(val.m_data.m_value.binary->subtype());
19038 o->write_character('}');
19039 }
19040 else
19041 {
19042 o->write_characters("null}", 5);
19043 }
19044 }
19045 return;
19046 }
19047
19048 case value_t::boolean:
19049 {
19050 if (val.m_data.m_value.boolean)
19051 {
19052 o->write_characters("true", 4);
19053 }
19054 else
19055 {
19056 o->write_characters("false", 5);
19057 }
19058 return;
19059 }
19060
19061 case value_t::number_integer:
19062 {
19063 dump_integer(val.m_data.m_value.number_integer);
19064 return;
19065 }
19066
19067 case value_t::number_unsigned:
19068 {
19069 dump_integer(val.m_data.m_value.number_unsigned);
19070 return;
19071 }
19072
19073 case value_t::number_float:
19074 {
19075 dump_float(val.m_data.m_value.number_float);
19076 return;
19077 }
19078
19079 case value_t::discarded:
19080 {
19081 o->write_characters("<discarded>", 11);
19082 return;
19083 }
19084
19085 case value_t::null:
19086 {
19087 o->write_characters("null", 4);
19088 return;
19089 }
19090
19091 default: // LCOV_EXCL_LINE
19092 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
19093 }
19094 }
19095
19096 JSON_PRIVATE_UNLESS_TESTED:
19097 /*!
19098 @brief dump escaped string
19099
19100 Escape a string by replacing certain special characters by a sequence of an
19101 escape character (backslash) and another character and other control
19102 characters by a sequence of "\u" followed by a four-digit hex
19103 representation. The escaped string is written to output stream @a o.
19104
19105 @param[in] s the string to escape
19106 @param[in] ensure_ascii whether to escape non-ASCII characters with
19107 \uXXXX sequences
19108
19109 @complexity Linear in the length of string @a s.
19110 */
19111 void dump_escaped(const string_t& s, const bool ensure_ascii)
19112 {
19113 std::uint32_t codepoint{};
19114 std::uint8_t state = UTF8_ACCEPT;
19115 std::size_t bytes = 0; // number of bytes written to string_buffer
19116
19117 // number of bytes written at the point of the last valid byte
19118 std::size_t bytes_after_last_accept = 0;
19119 std::size_t undumped_chars = 0;
19120
19121 for (std::size_t i = 0; i < s.size(); ++i)
19122 {
19123 const auto byte = static_cast<std::uint8_t>(s[i]);
19124
19125 switch (decode(state, codepoint, byte))
19126 {
19127 case UTF8_ACCEPT: // decode found a new code point
19128 {
19129 switch (codepoint)
19130 {
19131 case 0x08: // backspace
19132 {
19133 string_buffer[bytes++] = '\\';
19134 string_buffer[bytes++] = 'b';
19135 break;
19136 }
19137
19138 case 0x09: // horizontal tab
19139 {
19140 string_buffer[bytes++] = '\\';
19141 string_buffer[bytes++] = 't';
19142 break;
19143 }
19144
19145 case 0x0A: // newline
19146 {
19147 string_buffer[bytes++] = '\\';
19148 string_buffer[bytes++] = 'n';
19149 break;
19150 }
19151
19152 case 0x0C: // formfeed
19153 {
19154 string_buffer[bytes++] = '\\';
19155 string_buffer[bytes++] = 'f';
19156 break;
19157 }
19158
19159 case 0x0D: // carriage return
19160 {
19161 string_buffer[bytes++] = '\\';
19162 string_buffer[bytes++] = 'r';
19163 break;
19164 }
19165
19166 case 0x22: // quotation mark
19167 {
19168 string_buffer[bytes++] = '\\';
19169 string_buffer[bytes++] = '\"';
19170 break;
19171 }
19172
19173 case 0x5C: // reverse solidus
19174 {
19175 string_buffer[bytes++] = '\\';
19176 string_buffer[bytes++] = '\\';
19177 break;
19178 }
19179
19180 default:
19181 {
19182 // escape control characters (0x00..0x1F) or, if
19183 // ensure_ascii parameter is used, non-ASCII characters
19184 if ((codepoint <= 0x1F) || (ensure_ascii && (codepoint >= 0x7F)))
19185 {
19186 if (codepoint <= 0xFFFF)
19187 {
19188 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
19189 static_cast<void>((std::snprintf)(string_buffer.data() + bytes, 7, "\\u%04x",
19190 static_cast<std::uint16_t>(codepoint)));
19191 bytes += 6;
19192 }
19193 else
19194 {
19195 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
19196 static_cast<void>((std::snprintf)(string_buffer.data() + bytes, 13, "\\u%04x\\u%04x",
19197 static_cast<std::uint16_t>(0xD7C0u + (codepoint >> 10u)),
19198 static_cast<std::uint16_t>(0xDC00u + (codepoint & 0x3FFu))));
19199 bytes += 12;
19200 }
19201 }
19202 else
19203 {
19204 // copy byte to buffer (all previous bytes
19205 // been copied have in default case above)
19206 string_buffer[bytes++] = s[i];
19207 }
19208 break;
19209 }
19210 }
19211
19212 // write buffer and reset index; there must be 13 bytes
19213 // left, as this is the maximal number of bytes to be
19214 // written ("\uxxxx\uxxxx\0") for one code point
19215 if (string_buffer.size() - bytes < 13)
19216 {
19217 o->write_characters(string_buffer.data(), bytes);
19218 bytes = 0;
19219 }
19220
19221 // remember the byte position of this accept
19222 bytes_after_last_accept = bytes;
19223 undumped_chars = 0;
19224 break;
19225 }
19226
19227 case UTF8_REJECT: // decode found invalid UTF-8 byte
19228 {
19229 switch (error_handler)
19230 {
19231 case error_handler_t::strict:
19232 {
19233 JSON_THROW(type_error::create(316, concat("invalid UTF-8 byte at index ", std::to_string(i), ": 0x", hex_bytes(byte | 0)), nullptr));
19234 }
19235
19236 case error_handler_t::ignore:
19237 case error_handler_t::replace:
19238 {
19239 // in case we saw this character the first time, we
19240 // would like to read it again, because the byte
19241 // may be OK for itself, but just not OK for the
19242 // previous sequence
19243 if (undumped_chars > 0)
19244 {
19245 --i;
19246 }
19247
19248 // reset length buffer to the last accepted index;
19249 // thus removing/ignoring the invalid characters
19250 bytes = bytes_after_last_accept;
19251
19252 if (error_handler == error_handler_t::replace)
19253 {
19254 // add a replacement character
19255 if (ensure_ascii)
19256 {
19257 string_buffer[bytes++] = '\\';
19258 string_buffer[bytes++] = 'u';
19259 string_buffer[bytes++] = 'f';
19260 string_buffer[bytes++] = 'f';
19261 string_buffer[bytes++] = 'f';
19262 string_buffer[bytes++] = 'd';
19263 }
19264 else
19265 {
19266 string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xEF');
19267 string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBF');
19268 string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBD');
19269 }
19270
19271 // write buffer and reset index; there must be 13 bytes
19272 // left, as this is the maximal number of bytes to be
19273 // written ("\uxxxx\uxxxx\0") for one code point
19274 if (string_buffer.size() - bytes < 13)
19275 {
19276 o->write_characters(string_buffer.data(), bytes);
19277 bytes = 0;
19278 }
19279
19280 bytes_after_last_accept = bytes;
19281 }
19282
19283 undumped_chars = 0;
19284
19285 // continue processing the string
19286 state = UTF8_ACCEPT;
19287 break;
19288 }
19289
19290 default: // LCOV_EXCL_LINE
19291 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
19292 }
19293 break;
19294 }
19295
19296 default: // decode found yet incomplete multi-byte code point
19297 {
19298 if (!ensure_ascii)
19299 {
19300 // code point will not be escaped - copy byte to buffer
19301 string_buffer[bytes++] = s[i];
19302 }
19303 ++undumped_chars;
19304 break;
19305 }
19306 }
19307 }
19308
19309 // we finished processing the string
19310 if (JSON_HEDLEY_LIKELY(state == UTF8_ACCEPT))
19311 {
19312 // write buffer
19313 if (bytes > 0)
19314 {
19315 o->write_characters(string_buffer.data(), bytes);
19316 }
19317 }
19318 else
19319 {
19320 // we finish reading, but do not accept: string was incomplete
19321 switch (error_handler)
19322 {
19323 case error_handler_t::strict:
19324 {
19325 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));
19326 }
19327
19328 case error_handler_t::ignore:
19329 {
19330 // write all accepted bytes
19331 o->write_characters(string_buffer.data(), bytes_after_last_accept);
19332 break;
19333 }
19334
19335 case error_handler_t::replace:
19336 {
19337 // write all accepted bytes
19338 o->write_characters(string_buffer.data(), bytes_after_last_accept);
19339 // add a replacement character
19340 if (ensure_ascii)
19341 {
19342 o->write_characters("\\ufffd", 6);
19343 }
19344 else
19345 {
19346 o->write_characters("\xEF\xBF\xBD", 3);
19347 }
19348 break;
19349 }
19350
19351 default: // LCOV_EXCL_LINE
19352 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
19353 }
19354 }
19355 }
19356
19357 private:
19358 /*!
19359 @brief count digits
19360
19361 Count the number of decimal (base 10) digits for an input unsigned integer.
19362
19363 @param[in] x unsigned integer number to count its digits
19364 @return number of decimal digits
19365 */
19366 unsigned int count_digits(number_unsigned_t x) noexcept
19367 {
19368 unsigned int n_digits = 1;
19369 for (;;)
19370 {
19371 if (x < 10)
19372 {
19373 return n_digits;
19374 }
19375 if (x < 100)
19376 {
19377 return n_digits + 1;
19378 }
19379 if (x < 1000)
19380 {
19381 return n_digits + 2;
19382 }
19383 if (x < 10000)
19384 {
19385 return n_digits + 3;
19386 }
19387 x = x / 10000u;
19388 n_digits += 4;
19389 }
19390 }
19391
19392 /*!
19393 * @brief convert a byte to a uppercase hex representation
19394 * @param[in] byte byte to represent
19395 * @return representation ("00".."FF")
19396 */
19397 static std::string hex_bytes(std::uint8_t byte)
19398 {
19399 std::string result = "FF";
19400 constexpr const char* nibble_to_hex = "0123456789ABCDEF";
19401 result[0] = nibble_to_hex[byte / 16];
19402 result[1] = nibble_to_hex[byte % 16];
19403 return result;
19404 }
19405
19406 // templates to avoid warnings about useless casts
19407 template <typename NumberType, enable_if_t<std::is_signed<NumberType>::value, int> = 0>
19408 bool is_negative_number(NumberType x)
19409 {
19410 return x < 0;
19411 }
19412
19413 template < typename NumberType, enable_if_t <std::is_unsigned<NumberType>::value, int > = 0 >
19414 bool is_negative_number(NumberType /*unused*/)
19415 {
19416 return false;
19417 }
19418
19419 /*!
19420 @brief dump an integer
19421
19422 Dump a given integer to output stream @a o. Works internally with
19423 @a number_buffer.
19424
19425 @param[in] x integer number (signed or unsigned) to dump
19426 @tparam NumberType either @a number_integer_t or @a number_unsigned_t
19427 */
19428 template < typename NumberType, detail::enable_if_t <
19429 std::is_integral<NumberType>::value ||
19430 std::is_same<NumberType, number_unsigned_t>::value ||
19431 std::is_same<NumberType, number_integer_t>::value ||
19432 std::is_same<NumberType, binary_char_t>::value,
19433 int > = 0 >
19434 void dump_integer(NumberType x)
19435 {
19436 static constexpr std::array<std::array<char, 2>, 100> digits_to_99
19437 {
19438 {
19439 {{'0', '0'}}, {{'0', '1'}}, {{'0', '2'}}, {{'0', '3'}}, {{'0', '4'}}, {{'0', '5'}}, {{'0', '6'}}, {{'0', '7'}}, {{'0', '8'}}, {{'0', '9'}},
19440 {{'1', '0'}}, {{'1', '1'}}, {{'1', '2'}}, {{'1', '3'}}, {{'1', '4'}}, {{'1', '5'}}, {{'1', '6'}}, {{'1', '7'}}, {{'1', '8'}}, {{'1', '9'}},
19441 {{'2', '0'}}, {{'2', '1'}}, {{'2', '2'}}, {{'2', '3'}}, {{'2', '4'}}, {{'2', '5'}}, {{'2', '6'}}, {{'2', '7'}}, {{'2', '8'}}, {{'2', '9'}},
19442 {{'3', '0'}}, {{'3', '1'}}, {{'3', '2'}}, {{'3', '3'}}, {{'3', '4'}}, {{'3', '5'}}, {{'3', '6'}}, {{'3', '7'}}, {{'3', '8'}}, {{'3', '9'}},
19443 {{'4', '0'}}, {{'4', '1'}}, {{'4', '2'}}, {{'4', '3'}}, {{'4', '4'}}, {{'4', '5'}}, {{'4', '6'}}, {{'4', '7'}}, {{'4', '8'}}, {{'4', '9'}},
19444 {{'5', '0'}}, {{'5', '1'}}, {{'5', '2'}}, {{'5', '3'}}, {{'5', '4'}}, {{'5', '5'}}, {{'5', '6'}}, {{'5', '7'}}, {{'5', '8'}}, {{'5', '9'}},
19445 {{'6', '0'}}, {{'6', '1'}}, {{'6', '2'}}, {{'6', '3'}}, {{'6', '4'}}, {{'6', '5'}}, {{'6', '6'}}, {{'6', '7'}}, {{'6', '8'}}, {{'6', '9'}},
19446 {{'7', '0'}}, {{'7', '1'}}, {{'7', '2'}}, {{'7', '3'}}, {{'7', '4'}}, {{'7', '5'}}, {{'7', '6'}}, {{'7', '7'}}, {{'7', '8'}}, {{'7', '9'}},
19447 {{'8', '0'}}, {{'8', '1'}}, {{'8', '2'}}, {{'8', '3'}}, {{'8', '4'}}, {{'8', '5'}}, {{'8', '6'}}, {{'8', '7'}}, {{'8', '8'}}, {{'8', '9'}},
19448 {{'9', '0'}}, {{'9', '1'}}, {{'9', '2'}}, {{'9', '3'}}, {{'9', '4'}}, {{'9', '5'}}, {{'9', '6'}}, {{'9', '7'}}, {{'9', '8'}}, {{'9', '9'}},
19449 }
19450 };
19451
19452 // special case for "0"
19453 if (x == 0)
19454 {
19455 o->write_character('0');
19456 return;
19457 }
19458
19459 // use a pointer to fill the buffer
19460 auto buffer_ptr = number_buffer.begin(); // NOLINT(llvm-qualified-auto,readability-qualified-auto,cppcoreguidelines-pro-type-vararg,hicpp-vararg)
19461
19462 number_unsigned_t abs_value;
19463
19464 unsigned int n_chars{};
19465
19466 if (is_negative_number(x))
19467 {
19468 *buffer_ptr = '-';
19469 abs_value = remove_sign(static_cast<number_integer_t>(x));
19470
19471 // account one more byte for the minus sign
19472 n_chars = 1 + count_digits(abs_value);
19473 }
19474 else
19475 {
19476 abs_value = static_cast<number_unsigned_t>(x);
19477 n_chars = count_digits(abs_value);
19478 }
19479
19480 // spare 1 byte for '\0'
19481 JSON_ASSERT(n_chars < number_buffer.size() - 1);
19482
19483 // jump to the end to generate the string from backward,
19484 // so we later avoid reversing the result
19485 buffer_ptr += n_chars;
19486
19487 // Fast int2ascii implementation inspired by "Fastware" talk by Andrei Alexandrescu
19488 // See: https://www.youtube.com/watch?v=o4-CwDo2zpg
19489 while (abs_value >= 100)
19490 {
19491 const auto digits_index = static_cast<unsigned>((abs_value % 100));
19492 abs_value /= 100;
19493 *(--buffer_ptr) = digits_to_99[digits_index][1];
19494 *(--buffer_ptr) = digits_to_99[digits_index][0];
19495 }
19496
19497 if (abs_value >= 10)
19498 {
19499 const auto digits_index = static_cast<unsigned>(abs_value);
19500 *(--buffer_ptr) = digits_to_99[digits_index][1];
19501 *(--buffer_ptr) = digits_to_99[digits_index][0];
19502 }
19503 else
19504 {
19505 *(--buffer_ptr) = static_cast<char>('0' + abs_value);
19506 }
19507
19508 o->write_characters(number_buffer.data(), n_chars);
19509 }
19510
19511 /*!
19512 @brief dump a floating-point number
19513
19514 Dump a given floating-point number to output stream @a o. Works internally
19515 with @a number_buffer.
19516
19517 @param[in] x floating-point number to dump
19518 */
19519 void dump_float(number_float_t x)
19520 {
19521 // NaN / inf
19522 if (!std::isfinite(x))
19523 {
19524 o->write_characters("null", 4);
19525 return;
19526 }
19527
19528 // If number_float_t is an IEEE-754 single or double precision number,
19529 // use the Grisu2 algorithm to produce short numbers which are
19530 // guaranteed to round-trip, using strtof and strtod, resp.
19531 //
19532 // NB: The test below works if <long double> == <double>.
19533 static constexpr bool is_ieee_single_or_double
19534 = (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) ||
19535 (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);
19536
19537 dump_float(x, std::integral_constant<bool, is_ieee_single_or_double>());
19538 }
19539
19540 void dump_float(number_float_t x, std::true_type /*is_ieee_single_or_double*/)
19541 {
19542 auto* begin = number_buffer.data();
19543 auto* end = ::nlohmann::detail::to_chars(begin, begin + number_buffer.size(), x);
19544
19545 o->write_characters(begin, static_cast<size_t>(end - begin));
19546 }
19547
19548 void dump_float(number_float_t x, std::false_type /*is_ieee_single_or_double*/)
19549 {
19550 // get number of digits for a float -> text -> float round-trip
19551 static constexpr auto d = std::numeric_limits<number_float_t>::max_digits10;
19552
19553 // the actual conversion
19554 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
19555 std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*g", d, x);
19556
19557 // negative value indicates an error
19558 JSON_ASSERT(len > 0);
19559 // check if buffer was large enough
19560 JSON_ASSERT(static_cast<std::size_t>(len) < number_buffer.size());
19561
19562 // erase thousands separator
19563 if (thousands_sep != '\0')
19564 {
19565 // NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::remove returns an iterator, see https://github.com/nlohmann/json/issues/3081
19566 const auto end = std::remove(number_buffer.begin(), number_buffer.begin() + len, thousands_sep);
19567 std::fill(end, number_buffer.end(), '\0');
19568 JSON_ASSERT((end - number_buffer.begin()) <= len);
19569 len = (end - number_buffer.begin());
19570 }
19571
19572 // convert decimal point to '.'
19573 if (decimal_point != '\0' && decimal_point != '.')
19574 {
19575 // NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::find returns an iterator, see https://github.com/nlohmann/json/issues/3081
19576 const auto dec_pos = std::find(number_buffer.begin(), number_buffer.end(), decimal_point);
19577 if (dec_pos != number_buffer.end())
19578 {
19579 *dec_pos = '.';
19580 }
19581 }
19582
19583 o->write_characters(number_buffer.data(), static_cast<std::size_t>(len));
19584
19585 // determine if we need to append ".0"
19586 const bool value_is_int_like =
19587 std::none_of(number_buffer.begin(), number_buffer.begin() + len + 1,
19588 [](char c)
19589 {
19590 return c == '.' || c == 'e';
19591 });
19592
19593 if (value_is_int_like)
19594 {
19595 o->write_characters(".0", 2);
19596 }
19597 }
19598
19599 /*!
19600 @brief check whether a string is UTF-8 encoded
19601
19602 The function checks each byte of a string whether it is UTF-8 encoded. The
19603 result of the check is stored in the @a state parameter. The function must
19604 be called initially with state 0 (accept). State 1 means the string must
19605 be rejected, because the current byte is not allowed. If the string is
19606 completely processed, but the state is non-zero, the string ended
19607 prematurely; that is, the last byte indicated more bytes should have
19608 followed.
19609
19610 @param[in,out] state the state of the decoding
19611 @param[in,out] codep codepoint (valid only if resulting state is UTF8_ACCEPT)
19612 @param[in] byte next byte to decode
19613 @return new state
19614
19615 @note The function has been edited: a std::array is used.
19616
19617 @copyright Copyright (c) 2008-2009 Bjoern Hoehrmann <[email protected]>
19618 @sa http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
19619 */
19620 static std::uint8_t decode(std::uint8_t& state, std::uint32_t& codep, const std::uint8_t byte) noexcept
19621 {
19622 static const std::array<std::uint8_t, 400> utf8d =
19623 {
19624 {
19625 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
19626 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
19627 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
19628 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
19629 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
19630 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
19631 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
19632 0xA, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3, // E0..EF
19633 0xB, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, // F0..FF
19634 0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, 0x6, 0x1, 0x1, 0x1, 0x1, // s0..s0
19635 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
19636 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
19637 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
19638 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
19639 }
19640 };
19641
19642 JSON_ASSERT(byte < utf8d.size());
19643 const std::uint8_t type = utf8d[byte];
19644
19645 codep = (state != UTF8_ACCEPT)
19646 ? (byte & 0x3fu) | (codep << 6u)
19647 : (0xFFu >> type) & (byte);
19648
19649 const std::size_t index = 256u + (static_cast<size_t>(state) * 16u) + static_cast<size_t>(type);
19650 JSON_ASSERT(index < utf8d.size());
19651 state = utf8d[index];
19652 return state;
19653 }
19654
19655 /*
19656 * Overload to make the compiler happy while it is instantiating
19657 * dump_integer for number_unsigned_t.
19658 * Must never be called.
19659 */
19660 number_unsigned_t remove_sign(number_unsigned_t x)
19661 {
19662 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
19663 return x; // LCOV_EXCL_LINE
19664 }
19665
19666 /*
19667 * Helper function for dump_integer
19668 *
19669 * This function takes a negative signed integer and returns its absolute
19670 * value as unsigned integer. The plus/minus shuffling is necessary as we can
19671 * not directly remove the sign of an arbitrary signed integer as the
19672 * absolute values of INT_MIN and INT_MAX are usually not the same. See
19673 * #1708 for details.
19674 */
19675 number_unsigned_t remove_sign(number_integer_t x) noexcept
19676 {
19677 JSON_ASSERT(x < 0 && x < (std::numeric_limits<number_integer_t>::max)()); // NOLINT(misc-redundant-expression)
19678 return static_cast<number_unsigned_t>(-(x + 1)) + 1;
19679 }
19680
19681 private:
19682 /// the output of the serializer
19683 output_adapter_t<char> o = nullptr;
19684
19685 /// a (hopefully) large enough character buffer
19686 std::array<char, 64> number_buffer{{}};
19687
19688 /// the locale
19689 const std::lconv* loc = nullptr;
19690 /// the locale's thousand separator character
19691 const char thousands_sep = '\0';
19692 /// the locale's decimal point character
19693 const char decimal_point = '\0';
19694
19695 /// string buffer
19696 std::array<char, 512> string_buffer{{}};
19697
19698 /// the indentation character
19699 const char indent_char;
19700 /// the indentation string
19701 string_t indent_string;
19702
19703 /// error_handler how to react on decoding errors
19704 const error_handler_t error_handler;
19705 };
19706
19707 } // namespace detail
19708 NLOHMANN_JSON_NAMESPACE_END
19709
19710 // #include <nlohmann/detail/value_t.hpp>
19711
19712 // #include <nlohmann/json_fwd.hpp>
19713
19714 // #include <nlohmann/ordered_map.hpp>
19715 // __ _____ _____ _____
19716 // __| | __| | | | JSON for Modern C++
19717 // | | |__ | | | | | | version 3.12.0
19718 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
19719 //
19720 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
19721 // SPDX-License-Identifier: MIT
19722
19723
19724
19725 #include <functional> // equal_to, less
19726 #include <initializer_list> // initializer_list
19727 #include <iterator> // input_iterator_tag, iterator_traits
19728 #include <memory> // allocator
19729 #include <stdexcept> // for out_of_range
19730 #include <type_traits> // enable_if, is_convertible
19731 #include <utility> // pair
19732 #include <vector> // vector
19733
19734 // #include <nlohmann/detail/macro_scope.hpp>
19735
19736 // #include <nlohmann/detail/meta/type_traits.hpp>
19737
19738
19739 NLOHMANN_JSON_NAMESPACE_BEGIN
19740
19741 /// ordered_map: a minimal map-like container that preserves insertion order
19742 /// for use within nlohmann::basic_json<ordered_map>
19743 template <class Key, class T, class IgnoredLess = std::less<Key>,
19744 class Allocator = std::allocator<std::pair<const Key, T>>>
19745 struct ordered_map : std::vector<std::pair<const Key, T>, Allocator>
19746 {
19747 using key_type = Key;
19748 using mapped_type = T;
19749 using Container = std::vector<std::pair<const Key, T>, Allocator>;
19750 using iterator = typename Container::iterator;
19751 using const_iterator = typename Container::const_iterator;
19752 using size_type = typename Container::size_type;
19753 using value_type = typename Container::value_type;
19754 #ifdef JSON_HAS_CPP_14
19755 using key_compare = std::equal_to<>;
19756 #else
19757 using key_compare = std::equal_to<Key>;
19758 #endif
19759
19760 // Explicit constructors instead of `using Container::Container`
19761 // otherwise older compilers choke on it (GCC <= 5.5, xcode <= 9.4)
19762 ordered_map() noexcept(noexcept(Container())) : Container{} {}
19763 explicit ordered_map(const Allocator& alloc) noexcept(noexcept(Container(alloc))) : Container{alloc} {}
19764 template <class It>
19765 ordered_map(It first, It last, const Allocator& alloc = Allocator())
19766 : Container{first, last, alloc} {}
19767 ordered_map(std::initializer_list<value_type> init, const Allocator& alloc = Allocator() )
19768 : Container{init, alloc} {}
19769
19770 std::pair<iterator, bool> emplace(const key_type& key, T&& t)
19771 {
19772 for (auto it = this->begin(); it != this->end(); ++it)
19773 {
19774 if (m_compare(it->first, key))
19775 {
19776 return {it, false};
19777 }
19778 }
19779 Container::emplace_back(key, std::forward<T>(t));
19780 return {std::prev(this->end()), true};
19781 }
19782
19783 template<class KeyType, detail::enable_if_t<
19784 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
19785 std::pair<iterator, bool> emplace(KeyType && key, T && t)
19786 {
19787 for (auto it = this->begin(); it != this->end(); ++it)
19788 {
19789 if (m_compare(it->first, key))
19790 {
19791 return {it, false};
19792 }
19793 }
19794 Container::emplace_back(std::forward<KeyType>(key), std::forward<T>(t));
19795 return {std::prev(this->end()), true};
19796 }
19797
19798 T& operator[](const key_type& key)
19799 {
19800 return emplace(key, T{}).first->second;
19801 }
19802
19803 template<class KeyType, detail::enable_if_t<
19804 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
19805 T & operator[](KeyType && key)
19806 {
19807 return emplace(std::forward<KeyType>(key), T{}).first->second;
19808 }
19809
19810 const T& operator[](const key_type& key) const
19811 {
19812 return at(key);
19813 }
19814
19815 template<class KeyType, detail::enable_if_t<
19816 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
19817 const T & operator[](KeyType && key) const
19818 {
19819 return at(std::forward<KeyType>(key));
19820 }
19821
19822 T& at(const key_type& key)
19823 {
19824 for (auto it = this->begin(); it != this->end(); ++it)
19825 {
19826 if (m_compare(it->first, key))
19827 {
19828 return it->second;
19829 }
19830 }
19831
19832 JSON_THROW(std::out_of_range("key not found"));
19833 }
19834
19835 template<class KeyType, detail::enable_if_t<
19836 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
19837 T & at(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
19838 {
19839 for (auto it = this->begin(); it != this->end(); ++it)
19840 {
19841 if (m_compare(it->first, key))
19842 {
19843 return it->second;
19844 }
19845 }
19846
19847 JSON_THROW(std::out_of_range("key not found"));
19848 }
19849
19850 const T& at(const key_type& key) const
19851 {
19852 for (auto it = this->begin(); it != this->end(); ++it)
19853 {
19854 if (m_compare(it->first, key))
19855 {
19856 return it->second;
19857 }
19858 }
19859
19860 JSON_THROW(std::out_of_range("key not found"));
19861 }
19862
19863 template<class KeyType, detail::enable_if_t<
19864 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
19865 const T & at(KeyType && key) const // NOLINT(cppcoreguidelines-missing-std-forward)
19866 {
19867 for (auto it = this->begin(); it != this->end(); ++it)
19868 {
19869 if (m_compare(it->first, key))
19870 {
19871 return it->second;
19872 }
19873 }
19874
19875 JSON_THROW(std::out_of_range("key not found"));
19876 }
19877
19878 size_type erase(const key_type& key)
19879 {
19880 for (auto it = this->begin(); it != this->end(); ++it)
19881 {
19882 if (m_compare(it->first, key))
19883 {
19884 // Since we cannot move const Keys, re-construct them in place
19885 for (auto next = it; ++next != this->end(); ++it)
19886 {
19887 it->~value_type(); // Destroy but keep allocation
19888 new (&*it) value_type{std::move(*next)};
19889 }
19890 Container::pop_back();
19891 return 1;
19892 }
19893 }
19894 return 0;
19895 }
19896
19897 template<class KeyType, detail::enable_if_t<
19898 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
19899 size_type erase(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
19900 {
19901 for (auto it = this->begin(); it != this->end(); ++it)
19902 {
19903 if (m_compare(it->first, key))
19904 {
19905 // Since we cannot move const Keys, re-construct them in place
19906 for (auto next = it; ++next != this->end(); ++it)
19907 {
19908 it->~value_type(); // Destroy but keep allocation
19909 new (&*it) value_type{std::move(*next)};
19910 }
19911 Container::pop_back();
19912 return 1;
19913 }
19914 }
19915 return 0;
19916 }
19917
19918 iterator erase(iterator pos)
19919 {
19920 return erase(pos, std::next(pos));
19921 }
19922
19923 iterator erase(iterator first, iterator last)
19924 {
19925 if (first == last)
19926 {
19927 return first;
19928 }
19929
19930 const auto elements_affected = std::distance(first, last);
19931 const auto offset = std::distance(Container::begin(), first);
19932
19933 // This is the start situation. We need to delete elements_affected
19934 // elements (3 in this example: e, f, g), and need to return an
19935 // iterator past the last deleted element (h in this example).
19936 // Note that offset is the distance from the start of the vector
19937 // to first. We will need this later.
19938
19939 // [ a, b, c, d, e, f, g, h, i, j ]
19940 // ^ ^
19941 // first last
19942
19943 // Since we cannot move const Keys, we re-construct them in place.
19944 // We start at first and re-construct (viz. copy) the elements from
19945 // the back of the vector. Example for first iteration:
19946
19947 // ,--------.
19948 // v | destroy e and re-construct with h
19949 // [ a, b, c, d, e, f, g, h, i, j ]
19950 // ^ ^
19951 // it it + elements_affected
19952
19953 for (auto it = first; std::next(it, elements_affected) != Container::end(); ++it)
19954 {
19955 it->~value_type(); // destroy but keep allocation
19956 new (&*it) value_type{std::move(*std::next(it, elements_affected))}; // "move" next element to it
19957 }
19958
19959 // [ a, b, c, d, h, i, j, h, i, j ]
19960 // ^ ^
19961 // first last
19962
19963 // remove the unneeded elements at the end of the vector
19964 Container::resize(this->size() - static_cast<size_type>(elements_affected));
19965
19966 // [ a, b, c, d, h, i, j ]
19967 // ^ ^
19968 // first last
19969
19970 // first is now pointing past the last deleted element, but we cannot
19971 // use this iterator, because it may have been invalidated by the
19972 // resize call. Instead, we can return begin() + offset.
19973 return Container::begin() + offset;
19974 }
19975
19976 size_type count(const key_type& key) const
19977 {
19978 for (auto it = this->begin(); it != this->end(); ++it)
19979 {
19980 if (m_compare(it->first, key))
19981 {
19982 return 1;
19983 }
19984 }
19985 return 0;
19986 }
19987
19988 template<class KeyType, detail::enable_if_t<
19989 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
19990 size_type count(KeyType && key) const // NOLINT(cppcoreguidelines-missing-std-forward)
19991 {
19992 for (auto it = this->begin(); it != this->end(); ++it)
19993 {
19994 if (m_compare(it->first, key))
19995 {
19996 return 1;
19997 }
19998 }
19999 return 0;
20000 }
20001
20002 iterator find(const key_type& key)
20003 {
20004 for (auto it = this->begin(); it != this->end(); ++it)
20005 {
20006 if (m_compare(it->first, key))
20007 {
20008 return it;
20009 }
20010 }
20011 return Container::end();
20012 }
20013
20014 template<class KeyType, detail::enable_if_t<
20015 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
20016 iterator find(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
20017 {
20018 for (auto it = this->begin(); it != this->end(); ++it)
20019 {
20020 if (m_compare(it->first, key))
20021 {
20022 return it;
20023 }
20024 }
20025 return Container::end();
20026 }
20027
20028 const_iterator find(const key_type& key) const
20029 {
20030 for (auto it = this->begin(); it != this->end(); ++it)
20031 {
20032 if (m_compare(it->first, key))
20033 {
20034 return it;
20035 }
20036 }
20037 return Container::end();
20038 }
20039
20040 std::pair<iterator, bool> insert( value_type&& value )
20041 {
20042 return emplace(value.first, std::move(value.second));
20043 }
20044
20045 std::pair<iterator, bool> insert( const value_type& value )
20046 {
20047 for (auto it = this->begin(); it != this->end(); ++it)
20048 {
20049 if (m_compare(it->first, value.first))
20050 {
20051 return {it, false};
20052 }
20053 }
20054 Container::push_back(value);
20055 return {--this->end(), true};
20056 }
20057
20058 template<typename InputIt>
20059 using require_input_iter = typename std::enable_if<std::is_convertible<typename std::iterator_traits<InputIt>::iterator_category,
20060 std::input_iterator_tag>::value>::type;
20061
20062 template<typename InputIt, typename = require_input_iter<InputIt>>
20063 void insert(InputIt first, InputIt last)
20064 {
20065 for (auto it = first; it != last; ++it)
20066 {
20067 insert(*it);
20068 }
20069 }
20070
20071 private:
20072 JSON_NO_UNIQUE_ADDRESS key_compare m_compare = key_compare();
20073 };
20074
20075 NLOHMANN_JSON_NAMESPACE_END
20076
20077
20078 #if defined(JSON_HAS_CPP_17)
20079 #if JSON_HAS_STATIC_RTTI
20080 #include <any>
20081 #endif
20082 #include <string_view>
20083 #endif
20084
20085 /*!
20086 @brief namespace for Niels Lohmann
20087 @see https://github.com/nlohmann
20088 @since version 1.0.0
20089 */
20090 NLOHMANN_JSON_NAMESPACE_BEGIN
20091
20092 /*!
20093 @brief a class to store JSON values
20094
20095 @internal
20096 @invariant The member variables @a m_value and @a m_type have the following
20097 relationship:
20098 - If `m_type == value_t::object`, then `m_value.object != nullptr`.
20099 - If `m_type == value_t::array`, then `m_value.array != nullptr`.
20100 - If `m_type == value_t::string`, then `m_value.string != nullptr`.
20101 The invariants are checked by member function assert_invariant().
20102
20103 @note ObjectType trick from https://stackoverflow.com/a/9860911
20104 @endinternal
20105
20106 @since version 1.0.0
20107
20108 @nosubgrouping
20109 */
20110 NLOHMANN_BASIC_JSON_TPL_DECLARATION
20111 class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions)
20112 : public ::nlohmann::detail::json_base_class<CustomBaseClass>
20113 {
20114 private:
20115 template<detail::value_t> friend struct detail::external_constructor;
20116
20117 template<typename>
20118 friend class ::nlohmann::json_pointer;
20119 // can be restored when json_pointer backwards compatibility is removed
20120 // friend ::nlohmann::json_pointer<StringType>;
20121
20122 template<typename BasicJsonType, typename InputType>
20123 friend class ::nlohmann::detail::parser;
20124 friend ::nlohmann::detail::serializer<basic_json>;
20125 template<typename BasicJsonType>
20126 friend class ::nlohmann::detail::iter_impl;
20127 template<typename BasicJsonType, typename CharType>
20128 friend class ::nlohmann::detail::binary_writer;
20129 template<typename BasicJsonType, typename InputType, typename SAX>
20130 friend class ::nlohmann::detail::binary_reader;
20131 template<typename BasicJsonType, typename InputAdapterType>
20132 friend class ::nlohmann::detail::json_sax_dom_parser;
20133 template<typename BasicJsonType, typename InputAdapterType>
20134 friend class ::nlohmann::detail::json_sax_dom_callback_parser;
20135 friend class ::nlohmann::detail::exception;
20136
20137 /// workaround type for MSVC
20138 using basic_json_t = NLOHMANN_BASIC_JSON_TPL;
20139 using json_base_class_t = ::nlohmann::detail::json_base_class<CustomBaseClass>;
20140
20141 JSON_PRIVATE_UNLESS_TESTED:
20142 // convenience aliases for types residing in namespace detail;
20143 using lexer = ::nlohmann::detail::lexer_base<basic_json>;
20144
20145 template<typename InputAdapterType>
20146 static ::nlohmann::detail::parser<basic_json, InputAdapterType> parser(
20147 InputAdapterType adapter,
20148 detail::parser_callback_t<basic_json>cb = nullptr,
20149 const bool allow_exceptions = true,
20150 const bool ignore_comments = false
20151 )
20152 {
20153 return ::nlohmann::detail::parser<basic_json, InputAdapterType>(std::move(adapter),
20154 std::move(cb), allow_exceptions, ignore_comments);
20155 }
20156
20157 private:
20158 using primitive_iterator_t = ::nlohmann::detail::primitive_iterator_t;
20159 template<typename BasicJsonType>
20160 using internal_iterator = ::nlohmann::detail::internal_iterator<BasicJsonType>;
20161 template<typename BasicJsonType>
20162 using iter_impl = ::nlohmann::detail::iter_impl<BasicJsonType>;
20163 template<typename Iterator>
20164 using iteration_proxy = ::nlohmann::detail::iteration_proxy<Iterator>;
20165 template<typename Base> using json_reverse_iterator = ::nlohmann::detail::json_reverse_iterator<Base>;
20166
20167 template<typename CharType>
20168 using output_adapter_t = ::nlohmann::detail::output_adapter_t<CharType>;
20169
20170 template<typename InputType>
20171 using binary_reader = ::nlohmann::detail::binary_reader<basic_json, InputType>;
20172 template<typename CharType> using binary_writer = ::nlohmann::detail::binary_writer<basic_json, CharType>;
20173
20174 JSON_PRIVATE_UNLESS_TESTED:
20175 using serializer = ::nlohmann::detail::serializer<basic_json>;
20176
20177 public:
20178 using value_t = detail::value_t;
20179 /// JSON Pointer, see @ref nlohmann::json_pointer
20180 using json_pointer = ::nlohmann::json_pointer<StringType>;
20181 template<typename T, typename SFINAE>
20182 using json_serializer = JSONSerializer<T, SFINAE>;
20183 /// how to treat decoding errors
20184 using error_handler_t = detail::error_handler_t;
20185 /// how to treat CBOR tags
20186 using cbor_tag_handler_t = detail::cbor_tag_handler_t;
20187 /// how to encode BJData
20188 using bjdata_version_t = detail::bjdata_version_t;
20189 /// helper type for initializer lists of basic_json values
20190 using initializer_list_t = std::initializer_list<detail::json_ref<basic_json>>;
20191
20192 using input_format_t = detail::input_format_t;
20193 /// SAX interface type, see @ref nlohmann::json_sax
20194 using json_sax_t = json_sax<basic_json>;
20195
20196 ////////////////
20197 // exceptions //
20198 ////////////////
20199
20200 /// @name exceptions
20201 /// Classes to implement user-defined exceptions.
20202 /// @{
20203
20204 using exception = detail::exception;
20205 using parse_error = detail::parse_error;
20206 using invalid_iterator = detail::invalid_iterator;
20207 using type_error = detail::type_error;
20208 using out_of_range = detail::out_of_range;
20209 using other_error = detail::other_error;
20210
20211 /// @}
20212
20213 /////////////////////
20214 // container types //
20215 /////////////////////
20216
20217 /// @name container types
20218 /// The canonic container types to use @ref basic_json like any other STL
20219 /// container.
20220 /// @{
20221
20222 /// the type of elements in a basic_json container
20223 using value_type = basic_json;
20224
20225 /// the type of an element reference
20226 using reference = value_type&;
20227 /// the type of an element const reference
20228 using const_reference = const value_type&;
20229
20230 /// a type to represent differences between iterators
20231 using difference_type = std::ptrdiff_t;
20232 /// a type to represent container sizes
20233 using size_type = std::size_t;
20234
20235 /// the allocator type
20236 using allocator_type = AllocatorType<basic_json>;
20237
20238 /// the type of an element pointer
20239 using pointer = typename std::allocator_traits<allocator_type>::pointer;
20240 /// the type of an element const pointer
20241 using const_pointer = typename std::allocator_traits<allocator_type>::const_pointer;
20242
20243 /// an iterator for a basic_json container
20244 using iterator = iter_impl<basic_json>;
20245 /// a const iterator for a basic_json container
20246 using const_iterator = iter_impl<const basic_json>;
20247 /// a reverse iterator for a basic_json container
20248 using reverse_iterator = json_reverse_iterator<typename basic_json::iterator>;
20249 /// a const reverse iterator for a basic_json container
20250 using const_reverse_iterator = json_reverse_iterator<typename basic_json::const_iterator>;
20251
20252 /// @}
20253
20254 /// @brief returns the allocator associated with the container
20255 /// @sa https://json.nlohmann.me/api/basic_json/get_allocator/
20256 static allocator_type get_allocator()
20257 {
20258 return allocator_type();
20259 }
20260
20261 /// @brief returns version information on the library
20262 /// @sa https://json.nlohmann.me/api/basic_json/meta/
20263 JSON_HEDLEY_WARN_UNUSED_RESULT
20264 static basic_json meta()
20265 {
20266 basic_json result;
20267
20268 result["copyright"] = "(C) 2013-2025 Niels Lohmann";
20269 result["name"] = "JSON for Modern C++";
20270 result["url"] = "https://github.com/nlohmann/json";
20271 result["version"]["string"] =
20272 detail::concat(std::to_string(NLOHMANN_JSON_VERSION_MAJOR), '.',
20273 std::to_string(NLOHMANN_JSON_VERSION_MINOR), '.',
20274 std::to_string(NLOHMANN_JSON_VERSION_PATCH));
20275 result["version"]["major"] = NLOHMANN_JSON_VERSION_MAJOR;
20276 result["version"]["minor"] = NLOHMANN_JSON_VERSION_MINOR;
20277 result["version"]["patch"] = NLOHMANN_JSON_VERSION_PATCH;
20278
20279 #ifdef _WIN32
20280 result["platform"] = "win32";
20281 #elif defined __linux__
20282 result["platform"] = "linux";
20283 #elif defined __APPLE__
20284 result["platform"] = "apple";
20285 #elif defined __unix__
20286 result["platform"] = "unix";
20287 #else
20288 result["platform"] = "unknown";
20289 #endif
20290
20291 #if defined(__ICC) || defined(__INTEL_COMPILER)
20292 result["compiler"] = {{"family", "icc"}, {"version", __INTEL_COMPILER}};
20293 #elif defined(__clang__)
20294 result["compiler"] = {{"family", "clang"}, {"version", __clang_version__}};
20295 #elif defined(__GNUC__) || defined(__GNUG__)
20296 result["compiler"] = {{"family", "gcc"}, {"version", detail::concat(
20297 std::to_string(__GNUC__), '.',
20298 std::to_string(__GNUC_MINOR__), '.',
20299 std::to_string(__GNUC_PATCHLEVEL__))
20300 }
20301 };
20302 #elif defined(__HP_cc) || defined(__HP_aCC)
20303 result["compiler"] = "hp"
20304 #elif defined(__IBMCPP__)
20305 result["compiler"] = {{"family", "ilecpp"}, {"version", __IBMCPP__}};
20306 #elif defined(_MSC_VER)
20307 result["compiler"] = {{"family", "msvc"}, {"version", _MSC_VER}};
20308 #elif defined(__PGI)
20309 result["compiler"] = {{"family", "pgcpp"}, {"version", __PGI}};
20310 #elif defined(__SUNPRO_CC)
20311 result["compiler"] = {{"family", "sunpro"}, {"version", __SUNPRO_CC}};
20312 #else
20313 result["compiler"] = {{"family", "unknown"}, {"version", "unknown"}};
20314 #endif
20315
20316 #if defined(_MSVC_LANG)
20317 result["compiler"]["c++"] = std::to_string(_MSVC_LANG);
20318 #elif defined(__cplusplus)
20319 result["compiler"]["c++"] = std::to_string(__cplusplus);
20320 #else
20321 result["compiler"]["c++"] = "unknown";
20322 #endif
20323 return result;
20324 }
20325
20326 ///////////////////////////
20327 // JSON value data types //
20328 ///////////////////////////
20329
20330 /// @name JSON value data types
20331 /// The data types to store a JSON value. These types are derived from
20332 /// the template arguments passed to class @ref basic_json.
20333 /// @{
20334
20335 /// @brief default object key comparator type
20336 /// The actual object key comparator type (@ref object_comparator_t) may be
20337 /// different.
20338 /// @sa https://json.nlohmann.me/api/basic_json/default_object_comparator_t/
20339 #if defined(JSON_HAS_CPP_14)
20340 // use of transparent comparator avoids unnecessary repeated construction of temporaries
20341 // in functions involving lookup by key with types other than object_t::key_type (aka. StringType)
20342 using default_object_comparator_t = std::less<>;
20343 #else
20344 using default_object_comparator_t = std::less<StringType>;
20345 #endif
20346
20347 /// @brief a type for an object
20348 /// @sa https://json.nlohmann.me/api/basic_json/object_t/
20349 using object_t = ObjectType<StringType,
20350 basic_json,
20351 default_object_comparator_t,
20352 AllocatorType<std::pair<const StringType,
20353 basic_json>>>;
20354
20355 /// @brief a type for an array
20356 /// @sa https://json.nlohmann.me/api/basic_json/array_t/
20357 using array_t = ArrayType<basic_json, AllocatorType<basic_json>>;
20358
20359 /// @brief a type for a string
20360 /// @sa https://json.nlohmann.me/api/basic_json/string_t/
20361 using string_t = StringType;
20362
20363 /// @brief a type for a boolean
20364 /// @sa https://json.nlohmann.me/api/basic_json/boolean_t/
20365 using boolean_t = BooleanType;
20366
20367 /// @brief a type for a number (integer)
20368 /// @sa https://json.nlohmann.me/api/basic_json/number_integer_t/
20369 using number_integer_t = NumberIntegerType;
20370
20371 /// @brief a type for a number (unsigned)
20372 /// @sa https://json.nlohmann.me/api/basic_json/number_unsigned_t/
20373 using number_unsigned_t = NumberUnsignedType;
20374
20375 /// @brief a type for a number (floating-point)
20376 /// @sa https://json.nlohmann.me/api/basic_json/number_float_t/
20377 using number_float_t = NumberFloatType;
20378
20379 /// @brief a type for a packed binary type
20380 /// @sa https://json.nlohmann.me/api/basic_json/binary_t/
20381 using binary_t = nlohmann::byte_container_with_subtype<BinaryType>;
20382
20383 /// @brief object key comparator type
20384 /// @sa https://json.nlohmann.me/api/basic_json/object_comparator_t/
20385 using object_comparator_t = detail::actual_object_comparator_t<basic_json>;
20386
20387 /// @}
20388
20389 private:
20390
20391 /// helper for exception-safe object creation
20392 template<typename T, typename... Args>
20393 JSON_HEDLEY_RETURNS_NON_NULL
20394 static T* create(Args&& ... args)
20395 {
20396 AllocatorType<T> alloc;
20397 using AllocatorTraits = std::allocator_traits<AllocatorType<T>>;
20398
20399 auto deleter = [&](T * obj)
20400 {
20401 AllocatorTraits::deallocate(alloc, obj, 1);
20402 };
20403 std::unique_ptr<T, decltype(deleter)> obj(AllocatorTraits::allocate(alloc, 1), deleter);
20404 AllocatorTraits::construct(alloc, obj.get(), std::forward<Args>(args)...);
20405 JSON_ASSERT(obj != nullptr);
20406 return obj.release();
20407 }
20408
20409 ////////////////////////
20410 // JSON value storage //
20411 ////////////////////////
20412
20413 JSON_PRIVATE_UNLESS_TESTED:
20414 /*!
20415 @brief a JSON value
20416
20417 The actual storage for a JSON value of the @ref basic_json class. This
20418 union combines the different storage types for the JSON value types
20419 defined in @ref value_t.
20420
20421 JSON type | value_t type | used type
20422 --------- | --------------- | ------------------------
20423 object | object | pointer to @ref object_t
20424 array | array | pointer to @ref array_t
20425 string | string | pointer to @ref string_t
20426 boolean | boolean | @ref boolean_t
20427 number | number_integer | @ref number_integer_t
20428 number | number_unsigned | @ref number_unsigned_t
20429 number | number_float | @ref number_float_t
20430 binary | binary | pointer to @ref binary_t
20431 null | null | *no value is stored*
20432
20433 @note Variable-length types (objects, arrays, and strings) are stored as
20434 pointers. The size of the union should not exceed 64 bits if the default
20435 value types are used.
20436
20437 @since version 1.0.0
20438 */
20439 union json_value
20440 {
20441 /// object (stored with pointer to save storage)
20442 object_t* object;
20443 /// array (stored with pointer to save storage)
20444 array_t* array;
20445 /// string (stored with pointer to save storage)
20446 string_t* string;
20447 /// binary (stored with pointer to save storage)
20448 binary_t* binary;
20449 /// boolean
20450 boolean_t boolean;
20451 /// number (integer)
20452 number_integer_t number_integer;
20453 /// number (unsigned integer)
20454 number_unsigned_t number_unsigned;
20455 /// number (floating-point)
20456 number_float_t number_float;
20457
20458 /// default constructor (for null values)
20459 json_value() = default;
20460 /// constructor for booleans
20461 json_value(boolean_t v) noexcept : boolean(v) {}
20462 /// constructor for numbers (integer)
20463 json_value(number_integer_t v) noexcept : number_integer(v) {}
20464 /// constructor for numbers (unsigned)
20465 json_value(number_unsigned_t v) noexcept : number_unsigned(v) {}
20466 /// constructor for numbers (floating-point)
20467 json_value(number_float_t v) noexcept : number_float(v) {}
20468 /// constructor for empty values of a given type
20469 json_value(value_t t)
20470 {
20471 switch (t)
20472 {
20473 case value_t::object:
20474 {
20475 object = create<object_t>();
20476 break;
20477 }
20478
20479 case value_t::array:
20480 {
20481 array = create<array_t>();
20482 break;
20483 }
20484
20485 case value_t::string:
20486 {
20487 string = create<string_t>("");
20488 break;
20489 }
20490
20491 case value_t::binary:
20492 {
20493 binary = create<binary_t>();
20494 break;
20495 }
20496
20497 case value_t::boolean:
20498 {
20499 boolean = static_cast<boolean_t>(false);
20500 break;
20501 }
20502
20503 case value_t::number_integer:
20504 {
20505 number_integer = static_cast<number_integer_t>(0);
20506 break;
20507 }
20508
20509 case value_t::number_unsigned:
20510 {
20511 number_unsigned = static_cast<number_unsigned_t>(0);
20512 break;
20513 }
20514
20515 case value_t::number_float:
20516 {
20517 number_float = static_cast<number_float_t>(0.0);
20518 break;
20519 }
20520
20521 case value_t::null:
20522 {
20523 object = nullptr; // silence warning, see #821
20524 break;
20525 }
20526
20527 case value_t::discarded:
20528 default:
20529 {
20530 object = nullptr; // silence warning, see #821
20531 if (JSON_HEDLEY_UNLIKELY(t == value_t::null))
20532 {
20533 JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.12.0", nullptr)); // LCOV_EXCL_LINE
20534 }
20535 break;
20536 }
20537 }
20538 }
20539
20540 /// constructor for strings
20541 json_value(const string_t& value) : string(create<string_t>(value)) {}
20542
20543 /// constructor for rvalue strings
20544 json_value(string_t&& value) : string(create<string_t>(std::move(value))) {}
20545
20546 /// constructor for objects
20547 json_value(const object_t& value) : object(create<object_t>(value)) {}
20548
20549 /// constructor for rvalue objects
20550 json_value(object_t&& value) : object(create<object_t>(std::move(value))) {}
20551
20552 /// constructor for arrays
20553 json_value(const array_t& value) : array(create<array_t>(value)) {}
20554
20555 /// constructor for rvalue arrays
20556 json_value(array_t&& value) : array(create<array_t>(std::move(value))) {}
20557
20558 /// constructor for binary arrays
20559 json_value(const typename binary_t::container_type& value) : binary(create<binary_t>(value)) {}
20560
20561 /// constructor for rvalue binary arrays
20562 json_value(typename binary_t::container_type&& value) : binary(create<binary_t>(std::move(value))) {}
20563
20564 /// constructor for binary arrays (internal type)
20565 json_value(const binary_t& value) : binary(create<binary_t>(value)) {}
20566
20567 /// constructor for rvalue binary arrays (internal type)
20568 json_value(binary_t&& value) : binary(create<binary_t>(std::move(value))) {}
20569
20570 void destroy(value_t t)
20571 {
20572 if (
20573 (t == value_t::object && object == nullptr) ||
20574 (t == value_t::array && array == nullptr) ||
20575 (t == value_t::string && string == nullptr) ||
20576 (t == value_t::binary && binary == nullptr)
20577 )
20578 {
20579 //not initialized (e.g. due to exception in the ctor)
20580 return;
20581 }
20582 if (t == value_t::array || t == value_t::object)
20583 {
20584 // flatten the current json_value to a heap-allocated stack
20585 std::vector<basic_json> stack;
20586
20587 // move the top-level items to stack
20588 if (t == value_t::array)
20589 {
20590 stack.reserve(array->size());
20591 std::move(array->begin(), array->end(), std::back_inserter(stack));
20592 }
20593 else
20594 {
20595 stack.reserve(object->size());
20596 for (auto&& it : *object)
20597 {
20598 stack.push_back(std::move(it.second));
20599 }
20600 }
20601
20602 while (!stack.empty())
20603 {
20604 // move the last item to local variable to be processed
20605 basic_json current_item(std::move(stack.back()));
20606 stack.pop_back();
20607
20608 // if current_item is array/object, move
20609 // its children to the stack to be processed later
20610 if (current_item.is_array())
20611 {
20612 std::move(current_item.m_data.m_value.array->begin(), current_item.m_data.m_value.array->end(), std::back_inserter(stack));
20613
20614 current_item.m_data.m_value.array->clear();
20615 }
20616 else if (current_item.is_object())
20617 {
20618 for (auto&& it : *current_item.m_data.m_value.object)
20619 {
20620 stack.push_back(std::move(it.second));
20621 }
20622
20623 current_item.m_data.m_value.object->clear();
20624 }
20625
20626 // it's now safe that current_item get destructed
20627 // since it doesn't have any children
20628 }
20629 }
20630
20631 switch (t)
20632 {
20633 case value_t::object:
20634 {
20635 AllocatorType<object_t> alloc;
20636 std::allocator_traits<decltype(alloc)>::destroy(alloc, object);
20637 std::allocator_traits<decltype(alloc)>::deallocate(alloc, object, 1);
20638 break;
20639 }
20640
20641 case value_t::array:
20642 {
20643 AllocatorType<array_t> alloc;
20644 std::allocator_traits<decltype(alloc)>::destroy(alloc, array);
20645 std::allocator_traits<decltype(alloc)>::deallocate(alloc, array, 1);
20646 break;
20647 }
20648
20649 case value_t::string:
20650 {
20651 AllocatorType<string_t> alloc;
20652 std::allocator_traits<decltype(alloc)>::destroy(alloc, string);
20653 std::allocator_traits<decltype(alloc)>::deallocate(alloc, string, 1);
20654 break;
20655 }
20656
20657 case value_t::binary:
20658 {
20659 AllocatorType<binary_t> alloc;
20660 std::allocator_traits<decltype(alloc)>::destroy(alloc, binary);
20661 std::allocator_traits<decltype(alloc)>::deallocate(alloc, binary, 1);
20662 break;
20663 }
20664
20665 case value_t::null:
20666 case value_t::boolean:
20667 case value_t::number_integer:
20668 case value_t::number_unsigned:
20669 case value_t::number_float:
20670 case value_t::discarded:
20671 default:
20672 {
20673 break;
20674 }
20675 }
20676 }
20677 };
20678
20679 private:
20680 /*!
20681 @brief checks the class invariants
20682
20683 This function asserts the class invariants. It needs to be called at the
20684 end of every constructor to make sure that created objects respect the
20685 invariant. Furthermore, it has to be called each time the type of a JSON
20686 value is changed, because the invariant expresses a relationship between
20687 @a m_type and @a m_value.
20688
20689 Furthermore, the parent relation is checked for arrays and objects: If
20690 @a check_parents true and the value is an array or object, then the
20691 container's elements must have the current value as parent.
20692
20693 @param[in] check_parents whether the parent relation should be checked.
20694 The value is true by default and should only be set to false
20695 during destruction of objects when the invariant does not
20696 need to hold.
20697 */
20698 void assert_invariant(bool check_parents = true) const noexcept
20699 {
20700 JSON_ASSERT(m_data.m_type != value_t::object || m_data.m_value.object != nullptr);
20701 JSON_ASSERT(m_data.m_type != value_t::array || m_data.m_value.array != nullptr);
20702 JSON_ASSERT(m_data.m_type != value_t::string || m_data.m_value.string != nullptr);
20703 JSON_ASSERT(m_data.m_type != value_t::binary || m_data.m_value.binary != nullptr);
20704
20705 #if JSON_DIAGNOSTICS
20706 JSON_TRY
20707 {
20708 // cppcheck-suppress assertWithSideEffect
20709 JSON_ASSERT(!check_parents || !is_structured() || std::all_of(begin(), end(), [this](const basic_json & j)
20710 {
20711 return j.m_parent == this;
20712 }));
20713 }
20714 JSON_CATCH(...) {} // LCOV_EXCL_LINE
20715 #endif
20716 static_cast<void>(check_parents);
20717 }
20718
20719 void set_parents()
20720 {
20721 #if JSON_DIAGNOSTICS
20722 switch (m_data.m_type)
20723 {
20724 case value_t::array:
20725 {
20726 for (auto& element : *m_data.m_value.array)
20727 {
20728 element.m_parent = this;
20729 }
20730 break;
20731 }
20732
20733 case value_t::object:
20734 {
20735 for (auto& element : *m_data.m_value.object)
20736 {
20737 element.second.m_parent = this;
20738 }
20739 break;
20740 }
20741
20742 case value_t::null:
20743 case value_t::string:
20744 case value_t::boolean:
20745 case value_t::number_integer:
20746 case value_t::number_unsigned:
20747 case value_t::number_float:
20748 case value_t::binary:
20749 case value_t::discarded:
20750 default:
20751 break;
20752 }
20753 #endif
20754 }
20755
20756 iterator set_parents(iterator it, typename iterator::difference_type count_set_parents)
20757 {
20758 #if JSON_DIAGNOSTICS
20759 for (typename iterator::difference_type i = 0; i < count_set_parents; ++i)
20760 {
20761 (it + i)->m_parent = this;
20762 }
20763 #else
20764 static_cast<void>(count_set_parents);
20765 #endif
20766 return it;
20767 }
20768
20769 reference set_parent(reference j, std::size_t old_capacity = detail::unknown_size())
20770 {
20771 #if JSON_DIAGNOSTICS
20772 if (old_capacity != detail::unknown_size())
20773 {
20774 // see https://github.com/nlohmann/json/issues/2838
20775 JSON_ASSERT(type() == value_t::array);
20776 if (JSON_HEDLEY_UNLIKELY(m_data.m_value.array->capacity() != old_capacity))
20777 {
20778 // capacity has changed: update all parents
20779 set_parents();
20780 return j;
20781 }
20782 }
20783
20784 // ordered_json uses a vector internally, so pointers could have
20785 // been invalidated; see https://github.com/nlohmann/json/issues/2962
20786 #ifdef JSON_HEDLEY_MSVC_VERSION
20787 #pragma warning(push )
20788 #pragma warning(disable : 4127) // ignore warning to replace if with if constexpr
20789 #endif
20790 if (detail::is_ordered_map<object_t>::value)
20791 {
20792 set_parents();
20793 return j;
20794 }
20795 #ifdef JSON_HEDLEY_MSVC_VERSION
20796 #pragma warning( pop )
20797 #endif
20798
20799 j.m_parent = this;
20800 #else
20801 static_cast<void>(j);
20802 static_cast<void>(old_capacity);
20803 #endif
20804 return j;
20805 }
20806
20807 public:
20808 //////////////////////////
20809 // JSON parser callback //
20810 //////////////////////////
20811
20812 /// @brief parser event types
20813 /// @sa https://json.nlohmann.me/api/basic_json/parse_event_t/
20814 using parse_event_t = detail::parse_event_t;
20815
20816 /// @brief per-element parser callback type
20817 /// @sa https://json.nlohmann.me/api/basic_json/parser_callback_t/
20818 using parser_callback_t = detail::parser_callback_t<basic_json>;
20819
20820 //////////////////
20821 // constructors //
20822 //////////////////
20823
20824 /// @name constructors and destructors
20825 /// Constructors of class @ref basic_json, copy/move constructor, copy
20826 /// assignment, static functions creating objects, and the destructor.
20827 /// @{
20828
20829 /// @brief create an empty value with a given type
20830 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
20831 basic_json(const value_t v)
20832 : m_data(v)
20833 {
20834 assert_invariant();
20835 }
20836
20837 /// @brief create a null object
20838 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
20839 basic_json(std::nullptr_t = nullptr) noexcept // NOLINT(bugprone-exception-escape)
20840 : basic_json(value_t::null)
20841 {
20842 assert_invariant();
20843 }
20844
20845 /// @brief create a JSON value from compatible types
20846 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
20847 template < typename CompatibleType,
20848 typename U = detail::uncvref_t<CompatibleType>,
20849 detail::enable_if_t <
20850 !detail::is_basic_json<U>::value && detail::is_compatible_type<basic_json_t, U>::value, int > = 0 >
20851 basic_json(CompatibleType && val) noexcept(noexcept( // NOLINT(bugprone-forwarding-reference-overload,bugprone-exception-escape)
20852 JSONSerializer<U>::to_json(std::declval<basic_json_t&>(),
20853 std::forward<CompatibleType>(val))))
20854 {
20855 JSONSerializer<U>::to_json(*this, std::forward<CompatibleType>(val));
20856 set_parents();
20857 assert_invariant();
20858 }
20859
20860 /// @brief create a JSON value from an existing one
20861 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
20862 template < typename BasicJsonType,
20863 detail::enable_if_t <
20864 detail::is_basic_json<BasicJsonType>::value&& !std::is_same<basic_json, BasicJsonType>::value, int > = 0 >
20865 basic_json(const BasicJsonType& val)
20866 #if JSON_DIAGNOSTIC_POSITIONS
20867 : start_position(val.start_pos()),
20868 end_position(val.end_pos())
20869 #endif
20870 {
20871 using other_boolean_t = typename BasicJsonType::boolean_t;
20872 using other_number_float_t = typename BasicJsonType::number_float_t;
20873 using other_number_integer_t = typename BasicJsonType::number_integer_t;
20874 using other_number_unsigned_t = typename BasicJsonType::number_unsigned_t;
20875 using other_string_t = typename BasicJsonType::string_t;
20876 using other_object_t = typename BasicJsonType::object_t;
20877 using other_array_t = typename BasicJsonType::array_t;
20878 using other_binary_t = typename BasicJsonType::binary_t;
20879
20880 switch (val.type())
20881 {
20882 case value_t::boolean:
20883 JSONSerializer<other_boolean_t>::to_json(*this, val.template get<other_boolean_t>());
20884 break;
20885 case value_t::number_float:
20886 JSONSerializer<other_number_float_t>::to_json(*this, val.template get<other_number_float_t>());
20887 break;
20888 case value_t::number_integer:
20889 JSONSerializer<other_number_integer_t>::to_json(*this, val.template get<other_number_integer_t>());
20890 break;
20891 case value_t::number_unsigned:
20892 JSONSerializer<other_number_unsigned_t>::to_json(*this, val.template get<other_number_unsigned_t>());
20893 break;
20894 case value_t::string:
20895 JSONSerializer<other_string_t>::to_json(*this, val.template get_ref<const other_string_t&>());
20896 break;
20897 case value_t::object:
20898 JSONSerializer<other_object_t>::to_json(*this, val.template get_ref<const other_object_t&>());
20899 break;
20900 case value_t::array:
20901 JSONSerializer<other_array_t>::to_json(*this, val.template get_ref<const other_array_t&>());
20902 break;
20903 case value_t::binary:
20904 JSONSerializer<other_binary_t>::to_json(*this, val.template get_ref<const other_binary_t&>());
20905 break;
20906 case value_t::null:
20907 *this = nullptr;
20908 break;
20909 case value_t::discarded:
20910 m_data.m_type = value_t::discarded;
20911 break;
20912 default: // LCOV_EXCL_LINE
20913 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
20914 }
20915 JSON_ASSERT(m_data.m_type == val.type());
20916
20917 set_parents();
20918 assert_invariant();
20919 }
20920
20921 /// @brief create a container (array or object) from an initializer list
20922 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
20923 basic_json(initializer_list_t init,
20924 bool type_deduction = true,
20925 value_t manual_type = value_t::array)
20926 {
20927 // check if each element is an array with two elements whose first
20928 // element is a string
20929 bool is_an_object = std::all_of(init.begin(), init.end(),
20930 [](const detail::json_ref<basic_json>& element_ref)
20931 {
20932 // The cast is to ensure op[size_type] is called, bearing in mind size_type may not be int;
20933 // (many string types can be constructed from 0 via its null-pointer guise, so we get a
20934 // broken call to op[key_type], the wrong semantics and a 4804 warning on Windows)
20935 return element_ref->is_array() && element_ref->size() == 2 && (*element_ref)[static_cast<size_type>(0)].is_string();
20936 });
20937
20938 // adjust type if type deduction is not wanted
20939 if (!type_deduction)
20940 {
20941 // if array is wanted, do not create an object though possible
20942 if (manual_type == value_t::array)
20943 {
20944 is_an_object = false;
20945 }
20946
20947 // if object is wanted but impossible, throw an exception
20948 if (JSON_HEDLEY_UNLIKELY(manual_type == value_t::object && !is_an_object))
20949 {
20950 JSON_THROW(type_error::create(301, "cannot create object from initializer list", nullptr));
20951 }
20952 }
20953
20954 if (is_an_object)
20955 {
20956 // the initializer list is a list of pairs -> create object
20957 m_data.m_type = value_t::object;
20958 m_data.m_value = value_t::object;
20959
20960 for (auto& element_ref : init)
20961 {
20962 auto element = element_ref.moved_or_copied();
20963 m_data.m_value.object->emplace(
20964 std::move(*((*element.m_data.m_value.array)[0].m_data.m_value.string)),
20965 std::move((*element.m_data.m_value.array)[1]));
20966 }
20967 }
20968 else
20969 {
20970 // the initializer list describes an array -> create array
20971 m_data.m_type = value_t::array;
20972 m_data.m_value.array = create<array_t>(init.begin(), init.end());
20973 }
20974
20975 set_parents();
20976 assert_invariant();
20977 }
20978
20979 /// @brief explicitly create a binary array (without subtype)
20980 /// @sa https://json.nlohmann.me/api/basic_json/binary/
20981 JSON_HEDLEY_WARN_UNUSED_RESULT
20982 static basic_json binary(const typename binary_t::container_type& init)
20983 {
20984 auto res = basic_json();
20985 res.m_data.m_type = value_t::binary;
20986 res.m_data.m_value = init;
20987 return res;
20988 }
20989
20990 /// @brief explicitly create a binary array (with subtype)
20991 /// @sa https://json.nlohmann.me/api/basic_json/binary/
20992 JSON_HEDLEY_WARN_UNUSED_RESULT
20993 static basic_json binary(const typename binary_t::container_type& init, typename binary_t::subtype_type subtype)
20994 {
20995 auto res = basic_json();
20996 res.m_data.m_type = value_t::binary;
20997 res.m_data.m_value = binary_t(init, subtype);
20998 return res;
20999 }
21000
21001 /// @brief explicitly create a binary array
21002 /// @sa https://json.nlohmann.me/api/basic_json/binary/
21003 JSON_HEDLEY_WARN_UNUSED_RESULT
21004 static basic_json binary(typename binary_t::container_type&& init)
21005 {
21006 auto res = basic_json();
21007 res.m_data.m_type = value_t::binary;
21008 res.m_data.m_value = std::move(init);
21009 return res;
21010 }
21011
21012 /// @brief explicitly create a binary array (with subtype)
21013 /// @sa https://json.nlohmann.me/api/basic_json/binary/
21014 JSON_HEDLEY_WARN_UNUSED_RESULT
21015 static basic_json binary(typename binary_t::container_type&& init, typename binary_t::subtype_type subtype)
21016 {
21017 auto res = basic_json();
21018 res.m_data.m_type = value_t::binary;
21019 res.m_data.m_value = binary_t(std::move(init), subtype);
21020 return res;
21021 }
21022
21023 /// @brief explicitly create an array from an initializer list
21024 /// @sa https://json.nlohmann.me/api/basic_json/array/
21025 JSON_HEDLEY_WARN_UNUSED_RESULT
21026 static basic_json array(initializer_list_t init = {})
21027 {
21028 return basic_json(init, false, value_t::array);
21029 }
21030
21031 /// @brief explicitly create an object from an initializer list
21032 /// @sa https://json.nlohmann.me/api/basic_json/object/
21033 JSON_HEDLEY_WARN_UNUSED_RESULT
21034 static basic_json object(initializer_list_t init = {})
21035 {
21036 return basic_json(init, false, value_t::object);
21037 }
21038
21039 /// @brief construct an array with count copies of given value
21040 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
21041 basic_json(size_type cnt, const basic_json& val):
21042 m_data{cnt, val}
21043 {
21044 set_parents();
21045 assert_invariant();
21046 }
21047
21048 /// @brief construct a JSON container given an iterator range
21049 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
21050 template < class InputIT, typename std::enable_if <
21051 std::is_same<InputIT, typename basic_json_t::iterator>::value ||
21052 std::is_same<InputIT, typename basic_json_t::const_iterator>::value, int >::type = 0 >
21053 basic_json(InputIT first, InputIT last) // NOLINT(performance-unnecessary-value-param)
21054 {
21055 JSON_ASSERT(first.m_object != nullptr);
21056 JSON_ASSERT(last.m_object != nullptr);
21057
21058 // make sure iterator fits the current value
21059 if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
21060 {
21061 JSON_THROW(invalid_iterator::create(201, "iterators are not compatible", nullptr));
21062 }
21063
21064 // copy type from first iterator
21065 m_data.m_type = first.m_object->m_data.m_type;
21066
21067 // check if iterator range is complete for primitive values
21068 switch (m_data.m_type)
21069 {
21070 case value_t::boolean:
21071 case value_t::number_float:
21072 case value_t::number_integer:
21073 case value_t::number_unsigned:
21074 case value_t::string:
21075 {
21076 if (JSON_HEDLEY_UNLIKELY(!first.m_it.primitive_iterator.is_begin()
21077 || !last.m_it.primitive_iterator.is_end()))
21078 {
21079 JSON_THROW(invalid_iterator::create(204, "iterators out of range", first.m_object));
21080 }
21081 break;
21082 }
21083
21084 case value_t::null:
21085 case value_t::object:
21086 case value_t::array:
21087 case value_t::binary:
21088 case value_t::discarded:
21089 default:
21090 break;
21091 }
21092
21093 switch (m_data.m_type)
21094 {
21095 case value_t::number_integer:
21096 {
21097 m_data.m_value.number_integer = first.m_object->m_data.m_value.number_integer;
21098 break;
21099 }
21100
21101 case value_t::number_unsigned:
21102 {
21103 m_data.m_value.number_unsigned = first.m_object->m_data.m_value.number_unsigned;
21104 break;
21105 }
21106
21107 case value_t::number_float:
21108 {
21109 m_data.m_value.number_float = first.m_object->m_data.m_value.number_float;
21110 break;
21111 }
21112
21113 case value_t::boolean:
21114 {
21115 m_data.m_value.boolean = first.m_object->m_data.m_value.boolean;
21116 break;
21117 }
21118
21119 case value_t::string:
21120 {
21121 m_data.m_value = *first.m_object->m_data.m_value.string;
21122 break;
21123 }
21124
21125 case value_t::object:
21126 {
21127 m_data.m_value.object = create<object_t>(first.m_it.object_iterator,
21128 last.m_it.object_iterator);
21129 break;
21130 }
21131
21132 case value_t::array:
21133 {
21134 m_data.m_value.array = create<array_t>(first.m_it.array_iterator,
21135 last.m_it.array_iterator);
21136 break;
21137 }
21138
21139 case value_t::binary:
21140 {
21141 m_data.m_value = *first.m_object->m_data.m_value.binary;
21142 break;
21143 }
21144
21145 case value_t::null:
21146 case value_t::discarded:
21147 default:
21148 JSON_THROW(invalid_iterator::create(206, detail::concat("cannot construct with iterators from ", first.m_object->type_name()), first.m_object));
21149 }
21150
21151 set_parents();
21152 assert_invariant();
21153 }
21154
21155 ///////////////////////////////////////
21156 // other constructors and destructor //
21157 ///////////////////////////////////////
21158
21159 template<typename JsonRef,
21160 detail::enable_if_t<detail::conjunction<detail::is_json_ref<JsonRef>,
21161 std::is_same<typename JsonRef::value_type, basic_json>>::value, int> = 0 >
21162 basic_json(const JsonRef& ref) : basic_json(ref.moved_or_copied()) {}
21163
21164 /// @brief copy constructor
21165 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
21166 basic_json(const basic_json& other)
21167 : json_base_class_t(other)
21168 #if JSON_DIAGNOSTIC_POSITIONS
21169 , start_position(other.start_position)
21170 , end_position(other.end_position)
21171 #endif
21172 {
21173 m_data.m_type = other.m_data.m_type;
21174 // check of passed value is valid
21175 other.assert_invariant();
21176
21177 switch (m_data.m_type)
21178 {
21179 case value_t::object:
21180 {
21181 m_data.m_value = *other.m_data.m_value.object;
21182 break;
21183 }
21184
21185 case value_t::array:
21186 {
21187 m_data.m_value = *other.m_data.m_value.array;
21188 break;
21189 }
21190
21191 case value_t::string:
21192 {
21193 m_data.m_value = *other.m_data.m_value.string;
21194 break;
21195 }
21196
21197 case value_t::boolean:
21198 {
21199 m_data.m_value = other.m_data.m_value.boolean;
21200 break;
21201 }
21202
21203 case value_t::number_integer:
21204 {
21205 m_data.m_value = other.m_data.m_value.number_integer;
21206 break;
21207 }
21208
21209 case value_t::number_unsigned:
21210 {
21211 m_data.m_value = other.m_data.m_value.number_unsigned;
21212 break;
21213 }
21214
21215 case value_t::number_float:
21216 {
21217 m_data.m_value = other.m_data.m_value.number_float;
21218 break;
21219 }
21220
21221 case value_t::binary:
21222 {
21223 m_data.m_value = *other.m_data.m_value.binary;
21224 break;
21225 }
21226
21227 case value_t::null:
21228 case value_t::discarded:
21229 default:
21230 break;
21231 }
21232
21233 set_parents();
21234 assert_invariant();
21235 }
21236
21237 /// @brief move constructor
21238 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
21239 basic_json(basic_json&& other) noexcept
21240 : json_base_class_t(std::forward<json_base_class_t>(other)),
21241 m_data(std::move(other.m_data)) // cppcheck-suppress[accessForwarded] TODO check
21242 #if JSON_DIAGNOSTIC_POSITIONS
21243 , start_position(other.start_position) // cppcheck-suppress[accessForwarded] TODO check
21244 , end_position(other.end_position) // cppcheck-suppress[accessForwarded] TODO check
21245 #endif
21246 {
21247 // check that passed value is valid
21248 other.assert_invariant(false); // cppcheck-suppress[accessForwarded]
21249
21250 // invalidate payload
21251 other.m_data.m_type = value_t::null;
21252 other.m_data.m_value = {};
21253
21254 #if JSON_DIAGNOSTIC_POSITIONS
21255 other.start_position = std::string::npos;
21256 other.end_position = std::string::npos;
21257 #endif
21258
21259 set_parents();
21260 assert_invariant();
21261 }
21262
21263 /// @brief copy assignment
21264 /// @sa https://json.nlohmann.me/api/basic_json/operator=/
21265 basic_json& operator=(basic_json other) noexcept (
21266 std::is_nothrow_move_constructible<value_t>::value&&
21267 std::is_nothrow_move_assignable<value_t>::value&&
21268 std::is_nothrow_move_constructible<json_value>::value&&
21269 std::is_nothrow_move_assignable<json_value>::value&&
21270 std::is_nothrow_move_assignable<json_base_class_t>::value
21271 )
21272 {
21273 // check that passed value is valid
21274 other.assert_invariant();
21275
21276 using std::swap;
21277 swap(m_data.m_type, other.m_data.m_type);
21278 swap(m_data.m_value, other.m_data.m_value);
21279
21280 #if JSON_DIAGNOSTIC_POSITIONS
21281 swap(start_position, other.start_position);
21282 swap(end_position, other.end_position);
21283 #endif
21284
21285 json_base_class_t::operator=(std::move(other));
21286
21287 set_parents();
21288 assert_invariant();
21289 return *this;
21290 }
21291
21292 /// @brief destructor
21293 /// @sa https://json.nlohmann.me/api/basic_json/~basic_json/
21294 ~basic_json() noexcept
21295 {
21296 assert_invariant(false);
21297 }
21298
21299 /// @}
21300
21301 public:
21302 ///////////////////////
21303 // object inspection //
21304 ///////////////////////
21305
21306 /// @name object inspection
21307 /// Functions to inspect the type of a JSON value.
21308 /// @{
21309
21310 /// @brief serialization
21311 /// @sa https://json.nlohmann.me/api/basic_json/dump/
21312 string_t dump(const int indent = -1,
21313 const char indent_char = ' ',
21314 const bool ensure_ascii = false,
21315 const error_handler_t error_handler = error_handler_t::strict) const
21316 {
21317 string_t result;
21318 serializer s(detail::output_adapter<char, string_t>(result), indent_char, error_handler);
21319
21320 if (indent >= 0)
21321 {
21322 s.dump(*this, true, ensure_ascii, static_cast<unsigned int>(indent));
21323 }
21324 else
21325 {
21326 s.dump(*this, false, ensure_ascii, 0);
21327 }
21328
21329 return result;
21330 }
21331
21332 /// @brief return the type of the JSON value (explicit)
21333 /// @sa https://json.nlohmann.me/api/basic_json/type/
21334 constexpr value_t type() const noexcept
21335 {
21336 return m_data.m_type;
21337 }
21338
21339 /// @brief return whether type is primitive
21340 /// @sa https://json.nlohmann.me/api/basic_json/is_primitive/
21341 constexpr bool is_primitive() const noexcept
21342 {
21343 return is_null() || is_string() || is_boolean() || is_number() || is_binary();
21344 }
21345
21346 /// @brief return whether type is structured
21347 /// @sa https://json.nlohmann.me/api/basic_json/is_structured/
21348 constexpr bool is_structured() const noexcept
21349 {
21350 return is_array() || is_object();
21351 }
21352
21353 /// @brief return whether value is null
21354 /// @sa https://json.nlohmann.me/api/basic_json/is_null/
21355 constexpr bool is_null() const noexcept
21356 {
21357 return m_data.m_type == value_t::null;
21358 }
21359
21360 /// @brief return whether value is a boolean
21361 /// @sa https://json.nlohmann.me/api/basic_json/is_boolean/
21362 constexpr bool is_boolean() const noexcept
21363 {
21364 return m_data.m_type == value_t::boolean;
21365 }
21366
21367 /// @brief return whether value is a number
21368 /// @sa https://json.nlohmann.me/api/basic_json/is_number/
21369 constexpr bool is_number() const noexcept
21370 {
21371 return is_number_integer() || is_number_float();
21372 }
21373
21374 /// @brief return whether value is an integer number
21375 /// @sa https://json.nlohmann.me/api/basic_json/is_number_integer/
21376 constexpr bool is_number_integer() const noexcept
21377 {
21378 return m_data.m_type == value_t::number_integer || m_data.m_type == value_t::number_unsigned;
21379 }
21380
21381 /// @brief return whether value is an unsigned integer number
21382 /// @sa https://json.nlohmann.me/api/basic_json/is_number_unsigned/
21383 constexpr bool is_number_unsigned() const noexcept
21384 {
21385 return m_data.m_type == value_t::number_unsigned;
21386 }
21387
21388 /// @brief return whether value is a floating-point number
21389 /// @sa https://json.nlohmann.me/api/basic_json/is_number_float/
21390 constexpr bool is_number_float() const noexcept
21391 {
21392 return m_data.m_type == value_t::number_float;
21393 }
21394
21395 /// @brief return whether value is an object
21396 /// @sa https://json.nlohmann.me/api/basic_json/is_object/
21397 constexpr bool is_object() const noexcept
21398 {
21399 return m_data.m_type == value_t::object;
21400 }
21401
21402 /// @brief return whether value is an array
21403 /// @sa https://json.nlohmann.me/api/basic_json/is_array/
21404 constexpr bool is_array() const noexcept
21405 {
21406 return m_data.m_type == value_t::array;
21407 }
21408
21409 /// @brief return whether value is a string
21410 /// @sa https://json.nlohmann.me/api/basic_json/is_string/
21411 constexpr bool is_string() const noexcept
21412 {
21413 return m_data.m_type == value_t::string;
21414 }
21415
21416 /// @brief return whether value is a binary array
21417 /// @sa https://json.nlohmann.me/api/basic_json/is_binary/
21418 constexpr bool is_binary() const noexcept
21419 {
21420 return m_data.m_type == value_t::binary;
21421 }
21422
21423 /// @brief return whether value is discarded
21424 /// @sa https://json.nlohmann.me/api/basic_json/is_discarded/
21425 constexpr bool is_discarded() const noexcept
21426 {
21427 return m_data.m_type == value_t::discarded;
21428 }
21429
21430 /// @brief return the type of the JSON value (implicit)
21431 /// @sa https://json.nlohmann.me/api/basic_json/operator_value_t/
21432 constexpr operator value_t() const noexcept
21433 {
21434 return m_data.m_type;
21435 }
21436
21437 /// @}
21438
21439 private:
21440 //////////////////
21441 // value access //
21442 //////////////////
21443
21444 /// get a boolean (explicit)
21445 boolean_t get_impl(boolean_t* /*unused*/) const
21446 {
21447 if (JSON_HEDLEY_LIKELY(is_boolean()))
21448 {
21449 return m_data.m_value.boolean;
21450 }
21451
21452 JSON_THROW(type_error::create(302, detail::concat("type must be boolean, but is ", type_name()), this));
21453 }
21454
21455 /// get a pointer to the value (object)
21456 object_t* get_impl_ptr(object_t* /*unused*/) noexcept
21457 {
21458 return is_object() ? m_data.m_value.object : nullptr;
21459 }
21460
21461 /// get a pointer to the value (object)
21462 constexpr const object_t* get_impl_ptr(const object_t* /*unused*/) const noexcept
21463 {
21464 return is_object() ? m_data.m_value.object : nullptr;
21465 }
21466
21467 /// get a pointer to the value (array)
21468 array_t* get_impl_ptr(array_t* /*unused*/) noexcept
21469 {
21470 return is_array() ? m_data.m_value.array : nullptr;
21471 }
21472
21473 /// get a pointer to the value (array)
21474 constexpr const array_t* get_impl_ptr(const array_t* /*unused*/) const noexcept
21475 {
21476 return is_array() ? m_data.m_value.array : nullptr;
21477 }
21478
21479 /// get a pointer to the value (string)
21480 string_t* get_impl_ptr(string_t* /*unused*/) noexcept
21481 {
21482 return is_string() ? m_data.m_value.string : nullptr;
21483 }
21484
21485 /// get a pointer to the value (string)
21486 constexpr const string_t* get_impl_ptr(const string_t* /*unused*/) const noexcept
21487 {
21488 return is_string() ? m_data.m_value.string : nullptr;
21489 }
21490
21491 /// get a pointer to the value (boolean)
21492 boolean_t* get_impl_ptr(boolean_t* /*unused*/) noexcept
21493 {
21494 return is_boolean() ? &m_data.m_value.boolean : nullptr;
21495 }
21496
21497 /// get a pointer to the value (boolean)
21498 constexpr const boolean_t* get_impl_ptr(const boolean_t* /*unused*/) const noexcept
21499 {
21500 return is_boolean() ? &m_data.m_value.boolean : nullptr;
21501 }
21502
21503 /// get a pointer to the value (integer number)
21504 number_integer_t* get_impl_ptr(number_integer_t* /*unused*/) noexcept
21505 {
21506 return m_data.m_type == value_t::number_integer ? &m_data.m_value.number_integer : nullptr;
21507 }
21508
21509 /// get a pointer to the value (integer number)
21510 constexpr const number_integer_t* get_impl_ptr(const number_integer_t* /*unused*/) const noexcept
21511 {
21512 return m_data.m_type == value_t::number_integer ? &m_data.m_value.number_integer : nullptr;
21513 }
21514
21515 /// get a pointer to the value (unsigned number)
21516 number_unsigned_t* get_impl_ptr(number_unsigned_t* /*unused*/) noexcept
21517 {
21518 return is_number_unsigned() ? &m_data.m_value.number_unsigned : nullptr;
21519 }
21520
21521 /// get a pointer to the value (unsigned number)
21522 constexpr const number_unsigned_t* get_impl_ptr(const number_unsigned_t* /*unused*/) const noexcept
21523 {
21524 return is_number_unsigned() ? &m_data.m_value.number_unsigned : nullptr;
21525 }
21526
21527 /// get a pointer to the value (floating-point number)
21528 number_float_t* get_impl_ptr(number_float_t* /*unused*/) noexcept
21529 {
21530 return is_number_float() ? &m_data.m_value.number_float : nullptr;
21531 }
21532
21533 /// get a pointer to the value (floating-point number)
21534 constexpr const number_float_t* get_impl_ptr(const number_float_t* /*unused*/) const noexcept
21535 {
21536 return is_number_float() ? &m_data.m_value.number_float : nullptr;
21537 }
21538
21539 /// get a pointer to the value (binary)
21540 binary_t* get_impl_ptr(binary_t* /*unused*/) noexcept
21541 {
21542 return is_binary() ? m_data.m_value.binary : nullptr;
21543 }
21544
21545 /// get a pointer to the value (binary)
21546 constexpr const binary_t* get_impl_ptr(const binary_t* /*unused*/) const noexcept
21547 {
21548 return is_binary() ? m_data.m_value.binary : nullptr;
21549 }
21550
21551 /*!
21552 @brief helper function to implement get_ref()
21553
21554 This function helps to implement get_ref() without code duplication for
21555 const and non-const overloads
21556
21557 @tparam ThisType will be deduced as `basic_json` or `const basic_json`
21558
21559 @throw type_error.303 if ReferenceType does not match underlying value
21560 type of the current JSON
21561 */
21562 template<typename ReferenceType, typename ThisType>
21563 static ReferenceType get_ref_impl(ThisType& obj)
21564 {
21565 // delegate the call to get_ptr<>()
21566 auto* ptr = obj.template get_ptr<typename std::add_pointer<ReferenceType>::type>();
21567
21568 if (JSON_HEDLEY_LIKELY(ptr != nullptr))
21569 {
21570 return *ptr;
21571 }
21572
21573 JSON_THROW(type_error::create(303, detail::concat("incompatible ReferenceType for get_ref, actual type is ", obj.type_name()), &obj));
21574 }
21575
21576 public:
21577 /// @name value access
21578 /// Direct access to the stored value of a JSON value.
21579 /// @{
21580
21581 /// @brief get a pointer value (implicit)
21582 /// @sa https://json.nlohmann.me/api/basic_json/get_ptr/
21583 template<typename PointerType, typename std::enable_if<
21584 std::is_pointer<PointerType>::value, int>::type = 0>
21585 auto get_ptr() noexcept -> decltype(std::declval<basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
21586 {
21587 // delegate the call to get_impl_ptr<>()
21588 return get_impl_ptr(static_cast<PointerType>(nullptr));
21589 }
21590
21591 /// @brief get a pointer value (implicit)
21592 /// @sa https://json.nlohmann.me/api/basic_json/get_ptr/
21593 template < typename PointerType, typename std::enable_if <
21594 std::is_pointer<PointerType>::value&&
21595 std::is_const<typename std::remove_pointer<PointerType>::type>::value, int >::type = 0 >
21596 constexpr auto get_ptr() const noexcept -> decltype(std::declval<const basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
21597 {
21598 // delegate the call to get_impl_ptr<>() const
21599 return get_impl_ptr(static_cast<PointerType>(nullptr));
21600 }
21601
21602 private:
21603 /*!
21604 @brief get a value (explicit)
21605
21606 Explicit type conversion between the JSON value and a compatible value
21607 which is [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
21608 and [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
21609 The value is converted by calling the @ref json_serializer<ValueType>
21610 `from_json()` method.
21611
21612 The function is equivalent to executing
21613 @code {.cpp}
21614 ValueType ret;
21615 JSONSerializer<ValueType>::from_json(*this, ret);
21616 return ret;
21617 @endcode
21618
21619 This overloads is chosen if:
21620 - @a ValueType is not @ref basic_json,
21621 - @ref json_serializer<ValueType> has a `from_json()` method of the form
21622 `void from_json(const basic_json&, ValueType&)`, and
21623 - @ref json_serializer<ValueType> does not have a `from_json()` method of
21624 the form `ValueType from_json(const basic_json&)`
21625
21626 @tparam ValueType the returned value type
21627
21628 @return copy of the JSON value, converted to @a ValueType
21629
21630 @throw what @ref json_serializer<ValueType> `from_json()` method throws
21631
21632 @liveexample{The example below shows several conversions from JSON values
21633 to other types. There a few things to note: (1) Floating-point numbers can
21634 be converted to integers\, (2) A JSON array can be converted to a standard
21635 `std::vector<short>`\, (3) A JSON object can be converted to C++
21636 associative containers such as `std::unordered_map<std::string\,
21637 json>`.,get__ValueType_const}
21638
21639 @since version 2.1.0
21640 */
21641 template < typename ValueType,
21642 detail::enable_if_t <
21643 detail::is_default_constructible<ValueType>::value&&
21644 detail::has_from_json<basic_json_t, ValueType>::value,
21645 int > = 0 >
21646 ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept(
21647 JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), std::declval<ValueType&>())))
21648 {
21649 auto ret = ValueType();
21650 JSONSerializer<ValueType>::from_json(*this, ret);
21651 return ret;
21652 }
21653
21654 /*!
21655 @brief get a value (explicit); special case
21656
21657 Explicit type conversion between the JSON value and a compatible value
21658 which is **not** [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
21659 and **not** [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
21660 The value is converted by calling the @ref json_serializer<ValueType>
21661 `from_json()` method.
21662
21663 The function is equivalent to executing
21664 @code {.cpp}
21665 return JSONSerializer<ValueType>::from_json(*this);
21666 @endcode
21667
21668 This overloads is chosen if:
21669 - @a ValueType is not @ref basic_json and
21670 - @ref json_serializer<ValueType> has a `from_json()` method of the form
21671 `ValueType from_json(const basic_json&)`
21672
21673 @note If @ref json_serializer<ValueType> has both overloads of
21674 `from_json()`, this one is chosen.
21675
21676 @tparam ValueType the returned value type
21677
21678 @return copy of the JSON value, converted to @a ValueType
21679
21680 @throw what @ref json_serializer<ValueType> `from_json()` method throws
21681
21682 @since version 2.1.0
21683 */
21684 template < typename ValueType,
21685 detail::enable_if_t <
21686 detail::has_non_default_from_json<basic_json_t, ValueType>::value,
21687 int > = 0 >
21688 ValueType get_impl(detail::priority_tag<1> /*unused*/) const noexcept(noexcept(
21689 JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>())))
21690 {
21691 return JSONSerializer<ValueType>::from_json(*this);
21692 }
21693
21694 /*!
21695 @brief get special-case overload
21696
21697 This overloads converts the current @ref basic_json in a different
21698 @ref basic_json type
21699
21700 @tparam BasicJsonType == @ref basic_json
21701
21702 @return a copy of *this, converted into @a BasicJsonType
21703
21704 @complexity Depending on the implementation of the called `from_json()`
21705 method.
21706
21707 @since version 3.2.0
21708 */
21709 template < typename BasicJsonType,
21710 detail::enable_if_t <
21711 detail::is_basic_json<BasicJsonType>::value,
21712 int > = 0 >
21713 BasicJsonType get_impl(detail::priority_tag<2> /*unused*/) const
21714 {
21715 return *this;
21716 }
21717
21718 /*!
21719 @brief get special-case overload
21720
21721 This overloads avoids a lot of template boilerplate, it can be seen as the
21722 identity method
21723
21724 @tparam BasicJsonType == @ref basic_json
21725
21726 @return a copy of *this
21727
21728 @complexity Constant.
21729
21730 @since version 2.1.0
21731 */
21732 template<typename BasicJsonType,
21733 detail::enable_if_t<
21734 std::is_same<BasicJsonType, basic_json_t>::value,
21735 int> = 0>
21736 basic_json get_impl(detail::priority_tag<3> /*unused*/) const
21737 {
21738 return *this;
21739 }
21740
21741 /*!
21742 @brief get a pointer value (explicit)
21743 @copydoc get()
21744 */
21745 template<typename PointerType,
21746 detail::enable_if_t<
21747 std::is_pointer<PointerType>::value,
21748 int> = 0>
21749 constexpr auto get_impl(detail::priority_tag<4> /*unused*/) const noexcept
21750 -> decltype(std::declval<const basic_json_t&>().template get_ptr<PointerType>())
21751 {
21752 // delegate the call to get_ptr
21753 return get_ptr<PointerType>();
21754 }
21755
21756 public:
21757 /*!
21758 @brief get a (pointer) value (explicit)
21759
21760 Performs explicit type conversion between the JSON value and a compatible value if required.
21761
21762 - If the requested type is a pointer to the internally stored JSON value that pointer is returned.
21763 No copies are made.
21764
21765 - If the requested type is the current @ref basic_json, or a different @ref basic_json convertible
21766 from the current @ref basic_json.
21767
21768 - Otherwise the value is converted by calling the @ref json_serializer<ValueType> `from_json()`
21769 method.
21770
21771 @tparam ValueTypeCV the provided value type
21772 @tparam ValueType the returned value type
21773
21774 @return copy of the JSON value, converted to @tparam ValueType if necessary
21775
21776 @throw what @ref json_serializer<ValueType> `from_json()` method throws if conversion is required
21777
21778 @since version 2.1.0
21779 */
21780 template < typename ValueTypeCV, typename ValueType = detail::uncvref_t<ValueTypeCV>>
21781 #if defined(JSON_HAS_CPP_14)
21782 constexpr
21783 #endif
21784 auto get() const noexcept(
21785 noexcept(std::declval<const basic_json_t&>().template get_impl<ValueType>(detail::priority_tag<4> {})))
21786 -> decltype(std::declval<const basic_json_t&>().template get_impl<ValueType>(detail::priority_tag<4> {}))
21787 {
21788 // we cannot static_assert on ValueTypeCV being non-const, because
21789 // there is support for get<const basic_json_t>(), which is why we
21790 // still need the uncvref
21791 static_assert(!std::is_reference<ValueTypeCV>::value,
21792 "get() cannot be used with reference types, you might want to use get_ref()");
21793 return get_impl<ValueType>(detail::priority_tag<4> {});
21794 }
21795
21796 /*!
21797 @brief get a pointer value (explicit)
21798
21799 Explicit pointer access to the internally stored JSON value. No copies are
21800 made.
21801
21802 @warning The pointer becomes invalid if the underlying JSON object
21803 changes.
21804
21805 @tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
21806 object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
21807 @ref number_unsigned_t, or @ref number_float_t.
21808
21809 @return pointer to the internally stored JSON value if the requested
21810 pointer type @a PointerType fits to the JSON value; `nullptr` otherwise
21811
21812 @complexity Constant.
21813
21814 @liveexample{The example below shows how pointers to internal values of a
21815 JSON value can be requested. Note that no type conversions are made and a
21816 `nullptr` is returned if the value and the requested pointer type does not
21817 match.,get__PointerType}
21818
21819 @sa see @ref get_ptr() for explicit pointer-member access
21820
21821 @since version 1.0.0
21822 */
21823 template<typename PointerType, typename std::enable_if<
21824 std::is_pointer<PointerType>::value, int>::type = 0>
21825 auto get() noexcept -> decltype(std::declval<basic_json_t&>().template get_ptr<PointerType>())
21826 {
21827 // delegate the call to get_ptr
21828 return get_ptr<PointerType>();
21829 }
21830
21831 /// @brief get a value (explicit)
21832 /// @sa https://json.nlohmann.me/api/basic_json/get_to/
21833 template < typename ValueType,
21834 detail::enable_if_t <
21835 !detail::is_basic_json<ValueType>::value&&
21836 detail::has_from_json<basic_json_t, ValueType>::value,
21837 int > = 0 >
21838 ValueType & get_to(ValueType& v) const noexcept(noexcept(
21839 JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), v)))
21840 {
21841 JSONSerializer<ValueType>::from_json(*this, v);
21842 return v;
21843 }
21844
21845 // specialization to allow calling get_to with a basic_json value
21846 // see https://github.com/nlohmann/json/issues/2175
21847 template<typename ValueType,
21848 detail::enable_if_t <
21849 detail::is_basic_json<ValueType>::value,
21850 int> = 0>
21851 ValueType & get_to(ValueType& v) const
21852 {
21853 v = *this;
21854 return v;
21855 }
21856
21857 template <
21858 typename T, std::size_t N,
21859 typename Array = T (&)[N], // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
21860 detail::enable_if_t <
21861 detail::has_from_json<basic_json_t, Array>::value, int > = 0 >
21862 Array get_to(T (&v)[N]) const // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
21863 noexcept(noexcept(JSONSerializer<Array>::from_json(
21864 std::declval<const basic_json_t&>(), v)))
21865 {
21866 JSONSerializer<Array>::from_json(*this, v);
21867 return v;
21868 }
21869
21870 /// @brief get a reference value (implicit)
21871 /// @sa https://json.nlohmann.me/api/basic_json/get_ref/
21872 template<typename ReferenceType, typename std::enable_if<
21873 std::is_reference<ReferenceType>::value, int>::type = 0>
21874 ReferenceType get_ref()
21875 {
21876 // delegate call to get_ref_impl
21877 return get_ref_impl<ReferenceType>(*this);
21878 }
21879
21880 /// @brief get a reference value (implicit)
21881 /// @sa https://json.nlohmann.me/api/basic_json/get_ref/
21882 template < typename ReferenceType, typename std::enable_if <
21883 std::is_reference<ReferenceType>::value&&
21884 std::is_const<typename std::remove_reference<ReferenceType>::type>::value, int >::type = 0 >
21885 ReferenceType get_ref() const
21886 {
21887 // delegate call to get_ref_impl
21888 return get_ref_impl<ReferenceType>(*this);
21889 }
21890
21891 /*!
21892 @brief get a value (implicit)
21893
21894 Implicit type conversion between the JSON value and a compatible value.
21895 The call is realized by calling @ref get() const.
21896
21897 @tparam ValueType non-pointer type compatible to the JSON value, for
21898 instance `int` for JSON integer numbers, `bool` for JSON booleans, or
21899 `std::vector` types for JSON arrays. The character type of @ref string_t
21900 as well as an initializer list of this type is excluded to avoid
21901 ambiguities as these types implicitly convert to `std::string`.
21902
21903 @return copy of the JSON value, converted to type @a ValueType
21904
21905 @throw type_error.302 in case passed type @a ValueType is incompatible
21906 to the JSON value type (e.g., the JSON value is of type boolean, but a
21907 string is requested); see example below
21908
21909 @complexity Linear in the size of the JSON value.
21910
21911 @liveexample{The example below shows several conversions from JSON values
21912 to other types. There a few things to note: (1) Floating-point numbers can
21913 be converted to integers\, (2) A JSON array can be converted to a standard
21914 `std::vector<short>`\, (3) A JSON object can be converted to C++
21915 associative containers such as `std::unordered_map<std::string\,
21916 json>`.,operator__ValueType}
21917
21918 @since version 1.0.0
21919 */
21920 template < typename ValueType, typename std::enable_if <
21921 detail::conjunction <
21922 detail::negation<std::is_pointer<ValueType>>,
21923 detail::negation<std::is_same<ValueType, std::nullptr_t>>,
21924 detail::negation<std::is_same<ValueType, detail::json_ref<basic_json>>>,
21925 detail::negation<std::is_same<ValueType, typename string_t::value_type>>,
21926 detail::negation<detail::is_basic_json<ValueType>>,
21927 detail::negation<std::is_same<ValueType, std::initializer_list<typename string_t::value_type>>>,
21928 #if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER <= 1914))
21929 detail::negation<std::is_same<ValueType, std::string_view>>,
21930 #endif
21931 #if defined(JSON_HAS_CPP_17) && JSON_HAS_STATIC_RTTI
21932 detail::negation<std::is_same<ValueType, std::any>>,
21933 #endif
21934 detail::is_detected_lazy<detail::get_template_function, const basic_json_t&, ValueType>
21935 >::value, int >::type = 0 >
21936 JSON_EXPLICIT operator ValueType() const
21937 {
21938 // delegate the call to get<>() const
21939 return get<ValueType>();
21940 }
21941
21942 /// @brief get a binary value
21943 /// @sa https://json.nlohmann.me/api/basic_json/get_binary/
21944 binary_t& get_binary()
21945 {
21946 if (!is_binary())
21947 {
21948 JSON_THROW(type_error::create(302, detail::concat("type must be binary, but is ", type_name()), this));
21949 }
21950
21951 return *get_ptr<binary_t*>();
21952 }
21953
21954 /// @brief get a binary value
21955 /// @sa https://json.nlohmann.me/api/basic_json/get_binary/
21956 const binary_t& get_binary() const
21957 {
21958 if (!is_binary())
21959 {
21960 JSON_THROW(type_error::create(302, detail::concat("type must be binary, but is ", type_name()), this));
21961 }
21962
21963 return *get_ptr<const binary_t*>();
21964 }
21965
21966 /// @}
21967
21968 ////////////////////
21969 // element access //
21970 ////////////////////
21971
21972 /// @name element access
21973 /// Access to the JSON value.
21974 /// @{
21975
21976 /// @brief access specified array element with bounds checking
21977 /// @sa https://json.nlohmann.me/api/basic_json/at/
21978 reference at(size_type idx)
21979 {
21980 // at only works for arrays
21981 if (JSON_HEDLEY_LIKELY(is_array()))
21982 {
21983 JSON_TRY
21984 {
21985 return set_parent(m_data.m_value.array->at(idx));
21986 }
21987 JSON_CATCH (std::out_of_range&)
21988 {
21989 // create better exception explanation
21990 JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
21991 } // cppcheck-suppress[missingReturn]
21992 }
21993 else
21994 {
21995 JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
21996 }
21997 }
21998
21999 /// @brief access specified array element with bounds checking
22000 /// @sa https://json.nlohmann.me/api/basic_json/at/
22001 const_reference at(size_type idx) const
22002 {
22003 // at only works for arrays
22004 if (JSON_HEDLEY_LIKELY(is_array()))
22005 {
22006 JSON_TRY
22007 {
22008 return m_data.m_value.array->at(idx);
22009 }
22010 JSON_CATCH (std::out_of_range&)
22011 {
22012 // create better exception explanation
22013 JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
22014 } // cppcheck-suppress[missingReturn]
22015 }
22016 else
22017 {
22018 JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
22019 }
22020 }
22021
22022 /// @brief access specified object element with bounds checking
22023 /// @sa https://json.nlohmann.me/api/basic_json/at/
22024 reference at(const typename object_t::key_type& key)
22025 {
22026 // at only works for objects
22027 if (JSON_HEDLEY_UNLIKELY(!is_object()))
22028 {
22029 JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
22030 }
22031
22032 auto it = m_data.m_value.object->find(key);
22033 if (it == m_data.m_value.object->end())
22034 {
22035 JSON_THROW(out_of_range::create(403, detail::concat("key '", key, "' not found"), this));
22036 }
22037 return set_parent(it->second);
22038 }
22039
22040 /// @brief access specified object element with bounds checking
22041 /// @sa https://json.nlohmann.me/api/basic_json/at/
22042 template<class KeyType, detail::enable_if_t<
22043 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
22044 reference at(KeyType && key)
22045 {
22046 // at only works for objects
22047 if (JSON_HEDLEY_UNLIKELY(!is_object()))
22048 {
22049 JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
22050 }
22051
22052 auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
22053 if (it == m_data.m_value.object->end())
22054 {
22055 JSON_THROW(out_of_range::create(403, detail::concat("key '", string_t(std::forward<KeyType>(key)), "' not found"), this));
22056 }
22057 return set_parent(it->second);
22058 }
22059
22060 /// @brief access specified object element with bounds checking
22061 /// @sa https://json.nlohmann.me/api/basic_json/at/
22062 const_reference at(const typename object_t::key_type& key) const
22063 {
22064 // at only works for objects
22065 if (JSON_HEDLEY_UNLIKELY(!is_object()))
22066 {
22067 JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
22068 }
22069
22070 auto it = m_data.m_value.object->find(key);
22071 if (it == m_data.m_value.object->end())
22072 {
22073 JSON_THROW(out_of_range::create(403, detail::concat("key '", key, "' not found"), this));
22074 }
22075 return it->second;
22076 }
22077
22078 /// @brief access specified object element with bounds checking
22079 /// @sa https://json.nlohmann.me/api/basic_json/at/
22080 template<class KeyType, detail::enable_if_t<
22081 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
22082 const_reference at(KeyType && key) const
22083 {
22084 // at only works for objects
22085 if (JSON_HEDLEY_UNLIKELY(!is_object()))
22086 {
22087 JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
22088 }
22089
22090 auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
22091 if (it == m_data.m_value.object->end())
22092 {
22093 JSON_THROW(out_of_range::create(403, detail::concat("key '", string_t(std::forward<KeyType>(key)), "' not found"), this));
22094 }
22095 return it->second;
22096 }
22097
22098 /// @brief access specified array element
22099 /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
22100 reference operator[](size_type idx)
22101 {
22102 // implicitly convert null value to an empty array
22103 if (is_null())
22104 {
22105 m_data.m_type = value_t::array;
22106 m_data.m_value.array = create<array_t>();
22107 assert_invariant();
22108 }
22109
22110 // operator[] only works for arrays
22111 if (JSON_HEDLEY_LIKELY(is_array()))
22112 {
22113 // fill up array with null values if given idx is outside range
22114 if (idx >= m_data.m_value.array->size())
22115 {
22116 #if JSON_DIAGNOSTICS
22117 // remember array size & capacity before resizing
22118 const auto old_size = m_data.m_value.array->size();
22119 const auto old_capacity = m_data.m_value.array->capacity();
22120 #endif
22121 m_data.m_value.array->resize(idx + 1);
22122
22123 #if JSON_DIAGNOSTICS
22124 if (JSON_HEDLEY_UNLIKELY(m_data.m_value.array->capacity() != old_capacity))
22125 {
22126 // capacity has changed: update all parents
22127 set_parents();
22128 }
22129 else
22130 {
22131 // set parent for values added above
22132 set_parents(begin() + static_cast<typename iterator::difference_type>(old_size), static_cast<typename iterator::difference_type>(idx + 1 - old_size));
22133 }
22134 #endif
22135 assert_invariant();
22136 }
22137
22138 return m_data.m_value.array->operator[](idx);
22139 }
22140
22141 JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a numeric argument with ", type_name()), this));
22142 }
22143
22144 /// @brief access specified array element
22145 /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
22146 const_reference operator[](size_type idx) const
22147 {
22148 // const operator[] only works for arrays
22149 if (JSON_HEDLEY_LIKELY(is_array()))
22150 {
22151 return m_data.m_value.array->operator[](idx);
22152 }
22153
22154 JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a numeric argument with ", type_name()), this));
22155 }
22156
22157 /// @brief access specified object element
22158 /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
22159 reference operator[](typename object_t::key_type key) // NOLINT(performance-unnecessary-value-param)
22160 {
22161 // implicitly convert null value to an empty object
22162 if (is_null())
22163 {
22164 m_data.m_type = value_t::object;
22165 m_data.m_value.object = create<object_t>();
22166 assert_invariant();
22167 }
22168
22169 // operator[] only works for objects
22170 if (JSON_HEDLEY_LIKELY(is_object()))
22171 {
22172 auto result = m_data.m_value.object->emplace(std::move(key), nullptr);
22173 return set_parent(result.first->second);
22174 }
22175
22176 JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
22177 }
22178
22179 /// @brief access specified object element
22180 /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
22181 const_reference operator[](const typename object_t::key_type& key) const
22182 {
22183 // const operator[] only works for objects
22184 if (JSON_HEDLEY_LIKELY(is_object()))
22185 {
22186 auto it = m_data.m_value.object->find(key);
22187 JSON_ASSERT(it != m_data.m_value.object->end());
22188 return it->second;
22189 }
22190
22191 JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
22192 }
22193
22194 // these two functions resolve a (const) char * ambiguity affecting Clang and MSVC
22195 // (they seemingly cannot be constrained to resolve the ambiguity)
22196 template<typename T>
22197 reference operator[](T* key)
22198 {
22199 return operator[](typename object_t::key_type(key));
22200 }
22201
22202 template<typename T>
22203 const_reference operator[](T* key) const
22204 {
22205 return operator[](typename object_t::key_type(key));
22206 }
22207
22208 /// @brief access specified object element
22209 /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
22210 template<class KeyType, detail::enable_if_t<
22211 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int > = 0 >
22212 reference operator[](KeyType && key)
22213 {
22214 // implicitly convert null value to an empty object
22215 if (is_null())
22216 {
22217 m_data.m_type = value_t::object;
22218 m_data.m_value.object = create<object_t>();
22219 assert_invariant();
22220 }
22221
22222 // operator[] only works for objects
22223 if (JSON_HEDLEY_LIKELY(is_object()))
22224 {
22225 auto result = m_data.m_value.object->emplace(std::forward<KeyType>(key), nullptr);
22226 return set_parent(result.first->second);
22227 }
22228
22229 JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
22230 }
22231
22232 /// @brief access specified object element
22233 /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
22234 template<class KeyType, detail::enable_if_t<
22235 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int > = 0 >
22236 const_reference operator[](KeyType && key) const
22237 {
22238 // const operator[] only works for objects
22239 if (JSON_HEDLEY_LIKELY(is_object()))
22240 {
22241 auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
22242 JSON_ASSERT(it != m_data.m_value.object->end());
22243 return it->second;
22244 }
22245
22246 JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
22247 }
22248
22249 private:
22250 template<typename KeyType>
22251 using is_comparable_with_object_key = detail::is_comparable <
22252 object_comparator_t, const typename object_t::key_type&, KeyType >;
22253
22254 template<typename ValueType>
22255 using value_return_type = std::conditional <
22256 detail::is_c_string_uncvref<ValueType>::value,
22257 string_t, typename std::decay<ValueType>::type >;
22258
22259 public:
22260 /// @brief access specified object element with default value
22261 /// @sa https://json.nlohmann.me/api/basic_json/value/
22262 template < class ValueType, detail::enable_if_t <
22263 !detail::is_transparent<object_comparator_t>::value
22264 && detail::is_getable<basic_json_t, ValueType>::value
22265 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22266 ValueType value(const typename object_t::key_type& key, const ValueType& default_value) const
22267 {
22268 // value only works for objects
22269 if (JSON_HEDLEY_LIKELY(is_object()))
22270 {
22271 // if key is found, return value and given default value otherwise
22272 const auto it = find(key);
22273 if (it != end())
22274 {
22275 return it->template get<ValueType>();
22276 }
22277
22278 return default_value;
22279 }
22280
22281 JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
22282 }
22283
22284 /// @brief access specified object element with default value
22285 /// @sa https://json.nlohmann.me/api/basic_json/value/
22286 template < class ValueType, class ReturnType = typename value_return_type<ValueType>::type,
22287 detail::enable_if_t <
22288 !detail::is_transparent<object_comparator_t>::value
22289 && detail::is_getable<basic_json_t, ReturnType>::value
22290 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22291 ReturnType value(const typename object_t::key_type& key, ValueType && default_value) const
22292 {
22293 // value only works for objects
22294 if (JSON_HEDLEY_LIKELY(is_object()))
22295 {
22296 // if key is found, return value and given default value otherwise
22297 const auto it = find(key);
22298 if (it != end())
22299 {
22300 return it->template get<ReturnType>();
22301 }
22302
22303 return std::forward<ValueType>(default_value);
22304 }
22305
22306 JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
22307 }
22308
22309 /// @brief access specified object element with default value
22310 /// @sa https://json.nlohmann.me/api/basic_json/value/
22311 template < class ValueType, class KeyType, detail::enable_if_t <
22312 detail::is_transparent<object_comparator_t>::value
22313 && !detail::is_json_pointer<KeyType>::value
22314 && is_comparable_with_object_key<KeyType>::value
22315 && detail::is_getable<basic_json_t, ValueType>::value
22316 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22317 ValueType value(KeyType && key, const ValueType& default_value) const
22318 {
22319 // value only works for objects
22320 if (JSON_HEDLEY_LIKELY(is_object()))
22321 {
22322 // if key is found, return value and given default value otherwise
22323 const auto it = find(std::forward<KeyType>(key));
22324 if (it != end())
22325 {
22326 return it->template get<ValueType>();
22327 }
22328
22329 return default_value;
22330 }
22331
22332 JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
22333 }
22334
22335 /// @brief access specified object element via JSON Pointer with default value
22336 /// @sa https://json.nlohmann.me/api/basic_json/value/
22337 template < class ValueType, class KeyType, class ReturnType = typename value_return_type<ValueType>::type,
22338 detail::enable_if_t <
22339 detail::is_transparent<object_comparator_t>::value
22340 && !detail::is_json_pointer<KeyType>::value
22341 && is_comparable_with_object_key<KeyType>::value
22342 && detail::is_getable<basic_json_t, ReturnType>::value
22343 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22344 ReturnType value(KeyType && key, ValueType && default_value) const
22345 {
22346 // value only works for objects
22347 if (JSON_HEDLEY_LIKELY(is_object()))
22348 {
22349 // if key is found, return value and given default value otherwise
22350 const auto it = find(std::forward<KeyType>(key));
22351 if (it != end())
22352 {
22353 return it->template get<ReturnType>();
22354 }
22355
22356 return std::forward<ValueType>(default_value);
22357 }
22358
22359 JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
22360 }
22361
22362 /// @brief access specified object element via JSON Pointer with default value
22363 /// @sa https://json.nlohmann.me/api/basic_json/value/
22364 template < class ValueType, detail::enable_if_t <
22365 detail::is_getable<basic_json_t, ValueType>::value
22366 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22367 ValueType value(const json_pointer& ptr, const ValueType& default_value) const
22368 {
22369 // value only works for objects
22370 if (JSON_HEDLEY_LIKELY(is_object()))
22371 {
22372 // if pointer resolves a value, return it or use default value
22373 JSON_TRY
22374 {
22375 return ptr.get_checked(this).template get<ValueType>();
22376 }
22377 JSON_INTERNAL_CATCH (out_of_range&)
22378 {
22379 return default_value;
22380 }
22381 }
22382
22383 JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
22384 }
22385
22386 /// @brief access specified object element via JSON Pointer with default value
22387 /// @sa https://json.nlohmann.me/api/basic_json/value/
22388 template < class ValueType, class ReturnType = typename value_return_type<ValueType>::type,
22389 detail::enable_if_t <
22390 detail::is_getable<basic_json_t, ReturnType>::value
22391 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22392 ReturnType value(const json_pointer& ptr, ValueType && default_value) const
22393 {
22394 // value only works for objects
22395 if (JSON_HEDLEY_LIKELY(is_object()))
22396 {
22397 // if pointer resolves a value, return it or use default value
22398 JSON_TRY
22399 {
22400 return ptr.get_checked(this).template get<ReturnType>();
22401 }
22402 JSON_INTERNAL_CATCH (out_of_range&)
22403 {
22404 return std::forward<ValueType>(default_value);
22405 }
22406 }
22407
22408 JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
22409 }
22410
22411 template < class ValueType, class BasicJsonType, detail::enable_if_t <
22412 detail::is_basic_json<BasicJsonType>::value
22413 && detail::is_getable<basic_json_t, ValueType>::value
22414 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22415 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
22416 ValueType value(const ::nlohmann::json_pointer<BasicJsonType>& ptr, const ValueType& default_value) const
22417 {
22418 return value(ptr.convert(), default_value);
22419 }
22420
22421 template < class ValueType, class BasicJsonType, class ReturnType = typename value_return_type<ValueType>::type,
22422 detail::enable_if_t <
22423 detail::is_basic_json<BasicJsonType>::value
22424 && detail::is_getable<basic_json_t, ReturnType>::value
22425 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22426 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
22427 ReturnType value(const ::nlohmann::json_pointer<BasicJsonType>& ptr, ValueType && default_value) const
22428 {
22429 return value(ptr.convert(), std::forward<ValueType>(default_value));
22430 }
22431
22432 /// @brief access the first element
22433 /// @sa https://json.nlohmann.me/api/basic_json/front/
22434 reference front()
22435 {
22436 return *begin();
22437 }
22438
22439 /// @brief access the first element
22440 /// @sa https://json.nlohmann.me/api/basic_json/front/
22441 const_reference front() const
22442 {
22443 return *cbegin();
22444 }
22445
22446 /// @brief access the last element
22447 /// @sa https://json.nlohmann.me/api/basic_json/back/
22448 reference back()
22449 {
22450 auto tmp = end();
22451 --tmp;
22452 return *tmp;
22453 }
22454
22455 /// @brief access the last element
22456 /// @sa https://json.nlohmann.me/api/basic_json/back/
22457 const_reference back() const
22458 {
22459 auto tmp = cend();
22460 --tmp;
22461 return *tmp;
22462 }
22463
22464 /// @brief remove element given an iterator
22465 /// @sa https://json.nlohmann.me/api/basic_json/erase/
22466 template < class IteratorType, detail::enable_if_t <
22467 std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
22468 std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int > = 0 >
22469 IteratorType erase(IteratorType pos) // NOLINT(performance-unnecessary-value-param)
22470 {
22471 // make sure iterator fits the current value
22472 if (JSON_HEDLEY_UNLIKELY(this != pos.m_object))
22473 {
22474 JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
22475 }
22476
22477 IteratorType result = end();
22478
22479 switch (m_data.m_type)
22480 {
22481 case value_t::boolean:
22482 case value_t::number_float:
22483 case value_t::number_integer:
22484 case value_t::number_unsigned:
22485 case value_t::string:
22486 case value_t::binary:
22487 {
22488 if (JSON_HEDLEY_UNLIKELY(!pos.m_it.primitive_iterator.is_begin()))
22489 {
22490 JSON_THROW(invalid_iterator::create(205, "iterator out of range", this));
22491 }
22492
22493 if (is_string())
22494 {
22495 AllocatorType<string_t> alloc;
22496 std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.string);
22497 std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.string, 1);
22498 m_data.m_value.string = nullptr;
22499 }
22500 else if (is_binary())
22501 {
22502 AllocatorType<binary_t> alloc;
22503 std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.binary);
22504 std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.binary, 1);
22505 m_data.m_value.binary = nullptr;
22506 }
22507
22508 m_data.m_type = value_t::null;
22509 assert_invariant();
22510 break;
22511 }
22512
22513 case value_t::object:
22514 {
22515 result.m_it.object_iterator = m_data.m_value.object->erase(pos.m_it.object_iterator);
22516 break;
22517 }
22518
22519 case value_t::array:
22520 {
22521 result.m_it.array_iterator = m_data.m_value.array->erase(pos.m_it.array_iterator);
22522 break;
22523 }
22524
22525 case value_t::null:
22526 case value_t::discarded:
22527 default:
22528 JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
22529 }
22530
22531 return result;
22532 }
22533
22534 /// @brief remove elements given an iterator range
22535 /// @sa https://json.nlohmann.me/api/basic_json/erase/
22536 template < class IteratorType, detail::enable_if_t <
22537 std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
22538 std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int > = 0 >
22539 IteratorType erase(IteratorType first, IteratorType last) // NOLINT(performance-unnecessary-value-param)
22540 {
22541 // make sure iterator fits the current value
22542 if (JSON_HEDLEY_UNLIKELY(this != first.m_object || this != last.m_object))
22543 {
22544 JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value", this));
22545 }
22546
22547 IteratorType result = end();
22548
22549 switch (m_data.m_type)
22550 {
22551 case value_t::boolean:
22552 case value_t::number_float:
22553 case value_t::number_integer:
22554 case value_t::number_unsigned:
22555 case value_t::string:
22556 case value_t::binary:
22557 {
22558 if (JSON_HEDLEY_LIKELY(!first.m_it.primitive_iterator.is_begin()
22559 || !last.m_it.primitive_iterator.is_end()))
22560 {
22561 JSON_THROW(invalid_iterator::create(204, "iterators out of range", this));
22562 }
22563
22564 if (is_string())
22565 {
22566 AllocatorType<string_t> alloc;
22567 std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.string);
22568 std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.string, 1);
22569 m_data.m_value.string = nullptr;
22570 }
22571 else if (is_binary())
22572 {
22573 AllocatorType<binary_t> alloc;
22574 std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.binary);
22575 std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.binary, 1);
22576 m_data.m_value.binary = nullptr;
22577 }
22578
22579 m_data.m_type = value_t::null;
22580 assert_invariant();
22581 break;
22582 }
22583
22584 case value_t::object:
22585 {
22586 result.m_it.object_iterator = m_data.m_value.object->erase(first.m_it.object_iterator,
22587 last.m_it.object_iterator);
22588 break;
22589 }
22590
22591 case value_t::array:
22592 {
22593 result.m_it.array_iterator = m_data.m_value.array->erase(first.m_it.array_iterator,
22594 last.m_it.array_iterator);
22595 break;
22596 }
22597
22598 case value_t::null:
22599 case value_t::discarded:
22600 default:
22601 JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
22602 }
22603
22604 return result;
22605 }
22606
22607 private:
22608 template < typename KeyType, detail::enable_if_t <
22609 detail::has_erase_with_key_type<basic_json_t, KeyType>::value, int > = 0 >
22610 size_type erase_internal(KeyType && key)
22611 {
22612 // this erase only works for objects
22613 if (JSON_HEDLEY_UNLIKELY(!is_object()))
22614 {
22615 JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
22616 }
22617
22618 return m_data.m_value.object->erase(std::forward<KeyType>(key));
22619 }
22620
22621 template < typename KeyType, detail::enable_if_t <
22622 !detail::has_erase_with_key_type<basic_json_t, KeyType>::value, int > = 0 >
22623 size_type erase_internal(KeyType && key)
22624 {
22625 // this erase only works for objects
22626 if (JSON_HEDLEY_UNLIKELY(!is_object()))
22627 {
22628 JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
22629 }
22630
22631 const auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
22632 if (it != m_data.m_value.object->end())
22633 {
22634 m_data.m_value.object->erase(it);
22635 return 1;
22636 }
22637 return 0;
22638 }
22639
22640 public:
22641
22642 /// @brief remove element from a JSON object given a key
22643 /// @sa https://json.nlohmann.me/api/basic_json/erase/
22644 size_type erase(const typename object_t::key_type& key)
22645 {
22646 // the indirection via erase_internal() is added to avoid making this
22647 // function a template and thus de-rank it during overload resolution
22648 return erase_internal(key);
22649 }
22650
22651 /// @brief remove element from a JSON object given a key
22652 /// @sa https://json.nlohmann.me/api/basic_json/erase/
22653 template<class KeyType, detail::enable_if_t<
22654 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
22655 size_type erase(KeyType && key)
22656 {
22657 return erase_internal(std::forward<KeyType>(key));
22658 }
22659
22660 /// @brief remove element from a JSON array given an index
22661 /// @sa https://json.nlohmann.me/api/basic_json/erase/
22662 void erase(const size_type idx)
22663 {
22664 // this erase only works for arrays
22665 if (JSON_HEDLEY_LIKELY(is_array()))
22666 {
22667 if (JSON_HEDLEY_UNLIKELY(idx >= size()))
22668 {
22669 JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
22670 }
22671
22672 m_data.m_value.array->erase(m_data.m_value.array->begin() + static_cast<difference_type>(idx));
22673 }
22674 else
22675 {
22676 JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
22677 }
22678 }
22679
22680 /// @}
22681
22682 ////////////
22683 // lookup //
22684 ////////////
22685
22686 /// @name lookup
22687 /// @{
22688
22689 /// @brief find an element in a JSON object
22690 /// @sa https://json.nlohmann.me/api/basic_json/find/
22691 iterator find(const typename object_t::key_type& key)
22692 {
22693 auto result = end();
22694
22695 if (is_object())
22696 {
22697 result.m_it.object_iterator = m_data.m_value.object->find(key);
22698 }
22699
22700 return result;
22701 }
22702
22703 /// @brief find an element in a JSON object
22704 /// @sa https://json.nlohmann.me/api/basic_json/find/
22705 const_iterator find(const typename object_t::key_type& key) const
22706 {
22707 auto result = cend();
22708
22709 if (is_object())
22710 {
22711 result.m_it.object_iterator = m_data.m_value.object->find(key);
22712 }
22713
22714 return result;
22715 }
22716
22717 /// @brief find an element in a JSON object
22718 /// @sa https://json.nlohmann.me/api/basic_json/find/
22719 template<class KeyType, detail::enable_if_t<
22720 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
22721 iterator find(KeyType && key)
22722 {
22723 auto result = end();
22724
22725 if (is_object())
22726 {
22727 result.m_it.object_iterator = m_data.m_value.object->find(std::forward<KeyType>(key));
22728 }
22729
22730 return result;
22731 }
22732
22733 /// @brief find an element in a JSON object
22734 /// @sa https://json.nlohmann.me/api/basic_json/find/
22735 template<class KeyType, detail::enable_if_t<
22736 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
22737 const_iterator find(KeyType && key) const
22738 {
22739 auto result = cend();
22740
22741 if (is_object())
22742 {
22743 result.m_it.object_iterator = m_data.m_value.object->find(std::forward<KeyType>(key));
22744 }
22745
22746 return result;
22747 }
22748
22749 /// @brief returns the number of occurrences of a key in a JSON object
22750 /// @sa https://json.nlohmann.me/api/basic_json/count/
22751 size_type count(const typename object_t::key_type& key) const
22752 {
22753 // return 0 for all nonobject types
22754 return is_object() ? m_data.m_value.object->count(key) : 0;
22755 }
22756
22757 /// @brief returns the number of occurrences of a key in a JSON object
22758 /// @sa https://json.nlohmann.me/api/basic_json/count/
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 size_type count(KeyType && key) const
22762 {
22763 // return 0 for all nonobject types
22764 return is_object() ? m_data.m_value.object->count(std::forward<KeyType>(key)) : 0;
22765 }
22766
22767 /// @brief check the existence of an element in a JSON object
22768 /// @sa https://json.nlohmann.me/api/basic_json/contains/
22769 bool contains(const typename object_t::key_type& key) const
22770 {
22771 return is_object() && m_data.m_value.object->find(key) != m_data.m_value.object->end();
22772 }
22773
22774 /// @brief check the existence of an element in a JSON object
22775 /// @sa https://json.nlohmann.me/api/basic_json/contains/
22776 template<class KeyType, detail::enable_if_t<
22777 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
22778 bool contains(KeyType && key) const
22779 {
22780 return is_object() && m_data.m_value.object->find(std::forward<KeyType>(key)) != m_data.m_value.object->end();
22781 }
22782
22783 /// @brief check the existence of an element in a JSON object given a JSON pointer
22784 /// @sa https://json.nlohmann.me/api/basic_json/contains/
22785 bool contains(const json_pointer& ptr) const
22786 {
22787 return ptr.contains(this);
22788 }
22789
22790 template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
22791 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
22792 bool contains(const typename ::nlohmann::json_pointer<BasicJsonType>& ptr) const
22793 {
22794 return ptr.contains(this);
22795 }
22796
22797 /// @}
22798
22799 ///////////////
22800 // iterators //
22801 ///////////////
22802
22803 /// @name iterators
22804 /// @{
22805
22806 /// @brief returns an iterator to the first element
22807 /// @sa https://json.nlohmann.me/api/basic_json/begin/
22808 iterator begin() noexcept
22809 {
22810 iterator result(this);
22811 result.set_begin();
22812 return result;
22813 }
22814
22815 /// @brief returns an iterator to the first element
22816 /// @sa https://json.nlohmann.me/api/basic_json/begin/
22817 const_iterator begin() const noexcept
22818 {
22819 return cbegin();
22820 }
22821
22822 /// @brief returns a const iterator to the first element
22823 /// @sa https://json.nlohmann.me/api/basic_json/cbegin/
22824 const_iterator cbegin() const noexcept
22825 {
22826 const_iterator result(this);
22827 result.set_begin();
22828 return result;
22829 }
22830
22831 /// @brief returns an iterator to one past the last element
22832 /// @sa https://json.nlohmann.me/api/basic_json/end/
22833 iterator end() noexcept
22834 {
22835 iterator result(this);
22836 result.set_end();
22837 return result;
22838 }
22839
22840 /// @brief returns an iterator to one past the last element
22841 /// @sa https://json.nlohmann.me/api/basic_json/end/
22842 const_iterator end() const noexcept
22843 {
22844 return cend();
22845 }
22846
22847 /// @brief returns an iterator to one past the last element
22848 /// @sa https://json.nlohmann.me/api/basic_json/cend/
22849 const_iterator cend() const noexcept
22850 {
22851 const_iterator result(this);
22852 result.set_end();
22853 return result;
22854 }
22855
22856 /// @brief returns an iterator to the reverse-beginning
22857 /// @sa https://json.nlohmann.me/api/basic_json/rbegin/
22858 reverse_iterator rbegin() noexcept
22859 {
22860 return reverse_iterator(end());
22861 }
22862
22863 /// @brief returns an iterator to the reverse-beginning
22864 /// @sa https://json.nlohmann.me/api/basic_json/rbegin/
22865 const_reverse_iterator rbegin() const noexcept
22866 {
22867 return crbegin();
22868 }
22869
22870 /// @brief returns an iterator to the reverse-end
22871 /// @sa https://json.nlohmann.me/api/basic_json/rend/
22872 reverse_iterator rend() noexcept
22873 {
22874 return reverse_iterator(begin());
22875 }
22876
22877 /// @brief returns an iterator to the reverse-end
22878 /// @sa https://json.nlohmann.me/api/basic_json/rend/
22879 const_reverse_iterator rend() const noexcept
22880 {
22881 return crend();
22882 }
22883
22884 /// @brief returns a const reverse iterator to the last element
22885 /// @sa https://json.nlohmann.me/api/basic_json/crbegin/
22886 const_reverse_iterator crbegin() const noexcept
22887 {
22888 return const_reverse_iterator(cend());
22889 }
22890
22891 /// @brief returns a const reverse iterator to one before the first
22892 /// @sa https://json.nlohmann.me/api/basic_json/crend/
22893 const_reverse_iterator crend() const noexcept
22894 {
22895 return const_reverse_iterator(cbegin());
22896 }
22897
22898 public:
22899 /// @brief wrapper to access iterator member functions in range-based for
22900 /// @sa https://json.nlohmann.me/api/basic_json/items/
22901 /// @deprecated This function is deprecated since 3.1.0 and will be removed in
22902 /// version 4.0.0 of the library. Please use @ref items() instead;
22903 /// that is, replace `json::iterator_wrapper(j)` with `j.items()`.
22904 JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items())
22905 static iteration_proxy<iterator> iterator_wrapper(reference ref) noexcept
22906 {
22907 return ref.items();
22908 }
22909
22910 /// @brief wrapper to access iterator member functions in range-based for
22911 /// @sa https://json.nlohmann.me/api/basic_json/items/
22912 /// @deprecated This function is deprecated since 3.1.0 and will be removed in
22913 /// version 4.0.0 of the library. Please use @ref items() instead;
22914 /// that is, replace `json::iterator_wrapper(j)` with `j.items()`.
22915 JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items())
22916 static iteration_proxy<const_iterator> iterator_wrapper(const_reference ref) noexcept
22917 {
22918 return ref.items();
22919 }
22920
22921 /// @brief helper to access iterator member functions in range-based for
22922 /// @sa https://json.nlohmann.me/api/basic_json/items/
22923 iteration_proxy<iterator> items() noexcept
22924 {
22925 return iteration_proxy<iterator>(*this);
22926 }
22927
22928 /// @brief helper to access iterator member functions in range-based for
22929 /// @sa https://json.nlohmann.me/api/basic_json/items/
22930 iteration_proxy<const_iterator> items() const noexcept
22931 {
22932 return iteration_proxy<const_iterator>(*this);
22933 }
22934
22935 /// @}
22936
22937 //////////////
22938 // capacity //
22939 //////////////
22940
22941 /// @name capacity
22942 /// @{
22943
22944 /// @brief checks whether the container is empty.
22945 /// @sa https://json.nlohmann.me/api/basic_json/empty/
22946 bool empty() const noexcept
22947 {
22948 switch (m_data.m_type)
22949 {
22950 case value_t::null:
22951 {
22952 // null values are empty
22953 return true;
22954 }
22955
22956 case value_t::array:
22957 {
22958 // delegate call to array_t::empty()
22959 return m_data.m_value.array->empty();
22960 }
22961
22962 case value_t::object:
22963 {
22964 // delegate call to object_t::empty()
22965 return m_data.m_value.object->empty();
22966 }
22967
22968 case value_t::string:
22969 case value_t::boolean:
22970 case value_t::number_integer:
22971 case value_t::number_unsigned:
22972 case value_t::number_float:
22973 case value_t::binary:
22974 case value_t::discarded:
22975 default:
22976 {
22977 // all other types are nonempty
22978 return false;
22979 }
22980 }
22981 }
22982
22983 /// @brief returns the number of elements
22984 /// @sa https://json.nlohmann.me/api/basic_json/size/
22985 size_type size() const noexcept
22986 {
22987 switch (m_data.m_type)
22988 {
22989 case value_t::null:
22990 {
22991 // null values are empty
22992 return 0;
22993 }
22994
22995 case value_t::array:
22996 {
22997 // delegate call to array_t::size()
22998 return m_data.m_value.array->size();
22999 }
23000
23001 case value_t::object:
23002 {
23003 // delegate call to object_t::size()
23004 return m_data.m_value.object->size();
23005 }
23006
23007 case value_t::string:
23008 case value_t::boolean:
23009 case value_t::number_integer:
23010 case value_t::number_unsigned:
23011 case value_t::number_float:
23012 case value_t::binary:
23013 case value_t::discarded:
23014 default:
23015 {
23016 // all other types have size 1
23017 return 1;
23018 }
23019 }
23020 }
23021
23022 /// @brief returns the maximum possible number of elements
23023 /// @sa https://json.nlohmann.me/api/basic_json/max_size/
23024 size_type max_size() const noexcept
23025 {
23026 switch (m_data.m_type)
23027 {
23028 case value_t::array:
23029 {
23030 // delegate call to array_t::max_size()
23031 return m_data.m_value.array->max_size();
23032 }
23033
23034 case value_t::object:
23035 {
23036 // delegate call to object_t::max_size()
23037 return m_data.m_value.object->max_size();
23038 }
23039
23040 case value_t::null:
23041 case value_t::string:
23042 case value_t::boolean:
23043 case value_t::number_integer:
23044 case value_t::number_unsigned:
23045 case value_t::number_float:
23046 case value_t::binary:
23047 case value_t::discarded:
23048 default:
23049 {
23050 // all other types have max_size() == size()
23051 return size();
23052 }
23053 }
23054 }
23055
23056 /// @}
23057
23058 ///////////////
23059 // modifiers //
23060 ///////////////
23061
23062 /// @name modifiers
23063 /// @{
23064
23065 /// @brief clears the contents
23066 /// @sa https://json.nlohmann.me/api/basic_json/clear/
23067 void clear() noexcept
23068 {
23069 switch (m_data.m_type)
23070 {
23071 case value_t::number_integer:
23072 {
23073 m_data.m_value.number_integer = 0;
23074 break;
23075 }
23076
23077 case value_t::number_unsigned:
23078 {
23079 m_data.m_value.number_unsigned = 0;
23080 break;
23081 }
23082
23083 case value_t::number_float:
23084 {
23085 m_data.m_value.number_float = 0.0;
23086 break;
23087 }
23088
23089 case value_t::boolean:
23090 {
23091 m_data.m_value.boolean = false;
23092 break;
23093 }
23094
23095 case value_t::string:
23096 {
23097 m_data.m_value.string->clear();
23098 break;
23099 }
23100
23101 case value_t::binary:
23102 {
23103 m_data.m_value.binary->clear();
23104 break;
23105 }
23106
23107 case value_t::array:
23108 {
23109 m_data.m_value.array->clear();
23110 break;
23111 }
23112
23113 case value_t::object:
23114 {
23115 m_data.m_value.object->clear();
23116 break;
23117 }
23118
23119 case value_t::null:
23120 case value_t::discarded:
23121 default:
23122 break;
23123 }
23124 }
23125
23126 /// @brief add an object to an array
23127 /// @sa https://json.nlohmann.me/api/basic_json/push_back/
23128 void push_back(basic_json&& val)
23129 {
23130 // push_back only works for null objects or arrays
23131 if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
23132 {
23133 JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
23134 }
23135
23136 // transform null object into an array
23137 if (is_null())
23138 {
23139 m_data.m_type = value_t::array;
23140 m_data.m_value = value_t::array;
23141 assert_invariant();
23142 }
23143
23144 // add element to array (move semantics)
23145 const auto old_capacity = m_data.m_value.array->capacity();
23146 m_data.m_value.array->push_back(std::move(val));
23147 set_parent(m_data.m_value.array->back(), old_capacity);
23148 // if val is moved from, basic_json move constructor marks it null, so we do not call the destructor
23149 }
23150
23151 /// @brief add an object to an array
23152 /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
23153 reference operator+=(basic_json&& val)
23154 {
23155 push_back(std::move(val));
23156 return *this;
23157 }
23158
23159 /// @brief add an object to an array
23160 /// @sa https://json.nlohmann.me/api/basic_json/push_back/
23161 void push_back(const basic_json& val)
23162 {
23163 // push_back only works for null objects or arrays
23164 if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
23165 {
23166 JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
23167 }
23168
23169 // transform null object into an array
23170 if (is_null())
23171 {
23172 m_data.m_type = value_t::array;
23173 m_data.m_value = value_t::array;
23174 assert_invariant();
23175 }
23176
23177 // add element to array
23178 const auto old_capacity = m_data.m_value.array->capacity();
23179 m_data.m_value.array->push_back(val);
23180 set_parent(m_data.m_value.array->back(), old_capacity);
23181 }
23182
23183 /// @brief add an object to an array
23184 /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
23185 reference operator+=(const basic_json& val)
23186 {
23187 push_back(val);
23188 return *this;
23189 }
23190
23191 /// @brief add an object to an object
23192 /// @sa https://json.nlohmann.me/api/basic_json/push_back/
23193 void push_back(const typename object_t::value_type& val)
23194 {
23195 // push_back only works for null objects or objects
23196 if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
23197 {
23198 JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
23199 }
23200
23201 // transform null object into an object
23202 if (is_null())
23203 {
23204 m_data.m_type = value_t::object;
23205 m_data.m_value = value_t::object;
23206 assert_invariant();
23207 }
23208
23209 // add element to object
23210 auto res = m_data.m_value.object->insert(val);
23211 set_parent(res.first->second);
23212 }
23213
23214 /// @brief add an object to an object
23215 /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
23216 reference operator+=(const typename object_t::value_type& val)
23217 {
23218 push_back(val);
23219 return *this;
23220 }
23221
23222 /// @brief add an object to an object
23223 /// @sa https://json.nlohmann.me/api/basic_json/push_back/
23224 void push_back(initializer_list_t init)
23225 {
23226 if (is_object() && init.size() == 2 && (*init.begin())->is_string())
23227 {
23228 basic_json&& key = init.begin()->moved_or_copied();
23229 push_back(typename object_t::value_type(
23230 std::move(key.get_ref<string_t&>()), (init.begin() + 1)->moved_or_copied()));
23231 }
23232 else
23233 {
23234 push_back(basic_json(init));
23235 }
23236 }
23237
23238 /// @brief add an object to an object
23239 /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
23240 reference operator+=(initializer_list_t init)
23241 {
23242 push_back(init);
23243 return *this;
23244 }
23245
23246 /// @brief add an object to an array
23247 /// @sa https://json.nlohmann.me/api/basic_json/emplace_back/
23248 template<class... Args>
23249 reference emplace_back(Args&& ... args)
23250 {
23251 // emplace_back only works for null objects or arrays
23252 if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
23253 {
23254 JSON_THROW(type_error::create(311, detail::concat("cannot use emplace_back() with ", type_name()), this));
23255 }
23256
23257 // transform null object into an array
23258 if (is_null())
23259 {
23260 m_data.m_type = value_t::array;
23261 m_data.m_value = value_t::array;
23262 assert_invariant();
23263 }
23264
23265 // add element to array (perfect forwarding)
23266 const auto old_capacity = m_data.m_value.array->capacity();
23267 m_data.m_value.array->emplace_back(std::forward<Args>(args)...);
23268 return set_parent(m_data.m_value.array->back(), old_capacity);
23269 }
23270
23271 /// @brief add an object to an object if key does not exist
23272 /// @sa https://json.nlohmann.me/api/basic_json/emplace/
23273 template<class... Args>
23274 std::pair<iterator, bool> emplace(Args&& ... args)
23275 {
23276 // emplace only works for null objects or arrays
23277 if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
23278 {
23279 JSON_THROW(type_error::create(311, detail::concat("cannot use emplace() with ", type_name()), this));
23280 }
23281
23282 // transform null object into an object
23283 if (is_null())
23284 {
23285 m_data.m_type = value_t::object;
23286 m_data.m_value = value_t::object;
23287 assert_invariant();
23288 }
23289
23290 // add element to array (perfect forwarding)
23291 auto res = m_data.m_value.object->emplace(std::forward<Args>(args)...);
23292 set_parent(res.first->second);
23293
23294 // create result iterator and set iterator to the result of emplace
23295 auto it = begin();
23296 it.m_it.object_iterator = res.first;
23297
23298 // return pair of iterator and boolean
23299 return {it, res.second};
23300 }
23301
23302 /// Helper for insertion of an iterator
23303 /// @note: This uses std::distance to support GCC 4.8,
23304 /// see https://github.com/nlohmann/json/pull/1257
23305 template<typename... Args>
23306 iterator insert_iterator(const_iterator pos, Args&& ... args) // NOLINT(performance-unnecessary-value-param)
23307 {
23308 iterator result(this);
23309 JSON_ASSERT(m_data.m_value.array != nullptr);
23310
23311 auto insert_pos = std::distance(m_data.m_value.array->begin(), pos.m_it.array_iterator);
23312 m_data.m_value.array->insert(pos.m_it.array_iterator, std::forward<Args>(args)...);
23313 result.m_it.array_iterator = m_data.m_value.array->begin() + insert_pos;
23314
23315 // This could have been written as:
23316 // result.m_it.array_iterator = m_data.m_value.array->insert(pos.m_it.array_iterator, cnt, val);
23317 // but the return value of insert is missing in GCC 4.8, so it is written this way instead.
23318
23319 set_parents();
23320 return result;
23321 }
23322
23323 /// @brief inserts element into array
23324 /// @sa https://json.nlohmann.me/api/basic_json/insert/
23325 iterator insert(const_iterator pos, const basic_json& val) // NOLINT(performance-unnecessary-value-param)
23326 {
23327 // insert only works for arrays
23328 if (JSON_HEDLEY_LIKELY(is_array()))
23329 {
23330 // check if iterator pos fits to this JSON value
23331 if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
23332 {
23333 JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
23334 }
23335
23336 // insert to array and return iterator
23337 return insert_iterator(pos, val);
23338 }
23339
23340 JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
23341 }
23342
23343 /// @brief inserts element into array
23344 /// @sa https://json.nlohmann.me/api/basic_json/insert/
23345 iterator insert(const_iterator pos, basic_json&& val) // NOLINT(performance-unnecessary-value-param)
23346 {
23347 return insert(pos, val);
23348 }
23349
23350 /// @brief inserts copies of element into array
23351 /// @sa https://json.nlohmann.me/api/basic_json/insert/
23352 iterator insert(const_iterator pos, size_type cnt, const basic_json& val) // NOLINT(performance-unnecessary-value-param)
23353 {
23354 // insert only works for arrays
23355 if (JSON_HEDLEY_LIKELY(is_array()))
23356 {
23357 // check if iterator pos fits to this JSON value
23358 if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
23359 {
23360 JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
23361 }
23362
23363 // insert to array and return iterator
23364 return insert_iterator(pos, cnt, val);
23365 }
23366
23367 JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
23368 }
23369
23370 /// @brief inserts range of elements into array
23371 /// @sa https://json.nlohmann.me/api/basic_json/insert/
23372 iterator insert(const_iterator pos, const_iterator first, const_iterator last) // NOLINT(performance-unnecessary-value-param)
23373 {
23374 // insert only works for arrays
23375 if (JSON_HEDLEY_UNLIKELY(!is_array()))
23376 {
23377 JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
23378 }
23379
23380 // check if iterator pos fits to this JSON value
23381 if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
23382 {
23383 JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
23384 }
23385
23386 // check if range iterators belong to the same JSON object
23387 if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
23388 {
23389 JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
23390 }
23391
23392 if (JSON_HEDLEY_UNLIKELY(first.m_object == this))
23393 {
23394 JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container", this));
23395 }
23396
23397 // insert to array and return iterator
23398 return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator);
23399 }
23400
23401 /// @brief inserts elements from initializer list into array
23402 /// @sa https://json.nlohmann.me/api/basic_json/insert/
23403 iterator insert(const_iterator pos, initializer_list_t ilist) // NOLINT(performance-unnecessary-value-param)
23404 {
23405 // insert only works for arrays
23406 if (JSON_HEDLEY_UNLIKELY(!is_array()))
23407 {
23408 JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
23409 }
23410
23411 // check if iterator pos fits to this JSON value
23412 if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
23413 {
23414 JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
23415 }
23416
23417 // insert to array and return iterator
23418 return insert_iterator(pos, ilist.begin(), ilist.end());
23419 }
23420
23421 /// @brief inserts range of elements into object
23422 /// @sa https://json.nlohmann.me/api/basic_json/insert/
23423 void insert(const_iterator first, const_iterator last) // NOLINT(performance-unnecessary-value-param)
23424 {
23425 // insert only works for objects
23426 if (JSON_HEDLEY_UNLIKELY(!is_object()))
23427 {
23428 JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
23429 }
23430
23431 // check if range iterators belong to the same JSON object
23432 if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
23433 {
23434 JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
23435 }
23436
23437 // passed iterators must belong to objects
23438 if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()))
23439 {
23440 JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", this));
23441 }
23442
23443 m_data.m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator);
23444 set_parents();
23445 }
23446
23447 /// @brief updates a JSON object from another object, overwriting existing keys
23448 /// @sa https://json.nlohmann.me/api/basic_json/update/
23449 void update(const_reference j, bool merge_objects = false)
23450 {
23451 update(j.begin(), j.end(), merge_objects);
23452 }
23453
23454 /// @brief updates a JSON object from another object, overwriting existing keys
23455 /// @sa https://json.nlohmann.me/api/basic_json/update/
23456 void update(const_iterator first, const_iterator last, bool merge_objects = false) // NOLINT(performance-unnecessary-value-param)
23457 {
23458 // implicitly convert null value to an empty object
23459 if (is_null())
23460 {
23461 m_data.m_type = value_t::object;
23462 m_data.m_value.object = create<object_t>();
23463 assert_invariant();
23464 }
23465
23466 if (JSON_HEDLEY_UNLIKELY(!is_object()))
23467 {
23468 JSON_THROW(type_error::create(312, detail::concat("cannot use update() 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(type_error::create(312, detail::concat("cannot use update() with ", first.m_object->type_name()), first.m_object));
23481 }
23482
23483 for (auto it = first; it != last; ++it)
23484 {
23485 if (merge_objects && it.value().is_object())
23486 {
23487 auto it2 = m_data.m_value.object->find(it.key());
23488 if (it2 != m_data.m_value.object->end())
23489 {
23490 it2->second.update(it.value(), true);
23491 continue;
23492 }
23493 }
23494 m_data.m_value.object->operator[](it.key()) = it.value();
23495 #if JSON_DIAGNOSTICS
23496 m_data.m_value.object->operator[](it.key()).m_parent = this;
23497 #endif
23498 }
23499 }
23500
23501 /// @brief exchanges the values
23502 /// @sa https://json.nlohmann.me/api/basic_json/swap/
23503 void swap(reference other) noexcept (
23504 std::is_nothrow_move_constructible<value_t>::value&&
23505 std::is_nothrow_move_assignable<value_t>::value&&
23506 std::is_nothrow_move_constructible<json_value>::value&& // NOLINT(cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
23507 std::is_nothrow_move_assignable<json_value>::value
23508 )
23509 {
23510 std::swap(m_data.m_type, other.m_data.m_type);
23511 std::swap(m_data.m_value, other.m_data.m_value);
23512
23513 set_parents();
23514 other.set_parents();
23515 assert_invariant();
23516 }
23517
23518 /// @brief exchanges the values
23519 /// @sa https://json.nlohmann.me/api/basic_json/swap/
23520 friend void swap(reference left, reference right) noexcept (
23521 std::is_nothrow_move_constructible<value_t>::value&&
23522 std::is_nothrow_move_assignable<value_t>::value&&
23523 std::is_nothrow_move_constructible<json_value>::value&& // NOLINT(cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
23524 std::is_nothrow_move_assignable<json_value>::value
23525 )
23526 {
23527 left.swap(right);
23528 }
23529
23530 /// @brief exchanges the values
23531 /// @sa https://json.nlohmann.me/api/basic_json/swap/
23532 void swap(array_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
23533 {
23534 // swap only works for arrays
23535 if (JSON_HEDLEY_LIKELY(is_array()))
23536 {
23537 using std::swap;
23538 swap(*(m_data.m_value.array), other);
23539 }
23540 else
23541 {
23542 JSON_THROW(type_error::create(310, detail::concat("cannot use swap(array_t&) with ", type_name()), this));
23543 }
23544 }
23545
23546 /// @brief exchanges the values
23547 /// @sa https://json.nlohmann.me/api/basic_json/swap/
23548 void swap(object_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
23549 {
23550 // swap only works for objects
23551 if (JSON_HEDLEY_LIKELY(is_object()))
23552 {
23553 using std::swap;
23554 swap(*(m_data.m_value.object), other);
23555 }
23556 else
23557 {
23558 JSON_THROW(type_error::create(310, detail::concat("cannot use swap(object_t&) with ", type_name()), this));
23559 }
23560 }
23561
23562 /// @brief exchanges the values
23563 /// @sa https://json.nlohmann.me/api/basic_json/swap/
23564 void swap(string_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
23565 {
23566 // swap only works for strings
23567 if (JSON_HEDLEY_LIKELY(is_string()))
23568 {
23569 using std::swap;
23570 swap(*(m_data.m_value.string), other);
23571 }
23572 else
23573 {
23574 JSON_THROW(type_error::create(310, detail::concat("cannot use swap(string_t&) with ", type_name()), this));
23575 }
23576 }
23577
23578 /// @brief exchanges the values
23579 /// @sa https://json.nlohmann.me/api/basic_json/swap/
23580 void swap(binary_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
23581 {
23582 // swap only works for strings
23583 if (JSON_HEDLEY_LIKELY(is_binary()))
23584 {
23585 using std::swap;
23586 swap(*(m_data.m_value.binary), other);
23587 }
23588 else
23589 {
23590 JSON_THROW(type_error::create(310, detail::concat("cannot use swap(binary_t&) with ", type_name()), this));
23591 }
23592 }
23593
23594 /// @brief exchanges the values
23595 /// @sa https://json.nlohmann.me/api/basic_json/swap/
23596 void swap(typename binary_t::container_type& other) // NOLINT(bugprone-exception-escape)
23597 {
23598 // swap only works for strings
23599 if (JSON_HEDLEY_LIKELY(is_binary()))
23600 {
23601 using std::swap;
23602 swap(*(m_data.m_value.binary), other);
23603 }
23604 else
23605 {
23606 JSON_THROW(type_error::create(310, detail::concat("cannot use swap(binary_t::container_type&) with ", type_name()), this));
23607 }
23608 }
23609
23610 /// @}
23611
23612 //////////////////////////////////////////
23613 // lexicographical comparison operators //
23614 //////////////////////////////////////////
23615
23616 /// @name lexicographical comparison operators
23617 /// @{
23618
23619 // note parentheses around operands are necessary; see
23620 // https://github.com/nlohmann/json/issues/1530
23621 #define JSON_IMPLEMENT_OPERATOR(op, null_result, unordered_result, default_result) \
23622 const auto lhs_type = lhs.type(); \
23623 const auto rhs_type = rhs.type(); \
23624 \
23625 if (lhs_type == rhs_type) /* NOLINT(readability/braces) */ \
23626 { \
23627 switch (lhs_type) \
23628 { \
23629 case value_t::array: \
23630 return (*lhs.m_data.m_value.array) op (*rhs.m_data.m_value.array); \
23631 \
23632 case value_t::object: \
23633 return (*lhs.m_data.m_value.object) op (*rhs.m_data.m_value.object); \
23634 \
23635 case value_t::null: \
23636 return (null_result); \
23637 \
23638 case value_t::string: \
23639 return (*lhs.m_data.m_value.string) op (*rhs.m_data.m_value.string); \
23640 \
23641 case value_t::boolean: \
23642 return (lhs.m_data.m_value.boolean) op (rhs.m_data.m_value.boolean); \
23643 \
23644 case value_t::number_integer: \
23645 return (lhs.m_data.m_value.number_integer) op (rhs.m_data.m_value.number_integer); \
23646 \
23647 case value_t::number_unsigned: \
23648 return (lhs.m_data.m_value.number_unsigned) op (rhs.m_data.m_value.number_unsigned); \
23649 \
23650 case value_t::number_float: \
23651 return (lhs.m_data.m_value.number_float) op (rhs.m_data.m_value.number_float); \
23652 \
23653 case value_t::binary: \
23654 return (*lhs.m_data.m_value.binary) op (*rhs.m_data.m_value.binary); \
23655 \
23656 case value_t::discarded: \
23657 default: \
23658 return (unordered_result); \
23659 } \
23660 } \
23661 else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_float) \
23662 { \
23663 return static_cast<number_float_t>(lhs.m_data.m_value.number_integer) op rhs.m_data.m_value.number_float; \
23664 } \
23665 else if (lhs_type == value_t::number_float && rhs_type == value_t::number_integer) \
23666 { \
23667 return lhs.m_data.m_value.number_float op static_cast<number_float_t>(rhs.m_data.m_value.number_integer); \
23668 } \
23669 else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_float) \
23670 { \
23671 return static_cast<number_float_t>(lhs.m_data.m_value.number_unsigned) op rhs.m_data.m_value.number_float; \
23672 } \
23673 else if (lhs_type == value_t::number_float && rhs_type == value_t::number_unsigned) \
23674 { \
23675 return lhs.m_data.m_value.number_float op static_cast<number_float_t>(rhs.m_data.m_value.number_unsigned); \
23676 } \
23677 else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_integer) \
23678 { \
23679 return static_cast<number_integer_t>(lhs.m_data.m_value.number_unsigned) op rhs.m_data.m_value.number_integer; \
23680 } \
23681 else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_unsigned) \
23682 { \
23683 return lhs.m_data.m_value.number_integer op static_cast<number_integer_t>(rhs.m_data.m_value.number_unsigned); \
23684 } \
23685 else if(compares_unordered(lhs, rhs))\
23686 {\
23687 return (unordered_result);\
23688 }\
23689 \
23690 return (default_result);
23691
23692 JSON_PRIVATE_UNLESS_TESTED:
23693 // returns true if:
23694 // - any operand is NaN and the other operand is of number type
23695 // - any operand is discarded
23696 // in legacy mode, discarded values are considered ordered if
23697 // an operation is computed as an odd number of inverses of others
23698 static bool compares_unordered(const_reference lhs, const_reference rhs, bool inverse = false) noexcept
23699 {
23700 if ((lhs.is_number_float() && std::isnan(lhs.m_data.m_value.number_float) && rhs.is_number())
23701 || (rhs.is_number_float() && std::isnan(rhs.m_data.m_value.number_float) && lhs.is_number()))
23702 {
23703 return true;
23704 }
23705 #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
23706 return (lhs.is_discarded() || rhs.is_discarded()) && !inverse;
23707 #else
23708 static_cast<void>(inverse);
23709 return lhs.is_discarded() || rhs.is_discarded();
23710 #endif
23711 }
23712
23713 private:
23714 bool compares_unordered(const_reference rhs, bool inverse = false) const noexcept
23715 {
23716 return compares_unordered(*this, rhs, inverse);
23717 }
23718
23719 public:
23720 #if JSON_HAS_THREE_WAY_COMPARISON
23721 /// @brief comparison: equal
23722 /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
23723 bool operator==(const_reference rhs) const noexcept
23724 {
23725 #ifdef __GNUC__
23726 #pragma GCC diagnostic push
23727 #pragma GCC diagnostic ignored "-Wfloat-equal"
23728 #endif
23729 const_reference lhs = *this;
23730 JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
23731 #ifdef __GNUC__
23732 #pragma GCC diagnostic pop
23733 #endif
23734 }
23735
23736 /// @brief comparison: equal
23737 /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
23738 template<typename ScalarType>
23739 requires std::is_scalar_v<ScalarType>
23740 bool operator==(ScalarType rhs) const noexcept
23741 {
23742 return *this == basic_json(rhs);
23743 }
23744
23745 /// @brief comparison: not equal
23746 /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
23747 bool operator!=(const_reference rhs) const noexcept
23748 {
23749 if (compares_unordered(rhs, true))
23750 {
23751 return false;
23752 }
23753 return !operator==(rhs);
23754 }
23755
23756 /// @brief comparison: 3-way
23757 /// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
23758 std::partial_ordering operator<=>(const_reference rhs) const noexcept // *NOPAD*
23759 {
23760 const_reference lhs = *this;
23761 // default_result is used if we cannot compare values. In that case,
23762 // we compare types.
23763 JSON_IMPLEMENT_OPERATOR(<=>, // *NOPAD*
23764 std::partial_ordering::equivalent,
23765 std::partial_ordering::unordered,
23766 lhs_type <=> rhs_type) // *NOPAD*
23767 }
23768
23769 /// @brief comparison: 3-way
23770 /// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
23771 template<typename ScalarType>
23772 requires std::is_scalar_v<ScalarType>
23773 std::partial_ordering operator<=>(ScalarType rhs) const noexcept // *NOPAD*
23774 {
23775 return *this <=> basic_json(rhs); // *NOPAD*
23776 }
23777
23778 #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
23779 // all operators that are computed as an odd number of inverses of others
23780 // need to be overloaded to emulate the legacy comparison behavior
23781
23782 /// @brief comparison: less than or equal
23783 /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
23784 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON)
23785 bool operator<=(const_reference rhs) const noexcept
23786 {
23787 if (compares_unordered(rhs, true))
23788 {
23789 return false;
23790 }
23791 return !(rhs < *this);
23792 }
23793
23794 /// @brief comparison: less than or equal
23795 /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
23796 template<typename ScalarType>
23797 requires std::is_scalar_v<ScalarType>
23798 bool operator<=(ScalarType rhs) const noexcept
23799 {
23800 return *this <= basic_json(rhs);
23801 }
23802
23803 /// @brief comparison: greater than or equal
23804 /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
23805 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON)
23806 bool operator>=(const_reference rhs) const noexcept
23807 {
23808 if (compares_unordered(rhs, true))
23809 {
23810 return false;
23811 }
23812 return !(*this < rhs);
23813 }
23814
23815 /// @brief comparison: greater than or equal
23816 /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
23817 template<typename ScalarType>
23818 requires std::is_scalar_v<ScalarType>
23819 bool operator>=(ScalarType rhs) const noexcept
23820 {
23821 return *this >= basic_json(rhs);
23822 }
23823 #endif
23824 #else
23825 /// @brief comparison: equal
23826 /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
23827 friend bool operator==(const_reference lhs, const_reference rhs) noexcept
23828 {
23829 #ifdef __GNUC__
23830 #pragma GCC diagnostic push
23831 #pragma GCC diagnostic ignored "-Wfloat-equal"
23832 #endif
23833 JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
23834 #ifdef __GNUC__
23835 #pragma GCC diagnostic pop
23836 #endif
23837 }
23838
23839 /// @brief comparison: equal
23840 /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
23841 template<typename ScalarType, typename std::enable_if<
23842 std::is_scalar<ScalarType>::value, int>::type = 0>
23843 friend bool operator==(const_reference lhs, ScalarType rhs) noexcept
23844 {
23845 return lhs == basic_json(rhs);
23846 }
23847
23848 /// @brief comparison: equal
23849 /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
23850 template<typename ScalarType, typename std::enable_if<
23851 std::is_scalar<ScalarType>::value, int>::type = 0>
23852 friend bool operator==(ScalarType lhs, const_reference rhs) noexcept
23853 {
23854 return basic_json(lhs) == rhs;
23855 }
23856
23857 /// @brief comparison: not equal
23858 /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
23859 friend bool operator!=(const_reference lhs, const_reference rhs) noexcept
23860 {
23861 if (compares_unordered(lhs, rhs, true))
23862 {
23863 return false;
23864 }
23865 return !(lhs == rhs);
23866 }
23867
23868 /// @brief comparison: not equal
23869 /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
23870 template<typename ScalarType, typename std::enable_if<
23871 std::is_scalar<ScalarType>::value, int>::type = 0>
23872 friend bool operator!=(const_reference lhs, ScalarType rhs) noexcept
23873 {
23874 return lhs != basic_json(rhs);
23875 }
23876
23877 /// @brief comparison: not equal
23878 /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
23879 template<typename ScalarType, typename std::enable_if<
23880 std::is_scalar<ScalarType>::value, int>::type = 0>
23881 friend bool operator!=(ScalarType lhs, const_reference rhs) noexcept
23882 {
23883 return basic_json(lhs) != rhs;
23884 }
23885
23886 /// @brief comparison: less than
23887 /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
23888 friend bool operator<(const_reference lhs, const_reference rhs) noexcept
23889 {
23890 // default_result is used if we cannot compare values. In that case,
23891 // we compare types. Note we have to call the operator explicitly,
23892 // because MSVC has problems otherwise.
23893 JSON_IMPLEMENT_OPERATOR( <, false, false, operator<(lhs_type, rhs_type))
23894 }
23895
23896 /// @brief comparison: less than
23897 /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
23898 template<typename ScalarType, typename std::enable_if<
23899 std::is_scalar<ScalarType>::value, int>::type = 0>
23900 friend bool operator<(const_reference lhs, ScalarType rhs) noexcept
23901 {
23902 return lhs < basic_json(rhs);
23903 }
23904
23905 /// @brief comparison: less than
23906 /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
23907 template<typename ScalarType, typename std::enable_if<
23908 std::is_scalar<ScalarType>::value, int>::type = 0>
23909 friend bool operator<(ScalarType lhs, const_reference rhs) noexcept
23910 {
23911 return basic_json(lhs) < rhs;
23912 }
23913
23914 /// @brief comparison: less than or equal
23915 /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
23916 friend bool operator<=(const_reference lhs, const_reference rhs) noexcept
23917 {
23918 if (compares_unordered(lhs, rhs, true))
23919 {
23920 return false;
23921 }
23922 return !(rhs < lhs);
23923 }
23924
23925 /// @brief comparison: less than or equal
23926 /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
23927 template<typename ScalarType, typename std::enable_if<
23928 std::is_scalar<ScalarType>::value, int>::type = 0>
23929 friend bool operator<=(const_reference lhs, ScalarType rhs) noexcept
23930 {
23931 return lhs <= basic_json(rhs);
23932 }
23933
23934 /// @brief comparison: less than or equal
23935 /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
23936 template<typename ScalarType, typename std::enable_if<
23937 std::is_scalar<ScalarType>::value, int>::type = 0>
23938 friend bool operator<=(ScalarType lhs, const_reference rhs) noexcept
23939 {
23940 return basic_json(lhs) <= rhs;
23941 }
23942
23943 /// @brief comparison: greater than
23944 /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
23945 friend bool operator>(const_reference lhs, const_reference rhs) noexcept
23946 {
23947 // double inverse
23948 if (compares_unordered(lhs, rhs))
23949 {
23950 return false;
23951 }
23952 return !(lhs <= rhs);
23953 }
23954
23955 /// @brief comparison: greater than
23956 /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
23957 template<typename ScalarType, typename std::enable_if<
23958 std::is_scalar<ScalarType>::value, int>::type = 0>
23959 friend bool operator>(const_reference lhs, ScalarType rhs) noexcept
23960 {
23961 return lhs > basic_json(rhs);
23962 }
23963
23964 /// @brief comparison: greater than
23965 /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
23966 template<typename ScalarType, typename std::enable_if<
23967 std::is_scalar<ScalarType>::value, int>::type = 0>
23968 friend bool operator>(ScalarType lhs, const_reference rhs) noexcept
23969 {
23970 return basic_json(lhs) > rhs;
23971 }
23972
23973 /// @brief comparison: greater than or equal
23974 /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
23975 friend bool operator>=(const_reference lhs, const_reference rhs) noexcept
23976 {
23977 if (compares_unordered(lhs, rhs, true))
23978 {
23979 return false;
23980 }
23981 return !(lhs < rhs);
23982 }
23983
23984 /// @brief comparison: greater than or equal
23985 /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
23986 template<typename ScalarType, typename std::enable_if<
23987 std::is_scalar<ScalarType>::value, int>::type = 0>
23988 friend bool operator>=(const_reference lhs, ScalarType rhs) noexcept
23989 {
23990 return lhs >= basic_json(rhs);
23991 }
23992
23993 /// @brief comparison: greater than or equal
23994 /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
23995 template<typename ScalarType, typename std::enable_if<
23996 std::is_scalar<ScalarType>::value, int>::type = 0>
23997 friend bool operator>=(ScalarType lhs, const_reference rhs) noexcept
23998 {
23999 return basic_json(lhs) >= rhs;
24000 }
24001 #endif
24002
24003 #undef JSON_IMPLEMENT_OPERATOR
24004
24005 /// @}
24006
24007 ///////////////////
24008 // serialization //
24009 ///////////////////
24010
24011 /// @name serialization
24012 /// @{
24013 #ifndef JSON_NO_IO
24014 /// @brief serialize to stream
24015 /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
24016 friend std::ostream& operator<<(std::ostream& o, const basic_json& j)
24017 {
24018 // read width member and use it as indentation parameter if nonzero
24019 const bool pretty_print = o.width() > 0;
24020 const auto indentation = pretty_print ? o.width() : 0;
24021
24022 // reset width to 0 for subsequent calls to this stream
24023 o.width(0);
24024
24025 // do the actual serialization
24026 serializer s(detail::output_adapter<char>(o), o.fill());
24027 s.dump(j, pretty_print, false, static_cast<unsigned int>(indentation));
24028 return o;
24029 }
24030
24031 /// @brief serialize to stream
24032 /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
24033 /// @deprecated This function is deprecated since 3.0.0 and will be removed in
24034 /// version 4.0.0 of the library. Please use
24035 /// operator<<(std::ostream&, const basic_json&) instead; that is,
24036 /// replace calls like `j >> o;` with `o << j;`.
24037 JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator<<(std::ostream&, const basic_json&))
24038 friend std::ostream& operator>>(const basic_json& j, std::ostream& o)
24039 {
24040 return o << j;
24041 }
24042 #endif // JSON_NO_IO
24043 /// @}
24044
24045 /////////////////////
24046 // deserialization //
24047 /////////////////////
24048
24049 /// @name deserialization
24050 /// @{
24051
24052 /// @brief deserialize from a compatible input
24053 /// @sa https://json.nlohmann.me/api/basic_json/parse/
24054 template<typename InputType>
24055 JSON_HEDLEY_WARN_UNUSED_RESULT
24056 static basic_json parse(InputType&& i,
24057 parser_callback_t cb = nullptr,
24058 const bool allow_exceptions = true,
24059 const bool ignore_comments = false)
24060 {
24061 basic_json result;
24062 parser(detail::input_adapter(std::forward<InputType>(i)), std::move(cb), allow_exceptions, ignore_comments).parse(true, result); // cppcheck-suppress[accessMoved,accessForwarded]
24063 return result;
24064 }
24065
24066 /// @brief deserialize from a pair of character iterators
24067 /// @sa https://json.nlohmann.me/api/basic_json/parse/
24068 template<typename IteratorType>
24069 JSON_HEDLEY_WARN_UNUSED_RESULT
24070 static basic_json parse(IteratorType first,
24071 IteratorType last,
24072 parser_callback_t cb = nullptr,
24073 const bool allow_exceptions = true,
24074 const bool ignore_comments = false)
24075 {
24076 basic_json result;
24077 parser(detail::input_adapter(std::move(first), std::move(last)), std::move(cb), allow_exceptions, ignore_comments).parse(true, result); // cppcheck-suppress[accessMoved]
24078 return result;
24079 }
24080
24081 JSON_HEDLEY_WARN_UNUSED_RESULT
24082 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, parse(ptr, ptr + len))
24083 static basic_json parse(detail::span_input_adapter&& i,
24084 parser_callback_t cb = nullptr,
24085 const bool allow_exceptions = true,
24086 const bool ignore_comments = false)
24087 {
24088 basic_json result;
24089 parser(i.get(), std::move(cb), allow_exceptions, ignore_comments).parse(true, result); // cppcheck-suppress[accessMoved]
24090 return result;
24091 }
24092
24093 /// @brief check if the input is valid JSON
24094 /// @sa https://json.nlohmann.me/api/basic_json/accept/
24095 template<typename InputType>
24096 static bool accept(InputType&& i,
24097 const bool ignore_comments = false)
24098 {
24099 return parser(detail::input_adapter(std::forward<InputType>(i)), nullptr, false, ignore_comments).accept(true);
24100 }
24101
24102 /// @brief check if the input is valid JSON
24103 /// @sa https://json.nlohmann.me/api/basic_json/accept/
24104 template<typename IteratorType>
24105 static bool accept(IteratorType first, IteratorType last,
24106 const bool ignore_comments = false)
24107 {
24108 return parser(detail::input_adapter(std::move(first), std::move(last)), nullptr, false, ignore_comments).accept(true);
24109 }
24110
24111 JSON_HEDLEY_WARN_UNUSED_RESULT
24112 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, accept(ptr, ptr + len))
24113 static bool accept(detail::span_input_adapter&& i,
24114 const bool ignore_comments = false)
24115 {
24116 return parser(i.get(), nullptr, false, ignore_comments).accept(true);
24117 }
24118
24119 /// @brief generate SAX events
24120 /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
24121 template <typename InputType, typename SAX>
24122 JSON_HEDLEY_NON_NULL(2)
24123 static bool sax_parse(InputType&& i, SAX* sax,
24124 input_format_t format = input_format_t::json,
24125 const bool strict = true,
24126 const bool ignore_comments = false)
24127 {
24128 auto ia = detail::input_adapter(std::forward<InputType>(i));
24129 return format == input_format_t::json
24130 ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
24131 : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
24132 }
24133
24134 /// @brief generate SAX events
24135 /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
24136 template<class IteratorType, class SAX>
24137 JSON_HEDLEY_NON_NULL(3)
24138 static bool sax_parse(IteratorType first, IteratorType last, SAX* sax,
24139 input_format_t format = input_format_t::json,
24140 const bool strict = true,
24141 const bool ignore_comments = false)
24142 {
24143 auto ia = detail::input_adapter(std::move(first), std::move(last));
24144 return format == input_format_t::json
24145 ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
24146 : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
24147 }
24148
24149 /// @brief generate SAX events
24150 /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
24151 /// @deprecated This function is deprecated since 3.8.0 and will be removed in
24152 /// version 4.0.0 of the library. Please use
24153 /// sax_parse(ptr, ptr + len) instead.
24154 template <typename SAX>
24155 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, sax_parse(ptr, ptr + len, ...))
24156 JSON_HEDLEY_NON_NULL(2)
24157 static bool sax_parse(detail::span_input_adapter&& i, SAX* sax,
24158 input_format_t format = input_format_t::json,
24159 const bool strict = true,
24160 const bool ignore_comments = false)
24161 {
24162 auto ia = i.get();
24163 return format == input_format_t::json
24164 // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
24165 ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
24166 // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
24167 : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
24168 }
24169 #ifndef JSON_NO_IO
24170 /// @brief deserialize from stream
24171 /// @sa https://json.nlohmann.me/api/basic_json/operator_gtgt/
24172 /// @deprecated This stream operator is deprecated since 3.0.0 and will be removed in
24173 /// version 4.0.0 of the library. Please use
24174 /// operator>>(std::istream&, basic_json&) instead; that is,
24175 /// replace calls like `j << i;` with `i >> j;`.
24176 JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator>>(std::istream&, basic_json&))
24177 friend std::istream& operator<<(basic_json& j, std::istream& i)
24178 {
24179 return operator>>(i, j);
24180 }
24181
24182 /// @brief deserialize from stream
24183 /// @sa https://json.nlohmann.me/api/basic_json/operator_gtgt/
24184 friend std::istream& operator>>(std::istream& i, basic_json& j)
24185 {
24186 parser(detail::input_adapter(i)).parse(false, j);
24187 return i;
24188 }
24189 #endif // JSON_NO_IO
24190 /// @}
24191
24192 ///////////////////////////
24193 // convenience functions //
24194 ///////////////////////////
24195
24196 /// @brief return the type as string
24197 /// @sa https://json.nlohmann.me/api/basic_json/type_name/
24198 JSON_HEDLEY_RETURNS_NON_NULL
24199 const char* type_name() const noexcept
24200 {
24201 switch (m_data.m_type)
24202 {
24203 case value_t::null:
24204 return "null";
24205 case value_t::object:
24206 return "object";
24207 case value_t::array:
24208 return "array";
24209 case value_t::string:
24210 return "string";
24211 case value_t::boolean:
24212 return "boolean";
24213 case value_t::binary:
24214 return "binary";
24215 case value_t::discarded:
24216 return "discarded";
24217 case value_t::number_integer:
24218 case value_t::number_unsigned:
24219 case value_t::number_float:
24220 default:
24221 return "number";
24222 }
24223 }
24224
24225 JSON_PRIVATE_UNLESS_TESTED:
24226 //////////////////////
24227 // member variables //
24228 //////////////////////
24229
24230 struct data
24231 {
24232 /// the type of the current element
24233 value_t m_type = value_t::null;
24234
24235 /// the value of the current element
24236 json_value m_value = {};
24237
24238 data(const value_t v)
24239 : m_type(v), m_value(v)
24240 {
24241 }
24242
24243 data(size_type cnt, const basic_json& val)
24244 : m_type(value_t::array)
24245 {
24246 m_value.array = create<array_t>(cnt, val);
24247 }
24248
24249 data() noexcept = default;
24250 data(data&&) noexcept = default;
24251 data(const data&) noexcept = delete;
24252 data& operator=(data&&) noexcept = delete;
24253 data& operator=(const data&) noexcept = delete;
24254
24255 ~data() noexcept
24256 {
24257 m_value.destroy(m_type);
24258 }
24259 };
24260
24261 data m_data = {};
24262
24263 #if JSON_DIAGNOSTICS
24264 /// a pointer to a parent value (for debugging purposes)
24265 basic_json* m_parent = nullptr;
24266 #endif
24267
24268 #if JSON_DIAGNOSTIC_POSITIONS
24269 /// the start position of the value
24270 std::size_t start_position = std::string::npos;
24271 /// the end position of the value
24272 std::size_t end_position = std::string::npos;
24273 public:
24274 constexpr std::size_t start_pos() const noexcept
24275 {
24276 return start_position;
24277 }
24278
24279 constexpr std::size_t end_pos() const noexcept
24280 {
24281 return end_position;
24282 }
24283 #endif
24284
24285 //////////////////////////////////////////
24286 // binary serialization/deserialization //
24287 //////////////////////////////////////////
24288
24289 /// @name binary serialization/deserialization support
24290 /// @{
24291
24292 public:
24293 /// @brief create a CBOR serialization of a given JSON value
24294 /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
24295 static std::vector<std::uint8_t> to_cbor(const basic_json& j)
24296 {
24297 std::vector<std::uint8_t> result;
24298 to_cbor(j, result);
24299 return result;
24300 }
24301
24302 /// @brief create a CBOR serialization of a given JSON value
24303 /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
24304 static void to_cbor(const basic_json& j, detail::output_adapter<std::uint8_t> o)
24305 {
24306 binary_writer<std::uint8_t>(o).write_cbor(j);
24307 }
24308
24309 /// @brief create a CBOR serialization of a given JSON value
24310 /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
24311 static void to_cbor(const basic_json& j, detail::output_adapter<char> o)
24312 {
24313 binary_writer<char>(o).write_cbor(j);
24314 }
24315
24316 /// @brief create a MessagePack serialization of a given JSON value
24317 /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
24318 static std::vector<std::uint8_t> to_msgpack(const basic_json& j)
24319 {
24320 std::vector<std::uint8_t> result;
24321 to_msgpack(j, result);
24322 return result;
24323 }
24324
24325 /// @brief create a MessagePack serialization of a given JSON value
24326 /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
24327 static void to_msgpack(const basic_json& j, detail::output_adapter<std::uint8_t> o)
24328 {
24329 binary_writer<std::uint8_t>(o).write_msgpack(j);
24330 }
24331
24332 /// @brief create a MessagePack serialization of a given JSON value
24333 /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
24334 static void to_msgpack(const basic_json& j, detail::output_adapter<char> o)
24335 {
24336 binary_writer<char>(o).write_msgpack(j);
24337 }
24338
24339 /// @brief create a UBJSON serialization of a given JSON value
24340 /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
24341 static std::vector<std::uint8_t> to_ubjson(const basic_json& j,
24342 const bool use_size = false,
24343 const bool use_type = false)
24344 {
24345 std::vector<std::uint8_t> result;
24346 to_ubjson(j, result, use_size, use_type);
24347 return result;
24348 }
24349
24350 /// @brief create a UBJSON serialization of a given JSON value
24351 /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
24352 static void to_ubjson(const basic_json& j, detail::output_adapter<std::uint8_t> o,
24353 const bool use_size = false, const bool use_type = false)
24354 {
24355 binary_writer<std::uint8_t>(o).write_ubjson(j, use_size, use_type);
24356 }
24357
24358 /// @brief create a UBJSON serialization of a given JSON value
24359 /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
24360 static void to_ubjson(const basic_json& j, detail::output_adapter<char> o,
24361 const bool use_size = false, const bool use_type = false)
24362 {
24363 binary_writer<char>(o).write_ubjson(j, use_size, use_type);
24364 }
24365
24366 /// @brief create a BJData serialization of a given JSON value
24367 /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
24368 static std::vector<std::uint8_t> to_bjdata(const basic_json& j,
24369 const bool use_size = false,
24370 const bool use_type = false,
24371 const bjdata_version_t version = bjdata_version_t::draft2)
24372 {
24373 std::vector<std::uint8_t> result;
24374 to_bjdata(j, result, use_size, use_type, version);
24375 return result;
24376 }
24377
24378 /// @brief create a BJData serialization of a given JSON value
24379 /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
24380 static void to_bjdata(const basic_json& j, detail::output_adapter<std::uint8_t> o,
24381 const bool use_size = false, const bool use_type = false,
24382 const bjdata_version_t version = bjdata_version_t::draft2)
24383 {
24384 binary_writer<std::uint8_t>(o).write_ubjson(j, use_size, use_type, true, true, version);
24385 }
24386
24387 /// @brief create a BJData serialization of a given JSON value
24388 /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
24389 static void to_bjdata(const basic_json& j, detail::output_adapter<char> o,
24390 const bool use_size = false, const bool use_type = false,
24391 const bjdata_version_t version = bjdata_version_t::draft2)
24392 {
24393 binary_writer<char>(o).write_ubjson(j, use_size, use_type, true, true, version);
24394 }
24395
24396 /// @brief create a BSON serialization of a given JSON value
24397 /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
24398 static std::vector<std::uint8_t> to_bson(const basic_json& j)
24399 {
24400 std::vector<std::uint8_t> result;
24401 to_bson(j, result);
24402 return result;
24403 }
24404
24405 /// @brief create a BSON serialization of a given JSON value
24406 /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
24407 static void to_bson(const basic_json& j, detail::output_adapter<std::uint8_t> o)
24408 {
24409 binary_writer<std::uint8_t>(o).write_bson(j);
24410 }
24411
24412 /// @brief create a BSON serialization of a given JSON value
24413 /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
24414 static void to_bson(const basic_json& j, detail::output_adapter<char> o)
24415 {
24416 binary_writer<char>(o).write_bson(j);
24417 }
24418
24419 /// @brief create a JSON value from an input in CBOR format
24420 /// @sa https://json.nlohmann.me/api/basic_json/from_cbor/
24421 template<typename InputType>
24422 JSON_HEDLEY_WARN_UNUSED_RESULT
24423 static basic_json from_cbor(InputType&& i,
24424 const bool strict = true,
24425 const bool allow_exceptions = true,
24426 const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
24427 {
24428 basic_json result;
24429 auto ia = detail::input_adapter(std::forward<InputType>(i));
24430 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24431 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]
24432 return res ? result : basic_json(value_t::discarded);
24433 }
24434
24435 /// @brief create a JSON value from an input in CBOR format
24436 /// @sa https://json.nlohmann.me/api/basic_json/from_cbor/
24437 template<typename IteratorType>
24438 JSON_HEDLEY_WARN_UNUSED_RESULT
24439 static basic_json from_cbor(IteratorType first, IteratorType last,
24440 const bool strict = true,
24441 const bool allow_exceptions = true,
24442 const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
24443 {
24444 basic_json result;
24445 auto ia = detail::input_adapter(std::move(first), std::move(last));
24446 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24447 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]
24448 return res ? result : basic_json(value_t::discarded);
24449 }
24450
24451 template<typename T>
24452 JSON_HEDLEY_WARN_UNUSED_RESULT
24453 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len))
24454 static basic_json from_cbor(const T* ptr, std::size_t len,
24455 const bool strict = true,
24456 const bool allow_exceptions = true,
24457 const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
24458 {
24459 return from_cbor(ptr, ptr + len, strict, allow_exceptions, tag_handler);
24460 }
24461
24462 JSON_HEDLEY_WARN_UNUSED_RESULT
24463 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len))
24464 static basic_json from_cbor(detail::span_input_adapter&& i,
24465 const bool strict = true,
24466 const bool allow_exceptions = true,
24467 const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
24468 {
24469 basic_json result;
24470 auto ia = i.get();
24471 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24472 // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
24473 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]
24474 return res ? result : basic_json(value_t::discarded);
24475 }
24476
24477 /// @brief create a JSON value from an input in MessagePack format
24478 /// @sa https://json.nlohmann.me/api/basic_json/from_msgpack/
24479 template<typename InputType>
24480 JSON_HEDLEY_WARN_UNUSED_RESULT
24481 static basic_json from_msgpack(InputType&& i,
24482 const bool strict = true,
24483 const bool allow_exceptions = true)
24484 {
24485 basic_json result;
24486 auto ia = detail::input_adapter(std::forward<InputType>(i));
24487 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24488 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]
24489 return res ? result : basic_json(value_t::discarded);
24490 }
24491
24492 /// @brief create a JSON value from an input in MessagePack format
24493 /// @sa https://json.nlohmann.me/api/basic_json/from_msgpack/
24494 template<typename IteratorType>
24495 JSON_HEDLEY_WARN_UNUSED_RESULT
24496 static basic_json from_msgpack(IteratorType first, IteratorType last,
24497 const bool strict = true,
24498 const bool allow_exceptions = true)
24499 {
24500 basic_json result;
24501 auto ia = detail::input_adapter(std::move(first), std::move(last));
24502 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24503 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]
24504 return res ? result : basic_json(value_t::discarded);
24505 }
24506
24507 template<typename T>
24508 JSON_HEDLEY_WARN_UNUSED_RESULT
24509 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len))
24510 static basic_json from_msgpack(const T* ptr, std::size_t len,
24511 const bool strict = true,
24512 const bool allow_exceptions = true)
24513 {
24514 return from_msgpack(ptr, ptr + len, strict, allow_exceptions);
24515 }
24516
24517 JSON_HEDLEY_WARN_UNUSED_RESULT
24518 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len))
24519 static basic_json from_msgpack(detail::span_input_adapter&& i,
24520 const bool strict = true,
24521 const bool allow_exceptions = true)
24522 {
24523 basic_json result;
24524 auto ia = i.get();
24525 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24526 // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
24527 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]
24528 return res ? result : basic_json(value_t::discarded);
24529 }
24530
24531 /// @brief create a JSON value from an input in UBJSON format
24532 /// @sa https://json.nlohmann.me/api/basic_json/from_ubjson/
24533 template<typename InputType>
24534 JSON_HEDLEY_WARN_UNUSED_RESULT
24535 static basic_json from_ubjson(InputType&& i,
24536 const bool strict = true,
24537 const bool allow_exceptions = true)
24538 {
24539 basic_json result;
24540 auto ia = detail::input_adapter(std::forward<InputType>(i));
24541 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24542 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]
24543 return res ? result : basic_json(value_t::discarded);
24544 }
24545
24546 /// @brief create a JSON value from an input in UBJSON format
24547 /// @sa https://json.nlohmann.me/api/basic_json/from_ubjson/
24548 template<typename IteratorType>
24549 JSON_HEDLEY_WARN_UNUSED_RESULT
24550 static basic_json from_ubjson(IteratorType first, IteratorType last,
24551 const bool strict = true,
24552 const bool allow_exceptions = true)
24553 {
24554 basic_json result;
24555 auto ia = detail::input_adapter(std::move(first), std::move(last));
24556 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24557 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]
24558 return res ? result : basic_json(value_t::discarded);
24559 }
24560
24561 template<typename T>
24562 JSON_HEDLEY_WARN_UNUSED_RESULT
24563 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len))
24564 static basic_json from_ubjson(const T* ptr, std::size_t len,
24565 const bool strict = true,
24566 const bool allow_exceptions = true)
24567 {
24568 return from_ubjson(ptr, ptr + len, strict, allow_exceptions);
24569 }
24570
24571 JSON_HEDLEY_WARN_UNUSED_RESULT
24572 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len))
24573 static basic_json from_ubjson(detail::span_input_adapter&& i,
24574 const bool strict = true,
24575 const bool allow_exceptions = true)
24576 {
24577 basic_json result;
24578 auto ia = i.get();
24579 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24580 // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
24581 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]
24582 return res ? result : basic_json(value_t::discarded);
24583 }
24584
24585 /// @brief create a JSON value from an input in BJData format
24586 /// @sa https://json.nlohmann.me/api/basic_json/from_bjdata/
24587 template<typename InputType>
24588 JSON_HEDLEY_WARN_UNUSED_RESULT
24589 static basic_json from_bjdata(InputType&& i,
24590 const bool strict = true,
24591 const bool allow_exceptions = true)
24592 {
24593 basic_json result;
24594 auto ia = detail::input_adapter(std::forward<InputType>(i));
24595 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24596 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]
24597 return res ? result : basic_json(value_t::discarded);
24598 }
24599
24600 /// @brief create a JSON value from an input in BJData format
24601 /// @sa https://json.nlohmann.me/api/basic_json/from_bjdata/
24602 template<typename IteratorType>
24603 JSON_HEDLEY_WARN_UNUSED_RESULT
24604 static basic_json from_bjdata(IteratorType first, IteratorType last,
24605 const bool strict = true,
24606 const bool allow_exceptions = true)
24607 {
24608 basic_json result;
24609 auto ia = detail::input_adapter(std::move(first), std::move(last));
24610 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24611 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]
24612 return res ? result : basic_json(value_t::discarded);
24613 }
24614
24615 /// @brief create a JSON value from an input in BSON format
24616 /// @sa https://json.nlohmann.me/api/basic_json/from_bson/
24617 template<typename InputType>
24618 JSON_HEDLEY_WARN_UNUSED_RESULT
24619 static basic_json from_bson(InputType&& i,
24620 const bool strict = true,
24621 const bool allow_exceptions = true)
24622 {
24623 basic_json result;
24624 auto ia = detail::input_adapter(std::forward<InputType>(i));
24625 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24626 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]
24627 return res ? result : basic_json(value_t::discarded);
24628 }
24629
24630 /// @brief create a JSON value from an input in BSON format
24631 /// @sa https://json.nlohmann.me/api/basic_json/from_bson/
24632 template<typename IteratorType>
24633 JSON_HEDLEY_WARN_UNUSED_RESULT
24634 static basic_json from_bson(IteratorType first, IteratorType last,
24635 const bool strict = true,
24636 const bool allow_exceptions = true)
24637 {
24638 basic_json result;
24639 auto ia = detail::input_adapter(std::move(first), std::move(last));
24640 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24641 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]
24642 return res ? result : basic_json(value_t::discarded);
24643 }
24644
24645 template<typename T>
24646 JSON_HEDLEY_WARN_UNUSED_RESULT
24647 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len))
24648 static basic_json from_bson(const T* ptr, std::size_t len,
24649 const bool strict = true,
24650 const bool allow_exceptions = true)
24651 {
24652 return from_bson(ptr, ptr + len, strict, allow_exceptions);
24653 }
24654
24655 JSON_HEDLEY_WARN_UNUSED_RESULT
24656 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len))
24657 static basic_json from_bson(detail::span_input_adapter&& i,
24658 const bool strict = true,
24659 const bool allow_exceptions = true)
24660 {
24661 basic_json result;
24662 auto ia = i.get();
24663 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24664 // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
24665 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]
24666 return res ? result : basic_json(value_t::discarded);
24667 }
24668 /// @}
24669
24670 //////////////////////////
24671 // JSON Pointer support //
24672 //////////////////////////
24673
24674 /// @name JSON Pointer functions
24675 /// @{
24676
24677 /// @brief access specified element via JSON Pointer
24678 /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
24679 reference operator[](const json_pointer& ptr)
24680 {
24681 return ptr.get_unchecked(this);
24682 }
24683
24684 template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
24685 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
24686 reference operator[](const ::nlohmann::json_pointer<BasicJsonType>& ptr)
24687 {
24688 return ptr.get_unchecked(this);
24689 }
24690
24691 /// @brief access specified element via JSON Pointer
24692 /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
24693 const_reference operator[](const json_pointer& ptr) const
24694 {
24695 return ptr.get_unchecked(this);
24696 }
24697
24698 template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
24699 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
24700 const_reference operator[](const ::nlohmann::json_pointer<BasicJsonType>& ptr) const
24701 {
24702 return ptr.get_unchecked(this);
24703 }
24704
24705 /// @brief access specified element via JSON Pointer
24706 /// @sa https://json.nlohmann.me/api/basic_json/at/
24707 reference at(const json_pointer& ptr)
24708 {
24709 return ptr.get_checked(this);
24710 }
24711
24712 template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
24713 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
24714 reference at(const ::nlohmann::json_pointer<BasicJsonType>& ptr)
24715 {
24716 return ptr.get_checked(this);
24717 }
24718
24719 /// @brief access specified element via JSON Pointer
24720 /// @sa https://json.nlohmann.me/api/basic_json/at/
24721 const_reference at(const json_pointer& ptr) const
24722 {
24723 return ptr.get_checked(this);
24724 }
24725
24726 template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
24727 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
24728 const_reference at(const ::nlohmann::json_pointer<BasicJsonType>& ptr) const
24729 {
24730 return ptr.get_checked(this);
24731 }
24732
24733 /// @brief return flattened JSON value
24734 /// @sa https://json.nlohmann.me/api/basic_json/flatten/
24735 basic_json flatten() const
24736 {
24737 basic_json result(value_t::object);
24738 json_pointer::flatten("", *this, result);
24739 return result;
24740 }
24741
24742 /// @brief unflatten a previously flattened JSON value
24743 /// @sa https://json.nlohmann.me/api/basic_json/unflatten/
24744 basic_json unflatten() const
24745 {
24746 return json_pointer::unflatten(*this);
24747 }
24748
24749 /// @}
24750
24751 //////////////////////////
24752 // JSON Patch functions //
24753 //////////////////////////
24754
24755 /// @name JSON Patch functions
24756 /// @{
24757
24758 /// @brief applies a JSON patch in-place without copying the object
24759 /// @sa https://json.nlohmann.me/api/basic_json/patch/
24760 void patch_inplace(const basic_json& json_patch)
24761 {
24762 basic_json& result = *this;
24763 // the valid JSON Patch operations
24764 enum class patch_operations {add, remove, replace, move, copy, test, invalid};
24765
24766 const auto get_op = [](const string_t& op)
24767 {
24768 if (op == "add")
24769 {
24770 return patch_operations::add;
24771 }
24772 if (op == "remove")
24773 {
24774 return patch_operations::remove;
24775 }
24776 if (op == "replace")
24777 {
24778 return patch_operations::replace;
24779 }
24780 if (op == "move")
24781 {
24782 return patch_operations::move;
24783 }
24784 if (op == "copy")
24785 {
24786 return patch_operations::copy;
24787 }
24788 if (op == "test")
24789 {
24790 return patch_operations::test;
24791 }
24792
24793 return patch_operations::invalid;
24794 };
24795
24796 // wrapper for "add" operation; add value at ptr
24797 const auto operation_add = [&result](json_pointer & ptr, const basic_json & val)
24798 {
24799 // adding to the root of the target document means replacing it
24800 if (ptr.empty())
24801 {
24802 result = val;
24803 return;
24804 }
24805
24806 // make sure the top element of the pointer exists
24807 json_pointer const top_pointer = ptr.top();
24808 if (top_pointer != ptr)
24809 {
24810 result.at(top_pointer);
24811 }
24812
24813 // get reference to parent of JSON pointer ptr
24814 const auto last_path = ptr.back();
24815 ptr.pop_back();
24816 // parent must exist when performing patch add per RFC6902 specs
24817 basic_json& parent = result.at(ptr);
24818
24819 switch (parent.m_data.m_type)
24820 {
24821 case value_t::null:
24822 case value_t::object:
24823 {
24824 // use operator[] to add value
24825 parent[last_path] = val;
24826 break;
24827 }
24828
24829 case value_t::array:
24830 {
24831 if (last_path == "-")
24832 {
24833 // special case: append to back
24834 parent.push_back(val);
24835 }
24836 else
24837 {
24838 const auto idx = json_pointer::template array_index<basic_json_t>(last_path);
24839 if (JSON_HEDLEY_UNLIKELY(idx > parent.size()))
24840 {
24841 // avoid undefined behavior
24842 JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), &parent));
24843 }
24844
24845 // default case: insert add offset
24846 parent.insert(parent.begin() + static_cast<difference_type>(idx), val);
24847 }
24848 break;
24849 }
24850
24851 // if there exists a parent it cannot be primitive
24852 case value_t::string: // LCOV_EXCL_LINE
24853 case value_t::boolean: // LCOV_EXCL_LINE
24854 case value_t::number_integer: // LCOV_EXCL_LINE
24855 case value_t::number_unsigned: // LCOV_EXCL_LINE
24856 case value_t::number_float: // LCOV_EXCL_LINE
24857 case value_t::binary: // LCOV_EXCL_LINE
24858 case value_t::discarded: // LCOV_EXCL_LINE
24859 default: // LCOV_EXCL_LINE
24860 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
24861 }
24862 };
24863
24864 // wrapper for "remove" operation; remove value at ptr
24865 const auto operation_remove = [this, & result](json_pointer & ptr)
24866 {
24867 // get reference to parent of JSON pointer ptr
24868 const auto last_path = ptr.back();
24869 ptr.pop_back();
24870 basic_json& parent = result.at(ptr);
24871
24872 // remove child
24873 if (parent.is_object())
24874 {
24875 // perform range check
24876 auto it = parent.find(last_path);
24877 if (JSON_HEDLEY_LIKELY(it != parent.end()))
24878 {
24879 parent.erase(it);
24880 }
24881 else
24882 {
24883 JSON_THROW(out_of_range::create(403, detail::concat("key '", last_path, "' not found"), this));
24884 }
24885 }
24886 else if (parent.is_array())
24887 {
24888 // note erase performs range check
24889 parent.erase(json_pointer::template array_index<basic_json_t>(last_path));
24890 }
24891 };
24892
24893 // type check: top level value must be an array
24894 if (JSON_HEDLEY_UNLIKELY(!json_patch.is_array()))
24895 {
24896 JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", &json_patch));
24897 }
24898
24899 // iterate and apply the operations
24900 for (const auto& val : json_patch)
24901 {
24902 // wrapper to get a value for an operation
24903 const auto get_value = [&val](const string_t& op,
24904 const string_t& member,
24905 bool string_type) -> basic_json &
24906 {
24907 // find value
24908 auto it = val.m_data.m_value.object->find(member);
24909
24910 // context-sensitive error message
24911 const auto error_msg = (op == "op") ? "operation" : detail::concat("operation '", op, '\''); // NOLINT(bugprone-unused-local-non-trivial-variable)
24912
24913 // check if desired value is present
24914 if (JSON_HEDLEY_UNLIKELY(it == val.m_data.m_value.object->end()))
24915 {
24916 // NOLINTNEXTLINE(performance-inefficient-string-concatenation)
24917 JSON_THROW(parse_error::create(105, 0, detail::concat(error_msg, " must have member '", member, "'"), &val));
24918 }
24919
24920 // check if result is of type string
24921 if (JSON_HEDLEY_UNLIKELY(string_type && !it->second.is_string()))
24922 {
24923 // NOLINTNEXTLINE(performance-inefficient-string-concatenation)
24924 JSON_THROW(parse_error::create(105, 0, detail::concat(error_msg, " must have string member '", member, "'"), &val));
24925 }
24926
24927 // no error: return value
24928 return it->second;
24929 };
24930
24931 // type check: every element of the array must be an object
24932 if (JSON_HEDLEY_UNLIKELY(!val.is_object()))
24933 {
24934 JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", &val));
24935 }
24936
24937 // collect mandatory members
24938 const auto op = get_value("op", "op", true).template get<string_t>();
24939 const auto path = get_value(op, "path", true).template get<string_t>();
24940 json_pointer ptr(path);
24941
24942 switch (get_op(op))
24943 {
24944 case patch_operations::add:
24945 {
24946 operation_add(ptr, get_value("add", "value", false));
24947 break;
24948 }
24949
24950 case patch_operations::remove:
24951 {
24952 operation_remove(ptr);
24953 break;
24954 }
24955
24956 case patch_operations::replace:
24957 {
24958 // the "path" location must exist - use at()
24959 result.at(ptr) = get_value("replace", "value", false);
24960 break;
24961 }
24962
24963 case patch_operations::move:
24964 {
24965 const auto from_path = get_value("move", "from", true).template get<string_t>();
24966 json_pointer from_ptr(from_path);
24967
24968 // the "from" location must exist - use at()
24969 basic_json const v = result.at(from_ptr);
24970
24971 // The move operation is functionally identical to a
24972 // "remove" operation on the "from" location, followed
24973 // immediately by an "add" operation at the target
24974 // location with the value that was just removed.
24975 operation_remove(from_ptr);
24976 operation_add(ptr, v);
24977 break;
24978 }
24979
24980 case patch_operations::copy:
24981 {
24982 const auto from_path = get_value("copy", "from", true).template get<string_t>();
24983 const json_pointer from_ptr(from_path);
24984
24985 // the "from" location must exist - use at()
24986 basic_json const v = result.at(from_ptr);
24987
24988 // The copy is functionally identical to an "add"
24989 // operation at the target location using the value
24990 // specified in the "from" member.
24991 operation_add(ptr, v);
24992 break;
24993 }
24994
24995 case patch_operations::test:
24996 {
24997 bool success = false;
24998 JSON_TRY
24999 {
25000 // check if "value" matches the one at "path"
25001 // the "path" location must exist - use at()
25002 success = (result.at(ptr) == get_value("test", "value", false));
25003 }
25004 JSON_INTERNAL_CATCH (out_of_range&)
25005 {
25006 // ignore out of range errors: success remains false
25007 }
25008
25009 // throw an exception if test fails
25010 if (JSON_HEDLEY_UNLIKELY(!success))
25011 {
25012 JSON_THROW(other_error::create(501, detail::concat("unsuccessful: ", val.dump()), &val));
25013 }
25014
25015 break;
25016 }
25017
25018 case patch_operations::invalid:
25019 default:
25020 {
25021 // op must be "add", "remove", "replace", "move", "copy", or
25022 // "test"
25023 JSON_THROW(parse_error::create(105, 0, detail::concat("operation value '", op, "' is invalid"), &val));
25024 }
25025 }
25026 }
25027 }
25028
25029 /// @brief applies a JSON patch to a copy of the current object
25030 /// @sa https://json.nlohmann.me/api/basic_json/patch/
25031 basic_json patch(const basic_json& json_patch) const
25032 {
25033 basic_json result = *this;
25034 result.patch_inplace(json_patch);
25035 return result;
25036 }
25037
25038 /// @brief creates a diff as a JSON patch
25039 /// @sa https://json.nlohmann.me/api/basic_json/diff/
25040 JSON_HEDLEY_WARN_UNUSED_RESULT
25041 static basic_json diff(const basic_json& source, const basic_json& target,
25042 const string_t& path = "")
25043 {
25044 // the patch
25045 basic_json result(value_t::array);
25046
25047 // if the values are the same, return empty patch
25048 if (source == target)
25049 {
25050 return result;
25051 }
25052
25053 if (source.type() != target.type())
25054 {
25055 // different types: replace value
25056 result.push_back(
25057 {
25058 {"op", "replace"}, {"path", path}, {"value", target}
25059 });
25060 return result;
25061 }
25062
25063 switch (source.type())
25064 {
25065 case value_t::array:
25066 {
25067 // first pass: traverse common elements
25068 std::size_t i = 0;
25069 while (i < source.size() && i < target.size())
25070 {
25071 // recursive call to compare array values at index i
25072 auto temp_diff = diff(source[i], target[i], detail::concat<string_t>(path, '/', detail::to_string<string_t>(i)));
25073 result.insert(result.end(), temp_diff.begin(), temp_diff.end());
25074 ++i;
25075 }
25076
25077 // We now reached the end of at least one array
25078 // in a second pass, traverse the remaining elements
25079
25080 // remove my remaining elements
25081 const auto end_index = static_cast<difference_type>(result.size());
25082 while (i < source.size())
25083 {
25084 // add operations in reverse order to avoid invalid
25085 // indices
25086 result.insert(result.begin() + end_index, object(
25087 {
25088 {"op", "remove"},
25089 {"path", detail::concat<string_t>(path, '/', detail::to_string<string_t>(i))}
25090 }));
25091 ++i;
25092 }
25093
25094 // add other remaining elements
25095 while (i < target.size())
25096 {
25097 result.push_back(
25098 {
25099 {"op", "add"},
25100 {"path", detail::concat<string_t>(path, "/-")},
25101 {"value", target[i]}
25102 });
25103 ++i;
25104 }
25105
25106 break;
25107 }
25108
25109 case value_t::object:
25110 {
25111 // first pass: traverse this object's elements
25112 for (auto it = source.cbegin(); it != source.cend(); ++it)
25113 {
25114 // escape the key name to be used in a JSON patch
25115 const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
25116
25117 if (target.find(it.key()) != target.end())
25118 {
25119 // recursive call to compare object values at key it
25120 auto temp_diff = diff(it.value(), target[it.key()], path_key);
25121 result.insert(result.end(), temp_diff.begin(), temp_diff.end());
25122 }
25123 else
25124 {
25125 // found a key that is not in o -> remove it
25126 result.push_back(object(
25127 {
25128 {"op", "remove"}, {"path", path_key}
25129 }));
25130 }
25131 }
25132
25133 // second pass: traverse other object's elements
25134 for (auto it = target.cbegin(); it != target.cend(); ++it)
25135 {
25136 if (source.find(it.key()) == source.end())
25137 {
25138 // found a key that is not in this -> add it
25139 const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
25140 result.push_back(
25141 {
25142 {"op", "add"}, {"path", path_key},
25143 {"value", it.value()}
25144 });
25145 }
25146 }
25147
25148 break;
25149 }
25150
25151 case value_t::null:
25152 case value_t::string:
25153 case value_t::boolean:
25154 case value_t::number_integer:
25155 case value_t::number_unsigned:
25156 case value_t::number_float:
25157 case value_t::binary:
25158 case value_t::discarded:
25159 default:
25160 {
25161 // both primitive type: replace value
25162 result.push_back(
25163 {
25164 {"op", "replace"}, {"path", path}, {"value", target}
25165 });
25166 break;
25167 }
25168 }
25169
25170 return result;
25171 }
25172 /// @}
25173
25174 ////////////////////////////////
25175 // JSON Merge Patch functions //
25176 ////////////////////////////////
25177
25178 /// @name JSON Merge Patch functions
25179 /// @{
25180
25181 /// @brief applies a JSON Merge Patch
25182 /// @sa https://json.nlohmann.me/api/basic_json/merge_patch/
25183 void merge_patch(const basic_json& apply_patch)
25184 {
25185 if (apply_patch.is_object())
25186 {
25187 if (!is_object())
25188 {
25189 *this = object();
25190 }
25191 for (auto it = apply_patch.begin(); it != apply_patch.end(); ++it)
25192 {
25193 if (it.value().is_null())
25194 {
25195 erase(it.key());
25196 }
25197 else
25198 {
25199 operator[](it.key()).merge_patch(it.value());
25200 }
25201 }
25202 }
25203 else
25204 {
25205 *this = apply_patch;
25206 }
25207 }
25208
25209 /// @}
25210 };
25211
25212 /// @brief user-defined to_string function for JSON values
25213 /// @sa https://json.nlohmann.me/api/basic_json/to_string/
25214 NLOHMANN_BASIC_JSON_TPL_DECLARATION
25215 std::string to_string(const NLOHMANN_BASIC_JSON_TPL& j)
25216 {
25217 return j.dump();
25218 }
25219
25220 inline namespace literals
25221 {
25222 inline namespace json_literals
25223 {
25224
25225 /// @brief user-defined string literal for JSON values
25226 /// @sa https://json.nlohmann.me/api/basic_json/operator_literal_json/
25227 JSON_HEDLEY_NON_NULL(1)
25228 #if !defined(JSON_HEDLEY_GCC_VERSION) || JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0)
25229 inline nlohmann::json operator ""_json(const char* s, std::size_t n)
25230 #else
25231 inline nlohmann::json operator "" _json(const char* s, std::size_t n)
25232 #endif
25233 {
25234 return nlohmann::json::parse(s, s + n);
25235 }
25236
25237 /// @brief user-defined string literal for JSON pointer
25238 /// @sa https://json.nlohmann.me/api/basic_json/operator_literal_json_pointer/
25239 JSON_HEDLEY_NON_NULL(1)
25240 #if !defined(JSON_HEDLEY_GCC_VERSION) || JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0)
25241 inline nlohmann::json::json_pointer operator ""_json_pointer(const char* s, std::size_t n)
25242 #else
25243 inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std::size_t n)
25244 #endif
25245 {
25246 return nlohmann::json::json_pointer(std::string(s, n));
25247 }
25248
25249 } // namespace json_literals
25250 } // namespace literals
25251 NLOHMANN_JSON_NAMESPACE_END
25252
25253 ///////////////////////
25254 // nonmember support //
25255 ///////////////////////
25256
25257 namespace std // NOLINT(cert-dcl58-cpp)
25258 {
25259
25260 /// @brief hash value for JSON objects
25261 /// @sa https://json.nlohmann.me/api/basic_json/std_hash/
25262 NLOHMANN_BASIC_JSON_TPL_DECLARATION
25263 struct hash<nlohmann::NLOHMANN_BASIC_JSON_TPL> // NOLINT(cert-dcl58-cpp)
25264 {
25265 std::size_t operator()(const nlohmann::NLOHMANN_BASIC_JSON_TPL& j) const
25266 {
25267 return nlohmann::detail::hash(j);
25268 }
25269 };
25270
25271 // specialization for std::less<value_t>
25272 template<>
25273 struct less< ::nlohmann::detail::value_t> // do not remove the space after '<', see https://github.com/nlohmann/json/pull/679
25274 {
25275 /*!
25276 @brief compare two value_t enum values
25277 @since version 3.0.0
25278 */
25279 bool operator()(::nlohmann::detail::value_t lhs,
25280 ::nlohmann::detail::value_t rhs) const noexcept
25281 {
25282 #if JSON_HAS_THREE_WAY_COMPARISON
25283 return std::is_lt(lhs <=> rhs); // *NOPAD*
25284 #else
25285 return ::nlohmann::detail::operator<(lhs, rhs);
25286 #endif
25287 }
25288 };
25289
25290 // C++20 prohibit function specialization in the std namespace.
25291 #ifndef JSON_HAS_CPP_20
25292
25293 /// @brief exchanges the values of two JSON objects
25294 /// @sa https://json.nlohmann.me/api/basic_json/std_swap/
25295 NLOHMANN_BASIC_JSON_TPL_DECLARATION
25296 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)
25297 is_nothrow_move_constructible<nlohmann::NLOHMANN_BASIC_JSON_TPL>::value&& // NOLINT(misc-redundant-expression,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
25298 is_nothrow_move_assignable<nlohmann::NLOHMANN_BASIC_JSON_TPL>::value)
25299 {
25300 j1.swap(j2);
25301 }
25302
25303 #endif
25304
25305 } // namespace std
25306
25307 #if JSON_USE_GLOBAL_UDLS
25308 #if !defined(JSON_HEDLEY_GCC_VERSION) || JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0)
25309 using nlohmann::literals::json_literals::operator ""_json; // NOLINT(misc-unused-using-decls,google-global-names-in-headers)
25310 using nlohmann::literals::json_literals::operator ""_json_pointer; //NOLINT(misc-unused-using-decls,google-global-names-in-headers)
25311 #else
25312 using nlohmann::literals::json_literals::operator "" _json; // NOLINT(misc-unused-using-decls,google-global-names-in-headers)
25313 using nlohmann::literals::json_literals::operator "" _json_pointer; //NOLINT(misc-unused-using-decls,google-global-names-in-headers)
25314 #endif
25315 #endif
25316
25317 // #include <nlohmann/detail/macro_unscope.hpp>
25318 // __ _____ _____ _____
25319 // __| | __| | | | JSON for Modern C++
25320 // | | |__ | | | | | | version 3.12.0
25321 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
25322 //
25323 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
25324 // SPDX-License-Identifier: MIT
25325
25326
25327
25328 // restore clang diagnostic settings
25329 #if defined(__clang__)
25330 #pragma clang diagnostic pop
25331 #endif
25332
25333 // clean up
25334 #undef JSON_ASSERT
25335 #undef JSON_INTERNAL_CATCH
25336 #undef JSON_THROW
25337 #undef JSON_PRIVATE_UNLESS_TESTED
25338 #undef NLOHMANN_BASIC_JSON_TPL_DECLARATION
25339 #undef NLOHMANN_BASIC_JSON_TPL
25340 #undef JSON_EXPLICIT
25341 #undef NLOHMANN_CAN_CALL_STD_FUNC_IMPL
25342 #undef JSON_INLINE_VARIABLE
25343 #undef JSON_NO_UNIQUE_ADDRESS
25344 #undef JSON_DISABLE_ENUM_SERIALIZATION
25345 #undef JSON_USE_GLOBAL_UDLS
25346
25347 #ifndef JSON_TEST_KEEP_MACROS
25348 #undef JSON_CATCH
25349 #undef JSON_TRY
25350 #undef JSON_HAS_CPP_11
25351 #undef JSON_HAS_CPP_14
25352 #undef JSON_HAS_CPP_17
25353 #undef JSON_HAS_CPP_20
25354 #undef JSON_HAS_CPP_23
25355 #undef JSON_HAS_FILESYSTEM
25356 #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
25357 #undef JSON_HAS_THREE_WAY_COMPARISON
25358 #undef JSON_HAS_RANGES
25359 #undef JSON_HAS_STATIC_RTTI
25360 #undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
25361 #endif
25362
25363 // #include <nlohmann/thirdparty/hedley/hedley_undef.hpp>
25364 // __ _____ _____ _____
25365 // __| | __| | | | JSON for Modern C++
25366 // | | |__ | | | | | | version 3.12.0
25367 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
25368 //
25369 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
25370 // SPDX-License-Identifier: MIT
25371
25372
25373
25374 #undef JSON_HEDLEY_ALWAYS_INLINE
25375 #undef JSON_HEDLEY_ARM_VERSION
25376 #undef JSON_HEDLEY_ARM_VERSION_CHECK
25377 #undef JSON_HEDLEY_ARRAY_PARAM
25378 #undef JSON_HEDLEY_ASSUME
25379 #undef JSON_HEDLEY_BEGIN_C_DECLS
25380 #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE
25381 #undef JSON_HEDLEY_CLANG_HAS_BUILTIN
25382 #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE
25383 #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE
25384 #undef JSON_HEDLEY_CLANG_HAS_EXTENSION
25385 #undef JSON_HEDLEY_CLANG_HAS_FEATURE
25386 #undef JSON_HEDLEY_CLANG_HAS_WARNING
25387 #undef JSON_HEDLEY_COMPCERT_VERSION
25388 #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK
25389 #undef JSON_HEDLEY_CONCAT
25390 #undef JSON_HEDLEY_CONCAT3
25391 #undef JSON_HEDLEY_CONCAT3_EX
25392 #undef JSON_HEDLEY_CONCAT_EX
25393 #undef JSON_HEDLEY_CONST
25394 #undef JSON_HEDLEY_CONSTEXPR
25395 #undef JSON_HEDLEY_CONST_CAST
25396 #undef JSON_HEDLEY_CPP_CAST
25397 #undef JSON_HEDLEY_CRAY_VERSION
25398 #undef JSON_HEDLEY_CRAY_VERSION_CHECK
25399 #undef JSON_HEDLEY_C_DECL
25400 #undef JSON_HEDLEY_DEPRECATED
25401 #undef JSON_HEDLEY_DEPRECATED_FOR
25402 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
25403 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_
25404 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
25405 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
25406 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
25407 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
25408 #undef JSON_HEDLEY_DIAGNOSTIC_POP
25409 #undef JSON_HEDLEY_DIAGNOSTIC_PUSH
25410 #undef JSON_HEDLEY_DMC_VERSION
25411 #undef JSON_HEDLEY_DMC_VERSION_CHECK
25412 #undef JSON_HEDLEY_EMPTY_BASES
25413 #undef JSON_HEDLEY_EMSCRIPTEN_VERSION
25414 #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK
25415 #undef JSON_HEDLEY_END_C_DECLS
25416 #undef JSON_HEDLEY_FLAGS
25417 #undef JSON_HEDLEY_FLAGS_CAST
25418 #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE
25419 #undef JSON_HEDLEY_GCC_HAS_BUILTIN
25420 #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE
25421 #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE
25422 #undef JSON_HEDLEY_GCC_HAS_EXTENSION
25423 #undef JSON_HEDLEY_GCC_HAS_FEATURE
25424 #undef JSON_HEDLEY_GCC_HAS_WARNING
25425 #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK
25426 #undef JSON_HEDLEY_GCC_VERSION
25427 #undef JSON_HEDLEY_GCC_VERSION_CHECK
25428 #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE
25429 #undef JSON_HEDLEY_GNUC_HAS_BUILTIN
25430 #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE
25431 #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE
25432 #undef JSON_HEDLEY_GNUC_HAS_EXTENSION
25433 #undef JSON_HEDLEY_GNUC_HAS_FEATURE
25434 #undef JSON_HEDLEY_GNUC_HAS_WARNING
25435 #undef JSON_HEDLEY_GNUC_VERSION
25436 #undef JSON_HEDLEY_GNUC_VERSION_CHECK
25437 #undef JSON_HEDLEY_HAS_ATTRIBUTE
25438 #undef JSON_HEDLEY_HAS_BUILTIN
25439 #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE
25440 #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS
25441 #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE
25442 #undef JSON_HEDLEY_HAS_EXTENSION
25443 #undef JSON_HEDLEY_HAS_FEATURE
25444 #undef JSON_HEDLEY_HAS_WARNING
25445 #undef JSON_HEDLEY_IAR_VERSION
25446 #undef JSON_HEDLEY_IAR_VERSION_CHECK
25447 #undef JSON_HEDLEY_IBM_VERSION
25448 #undef JSON_HEDLEY_IBM_VERSION_CHECK
25449 #undef JSON_HEDLEY_IMPORT
25450 #undef JSON_HEDLEY_INLINE
25451 #undef JSON_HEDLEY_INTEL_CL_VERSION
25452 #undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK
25453 #undef JSON_HEDLEY_INTEL_VERSION
25454 #undef JSON_HEDLEY_INTEL_VERSION_CHECK
25455 #undef JSON_HEDLEY_IS_CONSTANT
25456 #undef JSON_HEDLEY_IS_CONSTEXPR_
25457 #undef JSON_HEDLEY_LIKELY
25458 #undef JSON_HEDLEY_MALLOC
25459 #undef JSON_HEDLEY_MCST_LCC_VERSION
25460 #undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK
25461 #undef JSON_HEDLEY_MESSAGE
25462 #undef JSON_HEDLEY_MSVC_VERSION
25463 #undef JSON_HEDLEY_MSVC_VERSION_CHECK
25464 #undef JSON_HEDLEY_NEVER_INLINE
25465 #undef JSON_HEDLEY_NON_NULL
25466 #undef JSON_HEDLEY_NO_ESCAPE
25467 #undef JSON_HEDLEY_NO_RETURN
25468 #undef JSON_HEDLEY_NO_THROW
25469 #undef JSON_HEDLEY_NULL
25470 #undef JSON_HEDLEY_PELLES_VERSION
25471 #undef JSON_HEDLEY_PELLES_VERSION_CHECK
25472 #undef JSON_HEDLEY_PGI_VERSION
25473 #undef JSON_HEDLEY_PGI_VERSION_CHECK
25474 #undef JSON_HEDLEY_PREDICT
25475 #undef JSON_HEDLEY_PRINTF_FORMAT
25476 #undef JSON_HEDLEY_PRIVATE
25477 #undef JSON_HEDLEY_PUBLIC
25478 #undef JSON_HEDLEY_PURE
25479 #undef JSON_HEDLEY_REINTERPRET_CAST
25480 #undef JSON_HEDLEY_REQUIRE
25481 #undef JSON_HEDLEY_REQUIRE_CONSTEXPR
25482 #undef JSON_HEDLEY_REQUIRE_MSG
25483 #undef JSON_HEDLEY_RESTRICT
25484 #undef JSON_HEDLEY_RETURNS_NON_NULL
25485 #undef JSON_HEDLEY_SENTINEL
25486 #undef JSON_HEDLEY_STATIC_ASSERT
25487 #undef JSON_HEDLEY_STATIC_CAST
25488 #undef JSON_HEDLEY_STRINGIFY
25489 #undef JSON_HEDLEY_STRINGIFY_EX
25490 #undef JSON_HEDLEY_SUNPRO_VERSION
25491 #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK
25492 #undef JSON_HEDLEY_TINYC_VERSION
25493 #undef JSON_HEDLEY_TINYC_VERSION_CHECK
25494 #undef JSON_HEDLEY_TI_ARMCL_VERSION
25495 #undef JSON_HEDLEY_TI_ARMCL_VERSION_CHECK
25496 #undef JSON_HEDLEY_TI_CL2000_VERSION
25497 #undef JSON_HEDLEY_TI_CL2000_VERSION_CHECK
25498 #undef JSON_HEDLEY_TI_CL430_VERSION
25499 #undef JSON_HEDLEY_TI_CL430_VERSION_CHECK
25500 #undef JSON_HEDLEY_TI_CL6X_VERSION
25501 #undef JSON_HEDLEY_TI_CL6X_VERSION_CHECK
25502 #undef JSON_HEDLEY_TI_CL7X_VERSION
25503 #undef JSON_HEDLEY_TI_CL7X_VERSION_CHECK
25504 #undef JSON_HEDLEY_TI_CLPRU_VERSION
25505 #undef JSON_HEDLEY_TI_CLPRU_VERSION_CHECK
25506 #undef JSON_HEDLEY_TI_VERSION
25507 #undef JSON_HEDLEY_TI_VERSION_CHECK
25508 #undef JSON_HEDLEY_UNAVAILABLE
25509 #undef JSON_HEDLEY_UNLIKELY
25510 #undef JSON_HEDLEY_UNPREDICTABLE
25511 #undef JSON_HEDLEY_UNREACHABLE
25512 #undef JSON_HEDLEY_UNREACHABLE_RETURN
25513 #undef JSON_HEDLEY_VERSION
25514 #undef JSON_HEDLEY_VERSION_DECODE_MAJOR
25515 #undef JSON_HEDLEY_VERSION_DECODE_MINOR
25516 #undef JSON_HEDLEY_VERSION_DECODE_REVISION
25517 #undef JSON_HEDLEY_VERSION_ENCODE
25518 #undef JSON_HEDLEY_WARNING
25519 #undef JSON_HEDLEY_WARN_UNUSED_RESULT
25520 #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG
25521 #undef JSON_HEDLEY_FALL_THROUGH
25522
25523
25524
25525 #endif // INCLUDE_NLOHMANN_JSON_HPP_