Debian Codegraph @

single_include/nlohmann/json.hpp

raw symbols ◀ ▶
Source reconstructed at patch state 3 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 #if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
4823 #include <string_view> // u8string_view
4824 #endif
4825
4826 NLOHMANN_JSON_NAMESPACE_BEGIN
4827 namespace detail
4828 {
4829
4830 template<typename BasicJsonType>
4831 inline void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
4832 {
4833 if (JSON_HEDLEY_UNLIKELY(!j.is_null()))
4834 {
4835 JSON_THROW(type_error::create(302, concat("type must be null, but is ", j.type_name()), &j));
4836 }
4837 n = nullptr;
4838 }
4839
4840 #ifdef JSON_HAS_CPP_17
4841 template<typename BasicJsonType, typename T>
4842 void from_json(const BasicJsonType& j, std::optional<T>& opt)
4843 {
4844 if (j.is_null())
4845 {
4846 opt = std::nullopt;
4847 }
4848 else
4849 {
4850 opt.emplace(j.template get<T>());
4851 }
4852 }
4853 #endif // JSON_HAS_CPP_17
4854
4855 // overloads for basic_json template parameters
4856 template < typename BasicJsonType, typename ArithmeticType,
4857 enable_if_t < std::is_arithmetic<ArithmeticType>::value&&
4858 !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
4859 int > = 0 >
4860 void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
4861 {
4862 switch (static_cast<value_t>(j))
4863 {
4864 case value_t::number_unsigned:
4865 {
4866 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
4867 break;
4868 }
4869 case value_t::number_integer:
4870 {
4871 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
4872 break;
4873 }
4874 case value_t::number_float:
4875 {
4876 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
4877 break;
4878 }
4879
4880 case value_t::null:
4881 case value_t::object:
4882 case value_t::array:
4883 case value_t::string:
4884 case value_t::boolean:
4885 case value_t::binary:
4886 case value_t::discarded:
4887 default:
4888 JSON_THROW(type_error::create(302, concat("type must be number, but is ", j.type_name()), &j));
4889 }
4890 }
4891
4892 template<typename BasicJsonType>
4893 inline void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
4894 {
4895 if (JSON_HEDLEY_UNLIKELY(!j.is_boolean()))
4896 {
4897 JSON_THROW(type_error::create(302, concat("type must be boolean, but is ", j.type_name()), &j));
4898 }
4899 b = *j.template get_ptr<const typename BasicJsonType::boolean_t*>();
4900 }
4901
4902 template<typename BasicJsonType>
4903 inline void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
4904 {
4905 if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
4906 {
4907 JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
4908 }
4909 s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
4910 }
4911
4912 template <
4913 typename BasicJsonType, typename StringType,
4914 enable_if_t <
4915 std::is_assignable<StringType&, const typename BasicJsonType::string_t>::value
4916 && is_detected_exact<typename BasicJsonType::string_t::value_type, value_type_t, StringType>::value
4917 && !std::is_same<typename BasicJsonType::string_t, StringType>::value
4918 && !is_json_ref<StringType>::value, int > = 0 >
4919 inline void from_json(const BasicJsonType& j, StringType& s)
4920 {
4921 if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
4922 {
4923 JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
4924 }
4925
4926 s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
4927 }
4928
4929 template<typename BasicJsonType>
4930 inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val)
4931 {
4932 get_arithmetic_value(j, val);
4933 }
4934
4935 template<typename BasicJsonType>
4936 inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val)
4937 {
4938 get_arithmetic_value(j, val);
4939 }
4940
4941 template<typename BasicJsonType>
4942 inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val)
4943 {
4944 get_arithmetic_value(j, val);
4945 }
4946
4947 #if !JSON_DISABLE_ENUM_SERIALIZATION
4948 template<typename BasicJsonType, typename EnumType,
4949 enable_if_t<std::is_enum<EnumType>::value, int> = 0>
4950 inline void from_json(const BasicJsonType& j, EnumType& e)
4951 {
4952 typename std::underlying_type<EnumType>::type val;
4953 get_arithmetic_value(j, val);
4954 e = static_cast<EnumType>(val);
4955 }
4956 #endif // JSON_DISABLE_ENUM_SERIALIZATION
4957
4958 // forward_list doesn't have an insert method
4959 template<typename BasicJsonType, typename T, typename Allocator,
4960 enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
4961 inline void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
4962 {
4963 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
4964 {
4965 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
4966 }
4967 l.clear();
4968 std::transform(j.rbegin(), j.rend(),
4969 std::front_inserter(l), [](const BasicJsonType & i)
4970 {
4971 return i.template get<T>();
4972 });
4973 }
4974
4975 // valarray doesn't have an insert method
4976 template<typename BasicJsonType, typename T,
4977 enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
4978 inline void from_json(const BasicJsonType& j, std::valarray<T>& l)
4979 {
4980 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
4981 {
4982 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
4983 }
4984 l.resize(j.size());
4985 std::transform(j.begin(), j.end(), std::begin(l),
4986 [](const BasicJsonType & elem)
4987 {
4988 return elem.template get<T>();
4989 });
4990 }
4991
4992 template<typename BasicJsonType, typename T, std::size_t N>
4993 auto from_json(const BasicJsonType& j, T (&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
4994 -> decltype(j.template get<T>(), void())
4995 {
4996 for (std::size_t i = 0; i < N; ++i)
4997 {
4998 arr[i] = j.at(i).template get<T>();
4999 }
5000 }
5001
5002 template<typename BasicJsonType, typename T, std::size_t N1, std::size_t N2>
5003 auto from_json(const BasicJsonType& j, T (&arr)[N1][N2]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
5004 -> decltype(j.template get<T>(), void())
5005 {
5006 for (std::size_t i1 = 0; i1 < N1; ++i1)
5007 {
5008 for (std::size_t i2 = 0; i2 < N2; ++i2)
5009 {
5010 arr[i1][i2] = j.at(i1).at(i2).template get<T>();
5011 }
5012 }
5013 }
5014
5015 template<typename BasicJsonType, typename T, std::size_t N1, std::size_t N2, std::size_t N3>
5016 auto from_json(const BasicJsonType& j, T (&arr)[N1][N2][N3]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
5017 -> decltype(j.template get<T>(), void())
5018 {
5019 for (std::size_t i1 = 0; i1 < N1; ++i1)
5020 {
5021 for (std::size_t i2 = 0; i2 < N2; ++i2)
5022 {
5023 for (std::size_t i3 = 0; i3 < N3; ++i3)
5024 {
5025 arr[i1][i2][i3] = j.at(i1).at(i2).at(i3).template get<T>();
5026 }
5027 }
5028 }
5029 }
5030
5031 template<typename BasicJsonType, typename T, std::size_t N1, std::size_t N2, std::size_t N3, std::size_t N4>
5032 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)
5033 -> decltype(j.template get<T>(), void())
5034 {
5035 for (std::size_t i1 = 0; i1 < N1; ++i1)
5036 {
5037 for (std::size_t i2 = 0; i2 < N2; ++i2)
5038 {
5039 for (std::size_t i3 = 0; i3 < N3; ++i3)
5040 {
5041 for (std::size_t i4 = 0; i4 < N4; ++i4)
5042 {
5043 arr[i1][i2][i3][i4] = j.at(i1).at(i2).at(i3).at(i4).template get<T>();
5044 }
5045 }
5046 }
5047 }
5048 }
5049
5050 template<typename BasicJsonType>
5051 inline void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType::array_t& arr, priority_tag<3> /*unused*/)
5052 {
5053 arr = *j.template get_ptr<const typename BasicJsonType::array_t*>();
5054 }
5055
5056 template<typename BasicJsonType, typename T, std::size_t N>
5057 auto from_json_array_impl(const BasicJsonType& j, std::array<T, N>& arr,
5058 priority_tag<2> /*unused*/)
5059 -> decltype(j.template get<T>(), void())
5060 {
5061 for (std::size_t i = 0; i < N; ++i)
5062 {
5063 arr[i] = j.at(i).template get<T>();
5064 }
5065 }
5066
5067 template<typename BasicJsonType, typename ConstructibleArrayType,
5068 enable_if_t<
5069 std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
5070 int> = 0>
5071 auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<1> /*unused*/)
5072 -> decltype(
5073 arr.reserve(std::declval<typename ConstructibleArrayType::size_type>()),
5074 j.template get<typename ConstructibleArrayType::value_type>(),
5075 void())
5076 {
5077 using std::end;
5078
5079 ConstructibleArrayType ret;
5080 ret.reserve(j.size());
5081 std::transform(j.begin(), j.end(),
5082 std::inserter(ret, end(ret)), [](const BasicJsonType & i)
5083 {
5084 // get<BasicJsonType>() returns *this, this won't call a from_json
5085 // method when value_type is BasicJsonType
5086 return i.template get<typename ConstructibleArrayType::value_type>();
5087 });
5088 arr = std::move(ret);
5089 }
5090
5091 template<typename BasicJsonType, typename ConstructibleArrayType,
5092 enable_if_t<
5093 std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
5094 int> = 0>
5095 inline void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr,
5096 priority_tag<0> /*unused*/)
5097 {
5098 using std::end;
5099
5100 ConstructibleArrayType ret;
5101 std::transform(
5102 j.begin(), j.end(), std::inserter(ret, end(ret)),
5103 [](const BasicJsonType & i)
5104 {
5105 // get<BasicJsonType>() returns *this, this won't call a from_json
5106 // method when value_type is BasicJsonType
5107 return i.template get<typename ConstructibleArrayType::value_type>();
5108 });
5109 arr = std::move(ret);
5110 }
5111
5112 template < typename BasicJsonType, typename ConstructibleArrayType,
5113 enable_if_t <
5114 is_constructible_array_type<BasicJsonType, ConstructibleArrayType>::value&&
5115 !is_constructible_object_type<BasicJsonType, ConstructibleArrayType>::value&&
5116 !is_constructible_string_type<BasicJsonType, ConstructibleArrayType>::value&&
5117 !std::is_same<ConstructibleArrayType, typename BasicJsonType::binary_t>::value&&
5118 !is_basic_json<ConstructibleArrayType>::value,
5119 int > = 0 >
5120 auto from_json(const BasicJsonType& j, ConstructibleArrayType& arr)
5121 -> decltype(from_json_array_impl(j, arr, priority_tag<3> {}),
5122 j.template get<typename ConstructibleArrayType::value_type>(),
5123 void())
5124 {
5125 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
5126 {
5127 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
5128 }
5129
5130 from_json_array_impl(j, arr, priority_tag<3> {});
5131 }
5132
5133 template < typename BasicJsonType, typename T, std::size_t... Idx >
5134 std::array<T, sizeof...(Idx)> from_json_inplace_array_impl(BasicJsonType&& j,
5135 identity_tag<std::array<T, sizeof...(Idx)>> /*unused*/, index_sequence<Idx...> /*unused*/)
5136 {
5137 return { { std::forward<BasicJsonType>(j).at(Idx).template get<T>()... } };
5138 }
5139
5140 template < typename BasicJsonType, typename T, std::size_t N >
5141 auto from_json(BasicJsonType&& j, identity_tag<std::array<T, N>> tag)
5142 -> decltype(from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {}))
5143 {
5144 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
5145 {
5146 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
5147 }
5148
5149 return from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {});
5150 }
5151
5152 template<typename BasicJsonType>
5153 inline void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin)
5154 {
5155 if (JSON_HEDLEY_UNLIKELY(!j.is_binary()))
5156 {
5157 JSON_THROW(type_error::create(302, concat("type must be binary, but is ", j.type_name()), &j));
5158 }
5159
5160 bin = *j.template get_ptr<const typename BasicJsonType::binary_t*>();
5161 }
5162
5163 template<typename BasicJsonType, typename ConstructibleObjectType,
5164 enable_if_t<is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value, int> = 0>
5165 inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
5166 {
5167 if (JSON_HEDLEY_UNLIKELY(!j.is_object()))
5168 {
5169 JSON_THROW(type_error::create(302, concat("type must be object, but is ", j.type_name()), &j));
5170 }
5171
5172 ConstructibleObjectType ret;
5173 const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
5174 using value_type = typename ConstructibleObjectType::value_type;
5175 std::transform(
5176 inner_object->begin(), inner_object->end(),
5177 std::inserter(ret, ret.begin()),
5178 [](typename BasicJsonType::object_t::value_type const & p)
5179 {
5180 return value_type(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
5181 });
5182 obj = std::move(ret);
5183 }
5184
5185 // overload for arithmetic types, not chosen for basic_json template arguments
5186 // (BooleanType, etc..); note: Is it really necessary to provide explicit
5187 // overloads for boolean_t etc. in case of a custom BooleanType which is not
5188 // an arithmetic type?
5189 template < typename BasicJsonType, typename ArithmeticType,
5190 enable_if_t <
5191 std::is_arithmetic<ArithmeticType>::value&&
5192 !std::is_same<ArithmeticType, typename BasicJsonType::number_unsigned_t>::value&&
5193 !std::is_same<ArithmeticType, typename BasicJsonType::number_integer_t>::value&&
5194 !std::is_same<ArithmeticType, typename BasicJsonType::number_float_t>::value&&
5195 !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
5196 int > = 0 >
5197 inline void from_json(const BasicJsonType& j, ArithmeticType& val)
5198 {
5199 switch (static_cast<value_t>(j))
5200 {
5201 case value_t::number_unsigned:
5202 {
5203 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
5204 break;
5205 }
5206 case value_t::number_integer:
5207 {
5208 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
5209 break;
5210 }
5211 case value_t::number_float:
5212 {
5213 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
5214 break;
5215 }
5216 case value_t::boolean:
5217 {
5218 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
5219 break;
5220 }
5221
5222 case value_t::null:
5223 case value_t::object:
5224 case value_t::array:
5225 case value_t::string:
5226 case value_t::binary:
5227 case value_t::discarded:
5228 default:
5229 JSON_THROW(type_error::create(302, concat("type must be number, but is ", j.type_name()), &j));
5230 }
5231 }
5232
5233 template<typename BasicJsonType, typename... Args, std::size_t... Idx>
5234 std::tuple<Args...> from_json_tuple_impl_base(BasicJsonType&& j, index_sequence<Idx...> /*unused*/)
5235 {
5236 return std::make_tuple(std::forward<BasicJsonType>(j).at(Idx).template get<Args>()...);
5237 }
5238
5239 template<typename BasicJsonType>
5240 std::tuple<> from_json_tuple_impl_base(BasicJsonType& /*unused*/, index_sequence<> /*unused*/)
5241 {
5242 return {};
5243 }
5244
5245 template < typename BasicJsonType, class A1, class A2 >
5246 std::pair<A1, A2> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::pair<A1, A2>> /*unused*/, priority_tag<0> /*unused*/)
5247 {
5248 return {std::forward<BasicJsonType>(j).at(0).template get<A1>(),
5249 std::forward<BasicJsonType>(j).at(1).template get<A2>()};
5250 }
5251
5252 template<typename BasicJsonType, typename A1, typename A2>
5253 inline void from_json_tuple_impl(BasicJsonType&& j, std::pair<A1, A2>& p, priority_tag<1> /*unused*/)
5254 {
5255 p = from_json_tuple_impl(std::forward<BasicJsonType>(j), identity_tag<std::pair<A1, A2>> {}, priority_tag<0> {});
5256 }
5257
5258 template<typename BasicJsonType, typename... Args>
5259 std::tuple<Args...> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::tuple<Args...>> /*unused*/, priority_tag<2> /*unused*/)
5260 {
5261 return from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
5262 }
5263
5264 template<typename BasicJsonType, typename... Args>
5265 inline void from_json_tuple_impl(BasicJsonType&& j, std::tuple<Args...>& t, priority_tag<3> /*unused*/)
5266 {
5267 t = from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
5268 }
5269
5270 template<typename BasicJsonType, typename TupleRelated>
5271 auto from_json(BasicJsonType&& j, TupleRelated&& t)
5272 -> decltype(from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {}))
5273 {
5274 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
5275 {
5276 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
5277 }
5278
5279 return from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {});
5280 }
5281
5282 template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator,
5283 typename = enable_if_t < !std::is_constructible <
5284 typename BasicJsonType::string_t, Key >::value >>
5285 inline void from_json(const BasicJsonType& j, std::map<Key, Value, Compare, Allocator>& m)
5286 {
5287 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
5288 {
5289 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
5290 }
5291 m.clear();
5292 for (const auto& p : j)
5293 {
5294 if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
5295 {
5296 JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
5297 }
5298 m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
5299 }
5300 }
5301
5302 template < typename BasicJsonType, typename Key, typename Value, typename Hash, typename KeyEqual, typename Allocator,
5303 typename = enable_if_t < !std::is_constructible <
5304 typename BasicJsonType::string_t, Key >::value >>
5305 inline void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>& m)
5306 {
5307 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
5308 {
5309 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
5310 }
5311 m.clear();
5312 for (const auto& p : j)
5313 {
5314 if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
5315 {
5316 JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
5317 }
5318 m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
5319 }
5320 }
5321
5322 #if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
5323 template<typename BasicJsonType>
5324 inline void from_json(const BasicJsonType& j, std_fs::path& p)
5325 {
5326 if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
5327 {
5328 JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
5329 }
5330 const auto& s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
5331 // Checking for C++20 standard or later can be insufficient in case the
5332 // library support for char8_t is either incomplete or was disabled
5333 // altogether. Use the __cpp_lib_char8_t feature test instead.
5334 #if defined(__cpp_lib_char8_t) && (__cpp_lib_char8_t >= 201907L)
5335 p = std_fs::path(std::u8string_view(reinterpret_cast<const char8_t*>(s.data()), s.size()));
5336 #else
5337 p = std_fs::u8path(s); // accepts UTF-8 encoded std::string in C++17, deprecated in C++20
5338 #endif
5339 }
5340 #endif
5341
5342 struct from_json_fn
5343 {
5344 template<typename BasicJsonType, typename T>
5345 auto operator()(const BasicJsonType& j, T&& val) const
5346 noexcept(noexcept(from_json(j, std::forward<T>(val))))
5347 -> decltype(from_json(j, std::forward<T>(val)))
5348 {
5349 return from_json(j, std::forward<T>(val));
5350 }
5351 };
5352
5353 } // namespace detail
5354
5355 #ifndef JSON_HAS_CPP_17
5356 /// namespace to hold default `from_json` function
5357 /// to see why this is required:
5358 /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
5359 namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
5360 {
5361 #endif
5362 JSON_INLINE_VARIABLE constexpr const auto& from_json = // NOLINT(misc-definitions-in-headers)
5363 detail::static_const<detail::from_json_fn>::value;
5364 #ifndef JSON_HAS_CPP_17
5365 } // namespace
5366 #endif
5367
5368 NLOHMANN_JSON_NAMESPACE_END
5369
5370 // #include <nlohmann/detail/conversions/to_json.hpp>
5371 // __ _____ _____ _____
5372 // __| | __| | | | JSON for Modern C++
5373 // | | |__ | | | | | | version 3.12.0
5374 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
5375 //
5376 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
5377 // SPDX-License-Identifier: MIT
5378
5379
5380
5381 // #include <nlohmann/detail/macro_scope.hpp>
5382 // JSON_HAS_CPP_17
5383 #ifdef JSON_HAS_CPP_17
5384 #include <optional> // optional
5385 #endif
5386
5387 #include <algorithm> // copy
5388 #include <iterator> // begin, end
5389 #include <memory> // allocator_traits
5390 #include <string> // basic_string, char_traits
5391 #include <tuple> // tuple, get
5392 #include <type_traits> // is_same, is_constructible, is_floating_point, is_enum, underlying_type
5393 #include <utility> // move, forward, declval, pair
5394 #include <valarray> // valarray
5395 #include <vector> // vector
5396
5397 // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
5398 // __ _____ _____ _____
5399 // __| | __| | | | JSON for Modern C++
5400 // | | |__ | | | | | | version 3.12.0
5401 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
5402 //
5403 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
5404 // SPDX-License-Identifier: MIT
5405
5406
5407
5408 #include <cstddef> // size_t
5409 #include <iterator> // forward_iterator_tag
5410 #include <tuple> // tuple_size, get, tuple_element
5411 #include <utility> // move
5412
5413 #if JSON_HAS_RANGES
5414 #include <ranges> // enable_borrowed_range
5415 #endif
5416
5417 // #include <nlohmann/detail/abi_macros.hpp>
5418
5419 // #include <nlohmann/detail/meta/type_traits.hpp>
5420
5421 // #include <nlohmann/detail/string_utils.hpp>
5422 // __ _____ _____ _____
5423 // __| | __| | | | JSON for Modern C++
5424 // | | |__ | | | | | | version 3.12.0
5425 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
5426 //
5427 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
5428 // SPDX-License-Identifier: MIT
5429
5430
5431
5432 #include <cstddef> // size_t
5433 #include <string> // string, to_string
5434
5435 // #include <nlohmann/detail/abi_macros.hpp>
5436
5437
5438 NLOHMANN_JSON_NAMESPACE_BEGIN
5439 namespace detail
5440 {
5441
5442 template<typename StringType>
5443 void int_to_string(StringType& target, std::size_t value)
5444 {
5445 // For ADL
5446 using std::to_string;
5447 target = to_string(value);
5448 }
5449
5450 template<typename StringType>
5451 StringType to_string(std::size_t value)
5452 {
5453 StringType result;
5454 int_to_string(result, value);
5455 return result;
5456 }
5457
5458 } // namespace detail
5459 NLOHMANN_JSON_NAMESPACE_END
5460
5461 // #include <nlohmann/detail/value_t.hpp>
5462
5463
5464 NLOHMANN_JSON_NAMESPACE_BEGIN
5465 namespace detail
5466 {
5467
5468 template<typename IteratorType> class iteration_proxy_value
5469 {
5470 public:
5471 using difference_type = std::ptrdiff_t;
5472 using value_type = iteration_proxy_value;
5473 using pointer = value_type *;
5474 using reference = value_type &;
5475 using iterator_category = std::forward_iterator_tag;
5476 using string_type = typename std::remove_cv< typename std::remove_reference<decltype( std::declval<IteratorType>().key() ) >::type >::type;
5477
5478 private:
5479 /// the iterator
5480 IteratorType anchor{};
5481 /// an index for arrays (used to create key names)
5482 std::size_t array_index = 0;
5483 /// last stringified array index
5484 mutable std::size_t array_index_last = 0;
5485 /// a string representation of the array index
5486 mutable string_type array_index_str = "0";
5487 /// an empty string (to return a reference for primitive values)
5488 string_type empty_str{};
5489
5490 public:
5491 explicit iteration_proxy_value() = default;
5492 explicit iteration_proxy_value(IteratorType it, std::size_t array_index_ = 0)
5493 noexcept(std::is_nothrow_move_constructible<IteratorType>::value
5494 && std::is_nothrow_default_constructible<string_type>::value)
5495 : anchor(std::move(it))
5496 , array_index(array_index_)
5497 {}
5498
5499 iteration_proxy_value(iteration_proxy_value const&) = default;
5500 iteration_proxy_value& operator=(iteration_proxy_value const&) = default;
5501 // older GCCs are a bit fussy and require explicit noexcept specifiers on defaulted functions
5502 iteration_proxy_value(iteration_proxy_value&&)
5503 noexcept(std::is_nothrow_move_constructible<IteratorType>::value
5504 && std::is_nothrow_move_constructible<string_type>::value) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor,cppcoreguidelines-noexcept-move-operations)
5505 iteration_proxy_value& operator=(iteration_proxy_value&&)
5506 noexcept(std::is_nothrow_move_assignable<IteratorType>::value
5507 && std::is_nothrow_move_assignable<string_type>::value) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor,cppcoreguidelines-noexcept-move-operations)
5508 ~iteration_proxy_value() = default;
5509
5510 /// dereference operator (needed for range-based for)
5511 const iteration_proxy_value& operator*() const
5512 {
5513 return *this;
5514 }
5515
5516 /// increment operator (needed for range-based for)
5517 iteration_proxy_value& operator++()
5518 {
5519 ++anchor;
5520 ++array_index;
5521
5522 return *this;
5523 }
5524
5525 iteration_proxy_value operator++(int)& // NOLINT(cert-dcl21-cpp)
5526 {
5527 auto tmp = iteration_proxy_value(anchor, array_index);
5528 ++anchor;
5529 ++array_index;
5530 return tmp;
5531 }
5532
5533 /// equality operator (needed for InputIterator)
5534 bool operator==(const iteration_proxy_value& o) const
5535 {
5536 return anchor == o.anchor;
5537 }
5538
5539 /// inequality operator (needed for range-based for)
5540 bool operator!=(const iteration_proxy_value& o) const
5541 {
5542 return anchor != o.anchor;
5543 }
5544
5545 /// return key of the iterator
5546 const string_type& key() const
5547 {
5548 JSON_ASSERT(anchor.m_object != nullptr);
5549
5550 switch (anchor.m_object->type())
5551 {
5552 // use integer array index as key
5553 case value_t::array:
5554 {
5555 if (array_index != array_index_last)
5556 {
5557 int_to_string( array_index_str, array_index );
5558 array_index_last = array_index;
5559 }
5560 return array_index_str;
5561 }
5562
5563 // use key from the object
5564 case value_t::object:
5565 return anchor.key();
5566
5567 // use an empty key for all primitive types
5568 case value_t::null:
5569 case value_t::string:
5570 case value_t::boolean:
5571 case value_t::number_integer:
5572 case value_t::number_unsigned:
5573 case value_t::number_float:
5574 case value_t::binary:
5575 case value_t::discarded:
5576 default:
5577 return empty_str;
5578 }
5579 }
5580
5581 /// return value of the iterator
5582 typename IteratorType::reference value() const
5583 {
5584 return anchor.value();
5585 }
5586 };
5587
5588 /// proxy class for the items() function
5589 template<typename IteratorType> class iteration_proxy
5590 {
5591 private:
5592 /// the container to iterate
5593 typename IteratorType::pointer container = nullptr;
5594
5595 public:
5596 explicit iteration_proxy() = default;
5597
5598 /// construct iteration proxy from a container
5599 explicit iteration_proxy(typename IteratorType::reference cont) noexcept
5600 : container(&cont) {}
5601
5602 iteration_proxy(iteration_proxy const&) = default;
5603 iteration_proxy& operator=(iteration_proxy const&) = default;
5604 iteration_proxy(iteration_proxy&&) noexcept = default;
5605 iteration_proxy& operator=(iteration_proxy&&) noexcept = default;
5606 ~iteration_proxy() = default;
5607
5608 /// return iterator begin (needed for range-based for)
5609 iteration_proxy_value<IteratorType> begin() const noexcept
5610 {
5611 return iteration_proxy_value<IteratorType>(container->begin());
5612 }
5613
5614 /// return iterator end (needed for range-based for)
5615 iteration_proxy_value<IteratorType> end() const noexcept
5616 {
5617 return iteration_proxy_value<IteratorType>(container->end());
5618 }
5619 };
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 == 0, int> = 0>
5625 auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.key())
5626 {
5627 return i.key();
5628 }
5629 // Structured Bindings Support
5630 // For further reference see https://blog.tartanllama.xyz/structured-bindings/
5631 // And see https://github.com/nlohmann/json/pull/1391
5632 template<std::size_t N, typename IteratorType, enable_if_t<N == 1, int> = 0>
5633 auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.value())
5634 {
5635 return i.value();
5636 }
5637
5638 } // namespace detail
5639 NLOHMANN_JSON_NAMESPACE_END
5640
5641 // The Addition to the STD Namespace is required to add
5642 // Structured Bindings Support to the iteration_proxy_value class
5643 // For further reference see https://blog.tartanllama.xyz/structured-bindings/
5644 // And see https://github.com/nlohmann/json/pull/1391
5645 namespace std
5646 {
5647
5648 #if defined(__clang__)
5649 // Fix: https://github.com/nlohmann/json/issues/1401
5650 #pragma clang diagnostic push
5651 #pragma clang diagnostic ignored "-Wmismatched-tags"
5652 #endif
5653 template<typename IteratorType>
5654 class tuple_size<::nlohmann::detail::iteration_proxy_value<IteratorType>> // NOLINT(cert-dcl58-cpp)
5655 : public std::integral_constant<std::size_t, 2> {};
5656
5657 template<std::size_t N, typename IteratorType>
5658 class tuple_element<N, ::nlohmann::detail::iteration_proxy_value<IteratorType >> // NOLINT(cert-dcl58-cpp)
5659 {
5660 public:
5661 using type = decltype(
5662 get<N>(std::declval <
5663 ::nlohmann::detail::iteration_proxy_value<IteratorType >> ()));
5664 };
5665 #if defined(__clang__)
5666 #pragma clang diagnostic pop
5667 #endif
5668
5669 } // namespace std
5670
5671 #if JSON_HAS_RANGES
5672 template <typename IteratorType>
5673 inline constexpr bool ::std::ranges::enable_borrowed_range<::nlohmann::detail::iteration_proxy<IteratorType>> = true;
5674 #endif
5675
5676 // #include <nlohmann/detail/meta/cpp_future.hpp>
5677
5678 // #include <nlohmann/detail/meta/std_fs.hpp>
5679
5680 // #include <nlohmann/detail/meta/type_traits.hpp>
5681
5682 // #include <nlohmann/detail/value_t.hpp>
5683
5684
5685 NLOHMANN_JSON_NAMESPACE_BEGIN
5686 namespace detail
5687 {
5688
5689 //////////////////
5690 // constructors //
5691 //////////////////
5692
5693 /*
5694 * Note all external_constructor<>::construct functions need to call
5695 * j.m_data.m_value.destroy(j.m_data.m_type) to avoid a memory leak in case j contains an
5696 * allocated value (e.g., a string). See bug issue
5697 * https://github.com/nlohmann/json/issues/2865 for more information.
5698 */
5699
5700 template<value_t> struct external_constructor;
5701
5702 template<>
5703 struct external_constructor<value_t::boolean>
5704 {
5705 template<typename BasicJsonType>
5706 static void construct(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept
5707 {
5708 j.m_data.m_value.destroy(j.m_data.m_type);
5709 j.m_data.m_type = value_t::boolean;
5710 j.m_data.m_value = b;
5711 j.assert_invariant();
5712 }
5713 };
5714
5715 template<>
5716 struct external_constructor<value_t::string>
5717 {
5718 template<typename BasicJsonType>
5719 static void construct(BasicJsonType& j, const typename BasicJsonType::string_t& s)
5720 {
5721 j.m_data.m_value.destroy(j.m_data.m_type);
5722 j.m_data.m_type = value_t::string;
5723 j.m_data.m_value = s;
5724 j.assert_invariant();
5725 }
5726
5727 template<typename BasicJsonType>
5728 static void construct(BasicJsonType& j, typename BasicJsonType::string_t&& s)
5729 {
5730 j.m_data.m_value.destroy(j.m_data.m_type);
5731 j.m_data.m_type = value_t::string;
5732 j.m_data.m_value = std::move(s);
5733 j.assert_invariant();
5734 }
5735
5736 template < typename BasicJsonType, typename CompatibleStringType,
5737 enable_if_t < !std::is_same<CompatibleStringType, typename BasicJsonType::string_t>::value,
5738 int > = 0 >
5739 static void construct(BasicJsonType& j, const CompatibleStringType& str)
5740 {
5741 j.m_data.m_value.destroy(j.m_data.m_type);
5742 j.m_data.m_type = value_t::string;
5743 j.m_data.m_value.string = j.template create<typename BasicJsonType::string_t>(str);
5744 j.assert_invariant();
5745 }
5746 };
5747
5748 template<>
5749 struct external_constructor<value_t::binary>
5750 {
5751 template<typename BasicJsonType>
5752 static void construct(BasicJsonType& j, const typename BasicJsonType::binary_t& b)
5753 {
5754 j.m_data.m_value.destroy(j.m_data.m_type);
5755 j.m_data.m_type = value_t::binary;
5756 j.m_data.m_value = typename BasicJsonType::binary_t(b);
5757 j.assert_invariant();
5758 }
5759
5760 template<typename BasicJsonType>
5761 static void construct(BasicJsonType& j, typename BasicJsonType::binary_t&& b)
5762 {
5763 j.m_data.m_value.destroy(j.m_data.m_type);
5764 j.m_data.m_type = value_t::binary;
5765 j.m_data.m_value = typename BasicJsonType::binary_t(std::move(b));
5766 j.assert_invariant();
5767 }
5768 };
5769
5770 template<>
5771 struct external_constructor<value_t::number_float>
5772 {
5773 template<typename BasicJsonType>
5774 static void construct(BasicJsonType& j, typename BasicJsonType::number_float_t val) noexcept
5775 {
5776 j.m_data.m_value.destroy(j.m_data.m_type);
5777 j.m_data.m_type = value_t::number_float;
5778 j.m_data.m_value = val;
5779 j.assert_invariant();
5780 }
5781 };
5782
5783 template<>
5784 struct external_constructor<value_t::number_unsigned>
5785 {
5786 template<typename BasicJsonType>
5787 static void construct(BasicJsonType& j, typename BasicJsonType::number_unsigned_t val) noexcept
5788 {
5789 j.m_data.m_value.destroy(j.m_data.m_type);
5790 j.m_data.m_type = value_t::number_unsigned;
5791 j.m_data.m_value = val;
5792 j.assert_invariant();
5793 }
5794 };
5795
5796 template<>
5797 struct external_constructor<value_t::number_integer>
5798 {
5799 template<typename BasicJsonType>
5800 static void construct(BasicJsonType& j, typename BasicJsonType::number_integer_t val) noexcept
5801 {
5802 j.m_data.m_value.destroy(j.m_data.m_type);
5803 j.m_data.m_type = value_t::number_integer;
5804 j.m_data.m_value = val;
5805 j.assert_invariant();
5806 }
5807 };
5808
5809 template<>
5810 struct external_constructor<value_t::array>
5811 {
5812 template<typename BasicJsonType>
5813 static void construct(BasicJsonType& j, const typename BasicJsonType::array_t& arr)
5814 {
5815 j.m_data.m_value.destroy(j.m_data.m_type);
5816 j.m_data.m_type = value_t::array;
5817 j.m_data.m_value = arr;
5818 j.set_parents();
5819 j.assert_invariant();
5820 }
5821
5822 template<typename BasicJsonType>
5823 static void construct(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
5824 {
5825 j.m_data.m_value.destroy(j.m_data.m_type);
5826 j.m_data.m_type = value_t::array;
5827 j.m_data.m_value = std::move(arr);
5828 j.set_parents();
5829 j.assert_invariant();
5830 }
5831
5832 template < typename BasicJsonType, typename CompatibleArrayType,
5833 enable_if_t < !std::is_same<CompatibleArrayType, typename BasicJsonType::array_t>::value,
5834 int > = 0 >
5835 static void construct(BasicJsonType& j, const CompatibleArrayType& arr)
5836 {
5837 using std::begin;
5838 using std::end;
5839
5840 j.m_data.m_value.destroy(j.m_data.m_type);
5841 j.m_data.m_type = value_t::array;
5842 j.m_data.m_value.array = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
5843 j.set_parents();
5844 j.assert_invariant();
5845 }
5846
5847 template<typename BasicJsonType>
5848 static void construct(BasicJsonType& j, const std::vector<bool>& arr)
5849 {
5850 j.m_data.m_value.destroy(j.m_data.m_type);
5851 j.m_data.m_type = value_t::array;
5852 j.m_data.m_value = value_t::array;
5853 j.m_data.m_value.array->reserve(arr.size());
5854 for (const bool x : arr)
5855 {
5856 j.m_data.m_value.array->push_back(x);
5857 j.set_parent(j.m_data.m_value.array->back());
5858 }
5859 j.assert_invariant();
5860 }
5861
5862 template<typename BasicJsonType, typename T,
5863 enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
5864 static void construct(BasicJsonType& j, const std::valarray<T>& arr)
5865 {
5866 j.m_data.m_value.destroy(j.m_data.m_type);
5867 j.m_data.m_type = value_t::array;
5868 j.m_data.m_value = value_t::array;
5869 j.m_data.m_value.array->resize(arr.size());
5870 if (arr.size() > 0)
5871 {
5872 std::copy(std::begin(arr), std::end(arr), j.m_data.m_value.array->begin());
5873 }
5874 j.set_parents();
5875 j.assert_invariant();
5876 }
5877 };
5878
5879 template<>
5880 struct external_constructor<value_t::object>
5881 {
5882 template<typename BasicJsonType>
5883 static void construct(BasicJsonType& j, const typename BasicJsonType::object_t& obj)
5884 {
5885 j.m_data.m_value.destroy(j.m_data.m_type);
5886 j.m_data.m_type = value_t::object;
5887 j.m_data.m_value = obj;
5888 j.set_parents();
5889 j.assert_invariant();
5890 }
5891
5892 template<typename BasicJsonType>
5893 static void construct(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
5894 {
5895 j.m_data.m_value.destroy(j.m_data.m_type);
5896 j.m_data.m_type = value_t::object;
5897 j.m_data.m_value = std::move(obj);
5898 j.set_parents();
5899 j.assert_invariant();
5900 }
5901
5902 template < typename BasicJsonType, typename CompatibleObjectType,
5903 enable_if_t < !std::is_same<CompatibleObjectType, typename BasicJsonType::object_t>::value, int > = 0 >
5904 static void construct(BasicJsonType& j, const CompatibleObjectType& obj)
5905 {
5906 using std::begin;
5907 using std::end;
5908
5909 j.m_data.m_value.destroy(j.m_data.m_type);
5910 j.m_data.m_type = value_t::object;
5911 j.m_data.m_value.object = j.template create<typename BasicJsonType::object_t>(begin(obj), end(obj));
5912 j.set_parents();
5913 j.assert_invariant();
5914 }
5915 };
5916
5917 /////////////
5918 // to_json //
5919 /////////////
5920
5921 #ifdef JSON_HAS_CPP_17
5922 template<typename BasicJsonType, typename T,
5923 enable_if_t<std::is_constructible<BasicJsonType, T>::value, int> = 0>
5924 void to_json(BasicJsonType& j, const std::optional<T>& opt) noexcept
5925 {
5926 if (opt.has_value())
5927 {
5928 j = *opt;
5929 }
5930 else
5931 {
5932 j = nullptr;
5933 }
5934 }
5935 #endif
5936
5937 template<typename BasicJsonType, typename T,
5938 enable_if_t<std::is_same<T, typename BasicJsonType::boolean_t>::value, int> = 0>
5939 inline void to_json(BasicJsonType& j, T b) noexcept
5940 {
5941 external_constructor<value_t::boolean>::construct(j, b);
5942 }
5943
5944 template < typename BasicJsonType, typename BoolRef,
5945 enable_if_t <
5946 ((std::is_same<std::vector<bool>::reference, BoolRef>::value
5947 && !std::is_same <std::vector<bool>::reference, typename BasicJsonType::boolean_t&>::value)
5948 || (std::is_same<std::vector<bool>::const_reference, BoolRef>::value
5949 && !std::is_same <detail::uncvref_t<std::vector<bool>::const_reference>,
5950 typename BasicJsonType::boolean_t >::value))
5951 && std::is_convertible<const BoolRef&, typename BasicJsonType::boolean_t>::value, int > = 0 >
5952 inline void to_json(BasicJsonType& j, const BoolRef& b) noexcept
5953 {
5954 external_constructor<value_t::boolean>::construct(j, static_cast<typename BasicJsonType::boolean_t>(b));
5955 }
5956
5957 template<typename BasicJsonType, typename CompatibleString,
5958 enable_if_t<std::is_constructible<typename BasicJsonType::string_t, CompatibleString>::value, int> = 0>
5959 inline void to_json(BasicJsonType& j, const CompatibleString& s)
5960 {
5961 external_constructor<value_t::string>::construct(j, s);
5962 }
5963
5964 template<typename BasicJsonType>
5965 inline void to_json(BasicJsonType& j, typename BasicJsonType::string_t&& s)
5966 {
5967 external_constructor<value_t::string>::construct(j, std::move(s));
5968 }
5969
5970 template<typename BasicJsonType, typename FloatType,
5971 enable_if_t<std::is_floating_point<FloatType>::value, int> = 0>
5972 inline void to_json(BasicJsonType& j, FloatType val) noexcept
5973 {
5974 external_constructor<value_t::number_float>::construct(j, static_cast<typename BasicJsonType::number_float_t>(val));
5975 }
5976
5977 template<typename BasicJsonType, typename CompatibleNumberUnsignedType,
5978 enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_unsigned_t, CompatibleNumberUnsignedType>::value, int> = 0>
5979 inline void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept
5980 {
5981 external_constructor<value_t::number_unsigned>::construct(j, static_cast<typename BasicJsonType::number_unsigned_t>(val));
5982 }
5983
5984 template<typename BasicJsonType, typename CompatibleNumberIntegerType,
5985 enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_integer_t, CompatibleNumberIntegerType>::value, int> = 0>
5986 inline void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept
5987 {
5988 external_constructor<value_t::number_integer>::construct(j, static_cast<typename BasicJsonType::number_integer_t>(val));
5989 }
5990
5991 #if !JSON_DISABLE_ENUM_SERIALIZATION
5992 template<typename BasicJsonType, typename EnumType,
5993 enable_if_t<std::is_enum<EnumType>::value, int> = 0>
5994 inline void to_json(BasicJsonType& j, EnumType e) noexcept
5995 {
5996 using underlying_type = typename std::underlying_type<EnumType>::type;
5997 static constexpr value_t integral_value_t = std::is_unsigned<underlying_type>::value ? value_t::number_unsigned : value_t::number_integer;
5998 external_constructor<integral_value_t>::construct(j, static_cast<underlying_type>(e));
5999 }
6000 #endif // JSON_DISABLE_ENUM_SERIALIZATION
6001
6002 template<typename BasicJsonType>
6003 inline void to_json(BasicJsonType& j, const std::vector<bool>& e)
6004 {
6005 external_constructor<value_t::array>::construct(j, e);
6006 }
6007
6008 template < typename BasicJsonType, typename CompatibleArrayType,
6009 enable_if_t < is_compatible_array_type<BasicJsonType,
6010 CompatibleArrayType>::value&&
6011 !is_compatible_object_type<BasicJsonType, CompatibleArrayType>::value&&
6012 !is_compatible_string_type<BasicJsonType, CompatibleArrayType>::value&&
6013 !std::is_same<typename BasicJsonType::binary_t, CompatibleArrayType>::value&&
6014 !is_basic_json<CompatibleArrayType>::value,
6015 int > = 0 >
6016 inline void to_json(BasicJsonType& j, const CompatibleArrayType& arr)
6017 {
6018 external_constructor<value_t::array>::construct(j, arr);
6019 }
6020
6021 template<typename BasicJsonType>
6022 inline void to_json(BasicJsonType& j, const typename BasicJsonType::binary_t& bin)
6023 {
6024 external_constructor<value_t::binary>::construct(j, bin);
6025 }
6026
6027 template<typename BasicJsonType, typename T,
6028 enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
6029 inline void to_json(BasicJsonType& j, const std::valarray<T>& arr)
6030 {
6031 external_constructor<value_t::array>::construct(j, std::move(arr));
6032 }
6033
6034 template<typename BasicJsonType>
6035 inline void to_json(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
6036 {
6037 external_constructor<value_t::array>::construct(j, std::move(arr));
6038 }
6039
6040 template < typename BasicJsonType, typename CompatibleObjectType,
6041 enable_if_t < is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value&& !is_basic_json<CompatibleObjectType>::value, int > = 0 >
6042 inline void to_json(BasicJsonType& j, const CompatibleObjectType& obj)
6043 {
6044 external_constructor<value_t::object>::construct(j, obj);
6045 }
6046
6047 template<typename BasicJsonType>
6048 inline void to_json(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
6049 {
6050 external_constructor<value_t::object>::construct(j, std::move(obj));
6051 }
6052
6053 template <
6054 typename BasicJsonType, typename T, std::size_t N,
6055 enable_if_t < !std::is_constructible<typename BasicJsonType::string_t,
6056 const T(&)[N]>::value, // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
6057 int > = 0 >
6058 inline void to_json(BasicJsonType& j, const T(&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
6059 {
6060 external_constructor<value_t::array>::construct(j, arr);
6061 }
6062
6063 template < typename BasicJsonType, typename T1, typename T2, enable_if_t < std::is_constructible<BasicJsonType, T1>::value&& std::is_constructible<BasicJsonType, T2>::value, int > = 0 >
6064 inline void to_json(BasicJsonType& j, const std::pair<T1, T2>& p)
6065 {
6066 j = { p.first, p.second };
6067 }
6068
6069 // for https://github.com/nlohmann/json/pull/1134
6070 template<typename BasicJsonType, typename T,
6071 enable_if_t<std::is_same<T, iteration_proxy_value<typename BasicJsonType::iterator>>::value, int> = 0>
6072 inline void to_json(BasicJsonType& j, const T& b)
6073 {
6074 j = { {b.key(), b.value()} };
6075 }
6076
6077 template<typename BasicJsonType, typename Tuple, std::size_t... Idx>
6078 inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence<Idx...> /*unused*/)
6079 {
6080 j = { std::get<Idx>(t)... };
6081 }
6082
6083 template<typename BasicJsonType, typename Tuple>
6084 inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& /*unused*/, index_sequence<> /*unused*/)
6085 {
6086 using array_t = typename BasicJsonType::array_t;
6087 j = array_t();
6088 }
6089
6090 template<typename BasicJsonType, typename T, enable_if_t<is_constructible_tuple<BasicJsonType, T>::value, int > = 0>
6091 inline void to_json(BasicJsonType& j, const T& t)
6092 {
6093 to_json_tuple_impl(j, t, make_index_sequence<std::tuple_size<T>::value> {});
6094 }
6095
6096 #if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
6097 #if defined(__cpp_lib_char8_t)
6098 template<typename BasicJsonType, typename Tr, typename Allocator>
6099 inline void to_json(BasicJsonType& j, const std::basic_string<char8_t, Tr, Allocator>& s)
6100 {
6101 using OtherAllocator = typename std::allocator_traits<Allocator>::template rebind_alloc<char>;
6102 j = std::basic_string<char, std::char_traits<char>, OtherAllocator>(s.begin(), s.end(), s.get_allocator());
6103 }
6104 #endif
6105
6106 template<typename BasicJsonType>
6107 inline void to_json(BasicJsonType& j, const std_fs::path& p)
6108 {
6109 // Returns either a std::string or a std::u8string depending whether library
6110 // support for char8_t is enabled.
6111 j = p.u8string();
6112 }
6113 #endif
6114
6115 struct to_json_fn
6116 {
6117 template<typename BasicJsonType, typename T>
6118 auto operator()(BasicJsonType& j, T&& val) const noexcept(noexcept(to_json(j, std::forward<T>(val))))
6119 -> decltype(to_json(j, std::forward<T>(val)), void())
6120 {
6121 return to_json(j, std::forward<T>(val));
6122 }
6123 };
6124 } // namespace detail
6125
6126 #ifndef JSON_HAS_CPP_17
6127 /// namespace to hold default `to_json` function
6128 /// to see why this is required:
6129 /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
6130 namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
6131 {
6132 #endif
6133 JSON_INLINE_VARIABLE constexpr const auto& to_json = // NOLINT(misc-definitions-in-headers)
6134 detail::static_const<detail::to_json_fn>::value;
6135 #ifndef JSON_HAS_CPP_17
6136 } // namespace
6137 #endif
6138
6139 NLOHMANN_JSON_NAMESPACE_END
6140
6141 // #include <nlohmann/detail/meta/identity_tag.hpp>
6142
6143
6144 NLOHMANN_JSON_NAMESPACE_BEGIN
6145
6146 /// @sa https://json.nlohmann.me/api/adl_serializer/
6147 template<typename ValueType, typename>
6148 struct adl_serializer
6149 {
6150 /// @brief convert a JSON value to any value type
6151 /// @sa https://json.nlohmann.me/api/adl_serializer/from_json/
6152 template<typename BasicJsonType, typename TargetType = ValueType>
6153 static auto from_json(BasicJsonType && j, TargetType& val) noexcept(
6154 noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), val)))
6155 -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), val), void())
6156 {
6157 ::nlohmann::from_json(std::forward<BasicJsonType>(j), val);
6158 }
6159
6160 /// @brief convert a JSON value to any value type
6161 /// @sa https://json.nlohmann.me/api/adl_serializer/from_json/
6162 template<typename BasicJsonType, typename TargetType = ValueType>
6163 static auto from_json(BasicJsonType && j) noexcept(
6164 noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {})))
6165 -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {}))
6166 {
6167 return ::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {});
6168 }
6169
6170 /// @brief convert any value type to a JSON value
6171 /// @sa https://json.nlohmann.me/api/adl_serializer/to_json/
6172 template<typename BasicJsonType, typename TargetType = ValueType>
6173 static auto to_json(BasicJsonType& j, TargetType && val) noexcept(
6174 noexcept(::nlohmann::to_json(j, std::forward<TargetType>(val))))
6175 -> decltype(::nlohmann::to_json(j, std::forward<TargetType>(val)), void())
6176 {
6177 ::nlohmann::to_json(j, std::forward<TargetType>(val));
6178 }
6179 };
6180
6181 NLOHMANN_JSON_NAMESPACE_END
6182
6183 // #include <nlohmann/byte_container_with_subtype.hpp>
6184 // __ _____ _____ _____
6185 // __| | __| | | | JSON for Modern C++
6186 // | | |__ | | | | | | version 3.12.0
6187 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
6188 //
6189 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
6190 // SPDX-License-Identifier: MIT
6191
6192
6193
6194 #include <cstdint> // uint8_t, uint64_t
6195 #include <tuple> // tie
6196 #include <utility> // move
6197
6198 // #include <nlohmann/detail/abi_macros.hpp>
6199
6200
6201 NLOHMANN_JSON_NAMESPACE_BEGIN
6202
6203 /// @brief an internal type for a backed binary type
6204 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/
6205 template<typename BinaryType>
6206 class byte_container_with_subtype : public BinaryType
6207 {
6208 public:
6209 using container_type = BinaryType;
6210 using subtype_type = std::uint64_t;
6211
6212 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
6213 byte_container_with_subtype() noexcept(noexcept(container_type()))
6214 : container_type()
6215 {}
6216
6217 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
6218 byte_container_with_subtype(const container_type& b) noexcept(noexcept(container_type(b)))
6219 : container_type(b)
6220 {}
6221
6222 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
6223 byte_container_with_subtype(container_type&& b) noexcept(noexcept(container_type(std::move(b))))
6224 : container_type(std::move(b))
6225 {}
6226
6227 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
6228 byte_container_with_subtype(const container_type& b, subtype_type subtype_) noexcept(noexcept(container_type(b)))
6229 : container_type(b)
6230 , m_subtype(subtype_)
6231 , m_has_subtype(true)
6232 {}
6233
6234 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
6235 byte_container_with_subtype(container_type&& b, subtype_type subtype_) noexcept(noexcept(container_type(std::move(b))))
6236 : container_type(std::move(b))
6237 , m_subtype(subtype_)
6238 , m_has_subtype(true)
6239 {}
6240
6241 bool operator==(const byte_container_with_subtype& rhs) const
6242 {
6243 return std::tie(static_cast<const BinaryType&>(*this), m_subtype, m_has_subtype) ==
6244 std::tie(static_cast<const BinaryType&>(rhs), rhs.m_subtype, rhs.m_has_subtype);
6245 }
6246
6247 bool operator!=(const byte_container_with_subtype& rhs) const
6248 {
6249 return !(rhs == *this);
6250 }
6251
6252 /// @brief sets the binary subtype
6253 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/set_subtype/
6254 void set_subtype(subtype_type subtype_) noexcept
6255 {
6256 m_subtype = subtype_;
6257 m_has_subtype = true;
6258 }
6259
6260 /// @brief return the binary subtype
6261 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/subtype/
6262 constexpr subtype_type subtype() const noexcept
6263 {
6264 return m_has_subtype ? m_subtype : static_cast<subtype_type>(-1);
6265 }
6266
6267 /// @brief return whether the value has a subtype
6268 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/has_subtype/
6269 constexpr bool has_subtype() const noexcept
6270 {
6271 return m_has_subtype;
6272 }
6273
6274 /// @brief clears the binary subtype
6275 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/clear_subtype/
6276 void clear_subtype() noexcept
6277 {
6278 m_subtype = 0;
6279 m_has_subtype = false;
6280 }
6281
6282 private:
6283 subtype_type m_subtype = 0;
6284 bool m_has_subtype = false;
6285 };
6286
6287 NLOHMANN_JSON_NAMESPACE_END
6288
6289 // #include <nlohmann/detail/conversions/from_json.hpp>
6290
6291 // #include <nlohmann/detail/conversions/to_json.hpp>
6292
6293 // #include <nlohmann/detail/exceptions.hpp>
6294
6295 // #include <nlohmann/detail/hash.hpp>
6296 // __ _____ _____ _____
6297 // __| | __| | | | JSON for Modern C++
6298 // | | |__ | | | | | | version 3.12.0
6299 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
6300 //
6301 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
6302 // SPDX-License-Identifier: MIT
6303
6304
6305
6306 #include <cstdint> // uint8_t
6307 #include <cstddef> // size_t
6308 #include <functional> // hash
6309
6310 // #include <nlohmann/detail/abi_macros.hpp>
6311
6312 // #include <nlohmann/detail/value_t.hpp>
6313
6314
6315 NLOHMANN_JSON_NAMESPACE_BEGIN
6316 namespace detail
6317 {
6318
6319 // boost::hash_combine
6320 inline std::size_t combine(std::size_t seed, std::size_t h) noexcept
6321 {
6322 seed ^= h + 0x9e3779b9 + (seed << 6U) + (seed >> 2U);
6323 return seed;
6324 }
6325
6326 /*!
6327 @brief hash a JSON value
6328
6329 The hash function tries to rely on std::hash where possible. Furthermore, the
6330 type of the JSON value is taken into account to have different hash values for
6331 null, 0, 0U, and false, etc.
6332
6333 @tparam BasicJsonType basic_json specialization
6334 @param j JSON value to hash
6335 @return hash value of j
6336 */
6337 template<typename BasicJsonType>
6338 std::size_t hash(const BasicJsonType& j)
6339 {
6340 using string_t = typename BasicJsonType::string_t;
6341 using number_integer_t = typename BasicJsonType::number_integer_t;
6342 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
6343 using number_float_t = typename BasicJsonType::number_float_t;
6344
6345 const auto type = static_cast<std::size_t>(j.type());
6346 switch (j.type())
6347 {
6348 case BasicJsonType::value_t::null:
6349 case BasicJsonType::value_t::discarded:
6350 {
6351 return combine(type, 0);
6352 }
6353
6354 case BasicJsonType::value_t::object:
6355 {
6356 auto seed = combine(type, j.size());
6357 for (const auto& element : j.items())
6358 {
6359 const auto h = std::hash<string_t> {}(element.key());
6360 seed = combine(seed, h);
6361 seed = combine(seed, hash(element.value()));
6362 }
6363 return seed;
6364 }
6365
6366 case BasicJsonType::value_t::array:
6367 {
6368 auto seed = combine(type, j.size());
6369 for (const auto& element : j)
6370 {
6371 seed = combine(seed, hash(element));
6372 }
6373 return seed;
6374 }
6375
6376 case BasicJsonType::value_t::string:
6377 {
6378 const auto h = std::hash<string_t> {}(j.template get_ref<const string_t&>());
6379 return combine(type, h);
6380 }
6381
6382 case BasicJsonType::value_t::boolean:
6383 {
6384 const auto h = std::hash<bool> {}(j.template get<bool>());
6385 return combine(type, h);
6386 }
6387
6388 case BasicJsonType::value_t::number_integer:
6389 {
6390 const auto h = std::hash<number_integer_t> {}(j.template get<number_integer_t>());
6391 return combine(type, h);
6392 }
6393
6394 case BasicJsonType::value_t::number_unsigned:
6395 {
6396 const auto h = std::hash<number_unsigned_t> {}(j.template get<number_unsigned_t>());
6397 return combine(type, h);
6398 }
6399
6400 case BasicJsonType::value_t::number_float:
6401 {
6402 const auto h = std::hash<number_float_t> {}(j.template get<number_float_t>());
6403 return combine(type, h);
6404 }
6405
6406 case BasicJsonType::value_t::binary:
6407 {
6408 auto seed = combine(type, j.get_binary().size());
6409 const auto h = std::hash<bool> {}(j.get_binary().has_subtype());
6410 seed = combine(seed, h);
6411 seed = combine(seed, static_cast<std::size_t>(j.get_binary().subtype()));
6412 for (const auto byte : j.get_binary())
6413 {
6414 seed = combine(seed, std::hash<std::uint8_t> {}(byte));
6415 }
6416 return seed;
6417 }
6418
6419 default: // LCOV_EXCL_LINE
6420 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
6421 return 0; // LCOV_EXCL_LINE
6422 }
6423 }
6424
6425 } // namespace detail
6426 NLOHMANN_JSON_NAMESPACE_END
6427
6428 // #include <nlohmann/detail/input/binary_reader.hpp>
6429 // __ _____ _____ _____
6430 // __| | __| | | | JSON for Modern C++
6431 // | | |__ | | | | | | version 3.12.0
6432 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
6433 //
6434 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
6435 // SPDX-License-Identifier: MIT
6436
6437
6438
6439 #include <algorithm> // generate_n
6440 #include <array> // array
6441 #include <cmath> // ldexp
6442 #include <cstddef> // size_t
6443 #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
6444 #include <cstdio> // snprintf
6445 #include <cstring> // memcpy
6446 #include <iterator> // back_inserter
6447 #include <limits> // numeric_limits
6448 #include <string> // char_traits, string
6449 #include <utility> // make_pair, move
6450 #include <vector> // vector
6451 #ifdef __cpp_lib_byteswap
6452 #include <bit> //byteswap
6453 #endif
6454
6455 // #include <nlohmann/detail/exceptions.hpp>
6456
6457 // #include <nlohmann/detail/input/input_adapters.hpp>
6458 // __ _____ _____ _____
6459 // __| | __| | | | JSON for Modern C++
6460 // | | |__ | | | | | | version 3.12.0
6461 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
6462 //
6463 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
6464 // SPDX-License-Identifier: MIT
6465
6466
6467
6468 #include <array> // array
6469 #include <cstddef> // size_t
6470 #include <cstring> // strlen
6471 #include <iterator> // begin, end, iterator_traits, random_access_iterator_tag, distance, next
6472 #include <memory> // shared_ptr, make_shared, addressof
6473 #include <numeric> // accumulate
6474 #include <string> // string, char_traits
6475 #include <type_traits> // enable_if, is_base_of, is_pointer, is_integral, remove_pointer
6476 #include <utility> // pair, declval
6477
6478 #ifndef JSON_NO_IO
6479 #include <cstdio> // FILE *
6480 #include <istream> // istream
6481 #endif // JSON_NO_IO
6482
6483 // #include <nlohmann/detail/exceptions.hpp>
6484
6485 // #include <nlohmann/detail/iterators/iterator_traits.hpp>
6486
6487 // #include <nlohmann/detail/macro_scope.hpp>
6488
6489 // #include <nlohmann/detail/meta/type_traits.hpp>
6490
6491
6492 NLOHMANN_JSON_NAMESPACE_BEGIN
6493 namespace detail
6494 {
6495
6496 /// the supported input formats
6497 enum class input_format_t { json, cbor, msgpack, ubjson, bson, bjdata };
6498
6499 ////////////////////
6500 // input adapters //
6501 ////////////////////
6502
6503 #ifndef JSON_NO_IO
6504 /*!
6505 Input adapter for stdio file access. This adapter read only 1 byte and do not use any
6506 buffer. This adapter is a very low level adapter.
6507 */
6508 class file_input_adapter
6509 {
6510 public:
6511 using char_type = char;
6512
6513 JSON_HEDLEY_NON_NULL(2)
6514 explicit file_input_adapter(std::FILE* f) noexcept
6515 : m_file(f)
6516 {
6517 JSON_ASSERT(m_file != nullptr);
6518 }
6519
6520 // make class move-only
6521 file_input_adapter(const file_input_adapter&) = delete;
6522 file_input_adapter(file_input_adapter&&) noexcept = default;
6523 file_input_adapter& operator=(const file_input_adapter&) = delete;
6524 file_input_adapter& operator=(file_input_adapter&&) = delete;
6525 ~file_input_adapter() = default;
6526
6527 std::char_traits<char>::int_type get_character() noexcept
6528 {
6529 return std::fgetc(m_file);
6530 }
6531
6532 // returns the number of characters successfully read
6533 template<class T>
6534 std::size_t get_elements(T* dest, std::size_t count = 1)
6535 {
6536 return fread(dest, 1, sizeof(T) * count, m_file);
6537 }
6538
6539 private:
6540 /// the file pointer to read from
6541 std::FILE* m_file;
6542 };
6543
6544 /*!
6545 Input adapter for a (caching) istream. Ignores a UFT Byte Order Mark at
6546 beginning of input. Does not support changing the underlying std::streambuf
6547 in mid-input. Maintains underlying std::istream and std::streambuf to support
6548 subsequent use of standard std::istream operations to process any input
6549 characters following those used in parsing the JSON input. Clears the
6550 std::istream flags; any input errors (e.g., EOF) will be detected by the first
6551 subsequent call for input from the std::istream.
6552 */
6553 class input_stream_adapter
6554 {
6555 public:
6556 using char_type = char;
6557
6558 ~input_stream_adapter()
6559 {
6560 // clear stream flags; we use underlying streambuf I/O, do not
6561 // maintain ifstream flags, except eof
6562 if (is != nullptr)
6563 {
6564 is->clear(is->rdstate() & std::ios::eofbit);
6565 }
6566 }
6567
6568 explicit input_stream_adapter(std::istream& i)
6569 : is(&i), sb(i.rdbuf())
6570 {}
6571
6572 // delete because of pointer members
6573 input_stream_adapter(const input_stream_adapter&) = delete;
6574 input_stream_adapter& operator=(input_stream_adapter&) = delete;
6575 input_stream_adapter& operator=(input_stream_adapter&&) = delete;
6576
6577 input_stream_adapter(input_stream_adapter&& rhs) noexcept
6578 : is(rhs.is), sb(rhs.sb)
6579 {
6580 rhs.is = nullptr;
6581 rhs.sb = nullptr;
6582 }
6583
6584 // std::istream/std::streambuf use std::char_traits<char>::to_int_type, to
6585 // ensure that std::char_traits<char>::eof() and the character 0xFF do not
6586 // end up as the same value, e.g. 0xFFFFFFFF.
6587 std::char_traits<char>::int_type get_character()
6588 {
6589 auto res = sb->sbumpc();
6590 // set eof manually, as we don't use the istream interface.
6591 if (JSON_HEDLEY_UNLIKELY(res == std::char_traits<char>::eof()))
6592 {
6593 is->clear(is->rdstate() | std::ios::eofbit);
6594 }
6595 return res;
6596 }
6597
6598 template<class T>
6599 std::size_t get_elements(T* dest, std::size_t count = 1)
6600 {
6601 auto res = static_cast<std::size_t>(sb->sgetn(reinterpret_cast<char*>(dest), static_cast<std::streamsize>(count * sizeof(T))));
6602 if (JSON_HEDLEY_UNLIKELY(res < count * sizeof(T)))
6603 {
6604 is->clear(is->rdstate() | std::ios::eofbit);
6605 }
6606 return res;
6607 }
6608
6609 private:
6610 /// the associated input stream
6611 std::istream* is = nullptr;
6612 std::streambuf* sb = nullptr;
6613 };
6614 #endif // JSON_NO_IO
6615
6616 // General-purpose iterator-based adapter. It might not be as fast as
6617 // theoretically possible for some containers, but it is extremely versatile.
6618 template<typename IteratorType>
6619 class iterator_input_adapter
6620 {
6621 public:
6622 using char_type = typename std::iterator_traits<IteratorType>::value_type;
6623
6624 iterator_input_adapter(IteratorType first, IteratorType last)
6625 : current(std::move(first)), end(std::move(last))
6626 {}
6627
6628 typename char_traits<char_type>::int_type get_character()
6629 {
6630 if (JSON_HEDLEY_LIKELY(current != end))
6631 {
6632 auto result = char_traits<char_type>::to_int_type(*current);
6633 std::advance(current, 1);
6634 return result;
6635 }
6636
6637 return char_traits<char_type>::eof();
6638 }
6639
6640 // for general iterators, we cannot really do something better than falling back to processing the range one-by-one
6641 template<class T>
6642 std::size_t get_elements(T* dest, std::size_t count = 1)
6643 {
6644 auto* ptr = reinterpret_cast<char*>(dest);
6645 for (std::size_t read_index = 0; read_index < count * sizeof(T); ++read_index)
6646 {
6647 if (JSON_HEDLEY_LIKELY(current != end))
6648 {
6649 ptr[read_index] = static_cast<char>(*current);
6650 std::advance(current, 1);
6651 }
6652 else
6653 {
6654 return read_index;
6655 }
6656 }
6657 return count * sizeof(T);
6658 }
6659
6660 private:
6661 IteratorType current;
6662 IteratorType end;
6663
6664 template<typename BaseInputAdapter, size_t T>
6665 friend struct wide_string_input_helper;
6666
6667 bool empty() const
6668 {
6669 return current == end;
6670 }
6671 };
6672
6673 template<typename BaseInputAdapter, size_t T>
6674 struct wide_string_input_helper;
6675
6676 template<typename BaseInputAdapter>
6677 struct wide_string_input_helper<BaseInputAdapter, 4>
6678 {
6679 // UTF-32
6680 static void fill_buffer(BaseInputAdapter& input,
6681 std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
6682 size_t& utf8_bytes_index,
6683 size_t& utf8_bytes_filled)
6684 {
6685 utf8_bytes_index = 0;
6686
6687 if (JSON_HEDLEY_UNLIKELY(input.empty()))
6688 {
6689 utf8_bytes[0] = std::char_traits<char>::eof();
6690 utf8_bytes_filled = 1;
6691 }
6692 else
6693 {
6694 // get the current character
6695 const auto wc = input.get_character();
6696
6697 // UTF-32 to UTF-8 encoding
6698 if (wc < 0x80)
6699 {
6700 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
6701 utf8_bytes_filled = 1;
6702 }
6703 else if (wc <= 0x7FF)
6704 {
6705 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((static_cast<unsigned int>(wc) >> 6u) & 0x1Fu));
6706 utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
6707 utf8_bytes_filled = 2;
6708 }
6709 else if (wc <= 0xFFFF)
6710 {
6711 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((static_cast<unsigned int>(wc) >> 12u) & 0x0Fu));
6712 utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
6713 utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
6714 utf8_bytes_filled = 3;
6715 }
6716 else if (wc <= 0x10FFFF)
6717 {
6718 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | ((static_cast<unsigned int>(wc) >> 18u) & 0x07u));
6719 utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 12u) & 0x3Fu));
6720 utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
6721 utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
6722 utf8_bytes_filled = 4;
6723 }
6724 else
6725 {
6726 // unknown character
6727 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
6728 utf8_bytes_filled = 1;
6729 }
6730 }
6731 }
6732 };
6733
6734 template<typename BaseInputAdapter>
6735 struct wide_string_input_helper<BaseInputAdapter, 2>
6736 {
6737 // UTF-16
6738 static void fill_buffer(BaseInputAdapter& input,
6739 std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
6740 size_t& utf8_bytes_index,
6741 size_t& utf8_bytes_filled)
6742 {
6743 utf8_bytes_index = 0;
6744
6745 if (JSON_HEDLEY_UNLIKELY(input.empty()))
6746 {
6747 utf8_bytes[0] = std::char_traits<char>::eof();
6748 utf8_bytes_filled = 1;
6749 }
6750 else
6751 {
6752 // get the current character
6753 const auto wc = input.get_character();
6754
6755 // UTF-16 to UTF-8 encoding
6756 if (wc < 0x80)
6757 {
6758 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
6759 utf8_bytes_filled = 1;
6760 }
6761 else if (wc <= 0x7FF)
6762 {
6763 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((static_cast<unsigned int>(wc) >> 6u)));
6764 utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
6765 utf8_bytes_filled = 2;
6766 }
6767 else if (0xD800 > wc || wc >= 0xE000)
6768 {
6769 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((static_cast<unsigned int>(wc) >> 12u)));
6770 utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
6771 utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
6772 utf8_bytes_filled = 3;
6773 }
6774 else
6775 {
6776 if (JSON_HEDLEY_UNLIKELY(!input.empty()))
6777 {
6778 const auto wc2 = static_cast<unsigned int>(input.get_character());
6779 const auto charcode = 0x10000u + (((static_cast<unsigned int>(wc) & 0x3FFu) << 10u) | (wc2 & 0x3FFu));
6780 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | (charcode >> 18u));
6781 utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 12u) & 0x3Fu));
6782 utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 6u) & 0x3Fu));
6783 utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (charcode & 0x3Fu));
6784 utf8_bytes_filled = 4;
6785 }
6786 else
6787 {
6788 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
6789 utf8_bytes_filled = 1;
6790 }
6791 }
6792 }
6793 }
6794 };
6795
6796 // Wraps another input adapter to convert wide character types into individual bytes.
6797 template<typename BaseInputAdapter, typename WideCharType>
6798 class wide_string_input_adapter
6799 {
6800 public:
6801 using char_type = char;
6802
6803 wide_string_input_adapter(BaseInputAdapter base)
6804 : base_adapter(base) {}
6805
6806 typename std::char_traits<char>::int_type get_character() noexcept
6807 {
6808 // check if buffer needs to be filled
6809 if (utf8_bytes_index == utf8_bytes_filled)
6810 {
6811 fill_buffer<sizeof(WideCharType)>();
6812
6813 JSON_ASSERT(utf8_bytes_filled > 0);
6814 JSON_ASSERT(utf8_bytes_index == 0);
6815 }
6816
6817 // use buffer
6818 JSON_ASSERT(utf8_bytes_filled > 0);
6819 JSON_ASSERT(utf8_bytes_index < utf8_bytes_filled);
6820 return utf8_bytes[utf8_bytes_index++];
6821 }
6822
6823 // parsing binary with wchar doesn't make sense, but since the parsing mode can be runtime, we need something here
6824 template<class T>
6825 std::size_t get_elements(T* /*dest*/, std::size_t /*count*/ = 1)
6826 {
6827 JSON_THROW(parse_error::create(112, 1, "wide string type cannot be interpreted as binary data", nullptr));
6828 }
6829
6830 private:
6831 BaseInputAdapter base_adapter;
6832
6833 template<size_t T>
6834 void fill_buffer()
6835 {
6836 wide_string_input_helper<BaseInputAdapter, T>::fill_buffer(base_adapter, utf8_bytes, utf8_bytes_index, utf8_bytes_filled);
6837 }
6838
6839 /// a buffer for UTF-8 bytes
6840 std::array<std::char_traits<char>::int_type, 4> utf8_bytes = {{0, 0, 0, 0}};
6841
6842 /// index to the utf8_codes array for the next valid byte
6843 std::size_t utf8_bytes_index = 0;
6844 /// number of valid bytes in the utf8_codes array
6845 std::size_t utf8_bytes_filled = 0;
6846 };
6847
6848 template<typename IteratorType, typename Enable = void>
6849 struct iterator_input_adapter_factory
6850 {
6851 using iterator_type = IteratorType;
6852 using char_type = typename std::iterator_traits<iterator_type>::value_type;
6853 using adapter_type = iterator_input_adapter<iterator_type>;
6854
6855 static adapter_type create(IteratorType first, IteratorType last)
6856 {
6857 return adapter_type(std::move(first), std::move(last));
6858 }
6859 };
6860
6861 template<typename T>
6862 struct is_iterator_of_multibyte
6863 {
6864 using value_type = typename std::iterator_traits<T>::value_type;
6865 enum
6866 {
6867 value = sizeof(value_type) > 1
6868 };
6869 };
6870
6871 template<typename IteratorType>
6872 struct iterator_input_adapter_factory<IteratorType, enable_if_t<is_iterator_of_multibyte<IteratorType>::value>>
6873 {
6874 using iterator_type = IteratorType;
6875 using char_type = typename std::iterator_traits<iterator_type>::value_type;
6876 using base_adapter_type = iterator_input_adapter<iterator_type>;
6877 using adapter_type = wide_string_input_adapter<base_adapter_type, char_type>;
6878
6879 static adapter_type create(IteratorType first, IteratorType last)
6880 {
6881 return adapter_type(base_adapter_type(std::move(first), std::move(last)));
6882 }
6883 };
6884
6885 // General purpose iterator-based input
6886 template<typename IteratorType>
6887 typename iterator_input_adapter_factory<IteratorType>::adapter_type input_adapter(IteratorType first, IteratorType last)
6888 {
6889 using factory_type = iterator_input_adapter_factory<IteratorType>;
6890 return factory_type::create(first, last);
6891 }
6892
6893 // Convenience shorthand from container to iterator
6894 // Enables ADL on begin(container) and end(container)
6895 // Encloses the using declarations in namespace for not to leak them to outside scope
6896
6897 namespace container_input_adapter_factory_impl
6898 {
6899
6900 using std::begin;
6901 using std::end;
6902
6903 template<typename ContainerType, typename Enable = void>
6904 struct container_input_adapter_factory {};
6905
6906 template<typename ContainerType>
6907 struct container_input_adapter_factory< ContainerType,
6908 void_t<decltype(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>()))>>
6909 {
6910 using adapter_type = decltype(input_adapter(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>())));
6911
6912 static adapter_type create(const ContainerType& container)
6913 {
6914 return input_adapter(begin(container), end(container));
6915 }
6916 };
6917
6918 } // namespace container_input_adapter_factory_impl
6919
6920 template<typename ContainerType>
6921 typename container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::adapter_type input_adapter(const ContainerType& container)
6922 {
6923 return container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::create(container);
6924 }
6925
6926 // specialization for std::string
6927 using string_input_adapter_type = decltype(input_adapter(std::declval<std::string>()));
6928
6929 #ifndef JSON_NO_IO
6930 // Special cases with fast paths
6931 inline file_input_adapter input_adapter(std::FILE* file)
6932 {
6933 if (file == nullptr)
6934 {
6935 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));
6936 }
6937 return file_input_adapter(file);
6938 }
6939
6940 inline input_stream_adapter input_adapter(std::istream& stream)
6941 {
6942 return input_stream_adapter(stream);
6943 }
6944
6945 inline input_stream_adapter input_adapter(std::istream&& stream)
6946 {
6947 return input_stream_adapter(stream);
6948 }
6949 #endif // JSON_NO_IO
6950
6951 using contiguous_bytes_input_adapter = decltype(input_adapter(std::declval<const char*>(), std::declval<const char*>()));
6952
6953 // Null-delimited strings, and the like.
6954 template < typename CharT,
6955 typename std::enable_if <
6956 std::is_pointer<CharT>::value&&
6957 !std::is_array<CharT>::value&&
6958 std::is_integral<typename std::remove_pointer<CharT>::type>::value&&
6959 sizeof(typename std::remove_pointer<CharT>::type) == 1,
6960 int >::type = 0 >
6961 contiguous_bytes_input_adapter input_adapter(CharT b)
6962 {
6963 if (b == nullptr)
6964 {
6965 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));
6966 }
6967 auto length = std::strlen(reinterpret_cast<const char*>(b));
6968 const auto* ptr = reinterpret_cast<const char*>(b);
6969 return input_adapter(ptr, ptr + length); // cppcheck-suppress[nullPointerArithmeticRedundantCheck]
6970 }
6971
6972 template<typename T, std::size_t N>
6973 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)
6974 {
6975 return input_adapter(array, array + N);
6976 }
6977
6978 // This class only handles inputs of input_buffer_adapter type.
6979 // It's required so that expressions like {ptr, len} can be implicitly cast
6980 // to the correct adapter.
6981 class span_input_adapter
6982 {
6983 public:
6984 template < typename CharT,
6985 typename std::enable_if <
6986 std::is_pointer<CharT>::value&&
6987 std::is_integral<typename std::remove_pointer<CharT>::type>::value&&
6988 sizeof(typename std::remove_pointer<CharT>::type) == 1,
6989 int >::type = 0 >
6990 span_input_adapter(CharT b, std::size_t l)
6991 : ia(reinterpret_cast<const char*>(b), reinterpret_cast<const char*>(b) + l) {}
6992
6993 template<class IteratorType,
6994 typename std::enable_if<
6995 std::is_same<typename iterator_traits<IteratorType>::iterator_category, std::random_access_iterator_tag>::value,
6996 int>::type = 0>
6997 span_input_adapter(IteratorType first, IteratorType last)
6998 : ia(input_adapter(first, last)) {}
6999
7000 contiguous_bytes_input_adapter&& get()
7001 {
7002 return std::move(ia); // NOLINT(hicpp-move-const-arg,performance-move-const-arg)
7003 }
7004
7005 private:
7006 contiguous_bytes_input_adapter ia;
7007 };
7008
7009 } // namespace detail
7010 NLOHMANN_JSON_NAMESPACE_END
7011
7012 // #include <nlohmann/detail/input/json_sax.hpp>
7013 // __ _____ _____ _____
7014 // __| | __| | | | JSON for Modern C++
7015 // | | |__ | | | | | | version 3.12.0
7016 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
7017 //
7018 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
7019 // SPDX-License-Identifier: MIT
7020
7021
7022
7023 #include <cstddef>
7024 #include <string> // string
7025 #include <type_traits> // enable_if_t
7026 #include <utility> // move
7027 #include <vector> // vector
7028
7029 // #include <nlohmann/detail/exceptions.hpp>
7030
7031 // #include <nlohmann/detail/input/lexer.hpp>
7032 // __ _____ _____ _____
7033 // __| | __| | | | JSON for Modern C++
7034 // | | |__ | | | | | | version 3.12.0
7035 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
7036 //
7037 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
7038 // SPDX-License-Identifier: MIT
7039
7040
7041
7042 #include <array> // array
7043 #include <clocale> // localeconv
7044 #include <cstddef> // size_t
7045 #include <cstdio> // snprintf
7046 #include <cstdlib> // strtof, strtod, strtold, strtoll, strtoull
7047 #include <initializer_list> // initializer_list
7048 #include <string> // char_traits, string
7049 #include <utility> // move
7050 #include <vector> // vector
7051
7052 // #include <nlohmann/detail/input/input_adapters.hpp>
7053
7054 // #include <nlohmann/detail/input/position_t.hpp>
7055
7056 // #include <nlohmann/detail/macro_scope.hpp>
7057
7058 // #include <nlohmann/detail/meta/type_traits.hpp>
7059
7060
7061 NLOHMANN_JSON_NAMESPACE_BEGIN
7062 namespace detail
7063 {
7064
7065 ///////////
7066 // lexer //
7067 ///////////
7068
7069 template<typename BasicJsonType>
7070 class lexer_base
7071 {
7072 public:
7073 /// token types for the parser
7074 enum class token_type
7075 {
7076 uninitialized, ///< indicating the scanner is uninitialized
7077 literal_true, ///< the `true` literal
7078 literal_false, ///< the `false` literal
7079 literal_null, ///< the `null` literal
7080 value_string, ///< a string -- use get_string() for actual value
7081 value_unsigned, ///< an unsigned integer -- use get_number_unsigned() for actual value
7082 value_integer, ///< a signed integer -- use get_number_integer() for actual value
7083 value_float, ///< an floating point number -- use get_number_float() for actual value
7084 begin_array, ///< the character for array begin `[`
7085 begin_object, ///< the character for object begin `{`
7086 end_array, ///< the character for array end `]`
7087 end_object, ///< the character for object end `}`
7088 name_separator, ///< the name separator `:`
7089 value_separator, ///< the value separator `,`
7090 parse_error, ///< indicating a parse error
7091 end_of_input, ///< indicating the end of the input buffer
7092 literal_or_value ///< a literal or the begin of a value (only for diagnostics)
7093 };
7094
7095 /// return name of values of type token_type (only used for errors)
7096 JSON_HEDLEY_RETURNS_NON_NULL
7097 JSON_HEDLEY_CONST
7098 static const char* token_type_name(const token_type t) noexcept
7099 {
7100 switch (t)
7101 {
7102 case token_type::uninitialized:
7103 return "<uninitialized>";
7104 case token_type::literal_true:
7105 return "true literal";
7106 case token_type::literal_false:
7107 return "false literal";
7108 case token_type::literal_null:
7109 return "null literal";
7110 case token_type::value_string:
7111 return "string literal";
7112 case token_type::value_unsigned:
7113 case token_type::value_integer:
7114 case token_type::value_float:
7115 return "number literal";
7116 case token_type::begin_array:
7117 return "'['";
7118 case token_type::begin_object:
7119 return "'{'";
7120 case token_type::end_array:
7121 return "']'";
7122 case token_type::end_object:
7123 return "'}'";
7124 case token_type::name_separator:
7125 return "':'";
7126 case token_type::value_separator:
7127 return "','";
7128 case token_type::parse_error:
7129 return "<parse error>";
7130 case token_type::end_of_input:
7131 return "end of input";
7132 case token_type::literal_or_value:
7133 return "'[', '{', or a literal";
7134 // LCOV_EXCL_START
7135 default: // catch non-enum values
7136 return "unknown token";
7137 // LCOV_EXCL_STOP
7138 }
7139 }
7140 };
7141 /*!
7142 @brief lexical analysis
7143
7144 This class organizes the lexical analysis during JSON deserialization.
7145 */
7146 template<typename BasicJsonType, typename InputAdapterType>
7147 class lexer : public lexer_base<BasicJsonType>
7148 {
7149 using number_integer_t = typename BasicJsonType::number_integer_t;
7150 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
7151 using number_float_t = typename BasicJsonType::number_float_t;
7152 using string_t = typename BasicJsonType::string_t;
7153 using char_type = typename InputAdapterType::char_type;
7154 using char_int_type = typename char_traits<char_type>::int_type;
7155
7156 public:
7157 using token_type = typename lexer_base<BasicJsonType>::token_type;
7158
7159 explicit lexer(InputAdapterType&& adapter, bool ignore_comments_ = false) noexcept
7160 : ia(std::move(adapter))
7161 , ignore_comments(ignore_comments_)
7162 , decimal_point_char(static_cast<char_int_type>(get_decimal_point()))
7163 {}
7164
7165 // delete because of pointer members
7166 lexer(const lexer&) = delete;
7167 lexer(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
7168 lexer& operator=(lexer&) = delete;
7169 lexer& operator=(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
7170 ~lexer() = default;
7171
7172 private:
7173 /////////////////////
7174 // locales
7175 /////////////////////
7176
7177 /// return the locale-dependent decimal point
7178 JSON_HEDLEY_PURE
7179 static char get_decimal_point() noexcept
7180 {
7181 const auto* loc = localeconv();
7182 JSON_ASSERT(loc != nullptr);
7183 return (loc->decimal_point == nullptr) ? '.' : *(loc->decimal_point);
7184 }
7185
7186 /////////////////////
7187 // scan functions
7188 /////////////////////
7189
7190 /*!
7191 @brief get codepoint from 4 hex characters following `\u`
7192
7193 For input "\u c1 c2 c3 c4" the codepoint is:
7194 (c1 * 0x1000) + (c2 * 0x0100) + (c3 * 0x0010) + c4
7195 = (c1 << 12) + (c2 << 8) + (c3 << 4) + (c4 << 0)
7196
7197 Furthermore, the possible characters '0'..'9', 'A'..'F', and 'a'..'f'
7198 must be converted to the integers 0x0..0x9, 0xA..0xF, 0xA..0xF, resp. The
7199 conversion is done by subtracting the offset (0x30, 0x37, and 0x57)
7200 between the ASCII value of the character and the desired integer value.
7201
7202 @return codepoint (0x0000..0xFFFF) or -1 in case of an error (e.g. EOF or
7203 non-hex character)
7204 */
7205 int get_codepoint()
7206 {
7207 // this function only makes sense after reading `\u`
7208 JSON_ASSERT(current == 'u');
7209 int codepoint = 0;
7210
7211 const auto factors = { 12u, 8u, 4u, 0u };
7212 for (const auto factor : factors)
7213 {
7214 get();
7215
7216 if (current >= '0' && current <= '9')
7217 {
7218 codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x30u) << factor);
7219 }
7220 else if (current >= 'A' && current <= 'F')
7221 {
7222 codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x37u) << factor);
7223 }
7224 else if (current >= 'a' && current <= 'f')
7225 {
7226 codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x57u) << factor);
7227 }
7228 else
7229 {
7230 return -1;
7231 }
7232 }
7233
7234 JSON_ASSERT(0x0000 <= codepoint && codepoint <= 0xFFFF);
7235 return codepoint;
7236 }
7237
7238 /*!
7239 @brief check if the next byte(s) are inside a given range
7240
7241 Adds the current byte and, for each passed range, reads a new byte and
7242 checks if it is inside the range. If a violation was detected, set up an
7243 error message and return false. Otherwise, return true.
7244
7245 @param[in] ranges list of integers; interpreted as list of pairs of
7246 inclusive lower and upper bound, respectively
7247
7248 @pre The passed list @a ranges must have 2, 4, or 6 elements; that is,
7249 1, 2, or 3 pairs. This precondition is enforced by an assertion.
7250
7251 @return true if and only if no range violation was detected
7252 */
7253 bool next_byte_in_range(std::initializer_list<char_int_type> ranges)
7254 {
7255 JSON_ASSERT(ranges.size() == 2 || ranges.size() == 4 || ranges.size() == 6);
7256 add(current);
7257
7258 for (auto range = ranges.begin(); range != ranges.end(); ++range)
7259 {
7260 get();
7261 if (JSON_HEDLEY_LIKELY(*range <= current && current <= *(++range))) // NOLINT(bugprone-inc-dec-in-conditions)
7262 {
7263 add(current);
7264 }
7265 else
7266 {
7267 error_message = "invalid string: ill-formed UTF-8 byte";
7268 return false;
7269 }
7270 }
7271
7272 return true;
7273 }
7274
7275 /*!
7276 @brief scan a string literal
7277
7278 This function scans a string according to Sect. 7 of RFC 8259. While
7279 scanning, bytes are escaped and copied into buffer token_buffer. Then the
7280 function returns successfully, token_buffer is *not* null-terminated (as it
7281 may contain \0 bytes), and token_buffer.size() is the number of bytes in the
7282 string.
7283
7284 @return token_type::value_string if string could be successfully scanned,
7285 token_type::parse_error otherwise
7286
7287 @note In case of errors, variable error_message contains a textual
7288 description.
7289 */
7290 token_type scan_string()
7291 {
7292 // reset token_buffer (ignore opening quote)
7293 reset();
7294
7295 // we entered the function by reading an open quote
7296 JSON_ASSERT(current == '\"');
7297
7298 while (true)
7299 {
7300 // get next character
7301 switch (get())
7302 {
7303 // end of file while parsing string
7304 case char_traits<char_type>::eof():
7305 {
7306 error_message = "invalid string: missing closing quote";
7307 return token_type::parse_error;
7308 }
7309
7310 // closing quote
7311 case '\"':
7312 {
7313 return token_type::value_string;
7314 }
7315
7316 // escapes
7317 case '\\':
7318 {
7319 switch (get())
7320 {
7321 // quotation mark
7322 case '\"':
7323 add('\"');
7324 break;
7325 // reverse solidus
7326 case '\\':
7327 add('\\');
7328 break;
7329 // solidus
7330 case '/':
7331 add('/');
7332 break;
7333 // backspace
7334 case 'b':
7335 add('\b');
7336 break;
7337 // form feed
7338 case 'f':
7339 add('\f');
7340 break;
7341 // line feed
7342 case 'n':
7343 add('\n');
7344 break;
7345 // carriage return
7346 case 'r':
7347 add('\r');
7348 break;
7349 // tab
7350 case 't':
7351 add('\t');
7352 break;
7353
7354 // unicode escapes
7355 case 'u':
7356 {
7357 const int codepoint1 = get_codepoint();
7358 int codepoint = codepoint1; // start with codepoint1
7359
7360 if (JSON_HEDLEY_UNLIKELY(codepoint1 == -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 code point is a high surrogate
7367 if (0xD800 <= codepoint1 && codepoint1 <= 0xDBFF)
7368 {
7369 // expect next \uxxxx entry
7370 if (JSON_HEDLEY_LIKELY(get() == '\\' && get() == 'u'))
7371 {
7372 const int codepoint2 = get_codepoint();
7373
7374 if (JSON_HEDLEY_UNLIKELY(codepoint2 == -1))
7375 {
7376 error_message = "invalid string: '\\u' must be followed by 4 hex digits";
7377 return token_type::parse_error;
7378 }
7379
7380 // check if codepoint2 is a low surrogate
7381 if (JSON_HEDLEY_LIKELY(0xDC00 <= codepoint2 && codepoint2 <= 0xDFFF))
7382 {
7383 // overwrite codepoint
7384 codepoint = static_cast<int>(
7385 // high surrogate occupies the most significant 22 bits
7386 (static_cast<unsigned int>(codepoint1) << 10u)
7387 // low surrogate occupies the least significant 15 bits
7388 + static_cast<unsigned int>(codepoint2)
7389 // there is still the 0xD800, 0xDC00 and 0x10000 noise
7390 // in the result, so we have to subtract with:
7391 // (0xD800 << 10) + DC00 - 0x10000 = 0x35FDC00
7392 - 0x35FDC00u);
7393 }
7394 else
7395 {
7396 error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF";
7397 return token_type::parse_error;
7398 }
7399 }
7400 else
7401 {
7402 error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF";
7403 return token_type::parse_error;
7404 }
7405 }
7406 else
7407 {
7408 if (JSON_HEDLEY_UNLIKELY(0xDC00 <= codepoint1 && codepoint1 <= 0xDFFF))
7409 {
7410 error_message = "invalid string: surrogate U+DC00..U+DFFF must follow U+D800..U+DBFF";
7411 return token_type::parse_error;
7412 }
7413 }
7414
7415 // result of the above calculation yields a proper codepoint
7416 JSON_ASSERT(0x00 <= codepoint && codepoint <= 0x10FFFF);
7417
7418 // translate codepoint into bytes
7419 if (codepoint < 0x80)
7420 {
7421 // 1-byte characters: 0xxxxxxx (ASCII)
7422 add(static_cast<char_int_type>(codepoint));
7423 }
7424 else if (codepoint <= 0x7FF)
7425 {
7426 // 2-byte characters: 110xxxxx 10xxxxxx
7427 add(static_cast<char_int_type>(0xC0u | (static_cast<unsigned int>(codepoint) >> 6u)));
7428 add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
7429 }
7430 else if (codepoint <= 0xFFFF)
7431 {
7432 // 3-byte characters: 1110xxxx 10xxxxxx 10xxxxxx
7433 add(static_cast<char_int_type>(0xE0u | (static_cast<unsigned int>(codepoint) >> 12u)));
7434 add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
7435 add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
7436 }
7437 else
7438 {
7439 // 4-byte characters: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
7440 add(static_cast<char_int_type>(0xF0u | (static_cast<unsigned int>(codepoint) >> 18u)));
7441 add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 12u) & 0x3Fu)));
7442 add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
7443 add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
7444 }
7445
7446 break;
7447 }
7448
7449 // other characters after escape
7450 default:
7451 error_message = "invalid string: forbidden character after backslash";
7452 return token_type::parse_error;
7453 }
7454
7455 break;
7456 }
7457
7458 // invalid control characters
7459 case 0x00:
7460 {
7461 error_message = "invalid string: control character U+0000 (NUL) must be escaped to \\u0000";
7462 return token_type::parse_error;
7463 }
7464
7465 case 0x01:
7466 {
7467 error_message = "invalid string: control character U+0001 (SOH) must be escaped to \\u0001";
7468 return token_type::parse_error;
7469 }
7470
7471 case 0x02:
7472 {
7473 error_message = "invalid string: control character U+0002 (STX) must be escaped to \\u0002";
7474 return token_type::parse_error;
7475 }
7476
7477 case 0x03:
7478 {
7479 error_message = "invalid string: control character U+0003 (ETX) must be escaped to \\u0003";
7480 return token_type::parse_error;
7481 }
7482
7483 case 0x04:
7484 {
7485 error_message = "invalid string: control character U+0004 (EOT) must be escaped to \\u0004";
7486 return token_type::parse_error;
7487 }
7488
7489 case 0x05:
7490 {
7491 error_message = "invalid string: control character U+0005 (ENQ) must be escaped to \\u0005";
7492 return token_type::parse_error;
7493 }
7494
7495 case 0x06:
7496 {
7497 error_message = "invalid string: control character U+0006 (ACK) must be escaped to \\u0006";
7498 return token_type::parse_error;
7499 }
7500
7501 case 0x07:
7502 {
7503 error_message = "invalid string: control character U+0007 (BEL) must be escaped to \\u0007";
7504 return token_type::parse_error;
7505 }
7506
7507 case 0x08:
7508 {
7509 error_message = "invalid string: control character U+0008 (BS) must be escaped to \\u0008 or \\b";
7510 return token_type::parse_error;
7511 }
7512
7513 case 0x09:
7514 {
7515 error_message = "invalid string: control character U+0009 (HT) must be escaped to \\u0009 or \\t";
7516 return token_type::parse_error;
7517 }
7518
7519 case 0x0A:
7520 {
7521 error_message = "invalid string: control character U+000A (LF) must be escaped to \\u000A or \\n";
7522 return token_type::parse_error;
7523 }
7524
7525 case 0x0B:
7526 {
7527 error_message = "invalid string: control character U+000B (VT) must be escaped to \\u000B";
7528 return token_type::parse_error;
7529 }
7530
7531 case 0x0C:
7532 {
7533 error_message = "invalid string: control character U+000C (FF) must be escaped to \\u000C or \\f";
7534 return token_type::parse_error;
7535 }
7536
7537 case 0x0D:
7538 {
7539 error_message = "invalid string: control character U+000D (CR) must be escaped to \\u000D or \\r";
7540 return token_type::parse_error;
7541 }
7542
7543 case 0x0E:
7544 {
7545 error_message = "invalid string: control character U+000E (SO) must be escaped to \\u000E";
7546 return token_type::parse_error;
7547 }
7548
7549 case 0x0F:
7550 {
7551 error_message = "invalid string: control character U+000F (SI) must be escaped to \\u000F";
7552 return token_type::parse_error;
7553 }
7554
7555 case 0x10:
7556 {
7557 error_message = "invalid string: control character U+0010 (DLE) must be escaped to \\u0010";
7558 return token_type::parse_error;
7559 }
7560
7561 case 0x11:
7562 {
7563 error_message = "invalid string: control character U+0011 (DC1) must be escaped to \\u0011";
7564 return token_type::parse_error;
7565 }
7566
7567 case 0x12:
7568 {
7569 error_message = "invalid string: control character U+0012 (DC2) must be escaped to \\u0012";
7570 return token_type::parse_error;
7571 }
7572
7573 case 0x13:
7574 {
7575 error_message = "invalid string: control character U+0013 (DC3) must be escaped to \\u0013";
7576 return token_type::parse_error;
7577 }
7578
7579 case 0x14:
7580 {
7581 error_message = "invalid string: control character U+0014 (DC4) must be escaped to \\u0014";
7582 return token_type::parse_error;
7583 }
7584
7585 case 0x15:
7586 {
7587 error_message = "invalid string: control character U+0015 (NAK) must be escaped to \\u0015";
7588 return token_type::parse_error;
7589 }
7590
7591 case 0x16:
7592 {
7593 error_message = "invalid string: control character U+0016 (SYN) must be escaped to \\u0016";
7594 return token_type::parse_error;
7595 }
7596
7597 case 0x17:
7598 {
7599 error_message = "invalid string: control character U+0017 (ETB) must be escaped to \\u0017";
7600 return token_type::parse_error;
7601 }
7602
7603 case 0x18:
7604 {
7605 error_message = "invalid string: control character U+0018 (CAN) must be escaped to \\u0018";
7606 return token_type::parse_error;
7607 }
7608
7609 case 0x19:
7610 {
7611 error_message = "invalid string: control character U+0019 (EM) must be escaped to \\u0019";
7612 return token_type::parse_error;
7613 }
7614
7615 case 0x1A:
7616 {
7617 error_message = "invalid string: control character U+001A (SUB) must be escaped to \\u001A";
7618 return token_type::parse_error;
7619 }
7620
7621 case 0x1B:
7622 {
7623 error_message = "invalid string: control character U+001B (ESC) must be escaped to \\u001B";
7624 return token_type::parse_error;
7625 }
7626
7627 case 0x1C:
7628 {
7629 error_message = "invalid string: control character U+001C (FS) must be escaped to \\u001C";
7630 return token_type::parse_error;
7631 }
7632
7633 case 0x1D:
7634 {
7635 error_message = "invalid string: control character U+001D (GS) must be escaped to \\u001D";
7636 return token_type::parse_error;
7637 }
7638
7639 case 0x1E:
7640 {
7641 error_message = "invalid string: control character U+001E (RS) must be escaped to \\u001E";
7642 return token_type::parse_error;
7643 }
7644
7645 case 0x1F:
7646 {
7647 error_message = "invalid string: control character U+001F (US) must be escaped to \\u001F";
7648 return token_type::parse_error;
7649 }
7650
7651 // U+0020..U+007F (except U+0022 (quote) and U+005C (backspace))
7652 case 0x20:
7653 case 0x21:
7654 case 0x23:
7655 case 0x24:
7656 case 0x25:
7657 case 0x26:
7658 case 0x27:
7659 case 0x28:
7660 case 0x29:
7661 case 0x2A:
7662 case 0x2B:
7663 case 0x2C:
7664 case 0x2D:
7665 case 0x2E:
7666 case 0x2F:
7667 case 0x30:
7668 case 0x31:
7669 case 0x32:
7670 case 0x33:
7671 case 0x34:
7672 case 0x35:
7673 case 0x36:
7674 case 0x37:
7675 case 0x38:
7676 case 0x39:
7677 case 0x3A:
7678 case 0x3B:
7679 case 0x3C:
7680 case 0x3D:
7681 case 0x3E:
7682 case 0x3F:
7683 case 0x40:
7684 case 0x41:
7685 case 0x42:
7686 case 0x43:
7687 case 0x44:
7688 case 0x45:
7689 case 0x46:
7690 case 0x47:
7691 case 0x48:
7692 case 0x49:
7693 case 0x4A:
7694 case 0x4B:
7695 case 0x4C:
7696 case 0x4D:
7697 case 0x4E:
7698 case 0x4F:
7699 case 0x50:
7700 case 0x51:
7701 case 0x52:
7702 case 0x53:
7703 case 0x54:
7704 case 0x55:
7705 case 0x56:
7706 case 0x57:
7707 case 0x58:
7708 case 0x59:
7709 case 0x5A:
7710 case 0x5B:
7711 case 0x5D:
7712 case 0x5E:
7713 case 0x5F:
7714 case 0x60:
7715 case 0x61:
7716 case 0x62:
7717 case 0x63:
7718 case 0x64:
7719 case 0x65:
7720 case 0x66:
7721 case 0x67:
7722 case 0x68:
7723 case 0x69:
7724 case 0x6A:
7725 case 0x6B:
7726 case 0x6C:
7727 case 0x6D:
7728 case 0x6E:
7729 case 0x6F:
7730 case 0x70:
7731 case 0x71:
7732 case 0x72:
7733 case 0x73:
7734 case 0x74:
7735 case 0x75:
7736 case 0x76:
7737 case 0x77:
7738 case 0x78:
7739 case 0x79:
7740 case 0x7A:
7741 case 0x7B:
7742 case 0x7C:
7743 case 0x7D:
7744 case 0x7E:
7745 case 0x7F:
7746 {
7747 add(current);
7748 break;
7749 }
7750
7751 // U+0080..U+07FF: bytes C2..DF 80..BF
7752 case 0xC2:
7753 case 0xC3:
7754 case 0xC4:
7755 case 0xC5:
7756 case 0xC6:
7757 case 0xC7:
7758 case 0xC8:
7759 case 0xC9:
7760 case 0xCA:
7761 case 0xCB:
7762 case 0xCC:
7763 case 0xCD:
7764 case 0xCE:
7765 case 0xCF:
7766 case 0xD0:
7767 case 0xD1:
7768 case 0xD2:
7769 case 0xD3:
7770 case 0xD4:
7771 case 0xD5:
7772 case 0xD6:
7773 case 0xD7:
7774 case 0xD8:
7775 case 0xD9:
7776 case 0xDA:
7777 case 0xDB:
7778 case 0xDC:
7779 case 0xDD:
7780 case 0xDE:
7781 case 0xDF:
7782 {
7783 if (JSON_HEDLEY_UNLIKELY(!next_byte_in_range({0x80, 0xBF})))
7784 {
7785 return token_type::parse_error;
7786 }
7787 break;
7788 }
7789
7790 // U+0800..U+0FFF: bytes E0 A0..BF 80..BF
7791 case 0xE0:
7792 {
7793 if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0xA0, 0xBF, 0x80, 0xBF}))))
7794 {
7795 return token_type::parse_error;
7796 }
7797 break;
7798 }
7799
7800 // U+1000..U+CFFF: bytes E1..EC 80..BF 80..BF
7801 // U+E000..U+FFFF: bytes EE..EF 80..BF 80..BF
7802 case 0xE1:
7803 case 0xE2:
7804 case 0xE3:
7805 case 0xE4:
7806 case 0xE5:
7807 case 0xE6:
7808 case 0xE7:
7809 case 0xE8:
7810 case 0xE9:
7811 case 0xEA:
7812 case 0xEB:
7813 case 0xEC:
7814 case 0xEE:
7815 case 0xEF:
7816 {
7817 if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF}))))
7818 {
7819 return token_type::parse_error;
7820 }
7821 break;
7822 }
7823
7824 // U+D000..U+D7FF: bytes ED 80..9F 80..BF
7825 case 0xED:
7826 {
7827 if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x9F, 0x80, 0xBF}))))
7828 {
7829 return token_type::parse_error;
7830 }
7831 break;
7832 }
7833
7834 // U+10000..U+3FFFF F0 90..BF 80..BF 80..BF
7835 case 0xF0:
7836 {
7837 if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x90, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
7838 {
7839 return token_type::parse_error;
7840 }
7841 break;
7842 }
7843
7844 // U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF
7845 case 0xF1:
7846 case 0xF2:
7847 case 0xF3:
7848 {
7849 if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
7850 {
7851 return token_type::parse_error;
7852 }
7853 break;
7854 }
7855
7856 // U+100000..U+10FFFF F4 80..8F 80..BF 80..BF
7857 case 0xF4:
7858 {
7859 if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x8F, 0x80, 0xBF, 0x80, 0xBF}))))
7860 {
7861 return token_type::parse_error;
7862 }
7863 break;
7864 }
7865
7866 // remaining bytes (80..C1 and F5..FF) are ill-formed
7867 default:
7868 {
7869 error_message = "invalid string: ill-formed UTF-8 byte";
7870 return token_type::parse_error;
7871 }
7872 }
7873 }
7874 }
7875
7876 /*!
7877 * @brief scan a comment
7878 * @return whether comment could be scanned successfully
7879 */
7880 bool scan_comment()
7881 {
7882 switch (get())
7883 {
7884 // single-line comments skip input until a newline or EOF is read
7885 case '/':
7886 {
7887 while (true)
7888 {
7889 switch (get())
7890 {
7891 case '\n':
7892 case '\r':
7893 case char_traits<char_type>::eof():
7894 case '\0':
7895 return true;
7896
7897 default:
7898 break;
7899 }
7900 }
7901 }
7902
7903 // multi-line comments skip input until */ is read
7904 case '*':
7905 {
7906 while (true)
7907 {
7908 switch (get())
7909 {
7910 case char_traits<char_type>::eof():
7911 case '\0':
7912 {
7913 error_message = "invalid comment; missing closing '*/'";
7914 return false;
7915 }
7916
7917 case '*':
7918 {
7919 switch (get())
7920 {
7921 case '/':
7922 return true;
7923
7924 default:
7925 {
7926 unget();
7927 continue;
7928 }
7929 }
7930 }
7931
7932 default:
7933 continue;
7934 }
7935 }
7936 }
7937
7938 // unexpected character after reading '/'
7939 default:
7940 {
7941 error_message = "invalid comment; expecting '/' or '*' after '/'";
7942 return false;
7943 }
7944 }
7945 }
7946
7947 JSON_HEDLEY_NON_NULL(2)
7948 static void strtof(float& f, const char* str, char** endptr) noexcept
7949 {
7950 f = std::strtof(str, endptr);
7951 }
7952
7953 JSON_HEDLEY_NON_NULL(2)
7954 static void strtof(double& f, const char* str, char** endptr) noexcept
7955 {
7956 f = std::strtod(str, endptr);
7957 }
7958
7959 JSON_HEDLEY_NON_NULL(2)
7960 static void strtof(long double& f, const char* str, char** endptr) noexcept
7961 {
7962 f = std::strtold(str, endptr);
7963 }
7964
7965 /*!
7966 @brief scan a number literal
7967
7968 This function scans a string according to Sect. 6 of RFC 8259.
7969
7970 The function is realized with a deterministic finite state machine derived
7971 from the grammar described in RFC 8259. Starting in state "init", the
7972 input is read and used to determined the next state. Only state "done"
7973 accepts the number. State "error" is a trap state to model errors. In the
7974 table below, "anything" means any character but the ones listed before.
7975
7976 state | 0 | 1-9 | e E | + | - | . | anything
7977 ---------|----------|----------|----------|---------|---------|----------|-----------
7978 init | zero | any1 | [error] | [error] | minus | [error] | [error]
7979 minus | zero | any1 | [error] | [error] | [error] | [error] | [error]
7980 zero | done | done | exponent | done | done | decimal1 | done
7981 any1 | any1 | any1 | exponent | done | done | decimal1 | done
7982 decimal1 | decimal2 | decimal2 | [error] | [error] | [error] | [error] | [error]
7983 decimal2 | decimal2 | decimal2 | exponent | done | done | done | done
7984 exponent | any2 | any2 | [error] | sign | sign | [error] | [error]
7985 sign | any2 | any2 | [error] | [error] | [error] | [error] | [error]
7986 any2 | any2 | any2 | done | done | done | done | done
7987
7988 The state machine is realized with one label per state (prefixed with
7989 "scan_number_") and `goto` statements between them. The state machine
7990 contains cycles, but any cycle can be left when EOF is read. Therefore,
7991 the function is guaranteed to terminate.
7992
7993 During scanning, the read bytes are stored in token_buffer. This string is
7994 then converted to a signed integer, an unsigned integer, or a
7995 floating-point number.
7996
7997 @return token_type::value_unsigned, token_type::value_integer, or
7998 token_type::value_float if number could be successfully scanned,
7999 token_type::parse_error otherwise
8000
8001 @note The scanner is independent of the current locale. Internally, the
8002 locale's decimal point is used instead of `.` to work with the
8003 locale-dependent converters.
8004 */
8005 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.
8006 {
8007 // reset token_buffer to store the number's bytes
8008 reset();
8009
8010 // the type of the parsed number; initially set to unsigned; will be
8011 // changed if minus sign, decimal point or exponent is read
8012 token_type number_type = token_type::value_unsigned;
8013
8014 // state (init): we just found out we need to scan a number
8015 switch (current)
8016 {
8017 case '-':
8018 {
8019 add(current);
8020 goto scan_number_minus;
8021 }
8022
8023 case '0':
8024 {
8025 add(current);
8026 goto scan_number_zero;
8027 }
8028
8029 case '1':
8030 case '2':
8031 case '3':
8032 case '4':
8033 case '5':
8034 case '6':
8035 case '7':
8036 case '8':
8037 case '9':
8038 {
8039 add(current);
8040 goto scan_number_any1;
8041 }
8042
8043 // all other characters are rejected outside scan_number()
8044 default: // LCOV_EXCL_LINE
8045 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
8046 }
8047
8048 scan_number_minus:
8049 // state: we just parsed a leading minus sign
8050 number_type = token_type::value_integer;
8051 switch (get())
8052 {
8053 case '0':
8054 {
8055 add(current);
8056 goto scan_number_zero;
8057 }
8058
8059 case '1':
8060 case '2':
8061 case '3':
8062 case '4':
8063 case '5':
8064 case '6':
8065 case '7':
8066 case '8':
8067 case '9':
8068 {
8069 add(current);
8070 goto scan_number_any1;
8071 }
8072
8073 default:
8074 {
8075 error_message = "invalid number; expected digit after '-'";
8076 return token_type::parse_error;
8077 }
8078 }
8079
8080 scan_number_zero:
8081 // state: we just parse a zero (maybe with a leading minus sign)
8082 switch (get())
8083 {
8084 case '.':
8085 {
8086 add(decimal_point_char);
8087 decimal_point_position = token_buffer.size() - 1;
8088 goto scan_number_decimal1;
8089 }
8090
8091 case 'e':
8092 case 'E':
8093 {
8094 add(current);
8095 goto scan_number_exponent;
8096 }
8097
8098 default:
8099 goto scan_number_done;
8100 }
8101
8102 scan_number_any1:
8103 // state: we just parsed a number 0-9 (maybe with a leading minus sign)
8104 switch (get())
8105 {
8106 case '0':
8107 case '1':
8108 case '2':
8109 case '3':
8110 case '4':
8111 case '5':
8112 case '6':
8113 case '7':
8114 case '8':
8115 case '9':
8116 {
8117 add(current);
8118 goto scan_number_any1;
8119 }
8120
8121 case '.':
8122 {
8123 add(decimal_point_char);
8124 decimal_point_position = token_buffer.size() - 1;
8125 goto scan_number_decimal1;
8126 }
8127
8128 case 'e':
8129 case 'E':
8130 {
8131 add(current);
8132 goto scan_number_exponent;
8133 }
8134
8135 default:
8136 goto scan_number_done;
8137 }
8138
8139 scan_number_decimal1:
8140 // state: we just parsed a decimal point
8141 number_type = token_type::value_float;
8142 switch (get())
8143 {
8144 case '0':
8145 case '1':
8146 case '2':
8147 case '3':
8148 case '4':
8149 case '5':
8150 case '6':
8151 case '7':
8152 case '8':
8153 case '9':
8154 {
8155 add(current);
8156 goto scan_number_decimal2;
8157 }
8158
8159 default:
8160 {
8161 error_message = "invalid number; expected digit after '.'";
8162 return token_type::parse_error;
8163 }
8164 }
8165
8166 scan_number_decimal2:
8167 // we just parsed at least one number after a decimal point
8168 switch (get())
8169 {
8170 case '0':
8171 case '1':
8172 case '2':
8173 case '3':
8174 case '4':
8175 case '5':
8176 case '6':
8177 case '7':
8178 case '8':
8179 case '9':
8180 {
8181 add(current);
8182 goto scan_number_decimal2;
8183 }
8184
8185 case 'e':
8186 case 'E':
8187 {
8188 add(current);
8189 goto scan_number_exponent;
8190 }
8191
8192 default:
8193 goto scan_number_done;
8194 }
8195
8196 scan_number_exponent:
8197 // we just parsed an exponent
8198 number_type = token_type::value_float;
8199 switch (get())
8200 {
8201 case '+':
8202 case '-':
8203 {
8204 add(current);
8205 goto scan_number_sign;
8206 }
8207
8208 case '0':
8209 case '1':
8210 case '2':
8211 case '3':
8212 case '4':
8213 case '5':
8214 case '6':
8215 case '7':
8216 case '8':
8217 case '9':
8218 {
8219 add(current);
8220 goto scan_number_any2;
8221 }
8222
8223 default:
8224 {
8225 error_message =
8226 "invalid number; expected '+', '-', or digit after exponent";
8227 return token_type::parse_error;
8228 }
8229 }
8230
8231 scan_number_sign:
8232 // we just parsed an exponent sign
8233 switch (get())
8234 {
8235 case '0':
8236 case '1':
8237 case '2':
8238 case '3':
8239 case '4':
8240 case '5':
8241 case '6':
8242 case '7':
8243 case '8':
8244 case '9':
8245 {
8246 add(current);
8247 goto scan_number_any2;
8248 }
8249
8250 default:
8251 {
8252 error_message = "invalid number; expected digit after exponent sign";
8253 return token_type::parse_error;
8254 }
8255 }
8256
8257 scan_number_any2:
8258 // we just parsed a number after the exponent or exponent sign
8259 switch (get())
8260 {
8261 case '0':
8262 case '1':
8263 case '2':
8264 case '3':
8265 case '4':
8266 case '5':
8267 case '6':
8268 case '7':
8269 case '8':
8270 case '9':
8271 {
8272 add(current);
8273 goto scan_number_any2;
8274 }
8275
8276 default:
8277 goto scan_number_done;
8278 }
8279
8280 scan_number_done:
8281 // unget the character after the number (we only read it to know that
8282 // we are done scanning a number)
8283 unget();
8284
8285 char* endptr = nullptr; // NOLINT(misc-const-correctness,cppcoreguidelines-pro-type-vararg,hicpp-vararg)
8286 errno = 0;
8287
8288 // try to parse integers first and fall back to floats
8289 if (number_type == token_type::value_unsigned)
8290 {
8291 const auto x = std::strtoull(token_buffer.data(), &endptr, 10);
8292
8293 // we checked the number format before
8294 JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
8295
8296 if (errno != ERANGE)
8297 {
8298 value_unsigned = static_cast<number_unsigned_t>(x);
8299 if (value_unsigned == x)
8300 {
8301 return token_type::value_unsigned;
8302 }
8303 }
8304 }
8305 else if (number_type == token_type::value_integer)
8306 {
8307 const auto x = std::strtoll(token_buffer.data(), &endptr, 10);
8308
8309 // we checked the number format before
8310 JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
8311
8312 if (errno != ERANGE)
8313 {
8314 value_integer = static_cast<number_integer_t>(x);
8315 if (value_integer == x)
8316 {
8317 return token_type::value_integer;
8318 }
8319 }
8320 }
8321
8322 // this code is reached if we parse a floating-point number or if an
8323 // integer conversion above failed
8324 strtof(value_float, token_buffer.data(), &endptr);
8325
8326 // we checked the number format before
8327 JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
8328
8329 return token_type::value_float;
8330 }
8331
8332 /*!
8333 @param[in] literal_text the literal text to expect
8334 @param[in] length the length of the passed literal text
8335 @param[in] return_type the token type to return on success
8336 */
8337 JSON_HEDLEY_NON_NULL(2)
8338 token_type scan_literal(const char_type* literal_text, const std::size_t length,
8339 token_type return_type)
8340 {
8341 JSON_ASSERT(char_traits<char_type>::to_char_type(current) == literal_text[0]);
8342 for (std::size_t i = 1; i < length; ++i)
8343 {
8344 if (JSON_HEDLEY_UNLIKELY(char_traits<char_type>::to_char_type(get()) != literal_text[i]))
8345 {
8346 error_message = "invalid literal";
8347 return token_type::parse_error;
8348 }
8349 }
8350 return return_type;
8351 }
8352
8353 /////////////////////
8354 // input management
8355 /////////////////////
8356
8357 /// reset token_buffer; current character is beginning of token
8358 void reset() noexcept
8359 {
8360 token_buffer.clear();
8361 token_string.clear();
8362 decimal_point_position = std::string::npos;
8363 token_string.push_back(char_traits<char_type>::to_char_type(current));
8364 }
8365
8366 /*
8367 @brief get next character from the input
8368
8369 This function provides the interface to the used input adapter. It does
8370 not throw in case the input reached EOF, but returns a
8371 `char_traits<char>::eof()` in that case. Stores the scanned characters
8372 for use in error messages.
8373
8374 @return character read from the input
8375 */
8376 char_int_type get()
8377 {
8378 ++position.chars_read_total;
8379 ++position.chars_read_current_line;
8380
8381 if (next_unget)
8382 {
8383 // just reset the next_unget variable and work with current
8384 next_unget = false;
8385 }
8386 else
8387 {
8388 current = ia.get_character();
8389 }
8390
8391 if (JSON_HEDLEY_LIKELY(current != char_traits<char_type>::eof()))
8392 {
8393 token_string.push_back(char_traits<char_type>::to_char_type(current));
8394 }
8395
8396 if (current == '\n')
8397 {
8398 ++position.lines_read;
8399 position.chars_read_current_line = 0;
8400 }
8401
8402 return current;
8403 }
8404
8405 /*!
8406 @brief unget current character (read it again on next get)
8407
8408 We implement unget by setting variable next_unget to true. The input is not
8409 changed - we just simulate ungetting by modifying chars_read_total,
8410 chars_read_current_line, and token_string. The next call to get() will
8411 behave as if the unget character is read again.
8412 */
8413 void unget()
8414 {
8415 next_unget = true;
8416
8417 --position.chars_read_total;
8418
8419 // in case we "unget" a newline, we have to also decrement the lines_read
8420 if (position.chars_read_current_line == 0)
8421 {
8422 if (position.lines_read > 0)
8423 {
8424 --position.lines_read;
8425 }
8426 }
8427 else
8428 {
8429 --position.chars_read_current_line;
8430 }
8431
8432 if (JSON_HEDLEY_LIKELY(current != char_traits<char_type>::eof()))
8433 {
8434 JSON_ASSERT(!token_string.empty());
8435 token_string.pop_back();
8436 }
8437 }
8438
8439 /// add a character to token_buffer
8440 void add(char_int_type c)
8441 {
8442 token_buffer.push_back(static_cast<typename string_t::value_type>(c));
8443 }
8444
8445 public:
8446 /////////////////////
8447 // value getters
8448 /////////////////////
8449
8450 /// return integer value
8451 constexpr number_integer_t get_number_integer() const noexcept
8452 {
8453 return value_integer;
8454 }
8455
8456 /// return unsigned integer value
8457 constexpr number_unsigned_t get_number_unsigned() const noexcept
8458 {
8459 return value_unsigned;
8460 }
8461
8462 /// return floating-point value
8463 constexpr number_float_t get_number_float() const noexcept
8464 {
8465 return value_float;
8466 }
8467
8468 /// return current string value (implicitly resets the token; useful only once)
8469 string_t& get_string()
8470 {
8471 // translate decimal points from locale back to '.' (#4084)
8472 if (decimal_point_char != '.' && decimal_point_position != std::string::npos)
8473 {
8474 token_buffer[decimal_point_position] = '.';
8475 }
8476 return token_buffer;
8477 }
8478
8479 /////////////////////
8480 // diagnostics
8481 /////////////////////
8482
8483 /// return position of last read token
8484 constexpr position_t get_position() const noexcept
8485 {
8486 return position;
8487 }
8488
8489 /// return the last read token (for errors only). Will never contain EOF
8490 /// (an arbitrary value that is not a valid char value, often -1), because
8491 /// 255 may legitimately occur. May contain NUL, which should be escaped.
8492 std::string get_token_string() const
8493 {
8494 // escape control characters
8495 std::string result;
8496 for (const auto c : token_string)
8497 {
8498 if (static_cast<unsigned char>(c) <= '\x1F')
8499 {
8500 // escape control characters
8501 std::array<char, 9> cs{{}};
8502 static_cast<void>((std::snprintf)(cs.data(), cs.size(), "<U+%.4X>", static_cast<unsigned char>(c))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
8503 result += cs.data();
8504 }
8505 else
8506 {
8507 // add character as is
8508 result.push_back(static_cast<std::string::value_type>(c));
8509 }
8510 }
8511
8512 return result;
8513 }
8514
8515 /// return syntax error message
8516 JSON_HEDLEY_RETURNS_NON_NULL
8517 constexpr const char* get_error_message() const noexcept
8518 {
8519 return error_message;
8520 }
8521
8522 /////////////////////
8523 // actual scanner
8524 /////////////////////
8525
8526 /*!
8527 @brief skip the UTF-8 byte order mark
8528 @return true iff there is no BOM or the correct BOM has been skipped
8529 */
8530 bool skip_bom()
8531 {
8532 if (get() == 0xEF)
8533 {
8534 // check if we completely parse the BOM
8535 return get() == 0xBB && get() == 0xBF;
8536 }
8537
8538 // the first character is not the beginning of the BOM; unget it to
8539 // process is later
8540 unget();
8541 return true;
8542 }
8543
8544 void skip_whitespace()
8545 {
8546 do
8547 {
8548 get();
8549 }
8550 while (current == ' ' || current == '\t' || current == '\n' || current == '\r');
8551 }
8552
8553 token_type scan()
8554 {
8555 // initially, skip the BOM
8556 if (position.chars_read_total == 0 && !skip_bom())
8557 {
8558 error_message = "invalid BOM; must be 0xEF 0xBB 0xBF if given";
8559 return token_type::parse_error;
8560 }
8561
8562 // read next character and ignore whitespace
8563 skip_whitespace();
8564
8565 // ignore comments
8566 while (ignore_comments && current == '/')
8567 {
8568 if (!scan_comment())
8569 {
8570 return token_type::parse_error;
8571 }
8572
8573 // skip following whitespace
8574 skip_whitespace();
8575 }
8576
8577 switch (current)
8578 {
8579 // structural characters
8580 case '[':
8581 return token_type::begin_array;
8582 case ']':
8583 return token_type::end_array;
8584 case '{':
8585 return token_type::begin_object;
8586 case '}':
8587 return token_type::end_object;
8588 case ':':
8589 return token_type::name_separator;
8590 case ',':
8591 return token_type::value_separator;
8592
8593 // literals
8594 case 't':
8595 {
8596 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')}};
8597 return scan_literal(true_literal.data(), true_literal.size(), token_type::literal_true);
8598 }
8599 case 'f':
8600 {
8601 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')}};
8602 return scan_literal(false_literal.data(), false_literal.size(), token_type::literal_false);
8603 }
8604 case 'n':
8605 {
8606 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')}};
8607 return scan_literal(null_literal.data(), null_literal.size(), token_type::literal_null);
8608 }
8609
8610 // string
8611 case '\"':
8612 return scan_string();
8613
8614 // number
8615 case '-':
8616 case '0':
8617 case '1':
8618 case '2':
8619 case '3':
8620 case '4':
8621 case '5':
8622 case '6':
8623 case '7':
8624 case '8':
8625 case '9':
8626 return scan_number();
8627
8628 // end of input (the null byte is needed when parsing from
8629 // string literals)
8630 case '\0':
8631 case char_traits<char_type>::eof():
8632 return token_type::end_of_input;
8633
8634 // error
8635 default:
8636 error_message = "invalid literal";
8637 return token_type::parse_error;
8638 }
8639 }
8640
8641 private:
8642 /// input adapter
8643 InputAdapterType ia;
8644
8645 /// whether comments should be ignored (true) or signaled as errors (false)
8646 const bool ignore_comments = false;
8647
8648 /// the current character
8649 char_int_type current = char_traits<char_type>::eof();
8650
8651 /// whether the next get() call should just return current
8652 bool next_unget = false;
8653
8654 /// the start position of the current token
8655 position_t position {};
8656
8657 /// raw input token string (for error messages)
8658 std::vector<char_type> token_string {};
8659
8660 /// buffer for variable-length tokens (numbers, strings)
8661 string_t token_buffer {};
8662
8663 /// a description of occurred lexer errors
8664 const char* error_message = "";
8665
8666 // number values
8667 number_integer_t value_integer = 0;
8668 number_unsigned_t value_unsigned = 0;
8669 number_float_t value_float = 0;
8670
8671 /// the decimal point
8672 const char_int_type decimal_point_char = '.';
8673 /// the position of the decimal point in the input
8674 std::size_t decimal_point_position = std::string::npos;
8675 };
8676
8677 } // namespace detail
8678 NLOHMANN_JSON_NAMESPACE_END
8679
8680 // #include <nlohmann/detail/macro_scope.hpp>
8681
8682 // #include <nlohmann/detail/string_concat.hpp>
8683
8684 NLOHMANN_JSON_NAMESPACE_BEGIN
8685
8686 /*!
8687 @brief SAX interface
8688
8689 This class describes the SAX interface used by @ref nlohmann::json::sax_parse.
8690 Each function is called in different situations while the input is parsed. The
8691 boolean return value informs the parser whether to continue processing the
8692 input.
8693 */
8694 template<typename BasicJsonType>
8695 struct json_sax
8696 {
8697 using number_integer_t = typename BasicJsonType::number_integer_t;
8698 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
8699 using number_float_t = typename BasicJsonType::number_float_t;
8700 using string_t = typename BasicJsonType::string_t;
8701 using binary_t = typename BasicJsonType::binary_t;
8702
8703 /*!
8704 @brief a null value was read
8705 @return whether parsing should proceed
8706 */
8707 virtual bool null() = 0;
8708
8709 /*!
8710 @brief a boolean value was read
8711 @param[in] val boolean value
8712 @return whether parsing should proceed
8713 */
8714 virtual bool boolean(bool val) = 0;
8715
8716 /*!
8717 @brief an integer number was read
8718 @param[in] val integer value
8719 @return whether parsing should proceed
8720 */
8721 virtual bool number_integer(number_integer_t val) = 0;
8722
8723 /*!
8724 @brief an unsigned integer number was read
8725 @param[in] val unsigned integer value
8726 @return whether parsing should proceed
8727 */
8728 virtual bool number_unsigned(number_unsigned_t val) = 0;
8729
8730 /*!
8731 @brief a floating-point number was read
8732 @param[in] val floating-point value
8733 @param[in] s raw token value
8734 @return whether parsing should proceed
8735 */
8736 virtual bool number_float(number_float_t val, const string_t& s) = 0;
8737
8738 /*!
8739 @brief a string value was read
8740 @param[in] val string value
8741 @return whether parsing should proceed
8742 @note It is safe to move the passed string value.
8743 */
8744 virtual bool string(string_t& val) = 0;
8745
8746 /*!
8747 @brief a binary value was read
8748 @param[in] val binary value
8749 @return whether parsing should proceed
8750 @note It is safe to move the passed binary value.
8751 */
8752 virtual bool binary(binary_t& val) = 0;
8753
8754 /*!
8755 @brief the beginning of an object was read
8756 @param[in] elements number of object elements or -1 if unknown
8757 @return whether parsing should proceed
8758 @note binary formats may report the number of elements
8759 */
8760 virtual bool start_object(std::size_t elements) = 0;
8761
8762 /*!
8763 @brief an object key was read
8764 @param[in] val object key
8765 @return whether parsing should proceed
8766 @note It is safe to move the passed string.
8767 */
8768 virtual bool key(string_t& val) = 0;
8769
8770 /*!
8771 @brief the end of an object was read
8772 @return whether parsing should proceed
8773 */
8774 virtual bool end_object() = 0;
8775
8776 /*!
8777 @brief the beginning of an array was read
8778 @param[in] elements number of array elements or -1 if unknown
8779 @return whether parsing should proceed
8780 @note binary formats may report the number of elements
8781 */
8782 virtual bool start_array(std::size_t elements) = 0;
8783
8784 /*!
8785 @brief the end of an array was read
8786 @return whether parsing should proceed
8787 */
8788 virtual bool end_array() = 0;
8789
8790 /*!
8791 @brief a parse error occurred
8792 @param[in] position the position in the input where the error occurs
8793 @param[in] last_token the last read token
8794 @param[in] ex an exception object describing the error
8795 @return whether parsing should proceed (must return false)
8796 */
8797 virtual bool parse_error(std::size_t position,
8798 const std::string& last_token,
8799 const detail::exception& ex) = 0;
8800
8801 json_sax() = default;
8802 json_sax(const json_sax&) = default;
8803 json_sax(json_sax&&) noexcept = default;
8804 json_sax& operator=(const json_sax&) = default;
8805 json_sax& operator=(json_sax&&) noexcept = default;
8806 virtual ~json_sax() = default;
8807 };
8808
8809 namespace detail
8810 {
8811 constexpr std::size_t unknown_size()
8812 {
8813 return (std::numeric_limits<std::size_t>::max)();
8814 }
8815
8816 /*!
8817 @brief SAX implementation to create a JSON value from SAX events
8818
8819 This class implements the @ref json_sax interface and processes the SAX events
8820 to create a JSON value which makes it basically a DOM parser. The structure or
8821 hierarchy of the JSON value is managed by the stack `ref_stack` which contains
8822 a pointer to the respective array or object for each recursion depth.
8823
8824 After successful parsing, the value that is passed by reference to the
8825 constructor contains the parsed value.
8826
8827 @tparam BasicJsonType the JSON type
8828 */
8829 template<typename BasicJsonType, typename InputAdapterType>
8830 class json_sax_dom_parser
8831 {
8832 public:
8833 using number_integer_t = typename BasicJsonType::number_integer_t;
8834 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
8835 using number_float_t = typename BasicJsonType::number_float_t;
8836 using string_t = typename BasicJsonType::string_t;
8837 using binary_t = typename BasicJsonType::binary_t;
8838 using lexer_t = lexer<BasicJsonType, InputAdapterType>;
8839
8840 /*!
8841 @param[in,out] r reference to a JSON value that is manipulated while
8842 parsing
8843 @param[in] allow_exceptions_ whether parse errors yield exceptions
8844 */
8845 explicit json_sax_dom_parser(BasicJsonType& r, const bool allow_exceptions_ = true, lexer_t* lexer_ = nullptr)
8846 : root(r), allow_exceptions(allow_exceptions_), m_lexer_ref(lexer_)
8847 {}
8848
8849 // make class move-only
8850 json_sax_dom_parser(const json_sax_dom_parser&) = delete;
8851 json_sax_dom_parser(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
8852 json_sax_dom_parser& operator=(const json_sax_dom_parser&) = delete;
8853 json_sax_dom_parser& operator=(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
8854 ~json_sax_dom_parser() = default;
8855
8856 bool null()
8857 {
8858 handle_value(nullptr);
8859 return true;
8860 }
8861
8862 bool boolean(bool val)
8863 {
8864 handle_value(val);
8865 return true;
8866 }
8867
8868 bool number_integer(number_integer_t val)
8869 {
8870 handle_value(val);
8871 return true;
8872 }
8873
8874 bool number_unsigned(number_unsigned_t val)
8875 {
8876 handle_value(val);
8877 return true;
8878 }
8879
8880 bool number_float(number_float_t val, const string_t& /*unused*/)
8881 {
8882 handle_value(val);
8883 return true;
8884 }
8885
8886 bool string(string_t& val)
8887 {
8888 handle_value(val);
8889 return true;
8890 }
8891
8892 bool binary(binary_t& val)
8893 {
8894 handle_value(std::move(val));
8895 return true;
8896 }
8897
8898 bool start_object(std::size_t len)
8899 {
8900 ref_stack.push_back(handle_value(BasicJsonType::value_t::object));
8901
8902 #if JSON_DIAGNOSTIC_POSITIONS
8903 // Manually set the start position of the object here.
8904 // Ensure this is after the call to handle_value to ensure correct start position.
8905 if (m_lexer_ref)
8906 {
8907 // Lexer has read the first character of the object, so
8908 // subtract 1 from the position to get the correct start position.
8909 ref_stack.back()->start_position = m_lexer_ref->get_position() - 1;
8910 }
8911 #endif
8912
8913 if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
8914 {
8915 JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
8916 }
8917
8918 return true;
8919 }
8920
8921 bool key(string_t& val)
8922 {
8923 JSON_ASSERT(!ref_stack.empty());
8924 JSON_ASSERT(ref_stack.back()->is_object());
8925
8926 // add null at given key and store the reference for later
8927 object_element = &(ref_stack.back()->m_data.m_value.object->operator[](val));
8928 return true;
8929 }
8930
8931 bool end_object()
8932 {
8933 JSON_ASSERT(!ref_stack.empty());
8934 JSON_ASSERT(ref_stack.back()->is_object());
8935
8936 #if JSON_DIAGNOSTIC_POSITIONS
8937 if (m_lexer_ref)
8938 {
8939 // Lexer's position is past the closing brace, so set that as the end position.
8940 ref_stack.back()->end_position = m_lexer_ref->get_position();
8941 }
8942 #endif
8943
8944 ref_stack.back()->set_parents();
8945 ref_stack.pop_back();
8946 return true;
8947 }
8948
8949 bool start_array(std::size_t len)
8950 {
8951 ref_stack.push_back(handle_value(BasicJsonType::value_t::array));
8952
8953 #if JSON_DIAGNOSTIC_POSITIONS
8954 // Manually set the start position of the array here.
8955 // Ensure this is after the call to handle_value to ensure correct start position.
8956 if (m_lexer_ref)
8957 {
8958 ref_stack.back()->start_position = m_lexer_ref->get_position() - 1;
8959 }
8960 #endif
8961
8962 if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
8963 {
8964 JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
8965 }
8966
8967 return true;
8968 }
8969
8970 bool end_array()
8971 {
8972 JSON_ASSERT(!ref_stack.empty());
8973 JSON_ASSERT(ref_stack.back()->is_array());
8974
8975 #if JSON_DIAGNOSTIC_POSITIONS
8976 if (m_lexer_ref)
8977 {
8978 // Lexer's position is past the closing bracket, so set that as the end position.
8979 ref_stack.back()->end_position = m_lexer_ref->get_position();
8980 }
8981 #endif
8982
8983 ref_stack.back()->set_parents();
8984 ref_stack.pop_back();
8985 return true;
8986 }
8987
8988 template<class Exception>
8989 bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
8990 const Exception& ex)
8991 {
8992 errored = true;
8993 static_cast<void>(ex);
8994 if (allow_exceptions)
8995 {
8996 JSON_THROW(ex);
8997 }
8998 return false;
8999 }
9000
9001 constexpr bool is_errored() const
9002 {
9003 return errored;
9004 }
9005
9006 private:
9007
9008 #if JSON_DIAGNOSTIC_POSITIONS
9009 void handle_diagnostic_positions_for_json_value(BasicJsonType& v)
9010 {
9011 if (m_lexer_ref)
9012 {
9013 // Lexer has read past the current field value, so set the end position to the current position.
9014 // The start position will be set below based on the length of the string representation
9015 // of the value.
9016 v.end_position = m_lexer_ref->get_position();
9017
9018 switch (v.type())
9019 {
9020 case value_t::boolean:
9021 {
9022 // 4 and 5 are the string length of "true" and "false"
9023 v.start_position = v.end_position - (v.m_data.m_value.boolean ? 4 : 5);
9024 break;
9025 }
9026
9027 case value_t::null:
9028 {
9029 // 4 is the string length of "null"
9030 v.start_position = v.end_position - 4;
9031 break;
9032 }
9033
9034 case value_t::string:
9035 {
9036 // include the length of the quotes, which is 2
9037 v.start_position = v.end_position - v.m_data.m_value.string->size() - 2;
9038 break;
9039 }
9040
9041 // As we handle the start and end positions for values created during parsing,
9042 // we do not expect the following value type to be called. Regardless, set the positions
9043 // in case this is created manually or through a different constructor. Exclude from lcov
9044 // since the exact condition of this switch is esoteric.
9045 // LCOV_EXCL_START
9046 case value_t::discarded:
9047 {
9048 v.end_position = std::string::npos;
9049 v.start_position = v.end_position;
9050 break;
9051 }
9052 // LCOV_EXCL_STOP
9053 case value_t::binary:
9054 case value_t::number_integer:
9055 case value_t::number_unsigned:
9056 case value_t::number_float:
9057 {
9058 v.start_position = v.end_position - m_lexer_ref->get_string().size();
9059 break;
9060 }
9061 case value_t::object:
9062 case value_t::array:
9063 {
9064 // object and array are handled in start_object() and start_array() handlers
9065 // skip setting the values here.
9066 break;
9067 }
9068 default: // LCOV_EXCL_LINE
9069 // Handle all possible types discretely, default handler should never be reached.
9070 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert,-warnings-as-errors) LCOV_EXCL_LINE
9071 }
9072 }
9073 }
9074 #endif
9075
9076 /*!
9077 @invariant If the ref stack is empty, then the passed value will be the new
9078 root.
9079 @invariant If the ref stack contains a value, then it is an array or an
9080 object to which we can add elements
9081 */
9082 template<typename Value>
9083 JSON_HEDLEY_RETURNS_NON_NULL
9084 BasicJsonType* handle_value(Value&& v)
9085 {
9086 if (ref_stack.empty())
9087 {
9088 root = BasicJsonType(std::forward<Value>(v));
9089
9090 #if JSON_DIAGNOSTIC_POSITIONS
9091 handle_diagnostic_positions_for_json_value(root);
9092 #endif
9093
9094 return &root;
9095 }
9096
9097 JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
9098
9099 if (ref_stack.back()->is_array())
9100 {
9101 ref_stack.back()->m_data.m_value.array->emplace_back(std::forward<Value>(v));
9102
9103 #if JSON_DIAGNOSTIC_POSITIONS
9104 handle_diagnostic_positions_for_json_value(ref_stack.back()->m_data.m_value.array->back());
9105 #endif
9106
9107 return &(ref_stack.back()->m_data.m_value.array->back());
9108 }
9109
9110 JSON_ASSERT(ref_stack.back()->is_object());
9111 JSON_ASSERT(object_element);
9112 *object_element = BasicJsonType(std::forward<Value>(v));
9113
9114 #if JSON_DIAGNOSTIC_POSITIONS
9115 handle_diagnostic_positions_for_json_value(*object_element);
9116 #endif
9117
9118 return object_element;
9119 }
9120
9121 /// the parsed JSON value
9122 BasicJsonType& root;
9123 /// stack to model hierarchy of values
9124 std::vector<BasicJsonType*> ref_stack {};
9125 /// helper to hold the reference for the next object element
9126 BasicJsonType* object_element = nullptr;
9127 /// whether a syntax error occurred
9128 bool errored = false;
9129 /// whether to throw exceptions in case of errors
9130 const bool allow_exceptions = true;
9131 /// the lexer reference to obtain the current position
9132 lexer_t* m_lexer_ref = nullptr;
9133 };
9134
9135 template<typename BasicJsonType, typename InputAdapterType>
9136 class json_sax_dom_callback_parser
9137 {
9138 public:
9139 using number_integer_t = typename BasicJsonType::number_integer_t;
9140 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
9141 using number_float_t = typename BasicJsonType::number_float_t;
9142 using string_t = typename BasicJsonType::string_t;
9143 using binary_t = typename BasicJsonType::binary_t;
9144 using parser_callback_t = typename BasicJsonType::parser_callback_t;
9145 using parse_event_t = typename BasicJsonType::parse_event_t;
9146 using lexer_t = lexer<BasicJsonType, InputAdapterType>;
9147
9148 json_sax_dom_callback_parser(BasicJsonType& r,
9149 parser_callback_t cb,
9150 const bool allow_exceptions_ = true,
9151 lexer_t* lexer_ = nullptr)
9152 : root(r), callback(std::move(cb)), allow_exceptions(allow_exceptions_), m_lexer_ref(lexer_)
9153 {
9154 keep_stack.push_back(true);
9155 }
9156
9157 // make class move-only
9158 json_sax_dom_callback_parser(const json_sax_dom_callback_parser&) = delete;
9159 json_sax_dom_callback_parser(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
9160 json_sax_dom_callback_parser& operator=(const json_sax_dom_callback_parser&) = delete;
9161 json_sax_dom_callback_parser& operator=(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
9162 ~json_sax_dom_callback_parser() = default;
9163
9164 bool null()
9165 {
9166 handle_value(nullptr);
9167 return true;
9168 }
9169
9170 bool boolean(bool val)
9171 {
9172 handle_value(val);
9173 return true;
9174 }
9175
9176 bool number_integer(number_integer_t val)
9177 {
9178 handle_value(val);
9179 return true;
9180 }
9181
9182 bool number_unsigned(number_unsigned_t val)
9183 {
9184 handle_value(val);
9185 return true;
9186 }
9187
9188 bool number_float(number_float_t val, const string_t& /*unused*/)
9189 {
9190 handle_value(val);
9191 return true;
9192 }
9193
9194 bool string(string_t& val)
9195 {
9196 handle_value(val);
9197 return true;
9198 }
9199
9200 bool binary(binary_t& val)
9201 {
9202 handle_value(std::move(val));
9203 return true;
9204 }
9205
9206 bool start_object(std::size_t len)
9207 {
9208 // check callback for object start
9209 const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::object_start, discarded);
9210 keep_stack.push_back(keep);
9211
9212 auto val = handle_value(BasicJsonType::value_t::object, true);
9213 ref_stack.push_back(val.second);
9214
9215 if (ref_stack.back())
9216 {
9217
9218 #if JSON_DIAGNOSTIC_POSITIONS
9219 // Manually set the start position of the object here.
9220 // Ensure this is after the call to handle_value to ensure correct start position.
9221 if (m_lexer_ref)
9222 {
9223 // Lexer has read the first character of the object, so
9224 // subtract 1 from the position to get the correct start position.
9225 ref_stack.back()->start_position = m_lexer_ref->get_position() - 1;
9226 }
9227 #endif
9228
9229 // check object limit
9230 if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
9231 {
9232 JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
9233 }
9234 }
9235 return true;
9236 }
9237
9238 bool key(string_t& val)
9239 {
9240 BasicJsonType k = BasicJsonType(val);
9241
9242 // check callback for key
9243 const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::key, k);
9244 key_keep_stack.push_back(keep);
9245
9246 // add discarded value at given key and store the reference for later
9247 if (keep && ref_stack.back())
9248 {
9249 object_element = &(ref_stack.back()->m_data.m_value.object->operator[](val) = discarded);
9250 }
9251
9252 return true;
9253 }
9254
9255 bool end_object()
9256 {
9257 if (ref_stack.back())
9258 {
9259 if (!callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back()))
9260 {
9261 // discard object
9262 *ref_stack.back() = discarded;
9263
9264 #if JSON_DIAGNOSTIC_POSITIONS
9265 // Set start/end positions for discarded object.
9266 handle_diagnostic_positions_for_json_value(*ref_stack.back());
9267 #endif
9268 }
9269 else
9270 {
9271
9272 #if JSON_DIAGNOSTIC_POSITIONS
9273 if (m_lexer_ref)
9274 {
9275 // Lexer's position is past the closing brace, so set that as the end position.
9276 ref_stack.back()->end_position = m_lexer_ref->get_position();
9277 }
9278 #endif
9279
9280 ref_stack.back()->set_parents();
9281 }
9282 }
9283
9284 JSON_ASSERT(!ref_stack.empty());
9285 JSON_ASSERT(!keep_stack.empty());
9286 ref_stack.pop_back();
9287 keep_stack.pop_back();
9288
9289 if (!ref_stack.empty() && ref_stack.back() && ref_stack.back()->is_structured())
9290 {
9291 // remove discarded value
9292 for (auto it = ref_stack.back()->begin(); it != ref_stack.back()->end(); ++it)
9293 {
9294 if (it->is_discarded())
9295 {
9296 ref_stack.back()->erase(it);
9297 break;
9298 }
9299 }
9300 }
9301
9302 return true;
9303 }
9304
9305 bool start_array(std::size_t len)
9306 {
9307 const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::array_start, discarded);
9308 keep_stack.push_back(keep);
9309
9310 auto val = handle_value(BasicJsonType::value_t::array, true);
9311 ref_stack.push_back(val.second);
9312
9313 if (ref_stack.back())
9314 {
9315
9316 #if JSON_DIAGNOSTIC_POSITIONS
9317 // Manually set the start position of the array here.
9318 // Ensure this is after the call to handle_value to ensure correct start position.
9319 if (m_lexer_ref)
9320 {
9321 // Lexer has read the first character of the array, so
9322 // subtract 1 from the position to get the correct start position.
9323 ref_stack.back()->start_position = m_lexer_ref->get_position() - 1;
9324 }
9325 #endif
9326
9327 // check array limit
9328 if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
9329 {
9330 JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
9331 }
9332 }
9333
9334 return true;
9335 }
9336
9337 bool end_array()
9338 {
9339 bool keep = true;
9340
9341 if (ref_stack.back())
9342 {
9343 keep = callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back());
9344 if (keep)
9345 {
9346
9347 #if JSON_DIAGNOSTIC_POSITIONS
9348 if (m_lexer_ref)
9349 {
9350 // Lexer's position is past the closing bracket, so set that as the end position.
9351 ref_stack.back()->end_position = m_lexer_ref->get_position();
9352 }
9353 #endif
9354
9355 ref_stack.back()->set_parents();
9356 }
9357 else
9358 {
9359 // discard array
9360 *ref_stack.back() = discarded;
9361
9362 #if JSON_DIAGNOSTIC_POSITIONS
9363 // Set start/end positions for discarded array.
9364 handle_diagnostic_positions_for_json_value(*ref_stack.back());
9365 #endif
9366 }
9367 }
9368
9369 JSON_ASSERT(!ref_stack.empty());
9370 JSON_ASSERT(!keep_stack.empty());
9371 ref_stack.pop_back();
9372 keep_stack.pop_back();
9373
9374 // remove discarded value
9375 if (!keep && !ref_stack.empty() && ref_stack.back()->is_array())
9376 {
9377 ref_stack.back()->m_data.m_value.array->pop_back();
9378 }
9379
9380 return true;
9381 }
9382
9383 template<class Exception>
9384 bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
9385 const Exception& ex)
9386 {
9387 errored = true;
9388 static_cast<void>(ex);
9389 if (allow_exceptions)
9390 {
9391 JSON_THROW(ex);
9392 }
9393 return false;
9394 }
9395
9396 constexpr bool is_errored() const
9397 {
9398 return errored;
9399 }
9400
9401 private:
9402
9403 #if JSON_DIAGNOSTIC_POSITIONS
9404 void handle_diagnostic_positions_for_json_value(BasicJsonType& v)
9405 {
9406 if (m_lexer_ref)
9407 {
9408 // Lexer has read past the current field value, so set the end position to the current position.
9409 // The start position will be set below based on the length of the string representation
9410 // of the value.
9411 v.end_position = m_lexer_ref->get_position();
9412
9413 switch (v.type())
9414 {
9415 case value_t::boolean:
9416 {
9417 // 4 and 5 are the string length of "true" and "false"
9418 v.start_position = v.end_position - (v.m_data.m_value.boolean ? 4 : 5);
9419 break;
9420 }
9421
9422 case value_t::null:
9423 {
9424 // 4 is the string length of "null"
9425 v.start_position = v.end_position - 4;
9426 break;
9427 }
9428
9429 case value_t::string:
9430 {
9431 // include the length of the quotes, which is 2
9432 v.start_position = v.end_position - v.m_data.m_value.string->size() - 2;
9433 break;
9434 }
9435
9436 case value_t::discarded:
9437 {
9438 v.end_position = std::string::npos;
9439 v.start_position = v.end_position;
9440 break;
9441 }
9442
9443 case value_t::binary:
9444 case value_t::number_integer:
9445 case value_t::number_unsigned:
9446 case value_t::number_float:
9447 {
9448 v.start_position = v.end_position - m_lexer_ref->get_string().size();
9449 break;
9450 }
9451
9452 case value_t::object:
9453 case value_t::array:
9454 {
9455 // object and array are handled in start_object() and start_array() handlers
9456 // skip setting the values here.
9457 break;
9458 }
9459 default: // LCOV_EXCL_LINE
9460 // Handle all possible types discretely, default handler should never be reached.
9461 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert,-warnings-as-errors) LCOV_EXCL_LINE
9462 }
9463 }
9464 }
9465 #endif
9466
9467 /*!
9468 @param[in] v value to add to the JSON value we build during parsing
9469 @param[in] skip_callback whether we should skip calling the callback
9470 function; this is required after start_array() and
9471 start_object() SAX events, because otherwise we would call the
9472 callback function with an empty array or object, respectively.
9473
9474 @invariant If the ref stack is empty, then the passed value will be the new
9475 root.
9476 @invariant If the ref stack contains a value, then it is an array or an
9477 object to which we can add elements
9478
9479 @return pair of boolean (whether value should be kept) and pointer (to the
9480 passed value in the ref_stack hierarchy; nullptr if not kept)
9481 */
9482 template<typename Value>
9483 std::pair<bool, BasicJsonType*> handle_value(Value&& v, const bool skip_callback = false)
9484 {
9485 JSON_ASSERT(!keep_stack.empty());
9486
9487 // do not handle this value if we know it would be added to a discarded
9488 // container
9489 if (!keep_stack.back())
9490 {
9491 return {false, nullptr};
9492 }
9493
9494 // create value
9495 auto value = BasicJsonType(std::forward<Value>(v));
9496
9497 #if JSON_DIAGNOSTIC_POSITIONS
9498 handle_diagnostic_positions_for_json_value(value);
9499 #endif
9500
9501 // check callback
9502 const bool keep = skip_callback || callback(static_cast<int>(ref_stack.size()), parse_event_t::value, value);
9503
9504 // do not handle this value if we just learnt it shall be discarded
9505 if (!keep)
9506 {
9507 return {false, nullptr};
9508 }
9509
9510 if (ref_stack.empty())
9511 {
9512 root = std::move(value);
9513 return {true, & root};
9514 }
9515
9516 // skip this value if we already decided to skip the parent
9517 // (https://github.com/nlohmann/json/issues/971#issuecomment-413678360)
9518 if (!ref_stack.back())
9519 {
9520 return {false, nullptr};
9521 }
9522
9523 // we now only expect arrays and objects
9524 JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
9525
9526 // array
9527 if (ref_stack.back()->is_array())
9528 {
9529 ref_stack.back()->m_data.m_value.array->emplace_back(std::move(value));
9530 return {true, & (ref_stack.back()->m_data.m_value.array->back())};
9531 }
9532
9533 // object
9534 JSON_ASSERT(ref_stack.back()->is_object());
9535 // check if we should store an element for the current key
9536 JSON_ASSERT(!key_keep_stack.empty());
9537 const bool store_element = key_keep_stack.back();
9538 key_keep_stack.pop_back();
9539
9540 if (!store_element)
9541 {
9542 return {false, nullptr};
9543 }
9544
9545 JSON_ASSERT(object_element);
9546 *object_element = std::move(value);
9547 return {true, object_element};
9548 }
9549
9550 /// the parsed JSON value
9551 BasicJsonType& root;
9552 /// stack to model hierarchy of values
9553 std::vector<BasicJsonType*> ref_stack {};
9554 /// stack to manage which values to keep
9555 std::vector<bool> keep_stack {}; // NOLINT(readability-redundant-member-init)
9556 /// stack to manage which object keys to keep
9557 std::vector<bool> key_keep_stack {}; // NOLINT(readability-redundant-member-init)
9558 /// helper to hold the reference for the next object element
9559 BasicJsonType* object_element = nullptr;
9560 /// whether a syntax error occurred
9561 bool errored = false;
9562 /// callback function
9563 const parser_callback_t callback = nullptr;
9564 /// whether to throw exceptions in case of errors
9565 const bool allow_exceptions = true;
9566 /// a discarded value for the callback
9567 BasicJsonType discarded = BasicJsonType::value_t::discarded;
9568 /// the lexer reference to obtain the current position
9569 lexer_t* m_lexer_ref = nullptr;
9570 };
9571
9572 template<typename BasicJsonType>
9573 class json_sax_acceptor
9574 {
9575 public:
9576 using number_integer_t = typename BasicJsonType::number_integer_t;
9577 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
9578 using number_float_t = typename BasicJsonType::number_float_t;
9579 using string_t = typename BasicJsonType::string_t;
9580 using binary_t = typename BasicJsonType::binary_t;
9581
9582 bool null()
9583 {
9584 return true;
9585 }
9586
9587 bool boolean(bool /*unused*/)
9588 {
9589 return true;
9590 }
9591
9592 bool number_integer(number_integer_t /*unused*/)
9593 {
9594 return true;
9595 }
9596
9597 bool number_unsigned(number_unsigned_t /*unused*/)
9598 {
9599 return true;
9600 }
9601
9602 bool number_float(number_float_t /*unused*/, const string_t& /*unused*/)
9603 {
9604 return true;
9605 }
9606
9607 bool string(string_t& /*unused*/)
9608 {
9609 return true;
9610 }
9611
9612 bool binary(binary_t& /*unused*/)
9613 {
9614 return true;
9615 }
9616
9617 bool start_object(std::size_t /*unused*/ = detail::unknown_size())
9618 {
9619 return true;
9620 }
9621
9622 bool key(string_t& /*unused*/)
9623 {
9624 return true;
9625 }
9626
9627 bool end_object()
9628 {
9629 return true;
9630 }
9631
9632 bool start_array(std::size_t /*unused*/ = detail::unknown_size())
9633 {
9634 return true;
9635 }
9636
9637 bool end_array()
9638 {
9639 return true;
9640 }
9641
9642 bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const detail::exception& /*unused*/)
9643 {
9644 return false;
9645 }
9646 };
9647
9648 } // namespace detail
9649 NLOHMANN_JSON_NAMESPACE_END
9650
9651 // #include <nlohmann/detail/input/lexer.hpp>
9652
9653 // #include <nlohmann/detail/macro_scope.hpp>
9654
9655 // #include <nlohmann/detail/meta/is_sax.hpp>
9656 // __ _____ _____ _____
9657 // __| | __| | | | JSON for Modern C++
9658 // | | |__ | | | | | | version 3.12.0
9659 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
9660 //
9661 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
9662 // SPDX-License-Identifier: MIT
9663
9664
9665
9666 #include <cstdint> // size_t
9667 #include <utility> // declval
9668 #include <string> // string
9669
9670 // #include <nlohmann/detail/abi_macros.hpp>
9671
9672 // #include <nlohmann/detail/meta/detected.hpp>
9673
9674 // #include <nlohmann/detail/meta/type_traits.hpp>
9675
9676
9677 NLOHMANN_JSON_NAMESPACE_BEGIN
9678 namespace detail
9679 {
9680
9681 template<typename T>
9682 using null_function_t = decltype(std::declval<T&>().null());
9683
9684 template<typename T>
9685 using boolean_function_t =
9686 decltype(std::declval<T&>().boolean(std::declval<bool>()));
9687
9688 template<typename T, typename Integer>
9689 using number_integer_function_t =
9690 decltype(std::declval<T&>().number_integer(std::declval<Integer>()));
9691
9692 template<typename T, typename Unsigned>
9693 using number_unsigned_function_t =
9694 decltype(std::declval<T&>().number_unsigned(std::declval<Unsigned>()));
9695
9696 template<typename T, typename Float, typename String>
9697 using number_float_function_t = decltype(std::declval<T&>().number_float(
9698 std::declval<Float>(), std::declval<const String&>()));
9699
9700 template<typename T, typename String>
9701 using string_function_t =
9702 decltype(std::declval<T&>().string(std::declval<String&>()));
9703
9704 template<typename T, typename Binary>
9705 using binary_function_t =
9706 decltype(std::declval<T&>().binary(std::declval<Binary&>()));
9707
9708 template<typename T>
9709 using start_object_function_t =
9710 decltype(std::declval<T&>().start_object(std::declval<std::size_t>()));
9711
9712 template<typename T, typename String>
9713 using key_function_t =
9714 decltype(std::declval<T&>().key(std::declval<String&>()));
9715
9716 template<typename T>
9717 using end_object_function_t = decltype(std::declval<T&>().end_object());
9718
9719 template<typename T>
9720 using start_array_function_t =
9721 decltype(std::declval<T&>().start_array(std::declval<std::size_t>()));
9722
9723 template<typename T>
9724 using end_array_function_t = decltype(std::declval<T&>().end_array());
9725
9726 template<typename T, typename Exception>
9727 using parse_error_function_t = decltype(std::declval<T&>().parse_error(
9728 std::declval<std::size_t>(), std::declval<const std::string&>(),
9729 std::declval<const Exception&>()));
9730
9731 template<typename SAX, typename BasicJsonType>
9732 struct is_sax
9733 {
9734 private:
9735 static_assert(is_basic_json<BasicJsonType>::value,
9736 "BasicJsonType must be of type basic_json<...>");
9737
9738 using number_integer_t = typename BasicJsonType::number_integer_t;
9739 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
9740 using number_float_t = typename BasicJsonType::number_float_t;
9741 using string_t = typename BasicJsonType::string_t;
9742 using binary_t = typename BasicJsonType::binary_t;
9743 using exception_t = typename BasicJsonType::exception;
9744
9745 public:
9746 static constexpr bool value =
9747 is_detected_exact<bool, null_function_t, SAX>::value &&
9748 is_detected_exact<bool, boolean_function_t, SAX>::value &&
9749 is_detected_exact<bool, number_integer_function_t, SAX, number_integer_t>::value &&
9750 is_detected_exact<bool, number_unsigned_function_t, SAX, number_unsigned_t>::value &&
9751 is_detected_exact<bool, number_float_function_t, SAX, number_float_t, string_t>::value &&
9752 is_detected_exact<bool, string_function_t, SAX, string_t>::value &&
9753 is_detected_exact<bool, binary_function_t, SAX, binary_t>::value &&
9754 is_detected_exact<bool, start_object_function_t, SAX>::value &&
9755 is_detected_exact<bool, key_function_t, SAX, string_t>::value &&
9756 is_detected_exact<bool, end_object_function_t, SAX>::value &&
9757 is_detected_exact<bool, start_array_function_t, SAX>::value &&
9758 is_detected_exact<bool, end_array_function_t, SAX>::value &&
9759 is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value;
9760 };
9761
9762 template<typename SAX, typename BasicJsonType>
9763 struct is_sax_static_asserts
9764 {
9765 private:
9766 static_assert(is_basic_json<BasicJsonType>::value,
9767 "BasicJsonType must be of type basic_json<...>");
9768
9769 using number_integer_t = typename BasicJsonType::number_integer_t;
9770 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
9771 using number_float_t = typename BasicJsonType::number_float_t;
9772 using string_t = typename BasicJsonType::string_t;
9773 using binary_t = typename BasicJsonType::binary_t;
9774 using exception_t = typename BasicJsonType::exception;
9775
9776 public:
9777 static_assert(is_detected_exact<bool, null_function_t, SAX>::value,
9778 "Missing/invalid function: bool null()");
9779 static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
9780 "Missing/invalid function: bool boolean(bool)");
9781 static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
9782 "Missing/invalid function: bool boolean(bool)");
9783 static_assert(
9784 is_detected_exact<bool, number_integer_function_t, SAX,
9785 number_integer_t>::value,
9786 "Missing/invalid function: bool number_integer(number_integer_t)");
9787 static_assert(
9788 is_detected_exact<bool, number_unsigned_function_t, SAX,
9789 number_unsigned_t>::value,
9790 "Missing/invalid function: bool number_unsigned(number_unsigned_t)");
9791 static_assert(is_detected_exact<bool, number_float_function_t, SAX,
9792 number_float_t, string_t>::value,
9793 "Missing/invalid function: bool number_float(number_float_t, const string_t&)");
9794 static_assert(
9795 is_detected_exact<bool, string_function_t, SAX, string_t>::value,
9796 "Missing/invalid function: bool string(string_t&)");
9797 static_assert(
9798 is_detected_exact<bool, binary_function_t, SAX, binary_t>::value,
9799 "Missing/invalid function: bool binary(binary_t&)");
9800 static_assert(is_detected_exact<bool, start_object_function_t, SAX>::value,
9801 "Missing/invalid function: bool start_object(std::size_t)");
9802 static_assert(is_detected_exact<bool, key_function_t, SAX, string_t>::value,
9803 "Missing/invalid function: bool key(string_t&)");
9804 static_assert(is_detected_exact<bool, end_object_function_t, SAX>::value,
9805 "Missing/invalid function: bool end_object()");
9806 static_assert(is_detected_exact<bool, start_array_function_t, SAX>::value,
9807 "Missing/invalid function: bool start_array(std::size_t)");
9808 static_assert(is_detected_exact<bool, end_array_function_t, SAX>::value,
9809 "Missing/invalid function: bool end_array()");
9810 static_assert(
9811 is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value,
9812 "Missing/invalid function: bool parse_error(std::size_t, const "
9813 "std::string&, const exception&)");
9814 };
9815
9816 } // namespace detail
9817 NLOHMANN_JSON_NAMESPACE_END
9818
9819 // #include <nlohmann/detail/meta/type_traits.hpp>
9820
9821 // #include <nlohmann/detail/string_concat.hpp>
9822
9823 // #include <nlohmann/detail/value_t.hpp>
9824
9825
9826 NLOHMANN_JSON_NAMESPACE_BEGIN
9827 namespace detail
9828 {
9829
9830 /// how to treat CBOR tags
9831 enum class cbor_tag_handler_t
9832 {
9833 error, ///< throw a parse_error exception in case of a tag
9834 ignore, ///< ignore tags
9835 store ///< store tags as binary type
9836 };
9837
9838 /*!
9839 @brief determine system byte order
9840
9841 @return true if and only if system's byte order is little endian
9842
9843 @note from https://stackoverflow.com/a/1001328/266378
9844 */
9845 static inline bool little_endianness(int num = 1) noexcept
9846 {
9847 return *reinterpret_cast<char*>(&num) == 1;
9848 }
9849
9850 ///////////////////
9851 // binary reader //
9852 ///////////////////
9853
9854 /*!
9855 @brief deserialization of CBOR, MessagePack, and UBJSON values
9856 */
9857 template<typename BasicJsonType, typename InputAdapterType, typename SAX = json_sax_dom_parser<BasicJsonType, InputAdapterType>>
9858 class binary_reader
9859 {
9860 using number_integer_t = typename BasicJsonType::number_integer_t;
9861 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
9862 using number_float_t = typename BasicJsonType::number_float_t;
9863 using string_t = typename BasicJsonType::string_t;
9864 using binary_t = typename BasicJsonType::binary_t;
9865 using json_sax_t = SAX;
9866 using char_type = typename InputAdapterType::char_type;
9867 using char_int_type = typename char_traits<char_type>::int_type;
9868
9869 public:
9870 /*!
9871 @brief create a binary reader
9872
9873 @param[in] adapter input adapter to read from
9874 */
9875 explicit binary_reader(InputAdapterType&& adapter, const input_format_t format = input_format_t::json) noexcept : ia(std::move(adapter)), input_format(format)
9876 {
9877 (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
9878 }
9879
9880 // make class move-only
9881 binary_reader(const binary_reader&) = delete;
9882 binary_reader(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
9883 binary_reader& operator=(const binary_reader&) = delete;
9884 binary_reader& operator=(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
9885 ~binary_reader() = default;
9886
9887 /*!
9888 @param[in] format the binary format to parse
9889 @param[in] sax_ a SAX event processor
9890 @param[in] strict whether to expect the input to be consumed completed
9891 @param[in] tag_handler how to treat CBOR tags
9892
9893 @return whether parsing was successful
9894 */
9895 JSON_HEDLEY_NON_NULL(3)
9896 bool sax_parse(const input_format_t format,
9897 json_sax_t* sax_,
9898 const bool strict = true,
9899 const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
9900 {
9901 sax = sax_;
9902 bool result = false;
9903
9904 switch (format)
9905 {
9906 case input_format_t::bson:
9907 result = parse_bson_internal();
9908 break;
9909
9910 case input_format_t::cbor:
9911 result = parse_cbor_internal(true, tag_handler);
9912 break;
9913
9914 case input_format_t::msgpack:
9915 result = parse_msgpack_internal();
9916 break;
9917
9918 case input_format_t::ubjson:
9919 case input_format_t::bjdata:
9920 result = parse_ubjson_internal();
9921 break;
9922
9923 case input_format_t::json: // LCOV_EXCL_LINE
9924 default: // LCOV_EXCL_LINE
9925 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
9926 }
9927
9928 // strict mode: next byte must be EOF
9929 if (result && strict)
9930 {
9931 if (input_format == input_format_t::ubjson || input_format == input_format_t::bjdata)
9932 {
9933 get_ignore_noop();
9934 }
9935 else
9936 {
9937 get();
9938 }
9939
9940 if (JSON_HEDLEY_UNLIKELY(current != char_traits<char_type>::eof()))
9941 {
9942 return sax->parse_error(chars_read, get_token_string(), parse_error::create(110, chars_read,
9943 exception_message(input_format, concat("expected end of input; last byte: 0x", get_token_string()), "value"), nullptr));
9944 }
9945 }
9946
9947 return result;
9948 }
9949
9950 private:
9951 //////////
9952 // BSON //
9953 //////////
9954
9955 /*!
9956 @brief Reads in a BSON-object and passes it to the SAX-parser.
9957 @return whether a valid BSON-value was passed to the SAX parser
9958 */
9959 bool parse_bson_internal()
9960 {
9961 std::int32_t document_size{};
9962 get_number<std::int32_t, true>(input_format_t::bson, document_size);
9963
9964 if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
9965 {
9966 return false;
9967 }
9968
9969 if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/false)))
9970 {
9971 return false;
9972 }
9973
9974 return sax->end_object();
9975 }
9976
9977 /*!
9978 @brief Parses a C-style string from the BSON input.
9979 @param[in,out] result A reference to the string variable where the read
9980 string is to be stored.
9981 @return `true` if the \x00-byte indicating the end of the string was
9982 encountered before the EOF; false` indicates an unexpected EOF.
9983 */
9984 bool get_bson_cstr(string_t& result)
9985 {
9986 auto out = std::back_inserter(result);
9987 while (true)
9988 {
9989 get();
9990 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "cstring")))
9991 {
9992 return false;
9993 }
9994 if (current == 0x00)
9995 {
9996 return true;
9997 }
9998 *out++ = static_cast<typename string_t::value_type>(current);
9999 }
10000 }
10001
10002 /*!
10003 @brief Parses a zero-terminated string of length @a len from the BSON
10004 input.
10005 @param[in] len The length (including the zero-byte at the end) of the
10006 string to be read.
10007 @param[in,out] result A reference to the string variable where the read
10008 string is to be stored.
10009 @tparam NumberType The type of the length @a len
10010 @pre len >= 1
10011 @return `true` if the string was successfully parsed
10012 */
10013 template<typename NumberType>
10014 bool get_bson_string(const NumberType len, string_t& result)
10015 {
10016 if (JSON_HEDLEY_UNLIKELY(len < 1))
10017 {
10018 auto last_token = get_token_string();
10019 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
10020 exception_message(input_format_t::bson, concat("string length must be at least 1, is ", std::to_string(len)), "string"), nullptr));
10021 }
10022
10023 return get_string(input_format_t::bson, len - static_cast<NumberType>(1), result) && get() != char_traits<char_type>::eof();
10024 }
10025
10026 /*!
10027 @brief Parses a byte array input of length @a len from the BSON input.
10028 @param[in] len The length of the byte array to be read.
10029 @param[in,out] result A reference to the binary variable where the read
10030 array is to be stored.
10031 @tparam NumberType The type of the length @a len
10032 @pre len >= 0
10033 @return `true` if the byte array was successfully parsed
10034 */
10035 template<typename NumberType>
10036 bool get_bson_binary(const NumberType len, binary_t& result)
10037 {
10038 if (JSON_HEDLEY_UNLIKELY(len < 0))
10039 {
10040 auto last_token = get_token_string();
10041 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
10042 exception_message(input_format_t::bson, concat("byte array length cannot be negative, is ", std::to_string(len)), "binary"), nullptr));
10043 }
10044
10045 // All BSON binary values have a subtype
10046 std::uint8_t subtype{};
10047 get_number<std::uint8_t>(input_format_t::bson, subtype);
10048 result.set_subtype(subtype);
10049
10050 return get_binary(input_format_t::bson, len, result);
10051 }
10052
10053 /*!
10054 @brief Read a BSON document element of the given @a element_type.
10055 @param[in] element_type The BSON element type, c.f. http://bsonspec.org/spec.html
10056 @param[in] element_type_parse_position The position in the input stream,
10057 where the `element_type` was read.
10058 @warning Not all BSON element types are supported yet. An unsupported
10059 @a element_type will give rise to a parse_error.114:
10060 Unsupported BSON record type 0x...
10061 @return whether a valid BSON-object/array was passed to the SAX parser
10062 */
10063 bool parse_bson_element_internal(const char_int_type element_type,
10064 const std::size_t element_type_parse_position)
10065 {
10066 switch (element_type)
10067 {
10068 case 0x01: // double
10069 {
10070 double number{};
10071 return get_number<double, true>(input_format_t::bson, number) && sax->number_float(static_cast<number_float_t>(number), "");
10072 }
10073
10074 case 0x02: // string
10075 {
10076 std::int32_t len{};
10077 string_t value;
10078 return get_number<std::int32_t, true>(input_format_t::bson, len) && get_bson_string(len, value) && sax->string(value);
10079 }
10080
10081 case 0x03: // object
10082 {
10083 return parse_bson_internal();
10084 }
10085
10086 case 0x04: // array
10087 {
10088 return parse_bson_array();
10089 }
10090
10091 case 0x05: // binary
10092 {
10093 std::int32_t len{};
10094 binary_t value;
10095 return get_number<std::int32_t, true>(input_format_t::bson, len) && get_bson_binary(len, value) && sax->binary(value);
10096 }
10097
10098 case 0x08: // boolean
10099 {
10100 return sax->boolean(get() != 0);
10101 }
10102
10103 case 0x0A: // null
10104 {
10105 return sax->null();
10106 }
10107
10108 case 0x10: // int32
10109 {
10110 std::int32_t value{};
10111 return get_number<std::int32_t, true>(input_format_t::bson, value) && sax->number_integer(value);
10112 }
10113
10114 case 0x12: // int64
10115 {
10116 std::int64_t value{};
10117 return get_number<std::int64_t, true>(input_format_t::bson, value) && sax->number_integer(value);
10118 }
10119
10120 case 0x11: // uint64
10121 {
10122 std::uint64_t value{};
10123 return get_number<std::uint64_t, true>(input_format_t::bson, value) && sax->number_unsigned(value);
10124 }
10125
10126 default: // anything else not supported (yet)
10127 {
10128 std::array<char, 3> cr{{}};
10129 static_cast<void>((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(element_type))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
10130 const std::string cr_str{cr.data()};
10131 return sax->parse_error(element_type_parse_position, cr_str,
10132 parse_error::create(114, element_type_parse_position, concat("Unsupported BSON record type 0x", cr_str), nullptr));
10133 }
10134 }
10135 }
10136
10137 /*!
10138 @brief Read a BSON element list (as specified in the BSON-spec)
10139
10140 The same binary layout is used for objects and arrays, hence it must be
10141 indicated with the argument @a is_array which one is expected
10142 (true --> array, false --> object).
10143
10144 @param[in] is_array Determines if the element list being read is to be
10145 treated as an object (@a is_array == false), or as an
10146 array (@a is_array == true).
10147 @return whether a valid BSON-object/array was passed to the SAX parser
10148 */
10149 bool parse_bson_element_list(const bool is_array)
10150 {
10151 string_t key;
10152
10153 while (auto element_type = get())
10154 {
10155 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "element list")))
10156 {
10157 return false;
10158 }
10159
10160 const std::size_t element_type_parse_position = chars_read;
10161 if (JSON_HEDLEY_UNLIKELY(!get_bson_cstr(key)))
10162 {
10163 return false;
10164 }
10165
10166 if (!is_array && !sax->key(key))
10167 {
10168 return false;
10169 }
10170
10171 if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_internal(element_type, element_type_parse_position)))
10172 {
10173 return false;
10174 }
10175
10176 // get_bson_cstr only appends
10177 key.clear();
10178 }
10179
10180 return true;
10181 }
10182
10183 /*!
10184 @brief Reads an array from the BSON input and passes it to the SAX-parser.
10185 @return whether a valid BSON-array was passed to the SAX parser
10186 */
10187 bool parse_bson_array()
10188 {
10189 std::int32_t document_size{};
10190 get_number<std::int32_t, true>(input_format_t::bson, document_size);
10191
10192 if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
10193 {
10194 return false;
10195 }
10196
10197 if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/true)))
10198 {
10199 return false;
10200 }
10201
10202 return sax->end_array();
10203 }
10204
10205 //////////
10206 // CBOR //
10207 //////////
10208
10209 /*!
10210 @param[in] get_char whether a new character should be retrieved from the
10211 input (true) or whether the last read character should
10212 be considered instead (false)
10213 @param[in] tag_handler how CBOR tags should be treated
10214
10215 @return whether a valid CBOR value was passed to the SAX parser
10216 */
10217 bool parse_cbor_internal(const bool get_char,
10218 const cbor_tag_handler_t tag_handler)
10219 {
10220 switch (get_char ? get() : current)
10221 {
10222 // EOF
10223 case char_traits<char_type>::eof():
10224 return unexpect_eof(input_format_t::cbor, "value");
10225
10226 // Integer 0x00..0x17 (0..23)
10227 case 0x00:
10228 case 0x01:
10229 case 0x02:
10230 case 0x03:
10231 case 0x04:
10232 case 0x05:
10233 case 0x06:
10234 case 0x07:
10235 case 0x08:
10236 case 0x09:
10237 case 0x0A:
10238 case 0x0B:
10239 case 0x0C:
10240 case 0x0D:
10241 case 0x0E:
10242 case 0x0F:
10243 case 0x10:
10244 case 0x11:
10245 case 0x12:
10246 case 0x13:
10247 case 0x14:
10248 case 0x15:
10249 case 0x16:
10250 case 0x17:
10251 return sax->number_unsigned(static_cast<number_unsigned_t>(current));
10252
10253 case 0x18: // Unsigned integer (one-byte uint8_t follows)
10254 {
10255 std::uint8_t number{};
10256 return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
10257 }
10258
10259 case 0x19: // Unsigned integer (two-byte uint16_t follows)
10260 {
10261 std::uint16_t number{};
10262 return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
10263 }
10264
10265 case 0x1A: // Unsigned integer (four-byte uint32_t follows)
10266 {
10267 std::uint32_t number{};
10268 return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
10269 }
10270
10271 case 0x1B: // Unsigned integer (eight-byte uint64_t follows)
10272 {
10273 std::uint64_t number{};
10274 return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
10275 }
10276
10277 // Negative integer -1-0x00..-1-0x17 (-1..-24)
10278 case 0x20:
10279 case 0x21:
10280 case 0x22:
10281 case 0x23:
10282 case 0x24:
10283 case 0x25:
10284 case 0x26:
10285 case 0x27:
10286 case 0x28:
10287 case 0x29:
10288 case 0x2A:
10289 case 0x2B:
10290 case 0x2C:
10291 case 0x2D:
10292 case 0x2E:
10293 case 0x2F:
10294 case 0x30:
10295 case 0x31:
10296 case 0x32:
10297 case 0x33:
10298 case 0x34:
10299 case 0x35:
10300 case 0x36:
10301 case 0x37:
10302 return sax->number_integer(static_cast<std::int8_t>(0x20 - 1 - current));
10303
10304 case 0x38: // Negative integer (one-byte uint8_t follows)
10305 {
10306 std::uint8_t number{};
10307 return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
10308 }
10309
10310 case 0x39: // Negative integer -1-n (two-byte uint16_t follows)
10311 {
10312 std::uint16_t number{};
10313 return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
10314 }
10315
10316 case 0x3A: // Negative integer -1-n (four-byte uint32_t follows)
10317 {
10318 std::uint32_t number{};
10319 return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
10320 }
10321
10322 case 0x3B: // Negative integer -1-n (eight-byte uint64_t follows)
10323 {
10324 std::uint64_t number{};
10325 return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1)
10326 - static_cast<number_integer_t>(number));
10327 }
10328
10329 // Binary data (0x00..0x17 bytes follow)
10330 case 0x40:
10331 case 0x41:
10332 case 0x42:
10333 case 0x43:
10334 case 0x44:
10335 case 0x45:
10336 case 0x46:
10337 case 0x47:
10338 case 0x48:
10339 case 0x49:
10340 case 0x4A:
10341 case 0x4B:
10342 case 0x4C:
10343 case 0x4D:
10344 case 0x4E:
10345 case 0x4F:
10346 case 0x50:
10347 case 0x51:
10348 case 0x52:
10349 case 0x53:
10350 case 0x54:
10351 case 0x55:
10352 case 0x56:
10353 case 0x57:
10354 case 0x58: // Binary data (one-byte uint8_t for n follows)
10355 case 0x59: // Binary data (two-byte uint16_t for n follow)
10356 case 0x5A: // Binary data (four-byte uint32_t for n follow)
10357 case 0x5B: // Binary data (eight-byte uint64_t for n follow)
10358 case 0x5F: // Binary data (indefinite length)
10359 {
10360 binary_t b;
10361 return get_cbor_binary(b) && sax->binary(b);
10362 }
10363
10364 // UTF-8 string (0x00..0x17 bytes follow)
10365 case 0x60:
10366 case 0x61:
10367 case 0x62:
10368 case 0x63:
10369 case 0x64:
10370 case 0x65:
10371 case 0x66:
10372 case 0x67:
10373 case 0x68:
10374 case 0x69:
10375 case 0x6A:
10376 case 0x6B:
10377 case 0x6C:
10378 case 0x6D:
10379 case 0x6E:
10380 case 0x6F:
10381 case 0x70:
10382 case 0x71:
10383 case 0x72:
10384 case 0x73:
10385 case 0x74:
10386 case 0x75:
10387 case 0x76:
10388 case 0x77:
10389 case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
10390 case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
10391 case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
10392 case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
10393 case 0x7F: // UTF-8 string (indefinite length)
10394 {
10395 string_t s;
10396 return get_cbor_string(s) && sax->string(s);
10397 }
10398
10399 // array (0x00..0x17 data items follow)
10400 case 0x80:
10401 case 0x81:
10402 case 0x82:
10403 case 0x83:
10404 case 0x84:
10405 case 0x85:
10406 case 0x86:
10407 case 0x87:
10408 case 0x88:
10409 case 0x89:
10410 case 0x8A:
10411 case 0x8B:
10412 case 0x8C:
10413 case 0x8D:
10414 case 0x8E:
10415 case 0x8F:
10416 case 0x90:
10417 case 0x91:
10418 case 0x92:
10419 case 0x93:
10420 case 0x94:
10421 case 0x95:
10422 case 0x96:
10423 case 0x97:
10424 return get_cbor_array(
10425 conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
10426
10427 case 0x98: // array (one-byte uint8_t for n follows)
10428 {
10429 std::uint8_t len{};
10430 return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
10431 }
10432
10433 case 0x99: // array (two-byte uint16_t for n follow)
10434 {
10435 std::uint16_t len{};
10436 return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
10437 }
10438
10439 case 0x9A: // array (four-byte uint32_t for n follow)
10440 {
10441 std::uint32_t len{};
10442 return get_number(input_format_t::cbor, len) && get_cbor_array(conditional_static_cast<std::size_t>(len), tag_handler);
10443 }
10444
10445 case 0x9B: // array (eight-byte uint64_t for n follow)
10446 {
10447 std::uint64_t len{};
10448 return get_number(input_format_t::cbor, len) && get_cbor_array(conditional_static_cast<std::size_t>(len), tag_handler);
10449 }
10450
10451 case 0x9F: // array (indefinite length)
10452 return get_cbor_array(detail::unknown_size(), tag_handler);
10453
10454 // map (0x00..0x17 pairs of data items follow)
10455 case 0xA0:
10456 case 0xA1:
10457 case 0xA2:
10458 case 0xA3:
10459 case 0xA4:
10460 case 0xA5:
10461 case 0xA6:
10462 case 0xA7:
10463 case 0xA8:
10464 case 0xA9:
10465 case 0xAA:
10466 case 0xAB:
10467 case 0xAC:
10468 case 0xAD:
10469 case 0xAE:
10470 case 0xAF:
10471 case 0xB0:
10472 case 0xB1:
10473 case 0xB2:
10474 case 0xB3:
10475 case 0xB4:
10476 case 0xB5:
10477 case 0xB6:
10478 case 0xB7:
10479 return get_cbor_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
10480
10481 case 0xB8: // map (one-byte uint8_t for n follows)
10482 {
10483 std::uint8_t len{};
10484 return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
10485 }
10486
10487 case 0xB9: // map (two-byte uint16_t for n follow)
10488 {
10489 std::uint16_t len{};
10490 return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
10491 }
10492
10493 case 0xBA: // map (four-byte uint32_t for n follow)
10494 {
10495 std::uint32_t len{};
10496 return get_number(input_format_t::cbor, len) && get_cbor_object(conditional_static_cast<std::size_t>(len), tag_handler);
10497 }
10498
10499 case 0xBB: // map (eight-byte uint64_t for n follow)
10500 {
10501 std::uint64_t len{};
10502 return get_number(input_format_t::cbor, len) && get_cbor_object(conditional_static_cast<std::size_t>(len), tag_handler);
10503 }
10504
10505 case 0xBF: // map (indefinite length)
10506 return get_cbor_object(detail::unknown_size(), tag_handler);
10507
10508 case 0xC6: // tagged item
10509 case 0xC7:
10510 case 0xC8:
10511 case 0xC9:
10512 case 0xCA:
10513 case 0xCB:
10514 case 0xCC:
10515 case 0xCD:
10516 case 0xCE:
10517 case 0xCF:
10518 case 0xD0:
10519 case 0xD1:
10520 case 0xD2:
10521 case 0xD3:
10522 case 0xD4:
10523 case 0xD8: // tagged item (1 bytes follow)
10524 case 0xD9: // tagged item (2 bytes follow)
10525 case 0xDA: // tagged item (4 bytes follow)
10526 case 0xDB: // tagged item (8 bytes follow)
10527 {
10528 switch (tag_handler)
10529 {
10530 case cbor_tag_handler_t::error:
10531 {
10532 auto last_token = get_token_string();
10533 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
10534 exception_message(input_format_t::cbor, concat("invalid byte: 0x", last_token), "value"), nullptr));
10535 }
10536
10537 case cbor_tag_handler_t::ignore:
10538 {
10539 // ignore binary subtype
10540 switch (current)
10541 {
10542 case 0xD8:
10543 {
10544 std::uint8_t subtype_to_ignore{};
10545 get_number(input_format_t::cbor, subtype_to_ignore);
10546 break;
10547 }
10548 case 0xD9:
10549 {
10550 std::uint16_t subtype_to_ignore{};
10551 get_number(input_format_t::cbor, subtype_to_ignore);
10552 break;
10553 }
10554 case 0xDA:
10555 {
10556 std::uint32_t subtype_to_ignore{};
10557 get_number(input_format_t::cbor, subtype_to_ignore);
10558 break;
10559 }
10560 case 0xDB:
10561 {
10562 std::uint64_t subtype_to_ignore{};
10563 get_number(input_format_t::cbor, subtype_to_ignore);
10564 break;
10565 }
10566 default:
10567 break;
10568 }
10569 return parse_cbor_internal(true, tag_handler);
10570 }
10571
10572 case cbor_tag_handler_t::store:
10573 {
10574 binary_t b;
10575 // use binary subtype and store in binary container
10576 switch (current)
10577 {
10578 case 0xD8:
10579 {
10580 std::uint8_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 0xD9:
10586 {
10587 std::uint16_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 case 0xDA:
10593 {
10594 std::uint32_t subtype{};
10595 get_number(input_format_t::cbor, subtype);
10596 b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
10597 break;
10598 }
10599 case 0xDB:
10600 {
10601 std::uint64_t subtype{};
10602 get_number(input_format_t::cbor, subtype);
10603 b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
10604 break;
10605 }
10606 default:
10607 return parse_cbor_internal(true, tag_handler);
10608 }
10609 get();
10610 return get_cbor_binary(b) && sax->binary(b);
10611 }
10612
10613 default: // LCOV_EXCL_LINE
10614 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
10615 return false; // LCOV_EXCL_LINE
10616 }
10617 }
10618
10619 case 0xF4: // false
10620 return sax->boolean(false);
10621
10622 case 0xF5: // true
10623 return sax->boolean(true);
10624
10625 case 0xF6: // null
10626 return sax->null();
10627
10628 case 0xF9: // Half-Precision Float (two-byte IEEE 754)
10629 {
10630 const auto byte1_raw = get();
10631 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
10632 {
10633 return false;
10634 }
10635 const auto byte2_raw = get();
10636 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
10637 {
10638 return false;
10639 }
10640
10641 const auto byte1 = static_cast<unsigned char>(byte1_raw);
10642 const auto byte2 = static_cast<unsigned char>(byte2_raw);
10643
10644 // code from RFC 7049, Appendix D, Figure 3:
10645 // As half-precision floating-point numbers were only added
10646 // to IEEE 754 in 2008, today's programming platforms often
10647 // still only have limited support for them. It is very
10648 // easy to include at least decoding support for them even
10649 // without such support. An example of a small decoder for
10650 // half-precision floating-point numbers in the C language
10651 // is shown in Fig. 3.
10652 const auto half = static_cast<unsigned int>((byte1 << 8u) + byte2);
10653 const double val = [&half]
10654 {
10655 const int exp = (half >> 10u) & 0x1Fu;
10656 const unsigned int mant = half & 0x3FFu;
10657 JSON_ASSERT(0 <= exp&& exp <= 32);
10658 JSON_ASSERT(mant <= 1024);
10659 switch (exp)
10660 {
10661 case 0:
10662 return std::ldexp(mant, -24);
10663 case 31:
10664 return (mant == 0)
10665 ? std::numeric_limits<double>::infinity()
10666 : std::numeric_limits<double>::quiet_NaN();
10667 default:
10668 return std::ldexp(mant + 1024, exp - 25);
10669 }
10670 }();
10671 return sax->number_float((half & 0x8000u) != 0
10672 ? static_cast<number_float_t>(-val)
10673 : static_cast<number_float_t>(val), "");
10674 }
10675
10676 case 0xFA: // Single-Precision Float (four-byte IEEE 754)
10677 {
10678 float number{};
10679 return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
10680 }
10681
10682 case 0xFB: // Double-Precision Float (eight-byte IEEE 754)
10683 {
10684 double number{};
10685 return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
10686 }
10687
10688 default: // anything else (0xFF is handled inside the other types)
10689 {
10690 auto last_token = get_token_string();
10691 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
10692 exception_message(input_format_t::cbor, concat("invalid byte: 0x", last_token), "value"), nullptr));
10693 }
10694 }
10695 }
10696
10697 /*!
10698 @brief reads a CBOR string
10699
10700 This function first reads starting bytes to determine the expected
10701 string length and then copies this number of bytes into a string.
10702 Additionally, CBOR's strings with indefinite lengths are supported.
10703
10704 @param[out] result created string
10705
10706 @return whether string creation completed
10707 */
10708 bool get_cbor_string(string_t& result)
10709 {
10710 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "string")))
10711 {
10712 return false;
10713 }
10714
10715 switch (current)
10716 {
10717 // UTF-8 string (0x00..0x17 bytes follow)
10718 case 0x60:
10719 case 0x61:
10720 case 0x62:
10721 case 0x63:
10722 case 0x64:
10723 case 0x65:
10724 case 0x66:
10725 case 0x67:
10726 case 0x68:
10727 case 0x69:
10728 case 0x6A:
10729 case 0x6B:
10730 case 0x6C:
10731 case 0x6D:
10732 case 0x6E:
10733 case 0x6F:
10734 case 0x70:
10735 case 0x71:
10736 case 0x72:
10737 case 0x73:
10738 case 0x74:
10739 case 0x75:
10740 case 0x76:
10741 case 0x77:
10742 {
10743 return get_string(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
10744 }
10745
10746 case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
10747 {
10748 std::uint8_t len{};
10749 return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
10750 }
10751
10752 case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
10753 {
10754 std::uint16_t len{};
10755 return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
10756 }
10757
10758 case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
10759 {
10760 std::uint32_t len{};
10761 return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
10762 }
10763
10764 case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
10765 {
10766 std::uint64_t len{};
10767 return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
10768 }
10769
10770 case 0x7F: // UTF-8 string (indefinite length)
10771 {
10772 while (get() != 0xFF)
10773 {
10774 string_t chunk;
10775 if (!get_cbor_string(chunk))
10776 {
10777 return false;
10778 }
10779 result.append(chunk);
10780 }
10781 return true;
10782 }
10783
10784 default:
10785 {
10786 auto last_token = get_token_string();
10787 return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
10788 exception_message(input_format_t::cbor, concat("expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x", last_token), "string"), nullptr));
10789 }
10790 }
10791 }
10792
10793 /*!
10794 @brief reads a CBOR byte array
10795
10796 This function first reads starting bytes to determine the expected
10797 byte array length and then copies this number of bytes into the byte array.
10798 Additionally, CBOR's byte arrays with indefinite lengths are supported.
10799
10800 @param[out] result created byte array
10801
10802 @return whether byte array creation completed
10803 */
10804 bool get_cbor_binary(binary_t& result)
10805 {
10806 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "binary")))
10807 {
10808 return false;
10809 }
10810
10811 switch (current)
10812 {
10813 // Binary data (0x00..0x17 bytes follow)
10814 case 0x40:
10815 case 0x41:
10816 case 0x42:
10817 case 0x43:
10818 case 0x44:
10819 case 0x45:
10820 case 0x46:
10821 case 0x47:
10822 case 0x48:
10823 case 0x49:
10824 case 0x4A:
10825 case 0x4B:
10826 case 0x4C:
10827 case 0x4D:
10828 case 0x4E:
10829 case 0x4F:
10830 case 0x50:
10831 case 0x51:
10832 case 0x52:
10833 case 0x53:
10834 case 0x54:
10835 case 0x55:
10836 case 0x56:
10837 case 0x57:
10838 {
10839 return get_binary(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
10840 }
10841
10842 case 0x58: // Binary data (one-byte uint8_t for n follows)
10843 {
10844 std::uint8_t len{};
10845 return get_number(input_format_t::cbor, len) &&
10846 get_binary(input_format_t::cbor, len, result);
10847 }
10848
10849 case 0x59: // Binary data (two-byte uint16_t for n follow)
10850 {
10851 std::uint16_t len{};
10852 return get_number(input_format_t::cbor, len) &&
10853 get_binary(input_format_t::cbor, len, result);
10854 }
10855
10856 case 0x5A: // Binary data (four-byte uint32_t for n follow)
10857 {
10858 std::uint32_t len{};
10859 return get_number(input_format_t::cbor, len) &&
10860 get_binary(input_format_t::cbor, len, result);
10861 }
10862
10863 case 0x5B: // Binary data (eight-byte uint64_t for n follow)
10864 {
10865 std::uint64_t len{};
10866 return get_number(input_format_t::cbor, len) &&
10867 get_binary(input_format_t::cbor, len, result);
10868 }
10869
10870 case 0x5F: // Binary data (indefinite length)
10871 {
10872 while (get() != 0xFF)
10873 {
10874 binary_t chunk;
10875 if (!get_cbor_binary(chunk))
10876 {
10877 return false;
10878 }
10879 result.insert(result.end(), chunk.begin(), chunk.end());
10880 }
10881 return true;
10882 }
10883
10884 default:
10885 {
10886 auto last_token = get_token_string();
10887 return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
10888 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));
10889 }
10890 }
10891 }
10892
10893 /*!
10894 @param[in] len the length of the array or detail::unknown_size() for an
10895 array of indefinite size
10896 @param[in] tag_handler how CBOR tags should be treated
10897 @return whether array creation completed
10898 */
10899 bool get_cbor_array(const std::size_t len,
10900 const cbor_tag_handler_t tag_handler)
10901 {
10902 if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
10903 {
10904 return false;
10905 }
10906
10907 if (len != detail::unknown_size())
10908 {
10909 for (std::size_t i = 0; i < len; ++i)
10910 {
10911 if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
10912 {
10913 return false;
10914 }
10915 }
10916 }
10917 else
10918 {
10919 while (get() != 0xFF)
10920 {
10921 if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(false, tag_handler)))
10922 {
10923 return false;
10924 }
10925 }
10926 }
10927
10928 return sax->end_array();
10929 }
10930
10931 /*!
10932 @param[in] len the length of the object or detail::unknown_size() for an
10933 object of indefinite size
10934 @param[in] tag_handler how CBOR tags should be treated
10935 @return whether object creation completed
10936 */
10937 bool get_cbor_object(const std::size_t len,
10938 const cbor_tag_handler_t tag_handler)
10939 {
10940 if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
10941 {
10942 return false;
10943 }
10944
10945 if (len != 0)
10946 {
10947 string_t key;
10948 if (len != detail::unknown_size())
10949 {
10950 for (std::size_t i = 0; i < len; ++i)
10951 {
10952 get();
10953 if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
10954 {
10955 return false;
10956 }
10957
10958 if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
10959 {
10960 return false;
10961 }
10962 key.clear();
10963 }
10964 }
10965 else
10966 {
10967 while (get() != 0xFF)
10968 {
10969 if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
10970 {
10971 return false;
10972 }
10973
10974 if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
10975 {
10976 return false;
10977 }
10978 key.clear();
10979 }
10980 }
10981 }
10982
10983 return sax->end_object();
10984 }
10985
10986 /////////////
10987 // MsgPack //
10988 /////////////
10989
10990 /*!
10991 @return whether a valid MessagePack value was passed to the SAX parser
10992 */
10993 bool parse_msgpack_internal()
10994 {
10995 switch (get())
10996 {
10997 // EOF
10998 case char_traits<char_type>::eof():
10999 return unexpect_eof(input_format_t::msgpack, "value");
11000
11001 // positive fixint
11002 case 0x00:
11003 case 0x01:
11004 case 0x02:
11005 case 0x03:
11006 case 0x04:
11007 case 0x05:
11008 case 0x06:
11009 case 0x07:
11010 case 0x08:
11011 case 0x09:
11012 case 0x0A:
11013 case 0x0B:
11014 case 0x0C:
11015 case 0x0D:
11016 case 0x0E:
11017 case 0x0F:
11018 case 0x10:
11019 case 0x11:
11020 case 0x12:
11021 case 0x13:
11022 case 0x14:
11023 case 0x15:
11024 case 0x16:
11025 case 0x17:
11026 case 0x18:
11027 case 0x19:
11028 case 0x1A:
11029 case 0x1B:
11030 case 0x1C:
11031 case 0x1D:
11032 case 0x1E:
11033 case 0x1F:
11034 case 0x20:
11035 case 0x21:
11036 case 0x22:
11037 case 0x23:
11038 case 0x24:
11039 case 0x25:
11040 case 0x26:
11041 case 0x27:
11042 case 0x28:
11043 case 0x29:
11044 case 0x2A:
11045 case 0x2B:
11046 case 0x2C:
11047 case 0x2D:
11048 case 0x2E:
11049 case 0x2F:
11050 case 0x30:
11051 case 0x31:
11052 case 0x32:
11053 case 0x33:
11054 case 0x34:
11055 case 0x35:
11056 case 0x36:
11057 case 0x37:
11058 case 0x38:
11059 case 0x39:
11060 case 0x3A:
11061 case 0x3B:
11062 case 0x3C:
11063 case 0x3D:
11064 case 0x3E:
11065 case 0x3F:
11066 case 0x40:
11067 case 0x41:
11068 case 0x42:
11069 case 0x43:
11070 case 0x44:
11071 case 0x45:
11072 case 0x46:
11073 case 0x47:
11074 case 0x48:
11075 case 0x49:
11076 case 0x4A:
11077 case 0x4B:
11078 case 0x4C:
11079 case 0x4D:
11080 case 0x4E:
11081 case 0x4F:
11082 case 0x50:
11083 case 0x51:
11084 case 0x52:
11085 case 0x53:
11086 case 0x54:
11087 case 0x55:
11088 case 0x56:
11089 case 0x57:
11090 case 0x58:
11091 case 0x59:
11092 case 0x5A:
11093 case 0x5B:
11094 case 0x5C:
11095 case 0x5D:
11096 case 0x5E:
11097 case 0x5F:
11098 case 0x60:
11099 case 0x61:
11100 case 0x62:
11101 case 0x63:
11102 case 0x64:
11103 case 0x65:
11104 case 0x66:
11105 case 0x67:
11106 case 0x68:
11107 case 0x69:
11108 case 0x6A:
11109 case 0x6B:
11110 case 0x6C:
11111 case 0x6D:
11112 case 0x6E:
11113 case 0x6F:
11114 case 0x70:
11115 case 0x71:
11116 case 0x72:
11117 case 0x73:
11118 case 0x74:
11119 case 0x75:
11120 case 0x76:
11121 case 0x77:
11122 case 0x78:
11123 case 0x79:
11124 case 0x7A:
11125 case 0x7B:
11126 case 0x7C:
11127 case 0x7D:
11128 case 0x7E:
11129 case 0x7F:
11130 return sax->number_unsigned(static_cast<number_unsigned_t>(current));
11131
11132 // fixmap
11133 case 0x80:
11134 case 0x81:
11135 case 0x82:
11136 case 0x83:
11137 case 0x84:
11138 case 0x85:
11139 case 0x86:
11140 case 0x87:
11141 case 0x88:
11142 case 0x89:
11143 case 0x8A:
11144 case 0x8B:
11145 case 0x8C:
11146 case 0x8D:
11147 case 0x8E:
11148 case 0x8F:
11149 return get_msgpack_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
11150
11151 // fixarray
11152 case 0x90:
11153 case 0x91:
11154 case 0x92:
11155 case 0x93:
11156 case 0x94:
11157 case 0x95:
11158 case 0x96:
11159 case 0x97:
11160 case 0x98:
11161 case 0x99:
11162 case 0x9A:
11163 case 0x9B:
11164 case 0x9C:
11165 case 0x9D:
11166 case 0x9E:
11167 case 0x9F:
11168 return get_msgpack_array(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
11169
11170 // fixstr
11171 case 0xA0:
11172 case 0xA1:
11173 case 0xA2:
11174 case 0xA3:
11175 case 0xA4:
11176 case 0xA5:
11177 case 0xA6:
11178 case 0xA7:
11179 case 0xA8:
11180 case 0xA9:
11181 case 0xAA:
11182 case 0xAB:
11183 case 0xAC:
11184 case 0xAD:
11185 case 0xAE:
11186 case 0xAF:
11187 case 0xB0:
11188 case 0xB1:
11189 case 0xB2:
11190 case 0xB3:
11191 case 0xB4:
11192 case 0xB5:
11193 case 0xB6:
11194 case 0xB7:
11195 case 0xB8:
11196 case 0xB9:
11197 case 0xBA:
11198 case 0xBB:
11199 case 0xBC:
11200 case 0xBD:
11201 case 0xBE:
11202 case 0xBF:
11203 case 0xD9: // str 8
11204 case 0xDA: // str 16
11205 case 0xDB: // str 32
11206 {
11207 string_t s;
11208 return get_msgpack_string(s) && sax->string(s);
11209 }
11210
11211 case 0xC0: // nil
11212 return sax->null();
11213
11214 case 0xC2: // false
11215 return sax->boolean(false);
11216
11217 case 0xC3: // true
11218 return sax->boolean(true);
11219
11220 case 0xC4: // bin 8
11221 case 0xC5: // bin 16
11222 case 0xC6: // bin 32
11223 case 0xC7: // ext 8
11224 case 0xC8: // ext 16
11225 case 0xC9: // ext 32
11226 case 0xD4: // fixext 1
11227 case 0xD5: // fixext 2
11228 case 0xD6: // fixext 4
11229 case 0xD7: // fixext 8
11230 case 0xD8: // fixext 16
11231 {
11232 binary_t b;
11233 return get_msgpack_binary(b) && sax->binary(b);
11234 }
11235
11236 case 0xCA: // float 32
11237 {
11238 float number{};
11239 return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast<number_float_t>(number), "");
11240 }
11241
11242 case 0xCB: // float 64
11243 {
11244 double number{};
11245 return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast<number_float_t>(number), "");
11246 }
11247
11248 case 0xCC: // uint 8
11249 {
11250 std::uint8_t number{};
11251 return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
11252 }
11253
11254 case 0xCD: // uint 16
11255 {
11256 std::uint16_t number{};
11257 return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
11258 }
11259
11260 case 0xCE: // uint 32
11261 {
11262 std::uint32_t number{};
11263 return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
11264 }
11265
11266 case 0xCF: // uint 64
11267 {
11268 std::uint64_t number{};
11269 return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
11270 }
11271
11272 case 0xD0: // int 8
11273 {
11274 std::int8_t number{};
11275 return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
11276 }
11277
11278 case 0xD1: // int 16
11279 {
11280 std::int16_t number{};
11281 return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
11282 }
11283
11284 case 0xD2: // int 32
11285 {
11286 std::int32_t number{};
11287 return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
11288 }
11289
11290 case 0xD3: // int 64
11291 {
11292 std::int64_t number{};
11293 return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
11294 }
11295
11296 case 0xDC: // array 16
11297 {
11298 std::uint16_t len{};
11299 return get_number(input_format_t::msgpack, len) && get_msgpack_array(static_cast<std::size_t>(len));
11300 }
11301
11302 case 0xDD: // array 32
11303 {
11304 std::uint32_t len{};
11305 return get_number(input_format_t::msgpack, len) && get_msgpack_array(conditional_static_cast<std::size_t>(len));
11306 }
11307
11308 case 0xDE: // map 16
11309 {
11310 std::uint16_t len{};
11311 return get_number(input_format_t::msgpack, len) && get_msgpack_object(static_cast<std::size_t>(len));
11312 }
11313
11314 case 0xDF: // map 32
11315 {
11316 std::uint32_t len{};
11317 return get_number(input_format_t::msgpack, len) && get_msgpack_object(conditional_static_cast<std::size_t>(len));
11318 }
11319
11320 // negative fixint
11321 case 0xE0:
11322 case 0xE1:
11323 case 0xE2:
11324 case 0xE3:
11325 case 0xE4:
11326 case 0xE5:
11327 case 0xE6:
11328 case 0xE7:
11329 case 0xE8:
11330 case 0xE9:
11331 case 0xEA:
11332 case 0xEB:
11333 case 0xEC:
11334 case 0xED:
11335 case 0xEE:
11336 case 0xEF:
11337 case 0xF0:
11338 case 0xF1:
11339 case 0xF2:
11340 case 0xF3:
11341 case 0xF4:
11342 case 0xF5:
11343 case 0xF6:
11344 case 0xF7:
11345 case 0xF8:
11346 case 0xF9:
11347 case 0xFA:
11348 case 0xFB:
11349 case 0xFC:
11350 case 0xFD:
11351 case 0xFE:
11352 case 0xFF:
11353 return sax->number_integer(static_cast<std::int8_t>(current));
11354
11355 default: // anything else
11356 {
11357 auto last_token = get_token_string();
11358 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
11359 exception_message(input_format_t::msgpack, concat("invalid byte: 0x", last_token), "value"), nullptr));
11360 }
11361 }
11362 }
11363
11364 /*!
11365 @brief reads a MessagePack string
11366
11367 This function first reads starting bytes to determine the expected
11368 string length and then copies this number of bytes into a string.
11369
11370 @param[out] result created string
11371
11372 @return whether string creation completed
11373 */
11374 bool get_msgpack_string(string_t& result)
11375 {
11376 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::msgpack, "string")))
11377 {
11378 return false;
11379 }
11380
11381 switch (current)
11382 {
11383 // fixstr
11384 case 0xA0:
11385 case 0xA1:
11386 case 0xA2:
11387 case 0xA3:
11388 case 0xA4:
11389 case 0xA5:
11390 case 0xA6:
11391 case 0xA7:
11392 case 0xA8:
11393 case 0xA9:
11394 case 0xAA:
11395 case 0xAB:
11396 case 0xAC:
11397 case 0xAD:
11398 case 0xAE:
11399 case 0xAF:
11400 case 0xB0:
11401 case 0xB1:
11402 case 0xB2:
11403 case 0xB3:
11404 case 0xB4:
11405 case 0xB5:
11406 case 0xB6:
11407 case 0xB7:
11408 case 0xB8:
11409 case 0xB9:
11410 case 0xBA:
11411 case 0xBB:
11412 case 0xBC:
11413 case 0xBD:
11414 case 0xBE:
11415 case 0xBF:
11416 {
11417 return get_string(input_format_t::msgpack, static_cast<unsigned int>(current) & 0x1Fu, result);
11418 }
11419
11420 case 0xD9: // str 8
11421 {
11422 std::uint8_t len{};
11423 return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
11424 }
11425
11426 case 0xDA: // str 16
11427 {
11428 std::uint16_t len{};
11429 return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
11430 }
11431
11432 case 0xDB: // str 32
11433 {
11434 std::uint32_t len{};
11435 return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
11436 }
11437
11438 default:
11439 {
11440 auto last_token = get_token_string();
11441 return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
11442 exception_message(input_format_t::msgpack, concat("expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x", last_token), "string"), nullptr));
11443 }
11444 }
11445 }
11446
11447 /*!
11448 @brief reads a MessagePack byte array
11449
11450 This function first reads starting bytes to determine the expected
11451 byte array length and then copies this number of bytes into a byte array.
11452
11453 @param[out] result created byte array
11454
11455 @return whether byte array creation completed
11456 */
11457 bool get_msgpack_binary(binary_t& result)
11458 {
11459 // helper function to set the subtype
11460 auto assign_and_return_true = [&result](std::int8_t subtype)
11461 {
11462 result.set_subtype(static_cast<std::uint8_t>(subtype));
11463 return true;
11464 };
11465
11466 switch (current)
11467 {
11468 case 0xC4: // bin 8
11469 {
11470 std::uint8_t len{};
11471 return get_number(input_format_t::msgpack, len) &&
11472 get_binary(input_format_t::msgpack, len, result);
11473 }
11474
11475 case 0xC5: // bin 16
11476 {
11477 std::uint16_t len{};
11478 return get_number(input_format_t::msgpack, len) &&
11479 get_binary(input_format_t::msgpack, len, result);
11480 }
11481
11482 case 0xC6: // bin 32
11483 {
11484 std::uint32_t len{};
11485 return get_number(input_format_t::msgpack, len) &&
11486 get_binary(input_format_t::msgpack, len, result);
11487 }
11488
11489 case 0xC7: // ext 8
11490 {
11491 std::uint8_t len{};
11492 std::int8_t subtype{};
11493 return get_number(input_format_t::msgpack, len) &&
11494 get_number(input_format_t::msgpack, subtype) &&
11495 get_binary(input_format_t::msgpack, len, result) &&
11496 assign_and_return_true(subtype);
11497 }
11498
11499 case 0xC8: // ext 16
11500 {
11501 std::uint16_t len{};
11502 std::int8_t subtype{};
11503 return get_number(input_format_t::msgpack, len) &&
11504 get_number(input_format_t::msgpack, subtype) &&
11505 get_binary(input_format_t::msgpack, len, result) &&
11506 assign_and_return_true(subtype);
11507 }
11508
11509 case 0xC9: // ext 32
11510 {
11511 std::uint32_t len{};
11512 std::int8_t subtype{};
11513 return get_number(input_format_t::msgpack, len) &&
11514 get_number(input_format_t::msgpack, subtype) &&
11515 get_binary(input_format_t::msgpack, len, result) &&
11516 assign_and_return_true(subtype);
11517 }
11518
11519 case 0xD4: // fixext 1
11520 {
11521 std::int8_t subtype{};
11522 return get_number(input_format_t::msgpack, subtype) &&
11523 get_binary(input_format_t::msgpack, 1, result) &&
11524 assign_and_return_true(subtype);
11525 }
11526
11527 case 0xD5: // fixext 2
11528 {
11529 std::int8_t subtype{};
11530 return get_number(input_format_t::msgpack, subtype) &&
11531 get_binary(input_format_t::msgpack, 2, result) &&
11532 assign_and_return_true(subtype);
11533 }
11534
11535 case 0xD6: // fixext 4
11536 {
11537 std::int8_t subtype{};
11538 return get_number(input_format_t::msgpack, subtype) &&
11539 get_binary(input_format_t::msgpack, 4, result) &&
11540 assign_and_return_true(subtype);
11541 }
11542
11543 case 0xD7: // fixext 8
11544 {
11545 std::int8_t subtype{};
11546 return get_number(input_format_t::msgpack, subtype) &&
11547 get_binary(input_format_t::msgpack, 8, result) &&
11548 assign_and_return_true(subtype);
11549 }
11550
11551 case 0xD8: // fixext 16
11552 {
11553 std::int8_t subtype{};
11554 return get_number(input_format_t::msgpack, subtype) &&
11555 get_binary(input_format_t::msgpack, 16, result) &&
11556 assign_and_return_true(subtype);
11557 }
11558
11559 default: // LCOV_EXCL_LINE
11560 return false; // LCOV_EXCL_LINE
11561 }
11562 }
11563
11564 /*!
11565 @param[in] len the length of the array
11566 @return whether array creation completed
11567 */
11568 bool get_msgpack_array(const std::size_t len)
11569 {
11570 if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
11571 {
11572 return false;
11573 }
11574
11575 for (std::size_t i = 0; i < len; ++i)
11576 {
11577 if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
11578 {
11579 return false;
11580 }
11581 }
11582
11583 return sax->end_array();
11584 }
11585
11586 /*!
11587 @param[in] len the length of the object
11588 @return whether object creation completed
11589 */
11590 bool get_msgpack_object(const std::size_t len)
11591 {
11592 if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
11593 {
11594 return false;
11595 }
11596
11597 string_t key;
11598 for (std::size_t i = 0; i < len; ++i)
11599 {
11600 get();
11601 if (JSON_HEDLEY_UNLIKELY(!get_msgpack_string(key) || !sax->key(key)))
11602 {
11603 return false;
11604 }
11605
11606 if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
11607 {
11608 return false;
11609 }
11610 key.clear();
11611 }
11612
11613 return sax->end_object();
11614 }
11615
11616 ////////////
11617 // UBJSON //
11618 ////////////
11619
11620 /*!
11621 @param[in] get_char whether a new character should be retrieved from the
11622 input (true, default) or whether the last read
11623 character should be considered instead
11624
11625 @return whether a valid UBJSON value was passed to the SAX parser
11626 */
11627 bool parse_ubjson_internal(const bool get_char = true)
11628 {
11629 return get_ubjson_value(get_char ? get_ignore_noop() : current);
11630 }
11631
11632 /*!
11633 @brief reads a UBJSON string
11634
11635 This function is either called after reading the 'S' byte explicitly
11636 indicating a string, or in case of an object key where the 'S' byte can be
11637 left out.
11638
11639 @param[out] result created string
11640 @param[in] get_char whether a new character should be retrieved from the
11641 input (true, default) or whether the last read
11642 character should be considered instead
11643
11644 @return whether string creation completed
11645 */
11646 bool get_ubjson_string(string_t& result, const bool get_char = true)
11647 {
11648 if (get_char)
11649 {
11650 get(); // TODO(niels): may we ignore N here?
11651 }
11652
11653 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "value")))
11654 {
11655 return false;
11656 }
11657
11658 switch (current)
11659 {
11660 case 'U':
11661 {
11662 std::uint8_t len{};
11663 return get_number(input_format, len) && get_string(input_format, len, result);
11664 }
11665
11666 case 'i':
11667 {
11668 std::int8_t len{};
11669 return get_number(input_format, len) && get_string(input_format, len, result);
11670 }
11671
11672 case 'I':
11673 {
11674 std::int16_t len{};
11675 return get_number(input_format, len) && get_string(input_format, len, result);
11676 }
11677
11678 case 'l':
11679 {
11680 std::int32_t len{};
11681 return get_number(input_format, len) && get_string(input_format, len, result);
11682 }
11683
11684 case 'L':
11685 {
11686 std::int64_t len{};
11687 return get_number(input_format, len) && get_string(input_format, len, result);
11688 }
11689
11690 case 'u':
11691 {
11692 if (input_format != input_format_t::bjdata)
11693 {
11694 break;
11695 }
11696 std::uint16_t len{};
11697 return get_number(input_format, len) && get_string(input_format, len, result);
11698 }
11699
11700 case 'm':
11701 {
11702 if (input_format != input_format_t::bjdata)
11703 {
11704 break;
11705 }
11706 std::uint32_t len{};
11707 return get_number(input_format, len) && get_string(input_format, len, result);
11708 }
11709
11710 case 'M':
11711 {
11712 if (input_format != input_format_t::bjdata)
11713 {
11714 break;
11715 }
11716 std::uint64_t len{};
11717 return get_number(input_format, len) && get_string(input_format, len, result);
11718 }
11719
11720 default:
11721 break;
11722 }
11723 auto last_token = get_token_string();
11724 std::string message;
11725
11726 if (input_format != input_format_t::bjdata)
11727 {
11728 message = "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token;
11729 }
11730 else
11731 {
11732 message = "expected length type specification (U, i, u, I, m, l, M, L); last byte: 0x" + last_token;
11733 }
11734 return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "string"), nullptr));
11735 }
11736
11737 /*!
11738 @param[out] dim an integer vector storing the ND array dimensions
11739 @return whether reading ND array size vector is successful
11740 */
11741 bool get_ubjson_ndarray_size(std::vector<size_t>& dim)
11742 {
11743 std::pair<std::size_t, char_int_type> size_and_type;
11744 size_t dimlen = 0;
11745 bool no_ndarray = true;
11746
11747 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type, no_ndarray)))
11748 {
11749 return false;
11750 }
11751
11752 if (size_and_type.first != npos)
11753 {
11754 if (size_and_type.second != 0)
11755 {
11756 if (size_and_type.second != 'N')
11757 {
11758 for (std::size_t i = 0; i < size_and_type.first; ++i)
11759 {
11760 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, size_and_type.second)))
11761 {
11762 return false;
11763 }
11764 dim.push_back(dimlen);
11765 }
11766 }
11767 }
11768 else
11769 {
11770 for (std::size_t i = 0; i < size_and_type.first; ++i)
11771 {
11772 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray)))
11773 {
11774 return false;
11775 }
11776 dim.push_back(dimlen);
11777 }
11778 }
11779 }
11780 else
11781 {
11782 while (current != ']')
11783 {
11784 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, current)))
11785 {
11786 return false;
11787 }
11788 dim.push_back(dimlen);
11789 get_ignore_noop();
11790 }
11791 }
11792 return true;
11793 }
11794
11795 /*!
11796 @param[out] result determined size
11797 @param[in,out] is_ndarray for input, `true` means already inside an ndarray vector
11798 or ndarray dimension is not allowed; `false` means ndarray
11799 is allowed; for output, `true` means an ndarray is found;
11800 is_ndarray can only return `true` when its initial value
11801 is `false`
11802 @param[in] prefix type marker if already read, otherwise set to 0
11803
11804 @return whether size determination completed
11805 */
11806 bool get_ubjson_size_value(std::size_t& result, bool& is_ndarray, char_int_type prefix = 0)
11807 {
11808 if (prefix == 0)
11809 {
11810 prefix = get_ignore_noop();
11811 }
11812
11813 switch (prefix)
11814 {
11815 case 'U':
11816 {
11817 std::uint8_t number{};
11818 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11819 {
11820 return false;
11821 }
11822 result = static_cast<std::size_t>(number);
11823 return true;
11824 }
11825
11826 case 'i':
11827 {
11828 std::int8_t number{};
11829 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11830 {
11831 return false;
11832 }
11833 if (number < 0)
11834 {
11835 return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
11836 exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
11837 }
11838 result = static_cast<std::size_t>(number); // NOLINT(bugprone-signed-char-misuse,cert-str34-c): number is not a char
11839 return true;
11840 }
11841
11842 case 'I':
11843 {
11844 std::int16_t number{};
11845 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11846 {
11847 return false;
11848 }
11849 if (number < 0)
11850 {
11851 return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
11852 exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
11853 }
11854 result = static_cast<std::size_t>(number);
11855 return true;
11856 }
11857
11858 case 'l':
11859 {
11860 std::int32_t number{};
11861 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11862 {
11863 return false;
11864 }
11865 if (number < 0)
11866 {
11867 return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
11868 exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
11869 }
11870 result = static_cast<std::size_t>(number);
11871 return true;
11872 }
11873
11874 case 'L':
11875 {
11876 std::int64_t number{};
11877 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11878 {
11879 return false;
11880 }
11881 if (number < 0)
11882 {
11883 return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
11884 exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
11885 }
11886 if (!value_in_range_of<std::size_t>(number))
11887 {
11888 return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408,
11889 exception_message(input_format, "integer value overflow", "size"), nullptr));
11890 }
11891 result = static_cast<std::size_t>(number);
11892 return true;
11893 }
11894
11895 case 'u':
11896 {
11897 if (input_format != input_format_t::bjdata)
11898 {
11899 break;
11900 }
11901 std::uint16_t number{};
11902 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11903 {
11904 return false;
11905 }
11906 result = static_cast<std::size_t>(number);
11907 return true;
11908 }
11909
11910 case 'm':
11911 {
11912 if (input_format != input_format_t::bjdata)
11913 {
11914 break;
11915 }
11916 std::uint32_t number{};
11917 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11918 {
11919 return false;
11920 }
11921 result = conditional_static_cast<std::size_t>(number);
11922 return true;
11923 }
11924
11925 case 'M':
11926 {
11927 if (input_format != input_format_t::bjdata)
11928 {
11929 break;
11930 }
11931 std::uint64_t number{};
11932 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11933 {
11934 return false;
11935 }
11936 if (!value_in_range_of<std::size_t>(number))
11937 {
11938 return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408,
11939 exception_message(input_format, "integer value overflow", "size"), nullptr));
11940 }
11941 result = detail::conditional_static_cast<std::size_t>(number);
11942 return true;
11943 }
11944
11945 case '[':
11946 {
11947 if (input_format != input_format_t::bjdata)
11948 {
11949 break;
11950 }
11951 if (is_ndarray) // ndarray dimensional vector can only contain integers, and can not embed another array
11952 {
11953 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));
11954 }
11955 std::vector<size_t> dim;
11956 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_ndarray_size(dim)))
11957 {
11958 return false;
11959 }
11960 if (dim.size() == 1 || (dim.size() == 2 && dim.at(0) == 1)) // return normal array size if 1D row vector
11961 {
11962 result = dim.at(dim.size() - 1);
11963 return true;
11964 }
11965 if (!dim.empty()) // if ndarray, convert to an object in JData annotated array format
11966 {
11967 for (auto i : dim) // test if any dimension in an ndarray is 0, if so, return a 1D empty container
11968 {
11969 if ( i == 0 )
11970 {
11971 result = 0;
11972 return true;
11973 }
11974 }
11975
11976 string_t key = "_ArraySize_";
11977 if (JSON_HEDLEY_UNLIKELY(!sax->start_object(3) || !sax->key(key) || !sax->start_array(dim.size())))
11978 {
11979 return false;
11980 }
11981 result = 1;
11982 for (auto i : dim)
11983 {
11984 result *= i;
11985 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()
11986 {
11987 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));
11988 }
11989 if (JSON_HEDLEY_UNLIKELY(!sax->number_unsigned(static_cast<number_unsigned_t>(i))))
11990 {
11991 return false;
11992 }
11993 }
11994 is_ndarray = true;
11995 return sax->end_array();
11996 }
11997 result = 0;
11998 return true;
11999 }
12000
12001 default:
12002 break;
12003 }
12004 auto last_token = get_token_string();
12005 std::string message;
12006
12007 if (input_format != input_format_t::bjdata)
12008 {
12009 message = "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token;
12010 }
12011 else
12012 {
12013 message = "expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x" + last_token;
12014 }
12015 return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "size"), nullptr));
12016 }
12017
12018 /*!
12019 @brief determine the type and size for a container
12020
12021 In the optimized UBJSON format, a type and a size can be provided to allow
12022 for a more compact representation.
12023
12024 @param[out] result pair of the size and the type
12025 @param[in] inside_ndarray whether the parser is parsing an ND array dimensional vector
12026
12027 @return whether pair creation completed
12028 */
12029 bool get_ubjson_size_type(std::pair<std::size_t, char_int_type>& result, bool inside_ndarray = false)
12030 {
12031 result.first = npos; // size
12032 result.second = 0; // type
12033 bool is_ndarray = false;
12034
12035 get_ignore_noop();
12036
12037 if (current == '$')
12038 {
12039 result.second = get(); // must not ignore 'N', because 'N' maybe the type
12040 if (input_format == input_format_t::bjdata
12041 && JSON_HEDLEY_UNLIKELY(std::binary_search(bjd_optimized_type_markers.begin(), bjd_optimized_type_markers.end(), result.second)))
12042 {
12043 auto last_token = get_token_string();
12044 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
12045 exception_message(input_format, concat("marker 0x", last_token, " is not a permitted optimized array type"), "type"), nullptr));
12046 }
12047
12048 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "type")))
12049 {
12050 return false;
12051 }
12052
12053 get_ignore_noop();
12054 if (JSON_HEDLEY_UNLIKELY(current != '#'))
12055 {
12056 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "value")))
12057 {
12058 return false;
12059 }
12060 auto last_token = get_token_string();
12061 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
12062 exception_message(input_format, concat("expected '#' after type information; last byte: 0x", last_token), "size"), nullptr));
12063 }
12064
12065 const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
12066 if (input_format == input_format_t::bjdata && is_ndarray)
12067 {
12068 if (inside_ndarray)
12069 {
12070 return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
12071 exception_message(input_format, "ndarray can not be recursive", "size"), nullptr));
12072 }
12073 result.second |= (1 << 8); // use bit 8 to indicate ndarray, all UBJSON and BJData markers should be ASCII letters
12074 }
12075 return is_error;
12076 }
12077
12078 if (current == '#')
12079 {
12080 const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
12081 if (input_format == input_format_t::bjdata && is_ndarray)
12082 {
12083 return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
12084 exception_message(input_format, "ndarray requires both type and size", "size"), nullptr));
12085 }
12086 return is_error;
12087 }
12088
12089 return true;
12090 }
12091
12092 /*!
12093 @param prefix the previously read or set type prefix
12094 @return whether value creation completed
12095 */
12096 bool get_ubjson_value(const char_int_type prefix)
12097 {
12098 switch (prefix)
12099 {
12100 case char_traits<char_type>::eof(): // EOF
12101 return unexpect_eof(input_format, "value");
12102
12103 case 'T': // true
12104 return sax->boolean(true);
12105 case 'F': // false
12106 return sax->boolean(false);
12107
12108 case 'Z': // null
12109 return sax->null();
12110
12111 case 'B': // byte
12112 {
12113 if (input_format != input_format_t::bjdata)
12114 {
12115 break;
12116 }
12117 std::uint8_t number{};
12118 return get_number(input_format, number) && sax->number_unsigned(number);
12119 }
12120
12121 case 'U':
12122 {
12123 std::uint8_t number{};
12124 return get_number(input_format, number) && sax->number_unsigned(number);
12125 }
12126
12127 case 'i':
12128 {
12129 std::int8_t number{};
12130 return get_number(input_format, number) && sax->number_integer(number);
12131 }
12132
12133 case 'I':
12134 {
12135 std::int16_t number{};
12136 return get_number(input_format, number) && sax->number_integer(number);
12137 }
12138
12139 case 'l':
12140 {
12141 std::int32_t number{};
12142 return get_number(input_format, number) && sax->number_integer(number);
12143 }
12144
12145 case 'L':
12146 {
12147 std::int64_t number{};
12148 return get_number(input_format, number) && sax->number_integer(number);
12149 }
12150
12151 case 'u':
12152 {
12153 if (input_format != input_format_t::bjdata)
12154 {
12155 break;
12156 }
12157 std::uint16_t number{};
12158 return get_number(input_format, number) && sax->number_unsigned(number);
12159 }
12160
12161 case 'm':
12162 {
12163 if (input_format != input_format_t::bjdata)
12164 {
12165 break;
12166 }
12167 std::uint32_t number{};
12168 return get_number(input_format, number) && sax->number_unsigned(number);
12169 }
12170
12171 case 'M':
12172 {
12173 if (input_format != input_format_t::bjdata)
12174 {
12175 break;
12176 }
12177 std::uint64_t number{};
12178 return get_number(input_format, number) && sax->number_unsigned(number);
12179 }
12180
12181 case 'h':
12182 {
12183 if (input_format != input_format_t::bjdata)
12184 {
12185 break;
12186 }
12187 const auto byte1_raw = get();
12188 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
12189 {
12190 return false;
12191 }
12192 const auto byte2_raw = get();
12193 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
12194 {
12195 return false;
12196 }
12197
12198 const auto byte1 = static_cast<unsigned char>(byte1_raw);
12199 const auto byte2 = static_cast<unsigned char>(byte2_raw);
12200
12201 // code from RFC 7049, Appendix D, Figure 3:
12202 // As half-precision floating-point numbers were only added
12203 // to IEEE 754 in 2008, today's programming platforms often
12204 // still only have limited support for them. It is very
12205 // easy to include at least decoding support for them even
12206 // without such support. An example of a small decoder for
12207 // half-precision floating-point numbers in the C language
12208 // is shown in Fig. 3.
12209 const auto half = static_cast<unsigned int>((byte2 << 8u) + byte1);
12210 const double val = [&half]
12211 {
12212 const int exp = (half >> 10u) & 0x1Fu;
12213 const unsigned int mant = half & 0x3FFu;
12214 JSON_ASSERT(0 <= exp&& exp <= 32);
12215 JSON_ASSERT(mant <= 1024);
12216 switch (exp)
12217 {
12218 case 0:
12219 return std::ldexp(mant, -24);
12220 case 31:
12221 return (mant == 0)
12222 ? std::numeric_limits<double>::infinity()
12223 : std::numeric_limits<double>::quiet_NaN();
12224 default:
12225 return std::ldexp(mant + 1024, exp - 25);
12226 }
12227 }();
12228 return sax->number_float((half & 0x8000u) != 0
12229 ? static_cast<number_float_t>(-val)
12230 : static_cast<number_float_t>(val), "");
12231 }
12232
12233 case 'd':
12234 {
12235 float number{};
12236 return get_number(input_format, number) && sax->number_float(static_cast<number_float_t>(number), "");
12237 }
12238
12239 case 'D':
12240 {
12241 double number{};
12242 return get_number(input_format, number) && sax->number_float(static_cast<number_float_t>(number), "");
12243 }
12244
12245 case 'H':
12246 {
12247 return get_ubjson_high_precision_number();
12248 }
12249
12250 case 'C': // char
12251 {
12252 get();
12253 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "char")))
12254 {
12255 return false;
12256 }
12257 if (JSON_HEDLEY_UNLIKELY(current > 127))
12258 {
12259 auto last_token = get_token_string();
12260 return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
12261 exception_message(input_format, concat("byte after 'C' must be in range 0x00..0x7F; last byte: 0x", last_token), "char"), nullptr));
12262 }
12263 string_t s(1, static_cast<typename string_t::value_type>(current));
12264 return sax->string(s);
12265 }
12266
12267 case 'S': // string
12268 {
12269 string_t s;
12270 return get_ubjson_string(s) && sax->string(s);
12271 }
12272
12273 case '[': // array
12274 return get_ubjson_array();
12275
12276 case '{': // object
12277 return get_ubjson_object();
12278
12279 default: // anything else
12280 break;
12281 }
12282 auto last_token = get_token_string();
12283 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));
12284 }
12285
12286 /*!
12287 @return whether array creation completed
12288 */
12289 bool get_ubjson_array()
12290 {
12291 std::pair<std::size_t, char_int_type> size_and_type;
12292 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type)))
12293 {
12294 return false;
12295 }
12296
12297 // 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):
12298 // {"_ArrayType_" : "typeid", "_ArraySize_" : [n1, n2, ...], "_ArrayData_" : [v1, v2, ...]}
12299
12300 if (input_format == input_format_t::bjdata && size_and_type.first != npos && (size_and_type.second & (1 << 8)) != 0)
12301 {
12302 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
12303 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)
12304 {
12305 return p.first < t;
12306 });
12307 string_t key = "_ArrayType_";
12308 if (JSON_HEDLEY_UNLIKELY(it == bjd_types_map.end() || it->first != size_and_type.second))
12309 {
12310 auto last_token = get_token_string();
12311 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
12312 exception_message(input_format, "invalid byte: 0x" + last_token, "type"), nullptr));
12313 }
12314
12315 string_t type = it->second; // sax->string() takes a reference
12316 if (JSON_HEDLEY_UNLIKELY(!sax->key(key) || !sax->string(type)))
12317 {
12318 return false;
12319 }
12320
12321 if (size_and_type.second == 'C' || size_and_type.second == 'B')
12322 {
12323 size_and_type.second = 'U';
12324 }
12325
12326 key = "_ArrayData_";
12327 if (JSON_HEDLEY_UNLIKELY(!sax->key(key) || !sax->start_array(size_and_type.first) ))
12328 {
12329 return false;
12330 }
12331
12332 for (std::size_t i = 0; i < size_and_type.first; ++i)
12333 {
12334 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
12335 {
12336 return false;
12337 }
12338 }
12339
12340 return (sax->end_array() && sax->end_object());
12341 }
12342
12343 // If BJData type marker is 'B' decode as binary
12344 if (input_format == input_format_t::bjdata && size_and_type.first != npos && size_and_type.second == 'B')
12345 {
12346 binary_t result;
12347 return get_binary(input_format, size_and_type.first, result) && sax->binary(result);
12348 }
12349
12350 if (size_and_type.first != npos)
12351 {
12352 if (JSON_HEDLEY_UNLIKELY(!sax->start_array(size_and_type.first)))
12353 {
12354 return false;
12355 }
12356
12357 if (size_and_type.second != 0)
12358 {
12359 if (size_and_type.second != 'N')
12360 {
12361 for (std::size_t i = 0; i < size_and_type.first; ++i)
12362 {
12363 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
12364 {
12365 return false;
12366 }
12367 }
12368 }
12369 }
12370 else
12371 {
12372 for (std::size_t i = 0; i < size_and_type.first; ++i)
12373 {
12374 if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
12375 {
12376 return false;
12377 }
12378 }
12379 }
12380 }
12381 else
12382 {
12383 if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
12384 {
12385 return false;
12386 }
12387
12388 while (current != ']')
12389 {
12390 if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal(false)))
12391 {
12392 return false;
12393 }
12394 get_ignore_noop();
12395 }
12396 }
12397
12398 return sax->end_array();
12399 }
12400
12401 /*!
12402 @return whether object creation completed
12403 */
12404 bool get_ubjson_object()
12405 {
12406 std::pair<std::size_t, char_int_type> size_and_type;
12407 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type)))
12408 {
12409 return false;
12410 }
12411
12412 // do not accept ND-array size in objects in BJData
12413 if (input_format == input_format_t::bjdata && size_and_type.first != npos && (size_and_type.second & (1 << 8)) != 0)
12414 {
12415 auto last_token = get_token_string();
12416 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
12417 exception_message(input_format, "BJData object does not support ND-array size in optimized format", "object"), nullptr));
12418 }
12419
12420 string_t key;
12421 if (size_and_type.first != npos)
12422 {
12423 if (JSON_HEDLEY_UNLIKELY(!sax->start_object(size_and_type.first)))
12424 {
12425 return false;
12426 }
12427
12428 if (size_and_type.second != 0)
12429 {
12430 for (std::size_t i = 0; i < size_and_type.first; ++i)
12431 {
12432 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
12433 {
12434 return false;
12435 }
12436 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
12437 {
12438 return false;
12439 }
12440 key.clear();
12441 }
12442 }
12443 else
12444 {
12445 for (std::size_t i = 0; i < size_and_type.first; ++i)
12446 {
12447 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
12448 {
12449 return false;
12450 }
12451 if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
12452 {
12453 return false;
12454 }
12455 key.clear();
12456 }
12457 }
12458 }
12459 else
12460 {
12461 if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
12462 {
12463 return false;
12464 }
12465
12466 while (current != '}')
12467 {
12468 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key, false) || !sax->key(key)))
12469 {
12470 return false;
12471 }
12472 if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
12473 {
12474 return false;
12475 }
12476 get_ignore_noop();
12477 key.clear();
12478 }
12479 }
12480
12481 return sax->end_object();
12482 }
12483
12484 // Note, no reader for UBJSON binary types is implemented because they do
12485 // not exist
12486
12487 bool get_ubjson_high_precision_number()
12488 {
12489 // get size of following number string
12490 std::size_t size{};
12491 bool no_ndarray = true;
12492 auto res = get_ubjson_size_value(size, no_ndarray);
12493 if (JSON_HEDLEY_UNLIKELY(!res))
12494 {
12495 return res;
12496 }
12497
12498 // get number string
12499 std::vector<char> number_vector;
12500 for (std::size_t i = 0; i < size; ++i)
12501 {
12502 get();
12503 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
12504 {
12505 return false;
12506 }
12507 number_vector.push_back(static_cast<char>(current));
12508 }
12509
12510 // parse number string
12511 using ia_type = decltype(detail::input_adapter(number_vector));
12512 auto number_lexer = detail::lexer<BasicJsonType, ia_type>(detail::input_adapter(number_vector), false);
12513 const auto result_number = number_lexer.scan();
12514 const auto number_string = number_lexer.get_token_string();
12515 const auto result_remainder = number_lexer.scan();
12516
12517 using token_type = typename detail::lexer_base<BasicJsonType>::token_type;
12518
12519 if (JSON_HEDLEY_UNLIKELY(result_remainder != token_type::end_of_input))
12520 {
12521 return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read,
12522 exception_message(input_format, concat("invalid number text: ", number_lexer.get_token_string()), "high-precision number"), nullptr));
12523 }
12524
12525 switch (result_number)
12526 {
12527 case token_type::value_integer:
12528 return sax->number_integer(number_lexer.get_number_integer());
12529 case token_type::value_unsigned:
12530 return sax->number_unsigned(number_lexer.get_number_unsigned());
12531 case token_type::value_float:
12532 return sax->number_float(number_lexer.get_number_float(), std::move(number_string));
12533 case token_type::uninitialized:
12534 case token_type::literal_true:
12535 case token_type::literal_false:
12536 case token_type::literal_null:
12537 case token_type::value_string:
12538 case token_type::begin_array:
12539 case token_type::begin_object:
12540 case token_type::end_array:
12541 case token_type::end_object:
12542 case token_type::name_separator:
12543 case token_type::value_separator:
12544 case token_type::parse_error:
12545 case token_type::end_of_input:
12546 case token_type::literal_or_value:
12547 default:
12548 return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read,
12549 exception_message(input_format, concat("invalid number text: ", number_lexer.get_token_string()), "high-precision number"), nullptr));
12550 }
12551 }
12552
12553 ///////////////////////
12554 // Utility functions //
12555 ///////////////////////
12556
12557 /*!
12558 @brief get next character from the input
12559
12560 This function provides the interface to the used input adapter. It does
12561 not throw in case the input reached EOF, but returns a -'ve valued
12562 `char_traits<char_type>::eof()` in that case.
12563
12564 @return character read from the input
12565 */
12566 char_int_type get()
12567 {
12568 ++chars_read;
12569 return current = ia.get_character();
12570 }
12571
12572 /*!
12573 @brief get_to read into a primitive type
12574
12575 This function provides the interface to the used input adapter. It does
12576 not throw in case the input reached EOF, but returns false instead
12577
12578 @return bool, whether the read was successful
12579 */
12580 template<class T>
12581 bool get_to(T& dest, const input_format_t format, const char* context)
12582 {
12583 auto new_chars_read = ia.get_elements(&dest);
12584 chars_read += new_chars_read;
12585 if (JSON_HEDLEY_UNLIKELY(new_chars_read < sizeof(T)))
12586 {
12587 // in case of failure, advance position by 1 to report failing location
12588 ++chars_read;
12589 sax->parse_error(chars_read, "<end of file>", parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), nullptr));
12590 return false;
12591 }
12592 return true;
12593 }
12594
12595 /*!
12596 @return character read from the input after ignoring all 'N' entries
12597 */
12598 char_int_type get_ignore_noop()
12599 {
12600 do
12601 {
12602 get();
12603 }
12604 while (current == 'N');
12605
12606 return current;
12607 }
12608
12609 template<class NumberType>
12610 static void byte_swap(NumberType& number)
12611 {
12612 constexpr std::size_t sz = sizeof(number);
12613 #ifdef __cpp_lib_byteswap
12614 if constexpr (sz == 1)
12615 {
12616 return;
12617 }
12618 if constexpr(std::is_integral_v<NumberType>)
12619 {
12620 number = std::byteswap(number);
12621 return;
12622 }
12623 #endif
12624 auto* ptr = reinterpret_cast<std::uint8_t*>(&number);
12625 for (std::size_t i = 0; i < sz / 2; ++i)
12626 {
12627 std::swap(ptr[i], ptr[sz - i - 1]);
12628 }
12629 }
12630
12631 /*
12632 @brief read a number from the input
12633
12634 @tparam NumberType the type of the number
12635 @param[in] format the current format (for diagnostics)
12636 @param[out] result number of type @a NumberType
12637
12638 @return whether conversion completed
12639
12640 @note This function needs to respect the system's endianness, because
12641 bytes in CBOR, MessagePack, and UBJSON are stored in network order
12642 (big endian) and therefore need reordering on little endian systems.
12643 On the other hand, BSON and BJData use little endian and should reorder
12644 on big endian systems.
12645 */
12646 template<typename NumberType, bool InputIsLittleEndian = false>
12647 bool get_number(const input_format_t format, NumberType& result)
12648 {
12649 // read in the original format
12650
12651 if (JSON_HEDLEY_UNLIKELY(!get_to(result, format, "number")))
12652 {
12653 return false;
12654 }
12655 if (is_little_endian != (InputIsLittleEndian || format == input_format_t::bjdata))
12656 {
12657 byte_swap(result);
12658 }
12659 return true;
12660 }
12661
12662 /*!
12663 @brief create a string by reading characters from the input
12664
12665 @tparam NumberType the type of the number
12666 @param[in] format the current format (for diagnostics)
12667 @param[in] len number of characters to read
12668 @param[out] result string created by reading @a len bytes
12669
12670 @return whether string creation completed
12671
12672 @note We can not reserve @a len bytes for the result, because @a len
12673 may be too large. Usually, @ref unexpect_eof() detects the end of
12674 the input before we run out of string memory.
12675 */
12676 template<typename NumberType>
12677 bool get_string(const input_format_t format,
12678 const NumberType len,
12679 string_t& result)
12680 {
12681 bool success = true;
12682 for (NumberType i = 0; i < len; i++)
12683 {
12684 get();
12685 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "string")))
12686 {
12687 success = false;
12688 break;
12689 }
12690 result.push_back(static_cast<typename string_t::value_type>(current));
12691 }
12692 return success;
12693 }
12694
12695 /*!
12696 @brief create a byte array by reading bytes from the input
12697
12698 @tparam NumberType the type of the number
12699 @param[in] format the current format (for diagnostics)
12700 @param[in] len number of bytes to read
12701 @param[out] result byte array created by reading @a len bytes
12702
12703 @return whether byte array creation completed
12704
12705 @note We can not reserve @a len bytes for the result, because @a len
12706 may be too large. Usually, @ref unexpect_eof() detects the end of
12707 the input before we run out of memory.
12708 */
12709 template<typename NumberType>
12710 bool get_binary(const input_format_t format,
12711 const NumberType len,
12712 binary_t& result)
12713 {
12714 bool success = true;
12715 for (NumberType i = 0; i < len; i++)
12716 {
12717 get();
12718 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "binary")))
12719 {
12720 success = false;
12721 break;
12722 }
12723 result.push_back(static_cast<std::uint8_t>(current));
12724 }
12725 return success;
12726 }
12727
12728 /*!
12729 @param[in] format the current format (for diagnostics)
12730 @param[in] context further context information (for diagnostics)
12731 @return whether the last read character is not EOF
12732 */
12733 JSON_HEDLEY_NON_NULL(3)
12734 bool unexpect_eof(const input_format_t format, const char* context) const
12735 {
12736 if (JSON_HEDLEY_UNLIKELY(current == char_traits<char_type>::eof()))
12737 {
12738 return sax->parse_error(chars_read, "<end of file>",
12739 parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), nullptr));
12740 }
12741 return true;
12742 }
12743
12744 /*!
12745 @return a string representation of the last read byte
12746 */
12747 std::string get_token_string() const
12748 {
12749 std::array<char, 3> cr{{}};
12750 static_cast<void>((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(current))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
12751 return std::string{cr.data()};
12752 }
12753
12754 /*!
12755 @param[in] format the current format
12756 @param[in] detail a detailed error message
12757 @param[in] context further context information
12758 @return a message string to use in the parse_error exceptions
12759 */
12760 std::string exception_message(const input_format_t format,
12761 const std::string& detail,
12762 const std::string& context) const
12763 {
12764 std::string error_msg = "syntax error while parsing ";
12765
12766 switch (format)
12767 {
12768 case input_format_t::cbor:
12769 error_msg += "CBOR";
12770 break;
12771
12772 case input_format_t::msgpack:
12773 error_msg += "MessagePack";
12774 break;
12775
12776 case input_format_t::ubjson:
12777 error_msg += "UBJSON";
12778 break;
12779
12780 case input_format_t::bson:
12781 error_msg += "BSON";
12782 break;
12783
12784 case input_format_t::bjdata:
12785 error_msg += "BJData";
12786 break;
12787
12788 case input_format_t::json: // LCOV_EXCL_LINE
12789 default: // LCOV_EXCL_LINE
12790 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
12791 }
12792
12793 return concat(error_msg, ' ', context, ": ", detail);
12794 }
12795
12796 private:
12797 static JSON_INLINE_VARIABLE constexpr std::size_t npos = detail::unknown_size();
12798
12799 /// input adapter
12800 InputAdapterType ia;
12801
12802 /// the current character
12803 char_int_type current = char_traits<char_type>::eof();
12804
12805 /// the number of characters read
12806 std::size_t chars_read = 0;
12807
12808 /// whether we can assume little endianness
12809 const bool is_little_endian = little_endianness();
12810
12811 /// input format
12812 const input_format_t input_format = input_format_t::json;
12813
12814 /// the SAX parser
12815 json_sax_t* sax = nullptr;
12816
12817 // excluded markers in bjdata optimized type
12818 #define JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_ \
12819 make_array<char_int_type>('F', 'H', 'N', 'S', 'T', 'Z', '[', '{')
12820
12821 #define JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_ \
12822 make_array<bjd_type>( \
12823 bjd_type{'B', "byte"}, \
12824 bjd_type{'C', "char"}, \
12825 bjd_type{'D', "double"}, \
12826 bjd_type{'I', "int16"}, \
12827 bjd_type{'L', "int64"}, \
12828 bjd_type{'M', "uint64"}, \
12829 bjd_type{'U', "uint8"}, \
12830 bjd_type{'d', "single"}, \
12831 bjd_type{'i', "int8"}, \
12832 bjd_type{'l', "int32"}, \
12833 bjd_type{'m', "uint32"}, \
12834 bjd_type{'u', "uint16"})
12835
12836 JSON_PRIVATE_UNLESS_TESTED:
12837 // lookup tables
12838 // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
12839 const decltype(JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_) bjd_optimized_type_markers =
12840 JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_;
12841
12842 using bjd_type = std::pair<char_int_type, string_t>;
12843 // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
12844 const decltype(JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_) bjd_types_map =
12845 JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_;
12846
12847 #undef JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_
12848 #undef JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_
12849 };
12850
12851 #ifndef JSON_HAS_CPP_17
12852 template<typename BasicJsonType, typename InputAdapterType, typename SAX>
12853 constexpr std::size_t binary_reader<BasicJsonType, InputAdapterType, SAX>::npos;
12854 #endif
12855
12856 } // namespace detail
12857 NLOHMANN_JSON_NAMESPACE_END
12858
12859 // #include <nlohmann/detail/input/input_adapters.hpp>
12860
12861 // #include <nlohmann/detail/input/lexer.hpp>
12862
12863 // #include <nlohmann/detail/input/parser.hpp>
12864 // __ _____ _____ _____
12865 // __| | __| | | | JSON for Modern C++
12866 // | | |__ | | | | | | version 3.12.0
12867 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
12868 //
12869 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
12870 // SPDX-License-Identifier: MIT
12871
12872
12873
12874 #include <cmath> // isfinite
12875 #include <cstdint> // uint8_t
12876 #include <functional> // function
12877 #include <string> // string
12878 #include <utility> // move
12879 #include <vector> // vector
12880
12881 // #include <nlohmann/detail/exceptions.hpp>
12882
12883 // #include <nlohmann/detail/input/input_adapters.hpp>
12884
12885 // #include <nlohmann/detail/input/json_sax.hpp>
12886
12887 // #include <nlohmann/detail/input/lexer.hpp>
12888
12889 // #include <nlohmann/detail/macro_scope.hpp>
12890
12891 // #include <nlohmann/detail/meta/is_sax.hpp>
12892
12893 // #include <nlohmann/detail/string_concat.hpp>
12894
12895 // #include <nlohmann/detail/value_t.hpp>
12896
12897
12898 NLOHMANN_JSON_NAMESPACE_BEGIN
12899 namespace detail
12900 {
12901 ////////////
12902 // parser //
12903 ////////////
12904
12905 enum class parse_event_t : std::uint8_t
12906 {
12907 /// the parser read `{` and started to process a JSON object
12908 object_start,
12909 /// the parser read `}` and finished processing a JSON object
12910 object_end,
12911 /// the parser read `[` and started to process a JSON array
12912 array_start,
12913 /// the parser read `]` and finished processing a JSON array
12914 array_end,
12915 /// the parser read a key of a value in an object
12916 key,
12917 /// the parser finished reading a JSON value
12918 value
12919 };
12920
12921 template<typename BasicJsonType>
12922 using parser_callback_t =
12923 std::function<bool(int /*depth*/, parse_event_t /*event*/, BasicJsonType& /*parsed*/)>;
12924
12925 /*!
12926 @brief syntax analysis
12927
12928 This class implements a recursive descent parser.
12929 */
12930 template<typename BasicJsonType, typename InputAdapterType>
12931 class parser
12932 {
12933 using number_integer_t = typename BasicJsonType::number_integer_t;
12934 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
12935 using number_float_t = typename BasicJsonType::number_float_t;
12936 using string_t = typename BasicJsonType::string_t;
12937 using lexer_t = lexer<BasicJsonType, InputAdapterType>;
12938 using token_type = typename lexer_t::token_type;
12939
12940 public:
12941 /// a parser reading from an input adapter
12942 explicit parser(InputAdapterType&& adapter,
12943 parser_callback_t<BasicJsonType> cb = nullptr,
12944 const bool allow_exceptions_ = true,
12945 const bool skip_comments = false)
12946 : callback(std::move(cb))
12947 , m_lexer(std::move(adapter), skip_comments)
12948 , allow_exceptions(allow_exceptions_)
12949 {
12950 // read first token
12951 get_token();
12952 }
12953
12954 /*!
12955 @brief public parser interface
12956
12957 @param[in] strict whether to expect the last token to be EOF
12958 @param[in,out] result parsed JSON value
12959
12960 @throw parse_error.101 in case of an unexpected token
12961 @throw parse_error.102 if to_unicode fails or surrogate error
12962 @throw parse_error.103 if to_unicode fails
12963 */
12964 void parse(const bool strict, BasicJsonType& result)
12965 {
12966 if (callback)
12967 {
12968 json_sax_dom_callback_parser<BasicJsonType, InputAdapterType> sdp(result, callback, allow_exceptions, &m_lexer);
12969 sax_parse_internal(&sdp);
12970
12971 // in strict mode, input must be completely read
12972 if (strict && (get_token() != token_type::end_of_input))
12973 {
12974 sdp.parse_error(m_lexer.get_position(),
12975 m_lexer.get_token_string(),
12976 parse_error::create(101, m_lexer.get_position(),
12977 exception_message(token_type::end_of_input, "value"), nullptr));
12978 }
12979
12980 // in case of an error, return discarded value
12981 if (sdp.is_errored())
12982 {
12983 result = value_t::discarded;
12984 return;
12985 }
12986
12987 // set top-level value to null if it was discarded by the callback
12988 // function
12989 if (result.is_discarded())
12990 {
12991 result = nullptr;
12992 }
12993 }
12994 else
12995 {
12996 json_sax_dom_parser<BasicJsonType, InputAdapterType> sdp(result, allow_exceptions, &m_lexer);
12997 sax_parse_internal(&sdp);
12998
12999 // in strict mode, input must be completely read
13000 if (strict && (get_token() != token_type::end_of_input))
13001 {
13002 sdp.parse_error(m_lexer.get_position(),
13003 m_lexer.get_token_string(),
13004 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), nullptr));
13005 }
13006
13007 // in case of an error, return discarded value
13008 if (sdp.is_errored())
13009 {
13010 result = value_t::discarded;
13011 return;
13012 }
13013 }
13014
13015 result.assert_invariant();
13016 }
13017
13018 /*!
13019 @brief public accept interface
13020
13021 @param[in] strict whether to expect the last token to be EOF
13022 @return whether the input is a proper JSON text
13023 */
13024 bool accept(const bool strict = true)
13025 {
13026 json_sax_acceptor<BasicJsonType> sax_acceptor;
13027 return sax_parse(&sax_acceptor, strict);
13028 }
13029
13030 template<typename SAX>
13031 JSON_HEDLEY_NON_NULL(2)
13032 bool sax_parse(SAX* sax, const bool strict = true)
13033 {
13034 (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
13035 const bool result = sax_parse_internal(sax);
13036
13037 // strict mode: next byte must be EOF
13038 if (result && strict && (get_token() != token_type::end_of_input))
13039 {
13040 return sax->parse_error(m_lexer.get_position(),
13041 m_lexer.get_token_string(),
13042 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), nullptr));
13043 }
13044
13045 return result;
13046 }
13047
13048 private:
13049 template<typename SAX>
13050 JSON_HEDLEY_NON_NULL(2)
13051 bool sax_parse_internal(SAX* sax)
13052 {
13053 // stack to remember the hierarchy of structured values we are parsing
13054 // true = array; false = object
13055 std::vector<bool> states;
13056 // value to avoid a goto (see comment where set to true)
13057 bool skip_to_state_evaluation = false;
13058
13059 while (true)
13060 {
13061 if (!skip_to_state_evaluation)
13062 {
13063 // invariant: get_token() was called before each iteration
13064 switch (last_token)
13065 {
13066 case token_type::begin_object:
13067 {
13068 if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
13069 {
13070 return false;
13071 }
13072
13073 // closing } -> we are done
13074 if (get_token() == token_type::end_object)
13075 {
13076 if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
13077 {
13078 return false;
13079 }
13080 break;
13081 }
13082
13083 // parse key
13084 if (JSON_HEDLEY_UNLIKELY(last_token != token_type::value_string))
13085 {
13086 return sax->parse_error(m_lexer.get_position(),
13087 m_lexer.get_token_string(),
13088 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), nullptr));
13089 }
13090 if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
13091 {
13092 return false;
13093 }
13094
13095 // parse separator (:)
13096 if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
13097 {
13098 return sax->parse_error(m_lexer.get_position(),
13099 m_lexer.get_token_string(),
13100 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), nullptr));
13101 }
13102
13103 // remember we are now inside an object
13104 states.push_back(false);
13105
13106 // parse values
13107 get_token();
13108 continue;
13109 }
13110
13111 case token_type::begin_array:
13112 {
13113 if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
13114 {
13115 return false;
13116 }
13117
13118 // closing ] -> we are done
13119 if (get_token() == token_type::end_array)
13120 {
13121 if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
13122 {
13123 return false;
13124 }
13125 break;
13126 }
13127
13128 // remember we are now inside an array
13129 states.push_back(true);
13130
13131 // parse values (no need to call get_token)
13132 continue;
13133 }
13134
13135 case token_type::value_float:
13136 {
13137 const auto res = m_lexer.get_number_float();
13138
13139 if (JSON_HEDLEY_UNLIKELY(!std::isfinite(res)))
13140 {
13141 return sax->parse_error(m_lexer.get_position(),
13142 m_lexer.get_token_string(),
13143 out_of_range::create(406, concat("number overflow parsing '", m_lexer.get_token_string(), '\''), nullptr));
13144 }
13145
13146 if (JSON_HEDLEY_UNLIKELY(!sax->number_float(res, m_lexer.get_string())))
13147 {
13148 return false;
13149 }
13150
13151 break;
13152 }
13153
13154 case token_type::literal_false:
13155 {
13156 if (JSON_HEDLEY_UNLIKELY(!sax->boolean(false)))
13157 {
13158 return false;
13159 }
13160 break;
13161 }
13162
13163 case token_type::literal_null:
13164 {
13165 if (JSON_HEDLEY_UNLIKELY(!sax->null()))
13166 {
13167 return false;
13168 }
13169 break;
13170 }
13171
13172 case token_type::literal_true:
13173 {
13174 if (JSON_HEDLEY_UNLIKELY(!sax->boolean(true)))
13175 {
13176 return false;
13177 }
13178 break;
13179 }
13180
13181 case token_type::value_integer:
13182 {
13183 if (JSON_HEDLEY_UNLIKELY(!sax->number_integer(m_lexer.get_number_integer())))
13184 {
13185 return false;
13186 }
13187 break;
13188 }
13189
13190 case token_type::value_string:
13191 {
13192 if (JSON_HEDLEY_UNLIKELY(!sax->string(m_lexer.get_string())))
13193 {
13194 return false;
13195 }
13196 break;
13197 }
13198
13199 case token_type::value_unsigned:
13200 {
13201 if (JSON_HEDLEY_UNLIKELY(!sax->number_unsigned(m_lexer.get_number_unsigned())))
13202 {
13203 return false;
13204 }
13205 break;
13206 }
13207
13208 case token_type::parse_error:
13209 {
13210 // using "uninitialized" to avoid "expected" message
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::uninitialized, "value"), nullptr));
13214 }
13215 case token_type::end_of_input:
13216 {
13217 if (JSON_HEDLEY_UNLIKELY(m_lexer.get_position().chars_read_total == 1))
13218 {
13219 return sax->parse_error(m_lexer.get_position(),
13220 m_lexer.get_token_string(),
13221 parse_error::create(101, m_lexer.get_position(),
13222 "attempting to parse an empty input; check that your input string or stream contains the expected JSON", nullptr));
13223 }
13224
13225 return sax->parse_error(m_lexer.get_position(),
13226 m_lexer.get_token_string(),
13227 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::literal_or_value, "value"), nullptr));
13228 }
13229 case token_type::uninitialized:
13230 case token_type::end_array:
13231 case token_type::end_object:
13232 case token_type::name_separator:
13233 case token_type::value_separator:
13234 case token_type::literal_or_value:
13235 default: // the last token was unexpected
13236 {
13237 return sax->parse_error(m_lexer.get_position(),
13238 m_lexer.get_token_string(),
13239 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::literal_or_value, "value"), nullptr));
13240 }
13241 }
13242 }
13243 else
13244 {
13245 skip_to_state_evaluation = false;
13246 }
13247
13248 // we reached this line after we successfully parsed a value
13249 if (states.empty())
13250 {
13251 // empty stack: we reached the end of the hierarchy: done
13252 return true;
13253 }
13254
13255 if (states.back()) // array
13256 {
13257 // comma -> next value
13258 if (get_token() == token_type::value_separator)
13259 {
13260 // parse a new value
13261 get_token();
13262 continue;
13263 }
13264
13265 // closing ]
13266 if (JSON_HEDLEY_LIKELY(last_token == token_type::end_array))
13267 {
13268 if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
13269 {
13270 return false;
13271 }
13272
13273 // We are done with this array. Before we can parse a
13274 // new value, we need to evaluate the new state first.
13275 // By setting skip_to_state_evaluation to false, we
13276 // are effectively jumping to the beginning of this if.
13277 JSON_ASSERT(!states.empty());
13278 states.pop_back();
13279 skip_to_state_evaluation = true;
13280 continue;
13281 }
13282
13283 return sax->parse_error(m_lexer.get_position(),
13284 m_lexer.get_token_string(),
13285 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_array, "array"), nullptr));
13286 }
13287
13288 // states.back() is false -> object
13289
13290 // comma -> next value
13291 if (get_token() == token_type::value_separator)
13292 {
13293 // parse key
13294 if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::value_string))
13295 {
13296 return sax->parse_error(m_lexer.get_position(),
13297 m_lexer.get_token_string(),
13298 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), nullptr));
13299 }
13300
13301 if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
13302 {
13303 return false;
13304 }
13305
13306 // parse separator (:)
13307 if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
13308 {
13309 return sax->parse_error(m_lexer.get_position(),
13310 m_lexer.get_token_string(),
13311 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), nullptr));
13312 }
13313
13314 // parse values
13315 get_token();
13316 continue;
13317 }
13318
13319 // closing }
13320 if (JSON_HEDLEY_LIKELY(last_token == token_type::end_object))
13321 {
13322 if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
13323 {
13324 return false;
13325 }
13326
13327 // We are done with this object. Before we can parse a
13328 // new value, we need to evaluate the new state first.
13329 // By setting skip_to_state_evaluation to false, we
13330 // are effectively jumping to the beginning of this if.
13331 JSON_ASSERT(!states.empty());
13332 states.pop_back();
13333 skip_to_state_evaluation = true;
13334 continue;
13335 }
13336
13337 return sax->parse_error(m_lexer.get_position(),
13338 m_lexer.get_token_string(),
13339 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_object, "object"), nullptr));
13340 }
13341 }
13342
13343 /// get next token from lexer
13344 token_type get_token()
13345 {
13346 return last_token = m_lexer.scan();
13347 }
13348
13349 std::string exception_message(const token_type expected, const std::string& context)
13350 {
13351 std::string error_msg = "syntax error ";
13352
13353 if (!context.empty())
13354 {
13355 error_msg += concat("while parsing ", context, ' ');
13356 }
13357
13358 error_msg += "- ";
13359
13360 if (last_token == token_type::parse_error)
13361 {
13362 error_msg += concat(m_lexer.get_error_message(), "; last read: '",
13363 m_lexer.get_token_string(), '\'');
13364 }
13365 else
13366 {
13367 error_msg += concat("unexpected ", lexer_t::token_type_name(last_token));
13368 }
13369
13370 if (expected != token_type::uninitialized)
13371 {
13372 error_msg += concat("; expected ", lexer_t::token_type_name(expected));
13373 }
13374
13375 return error_msg;
13376 }
13377
13378 private:
13379 /// callback function
13380 const parser_callback_t<BasicJsonType> callback = nullptr;
13381 /// the type of the last read token
13382 token_type last_token = token_type::uninitialized;
13383 /// the lexer
13384 lexer_t m_lexer;
13385 /// whether to throw exceptions in case of errors
13386 const bool allow_exceptions = true;
13387 };
13388
13389 } // namespace detail
13390 NLOHMANN_JSON_NAMESPACE_END
13391
13392 // #include <nlohmann/detail/iterators/internal_iterator.hpp>
13393 // __ _____ _____ _____
13394 // __| | __| | | | JSON for Modern C++
13395 // | | |__ | | | | | | version 3.12.0
13396 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
13397 //
13398 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
13399 // SPDX-License-Identifier: MIT
13400
13401
13402
13403 // #include <nlohmann/detail/abi_macros.hpp>
13404
13405 // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
13406 // __ _____ _____ _____
13407 // __| | __| | | | JSON for Modern C++
13408 // | | |__ | | | | | | version 3.12.0
13409 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
13410 //
13411 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
13412 // SPDX-License-Identifier: MIT
13413
13414
13415
13416 #include <cstddef> // ptrdiff_t
13417 #include <limits> // numeric_limits
13418
13419 // #include <nlohmann/detail/macro_scope.hpp>
13420
13421
13422 NLOHMANN_JSON_NAMESPACE_BEGIN
13423 namespace detail
13424 {
13425
13426 /*
13427 @brief an iterator for primitive JSON types
13428
13429 This class models an iterator for primitive JSON types (boolean, number,
13430 string). It's only purpose is to allow the iterator/const_iterator classes
13431 to "iterate" over primitive values. Internally, the iterator is modeled by
13432 a `difference_type` variable. Value begin_value (`0`) models the begin,
13433 end_value (`1`) models past the end.
13434 */
13435 class primitive_iterator_t
13436 {
13437 private:
13438 using difference_type = std::ptrdiff_t;
13439 static constexpr difference_type begin_value = 0;
13440 static constexpr difference_type end_value = begin_value + 1;
13441
13442 JSON_PRIVATE_UNLESS_TESTED:
13443 /// iterator as signed integer type
13444 difference_type m_it = (std::numeric_limits<std::ptrdiff_t>::min)();
13445
13446 public:
13447 constexpr difference_type get_value() const noexcept
13448 {
13449 return m_it;
13450 }
13451
13452 /// set iterator to a defined beginning
13453 void set_begin() noexcept
13454 {
13455 m_it = begin_value;
13456 }
13457
13458 /// set iterator to a defined past the end
13459 void set_end() noexcept
13460 {
13461 m_it = end_value;
13462 }
13463
13464 /// return whether the iterator can be dereferenced
13465 constexpr bool is_begin() const noexcept
13466 {
13467 return m_it == begin_value;
13468 }
13469
13470 /// return whether the iterator is at end
13471 constexpr bool is_end() const noexcept
13472 {
13473 return m_it == end_value;
13474 }
13475
13476 friend constexpr bool operator==(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
13477 {
13478 return lhs.m_it == rhs.m_it;
13479 }
13480
13481 friend constexpr bool operator<(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
13482 {
13483 return lhs.m_it < rhs.m_it;
13484 }
13485
13486 primitive_iterator_t operator+(difference_type n) noexcept
13487 {
13488 auto result = *this;
13489 result += n;
13490 return result;
13491 }
13492
13493 friend constexpr difference_type operator-(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
13494 {
13495 return lhs.m_it - rhs.m_it;
13496 }
13497
13498 primitive_iterator_t& operator++() noexcept
13499 {
13500 ++m_it;
13501 return *this;
13502 }
13503
13504 primitive_iterator_t operator++(int)& noexcept // NOLINT(cert-dcl21-cpp)
13505 {
13506 auto result = *this;
13507 ++m_it;
13508 return result;
13509 }
13510
13511 primitive_iterator_t& operator--() noexcept
13512 {
13513 --m_it;
13514 return *this;
13515 }
13516
13517 primitive_iterator_t operator--(int)& noexcept // NOLINT(cert-dcl21-cpp)
13518 {
13519 auto result = *this;
13520 --m_it;
13521 return result;
13522 }
13523
13524 primitive_iterator_t& operator+=(difference_type n) noexcept
13525 {
13526 m_it += n;
13527 return *this;
13528 }
13529
13530 primitive_iterator_t& operator-=(difference_type n) noexcept
13531 {
13532 m_it -= n;
13533 return *this;
13534 }
13535 };
13536
13537 } // namespace detail
13538 NLOHMANN_JSON_NAMESPACE_END
13539
13540
13541 NLOHMANN_JSON_NAMESPACE_BEGIN
13542 namespace detail
13543 {
13544
13545 /*!
13546 @brief an iterator value
13547
13548 @note This structure could easily be a union, but MSVC currently does not allow
13549 unions members with complex constructors, see https://github.com/nlohmann/json/pull/105.
13550 */
13551 template<typename BasicJsonType> struct internal_iterator
13552 {
13553 /// iterator for JSON objects
13554 typename BasicJsonType::object_t::iterator object_iterator {};
13555 /// iterator for JSON arrays
13556 typename BasicJsonType::array_t::iterator array_iterator {};
13557 /// generic iterator for all other types
13558 primitive_iterator_t primitive_iterator {};
13559 };
13560
13561 } // namespace detail
13562 NLOHMANN_JSON_NAMESPACE_END
13563
13564 // #include <nlohmann/detail/iterators/iter_impl.hpp>
13565 // __ _____ _____ _____
13566 // __| | __| | | | JSON for Modern C++
13567 // | | |__ | | | | | | version 3.12.0
13568 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
13569 //
13570 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
13571 // SPDX-License-Identifier: MIT
13572
13573
13574
13575 #include <iterator> // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next
13576 #include <type_traits> // conditional, is_const, remove_const
13577
13578 // #include <nlohmann/detail/exceptions.hpp>
13579
13580 // #include <nlohmann/detail/iterators/internal_iterator.hpp>
13581
13582 // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
13583
13584 // #include <nlohmann/detail/macro_scope.hpp>
13585
13586 // #include <nlohmann/detail/meta/cpp_future.hpp>
13587
13588 // #include <nlohmann/detail/meta/type_traits.hpp>
13589
13590 // #include <nlohmann/detail/value_t.hpp>
13591
13592
13593 NLOHMANN_JSON_NAMESPACE_BEGIN
13594 namespace detail
13595 {
13596
13597 // forward declare, to be able to friend it later on
13598 template<typename IteratorType> class iteration_proxy;
13599 template<typename IteratorType> class iteration_proxy_value;
13600
13601 /*!
13602 @brief a template for a bidirectional iterator for the @ref basic_json class
13603 This class implements a both iterators (iterator and const_iterator) for the
13604 @ref basic_json class.
13605 @note An iterator is called *initialized* when a pointer to a JSON value has
13606 been set (e.g., by a constructor or a copy assignment). If the iterator is
13607 default-constructed, it is *uninitialized* and most methods are undefined.
13608 **The library uses assertions to detect calls on uninitialized iterators.**
13609 @requirement The class satisfies the following concept requirements:
13610 -
13611 [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
13612 The iterator that can be moved can be moved in both directions (i.e.
13613 incremented and decremented).
13614 @since version 1.0.0, simplified in version 2.0.9, change to bidirectional
13615 iterators in version 3.0.0 (see https://github.com/nlohmann/json/issues/593)
13616 */
13617 template<typename BasicJsonType>
13618 class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions)
13619 {
13620 /// the iterator with BasicJsonType of different const-ness
13621 using other_iter_impl = iter_impl<typename std::conditional<std::is_const<BasicJsonType>::value, typename std::remove_const<BasicJsonType>::type, const BasicJsonType>::type>;
13622 /// allow basic_json to access private members
13623 friend other_iter_impl;
13624 friend BasicJsonType;
13625 friend iteration_proxy<iter_impl>;
13626 friend iteration_proxy_value<iter_impl>;
13627
13628 using object_t = typename BasicJsonType::object_t;
13629 using array_t = typename BasicJsonType::array_t;
13630 // make sure BasicJsonType is basic_json or const basic_json
13631 static_assert(is_basic_json<typename std::remove_const<BasicJsonType>::type>::value,
13632 "iter_impl only accepts (const) basic_json");
13633 // superficial check for the LegacyBidirectionalIterator named requirement
13634 static_assert(std::is_base_of<std::bidirectional_iterator_tag, std::bidirectional_iterator_tag>::value
13635 && std::is_base_of<std::bidirectional_iterator_tag, typename std::iterator_traits<typename array_t::iterator>::iterator_category>::value,
13636 "basic_json iterator assumes array and object type iterators satisfy the LegacyBidirectionalIterator named requirement.");
13637
13638 public:
13639 /// The std::iterator class template (used as a base class to provide typedefs) is deprecated in C++17.
13640 /// The C++ Standard has never required user-defined iterators to derive from std::iterator.
13641 /// A user-defined iterator should provide publicly accessible typedefs named
13642 /// iterator_category, value_type, difference_type, pointer, and reference.
13643 /// Note that value_type is required to be non-const, even for constant iterators.
13644 using iterator_category = std::bidirectional_iterator_tag;
13645
13646 /// the type of the values when the iterator is dereferenced
13647 using value_type = typename BasicJsonType::value_type;
13648 /// a type to represent differences between iterators
13649 using difference_type = typename BasicJsonType::difference_type;
13650 /// defines a pointer to the type iterated over (value_type)
13651 using pointer = typename std::conditional<std::is_const<BasicJsonType>::value,
13652 typename BasicJsonType::const_pointer,
13653 typename BasicJsonType::pointer>::type;
13654 /// defines a reference to the type iterated over (value_type)
13655 using reference =
13656 typename std::conditional<std::is_const<BasicJsonType>::value,
13657 typename BasicJsonType::const_reference,
13658 typename BasicJsonType::reference>::type;
13659
13660 iter_impl() = default;
13661 ~iter_impl() = default;
13662 iter_impl(iter_impl&&) noexcept = default;
13663 iter_impl& operator=(iter_impl&&) noexcept = default;
13664
13665 /*!
13666 @brief constructor for a given JSON instance
13667 @param[in] object pointer to a JSON object for this iterator
13668 @pre object != nullptr
13669 @post The iterator is initialized; i.e. `m_object != nullptr`.
13670 */
13671 explicit iter_impl(pointer object) noexcept : m_object(object)
13672 {
13673 JSON_ASSERT(m_object != nullptr);
13674
13675 switch (m_object->m_data.m_type)
13676 {
13677 case value_t::object:
13678 {
13679 m_it.object_iterator = typename object_t::iterator();
13680 break;
13681 }
13682
13683 case value_t::array:
13684 {
13685 m_it.array_iterator = typename array_t::iterator();
13686 break;
13687 }
13688
13689 case value_t::null:
13690 case value_t::string:
13691 case value_t::boolean:
13692 case value_t::number_integer:
13693 case value_t::number_unsigned:
13694 case value_t::number_float:
13695 case value_t::binary:
13696 case value_t::discarded:
13697 default:
13698 {
13699 m_it.primitive_iterator = primitive_iterator_t();
13700 break;
13701 }
13702 }
13703 }
13704
13705 /*!
13706 @note The conventional copy constructor and copy assignment are implicitly
13707 defined. Combined with the following converting constructor and
13708 assignment, they support: (1) copy from iterator to iterator, (2)
13709 copy from const iterator to const iterator, and (3) conversion from
13710 iterator to const iterator. However conversion from const iterator
13711 to iterator is not defined.
13712 */
13713
13714 /*!
13715 @brief const copy constructor
13716 @param[in] other const iterator to copy from
13717 @note This copy constructor had to be defined explicitly to circumvent a bug
13718 occurring on msvc v19.0 compiler (VS 2015) debug build. For more
13719 information refer to: https://github.com/nlohmann/json/issues/1608
13720 */
13721 iter_impl(const iter_impl<const BasicJsonType>& other) noexcept
13722 : m_object(other.m_object), m_it(other.m_it)
13723 {}
13724
13725 /*!
13726 @brief converting assignment
13727 @param[in] other const iterator to copy from
13728 @return const/non-const iterator
13729 @note It is not checked whether @a other is initialized.
13730 */
13731 iter_impl& operator=(const iter_impl<const BasicJsonType>& other) noexcept
13732 {
13733 if (&other != this)
13734 {
13735 m_object = other.m_object;
13736 m_it = other.m_it;
13737 }
13738 return *this;
13739 }
13740
13741 /*!
13742 @brief converting constructor
13743 @param[in] other non-const iterator to copy from
13744 @note It is not checked whether @a other is initialized.
13745 */
13746 iter_impl(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept
13747 : m_object(other.m_object), m_it(other.m_it)
13748 {}
13749
13750 /*!
13751 @brief converting assignment
13752 @param[in] other non-const iterator to copy from
13753 @return const/non-const iterator
13754 @note It is not checked whether @a other is initialized.
13755 */
13756 iter_impl& operator=(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept // NOLINT(cert-oop54-cpp)
13757 {
13758 m_object = other.m_object;
13759 m_it = other.m_it;
13760 return *this;
13761 }
13762
13763 JSON_PRIVATE_UNLESS_TESTED:
13764 /*!
13765 @brief set the iterator to the first value
13766 @pre The iterator is initialized; i.e. `m_object != nullptr`.
13767 */
13768 void set_begin() noexcept
13769 {
13770 JSON_ASSERT(m_object != nullptr);
13771
13772 switch (m_object->m_data.m_type)
13773 {
13774 case value_t::object:
13775 {
13776 m_it.object_iterator = m_object->m_data.m_value.object->begin();
13777 break;
13778 }
13779
13780 case value_t::array:
13781 {
13782 m_it.array_iterator = m_object->m_data.m_value.array->begin();
13783 break;
13784 }
13785
13786 case value_t::null:
13787 {
13788 // set to end so begin()==end() is true: null is empty
13789 m_it.primitive_iterator.set_end();
13790 break;
13791 }
13792
13793 case value_t::string:
13794 case value_t::boolean:
13795 case value_t::number_integer:
13796 case value_t::number_unsigned:
13797 case value_t::number_float:
13798 case value_t::binary:
13799 case value_t::discarded:
13800 default:
13801 {
13802 m_it.primitive_iterator.set_begin();
13803 break;
13804 }
13805 }
13806 }
13807
13808 /*!
13809 @brief set the iterator past the last value
13810 @pre The iterator is initialized; i.e. `m_object != nullptr`.
13811 */
13812 void set_end() noexcept
13813 {
13814 JSON_ASSERT(m_object != nullptr);
13815
13816 switch (m_object->m_data.m_type)
13817 {
13818 case value_t::object:
13819 {
13820 m_it.object_iterator = m_object->m_data.m_value.object->end();
13821 break;
13822 }
13823
13824 case value_t::array:
13825 {
13826 m_it.array_iterator = m_object->m_data.m_value.array->end();
13827 break;
13828 }
13829
13830 case value_t::null:
13831 case value_t::string:
13832 case value_t::boolean:
13833 case value_t::number_integer:
13834 case value_t::number_unsigned:
13835 case value_t::number_float:
13836 case value_t::binary:
13837 case value_t::discarded:
13838 default:
13839 {
13840 m_it.primitive_iterator.set_end();
13841 break;
13842 }
13843 }
13844 }
13845
13846 public:
13847 /*!
13848 @brief return a reference to the value pointed to by the iterator
13849 @pre The iterator is initialized; i.e. `m_object != nullptr`.
13850 */
13851 reference operator*() const
13852 {
13853 JSON_ASSERT(m_object != nullptr);
13854
13855 switch (m_object->m_data.m_type)
13856 {
13857 case value_t::object:
13858 {
13859 JSON_ASSERT(m_it.object_iterator != m_object->m_data.m_value.object->end());
13860 return m_it.object_iterator->second;
13861 }
13862
13863 case value_t::array:
13864 {
13865 JSON_ASSERT(m_it.array_iterator != m_object->m_data.m_value.array->end());
13866 return *m_it.array_iterator;
13867 }
13868
13869 case value_t::null:
13870 JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
13871
13872 case value_t::string:
13873 case value_t::boolean:
13874 case value_t::number_integer:
13875 case value_t::number_unsigned:
13876 case value_t::number_float:
13877 case value_t::binary:
13878 case value_t::discarded:
13879 default:
13880 {
13881 if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
13882 {
13883 return *m_object;
13884 }
13885
13886 JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
13887 }
13888 }
13889 }
13890
13891 /*!
13892 @brief dereference the iterator
13893 @pre The iterator is initialized; i.e. `m_object != nullptr`.
13894 */
13895 pointer operator->() const
13896 {
13897 JSON_ASSERT(m_object != nullptr);
13898
13899 switch (m_object->m_data.m_type)
13900 {
13901 case value_t::object:
13902 {
13903 JSON_ASSERT(m_it.object_iterator != m_object->m_data.m_value.object->end());
13904 return &(m_it.object_iterator->second);
13905 }
13906
13907 case value_t::array:
13908 {
13909 JSON_ASSERT(m_it.array_iterator != m_object->m_data.m_value.array->end());
13910 return &*m_it.array_iterator;
13911 }
13912
13913 case value_t::null:
13914 case value_t::string:
13915 case value_t::boolean:
13916 case value_t::number_integer:
13917 case value_t::number_unsigned:
13918 case value_t::number_float:
13919 case value_t::binary:
13920 case value_t::discarded:
13921 default:
13922 {
13923 if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
13924 {
13925 return m_object;
13926 }
13927
13928 JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
13929 }
13930 }
13931 }
13932
13933 /*!
13934 @brief post-increment (it++)
13935 @pre The iterator is initialized; i.e. `m_object != nullptr`.
13936 */
13937 iter_impl operator++(int)& // NOLINT(cert-dcl21-cpp)
13938 {
13939 auto result = *this;
13940 ++(*this);
13941 return result;
13942 }
13943
13944 /*!
13945 @brief pre-increment (++it)
13946 @pre The iterator is initialized; i.e. `m_object != nullptr`.
13947 */
13948 iter_impl& operator++()
13949 {
13950 JSON_ASSERT(m_object != nullptr);
13951
13952 switch (m_object->m_data.m_type)
13953 {
13954 case value_t::object:
13955 {
13956 std::advance(m_it.object_iterator, 1);
13957 break;
13958 }
13959
13960 case value_t::array:
13961 {
13962 std::advance(m_it.array_iterator, 1);
13963 break;
13964 }
13965
13966 case value_t::null:
13967 case value_t::string:
13968 case value_t::boolean:
13969 case value_t::number_integer:
13970 case value_t::number_unsigned:
13971 case value_t::number_float:
13972 case value_t::binary:
13973 case value_t::discarded:
13974 default:
13975 {
13976 ++m_it.primitive_iterator;
13977 break;
13978 }
13979 }
13980
13981 return *this;
13982 }
13983
13984 /*!
13985 @brief post-decrement (it--)
13986 @pre The iterator is initialized; i.e. `m_object != nullptr`.
13987 */
13988 iter_impl operator--(int)& // NOLINT(cert-dcl21-cpp)
13989 {
13990 auto result = *this;
13991 --(*this);
13992 return result;
13993 }
13994
13995 /*!
13996 @brief pre-decrement (--it)
13997 @pre The iterator is initialized; i.e. `m_object != nullptr`.
13998 */
13999 iter_impl& operator--()
14000 {
14001 JSON_ASSERT(m_object != nullptr);
14002
14003 switch (m_object->m_data.m_type)
14004 {
14005 case value_t::object:
14006 {
14007 std::advance(m_it.object_iterator, -1);
14008 break;
14009 }
14010
14011 case value_t::array:
14012 {
14013 std::advance(m_it.array_iterator, -1);
14014 break;
14015 }
14016
14017 case value_t::null:
14018 case value_t::string:
14019 case value_t::boolean:
14020 case value_t::number_integer:
14021 case value_t::number_unsigned:
14022 case value_t::number_float:
14023 case value_t::binary:
14024 case value_t::discarded:
14025 default:
14026 {
14027 --m_it.primitive_iterator;
14028 break;
14029 }
14030 }
14031
14032 return *this;
14033 }
14034
14035 /*!
14036 @brief comparison: equal
14037 @pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
14038 */
14039 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 >
14040 bool operator==(const IterImpl& other) const
14041 {
14042 // if objects are not the same, the comparison is undefined
14043 if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
14044 {
14045 JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object));
14046 }
14047
14048 // value-initialized forward iterators can be compared, and must compare equal to other value-initialized iterators of the same type #4493
14049 if (m_object == nullptr)
14050 {
14051 return true;
14052 }
14053
14054 switch (m_object->m_data.m_type)
14055 {
14056 case value_t::object:
14057 return (m_it.object_iterator == other.m_it.object_iterator);
14058
14059 case value_t::array:
14060 return (m_it.array_iterator == other.m_it.array_iterator);
14061
14062 case value_t::null:
14063 case value_t::string:
14064 case value_t::boolean:
14065 case value_t::number_integer:
14066 case value_t::number_unsigned:
14067 case value_t::number_float:
14068 case value_t::binary:
14069 case value_t::discarded:
14070 default:
14071 return (m_it.primitive_iterator == other.m_it.primitive_iterator);
14072 }
14073 }
14074
14075 /*!
14076 @brief comparison: not equal
14077 @pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
14078 */
14079 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 >
14080 bool operator!=(const IterImpl& other) const
14081 {
14082 return !operator==(other);
14083 }
14084
14085 /*!
14086 @brief comparison: smaller
14087 @pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
14088 */
14089 bool operator<(const iter_impl& other) const
14090 {
14091 // if objects are not the same, the comparison is undefined
14092 if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
14093 {
14094 JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object));
14095 }
14096
14097 // value-initialized forward iterators can be compared, and must compare equal to other value-initialized iterators of the same type #4493
14098 if (m_object == nullptr)
14099 {
14100 // the iterators are both value-initialized and are to be considered equal, but this function checks for smaller, so we return false
14101 return false;
14102 }
14103
14104 switch (m_object->m_data.m_type)
14105 {
14106 case value_t::object:
14107 JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators", m_object));
14108
14109 case value_t::array:
14110 return (m_it.array_iterator < other.m_it.array_iterator);
14111
14112 case value_t::null:
14113 case value_t::string:
14114 case value_t::boolean:
14115 case value_t::number_integer:
14116 case value_t::number_unsigned:
14117 case value_t::number_float:
14118 case value_t::binary:
14119 case value_t::discarded:
14120 default:
14121 return (m_it.primitive_iterator < other.m_it.primitive_iterator);
14122 }
14123 }
14124
14125 /*!
14126 @brief comparison: less than or equal
14127 @pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
14128 */
14129 bool operator<=(const iter_impl& other) const
14130 {
14131 return !other.operator < (*this);
14132 }
14133
14134 /*!
14135 @brief comparison: greater than
14136 @pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
14137 */
14138 bool operator>(const iter_impl& other) const
14139 {
14140 return !operator<=(other);
14141 }
14142
14143 /*!
14144 @brief comparison: greater than or equal
14145 @pre (1) The iterator is initialized; i.e. `m_object != nullptr`, or (2) both iterators are value-initialized.
14146 */
14147 bool operator>=(const iter_impl& other) const
14148 {
14149 return !operator<(other);
14150 }
14151
14152 /*!
14153 @brief add to iterator
14154 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14155 */
14156 iter_impl& operator+=(difference_type i)
14157 {
14158 JSON_ASSERT(m_object != nullptr);
14159
14160 switch (m_object->m_data.m_type)
14161 {
14162 case value_t::object:
14163 JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", m_object));
14164
14165 case value_t::array:
14166 {
14167 std::advance(m_it.array_iterator, i);
14168 break;
14169 }
14170
14171 case value_t::null:
14172 case value_t::string:
14173 case value_t::boolean:
14174 case value_t::number_integer:
14175 case value_t::number_unsigned:
14176 case value_t::number_float:
14177 case value_t::binary:
14178 case value_t::discarded:
14179 default:
14180 {
14181 m_it.primitive_iterator += i;
14182 break;
14183 }
14184 }
14185
14186 return *this;
14187 }
14188
14189 /*!
14190 @brief subtract from iterator
14191 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14192 */
14193 iter_impl& operator-=(difference_type i)
14194 {
14195 return operator+=(-i);
14196 }
14197
14198 /*!
14199 @brief add to iterator
14200 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14201 */
14202 iter_impl operator+(difference_type i) const
14203 {
14204 auto result = *this;
14205 result += i;
14206 return result;
14207 }
14208
14209 /*!
14210 @brief addition of distance and iterator
14211 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14212 */
14213 friend iter_impl operator+(difference_type i, const iter_impl& it)
14214 {
14215 auto result = it;
14216 result += i;
14217 return result;
14218 }
14219
14220 /*!
14221 @brief subtract from iterator
14222 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14223 */
14224 iter_impl operator-(difference_type i) const
14225 {
14226 auto result = *this;
14227 result -= i;
14228 return result;
14229 }
14230
14231 /*!
14232 @brief return difference
14233 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14234 */
14235 difference_type operator-(const iter_impl& other) const
14236 {
14237 JSON_ASSERT(m_object != nullptr);
14238
14239 switch (m_object->m_data.m_type)
14240 {
14241 case value_t::object:
14242 JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", m_object));
14243
14244 case value_t::array:
14245 return m_it.array_iterator - other.m_it.array_iterator;
14246
14247 case value_t::null:
14248 case value_t::string:
14249 case value_t::boolean:
14250 case value_t::number_integer:
14251 case value_t::number_unsigned:
14252 case value_t::number_float:
14253 case value_t::binary:
14254 case value_t::discarded:
14255 default:
14256 return m_it.primitive_iterator - other.m_it.primitive_iterator;
14257 }
14258 }
14259
14260 /*!
14261 @brief access to successor
14262 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14263 */
14264 reference operator[](difference_type n) const
14265 {
14266 JSON_ASSERT(m_object != nullptr);
14267
14268 switch (m_object->m_data.m_type)
14269 {
14270 case value_t::object:
14271 JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators", m_object));
14272
14273 case value_t::array:
14274 return *std::next(m_it.array_iterator, n);
14275
14276 case value_t::null:
14277 JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
14278
14279 case value_t::string:
14280 case value_t::boolean:
14281 case value_t::number_integer:
14282 case value_t::number_unsigned:
14283 case value_t::number_float:
14284 case value_t::binary:
14285 case value_t::discarded:
14286 default:
14287 {
14288 if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.get_value() == -n))
14289 {
14290 return *m_object;
14291 }
14292
14293 JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
14294 }
14295 }
14296 }
14297
14298 /*!
14299 @brief return the key of an object iterator
14300 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14301 */
14302 const typename object_t::key_type& key() const
14303 {
14304 JSON_ASSERT(m_object != nullptr);
14305
14306 if (JSON_HEDLEY_LIKELY(m_object->is_object()))
14307 {
14308 return m_it.object_iterator->first;
14309 }
14310
14311 JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators", m_object));
14312 }
14313
14314 /*!
14315 @brief return the value of an iterator
14316 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14317 */
14318 reference value() const
14319 {
14320 return operator*();
14321 }
14322
14323 JSON_PRIVATE_UNLESS_TESTED:
14324 /// associated JSON instance
14325 pointer m_object = nullptr;
14326 /// the actual iterator of the associated instance
14327 internal_iterator<typename std::remove_const<BasicJsonType>::type> m_it {};
14328 };
14329
14330 } // namespace detail
14331 NLOHMANN_JSON_NAMESPACE_END
14332
14333 // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
14334
14335 // #include <nlohmann/detail/iterators/json_reverse_iterator.hpp>
14336 // __ _____ _____ _____
14337 // __| | __| | | | JSON for Modern C++
14338 // | | |__ | | | | | | version 3.12.0
14339 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
14340 //
14341 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
14342 // SPDX-License-Identifier: MIT
14343
14344
14345
14346 #include <cstddef> // ptrdiff_t
14347 #include <iterator> // reverse_iterator
14348 #include <utility> // declval
14349
14350 // #include <nlohmann/detail/abi_macros.hpp>
14351
14352
14353 NLOHMANN_JSON_NAMESPACE_BEGIN
14354 namespace detail
14355 {
14356
14357 //////////////////////
14358 // reverse_iterator //
14359 //////////////////////
14360
14361 /*!
14362 @brief a template for a reverse iterator class
14363
14364 @tparam Base the base iterator type to reverse. Valid types are @ref
14365 iterator (to create @ref reverse_iterator) and @ref const_iterator (to
14366 create @ref const_reverse_iterator).
14367
14368 @requirement The class satisfies the following concept requirements:
14369 -
14370 [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
14371 The iterator that can be moved can be moved in both directions (i.e.
14372 incremented and decremented).
14373 - [OutputIterator](https://en.cppreference.com/w/cpp/named_req/OutputIterator):
14374 It is possible to write to the pointed-to element (only if @a Base is
14375 @ref iterator).
14376
14377 @since version 1.0.0
14378 */
14379 template<typename Base>
14380 class json_reverse_iterator : public std::reverse_iterator<Base>
14381 {
14382 public:
14383 using difference_type = std::ptrdiff_t;
14384 /// shortcut to the reverse iterator adapter
14385 using base_iterator = std::reverse_iterator<Base>;
14386 /// the reference type for the pointed-to element
14387 using reference = typename Base::reference;
14388
14389 /// create reverse iterator from iterator
14390 explicit json_reverse_iterator(const typename base_iterator::iterator_type& it) noexcept
14391 : base_iterator(it) {}
14392
14393 /// create reverse iterator from base class
14394 explicit json_reverse_iterator(const base_iterator& it) noexcept : base_iterator(it) {}
14395
14396 /// post-increment (it++)
14397 json_reverse_iterator operator++(int)& // NOLINT(cert-dcl21-cpp)
14398 {
14399 return static_cast<json_reverse_iterator>(base_iterator::operator++(1));
14400 }
14401
14402 /// pre-increment (++it)
14403 json_reverse_iterator& operator++()
14404 {
14405 return static_cast<json_reverse_iterator&>(base_iterator::operator++());
14406 }
14407
14408 /// post-decrement (it--)
14409 json_reverse_iterator operator--(int)& // NOLINT(cert-dcl21-cpp)
14410 {
14411 return static_cast<json_reverse_iterator>(base_iterator::operator--(1));
14412 }
14413
14414 /// pre-decrement (--it)
14415 json_reverse_iterator& operator--()
14416 {
14417 return static_cast<json_reverse_iterator&>(base_iterator::operator--());
14418 }
14419
14420 /// add to iterator
14421 json_reverse_iterator& operator+=(difference_type i)
14422 {
14423 return static_cast<json_reverse_iterator&>(base_iterator::operator+=(i));
14424 }
14425
14426 /// add to iterator
14427 json_reverse_iterator operator+(difference_type i) const
14428 {
14429 return static_cast<json_reverse_iterator>(base_iterator::operator+(i));
14430 }
14431
14432 /// subtract from iterator
14433 json_reverse_iterator operator-(difference_type i) const
14434 {
14435 return static_cast<json_reverse_iterator>(base_iterator::operator-(i));
14436 }
14437
14438 /// return difference
14439 difference_type operator-(const json_reverse_iterator& other) const
14440 {
14441 return base_iterator(*this) - base_iterator(other);
14442 }
14443
14444 /// access to successor
14445 reference operator[](difference_type n) const
14446 {
14447 return *(this->operator+(n));
14448 }
14449
14450 /// return the key of an object iterator
14451 auto key() const -> decltype(std::declval<Base>().key())
14452 {
14453 auto it = --this->base();
14454 return it.key();
14455 }
14456
14457 /// return the value of an iterator
14458 reference value() const
14459 {
14460 auto it = --this->base();
14461 return it.operator * ();
14462 }
14463 };
14464
14465 } // namespace detail
14466 NLOHMANN_JSON_NAMESPACE_END
14467
14468 // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
14469
14470 // #include <nlohmann/detail/json_custom_base_class.hpp>
14471 // __ _____ _____ _____
14472 // __| | __| | | | JSON for Modern C++
14473 // | | |__ | | | | | | version 3.12.0
14474 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
14475 //
14476 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
14477 // SPDX-License-Identifier: MIT
14478
14479
14480
14481 #include <type_traits> // conditional, is_same
14482
14483 // #include <nlohmann/detail/abi_macros.hpp>
14484
14485
14486 NLOHMANN_JSON_NAMESPACE_BEGIN
14487 namespace detail
14488 {
14489
14490 /*!
14491 @brief Default base class of the @ref basic_json class.
14492
14493 So that the correct implementations of the copy / move ctors / assign operators
14494 of @ref basic_json do not require complex case distinctions
14495 (no base class / custom base class used as customization point),
14496 @ref basic_json always has a base class.
14497 By default, this class is used because it is empty and thus has no effect
14498 on the behavior of @ref basic_json.
14499 */
14500 struct json_default_base {};
14501
14502 template<class T>
14503 using json_base_class = typename std::conditional <
14504 std::is_same<T, void>::value,
14505 json_default_base,
14506 T
14507 >::type;
14508
14509 } // namespace detail
14510 NLOHMANN_JSON_NAMESPACE_END
14511
14512 // #include <nlohmann/detail/json_pointer.hpp>
14513 // __ _____ _____ _____
14514 // __| | __| | | | JSON for Modern C++
14515 // | | |__ | | | | | | version 3.12.0
14516 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
14517 //
14518 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
14519 // SPDX-License-Identifier: MIT
14520
14521
14522
14523 #include <algorithm> // all_of
14524 #include <cctype> // isdigit
14525 #include <cerrno> // errno, ERANGE
14526 #include <cstdlib> // strtoull
14527 #ifndef JSON_NO_IO
14528 #include <iosfwd> // ostream
14529 #endif // JSON_NO_IO
14530 #include <limits> // max
14531 #include <numeric> // accumulate
14532 #include <string> // string
14533 #include <utility> // move
14534 #include <vector> // vector
14535
14536 // #include <nlohmann/detail/exceptions.hpp>
14537
14538 // #include <nlohmann/detail/macro_scope.hpp>
14539
14540 // #include <nlohmann/detail/string_concat.hpp>
14541
14542 // #include <nlohmann/detail/string_escape.hpp>
14543
14544 // #include <nlohmann/detail/value_t.hpp>
14545
14546
14547 NLOHMANN_JSON_NAMESPACE_BEGIN
14548
14549 /// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document
14550 /// @sa https://json.nlohmann.me/api/json_pointer/
14551 template<typename RefStringType>
14552 class json_pointer
14553 {
14554 // allow basic_json to access private members
14555 NLOHMANN_BASIC_JSON_TPL_DECLARATION
14556 friend class basic_json;
14557
14558 template<typename>
14559 friend class json_pointer;
14560
14561 template<typename T>
14562 struct string_t_helper
14563 {
14564 using type = T;
14565 };
14566
14567 NLOHMANN_BASIC_JSON_TPL_DECLARATION
14568 struct string_t_helper<NLOHMANN_BASIC_JSON_TPL>
14569 {
14570 using type = StringType;
14571 };
14572
14573 public:
14574 // for backwards compatibility accept BasicJsonType
14575 using string_t = typename string_t_helper<RefStringType>::type;
14576
14577 /// @brief create JSON pointer
14578 /// @sa https://json.nlohmann.me/api/json_pointer/json_pointer/
14579 explicit json_pointer(const string_t& s = "")
14580 : reference_tokens(split(s))
14581 {}
14582
14583 /// @brief return a string representation of the JSON pointer
14584 /// @sa https://json.nlohmann.me/api/json_pointer/to_string/
14585 string_t to_string() const
14586 {
14587 return std::accumulate(reference_tokens.begin(), reference_tokens.end(),
14588 string_t{},
14589 [](const string_t& a, const string_t& b)
14590 {
14591 return detail::concat(a, '/', detail::escape(b));
14592 });
14593 }
14594
14595 /// @brief return a string representation of the JSON pointer
14596 /// @sa https://json.nlohmann.me/api/json_pointer/operator_string/
14597 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, to_string())
14598 operator string_t() const
14599 {
14600 return to_string();
14601 }
14602
14603 #ifndef JSON_NO_IO
14604 /// @brief write string representation of the JSON pointer to stream
14605 /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
14606 friend std::ostream& operator<<(std::ostream& o, const json_pointer& ptr)
14607 {
14608 o << ptr.to_string();
14609 return o;
14610 }
14611 #endif
14612
14613 /// @brief append another JSON pointer at the end of this JSON pointer
14614 /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
14615 json_pointer& operator/=(const json_pointer& ptr)
14616 {
14617 reference_tokens.insert(reference_tokens.end(),
14618 ptr.reference_tokens.begin(),
14619 ptr.reference_tokens.end());
14620 return *this;
14621 }
14622
14623 /// @brief append an unescaped reference token at the end of this JSON pointer
14624 /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
14625 json_pointer& operator/=(string_t token)
14626 {
14627 push_back(std::move(token));
14628 return *this;
14629 }
14630
14631 /// @brief append an array index at the end of this JSON pointer
14632 /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
14633 json_pointer& operator/=(std::size_t array_idx)
14634 {
14635 return *this /= std::to_string(array_idx);
14636 }
14637
14638 /// @brief create a new JSON pointer by appending the right JSON pointer at the end of the left JSON pointer
14639 /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
14640 friend json_pointer operator/(const json_pointer& lhs,
14641 const json_pointer& rhs)
14642 {
14643 return json_pointer(lhs) /= rhs;
14644 }
14645
14646 /// @brief create a new JSON pointer by appending the unescaped token at the end of the JSON pointer
14647 /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
14648 friend json_pointer operator/(const json_pointer& lhs, string_t token) // NOLINT(performance-unnecessary-value-param)
14649 {
14650 return json_pointer(lhs) /= std::move(token);
14651 }
14652
14653 /// @brief create a new JSON pointer by appending the array-index-token at the end of the JSON pointer
14654 /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
14655 friend json_pointer operator/(const json_pointer& lhs, std::size_t array_idx)
14656 {
14657 return json_pointer(lhs) /= array_idx;
14658 }
14659
14660 /// @brief returns the parent of this JSON pointer
14661 /// @sa https://json.nlohmann.me/api/json_pointer/parent_pointer/
14662 json_pointer parent_pointer() const
14663 {
14664 if (empty())
14665 {
14666 return *this;
14667 }
14668
14669 json_pointer res = *this;
14670 res.pop_back();
14671 return res;
14672 }
14673
14674 /// @brief remove last reference token
14675 /// @sa https://json.nlohmann.me/api/json_pointer/pop_back/
14676 void pop_back()
14677 {
14678 if (JSON_HEDLEY_UNLIKELY(empty()))
14679 {
14680 JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
14681 }
14682
14683 reference_tokens.pop_back();
14684 }
14685
14686 /// @brief return last reference token
14687 /// @sa https://json.nlohmann.me/api/json_pointer/back/
14688 const string_t& back() const
14689 {
14690 if (JSON_HEDLEY_UNLIKELY(empty()))
14691 {
14692 JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
14693 }
14694
14695 return reference_tokens.back();
14696 }
14697
14698 /// @brief append an unescaped token at the end of the reference pointer
14699 /// @sa https://json.nlohmann.me/api/json_pointer/push_back/
14700 void push_back(const string_t& token)
14701 {
14702 reference_tokens.push_back(token);
14703 }
14704
14705 /// @brief append an unescaped token at the end of the reference pointer
14706 /// @sa https://json.nlohmann.me/api/json_pointer/push_back/
14707 void push_back(string_t&& token)
14708 {
14709 reference_tokens.push_back(std::move(token));
14710 }
14711
14712 /// @brief return whether pointer points to the root document
14713 /// @sa https://json.nlohmann.me/api/json_pointer/empty/
14714 bool empty() const noexcept
14715 {
14716 return reference_tokens.empty();
14717 }
14718
14719 private:
14720 /*!
14721 @param[in] s reference token to be converted into an array index
14722
14723 @return integer representation of @a s
14724
14725 @throw parse_error.106 if an array index begins with '0'
14726 @throw parse_error.109 if an array index begins not with a digit
14727 @throw out_of_range.404 if string @a s could not be converted to an integer
14728 @throw out_of_range.410 if an array index exceeds size_type
14729 */
14730 template<typename BasicJsonType>
14731 static typename BasicJsonType::size_type array_index(const string_t& s)
14732 {
14733 using size_type = typename BasicJsonType::size_type;
14734
14735 // error condition (cf. RFC 6901, Sect. 4)
14736 if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && s[0] == '0'))
14737 {
14738 JSON_THROW(detail::parse_error::create(106, 0, detail::concat("array index '", s, "' must not begin with '0'"), nullptr));
14739 }
14740
14741 // error condition (cf. RFC 6901, Sect. 4)
14742 if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && !(s[0] >= '1' && s[0] <= '9')))
14743 {
14744 JSON_THROW(detail::parse_error::create(109, 0, detail::concat("array index '", s, "' is not a number"), nullptr));
14745 }
14746
14747 const char* p = s.c_str();
14748 char* p_end = nullptr; // NOLINT(misc-const-correctness)
14749 errno = 0; // strtoull doesn't reset errno
14750 const unsigned long long res = std::strtoull(p, &p_end, 10); // NOLINT(runtime/int)
14751 if (p == p_end // invalid input or empty string
14752 || errno == ERANGE // out of range
14753 || JSON_HEDLEY_UNLIKELY(static_cast<std::size_t>(p_end - p) != s.size())) // incomplete read
14754 {
14755 JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", s, "'"), nullptr));
14756 }
14757
14758 // only triggered on special platforms (like 32bit), see also
14759 // https://github.com/nlohmann/json/pull/2203
14760 if (res >= static_cast<unsigned long long>((std::numeric_limits<size_type>::max)())) // NOLINT(runtime/int)
14761 {
14762 JSON_THROW(detail::out_of_range::create(410, detail::concat("array index ", s, " exceeds size_type"), nullptr)); // LCOV_EXCL_LINE
14763 }
14764
14765 return static_cast<size_type>(res);
14766 }
14767
14768 JSON_PRIVATE_UNLESS_TESTED:
14769 json_pointer top() const
14770 {
14771 if (JSON_HEDLEY_UNLIKELY(empty()))
14772 {
14773 JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
14774 }
14775
14776 json_pointer result = *this;
14777 result.reference_tokens = {reference_tokens[0]};
14778 return result;
14779 }
14780
14781 private:
14782 /*!
14783 @brief create and return a reference to the pointed to value
14784
14785 @complexity Linear in the number of reference tokens.
14786
14787 @throw parse_error.109 if array index is not a number
14788 @throw type_error.313 if value cannot be unflattened
14789 */
14790 template<typename BasicJsonType>
14791 BasicJsonType& get_and_create(BasicJsonType& j) const
14792 {
14793 auto* result = &j;
14794
14795 // in case no reference tokens exist, return a reference to the JSON value
14796 // j which will be overwritten by a primitive value
14797 for (const auto& reference_token : reference_tokens)
14798 {
14799 switch (result->type())
14800 {
14801 case detail::value_t::null:
14802 {
14803 if (reference_token == "0")
14804 {
14805 // start a new array if reference token is 0
14806 result = &result->operator[](0);
14807 }
14808 else
14809 {
14810 // start a new object otherwise
14811 result = &result->operator[](reference_token);
14812 }
14813 break;
14814 }
14815
14816 case detail::value_t::object:
14817 {
14818 // create an entry in the object
14819 result = &result->operator[](reference_token);
14820 break;
14821 }
14822
14823 case detail::value_t::array:
14824 {
14825 // create an entry in the array
14826 result = &result->operator[](array_index<BasicJsonType>(reference_token));
14827 break;
14828 }
14829
14830 /*
14831 The following code is only reached if there exists a reference
14832 token _and_ the current value is primitive. In this case, we have
14833 an error situation, because primitive values may only occur as
14834 single value; that is, with an empty list of reference tokens.
14835 */
14836 case detail::value_t::string:
14837 case detail::value_t::boolean:
14838 case detail::value_t::number_integer:
14839 case detail::value_t::number_unsigned:
14840 case detail::value_t::number_float:
14841 case detail::value_t::binary:
14842 case detail::value_t::discarded:
14843 default:
14844 JSON_THROW(detail::type_error::create(313, "invalid value to unflatten", &j));
14845 }
14846 }
14847
14848 return *result;
14849 }
14850
14851 /*!
14852 @brief return a reference to the pointed to value
14853
14854 @note This version does not throw if a value is not present, but tries to
14855 create nested values instead. For instance, calling this function
14856 with pointer `"/this/that"` on a null value is equivalent to calling
14857 `operator[]("this").operator[]("that")` on that value, effectively
14858 changing the null value to an object.
14859
14860 @param[in] ptr a JSON value
14861
14862 @return reference to the JSON value pointed to by the JSON pointer
14863
14864 @complexity Linear in the length of the JSON pointer.
14865
14866 @throw parse_error.106 if an array index begins with '0'
14867 @throw parse_error.109 if an array index was not a number
14868 @throw out_of_range.404 if the JSON pointer can not be resolved
14869 */
14870 template<typename BasicJsonType>
14871 BasicJsonType& get_unchecked(BasicJsonType* ptr) const
14872 {
14873 for (const auto& reference_token : reference_tokens)
14874 {
14875 // convert null values to arrays or objects before continuing
14876 if (ptr->is_null())
14877 {
14878 // check if reference token is a number
14879 const bool nums =
14880 std::all_of(reference_token.begin(), reference_token.end(),
14881 [](const unsigned char x)
14882 {
14883 return std::isdigit(x);
14884 });
14885
14886 // change value to array for numbers or "-" or to object otherwise
14887 *ptr = (nums || reference_token == "-")
14888 ? detail::value_t::array
14889 : detail::value_t::object;
14890 }
14891
14892 switch (ptr->type())
14893 {
14894 case detail::value_t::object:
14895 {
14896 // use unchecked object access
14897 ptr = &ptr->operator[](reference_token);
14898 break;
14899 }
14900
14901 case detail::value_t::array:
14902 {
14903 if (reference_token == "-")
14904 {
14905 // explicitly treat "-" as index beyond the end
14906 ptr = &ptr->operator[](ptr->m_data.m_value.array->size());
14907 }
14908 else
14909 {
14910 // convert array index to number; unchecked access
14911 ptr = &ptr->operator[](array_index<BasicJsonType>(reference_token));
14912 }
14913 break;
14914 }
14915
14916 case detail::value_t::null:
14917 case detail::value_t::string:
14918 case detail::value_t::boolean:
14919 case detail::value_t::number_integer:
14920 case detail::value_t::number_unsigned:
14921 case detail::value_t::number_float:
14922 case detail::value_t::binary:
14923 case detail::value_t::discarded:
14924 default:
14925 JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
14926 }
14927 }
14928
14929 return *ptr;
14930 }
14931
14932 /*!
14933 @throw parse_error.106 if an array index begins with '0'
14934 @throw parse_error.109 if an array index was not a number
14935 @throw out_of_range.402 if the array index '-' is used
14936 @throw out_of_range.404 if the JSON pointer can not be resolved
14937 */
14938 template<typename BasicJsonType>
14939 BasicJsonType& get_checked(BasicJsonType* ptr) const
14940 {
14941 for (const auto& reference_token : reference_tokens)
14942 {
14943 switch (ptr->type())
14944 {
14945 case detail::value_t::object:
14946 {
14947 // note: at performs range check
14948 ptr = &ptr->at(reference_token);
14949 break;
14950 }
14951
14952 case detail::value_t::array:
14953 {
14954 if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
14955 {
14956 // "-" always fails the range check
14957 JSON_THROW(detail::out_of_range::create(402, detail::concat(
14958 "array index '-' (", std::to_string(ptr->m_data.m_value.array->size()),
14959 ") is out of range"), ptr));
14960 }
14961
14962 // note: at performs range check
14963 ptr = &ptr->at(array_index<BasicJsonType>(reference_token));
14964 break;
14965 }
14966
14967 case detail::value_t::null:
14968 case detail::value_t::string:
14969 case detail::value_t::boolean:
14970 case detail::value_t::number_integer:
14971 case detail::value_t::number_unsigned:
14972 case detail::value_t::number_float:
14973 case detail::value_t::binary:
14974 case detail::value_t::discarded:
14975 default:
14976 JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
14977 }
14978 }
14979
14980 return *ptr;
14981 }
14982
14983 /*!
14984 @brief return a const reference to the pointed to value
14985
14986 @param[in] ptr a JSON value
14987
14988 @return const reference to the JSON value pointed to by the JSON
14989 pointer
14990
14991 @throw parse_error.106 if an array index begins with '0'
14992 @throw parse_error.109 if an array index was not a number
14993 @throw out_of_range.402 if the array index '-' is used
14994 @throw out_of_range.404 if the JSON pointer can not be resolved
14995 */
14996 template<typename BasicJsonType>
14997 const BasicJsonType& get_unchecked(const BasicJsonType* ptr) const
14998 {
14999 for (const auto& reference_token : reference_tokens)
15000 {
15001 switch (ptr->type())
15002 {
15003 case detail::value_t::object:
15004 {
15005 // use unchecked object access
15006 ptr = &ptr->operator[](reference_token);
15007 break;
15008 }
15009
15010 case detail::value_t::array:
15011 {
15012 if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
15013 {
15014 // "-" cannot be used for const access
15015 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));
15016 }
15017
15018 // use unchecked array access
15019 ptr = &ptr->operator[](array_index<BasicJsonType>(reference_token));
15020 break;
15021 }
15022
15023 case detail::value_t::null:
15024 case detail::value_t::string:
15025 case detail::value_t::boolean:
15026 case detail::value_t::number_integer:
15027 case detail::value_t::number_unsigned:
15028 case detail::value_t::number_float:
15029 case detail::value_t::binary:
15030 case detail::value_t::discarded:
15031 default:
15032 JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
15033 }
15034 }
15035
15036 return *ptr;
15037 }
15038
15039 /*!
15040 @throw parse_error.106 if an array index begins with '0'
15041 @throw parse_error.109 if an array index was not a number
15042 @throw out_of_range.402 if the array index '-' is used
15043 @throw out_of_range.404 if the JSON pointer can not be resolved
15044 */
15045 template<typename BasicJsonType>
15046 const BasicJsonType& get_checked(const BasicJsonType* ptr) const
15047 {
15048 for (const auto& reference_token : reference_tokens)
15049 {
15050 switch (ptr->type())
15051 {
15052 case detail::value_t::object:
15053 {
15054 // note: at performs range check
15055 ptr = &ptr->at(reference_token);
15056 break;
15057 }
15058
15059 case detail::value_t::array:
15060 {
15061 if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
15062 {
15063 // "-" always fails the range check
15064 JSON_THROW(detail::out_of_range::create(402, detail::concat(
15065 "array index '-' (", std::to_string(ptr->m_data.m_value.array->size()),
15066 ") is out of range"), ptr));
15067 }
15068
15069 // note: at performs range check
15070 ptr = &ptr->at(array_index<BasicJsonType>(reference_token));
15071 break;
15072 }
15073
15074 case detail::value_t::null:
15075 case detail::value_t::string:
15076 case detail::value_t::boolean:
15077 case detail::value_t::number_integer:
15078 case detail::value_t::number_unsigned:
15079 case detail::value_t::number_float:
15080 case detail::value_t::binary:
15081 case detail::value_t::discarded:
15082 default:
15083 JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
15084 }
15085 }
15086
15087 return *ptr;
15088 }
15089
15090 /*!
15091 @throw parse_error.106 if an array index begins with '0'
15092 @throw parse_error.109 if an array index was not a number
15093 */
15094 template<typename BasicJsonType>
15095 bool contains(const BasicJsonType* ptr) const
15096 {
15097 for (const auto& reference_token : reference_tokens)
15098 {
15099 switch (ptr->type())
15100 {
15101 case detail::value_t::object:
15102 {
15103 if (!ptr->contains(reference_token))
15104 {
15105 // we did not find the key in the object
15106 return false;
15107 }
15108
15109 ptr = &ptr->operator[](reference_token);
15110 break;
15111 }
15112
15113 case detail::value_t::array:
15114 {
15115 if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
15116 {
15117 // "-" always fails the range check
15118 return false;
15119 }
15120 if (JSON_HEDLEY_UNLIKELY(reference_token.size() == 1 && !("0" <= reference_token && reference_token <= "9")))
15121 {
15122 // invalid char
15123 return false;
15124 }
15125 if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1))
15126 {
15127 if (JSON_HEDLEY_UNLIKELY(!('1' <= reference_token[0] && reference_token[0] <= '9')))
15128 {
15129 // first char should be between '1' and '9'
15130 return false;
15131 }
15132 for (std::size_t i = 1; i < reference_token.size(); i++)
15133 {
15134 if (JSON_HEDLEY_UNLIKELY(!('0' <= reference_token[i] && reference_token[i] <= '9')))
15135 {
15136 // other char should be between '0' and '9'
15137 return false;
15138 }
15139 }
15140 }
15141
15142 const auto idx = array_index<BasicJsonType>(reference_token);
15143 if (idx >= ptr->size())
15144 {
15145 // index out of range
15146 return false;
15147 }
15148
15149 ptr = &ptr->operator[](idx);
15150 break;
15151 }
15152
15153 case detail::value_t::null:
15154 case detail::value_t::string:
15155 case detail::value_t::boolean:
15156 case detail::value_t::number_integer:
15157 case detail::value_t::number_unsigned:
15158 case detail::value_t::number_float:
15159 case detail::value_t::binary:
15160 case detail::value_t::discarded:
15161 default:
15162 {
15163 // we do not expect primitive values if there is still a
15164 // reference token to process
15165 return false;
15166 }
15167 }
15168 }
15169
15170 // no reference token left means we found a primitive value
15171 return true;
15172 }
15173
15174 /*!
15175 @brief split the string input to reference tokens
15176
15177 @note This function is only called by the json_pointer constructor.
15178 All exceptions below are documented there.
15179
15180 @throw parse_error.107 if the pointer is not empty or begins with '/'
15181 @throw parse_error.108 if character '~' is not followed by '0' or '1'
15182 */
15183 static std::vector<string_t> split(const string_t& reference_string)
15184 {
15185 std::vector<string_t> result;
15186
15187 // special case: empty reference string -> no reference tokens
15188 if (reference_string.empty())
15189 {
15190 return result;
15191 }
15192
15193 // check if nonempty reference string begins with slash
15194 if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/'))
15195 {
15196 JSON_THROW(detail::parse_error::create(107, 1, detail::concat("JSON pointer must be empty or begin with '/' - was: '", reference_string, "'"), nullptr));
15197 }
15198
15199 // extract the reference tokens:
15200 // - slash: position of the last read slash (or end of string)
15201 // - start: position after the previous slash
15202 for (
15203 // search for the first slash after the first character
15204 std::size_t slash = reference_string.find_first_of('/', 1),
15205 // set the beginning of the first reference token
15206 start = 1;
15207 // we can stop if start == 0 (if slash == string_t::npos)
15208 start != 0;
15209 // set the beginning of the next reference token
15210 // (will eventually be 0 if slash == string_t::npos)
15211 start = (slash == string_t::npos) ? 0 : slash + 1,
15212 // find next slash
15213 slash = reference_string.find_first_of('/', start))
15214 {
15215 // use the text between the beginning of the reference token
15216 // (start) and the last slash (slash).
15217 auto reference_token = reference_string.substr(start, slash - start);
15218
15219 // check reference tokens are properly escaped
15220 for (std::size_t pos = reference_token.find_first_of('~');
15221 pos != string_t::npos;
15222 pos = reference_token.find_first_of('~', pos + 1))
15223 {
15224 JSON_ASSERT(reference_token[pos] == '~');
15225
15226 // ~ must be followed by 0 or 1
15227 if (JSON_HEDLEY_UNLIKELY(pos == reference_token.size() - 1 ||
15228 (reference_token[pos + 1] != '0' &&
15229 reference_token[pos + 1] != '1')))
15230 {
15231 JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'", nullptr));
15232 }
15233 }
15234
15235 // finally, store the reference token
15236 detail::unescape(reference_token);
15237 result.push_back(reference_token);
15238 }
15239
15240 return result;
15241 }
15242
15243 private:
15244 /*!
15245 @param[in] reference_string the reference string to the current value
15246 @param[in] value the value to consider
15247 @param[in,out] result the result object to insert values to
15248
15249 @note Empty objects or arrays are flattened to `null`.
15250 */
15251 template<typename BasicJsonType>
15252 static void flatten(const string_t& reference_string,
15253 const BasicJsonType& value,
15254 BasicJsonType& result)
15255 {
15256 switch (value.type())
15257 {
15258 case detail::value_t::array:
15259 {
15260 if (value.m_data.m_value.array->empty())
15261 {
15262 // flatten empty array as null
15263 result[reference_string] = nullptr;
15264 }
15265 else
15266 {
15267 // iterate array and use index as reference string
15268 for (std::size_t i = 0; i < value.m_data.m_value.array->size(); ++i)
15269 {
15270 flatten(detail::concat<string_t>(reference_string, '/', std::to_string(i)),
15271 value.m_data.m_value.array->operator[](i), result);
15272 }
15273 }
15274 break;
15275 }
15276
15277 case detail::value_t::object:
15278 {
15279 if (value.m_data.m_value.object->empty())
15280 {
15281 // flatten empty object as null
15282 result[reference_string] = nullptr;
15283 }
15284 else
15285 {
15286 // iterate object and use keys as reference string
15287 for (const auto& element : *value.m_data.m_value.object)
15288 {
15289 flatten(detail::concat<string_t>(reference_string, '/', detail::escape(element.first)), element.second, result);
15290 }
15291 }
15292 break;
15293 }
15294
15295 case detail::value_t::null:
15296 case detail::value_t::string:
15297 case detail::value_t::boolean:
15298 case detail::value_t::number_integer:
15299 case detail::value_t::number_unsigned:
15300 case detail::value_t::number_float:
15301 case detail::value_t::binary:
15302 case detail::value_t::discarded:
15303 default:
15304 {
15305 // add primitive value with its reference string
15306 result[reference_string] = value;
15307 break;
15308 }
15309 }
15310 }
15311
15312 /*!
15313 @param[in] value flattened JSON
15314
15315 @return unflattened JSON
15316
15317 @throw parse_error.109 if array index is not a number
15318 @throw type_error.314 if value is not an object
15319 @throw type_error.315 if object values are not primitive
15320 @throw type_error.313 if value cannot be unflattened
15321 */
15322 template<typename BasicJsonType>
15323 static BasicJsonType
15324 unflatten(const BasicJsonType& value)
15325 {
15326 if (JSON_HEDLEY_UNLIKELY(!value.is_object()))
15327 {
15328 JSON_THROW(detail::type_error::create(314, "only objects can be unflattened", &value));
15329 }
15330
15331 BasicJsonType result;
15332
15333 // iterate the JSON object values
15334 for (const auto& element : *value.m_data.m_value.object)
15335 {
15336 if (JSON_HEDLEY_UNLIKELY(!element.second.is_primitive()))
15337 {
15338 JSON_THROW(detail::type_error::create(315, "values in object must be primitive", &element.second));
15339 }
15340
15341 // assign value to reference pointed to by JSON pointer; Note that if
15342 // the JSON pointer is "" (i.e., points to the whole value), function
15343 // get_and_create returns a reference to result itself. An assignment
15344 // will then create a primitive value.
15345 json_pointer(element.first).get_and_create(result) = element.second;
15346 }
15347
15348 return result;
15349 }
15350
15351 // can't use conversion operator because of ambiguity
15352 json_pointer<string_t> convert() const&
15353 {
15354 json_pointer<string_t> result;
15355 result.reference_tokens = reference_tokens;
15356 return result;
15357 }
15358
15359 json_pointer<string_t> convert()&&
15360 {
15361 json_pointer<string_t> result;
15362 result.reference_tokens = std::move(reference_tokens);
15363 return result;
15364 }
15365
15366 public:
15367 #if JSON_HAS_THREE_WAY_COMPARISON
15368 /// @brief compares two JSON pointers for equality
15369 /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
15370 template<typename RefStringTypeRhs>
15371 bool operator==(const json_pointer<RefStringTypeRhs>& rhs) const noexcept
15372 {
15373 return reference_tokens == rhs.reference_tokens;
15374 }
15375
15376 /// @brief compares JSON pointer and string for equality
15377 /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
15378 JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer))
15379 bool operator==(const string_t& rhs) const
15380 {
15381 return *this == json_pointer(rhs);
15382 }
15383
15384 /// @brief 3-way compares two JSON pointers
15385 template<typename RefStringTypeRhs>
15386 std::strong_ordering operator<=>(const json_pointer<RefStringTypeRhs>& rhs) const noexcept // *NOPAD*
15387 {
15388 return reference_tokens <=> rhs.reference_tokens; // *NOPAD*
15389 }
15390 #else
15391 /// @brief compares two JSON pointers for equality
15392 /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
15393 template<typename RefStringTypeLhs, typename RefStringTypeRhs>
15394 // NOLINTNEXTLINE(readability-redundant-declaration)
15395 friend bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
15396 const json_pointer<RefStringTypeRhs>& rhs) noexcept;
15397
15398 /// @brief compares JSON pointer and string for equality
15399 /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
15400 template<typename RefStringTypeLhs, typename StringType>
15401 // NOLINTNEXTLINE(readability-redundant-declaration)
15402 friend bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
15403 const StringType& rhs);
15404
15405 /// @brief compares string and JSON pointer for equality
15406 /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
15407 template<typename RefStringTypeRhs, typename StringType>
15408 // NOLINTNEXTLINE(readability-redundant-declaration)
15409 friend bool operator==(const StringType& lhs,
15410 const json_pointer<RefStringTypeRhs>& rhs);
15411
15412 /// @brief compares two JSON pointers for inequality
15413 /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
15414 template<typename RefStringTypeLhs, typename RefStringTypeRhs>
15415 // NOLINTNEXTLINE(readability-redundant-declaration)
15416 friend bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
15417 const json_pointer<RefStringTypeRhs>& rhs) noexcept;
15418
15419 /// @brief compares JSON pointer and string for inequality
15420 /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
15421 template<typename RefStringTypeLhs, typename StringType>
15422 // NOLINTNEXTLINE(readability-redundant-declaration)
15423 friend bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
15424 const StringType& rhs);
15425
15426 /// @brief compares string and JSON pointer for inequality
15427 /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
15428 template<typename RefStringTypeRhs, typename StringType>
15429 // NOLINTNEXTLINE(readability-redundant-declaration)
15430 friend bool operator!=(const StringType& lhs,
15431 const json_pointer<RefStringTypeRhs>& rhs);
15432
15433 /// @brief compares two JSON pointer for less-than
15434 template<typename RefStringTypeLhs, typename RefStringTypeRhs>
15435 // NOLINTNEXTLINE(readability-redundant-declaration)
15436 friend bool operator<(const json_pointer<RefStringTypeLhs>& lhs,
15437 const json_pointer<RefStringTypeRhs>& rhs) noexcept;
15438 #endif
15439
15440 private:
15441 /// the reference tokens
15442 std::vector<string_t> reference_tokens;
15443 };
15444
15445 #if !JSON_HAS_THREE_WAY_COMPARISON
15446 // functions cannot be defined inside class due to ODR violations
15447 template<typename RefStringTypeLhs, typename RefStringTypeRhs>
15448 inline bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
15449 const json_pointer<RefStringTypeRhs>& rhs) noexcept
15450 {
15451 return lhs.reference_tokens == rhs.reference_tokens;
15452 }
15453
15454 template<typename RefStringTypeLhs,
15455 typename StringType = typename json_pointer<RefStringTypeLhs>::string_t>
15456 JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer, json_pointer))
15457 inline bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
15458 const StringType& rhs)
15459 {
15460 return lhs == json_pointer<RefStringTypeLhs>(rhs);
15461 }
15462
15463 template<typename RefStringTypeRhs,
15464 typename StringType = typename json_pointer<RefStringTypeRhs>::string_t>
15465 JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer, json_pointer))
15466 inline bool operator==(const StringType& lhs,
15467 const json_pointer<RefStringTypeRhs>& rhs)
15468 {
15469 return json_pointer<RefStringTypeRhs>(lhs) == rhs;
15470 }
15471
15472 template<typename RefStringTypeLhs, typename RefStringTypeRhs>
15473 inline bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
15474 const json_pointer<RefStringTypeRhs>& rhs) noexcept
15475 {
15476 return !(lhs == rhs);
15477 }
15478
15479 template<typename RefStringTypeLhs,
15480 typename StringType = typename json_pointer<RefStringTypeLhs>::string_t>
15481 JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator!=(json_pointer, json_pointer))
15482 inline bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
15483 const StringType& rhs)
15484 {
15485 return !(lhs == rhs);
15486 }
15487
15488 template<typename RefStringTypeRhs,
15489 typename StringType = typename json_pointer<RefStringTypeRhs>::string_t>
15490 JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator!=(json_pointer, json_pointer))
15491 inline bool operator!=(const StringType& lhs,
15492 const json_pointer<RefStringTypeRhs>& rhs)
15493 {
15494 return !(lhs == rhs);
15495 }
15496
15497 template<typename RefStringTypeLhs, typename RefStringTypeRhs>
15498 inline bool operator<(const json_pointer<RefStringTypeLhs>& lhs,
15499 const json_pointer<RefStringTypeRhs>& rhs) noexcept
15500 {
15501 return lhs.reference_tokens < rhs.reference_tokens;
15502 }
15503 #endif
15504
15505 NLOHMANN_JSON_NAMESPACE_END
15506
15507 // #include <nlohmann/detail/json_ref.hpp>
15508 // __ _____ _____ _____
15509 // __| | __| | | | JSON for Modern C++
15510 // | | |__ | | | | | | version 3.12.0
15511 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
15512 //
15513 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
15514 // SPDX-License-Identifier: MIT
15515
15516
15517
15518 #include <initializer_list>
15519 #include <utility>
15520
15521 // #include <nlohmann/detail/abi_macros.hpp>
15522
15523 // #include <nlohmann/detail/meta/type_traits.hpp>
15524
15525
15526 NLOHMANN_JSON_NAMESPACE_BEGIN
15527 namespace detail
15528 {
15529
15530 template<typename BasicJsonType>
15531 class json_ref
15532 {
15533 public:
15534 using value_type = BasicJsonType;
15535
15536 json_ref(value_type&& value)
15537 : owned_value(std::move(value))
15538 {}
15539
15540 json_ref(const value_type& value)
15541 : value_ref(&value)
15542 {}
15543
15544 json_ref(std::initializer_list<json_ref> init)
15545 : owned_value(init)
15546 {}
15547
15548 template <
15549 class... Args,
15550 enable_if_t<std::is_constructible<value_type, Args...>::value, int> = 0 >
15551 json_ref(Args && ... args)
15552 : owned_value(std::forward<Args>(args)...)
15553 {}
15554
15555 // class should be movable only
15556 json_ref(json_ref&&) noexcept = default;
15557 json_ref(const json_ref&) = delete;
15558 json_ref& operator=(const json_ref&) = delete;
15559 json_ref& operator=(json_ref&&) = delete;
15560 ~json_ref() = default;
15561
15562 value_type moved_or_copied() const
15563 {
15564 if (value_ref == nullptr)
15565 {
15566 return std::move(owned_value);
15567 }
15568 return *value_ref;
15569 }
15570
15571 value_type const& operator*() const
15572 {
15573 return value_ref ? *value_ref : owned_value;
15574 }
15575
15576 value_type const* operator->() const
15577 {
15578 return &** this;
15579 }
15580
15581 private:
15582 mutable value_type owned_value = nullptr;
15583 value_type const* value_ref = nullptr;
15584 };
15585
15586 } // namespace detail
15587 NLOHMANN_JSON_NAMESPACE_END
15588
15589 // #include <nlohmann/detail/macro_scope.hpp>
15590
15591 // #include <nlohmann/detail/string_concat.hpp>
15592
15593 // #include <nlohmann/detail/string_escape.hpp>
15594
15595 // #include <nlohmann/detail/string_utils.hpp>
15596
15597 // #include <nlohmann/detail/meta/cpp_future.hpp>
15598
15599 // #include <nlohmann/detail/meta/type_traits.hpp>
15600
15601 // #include <nlohmann/detail/output/binary_writer.hpp>
15602 // __ _____ _____ _____
15603 // __| | __| | | | JSON for Modern C++
15604 // | | |__ | | | | | | version 3.12.0
15605 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
15606 //
15607 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
15608 // SPDX-License-Identifier: MIT
15609
15610
15611
15612 #include <algorithm> // reverse
15613 #include <array> // array
15614 #include <map> // map
15615 #include <cmath> // isnan, isinf
15616 #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
15617 #include <cstring> // memcpy
15618 #include <limits> // numeric_limits
15619 #include <string> // string
15620 #include <utility> // move
15621 #include <vector> // vector
15622
15623 // #include <nlohmann/detail/input/binary_reader.hpp>
15624
15625 // #include <nlohmann/detail/macro_scope.hpp>
15626
15627 // #include <nlohmann/detail/output/output_adapters.hpp>
15628 // __ _____ _____ _____
15629 // __| | __| | | | JSON for Modern C++
15630 // | | |__ | | | | | | version 3.12.0
15631 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
15632 //
15633 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
15634 // SPDX-License-Identifier: MIT
15635
15636
15637
15638 #include <algorithm> // copy
15639 #include <cstddef> // size_t
15640 #include <iterator> // back_inserter
15641 #include <memory> // shared_ptr, make_shared
15642 #include <string> // basic_string
15643 #include <vector> // vector
15644
15645 #ifndef JSON_NO_IO
15646 #include <ios> // streamsize
15647 #include <ostream> // basic_ostream
15648 #endif // JSON_NO_IO
15649
15650 // #include <nlohmann/detail/macro_scope.hpp>
15651
15652
15653 NLOHMANN_JSON_NAMESPACE_BEGIN
15654 namespace detail
15655 {
15656
15657 /// abstract output adapter interface
15658 template<typename CharType> struct output_adapter_protocol
15659 {
15660 virtual void write_character(CharType c) = 0;
15661 virtual void write_characters(const CharType* s, std::size_t length) = 0;
15662 virtual ~output_adapter_protocol() = default;
15663
15664 output_adapter_protocol() = default;
15665 output_adapter_protocol(const output_adapter_protocol&) = default;
15666 output_adapter_protocol(output_adapter_protocol&&) noexcept = default;
15667 output_adapter_protocol& operator=(const output_adapter_protocol&) = default;
15668 output_adapter_protocol& operator=(output_adapter_protocol&&) noexcept = default;
15669 };
15670
15671 /// a type to simplify interfaces
15672 template<typename CharType>
15673 using output_adapter_t = std::shared_ptr<output_adapter_protocol<CharType>>;
15674
15675 /// output adapter for byte vectors
15676 template<typename CharType, typename AllocatorType = std::allocator<CharType>>
15677 class output_vector_adapter : public output_adapter_protocol<CharType>
15678 {
15679 public:
15680 explicit output_vector_adapter(std::vector<CharType, AllocatorType>& vec) noexcept
15681 : v(vec)
15682 {}
15683
15684 void write_character(CharType c) override
15685 {
15686 v.push_back(c);
15687 }
15688
15689 JSON_HEDLEY_NON_NULL(2)
15690 void write_characters(const CharType* s, std::size_t length) override
15691 {
15692 v.insert(v.end(), s, s + length);
15693 }
15694
15695 private:
15696 std::vector<CharType, AllocatorType>& v;
15697 };
15698
15699 #ifndef JSON_NO_IO
15700 /// output adapter for output streams
15701 template<typename CharType>
15702 class output_stream_adapter : public output_adapter_protocol<CharType>
15703 {
15704 public:
15705 explicit output_stream_adapter(std::basic_ostream<CharType>& s) noexcept
15706 : stream(s)
15707 {}
15708
15709 void write_character(CharType c) override
15710 {
15711 stream.put(c);
15712 }
15713
15714 JSON_HEDLEY_NON_NULL(2)
15715 void write_characters(const CharType* s, std::size_t length) override
15716 {
15717 stream.write(s, static_cast<std::streamsize>(length));
15718 }
15719
15720 private:
15721 std::basic_ostream<CharType>& stream;
15722 };
15723 #endif // JSON_NO_IO
15724
15725 /// output adapter for basic_string
15726 template<typename CharType, typename StringType = std::basic_string<CharType>>
15727 class output_string_adapter : public output_adapter_protocol<CharType>
15728 {
15729 public:
15730 explicit output_string_adapter(StringType& s) noexcept
15731 : str(s)
15732 {}
15733
15734 void write_character(CharType c) override
15735 {
15736 str.push_back(c);
15737 }
15738
15739 JSON_HEDLEY_NON_NULL(2)
15740 void write_characters(const CharType* s, std::size_t length) override
15741 {
15742 str.append(s, length);
15743 }
15744
15745 private:
15746 StringType& str;
15747 };
15748
15749 template<typename CharType, typename StringType = std::basic_string<CharType>>
15750 class output_adapter
15751 {
15752 public:
15753 template<typename AllocatorType = std::allocator<CharType>>
15754 output_adapter(std::vector<CharType, AllocatorType>& vec)
15755 : oa(std::make_shared<output_vector_adapter<CharType, AllocatorType>>(vec)) {}
15756
15757 #ifndef JSON_NO_IO
15758 output_adapter(std::basic_ostream<CharType>& s)
15759 : oa(std::make_shared<output_stream_adapter<CharType>>(s)) {}
15760 #endif // JSON_NO_IO
15761
15762 output_adapter(StringType& s)
15763 : oa(std::make_shared<output_string_adapter<CharType, StringType>>(s)) {}
15764
15765 operator output_adapter_t<CharType>()
15766 {
15767 return oa;
15768 }
15769
15770 private:
15771 output_adapter_t<CharType> oa = nullptr;
15772 };
15773
15774 } // namespace detail
15775 NLOHMANN_JSON_NAMESPACE_END
15776
15777 // #include <nlohmann/detail/string_concat.hpp>
15778
15779
15780 NLOHMANN_JSON_NAMESPACE_BEGIN
15781 namespace detail
15782 {
15783
15784 /// how to encode BJData
15785 enum class bjdata_version_t
15786 {
15787 draft2,
15788 draft3,
15789 };
15790
15791 ///////////////////
15792 // binary writer //
15793 ///////////////////
15794
15795 /*!
15796 @brief serialization to CBOR and MessagePack values
15797 */
15798 template<typename BasicJsonType, typename CharType>
15799 class binary_writer
15800 {
15801 using string_t = typename BasicJsonType::string_t;
15802 using binary_t = typename BasicJsonType::binary_t;
15803 using number_float_t = typename BasicJsonType::number_float_t;
15804
15805 public:
15806 /*!
15807 @brief create a binary writer
15808
15809 @param[in] adapter output adapter to write to
15810 */
15811 explicit binary_writer(output_adapter_t<CharType> adapter) : oa(std::move(adapter))
15812 {
15813 JSON_ASSERT(oa);
15814 }
15815
15816 /*!
15817 @param[in] j JSON value to serialize
15818 @pre j.type() == value_t::object
15819 */
15820 void write_bson(const BasicJsonType& j)
15821 {
15822 switch (j.type())
15823 {
15824 case value_t::object:
15825 {
15826 write_bson_object(*j.m_data.m_value.object);
15827 break;
15828 }
15829
15830 case value_t::null:
15831 case value_t::array:
15832 case value_t::string:
15833 case value_t::boolean:
15834 case value_t::number_integer:
15835 case value_t::number_unsigned:
15836 case value_t::number_float:
15837 case value_t::binary:
15838 case value_t::discarded:
15839 default:
15840 {
15841 JSON_THROW(type_error::create(317, concat("to serialize to BSON, top-level type must be object, but is ", j.type_name()), &j));
15842 }
15843 }
15844 }
15845
15846 /*!
15847 @param[in] j JSON value to serialize
15848 */
15849 void write_cbor(const BasicJsonType& j)
15850 {
15851 switch (j.type())
15852 {
15853 case value_t::null:
15854 {
15855 oa->write_character(to_char_type(0xF6));
15856 break;
15857 }
15858
15859 case value_t::boolean:
15860 {
15861 oa->write_character(j.m_data.m_value.boolean
15862 ? to_char_type(0xF5)
15863 : to_char_type(0xF4));
15864 break;
15865 }
15866
15867 case value_t::number_integer:
15868 {
15869 if (j.m_data.m_value.number_integer >= 0)
15870 {
15871 // CBOR does not differentiate between positive signed
15872 // integers and unsigned integers. Therefore, we used the
15873 // code from the value_t::number_unsigned case here.
15874 if (j.m_data.m_value.number_integer <= 0x17)
15875 {
15876 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
15877 }
15878 else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint8_t>::max)())
15879 {
15880 oa->write_character(to_char_type(0x18));
15881 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
15882 }
15883 else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint16_t>::max)())
15884 {
15885 oa->write_character(to_char_type(0x19));
15886 write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_integer));
15887 }
15888 else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint32_t>::max)())
15889 {
15890 oa->write_character(to_char_type(0x1A));
15891 write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_integer));
15892 }
15893 else
15894 {
15895 oa->write_character(to_char_type(0x1B));
15896 write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_integer));
15897 }
15898 }
15899 else
15900 {
15901 // The conversions below encode the sign in the first
15902 // byte, and the value is converted to a positive number.
15903 const auto positive_number = -1 - j.m_data.m_value.number_integer;
15904 if (j.m_data.m_value.number_integer >= -24)
15905 {
15906 write_number(static_cast<std::uint8_t>(0x20 + positive_number));
15907 }
15908 else if (positive_number <= (std::numeric_limits<std::uint8_t>::max)())
15909 {
15910 oa->write_character(to_char_type(0x38));
15911 write_number(static_cast<std::uint8_t>(positive_number));
15912 }
15913 else if (positive_number <= (std::numeric_limits<std::uint16_t>::max)())
15914 {
15915 oa->write_character(to_char_type(0x39));
15916 write_number(static_cast<std::uint16_t>(positive_number));
15917 }
15918 else if (positive_number <= (std::numeric_limits<std::uint32_t>::max)())
15919 {
15920 oa->write_character(to_char_type(0x3A));
15921 write_number(static_cast<std::uint32_t>(positive_number));
15922 }
15923 else
15924 {
15925 oa->write_character(to_char_type(0x3B));
15926 write_number(static_cast<std::uint64_t>(positive_number));
15927 }
15928 }
15929 break;
15930 }
15931
15932 case value_t::number_unsigned:
15933 {
15934 if (j.m_data.m_value.number_unsigned <= 0x17)
15935 {
15936 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_unsigned));
15937 }
15938 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
15939 {
15940 oa->write_character(to_char_type(0x18));
15941 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_unsigned));
15942 }
15943 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
15944 {
15945 oa->write_character(to_char_type(0x19));
15946 write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_unsigned));
15947 }
15948 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
15949 {
15950 oa->write_character(to_char_type(0x1A));
15951 write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_unsigned));
15952 }
15953 else
15954 {
15955 oa->write_character(to_char_type(0x1B));
15956 write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_unsigned));
15957 }
15958 break;
15959 }
15960
15961 case value_t::number_float:
15962 {
15963 if (std::isnan(j.m_data.m_value.number_float))
15964 {
15965 // NaN is 0xf97e00 in CBOR
15966 oa->write_character(to_char_type(0xF9));
15967 oa->write_character(to_char_type(0x7E));
15968 oa->write_character(to_char_type(0x00));
15969 }
15970 else if (std::isinf(j.m_data.m_value.number_float))
15971 {
15972 // Infinity is 0xf97c00, -Infinity is 0xf9fc00
15973 oa->write_character(to_char_type(0xf9));
15974 oa->write_character(j.m_data.m_value.number_float > 0 ? to_char_type(0x7C) : to_char_type(0xFC));
15975 oa->write_character(to_char_type(0x00));
15976 }
15977 else
15978 {
15979 write_compact_float(j.m_data.m_value.number_float, detail::input_format_t::cbor);
15980 }
15981 break;
15982 }
15983
15984 case value_t::string:
15985 {
15986 // step 1: write control byte and the string length
15987 const auto N = j.m_data.m_value.string->size();
15988 if (N <= 0x17)
15989 {
15990 write_number(static_cast<std::uint8_t>(0x60 + N));
15991 }
15992 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
15993 {
15994 oa->write_character(to_char_type(0x78));
15995 write_number(static_cast<std::uint8_t>(N));
15996 }
15997 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
15998 {
15999 oa->write_character(to_char_type(0x79));
16000 write_number(static_cast<std::uint16_t>(N));
16001 }
16002 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
16003 {
16004 oa->write_character(to_char_type(0x7A));
16005 write_number(static_cast<std::uint32_t>(N));
16006 }
16007 // LCOV_EXCL_START
16008 else if (N <= (std::numeric_limits<std::uint64_t>::max)())
16009 {
16010 oa->write_character(to_char_type(0x7B));
16011 write_number(static_cast<std::uint64_t>(N));
16012 }
16013 // LCOV_EXCL_STOP
16014
16015 // step 2: write the string
16016 oa->write_characters(
16017 reinterpret_cast<const CharType*>(j.m_data.m_value.string->c_str()),
16018 j.m_data.m_value.string->size());
16019 break;
16020 }
16021
16022 case value_t::array:
16023 {
16024 // step 1: write control byte and the array size
16025 const auto N = j.m_data.m_value.array->size();
16026 if (N <= 0x17)
16027 {
16028 write_number(static_cast<std::uint8_t>(0x80 + N));
16029 }
16030 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
16031 {
16032 oa->write_character(to_char_type(0x98));
16033 write_number(static_cast<std::uint8_t>(N));
16034 }
16035 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
16036 {
16037 oa->write_character(to_char_type(0x99));
16038 write_number(static_cast<std::uint16_t>(N));
16039 }
16040 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
16041 {
16042 oa->write_character(to_char_type(0x9A));
16043 write_number(static_cast<std::uint32_t>(N));
16044 }
16045 // LCOV_EXCL_START
16046 else if (N <= (std::numeric_limits<std::uint64_t>::max)())
16047 {
16048 oa->write_character(to_char_type(0x9B));
16049 write_number(static_cast<std::uint64_t>(N));
16050 }
16051 // LCOV_EXCL_STOP
16052
16053 // step 2: write each element
16054 for (const auto& el : *j.m_data.m_value.array)
16055 {
16056 write_cbor(el);
16057 }
16058 break;
16059 }
16060
16061 case value_t::binary:
16062 {
16063 if (j.m_data.m_value.binary->has_subtype())
16064 {
16065 if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint8_t>::max)())
16066 {
16067 write_number(static_cast<std::uint8_t>(0xd8));
16068 write_number(static_cast<std::uint8_t>(j.m_data.m_value.binary->subtype()));
16069 }
16070 else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint16_t>::max)())
16071 {
16072 write_number(static_cast<std::uint8_t>(0xd9));
16073 write_number(static_cast<std::uint16_t>(j.m_data.m_value.binary->subtype()));
16074 }
16075 else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint32_t>::max)())
16076 {
16077 write_number(static_cast<std::uint8_t>(0xda));
16078 write_number(static_cast<std::uint32_t>(j.m_data.m_value.binary->subtype()));
16079 }
16080 else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint64_t>::max)())
16081 {
16082 write_number(static_cast<std::uint8_t>(0xdb));
16083 write_number(static_cast<std::uint64_t>(j.m_data.m_value.binary->subtype()));
16084 }
16085 }
16086
16087 // step 1: write control byte and the binary array size
16088 const auto N = j.m_data.m_value.binary->size();
16089 if (N <= 0x17)
16090 {
16091 write_number(static_cast<std::uint8_t>(0x40 + N));
16092 }
16093 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
16094 {
16095 oa->write_character(to_char_type(0x58));
16096 write_number(static_cast<std::uint8_t>(N));
16097 }
16098 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
16099 {
16100 oa->write_character(to_char_type(0x59));
16101 write_number(static_cast<std::uint16_t>(N));
16102 }
16103 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
16104 {
16105 oa->write_character(to_char_type(0x5A));
16106 write_number(static_cast<std::uint32_t>(N));
16107 }
16108 // LCOV_EXCL_START
16109 else if (N <= (std::numeric_limits<std::uint64_t>::max)())
16110 {
16111 oa->write_character(to_char_type(0x5B));
16112 write_number(static_cast<std::uint64_t>(N));
16113 }
16114 // LCOV_EXCL_STOP
16115
16116 // step 2: write each element
16117 oa->write_characters(
16118 reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
16119 N);
16120
16121 break;
16122 }
16123
16124 case value_t::object:
16125 {
16126 // step 1: write control byte and the object size
16127 const auto N = j.m_data.m_value.object->size();
16128 if (N <= 0x17)
16129 {
16130 write_number(static_cast<std::uint8_t>(0xA0 + N));
16131 }
16132 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
16133 {
16134 oa->write_character(to_char_type(0xB8));
16135 write_number(static_cast<std::uint8_t>(N));
16136 }
16137 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
16138 {
16139 oa->write_character(to_char_type(0xB9));
16140 write_number(static_cast<std::uint16_t>(N));
16141 }
16142 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
16143 {
16144 oa->write_character(to_char_type(0xBA));
16145 write_number(static_cast<std::uint32_t>(N));
16146 }
16147 // LCOV_EXCL_START
16148 else if (N <= (std::numeric_limits<std::uint64_t>::max)())
16149 {
16150 oa->write_character(to_char_type(0xBB));
16151 write_number(static_cast<std::uint64_t>(N));
16152 }
16153 // LCOV_EXCL_STOP
16154
16155 // step 2: write each element
16156 for (const auto& el : *j.m_data.m_value.object)
16157 {
16158 write_cbor(el.first);
16159 write_cbor(el.second);
16160 }
16161 break;
16162 }
16163
16164 case value_t::discarded:
16165 default:
16166 break;
16167 }
16168 }
16169
16170 /*!
16171 @param[in] j JSON value to serialize
16172 */
16173 void write_msgpack(const BasicJsonType& j)
16174 {
16175 switch (j.type())
16176 {
16177 case value_t::null: // nil
16178 {
16179 oa->write_character(to_char_type(0xC0));
16180 break;
16181 }
16182
16183 case value_t::boolean: // true and false
16184 {
16185 oa->write_character(j.m_data.m_value.boolean
16186 ? to_char_type(0xC3)
16187 : to_char_type(0xC2));
16188 break;
16189 }
16190
16191 case value_t::number_integer:
16192 {
16193 if (j.m_data.m_value.number_integer >= 0)
16194 {
16195 // MessagePack does not differentiate between positive
16196 // signed integers and unsigned integers. Therefore, we used
16197 // the code from the value_t::number_unsigned case here.
16198 if (j.m_data.m_value.number_unsigned < 128)
16199 {
16200 // positive fixnum
16201 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
16202 }
16203 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
16204 {
16205 // uint 8
16206 oa->write_character(to_char_type(0xCC));
16207 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
16208 }
16209 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
16210 {
16211 // uint 16
16212 oa->write_character(to_char_type(0xCD));
16213 write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_integer));
16214 }
16215 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
16216 {
16217 // uint 32
16218 oa->write_character(to_char_type(0xCE));
16219 write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_integer));
16220 }
16221 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
16222 {
16223 // uint 64
16224 oa->write_character(to_char_type(0xCF));
16225 write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_integer));
16226 }
16227 }
16228 else
16229 {
16230 if (j.m_data.m_value.number_integer >= -32)
16231 {
16232 // negative fixnum
16233 write_number(static_cast<std::int8_t>(j.m_data.m_value.number_integer));
16234 }
16235 else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int8_t>::min)() &&
16236 j.m_data.m_value.number_integer <= (std::numeric_limits<std::int8_t>::max)())
16237 {
16238 // int 8
16239 oa->write_character(to_char_type(0xD0));
16240 write_number(static_cast<std::int8_t>(j.m_data.m_value.number_integer));
16241 }
16242 else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int16_t>::min)() &&
16243 j.m_data.m_value.number_integer <= (std::numeric_limits<std::int16_t>::max)())
16244 {
16245 // int 16
16246 oa->write_character(to_char_type(0xD1));
16247 write_number(static_cast<std::int16_t>(j.m_data.m_value.number_integer));
16248 }
16249 else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int32_t>::min)() &&
16250 j.m_data.m_value.number_integer <= (std::numeric_limits<std::int32_t>::max)())
16251 {
16252 // int 32
16253 oa->write_character(to_char_type(0xD2));
16254 write_number(static_cast<std::int32_t>(j.m_data.m_value.number_integer));
16255 }
16256 else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int64_t>::min)() &&
16257 j.m_data.m_value.number_integer <= (std::numeric_limits<std::int64_t>::max)())
16258 {
16259 // int 64
16260 oa->write_character(to_char_type(0xD3));
16261 write_number(static_cast<std::int64_t>(j.m_data.m_value.number_integer));
16262 }
16263 }
16264 break;
16265 }
16266
16267 case value_t::number_unsigned:
16268 {
16269 if (j.m_data.m_value.number_unsigned < 128)
16270 {
16271 // positive fixnum
16272 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
16273 }
16274 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
16275 {
16276 // uint 8
16277 oa->write_character(to_char_type(0xCC));
16278 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
16279 }
16280 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
16281 {
16282 // uint 16
16283 oa->write_character(to_char_type(0xCD));
16284 write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_integer));
16285 }
16286 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
16287 {
16288 // uint 32
16289 oa->write_character(to_char_type(0xCE));
16290 write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_integer));
16291 }
16292 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
16293 {
16294 // uint 64
16295 oa->write_character(to_char_type(0xCF));
16296 write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_integer));
16297 }
16298 break;
16299 }
16300
16301 case value_t::number_float:
16302 {
16303 write_compact_float(j.m_data.m_value.number_float, detail::input_format_t::msgpack);
16304 break;
16305 }
16306
16307 case value_t::string:
16308 {
16309 // step 1: write control byte and the string length
16310 const auto N = j.m_data.m_value.string->size();
16311 if (N <= 31)
16312 {
16313 // fixstr
16314 write_number(static_cast<std::uint8_t>(0xA0 | N));
16315 }
16316 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
16317 {
16318 // str 8
16319 oa->write_character(to_char_type(0xD9));
16320 write_number(static_cast<std::uint8_t>(N));
16321 }
16322 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
16323 {
16324 // str 16
16325 oa->write_character(to_char_type(0xDA));
16326 write_number(static_cast<std::uint16_t>(N));
16327 }
16328 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
16329 {
16330 // str 32
16331 oa->write_character(to_char_type(0xDB));
16332 write_number(static_cast<std::uint32_t>(N));
16333 }
16334
16335 // step 2: write the string
16336 oa->write_characters(
16337 reinterpret_cast<const CharType*>(j.m_data.m_value.string->c_str()),
16338 j.m_data.m_value.string->size());
16339 break;
16340 }
16341
16342 case value_t::array:
16343 {
16344 // step 1: write control byte and the array size
16345 const auto N = j.m_data.m_value.array->size();
16346 if (N <= 15)
16347 {
16348 // fixarray
16349 write_number(static_cast<std::uint8_t>(0x90 | N));
16350 }
16351 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
16352 {
16353 // array 16
16354 oa->write_character(to_char_type(0xDC));
16355 write_number(static_cast<std::uint16_t>(N));
16356 }
16357 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
16358 {
16359 // array 32
16360 oa->write_character(to_char_type(0xDD));
16361 write_number(static_cast<std::uint32_t>(N));
16362 }
16363
16364 // step 2: write each element
16365 for (const auto& el : *j.m_data.m_value.array)
16366 {
16367 write_msgpack(el);
16368 }
16369 break;
16370 }
16371
16372 case value_t::binary:
16373 {
16374 // step 0: determine if the binary type has a set subtype to
16375 // determine whether to use the ext or fixext types
16376 const bool use_ext = j.m_data.m_value.binary->has_subtype();
16377
16378 // step 1: write control byte and the byte string length
16379 const auto N = j.m_data.m_value.binary->size();
16380 if (N <= (std::numeric_limits<std::uint8_t>::max)())
16381 {
16382 std::uint8_t output_type{};
16383 bool fixed = true;
16384 if (use_ext)
16385 {
16386 switch (N)
16387 {
16388 case 1:
16389 output_type = 0xD4; // fixext 1
16390 break;
16391 case 2:
16392 output_type = 0xD5; // fixext 2
16393 break;
16394 case 4:
16395 output_type = 0xD6; // fixext 4
16396 break;
16397 case 8:
16398 output_type = 0xD7; // fixext 8
16399 break;
16400 case 16:
16401 output_type = 0xD8; // fixext 16
16402 break;
16403 default:
16404 output_type = 0xC7; // ext 8
16405 fixed = false;
16406 break;
16407 }
16408
16409 }
16410 else
16411 {
16412 output_type = 0xC4; // bin 8
16413 fixed = false;
16414 }
16415
16416 oa->write_character(to_char_type(output_type));
16417 if (!fixed)
16418 {
16419 write_number(static_cast<std::uint8_t>(N));
16420 }
16421 }
16422 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
16423 {
16424 const std::uint8_t output_type = use_ext
16425 ? 0xC8 // ext 16
16426 : 0xC5; // bin 16
16427
16428 oa->write_character(to_char_type(output_type));
16429 write_number(static_cast<std::uint16_t>(N));
16430 }
16431 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
16432 {
16433 const std::uint8_t output_type = use_ext
16434 ? 0xC9 // ext 32
16435 : 0xC6; // bin 32
16436
16437 oa->write_character(to_char_type(output_type));
16438 write_number(static_cast<std::uint32_t>(N));
16439 }
16440
16441 // step 1.5: if this is an ext type, write the subtype
16442 if (use_ext)
16443 {
16444 write_number(static_cast<std::int8_t>(j.m_data.m_value.binary->subtype()));
16445 }
16446
16447 // step 2: write the byte string
16448 oa->write_characters(
16449 reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
16450 N);
16451
16452 break;
16453 }
16454
16455 case value_t::object:
16456 {
16457 // step 1: write control byte and the object size
16458 const auto N = j.m_data.m_value.object->size();
16459 if (N <= 15)
16460 {
16461 // fixmap
16462 write_number(static_cast<std::uint8_t>(0x80 | (N & 0xF)));
16463 }
16464 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
16465 {
16466 // map 16
16467 oa->write_character(to_char_type(0xDE));
16468 write_number(static_cast<std::uint16_t>(N));
16469 }
16470 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
16471 {
16472 // map 32
16473 oa->write_character(to_char_type(0xDF));
16474 write_number(static_cast<std::uint32_t>(N));
16475 }
16476
16477 // step 2: write each element
16478 for (const auto& el : *j.m_data.m_value.object)
16479 {
16480 write_msgpack(el.first);
16481 write_msgpack(el.second);
16482 }
16483 break;
16484 }
16485
16486 case value_t::discarded:
16487 default:
16488 break;
16489 }
16490 }
16491
16492 /*!
16493 @param[in] j JSON value to serialize
16494 @param[in] use_count whether to use '#' prefixes (optimized format)
16495 @param[in] use_type whether to use '$' prefixes (optimized format)
16496 @param[in] add_prefix whether prefixes need to be used for this value
16497 @param[in] use_bjdata whether write in BJData format, default is false
16498 @param[in] bjdata_version which BJData version to use, default is draft2
16499 */
16500 void write_ubjson(const BasicJsonType& j, const bool use_count,
16501 const bool use_type, const bool add_prefix = true,
16502 const bool use_bjdata = false, const bjdata_version_t bjdata_version = bjdata_version_t::draft2)
16503 {
16504 const bool bjdata_draft3 = use_bjdata && bjdata_version == bjdata_version_t::draft3;
16505
16506 switch (j.type())
16507 {
16508 case value_t::null:
16509 {
16510 if (add_prefix)
16511 {
16512 oa->write_character(to_char_type('Z'));
16513 }
16514 break;
16515 }
16516
16517 case value_t::boolean:
16518 {
16519 if (add_prefix)
16520 {
16521 oa->write_character(j.m_data.m_value.boolean
16522 ? to_char_type('T')
16523 : to_char_type('F'));
16524 }
16525 break;
16526 }
16527
16528 case value_t::number_integer:
16529 {
16530 write_number_with_ubjson_prefix(j.m_data.m_value.number_integer, add_prefix, use_bjdata);
16531 break;
16532 }
16533
16534 case value_t::number_unsigned:
16535 {
16536 write_number_with_ubjson_prefix(j.m_data.m_value.number_unsigned, add_prefix, use_bjdata);
16537 break;
16538 }
16539
16540 case value_t::number_float:
16541 {
16542 write_number_with_ubjson_prefix(j.m_data.m_value.number_float, add_prefix, use_bjdata);
16543 break;
16544 }
16545
16546 case value_t::string:
16547 {
16548 if (add_prefix)
16549 {
16550 oa->write_character(to_char_type('S'));
16551 }
16552 write_number_with_ubjson_prefix(j.m_data.m_value.string->size(), true, use_bjdata);
16553 oa->write_characters(
16554 reinterpret_cast<const CharType*>(j.m_data.m_value.string->c_str()),
16555 j.m_data.m_value.string->size());
16556 break;
16557 }
16558
16559 case value_t::array:
16560 {
16561 if (add_prefix)
16562 {
16563 oa->write_character(to_char_type('['));
16564 }
16565
16566 bool prefix_required = true;
16567 if (use_type && !j.m_data.m_value.array->empty())
16568 {
16569 JSON_ASSERT(use_count);
16570 const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata);
16571 const bool same_prefix = std::all_of(j.begin() + 1, j.end(),
16572 [this, first_prefix, use_bjdata](const BasicJsonType & v)
16573 {
16574 return ubjson_prefix(v, use_bjdata) == first_prefix;
16575 });
16576
16577 std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
16578
16579 if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
16580 {
16581 prefix_required = false;
16582 oa->write_character(to_char_type('$'));
16583 oa->write_character(first_prefix);
16584 }
16585 }
16586
16587 if (use_count)
16588 {
16589 oa->write_character(to_char_type('#'));
16590 write_number_with_ubjson_prefix(j.m_data.m_value.array->size(), true, use_bjdata);
16591 }
16592
16593 for (const auto& el : *j.m_data.m_value.array)
16594 {
16595 write_ubjson(el, use_count, use_type, prefix_required, use_bjdata, bjdata_version);
16596 }
16597
16598 if (!use_count)
16599 {
16600 oa->write_character(to_char_type(']'));
16601 }
16602
16603 break;
16604 }
16605
16606 case value_t::binary:
16607 {
16608 if (add_prefix)
16609 {
16610 oa->write_character(to_char_type('['));
16611 }
16612
16613 if (use_type && (bjdata_draft3 || !j.m_data.m_value.binary->empty()))
16614 {
16615 JSON_ASSERT(use_count);
16616 oa->write_character(to_char_type('$'));
16617 oa->write_character(bjdata_draft3 ? 'B' : 'U');
16618 }
16619
16620 if (use_count)
16621 {
16622 oa->write_character(to_char_type('#'));
16623 write_number_with_ubjson_prefix(j.m_data.m_value.binary->size(), true, use_bjdata);
16624 }
16625
16626 if (use_type)
16627 {
16628 oa->write_characters(
16629 reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
16630 j.m_data.m_value.binary->size());
16631 }
16632 else
16633 {
16634 for (size_t i = 0; i < j.m_data.m_value.binary->size(); ++i)
16635 {
16636 oa->write_character(to_char_type(bjdata_draft3 ? 'B' : 'U'));
16637 oa->write_character(j.m_data.m_value.binary->data()[i]);
16638 }
16639 }
16640
16641 if (!use_count)
16642 {
16643 oa->write_character(to_char_type(']'));
16644 }
16645
16646 break;
16647 }
16648
16649 case value_t::object:
16650 {
16651 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())
16652 {
16653 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)
16654 {
16655 break;
16656 }
16657 }
16658
16659 if (add_prefix)
16660 {
16661 oa->write_character(to_char_type('{'));
16662 }
16663
16664 bool prefix_required = true;
16665 if (use_type && !j.m_data.m_value.object->empty())
16666 {
16667 JSON_ASSERT(use_count);
16668 const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata);
16669 const bool same_prefix = std::all_of(j.begin(), j.end(),
16670 [this, first_prefix, use_bjdata](const BasicJsonType & v)
16671 {
16672 return ubjson_prefix(v, use_bjdata) == first_prefix;
16673 });
16674
16675 std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
16676
16677 if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
16678 {
16679 prefix_required = false;
16680 oa->write_character(to_char_type('$'));
16681 oa->write_character(first_prefix);
16682 }
16683 }
16684
16685 if (use_count)
16686 {
16687 oa->write_character(to_char_type('#'));
16688 write_number_with_ubjson_prefix(j.m_data.m_value.object->size(), true, use_bjdata);
16689 }
16690
16691 for (const auto& el : *j.m_data.m_value.object)
16692 {
16693 write_number_with_ubjson_prefix(el.first.size(), true, use_bjdata);
16694 oa->write_characters(
16695 reinterpret_cast<const CharType*>(el.first.c_str()),
16696 el.first.size());
16697 write_ubjson(el.second, use_count, use_type, prefix_required, use_bjdata, bjdata_version);
16698 }
16699
16700 if (!use_count)
16701 {
16702 oa->write_character(to_char_type('}'));
16703 }
16704
16705 break;
16706 }
16707
16708 case value_t::discarded:
16709 default:
16710 break;
16711 }
16712 }
16713
16714 private:
16715 //////////
16716 // BSON //
16717 //////////
16718
16719 /*!
16720 @return The size of a BSON document entry header, including the id marker
16721 and the entry name size (and its null-terminator).
16722 */
16723 static std::size_t calc_bson_entry_header_size(const string_t& name, const BasicJsonType& j)
16724 {
16725 const auto it = name.find(static_cast<typename string_t::value_type>(0));
16726 if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos))
16727 {
16728 JSON_THROW(out_of_range::create(409, concat("BSON key cannot contain code point U+0000 (at byte ", std::to_string(it), ")"), &j));
16729 static_cast<void>(j);
16730 }
16731
16732 return /*id*/ 1ul + name.size() + /*zero-terminator*/1u;
16733 }
16734
16735 /*!
16736 @brief Writes the given @a element_type and @a name to the output adapter
16737 */
16738 void write_bson_entry_header(const string_t& name,
16739 const std::uint8_t element_type)
16740 {
16741 oa->write_character(to_char_type(element_type)); // boolean
16742 oa->write_characters(
16743 reinterpret_cast<const CharType*>(name.c_str()),
16744 name.size() + 1u);
16745 }
16746
16747 /*!
16748 @brief Writes a BSON element with key @a name and boolean value @a value
16749 */
16750 void write_bson_boolean(const string_t& name,
16751 const bool value)
16752 {
16753 write_bson_entry_header(name, 0x08);
16754 oa->write_character(value ? to_char_type(0x01) : to_char_type(0x00));
16755 }
16756
16757 /*!
16758 @brief Writes a BSON element with key @a name and double value @a value
16759 */
16760 void write_bson_double(const string_t& name,
16761 const double value)
16762 {
16763 write_bson_entry_header(name, 0x01);
16764 write_number<double>(value, true);
16765 }
16766
16767 /*!
16768 @return The size of the BSON-encoded string in @a value
16769 */
16770 static std::size_t calc_bson_string_size(const string_t& value)
16771 {
16772 return sizeof(std::int32_t) + value.size() + 1ul;
16773 }
16774
16775 /*!
16776 @brief Writes a BSON element with key @a name and string value @a value
16777 */
16778 void write_bson_string(const string_t& name,
16779 const string_t& value)
16780 {
16781 write_bson_entry_header(name, 0x02);
16782
16783 write_number<std::int32_t>(static_cast<std::int32_t>(value.size() + 1ul), true);
16784 oa->write_characters(
16785 reinterpret_cast<const CharType*>(value.c_str()),
16786 value.size() + 1);
16787 }
16788
16789 /*!
16790 @brief Writes a BSON element with key @a name and null value
16791 */
16792 void write_bson_null(const string_t& name)
16793 {
16794 write_bson_entry_header(name, 0x0A);
16795 }
16796
16797 /*!
16798 @return The size of the BSON-encoded integer @a value
16799 */
16800 static std::size_t calc_bson_integer_size(const std::int64_t value)
16801 {
16802 return (std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)()
16803 ? sizeof(std::int32_t)
16804 : sizeof(std::int64_t);
16805 }
16806
16807 /*!
16808 @brief Writes a BSON element with key @a name and integer @a value
16809 */
16810 void write_bson_integer(const string_t& name,
16811 const std::int64_t value)
16812 {
16813 if ((std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)())
16814 {
16815 write_bson_entry_header(name, 0x10); // int32
16816 write_number<std::int32_t>(static_cast<std::int32_t>(value), true);
16817 }
16818 else
16819 {
16820 write_bson_entry_header(name, 0x12); // int64
16821 write_number<std::int64_t>(static_cast<std::int64_t>(value), true);
16822 }
16823 }
16824
16825 /*!
16826 @return The size of the BSON-encoded unsigned integer in @a j
16827 */
16828 static constexpr std::size_t calc_bson_unsigned_size(const std::uint64_t value) noexcept
16829 {
16830 return (value <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
16831 ? sizeof(std::int32_t)
16832 : sizeof(std::int64_t);
16833 }
16834
16835 /*!
16836 @brief Writes a BSON element with key @a name and unsigned @a value
16837 */
16838 void write_bson_unsigned(const string_t& name,
16839 const BasicJsonType& j)
16840 {
16841 if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
16842 {
16843 write_bson_entry_header(name, 0x10 /* int32 */);
16844 write_number<std::int32_t>(static_cast<std::int32_t>(j.m_data.m_value.number_unsigned), true);
16845 }
16846 else if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
16847 {
16848 write_bson_entry_header(name, 0x12 /* int64 */);
16849 write_number<std::int64_t>(static_cast<std::int64_t>(j.m_data.m_value.number_unsigned), true);
16850 }
16851 else
16852 {
16853 write_bson_entry_header(name, 0x11 /* uint64 */);
16854 write_number<std::uint64_t>(static_cast<std::uint64_t>(j.m_data.m_value.number_unsigned), true);
16855 }
16856 }
16857
16858 /*!
16859 @brief Writes a BSON element with key @a name and object @a value
16860 */
16861 void write_bson_object_entry(const string_t& name,
16862 const typename BasicJsonType::object_t& value)
16863 {
16864 write_bson_entry_header(name, 0x03); // object
16865 write_bson_object(value);
16866 }
16867
16868 /*!
16869 @return The size of the BSON-encoded array @a value
16870 */
16871 static std::size_t calc_bson_array_size(const typename BasicJsonType::array_t& value)
16872 {
16873 std::size_t array_index = 0ul;
16874
16875 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)
16876 {
16877 return result + calc_bson_element_size(std::to_string(array_index++), el);
16878 });
16879
16880 return sizeof(std::int32_t) + embedded_document_size + 1ul;
16881 }
16882
16883 /*!
16884 @return The size of the BSON-encoded binary array @a value
16885 */
16886 static std::size_t calc_bson_binary_size(const typename BasicJsonType::binary_t& value)
16887 {
16888 return sizeof(std::int32_t) + value.size() + 1ul;
16889 }
16890
16891 /*!
16892 @brief Writes a BSON element with key @a name and array @a value
16893 */
16894 void write_bson_array(const string_t& name,
16895 const typename BasicJsonType::array_t& value)
16896 {
16897 write_bson_entry_header(name, 0x04); // array
16898 write_number<std::int32_t>(static_cast<std::int32_t>(calc_bson_array_size(value)), true);
16899
16900 std::size_t array_index = 0ul;
16901
16902 for (const auto& el : value)
16903 {
16904 write_bson_element(std::to_string(array_index++), el);
16905 }
16906
16907 oa->write_character(to_char_type(0x00));
16908 }
16909
16910 /*!
16911 @brief Writes a BSON element with key @a name and binary value @a value
16912 */
16913 void write_bson_binary(const string_t& name,
16914 const binary_t& value)
16915 {
16916 write_bson_entry_header(name, 0x05);
16917
16918 write_number<std::int32_t>(static_cast<std::int32_t>(value.size()), true);
16919 write_number(value.has_subtype() ? static_cast<std::uint8_t>(value.subtype()) : static_cast<std::uint8_t>(0x00));
16920
16921 oa->write_characters(reinterpret_cast<const CharType*>(value.data()), value.size());
16922 }
16923
16924 /*!
16925 @brief Calculates the size necessary to serialize the JSON value @a j with its @a name
16926 @return The calculated size for the BSON document entry for @a j with the given @a name.
16927 */
16928 static std::size_t calc_bson_element_size(const string_t& name,
16929 const BasicJsonType& j)
16930 {
16931 const auto header_size = calc_bson_entry_header_size(name, j);
16932 switch (j.type())
16933 {
16934 case value_t::object:
16935 return header_size + calc_bson_object_size(*j.m_data.m_value.object);
16936
16937 case value_t::array:
16938 return header_size + calc_bson_array_size(*j.m_data.m_value.array);
16939
16940 case value_t::binary:
16941 return header_size + calc_bson_binary_size(*j.m_data.m_value.binary);
16942
16943 case value_t::boolean:
16944 return header_size + 1ul;
16945
16946 case value_t::number_float:
16947 return header_size + 8ul;
16948
16949 case value_t::number_integer:
16950 return header_size + calc_bson_integer_size(j.m_data.m_value.number_integer);
16951
16952 case value_t::number_unsigned:
16953 return header_size + calc_bson_unsigned_size(j.m_data.m_value.number_unsigned);
16954
16955 case value_t::string:
16956 return header_size + calc_bson_string_size(*j.m_data.m_value.string);
16957
16958 case value_t::null:
16959 return header_size + 0ul;
16960
16961 // LCOV_EXCL_START
16962 case value_t::discarded:
16963 default:
16964 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert)
16965 return 0ul;
16966 // LCOV_EXCL_STOP
16967 }
16968 }
16969
16970 /*!
16971 @brief Serializes the JSON value @a j to BSON and associates it with the
16972 key @a name.
16973 @param name The name to associate with the JSON entity @a j within the
16974 current BSON document
16975 */
16976 void write_bson_element(const string_t& name,
16977 const BasicJsonType& j)
16978 {
16979 switch (j.type())
16980 {
16981 case value_t::object:
16982 return write_bson_object_entry(name, *j.m_data.m_value.object);
16983
16984 case value_t::array:
16985 return write_bson_array(name, *j.m_data.m_value.array);
16986
16987 case value_t::binary:
16988 return write_bson_binary(name, *j.m_data.m_value.binary);
16989
16990 case value_t::boolean:
16991 return write_bson_boolean(name, j.m_data.m_value.boolean);
16992
16993 case value_t::number_float:
16994 return write_bson_double(name, j.m_data.m_value.number_float);
16995
16996 case value_t::number_integer:
16997 return write_bson_integer(name, j.m_data.m_value.number_integer);
16998
16999 case value_t::number_unsigned:
17000 return write_bson_unsigned(name, j);
17001
17002 case value_t::string:
17003 return write_bson_string(name, *j.m_data.m_value.string);
17004
17005 case value_t::null:
17006 return write_bson_null(name);
17007
17008 // LCOV_EXCL_START
17009 case value_t::discarded:
17010 default:
17011 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert)
17012 return;
17013 // LCOV_EXCL_STOP
17014 }
17015 }
17016
17017 /*!
17018 @brief Calculates the size of the BSON serialization of the given
17019 JSON-object @a j.
17020 @param[in] value JSON value to serialize
17021 @pre value.type() == value_t::object
17022 */
17023 static std::size_t calc_bson_object_size(const typename BasicJsonType::object_t& value)
17024 {
17025 const std::size_t document_size = std::accumulate(value.begin(), value.end(), static_cast<std::size_t>(0),
17026 [](size_t result, const typename BasicJsonType::object_t::value_type & el)
17027 {
17028 return result += calc_bson_element_size(el.first, el.second);
17029 });
17030
17031 return sizeof(std::int32_t) + document_size + 1ul;
17032 }
17033
17034 /*!
17035 @param[in] value JSON value to serialize
17036 @pre value.type() == value_t::object
17037 */
17038 void write_bson_object(const typename BasicJsonType::object_t& value)
17039 {
17040 write_number<std::int32_t>(static_cast<std::int32_t>(calc_bson_object_size(value)), true);
17041
17042 for (const auto& el : value)
17043 {
17044 write_bson_element(el.first, el.second);
17045 }
17046
17047 oa->write_character(to_char_type(0x00));
17048 }
17049
17050 //////////
17051 // CBOR //
17052 //////////
17053
17054 static constexpr CharType get_cbor_float_prefix(float /*unused*/)
17055 {
17056 return to_char_type(0xFA); // Single-Precision Float
17057 }
17058
17059 static constexpr CharType get_cbor_float_prefix(double /*unused*/)
17060 {
17061 return to_char_type(0xFB); // Double-Precision Float
17062 }
17063
17064 /////////////
17065 // MsgPack //
17066 /////////////
17067
17068 static constexpr CharType get_msgpack_float_prefix(float /*unused*/)
17069 {
17070 return to_char_type(0xCA); // float 32
17071 }
17072
17073 static constexpr CharType get_msgpack_float_prefix(double /*unused*/)
17074 {
17075 return to_char_type(0xCB); // float 64
17076 }
17077
17078 ////////////
17079 // UBJSON //
17080 ////////////
17081
17082 // UBJSON: write number (floating point)
17083 template<typename NumberType, typename std::enable_if<
17084 std::is_floating_point<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 (add_prefix)
17090 {
17091 oa->write_character(get_ubjson_float_prefix(n));
17092 }
17093 write_number(n, use_bjdata);
17094 }
17095
17096 // UBJSON: write number (unsigned integer)
17097 template<typename NumberType, typename std::enable_if<
17098 std::is_unsigned<NumberType>::value, int>::type = 0>
17099 void write_number_with_ubjson_prefix(const NumberType n,
17100 const bool add_prefix,
17101 const bool use_bjdata)
17102 {
17103 if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
17104 {
17105 if (add_prefix)
17106 {
17107 oa->write_character(to_char_type('i')); // int8
17108 }
17109 write_number(static_cast<std::uint8_t>(n), use_bjdata);
17110 }
17111 else if (n <= (std::numeric_limits<std::uint8_t>::max)())
17112 {
17113 if (add_prefix)
17114 {
17115 oa->write_character(to_char_type('U')); // uint8
17116 }
17117 write_number(static_cast<std::uint8_t>(n), use_bjdata);
17118 }
17119 else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
17120 {
17121 if (add_prefix)
17122 {
17123 oa->write_character(to_char_type('I')); // int16
17124 }
17125 write_number(static_cast<std::int16_t>(n), use_bjdata);
17126 }
17127 else if (use_bjdata && n <= static_cast<uint64_t>((std::numeric_limits<uint16_t>::max)()))
17128 {
17129 if (add_prefix)
17130 {
17131 oa->write_character(to_char_type('u')); // uint16 - bjdata only
17132 }
17133 write_number(static_cast<std::uint16_t>(n), use_bjdata);
17134 }
17135 else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
17136 {
17137 if (add_prefix)
17138 {
17139 oa->write_character(to_char_type('l')); // int32
17140 }
17141 write_number(static_cast<std::int32_t>(n), use_bjdata);
17142 }
17143 else if (use_bjdata && n <= static_cast<uint64_t>((std::numeric_limits<uint32_t>::max)()))
17144 {
17145 if (add_prefix)
17146 {
17147 oa->write_character(to_char_type('m')); // uint32 - bjdata only
17148 }
17149 write_number(static_cast<std::uint32_t>(n), use_bjdata);
17150 }
17151 else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
17152 {
17153 if (add_prefix)
17154 {
17155 oa->write_character(to_char_type('L')); // int64
17156 }
17157 write_number(static_cast<std::int64_t>(n), use_bjdata);
17158 }
17159 else if (use_bjdata && n <= (std::numeric_limits<uint64_t>::max)())
17160 {
17161 if (add_prefix)
17162 {
17163 oa->write_character(to_char_type('M')); // uint64 - bjdata only
17164 }
17165 write_number(static_cast<std::uint64_t>(n), use_bjdata);
17166 }
17167 else
17168 {
17169 if (add_prefix)
17170 {
17171 oa->write_character(to_char_type('H')); // high-precision number
17172 }
17173
17174 const auto number = BasicJsonType(n).dump();
17175 write_number_with_ubjson_prefix(number.size(), true, use_bjdata);
17176 for (std::size_t i = 0; i < number.size(); ++i)
17177 {
17178 oa->write_character(to_char_type(static_cast<std::uint8_t>(number[i])));
17179 }
17180 }
17181 }
17182
17183 // UBJSON: write number (signed integer)
17184 template < typename NumberType, typename std::enable_if <
17185 std::is_signed<NumberType>::value&&
17186 !std::is_floating_point<NumberType>::value, int >::type = 0 >
17187 void write_number_with_ubjson_prefix(const NumberType n,
17188 const bool add_prefix,
17189 const bool use_bjdata)
17190 {
17191 if ((std::numeric_limits<std::int8_t>::min)() <= n && n <= (std::numeric_limits<std::int8_t>::max)())
17192 {
17193 if (add_prefix)
17194 {
17195 oa->write_character(to_char_type('i')); // int8
17196 }
17197 write_number(static_cast<std::int8_t>(n), use_bjdata);
17198 }
17199 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)()))
17200 {
17201 if (add_prefix)
17202 {
17203 oa->write_character(to_char_type('U')); // uint8
17204 }
17205 write_number(static_cast<std::uint8_t>(n), use_bjdata);
17206 }
17207 else if ((std::numeric_limits<std::int16_t>::min)() <= n && n <= (std::numeric_limits<std::int16_t>::max)())
17208 {
17209 if (add_prefix)
17210 {
17211 oa->write_character(to_char_type('I')); // int16
17212 }
17213 write_number(static_cast<std::int16_t>(n), use_bjdata);
17214 }
17215 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)())))
17216 {
17217 if (add_prefix)
17218 {
17219 oa->write_character(to_char_type('u')); // uint16 - bjdata only
17220 }
17221 write_number(static_cast<uint16_t>(n), use_bjdata);
17222 }
17223 else if ((std::numeric_limits<std::int32_t>::min)() <= n && n <= (std::numeric_limits<std::int32_t>::max)())
17224 {
17225 if (add_prefix)
17226 {
17227 oa->write_character(to_char_type('l')); // int32
17228 }
17229 write_number(static_cast<std::int32_t>(n), use_bjdata);
17230 }
17231 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)())))
17232 {
17233 if (add_prefix)
17234 {
17235 oa->write_character(to_char_type('m')); // uint32 - bjdata only
17236 }
17237 write_number(static_cast<uint32_t>(n), use_bjdata);
17238 }
17239 else if ((std::numeric_limits<std::int64_t>::min)() <= n && n <= (std::numeric_limits<std::int64_t>::max)())
17240 {
17241 if (add_prefix)
17242 {
17243 oa->write_character(to_char_type('L')); // int64
17244 }
17245 write_number(static_cast<std::int64_t>(n), use_bjdata);
17246 }
17247 // LCOV_EXCL_START
17248 else
17249 {
17250 if (add_prefix)
17251 {
17252 oa->write_character(to_char_type('H')); // high-precision number
17253 }
17254
17255 const auto number = BasicJsonType(n).dump();
17256 write_number_with_ubjson_prefix(number.size(), true, use_bjdata);
17257 for (std::size_t i = 0; i < number.size(); ++i)
17258 {
17259 oa->write_character(to_char_type(static_cast<std::uint8_t>(number[i])));
17260 }
17261 }
17262 // LCOV_EXCL_STOP
17263 }
17264
17265 /*!
17266 @brief determine the type prefix of container values
17267 */
17268 CharType ubjson_prefix(const BasicJsonType& j, const bool use_bjdata) const noexcept
17269 {
17270 switch (j.type())
17271 {
17272 case value_t::null:
17273 return 'Z';
17274
17275 case value_t::boolean:
17276 return j.m_data.m_value.boolean ? 'T' : 'F';
17277
17278 case value_t::number_integer:
17279 {
17280 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)())
17281 {
17282 return 'i';
17283 }
17284 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)())
17285 {
17286 return 'U';
17287 }
17288 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)())
17289 {
17290 return 'I';
17291 }
17292 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)()))
17293 {
17294 return 'u';
17295 }
17296 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)())
17297 {
17298 return 'l';
17299 }
17300 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)()))
17301 {
17302 return 'm';
17303 }
17304 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)())
17305 {
17306 return 'L';
17307 }
17308 // anything else is treated as high-precision number
17309 return 'H'; // LCOV_EXCL_LINE
17310 }
17311
17312 case value_t::number_unsigned:
17313 {
17314 if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
17315 {
17316 return 'i';
17317 }
17318 if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint8_t>::max)()))
17319 {
17320 return 'U';
17321 }
17322 if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
17323 {
17324 return 'I';
17325 }
17326 if (use_bjdata && j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint16_t>::max)()))
17327 {
17328 return 'u';
17329 }
17330 if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
17331 {
17332 return 'l';
17333 }
17334 if (use_bjdata && j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint32_t>::max)()))
17335 {
17336 return 'm';
17337 }
17338 if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
17339 {
17340 return 'L';
17341 }
17342 if (use_bjdata && j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
17343 {
17344 return 'M';
17345 }
17346 // anything else is treated as high-precision number
17347 return 'H'; // LCOV_EXCL_LINE
17348 }
17349
17350 case value_t::number_float:
17351 return get_ubjson_float_prefix(j.m_data.m_value.number_float);
17352
17353 case value_t::string:
17354 return 'S';
17355
17356 case value_t::array: // fallthrough
17357 case value_t::binary:
17358 return '[';
17359
17360 case value_t::object:
17361 return '{';
17362
17363 case value_t::discarded:
17364 default: // discarded values
17365 return 'N';
17366 }
17367 }
17368
17369 static constexpr CharType get_ubjson_float_prefix(float /*unused*/)
17370 {
17371 return 'd'; // float 32
17372 }
17373
17374 static constexpr CharType get_ubjson_float_prefix(double /*unused*/)
17375 {
17376 return 'D'; // float 64
17377 }
17378
17379 /*!
17380 @return false if the object is successfully converted to a bjdata ndarray, true if the type or size is invalid
17381 */
17382 bool write_bjdata_ndarray(const typename BasicJsonType::object_t& value, const bool use_count, const bool use_type, const bjdata_version_t bjdata_version)
17383 {
17384 std::map<string_t, CharType> bjdtype = {{"uint8", 'U'}, {"int8", 'i'}, {"uint16", 'u'}, {"int16", 'I'},
17385 {"uint32", 'm'}, {"int32", 'l'}, {"uint64", 'M'}, {"int64", 'L'}, {"single", 'd'}, {"double", 'D'},
17386 {"char", 'C'}, {"byte", 'B'}
17387 };
17388
17389 string_t key = "_ArrayType_";
17390 auto it = bjdtype.find(static_cast<string_t>(value.at(key)));
17391 if (it == bjdtype.end())
17392 {
17393 return true;
17394 }
17395 CharType dtype = it->second;
17396
17397 key = "_ArraySize_";
17398 std::size_t len = (value.at(key).empty() ? 0 : 1);
17399 for (const auto& el : value.at(key))
17400 {
17401 len *= static_cast<std::size_t>(el.m_data.m_value.number_unsigned);
17402 }
17403
17404 key = "_ArrayData_";
17405 if (value.at(key).size() != len)
17406 {
17407 return true;
17408 }
17409
17410 oa->write_character('[');
17411 oa->write_character('$');
17412 oa->write_character(dtype);
17413 oa->write_character('#');
17414
17415 key = "_ArraySize_";
17416 write_ubjson(value.at(key), use_count, use_type, true, true, bjdata_version);
17417
17418 key = "_ArrayData_";
17419 if (dtype == 'U' || dtype == 'C' || dtype == 'B')
17420 {
17421 for (const auto& el : value.at(key))
17422 {
17423 write_number(static_cast<std::uint8_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::int8_t>(el.m_data.m_value.number_integer), true);
17431 }
17432 }
17433 else if (dtype == 'u')
17434 {
17435 for (const auto& el : value.at(key))
17436 {
17437 write_number(static_cast<std::uint16_t>(el.m_data.m_value.number_unsigned), true);
17438 }
17439 }
17440 else if (dtype == 'I')
17441 {
17442 for (const auto& el : value.at(key))
17443 {
17444 write_number(static_cast<std::int16_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::uint32_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::int32_t>(el.m_data.m_value.number_integer), true);
17459 }
17460 }
17461 else if (dtype == 'M')
17462 {
17463 for (const auto& el : value.at(key))
17464 {
17465 write_number(static_cast<std::uint64_t>(el.m_data.m_value.number_unsigned), true);
17466 }
17467 }
17468 else if (dtype == 'L')
17469 {
17470 for (const auto& el : value.at(key))
17471 {
17472 write_number(static_cast<std::int64_t>(el.m_data.m_value.number_integer), true);
17473 }
17474 }
17475 else if (dtype == 'd')
17476 {
17477 for (const auto& el : value.at(key))
17478 {
17479 write_number(static_cast<float>(el.m_data.m_value.number_float), true);
17480 }
17481 }
17482 else if (dtype == 'D')
17483 {
17484 for (const auto& el : value.at(key))
17485 {
17486 write_number(static_cast<double>(el.m_data.m_value.number_float), true);
17487 }
17488 }
17489 return false;
17490 }
17491
17492 ///////////////////////
17493 // Utility functions //
17494 ///////////////////////
17495
17496 /*
17497 @brief write a number to output input
17498 @param[in] n number of type @a NumberType
17499 @param[in] OutputIsLittleEndian Set to true if output data is
17500 required to be little endian
17501 @tparam NumberType the type of the number
17502
17503 @note This function needs to respect the system's endianness, because bytes
17504 in CBOR, MessagePack, and UBJSON are stored in network order (big
17505 endian) and therefore need reordering on little endian systems.
17506 On the other hand, BSON and BJData use little endian and should reorder
17507 on big endian systems.
17508 */
17509 template<typename NumberType>
17510 void write_number(const NumberType n, const bool OutputIsLittleEndian = false)
17511 {
17512 // step 1: write number to array of length NumberType
17513 std::array<CharType, sizeof(NumberType)> vec{};
17514 std::memcpy(vec.data(), &n, sizeof(NumberType));
17515
17516 // step 2: write array to output (with possible reordering)
17517 if (is_little_endian != OutputIsLittleEndian)
17518 {
17519 // reverse byte order prior to conversion if necessary
17520 std::reverse(vec.begin(), vec.end());
17521 }
17522
17523 oa->write_characters(vec.data(), sizeof(NumberType));
17524 }
17525
17526 void write_compact_float(const number_float_t n, detail::input_format_t format)
17527 {
17528 #ifdef __GNUC__
17529 #pragma GCC diagnostic push
17530 #pragma GCC diagnostic ignored "-Wfloat-equal"
17531 #endif
17532 if (static_cast<double>(n) >= static_cast<double>(std::numeric_limits<float>::lowest()) &&
17533 static_cast<double>(n) <= static_cast<double>((std::numeric_limits<float>::max)()) &&
17534 static_cast<double>(static_cast<float>(n)) == static_cast<double>(n))
17535 {
17536 oa->write_character(format == detail::input_format_t::cbor
17537 ? get_cbor_float_prefix(static_cast<float>(n))
17538 : get_msgpack_float_prefix(static_cast<float>(n)));
17539 write_number(static_cast<float>(n));
17540 }
17541 else
17542 {
17543 oa->write_character(format == detail::input_format_t::cbor
17544 ? get_cbor_float_prefix(n)
17545 : get_msgpack_float_prefix(n));
17546 write_number(n);
17547 }
17548 #ifdef __GNUC__
17549 #pragma GCC diagnostic pop
17550 #endif
17551 }
17552
17553 public:
17554 // The following to_char_type functions are implement the conversion
17555 // between uint8_t and CharType. In case CharType is not unsigned,
17556 // such a conversion is required to allow values greater than 128.
17557 // See <https://github.com/nlohmann/json/issues/1286> for a discussion.
17558 template < typename C = CharType,
17559 enable_if_t < std::is_signed<C>::value && std::is_signed<char>::value > * = nullptr >
17560 static constexpr CharType to_char_type(std::uint8_t x) noexcept
17561 {
17562 return *reinterpret_cast<char*>(&x);
17563 }
17564
17565 template < typename C = CharType,
17566 enable_if_t < std::is_signed<C>::value && std::is_unsigned<char>::value > * = nullptr >
17567 static CharType to_char_type(std::uint8_t x) noexcept
17568 {
17569 static_assert(sizeof(std::uint8_t) == sizeof(CharType), "size of CharType must be equal to std::uint8_t");
17570 static_assert(std::is_trivial<CharType>::value, "CharType must be trivial");
17571 CharType result;
17572 std::memcpy(&result, &x, sizeof(x));
17573 return result;
17574 }
17575
17576 template<typename C = CharType,
17577 enable_if_t<std::is_unsigned<C>::value>* = nullptr>
17578 static constexpr CharType to_char_type(std::uint8_t x) noexcept
17579 {
17580 return x;
17581 }
17582
17583 template < typename InputCharType, typename C = CharType,
17584 enable_if_t <
17585 std::is_signed<C>::value &&
17586 std::is_signed<char>::value &&
17587 std::is_same<char, typename std::remove_cv<InputCharType>::type>::value
17588 > * = nullptr >
17589 static constexpr CharType to_char_type(InputCharType x) noexcept
17590 {
17591 return x;
17592 }
17593
17594 private:
17595 /// whether we can assume little endianness
17596 const bool is_little_endian = little_endianness();
17597
17598 /// the output
17599 output_adapter_t<CharType> oa = nullptr;
17600 };
17601
17602 } // namespace detail
17603 NLOHMANN_JSON_NAMESPACE_END
17604
17605 // #include <nlohmann/detail/output/output_adapters.hpp>
17606
17607 // #include <nlohmann/detail/output/serializer.hpp>
17608 // __ _____ _____ _____
17609 // __| | __| | | | JSON for Modern C++
17610 // | | |__ | | | | | | version 3.12.0
17611 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
17612 //
17613 // SPDX-FileCopyrightText: 2008 - 2009 Björn Hoehrmann <[email protected]>
17614 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
17615 // SPDX-License-Identifier: MIT
17616
17617
17618
17619 #include <algorithm> // reverse, remove, fill, find, none_of
17620 #include <array> // array
17621 #include <clocale> // localeconv, lconv
17622 #include <cmath> // labs, isfinite, isnan, signbit
17623 #include <cstddef> // size_t, ptrdiff_t
17624 #include <cstdint> // uint8_t
17625 #include <cstdio> // snprintf
17626 #include <limits> // numeric_limits
17627 #include <string> // string, char_traits
17628 #include <iomanip> // setfill, setw
17629 #include <type_traits> // is_same
17630 #include <utility> // move
17631
17632 // #include <nlohmann/detail/conversions/to_chars.hpp>
17633 // __ _____ _____ _____
17634 // __| | __| | | | JSON for Modern C++
17635 // | | |__ | | | | | | version 3.12.0
17636 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
17637 //
17638 // SPDX-FileCopyrightText: 2009 Florian Loitsch <https://florian.loitsch.com/>
17639 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
17640 // SPDX-License-Identifier: MIT
17641
17642
17643
17644 #include <array> // array
17645 #include <cmath> // signbit, isfinite
17646 #include <cstdint> // intN_t, uintN_t
17647 #include <cstring> // memcpy, memmove
17648 #include <limits> // numeric_limits
17649 #include <type_traits> // conditional
17650
17651 // #include <nlohmann/detail/macro_scope.hpp>
17652
17653
17654 NLOHMANN_JSON_NAMESPACE_BEGIN
17655 namespace detail
17656 {
17657
17658 /*!
17659 @brief implements the Grisu2 algorithm for binary to decimal floating-point
17660 conversion.
17661
17662 This implementation is a slightly modified version of the reference
17663 implementation which may be obtained from
17664 http://florian.loitsch.com/publications (bench.tar.gz).
17665
17666 The code is distributed under the MIT license, Copyright (c) 2009 Florian Loitsch.
17667
17668 For a detailed description of the algorithm see:
17669
17670 [1] Loitsch, "Printing Floating-Point Numbers Quickly and Accurately with
17671 Integers", Proceedings of the ACM SIGPLAN 2010 Conference on Programming
17672 Language Design and Implementation, PLDI 2010
17673 [2] Burger, Dybvig, "Printing Floating-Point Numbers Quickly and Accurately",
17674 Proceedings of the ACM SIGPLAN 1996 Conference on Programming Language
17675 Design and Implementation, PLDI 1996
17676 */
17677 namespace dtoa_impl
17678 {
17679
17680 template<typename Target, typename Source>
17681 Target reinterpret_bits(const Source source)
17682 {
17683 static_assert(sizeof(Target) == sizeof(Source), "size mismatch");
17684
17685 Target target;
17686 std::memcpy(&target, &source, sizeof(Source));
17687 return target;
17688 }
17689
17690 struct diyfp // f * 2^e
17691 {
17692 static constexpr int kPrecision = 64; // = q
17693
17694 std::uint64_t f = 0;
17695 int e = 0;
17696
17697 constexpr diyfp(std::uint64_t f_, int e_) noexcept : f(f_), e(e_) {}
17698
17699 /*!
17700 @brief returns x - y
17701 @pre x.e == y.e and x.f >= y.f
17702 */
17703 static diyfp sub(const diyfp& x, const diyfp& y) noexcept
17704 {
17705 JSON_ASSERT(x.e == y.e);
17706 JSON_ASSERT(x.f >= y.f);
17707
17708 return {x.f - y.f, x.e};
17709 }
17710
17711 /*!
17712 @brief returns x * y
17713 @note The result is rounded. (Only the upper q bits are returned.)
17714 */
17715 static diyfp mul(const diyfp& x, const diyfp& y) noexcept
17716 {
17717 static_assert(kPrecision == 64, "internal error");
17718
17719 // Computes:
17720 // f = round((x.f * y.f) / 2^q)
17721 // e = x.e + y.e + q
17722
17723 // Emulate the 64-bit * 64-bit multiplication:
17724 //
17725 // p = u * v
17726 // = (u_lo + 2^32 u_hi) (v_lo + 2^32 v_hi)
17727 // = (u_lo v_lo ) + 2^32 ((u_lo v_hi ) + (u_hi v_lo )) + 2^64 (u_hi v_hi )
17728 // = (p0 ) + 2^32 ((p1 ) + (p2 )) + 2^64 (p3 )
17729 // = (p0_lo + 2^32 p0_hi) + 2^32 ((p1_lo + 2^32 p1_hi) + (p2_lo + 2^32 p2_hi)) + 2^64 (p3 )
17730 // = (p0_lo ) + 2^32 (p0_hi + p1_lo + p2_lo ) + 2^64 (p1_hi + p2_hi + p3)
17731 // = (p0_lo ) + 2^32 (Q ) + 2^64 (H )
17732 // = (p0_lo ) + 2^32 (Q_lo + 2^32 Q_hi ) + 2^64 (H )
17733 //
17734 // (Since Q might be larger than 2^32 - 1)
17735 //
17736 // = (p0_lo + 2^32 Q_lo) + 2^64 (Q_hi + H)
17737 //
17738 // (Q_hi + H does not overflow a 64-bit int)
17739 //
17740 // = p_lo + 2^64 p_hi
17741
17742 const std::uint64_t u_lo = x.f & 0xFFFFFFFFu;
17743 const std::uint64_t u_hi = x.f >> 32u;
17744 const std::uint64_t v_lo = y.f & 0xFFFFFFFFu;
17745 const std::uint64_t v_hi = y.f >> 32u;
17746
17747 const std::uint64_t p0 = u_lo * v_lo;
17748 const std::uint64_t p1 = u_lo * v_hi;
17749 const std::uint64_t p2 = u_hi * v_lo;
17750 const std::uint64_t p3 = u_hi * v_hi;
17751
17752 const std::uint64_t p0_hi = p0 >> 32u;
17753 const std::uint64_t p1_lo = p1 & 0xFFFFFFFFu;
17754 const std::uint64_t p1_hi = p1 >> 32u;
17755 const std::uint64_t p2_lo = p2 & 0xFFFFFFFFu;
17756 const std::uint64_t p2_hi = p2 >> 32u;
17757
17758 std::uint64_t Q = p0_hi + p1_lo + p2_lo;
17759
17760 // The full product might now be computed as
17761 //
17762 // p_hi = p3 + p2_hi + p1_hi + (Q >> 32)
17763 // p_lo = p0_lo + (Q << 32)
17764 //
17765 // But in this particular case here, the full p_lo is not required.
17766 // Effectively we only need to add the highest bit in p_lo to p_hi (and
17767 // Q_hi + 1 does not overflow).
17768
17769 Q += std::uint64_t{1} << (64u - 32u - 1u); // round, ties up
17770
17771 const std::uint64_t h = p3 + p2_hi + p1_hi + (Q >> 32u);
17772
17773 return {h, x.e + y.e + 64};
17774 }
17775
17776 /*!
17777 @brief normalize x such that the significand is >= 2^(q-1)
17778 @pre x.f != 0
17779 */
17780 static diyfp normalize(diyfp x) noexcept
17781 {
17782 JSON_ASSERT(x.f != 0);
17783
17784 while ((x.f >> 63u) == 0)
17785 {
17786 x.f <<= 1u;
17787 x.e--;
17788 }
17789
17790 return x;
17791 }
17792
17793 /*!
17794 @brief normalize x such that the result has the exponent E
17795 @pre e >= x.e and the upper e - x.e bits of x.f must be zero.
17796 */
17797 static diyfp normalize_to(const diyfp& x, const int target_exponent) noexcept
17798 {
17799 const int delta = x.e - target_exponent;
17800
17801 JSON_ASSERT(delta >= 0);
17802 JSON_ASSERT(((x.f << delta) >> delta) == x.f);
17803
17804 return {x.f << delta, target_exponent};
17805 }
17806 };
17807
17808 struct boundaries
17809 {
17810 diyfp w;
17811 diyfp minus;
17812 diyfp plus;
17813 };
17814
17815 /*!
17816 Compute the (normalized) diyfp representing the input number 'value' and its
17817 boundaries.
17818
17819 @pre value must be finite and positive
17820 */
17821 template<typename FloatType>
17822 boundaries compute_boundaries(FloatType value)
17823 {
17824 JSON_ASSERT(std::isfinite(value));
17825 JSON_ASSERT(value > 0);
17826
17827 // Convert the IEEE representation into a diyfp.
17828 //
17829 // If v is denormal:
17830 // value = 0.F * 2^(1 - bias) = ( F) * 2^(1 - bias - (p-1))
17831 // If v is normalized:
17832 // value = 1.F * 2^(E - bias) = (2^(p-1) + F) * 2^(E - bias - (p-1))
17833
17834 static_assert(std::numeric_limits<FloatType>::is_iec559,
17835 "internal error: dtoa_short requires an IEEE-754 floating-point implementation");
17836
17837 constexpr int kPrecision = std::numeric_limits<FloatType>::digits; // = p (includes the hidden bit)
17838 constexpr int kBias = std::numeric_limits<FloatType>::max_exponent - 1 + (kPrecision - 1);
17839 constexpr int kMinExp = 1 - kBias;
17840 constexpr std::uint64_t kHiddenBit = std::uint64_t{1} << (kPrecision - 1); // = 2^(p-1)
17841
17842 using bits_type = typename std::conditional<kPrecision == 24, std::uint32_t, std::uint64_t >::type;
17843
17844 const auto bits = static_cast<std::uint64_t>(reinterpret_bits<bits_type>(value));
17845 const std::uint64_t E = bits >> (kPrecision - 1);
17846 const std::uint64_t F = bits & (kHiddenBit - 1);
17847
17848 const bool is_denormal = E == 0;
17849 const diyfp v = is_denormal
17850 ? diyfp(F, kMinExp)
17851 : diyfp(F + kHiddenBit, static_cast<int>(E) - kBias);
17852
17853 // Compute the boundaries m- and m+ of the floating-point value
17854 // v = f * 2^e.
17855 //
17856 // Determine v- and v+, the floating-point predecessor and successor if v,
17857 // respectively.
17858 //
17859 // v- = v - 2^e if f != 2^(p-1) or e == e_min (A)
17860 // = v - 2^(e-1) if f == 2^(p-1) and e > e_min (B)
17861 //
17862 // v+ = v + 2^e
17863 //
17864 // Let m- = (v- + v) / 2 and m+ = (v + v+) / 2. All real numbers _strictly_
17865 // between m- and m+ round to v, regardless of how the input rounding
17866 // algorithm breaks ties.
17867 //
17868 // ---+-------------+-------------+-------------+-------------+--- (A)
17869 // v- m- v m+ v+
17870 //
17871 // -----------------+------+------+-------------+-------------+--- (B)
17872 // v- m- v m+ v+
17873
17874 const bool lower_boundary_is_closer = F == 0 && E > 1;
17875 const diyfp m_plus = diyfp((2 * v.f) + 1, v.e - 1);
17876 const diyfp m_minus = lower_boundary_is_closer
17877 ? diyfp((4 * v.f) - 1, v.e - 2) // (B)
17878 : diyfp((2 * v.f) - 1, v.e - 1); // (A)
17879
17880 // Determine the normalized w+ = m+.
17881 const diyfp w_plus = diyfp::normalize(m_plus);
17882
17883 // Determine w- = m- such that e_(w-) = e_(w+).
17884 const diyfp w_minus = diyfp::normalize_to(m_minus, w_plus.e);
17885
17886 return {diyfp::normalize(v), w_minus, w_plus};
17887 }
17888
17889 // Given normalized diyfp w, Grisu needs to find a (normalized) cached
17890 // power-of-ten c, such that the exponent of the product c * w = f * 2^e lies
17891 // within a certain range [alpha, gamma] (Definition 3.2 from [1])
17892 //
17893 // alpha <= e = e_c + e_w + q <= gamma
17894 //
17895 // or
17896 //
17897 // f_c * f_w * 2^alpha <= f_c 2^(e_c) * f_w 2^(e_w) * 2^q
17898 // <= f_c * f_w * 2^gamma
17899 //
17900 // Since c and w are normalized, i.e. 2^(q-1) <= f < 2^q, this implies
17901 //
17902 // 2^(q-1) * 2^(q-1) * 2^alpha <= c * w * 2^q < 2^q * 2^q * 2^gamma
17903 //
17904 // or
17905 //
17906 // 2^(q - 2 + alpha) <= c * w < 2^(q + gamma)
17907 //
17908 // The choice of (alpha,gamma) determines the size of the table and the form of
17909 // the digit generation procedure. Using (alpha,gamma)=(-60,-32) works out well
17910 // in practice:
17911 //
17912 // The idea is to cut the number c * w = f * 2^e into two parts, which can be
17913 // processed independently: An integral part p1, and a fractional part p2:
17914 //
17915 // f * 2^e = ( (f div 2^-e) * 2^-e + (f mod 2^-e) ) * 2^e
17916 // = (f div 2^-e) + (f mod 2^-e) * 2^e
17917 // = p1 + p2 * 2^e
17918 //
17919 // The conversion of p1 into decimal form requires a series of divisions and
17920 // modulos by (a power of) 10. These operations are faster for 32-bit than for
17921 // 64-bit integers, so p1 should ideally fit into a 32-bit integer. This can be
17922 // achieved by choosing
17923 //
17924 // -e >= 32 or e <= -32 := gamma
17925 //
17926 // In order to convert the fractional part
17927 //
17928 // p2 * 2^e = p2 / 2^-e = d[-1] / 10^1 + d[-2] / 10^2 + ...
17929 //
17930 // into decimal form, the fraction is repeatedly multiplied by 10 and the digits
17931 // d[-i] are extracted in order:
17932 //
17933 // (10 * p2) div 2^-e = d[-1]
17934 // (10 * p2) mod 2^-e = d[-2] / 10^1 + ...
17935 //
17936 // The multiplication by 10 must not overflow. It is sufficient to choose
17937 //
17938 // 10 * p2 < 16 * p2 = 2^4 * p2 <= 2^64.
17939 //
17940 // Since p2 = f mod 2^-e < 2^-e,
17941 //
17942 // -e <= 60 or e >= -60 := alpha
17943
17944 constexpr int kAlpha = -60;
17945 constexpr int kGamma = -32;
17946
17947 struct cached_power // c = f * 2^e ~= 10^k
17948 {
17949 std::uint64_t f;
17950 int e;
17951 int k;
17952 };
17953
17954 /*!
17955 For a normalized diyfp w = f * 2^e, this function returns a (normalized) cached
17956 power-of-ten c = f_c * 2^e_c, such that the exponent of the product w * c
17957 satisfies (Definition 3.2 from [1])
17958
17959 alpha <= e_c + e + q <= gamma.
17960 */
17961 inline cached_power get_cached_power_for_binary_exponent(int e)
17962 {
17963 // Now
17964 //
17965 // alpha <= e_c + e + q <= gamma (1)
17966 // ==> f_c * 2^alpha <= c * 2^e * 2^q
17967 //
17968 // and since the c's are normalized, 2^(q-1) <= f_c,
17969 //
17970 // ==> 2^(q - 1 + alpha) <= c * 2^(e + q)
17971 // ==> 2^(alpha - e - 1) <= c
17972 //
17973 // If c were an exact power of ten, i.e. c = 10^k, one may determine k as
17974 //
17975 // k = ceil( log_10( 2^(alpha - e - 1) ) )
17976 // = ceil( (alpha - e - 1) * log_10(2) )
17977 //
17978 // From the paper:
17979 // "In theory the result of the procedure could be wrong since c is rounded,
17980 // and the computation itself is approximated [...]. In practice, however,
17981 // this simple function is sufficient."
17982 //
17983 // For IEEE double precision floating-point numbers converted into
17984 // normalized diyfp's w = f * 2^e, with q = 64,
17985 //
17986 // e >= -1022 (min IEEE exponent)
17987 // -52 (p - 1)
17988 // -52 (p - 1, possibly normalize denormal IEEE numbers)
17989 // -11 (normalize the diyfp)
17990 // = -1137
17991 //
17992 // and
17993 //
17994 // e <= +1023 (max IEEE exponent)
17995 // -52 (p - 1)
17996 // -11 (normalize the diyfp)
17997 // = 960
17998 //
17999 // This binary exponent range [-1137,960] results in a decimal exponent
18000 // range [-307,324]. One does not need to store a cached power for each
18001 // k in this range. For each such k it suffices to find a cached power
18002 // such that the exponent of the product lies in [alpha,gamma].
18003 // This implies that the difference of the decimal exponents of adjacent
18004 // table entries must be less than or equal to
18005 //
18006 // floor( (gamma - alpha) * log_10(2) ) = 8.
18007 //
18008 // (A smaller distance gamma-alpha would require a larger table.)
18009
18010 // NB:
18011 // Actually this function returns c, such that -60 <= e_c + e + 64 <= -34.
18012
18013 constexpr int kCachedPowersMinDecExp = -300;
18014 constexpr int kCachedPowersDecStep = 8;
18015
18016 static constexpr std::array<cached_power, 79> kCachedPowers =
18017 {
18018 {
18019 { 0xAB70FE17C79AC6CA, -1060, -300 },
18020 { 0xFF77B1FCBEBCDC4F, -1034, -292 },
18021 { 0xBE5691EF416BD60C, -1007, -284 },
18022 { 0x8DD01FAD907FFC3C, -980, -276 },
18023 { 0xD3515C2831559A83, -954, -268 },
18024 { 0x9D71AC8FADA6C9B5, -927, -260 },
18025 { 0xEA9C227723EE8BCB, -901, -252 },
18026 { 0xAECC49914078536D, -874, -244 },
18027 { 0x823C12795DB6CE57, -847, -236 },
18028 { 0xC21094364DFB5637, -821, -228 },
18029 { 0x9096EA6F3848984F, -794, -220 },
18030 { 0xD77485CB25823AC7, -768, -212 },
18031 { 0xA086CFCD97BF97F4, -741, -204 },
18032 { 0xEF340A98172AACE5, -715, -196 },
18033 { 0xB23867FB2A35B28E, -688, -188 },
18034 { 0x84C8D4DFD2C63F3B, -661, -180 },
18035 { 0xC5DD44271AD3CDBA, -635, -172 },
18036 { 0x936B9FCEBB25C996, -608, -164 },
18037 { 0xDBAC6C247D62A584, -582, -156 },
18038 { 0xA3AB66580D5FDAF6, -555, -148 },
18039 { 0xF3E2F893DEC3F126, -529, -140 },
18040 { 0xB5B5ADA8AAFF80B8, -502, -132 },
18041 { 0x87625F056C7C4A8B, -475, -124 },
18042 { 0xC9BCFF6034C13053, -449, -116 },
18043 { 0x964E858C91BA2655, -422, -108 },
18044 { 0xDFF9772470297EBD, -396, -100 },
18045 { 0xA6DFBD9FB8E5B88F, -369, -92 },
18046 { 0xF8A95FCF88747D94, -343, -84 },
18047 { 0xB94470938FA89BCF, -316, -76 },
18048 { 0x8A08F0F8BF0F156B, -289, -68 },
18049 { 0xCDB02555653131B6, -263, -60 },
18050 { 0x993FE2C6D07B7FAC, -236, -52 },
18051 { 0xE45C10C42A2B3B06, -210, -44 },
18052 { 0xAA242499697392D3, -183, -36 },
18053 { 0xFD87B5F28300CA0E, -157, -28 },
18054 { 0xBCE5086492111AEB, -130, -20 },
18055 { 0x8CBCCC096F5088CC, -103, -12 },
18056 { 0xD1B71758E219652C, -77, -4 },
18057 { 0x9C40000000000000, -50, 4 },
18058 { 0xE8D4A51000000000, -24, 12 },
18059 { 0xAD78EBC5AC620000, 3, 20 },
18060 { 0x813F3978F8940984, 30, 28 },
18061 { 0xC097CE7BC90715B3, 56, 36 },
18062 { 0x8F7E32CE7BEA5C70, 83, 44 },
18063 { 0xD5D238A4ABE98068, 109, 52 },
18064 { 0x9F4F2726179A2245, 136, 60 },
18065 { 0xED63A231D4C4FB27, 162, 68 },
18066 { 0xB0DE65388CC8ADA8, 189, 76 },
18067 { 0x83C7088E1AAB65DB, 216, 84 },
18068 { 0xC45D1DF942711D9A, 242, 92 },
18069 { 0x924D692CA61BE758, 269, 100 },
18070 { 0xDA01EE641A708DEA, 295, 108 },
18071 { 0xA26DA3999AEF774A, 322, 116 },
18072 { 0xF209787BB47D6B85, 348, 124 },
18073 { 0xB454E4A179DD1877, 375, 132 },
18074 { 0x865B86925B9BC5C2, 402, 140 },
18075 { 0xC83553C5C8965D3D, 428, 148 },
18076 { 0x952AB45CFA97A0B3, 455, 156 },
18077 { 0xDE469FBD99A05FE3, 481, 164 },
18078 { 0xA59BC234DB398C25, 508, 172 },
18079 { 0xF6C69A72A3989F5C, 534, 180 },
18080 { 0xB7DCBF5354E9BECE, 561, 188 },
18081 { 0x88FCF317F22241E2, 588, 196 },
18082 { 0xCC20CE9BD35C78A5, 614, 204 },
18083 { 0x98165AF37B2153DF, 641, 212 },
18084 { 0xE2A0B5DC971F303A, 667, 220 },
18085 { 0xA8D9D1535CE3B396, 694, 228 },
18086 { 0xFB9B7CD9A4A7443C, 720, 236 },
18087 { 0xBB764C4CA7A44410, 747, 244 },
18088 { 0x8BAB8EEFB6409C1A, 774, 252 },
18089 { 0xD01FEF10A657842C, 800, 260 },
18090 { 0x9B10A4E5E9913129, 827, 268 },
18091 { 0xE7109BFBA19C0C9D, 853, 276 },
18092 { 0xAC2820D9623BF429, 880, 284 },
18093 { 0x80444B5E7AA7CF85, 907, 292 },
18094 { 0xBF21E44003ACDD2D, 933, 300 },
18095 { 0x8E679C2F5E44FF8F, 960, 308 },
18096 { 0xD433179D9C8CB841, 986, 316 },
18097 { 0x9E19DB92B4E31BA9, 1013, 324 },
18098 }
18099 };
18100
18101 // This computation gives exactly the same results for k as
18102 // k = ceil((kAlpha - e - 1) * 0.30102999566398114)
18103 // for |e| <= 1500, but doesn't require floating-point operations.
18104 // NB: log_10(2) ~= 78913 / 2^18
18105 JSON_ASSERT(e >= -1500);
18106 JSON_ASSERT(e <= 1500);
18107 const int f = kAlpha - e - 1;
18108 const int k = ((f * 78913) / (1 << 18)) + static_cast<int>(f > 0);
18109
18110 const int index = (-kCachedPowersMinDecExp + k + (kCachedPowersDecStep - 1)) / kCachedPowersDecStep;
18111 JSON_ASSERT(index >= 0);
18112 JSON_ASSERT(static_cast<std::size_t>(index) < kCachedPowers.size());
18113
18114 const cached_power cached = kCachedPowers[static_cast<std::size_t>(index)];
18115 JSON_ASSERT(kAlpha <= cached.e + e + 64);
18116 JSON_ASSERT(kGamma >= cached.e + e + 64);
18117
18118 return cached;
18119 }
18120
18121 /*!
18122 For n != 0, returns k, such that pow10 := 10^(k-1) <= n < 10^k.
18123 For n == 0, returns 1 and sets pow10 := 1.
18124 */
18125 inline int find_largest_pow10(const std::uint32_t n, std::uint32_t& pow10)
18126 {
18127 // LCOV_EXCL_START
18128 if (n >= 1000000000)
18129 {
18130 pow10 = 1000000000;
18131 return 10;
18132 }
18133 // LCOV_EXCL_STOP
18134 if (n >= 100000000)
18135 {
18136 pow10 = 100000000;
18137 return 9;
18138 }
18139 if (n >= 10000000)
18140 {
18141 pow10 = 10000000;
18142 return 8;
18143 }
18144 if (n >= 1000000)
18145 {
18146 pow10 = 1000000;
18147 return 7;
18148 }
18149 if (n >= 100000)
18150 {
18151 pow10 = 100000;
18152 return 6;
18153 }
18154 if (n >= 10000)
18155 {
18156 pow10 = 10000;
18157 return 5;
18158 }
18159 if (n >= 1000)
18160 {
18161 pow10 = 1000;
18162 return 4;
18163 }
18164 if (n >= 100)
18165 {
18166 pow10 = 100;
18167 return 3;
18168 }
18169 if (n >= 10)
18170 {
18171 pow10 = 10;
18172 return 2;
18173 }
18174
18175 pow10 = 1;
18176 return 1;
18177 }
18178
18179 inline void grisu2_round(char* buf, int len, std::uint64_t dist, std::uint64_t delta,
18180 std::uint64_t rest, std::uint64_t ten_k)
18181 {
18182 JSON_ASSERT(len >= 1);
18183 JSON_ASSERT(dist <= delta);
18184 JSON_ASSERT(rest <= delta);
18185 JSON_ASSERT(ten_k > 0);
18186
18187 // <--------------------------- delta ---->
18188 // <---- dist --------->
18189 // --------------[------------------+-------------------]--------------
18190 // M- w M+
18191 //
18192 // ten_k
18193 // <------>
18194 // <---- rest ---->
18195 // --------------[------------------+----+--------------]--------------
18196 // w V
18197 // = buf * 10^k
18198 //
18199 // ten_k represents a unit-in-the-last-place in the decimal representation
18200 // stored in buf.
18201 // Decrement buf by ten_k while this takes buf closer to w.
18202
18203 // The tests are written in this order to avoid overflow in unsigned
18204 // integer arithmetic.
18205
18206 while (rest < dist
18207 && delta - rest >= ten_k
18208 && (rest + ten_k < dist || dist - rest > rest + ten_k - dist))
18209 {
18210 JSON_ASSERT(buf[len - 1] != '0');
18211 buf[len - 1]--;
18212 rest += ten_k;
18213 }
18214 }
18215
18216 /*!
18217 Generates V = buffer * 10^decimal_exponent, such that M- <= V <= M+.
18218 M- and M+ must be normalized and share the same exponent -60 <= e <= -32.
18219 */
18220 inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent,
18221 diyfp M_minus, diyfp w, diyfp M_plus)
18222 {
18223 static_assert(kAlpha >= -60, "internal error");
18224 static_assert(kGamma <= -32, "internal error");
18225
18226 // Generates the digits (and the exponent) of a decimal floating-point
18227 // number V = buffer * 10^decimal_exponent in the range [M-, M+]. The diyfp's
18228 // w, M- and M+ share the same exponent e, which satisfies alpha <= e <= gamma.
18229 //
18230 // <--------------------------- delta ---->
18231 // <---- dist --------->
18232 // --------------[------------------+-------------------]--------------
18233 // M- w M+
18234 //
18235 // Grisu2 generates the digits of M+ from left to right and stops as soon as
18236 // V is in [M-,M+].
18237
18238 JSON_ASSERT(M_plus.e >= kAlpha);
18239 JSON_ASSERT(M_plus.e <= kGamma);
18240
18241 std::uint64_t delta = diyfp::sub(M_plus, M_minus).f; // (significand of (M+ - M-), implicit exponent is e)
18242 std::uint64_t dist = diyfp::sub(M_plus, w ).f; // (significand of (M+ - w ), implicit exponent is e)
18243
18244 // Split M+ = f * 2^e into two parts p1 and p2 (note: e < 0):
18245 //
18246 // M+ = f * 2^e
18247 // = ((f div 2^-e) * 2^-e + (f mod 2^-e)) * 2^e
18248 // = ((p1 ) * 2^-e + (p2 )) * 2^e
18249 // = p1 + p2 * 2^e
18250
18251 const diyfp one(std::uint64_t{1} << -M_plus.e, M_plus.e);
18252
18253 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.)
18254 std::uint64_t p2 = M_plus.f & (one.f - 1); // p2 = f mod 2^-e
18255
18256 // 1)
18257 //
18258 // Generate the digits of the integral part p1 = d[n-1]...d[1]d[0]
18259
18260 JSON_ASSERT(p1 > 0);
18261
18262 std::uint32_t pow10{};
18263 const int k = find_largest_pow10(p1, pow10);
18264
18265 // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1)
18266 //
18267 // p1 = (p1 div 10^(k-1)) * 10^(k-1) + (p1 mod 10^(k-1))
18268 // = (d[k-1] ) * 10^(k-1) + (p1 mod 10^(k-1))
18269 //
18270 // M+ = p1 + p2 * 2^e
18271 // = d[k-1] * 10^(k-1) + (p1 mod 10^(k-1)) + p2 * 2^e
18272 // = d[k-1] * 10^(k-1) + ((p1 mod 10^(k-1)) * 2^-e + p2) * 2^e
18273 // = d[k-1] * 10^(k-1) + ( rest) * 2^e
18274 //
18275 // Now generate the digits d[n] of p1 from left to right (n = k-1,...,0)
18276 //
18277 // p1 = d[k-1]...d[n] * 10^n + d[n-1]...d[0]
18278 //
18279 // but stop as soon as
18280 //
18281 // rest * 2^e = (d[n-1]...d[0] * 2^-e + p2) * 2^e <= delta * 2^e
18282
18283 int n = k;
18284 while (n > 0)
18285 {
18286 // Invariants:
18287 // M+ = buffer * 10^n + (p1 + p2 * 2^e) (buffer = 0 for n = k)
18288 // pow10 = 10^(n-1) <= p1 < 10^n
18289 //
18290 const std::uint32_t d = p1 / pow10; // d = p1 div 10^(n-1)
18291 const std::uint32_t r = p1 % pow10; // r = p1 mod 10^(n-1)
18292 //
18293 // M+ = buffer * 10^n + (d * 10^(n-1) + r) + p2 * 2^e
18294 // = (buffer * 10 + d) * 10^(n-1) + (r + p2 * 2^e)
18295 //
18296 JSON_ASSERT(d <= 9);
18297 buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
18298 //
18299 // M+ = buffer * 10^(n-1) + (r + p2 * 2^e)
18300 //
18301 p1 = r;
18302 n--;
18303 //
18304 // M+ = buffer * 10^n + (p1 + p2 * 2^e)
18305 // pow10 = 10^n
18306 //
18307
18308 // Now check if enough digits have been generated.
18309 // Compute
18310 //
18311 // p1 + p2 * 2^e = (p1 * 2^-e + p2) * 2^e = rest * 2^e
18312 //
18313 // Note:
18314 // Since rest and delta share the same exponent e, it suffices to
18315 // compare the significands.
18316 const std::uint64_t rest = (std::uint64_t{p1} << -one.e) + p2;
18317 if (rest <= delta)
18318 {
18319 // V = buffer * 10^n, with M- <= V <= M+.
18320
18321 decimal_exponent += n;
18322
18323 // We may now just stop. But instead look if the buffer could be
18324 // decremented to bring V closer to w.
18325 //
18326 // pow10 = 10^n is now 1 ulp in the decimal representation V.
18327 // The rounding procedure works with diyfp's with an implicit
18328 // exponent of e.
18329 //
18330 // 10^n = (10^n * 2^-e) * 2^e = ulp * 2^e
18331 //
18332 const std::uint64_t ten_n = std::uint64_t{pow10} << -one.e;
18333 grisu2_round(buffer, length, dist, delta, rest, ten_n);
18334
18335 return;
18336 }
18337
18338 pow10 /= 10;
18339 //
18340 // pow10 = 10^(n-1) <= p1 < 10^n
18341 // Invariants restored.
18342 }
18343
18344 // 2)
18345 //
18346 // The digits of the integral part have been generated:
18347 //
18348 // M+ = d[k-1]...d[1]d[0] + p2 * 2^e
18349 // = buffer + p2 * 2^e
18350 //
18351 // Now generate the digits of the fractional part p2 * 2^e.
18352 //
18353 // Note:
18354 // No decimal point is generated: the exponent is adjusted instead.
18355 //
18356 // p2 actually represents the fraction
18357 //
18358 // p2 * 2^e
18359 // = p2 / 2^-e
18360 // = d[-1] / 10^1 + d[-2] / 10^2 + ...
18361 //
18362 // Now generate the digits d[-m] of p1 from left to right (m = 1,2,...)
18363 //
18364 // p2 * 2^e = d[-1]d[-2]...d[-m] * 10^-m
18365 // + 10^-m * (d[-m-1] / 10^1 + d[-m-2] / 10^2 + ...)
18366 //
18367 // using
18368 //
18369 // 10^m * p2 = ((10^m * p2) div 2^-e) * 2^-e + ((10^m * p2) mod 2^-e)
18370 // = ( d) * 2^-e + ( r)
18371 //
18372 // or
18373 // 10^m * p2 * 2^e = d + r * 2^e
18374 //
18375 // i.e.
18376 //
18377 // M+ = buffer + p2 * 2^e
18378 // = buffer + 10^-m * (d + r * 2^e)
18379 // = (buffer * 10^m + d) * 10^-m + 10^-m * r * 2^e
18380 //
18381 // and stop as soon as 10^-m * r * 2^e <= delta * 2^e
18382
18383 JSON_ASSERT(p2 > delta);
18384
18385 int m = 0;
18386 for (;;)
18387 {
18388 // Invariant:
18389 // M+ = buffer * 10^-m + 10^-m * (d[-m-1] / 10 + d[-m-2] / 10^2 + ...) * 2^e
18390 // = buffer * 10^-m + 10^-m * (p2 ) * 2^e
18391 // = buffer * 10^-m + 10^-m * (1/10 * (10 * p2) ) * 2^e
18392 // = buffer * 10^-m + 10^-m * (1/10 * ((10*p2 div 2^-e) * 2^-e + (10*p2 mod 2^-e)) * 2^e
18393 //
18394 JSON_ASSERT(p2 <= (std::numeric_limits<std::uint64_t>::max)() / 10);
18395 p2 *= 10;
18396 const std::uint64_t d = p2 >> -one.e; // d = (10 * p2) div 2^-e
18397 const std::uint64_t r = p2 & (one.f - 1); // r = (10 * p2) mod 2^-e
18398 //
18399 // M+ = buffer * 10^-m + 10^-m * (1/10 * (d * 2^-e + r) * 2^e
18400 // = buffer * 10^-m + 10^-m * (1/10 * (d + r * 2^e))
18401 // = (buffer * 10 + d) * 10^(-m-1) + 10^(-m-1) * r * 2^e
18402 //
18403 JSON_ASSERT(d <= 9);
18404 buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
18405 //
18406 // M+ = buffer * 10^(-m-1) + 10^(-m-1) * r * 2^e
18407 //
18408 p2 = r;
18409 m++;
18410 //
18411 // M+ = buffer * 10^-m + 10^-m * p2 * 2^e
18412 // Invariant restored.
18413
18414 // Check if enough digits have been generated.
18415 //
18416 // 10^-m * p2 * 2^e <= delta * 2^e
18417 // p2 * 2^e <= 10^m * delta * 2^e
18418 // p2 <= 10^m * delta
18419 delta *= 10;
18420 dist *= 10;
18421 if (p2 <= delta)
18422 {
18423 break;
18424 }
18425 }
18426
18427 // V = buffer * 10^-m, with M- <= V <= M+.
18428
18429 decimal_exponent -= m;
18430
18431 // 1 ulp in the decimal representation is now 10^-m.
18432 // Since delta and dist are now scaled by 10^m, we need to do the
18433 // same with ulp in order to keep the units in sync.
18434 //
18435 // 10^m * 10^-m = 1 = 2^-e * 2^e = ten_m * 2^e
18436 //
18437 const std::uint64_t ten_m = one.f;
18438 grisu2_round(buffer, length, dist, delta, p2, ten_m);
18439
18440 // By construction this algorithm generates the shortest possible decimal
18441 // number (Loitsch, Theorem 6.2) which rounds back to w.
18442 // For an input number of precision p, at least
18443 //
18444 // N = 1 + ceil(p * log_10(2))
18445 //
18446 // decimal digits are sufficient to identify all binary floating-point
18447 // numbers (Matula, "In-and-Out conversions").
18448 // This implies that the algorithm does not produce more than N decimal
18449 // digits.
18450 //
18451 // N = 17 for p = 53 (IEEE double precision)
18452 // N = 9 for p = 24 (IEEE single precision)
18453 }
18454
18455 /*!
18456 v = buf * 10^decimal_exponent
18457 len is the length of the buffer (number of decimal digits)
18458 The buffer must be large enough, i.e. >= max_digits10.
18459 */
18460 JSON_HEDLEY_NON_NULL(1)
18461 inline void grisu2(char* buf, int& len, int& decimal_exponent,
18462 diyfp m_minus, diyfp v, diyfp m_plus)
18463 {
18464 JSON_ASSERT(m_plus.e == m_minus.e);
18465 JSON_ASSERT(m_plus.e == v.e);
18466
18467 // --------(-----------------------+-----------------------)-------- (A)
18468 // m- v m+
18469 //
18470 // --------------------(-----------+-----------------------)-------- (B)
18471 // m- v m+
18472 //
18473 // First scale v (and m- and m+) such that the exponent is in the range
18474 // [alpha, gamma].
18475
18476 const cached_power cached = get_cached_power_for_binary_exponent(m_plus.e);
18477
18478 const diyfp c_minus_k(cached.f, cached.e); // = c ~= 10^-k
18479
18480 // The exponent of the products is = v.e + c_minus_k.e + q and is in the range [alpha,gamma]
18481 const diyfp w = diyfp::mul(v, c_minus_k);
18482 const diyfp w_minus = diyfp::mul(m_minus, c_minus_k);
18483 const diyfp w_plus = diyfp::mul(m_plus, c_minus_k);
18484
18485 // ----(---+---)---------------(---+---)---------------(---+---)----
18486 // w- w w+
18487 // = c*m- = c*v = c*m+
18488 //
18489 // diyfp::mul rounds its result and c_minus_k is approximated too. w, w- and
18490 // w+ are now off by a small amount.
18491 // In fact:
18492 //
18493 // w - v * 10^k < 1 ulp
18494 //
18495 // To account for this inaccuracy, add resp. subtract 1 ulp.
18496 //
18497 // --------+---[---------------(---+---)---------------]---+--------
18498 // w- M- w M+ w+
18499 //
18500 // Now any number in [M-, M+] (bounds included) will round to w when input,
18501 // regardless of how the input rounding algorithm breaks ties.
18502 //
18503 // And digit_gen generates the shortest possible such number in [M-, M+].
18504 // Note that this does not mean that Grisu2 always generates the shortest
18505 // possible number in the interval (m-, m+).
18506 const diyfp M_minus(w_minus.f + 1, w_minus.e);
18507 const diyfp M_plus (w_plus.f - 1, w_plus.e );
18508
18509 decimal_exponent = -cached.k; // = -(-k) = k
18510
18511 grisu2_digit_gen(buf, len, decimal_exponent, M_minus, w, M_plus);
18512 }
18513
18514 /*!
18515 v = buf * 10^decimal_exponent
18516 len is the length of the buffer (number of decimal digits)
18517 The buffer must be large enough, i.e. >= max_digits10.
18518 */
18519 template<typename FloatType>
18520 JSON_HEDLEY_NON_NULL(1)
18521 void grisu2(char* buf, int& len, int& decimal_exponent, FloatType value)
18522 {
18523 static_assert(diyfp::kPrecision >= std::numeric_limits<FloatType>::digits + 3,
18524 "internal error: not enough precision");
18525
18526 JSON_ASSERT(std::isfinite(value));
18527 JSON_ASSERT(value > 0);
18528
18529 // If the neighbors (and boundaries) of 'value' are always computed for double-precision
18530 // numbers, all float's can be recovered using strtod (and strtof). However, the resulting
18531 // decimal representations are not exactly "short".
18532 //
18533 // The documentation for 'std::to_chars' (https://en.cppreference.com/w/cpp/utility/to_chars)
18534 // says "value is converted to a string as if by std::sprintf in the default ("C") locale"
18535 // and since sprintf promotes floats to doubles, I think this is exactly what 'std::to_chars'
18536 // does.
18537 // On the other hand, the documentation for 'std::to_chars' requires that "parsing the
18538 // representation using the corresponding std::from_chars function recovers value exactly". That
18539 // indicates that single precision floating-point numbers should be recovered using
18540 // 'std::strtof'.
18541 //
18542 // NB: If the neighbors are computed for single-precision numbers, there is a single float
18543 // (7.0385307e-26f) which can't be recovered using strtod. The resulting double precision
18544 // value is off by 1 ulp.
18545 #if 0 // NOLINT(readability-avoid-unconditional-preprocessor-if)
18546 const boundaries w = compute_boundaries(static_cast<double>(value));
18547 #else
18548 const boundaries w = compute_boundaries(value);
18549 #endif
18550
18551 grisu2(buf, len, decimal_exponent, w.minus, w.w, w.plus);
18552 }
18553
18554 /*!
18555 @brief appends a decimal representation of e to buf
18556 @return a pointer to the element following the exponent.
18557 @pre -1000 < e < 1000
18558 */
18559 JSON_HEDLEY_NON_NULL(1)
18560 JSON_HEDLEY_RETURNS_NON_NULL
18561 inline char* append_exponent(char* buf, int e)
18562 {
18563 JSON_ASSERT(e > -1000);
18564 JSON_ASSERT(e < 1000);
18565
18566 if (e < 0)
18567 {
18568 e = -e;
18569 *buf++ = '-';
18570 }
18571 else
18572 {
18573 *buf++ = '+';
18574 }
18575
18576 auto k = static_cast<std::uint32_t>(e);
18577 if (k < 10)
18578 {
18579 // Always print at least two digits in the exponent.
18580 // This is for compatibility with printf("%g").
18581 *buf++ = '0';
18582 *buf++ = static_cast<char>('0' + k);
18583 }
18584 else if (k < 100)
18585 {
18586 *buf++ = static_cast<char>('0' + (k / 10));
18587 k %= 10;
18588 *buf++ = static_cast<char>('0' + k);
18589 }
18590 else
18591 {
18592 *buf++ = static_cast<char>('0' + (k / 100));
18593 k %= 100;
18594 *buf++ = static_cast<char>('0' + (k / 10));
18595 k %= 10;
18596 *buf++ = static_cast<char>('0' + k);
18597 }
18598
18599 return buf;
18600 }
18601
18602 /*!
18603 @brief prettify v = buf * 10^decimal_exponent
18604
18605 If v is in the range [10^min_exp, 10^max_exp) it will be printed in fixed-point
18606 notation. Otherwise it will be printed in exponential notation.
18607
18608 @pre min_exp < 0
18609 @pre max_exp > 0
18610 */
18611 JSON_HEDLEY_NON_NULL(1)
18612 JSON_HEDLEY_RETURNS_NON_NULL
18613 inline char* format_buffer(char* buf, int len, int decimal_exponent,
18614 int min_exp, int max_exp)
18615 {
18616 JSON_ASSERT(min_exp < 0);
18617 JSON_ASSERT(max_exp > 0);
18618
18619 const int k = len;
18620 const int n = len + decimal_exponent;
18621
18622 // v = buf * 10^(n-k)
18623 // k is the length of the buffer (number of decimal digits)
18624 // n is the position of the decimal point relative to the start of the buffer.
18625
18626 if (k <= n && n <= max_exp)
18627 {
18628 // digits[000]
18629 // len <= max_exp + 2
18630
18631 std::memset(buf + k, '0', static_cast<size_t>(n) - static_cast<size_t>(k));
18632 // Make it look like a floating-point number (#362, #378)
18633 buf[n + 0] = '.';
18634 buf[n + 1] = '0';
18635 return buf + (static_cast<size_t>(n) + 2);
18636 }
18637
18638 if (0 < n && n <= max_exp)
18639 {
18640 // dig.its
18641 // len <= max_digits10 + 1
18642
18643 JSON_ASSERT(k > n);
18644
18645 std::memmove(buf + (static_cast<size_t>(n) + 1), buf + n, static_cast<size_t>(k) - static_cast<size_t>(n));
18646 buf[n] = '.';
18647 return buf + (static_cast<size_t>(k) + 1U);
18648 }
18649
18650 if (min_exp < n && n <= 0)
18651 {
18652 // 0.[000]digits
18653 // len <= 2 + (-min_exp - 1) + max_digits10
18654
18655 std::memmove(buf + (2 + static_cast<size_t>(-n)), buf, static_cast<size_t>(k));
18656 buf[0] = '0';
18657 buf[1] = '.';
18658 std::memset(buf + 2, '0', static_cast<size_t>(-n));
18659 return buf + (2U + static_cast<size_t>(-n) + static_cast<size_t>(k));
18660 }
18661
18662 if (k == 1)
18663 {
18664 // dE+123
18665 // len <= 1 + 5
18666
18667 buf += 1;
18668 }
18669 else
18670 {
18671 // d.igitsE+123
18672 // len <= max_digits10 + 1 + 5
18673
18674 std::memmove(buf + 2, buf + 1, static_cast<size_t>(k) - 1);
18675 buf[1] = '.';
18676 buf += 1 + static_cast<size_t>(k);
18677 }
18678
18679 *buf++ = 'e';
18680 return append_exponent(buf, n - 1);
18681 }
18682
18683 } // namespace dtoa_impl
18684
18685 /*!
18686 @brief generates a decimal representation of the floating-point number value in [first, last).
18687
18688 The format of the resulting decimal representation is similar to printf's %g
18689 format. Returns an iterator pointing past-the-end of the decimal representation.
18690
18691 @note The input number must be finite, i.e. NaN's and Inf's are not supported.
18692 @note The buffer must be large enough.
18693 @note The result is NOT null-terminated.
18694 */
18695 template<typename FloatType>
18696 JSON_HEDLEY_NON_NULL(1, 2)
18697 JSON_HEDLEY_RETURNS_NON_NULL
18698 char* to_chars(char* first, const char* last, FloatType value)
18699 {
18700 static_cast<void>(last); // maybe unused - fix warning
18701 JSON_ASSERT(std::isfinite(value));
18702
18703 // Use signbit(value) instead of (value < 0) since signbit works for -0.
18704 if (std::signbit(value))
18705 {
18706 value = -value;
18707 *first++ = '-';
18708 }
18709
18710 #ifdef __GNUC__
18711 #pragma GCC diagnostic push
18712 #pragma GCC diagnostic ignored "-Wfloat-equal"
18713 #endif
18714 if (value == 0) // +-0
18715 {
18716 *first++ = '0';
18717 // Make it look like a floating-point number (#362, #378)
18718 *first++ = '.';
18719 *first++ = '0';
18720 return first;
18721 }
18722 #ifdef __GNUC__
18723 #pragma GCC diagnostic pop
18724 #endif
18725
18726 JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10);
18727
18728 // Compute v = buffer * 10^decimal_exponent.
18729 // The decimal digits are stored in the buffer, which needs to be interpreted
18730 // as an unsigned decimal integer.
18731 // len is the length of the buffer, i.e. the number of decimal digits.
18732 int len = 0;
18733 int decimal_exponent = 0;
18734 dtoa_impl::grisu2(first, len, decimal_exponent, value);
18735
18736 JSON_ASSERT(len <= std::numeric_limits<FloatType>::max_digits10);
18737
18738 // Format the buffer like printf("%.*g", prec, value)
18739 constexpr int kMinExp = -4;
18740 // Use digits10 here to increase compatibility with version 2.
18741 constexpr int kMaxExp = std::numeric_limits<FloatType>::digits10;
18742
18743 JSON_ASSERT(last - first >= kMaxExp + 2);
18744 JSON_ASSERT(last - first >= 2 + (-kMinExp - 1) + std::numeric_limits<FloatType>::max_digits10);
18745 JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10 + 6);
18746
18747 return dtoa_impl::format_buffer(first, len, decimal_exponent, kMinExp, kMaxExp);
18748 }
18749
18750 } // namespace detail
18751 NLOHMANN_JSON_NAMESPACE_END
18752
18753 // #include <nlohmann/detail/exceptions.hpp>
18754
18755 // #include <nlohmann/detail/macro_scope.hpp>
18756
18757 // #include <nlohmann/detail/meta/cpp_future.hpp>
18758
18759 // #include <nlohmann/detail/output/binary_writer.hpp>
18760
18761 // #include <nlohmann/detail/output/output_adapters.hpp>
18762
18763 // #include <nlohmann/detail/string_concat.hpp>
18764
18765 // #include <nlohmann/detail/value_t.hpp>
18766
18767
18768 NLOHMANN_JSON_NAMESPACE_BEGIN
18769 namespace detail
18770 {
18771
18772 ///////////////////
18773 // serialization //
18774 ///////////////////
18775
18776 /// how to treat decoding errors
18777 enum class error_handler_t
18778 {
18779 strict, ///< throw a type_error exception in case of invalid UTF-8
18780 replace, ///< replace invalid UTF-8 sequences with U+FFFD
18781 ignore ///< ignore invalid UTF-8 sequences
18782 };
18783
18784 template<typename BasicJsonType>
18785 class serializer
18786 {
18787 using string_t = typename BasicJsonType::string_t;
18788 using number_float_t = typename BasicJsonType::number_float_t;
18789 using number_integer_t = typename BasicJsonType::number_integer_t;
18790 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
18791 using binary_char_t = typename BasicJsonType::binary_t::value_type;
18792 static constexpr std::uint8_t UTF8_ACCEPT = 0;
18793 static constexpr std::uint8_t UTF8_REJECT = 1;
18794
18795 public:
18796 /*!
18797 @param[in] s output stream to serialize to
18798 @param[in] ichar indentation character to use
18799 @param[in] error_handler_ how to react on decoding errors
18800 */
18801 serializer(output_adapter_t<char> s, const char ichar,
18802 error_handler_t error_handler_ = error_handler_t::strict)
18803 : o(std::move(s))
18804 , loc(std::localeconv())
18805 , thousands_sep(loc->thousands_sep == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->thousands_sep)))
18806 , decimal_point(loc->decimal_point == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->decimal_point)))
18807 , indent_char(ichar)
18808 , indent_string(512, indent_char)
18809 , error_handler(error_handler_)
18810 {}
18811
18812 // delete because of pointer members
18813 serializer(const serializer&) = delete;
18814 serializer& operator=(const serializer&) = delete;
18815 serializer(serializer&&) = delete;
18816 serializer& operator=(serializer&&) = delete;
18817 ~serializer() = default;
18818
18819 /*!
18820 @brief internal implementation of the serialization function
18821
18822 This function is called by the public member function dump and organizes
18823 the serialization internally. The indentation level is propagated as
18824 additional parameter. In case of arrays and objects, the function is
18825 called recursively.
18826
18827 - strings and object keys are escaped using `escape_string()`
18828 - integer numbers are converted implicitly via `operator<<`
18829 - floating-point numbers are converted to a string using `"%g"` format
18830 - binary values are serialized as objects containing the subtype and the
18831 byte array
18832
18833 @param[in] val value to serialize
18834 @param[in] pretty_print whether the output shall be pretty-printed
18835 @param[in] ensure_ascii If @a ensure_ascii is true, all non-ASCII characters
18836 in the output are escaped with `\uXXXX` sequences, and the result consists
18837 of ASCII characters only.
18838 @param[in] indent_step the indent level
18839 @param[in] current_indent the current indent level (only used internally)
18840 */
18841 void dump(const BasicJsonType& val,
18842 const bool pretty_print,
18843 const bool ensure_ascii,
18844 const unsigned int indent_step,
18845 const unsigned int current_indent = 0)
18846 {
18847 switch (val.m_data.m_type)
18848 {
18849 case value_t::object:
18850 {
18851 if (val.m_data.m_value.object->empty())
18852 {
18853 o->write_characters("{}", 2);
18854 return;
18855 }
18856
18857 if (pretty_print)
18858 {
18859 o->write_characters("{\n", 2);
18860
18861 // variable to hold indentation for recursive calls
18862 const auto new_indent = current_indent + indent_step;
18863 if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
18864 {
18865 indent_string.resize(indent_string.size() * 2, ' ');
18866 }
18867
18868 // first n-1 elements
18869 auto i = val.m_data.m_value.object->cbegin();
18870 for (std::size_t cnt = 0; cnt < val.m_data.m_value.object->size() - 1; ++cnt, ++i)
18871 {
18872 o->write_characters(indent_string.c_str(), new_indent);
18873 o->write_character('\"');
18874 dump_escaped(i->first, ensure_ascii);
18875 o->write_characters("\": ", 3);
18876 dump(i->second, true, ensure_ascii, indent_step, new_indent);
18877 o->write_characters(",\n", 2);
18878 }
18879
18880 // last element
18881 JSON_ASSERT(i != val.m_data.m_value.object->cend());
18882 JSON_ASSERT(std::next(i) == val.m_data.m_value.object->cend());
18883 o->write_characters(indent_string.c_str(), new_indent);
18884 o->write_character('\"');
18885 dump_escaped(i->first, ensure_ascii);
18886 o->write_characters("\": ", 3);
18887 dump(i->second, true, ensure_ascii, indent_step, new_indent);
18888
18889 o->write_character('\n');
18890 o->write_characters(indent_string.c_str(), current_indent);
18891 o->write_character('}');
18892 }
18893 else
18894 {
18895 o->write_character('{');
18896
18897 // first n-1 elements
18898 auto i = val.m_data.m_value.object->cbegin();
18899 for (std::size_t cnt = 0; cnt < val.m_data.m_value.object->size() - 1; ++cnt, ++i)
18900 {
18901 o->write_character('\"');
18902 dump_escaped(i->first, ensure_ascii);
18903 o->write_characters("\":", 2);
18904 dump(i->second, false, ensure_ascii, indent_step, current_indent);
18905 o->write_character(',');
18906 }
18907
18908 // last element
18909 JSON_ASSERT(i != val.m_data.m_value.object->cend());
18910 JSON_ASSERT(std::next(i) == val.m_data.m_value.object->cend());
18911 o->write_character('\"');
18912 dump_escaped(i->first, ensure_ascii);
18913 o->write_characters("\":", 2);
18914 dump(i->second, false, ensure_ascii, indent_step, current_indent);
18915
18916 o->write_character('}');
18917 }
18918
18919 return;
18920 }
18921
18922 case value_t::array:
18923 {
18924 if (val.m_data.m_value.array->empty())
18925 {
18926 o->write_characters("[]", 2);
18927 return;
18928 }
18929
18930 if (pretty_print)
18931 {
18932 o->write_characters("[\n", 2);
18933
18934 // variable to hold indentation for recursive calls
18935 const auto new_indent = current_indent + indent_step;
18936 if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
18937 {
18938 indent_string.resize(indent_string.size() * 2, ' ');
18939 }
18940
18941 // first n-1 elements
18942 for (auto i = val.m_data.m_value.array->cbegin();
18943 i != val.m_data.m_value.array->cend() - 1; ++i)
18944 {
18945 o->write_characters(indent_string.c_str(), new_indent);
18946 dump(*i, true, ensure_ascii, indent_step, new_indent);
18947 o->write_characters(",\n", 2);
18948 }
18949
18950 // last element
18951 JSON_ASSERT(!val.m_data.m_value.array->empty());
18952 o->write_characters(indent_string.c_str(), new_indent);
18953 dump(val.m_data.m_value.array->back(), true, ensure_ascii, indent_step, new_indent);
18954
18955 o->write_character('\n');
18956 o->write_characters(indent_string.c_str(), current_indent);
18957 o->write_character(']');
18958 }
18959 else
18960 {
18961 o->write_character('[');
18962
18963 // first n-1 elements
18964 for (auto i = val.m_data.m_value.array->cbegin();
18965 i != val.m_data.m_value.array->cend() - 1; ++i)
18966 {
18967 dump(*i, false, ensure_ascii, indent_step, current_indent);
18968 o->write_character(',');
18969 }
18970
18971 // last element
18972 JSON_ASSERT(!val.m_data.m_value.array->empty());
18973 dump(val.m_data.m_value.array->back(), false, ensure_ascii, indent_step, current_indent);
18974
18975 o->write_character(']');
18976 }
18977
18978 return;
18979 }
18980
18981 case value_t::string:
18982 {
18983 o->write_character('\"');
18984 dump_escaped(*val.m_data.m_value.string, ensure_ascii);
18985 o->write_character('\"');
18986 return;
18987 }
18988
18989 case value_t::binary:
18990 {
18991 if (pretty_print)
18992 {
18993 o->write_characters("{\n", 2);
18994
18995 // variable to hold indentation for recursive calls
18996 const auto new_indent = current_indent + indent_step;
18997 if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
18998 {
18999 indent_string.resize(indent_string.size() * 2, ' ');
19000 }
19001
19002 o->write_characters(indent_string.c_str(), new_indent);
19003
19004 o->write_characters("\"bytes\": [", 10);
19005
19006 if (!val.m_data.m_value.binary->empty())
19007 {
19008 for (auto i = val.m_data.m_value.binary->cbegin();
19009 i != val.m_data.m_value.binary->cend() - 1; ++i)
19010 {
19011 dump_integer(*i);
19012 o->write_characters(", ", 2);
19013 }
19014 dump_integer(val.m_data.m_value.binary->back());
19015 }
19016
19017 o->write_characters("],\n", 3);
19018 o->write_characters(indent_string.c_str(), new_indent);
19019
19020 o->write_characters("\"subtype\": ", 11);
19021 if (val.m_data.m_value.binary->has_subtype())
19022 {
19023 dump_integer(val.m_data.m_value.binary->subtype());
19024 }
19025 else
19026 {
19027 o->write_characters("null", 4);
19028 }
19029 o->write_character('\n');
19030 o->write_characters(indent_string.c_str(), current_indent);
19031 o->write_character('}');
19032 }
19033 else
19034 {
19035 o->write_characters("{\"bytes\":[", 10);
19036
19037 if (!val.m_data.m_value.binary->empty())
19038 {
19039 for (auto i = val.m_data.m_value.binary->cbegin();
19040 i != val.m_data.m_value.binary->cend() - 1; ++i)
19041 {
19042 dump_integer(*i);
19043 o->write_character(',');
19044 }
19045 dump_integer(val.m_data.m_value.binary->back());
19046 }
19047
19048 o->write_characters("],\"subtype\":", 12);
19049 if (val.m_data.m_value.binary->has_subtype())
19050 {
19051 dump_integer(val.m_data.m_value.binary->subtype());
19052 o->write_character('}');
19053 }
19054 else
19055 {
19056 o->write_characters("null}", 5);
19057 }
19058 }
19059 return;
19060 }
19061
19062 case value_t::boolean:
19063 {
19064 if (val.m_data.m_value.boolean)
19065 {
19066 o->write_characters("true", 4);
19067 }
19068 else
19069 {
19070 o->write_characters("false", 5);
19071 }
19072 return;
19073 }
19074
19075 case value_t::number_integer:
19076 {
19077 dump_integer(val.m_data.m_value.number_integer);
19078 return;
19079 }
19080
19081 case value_t::number_unsigned:
19082 {
19083 dump_integer(val.m_data.m_value.number_unsigned);
19084 return;
19085 }
19086
19087 case value_t::number_float:
19088 {
19089 dump_float(val.m_data.m_value.number_float);
19090 return;
19091 }
19092
19093 case value_t::discarded:
19094 {
19095 o->write_characters("<discarded>", 11);
19096 return;
19097 }
19098
19099 case value_t::null:
19100 {
19101 o->write_characters("null", 4);
19102 return;
19103 }
19104
19105 default: // LCOV_EXCL_LINE
19106 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
19107 }
19108 }
19109
19110 JSON_PRIVATE_UNLESS_TESTED:
19111 /*!
19112 @brief dump escaped string
19113
19114 Escape a string by replacing certain special characters by a sequence of an
19115 escape character (backslash) and another character and other control
19116 characters by a sequence of "\u" followed by a four-digit hex
19117 representation. The escaped string is written to output stream @a o.
19118
19119 @param[in] s the string to escape
19120 @param[in] ensure_ascii whether to escape non-ASCII characters with
19121 \uXXXX sequences
19122
19123 @complexity Linear in the length of string @a s.
19124 */
19125 void dump_escaped(const string_t& s, const bool ensure_ascii)
19126 {
19127 std::uint32_t codepoint{};
19128 std::uint8_t state = UTF8_ACCEPT;
19129 std::size_t bytes = 0; // number of bytes written to string_buffer
19130
19131 // number of bytes written at the point of the last valid byte
19132 std::size_t bytes_after_last_accept = 0;
19133 std::size_t undumped_chars = 0;
19134
19135 for (std::size_t i = 0; i < s.size(); ++i)
19136 {
19137 const auto byte = static_cast<std::uint8_t>(s[i]);
19138
19139 switch (decode(state, codepoint, byte))
19140 {
19141 case UTF8_ACCEPT: // decode found a new code point
19142 {
19143 switch (codepoint)
19144 {
19145 case 0x08: // backspace
19146 {
19147 string_buffer[bytes++] = '\\';
19148 string_buffer[bytes++] = 'b';
19149 break;
19150 }
19151
19152 case 0x09: // horizontal tab
19153 {
19154 string_buffer[bytes++] = '\\';
19155 string_buffer[bytes++] = 't';
19156 break;
19157 }
19158
19159 case 0x0A: // newline
19160 {
19161 string_buffer[bytes++] = '\\';
19162 string_buffer[bytes++] = 'n';
19163 break;
19164 }
19165
19166 case 0x0C: // formfeed
19167 {
19168 string_buffer[bytes++] = '\\';
19169 string_buffer[bytes++] = 'f';
19170 break;
19171 }
19172
19173 case 0x0D: // carriage return
19174 {
19175 string_buffer[bytes++] = '\\';
19176 string_buffer[bytes++] = 'r';
19177 break;
19178 }
19179
19180 case 0x22: // quotation mark
19181 {
19182 string_buffer[bytes++] = '\\';
19183 string_buffer[bytes++] = '\"';
19184 break;
19185 }
19186
19187 case 0x5C: // reverse solidus
19188 {
19189 string_buffer[bytes++] = '\\';
19190 string_buffer[bytes++] = '\\';
19191 break;
19192 }
19193
19194 default:
19195 {
19196 // escape control characters (0x00..0x1F) or, if
19197 // ensure_ascii parameter is used, non-ASCII characters
19198 if ((codepoint <= 0x1F) || (ensure_ascii && (codepoint >= 0x7F)))
19199 {
19200 if (codepoint <= 0xFFFF)
19201 {
19202 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
19203 static_cast<void>((std::snprintf)(string_buffer.data() + bytes, 7, "\\u%04x",
19204 static_cast<std::uint16_t>(codepoint)));
19205 bytes += 6;
19206 }
19207 else
19208 {
19209 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
19210 static_cast<void>((std::snprintf)(string_buffer.data() + bytes, 13, "\\u%04x\\u%04x",
19211 static_cast<std::uint16_t>(0xD7C0u + (codepoint >> 10u)),
19212 static_cast<std::uint16_t>(0xDC00u + (codepoint & 0x3FFu))));
19213 bytes += 12;
19214 }
19215 }
19216 else
19217 {
19218 // copy byte to buffer (all previous bytes
19219 // been copied have in default case above)
19220 string_buffer[bytes++] = s[i];
19221 }
19222 break;
19223 }
19224 }
19225
19226 // write buffer and reset index; there must be 13 bytes
19227 // left, as this is the maximal number of bytes to be
19228 // written ("\uxxxx\uxxxx\0") for one code point
19229 if (string_buffer.size() - bytes < 13)
19230 {
19231 o->write_characters(string_buffer.data(), bytes);
19232 bytes = 0;
19233 }
19234
19235 // remember the byte position of this accept
19236 bytes_after_last_accept = bytes;
19237 undumped_chars = 0;
19238 break;
19239 }
19240
19241 case UTF8_REJECT: // decode found invalid UTF-8 byte
19242 {
19243 switch (error_handler)
19244 {
19245 case error_handler_t::strict:
19246 {
19247 JSON_THROW(type_error::create(316, concat("invalid UTF-8 byte at index ", std::to_string(i), ": 0x", hex_bytes(byte | 0)), nullptr));
19248 }
19249
19250 case error_handler_t::ignore:
19251 case error_handler_t::replace:
19252 {
19253 // in case we saw this character the first time, we
19254 // would like to read it again, because the byte
19255 // may be OK for itself, but just not OK for the
19256 // previous sequence
19257 if (undumped_chars > 0)
19258 {
19259 --i;
19260 }
19261
19262 // reset length buffer to the last accepted index;
19263 // thus removing/ignoring the invalid characters
19264 bytes = bytes_after_last_accept;
19265
19266 if (error_handler == error_handler_t::replace)
19267 {
19268 // add a replacement character
19269 if (ensure_ascii)
19270 {
19271 string_buffer[bytes++] = '\\';
19272 string_buffer[bytes++] = 'u';
19273 string_buffer[bytes++] = 'f';
19274 string_buffer[bytes++] = 'f';
19275 string_buffer[bytes++] = 'f';
19276 string_buffer[bytes++] = 'd';
19277 }
19278 else
19279 {
19280 string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xEF');
19281 string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBF');
19282 string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBD');
19283 }
19284
19285 // write buffer and reset index; there must be 13 bytes
19286 // left, as this is the maximal number of bytes to be
19287 // written ("\uxxxx\uxxxx\0") for one code point
19288 if (string_buffer.size() - bytes < 13)
19289 {
19290 o->write_characters(string_buffer.data(), bytes);
19291 bytes = 0;
19292 }
19293
19294 bytes_after_last_accept = bytes;
19295 }
19296
19297 undumped_chars = 0;
19298
19299 // continue processing the string
19300 state = UTF8_ACCEPT;
19301 break;
19302 }
19303
19304 default: // LCOV_EXCL_LINE
19305 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
19306 }
19307 break;
19308 }
19309
19310 default: // decode found yet incomplete multi-byte code point
19311 {
19312 if (!ensure_ascii)
19313 {
19314 // code point will not be escaped - copy byte to buffer
19315 string_buffer[bytes++] = s[i];
19316 }
19317 ++undumped_chars;
19318 break;
19319 }
19320 }
19321 }
19322
19323 // we finished processing the string
19324 if (JSON_HEDLEY_LIKELY(state == UTF8_ACCEPT))
19325 {
19326 // write buffer
19327 if (bytes > 0)
19328 {
19329 o->write_characters(string_buffer.data(), bytes);
19330 }
19331 }
19332 else
19333 {
19334 // we finish reading, but do not accept: string was incomplete
19335 switch (error_handler)
19336 {
19337 case error_handler_t::strict:
19338 {
19339 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));
19340 }
19341
19342 case error_handler_t::ignore:
19343 {
19344 // write all accepted bytes
19345 o->write_characters(string_buffer.data(), bytes_after_last_accept);
19346 break;
19347 }
19348
19349 case error_handler_t::replace:
19350 {
19351 // write all accepted bytes
19352 o->write_characters(string_buffer.data(), bytes_after_last_accept);
19353 // add a replacement character
19354 if (ensure_ascii)
19355 {
19356 o->write_characters("\\ufffd", 6);
19357 }
19358 else
19359 {
19360 o->write_characters("\xEF\xBF\xBD", 3);
19361 }
19362 break;
19363 }
19364
19365 default: // LCOV_EXCL_LINE
19366 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
19367 }
19368 }
19369 }
19370
19371 private:
19372 /*!
19373 @brief count digits
19374
19375 Count the number of decimal (base 10) digits for an input unsigned integer.
19376
19377 @param[in] x unsigned integer number to count its digits
19378 @return number of decimal digits
19379 */
19380 unsigned int count_digits(number_unsigned_t x) noexcept
19381 {
19382 unsigned int n_digits = 1;
19383 for (;;)
19384 {
19385 if (x < 10)
19386 {
19387 return n_digits;
19388 }
19389 if (x < 100)
19390 {
19391 return n_digits + 1;
19392 }
19393 if (x < 1000)
19394 {
19395 return n_digits + 2;
19396 }
19397 if (x < 10000)
19398 {
19399 return n_digits + 3;
19400 }
19401 x = x / 10000u;
19402 n_digits += 4;
19403 }
19404 }
19405
19406 /*!
19407 * @brief convert a byte to a uppercase hex representation
19408 * @param[in] byte byte to represent
19409 * @return representation ("00".."FF")
19410 */
19411 static std::string hex_bytes(std::uint8_t byte)
19412 {
19413 std::string result = "FF";
19414 constexpr const char* nibble_to_hex = "0123456789ABCDEF";
19415 result[0] = nibble_to_hex[byte / 16];
19416 result[1] = nibble_to_hex[byte % 16];
19417 return result;
19418 }
19419
19420 // templates to avoid warnings about useless casts
19421 template <typename NumberType, enable_if_t<std::is_signed<NumberType>::value, int> = 0>
19422 bool is_negative_number(NumberType x)
19423 {
19424 return x < 0;
19425 }
19426
19427 template < typename NumberType, enable_if_t <std::is_unsigned<NumberType>::value, int > = 0 >
19428 bool is_negative_number(NumberType /*unused*/)
19429 {
19430 return false;
19431 }
19432
19433 /*!
19434 @brief dump an integer
19435
19436 Dump a given integer to output stream @a o. Works internally with
19437 @a number_buffer.
19438
19439 @param[in] x integer number (signed or unsigned) to dump
19440 @tparam NumberType either @a number_integer_t or @a number_unsigned_t
19441 */
19442 template < typename NumberType, detail::enable_if_t <
19443 std::is_integral<NumberType>::value ||
19444 std::is_same<NumberType, number_unsigned_t>::value ||
19445 std::is_same<NumberType, number_integer_t>::value ||
19446 std::is_same<NumberType, binary_char_t>::value,
19447 int > = 0 >
19448 void dump_integer(NumberType x)
19449 {
19450 static constexpr std::array<std::array<char, 2>, 100> digits_to_99
19451 {
19452 {
19453 {{'0', '0'}}, {{'0', '1'}}, {{'0', '2'}}, {{'0', '3'}}, {{'0', '4'}}, {{'0', '5'}}, {{'0', '6'}}, {{'0', '7'}}, {{'0', '8'}}, {{'0', '9'}},
19454 {{'1', '0'}}, {{'1', '1'}}, {{'1', '2'}}, {{'1', '3'}}, {{'1', '4'}}, {{'1', '5'}}, {{'1', '6'}}, {{'1', '7'}}, {{'1', '8'}}, {{'1', '9'}},
19455 {{'2', '0'}}, {{'2', '1'}}, {{'2', '2'}}, {{'2', '3'}}, {{'2', '4'}}, {{'2', '5'}}, {{'2', '6'}}, {{'2', '7'}}, {{'2', '8'}}, {{'2', '9'}},
19456 {{'3', '0'}}, {{'3', '1'}}, {{'3', '2'}}, {{'3', '3'}}, {{'3', '4'}}, {{'3', '5'}}, {{'3', '6'}}, {{'3', '7'}}, {{'3', '8'}}, {{'3', '9'}},
19457 {{'4', '0'}}, {{'4', '1'}}, {{'4', '2'}}, {{'4', '3'}}, {{'4', '4'}}, {{'4', '5'}}, {{'4', '6'}}, {{'4', '7'}}, {{'4', '8'}}, {{'4', '9'}},
19458 {{'5', '0'}}, {{'5', '1'}}, {{'5', '2'}}, {{'5', '3'}}, {{'5', '4'}}, {{'5', '5'}}, {{'5', '6'}}, {{'5', '7'}}, {{'5', '8'}}, {{'5', '9'}},
19459 {{'6', '0'}}, {{'6', '1'}}, {{'6', '2'}}, {{'6', '3'}}, {{'6', '4'}}, {{'6', '5'}}, {{'6', '6'}}, {{'6', '7'}}, {{'6', '8'}}, {{'6', '9'}},
19460 {{'7', '0'}}, {{'7', '1'}}, {{'7', '2'}}, {{'7', '3'}}, {{'7', '4'}}, {{'7', '5'}}, {{'7', '6'}}, {{'7', '7'}}, {{'7', '8'}}, {{'7', '9'}},
19461 {{'8', '0'}}, {{'8', '1'}}, {{'8', '2'}}, {{'8', '3'}}, {{'8', '4'}}, {{'8', '5'}}, {{'8', '6'}}, {{'8', '7'}}, {{'8', '8'}}, {{'8', '9'}},
19462 {{'9', '0'}}, {{'9', '1'}}, {{'9', '2'}}, {{'9', '3'}}, {{'9', '4'}}, {{'9', '5'}}, {{'9', '6'}}, {{'9', '7'}}, {{'9', '8'}}, {{'9', '9'}},
19463 }
19464 };
19465
19466 // special case for "0"
19467 if (x == 0)
19468 {
19469 o->write_character('0');
19470 return;
19471 }
19472
19473 // use a pointer to fill the buffer
19474 auto buffer_ptr = number_buffer.begin(); // NOLINT(llvm-qualified-auto,readability-qualified-auto,cppcoreguidelines-pro-type-vararg,hicpp-vararg)
19475
19476 number_unsigned_t abs_value;
19477
19478 unsigned int n_chars{};
19479
19480 if (is_negative_number(x))
19481 {
19482 *buffer_ptr = '-';
19483 abs_value = remove_sign(static_cast<number_integer_t>(x));
19484
19485 // account one more byte for the minus sign
19486 n_chars = 1 + count_digits(abs_value);
19487 }
19488 else
19489 {
19490 abs_value = static_cast<number_unsigned_t>(x);
19491 n_chars = count_digits(abs_value);
19492 }
19493
19494 // spare 1 byte for '\0'
19495 JSON_ASSERT(n_chars < number_buffer.size() - 1);
19496
19497 // jump to the end to generate the string from backward,
19498 // so we later avoid reversing the result
19499 buffer_ptr += n_chars;
19500
19501 // Fast int2ascii implementation inspired by "Fastware" talk by Andrei Alexandrescu
19502 // See: https://www.youtube.com/watch?v=o4-CwDo2zpg
19503 while (abs_value >= 100)
19504 {
19505 const auto digits_index = static_cast<unsigned>((abs_value % 100));
19506 abs_value /= 100;
19507 *(--buffer_ptr) = digits_to_99[digits_index][1];
19508 *(--buffer_ptr) = digits_to_99[digits_index][0];
19509 }
19510
19511 if (abs_value >= 10)
19512 {
19513 const auto digits_index = static_cast<unsigned>(abs_value);
19514 *(--buffer_ptr) = digits_to_99[digits_index][1];
19515 *(--buffer_ptr) = digits_to_99[digits_index][0];
19516 }
19517 else
19518 {
19519 *(--buffer_ptr) = static_cast<char>('0' + abs_value);
19520 }
19521
19522 o->write_characters(number_buffer.data(), n_chars);
19523 }
19524
19525 /*!
19526 @brief dump a floating-point number
19527
19528 Dump a given floating-point number to output stream @a o. Works internally
19529 with @a number_buffer.
19530
19531 @param[in] x floating-point number to dump
19532 */
19533 void dump_float(number_float_t x)
19534 {
19535 // NaN / inf
19536 if (!std::isfinite(x))
19537 {
19538 o->write_characters("null", 4);
19539 return;
19540 }
19541
19542 // If number_float_t is an IEEE-754 single or double precision number,
19543 // use the Grisu2 algorithm to produce short numbers which are
19544 // guaranteed to round-trip, using strtof and strtod, resp.
19545 //
19546 // NB: The test below works if <long double> == <double>.
19547 static constexpr bool is_ieee_single_or_double
19548 = (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) ||
19549 (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);
19550
19551 dump_float(x, std::integral_constant<bool, is_ieee_single_or_double>());
19552 }
19553
19554 void dump_float(number_float_t x, std::true_type /*is_ieee_single_or_double*/)
19555 {
19556 auto* begin = number_buffer.data();
19557 auto* end = ::nlohmann::detail::to_chars(begin, begin + number_buffer.size(), x);
19558
19559 o->write_characters(begin, static_cast<size_t>(end - begin));
19560 }
19561
19562 void dump_float(number_float_t x, std::false_type /*is_ieee_single_or_double*/)
19563 {
19564 // get number of digits for a float -> text -> float round-trip
19565 static constexpr auto d = std::numeric_limits<number_float_t>::max_digits10;
19566
19567 // the actual conversion
19568 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
19569 std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*g", d, x);
19570
19571 // negative value indicates an error
19572 JSON_ASSERT(len > 0);
19573 // check if buffer was large enough
19574 JSON_ASSERT(static_cast<std::size_t>(len) < number_buffer.size());
19575
19576 // erase thousands separator
19577 if (thousands_sep != '\0')
19578 {
19579 // NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::remove returns an iterator, see https://github.com/nlohmann/json/issues/3081
19580 const auto end = std::remove(number_buffer.begin(), number_buffer.begin() + len, thousands_sep);
19581 std::fill(end, number_buffer.end(), '\0');
19582 JSON_ASSERT((end - number_buffer.begin()) <= len);
19583 len = (end - number_buffer.begin());
19584 }
19585
19586 // convert decimal point to '.'
19587 if (decimal_point != '\0' && decimal_point != '.')
19588 {
19589 // NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::find returns an iterator, see https://github.com/nlohmann/json/issues/3081
19590 const auto dec_pos = std::find(number_buffer.begin(), number_buffer.end(), decimal_point);
19591 if (dec_pos != number_buffer.end())
19592 {
19593 *dec_pos = '.';
19594 }
19595 }
19596
19597 o->write_characters(number_buffer.data(), static_cast<std::size_t>(len));
19598
19599 // determine if we need to append ".0"
19600 const bool value_is_int_like =
19601 std::none_of(number_buffer.begin(), number_buffer.begin() + len + 1,
19602 [](char c)
19603 {
19604 return c == '.' || c == 'e';
19605 });
19606
19607 if (value_is_int_like)
19608 {
19609 o->write_characters(".0", 2);
19610 }
19611 }
19612
19613 /*!
19614 @brief check whether a string is UTF-8 encoded
19615
19616 The function checks each byte of a string whether it is UTF-8 encoded. The
19617 result of the check is stored in the @a state parameter. The function must
19618 be called initially with state 0 (accept). State 1 means the string must
19619 be rejected, because the current byte is not allowed. If the string is
19620 completely processed, but the state is non-zero, the string ended
19621 prematurely; that is, the last byte indicated more bytes should have
19622 followed.
19623
19624 @param[in,out] state the state of the decoding
19625 @param[in,out] codep codepoint (valid only if resulting state is UTF8_ACCEPT)
19626 @param[in] byte next byte to decode
19627 @return new state
19628
19629 @note The function has been edited: a std::array is used.
19630
19631 @copyright Copyright (c) 2008-2009 Bjoern Hoehrmann <[email protected]>
19632 @sa http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
19633 */
19634 static std::uint8_t decode(std::uint8_t& state, std::uint32_t& codep, const std::uint8_t byte) noexcept
19635 {
19636 static const std::array<std::uint8_t, 400> utf8d =
19637 {
19638 {
19639 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
19640 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
19641 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
19642 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
19643 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
19644 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
19645 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
19646 0xA, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3, // E0..EF
19647 0xB, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, // F0..FF
19648 0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, 0x6, 0x1, 0x1, 0x1, 0x1, // s0..s0
19649 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
19650 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
19651 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
19652 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
19653 }
19654 };
19655
19656 JSON_ASSERT(byte < utf8d.size());
19657 const std::uint8_t type = utf8d[byte];
19658
19659 codep = (state != UTF8_ACCEPT)
19660 ? (byte & 0x3fu) | (codep << 6u)
19661 : (0xFFu >> type) & (byte);
19662
19663 const std::size_t index = 256u + (static_cast<size_t>(state) * 16u) + static_cast<size_t>(type);
19664 JSON_ASSERT(index < utf8d.size());
19665 state = utf8d[index];
19666 return state;
19667 }
19668
19669 /*
19670 * Overload to make the compiler happy while it is instantiating
19671 * dump_integer for number_unsigned_t.
19672 * Must never be called.
19673 */
19674 number_unsigned_t remove_sign(number_unsigned_t x)
19675 {
19676 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
19677 return x; // LCOV_EXCL_LINE
19678 }
19679
19680 /*
19681 * Helper function for dump_integer
19682 *
19683 * This function takes a negative signed integer and returns its absolute
19684 * value as unsigned integer. The plus/minus shuffling is necessary as we can
19685 * not directly remove the sign of an arbitrary signed integer as the
19686 * absolute values of INT_MIN and INT_MAX are usually not the same. See
19687 * #1708 for details.
19688 */
19689 number_unsigned_t remove_sign(number_integer_t x) noexcept
19690 {
19691 JSON_ASSERT(x < 0 && x < (std::numeric_limits<number_integer_t>::max)()); // NOLINT(misc-redundant-expression)
19692 return static_cast<number_unsigned_t>(-(x + 1)) + 1;
19693 }
19694
19695 private:
19696 /// the output of the serializer
19697 output_adapter_t<char> o = nullptr;
19698
19699 /// a (hopefully) large enough character buffer
19700 std::array<char, 64> number_buffer{{}};
19701
19702 /// the locale
19703 const std::lconv* loc = nullptr;
19704 /// the locale's thousand separator character
19705 const char thousands_sep = '\0';
19706 /// the locale's decimal point character
19707 const char decimal_point = '\0';
19708
19709 /// string buffer
19710 std::array<char, 512> string_buffer{{}};
19711
19712 /// the indentation character
19713 const char indent_char;
19714 /// the indentation string
19715 string_t indent_string;
19716
19717 /// error_handler how to react on decoding errors
19718 const error_handler_t error_handler;
19719 };
19720
19721 } // namespace detail
19722 NLOHMANN_JSON_NAMESPACE_END
19723
19724 // #include <nlohmann/detail/value_t.hpp>
19725
19726 // #include <nlohmann/json_fwd.hpp>
19727
19728 // #include <nlohmann/ordered_map.hpp>
19729 // __ _____ _____ _____
19730 // __| | __| | | | JSON for Modern C++
19731 // | | |__ | | | | | | version 3.12.0
19732 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
19733 //
19734 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
19735 // SPDX-License-Identifier: MIT
19736
19737
19738
19739 #include <functional> // equal_to, less
19740 #include <initializer_list> // initializer_list
19741 #include <iterator> // input_iterator_tag, iterator_traits
19742 #include <memory> // allocator
19743 #include <stdexcept> // for out_of_range
19744 #include <type_traits> // enable_if, is_convertible
19745 #include <utility> // pair
19746 #include <vector> // vector
19747
19748 // #include <nlohmann/detail/macro_scope.hpp>
19749
19750 // #include <nlohmann/detail/meta/type_traits.hpp>
19751
19752
19753 NLOHMANN_JSON_NAMESPACE_BEGIN
19754
19755 /// ordered_map: a minimal map-like container that preserves insertion order
19756 /// for use within nlohmann::basic_json<ordered_map>
19757 template <class Key, class T, class IgnoredLess = std::less<Key>,
19758 class Allocator = std::allocator<std::pair<const Key, T>>>
19759 struct ordered_map : std::vector<std::pair<const Key, T>, Allocator>
19760 {
19761 using key_type = Key;
19762 using mapped_type = T;
19763 using Container = std::vector<std::pair<const Key, T>, Allocator>;
19764 using iterator = typename Container::iterator;
19765 using const_iterator = typename Container::const_iterator;
19766 using size_type = typename Container::size_type;
19767 using value_type = typename Container::value_type;
19768 #ifdef JSON_HAS_CPP_14
19769 using key_compare = std::equal_to<>;
19770 #else
19771 using key_compare = std::equal_to<Key>;
19772 #endif
19773
19774 // Explicit constructors instead of `using Container::Container`
19775 // otherwise older compilers choke on it (GCC <= 5.5, xcode <= 9.4)
19776 ordered_map() noexcept(noexcept(Container())) : Container{} {}
19777 explicit ordered_map(const Allocator& alloc) noexcept(noexcept(Container(alloc))) : Container{alloc} {}
19778 template <class It>
19779 ordered_map(It first, It last, const Allocator& alloc = Allocator())
19780 : Container{first, last, alloc} {}
19781 ordered_map(std::initializer_list<value_type> init, const Allocator& alloc = Allocator() )
19782 : Container{init, alloc} {}
19783
19784 std::pair<iterator, bool> emplace(const key_type& key, T&& t)
19785 {
19786 for (auto it = this->begin(); it != this->end(); ++it)
19787 {
19788 if (m_compare(it->first, key))
19789 {
19790 return {it, false};
19791 }
19792 }
19793 Container::emplace_back(key, std::forward<T>(t));
19794 return {std::prev(this->end()), true};
19795 }
19796
19797 template<class KeyType, detail::enable_if_t<
19798 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
19799 std::pair<iterator, bool> emplace(KeyType && key, T && t)
19800 {
19801 for (auto it = this->begin(); it != this->end(); ++it)
19802 {
19803 if (m_compare(it->first, key))
19804 {
19805 return {it, false};
19806 }
19807 }
19808 Container::emplace_back(std::forward<KeyType>(key), std::forward<T>(t));
19809 return {std::prev(this->end()), true};
19810 }
19811
19812 T& operator[](const key_type& key)
19813 {
19814 return emplace(key, T{}).first->second;
19815 }
19816
19817 template<class KeyType, detail::enable_if_t<
19818 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
19819 T & operator[](KeyType && key)
19820 {
19821 return emplace(std::forward<KeyType>(key), T{}).first->second;
19822 }
19823
19824 const T& operator[](const key_type& key) const
19825 {
19826 return at(key);
19827 }
19828
19829 template<class KeyType, detail::enable_if_t<
19830 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
19831 const T & operator[](KeyType && key) const
19832 {
19833 return at(std::forward<KeyType>(key));
19834 }
19835
19836 T& at(const key_type& key)
19837 {
19838 for (auto it = this->begin(); it != this->end(); ++it)
19839 {
19840 if (m_compare(it->first, key))
19841 {
19842 return it->second;
19843 }
19844 }
19845
19846 JSON_THROW(std::out_of_range("key not found"));
19847 }
19848
19849 template<class KeyType, detail::enable_if_t<
19850 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
19851 T & at(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
19852 {
19853 for (auto it = this->begin(); it != this->end(); ++it)
19854 {
19855 if (m_compare(it->first, key))
19856 {
19857 return it->second;
19858 }
19859 }
19860
19861 JSON_THROW(std::out_of_range("key not found"));
19862 }
19863
19864 const T& at(const key_type& key) const
19865 {
19866 for (auto it = this->begin(); it != this->end(); ++it)
19867 {
19868 if (m_compare(it->first, key))
19869 {
19870 return it->second;
19871 }
19872 }
19873
19874 JSON_THROW(std::out_of_range("key not found"));
19875 }
19876
19877 template<class KeyType, detail::enable_if_t<
19878 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
19879 const T & at(KeyType && key) const // NOLINT(cppcoreguidelines-missing-std-forward)
19880 {
19881 for (auto it = this->begin(); it != this->end(); ++it)
19882 {
19883 if (m_compare(it->first, key))
19884 {
19885 return it->second;
19886 }
19887 }
19888
19889 JSON_THROW(std::out_of_range("key not found"));
19890 }
19891
19892 size_type erase(const key_type& key)
19893 {
19894 for (auto it = this->begin(); it != this->end(); ++it)
19895 {
19896 if (m_compare(it->first, key))
19897 {
19898 // Since we cannot move const Keys, re-construct them in place
19899 for (auto next = it; ++next != this->end(); ++it)
19900 {
19901 it->~value_type(); // Destroy but keep allocation
19902 new (&*it) value_type{std::move(*next)};
19903 }
19904 Container::pop_back();
19905 return 1;
19906 }
19907 }
19908 return 0;
19909 }
19910
19911 template<class KeyType, detail::enable_if_t<
19912 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
19913 size_type erase(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
19914 {
19915 for (auto it = this->begin(); it != this->end(); ++it)
19916 {
19917 if (m_compare(it->first, key))
19918 {
19919 // Since we cannot move const Keys, re-construct them in place
19920 for (auto next = it; ++next != this->end(); ++it)
19921 {
19922 it->~value_type(); // Destroy but keep allocation
19923 new (&*it) value_type{std::move(*next)};
19924 }
19925 Container::pop_back();
19926 return 1;
19927 }
19928 }
19929 return 0;
19930 }
19931
19932 iterator erase(iterator pos)
19933 {
19934 return erase(pos, std::next(pos));
19935 }
19936
19937 iterator erase(iterator first, iterator last)
19938 {
19939 if (first == last)
19940 {
19941 return first;
19942 }
19943
19944 const auto elements_affected = std::distance(first, last);
19945 const auto offset = std::distance(Container::begin(), first);
19946
19947 // This is the start situation. We need to delete elements_affected
19948 // elements (3 in this example: e, f, g), and need to return an
19949 // iterator past the last deleted element (h in this example).
19950 // Note that offset is the distance from the start of the vector
19951 // to first. We will need this later.
19952
19953 // [ a, b, c, d, e, f, g, h, i, j ]
19954 // ^ ^
19955 // first last
19956
19957 // Since we cannot move const Keys, we re-construct them in place.
19958 // We start at first and re-construct (viz. copy) the elements from
19959 // the back of the vector. Example for first iteration:
19960
19961 // ,--------.
19962 // v | destroy e and re-construct with h
19963 // [ a, b, c, d, e, f, g, h, i, j ]
19964 // ^ ^
19965 // it it + elements_affected
19966
19967 for (auto it = first; std::next(it, elements_affected) != Container::end(); ++it)
19968 {
19969 it->~value_type(); // destroy but keep allocation
19970 new (&*it) value_type{std::move(*std::next(it, elements_affected))}; // "move" next element to it
19971 }
19972
19973 // [ a, b, c, d, h, i, j, h, i, j ]
19974 // ^ ^
19975 // first last
19976
19977 // remove the unneeded elements at the end of the vector
19978 Container::resize(this->size() - static_cast<size_type>(elements_affected));
19979
19980 // [ a, b, c, d, h, i, j ]
19981 // ^ ^
19982 // first last
19983
19984 // first is now pointing past the last deleted element, but we cannot
19985 // use this iterator, because it may have been invalidated by the
19986 // resize call. Instead, we can return begin() + offset.
19987 return Container::begin() + offset;
19988 }
19989
19990 size_type count(const key_type& key) const
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 template<class KeyType, detail::enable_if_t<
20003 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
20004 size_type count(KeyType && key) const // NOLINT(cppcoreguidelines-missing-std-forward)
20005 {
20006 for (auto it = this->begin(); it != this->end(); ++it)
20007 {
20008 if (m_compare(it->first, key))
20009 {
20010 return 1;
20011 }
20012 }
20013 return 0;
20014 }
20015
20016 iterator find(const key_type& key)
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 template<class KeyType, detail::enable_if_t<
20029 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
20030 iterator find(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
20031 {
20032 for (auto it = this->begin(); it != this->end(); ++it)
20033 {
20034 if (m_compare(it->first, key))
20035 {
20036 return it;
20037 }
20038 }
20039 return Container::end();
20040 }
20041
20042 const_iterator find(const key_type& key) const
20043 {
20044 for (auto it = this->begin(); it != this->end(); ++it)
20045 {
20046 if (m_compare(it->first, key))
20047 {
20048 return it;
20049 }
20050 }
20051 return Container::end();
20052 }
20053
20054 std::pair<iterator, bool> insert( value_type&& value )
20055 {
20056 return emplace(value.first, std::move(value.second));
20057 }
20058
20059 std::pair<iterator, bool> insert( const value_type& value )
20060 {
20061 for (auto it = this->begin(); it != this->end(); ++it)
20062 {
20063 if (m_compare(it->first, value.first))
20064 {
20065 return {it, false};
20066 }
20067 }
20068 Container::push_back(value);
20069 return {--this->end(), true};
20070 }
20071
20072 template<typename InputIt>
20073 using require_input_iter = typename std::enable_if<std::is_convertible<typename std::iterator_traits<InputIt>::iterator_category,
20074 std::input_iterator_tag>::value>::type;
20075
20076 template<typename InputIt, typename = require_input_iter<InputIt>>
20077 void insert(InputIt first, InputIt last)
20078 {
20079 for (auto it = first; it != last; ++it)
20080 {
20081 insert(*it);
20082 }
20083 }
20084
20085 private:
20086 JSON_NO_UNIQUE_ADDRESS key_compare m_compare = key_compare();
20087 };
20088
20089 NLOHMANN_JSON_NAMESPACE_END
20090
20091
20092 #if defined(JSON_HAS_CPP_17)
20093 #if JSON_HAS_STATIC_RTTI
20094 #include <any>
20095 #endif
20096 #include <string_view>
20097 #endif
20098
20099 /*!
20100 @brief namespace for Niels Lohmann
20101 @see https://github.com/nlohmann
20102 @since version 1.0.0
20103 */
20104 NLOHMANN_JSON_NAMESPACE_BEGIN
20105
20106 /*!
20107 @brief a class to store JSON values
20108
20109 @internal
20110 @invariant The member variables @a m_value and @a m_type have the following
20111 relationship:
20112 - If `m_type == value_t::object`, then `m_value.object != nullptr`.
20113 - If `m_type == value_t::array`, then `m_value.array != nullptr`.
20114 - If `m_type == value_t::string`, then `m_value.string != nullptr`.
20115 The invariants are checked by member function assert_invariant().
20116
20117 @note ObjectType trick from https://stackoverflow.com/a/9860911
20118 @endinternal
20119
20120 @since version 1.0.0
20121
20122 @nosubgrouping
20123 */
20124 NLOHMANN_BASIC_JSON_TPL_DECLARATION
20125 class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions)
20126 : public ::nlohmann::detail::json_base_class<CustomBaseClass>
20127 {
20128 private:
20129 template<detail::value_t> friend struct detail::external_constructor;
20130
20131 template<typename>
20132 friend class ::nlohmann::json_pointer;
20133 // can be restored when json_pointer backwards compatibility is removed
20134 // friend ::nlohmann::json_pointer<StringType>;
20135
20136 template<typename BasicJsonType, typename InputType>
20137 friend class ::nlohmann::detail::parser;
20138 friend ::nlohmann::detail::serializer<basic_json>;
20139 template<typename BasicJsonType>
20140 friend class ::nlohmann::detail::iter_impl;
20141 template<typename BasicJsonType, typename CharType>
20142 friend class ::nlohmann::detail::binary_writer;
20143 template<typename BasicJsonType, typename InputType, typename SAX>
20144 friend class ::nlohmann::detail::binary_reader;
20145 template<typename BasicJsonType, typename InputAdapterType>
20146 friend class ::nlohmann::detail::json_sax_dom_parser;
20147 template<typename BasicJsonType, typename InputAdapterType>
20148 friend class ::nlohmann::detail::json_sax_dom_callback_parser;
20149 friend class ::nlohmann::detail::exception;
20150
20151 /// workaround type for MSVC
20152 using basic_json_t = NLOHMANN_BASIC_JSON_TPL;
20153 using json_base_class_t = ::nlohmann::detail::json_base_class<CustomBaseClass>;
20154
20155 JSON_PRIVATE_UNLESS_TESTED:
20156 // convenience aliases for types residing in namespace detail;
20157 using lexer = ::nlohmann::detail::lexer_base<basic_json>;
20158
20159 template<typename InputAdapterType>
20160 static ::nlohmann::detail::parser<basic_json, InputAdapterType> parser(
20161 InputAdapterType adapter,
20162 detail::parser_callback_t<basic_json>cb = nullptr,
20163 const bool allow_exceptions = true,
20164 const bool ignore_comments = false
20165 )
20166 {
20167 return ::nlohmann::detail::parser<basic_json, InputAdapterType>(std::move(adapter),
20168 std::move(cb), allow_exceptions, ignore_comments);
20169 }
20170
20171 private:
20172 using primitive_iterator_t = ::nlohmann::detail::primitive_iterator_t;
20173 template<typename BasicJsonType>
20174 using internal_iterator = ::nlohmann::detail::internal_iterator<BasicJsonType>;
20175 template<typename BasicJsonType>
20176 using iter_impl = ::nlohmann::detail::iter_impl<BasicJsonType>;
20177 template<typename Iterator>
20178 using iteration_proxy = ::nlohmann::detail::iteration_proxy<Iterator>;
20179 template<typename Base> using json_reverse_iterator = ::nlohmann::detail::json_reverse_iterator<Base>;
20180
20181 template<typename CharType>
20182 using output_adapter_t = ::nlohmann::detail::output_adapter_t<CharType>;
20183
20184 template<typename InputType>
20185 using binary_reader = ::nlohmann::detail::binary_reader<basic_json, InputType>;
20186 template<typename CharType> using binary_writer = ::nlohmann::detail::binary_writer<basic_json, CharType>;
20187
20188 JSON_PRIVATE_UNLESS_TESTED:
20189 using serializer = ::nlohmann::detail::serializer<basic_json>;
20190
20191 public:
20192 using value_t = detail::value_t;
20193 /// JSON Pointer, see @ref nlohmann::json_pointer
20194 using json_pointer = ::nlohmann::json_pointer<StringType>;
20195 template<typename T, typename SFINAE>
20196 using json_serializer = JSONSerializer<T, SFINAE>;
20197 /// how to treat decoding errors
20198 using error_handler_t = detail::error_handler_t;
20199 /// how to treat CBOR tags
20200 using cbor_tag_handler_t = detail::cbor_tag_handler_t;
20201 /// how to encode BJData
20202 using bjdata_version_t = detail::bjdata_version_t;
20203 /// helper type for initializer lists of basic_json values
20204 using initializer_list_t = std::initializer_list<detail::json_ref<basic_json>>;
20205
20206 using input_format_t = detail::input_format_t;
20207 /// SAX interface type, see @ref nlohmann::json_sax
20208 using json_sax_t = json_sax<basic_json>;
20209
20210 ////////////////
20211 // exceptions //
20212 ////////////////
20213
20214 /// @name exceptions
20215 /// Classes to implement user-defined exceptions.
20216 /// @{
20217
20218 using exception = detail::exception;
20219 using parse_error = detail::parse_error;
20220 using invalid_iterator = detail::invalid_iterator;
20221 using type_error = detail::type_error;
20222 using out_of_range = detail::out_of_range;
20223 using other_error = detail::other_error;
20224
20225 /// @}
20226
20227 /////////////////////
20228 // container types //
20229 /////////////////////
20230
20231 /// @name container types
20232 /// The canonic container types to use @ref basic_json like any other STL
20233 /// container.
20234 /// @{
20235
20236 /// the type of elements in a basic_json container
20237 using value_type = basic_json;
20238
20239 /// the type of an element reference
20240 using reference = value_type&;
20241 /// the type of an element const reference
20242 using const_reference = const value_type&;
20243
20244 /// a type to represent differences between iterators
20245 using difference_type = std::ptrdiff_t;
20246 /// a type to represent container sizes
20247 using size_type = std::size_t;
20248
20249 /// the allocator type
20250 using allocator_type = AllocatorType<basic_json>;
20251
20252 /// the type of an element pointer
20253 using pointer = typename std::allocator_traits<allocator_type>::pointer;
20254 /// the type of an element const pointer
20255 using const_pointer = typename std::allocator_traits<allocator_type>::const_pointer;
20256
20257 /// an iterator for a basic_json container
20258 using iterator = iter_impl<basic_json>;
20259 /// a const iterator for a basic_json container
20260 using const_iterator = iter_impl<const basic_json>;
20261 /// a reverse iterator for a basic_json container
20262 using reverse_iterator = json_reverse_iterator<typename basic_json::iterator>;
20263 /// a const reverse iterator for a basic_json container
20264 using const_reverse_iterator = json_reverse_iterator<typename basic_json::const_iterator>;
20265
20266 /// @}
20267
20268 /// @brief returns the allocator associated with the container
20269 /// @sa https://json.nlohmann.me/api/basic_json/get_allocator/
20270 static allocator_type get_allocator()
20271 {
20272 return allocator_type();
20273 }
20274
20275 /// @brief returns version information on the library
20276 /// @sa https://json.nlohmann.me/api/basic_json/meta/
20277 JSON_HEDLEY_WARN_UNUSED_RESULT
20278 static basic_json meta()
20279 {
20280 basic_json result;
20281
20282 result["copyright"] = "(C) 2013-2025 Niels Lohmann";
20283 result["name"] = "JSON for Modern C++";
20284 result["url"] = "https://github.com/nlohmann/json";
20285 result["version"]["string"] =
20286 detail::concat(std::to_string(NLOHMANN_JSON_VERSION_MAJOR), '.',
20287 std::to_string(NLOHMANN_JSON_VERSION_MINOR), '.',
20288 std::to_string(NLOHMANN_JSON_VERSION_PATCH));
20289 result["version"]["major"] = NLOHMANN_JSON_VERSION_MAJOR;
20290 result["version"]["minor"] = NLOHMANN_JSON_VERSION_MINOR;
20291 result["version"]["patch"] = NLOHMANN_JSON_VERSION_PATCH;
20292
20293 #ifdef _WIN32
20294 result["platform"] = "win32";
20295 #elif defined __linux__
20296 result["platform"] = "linux";
20297 #elif defined __APPLE__
20298 result["platform"] = "apple";
20299 #elif defined __unix__
20300 result["platform"] = "unix";
20301 #else
20302 result["platform"] = "unknown";
20303 #endif
20304
20305 #if defined(__ICC) || defined(__INTEL_COMPILER)
20306 result["compiler"] = {{"family", "icc"}, {"version", __INTEL_COMPILER}};
20307 #elif defined(__clang__)
20308 result["compiler"] = {{"family", "clang"}, {"version", __clang_version__}};
20309 #elif defined(__GNUC__) || defined(__GNUG__)
20310 result["compiler"] = {{"family", "gcc"}, {"version", detail::concat(
20311 std::to_string(__GNUC__), '.',
20312 std::to_string(__GNUC_MINOR__), '.',
20313 std::to_string(__GNUC_PATCHLEVEL__))
20314 }
20315 };
20316 #elif defined(__HP_cc) || defined(__HP_aCC)
20317 result["compiler"] = "hp"
20318 #elif defined(__IBMCPP__)
20319 result["compiler"] = {{"family", "ilecpp"}, {"version", __IBMCPP__}};
20320 #elif defined(_MSC_VER)
20321 result["compiler"] = {{"family", "msvc"}, {"version", _MSC_VER}};
20322 #elif defined(__PGI)
20323 result["compiler"] = {{"family", "pgcpp"}, {"version", __PGI}};
20324 #elif defined(__SUNPRO_CC)
20325 result["compiler"] = {{"family", "sunpro"}, {"version", __SUNPRO_CC}};
20326 #else
20327 result["compiler"] = {{"family", "unknown"}, {"version", "unknown"}};
20328 #endif
20329
20330 #if defined(_MSVC_LANG)
20331 result["compiler"]["c++"] = std::to_string(_MSVC_LANG);
20332 #elif defined(__cplusplus)
20333 result["compiler"]["c++"] = std::to_string(__cplusplus);
20334 #else
20335 result["compiler"]["c++"] = "unknown";
20336 #endif
20337 return result;
20338 }
20339
20340 ///////////////////////////
20341 // JSON value data types //
20342 ///////////////////////////
20343
20344 /// @name JSON value data types
20345 /// The data types to store a JSON value. These types are derived from
20346 /// the template arguments passed to class @ref basic_json.
20347 /// @{
20348
20349 /// @brief default object key comparator type
20350 /// The actual object key comparator type (@ref object_comparator_t) may be
20351 /// different.
20352 /// @sa https://json.nlohmann.me/api/basic_json/default_object_comparator_t/
20353 #if defined(JSON_HAS_CPP_14)
20354 // use of transparent comparator avoids unnecessary repeated construction of temporaries
20355 // in functions involving lookup by key with types other than object_t::key_type (aka. StringType)
20356 using default_object_comparator_t = std::less<>;
20357 #else
20358 using default_object_comparator_t = std::less<StringType>;
20359 #endif
20360
20361 /// @brief a type for an object
20362 /// @sa https://json.nlohmann.me/api/basic_json/object_t/
20363 using object_t = ObjectType<StringType,
20364 basic_json,
20365 default_object_comparator_t,
20366 AllocatorType<std::pair<const StringType,
20367 basic_json>>>;
20368
20369 /// @brief a type for an array
20370 /// @sa https://json.nlohmann.me/api/basic_json/array_t/
20371 using array_t = ArrayType<basic_json, AllocatorType<basic_json>>;
20372
20373 /// @brief a type for a string
20374 /// @sa https://json.nlohmann.me/api/basic_json/string_t/
20375 using string_t = StringType;
20376
20377 /// @brief a type for a boolean
20378 /// @sa https://json.nlohmann.me/api/basic_json/boolean_t/
20379 using boolean_t = BooleanType;
20380
20381 /// @brief a type for a number (integer)
20382 /// @sa https://json.nlohmann.me/api/basic_json/number_integer_t/
20383 using number_integer_t = NumberIntegerType;
20384
20385 /// @brief a type for a number (unsigned)
20386 /// @sa https://json.nlohmann.me/api/basic_json/number_unsigned_t/
20387 using number_unsigned_t = NumberUnsignedType;
20388
20389 /// @brief a type for a number (floating-point)
20390 /// @sa https://json.nlohmann.me/api/basic_json/number_float_t/
20391 using number_float_t = NumberFloatType;
20392
20393 /// @brief a type for a packed binary type
20394 /// @sa https://json.nlohmann.me/api/basic_json/binary_t/
20395 using binary_t = nlohmann::byte_container_with_subtype<BinaryType>;
20396
20397 /// @brief object key comparator type
20398 /// @sa https://json.nlohmann.me/api/basic_json/object_comparator_t/
20399 using object_comparator_t = detail::actual_object_comparator_t<basic_json>;
20400
20401 /// @}
20402
20403 private:
20404
20405 /// helper for exception-safe object creation
20406 template<typename T, typename... Args>
20407 JSON_HEDLEY_RETURNS_NON_NULL
20408 static T* create(Args&& ... args)
20409 {
20410 AllocatorType<T> alloc;
20411 using AllocatorTraits = std::allocator_traits<AllocatorType<T>>;
20412
20413 auto deleter = [&](T * obj)
20414 {
20415 AllocatorTraits::deallocate(alloc, obj, 1);
20416 };
20417 std::unique_ptr<T, decltype(deleter)> obj(AllocatorTraits::allocate(alloc, 1), deleter);
20418 AllocatorTraits::construct(alloc, obj.get(), std::forward<Args>(args)...);
20419 JSON_ASSERT(obj != nullptr);
20420 return obj.release();
20421 }
20422
20423 ////////////////////////
20424 // JSON value storage //
20425 ////////////////////////
20426
20427 JSON_PRIVATE_UNLESS_TESTED:
20428 /*!
20429 @brief a JSON value
20430
20431 The actual storage for a JSON value of the @ref basic_json class. This
20432 union combines the different storage types for the JSON value types
20433 defined in @ref value_t.
20434
20435 JSON type | value_t type | used type
20436 --------- | --------------- | ------------------------
20437 object | object | pointer to @ref object_t
20438 array | array | pointer to @ref array_t
20439 string | string | pointer to @ref string_t
20440 boolean | boolean | @ref boolean_t
20441 number | number_integer | @ref number_integer_t
20442 number | number_unsigned | @ref number_unsigned_t
20443 number | number_float | @ref number_float_t
20444 binary | binary | pointer to @ref binary_t
20445 null | null | *no value is stored*
20446
20447 @note Variable-length types (objects, arrays, and strings) are stored as
20448 pointers. The size of the union should not exceed 64 bits if the default
20449 value types are used.
20450
20451 @since version 1.0.0
20452 */
20453 union json_value
20454 {
20455 /// object (stored with pointer to save storage)
20456 object_t* object;
20457 /// array (stored with pointer to save storage)
20458 array_t* array;
20459 /// string (stored with pointer to save storage)
20460 string_t* string;
20461 /// binary (stored with pointer to save storage)
20462 binary_t* binary;
20463 /// boolean
20464 boolean_t boolean;
20465 /// number (integer)
20466 number_integer_t number_integer;
20467 /// number (unsigned integer)
20468 number_unsigned_t number_unsigned;
20469 /// number (floating-point)
20470 number_float_t number_float;
20471
20472 /// default constructor (for null values)
20473 json_value() = default;
20474 /// constructor for booleans
20475 json_value(boolean_t v) noexcept : boolean(v) {}
20476 /// constructor for numbers (integer)
20477 json_value(number_integer_t v) noexcept : number_integer(v) {}
20478 /// constructor for numbers (unsigned)
20479 json_value(number_unsigned_t v) noexcept : number_unsigned(v) {}
20480 /// constructor for numbers (floating-point)
20481 json_value(number_float_t v) noexcept : number_float(v) {}
20482 /// constructor for empty values of a given type
20483 json_value(value_t t)
20484 {
20485 switch (t)
20486 {
20487 case value_t::object:
20488 {
20489 object = create<object_t>();
20490 break;
20491 }
20492
20493 case value_t::array:
20494 {
20495 array = create<array_t>();
20496 break;
20497 }
20498
20499 case value_t::string:
20500 {
20501 string = create<string_t>("");
20502 break;
20503 }
20504
20505 case value_t::binary:
20506 {
20507 binary = create<binary_t>();
20508 break;
20509 }
20510
20511 case value_t::boolean:
20512 {
20513 boolean = static_cast<boolean_t>(false);
20514 break;
20515 }
20516
20517 case value_t::number_integer:
20518 {
20519 number_integer = static_cast<number_integer_t>(0);
20520 break;
20521 }
20522
20523 case value_t::number_unsigned:
20524 {
20525 number_unsigned = static_cast<number_unsigned_t>(0);
20526 break;
20527 }
20528
20529 case value_t::number_float:
20530 {
20531 number_float = static_cast<number_float_t>(0.0);
20532 break;
20533 }
20534
20535 case value_t::null:
20536 {
20537 object = nullptr; // silence warning, see #821
20538 break;
20539 }
20540
20541 case value_t::discarded:
20542 default:
20543 {
20544 object = nullptr; // silence warning, see #821
20545 if (JSON_HEDLEY_UNLIKELY(t == value_t::null))
20546 {
20547 JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.12.0", nullptr)); // LCOV_EXCL_LINE
20548 }
20549 break;
20550 }
20551 }
20552 }
20553
20554 /// constructor for strings
20555 json_value(const string_t& value) : string(create<string_t>(value)) {}
20556
20557 /// constructor for rvalue strings
20558 json_value(string_t&& value) : string(create<string_t>(std::move(value))) {}
20559
20560 /// constructor for objects
20561 json_value(const object_t& value) : object(create<object_t>(value)) {}
20562
20563 /// constructor for rvalue objects
20564 json_value(object_t&& value) : object(create<object_t>(std::move(value))) {}
20565
20566 /// constructor for arrays
20567 json_value(const array_t& value) : array(create<array_t>(value)) {}
20568
20569 /// constructor for rvalue arrays
20570 json_value(array_t&& value) : array(create<array_t>(std::move(value))) {}
20571
20572 /// constructor for binary arrays
20573 json_value(const typename binary_t::container_type& value) : binary(create<binary_t>(value)) {}
20574
20575 /// constructor for rvalue binary arrays
20576 json_value(typename binary_t::container_type&& value) : binary(create<binary_t>(std::move(value))) {}
20577
20578 /// constructor for binary arrays (internal type)
20579 json_value(const binary_t& value) : binary(create<binary_t>(value)) {}
20580
20581 /// constructor for rvalue binary arrays (internal type)
20582 json_value(binary_t&& value) : binary(create<binary_t>(std::move(value))) {}
20583
20584 void destroy(value_t t)
20585 {
20586 if (
20587 (t == value_t::object && object == nullptr) ||
20588 (t == value_t::array && array == nullptr) ||
20589 (t == value_t::string && string == nullptr) ||
20590 (t == value_t::binary && binary == nullptr)
20591 )
20592 {
20593 //not initialized (e.g. due to exception in the ctor)
20594 return;
20595 }
20596 if (t == value_t::array || t == value_t::object)
20597 {
20598 // flatten the current json_value to a heap-allocated stack
20599 std::vector<basic_json> stack;
20600
20601 // move the top-level items to stack
20602 if (t == value_t::array)
20603 {
20604 stack.reserve(array->size());
20605 std::move(array->begin(), array->end(), std::back_inserter(stack));
20606 }
20607 else
20608 {
20609 stack.reserve(object->size());
20610 for (auto&& it : *object)
20611 {
20612 stack.push_back(std::move(it.second));
20613 }
20614 }
20615
20616 while (!stack.empty())
20617 {
20618 // move the last item to local variable to be processed
20619 basic_json current_item(std::move(stack.back()));
20620 stack.pop_back();
20621
20622 // if current_item is array/object, move
20623 // its children to the stack to be processed later
20624 if (current_item.is_array())
20625 {
20626 std::move(current_item.m_data.m_value.array->begin(), current_item.m_data.m_value.array->end(), std::back_inserter(stack));
20627
20628 current_item.m_data.m_value.array->clear();
20629 }
20630 else if (current_item.is_object())
20631 {
20632 for (auto&& it : *current_item.m_data.m_value.object)
20633 {
20634 stack.push_back(std::move(it.second));
20635 }
20636
20637 current_item.m_data.m_value.object->clear();
20638 }
20639
20640 // it's now safe that current_item get destructed
20641 // since it doesn't have any children
20642 }
20643 }
20644
20645 switch (t)
20646 {
20647 case value_t::object:
20648 {
20649 AllocatorType<object_t> alloc;
20650 std::allocator_traits<decltype(alloc)>::destroy(alloc, object);
20651 std::allocator_traits<decltype(alloc)>::deallocate(alloc, object, 1);
20652 break;
20653 }
20654
20655 case value_t::array:
20656 {
20657 AllocatorType<array_t> alloc;
20658 std::allocator_traits<decltype(alloc)>::destroy(alloc, array);
20659 std::allocator_traits<decltype(alloc)>::deallocate(alloc, array, 1);
20660 break;
20661 }
20662
20663 case value_t::string:
20664 {
20665 AllocatorType<string_t> alloc;
20666 std::allocator_traits<decltype(alloc)>::destroy(alloc, string);
20667 std::allocator_traits<decltype(alloc)>::deallocate(alloc, string, 1);
20668 break;
20669 }
20670
20671 case value_t::binary:
20672 {
20673 AllocatorType<binary_t> alloc;
20674 std::allocator_traits<decltype(alloc)>::destroy(alloc, binary);
20675 std::allocator_traits<decltype(alloc)>::deallocate(alloc, binary, 1);
20676 break;
20677 }
20678
20679 case value_t::null:
20680 case value_t::boolean:
20681 case value_t::number_integer:
20682 case value_t::number_unsigned:
20683 case value_t::number_float:
20684 case value_t::discarded:
20685 default:
20686 {
20687 break;
20688 }
20689 }
20690 }
20691 };
20692
20693 private:
20694 /*!
20695 @brief checks the class invariants
20696
20697 This function asserts the class invariants. It needs to be called at the
20698 end of every constructor to make sure that created objects respect the
20699 invariant. Furthermore, it has to be called each time the type of a JSON
20700 value is changed, because the invariant expresses a relationship between
20701 @a m_type and @a m_value.
20702
20703 Furthermore, the parent relation is checked for arrays and objects: If
20704 @a check_parents true and the value is an array or object, then the
20705 container's elements must have the current value as parent.
20706
20707 @param[in] check_parents whether the parent relation should be checked.
20708 The value is true by default and should only be set to false
20709 during destruction of objects when the invariant does not
20710 need to hold.
20711 */
20712 void assert_invariant(bool check_parents = true) const noexcept
20713 {
20714 JSON_ASSERT(m_data.m_type != value_t::object || m_data.m_value.object != nullptr);
20715 JSON_ASSERT(m_data.m_type != value_t::array || m_data.m_value.array != nullptr);
20716 JSON_ASSERT(m_data.m_type != value_t::string || m_data.m_value.string != nullptr);
20717 JSON_ASSERT(m_data.m_type != value_t::binary || m_data.m_value.binary != nullptr);
20718
20719 #if JSON_DIAGNOSTICS
20720 JSON_TRY
20721 {
20722 // cppcheck-suppress assertWithSideEffect
20723 JSON_ASSERT(!check_parents || !is_structured() || std::all_of(begin(), end(), [this](const basic_json & j)
20724 {
20725 return j.m_parent == this;
20726 }));
20727 }
20728 JSON_CATCH(...) {} // LCOV_EXCL_LINE
20729 #endif
20730 static_cast<void>(check_parents);
20731 }
20732
20733 void set_parents()
20734 {
20735 #if JSON_DIAGNOSTICS
20736 switch (m_data.m_type)
20737 {
20738 case value_t::array:
20739 {
20740 for (auto& element : *m_data.m_value.array)
20741 {
20742 element.m_parent = this;
20743 }
20744 break;
20745 }
20746
20747 case value_t::object:
20748 {
20749 for (auto& element : *m_data.m_value.object)
20750 {
20751 element.second.m_parent = this;
20752 }
20753 break;
20754 }
20755
20756 case value_t::null:
20757 case value_t::string:
20758 case value_t::boolean:
20759 case value_t::number_integer:
20760 case value_t::number_unsigned:
20761 case value_t::number_float:
20762 case value_t::binary:
20763 case value_t::discarded:
20764 default:
20765 break;
20766 }
20767 #endif
20768 }
20769
20770 iterator set_parents(iterator it, typename iterator::difference_type count_set_parents)
20771 {
20772 #if JSON_DIAGNOSTICS
20773 for (typename iterator::difference_type i = 0; i < count_set_parents; ++i)
20774 {
20775 (it + i)->m_parent = this;
20776 }
20777 #else
20778 static_cast<void>(count_set_parents);
20779 #endif
20780 return it;
20781 }
20782
20783 reference set_parent(reference j, std::size_t old_capacity = detail::unknown_size())
20784 {
20785 #if JSON_DIAGNOSTICS
20786 if (old_capacity != detail::unknown_size())
20787 {
20788 // see https://github.com/nlohmann/json/issues/2838
20789 JSON_ASSERT(type() == value_t::array);
20790 if (JSON_HEDLEY_UNLIKELY(m_data.m_value.array->capacity() != old_capacity))
20791 {
20792 // capacity has changed: update all parents
20793 set_parents();
20794 return j;
20795 }
20796 }
20797
20798 // ordered_json uses a vector internally, so pointers could have
20799 // been invalidated; see https://github.com/nlohmann/json/issues/2962
20800 #ifdef JSON_HEDLEY_MSVC_VERSION
20801 #pragma warning(push )
20802 #pragma warning(disable : 4127) // ignore warning to replace if with if constexpr
20803 #endif
20804 if (detail::is_ordered_map<object_t>::value)
20805 {
20806 set_parents();
20807 return j;
20808 }
20809 #ifdef JSON_HEDLEY_MSVC_VERSION
20810 #pragma warning( pop )
20811 #endif
20812
20813 j.m_parent = this;
20814 #else
20815 static_cast<void>(j);
20816 static_cast<void>(old_capacity);
20817 #endif
20818 return j;
20819 }
20820
20821 public:
20822 //////////////////////////
20823 // JSON parser callback //
20824 //////////////////////////
20825
20826 /// @brief parser event types
20827 /// @sa https://json.nlohmann.me/api/basic_json/parse_event_t/
20828 using parse_event_t = detail::parse_event_t;
20829
20830 /// @brief per-element parser callback type
20831 /// @sa https://json.nlohmann.me/api/basic_json/parser_callback_t/
20832 using parser_callback_t = detail::parser_callback_t<basic_json>;
20833
20834 //////////////////
20835 // constructors //
20836 //////////////////
20837
20838 /// @name constructors and destructors
20839 /// Constructors of class @ref basic_json, copy/move constructor, copy
20840 /// assignment, static functions creating objects, and the destructor.
20841 /// @{
20842
20843 /// @brief create an empty value with a given type
20844 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
20845 basic_json(const value_t v)
20846 : m_data(v)
20847 {
20848 assert_invariant();
20849 }
20850
20851 /// @brief create a null object
20852 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
20853 basic_json(std::nullptr_t = nullptr) noexcept // NOLINT(bugprone-exception-escape)
20854 : basic_json(value_t::null)
20855 {
20856 assert_invariant();
20857 }
20858
20859 /// @brief create a JSON value from compatible types
20860 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
20861 template < typename CompatibleType,
20862 typename U = detail::uncvref_t<CompatibleType>,
20863 detail::enable_if_t <
20864 !detail::is_basic_json<U>::value && detail::is_compatible_type<basic_json_t, U>::value, int > = 0 >
20865 basic_json(CompatibleType && val) noexcept(noexcept( // NOLINT(bugprone-forwarding-reference-overload,bugprone-exception-escape)
20866 JSONSerializer<U>::to_json(std::declval<basic_json_t&>(),
20867 std::forward<CompatibleType>(val))))
20868 {
20869 JSONSerializer<U>::to_json(*this, std::forward<CompatibleType>(val));
20870 set_parents();
20871 assert_invariant();
20872 }
20873
20874 /// @brief create a JSON value from an existing one
20875 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
20876 template < typename BasicJsonType,
20877 detail::enable_if_t <
20878 detail::is_basic_json<BasicJsonType>::value&& !std::is_same<basic_json, BasicJsonType>::value, int > = 0 >
20879 basic_json(const BasicJsonType& val)
20880 #if JSON_DIAGNOSTIC_POSITIONS
20881 : start_position(val.start_pos()),
20882 end_position(val.end_pos())
20883 #endif
20884 {
20885 using other_boolean_t = typename BasicJsonType::boolean_t;
20886 using other_number_float_t = typename BasicJsonType::number_float_t;
20887 using other_number_integer_t = typename BasicJsonType::number_integer_t;
20888 using other_number_unsigned_t = typename BasicJsonType::number_unsigned_t;
20889 using other_string_t = typename BasicJsonType::string_t;
20890 using other_object_t = typename BasicJsonType::object_t;
20891 using other_array_t = typename BasicJsonType::array_t;
20892 using other_binary_t = typename BasicJsonType::binary_t;
20893
20894 switch (val.type())
20895 {
20896 case value_t::boolean:
20897 JSONSerializer<other_boolean_t>::to_json(*this, val.template get<other_boolean_t>());
20898 break;
20899 case value_t::number_float:
20900 JSONSerializer<other_number_float_t>::to_json(*this, val.template get<other_number_float_t>());
20901 break;
20902 case value_t::number_integer:
20903 JSONSerializer<other_number_integer_t>::to_json(*this, val.template get<other_number_integer_t>());
20904 break;
20905 case value_t::number_unsigned:
20906 JSONSerializer<other_number_unsigned_t>::to_json(*this, val.template get<other_number_unsigned_t>());
20907 break;
20908 case value_t::string:
20909 JSONSerializer<other_string_t>::to_json(*this, val.template get_ref<const other_string_t&>());
20910 break;
20911 case value_t::object:
20912 JSONSerializer<other_object_t>::to_json(*this, val.template get_ref<const other_object_t&>());
20913 break;
20914 case value_t::array:
20915 JSONSerializer<other_array_t>::to_json(*this, val.template get_ref<const other_array_t&>());
20916 break;
20917 case value_t::binary:
20918 JSONSerializer<other_binary_t>::to_json(*this, val.template get_ref<const other_binary_t&>());
20919 break;
20920 case value_t::null:
20921 *this = nullptr;
20922 break;
20923 case value_t::discarded:
20924 m_data.m_type = value_t::discarded;
20925 break;
20926 default: // LCOV_EXCL_LINE
20927 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
20928 }
20929 JSON_ASSERT(m_data.m_type == val.type());
20930
20931 set_parents();
20932 assert_invariant();
20933 }
20934
20935 /// @brief create a container (array or object) from an initializer list
20936 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
20937 basic_json(initializer_list_t init,
20938 bool type_deduction = true,
20939 value_t manual_type = value_t::array)
20940 {
20941 // check if each element is an array with two elements whose first
20942 // element is a string
20943 bool is_an_object = std::all_of(init.begin(), init.end(),
20944 [](const detail::json_ref<basic_json>& element_ref)
20945 {
20946 // The cast is to ensure op[size_type] is called, bearing in mind size_type may not be int;
20947 // (many string types can be constructed from 0 via its null-pointer guise, so we get a
20948 // broken call to op[key_type], the wrong semantics and a 4804 warning on Windows)
20949 return element_ref->is_array() && element_ref->size() == 2 && (*element_ref)[static_cast<size_type>(0)].is_string();
20950 });
20951
20952 // adjust type if type deduction is not wanted
20953 if (!type_deduction)
20954 {
20955 // if array is wanted, do not create an object though possible
20956 if (manual_type == value_t::array)
20957 {
20958 is_an_object = false;
20959 }
20960
20961 // if object is wanted but impossible, throw an exception
20962 if (JSON_HEDLEY_UNLIKELY(manual_type == value_t::object && !is_an_object))
20963 {
20964 JSON_THROW(type_error::create(301, "cannot create object from initializer list", nullptr));
20965 }
20966 }
20967
20968 if (is_an_object)
20969 {
20970 // the initializer list is a list of pairs -> create object
20971 m_data.m_type = value_t::object;
20972 m_data.m_value = value_t::object;
20973
20974 for (auto& element_ref : init)
20975 {
20976 auto element = element_ref.moved_or_copied();
20977 m_data.m_value.object->emplace(
20978 std::move(*((*element.m_data.m_value.array)[0].m_data.m_value.string)),
20979 std::move((*element.m_data.m_value.array)[1]));
20980 }
20981 }
20982 else
20983 {
20984 // the initializer list describes an array -> create array
20985 m_data.m_type = value_t::array;
20986 m_data.m_value.array = create<array_t>(init.begin(), init.end());
20987 }
20988
20989 set_parents();
20990 assert_invariant();
20991 }
20992
20993 /// @brief explicitly create a binary array (without subtype)
20994 /// @sa https://json.nlohmann.me/api/basic_json/binary/
20995 JSON_HEDLEY_WARN_UNUSED_RESULT
20996 static basic_json binary(const typename binary_t::container_type& init)
20997 {
20998 auto res = basic_json();
20999 res.m_data.m_type = value_t::binary;
21000 res.m_data.m_value = init;
21001 return res;
21002 }
21003
21004 /// @brief explicitly create a binary array (with subtype)
21005 /// @sa https://json.nlohmann.me/api/basic_json/binary/
21006 JSON_HEDLEY_WARN_UNUSED_RESULT
21007 static basic_json binary(const typename binary_t::container_type& init, typename binary_t::subtype_type subtype)
21008 {
21009 auto res = basic_json();
21010 res.m_data.m_type = value_t::binary;
21011 res.m_data.m_value = binary_t(init, subtype);
21012 return res;
21013 }
21014
21015 /// @brief explicitly create a binary array
21016 /// @sa https://json.nlohmann.me/api/basic_json/binary/
21017 JSON_HEDLEY_WARN_UNUSED_RESULT
21018 static basic_json binary(typename binary_t::container_type&& init)
21019 {
21020 auto res = basic_json();
21021 res.m_data.m_type = value_t::binary;
21022 res.m_data.m_value = std::move(init);
21023 return res;
21024 }
21025
21026 /// @brief explicitly create a binary array (with subtype)
21027 /// @sa https://json.nlohmann.me/api/basic_json/binary/
21028 JSON_HEDLEY_WARN_UNUSED_RESULT
21029 static basic_json binary(typename binary_t::container_type&& init, typename binary_t::subtype_type subtype)
21030 {
21031 auto res = basic_json();
21032 res.m_data.m_type = value_t::binary;
21033 res.m_data.m_value = binary_t(std::move(init), subtype);
21034 return res;
21035 }
21036
21037 /// @brief explicitly create an array from an initializer list
21038 /// @sa https://json.nlohmann.me/api/basic_json/array/
21039 JSON_HEDLEY_WARN_UNUSED_RESULT
21040 static basic_json array(initializer_list_t init = {})
21041 {
21042 return basic_json(init, false, value_t::array);
21043 }
21044
21045 /// @brief explicitly create an object from an initializer list
21046 /// @sa https://json.nlohmann.me/api/basic_json/object/
21047 JSON_HEDLEY_WARN_UNUSED_RESULT
21048 static basic_json object(initializer_list_t init = {})
21049 {
21050 return basic_json(init, false, value_t::object);
21051 }
21052
21053 /// @brief construct an array with count copies of given value
21054 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
21055 basic_json(size_type cnt, const basic_json& val):
21056 m_data{cnt, val}
21057 {
21058 set_parents();
21059 assert_invariant();
21060 }
21061
21062 /// @brief construct a JSON container given an iterator range
21063 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
21064 template < class InputIT, typename std::enable_if <
21065 std::is_same<InputIT, typename basic_json_t::iterator>::value ||
21066 std::is_same<InputIT, typename basic_json_t::const_iterator>::value, int >::type = 0 >
21067 basic_json(InputIT first, InputIT last) // NOLINT(performance-unnecessary-value-param)
21068 {
21069 JSON_ASSERT(first.m_object != nullptr);
21070 JSON_ASSERT(last.m_object != nullptr);
21071
21072 // make sure iterator fits the current value
21073 if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
21074 {
21075 JSON_THROW(invalid_iterator::create(201, "iterators are not compatible", nullptr));
21076 }
21077
21078 // copy type from first iterator
21079 m_data.m_type = first.m_object->m_data.m_type;
21080
21081 // check if iterator range is complete for primitive values
21082 switch (m_data.m_type)
21083 {
21084 case value_t::boolean:
21085 case value_t::number_float:
21086 case value_t::number_integer:
21087 case value_t::number_unsigned:
21088 case value_t::string:
21089 {
21090 if (JSON_HEDLEY_UNLIKELY(!first.m_it.primitive_iterator.is_begin()
21091 || !last.m_it.primitive_iterator.is_end()))
21092 {
21093 JSON_THROW(invalid_iterator::create(204, "iterators out of range", first.m_object));
21094 }
21095 break;
21096 }
21097
21098 case value_t::null:
21099 case value_t::object:
21100 case value_t::array:
21101 case value_t::binary:
21102 case value_t::discarded:
21103 default:
21104 break;
21105 }
21106
21107 switch (m_data.m_type)
21108 {
21109 case value_t::number_integer:
21110 {
21111 m_data.m_value.number_integer = first.m_object->m_data.m_value.number_integer;
21112 break;
21113 }
21114
21115 case value_t::number_unsigned:
21116 {
21117 m_data.m_value.number_unsigned = first.m_object->m_data.m_value.number_unsigned;
21118 break;
21119 }
21120
21121 case value_t::number_float:
21122 {
21123 m_data.m_value.number_float = first.m_object->m_data.m_value.number_float;
21124 break;
21125 }
21126
21127 case value_t::boolean:
21128 {
21129 m_data.m_value.boolean = first.m_object->m_data.m_value.boolean;
21130 break;
21131 }
21132
21133 case value_t::string:
21134 {
21135 m_data.m_value = *first.m_object->m_data.m_value.string;
21136 break;
21137 }
21138
21139 case value_t::object:
21140 {
21141 m_data.m_value.object = create<object_t>(first.m_it.object_iterator,
21142 last.m_it.object_iterator);
21143 break;
21144 }
21145
21146 case value_t::array:
21147 {
21148 m_data.m_value.array = create<array_t>(first.m_it.array_iterator,
21149 last.m_it.array_iterator);
21150 break;
21151 }
21152
21153 case value_t::binary:
21154 {
21155 m_data.m_value = *first.m_object->m_data.m_value.binary;
21156 break;
21157 }
21158
21159 case value_t::null:
21160 case value_t::discarded:
21161 default:
21162 JSON_THROW(invalid_iterator::create(206, detail::concat("cannot construct with iterators from ", first.m_object->type_name()), first.m_object));
21163 }
21164
21165 set_parents();
21166 assert_invariant();
21167 }
21168
21169 ///////////////////////////////////////
21170 // other constructors and destructor //
21171 ///////////////////////////////////////
21172
21173 template<typename JsonRef,
21174 detail::enable_if_t<detail::conjunction<detail::is_json_ref<JsonRef>,
21175 std::is_same<typename JsonRef::value_type, basic_json>>::value, int> = 0 >
21176 basic_json(const JsonRef& ref) : basic_json(ref.moved_or_copied()) {}
21177
21178 /// @brief copy constructor
21179 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
21180 basic_json(const basic_json& other)
21181 : json_base_class_t(other)
21182 #if JSON_DIAGNOSTIC_POSITIONS
21183 , start_position(other.start_position)
21184 , end_position(other.end_position)
21185 #endif
21186 {
21187 m_data.m_type = other.m_data.m_type;
21188 // check of passed value is valid
21189 other.assert_invariant();
21190
21191 switch (m_data.m_type)
21192 {
21193 case value_t::object:
21194 {
21195 m_data.m_value = *other.m_data.m_value.object;
21196 break;
21197 }
21198
21199 case value_t::array:
21200 {
21201 m_data.m_value = *other.m_data.m_value.array;
21202 break;
21203 }
21204
21205 case value_t::string:
21206 {
21207 m_data.m_value = *other.m_data.m_value.string;
21208 break;
21209 }
21210
21211 case value_t::boolean:
21212 {
21213 m_data.m_value = other.m_data.m_value.boolean;
21214 break;
21215 }
21216
21217 case value_t::number_integer:
21218 {
21219 m_data.m_value = other.m_data.m_value.number_integer;
21220 break;
21221 }
21222
21223 case value_t::number_unsigned:
21224 {
21225 m_data.m_value = other.m_data.m_value.number_unsigned;
21226 break;
21227 }
21228
21229 case value_t::number_float:
21230 {
21231 m_data.m_value = other.m_data.m_value.number_float;
21232 break;
21233 }
21234
21235 case value_t::binary:
21236 {
21237 m_data.m_value = *other.m_data.m_value.binary;
21238 break;
21239 }
21240
21241 case value_t::null:
21242 case value_t::discarded:
21243 default:
21244 break;
21245 }
21246
21247 set_parents();
21248 assert_invariant();
21249 }
21250
21251 /// @brief move constructor
21252 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
21253 basic_json(basic_json&& other) noexcept
21254 : json_base_class_t(std::forward<json_base_class_t>(other)),
21255 m_data(std::move(other.m_data)) // cppcheck-suppress[accessForwarded] TODO check
21256 #if JSON_DIAGNOSTIC_POSITIONS
21257 , start_position(other.start_position) // cppcheck-suppress[accessForwarded] TODO check
21258 , end_position(other.end_position) // cppcheck-suppress[accessForwarded] TODO check
21259 #endif
21260 {
21261 // check that passed value is valid
21262 other.assert_invariant(false); // cppcheck-suppress[accessForwarded]
21263
21264 // invalidate payload
21265 other.m_data.m_type = value_t::null;
21266 other.m_data.m_value = {};
21267
21268 #if JSON_DIAGNOSTIC_POSITIONS
21269 other.start_position = std::string::npos;
21270 other.end_position = std::string::npos;
21271 #endif
21272
21273 set_parents();
21274 assert_invariant();
21275 }
21276
21277 /// @brief copy assignment
21278 /// @sa https://json.nlohmann.me/api/basic_json/operator=/
21279 basic_json& operator=(basic_json other) noexcept (
21280 std::is_nothrow_move_constructible<value_t>::value&&
21281 std::is_nothrow_move_assignable<value_t>::value&&
21282 std::is_nothrow_move_constructible<json_value>::value&&
21283 std::is_nothrow_move_assignable<json_value>::value&&
21284 std::is_nothrow_move_assignable<json_base_class_t>::value
21285 )
21286 {
21287 // check that passed value is valid
21288 other.assert_invariant();
21289
21290 using std::swap;
21291 swap(m_data.m_type, other.m_data.m_type);
21292 swap(m_data.m_value, other.m_data.m_value);
21293
21294 #if JSON_DIAGNOSTIC_POSITIONS
21295 swap(start_position, other.start_position);
21296 swap(end_position, other.end_position);
21297 #endif
21298
21299 json_base_class_t::operator=(std::move(other));
21300
21301 set_parents();
21302 assert_invariant();
21303 return *this;
21304 }
21305
21306 /// @brief destructor
21307 /// @sa https://json.nlohmann.me/api/basic_json/~basic_json/
21308 ~basic_json() noexcept
21309 {
21310 assert_invariant(false);
21311 }
21312
21313 /// @}
21314
21315 public:
21316 ///////////////////////
21317 // object inspection //
21318 ///////////////////////
21319
21320 /// @name object inspection
21321 /// Functions to inspect the type of a JSON value.
21322 /// @{
21323
21324 /// @brief serialization
21325 /// @sa https://json.nlohmann.me/api/basic_json/dump/
21326 string_t dump(const int indent = -1,
21327 const char indent_char = ' ',
21328 const bool ensure_ascii = false,
21329 const error_handler_t error_handler = error_handler_t::strict) const
21330 {
21331 string_t result;
21332 serializer s(detail::output_adapter<char, string_t>(result), indent_char, error_handler);
21333
21334 if (indent >= 0)
21335 {
21336 s.dump(*this, true, ensure_ascii, static_cast<unsigned int>(indent));
21337 }
21338 else
21339 {
21340 s.dump(*this, false, ensure_ascii, 0);
21341 }
21342
21343 return result;
21344 }
21345
21346 /// @brief return the type of the JSON value (explicit)
21347 /// @sa https://json.nlohmann.me/api/basic_json/type/
21348 constexpr value_t type() const noexcept
21349 {
21350 return m_data.m_type;
21351 }
21352
21353 /// @brief return whether type is primitive
21354 /// @sa https://json.nlohmann.me/api/basic_json/is_primitive/
21355 constexpr bool is_primitive() const noexcept
21356 {
21357 return is_null() || is_string() || is_boolean() || is_number() || is_binary();
21358 }
21359
21360 /// @brief return whether type is structured
21361 /// @sa https://json.nlohmann.me/api/basic_json/is_structured/
21362 constexpr bool is_structured() const noexcept
21363 {
21364 return is_array() || is_object();
21365 }
21366
21367 /// @brief return whether value is null
21368 /// @sa https://json.nlohmann.me/api/basic_json/is_null/
21369 constexpr bool is_null() const noexcept
21370 {
21371 return m_data.m_type == value_t::null;
21372 }
21373
21374 /// @brief return whether value is a boolean
21375 /// @sa https://json.nlohmann.me/api/basic_json/is_boolean/
21376 constexpr bool is_boolean() const noexcept
21377 {
21378 return m_data.m_type == value_t::boolean;
21379 }
21380
21381 /// @brief return whether value is a number
21382 /// @sa https://json.nlohmann.me/api/basic_json/is_number/
21383 constexpr bool is_number() const noexcept
21384 {
21385 return is_number_integer() || is_number_float();
21386 }
21387
21388 /// @brief return whether value is an integer number
21389 /// @sa https://json.nlohmann.me/api/basic_json/is_number_integer/
21390 constexpr bool is_number_integer() const noexcept
21391 {
21392 return m_data.m_type == value_t::number_integer || m_data.m_type == value_t::number_unsigned;
21393 }
21394
21395 /// @brief return whether value is an unsigned integer number
21396 /// @sa https://json.nlohmann.me/api/basic_json/is_number_unsigned/
21397 constexpr bool is_number_unsigned() const noexcept
21398 {
21399 return m_data.m_type == value_t::number_unsigned;
21400 }
21401
21402 /// @brief return whether value is a floating-point number
21403 /// @sa https://json.nlohmann.me/api/basic_json/is_number_float/
21404 constexpr bool is_number_float() const noexcept
21405 {
21406 return m_data.m_type == value_t::number_float;
21407 }
21408
21409 /// @brief return whether value is an object
21410 /// @sa https://json.nlohmann.me/api/basic_json/is_object/
21411 constexpr bool is_object() const noexcept
21412 {
21413 return m_data.m_type == value_t::object;
21414 }
21415
21416 /// @brief return whether value is an array
21417 /// @sa https://json.nlohmann.me/api/basic_json/is_array/
21418 constexpr bool is_array() const noexcept
21419 {
21420 return m_data.m_type == value_t::array;
21421 }
21422
21423 /// @brief return whether value is a string
21424 /// @sa https://json.nlohmann.me/api/basic_json/is_string/
21425 constexpr bool is_string() const noexcept
21426 {
21427 return m_data.m_type == value_t::string;
21428 }
21429
21430 /// @brief return whether value is a binary array
21431 /// @sa https://json.nlohmann.me/api/basic_json/is_binary/
21432 constexpr bool is_binary() const noexcept
21433 {
21434 return m_data.m_type == value_t::binary;
21435 }
21436
21437 /// @brief return whether value is discarded
21438 /// @sa https://json.nlohmann.me/api/basic_json/is_discarded/
21439 constexpr bool is_discarded() const noexcept
21440 {
21441 return m_data.m_type == value_t::discarded;
21442 }
21443
21444 /// @brief return the type of the JSON value (implicit)
21445 /// @sa https://json.nlohmann.me/api/basic_json/operator_value_t/
21446 constexpr operator value_t() const noexcept
21447 {
21448 return m_data.m_type;
21449 }
21450
21451 /// @}
21452
21453 private:
21454 //////////////////
21455 // value access //
21456 //////////////////
21457
21458 /// get a boolean (explicit)
21459 boolean_t get_impl(boolean_t* /*unused*/) const
21460 {
21461 if (JSON_HEDLEY_LIKELY(is_boolean()))
21462 {
21463 return m_data.m_value.boolean;
21464 }
21465
21466 JSON_THROW(type_error::create(302, detail::concat("type must be boolean, but is ", type_name()), this));
21467 }
21468
21469 /// get a pointer to the value (object)
21470 object_t* get_impl_ptr(object_t* /*unused*/) noexcept
21471 {
21472 return is_object() ? m_data.m_value.object : nullptr;
21473 }
21474
21475 /// get a pointer to the value (object)
21476 constexpr const object_t* get_impl_ptr(const object_t* /*unused*/) const noexcept
21477 {
21478 return is_object() ? m_data.m_value.object : nullptr;
21479 }
21480
21481 /// get a pointer to the value (array)
21482 array_t* get_impl_ptr(array_t* /*unused*/) noexcept
21483 {
21484 return is_array() ? m_data.m_value.array : nullptr;
21485 }
21486
21487 /// get a pointer to the value (array)
21488 constexpr const array_t* get_impl_ptr(const array_t* /*unused*/) const noexcept
21489 {
21490 return is_array() ? m_data.m_value.array : nullptr;
21491 }
21492
21493 /// get a pointer to the value (string)
21494 string_t* get_impl_ptr(string_t* /*unused*/) noexcept
21495 {
21496 return is_string() ? m_data.m_value.string : nullptr;
21497 }
21498
21499 /// get a pointer to the value (string)
21500 constexpr const string_t* get_impl_ptr(const string_t* /*unused*/) const noexcept
21501 {
21502 return is_string() ? m_data.m_value.string : nullptr;
21503 }
21504
21505 /// get a pointer to the value (boolean)
21506 boolean_t* get_impl_ptr(boolean_t* /*unused*/) noexcept
21507 {
21508 return is_boolean() ? &m_data.m_value.boolean : nullptr;
21509 }
21510
21511 /// get a pointer to the value (boolean)
21512 constexpr const boolean_t* get_impl_ptr(const boolean_t* /*unused*/) const noexcept
21513 {
21514 return is_boolean() ? &m_data.m_value.boolean : nullptr;
21515 }
21516
21517 /// get a pointer to the value (integer number)
21518 number_integer_t* get_impl_ptr(number_integer_t* /*unused*/) noexcept
21519 {
21520 return m_data.m_type == value_t::number_integer ? &m_data.m_value.number_integer : nullptr;
21521 }
21522
21523 /// get a pointer to the value (integer number)
21524 constexpr const number_integer_t* get_impl_ptr(const number_integer_t* /*unused*/) const noexcept
21525 {
21526 return m_data.m_type == value_t::number_integer ? &m_data.m_value.number_integer : nullptr;
21527 }
21528
21529 /// get a pointer to the value (unsigned number)
21530 number_unsigned_t* get_impl_ptr(number_unsigned_t* /*unused*/) noexcept
21531 {
21532 return is_number_unsigned() ? &m_data.m_value.number_unsigned : nullptr;
21533 }
21534
21535 /// get a pointer to the value (unsigned number)
21536 constexpr const number_unsigned_t* get_impl_ptr(const number_unsigned_t* /*unused*/) const noexcept
21537 {
21538 return is_number_unsigned() ? &m_data.m_value.number_unsigned : nullptr;
21539 }
21540
21541 /// get a pointer to the value (floating-point number)
21542 number_float_t* get_impl_ptr(number_float_t* /*unused*/) noexcept
21543 {
21544 return is_number_float() ? &m_data.m_value.number_float : nullptr;
21545 }
21546
21547 /// get a pointer to the value (floating-point number)
21548 constexpr const number_float_t* get_impl_ptr(const number_float_t* /*unused*/) const noexcept
21549 {
21550 return is_number_float() ? &m_data.m_value.number_float : nullptr;
21551 }
21552
21553 /// get a pointer to the value (binary)
21554 binary_t* get_impl_ptr(binary_t* /*unused*/) noexcept
21555 {
21556 return is_binary() ? m_data.m_value.binary : nullptr;
21557 }
21558
21559 /// get a pointer to the value (binary)
21560 constexpr const binary_t* get_impl_ptr(const binary_t* /*unused*/) const noexcept
21561 {
21562 return is_binary() ? m_data.m_value.binary : nullptr;
21563 }
21564
21565 /*!
21566 @brief helper function to implement get_ref()
21567
21568 This function helps to implement get_ref() without code duplication for
21569 const and non-const overloads
21570
21571 @tparam ThisType will be deduced as `basic_json` or `const basic_json`
21572
21573 @throw type_error.303 if ReferenceType does not match underlying value
21574 type of the current JSON
21575 */
21576 template<typename ReferenceType, typename ThisType>
21577 static ReferenceType get_ref_impl(ThisType& obj)
21578 {
21579 // delegate the call to get_ptr<>()
21580 auto* ptr = obj.template get_ptr<typename std::add_pointer<ReferenceType>::type>();
21581
21582 if (JSON_HEDLEY_LIKELY(ptr != nullptr))
21583 {
21584 return *ptr;
21585 }
21586
21587 JSON_THROW(type_error::create(303, detail::concat("incompatible ReferenceType for get_ref, actual type is ", obj.type_name()), &obj));
21588 }
21589
21590 public:
21591 /// @name value access
21592 /// Direct access to the stored value of a JSON value.
21593 /// @{
21594
21595 /// @brief get a pointer value (implicit)
21596 /// @sa https://json.nlohmann.me/api/basic_json/get_ptr/
21597 template<typename PointerType, typename std::enable_if<
21598 std::is_pointer<PointerType>::value, int>::type = 0>
21599 auto get_ptr() noexcept -> decltype(std::declval<basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
21600 {
21601 // delegate the call to get_impl_ptr<>()
21602 return get_impl_ptr(static_cast<PointerType>(nullptr));
21603 }
21604
21605 /// @brief get a pointer value (implicit)
21606 /// @sa https://json.nlohmann.me/api/basic_json/get_ptr/
21607 template < typename PointerType, typename std::enable_if <
21608 std::is_pointer<PointerType>::value&&
21609 std::is_const<typename std::remove_pointer<PointerType>::type>::value, int >::type = 0 >
21610 constexpr auto get_ptr() const noexcept -> decltype(std::declval<const basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
21611 {
21612 // delegate the call to get_impl_ptr<>() const
21613 return get_impl_ptr(static_cast<PointerType>(nullptr));
21614 }
21615
21616 private:
21617 /*!
21618 @brief get a value (explicit)
21619
21620 Explicit type conversion between the JSON value and a compatible value
21621 which is [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
21622 and [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
21623 The value is converted by calling the @ref json_serializer<ValueType>
21624 `from_json()` method.
21625
21626 The function is equivalent to executing
21627 @code {.cpp}
21628 ValueType ret;
21629 JSONSerializer<ValueType>::from_json(*this, ret);
21630 return ret;
21631 @endcode
21632
21633 This overloads is chosen if:
21634 - @a ValueType is not @ref basic_json,
21635 - @ref json_serializer<ValueType> has a `from_json()` method of the form
21636 `void from_json(const basic_json&, ValueType&)`, and
21637 - @ref json_serializer<ValueType> does not have a `from_json()` method of
21638 the form `ValueType from_json(const basic_json&)`
21639
21640 @tparam ValueType the returned value type
21641
21642 @return copy of the JSON value, converted to @a ValueType
21643
21644 @throw what @ref json_serializer<ValueType> `from_json()` method throws
21645
21646 @liveexample{The example below shows several conversions from JSON values
21647 to other types. There a few things to note: (1) Floating-point numbers can
21648 be converted to integers\, (2) A JSON array can be converted to a standard
21649 `std::vector<short>`\, (3) A JSON object can be converted to C++
21650 associative containers such as `std::unordered_map<std::string\,
21651 json>`.,get__ValueType_const}
21652
21653 @since version 2.1.0
21654 */
21655 template < typename ValueType,
21656 detail::enable_if_t <
21657 detail::is_default_constructible<ValueType>::value&&
21658 detail::has_from_json<basic_json_t, ValueType>::value,
21659 int > = 0 >
21660 ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept(
21661 JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), std::declval<ValueType&>())))
21662 {
21663 auto ret = ValueType();
21664 JSONSerializer<ValueType>::from_json(*this, ret);
21665 return ret;
21666 }
21667
21668 /*!
21669 @brief get a value (explicit); special case
21670
21671 Explicit type conversion between the JSON value and a compatible value
21672 which is **not** [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
21673 and **not** [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
21674 The value is converted by calling the @ref json_serializer<ValueType>
21675 `from_json()` method.
21676
21677 The function is equivalent to executing
21678 @code {.cpp}
21679 return JSONSerializer<ValueType>::from_json(*this);
21680 @endcode
21681
21682 This overloads is chosen if:
21683 - @a ValueType is not @ref basic_json and
21684 - @ref json_serializer<ValueType> has a `from_json()` method of the form
21685 `ValueType from_json(const basic_json&)`
21686
21687 @note If @ref json_serializer<ValueType> has both overloads of
21688 `from_json()`, this one is chosen.
21689
21690 @tparam ValueType the returned value type
21691
21692 @return copy of the JSON value, converted to @a ValueType
21693
21694 @throw what @ref json_serializer<ValueType> `from_json()` method throws
21695
21696 @since version 2.1.0
21697 */
21698 template < typename ValueType,
21699 detail::enable_if_t <
21700 detail::has_non_default_from_json<basic_json_t, ValueType>::value,
21701 int > = 0 >
21702 ValueType get_impl(detail::priority_tag<1> /*unused*/) const noexcept(noexcept(
21703 JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>())))
21704 {
21705 return JSONSerializer<ValueType>::from_json(*this);
21706 }
21707
21708 /*!
21709 @brief get special-case overload
21710
21711 This overloads converts the current @ref basic_json in a different
21712 @ref basic_json type
21713
21714 @tparam BasicJsonType == @ref basic_json
21715
21716 @return a copy of *this, converted into @a BasicJsonType
21717
21718 @complexity Depending on the implementation of the called `from_json()`
21719 method.
21720
21721 @since version 3.2.0
21722 */
21723 template < typename BasicJsonType,
21724 detail::enable_if_t <
21725 detail::is_basic_json<BasicJsonType>::value,
21726 int > = 0 >
21727 BasicJsonType get_impl(detail::priority_tag<2> /*unused*/) const
21728 {
21729 return *this;
21730 }
21731
21732 /*!
21733 @brief get special-case overload
21734
21735 This overloads avoids a lot of template boilerplate, it can be seen as the
21736 identity method
21737
21738 @tparam BasicJsonType == @ref basic_json
21739
21740 @return a copy of *this
21741
21742 @complexity Constant.
21743
21744 @since version 2.1.0
21745 */
21746 template<typename BasicJsonType,
21747 detail::enable_if_t<
21748 std::is_same<BasicJsonType, basic_json_t>::value,
21749 int> = 0>
21750 basic_json get_impl(detail::priority_tag<3> /*unused*/) const
21751 {
21752 return *this;
21753 }
21754
21755 /*!
21756 @brief get a pointer value (explicit)
21757 @copydoc get()
21758 */
21759 template<typename PointerType,
21760 detail::enable_if_t<
21761 std::is_pointer<PointerType>::value,
21762 int> = 0>
21763 constexpr auto get_impl(detail::priority_tag<4> /*unused*/) const noexcept
21764 -> decltype(std::declval<const basic_json_t&>().template get_ptr<PointerType>())
21765 {
21766 // delegate the call to get_ptr
21767 return get_ptr<PointerType>();
21768 }
21769
21770 public:
21771 /*!
21772 @brief get a (pointer) value (explicit)
21773
21774 Performs explicit type conversion between the JSON value and a compatible value if required.
21775
21776 - If the requested type is a pointer to the internally stored JSON value that pointer is returned.
21777 No copies are made.
21778
21779 - If the requested type is the current @ref basic_json, or a different @ref basic_json convertible
21780 from the current @ref basic_json.
21781
21782 - Otherwise the value is converted by calling the @ref json_serializer<ValueType> `from_json()`
21783 method.
21784
21785 @tparam ValueTypeCV the provided value type
21786 @tparam ValueType the returned value type
21787
21788 @return copy of the JSON value, converted to @tparam ValueType if necessary
21789
21790 @throw what @ref json_serializer<ValueType> `from_json()` method throws if conversion is required
21791
21792 @since version 2.1.0
21793 */
21794 template < typename ValueTypeCV, typename ValueType = detail::uncvref_t<ValueTypeCV>>
21795 #if defined(JSON_HAS_CPP_14)
21796 constexpr
21797 #endif
21798 auto get() const noexcept(
21799 noexcept(std::declval<const basic_json_t&>().template get_impl<ValueType>(detail::priority_tag<4> {})))
21800 -> decltype(std::declval<const basic_json_t&>().template get_impl<ValueType>(detail::priority_tag<4> {}))
21801 {
21802 // we cannot static_assert on ValueTypeCV being non-const, because
21803 // there is support for get<const basic_json_t>(), which is why we
21804 // still need the uncvref
21805 static_assert(!std::is_reference<ValueTypeCV>::value,
21806 "get() cannot be used with reference types, you might want to use get_ref()");
21807 return get_impl<ValueType>(detail::priority_tag<4> {});
21808 }
21809
21810 /*!
21811 @brief get a pointer value (explicit)
21812
21813 Explicit pointer access to the internally stored JSON value. No copies are
21814 made.
21815
21816 @warning The pointer becomes invalid if the underlying JSON object
21817 changes.
21818
21819 @tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
21820 object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
21821 @ref number_unsigned_t, or @ref number_float_t.
21822
21823 @return pointer to the internally stored JSON value if the requested
21824 pointer type @a PointerType fits to the JSON value; `nullptr` otherwise
21825
21826 @complexity Constant.
21827
21828 @liveexample{The example below shows how pointers to internal values of a
21829 JSON value can be requested. Note that no type conversions are made and a
21830 `nullptr` is returned if the value and the requested pointer type does not
21831 match.,get__PointerType}
21832
21833 @sa see @ref get_ptr() for explicit pointer-member access
21834
21835 @since version 1.0.0
21836 */
21837 template<typename PointerType, typename std::enable_if<
21838 std::is_pointer<PointerType>::value, int>::type = 0>
21839 auto get() noexcept -> decltype(std::declval<basic_json_t&>().template get_ptr<PointerType>())
21840 {
21841 // delegate the call to get_ptr
21842 return get_ptr<PointerType>();
21843 }
21844
21845 /// @brief get a value (explicit)
21846 /// @sa https://json.nlohmann.me/api/basic_json/get_to/
21847 template < typename ValueType,
21848 detail::enable_if_t <
21849 !detail::is_basic_json<ValueType>::value&&
21850 detail::has_from_json<basic_json_t, ValueType>::value,
21851 int > = 0 >
21852 ValueType & get_to(ValueType& v) const noexcept(noexcept(
21853 JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), v)))
21854 {
21855 JSONSerializer<ValueType>::from_json(*this, v);
21856 return v;
21857 }
21858
21859 // specialization to allow calling get_to with a basic_json value
21860 // see https://github.com/nlohmann/json/issues/2175
21861 template<typename ValueType,
21862 detail::enable_if_t <
21863 detail::is_basic_json<ValueType>::value,
21864 int> = 0>
21865 ValueType & get_to(ValueType& v) const
21866 {
21867 v = *this;
21868 return v;
21869 }
21870
21871 template <
21872 typename T, std::size_t N,
21873 typename Array = T (&)[N], // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
21874 detail::enable_if_t <
21875 detail::has_from_json<basic_json_t, Array>::value, int > = 0 >
21876 Array get_to(T (&v)[N]) const // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
21877 noexcept(noexcept(JSONSerializer<Array>::from_json(
21878 std::declval<const basic_json_t&>(), v)))
21879 {
21880 JSONSerializer<Array>::from_json(*this, v);
21881 return v;
21882 }
21883
21884 /// @brief get a reference value (implicit)
21885 /// @sa https://json.nlohmann.me/api/basic_json/get_ref/
21886 template<typename ReferenceType, typename std::enable_if<
21887 std::is_reference<ReferenceType>::value, int>::type = 0>
21888 ReferenceType get_ref()
21889 {
21890 // delegate call to get_ref_impl
21891 return get_ref_impl<ReferenceType>(*this);
21892 }
21893
21894 /// @brief get a reference value (implicit)
21895 /// @sa https://json.nlohmann.me/api/basic_json/get_ref/
21896 template < typename ReferenceType, typename std::enable_if <
21897 std::is_reference<ReferenceType>::value&&
21898 std::is_const<typename std::remove_reference<ReferenceType>::type>::value, int >::type = 0 >
21899 ReferenceType get_ref() const
21900 {
21901 // delegate call to get_ref_impl
21902 return get_ref_impl<ReferenceType>(*this);
21903 }
21904
21905 /*!
21906 @brief get a value (implicit)
21907
21908 Implicit type conversion between the JSON value and a compatible value.
21909 The call is realized by calling @ref get() const.
21910
21911 @tparam ValueType non-pointer type compatible to the JSON value, for
21912 instance `int` for JSON integer numbers, `bool` for JSON booleans, or
21913 `std::vector` types for JSON arrays. The character type of @ref string_t
21914 as well as an initializer list of this type is excluded to avoid
21915 ambiguities as these types implicitly convert to `std::string`.
21916
21917 @return copy of the JSON value, converted to type @a ValueType
21918
21919 @throw type_error.302 in case passed type @a ValueType is incompatible
21920 to the JSON value type (e.g., the JSON value is of type boolean, but a
21921 string is requested); see example below
21922
21923 @complexity Linear in the size of the JSON value.
21924
21925 @liveexample{The example below shows several conversions from JSON values
21926 to other types. There a few things to note: (1) Floating-point numbers can
21927 be converted to integers\, (2) A JSON array can be converted to a standard
21928 `std::vector<short>`\, (3) A JSON object can be converted to C++
21929 associative containers such as `std::unordered_map<std::string\,
21930 json>`.,operator__ValueType}
21931
21932 @since version 1.0.0
21933 */
21934 template < typename ValueType, typename std::enable_if <
21935 detail::conjunction <
21936 detail::negation<std::is_pointer<ValueType>>,
21937 detail::negation<std::is_same<ValueType, std::nullptr_t>>,
21938 detail::negation<std::is_same<ValueType, detail::json_ref<basic_json>>>,
21939 detail::negation<std::is_same<ValueType, typename string_t::value_type>>,
21940 detail::negation<detail::is_basic_json<ValueType>>,
21941 detail::negation<std::is_same<ValueType, std::initializer_list<typename string_t::value_type>>>,
21942 #if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER <= 1914))
21943 detail::negation<std::is_same<ValueType, std::string_view>>,
21944 #endif
21945 #if defined(JSON_HAS_CPP_17) && JSON_HAS_STATIC_RTTI
21946 detail::negation<std::is_same<ValueType, std::any>>,
21947 #endif
21948 detail::is_detected_lazy<detail::get_template_function, const basic_json_t&, ValueType>
21949 >::value, int >::type = 0 >
21950 JSON_EXPLICIT operator ValueType() const
21951 {
21952 // delegate the call to get<>() const
21953 return get<ValueType>();
21954 }
21955
21956 /// @brief get a binary value
21957 /// @sa https://json.nlohmann.me/api/basic_json/get_binary/
21958 binary_t& get_binary()
21959 {
21960 if (!is_binary())
21961 {
21962 JSON_THROW(type_error::create(302, detail::concat("type must be binary, but is ", type_name()), this));
21963 }
21964
21965 return *get_ptr<binary_t*>();
21966 }
21967
21968 /// @brief get a binary value
21969 /// @sa https://json.nlohmann.me/api/basic_json/get_binary/
21970 const binary_t& get_binary() const
21971 {
21972 if (!is_binary())
21973 {
21974 JSON_THROW(type_error::create(302, detail::concat("type must be binary, but is ", type_name()), this));
21975 }
21976
21977 return *get_ptr<const binary_t*>();
21978 }
21979
21980 /// @}
21981
21982 ////////////////////
21983 // element access //
21984 ////////////////////
21985
21986 /// @name element access
21987 /// Access to the JSON value.
21988 /// @{
21989
21990 /// @brief access specified array element with bounds checking
21991 /// @sa https://json.nlohmann.me/api/basic_json/at/
21992 reference at(size_type idx)
21993 {
21994 // at only works for arrays
21995 if (JSON_HEDLEY_LIKELY(is_array()))
21996 {
21997 JSON_TRY
21998 {
21999 return set_parent(m_data.m_value.array->at(idx));
22000 }
22001 JSON_CATCH (std::out_of_range&)
22002 {
22003 // create better exception explanation
22004 JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
22005 } // cppcheck-suppress[missingReturn]
22006 }
22007 else
22008 {
22009 JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
22010 }
22011 }
22012
22013 /// @brief access specified array element with bounds checking
22014 /// @sa https://json.nlohmann.me/api/basic_json/at/
22015 const_reference at(size_type idx) const
22016 {
22017 // at only works for arrays
22018 if (JSON_HEDLEY_LIKELY(is_array()))
22019 {
22020 JSON_TRY
22021 {
22022 return m_data.m_value.array->at(idx);
22023 }
22024 JSON_CATCH (std::out_of_range&)
22025 {
22026 // create better exception explanation
22027 JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
22028 } // cppcheck-suppress[missingReturn]
22029 }
22030 else
22031 {
22032 JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
22033 }
22034 }
22035
22036 /// @brief access specified object element with bounds checking
22037 /// @sa https://json.nlohmann.me/api/basic_json/at/
22038 reference at(const typename object_t::key_type& key)
22039 {
22040 // at only works for objects
22041 if (JSON_HEDLEY_UNLIKELY(!is_object()))
22042 {
22043 JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
22044 }
22045
22046 auto it = m_data.m_value.object->find(key);
22047 if (it == m_data.m_value.object->end())
22048 {
22049 JSON_THROW(out_of_range::create(403, detail::concat("key '", key, "' not found"), this));
22050 }
22051 return set_parent(it->second);
22052 }
22053
22054 /// @brief access specified object element with bounds checking
22055 /// @sa https://json.nlohmann.me/api/basic_json/at/
22056 template<class KeyType, detail::enable_if_t<
22057 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
22058 reference at(KeyType && key)
22059 {
22060 // at only works for objects
22061 if (JSON_HEDLEY_UNLIKELY(!is_object()))
22062 {
22063 JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
22064 }
22065
22066 auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
22067 if (it == m_data.m_value.object->end())
22068 {
22069 JSON_THROW(out_of_range::create(403, detail::concat("key '", string_t(std::forward<KeyType>(key)), "' not found"), this));
22070 }
22071 return set_parent(it->second);
22072 }
22073
22074 /// @brief access specified object element with bounds checking
22075 /// @sa https://json.nlohmann.me/api/basic_json/at/
22076 const_reference at(const typename object_t::key_type& key) const
22077 {
22078 // at only works for objects
22079 if (JSON_HEDLEY_UNLIKELY(!is_object()))
22080 {
22081 JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
22082 }
22083
22084 auto it = m_data.m_value.object->find(key);
22085 if (it == m_data.m_value.object->end())
22086 {
22087 JSON_THROW(out_of_range::create(403, detail::concat("key '", key, "' not found"), this));
22088 }
22089 return it->second;
22090 }
22091
22092 /// @brief access specified object element with bounds checking
22093 /// @sa https://json.nlohmann.me/api/basic_json/at/
22094 template<class KeyType, detail::enable_if_t<
22095 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
22096 const_reference at(KeyType && key) const
22097 {
22098 // at only works for objects
22099 if (JSON_HEDLEY_UNLIKELY(!is_object()))
22100 {
22101 JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
22102 }
22103
22104 auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
22105 if (it == m_data.m_value.object->end())
22106 {
22107 JSON_THROW(out_of_range::create(403, detail::concat("key '", string_t(std::forward<KeyType>(key)), "' not found"), this));
22108 }
22109 return it->second;
22110 }
22111
22112 /// @brief access specified array element
22113 /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
22114 reference operator[](size_type idx)
22115 {
22116 // implicitly convert null value to an empty array
22117 if (is_null())
22118 {
22119 m_data.m_type = value_t::array;
22120 m_data.m_value.array = create<array_t>();
22121 assert_invariant();
22122 }
22123
22124 // operator[] only works for arrays
22125 if (JSON_HEDLEY_LIKELY(is_array()))
22126 {
22127 // fill up array with null values if given idx is outside range
22128 if (idx >= m_data.m_value.array->size())
22129 {
22130 #if JSON_DIAGNOSTICS
22131 // remember array size & capacity before resizing
22132 const auto old_size = m_data.m_value.array->size();
22133 const auto old_capacity = m_data.m_value.array->capacity();
22134 #endif
22135 m_data.m_value.array->resize(idx + 1);
22136
22137 #if JSON_DIAGNOSTICS
22138 if (JSON_HEDLEY_UNLIKELY(m_data.m_value.array->capacity() != old_capacity))
22139 {
22140 // capacity has changed: update all parents
22141 set_parents();
22142 }
22143 else
22144 {
22145 // set parent for values added above
22146 set_parents(begin() + static_cast<typename iterator::difference_type>(old_size), static_cast<typename iterator::difference_type>(idx + 1 - old_size));
22147 }
22148 #endif
22149 assert_invariant();
22150 }
22151
22152 return m_data.m_value.array->operator[](idx);
22153 }
22154
22155 JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a numeric argument with ", type_name()), this));
22156 }
22157
22158 /// @brief access specified array element
22159 /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
22160 const_reference operator[](size_type idx) const
22161 {
22162 // const operator[] only works for arrays
22163 if (JSON_HEDLEY_LIKELY(is_array()))
22164 {
22165 return m_data.m_value.array->operator[](idx);
22166 }
22167
22168 JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a numeric argument with ", type_name()), this));
22169 }
22170
22171 /// @brief access specified object element
22172 /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
22173 reference operator[](typename object_t::key_type key) // NOLINT(performance-unnecessary-value-param)
22174 {
22175 // implicitly convert null value to an empty object
22176 if (is_null())
22177 {
22178 m_data.m_type = value_t::object;
22179 m_data.m_value.object = create<object_t>();
22180 assert_invariant();
22181 }
22182
22183 // operator[] only works for objects
22184 if (JSON_HEDLEY_LIKELY(is_object()))
22185 {
22186 auto result = m_data.m_value.object->emplace(std::move(key), nullptr);
22187 return set_parent(result.first->second);
22188 }
22189
22190 JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
22191 }
22192
22193 /// @brief access specified object element
22194 /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
22195 const_reference operator[](const typename object_t::key_type& key) const
22196 {
22197 // const operator[] only works for objects
22198 if (JSON_HEDLEY_LIKELY(is_object()))
22199 {
22200 auto it = m_data.m_value.object->find(key);
22201 JSON_ASSERT(it != m_data.m_value.object->end());
22202 return it->second;
22203 }
22204
22205 JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
22206 }
22207
22208 // these two functions resolve a (const) char * ambiguity affecting Clang and MSVC
22209 // (they seemingly cannot be constrained to resolve the ambiguity)
22210 template<typename T>
22211 reference operator[](T* key)
22212 {
22213 return operator[](typename object_t::key_type(key));
22214 }
22215
22216 template<typename T>
22217 const_reference operator[](T* key) const
22218 {
22219 return operator[](typename object_t::key_type(key));
22220 }
22221
22222 /// @brief access specified object element
22223 /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
22224 template<class KeyType, detail::enable_if_t<
22225 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int > = 0 >
22226 reference operator[](KeyType && key)
22227 {
22228 // implicitly convert null value to an empty object
22229 if (is_null())
22230 {
22231 m_data.m_type = value_t::object;
22232 m_data.m_value.object = create<object_t>();
22233 assert_invariant();
22234 }
22235
22236 // operator[] only works for objects
22237 if (JSON_HEDLEY_LIKELY(is_object()))
22238 {
22239 auto result = m_data.m_value.object->emplace(std::forward<KeyType>(key), nullptr);
22240 return set_parent(result.first->second);
22241 }
22242
22243 JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
22244 }
22245
22246 /// @brief access specified object element
22247 /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
22248 template<class KeyType, detail::enable_if_t<
22249 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int > = 0 >
22250 const_reference operator[](KeyType && key) const
22251 {
22252 // const operator[] only works for objects
22253 if (JSON_HEDLEY_LIKELY(is_object()))
22254 {
22255 auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
22256 JSON_ASSERT(it != m_data.m_value.object->end());
22257 return it->second;
22258 }
22259
22260 JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
22261 }
22262
22263 private:
22264 template<typename KeyType>
22265 using is_comparable_with_object_key = detail::is_comparable <
22266 object_comparator_t, const typename object_t::key_type&, KeyType >;
22267
22268 template<typename ValueType>
22269 using value_return_type = std::conditional <
22270 detail::is_c_string_uncvref<ValueType>::value,
22271 string_t, typename std::decay<ValueType>::type >;
22272
22273 public:
22274 /// @brief access specified object element with default value
22275 /// @sa https://json.nlohmann.me/api/basic_json/value/
22276 template < class ValueType, detail::enable_if_t <
22277 !detail::is_transparent<object_comparator_t>::value
22278 && detail::is_getable<basic_json_t, ValueType>::value
22279 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22280 ValueType value(const typename object_t::key_type& key, const ValueType& default_value) const
22281 {
22282 // value only works for objects
22283 if (JSON_HEDLEY_LIKELY(is_object()))
22284 {
22285 // if key is found, return value and given default value otherwise
22286 const auto it = find(key);
22287 if (it != end())
22288 {
22289 return it->template get<ValueType>();
22290 }
22291
22292 return default_value;
22293 }
22294
22295 JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
22296 }
22297
22298 /// @brief access specified object element with default value
22299 /// @sa https://json.nlohmann.me/api/basic_json/value/
22300 template < class ValueType, class ReturnType = typename value_return_type<ValueType>::type,
22301 detail::enable_if_t <
22302 !detail::is_transparent<object_comparator_t>::value
22303 && detail::is_getable<basic_json_t, ReturnType>::value
22304 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22305 ReturnType value(const typename object_t::key_type& key, ValueType && default_value) const
22306 {
22307 // value only works for objects
22308 if (JSON_HEDLEY_LIKELY(is_object()))
22309 {
22310 // if key is found, return value and given default value otherwise
22311 const auto it = find(key);
22312 if (it != end())
22313 {
22314 return it->template get<ReturnType>();
22315 }
22316
22317 return std::forward<ValueType>(default_value);
22318 }
22319
22320 JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
22321 }
22322
22323 /// @brief access specified object element with default value
22324 /// @sa https://json.nlohmann.me/api/basic_json/value/
22325 template < class ValueType, class KeyType, detail::enable_if_t <
22326 detail::is_transparent<object_comparator_t>::value
22327 && !detail::is_json_pointer<KeyType>::value
22328 && is_comparable_with_object_key<KeyType>::value
22329 && detail::is_getable<basic_json_t, ValueType>::value
22330 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22331 ValueType value(KeyType && key, const ValueType& default_value) const
22332 {
22333 // value only works for objects
22334 if (JSON_HEDLEY_LIKELY(is_object()))
22335 {
22336 // if key is found, return value and given default value otherwise
22337 const auto it = find(std::forward<KeyType>(key));
22338 if (it != end())
22339 {
22340 return it->template get<ValueType>();
22341 }
22342
22343 return default_value;
22344 }
22345
22346 JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
22347 }
22348
22349 /// @brief access specified object element via JSON Pointer with default value
22350 /// @sa https://json.nlohmann.me/api/basic_json/value/
22351 template < class ValueType, class KeyType, class ReturnType = typename value_return_type<ValueType>::type,
22352 detail::enable_if_t <
22353 detail::is_transparent<object_comparator_t>::value
22354 && !detail::is_json_pointer<KeyType>::value
22355 && is_comparable_with_object_key<KeyType>::value
22356 && detail::is_getable<basic_json_t, ReturnType>::value
22357 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22358 ReturnType value(KeyType && key, ValueType && default_value) const
22359 {
22360 // value only works for objects
22361 if (JSON_HEDLEY_LIKELY(is_object()))
22362 {
22363 // if key is found, return value and given default value otherwise
22364 const auto it = find(std::forward<KeyType>(key));
22365 if (it != end())
22366 {
22367 return it->template get<ReturnType>();
22368 }
22369
22370 return std::forward<ValueType>(default_value);
22371 }
22372
22373 JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
22374 }
22375
22376 /// @brief access specified object element via JSON Pointer with default value
22377 /// @sa https://json.nlohmann.me/api/basic_json/value/
22378 template < class ValueType, detail::enable_if_t <
22379 detail::is_getable<basic_json_t, ValueType>::value
22380 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22381 ValueType value(const json_pointer& ptr, const ValueType& default_value) const
22382 {
22383 // value only works for objects
22384 if (JSON_HEDLEY_LIKELY(is_object()))
22385 {
22386 // if pointer resolves a value, return it or use default value
22387 JSON_TRY
22388 {
22389 return ptr.get_checked(this).template get<ValueType>();
22390 }
22391 JSON_INTERNAL_CATCH (out_of_range&)
22392 {
22393 return default_value;
22394 }
22395 }
22396
22397 JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
22398 }
22399
22400 /// @brief access specified object element via JSON Pointer with default value
22401 /// @sa https://json.nlohmann.me/api/basic_json/value/
22402 template < class ValueType, class ReturnType = typename value_return_type<ValueType>::type,
22403 detail::enable_if_t <
22404 detail::is_getable<basic_json_t, ReturnType>::value
22405 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22406 ReturnType value(const json_pointer& ptr, ValueType && default_value) const
22407 {
22408 // value only works for objects
22409 if (JSON_HEDLEY_LIKELY(is_object()))
22410 {
22411 // if pointer resolves a value, return it or use default value
22412 JSON_TRY
22413 {
22414 return ptr.get_checked(this).template get<ReturnType>();
22415 }
22416 JSON_INTERNAL_CATCH (out_of_range&)
22417 {
22418 return std::forward<ValueType>(default_value);
22419 }
22420 }
22421
22422 JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
22423 }
22424
22425 template < class ValueType, class BasicJsonType, detail::enable_if_t <
22426 detail::is_basic_json<BasicJsonType>::value
22427 && detail::is_getable<basic_json_t, ValueType>::value
22428 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22429 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
22430 ValueType value(const ::nlohmann::json_pointer<BasicJsonType>& ptr, const ValueType& default_value) const
22431 {
22432 return value(ptr.convert(), default_value);
22433 }
22434
22435 template < class ValueType, class BasicJsonType, class ReturnType = typename value_return_type<ValueType>::type,
22436 detail::enable_if_t <
22437 detail::is_basic_json<BasicJsonType>::value
22438 && detail::is_getable<basic_json_t, ReturnType>::value
22439 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22440 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
22441 ReturnType value(const ::nlohmann::json_pointer<BasicJsonType>& ptr, ValueType && default_value) const
22442 {
22443 return value(ptr.convert(), std::forward<ValueType>(default_value));
22444 }
22445
22446 /// @brief access the first element
22447 /// @sa https://json.nlohmann.me/api/basic_json/front/
22448 reference front()
22449 {
22450 return *begin();
22451 }
22452
22453 /// @brief access the first element
22454 /// @sa https://json.nlohmann.me/api/basic_json/front/
22455 const_reference front() const
22456 {
22457 return *cbegin();
22458 }
22459
22460 /// @brief access the last element
22461 /// @sa https://json.nlohmann.me/api/basic_json/back/
22462 reference back()
22463 {
22464 auto tmp = end();
22465 --tmp;
22466 return *tmp;
22467 }
22468
22469 /// @brief access the last element
22470 /// @sa https://json.nlohmann.me/api/basic_json/back/
22471 const_reference back() const
22472 {
22473 auto tmp = cend();
22474 --tmp;
22475 return *tmp;
22476 }
22477
22478 /// @brief remove element given an iterator
22479 /// @sa https://json.nlohmann.me/api/basic_json/erase/
22480 template < class IteratorType, detail::enable_if_t <
22481 std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
22482 std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int > = 0 >
22483 IteratorType erase(IteratorType pos) // NOLINT(performance-unnecessary-value-param)
22484 {
22485 // make sure iterator fits the current value
22486 if (JSON_HEDLEY_UNLIKELY(this != pos.m_object))
22487 {
22488 JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
22489 }
22490
22491 IteratorType result = end();
22492
22493 switch (m_data.m_type)
22494 {
22495 case value_t::boolean:
22496 case value_t::number_float:
22497 case value_t::number_integer:
22498 case value_t::number_unsigned:
22499 case value_t::string:
22500 case value_t::binary:
22501 {
22502 if (JSON_HEDLEY_UNLIKELY(!pos.m_it.primitive_iterator.is_begin()))
22503 {
22504 JSON_THROW(invalid_iterator::create(205, "iterator out of range", this));
22505 }
22506
22507 if (is_string())
22508 {
22509 AllocatorType<string_t> alloc;
22510 std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.string);
22511 std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.string, 1);
22512 m_data.m_value.string = nullptr;
22513 }
22514 else if (is_binary())
22515 {
22516 AllocatorType<binary_t> alloc;
22517 std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.binary);
22518 std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.binary, 1);
22519 m_data.m_value.binary = nullptr;
22520 }
22521
22522 m_data.m_type = value_t::null;
22523 assert_invariant();
22524 break;
22525 }
22526
22527 case value_t::object:
22528 {
22529 result.m_it.object_iterator = m_data.m_value.object->erase(pos.m_it.object_iterator);
22530 break;
22531 }
22532
22533 case value_t::array:
22534 {
22535 result.m_it.array_iterator = m_data.m_value.array->erase(pos.m_it.array_iterator);
22536 break;
22537 }
22538
22539 case value_t::null:
22540 case value_t::discarded:
22541 default:
22542 JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
22543 }
22544
22545 return result;
22546 }
22547
22548 /// @brief remove elements given an iterator range
22549 /// @sa https://json.nlohmann.me/api/basic_json/erase/
22550 template < class IteratorType, detail::enable_if_t <
22551 std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
22552 std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int > = 0 >
22553 IteratorType erase(IteratorType first, IteratorType last) // NOLINT(performance-unnecessary-value-param)
22554 {
22555 // make sure iterator fits the current value
22556 if (JSON_HEDLEY_UNLIKELY(this != first.m_object || this != last.m_object))
22557 {
22558 JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value", this));
22559 }
22560
22561 IteratorType result = end();
22562
22563 switch (m_data.m_type)
22564 {
22565 case value_t::boolean:
22566 case value_t::number_float:
22567 case value_t::number_integer:
22568 case value_t::number_unsigned:
22569 case value_t::string:
22570 case value_t::binary:
22571 {
22572 if (JSON_HEDLEY_LIKELY(!first.m_it.primitive_iterator.is_begin()
22573 || !last.m_it.primitive_iterator.is_end()))
22574 {
22575 JSON_THROW(invalid_iterator::create(204, "iterators out of range", this));
22576 }
22577
22578 if (is_string())
22579 {
22580 AllocatorType<string_t> alloc;
22581 std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.string);
22582 std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.string, 1);
22583 m_data.m_value.string = nullptr;
22584 }
22585 else if (is_binary())
22586 {
22587 AllocatorType<binary_t> alloc;
22588 std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.binary);
22589 std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.binary, 1);
22590 m_data.m_value.binary = nullptr;
22591 }
22592
22593 m_data.m_type = value_t::null;
22594 assert_invariant();
22595 break;
22596 }
22597
22598 case value_t::object:
22599 {
22600 result.m_it.object_iterator = m_data.m_value.object->erase(first.m_it.object_iterator,
22601 last.m_it.object_iterator);
22602 break;
22603 }
22604
22605 case value_t::array:
22606 {
22607 result.m_it.array_iterator = m_data.m_value.array->erase(first.m_it.array_iterator,
22608 last.m_it.array_iterator);
22609 break;
22610 }
22611
22612 case value_t::null:
22613 case value_t::discarded:
22614 default:
22615 JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
22616 }
22617
22618 return result;
22619 }
22620
22621 private:
22622 template < typename KeyType, detail::enable_if_t <
22623 detail::has_erase_with_key_type<basic_json_t, KeyType>::value, int > = 0 >
22624 size_type erase_internal(KeyType && key)
22625 {
22626 // this erase only works for objects
22627 if (JSON_HEDLEY_UNLIKELY(!is_object()))
22628 {
22629 JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
22630 }
22631
22632 return m_data.m_value.object->erase(std::forward<KeyType>(key));
22633 }
22634
22635 template < typename KeyType, detail::enable_if_t <
22636 !detail::has_erase_with_key_type<basic_json_t, KeyType>::value, int > = 0 >
22637 size_type erase_internal(KeyType && key)
22638 {
22639 // this erase only works for objects
22640 if (JSON_HEDLEY_UNLIKELY(!is_object()))
22641 {
22642 JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
22643 }
22644
22645 const auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
22646 if (it != m_data.m_value.object->end())
22647 {
22648 m_data.m_value.object->erase(it);
22649 return 1;
22650 }
22651 return 0;
22652 }
22653
22654 public:
22655
22656 /// @brief remove element from a JSON object given a key
22657 /// @sa https://json.nlohmann.me/api/basic_json/erase/
22658 size_type erase(const typename object_t::key_type& key)
22659 {
22660 // the indirection via erase_internal() is added to avoid making this
22661 // function a template and thus de-rank it during overload resolution
22662 return erase_internal(key);
22663 }
22664
22665 /// @brief remove element from a JSON object given a key
22666 /// @sa https://json.nlohmann.me/api/basic_json/erase/
22667 template<class KeyType, detail::enable_if_t<
22668 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
22669 size_type erase(KeyType && key)
22670 {
22671 return erase_internal(std::forward<KeyType>(key));
22672 }
22673
22674 /// @brief remove element from a JSON array given an index
22675 /// @sa https://json.nlohmann.me/api/basic_json/erase/
22676 void erase(const size_type idx)
22677 {
22678 // this erase only works for arrays
22679 if (JSON_HEDLEY_LIKELY(is_array()))
22680 {
22681 if (JSON_HEDLEY_UNLIKELY(idx >= size()))
22682 {
22683 JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
22684 }
22685
22686 m_data.m_value.array->erase(m_data.m_value.array->begin() + static_cast<difference_type>(idx));
22687 }
22688 else
22689 {
22690 JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
22691 }
22692 }
22693
22694 /// @}
22695
22696 ////////////
22697 // lookup //
22698 ////////////
22699
22700 /// @name lookup
22701 /// @{
22702
22703 /// @brief find an element in a JSON object
22704 /// @sa https://json.nlohmann.me/api/basic_json/find/
22705 iterator find(const typename object_t::key_type& key)
22706 {
22707 auto result = end();
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 const_iterator find(const typename object_t::key_type& key) const
22720 {
22721 auto result = cend();
22722
22723 if (is_object())
22724 {
22725 result.m_it.object_iterator = m_data.m_value.object->find(key);
22726 }
22727
22728 return result;
22729 }
22730
22731 /// @brief find an element in a JSON object
22732 /// @sa https://json.nlohmann.me/api/basic_json/find/
22733 template<class KeyType, detail::enable_if_t<
22734 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
22735 iterator find(KeyType && key)
22736 {
22737 auto result = end();
22738
22739 if (is_object())
22740 {
22741 result.m_it.object_iterator = m_data.m_value.object->find(std::forward<KeyType>(key));
22742 }
22743
22744 return result;
22745 }
22746
22747 /// @brief find an element in a JSON object
22748 /// @sa https://json.nlohmann.me/api/basic_json/find/
22749 template<class KeyType, detail::enable_if_t<
22750 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
22751 const_iterator find(KeyType && key) const
22752 {
22753 auto result = cend();
22754
22755 if (is_object())
22756 {
22757 result.m_it.object_iterator = m_data.m_value.object->find(std::forward<KeyType>(key));
22758 }
22759
22760 return result;
22761 }
22762
22763 /// @brief returns the number of occurrences of a key in a JSON object
22764 /// @sa https://json.nlohmann.me/api/basic_json/count/
22765 size_type count(const typename object_t::key_type& key) const
22766 {
22767 // return 0 for all nonobject types
22768 return is_object() ? m_data.m_value.object->count(key) : 0;
22769 }
22770
22771 /// @brief returns the number of occurrences of a key in a JSON object
22772 /// @sa https://json.nlohmann.me/api/basic_json/count/
22773 template<class KeyType, detail::enable_if_t<
22774 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
22775 size_type count(KeyType && key) const
22776 {
22777 // return 0 for all nonobject types
22778 return is_object() ? m_data.m_value.object->count(std::forward<KeyType>(key)) : 0;
22779 }
22780
22781 /// @brief check the existence of an element in a JSON object
22782 /// @sa https://json.nlohmann.me/api/basic_json/contains/
22783 bool contains(const typename object_t::key_type& key) const
22784 {
22785 return is_object() && m_data.m_value.object->find(key) != m_data.m_value.object->end();
22786 }
22787
22788 /// @brief check the existence of an element in a JSON object
22789 /// @sa https://json.nlohmann.me/api/basic_json/contains/
22790 template<class KeyType, detail::enable_if_t<
22791 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
22792 bool contains(KeyType && key) const
22793 {
22794 return is_object() && m_data.m_value.object->find(std::forward<KeyType>(key)) != m_data.m_value.object->end();
22795 }
22796
22797 /// @brief check the existence of an element in a JSON object given a JSON pointer
22798 /// @sa https://json.nlohmann.me/api/basic_json/contains/
22799 bool contains(const json_pointer& ptr) const
22800 {
22801 return ptr.contains(this);
22802 }
22803
22804 template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
22805 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
22806 bool contains(const typename ::nlohmann::json_pointer<BasicJsonType>& ptr) const
22807 {
22808 return ptr.contains(this);
22809 }
22810
22811 /// @}
22812
22813 ///////////////
22814 // iterators //
22815 ///////////////
22816
22817 /// @name iterators
22818 /// @{
22819
22820 /// @brief returns an iterator to the first element
22821 /// @sa https://json.nlohmann.me/api/basic_json/begin/
22822 iterator begin() noexcept
22823 {
22824 iterator result(this);
22825 result.set_begin();
22826 return result;
22827 }
22828
22829 /// @brief returns an iterator to the first element
22830 /// @sa https://json.nlohmann.me/api/basic_json/begin/
22831 const_iterator begin() const noexcept
22832 {
22833 return cbegin();
22834 }
22835
22836 /// @brief returns a const iterator to the first element
22837 /// @sa https://json.nlohmann.me/api/basic_json/cbegin/
22838 const_iterator cbegin() const noexcept
22839 {
22840 const_iterator result(this);
22841 result.set_begin();
22842 return result;
22843 }
22844
22845 /// @brief returns an iterator to one past the last element
22846 /// @sa https://json.nlohmann.me/api/basic_json/end/
22847 iterator end() noexcept
22848 {
22849 iterator result(this);
22850 result.set_end();
22851 return result;
22852 }
22853
22854 /// @brief returns an iterator to one past the last element
22855 /// @sa https://json.nlohmann.me/api/basic_json/end/
22856 const_iterator end() const noexcept
22857 {
22858 return cend();
22859 }
22860
22861 /// @brief returns an iterator to one past the last element
22862 /// @sa https://json.nlohmann.me/api/basic_json/cend/
22863 const_iterator cend() const noexcept
22864 {
22865 const_iterator result(this);
22866 result.set_end();
22867 return result;
22868 }
22869
22870 /// @brief returns an iterator to the reverse-beginning
22871 /// @sa https://json.nlohmann.me/api/basic_json/rbegin/
22872 reverse_iterator rbegin() noexcept
22873 {
22874 return reverse_iterator(end());
22875 }
22876
22877 /// @brief returns an iterator to the reverse-beginning
22878 /// @sa https://json.nlohmann.me/api/basic_json/rbegin/
22879 const_reverse_iterator rbegin() const noexcept
22880 {
22881 return crbegin();
22882 }
22883
22884 /// @brief returns an iterator to the reverse-end
22885 /// @sa https://json.nlohmann.me/api/basic_json/rend/
22886 reverse_iterator rend() noexcept
22887 {
22888 return reverse_iterator(begin());
22889 }
22890
22891 /// @brief returns an iterator to the reverse-end
22892 /// @sa https://json.nlohmann.me/api/basic_json/rend/
22893 const_reverse_iterator rend() const noexcept
22894 {
22895 return crend();
22896 }
22897
22898 /// @brief returns a const reverse iterator to the last element
22899 /// @sa https://json.nlohmann.me/api/basic_json/crbegin/
22900 const_reverse_iterator crbegin() const noexcept
22901 {
22902 return const_reverse_iterator(cend());
22903 }
22904
22905 /// @brief returns a const reverse iterator to one before the first
22906 /// @sa https://json.nlohmann.me/api/basic_json/crend/
22907 const_reverse_iterator crend() const noexcept
22908 {
22909 return const_reverse_iterator(cbegin());
22910 }
22911
22912 public:
22913 /// @brief wrapper to access iterator member functions in range-based for
22914 /// @sa https://json.nlohmann.me/api/basic_json/items/
22915 /// @deprecated This function is deprecated since 3.1.0 and will be removed in
22916 /// version 4.0.0 of the library. Please use @ref items() instead;
22917 /// that is, replace `json::iterator_wrapper(j)` with `j.items()`.
22918 JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items())
22919 static iteration_proxy<iterator> iterator_wrapper(reference ref) noexcept
22920 {
22921 return ref.items();
22922 }
22923
22924 /// @brief wrapper to access iterator member functions in range-based for
22925 /// @sa https://json.nlohmann.me/api/basic_json/items/
22926 /// @deprecated This function is deprecated since 3.1.0 and will be removed in
22927 /// version 4.0.0 of the library. Please use @ref items() instead;
22928 /// that is, replace `json::iterator_wrapper(j)` with `j.items()`.
22929 JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items())
22930 static iteration_proxy<const_iterator> iterator_wrapper(const_reference ref) noexcept
22931 {
22932 return ref.items();
22933 }
22934
22935 /// @brief helper to access iterator member functions in range-based for
22936 /// @sa https://json.nlohmann.me/api/basic_json/items/
22937 iteration_proxy<iterator> items() noexcept
22938 {
22939 return iteration_proxy<iterator>(*this);
22940 }
22941
22942 /// @brief helper to access iterator member functions in range-based for
22943 /// @sa https://json.nlohmann.me/api/basic_json/items/
22944 iteration_proxy<const_iterator> items() const noexcept
22945 {
22946 return iteration_proxy<const_iterator>(*this);
22947 }
22948
22949 /// @}
22950
22951 //////////////
22952 // capacity //
22953 //////////////
22954
22955 /// @name capacity
22956 /// @{
22957
22958 /// @brief checks whether the container is empty.
22959 /// @sa https://json.nlohmann.me/api/basic_json/empty/
22960 bool empty() const noexcept
22961 {
22962 switch (m_data.m_type)
22963 {
22964 case value_t::null:
22965 {
22966 // null values are empty
22967 return true;
22968 }
22969
22970 case value_t::array:
22971 {
22972 // delegate call to array_t::empty()
22973 return m_data.m_value.array->empty();
22974 }
22975
22976 case value_t::object:
22977 {
22978 // delegate call to object_t::empty()
22979 return m_data.m_value.object->empty();
22980 }
22981
22982 case value_t::string:
22983 case value_t::boolean:
22984 case value_t::number_integer:
22985 case value_t::number_unsigned:
22986 case value_t::number_float:
22987 case value_t::binary:
22988 case value_t::discarded:
22989 default:
22990 {
22991 // all other types are nonempty
22992 return false;
22993 }
22994 }
22995 }
22996
22997 /// @brief returns the number of elements
22998 /// @sa https://json.nlohmann.me/api/basic_json/size/
22999 size_type size() const noexcept
23000 {
23001 switch (m_data.m_type)
23002 {
23003 case value_t::null:
23004 {
23005 // null values are empty
23006 return 0;
23007 }
23008
23009 case value_t::array:
23010 {
23011 // delegate call to array_t::size()
23012 return m_data.m_value.array->size();
23013 }
23014
23015 case value_t::object:
23016 {
23017 // delegate call to object_t::size()
23018 return m_data.m_value.object->size();
23019 }
23020
23021 case value_t::string:
23022 case value_t::boolean:
23023 case value_t::number_integer:
23024 case value_t::number_unsigned:
23025 case value_t::number_float:
23026 case value_t::binary:
23027 case value_t::discarded:
23028 default:
23029 {
23030 // all other types have size 1
23031 return 1;
23032 }
23033 }
23034 }
23035
23036 /// @brief returns the maximum possible number of elements
23037 /// @sa https://json.nlohmann.me/api/basic_json/max_size/
23038 size_type max_size() const noexcept
23039 {
23040 switch (m_data.m_type)
23041 {
23042 case value_t::array:
23043 {
23044 // delegate call to array_t::max_size()
23045 return m_data.m_value.array->max_size();
23046 }
23047
23048 case value_t::object:
23049 {
23050 // delegate call to object_t::max_size()
23051 return m_data.m_value.object->max_size();
23052 }
23053
23054 case value_t::null:
23055 case value_t::string:
23056 case value_t::boolean:
23057 case value_t::number_integer:
23058 case value_t::number_unsigned:
23059 case value_t::number_float:
23060 case value_t::binary:
23061 case value_t::discarded:
23062 default:
23063 {
23064 // all other types have max_size() == size()
23065 return size();
23066 }
23067 }
23068 }
23069
23070 /// @}
23071
23072 ///////////////
23073 // modifiers //
23074 ///////////////
23075
23076 /// @name modifiers
23077 /// @{
23078
23079 /// @brief clears the contents
23080 /// @sa https://json.nlohmann.me/api/basic_json/clear/
23081 void clear() noexcept
23082 {
23083 switch (m_data.m_type)
23084 {
23085 case value_t::number_integer:
23086 {
23087 m_data.m_value.number_integer = 0;
23088 break;
23089 }
23090
23091 case value_t::number_unsigned:
23092 {
23093 m_data.m_value.number_unsigned = 0;
23094 break;
23095 }
23096
23097 case value_t::number_float:
23098 {
23099 m_data.m_value.number_float = 0.0;
23100 break;
23101 }
23102
23103 case value_t::boolean:
23104 {
23105 m_data.m_value.boolean = false;
23106 break;
23107 }
23108
23109 case value_t::string:
23110 {
23111 m_data.m_value.string->clear();
23112 break;
23113 }
23114
23115 case value_t::binary:
23116 {
23117 m_data.m_value.binary->clear();
23118 break;
23119 }
23120
23121 case value_t::array:
23122 {
23123 m_data.m_value.array->clear();
23124 break;
23125 }
23126
23127 case value_t::object:
23128 {
23129 m_data.m_value.object->clear();
23130 break;
23131 }
23132
23133 case value_t::null:
23134 case value_t::discarded:
23135 default:
23136 break;
23137 }
23138 }
23139
23140 /// @brief add an object to an array
23141 /// @sa https://json.nlohmann.me/api/basic_json/push_back/
23142 void push_back(basic_json&& val)
23143 {
23144 // push_back only works for null objects or arrays
23145 if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
23146 {
23147 JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
23148 }
23149
23150 // transform null object into an array
23151 if (is_null())
23152 {
23153 m_data.m_type = value_t::array;
23154 m_data.m_value = value_t::array;
23155 assert_invariant();
23156 }
23157
23158 // add element to array (move semantics)
23159 const auto old_capacity = m_data.m_value.array->capacity();
23160 m_data.m_value.array->push_back(std::move(val));
23161 set_parent(m_data.m_value.array->back(), old_capacity);
23162 // if val is moved from, basic_json move constructor marks it null, so we do not call the destructor
23163 }
23164
23165 /// @brief add an object to an array
23166 /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
23167 reference operator+=(basic_json&& val)
23168 {
23169 push_back(std::move(val));
23170 return *this;
23171 }
23172
23173 /// @brief add an object to an array
23174 /// @sa https://json.nlohmann.me/api/basic_json/push_back/
23175 void push_back(const basic_json& val)
23176 {
23177 // push_back only works for null objects or arrays
23178 if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
23179 {
23180 JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
23181 }
23182
23183 // transform null object into an array
23184 if (is_null())
23185 {
23186 m_data.m_type = value_t::array;
23187 m_data.m_value = value_t::array;
23188 assert_invariant();
23189 }
23190
23191 // add element to array
23192 const auto old_capacity = m_data.m_value.array->capacity();
23193 m_data.m_value.array->push_back(val);
23194 set_parent(m_data.m_value.array->back(), old_capacity);
23195 }
23196
23197 /// @brief add an object to an array
23198 /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
23199 reference operator+=(const basic_json& val)
23200 {
23201 push_back(val);
23202 return *this;
23203 }
23204
23205 /// @brief add an object to an object
23206 /// @sa https://json.nlohmann.me/api/basic_json/push_back/
23207 void push_back(const typename object_t::value_type& val)
23208 {
23209 // push_back only works for null objects or objects
23210 if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
23211 {
23212 JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
23213 }
23214
23215 // transform null object into an object
23216 if (is_null())
23217 {
23218 m_data.m_type = value_t::object;
23219 m_data.m_value = value_t::object;
23220 assert_invariant();
23221 }
23222
23223 // add element to object
23224 auto res = m_data.m_value.object->insert(val);
23225 set_parent(res.first->second);
23226 }
23227
23228 /// @brief add an object to an object
23229 /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
23230 reference operator+=(const typename object_t::value_type& val)
23231 {
23232 push_back(val);
23233 return *this;
23234 }
23235
23236 /// @brief add an object to an object
23237 /// @sa https://json.nlohmann.me/api/basic_json/push_back/
23238 void push_back(initializer_list_t init)
23239 {
23240 if (is_object() && init.size() == 2 && (*init.begin())->is_string())
23241 {
23242 basic_json&& key = init.begin()->moved_or_copied();
23243 push_back(typename object_t::value_type(
23244 std::move(key.get_ref<string_t&>()), (init.begin() + 1)->moved_or_copied()));
23245 }
23246 else
23247 {
23248 push_back(basic_json(init));
23249 }
23250 }
23251
23252 /// @brief add an object to an object
23253 /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
23254 reference operator+=(initializer_list_t init)
23255 {
23256 push_back(init);
23257 return *this;
23258 }
23259
23260 /// @brief add an object to an array
23261 /// @sa https://json.nlohmann.me/api/basic_json/emplace_back/
23262 template<class... Args>
23263 reference emplace_back(Args&& ... args)
23264 {
23265 // emplace_back only works for null objects or arrays
23266 if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
23267 {
23268 JSON_THROW(type_error::create(311, detail::concat("cannot use emplace_back() with ", type_name()), this));
23269 }
23270
23271 // transform null object into an array
23272 if (is_null())
23273 {
23274 m_data.m_type = value_t::array;
23275 m_data.m_value = value_t::array;
23276 assert_invariant();
23277 }
23278
23279 // add element to array (perfect forwarding)
23280 const auto old_capacity = m_data.m_value.array->capacity();
23281 m_data.m_value.array->emplace_back(std::forward<Args>(args)...);
23282 return set_parent(m_data.m_value.array->back(), old_capacity);
23283 }
23284
23285 /// @brief add an object to an object if key does not exist
23286 /// @sa https://json.nlohmann.me/api/basic_json/emplace/
23287 template<class... Args>
23288 std::pair<iterator, bool> emplace(Args&& ... args)
23289 {
23290 // emplace only works for null objects or arrays
23291 if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
23292 {
23293 JSON_THROW(type_error::create(311, detail::concat("cannot use emplace() with ", type_name()), this));
23294 }
23295
23296 // transform null object into an object
23297 if (is_null())
23298 {
23299 m_data.m_type = value_t::object;
23300 m_data.m_value = value_t::object;
23301 assert_invariant();
23302 }
23303
23304 // add element to array (perfect forwarding)
23305 auto res = m_data.m_value.object->emplace(std::forward<Args>(args)...);
23306 set_parent(res.first->second);
23307
23308 // create result iterator and set iterator to the result of emplace
23309 auto it = begin();
23310 it.m_it.object_iterator = res.first;
23311
23312 // return pair of iterator and boolean
23313 return {it, res.second};
23314 }
23315
23316 /// Helper for insertion of an iterator
23317 /// @note: This uses std::distance to support GCC 4.8,
23318 /// see https://github.com/nlohmann/json/pull/1257
23319 template<typename... Args>
23320 iterator insert_iterator(const_iterator pos, Args&& ... args) // NOLINT(performance-unnecessary-value-param)
23321 {
23322 iterator result(this);
23323 JSON_ASSERT(m_data.m_value.array != nullptr);
23324
23325 auto insert_pos = std::distance(m_data.m_value.array->begin(), pos.m_it.array_iterator);
23326 m_data.m_value.array->insert(pos.m_it.array_iterator, std::forward<Args>(args)...);
23327 result.m_it.array_iterator = m_data.m_value.array->begin() + insert_pos;
23328
23329 // This could have been written as:
23330 // result.m_it.array_iterator = m_data.m_value.array->insert(pos.m_it.array_iterator, cnt, val);
23331 // but the return value of insert is missing in GCC 4.8, so it is written this way instead.
23332
23333 set_parents();
23334 return result;
23335 }
23336
23337 /// @brief inserts element into array
23338 /// @sa https://json.nlohmann.me/api/basic_json/insert/
23339 iterator insert(const_iterator pos, const basic_json& val) // NOLINT(performance-unnecessary-value-param)
23340 {
23341 // insert only works for arrays
23342 if (JSON_HEDLEY_LIKELY(is_array()))
23343 {
23344 // check if iterator pos fits to this JSON value
23345 if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
23346 {
23347 JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
23348 }
23349
23350 // insert to array and return iterator
23351 return insert_iterator(pos, val);
23352 }
23353
23354 JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
23355 }
23356
23357 /// @brief inserts element into array
23358 /// @sa https://json.nlohmann.me/api/basic_json/insert/
23359 iterator insert(const_iterator pos, basic_json&& val) // NOLINT(performance-unnecessary-value-param)
23360 {
23361 return insert(pos, val);
23362 }
23363
23364 /// @brief inserts copies of element into array
23365 /// @sa https://json.nlohmann.me/api/basic_json/insert/
23366 iterator insert(const_iterator pos, size_type cnt, const basic_json& val) // NOLINT(performance-unnecessary-value-param)
23367 {
23368 // insert only works for arrays
23369 if (JSON_HEDLEY_LIKELY(is_array()))
23370 {
23371 // check if iterator pos fits to this JSON value
23372 if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
23373 {
23374 JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
23375 }
23376
23377 // insert to array and return iterator
23378 return insert_iterator(pos, cnt, val);
23379 }
23380
23381 JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
23382 }
23383
23384 /// @brief inserts range of elements into array
23385 /// @sa https://json.nlohmann.me/api/basic_json/insert/
23386 iterator insert(const_iterator pos, const_iterator first, const_iterator last) // NOLINT(performance-unnecessary-value-param)
23387 {
23388 // insert only works for arrays
23389 if (JSON_HEDLEY_UNLIKELY(!is_array()))
23390 {
23391 JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
23392 }
23393
23394 // check if iterator pos fits to this JSON value
23395 if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
23396 {
23397 JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
23398 }
23399
23400 // check if range iterators belong to the same JSON object
23401 if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
23402 {
23403 JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
23404 }
23405
23406 if (JSON_HEDLEY_UNLIKELY(first.m_object == this))
23407 {
23408 JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container", this));
23409 }
23410
23411 // insert to array and return iterator
23412 return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator);
23413 }
23414
23415 /// @brief inserts elements from initializer list into array
23416 /// @sa https://json.nlohmann.me/api/basic_json/insert/
23417 iterator insert(const_iterator pos, initializer_list_t ilist) // NOLINT(performance-unnecessary-value-param)
23418 {
23419 // insert only works for arrays
23420 if (JSON_HEDLEY_UNLIKELY(!is_array()))
23421 {
23422 JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
23423 }
23424
23425 // check if iterator pos fits to this JSON value
23426 if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
23427 {
23428 JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
23429 }
23430
23431 // insert to array and return iterator
23432 return insert_iterator(pos, ilist.begin(), ilist.end());
23433 }
23434
23435 /// @brief inserts range of elements into object
23436 /// @sa https://json.nlohmann.me/api/basic_json/insert/
23437 void insert(const_iterator first, const_iterator last) // NOLINT(performance-unnecessary-value-param)
23438 {
23439 // insert only works for objects
23440 if (JSON_HEDLEY_UNLIKELY(!is_object()))
23441 {
23442 JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
23443 }
23444
23445 // check if range iterators belong to the same JSON object
23446 if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
23447 {
23448 JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
23449 }
23450
23451 // passed iterators must belong to objects
23452 if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()))
23453 {
23454 JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", this));
23455 }
23456
23457 m_data.m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator);
23458 set_parents();
23459 }
23460
23461 /// @brief updates a JSON object from another object, overwriting existing keys
23462 /// @sa https://json.nlohmann.me/api/basic_json/update/
23463 void update(const_reference j, bool merge_objects = false)
23464 {
23465 update(j.begin(), j.end(), merge_objects);
23466 }
23467
23468 /// @brief updates a JSON object from another object, overwriting existing keys
23469 /// @sa https://json.nlohmann.me/api/basic_json/update/
23470 void update(const_iterator first, const_iterator last, bool merge_objects = false) // NOLINT(performance-unnecessary-value-param)
23471 {
23472 // implicitly convert null value to an empty object
23473 if (is_null())
23474 {
23475 m_data.m_type = value_t::object;
23476 m_data.m_value.object = create<object_t>();
23477 assert_invariant();
23478 }
23479
23480 if (JSON_HEDLEY_UNLIKELY(!is_object()))
23481 {
23482 JSON_THROW(type_error::create(312, detail::concat("cannot use update() with ", type_name()), this));
23483 }
23484
23485 // check if range iterators belong to the same JSON object
23486 if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
23487 {
23488 JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
23489 }
23490
23491 // passed iterators must belong to objects
23492 if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()))
23493 {
23494 JSON_THROW(type_error::create(312, detail::concat("cannot use update() with ", first.m_object->type_name()), first.m_object));
23495 }
23496
23497 for (auto it = first; it != last; ++it)
23498 {
23499 if (merge_objects && it.value().is_object())
23500 {
23501 auto it2 = m_data.m_value.object->find(it.key());
23502 if (it2 != m_data.m_value.object->end())
23503 {
23504 it2->second.update(it.value(), true);
23505 continue;
23506 }
23507 }
23508 m_data.m_value.object->operator[](it.key()) = it.value();
23509 #if JSON_DIAGNOSTICS
23510 m_data.m_value.object->operator[](it.key()).m_parent = this;
23511 #endif
23512 }
23513 }
23514
23515 /// @brief exchanges the values
23516 /// @sa https://json.nlohmann.me/api/basic_json/swap/
23517 void swap(reference other) noexcept (
23518 std::is_nothrow_move_constructible<value_t>::value&&
23519 std::is_nothrow_move_assignable<value_t>::value&&
23520 std::is_nothrow_move_constructible<json_value>::value&& // NOLINT(cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
23521 std::is_nothrow_move_assignable<json_value>::value
23522 )
23523 {
23524 std::swap(m_data.m_type, other.m_data.m_type);
23525 std::swap(m_data.m_value, other.m_data.m_value);
23526
23527 set_parents();
23528 other.set_parents();
23529 assert_invariant();
23530 }
23531
23532 /// @brief exchanges the values
23533 /// @sa https://json.nlohmann.me/api/basic_json/swap/
23534 friend void swap(reference left, reference right) noexcept (
23535 std::is_nothrow_move_constructible<value_t>::value&&
23536 std::is_nothrow_move_assignable<value_t>::value&&
23537 std::is_nothrow_move_constructible<json_value>::value&& // NOLINT(cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
23538 std::is_nothrow_move_assignable<json_value>::value
23539 )
23540 {
23541 left.swap(right);
23542 }
23543
23544 /// @brief exchanges the values
23545 /// @sa https://json.nlohmann.me/api/basic_json/swap/
23546 void swap(array_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
23547 {
23548 // swap only works for arrays
23549 if (JSON_HEDLEY_LIKELY(is_array()))
23550 {
23551 using std::swap;
23552 swap(*(m_data.m_value.array), other);
23553 }
23554 else
23555 {
23556 JSON_THROW(type_error::create(310, detail::concat("cannot use swap(array_t&) with ", type_name()), this));
23557 }
23558 }
23559
23560 /// @brief exchanges the values
23561 /// @sa https://json.nlohmann.me/api/basic_json/swap/
23562 void swap(object_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
23563 {
23564 // swap only works for objects
23565 if (JSON_HEDLEY_LIKELY(is_object()))
23566 {
23567 using std::swap;
23568 swap(*(m_data.m_value.object), other);
23569 }
23570 else
23571 {
23572 JSON_THROW(type_error::create(310, detail::concat("cannot use swap(object_t&) with ", type_name()), this));
23573 }
23574 }
23575
23576 /// @brief exchanges the values
23577 /// @sa https://json.nlohmann.me/api/basic_json/swap/
23578 void swap(string_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
23579 {
23580 // swap only works for strings
23581 if (JSON_HEDLEY_LIKELY(is_string()))
23582 {
23583 using std::swap;
23584 swap(*(m_data.m_value.string), other);
23585 }
23586 else
23587 {
23588 JSON_THROW(type_error::create(310, detail::concat("cannot use swap(string_t&) with ", type_name()), this));
23589 }
23590 }
23591
23592 /// @brief exchanges the values
23593 /// @sa https://json.nlohmann.me/api/basic_json/swap/
23594 void swap(binary_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
23595 {
23596 // swap only works for strings
23597 if (JSON_HEDLEY_LIKELY(is_binary()))
23598 {
23599 using std::swap;
23600 swap(*(m_data.m_value.binary), other);
23601 }
23602 else
23603 {
23604 JSON_THROW(type_error::create(310, detail::concat("cannot use swap(binary_t&) with ", type_name()), this));
23605 }
23606 }
23607
23608 /// @brief exchanges the values
23609 /// @sa https://json.nlohmann.me/api/basic_json/swap/
23610 void swap(typename binary_t::container_type& other) // NOLINT(bugprone-exception-escape)
23611 {
23612 // swap only works for strings
23613 if (JSON_HEDLEY_LIKELY(is_binary()))
23614 {
23615 using std::swap;
23616 swap(*(m_data.m_value.binary), other);
23617 }
23618 else
23619 {
23620 JSON_THROW(type_error::create(310, detail::concat("cannot use swap(binary_t::container_type&) with ", type_name()), this));
23621 }
23622 }
23623
23624 /// @}
23625
23626 //////////////////////////////////////////
23627 // lexicographical comparison operators //
23628 //////////////////////////////////////////
23629
23630 /// @name lexicographical comparison operators
23631 /// @{
23632
23633 // note parentheses around operands are necessary; see
23634 // https://github.com/nlohmann/json/issues/1530
23635 #define JSON_IMPLEMENT_OPERATOR(op, null_result, unordered_result, default_result) \
23636 const auto lhs_type = lhs.type(); \
23637 const auto rhs_type = rhs.type(); \
23638 \
23639 if (lhs_type == rhs_type) /* NOLINT(readability/braces) */ \
23640 { \
23641 switch (lhs_type) \
23642 { \
23643 case value_t::array: \
23644 return (*lhs.m_data.m_value.array) op (*rhs.m_data.m_value.array); \
23645 \
23646 case value_t::object: \
23647 return (*lhs.m_data.m_value.object) op (*rhs.m_data.m_value.object); \
23648 \
23649 case value_t::null: \
23650 return (null_result); \
23651 \
23652 case value_t::string: \
23653 return (*lhs.m_data.m_value.string) op (*rhs.m_data.m_value.string); \
23654 \
23655 case value_t::boolean: \
23656 return (lhs.m_data.m_value.boolean) op (rhs.m_data.m_value.boolean); \
23657 \
23658 case value_t::number_integer: \
23659 return (lhs.m_data.m_value.number_integer) op (rhs.m_data.m_value.number_integer); \
23660 \
23661 case value_t::number_unsigned: \
23662 return (lhs.m_data.m_value.number_unsigned) op (rhs.m_data.m_value.number_unsigned); \
23663 \
23664 case value_t::number_float: \
23665 return (lhs.m_data.m_value.number_float) op (rhs.m_data.m_value.number_float); \
23666 \
23667 case value_t::binary: \
23668 return (*lhs.m_data.m_value.binary) op (*rhs.m_data.m_value.binary); \
23669 \
23670 case value_t::discarded: \
23671 default: \
23672 return (unordered_result); \
23673 } \
23674 } \
23675 else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_float) \
23676 { \
23677 return static_cast<number_float_t>(lhs.m_data.m_value.number_integer) op rhs.m_data.m_value.number_float; \
23678 } \
23679 else if (lhs_type == value_t::number_float && rhs_type == value_t::number_integer) \
23680 { \
23681 return lhs.m_data.m_value.number_float op static_cast<number_float_t>(rhs.m_data.m_value.number_integer); \
23682 } \
23683 else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_float) \
23684 { \
23685 return static_cast<number_float_t>(lhs.m_data.m_value.number_unsigned) op rhs.m_data.m_value.number_float; \
23686 } \
23687 else if (lhs_type == value_t::number_float && rhs_type == value_t::number_unsigned) \
23688 { \
23689 return lhs.m_data.m_value.number_float op static_cast<number_float_t>(rhs.m_data.m_value.number_unsigned); \
23690 } \
23691 else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_integer) \
23692 { \
23693 return static_cast<number_integer_t>(lhs.m_data.m_value.number_unsigned) op rhs.m_data.m_value.number_integer; \
23694 } \
23695 else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_unsigned) \
23696 { \
23697 return lhs.m_data.m_value.number_integer op static_cast<number_integer_t>(rhs.m_data.m_value.number_unsigned); \
23698 } \
23699 else if(compares_unordered(lhs, rhs))\
23700 {\
23701 return (unordered_result);\
23702 }\
23703 \
23704 return (default_result);
23705
23706 JSON_PRIVATE_UNLESS_TESTED:
23707 // returns true if:
23708 // - any operand is NaN and the other operand is of number type
23709 // - any operand is discarded
23710 // in legacy mode, discarded values are considered ordered if
23711 // an operation is computed as an odd number of inverses of others
23712 static bool compares_unordered(const_reference lhs, const_reference rhs, bool inverse = false) noexcept
23713 {
23714 if ((lhs.is_number_float() && std::isnan(lhs.m_data.m_value.number_float) && rhs.is_number())
23715 || (rhs.is_number_float() && std::isnan(rhs.m_data.m_value.number_float) && lhs.is_number()))
23716 {
23717 return true;
23718 }
23719 #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
23720 return (lhs.is_discarded() || rhs.is_discarded()) && !inverse;
23721 #else
23722 static_cast<void>(inverse);
23723 return lhs.is_discarded() || rhs.is_discarded();
23724 #endif
23725 }
23726
23727 private:
23728 bool compares_unordered(const_reference rhs, bool inverse = false) const noexcept
23729 {
23730 return compares_unordered(*this, rhs, inverse);
23731 }
23732
23733 public:
23734 #if JSON_HAS_THREE_WAY_COMPARISON
23735 /// @brief comparison: equal
23736 /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
23737 bool operator==(const_reference rhs) const noexcept
23738 {
23739 #ifdef __GNUC__
23740 #pragma GCC diagnostic push
23741 #pragma GCC diagnostic ignored "-Wfloat-equal"
23742 #endif
23743 const_reference lhs = *this;
23744 JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
23745 #ifdef __GNUC__
23746 #pragma GCC diagnostic pop
23747 #endif
23748 }
23749
23750 /// @brief comparison: equal
23751 /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
23752 template<typename ScalarType>
23753 requires std::is_scalar_v<ScalarType>
23754 bool operator==(ScalarType rhs) const noexcept
23755 {
23756 return *this == basic_json(rhs);
23757 }
23758
23759 /// @brief comparison: not equal
23760 /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
23761 bool operator!=(const_reference rhs) const noexcept
23762 {
23763 if (compares_unordered(rhs, true))
23764 {
23765 return false;
23766 }
23767 return !operator==(rhs);
23768 }
23769
23770 /// @brief comparison: 3-way
23771 /// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
23772 std::partial_ordering operator<=>(const_reference rhs) const noexcept // *NOPAD*
23773 {
23774 const_reference lhs = *this;
23775 // default_result is used if we cannot compare values. In that case,
23776 // we compare types.
23777 JSON_IMPLEMENT_OPERATOR(<=>, // *NOPAD*
23778 std::partial_ordering::equivalent,
23779 std::partial_ordering::unordered,
23780 lhs_type <=> rhs_type) // *NOPAD*
23781 }
23782
23783 /// @brief comparison: 3-way
23784 /// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
23785 template<typename ScalarType>
23786 requires std::is_scalar_v<ScalarType>
23787 std::partial_ordering operator<=>(ScalarType rhs) const noexcept // *NOPAD*
23788 {
23789 return *this <=> basic_json(rhs); // *NOPAD*
23790 }
23791
23792 #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
23793 // all operators that are computed as an odd number of inverses of others
23794 // need to be overloaded to emulate the legacy comparison behavior
23795
23796 /// @brief comparison: less than or equal
23797 /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
23798 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON)
23799 bool operator<=(const_reference rhs) const noexcept
23800 {
23801 if (compares_unordered(rhs, true))
23802 {
23803 return false;
23804 }
23805 return !(rhs < *this);
23806 }
23807
23808 /// @brief comparison: less than or equal
23809 /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
23810 template<typename ScalarType>
23811 requires std::is_scalar_v<ScalarType>
23812 bool operator<=(ScalarType rhs) const noexcept
23813 {
23814 return *this <= basic_json(rhs);
23815 }
23816
23817 /// @brief comparison: greater than or equal
23818 /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
23819 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON)
23820 bool operator>=(const_reference rhs) const noexcept
23821 {
23822 if (compares_unordered(rhs, true))
23823 {
23824 return false;
23825 }
23826 return !(*this < rhs);
23827 }
23828
23829 /// @brief comparison: greater than or equal
23830 /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
23831 template<typename ScalarType>
23832 requires std::is_scalar_v<ScalarType>
23833 bool operator>=(ScalarType rhs) const noexcept
23834 {
23835 return *this >= basic_json(rhs);
23836 }
23837 #endif
23838 #else
23839 /// @brief comparison: equal
23840 /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
23841 friend bool operator==(const_reference lhs, const_reference rhs) noexcept
23842 {
23843 #ifdef __GNUC__
23844 #pragma GCC diagnostic push
23845 #pragma GCC diagnostic ignored "-Wfloat-equal"
23846 #endif
23847 JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
23848 #ifdef __GNUC__
23849 #pragma GCC diagnostic pop
23850 #endif
23851 }
23852
23853 /// @brief comparison: equal
23854 /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
23855 template<typename ScalarType, typename std::enable_if<
23856 std::is_scalar<ScalarType>::value, int>::type = 0>
23857 friend bool operator==(const_reference lhs, ScalarType rhs) noexcept
23858 {
23859 return lhs == basic_json(rhs);
23860 }
23861
23862 /// @brief comparison: equal
23863 /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
23864 template<typename ScalarType, typename std::enable_if<
23865 std::is_scalar<ScalarType>::value, int>::type = 0>
23866 friend bool operator==(ScalarType lhs, const_reference rhs) noexcept
23867 {
23868 return basic_json(lhs) == rhs;
23869 }
23870
23871 /// @brief comparison: not equal
23872 /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
23873 friend bool operator!=(const_reference lhs, const_reference rhs) noexcept
23874 {
23875 if (compares_unordered(lhs, rhs, true))
23876 {
23877 return false;
23878 }
23879 return !(lhs == rhs);
23880 }
23881
23882 /// @brief comparison: not equal
23883 /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
23884 template<typename ScalarType, typename std::enable_if<
23885 std::is_scalar<ScalarType>::value, int>::type = 0>
23886 friend bool operator!=(const_reference lhs, ScalarType rhs) noexcept
23887 {
23888 return lhs != basic_json(rhs);
23889 }
23890
23891 /// @brief comparison: not equal
23892 /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
23893 template<typename ScalarType, typename std::enable_if<
23894 std::is_scalar<ScalarType>::value, int>::type = 0>
23895 friend bool operator!=(ScalarType lhs, const_reference rhs) noexcept
23896 {
23897 return basic_json(lhs) != rhs;
23898 }
23899
23900 /// @brief comparison: less than
23901 /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
23902 friend bool operator<(const_reference lhs, const_reference rhs) noexcept
23903 {
23904 // default_result is used if we cannot compare values. In that case,
23905 // we compare types. Note we have to call the operator explicitly,
23906 // because MSVC has problems otherwise.
23907 JSON_IMPLEMENT_OPERATOR( <, false, false, operator<(lhs_type, rhs_type))
23908 }
23909
23910 /// @brief comparison: less than
23911 /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
23912 template<typename ScalarType, typename std::enable_if<
23913 std::is_scalar<ScalarType>::value, int>::type = 0>
23914 friend bool operator<(const_reference lhs, ScalarType rhs) noexcept
23915 {
23916 return lhs < basic_json(rhs);
23917 }
23918
23919 /// @brief comparison: less than
23920 /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
23921 template<typename ScalarType, typename std::enable_if<
23922 std::is_scalar<ScalarType>::value, int>::type = 0>
23923 friend bool operator<(ScalarType lhs, const_reference rhs) noexcept
23924 {
23925 return basic_json(lhs) < rhs;
23926 }
23927
23928 /// @brief comparison: less than or equal
23929 /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
23930 friend bool operator<=(const_reference lhs, const_reference rhs) noexcept
23931 {
23932 if (compares_unordered(lhs, rhs, true))
23933 {
23934 return false;
23935 }
23936 return !(rhs < lhs);
23937 }
23938
23939 /// @brief comparison: less than or equal
23940 /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
23941 template<typename ScalarType, typename std::enable_if<
23942 std::is_scalar<ScalarType>::value, int>::type = 0>
23943 friend bool operator<=(const_reference lhs, ScalarType rhs) noexcept
23944 {
23945 return lhs <= basic_json(rhs);
23946 }
23947
23948 /// @brief comparison: less than or equal
23949 /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
23950 template<typename ScalarType, typename std::enable_if<
23951 std::is_scalar<ScalarType>::value, int>::type = 0>
23952 friend bool operator<=(ScalarType lhs, const_reference rhs) noexcept
23953 {
23954 return basic_json(lhs) <= rhs;
23955 }
23956
23957 /// @brief comparison: greater than
23958 /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
23959 friend bool operator>(const_reference lhs, const_reference rhs) noexcept
23960 {
23961 // double inverse
23962 if (compares_unordered(lhs, rhs))
23963 {
23964 return false;
23965 }
23966 return !(lhs <= rhs);
23967 }
23968
23969 /// @brief comparison: greater than
23970 /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
23971 template<typename ScalarType, typename std::enable_if<
23972 std::is_scalar<ScalarType>::value, int>::type = 0>
23973 friend bool operator>(const_reference lhs, ScalarType rhs) noexcept
23974 {
23975 return lhs > basic_json(rhs);
23976 }
23977
23978 /// @brief comparison: greater than
23979 /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
23980 template<typename ScalarType, typename std::enable_if<
23981 std::is_scalar<ScalarType>::value, int>::type = 0>
23982 friend bool operator>(ScalarType lhs, const_reference rhs) noexcept
23983 {
23984 return basic_json(lhs) > rhs;
23985 }
23986
23987 /// @brief comparison: greater than or equal
23988 /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
23989 friend bool operator>=(const_reference lhs, const_reference rhs) noexcept
23990 {
23991 if (compares_unordered(lhs, rhs, true))
23992 {
23993 return false;
23994 }
23995 return !(lhs < rhs);
23996 }
23997
23998 /// @brief comparison: greater than or equal
23999 /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
24000 template<typename ScalarType, typename std::enable_if<
24001 std::is_scalar<ScalarType>::value, int>::type = 0>
24002 friend bool operator>=(const_reference lhs, ScalarType rhs) noexcept
24003 {
24004 return lhs >= basic_json(rhs);
24005 }
24006
24007 /// @brief comparison: greater than or equal
24008 /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
24009 template<typename ScalarType, typename std::enable_if<
24010 std::is_scalar<ScalarType>::value, int>::type = 0>
24011 friend bool operator>=(ScalarType lhs, const_reference rhs) noexcept
24012 {
24013 return basic_json(lhs) >= rhs;
24014 }
24015 #endif
24016
24017 #undef JSON_IMPLEMENT_OPERATOR
24018
24019 /// @}
24020
24021 ///////////////////
24022 // serialization //
24023 ///////////////////
24024
24025 /// @name serialization
24026 /// @{
24027 #ifndef JSON_NO_IO
24028 /// @brief serialize to stream
24029 /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
24030 friend std::ostream& operator<<(std::ostream& o, const basic_json& j)
24031 {
24032 // read width member and use it as indentation parameter if nonzero
24033 const bool pretty_print = o.width() > 0;
24034 const auto indentation = pretty_print ? o.width() : 0;
24035
24036 // reset width to 0 for subsequent calls to this stream
24037 o.width(0);
24038
24039 // do the actual serialization
24040 serializer s(detail::output_adapter<char>(o), o.fill());
24041 s.dump(j, pretty_print, false, static_cast<unsigned int>(indentation));
24042 return o;
24043 }
24044
24045 /// @brief serialize to stream
24046 /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
24047 /// @deprecated This function is deprecated since 3.0.0 and will be removed in
24048 /// version 4.0.0 of the library. Please use
24049 /// operator<<(std::ostream&, const basic_json&) instead; that is,
24050 /// replace calls like `j >> o;` with `o << j;`.
24051 JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator<<(std::ostream&, const basic_json&))
24052 friend std::ostream& operator>>(const basic_json& j, std::ostream& o)
24053 {
24054 return o << j;
24055 }
24056 #endif // JSON_NO_IO
24057 /// @}
24058
24059 /////////////////////
24060 // deserialization //
24061 /////////////////////
24062
24063 /// @name deserialization
24064 /// @{
24065
24066 /// @brief deserialize from a compatible input
24067 /// @sa https://json.nlohmann.me/api/basic_json/parse/
24068 template<typename InputType>
24069 JSON_HEDLEY_WARN_UNUSED_RESULT
24070 static basic_json parse(InputType&& i,
24071 parser_callback_t cb = nullptr,
24072 const bool allow_exceptions = true,
24073 const bool ignore_comments = false)
24074 {
24075 basic_json result;
24076 parser(detail::input_adapter(std::forward<InputType>(i)), std::move(cb), allow_exceptions, ignore_comments).parse(true, result); // cppcheck-suppress[accessMoved,accessForwarded]
24077 return result;
24078 }
24079
24080 /// @brief deserialize from a pair of character iterators
24081 /// @sa https://json.nlohmann.me/api/basic_json/parse/
24082 template<typename IteratorType>
24083 JSON_HEDLEY_WARN_UNUSED_RESULT
24084 static basic_json parse(IteratorType first,
24085 IteratorType last,
24086 parser_callback_t cb = nullptr,
24087 const bool allow_exceptions = true,
24088 const bool ignore_comments = false)
24089 {
24090 basic_json result;
24091 parser(detail::input_adapter(std::move(first), std::move(last)), std::move(cb), allow_exceptions, ignore_comments).parse(true, result); // cppcheck-suppress[accessMoved]
24092 return result;
24093 }
24094
24095 JSON_HEDLEY_WARN_UNUSED_RESULT
24096 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, parse(ptr, ptr + len))
24097 static basic_json parse(detail::span_input_adapter&& i,
24098 parser_callback_t cb = nullptr,
24099 const bool allow_exceptions = true,
24100 const bool ignore_comments = false)
24101 {
24102 basic_json result;
24103 parser(i.get(), std::move(cb), allow_exceptions, ignore_comments).parse(true, result); // cppcheck-suppress[accessMoved]
24104 return result;
24105 }
24106
24107 /// @brief check if the input is valid JSON
24108 /// @sa https://json.nlohmann.me/api/basic_json/accept/
24109 template<typename InputType>
24110 static bool accept(InputType&& i,
24111 const bool ignore_comments = false)
24112 {
24113 return parser(detail::input_adapter(std::forward<InputType>(i)), nullptr, false, ignore_comments).accept(true);
24114 }
24115
24116 /// @brief check if the input is valid JSON
24117 /// @sa https://json.nlohmann.me/api/basic_json/accept/
24118 template<typename IteratorType>
24119 static bool accept(IteratorType first, IteratorType last,
24120 const bool ignore_comments = false)
24121 {
24122 return parser(detail::input_adapter(std::move(first), std::move(last)), nullptr, false, ignore_comments).accept(true);
24123 }
24124
24125 JSON_HEDLEY_WARN_UNUSED_RESULT
24126 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, accept(ptr, ptr + len))
24127 static bool accept(detail::span_input_adapter&& i,
24128 const bool ignore_comments = false)
24129 {
24130 return parser(i.get(), nullptr, false, ignore_comments).accept(true);
24131 }
24132
24133 /// @brief generate SAX events
24134 /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
24135 template <typename InputType, typename SAX>
24136 JSON_HEDLEY_NON_NULL(2)
24137 static bool sax_parse(InputType&& i, SAX* sax,
24138 input_format_t format = input_format_t::json,
24139 const bool strict = true,
24140 const bool ignore_comments = false)
24141 {
24142 auto ia = detail::input_adapter(std::forward<InputType>(i));
24143 return format == input_format_t::json
24144 ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
24145 : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
24146 }
24147
24148 /// @brief generate SAX events
24149 /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
24150 template<class IteratorType, class SAX>
24151 JSON_HEDLEY_NON_NULL(3)
24152 static bool sax_parse(IteratorType first, IteratorType last, SAX* sax,
24153 input_format_t format = input_format_t::json,
24154 const bool strict = true,
24155 const bool ignore_comments = false)
24156 {
24157 auto ia = detail::input_adapter(std::move(first), std::move(last));
24158 return format == input_format_t::json
24159 ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
24160 : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
24161 }
24162
24163 /// @brief generate SAX events
24164 /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
24165 /// @deprecated This function is deprecated since 3.8.0 and will be removed in
24166 /// version 4.0.0 of the library. Please use
24167 /// sax_parse(ptr, ptr + len) instead.
24168 template <typename SAX>
24169 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, sax_parse(ptr, ptr + len, ...))
24170 JSON_HEDLEY_NON_NULL(2)
24171 static bool sax_parse(detail::span_input_adapter&& i, SAX* sax,
24172 input_format_t format = input_format_t::json,
24173 const bool strict = true,
24174 const bool ignore_comments = false)
24175 {
24176 auto ia = i.get();
24177 return format == input_format_t::json
24178 // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
24179 ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
24180 // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
24181 : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
24182 }
24183 #ifndef JSON_NO_IO
24184 /// @brief deserialize from stream
24185 /// @sa https://json.nlohmann.me/api/basic_json/operator_gtgt/
24186 /// @deprecated This stream operator is deprecated since 3.0.0 and will be removed in
24187 /// version 4.0.0 of the library. Please use
24188 /// operator>>(std::istream&, basic_json&) instead; that is,
24189 /// replace calls like `j << i;` with `i >> j;`.
24190 JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator>>(std::istream&, basic_json&))
24191 friend std::istream& operator<<(basic_json& j, std::istream& i)
24192 {
24193 return operator>>(i, j);
24194 }
24195
24196 /// @brief deserialize from stream
24197 /// @sa https://json.nlohmann.me/api/basic_json/operator_gtgt/
24198 friend std::istream& operator>>(std::istream& i, basic_json& j)
24199 {
24200 parser(detail::input_adapter(i)).parse(false, j);
24201 return i;
24202 }
24203 #endif // JSON_NO_IO
24204 /// @}
24205
24206 ///////////////////////////
24207 // convenience functions //
24208 ///////////////////////////
24209
24210 /// @brief return the type as string
24211 /// @sa https://json.nlohmann.me/api/basic_json/type_name/
24212 JSON_HEDLEY_RETURNS_NON_NULL
24213 const char* type_name() const noexcept
24214 {
24215 switch (m_data.m_type)
24216 {
24217 case value_t::null:
24218 return "null";
24219 case value_t::object:
24220 return "object";
24221 case value_t::array:
24222 return "array";
24223 case value_t::string:
24224 return "string";
24225 case value_t::boolean:
24226 return "boolean";
24227 case value_t::binary:
24228 return "binary";
24229 case value_t::discarded:
24230 return "discarded";
24231 case value_t::number_integer:
24232 case value_t::number_unsigned:
24233 case value_t::number_float:
24234 default:
24235 return "number";
24236 }
24237 }
24238
24239 JSON_PRIVATE_UNLESS_TESTED:
24240 //////////////////////
24241 // member variables //
24242 //////////////////////
24243
24244 struct data
24245 {
24246 /// the type of the current element
24247 value_t m_type = value_t::null;
24248
24249 /// the value of the current element
24250 json_value m_value = {};
24251
24252 data(const value_t v)
24253 : m_type(v), m_value(v)
24254 {
24255 }
24256
24257 data(size_type cnt, const basic_json& val)
24258 : m_type(value_t::array)
24259 {
24260 m_value.array = create<array_t>(cnt, val);
24261 }
24262
24263 data() noexcept = default;
24264 data(data&&) noexcept = default;
24265 data(const data&) noexcept = delete;
24266 data& operator=(data&&) noexcept = delete;
24267 data& operator=(const data&) noexcept = delete;
24268
24269 ~data() noexcept
24270 {
24271 m_value.destroy(m_type);
24272 }
24273 };
24274
24275 data m_data = {};
24276
24277 #if JSON_DIAGNOSTICS
24278 /// a pointer to a parent value (for debugging purposes)
24279 basic_json* m_parent = nullptr;
24280 #endif
24281
24282 #if JSON_DIAGNOSTIC_POSITIONS
24283 /// the start position of the value
24284 std::size_t start_position = std::string::npos;
24285 /// the end position of the value
24286 std::size_t end_position = std::string::npos;
24287 public:
24288 constexpr std::size_t start_pos() const noexcept
24289 {
24290 return start_position;
24291 }
24292
24293 constexpr std::size_t end_pos() const noexcept
24294 {
24295 return end_position;
24296 }
24297 #endif
24298
24299 //////////////////////////////////////////
24300 // binary serialization/deserialization //
24301 //////////////////////////////////////////
24302
24303 /// @name binary serialization/deserialization support
24304 /// @{
24305
24306 public:
24307 /// @brief create a CBOR serialization of a given JSON value
24308 /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
24309 static std::vector<std::uint8_t> to_cbor(const basic_json& j)
24310 {
24311 std::vector<std::uint8_t> result;
24312 to_cbor(j, result);
24313 return result;
24314 }
24315
24316 /// @brief create a CBOR serialization of a given JSON value
24317 /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
24318 static void to_cbor(const basic_json& j, detail::output_adapter<std::uint8_t> o)
24319 {
24320 binary_writer<std::uint8_t>(o).write_cbor(j);
24321 }
24322
24323 /// @brief create a CBOR serialization of a given JSON value
24324 /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
24325 static void to_cbor(const basic_json& j, detail::output_adapter<char> o)
24326 {
24327 binary_writer<char>(o).write_cbor(j);
24328 }
24329
24330 /// @brief create a MessagePack serialization of a given JSON value
24331 /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
24332 static std::vector<std::uint8_t> to_msgpack(const basic_json& j)
24333 {
24334 std::vector<std::uint8_t> result;
24335 to_msgpack(j, result);
24336 return result;
24337 }
24338
24339 /// @brief create a MessagePack serialization of a given JSON value
24340 /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
24341 static void to_msgpack(const basic_json& j, detail::output_adapter<std::uint8_t> o)
24342 {
24343 binary_writer<std::uint8_t>(o).write_msgpack(j);
24344 }
24345
24346 /// @brief create a MessagePack serialization of a given JSON value
24347 /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
24348 static void to_msgpack(const basic_json& j, detail::output_adapter<char> o)
24349 {
24350 binary_writer<char>(o).write_msgpack(j);
24351 }
24352
24353 /// @brief create a UBJSON serialization of a given JSON value
24354 /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
24355 static std::vector<std::uint8_t> to_ubjson(const basic_json& j,
24356 const bool use_size = false,
24357 const bool use_type = false)
24358 {
24359 std::vector<std::uint8_t> result;
24360 to_ubjson(j, result, use_size, use_type);
24361 return result;
24362 }
24363
24364 /// @brief create a UBJSON serialization of a given JSON value
24365 /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
24366 static void to_ubjson(const basic_json& j, detail::output_adapter<std::uint8_t> o,
24367 const bool use_size = false, const bool use_type = false)
24368 {
24369 binary_writer<std::uint8_t>(o).write_ubjson(j, use_size, use_type);
24370 }
24371
24372 /// @brief create a UBJSON serialization of a given JSON value
24373 /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
24374 static void to_ubjson(const basic_json& j, detail::output_adapter<char> o,
24375 const bool use_size = false, const bool use_type = false)
24376 {
24377 binary_writer<char>(o).write_ubjson(j, use_size, use_type);
24378 }
24379
24380 /// @brief create a BJData serialization of a given JSON value
24381 /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
24382 static std::vector<std::uint8_t> to_bjdata(const basic_json& j,
24383 const bool use_size = false,
24384 const bool use_type = false,
24385 const bjdata_version_t version = bjdata_version_t::draft2)
24386 {
24387 std::vector<std::uint8_t> result;
24388 to_bjdata(j, result, use_size, use_type, version);
24389 return result;
24390 }
24391
24392 /// @brief create a BJData serialization of a given JSON value
24393 /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
24394 static void to_bjdata(const basic_json& j, detail::output_adapter<std::uint8_t> o,
24395 const bool use_size = false, const bool use_type = false,
24396 const bjdata_version_t version = bjdata_version_t::draft2)
24397 {
24398 binary_writer<std::uint8_t>(o).write_ubjson(j, use_size, use_type, true, true, version);
24399 }
24400
24401 /// @brief create a BJData serialization of a given JSON value
24402 /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
24403 static void to_bjdata(const basic_json& j, detail::output_adapter<char> o,
24404 const bool use_size = false, const bool use_type = false,
24405 const bjdata_version_t version = bjdata_version_t::draft2)
24406 {
24407 binary_writer<char>(o).write_ubjson(j, use_size, use_type, true, true, version);
24408 }
24409
24410 /// @brief create a BSON serialization of a given JSON value
24411 /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
24412 static std::vector<std::uint8_t> to_bson(const basic_json& j)
24413 {
24414 std::vector<std::uint8_t> result;
24415 to_bson(j, result);
24416 return result;
24417 }
24418
24419 /// @brief create a BSON serialization of a given JSON value
24420 /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
24421 static void to_bson(const basic_json& j, detail::output_adapter<std::uint8_t> o)
24422 {
24423 binary_writer<std::uint8_t>(o).write_bson(j);
24424 }
24425
24426 /// @brief create a BSON serialization of a given JSON value
24427 /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
24428 static void to_bson(const basic_json& j, detail::output_adapter<char> o)
24429 {
24430 binary_writer<char>(o).write_bson(j);
24431 }
24432
24433 /// @brief create a JSON value from an input in CBOR format
24434 /// @sa https://json.nlohmann.me/api/basic_json/from_cbor/
24435 template<typename InputType>
24436 JSON_HEDLEY_WARN_UNUSED_RESULT
24437 static basic_json from_cbor(InputType&& i,
24438 const bool strict = true,
24439 const bool allow_exceptions = true,
24440 const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
24441 {
24442 basic_json result;
24443 auto ia = detail::input_adapter(std::forward<InputType>(i));
24444 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24445 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]
24446 return res ? result : basic_json(value_t::discarded);
24447 }
24448
24449 /// @brief create a JSON value from an input in CBOR format
24450 /// @sa https://json.nlohmann.me/api/basic_json/from_cbor/
24451 template<typename IteratorType>
24452 JSON_HEDLEY_WARN_UNUSED_RESULT
24453 static basic_json from_cbor(IteratorType first, IteratorType last,
24454 const bool strict = true,
24455 const bool allow_exceptions = true,
24456 const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
24457 {
24458 basic_json result;
24459 auto ia = detail::input_adapter(std::move(first), std::move(last));
24460 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24461 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]
24462 return res ? result : basic_json(value_t::discarded);
24463 }
24464
24465 template<typename T>
24466 JSON_HEDLEY_WARN_UNUSED_RESULT
24467 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len))
24468 static basic_json from_cbor(const T* ptr, std::size_t len,
24469 const bool strict = true,
24470 const bool allow_exceptions = true,
24471 const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
24472 {
24473 return from_cbor(ptr, ptr + len, strict, allow_exceptions, tag_handler);
24474 }
24475
24476 JSON_HEDLEY_WARN_UNUSED_RESULT
24477 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len))
24478 static basic_json from_cbor(detail::span_input_adapter&& i,
24479 const bool strict = true,
24480 const bool allow_exceptions = true,
24481 const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
24482 {
24483 basic_json result;
24484 auto ia = i.get();
24485 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24486 // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
24487 const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler); // cppcheck-suppress[accessMoved]
24488 return res ? result : basic_json(value_t::discarded);
24489 }
24490
24491 /// @brief create a JSON value from an input in MessagePack format
24492 /// @sa https://json.nlohmann.me/api/basic_json/from_msgpack/
24493 template<typename InputType>
24494 JSON_HEDLEY_WARN_UNUSED_RESULT
24495 static basic_json from_msgpack(InputType&& i,
24496 const bool strict = true,
24497 const bool allow_exceptions = true)
24498 {
24499 basic_json result;
24500 auto ia = detail::input_adapter(std::forward<InputType>(i));
24501 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24502 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]
24503 return res ? result : basic_json(value_t::discarded);
24504 }
24505
24506 /// @brief create a JSON value from an input in MessagePack format
24507 /// @sa https://json.nlohmann.me/api/basic_json/from_msgpack/
24508 template<typename IteratorType>
24509 JSON_HEDLEY_WARN_UNUSED_RESULT
24510 static basic_json from_msgpack(IteratorType first, IteratorType last,
24511 const bool strict = true,
24512 const bool allow_exceptions = true)
24513 {
24514 basic_json result;
24515 auto ia = detail::input_adapter(std::move(first), std::move(last));
24516 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24517 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]
24518 return res ? result : basic_json(value_t::discarded);
24519 }
24520
24521 template<typename T>
24522 JSON_HEDLEY_WARN_UNUSED_RESULT
24523 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len))
24524 static basic_json from_msgpack(const T* ptr, std::size_t len,
24525 const bool strict = true,
24526 const bool allow_exceptions = true)
24527 {
24528 return from_msgpack(ptr, ptr + len, strict, allow_exceptions);
24529 }
24530
24531 JSON_HEDLEY_WARN_UNUSED_RESULT
24532 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len))
24533 static basic_json from_msgpack(detail::span_input_adapter&& i,
24534 const bool strict = true,
24535 const bool allow_exceptions = true)
24536 {
24537 basic_json result;
24538 auto ia = i.get();
24539 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24540 // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
24541 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]
24542 return res ? result : basic_json(value_t::discarded);
24543 }
24544
24545 /// @brief create a JSON value from an input in UBJSON format
24546 /// @sa https://json.nlohmann.me/api/basic_json/from_ubjson/
24547 template<typename InputType>
24548 JSON_HEDLEY_WARN_UNUSED_RESULT
24549 static basic_json from_ubjson(InputType&& i,
24550 const bool strict = true,
24551 const bool allow_exceptions = true)
24552 {
24553 basic_json result;
24554 auto ia = detail::input_adapter(std::forward<InputType>(i));
24555 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24556 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]
24557 return res ? result : basic_json(value_t::discarded);
24558 }
24559
24560 /// @brief create a JSON value from an input in UBJSON format
24561 /// @sa https://json.nlohmann.me/api/basic_json/from_ubjson/
24562 template<typename IteratorType>
24563 JSON_HEDLEY_WARN_UNUSED_RESULT
24564 static basic_json from_ubjson(IteratorType first, IteratorType last,
24565 const bool strict = true,
24566 const bool allow_exceptions = true)
24567 {
24568 basic_json result;
24569 auto ia = detail::input_adapter(std::move(first), std::move(last));
24570 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24571 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]
24572 return res ? result : basic_json(value_t::discarded);
24573 }
24574
24575 template<typename T>
24576 JSON_HEDLEY_WARN_UNUSED_RESULT
24577 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len))
24578 static basic_json from_ubjson(const T* ptr, std::size_t len,
24579 const bool strict = true,
24580 const bool allow_exceptions = true)
24581 {
24582 return from_ubjson(ptr, ptr + len, strict, allow_exceptions);
24583 }
24584
24585 JSON_HEDLEY_WARN_UNUSED_RESULT
24586 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len))
24587 static basic_json from_ubjson(detail::span_input_adapter&& i,
24588 const bool strict = true,
24589 const bool allow_exceptions = true)
24590 {
24591 basic_json result;
24592 auto ia = i.get();
24593 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24594 // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
24595 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]
24596 return res ? result : basic_json(value_t::discarded);
24597 }
24598
24599 /// @brief create a JSON value from an input in BJData format
24600 /// @sa https://json.nlohmann.me/api/basic_json/from_bjdata/
24601 template<typename InputType>
24602 JSON_HEDLEY_WARN_UNUSED_RESULT
24603 static basic_json from_bjdata(InputType&& i,
24604 const bool strict = true,
24605 const bool allow_exceptions = true)
24606 {
24607 basic_json result;
24608 auto ia = detail::input_adapter(std::forward<InputType>(i));
24609 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24610 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]
24611 return res ? result : basic_json(value_t::discarded);
24612 }
24613
24614 /// @brief create a JSON value from an input in BJData format
24615 /// @sa https://json.nlohmann.me/api/basic_json/from_bjdata/
24616 template<typename IteratorType>
24617 JSON_HEDLEY_WARN_UNUSED_RESULT
24618 static basic_json from_bjdata(IteratorType first, IteratorType last,
24619 const bool strict = true,
24620 const bool allow_exceptions = true)
24621 {
24622 basic_json result;
24623 auto ia = detail::input_adapter(std::move(first), std::move(last));
24624 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24625 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]
24626 return res ? result : basic_json(value_t::discarded);
24627 }
24628
24629 /// @brief create a JSON value from an input in BSON format
24630 /// @sa https://json.nlohmann.me/api/basic_json/from_bson/
24631 template<typename InputType>
24632 JSON_HEDLEY_WARN_UNUSED_RESULT
24633 static basic_json from_bson(InputType&& i,
24634 const bool strict = true,
24635 const bool allow_exceptions = true)
24636 {
24637 basic_json result;
24638 auto ia = detail::input_adapter(std::forward<InputType>(i));
24639 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24640 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]
24641 return res ? result : basic_json(value_t::discarded);
24642 }
24643
24644 /// @brief create a JSON value from an input in BSON format
24645 /// @sa https://json.nlohmann.me/api/basic_json/from_bson/
24646 template<typename IteratorType>
24647 JSON_HEDLEY_WARN_UNUSED_RESULT
24648 static basic_json from_bson(IteratorType first, IteratorType last,
24649 const bool strict = true,
24650 const bool allow_exceptions = true)
24651 {
24652 basic_json result;
24653 auto ia = detail::input_adapter(std::move(first), std::move(last));
24654 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24655 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]
24656 return res ? result : basic_json(value_t::discarded);
24657 }
24658
24659 template<typename T>
24660 JSON_HEDLEY_WARN_UNUSED_RESULT
24661 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len))
24662 static basic_json from_bson(const T* ptr, std::size_t len,
24663 const bool strict = true,
24664 const bool allow_exceptions = true)
24665 {
24666 return from_bson(ptr, ptr + len, strict, allow_exceptions);
24667 }
24668
24669 JSON_HEDLEY_WARN_UNUSED_RESULT
24670 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len))
24671 static basic_json from_bson(detail::span_input_adapter&& i,
24672 const bool strict = true,
24673 const bool allow_exceptions = true)
24674 {
24675 basic_json result;
24676 auto ia = i.get();
24677 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24678 // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
24679 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]
24680 return res ? result : basic_json(value_t::discarded);
24681 }
24682 /// @}
24683
24684 //////////////////////////
24685 // JSON Pointer support //
24686 //////////////////////////
24687
24688 /// @name JSON Pointer functions
24689 /// @{
24690
24691 /// @brief access specified element via JSON Pointer
24692 /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
24693 reference operator[](const json_pointer& ptr)
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 reference operator[](const ::nlohmann::json_pointer<BasicJsonType>& ptr)
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/operator%5B%5D/
24707 const_reference operator[](const json_pointer& ptr) const
24708 {
24709 return ptr.get_unchecked(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 const_reference operator[](const ::nlohmann::json_pointer<BasicJsonType>& ptr) const
24715 {
24716 return ptr.get_unchecked(this);
24717 }
24718
24719 /// @brief access specified element via JSON Pointer
24720 /// @sa https://json.nlohmann.me/api/basic_json/at/
24721 reference at(const json_pointer& ptr)
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 reference at(const ::nlohmann::json_pointer<BasicJsonType>& ptr)
24729 {
24730 return ptr.get_checked(this);
24731 }
24732
24733 /// @brief access specified element via JSON Pointer
24734 /// @sa https://json.nlohmann.me/api/basic_json/at/
24735 const_reference at(const json_pointer& ptr) const
24736 {
24737 return ptr.get_checked(this);
24738 }
24739
24740 template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
24741 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
24742 const_reference at(const ::nlohmann::json_pointer<BasicJsonType>& ptr) const
24743 {
24744 return ptr.get_checked(this);
24745 }
24746
24747 /// @brief return flattened JSON value
24748 /// @sa https://json.nlohmann.me/api/basic_json/flatten/
24749 basic_json flatten() const
24750 {
24751 basic_json result(value_t::object);
24752 json_pointer::flatten("", *this, result);
24753 return result;
24754 }
24755
24756 /// @brief unflatten a previously flattened JSON value
24757 /// @sa https://json.nlohmann.me/api/basic_json/unflatten/
24758 basic_json unflatten() const
24759 {
24760 return json_pointer::unflatten(*this);
24761 }
24762
24763 /// @}
24764
24765 //////////////////////////
24766 // JSON Patch functions //
24767 //////////////////////////
24768
24769 /// @name JSON Patch functions
24770 /// @{
24771
24772 /// @brief applies a JSON patch in-place without copying the object
24773 /// @sa https://json.nlohmann.me/api/basic_json/patch/
24774 void patch_inplace(const basic_json& json_patch)
24775 {
24776 basic_json& result = *this;
24777 // the valid JSON Patch operations
24778 enum class patch_operations {add, remove, replace, move, copy, test, invalid};
24779
24780 const auto get_op = [](const string_t& op)
24781 {
24782 if (op == "add")
24783 {
24784 return patch_operations::add;
24785 }
24786 if (op == "remove")
24787 {
24788 return patch_operations::remove;
24789 }
24790 if (op == "replace")
24791 {
24792 return patch_operations::replace;
24793 }
24794 if (op == "move")
24795 {
24796 return patch_operations::move;
24797 }
24798 if (op == "copy")
24799 {
24800 return patch_operations::copy;
24801 }
24802 if (op == "test")
24803 {
24804 return patch_operations::test;
24805 }
24806
24807 return patch_operations::invalid;
24808 };
24809
24810 // wrapper for "add" operation; add value at ptr
24811 const auto operation_add = [&result](json_pointer & ptr, const basic_json & val)
24812 {
24813 // adding to the root of the target document means replacing it
24814 if (ptr.empty())
24815 {
24816 result = val;
24817 return;
24818 }
24819
24820 // make sure the top element of the pointer exists
24821 json_pointer const top_pointer = ptr.top();
24822 if (top_pointer != ptr)
24823 {
24824 result.at(top_pointer);
24825 }
24826
24827 // get reference to parent of JSON pointer ptr
24828 const auto last_path = ptr.back();
24829 ptr.pop_back();
24830 // parent must exist when performing patch add per RFC6902 specs
24831 basic_json& parent = result.at(ptr);
24832
24833 switch (parent.m_data.m_type)
24834 {
24835 case value_t::null:
24836 case value_t::object:
24837 {
24838 // use operator[] to add value
24839 parent[last_path] = val;
24840 break;
24841 }
24842
24843 case value_t::array:
24844 {
24845 if (last_path == "-")
24846 {
24847 // special case: append to back
24848 parent.push_back(val);
24849 }
24850 else
24851 {
24852 const auto idx = json_pointer::template array_index<basic_json_t>(last_path);
24853 if (JSON_HEDLEY_UNLIKELY(idx > parent.size()))
24854 {
24855 // avoid undefined behavior
24856 JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), &parent));
24857 }
24858
24859 // default case: insert add offset
24860 parent.insert(parent.begin() + static_cast<difference_type>(idx), val);
24861 }
24862 break;
24863 }
24864
24865 // if there exists a parent it cannot be primitive
24866 case value_t::string: // LCOV_EXCL_LINE
24867 case value_t::boolean: // LCOV_EXCL_LINE
24868 case value_t::number_integer: // LCOV_EXCL_LINE
24869 case value_t::number_unsigned: // LCOV_EXCL_LINE
24870 case value_t::number_float: // LCOV_EXCL_LINE
24871 case value_t::binary: // LCOV_EXCL_LINE
24872 case value_t::discarded: // LCOV_EXCL_LINE
24873 default: // LCOV_EXCL_LINE
24874 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
24875 }
24876 };
24877
24878 // wrapper for "remove" operation; remove value at ptr
24879 const auto operation_remove = [this, & result](json_pointer & ptr)
24880 {
24881 // get reference to parent of JSON pointer ptr
24882 const auto last_path = ptr.back();
24883 ptr.pop_back();
24884 basic_json& parent = result.at(ptr);
24885
24886 // remove child
24887 if (parent.is_object())
24888 {
24889 // perform range check
24890 auto it = parent.find(last_path);
24891 if (JSON_HEDLEY_LIKELY(it != parent.end()))
24892 {
24893 parent.erase(it);
24894 }
24895 else
24896 {
24897 JSON_THROW(out_of_range::create(403, detail::concat("key '", last_path, "' not found"), this));
24898 }
24899 }
24900 else if (parent.is_array())
24901 {
24902 // note erase performs range check
24903 parent.erase(json_pointer::template array_index<basic_json_t>(last_path));
24904 }
24905 };
24906
24907 // type check: top level value must be an array
24908 if (JSON_HEDLEY_UNLIKELY(!json_patch.is_array()))
24909 {
24910 JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", &json_patch));
24911 }
24912
24913 // iterate and apply the operations
24914 for (const auto& val : json_patch)
24915 {
24916 // wrapper to get a value for an operation
24917 const auto get_value = [&val](const string_t& op,
24918 const string_t& member,
24919 bool string_type) -> basic_json &
24920 {
24921 // find value
24922 auto it = val.m_data.m_value.object->find(member);
24923
24924 // context-sensitive error message
24925 const auto error_msg = (op == "op") ? "operation" : detail::concat("operation '", op, '\''); // NOLINT(bugprone-unused-local-non-trivial-variable)
24926
24927 // check if desired value is present
24928 if (JSON_HEDLEY_UNLIKELY(it == val.m_data.m_value.object->end()))
24929 {
24930 // NOLINTNEXTLINE(performance-inefficient-string-concatenation)
24931 JSON_THROW(parse_error::create(105, 0, detail::concat(error_msg, " must have member '", member, "'"), &val));
24932 }
24933
24934 // check if result is of type string
24935 if (JSON_HEDLEY_UNLIKELY(string_type && !it->second.is_string()))
24936 {
24937 // NOLINTNEXTLINE(performance-inefficient-string-concatenation)
24938 JSON_THROW(parse_error::create(105, 0, detail::concat(error_msg, " must have string member '", member, "'"), &val));
24939 }
24940
24941 // no error: return value
24942 return it->second;
24943 };
24944
24945 // type check: every element of the array must be an object
24946 if (JSON_HEDLEY_UNLIKELY(!val.is_object()))
24947 {
24948 JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", &val));
24949 }
24950
24951 // collect mandatory members
24952 const auto op = get_value("op", "op", true).template get<string_t>();
24953 const auto path = get_value(op, "path", true).template get<string_t>();
24954 json_pointer ptr(path);
24955
24956 switch (get_op(op))
24957 {
24958 case patch_operations::add:
24959 {
24960 operation_add(ptr, get_value("add", "value", false));
24961 break;
24962 }
24963
24964 case patch_operations::remove:
24965 {
24966 operation_remove(ptr);
24967 break;
24968 }
24969
24970 case patch_operations::replace:
24971 {
24972 // the "path" location must exist - use at()
24973 result.at(ptr) = get_value("replace", "value", false);
24974 break;
24975 }
24976
24977 case patch_operations::move:
24978 {
24979 const auto from_path = get_value("move", "from", true).template get<string_t>();
24980 json_pointer from_ptr(from_path);
24981
24982 // the "from" location must exist - use at()
24983 basic_json const v = result.at(from_ptr);
24984
24985 // The move operation is functionally identical to a
24986 // "remove" operation on the "from" location, followed
24987 // immediately by an "add" operation at the target
24988 // location with the value that was just removed.
24989 operation_remove(from_ptr);
24990 operation_add(ptr, v);
24991 break;
24992 }
24993
24994 case patch_operations::copy:
24995 {
24996 const auto from_path = get_value("copy", "from", true).template get<string_t>();
24997 const json_pointer from_ptr(from_path);
24998
24999 // the "from" location must exist - use at()
25000 basic_json const v = result.at(from_ptr);
25001
25002 // The copy is functionally identical to an "add"
25003 // operation at the target location using the value
25004 // specified in the "from" member.
25005 operation_add(ptr, v);
25006 break;
25007 }
25008
25009 case patch_operations::test:
25010 {
25011 bool success = false;
25012 JSON_TRY
25013 {
25014 // check if "value" matches the one at "path"
25015 // the "path" location must exist - use at()
25016 success = (result.at(ptr) == get_value("test", "value", false));
25017 }
25018 JSON_INTERNAL_CATCH (out_of_range&)
25019 {
25020 // ignore out of range errors: success remains false
25021 }
25022
25023 // throw an exception if test fails
25024 if (JSON_HEDLEY_UNLIKELY(!success))
25025 {
25026 JSON_THROW(other_error::create(501, detail::concat("unsuccessful: ", val.dump()), &val));
25027 }
25028
25029 break;
25030 }
25031
25032 case patch_operations::invalid:
25033 default:
25034 {
25035 // op must be "add", "remove", "replace", "move", "copy", or
25036 // "test"
25037 JSON_THROW(parse_error::create(105, 0, detail::concat("operation value '", op, "' is invalid"), &val));
25038 }
25039 }
25040 }
25041 }
25042
25043 /// @brief applies a JSON patch to a copy of the current object
25044 /// @sa https://json.nlohmann.me/api/basic_json/patch/
25045 basic_json patch(const basic_json& json_patch) const
25046 {
25047 basic_json result = *this;
25048 result.patch_inplace(json_patch);
25049 return result;
25050 }
25051
25052 /// @brief creates a diff as a JSON patch
25053 /// @sa https://json.nlohmann.me/api/basic_json/diff/
25054 JSON_HEDLEY_WARN_UNUSED_RESULT
25055 static basic_json diff(const basic_json& source, const basic_json& target,
25056 const string_t& path = "")
25057 {
25058 // the patch
25059 basic_json result(value_t::array);
25060
25061 // if the values are the same, return empty patch
25062 if (source == target)
25063 {
25064 return result;
25065 }
25066
25067 if (source.type() != target.type())
25068 {
25069 // different types: replace value
25070 result.push_back(
25071 {
25072 {"op", "replace"}, {"path", path}, {"value", target}
25073 });
25074 return result;
25075 }
25076
25077 switch (source.type())
25078 {
25079 case value_t::array:
25080 {
25081 // first pass: traverse common elements
25082 std::size_t i = 0;
25083 while (i < source.size() && i < target.size())
25084 {
25085 // recursive call to compare array values at index i
25086 auto temp_diff = diff(source[i], target[i], detail::concat<string_t>(path, '/', detail::to_string<string_t>(i)));
25087 result.insert(result.end(), temp_diff.begin(), temp_diff.end());
25088 ++i;
25089 }
25090
25091 // We now reached the end of at least one array
25092 // in a second pass, traverse the remaining elements
25093
25094 // remove my remaining elements
25095 const auto end_index = static_cast<difference_type>(result.size());
25096 while (i < source.size())
25097 {
25098 // add operations in reverse order to avoid invalid
25099 // indices
25100 result.insert(result.begin() + end_index, object(
25101 {
25102 {"op", "remove"},
25103 {"path", detail::concat<string_t>(path, '/', detail::to_string<string_t>(i))}
25104 }));
25105 ++i;
25106 }
25107
25108 // add other remaining elements
25109 while (i < target.size())
25110 {
25111 result.push_back(
25112 {
25113 {"op", "add"},
25114 {"path", detail::concat<string_t>(path, "/-")},
25115 {"value", target[i]}
25116 });
25117 ++i;
25118 }
25119
25120 break;
25121 }
25122
25123 case value_t::object:
25124 {
25125 // first pass: traverse this object's elements
25126 for (auto it = source.cbegin(); it != source.cend(); ++it)
25127 {
25128 // escape the key name to be used in a JSON patch
25129 const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
25130
25131 if (target.find(it.key()) != target.end())
25132 {
25133 // recursive call to compare object values at key it
25134 auto temp_diff = diff(it.value(), target[it.key()], path_key);
25135 result.insert(result.end(), temp_diff.begin(), temp_diff.end());
25136 }
25137 else
25138 {
25139 // found a key that is not in o -> remove it
25140 result.push_back(object(
25141 {
25142 {"op", "remove"}, {"path", path_key}
25143 }));
25144 }
25145 }
25146
25147 // second pass: traverse other object's elements
25148 for (auto it = target.cbegin(); it != target.cend(); ++it)
25149 {
25150 if (source.find(it.key()) == source.end())
25151 {
25152 // found a key that is not in this -> add it
25153 const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
25154 result.push_back(
25155 {
25156 {"op", "add"}, {"path", path_key},
25157 {"value", it.value()}
25158 });
25159 }
25160 }
25161
25162 break;
25163 }
25164
25165 case value_t::null:
25166 case value_t::string:
25167 case value_t::boolean:
25168 case value_t::number_integer:
25169 case value_t::number_unsigned:
25170 case value_t::number_float:
25171 case value_t::binary:
25172 case value_t::discarded:
25173 default:
25174 {
25175 // both primitive type: replace value
25176 result.push_back(
25177 {
25178 {"op", "replace"}, {"path", path}, {"value", target}
25179 });
25180 break;
25181 }
25182 }
25183
25184 return result;
25185 }
25186 /// @}
25187
25188 ////////////////////////////////
25189 // JSON Merge Patch functions //
25190 ////////////////////////////////
25191
25192 /// @name JSON Merge Patch functions
25193 /// @{
25194
25195 /// @brief applies a JSON Merge Patch
25196 /// @sa https://json.nlohmann.me/api/basic_json/merge_patch/
25197 void merge_patch(const basic_json& apply_patch)
25198 {
25199 if (apply_patch.is_object())
25200 {
25201 if (!is_object())
25202 {
25203 *this = object();
25204 }
25205 for (auto it = apply_patch.begin(); it != apply_patch.end(); ++it)
25206 {
25207 if (it.value().is_null())
25208 {
25209 erase(it.key());
25210 }
25211 else
25212 {
25213 operator[](it.key()).merge_patch(it.value());
25214 }
25215 }
25216 }
25217 else
25218 {
25219 *this = apply_patch;
25220 }
25221 }
25222
25223 /// @}
25224 };
25225
25226 /// @brief user-defined to_string function for JSON values
25227 /// @sa https://json.nlohmann.me/api/basic_json/to_string/
25228 NLOHMANN_BASIC_JSON_TPL_DECLARATION
25229 std::string to_string(const NLOHMANN_BASIC_JSON_TPL& j)
25230 {
25231 return j.dump();
25232 }
25233
25234 inline namespace literals
25235 {
25236 inline namespace json_literals
25237 {
25238
25239 /// @brief user-defined string literal for JSON values
25240 /// @sa https://json.nlohmann.me/api/basic_json/operator_literal_json/
25241 JSON_HEDLEY_NON_NULL(1)
25242 #if !defined(JSON_HEDLEY_GCC_VERSION) || JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0)
25243 inline nlohmann::json operator ""_json(const char* s, std::size_t n)
25244 #else
25245 inline nlohmann::json operator "" _json(const char* s, std::size_t n)
25246 #endif
25247 {
25248 return nlohmann::json::parse(s, s + n);
25249 }
25250
25251 /// @brief user-defined string literal for JSON pointer
25252 /// @sa https://json.nlohmann.me/api/basic_json/operator_literal_json_pointer/
25253 JSON_HEDLEY_NON_NULL(1)
25254 #if !defined(JSON_HEDLEY_GCC_VERSION) || JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0)
25255 inline nlohmann::json::json_pointer operator ""_json_pointer(const char* s, std::size_t n)
25256 #else
25257 inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std::size_t n)
25258 #endif
25259 {
25260 return nlohmann::json::json_pointer(std::string(s, n));
25261 }
25262
25263 } // namespace json_literals
25264 } // namespace literals
25265 NLOHMANN_JSON_NAMESPACE_END
25266
25267 ///////////////////////
25268 // nonmember support //
25269 ///////////////////////
25270
25271 namespace std // NOLINT(cert-dcl58-cpp)
25272 {
25273
25274 /// @brief hash value for JSON objects
25275 /// @sa https://json.nlohmann.me/api/basic_json/std_hash/
25276 NLOHMANN_BASIC_JSON_TPL_DECLARATION
25277 struct hash<nlohmann::NLOHMANN_BASIC_JSON_TPL> // NOLINT(cert-dcl58-cpp)
25278 {
25279 std::size_t operator()(const nlohmann::NLOHMANN_BASIC_JSON_TPL& j) const
25280 {
25281 return nlohmann::detail::hash(j);
25282 }
25283 };
25284
25285 // specialization for std::less<value_t>
25286 template<>
25287 struct less< ::nlohmann::detail::value_t> // do not remove the space after '<', see https://github.com/nlohmann/json/pull/679
25288 {
25289 /*!
25290 @brief compare two value_t enum values
25291 @since version 3.0.0
25292 */
25293 bool operator()(::nlohmann::detail::value_t lhs,
25294 ::nlohmann::detail::value_t rhs) const noexcept
25295 {
25296 #if JSON_HAS_THREE_WAY_COMPARISON
25297 return std::is_lt(lhs <=> rhs); // *NOPAD*
25298 #else
25299 return ::nlohmann::detail::operator<(lhs, rhs);
25300 #endif
25301 }
25302 };
25303
25304 // C++20 prohibit function specialization in the std namespace.
25305 #ifndef JSON_HAS_CPP_20
25306
25307 /// @brief exchanges the values of two JSON objects
25308 /// @sa https://json.nlohmann.me/api/basic_json/std_swap/
25309 NLOHMANN_BASIC_JSON_TPL_DECLARATION
25310 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)
25311 is_nothrow_move_constructible<nlohmann::NLOHMANN_BASIC_JSON_TPL>::value&& // NOLINT(misc-redundant-expression,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
25312 is_nothrow_move_assignable<nlohmann::NLOHMANN_BASIC_JSON_TPL>::value)
25313 {
25314 j1.swap(j2);
25315 }
25316
25317 #endif
25318
25319 } // namespace std
25320
25321 #if JSON_USE_GLOBAL_UDLS
25322 #if !defined(JSON_HEDLEY_GCC_VERSION) || JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0)
25323 using nlohmann::literals::json_literals::operator ""_json; // NOLINT(misc-unused-using-decls,google-global-names-in-headers)
25324 using nlohmann::literals::json_literals::operator ""_json_pointer; //NOLINT(misc-unused-using-decls,google-global-names-in-headers)
25325 #else
25326 using nlohmann::literals::json_literals::operator "" _json; // NOLINT(misc-unused-using-decls,google-global-names-in-headers)
25327 using nlohmann::literals::json_literals::operator "" _json_pointer; //NOLINT(misc-unused-using-decls,google-global-names-in-headers)
25328 #endif
25329 #endif
25330
25331 // #include <nlohmann/detail/macro_unscope.hpp>
25332 // __ _____ _____ _____
25333 // __| | __| | | | JSON for Modern C++
25334 // | | |__ | | | | | | version 3.12.0
25335 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
25336 //
25337 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
25338 // SPDX-License-Identifier: MIT
25339
25340
25341
25342 // restore clang diagnostic settings
25343 #if defined(__clang__)
25344 #pragma clang diagnostic pop
25345 #endif
25346
25347 // clean up
25348 #undef JSON_ASSERT
25349 #undef JSON_INTERNAL_CATCH
25350 #undef JSON_THROW
25351 #undef JSON_PRIVATE_UNLESS_TESTED
25352 #undef NLOHMANN_BASIC_JSON_TPL_DECLARATION
25353 #undef NLOHMANN_BASIC_JSON_TPL
25354 #undef JSON_EXPLICIT
25355 #undef NLOHMANN_CAN_CALL_STD_FUNC_IMPL
25356 #undef JSON_INLINE_VARIABLE
25357 #undef JSON_NO_UNIQUE_ADDRESS
25358 #undef JSON_DISABLE_ENUM_SERIALIZATION
25359 #undef JSON_USE_GLOBAL_UDLS
25360
25361 #ifndef JSON_TEST_KEEP_MACROS
25362 #undef JSON_CATCH
25363 #undef JSON_TRY
25364 #undef JSON_HAS_CPP_11
25365 #undef JSON_HAS_CPP_14
25366 #undef JSON_HAS_CPP_17
25367 #undef JSON_HAS_CPP_20
25368 #undef JSON_HAS_CPP_23
25369 #undef JSON_HAS_FILESYSTEM
25370 #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
25371 #undef JSON_HAS_THREE_WAY_COMPARISON
25372 #undef JSON_HAS_RANGES
25373 #undef JSON_HAS_STATIC_RTTI
25374 #undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
25375 #endif
25376
25377 // #include <nlohmann/thirdparty/hedley/hedley_undef.hpp>
25378 // __ _____ _____ _____
25379 // __| | __| | | | JSON for Modern C++
25380 // | | |__ | | | | | | version 3.12.0
25381 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
25382 //
25383 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
25384 // SPDX-License-Identifier: MIT
25385
25386
25387
25388 #undef JSON_HEDLEY_ALWAYS_INLINE
25389 #undef JSON_HEDLEY_ARM_VERSION
25390 #undef JSON_HEDLEY_ARM_VERSION_CHECK
25391 #undef JSON_HEDLEY_ARRAY_PARAM
25392 #undef JSON_HEDLEY_ASSUME
25393 #undef JSON_HEDLEY_BEGIN_C_DECLS
25394 #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE
25395 #undef JSON_HEDLEY_CLANG_HAS_BUILTIN
25396 #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE
25397 #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE
25398 #undef JSON_HEDLEY_CLANG_HAS_EXTENSION
25399 #undef JSON_HEDLEY_CLANG_HAS_FEATURE
25400 #undef JSON_HEDLEY_CLANG_HAS_WARNING
25401 #undef JSON_HEDLEY_COMPCERT_VERSION
25402 #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK
25403 #undef JSON_HEDLEY_CONCAT
25404 #undef JSON_HEDLEY_CONCAT3
25405 #undef JSON_HEDLEY_CONCAT3_EX
25406 #undef JSON_HEDLEY_CONCAT_EX
25407 #undef JSON_HEDLEY_CONST
25408 #undef JSON_HEDLEY_CONSTEXPR
25409 #undef JSON_HEDLEY_CONST_CAST
25410 #undef JSON_HEDLEY_CPP_CAST
25411 #undef JSON_HEDLEY_CRAY_VERSION
25412 #undef JSON_HEDLEY_CRAY_VERSION_CHECK
25413 #undef JSON_HEDLEY_C_DECL
25414 #undef JSON_HEDLEY_DEPRECATED
25415 #undef JSON_HEDLEY_DEPRECATED_FOR
25416 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
25417 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_
25418 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
25419 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
25420 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
25421 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
25422 #undef JSON_HEDLEY_DIAGNOSTIC_POP
25423 #undef JSON_HEDLEY_DIAGNOSTIC_PUSH
25424 #undef JSON_HEDLEY_DMC_VERSION
25425 #undef JSON_HEDLEY_DMC_VERSION_CHECK
25426 #undef JSON_HEDLEY_EMPTY_BASES
25427 #undef JSON_HEDLEY_EMSCRIPTEN_VERSION
25428 #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK
25429 #undef JSON_HEDLEY_END_C_DECLS
25430 #undef JSON_HEDLEY_FLAGS
25431 #undef JSON_HEDLEY_FLAGS_CAST
25432 #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE
25433 #undef JSON_HEDLEY_GCC_HAS_BUILTIN
25434 #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE
25435 #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE
25436 #undef JSON_HEDLEY_GCC_HAS_EXTENSION
25437 #undef JSON_HEDLEY_GCC_HAS_FEATURE
25438 #undef JSON_HEDLEY_GCC_HAS_WARNING
25439 #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK
25440 #undef JSON_HEDLEY_GCC_VERSION
25441 #undef JSON_HEDLEY_GCC_VERSION_CHECK
25442 #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE
25443 #undef JSON_HEDLEY_GNUC_HAS_BUILTIN
25444 #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE
25445 #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE
25446 #undef JSON_HEDLEY_GNUC_HAS_EXTENSION
25447 #undef JSON_HEDLEY_GNUC_HAS_FEATURE
25448 #undef JSON_HEDLEY_GNUC_HAS_WARNING
25449 #undef JSON_HEDLEY_GNUC_VERSION
25450 #undef JSON_HEDLEY_GNUC_VERSION_CHECK
25451 #undef JSON_HEDLEY_HAS_ATTRIBUTE
25452 #undef JSON_HEDLEY_HAS_BUILTIN
25453 #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE
25454 #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS
25455 #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE
25456 #undef JSON_HEDLEY_HAS_EXTENSION
25457 #undef JSON_HEDLEY_HAS_FEATURE
25458 #undef JSON_HEDLEY_HAS_WARNING
25459 #undef JSON_HEDLEY_IAR_VERSION
25460 #undef JSON_HEDLEY_IAR_VERSION_CHECK
25461 #undef JSON_HEDLEY_IBM_VERSION
25462 #undef JSON_HEDLEY_IBM_VERSION_CHECK
25463 #undef JSON_HEDLEY_IMPORT
25464 #undef JSON_HEDLEY_INLINE
25465 #undef JSON_HEDLEY_INTEL_CL_VERSION
25466 #undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK
25467 #undef JSON_HEDLEY_INTEL_VERSION
25468 #undef JSON_HEDLEY_INTEL_VERSION_CHECK
25469 #undef JSON_HEDLEY_IS_CONSTANT
25470 #undef JSON_HEDLEY_IS_CONSTEXPR_
25471 #undef JSON_HEDLEY_LIKELY
25472 #undef JSON_HEDLEY_MALLOC
25473 #undef JSON_HEDLEY_MCST_LCC_VERSION
25474 #undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK
25475 #undef JSON_HEDLEY_MESSAGE
25476 #undef JSON_HEDLEY_MSVC_VERSION
25477 #undef JSON_HEDLEY_MSVC_VERSION_CHECK
25478 #undef JSON_HEDLEY_NEVER_INLINE
25479 #undef JSON_HEDLEY_NON_NULL
25480 #undef JSON_HEDLEY_NO_ESCAPE
25481 #undef JSON_HEDLEY_NO_RETURN
25482 #undef JSON_HEDLEY_NO_THROW
25483 #undef JSON_HEDLEY_NULL
25484 #undef JSON_HEDLEY_PELLES_VERSION
25485 #undef JSON_HEDLEY_PELLES_VERSION_CHECK
25486 #undef JSON_HEDLEY_PGI_VERSION
25487 #undef JSON_HEDLEY_PGI_VERSION_CHECK
25488 #undef JSON_HEDLEY_PREDICT
25489 #undef JSON_HEDLEY_PRINTF_FORMAT
25490 #undef JSON_HEDLEY_PRIVATE
25491 #undef JSON_HEDLEY_PUBLIC
25492 #undef JSON_HEDLEY_PURE
25493 #undef JSON_HEDLEY_REINTERPRET_CAST
25494 #undef JSON_HEDLEY_REQUIRE
25495 #undef JSON_HEDLEY_REQUIRE_CONSTEXPR
25496 #undef JSON_HEDLEY_REQUIRE_MSG
25497 #undef JSON_HEDLEY_RESTRICT
25498 #undef JSON_HEDLEY_RETURNS_NON_NULL
25499 #undef JSON_HEDLEY_SENTINEL
25500 #undef JSON_HEDLEY_STATIC_ASSERT
25501 #undef JSON_HEDLEY_STATIC_CAST
25502 #undef JSON_HEDLEY_STRINGIFY
25503 #undef JSON_HEDLEY_STRINGIFY_EX
25504 #undef JSON_HEDLEY_SUNPRO_VERSION
25505 #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK
25506 #undef JSON_HEDLEY_TINYC_VERSION
25507 #undef JSON_HEDLEY_TINYC_VERSION_CHECK
25508 #undef JSON_HEDLEY_TI_ARMCL_VERSION
25509 #undef JSON_HEDLEY_TI_ARMCL_VERSION_CHECK
25510 #undef JSON_HEDLEY_TI_CL2000_VERSION
25511 #undef JSON_HEDLEY_TI_CL2000_VERSION_CHECK
25512 #undef JSON_HEDLEY_TI_CL430_VERSION
25513 #undef JSON_HEDLEY_TI_CL430_VERSION_CHECK
25514 #undef JSON_HEDLEY_TI_CL6X_VERSION
25515 #undef JSON_HEDLEY_TI_CL6X_VERSION_CHECK
25516 #undef JSON_HEDLEY_TI_CL7X_VERSION
25517 #undef JSON_HEDLEY_TI_CL7X_VERSION_CHECK
25518 #undef JSON_HEDLEY_TI_CLPRU_VERSION
25519 #undef JSON_HEDLEY_TI_CLPRU_VERSION_CHECK
25520 #undef JSON_HEDLEY_TI_VERSION
25521 #undef JSON_HEDLEY_TI_VERSION_CHECK
25522 #undef JSON_HEDLEY_UNAVAILABLE
25523 #undef JSON_HEDLEY_UNLIKELY
25524 #undef JSON_HEDLEY_UNPREDICTABLE
25525 #undef JSON_HEDLEY_UNREACHABLE
25526 #undef JSON_HEDLEY_UNREACHABLE_RETURN
25527 #undef JSON_HEDLEY_VERSION
25528 #undef JSON_HEDLEY_VERSION_DECODE_MAJOR
25529 #undef JSON_HEDLEY_VERSION_DECODE_MINOR
25530 #undef JSON_HEDLEY_VERSION_DECODE_REVISION
25531 #undef JSON_HEDLEY_VERSION_ENCODE
25532 #undef JSON_HEDLEY_WARNING
25533 #undef JSON_HEDLEY_WARN_UNUSED_RESULT
25534 #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG
25535 #undef JSON_HEDLEY_FALL_THROUGH
25536
25537
25538
25539 #endif // INCLUDE_NLOHMANN_JSON_HPP_