Debian Codegraph @

single_include/nlohmann/json.hpp

raw symbols ◀ ▶
Source reconstructed at patch state 2 of 6 — not the installed (fully patched) source.
6 Debian patches touch this file
1 // __ _____ _____ _____
2 // __| | __| | | | JSON for Modern C++
3 // | | |__ | | | | | | version 3.12.0
4 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
5 //
6 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
7 // SPDX-License-Identifier: MIT
8
9 /****************************************************************************\
10 * Note on documentation: The source files contain links to the online *
11 * documentation of the public API at https://json.nlohmann.me. This URL *
12 * contains the most recent documentation and should also be applicable to *
13 * previous versions; documentation for deprecated functions is not *
14 * removed, but marked deprecated. See "Generate documentation" section in *
15 * file docs/README.md. *
16 \****************************************************************************/
17
18 #ifndef INCLUDE_NLOHMANN_JSON_HPP_
19 #define INCLUDE_NLOHMANN_JSON_HPP_
20
21 #include <algorithm> // all_of, find, for_each
22 #include <cstddef> // nullptr_t, ptrdiff_t, size_t
23 #include <functional> // hash, less
24 #include <initializer_list> // initializer_list
25 #ifndef JSON_NO_IO
26 #include <iosfwd> // istream, ostream
27 #endif // JSON_NO_IO
28 #include <iterator> // random_access_iterator_tag
29 #include <memory> // unique_ptr
30 #include <string> // string, stoi, to_string
31 #include <utility> // declval, forward, move, pair, swap
32 #include <vector> // vector
33
34 // #include <nlohmann/adl_serializer.hpp>
35 // __ _____ _____ _____
36 // __| | __| | | | JSON for Modern C++
37 // | | |__ | | | | | | version 3.12.0
38 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
39 //
40 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
41 // SPDX-License-Identifier: MIT
42
43
44
45 #include <utility>
46
47 // #include <nlohmann/detail/abi_macros.hpp>
48 // __ _____ _____ _____
49 // __| | __| | | | JSON for Modern C++
50 // | | |__ | | | | | | version 3.12.0
51 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
52 //
53 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
54 // SPDX-License-Identifier: MIT
55
56
57
58 // This file contains all macro definitions affecting or depending on the ABI
59
60 #ifndef JSON_SKIP_LIBRARY_VERSION_CHECK
61 #if defined(NLOHMANN_JSON_VERSION_MAJOR) && defined(NLOHMANN_JSON_VERSION_MINOR) && defined(NLOHMANN_JSON_VERSION_PATCH)
62 #if NLOHMANN_JSON_VERSION_MAJOR != 3 || NLOHMANN_JSON_VERSION_MINOR != 12 || NLOHMANN_JSON_VERSION_PATCH != 0
63 #warning "Already included a different version of the library!"
64 #endif
65 #endif
66 #endif
67
68 #define NLOHMANN_JSON_VERSION_MAJOR 3 // NOLINT(modernize-macro-to-enum)
69 #define NLOHMANN_JSON_VERSION_MINOR 12 // NOLINT(modernize-macro-to-enum)
70 #define NLOHMANN_JSON_VERSION_PATCH 0 // NOLINT(modernize-macro-to-enum)
71
72 #ifndef JSON_DIAGNOSTICS
73 #define JSON_DIAGNOSTICS 0
74 #endif
75
76 #ifndef JSON_DIAGNOSTIC_POSITIONS
77 #define JSON_DIAGNOSTIC_POSITIONS 0
78 #endif
79
80 #ifndef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
81 #define JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON 0
82 #endif
83
84 #if JSON_DIAGNOSTICS
85 #define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS _diag
86 #else
87 #define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS
88 #endif
89
90 #if JSON_DIAGNOSTIC_POSITIONS
91 #define NLOHMANN_JSON_ABI_TAG_DIAGNOSTIC_POSITIONS _dp
92 #else
93 #define NLOHMANN_JSON_ABI_TAG_DIAGNOSTIC_POSITIONS
94 #endif
95
96 #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
97 #define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON _ldvcmp
98 #else
99 #define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON
100 #endif
101
102 #ifndef NLOHMANN_JSON_NAMESPACE_NO_VERSION
103 #define NLOHMANN_JSON_NAMESPACE_NO_VERSION 0
104 #endif
105
106 // Construct the namespace ABI tags component
107 #define NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b, c) json_abi ## a ## b ## c
108 #define NLOHMANN_JSON_ABI_TAGS_CONCAT(a, b, c) \
109 NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b, c)
110
111 #define NLOHMANN_JSON_ABI_TAGS \
112 NLOHMANN_JSON_ABI_TAGS_CONCAT( \
113 NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS, \
114 NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON, \
115 NLOHMANN_JSON_ABI_TAG_DIAGNOSTIC_POSITIONS)
116
117 // Construct the namespace version component
118 #define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch) \
119 _v ## major ## _ ## minor ## _ ## patch
120 #define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(major, minor, patch) \
121 NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch)
122
123 #if NLOHMANN_JSON_NAMESPACE_NO_VERSION
124 #define NLOHMANN_JSON_NAMESPACE_VERSION
125 #else
126 #define NLOHMANN_JSON_NAMESPACE_VERSION \
127 NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(NLOHMANN_JSON_VERSION_MAJOR, \
128 NLOHMANN_JSON_VERSION_MINOR, \
129 NLOHMANN_JSON_VERSION_PATCH)
130 #endif
131
132 // Combine namespace components
133 #define NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b) a ## b
134 #define NLOHMANN_JSON_NAMESPACE_CONCAT(a, b) \
135 NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b)
136
137 #ifndef NLOHMANN_JSON_NAMESPACE
138 #define NLOHMANN_JSON_NAMESPACE \
139 nlohmann::NLOHMANN_JSON_NAMESPACE_CONCAT( \
140 NLOHMANN_JSON_ABI_TAGS, \
141 NLOHMANN_JSON_NAMESPACE_VERSION)
142 #endif
143
144 #ifndef NLOHMANN_JSON_NAMESPACE_BEGIN
145 #define NLOHMANN_JSON_NAMESPACE_BEGIN \
146 namespace nlohmann \
147 { \
148 inline namespace NLOHMANN_JSON_NAMESPACE_CONCAT( \
149 NLOHMANN_JSON_ABI_TAGS, \
150 NLOHMANN_JSON_NAMESPACE_VERSION) \
151 {
152 #endif
153
154 #ifndef NLOHMANN_JSON_NAMESPACE_END
155 #define NLOHMANN_JSON_NAMESPACE_END \
156 } /* namespace (inline namespace) NOLINT(readability/namespace) */ \
157 } // namespace nlohmann
158 #endif
159
160 // #include <nlohmann/detail/conversions/from_json.hpp>
161 // __ _____ _____ _____
162 // __| | __| | | | JSON for Modern C++
163 // | | |__ | | | | | | version 3.12.0
164 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
165 //
166 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
167 // SPDX-License-Identifier: MIT
168
169
170
171 #include <algorithm> // transform
172 #include <array> // array
173 #include <forward_list> // forward_list
174 #include <iterator> // inserter, front_inserter, end
175 #include <map> // map
176 #include <string> // string
177 #include <tuple> // tuple, make_tuple
178 #include <type_traits> // is_arithmetic, is_same, is_enum, underlying_type, is_convertible
179 #include <unordered_map> // unordered_map
180 #include <utility> // pair, declval
181 #include <valarray> // valarray
182
183 // #include <nlohmann/detail/exceptions.hpp>
184 // __ _____ _____ _____
185 // __| | __| | | | JSON for Modern C++
186 // | | |__ | | | | | | version 3.12.0
187 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
188 //
189 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
190 // SPDX-License-Identifier: MIT
191
192
193
194 #include <cstddef> // nullptr_t
195 #include <exception> // exception
196 #if JSON_DIAGNOSTICS
197 #include <numeric> // accumulate
198 #endif
199 #include <stdexcept> // runtime_error
200 #include <string> // to_string
201 #include <vector> // vector
202
203 // #include <nlohmann/detail/value_t.hpp>
204 // __ _____ _____ _____
205 // __| | __| | | | JSON for Modern C++
206 // | | |__ | | | | | | version 3.12.0
207 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
208 //
209 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
210 // SPDX-License-Identifier: MIT
211
212
213
214 #include <array> // array
215 #include <cstddef> // size_t
216 #include <cstdint> // uint8_t
217 #include <string> // string
218
219 // #include <nlohmann/detail/macro_scope.hpp>
220 // __ _____ _____ _____
221 // __| | __| | | | JSON for Modern C++
222 // | | |__ | | | | | | version 3.12.0
223 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
224 //
225 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
226 // SPDX-License-Identifier: MIT
227
228
229
230 #include <utility> // declval, pair
231 // #include <nlohmann/detail/meta/detected.hpp>
232 // __ _____ _____ _____
233 // __| | __| | | | JSON for Modern C++
234 // | | |__ | | | | | | version 3.12.0
235 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
236 //
237 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
238 // SPDX-License-Identifier: MIT
239
240
241
242 #include <type_traits>
243
244 // #include <nlohmann/detail/meta/void_t.hpp>
245 // __ _____ _____ _____
246 // __| | __| | | | JSON for Modern C++
247 // | | |__ | | | | | | version 3.12.0
248 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
249 //
250 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
251 // SPDX-License-Identifier: MIT
252
253
254
255 // #include <nlohmann/detail/abi_macros.hpp>
256
257
258 NLOHMANN_JSON_NAMESPACE_BEGIN
259 namespace detail
260 {
261
262 template<typename ...Ts> struct make_void
263 {
264 using type = void;
265 };
266 template<typename ...Ts> using void_t = typename make_void<Ts...>::type;
267
268 } // namespace detail
269 NLOHMANN_JSON_NAMESPACE_END
270
271
272 NLOHMANN_JSON_NAMESPACE_BEGIN
273 namespace detail
274 {
275
276 // https://en.cppreference.com/w/cpp/experimental/is_detected
277 struct nonesuch
278 {
279 nonesuch() = delete;
280 ~nonesuch() = delete;
281 nonesuch(nonesuch const&) = delete;
282 nonesuch(nonesuch const&&) = delete;
283 void operator=(nonesuch const&) = delete;
284 void operator=(nonesuch&&) = delete;
285 };
286
287 template<class Default,
288 class AlwaysVoid,
289 template<class...> class Op,
290 class... Args>
291 struct detector
292 {
293 using value_t = std::false_type;
294 using type = Default;
295 };
296
297 template<class Default, template<class...> class Op, class... Args>
298 struct detector<Default, void_t<Op<Args...>>, Op, Args...>
299 {
300 using value_t = std::true_type;
301 using type = Op<Args...>;
302 };
303
304 template<template<class...> class Op, class... Args>
305 using is_detected = typename detector<nonesuch, void, Op, Args...>::value_t;
306
307 template<template<class...> class Op, class... Args>
308 struct is_detected_lazy : is_detected<Op, Args...> { };
309
310 template<template<class...> class Op, class... Args>
311 using detected_t = typename detector<nonesuch, void, Op, Args...>::type;
312
313 template<class Default, template<class...> class Op, class... Args>
314 using detected_or = detector<Default, void, Op, Args...>;
315
316 template<class Default, template<class...> class Op, class... Args>
317 using detected_or_t = typename detected_or<Default, Op, Args...>::type;
318
319 template<class Expected, template<class...> class Op, class... Args>
320 using is_detected_exact = std::is_same<Expected, detected_t<Op, Args...>>;
321
322 template<class To, template<class...> class Op, class... Args>
323 using is_detected_convertible =
324 std::is_convertible<detected_t<Op, Args...>, To>;
325
326 } // namespace detail
327 NLOHMANN_JSON_NAMESPACE_END
328
329 // #include <nlohmann/thirdparty/hedley/hedley.hpp>
330
331
332 // __ _____ _____ _____
333 // __| | __| | | | JSON for Modern C++
334 // | | |__ | | | | | | version 3.12.0
335 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
336 //
337 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
338 // SPDX-FileCopyrightText: 2016 - 2021 Evan Nemerson <[email protected]>
339 // SPDX-License-Identifier: MIT
340
341 /* Hedley - https://nemequ.github.io/hedley
342 * Created by Evan Nemerson <[email protected]>
343 */
344
345 #if !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < 15)
346 #if defined(JSON_HEDLEY_VERSION)
347 #undef JSON_HEDLEY_VERSION
348 #endif
349 #define JSON_HEDLEY_VERSION 15
350
351 #if defined(JSON_HEDLEY_STRINGIFY_EX)
352 #undef JSON_HEDLEY_STRINGIFY_EX
353 #endif
354 #define JSON_HEDLEY_STRINGIFY_EX(x) #x
355
356 #if defined(JSON_HEDLEY_STRINGIFY)
357 #undef JSON_HEDLEY_STRINGIFY
358 #endif
359 #define JSON_HEDLEY_STRINGIFY(x) JSON_HEDLEY_STRINGIFY_EX(x)
360
361 #if defined(JSON_HEDLEY_CONCAT_EX)
362 #undef JSON_HEDLEY_CONCAT_EX
363 #endif
364 #define JSON_HEDLEY_CONCAT_EX(a,b) a##b
365
366 #if defined(JSON_HEDLEY_CONCAT)
367 #undef JSON_HEDLEY_CONCAT
368 #endif
369 #define JSON_HEDLEY_CONCAT(a,b) JSON_HEDLEY_CONCAT_EX(a,b)
370
371 #if defined(JSON_HEDLEY_CONCAT3_EX)
372 #undef JSON_HEDLEY_CONCAT3_EX
373 #endif
374 #define JSON_HEDLEY_CONCAT3_EX(a,b,c) a##b##c
375
376 #if defined(JSON_HEDLEY_CONCAT3)
377 #undef JSON_HEDLEY_CONCAT3
378 #endif
379 #define JSON_HEDLEY_CONCAT3(a,b,c) JSON_HEDLEY_CONCAT3_EX(a,b,c)
380
381 #if defined(JSON_HEDLEY_VERSION_ENCODE)
382 #undef JSON_HEDLEY_VERSION_ENCODE
383 #endif
384 #define JSON_HEDLEY_VERSION_ENCODE(major,minor,revision) (((major) * 1000000) + ((minor) * 1000) + (revision))
385
386 #if defined(JSON_HEDLEY_VERSION_DECODE_MAJOR)
387 #undef JSON_HEDLEY_VERSION_DECODE_MAJOR
388 #endif
389 #define JSON_HEDLEY_VERSION_DECODE_MAJOR(version) ((version) / 1000000)
390
391 #if defined(JSON_HEDLEY_VERSION_DECODE_MINOR)
392 #undef JSON_HEDLEY_VERSION_DECODE_MINOR
393 #endif
394 #define JSON_HEDLEY_VERSION_DECODE_MINOR(version) (((version) % 1000000) / 1000)
395
396 #if defined(JSON_HEDLEY_VERSION_DECODE_REVISION)
397 #undef JSON_HEDLEY_VERSION_DECODE_REVISION
398 #endif
399 #define JSON_HEDLEY_VERSION_DECODE_REVISION(version) ((version) % 1000)
400
401 #if defined(JSON_HEDLEY_GNUC_VERSION)
402 #undef JSON_HEDLEY_GNUC_VERSION
403 #endif
404 #if defined(__GNUC__) && defined(__GNUC_PATCHLEVEL__)
405 #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
406 #elif defined(__GNUC__)
407 #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, 0)
408 #endif
409
410 #if defined(JSON_HEDLEY_GNUC_VERSION_CHECK)
411 #undef JSON_HEDLEY_GNUC_VERSION_CHECK
412 #endif
413 #if defined(JSON_HEDLEY_GNUC_VERSION)
414 #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GNUC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
415 #else
416 #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (0)
417 #endif
418
419 #if defined(JSON_HEDLEY_MSVC_VERSION)
420 #undef JSON_HEDLEY_MSVC_VERSION
421 #endif
422 #if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 140000000) && !defined(__ICL)
423 #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 10000000, (_MSC_FULL_VER % 10000000) / 100000, (_MSC_FULL_VER % 100000) / 100)
424 #elif defined(_MSC_FULL_VER) && !defined(__ICL)
425 #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 1000000, (_MSC_FULL_VER % 1000000) / 10000, (_MSC_FULL_VER % 10000) / 10)
426 #elif defined(_MSC_VER) && !defined(__ICL)
427 #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_VER / 100, _MSC_VER % 100, 0)
428 #endif
429
430 #if defined(JSON_HEDLEY_MSVC_VERSION_CHECK)
431 #undef JSON_HEDLEY_MSVC_VERSION_CHECK
432 #endif
433 #if !defined(JSON_HEDLEY_MSVC_VERSION)
434 #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (0)
435 #elif defined(_MSC_VER) && (_MSC_VER >= 1400)
436 #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 10000000) + (minor * 100000) + (patch)))
437 #elif defined(_MSC_VER) && (_MSC_VER >= 1200)
438 #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 1000000) + (minor * 10000) + (patch)))
439 #else
440 #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_VER >= ((major * 100) + (minor)))
441 #endif
442
443 #if defined(JSON_HEDLEY_INTEL_VERSION)
444 #undef JSON_HEDLEY_INTEL_VERSION
445 #endif
446 #if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && !defined(__ICL)
447 #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, __INTEL_COMPILER_UPDATE)
448 #elif defined(__INTEL_COMPILER) && !defined(__ICL)
449 #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, 0)
450 #endif
451
452 #if defined(JSON_HEDLEY_INTEL_VERSION_CHECK)
453 #undef JSON_HEDLEY_INTEL_VERSION_CHECK
454 #endif
455 #if defined(JSON_HEDLEY_INTEL_VERSION)
456 #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
457 #else
458 #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (0)
459 #endif
460
461 #if defined(JSON_HEDLEY_INTEL_CL_VERSION)
462 #undef JSON_HEDLEY_INTEL_CL_VERSION
463 #endif
464 #if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && defined(__ICL)
465 #define JSON_HEDLEY_INTEL_CL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER, __INTEL_COMPILER_UPDATE, 0)
466 #endif
467
468 #if defined(JSON_HEDLEY_INTEL_CL_VERSION_CHECK)
469 #undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK
470 #endif
471 #if defined(JSON_HEDLEY_INTEL_CL_VERSION)
472 #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_CL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
473 #else
474 #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (0)
475 #endif
476
477 #if defined(JSON_HEDLEY_PGI_VERSION)
478 #undef JSON_HEDLEY_PGI_VERSION
479 #endif
480 #if defined(__PGI) && defined(__PGIC__) && defined(__PGIC_MINOR__) && defined(__PGIC_PATCHLEVEL__)
481 #define JSON_HEDLEY_PGI_VERSION JSON_HEDLEY_VERSION_ENCODE(__PGIC__, __PGIC_MINOR__, __PGIC_PATCHLEVEL__)
482 #endif
483
484 #if defined(JSON_HEDLEY_PGI_VERSION_CHECK)
485 #undef JSON_HEDLEY_PGI_VERSION_CHECK
486 #endif
487 #if defined(JSON_HEDLEY_PGI_VERSION)
488 #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PGI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
489 #else
490 #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (0)
491 #endif
492
493 #if defined(JSON_HEDLEY_SUNPRO_VERSION)
494 #undef JSON_HEDLEY_SUNPRO_VERSION
495 #endif
496 #if defined(__SUNPRO_C) && (__SUNPRO_C > 0x1000)
497 #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((((__SUNPRO_C >> 16) & 0xf) * 10) + ((__SUNPRO_C >> 12) & 0xf), (((__SUNPRO_C >> 8) & 0xf) * 10) + ((__SUNPRO_C >> 4) & 0xf), (__SUNPRO_C & 0xf) * 10)
498 #elif defined(__SUNPRO_C)
499 #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_C >> 8) & 0xf, (__SUNPRO_C >> 4) & 0xf, (__SUNPRO_C) & 0xf)
500 #elif defined(__SUNPRO_CC) && (__SUNPRO_CC > 0x1000)
501 #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((((__SUNPRO_CC >> 16) & 0xf) * 10) + ((__SUNPRO_CC >> 12) & 0xf), (((__SUNPRO_CC >> 8) & 0xf) * 10) + ((__SUNPRO_CC >> 4) & 0xf), (__SUNPRO_CC & 0xf) * 10)
502 #elif defined(__SUNPRO_CC)
503 #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_CC >> 8) & 0xf, (__SUNPRO_CC >> 4) & 0xf, (__SUNPRO_CC) & 0xf)
504 #endif
505
506 #if defined(JSON_HEDLEY_SUNPRO_VERSION_CHECK)
507 #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK
508 #endif
509 #if defined(JSON_HEDLEY_SUNPRO_VERSION)
510 #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_SUNPRO_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
511 #else
512 #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (0)
513 #endif
514
515 #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION)
516 #undef JSON_HEDLEY_EMSCRIPTEN_VERSION
517 #endif
518 #if defined(__EMSCRIPTEN__)
519 #define JSON_HEDLEY_EMSCRIPTEN_VERSION JSON_HEDLEY_VERSION_ENCODE(__EMSCRIPTEN_major__, __EMSCRIPTEN_minor__, __EMSCRIPTEN_tiny__)
520 #endif
521
522 #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK)
523 #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK
524 #endif
525 #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION)
526 #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_EMSCRIPTEN_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
527 #else
528 #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (0)
529 #endif
530
531 #if defined(JSON_HEDLEY_ARM_VERSION)
532 #undef JSON_HEDLEY_ARM_VERSION
533 #endif
534 #if defined(__CC_ARM) && defined(__ARMCOMPILER_VERSION)
535 #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCOMPILER_VERSION / 1000000, (__ARMCOMPILER_VERSION % 1000000) / 10000, (__ARMCOMPILER_VERSION % 10000) / 100)
536 #elif defined(__CC_ARM) && defined(__ARMCC_VERSION)
537 #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCC_VERSION / 1000000, (__ARMCC_VERSION % 1000000) / 10000, (__ARMCC_VERSION % 10000) / 100)
538 #endif
539
540 #if defined(JSON_HEDLEY_ARM_VERSION_CHECK)
541 #undef JSON_HEDLEY_ARM_VERSION_CHECK
542 #endif
543 #if defined(JSON_HEDLEY_ARM_VERSION)
544 #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_ARM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
545 #else
546 #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (0)
547 #endif
548
549 #if defined(JSON_HEDLEY_IBM_VERSION)
550 #undef JSON_HEDLEY_IBM_VERSION
551 #endif
552 #if defined(__ibmxl__)
553 #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ibmxl_version__, __ibmxl_release__, __ibmxl_modification__)
554 #elif defined(__xlC__) && defined(__xlC_ver__)
555 #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8, __xlC__ & 0xff, (__xlC_ver__ >> 8) & 0xff)
556 #elif defined(__xlC__)
557 #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8, __xlC__ & 0xff, 0)
558 #endif
559
560 #if defined(JSON_HEDLEY_IBM_VERSION_CHECK)
561 #undef JSON_HEDLEY_IBM_VERSION_CHECK
562 #endif
563 #if defined(JSON_HEDLEY_IBM_VERSION)
564 #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IBM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
565 #else
566 #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (0)
567 #endif
568
569 #if defined(JSON_HEDLEY_TI_VERSION)
570 #undef JSON_HEDLEY_TI_VERSION
571 #endif
572 #if \
573 defined(__TI_COMPILER_VERSION__) && \
574 ( \
575 defined(__TMS470__) || defined(__TI_ARM__) || \
576 defined(__MSP430__) || \
577 defined(__TMS320C2000__) \
578 )
579 #if (__TI_COMPILER_VERSION__ >= 16000000)
580 #define JSON_HEDLEY_TI_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
581 #endif
582 #endif
583
584 #if defined(JSON_HEDLEY_TI_VERSION_CHECK)
585 #undef JSON_HEDLEY_TI_VERSION_CHECK
586 #endif
587 #if defined(JSON_HEDLEY_TI_VERSION)
588 #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
589 #else
590 #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (0)
591 #endif
592
593 #if defined(JSON_HEDLEY_TI_CL2000_VERSION)
594 #undef JSON_HEDLEY_TI_CL2000_VERSION
595 #endif
596 #if defined(__TI_COMPILER_VERSION__) && defined(__TMS320C2000__)
597 #define JSON_HEDLEY_TI_CL2000_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
598 #endif
599
600 #if defined(JSON_HEDLEY_TI_CL2000_VERSION_CHECK)
601 #undef JSON_HEDLEY_TI_CL2000_VERSION_CHECK
602 #endif
603 #if defined(JSON_HEDLEY_TI_CL2000_VERSION)
604 #define JSON_HEDLEY_TI_CL2000_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL2000_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
605 #else
606 #define JSON_HEDLEY_TI_CL2000_VERSION_CHECK(major,minor,patch) (0)
607 #endif
608
609 #if defined(JSON_HEDLEY_TI_CL430_VERSION)
610 #undef JSON_HEDLEY_TI_CL430_VERSION
611 #endif
612 #if defined(__TI_COMPILER_VERSION__) && defined(__MSP430__)
613 #define JSON_HEDLEY_TI_CL430_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
614 #endif
615
616 #if defined(JSON_HEDLEY_TI_CL430_VERSION_CHECK)
617 #undef JSON_HEDLEY_TI_CL430_VERSION_CHECK
618 #endif
619 #if defined(JSON_HEDLEY_TI_CL430_VERSION)
620 #define JSON_HEDLEY_TI_CL430_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL430_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
621 #else
622 #define JSON_HEDLEY_TI_CL430_VERSION_CHECK(major,minor,patch) (0)
623 #endif
624
625 #if defined(JSON_HEDLEY_TI_ARMCL_VERSION)
626 #undef JSON_HEDLEY_TI_ARMCL_VERSION
627 #endif
628 #if defined(__TI_COMPILER_VERSION__) && (defined(__TMS470__) || defined(__TI_ARM__))
629 #define JSON_HEDLEY_TI_ARMCL_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
630 #endif
631
632 #if defined(JSON_HEDLEY_TI_ARMCL_VERSION_CHECK)
633 #undef JSON_HEDLEY_TI_ARMCL_VERSION_CHECK
634 #endif
635 #if defined(JSON_HEDLEY_TI_ARMCL_VERSION)
636 #define JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_ARMCL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
637 #else
638 #define JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(major,minor,patch) (0)
639 #endif
640
641 #if defined(JSON_HEDLEY_TI_CL6X_VERSION)
642 #undef JSON_HEDLEY_TI_CL6X_VERSION
643 #endif
644 #if defined(__TI_COMPILER_VERSION__) && defined(__TMS320C6X__)
645 #define JSON_HEDLEY_TI_CL6X_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
646 #endif
647
648 #if defined(JSON_HEDLEY_TI_CL6X_VERSION_CHECK)
649 #undef JSON_HEDLEY_TI_CL6X_VERSION_CHECK
650 #endif
651 #if defined(JSON_HEDLEY_TI_CL6X_VERSION)
652 #define JSON_HEDLEY_TI_CL6X_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL6X_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
653 #else
654 #define JSON_HEDLEY_TI_CL6X_VERSION_CHECK(major,minor,patch) (0)
655 #endif
656
657 #if defined(JSON_HEDLEY_TI_CL7X_VERSION)
658 #undef JSON_HEDLEY_TI_CL7X_VERSION
659 #endif
660 #if defined(__TI_COMPILER_VERSION__) && defined(__C7000__)
661 #define JSON_HEDLEY_TI_CL7X_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
662 #endif
663
664 #if defined(JSON_HEDLEY_TI_CL7X_VERSION_CHECK)
665 #undef JSON_HEDLEY_TI_CL7X_VERSION_CHECK
666 #endif
667 #if defined(JSON_HEDLEY_TI_CL7X_VERSION)
668 #define JSON_HEDLEY_TI_CL7X_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL7X_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
669 #else
670 #define JSON_HEDLEY_TI_CL7X_VERSION_CHECK(major,minor,patch) (0)
671 #endif
672
673 #if defined(JSON_HEDLEY_TI_CLPRU_VERSION)
674 #undef JSON_HEDLEY_TI_CLPRU_VERSION
675 #endif
676 #if defined(__TI_COMPILER_VERSION__) && defined(__PRU__)
677 #define JSON_HEDLEY_TI_CLPRU_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
678 #endif
679
680 #if defined(JSON_HEDLEY_TI_CLPRU_VERSION_CHECK)
681 #undef JSON_HEDLEY_TI_CLPRU_VERSION_CHECK
682 #endif
683 #if defined(JSON_HEDLEY_TI_CLPRU_VERSION)
684 #define JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CLPRU_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
685 #else
686 #define JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(major,minor,patch) (0)
687 #endif
688
689 #if defined(JSON_HEDLEY_CRAY_VERSION)
690 #undef JSON_HEDLEY_CRAY_VERSION
691 #endif
692 #if defined(_CRAYC)
693 #if defined(_RELEASE_PATCHLEVEL)
694 #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, _RELEASE_PATCHLEVEL)
695 #else
696 #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, 0)
697 #endif
698 #endif
699
700 #if defined(JSON_HEDLEY_CRAY_VERSION_CHECK)
701 #undef JSON_HEDLEY_CRAY_VERSION_CHECK
702 #endif
703 #if defined(JSON_HEDLEY_CRAY_VERSION)
704 #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_CRAY_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
705 #else
706 #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (0)
707 #endif
708
709 #if defined(JSON_HEDLEY_IAR_VERSION)
710 #undef JSON_HEDLEY_IAR_VERSION
711 #endif
712 #if defined(__IAR_SYSTEMS_ICC__)
713 #if __VER__ > 1000
714 #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE((__VER__ / 1000000), ((__VER__ / 1000) % 1000), (__VER__ % 1000))
715 #else
716 #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE(__VER__ / 100, __VER__ % 100, 0)
717 #endif
718 #endif
719
720 #if defined(JSON_HEDLEY_IAR_VERSION_CHECK)
721 #undef JSON_HEDLEY_IAR_VERSION_CHECK
722 #endif
723 #if defined(JSON_HEDLEY_IAR_VERSION)
724 #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IAR_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
725 #else
726 #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (0)
727 #endif
728
729 #if defined(JSON_HEDLEY_TINYC_VERSION)
730 #undef JSON_HEDLEY_TINYC_VERSION
731 #endif
732 #if defined(__TINYC__)
733 #define JSON_HEDLEY_TINYC_VERSION JSON_HEDLEY_VERSION_ENCODE(__TINYC__ / 1000, (__TINYC__ / 100) % 10, __TINYC__ % 100)
734 #endif
735
736 #if defined(JSON_HEDLEY_TINYC_VERSION_CHECK)
737 #undef JSON_HEDLEY_TINYC_VERSION_CHECK
738 #endif
739 #if defined(JSON_HEDLEY_TINYC_VERSION)
740 #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TINYC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
741 #else
742 #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (0)
743 #endif
744
745 #if defined(JSON_HEDLEY_DMC_VERSION)
746 #undef JSON_HEDLEY_DMC_VERSION
747 #endif
748 #if defined(__DMC__)
749 #define JSON_HEDLEY_DMC_VERSION JSON_HEDLEY_VERSION_ENCODE(__DMC__ >> 8, (__DMC__ >> 4) & 0xf, __DMC__ & 0xf)
750 #endif
751
752 #if defined(JSON_HEDLEY_DMC_VERSION_CHECK)
753 #undef JSON_HEDLEY_DMC_VERSION_CHECK
754 #endif
755 #if defined(JSON_HEDLEY_DMC_VERSION)
756 #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_DMC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
757 #else
758 #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (0)
759 #endif
760
761 #if defined(JSON_HEDLEY_COMPCERT_VERSION)
762 #undef JSON_HEDLEY_COMPCERT_VERSION
763 #endif
764 #if defined(__COMPCERT_VERSION__)
765 #define JSON_HEDLEY_COMPCERT_VERSION JSON_HEDLEY_VERSION_ENCODE(__COMPCERT_VERSION__ / 10000, (__COMPCERT_VERSION__ / 100) % 100, __COMPCERT_VERSION__ % 100)
766 #endif
767
768 #if defined(JSON_HEDLEY_COMPCERT_VERSION_CHECK)
769 #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK
770 #endif
771 #if defined(JSON_HEDLEY_COMPCERT_VERSION)
772 #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_COMPCERT_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
773 #else
774 #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (0)
775 #endif
776
777 #if defined(JSON_HEDLEY_PELLES_VERSION)
778 #undef JSON_HEDLEY_PELLES_VERSION
779 #endif
780 #if defined(__POCC__)
781 #define JSON_HEDLEY_PELLES_VERSION JSON_HEDLEY_VERSION_ENCODE(__POCC__ / 100, __POCC__ % 100, 0)
782 #endif
783
784 #if defined(JSON_HEDLEY_PELLES_VERSION_CHECK)
785 #undef JSON_HEDLEY_PELLES_VERSION_CHECK
786 #endif
787 #if defined(JSON_HEDLEY_PELLES_VERSION)
788 #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PELLES_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
789 #else
790 #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (0)
791 #endif
792
793 #if defined(JSON_HEDLEY_MCST_LCC_VERSION)
794 #undef JSON_HEDLEY_MCST_LCC_VERSION
795 #endif
796 #if defined(__LCC__) && defined(__LCC_MINOR__)
797 #define JSON_HEDLEY_MCST_LCC_VERSION JSON_HEDLEY_VERSION_ENCODE(__LCC__ / 100, __LCC__ % 100, __LCC_MINOR__)
798 #endif
799
800 #if defined(JSON_HEDLEY_MCST_LCC_VERSION_CHECK)
801 #undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK
802 #endif
803 #if defined(JSON_HEDLEY_MCST_LCC_VERSION)
804 #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_MCST_LCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
805 #else
806 #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (0)
807 #endif
808
809 #if defined(JSON_HEDLEY_GCC_VERSION)
810 #undef JSON_HEDLEY_GCC_VERSION
811 #endif
812 #if \
813 defined(JSON_HEDLEY_GNUC_VERSION) && \
814 !defined(__clang__) && \
815 !defined(JSON_HEDLEY_INTEL_VERSION) && \
816 !defined(JSON_HEDLEY_PGI_VERSION) && \
817 !defined(JSON_HEDLEY_ARM_VERSION) && \
818 !defined(JSON_HEDLEY_CRAY_VERSION) && \
819 !defined(JSON_HEDLEY_TI_VERSION) && \
820 !defined(JSON_HEDLEY_TI_ARMCL_VERSION) && \
821 !defined(JSON_HEDLEY_TI_CL430_VERSION) && \
822 !defined(JSON_HEDLEY_TI_CL2000_VERSION) && \
823 !defined(JSON_HEDLEY_TI_CL6X_VERSION) && \
824 !defined(JSON_HEDLEY_TI_CL7X_VERSION) && \
825 !defined(JSON_HEDLEY_TI_CLPRU_VERSION) && \
826 !defined(__COMPCERT__) && \
827 !defined(JSON_HEDLEY_MCST_LCC_VERSION)
828 #define JSON_HEDLEY_GCC_VERSION JSON_HEDLEY_GNUC_VERSION
829 #endif
830
831 #if defined(JSON_HEDLEY_GCC_VERSION_CHECK)
832 #undef JSON_HEDLEY_GCC_VERSION_CHECK
833 #endif
834 #if defined(JSON_HEDLEY_GCC_VERSION)
835 #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
836 #else
837 #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (0)
838 #endif
839
840 #if defined(JSON_HEDLEY_HAS_ATTRIBUTE)
841 #undef JSON_HEDLEY_HAS_ATTRIBUTE
842 #endif
843 #if \
844 defined(__has_attribute) && \
845 ( \
846 (!defined(JSON_HEDLEY_IAR_VERSION) || JSON_HEDLEY_IAR_VERSION_CHECK(8,5,9)) \
847 )
848 # define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) __has_attribute(attribute)
849 #else
850 # define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) (0)
851 #endif
852
853 #if defined(JSON_HEDLEY_GNUC_HAS_ATTRIBUTE)
854 #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE
855 #endif
856 #if defined(__has_attribute)
857 #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
858 #else
859 #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
860 #endif
861
862 #if defined(JSON_HEDLEY_GCC_HAS_ATTRIBUTE)
863 #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE
864 #endif
865 #if defined(__has_attribute)
866 #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
867 #else
868 #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
869 #endif
870
871 #if defined(JSON_HEDLEY_HAS_CPP_ATTRIBUTE)
872 #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE
873 #endif
874 #if \
875 defined(__has_cpp_attribute) && \
876 defined(__cplusplus) && \
877 (!defined(JSON_HEDLEY_SUNPRO_VERSION) || JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0))
878 #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) __has_cpp_attribute(attribute)
879 #else
880 #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) (0)
881 #endif
882
883 #if defined(JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS)
884 #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS
885 #endif
886 #if !defined(__cplusplus) || !defined(__has_cpp_attribute)
887 #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) (0)
888 #elif \
889 !defined(JSON_HEDLEY_PGI_VERSION) && \
890 !defined(JSON_HEDLEY_IAR_VERSION) && \
891 (!defined(JSON_HEDLEY_SUNPRO_VERSION) || JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0)) && \
892 (!defined(JSON_HEDLEY_MSVC_VERSION) || JSON_HEDLEY_MSVC_VERSION_CHECK(19,20,0))
893 #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) JSON_HEDLEY_HAS_CPP_ATTRIBUTE(ns::attribute)
894 #else
895 #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) (0)
896 #endif
897
898 #if defined(JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE)
899 #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE
900 #endif
901 #if defined(__has_cpp_attribute) && defined(__cplusplus)
902 #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute)
903 #else
904 #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
905 #endif
906
907 #if defined(JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE)
908 #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE
909 #endif
910 #if defined(__has_cpp_attribute) && defined(__cplusplus)
911 #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute)
912 #else
913 #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
914 #endif
915
916 #if defined(JSON_HEDLEY_HAS_BUILTIN)
917 #undef JSON_HEDLEY_HAS_BUILTIN
918 #endif
919 #if defined(__has_builtin)
920 #define JSON_HEDLEY_HAS_BUILTIN(builtin) __has_builtin(builtin)
921 #else
922 #define JSON_HEDLEY_HAS_BUILTIN(builtin) (0)
923 #endif
924
925 #if defined(JSON_HEDLEY_GNUC_HAS_BUILTIN)
926 #undef JSON_HEDLEY_GNUC_HAS_BUILTIN
927 #endif
928 #if defined(__has_builtin)
929 #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin)
930 #else
931 #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
932 #endif
933
934 #if defined(JSON_HEDLEY_GCC_HAS_BUILTIN)
935 #undef JSON_HEDLEY_GCC_HAS_BUILTIN
936 #endif
937 #if defined(__has_builtin)
938 #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin)
939 #else
940 #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
941 #endif
942
943 #if defined(JSON_HEDLEY_HAS_FEATURE)
944 #undef JSON_HEDLEY_HAS_FEATURE
945 #endif
946 #if defined(__has_feature)
947 #define JSON_HEDLEY_HAS_FEATURE(feature) __has_feature(feature)
948 #else
949 #define JSON_HEDLEY_HAS_FEATURE(feature) (0)
950 #endif
951
952 #if defined(JSON_HEDLEY_GNUC_HAS_FEATURE)
953 #undef JSON_HEDLEY_GNUC_HAS_FEATURE
954 #endif
955 #if defined(__has_feature)
956 #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature)
957 #else
958 #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
959 #endif
960
961 #if defined(JSON_HEDLEY_GCC_HAS_FEATURE)
962 #undef JSON_HEDLEY_GCC_HAS_FEATURE
963 #endif
964 #if defined(__has_feature)
965 #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature)
966 #else
967 #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
968 #endif
969
970 #if defined(JSON_HEDLEY_HAS_EXTENSION)
971 #undef JSON_HEDLEY_HAS_EXTENSION
972 #endif
973 #if defined(__has_extension)
974 #define JSON_HEDLEY_HAS_EXTENSION(extension) __has_extension(extension)
975 #else
976 #define JSON_HEDLEY_HAS_EXTENSION(extension) (0)
977 #endif
978
979 #if defined(JSON_HEDLEY_GNUC_HAS_EXTENSION)
980 #undef JSON_HEDLEY_GNUC_HAS_EXTENSION
981 #endif
982 #if defined(__has_extension)
983 #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension)
984 #else
985 #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
986 #endif
987
988 #if defined(JSON_HEDLEY_GCC_HAS_EXTENSION)
989 #undef JSON_HEDLEY_GCC_HAS_EXTENSION
990 #endif
991 #if defined(__has_extension)
992 #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension)
993 #else
994 #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
995 #endif
996
997 #if defined(JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE)
998 #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE
999 #endif
1000 #if defined(__has_declspec_attribute)
1001 #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) __has_declspec_attribute(attribute)
1002 #else
1003 #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) (0)
1004 #endif
1005
1006 #if defined(JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE)
1007 #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE
1008 #endif
1009 #if defined(__has_declspec_attribute)
1010 #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute)
1011 #else
1012 #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
1013 #endif
1014
1015 #if defined(JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE)
1016 #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE
1017 #endif
1018 #if defined(__has_declspec_attribute)
1019 #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute)
1020 #else
1021 #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
1022 #endif
1023
1024 #if defined(JSON_HEDLEY_HAS_WARNING)
1025 #undef JSON_HEDLEY_HAS_WARNING
1026 #endif
1027 #if defined(__has_warning)
1028 #define JSON_HEDLEY_HAS_WARNING(warning) __has_warning(warning)
1029 #else
1030 #define JSON_HEDLEY_HAS_WARNING(warning) (0)
1031 #endif
1032
1033 #if defined(JSON_HEDLEY_GNUC_HAS_WARNING)
1034 #undef JSON_HEDLEY_GNUC_HAS_WARNING
1035 #endif
1036 #if defined(__has_warning)
1037 #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning)
1038 #else
1039 #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
1040 #endif
1041
1042 #if defined(JSON_HEDLEY_GCC_HAS_WARNING)
1043 #undef JSON_HEDLEY_GCC_HAS_WARNING
1044 #endif
1045 #if defined(__has_warning)
1046 #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning)
1047 #else
1048 #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
1049 #endif
1050
1051 #if \
1052 (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
1053 defined(__clang__) || \
1054 JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \
1055 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1056 JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \
1057 JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \
1058 JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1059 JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1060 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \
1061 JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \
1062 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \
1063 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,0,0) || \
1064 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1065 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1066 JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0) || \
1067 JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,17) || \
1068 JSON_HEDLEY_SUNPRO_VERSION_CHECK(8,0,0) || \
1069 (JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) && defined(__C99_PRAGMA_OPERATOR))
1070 #define JSON_HEDLEY_PRAGMA(value) _Pragma(#value)
1071 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
1072 #define JSON_HEDLEY_PRAGMA(value) __pragma(value)
1073 #else
1074 #define JSON_HEDLEY_PRAGMA(value)
1075 #endif
1076
1077 #if defined(JSON_HEDLEY_DIAGNOSTIC_PUSH)
1078 #undef JSON_HEDLEY_DIAGNOSTIC_PUSH
1079 #endif
1080 #if defined(JSON_HEDLEY_DIAGNOSTIC_POP)
1081 #undef JSON_HEDLEY_DIAGNOSTIC_POP
1082 #endif
1083 #if defined(__clang__)
1084 #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("clang diagnostic push")
1085 #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("clang diagnostic pop")
1086 #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
1087 #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)")
1088 #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)")
1089 #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0)
1090 #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")
1091 #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("GCC diagnostic pop")
1092 #elif \
1093 JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \
1094 JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1095 #define JSON_HEDLEY_DIAGNOSTIC_PUSH __pragma(warning(push))
1096 #define JSON_HEDLEY_DIAGNOSTIC_POP __pragma(warning(pop))
1097 #elif JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0)
1098 #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("push")
1099 #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("pop")
1100 #elif \
1101 JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1102 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1103 JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,4,0) || \
1104 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \
1105 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1106 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0)
1107 #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("diag_push")
1108 #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("diag_pop")
1109 #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0)
1110 #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)")
1111 #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)")
1112 #else
1113 #define JSON_HEDLEY_DIAGNOSTIC_PUSH
1114 #define JSON_HEDLEY_DIAGNOSTIC_POP
1115 #endif
1116
1117 /* JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_ is for
1118 HEDLEY INTERNAL USE ONLY. API subject to change without notice. */
1119 #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_)
1120 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_
1121 #endif
1122 #if defined(__cplusplus)
1123 # if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat")
1124 # if JSON_HEDLEY_HAS_WARNING("-Wc++17-extensions")
1125 # if JSON_HEDLEY_HAS_WARNING("-Wc++1z-extensions")
1126 # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
1127 JSON_HEDLEY_DIAGNOSTIC_PUSH \
1128 _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
1129 _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \
1130 _Pragma("clang diagnostic ignored \"-Wc++1z-extensions\"") \
1131 xpr \
1132 JSON_HEDLEY_DIAGNOSTIC_POP
1133 # else
1134 # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
1135 JSON_HEDLEY_DIAGNOSTIC_PUSH \
1136 _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
1137 _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \
1138 xpr \
1139 JSON_HEDLEY_DIAGNOSTIC_POP
1140 # endif
1141 # else
1142 # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
1143 JSON_HEDLEY_DIAGNOSTIC_PUSH \
1144 _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
1145 xpr \
1146 JSON_HEDLEY_DIAGNOSTIC_POP
1147 # endif
1148 # endif
1149 #endif
1150 #if !defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_)
1151 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(x) x
1152 #endif
1153
1154 #if defined(JSON_HEDLEY_CONST_CAST)
1155 #undef JSON_HEDLEY_CONST_CAST
1156 #endif
1157 #if defined(__cplusplus)
1158 # define JSON_HEDLEY_CONST_CAST(T, expr) (const_cast<T>(expr))
1159 #elif \
1160 JSON_HEDLEY_HAS_WARNING("-Wcast-qual") || \
1161 JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0) || \
1162 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
1163 # define JSON_HEDLEY_CONST_CAST(T, expr) (__extension__ ({ \
1164 JSON_HEDLEY_DIAGNOSTIC_PUSH \
1165 JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL \
1166 ((T) (expr)); \
1167 JSON_HEDLEY_DIAGNOSTIC_POP \
1168 }))
1169 #else
1170 # define JSON_HEDLEY_CONST_CAST(T, expr) ((T) (expr))
1171 #endif
1172
1173 #if defined(JSON_HEDLEY_REINTERPRET_CAST)
1174 #undef JSON_HEDLEY_REINTERPRET_CAST
1175 #endif
1176 #if defined(__cplusplus)
1177 #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) (reinterpret_cast<T>(expr))
1178 #else
1179 #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) ((T) (expr))
1180 #endif
1181
1182 #if defined(JSON_HEDLEY_STATIC_CAST)
1183 #undef JSON_HEDLEY_STATIC_CAST
1184 #endif
1185 #if defined(__cplusplus)
1186 #define JSON_HEDLEY_STATIC_CAST(T, expr) (static_cast<T>(expr))
1187 #else
1188 #define JSON_HEDLEY_STATIC_CAST(T, expr) ((T) (expr))
1189 #endif
1190
1191 #if defined(JSON_HEDLEY_CPP_CAST)
1192 #undef JSON_HEDLEY_CPP_CAST
1193 #endif
1194 #if defined(__cplusplus)
1195 # if JSON_HEDLEY_HAS_WARNING("-Wold-style-cast")
1196 # define JSON_HEDLEY_CPP_CAST(T, expr) \
1197 JSON_HEDLEY_DIAGNOSTIC_PUSH \
1198 _Pragma("clang diagnostic ignored \"-Wold-style-cast\"") \
1199 ((T) (expr)) \
1200 JSON_HEDLEY_DIAGNOSTIC_POP
1201 # elif JSON_HEDLEY_IAR_VERSION_CHECK(8,3,0)
1202 # define JSON_HEDLEY_CPP_CAST(T, expr) \
1203 JSON_HEDLEY_DIAGNOSTIC_PUSH \
1204 _Pragma("diag_suppress=Pe137") \
1205 JSON_HEDLEY_DIAGNOSTIC_POP
1206 # else
1207 # define JSON_HEDLEY_CPP_CAST(T, expr) ((T) (expr))
1208 # endif
1209 #else
1210 # define JSON_HEDLEY_CPP_CAST(T, expr) (expr)
1211 #endif
1212
1213 #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED)
1214 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
1215 #endif
1216 #if JSON_HEDLEY_HAS_WARNING("-Wdeprecated-declarations")
1217 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")
1218 #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
1219 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warning(disable:1478 1786)")
1220 #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1221 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:1478 1786))
1222 #elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0)
1223 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1216,1444,1445")
1224 #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
1225 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444")
1226 #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0)
1227 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
1228 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
1229 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:4996))
1230 #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1231 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444")
1232 #elif \
1233 JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1234 (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1235 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1236 (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1237 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1238 (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1239 JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1240 (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1241 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1242 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1243 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0)
1244 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1291,1718")
1245 #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) && !defined(__cplusplus)
1246 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,E_DEPRECATED_ATT,E_DEPRECATED_ATT_MESS)")
1247 #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) && defined(__cplusplus)
1248 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,symdeprecated,symdeprecated2)")
1249 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
1250 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress=Pe1444,Pe1215")
1251 #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0)
1252 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warn(disable:2241)")
1253 #else
1254 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
1255 #endif
1256
1257 #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS)
1258 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
1259 #endif
1260 #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
1261 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("clang diagnostic ignored \"-Wunknown-pragmas\"")
1262 #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
1263 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("warning(disable:161)")
1264 #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1265 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:161))
1266 #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
1267 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 1675")
1268 #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0)
1269 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("GCC diagnostic ignored \"-Wunknown-pragmas\"")
1270 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
1271 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:4068))
1272 #elif \
1273 JSON_HEDLEY_TI_VERSION_CHECK(16,9,0) || \
1274 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \
1275 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1276 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0)
1277 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163")
1278 #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0)
1279 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163")
1280 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
1281 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress=Pe161")
1282 #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1283 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 161")
1284 #else
1285 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
1286 #endif
1287
1288 #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES)
1289 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
1290 #endif
1291 #if JSON_HEDLEY_HAS_WARNING("-Wunknown-attributes")
1292 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("clang diagnostic ignored \"-Wunknown-attributes\"")
1293 #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0)
1294 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
1295 #elif JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0)
1296 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("warning(disable:1292)")
1297 #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1298 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:1292))
1299 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,0)
1300 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:5030))
1301 #elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0)
1302 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097,1098")
1303 #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
1304 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097")
1305 #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus)
1306 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("error_messages(off,attrskipunsup)")
1307 #elif \
1308 JSON_HEDLEY_TI_VERSION_CHECK(18,1,0) || \
1309 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \
1310 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0)
1311 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1173")
1312 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
1313 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress=Pe1097")
1314 #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1315 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097")
1316 #else
1317 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
1318 #endif
1319
1320 #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL)
1321 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
1322 #endif
1323 #if JSON_HEDLEY_HAS_WARNING("-Wcast-qual")
1324 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("clang diagnostic ignored \"-Wcast-qual\"")
1325 #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
1326 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("warning(disable:2203 2331)")
1327 #elif JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0)
1328 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("GCC diagnostic ignored \"-Wcast-qual\"")
1329 #else
1330 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
1331 #endif
1332
1333 #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION)
1334 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
1335 #endif
1336 #if JSON_HEDLEY_HAS_WARNING("-Wunused-function")
1337 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("clang diagnostic ignored \"-Wunused-function\"")
1338 #elif JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0)
1339 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("GCC diagnostic ignored \"-Wunused-function\"")
1340 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(1,0,0)
1341 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION __pragma(warning(disable:4505))
1342 #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1343 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("diag_suppress 3142")
1344 #else
1345 #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
1346 #endif
1347
1348 #if defined(JSON_HEDLEY_DEPRECATED)
1349 #undef JSON_HEDLEY_DEPRECATED
1350 #endif
1351 #if defined(JSON_HEDLEY_DEPRECATED_FOR)
1352 #undef JSON_HEDLEY_DEPRECATED_FOR
1353 #endif
1354 #if \
1355 JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
1356 JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1357 #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated("Since " # since))
1358 #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated("Since " #since "; use " #replacement))
1359 #elif \
1360 (JSON_HEDLEY_HAS_EXTENSION(attribute_deprecated_with_message) && !defined(JSON_HEDLEY_IAR_VERSION)) || \
1361 JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \
1362 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1363 JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \
1364 JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) || \
1365 JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
1366 JSON_HEDLEY_TI_VERSION_CHECK(18,1,0) || \
1367 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(18,1,0) || \
1368 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \
1369 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1370 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0) || \
1371 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1372 #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__("Since " #since)))
1373 #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__("Since " #since "; use " #replacement)))
1374 #elif defined(__cplusplus) && (__cplusplus >= 201402L)
1375 #define JSON_HEDLEY_DEPRECATED(since) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since)]])
1376 #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since "; use " #replacement)]])
1377 #elif \
1378 JSON_HEDLEY_HAS_ATTRIBUTE(deprecated) || \
1379 JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
1380 JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1381 JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1382 (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1383 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1384 (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1385 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1386 (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1387 JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1388 (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1389 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1390 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1391 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1392 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
1393 JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
1394 #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__))
1395 #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__))
1396 #elif \
1397 JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
1398 JSON_HEDLEY_PELLES_VERSION_CHECK(6,50,0) || \
1399 JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1400 #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated)
1401 #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated)
1402 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
1403 #define JSON_HEDLEY_DEPRECATED(since) _Pragma("deprecated")
1404 #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) _Pragma("deprecated")
1405 #else
1406 #define JSON_HEDLEY_DEPRECATED(since)
1407 #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement)
1408 #endif
1409
1410 #if defined(JSON_HEDLEY_UNAVAILABLE)
1411 #undef JSON_HEDLEY_UNAVAILABLE
1412 #endif
1413 #if \
1414 JSON_HEDLEY_HAS_ATTRIBUTE(warning) || \
1415 JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0) || \
1416 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1417 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1418 #define JSON_HEDLEY_UNAVAILABLE(available_since) __attribute__((__warning__("Not available until " #available_since)))
1419 #else
1420 #define JSON_HEDLEY_UNAVAILABLE(available_since)
1421 #endif
1422
1423 #if defined(JSON_HEDLEY_WARN_UNUSED_RESULT)
1424 #undef JSON_HEDLEY_WARN_UNUSED_RESULT
1425 #endif
1426 #if defined(JSON_HEDLEY_WARN_UNUSED_RESULT_MSG)
1427 #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG
1428 #endif
1429 #if \
1430 JSON_HEDLEY_HAS_ATTRIBUTE(warn_unused_result) || \
1431 JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
1432 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1433 JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1434 (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1435 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1436 (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1437 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1438 (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1439 JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1440 (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1441 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1442 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1443 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1444 (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \
1445 JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
1446 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1447 #define JSON_HEDLEY_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
1448 #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) __attribute__((__warn_unused_result__))
1449 #elif (JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard) >= 201907L)
1450 #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
1451 #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard(msg)]])
1452 #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard)
1453 #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
1454 #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
1455 #elif defined(_Check_return_) /* SAL */
1456 #define JSON_HEDLEY_WARN_UNUSED_RESULT _Check_return_
1457 #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) _Check_return_
1458 #else
1459 #define JSON_HEDLEY_WARN_UNUSED_RESULT
1460 #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg)
1461 #endif
1462
1463 #if defined(JSON_HEDLEY_SENTINEL)
1464 #undef JSON_HEDLEY_SENTINEL
1465 #endif
1466 #if \
1467 JSON_HEDLEY_HAS_ATTRIBUTE(sentinel) || \
1468 JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
1469 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1470 JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) || \
1471 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1472 #define JSON_HEDLEY_SENTINEL(position) __attribute__((__sentinel__(position)))
1473 #else
1474 #define JSON_HEDLEY_SENTINEL(position)
1475 #endif
1476
1477 #if defined(JSON_HEDLEY_NO_RETURN)
1478 #undef JSON_HEDLEY_NO_RETURN
1479 #endif
1480 #if JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
1481 #define JSON_HEDLEY_NO_RETURN __noreturn
1482 #elif \
1483 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1484 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1485 #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__))
1486 #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
1487 #define JSON_HEDLEY_NO_RETURN _Noreturn
1488 #elif defined(__cplusplus) && (__cplusplus >= 201103L)
1489 #define JSON_HEDLEY_NO_RETURN JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[noreturn]])
1490 #elif \
1491 JSON_HEDLEY_HAS_ATTRIBUTE(noreturn) || \
1492 JSON_HEDLEY_GCC_VERSION_CHECK(3,2,0) || \
1493 JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
1494 JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1495 JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1496 JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1497 (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1498 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1499 (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1500 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1501 (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1502 JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1503 (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1504 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1505 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1506 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1507 JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
1508 #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__))
1509 #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
1510 #define JSON_HEDLEY_NO_RETURN _Pragma("does_not_return")
1511 #elif \
1512 JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
1513 JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1514 #define JSON_HEDLEY_NO_RETURN __declspec(noreturn)
1515 #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus)
1516 #define JSON_HEDLEY_NO_RETURN _Pragma("FUNC_NEVER_RETURNS;")
1517 #elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3,2,0)
1518 #define JSON_HEDLEY_NO_RETURN __attribute((noreturn))
1519 #elif JSON_HEDLEY_PELLES_VERSION_CHECK(9,0,0)
1520 #define JSON_HEDLEY_NO_RETURN __declspec(noreturn)
1521 #else
1522 #define JSON_HEDLEY_NO_RETURN
1523 #endif
1524
1525 #if defined(JSON_HEDLEY_NO_ESCAPE)
1526 #undef JSON_HEDLEY_NO_ESCAPE
1527 #endif
1528 #if JSON_HEDLEY_HAS_ATTRIBUTE(noescape)
1529 #define JSON_HEDLEY_NO_ESCAPE __attribute__((__noescape__))
1530 #else
1531 #define JSON_HEDLEY_NO_ESCAPE
1532 #endif
1533
1534 #if defined(JSON_HEDLEY_UNREACHABLE)
1535 #undef JSON_HEDLEY_UNREACHABLE
1536 #endif
1537 #if defined(JSON_HEDLEY_UNREACHABLE_RETURN)
1538 #undef JSON_HEDLEY_UNREACHABLE_RETURN
1539 #endif
1540 #if defined(JSON_HEDLEY_ASSUME)
1541 #undef JSON_HEDLEY_ASSUME
1542 #endif
1543 #if \
1544 JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
1545 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1546 JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1547 #define JSON_HEDLEY_ASSUME(expr) __assume(expr)
1548 #elif JSON_HEDLEY_HAS_BUILTIN(__builtin_assume)
1549 #define JSON_HEDLEY_ASSUME(expr) __builtin_assume(expr)
1550 #elif \
1551 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
1552 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0)
1553 #if defined(__cplusplus)
1554 #define JSON_HEDLEY_ASSUME(expr) std::_nassert(expr)
1555 #else
1556 #define JSON_HEDLEY_ASSUME(expr) _nassert(expr)
1557 #endif
1558 #endif
1559 #if \
1560 (JSON_HEDLEY_HAS_BUILTIN(__builtin_unreachable) && (!defined(JSON_HEDLEY_ARM_VERSION))) || \
1561 JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \
1562 JSON_HEDLEY_PGI_VERSION_CHECK(18,10,0) || \
1563 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1564 JSON_HEDLEY_IBM_VERSION_CHECK(13,1,5) || \
1565 JSON_HEDLEY_CRAY_VERSION_CHECK(10,0,0) || \
1566 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1567 #define JSON_HEDLEY_UNREACHABLE() __builtin_unreachable()
1568 #elif defined(JSON_HEDLEY_ASSUME)
1569 #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0)
1570 #endif
1571 #if !defined(JSON_HEDLEY_ASSUME)
1572 #if defined(JSON_HEDLEY_UNREACHABLE)
1573 #define JSON_HEDLEY_ASSUME(expr) JSON_HEDLEY_STATIC_CAST(void, ((expr) ? 1 : (JSON_HEDLEY_UNREACHABLE(), 1)))
1574 #else
1575 #define JSON_HEDLEY_ASSUME(expr) JSON_HEDLEY_STATIC_CAST(void, expr)
1576 #endif
1577 #endif
1578 #if defined(JSON_HEDLEY_UNREACHABLE)
1579 #if \
1580 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
1581 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0)
1582 #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return (JSON_HEDLEY_STATIC_CAST(void, JSON_HEDLEY_ASSUME(0)), (value))
1583 #else
1584 #define JSON_HEDLEY_UNREACHABLE_RETURN(value) JSON_HEDLEY_UNREACHABLE()
1585 #endif
1586 #else
1587 #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return (value)
1588 #endif
1589 #if !defined(JSON_HEDLEY_UNREACHABLE)
1590 #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0)
1591 #endif
1592
1593 JSON_HEDLEY_DIAGNOSTIC_PUSH
1594 #if JSON_HEDLEY_HAS_WARNING("-Wpedantic")
1595 #pragma clang diagnostic ignored "-Wpedantic"
1596 #endif
1597 #if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat-pedantic") && defined(__cplusplus)
1598 #pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
1599 #endif
1600 #if JSON_HEDLEY_GCC_HAS_WARNING("-Wvariadic-macros",4,0,0)
1601 #if defined(__clang__)
1602 #pragma clang diagnostic ignored "-Wvariadic-macros"
1603 #elif defined(JSON_HEDLEY_GCC_VERSION)
1604 #pragma GCC diagnostic ignored "-Wvariadic-macros"
1605 #endif
1606 #endif
1607 #if defined(JSON_HEDLEY_NON_NULL)
1608 #undef JSON_HEDLEY_NON_NULL
1609 #endif
1610 #if \
1611 JSON_HEDLEY_HAS_ATTRIBUTE(nonnull) || \
1612 JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
1613 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1614 JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0)
1615 #define JSON_HEDLEY_NON_NULL(...) __attribute__((__nonnull__(__VA_ARGS__)))
1616 #else
1617 #define JSON_HEDLEY_NON_NULL(...)
1618 #endif
1619 JSON_HEDLEY_DIAGNOSTIC_POP
1620
1621 #if defined(JSON_HEDLEY_PRINTF_FORMAT)
1622 #undef JSON_HEDLEY_PRINTF_FORMAT
1623 #endif
1624 #if defined(__MINGW32__) && JSON_HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && !defined(__USE_MINGW_ANSI_STDIO)
1625 #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(ms_printf, string_idx, first_to_check)))
1626 #elif defined(__MINGW32__) && JSON_HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && defined(__USE_MINGW_ANSI_STDIO)
1627 #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(gnu_printf, string_idx, first_to_check)))
1628 #elif \
1629 JSON_HEDLEY_HAS_ATTRIBUTE(format) || \
1630 JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
1631 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1632 JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \
1633 JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1634 JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1635 (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1636 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1637 (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1638 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1639 (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1640 JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1641 (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1642 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1643 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1644 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1645 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1646 #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(__printf__, string_idx, first_to_check)))
1647 #elif JSON_HEDLEY_PELLES_VERSION_CHECK(6,0,0)
1648 #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __declspec(vaformat(printf,string_idx,first_to_check))
1649 #else
1650 #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check)
1651 #endif
1652
1653 #if defined(JSON_HEDLEY_CONSTEXPR)
1654 #undef JSON_HEDLEY_CONSTEXPR
1655 #endif
1656 #if defined(__cplusplus)
1657 #if __cplusplus >= 201103L
1658 #define JSON_HEDLEY_CONSTEXPR JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(constexpr)
1659 #endif
1660 #endif
1661 #if !defined(JSON_HEDLEY_CONSTEXPR)
1662 #define JSON_HEDLEY_CONSTEXPR
1663 #endif
1664
1665 #if defined(JSON_HEDLEY_PREDICT)
1666 #undef JSON_HEDLEY_PREDICT
1667 #endif
1668 #if defined(JSON_HEDLEY_LIKELY)
1669 #undef JSON_HEDLEY_LIKELY
1670 #endif
1671 #if defined(JSON_HEDLEY_UNLIKELY)
1672 #undef JSON_HEDLEY_UNLIKELY
1673 #endif
1674 #if defined(JSON_HEDLEY_UNPREDICTABLE)
1675 #undef JSON_HEDLEY_UNPREDICTABLE
1676 #endif
1677 #if JSON_HEDLEY_HAS_BUILTIN(__builtin_unpredictable)
1678 #define JSON_HEDLEY_UNPREDICTABLE(expr) __builtin_unpredictable((expr))
1679 #endif
1680 #if \
1681 (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect_with_probability) && !defined(JSON_HEDLEY_PGI_VERSION)) || \
1682 JSON_HEDLEY_GCC_VERSION_CHECK(9,0,0) || \
1683 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1684 # define JSON_HEDLEY_PREDICT(expr, value, probability) __builtin_expect_with_probability( (expr), (value), (probability))
1685 # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) __builtin_expect_with_probability(!!(expr), 1 , (probability))
1686 # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) __builtin_expect_with_probability(!!(expr), 0 , (probability))
1687 # define JSON_HEDLEY_LIKELY(expr) __builtin_expect (!!(expr), 1 )
1688 # define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect (!!(expr), 0 )
1689 #elif \
1690 (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \
1691 JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \
1692 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1693 (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \
1694 JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1695 JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1696 JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1697 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \
1698 JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \
1699 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \
1700 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
1701 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1702 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1703 JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,27) || \
1704 JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
1705 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1706 # define JSON_HEDLEY_PREDICT(expr, expected, probability) \
1707 (((probability) >= 0.9) ? __builtin_expect((expr), (expected)) : (JSON_HEDLEY_STATIC_CAST(void, expected), (expr)))
1708 # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) \
1709 (__extension__ ({ \
1710 double hedley_probability_ = (probability); \
1711 ((hedley_probability_ >= 0.9) ? __builtin_expect(!!(expr), 1) : ((hedley_probability_ <= 0.1) ? __builtin_expect(!!(expr), 0) : !!(expr))); \
1712 }))
1713 # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) \
1714 (__extension__ ({ \
1715 double hedley_probability_ = (probability); \
1716 ((hedley_probability_ >= 0.9) ? __builtin_expect(!!(expr), 0) : ((hedley_probability_ <= 0.1) ? __builtin_expect(!!(expr), 1) : !!(expr))); \
1717 }))
1718 # define JSON_HEDLEY_LIKELY(expr) __builtin_expect(!!(expr), 1)
1719 # define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect(!!(expr), 0)
1720 #else
1721 # define JSON_HEDLEY_PREDICT(expr, expected, probability) (JSON_HEDLEY_STATIC_CAST(void, expected), (expr))
1722 # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) (!!(expr))
1723 # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) (!!(expr))
1724 # define JSON_HEDLEY_LIKELY(expr) (!!(expr))
1725 # define JSON_HEDLEY_UNLIKELY(expr) (!!(expr))
1726 #endif
1727 #if !defined(JSON_HEDLEY_UNPREDICTABLE)
1728 #define JSON_HEDLEY_UNPREDICTABLE(expr) JSON_HEDLEY_PREDICT(expr, 1, 0.5)
1729 #endif
1730
1731 #if defined(JSON_HEDLEY_MALLOC)
1732 #undef JSON_HEDLEY_MALLOC
1733 #endif
1734 #if \
1735 JSON_HEDLEY_HAS_ATTRIBUTE(malloc) || \
1736 JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
1737 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1738 JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
1739 JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1740 JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \
1741 JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1742 (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1743 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1744 (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1745 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1746 (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1747 JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1748 (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1749 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1750 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1751 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1752 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1753 #define JSON_HEDLEY_MALLOC __attribute__((__malloc__))
1754 #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
1755 #define JSON_HEDLEY_MALLOC _Pragma("returns_new_memory")
1756 #elif \
1757 JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
1758 JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1759 #define JSON_HEDLEY_MALLOC __declspec(restrict)
1760 #else
1761 #define JSON_HEDLEY_MALLOC
1762 #endif
1763
1764 #if defined(JSON_HEDLEY_PURE)
1765 #undef JSON_HEDLEY_PURE
1766 #endif
1767 #if \
1768 JSON_HEDLEY_HAS_ATTRIBUTE(pure) || \
1769 JSON_HEDLEY_GCC_VERSION_CHECK(2,96,0) || \
1770 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1771 JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
1772 JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1773 JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1774 JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1775 (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1776 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1777 (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1778 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1779 (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1780 JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1781 (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1782 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1783 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1784 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1785 JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
1786 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1787 # define JSON_HEDLEY_PURE __attribute__((__pure__))
1788 #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
1789 # define JSON_HEDLEY_PURE _Pragma("does_not_write_global_data")
1790 #elif defined(__cplusplus) && \
1791 ( \
1792 JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \
1793 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0) || \
1794 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) \
1795 )
1796 # define JSON_HEDLEY_PURE _Pragma("FUNC_IS_PURE;")
1797 #else
1798 # define JSON_HEDLEY_PURE
1799 #endif
1800
1801 #if defined(JSON_HEDLEY_CONST)
1802 #undef JSON_HEDLEY_CONST
1803 #endif
1804 #if \
1805 JSON_HEDLEY_HAS_ATTRIBUTE(const) || \
1806 JSON_HEDLEY_GCC_VERSION_CHECK(2,5,0) || \
1807 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1808 JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
1809 JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1810 JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1811 JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1812 (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1813 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1814 (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1815 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1816 (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1817 JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1818 (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1819 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1820 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1821 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1822 JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
1823 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1824 #define JSON_HEDLEY_CONST __attribute__((__const__))
1825 #elif \
1826 JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
1827 #define JSON_HEDLEY_CONST _Pragma("no_side_effect")
1828 #else
1829 #define JSON_HEDLEY_CONST JSON_HEDLEY_PURE
1830 #endif
1831
1832 #if defined(JSON_HEDLEY_RESTRICT)
1833 #undef JSON_HEDLEY_RESTRICT
1834 #endif
1835 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && !defined(__cplusplus)
1836 #define JSON_HEDLEY_RESTRICT restrict
1837 #elif \
1838 JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
1839 JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
1840 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1841 JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
1842 JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1843 JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1844 JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
1845 JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1846 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,4) || \
1847 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \
1848 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1849 (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus)) || \
1850 JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \
1851 defined(__clang__) || \
1852 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1853 #define JSON_HEDLEY_RESTRICT __restrict
1854 #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,3,0) && !defined(__cplusplus)
1855 #define JSON_HEDLEY_RESTRICT _Restrict
1856 #else
1857 #define JSON_HEDLEY_RESTRICT
1858 #endif
1859
1860 #if defined(JSON_HEDLEY_INLINE)
1861 #undef JSON_HEDLEY_INLINE
1862 #endif
1863 #if \
1864 (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
1865 (defined(__cplusplus) && (__cplusplus >= 199711L))
1866 #define JSON_HEDLEY_INLINE inline
1867 #elif \
1868 defined(JSON_HEDLEY_GCC_VERSION) || \
1869 JSON_HEDLEY_ARM_VERSION_CHECK(6,2,0)
1870 #define JSON_HEDLEY_INLINE __inline__
1871 #elif \
1872 JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \
1873 JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
1874 JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1875 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,1,0) || \
1876 JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \
1877 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
1878 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \
1879 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1880 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1881 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1882 #define JSON_HEDLEY_INLINE __inline
1883 #else
1884 #define JSON_HEDLEY_INLINE
1885 #endif
1886
1887 #if defined(JSON_HEDLEY_ALWAYS_INLINE)
1888 #undef JSON_HEDLEY_ALWAYS_INLINE
1889 #endif
1890 #if \
1891 JSON_HEDLEY_HAS_ATTRIBUTE(always_inline) || \
1892 JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
1893 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1894 JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
1895 JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1896 JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1897 JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1898 (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1899 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1900 (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1901 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1902 (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1903 JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1904 (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1905 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1906 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1907 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1908 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
1909 JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
1910 # define JSON_HEDLEY_ALWAYS_INLINE __attribute__((__always_inline__)) JSON_HEDLEY_INLINE
1911 #elif \
1912 JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \
1913 JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1914 # define JSON_HEDLEY_ALWAYS_INLINE __forceinline
1915 #elif defined(__cplusplus) && \
1916 ( \
1917 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1918 JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1919 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1920 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
1921 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1922 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) \
1923 )
1924 # define JSON_HEDLEY_ALWAYS_INLINE _Pragma("FUNC_ALWAYS_INLINE;")
1925 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
1926 # define JSON_HEDLEY_ALWAYS_INLINE _Pragma("inline=forced")
1927 #else
1928 # define JSON_HEDLEY_ALWAYS_INLINE JSON_HEDLEY_INLINE
1929 #endif
1930
1931 #if defined(JSON_HEDLEY_NEVER_INLINE)
1932 #undef JSON_HEDLEY_NEVER_INLINE
1933 #endif
1934 #if \
1935 JSON_HEDLEY_HAS_ATTRIBUTE(noinline) || \
1936 JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
1937 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1938 JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
1939 JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1940 JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1941 JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1942 (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1943 JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1944 (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1945 JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1946 (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1947 JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1948 (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1949 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1950 JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1951 JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1952 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
1953 JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
1954 #define JSON_HEDLEY_NEVER_INLINE __attribute__((__noinline__))
1955 #elif \
1956 JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
1957 JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1958 #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline)
1959 #elif JSON_HEDLEY_PGI_VERSION_CHECK(10,2,0)
1960 #define JSON_HEDLEY_NEVER_INLINE _Pragma("noinline")
1961 #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus)
1962 #define JSON_HEDLEY_NEVER_INLINE _Pragma("FUNC_CANNOT_INLINE;")
1963 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
1964 #define JSON_HEDLEY_NEVER_INLINE _Pragma("inline=never")
1965 #elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3,2,0)
1966 #define JSON_HEDLEY_NEVER_INLINE __attribute((noinline))
1967 #elif JSON_HEDLEY_PELLES_VERSION_CHECK(9,0,0)
1968 #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline)
1969 #else
1970 #define JSON_HEDLEY_NEVER_INLINE
1971 #endif
1972
1973 #if defined(JSON_HEDLEY_PRIVATE)
1974 #undef JSON_HEDLEY_PRIVATE
1975 #endif
1976 #if defined(JSON_HEDLEY_PUBLIC)
1977 #undef JSON_HEDLEY_PUBLIC
1978 #endif
1979 #if defined(JSON_HEDLEY_IMPORT)
1980 #undef JSON_HEDLEY_IMPORT
1981 #endif
1982 #if defined(_WIN32) || defined(__CYGWIN__)
1983 # define JSON_HEDLEY_PRIVATE
1984 # define JSON_HEDLEY_PUBLIC __declspec(dllexport)
1985 # define JSON_HEDLEY_IMPORT __declspec(dllimport)
1986 #else
1987 # if \
1988 JSON_HEDLEY_HAS_ATTRIBUTE(visibility) || \
1989 JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
1990 JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
1991 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1992 JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1993 JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
1994 ( \
1995 defined(__TI_EABI__) && \
1996 ( \
1997 (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1998 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) \
1999 ) \
2000 ) || \
2001 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
2002 # define JSON_HEDLEY_PRIVATE __attribute__((__visibility__("hidden")))
2003 # define JSON_HEDLEY_PUBLIC __attribute__((__visibility__("default")))
2004 # else
2005 # define JSON_HEDLEY_PRIVATE
2006 # define JSON_HEDLEY_PUBLIC
2007 # endif
2008 # define JSON_HEDLEY_IMPORT extern
2009 #endif
2010
2011 #if defined(JSON_HEDLEY_NO_THROW)
2012 #undef JSON_HEDLEY_NO_THROW
2013 #endif
2014 #if \
2015 JSON_HEDLEY_HAS_ATTRIBUTE(nothrow) || \
2016 JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
2017 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
2018 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
2019 #define JSON_HEDLEY_NO_THROW __attribute__((__nothrow__))
2020 #elif \
2021 JSON_HEDLEY_MSVC_VERSION_CHECK(13,1,0) || \
2022 JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
2023 JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0)
2024 #define JSON_HEDLEY_NO_THROW __declspec(nothrow)
2025 #else
2026 #define JSON_HEDLEY_NO_THROW
2027 #endif
2028
2029 #if defined(JSON_HEDLEY_FALL_THROUGH)
2030 #undef JSON_HEDLEY_FALL_THROUGH
2031 #endif
2032 #if \
2033 JSON_HEDLEY_HAS_ATTRIBUTE(fallthrough) || \
2034 JSON_HEDLEY_GCC_VERSION_CHECK(7,0,0) || \
2035 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
2036 #define JSON_HEDLEY_FALL_THROUGH __attribute__((__fallthrough__))
2037 #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(clang,fallthrough)
2038 #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[clang::fallthrough]])
2039 #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(fallthrough)
2040 #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[fallthrough]])
2041 #elif defined(__fallthrough) /* SAL */
2042 #define JSON_HEDLEY_FALL_THROUGH __fallthrough
2043 #else
2044 #define JSON_HEDLEY_FALL_THROUGH
2045 #endif
2046
2047 #if defined(JSON_HEDLEY_RETURNS_NON_NULL)
2048 #undef JSON_HEDLEY_RETURNS_NON_NULL
2049 #endif
2050 #if \
2051 JSON_HEDLEY_HAS_ATTRIBUTE(returns_nonnull) || \
2052 JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \
2053 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
2054 #define JSON_HEDLEY_RETURNS_NON_NULL __attribute__((__returns_nonnull__))
2055 #elif defined(_Ret_notnull_) /* SAL */
2056 #define JSON_HEDLEY_RETURNS_NON_NULL _Ret_notnull_
2057 #else
2058 #define JSON_HEDLEY_RETURNS_NON_NULL
2059 #endif
2060
2061 #if defined(JSON_HEDLEY_ARRAY_PARAM)
2062 #undef JSON_HEDLEY_ARRAY_PARAM
2063 #endif
2064 #if \
2065 defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
2066 !defined(__STDC_NO_VLA__) && \
2067 !defined(__cplusplus) && \
2068 !defined(JSON_HEDLEY_PGI_VERSION) && \
2069 !defined(JSON_HEDLEY_TINYC_VERSION)
2070 #define JSON_HEDLEY_ARRAY_PARAM(name) (name)
2071 #else
2072 #define JSON_HEDLEY_ARRAY_PARAM(name)
2073 #endif
2074
2075 #if defined(JSON_HEDLEY_IS_CONSTANT)
2076 #undef JSON_HEDLEY_IS_CONSTANT
2077 #endif
2078 #if defined(JSON_HEDLEY_REQUIRE_CONSTEXPR)
2079 #undef JSON_HEDLEY_REQUIRE_CONSTEXPR
2080 #endif
2081 /* JSON_HEDLEY_IS_CONSTEXPR_ is for
2082 HEDLEY INTERNAL USE ONLY. API subject to change without notice. */
2083 #if defined(JSON_HEDLEY_IS_CONSTEXPR_)
2084 #undef JSON_HEDLEY_IS_CONSTEXPR_
2085 #endif
2086 #if \
2087 JSON_HEDLEY_HAS_BUILTIN(__builtin_constant_p) || \
2088 JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
2089 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
2090 JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,19) || \
2091 JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
2092 JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
2093 JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
2094 (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) && !defined(__cplusplus)) || \
2095 JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
2096 JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
2097 #define JSON_HEDLEY_IS_CONSTANT(expr) __builtin_constant_p(expr)
2098 #endif
2099 #if !defined(__cplusplus)
2100 # if \
2101 JSON_HEDLEY_HAS_BUILTIN(__builtin_types_compatible_p) || \
2102 JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
2103 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
2104 JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
2105 JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
2106 JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) || \
2107 JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,24)
2108 #if defined(__INTPTR_TYPE__)
2109 #define JSON_HEDLEY_IS_CONSTEXPR_(expr) __builtin_types_compatible_p(__typeof__((1 ? (void*) ((__INTPTR_TYPE__) ((expr) * 0)) : (int*) 0)), int*)
2110 #else
2111 #include <stdint.h>
2112 #define JSON_HEDLEY_IS_CONSTEXPR_(expr) __builtin_types_compatible_p(__typeof__((1 ? (void*) ((intptr_t) ((expr) * 0)) : (int*) 0)), int*)
2113 #endif
2114 # elif \
2115 ( \
2116 defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) && \
2117 !defined(JSON_HEDLEY_SUNPRO_VERSION) && \
2118 !defined(JSON_HEDLEY_PGI_VERSION) && \
2119 !defined(JSON_HEDLEY_IAR_VERSION)) || \
2120 (JSON_HEDLEY_HAS_EXTENSION(c_generic_selections) && !defined(JSON_HEDLEY_IAR_VERSION)) || \
2121 JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \
2122 JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0) || \
2123 JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \
2124 JSON_HEDLEY_ARM_VERSION_CHECK(5,3,0)
2125 #if defined(__INTPTR_TYPE__)
2126 #define JSON_HEDLEY_IS_CONSTEXPR_(expr) _Generic((1 ? (void*) ((__INTPTR_TYPE__) ((expr) * 0)) : (int*) 0), int*: 1, void*: 0)
2127 #else
2128 #include <stdint.h>
2129 #define JSON_HEDLEY_IS_CONSTEXPR_(expr) _Generic((1 ? (void*) ((intptr_t) * 0) : (int*) 0), int*: 1, void*: 0)
2130 #endif
2131 # elif \
2132 defined(JSON_HEDLEY_GCC_VERSION) || \
2133 defined(JSON_HEDLEY_INTEL_VERSION) || \
2134 defined(JSON_HEDLEY_TINYC_VERSION) || \
2135 defined(JSON_HEDLEY_TI_ARMCL_VERSION) || \
2136 JSON_HEDLEY_TI_CL430_VERSION_CHECK(18,12,0) || \
2137 defined(JSON_HEDLEY_TI_CL2000_VERSION) || \
2138 defined(JSON_HEDLEY_TI_CL6X_VERSION) || \
2139 defined(JSON_HEDLEY_TI_CL7X_VERSION) || \
2140 defined(JSON_HEDLEY_TI_CLPRU_VERSION) || \
2141 defined(__clang__)
2142 # define JSON_HEDLEY_IS_CONSTEXPR_(expr) ( \
2143 sizeof(void) != \
2144 sizeof(*( \
2145 1 ? \
2146 ((void*) ((expr) * 0L) ) : \
2147 ((struct { char v[sizeof(void) * 2]; } *) 1) \
2148 ) \
2149 ) \
2150 )
2151 # endif
2152 #endif
2153 #if defined(JSON_HEDLEY_IS_CONSTEXPR_)
2154 #if !defined(JSON_HEDLEY_IS_CONSTANT)
2155 #define JSON_HEDLEY_IS_CONSTANT(expr) JSON_HEDLEY_IS_CONSTEXPR_(expr)
2156 #endif
2157 #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (JSON_HEDLEY_IS_CONSTEXPR_(expr) ? (expr) : (-1))
2158 #else
2159 #if !defined(JSON_HEDLEY_IS_CONSTANT)
2160 #define JSON_HEDLEY_IS_CONSTANT(expr) (0)
2161 #endif
2162 #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (expr)
2163 #endif
2164
2165 #if defined(JSON_HEDLEY_BEGIN_C_DECLS)
2166 #undef JSON_HEDLEY_BEGIN_C_DECLS
2167 #endif
2168 #if defined(JSON_HEDLEY_END_C_DECLS)
2169 #undef JSON_HEDLEY_END_C_DECLS
2170 #endif
2171 #if defined(JSON_HEDLEY_C_DECL)
2172 #undef JSON_HEDLEY_C_DECL
2173 #endif
2174 #if defined(__cplusplus)
2175 #define JSON_HEDLEY_BEGIN_C_DECLS extern "C" {
2176 #define JSON_HEDLEY_END_C_DECLS }
2177 #define JSON_HEDLEY_C_DECL extern "C"
2178 #else
2179 #define JSON_HEDLEY_BEGIN_C_DECLS
2180 #define JSON_HEDLEY_END_C_DECLS
2181 #define JSON_HEDLEY_C_DECL
2182 #endif
2183
2184 #if defined(JSON_HEDLEY_STATIC_ASSERT)
2185 #undef JSON_HEDLEY_STATIC_ASSERT
2186 #endif
2187 #if \
2188 !defined(__cplusplus) && ( \
2189 (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)) || \
2190 (JSON_HEDLEY_HAS_FEATURE(c_static_assert) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \
2191 JSON_HEDLEY_GCC_VERSION_CHECK(6,0,0) || \
2192 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
2193 defined(_Static_assert) \
2194 )
2195 # define JSON_HEDLEY_STATIC_ASSERT(expr, message) _Static_assert(expr, message)
2196 #elif \
2197 (defined(__cplusplus) && (__cplusplus >= 201103L)) || \
2198 JSON_HEDLEY_MSVC_VERSION_CHECK(16,0,0) || \
2199 JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
2200 # define JSON_HEDLEY_STATIC_ASSERT(expr, message) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(static_assert(expr, message))
2201 #else
2202 # define JSON_HEDLEY_STATIC_ASSERT(expr, message)
2203 #endif
2204
2205 #if defined(JSON_HEDLEY_NULL)
2206 #undef JSON_HEDLEY_NULL
2207 #endif
2208 #if defined(__cplusplus)
2209 #if __cplusplus >= 201103L
2210 #define JSON_HEDLEY_NULL JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(nullptr)
2211 #elif defined(NULL)
2212 #define JSON_HEDLEY_NULL NULL
2213 #else
2214 #define JSON_HEDLEY_NULL JSON_HEDLEY_STATIC_CAST(void*, 0)
2215 #endif
2216 #elif defined(NULL)
2217 #define JSON_HEDLEY_NULL NULL
2218 #else
2219 #define JSON_HEDLEY_NULL ((void*) 0)
2220 #endif
2221
2222 #if defined(JSON_HEDLEY_MESSAGE)
2223 #undef JSON_HEDLEY_MESSAGE
2224 #endif
2225 #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
2226 # define JSON_HEDLEY_MESSAGE(msg) \
2227 JSON_HEDLEY_DIAGNOSTIC_PUSH \
2228 JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS \
2229 JSON_HEDLEY_PRAGMA(message msg) \
2230 JSON_HEDLEY_DIAGNOSTIC_POP
2231 #elif \
2232 JSON_HEDLEY_GCC_VERSION_CHECK(4,4,0) || \
2233 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
2234 # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message msg)
2235 #elif JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0)
2236 # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(_CRI message msg)
2237 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
2238 # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg))
2239 #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,0,0)
2240 # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg))
2241 #else
2242 # define JSON_HEDLEY_MESSAGE(msg)
2243 #endif
2244
2245 #if defined(JSON_HEDLEY_WARNING)
2246 #undef JSON_HEDLEY_WARNING
2247 #endif
2248 #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
2249 # define JSON_HEDLEY_WARNING(msg) \
2250 JSON_HEDLEY_DIAGNOSTIC_PUSH \
2251 JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS \
2252 JSON_HEDLEY_PRAGMA(clang warning msg) \
2253 JSON_HEDLEY_DIAGNOSTIC_POP
2254 #elif \
2255 JSON_HEDLEY_GCC_VERSION_CHECK(4,8,0) || \
2256 JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \
2257 JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
2258 # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(GCC warning msg)
2259 #elif \
2260 JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \
2261 JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
2262 # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(message(msg))
2263 #else
2264 # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_MESSAGE(msg)
2265 #endif
2266
2267 #if defined(JSON_HEDLEY_REQUIRE)
2268 #undef JSON_HEDLEY_REQUIRE
2269 #endif
2270 #if defined(JSON_HEDLEY_REQUIRE_MSG)
2271 #undef JSON_HEDLEY_REQUIRE_MSG
2272 #endif
2273 #if JSON_HEDLEY_HAS_ATTRIBUTE(diagnose_if)
2274 # if JSON_HEDLEY_HAS_WARNING("-Wgcc-compat")
2275 # define JSON_HEDLEY_REQUIRE(expr) \
2276 JSON_HEDLEY_DIAGNOSTIC_PUSH \
2277 _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \
2278 __attribute__((diagnose_if(!(expr), #expr, "error"))) \
2279 JSON_HEDLEY_DIAGNOSTIC_POP
2280 # define JSON_HEDLEY_REQUIRE_MSG(expr,msg) \
2281 JSON_HEDLEY_DIAGNOSTIC_PUSH \
2282 _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \
2283 __attribute__((diagnose_if(!(expr), msg, "error"))) \
2284 JSON_HEDLEY_DIAGNOSTIC_POP
2285 # else
2286 # define JSON_HEDLEY_REQUIRE(expr) __attribute__((diagnose_if(!(expr), #expr, "error")))
2287 # define JSON_HEDLEY_REQUIRE_MSG(expr,msg) __attribute__((diagnose_if(!(expr), msg, "error")))
2288 # endif
2289 #else
2290 # define JSON_HEDLEY_REQUIRE(expr)
2291 # define JSON_HEDLEY_REQUIRE_MSG(expr,msg)
2292 #endif
2293
2294 #if defined(JSON_HEDLEY_FLAGS)
2295 #undef JSON_HEDLEY_FLAGS
2296 #endif
2297 #if JSON_HEDLEY_HAS_ATTRIBUTE(flag_enum) && (!defined(__cplusplus) || JSON_HEDLEY_HAS_WARNING("-Wbitfield-enum-conversion"))
2298 #define JSON_HEDLEY_FLAGS __attribute__((__flag_enum__))
2299 #else
2300 #define JSON_HEDLEY_FLAGS
2301 #endif
2302
2303 #if defined(JSON_HEDLEY_FLAGS_CAST)
2304 #undef JSON_HEDLEY_FLAGS_CAST
2305 #endif
2306 #if JSON_HEDLEY_INTEL_VERSION_CHECK(19,0,0)
2307 # define JSON_HEDLEY_FLAGS_CAST(T, expr) (__extension__ ({ \
2308 JSON_HEDLEY_DIAGNOSTIC_PUSH \
2309 _Pragma("warning(disable:188)") \
2310 ((T) (expr)); \
2311 JSON_HEDLEY_DIAGNOSTIC_POP \
2312 }))
2313 #else
2314 # define JSON_HEDLEY_FLAGS_CAST(T, expr) JSON_HEDLEY_STATIC_CAST(T, expr)
2315 #endif
2316
2317 #if defined(JSON_HEDLEY_EMPTY_BASES)
2318 #undef JSON_HEDLEY_EMPTY_BASES
2319 #endif
2320 #if \
2321 (JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,23918) && !JSON_HEDLEY_MSVC_VERSION_CHECK(20,0,0)) || \
2322 JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
2323 #define JSON_HEDLEY_EMPTY_BASES __declspec(empty_bases)
2324 #else
2325 #define JSON_HEDLEY_EMPTY_BASES
2326 #endif
2327
2328 /* Remaining macros are deprecated. */
2329
2330 #if defined(JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK)
2331 #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK
2332 #endif
2333 #if defined(__clang__)
2334 #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) (0)
2335 #else
2336 #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
2337 #endif
2338
2339 #if defined(JSON_HEDLEY_CLANG_HAS_ATTRIBUTE)
2340 #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE
2341 #endif
2342 #define JSON_HEDLEY_CLANG_HAS_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
2343
2344 #if defined(JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE)
2345 #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE
2346 #endif
2347 #define JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute)
2348
2349 #if defined(JSON_HEDLEY_CLANG_HAS_BUILTIN)
2350 #undef JSON_HEDLEY_CLANG_HAS_BUILTIN
2351 #endif
2352 #define JSON_HEDLEY_CLANG_HAS_BUILTIN(builtin) JSON_HEDLEY_HAS_BUILTIN(builtin)
2353
2354 #if defined(JSON_HEDLEY_CLANG_HAS_FEATURE)
2355 #undef JSON_HEDLEY_CLANG_HAS_FEATURE
2356 #endif
2357 #define JSON_HEDLEY_CLANG_HAS_FEATURE(feature) JSON_HEDLEY_HAS_FEATURE(feature)
2358
2359 #if defined(JSON_HEDLEY_CLANG_HAS_EXTENSION)
2360 #undef JSON_HEDLEY_CLANG_HAS_EXTENSION
2361 #endif
2362 #define JSON_HEDLEY_CLANG_HAS_EXTENSION(extension) JSON_HEDLEY_HAS_EXTENSION(extension)
2363
2364 #if defined(JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE)
2365 #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE
2366 #endif
2367 #define JSON_HEDLEY_CLANG_HAS_DECLSPEC_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute)
2368
2369 #if defined(JSON_HEDLEY_CLANG_HAS_WARNING)
2370 #undef JSON_HEDLEY_CLANG_HAS_WARNING
2371 #endif
2372 #define JSON_HEDLEY_CLANG_HAS_WARNING(warning) JSON_HEDLEY_HAS_WARNING(warning)
2373
2374 #endif /* !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < X) */
2375
2376
2377 // This file contains all internal macro definitions (except those affecting ABI)
2378 // You MUST include macro_unscope.hpp at the end of json.hpp to undef all of them
2379
2380 // #include <nlohmann/detail/abi_macros.hpp>
2381
2382
2383 // exclude unsupported compilers
2384 #if !defined(JSON_SKIP_UNSUPPORTED_COMPILER_CHECK)
2385 #if defined(__clang__)
2386 #if (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) < 30400
2387 #error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers"
2388 #endif
2389 #elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER))
2390 #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800
2391 #error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers"
2392 #endif
2393 #endif
2394 #endif
2395
2396 // C++ language standard detection
2397 // if the user manually specified the used c++ version this is skipped
2398 #if !defined(JSON_HAS_CPP_23) && !defined(JSON_HAS_CPP_20) && !defined(JSON_HAS_CPP_17) && !defined(JSON_HAS_CPP_14) && !defined(JSON_HAS_CPP_11)
2399 #if (defined(__cplusplus) && __cplusplus > 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG > 202002L)
2400 #define JSON_HAS_CPP_23
2401 #define JSON_HAS_CPP_20
2402 #define JSON_HAS_CPP_17
2403 #define JSON_HAS_CPP_14
2404 #elif (defined(__cplusplus) && __cplusplus > 201703L) || (defined(_MSVC_LANG) && _MSVC_LANG > 201703L)
2405 #define JSON_HAS_CPP_20
2406 #define JSON_HAS_CPP_17
2407 #define JSON_HAS_CPP_14
2408 #elif (defined(__cplusplus) && __cplusplus > 201402L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464
2409 #define JSON_HAS_CPP_17
2410 #define JSON_HAS_CPP_14
2411 #elif (defined(__cplusplus) && __cplusplus > 201103L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1)
2412 #define JSON_HAS_CPP_14
2413 #endif
2414 // the cpp 11 flag is always specified because it is the minimal required version
2415 #define JSON_HAS_CPP_11
2416 #endif
2417
2418 #ifdef __has_include
2419 #if __has_include(<version>)
2420 #include <version>
2421 #endif
2422 #endif
2423
2424 #if !defined(JSON_HAS_FILESYSTEM) && !defined(JSON_HAS_EXPERIMENTAL_FILESYSTEM)
2425 #ifdef JSON_HAS_CPP_17
2426 #if defined(__cpp_lib_filesystem)
2427 #define JSON_HAS_FILESYSTEM 1
2428 #elif defined(__cpp_lib_experimental_filesystem)
2429 #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
2430 #elif !defined(__has_include)
2431 #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
2432 #elif __has_include(<filesystem>)
2433 #define JSON_HAS_FILESYSTEM 1
2434 #elif __has_include(<experimental/filesystem>)
2435 #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 1
2436 #endif
2437
2438 // std::filesystem does not work on MinGW GCC 8: https://sourceforge.net/p/mingw-w64/bugs/737/
2439 #if defined(__MINGW32__) && defined(__GNUC__) && __GNUC__ == 8
2440 #undef JSON_HAS_FILESYSTEM
2441 #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
2442 #endif
2443
2444 // no filesystem support before GCC 8: https://en.cppreference.com/w/cpp/compiler_support
2445 #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 8
2446 #undef JSON_HAS_FILESYSTEM
2447 #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
2448 #endif
2449
2450 // no filesystem support before Clang 7: https://en.cppreference.com/w/cpp/compiler_support
2451 #if defined(__clang_major__) && __clang_major__ < 7
2452 #undef JSON_HAS_FILESYSTEM
2453 #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
2454 #endif
2455
2456 // no filesystem support before MSVC 19.14: https://en.cppreference.com/w/cpp/compiler_support
2457 #if defined(_MSC_VER) && _MSC_VER < 1914
2458 #undef JSON_HAS_FILESYSTEM
2459 #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
2460 #endif
2461
2462 // no filesystem support before iOS 13
2463 #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED < 130000
2464 #undef JSON_HAS_FILESYSTEM
2465 #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
2466 #endif
2467
2468 // no filesystem support before macOS Catalina
2469 #if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED < 101500
2470 #undef JSON_HAS_FILESYSTEM
2471 #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
2472 #endif
2473 #endif
2474 #endif
2475
2476 #ifndef JSON_HAS_EXPERIMENTAL_FILESYSTEM
2477 #define JSON_HAS_EXPERIMENTAL_FILESYSTEM 0
2478 #endif
2479
2480 #ifndef JSON_HAS_FILESYSTEM
2481 #define JSON_HAS_FILESYSTEM 0
2482 #endif
2483
2484 #ifndef JSON_HAS_THREE_WAY_COMPARISON
2485 #if defined(__cpp_impl_three_way_comparison) && __cpp_impl_three_way_comparison >= 201907L \
2486 && defined(__cpp_lib_three_way_comparison) && __cpp_lib_three_way_comparison >= 201907L
2487 #define JSON_HAS_THREE_WAY_COMPARISON 1
2488 #else
2489 #define JSON_HAS_THREE_WAY_COMPARISON 0
2490 #endif
2491 #endif
2492
2493 #ifndef JSON_HAS_RANGES
2494 // ranges header shipping in GCC 11.1.0 (released 2021-04-27) has syntax error
2495 #if defined(__GLIBCXX__) && __GLIBCXX__ == 20210427
2496 #define JSON_HAS_RANGES 0
2497 #elif defined(__cpp_lib_ranges)
2498 #define JSON_HAS_RANGES 1
2499 #else
2500 #define JSON_HAS_RANGES 0
2501 #endif
2502 #endif
2503
2504 #ifndef JSON_HAS_STATIC_RTTI
2505 #if !defined(_HAS_STATIC_RTTI) || _HAS_STATIC_RTTI != 0
2506 #define JSON_HAS_STATIC_RTTI 1
2507 #else
2508 #define JSON_HAS_STATIC_RTTI 0
2509 #endif
2510 #endif
2511
2512 #ifdef JSON_HAS_CPP_17
2513 #define JSON_INLINE_VARIABLE inline
2514 #else
2515 #define JSON_INLINE_VARIABLE
2516 #endif
2517
2518 #if JSON_HEDLEY_HAS_ATTRIBUTE(no_unique_address)
2519 #define JSON_NO_UNIQUE_ADDRESS [[no_unique_address]]
2520 #else
2521 #define JSON_NO_UNIQUE_ADDRESS
2522 #endif
2523
2524 // disable documentation warnings on clang
2525 #if defined(__clang__)
2526 #pragma clang diagnostic push
2527 #pragma clang diagnostic ignored "-Wdocumentation"
2528 #pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
2529 #endif
2530
2531 // allow disabling exceptions
2532 #if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION)
2533 #define JSON_THROW(exception) throw exception
2534 #define JSON_TRY try
2535 #define JSON_CATCH(exception) catch(exception)
2536 #define JSON_INTERNAL_CATCH(exception) catch(exception)
2537 #else
2538 #include <cstdlib>
2539 #define JSON_THROW(exception) std::abort()
2540 #define JSON_TRY if(true)
2541 #define JSON_CATCH(exception) if(false)
2542 #define JSON_INTERNAL_CATCH(exception) if(false)
2543 #endif
2544
2545 // override exception macros
2546 #if defined(JSON_THROW_USER)
2547 #undef JSON_THROW
2548 #define JSON_THROW JSON_THROW_USER
2549 #endif
2550 #if defined(JSON_TRY_USER)
2551 #undef JSON_TRY
2552 #define JSON_TRY JSON_TRY_USER
2553 #endif
2554 #if defined(JSON_CATCH_USER)
2555 #undef JSON_CATCH
2556 #define JSON_CATCH JSON_CATCH_USER
2557 #undef JSON_INTERNAL_CATCH
2558 #define JSON_INTERNAL_CATCH JSON_CATCH_USER
2559 #endif
2560 #if defined(JSON_INTERNAL_CATCH_USER)
2561 #undef JSON_INTERNAL_CATCH
2562 #define JSON_INTERNAL_CATCH JSON_INTERNAL_CATCH_USER
2563 #endif
2564
2565 // allow overriding assert
2566 #if !defined(JSON_ASSERT)
2567 #include <cassert> // assert
2568 #define JSON_ASSERT(x) assert(x)
2569 #endif
2570
2571 // allow to access some private functions (needed by the test suite)
2572 #if defined(JSON_TESTS_PRIVATE)
2573 #define JSON_PRIVATE_UNLESS_TESTED public
2574 #else
2575 #define JSON_PRIVATE_UNLESS_TESTED private
2576 #endif
2577
2578 /*!
2579 @brief macro to briefly define a mapping between an enum and JSON
2580 @def NLOHMANN_JSON_SERIALIZE_ENUM
2581 @since version 3.4.0
2582 */
2583 #define NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...) \
2584 template<typename BasicJsonType> \
2585 inline void to_json(BasicJsonType& j, const ENUM_TYPE& e) \
2586 { \
2587 /* NOLINTNEXTLINE(modernize-type-traits) we use C++11 */ \
2588 static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
2589 /* NOLINTNEXTLINE(modernize-avoid-c-arrays) we don't want to depend on <array> */ \
2590 static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
2591 auto it = std::find_if(std::begin(m), std::end(m), \
2592 [e](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
2593 { \
2594 return ej_pair.first == e; \
2595 }); \
2596 j = ((it != std::end(m)) ? it : std::begin(m))->second; \
2597 } \
2598 template<typename BasicJsonType> \
2599 inline void from_json(const BasicJsonType& j, ENUM_TYPE& e) \
2600 { \
2601 /* NOLINTNEXTLINE(modernize-type-traits) we use C++11 */ \
2602 static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
2603 /* NOLINTNEXTLINE(modernize-avoid-c-arrays) we don't want to depend on <array> */ \
2604 static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
2605 auto it = std::find_if(std::begin(m), std::end(m), \
2606 [&j](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
2607 { \
2608 return ej_pair.second == j; \
2609 }); \
2610 e = ((it != std::end(m)) ? it : std::begin(m))->first; \
2611 }
2612
2613 // Ugly macros to avoid uglier copy-paste when specializing basic_json. They
2614 // may be removed in the future once the class is split.
2615
2616 #define NLOHMANN_BASIC_JSON_TPL_DECLARATION \
2617 template<template<typename, typename, typename...> class ObjectType, \
2618 template<typename, typename...> class ArrayType, \
2619 class StringType, class BooleanType, class NumberIntegerType, \
2620 class NumberUnsignedType, class NumberFloatType, \
2621 template<typename> class AllocatorType, \
2622 template<typename, typename = void> class JSONSerializer, \
2623 class BinaryType, \
2624 class CustomBaseClass>
2625
2626 #define NLOHMANN_BASIC_JSON_TPL \
2627 basic_json<ObjectType, ArrayType, StringType, BooleanType, \
2628 NumberIntegerType, NumberUnsignedType, NumberFloatType, \
2629 AllocatorType, JSONSerializer, BinaryType, CustomBaseClass>
2630
2631 // Macros to simplify conversion from/to types
2632
2633 #define NLOHMANN_JSON_EXPAND( x ) x
2634 #define NLOHMANN_JSON_GET_MACRO(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, _62, _63, _64, NAME,...) NAME
2635 #define NLOHMANN_JSON_PASTE(...) NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_GET_MACRO(__VA_ARGS__, \
2636 NLOHMANN_JSON_PASTE64, \
2637 NLOHMANN_JSON_PASTE63, \
2638 NLOHMANN_JSON_PASTE62, \
2639 NLOHMANN_JSON_PASTE61, \
2640 NLOHMANN_JSON_PASTE60, \
2641 NLOHMANN_JSON_PASTE59, \
2642 NLOHMANN_JSON_PASTE58, \
2643 NLOHMANN_JSON_PASTE57, \
2644 NLOHMANN_JSON_PASTE56, \
2645 NLOHMANN_JSON_PASTE55, \
2646 NLOHMANN_JSON_PASTE54, \
2647 NLOHMANN_JSON_PASTE53, \
2648 NLOHMANN_JSON_PASTE52, \
2649 NLOHMANN_JSON_PASTE51, \
2650 NLOHMANN_JSON_PASTE50, \
2651 NLOHMANN_JSON_PASTE49, \
2652 NLOHMANN_JSON_PASTE48, \
2653 NLOHMANN_JSON_PASTE47, \
2654 NLOHMANN_JSON_PASTE46, \
2655 NLOHMANN_JSON_PASTE45, \
2656 NLOHMANN_JSON_PASTE44, \
2657 NLOHMANN_JSON_PASTE43, \
2658 NLOHMANN_JSON_PASTE42, \
2659 NLOHMANN_JSON_PASTE41, \
2660 NLOHMANN_JSON_PASTE40, \
2661 NLOHMANN_JSON_PASTE39, \
2662 NLOHMANN_JSON_PASTE38, \
2663 NLOHMANN_JSON_PASTE37, \
2664 NLOHMANN_JSON_PASTE36, \
2665 NLOHMANN_JSON_PASTE35, \
2666 NLOHMANN_JSON_PASTE34, \
2667 NLOHMANN_JSON_PASTE33, \
2668 NLOHMANN_JSON_PASTE32, \
2669 NLOHMANN_JSON_PASTE31, \
2670 NLOHMANN_JSON_PASTE30, \
2671 NLOHMANN_JSON_PASTE29, \
2672 NLOHMANN_JSON_PASTE28, \
2673 NLOHMANN_JSON_PASTE27, \
2674 NLOHMANN_JSON_PASTE26, \
2675 NLOHMANN_JSON_PASTE25, \
2676 NLOHMANN_JSON_PASTE24, \
2677 NLOHMANN_JSON_PASTE23, \
2678 NLOHMANN_JSON_PASTE22, \
2679 NLOHMANN_JSON_PASTE21, \
2680 NLOHMANN_JSON_PASTE20, \
2681 NLOHMANN_JSON_PASTE19, \
2682 NLOHMANN_JSON_PASTE18, \
2683 NLOHMANN_JSON_PASTE17, \
2684 NLOHMANN_JSON_PASTE16, \
2685 NLOHMANN_JSON_PASTE15, \
2686 NLOHMANN_JSON_PASTE14, \
2687 NLOHMANN_JSON_PASTE13, \
2688 NLOHMANN_JSON_PASTE12, \
2689 NLOHMANN_JSON_PASTE11, \
2690 NLOHMANN_JSON_PASTE10, \
2691 NLOHMANN_JSON_PASTE9, \
2692 NLOHMANN_JSON_PASTE8, \
2693 NLOHMANN_JSON_PASTE7, \
2694 NLOHMANN_JSON_PASTE6, \
2695 NLOHMANN_JSON_PASTE5, \
2696 NLOHMANN_JSON_PASTE4, \
2697 NLOHMANN_JSON_PASTE3, \
2698 NLOHMANN_JSON_PASTE2, \
2699 NLOHMANN_JSON_PASTE1)(__VA_ARGS__))
2700 #define NLOHMANN_JSON_PASTE2(func, v1) func(v1)
2701 #define NLOHMANN_JSON_PASTE3(func, v1, v2) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE2(func, v2)
2702 #define NLOHMANN_JSON_PASTE4(func, v1, v2, v3) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE3(func, v2, v3)
2703 #define NLOHMANN_JSON_PASTE5(func, v1, v2, v3, v4) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE4(func, v2, v3, v4)
2704 #define NLOHMANN_JSON_PASTE6(func, v1, v2, v3, v4, v5) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE5(func, v2, v3, v4, v5)
2705 #define NLOHMANN_JSON_PASTE7(func, v1, v2, v3, v4, v5, v6) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE6(func, v2, v3, v4, v5, v6)
2706 #define NLOHMANN_JSON_PASTE8(func, v1, v2, v3, v4, v5, v6, v7) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE7(func, v2, v3, v4, v5, v6, v7)
2707 #define NLOHMANN_JSON_PASTE9(func, v1, v2, v3, v4, v5, v6, v7, v8) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE8(func, v2, v3, v4, v5, v6, v7, v8)
2708 #define NLOHMANN_JSON_PASTE10(func, v1, v2, v3, v4, v5, v6, v7, v8, v9) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE9(func, v2, v3, v4, v5, v6, v7, v8, v9)
2709 #define NLOHMANN_JSON_PASTE11(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE10(func, v2, v3, v4, v5, v6, v7, v8, v9, v10)
2710 #define NLOHMANN_JSON_PASTE12(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE11(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11)
2711 #define NLOHMANN_JSON_PASTE13(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE12(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12)
2712 #define NLOHMANN_JSON_PASTE14(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE13(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13)
2713 #define NLOHMANN_JSON_PASTE15(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE14(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14)
2714 #define NLOHMANN_JSON_PASTE16(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE15(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15)
2715 #define NLOHMANN_JSON_PASTE17(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE16(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16)
2716 #define NLOHMANN_JSON_PASTE18(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE17(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17)
2717 #define NLOHMANN_JSON_PASTE19(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE18(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18)
2718 #define NLOHMANN_JSON_PASTE20(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE19(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19)
2719 #define NLOHMANN_JSON_PASTE21(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE20(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20)
2720 #define NLOHMANN_JSON_PASTE22(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE21(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21)
2721 #define NLOHMANN_JSON_PASTE23(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE22(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22)
2722 #define NLOHMANN_JSON_PASTE24(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE23(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23)
2723 #define NLOHMANN_JSON_PASTE25(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE24(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24)
2724 #define NLOHMANN_JSON_PASTE26(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE25(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25)
2725 #define NLOHMANN_JSON_PASTE27(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE26(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26)
2726 #define NLOHMANN_JSON_PASTE28(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE27(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27)
2727 #define NLOHMANN_JSON_PASTE29(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE28(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28)
2728 #define NLOHMANN_JSON_PASTE30(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE29(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29)
2729 #define NLOHMANN_JSON_PASTE31(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE30(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30)
2730 #define NLOHMANN_JSON_PASTE32(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE31(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31)
2731 #define NLOHMANN_JSON_PASTE33(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE32(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32)
2732 #define NLOHMANN_JSON_PASTE34(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE33(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33)
2733 #define NLOHMANN_JSON_PASTE35(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE34(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34)
2734 #define NLOHMANN_JSON_PASTE36(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE35(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35)
2735 #define NLOHMANN_JSON_PASTE37(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE36(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36)
2736 #define NLOHMANN_JSON_PASTE38(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE37(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37)
2737 #define NLOHMANN_JSON_PASTE39(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE38(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38)
2738 #define NLOHMANN_JSON_PASTE40(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE39(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39)
2739 #define NLOHMANN_JSON_PASTE41(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE40(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40)
2740 #define NLOHMANN_JSON_PASTE42(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE41(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41)
2741 #define NLOHMANN_JSON_PASTE43(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE42(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42)
2742 #define NLOHMANN_JSON_PASTE44(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE43(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43)
2743 #define NLOHMANN_JSON_PASTE45(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE44(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44)
2744 #define NLOHMANN_JSON_PASTE46(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE45(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45)
2745 #define NLOHMANN_JSON_PASTE47(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE46(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46)
2746 #define NLOHMANN_JSON_PASTE48(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE47(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47)
2747 #define NLOHMANN_JSON_PASTE49(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE48(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48)
2748 #define NLOHMANN_JSON_PASTE50(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE49(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49)
2749 #define NLOHMANN_JSON_PASTE51(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE50(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50)
2750 #define NLOHMANN_JSON_PASTE52(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE51(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51)
2751 #define NLOHMANN_JSON_PASTE53(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE52(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52)
2752 #define NLOHMANN_JSON_PASTE54(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE53(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53)
2753 #define NLOHMANN_JSON_PASTE55(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE54(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54)
2754 #define NLOHMANN_JSON_PASTE56(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE55(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55)
2755 #define NLOHMANN_JSON_PASTE57(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE56(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56)
2756 #define NLOHMANN_JSON_PASTE58(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE57(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57)
2757 #define NLOHMANN_JSON_PASTE59(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE58(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58)
2758 #define NLOHMANN_JSON_PASTE60(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE59(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59)
2759 #define NLOHMANN_JSON_PASTE61(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE60(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60)
2760 #define NLOHMANN_JSON_PASTE62(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE61(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61)
2761 #define NLOHMANN_JSON_PASTE63(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE62(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62)
2762 #define NLOHMANN_JSON_PASTE64(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62, v63) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE63(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62, v63)
2763
2764 #define NLOHMANN_JSON_TO(v1) nlohmann_json_j[#v1] = nlohmann_json_t.v1;
2765 #define NLOHMANN_JSON_FROM(v1) nlohmann_json_j.at(#v1).get_to(nlohmann_json_t.v1);
2766 #define NLOHMANN_JSON_FROM_WITH_DEFAULT(v1) nlohmann_json_t.v1 = !nlohmann_json_j.is_null() ? nlohmann_json_j.value(#v1, nlohmann_json_default_obj.v1) : nlohmann_json_default_obj.v1;
2767
2768 /*!
2769 @brief macro
2770 @def NLOHMANN_DEFINE_TYPE_INTRUSIVE
2771 @since version 3.9.0
2772 @sa https://json.nlohmann.me/api/macros/nlohmann_define_type_intrusive/
2773 */
2774 #define NLOHMANN_DEFINE_TYPE_INTRUSIVE(Type, ...) \
2775 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2776 friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
2777 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2778 friend void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }
2779
2780 /*!
2781 @brief macro
2782 @def NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT
2783 @since version 3.11.0
2784 @sa https://json.nlohmann.me/api/macros/nlohmann_define_type_intrusive/
2785 */
2786 #define NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Type, ...) \
2787 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2788 friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
2789 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2790 friend void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) { const Type nlohmann_json_default_obj{}; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) }
2791
2792 /*!
2793 @brief macro
2794 @def NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE
2795 @since version 3.11.3
2796 @sa https://json.nlohmann.me/api/macros/nlohmann_define_type_intrusive/
2797 */
2798 #define NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE(Type, ...) \
2799 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2800 friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) }
2801
2802 /*!
2803 @brief macro
2804 @def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE
2805 @since version 3.9.0
2806 @sa https://json.nlohmann.me/api/macros/nlohmann_define_type_non_intrusive/
2807 */
2808 #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Type, ...) \
2809 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2810 void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
2811 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2812 void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }
2813
2814 /*!
2815 @brief macro
2816 @def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT
2817 @since version 3.11.0
2818 @sa https://json.nlohmann.me/api/macros/nlohmann_define_type_non_intrusive/
2819 */
2820 #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Type, ...) \
2821 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2822 void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
2823 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2824 void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) { const Type nlohmann_json_default_obj{}; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) }
2825
2826 /*!
2827 @brief macro
2828 @def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE
2829 @since version 3.11.3
2830 @sa https://json.nlohmann.me/api/macros/nlohmann_define_type_non_intrusive/
2831 */
2832 #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE(Type, ...) \
2833 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2834 void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) }
2835
2836 /*!
2837 @brief macro
2838 @def NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE
2839 @since version 3.12.0
2840 @sa https://json.nlohmann.me/api/macros/nlohmann_define_derived_type/
2841 */
2842 #define NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE(Type, BaseType, ...) \
2843 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2844 friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { nlohmann::to_json(nlohmann_json_j, static_cast<const BaseType &>(nlohmann_json_t)); NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
2845 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2846 friend void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) { nlohmann::from_json(nlohmann_json_j, static_cast<BaseType&>(nlohmann_json_t)); NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }
2847
2848 /*!
2849 @brief macro
2850 @def NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT
2851 @since version 3.12.0
2852 @sa https://json.nlohmann.me/api/macros/nlohmann_define_derived_type/
2853 */
2854 #define NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT(Type, BaseType, ...) \
2855 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2856 friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { nlohmann::to_json(nlohmann_json_j, static_cast<const BaseType&>(nlohmann_json_t)); NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
2857 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2858 friend void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) { nlohmann::from_json(nlohmann_json_j, static_cast<BaseType&>(nlohmann_json_t)); const Type nlohmann_json_default_obj{}; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) }
2859
2860 /*!
2861 @brief macro
2862 @def NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE
2863 @since version 3.12.0
2864 @sa https://json.nlohmann.me/api/macros/nlohmann_define_derived_type/
2865 */
2866 #define NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE(Type, BaseType, ...) \
2867 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2868 friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { nlohmann::to_json(nlohmann_json_j, static_cast<const BaseType &>(nlohmann_json_t)); NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) }
2869
2870 /*!
2871 @brief macro
2872 @def NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE
2873 @since version 3.12.0
2874 @sa https://json.nlohmann.me/api/macros/nlohmann_define_derived_type/
2875 */
2876 #define NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE(Type, BaseType, ...) \
2877 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2878 void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { nlohmann::to_json(nlohmann_json_j, static_cast<const BaseType &>(nlohmann_json_t)); NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
2879 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2880 void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) { nlohmann::from_json(nlohmann_json_j, static_cast<BaseType&>(nlohmann_json_t)); NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }
2881
2882 /*!
2883 @brief macro
2884 @def NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT
2885 @since version 3.12.0
2886 @sa https://json.nlohmann.me/api/macros/nlohmann_define_derived_type/
2887 */
2888 #define NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Type, BaseType, ...) \
2889 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2890 void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { nlohmann::to_json(nlohmann_json_j, static_cast<const BaseType &>(nlohmann_json_t)); NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
2891 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2892 void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) { nlohmann::from_json(nlohmann_json_j, static_cast<BaseType&>(nlohmann_json_t)); const Type nlohmann_json_default_obj{}; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) }
2893
2894 /*!
2895 @brief macro
2896 @def NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE
2897 @since version 3.12.0
2898 @sa https://json.nlohmann.me/api/macros/nlohmann_define_derived_type/
2899 */
2900 #define NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE(Type, BaseType, ...) \
2901 template<typename BasicJsonType, nlohmann::detail::enable_if_t<nlohmann::detail::is_basic_json<BasicJsonType>::value, int> = 0> \
2902 void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { nlohmann::to_json(nlohmann_json_j, static_cast<const BaseType &>(nlohmann_json_t)); NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) }
2903
2904 // inspired from https://stackoverflow.com/a/26745591
2905 // allows calling any std function as if (e.g., with begin):
2906 // using std::begin; begin(x);
2907 //
2908 // it allows using the detected idiom to retrieve the return type
2909 // of such an expression
2910 #define NLOHMANN_CAN_CALL_STD_FUNC_IMPL(std_name) \
2911 namespace detail { \
2912 using std::std_name; \
2913 \
2914 template<typename... T> \
2915 using result_of_##std_name = decltype(std_name(std::declval<T>()...)); \
2916 } \
2917 \
2918 namespace detail2 { \
2919 struct std_name##_tag \
2920 { \
2921 }; \
2922 \
2923 template<typename... T> \
2924 std_name##_tag std_name(T&&...); \
2925 \
2926 template<typename... T> \
2927 using result_of_##std_name = decltype(std_name(std::declval<T>()...)); \
2928 \
2929 template<typename... T> \
2930 struct would_call_std_##std_name \
2931 { \
2932 static constexpr auto const value = ::nlohmann::detail:: \
2933 is_detected_exact<std_name##_tag, result_of_##std_name, T...>::value; \
2934 }; \
2935 } /* namespace detail2 */ \
2936 \
2937 template<typename... T> \
2938 struct would_call_std_##std_name : detail2::would_call_std_##std_name<T...> \
2939 { \
2940 }
2941
2942 #ifndef JSON_USE_IMPLICIT_CONVERSIONS
2943 #define JSON_USE_IMPLICIT_CONVERSIONS 1
2944 #endif
2945
2946 #if JSON_USE_IMPLICIT_CONVERSIONS
2947 #define JSON_EXPLICIT
2948 #else
2949 #define JSON_EXPLICIT explicit
2950 #endif
2951
2952 #ifndef JSON_DISABLE_ENUM_SERIALIZATION
2953 #define JSON_DISABLE_ENUM_SERIALIZATION 0
2954 #endif
2955
2956 #ifndef JSON_USE_GLOBAL_UDLS
2957 #define JSON_USE_GLOBAL_UDLS 1
2958 #endif
2959
2960 #if JSON_HAS_THREE_WAY_COMPARISON
2961 #include <compare> // partial_ordering
2962 #endif
2963
2964 NLOHMANN_JSON_NAMESPACE_BEGIN
2965 namespace detail
2966 {
2967
2968 ///////////////////////////
2969 // JSON type enumeration //
2970 ///////////////////////////
2971
2972 /*!
2973 @brief the JSON type enumeration
2974
2975 This enumeration collects the different JSON types. It is internally used to
2976 distinguish the stored values, and the functions @ref basic_json::is_null(),
2977 @ref basic_json::is_object(), @ref basic_json::is_array(),
2978 @ref basic_json::is_string(), @ref basic_json::is_boolean(),
2979 @ref basic_json::is_number() (with @ref basic_json::is_number_integer(),
2980 @ref basic_json::is_number_unsigned(), and @ref basic_json::is_number_float()),
2981 @ref basic_json::is_discarded(), @ref basic_json::is_primitive(), and
2982 @ref basic_json::is_structured() rely on it.
2983
2984 @note There are three enumeration entries (number_integer, number_unsigned, and
2985 number_float), because the library distinguishes these three types for numbers:
2986 @ref basic_json::number_unsigned_t is used for unsigned integers,
2987 @ref basic_json::number_integer_t is used for signed integers, and
2988 @ref basic_json::number_float_t is used for floating-point numbers or to
2989 approximate integers which do not fit in the limits of their respective type.
2990
2991 @sa see @ref basic_json::basic_json(const value_t value_type) -- create a JSON
2992 value with the default value for a given type
2993
2994 @since version 1.0.0
2995 */
2996 enum class value_t : std::uint8_t
2997 {
2998 null, ///< null value
2999 object, ///< object (unordered set of name/value pairs)
3000 array, ///< array (ordered collection of values)
3001 string, ///< string value
3002 boolean, ///< boolean value
3003 number_integer, ///< number value (signed integer)
3004 number_unsigned, ///< number value (unsigned integer)
3005 number_float, ///< number value (floating-point)
3006 binary, ///< binary array (ordered collection of bytes)
3007 discarded ///< discarded by the parser callback function
3008 };
3009
3010 /*!
3011 @brief comparison operator for JSON types
3012
3013 Returns an ordering that is similar to Python:
3014 - order: null < boolean < number < object < array < string < binary
3015 - furthermore, each type is not smaller than itself
3016 - discarded values are not comparable
3017 - binary is represented as a b"" string in python and directly comparable to a
3018 string; however, making a binary array directly comparable with a string would
3019 be surprising behavior in a JSON file.
3020
3021 @since version 1.0.0
3022 */
3023 #if JSON_HAS_THREE_WAY_COMPARISON
3024 inline std::partial_ordering operator<=>(const value_t lhs, const value_t rhs) noexcept // *NOPAD*
3025 #else
3026 inline bool operator<(const value_t lhs, const value_t rhs) noexcept
3027 #endif
3028 {
3029 static constexpr std::array<std::uint8_t, 9> order = {{
3030 0 /* null */, 3 /* object */, 4 /* array */, 5 /* string */,
3031 1 /* boolean */, 2 /* integer */, 2 /* unsigned */, 2 /* float */,
3032 6 /* binary */
3033 }
3034 };
3035
3036 const auto l_index = static_cast<std::size_t>(lhs);
3037 const auto r_index = static_cast<std::size_t>(rhs);
3038 #if JSON_HAS_THREE_WAY_COMPARISON
3039 if (l_index < order.size() && r_index < order.size())
3040 {
3041 return order[l_index] <=> order[r_index]; // *NOPAD*
3042 }
3043 return std::partial_ordering::unordered;
3044 #else
3045 return l_index < order.size() && r_index < order.size() && order[l_index] < order[r_index];
3046 #endif
3047 }
3048
3049 // GCC selects the built-in operator< over an operator rewritten from
3050 // a user-defined spaceship operator
3051 // Clang, MSVC, and ICC select the rewritten candidate
3052 // (see GCC bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105200)
3053 #if JSON_HAS_THREE_WAY_COMPARISON && defined(__GNUC__)
3054 inline bool operator<(const value_t lhs, const value_t rhs) noexcept
3055 {
3056 return std::is_lt(lhs <=> rhs); // *NOPAD*
3057 }
3058 #endif
3059
3060 } // namespace detail
3061 NLOHMANN_JSON_NAMESPACE_END
3062
3063 // #include <nlohmann/detail/string_escape.hpp>
3064 // __ _____ _____ _____
3065 // __| | __| | | | JSON for Modern C++
3066 // | | |__ | | | | | | version 3.12.0
3067 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
3068 //
3069 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
3070 // SPDX-License-Identifier: MIT
3071
3072
3073
3074 // #include <nlohmann/detail/abi_macros.hpp>
3075
3076
3077 NLOHMANN_JSON_NAMESPACE_BEGIN
3078 namespace detail
3079 {
3080
3081 /*!
3082 @brief replace all occurrences of a substring by another string
3083
3084 @param[in,out] s the string to manipulate; changed so that all
3085 occurrences of @a f are replaced with @a t
3086 @param[in] f the substring to replace with @a t
3087 @param[in] t the string to replace @a f
3088
3089 @pre The search string @a f must not be empty. **This precondition is
3090 enforced with an assertion.**
3091
3092 @since version 2.0.0
3093 */
3094 template<typename StringType>
3095 inline void replace_substring(StringType& s, const StringType& f,
3096 const StringType& t)
3097 {
3098 JSON_ASSERT(!f.empty());
3099 for (auto pos = s.find(f); // find first occurrence of f
3100 pos != StringType::npos; // make sure f was found
3101 s.replace(pos, f.size(), t), // replace with t, and
3102 pos = s.find(f, pos + t.size())) // find next occurrence of f
3103 {}
3104 }
3105
3106 /*!
3107 * @brief string escaping as described in RFC 6901 (Sect. 4)
3108 * @param[in] s string to escape
3109 * @return escaped string
3110 *
3111 * Note the order of escaping "~" to "~0" and "/" to "~1" is important.
3112 */
3113 template<typename StringType>
3114 inline StringType escape(StringType s)
3115 {
3116 replace_substring(s, StringType{"~"}, StringType{"~0"});
3117 replace_substring(s, StringType{"/"}, StringType{"~1"});
3118 return s;
3119 }
3120
3121 /*!
3122 * @brief string unescaping as described in RFC 6901 (Sect. 4)
3123 * @param[in] s string to unescape
3124 * @return unescaped string
3125 *
3126 * Note the order of escaping "~1" to "/" and "~0" to "~" is important.
3127 */
3128 template<typename StringType>
3129 static void unescape(StringType& s)
3130 {
3131 replace_substring(s, StringType{"~1"}, StringType{"/"});
3132 replace_substring(s, StringType{"~0"}, StringType{"~"});
3133 }
3134
3135 } // namespace detail
3136 NLOHMANN_JSON_NAMESPACE_END
3137
3138 // #include <nlohmann/detail/input/position_t.hpp>
3139 // __ _____ _____ _____
3140 // __| | __| | | | JSON for Modern C++
3141 // | | |__ | | | | | | version 3.12.0
3142 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
3143 //
3144 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
3145 // SPDX-License-Identifier: MIT
3146
3147
3148
3149 #include <cstddef> // size_t
3150
3151 // #include <nlohmann/detail/abi_macros.hpp>
3152
3153
3154 NLOHMANN_JSON_NAMESPACE_BEGIN
3155 namespace detail
3156 {
3157
3158 /// struct to capture the start position of the current token
3159 struct position_t
3160 {
3161 /// the total number of characters read
3162 std::size_t chars_read_total = 0;
3163 /// the number of characters read in the current line
3164 std::size_t chars_read_current_line = 0;
3165 /// the number of lines read
3166 std::size_t lines_read = 0;
3167
3168 /// conversion to size_t to preserve SAX interface
3169 constexpr operator size_t() const
3170 {
3171 return chars_read_total;
3172 }
3173 };
3174
3175 } // namespace detail
3176 NLOHMANN_JSON_NAMESPACE_END
3177
3178 // #include <nlohmann/detail/macro_scope.hpp>
3179
3180 // #include <nlohmann/detail/meta/cpp_future.hpp>
3181 // __ _____ _____ _____
3182 // __| | __| | | | JSON for Modern C++
3183 // | | |__ | | | | | | version 3.12.0
3184 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
3185 //
3186 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
3187 // SPDX-FileCopyrightText: 2018 The Abseil Authors
3188 // SPDX-License-Identifier: MIT
3189
3190
3191
3192 #include <array> // array
3193 #include <cstddef> // size_t
3194 #include <type_traits> // conditional, enable_if, false_type, integral_constant, is_constructible, is_integral, is_same, remove_cv, remove_reference, true_type
3195 #include <utility> // index_sequence, make_index_sequence, index_sequence_for
3196
3197 // #include <nlohmann/detail/macro_scope.hpp>
3198
3199
3200 NLOHMANN_JSON_NAMESPACE_BEGIN
3201 namespace detail
3202 {
3203
3204 template<typename T>
3205 using uncvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
3206
3207 #ifdef JSON_HAS_CPP_14
3208
3209 // the following utilities are natively available in C++14
3210 using std::enable_if_t;
3211 using std::index_sequence;
3212 using std::make_index_sequence;
3213 using std::index_sequence_for;
3214
3215 #else
3216
3217 // alias templates to reduce boilerplate
3218 template<bool B, typename T = void>
3219 using enable_if_t = typename std::enable_if<B, T>::type;
3220
3221 // The following code is taken from https://github.com/abseil/abseil-cpp/blob/10cb35e459f5ecca5b2ff107635da0bfa41011b4/absl/utility/utility.h
3222 // which is part of Google Abseil (https://github.com/abseil/abseil-cpp), licensed under the Apache License 2.0.
3223
3224 //// START OF CODE FROM GOOGLE ABSEIL
3225
3226 // integer_sequence
3227 //
3228 // Class template representing a compile-time integer sequence. An instantiation
3229 // of `integer_sequence<T, Ints...>` has a sequence of integers encoded in its
3230 // type through its template arguments (which is a common need when
3231 // working with C++11 variadic templates). `absl::integer_sequence` is designed
3232 // to be a drop-in replacement for C++14's `std::integer_sequence`.
3233 //
3234 // Example:
3235 //
3236 // template< class T, T... Ints >
3237 // void user_function(integer_sequence<T, Ints...>);
3238 //
3239 // int main()
3240 // {
3241 // // user_function's `T` will be deduced to `int` and `Ints...`
3242 // // will be deduced to `0, 1, 2, 3, 4`.
3243 // user_function(make_integer_sequence<int, 5>());
3244 // }
3245 template <typename T, T... Ints>
3246 struct integer_sequence
3247 {
3248 using value_type = T;
3249 static constexpr std::size_t size() noexcept
3250 {
3251 return sizeof...(Ints);
3252 }
3253 };
3254
3255 // index_sequence
3256 //
3257 // A helper template for an `integer_sequence` of `size_t`,
3258 // `absl::index_sequence` is designed to be a drop-in replacement for C++14's
3259 // `std::index_sequence`.
3260 template <size_t... Ints>
3261 using index_sequence = integer_sequence<size_t, Ints...>;
3262
3263 namespace utility_internal
3264 {
3265
3266 template <typename Seq, size_t SeqSize, size_t Rem>
3267 struct Extend;
3268
3269 // Note that SeqSize == sizeof...(Ints). It's passed explicitly for efficiency.
3270 template <typename T, T... Ints, size_t SeqSize>
3271 struct Extend<integer_sequence<T, Ints...>, SeqSize, 0>
3272 {
3273 using type = integer_sequence < T, Ints..., (Ints + SeqSize)... >;
3274 };
3275
3276 template <typename T, T... Ints, size_t SeqSize>
3277 struct Extend<integer_sequence<T, Ints...>, SeqSize, 1>
3278 {
3279 using type = integer_sequence < T, Ints..., (Ints + SeqSize)..., 2 * SeqSize >;
3280 };
3281
3282 // Recursion helper for 'make_integer_sequence<T, N>'.
3283 // 'Gen<T, N>::type' is an alias for 'integer_sequence<T, 0, 1, ... N-1>'.
3284 template <typename T, size_t N>
3285 struct Gen
3286 {
3287 using type =
3288 typename Extend < typename Gen < T, N / 2 >::type, N / 2, N % 2 >::type;
3289 };
3290
3291 template <typename T>
3292 struct Gen<T, 0>
3293 {
3294 using type = integer_sequence<T>;
3295 };
3296
3297 } // namespace utility_internal
3298
3299 // Compile-time sequences of integers
3300
3301 // make_integer_sequence
3302 //
3303 // This template alias is equivalent to
3304 // `integer_sequence<int, 0, 1, ..., N-1>`, and is designed to be a drop-in
3305 // replacement for C++14's `std::make_integer_sequence`.
3306 template <typename T, T N>
3307 using make_integer_sequence = typename utility_internal::Gen<T, N>::type;
3308
3309 // make_index_sequence
3310 //
3311 // This template alias is equivalent to `index_sequence<0, 1, ..., N-1>`,
3312 // and is designed to be a drop-in replacement for C++14's
3313 // `std::make_index_sequence`.
3314 template <size_t N>
3315 using make_index_sequence = make_integer_sequence<size_t, N>;
3316
3317 // index_sequence_for
3318 //
3319 // Converts a typename pack into an index sequence of the same length, and
3320 // is designed to be a drop-in replacement for C++14's
3321 // `std::index_sequence_for()`
3322 template <typename... Ts>
3323 using index_sequence_for = make_index_sequence<sizeof...(Ts)>;
3324
3325 //// END OF CODE FROM GOOGLE ABSEIL
3326
3327 #endif
3328
3329 // dispatch utility (taken from ranges-v3)
3330 template<unsigned N> struct priority_tag : priority_tag < N - 1 > {};
3331 template<> struct priority_tag<0> {};
3332
3333 // taken from ranges-v3
3334 template<typename T>
3335 struct static_const
3336 {
3337 static JSON_INLINE_VARIABLE constexpr T value{};
3338 };
3339
3340 #ifndef JSON_HAS_CPP_17
3341 template<typename T>
3342 constexpr T static_const<T>::value;
3343 #endif
3344
3345 template<typename T, typename... Args>
3346 constexpr std::array<T, sizeof...(Args)> make_array(Args&& ... args)
3347 {
3348 return std::array<T, sizeof...(Args)> {{static_cast<T>(std::forward<Args>(args))...}};
3349 }
3350
3351 } // namespace detail
3352 NLOHMANN_JSON_NAMESPACE_END
3353
3354 // #include <nlohmann/detail/meta/type_traits.hpp>
3355 // __ _____ _____ _____
3356 // __| | __| | | | JSON for Modern C++
3357 // | | |__ | | | | | | version 3.12.0
3358 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
3359 //
3360 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
3361 // SPDX-License-Identifier: MIT
3362
3363
3364
3365 #include <limits> // numeric_limits
3366 #include <string> // char_traits
3367 #include <tuple> // tuple
3368 #include <type_traits> // false_type, is_constructible, is_integral, is_same, true_type
3369 #include <utility> // declval
3370
3371 // #include <nlohmann/detail/iterators/iterator_traits.hpp>
3372 // __ _____ _____ _____
3373 // __| | __| | | | JSON for Modern C++
3374 // | | |__ | | | | | | version 3.12.0
3375 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
3376 //
3377 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
3378 // SPDX-License-Identifier: MIT
3379
3380
3381
3382 #include <iterator> // random_access_iterator_tag
3383
3384 // #include <nlohmann/detail/abi_macros.hpp>
3385
3386 // #include <nlohmann/detail/meta/void_t.hpp>
3387
3388 // #include <nlohmann/detail/meta/cpp_future.hpp>
3389
3390
3391 NLOHMANN_JSON_NAMESPACE_BEGIN
3392 namespace detail
3393 {
3394
3395 template<typename It, typename = void>
3396 struct iterator_types {};
3397
3398 template<typename It>
3399 struct iterator_types <
3400 It,
3401 void_t<typename It::difference_type, typename It::value_type, typename It::pointer,
3402 typename It::reference, typename It::iterator_category >>
3403 {
3404 using difference_type = typename It::difference_type;
3405 using value_type = typename It::value_type;
3406 using pointer = typename It::pointer;
3407 using reference = typename It::reference;
3408 using iterator_category = typename It::iterator_category;
3409 };
3410
3411 // This is required as some compilers implement std::iterator_traits in a way that
3412 // doesn't work with SFINAE. See https://github.com/nlohmann/json/issues/1341.
3413 template<typename T, typename = void>
3414 struct iterator_traits
3415 {
3416 };
3417
3418 template<typename T>
3419 struct iterator_traits < T, enable_if_t < !std::is_pointer<T>::value >>
3420 : iterator_types<T>
3421 {
3422 };
3423
3424 template<typename T>
3425 struct iterator_traits<T*, enable_if_t<std::is_object<T>::value>>
3426 {
3427 using iterator_category = std::random_access_iterator_tag;
3428 using value_type = T;
3429 using difference_type = ptrdiff_t;
3430 using pointer = T*;
3431 using reference = T&;
3432 };
3433
3434 } // namespace detail
3435 NLOHMANN_JSON_NAMESPACE_END
3436
3437 // #include <nlohmann/detail/macro_scope.hpp>
3438
3439 // #include <nlohmann/detail/meta/call_std/begin.hpp>
3440 // __ _____ _____ _____
3441 // __| | __| | | | JSON for Modern C++
3442 // | | |__ | | | | | | version 3.12.0
3443 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
3444 //
3445 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
3446 // SPDX-License-Identifier: MIT
3447
3448
3449
3450 // #include <nlohmann/detail/macro_scope.hpp>
3451
3452
3453 NLOHMANN_JSON_NAMESPACE_BEGIN
3454
3455 NLOHMANN_CAN_CALL_STD_FUNC_IMPL(begin);
3456
3457 NLOHMANN_JSON_NAMESPACE_END
3458
3459 // #include <nlohmann/detail/meta/call_std/end.hpp>
3460 // __ _____ _____ _____
3461 // __| | __| | | | JSON for Modern C++
3462 // | | |__ | | | | | | version 3.12.0
3463 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
3464 //
3465 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
3466 // SPDX-License-Identifier: MIT
3467
3468
3469
3470 // #include <nlohmann/detail/macro_scope.hpp>
3471
3472
3473 NLOHMANN_JSON_NAMESPACE_BEGIN
3474
3475 NLOHMANN_CAN_CALL_STD_FUNC_IMPL(end);
3476
3477 NLOHMANN_JSON_NAMESPACE_END
3478
3479 // #include <nlohmann/detail/meta/cpp_future.hpp>
3480
3481 // #include <nlohmann/detail/meta/detected.hpp>
3482
3483 // #include <nlohmann/json_fwd.hpp>
3484 // __ _____ _____ _____
3485 // __| | __| | | | JSON for Modern C++
3486 // | | |__ | | | | | | version 3.12.0
3487 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
3488 //
3489 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
3490 // SPDX-License-Identifier: MIT
3491
3492 #ifndef INCLUDE_NLOHMANN_JSON_FWD_HPP_
3493 #define INCLUDE_NLOHMANN_JSON_FWD_HPP_
3494
3495 #include <cstdint> // int64_t, uint64_t
3496 #include <map> // map
3497 #include <memory> // allocator
3498 #include <string> // string
3499 #include <vector> // vector
3500
3501 // #include <nlohmann/detail/abi_macros.hpp>
3502
3503
3504 /*!
3505 @brief namespace for Niels Lohmann
3506 @see https://github.com/nlohmann
3507 @since version 1.0.0
3508 */
3509 NLOHMANN_JSON_NAMESPACE_BEGIN
3510
3511 /*!
3512 @brief default JSONSerializer template argument
3513
3514 This serializer ignores the template arguments and uses ADL
3515 ([argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl))
3516 for serialization.
3517 */
3518 template<typename T = void, typename SFINAE = void>
3519 struct adl_serializer;
3520
3521 /// a class to store JSON values
3522 /// @sa https://json.nlohmann.me/api/basic_json/
3523 template<template<typename U, typename V, typename... Args> class ObjectType =
3524 std::map,
3525 template<typename U, typename... Args> class ArrayType = std::vector,
3526 class StringType = std::string, class BooleanType = bool,
3527 class NumberIntegerType = std::int64_t,
3528 class NumberUnsignedType = std::uint64_t,
3529 class NumberFloatType = double,
3530 template<typename U> class AllocatorType = std::allocator,
3531 template<typename T, typename SFINAE = void> class JSONSerializer =
3532 adl_serializer,
3533 class BinaryType = std::vector<std::uint8_t>, // cppcheck-suppress syntaxError
3534 class CustomBaseClass = void>
3535 class basic_json;
3536
3537 /// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document
3538 /// @sa https://json.nlohmann.me/api/json_pointer/
3539 template<typename RefStringType>
3540 class json_pointer;
3541
3542 /*!
3543 @brief default specialization
3544 @sa https://json.nlohmann.me/api/json/
3545 */
3546 using json = basic_json<>;
3547
3548 /// @brief a minimal map-like container that preserves insertion order
3549 /// @sa https://json.nlohmann.me/api/ordered_map/
3550 template<class Key, class T, class IgnoredLess, class Allocator>
3551 struct ordered_map;
3552
3553 /// @brief specialization that maintains the insertion order of object keys
3554 /// @sa https://json.nlohmann.me/api/ordered_json/
3555 using ordered_json = basic_json<nlohmann::ordered_map>;
3556
3557 NLOHMANN_JSON_NAMESPACE_END
3558
3559 #endif // INCLUDE_NLOHMANN_JSON_FWD_HPP_
3560
3561
3562 NLOHMANN_JSON_NAMESPACE_BEGIN
3563 /*!
3564 @brief detail namespace with internal helper functions
3565
3566 This namespace collects functions that should not be exposed,
3567 implementations of some @ref basic_json methods, and meta-programming helpers.
3568
3569 @since version 2.1.0
3570 */
3571 namespace detail
3572 {
3573
3574 /////////////
3575 // helpers //
3576 /////////////
3577
3578 // Note to maintainers:
3579 //
3580 // Every trait in this file expects a non CV-qualified type.
3581 // The only exceptions are in the 'aliases for detected' section
3582 // (i.e. those of the form: decltype(T::member_function(std::declval<T>())))
3583 //
3584 // In this case, T has to be properly CV-qualified to constraint the function arguments
3585 // (e.g. to_json(BasicJsonType&, const T&))
3586
3587 template<typename> struct is_basic_json : std::false_type {};
3588
3589 NLOHMANN_BASIC_JSON_TPL_DECLARATION
3590 struct is_basic_json<NLOHMANN_BASIC_JSON_TPL> : std::true_type {};
3591
3592 // used by exceptions create() member functions
3593 // true_type for pointer to possibly cv-qualified basic_json or std::nullptr_t
3594 // false_type otherwise
3595 template<typename BasicJsonContext>
3596 struct is_basic_json_context :
3597 std::integral_constant < bool,
3598 is_basic_json<typename std::remove_cv<typename std::remove_pointer<BasicJsonContext>::type>::type>::value
3599 || std::is_same<BasicJsonContext, std::nullptr_t>::value >
3600 {};
3601
3602 //////////////////////
3603 // json_ref helpers //
3604 //////////////////////
3605
3606 template<typename>
3607 class json_ref;
3608
3609 template<typename>
3610 struct is_json_ref : std::false_type {};
3611
3612 template<typename T>
3613 struct is_json_ref<json_ref<T>> : std::true_type {};
3614
3615 //////////////////////////
3616 // aliases for detected //
3617 //////////////////////////
3618
3619 template<typename T>
3620 using mapped_type_t = typename T::mapped_type;
3621
3622 template<typename T>
3623 using key_type_t = typename T::key_type;
3624
3625 template<typename T>
3626 using value_type_t = typename T::value_type;
3627
3628 template<typename T>
3629 using difference_type_t = typename T::difference_type;
3630
3631 template<typename T>
3632 using pointer_t = typename T::pointer;
3633
3634 template<typename T>
3635 using reference_t = typename T::reference;
3636
3637 template<typename T>
3638 using iterator_category_t = typename T::iterator_category;
3639
3640 template<typename T, typename... Args>
3641 using to_json_function = decltype(T::to_json(std::declval<Args>()...));
3642
3643 template<typename T, typename... Args>
3644 using from_json_function = decltype(T::from_json(std::declval<Args>()...));
3645
3646 template<typename T, typename U>
3647 using get_template_function = decltype(std::declval<T>().template get<U>());
3648
3649 // trait checking if JSONSerializer<T>::from_json(json const&, udt&) exists
3650 template<typename BasicJsonType, typename T, typename = void>
3651 struct has_from_json : std::false_type {};
3652
3653 // trait checking if j.get<T> is valid
3654 // use this trait instead of std::is_constructible or std::is_convertible,
3655 // both rely on, or make use of implicit conversions, and thus fail when T
3656 // has several constructors/operator= (see https://github.com/nlohmann/json/issues/958)
3657 template <typename BasicJsonType, typename T>
3658 struct is_getable
3659 {
3660 static constexpr bool value = is_detected<get_template_function, const BasicJsonType&, T>::value;
3661 };
3662
3663 template<typename BasicJsonType, typename T>
3664 struct has_from_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
3665 {
3666 using serializer = typename BasicJsonType::template json_serializer<T, void>;
3667
3668 static constexpr bool value =
3669 is_detected_exact<void, from_json_function, serializer,
3670 const BasicJsonType&, T&>::value;
3671 };
3672
3673 // This trait checks if JSONSerializer<T>::from_json(json const&) exists
3674 // this overload is used for non-default-constructible user-defined-types
3675 template<typename BasicJsonType, typename T, typename = void>
3676 struct has_non_default_from_json : std::false_type {};
3677
3678 template<typename BasicJsonType, typename T>
3679 struct has_non_default_from_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
3680 {
3681 using serializer = typename BasicJsonType::template json_serializer<T, void>;
3682
3683 static constexpr bool value =
3684 is_detected_exact<T, from_json_function, serializer,
3685 const BasicJsonType&>::value;
3686 };
3687
3688 // This trait checks if BasicJsonType::json_serializer<T>::to_json exists
3689 // Do not evaluate the trait when T is a basic_json type, to avoid template instantiation infinite recursion.
3690 template<typename BasicJsonType, typename T, typename = void>
3691 struct has_to_json : std::false_type {};
3692
3693 template<typename BasicJsonType, typename T>
3694 struct has_to_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
3695 {
3696 using serializer = typename BasicJsonType::template json_serializer<T, void>;
3697
3698 static constexpr bool value =
3699 is_detected_exact<void, to_json_function, serializer, BasicJsonType&,
3700 T>::value;
3701 };
3702
3703 template<typename T>
3704 using detect_key_compare = typename T::key_compare;
3705
3706 template<typename T>
3707 struct has_key_compare : std::integral_constant<bool, is_detected<detect_key_compare, T>::value> {};
3708
3709 // obtains the actual object key comparator
3710 template<typename BasicJsonType>
3711 struct actual_object_comparator
3712 {
3713 using object_t = typename BasicJsonType::object_t;
3714 using object_comparator_t = typename BasicJsonType::default_object_comparator_t;
3715 using type = typename std::conditional < has_key_compare<object_t>::value,
3716 typename object_t::key_compare, object_comparator_t>::type;
3717 };
3718
3719 template<typename BasicJsonType>
3720 using actual_object_comparator_t = typename actual_object_comparator<BasicJsonType>::type;
3721
3722 /////////////////
3723 // char_traits //
3724 /////////////////
3725
3726 // Primary template of char_traits calls std char_traits
3727 template<typename T>
3728 struct char_traits : std::char_traits<T>
3729 {};
3730
3731 // Explicitly define char traits for unsigned char since it is not standard
3732 template<>
3733 struct char_traits<unsigned char> : std::char_traits<char>
3734 {
3735 using char_type = unsigned char;
3736 using int_type = uint64_t;
3737
3738 // Redefine to_int_type function
3739 static int_type to_int_type(char_type c) noexcept
3740 {
3741 return static_cast<int_type>(c);
3742 }
3743
3744 static char_type to_char_type(int_type i) noexcept
3745 {
3746 return static_cast<char_type>(i);
3747 }
3748
3749 static constexpr int_type eof() noexcept
3750 {
3751 return static_cast<int_type>(std::char_traits<char>::eof());
3752 }
3753 };
3754
3755 // Explicitly define char traits for signed char since it is not standard
3756 template<>
3757 struct char_traits<signed char> : std::char_traits<char>
3758 {
3759 using char_type = signed char;
3760 using int_type = uint64_t;
3761
3762 // Redefine to_int_type function
3763 static int_type to_int_type(char_type c) noexcept
3764 {
3765 return static_cast<int_type>(c);
3766 }
3767
3768 static char_type to_char_type(int_type i) noexcept
3769 {
3770 return static_cast<char_type>(i);
3771 }
3772
3773 static constexpr int_type eof() noexcept
3774 {
3775 return static_cast<int_type>(std::char_traits<char>::eof());
3776 }
3777 };
3778
3779 ///////////////////
3780 // is_ functions //
3781 ///////////////////
3782
3783 // https://en.cppreference.com/w/cpp/types/conjunction
3784 template<class...> struct conjunction : std::true_type { };
3785 template<class B> struct conjunction<B> : B { };
3786 template<class B, class... Bn>
3787 struct conjunction<B, Bn...>
3788 : std::conditional<static_cast<bool>(B::value), conjunction<Bn...>, B>::type {};
3789
3790 // https://en.cppreference.com/w/cpp/types/negation
3791 template<class B> struct negation : std::integral_constant < bool, !B::value > { };
3792
3793 // Reimplementation of is_constructible and is_default_constructible, due to them being broken for
3794 // std::pair and std::tuple until LWG 2367 fix (see https://cplusplus.github.io/LWG/lwg-defects.html#2367).
3795 // This causes compile errors in e.g. clang 3.5 or gcc 4.9.
3796 template <typename T>
3797 struct is_default_constructible : std::is_default_constructible<T> {};
3798
3799 template <typename T1, typename T2>
3800 struct is_default_constructible<std::pair<T1, T2>>
3801 : conjunction<is_default_constructible<T1>, is_default_constructible<T2>> {};
3802
3803 template <typename T1, typename T2>
3804 struct is_default_constructible<const std::pair<T1, T2>>
3805 : conjunction<is_default_constructible<T1>, is_default_constructible<T2>> {};
3806
3807 template <typename... Ts>
3808 struct is_default_constructible<std::tuple<Ts...>>
3809 : conjunction<is_default_constructible<Ts>...> {};
3810
3811 template <typename... Ts>
3812 struct is_default_constructible<const std::tuple<Ts...>>
3813 : conjunction<is_default_constructible<Ts>...> {};
3814
3815 template <typename T, typename... Args>
3816 struct is_constructible : std::is_constructible<T, Args...> {};
3817
3818 template <typename T1, typename T2>
3819 struct is_constructible<std::pair<T1, T2>> : is_default_constructible<std::pair<T1, T2>> {};
3820
3821 template <typename T1, typename T2>
3822 struct is_constructible<const std::pair<T1, T2>> : is_default_constructible<const std::pair<T1, T2>> {};
3823
3824 template <typename... Ts>
3825 struct is_constructible<std::tuple<Ts...>> : is_default_constructible<std::tuple<Ts...>> {};
3826
3827 template <typename... Ts>
3828 struct is_constructible<const std::tuple<Ts...>> : is_default_constructible<const std::tuple<Ts...>> {};
3829
3830 template<typename T, typename = void>
3831 struct is_iterator_traits : std::false_type {};
3832
3833 template<typename T>
3834 struct is_iterator_traits<iterator_traits<T>>
3835 {
3836 private:
3837 using traits = iterator_traits<T>;
3838
3839 public:
3840 static constexpr auto value =
3841 is_detected<value_type_t, traits>::value &&
3842 is_detected<difference_type_t, traits>::value &&
3843 is_detected<pointer_t, traits>::value &&
3844 is_detected<iterator_category_t, traits>::value &&
3845 is_detected<reference_t, traits>::value;
3846 };
3847
3848 template<typename T>
3849 struct is_range
3850 {
3851 private:
3852 using t_ref = typename std::add_lvalue_reference<T>::type;
3853
3854 using iterator = detected_t<result_of_begin, t_ref>;
3855 using sentinel = detected_t<result_of_end, t_ref>;
3856
3857 // to be 100% correct, it should use https://en.cppreference.com/w/cpp/iterator/input_or_output_iterator
3858 // and https://en.cppreference.com/w/cpp/iterator/sentinel_for
3859 // but reimplementing these would be too much work, as a lot of other concepts are used underneath
3860 static constexpr auto is_iterator_begin =
3861 is_iterator_traits<iterator_traits<iterator>>::value;
3862
3863 public:
3864 static constexpr bool value = !std::is_same<iterator, nonesuch>::value && !std::is_same<sentinel, nonesuch>::value && is_iterator_begin;
3865 };
3866
3867 template<typename R>
3868 using iterator_t = enable_if_t<is_range<R>::value, result_of_begin<decltype(std::declval<R&>())>>;
3869
3870 template<typename T>
3871 using range_value_t = value_type_t<iterator_traits<iterator_t<T>>>;
3872
3873 // The following implementation of is_complete_type is taken from
3874 // https://blogs.msdn.microsoft.com/vcblog/2015/12/02/partial-support-for-expression-sfinae-in-vs-2015-update-1/
3875 // and is written by Xiang Fan who agreed to using it in this library.
3876
3877 template<typename T, typename = void>
3878 struct is_complete_type : std::false_type {};
3879
3880 template<typename T>
3881 struct is_complete_type<T, decltype(void(sizeof(T)))> : std::true_type {};
3882
3883 template<typename BasicJsonType, typename CompatibleObjectType,
3884 typename = void>
3885 struct is_compatible_object_type_impl : std::false_type {};
3886
3887 template<typename BasicJsonType, typename CompatibleObjectType>
3888 struct is_compatible_object_type_impl <
3889 BasicJsonType, CompatibleObjectType,
3890 enable_if_t < is_detected<mapped_type_t, CompatibleObjectType>::value&&
3891 is_detected<key_type_t, CompatibleObjectType>::value >>
3892 {
3893 using object_t = typename BasicJsonType::object_t;
3894
3895 // macOS's is_constructible does not play well with nonesuch...
3896 static constexpr bool value =
3897 is_constructible<typename object_t::key_type,
3898 typename CompatibleObjectType::key_type>::value &&
3899 is_constructible<typename object_t::mapped_type,
3900 typename CompatibleObjectType::mapped_type>::value;
3901 };
3902
3903 template<typename BasicJsonType, typename CompatibleObjectType>
3904 struct is_compatible_object_type
3905 : is_compatible_object_type_impl<BasicJsonType, CompatibleObjectType> {};
3906
3907 template<typename BasicJsonType, typename ConstructibleObjectType,
3908 typename = void>
3909 struct is_constructible_object_type_impl : std::false_type {};
3910
3911 template<typename BasicJsonType, typename ConstructibleObjectType>
3912 struct is_constructible_object_type_impl <
3913 BasicJsonType, ConstructibleObjectType,
3914 enable_if_t < is_detected<mapped_type_t, ConstructibleObjectType>::value&&
3915 is_detected<key_type_t, ConstructibleObjectType>::value >>
3916 {
3917 using object_t = typename BasicJsonType::object_t;
3918
3919 static constexpr bool value =
3920 (is_default_constructible<ConstructibleObjectType>::value &&
3921 (std::is_move_assignable<ConstructibleObjectType>::value ||
3922 std::is_copy_assignable<ConstructibleObjectType>::value) &&
3923 (is_constructible<typename ConstructibleObjectType::key_type,
3924 typename object_t::key_type>::value &&
3925 std::is_same <
3926 typename object_t::mapped_type,
3927 typename ConstructibleObjectType::mapped_type >::value)) ||
3928 (has_from_json<BasicJsonType,
3929 typename ConstructibleObjectType::mapped_type>::value ||
3930 has_non_default_from_json <
3931 BasicJsonType,
3932 typename ConstructibleObjectType::mapped_type >::value);
3933 };
3934
3935 template<typename BasicJsonType, typename ConstructibleObjectType>
3936 struct is_constructible_object_type
3937 : is_constructible_object_type_impl<BasicJsonType,
3938 ConstructibleObjectType> {};
3939
3940 template<typename BasicJsonType, typename CompatibleStringType>
3941 struct is_compatible_string_type
3942 {
3943 static constexpr auto value =
3944 is_constructible<typename BasicJsonType::string_t, CompatibleStringType>::value;
3945 };
3946
3947 template<typename BasicJsonType, typename ConstructibleStringType>
3948 struct is_constructible_string_type
3949 {
3950 // launder type through decltype() to fix compilation failure on ICPC
3951 #ifdef __INTEL_COMPILER
3952 using laundered_type = decltype(std::declval<ConstructibleStringType>());
3953 #else
3954 using laundered_type = ConstructibleStringType;
3955 #endif
3956
3957 static constexpr auto value =
3958 conjunction <
3959 is_constructible<laundered_type, typename BasicJsonType::string_t>,
3960 is_detected_exact<typename BasicJsonType::string_t::value_type,
3961 value_type_t, laundered_type >>::value;
3962 };
3963
3964 template<typename BasicJsonType, typename CompatibleArrayType, typename = void>
3965 struct is_compatible_array_type_impl : std::false_type {};
3966
3967 template<typename BasicJsonType, typename CompatibleArrayType>
3968 struct is_compatible_array_type_impl <
3969 BasicJsonType, CompatibleArrayType,
3970 enable_if_t <
3971 is_detected<iterator_t, CompatibleArrayType>::value&&
3972 is_iterator_traits<iterator_traits<detected_t<iterator_t, CompatibleArrayType>>>::value&&
3973 // special case for types like std::filesystem::path whose iterator's value_type are themselves
3974 // c.f. https://github.com/nlohmann/json/pull/3073
3975 !std::is_same<CompatibleArrayType, detected_t<range_value_t, CompatibleArrayType>>::value >>
3976 {
3977 static constexpr bool value =
3978 is_constructible<BasicJsonType,
3979 range_value_t<CompatibleArrayType>>::value;
3980 };
3981
3982 template<typename BasicJsonType, typename CompatibleArrayType>
3983 struct is_compatible_array_type
3984 : is_compatible_array_type_impl<BasicJsonType, CompatibleArrayType> {};
3985
3986 template<typename BasicJsonType, typename ConstructibleArrayType, typename = void>
3987 struct is_constructible_array_type_impl : std::false_type {};
3988
3989 template<typename BasicJsonType, typename ConstructibleArrayType>
3990 struct is_constructible_array_type_impl <
3991 BasicJsonType, ConstructibleArrayType,
3992 enable_if_t<std::is_same<ConstructibleArrayType,
3993 typename BasicJsonType::value_type>::value >>
3994 : std::true_type {};
3995
3996 template<typename BasicJsonType, typename ConstructibleArrayType>
3997 struct is_constructible_array_type_impl <
3998 BasicJsonType, ConstructibleArrayType,
3999 enable_if_t < !std::is_same<ConstructibleArrayType,
4000 typename BasicJsonType::value_type>::value&&
4001 !is_compatible_string_type<BasicJsonType, ConstructibleArrayType>::value&&
4002 is_default_constructible<ConstructibleArrayType>::value&&
4003 (std::is_move_assignable<ConstructibleArrayType>::value ||
4004 std::is_copy_assignable<ConstructibleArrayType>::value)&&
4005 is_detected<iterator_t, ConstructibleArrayType>::value&&
4006 is_iterator_traits<iterator_traits<detected_t<iterator_t, ConstructibleArrayType>>>::value&&
4007 is_detected<range_value_t, ConstructibleArrayType>::value&&
4008 // special case for types like std::filesystem::path whose iterator's value_type are themselves
4009 // c.f. https://github.com/nlohmann/json/pull/3073
4010 !std::is_same<ConstructibleArrayType, detected_t<range_value_t, ConstructibleArrayType>>::value&&
4011 is_complete_type <
4012 detected_t<range_value_t, ConstructibleArrayType >>::value >>
4013 {
4014 using value_type = range_value_t<ConstructibleArrayType>;
4015
4016 static constexpr bool value =
4017 std::is_same<value_type,
4018 typename BasicJsonType::array_t::value_type>::value ||
4019 has_from_json<BasicJsonType,
4020 value_type>::value ||
4021 has_non_default_from_json <
4022 BasicJsonType,
4023 value_type >::value;
4024 };
4025
4026 template<typename BasicJsonType, typename ConstructibleArrayType>
4027 struct is_constructible_array_type
4028 : is_constructible_array_type_impl<BasicJsonType, ConstructibleArrayType> {};
4029
4030 template<typename RealIntegerType, typename CompatibleNumberIntegerType,
4031 typename = void>
4032 struct is_compatible_integer_type_impl : std::false_type {};
4033
4034 template<typename RealIntegerType, typename CompatibleNumberIntegerType>
4035 struct is_compatible_integer_type_impl <
4036 RealIntegerType, CompatibleNumberIntegerType,
4037 enable_if_t < std::is_integral<RealIntegerType>::value&&
4038 std::is_integral<CompatibleNumberIntegerType>::value&&
4039 !std::is_same<bool, CompatibleNumberIntegerType>::value >>
4040 {
4041 // is there an assert somewhere on overflows?
4042 using RealLimits = std::numeric_limits<RealIntegerType>;
4043 using CompatibleLimits = std::numeric_limits<CompatibleNumberIntegerType>;
4044
4045 static constexpr auto value =
4046 is_constructible<RealIntegerType,
4047 CompatibleNumberIntegerType>::value &&
4048 CompatibleLimits::is_integer &&
4049 RealLimits::is_signed == CompatibleLimits::is_signed;
4050 };
4051
4052 template<typename RealIntegerType, typename CompatibleNumberIntegerType>
4053 struct is_compatible_integer_type
4054 : is_compatible_integer_type_impl<RealIntegerType,
4055 CompatibleNumberIntegerType> {};
4056
4057 template<typename BasicJsonType, typename CompatibleType, typename = void>
4058 struct is_compatible_type_impl: std::false_type {};
4059
4060 template<typename BasicJsonType, typename CompatibleType>
4061 struct is_compatible_type_impl <
4062 BasicJsonType, CompatibleType,
4063 enable_if_t<is_complete_type<CompatibleType>::value >>
4064 {
4065 static constexpr bool value =
4066 has_to_json<BasicJsonType, CompatibleType>::value;
4067 };
4068
4069 template<typename BasicJsonType, typename CompatibleType>
4070 struct is_compatible_type
4071 : is_compatible_type_impl<BasicJsonType, CompatibleType> {};
4072
4073 template<typename T1, typename T2>
4074 struct is_constructible_tuple : std::false_type {};
4075
4076 template<typename T1, typename... Args>
4077 struct is_constructible_tuple<T1, std::tuple<Args...>> : conjunction<is_constructible<T1, Args>...> {};
4078
4079 template<typename BasicJsonType, typename T>
4080 struct is_json_iterator_of : std::false_type {};
4081
4082 template<typename BasicJsonType>
4083 struct is_json_iterator_of<BasicJsonType, typename BasicJsonType::iterator> : std::true_type {};
4084
4085 template<typename BasicJsonType>
4086 struct is_json_iterator_of<BasicJsonType, typename BasicJsonType::const_iterator> : std::true_type
4087 {};
4088
4089 // checks if a given type T is a template specialization of Primary
4090 template<template <typename...> class Primary, typename T>
4091 struct is_specialization_of : std::false_type {};
4092
4093 template<template <typename...> class Primary, typename... Args>
4094 struct is_specialization_of<Primary, Primary<Args...>> : std::true_type {};
4095
4096 template<typename T>
4097 using is_json_pointer = is_specialization_of<::nlohmann::json_pointer, uncvref_t<T>>;
4098
4099 // checks if A and B are comparable using Compare functor
4100 template<typename Compare, typename A, typename B, typename = void>
4101 struct is_comparable : std::false_type {};
4102
4103 template<typename Compare, typename A, typename B>
4104 struct is_comparable<Compare, A, B, void_t<
4105 decltype(std::declval<Compare>()(std::declval<A>(), std::declval<B>())),
4106 decltype(std::declval<Compare>()(std::declval<B>(), std::declval<A>()))
4107 >> : std::true_type {};
4108
4109 template<typename T>
4110 using detect_is_transparent = typename T::is_transparent;
4111
4112 // type trait to check if KeyType can be used as object key (without a BasicJsonType)
4113 // see is_usable_as_basic_json_key_type below
4114 template<typename Comparator, typename ObjectKeyType, typename KeyTypeCVRef, bool RequireTransparentComparator = true,
4115 bool ExcludeObjectKeyType = RequireTransparentComparator, typename KeyType = uncvref_t<KeyTypeCVRef>>
4116 using is_usable_as_key_type = typename std::conditional <
4117 is_comparable<Comparator, ObjectKeyType, KeyTypeCVRef>::value
4118 && !(ExcludeObjectKeyType && std::is_same<KeyType,
4119 ObjectKeyType>::value)
4120 && (!RequireTransparentComparator
4121 || is_detected <detect_is_transparent, Comparator>::value)
4122 && !is_json_pointer<KeyType>::value,
4123 std::true_type,
4124 std::false_type >::type;
4125
4126 // type trait to check if KeyType can be used as object key
4127 // true if:
4128 // - KeyType is comparable with BasicJsonType::object_t::key_type
4129 // - if ExcludeObjectKeyType is true, KeyType is not BasicJsonType::object_t::key_type
4130 // - the comparator is transparent or RequireTransparentComparator is false
4131 // - KeyType is not a JSON iterator or json_pointer
4132 template<typename BasicJsonType, typename KeyTypeCVRef, bool RequireTransparentComparator = true,
4133 bool ExcludeObjectKeyType = RequireTransparentComparator, typename KeyType = uncvref_t<KeyTypeCVRef>>
4134 using is_usable_as_basic_json_key_type = typename std::conditional <
4135 is_usable_as_key_type<typename BasicJsonType::object_comparator_t,
4136 typename BasicJsonType::object_t::key_type, KeyTypeCVRef,
4137 RequireTransparentComparator, ExcludeObjectKeyType>::value
4138 && !is_json_iterator_of<BasicJsonType, KeyType>::value,
4139 std::true_type,
4140 std::false_type >::type;
4141
4142 template<typename ObjectType, typename KeyType>
4143 using detect_erase_with_key_type = decltype(std::declval<ObjectType&>().erase(std::declval<KeyType>()));
4144
4145 // type trait to check if object_t has an erase() member functions accepting KeyType
4146 template<typename BasicJsonType, typename KeyType>
4147 using has_erase_with_key_type = typename std::conditional <
4148 is_detected <
4149 detect_erase_with_key_type,
4150 typename BasicJsonType::object_t, KeyType >::value,
4151 std::true_type,
4152 std::false_type >::type;
4153
4154 // a naive helper to check if a type is an ordered_map (exploits the fact that
4155 // ordered_map inherits capacity() from std::vector)
4156 template <typename T>
4157 struct is_ordered_map
4158 {
4159 using one = char;
4160
4161 struct two
4162 {
4163 char x[2]; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
4164 };
4165
4166 template <typename C> static one test( decltype(&C::capacity) ) ;
4167 template <typename C> static two test(...);
4168
4169 enum { value = sizeof(test<T>(nullptr)) == sizeof(char) }; // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
4170 };
4171
4172 // to avoid useless casts (see https://github.com/nlohmann/json/issues/2893#issuecomment-889152324)
4173 template < typename T, typename U, enable_if_t < !std::is_same<T, U>::value, int > = 0 >
4174 T conditional_static_cast(U value)
4175 {
4176 return static_cast<T>(value);
4177 }
4178
4179 template<typename T, typename U, enable_if_t<std::is_same<T, U>::value, int> = 0>
4180 T conditional_static_cast(U value)
4181 {
4182 return value;
4183 }
4184
4185 template<typename... Types>
4186 using all_integral = conjunction<std::is_integral<Types>...>;
4187
4188 template<typename... Types>
4189 using all_signed = conjunction<std::is_signed<Types>...>;
4190
4191 template<typename... Types>
4192 using all_unsigned = conjunction<std::is_unsigned<Types>...>;
4193
4194 // there's a disjunction trait in another PR; replace when merged
4195 template<typename... Types>
4196 using same_sign = std::integral_constant < bool,
4197 all_signed<Types...>::value || all_unsigned<Types...>::value >;
4198
4199 template<typename OfType, typename T>
4200 using never_out_of_range = std::integral_constant < bool,
4201 (std::is_signed<OfType>::value && (sizeof(T) < sizeof(OfType)))
4202 || (same_sign<OfType, T>::value && sizeof(OfType) == sizeof(T)) >;
4203
4204 template<typename OfType, typename T,
4205 bool OfTypeSigned = std::is_signed<OfType>::value,
4206 bool TSigned = std::is_signed<T>::value>
4207 struct value_in_range_of_impl2;
4208
4209 template<typename OfType, typename T>
4210 struct value_in_range_of_impl2<OfType, T, false, false>
4211 {
4212 static constexpr bool test(T val)
4213 {
4214 using CommonType = typename std::common_type<OfType, T>::type;
4215 return static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
4216 }
4217 };
4218
4219 template<typename OfType, typename T>
4220 struct value_in_range_of_impl2<OfType, T, true, false>
4221 {
4222 static constexpr bool test(T val)
4223 {
4224 using CommonType = typename std::common_type<OfType, T>::type;
4225 return static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
4226 }
4227 };
4228
4229 template<typename OfType, typename T>
4230 struct value_in_range_of_impl2<OfType, T, false, true>
4231 {
4232 static constexpr bool test(T val)
4233 {
4234 using CommonType = typename std::common_type<OfType, T>::type;
4235 return val >= 0 && static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
4236 }
4237 };
4238
4239 template<typename OfType, typename T>
4240 struct value_in_range_of_impl2<OfType, T, true, true>
4241 {
4242 static constexpr bool test(T val)
4243 {
4244 using CommonType = typename std::common_type<OfType, T>::type;
4245 return static_cast<CommonType>(val) >= static_cast<CommonType>((std::numeric_limits<OfType>::min)())
4246 && static_cast<CommonType>(val) <= static_cast<CommonType>((std::numeric_limits<OfType>::max)());
4247 }
4248 };
4249
4250 template<typename OfType, typename T,
4251 bool NeverOutOfRange = never_out_of_range<OfType, T>::value,
4252 typename = detail::enable_if_t<all_integral<OfType, T>::value>>
4253 struct value_in_range_of_impl1;
4254
4255 template<typename OfType, typename T>
4256 struct value_in_range_of_impl1<OfType, T, false>
4257 {
4258 static constexpr bool test(T val)
4259 {
4260 return value_in_range_of_impl2<OfType, T>::test(val);
4261 }
4262 };
4263
4264 template<typename OfType, typename T>
4265 struct value_in_range_of_impl1<OfType, T, true>
4266 {
4267 static constexpr bool test(T /*val*/)
4268 {
4269 return true;
4270 }
4271 };
4272
4273 template<typename OfType, typename T>
4274 constexpr bool value_in_range_of(T val)
4275 {
4276 return value_in_range_of_impl1<OfType, T>::test(val);
4277 }
4278
4279 template<bool Value>
4280 using bool_constant = std::integral_constant<bool, Value>;
4281
4282 ///////////////////////////////////////////////////////////////////////////////
4283 // is_c_string
4284 ///////////////////////////////////////////////////////////////////////////////
4285
4286 namespace impl
4287 {
4288
4289 template<typename T>
4290 constexpr bool is_c_string()
4291 {
4292 using TUnExt = typename std::remove_extent<T>::type;
4293 using TUnCVExt = typename std::remove_cv<TUnExt>::type;
4294 using TUnPtr = typename std::remove_pointer<T>::type;
4295 using TUnCVPtr = typename std::remove_cv<TUnPtr>::type;
4296 return
4297 (std::is_array<T>::value && std::is_same<TUnCVExt, char>::value)
4298 || (std::is_pointer<T>::value && std::is_same<TUnCVPtr, char>::value);
4299 }
4300
4301 } // namespace impl
4302
4303 // checks whether T is a [cv] char */[cv] char[] C string
4304 template<typename T>
4305 struct is_c_string : bool_constant<impl::is_c_string<T>()> {};
4306
4307 template<typename T>
4308 using is_c_string_uncvref = is_c_string<uncvref_t<T>>;
4309
4310 ///////////////////////////////////////////////////////////////////////////////
4311 // is_transparent
4312 ///////////////////////////////////////////////////////////////////////////////
4313
4314 namespace impl
4315 {
4316
4317 template<typename T>
4318 constexpr bool is_transparent()
4319 {
4320 return is_detected<detect_is_transparent, T>::value;
4321 }
4322
4323 } // namespace impl
4324
4325 // checks whether T has a member named is_transparent
4326 template<typename T>
4327 struct is_transparent : bool_constant<impl::is_transparent<T>()> {};
4328
4329 ///////////////////////////////////////////////////////////////////////////////
4330
4331 } // namespace detail
4332 NLOHMANN_JSON_NAMESPACE_END
4333
4334 // #include <nlohmann/detail/string_concat.hpp>
4335 // __ _____ _____ _____
4336 // __| | __| | | | JSON for Modern C++
4337 // | | |__ | | | | | | version 3.12.0
4338 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
4339 //
4340 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
4341 // SPDX-License-Identifier: MIT
4342
4343
4344
4345 #include <cstring> // strlen
4346 #include <string> // string
4347 #include <utility> // forward
4348
4349 // #include <nlohmann/detail/meta/cpp_future.hpp>
4350
4351 // #include <nlohmann/detail/meta/detected.hpp>
4352
4353
4354 NLOHMANN_JSON_NAMESPACE_BEGIN
4355 namespace detail
4356 {
4357
4358 inline std::size_t concat_length()
4359 {
4360 return 0;
4361 }
4362
4363 template<typename... Args>
4364 inline std::size_t concat_length(const char* cstr, const Args& ... rest);
4365
4366 template<typename StringType, typename... Args>
4367 inline std::size_t concat_length(const StringType& str, const Args& ... rest);
4368
4369 template<typename... Args>
4370 inline std::size_t concat_length(const char /*c*/, const Args& ... rest)
4371 {
4372 return 1 + concat_length(rest...);
4373 }
4374
4375 template<typename... Args>
4376 inline std::size_t concat_length(const char* cstr, const Args& ... rest)
4377 {
4378 // cppcheck-suppress ignoredReturnValue
4379 return ::strlen(cstr) + concat_length(rest...);
4380 }
4381
4382 template<typename StringType, typename... Args>
4383 inline std::size_t concat_length(const StringType& str, const Args& ... rest)
4384 {
4385 return str.size() + concat_length(rest...);
4386 }
4387
4388 template<typename OutStringType>
4389 inline void concat_into(OutStringType& /*out*/)
4390 {}
4391
4392 template<typename StringType, typename Arg>
4393 using string_can_append = decltype(std::declval<StringType&>().append(std::declval < Arg && > ()));
4394
4395 template<typename StringType, typename Arg>
4396 using detect_string_can_append = is_detected<string_can_append, StringType, Arg>;
4397
4398 template<typename StringType, typename Arg>
4399 using string_can_append_op = decltype(std::declval<StringType&>() += std::declval < Arg && > ());
4400
4401 template<typename StringType, typename Arg>
4402 using detect_string_can_append_op = is_detected<string_can_append_op, StringType, Arg>;
4403
4404 template<typename StringType, typename Arg>
4405 using string_can_append_iter = decltype(std::declval<StringType&>().append(std::declval<const Arg&>().begin(), std::declval<const Arg&>().end()));
4406
4407 template<typename StringType, typename Arg>
4408 using detect_string_can_append_iter = is_detected<string_can_append_iter, StringType, Arg>;
4409
4410 template<typename StringType, typename Arg>
4411 using string_can_append_data = decltype(std::declval<StringType&>().append(std::declval<const Arg&>().data(), std::declval<const Arg&>().size()));
4412
4413 template<typename StringType, typename Arg>
4414 using detect_string_can_append_data = is_detected<string_can_append_data, StringType, Arg>;
4415
4416 template < typename OutStringType, typename Arg, typename... Args,
4417 enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
4418 && detect_string_can_append_op<OutStringType, Arg>::value, int > = 0 >
4419 inline void concat_into(OutStringType& out, Arg && arg, Args && ... rest);
4420
4421 template < typename OutStringType, typename Arg, typename... Args,
4422 enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
4423 && !detect_string_can_append_op<OutStringType, Arg>::value
4424 && detect_string_can_append_iter<OutStringType, Arg>::value, int > = 0 >
4425 inline void concat_into(OutStringType& out, const Arg& arg, Args && ... rest);
4426
4427 template < typename OutStringType, typename Arg, typename... Args,
4428 enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
4429 && !detect_string_can_append_op<OutStringType, Arg>::value
4430 && !detect_string_can_append_iter<OutStringType, Arg>::value
4431 && detect_string_can_append_data<OutStringType, Arg>::value, int > = 0 >
4432 inline void concat_into(OutStringType& out, const Arg& arg, Args && ... rest);
4433
4434 template<typename OutStringType, typename Arg, typename... Args,
4435 enable_if_t<detect_string_can_append<OutStringType, Arg>::value, int> = 0>
4436 inline void concat_into(OutStringType& out, Arg && arg, Args && ... rest)
4437 {
4438 out.append(std::forward<Arg>(arg));
4439 concat_into(out, std::forward<Args>(rest)...);
4440 }
4441
4442 template < typename OutStringType, typename Arg, typename... Args,
4443 enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
4444 && detect_string_can_append_op<OutStringType, Arg>::value, int > >
4445 inline void concat_into(OutStringType& out, Arg&& arg, Args&& ... rest)
4446 {
4447 out += std::forward<Arg>(arg);
4448 concat_into(out, std::forward<Args>(rest)...);
4449 }
4450
4451 template < typename OutStringType, typename Arg, typename... Args,
4452 enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
4453 && !detect_string_can_append_op<OutStringType, Arg>::value
4454 && detect_string_can_append_iter<OutStringType, Arg>::value, int > >
4455 inline void concat_into(OutStringType& out, const Arg& arg, Args&& ... rest)
4456 {
4457 out.append(arg.begin(), arg.end());
4458 concat_into(out, std::forward<Args>(rest)...);
4459 }
4460
4461 template < typename OutStringType, typename Arg, typename... Args,
4462 enable_if_t < !detect_string_can_append<OutStringType, Arg>::value
4463 && !detect_string_can_append_op<OutStringType, Arg>::value
4464 && !detect_string_can_append_iter<OutStringType, Arg>::value
4465 && detect_string_can_append_data<OutStringType, Arg>::value, int > >
4466 inline void concat_into(OutStringType& out, const Arg& arg, Args&& ... rest)
4467 {
4468 out.append(arg.data(), arg.size());
4469 concat_into(out, std::forward<Args>(rest)...);
4470 }
4471
4472 template<typename OutStringType = std::string, typename... Args>
4473 inline OutStringType concat(Args && ... args)
4474 {
4475 OutStringType str;
4476 str.reserve(concat_length(args...));
4477 concat_into(str, std::forward<Args>(args)...);
4478 return str;
4479 }
4480
4481 } // namespace detail
4482 NLOHMANN_JSON_NAMESPACE_END
4483
4484
4485 // With -Wweak-vtables, Clang will complain about the exception classes as they
4486 // have no out-of-line virtual method definitions and their vtable will be
4487 // emitted in every translation unit. This issue cannot be fixed with a
4488 // header-only library as there is no implementation file to move these
4489 // functions to. As a result, we suppress this warning here to avoid client
4490 // code to stumble over this. See https://github.com/nlohmann/json/issues/4087
4491 // for a discussion.
4492 #if defined(__clang__)
4493 #pragma clang diagnostic push
4494 #pragma clang diagnostic ignored "-Wweak-vtables"
4495 #endif
4496
4497 NLOHMANN_JSON_NAMESPACE_BEGIN
4498 namespace detail
4499 {
4500
4501 ////////////////
4502 // exceptions //
4503 ////////////////
4504
4505 /// @brief general exception of the @ref basic_json class
4506 /// @sa https://json.nlohmann.me/api/basic_json/exception/
4507 class exception : public std::exception
4508 {
4509 public:
4510 /// returns the explanatory string
4511 const char* what() const noexcept override
4512 {
4513 return m.what();
4514 }
4515
4516 /// the id of the exception
4517 const int id; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes)
4518
4519 protected:
4520 JSON_HEDLEY_NON_NULL(3)
4521 exception(int id_, const char* what_arg) : id(id_), m(what_arg) {} // NOLINT(bugprone-throw-keyword-missing)
4522
4523 static std::string name(const std::string& ename, int id_)
4524 {
4525 return concat("[json.exception.", ename, '.', std::to_string(id_), "] ");
4526 }
4527
4528 static std::string diagnostics(std::nullptr_t /*leaf_element*/)
4529 {
4530 return "";
4531 }
4532
4533 template<typename BasicJsonType>
4534 static std::string diagnostics(const BasicJsonType* leaf_element)
4535 {
4536 #if JSON_DIAGNOSTICS
4537 std::vector<std::string> tokens;
4538 for (const auto* current = leaf_element; current != nullptr && current->m_parent != nullptr; current = current->m_parent)
4539 {
4540 switch (current->m_parent->type())
4541 {
4542 case value_t::array:
4543 {
4544 for (std::size_t i = 0; i < current->m_parent->m_data.m_value.array->size(); ++i)
4545 {
4546 if (&current->m_parent->m_data.m_value.array->operator[](i) == current)
4547 {
4548 tokens.emplace_back(std::to_string(i));
4549 break;
4550 }
4551 }
4552 break;
4553 }
4554
4555 case value_t::object:
4556 {
4557 for (const auto& element : *current->m_parent->m_data.m_value.object)
4558 {
4559 if (&element.second == current)
4560 {
4561 tokens.emplace_back(element.first.c_str());
4562 break;
4563 }
4564 }
4565 break;
4566 }
4567
4568 case value_t::null: // LCOV_EXCL_LINE
4569 case value_t::string: // LCOV_EXCL_LINE
4570 case value_t::boolean: // LCOV_EXCL_LINE
4571 case value_t::number_integer: // LCOV_EXCL_LINE
4572 case value_t::number_unsigned: // LCOV_EXCL_LINE
4573 case value_t::number_float: // LCOV_EXCL_LINE
4574 case value_t::binary: // LCOV_EXCL_LINE
4575 case value_t::discarded: // LCOV_EXCL_LINE
4576 default: // LCOV_EXCL_LINE
4577 break; // LCOV_EXCL_LINE
4578 }
4579 }
4580
4581 if (tokens.empty())
4582 {
4583 return "";
4584 }
4585
4586 auto str = std::accumulate(tokens.rbegin(), tokens.rend(), std::string{},
4587 [](const std::string & a, const std::string & b)
4588 {
4589 return concat(a, '/', detail::escape(b));
4590 });
4591
4592 return concat('(', str, ") ", get_byte_positions(leaf_element));
4593 #else
4594 return get_byte_positions(leaf_element);
4595 #endif
4596 }
4597
4598 private:
4599 /// an exception object as storage for error messages
4600 std::runtime_error m;
4601 #if JSON_DIAGNOSTIC_POSITIONS
4602 template<typename BasicJsonType>
4603 static std::string get_byte_positions(const BasicJsonType* leaf_element)
4604 {
4605 if ((leaf_element->start_pos() != std::string::npos) && (leaf_element->end_pos() != std::string::npos))
4606 {
4607 return concat("(bytes ", std::to_string(leaf_element->start_pos()), "-", std::to_string(leaf_element->end_pos()), ") ");
4608 }
4609 return "";
4610 }
4611 #else
4612 template<typename BasicJsonType>
4613 static std::string get_byte_positions(const BasicJsonType* leaf_element)
4614 {
4615 static_cast<void>(leaf_element);
4616 return "";
4617 }
4618 #endif
4619 };
4620
4621 /// @brief exception indicating a parse error
4622 /// @sa https://json.nlohmann.me/api/basic_json/parse_error/
4623 class parse_error : public exception
4624 {
4625 public:
4626 /*!
4627 @brief create a parse error exception
4628 @param[in] id_ the id of the exception
4629 @param[in] pos the position where the error occurred (or with
4630 chars_read_total=0 if the position cannot be
4631 determined)
4632 @param[in] what_arg the explanatory string
4633 @return parse_error object
4634 */
4635 template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
4636 static parse_error create(int id_, const position_t& pos, const std::string& what_arg, BasicJsonContext context)
4637 {
4638 const std::string w = concat(exception::name("parse_error", id_), "parse error",
4639 position_string(pos), ": ", exception::diagnostics(context), what_arg);
4640 return {id_, pos.chars_read_total, w.c_str()};
4641 }
4642
4643 template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
4644 static parse_error create(int id_, std::size_t byte_, const std::string& what_arg, BasicJsonContext context)
4645 {
4646 const std::string w = concat(exception::name("parse_error", id_), "parse error",
4647 (byte_ != 0 ? (concat(" at byte ", std::to_string(byte_))) : ""),
4648 ": ", exception::diagnostics(context), what_arg);
4649 return {id_, byte_, w.c_str()};
4650 }
4651
4652 /*!
4653 @brief byte index of the parse error
4654
4655 The byte index of the last read character in the input file.
4656
4657 @note For an input with n bytes, 1 is the index of the first character and
4658 n+1 is the index of the terminating null byte or the end of file.
4659 This also holds true when reading a byte vector (CBOR or MessagePack).
4660 */
4661 const std::size_t byte;
4662
4663 private:
4664 parse_error(int id_, std::size_t byte_, const char* what_arg)
4665 : exception(id_, what_arg), byte(byte_) {}
4666
4667 static std::string position_string(const position_t& pos)
4668 {
4669 return concat(" at line ", std::to_string(pos.lines_read + 1),
4670 ", column ", std::to_string(pos.chars_read_current_line));
4671 }
4672 };
4673
4674 /// @brief exception indicating errors with iterators
4675 /// @sa https://json.nlohmann.me/api/basic_json/invalid_iterator/
4676 class invalid_iterator : public exception
4677 {
4678 public:
4679 template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
4680 static invalid_iterator create(int id_, const std::string& what_arg, BasicJsonContext context)
4681 {
4682 const std::string w = concat(exception::name("invalid_iterator", id_), exception::diagnostics(context), what_arg);
4683 return {id_, w.c_str()};
4684 }
4685
4686 private:
4687 JSON_HEDLEY_NON_NULL(3)
4688 invalid_iterator(int id_, const char* what_arg)
4689 : exception(id_, what_arg) {}
4690 };
4691
4692 /// @brief exception indicating executing a member function with a wrong type
4693 /// @sa https://json.nlohmann.me/api/basic_json/type_error/
4694 class type_error : public exception
4695 {
4696 public:
4697 template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
4698 static type_error create(int id_, const std::string& what_arg, BasicJsonContext context)
4699 {
4700 const std::string w = concat(exception::name("type_error", id_), exception::diagnostics(context), what_arg);
4701 return {id_, w.c_str()};
4702 }
4703
4704 private:
4705 JSON_HEDLEY_NON_NULL(3)
4706 type_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
4707 };
4708
4709 /// @brief exception indicating access out of the defined range
4710 /// @sa https://json.nlohmann.me/api/basic_json/out_of_range/
4711 class out_of_range : public exception
4712 {
4713 public:
4714 template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
4715 static out_of_range create(int id_, const std::string& what_arg, BasicJsonContext context)
4716 {
4717 const std::string w = concat(exception::name("out_of_range", id_), exception::diagnostics(context), what_arg);
4718 return {id_, w.c_str()};
4719 }
4720
4721 private:
4722 JSON_HEDLEY_NON_NULL(3)
4723 out_of_range(int id_, const char* what_arg) : exception(id_, what_arg) {}
4724 };
4725
4726 /// @brief exception indicating other library errors
4727 /// @sa https://json.nlohmann.me/api/basic_json/other_error/
4728 class other_error : public exception
4729 {
4730 public:
4731 template<typename BasicJsonContext, enable_if_t<is_basic_json_context<BasicJsonContext>::value, int> = 0>
4732 static other_error create(int id_, const std::string& what_arg, BasicJsonContext context)
4733 {
4734 const std::string w = concat(exception::name("other_error", id_), exception::diagnostics(context), what_arg);
4735 return {id_, w.c_str()};
4736 }
4737
4738 private:
4739 JSON_HEDLEY_NON_NULL(3)
4740 other_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
4741 };
4742
4743 } // namespace detail
4744 NLOHMANN_JSON_NAMESPACE_END
4745
4746 #if defined(__clang__)
4747 #pragma clang diagnostic pop
4748 #endif
4749
4750 // #include <nlohmann/detail/macro_scope.hpp>
4751
4752 // #include <nlohmann/detail/meta/cpp_future.hpp>
4753
4754 // #include <nlohmann/detail/meta/identity_tag.hpp>
4755 // __ _____ _____ _____
4756 // __| | __| | | | JSON for Modern C++
4757 // | | |__ | | | | | | version 3.12.0
4758 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
4759 //
4760 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
4761 // SPDX-License-Identifier: MIT
4762
4763
4764
4765 // #include <nlohmann/detail/abi_macros.hpp>
4766
4767
4768 NLOHMANN_JSON_NAMESPACE_BEGIN
4769 namespace detail
4770 {
4771
4772 // dispatching helper struct
4773 template <class T> struct identity_tag {};
4774
4775 } // namespace detail
4776 NLOHMANN_JSON_NAMESPACE_END
4777
4778 // #include <nlohmann/detail/meta/std_fs.hpp>
4779 // __ _____ _____ _____
4780 // __| | __| | | | JSON for Modern C++
4781 // | | |__ | | | | | | version 3.12.0
4782 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
4783 //
4784 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
4785 // SPDX-License-Identifier: MIT
4786
4787
4788
4789 // #include <nlohmann/detail/macro_scope.hpp>
4790
4791
4792 #if JSON_HAS_EXPERIMENTAL_FILESYSTEM
4793 #include <experimental/filesystem>
4794 NLOHMANN_JSON_NAMESPACE_BEGIN
4795 namespace detail
4796 {
4797 namespace std_fs = std::experimental::filesystem;
4798 } // namespace detail
4799 NLOHMANN_JSON_NAMESPACE_END
4800 #elif JSON_HAS_FILESYSTEM
4801 #include <filesystem> // NOLINT(build/c++17)
4802 NLOHMANN_JSON_NAMESPACE_BEGIN
4803 namespace detail
4804 {
4805 namespace std_fs = std::filesystem;
4806 } // namespace detail
4807 NLOHMANN_JSON_NAMESPACE_END
4808 #endif
4809
4810 // #include <nlohmann/detail/meta/type_traits.hpp>
4811
4812 // #include <nlohmann/detail/string_concat.hpp>
4813
4814 // #include <nlohmann/detail/value_t.hpp>
4815
4816
4817 // include after macro_scope.hpp
4818 #ifdef JSON_HAS_CPP_17
4819 #include <optional> // optional
4820 #endif
4821
4822 NLOHMANN_JSON_NAMESPACE_BEGIN
4823 namespace detail
4824 {
4825
4826 template<typename BasicJsonType>
4827 inline void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
4828 {
4829 if (JSON_HEDLEY_UNLIKELY(!j.is_null()))
4830 {
4831 JSON_THROW(type_error::create(302, concat("type must be null, but is ", j.type_name()), &j));
4832 }
4833 n = nullptr;
4834 }
4835
4836 #ifdef JSON_HAS_CPP_17
4837 template<typename BasicJsonType, typename T>
4838 void from_json(const BasicJsonType& j, std::optional<T>& opt)
4839 {
4840 if (j.is_null())
4841 {
4842 opt = std::nullopt;
4843 }
4844 else
4845 {
4846 opt.emplace(j.template get<T>());
4847 }
4848 }
4849 #endif // JSON_HAS_CPP_17
4850
4851 // overloads for basic_json template parameters
4852 template < typename BasicJsonType, typename ArithmeticType,
4853 enable_if_t < std::is_arithmetic<ArithmeticType>::value&&
4854 !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
4855 int > = 0 >
4856 void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
4857 {
4858 switch (static_cast<value_t>(j))
4859 {
4860 case value_t::number_unsigned:
4861 {
4862 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
4863 break;
4864 }
4865 case value_t::number_integer:
4866 {
4867 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
4868 break;
4869 }
4870 case value_t::number_float:
4871 {
4872 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
4873 break;
4874 }
4875
4876 case value_t::null:
4877 case value_t::object:
4878 case value_t::array:
4879 case value_t::string:
4880 case value_t::boolean:
4881 case value_t::binary:
4882 case value_t::discarded:
4883 default:
4884 JSON_THROW(type_error::create(302, concat("type must be number, but is ", j.type_name()), &j));
4885 }
4886 }
4887
4888 template<typename BasicJsonType>
4889 inline void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
4890 {
4891 if (JSON_HEDLEY_UNLIKELY(!j.is_boolean()))
4892 {
4893 JSON_THROW(type_error::create(302, concat("type must be boolean, but is ", j.type_name()), &j));
4894 }
4895 b = *j.template get_ptr<const typename BasicJsonType::boolean_t*>();
4896 }
4897
4898 template<typename BasicJsonType>
4899 inline void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
4900 {
4901 if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
4902 {
4903 JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
4904 }
4905 s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
4906 }
4907
4908 template <
4909 typename BasicJsonType, typename StringType,
4910 enable_if_t <
4911 std::is_assignable<StringType&, const typename BasicJsonType::string_t>::value
4912 && is_detected_exact<typename BasicJsonType::string_t::value_type, value_type_t, StringType>::value
4913 && !std::is_same<typename BasicJsonType::string_t, StringType>::value
4914 && !is_json_ref<StringType>::value, int > = 0 >
4915 inline void from_json(const BasicJsonType& j, StringType& s)
4916 {
4917 if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
4918 {
4919 JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
4920 }
4921
4922 s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
4923 }
4924
4925 template<typename BasicJsonType>
4926 inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val)
4927 {
4928 get_arithmetic_value(j, val);
4929 }
4930
4931 template<typename BasicJsonType>
4932 inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val)
4933 {
4934 get_arithmetic_value(j, val);
4935 }
4936
4937 template<typename BasicJsonType>
4938 inline void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val)
4939 {
4940 get_arithmetic_value(j, val);
4941 }
4942
4943 #if !JSON_DISABLE_ENUM_SERIALIZATION
4944 template<typename BasicJsonType, typename EnumType,
4945 enable_if_t<std::is_enum<EnumType>::value, int> = 0>
4946 inline void from_json(const BasicJsonType& j, EnumType& e)
4947 {
4948 typename std::underlying_type<EnumType>::type val;
4949 get_arithmetic_value(j, val);
4950 e = static_cast<EnumType>(val);
4951 }
4952 #endif // JSON_DISABLE_ENUM_SERIALIZATION
4953
4954 // forward_list doesn't have an insert method
4955 template<typename BasicJsonType, typename T, typename Allocator,
4956 enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
4957 inline void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
4958 {
4959 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
4960 {
4961 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
4962 }
4963 l.clear();
4964 std::transform(j.rbegin(), j.rend(),
4965 std::front_inserter(l), [](const BasicJsonType & i)
4966 {
4967 return i.template get<T>();
4968 });
4969 }
4970
4971 // valarray doesn't have an insert method
4972 template<typename BasicJsonType, typename T,
4973 enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
4974 inline void from_json(const BasicJsonType& j, std::valarray<T>& l)
4975 {
4976 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
4977 {
4978 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
4979 }
4980 l.resize(j.size());
4981 std::transform(j.begin(), j.end(), std::begin(l),
4982 [](const BasicJsonType & elem)
4983 {
4984 return elem.template get<T>();
4985 });
4986 }
4987
4988 template<typename BasicJsonType, typename T, std::size_t N>
4989 auto from_json(const BasicJsonType& j, T (&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
4990 -> decltype(j.template get<T>(), void())
4991 {
4992 for (std::size_t i = 0; i < N; ++i)
4993 {
4994 arr[i] = j.at(i).template get<T>();
4995 }
4996 }
4997
4998 template<typename BasicJsonType, typename T, std::size_t N1, std::size_t N2>
4999 auto from_json(const BasicJsonType& j, T (&arr)[N1][N2]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
5000 -> decltype(j.template get<T>(), void())
5001 {
5002 for (std::size_t i1 = 0; i1 < N1; ++i1)
5003 {
5004 for (std::size_t i2 = 0; i2 < N2; ++i2)
5005 {
5006 arr[i1][i2] = j.at(i1).at(i2).template get<T>();
5007 }
5008 }
5009 }
5010
5011 template<typename BasicJsonType, typename T, std::size_t N1, std::size_t N2, std::size_t N3>
5012 auto from_json(const BasicJsonType& j, T (&arr)[N1][N2][N3]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
5013 -> decltype(j.template get<T>(), void())
5014 {
5015 for (std::size_t i1 = 0; i1 < N1; ++i1)
5016 {
5017 for (std::size_t i2 = 0; i2 < N2; ++i2)
5018 {
5019 for (std::size_t i3 = 0; i3 < N3; ++i3)
5020 {
5021 arr[i1][i2][i3] = j.at(i1).at(i2).at(i3).template get<T>();
5022 }
5023 }
5024 }
5025 }
5026
5027 template<typename BasicJsonType, typename T, std::size_t N1, std::size_t N2, std::size_t N3, std::size_t N4>
5028 auto from_json(const BasicJsonType& j, T (&arr)[N1][N2][N3][N4]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
5029 -> decltype(j.template get<T>(), void())
5030 {
5031 for (std::size_t i1 = 0; i1 < N1; ++i1)
5032 {
5033 for (std::size_t i2 = 0; i2 < N2; ++i2)
5034 {
5035 for (std::size_t i3 = 0; i3 < N3; ++i3)
5036 {
5037 for (std::size_t i4 = 0; i4 < N4; ++i4)
5038 {
5039 arr[i1][i2][i3][i4] = j.at(i1).at(i2).at(i3).at(i4).template get<T>();
5040 }
5041 }
5042 }
5043 }
5044 }
5045
5046 template<typename BasicJsonType>
5047 inline void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType::array_t& arr, priority_tag<3> /*unused*/)
5048 {
5049 arr = *j.template get_ptr<const typename BasicJsonType::array_t*>();
5050 }
5051
5052 template<typename BasicJsonType, typename T, std::size_t N>
5053 auto from_json_array_impl(const BasicJsonType& j, std::array<T, N>& arr,
5054 priority_tag<2> /*unused*/)
5055 -> decltype(j.template get<T>(), void())
5056 {
5057 for (std::size_t i = 0; i < N; ++i)
5058 {
5059 arr[i] = j.at(i).template get<T>();
5060 }
5061 }
5062
5063 template<typename BasicJsonType, typename ConstructibleArrayType,
5064 enable_if_t<
5065 std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
5066 int> = 0>
5067 auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<1> /*unused*/)
5068 -> decltype(
5069 arr.reserve(std::declval<typename ConstructibleArrayType::size_type>()),
5070 j.template get<typename ConstructibleArrayType::value_type>(),
5071 void())
5072 {
5073 using std::end;
5074
5075 ConstructibleArrayType ret;
5076 ret.reserve(j.size());
5077 std::transform(j.begin(), j.end(),
5078 std::inserter(ret, end(ret)), [](const BasicJsonType & i)
5079 {
5080 // get<BasicJsonType>() returns *this, this won't call a from_json
5081 // method when value_type is BasicJsonType
5082 return i.template get<typename ConstructibleArrayType::value_type>();
5083 });
5084 arr = std::move(ret);
5085 }
5086
5087 template<typename BasicJsonType, typename ConstructibleArrayType,
5088 enable_if_t<
5089 std::is_assignable<ConstructibleArrayType&, ConstructibleArrayType>::value,
5090 int> = 0>
5091 inline void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr,
5092 priority_tag<0> /*unused*/)
5093 {
5094 using std::end;
5095
5096 ConstructibleArrayType ret;
5097 std::transform(
5098 j.begin(), j.end(), std::inserter(ret, end(ret)),
5099 [](const BasicJsonType & i)
5100 {
5101 // get<BasicJsonType>() returns *this, this won't call a from_json
5102 // method when value_type is BasicJsonType
5103 return i.template get<typename ConstructibleArrayType::value_type>();
5104 });
5105 arr = std::move(ret);
5106 }
5107
5108 template < typename BasicJsonType, typename ConstructibleArrayType,
5109 enable_if_t <
5110 is_constructible_array_type<BasicJsonType, ConstructibleArrayType>::value&&
5111 !is_constructible_object_type<BasicJsonType, ConstructibleArrayType>::value&&
5112 !is_constructible_string_type<BasicJsonType, ConstructibleArrayType>::value&&
5113 !std::is_same<ConstructibleArrayType, typename BasicJsonType::binary_t>::value&&
5114 !is_basic_json<ConstructibleArrayType>::value,
5115 int > = 0 >
5116 auto from_json(const BasicJsonType& j, ConstructibleArrayType& arr)
5117 -> decltype(from_json_array_impl(j, arr, priority_tag<3> {}),
5118 j.template get<typename ConstructibleArrayType::value_type>(),
5119 void())
5120 {
5121 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
5122 {
5123 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
5124 }
5125
5126 from_json_array_impl(j, arr, priority_tag<3> {});
5127 }
5128
5129 template < typename BasicJsonType, typename T, std::size_t... Idx >
5130 std::array<T, sizeof...(Idx)> from_json_inplace_array_impl(BasicJsonType&& j,
5131 identity_tag<std::array<T, sizeof...(Idx)>> /*unused*/, index_sequence<Idx...> /*unused*/)
5132 {
5133 return { { std::forward<BasicJsonType>(j).at(Idx).template get<T>()... } };
5134 }
5135
5136 template < typename BasicJsonType, typename T, std::size_t N >
5137 auto from_json(BasicJsonType&& j, identity_tag<std::array<T, N>> tag)
5138 -> decltype(from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {}))
5139 {
5140 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
5141 {
5142 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
5143 }
5144
5145 return from_json_inplace_array_impl(std::forward<BasicJsonType>(j), tag, make_index_sequence<N> {});
5146 }
5147
5148 template<typename BasicJsonType>
5149 inline void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin)
5150 {
5151 if (JSON_HEDLEY_UNLIKELY(!j.is_binary()))
5152 {
5153 JSON_THROW(type_error::create(302, concat("type must be binary, but is ", j.type_name()), &j));
5154 }
5155
5156 bin = *j.template get_ptr<const typename BasicJsonType::binary_t*>();
5157 }
5158
5159 template<typename BasicJsonType, typename ConstructibleObjectType,
5160 enable_if_t<is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value, int> = 0>
5161 inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
5162 {
5163 if (JSON_HEDLEY_UNLIKELY(!j.is_object()))
5164 {
5165 JSON_THROW(type_error::create(302, concat("type must be object, but is ", j.type_name()), &j));
5166 }
5167
5168 ConstructibleObjectType ret;
5169 const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
5170 using value_type = typename ConstructibleObjectType::value_type;
5171 std::transform(
5172 inner_object->begin(), inner_object->end(),
5173 std::inserter(ret, ret.begin()),
5174 [](typename BasicJsonType::object_t::value_type const & p)
5175 {
5176 return value_type(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
5177 });
5178 obj = std::move(ret);
5179 }
5180
5181 // overload for arithmetic types, not chosen for basic_json template arguments
5182 // (BooleanType, etc..); note: Is it really necessary to provide explicit
5183 // overloads for boolean_t etc. in case of a custom BooleanType which is not
5184 // an arithmetic type?
5185 template < typename BasicJsonType, typename ArithmeticType,
5186 enable_if_t <
5187 std::is_arithmetic<ArithmeticType>::value&&
5188 !std::is_same<ArithmeticType, typename BasicJsonType::number_unsigned_t>::value&&
5189 !std::is_same<ArithmeticType, typename BasicJsonType::number_integer_t>::value&&
5190 !std::is_same<ArithmeticType, typename BasicJsonType::number_float_t>::value&&
5191 !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
5192 int > = 0 >
5193 inline void from_json(const BasicJsonType& j, ArithmeticType& val)
5194 {
5195 switch (static_cast<value_t>(j))
5196 {
5197 case value_t::number_unsigned:
5198 {
5199 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
5200 break;
5201 }
5202 case value_t::number_integer:
5203 {
5204 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
5205 break;
5206 }
5207 case value_t::number_float:
5208 {
5209 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
5210 break;
5211 }
5212 case value_t::boolean:
5213 {
5214 val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
5215 break;
5216 }
5217
5218 case value_t::null:
5219 case value_t::object:
5220 case value_t::array:
5221 case value_t::string:
5222 case value_t::binary:
5223 case value_t::discarded:
5224 default:
5225 JSON_THROW(type_error::create(302, concat("type must be number, but is ", j.type_name()), &j));
5226 }
5227 }
5228
5229 template<typename BasicJsonType, typename... Args, std::size_t... Idx>
5230 std::tuple<Args...> from_json_tuple_impl_base(BasicJsonType&& j, index_sequence<Idx...> /*unused*/)
5231 {
5232 return std::make_tuple(std::forward<BasicJsonType>(j).at(Idx).template get<Args>()...);
5233 }
5234
5235 template<typename BasicJsonType>
5236 std::tuple<> from_json_tuple_impl_base(BasicJsonType& /*unused*/, index_sequence<> /*unused*/)
5237 {
5238 return {};
5239 }
5240
5241 template < typename BasicJsonType, class A1, class A2 >
5242 std::pair<A1, A2> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::pair<A1, A2>> /*unused*/, priority_tag<0> /*unused*/)
5243 {
5244 return {std::forward<BasicJsonType>(j).at(0).template get<A1>(),
5245 std::forward<BasicJsonType>(j).at(1).template get<A2>()};
5246 }
5247
5248 template<typename BasicJsonType, typename A1, typename A2>
5249 inline void from_json_tuple_impl(BasicJsonType&& j, std::pair<A1, A2>& p, priority_tag<1> /*unused*/)
5250 {
5251 p = from_json_tuple_impl(std::forward<BasicJsonType>(j), identity_tag<std::pair<A1, A2>> {}, priority_tag<0> {});
5252 }
5253
5254 template<typename BasicJsonType, typename... Args>
5255 std::tuple<Args...> from_json_tuple_impl(BasicJsonType&& j, identity_tag<std::tuple<Args...>> /*unused*/, priority_tag<2> /*unused*/)
5256 {
5257 return from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
5258 }
5259
5260 template<typename BasicJsonType, typename... Args>
5261 inline void from_json_tuple_impl(BasicJsonType&& j, std::tuple<Args...>& t, priority_tag<3> /*unused*/)
5262 {
5263 t = from_json_tuple_impl_base<BasicJsonType, Args...>(std::forward<BasicJsonType>(j), index_sequence_for<Args...> {});
5264 }
5265
5266 template<typename BasicJsonType, typename TupleRelated>
5267 auto from_json(BasicJsonType&& j, TupleRelated&& t)
5268 -> decltype(from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {}))
5269 {
5270 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
5271 {
5272 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
5273 }
5274
5275 return from_json_tuple_impl(std::forward<BasicJsonType>(j), std::forward<TupleRelated>(t), priority_tag<3> {});
5276 }
5277
5278 template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator,
5279 typename = enable_if_t < !std::is_constructible <
5280 typename BasicJsonType::string_t, Key >::value >>
5281 inline void from_json(const BasicJsonType& j, std::map<Key, Value, Compare, Allocator>& m)
5282 {
5283 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
5284 {
5285 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
5286 }
5287 m.clear();
5288 for (const auto& p : j)
5289 {
5290 if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
5291 {
5292 JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
5293 }
5294 m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
5295 }
5296 }
5297
5298 template < typename BasicJsonType, typename Key, typename Value, typename Hash, typename KeyEqual, typename Allocator,
5299 typename = enable_if_t < !std::is_constructible <
5300 typename BasicJsonType::string_t, Key >::value >>
5301 inline void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>& m)
5302 {
5303 if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
5304 {
5305 JSON_THROW(type_error::create(302, concat("type must be array, but is ", j.type_name()), &j));
5306 }
5307 m.clear();
5308 for (const auto& p : j)
5309 {
5310 if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
5311 {
5312 JSON_THROW(type_error::create(302, concat("type must be array, but is ", p.type_name()), &j));
5313 }
5314 m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
5315 }
5316 }
5317
5318 #if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
5319 template<typename BasicJsonType>
5320 inline void from_json(const BasicJsonType& j, std_fs::path& p)
5321 {
5322 if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
5323 {
5324 JSON_THROW(type_error::create(302, concat("type must be string, but is ", j.type_name()), &j));
5325 }
5326 const auto& s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
5327 // Checking for C++20 standard or later can be insufficient in case the
5328 // library support for char8_t is either incomplete or was disabled
5329 // altogether. Use the __cpp_lib_char8_t feature test instead.
5330 #if defined(__cpp_lib_char8_t) && (__cpp_lib_char8_t >= 201907L)
5331 p = std_fs::path(std::u8string_view(reinterpret_cast<const char8_t*>(s.data()), s.size()));
5332 #else
5333 p = std_fs::u8path(s); // accepts UTF-8 encoded std::string in C++17, deprecated in C++20
5334 #endif
5335 }
5336 #endif
5337
5338 struct from_json_fn
5339 {
5340 template<typename BasicJsonType, typename T>
5341 auto operator()(const BasicJsonType& j, T&& val) const
5342 noexcept(noexcept(from_json(j, std::forward<T>(val))))
5343 -> decltype(from_json(j, std::forward<T>(val)))
5344 {
5345 return from_json(j, std::forward<T>(val));
5346 }
5347 };
5348
5349 } // namespace detail
5350
5351 #ifndef JSON_HAS_CPP_17
5352 /// namespace to hold default `from_json` function
5353 /// to see why this is required:
5354 /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
5355 namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
5356 {
5357 #endif
5358 JSON_INLINE_VARIABLE constexpr const auto& from_json = // NOLINT(misc-definitions-in-headers)
5359 detail::static_const<detail::from_json_fn>::value;
5360 #ifndef JSON_HAS_CPP_17
5361 } // namespace
5362 #endif
5363
5364 NLOHMANN_JSON_NAMESPACE_END
5365
5366 // #include <nlohmann/detail/conversions/to_json.hpp>
5367 // __ _____ _____ _____
5368 // __| | __| | | | JSON for Modern C++
5369 // | | |__ | | | | | | version 3.12.0
5370 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
5371 //
5372 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
5373 // SPDX-License-Identifier: MIT
5374
5375
5376
5377 // #include <nlohmann/detail/macro_scope.hpp>
5378 // JSON_HAS_CPP_17
5379 #ifdef JSON_HAS_CPP_17
5380 #include <optional> // optional
5381 #endif
5382
5383 #include <algorithm> // copy
5384 #include <iterator> // begin, end
5385 #include <memory> // allocator_traits
5386 #include <string> // basic_string, char_traits
5387 #include <tuple> // tuple, get
5388 #include <type_traits> // is_same, is_constructible, is_floating_point, is_enum, underlying_type
5389 #include <utility> // move, forward, declval, pair
5390 #include <valarray> // valarray
5391 #include <vector> // vector
5392
5393 // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
5394 // __ _____ _____ _____
5395 // __| | __| | | | JSON for Modern C++
5396 // | | |__ | | | | | | version 3.12.0
5397 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
5398 //
5399 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
5400 // SPDX-License-Identifier: MIT
5401
5402
5403
5404 #include <cstddef> // size_t
5405 #include <iterator> // forward_iterator_tag
5406 #include <tuple> // tuple_size, get, tuple_element
5407 #include <utility> // move
5408
5409 #if JSON_HAS_RANGES
5410 #include <ranges> // enable_borrowed_range
5411 #endif
5412
5413 // #include <nlohmann/detail/abi_macros.hpp>
5414
5415 // #include <nlohmann/detail/meta/type_traits.hpp>
5416
5417 // #include <nlohmann/detail/string_utils.hpp>
5418 // __ _____ _____ _____
5419 // __| | __| | | | JSON for Modern C++
5420 // | | |__ | | | | | | version 3.12.0
5421 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
5422 //
5423 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
5424 // SPDX-License-Identifier: MIT
5425
5426
5427
5428 #include <cstddef> // size_t
5429 #include <string> // string, to_string
5430
5431 // #include <nlohmann/detail/abi_macros.hpp>
5432
5433
5434 NLOHMANN_JSON_NAMESPACE_BEGIN
5435 namespace detail
5436 {
5437
5438 template<typename StringType>
5439 void int_to_string(StringType& target, std::size_t value)
5440 {
5441 // For ADL
5442 using std::to_string;
5443 target = to_string(value);
5444 }
5445
5446 template<typename StringType>
5447 StringType to_string(std::size_t value)
5448 {
5449 StringType result;
5450 int_to_string(result, value);
5451 return result;
5452 }
5453
5454 } // namespace detail
5455 NLOHMANN_JSON_NAMESPACE_END
5456
5457 // #include <nlohmann/detail/value_t.hpp>
5458
5459
5460 NLOHMANN_JSON_NAMESPACE_BEGIN
5461 namespace detail
5462 {
5463
5464 template<typename IteratorType> class iteration_proxy_value
5465 {
5466 public:
5467 using difference_type = std::ptrdiff_t;
5468 using value_type = iteration_proxy_value;
5469 using pointer = value_type *;
5470 using reference = value_type &;
5471 using iterator_category = std::forward_iterator_tag;
5472 using string_type = typename std::remove_cv< typename std::remove_reference<decltype( std::declval<IteratorType>().key() ) >::type >::type;
5473
5474 private:
5475 /// the iterator
5476 IteratorType anchor{};
5477 /// an index for arrays (used to create key names)
5478 std::size_t array_index = 0;
5479 /// last stringified array index
5480 mutable std::size_t array_index_last = 0;
5481 /// a string representation of the array index
5482 mutable string_type array_index_str = "0";
5483 /// an empty string (to return a reference for primitive values)
5484 string_type empty_str{};
5485
5486 public:
5487 explicit iteration_proxy_value() = default;
5488 explicit iteration_proxy_value(IteratorType it, std::size_t array_index_ = 0)
5489 noexcept(std::is_nothrow_move_constructible<IteratorType>::value
5490 && std::is_nothrow_default_constructible<string_type>::value)
5491 : anchor(std::move(it))
5492 , array_index(array_index_)
5493 {}
5494
5495 iteration_proxy_value(iteration_proxy_value const&) = default;
5496 iteration_proxy_value& operator=(iteration_proxy_value const&) = default;
5497 // older GCCs are a bit fussy and require explicit noexcept specifiers on defaulted functions
5498 iteration_proxy_value(iteration_proxy_value&&)
5499 noexcept(std::is_nothrow_move_constructible<IteratorType>::value
5500 && std::is_nothrow_move_constructible<string_type>::value) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor,cppcoreguidelines-noexcept-move-operations)
5501 iteration_proxy_value& operator=(iteration_proxy_value&&)
5502 noexcept(std::is_nothrow_move_assignable<IteratorType>::value
5503 && std::is_nothrow_move_assignable<string_type>::value) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor,cppcoreguidelines-noexcept-move-operations)
5504 ~iteration_proxy_value() = default;
5505
5506 /// dereference operator (needed for range-based for)
5507 const iteration_proxy_value& operator*() const
5508 {
5509 return *this;
5510 }
5511
5512 /// increment operator (needed for range-based for)
5513 iteration_proxy_value& operator++()
5514 {
5515 ++anchor;
5516 ++array_index;
5517
5518 return *this;
5519 }
5520
5521 iteration_proxy_value operator++(int)& // NOLINT(cert-dcl21-cpp)
5522 {
5523 auto tmp = iteration_proxy_value(anchor, array_index);
5524 ++anchor;
5525 ++array_index;
5526 return tmp;
5527 }
5528
5529 /// equality operator (needed for InputIterator)
5530 bool operator==(const iteration_proxy_value& o) const
5531 {
5532 return anchor == o.anchor;
5533 }
5534
5535 /// inequality operator (needed for range-based for)
5536 bool operator!=(const iteration_proxy_value& o) const
5537 {
5538 return anchor != o.anchor;
5539 }
5540
5541 /// return key of the iterator
5542 const string_type& key() const
5543 {
5544 JSON_ASSERT(anchor.m_object != nullptr);
5545
5546 switch (anchor.m_object->type())
5547 {
5548 // use integer array index as key
5549 case value_t::array:
5550 {
5551 if (array_index != array_index_last)
5552 {
5553 int_to_string( array_index_str, array_index );
5554 array_index_last = array_index;
5555 }
5556 return array_index_str;
5557 }
5558
5559 // use key from the object
5560 case value_t::object:
5561 return anchor.key();
5562
5563 // use an empty key for all primitive types
5564 case value_t::null:
5565 case value_t::string:
5566 case value_t::boolean:
5567 case value_t::number_integer:
5568 case value_t::number_unsigned:
5569 case value_t::number_float:
5570 case value_t::binary:
5571 case value_t::discarded:
5572 default:
5573 return empty_str;
5574 }
5575 }
5576
5577 /// return value of the iterator
5578 typename IteratorType::reference value() const
5579 {
5580 return anchor.value();
5581 }
5582 };
5583
5584 /// proxy class for the items() function
5585 template<typename IteratorType> class iteration_proxy
5586 {
5587 private:
5588 /// the container to iterate
5589 typename IteratorType::pointer container = nullptr;
5590
5591 public:
5592 explicit iteration_proxy() = default;
5593
5594 /// construct iteration proxy from a container
5595 explicit iteration_proxy(typename IteratorType::reference cont) noexcept
5596 : container(&cont) {}
5597
5598 iteration_proxy(iteration_proxy const&) = default;
5599 iteration_proxy& operator=(iteration_proxy const&) = default;
5600 iteration_proxy(iteration_proxy&&) noexcept = default;
5601 iteration_proxy& operator=(iteration_proxy&&) noexcept = default;
5602 ~iteration_proxy() = default;
5603
5604 /// return iterator begin (needed for range-based for)
5605 iteration_proxy_value<IteratorType> begin() const noexcept
5606 {
5607 return iteration_proxy_value<IteratorType>(container->begin());
5608 }
5609
5610 /// return iterator end (needed for range-based for)
5611 iteration_proxy_value<IteratorType> end() const noexcept
5612 {
5613 return iteration_proxy_value<IteratorType>(container->end());
5614 }
5615 };
5616
5617 // Structured Bindings Support
5618 // For further reference see https://blog.tartanllama.xyz/structured-bindings/
5619 // And see https://github.com/nlohmann/json/pull/1391
5620 template<std::size_t N, typename IteratorType, enable_if_t<N == 0, int> = 0>
5621 auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.key())
5622 {
5623 return i.key();
5624 }
5625 // Structured Bindings Support
5626 // For further reference see https://blog.tartanllama.xyz/structured-bindings/
5627 // And see https://github.com/nlohmann/json/pull/1391
5628 template<std::size_t N, typename IteratorType, enable_if_t<N == 1, int> = 0>
5629 auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.value())
5630 {
5631 return i.value();
5632 }
5633
5634 } // namespace detail
5635 NLOHMANN_JSON_NAMESPACE_END
5636
5637 // The Addition to the STD Namespace is required to add
5638 // Structured Bindings Support to the iteration_proxy_value class
5639 // For further reference see https://blog.tartanllama.xyz/structured-bindings/
5640 // And see https://github.com/nlohmann/json/pull/1391
5641 namespace std
5642 {
5643
5644 #if defined(__clang__)
5645 // Fix: https://github.com/nlohmann/json/issues/1401
5646 #pragma clang diagnostic push
5647 #pragma clang diagnostic ignored "-Wmismatched-tags"
5648 #endif
5649 template<typename IteratorType>
5650 class tuple_size<::nlohmann::detail::iteration_proxy_value<IteratorType>> // NOLINT(cert-dcl58-cpp)
5651 : public std::integral_constant<std::size_t, 2> {};
5652
5653 template<std::size_t N, typename IteratorType>
5654 class tuple_element<N, ::nlohmann::detail::iteration_proxy_value<IteratorType >> // NOLINT(cert-dcl58-cpp)
5655 {
5656 public:
5657 using type = decltype(
5658 get<N>(std::declval <
5659 ::nlohmann::detail::iteration_proxy_value<IteratorType >> ()));
5660 };
5661 #if defined(__clang__)
5662 #pragma clang diagnostic pop
5663 #endif
5664
5665 } // namespace std
5666
5667 #if JSON_HAS_RANGES
5668 template <typename IteratorType>
5669 inline constexpr bool ::std::ranges::enable_borrowed_range<::nlohmann::detail::iteration_proxy<IteratorType>> = true;
5670 #endif
5671
5672 // #include <nlohmann/detail/meta/cpp_future.hpp>
5673
5674 // #include <nlohmann/detail/meta/std_fs.hpp>
5675
5676 // #include <nlohmann/detail/meta/type_traits.hpp>
5677
5678 // #include <nlohmann/detail/value_t.hpp>
5679
5680
5681 NLOHMANN_JSON_NAMESPACE_BEGIN
5682 namespace detail
5683 {
5684
5685 //////////////////
5686 // constructors //
5687 //////////////////
5688
5689 /*
5690 * Note all external_constructor<>::construct functions need to call
5691 * j.m_data.m_value.destroy(j.m_data.m_type) to avoid a memory leak in case j contains an
5692 * allocated value (e.g., a string). See bug issue
5693 * https://github.com/nlohmann/json/issues/2865 for more information.
5694 */
5695
5696 template<value_t> struct external_constructor;
5697
5698 template<>
5699 struct external_constructor<value_t::boolean>
5700 {
5701 template<typename BasicJsonType>
5702 static void construct(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept
5703 {
5704 j.m_data.m_value.destroy(j.m_data.m_type);
5705 j.m_data.m_type = value_t::boolean;
5706 j.m_data.m_value = b;
5707 j.assert_invariant();
5708 }
5709 };
5710
5711 template<>
5712 struct external_constructor<value_t::string>
5713 {
5714 template<typename BasicJsonType>
5715 static void construct(BasicJsonType& j, const typename BasicJsonType::string_t& s)
5716 {
5717 j.m_data.m_value.destroy(j.m_data.m_type);
5718 j.m_data.m_type = value_t::string;
5719 j.m_data.m_value = s;
5720 j.assert_invariant();
5721 }
5722
5723 template<typename BasicJsonType>
5724 static void construct(BasicJsonType& j, typename BasicJsonType::string_t&& s)
5725 {
5726 j.m_data.m_value.destroy(j.m_data.m_type);
5727 j.m_data.m_type = value_t::string;
5728 j.m_data.m_value = std::move(s);
5729 j.assert_invariant();
5730 }
5731
5732 template < typename BasicJsonType, typename CompatibleStringType,
5733 enable_if_t < !std::is_same<CompatibleStringType, typename BasicJsonType::string_t>::value,
5734 int > = 0 >
5735 static void construct(BasicJsonType& j, const CompatibleStringType& str)
5736 {
5737 j.m_data.m_value.destroy(j.m_data.m_type);
5738 j.m_data.m_type = value_t::string;
5739 j.m_data.m_value.string = j.template create<typename BasicJsonType::string_t>(str);
5740 j.assert_invariant();
5741 }
5742 };
5743
5744 template<>
5745 struct external_constructor<value_t::binary>
5746 {
5747 template<typename BasicJsonType>
5748 static void construct(BasicJsonType& j, const typename BasicJsonType::binary_t& b)
5749 {
5750 j.m_data.m_value.destroy(j.m_data.m_type);
5751 j.m_data.m_type = value_t::binary;
5752 j.m_data.m_value = typename BasicJsonType::binary_t(b);
5753 j.assert_invariant();
5754 }
5755
5756 template<typename BasicJsonType>
5757 static void construct(BasicJsonType& j, typename BasicJsonType::binary_t&& b)
5758 {
5759 j.m_data.m_value.destroy(j.m_data.m_type);
5760 j.m_data.m_type = value_t::binary;
5761 j.m_data.m_value = typename BasicJsonType::binary_t(std::move(b));
5762 j.assert_invariant();
5763 }
5764 };
5765
5766 template<>
5767 struct external_constructor<value_t::number_float>
5768 {
5769 template<typename BasicJsonType>
5770 static void construct(BasicJsonType& j, typename BasicJsonType::number_float_t val) noexcept
5771 {
5772 j.m_data.m_value.destroy(j.m_data.m_type);
5773 j.m_data.m_type = value_t::number_float;
5774 j.m_data.m_value = val;
5775 j.assert_invariant();
5776 }
5777 };
5778
5779 template<>
5780 struct external_constructor<value_t::number_unsigned>
5781 {
5782 template<typename BasicJsonType>
5783 static void construct(BasicJsonType& j, typename BasicJsonType::number_unsigned_t val) noexcept
5784 {
5785 j.m_data.m_value.destroy(j.m_data.m_type);
5786 j.m_data.m_type = value_t::number_unsigned;
5787 j.m_data.m_value = val;
5788 j.assert_invariant();
5789 }
5790 };
5791
5792 template<>
5793 struct external_constructor<value_t::number_integer>
5794 {
5795 template<typename BasicJsonType>
5796 static void construct(BasicJsonType& j, typename BasicJsonType::number_integer_t val) noexcept
5797 {
5798 j.m_data.m_value.destroy(j.m_data.m_type);
5799 j.m_data.m_type = value_t::number_integer;
5800 j.m_data.m_value = val;
5801 j.assert_invariant();
5802 }
5803 };
5804
5805 template<>
5806 struct external_constructor<value_t::array>
5807 {
5808 template<typename BasicJsonType>
5809 static void construct(BasicJsonType& j, const typename BasicJsonType::array_t& arr)
5810 {
5811 j.m_data.m_value.destroy(j.m_data.m_type);
5812 j.m_data.m_type = value_t::array;
5813 j.m_data.m_value = arr;
5814 j.set_parents();
5815 j.assert_invariant();
5816 }
5817
5818 template<typename BasicJsonType>
5819 static void construct(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
5820 {
5821 j.m_data.m_value.destroy(j.m_data.m_type);
5822 j.m_data.m_type = value_t::array;
5823 j.m_data.m_value = std::move(arr);
5824 j.set_parents();
5825 j.assert_invariant();
5826 }
5827
5828 template < typename BasicJsonType, typename CompatibleArrayType,
5829 enable_if_t < !std::is_same<CompatibleArrayType, typename BasicJsonType::array_t>::value,
5830 int > = 0 >
5831 static void construct(BasicJsonType& j, const CompatibleArrayType& arr)
5832 {
5833 using std::begin;
5834 using std::end;
5835
5836 j.m_data.m_value.destroy(j.m_data.m_type);
5837 j.m_data.m_type = value_t::array;
5838 j.m_data.m_value.array = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
5839 j.set_parents();
5840 j.assert_invariant();
5841 }
5842
5843 template<typename BasicJsonType>
5844 static void construct(BasicJsonType& j, const std::vector<bool>& arr)
5845 {
5846 j.m_data.m_value.destroy(j.m_data.m_type);
5847 j.m_data.m_type = value_t::array;
5848 j.m_data.m_value = value_t::array;
5849 j.m_data.m_value.array->reserve(arr.size());
5850 for (const bool x : arr)
5851 {
5852 j.m_data.m_value.array->push_back(x);
5853 j.set_parent(j.m_data.m_value.array->back());
5854 }
5855 j.assert_invariant();
5856 }
5857
5858 template<typename BasicJsonType, typename T,
5859 enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
5860 static void construct(BasicJsonType& j, const std::valarray<T>& arr)
5861 {
5862 j.m_data.m_value.destroy(j.m_data.m_type);
5863 j.m_data.m_type = value_t::array;
5864 j.m_data.m_value = value_t::array;
5865 j.m_data.m_value.array->resize(arr.size());
5866 if (arr.size() > 0)
5867 {
5868 std::copy(std::begin(arr), std::end(arr), j.m_data.m_value.array->begin());
5869 }
5870 j.set_parents();
5871 j.assert_invariant();
5872 }
5873 };
5874
5875 template<>
5876 struct external_constructor<value_t::object>
5877 {
5878 template<typename BasicJsonType>
5879 static void construct(BasicJsonType& j, const typename BasicJsonType::object_t& obj)
5880 {
5881 j.m_data.m_value.destroy(j.m_data.m_type);
5882 j.m_data.m_type = value_t::object;
5883 j.m_data.m_value = obj;
5884 j.set_parents();
5885 j.assert_invariant();
5886 }
5887
5888 template<typename BasicJsonType>
5889 static void construct(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
5890 {
5891 j.m_data.m_value.destroy(j.m_data.m_type);
5892 j.m_data.m_type = value_t::object;
5893 j.m_data.m_value = std::move(obj);
5894 j.set_parents();
5895 j.assert_invariant();
5896 }
5897
5898 template < typename BasicJsonType, typename CompatibleObjectType,
5899 enable_if_t < !std::is_same<CompatibleObjectType, typename BasicJsonType::object_t>::value, int > = 0 >
5900 static void construct(BasicJsonType& j, const CompatibleObjectType& obj)
5901 {
5902 using std::begin;
5903 using std::end;
5904
5905 j.m_data.m_value.destroy(j.m_data.m_type);
5906 j.m_data.m_type = value_t::object;
5907 j.m_data.m_value.object = j.template create<typename BasicJsonType::object_t>(begin(obj), end(obj));
5908 j.set_parents();
5909 j.assert_invariant();
5910 }
5911 };
5912
5913 /////////////
5914 // to_json //
5915 /////////////
5916
5917 #ifdef JSON_HAS_CPP_17
5918 template<typename BasicJsonType, typename T,
5919 enable_if_t<std::is_constructible<BasicJsonType, T>::value, int> = 0>
5920 void to_json(BasicJsonType& j, const std::optional<T>& opt) noexcept
5921 {
5922 if (opt.has_value())
5923 {
5924 j = *opt;
5925 }
5926 else
5927 {
5928 j = nullptr;
5929 }
5930 }
5931 #endif
5932
5933 template<typename BasicJsonType, typename T,
5934 enable_if_t<std::is_same<T, typename BasicJsonType::boolean_t>::value, int> = 0>
5935 inline void to_json(BasicJsonType& j, T b) noexcept
5936 {
5937 external_constructor<value_t::boolean>::construct(j, b);
5938 }
5939
5940 template < typename BasicJsonType, typename BoolRef,
5941 enable_if_t <
5942 ((std::is_same<std::vector<bool>::reference, BoolRef>::value
5943 && !std::is_same <std::vector<bool>::reference, typename BasicJsonType::boolean_t&>::value)
5944 || (std::is_same<std::vector<bool>::const_reference, BoolRef>::value
5945 && !std::is_same <detail::uncvref_t<std::vector<bool>::const_reference>,
5946 typename BasicJsonType::boolean_t >::value))
5947 && std::is_convertible<const BoolRef&, typename BasicJsonType::boolean_t>::value, int > = 0 >
5948 inline void to_json(BasicJsonType& j, const BoolRef& b) noexcept
5949 {
5950 external_constructor<value_t::boolean>::construct(j, static_cast<typename BasicJsonType::boolean_t>(b));
5951 }
5952
5953 template<typename BasicJsonType, typename CompatibleString,
5954 enable_if_t<std::is_constructible<typename BasicJsonType::string_t, CompatibleString>::value, int> = 0>
5955 inline void to_json(BasicJsonType& j, const CompatibleString& s)
5956 {
5957 external_constructor<value_t::string>::construct(j, s);
5958 }
5959
5960 template<typename BasicJsonType>
5961 inline void to_json(BasicJsonType& j, typename BasicJsonType::string_t&& s)
5962 {
5963 external_constructor<value_t::string>::construct(j, std::move(s));
5964 }
5965
5966 template<typename BasicJsonType, typename FloatType,
5967 enable_if_t<std::is_floating_point<FloatType>::value, int> = 0>
5968 inline void to_json(BasicJsonType& j, FloatType val) noexcept
5969 {
5970 external_constructor<value_t::number_float>::construct(j, static_cast<typename BasicJsonType::number_float_t>(val));
5971 }
5972
5973 template<typename BasicJsonType, typename CompatibleNumberUnsignedType,
5974 enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_unsigned_t, CompatibleNumberUnsignedType>::value, int> = 0>
5975 inline void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept
5976 {
5977 external_constructor<value_t::number_unsigned>::construct(j, static_cast<typename BasicJsonType::number_unsigned_t>(val));
5978 }
5979
5980 template<typename BasicJsonType, typename CompatibleNumberIntegerType,
5981 enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_integer_t, CompatibleNumberIntegerType>::value, int> = 0>
5982 inline void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept
5983 {
5984 external_constructor<value_t::number_integer>::construct(j, static_cast<typename BasicJsonType::number_integer_t>(val));
5985 }
5986
5987 #if !JSON_DISABLE_ENUM_SERIALIZATION
5988 template<typename BasicJsonType, typename EnumType,
5989 enable_if_t<std::is_enum<EnumType>::value, int> = 0>
5990 inline void to_json(BasicJsonType& j, EnumType e) noexcept
5991 {
5992 using underlying_type = typename std::underlying_type<EnumType>::type;
5993 static constexpr value_t integral_value_t = std::is_unsigned<underlying_type>::value ? value_t::number_unsigned : value_t::number_integer;
5994 external_constructor<integral_value_t>::construct(j, static_cast<underlying_type>(e));
5995 }
5996 #endif // JSON_DISABLE_ENUM_SERIALIZATION
5997
5998 template<typename BasicJsonType>
5999 inline void to_json(BasicJsonType& j, const std::vector<bool>& e)
6000 {
6001 external_constructor<value_t::array>::construct(j, e);
6002 }
6003
6004 template < typename BasicJsonType, typename CompatibleArrayType,
6005 enable_if_t < is_compatible_array_type<BasicJsonType,
6006 CompatibleArrayType>::value&&
6007 !is_compatible_object_type<BasicJsonType, CompatibleArrayType>::value&&
6008 !is_compatible_string_type<BasicJsonType, CompatibleArrayType>::value&&
6009 !std::is_same<typename BasicJsonType::binary_t, CompatibleArrayType>::value&&
6010 !is_basic_json<CompatibleArrayType>::value,
6011 int > = 0 >
6012 inline void to_json(BasicJsonType& j, const CompatibleArrayType& arr)
6013 {
6014 external_constructor<value_t::array>::construct(j, arr);
6015 }
6016
6017 template<typename BasicJsonType>
6018 inline void to_json(BasicJsonType& j, const typename BasicJsonType::binary_t& bin)
6019 {
6020 external_constructor<value_t::binary>::construct(j, bin);
6021 }
6022
6023 template<typename BasicJsonType, typename T,
6024 enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
6025 inline void to_json(BasicJsonType& j, const std::valarray<T>& arr)
6026 {
6027 external_constructor<value_t::array>::construct(j, std::move(arr));
6028 }
6029
6030 template<typename BasicJsonType>
6031 inline void to_json(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
6032 {
6033 external_constructor<value_t::array>::construct(j, std::move(arr));
6034 }
6035
6036 template < typename BasicJsonType, typename CompatibleObjectType,
6037 enable_if_t < is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value&& !is_basic_json<CompatibleObjectType>::value, int > = 0 >
6038 inline void to_json(BasicJsonType& j, const CompatibleObjectType& obj)
6039 {
6040 external_constructor<value_t::object>::construct(j, obj);
6041 }
6042
6043 template<typename BasicJsonType>
6044 inline void to_json(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
6045 {
6046 external_constructor<value_t::object>::construct(j, std::move(obj));
6047 }
6048
6049 template <
6050 typename BasicJsonType, typename T, std::size_t N,
6051 enable_if_t < !std::is_constructible<typename BasicJsonType::string_t,
6052 const T(&)[N]>::value, // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
6053 int > = 0 >
6054 inline void to_json(BasicJsonType& j, const T(&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
6055 {
6056 external_constructor<value_t::array>::construct(j, arr);
6057 }
6058
6059 template < typename BasicJsonType, typename T1, typename T2, enable_if_t < std::is_constructible<BasicJsonType, T1>::value&& std::is_constructible<BasicJsonType, T2>::value, int > = 0 >
6060 inline void to_json(BasicJsonType& j, const std::pair<T1, T2>& p)
6061 {
6062 j = { p.first, p.second };
6063 }
6064
6065 // for https://github.com/nlohmann/json/pull/1134
6066 template<typename BasicJsonType, typename T,
6067 enable_if_t<std::is_same<T, iteration_proxy_value<typename BasicJsonType::iterator>>::value, int> = 0>
6068 inline void to_json(BasicJsonType& j, const T& b)
6069 {
6070 j = { {b.key(), b.value()} };
6071 }
6072
6073 template<typename BasicJsonType, typename Tuple, std::size_t... Idx>
6074 inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence<Idx...> /*unused*/)
6075 {
6076 j = { std::get<Idx>(t)... };
6077 }
6078
6079 template<typename BasicJsonType, typename Tuple>
6080 inline void to_json_tuple_impl(BasicJsonType& j, const Tuple& /*unused*/, index_sequence<> /*unused*/)
6081 {
6082 using array_t = typename BasicJsonType::array_t;
6083 j = array_t();
6084 }
6085
6086 template<typename BasicJsonType, typename T, enable_if_t<is_constructible_tuple<BasicJsonType, T>::value, int > = 0>
6087 inline void to_json(BasicJsonType& j, const T& t)
6088 {
6089 to_json_tuple_impl(j, t, make_index_sequence<std::tuple_size<T>::value> {});
6090 }
6091
6092 #if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
6093 #if defined(__cpp_lib_char8_t)
6094 template<typename BasicJsonType, typename Tr, typename Allocator>
6095 inline void to_json(BasicJsonType& j, const std::basic_string<char8_t, Tr, Allocator>& s)
6096 {
6097 using OtherAllocator = typename std::allocator_traits<Allocator>::template rebind_alloc<char>;
6098 j = std::basic_string<char, std::char_traits<char>, OtherAllocator>(s.begin(), s.end(), s.get_allocator());
6099 }
6100 #endif
6101
6102 template<typename BasicJsonType>
6103 inline void to_json(BasicJsonType& j, const std_fs::path& p)
6104 {
6105 // Returns either a std::string or a std::u8string depending whether library
6106 // support for char8_t is enabled.
6107 j = p.u8string();
6108 }
6109 #endif
6110
6111 struct to_json_fn
6112 {
6113 template<typename BasicJsonType, typename T>
6114 auto operator()(BasicJsonType& j, T&& val) const noexcept(noexcept(to_json(j, std::forward<T>(val))))
6115 -> decltype(to_json(j, std::forward<T>(val)), void())
6116 {
6117 return to_json(j, std::forward<T>(val));
6118 }
6119 };
6120 } // namespace detail
6121
6122 #ifndef JSON_HAS_CPP_17
6123 /// namespace to hold default `to_json` function
6124 /// to see why this is required:
6125 /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
6126 namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces)
6127 {
6128 #endif
6129 JSON_INLINE_VARIABLE constexpr const auto& to_json = // NOLINT(misc-definitions-in-headers)
6130 detail::static_const<detail::to_json_fn>::value;
6131 #ifndef JSON_HAS_CPP_17
6132 } // namespace
6133 #endif
6134
6135 NLOHMANN_JSON_NAMESPACE_END
6136
6137 // #include <nlohmann/detail/meta/identity_tag.hpp>
6138
6139
6140 NLOHMANN_JSON_NAMESPACE_BEGIN
6141
6142 /// @sa https://json.nlohmann.me/api/adl_serializer/
6143 template<typename ValueType, typename>
6144 struct adl_serializer
6145 {
6146 /// @brief convert a JSON value to any value type
6147 /// @sa https://json.nlohmann.me/api/adl_serializer/from_json/
6148 template<typename BasicJsonType, typename TargetType = ValueType>
6149 static auto from_json(BasicJsonType && j, TargetType& val) noexcept(
6150 noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), val)))
6151 -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), val), void())
6152 {
6153 ::nlohmann::from_json(std::forward<BasicJsonType>(j), val);
6154 }
6155
6156 /// @brief convert a JSON value to any value type
6157 /// @sa https://json.nlohmann.me/api/adl_serializer/from_json/
6158 template<typename BasicJsonType, typename TargetType = ValueType>
6159 static auto from_json(BasicJsonType && j) noexcept(
6160 noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {})))
6161 -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {}))
6162 {
6163 return ::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {});
6164 }
6165
6166 /// @brief convert any value type to a JSON value
6167 /// @sa https://json.nlohmann.me/api/adl_serializer/to_json/
6168 template<typename BasicJsonType, typename TargetType = ValueType>
6169 static auto to_json(BasicJsonType& j, TargetType && val) noexcept(
6170 noexcept(::nlohmann::to_json(j, std::forward<TargetType>(val))))
6171 -> decltype(::nlohmann::to_json(j, std::forward<TargetType>(val)), void())
6172 {
6173 ::nlohmann::to_json(j, std::forward<TargetType>(val));
6174 }
6175 };
6176
6177 NLOHMANN_JSON_NAMESPACE_END
6178
6179 // #include <nlohmann/byte_container_with_subtype.hpp>
6180 // __ _____ _____ _____
6181 // __| | __| | | | JSON for Modern C++
6182 // | | |__ | | | | | | version 3.12.0
6183 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
6184 //
6185 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
6186 // SPDX-License-Identifier: MIT
6187
6188
6189
6190 #include <cstdint> // uint8_t, uint64_t
6191 #include <tuple> // tie
6192 #include <utility> // move
6193
6194 // #include <nlohmann/detail/abi_macros.hpp>
6195
6196
6197 NLOHMANN_JSON_NAMESPACE_BEGIN
6198
6199 /// @brief an internal type for a backed binary type
6200 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/
6201 template<typename BinaryType>
6202 class byte_container_with_subtype : public BinaryType
6203 {
6204 public:
6205 using container_type = BinaryType;
6206 using subtype_type = std::uint64_t;
6207
6208 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
6209 byte_container_with_subtype() noexcept(noexcept(container_type()))
6210 : container_type()
6211 {}
6212
6213 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
6214 byte_container_with_subtype(const container_type& b) noexcept(noexcept(container_type(b)))
6215 : container_type(b)
6216 {}
6217
6218 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
6219 byte_container_with_subtype(container_type&& b) noexcept(noexcept(container_type(std::move(b))))
6220 : container_type(std::move(b))
6221 {}
6222
6223 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
6224 byte_container_with_subtype(const container_type& b, subtype_type subtype_) noexcept(noexcept(container_type(b)))
6225 : container_type(b)
6226 , m_subtype(subtype_)
6227 , m_has_subtype(true)
6228 {}
6229
6230 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/byte_container_with_subtype/
6231 byte_container_with_subtype(container_type&& b, subtype_type subtype_) noexcept(noexcept(container_type(std::move(b))))
6232 : container_type(std::move(b))
6233 , m_subtype(subtype_)
6234 , m_has_subtype(true)
6235 {}
6236
6237 bool operator==(const byte_container_with_subtype& rhs) const
6238 {
6239 return std::tie(static_cast<const BinaryType&>(*this), m_subtype, m_has_subtype) ==
6240 std::tie(static_cast<const BinaryType&>(rhs), rhs.m_subtype, rhs.m_has_subtype);
6241 }
6242
6243 bool operator!=(const byte_container_with_subtype& rhs) const
6244 {
6245 return !(rhs == *this);
6246 }
6247
6248 /// @brief sets the binary subtype
6249 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/set_subtype/
6250 void set_subtype(subtype_type subtype_) noexcept
6251 {
6252 m_subtype = subtype_;
6253 m_has_subtype = true;
6254 }
6255
6256 /// @brief return the binary subtype
6257 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/subtype/
6258 constexpr subtype_type subtype() const noexcept
6259 {
6260 return m_has_subtype ? m_subtype : static_cast<subtype_type>(-1);
6261 }
6262
6263 /// @brief return whether the value has a subtype
6264 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/has_subtype/
6265 constexpr bool has_subtype() const noexcept
6266 {
6267 return m_has_subtype;
6268 }
6269
6270 /// @brief clears the binary subtype
6271 /// @sa https://json.nlohmann.me/api/byte_container_with_subtype/clear_subtype/
6272 void clear_subtype() noexcept
6273 {
6274 m_subtype = 0;
6275 m_has_subtype = false;
6276 }
6277
6278 private:
6279 subtype_type m_subtype = 0;
6280 bool m_has_subtype = false;
6281 };
6282
6283 NLOHMANN_JSON_NAMESPACE_END
6284
6285 // #include <nlohmann/detail/conversions/from_json.hpp>
6286
6287 // #include <nlohmann/detail/conversions/to_json.hpp>
6288
6289 // #include <nlohmann/detail/exceptions.hpp>
6290
6291 // #include <nlohmann/detail/hash.hpp>
6292 // __ _____ _____ _____
6293 // __| | __| | | | JSON for Modern C++
6294 // | | |__ | | | | | | version 3.12.0
6295 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
6296 //
6297 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
6298 // SPDX-License-Identifier: MIT
6299
6300
6301
6302 #include <cstdint> // uint8_t
6303 #include <cstddef> // size_t
6304 #include <functional> // hash
6305
6306 // #include <nlohmann/detail/abi_macros.hpp>
6307
6308 // #include <nlohmann/detail/value_t.hpp>
6309
6310
6311 NLOHMANN_JSON_NAMESPACE_BEGIN
6312 namespace detail
6313 {
6314
6315 // boost::hash_combine
6316 inline std::size_t combine(std::size_t seed, std::size_t h) noexcept
6317 {
6318 seed ^= h + 0x9e3779b9 + (seed << 6U) + (seed >> 2U);
6319 return seed;
6320 }
6321
6322 /*!
6323 @brief hash a JSON value
6324
6325 The hash function tries to rely on std::hash where possible. Furthermore, the
6326 type of the JSON value is taken into account to have different hash values for
6327 null, 0, 0U, and false, etc.
6328
6329 @tparam BasicJsonType basic_json specialization
6330 @param j JSON value to hash
6331 @return hash value of j
6332 */
6333 template<typename BasicJsonType>
6334 std::size_t hash(const BasicJsonType& j)
6335 {
6336 using string_t = typename BasicJsonType::string_t;
6337 using number_integer_t = typename BasicJsonType::number_integer_t;
6338 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
6339 using number_float_t = typename BasicJsonType::number_float_t;
6340
6341 const auto type = static_cast<std::size_t>(j.type());
6342 switch (j.type())
6343 {
6344 case BasicJsonType::value_t::null:
6345 case BasicJsonType::value_t::discarded:
6346 {
6347 return combine(type, 0);
6348 }
6349
6350 case BasicJsonType::value_t::object:
6351 {
6352 auto seed = combine(type, j.size());
6353 for (const auto& element : j.items())
6354 {
6355 const auto h = std::hash<string_t> {}(element.key());
6356 seed = combine(seed, h);
6357 seed = combine(seed, hash(element.value()));
6358 }
6359 return seed;
6360 }
6361
6362 case BasicJsonType::value_t::array:
6363 {
6364 auto seed = combine(type, j.size());
6365 for (const auto& element : j)
6366 {
6367 seed = combine(seed, hash(element));
6368 }
6369 return seed;
6370 }
6371
6372 case BasicJsonType::value_t::string:
6373 {
6374 const auto h = std::hash<string_t> {}(j.template get_ref<const string_t&>());
6375 return combine(type, h);
6376 }
6377
6378 case BasicJsonType::value_t::boolean:
6379 {
6380 const auto h = std::hash<bool> {}(j.template get<bool>());
6381 return combine(type, h);
6382 }
6383
6384 case BasicJsonType::value_t::number_integer:
6385 {
6386 const auto h = std::hash<number_integer_t> {}(j.template get<number_integer_t>());
6387 return combine(type, h);
6388 }
6389
6390 case BasicJsonType::value_t::number_unsigned:
6391 {
6392 const auto h = std::hash<number_unsigned_t> {}(j.template get<number_unsigned_t>());
6393 return combine(type, h);
6394 }
6395
6396 case BasicJsonType::value_t::number_float:
6397 {
6398 const auto h = std::hash<number_float_t> {}(j.template get<number_float_t>());
6399 return combine(type, h);
6400 }
6401
6402 case BasicJsonType::value_t::binary:
6403 {
6404 auto seed = combine(type, j.get_binary().size());
6405 const auto h = std::hash<bool> {}(j.get_binary().has_subtype());
6406 seed = combine(seed, h);
6407 seed = combine(seed, static_cast<std::size_t>(j.get_binary().subtype()));
6408 for (const auto byte : j.get_binary())
6409 {
6410 seed = combine(seed, std::hash<std::uint8_t> {}(byte));
6411 }
6412 return seed;
6413 }
6414
6415 default: // LCOV_EXCL_LINE
6416 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
6417 return 0; // LCOV_EXCL_LINE
6418 }
6419 }
6420
6421 } // namespace detail
6422 NLOHMANN_JSON_NAMESPACE_END
6423
6424 // #include <nlohmann/detail/input/binary_reader.hpp>
6425 // __ _____ _____ _____
6426 // __| | __| | | | JSON for Modern C++
6427 // | | |__ | | | | | | version 3.12.0
6428 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
6429 //
6430 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
6431 // SPDX-License-Identifier: MIT
6432
6433
6434
6435 #include <algorithm> // generate_n
6436 #include <array> // array
6437 #include <cmath> // ldexp
6438 #include <cstddef> // size_t
6439 #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
6440 #include <cstdio> // snprintf
6441 #include <cstring> // memcpy
6442 #include <iterator> // back_inserter
6443 #include <limits> // numeric_limits
6444 #include <string> // char_traits, string
6445 #include <utility> // make_pair, move
6446 #include <vector> // vector
6447 #ifdef __cpp_lib_byteswap
6448 #include <bit> //byteswap
6449 #endif
6450
6451 // #include <nlohmann/detail/exceptions.hpp>
6452
6453 // #include <nlohmann/detail/input/input_adapters.hpp>
6454 // __ _____ _____ _____
6455 // __| | __| | | | JSON for Modern C++
6456 // | | |__ | | | | | | version 3.12.0
6457 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
6458 //
6459 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
6460 // SPDX-License-Identifier: MIT
6461
6462
6463
6464 #include <array> // array
6465 #include <cstddef> // size_t
6466 #include <cstring> // strlen
6467 #include <iterator> // begin, end, iterator_traits, random_access_iterator_tag, distance, next
6468 #include <memory> // shared_ptr, make_shared, addressof
6469 #include <numeric> // accumulate
6470 #include <string> // string, char_traits
6471 #include <type_traits> // enable_if, is_base_of, is_pointer, is_integral, remove_pointer
6472 #include <utility> // pair, declval
6473
6474 #ifndef JSON_NO_IO
6475 #include <cstdio> // FILE *
6476 #include <istream> // istream
6477 #endif // JSON_NO_IO
6478
6479 // #include <nlohmann/detail/exceptions.hpp>
6480
6481 // #include <nlohmann/detail/iterators/iterator_traits.hpp>
6482
6483 // #include <nlohmann/detail/macro_scope.hpp>
6484
6485 // #include <nlohmann/detail/meta/type_traits.hpp>
6486
6487
6488 NLOHMANN_JSON_NAMESPACE_BEGIN
6489 namespace detail
6490 {
6491
6492 /// the supported input formats
6493 enum class input_format_t { json, cbor, msgpack, ubjson, bson, bjdata };
6494
6495 ////////////////////
6496 // input adapters //
6497 ////////////////////
6498
6499 #ifndef JSON_NO_IO
6500 /*!
6501 Input adapter for stdio file access. This adapter read only 1 byte and do not use any
6502 buffer. This adapter is a very low level adapter.
6503 */
6504 class file_input_adapter
6505 {
6506 public:
6507 using char_type = char;
6508
6509 JSON_HEDLEY_NON_NULL(2)
6510 explicit file_input_adapter(std::FILE* f) noexcept
6511 : m_file(f)
6512 {
6513 JSON_ASSERT(m_file != nullptr);
6514 }
6515
6516 // make class move-only
6517 file_input_adapter(const file_input_adapter&) = delete;
6518 file_input_adapter(file_input_adapter&&) noexcept = default;
6519 file_input_adapter& operator=(const file_input_adapter&) = delete;
6520 file_input_adapter& operator=(file_input_adapter&&) = delete;
6521 ~file_input_adapter() = default;
6522
6523 std::char_traits<char>::int_type get_character() noexcept
6524 {
6525 return std::fgetc(m_file);
6526 }
6527
6528 // returns the number of characters successfully read
6529 template<class T>
6530 std::size_t get_elements(T* dest, std::size_t count = 1)
6531 {
6532 return fread(dest, 1, sizeof(T) * count, m_file);
6533 }
6534
6535 private:
6536 /// the file pointer to read from
6537 std::FILE* m_file;
6538 };
6539
6540 /*!
6541 Input adapter for a (caching) istream. Ignores a UFT Byte Order Mark at
6542 beginning of input. Does not support changing the underlying std::streambuf
6543 in mid-input. Maintains underlying std::istream and std::streambuf to support
6544 subsequent use of standard std::istream operations to process any input
6545 characters following those used in parsing the JSON input. Clears the
6546 std::istream flags; any input errors (e.g., EOF) will be detected by the first
6547 subsequent call for input from the std::istream.
6548 */
6549 class input_stream_adapter
6550 {
6551 public:
6552 using char_type = char;
6553
6554 ~input_stream_adapter()
6555 {
6556 // clear stream flags; we use underlying streambuf I/O, do not
6557 // maintain ifstream flags, except eof
6558 if (is != nullptr)
6559 {
6560 is->clear(is->rdstate() & std::ios::eofbit);
6561 }
6562 }
6563
6564 explicit input_stream_adapter(std::istream& i)
6565 : is(&i), sb(i.rdbuf())
6566 {}
6567
6568 // delete because of pointer members
6569 input_stream_adapter(const input_stream_adapter&) = delete;
6570 input_stream_adapter& operator=(input_stream_adapter&) = delete;
6571 input_stream_adapter& operator=(input_stream_adapter&&) = delete;
6572
6573 input_stream_adapter(input_stream_adapter&& rhs) noexcept
6574 : is(rhs.is), sb(rhs.sb)
6575 {
6576 rhs.is = nullptr;
6577 rhs.sb = nullptr;
6578 }
6579
6580 // std::istream/std::streambuf use std::char_traits<char>::to_int_type, to
6581 // ensure that std::char_traits<char>::eof() and the character 0xFF do not
6582 // end up as the same value, e.g. 0xFFFFFFFF.
6583 std::char_traits<char>::int_type get_character()
6584 {
6585 auto res = sb->sbumpc();
6586 // set eof manually, as we don't use the istream interface.
6587 if (JSON_HEDLEY_UNLIKELY(res == std::char_traits<char>::eof()))
6588 {
6589 is->clear(is->rdstate() | std::ios::eofbit);
6590 }
6591 return res;
6592 }
6593
6594 template<class T>
6595 std::size_t get_elements(T* dest, std::size_t count = 1)
6596 {
6597 auto res = static_cast<std::size_t>(sb->sgetn(reinterpret_cast<char*>(dest), static_cast<std::streamsize>(count * sizeof(T))));
6598 if (JSON_HEDLEY_UNLIKELY(res < count * sizeof(T)))
6599 {
6600 is->clear(is->rdstate() | std::ios::eofbit);
6601 }
6602 return res;
6603 }
6604
6605 private:
6606 /// the associated input stream
6607 std::istream* is = nullptr;
6608 std::streambuf* sb = nullptr;
6609 };
6610 #endif // JSON_NO_IO
6611
6612 // General-purpose iterator-based adapter. It might not be as fast as
6613 // theoretically possible for some containers, but it is extremely versatile.
6614 template<typename IteratorType>
6615 class iterator_input_adapter
6616 {
6617 public:
6618 using char_type = typename std::iterator_traits<IteratorType>::value_type;
6619
6620 iterator_input_adapter(IteratorType first, IteratorType last)
6621 : current(std::move(first)), end(std::move(last))
6622 {}
6623
6624 typename char_traits<char_type>::int_type get_character()
6625 {
6626 if (JSON_HEDLEY_LIKELY(current != end))
6627 {
6628 auto result = char_traits<char_type>::to_int_type(*current);
6629 std::advance(current, 1);
6630 return result;
6631 }
6632
6633 return char_traits<char_type>::eof();
6634 }
6635
6636 // for general iterators, we cannot really do something better than falling back to processing the range one-by-one
6637 template<class T>
6638 std::size_t get_elements(T* dest, std::size_t count = 1)
6639 {
6640 auto* ptr = reinterpret_cast<char*>(dest);
6641 for (std::size_t read_index = 0; read_index < count * sizeof(T); ++read_index)
6642 {
6643 if (JSON_HEDLEY_LIKELY(current != end))
6644 {
6645 ptr[read_index] = static_cast<char>(*current);
6646 std::advance(current, 1);
6647 }
6648 else
6649 {
6650 return read_index;
6651 }
6652 }
6653 return count * sizeof(T);
6654 }
6655
6656 private:
6657 IteratorType current;
6658 IteratorType end;
6659
6660 template<typename BaseInputAdapter, size_t T>
6661 friend struct wide_string_input_helper;
6662
6663 bool empty() const
6664 {
6665 return current == end;
6666 }
6667 };
6668
6669 template<typename BaseInputAdapter, size_t T>
6670 struct wide_string_input_helper;
6671
6672 template<typename BaseInputAdapter>
6673 struct wide_string_input_helper<BaseInputAdapter, 4>
6674 {
6675 // UTF-32
6676 static void fill_buffer(BaseInputAdapter& input,
6677 std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
6678 size_t& utf8_bytes_index,
6679 size_t& utf8_bytes_filled)
6680 {
6681 utf8_bytes_index = 0;
6682
6683 if (JSON_HEDLEY_UNLIKELY(input.empty()))
6684 {
6685 utf8_bytes[0] = std::char_traits<char>::eof();
6686 utf8_bytes_filled = 1;
6687 }
6688 else
6689 {
6690 // get the current character
6691 const auto wc = input.get_character();
6692
6693 // UTF-32 to UTF-8 encoding
6694 if (wc < 0x80)
6695 {
6696 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
6697 utf8_bytes_filled = 1;
6698 }
6699 else if (wc <= 0x7FF)
6700 {
6701 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((static_cast<unsigned int>(wc) >> 6u) & 0x1Fu));
6702 utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
6703 utf8_bytes_filled = 2;
6704 }
6705 else if (wc <= 0xFFFF)
6706 {
6707 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((static_cast<unsigned int>(wc) >> 12u) & 0x0Fu));
6708 utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
6709 utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
6710 utf8_bytes_filled = 3;
6711 }
6712 else if (wc <= 0x10FFFF)
6713 {
6714 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | ((static_cast<unsigned int>(wc) >> 18u) & 0x07u));
6715 utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 12u) & 0x3Fu));
6716 utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
6717 utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
6718 utf8_bytes_filled = 4;
6719 }
6720 else
6721 {
6722 // unknown character
6723 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
6724 utf8_bytes_filled = 1;
6725 }
6726 }
6727 }
6728 };
6729
6730 template<typename BaseInputAdapter>
6731 struct wide_string_input_helper<BaseInputAdapter, 2>
6732 {
6733 // UTF-16
6734 static void fill_buffer(BaseInputAdapter& input,
6735 std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
6736 size_t& utf8_bytes_index,
6737 size_t& utf8_bytes_filled)
6738 {
6739 utf8_bytes_index = 0;
6740
6741 if (JSON_HEDLEY_UNLIKELY(input.empty()))
6742 {
6743 utf8_bytes[0] = std::char_traits<char>::eof();
6744 utf8_bytes_filled = 1;
6745 }
6746 else
6747 {
6748 // get the current character
6749 const auto wc = input.get_character();
6750
6751 // UTF-16 to UTF-8 encoding
6752 if (wc < 0x80)
6753 {
6754 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
6755 utf8_bytes_filled = 1;
6756 }
6757 else if (wc <= 0x7FF)
6758 {
6759 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((static_cast<unsigned int>(wc) >> 6u)));
6760 utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
6761 utf8_bytes_filled = 2;
6762 }
6763 else if (0xD800 > wc || wc >= 0xE000)
6764 {
6765 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((static_cast<unsigned int>(wc) >> 12u)));
6766 utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
6767 utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
6768 utf8_bytes_filled = 3;
6769 }
6770 else
6771 {
6772 if (JSON_HEDLEY_UNLIKELY(!input.empty()))
6773 {
6774 const auto wc2 = static_cast<unsigned int>(input.get_character());
6775 const auto charcode = 0x10000u + (((static_cast<unsigned int>(wc) & 0x3FFu) << 10u) | (wc2 & 0x3FFu));
6776 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | (charcode >> 18u));
6777 utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 12u) & 0x3Fu));
6778 utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 6u) & 0x3Fu));
6779 utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (charcode & 0x3Fu));
6780 utf8_bytes_filled = 4;
6781 }
6782 else
6783 {
6784 utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
6785 utf8_bytes_filled = 1;
6786 }
6787 }
6788 }
6789 }
6790 };
6791
6792 // Wraps another input adapter to convert wide character types into individual bytes.
6793 template<typename BaseInputAdapter, typename WideCharType>
6794 class wide_string_input_adapter
6795 {
6796 public:
6797 using char_type = char;
6798
6799 wide_string_input_adapter(BaseInputAdapter base)
6800 : base_adapter(base) {}
6801
6802 typename std::char_traits<char>::int_type get_character() noexcept
6803 {
6804 // check if buffer needs to be filled
6805 if (utf8_bytes_index == utf8_bytes_filled)
6806 {
6807 fill_buffer<sizeof(WideCharType)>();
6808
6809 JSON_ASSERT(utf8_bytes_filled > 0);
6810 JSON_ASSERT(utf8_bytes_index == 0);
6811 }
6812
6813 // use buffer
6814 JSON_ASSERT(utf8_bytes_filled > 0);
6815 JSON_ASSERT(utf8_bytes_index < utf8_bytes_filled);
6816 return utf8_bytes[utf8_bytes_index++];
6817 }
6818
6819 // parsing binary with wchar doesn't make sense, but since the parsing mode can be runtime, we need something here
6820 template<class T>
6821 std::size_t get_elements(T* /*dest*/, std::size_t /*count*/ = 1)
6822 {
6823 JSON_THROW(parse_error::create(112, 1, "wide string type cannot be interpreted as binary data", nullptr));
6824 }
6825
6826 private:
6827 BaseInputAdapter base_adapter;
6828
6829 template<size_t T>
6830 void fill_buffer()
6831 {
6832 wide_string_input_helper<BaseInputAdapter, T>::fill_buffer(base_adapter, utf8_bytes, utf8_bytes_index, utf8_bytes_filled);
6833 }
6834
6835 /// a buffer for UTF-8 bytes
6836 std::array<std::char_traits<char>::int_type, 4> utf8_bytes = {{0, 0, 0, 0}};
6837
6838 /// index to the utf8_codes array for the next valid byte
6839 std::size_t utf8_bytes_index = 0;
6840 /// number of valid bytes in the utf8_codes array
6841 std::size_t utf8_bytes_filled = 0;
6842 };
6843
6844 template<typename IteratorType, typename Enable = void>
6845 struct iterator_input_adapter_factory
6846 {
6847 using iterator_type = IteratorType;
6848 using char_type = typename std::iterator_traits<iterator_type>::value_type;
6849 using adapter_type = iterator_input_adapter<iterator_type>;
6850
6851 static adapter_type create(IteratorType first, IteratorType last)
6852 {
6853 return adapter_type(std::move(first), std::move(last));
6854 }
6855 };
6856
6857 template<typename T>
6858 struct is_iterator_of_multibyte
6859 {
6860 using value_type = typename std::iterator_traits<T>::value_type;
6861 enum
6862 {
6863 value = sizeof(value_type) > 1
6864 };
6865 };
6866
6867 template<typename IteratorType>
6868 struct iterator_input_adapter_factory<IteratorType, enable_if_t<is_iterator_of_multibyte<IteratorType>::value>>
6869 {
6870 using iterator_type = IteratorType;
6871 using char_type = typename std::iterator_traits<iterator_type>::value_type;
6872 using base_adapter_type = iterator_input_adapter<iterator_type>;
6873 using adapter_type = wide_string_input_adapter<base_adapter_type, char_type>;
6874
6875 static adapter_type create(IteratorType first, IteratorType last)
6876 {
6877 return adapter_type(base_adapter_type(std::move(first), std::move(last)));
6878 }
6879 };
6880
6881 // General purpose iterator-based input
6882 template<typename IteratorType>
6883 typename iterator_input_adapter_factory<IteratorType>::adapter_type input_adapter(IteratorType first, IteratorType last)
6884 {
6885 using factory_type = iterator_input_adapter_factory<IteratorType>;
6886 return factory_type::create(first, last);
6887 }
6888
6889 // Convenience shorthand from container to iterator
6890 // Enables ADL on begin(container) and end(container)
6891 // Encloses the using declarations in namespace for not to leak them to outside scope
6892
6893 namespace container_input_adapter_factory_impl
6894 {
6895
6896 using std::begin;
6897 using std::end;
6898
6899 template<typename ContainerType, typename Enable = void>
6900 struct container_input_adapter_factory {};
6901
6902 template<typename ContainerType>
6903 struct container_input_adapter_factory< ContainerType,
6904 void_t<decltype(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>()))>>
6905 {
6906 using adapter_type = decltype(input_adapter(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>())));
6907
6908 static adapter_type create(const ContainerType& container)
6909 {
6910 return input_adapter(begin(container), end(container));
6911 }
6912 };
6913
6914 } // namespace container_input_adapter_factory_impl
6915
6916 template<typename ContainerType>
6917 typename container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::adapter_type input_adapter(const ContainerType& container)
6918 {
6919 return container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::create(container);
6920 }
6921
6922 // specialization for std::string
6923 using string_input_adapter_type = decltype(input_adapter(std::declval<std::string>()));
6924
6925 #ifndef JSON_NO_IO
6926 // Special cases with fast paths
6927 inline file_input_adapter input_adapter(std::FILE* file)
6928 {
6929 if (file == nullptr)
6930 {
6931 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));
6932 }
6933 return file_input_adapter(file);
6934 }
6935
6936 inline input_stream_adapter input_adapter(std::istream& stream)
6937 {
6938 return input_stream_adapter(stream);
6939 }
6940
6941 inline input_stream_adapter input_adapter(std::istream&& stream)
6942 {
6943 return input_stream_adapter(stream);
6944 }
6945 #endif // JSON_NO_IO
6946
6947 using contiguous_bytes_input_adapter = decltype(input_adapter(std::declval<const char*>(), std::declval<const char*>()));
6948
6949 // Null-delimited strings, and the like.
6950 template < typename CharT,
6951 typename std::enable_if <
6952 std::is_pointer<CharT>::value&&
6953 !std::is_array<CharT>::value&&
6954 std::is_integral<typename std::remove_pointer<CharT>::type>::value&&
6955 sizeof(typename std::remove_pointer<CharT>::type) == 1,
6956 int >::type = 0 >
6957 contiguous_bytes_input_adapter input_adapter(CharT b)
6958 {
6959 if (b == nullptr)
6960 {
6961 JSON_THROW(parse_error::create(101, 0, "attempting to parse an empty input; check that your input string or stream contains the expected JSON", nullptr));
6962 }
6963 auto length = std::strlen(reinterpret_cast<const char*>(b));
6964 const auto* ptr = reinterpret_cast<const char*>(b);
6965 return input_adapter(ptr, ptr + length); // cppcheck-suppress[nullPointerArithmeticRedundantCheck]
6966 }
6967
6968 template<typename T, std::size_t N>
6969 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)
6970 {
6971 return input_adapter(array, array + N);
6972 }
6973
6974 // This class only handles inputs of input_buffer_adapter type.
6975 // It's required so that expressions like {ptr, len} can be implicitly cast
6976 // to the correct adapter.
6977 class span_input_adapter
6978 {
6979 public:
6980 template < typename CharT,
6981 typename std::enable_if <
6982 std::is_pointer<CharT>::value&&
6983 std::is_integral<typename std::remove_pointer<CharT>::type>::value&&
6984 sizeof(typename std::remove_pointer<CharT>::type) == 1,
6985 int >::type = 0 >
6986 span_input_adapter(CharT b, std::size_t l)
6987 : ia(reinterpret_cast<const char*>(b), reinterpret_cast<const char*>(b) + l) {}
6988
6989 template<class IteratorType,
6990 typename std::enable_if<
6991 std::is_same<typename iterator_traits<IteratorType>::iterator_category, std::random_access_iterator_tag>::value,
6992 int>::type = 0>
6993 span_input_adapter(IteratorType first, IteratorType last)
6994 : ia(input_adapter(first, last)) {}
6995
6996 contiguous_bytes_input_adapter&& get()
6997 {
6998 return std::move(ia); // NOLINT(hicpp-move-const-arg,performance-move-const-arg)
6999 }
7000
7001 private:
7002 contiguous_bytes_input_adapter ia;
7003 };
7004
7005 } // namespace detail
7006 NLOHMANN_JSON_NAMESPACE_END
7007
7008 // #include <nlohmann/detail/input/json_sax.hpp>
7009 // __ _____ _____ _____
7010 // __| | __| | | | JSON for Modern C++
7011 // | | |__ | | | | | | version 3.12.0
7012 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
7013 //
7014 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
7015 // SPDX-License-Identifier: MIT
7016
7017
7018
7019 #include <cstddef>
7020 #include <string> // string
7021 #include <type_traits> // enable_if_t
7022 #include <utility> // move
7023 #include <vector> // vector
7024
7025 // #include <nlohmann/detail/exceptions.hpp>
7026
7027 // #include <nlohmann/detail/input/lexer.hpp>
7028 // __ _____ _____ _____
7029 // __| | __| | | | JSON for Modern C++
7030 // | | |__ | | | | | | version 3.12.0
7031 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
7032 //
7033 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
7034 // SPDX-License-Identifier: MIT
7035
7036
7037
7038 #include <array> // array
7039 #include <clocale> // localeconv
7040 #include <cstddef> // size_t
7041 #include <cstdio> // snprintf
7042 #include <cstdlib> // strtof, strtod, strtold, strtoll, strtoull
7043 #include <initializer_list> // initializer_list
7044 #include <string> // char_traits, string
7045 #include <utility> // move
7046 #include <vector> // vector
7047
7048 // #include <nlohmann/detail/input/input_adapters.hpp>
7049
7050 // #include <nlohmann/detail/input/position_t.hpp>
7051
7052 // #include <nlohmann/detail/macro_scope.hpp>
7053
7054 // #include <nlohmann/detail/meta/type_traits.hpp>
7055
7056
7057 NLOHMANN_JSON_NAMESPACE_BEGIN
7058 namespace detail
7059 {
7060
7061 ///////////
7062 // lexer //
7063 ///////////
7064
7065 template<typename BasicJsonType>
7066 class lexer_base
7067 {
7068 public:
7069 /// token types for the parser
7070 enum class token_type
7071 {
7072 uninitialized, ///< indicating the scanner is uninitialized
7073 literal_true, ///< the `true` literal
7074 literal_false, ///< the `false` literal
7075 literal_null, ///< the `null` literal
7076 value_string, ///< a string -- use get_string() for actual value
7077 value_unsigned, ///< an unsigned integer -- use get_number_unsigned() for actual value
7078 value_integer, ///< a signed integer -- use get_number_integer() for actual value
7079 value_float, ///< an floating point number -- use get_number_float() for actual value
7080 begin_array, ///< the character for array begin `[`
7081 begin_object, ///< the character for object begin `{`
7082 end_array, ///< the character for array end `]`
7083 end_object, ///< the character for object end `}`
7084 name_separator, ///< the name separator `:`
7085 value_separator, ///< the value separator `,`
7086 parse_error, ///< indicating a parse error
7087 end_of_input, ///< indicating the end of the input buffer
7088 literal_or_value ///< a literal or the begin of a value (only for diagnostics)
7089 };
7090
7091 /// return name of values of type token_type (only used for errors)
7092 JSON_HEDLEY_RETURNS_NON_NULL
7093 JSON_HEDLEY_CONST
7094 static const char* token_type_name(const token_type t) noexcept
7095 {
7096 switch (t)
7097 {
7098 case token_type::uninitialized:
7099 return "<uninitialized>";
7100 case token_type::literal_true:
7101 return "true literal";
7102 case token_type::literal_false:
7103 return "false literal";
7104 case token_type::literal_null:
7105 return "null literal";
7106 case token_type::value_string:
7107 return "string literal";
7108 case token_type::value_unsigned:
7109 case token_type::value_integer:
7110 case token_type::value_float:
7111 return "number literal";
7112 case token_type::begin_array:
7113 return "'['";
7114 case token_type::begin_object:
7115 return "'{'";
7116 case token_type::end_array:
7117 return "']'";
7118 case token_type::end_object:
7119 return "'}'";
7120 case token_type::name_separator:
7121 return "':'";
7122 case token_type::value_separator:
7123 return "','";
7124 case token_type::parse_error:
7125 return "<parse error>";
7126 case token_type::end_of_input:
7127 return "end of input";
7128 case token_type::literal_or_value:
7129 return "'[', '{', or a literal";
7130 // LCOV_EXCL_START
7131 default: // catch non-enum values
7132 return "unknown token";
7133 // LCOV_EXCL_STOP
7134 }
7135 }
7136 };
7137 /*!
7138 @brief lexical analysis
7139
7140 This class organizes the lexical analysis during JSON deserialization.
7141 */
7142 template<typename BasicJsonType, typename InputAdapterType>
7143 class lexer : public lexer_base<BasicJsonType>
7144 {
7145 using number_integer_t = typename BasicJsonType::number_integer_t;
7146 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
7147 using number_float_t = typename BasicJsonType::number_float_t;
7148 using string_t = typename BasicJsonType::string_t;
7149 using char_type = typename InputAdapterType::char_type;
7150 using char_int_type = typename char_traits<char_type>::int_type;
7151
7152 public:
7153 using token_type = typename lexer_base<BasicJsonType>::token_type;
7154
7155 explicit lexer(InputAdapterType&& adapter, bool ignore_comments_ = false) noexcept
7156 : ia(std::move(adapter))
7157 , ignore_comments(ignore_comments_)
7158 , decimal_point_char(static_cast<char_int_type>(get_decimal_point()))
7159 {}
7160
7161 // delete because of pointer members
7162 lexer(const lexer&) = delete;
7163 lexer(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
7164 lexer& operator=(lexer&) = delete;
7165 lexer& operator=(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
7166 ~lexer() = default;
7167
7168 private:
7169 /////////////////////
7170 // locales
7171 /////////////////////
7172
7173 /// return the locale-dependent decimal point
7174 JSON_HEDLEY_PURE
7175 static char get_decimal_point() noexcept
7176 {
7177 const auto* loc = localeconv();
7178 JSON_ASSERT(loc != nullptr);
7179 return (loc->decimal_point == nullptr) ? '.' : *(loc->decimal_point);
7180 }
7181
7182 /////////////////////
7183 // scan functions
7184 /////////////////////
7185
7186 /*!
7187 @brief get codepoint from 4 hex characters following `\u`
7188
7189 For input "\u c1 c2 c3 c4" the codepoint is:
7190 (c1 * 0x1000) + (c2 * 0x0100) + (c3 * 0x0010) + c4
7191 = (c1 << 12) + (c2 << 8) + (c3 << 4) + (c4 << 0)
7192
7193 Furthermore, the possible characters '0'..'9', 'A'..'F', and 'a'..'f'
7194 must be converted to the integers 0x0..0x9, 0xA..0xF, 0xA..0xF, resp. The
7195 conversion is done by subtracting the offset (0x30, 0x37, and 0x57)
7196 between the ASCII value of the character and the desired integer value.
7197
7198 @return codepoint (0x0000..0xFFFF) or -1 in case of an error (e.g. EOF or
7199 non-hex character)
7200 */
7201 int get_codepoint()
7202 {
7203 // this function only makes sense after reading `\u`
7204 JSON_ASSERT(current == 'u');
7205 int codepoint = 0;
7206
7207 const auto factors = { 12u, 8u, 4u, 0u };
7208 for (const auto factor : factors)
7209 {
7210 get();
7211
7212 if (current >= '0' && current <= '9')
7213 {
7214 codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x30u) << factor);
7215 }
7216 else if (current >= 'A' && current <= 'F')
7217 {
7218 codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x37u) << factor);
7219 }
7220 else if (current >= 'a' && current <= 'f')
7221 {
7222 codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x57u) << factor);
7223 }
7224 else
7225 {
7226 return -1;
7227 }
7228 }
7229
7230 JSON_ASSERT(0x0000 <= codepoint && codepoint <= 0xFFFF);
7231 return codepoint;
7232 }
7233
7234 /*!
7235 @brief check if the next byte(s) are inside a given range
7236
7237 Adds the current byte and, for each passed range, reads a new byte and
7238 checks if it is inside the range. If a violation was detected, set up an
7239 error message and return false. Otherwise, return true.
7240
7241 @param[in] ranges list of integers; interpreted as list of pairs of
7242 inclusive lower and upper bound, respectively
7243
7244 @pre The passed list @a ranges must have 2, 4, or 6 elements; that is,
7245 1, 2, or 3 pairs. This precondition is enforced by an assertion.
7246
7247 @return true if and only if no range violation was detected
7248 */
7249 bool next_byte_in_range(std::initializer_list<char_int_type> ranges)
7250 {
7251 JSON_ASSERT(ranges.size() == 2 || ranges.size() == 4 || ranges.size() == 6);
7252 add(current);
7253
7254 for (auto range = ranges.begin(); range != ranges.end(); ++range)
7255 {
7256 get();
7257 if (JSON_HEDLEY_LIKELY(*range <= current && current <= *(++range))) // NOLINT(bugprone-inc-dec-in-conditions)
7258 {
7259 add(current);
7260 }
7261 else
7262 {
7263 error_message = "invalid string: ill-formed UTF-8 byte";
7264 return false;
7265 }
7266 }
7267
7268 return true;
7269 }
7270
7271 /*!
7272 @brief scan a string literal
7273
7274 This function scans a string according to Sect. 7 of RFC 8259. While
7275 scanning, bytes are escaped and copied into buffer token_buffer. Then the
7276 function returns successfully, token_buffer is *not* null-terminated (as it
7277 may contain \0 bytes), and token_buffer.size() is the number of bytes in the
7278 string.
7279
7280 @return token_type::value_string if string could be successfully scanned,
7281 token_type::parse_error otherwise
7282
7283 @note In case of errors, variable error_message contains a textual
7284 description.
7285 */
7286 token_type scan_string()
7287 {
7288 // reset token_buffer (ignore opening quote)
7289 reset();
7290
7291 // we entered the function by reading an open quote
7292 JSON_ASSERT(current == '\"');
7293
7294 while (true)
7295 {
7296 // get next character
7297 switch (get())
7298 {
7299 // end of file while parsing string
7300 case char_traits<char_type>::eof():
7301 {
7302 error_message = "invalid string: missing closing quote";
7303 return token_type::parse_error;
7304 }
7305
7306 // closing quote
7307 case '\"':
7308 {
7309 return token_type::value_string;
7310 }
7311
7312 // escapes
7313 case '\\':
7314 {
7315 switch (get())
7316 {
7317 // quotation mark
7318 case '\"':
7319 add('\"');
7320 break;
7321 // reverse solidus
7322 case '\\':
7323 add('\\');
7324 break;
7325 // solidus
7326 case '/':
7327 add('/');
7328 break;
7329 // backspace
7330 case 'b':
7331 add('\b');
7332 break;
7333 // form feed
7334 case 'f':
7335 add('\f');
7336 break;
7337 // line feed
7338 case 'n':
7339 add('\n');
7340 break;
7341 // carriage return
7342 case 'r':
7343 add('\r');
7344 break;
7345 // tab
7346 case 't':
7347 add('\t');
7348 break;
7349
7350 // unicode escapes
7351 case 'u':
7352 {
7353 const int codepoint1 = get_codepoint();
7354 int codepoint = codepoint1; // start with codepoint1
7355
7356 if (JSON_HEDLEY_UNLIKELY(codepoint1 == -1))
7357 {
7358 error_message = "invalid string: '\\u' must be followed by 4 hex digits";
7359 return token_type::parse_error;
7360 }
7361
7362 // check if code point is a high surrogate
7363 if (0xD800 <= codepoint1 && codepoint1 <= 0xDBFF)
7364 {
7365 // expect next \uxxxx entry
7366 if (JSON_HEDLEY_LIKELY(get() == '\\' && get() == 'u'))
7367 {
7368 const int codepoint2 = get_codepoint();
7369
7370 if (JSON_HEDLEY_UNLIKELY(codepoint2 == -1))
7371 {
7372 error_message = "invalid string: '\\u' must be followed by 4 hex digits";
7373 return token_type::parse_error;
7374 }
7375
7376 // check if codepoint2 is a low surrogate
7377 if (JSON_HEDLEY_LIKELY(0xDC00 <= codepoint2 && codepoint2 <= 0xDFFF))
7378 {
7379 // overwrite codepoint
7380 codepoint = static_cast<int>(
7381 // high surrogate occupies the most significant 22 bits
7382 (static_cast<unsigned int>(codepoint1) << 10u)
7383 // low surrogate occupies the least significant 15 bits
7384 + static_cast<unsigned int>(codepoint2)
7385 // there is still the 0xD800, 0xDC00 and 0x10000 noise
7386 // in the result, so we have to subtract with:
7387 // (0xD800 << 10) + DC00 - 0x10000 = 0x35FDC00
7388 - 0x35FDC00u);
7389 }
7390 else
7391 {
7392 error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF";
7393 return token_type::parse_error;
7394 }
7395 }
7396 else
7397 {
7398 error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF";
7399 return token_type::parse_error;
7400 }
7401 }
7402 else
7403 {
7404 if (JSON_HEDLEY_UNLIKELY(0xDC00 <= codepoint1 && codepoint1 <= 0xDFFF))
7405 {
7406 error_message = "invalid string: surrogate U+DC00..U+DFFF must follow U+D800..U+DBFF";
7407 return token_type::parse_error;
7408 }
7409 }
7410
7411 // result of the above calculation yields a proper codepoint
7412 JSON_ASSERT(0x00 <= codepoint && codepoint <= 0x10FFFF);
7413
7414 // translate codepoint into bytes
7415 if (codepoint < 0x80)
7416 {
7417 // 1-byte characters: 0xxxxxxx (ASCII)
7418 add(static_cast<char_int_type>(codepoint));
7419 }
7420 else if (codepoint <= 0x7FF)
7421 {
7422 // 2-byte characters: 110xxxxx 10xxxxxx
7423 add(static_cast<char_int_type>(0xC0u | (static_cast<unsigned int>(codepoint) >> 6u)));
7424 add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
7425 }
7426 else if (codepoint <= 0xFFFF)
7427 {
7428 // 3-byte characters: 1110xxxx 10xxxxxx 10xxxxxx
7429 add(static_cast<char_int_type>(0xE0u | (static_cast<unsigned int>(codepoint) >> 12u)));
7430 add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
7431 add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
7432 }
7433 else
7434 {
7435 // 4-byte characters: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
7436 add(static_cast<char_int_type>(0xF0u | (static_cast<unsigned int>(codepoint) >> 18u)));
7437 add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 12u) & 0x3Fu)));
7438 add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
7439 add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
7440 }
7441
7442 break;
7443 }
7444
7445 // other characters after escape
7446 default:
7447 error_message = "invalid string: forbidden character after backslash";
7448 return token_type::parse_error;
7449 }
7450
7451 break;
7452 }
7453
7454 // invalid control characters
7455 case 0x00:
7456 {
7457 error_message = "invalid string: control character U+0000 (NUL) must be escaped to \\u0000";
7458 return token_type::parse_error;
7459 }
7460
7461 case 0x01:
7462 {
7463 error_message = "invalid string: control character U+0001 (SOH) must be escaped to \\u0001";
7464 return token_type::parse_error;
7465 }
7466
7467 case 0x02:
7468 {
7469 error_message = "invalid string: control character U+0002 (STX) must be escaped to \\u0002";
7470 return token_type::parse_error;
7471 }
7472
7473 case 0x03:
7474 {
7475 error_message = "invalid string: control character U+0003 (ETX) must be escaped to \\u0003";
7476 return token_type::parse_error;
7477 }
7478
7479 case 0x04:
7480 {
7481 error_message = "invalid string: control character U+0004 (EOT) must be escaped to \\u0004";
7482 return token_type::parse_error;
7483 }
7484
7485 case 0x05:
7486 {
7487 error_message = "invalid string: control character U+0005 (ENQ) must be escaped to \\u0005";
7488 return token_type::parse_error;
7489 }
7490
7491 case 0x06:
7492 {
7493 error_message = "invalid string: control character U+0006 (ACK) must be escaped to \\u0006";
7494 return token_type::parse_error;
7495 }
7496
7497 case 0x07:
7498 {
7499 error_message = "invalid string: control character U+0007 (BEL) must be escaped to \\u0007";
7500 return token_type::parse_error;
7501 }
7502
7503 case 0x08:
7504 {
7505 error_message = "invalid string: control character U+0008 (BS) must be escaped to \\u0008 or \\b";
7506 return token_type::parse_error;
7507 }
7508
7509 case 0x09:
7510 {
7511 error_message = "invalid string: control character U+0009 (HT) must be escaped to \\u0009 or \\t";
7512 return token_type::parse_error;
7513 }
7514
7515 case 0x0A:
7516 {
7517 error_message = "invalid string: control character U+000A (LF) must be escaped to \\u000A or \\n";
7518 return token_type::parse_error;
7519 }
7520
7521 case 0x0B:
7522 {
7523 error_message = "invalid string: control character U+000B (VT) must be escaped to \\u000B";
7524 return token_type::parse_error;
7525 }
7526
7527 case 0x0C:
7528 {
7529 error_message = "invalid string: control character U+000C (FF) must be escaped to \\u000C or \\f";
7530 return token_type::parse_error;
7531 }
7532
7533 case 0x0D:
7534 {
7535 error_message = "invalid string: control character U+000D (CR) must be escaped to \\u000D or \\r";
7536 return token_type::parse_error;
7537 }
7538
7539 case 0x0E:
7540 {
7541 error_message = "invalid string: control character U+000E (SO) must be escaped to \\u000E";
7542 return token_type::parse_error;
7543 }
7544
7545 case 0x0F:
7546 {
7547 error_message = "invalid string: control character U+000F (SI) must be escaped to \\u000F";
7548 return token_type::parse_error;
7549 }
7550
7551 case 0x10:
7552 {
7553 error_message = "invalid string: control character U+0010 (DLE) must be escaped to \\u0010";
7554 return token_type::parse_error;
7555 }
7556
7557 case 0x11:
7558 {
7559 error_message = "invalid string: control character U+0011 (DC1) must be escaped to \\u0011";
7560 return token_type::parse_error;
7561 }
7562
7563 case 0x12:
7564 {
7565 error_message = "invalid string: control character U+0012 (DC2) must be escaped to \\u0012";
7566 return token_type::parse_error;
7567 }
7568
7569 case 0x13:
7570 {
7571 error_message = "invalid string: control character U+0013 (DC3) must be escaped to \\u0013";
7572 return token_type::parse_error;
7573 }
7574
7575 case 0x14:
7576 {
7577 error_message = "invalid string: control character U+0014 (DC4) must be escaped to \\u0014";
7578 return token_type::parse_error;
7579 }
7580
7581 case 0x15:
7582 {
7583 error_message = "invalid string: control character U+0015 (NAK) must be escaped to \\u0015";
7584 return token_type::parse_error;
7585 }
7586
7587 case 0x16:
7588 {
7589 error_message = "invalid string: control character U+0016 (SYN) must be escaped to \\u0016";
7590 return token_type::parse_error;
7591 }
7592
7593 case 0x17:
7594 {
7595 error_message = "invalid string: control character U+0017 (ETB) must be escaped to \\u0017";
7596 return token_type::parse_error;
7597 }
7598
7599 case 0x18:
7600 {
7601 error_message = "invalid string: control character U+0018 (CAN) must be escaped to \\u0018";
7602 return token_type::parse_error;
7603 }
7604
7605 case 0x19:
7606 {
7607 error_message = "invalid string: control character U+0019 (EM) must be escaped to \\u0019";
7608 return token_type::parse_error;
7609 }
7610
7611 case 0x1A:
7612 {
7613 error_message = "invalid string: control character U+001A (SUB) must be escaped to \\u001A";
7614 return token_type::parse_error;
7615 }
7616
7617 case 0x1B:
7618 {
7619 error_message = "invalid string: control character U+001B (ESC) must be escaped to \\u001B";
7620 return token_type::parse_error;
7621 }
7622
7623 case 0x1C:
7624 {
7625 error_message = "invalid string: control character U+001C (FS) must be escaped to \\u001C";
7626 return token_type::parse_error;
7627 }
7628
7629 case 0x1D:
7630 {
7631 error_message = "invalid string: control character U+001D (GS) must be escaped to \\u001D";
7632 return token_type::parse_error;
7633 }
7634
7635 case 0x1E:
7636 {
7637 error_message = "invalid string: control character U+001E (RS) must be escaped to \\u001E";
7638 return token_type::parse_error;
7639 }
7640
7641 case 0x1F:
7642 {
7643 error_message = "invalid string: control character U+001F (US) must be escaped to \\u001F";
7644 return token_type::parse_error;
7645 }
7646
7647 // U+0020..U+007F (except U+0022 (quote) and U+005C (backspace))
7648 case 0x20:
7649 case 0x21:
7650 case 0x23:
7651 case 0x24:
7652 case 0x25:
7653 case 0x26:
7654 case 0x27:
7655 case 0x28:
7656 case 0x29:
7657 case 0x2A:
7658 case 0x2B:
7659 case 0x2C:
7660 case 0x2D:
7661 case 0x2E:
7662 case 0x2F:
7663 case 0x30:
7664 case 0x31:
7665 case 0x32:
7666 case 0x33:
7667 case 0x34:
7668 case 0x35:
7669 case 0x36:
7670 case 0x37:
7671 case 0x38:
7672 case 0x39:
7673 case 0x3A:
7674 case 0x3B:
7675 case 0x3C:
7676 case 0x3D:
7677 case 0x3E:
7678 case 0x3F:
7679 case 0x40:
7680 case 0x41:
7681 case 0x42:
7682 case 0x43:
7683 case 0x44:
7684 case 0x45:
7685 case 0x46:
7686 case 0x47:
7687 case 0x48:
7688 case 0x49:
7689 case 0x4A:
7690 case 0x4B:
7691 case 0x4C:
7692 case 0x4D:
7693 case 0x4E:
7694 case 0x4F:
7695 case 0x50:
7696 case 0x51:
7697 case 0x52:
7698 case 0x53:
7699 case 0x54:
7700 case 0x55:
7701 case 0x56:
7702 case 0x57:
7703 case 0x58:
7704 case 0x59:
7705 case 0x5A:
7706 case 0x5B:
7707 case 0x5D:
7708 case 0x5E:
7709 case 0x5F:
7710 case 0x60:
7711 case 0x61:
7712 case 0x62:
7713 case 0x63:
7714 case 0x64:
7715 case 0x65:
7716 case 0x66:
7717 case 0x67:
7718 case 0x68:
7719 case 0x69:
7720 case 0x6A:
7721 case 0x6B:
7722 case 0x6C:
7723 case 0x6D:
7724 case 0x6E:
7725 case 0x6F:
7726 case 0x70:
7727 case 0x71:
7728 case 0x72:
7729 case 0x73:
7730 case 0x74:
7731 case 0x75:
7732 case 0x76:
7733 case 0x77:
7734 case 0x78:
7735 case 0x79:
7736 case 0x7A:
7737 case 0x7B:
7738 case 0x7C:
7739 case 0x7D:
7740 case 0x7E:
7741 case 0x7F:
7742 {
7743 add(current);
7744 break;
7745 }
7746
7747 // U+0080..U+07FF: bytes C2..DF 80..BF
7748 case 0xC2:
7749 case 0xC3:
7750 case 0xC4:
7751 case 0xC5:
7752 case 0xC6:
7753 case 0xC7:
7754 case 0xC8:
7755 case 0xC9:
7756 case 0xCA:
7757 case 0xCB:
7758 case 0xCC:
7759 case 0xCD:
7760 case 0xCE:
7761 case 0xCF:
7762 case 0xD0:
7763 case 0xD1:
7764 case 0xD2:
7765 case 0xD3:
7766 case 0xD4:
7767 case 0xD5:
7768 case 0xD6:
7769 case 0xD7:
7770 case 0xD8:
7771 case 0xD9:
7772 case 0xDA:
7773 case 0xDB:
7774 case 0xDC:
7775 case 0xDD:
7776 case 0xDE:
7777 case 0xDF:
7778 {
7779 if (JSON_HEDLEY_UNLIKELY(!next_byte_in_range({0x80, 0xBF})))
7780 {
7781 return token_type::parse_error;
7782 }
7783 break;
7784 }
7785
7786 // U+0800..U+0FFF: bytes E0 A0..BF 80..BF
7787 case 0xE0:
7788 {
7789 if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0xA0, 0xBF, 0x80, 0xBF}))))
7790 {
7791 return token_type::parse_error;
7792 }
7793 break;
7794 }
7795
7796 // U+1000..U+CFFF: bytes E1..EC 80..BF 80..BF
7797 // U+E000..U+FFFF: bytes EE..EF 80..BF 80..BF
7798 case 0xE1:
7799 case 0xE2:
7800 case 0xE3:
7801 case 0xE4:
7802 case 0xE5:
7803 case 0xE6:
7804 case 0xE7:
7805 case 0xE8:
7806 case 0xE9:
7807 case 0xEA:
7808 case 0xEB:
7809 case 0xEC:
7810 case 0xEE:
7811 case 0xEF:
7812 {
7813 if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF}))))
7814 {
7815 return token_type::parse_error;
7816 }
7817 break;
7818 }
7819
7820 // U+D000..U+D7FF: bytes ED 80..9F 80..BF
7821 case 0xED:
7822 {
7823 if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x9F, 0x80, 0xBF}))))
7824 {
7825 return token_type::parse_error;
7826 }
7827 break;
7828 }
7829
7830 // U+10000..U+3FFFF F0 90..BF 80..BF 80..BF
7831 case 0xF0:
7832 {
7833 if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x90, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
7834 {
7835 return token_type::parse_error;
7836 }
7837 break;
7838 }
7839
7840 // U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF
7841 case 0xF1:
7842 case 0xF2:
7843 case 0xF3:
7844 {
7845 if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
7846 {
7847 return token_type::parse_error;
7848 }
7849 break;
7850 }
7851
7852 // U+100000..U+10FFFF F4 80..8F 80..BF 80..BF
7853 case 0xF4:
7854 {
7855 if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x8F, 0x80, 0xBF, 0x80, 0xBF}))))
7856 {
7857 return token_type::parse_error;
7858 }
7859 break;
7860 }
7861
7862 // remaining bytes (80..C1 and F5..FF) are ill-formed
7863 default:
7864 {
7865 error_message = "invalid string: ill-formed UTF-8 byte";
7866 return token_type::parse_error;
7867 }
7868 }
7869 }
7870 }
7871
7872 /*!
7873 * @brief scan a comment
7874 * @return whether comment could be scanned successfully
7875 */
7876 bool scan_comment()
7877 {
7878 switch (get())
7879 {
7880 // single-line comments skip input until a newline or EOF is read
7881 case '/':
7882 {
7883 while (true)
7884 {
7885 switch (get())
7886 {
7887 case '\n':
7888 case '\r':
7889 case char_traits<char_type>::eof():
7890 case '\0':
7891 return true;
7892
7893 default:
7894 break;
7895 }
7896 }
7897 }
7898
7899 // multi-line comments skip input until */ is read
7900 case '*':
7901 {
7902 while (true)
7903 {
7904 switch (get())
7905 {
7906 case char_traits<char_type>::eof():
7907 case '\0':
7908 {
7909 error_message = "invalid comment; missing closing '*/'";
7910 return false;
7911 }
7912
7913 case '*':
7914 {
7915 switch (get())
7916 {
7917 case '/':
7918 return true;
7919
7920 default:
7921 {
7922 unget();
7923 continue;
7924 }
7925 }
7926 }
7927
7928 default:
7929 continue;
7930 }
7931 }
7932 }
7933
7934 // unexpected character after reading '/'
7935 default:
7936 {
7937 error_message = "invalid comment; expecting '/' or '*' after '/'";
7938 return false;
7939 }
7940 }
7941 }
7942
7943 JSON_HEDLEY_NON_NULL(2)
7944 static void strtof(float& f, const char* str, char** endptr) noexcept
7945 {
7946 f = std::strtof(str, endptr);
7947 }
7948
7949 JSON_HEDLEY_NON_NULL(2)
7950 static void strtof(double& f, const char* str, char** endptr) noexcept
7951 {
7952 f = std::strtod(str, endptr);
7953 }
7954
7955 JSON_HEDLEY_NON_NULL(2)
7956 static void strtof(long double& f, const char* str, char** endptr) noexcept
7957 {
7958 f = std::strtold(str, endptr);
7959 }
7960
7961 /*!
7962 @brief scan a number literal
7963
7964 This function scans a string according to Sect. 6 of RFC 8259.
7965
7966 The function is realized with a deterministic finite state machine derived
7967 from the grammar described in RFC 8259. Starting in state "init", the
7968 input is read and used to determined the next state. Only state "done"
7969 accepts the number. State "error" is a trap state to model errors. In the
7970 table below, "anything" means any character but the ones listed before.
7971
7972 state | 0 | 1-9 | e E | + | - | . | anything
7973 ---------|----------|----------|----------|---------|---------|----------|-----------
7974 init | zero | any1 | [error] | [error] | minus | [error] | [error]
7975 minus | zero | any1 | [error] | [error] | [error] | [error] | [error]
7976 zero | done | done | exponent | done | done | decimal1 | done
7977 any1 | any1 | any1 | exponent | done | done | decimal1 | done
7978 decimal1 | decimal2 | decimal2 | [error] | [error] | [error] | [error] | [error]
7979 decimal2 | decimal2 | decimal2 | exponent | done | done | done | done
7980 exponent | any2 | any2 | [error] | sign | sign | [error] | [error]
7981 sign | any2 | any2 | [error] | [error] | [error] | [error] | [error]
7982 any2 | any2 | any2 | done | done | done | done | done
7983
7984 The state machine is realized with one label per state (prefixed with
7985 "scan_number_") and `goto` statements between them. The state machine
7986 contains cycles, but any cycle can be left when EOF is read. Therefore,
7987 the function is guaranteed to terminate.
7988
7989 During scanning, the read bytes are stored in token_buffer. This string is
7990 then converted to a signed integer, an unsigned integer, or a
7991 floating-point number.
7992
7993 @return token_type::value_unsigned, token_type::value_integer, or
7994 token_type::value_float if number could be successfully scanned,
7995 token_type::parse_error otherwise
7996
7997 @note The scanner is independent of the current locale. Internally, the
7998 locale's decimal point is used instead of `.` to work with the
7999 locale-dependent converters.
8000 */
8001 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.
8002 {
8003 // reset token_buffer to store the number's bytes
8004 reset();
8005
8006 // the type of the parsed number; initially set to unsigned; will be
8007 // changed if minus sign, decimal point or exponent is read
8008 token_type number_type = token_type::value_unsigned;
8009
8010 // state (init): we just found out we need to scan a number
8011 switch (current)
8012 {
8013 case '-':
8014 {
8015 add(current);
8016 goto scan_number_minus;
8017 }
8018
8019 case '0':
8020 {
8021 add(current);
8022 goto scan_number_zero;
8023 }
8024
8025 case '1':
8026 case '2':
8027 case '3':
8028 case '4':
8029 case '5':
8030 case '6':
8031 case '7':
8032 case '8':
8033 case '9':
8034 {
8035 add(current);
8036 goto scan_number_any1;
8037 }
8038
8039 // all other characters are rejected outside scan_number()
8040 default: // LCOV_EXCL_LINE
8041 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
8042 }
8043
8044 scan_number_minus:
8045 // state: we just parsed a leading minus sign
8046 number_type = token_type::value_integer;
8047 switch (get())
8048 {
8049 case '0':
8050 {
8051 add(current);
8052 goto scan_number_zero;
8053 }
8054
8055 case '1':
8056 case '2':
8057 case '3':
8058 case '4':
8059 case '5':
8060 case '6':
8061 case '7':
8062 case '8':
8063 case '9':
8064 {
8065 add(current);
8066 goto scan_number_any1;
8067 }
8068
8069 default:
8070 {
8071 error_message = "invalid number; expected digit after '-'";
8072 return token_type::parse_error;
8073 }
8074 }
8075
8076 scan_number_zero:
8077 // state: we just parse a zero (maybe with a leading minus sign)
8078 switch (get())
8079 {
8080 case '.':
8081 {
8082 add(decimal_point_char);
8083 decimal_point_position = token_buffer.size() - 1;
8084 goto scan_number_decimal1;
8085 }
8086
8087 case 'e':
8088 case 'E':
8089 {
8090 add(current);
8091 goto scan_number_exponent;
8092 }
8093
8094 default:
8095 goto scan_number_done;
8096 }
8097
8098 scan_number_any1:
8099 // state: we just parsed a number 0-9 (maybe with a leading minus sign)
8100 switch (get())
8101 {
8102 case '0':
8103 case '1':
8104 case '2':
8105 case '3':
8106 case '4':
8107 case '5':
8108 case '6':
8109 case '7':
8110 case '8':
8111 case '9':
8112 {
8113 add(current);
8114 goto scan_number_any1;
8115 }
8116
8117 case '.':
8118 {
8119 add(decimal_point_char);
8120 decimal_point_position = token_buffer.size() - 1;
8121 goto scan_number_decimal1;
8122 }
8123
8124 case 'e':
8125 case 'E':
8126 {
8127 add(current);
8128 goto scan_number_exponent;
8129 }
8130
8131 default:
8132 goto scan_number_done;
8133 }
8134
8135 scan_number_decimal1:
8136 // state: we just parsed a decimal point
8137 number_type = token_type::value_float;
8138 switch (get())
8139 {
8140 case '0':
8141 case '1':
8142 case '2':
8143 case '3':
8144 case '4':
8145 case '5':
8146 case '6':
8147 case '7':
8148 case '8':
8149 case '9':
8150 {
8151 add(current);
8152 goto scan_number_decimal2;
8153 }
8154
8155 default:
8156 {
8157 error_message = "invalid number; expected digit after '.'";
8158 return token_type::parse_error;
8159 }
8160 }
8161
8162 scan_number_decimal2:
8163 // we just parsed at least one number after a decimal point
8164 switch (get())
8165 {
8166 case '0':
8167 case '1':
8168 case '2':
8169 case '3':
8170 case '4':
8171 case '5':
8172 case '6':
8173 case '7':
8174 case '8':
8175 case '9':
8176 {
8177 add(current);
8178 goto scan_number_decimal2;
8179 }
8180
8181 case 'e':
8182 case 'E':
8183 {
8184 add(current);
8185 goto scan_number_exponent;
8186 }
8187
8188 default:
8189 goto scan_number_done;
8190 }
8191
8192 scan_number_exponent:
8193 // we just parsed an exponent
8194 number_type = token_type::value_float;
8195 switch (get())
8196 {
8197 case '+':
8198 case '-':
8199 {
8200 add(current);
8201 goto scan_number_sign;
8202 }
8203
8204 case '0':
8205 case '1':
8206 case '2':
8207 case '3':
8208 case '4':
8209 case '5':
8210 case '6':
8211 case '7':
8212 case '8':
8213 case '9':
8214 {
8215 add(current);
8216 goto scan_number_any2;
8217 }
8218
8219 default:
8220 {
8221 error_message =
8222 "invalid number; expected '+', '-', or digit after exponent";
8223 return token_type::parse_error;
8224 }
8225 }
8226
8227 scan_number_sign:
8228 // we just parsed an exponent sign
8229 switch (get())
8230 {
8231 case '0':
8232 case '1':
8233 case '2':
8234 case '3':
8235 case '4':
8236 case '5':
8237 case '6':
8238 case '7':
8239 case '8':
8240 case '9':
8241 {
8242 add(current);
8243 goto scan_number_any2;
8244 }
8245
8246 default:
8247 {
8248 error_message = "invalid number; expected digit after exponent sign";
8249 return token_type::parse_error;
8250 }
8251 }
8252
8253 scan_number_any2:
8254 // we just parsed a number after the exponent or exponent sign
8255 switch (get())
8256 {
8257 case '0':
8258 case '1':
8259 case '2':
8260 case '3':
8261 case '4':
8262 case '5':
8263 case '6':
8264 case '7':
8265 case '8':
8266 case '9':
8267 {
8268 add(current);
8269 goto scan_number_any2;
8270 }
8271
8272 default:
8273 goto scan_number_done;
8274 }
8275
8276 scan_number_done:
8277 // unget the character after the number (we only read it to know that
8278 // we are done scanning a number)
8279 unget();
8280
8281 char* endptr = nullptr; // NOLINT(misc-const-correctness,cppcoreguidelines-pro-type-vararg,hicpp-vararg)
8282 errno = 0;
8283
8284 // try to parse integers first and fall back to floats
8285 if (number_type == token_type::value_unsigned)
8286 {
8287 const auto x = std::strtoull(token_buffer.data(), &endptr, 10);
8288
8289 // we checked the number format before
8290 JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
8291
8292 if (errno != ERANGE)
8293 {
8294 value_unsigned = static_cast<number_unsigned_t>(x);
8295 if (value_unsigned == x)
8296 {
8297 return token_type::value_unsigned;
8298 }
8299 }
8300 }
8301 else if (number_type == token_type::value_integer)
8302 {
8303 const auto x = std::strtoll(token_buffer.data(), &endptr, 10);
8304
8305 // we checked the number format before
8306 JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
8307
8308 if (errno != ERANGE)
8309 {
8310 value_integer = static_cast<number_integer_t>(x);
8311 if (value_integer == x)
8312 {
8313 return token_type::value_integer;
8314 }
8315 }
8316 }
8317
8318 // this code is reached if we parse a floating-point number or if an
8319 // integer conversion above failed
8320 strtof(value_float, token_buffer.data(), &endptr);
8321
8322 // we checked the number format before
8323 JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
8324
8325 return token_type::value_float;
8326 }
8327
8328 /*!
8329 @param[in] literal_text the literal text to expect
8330 @param[in] length the length of the passed literal text
8331 @param[in] return_type the token type to return on success
8332 */
8333 JSON_HEDLEY_NON_NULL(2)
8334 token_type scan_literal(const char_type* literal_text, const std::size_t length,
8335 token_type return_type)
8336 {
8337 JSON_ASSERT(char_traits<char_type>::to_char_type(current) == literal_text[0]);
8338 for (std::size_t i = 1; i < length; ++i)
8339 {
8340 if (JSON_HEDLEY_UNLIKELY(char_traits<char_type>::to_char_type(get()) != literal_text[i]))
8341 {
8342 error_message = "invalid literal";
8343 return token_type::parse_error;
8344 }
8345 }
8346 return return_type;
8347 }
8348
8349 /////////////////////
8350 // input management
8351 /////////////////////
8352
8353 /// reset token_buffer; current character is beginning of token
8354 void reset() noexcept
8355 {
8356 token_buffer.clear();
8357 token_string.clear();
8358 decimal_point_position = std::string::npos;
8359 token_string.push_back(char_traits<char_type>::to_char_type(current));
8360 }
8361
8362 /*
8363 @brief get next character from the input
8364
8365 This function provides the interface to the used input adapter. It does
8366 not throw in case the input reached EOF, but returns a
8367 `char_traits<char>::eof()` in that case. Stores the scanned characters
8368 for use in error messages.
8369
8370 @return character read from the input
8371 */
8372 char_int_type get()
8373 {
8374 ++position.chars_read_total;
8375 ++position.chars_read_current_line;
8376
8377 if (next_unget)
8378 {
8379 // just reset the next_unget variable and work with current
8380 next_unget = false;
8381 }
8382 else
8383 {
8384 current = ia.get_character();
8385 }
8386
8387 if (JSON_HEDLEY_LIKELY(current != char_traits<char_type>::eof()))
8388 {
8389 token_string.push_back(char_traits<char_type>::to_char_type(current));
8390 }
8391
8392 if (current == '\n')
8393 {
8394 ++position.lines_read;
8395 position.chars_read_current_line = 0;
8396 }
8397
8398 return current;
8399 }
8400
8401 /*!
8402 @brief unget current character (read it again on next get)
8403
8404 We implement unget by setting variable next_unget to true. The input is not
8405 changed - we just simulate ungetting by modifying chars_read_total,
8406 chars_read_current_line, and token_string. The next call to get() will
8407 behave as if the unget character is read again.
8408 */
8409 void unget()
8410 {
8411 next_unget = true;
8412
8413 --position.chars_read_total;
8414
8415 // in case we "unget" a newline, we have to also decrement the lines_read
8416 if (position.chars_read_current_line == 0)
8417 {
8418 if (position.lines_read > 0)
8419 {
8420 --position.lines_read;
8421 }
8422 }
8423 else
8424 {
8425 --position.chars_read_current_line;
8426 }
8427
8428 if (JSON_HEDLEY_LIKELY(current != char_traits<char_type>::eof()))
8429 {
8430 JSON_ASSERT(!token_string.empty());
8431 token_string.pop_back();
8432 }
8433 }
8434
8435 /// add a character to token_buffer
8436 void add(char_int_type c)
8437 {
8438 token_buffer.push_back(static_cast<typename string_t::value_type>(c));
8439 }
8440
8441 public:
8442 /////////////////////
8443 // value getters
8444 /////////////////////
8445
8446 /// return integer value
8447 constexpr number_integer_t get_number_integer() const noexcept
8448 {
8449 return value_integer;
8450 }
8451
8452 /// return unsigned integer value
8453 constexpr number_unsigned_t get_number_unsigned() const noexcept
8454 {
8455 return value_unsigned;
8456 }
8457
8458 /// return floating-point value
8459 constexpr number_float_t get_number_float() const noexcept
8460 {
8461 return value_float;
8462 }
8463
8464 /// return current string value (implicitly resets the token; useful only once)
8465 string_t& get_string()
8466 {
8467 // translate decimal points from locale back to '.' (#4084)
8468 if (decimal_point_char != '.' && decimal_point_position != std::string::npos)
8469 {
8470 token_buffer[decimal_point_position] = '.';
8471 }
8472 return token_buffer;
8473 }
8474
8475 /////////////////////
8476 // diagnostics
8477 /////////////////////
8478
8479 /// return position of last read token
8480 constexpr position_t get_position() const noexcept
8481 {
8482 return position;
8483 }
8484
8485 /// return the last read token (for errors only). Will never contain EOF
8486 /// (an arbitrary value that is not a valid char value, often -1), because
8487 /// 255 may legitimately occur. May contain NUL, which should be escaped.
8488 std::string get_token_string() const
8489 {
8490 // escape control characters
8491 std::string result;
8492 for (const auto c : token_string)
8493 {
8494 if (static_cast<unsigned char>(c) <= '\x1F')
8495 {
8496 // escape control characters
8497 std::array<char, 9> cs{{}};
8498 static_cast<void>((std::snprintf)(cs.data(), cs.size(), "<U+%.4X>", static_cast<unsigned char>(c))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
8499 result += cs.data();
8500 }
8501 else
8502 {
8503 // add character as is
8504 result.push_back(static_cast<std::string::value_type>(c));
8505 }
8506 }
8507
8508 return result;
8509 }
8510
8511 /// return syntax error message
8512 JSON_HEDLEY_RETURNS_NON_NULL
8513 constexpr const char* get_error_message() const noexcept
8514 {
8515 return error_message;
8516 }
8517
8518 /////////////////////
8519 // actual scanner
8520 /////////////////////
8521
8522 /*!
8523 @brief skip the UTF-8 byte order mark
8524 @return true iff there is no BOM or the correct BOM has been skipped
8525 */
8526 bool skip_bom()
8527 {
8528 if (get() == 0xEF)
8529 {
8530 // check if we completely parse the BOM
8531 return get() == 0xBB && get() == 0xBF;
8532 }
8533
8534 // the first character is not the beginning of the BOM; unget it to
8535 // process is later
8536 unget();
8537 return true;
8538 }
8539
8540 void skip_whitespace()
8541 {
8542 do
8543 {
8544 get();
8545 }
8546 while (current == ' ' || current == '\t' || current == '\n' || current == '\r');
8547 }
8548
8549 token_type scan()
8550 {
8551 // initially, skip the BOM
8552 if (position.chars_read_total == 0 && !skip_bom())
8553 {
8554 error_message = "invalid BOM; must be 0xEF 0xBB 0xBF if given";
8555 return token_type::parse_error;
8556 }
8557
8558 // read next character and ignore whitespace
8559 skip_whitespace();
8560
8561 // ignore comments
8562 while (ignore_comments && current == '/')
8563 {
8564 if (!scan_comment())
8565 {
8566 return token_type::parse_error;
8567 }
8568
8569 // skip following whitespace
8570 skip_whitespace();
8571 }
8572
8573 switch (current)
8574 {
8575 // structural characters
8576 case '[':
8577 return token_type::begin_array;
8578 case ']':
8579 return token_type::end_array;
8580 case '{':
8581 return token_type::begin_object;
8582 case '}':
8583 return token_type::end_object;
8584 case ':':
8585 return token_type::name_separator;
8586 case ',':
8587 return token_type::value_separator;
8588
8589 // literals
8590 case 't':
8591 {
8592 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')}};
8593 return scan_literal(true_literal.data(), true_literal.size(), token_type::literal_true);
8594 }
8595 case 'f':
8596 {
8597 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')}};
8598 return scan_literal(false_literal.data(), false_literal.size(), token_type::literal_false);
8599 }
8600 case 'n':
8601 {
8602 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')}};
8603 return scan_literal(null_literal.data(), null_literal.size(), token_type::literal_null);
8604 }
8605
8606 // string
8607 case '\"':
8608 return scan_string();
8609
8610 // number
8611 case '-':
8612 case '0':
8613 case '1':
8614 case '2':
8615 case '3':
8616 case '4':
8617 case '5':
8618 case '6':
8619 case '7':
8620 case '8':
8621 case '9':
8622 return scan_number();
8623
8624 // end of input (the null byte is needed when parsing from
8625 // string literals)
8626 case '\0':
8627 case char_traits<char_type>::eof():
8628 return token_type::end_of_input;
8629
8630 // error
8631 default:
8632 error_message = "invalid literal";
8633 return token_type::parse_error;
8634 }
8635 }
8636
8637 private:
8638 /// input adapter
8639 InputAdapterType ia;
8640
8641 /// whether comments should be ignored (true) or signaled as errors (false)
8642 const bool ignore_comments = false;
8643
8644 /// the current character
8645 char_int_type current = char_traits<char_type>::eof();
8646
8647 /// whether the next get() call should just return current
8648 bool next_unget = false;
8649
8650 /// the start position of the current token
8651 position_t position {};
8652
8653 /// raw input token string (for error messages)
8654 std::vector<char_type> token_string {};
8655
8656 /// buffer for variable-length tokens (numbers, strings)
8657 string_t token_buffer {};
8658
8659 /// a description of occurred lexer errors
8660 const char* error_message = "";
8661
8662 // number values
8663 number_integer_t value_integer = 0;
8664 number_unsigned_t value_unsigned = 0;
8665 number_float_t value_float = 0;
8666
8667 /// the decimal point
8668 const char_int_type decimal_point_char = '.';
8669 /// the position of the decimal point in the input
8670 std::size_t decimal_point_position = std::string::npos;
8671 };
8672
8673 } // namespace detail
8674 NLOHMANN_JSON_NAMESPACE_END
8675
8676 // #include <nlohmann/detail/macro_scope.hpp>
8677
8678 // #include <nlohmann/detail/string_concat.hpp>
8679
8680 NLOHMANN_JSON_NAMESPACE_BEGIN
8681
8682 /*!
8683 @brief SAX interface
8684
8685 This class describes the SAX interface used by @ref nlohmann::json::sax_parse.
8686 Each function is called in different situations while the input is parsed. The
8687 boolean return value informs the parser whether to continue processing the
8688 input.
8689 */
8690 template<typename BasicJsonType>
8691 struct json_sax
8692 {
8693 using number_integer_t = typename BasicJsonType::number_integer_t;
8694 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
8695 using number_float_t = typename BasicJsonType::number_float_t;
8696 using string_t = typename BasicJsonType::string_t;
8697 using binary_t = typename BasicJsonType::binary_t;
8698
8699 /*!
8700 @brief a null value was read
8701 @return whether parsing should proceed
8702 */
8703 virtual bool null() = 0;
8704
8705 /*!
8706 @brief a boolean value was read
8707 @param[in] val boolean value
8708 @return whether parsing should proceed
8709 */
8710 virtual bool boolean(bool val) = 0;
8711
8712 /*!
8713 @brief an integer number was read
8714 @param[in] val integer value
8715 @return whether parsing should proceed
8716 */
8717 virtual bool number_integer(number_integer_t val) = 0;
8718
8719 /*!
8720 @brief an unsigned integer number was read
8721 @param[in] val unsigned integer value
8722 @return whether parsing should proceed
8723 */
8724 virtual bool number_unsigned(number_unsigned_t val) = 0;
8725
8726 /*!
8727 @brief a floating-point number was read
8728 @param[in] val floating-point value
8729 @param[in] s raw token value
8730 @return whether parsing should proceed
8731 */
8732 virtual bool number_float(number_float_t val, const string_t& s) = 0;
8733
8734 /*!
8735 @brief a string value was read
8736 @param[in] val string value
8737 @return whether parsing should proceed
8738 @note It is safe to move the passed string value.
8739 */
8740 virtual bool string(string_t& val) = 0;
8741
8742 /*!
8743 @brief a binary value was read
8744 @param[in] val binary value
8745 @return whether parsing should proceed
8746 @note It is safe to move the passed binary value.
8747 */
8748 virtual bool binary(binary_t& val) = 0;
8749
8750 /*!
8751 @brief the beginning of an object was read
8752 @param[in] elements number of object elements or -1 if unknown
8753 @return whether parsing should proceed
8754 @note binary formats may report the number of elements
8755 */
8756 virtual bool start_object(std::size_t elements) = 0;
8757
8758 /*!
8759 @brief an object key was read
8760 @param[in] val object key
8761 @return whether parsing should proceed
8762 @note It is safe to move the passed string.
8763 */
8764 virtual bool key(string_t& val) = 0;
8765
8766 /*!
8767 @brief the end of an object was read
8768 @return whether parsing should proceed
8769 */
8770 virtual bool end_object() = 0;
8771
8772 /*!
8773 @brief the beginning of an array was read
8774 @param[in] elements number of array elements or -1 if unknown
8775 @return whether parsing should proceed
8776 @note binary formats may report the number of elements
8777 */
8778 virtual bool start_array(std::size_t elements) = 0;
8779
8780 /*!
8781 @brief the end of an array was read
8782 @return whether parsing should proceed
8783 */
8784 virtual bool end_array() = 0;
8785
8786 /*!
8787 @brief a parse error occurred
8788 @param[in] position the position in the input where the error occurs
8789 @param[in] last_token the last read token
8790 @param[in] ex an exception object describing the error
8791 @return whether parsing should proceed (must return false)
8792 */
8793 virtual bool parse_error(std::size_t position,
8794 const std::string& last_token,
8795 const detail::exception& ex) = 0;
8796
8797 json_sax() = default;
8798 json_sax(const json_sax&) = default;
8799 json_sax(json_sax&&) noexcept = default;
8800 json_sax& operator=(const json_sax&) = default;
8801 json_sax& operator=(json_sax&&) noexcept = default;
8802 virtual ~json_sax() = default;
8803 };
8804
8805 namespace detail
8806 {
8807 constexpr std::size_t unknown_size()
8808 {
8809 return (std::numeric_limits<std::size_t>::max)();
8810 }
8811
8812 /*!
8813 @brief SAX implementation to create a JSON value from SAX events
8814
8815 This class implements the @ref json_sax interface and processes the SAX events
8816 to create a JSON value which makes it basically a DOM parser. The structure or
8817 hierarchy of the JSON value is managed by the stack `ref_stack` which contains
8818 a pointer to the respective array or object for each recursion depth.
8819
8820 After successful parsing, the value that is passed by reference to the
8821 constructor contains the parsed value.
8822
8823 @tparam BasicJsonType the JSON type
8824 */
8825 template<typename BasicJsonType, typename InputAdapterType>
8826 class json_sax_dom_parser
8827 {
8828 public:
8829 using number_integer_t = typename BasicJsonType::number_integer_t;
8830 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
8831 using number_float_t = typename BasicJsonType::number_float_t;
8832 using string_t = typename BasicJsonType::string_t;
8833 using binary_t = typename BasicJsonType::binary_t;
8834 using lexer_t = lexer<BasicJsonType, InputAdapterType>;
8835
8836 /*!
8837 @param[in,out] r reference to a JSON value that is manipulated while
8838 parsing
8839 @param[in] allow_exceptions_ whether parse errors yield exceptions
8840 */
8841 explicit json_sax_dom_parser(BasicJsonType& r, const bool allow_exceptions_ = true, lexer_t* lexer_ = nullptr)
8842 : root(r), allow_exceptions(allow_exceptions_), m_lexer_ref(lexer_)
8843 {}
8844
8845 // make class move-only
8846 json_sax_dom_parser(const json_sax_dom_parser&) = delete;
8847 json_sax_dom_parser(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
8848 json_sax_dom_parser& operator=(const json_sax_dom_parser&) = delete;
8849 json_sax_dom_parser& operator=(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
8850 ~json_sax_dom_parser() = default;
8851
8852 bool null()
8853 {
8854 handle_value(nullptr);
8855 return true;
8856 }
8857
8858 bool boolean(bool val)
8859 {
8860 handle_value(val);
8861 return true;
8862 }
8863
8864 bool number_integer(number_integer_t val)
8865 {
8866 handle_value(val);
8867 return true;
8868 }
8869
8870 bool number_unsigned(number_unsigned_t val)
8871 {
8872 handle_value(val);
8873 return true;
8874 }
8875
8876 bool number_float(number_float_t val, const string_t& /*unused*/)
8877 {
8878 handle_value(val);
8879 return true;
8880 }
8881
8882 bool string(string_t& val)
8883 {
8884 handle_value(val);
8885 return true;
8886 }
8887
8888 bool binary(binary_t& val)
8889 {
8890 handle_value(std::move(val));
8891 return true;
8892 }
8893
8894 bool start_object(std::size_t len)
8895 {
8896 ref_stack.push_back(handle_value(BasicJsonType::value_t::object));
8897
8898 #if JSON_DIAGNOSTIC_POSITIONS
8899 // Manually set the start position of the object here.
8900 // Ensure this is after the call to handle_value to ensure correct start position.
8901 if (m_lexer_ref)
8902 {
8903 // Lexer has read the first character of the object, so
8904 // subtract 1 from the position to get the correct start position.
8905 ref_stack.back()->start_position = m_lexer_ref->get_position() - 1;
8906 }
8907 #endif
8908
8909 if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
8910 {
8911 JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
8912 }
8913
8914 return true;
8915 }
8916
8917 bool key(string_t& val)
8918 {
8919 JSON_ASSERT(!ref_stack.empty());
8920 JSON_ASSERT(ref_stack.back()->is_object());
8921
8922 // add null at given key and store the reference for later
8923 object_element = &(ref_stack.back()->m_data.m_value.object->operator[](val));
8924 return true;
8925 }
8926
8927 bool end_object()
8928 {
8929 JSON_ASSERT(!ref_stack.empty());
8930 JSON_ASSERT(ref_stack.back()->is_object());
8931
8932 #if JSON_DIAGNOSTIC_POSITIONS
8933 if (m_lexer_ref)
8934 {
8935 // Lexer's position is past the closing brace, so set that as the end position.
8936 ref_stack.back()->end_position = m_lexer_ref->get_position();
8937 }
8938 #endif
8939
8940 ref_stack.back()->set_parents();
8941 ref_stack.pop_back();
8942 return true;
8943 }
8944
8945 bool start_array(std::size_t len)
8946 {
8947 ref_stack.push_back(handle_value(BasicJsonType::value_t::array));
8948
8949 #if JSON_DIAGNOSTIC_POSITIONS
8950 // Manually set the start position of the array here.
8951 // Ensure this is after the call to handle_value to ensure correct start position.
8952 if (m_lexer_ref)
8953 {
8954 ref_stack.back()->start_position = m_lexer_ref->get_position() - 1;
8955 }
8956 #endif
8957
8958 if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
8959 {
8960 JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
8961 }
8962
8963 return true;
8964 }
8965
8966 bool end_array()
8967 {
8968 JSON_ASSERT(!ref_stack.empty());
8969 JSON_ASSERT(ref_stack.back()->is_array());
8970
8971 #if JSON_DIAGNOSTIC_POSITIONS
8972 if (m_lexer_ref)
8973 {
8974 // Lexer's position is past the closing bracket, so set that as the end position.
8975 ref_stack.back()->end_position = m_lexer_ref->get_position();
8976 }
8977 #endif
8978
8979 ref_stack.back()->set_parents();
8980 ref_stack.pop_back();
8981 return true;
8982 }
8983
8984 template<class Exception>
8985 bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
8986 const Exception& ex)
8987 {
8988 errored = true;
8989 static_cast<void>(ex);
8990 if (allow_exceptions)
8991 {
8992 JSON_THROW(ex);
8993 }
8994 return false;
8995 }
8996
8997 constexpr bool is_errored() const
8998 {
8999 return errored;
9000 }
9001
9002 private:
9003
9004 #if JSON_DIAGNOSTIC_POSITIONS
9005 void handle_diagnostic_positions_for_json_value(BasicJsonType& v)
9006 {
9007 if (m_lexer_ref)
9008 {
9009 // Lexer has read past the current field value, so set the end position to the current position.
9010 // The start position will be set below based on the length of the string representation
9011 // of the value.
9012 v.end_position = m_lexer_ref->get_position();
9013
9014 switch (v.type())
9015 {
9016 case value_t::boolean:
9017 {
9018 // 4 and 5 are the string length of "true" and "false"
9019 v.start_position = v.end_position - (v.m_data.m_value.boolean ? 4 : 5);
9020 break;
9021 }
9022
9023 case value_t::null:
9024 {
9025 // 4 is the string length of "null"
9026 v.start_position = v.end_position - 4;
9027 break;
9028 }
9029
9030 case value_t::string:
9031 {
9032 // include the length of the quotes, which is 2
9033 v.start_position = v.end_position - v.m_data.m_value.string->size() - 2;
9034 break;
9035 }
9036
9037 // As we handle the start and end positions for values created during parsing,
9038 // we do not expect the following value type to be called. Regardless, set the positions
9039 // in case this is created manually or through a different constructor. Exclude from lcov
9040 // since the exact condition of this switch is esoteric.
9041 // LCOV_EXCL_START
9042 case value_t::discarded:
9043 {
9044 v.end_position = std::string::npos;
9045 v.start_position = v.end_position;
9046 break;
9047 }
9048 // LCOV_EXCL_STOP
9049 case value_t::binary:
9050 case value_t::number_integer:
9051 case value_t::number_unsigned:
9052 case value_t::number_float:
9053 {
9054 v.start_position = v.end_position - m_lexer_ref->get_string().size();
9055 break;
9056 }
9057 case value_t::object:
9058 case value_t::array:
9059 {
9060 // object and array are handled in start_object() and start_array() handlers
9061 // skip setting the values here.
9062 break;
9063 }
9064 default: // LCOV_EXCL_LINE
9065 // Handle all possible types discretely, default handler should never be reached.
9066 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert,-warnings-as-errors) LCOV_EXCL_LINE
9067 }
9068 }
9069 }
9070 #endif
9071
9072 /*!
9073 @invariant If the ref stack is empty, then the passed value will be the new
9074 root.
9075 @invariant If the ref stack contains a value, then it is an array or an
9076 object to which we can add elements
9077 */
9078 template<typename Value>
9079 JSON_HEDLEY_RETURNS_NON_NULL
9080 BasicJsonType* handle_value(Value&& v)
9081 {
9082 if (ref_stack.empty())
9083 {
9084 root = BasicJsonType(std::forward<Value>(v));
9085
9086 #if JSON_DIAGNOSTIC_POSITIONS
9087 handle_diagnostic_positions_for_json_value(root);
9088 #endif
9089
9090 return &root;
9091 }
9092
9093 JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
9094
9095 if (ref_stack.back()->is_array())
9096 {
9097 ref_stack.back()->m_data.m_value.array->emplace_back(std::forward<Value>(v));
9098
9099 #if JSON_DIAGNOSTIC_POSITIONS
9100 handle_diagnostic_positions_for_json_value(ref_stack.back()->m_data.m_value.array->back());
9101 #endif
9102
9103 return &(ref_stack.back()->m_data.m_value.array->back());
9104 }
9105
9106 JSON_ASSERT(ref_stack.back()->is_object());
9107 JSON_ASSERT(object_element);
9108 *object_element = BasicJsonType(std::forward<Value>(v));
9109
9110 #if JSON_DIAGNOSTIC_POSITIONS
9111 handle_diagnostic_positions_for_json_value(*object_element);
9112 #endif
9113
9114 return object_element;
9115 }
9116
9117 /// the parsed JSON value
9118 BasicJsonType& root;
9119 /// stack to model hierarchy of values
9120 std::vector<BasicJsonType*> ref_stack {};
9121 /// helper to hold the reference for the next object element
9122 BasicJsonType* object_element = nullptr;
9123 /// whether a syntax error occurred
9124 bool errored = false;
9125 /// whether to throw exceptions in case of errors
9126 const bool allow_exceptions = true;
9127 /// the lexer reference to obtain the current position
9128 lexer_t* m_lexer_ref = nullptr;
9129 };
9130
9131 template<typename BasicJsonType, typename InputAdapterType>
9132 class json_sax_dom_callback_parser
9133 {
9134 public:
9135 using number_integer_t = typename BasicJsonType::number_integer_t;
9136 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
9137 using number_float_t = typename BasicJsonType::number_float_t;
9138 using string_t = typename BasicJsonType::string_t;
9139 using binary_t = typename BasicJsonType::binary_t;
9140 using parser_callback_t = typename BasicJsonType::parser_callback_t;
9141 using parse_event_t = typename BasicJsonType::parse_event_t;
9142 using lexer_t = lexer<BasicJsonType, InputAdapterType>;
9143
9144 json_sax_dom_callback_parser(BasicJsonType& r,
9145 parser_callback_t cb,
9146 const bool allow_exceptions_ = true,
9147 lexer_t* lexer_ = nullptr)
9148 : root(r), callback(std::move(cb)), allow_exceptions(allow_exceptions_), m_lexer_ref(lexer_)
9149 {
9150 keep_stack.push_back(true);
9151 }
9152
9153 // make class move-only
9154 json_sax_dom_callback_parser(const json_sax_dom_callback_parser&) = delete;
9155 json_sax_dom_callback_parser(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
9156 json_sax_dom_callback_parser& operator=(const json_sax_dom_callback_parser&) = delete;
9157 json_sax_dom_callback_parser& operator=(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
9158 ~json_sax_dom_callback_parser() = default;
9159
9160 bool null()
9161 {
9162 handle_value(nullptr);
9163 return true;
9164 }
9165
9166 bool boolean(bool val)
9167 {
9168 handle_value(val);
9169 return true;
9170 }
9171
9172 bool number_integer(number_integer_t val)
9173 {
9174 handle_value(val);
9175 return true;
9176 }
9177
9178 bool number_unsigned(number_unsigned_t val)
9179 {
9180 handle_value(val);
9181 return true;
9182 }
9183
9184 bool number_float(number_float_t val, const string_t& /*unused*/)
9185 {
9186 handle_value(val);
9187 return true;
9188 }
9189
9190 bool string(string_t& val)
9191 {
9192 handle_value(val);
9193 return true;
9194 }
9195
9196 bool binary(binary_t& val)
9197 {
9198 handle_value(std::move(val));
9199 return true;
9200 }
9201
9202 bool start_object(std::size_t len)
9203 {
9204 // check callback for object start
9205 const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::object_start, discarded);
9206 keep_stack.push_back(keep);
9207
9208 auto val = handle_value(BasicJsonType::value_t::object, true);
9209 ref_stack.push_back(val.second);
9210
9211 if (ref_stack.back())
9212 {
9213
9214 #if JSON_DIAGNOSTIC_POSITIONS
9215 // Manually set the start position of the object here.
9216 // Ensure this is after the call to handle_value to ensure correct start position.
9217 if (m_lexer_ref)
9218 {
9219 // Lexer has read the first character of the object, so
9220 // subtract 1 from the position to get the correct start position.
9221 ref_stack.back()->start_position = m_lexer_ref->get_position() - 1;
9222 }
9223 #endif
9224
9225 // check object limit
9226 if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
9227 {
9228 JSON_THROW(out_of_range::create(408, concat("excessive object size: ", std::to_string(len)), ref_stack.back()));
9229 }
9230 }
9231 return true;
9232 }
9233
9234 bool key(string_t& val)
9235 {
9236 BasicJsonType k = BasicJsonType(val);
9237
9238 // check callback for key
9239 const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::key, k);
9240 key_keep_stack.push_back(keep);
9241
9242 // add discarded value at given key and store the reference for later
9243 if (keep && ref_stack.back())
9244 {
9245 object_element = &(ref_stack.back()->m_data.m_value.object->operator[](val) = discarded);
9246 }
9247
9248 return true;
9249 }
9250
9251 bool end_object()
9252 {
9253 if (ref_stack.back())
9254 {
9255 if (!callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back()))
9256 {
9257 // discard object
9258 *ref_stack.back() = discarded;
9259
9260 #if JSON_DIAGNOSTIC_POSITIONS
9261 // Set start/end positions for discarded object.
9262 handle_diagnostic_positions_for_json_value(*ref_stack.back());
9263 #endif
9264 }
9265 else
9266 {
9267
9268 #if JSON_DIAGNOSTIC_POSITIONS
9269 if (m_lexer_ref)
9270 {
9271 // Lexer's position is past the closing brace, so set that as the end position.
9272 ref_stack.back()->end_position = m_lexer_ref->get_position();
9273 }
9274 #endif
9275
9276 ref_stack.back()->set_parents();
9277 }
9278 }
9279
9280 JSON_ASSERT(!ref_stack.empty());
9281 JSON_ASSERT(!keep_stack.empty());
9282 ref_stack.pop_back();
9283 keep_stack.pop_back();
9284
9285 if (!ref_stack.empty() && ref_stack.back() && ref_stack.back()->is_structured())
9286 {
9287 // remove discarded value
9288 for (auto it = ref_stack.back()->begin(); it != ref_stack.back()->end(); ++it)
9289 {
9290 if (it->is_discarded())
9291 {
9292 ref_stack.back()->erase(it);
9293 break;
9294 }
9295 }
9296 }
9297
9298 return true;
9299 }
9300
9301 bool start_array(std::size_t len)
9302 {
9303 const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::array_start, discarded);
9304 keep_stack.push_back(keep);
9305
9306 auto val = handle_value(BasicJsonType::value_t::array, true);
9307 ref_stack.push_back(val.second);
9308
9309 if (ref_stack.back())
9310 {
9311
9312 #if JSON_DIAGNOSTIC_POSITIONS
9313 // Manually set the start position of the array here.
9314 // Ensure this is after the call to handle_value to ensure correct start position.
9315 if (m_lexer_ref)
9316 {
9317 // Lexer has read the first character of the array, so
9318 // subtract 1 from the position to get the correct start position.
9319 ref_stack.back()->start_position = m_lexer_ref->get_position() - 1;
9320 }
9321 #endif
9322
9323 // check array limit
9324 if (JSON_HEDLEY_UNLIKELY(len != detail::unknown_size() && len > ref_stack.back()->max_size()))
9325 {
9326 JSON_THROW(out_of_range::create(408, concat("excessive array size: ", std::to_string(len)), ref_stack.back()));
9327 }
9328 }
9329
9330 return true;
9331 }
9332
9333 bool end_array()
9334 {
9335 bool keep = true;
9336
9337 if (ref_stack.back())
9338 {
9339 keep = callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back());
9340 if (keep)
9341 {
9342
9343 #if JSON_DIAGNOSTIC_POSITIONS
9344 if (m_lexer_ref)
9345 {
9346 // Lexer's position is past the closing bracket, so set that as the end position.
9347 ref_stack.back()->end_position = m_lexer_ref->get_position();
9348 }
9349 #endif
9350
9351 ref_stack.back()->set_parents();
9352 }
9353 else
9354 {
9355 // discard array
9356 *ref_stack.back() = discarded;
9357
9358 #if JSON_DIAGNOSTIC_POSITIONS
9359 // Set start/end positions for discarded array.
9360 handle_diagnostic_positions_for_json_value(*ref_stack.back());
9361 #endif
9362 }
9363 }
9364
9365 JSON_ASSERT(!ref_stack.empty());
9366 JSON_ASSERT(!keep_stack.empty());
9367 ref_stack.pop_back();
9368 keep_stack.pop_back();
9369
9370 // remove discarded value
9371 if (!keep && !ref_stack.empty() && ref_stack.back()->is_array())
9372 {
9373 ref_stack.back()->m_data.m_value.array->pop_back();
9374 }
9375
9376 return true;
9377 }
9378
9379 template<class Exception>
9380 bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
9381 const Exception& ex)
9382 {
9383 errored = true;
9384 static_cast<void>(ex);
9385 if (allow_exceptions)
9386 {
9387 JSON_THROW(ex);
9388 }
9389 return false;
9390 }
9391
9392 constexpr bool is_errored() const
9393 {
9394 return errored;
9395 }
9396
9397 private:
9398
9399 #if JSON_DIAGNOSTIC_POSITIONS
9400 void handle_diagnostic_positions_for_json_value(BasicJsonType& v)
9401 {
9402 if (m_lexer_ref)
9403 {
9404 // Lexer has read past the current field value, so set the end position to the current position.
9405 // The start position will be set below based on the length of the string representation
9406 // of the value.
9407 v.end_position = m_lexer_ref->get_position();
9408
9409 switch (v.type())
9410 {
9411 case value_t::boolean:
9412 {
9413 // 4 and 5 are the string length of "true" and "false"
9414 v.start_position = v.end_position - (v.m_data.m_value.boolean ? 4 : 5);
9415 break;
9416 }
9417
9418 case value_t::null:
9419 {
9420 // 4 is the string length of "null"
9421 v.start_position = v.end_position - 4;
9422 break;
9423 }
9424
9425 case value_t::string:
9426 {
9427 // include the length of the quotes, which is 2
9428 v.start_position = v.end_position - v.m_data.m_value.string->size() - 2;
9429 break;
9430 }
9431
9432 case value_t::discarded:
9433 {
9434 v.end_position = std::string::npos;
9435 v.start_position = v.end_position;
9436 break;
9437 }
9438
9439 case value_t::binary:
9440 case value_t::number_integer:
9441 case value_t::number_unsigned:
9442 case value_t::number_float:
9443 {
9444 v.start_position = v.end_position - m_lexer_ref->get_string().size();
9445 break;
9446 }
9447
9448 case value_t::object:
9449 case value_t::array:
9450 {
9451 // object and array are handled in start_object() and start_array() handlers
9452 // skip setting the values here.
9453 break;
9454 }
9455 default: // LCOV_EXCL_LINE
9456 // Handle all possible types discretely, default handler should never be reached.
9457 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert,-warnings-as-errors) LCOV_EXCL_LINE
9458 }
9459 }
9460 }
9461 #endif
9462
9463 /*!
9464 @param[in] v value to add to the JSON value we build during parsing
9465 @param[in] skip_callback whether we should skip calling the callback
9466 function; this is required after start_array() and
9467 start_object() SAX events, because otherwise we would call the
9468 callback function with an empty array or object, respectively.
9469
9470 @invariant If the ref stack is empty, then the passed value will be the new
9471 root.
9472 @invariant If the ref stack contains a value, then it is an array or an
9473 object to which we can add elements
9474
9475 @return pair of boolean (whether value should be kept) and pointer (to the
9476 passed value in the ref_stack hierarchy; nullptr if not kept)
9477 */
9478 template<typename Value>
9479 std::pair<bool, BasicJsonType*> handle_value(Value&& v, const bool skip_callback = false)
9480 {
9481 JSON_ASSERT(!keep_stack.empty());
9482
9483 // do not handle this value if we know it would be added to a discarded
9484 // container
9485 if (!keep_stack.back())
9486 {
9487 return {false, nullptr};
9488 }
9489
9490 // create value
9491 auto value = BasicJsonType(std::forward<Value>(v));
9492
9493 #if JSON_DIAGNOSTIC_POSITIONS
9494 handle_diagnostic_positions_for_json_value(value);
9495 #endif
9496
9497 // check callback
9498 const bool keep = skip_callback || callback(static_cast<int>(ref_stack.size()), parse_event_t::value, value);
9499
9500 // do not handle this value if we just learnt it shall be discarded
9501 if (!keep)
9502 {
9503 return {false, nullptr};
9504 }
9505
9506 if (ref_stack.empty())
9507 {
9508 root = std::move(value);
9509 return {true, & root};
9510 }
9511
9512 // skip this value if we already decided to skip the parent
9513 // (https://github.com/nlohmann/json/issues/971#issuecomment-413678360)
9514 if (!ref_stack.back())
9515 {
9516 return {false, nullptr};
9517 }
9518
9519 // we now only expect arrays and objects
9520 JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
9521
9522 // array
9523 if (ref_stack.back()->is_array())
9524 {
9525 ref_stack.back()->m_data.m_value.array->emplace_back(std::move(value));
9526 return {true, & (ref_stack.back()->m_data.m_value.array->back())};
9527 }
9528
9529 // object
9530 JSON_ASSERT(ref_stack.back()->is_object());
9531 // check if we should store an element for the current key
9532 JSON_ASSERT(!key_keep_stack.empty());
9533 const bool store_element = key_keep_stack.back();
9534 key_keep_stack.pop_back();
9535
9536 if (!store_element)
9537 {
9538 return {false, nullptr};
9539 }
9540
9541 JSON_ASSERT(object_element);
9542 *object_element = std::move(value);
9543 return {true, object_element};
9544 }
9545
9546 /// the parsed JSON value
9547 BasicJsonType& root;
9548 /// stack to model hierarchy of values
9549 std::vector<BasicJsonType*> ref_stack {};
9550 /// stack to manage which values to keep
9551 std::vector<bool> keep_stack {}; // NOLINT(readability-redundant-member-init)
9552 /// stack to manage which object keys to keep
9553 std::vector<bool> key_keep_stack {}; // NOLINT(readability-redundant-member-init)
9554 /// helper to hold the reference for the next object element
9555 BasicJsonType* object_element = nullptr;
9556 /// whether a syntax error occurred
9557 bool errored = false;
9558 /// callback function
9559 const parser_callback_t callback = nullptr;
9560 /// whether to throw exceptions in case of errors
9561 const bool allow_exceptions = true;
9562 /// a discarded value for the callback
9563 BasicJsonType discarded = BasicJsonType::value_t::discarded;
9564 /// the lexer reference to obtain the current position
9565 lexer_t* m_lexer_ref = nullptr;
9566 };
9567
9568 template<typename BasicJsonType>
9569 class json_sax_acceptor
9570 {
9571 public:
9572 using number_integer_t = typename BasicJsonType::number_integer_t;
9573 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
9574 using number_float_t = typename BasicJsonType::number_float_t;
9575 using string_t = typename BasicJsonType::string_t;
9576 using binary_t = typename BasicJsonType::binary_t;
9577
9578 bool null()
9579 {
9580 return true;
9581 }
9582
9583 bool boolean(bool /*unused*/)
9584 {
9585 return true;
9586 }
9587
9588 bool number_integer(number_integer_t /*unused*/)
9589 {
9590 return true;
9591 }
9592
9593 bool number_unsigned(number_unsigned_t /*unused*/)
9594 {
9595 return true;
9596 }
9597
9598 bool number_float(number_float_t /*unused*/, const string_t& /*unused*/)
9599 {
9600 return true;
9601 }
9602
9603 bool string(string_t& /*unused*/)
9604 {
9605 return true;
9606 }
9607
9608 bool binary(binary_t& /*unused*/)
9609 {
9610 return true;
9611 }
9612
9613 bool start_object(std::size_t /*unused*/ = detail::unknown_size())
9614 {
9615 return true;
9616 }
9617
9618 bool key(string_t& /*unused*/)
9619 {
9620 return true;
9621 }
9622
9623 bool end_object()
9624 {
9625 return true;
9626 }
9627
9628 bool start_array(std::size_t /*unused*/ = detail::unknown_size())
9629 {
9630 return true;
9631 }
9632
9633 bool end_array()
9634 {
9635 return true;
9636 }
9637
9638 bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const detail::exception& /*unused*/)
9639 {
9640 return false;
9641 }
9642 };
9643
9644 } // namespace detail
9645 NLOHMANN_JSON_NAMESPACE_END
9646
9647 // #include <nlohmann/detail/input/lexer.hpp>
9648
9649 // #include <nlohmann/detail/macro_scope.hpp>
9650
9651 // #include <nlohmann/detail/meta/is_sax.hpp>
9652 // __ _____ _____ _____
9653 // __| | __| | | | JSON for Modern C++
9654 // | | |__ | | | | | | version 3.12.0
9655 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
9656 //
9657 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
9658 // SPDX-License-Identifier: MIT
9659
9660
9661
9662 #include <cstdint> // size_t
9663 #include <utility> // declval
9664 #include <string> // string
9665
9666 // #include <nlohmann/detail/abi_macros.hpp>
9667
9668 // #include <nlohmann/detail/meta/detected.hpp>
9669
9670 // #include <nlohmann/detail/meta/type_traits.hpp>
9671
9672
9673 NLOHMANN_JSON_NAMESPACE_BEGIN
9674 namespace detail
9675 {
9676
9677 template<typename T>
9678 using null_function_t = decltype(std::declval<T&>().null());
9679
9680 template<typename T>
9681 using boolean_function_t =
9682 decltype(std::declval<T&>().boolean(std::declval<bool>()));
9683
9684 template<typename T, typename Integer>
9685 using number_integer_function_t =
9686 decltype(std::declval<T&>().number_integer(std::declval<Integer>()));
9687
9688 template<typename T, typename Unsigned>
9689 using number_unsigned_function_t =
9690 decltype(std::declval<T&>().number_unsigned(std::declval<Unsigned>()));
9691
9692 template<typename T, typename Float, typename String>
9693 using number_float_function_t = decltype(std::declval<T&>().number_float(
9694 std::declval<Float>(), std::declval<const String&>()));
9695
9696 template<typename T, typename String>
9697 using string_function_t =
9698 decltype(std::declval<T&>().string(std::declval<String&>()));
9699
9700 template<typename T, typename Binary>
9701 using binary_function_t =
9702 decltype(std::declval<T&>().binary(std::declval<Binary&>()));
9703
9704 template<typename T>
9705 using start_object_function_t =
9706 decltype(std::declval<T&>().start_object(std::declval<std::size_t>()));
9707
9708 template<typename T, typename String>
9709 using key_function_t =
9710 decltype(std::declval<T&>().key(std::declval<String&>()));
9711
9712 template<typename T>
9713 using end_object_function_t = decltype(std::declval<T&>().end_object());
9714
9715 template<typename T>
9716 using start_array_function_t =
9717 decltype(std::declval<T&>().start_array(std::declval<std::size_t>()));
9718
9719 template<typename T>
9720 using end_array_function_t = decltype(std::declval<T&>().end_array());
9721
9722 template<typename T, typename Exception>
9723 using parse_error_function_t = decltype(std::declval<T&>().parse_error(
9724 std::declval<std::size_t>(), std::declval<const std::string&>(),
9725 std::declval<const Exception&>()));
9726
9727 template<typename SAX, typename BasicJsonType>
9728 struct is_sax
9729 {
9730 private:
9731 static_assert(is_basic_json<BasicJsonType>::value,
9732 "BasicJsonType must be of type basic_json<...>");
9733
9734 using number_integer_t = typename BasicJsonType::number_integer_t;
9735 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
9736 using number_float_t = typename BasicJsonType::number_float_t;
9737 using string_t = typename BasicJsonType::string_t;
9738 using binary_t = typename BasicJsonType::binary_t;
9739 using exception_t = typename BasicJsonType::exception;
9740
9741 public:
9742 static constexpr bool value =
9743 is_detected_exact<bool, null_function_t, SAX>::value &&
9744 is_detected_exact<bool, boolean_function_t, SAX>::value &&
9745 is_detected_exact<bool, number_integer_function_t, SAX, number_integer_t>::value &&
9746 is_detected_exact<bool, number_unsigned_function_t, SAX, number_unsigned_t>::value &&
9747 is_detected_exact<bool, number_float_function_t, SAX, number_float_t, string_t>::value &&
9748 is_detected_exact<bool, string_function_t, SAX, string_t>::value &&
9749 is_detected_exact<bool, binary_function_t, SAX, binary_t>::value &&
9750 is_detected_exact<bool, start_object_function_t, SAX>::value &&
9751 is_detected_exact<bool, key_function_t, SAX, string_t>::value &&
9752 is_detected_exact<bool, end_object_function_t, SAX>::value &&
9753 is_detected_exact<bool, start_array_function_t, SAX>::value &&
9754 is_detected_exact<bool, end_array_function_t, SAX>::value &&
9755 is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value;
9756 };
9757
9758 template<typename SAX, typename BasicJsonType>
9759 struct is_sax_static_asserts
9760 {
9761 private:
9762 static_assert(is_basic_json<BasicJsonType>::value,
9763 "BasicJsonType must be of type basic_json<...>");
9764
9765 using number_integer_t = typename BasicJsonType::number_integer_t;
9766 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
9767 using number_float_t = typename BasicJsonType::number_float_t;
9768 using string_t = typename BasicJsonType::string_t;
9769 using binary_t = typename BasicJsonType::binary_t;
9770 using exception_t = typename BasicJsonType::exception;
9771
9772 public:
9773 static_assert(is_detected_exact<bool, null_function_t, SAX>::value,
9774 "Missing/invalid function: bool null()");
9775 static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
9776 "Missing/invalid function: bool boolean(bool)");
9777 static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
9778 "Missing/invalid function: bool boolean(bool)");
9779 static_assert(
9780 is_detected_exact<bool, number_integer_function_t, SAX,
9781 number_integer_t>::value,
9782 "Missing/invalid function: bool number_integer(number_integer_t)");
9783 static_assert(
9784 is_detected_exact<bool, number_unsigned_function_t, SAX,
9785 number_unsigned_t>::value,
9786 "Missing/invalid function: bool number_unsigned(number_unsigned_t)");
9787 static_assert(is_detected_exact<bool, number_float_function_t, SAX,
9788 number_float_t, string_t>::value,
9789 "Missing/invalid function: bool number_float(number_float_t, const string_t&)");
9790 static_assert(
9791 is_detected_exact<bool, string_function_t, SAX, string_t>::value,
9792 "Missing/invalid function: bool string(string_t&)");
9793 static_assert(
9794 is_detected_exact<bool, binary_function_t, SAX, binary_t>::value,
9795 "Missing/invalid function: bool binary(binary_t&)");
9796 static_assert(is_detected_exact<bool, start_object_function_t, SAX>::value,
9797 "Missing/invalid function: bool start_object(std::size_t)");
9798 static_assert(is_detected_exact<bool, key_function_t, SAX, string_t>::value,
9799 "Missing/invalid function: bool key(string_t&)");
9800 static_assert(is_detected_exact<bool, end_object_function_t, SAX>::value,
9801 "Missing/invalid function: bool end_object()");
9802 static_assert(is_detected_exact<bool, start_array_function_t, SAX>::value,
9803 "Missing/invalid function: bool start_array(std::size_t)");
9804 static_assert(is_detected_exact<bool, end_array_function_t, SAX>::value,
9805 "Missing/invalid function: bool end_array()");
9806 static_assert(
9807 is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value,
9808 "Missing/invalid function: bool parse_error(std::size_t, const "
9809 "std::string&, const exception&)");
9810 };
9811
9812 } // namespace detail
9813 NLOHMANN_JSON_NAMESPACE_END
9814
9815 // #include <nlohmann/detail/meta/type_traits.hpp>
9816
9817 // #include <nlohmann/detail/string_concat.hpp>
9818
9819 // #include <nlohmann/detail/value_t.hpp>
9820
9821
9822 NLOHMANN_JSON_NAMESPACE_BEGIN
9823 namespace detail
9824 {
9825
9826 /// how to treat CBOR tags
9827 enum class cbor_tag_handler_t
9828 {
9829 error, ///< throw a parse_error exception in case of a tag
9830 ignore, ///< ignore tags
9831 store ///< store tags as binary type
9832 };
9833
9834 /*!
9835 @brief determine system byte order
9836
9837 @return true if and only if system's byte order is little endian
9838
9839 @note from https://stackoverflow.com/a/1001328/266378
9840 */
9841 static inline bool little_endianness(int num = 1) noexcept
9842 {
9843 return *reinterpret_cast<char*>(&num) == 1;
9844 }
9845
9846 ///////////////////
9847 // binary reader //
9848 ///////////////////
9849
9850 /*!
9851 @brief deserialization of CBOR, MessagePack, and UBJSON values
9852 */
9853 template<typename BasicJsonType, typename InputAdapterType, typename SAX = json_sax_dom_parser<BasicJsonType, InputAdapterType>>
9854 class binary_reader
9855 {
9856 using number_integer_t = typename BasicJsonType::number_integer_t;
9857 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
9858 using number_float_t = typename BasicJsonType::number_float_t;
9859 using string_t = typename BasicJsonType::string_t;
9860 using binary_t = typename BasicJsonType::binary_t;
9861 using json_sax_t = SAX;
9862 using char_type = typename InputAdapterType::char_type;
9863 using char_int_type = typename char_traits<char_type>::int_type;
9864
9865 public:
9866 /*!
9867 @brief create a binary reader
9868
9869 @param[in] adapter input adapter to read from
9870 */
9871 explicit binary_reader(InputAdapterType&& adapter, const input_format_t format = input_format_t::json) noexcept : ia(std::move(adapter)), input_format(format)
9872 {
9873 (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
9874 }
9875
9876 // make class move-only
9877 binary_reader(const binary_reader&) = delete;
9878 binary_reader(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
9879 binary_reader& operator=(const binary_reader&) = delete;
9880 binary_reader& operator=(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor)
9881 ~binary_reader() = default;
9882
9883 /*!
9884 @param[in] format the binary format to parse
9885 @param[in] sax_ a SAX event processor
9886 @param[in] strict whether to expect the input to be consumed completed
9887 @param[in] tag_handler how to treat CBOR tags
9888
9889 @return whether parsing was successful
9890 */
9891 JSON_HEDLEY_NON_NULL(3)
9892 bool sax_parse(const input_format_t format,
9893 json_sax_t* sax_,
9894 const bool strict = true,
9895 const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
9896 {
9897 sax = sax_;
9898 bool result = false;
9899
9900 switch (format)
9901 {
9902 case input_format_t::bson:
9903 result = parse_bson_internal();
9904 break;
9905
9906 case input_format_t::cbor:
9907 result = parse_cbor_internal(true, tag_handler);
9908 break;
9909
9910 case input_format_t::msgpack:
9911 result = parse_msgpack_internal();
9912 break;
9913
9914 case input_format_t::ubjson:
9915 case input_format_t::bjdata:
9916 result = parse_ubjson_internal();
9917 break;
9918
9919 case input_format_t::json: // LCOV_EXCL_LINE
9920 default: // LCOV_EXCL_LINE
9921 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
9922 }
9923
9924 // strict mode: next byte must be EOF
9925 if (result && strict)
9926 {
9927 if (input_format == input_format_t::ubjson || input_format == input_format_t::bjdata)
9928 {
9929 get_ignore_noop();
9930 }
9931 else
9932 {
9933 get();
9934 }
9935
9936 if (JSON_HEDLEY_UNLIKELY(current != char_traits<char_type>::eof()))
9937 {
9938 return sax->parse_error(chars_read, get_token_string(), parse_error::create(110, chars_read,
9939 exception_message(input_format, concat("expected end of input; last byte: 0x", get_token_string()), "value"), nullptr));
9940 }
9941 }
9942
9943 return result;
9944 }
9945
9946 private:
9947 //////////
9948 // BSON //
9949 //////////
9950
9951 /*!
9952 @brief Reads in a BSON-object and passes it to the SAX-parser.
9953 @return whether a valid BSON-value was passed to the SAX parser
9954 */
9955 bool parse_bson_internal()
9956 {
9957 std::int32_t document_size{};
9958 get_number<std::int32_t, true>(input_format_t::bson, document_size);
9959
9960 if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
9961 {
9962 return false;
9963 }
9964
9965 if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/false)))
9966 {
9967 return false;
9968 }
9969
9970 return sax->end_object();
9971 }
9972
9973 /*!
9974 @brief Parses a C-style string from the BSON input.
9975 @param[in,out] result A reference to the string variable where the read
9976 string is to be stored.
9977 @return `true` if the \x00-byte indicating the end of the string was
9978 encountered before the EOF; false` indicates an unexpected EOF.
9979 */
9980 bool get_bson_cstr(string_t& result)
9981 {
9982 auto out = std::back_inserter(result);
9983 while (true)
9984 {
9985 get();
9986 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "cstring")))
9987 {
9988 return false;
9989 }
9990 if (current == 0x00)
9991 {
9992 return true;
9993 }
9994 *out++ = static_cast<typename string_t::value_type>(current);
9995 }
9996 }
9997
9998 /*!
9999 @brief Parses a zero-terminated string of length @a len from the BSON
10000 input.
10001 @param[in] len The length (including the zero-byte at the end) of the
10002 string to be read.
10003 @param[in,out] result A reference to the string variable where the read
10004 string is to be stored.
10005 @tparam NumberType The type of the length @a len
10006 @pre len >= 1
10007 @return `true` if the string was successfully parsed
10008 */
10009 template<typename NumberType>
10010 bool get_bson_string(const NumberType len, string_t& result)
10011 {
10012 if (JSON_HEDLEY_UNLIKELY(len < 1))
10013 {
10014 auto last_token = get_token_string();
10015 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
10016 exception_message(input_format_t::bson, concat("string length must be at least 1, is ", std::to_string(len)), "string"), nullptr));
10017 }
10018
10019 return get_string(input_format_t::bson, len - static_cast<NumberType>(1), result) && get() != char_traits<char_type>::eof();
10020 }
10021
10022 /*!
10023 @brief Parses a byte array input of length @a len from the BSON input.
10024 @param[in] len The length of the byte array to be read.
10025 @param[in,out] result A reference to the binary variable where the read
10026 array is to be stored.
10027 @tparam NumberType The type of the length @a len
10028 @pre len >= 0
10029 @return `true` if the byte array was successfully parsed
10030 */
10031 template<typename NumberType>
10032 bool get_bson_binary(const NumberType len, binary_t& result)
10033 {
10034 if (JSON_HEDLEY_UNLIKELY(len < 0))
10035 {
10036 auto last_token = get_token_string();
10037 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
10038 exception_message(input_format_t::bson, concat("byte array length cannot be negative, is ", std::to_string(len)), "binary"), nullptr));
10039 }
10040
10041 // All BSON binary values have a subtype
10042 std::uint8_t subtype{};
10043 get_number<std::uint8_t>(input_format_t::bson, subtype);
10044 result.set_subtype(subtype);
10045
10046 return get_binary(input_format_t::bson, len, result);
10047 }
10048
10049 /*!
10050 @brief Read a BSON document element of the given @a element_type.
10051 @param[in] element_type The BSON element type, c.f. http://bsonspec.org/spec.html
10052 @param[in] element_type_parse_position The position in the input stream,
10053 where the `element_type` was read.
10054 @warning Not all BSON element types are supported yet. An unsupported
10055 @a element_type will give rise to a parse_error.114:
10056 Unsupported BSON record type 0x...
10057 @return whether a valid BSON-object/array was passed to the SAX parser
10058 */
10059 bool parse_bson_element_internal(const char_int_type element_type,
10060 const std::size_t element_type_parse_position)
10061 {
10062 switch (element_type)
10063 {
10064 case 0x01: // double
10065 {
10066 double number{};
10067 return get_number<double, true>(input_format_t::bson, number) && sax->number_float(static_cast<number_float_t>(number), "");
10068 }
10069
10070 case 0x02: // string
10071 {
10072 std::int32_t len{};
10073 string_t value;
10074 return get_number<std::int32_t, true>(input_format_t::bson, len) && get_bson_string(len, value) && sax->string(value);
10075 }
10076
10077 case 0x03: // object
10078 {
10079 return parse_bson_internal();
10080 }
10081
10082 case 0x04: // array
10083 {
10084 return parse_bson_array();
10085 }
10086
10087 case 0x05: // binary
10088 {
10089 std::int32_t len{};
10090 binary_t value;
10091 return get_number<std::int32_t, true>(input_format_t::bson, len) && get_bson_binary(len, value) && sax->binary(value);
10092 }
10093
10094 case 0x08: // boolean
10095 {
10096 return sax->boolean(get() != 0);
10097 }
10098
10099 case 0x0A: // null
10100 {
10101 return sax->null();
10102 }
10103
10104 case 0x10: // int32
10105 {
10106 std::int32_t value{};
10107 return get_number<std::int32_t, true>(input_format_t::bson, value) && sax->number_integer(value);
10108 }
10109
10110 case 0x12: // int64
10111 {
10112 std::int64_t value{};
10113 return get_number<std::int64_t, true>(input_format_t::bson, value) && sax->number_integer(value);
10114 }
10115
10116 case 0x11: // uint64
10117 {
10118 std::uint64_t value{};
10119 return get_number<std::uint64_t, true>(input_format_t::bson, value) && sax->number_unsigned(value);
10120 }
10121
10122 default: // anything else not supported (yet)
10123 {
10124 std::array<char, 3> cr{{}};
10125 static_cast<void>((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(element_type))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
10126 const std::string cr_str{cr.data()};
10127 return sax->parse_error(element_type_parse_position, cr_str,
10128 parse_error::create(114, element_type_parse_position, concat("Unsupported BSON record type 0x", cr_str), nullptr));
10129 }
10130 }
10131 }
10132
10133 /*!
10134 @brief Read a BSON element list (as specified in the BSON-spec)
10135
10136 The same binary layout is used for objects and arrays, hence it must be
10137 indicated with the argument @a is_array which one is expected
10138 (true --> array, false --> object).
10139
10140 @param[in] is_array Determines if the element list being read is to be
10141 treated as an object (@a is_array == false), or as an
10142 array (@a is_array == true).
10143 @return whether a valid BSON-object/array was passed to the SAX parser
10144 */
10145 bool parse_bson_element_list(const bool is_array)
10146 {
10147 string_t key;
10148
10149 while (auto element_type = get())
10150 {
10151 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "element list")))
10152 {
10153 return false;
10154 }
10155
10156 const std::size_t element_type_parse_position = chars_read;
10157 if (JSON_HEDLEY_UNLIKELY(!get_bson_cstr(key)))
10158 {
10159 return false;
10160 }
10161
10162 if (!is_array && !sax->key(key))
10163 {
10164 return false;
10165 }
10166
10167 if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_internal(element_type, element_type_parse_position)))
10168 {
10169 return false;
10170 }
10171
10172 // get_bson_cstr only appends
10173 key.clear();
10174 }
10175
10176 return true;
10177 }
10178
10179 /*!
10180 @brief Reads an array from the BSON input and passes it to the SAX-parser.
10181 @return whether a valid BSON-array was passed to the SAX parser
10182 */
10183 bool parse_bson_array()
10184 {
10185 std::int32_t document_size{};
10186 get_number<std::int32_t, true>(input_format_t::bson, document_size);
10187
10188 if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
10189 {
10190 return false;
10191 }
10192
10193 if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/true)))
10194 {
10195 return false;
10196 }
10197
10198 return sax->end_array();
10199 }
10200
10201 //////////
10202 // CBOR //
10203 //////////
10204
10205 /*!
10206 @param[in] get_char whether a new character should be retrieved from the
10207 input (true) or whether the last read character should
10208 be considered instead (false)
10209 @param[in] tag_handler how CBOR tags should be treated
10210
10211 @return whether a valid CBOR value was passed to the SAX parser
10212 */
10213 bool parse_cbor_internal(const bool get_char,
10214 const cbor_tag_handler_t tag_handler)
10215 {
10216 switch (get_char ? get() : current)
10217 {
10218 // EOF
10219 case char_traits<char_type>::eof():
10220 return unexpect_eof(input_format_t::cbor, "value");
10221
10222 // Integer 0x00..0x17 (0..23)
10223 case 0x00:
10224 case 0x01:
10225 case 0x02:
10226 case 0x03:
10227 case 0x04:
10228 case 0x05:
10229 case 0x06:
10230 case 0x07:
10231 case 0x08:
10232 case 0x09:
10233 case 0x0A:
10234 case 0x0B:
10235 case 0x0C:
10236 case 0x0D:
10237 case 0x0E:
10238 case 0x0F:
10239 case 0x10:
10240 case 0x11:
10241 case 0x12:
10242 case 0x13:
10243 case 0x14:
10244 case 0x15:
10245 case 0x16:
10246 case 0x17:
10247 return sax->number_unsigned(static_cast<number_unsigned_t>(current));
10248
10249 case 0x18: // Unsigned integer (one-byte uint8_t follows)
10250 {
10251 std::uint8_t number{};
10252 return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
10253 }
10254
10255 case 0x19: // Unsigned integer (two-byte uint16_t follows)
10256 {
10257 std::uint16_t number{};
10258 return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
10259 }
10260
10261 case 0x1A: // Unsigned integer (four-byte uint32_t follows)
10262 {
10263 std::uint32_t number{};
10264 return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
10265 }
10266
10267 case 0x1B: // Unsigned integer (eight-byte uint64_t follows)
10268 {
10269 std::uint64_t number{};
10270 return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
10271 }
10272
10273 // Negative integer -1-0x00..-1-0x17 (-1..-24)
10274 case 0x20:
10275 case 0x21:
10276 case 0x22:
10277 case 0x23:
10278 case 0x24:
10279 case 0x25:
10280 case 0x26:
10281 case 0x27:
10282 case 0x28:
10283 case 0x29:
10284 case 0x2A:
10285 case 0x2B:
10286 case 0x2C:
10287 case 0x2D:
10288 case 0x2E:
10289 case 0x2F:
10290 case 0x30:
10291 case 0x31:
10292 case 0x32:
10293 case 0x33:
10294 case 0x34:
10295 case 0x35:
10296 case 0x36:
10297 case 0x37:
10298 return sax->number_integer(static_cast<std::int8_t>(0x20 - 1 - current));
10299
10300 case 0x38: // Negative integer (one-byte uint8_t follows)
10301 {
10302 std::uint8_t number{};
10303 return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
10304 }
10305
10306 case 0x39: // Negative integer -1-n (two-byte uint16_t follows)
10307 {
10308 std::uint16_t number{};
10309 return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
10310 }
10311
10312 case 0x3A: // Negative integer -1-n (four-byte uint32_t follows)
10313 {
10314 std::uint32_t number{};
10315 return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
10316 }
10317
10318 case 0x3B: // Negative integer -1-n (eight-byte uint64_t follows)
10319 {
10320 std::uint64_t number{};
10321 return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1)
10322 - static_cast<number_integer_t>(number));
10323 }
10324
10325 // Binary data (0x00..0x17 bytes follow)
10326 case 0x40:
10327 case 0x41:
10328 case 0x42:
10329 case 0x43:
10330 case 0x44:
10331 case 0x45:
10332 case 0x46:
10333 case 0x47:
10334 case 0x48:
10335 case 0x49:
10336 case 0x4A:
10337 case 0x4B:
10338 case 0x4C:
10339 case 0x4D:
10340 case 0x4E:
10341 case 0x4F:
10342 case 0x50:
10343 case 0x51:
10344 case 0x52:
10345 case 0x53:
10346 case 0x54:
10347 case 0x55:
10348 case 0x56:
10349 case 0x57:
10350 case 0x58: // Binary data (one-byte uint8_t for n follows)
10351 case 0x59: // Binary data (two-byte uint16_t for n follow)
10352 case 0x5A: // Binary data (four-byte uint32_t for n follow)
10353 case 0x5B: // Binary data (eight-byte uint64_t for n follow)
10354 case 0x5F: // Binary data (indefinite length)
10355 {
10356 binary_t b;
10357 return get_cbor_binary(b) && sax->binary(b);
10358 }
10359
10360 // UTF-8 string (0x00..0x17 bytes follow)
10361 case 0x60:
10362 case 0x61:
10363 case 0x62:
10364 case 0x63:
10365 case 0x64:
10366 case 0x65:
10367 case 0x66:
10368 case 0x67:
10369 case 0x68:
10370 case 0x69:
10371 case 0x6A:
10372 case 0x6B:
10373 case 0x6C:
10374 case 0x6D:
10375 case 0x6E:
10376 case 0x6F:
10377 case 0x70:
10378 case 0x71:
10379 case 0x72:
10380 case 0x73:
10381 case 0x74:
10382 case 0x75:
10383 case 0x76:
10384 case 0x77:
10385 case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
10386 case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
10387 case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
10388 case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
10389 case 0x7F: // UTF-8 string (indefinite length)
10390 {
10391 string_t s;
10392 return get_cbor_string(s) && sax->string(s);
10393 }
10394
10395 // array (0x00..0x17 data items follow)
10396 case 0x80:
10397 case 0x81:
10398 case 0x82:
10399 case 0x83:
10400 case 0x84:
10401 case 0x85:
10402 case 0x86:
10403 case 0x87:
10404 case 0x88:
10405 case 0x89:
10406 case 0x8A:
10407 case 0x8B:
10408 case 0x8C:
10409 case 0x8D:
10410 case 0x8E:
10411 case 0x8F:
10412 case 0x90:
10413 case 0x91:
10414 case 0x92:
10415 case 0x93:
10416 case 0x94:
10417 case 0x95:
10418 case 0x96:
10419 case 0x97:
10420 return get_cbor_array(
10421 conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
10422
10423 case 0x98: // array (one-byte uint8_t for n follows)
10424 {
10425 std::uint8_t len{};
10426 return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
10427 }
10428
10429 case 0x99: // array (two-byte uint16_t for n follow)
10430 {
10431 std::uint16_t len{};
10432 return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
10433 }
10434
10435 case 0x9A: // array (four-byte uint32_t for n follow)
10436 {
10437 std::uint32_t len{};
10438 return get_number(input_format_t::cbor, len) && get_cbor_array(conditional_static_cast<std::size_t>(len), tag_handler);
10439 }
10440
10441 case 0x9B: // array (eight-byte uint64_t for n follow)
10442 {
10443 std::uint64_t len{};
10444 return get_number(input_format_t::cbor, len) && get_cbor_array(conditional_static_cast<std::size_t>(len), tag_handler);
10445 }
10446
10447 case 0x9F: // array (indefinite length)
10448 return get_cbor_array(detail::unknown_size(), tag_handler);
10449
10450 // map (0x00..0x17 pairs of data items follow)
10451 case 0xA0:
10452 case 0xA1:
10453 case 0xA2:
10454 case 0xA3:
10455 case 0xA4:
10456 case 0xA5:
10457 case 0xA6:
10458 case 0xA7:
10459 case 0xA8:
10460 case 0xA9:
10461 case 0xAA:
10462 case 0xAB:
10463 case 0xAC:
10464 case 0xAD:
10465 case 0xAE:
10466 case 0xAF:
10467 case 0xB0:
10468 case 0xB1:
10469 case 0xB2:
10470 case 0xB3:
10471 case 0xB4:
10472 case 0xB5:
10473 case 0xB6:
10474 case 0xB7:
10475 return get_cbor_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
10476
10477 case 0xB8: // map (one-byte uint8_t for n follows)
10478 {
10479 std::uint8_t len{};
10480 return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
10481 }
10482
10483 case 0xB9: // map (two-byte uint16_t for n follow)
10484 {
10485 std::uint16_t len{};
10486 return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
10487 }
10488
10489 case 0xBA: // map (four-byte uint32_t for n follow)
10490 {
10491 std::uint32_t len{};
10492 return get_number(input_format_t::cbor, len) && get_cbor_object(conditional_static_cast<std::size_t>(len), tag_handler);
10493 }
10494
10495 case 0xBB: // map (eight-byte uint64_t for n follow)
10496 {
10497 std::uint64_t len{};
10498 return get_number(input_format_t::cbor, len) && get_cbor_object(conditional_static_cast<std::size_t>(len), tag_handler);
10499 }
10500
10501 case 0xBF: // map (indefinite length)
10502 return get_cbor_object(detail::unknown_size(), tag_handler);
10503
10504 case 0xC6: // tagged item
10505 case 0xC7:
10506 case 0xC8:
10507 case 0xC9:
10508 case 0xCA:
10509 case 0xCB:
10510 case 0xCC:
10511 case 0xCD:
10512 case 0xCE:
10513 case 0xCF:
10514 case 0xD0:
10515 case 0xD1:
10516 case 0xD2:
10517 case 0xD3:
10518 case 0xD4:
10519 case 0xD8: // tagged item (1 bytes follow)
10520 case 0xD9: // tagged item (2 bytes follow)
10521 case 0xDA: // tagged item (4 bytes follow)
10522 case 0xDB: // tagged item (8 bytes follow)
10523 {
10524 switch (tag_handler)
10525 {
10526 case cbor_tag_handler_t::error:
10527 {
10528 auto last_token = get_token_string();
10529 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
10530 exception_message(input_format_t::cbor, concat("invalid byte: 0x", last_token), "value"), nullptr));
10531 }
10532
10533 case cbor_tag_handler_t::ignore:
10534 {
10535 // ignore binary subtype
10536 switch (current)
10537 {
10538 case 0xD8:
10539 {
10540 std::uint8_t subtype_to_ignore{};
10541 get_number(input_format_t::cbor, subtype_to_ignore);
10542 break;
10543 }
10544 case 0xD9:
10545 {
10546 std::uint16_t subtype_to_ignore{};
10547 get_number(input_format_t::cbor, subtype_to_ignore);
10548 break;
10549 }
10550 case 0xDA:
10551 {
10552 std::uint32_t subtype_to_ignore{};
10553 get_number(input_format_t::cbor, subtype_to_ignore);
10554 break;
10555 }
10556 case 0xDB:
10557 {
10558 std::uint64_t subtype_to_ignore{};
10559 get_number(input_format_t::cbor, subtype_to_ignore);
10560 break;
10561 }
10562 default:
10563 break;
10564 }
10565 return parse_cbor_internal(true, tag_handler);
10566 }
10567
10568 case cbor_tag_handler_t::store:
10569 {
10570 binary_t b;
10571 // use binary subtype and store in binary container
10572 switch (current)
10573 {
10574 case 0xD8:
10575 {
10576 std::uint8_t subtype{};
10577 get_number(input_format_t::cbor, subtype);
10578 b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
10579 break;
10580 }
10581 case 0xD9:
10582 {
10583 std::uint16_t subtype{};
10584 get_number(input_format_t::cbor, subtype);
10585 b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
10586 break;
10587 }
10588 case 0xDA:
10589 {
10590 std::uint32_t subtype{};
10591 get_number(input_format_t::cbor, subtype);
10592 b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
10593 break;
10594 }
10595 case 0xDB:
10596 {
10597 std::uint64_t subtype{};
10598 get_number(input_format_t::cbor, subtype);
10599 b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
10600 break;
10601 }
10602 default:
10603 return parse_cbor_internal(true, tag_handler);
10604 }
10605 get();
10606 return get_cbor_binary(b) && sax->binary(b);
10607 }
10608
10609 default: // LCOV_EXCL_LINE
10610 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
10611 return false; // LCOV_EXCL_LINE
10612 }
10613 }
10614
10615 case 0xF4: // false
10616 return sax->boolean(false);
10617
10618 case 0xF5: // true
10619 return sax->boolean(true);
10620
10621 case 0xF6: // null
10622 return sax->null();
10623
10624 case 0xF9: // Half-Precision Float (two-byte IEEE 754)
10625 {
10626 const auto byte1_raw = get();
10627 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
10628 {
10629 return false;
10630 }
10631 const auto byte2_raw = get();
10632 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
10633 {
10634 return false;
10635 }
10636
10637 const auto byte1 = static_cast<unsigned char>(byte1_raw);
10638 const auto byte2 = static_cast<unsigned char>(byte2_raw);
10639
10640 // code from RFC 7049, Appendix D, Figure 3:
10641 // As half-precision floating-point numbers were only added
10642 // to IEEE 754 in 2008, today's programming platforms often
10643 // still only have limited support for them. It is very
10644 // easy to include at least decoding support for them even
10645 // without such support. An example of a small decoder for
10646 // half-precision floating-point numbers in the C language
10647 // is shown in Fig. 3.
10648 const auto half = static_cast<unsigned int>((byte1 << 8u) + byte2);
10649 const double val = [&half]
10650 {
10651 const int exp = (half >> 10u) & 0x1Fu;
10652 const unsigned int mant = half & 0x3FFu;
10653 JSON_ASSERT(0 <= exp&& exp <= 32);
10654 JSON_ASSERT(mant <= 1024);
10655 switch (exp)
10656 {
10657 case 0:
10658 return std::ldexp(mant, -24);
10659 case 31:
10660 return (mant == 0)
10661 ? std::numeric_limits<double>::infinity()
10662 : std::numeric_limits<double>::quiet_NaN();
10663 default:
10664 return std::ldexp(mant + 1024, exp - 25);
10665 }
10666 }();
10667 return sax->number_float((half & 0x8000u) != 0
10668 ? static_cast<number_float_t>(-val)
10669 : static_cast<number_float_t>(val), "");
10670 }
10671
10672 case 0xFA: // Single-Precision Float (four-byte IEEE 754)
10673 {
10674 float number{};
10675 return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
10676 }
10677
10678 case 0xFB: // Double-Precision Float (eight-byte IEEE 754)
10679 {
10680 double number{};
10681 return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
10682 }
10683
10684 default: // anything else (0xFF is handled inside the other types)
10685 {
10686 auto last_token = get_token_string();
10687 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
10688 exception_message(input_format_t::cbor, concat("invalid byte: 0x", last_token), "value"), nullptr));
10689 }
10690 }
10691 }
10692
10693 /*!
10694 @brief reads a CBOR string
10695
10696 This function first reads starting bytes to determine the expected
10697 string length and then copies this number of bytes into a string.
10698 Additionally, CBOR's strings with indefinite lengths are supported.
10699
10700 @param[out] result created string
10701
10702 @return whether string creation completed
10703 */
10704 bool get_cbor_string(string_t& result)
10705 {
10706 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "string")))
10707 {
10708 return false;
10709 }
10710
10711 switch (current)
10712 {
10713 // UTF-8 string (0x00..0x17 bytes follow)
10714 case 0x60:
10715 case 0x61:
10716 case 0x62:
10717 case 0x63:
10718 case 0x64:
10719 case 0x65:
10720 case 0x66:
10721 case 0x67:
10722 case 0x68:
10723 case 0x69:
10724 case 0x6A:
10725 case 0x6B:
10726 case 0x6C:
10727 case 0x6D:
10728 case 0x6E:
10729 case 0x6F:
10730 case 0x70:
10731 case 0x71:
10732 case 0x72:
10733 case 0x73:
10734 case 0x74:
10735 case 0x75:
10736 case 0x76:
10737 case 0x77:
10738 {
10739 return get_string(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
10740 }
10741
10742 case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
10743 {
10744 std::uint8_t len{};
10745 return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
10746 }
10747
10748 case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
10749 {
10750 std::uint16_t len{};
10751 return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
10752 }
10753
10754 case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
10755 {
10756 std::uint32_t len{};
10757 return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
10758 }
10759
10760 case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
10761 {
10762 std::uint64_t len{};
10763 return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
10764 }
10765
10766 case 0x7F: // UTF-8 string (indefinite length)
10767 {
10768 while (get() != 0xFF)
10769 {
10770 string_t chunk;
10771 if (!get_cbor_string(chunk))
10772 {
10773 return false;
10774 }
10775 result.append(chunk);
10776 }
10777 return true;
10778 }
10779
10780 default:
10781 {
10782 auto last_token = get_token_string();
10783 return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
10784 exception_message(input_format_t::cbor, concat("expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x", last_token), "string"), nullptr));
10785 }
10786 }
10787 }
10788
10789 /*!
10790 @brief reads a CBOR byte array
10791
10792 This function first reads starting bytes to determine the expected
10793 byte array length and then copies this number of bytes into the byte array.
10794 Additionally, CBOR's byte arrays with indefinite lengths are supported.
10795
10796 @param[out] result created byte array
10797
10798 @return whether byte array creation completed
10799 */
10800 bool get_cbor_binary(binary_t& result)
10801 {
10802 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "binary")))
10803 {
10804 return false;
10805 }
10806
10807 switch (current)
10808 {
10809 // Binary data (0x00..0x17 bytes follow)
10810 case 0x40:
10811 case 0x41:
10812 case 0x42:
10813 case 0x43:
10814 case 0x44:
10815 case 0x45:
10816 case 0x46:
10817 case 0x47:
10818 case 0x48:
10819 case 0x49:
10820 case 0x4A:
10821 case 0x4B:
10822 case 0x4C:
10823 case 0x4D:
10824 case 0x4E:
10825 case 0x4F:
10826 case 0x50:
10827 case 0x51:
10828 case 0x52:
10829 case 0x53:
10830 case 0x54:
10831 case 0x55:
10832 case 0x56:
10833 case 0x57:
10834 {
10835 return get_binary(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
10836 }
10837
10838 case 0x58: // Binary data (one-byte uint8_t for n follows)
10839 {
10840 std::uint8_t len{};
10841 return get_number(input_format_t::cbor, len) &&
10842 get_binary(input_format_t::cbor, len, result);
10843 }
10844
10845 case 0x59: // Binary data (two-byte uint16_t for n follow)
10846 {
10847 std::uint16_t len{};
10848 return get_number(input_format_t::cbor, len) &&
10849 get_binary(input_format_t::cbor, len, result);
10850 }
10851
10852 case 0x5A: // Binary data (four-byte uint32_t for n follow)
10853 {
10854 std::uint32_t len{};
10855 return get_number(input_format_t::cbor, len) &&
10856 get_binary(input_format_t::cbor, len, result);
10857 }
10858
10859 case 0x5B: // Binary data (eight-byte uint64_t for n follow)
10860 {
10861 std::uint64_t len{};
10862 return get_number(input_format_t::cbor, len) &&
10863 get_binary(input_format_t::cbor, len, result);
10864 }
10865
10866 case 0x5F: // Binary data (indefinite length)
10867 {
10868 while (get() != 0xFF)
10869 {
10870 binary_t chunk;
10871 if (!get_cbor_binary(chunk))
10872 {
10873 return false;
10874 }
10875 result.insert(result.end(), chunk.begin(), chunk.end());
10876 }
10877 return true;
10878 }
10879
10880 default:
10881 {
10882 auto last_token = get_token_string();
10883 return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
10884 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));
10885 }
10886 }
10887 }
10888
10889 /*!
10890 @param[in] len the length of the array or detail::unknown_size() for an
10891 array of indefinite size
10892 @param[in] tag_handler how CBOR tags should be treated
10893 @return whether array creation completed
10894 */
10895 bool get_cbor_array(const std::size_t len,
10896 const cbor_tag_handler_t tag_handler)
10897 {
10898 if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
10899 {
10900 return false;
10901 }
10902
10903 if (len != detail::unknown_size())
10904 {
10905 for (std::size_t i = 0; i < len; ++i)
10906 {
10907 if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
10908 {
10909 return false;
10910 }
10911 }
10912 }
10913 else
10914 {
10915 while (get() != 0xFF)
10916 {
10917 if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(false, tag_handler)))
10918 {
10919 return false;
10920 }
10921 }
10922 }
10923
10924 return sax->end_array();
10925 }
10926
10927 /*!
10928 @param[in] len the length of the object or detail::unknown_size() for an
10929 object of indefinite size
10930 @param[in] tag_handler how CBOR tags should be treated
10931 @return whether object creation completed
10932 */
10933 bool get_cbor_object(const std::size_t len,
10934 const cbor_tag_handler_t tag_handler)
10935 {
10936 if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
10937 {
10938 return false;
10939 }
10940
10941 if (len != 0)
10942 {
10943 string_t key;
10944 if (len != detail::unknown_size())
10945 {
10946 for (std::size_t i = 0; i < len; ++i)
10947 {
10948 get();
10949 if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
10950 {
10951 return false;
10952 }
10953
10954 if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
10955 {
10956 return false;
10957 }
10958 key.clear();
10959 }
10960 }
10961 else
10962 {
10963 while (get() != 0xFF)
10964 {
10965 if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
10966 {
10967 return false;
10968 }
10969
10970 if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
10971 {
10972 return false;
10973 }
10974 key.clear();
10975 }
10976 }
10977 }
10978
10979 return sax->end_object();
10980 }
10981
10982 /////////////
10983 // MsgPack //
10984 /////////////
10985
10986 /*!
10987 @return whether a valid MessagePack value was passed to the SAX parser
10988 */
10989 bool parse_msgpack_internal()
10990 {
10991 switch (get())
10992 {
10993 // EOF
10994 case char_traits<char_type>::eof():
10995 return unexpect_eof(input_format_t::msgpack, "value");
10996
10997 // positive fixint
10998 case 0x00:
10999 case 0x01:
11000 case 0x02:
11001 case 0x03:
11002 case 0x04:
11003 case 0x05:
11004 case 0x06:
11005 case 0x07:
11006 case 0x08:
11007 case 0x09:
11008 case 0x0A:
11009 case 0x0B:
11010 case 0x0C:
11011 case 0x0D:
11012 case 0x0E:
11013 case 0x0F:
11014 case 0x10:
11015 case 0x11:
11016 case 0x12:
11017 case 0x13:
11018 case 0x14:
11019 case 0x15:
11020 case 0x16:
11021 case 0x17:
11022 case 0x18:
11023 case 0x19:
11024 case 0x1A:
11025 case 0x1B:
11026 case 0x1C:
11027 case 0x1D:
11028 case 0x1E:
11029 case 0x1F:
11030 case 0x20:
11031 case 0x21:
11032 case 0x22:
11033 case 0x23:
11034 case 0x24:
11035 case 0x25:
11036 case 0x26:
11037 case 0x27:
11038 case 0x28:
11039 case 0x29:
11040 case 0x2A:
11041 case 0x2B:
11042 case 0x2C:
11043 case 0x2D:
11044 case 0x2E:
11045 case 0x2F:
11046 case 0x30:
11047 case 0x31:
11048 case 0x32:
11049 case 0x33:
11050 case 0x34:
11051 case 0x35:
11052 case 0x36:
11053 case 0x37:
11054 case 0x38:
11055 case 0x39:
11056 case 0x3A:
11057 case 0x3B:
11058 case 0x3C:
11059 case 0x3D:
11060 case 0x3E:
11061 case 0x3F:
11062 case 0x40:
11063 case 0x41:
11064 case 0x42:
11065 case 0x43:
11066 case 0x44:
11067 case 0x45:
11068 case 0x46:
11069 case 0x47:
11070 case 0x48:
11071 case 0x49:
11072 case 0x4A:
11073 case 0x4B:
11074 case 0x4C:
11075 case 0x4D:
11076 case 0x4E:
11077 case 0x4F:
11078 case 0x50:
11079 case 0x51:
11080 case 0x52:
11081 case 0x53:
11082 case 0x54:
11083 case 0x55:
11084 case 0x56:
11085 case 0x57:
11086 case 0x58:
11087 case 0x59:
11088 case 0x5A:
11089 case 0x5B:
11090 case 0x5C:
11091 case 0x5D:
11092 case 0x5E:
11093 case 0x5F:
11094 case 0x60:
11095 case 0x61:
11096 case 0x62:
11097 case 0x63:
11098 case 0x64:
11099 case 0x65:
11100 case 0x66:
11101 case 0x67:
11102 case 0x68:
11103 case 0x69:
11104 case 0x6A:
11105 case 0x6B:
11106 case 0x6C:
11107 case 0x6D:
11108 case 0x6E:
11109 case 0x6F:
11110 case 0x70:
11111 case 0x71:
11112 case 0x72:
11113 case 0x73:
11114 case 0x74:
11115 case 0x75:
11116 case 0x76:
11117 case 0x77:
11118 case 0x78:
11119 case 0x79:
11120 case 0x7A:
11121 case 0x7B:
11122 case 0x7C:
11123 case 0x7D:
11124 case 0x7E:
11125 case 0x7F:
11126 return sax->number_unsigned(static_cast<number_unsigned_t>(current));
11127
11128 // fixmap
11129 case 0x80:
11130 case 0x81:
11131 case 0x82:
11132 case 0x83:
11133 case 0x84:
11134 case 0x85:
11135 case 0x86:
11136 case 0x87:
11137 case 0x88:
11138 case 0x89:
11139 case 0x8A:
11140 case 0x8B:
11141 case 0x8C:
11142 case 0x8D:
11143 case 0x8E:
11144 case 0x8F:
11145 return get_msgpack_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
11146
11147 // fixarray
11148 case 0x90:
11149 case 0x91:
11150 case 0x92:
11151 case 0x93:
11152 case 0x94:
11153 case 0x95:
11154 case 0x96:
11155 case 0x97:
11156 case 0x98:
11157 case 0x99:
11158 case 0x9A:
11159 case 0x9B:
11160 case 0x9C:
11161 case 0x9D:
11162 case 0x9E:
11163 case 0x9F:
11164 return get_msgpack_array(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
11165
11166 // fixstr
11167 case 0xA0:
11168 case 0xA1:
11169 case 0xA2:
11170 case 0xA3:
11171 case 0xA4:
11172 case 0xA5:
11173 case 0xA6:
11174 case 0xA7:
11175 case 0xA8:
11176 case 0xA9:
11177 case 0xAA:
11178 case 0xAB:
11179 case 0xAC:
11180 case 0xAD:
11181 case 0xAE:
11182 case 0xAF:
11183 case 0xB0:
11184 case 0xB1:
11185 case 0xB2:
11186 case 0xB3:
11187 case 0xB4:
11188 case 0xB5:
11189 case 0xB6:
11190 case 0xB7:
11191 case 0xB8:
11192 case 0xB9:
11193 case 0xBA:
11194 case 0xBB:
11195 case 0xBC:
11196 case 0xBD:
11197 case 0xBE:
11198 case 0xBF:
11199 case 0xD9: // str 8
11200 case 0xDA: // str 16
11201 case 0xDB: // str 32
11202 {
11203 string_t s;
11204 return get_msgpack_string(s) && sax->string(s);
11205 }
11206
11207 case 0xC0: // nil
11208 return sax->null();
11209
11210 case 0xC2: // false
11211 return sax->boolean(false);
11212
11213 case 0xC3: // true
11214 return sax->boolean(true);
11215
11216 case 0xC4: // bin 8
11217 case 0xC5: // bin 16
11218 case 0xC6: // bin 32
11219 case 0xC7: // ext 8
11220 case 0xC8: // ext 16
11221 case 0xC9: // ext 32
11222 case 0xD4: // fixext 1
11223 case 0xD5: // fixext 2
11224 case 0xD6: // fixext 4
11225 case 0xD7: // fixext 8
11226 case 0xD8: // fixext 16
11227 {
11228 binary_t b;
11229 return get_msgpack_binary(b) && sax->binary(b);
11230 }
11231
11232 case 0xCA: // float 32
11233 {
11234 float number{};
11235 return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast<number_float_t>(number), "");
11236 }
11237
11238 case 0xCB: // float 64
11239 {
11240 double number{};
11241 return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast<number_float_t>(number), "");
11242 }
11243
11244 case 0xCC: // uint 8
11245 {
11246 std::uint8_t number{};
11247 return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
11248 }
11249
11250 case 0xCD: // uint 16
11251 {
11252 std::uint16_t number{};
11253 return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
11254 }
11255
11256 case 0xCE: // uint 32
11257 {
11258 std::uint32_t number{};
11259 return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
11260 }
11261
11262 case 0xCF: // uint 64
11263 {
11264 std::uint64_t number{};
11265 return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
11266 }
11267
11268 case 0xD0: // int 8
11269 {
11270 std::int8_t number{};
11271 return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
11272 }
11273
11274 case 0xD1: // int 16
11275 {
11276 std::int16_t number{};
11277 return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
11278 }
11279
11280 case 0xD2: // int 32
11281 {
11282 std::int32_t number{};
11283 return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
11284 }
11285
11286 case 0xD3: // int 64
11287 {
11288 std::int64_t number{};
11289 return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
11290 }
11291
11292 case 0xDC: // array 16
11293 {
11294 std::uint16_t len{};
11295 return get_number(input_format_t::msgpack, len) && get_msgpack_array(static_cast<std::size_t>(len));
11296 }
11297
11298 case 0xDD: // array 32
11299 {
11300 std::uint32_t len{};
11301 return get_number(input_format_t::msgpack, len) && get_msgpack_array(conditional_static_cast<std::size_t>(len));
11302 }
11303
11304 case 0xDE: // map 16
11305 {
11306 std::uint16_t len{};
11307 return get_number(input_format_t::msgpack, len) && get_msgpack_object(static_cast<std::size_t>(len));
11308 }
11309
11310 case 0xDF: // map 32
11311 {
11312 std::uint32_t len{};
11313 return get_number(input_format_t::msgpack, len) && get_msgpack_object(conditional_static_cast<std::size_t>(len));
11314 }
11315
11316 // negative fixint
11317 case 0xE0:
11318 case 0xE1:
11319 case 0xE2:
11320 case 0xE3:
11321 case 0xE4:
11322 case 0xE5:
11323 case 0xE6:
11324 case 0xE7:
11325 case 0xE8:
11326 case 0xE9:
11327 case 0xEA:
11328 case 0xEB:
11329 case 0xEC:
11330 case 0xED:
11331 case 0xEE:
11332 case 0xEF:
11333 case 0xF0:
11334 case 0xF1:
11335 case 0xF2:
11336 case 0xF3:
11337 case 0xF4:
11338 case 0xF5:
11339 case 0xF6:
11340 case 0xF7:
11341 case 0xF8:
11342 case 0xF9:
11343 case 0xFA:
11344 case 0xFB:
11345 case 0xFC:
11346 case 0xFD:
11347 case 0xFE:
11348 case 0xFF:
11349 return sax->number_integer(static_cast<std::int8_t>(current));
11350
11351 default: // anything else
11352 {
11353 auto last_token = get_token_string();
11354 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
11355 exception_message(input_format_t::msgpack, concat("invalid byte: 0x", last_token), "value"), nullptr));
11356 }
11357 }
11358 }
11359
11360 /*!
11361 @brief reads a MessagePack string
11362
11363 This function first reads starting bytes to determine the expected
11364 string length and then copies this number of bytes into a string.
11365
11366 @param[out] result created string
11367
11368 @return whether string creation completed
11369 */
11370 bool get_msgpack_string(string_t& result)
11371 {
11372 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::msgpack, "string")))
11373 {
11374 return false;
11375 }
11376
11377 switch (current)
11378 {
11379 // fixstr
11380 case 0xA0:
11381 case 0xA1:
11382 case 0xA2:
11383 case 0xA3:
11384 case 0xA4:
11385 case 0xA5:
11386 case 0xA6:
11387 case 0xA7:
11388 case 0xA8:
11389 case 0xA9:
11390 case 0xAA:
11391 case 0xAB:
11392 case 0xAC:
11393 case 0xAD:
11394 case 0xAE:
11395 case 0xAF:
11396 case 0xB0:
11397 case 0xB1:
11398 case 0xB2:
11399 case 0xB3:
11400 case 0xB4:
11401 case 0xB5:
11402 case 0xB6:
11403 case 0xB7:
11404 case 0xB8:
11405 case 0xB9:
11406 case 0xBA:
11407 case 0xBB:
11408 case 0xBC:
11409 case 0xBD:
11410 case 0xBE:
11411 case 0xBF:
11412 {
11413 return get_string(input_format_t::msgpack, static_cast<unsigned int>(current) & 0x1Fu, result);
11414 }
11415
11416 case 0xD9: // str 8
11417 {
11418 std::uint8_t len{};
11419 return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
11420 }
11421
11422 case 0xDA: // str 16
11423 {
11424 std::uint16_t len{};
11425 return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
11426 }
11427
11428 case 0xDB: // str 32
11429 {
11430 std::uint32_t len{};
11431 return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
11432 }
11433
11434 default:
11435 {
11436 auto last_token = get_token_string();
11437 return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
11438 exception_message(input_format_t::msgpack, concat("expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x", last_token), "string"), nullptr));
11439 }
11440 }
11441 }
11442
11443 /*!
11444 @brief reads a MessagePack byte array
11445
11446 This function first reads starting bytes to determine the expected
11447 byte array length and then copies this number of bytes into a byte array.
11448
11449 @param[out] result created byte array
11450
11451 @return whether byte array creation completed
11452 */
11453 bool get_msgpack_binary(binary_t& result)
11454 {
11455 // helper function to set the subtype
11456 auto assign_and_return_true = [&result](std::int8_t subtype)
11457 {
11458 result.set_subtype(static_cast<std::uint8_t>(subtype));
11459 return true;
11460 };
11461
11462 switch (current)
11463 {
11464 case 0xC4: // bin 8
11465 {
11466 std::uint8_t len{};
11467 return get_number(input_format_t::msgpack, len) &&
11468 get_binary(input_format_t::msgpack, len, result);
11469 }
11470
11471 case 0xC5: // bin 16
11472 {
11473 std::uint16_t len{};
11474 return get_number(input_format_t::msgpack, len) &&
11475 get_binary(input_format_t::msgpack, len, result);
11476 }
11477
11478 case 0xC6: // bin 32
11479 {
11480 std::uint32_t len{};
11481 return get_number(input_format_t::msgpack, len) &&
11482 get_binary(input_format_t::msgpack, len, result);
11483 }
11484
11485 case 0xC7: // ext 8
11486 {
11487 std::uint8_t len{};
11488 std::int8_t subtype{};
11489 return get_number(input_format_t::msgpack, len) &&
11490 get_number(input_format_t::msgpack, subtype) &&
11491 get_binary(input_format_t::msgpack, len, result) &&
11492 assign_and_return_true(subtype);
11493 }
11494
11495 case 0xC8: // ext 16
11496 {
11497 std::uint16_t len{};
11498 std::int8_t subtype{};
11499 return get_number(input_format_t::msgpack, len) &&
11500 get_number(input_format_t::msgpack, subtype) &&
11501 get_binary(input_format_t::msgpack, len, result) &&
11502 assign_and_return_true(subtype);
11503 }
11504
11505 case 0xC9: // ext 32
11506 {
11507 std::uint32_t len{};
11508 std::int8_t subtype{};
11509 return get_number(input_format_t::msgpack, len) &&
11510 get_number(input_format_t::msgpack, subtype) &&
11511 get_binary(input_format_t::msgpack, len, result) &&
11512 assign_and_return_true(subtype);
11513 }
11514
11515 case 0xD4: // fixext 1
11516 {
11517 std::int8_t subtype{};
11518 return get_number(input_format_t::msgpack, subtype) &&
11519 get_binary(input_format_t::msgpack, 1, result) &&
11520 assign_and_return_true(subtype);
11521 }
11522
11523 case 0xD5: // fixext 2
11524 {
11525 std::int8_t subtype{};
11526 return get_number(input_format_t::msgpack, subtype) &&
11527 get_binary(input_format_t::msgpack, 2, result) &&
11528 assign_and_return_true(subtype);
11529 }
11530
11531 case 0xD6: // fixext 4
11532 {
11533 std::int8_t subtype{};
11534 return get_number(input_format_t::msgpack, subtype) &&
11535 get_binary(input_format_t::msgpack, 4, result) &&
11536 assign_and_return_true(subtype);
11537 }
11538
11539 case 0xD7: // fixext 8
11540 {
11541 std::int8_t subtype{};
11542 return get_number(input_format_t::msgpack, subtype) &&
11543 get_binary(input_format_t::msgpack, 8, result) &&
11544 assign_and_return_true(subtype);
11545 }
11546
11547 case 0xD8: // fixext 16
11548 {
11549 std::int8_t subtype{};
11550 return get_number(input_format_t::msgpack, subtype) &&
11551 get_binary(input_format_t::msgpack, 16, result) &&
11552 assign_and_return_true(subtype);
11553 }
11554
11555 default: // LCOV_EXCL_LINE
11556 return false; // LCOV_EXCL_LINE
11557 }
11558 }
11559
11560 /*!
11561 @param[in] len the length of the array
11562 @return whether array creation completed
11563 */
11564 bool get_msgpack_array(const std::size_t len)
11565 {
11566 if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
11567 {
11568 return false;
11569 }
11570
11571 for (std::size_t i = 0; i < len; ++i)
11572 {
11573 if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
11574 {
11575 return false;
11576 }
11577 }
11578
11579 return sax->end_array();
11580 }
11581
11582 /*!
11583 @param[in] len the length of the object
11584 @return whether object creation completed
11585 */
11586 bool get_msgpack_object(const std::size_t len)
11587 {
11588 if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
11589 {
11590 return false;
11591 }
11592
11593 string_t key;
11594 for (std::size_t i = 0; i < len; ++i)
11595 {
11596 get();
11597 if (JSON_HEDLEY_UNLIKELY(!get_msgpack_string(key) || !sax->key(key)))
11598 {
11599 return false;
11600 }
11601
11602 if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
11603 {
11604 return false;
11605 }
11606 key.clear();
11607 }
11608
11609 return sax->end_object();
11610 }
11611
11612 ////////////
11613 // UBJSON //
11614 ////////////
11615
11616 /*!
11617 @param[in] get_char whether a new character should be retrieved from the
11618 input (true, default) or whether the last read
11619 character should be considered instead
11620
11621 @return whether a valid UBJSON value was passed to the SAX parser
11622 */
11623 bool parse_ubjson_internal(const bool get_char = true)
11624 {
11625 return get_ubjson_value(get_char ? get_ignore_noop() : current);
11626 }
11627
11628 /*!
11629 @brief reads a UBJSON string
11630
11631 This function is either called after reading the 'S' byte explicitly
11632 indicating a string, or in case of an object key where the 'S' byte can be
11633 left out.
11634
11635 @param[out] result created string
11636 @param[in] get_char whether a new character should be retrieved from the
11637 input (true, default) or whether the last read
11638 character should be considered instead
11639
11640 @return whether string creation completed
11641 */
11642 bool get_ubjson_string(string_t& result, const bool get_char = true)
11643 {
11644 if (get_char)
11645 {
11646 get(); // TODO(niels): may we ignore N here?
11647 }
11648
11649 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "value")))
11650 {
11651 return false;
11652 }
11653
11654 switch (current)
11655 {
11656 case 'U':
11657 {
11658 std::uint8_t len{};
11659 return get_number(input_format, len) && get_string(input_format, len, result);
11660 }
11661
11662 case 'i':
11663 {
11664 std::int8_t len{};
11665 return get_number(input_format, len) && get_string(input_format, len, result);
11666 }
11667
11668 case 'I':
11669 {
11670 std::int16_t len{};
11671 return get_number(input_format, len) && get_string(input_format, len, result);
11672 }
11673
11674 case 'l':
11675 {
11676 std::int32_t len{};
11677 return get_number(input_format, len) && get_string(input_format, len, result);
11678 }
11679
11680 case 'L':
11681 {
11682 std::int64_t len{};
11683 return get_number(input_format, len) && get_string(input_format, len, result);
11684 }
11685
11686 case 'u':
11687 {
11688 if (input_format != input_format_t::bjdata)
11689 {
11690 break;
11691 }
11692 std::uint16_t len{};
11693 return get_number(input_format, len) && get_string(input_format, len, result);
11694 }
11695
11696 case 'm':
11697 {
11698 if (input_format != input_format_t::bjdata)
11699 {
11700 break;
11701 }
11702 std::uint32_t len{};
11703 return get_number(input_format, len) && get_string(input_format, len, result);
11704 }
11705
11706 case 'M':
11707 {
11708 if (input_format != input_format_t::bjdata)
11709 {
11710 break;
11711 }
11712 std::uint64_t len{};
11713 return get_number(input_format, len) && get_string(input_format, len, result);
11714 }
11715
11716 default:
11717 break;
11718 }
11719 auto last_token = get_token_string();
11720 std::string message;
11721
11722 if (input_format != input_format_t::bjdata)
11723 {
11724 message = "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token;
11725 }
11726 else
11727 {
11728 message = "expected length type specification (U, i, u, I, m, l, M, L); last byte: 0x" + last_token;
11729 }
11730 return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "string"), nullptr));
11731 }
11732
11733 /*!
11734 @param[out] dim an integer vector storing the ND array dimensions
11735 @return whether reading ND array size vector is successful
11736 */
11737 bool get_ubjson_ndarray_size(std::vector<size_t>& dim)
11738 {
11739 std::pair<std::size_t, char_int_type> size_and_type;
11740 size_t dimlen = 0;
11741 bool no_ndarray = true;
11742
11743 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type, no_ndarray)))
11744 {
11745 return false;
11746 }
11747
11748 if (size_and_type.first != npos)
11749 {
11750 if (size_and_type.second != 0)
11751 {
11752 if (size_and_type.second != 'N')
11753 {
11754 for (std::size_t i = 0; i < size_and_type.first; ++i)
11755 {
11756 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, size_and_type.second)))
11757 {
11758 return false;
11759 }
11760 dim.push_back(dimlen);
11761 }
11762 }
11763 }
11764 else
11765 {
11766 for (std::size_t i = 0; i < size_and_type.first; ++i)
11767 {
11768 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray)))
11769 {
11770 return false;
11771 }
11772 dim.push_back(dimlen);
11773 }
11774 }
11775 }
11776 else
11777 {
11778 while (current != ']')
11779 {
11780 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_value(dimlen, no_ndarray, current)))
11781 {
11782 return false;
11783 }
11784 dim.push_back(dimlen);
11785 get_ignore_noop();
11786 }
11787 }
11788 return true;
11789 }
11790
11791 /*!
11792 @param[out] result determined size
11793 @param[in,out] is_ndarray for input, `true` means already inside an ndarray vector
11794 or ndarray dimension is not allowed; `false` means ndarray
11795 is allowed; for output, `true` means an ndarray is found;
11796 is_ndarray can only return `true` when its initial value
11797 is `false`
11798 @param[in] prefix type marker if already read, otherwise set to 0
11799
11800 @return whether size determination completed
11801 */
11802 bool get_ubjson_size_value(std::size_t& result, bool& is_ndarray, char_int_type prefix = 0)
11803 {
11804 if (prefix == 0)
11805 {
11806 prefix = get_ignore_noop();
11807 }
11808
11809 switch (prefix)
11810 {
11811 case 'U':
11812 {
11813 std::uint8_t number{};
11814 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11815 {
11816 return false;
11817 }
11818 result = static_cast<std::size_t>(number);
11819 return true;
11820 }
11821
11822 case 'i':
11823 {
11824 std::int8_t number{};
11825 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11826 {
11827 return false;
11828 }
11829 if (number < 0)
11830 {
11831 return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
11832 exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
11833 }
11834 result = static_cast<std::size_t>(number); // NOLINT(bugprone-signed-char-misuse,cert-str34-c): number is not a char
11835 return true;
11836 }
11837
11838 case 'I':
11839 {
11840 std::int16_t number{};
11841 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11842 {
11843 return false;
11844 }
11845 if (number < 0)
11846 {
11847 return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
11848 exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
11849 }
11850 result = static_cast<std::size_t>(number);
11851 return true;
11852 }
11853
11854 case 'l':
11855 {
11856 std::int32_t number{};
11857 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11858 {
11859 return false;
11860 }
11861 if (number < 0)
11862 {
11863 return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
11864 exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
11865 }
11866 result = static_cast<std::size_t>(number);
11867 return true;
11868 }
11869
11870 case 'L':
11871 {
11872 std::int64_t number{};
11873 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11874 {
11875 return false;
11876 }
11877 if (number < 0)
11878 {
11879 return sax->parse_error(chars_read, get_token_string(), parse_error::create(113, chars_read,
11880 exception_message(input_format, "count in an optimized container must be positive", "size"), nullptr));
11881 }
11882 if (!value_in_range_of<std::size_t>(number))
11883 {
11884 return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408,
11885 exception_message(input_format, "integer value overflow", "size"), nullptr));
11886 }
11887 result = static_cast<std::size_t>(number);
11888 return true;
11889 }
11890
11891 case 'u':
11892 {
11893 if (input_format != input_format_t::bjdata)
11894 {
11895 break;
11896 }
11897 std::uint16_t number{};
11898 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11899 {
11900 return false;
11901 }
11902 result = static_cast<std::size_t>(number);
11903 return true;
11904 }
11905
11906 case 'm':
11907 {
11908 if (input_format != input_format_t::bjdata)
11909 {
11910 break;
11911 }
11912 std::uint32_t number{};
11913 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11914 {
11915 return false;
11916 }
11917 result = conditional_static_cast<std::size_t>(number);
11918 return true;
11919 }
11920
11921 case 'M':
11922 {
11923 if (input_format != input_format_t::bjdata)
11924 {
11925 break;
11926 }
11927 std::uint64_t number{};
11928 if (JSON_HEDLEY_UNLIKELY(!get_number(input_format, number)))
11929 {
11930 return false;
11931 }
11932 if (!value_in_range_of<std::size_t>(number))
11933 {
11934 return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408,
11935 exception_message(input_format, "integer value overflow", "size"), nullptr));
11936 }
11937 result = detail::conditional_static_cast<std::size_t>(number);
11938 return true;
11939 }
11940
11941 case '[':
11942 {
11943 if (input_format != input_format_t::bjdata)
11944 {
11945 break;
11946 }
11947 if (is_ndarray) // ndarray dimensional vector can only contain integers, and can not embed another array
11948 {
11949 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));
11950 }
11951 std::vector<size_t> dim;
11952 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_ndarray_size(dim)))
11953 {
11954 return false;
11955 }
11956 if (dim.size() == 1 || (dim.size() == 2 && dim.at(0) == 1)) // return normal array size if 1D row vector
11957 {
11958 result = dim.at(dim.size() - 1);
11959 return true;
11960 }
11961 if (!dim.empty()) // if ndarray, convert to an object in JData annotated array format
11962 {
11963 for (auto i : dim) // test if any dimension in an ndarray is 0, if so, return a 1D empty container
11964 {
11965 if ( i == 0 )
11966 {
11967 result = 0;
11968 return true;
11969 }
11970 }
11971
11972 string_t key = "_ArraySize_";
11973 if (JSON_HEDLEY_UNLIKELY(!sax->start_object(3) || !sax->key(key) || !sax->start_array(dim.size())))
11974 {
11975 return false;
11976 }
11977 result = 1;
11978 for (auto i : dim)
11979 {
11980 result *= i;
11981 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()
11982 {
11983 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));
11984 }
11985 if (JSON_HEDLEY_UNLIKELY(!sax->number_unsigned(static_cast<number_unsigned_t>(i))))
11986 {
11987 return false;
11988 }
11989 }
11990 is_ndarray = true;
11991 return sax->end_array();
11992 }
11993 result = 0;
11994 return true;
11995 }
11996
11997 default:
11998 break;
11999 }
12000 auto last_token = get_token_string();
12001 std::string message;
12002
12003 if (input_format != input_format_t::bjdata)
12004 {
12005 message = "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token;
12006 }
12007 else
12008 {
12009 message = "expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x" + last_token;
12010 }
12011 return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format, message, "size"), nullptr));
12012 }
12013
12014 /*!
12015 @brief determine the type and size for a container
12016
12017 In the optimized UBJSON format, a type and a size can be provided to allow
12018 for a more compact representation.
12019
12020 @param[out] result pair of the size and the type
12021 @param[in] inside_ndarray whether the parser is parsing an ND array dimensional vector
12022
12023 @return whether pair creation completed
12024 */
12025 bool get_ubjson_size_type(std::pair<std::size_t, char_int_type>& result, bool inside_ndarray = false)
12026 {
12027 result.first = npos; // size
12028 result.second = 0; // type
12029 bool is_ndarray = false;
12030
12031 get_ignore_noop();
12032
12033 if (current == '$')
12034 {
12035 result.second = get(); // must not ignore 'N', because 'N' maybe the type
12036 if (input_format == input_format_t::bjdata
12037 && JSON_HEDLEY_UNLIKELY(std::binary_search(bjd_optimized_type_markers.begin(), bjd_optimized_type_markers.end(), result.second)))
12038 {
12039 auto last_token = get_token_string();
12040 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
12041 exception_message(input_format, concat("marker 0x", last_token, " is not a permitted optimized array type"), "type"), nullptr));
12042 }
12043
12044 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "type")))
12045 {
12046 return false;
12047 }
12048
12049 get_ignore_noop();
12050 if (JSON_HEDLEY_UNLIKELY(current != '#'))
12051 {
12052 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "value")))
12053 {
12054 return false;
12055 }
12056 auto last_token = get_token_string();
12057 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
12058 exception_message(input_format, concat("expected '#' after type information; last byte: 0x", last_token), "size"), nullptr));
12059 }
12060
12061 const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
12062 if (input_format == input_format_t::bjdata && is_ndarray)
12063 {
12064 if (inside_ndarray)
12065 {
12066 return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
12067 exception_message(input_format, "ndarray can not be recursive", "size"), nullptr));
12068 }
12069 result.second |= (1 << 8); // use bit 8 to indicate ndarray, all UBJSON and BJData markers should be ASCII letters
12070 }
12071 return is_error;
12072 }
12073
12074 if (current == '#')
12075 {
12076 const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
12077 if (input_format == input_format_t::bjdata && is_ndarray)
12078 {
12079 return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
12080 exception_message(input_format, "ndarray requires both type and size", "size"), nullptr));
12081 }
12082 return is_error;
12083 }
12084
12085 return true;
12086 }
12087
12088 /*!
12089 @param prefix the previously read or set type prefix
12090 @return whether value creation completed
12091 */
12092 bool get_ubjson_value(const char_int_type prefix)
12093 {
12094 switch (prefix)
12095 {
12096 case char_traits<char_type>::eof(): // EOF
12097 return unexpect_eof(input_format, "value");
12098
12099 case 'T': // true
12100 return sax->boolean(true);
12101 case 'F': // false
12102 return sax->boolean(false);
12103
12104 case 'Z': // null
12105 return sax->null();
12106
12107 case 'B': // byte
12108 {
12109 if (input_format != input_format_t::bjdata)
12110 {
12111 break;
12112 }
12113 std::uint8_t number{};
12114 return get_number(input_format, number) && sax->number_unsigned(number);
12115 }
12116
12117 case 'U':
12118 {
12119 std::uint8_t number{};
12120 return get_number(input_format, number) && sax->number_unsigned(number);
12121 }
12122
12123 case 'i':
12124 {
12125 std::int8_t number{};
12126 return get_number(input_format, number) && sax->number_integer(number);
12127 }
12128
12129 case 'I':
12130 {
12131 std::int16_t number{};
12132 return get_number(input_format, number) && sax->number_integer(number);
12133 }
12134
12135 case 'l':
12136 {
12137 std::int32_t number{};
12138 return get_number(input_format, number) && sax->number_integer(number);
12139 }
12140
12141 case 'L':
12142 {
12143 std::int64_t number{};
12144 return get_number(input_format, number) && sax->number_integer(number);
12145 }
12146
12147 case 'u':
12148 {
12149 if (input_format != input_format_t::bjdata)
12150 {
12151 break;
12152 }
12153 std::uint16_t number{};
12154 return get_number(input_format, number) && sax->number_unsigned(number);
12155 }
12156
12157 case 'm':
12158 {
12159 if (input_format != input_format_t::bjdata)
12160 {
12161 break;
12162 }
12163 std::uint32_t number{};
12164 return get_number(input_format, number) && sax->number_unsigned(number);
12165 }
12166
12167 case 'M':
12168 {
12169 if (input_format != input_format_t::bjdata)
12170 {
12171 break;
12172 }
12173 std::uint64_t number{};
12174 return get_number(input_format, number) && sax->number_unsigned(number);
12175 }
12176
12177 case 'h':
12178 {
12179 if (input_format != input_format_t::bjdata)
12180 {
12181 break;
12182 }
12183 const auto byte1_raw = get();
12184 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
12185 {
12186 return false;
12187 }
12188 const auto byte2_raw = get();
12189 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
12190 {
12191 return false;
12192 }
12193
12194 const auto byte1 = static_cast<unsigned char>(byte1_raw);
12195 const auto byte2 = static_cast<unsigned char>(byte2_raw);
12196
12197 // code from RFC 7049, Appendix D, Figure 3:
12198 // As half-precision floating-point numbers were only added
12199 // to IEEE 754 in 2008, today's programming platforms often
12200 // still only have limited support for them. It is very
12201 // easy to include at least decoding support for them even
12202 // without such support. An example of a small decoder for
12203 // half-precision floating-point numbers in the C language
12204 // is shown in Fig. 3.
12205 const auto half = static_cast<unsigned int>((byte2 << 8u) + byte1);
12206 const double val = [&half]
12207 {
12208 const int exp = (half >> 10u) & 0x1Fu;
12209 const unsigned int mant = half & 0x3FFu;
12210 JSON_ASSERT(0 <= exp&& exp <= 32);
12211 JSON_ASSERT(mant <= 1024);
12212 switch (exp)
12213 {
12214 case 0:
12215 return std::ldexp(mant, -24);
12216 case 31:
12217 return (mant == 0)
12218 ? std::numeric_limits<double>::infinity()
12219 : std::numeric_limits<double>::quiet_NaN();
12220 default:
12221 return std::ldexp(mant + 1024, exp - 25);
12222 }
12223 }();
12224 return sax->number_float((half & 0x8000u) != 0
12225 ? static_cast<number_float_t>(-val)
12226 : static_cast<number_float_t>(val), "");
12227 }
12228
12229 case 'd':
12230 {
12231 float number{};
12232 return get_number(input_format, number) && sax->number_float(static_cast<number_float_t>(number), "");
12233 }
12234
12235 case 'D':
12236 {
12237 double number{};
12238 return get_number(input_format, number) && sax->number_float(static_cast<number_float_t>(number), "");
12239 }
12240
12241 case 'H':
12242 {
12243 return get_ubjson_high_precision_number();
12244 }
12245
12246 case 'C': // char
12247 {
12248 get();
12249 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "char")))
12250 {
12251 return false;
12252 }
12253 if (JSON_HEDLEY_UNLIKELY(current > 127))
12254 {
12255 auto last_token = get_token_string();
12256 return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read,
12257 exception_message(input_format, concat("byte after 'C' must be in range 0x00..0x7F; last byte: 0x", last_token), "char"), nullptr));
12258 }
12259 string_t s(1, static_cast<typename string_t::value_type>(current));
12260 return sax->string(s);
12261 }
12262
12263 case 'S': // string
12264 {
12265 string_t s;
12266 return get_ubjson_string(s) && sax->string(s);
12267 }
12268
12269 case '[': // array
12270 return get_ubjson_array();
12271
12272 case '{': // object
12273 return get_ubjson_object();
12274
12275 default: // anything else
12276 break;
12277 }
12278 auto last_token = get_token_string();
12279 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));
12280 }
12281
12282 /*!
12283 @return whether array creation completed
12284 */
12285 bool get_ubjson_array()
12286 {
12287 std::pair<std::size_t, char_int_type> size_and_type;
12288 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type)))
12289 {
12290 return false;
12291 }
12292
12293 // 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):
12294 // {"_ArrayType_" : "typeid", "_ArraySize_" : [n1, n2, ...], "_ArrayData_" : [v1, v2, ...]}
12295
12296 if (input_format == input_format_t::bjdata && size_and_type.first != npos && (size_and_type.second & (1 << 8)) != 0)
12297 {
12298 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
12299 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)
12300 {
12301 return p.first < t;
12302 });
12303 string_t key = "_ArrayType_";
12304 if (JSON_HEDLEY_UNLIKELY(it == bjd_types_map.end() || it->first != size_and_type.second))
12305 {
12306 auto last_token = get_token_string();
12307 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
12308 exception_message(input_format, "invalid byte: 0x" + last_token, "type"), nullptr));
12309 }
12310
12311 string_t type = it->second; // sax->string() takes a reference
12312 if (JSON_HEDLEY_UNLIKELY(!sax->key(key) || !sax->string(type)))
12313 {
12314 return false;
12315 }
12316
12317 if (size_and_type.second == 'C' || size_and_type.second == 'B')
12318 {
12319 size_and_type.second = 'U';
12320 }
12321
12322 key = "_ArrayData_";
12323 if (JSON_HEDLEY_UNLIKELY(!sax->key(key) || !sax->start_array(size_and_type.first) ))
12324 {
12325 return false;
12326 }
12327
12328 for (std::size_t i = 0; i < size_and_type.first; ++i)
12329 {
12330 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
12331 {
12332 return false;
12333 }
12334 }
12335
12336 return (sax->end_array() && sax->end_object());
12337 }
12338
12339 // If BJData type marker is 'B' decode as binary
12340 if (input_format == input_format_t::bjdata && size_and_type.first != npos && size_and_type.second == 'B')
12341 {
12342 binary_t result;
12343 return get_binary(input_format, size_and_type.first, result) && sax->binary(result);
12344 }
12345
12346 if (size_and_type.first != npos)
12347 {
12348 if (JSON_HEDLEY_UNLIKELY(!sax->start_array(size_and_type.first)))
12349 {
12350 return false;
12351 }
12352
12353 if (size_and_type.second != 0)
12354 {
12355 if (size_and_type.second != 'N')
12356 {
12357 for (std::size_t i = 0; i < size_and_type.first; ++i)
12358 {
12359 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
12360 {
12361 return false;
12362 }
12363 }
12364 }
12365 }
12366 else
12367 {
12368 for (std::size_t i = 0; i < size_and_type.first; ++i)
12369 {
12370 if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
12371 {
12372 return false;
12373 }
12374 }
12375 }
12376 }
12377 else
12378 {
12379 if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
12380 {
12381 return false;
12382 }
12383
12384 while (current != ']')
12385 {
12386 if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal(false)))
12387 {
12388 return false;
12389 }
12390 get_ignore_noop();
12391 }
12392 }
12393
12394 return sax->end_array();
12395 }
12396
12397 /*!
12398 @return whether object creation completed
12399 */
12400 bool get_ubjson_object()
12401 {
12402 std::pair<std::size_t, char_int_type> size_and_type;
12403 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type)))
12404 {
12405 return false;
12406 }
12407
12408 // do not accept ND-array size in objects in BJData
12409 if (input_format == input_format_t::bjdata && size_and_type.first != npos && (size_and_type.second & (1 << 8)) != 0)
12410 {
12411 auto last_token = get_token_string();
12412 return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read,
12413 exception_message(input_format, "BJData object does not support ND-array size in optimized format", "object"), nullptr));
12414 }
12415
12416 string_t key;
12417 if (size_and_type.first != npos)
12418 {
12419 if (JSON_HEDLEY_UNLIKELY(!sax->start_object(size_and_type.first)))
12420 {
12421 return false;
12422 }
12423
12424 if (size_and_type.second != 0)
12425 {
12426 for (std::size_t i = 0; i < size_and_type.first; ++i)
12427 {
12428 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
12429 {
12430 return false;
12431 }
12432 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
12433 {
12434 return false;
12435 }
12436 key.clear();
12437 }
12438 }
12439 else
12440 {
12441 for (std::size_t i = 0; i < size_and_type.first; ++i)
12442 {
12443 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
12444 {
12445 return false;
12446 }
12447 if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
12448 {
12449 return false;
12450 }
12451 key.clear();
12452 }
12453 }
12454 }
12455 else
12456 {
12457 if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
12458 {
12459 return false;
12460 }
12461
12462 while (current != '}')
12463 {
12464 if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key, false) || !sax->key(key)))
12465 {
12466 return false;
12467 }
12468 if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
12469 {
12470 return false;
12471 }
12472 get_ignore_noop();
12473 key.clear();
12474 }
12475 }
12476
12477 return sax->end_object();
12478 }
12479
12480 // Note, no reader for UBJSON binary types is implemented because they do
12481 // not exist
12482
12483 bool get_ubjson_high_precision_number()
12484 {
12485 // get size of following number string
12486 std::size_t size{};
12487 bool no_ndarray = true;
12488 auto res = get_ubjson_size_value(size, no_ndarray);
12489 if (JSON_HEDLEY_UNLIKELY(!res))
12490 {
12491 return res;
12492 }
12493
12494 // get number string
12495 std::vector<char> number_vector;
12496 for (std::size_t i = 0; i < size; ++i)
12497 {
12498 get();
12499 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format, "number")))
12500 {
12501 return false;
12502 }
12503 number_vector.push_back(static_cast<char>(current));
12504 }
12505
12506 // parse number string
12507 using ia_type = decltype(detail::input_adapter(number_vector));
12508 auto number_lexer = detail::lexer<BasicJsonType, ia_type>(detail::input_adapter(number_vector), false);
12509 const auto result_number = number_lexer.scan();
12510 const auto number_string = number_lexer.get_token_string();
12511 const auto result_remainder = number_lexer.scan();
12512
12513 using token_type = typename detail::lexer_base<BasicJsonType>::token_type;
12514
12515 if (JSON_HEDLEY_UNLIKELY(result_remainder != token_type::end_of_input))
12516 {
12517 return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read,
12518 exception_message(input_format, concat("invalid number text: ", number_lexer.get_token_string()), "high-precision number"), nullptr));
12519 }
12520
12521 switch (result_number)
12522 {
12523 case token_type::value_integer:
12524 return sax->number_integer(number_lexer.get_number_integer());
12525 case token_type::value_unsigned:
12526 return sax->number_unsigned(number_lexer.get_number_unsigned());
12527 case token_type::value_float:
12528 return sax->number_float(number_lexer.get_number_float(), std::move(number_string));
12529 case token_type::uninitialized:
12530 case token_type::literal_true:
12531 case token_type::literal_false:
12532 case token_type::literal_null:
12533 case token_type::value_string:
12534 case token_type::begin_array:
12535 case token_type::begin_object:
12536 case token_type::end_array:
12537 case token_type::end_object:
12538 case token_type::name_separator:
12539 case token_type::value_separator:
12540 case token_type::parse_error:
12541 case token_type::end_of_input:
12542 case token_type::literal_or_value:
12543 default:
12544 return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read,
12545 exception_message(input_format, concat("invalid number text: ", number_lexer.get_token_string()), "high-precision number"), nullptr));
12546 }
12547 }
12548
12549 ///////////////////////
12550 // Utility functions //
12551 ///////////////////////
12552
12553 /*!
12554 @brief get next character from the input
12555
12556 This function provides the interface to the used input adapter. It does
12557 not throw in case the input reached EOF, but returns a -'ve valued
12558 `char_traits<char_type>::eof()` in that case.
12559
12560 @return character read from the input
12561 */
12562 char_int_type get()
12563 {
12564 ++chars_read;
12565 return current = ia.get_character();
12566 }
12567
12568 /*!
12569 @brief get_to read into a primitive type
12570
12571 This function provides the interface to the used input adapter. It does
12572 not throw in case the input reached EOF, but returns false instead
12573
12574 @return bool, whether the read was successful
12575 */
12576 template<class T>
12577 bool get_to(T& dest, const input_format_t format, const char* context)
12578 {
12579 auto new_chars_read = ia.get_elements(&dest);
12580 chars_read += new_chars_read;
12581 if (JSON_HEDLEY_UNLIKELY(new_chars_read < sizeof(T)))
12582 {
12583 // in case of failure, advance position by 1 to report failing location
12584 ++chars_read;
12585 sax->parse_error(chars_read, "<end of file>", parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), nullptr));
12586 return false;
12587 }
12588 return true;
12589 }
12590
12591 /*!
12592 @return character read from the input after ignoring all 'N' entries
12593 */
12594 char_int_type get_ignore_noop()
12595 {
12596 do
12597 {
12598 get();
12599 }
12600 while (current == 'N');
12601
12602 return current;
12603 }
12604
12605 template<class NumberType>
12606 static void byte_swap(NumberType& number)
12607 {
12608 constexpr std::size_t sz = sizeof(number);
12609 #ifdef __cpp_lib_byteswap
12610 if constexpr (sz == 1)
12611 {
12612 return;
12613 }
12614 if constexpr(std::is_integral_v<NumberType>)
12615 {
12616 number = std::byteswap(number);
12617 return;
12618 }
12619 #endif
12620 auto* ptr = reinterpret_cast<std::uint8_t*>(&number);
12621 for (std::size_t i = 0; i < sz / 2; ++i)
12622 {
12623 std::swap(ptr[i], ptr[sz - i - 1]);
12624 }
12625 }
12626
12627 /*
12628 @brief read a number from the input
12629
12630 @tparam NumberType the type of the number
12631 @param[in] format the current format (for diagnostics)
12632 @param[out] result number of type @a NumberType
12633
12634 @return whether conversion completed
12635
12636 @note This function needs to respect the system's endianness, because
12637 bytes in CBOR, MessagePack, and UBJSON are stored in network order
12638 (big endian) and therefore need reordering on little endian systems.
12639 On the other hand, BSON and BJData use little endian and should reorder
12640 on big endian systems.
12641 */
12642 template<typename NumberType, bool InputIsLittleEndian = false>
12643 bool get_number(const input_format_t format, NumberType& result)
12644 {
12645 // read in the original format
12646
12647 if (JSON_HEDLEY_UNLIKELY(!get_to(result, format, "number")))
12648 {
12649 return false;
12650 }
12651 if (is_little_endian != (InputIsLittleEndian || format == input_format_t::bjdata))
12652 {
12653 byte_swap(result);
12654 }
12655 return true;
12656 }
12657
12658 /*!
12659 @brief create a string by reading characters from the input
12660
12661 @tparam NumberType the type of the number
12662 @param[in] format the current format (for diagnostics)
12663 @param[in] len number of characters to read
12664 @param[out] result string created by reading @a len bytes
12665
12666 @return whether string creation completed
12667
12668 @note We can not reserve @a len bytes for the result, because @a len
12669 may be too large. Usually, @ref unexpect_eof() detects the end of
12670 the input before we run out of string memory.
12671 */
12672 template<typename NumberType>
12673 bool get_string(const input_format_t format,
12674 const NumberType len,
12675 string_t& result)
12676 {
12677 bool success = true;
12678 for (NumberType i = 0; i < len; i++)
12679 {
12680 get();
12681 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "string")))
12682 {
12683 success = false;
12684 break;
12685 }
12686 result.push_back(static_cast<typename string_t::value_type>(current));
12687 }
12688 return success;
12689 }
12690
12691 /*!
12692 @brief create a byte array by reading bytes from the input
12693
12694 @tparam NumberType the type of the number
12695 @param[in] format the current format (for diagnostics)
12696 @param[in] len number of bytes to read
12697 @param[out] result byte array created by reading @a len bytes
12698
12699 @return whether byte array creation completed
12700
12701 @note We can not reserve @a len bytes for the result, because @a len
12702 may be too large. Usually, @ref unexpect_eof() detects the end of
12703 the input before we run out of memory.
12704 */
12705 template<typename NumberType>
12706 bool get_binary(const input_format_t format,
12707 const NumberType len,
12708 binary_t& result)
12709 {
12710 bool success = true;
12711 for (NumberType i = 0; i < len; i++)
12712 {
12713 get();
12714 if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "binary")))
12715 {
12716 success = false;
12717 break;
12718 }
12719 result.push_back(static_cast<std::uint8_t>(current));
12720 }
12721 return success;
12722 }
12723
12724 /*!
12725 @param[in] format the current format (for diagnostics)
12726 @param[in] context further context information (for diagnostics)
12727 @return whether the last read character is not EOF
12728 */
12729 JSON_HEDLEY_NON_NULL(3)
12730 bool unexpect_eof(const input_format_t format, const char* context) const
12731 {
12732 if (JSON_HEDLEY_UNLIKELY(current == char_traits<char_type>::eof()))
12733 {
12734 return sax->parse_error(chars_read, "<end of file>",
12735 parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), nullptr));
12736 }
12737 return true;
12738 }
12739
12740 /*!
12741 @return a string representation of the last read byte
12742 */
12743 std::string get_token_string() const
12744 {
12745 std::array<char, 3> cr{{}};
12746 static_cast<void>((std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(current))); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
12747 return std::string{cr.data()};
12748 }
12749
12750 /*!
12751 @param[in] format the current format
12752 @param[in] detail a detailed error message
12753 @param[in] context further context information
12754 @return a message string to use in the parse_error exceptions
12755 */
12756 std::string exception_message(const input_format_t format,
12757 const std::string& detail,
12758 const std::string& context) const
12759 {
12760 std::string error_msg = "syntax error while parsing ";
12761
12762 switch (format)
12763 {
12764 case input_format_t::cbor:
12765 error_msg += "CBOR";
12766 break;
12767
12768 case input_format_t::msgpack:
12769 error_msg += "MessagePack";
12770 break;
12771
12772 case input_format_t::ubjson:
12773 error_msg += "UBJSON";
12774 break;
12775
12776 case input_format_t::bson:
12777 error_msg += "BSON";
12778 break;
12779
12780 case input_format_t::bjdata:
12781 error_msg += "BJData";
12782 break;
12783
12784 case input_format_t::json: // LCOV_EXCL_LINE
12785 default: // LCOV_EXCL_LINE
12786 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
12787 }
12788
12789 return concat(error_msg, ' ', context, ": ", detail);
12790 }
12791
12792 private:
12793 static JSON_INLINE_VARIABLE constexpr std::size_t npos = detail::unknown_size();
12794
12795 /// input adapter
12796 InputAdapterType ia;
12797
12798 /// the current character
12799 char_int_type current = char_traits<char_type>::eof();
12800
12801 /// the number of characters read
12802 std::size_t chars_read = 0;
12803
12804 /// whether we can assume little endianness
12805 const bool is_little_endian = little_endianness();
12806
12807 /// input format
12808 const input_format_t input_format = input_format_t::json;
12809
12810 /// the SAX parser
12811 json_sax_t* sax = nullptr;
12812
12813 // excluded markers in bjdata optimized type
12814 #define JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_ \
12815 make_array<char_int_type>('F', 'H', 'N', 'S', 'T', 'Z', '[', '{')
12816
12817 #define JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_ \
12818 make_array<bjd_type>( \
12819 bjd_type{'B', "byte"}, \
12820 bjd_type{'C', "char"}, \
12821 bjd_type{'D', "double"}, \
12822 bjd_type{'I', "int16"}, \
12823 bjd_type{'L', "int64"}, \
12824 bjd_type{'M', "uint64"}, \
12825 bjd_type{'U', "uint8"}, \
12826 bjd_type{'d', "single"}, \
12827 bjd_type{'i', "int8"}, \
12828 bjd_type{'l', "int32"}, \
12829 bjd_type{'m', "uint32"}, \
12830 bjd_type{'u', "uint16"})
12831
12832 JSON_PRIVATE_UNLESS_TESTED:
12833 // lookup tables
12834 // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
12835 const decltype(JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_) bjd_optimized_type_markers =
12836 JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_;
12837
12838 using bjd_type = std::pair<char_int_type, string_t>;
12839 // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
12840 const decltype(JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_) bjd_types_map =
12841 JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_;
12842
12843 #undef JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_
12844 #undef JSON_BINARY_READER_MAKE_BJD_TYPES_MAP_
12845 };
12846
12847 #ifndef JSON_HAS_CPP_17
12848 template<typename BasicJsonType, typename InputAdapterType, typename SAX>
12849 constexpr std::size_t binary_reader<BasicJsonType, InputAdapterType, SAX>::npos;
12850 #endif
12851
12852 } // namespace detail
12853 NLOHMANN_JSON_NAMESPACE_END
12854
12855 // #include <nlohmann/detail/input/input_adapters.hpp>
12856
12857 // #include <nlohmann/detail/input/lexer.hpp>
12858
12859 // #include <nlohmann/detail/input/parser.hpp>
12860 // __ _____ _____ _____
12861 // __| | __| | | | JSON for Modern C++
12862 // | | |__ | | | | | | version 3.12.0
12863 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
12864 //
12865 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
12866 // SPDX-License-Identifier: MIT
12867
12868
12869
12870 #include <cmath> // isfinite
12871 #include <cstdint> // uint8_t
12872 #include <functional> // function
12873 #include <string> // string
12874 #include <utility> // move
12875 #include <vector> // vector
12876
12877 // #include <nlohmann/detail/exceptions.hpp>
12878
12879 // #include <nlohmann/detail/input/input_adapters.hpp>
12880
12881 // #include <nlohmann/detail/input/json_sax.hpp>
12882
12883 // #include <nlohmann/detail/input/lexer.hpp>
12884
12885 // #include <nlohmann/detail/macro_scope.hpp>
12886
12887 // #include <nlohmann/detail/meta/is_sax.hpp>
12888
12889 // #include <nlohmann/detail/string_concat.hpp>
12890
12891 // #include <nlohmann/detail/value_t.hpp>
12892
12893
12894 NLOHMANN_JSON_NAMESPACE_BEGIN
12895 namespace detail
12896 {
12897 ////////////
12898 // parser //
12899 ////////////
12900
12901 enum class parse_event_t : std::uint8_t
12902 {
12903 /// the parser read `{` and started to process a JSON object
12904 object_start,
12905 /// the parser read `}` and finished processing a JSON object
12906 object_end,
12907 /// the parser read `[` and started to process a JSON array
12908 array_start,
12909 /// the parser read `]` and finished processing a JSON array
12910 array_end,
12911 /// the parser read a key of a value in an object
12912 key,
12913 /// the parser finished reading a JSON value
12914 value
12915 };
12916
12917 template<typename BasicJsonType>
12918 using parser_callback_t =
12919 std::function<bool(int /*depth*/, parse_event_t /*event*/, BasicJsonType& /*parsed*/)>;
12920
12921 /*!
12922 @brief syntax analysis
12923
12924 This class implements a recursive descent parser.
12925 */
12926 template<typename BasicJsonType, typename InputAdapterType>
12927 class parser
12928 {
12929 using number_integer_t = typename BasicJsonType::number_integer_t;
12930 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
12931 using number_float_t = typename BasicJsonType::number_float_t;
12932 using string_t = typename BasicJsonType::string_t;
12933 using lexer_t = lexer<BasicJsonType, InputAdapterType>;
12934 using token_type = typename lexer_t::token_type;
12935
12936 public:
12937 /// a parser reading from an input adapter
12938 explicit parser(InputAdapterType&& adapter,
12939 parser_callback_t<BasicJsonType> cb = nullptr,
12940 const bool allow_exceptions_ = true,
12941 const bool skip_comments = false)
12942 : callback(std::move(cb))
12943 , m_lexer(std::move(adapter), skip_comments)
12944 , allow_exceptions(allow_exceptions_)
12945 {
12946 // read first token
12947 get_token();
12948 }
12949
12950 /*!
12951 @brief public parser interface
12952
12953 @param[in] strict whether to expect the last token to be EOF
12954 @param[in,out] result parsed JSON value
12955
12956 @throw parse_error.101 in case of an unexpected token
12957 @throw parse_error.102 if to_unicode fails or surrogate error
12958 @throw parse_error.103 if to_unicode fails
12959 */
12960 void parse(const bool strict, BasicJsonType& result)
12961 {
12962 if (callback)
12963 {
12964 json_sax_dom_callback_parser<BasicJsonType, InputAdapterType> sdp(result, callback, allow_exceptions, &m_lexer);
12965 sax_parse_internal(&sdp);
12966
12967 // in strict mode, input must be completely read
12968 if (strict && (get_token() != token_type::end_of_input))
12969 {
12970 sdp.parse_error(m_lexer.get_position(),
12971 m_lexer.get_token_string(),
12972 parse_error::create(101, m_lexer.get_position(),
12973 exception_message(token_type::end_of_input, "value"), nullptr));
12974 }
12975
12976 // in case of an error, return discarded value
12977 if (sdp.is_errored())
12978 {
12979 result = value_t::discarded;
12980 return;
12981 }
12982
12983 // set top-level value to null if it was discarded by the callback
12984 // function
12985 if (result.is_discarded())
12986 {
12987 result = nullptr;
12988 }
12989 }
12990 else
12991 {
12992 json_sax_dom_parser<BasicJsonType, InputAdapterType> sdp(result, allow_exceptions, &m_lexer);
12993 sax_parse_internal(&sdp);
12994
12995 // in strict mode, input must be completely read
12996 if (strict && (get_token() != token_type::end_of_input))
12997 {
12998 sdp.parse_error(m_lexer.get_position(),
12999 m_lexer.get_token_string(),
13000 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), nullptr));
13001 }
13002
13003 // in case of an error, return discarded value
13004 if (sdp.is_errored())
13005 {
13006 result = value_t::discarded;
13007 return;
13008 }
13009 }
13010
13011 result.assert_invariant();
13012 }
13013
13014 /*!
13015 @brief public accept interface
13016
13017 @param[in] strict whether to expect the last token to be EOF
13018 @return whether the input is a proper JSON text
13019 */
13020 bool accept(const bool strict = true)
13021 {
13022 json_sax_acceptor<BasicJsonType> sax_acceptor;
13023 return sax_parse(&sax_acceptor, strict);
13024 }
13025
13026 template<typename SAX>
13027 JSON_HEDLEY_NON_NULL(2)
13028 bool sax_parse(SAX* sax, const bool strict = true)
13029 {
13030 (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
13031 const bool result = sax_parse_internal(sax);
13032
13033 // strict mode: next byte must be EOF
13034 if (result && strict && (get_token() != token_type::end_of_input))
13035 {
13036 return sax->parse_error(m_lexer.get_position(),
13037 m_lexer.get_token_string(),
13038 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), nullptr));
13039 }
13040
13041 return result;
13042 }
13043
13044 private:
13045 template<typename SAX>
13046 JSON_HEDLEY_NON_NULL(2)
13047 bool sax_parse_internal(SAX* sax)
13048 {
13049 // stack to remember the hierarchy of structured values we are parsing
13050 // true = array; false = object
13051 std::vector<bool> states;
13052 // value to avoid a goto (see comment where set to true)
13053 bool skip_to_state_evaluation = false;
13054
13055 while (true)
13056 {
13057 if (!skip_to_state_evaluation)
13058 {
13059 // invariant: get_token() was called before each iteration
13060 switch (last_token)
13061 {
13062 case token_type::begin_object:
13063 {
13064 if (JSON_HEDLEY_UNLIKELY(!sax->start_object(detail::unknown_size())))
13065 {
13066 return false;
13067 }
13068
13069 // closing } -> we are done
13070 if (get_token() == token_type::end_object)
13071 {
13072 if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
13073 {
13074 return false;
13075 }
13076 break;
13077 }
13078
13079 // parse key
13080 if (JSON_HEDLEY_UNLIKELY(last_token != token_type::value_string))
13081 {
13082 return sax->parse_error(m_lexer.get_position(),
13083 m_lexer.get_token_string(),
13084 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), nullptr));
13085 }
13086 if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
13087 {
13088 return false;
13089 }
13090
13091 // parse separator (:)
13092 if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
13093 {
13094 return sax->parse_error(m_lexer.get_position(),
13095 m_lexer.get_token_string(),
13096 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), nullptr));
13097 }
13098
13099 // remember we are now inside an object
13100 states.push_back(false);
13101
13102 // parse values
13103 get_token();
13104 continue;
13105 }
13106
13107 case token_type::begin_array:
13108 {
13109 if (JSON_HEDLEY_UNLIKELY(!sax->start_array(detail::unknown_size())))
13110 {
13111 return false;
13112 }
13113
13114 // closing ] -> we are done
13115 if (get_token() == token_type::end_array)
13116 {
13117 if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
13118 {
13119 return false;
13120 }
13121 break;
13122 }
13123
13124 // remember we are now inside an array
13125 states.push_back(true);
13126
13127 // parse values (no need to call get_token)
13128 continue;
13129 }
13130
13131 case token_type::value_float:
13132 {
13133 const auto res = m_lexer.get_number_float();
13134
13135 if (JSON_HEDLEY_UNLIKELY(!std::isfinite(res)))
13136 {
13137 return sax->parse_error(m_lexer.get_position(),
13138 m_lexer.get_token_string(),
13139 out_of_range::create(406, concat("number overflow parsing '", m_lexer.get_token_string(), '\''), nullptr));
13140 }
13141
13142 if (JSON_HEDLEY_UNLIKELY(!sax->number_float(res, m_lexer.get_string())))
13143 {
13144 return false;
13145 }
13146
13147 break;
13148 }
13149
13150 case token_type::literal_false:
13151 {
13152 if (JSON_HEDLEY_UNLIKELY(!sax->boolean(false)))
13153 {
13154 return false;
13155 }
13156 break;
13157 }
13158
13159 case token_type::literal_null:
13160 {
13161 if (JSON_HEDLEY_UNLIKELY(!sax->null()))
13162 {
13163 return false;
13164 }
13165 break;
13166 }
13167
13168 case token_type::literal_true:
13169 {
13170 if (JSON_HEDLEY_UNLIKELY(!sax->boolean(true)))
13171 {
13172 return false;
13173 }
13174 break;
13175 }
13176
13177 case token_type::value_integer:
13178 {
13179 if (JSON_HEDLEY_UNLIKELY(!sax->number_integer(m_lexer.get_number_integer())))
13180 {
13181 return false;
13182 }
13183 break;
13184 }
13185
13186 case token_type::value_string:
13187 {
13188 if (JSON_HEDLEY_UNLIKELY(!sax->string(m_lexer.get_string())))
13189 {
13190 return false;
13191 }
13192 break;
13193 }
13194
13195 case token_type::value_unsigned:
13196 {
13197 if (JSON_HEDLEY_UNLIKELY(!sax->number_unsigned(m_lexer.get_number_unsigned())))
13198 {
13199 return false;
13200 }
13201 break;
13202 }
13203
13204 case token_type::parse_error:
13205 {
13206 // using "uninitialized" to avoid "expected" message
13207 return sax->parse_error(m_lexer.get_position(),
13208 m_lexer.get_token_string(),
13209 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::uninitialized, "value"), nullptr));
13210 }
13211 case token_type::end_of_input:
13212 {
13213 if (JSON_HEDLEY_UNLIKELY(m_lexer.get_position().chars_read_total == 1))
13214 {
13215 return sax->parse_error(m_lexer.get_position(),
13216 m_lexer.get_token_string(),
13217 parse_error::create(101, m_lexer.get_position(),
13218 "attempting to parse an empty input; check that your input string or stream contains the expected JSON", nullptr));
13219 }
13220
13221 return sax->parse_error(m_lexer.get_position(),
13222 m_lexer.get_token_string(),
13223 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::literal_or_value, "value"), nullptr));
13224 }
13225 case token_type::uninitialized:
13226 case token_type::end_array:
13227 case token_type::end_object:
13228 case token_type::name_separator:
13229 case token_type::value_separator:
13230 case token_type::literal_or_value:
13231 default: // the last token was unexpected
13232 {
13233 return sax->parse_error(m_lexer.get_position(),
13234 m_lexer.get_token_string(),
13235 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::literal_or_value, "value"), nullptr));
13236 }
13237 }
13238 }
13239 else
13240 {
13241 skip_to_state_evaluation = false;
13242 }
13243
13244 // we reached this line after we successfully parsed a value
13245 if (states.empty())
13246 {
13247 // empty stack: we reached the end of the hierarchy: done
13248 return true;
13249 }
13250
13251 if (states.back()) // array
13252 {
13253 // comma -> next value
13254 if (get_token() == token_type::value_separator)
13255 {
13256 // parse a new value
13257 get_token();
13258 continue;
13259 }
13260
13261 // closing ]
13262 if (JSON_HEDLEY_LIKELY(last_token == token_type::end_array))
13263 {
13264 if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
13265 {
13266 return false;
13267 }
13268
13269 // We are done with this array. Before we can parse a
13270 // new value, we need to evaluate the new state first.
13271 // By setting skip_to_state_evaluation to false, we
13272 // are effectively jumping to the beginning of this if.
13273 JSON_ASSERT(!states.empty());
13274 states.pop_back();
13275 skip_to_state_evaluation = true;
13276 continue;
13277 }
13278
13279 return sax->parse_error(m_lexer.get_position(),
13280 m_lexer.get_token_string(),
13281 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_array, "array"), nullptr));
13282 }
13283
13284 // states.back() is false -> object
13285
13286 // comma -> next value
13287 if (get_token() == token_type::value_separator)
13288 {
13289 // parse key
13290 if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::value_string))
13291 {
13292 return sax->parse_error(m_lexer.get_position(),
13293 m_lexer.get_token_string(),
13294 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), nullptr));
13295 }
13296
13297 if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
13298 {
13299 return false;
13300 }
13301
13302 // parse separator (:)
13303 if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
13304 {
13305 return sax->parse_error(m_lexer.get_position(),
13306 m_lexer.get_token_string(),
13307 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), nullptr));
13308 }
13309
13310 // parse values
13311 get_token();
13312 continue;
13313 }
13314
13315 // closing }
13316 if (JSON_HEDLEY_LIKELY(last_token == token_type::end_object))
13317 {
13318 if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
13319 {
13320 return false;
13321 }
13322
13323 // We are done with this object. Before we can parse a
13324 // new value, we need to evaluate the new state first.
13325 // By setting skip_to_state_evaluation to false, we
13326 // are effectively jumping to the beginning of this if.
13327 JSON_ASSERT(!states.empty());
13328 states.pop_back();
13329 skip_to_state_evaluation = true;
13330 continue;
13331 }
13332
13333 return sax->parse_error(m_lexer.get_position(),
13334 m_lexer.get_token_string(),
13335 parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_object, "object"), nullptr));
13336 }
13337 }
13338
13339 /// get next token from lexer
13340 token_type get_token()
13341 {
13342 return last_token = m_lexer.scan();
13343 }
13344
13345 std::string exception_message(const token_type expected, const std::string& context)
13346 {
13347 std::string error_msg = "syntax error ";
13348
13349 if (!context.empty())
13350 {
13351 error_msg += concat("while parsing ", context, ' ');
13352 }
13353
13354 error_msg += "- ";
13355
13356 if (last_token == token_type::parse_error)
13357 {
13358 error_msg += concat(m_lexer.get_error_message(), "; last read: '",
13359 m_lexer.get_token_string(), '\'');
13360 }
13361 else
13362 {
13363 error_msg += concat("unexpected ", lexer_t::token_type_name(last_token));
13364 }
13365
13366 if (expected != token_type::uninitialized)
13367 {
13368 error_msg += concat("; expected ", lexer_t::token_type_name(expected));
13369 }
13370
13371 return error_msg;
13372 }
13373
13374 private:
13375 /// callback function
13376 const parser_callback_t<BasicJsonType> callback = nullptr;
13377 /// the type of the last read token
13378 token_type last_token = token_type::uninitialized;
13379 /// the lexer
13380 lexer_t m_lexer;
13381 /// whether to throw exceptions in case of errors
13382 const bool allow_exceptions = true;
13383 };
13384
13385 } // namespace detail
13386 NLOHMANN_JSON_NAMESPACE_END
13387
13388 // #include <nlohmann/detail/iterators/internal_iterator.hpp>
13389 // __ _____ _____ _____
13390 // __| | __| | | | JSON for Modern C++
13391 // | | |__ | | | | | | version 3.12.0
13392 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
13393 //
13394 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
13395 // SPDX-License-Identifier: MIT
13396
13397
13398
13399 // #include <nlohmann/detail/abi_macros.hpp>
13400
13401 // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
13402 // __ _____ _____ _____
13403 // __| | __| | | | JSON for Modern C++
13404 // | | |__ | | | | | | version 3.12.0
13405 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
13406 //
13407 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
13408 // SPDX-License-Identifier: MIT
13409
13410
13411
13412 #include <cstddef> // ptrdiff_t
13413 #include <limits> // numeric_limits
13414
13415 // #include <nlohmann/detail/macro_scope.hpp>
13416
13417
13418 NLOHMANN_JSON_NAMESPACE_BEGIN
13419 namespace detail
13420 {
13421
13422 /*
13423 @brief an iterator for primitive JSON types
13424
13425 This class models an iterator for primitive JSON types (boolean, number,
13426 string). It's only purpose is to allow the iterator/const_iterator classes
13427 to "iterate" over primitive values. Internally, the iterator is modeled by
13428 a `difference_type` variable. Value begin_value (`0`) models the begin,
13429 end_value (`1`) models past the end.
13430 */
13431 class primitive_iterator_t
13432 {
13433 private:
13434 using difference_type = std::ptrdiff_t;
13435 static constexpr difference_type begin_value = 0;
13436 static constexpr difference_type end_value = begin_value + 1;
13437
13438 JSON_PRIVATE_UNLESS_TESTED:
13439 /// iterator as signed integer type
13440 difference_type m_it = (std::numeric_limits<std::ptrdiff_t>::min)();
13441
13442 public:
13443 constexpr difference_type get_value() const noexcept
13444 {
13445 return m_it;
13446 }
13447
13448 /// set iterator to a defined beginning
13449 void set_begin() noexcept
13450 {
13451 m_it = begin_value;
13452 }
13453
13454 /// set iterator to a defined past the end
13455 void set_end() noexcept
13456 {
13457 m_it = end_value;
13458 }
13459
13460 /// return whether the iterator can be dereferenced
13461 constexpr bool is_begin() const noexcept
13462 {
13463 return m_it == begin_value;
13464 }
13465
13466 /// return whether the iterator is at end
13467 constexpr bool is_end() const noexcept
13468 {
13469 return m_it == end_value;
13470 }
13471
13472 friend constexpr bool operator==(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
13473 {
13474 return lhs.m_it == rhs.m_it;
13475 }
13476
13477 friend constexpr bool operator<(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
13478 {
13479 return lhs.m_it < rhs.m_it;
13480 }
13481
13482 primitive_iterator_t operator+(difference_type n) noexcept
13483 {
13484 auto result = *this;
13485 result += n;
13486 return result;
13487 }
13488
13489 friend constexpr difference_type operator-(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
13490 {
13491 return lhs.m_it - rhs.m_it;
13492 }
13493
13494 primitive_iterator_t& operator++() noexcept
13495 {
13496 ++m_it;
13497 return *this;
13498 }
13499
13500 primitive_iterator_t operator++(int)& noexcept // NOLINT(cert-dcl21-cpp)
13501 {
13502 auto result = *this;
13503 ++m_it;
13504 return result;
13505 }
13506
13507 primitive_iterator_t& operator--() noexcept
13508 {
13509 --m_it;
13510 return *this;
13511 }
13512
13513 primitive_iterator_t operator--(int)& noexcept // NOLINT(cert-dcl21-cpp)
13514 {
13515 auto result = *this;
13516 --m_it;
13517 return result;
13518 }
13519
13520 primitive_iterator_t& operator+=(difference_type n) noexcept
13521 {
13522 m_it += n;
13523 return *this;
13524 }
13525
13526 primitive_iterator_t& operator-=(difference_type n) noexcept
13527 {
13528 m_it -= n;
13529 return *this;
13530 }
13531 };
13532
13533 } // namespace detail
13534 NLOHMANN_JSON_NAMESPACE_END
13535
13536
13537 NLOHMANN_JSON_NAMESPACE_BEGIN
13538 namespace detail
13539 {
13540
13541 /*!
13542 @brief an iterator value
13543
13544 @note This structure could easily be a union, but MSVC currently does not allow
13545 unions members with complex constructors, see https://github.com/nlohmann/json/pull/105.
13546 */
13547 template<typename BasicJsonType> struct internal_iterator
13548 {
13549 /// iterator for JSON objects
13550 typename BasicJsonType::object_t::iterator object_iterator {};
13551 /// iterator for JSON arrays
13552 typename BasicJsonType::array_t::iterator array_iterator {};
13553 /// generic iterator for all other types
13554 primitive_iterator_t primitive_iterator {};
13555 };
13556
13557 } // namespace detail
13558 NLOHMANN_JSON_NAMESPACE_END
13559
13560 // #include <nlohmann/detail/iterators/iter_impl.hpp>
13561 // __ _____ _____ _____
13562 // __| | __| | | | JSON for Modern C++
13563 // | | |__ | | | | | | version 3.12.0
13564 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
13565 //
13566 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
13567 // SPDX-License-Identifier: MIT
13568
13569
13570
13571 #include <iterator> // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next
13572 #include <type_traits> // conditional, is_const, remove_const
13573
13574 // #include <nlohmann/detail/exceptions.hpp>
13575
13576 // #include <nlohmann/detail/iterators/internal_iterator.hpp>
13577
13578 // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
13579
13580 // #include <nlohmann/detail/macro_scope.hpp>
13581
13582 // #include <nlohmann/detail/meta/cpp_future.hpp>
13583
13584 // #include <nlohmann/detail/meta/type_traits.hpp>
13585
13586 // #include <nlohmann/detail/value_t.hpp>
13587
13588
13589 NLOHMANN_JSON_NAMESPACE_BEGIN
13590 namespace detail
13591 {
13592
13593 // forward declare, to be able to friend it later on
13594 template<typename IteratorType> class iteration_proxy;
13595 template<typename IteratorType> class iteration_proxy_value;
13596
13597 /*!
13598 @brief a template for a bidirectional iterator for the @ref basic_json class
13599 This class implements a both iterators (iterator and const_iterator) for the
13600 @ref basic_json class.
13601 @note An iterator is called *initialized* when a pointer to a JSON value has
13602 been set (e.g., by a constructor or a copy assignment). If the iterator is
13603 default-constructed, it is *uninitialized* and most methods are undefined.
13604 **The library uses assertions to detect calls on uninitialized iterators.**
13605 @requirement The class satisfies the following concept requirements:
13606 -
13607 [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
13608 The iterator that can be moved can be moved in both directions (i.e.
13609 incremented and decremented).
13610 @since version 1.0.0, simplified in version 2.0.9, change to bidirectional
13611 iterators in version 3.0.0 (see https://github.com/nlohmann/json/issues/593)
13612 */
13613 template<typename BasicJsonType>
13614 class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions)
13615 {
13616 /// the iterator with BasicJsonType of different const-ness
13617 using other_iter_impl = iter_impl<typename std::conditional<std::is_const<BasicJsonType>::value, typename std::remove_const<BasicJsonType>::type, const BasicJsonType>::type>;
13618 /// allow basic_json to access private members
13619 friend other_iter_impl;
13620 friend BasicJsonType;
13621 friend iteration_proxy<iter_impl>;
13622 friend iteration_proxy_value<iter_impl>;
13623
13624 using object_t = typename BasicJsonType::object_t;
13625 using array_t = typename BasicJsonType::array_t;
13626 // make sure BasicJsonType is basic_json or const basic_json
13627 static_assert(is_basic_json<typename std::remove_const<BasicJsonType>::type>::value,
13628 "iter_impl only accepts (const) basic_json");
13629 // superficial check for the LegacyBidirectionalIterator named requirement
13630 static_assert(std::is_base_of<std::bidirectional_iterator_tag, std::bidirectional_iterator_tag>::value
13631 && std::is_base_of<std::bidirectional_iterator_tag, typename std::iterator_traits<typename array_t::iterator>::iterator_category>::value,
13632 "basic_json iterator assumes array and object type iterators satisfy the LegacyBidirectionalIterator named requirement.");
13633
13634 public:
13635 /// The std::iterator class template (used as a base class to provide typedefs) is deprecated in C++17.
13636 /// The C++ Standard has never required user-defined iterators to derive from std::iterator.
13637 /// A user-defined iterator should provide publicly accessible typedefs named
13638 /// iterator_category, value_type, difference_type, pointer, and reference.
13639 /// Note that value_type is required to be non-const, even for constant iterators.
13640 using iterator_category = std::bidirectional_iterator_tag;
13641
13642 /// the type of the values when the iterator is dereferenced
13643 using value_type = typename BasicJsonType::value_type;
13644 /// a type to represent differences between iterators
13645 using difference_type = typename BasicJsonType::difference_type;
13646 /// defines a pointer to the type iterated over (value_type)
13647 using pointer = typename std::conditional<std::is_const<BasicJsonType>::value,
13648 typename BasicJsonType::const_pointer,
13649 typename BasicJsonType::pointer>::type;
13650 /// defines a reference to the type iterated over (value_type)
13651 using reference =
13652 typename std::conditional<std::is_const<BasicJsonType>::value,
13653 typename BasicJsonType::const_reference,
13654 typename BasicJsonType::reference>::type;
13655
13656 iter_impl() = default;
13657 ~iter_impl() = default;
13658 iter_impl(iter_impl&&) noexcept = default;
13659 iter_impl& operator=(iter_impl&&) noexcept = default;
13660
13661 /*!
13662 @brief constructor for a given JSON instance
13663 @param[in] object pointer to a JSON object for this iterator
13664 @pre object != nullptr
13665 @post The iterator is initialized; i.e. `m_object != nullptr`.
13666 */
13667 explicit iter_impl(pointer object) noexcept : m_object(object)
13668 {
13669 JSON_ASSERT(m_object != nullptr);
13670
13671 switch (m_object->m_data.m_type)
13672 {
13673 case value_t::object:
13674 {
13675 m_it.object_iterator = typename object_t::iterator();
13676 break;
13677 }
13678
13679 case value_t::array:
13680 {
13681 m_it.array_iterator = typename array_t::iterator();
13682 break;
13683 }
13684
13685 case value_t::null:
13686 case value_t::string:
13687 case value_t::boolean:
13688 case value_t::number_integer:
13689 case value_t::number_unsigned:
13690 case value_t::number_float:
13691 case value_t::binary:
13692 case value_t::discarded:
13693 default:
13694 {
13695 m_it.primitive_iterator = primitive_iterator_t();
13696 break;
13697 }
13698 }
13699 }
13700
13701 /*!
13702 @note The conventional copy constructor and copy assignment are implicitly
13703 defined. Combined with the following converting constructor and
13704 assignment, they support: (1) copy from iterator to iterator, (2)
13705 copy from const iterator to const iterator, and (3) conversion from
13706 iterator to const iterator. However conversion from const iterator
13707 to iterator is not defined.
13708 */
13709
13710 /*!
13711 @brief const copy constructor
13712 @param[in] other const iterator to copy from
13713 @note This copy constructor had to be defined explicitly to circumvent a bug
13714 occurring on msvc v19.0 compiler (VS 2015) debug build. For more
13715 information refer to: https://github.com/nlohmann/json/issues/1608
13716 */
13717 iter_impl(const iter_impl<const BasicJsonType>& other) noexcept
13718 : m_object(other.m_object), m_it(other.m_it)
13719 {}
13720
13721 /*!
13722 @brief converting assignment
13723 @param[in] other const iterator to copy from
13724 @return const/non-const iterator
13725 @note It is not checked whether @a other is initialized.
13726 */
13727 iter_impl& operator=(const iter_impl<const BasicJsonType>& other) noexcept
13728 {
13729 if (&other != this)
13730 {
13731 m_object = other.m_object;
13732 m_it = other.m_it;
13733 }
13734 return *this;
13735 }
13736
13737 /*!
13738 @brief converting constructor
13739 @param[in] other non-const iterator to copy from
13740 @note It is not checked whether @a other is initialized.
13741 */
13742 iter_impl(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept
13743 : m_object(other.m_object), m_it(other.m_it)
13744 {}
13745
13746 /*!
13747 @brief converting assignment
13748 @param[in] other non-const iterator to copy from
13749 @return const/non-const iterator
13750 @note It is not checked whether @a other is initialized.
13751 */
13752 iter_impl& operator=(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept // NOLINT(cert-oop54-cpp)
13753 {
13754 m_object = other.m_object;
13755 m_it = other.m_it;
13756 return *this;
13757 }
13758
13759 JSON_PRIVATE_UNLESS_TESTED:
13760 /*!
13761 @brief set the iterator to the first value
13762 @pre The iterator is initialized; i.e. `m_object != nullptr`.
13763 */
13764 void set_begin() noexcept
13765 {
13766 JSON_ASSERT(m_object != nullptr);
13767
13768 switch (m_object->m_data.m_type)
13769 {
13770 case value_t::object:
13771 {
13772 m_it.object_iterator = m_object->m_data.m_value.object->begin();
13773 break;
13774 }
13775
13776 case value_t::array:
13777 {
13778 m_it.array_iterator = m_object->m_data.m_value.array->begin();
13779 break;
13780 }
13781
13782 case value_t::null:
13783 {
13784 // set to end so begin()==end() is true: null is empty
13785 m_it.primitive_iterator.set_end();
13786 break;
13787 }
13788
13789 case value_t::string:
13790 case value_t::boolean:
13791 case value_t::number_integer:
13792 case value_t::number_unsigned:
13793 case value_t::number_float:
13794 case value_t::binary:
13795 case value_t::discarded:
13796 default:
13797 {
13798 m_it.primitive_iterator.set_begin();
13799 break;
13800 }
13801 }
13802 }
13803
13804 /*!
13805 @brief set the iterator past the last value
13806 @pre The iterator is initialized; i.e. `m_object != nullptr`.
13807 */
13808 void set_end() noexcept
13809 {
13810 JSON_ASSERT(m_object != nullptr);
13811
13812 switch (m_object->m_data.m_type)
13813 {
13814 case value_t::object:
13815 {
13816 m_it.object_iterator = m_object->m_data.m_value.object->end();
13817 break;
13818 }
13819
13820 case value_t::array:
13821 {
13822 m_it.array_iterator = m_object->m_data.m_value.array->end();
13823 break;
13824 }
13825
13826 case value_t::null:
13827 case value_t::string:
13828 case value_t::boolean:
13829 case value_t::number_integer:
13830 case value_t::number_unsigned:
13831 case value_t::number_float:
13832 case value_t::binary:
13833 case value_t::discarded:
13834 default:
13835 {
13836 m_it.primitive_iterator.set_end();
13837 break;
13838 }
13839 }
13840 }
13841
13842 public:
13843 /*!
13844 @brief return a reference to the value pointed to by the iterator
13845 @pre The iterator is initialized; i.e. `m_object != nullptr`.
13846 */
13847 reference operator*() const
13848 {
13849 JSON_ASSERT(m_object != nullptr);
13850
13851 switch (m_object->m_data.m_type)
13852 {
13853 case value_t::object:
13854 {
13855 JSON_ASSERT(m_it.object_iterator != m_object->m_data.m_value.object->end());
13856 return m_it.object_iterator->second;
13857 }
13858
13859 case value_t::array:
13860 {
13861 JSON_ASSERT(m_it.array_iterator != m_object->m_data.m_value.array->end());
13862 return *m_it.array_iterator;
13863 }
13864
13865 case value_t::null:
13866 JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
13867
13868 case value_t::string:
13869 case value_t::boolean:
13870 case value_t::number_integer:
13871 case value_t::number_unsigned:
13872 case value_t::number_float:
13873 case value_t::binary:
13874 case value_t::discarded:
13875 default:
13876 {
13877 if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
13878 {
13879 return *m_object;
13880 }
13881
13882 JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
13883 }
13884 }
13885 }
13886
13887 /*!
13888 @brief dereference the iterator
13889 @pre The iterator is initialized; i.e. `m_object != nullptr`.
13890 */
13891 pointer operator->() const
13892 {
13893 JSON_ASSERT(m_object != nullptr);
13894
13895 switch (m_object->m_data.m_type)
13896 {
13897 case value_t::object:
13898 {
13899 JSON_ASSERT(m_it.object_iterator != m_object->m_data.m_value.object->end());
13900 return &(m_it.object_iterator->second);
13901 }
13902
13903 case value_t::array:
13904 {
13905 JSON_ASSERT(m_it.array_iterator != m_object->m_data.m_value.array->end());
13906 return &*m_it.array_iterator;
13907 }
13908
13909 case value_t::null:
13910 case value_t::string:
13911 case value_t::boolean:
13912 case value_t::number_integer:
13913 case value_t::number_unsigned:
13914 case value_t::number_float:
13915 case value_t::binary:
13916 case value_t::discarded:
13917 default:
13918 {
13919 if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
13920 {
13921 return m_object;
13922 }
13923
13924 JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
13925 }
13926 }
13927 }
13928
13929 /*!
13930 @brief post-increment (it++)
13931 @pre The iterator is initialized; i.e. `m_object != nullptr`.
13932 */
13933 iter_impl operator++(int)& // NOLINT(cert-dcl21-cpp)
13934 {
13935 auto result = *this;
13936 ++(*this);
13937 return result;
13938 }
13939
13940 /*!
13941 @brief pre-increment (++it)
13942 @pre The iterator is initialized; i.e. `m_object != nullptr`.
13943 */
13944 iter_impl& operator++()
13945 {
13946 JSON_ASSERT(m_object != nullptr);
13947
13948 switch (m_object->m_data.m_type)
13949 {
13950 case value_t::object:
13951 {
13952 std::advance(m_it.object_iterator, 1);
13953 break;
13954 }
13955
13956 case value_t::array:
13957 {
13958 std::advance(m_it.array_iterator, 1);
13959 break;
13960 }
13961
13962 case value_t::null:
13963 case value_t::string:
13964 case value_t::boolean:
13965 case value_t::number_integer:
13966 case value_t::number_unsigned:
13967 case value_t::number_float:
13968 case value_t::binary:
13969 case value_t::discarded:
13970 default:
13971 {
13972 ++m_it.primitive_iterator;
13973 break;
13974 }
13975 }
13976
13977 return *this;
13978 }
13979
13980 /*!
13981 @brief post-decrement (it--)
13982 @pre The iterator is initialized; i.e. `m_object != nullptr`.
13983 */
13984 iter_impl operator--(int)& // NOLINT(cert-dcl21-cpp)
13985 {
13986 auto result = *this;
13987 --(*this);
13988 return result;
13989 }
13990
13991 /*!
13992 @brief pre-decrement (--it)
13993 @pre The iterator is initialized; i.e. `m_object != nullptr`.
13994 */
13995 iter_impl& operator--()
13996 {
13997 JSON_ASSERT(m_object != nullptr);
13998
13999 switch (m_object->m_data.m_type)
14000 {
14001 case value_t::object:
14002 {
14003 std::advance(m_it.object_iterator, -1);
14004 break;
14005 }
14006
14007 case value_t::array:
14008 {
14009 std::advance(m_it.array_iterator, -1);
14010 break;
14011 }
14012
14013 case value_t::null:
14014 case value_t::string:
14015 case value_t::boolean:
14016 case value_t::number_integer:
14017 case value_t::number_unsigned:
14018 case value_t::number_float:
14019 case value_t::binary:
14020 case value_t::discarded:
14021 default:
14022 {
14023 --m_it.primitive_iterator;
14024 break;
14025 }
14026 }
14027
14028 return *this;
14029 }
14030
14031 /*!
14032 @brief comparison: equal
14033 @pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
14034 */
14035 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 >
14036 bool operator==(const IterImpl& other) const
14037 {
14038 // if objects are not the same, the comparison is undefined
14039 if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
14040 {
14041 JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object));
14042 }
14043
14044 // value-initialized forward iterators can be compared, and must compare equal to other value-initialized iterators of the same type #4493
14045 if (m_object == nullptr)
14046 {
14047 return true;
14048 }
14049
14050 switch (m_object->m_data.m_type)
14051 {
14052 case value_t::object:
14053 return (m_it.object_iterator == other.m_it.object_iterator);
14054
14055 case value_t::array:
14056 return (m_it.array_iterator == other.m_it.array_iterator);
14057
14058 case value_t::null:
14059 case value_t::string:
14060 case value_t::boolean:
14061 case value_t::number_integer:
14062 case value_t::number_unsigned:
14063 case value_t::number_float:
14064 case value_t::binary:
14065 case value_t::discarded:
14066 default:
14067 return (m_it.primitive_iterator == other.m_it.primitive_iterator);
14068 }
14069 }
14070
14071 /*!
14072 @brief comparison: not equal
14073 @pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
14074 */
14075 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 >
14076 bool operator!=(const IterImpl& other) const
14077 {
14078 return !operator==(other);
14079 }
14080
14081 /*!
14082 @brief comparison: smaller
14083 @pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
14084 */
14085 bool operator<(const iter_impl& other) const
14086 {
14087 // if objects are not the same, the comparison is undefined
14088 if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
14089 {
14090 JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object));
14091 }
14092
14093 // value-initialized forward iterators can be compared, and must compare equal to other value-initialized iterators of the same type #4493
14094 if (m_object == nullptr)
14095 {
14096 // the iterators are both value-initialized and are to be considered equal, but this function checks for smaller, so we return false
14097 return false;
14098 }
14099
14100 switch (m_object->m_data.m_type)
14101 {
14102 case value_t::object:
14103 JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators", m_object));
14104
14105 case value_t::array:
14106 return (m_it.array_iterator < other.m_it.array_iterator);
14107
14108 case value_t::null:
14109 case value_t::string:
14110 case value_t::boolean:
14111 case value_t::number_integer:
14112 case value_t::number_unsigned:
14113 case value_t::number_float:
14114 case value_t::binary:
14115 case value_t::discarded:
14116 default:
14117 return (m_it.primitive_iterator < other.m_it.primitive_iterator);
14118 }
14119 }
14120
14121 /*!
14122 @brief comparison: less than or equal
14123 @pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
14124 */
14125 bool operator<=(const iter_impl& other) const
14126 {
14127 return !other.operator < (*this);
14128 }
14129
14130 /*!
14131 @brief comparison: greater than
14132 @pre (1) Both iterators are initialized to point to the same object, or (2) both iterators are value-initialized.
14133 */
14134 bool operator>(const iter_impl& other) const
14135 {
14136 return !operator<=(other);
14137 }
14138
14139 /*!
14140 @brief comparison: greater than or equal
14141 @pre (1) The iterator is initialized; i.e. `m_object != nullptr`, or (2) both iterators are value-initialized.
14142 */
14143 bool operator>=(const iter_impl& other) const
14144 {
14145 return !operator<(other);
14146 }
14147
14148 /*!
14149 @brief add to iterator
14150 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14151 */
14152 iter_impl& operator+=(difference_type i)
14153 {
14154 JSON_ASSERT(m_object != nullptr);
14155
14156 switch (m_object->m_data.m_type)
14157 {
14158 case value_t::object:
14159 JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", m_object));
14160
14161 case value_t::array:
14162 {
14163 std::advance(m_it.array_iterator, i);
14164 break;
14165 }
14166
14167 case value_t::null:
14168 case value_t::string:
14169 case value_t::boolean:
14170 case value_t::number_integer:
14171 case value_t::number_unsigned:
14172 case value_t::number_float:
14173 case value_t::binary:
14174 case value_t::discarded:
14175 default:
14176 {
14177 m_it.primitive_iterator += i;
14178 break;
14179 }
14180 }
14181
14182 return *this;
14183 }
14184
14185 /*!
14186 @brief subtract from iterator
14187 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14188 */
14189 iter_impl& operator-=(difference_type i)
14190 {
14191 return operator+=(-i);
14192 }
14193
14194 /*!
14195 @brief add to iterator
14196 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14197 */
14198 iter_impl operator+(difference_type i) const
14199 {
14200 auto result = *this;
14201 result += i;
14202 return result;
14203 }
14204
14205 /*!
14206 @brief addition of distance and iterator
14207 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14208 */
14209 friend iter_impl operator+(difference_type i, const iter_impl& it)
14210 {
14211 auto result = it;
14212 result += i;
14213 return result;
14214 }
14215
14216 /*!
14217 @brief subtract from iterator
14218 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14219 */
14220 iter_impl operator-(difference_type i) const
14221 {
14222 auto result = *this;
14223 result -= i;
14224 return result;
14225 }
14226
14227 /*!
14228 @brief return difference
14229 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14230 */
14231 difference_type operator-(const iter_impl& other) const
14232 {
14233 JSON_ASSERT(m_object != nullptr);
14234
14235 switch (m_object->m_data.m_type)
14236 {
14237 case value_t::object:
14238 JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", m_object));
14239
14240 case value_t::array:
14241 return m_it.array_iterator - other.m_it.array_iterator;
14242
14243 case value_t::null:
14244 case value_t::string:
14245 case value_t::boolean:
14246 case value_t::number_integer:
14247 case value_t::number_unsigned:
14248 case value_t::number_float:
14249 case value_t::binary:
14250 case value_t::discarded:
14251 default:
14252 return m_it.primitive_iterator - other.m_it.primitive_iterator;
14253 }
14254 }
14255
14256 /*!
14257 @brief access to successor
14258 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14259 */
14260 reference operator[](difference_type n) const
14261 {
14262 JSON_ASSERT(m_object != nullptr);
14263
14264 switch (m_object->m_data.m_type)
14265 {
14266 case value_t::object:
14267 JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators", m_object));
14268
14269 case value_t::array:
14270 return *std::next(m_it.array_iterator, n);
14271
14272 case value_t::null:
14273 JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
14274
14275 case value_t::string:
14276 case value_t::boolean:
14277 case value_t::number_integer:
14278 case value_t::number_unsigned:
14279 case value_t::number_float:
14280 case value_t::binary:
14281 case value_t::discarded:
14282 default:
14283 {
14284 if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.get_value() == -n))
14285 {
14286 return *m_object;
14287 }
14288
14289 JSON_THROW(invalid_iterator::create(214, "cannot get value", m_object));
14290 }
14291 }
14292 }
14293
14294 /*!
14295 @brief return the key of an object iterator
14296 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14297 */
14298 const typename object_t::key_type& key() const
14299 {
14300 JSON_ASSERT(m_object != nullptr);
14301
14302 if (JSON_HEDLEY_LIKELY(m_object->is_object()))
14303 {
14304 return m_it.object_iterator->first;
14305 }
14306
14307 JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators", m_object));
14308 }
14309
14310 /*!
14311 @brief return the value of an iterator
14312 @pre The iterator is initialized; i.e. `m_object != nullptr`.
14313 */
14314 reference value() const
14315 {
14316 return operator*();
14317 }
14318
14319 JSON_PRIVATE_UNLESS_TESTED:
14320 /// associated JSON instance
14321 pointer m_object = nullptr;
14322 /// the actual iterator of the associated instance
14323 internal_iterator<typename std::remove_const<BasicJsonType>::type> m_it {};
14324 };
14325
14326 } // namespace detail
14327 NLOHMANN_JSON_NAMESPACE_END
14328
14329 // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
14330
14331 // #include <nlohmann/detail/iterators/json_reverse_iterator.hpp>
14332 // __ _____ _____ _____
14333 // __| | __| | | | JSON for Modern C++
14334 // | | |__ | | | | | | version 3.12.0
14335 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
14336 //
14337 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
14338 // SPDX-License-Identifier: MIT
14339
14340
14341
14342 #include <cstddef> // ptrdiff_t
14343 #include <iterator> // reverse_iterator
14344 #include <utility> // declval
14345
14346 // #include <nlohmann/detail/abi_macros.hpp>
14347
14348
14349 NLOHMANN_JSON_NAMESPACE_BEGIN
14350 namespace detail
14351 {
14352
14353 //////////////////////
14354 // reverse_iterator //
14355 //////////////////////
14356
14357 /*!
14358 @brief a template for a reverse iterator class
14359
14360 @tparam Base the base iterator type to reverse. Valid types are @ref
14361 iterator (to create @ref reverse_iterator) and @ref const_iterator (to
14362 create @ref const_reverse_iterator).
14363
14364 @requirement The class satisfies the following concept requirements:
14365 -
14366 [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
14367 The iterator that can be moved can be moved in both directions (i.e.
14368 incremented and decremented).
14369 - [OutputIterator](https://en.cppreference.com/w/cpp/named_req/OutputIterator):
14370 It is possible to write to the pointed-to element (only if @a Base is
14371 @ref iterator).
14372
14373 @since version 1.0.0
14374 */
14375 template<typename Base>
14376 class json_reverse_iterator : public std::reverse_iterator<Base>
14377 {
14378 public:
14379 using difference_type = std::ptrdiff_t;
14380 /// shortcut to the reverse iterator adapter
14381 using base_iterator = std::reverse_iterator<Base>;
14382 /// the reference type for the pointed-to element
14383 using reference = typename Base::reference;
14384
14385 /// create reverse iterator from iterator
14386 explicit json_reverse_iterator(const typename base_iterator::iterator_type& it) noexcept
14387 : base_iterator(it) {}
14388
14389 /// create reverse iterator from base class
14390 explicit json_reverse_iterator(const base_iterator& it) noexcept : base_iterator(it) {}
14391
14392 /// post-increment (it++)
14393 json_reverse_iterator operator++(int)& // NOLINT(cert-dcl21-cpp)
14394 {
14395 return static_cast<json_reverse_iterator>(base_iterator::operator++(1));
14396 }
14397
14398 /// pre-increment (++it)
14399 json_reverse_iterator& operator++()
14400 {
14401 return static_cast<json_reverse_iterator&>(base_iterator::operator++());
14402 }
14403
14404 /// post-decrement (it--)
14405 json_reverse_iterator operator--(int)& // NOLINT(cert-dcl21-cpp)
14406 {
14407 return static_cast<json_reverse_iterator>(base_iterator::operator--(1));
14408 }
14409
14410 /// pre-decrement (--it)
14411 json_reverse_iterator& operator--()
14412 {
14413 return static_cast<json_reverse_iterator&>(base_iterator::operator--());
14414 }
14415
14416 /// add to iterator
14417 json_reverse_iterator& operator+=(difference_type i)
14418 {
14419 return static_cast<json_reverse_iterator&>(base_iterator::operator+=(i));
14420 }
14421
14422 /// add to iterator
14423 json_reverse_iterator operator+(difference_type i) const
14424 {
14425 return static_cast<json_reverse_iterator>(base_iterator::operator+(i));
14426 }
14427
14428 /// subtract from iterator
14429 json_reverse_iterator operator-(difference_type i) const
14430 {
14431 return static_cast<json_reverse_iterator>(base_iterator::operator-(i));
14432 }
14433
14434 /// return difference
14435 difference_type operator-(const json_reverse_iterator& other) const
14436 {
14437 return base_iterator(*this) - base_iterator(other);
14438 }
14439
14440 /// access to successor
14441 reference operator[](difference_type n) const
14442 {
14443 return *(this->operator+(n));
14444 }
14445
14446 /// return the key of an object iterator
14447 auto key() const -> decltype(std::declval<Base>().key())
14448 {
14449 auto it = --this->base();
14450 return it.key();
14451 }
14452
14453 /// return the value of an iterator
14454 reference value() const
14455 {
14456 auto it = --this->base();
14457 return it.operator * ();
14458 }
14459 };
14460
14461 } // namespace detail
14462 NLOHMANN_JSON_NAMESPACE_END
14463
14464 // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
14465
14466 // #include <nlohmann/detail/json_custom_base_class.hpp>
14467 // __ _____ _____ _____
14468 // __| | __| | | | JSON for Modern C++
14469 // | | |__ | | | | | | version 3.12.0
14470 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
14471 //
14472 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
14473 // SPDX-License-Identifier: MIT
14474
14475
14476
14477 #include <type_traits> // conditional, is_same
14478
14479 // #include <nlohmann/detail/abi_macros.hpp>
14480
14481
14482 NLOHMANN_JSON_NAMESPACE_BEGIN
14483 namespace detail
14484 {
14485
14486 /*!
14487 @brief Default base class of the @ref basic_json class.
14488
14489 So that the correct implementations of the copy / move ctors / assign operators
14490 of @ref basic_json do not require complex case distinctions
14491 (no base class / custom base class used as customization point),
14492 @ref basic_json always has a base class.
14493 By default, this class is used because it is empty and thus has no effect
14494 on the behavior of @ref basic_json.
14495 */
14496 struct json_default_base {};
14497
14498 template<class T>
14499 using json_base_class = typename std::conditional <
14500 std::is_same<T, void>::value,
14501 json_default_base,
14502 T
14503 >::type;
14504
14505 } // namespace detail
14506 NLOHMANN_JSON_NAMESPACE_END
14507
14508 // #include <nlohmann/detail/json_pointer.hpp>
14509 // __ _____ _____ _____
14510 // __| | __| | | | JSON for Modern C++
14511 // | | |__ | | | | | | version 3.12.0
14512 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
14513 //
14514 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
14515 // SPDX-License-Identifier: MIT
14516
14517
14518
14519 #include <algorithm> // all_of
14520 #include <cctype> // isdigit
14521 #include <cerrno> // errno, ERANGE
14522 #include <cstdlib> // strtoull
14523 #ifndef JSON_NO_IO
14524 #include <iosfwd> // ostream
14525 #endif // JSON_NO_IO
14526 #include <limits> // max
14527 #include <numeric> // accumulate
14528 #include <string> // string
14529 #include <utility> // move
14530 #include <vector> // vector
14531
14532 // #include <nlohmann/detail/exceptions.hpp>
14533
14534 // #include <nlohmann/detail/macro_scope.hpp>
14535
14536 // #include <nlohmann/detail/string_concat.hpp>
14537
14538 // #include <nlohmann/detail/string_escape.hpp>
14539
14540 // #include <nlohmann/detail/value_t.hpp>
14541
14542
14543 NLOHMANN_JSON_NAMESPACE_BEGIN
14544
14545 /// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document
14546 /// @sa https://json.nlohmann.me/api/json_pointer/
14547 template<typename RefStringType>
14548 class json_pointer
14549 {
14550 // allow basic_json to access private members
14551 NLOHMANN_BASIC_JSON_TPL_DECLARATION
14552 friend class basic_json;
14553
14554 template<typename>
14555 friend class json_pointer;
14556
14557 template<typename T>
14558 struct string_t_helper
14559 {
14560 using type = T;
14561 };
14562
14563 NLOHMANN_BASIC_JSON_TPL_DECLARATION
14564 struct string_t_helper<NLOHMANN_BASIC_JSON_TPL>
14565 {
14566 using type = StringType;
14567 };
14568
14569 public:
14570 // for backwards compatibility accept BasicJsonType
14571 using string_t = typename string_t_helper<RefStringType>::type;
14572
14573 /// @brief create JSON pointer
14574 /// @sa https://json.nlohmann.me/api/json_pointer/json_pointer/
14575 explicit json_pointer(const string_t& s = "")
14576 : reference_tokens(split(s))
14577 {}
14578
14579 /// @brief return a string representation of the JSON pointer
14580 /// @sa https://json.nlohmann.me/api/json_pointer/to_string/
14581 string_t to_string() const
14582 {
14583 return std::accumulate(reference_tokens.begin(), reference_tokens.end(),
14584 string_t{},
14585 [](const string_t& a, const string_t& b)
14586 {
14587 return detail::concat(a, '/', detail::escape(b));
14588 });
14589 }
14590
14591 /// @brief return a string representation of the JSON pointer
14592 /// @sa https://json.nlohmann.me/api/json_pointer/operator_string/
14593 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, to_string())
14594 operator string_t() const
14595 {
14596 return to_string();
14597 }
14598
14599 #ifndef JSON_NO_IO
14600 /// @brief write string representation of the JSON pointer to stream
14601 /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
14602 friend std::ostream& operator<<(std::ostream& o, const json_pointer& ptr)
14603 {
14604 o << ptr.to_string();
14605 return o;
14606 }
14607 #endif
14608
14609 /// @brief append another JSON pointer at the end of this JSON pointer
14610 /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
14611 json_pointer& operator/=(const json_pointer& ptr)
14612 {
14613 reference_tokens.insert(reference_tokens.end(),
14614 ptr.reference_tokens.begin(),
14615 ptr.reference_tokens.end());
14616 return *this;
14617 }
14618
14619 /// @brief append an unescaped reference token at the end of this JSON pointer
14620 /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
14621 json_pointer& operator/=(string_t token)
14622 {
14623 push_back(std::move(token));
14624 return *this;
14625 }
14626
14627 /// @brief append an array index at the end of this JSON pointer
14628 /// @sa https://json.nlohmann.me/api/json_pointer/operator_slasheq/
14629 json_pointer& operator/=(std::size_t array_idx)
14630 {
14631 return *this /= std::to_string(array_idx);
14632 }
14633
14634 /// @brief create a new JSON pointer by appending the right JSON pointer at the end of the left JSON pointer
14635 /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
14636 friend json_pointer operator/(const json_pointer& lhs,
14637 const json_pointer& rhs)
14638 {
14639 return json_pointer(lhs) /= rhs;
14640 }
14641
14642 /// @brief create a new JSON pointer by appending the unescaped token at the end of the JSON pointer
14643 /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
14644 friend json_pointer operator/(const json_pointer& lhs, string_t token) // NOLINT(performance-unnecessary-value-param)
14645 {
14646 return json_pointer(lhs) /= std::move(token);
14647 }
14648
14649 /// @brief create a new JSON pointer by appending the array-index-token at the end of the JSON pointer
14650 /// @sa https://json.nlohmann.me/api/json_pointer/operator_slash/
14651 friend json_pointer operator/(const json_pointer& lhs, std::size_t array_idx)
14652 {
14653 return json_pointer(lhs) /= array_idx;
14654 }
14655
14656 /// @brief returns the parent of this JSON pointer
14657 /// @sa https://json.nlohmann.me/api/json_pointer/parent_pointer/
14658 json_pointer parent_pointer() const
14659 {
14660 if (empty())
14661 {
14662 return *this;
14663 }
14664
14665 json_pointer res = *this;
14666 res.pop_back();
14667 return res;
14668 }
14669
14670 /// @brief remove last reference token
14671 /// @sa https://json.nlohmann.me/api/json_pointer/pop_back/
14672 void pop_back()
14673 {
14674 if (JSON_HEDLEY_UNLIKELY(empty()))
14675 {
14676 JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
14677 }
14678
14679 reference_tokens.pop_back();
14680 }
14681
14682 /// @brief return last reference token
14683 /// @sa https://json.nlohmann.me/api/json_pointer/back/
14684 const string_t& back() const
14685 {
14686 if (JSON_HEDLEY_UNLIKELY(empty()))
14687 {
14688 JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
14689 }
14690
14691 return reference_tokens.back();
14692 }
14693
14694 /// @brief append an unescaped token at the end of the reference pointer
14695 /// @sa https://json.nlohmann.me/api/json_pointer/push_back/
14696 void push_back(const string_t& token)
14697 {
14698 reference_tokens.push_back(token);
14699 }
14700
14701 /// @brief append an unescaped token at the end of the reference pointer
14702 /// @sa https://json.nlohmann.me/api/json_pointer/push_back/
14703 void push_back(string_t&& token)
14704 {
14705 reference_tokens.push_back(std::move(token));
14706 }
14707
14708 /// @brief return whether pointer points to the root document
14709 /// @sa https://json.nlohmann.me/api/json_pointer/empty/
14710 bool empty() const noexcept
14711 {
14712 return reference_tokens.empty();
14713 }
14714
14715 private:
14716 /*!
14717 @param[in] s reference token to be converted into an array index
14718
14719 @return integer representation of @a s
14720
14721 @throw parse_error.106 if an array index begins with '0'
14722 @throw parse_error.109 if an array index begins not with a digit
14723 @throw out_of_range.404 if string @a s could not be converted to an integer
14724 @throw out_of_range.410 if an array index exceeds size_type
14725 */
14726 template<typename BasicJsonType>
14727 static typename BasicJsonType::size_type array_index(const string_t& s)
14728 {
14729 using size_type = typename BasicJsonType::size_type;
14730
14731 // error condition (cf. RFC 6901, Sect. 4)
14732 if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && s[0] == '0'))
14733 {
14734 JSON_THROW(detail::parse_error::create(106, 0, detail::concat("array index '", s, "' must not begin with '0'"), nullptr));
14735 }
14736
14737 // error condition (cf. RFC 6901, Sect. 4)
14738 if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && !(s[0] >= '1' && s[0] <= '9')))
14739 {
14740 JSON_THROW(detail::parse_error::create(109, 0, detail::concat("array index '", s, "' is not a number"), nullptr));
14741 }
14742
14743 const char* p = s.c_str();
14744 char* p_end = nullptr; // NOLINT(misc-const-correctness)
14745 errno = 0; // strtoull doesn't reset errno
14746 const unsigned long long res = std::strtoull(p, &p_end, 10); // NOLINT(runtime/int)
14747 if (p == p_end // invalid input or empty string
14748 || errno == ERANGE // out of range
14749 || JSON_HEDLEY_UNLIKELY(static_cast<std::size_t>(p_end - p) != s.size())) // incomplete read
14750 {
14751 JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", s, "'"), nullptr));
14752 }
14753
14754 // only triggered on special platforms (like 32bit), see also
14755 // https://github.com/nlohmann/json/pull/2203
14756 if (res >= static_cast<unsigned long long>((std::numeric_limits<size_type>::max)())) // NOLINT(runtime/int)
14757 {
14758 JSON_THROW(detail::out_of_range::create(410, detail::concat("array index ", s, " exceeds size_type"), nullptr)); // LCOV_EXCL_LINE
14759 }
14760
14761 return static_cast<size_type>(res);
14762 }
14763
14764 JSON_PRIVATE_UNLESS_TESTED:
14765 json_pointer top() const
14766 {
14767 if (JSON_HEDLEY_UNLIKELY(empty()))
14768 {
14769 JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", nullptr));
14770 }
14771
14772 json_pointer result = *this;
14773 result.reference_tokens = {reference_tokens[0]};
14774 return result;
14775 }
14776
14777 private:
14778 /*!
14779 @brief create and return a reference to the pointed to value
14780
14781 @complexity Linear in the number of reference tokens.
14782
14783 @throw parse_error.109 if array index is not a number
14784 @throw type_error.313 if value cannot be unflattened
14785 */
14786 template<typename BasicJsonType>
14787 BasicJsonType& get_and_create(BasicJsonType& j) const
14788 {
14789 auto* result = &j;
14790
14791 // in case no reference tokens exist, return a reference to the JSON value
14792 // j which will be overwritten by a primitive value
14793 for (const auto& reference_token : reference_tokens)
14794 {
14795 switch (result->type())
14796 {
14797 case detail::value_t::null:
14798 {
14799 if (reference_token == "0")
14800 {
14801 // start a new array if reference token is 0
14802 result = &result->operator[](0);
14803 }
14804 else
14805 {
14806 // start a new object otherwise
14807 result = &result->operator[](reference_token);
14808 }
14809 break;
14810 }
14811
14812 case detail::value_t::object:
14813 {
14814 // create an entry in the object
14815 result = &result->operator[](reference_token);
14816 break;
14817 }
14818
14819 case detail::value_t::array:
14820 {
14821 // create an entry in the array
14822 result = &result->operator[](array_index<BasicJsonType>(reference_token));
14823 break;
14824 }
14825
14826 /*
14827 The following code is only reached if there exists a reference
14828 token _and_ the current value is primitive. In this case, we have
14829 an error situation, because primitive values may only occur as
14830 single value; that is, with an empty list of reference tokens.
14831 */
14832 case detail::value_t::string:
14833 case detail::value_t::boolean:
14834 case detail::value_t::number_integer:
14835 case detail::value_t::number_unsigned:
14836 case detail::value_t::number_float:
14837 case detail::value_t::binary:
14838 case detail::value_t::discarded:
14839 default:
14840 JSON_THROW(detail::type_error::create(313, "invalid value to unflatten", &j));
14841 }
14842 }
14843
14844 return *result;
14845 }
14846
14847 /*!
14848 @brief return a reference to the pointed to value
14849
14850 @note This version does not throw if a value is not present, but tries to
14851 create nested values instead. For instance, calling this function
14852 with pointer `"/this/that"` on a null value is equivalent to calling
14853 `operator[]("this").operator[]("that")` on that value, effectively
14854 changing the null value to an object.
14855
14856 @param[in] ptr a JSON value
14857
14858 @return reference to the JSON value pointed to by the JSON pointer
14859
14860 @complexity Linear in the length of the JSON pointer.
14861
14862 @throw parse_error.106 if an array index begins with '0'
14863 @throw parse_error.109 if an array index was not a number
14864 @throw out_of_range.404 if the JSON pointer can not be resolved
14865 */
14866 template<typename BasicJsonType>
14867 BasicJsonType& get_unchecked(BasicJsonType* ptr) const
14868 {
14869 for (const auto& reference_token : reference_tokens)
14870 {
14871 // convert null values to arrays or objects before continuing
14872 if (ptr->is_null())
14873 {
14874 // check if reference token is a number
14875 const bool nums =
14876 std::all_of(reference_token.begin(), reference_token.end(),
14877 [](const unsigned char x)
14878 {
14879 return std::isdigit(x);
14880 });
14881
14882 // change value to array for numbers or "-" or to object otherwise
14883 *ptr = (nums || reference_token == "-")
14884 ? detail::value_t::array
14885 : detail::value_t::object;
14886 }
14887
14888 switch (ptr->type())
14889 {
14890 case detail::value_t::object:
14891 {
14892 // use unchecked object access
14893 ptr = &ptr->operator[](reference_token);
14894 break;
14895 }
14896
14897 case detail::value_t::array:
14898 {
14899 if (reference_token == "-")
14900 {
14901 // explicitly treat "-" as index beyond the end
14902 ptr = &ptr->operator[](ptr->m_data.m_value.array->size());
14903 }
14904 else
14905 {
14906 // convert array index to number; unchecked access
14907 ptr = &ptr->operator[](array_index<BasicJsonType>(reference_token));
14908 }
14909 break;
14910 }
14911
14912 case detail::value_t::null:
14913 case detail::value_t::string:
14914 case detail::value_t::boolean:
14915 case detail::value_t::number_integer:
14916 case detail::value_t::number_unsigned:
14917 case detail::value_t::number_float:
14918 case detail::value_t::binary:
14919 case detail::value_t::discarded:
14920 default:
14921 JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
14922 }
14923 }
14924
14925 return *ptr;
14926 }
14927
14928 /*!
14929 @throw parse_error.106 if an array index begins with '0'
14930 @throw parse_error.109 if an array index was not a number
14931 @throw out_of_range.402 if the array index '-' is used
14932 @throw out_of_range.404 if the JSON pointer can not be resolved
14933 */
14934 template<typename BasicJsonType>
14935 BasicJsonType& get_checked(BasicJsonType* ptr) const
14936 {
14937 for (const auto& reference_token : reference_tokens)
14938 {
14939 switch (ptr->type())
14940 {
14941 case detail::value_t::object:
14942 {
14943 // note: at performs range check
14944 ptr = &ptr->at(reference_token);
14945 break;
14946 }
14947
14948 case detail::value_t::array:
14949 {
14950 if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
14951 {
14952 // "-" always fails the range check
14953 JSON_THROW(detail::out_of_range::create(402, detail::concat(
14954 "array index '-' (", std::to_string(ptr->m_data.m_value.array->size()),
14955 ") is out of range"), ptr));
14956 }
14957
14958 // note: at performs range check
14959 ptr = &ptr->at(array_index<BasicJsonType>(reference_token));
14960 break;
14961 }
14962
14963 case detail::value_t::null:
14964 case detail::value_t::string:
14965 case detail::value_t::boolean:
14966 case detail::value_t::number_integer:
14967 case detail::value_t::number_unsigned:
14968 case detail::value_t::number_float:
14969 case detail::value_t::binary:
14970 case detail::value_t::discarded:
14971 default:
14972 JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
14973 }
14974 }
14975
14976 return *ptr;
14977 }
14978
14979 /*!
14980 @brief return a const reference to the pointed to value
14981
14982 @param[in] ptr a JSON value
14983
14984 @return const reference to the JSON value pointed to by the JSON
14985 pointer
14986
14987 @throw parse_error.106 if an array index begins with '0'
14988 @throw parse_error.109 if an array index was not a number
14989 @throw out_of_range.402 if the array index '-' is used
14990 @throw out_of_range.404 if the JSON pointer can not be resolved
14991 */
14992 template<typename BasicJsonType>
14993 const BasicJsonType& get_unchecked(const BasicJsonType* ptr) const
14994 {
14995 for (const auto& reference_token : reference_tokens)
14996 {
14997 switch (ptr->type())
14998 {
14999 case detail::value_t::object:
15000 {
15001 // use unchecked object access
15002 ptr = &ptr->operator[](reference_token);
15003 break;
15004 }
15005
15006 case detail::value_t::array:
15007 {
15008 if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
15009 {
15010 // "-" cannot be used for const access
15011 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));
15012 }
15013
15014 // use unchecked array access
15015 ptr = &ptr->operator[](array_index<BasicJsonType>(reference_token));
15016 break;
15017 }
15018
15019 case detail::value_t::null:
15020 case detail::value_t::string:
15021 case detail::value_t::boolean:
15022 case detail::value_t::number_integer:
15023 case detail::value_t::number_unsigned:
15024 case detail::value_t::number_float:
15025 case detail::value_t::binary:
15026 case detail::value_t::discarded:
15027 default:
15028 JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
15029 }
15030 }
15031
15032 return *ptr;
15033 }
15034
15035 /*!
15036 @throw parse_error.106 if an array index begins with '0'
15037 @throw parse_error.109 if an array index was not a number
15038 @throw out_of_range.402 if the array index '-' is used
15039 @throw out_of_range.404 if the JSON pointer can not be resolved
15040 */
15041 template<typename BasicJsonType>
15042 const BasicJsonType& get_checked(const BasicJsonType* ptr) const
15043 {
15044 for (const auto& reference_token : reference_tokens)
15045 {
15046 switch (ptr->type())
15047 {
15048 case detail::value_t::object:
15049 {
15050 // note: at performs range check
15051 ptr = &ptr->at(reference_token);
15052 break;
15053 }
15054
15055 case detail::value_t::array:
15056 {
15057 if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
15058 {
15059 // "-" always fails the range check
15060 JSON_THROW(detail::out_of_range::create(402, detail::concat(
15061 "array index '-' (", std::to_string(ptr->m_data.m_value.array->size()),
15062 ") is out of range"), ptr));
15063 }
15064
15065 // note: at performs range check
15066 ptr = &ptr->at(array_index<BasicJsonType>(reference_token));
15067 break;
15068 }
15069
15070 case detail::value_t::null:
15071 case detail::value_t::string:
15072 case detail::value_t::boolean:
15073 case detail::value_t::number_integer:
15074 case detail::value_t::number_unsigned:
15075 case detail::value_t::number_float:
15076 case detail::value_t::binary:
15077 case detail::value_t::discarded:
15078 default:
15079 JSON_THROW(detail::out_of_range::create(404, detail::concat("unresolved reference token '", reference_token, "'"), ptr));
15080 }
15081 }
15082
15083 return *ptr;
15084 }
15085
15086 /*!
15087 @throw parse_error.106 if an array index begins with '0'
15088 @throw parse_error.109 if an array index was not a number
15089 */
15090 template<typename BasicJsonType>
15091 bool contains(const BasicJsonType* ptr) const
15092 {
15093 for (const auto& reference_token : reference_tokens)
15094 {
15095 switch (ptr->type())
15096 {
15097 case detail::value_t::object:
15098 {
15099 if (!ptr->contains(reference_token))
15100 {
15101 // we did not find the key in the object
15102 return false;
15103 }
15104
15105 ptr = &ptr->operator[](reference_token);
15106 break;
15107 }
15108
15109 case detail::value_t::array:
15110 {
15111 if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
15112 {
15113 // "-" always fails the range check
15114 return false;
15115 }
15116 if (JSON_HEDLEY_UNLIKELY(reference_token.size() == 1 && !("0" <= reference_token && reference_token <= "9")))
15117 {
15118 // invalid char
15119 return false;
15120 }
15121 if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1))
15122 {
15123 if (JSON_HEDLEY_UNLIKELY(!('1' <= reference_token[0] && reference_token[0] <= '9')))
15124 {
15125 // first char should be between '1' and '9'
15126 return false;
15127 }
15128 for (std::size_t i = 1; i < reference_token.size(); i++)
15129 {
15130 if (JSON_HEDLEY_UNLIKELY(!('0' <= reference_token[i] && reference_token[i] <= '9')))
15131 {
15132 // other char should be between '0' and '9'
15133 return false;
15134 }
15135 }
15136 }
15137
15138 const auto idx = array_index<BasicJsonType>(reference_token);
15139 if (idx >= ptr->size())
15140 {
15141 // index out of range
15142 return false;
15143 }
15144
15145 ptr = &ptr->operator[](idx);
15146 break;
15147 }
15148
15149 case detail::value_t::null:
15150 case detail::value_t::string:
15151 case detail::value_t::boolean:
15152 case detail::value_t::number_integer:
15153 case detail::value_t::number_unsigned:
15154 case detail::value_t::number_float:
15155 case detail::value_t::binary:
15156 case detail::value_t::discarded:
15157 default:
15158 {
15159 // we do not expect primitive values if there is still a
15160 // reference token to process
15161 return false;
15162 }
15163 }
15164 }
15165
15166 // no reference token left means we found a primitive value
15167 return true;
15168 }
15169
15170 /*!
15171 @brief split the string input to reference tokens
15172
15173 @note This function is only called by the json_pointer constructor.
15174 All exceptions below are documented there.
15175
15176 @throw parse_error.107 if the pointer is not empty or begins with '/'
15177 @throw parse_error.108 if character '~' is not followed by '0' or '1'
15178 */
15179 static std::vector<string_t> split(const string_t& reference_string)
15180 {
15181 std::vector<string_t> result;
15182
15183 // special case: empty reference string -> no reference tokens
15184 if (reference_string.empty())
15185 {
15186 return result;
15187 }
15188
15189 // check if nonempty reference string begins with slash
15190 if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/'))
15191 {
15192 JSON_THROW(detail::parse_error::create(107, 1, detail::concat("JSON pointer must be empty or begin with '/' - was: '", reference_string, "'"), nullptr));
15193 }
15194
15195 // extract the reference tokens:
15196 // - slash: position of the last read slash (or end of string)
15197 // - start: position after the previous slash
15198 for (
15199 // search for the first slash after the first character
15200 std::size_t slash = reference_string.find_first_of('/', 1),
15201 // set the beginning of the first reference token
15202 start = 1;
15203 // we can stop if start == 0 (if slash == string_t::npos)
15204 start != 0;
15205 // set the beginning of the next reference token
15206 // (will eventually be 0 if slash == string_t::npos)
15207 start = (slash == string_t::npos) ? 0 : slash + 1,
15208 // find next slash
15209 slash = reference_string.find_first_of('/', start))
15210 {
15211 // use the text between the beginning of the reference token
15212 // (start) and the last slash (slash).
15213 auto reference_token = reference_string.substr(start, slash - start);
15214
15215 // check reference tokens are properly escaped
15216 for (std::size_t pos = reference_token.find_first_of('~');
15217 pos != string_t::npos;
15218 pos = reference_token.find_first_of('~', pos + 1))
15219 {
15220 JSON_ASSERT(reference_token[pos] == '~');
15221
15222 // ~ must be followed by 0 or 1
15223 if (JSON_HEDLEY_UNLIKELY(pos == reference_token.size() - 1 ||
15224 (reference_token[pos + 1] != '0' &&
15225 reference_token[pos + 1] != '1')))
15226 {
15227 JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'", nullptr));
15228 }
15229 }
15230
15231 // finally, store the reference token
15232 detail::unescape(reference_token);
15233 result.push_back(reference_token);
15234 }
15235
15236 return result;
15237 }
15238
15239 private:
15240 /*!
15241 @param[in] reference_string the reference string to the current value
15242 @param[in] value the value to consider
15243 @param[in,out] result the result object to insert values to
15244
15245 @note Empty objects or arrays are flattened to `null`.
15246 */
15247 template<typename BasicJsonType>
15248 static void flatten(const string_t& reference_string,
15249 const BasicJsonType& value,
15250 BasicJsonType& result)
15251 {
15252 switch (value.type())
15253 {
15254 case detail::value_t::array:
15255 {
15256 if (value.m_data.m_value.array->empty())
15257 {
15258 // flatten empty array as null
15259 result[reference_string] = nullptr;
15260 }
15261 else
15262 {
15263 // iterate array and use index as reference string
15264 for (std::size_t i = 0; i < value.m_data.m_value.array->size(); ++i)
15265 {
15266 flatten(detail::concat<string_t>(reference_string, '/', std::to_string(i)),
15267 value.m_data.m_value.array->operator[](i), result);
15268 }
15269 }
15270 break;
15271 }
15272
15273 case detail::value_t::object:
15274 {
15275 if (value.m_data.m_value.object->empty())
15276 {
15277 // flatten empty object as null
15278 result[reference_string] = nullptr;
15279 }
15280 else
15281 {
15282 // iterate object and use keys as reference string
15283 for (const auto& element : *value.m_data.m_value.object)
15284 {
15285 flatten(detail::concat<string_t>(reference_string, '/', detail::escape(element.first)), element.second, result);
15286 }
15287 }
15288 break;
15289 }
15290
15291 case detail::value_t::null:
15292 case detail::value_t::string:
15293 case detail::value_t::boolean:
15294 case detail::value_t::number_integer:
15295 case detail::value_t::number_unsigned:
15296 case detail::value_t::number_float:
15297 case detail::value_t::binary:
15298 case detail::value_t::discarded:
15299 default:
15300 {
15301 // add primitive value with its reference string
15302 result[reference_string] = value;
15303 break;
15304 }
15305 }
15306 }
15307
15308 /*!
15309 @param[in] value flattened JSON
15310
15311 @return unflattened JSON
15312
15313 @throw parse_error.109 if array index is not a number
15314 @throw type_error.314 if value is not an object
15315 @throw type_error.315 if object values are not primitive
15316 @throw type_error.313 if value cannot be unflattened
15317 */
15318 template<typename BasicJsonType>
15319 static BasicJsonType
15320 unflatten(const BasicJsonType& value)
15321 {
15322 if (JSON_HEDLEY_UNLIKELY(!value.is_object()))
15323 {
15324 JSON_THROW(detail::type_error::create(314, "only objects can be unflattened", &value));
15325 }
15326
15327 BasicJsonType result;
15328
15329 // iterate the JSON object values
15330 for (const auto& element : *value.m_data.m_value.object)
15331 {
15332 if (JSON_HEDLEY_UNLIKELY(!element.second.is_primitive()))
15333 {
15334 JSON_THROW(detail::type_error::create(315, "values in object must be primitive", &element.second));
15335 }
15336
15337 // assign value to reference pointed to by JSON pointer; Note that if
15338 // the JSON pointer is "" (i.e., points to the whole value), function
15339 // get_and_create returns a reference to result itself. An assignment
15340 // will then create a primitive value.
15341 json_pointer(element.first).get_and_create(result) = element.second;
15342 }
15343
15344 return result;
15345 }
15346
15347 // can't use conversion operator because of ambiguity
15348 json_pointer<string_t> convert() const&
15349 {
15350 json_pointer<string_t> result;
15351 result.reference_tokens = reference_tokens;
15352 return result;
15353 }
15354
15355 json_pointer<string_t> convert()&&
15356 {
15357 json_pointer<string_t> result;
15358 result.reference_tokens = std::move(reference_tokens);
15359 return result;
15360 }
15361
15362 public:
15363 #if JSON_HAS_THREE_WAY_COMPARISON
15364 /// @brief compares two JSON pointers for equality
15365 /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
15366 template<typename RefStringTypeRhs>
15367 bool operator==(const json_pointer<RefStringTypeRhs>& rhs) const noexcept
15368 {
15369 return reference_tokens == rhs.reference_tokens;
15370 }
15371
15372 /// @brief compares JSON pointer and string for equality
15373 /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
15374 JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer))
15375 bool operator==(const string_t& rhs) const
15376 {
15377 return *this == json_pointer(rhs);
15378 }
15379
15380 /// @brief 3-way compares two JSON pointers
15381 template<typename RefStringTypeRhs>
15382 std::strong_ordering operator<=>(const json_pointer<RefStringTypeRhs>& rhs) const noexcept // *NOPAD*
15383 {
15384 return reference_tokens <=> rhs.reference_tokens; // *NOPAD*
15385 }
15386 #else
15387 /// @brief compares two JSON pointers for equality
15388 /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
15389 template<typename RefStringTypeLhs, typename RefStringTypeRhs>
15390 // NOLINTNEXTLINE(readability-redundant-declaration)
15391 friend bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
15392 const json_pointer<RefStringTypeRhs>& rhs) noexcept;
15393
15394 /// @brief compares JSON pointer and string for equality
15395 /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
15396 template<typename RefStringTypeLhs, typename StringType>
15397 // NOLINTNEXTLINE(readability-redundant-declaration)
15398 friend bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
15399 const StringType& rhs);
15400
15401 /// @brief compares string and JSON pointer for equality
15402 /// @sa https://json.nlohmann.me/api/json_pointer/operator_eq/
15403 template<typename RefStringTypeRhs, typename StringType>
15404 // NOLINTNEXTLINE(readability-redundant-declaration)
15405 friend bool operator==(const StringType& lhs,
15406 const json_pointer<RefStringTypeRhs>& rhs);
15407
15408 /// @brief compares two JSON pointers for inequality
15409 /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
15410 template<typename RefStringTypeLhs, typename RefStringTypeRhs>
15411 // NOLINTNEXTLINE(readability-redundant-declaration)
15412 friend bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
15413 const json_pointer<RefStringTypeRhs>& rhs) noexcept;
15414
15415 /// @brief compares JSON pointer and string for inequality
15416 /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
15417 template<typename RefStringTypeLhs, typename StringType>
15418 // NOLINTNEXTLINE(readability-redundant-declaration)
15419 friend bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
15420 const StringType& rhs);
15421
15422 /// @brief compares string and JSON pointer for inequality
15423 /// @sa https://json.nlohmann.me/api/json_pointer/operator_ne/
15424 template<typename RefStringTypeRhs, typename StringType>
15425 // NOLINTNEXTLINE(readability-redundant-declaration)
15426 friend bool operator!=(const StringType& lhs,
15427 const json_pointer<RefStringTypeRhs>& rhs);
15428
15429 /// @brief compares two JSON pointer for less-than
15430 template<typename RefStringTypeLhs, typename RefStringTypeRhs>
15431 // NOLINTNEXTLINE(readability-redundant-declaration)
15432 friend bool operator<(const json_pointer<RefStringTypeLhs>& lhs,
15433 const json_pointer<RefStringTypeRhs>& rhs) noexcept;
15434 #endif
15435
15436 private:
15437 /// the reference tokens
15438 std::vector<string_t> reference_tokens;
15439 };
15440
15441 #if !JSON_HAS_THREE_WAY_COMPARISON
15442 // functions cannot be defined inside class due to ODR violations
15443 template<typename RefStringTypeLhs, typename RefStringTypeRhs>
15444 inline bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
15445 const json_pointer<RefStringTypeRhs>& rhs) noexcept
15446 {
15447 return lhs.reference_tokens == rhs.reference_tokens;
15448 }
15449
15450 template<typename RefStringTypeLhs,
15451 typename StringType = typename json_pointer<RefStringTypeLhs>::string_t>
15452 JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer, json_pointer))
15453 inline bool operator==(const json_pointer<RefStringTypeLhs>& lhs,
15454 const StringType& rhs)
15455 {
15456 return lhs == json_pointer<RefStringTypeLhs>(rhs);
15457 }
15458
15459 template<typename RefStringTypeRhs,
15460 typename StringType = typename json_pointer<RefStringTypeRhs>::string_t>
15461 JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator==(json_pointer, json_pointer))
15462 inline bool operator==(const StringType& lhs,
15463 const json_pointer<RefStringTypeRhs>& rhs)
15464 {
15465 return json_pointer<RefStringTypeRhs>(lhs) == rhs;
15466 }
15467
15468 template<typename RefStringTypeLhs, typename RefStringTypeRhs>
15469 inline bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
15470 const json_pointer<RefStringTypeRhs>& rhs) noexcept
15471 {
15472 return !(lhs == rhs);
15473 }
15474
15475 template<typename RefStringTypeLhs,
15476 typename StringType = typename json_pointer<RefStringTypeLhs>::string_t>
15477 JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator!=(json_pointer, json_pointer))
15478 inline bool operator!=(const json_pointer<RefStringTypeLhs>& lhs,
15479 const StringType& rhs)
15480 {
15481 return !(lhs == rhs);
15482 }
15483
15484 template<typename RefStringTypeRhs,
15485 typename StringType = typename json_pointer<RefStringTypeRhs>::string_t>
15486 JSON_HEDLEY_DEPRECATED_FOR(3.11.2, operator!=(json_pointer, json_pointer))
15487 inline bool operator!=(const StringType& lhs,
15488 const json_pointer<RefStringTypeRhs>& rhs)
15489 {
15490 return !(lhs == rhs);
15491 }
15492
15493 template<typename RefStringTypeLhs, typename RefStringTypeRhs>
15494 inline bool operator<(const json_pointer<RefStringTypeLhs>& lhs,
15495 const json_pointer<RefStringTypeRhs>& rhs) noexcept
15496 {
15497 return lhs.reference_tokens < rhs.reference_tokens;
15498 }
15499 #endif
15500
15501 NLOHMANN_JSON_NAMESPACE_END
15502
15503 // #include <nlohmann/detail/json_ref.hpp>
15504 // __ _____ _____ _____
15505 // __| | __| | | | JSON for Modern C++
15506 // | | |__ | | | | | | version 3.12.0
15507 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
15508 //
15509 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
15510 // SPDX-License-Identifier: MIT
15511
15512
15513
15514 #include <initializer_list>
15515 #include <utility>
15516
15517 // #include <nlohmann/detail/abi_macros.hpp>
15518
15519 // #include <nlohmann/detail/meta/type_traits.hpp>
15520
15521
15522 NLOHMANN_JSON_NAMESPACE_BEGIN
15523 namespace detail
15524 {
15525
15526 template<typename BasicJsonType>
15527 class json_ref
15528 {
15529 public:
15530 using value_type = BasicJsonType;
15531
15532 json_ref(value_type&& value)
15533 : owned_value(std::move(value))
15534 {}
15535
15536 json_ref(const value_type& value)
15537 : value_ref(&value)
15538 {}
15539
15540 json_ref(std::initializer_list<json_ref> init)
15541 : owned_value(init)
15542 {}
15543
15544 template <
15545 class... Args,
15546 enable_if_t<std::is_constructible<value_type, Args...>::value, int> = 0 >
15547 json_ref(Args && ... args)
15548 : owned_value(std::forward<Args>(args)...)
15549 {}
15550
15551 // class should be movable only
15552 json_ref(json_ref&&) noexcept = default;
15553 json_ref(const json_ref&) = delete;
15554 json_ref& operator=(const json_ref&) = delete;
15555 json_ref& operator=(json_ref&&) = delete;
15556 ~json_ref() = default;
15557
15558 value_type moved_or_copied() const
15559 {
15560 if (value_ref == nullptr)
15561 {
15562 return std::move(owned_value);
15563 }
15564 return *value_ref;
15565 }
15566
15567 value_type const& operator*() const
15568 {
15569 return value_ref ? *value_ref : owned_value;
15570 }
15571
15572 value_type const* operator->() const
15573 {
15574 return &** this;
15575 }
15576
15577 private:
15578 mutable value_type owned_value = nullptr;
15579 value_type const* value_ref = nullptr;
15580 };
15581
15582 } // namespace detail
15583 NLOHMANN_JSON_NAMESPACE_END
15584
15585 // #include <nlohmann/detail/macro_scope.hpp>
15586
15587 // #include <nlohmann/detail/string_concat.hpp>
15588
15589 // #include <nlohmann/detail/string_escape.hpp>
15590
15591 // #include <nlohmann/detail/string_utils.hpp>
15592
15593 // #include <nlohmann/detail/meta/cpp_future.hpp>
15594
15595 // #include <nlohmann/detail/meta/type_traits.hpp>
15596
15597 // #include <nlohmann/detail/output/binary_writer.hpp>
15598 // __ _____ _____ _____
15599 // __| | __| | | | JSON for Modern C++
15600 // | | |__ | | | | | | version 3.12.0
15601 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
15602 //
15603 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
15604 // SPDX-License-Identifier: MIT
15605
15606
15607
15608 #include <algorithm> // reverse
15609 #include <array> // array
15610 #include <map> // map
15611 #include <cmath> // isnan, isinf
15612 #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
15613 #include <cstring> // memcpy
15614 #include <limits> // numeric_limits
15615 #include <string> // string
15616 #include <utility> // move
15617 #include <vector> // vector
15618
15619 // #include <nlohmann/detail/input/binary_reader.hpp>
15620
15621 // #include <nlohmann/detail/macro_scope.hpp>
15622
15623 // #include <nlohmann/detail/output/output_adapters.hpp>
15624 // __ _____ _____ _____
15625 // __| | __| | | | JSON for Modern C++
15626 // | | |__ | | | | | | version 3.12.0
15627 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
15628 //
15629 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
15630 // SPDX-License-Identifier: MIT
15631
15632
15633
15634 #include <algorithm> // copy
15635 #include <cstddef> // size_t
15636 #include <iterator> // back_inserter
15637 #include <memory> // shared_ptr, make_shared
15638 #include <string> // basic_string
15639 #include <vector> // vector
15640
15641 #ifndef JSON_NO_IO
15642 #include <ios> // streamsize
15643 #include <ostream> // basic_ostream
15644 #endif // JSON_NO_IO
15645
15646 // #include <nlohmann/detail/macro_scope.hpp>
15647
15648
15649 NLOHMANN_JSON_NAMESPACE_BEGIN
15650 namespace detail
15651 {
15652
15653 /// abstract output adapter interface
15654 template<typename CharType> struct output_adapter_protocol
15655 {
15656 virtual void write_character(CharType c) = 0;
15657 virtual void write_characters(const CharType* s, std::size_t length) = 0;
15658 virtual ~output_adapter_protocol() = default;
15659
15660 output_adapter_protocol() = default;
15661 output_adapter_protocol(const output_adapter_protocol&) = default;
15662 output_adapter_protocol(output_adapter_protocol&&) noexcept = default;
15663 output_adapter_protocol& operator=(const output_adapter_protocol&) = default;
15664 output_adapter_protocol& operator=(output_adapter_protocol&&) noexcept = default;
15665 };
15666
15667 /// a type to simplify interfaces
15668 template<typename CharType>
15669 using output_adapter_t = std::shared_ptr<output_adapter_protocol<CharType>>;
15670
15671 /// output adapter for byte vectors
15672 template<typename CharType, typename AllocatorType = std::allocator<CharType>>
15673 class output_vector_adapter : public output_adapter_protocol<CharType>
15674 {
15675 public:
15676 explicit output_vector_adapter(std::vector<CharType, AllocatorType>& vec) noexcept
15677 : v(vec)
15678 {}
15679
15680 void write_character(CharType c) override
15681 {
15682 v.push_back(c);
15683 }
15684
15685 JSON_HEDLEY_NON_NULL(2)
15686 void write_characters(const CharType* s, std::size_t length) override
15687 {
15688 v.insert(v.end(), s, s + length);
15689 }
15690
15691 private:
15692 std::vector<CharType, AllocatorType>& v;
15693 };
15694
15695 #ifndef JSON_NO_IO
15696 /// output adapter for output streams
15697 template<typename CharType>
15698 class output_stream_adapter : public output_adapter_protocol<CharType>
15699 {
15700 public:
15701 explicit output_stream_adapter(std::basic_ostream<CharType>& s) noexcept
15702 : stream(s)
15703 {}
15704
15705 void write_character(CharType c) override
15706 {
15707 stream.put(c);
15708 }
15709
15710 JSON_HEDLEY_NON_NULL(2)
15711 void write_characters(const CharType* s, std::size_t length) override
15712 {
15713 stream.write(s, static_cast<std::streamsize>(length));
15714 }
15715
15716 private:
15717 std::basic_ostream<CharType>& stream;
15718 };
15719 #endif // JSON_NO_IO
15720
15721 /// output adapter for basic_string
15722 template<typename CharType, typename StringType = std::basic_string<CharType>>
15723 class output_string_adapter : public output_adapter_protocol<CharType>
15724 {
15725 public:
15726 explicit output_string_adapter(StringType& s) noexcept
15727 : str(s)
15728 {}
15729
15730 void write_character(CharType c) override
15731 {
15732 str.push_back(c);
15733 }
15734
15735 JSON_HEDLEY_NON_NULL(2)
15736 void write_characters(const CharType* s, std::size_t length) override
15737 {
15738 str.append(s, length);
15739 }
15740
15741 private:
15742 StringType& str;
15743 };
15744
15745 template<typename CharType, typename StringType = std::basic_string<CharType>>
15746 class output_adapter
15747 {
15748 public:
15749 template<typename AllocatorType = std::allocator<CharType>>
15750 output_adapter(std::vector<CharType, AllocatorType>& vec)
15751 : oa(std::make_shared<output_vector_adapter<CharType, AllocatorType>>(vec)) {}
15752
15753 #ifndef JSON_NO_IO
15754 output_adapter(std::basic_ostream<CharType>& s)
15755 : oa(std::make_shared<output_stream_adapter<CharType>>(s)) {}
15756 #endif // JSON_NO_IO
15757
15758 output_adapter(StringType& s)
15759 : oa(std::make_shared<output_string_adapter<CharType, StringType>>(s)) {}
15760
15761 operator output_adapter_t<CharType>()
15762 {
15763 return oa;
15764 }
15765
15766 private:
15767 output_adapter_t<CharType> oa = nullptr;
15768 };
15769
15770 } // namespace detail
15771 NLOHMANN_JSON_NAMESPACE_END
15772
15773 // #include <nlohmann/detail/string_concat.hpp>
15774
15775
15776 NLOHMANN_JSON_NAMESPACE_BEGIN
15777 namespace detail
15778 {
15779
15780 /// how to encode BJData
15781 enum class bjdata_version_t
15782 {
15783 draft2,
15784 draft3,
15785 };
15786
15787 ///////////////////
15788 // binary writer //
15789 ///////////////////
15790
15791 /*!
15792 @brief serialization to CBOR and MessagePack values
15793 */
15794 template<typename BasicJsonType, typename CharType>
15795 class binary_writer
15796 {
15797 using string_t = typename BasicJsonType::string_t;
15798 using binary_t = typename BasicJsonType::binary_t;
15799 using number_float_t = typename BasicJsonType::number_float_t;
15800
15801 public:
15802 /*!
15803 @brief create a binary writer
15804
15805 @param[in] adapter output adapter to write to
15806 */
15807 explicit binary_writer(output_adapter_t<CharType> adapter) : oa(std::move(adapter))
15808 {
15809 JSON_ASSERT(oa);
15810 }
15811
15812 /*!
15813 @param[in] j JSON value to serialize
15814 @pre j.type() == value_t::object
15815 */
15816 void write_bson(const BasicJsonType& j)
15817 {
15818 switch (j.type())
15819 {
15820 case value_t::object:
15821 {
15822 write_bson_object(*j.m_data.m_value.object);
15823 break;
15824 }
15825
15826 case value_t::null:
15827 case value_t::array:
15828 case value_t::string:
15829 case value_t::boolean:
15830 case value_t::number_integer:
15831 case value_t::number_unsigned:
15832 case value_t::number_float:
15833 case value_t::binary:
15834 case value_t::discarded:
15835 default:
15836 {
15837 JSON_THROW(type_error::create(317, concat("to serialize to BSON, top-level type must be object, but is ", j.type_name()), &j));
15838 }
15839 }
15840 }
15841
15842 /*!
15843 @param[in] j JSON value to serialize
15844 */
15845 void write_cbor(const BasicJsonType& j)
15846 {
15847 switch (j.type())
15848 {
15849 case value_t::null:
15850 {
15851 oa->write_character(to_char_type(0xF6));
15852 break;
15853 }
15854
15855 case value_t::boolean:
15856 {
15857 oa->write_character(j.m_data.m_value.boolean
15858 ? to_char_type(0xF5)
15859 : to_char_type(0xF4));
15860 break;
15861 }
15862
15863 case value_t::number_integer:
15864 {
15865 if (j.m_data.m_value.number_integer >= 0)
15866 {
15867 // CBOR does not differentiate between positive signed
15868 // integers and unsigned integers. Therefore, we used the
15869 // code from the value_t::number_unsigned case here.
15870 if (j.m_data.m_value.number_integer <= 0x17)
15871 {
15872 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
15873 }
15874 else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint8_t>::max)())
15875 {
15876 oa->write_character(to_char_type(0x18));
15877 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
15878 }
15879 else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint16_t>::max)())
15880 {
15881 oa->write_character(to_char_type(0x19));
15882 write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_integer));
15883 }
15884 else if (j.m_data.m_value.number_integer <= (std::numeric_limits<std::uint32_t>::max)())
15885 {
15886 oa->write_character(to_char_type(0x1A));
15887 write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_integer));
15888 }
15889 else
15890 {
15891 oa->write_character(to_char_type(0x1B));
15892 write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_integer));
15893 }
15894 }
15895 else
15896 {
15897 // The conversions below encode the sign in the first
15898 // byte, and the value is converted to a positive number.
15899 const auto positive_number = -1 - j.m_data.m_value.number_integer;
15900 if (j.m_data.m_value.number_integer >= -24)
15901 {
15902 write_number(static_cast<std::uint8_t>(0x20 + positive_number));
15903 }
15904 else if (positive_number <= (std::numeric_limits<std::uint8_t>::max)())
15905 {
15906 oa->write_character(to_char_type(0x38));
15907 write_number(static_cast<std::uint8_t>(positive_number));
15908 }
15909 else if (positive_number <= (std::numeric_limits<std::uint16_t>::max)())
15910 {
15911 oa->write_character(to_char_type(0x39));
15912 write_number(static_cast<std::uint16_t>(positive_number));
15913 }
15914 else if (positive_number <= (std::numeric_limits<std::uint32_t>::max)())
15915 {
15916 oa->write_character(to_char_type(0x3A));
15917 write_number(static_cast<std::uint32_t>(positive_number));
15918 }
15919 else
15920 {
15921 oa->write_character(to_char_type(0x3B));
15922 write_number(static_cast<std::uint64_t>(positive_number));
15923 }
15924 }
15925 break;
15926 }
15927
15928 case value_t::number_unsigned:
15929 {
15930 if (j.m_data.m_value.number_unsigned <= 0x17)
15931 {
15932 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_unsigned));
15933 }
15934 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
15935 {
15936 oa->write_character(to_char_type(0x18));
15937 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_unsigned));
15938 }
15939 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
15940 {
15941 oa->write_character(to_char_type(0x19));
15942 write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_unsigned));
15943 }
15944 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
15945 {
15946 oa->write_character(to_char_type(0x1A));
15947 write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_unsigned));
15948 }
15949 else
15950 {
15951 oa->write_character(to_char_type(0x1B));
15952 write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_unsigned));
15953 }
15954 break;
15955 }
15956
15957 case value_t::number_float:
15958 {
15959 if (std::isnan(j.m_data.m_value.number_float))
15960 {
15961 // NaN is 0xf97e00 in CBOR
15962 oa->write_character(to_char_type(0xF9));
15963 oa->write_character(to_char_type(0x7E));
15964 oa->write_character(to_char_type(0x00));
15965 }
15966 else if (std::isinf(j.m_data.m_value.number_float))
15967 {
15968 // Infinity is 0xf97c00, -Infinity is 0xf9fc00
15969 oa->write_character(to_char_type(0xf9));
15970 oa->write_character(j.m_data.m_value.number_float > 0 ? to_char_type(0x7C) : to_char_type(0xFC));
15971 oa->write_character(to_char_type(0x00));
15972 }
15973 else
15974 {
15975 write_compact_float(j.m_data.m_value.number_float, detail::input_format_t::cbor);
15976 }
15977 break;
15978 }
15979
15980 case value_t::string:
15981 {
15982 // step 1: write control byte and the string length
15983 const auto N = j.m_data.m_value.string->size();
15984 if (N <= 0x17)
15985 {
15986 write_number(static_cast<std::uint8_t>(0x60 + N));
15987 }
15988 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
15989 {
15990 oa->write_character(to_char_type(0x78));
15991 write_number(static_cast<std::uint8_t>(N));
15992 }
15993 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
15994 {
15995 oa->write_character(to_char_type(0x79));
15996 write_number(static_cast<std::uint16_t>(N));
15997 }
15998 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
15999 {
16000 oa->write_character(to_char_type(0x7A));
16001 write_number(static_cast<std::uint32_t>(N));
16002 }
16003 // LCOV_EXCL_START
16004 else if (N <= (std::numeric_limits<std::uint64_t>::max)())
16005 {
16006 oa->write_character(to_char_type(0x7B));
16007 write_number(static_cast<std::uint64_t>(N));
16008 }
16009 // LCOV_EXCL_STOP
16010
16011 // step 2: write the string
16012 oa->write_characters(
16013 reinterpret_cast<const CharType*>(j.m_data.m_value.string->c_str()),
16014 j.m_data.m_value.string->size());
16015 break;
16016 }
16017
16018 case value_t::array:
16019 {
16020 // step 1: write control byte and the array size
16021 const auto N = j.m_data.m_value.array->size();
16022 if (N <= 0x17)
16023 {
16024 write_number(static_cast<std::uint8_t>(0x80 + N));
16025 }
16026 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
16027 {
16028 oa->write_character(to_char_type(0x98));
16029 write_number(static_cast<std::uint8_t>(N));
16030 }
16031 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
16032 {
16033 oa->write_character(to_char_type(0x99));
16034 write_number(static_cast<std::uint16_t>(N));
16035 }
16036 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
16037 {
16038 oa->write_character(to_char_type(0x9A));
16039 write_number(static_cast<std::uint32_t>(N));
16040 }
16041 // LCOV_EXCL_START
16042 else if (N <= (std::numeric_limits<std::uint64_t>::max)())
16043 {
16044 oa->write_character(to_char_type(0x9B));
16045 write_number(static_cast<std::uint64_t>(N));
16046 }
16047 // LCOV_EXCL_STOP
16048
16049 // step 2: write each element
16050 for (const auto& el : *j.m_data.m_value.array)
16051 {
16052 write_cbor(el);
16053 }
16054 break;
16055 }
16056
16057 case value_t::binary:
16058 {
16059 if (j.m_data.m_value.binary->has_subtype())
16060 {
16061 if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint8_t>::max)())
16062 {
16063 write_number(static_cast<std::uint8_t>(0xd8));
16064 write_number(static_cast<std::uint8_t>(j.m_data.m_value.binary->subtype()));
16065 }
16066 else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint16_t>::max)())
16067 {
16068 write_number(static_cast<std::uint8_t>(0xd9));
16069 write_number(static_cast<std::uint16_t>(j.m_data.m_value.binary->subtype()));
16070 }
16071 else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint32_t>::max)())
16072 {
16073 write_number(static_cast<std::uint8_t>(0xda));
16074 write_number(static_cast<std::uint32_t>(j.m_data.m_value.binary->subtype()));
16075 }
16076 else if (j.m_data.m_value.binary->subtype() <= (std::numeric_limits<std::uint64_t>::max)())
16077 {
16078 write_number(static_cast<std::uint8_t>(0xdb));
16079 write_number(static_cast<std::uint64_t>(j.m_data.m_value.binary->subtype()));
16080 }
16081 }
16082
16083 // step 1: write control byte and the binary array size
16084 const auto N = j.m_data.m_value.binary->size();
16085 if (N <= 0x17)
16086 {
16087 write_number(static_cast<std::uint8_t>(0x40 + N));
16088 }
16089 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
16090 {
16091 oa->write_character(to_char_type(0x58));
16092 write_number(static_cast<std::uint8_t>(N));
16093 }
16094 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
16095 {
16096 oa->write_character(to_char_type(0x59));
16097 write_number(static_cast<std::uint16_t>(N));
16098 }
16099 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
16100 {
16101 oa->write_character(to_char_type(0x5A));
16102 write_number(static_cast<std::uint32_t>(N));
16103 }
16104 // LCOV_EXCL_START
16105 else if (N <= (std::numeric_limits<std::uint64_t>::max)())
16106 {
16107 oa->write_character(to_char_type(0x5B));
16108 write_number(static_cast<std::uint64_t>(N));
16109 }
16110 // LCOV_EXCL_STOP
16111
16112 // step 2: write each element
16113 oa->write_characters(
16114 reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
16115 N);
16116
16117 break;
16118 }
16119
16120 case value_t::object:
16121 {
16122 // step 1: write control byte and the object size
16123 const auto N = j.m_data.m_value.object->size();
16124 if (N <= 0x17)
16125 {
16126 write_number(static_cast<std::uint8_t>(0xA0 + N));
16127 }
16128 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
16129 {
16130 oa->write_character(to_char_type(0xB8));
16131 write_number(static_cast<std::uint8_t>(N));
16132 }
16133 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
16134 {
16135 oa->write_character(to_char_type(0xB9));
16136 write_number(static_cast<std::uint16_t>(N));
16137 }
16138 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
16139 {
16140 oa->write_character(to_char_type(0xBA));
16141 write_number(static_cast<std::uint32_t>(N));
16142 }
16143 // LCOV_EXCL_START
16144 else if (N <= (std::numeric_limits<std::uint64_t>::max)())
16145 {
16146 oa->write_character(to_char_type(0xBB));
16147 write_number(static_cast<std::uint64_t>(N));
16148 }
16149 // LCOV_EXCL_STOP
16150
16151 // step 2: write each element
16152 for (const auto& el : *j.m_data.m_value.object)
16153 {
16154 write_cbor(el.first);
16155 write_cbor(el.second);
16156 }
16157 break;
16158 }
16159
16160 case value_t::discarded:
16161 default:
16162 break;
16163 }
16164 }
16165
16166 /*!
16167 @param[in] j JSON value to serialize
16168 */
16169 void write_msgpack(const BasicJsonType& j)
16170 {
16171 switch (j.type())
16172 {
16173 case value_t::null: // nil
16174 {
16175 oa->write_character(to_char_type(0xC0));
16176 break;
16177 }
16178
16179 case value_t::boolean: // true and false
16180 {
16181 oa->write_character(j.m_data.m_value.boolean
16182 ? to_char_type(0xC3)
16183 : to_char_type(0xC2));
16184 break;
16185 }
16186
16187 case value_t::number_integer:
16188 {
16189 if (j.m_data.m_value.number_integer >= 0)
16190 {
16191 // MessagePack does not differentiate between positive
16192 // signed integers and unsigned integers. Therefore, we used
16193 // the code from the value_t::number_unsigned case here.
16194 if (j.m_data.m_value.number_unsigned < 128)
16195 {
16196 // positive fixnum
16197 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
16198 }
16199 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
16200 {
16201 // uint 8
16202 oa->write_character(to_char_type(0xCC));
16203 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
16204 }
16205 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
16206 {
16207 // uint 16
16208 oa->write_character(to_char_type(0xCD));
16209 write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_integer));
16210 }
16211 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
16212 {
16213 // uint 32
16214 oa->write_character(to_char_type(0xCE));
16215 write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_integer));
16216 }
16217 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
16218 {
16219 // uint 64
16220 oa->write_character(to_char_type(0xCF));
16221 write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_integer));
16222 }
16223 }
16224 else
16225 {
16226 if (j.m_data.m_value.number_integer >= -32)
16227 {
16228 // negative fixnum
16229 write_number(static_cast<std::int8_t>(j.m_data.m_value.number_integer));
16230 }
16231 else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int8_t>::min)() &&
16232 j.m_data.m_value.number_integer <= (std::numeric_limits<std::int8_t>::max)())
16233 {
16234 // int 8
16235 oa->write_character(to_char_type(0xD0));
16236 write_number(static_cast<std::int8_t>(j.m_data.m_value.number_integer));
16237 }
16238 else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int16_t>::min)() &&
16239 j.m_data.m_value.number_integer <= (std::numeric_limits<std::int16_t>::max)())
16240 {
16241 // int 16
16242 oa->write_character(to_char_type(0xD1));
16243 write_number(static_cast<std::int16_t>(j.m_data.m_value.number_integer));
16244 }
16245 else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int32_t>::min)() &&
16246 j.m_data.m_value.number_integer <= (std::numeric_limits<std::int32_t>::max)())
16247 {
16248 // int 32
16249 oa->write_character(to_char_type(0xD2));
16250 write_number(static_cast<std::int32_t>(j.m_data.m_value.number_integer));
16251 }
16252 else if (j.m_data.m_value.number_integer >= (std::numeric_limits<std::int64_t>::min)() &&
16253 j.m_data.m_value.number_integer <= (std::numeric_limits<std::int64_t>::max)())
16254 {
16255 // int 64
16256 oa->write_character(to_char_type(0xD3));
16257 write_number(static_cast<std::int64_t>(j.m_data.m_value.number_integer));
16258 }
16259 }
16260 break;
16261 }
16262
16263 case value_t::number_unsigned:
16264 {
16265 if (j.m_data.m_value.number_unsigned < 128)
16266 {
16267 // positive fixnum
16268 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
16269 }
16270 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
16271 {
16272 // uint 8
16273 oa->write_character(to_char_type(0xCC));
16274 write_number(static_cast<std::uint8_t>(j.m_data.m_value.number_integer));
16275 }
16276 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
16277 {
16278 // uint 16
16279 oa->write_character(to_char_type(0xCD));
16280 write_number(static_cast<std::uint16_t>(j.m_data.m_value.number_integer));
16281 }
16282 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
16283 {
16284 // uint 32
16285 oa->write_character(to_char_type(0xCE));
16286 write_number(static_cast<std::uint32_t>(j.m_data.m_value.number_integer));
16287 }
16288 else if (j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
16289 {
16290 // uint 64
16291 oa->write_character(to_char_type(0xCF));
16292 write_number(static_cast<std::uint64_t>(j.m_data.m_value.number_integer));
16293 }
16294 break;
16295 }
16296
16297 case value_t::number_float:
16298 {
16299 write_compact_float(j.m_data.m_value.number_float, detail::input_format_t::msgpack);
16300 break;
16301 }
16302
16303 case value_t::string:
16304 {
16305 // step 1: write control byte and the string length
16306 const auto N = j.m_data.m_value.string->size();
16307 if (N <= 31)
16308 {
16309 // fixstr
16310 write_number(static_cast<std::uint8_t>(0xA0 | N));
16311 }
16312 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
16313 {
16314 // str 8
16315 oa->write_character(to_char_type(0xD9));
16316 write_number(static_cast<std::uint8_t>(N));
16317 }
16318 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
16319 {
16320 // str 16
16321 oa->write_character(to_char_type(0xDA));
16322 write_number(static_cast<std::uint16_t>(N));
16323 }
16324 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
16325 {
16326 // str 32
16327 oa->write_character(to_char_type(0xDB));
16328 write_number(static_cast<std::uint32_t>(N));
16329 }
16330
16331 // step 2: write the string
16332 oa->write_characters(
16333 reinterpret_cast<const CharType*>(j.m_data.m_value.string->c_str()),
16334 j.m_data.m_value.string->size());
16335 break;
16336 }
16337
16338 case value_t::array:
16339 {
16340 // step 1: write control byte and the array size
16341 const auto N = j.m_data.m_value.array->size();
16342 if (N <= 15)
16343 {
16344 // fixarray
16345 write_number(static_cast<std::uint8_t>(0x90 | N));
16346 }
16347 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
16348 {
16349 // array 16
16350 oa->write_character(to_char_type(0xDC));
16351 write_number(static_cast<std::uint16_t>(N));
16352 }
16353 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
16354 {
16355 // array 32
16356 oa->write_character(to_char_type(0xDD));
16357 write_number(static_cast<std::uint32_t>(N));
16358 }
16359
16360 // step 2: write each element
16361 for (const auto& el : *j.m_data.m_value.array)
16362 {
16363 write_msgpack(el);
16364 }
16365 break;
16366 }
16367
16368 case value_t::binary:
16369 {
16370 // step 0: determine if the binary type has a set subtype to
16371 // determine whether to use the ext or fixext types
16372 const bool use_ext = j.m_data.m_value.binary->has_subtype();
16373
16374 // step 1: write control byte and the byte string length
16375 const auto N = j.m_data.m_value.binary->size();
16376 if (N <= (std::numeric_limits<std::uint8_t>::max)())
16377 {
16378 std::uint8_t output_type{};
16379 bool fixed = true;
16380 if (use_ext)
16381 {
16382 switch (N)
16383 {
16384 case 1:
16385 output_type = 0xD4; // fixext 1
16386 break;
16387 case 2:
16388 output_type = 0xD5; // fixext 2
16389 break;
16390 case 4:
16391 output_type = 0xD6; // fixext 4
16392 break;
16393 case 8:
16394 output_type = 0xD7; // fixext 8
16395 break;
16396 case 16:
16397 output_type = 0xD8; // fixext 16
16398 break;
16399 default:
16400 output_type = 0xC7; // ext 8
16401 fixed = false;
16402 break;
16403 }
16404
16405 }
16406 else
16407 {
16408 output_type = 0xC4; // bin 8
16409 fixed = false;
16410 }
16411
16412 oa->write_character(to_char_type(output_type));
16413 if (!fixed)
16414 {
16415 write_number(static_cast<std::uint8_t>(N));
16416 }
16417 }
16418 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
16419 {
16420 const std::uint8_t output_type = use_ext
16421 ? 0xC8 // ext 16
16422 : 0xC5; // bin 16
16423
16424 oa->write_character(to_char_type(output_type));
16425 write_number(static_cast<std::uint16_t>(N));
16426 }
16427 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
16428 {
16429 const std::uint8_t output_type = use_ext
16430 ? 0xC9 // ext 32
16431 : 0xC6; // bin 32
16432
16433 oa->write_character(to_char_type(output_type));
16434 write_number(static_cast<std::uint32_t>(N));
16435 }
16436
16437 // step 1.5: if this is an ext type, write the subtype
16438 if (use_ext)
16439 {
16440 write_number(static_cast<std::int8_t>(j.m_data.m_value.binary->subtype()));
16441 }
16442
16443 // step 2: write the byte string
16444 oa->write_characters(
16445 reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
16446 N);
16447
16448 break;
16449 }
16450
16451 case value_t::object:
16452 {
16453 // step 1: write control byte and the object size
16454 const auto N = j.m_data.m_value.object->size();
16455 if (N <= 15)
16456 {
16457 // fixmap
16458 write_number(static_cast<std::uint8_t>(0x80 | (N & 0xF)));
16459 }
16460 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
16461 {
16462 // map 16
16463 oa->write_character(to_char_type(0xDE));
16464 write_number(static_cast<std::uint16_t>(N));
16465 }
16466 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
16467 {
16468 // map 32
16469 oa->write_character(to_char_type(0xDF));
16470 write_number(static_cast<std::uint32_t>(N));
16471 }
16472
16473 // step 2: write each element
16474 for (const auto& el : *j.m_data.m_value.object)
16475 {
16476 write_msgpack(el.first);
16477 write_msgpack(el.second);
16478 }
16479 break;
16480 }
16481
16482 case value_t::discarded:
16483 default:
16484 break;
16485 }
16486 }
16487
16488 /*!
16489 @param[in] j JSON value to serialize
16490 @param[in] use_count whether to use '#' prefixes (optimized format)
16491 @param[in] use_type whether to use '$' prefixes (optimized format)
16492 @param[in] add_prefix whether prefixes need to be used for this value
16493 @param[in] use_bjdata whether write in BJData format, default is false
16494 @param[in] bjdata_version which BJData version to use, default is draft2
16495 */
16496 void write_ubjson(const BasicJsonType& j, const bool use_count,
16497 const bool use_type, const bool add_prefix = true,
16498 const bool use_bjdata = false, const bjdata_version_t bjdata_version = bjdata_version_t::draft2)
16499 {
16500 const bool bjdata_draft3 = use_bjdata && bjdata_version == bjdata_version_t::draft3;
16501
16502 switch (j.type())
16503 {
16504 case value_t::null:
16505 {
16506 if (add_prefix)
16507 {
16508 oa->write_character(to_char_type('Z'));
16509 }
16510 break;
16511 }
16512
16513 case value_t::boolean:
16514 {
16515 if (add_prefix)
16516 {
16517 oa->write_character(j.m_data.m_value.boolean
16518 ? to_char_type('T')
16519 : to_char_type('F'));
16520 }
16521 break;
16522 }
16523
16524 case value_t::number_integer:
16525 {
16526 write_number_with_ubjson_prefix(j.m_data.m_value.number_integer, add_prefix, use_bjdata);
16527 break;
16528 }
16529
16530 case value_t::number_unsigned:
16531 {
16532 write_number_with_ubjson_prefix(j.m_data.m_value.number_unsigned, add_prefix, use_bjdata);
16533 break;
16534 }
16535
16536 case value_t::number_float:
16537 {
16538 write_number_with_ubjson_prefix(j.m_data.m_value.number_float, add_prefix, use_bjdata);
16539 break;
16540 }
16541
16542 case value_t::string:
16543 {
16544 if (add_prefix)
16545 {
16546 oa->write_character(to_char_type('S'));
16547 }
16548 write_number_with_ubjson_prefix(j.m_data.m_value.string->size(), true, use_bjdata);
16549 oa->write_characters(
16550 reinterpret_cast<const CharType*>(j.m_data.m_value.string->c_str()),
16551 j.m_data.m_value.string->size());
16552 break;
16553 }
16554
16555 case value_t::array:
16556 {
16557 if (add_prefix)
16558 {
16559 oa->write_character(to_char_type('['));
16560 }
16561
16562 bool prefix_required = true;
16563 if (use_type && !j.m_data.m_value.array->empty())
16564 {
16565 JSON_ASSERT(use_count);
16566 const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata);
16567 const bool same_prefix = std::all_of(j.begin() + 1, j.end(),
16568 [this, first_prefix, use_bjdata](const BasicJsonType & v)
16569 {
16570 return ubjson_prefix(v, use_bjdata) == first_prefix;
16571 });
16572
16573 std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
16574
16575 if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
16576 {
16577 prefix_required = false;
16578 oa->write_character(to_char_type('$'));
16579 oa->write_character(first_prefix);
16580 }
16581 }
16582
16583 if (use_count)
16584 {
16585 oa->write_character(to_char_type('#'));
16586 write_number_with_ubjson_prefix(j.m_data.m_value.array->size(), true, use_bjdata);
16587 }
16588
16589 for (const auto& el : *j.m_data.m_value.array)
16590 {
16591 write_ubjson(el, use_count, use_type, prefix_required, use_bjdata, bjdata_version);
16592 }
16593
16594 if (!use_count)
16595 {
16596 oa->write_character(to_char_type(']'));
16597 }
16598
16599 break;
16600 }
16601
16602 case value_t::binary:
16603 {
16604 if (add_prefix)
16605 {
16606 oa->write_character(to_char_type('['));
16607 }
16608
16609 if (use_type && (bjdata_draft3 || !j.m_data.m_value.binary->empty()))
16610 {
16611 JSON_ASSERT(use_count);
16612 oa->write_character(to_char_type('$'));
16613 oa->write_character(bjdata_draft3 ? 'B' : 'U');
16614 }
16615
16616 if (use_count)
16617 {
16618 oa->write_character(to_char_type('#'));
16619 write_number_with_ubjson_prefix(j.m_data.m_value.binary->size(), true, use_bjdata);
16620 }
16621
16622 if (use_type)
16623 {
16624 oa->write_characters(
16625 reinterpret_cast<const CharType*>(j.m_data.m_value.binary->data()),
16626 j.m_data.m_value.binary->size());
16627 }
16628 else
16629 {
16630 for (size_t i = 0; i < j.m_data.m_value.binary->size(); ++i)
16631 {
16632 oa->write_character(to_char_type(bjdata_draft3 ? 'B' : 'U'));
16633 oa->write_character(j.m_data.m_value.binary->data()[i]);
16634 }
16635 }
16636
16637 if (!use_count)
16638 {
16639 oa->write_character(to_char_type(']'));
16640 }
16641
16642 break;
16643 }
16644
16645 case value_t::object:
16646 {
16647 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())
16648 {
16649 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)
16650 {
16651 break;
16652 }
16653 }
16654
16655 if (add_prefix)
16656 {
16657 oa->write_character(to_char_type('{'));
16658 }
16659
16660 bool prefix_required = true;
16661 if (use_type && !j.m_data.m_value.object->empty())
16662 {
16663 JSON_ASSERT(use_count);
16664 const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata);
16665 const bool same_prefix = std::all_of(j.begin(), j.end(),
16666 [this, first_prefix, use_bjdata](const BasicJsonType & v)
16667 {
16668 return ubjson_prefix(v, use_bjdata) == first_prefix;
16669 });
16670
16671 std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
16672
16673 if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
16674 {
16675 prefix_required = false;
16676 oa->write_character(to_char_type('$'));
16677 oa->write_character(first_prefix);
16678 }
16679 }
16680
16681 if (use_count)
16682 {
16683 oa->write_character(to_char_type('#'));
16684 write_number_with_ubjson_prefix(j.m_data.m_value.object->size(), true, use_bjdata);
16685 }
16686
16687 for (const auto& el : *j.m_data.m_value.object)
16688 {
16689 write_number_with_ubjson_prefix(el.first.size(), true, use_bjdata);
16690 oa->write_characters(
16691 reinterpret_cast<const CharType*>(el.first.c_str()),
16692 el.first.size());
16693 write_ubjson(el.second, use_count, use_type, prefix_required, use_bjdata, bjdata_version);
16694 }
16695
16696 if (!use_count)
16697 {
16698 oa->write_character(to_char_type('}'));
16699 }
16700
16701 break;
16702 }
16703
16704 case value_t::discarded:
16705 default:
16706 break;
16707 }
16708 }
16709
16710 private:
16711 //////////
16712 // BSON //
16713 //////////
16714
16715 /*!
16716 @return The size of a BSON document entry header, including the id marker
16717 and the entry name size (and its null-terminator).
16718 */
16719 static std::size_t calc_bson_entry_header_size(const string_t& name, const BasicJsonType& j)
16720 {
16721 const auto it = name.find(static_cast<typename string_t::value_type>(0));
16722 if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos))
16723 {
16724 JSON_THROW(out_of_range::create(409, concat("BSON key cannot contain code point U+0000 (at byte ", std::to_string(it), ")"), &j));
16725 static_cast<void>(j);
16726 }
16727
16728 return /*id*/ 1ul + name.size() + /*zero-terminator*/1u;
16729 }
16730
16731 /*!
16732 @brief Writes the given @a element_type and @a name to the output adapter
16733 */
16734 void write_bson_entry_header(const string_t& name,
16735 const std::uint8_t element_type)
16736 {
16737 oa->write_character(to_char_type(element_type)); // boolean
16738 oa->write_characters(
16739 reinterpret_cast<const CharType*>(name.c_str()),
16740 name.size() + 1u);
16741 }
16742
16743 /*!
16744 @brief Writes a BSON element with key @a name and boolean value @a value
16745 */
16746 void write_bson_boolean(const string_t& name,
16747 const bool value)
16748 {
16749 write_bson_entry_header(name, 0x08);
16750 oa->write_character(value ? to_char_type(0x01) : to_char_type(0x00));
16751 }
16752
16753 /*!
16754 @brief Writes a BSON element with key @a name and double value @a value
16755 */
16756 void write_bson_double(const string_t& name,
16757 const double value)
16758 {
16759 write_bson_entry_header(name, 0x01);
16760 write_number<double>(value, true);
16761 }
16762
16763 /*!
16764 @return The size of the BSON-encoded string in @a value
16765 */
16766 static std::size_t calc_bson_string_size(const string_t& value)
16767 {
16768 return sizeof(std::int32_t) + value.size() + 1ul;
16769 }
16770
16771 /*!
16772 @brief Writes a BSON element with key @a name and string value @a value
16773 */
16774 void write_bson_string(const string_t& name,
16775 const string_t& value)
16776 {
16777 write_bson_entry_header(name, 0x02);
16778
16779 write_number<std::int32_t>(static_cast<std::int32_t>(value.size() + 1ul), true);
16780 oa->write_characters(
16781 reinterpret_cast<const CharType*>(value.c_str()),
16782 value.size() + 1);
16783 }
16784
16785 /*!
16786 @brief Writes a BSON element with key @a name and null value
16787 */
16788 void write_bson_null(const string_t& name)
16789 {
16790 write_bson_entry_header(name, 0x0A);
16791 }
16792
16793 /*!
16794 @return The size of the BSON-encoded integer @a value
16795 */
16796 static std::size_t calc_bson_integer_size(const std::int64_t value)
16797 {
16798 return (std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)()
16799 ? sizeof(std::int32_t)
16800 : sizeof(std::int64_t);
16801 }
16802
16803 /*!
16804 @brief Writes a BSON element with key @a name and integer @a value
16805 */
16806 void write_bson_integer(const string_t& name,
16807 const std::int64_t value)
16808 {
16809 if ((std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)())
16810 {
16811 write_bson_entry_header(name, 0x10); // int32
16812 write_number<std::int32_t>(static_cast<std::int32_t>(value), true);
16813 }
16814 else
16815 {
16816 write_bson_entry_header(name, 0x12); // int64
16817 write_number<std::int64_t>(static_cast<std::int64_t>(value), true);
16818 }
16819 }
16820
16821 /*!
16822 @return The size of the BSON-encoded unsigned integer in @a j
16823 */
16824 static constexpr std::size_t calc_bson_unsigned_size(const std::uint64_t value) noexcept
16825 {
16826 return (value <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
16827 ? sizeof(std::int32_t)
16828 : sizeof(std::int64_t);
16829 }
16830
16831 /*!
16832 @brief Writes a BSON element with key @a name and unsigned @a value
16833 */
16834 void write_bson_unsigned(const string_t& name,
16835 const BasicJsonType& j)
16836 {
16837 if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
16838 {
16839 write_bson_entry_header(name, 0x10 /* int32 */);
16840 write_number<std::int32_t>(static_cast<std::int32_t>(j.m_data.m_value.number_unsigned), true);
16841 }
16842 else if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
16843 {
16844 write_bson_entry_header(name, 0x12 /* int64 */);
16845 write_number<std::int64_t>(static_cast<std::int64_t>(j.m_data.m_value.number_unsigned), true);
16846 }
16847 else
16848 {
16849 write_bson_entry_header(name, 0x11 /* uint64 */);
16850 write_number<std::uint64_t>(static_cast<std::uint64_t>(j.m_data.m_value.number_unsigned), true);
16851 }
16852 }
16853
16854 /*!
16855 @brief Writes a BSON element with key @a name and object @a value
16856 */
16857 void write_bson_object_entry(const string_t& name,
16858 const typename BasicJsonType::object_t& value)
16859 {
16860 write_bson_entry_header(name, 0x03); // object
16861 write_bson_object(value);
16862 }
16863
16864 /*!
16865 @return The size of the BSON-encoded array @a value
16866 */
16867 static std::size_t calc_bson_array_size(const typename BasicJsonType::array_t& value)
16868 {
16869 std::size_t array_index = 0ul;
16870
16871 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)
16872 {
16873 return result + calc_bson_element_size(std::to_string(array_index++), el);
16874 });
16875
16876 return sizeof(std::int32_t) + embedded_document_size + 1ul;
16877 }
16878
16879 /*!
16880 @return The size of the BSON-encoded binary array @a value
16881 */
16882 static std::size_t calc_bson_binary_size(const typename BasicJsonType::binary_t& value)
16883 {
16884 return sizeof(std::int32_t) + value.size() + 1ul;
16885 }
16886
16887 /*!
16888 @brief Writes a BSON element with key @a name and array @a value
16889 */
16890 void write_bson_array(const string_t& name,
16891 const typename BasicJsonType::array_t& value)
16892 {
16893 write_bson_entry_header(name, 0x04); // array
16894 write_number<std::int32_t>(static_cast<std::int32_t>(calc_bson_array_size(value)), true);
16895
16896 std::size_t array_index = 0ul;
16897
16898 for (const auto& el : value)
16899 {
16900 write_bson_element(std::to_string(array_index++), el);
16901 }
16902
16903 oa->write_character(to_char_type(0x00));
16904 }
16905
16906 /*!
16907 @brief Writes a BSON element with key @a name and binary value @a value
16908 */
16909 void write_bson_binary(const string_t& name,
16910 const binary_t& value)
16911 {
16912 write_bson_entry_header(name, 0x05);
16913
16914 write_number<std::int32_t>(static_cast<std::int32_t>(value.size()), true);
16915 write_number(value.has_subtype() ? static_cast<std::uint8_t>(value.subtype()) : static_cast<std::uint8_t>(0x00));
16916
16917 oa->write_characters(reinterpret_cast<const CharType*>(value.data()), value.size());
16918 }
16919
16920 /*!
16921 @brief Calculates the size necessary to serialize the JSON value @a j with its @a name
16922 @return The calculated size for the BSON document entry for @a j with the given @a name.
16923 */
16924 static std::size_t calc_bson_element_size(const string_t& name,
16925 const BasicJsonType& j)
16926 {
16927 const auto header_size = calc_bson_entry_header_size(name, j);
16928 switch (j.type())
16929 {
16930 case value_t::object:
16931 return header_size + calc_bson_object_size(*j.m_data.m_value.object);
16932
16933 case value_t::array:
16934 return header_size + calc_bson_array_size(*j.m_data.m_value.array);
16935
16936 case value_t::binary:
16937 return header_size + calc_bson_binary_size(*j.m_data.m_value.binary);
16938
16939 case value_t::boolean:
16940 return header_size + 1ul;
16941
16942 case value_t::number_float:
16943 return header_size + 8ul;
16944
16945 case value_t::number_integer:
16946 return header_size + calc_bson_integer_size(j.m_data.m_value.number_integer);
16947
16948 case value_t::number_unsigned:
16949 return header_size + calc_bson_unsigned_size(j.m_data.m_value.number_unsigned);
16950
16951 case value_t::string:
16952 return header_size + calc_bson_string_size(*j.m_data.m_value.string);
16953
16954 case value_t::null:
16955 return header_size + 0ul;
16956
16957 // LCOV_EXCL_START
16958 case value_t::discarded:
16959 default:
16960 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert)
16961 return 0ul;
16962 // LCOV_EXCL_STOP
16963 }
16964 }
16965
16966 /*!
16967 @brief Serializes the JSON value @a j to BSON and associates it with the
16968 key @a name.
16969 @param name The name to associate with the JSON entity @a j within the
16970 current BSON document
16971 */
16972 void write_bson_element(const string_t& name,
16973 const BasicJsonType& j)
16974 {
16975 switch (j.type())
16976 {
16977 case value_t::object:
16978 return write_bson_object_entry(name, *j.m_data.m_value.object);
16979
16980 case value_t::array:
16981 return write_bson_array(name, *j.m_data.m_value.array);
16982
16983 case value_t::binary:
16984 return write_bson_binary(name, *j.m_data.m_value.binary);
16985
16986 case value_t::boolean:
16987 return write_bson_boolean(name, j.m_data.m_value.boolean);
16988
16989 case value_t::number_float:
16990 return write_bson_double(name, j.m_data.m_value.number_float);
16991
16992 case value_t::number_integer:
16993 return write_bson_integer(name, j.m_data.m_value.number_integer);
16994
16995 case value_t::number_unsigned:
16996 return write_bson_unsigned(name, j);
16997
16998 case value_t::string:
16999 return write_bson_string(name, *j.m_data.m_value.string);
17000
17001 case value_t::null:
17002 return write_bson_null(name);
17003
17004 // LCOV_EXCL_START
17005 case value_t::discarded:
17006 default:
17007 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert)
17008 return;
17009 // LCOV_EXCL_STOP
17010 }
17011 }
17012
17013 /*!
17014 @brief Calculates the size of the BSON serialization of the given
17015 JSON-object @a j.
17016 @param[in] value JSON value to serialize
17017 @pre value.type() == value_t::object
17018 */
17019 static std::size_t calc_bson_object_size(const typename BasicJsonType::object_t& value)
17020 {
17021 const std::size_t document_size = std::accumulate(value.begin(), value.end(), static_cast<std::size_t>(0),
17022 [](size_t result, const typename BasicJsonType::object_t::value_type & el)
17023 {
17024 return result += calc_bson_element_size(el.first, el.second);
17025 });
17026
17027 return sizeof(std::int32_t) + document_size + 1ul;
17028 }
17029
17030 /*!
17031 @param[in] value JSON value to serialize
17032 @pre value.type() == value_t::object
17033 */
17034 void write_bson_object(const typename BasicJsonType::object_t& value)
17035 {
17036 write_number<std::int32_t>(static_cast<std::int32_t>(calc_bson_object_size(value)), true);
17037
17038 for (const auto& el : value)
17039 {
17040 write_bson_element(el.first, el.second);
17041 }
17042
17043 oa->write_character(to_char_type(0x00));
17044 }
17045
17046 //////////
17047 // CBOR //
17048 //////////
17049
17050 static constexpr CharType get_cbor_float_prefix(float /*unused*/)
17051 {
17052 return to_char_type(0xFA); // Single-Precision Float
17053 }
17054
17055 static constexpr CharType get_cbor_float_prefix(double /*unused*/)
17056 {
17057 return to_char_type(0xFB); // Double-Precision Float
17058 }
17059
17060 /////////////
17061 // MsgPack //
17062 /////////////
17063
17064 static constexpr CharType get_msgpack_float_prefix(float /*unused*/)
17065 {
17066 return to_char_type(0xCA); // float 32
17067 }
17068
17069 static constexpr CharType get_msgpack_float_prefix(double /*unused*/)
17070 {
17071 return to_char_type(0xCB); // float 64
17072 }
17073
17074 ////////////
17075 // UBJSON //
17076 ////////////
17077
17078 // UBJSON: write number (floating point)
17079 template<typename NumberType, typename std::enable_if<
17080 std::is_floating_point<NumberType>::value, int>::type = 0>
17081 void write_number_with_ubjson_prefix(const NumberType n,
17082 const bool add_prefix,
17083 const bool use_bjdata)
17084 {
17085 if (add_prefix)
17086 {
17087 oa->write_character(get_ubjson_float_prefix(n));
17088 }
17089 write_number(n, use_bjdata);
17090 }
17091
17092 // UBJSON: write number (unsigned integer)
17093 template<typename NumberType, typename std::enable_if<
17094 std::is_unsigned<NumberType>::value, int>::type = 0>
17095 void write_number_with_ubjson_prefix(const NumberType n,
17096 const bool add_prefix,
17097 const bool use_bjdata)
17098 {
17099 if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
17100 {
17101 if (add_prefix)
17102 {
17103 oa->write_character(to_char_type('i')); // int8
17104 }
17105 write_number(static_cast<std::uint8_t>(n), use_bjdata);
17106 }
17107 else if (n <= (std::numeric_limits<std::uint8_t>::max)())
17108 {
17109 if (add_prefix)
17110 {
17111 oa->write_character(to_char_type('U')); // uint8
17112 }
17113 write_number(static_cast<std::uint8_t>(n), use_bjdata);
17114 }
17115 else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
17116 {
17117 if (add_prefix)
17118 {
17119 oa->write_character(to_char_type('I')); // int16
17120 }
17121 write_number(static_cast<std::int16_t>(n), use_bjdata);
17122 }
17123 else if (use_bjdata && n <= static_cast<uint64_t>((std::numeric_limits<uint16_t>::max)()))
17124 {
17125 if (add_prefix)
17126 {
17127 oa->write_character(to_char_type('u')); // uint16 - bjdata only
17128 }
17129 write_number(static_cast<std::uint16_t>(n), use_bjdata);
17130 }
17131 else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
17132 {
17133 if (add_prefix)
17134 {
17135 oa->write_character(to_char_type('l')); // int32
17136 }
17137 write_number(static_cast<std::int32_t>(n), use_bjdata);
17138 }
17139 else if (use_bjdata && n <= static_cast<uint64_t>((std::numeric_limits<uint32_t>::max)()))
17140 {
17141 if (add_prefix)
17142 {
17143 oa->write_character(to_char_type('m')); // uint32 - bjdata only
17144 }
17145 write_number(static_cast<std::uint32_t>(n), use_bjdata);
17146 }
17147 else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
17148 {
17149 if (add_prefix)
17150 {
17151 oa->write_character(to_char_type('L')); // int64
17152 }
17153 write_number(static_cast<std::int64_t>(n), use_bjdata);
17154 }
17155 else if (use_bjdata && n <= (std::numeric_limits<uint64_t>::max)())
17156 {
17157 if (add_prefix)
17158 {
17159 oa->write_character(to_char_type('M')); // uint64 - bjdata only
17160 }
17161 write_number(static_cast<std::uint64_t>(n), use_bjdata);
17162 }
17163 else
17164 {
17165 if (add_prefix)
17166 {
17167 oa->write_character(to_char_type('H')); // high-precision number
17168 }
17169
17170 const auto number = BasicJsonType(n).dump();
17171 write_number_with_ubjson_prefix(number.size(), true, use_bjdata);
17172 for (std::size_t i = 0; i < number.size(); ++i)
17173 {
17174 oa->write_character(to_char_type(static_cast<std::uint8_t>(number[i])));
17175 }
17176 }
17177 }
17178
17179 // UBJSON: write number (signed integer)
17180 template < typename NumberType, typename std::enable_if <
17181 std::is_signed<NumberType>::value&&
17182 !std::is_floating_point<NumberType>::value, int >::type = 0 >
17183 void write_number_with_ubjson_prefix(const NumberType n,
17184 const bool add_prefix,
17185 const bool use_bjdata)
17186 {
17187 if ((std::numeric_limits<std::int8_t>::min)() <= n && n <= (std::numeric_limits<std::int8_t>::max)())
17188 {
17189 if (add_prefix)
17190 {
17191 oa->write_character(to_char_type('i')); // int8
17192 }
17193 write_number(static_cast<std::int8_t>(n), use_bjdata);
17194 }
17195 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)()))
17196 {
17197 if (add_prefix)
17198 {
17199 oa->write_character(to_char_type('U')); // uint8
17200 }
17201 write_number(static_cast<std::uint8_t>(n), use_bjdata);
17202 }
17203 else if ((std::numeric_limits<std::int16_t>::min)() <= n && n <= (std::numeric_limits<std::int16_t>::max)())
17204 {
17205 if (add_prefix)
17206 {
17207 oa->write_character(to_char_type('I')); // int16
17208 }
17209 write_number(static_cast<std::int16_t>(n), use_bjdata);
17210 }
17211 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)())))
17212 {
17213 if (add_prefix)
17214 {
17215 oa->write_character(to_char_type('u')); // uint16 - bjdata only
17216 }
17217 write_number(static_cast<uint16_t>(n), use_bjdata);
17218 }
17219 else if ((std::numeric_limits<std::int32_t>::min)() <= n && n <= (std::numeric_limits<std::int32_t>::max)())
17220 {
17221 if (add_prefix)
17222 {
17223 oa->write_character(to_char_type('l')); // int32
17224 }
17225 write_number(static_cast<std::int32_t>(n), use_bjdata);
17226 }
17227 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)())))
17228 {
17229 if (add_prefix)
17230 {
17231 oa->write_character(to_char_type('m')); // uint32 - bjdata only
17232 }
17233 write_number(static_cast<uint32_t>(n), use_bjdata);
17234 }
17235 else if ((std::numeric_limits<std::int64_t>::min)() <= n && n <= (std::numeric_limits<std::int64_t>::max)())
17236 {
17237 if (add_prefix)
17238 {
17239 oa->write_character(to_char_type('L')); // int64
17240 }
17241 write_number(static_cast<std::int64_t>(n), use_bjdata);
17242 }
17243 // LCOV_EXCL_START
17244 else
17245 {
17246 if (add_prefix)
17247 {
17248 oa->write_character(to_char_type('H')); // high-precision number
17249 }
17250
17251 const auto number = BasicJsonType(n).dump();
17252 write_number_with_ubjson_prefix(number.size(), true, use_bjdata);
17253 for (std::size_t i = 0; i < number.size(); ++i)
17254 {
17255 oa->write_character(to_char_type(static_cast<std::uint8_t>(number[i])));
17256 }
17257 }
17258 // LCOV_EXCL_STOP
17259 }
17260
17261 /*!
17262 @brief determine the type prefix of container values
17263 */
17264 CharType ubjson_prefix(const BasicJsonType& j, const bool use_bjdata) const noexcept
17265 {
17266 switch (j.type())
17267 {
17268 case value_t::null:
17269 return 'Z';
17270
17271 case value_t::boolean:
17272 return j.m_data.m_value.boolean ? 'T' : 'F';
17273
17274 case value_t::number_integer:
17275 {
17276 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)())
17277 {
17278 return 'i';
17279 }
17280 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)())
17281 {
17282 return 'U';
17283 }
17284 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)())
17285 {
17286 return 'I';
17287 }
17288 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)()))
17289 {
17290 return 'u';
17291 }
17292 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)())
17293 {
17294 return 'l';
17295 }
17296 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)()))
17297 {
17298 return 'm';
17299 }
17300 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)())
17301 {
17302 return 'L';
17303 }
17304 // anything else is treated as high-precision number
17305 return 'H'; // LCOV_EXCL_LINE
17306 }
17307
17308 case value_t::number_unsigned:
17309 {
17310 if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
17311 {
17312 return 'i';
17313 }
17314 if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint8_t>::max)()))
17315 {
17316 return 'U';
17317 }
17318 if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
17319 {
17320 return 'I';
17321 }
17322 if (use_bjdata && j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint16_t>::max)()))
17323 {
17324 return 'u';
17325 }
17326 if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
17327 {
17328 return 'l';
17329 }
17330 if (use_bjdata && j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint32_t>::max)()))
17331 {
17332 return 'm';
17333 }
17334 if (j.m_data.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
17335 {
17336 return 'L';
17337 }
17338 if (use_bjdata && j.m_data.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
17339 {
17340 return 'M';
17341 }
17342 // anything else is treated as high-precision number
17343 return 'H'; // LCOV_EXCL_LINE
17344 }
17345
17346 case value_t::number_float:
17347 return get_ubjson_float_prefix(j.m_data.m_value.number_float);
17348
17349 case value_t::string:
17350 return 'S';
17351
17352 case value_t::array: // fallthrough
17353 case value_t::binary:
17354 return '[';
17355
17356 case value_t::object:
17357 return '{';
17358
17359 case value_t::discarded:
17360 default: // discarded values
17361 return 'N';
17362 }
17363 }
17364
17365 static constexpr CharType get_ubjson_float_prefix(float /*unused*/)
17366 {
17367 return 'd'; // float 32
17368 }
17369
17370 static constexpr CharType get_ubjson_float_prefix(double /*unused*/)
17371 {
17372 return 'D'; // float 64
17373 }
17374
17375 /*!
17376 @return false if the object is successfully converted to a bjdata ndarray, true if the type or size is invalid
17377 */
17378 bool write_bjdata_ndarray(const typename BasicJsonType::object_t& value, const bool use_count, const bool use_type, const bjdata_version_t bjdata_version)
17379 {
17380 std::map<string_t, CharType> bjdtype = {{"uint8", 'U'}, {"int8", 'i'}, {"uint16", 'u'}, {"int16", 'I'},
17381 {"uint32", 'm'}, {"int32", 'l'}, {"uint64", 'M'}, {"int64", 'L'}, {"single", 'd'}, {"double", 'D'},
17382 {"char", 'C'}, {"byte", 'B'}
17383 };
17384
17385 string_t key = "_ArrayType_";
17386 auto it = bjdtype.find(static_cast<string_t>(value.at(key)));
17387 if (it == bjdtype.end())
17388 {
17389 return true;
17390 }
17391 CharType dtype = it->second;
17392
17393 key = "_ArraySize_";
17394 std::size_t len = (value.at(key).empty() ? 0 : 1);
17395 for (const auto& el : value.at(key))
17396 {
17397 len *= static_cast<std::size_t>(el.m_data.m_value.number_unsigned);
17398 }
17399
17400 key = "_ArrayData_";
17401 if (value.at(key).size() != len)
17402 {
17403 return true;
17404 }
17405
17406 oa->write_character('[');
17407 oa->write_character('$');
17408 oa->write_character(dtype);
17409 oa->write_character('#');
17410
17411 key = "_ArraySize_";
17412 write_ubjson(value.at(key), use_count, use_type, true, true, bjdata_version);
17413
17414 key = "_ArrayData_";
17415 if (dtype == 'U' || dtype == 'C' || dtype == 'B')
17416 {
17417 for (const auto& el : value.at(key))
17418 {
17419 write_number(static_cast<std::uint8_t>(el.m_data.m_value.number_unsigned), true);
17420 }
17421 }
17422 else if (dtype == 'i')
17423 {
17424 for (const auto& el : value.at(key))
17425 {
17426 write_number(static_cast<std::int8_t>(el.m_data.m_value.number_integer), true);
17427 }
17428 }
17429 else if (dtype == 'u')
17430 {
17431 for (const auto& el : value.at(key))
17432 {
17433 write_number(static_cast<std::uint16_t>(el.m_data.m_value.number_unsigned), true);
17434 }
17435 }
17436 else if (dtype == 'I')
17437 {
17438 for (const auto& el : value.at(key))
17439 {
17440 write_number(static_cast<std::int16_t>(el.m_data.m_value.number_integer), true);
17441 }
17442 }
17443 else if (dtype == 'm')
17444 {
17445 for (const auto& el : value.at(key))
17446 {
17447 write_number(static_cast<std::uint32_t>(el.m_data.m_value.number_unsigned), true);
17448 }
17449 }
17450 else if (dtype == 'l')
17451 {
17452 for (const auto& el : value.at(key))
17453 {
17454 write_number(static_cast<std::int32_t>(el.m_data.m_value.number_integer), true);
17455 }
17456 }
17457 else if (dtype == 'M')
17458 {
17459 for (const auto& el : value.at(key))
17460 {
17461 write_number(static_cast<std::uint64_t>(el.m_data.m_value.number_unsigned), true);
17462 }
17463 }
17464 else if (dtype == 'L')
17465 {
17466 for (const auto& el : value.at(key))
17467 {
17468 write_number(static_cast<std::int64_t>(el.m_data.m_value.number_integer), true);
17469 }
17470 }
17471 else if (dtype == 'd')
17472 {
17473 for (const auto& el : value.at(key))
17474 {
17475 write_number(static_cast<float>(el.m_data.m_value.number_float), true);
17476 }
17477 }
17478 else if (dtype == 'D')
17479 {
17480 for (const auto& el : value.at(key))
17481 {
17482 write_number(static_cast<double>(el.m_data.m_value.number_float), true);
17483 }
17484 }
17485 return false;
17486 }
17487
17488 ///////////////////////
17489 // Utility functions //
17490 ///////////////////////
17491
17492 /*
17493 @brief write a number to output input
17494 @param[in] n number of type @a NumberType
17495 @param[in] OutputIsLittleEndian Set to true if output data is
17496 required to be little endian
17497 @tparam NumberType the type of the number
17498
17499 @note This function needs to respect the system's endianness, because bytes
17500 in CBOR, MessagePack, and UBJSON are stored in network order (big
17501 endian) and therefore need reordering on little endian systems.
17502 On the other hand, BSON and BJData use little endian and should reorder
17503 on big endian systems.
17504 */
17505 template<typename NumberType>
17506 void write_number(const NumberType n, const bool OutputIsLittleEndian = false)
17507 {
17508 // step 1: write number to array of length NumberType
17509 std::array<CharType, sizeof(NumberType)> vec{};
17510 std::memcpy(vec.data(), &n, sizeof(NumberType));
17511
17512 // step 2: write array to output (with possible reordering)
17513 if (is_little_endian != OutputIsLittleEndian)
17514 {
17515 // reverse byte order prior to conversion if necessary
17516 std::reverse(vec.begin(), vec.end());
17517 }
17518
17519 oa->write_characters(vec.data(), sizeof(NumberType));
17520 }
17521
17522 void write_compact_float(const number_float_t n, detail::input_format_t format)
17523 {
17524 #ifdef __GNUC__
17525 #pragma GCC diagnostic push
17526 #pragma GCC diagnostic ignored "-Wfloat-equal"
17527 #endif
17528 if (static_cast<double>(n) >= static_cast<double>(std::numeric_limits<float>::lowest()) &&
17529 static_cast<double>(n) <= static_cast<double>((std::numeric_limits<float>::max)()) &&
17530 static_cast<double>(static_cast<float>(n)) == static_cast<double>(n))
17531 {
17532 oa->write_character(format == detail::input_format_t::cbor
17533 ? get_cbor_float_prefix(static_cast<float>(n))
17534 : get_msgpack_float_prefix(static_cast<float>(n)));
17535 write_number(static_cast<float>(n));
17536 }
17537 else
17538 {
17539 oa->write_character(format == detail::input_format_t::cbor
17540 ? get_cbor_float_prefix(n)
17541 : get_msgpack_float_prefix(n));
17542 write_number(n);
17543 }
17544 #ifdef __GNUC__
17545 #pragma GCC diagnostic pop
17546 #endif
17547 }
17548
17549 public:
17550 // The following to_char_type functions are implement the conversion
17551 // between uint8_t and CharType. In case CharType is not unsigned,
17552 // such a conversion is required to allow values greater than 128.
17553 // See <https://github.com/nlohmann/json/issues/1286> for a discussion.
17554 template < typename C = CharType,
17555 enable_if_t < std::is_signed<C>::value && std::is_signed<char>::value > * = nullptr >
17556 static constexpr CharType to_char_type(std::uint8_t x) noexcept
17557 {
17558 return *reinterpret_cast<char*>(&x);
17559 }
17560
17561 template < typename C = CharType,
17562 enable_if_t < std::is_signed<C>::value && std::is_unsigned<char>::value > * = nullptr >
17563 static CharType to_char_type(std::uint8_t x) noexcept
17564 {
17565 static_assert(sizeof(std::uint8_t) == sizeof(CharType), "size of CharType must be equal to std::uint8_t");
17566 static_assert(std::is_trivial<CharType>::value, "CharType must be trivial");
17567 CharType result;
17568 std::memcpy(&result, &x, sizeof(x));
17569 return result;
17570 }
17571
17572 template<typename C = CharType,
17573 enable_if_t<std::is_unsigned<C>::value>* = nullptr>
17574 static constexpr CharType to_char_type(std::uint8_t x) noexcept
17575 {
17576 return x;
17577 }
17578
17579 template < typename InputCharType, typename C = CharType,
17580 enable_if_t <
17581 std::is_signed<C>::value &&
17582 std::is_signed<char>::value &&
17583 std::is_same<char, typename std::remove_cv<InputCharType>::type>::value
17584 > * = nullptr >
17585 static constexpr CharType to_char_type(InputCharType x) noexcept
17586 {
17587 return x;
17588 }
17589
17590 private:
17591 /// whether we can assume little endianness
17592 const bool is_little_endian = little_endianness();
17593
17594 /// the output
17595 output_adapter_t<CharType> oa = nullptr;
17596 };
17597
17598 } // namespace detail
17599 NLOHMANN_JSON_NAMESPACE_END
17600
17601 // #include <nlohmann/detail/output/output_adapters.hpp>
17602
17603 // #include <nlohmann/detail/output/serializer.hpp>
17604 // __ _____ _____ _____
17605 // __| | __| | | | JSON for Modern C++
17606 // | | |__ | | | | | | version 3.12.0
17607 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
17608 //
17609 // SPDX-FileCopyrightText: 2008 - 2009 Björn Hoehrmann <[email protected]>
17610 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
17611 // SPDX-License-Identifier: MIT
17612
17613
17614
17615 #include <algorithm> // reverse, remove, fill, find, none_of
17616 #include <array> // array
17617 #include <clocale> // localeconv, lconv
17618 #include <cmath> // labs, isfinite, isnan, signbit
17619 #include <cstddef> // size_t, ptrdiff_t
17620 #include <cstdint> // uint8_t
17621 #include <cstdio> // snprintf
17622 #include <limits> // numeric_limits
17623 #include <string> // string, char_traits
17624 #include <iomanip> // setfill, setw
17625 #include <type_traits> // is_same
17626 #include <utility> // move
17627
17628 // #include <nlohmann/detail/conversions/to_chars.hpp>
17629 // __ _____ _____ _____
17630 // __| | __| | | | JSON for Modern C++
17631 // | | |__ | | | | | | version 3.12.0
17632 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
17633 //
17634 // SPDX-FileCopyrightText: 2009 Florian Loitsch <https://florian.loitsch.com/>
17635 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
17636 // SPDX-License-Identifier: MIT
17637
17638
17639
17640 #include <array> // array
17641 #include <cmath> // signbit, isfinite
17642 #include <cstdint> // intN_t, uintN_t
17643 #include <cstring> // memcpy, memmove
17644 #include <limits> // numeric_limits
17645 #include <type_traits> // conditional
17646
17647 // #include <nlohmann/detail/macro_scope.hpp>
17648
17649
17650 NLOHMANN_JSON_NAMESPACE_BEGIN
17651 namespace detail
17652 {
17653
17654 /*!
17655 @brief implements the Grisu2 algorithm for binary to decimal floating-point
17656 conversion.
17657
17658 This implementation is a slightly modified version of the reference
17659 implementation which may be obtained from
17660 http://florian.loitsch.com/publications (bench.tar.gz).
17661
17662 The code is distributed under the MIT license, Copyright (c) 2009 Florian Loitsch.
17663
17664 For a detailed description of the algorithm see:
17665
17666 [1] Loitsch, "Printing Floating-Point Numbers Quickly and Accurately with
17667 Integers", Proceedings of the ACM SIGPLAN 2010 Conference on Programming
17668 Language Design and Implementation, PLDI 2010
17669 [2] Burger, Dybvig, "Printing Floating-Point Numbers Quickly and Accurately",
17670 Proceedings of the ACM SIGPLAN 1996 Conference on Programming Language
17671 Design and Implementation, PLDI 1996
17672 */
17673 namespace dtoa_impl
17674 {
17675
17676 template<typename Target, typename Source>
17677 Target reinterpret_bits(const Source source)
17678 {
17679 static_assert(sizeof(Target) == sizeof(Source), "size mismatch");
17680
17681 Target target;
17682 std::memcpy(&target, &source, sizeof(Source));
17683 return target;
17684 }
17685
17686 struct diyfp // f * 2^e
17687 {
17688 static constexpr int kPrecision = 64; // = q
17689
17690 std::uint64_t f = 0;
17691 int e = 0;
17692
17693 constexpr diyfp(std::uint64_t f_, int e_) noexcept : f(f_), e(e_) {}
17694
17695 /*!
17696 @brief returns x - y
17697 @pre x.e == y.e and x.f >= y.f
17698 */
17699 static diyfp sub(const diyfp& x, const diyfp& y) noexcept
17700 {
17701 JSON_ASSERT(x.e == y.e);
17702 JSON_ASSERT(x.f >= y.f);
17703
17704 return {x.f - y.f, x.e};
17705 }
17706
17707 /*!
17708 @brief returns x * y
17709 @note The result is rounded. (Only the upper q bits are returned.)
17710 */
17711 static diyfp mul(const diyfp& x, const diyfp& y) noexcept
17712 {
17713 static_assert(kPrecision == 64, "internal error");
17714
17715 // Computes:
17716 // f = round((x.f * y.f) / 2^q)
17717 // e = x.e + y.e + q
17718
17719 // Emulate the 64-bit * 64-bit multiplication:
17720 //
17721 // p = u * v
17722 // = (u_lo + 2^32 u_hi) (v_lo + 2^32 v_hi)
17723 // = (u_lo v_lo ) + 2^32 ((u_lo v_hi ) + (u_hi v_lo )) + 2^64 (u_hi v_hi )
17724 // = (p0 ) + 2^32 ((p1 ) + (p2 )) + 2^64 (p3 )
17725 // = (p0_lo + 2^32 p0_hi) + 2^32 ((p1_lo + 2^32 p1_hi) + (p2_lo + 2^32 p2_hi)) + 2^64 (p3 )
17726 // = (p0_lo ) + 2^32 (p0_hi + p1_lo + p2_lo ) + 2^64 (p1_hi + p2_hi + p3)
17727 // = (p0_lo ) + 2^32 (Q ) + 2^64 (H )
17728 // = (p0_lo ) + 2^32 (Q_lo + 2^32 Q_hi ) + 2^64 (H )
17729 //
17730 // (Since Q might be larger than 2^32 - 1)
17731 //
17732 // = (p0_lo + 2^32 Q_lo) + 2^64 (Q_hi + H)
17733 //
17734 // (Q_hi + H does not overflow a 64-bit int)
17735 //
17736 // = p_lo + 2^64 p_hi
17737
17738 const std::uint64_t u_lo = x.f & 0xFFFFFFFFu;
17739 const std::uint64_t u_hi = x.f >> 32u;
17740 const std::uint64_t v_lo = y.f & 0xFFFFFFFFu;
17741 const std::uint64_t v_hi = y.f >> 32u;
17742
17743 const std::uint64_t p0 = u_lo * v_lo;
17744 const std::uint64_t p1 = u_lo * v_hi;
17745 const std::uint64_t p2 = u_hi * v_lo;
17746 const std::uint64_t p3 = u_hi * v_hi;
17747
17748 const std::uint64_t p0_hi = p0 >> 32u;
17749 const std::uint64_t p1_lo = p1 & 0xFFFFFFFFu;
17750 const std::uint64_t p1_hi = p1 >> 32u;
17751 const std::uint64_t p2_lo = p2 & 0xFFFFFFFFu;
17752 const std::uint64_t p2_hi = p2 >> 32u;
17753
17754 std::uint64_t Q = p0_hi + p1_lo + p2_lo;
17755
17756 // The full product might now be computed as
17757 //
17758 // p_hi = p3 + p2_hi + p1_hi + (Q >> 32)
17759 // p_lo = p0_lo + (Q << 32)
17760 //
17761 // But in this particular case here, the full p_lo is not required.
17762 // Effectively we only need to add the highest bit in p_lo to p_hi (and
17763 // Q_hi + 1 does not overflow).
17764
17765 Q += std::uint64_t{1} << (64u - 32u - 1u); // round, ties up
17766
17767 const std::uint64_t h = p3 + p2_hi + p1_hi + (Q >> 32u);
17768
17769 return {h, x.e + y.e + 64};
17770 }
17771
17772 /*!
17773 @brief normalize x such that the significand is >= 2^(q-1)
17774 @pre x.f != 0
17775 */
17776 static diyfp normalize(diyfp x) noexcept
17777 {
17778 JSON_ASSERT(x.f != 0);
17779
17780 while ((x.f >> 63u) == 0)
17781 {
17782 x.f <<= 1u;
17783 x.e--;
17784 }
17785
17786 return x;
17787 }
17788
17789 /*!
17790 @brief normalize x such that the result has the exponent E
17791 @pre e >= x.e and the upper e - x.e bits of x.f must be zero.
17792 */
17793 static diyfp normalize_to(const diyfp& x, const int target_exponent) noexcept
17794 {
17795 const int delta = x.e - target_exponent;
17796
17797 JSON_ASSERT(delta >= 0);
17798 JSON_ASSERT(((x.f << delta) >> delta) == x.f);
17799
17800 return {x.f << delta, target_exponent};
17801 }
17802 };
17803
17804 struct boundaries
17805 {
17806 diyfp w;
17807 diyfp minus;
17808 diyfp plus;
17809 };
17810
17811 /*!
17812 Compute the (normalized) diyfp representing the input number 'value' and its
17813 boundaries.
17814
17815 @pre value must be finite and positive
17816 */
17817 template<typename FloatType>
17818 boundaries compute_boundaries(FloatType value)
17819 {
17820 JSON_ASSERT(std::isfinite(value));
17821 JSON_ASSERT(value > 0);
17822
17823 // Convert the IEEE representation into a diyfp.
17824 //
17825 // If v is denormal:
17826 // value = 0.F * 2^(1 - bias) = ( F) * 2^(1 - bias - (p-1))
17827 // If v is normalized:
17828 // value = 1.F * 2^(E - bias) = (2^(p-1) + F) * 2^(E - bias - (p-1))
17829
17830 static_assert(std::numeric_limits<FloatType>::is_iec559,
17831 "internal error: dtoa_short requires an IEEE-754 floating-point implementation");
17832
17833 constexpr int kPrecision = std::numeric_limits<FloatType>::digits; // = p (includes the hidden bit)
17834 constexpr int kBias = std::numeric_limits<FloatType>::max_exponent - 1 + (kPrecision - 1);
17835 constexpr int kMinExp = 1 - kBias;
17836 constexpr std::uint64_t kHiddenBit = std::uint64_t{1} << (kPrecision - 1); // = 2^(p-1)
17837
17838 using bits_type = typename std::conditional<kPrecision == 24, std::uint32_t, std::uint64_t >::type;
17839
17840 const auto bits = static_cast<std::uint64_t>(reinterpret_bits<bits_type>(value));
17841 const std::uint64_t E = bits >> (kPrecision - 1);
17842 const std::uint64_t F = bits & (kHiddenBit - 1);
17843
17844 const bool is_denormal = E == 0;
17845 const diyfp v = is_denormal
17846 ? diyfp(F, kMinExp)
17847 : diyfp(F + kHiddenBit, static_cast<int>(E) - kBias);
17848
17849 // Compute the boundaries m- and m+ of the floating-point value
17850 // v = f * 2^e.
17851 //
17852 // Determine v- and v+, the floating-point predecessor and successor if v,
17853 // respectively.
17854 //
17855 // v- = v - 2^e if f != 2^(p-1) or e == e_min (A)
17856 // = v - 2^(e-1) if f == 2^(p-1) and e > e_min (B)
17857 //
17858 // v+ = v + 2^e
17859 //
17860 // Let m- = (v- + v) / 2 and m+ = (v + v+) / 2. All real numbers _strictly_
17861 // between m- and m+ round to v, regardless of how the input rounding
17862 // algorithm breaks ties.
17863 //
17864 // ---+-------------+-------------+-------------+-------------+--- (A)
17865 // v- m- v m+ v+
17866 //
17867 // -----------------+------+------+-------------+-------------+--- (B)
17868 // v- m- v m+ v+
17869
17870 const bool lower_boundary_is_closer = F == 0 && E > 1;
17871 const diyfp m_plus = diyfp((2 * v.f) + 1, v.e - 1);
17872 const diyfp m_minus = lower_boundary_is_closer
17873 ? diyfp((4 * v.f) - 1, v.e - 2) // (B)
17874 : diyfp((2 * v.f) - 1, v.e - 1); // (A)
17875
17876 // Determine the normalized w+ = m+.
17877 const diyfp w_plus = diyfp::normalize(m_plus);
17878
17879 // Determine w- = m- such that e_(w-) = e_(w+).
17880 const diyfp w_minus = diyfp::normalize_to(m_minus, w_plus.e);
17881
17882 return {diyfp::normalize(v), w_minus, w_plus};
17883 }
17884
17885 // Given normalized diyfp w, Grisu needs to find a (normalized) cached
17886 // power-of-ten c, such that the exponent of the product c * w = f * 2^e lies
17887 // within a certain range [alpha, gamma] (Definition 3.2 from [1])
17888 //
17889 // alpha <= e = e_c + e_w + q <= gamma
17890 //
17891 // or
17892 //
17893 // f_c * f_w * 2^alpha <= f_c 2^(e_c) * f_w 2^(e_w) * 2^q
17894 // <= f_c * f_w * 2^gamma
17895 //
17896 // Since c and w are normalized, i.e. 2^(q-1) <= f < 2^q, this implies
17897 //
17898 // 2^(q-1) * 2^(q-1) * 2^alpha <= c * w * 2^q < 2^q * 2^q * 2^gamma
17899 //
17900 // or
17901 //
17902 // 2^(q - 2 + alpha) <= c * w < 2^(q + gamma)
17903 //
17904 // The choice of (alpha,gamma) determines the size of the table and the form of
17905 // the digit generation procedure. Using (alpha,gamma)=(-60,-32) works out well
17906 // in practice:
17907 //
17908 // The idea is to cut the number c * w = f * 2^e into two parts, which can be
17909 // processed independently: An integral part p1, and a fractional part p2:
17910 //
17911 // f * 2^e = ( (f div 2^-e) * 2^-e + (f mod 2^-e) ) * 2^e
17912 // = (f div 2^-e) + (f mod 2^-e) * 2^e
17913 // = p1 + p2 * 2^e
17914 //
17915 // The conversion of p1 into decimal form requires a series of divisions and
17916 // modulos by (a power of) 10. These operations are faster for 32-bit than for
17917 // 64-bit integers, so p1 should ideally fit into a 32-bit integer. This can be
17918 // achieved by choosing
17919 //
17920 // -e >= 32 or e <= -32 := gamma
17921 //
17922 // In order to convert the fractional part
17923 //
17924 // p2 * 2^e = p2 / 2^-e = d[-1] / 10^1 + d[-2] / 10^2 + ...
17925 //
17926 // into decimal form, the fraction is repeatedly multiplied by 10 and the digits
17927 // d[-i] are extracted in order:
17928 //
17929 // (10 * p2) div 2^-e = d[-1]
17930 // (10 * p2) mod 2^-e = d[-2] / 10^1 + ...
17931 //
17932 // The multiplication by 10 must not overflow. It is sufficient to choose
17933 //
17934 // 10 * p2 < 16 * p2 = 2^4 * p2 <= 2^64.
17935 //
17936 // Since p2 = f mod 2^-e < 2^-e,
17937 //
17938 // -e <= 60 or e >= -60 := alpha
17939
17940 constexpr int kAlpha = -60;
17941 constexpr int kGamma = -32;
17942
17943 struct cached_power // c = f * 2^e ~= 10^k
17944 {
17945 std::uint64_t f;
17946 int e;
17947 int k;
17948 };
17949
17950 /*!
17951 For a normalized diyfp w = f * 2^e, this function returns a (normalized) cached
17952 power-of-ten c = f_c * 2^e_c, such that the exponent of the product w * c
17953 satisfies (Definition 3.2 from [1])
17954
17955 alpha <= e_c + e + q <= gamma.
17956 */
17957 inline cached_power get_cached_power_for_binary_exponent(int e)
17958 {
17959 // Now
17960 //
17961 // alpha <= e_c + e + q <= gamma (1)
17962 // ==> f_c * 2^alpha <= c * 2^e * 2^q
17963 //
17964 // and since the c's are normalized, 2^(q-1) <= f_c,
17965 //
17966 // ==> 2^(q - 1 + alpha) <= c * 2^(e + q)
17967 // ==> 2^(alpha - e - 1) <= c
17968 //
17969 // If c were an exact power of ten, i.e. c = 10^k, one may determine k as
17970 //
17971 // k = ceil( log_10( 2^(alpha - e - 1) ) )
17972 // = ceil( (alpha - e - 1) * log_10(2) )
17973 //
17974 // From the paper:
17975 // "In theory the result of the procedure could be wrong since c is rounded,
17976 // and the computation itself is approximated [...]. In practice, however,
17977 // this simple function is sufficient."
17978 //
17979 // For IEEE double precision floating-point numbers converted into
17980 // normalized diyfp's w = f * 2^e, with q = 64,
17981 //
17982 // e >= -1022 (min IEEE exponent)
17983 // -52 (p - 1)
17984 // -52 (p - 1, possibly normalize denormal IEEE numbers)
17985 // -11 (normalize the diyfp)
17986 // = -1137
17987 //
17988 // and
17989 //
17990 // e <= +1023 (max IEEE exponent)
17991 // -52 (p - 1)
17992 // -11 (normalize the diyfp)
17993 // = 960
17994 //
17995 // This binary exponent range [-1137,960] results in a decimal exponent
17996 // range [-307,324]. One does not need to store a cached power for each
17997 // k in this range. For each such k it suffices to find a cached power
17998 // such that the exponent of the product lies in [alpha,gamma].
17999 // This implies that the difference of the decimal exponents of adjacent
18000 // table entries must be less than or equal to
18001 //
18002 // floor( (gamma - alpha) * log_10(2) ) = 8.
18003 //
18004 // (A smaller distance gamma-alpha would require a larger table.)
18005
18006 // NB:
18007 // Actually this function returns c, such that -60 <= e_c + e + 64 <= -34.
18008
18009 constexpr int kCachedPowersMinDecExp = -300;
18010 constexpr int kCachedPowersDecStep = 8;
18011
18012 static constexpr std::array<cached_power, 79> kCachedPowers =
18013 {
18014 {
18015 { 0xAB70FE17C79AC6CA, -1060, -300 },
18016 { 0xFF77B1FCBEBCDC4F, -1034, -292 },
18017 { 0xBE5691EF416BD60C, -1007, -284 },
18018 { 0x8DD01FAD907FFC3C, -980, -276 },
18019 { 0xD3515C2831559A83, -954, -268 },
18020 { 0x9D71AC8FADA6C9B5, -927, -260 },
18021 { 0xEA9C227723EE8BCB, -901, -252 },
18022 { 0xAECC49914078536D, -874, -244 },
18023 { 0x823C12795DB6CE57, -847, -236 },
18024 { 0xC21094364DFB5637, -821, -228 },
18025 { 0x9096EA6F3848984F, -794, -220 },
18026 { 0xD77485CB25823AC7, -768, -212 },
18027 { 0xA086CFCD97BF97F4, -741, -204 },
18028 { 0xEF340A98172AACE5, -715, -196 },
18029 { 0xB23867FB2A35B28E, -688, -188 },
18030 { 0x84C8D4DFD2C63F3B, -661, -180 },
18031 { 0xC5DD44271AD3CDBA, -635, -172 },
18032 { 0x936B9FCEBB25C996, -608, -164 },
18033 { 0xDBAC6C247D62A584, -582, -156 },
18034 { 0xA3AB66580D5FDAF6, -555, -148 },
18035 { 0xF3E2F893DEC3F126, -529, -140 },
18036 { 0xB5B5ADA8AAFF80B8, -502, -132 },
18037 { 0x87625F056C7C4A8B, -475, -124 },
18038 { 0xC9BCFF6034C13053, -449, -116 },
18039 { 0x964E858C91BA2655, -422, -108 },
18040 { 0xDFF9772470297EBD, -396, -100 },
18041 { 0xA6DFBD9FB8E5B88F, -369, -92 },
18042 { 0xF8A95FCF88747D94, -343, -84 },
18043 { 0xB94470938FA89BCF, -316, -76 },
18044 { 0x8A08F0F8BF0F156B, -289, -68 },
18045 { 0xCDB02555653131B6, -263, -60 },
18046 { 0x993FE2C6D07B7FAC, -236, -52 },
18047 { 0xE45C10C42A2B3B06, -210, -44 },
18048 { 0xAA242499697392D3, -183, -36 },
18049 { 0xFD87B5F28300CA0E, -157, -28 },
18050 { 0xBCE5086492111AEB, -130, -20 },
18051 { 0x8CBCCC096F5088CC, -103, -12 },
18052 { 0xD1B71758E219652C, -77, -4 },
18053 { 0x9C40000000000000, -50, 4 },
18054 { 0xE8D4A51000000000, -24, 12 },
18055 { 0xAD78EBC5AC620000, 3, 20 },
18056 { 0x813F3978F8940984, 30, 28 },
18057 { 0xC097CE7BC90715B3, 56, 36 },
18058 { 0x8F7E32CE7BEA5C70, 83, 44 },
18059 { 0xD5D238A4ABE98068, 109, 52 },
18060 { 0x9F4F2726179A2245, 136, 60 },
18061 { 0xED63A231D4C4FB27, 162, 68 },
18062 { 0xB0DE65388CC8ADA8, 189, 76 },
18063 { 0x83C7088E1AAB65DB, 216, 84 },
18064 { 0xC45D1DF942711D9A, 242, 92 },
18065 { 0x924D692CA61BE758, 269, 100 },
18066 { 0xDA01EE641A708DEA, 295, 108 },
18067 { 0xA26DA3999AEF774A, 322, 116 },
18068 { 0xF209787BB47D6B85, 348, 124 },
18069 { 0xB454E4A179DD1877, 375, 132 },
18070 { 0x865B86925B9BC5C2, 402, 140 },
18071 { 0xC83553C5C8965D3D, 428, 148 },
18072 { 0x952AB45CFA97A0B3, 455, 156 },
18073 { 0xDE469FBD99A05FE3, 481, 164 },
18074 { 0xA59BC234DB398C25, 508, 172 },
18075 { 0xF6C69A72A3989F5C, 534, 180 },
18076 { 0xB7DCBF5354E9BECE, 561, 188 },
18077 { 0x88FCF317F22241E2, 588, 196 },
18078 { 0xCC20CE9BD35C78A5, 614, 204 },
18079 { 0x98165AF37B2153DF, 641, 212 },
18080 { 0xE2A0B5DC971F303A, 667, 220 },
18081 { 0xA8D9D1535CE3B396, 694, 228 },
18082 { 0xFB9B7CD9A4A7443C, 720, 236 },
18083 { 0xBB764C4CA7A44410, 747, 244 },
18084 { 0x8BAB8EEFB6409C1A, 774, 252 },
18085 { 0xD01FEF10A657842C, 800, 260 },
18086 { 0x9B10A4E5E9913129, 827, 268 },
18087 { 0xE7109BFBA19C0C9D, 853, 276 },
18088 { 0xAC2820D9623BF429, 880, 284 },
18089 { 0x80444B5E7AA7CF85, 907, 292 },
18090 { 0xBF21E44003ACDD2D, 933, 300 },
18091 { 0x8E679C2F5E44FF8F, 960, 308 },
18092 { 0xD433179D9C8CB841, 986, 316 },
18093 { 0x9E19DB92B4E31BA9, 1013, 324 },
18094 }
18095 };
18096
18097 // This computation gives exactly the same results for k as
18098 // k = ceil((kAlpha - e - 1) * 0.30102999566398114)
18099 // for |e| <= 1500, but doesn't require floating-point operations.
18100 // NB: log_10(2) ~= 78913 / 2^18
18101 JSON_ASSERT(e >= -1500);
18102 JSON_ASSERT(e <= 1500);
18103 const int f = kAlpha - e - 1;
18104 const int k = ((f * 78913) / (1 << 18)) + static_cast<int>(f > 0);
18105
18106 const int index = (-kCachedPowersMinDecExp + k + (kCachedPowersDecStep - 1)) / kCachedPowersDecStep;
18107 JSON_ASSERT(index >= 0);
18108 JSON_ASSERT(static_cast<std::size_t>(index) < kCachedPowers.size());
18109
18110 const cached_power cached = kCachedPowers[static_cast<std::size_t>(index)];
18111 JSON_ASSERT(kAlpha <= cached.e + e + 64);
18112 JSON_ASSERT(kGamma >= cached.e + e + 64);
18113
18114 return cached;
18115 }
18116
18117 /*!
18118 For n != 0, returns k, such that pow10 := 10^(k-1) <= n < 10^k.
18119 For n == 0, returns 1 and sets pow10 := 1.
18120 */
18121 inline int find_largest_pow10(const std::uint32_t n, std::uint32_t& pow10)
18122 {
18123 // LCOV_EXCL_START
18124 if (n >= 1000000000)
18125 {
18126 pow10 = 1000000000;
18127 return 10;
18128 }
18129 // LCOV_EXCL_STOP
18130 if (n >= 100000000)
18131 {
18132 pow10 = 100000000;
18133 return 9;
18134 }
18135 if (n >= 10000000)
18136 {
18137 pow10 = 10000000;
18138 return 8;
18139 }
18140 if (n >= 1000000)
18141 {
18142 pow10 = 1000000;
18143 return 7;
18144 }
18145 if (n >= 100000)
18146 {
18147 pow10 = 100000;
18148 return 6;
18149 }
18150 if (n >= 10000)
18151 {
18152 pow10 = 10000;
18153 return 5;
18154 }
18155 if (n >= 1000)
18156 {
18157 pow10 = 1000;
18158 return 4;
18159 }
18160 if (n >= 100)
18161 {
18162 pow10 = 100;
18163 return 3;
18164 }
18165 if (n >= 10)
18166 {
18167 pow10 = 10;
18168 return 2;
18169 }
18170
18171 pow10 = 1;
18172 return 1;
18173 }
18174
18175 inline void grisu2_round(char* buf, int len, std::uint64_t dist, std::uint64_t delta,
18176 std::uint64_t rest, std::uint64_t ten_k)
18177 {
18178 JSON_ASSERT(len >= 1);
18179 JSON_ASSERT(dist <= delta);
18180 JSON_ASSERT(rest <= delta);
18181 JSON_ASSERT(ten_k > 0);
18182
18183 // <--------------------------- delta ---->
18184 // <---- dist --------->
18185 // --------------[------------------+-------------------]--------------
18186 // M- w M+
18187 //
18188 // ten_k
18189 // <------>
18190 // <---- rest ---->
18191 // --------------[------------------+----+--------------]--------------
18192 // w V
18193 // = buf * 10^k
18194 //
18195 // ten_k represents a unit-in-the-last-place in the decimal representation
18196 // stored in buf.
18197 // Decrement buf by ten_k while this takes buf closer to w.
18198
18199 // The tests are written in this order to avoid overflow in unsigned
18200 // integer arithmetic.
18201
18202 while (rest < dist
18203 && delta - rest >= ten_k
18204 && (rest + ten_k < dist || dist - rest > rest + ten_k - dist))
18205 {
18206 JSON_ASSERT(buf[len - 1] != '0');
18207 buf[len - 1]--;
18208 rest += ten_k;
18209 }
18210 }
18211
18212 /*!
18213 Generates V = buffer * 10^decimal_exponent, such that M- <= V <= M+.
18214 M- and M+ must be normalized and share the same exponent -60 <= e <= -32.
18215 */
18216 inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent,
18217 diyfp M_minus, diyfp w, diyfp M_plus)
18218 {
18219 static_assert(kAlpha >= -60, "internal error");
18220 static_assert(kGamma <= -32, "internal error");
18221
18222 // Generates the digits (and the exponent) of a decimal floating-point
18223 // number V = buffer * 10^decimal_exponent in the range [M-, M+]. The diyfp's
18224 // w, M- and M+ share the same exponent e, which satisfies alpha <= e <= gamma.
18225 //
18226 // <--------------------------- delta ---->
18227 // <---- dist --------->
18228 // --------------[------------------+-------------------]--------------
18229 // M- w M+
18230 //
18231 // Grisu2 generates the digits of M+ from left to right and stops as soon as
18232 // V is in [M-,M+].
18233
18234 JSON_ASSERT(M_plus.e >= kAlpha);
18235 JSON_ASSERT(M_plus.e <= kGamma);
18236
18237 std::uint64_t delta = diyfp::sub(M_plus, M_minus).f; // (significand of (M+ - M-), implicit exponent is e)
18238 std::uint64_t dist = diyfp::sub(M_plus, w ).f; // (significand of (M+ - w ), implicit exponent is e)
18239
18240 // Split M+ = f * 2^e into two parts p1 and p2 (note: e < 0):
18241 //
18242 // M+ = f * 2^e
18243 // = ((f div 2^-e) * 2^-e + (f mod 2^-e)) * 2^e
18244 // = ((p1 ) * 2^-e + (p2 )) * 2^e
18245 // = p1 + p2 * 2^e
18246
18247 const diyfp one(std::uint64_t{1} << -M_plus.e, M_plus.e);
18248
18249 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.)
18250 std::uint64_t p2 = M_plus.f & (one.f - 1); // p2 = f mod 2^-e
18251
18252 // 1)
18253 //
18254 // Generate the digits of the integral part p1 = d[n-1]...d[1]d[0]
18255
18256 JSON_ASSERT(p1 > 0);
18257
18258 std::uint32_t pow10{};
18259 const int k = find_largest_pow10(p1, pow10);
18260
18261 // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1)
18262 //
18263 // p1 = (p1 div 10^(k-1)) * 10^(k-1) + (p1 mod 10^(k-1))
18264 // = (d[k-1] ) * 10^(k-1) + (p1 mod 10^(k-1))
18265 //
18266 // M+ = p1 + p2 * 2^e
18267 // = d[k-1] * 10^(k-1) + (p1 mod 10^(k-1)) + p2 * 2^e
18268 // = d[k-1] * 10^(k-1) + ((p1 mod 10^(k-1)) * 2^-e + p2) * 2^e
18269 // = d[k-1] * 10^(k-1) + ( rest) * 2^e
18270 //
18271 // Now generate the digits d[n] of p1 from left to right (n = k-1,...,0)
18272 //
18273 // p1 = d[k-1]...d[n] * 10^n + d[n-1]...d[0]
18274 //
18275 // but stop as soon as
18276 //
18277 // rest * 2^e = (d[n-1]...d[0] * 2^-e + p2) * 2^e <= delta * 2^e
18278
18279 int n = k;
18280 while (n > 0)
18281 {
18282 // Invariants:
18283 // M+ = buffer * 10^n + (p1 + p2 * 2^e) (buffer = 0 for n = k)
18284 // pow10 = 10^(n-1) <= p1 < 10^n
18285 //
18286 const std::uint32_t d = p1 / pow10; // d = p1 div 10^(n-1)
18287 const std::uint32_t r = p1 % pow10; // r = p1 mod 10^(n-1)
18288 //
18289 // M+ = buffer * 10^n + (d * 10^(n-1) + r) + p2 * 2^e
18290 // = (buffer * 10 + d) * 10^(n-1) + (r + p2 * 2^e)
18291 //
18292 JSON_ASSERT(d <= 9);
18293 buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
18294 //
18295 // M+ = buffer * 10^(n-1) + (r + p2 * 2^e)
18296 //
18297 p1 = r;
18298 n--;
18299 //
18300 // M+ = buffer * 10^n + (p1 + p2 * 2^e)
18301 // pow10 = 10^n
18302 //
18303
18304 // Now check if enough digits have been generated.
18305 // Compute
18306 //
18307 // p1 + p2 * 2^e = (p1 * 2^-e + p2) * 2^e = rest * 2^e
18308 //
18309 // Note:
18310 // Since rest and delta share the same exponent e, it suffices to
18311 // compare the significands.
18312 const std::uint64_t rest = (std::uint64_t{p1} << -one.e) + p2;
18313 if (rest <= delta)
18314 {
18315 // V = buffer * 10^n, with M- <= V <= M+.
18316
18317 decimal_exponent += n;
18318
18319 // We may now just stop. But instead look if the buffer could be
18320 // decremented to bring V closer to w.
18321 //
18322 // pow10 = 10^n is now 1 ulp in the decimal representation V.
18323 // The rounding procedure works with diyfp's with an implicit
18324 // exponent of e.
18325 //
18326 // 10^n = (10^n * 2^-e) * 2^e = ulp * 2^e
18327 //
18328 const std::uint64_t ten_n = std::uint64_t{pow10} << -one.e;
18329 grisu2_round(buffer, length, dist, delta, rest, ten_n);
18330
18331 return;
18332 }
18333
18334 pow10 /= 10;
18335 //
18336 // pow10 = 10^(n-1) <= p1 < 10^n
18337 // Invariants restored.
18338 }
18339
18340 // 2)
18341 //
18342 // The digits of the integral part have been generated:
18343 //
18344 // M+ = d[k-1]...d[1]d[0] + p2 * 2^e
18345 // = buffer + p2 * 2^e
18346 //
18347 // Now generate the digits of the fractional part p2 * 2^e.
18348 //
18349 // Note:
18350 // No decimal point is generated: the exponent is adjusted instead.
18351 //
18352 // p2 actually represents the fraction
18353 //
18354 // p2 * 2^e
18355 // = p2 / 2^-e
18356 // = d[-1] / 10^1 + d[-2] / 10^2 + ...
18357 //
18358 // Now generate the digits d[-m] of p1 from left to right (m = 1,2,...)
18359 //
18360 // p2 * 2^e = d[-1]d[-2]...d[-m] * 10^-m
18361 // + 10^-m * (d[-m-1] / 10^1 + d[-m-2] / 10^2 + ...)
18362 //
18363 // using
18364 //
18365 // 10^m * p2 = ((10^m * p2) div 2^-e) * 2^-e + ((10^m * p2) mod 2^-e)
18366 // = ( d) * 2^-e + ( r)
18367 //
18368 // or
18369 // 10^m * p2 * 2^e = d + r * 2^e
18370 //
18371 // i.e.
18372 //
18373 // M+ = buffer + p2 * 2^e
18374 // = buffer + 10^-m * (d + r * 2^e)
18375 // = (buffer * 10^m + d) * 10^-m + 10^-m * r * 2^e
18376 //
18377 // and stop as soon as 10^-m * r * 2^e <= delta * 2^e
18378
18379 JSON_ASSERT(p2 > delta);
18380
18381 int m = 0;
18382 for (;;)
18383 {
18384 // Invariant:
18385 // M+ = buffer * 10^-m + 10^-m * (d[-m-1] / 10 + d[-m-2] / 10^2 + ...) * 2^e
18386 // = buffer * 10^-m + 10^-m * (p2 ) * 2^e
18387 // = buffer * 10^-m + 10^-m * (1/10 * (10 * p2) ) * 2^e
18388 // = buffer * 10^-m + 10^-m * (1/10 * ((10*p2 div 2^-e) * 2^-e + (10*p2 mod 2^-e)) * 2^e
18389 //
18390 JSON_ASSERT(p2 <= (std::numeric_limits<std::uint64_t>::max)() / 10);
18391 p2 *= 10;
18392 const std::uint64_t d = p2 >> -one.e; // d = (10 * p2) div 2^-e
18393 const std::uint64_t r = p2 & (one.f - 1); // r = (10 * p2) mod 2^-e
18394 //
18395 // M+ = buffer * 10^-m + 10^-m * (1/10 * (d * 2^-e + r) * 2^e
18396 // = buffer * 10^-m + 10^-m * (1/10 * (d + r * 2^e))
18397 // = (buffer * 10 + d) * 10^(-m-1) + 10^(-m-1) * r * 2^e
18398 //
18399 JSON_ASSERT(d <= 9);
18400 buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
18401 //
18402 // M+ = buffer * 10^(-m-1) + 10^(-m-1) * r * 2^e
18403 //
18404 p2 = r;
18405 m++;
18406 //
18407 // M+ = buffer * 10^-m + 10^-m * p2 * 2^e
18408 // Invariant restored.
18409
18410 // Check if enough digits have been generated.
18411 //
18412 // 10^-m * p2 * 2^e <= delta * 2^e
18413 // p2 * 2^e <= 10^m * delta * 2^e
18414 // p2 <= 10^m * delta
18415 delta *= 10;
18416 dist *= 10;
18417 if (p2 <= delta)
18418 {
18419 break;
18420 }
18421 }
18422
18423 // V = buffer * 10^-m, with M- <= V <= M+.
18424
18425 decimal_exponent -= m;
18426
18427 // 1 ulp in the decimal representation is now 10^-m.
18428 // Since delta and dist are now scaled by 10^m, we need to do the
18429 // same with ulp in order to keep the units in sync.
18430 //
18431 // 10^m * 10^-m = 1 = 2^-e * 2^e = ten_m * 2^e
18432 //
18433 const std::uint64_t ten_m = one.f;
18434 grisu2_round(buffer, length, dist, delta, p2, ten_m);
18435
18436 // By construction this algorithm generates the shortest possible decimal
18437 // number (Loitsch, Theorem 6.2) which rounds back to w.
18438 // For an input number of precision p, at least
18439 //
18440 // N = 1 + ceil(p * log_10(2))
18441 //
18442 // decimal digits are sufficient to identify all binary floating-point
18443 // numbers (Matula, "In-and-Out conversions").
18444 // This implies that the algorithm does not produce more than N decimal
18445 // digits.
18446 //
18447 // N = 17 for p = 53 (IEEE double precision)
18448 // N = 9 for p = 24 (IEEE single precision)
18449 }
18450
18451 /*!
18452 v = buf * 10^decimal_exponent
18453 len is the length of the buffer (number of decimal digits)
18454 The buffer must be large enough, i.e. >= max_digits10.
18455 */
18456 JSON_HEDLEY_NON_NULL(1)
18457 inline void grisu2(char* buf, int& len, int& decimal_exponent,
18458 diyfp m_minus, diyfp v, diyfp m_plus)
18459 {
18460 JSON_ASSERT(m_plus.e == m_minus.e);
18461 JSON_ASSERT(m_plus.e == v.e);
18462
18463 // --------(-----------------------+-----------------------)-------- (A)
18464 // m- v m+
18465 //
18466 // --------------------(-----------+-----------------------)-------- (B)
18467 // m- v m+
18468 //
18469 // First scale v (and m- and m+) such that the exponent is in the range
18470 // [alpha, gamma].
18471
18472 const cached_power cached = get_cached_power_for_binary_exponent(m_plus.e);
18473
18474 const diyfp c_minus_k(cached.f, cached.e); // = c ~= 10^-k
18475
18476 // The exponent of the products is = v.e + c_minus_k.e + q and is in the range [alpha,gamma]
18477 const diyfp w = diyfp::mul(v, c_minus_k);
18478 const diyfp w_minus = diyfp::mul(m_minus, c_minus_k);
18479 const diyfp w_plus = diyfp::mul(m_plus, c_minus_k);
18480
18481 // ----(---+---)---------------(---+---)---------------(---+---)----
18482 // w- w w+
18483 // = c*m- = c*v = c*m+
18484 //
18485 // diyfp::mul rounds its result and c_minus_k is approximated too. w, w- and
18486 // w+ are now off by a small amount.
18487 // In fact:
18488 //
18489 // w - v * 10^k < 1 ulp
18490 //
18491 // To account for this inaccuracy, add resp. subtract 1 ulp.
18492 //
18493 // --------+---[---------------(---+---)---------------]---+--------
18494 // w- M- w M+ w+
18495 //
18496 // Now any number in [M-, M+] (bounds included) will round to w when input,
18497 // regardless of how the input rounding algorithm breaks ties.
18498 //
18499 // And digit_gen generates the shortest possible such number in [M-, M+].
18500 // Note that this does not mean that Grisu2 always generates the shortest
18501 // possible number in the interval (m-, m+).
18502 const diyfp M_minus(w_minus.f + 1, w_minus.e);
18503 const diyfp M_plus (w_plus.f - 1, w_plus.e );
18504
18505 decimal_exponent = -cached.k; // = -(-k) = k
18506
18507 grisu2_digit_gen(buf, len, decimal_exponent, M_minus, w, M_plus);
18508 }
18509
18510 /*!
18511 v = buf * 10^decimal_exponent
18512 len is the length of the buffer (number of decimal digits)
18513 The buffer must be large enough, i.e. >= max_digits10.
18514 */
18515 template<typename FloatType>
18516 JSON_HEDLEY_NON_NULL(1)
18517 void grisu2(char* buf, int& len, int& decimal_exponent, FloatType value)
18518 {
18519 static_assert(diyfp::kPrecision >= std::numeric_limits<FloatType>::digits + 3,
18520 "internal error: not enough precision");
18521
18522 JSON_ASSERT(std::isfinite(value));
18523 JSON_ASSERT(value > 0);
18524
18525 // If the neighbors (and boundaries) of 'value' are always computed for double-precision
18526 // numbers, all float's can be recovered using strtod (and strtof). However, the resulting
18527 // decimal representations are not exactly "short".
18528 //
18529 // The documentation for 'std::to_chars' (https://en.cppreference.com/w/cpp/utility/to_chars)
18530 // says "value is converted to a string as if by std::sprintf in the default ("C") locale"
18531 // and since sprintf promotes floats to doubles, I think this is exactly what 'std::to_chars'
18532 // does.
18533 // On the other hand, the documentation for 'std::to_chars' requires that "parsing the
18534 // representation using the corresponding std::from_chars function recovers value exactly". That
18535 // indicates that single precision floating-point numbers should be recovered using
18536 // 'std::strtof'.
18537 //
18538 // NB: If the neighbors are computed for single-precision numbers, there is a single float
18539 // (7.0385307e-26f) which can't be recovered using strtod. The resulting double precision
18540 // value is off by 1 ulp.
18541 #if 0 // NOLINT(readability-avoid-unconditional-preprocessor-if)
18542 const boundaries w = compute_boundaries(static_cast<double>(value));
18543 #else
18544 const boundaries w = compute_boundaries(value);
18545 #endif
18546
18547 grisu2(buf, len, decimal_exponent, w.minus, w.w, w.plus);
18548 }
18549
18550 /*!
18551 @brief appends a decimal representation of e to buf
18552 @return a pointer to the element following the exponent.
18553 @pre -1000 < e < 1000
18554 */
18555 JSON_HEDLEY_NON_NULL(1)
18556 JSON_HEDLEY_RETURNS_NON_NULL
18557 inline char* append_exponent(char* buf, int e)
18558 {
18559 JSON_ASSERT(e > -1000);
18560 JSON_ASSERT(e < 1000);
18561
18562 if (e < 0)
18563 {
18564 e = -e;
18565 *buf++ = '-';
18566 }
18567 else
18568 {
18569 *buf++ = '+';
18570 }
18571
18572 auto k = static_cast<std::uint32_t>(e);
18573 if (k < 10)
18574 {
18575 // Always print at least two digits in the exponent.
18576 // This is for compatibility with printf("%g").
18577 *buf++ = '0';
18578 *buf++ = static_cast<char>('0' + k);
18579 }
18580 else if (k < 100)
18581 {
18582 *buf++ = static_cast<char>('0' + (k / 10));
18583 k %= 10;
18584 *buf++ = static_cast<char>('0' + k);
18585 }
18586 else
18587 {
18588 *buf++ = static_cast<char>('0' + (k / 100));
18589 k %= 100;
18590 *buf++ = static_cast<char>('0' + (k / 10));
18591 k %= 10;
18592 *buf++ = static_cast<char>('0' + k);
18593 }
18594
18595 return buf;
18596 }
18597
18598 /*!
18599 @brief prettify v = buf * 10^decimal_exponent
18600
18601 If v is in the range [10^min_exp, 10^max_exp) it will be printed in fixed-point
18602 notation. Otherwise it will be printed in exponential notation.
18603
18604 @pre min_exp < 0
18605 @pre max_exp > 0
18606 */
18607 JSON_HEDLEY_NON_NULL(1)
18608 JSON_HEDLEY_RETURNS_NON_NULL
18609 inline char* format_buffer(char* buf, int len, int decimal_exponent,
18610 int min_exp, int max_exp)
18611 {
18612 JSON_ASSERT(min_exp < 0);
18613 JSON_ASSERT(max_exp > 0);
18614
18615 const int k = len;
18616 const int n = len + decimal_exponent;
18617
18618 // v = buf * 10^(n-k)
18619 // k is the length of the buffer (number of decimal digits)
18620 // n is the position of the decimal point relative to the start of the buffer.
18621
18622 if (k <= n && n <= max_exp)
18623 {
18624 // digits[000]
18625 // len <= max_exp + 2
18626
18627 std::memset(buf + k, '0', static_cast<size_t>(n) - static_cast<size_t>(k));
18628 // Make it look like a floating-point number (#362, #378)
18629 buf[n + 0] = '.';
18630 buf[n + 1] = '0';
18631 return buf + (static_cast<size_t>(n) + 2);
18632 }
18633
18634 if (0 < n && n <= max_exp)
18635 {
18636 // dig.its
18637 // len <= max_digits10 + 1
18638
18639 JSON_ASSERT(k > n);
18640
18641 std::memmove(buf + (static_cast<size_t>(n) + 1), buf + n, static_cast<size_t>(k) - static_cast<size_t>(n));
18642 buf[n] = '.';
18643 return buf + (static_cast<size_t>(k) + 1U);
18644 }
18645
18646 if (min_exp < n && n <= 0)
18647 {
18648 // 0.[000]digits
18649 // len <= 2 + (-min_exp - 1) + max_digits10
18650
18651 std::memmove(buf + (2 + static_cast<size_t>(-n)), buf, static_cast<size_t>(k));
18652 buf[0] = '0';
18653 buf[1] = '.';
18654 std::memset(buf + 2, '0', static_cast<size_t>(-n));
18655 return buf + (2U + static_cast<size_t>(-n) + static_cast<size_t>(k));
18656 }
18657
18658 if (k == 1)
18659 {
18660 // dE+123
18661 // len <= 1 + 5
18662
18663 buf += 1;
18664 }
18665 else
18666 {
18667 // d.igitsE+123
18668 // len <= max_digits10 + 1 + 5
18669
18670 std::memmove(buf + 2, buf + 1, static_cast<size_t>(k) - 1);
18671 buf[1] = '.';
18672 buf += 1 + static_cast<size_t>(k);
18673 }
18674
18675 *buf++ = 'e';
18676 return append_exponent(buf, n - 1);
18677 }
18678
18679 } // namespace dtoa_impl
18680
18681 /*!
18682 @brief generates a decimal representation of the floating-point number value in [first, last).
18683
18684 The format of the resulting decimal representation is similar to printf's %g
18685 format. Returns an iterator pointing past-the-end of the decimal representation.
18686
18687 @note The input number must be finite, i.e. NaN's and Inf's are not supported.
18688 @note The buffer must be large enough.
18689 @note The result is NOT null-terminated.
18690 */
18691 template<typename FloatType>
18692 JSON_HEDLEY_NON_NULL(1, 2)
18693 JSON_HEDLEY_RETURNS_NON_NULL
18694 char* to_chars(char* first, const char* last, FloatType value)
18695 {
18696 static_cast<void>(last); // maybe unused - fix warning
18697 JSON_ASSERT(std::isfinite(value));
18698
18699 // Use signbit(value) instead of (value < 0) since signbit works for -0.
18700 if (std::signbit(value))
18701 {
18702 value = -value;
18703 *first++ = '-';
18704 }
18705
18706 #ifdef __GNUC__
18707 #pragma GCC diagnostic push
18708 #pragma GCC diagnostic ignored "-Wfloat-equal"
18709 #endif
18710 if (value == 0) // +-0
18711 {
18712 *first++ = '0';
18713 // Make it look like a floating-point number (#362, #378)
18714 *first++ = '.';
18715 *first++ = '0';
18716 return first;
18717 }
18718 #ifdef __GNUC__
18719 #pragma GCC diagnostic pop
18720 #endif
18721
18722 JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10);
18723
18724 // Compute v = buffer * 10^decimal_exponent.
18725 // The decimal digits are stored in the buffer, which needs to be interpreted
18726 // as an unsigned decimal integer.
18727 // len is the length of the buffer, i.e. the number of decimal digits.
18728 int len = 0;
18729 int decimal_exponent = 0;
18730 dtoa_impl::grisu2(first, len, decimal_exponent, value);
18731
18732 JSON_ASSERT(len <= std::numeric_limits<FloatType>::max_digits10);
18733
18734 // Format the buffer like printf("%.*g", prec, value)
18735 constexpr int kMinExp = -4;
18736 // Use digits10 here to increase compatibility with version 2.
18737 constexpr int kMaxExp = std::numeric_limits<FloatType>::digits10;
18738
18739 JSON_ASSERT(last - first >= kMaxExp + 2);
18740 JSON_ASSERT(last - first >= 2 + (-kMinExp - 1) + std::numeric_limits<FloatType>::max_digits10);
18741 JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10 + 6);
18742
18743 return dtoa_impl::format_buffer(first, len, decimal_exponent, kMinExp, kMaxExp);
18744 }
18745
18746 } // namespace detail
18747 NLOHMANN_JSON_NAMESPACE_END
18748
18749 // #include <nlohmann/detail/exceptions.hpp>
18750
18751 // #include <nlohmann/detail/macro_scope.hpp>
18752
18753 // #include <nlohmann/detail/meta/cpp_future.hpp>
18754
18755 // #include <nlohmann/detail/output/binary_writer.hpp>
18756
18757 // #include <nlohmann/detail/output/output_adapters.hpp>
18758
18759 // #include <nlohmann/detail/string_concat.hpp>
18760
18761 // #include <nlohmann/detail/value_t.hpp>
18762
18763
18764 NLOHMANN_JSON_NAMESPACE_BEGIN
18765 namespace detail
18766 {
18767
18768 ///////////////////
18769 // serialization //
18770 ///////////////////
18771
18772 /// how to treat decoding errors
18773 enum class error_handler_t
18774 {
18775 strict, ///< throw a type_error exception in case of invalid UTF-8
18776 replace, ///< replace invalid UTF-8 sequences with U+FFFD
18777 ignore ///< ignore invalid UTF-8 sequences
18778 };
18779
18780 template<typename BasicJsonType>
18781 class serializer
18782 {
18783 using string_t = typename BasicJsonType::string_t;
18784 using number_float_t = typename BasicJsonType::number_float_t;
18785 using number_integer_t = typename BasicJsonType::number_integer_t;
18786 using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
18787 using binary_char_t = typename BasicJsonType::binary_t::value_type;
18788 static constexpr std::uint8_t UTF8_ACCEPT = 0;
18789 static constexpr std::uint8_t UTF8_REJECT = 1;
18790
18791 public:
18792 /*!
18793 @param[in] s output stream to serialize to
18794 @param[in] ichar indentation character to use
18795 @param[in] error_handler_ how to react on decoding errors
18796 */
18797 serializer(output_adapter_t<char> s, const char ichar,
18798 error_handler_t error_handler_ = error_handler_t::strict)
18799 : o(std::move(s))
18800 , loc(std::localeconv())
18801 , thousands_sep(loc->thousands_sep == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->thousands_sep)))
18802 , decimal_point(loc->decimal_point == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->decimal_point)))
18803 , indent_char(ichar)
18804 , indent_string(512, indent_char)
18805 , error_handler(error_handler_)
18806 {}
18807
18808 // delete because of pointer members
18809 serializer(const serializer&) = delete;
18810 serializer& operator=(const serializer&) = delete;
18811 serializer(serializer&&) = delete;
18812 serializer& operator=(serializer&&) = delete;
18813 ~serializer() = default;
18814
18815 /*!
18816 @brief internal implementation of the serialization function
18817
18818 This function is called by the public member function dump and organizes
18819 the serialization internally. The indentation level is propagated as
18820 additional parameter. In case of arrays and objects, the function is
18821 called recursively.
18822
18823 - strings and object keys are escaped using `escape_string()`
18824 - integer numbers are converted implicitly via `operator<<`
18825 - floating-point numbers are converted to a string using `"%g"` format
18826 - binary values are serialized as objects containing the subtype and the
18827 byte array
18828
18829 @param[in] val value to serialize
18830 @param[in] pretty_print whether the output shall be pretty-printed
18831 @param[in] ensure_ascii If @a ensure_ascii is true, all non-ASCII characters
18832 in the output are escaped with `\uXXXX` sequences, and the result consists
18833 of ASCII characters only.
18834 @param[in] indent_step the indent level
18835 @param[in] current_indent the current indent level (only used internally)
18836 */
18837 void dump(const BasicJsonType& val,
18838 const bool pretty_print,
18839 const bool ensure_ascii,
18840 const unsigned int indent_step,
18841 const unsigned int current_indent = 0)
18842 {
18843 switch (val.m_data.m_type)
18844 {
18845 case value_t::object:
18846 {
18847 if (val.m_data.m_value.object->empty())
18848 {
18849 o->write_characters("{}", 2);
18850 return;
18851 }
18852
18853 if (pretty_print)
18854 {
18855 o->write_characters("{\n", 2);
18856
18857 // variable to hold indentation for recursive calls
18858 const auto new_indent = current_indent + indent_step;
18859 if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
18860 {
18861 indent_string.resize(indent_string.size() * 2, ' ');
18862 }
18863
18864 // first n-1 elements
18865 auto i = val.m_data.m_value.object->cbegin();
18866 for (std::size_t cnt = 0; cnt < val.m_data.m_value.object->size() - 1; ++cnt, ++i)
18867 {
18868 o->write_characters(indent_string.c_str(), new_indent);
18869 o->write_character('\"');
18870 dump_escaped(i->first, ensure_ascii);
18871 o->write_characters("\": ", 3);
18872 dump(i->second, true, ensure_ascii, indent_step, new_indent);
18873 o->write_characters(",\n", 2);
18874 }
18875
18876 // last element
18877 JSON_ASSERT(i != val.m_data.m_value.object->cend());
18878 JSON_ASSERT(std::next(i) == val.m_data.m_value.object->cend());
18879 o->write_characters(indent_string.c_str(), new_indent);
18880 o->write_character('\"');
18881 dump_escaped(i->first, ensure_ascii);
18882 o->write_characters("\": ", 3);
18883 dump(i->second, true, ensure_ascii, indent_step, new_indent);
18884
18885 o->write_character('\n');
18886 o->write_characters(indent_string.c_str(), current_indent);
18887 o->write_character('}');
18888 }
18889 else
18890 {
18891 o->write_character('{');
18892
18893 // first n-1 elements
18894 auto i = val.m_data.m_value.object->cbegin();
18895 for (std::size_t cnt = 0; cnt < val.m_data.m_value.object->size() - 1; ++cnt, ++i)
18896 {
18897 o->write_character('\"');
18898 dump_escaped(i->first, ensure_ascii);
18899 o->write_characters("\":", 2);
18900 dump(i->second, false, ensure_ascii, indent_step, current_indent);
18901 o->write_character(',');
18902 }
18903
18904 // last element
18905 JSON_ASSERT(i != val.m_data.m_value.object->cend());
18906 JSON_ASSERT(std::next(i) == val.m_data.m_value.object->cend());
18907 o->write_character('\"');
18908 dump_escaped(i->first, ensure_ascii);
18909 o->write_characters("\":", 2);
18910 dump(i->second, false, ensure_ascii, indent_step, current_indent);
18911
18912 o->write_character('}');
18913 }
18914
18915 return;
18916 }
18917
18918 case value_t::array:
18919 {
18920 if (val.m_data.m_value.array->empty())
18921 {
18922 o->write_characters("[]", 2);
18923 return;
18924 }
18925
18926 if (pretty_print)
18927 {
18928 o->write_characters("[\n", 2);
18929
18930 // variable to hold indentation for recursive calls
18931 const auto new_indent = current_indent + indent_step;
18932 if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
18933 {
18934 indent_string.resize(indent_string.size() * 2, ' ');
18935 }
18936
18937 // first n-1 elements
18938 for (auto i = val.m_data.m_value.array->cbegin();
18939 i != val.m_data.m_value.array->cend() - 1; ++i)
18940 {
18941 o->write_characters(indent_string.c_str(), new_indent);
18942 dump(*i, true, ensure_ascii, indent_step, new_indent);
18943 o->write_characters(",\n", 2);
18944 }
18945
18946 // last element
18947 JSON_ASSERT(!val.m_data.m_value.array->empty());
18948 o->write_characters(indent_string.c_str(), new_indent);
18949 dump(val.m_data.m_value.array->back(), true, ensure_ascii, indent_step, new_indent);
18950
18951 o->write_character('\n');
18952 o->write_characters(indent_string.c_str(), current_indent);
18953 o->write_character(']');
18954 }
18955 else
18956 {
18957 o->write_character('[');
18958
18959 // first n-1 elements
18960 for (auto i = val.m_data.m_value.array->cbegin();
18961 i != val.m_data.m_value.array->cend() - 1; ++i)
18962 {
18963 dump(*i, false, ensure_ascii, indent_step, current_indent);
18964 o->write_character(',');
18965 }
18966
18967 // last element
18968 JSON_ASSERT(!val.m_data.m_value.array->empty());
18969 dump(val.m_data.m_value.array->back(), false, ensure_ascii, indent_step, current_indent);
18970
18971 o->write_character(']');
18972 }
18973
18974 return;
18975 }
18976
18977 case value_t::string:
18978 {
18979 o->write_character('\"');
18980 dump_escaped(*val.m_data.m_value.string, ensure_ascii);
18981 o->write_character('\"');
18982 return;
18983 }
18984
18985 case value_t::binary:
18986 {
18987 if (pretty_print)
18988 {
18989 o->write_characters("{\n", 2);
18990
18991 // variable to hold indentation for recursive calls
18992 const auto new_indent = current_indent + indent_step;
18993 if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
18994 {
18995 indent_string.resize(indent_string.size() * 2, ' ');
18996 }
18997
18998 o->write_characters(indent_string.c_str(), new_indent);
18999
19000 o->write_characters("\"bytes\": [", 10);
19001
19002 if (!val.m_data.m_value.binary->empty())
19003 {
19004 for (auto i = val.m_data.m_value.binary->cbegin();
19005 i != val.m_data.m_value.binary->cend() - 1; ++i)
19006 {
19007 dump_integer(*i);
19008 o->write_characters(", ", 2);
19009 }
19010 dump_integer(val.m_data.m_value.binary->back());
19011 }
19012
19013 o->write_characters("],\n", 3);
19014 o->write_characters(indent_string.c_str(), new_indent);
19015
19016 o->write_characters("\"subtype\": ", 11);
19017 if (val.m_data.m_value.binary->has_subtype())
19018 {
19019 dump_integer(val.m_data.m_value.binary->subtype());
19020 }
19021 else
19022 {
19023 o->write_characters("null", 4);
19024 }
19025 o->write_character('\n');
19026 o->write_characters(indent_string.c_str(), current_indent);
19027 o->write_character('}');
19028 }
19029 else
19030 {
19031 o->write_characters("{\"bytes\":[", 10);
19032
19033 if (!val.m_data.m_value.binary->empty())
19034 {
19035 for (auto i = val.m_data.m_value.binary->cbegin();
19036 i != val.m_data.m_value.binary->cend() - 1; ++i)
19037 {
19038 dump_integer(*i);
19039 o->write_character(',');
19040 }
19041 dump_integer(val.m_data.m_value.binary->back());
19042 }
19043
19044 o->write_characters("],\"subtype\":", 12);
19045 if (val.m_data.m_value.binary->has_subtype())
19046 {
19047 dump_integer(val.m_data.m_value.binary->subtype());
19048 o->write_character('}');
19049 }
19050 else
19051 {
19052 o->write_characters("null}", 5);
19053 }
19054 }
19055 return;
19056 }
19057
19058 case value_t::boolean:
19059 {
19060 if (val.m_data.m_value.boolean)
19061 {
19062 o->write_characters("true", 4);
19063 }
19064 else
19065 {
19066 o->write_characters("false", 5);
19067 }
19068 return;
19069 }
19070
19071 case value_t::number_integer:
19072 {
19073 dump_integer(val.m_data.m_value.number_integer);
19074 return;
19075 }
19076
19077 case value_t::number_unsigned:
19078 {
19079 dump_integer(val.m_data.m_value.number_unsigned);
19080 return;
19081 }
19082
19083 case value_t::number_float:
19084 {
19085 dump_float(val.m_data.m_value.number_float);
19086 return;
19087 }
19088
19089 case value_t::discarded:
19090 {
19091 o->write_characters("<discarded>", 11);
19092 return;
19093 }
19094
19095 case value_t::null:
19096 {
19097 o->write_characters("null", 4);
19098 return;
19099 }
19100
19101 default: // LCOV_EXCL_LINE
19102 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
19103 }
19104 }
19105
19106 JSON_PRIVATE_UNLESS_TESTED:
19107 /*!
19108 @brief dump escaped string
19109
19110 Escape a string by replacing certain special characters by a sequence of an
19111 escape character (backslash) and another character and other control
19112 characters by a sequence of "\u" followed by a four-digit hex
19113 representation. The escaped string is written to output stream @a o.
19114
19115 @param[in] s the string to escape
19116 @param[in] ensure_ascii whether to escape non-ASCII characters with
19117 \uXXXX sequences
19118
19119 @complexity Linear in the length of string @a s.
19120 */
19121 void dump_escaped(const string_t& s, const bool ensure_ascii)
19122 {
19123 std::uint32_t codepoint{};
19124 std::uint8_t state = UTF8_ACCEPT;
19125 std::size_t bytes = 0; // number of bytes written to string_buffer
19126
19127 // number of bytes written at the point of the last valid byte
19128 std::size_t bytes_after_last_accept = 0;
19129 std::size_t undumped_chars = 0;
19130
19131 for (std::size_t i = 0; i < s.size(); ++i)
19132 {
19133 const auto byte = static_cast<std::uint8_t>(s[i]);
19134
19135 switch (decode(state, codepoint, byte))
19136 {
19137 case UTF8_ACCEPT: // decode found a new code point
19138 {
19139 switch (codepoint)
19140 {
19141 case 0x08: // backspace
19142 {
19143 string_buffer[bytes++] = '\\';
19144 string_buffer[bytes++] = 'b';
19145 break;
19146 }
19147
19148 case 0x09: // horizontal tab
19149 {
19150 string_buffer[bytes++] = '\\';
19151 string_buffer[bytes++] = 't';
19152 break;
19153 }
19154
19155 case 0x0A: // newline
19156 {
19157 string_buffer[bytes++] = '\\';
19158 string_buffer[bytes++] = 'n';
19159 break;
19160 }
19161
19162 case 0x0C: // formfeed
19163 {
19164 string_buffer[bytes++] = '\\';
19165 string_buffer[bytes++] = 'f';
19166 break;
19167 }
19168
19169 case 0x0D: // carriage return
19170 {
19171 string_buffer[bytes++] = '\\';
19172 string_buffer[bytes++] = 'r';
19173 break;
19174 }
19175
19176 case 0x22: // quotation mark
19177 {
19178 string_buffer[bytes++] = '\\';
19179 string_buffer[bytes++] = '\"';
19180 break;
19181 }
19182
19183 case 0x5C: // reverse solidus
19184 {
19185 string_buffer[bytes++] = '\\';
19186 string_buffer[bytes++] = '\\';
19187 break;
19188 }
19189
19190 default:
19191 {
19192 // escape control characters (0x00..0x1F) or, if
19193 // ensure_ascii parameter is used, non-ASCII characters
19194 if ((codepoint <= 0x1F) || (ensure_ascii && (codepoint >= 0x7F)))
19195 {
19196 if (codepoint <= 0xFFFF)
19197 {
19198 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
19199 static_cast<void>((std::snprintf)(string_buffer.data() + bytes, 7, "\\u%04x",
19200 static_cast<std::uint16_t>(codepoint)));
19201 bytes += 6;
19202 }
19203 else
19204 {
19205 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
19206 static_cast<void>((std::snprintf)(string_buffer.data() + bytes, 13, "\\u%04x\\u%04x",
19207 static_cast<std::uint16_t>(0xD7C0u + (codepoint >> 10u)),
19208 static_cast<std::uint16_t>(0xDC00u + (codepoint & 0x3FFu))));
19209 bytes += 12;
19210 }
19211 }
19212 else
19213 {
19214 // copy byte to buffer (all previous bytes
19215 // been copied have in default case above)
19216 string_buffer[bytes++] = s[i];
19217 }
19218 break;
19219 }
19220 }
19221
19222 // write buffer and reset index; there must be 13 bytes
19223 // left, as this is the maximal number of bytes to be
19224 // written ("\uxxxx\uxxxx\0") for one code point
19225 if (string_buffer.size() - bytes < 13)
19226 {
19227 o->write_characters(string_buffer.data(), bytes);
19228 bytes = 0;
19229 }
19230
19231 // remember the byte position of this accept
19232 bytes_after_last_accept = bytes;
19233 undumped_chars = 0;
19234 break;
19235 }
19236
19237 case UTF8_REJECT: // decode found invalid UTF-8 byte
19238 {
19239 switch (error_handler)
19240 {
19241 case error_handler_t::strict:
19242 {
19243 JSON_THROW(type_error::create(316, concat("invalid UTF-8 byte at index ", std::to_string(i), ": 0x", hex_bytes(byte | 0)), nullptr));
19244 }
19245
19246 case error_handler_t::ignore:
19247 case error_handler_t::replace:
19248 {
19249 // in case we saw this character the first time, we
19250 // would like to read it again, because the byte
19251 // may be OK for itself, but just not OK for the
19252 // previous sequence
19253 if (undumped_chars > 0)
19254 {
19255 --i;
19256 }
19257
19258 // reset length buffer to the last accepted index;
19259 // thus removing/ignoring the invalid characters
19260 bytes = bytes_after_last_accept;
19261
19262 if (error_handler == error_handler_t::replace)
19263 {
19264 // add a replacement character
19265 if (ensure_ascii)
19266 {
19267 string_buffer[bytes++] = '\\';
19268 string_buffer[bytes++] = 'u';
19269 string_buffer[bytes++] = 'f';
19270 string_buffer[bytes++] = 'f';
19271 string_buffer[bytes++] = 'f';
19272 string_buffer[bytes++] = 'd';
19273 }
19274 else
19275 {
19276 string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xEF');
19277 string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBF');
19278 string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBD');
19279 }
19280
19281 // write buffer and reset index; there must be 13 bytes
19282 // left, as this is the maximal number of bytes to be
19283 // written ("\uxxxx\uxxxx\0") for one code point
19284 if (string_buffer.size() - bytes < 13)
19285 {
19286 o->write_characters(string_buffer.data(), bytes);
19287 bytes = 0;
19288 }
19289
19290 bytes_after_last_accept = bytes;
19291 }
19292
19293 undumped_chars = 0;
19294
19295 // continue processing the string
19296 state = UTF8_ACCEPT;
19297 break;
19298 }
19299
19300 default: // LCOV_EXCL_LINE
19301 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
19302 }
19303 break;
19304 }
19305
19306 default: // decode found yet incomplete multi-byte code point
19307 {
19308 if (!ensure_ascii)
19309 {
19310 // code point will not be escaped - copy byte to buffer
19311 string_buffer[bytes++] = s[i];
19312 }
19313 ++undumped_chars;
19314 break;
19315 }
19316 }
19317 }
19318
19319 // we finished processing the string
19320 if (JSON_HEDLEY_LIKELY(state == UTF8_ACCEPT))
19321 {
19322 // write buffer
19323 if (bytes > 0)
19324 {
19325 o->write_characters(string_buffer.data(), bytes);
19326 }
19327 }
19328 else
19329 {
19330 // we finish reading, but do not accept: string was incomplete
19331 switch (error_handler)
19332 {
19333 case error_handler_t::strict:
19334 {
19335 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));
19336 }
19337
19338 case error_handler_t::ignore:
19339 {
19340 // write all accepted bytes
19341 o->write_characters(string_buffer.data(), bytes_after_last_accept);
19342 break;
19343 }
19344
19345 case error_handler_t::replace:
19346 {
19347 // write all accepted bytes
19348 o->write_characters(string_buffer.data(), bytes_after_last_accept);
19349 // add a replacement character
19350 if (ensure_ascii)
19351 {
19352 o->write_characters("\\ufffd", 6);
19353 }
19354 else
19355 {
19356 o->write_characters("\xEF\xBF\xBD", 3);
19357 }
19358 break;
19359 }
19360
19361 default: // LCOV_EXCL_LINE
19362 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
19363 }
19364 }
19365 }
19366
19367 private:
19368 /*!
19369 @brief count digits
19370
19371 Count the number of decimal (base 10) digits for an input unsigned integer.
19372
19373 @param[in] x unsigned integer number to count its digits
19374 @return number of decimal digits
19375 */
19376 unsigned int count_digits(number_unsigned_t x) noexcept
19377 {
19378 unsigned int n_digits = 1;
19379 for (;;)
19380 {
19381 if (x < 10)
19382 {
19383 return n_digits;
19384 }
19385 if (x < 100)
19386 {
19387 return n_digits + 1;
19388 }
19389 if (x < 1000)
19390 {
19391 return n_digits + 2;
19392 }
19393 if (x < 10000)
19394 {
19395 return n_digits + 3;
19396 }
19397 x = x / 10000u;
19398 n_digits += 4;
19399 }
19400 }
19401
19402 /*!
19403 * @brief convert a byte to a uppercase hex representation
19404 * @param[in] byte byte to represent
19405 * @return representation ("00".."FF")
19406 */
19407 static std::string hex_bytes(std::uint8_t byte)
19408 {
19409 std::string result = "FF";
19410 constexpr const char* nibble_to_hex = "0123456789ABCDEF";
19411 result[0] = nibble_to_hex[byte / 16];
19412 result[1] = nibble_to_hex[byte % 16];
19413 return result;
19414 }
19415
19416 // templates to avoid warnings about useless casts
19417 template <typename NumberType, enable_if_t<std::is_signed<NumberType>::value, int> = 0>
19418 bool is_negative_number(NumberType x)
19419 {
19420 return x < 0;
19421 }
19422
19423 template < typename NumberType, enable_if_t <std::is_unsigned<NumberType>::value, int > = 0 >
19424 bool is_negative_number(NumberType /*unused*/)
19425 {
19426 return false;
19427 }
19428
19429 /*!
19430 @brief dump an integer
19431
19432 Dump a given integer to output stream @a o. Works internally with
19433 @a number_buffer.
19434
19435 @param[in] x integer number (signed or unsigned) to dump
19436 @tparam NumberType either @a number_integer_t or @a number_unsigned_t
19437 */
19438 template < typename NumberType, detail::enable_if_t <
19439 std::is_integral<NumberType>::value ||
19440 std::is_same<NumberType, number_unsigned_t>::value ||
19441 std::is_same<NumberType, number_integer_t>::value ||
19442 std::is_same<NumberType, binary_char_t>::value,
19443 int > = 0 >
19444 void dump_integer(NumberType x)
19445 {
19446 static constexpr std::array<std::array<char, 2>, 100> digits_to_99
19447 {
19448 {
19449 {{'0', '0'}}, {{'0', '1'}}, {{'0', '2'}}, {{'0', '3'}}, {{'0', '4'}}, {{'0', '5'}}, {{'0', '6'}}, {{'0', '7'}}, {{'0', '8'}}, {{'0', '9'}},
19450 {{'1', '0'}}, {{'1', '1'}}, {{'1', '2'}}, {{'1', '3'}}, {{'1', '4'}}, {{'1', '5'}}, {{'1', '6'}}, {{'1', '7'}}, {{'1', '8'}}, {{'1', '9'}},
19451 {{'2', '0'}}, {{'2', '1'}}, {{'2', '2'}}, {{'2', '3'}}, {{'2', '4'}}, {{'2', '5'}}, {{'2', '6'}}, {{'2', '7'}}, {{'2', '8'}}, {{'2', '9'}},
19452 {{'3', '0'}}, {{'3', '1'}}, {{'3', '2'}}, {{'3', '3'}}, {{'3', '4'}}, {{'3', '5'}}, {{'3', '6'}}, {{'3', '7'}}, {{'3', '8'}}, {{'3', '9'}},
19453 {{'4', '0'}}, {{'4', '1'}}, {{'4', '2'}}, {{'4', '3'}}, {{'4', '4'}}, {{'4', '5'}}, {{'4', '6'}}, {{'4', '7'}}, {{'4', '8'}}, {{'4', '9'}},
19454 {{'5', '0'}}, {{'5', '1'}}, {{'5', '2'}}, {{'5', '3'}}, {{'5', '4'}}, {{'5', '5'}}, {{'5', '6'}}, {{'5', '7'}}, {{'5', '8'}}, {{'5', '9'}},
19455 {{'6', '0'}}, {{'6', '1'}}, {{'6', '2'}}, {{'6', '3'}}, {{'6', '4'}}, {{'6', '5'}}, {{'6', '6'}}, {{'6', '7'}}, {{'6', '8'}}, {{'6', '9'}},
19456 {{'7', '0'}}, {{'7', '1'}}, {{'7', '2'}}, {{'7', '3'}}, {{'7', '4'}}, {{'7', '5'}}, {{'7', '6'}}, {{'7', '7'}}, {{'7', '8'}}, {{'7', '9'}},
19457 {{'8', '0'}}, {{'8', '1'}}, {{'8', '2'}}, {{'8', '3'}}, {{'8', '4'}}, {{'8', '5'}}, {{'8', '6'}}, {{'8', '7'}}, {{'8', '8'}}, {{'8', '9'}},
19458 {{'9', '0'}}, {{'9', '1'}}, {{'9', '2'}}, {{'9', '3'}}, {{'9', '4'}}, {{'9', '5'}}, {{'9', '6'}}, {{'9', '7'}}, {{'9', '8'}}, {{'9', '9'}},
19459 }
19460 };
19461
19462 // special case for "0"
19463 if (x == 0)
19464 {
19465 o->write_character('0');
19466 return;
19467 }
19468
19469 // use a pointer to fill the buffer
19470 auto buffer_ptr = number_buffer.begin(); // NOLINT(llvm-qualified-auto,readability-qualified-auto,cppcoreguidelines-pro-type-vararg,hicpp-vararg)
19471
19472 number_unsigned_t abs_value;
19473
19474 unsigned int n_chars{};
19475
19476 if (is_negative_number(x))
19477 {
19478 *buffer_ptr = '-';
19479 abs_value = remove_sign(static_cast<number_integer_t>(x));
19480
19481 // account one more byte for the minus sign
19482 n_chars = 1 + count_digits(abs_value);
19483 }
19484 else
19485 {
19486 abs_value = static_cast<number_unsigned_t>(x);
19487 n_chars = count_digits(abs_value);
19488 }
19489
19490 // spare 1 byte for '\0'
19491 JSON_ASSERT(n_chars < number_buffer.size() - 1);
19492
19493 // jump to the end to generate the string from backward,
19494 // so we later avoid reversing the result
19495 buffer_ptr += n_chars;
19496
19497 // Fast int2ascii implementation inspired by "Fastware" talk by Andrei Alexandrescu
19498 // See: https://www.youtube.com/watch?v=o4-CwDo2zpg
19499 while (abs_value >= 100)
19500 {
19501 const auto digits_index = static_cast<unsigned>((abs_value % 100));
19502 abs_value /= 100;
19503 *(--buffer_ptr) = digits_to_99[digits_index][1];
19504 *(--buffer_ptr) = digits_to_99[digits_index][0];
19505 }
19506
19507 if (abs_value >= 10)
19508 {
19509 const auto digits_index = static_cast<unsigned>(abs_value);
19510 *(--buffer_ptr) = digits_to_99[digits_index][1];
19511 *(--buffer_ptr) = digits_to_99[digits_index][0];
19512 }
19513 else
19514 {
19515 *(--buffer_ptr) = static_cast<char>('0' + abs_value);
19516 }
19517
19518 o->write_characters(number_buffer.data(), n_chars);
19519 }
19520
19521 /*!
19522 @brief dump a floating-point number
19523
19524 Dump a given floating-point number to output stream @a o. Works internally
19525 with @a number_buffer.
19526
19527 @param[in] x floating-point number to dump
19528 */
19529 void dump_float(number_float_t x)
19530 {
19531 // NaN / inf
19532 if (!std::isfinite(x))
19533 {
19534 o->write_characters("null", 4);
19535 return;
19536 }
19537
19538 // If number_float_t is an IEEE-754 single or double precision number,
19539 // use the Grisu2 algorithm to produce short numbers which are
19540 // guaranteed to round-trip, using strtof and strtod, resp.
19541 //
19542 // NB: The test below works if <long double> == <double>.
19543 static constexpr bool is_ieee_single_or_double
19544 = (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) ||
19545 (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);
19546
19547 dump_float(x, std::integral_constant<bool, is_ieee_single_or_double>());
19548 }
19549
19550 void dump_float(number_float_t x, std::true_type /*is_ieee_single_or_double*/)
19551 {
19552 auto* begin = number_buffer.data();
19553 auto* end = ::nlohmann::detail::to_chars(begin, begin + number_buffer.size(), x);
19554
19555 o->write_characters(begin, static_cast<size_t>(end - begin));
19556 }
19557
19558 void dump_float(number_float_t x, std::false_type /*is_ieee_single_or_double*/)
19559 {
19560 // get number of digits for a float -> text -> float round-trip
19561 static constexpr auto d = std::numeric_limits<number_float_t>::max_digits10;
19562
19563 // the actual conversion
19564 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
19565 std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*g", d, x);
19566
19567 // negative value indicates an error
19568 JSON_ASSERT(len > 0);
19569 // check if buffer was large enough
19570 JSON_ASSERT(static_cast<std::size_t>(len) < number_buffer.size());
19571
19572 // erase thousands separator
19573 if (thousands_sep != '\0')
19574 {
19575 // NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::remove returns an iterator, see https://github.com/nlohmann/json/issues/3081
19576 const auto end = std::remove(number_buffer.begin(), number_buffer.begin() + len, thousands_sep);
19577 std::fill(end, number_buffer.end(), '\0');
19578 JSON_ASSERT((end - number_buffer.begin()) <= len);
19579 len = (end - number_buffer.begin());
19580 }
19581
19582 // convert decimal point to '.'
19583 if (decimal_point != '\0' && decimal_point != '.')
19584 {
19585 // NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::find returns an iterator, see https://github.com/nlohmann/json/issues/3081
19586 const auto dec_pos = std::find(number_buffer.begin(), number_buffer.end(), decimal_point);
19587 if (dec_pos != number_buffer.end())
19588 {
19589 *dec_pos = '.';
19590 }
19591 }
19592
19593 o->write_characters(number_buffer.data(), static_cast<std::size_t>(len));
19594
19595 // determine if we need to append ".0"
19596 const bool value_is_int_like =
19597 std::none_of(number_buffer.begin(), number_buffer.begin() + len + 1,
19598 [](char c)
19599 {
19600 return c == '.' || c == 'e';
19601 });
19602
19603 if (value_is_int_like)
19604 {
19605 o->write_characters(".0", 2);
19606 }
19607 }
19608
19609 /*!
19610 @brief check whether a string is UTF-8 encoded
19611
19612 The function checks each byte of a string whether it is UTF-8 encoded. The
19613 result of the check is stored in the @a state parameter. The function must
19614 be called initially with state 0 (accept). State 1 means the string must
19615 be rejected, because the current byte is not allowed. If the string is
19616 completely processed, but the state is non-zero, the string ended
19617 prematurely; that is, the last byte indicated more bytes should have
19618 followed.
19619
19620 @param[in,out] state the state of the decoding
19621 @param[in,out] codep codepoint (valid only if resulting state is UTF8_ACCEPT)
19622 @param[in] byte next byte to decode
19623 @return new state
19624
19625 @note The function has been edited: a std::array is used.
19626
19627 @copyright Copyright (c) 2008-2009 Bjoern Hoehrmann <[email protected]>
19628 @sa http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
19629 */
19630 static std::uint8_t decode(std::uint8_t& state, std::uint32_t& codep, const std::uint8_t byte) noexcept
19631 {
19632 static const std::array<std::uint8_t, 400> utf8d =
19633 {
19634 {
19635 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
19636 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
19637 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
19638 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
19639 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
19640 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
19641 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
19642 0xA, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3, // E0..EF
19643 0xB, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, // F0..FF
19644 0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, 0x6, 0x1, 0x1, 0x1, 0x1, // s0..s0
19645 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
19646 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
19647 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
19648 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
19649 }
19650 };
19651
19652 JSON_ASSERT(byte < utf8d.size());
19653 const std::uint8_t type = utf8d[byte];
19654
19655 codep = (state != UTF8_ACCEPT)
19656 ? (byte & 0x3fu) | (codep << 6u)
19657 : (0xFFu >> type) & (byte);
19658
19659 const std::size_t index = 256u + (static_cast<size_t>(state) * 16u) + static_cast<size_t>(type);
19660 JSON_ASSERT(index < utf8d.size());
19661 state = utf8d[index];
19662 return state;
19663 }
19664
19665 /*
19666 * Overload to make the compiler happy while it is instantiating
19667 * dump_integer for number_unsigned_t.
19668 * Must never be called.
19669 */
19670 number_unsigned_t remove_sign(number_unsigned_t x)
19671 {
19672 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
19673 return x; // LCOV_EXCL_LINE
19674 }
19675
19676 /*
19677 * Helper function for dump_integer
19678 *
19679 * This function takes a negative signed integer and returns its absolute
19680 * value as unsigned integer. The plus/minus shuffling is necessary as we can
19681 * not directly remove the sign of an arbitrary signed integer as the
19682 * absolute values of INT_MIN and INT_MAX are usually not the same. See
19683 * #1708 for details.
19684 */
19685 number_unsigned_t remove_sign(number_integer_t x) noexcept
19686 {
19687 JSON_ASSERT(x < 0 && x < (std::numeric_limits<number_integer_t>::max)()); // NOLINT(misc-redundant-expression)
19688 return static_cast<number_unsigned_t>(-(x + 1)) + 1;
19689 }
19690
19691 private:
19692 /// the output of the serializer
19693 output_adapter_t<char> o = nullptr;
19694
19695 /// a (hopefully) large enough character buffer
19696 std::array<char, 64> number_buffer{{}};
19697
19698 /// the locale
19699 const std::lconv* loc = nullptr;
19700 /// the locale's thousand separator character
19701 const char thousands_sep = '\0';
19702 /// the locale's decimal point character
19703 const char decimal_point = '\0';
19704
19705 /// string buffer
19706 std::array<char, 512> string_buffer{{}};
19707
19708 /// the indentation character
19709 const char indent_char;
19710 /// the indentation string
19711 string_t indent_string;
19712
19713 /// error_handler how to react on decoding errors
19714 const error_handler_t error_handler;
19715 };
19716
19717 } // namespace detail
19718 NLOHMANN_JSON_NAMESPACE_END
19719
19720 // #include <nlohmann/detail/value_t.hpp>
19721
19722 // #include <nlohmann/json_fwd.hpp>
19723
19724 // #include <nlohmann/ordered_map.hpp>
19725 // __ _____ _____ _____
19726 // __| | __| | | | JSON for Modern C++
19727 // | | |__ | | | | | | version 3.12.0
19728 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
19729 //
19730 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
19731 // SPDX-License-Identifier: MIT
19732
19733
19734
19735 #include <functional> // equal_to, less
19736 #include <initializer_list> // initializer_list
19737 #include <iterator> // input_iterator_tag, iterator_traits
19738 #include <memory> // allocator
19739 #include <stdexcept> // for out_of_range
19740 #include <type_traits> // enable_if, is_convertible
19741 #include <utility> // pair
19742 #include <vector> // vector
19743
19744 // #include <nlohmann/detail/macro_scope.hpp>
19745
19746 // #include <nlohmann/detail/meta/type_traits.hpp>
19747
19748
19749 NLOHMANN_JSON_NAMESPACE_BEGIN
19750
19751 /// ordered_map: a minimal map-like container that preserves insertion order
19752 /// for use within nlohmann::basic_json<ordered_map>
19753 template <class Key, class T, class IgnoredLess = std::less<Key>,
19754 class Allocator = std::allocator<std::pair<const Key, T>>>
19755 struct ordered_map : std::vector<std::pair<const Key, T>, Allocator>
19756 {
19757 using key_type = Key;
19758 using mapped_type = T;
19759 using Container = std::vector<std::pair<const Key, T>, Allocator>;
19760 using iterator = typename Container::iterator;
19761 using const_iterator = typename Container::const_iterator;
19762 using size_type = typename Container::size_type;
19763 using value_type = typename Container::value_type;
19764 #ifdef JSON_HAS_CPP_14
19765 using key_compare = std::equal_to<>;
19766 #else
19767 using key_compare = std::equal_to<Key>;
19768 #endif
19769
19770 // Explicit constructors instead of `using Container::Container`
19771 // otherwise older compilers choke on it (GCC <= 5.5, xcode <= 9.4)
19772 ordered_map() noexcept(noexcept(Container())) : Container{} {}
19773 explicit ordered_map(const Allocator& alloc) noexcept(noexcept(Container(alloc))) : Container{alloc} {}
19774 template <class It>
19775 ordered_map(It first, It last, const Allocator& alloc = Allocator())
19776 : Container{first, last, alloc} {}
19777 ordered_map(std::initializer_list<value_type> init, const Allocator& alloc = Allocator() )
19778 : Container{init, alloc} {}
19779
19780 std::pair<iterator, bool> emplace(const key_type& key, T&& t)
19781 {
19782 for (auto it = this->begin(); it != this->end(); ++it)
19783 {
19784 if (m_compare(it->first, key))
19785 {
19786 return {it, false};
19787 }
19788 }
19789 Container::emplace_back(key, std::forward<T>(t));
19790 return {std::prev(this->end()), true};
19791 }
19792
19793 template<class KeyType, detail::enable_if_t<
19794 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
19795 std::pair<iterator, bool> emplace(KeyType && key, T && t)
19796 {
19797 for (auto it = this->begin(); it != this->end(); ++it)
19798 {
19799 if (m_compare(it->first, key))
19800 {
19801 return {it, false};
19802 }
19803 }
19804 Container::emplace_back(std::forward<KeyType>(key), std::forward<T>(t));
19805 return {std::prev(this->end()), true};
19806 }
19807
19808 T& operator[](const key_type& key)
19809 {
19810 return emplace(key, T{}).first->second;
19811 }
19812
19813 template<class KeyType, detail::enable_if_t<
19814 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
19815 T & operator[](KeyType && key)
19816 {
19817 return emplace(std::forward<KeyType>(key), T{}).first->second;
19818 }
19819
19820 const T& operator[](const key_type& key) const
19821 {
19822 return at(key);
19823 }
19824
19825 template<class KeyType, detail::enable_if_t<
19826 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
19827 const T & operator[](KeyType && key) const
19828 {
19829 return at(std::forward<KeyType>(key));
19830 }
19831
19832 T& at(const key_type& key)
19833 {
19834 for (auto it = this->begin(); it != this->end(); ++it)
19835 {
19836 if (m_compare(it->first, key))
19837 {
19838 return it->second;
19839 }
19840 }
19841
19842 JSON_THROW(std::out_of_range("key not found"));
19843 }
19844
19845 template<class KeyType, detail::enable_if_t<
19846 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
19847 T & at(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
19848 {
19849 for (auto it = this->begin(); it != this->end(); ++it)
19850 {
19851 if (m_compare(it->first, key))
19852 {
19853 return it->second;
19854 }
19855 }
19856
19857 JSON_THROW(std::out_of_range("key not found"));
19858 }
19859
19860 const T& at(const key_type& key) const
19861 {
19862 for (auto it = this->begin(); it != this->end(); ++it)
19863 {
19864 if (m_compare(it->first, key))
19865 {
19866 return it->second;
19867 }
19868 }
19869
19870 JSON_THROW(std::out_of_range("key not found"));
19871 }
19872
19873 template<class KeyType, detail::enable_if_t<
19874 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
19875 const T & at(KeyType && key) const // NOLINT(cppcoreguidelines-missing-std-forward)
19876 {
19877 for (auto it = this->begin(); it != this->end(); ++it)
19878 {
19879 if (m_compare(it->first, key))
19880 {
19881 return it->second;
19882 }
19883 }
19884
19885 JSON_THROW(std::out_of_range("key not found"));
19886 }
19887
19888 size_type erase(const key_type& key)
19889 {
19890 for (auto it = this->begin(); it != this->end(); ++it)
19891 {
19892 if (m_compare(it->first, key))
19893 {
19894 // Since we cannot move const Keys, re-construct them in place
19895 for (auto next = it; ++next != this->end(); ++it)
19896 {
19897 it->~value_type(); // Destroy but keep allocation
19898 new (&*it) value_type{std::move(*next)};
19899 }
19900 Container::pop_back();
19901 return 1;
19902 }
19903 }
19904 return 0;
19905 }
19906
19907 template<class KeyType, detail::enable_if_t<
19908 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
19909 size_type erase(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
19910 {
19911 for (auto it = this->begin(); it != this->end(); ++it)
19912 {
19913 if (m_compare(it->first, key))
19914 {
19915 // Since we cannot move const Keys, re-construct them in place
19916 for (auto next = it; ++next != this->end(); ++it)
19917 {
19918 it->~value_type(); // Destroy but keep allocation
19919 new (&*it) value_type{std::move(*next)};
19920 }
19921 Container::pop_back();
19922 return 1;
19923 }
19924 }
19925 return 0;
19926 }
19927
19928 iterator erase(iterator pos)
19929 {
19930 return erase(pos, std::next(pos));
19931 }
19932
19933 iterator erase(iterator first, iterator last)
19934 {
19935 if (first == last)
19936 {
19937 return first;
19938 }
19939
19940 const auto elements_affected = std::distance(first, last);
19941 const auto offset = std::distance(Container::begin(), first);
19942
19943 // This is the start situation. We need to delete elements_affected
19944 // elements (3 in this example: e, f, g), and need to return an
19945 // iterator past the last deleted element (h in this example).
19946 // Note that offset is the distance from the start of the vector
19947 // to first. We will need this later.
19948
19949 // [ a, b, c, d, e, f, g, h, i, j ]
19950 // ^ ^
19951 // first last
19952
19953 // Since we cannot move const Keys, we re-construct them in place.
19954 // We start at first and re-construct (viz. copy) the elements from
19955 // the back of the vector. Example for first iteration:
19956
19957 // ,--------.
19958 // v | destroy e and re-construct with h
19959 // [ a, b, c, d, e, f, g, h, i, j ]
19960 // ^ ^
19961 // it it + elements_affected
19962
19963 for (auto it = first; std::next(it, elements_affected) != Container::end(); ++it)
19964 {
19965 it->~value_type(); // destroy but keep allocation
19966 new (&*it) value_type{std::move(*std::next(it, elements_affected))}; // "move" next element to it
19967 }
19968
19969 // [ a, b, c, d, h, i, j, h, i, j ]
19970 // ^ ^
19971 // first last
19972
19973 // remove the unneeded elements at the end of the vector
19974 Container::resize(this->size() - static_cast<size_type>(elements_affected));
19975
19976 // [ a, b, c, d, h, i, j ]
19977 // ^ ^
19978 // first last
19979
19980 // first is now pointing past the last deleted element, but we cannot
19981 // use this iterator, because it may have been invalidated by the
19982 // resize call. Instead, we can return begin() + offset.
19983 return Container::begin() + offset;
19984 }
19985
19986 size_type count(const key_type& key) const
19987 {
19988 for (auto it = this->begin(); it != this->end(); ++it)
19989 {
19990 if (m_compare(it->first, key))
19991 {
19992 return 1;
19993 }
19994 }
19995 return 0;
19996 }
19997
19998 template<class KeyType, detail::enable_if_t<
19999 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
20000 size_type count(KeyType && key) const // NOLINT(cppcoreguidelines-missing-std-forward)
20001 {
20002 for (auto it = this->begin(); it != this->end(); ++it)
20003 {
20004 if (m_compare(it->first, key))
20005 {
20006 return 1;
20007 }
20008 }
20009 return 0;
20010 }
20011
20012 iterator find(const key_type& key)
20013 {
20014 for (auto it = this->begin(); it != this->end(); ++it)
20015 {
20016 if (m_compare(it->first, key))
20017 {
20018 return it;
20019 }
20020 }
20021 return Container::end();
20022 }
20023
20024 template<class KeyType, detail::enable_if_t<
20025 detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
20026 iterator find(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward)
20027 {
20028 for (auto it = this->begin(); it != this->end(); ++it)
20029 {
20030 if (m_compare(it->first, key))
20031 {
20032 return it;
20033 }
20034 }
20035 return Container::end();
20036 }
20037
20038 const_iterator find(const key_type& key) const
20039 {
20040 for (auto it = this->begin(); it != this->end(); ++it)
20041 {
20042 if (m_compare(it->first, key))
20043 {
20044 return it;
20045 }
20046 }
20047 return Container::end();
20048 }
20049
20050 std::pair<iterator, bool> insert( value_type&& value )
20051 {
20052 return emplace(value.first, std::move(value.second));
20053 }
20054
20055 std::pair<iterator, bool> insert( const value_type& value )
20056 {
20057 for (auto it = this->begin(); it != this->end(); ++it)
20058 {
20059 if (m_compare(it->first, value.first))
20060 {
20061 return {it, false};
20062 }
20063 }
20064 Container::push_back(value);
20065 return {--this->end(), true};
20066 }
20067
20068 template<typename InputIt>
20069 using require_input_iter = typename std::enable_if<std::is_convertible<typename std::iterator_traits<InputIt>::iterator_category,
20070 std::input_iterator_tag>::value>::type;
20071
20072 template<typename InputIt, typename = require_input_iter<InputIt>>
20073 void insert(InputIt first, InputIt last)
20074 {
20075 for (auto it = first; it != last; ++it)
20076 {
20077 insert(*it);
20078 }
20079 }
20080
20081 private:
20082 JSON_NO_UNIQUE_ADDRESS key_compare m_compare = key_compare();
20083 };
20084
20085 NLOHMANN_JSON_NAMESPACE_END
20086
20087
20088 #if defined(JSON_HAS_CPP_17)
20089 #if JSON_HAS_STATIC_RTTI
20090 #include <any>
20091 #endif
20092 #include <string_view>
20093 #endif
20094
20095 /*!
20096 @brief namespace for Niels Lohmann
20097 @see https://github.com/nlohmann
20098 @since version 1.0.0
20099 */
20100 NLOHMANN_JSON_NAMESPACE_BEGIN
20101
20102 /*!
20103 @brief a class to store JSON values
20104
20105 @internal
20106 @invariant The member variables @a m_value and @a m_type have the following
20107 relationship:
20108 - If `m_type == value_t::object`, then `m_value.object != nullptr`.
20109 - If `m_type == value_t::array`, then `m_value.array != nullptr`.
20110 - If `m_type == value_t::string`, then `m_value.string != nullptr`.
20111 The invariants are checked by member function assert_invariant().
20112
20113 @note ObjectType trick from https://stackoverflow.com/a/9860911
20114 @endinternal
20115
20116 @since version 1.0.0
20117
20118 @nosubgrouping
20119 */
20120 NLOHMANN_BASIC_JSON_TPL_DECLARATION
20121 class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions)
20122 : public ::nlohmann::detail::json_base_class<CustomBaseClass>
20123 {
20124 private:
20125 template<detail::value_t> friend struct detail::external_constructor;
20126
20127 template<typename>
20128 friend class ::nlohmann::json_pointer;
20129 // can be restored when json_pointer backwards compatibility is removed
20130 // friend ::nlohmann::json_pointer<StringType>;
20131
20132 template<typename BasicJsonType, typename InputType>
20133 friend class ::nlohmann::detail::parser;
20134 friend ::nlohmann::detail::serializer<basic_json>;
20135 template<typename BasicJsonType>
20136 friend class ::nlohmann::detail::iter_impl;
20137 template<typename BasicJsonType, typename CharType>
20138 friend class ::nlohmann::detail::binary_writer;
20139 template<typename BasicJsonType, typename InputType, typename SAX>
20140 friend class ::nlohmann::detail::binary_reader;
20141 template<typename BasicJsonType, typename InputAdapterType>
20142 friend class ::nlohmann::detail::json_sax_dom_parser;
20143 template<typename BasicJsonType, typename InputAdapterType>
20144 friend class ::nlohmann::detail::json_sax_dom_callback_parser;
20145 friend class ::nlohmann::detail::exception;
20146
20147 /// workaround type for MSVC
20148 using basic_json_t = NLOHMANN_BASIC_JSON_TPL;
20149 using json_base_class_t = ::nlohmann::detail::json_base_class<CustomBaseClass>;
20150
20151 JSON_PRIVATE_UNLESS_TESTED:
20152 // convenience aliases for types residing in namespace detail;
20153 using lexer = ::nlohmann::detail::lexer_base<basic_json>;
20154
20155 template<typename InputAdapterType>
20156 static ::nlohmann::detail::parser<basic_json, InputAdapterType> parser(
20157 InputAdapterType adapter,
20158 detail::parser_callback_t<basic_json>cb = nullptr,
20159 const bool allow_exceptions = true,
20160 const bool ignore_comments = false
20161 )
20162 {
20163 return ::nlohmann::detail::parser<basic_json, InputAdapterType>(std::move(adapter),
20164 std::move(cb), allow_exceptions, ignore_comments);
20165 }
20166
20167 private:
20168 using primitive_iterator_t = ::nlohmann::detail::primitive_iterator_t;
20169 template<typename BasicJsonType>
20170 using internal_iterator = ::nlohmann::detail::internal_iterator<BasicJsonType>;
20171 template<typename BasicJsonType>
20172 using iter_impl = ::nlohmann::detail::iter_impl<BasicJsonType>;
20173 template<typename Iterator>
20174 using iteration_proxy = ::nlohmann::detail::iteration_proxy<Iterator>;
20175 template<typename Base> using json_reverse_iterator = ::nlohmann::detail::json_reverse_iterator<Base>;
20176
20177 template<typename CharType>
20178 using output_adapter_t = ::nlohmann::detail::output_adapter_t<CharType>;
20179
20180 template<typename InputType>
20181 using binary_reader = ::nlohmann::detail::binary_reader<basic_json, InputType>;
20182 template<typename CharType> using binary_writer = ::nlohmann::detail::binary_writer<basic_json, CharType>;
20183
20184 JSON_PRIVATE_UNLESS_TESTED:
20185 using serializer = ::nlohmann::detail::serializer<basic_json>;
20186
20187 public:
20188 using value_t = detail::value_t;
20189 /// JSON Pointer, see @ref nlohmann::json_pointer
20190 using json_pointer = ::nlohmann::json_pointer<StringType>;
20191 template<typename T, typename SFINAE>
20192 using json_serializer = JSONSerializer<T, SFINAE>;
20193 /// how to treat decoding errors
20194 using error_handler_t = detail::error_handler_t;
20195 /// how to treat CBOR tags
20196 using cbor_tag_handler_t = detail::cbor_tag_handler_t;
20197 /// how to encode BJData
20198 using bjdata_version_t = detail::bjdata_version_t;
20199 /// helper type for initializer lists of basic_json values
20200 using initializer_list_t = std::initializer_list<detail::json_ref<basic_json>>;
20201
20202 using input_format_t = detail::input_format_t;
20203 /// SAX interface type, see @ref nlohmann::json_sax
20204 using json_sax_t = json_sax<basic_json>;
20205
20206 ////////////////
20207 // exceptions //
20208 ////////////////
20209
20210 /// @name exceptions
20211 /// Classes to implement user-defined exceptions.
20212 /// @{
20213
20214 using exception = detail::exception;
20215 using parse_error = detail::parse_error;
20216 using invalid_iterator = detail::invalid_iterator;
20217 using type_error = detail::type_error;
20218 using out_of_range = detail::out_of_range;
20219 using other_error = detail::other_error;
20220
20221 /// @}
20222
20223 /////////////////////
20224 // container types //
20225 /////////////////////
20226
20227 /// @name container types
20228 /// The canonic container types to use @ref basic_json like any other STL
20229 /// container.
20230 /// @{
20231
20232 /// the type of elements in a basic_json container
20233 using value_type = basic_json;
20234
20235 /// the type of an element reference
20236 using reference = value_type&;
20237 /// the type of an element const reference
20238 using const_reference = const value_type&;
20239
20240 /// a type to represent differences between iterators
20241 using difference_type = std::ptrdiff_t;
20242 /// a type to represent container sizes
20243 using size_type = std::size_t;
20244
20245 /// the allocator type
20246 using allocator_type = AllocatorType<basic_json>;
20247
20248 /// the type of an element pointer
20249 using pointer = typename std::allocator_traits<allocator_type>::pointer;
20250 /// the type of an element const pointer
20251 using const_pointer = typename std::allocator_traits<allocator_type>::const_pointer;
20252
20253 /// an iterator for a basic_json container
20254 using iterator = iter_impl<basic_json>;
20255 /// a const iterator for a basic_json container
20256 using const_iterator = iter_impl<const basic_json>;
20257 /// a reverse iterator for a basic_json container
20258 using reverse_iterator = json_reverse_iterator<typename basic_json::iterator>;
20259 /// a const reverse iterator for a basic_json container
20260 using const_reverse_iterator = json_reverse_iterator<typename basic_json::const_iterator>;
20261
20262 /// @}
20263
20264 /// @brief returns the allocator associated with the container
20265 /// @sa https://json.nlohmann.me/api/basic_json/get_allocator/
20266 static allocator_type get_allocator()
20267 {
20268 return allocator_type();
20269 }
20270
20271 /// @brief returns version information on the library
20272 /// @sa https://json.nlohmann.me/api/basic_json/meta/
20273 JSON_HEDLEY_WARN_UNUSED_RESULT
20274 static basic_json meta()
20275 {
20276 basic_json result;
20277
20278 result["copyright"] = "(C) 2013-2025 Niels Lohmann";
20279 result["name"] = "JSON for Modern C++";
20280 result["url"] = "https://github.com/nlohmann/json";
20281 result["version"]["string"] =
20282 detail::concat(std::to_string(NLOHMANN_JSON_VERSION_MAJOR), '.',
20283 std::to_string(NLOHMANN_JSON_VERSION_MINOR), '.',
20284 std::to_string(NLOHMANN_JSON_VERSION_PATCH));
20285 result["version"]["major"] = NLOHMANN_JSON_VERSION_MAJOR;
20286 result["version"]["minor"] = NLOHMANN_JSON_VERSION_MINOR;
20287 result["version"]["patch"] = NLOHMANN_JSON_VERSION_PATCH;
20288
20289 #ifdef _WIN32
20290 result["platform"] = "win32";
20291 #elif defined __linux__
20292 result["platform"] = "linux";
20293 #elif defined __APPLE__
20294 result["platform"] = "apple";
20295 #elif defined __unix__
20296 result["platform"] = "unix";
20297 #else
20298 result["platform"] = "unknown";
20299 #endif
20300
20301 #if defined(__ICC) || defined(__INTEL_COMPILER)
20302 result["compiler"] = {{"family", "icc"}, {"version", __INTEL_COMPILER}};
20303 #elif defined(__clang__)
20304 result["compiler"] = {{"family", "clang"}, {"version", __clang_version__}};
20305 #elif defined(__GNUC__) || defined(__GNUG__)
20306 result["compiler"] = {{"family", "gcc"}, {"version", detail::concat(
20307 std::to_string(__GNUC__), '.',
20308 std::to_string(__GNUC_MINOR__), '.',
20309 std::to_string(__GNUC_PATCHLEVEL__))
20310 }
20311 };
20312 #elif defined(__HP_cc) || defined(__HP_aCC)
20313 result["compiler"] = "hp"
20314 #elif defined(__IBMCPP__)
20315 result["compiler"] = {{"family", "ilecpp"}, {"version", __IBMCPP__}};
20316 #elif defined(_MSC_VER)
20317 result["compiler"] = {{"family", "msvc"}, {"version", _MSC_VER}};
20318 #elif defined(__PGI)
20319 result["compiler"] = {{"family", "pgcpp"}, {"version", __PGI}};
20320 #elif defined(__SUNPRO_CC)
20321 result["compiler"] = {{"family", "sunpro"}, {"version", __SUNPRO_CC}};
20322 #else
20323 result["compiler"] = {{"family", "unknown"}, {"version", "unknown"}};
20324 #endif
20325
20326 #if defined(_MSVC_LANG)
20327 result["compiler"]["c++"] = std::to_string(_MSVC_LANG);
20328 #elif defined(__cplusplus)
20329 result["compiler"]["c++"] = std::to_string(__cplusplus);
20330 #else
20331 result["compiler"]["c++"] = "unknown";
20332 #endif
20333 return result;
20334 }
20335
20336 ///////////////////////////
20337 // JSON value data types //
20338 ///////////////////////////
20339
20340 /// @name JSON value data types
20341 /// The data types to store a JSON value. These types are derived from
20342 /// the template arguments passed to class @ref basic_json.
20343 /// @{
20344
20345 /// @brief default object key comparator type
20346 /// The actual object key comparator type (@ref object_comparator_t) may be
20347 /// different.
20348 /// @sa https://json.nlohmann.me/api/basic_json/default_object_comparator_t/
20349 #if defined(JSON_HAS_CPP_14)
20350 // use of transparent comparator avoids unnecessary repeated construction of temporaries
20351 // in functions involving lookup by key with types other than object_t::key_type (aka. StringType)
20352 using default_object_comparator_t = std::less<>;
20353 #else
20354 using default_object_comparator_t = std::less<StringType>;
20355 #endif
20356
20357 /// @brief a type for an object
20358 /// @sa https://json.nlohmann.me/api/basic_json/object_t/
20359 using object_t = ObjectType<StringType,
20360 basic_json,
20361 default_object_comparator_t,
20362 AllocatorType<std::pair<const StringType,
20363 basic_json>>>;
20364
20365 /// @brief a type for an array
20366 /// @sa https://json.nlohmann.me/api/basic_json/array_t/
20367 using array_t = ArrayType<basic_json, AllocatorType<basic_json>>;
20368
20369 /// @brief a type for a string
20370 /// @sa https://json.nlohmann.me/api/basic_json/string_t/
20371 using string_t = StringType;
20372
20373 /// @brief a type for a boolean
20374 /// @sa https://json.nlohmann.me/api/basic_json/boolean_t/
20375 using boolean_t = BooleanType;
20376
20377 /// @brief a type for a number (integer)
20378 /// @sa https://json.nlohmann.me/api/basic_json/number_integer_t/
20379 using number_integer_t = NumberIntegerType;
20380
20381 /// @brief a type for a number (unsigned)
20382 /// @sa https://json.nlohmann.me/api/basic_json/number_unsigned_t/
20383 using number_unsigned_t = NumberUnsignedType;
20384
20385 /// @brief a type for a number (floating-point)
20386 /// @sa https://json.nlohmann.me/api/basic_json/number_float_t/
20387 using number_float_t = NumberFloatType;
20388
20389 /// @brief a type for a packed binary type
20390 /// @sa https://json.nlohmann.me/api/basic_json/binary_t/
20391 using binary_t = nlohmann::byte_container_with_subtype<BinaryType>;
20392
20393 /// @brief object key comparator type
20394 /// @sa https://json.nlohmann.me/api/basic_json/object_comparator_t/
20395 using object_comparator_t = detail::actual_object_comparator_t<basic_json>;
20396
20397 /// @}
20398
20399 private:
20400
20401 /// helper for exception-safe object creation
20402 template<typename T, typename... Args>
20403 JSON_HEDLEY_RETURNS_NON_NULL
20404 static T* create(Args&& ... args)
20405 {
20406 AllocatorType<T> alloc;
20407 using AllocatorTraits = std::allocator_traits<AllocatorType<T>>;
20408
20409 auto deleter = [&](T * obj)
20410 {
20411 AllocatorTraits::deallocate(alloc, obj, 1);
20412 };
20413 std::unique_ptr<T, decltype(deleter)> obj(AllocatorTraits::allocate(alloc, 1), deleter);
20414 AllocatorTraits::construct(alloc, obj.get(), std::forward<Args>(args)...);
20415 JSON_ASSERT(obj != nullptr);
20416 return obj.release();
20417 }
20418
20419 ////////////////////////
20420 // JSON value storage //
20421 ////////////////////////
20422
20423 JSON_PRIVATE_UNLESS_TESTED:
20424 /*!
20425 @brief a JSON value
20426
20427 The actual storage for a JSON value of the @ref basic_json class. This
20428 union combines the different storage types for the JSON value types
20429 defined in @ref value_t.
20430
20431 JSON type | value_t type | used type
20432 --------- | --------------- | ------------------------
20433 object | object | pointer to @ref object_t
20434 array | array | pointer to @ref array_t
20435 string | string | pointer to @ref string_t
20436 boolean | boolean | @ref boolean_t
20437 number | number_integer | @ref number_integer_t
20438 number | number_unsigned | @ref number_unsigned_t
20439 number | number_float | @ref number_float_t
20440 binary | binary | pointer to @ref binary_t
20441 null | null | *no value is stored*
20442
20443 @note Variable-length types (objects, arrays, and strings) are stored as
20444 pointers. The size of the union should not exceed 64 bits if the default
20445 value types are used.
20446
20447 @since version 1.0.0
20448 */
20449 union json_value
20450 {
20451 /// object (stored with pointer to save storage)
20452 object_t* object;
20453 /// array (stored with pointer to save storage)
20454 array_t* array;
20455 /// string (stored with pointer to save storage)
20456 string_t* string;
20457 /// binary (stored with pointer to save storage)
20458 binary_t* binary;
20459 /// boolean
20460 boolean_t boolean;
20461 /// number (integer)
20462 number_integer_t number_integer;
20463 /// number (unsigned integer)
20464 number_unsigned_t number_unsigned;
20465 /// number (floating-point)
20466 number_float_t number_float;
20467
20468 /// default constructor (for null values)
20469 json_value() = default;
20470 /// constructor for booleans
20471 json_value(boolean_t v) noexcept : boolean(v) {}
20472 /// constructor for numbers (integer)
20473 json_value(number_integer_t v) noexcept : number_integer(v) {}
20474 /// constructor for numbers (unsigned)
20475 json_value(number_unsigned_t v) noexcept : number_unsigned(v) {}
20476 /// constructor for numbers (floating-point)
20477 json_value(number_float_t v) noexcept : number_float(v) {}
20478 /// constructor for empty values of a given type
20479 json_value(value_t t)
20480 {
20481 switch (t)
20482 {
20483 case value_t::object:
20484 {
20485 object = create<object_t>();
20486 break;
20487 }
20488
20489 case value_t::array:
20490 {
20491 array = create<array_t>();
20492 break;
20493 }
20494
20495 case value_t::string:
20496 {
20497 string = create<string_t>("");
20498 break;
20499 }
20500
20501 case value_t::binary:
20502 {
20503 binary = create<binary_t>();
20504 break;
20505 }
20506
20507 case value_t::boolean:
20508 {
20509 boolean = static_cast<boolean_t>(false);
20510 break;
20511 }
20512
20513 case value_t::number_integer:
20514 {
20515 number_integer = static_cast<number_integer_t>(0);
20516 break;
20517 }
20518
20519 case value_t::number_unsigned:
20520 {
20521 number_unsigned = static_cast<number_unsigned_t>(0);
20522 break;
20523 }
20524
20525 case value_t::number_float:
20526 {
20527 number_float = static_cast<number_float_t>(0.0);
20528 break;
20529 }
20530
20531 case value_t::null:
20532 {
20533 object = nullptr; // silence warning, see #821
20534 break;
20535 }
20536
20537 case value_t::discarded:
20538 default:
20539 {
20540 object = nullptr; // silence warning, see #821
20541 if (JSON_HEDLEY_UNLIKELY(t == value_t::null))
20542 {
20543 JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.12.0", nullptr)); // LCOV_EXCL_LINE
20544 }
20545 break;
20546 }
20547 }
20548 }
20549
20550 /// constructor for strings
20551 json_value(const string_t& value) : string(create<string_t>(value)) {}
20552
20553 /// constructor for rvalue strings
20554 json_value(string_t&& value) : string(create<string_t>(std::move(value))) {}
20555
20556 /// constructor for objects
20557 json_value(const object_t& value) : object(create<object_t>(value)) {}
20558
20559 /// constructor for rvalue objects
20560 json_value(object_t&& value) : object(create<object_t>(std::move(value))) {}
20561
20562 /// constructor for arrays
20563 json_value(const array_t& value) : array(create<array_t>(value)) {}
20564
20565 /// constructor for rvalue arrays
20566 json_value(array_t&& value) : array(create<array_t>(std::move(value))) {}
20567
20568 /// constructor for binary arrays
20569 json_value(const typename binary_t::container_type& value) : binary(create<binary_t>(value)) {}
20570
20571 /// constructor for rvalue binary arrays
20572 json_value(typename binary_t::container_type&& value) : binary(create<binary_t>(std::move(value))) {}
20573
20574 /// constructor for binary arrays (internal type)
20575 json_value(const binary_t& value) : binary(create<binary_t>(value)) {}
20576
20577 /// constructor for rvalue binary arrays (internal type)
20578 json_value(binary_t&& value) : binary(create<binary_t>(std::move(value))) {}
20579
20580 void destroy(value_t t)
20581 {
20582 if (
20583 (t == value_t::object && object == nullptr) ||
20584 (t == value_t::array && array == nullptr) ||
20585 (t == value_t::string && string == nullptr) ||
20586 (t == value_t::binary && binary == nullptr)
20587 )
20588 {
20589 //not initialized (e.g. due to exception in the ctor)
20590 return;
20591 }
20592 if (t == value_t::array || t == value_t::object)
20593 {
20594 // flatten the current json_value to a heap-allocated stack
20595 std::vector<basic_json> stack;
20596
20597 // move the top-level items to stack
20598 if (t == value_t::array)
20599 {
20600 stack.reserve(array->size());
20601 std::move(array->begin(), array->end(), std::back_inserter(stack));
20602 }
20603 else
20604 {
20605 stack.reserve(object->size());
20606 for (auto&& it : *object)
20607 {
20608 stack.push_back(std::move(it.second));
20609 }
20610 }
20611
20612 while (!stack.empty())
20613 {
20614 // move the last item to local variable to be processed
20615 basic_json current_item(std::move(stack.back()));
20616 stack.pop_back();
20617
20618 // if current_item is array/object, move
20619 // its children to the stack to be processed later
20620 if (current_item.is_array())
20621 {
20622 std::move(current_item.m_data.m_value.array->begin(), current_item.m_data.m_value.array->end(), std::back_inserter(stack));
20623
20624 current_item.m_data.m_value.array->clear();
20625 }
20626 else if (current_item.is_object())
20627 {
20628 for (auto&& it : *current_item.m_data.m_value.object)
20629 {
20630 stack.push_back(std::move(it.second));
20631 }
20632
20633 current_item.m_data.m_value.object->clear();
20634 }
20635
20636 // it's now safe that current_item get destructed
20637 // since it doesn't have any children
20638 }
20639 }
20640
20641 switch (t)
20642 {
20643 case value_t::object:
20644 {
20645 AllocatorType<object_t> alloc;
20646 std::allocator_traits<decltype(alloc)>::destroy(alloc, object);
20647 std::allocator_traits<decltype(alloc)>::deallocate(alloc, object, 1);
20648 break;
20649 }
20650
20651 case value_t::array:
20652 {
20653 AllocatorType<array_t> alloc;
20654 std::allocator_traits<decltype(alloc)>::destroy(alloc, array);
20655 std::allocator_traits<decltype(alloc)>::deallocate(alloc, array, 1);
20656 break;
20657 }
20658
20659 case value_t::string:
20660 {
20661 AllocatorType<string_t> alloc;
20662 std::allocator_traits<decltype(alloc)>::destroy(alloc, string);
20663 std::allocator_traits<decltype(alloc)>::deallocate(alloc, string, 1);
20664 break;
20665 }
20666
20667 case value_t::binary:
20668 {
20669 AllocatorType<binary_t> alloc;
20670 std::allocator_traits<decltype(alloc)>::destroy(alloc, binary);
20671 std::allocator_traits<decltype(alloc)>::deallocate(alloc, binary, 1);
20672 break;
20673 }
20674
20675 case value_t::null:
20676 case value_t::boolean:
20677 case value_t::number_integer:
20678 case value_t::number_unsigned:
20679 case value_t::number_float:
20680 case value_t::discarded:
20681 default:
20682 {
20683 break;
20684 }
20685 }
20686 }
20687 };
20688
20689 private:
20690 /*!
20691 @brief checks the class invariants
20692
20693 This function asserts the class invariants. It needs to be called at the
20694 end of every constructor to make sure that created objects respect the
20695 invariant. Furthermore, it has to be called each time the type of a JSON
20696 value is changed, because the invariant expresses a relationship between
20697 @a m_type and @a m_value.
20698
20699 Furthermore, the parent relation is checked for arrays and objects: If
20700 @a check_parents true and the value is an array or object, then the
20701 container's elements must have the current value as parent.
20702
20703 @param[in] check_parents whether the parent relation should be checked.
20704 The value is true by default and should only be set to false
20705 during destruction of objects when the invariant does not
20706 need to hold.
20707 */
20708 void assert_invariant(bool check_parents = true) const noexcept
20709 {
20710 JSON_ASSERT(m_data.m_type != value_t::object || m_data.m_value.object != nullptr);
20711 JSON_ASSERT(m_data.m_type != value_t::array || m_data.m_value.array != nullptr);
20712 JSON_ASSERT(m_data.m_type != value_t::string || m_data.m_value.string != nullptr);
20713 JSON_ASSERT(m_data.m_type != value_t::binary || m_data.m_value.binary != nullptr);
20714
20715 #if JSON_DIAGNOSTICS
20716 JSON_TRY
20717 {
20718 // cppcheck-suppress assertWithSideEffect
20719 JSON_ASSERT(!check_parents || !is_structured() || std::all_of(begin(), end(), [this](const basic_json & j)
20720 {
20721 return j.m_parent == this;
20722 }));
20723 }
20724 JSON_CATCH(...) {} // LCOV_EXCL_LINE
20725 #endif
20726 static_cast<void>(check_parents);
20727 }
20728
20729 void set_parents()
20730 {
20731 #if JSON_DIAGNOSTICS
20732 switch (m_data.m_type)
20733 {
20734 case value_t::array:
20735 {
20736 for (auto& element : *m_data.m_value.array)
20737 {
20738 element.m_parent = this;
20739 }
20740 break;
20741 }
20742
20743 case value_t::object:
20744 {
20745 for (auto& element : *m_data.m_value.object)
20746 {
20747 element.second.m_parent = this;
20748 }
20749 break;
20750 }
20751
20752 case value_t::null:
20753 case value_t::string:
20754 case value_t::boolean:
20755 case value_t::number_integer:
20756 case value_t::number_unsigned:
20757 case value_t::number_float:
20758 case value_t::binary:
20759 case value_t::discarded:
20760 default:
20761 break;
20762 }
20763 #endif
20764 }
20765
20766 iterator set_parents(iterator it, typename iterator::difference_type count_set_parents)
20767 {
20768 #if JSON_DIAGNOSTICS
20769 for (typename iterator::difference_type i = 0; i < count_set_parents; ++i)
20770 {
20771 (it + i)->m_parent = this;
20772 }
20773 #else
20774 static_cast<void>(count_set_parents);
20775 #endif
20776 return it;
20777 }
20778
20779 reference set_parent(reference j, std::size_t old_capacity = detail::unknown_size())
20780 {
20781 #if JSON_DIAGNOSTICS
20782 if (old_capacity != detail::unknown_size())
20783 {
20784 // see https://github.com/nlohmann/json/issues/2838
20785 JSON_ASSERT(type() == value_t::array);
20786 if (JSON_HEDLEY_UNLIKELY(m_data.m_value.array->capacity() != old_capacity))
20787 {
20788 // capacity has changed: update all parents
20789 set_parents();
20790 return j;
20791 }
20792 }
20793
20794 // ordered_json uses a vector internally, so pointers could have
20795 // been invalidated; see https://github.com/nlohmann/json/issues/2962
20796 #ifdef JSON_HEDLEY_MSVC_VERSION
20797 #pragma warning(push )
20798 #pragma warning(disable : 4127) // ignore warning to replace if with if constexpr
20799 #endif
20800 if (detail::is_ordered_map<object_t>::value)
20801 {
20802 set_parents();
20803 return j;
20804 }
20805 #ifdef JSON_HEDLEY_MSVC_VERSION
20806 #pragma warning( pop )
20807 #endif
20808
20809 j.m_parent = this;
20810 #else
20811 static_cast<void>(j);
20812 static_cast<void>(old_capacity);
20813 #endif
20814 return j;
20815 }
20816
20817 public:
20818 //////////////////////////
20819 // JSON parser callback //
20820 //////////////////////////
20821
20822 /// @brief parser event types
20823 /// @sa https://json.nlohmann.me/api/basic_json/parse_event_t/
20824 using parse_event_t = detail::parse_event_t;
20825
20826 /// @brief per-element parser callback type
20827 /// @sa https://json.nlohmann.me/api/basic_json/parser_callback_t/
20828 using parser_callback_t = detail::parser_callback_t<basic_json>;
20829
20830 //////////////////
20831 // constructors //
20832 //////////////////
20833
20834 /// @name constructors and destructors
20835 /// Constructors of class @ref basic_json, copy/move constructor, copy
20836 /// assignment, static functions creating objects, and the destructor.
20837 /// @{
20838
20839 /// @brief create an empty value with a given type
20840 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
20841 basic_json(const value_t v)
20842 : m_data(v)
20843 {
20844 assert_invariant();
20845 }
20846
20847 /// @brief create a null object
20848 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
20849 basic_json(std::nullptr_t = nullptr) noexcept // NOLINT(bugprone-exception-escape)
20850 : basic_json(value_t::null)
20851 {
20852 assert_invariant();
20853 }
20854
20855 /// @brief create a JSON value from compatible types
20856 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
20857 template < typename CompatibleType,
20858 typename U = detail::uncvref_t<CompatibleType>,
20859 detail::enable_if_t <
20860 !detail::is_basic_json<U>::value && detail::is_compatible_type<basic_json_t, U>::value, int > = 0 >
20861 basic_json(CompatibleType && val) noexcept(noexcept( // NOLINT(bugprone-forwarding-reference-overload,bugprone-exception-escape)
20862 JSONSerializer<U>::to_json(std::declval<basic_json_t&>(),
20863 std::forward<CompatibleType>(val))))
20864 {
20865 JSONSerializer<U>::to_json(*this, std::forward<CompatibleType>(val));
20866 set_parents();
20867 assert_invariant();
20868 }
20869
20870 /// @brief create a JSON value from an existing one
20871 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
20872 template < typename BasicJsonType,
20873 detail::enable_if_t <
20874 detail::is_basic_json<BasicJsonType>::value&& !std::is_same<basic_json, BasicJsonType>::value, int > = 0 >
20875 basic_json(const BasicJsonType& val)
20876 #if JSON_DIAGNOSTIC_POSITIONS
20877 : start_position(val.start_pos()),
20878 end_position(val.end_pos())
20879 #endif
20880 {
20881 using other_boolean_t = typename BasicJsonType::boolean_t;
20882 using other_number_float_t = typename BasicJsonType::number_float_t;
20883 using other_number_integer_t = typename BasicJsonType::number_integer_t;
20884 using other_number_unsigned_t = typename BasicJsonType::number_unsigned_t;
20885 using other_string_t = typename BasicJsonType::string_t;
20886 using other_object_t = typename BasicJsonType::object_t;
20887 using other_array_t = typename BasicJsonType::array_t;
20888 using other_binary_t = typename BasicJsonType::binary_t;
20889
20890 switch (val.type())
20891 {
20892 case value_t::boolean:
20893 JSONSerializer<other_boolean_t>::to_json(*this, val.template get<other_boolean_t>());
20894 break;
20895 case value_t::number_float:
20896 JSONSerializer<other_number_float_t>::to_json(*this, val.template get<other_number_float_t>());
20897 break;
20898 case value_t::number_integer:
20899 JSONSerializer<other_number_integer_t>::to_json(*this, val.template get<other_number_integer_t>());
20900 break;
20901 case value_t::number_unsigned:
20902 JSONSerializer<other_number_unsigned_t>::to_json(*this, val.template get<other_number_unsigned_t>());
20903 break;
20904 case value_t::string:
20905 JSONSerializer<other_string_t>::to_json(*this, val.template get_ref<const other_string_t&>());
20906 break;
20907 case value_t::object:
20908 JSONSerializer<other_object_t>::to_json(*this, val.template get_ref<const other_object_t&>());
20909 break;
20910 case value_t::array:
20911 JSONSerializer<other_array_t>::to_json(*this, val.template get_ref<const other_array_t&>());
20912 break;
20913 case value_t::binary:
20914 JSONSerializer<other_binary_t>::to_json(*this, val.template get_ref<const other_binary_t&>());
20915 break;
20916 case value_t::null:
20917 *this = nullptr;
20918 break;
20919 case value_t::discarded:
20920 m_data.m_type = value_t::discarded;
20921 break;
20922 default: // LCOV_EXCL_LINE
20923 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
20924 }
20925 JSON_ASSERT(m_data.m_type == val.type());
20926
20927 set_parents();
20928 assert_invariant();
20929 }
20930
20931 /// @brief create a container (array or object) from an initializer list
20932 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
20933 basic_json(initializer_list_t init,
20934 bool type_deduction = true,
20935 value_t manual_type = value_t::array)
20936 {
20937 // check if each element is an array with two elements whose first
20938 // element is a string
20939 bool is_an_object = std::all_of(init.begin(), init.end(),
20940 [](const detail::json_ref<basic_json>& element_ref)
20941 {
20942 // The cast is to ensure op[size_type] is called, bearing in mind size_type may not be int;
20943 // (many string types can be constructed from 0 via its null-pointer guise, so we get a
20944 // broken call to op[key_type], the wrong semantics and a 4804 warning on Windows)
20945 return element_ref->is_array() && element_ref->size() == 2 && (*element_ref)[static_cast<size_type>(0)].is_string();
20946 });
20947
20948 // adjust type if type deduction is not wanted
20949 if (!type_deduction)
20950 {
20951 // if array is wanted, do not create an object though possible
20952 if (manual_type == value_t::array)
20953 {
20954 is_an_object = false;
20955 }
20956
20957 // if object is wanted but impossible, throw an exception
20958 if (JSON_HEDLEY_UNLIKELY(manual_type == value_t::object && !is_an_object))
20959 {
20960 JSON_THROW(type_error::create(301, "cannot create object from initializer list", nullptr));
20961 }
20962 }
20963
20964 if (is_an_object)
20965 {
20966 // the initializer list is a list of pairs -> create object
20967 m_data.m_type = value_t::object;
20968 m_data.m_value = value_t::object;
20969
20970 for (auto& element_ref : init)
20971 {
20972 auto element = element_ref.moved_or_copied();
20973 m_data.m_value.object->emplace(
20974 std::move(*((*element.m_data.m_value.array)[0].m_data.m_value.string)),
20975 std::move((*element.m_data.m_value.array)[1]));
20976 }
20977 }
20978 else
20979 {
20980 // the initializer list describes an array -> create array
20981 m_data.m_type = value_t::array;
20982 m_data.m_value.array = create<array_t>(init.begin(), init.end());
20983 }
20984
20985 set_parents();
20986 assert_invariant();
20987 }
20988
20989 /// @brief explicitly create a binary array (without subtype)
20990 /// @sa https://json.nlohmann.me/api/basic_json/binary/
20991 JSON_HEDLEY_WARN_UNUSED_RESULT
20992 static basic_json binary(const typename binary_t::container_type& init)
20993 {
20994 auto res = basic_json();
20995 res.m_data.m_type = value_t::binary;
20996 res.m_data.m_value = init;
20997 return res;
20998 }
20999
21000 /// @brief explicitly create a binary array (with subtype)
21001 /// @sa https://json.nlohmann.me/api/basic_json/binary/
21002 JSON_HEDLEY_WARN_UNUSED_RESULT
21003 static basic_json binary(const typename binary_t::container_type& init, typename binary_t::subtype_type subtype)
21004 {
21005 auto res = basic_json();
21006 res.m_data.m_type = value_t::binary;
21007 res.m_data.m_value = binary_t(init, subtype);
21008 return res;
21009 }
21010
21011 /// @brief explicitly create a binary array
21012 /// @sa https://json.nlohmann.me/api/basic_json/binary/
21013 JSON_HEDLEY_WARN_UNUSED_RESULT
21014 static basic_json binary(typename binary_t::container_type&& init)
21015 {
21016 auto res = basic_json();
21017 res.m_data.m_type = value_t::binary;
21018 res.m_data.m_value = std::move(init);
21019 return res;
21020 }
21021
21022 /// @brief explicitly create a binary array (with subtype)
21023 /// @sa https://json.nlohmann.me/api/basic_json/binary/
21024 JSON_HEDLEY_WARN_UNUSED_RESULT
21025 static basic_json binary(typename binary_t::container_type&& init, typename binary_t::subtype_type subtype)
21026 {
21027 auto res = basic_json();
21028 res.m_data.m_type = value_t::binary;
21029 res.m_data.m_value = binary_t(std::move(init), subtype);
21030 return res;
21031 }
21032
21033 /// @brief explicitly create an array from an initializer list
21034 /// @sa https://json.nlohmann.me/api/basic_json/array/
21035 JSON_HEDLEY_WARN_UNUSED_RESULT
21036 static basic_json array(initializer_list_t init = {})
21037 {
21038 return basic_json(init, false, value_t::array);
21039 }
21040
21041 /// @brief explicitly create an object from an initializer list
21042 /// @sa https://json.nlohmann.me/api/basic_json/object/
21043 JSON_HEDLEY_WARN_UNUSED_RESULT
21044 static basic_json object(initializer_list_t init = {})
21045 {
21046 return basic_json(init, false, value_t::object);
21047 }
21048
21049 /// @brief construct an array with count copies of given value
21050 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
21051 basic_json(size_type cnt, const basic_json& val):
21052 m_data{cnt, val}
21053 {
21054 set_parents();
21055 assert_invariant();
21056 }
21057
21058 /// @brief construct a JSON container given an iterator range
21059 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
21060 template < class InputIT, typename std::enable_if <
21061 std::is_same<InputIT, typename basic_json_t::iterator>::value ||
21062 std::is_same<InputIT, typename basic_json_t::const_iterator>::value, int >::type = 0 >
21063 basic_json(InputIT first, InputIT last) // NOLINT(performance-unnecessary-value-param)
21064 {
21065 JSON_ASSERT(first.m_object != nullptr);
21066 JSON_ASSERT(last.m_object != nullptr);
21067
21068 // make sure iterator fits the current value
21069 if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
21070 {
21071 JSON_THROW(invalid_iterator::create(201, "iterators are not compatible", nullptr));
21072 }
21073
21074 // copy type from first iterator
21075 m_data.m_type = first.m_object->m_data.m_type;
21076
21077 // check if iterator range is complete for primitive values
21078 switch (m_data.m_type)
21079 {
21080 case value_t::boolean:
21081 case value_t::number_float:
21082 case value_t::number_integer:
21083 case value_t::number_unsigned:
21084 case value_t::string:
21085 {
21086 if (JSON_HEDLEY_UNLIKELY(!first.m_it.primitive_iterator.is_begin()
21087 || !last.m_it.primitive_iterator.is_end()))
21088 {
21089 JSON_THROW(invalid_iterator::create(204, "iterators out of range", first.m_object));
21090 }
21091 break;
21092 }
21093
21094 case value_t::null:
21095 case value_t::object:
21096 case value_t::array:
21097 case value_t::binary:
21098 case value_t::discarded:
21099 default:
21100 break;
21101 }
21102
21103 switch (m_data.m_type)
21104 {
21105 case value_t::number_integer:
21106 {
21107 m_data.m_value.number_integer = first.m_object->m_data.m_value.number_integer;
21108 break;
21109 }
21110
21111 case value_t::number_unsigned:
21112 {
21113 m_data.m_value.number_unsigned = first.m_object->m_data.m_value.number_unsigned;
21114 break;
21115 }
21116
21117 case value_t::number_float:
21118 {
21119 m_data.m_value.number_float = first.m_object->m_data.m_value.number_float;
21120 break;
21121 }
21122
21123 case value_t::boolean:
21124 {
21125 m_data.m_value.boolean = first.m_object->m_data.m_value.boolean;
21126 break;
21127 }
21128
21129 case value_t::string:
21130 {
21131 m_data.m_value = *first.m_object->m_data.m_value.string;
21132 break;
21133 }
21134
21135 case value_t::object:
21136 {
21137 m_data.m_value.object = create<object_t>(first.m_it.object_iterator,
21138 last.m_it.object_iterator);
21139 break;
21140 }
21141
21142 case value_t::array:
21143 {
21144 m_data.m_value.array = create<array_t>(first.m_it.array_iterator,
21145 last.m_it.array_iterator);
21146 break;
21147 }
21148
21149 case value_t::binary:
21150 {
21151 m_data.m_value = *first.m_object->m_data.m_value.binary;
21152 break;
21153 }
21154
21155 case value_t::null:
21156 case value_t::discarded:
21157 default:
21158 JSON_THROW(invalid_iterator::create(206, detail::concat("cannot construct with iterators from ", first.m_object->type_name()), first.m_object));
21159 }
21160
21161 set_parents();
21162 assert_invariant();
21163 }
21164
21165 ///////////////////////////////////////
21166 // other constructors and destructor //
21167 ///////////////////////////////////////
21168
21169 template<typename JsonRef,
21170 detail::enable_if_t<detail::conjunction<detail::is_json_ref<JsonRef>,
21171 std::is_same<typename JsonRef::value_type, basic_json>>::value, int> = 0 >
21172 basic_json(const JsonRef& ref) : basic_json(ref.moved_or_copied()) {}
21173
21174 /// @brief copy constructor
21175 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
21176 basic_json(const basic_json& other)
21177 : json_base_class_t(other)
21178 #if JSON_DIAGNOSTIC_POSITIONS
21179 , start_position(other.start_position)
21180 , end_position(other.end_position)
21181 #endif
21182 {
21183 m_data.m_type = other.m_data.m_type;
21184 // check of passed value is valid
21185 other.assert_invariant();
21186
21187 switch (m_data.m_type)
21188 {
21189 case value_t::object:
21190 {
21191 m_data.m_value = *other.m_data.m_value.object;
21192 break;
21193 }
21194
21195 case value_t::array:
21196 {
21197 m_data.m_value = *other.m_data.m_value.array;
21198 break;
21199 }
21200
21201 case value_t::string:
21202 {
21203 m_data.m_value = *other.m_data.m_value.string;
21204 break;
21205 }
21206
21207 case value_t::boolean:
21208 {
21209 m_data.m_value = other.m_data.m_value.boolean;
21210 break;
21211 }
21212
21213 case value_t::number_integer:
21214 {
21215 m_data.m_value = other.m_data.m_value.number_integer;
21216 break;
21217 }
21218
21219 case value_t::number_unsigned:
21220 {
21221 m_data.m_value = other.m_data.m_value.number_unsigned;
21222 break;
21223 }
21224
21225 case value_t::number_float:
21226 {
21227 m_data.m_value = other.m_data.m_value.number_float;
21228 break;
21229 }
21230
21231 case value_t::binary:
21232 {
21233 m_data.m_value = *other.m_data.m_value.binary;
21234 break;
21235 }
21236
21237 case value_t::null:
21238 case value_t::discarded:
21239 default:
21240 break;
21241 }
21242
21243 set_parents();
21244 assert_invariant();
21245 }
21246
21247 /// @brief move constructor
21248 /// @sa https://json.nlohmann.me/api/basic_json/basic_json/
21249 basic_json(basic_json&& other) noexcept
21250 : json_base_class_t(std::forward<json_base_class_t>(other)),
21251 m_data(std::move(other.m_data)) // cppcheck-suppress[accessForwarded] TODO check
21252 #if JSON_DIAGNOSTIC_POSITIONS
21253 , start_position(other.start_position) // cppcheck-suppress[accessForwarded] TODO check
21254 , end_position(other.end_position) // cppcheck-suppress[accessForwarded] TODO check
21255 #endif
21256 {
21257 // check that passed value is valid
21258 other.assert_invariant(false); // cppcheck-suppress[accessForwarded]
21259
21260 // invalidate payload
21261 other.m_data.m_type = value_t::null;
21262 other.m_data.m_value = {};
21263
21264 #if JSON_DIAGNOSTIC_POSITIONS
21265 other.start_position = std::string::npos;
21266 other.end_position = std::string::npos;
21267 #endif
21268
21269 set_parents();
21270 assert_invariant();
21271 }
21272
21273 /// @brief copy assignment
21274 /// @sa https://json.nlohmann.me/api/basic_json/operator=/
21275 basic_json& operator=(basic_json other) noexcept (
21276 std::is_nothrow_move_constructible<value_t>::value&&
21277 std::is_nothrow_move_assignable<value_t>::value&&
21278 std::is_nothrow_move_constructible<json_value>::value&&
21279 std::is_nothrow_move_assignable<json_value>::value&&
21280 std::is_nothrow_move_assignable<json_base_class_t>::value
21281 )
21282 {
21283 // check that passed value is valid
21284 other.assert_invariant();
21285
21286 using std::swap;
21287 swap(m_data.m_type, other.m_data.m_type);
21288 swap(m_data.m_value, other.m_data.m_value);
21289
21290 #if JSON_DIAGNOSTIC_POSITIONS
21291 swap(start_position, other.start_position);
21292 swap(end_position, other.end_position);
21293 #endif
21294
21295 json_base_class_t::operator=(std::move(other));
21296
21297 set_parents();
21298 assert_invariant();
21299 return *this;
21300 }
21301
21302 /// @brief destructor
21303 /// @sa https://json.nlohmann.me/api/basic_json/~basic_json/
21304 ~basic_json() noexcept
21305 {
21306 assert_invariant(false);
21307 }
21308
21309 /// @}
21310
21311 public:
21312 ///////////////////////
21313 // object inspection //
21314 ///////////////////////
21315
21316 /// @name object inspection
21317 /// Functions to inspect the type of a JSON value.
21318 /// @{
21319
21320 /// @brief serialization
21321 /// @sa https://json.nlohmann.me/api/basic_json/dump/
21322 string_t dump(const int indent = -1,
21323 const char indent_char = ' ',
21324 const bool ensure_ascii = false,
21325 const error_handler_t error_handler = error_handler_t::strict) const
21326 {
21327 string_t result;
21328 serializer s(detail::output_adapter<char, string_t>(result), indent_char, error_handler);
21329
21330 if (indent >= 0)
21331 {
21332 s.dump(*this, true, ensure_ascii, static_cast<unsigned int>(indent));
21333 }
21334 else
21335 {
21336 s.dump(*this, false, ensure_ascii, 0);
21337 }
21338
21339 return result;
21340 }
21341
21342 /// @brief return the type of the JSON value (explicit)
21343 /// @sa https://json.nlohmann.me/api/basic_json/type/
21344 constexpr value_t type() const noexcept
21345 {
21346 return m_data.m_type;
21347 }
21348
21349 /// @brief return whether type is primitive
21350 /// @sa https://json.nlohmann.me/api/basic_json/is_primitive/
21351 constexpr bool is_primitive() const noexcept
21352 {
21353 return is_null() || is_string() || is_boolean() || is_number() || is_binary();
21354 }
21355
21356 /// @brief return whether type is structured
21357 /// @sa https://json.nlohmann.me/api/basic_json/is_structured/
21358 constexpr bool is_structured() const noexcept
21359 {
21360 return is_array() || is_object();
21361 }
21362
21363 /// @brief return whether value is null
21364 /// @sa https://json.nlohmann.me/api/basic_json/is_null/
21365 constexpr bool is_null() const noexcept
21366 {
21367 return m_data.m_type == value_t::null;
21368 }
21369
21370 /// @brief return whether value is a boolean
21371 /// @sa https://json.nlohmann.me/api/basic_json/is_boolean/
21372 constexpr bool is_boolean() const noexcept
21373 {
21374 return m_data.m_type == value_t::boolean;
21375 }
21376
21377 /// @brief return whether value is a number
21378 /// @sa https://json.nlohmann.me/api/basic_json/is_number/
21379 constexpr bool is_number() const noexcept
21380 {
21381 return is_number_integer() || is_number_float();
21382 }
21383
21384 /// @brief return whether value is an integer number
21385 /// @sa https://json.nlohmann.me/api/basic_json/is_number_integer/
21386 constexpr bool is_number_integer() const noexcept
21387 {
21388 return m_data.m_type == value_t::number_integer || m_data.m_type == value_t::number_unsigned;
21389 }
21390
21391 /// @brief return whether value is an unsigned integer number
21392 /// @sa https://json.nlohmann.me/api/basic_json/is_number_unsigned/
21393 constexpr bool is_number_unsigned() const noexcept
21394 {
21395 return m_data.m_type == value_t::number_unsigned;
21396 }
21397
21398 /// @brief return whether value is a floating-point number
21399 /// @sa https://json.nlohmann.me/api/basic_json/is_number_float/
21400 constexpr bool is_number_float() const noexcept
21401 {
21402 return m_data.m_type == value_t::number_float;
21403 }
21404
21405 /// @brief return whether value is an object
21406 /// @sa https://json.nlohmann.me/api/basic_json/is_object/
21407 constexpr bool is_object() const noexcept
21408 {
21409 return m_data.m_type == value_t::object;
21410 }
21411
21412 /// @brief return whether value is an array
21413 /// @sa https://json.nlohmann.me/api/basic_json/is_array/
21414 constexpr bool is_array() const noexcept
21415 {
21416 return m_data.m_type == value_t::array;
21417 }
21418
21419 /// @brief return whether value is a string
21420 /// @sa https://json.nlohmann.me/api/basic_json/is_string/
21421 constexpr bool is_string() const noexcept
21422 {
21423 return m_data.m_type == value_t::string;
21424 }
21425
21426 /// @brief return whether value is a binary array
21427 /// @sa https://json.nlohmann.me/api/basic_json/is_binary/
21428 constexpr bool is_binary() const noexcept
21429 {
21430 return m_data.m_type == value_t::binary;
21431 }
21432
21433 /// @brief return whether value is discarded
21434 /// @sa https://json.nlohmann.me/api/basic_json/is_discarded/
21435 constexpr bool is_discarded() const noexcept
21436 {
21437 return m_data.m_type == value_t::discarded;
21438 }
21439
21440 /// @brief return the type of the JSON value (implicit)
21441 /// @sa https://json.nlohmann.me/api/basic_json/operator_value_t/
21442 constexpr operator value_t() const noexcept
21443 {
21444 return m_data.m_type;
21445 }
21446
21447 /// @}
21448
21449 private:
21450 //////////////////
21451 // value access //
21452 //////////////////
21453
21454 /// get a boolean (explicit)
21455 boolean_t get_impl(boolean_t* /*unused*/) const
21456 {
21457 if (JSON_HEDLEY_LIKELY(is_boolean()))
21458 {
21459 return m_data.m_value.boolean;
21460 }
21461
21462 JSON_THROW(type_error::create(302, detail::concat("type must be boolean, but is ", type_name()), this));
21463 }
21464
21465 /// get a pointer to the value (object)
21466 object_t* get_impl_ptr(object_t* /*unused*/) noexcept
21467 {
21468 return is_object() ? m_data.m_value.object : nullptr;
21469 }
21470
21471 /// get a pointer to the value (object)
21472 constexpr const object_t* get_impl_ptr(const object_t* /*unused*/) const noexcept
21473 {
21474 return is_object() ? m_data.m_value.object : nullptr;
21475 }
21476
21477 /// get a pointer to the value (array)
21478 array_t* get_impl_ptr(array_t* /*unused*/) noexcept
21479 {
21480 return is_array() ? m_data.m_value.array : nullptr;
21481 }
21482
21483 /// get a pointer to the value (array)
21484 constexpr const array_t* get_impl_ptr(const array_t* /*unused*/) const noexcept
21485 {
21486 return is_array() ? m_data.m_value.array : nullptr;
21487 }
21488
21489 /// get a pointer to the value (string)
21490 string_t* get_impl_ptr(string_t* /*unused*/) noexcept
21491 {
21492 return is_string() ? m_data.m_value.string : nullptr;
21493 }
21494
21495 /// get a pointer to the value (string)
21496 constexpr const string_t* get_impl_ptr(const string_t* /*unused*/) const noexcept
21497 {
21498 return is_string() ? m_data.m_value.string : nullptr;
21499 }
21500
21501 /// get a pointer to the value (boolean)
21502 boolean_t* get_impl_ptr(boolean_t* /*unused*/) noexcept
21503 {
21504 return is_boolean() ? &m_data.m_value.boolean : nullptr;
21505 }
21506
21507 /// get a pointer to the value (boolean)
21508 constexpr const boolean_t* get_impl_ptr(const boolean_t* /*unused*/) const noexcept
21509 {
21510 return is_boolean() ? &m_data.m_value.boolean : nullptr;
21511 }
21512
21513 /// get a pointer to the value (integer number)
21514 number_integer_t* get_impl_ptr(number_integer_t* /*unused*/) noexcept
21515 {
21516 return m_data.m_type == value_t::number_integer ? &m_data.m_value.number_integer : nullptr;
21517 }
21518
21519 /// get a pointer to the value (integer number)
21520 constexpr const number_integer_t* get_impl_ptr(const number_integer_t* /*unused*/) const noexcept
21521 {
21522 return m_data.m_type == value_t::number_integer ? &m_data.m_value.number_integer : nullptr;
21523 }
21524
21525 /// get a pointer to the value (unsigned number)
21526 number_unsigned_t* get_impl_ptr(number_unsigned_t* /*unused*/) noexcept
21527 {
21528 return is_number_unsigned() ? &m_data.m_value.number_unsigned : nullptr;
21529 }
21530
21531 /// get a pointer to the value (unsigned number)
21532 constexpr const number_unsigned_t* get_impl_ptr(const number_unsigned_t* /*unused*/) const noexcept
21533 {
21534 return is_number_unsigned() ? &m_data.m_value.number_unsigned : nullptr;
21535 }
21536
21537 /// get a pointer to the value (floating-point number)
21538 number_float_t* get_impl_ptr(number_float_t* /*unused*/) noexcept
21539 {
21540 return is_number_float() ? &m_data.m_value.number_float : nullptr;
21541 }
21542
21543 /// get a pointer to the value (floating-point number)
21544 constexpr const number_float_t* get_impl_ptr(const number_float_t* /*unused*/) const noexcept
21545 {
21546 return is_number_float() ? &m_data.m_value.number_float : nullptr;
21547 }
21548
21549 /// get a pointer to the value (binary)
21550 binary_t* get_impl_ptr(binary_t* /*unused*/) noexcept
21551 {
21552 return is_binary() ? m_data.m_value.binary : nullptr;
21553 }
21554
21555 /// get a pointer to the value (binary)
21556 constexpr const binary_t* get_impl_ptr(const binary_t* /*unused*/) const noexcept
21557 {
21558 return is_binary() ? m_data.m_value.binary : nullptr;
21559 }
21560
21561 /*!
21562 @brief helper function to implement get_ref()
21563
21564 This function helps to implement get_ref() without code duplication for
21565 const and non-const overloads
21566
21567 @tparam ThisType will be deduced as `basic_json` or `const basic_json`
21568
21569 @throw type_error.303 if ReferenceType does not match underlying value
21570 type of the current JSON
21571 */
21572 template<typename ReferenceType, typename ThisType>
21573 static ReferenceType get_ref_impl(ThisType& obj)
21574 {
21575 // delegate the call to get_ptr<>()
21576 auto* ptr = obj.template get_ptr<typename std::add_pointer<ReferenceType>::type>();
21577
21578 if (JSON_HEDLEY_LIKELY(ptr != nullptr))
21579 {
21580 return *ptr;
21581 }
21582
21583 JSON_THROW(type_error::create(303, detail::concat("incompatible ReferenceType for get_ref, actual type is ", obj.type_name()), &obj));
21584 }
21585
21586 public:
21587 /// @name value access
21588 /// Direct access to the stored value of a JSON value.
21589 /// @{
21590
21591 /// @brief get a pointer value (implicit)
21592 /// @sa https://json.nlohmann.me/api/basic_json/get_ptr/
21593 template<typename PointerType, typename std::enable_if<
21594 std::is_pointer<PointerType>::value, int>::type = 0>
21595 auto get_ptr() noexcept -> decltype(std::declval<basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
21596 {
21597 // delegate the call to get_impl_ptr<>()
21598 return get_impl_ptr(static_cast<PointerType>(nullptr));
21599 }
21600
21601 /// @brief get a pointer value (implicit)
21602 /// @sa https://json.nlohmann.me/api/basic_json/get_ptr/
21603 template < typename PointerType, typename std::enable_if <
21604 std::is_pointer<PointerType>::value&&
21605 std::is_const<typename std::remove_pointer<PointerType>::type>::value, int >::type = 0 >
21606 constexpr auto get_ptr() const noexcept -> decltype(std::declval<const basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
21607 {
21608 // delegate the call to get_impl_ptr<>() const
21609 return get_impl_ptr(static_cast<PointerType>(nullptr));
21610 }
21611
21612 private:
21613 /*!
21614 @brief get a value (explicit)
21615
21616 Explicit type conversion between the JSON value and a compatible value
21617 which is [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
21618 and [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
21619 The value is converted by calling the @ref json_serializer<ValueType>
21620 `from_json()` method.
21621
21622 The function is equivalent to executing
21623 @code {.cpp}
21624 ValueType ret;
21625 JSONSerializer<ValueType>::from_json(*this, ret);
21626 return ret;
21627 @endcode
21628
21629 This overloads is chosen if:
21630 - @a ValueType is not @ref basic_json,
21631 - @ref json_serializer<ValueType> has a `from_json()` method of the form
21632 `void from_json(const basic_json&, ValueType&)`, and
21633 - @ref json_serializer<ValueType> does not have a `from_json()` method of
21634 the form `ValueType from_json(const basic_json&)`
21635
21636 @tparam ValueType the returned value type
21637
21638 @return copy of the JSON value, converted to @a ValueType
21639
21640 @throw what @ref json_serializer<ValueType> `from_json()` method throws
21641
21642 @liveexample{The example below shows several conversions from JSON values
21643 to other types. There a few things to note: (1) Floating-point numbers can
21644 be converted to integers\, (2) A JSON array can be converted to a standard
21645 `std::vector<short>`\, (3) A JSON object can be converted to C++
21646 associative containers such as `std::unordered_map<std::string\,
21647 json>`.,get__ValueType_const}
21648
21649 @since version 2.1.0
21650 */
21651 template < typename ValueType,
21652 detail::enable_if_t <
21653 detail::is_default_constructible<ValueType>::value&&
21654 detail::has_from_json<basic_json_t, ValueType>::value,
21655 int > = 0 >
21656 ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept(
21657 JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), std::declval<ValueType&>())))
21658 {
21659 auto ret = ValueType();
21660 JSONSerializer<ValueType>::from_json(*this, ret);
21661 return ret;
21662 }
21663
21664 /*!
21665 @brief get a value (explicit); special case
21666
21667 Explicit type conversion between the JSON value and a compatible value
21668 which is **not** [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
21669 and **not** [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
21670 The value is converted by calling the @ref json_serializer<ValueType>
21671 `from_json()` method.
21672
21673 The function is equivalent to executing
21674 @code {.cpp}
21675 return JSONSerializer<ValueType>::from_json(*this);
21676 @endcode
21677
21678 This overloads is chosen if:
21679 - @a ValueType is not @ref basic_json and
21680 - @ref json_serializer<ValueType> has a `from_json()` method of the form
21681 `ValueType from_json(const basic_json&)`
21682
21683 @note If @ref json_serializer<ValueType> has both overloads of
21684 `from_json()`, this one is chosen.
21685
21686 @tparam ValueType the returned value type
21687
21688 @return copy of the JSON value, converted to @a ValueType
21689
21690 @throw what @ref json_serializer<ValueType> `from_json()` method throws
21691
21692 @since version 2.1.0
21693 */
21694 template < typename ValueType,
21695 detail::enable_if_t <
21696 detail::has_non_default_from_json<basic_json_t, ValueType>::value,
21697 int > = 0 >
21698 ValueType get_impl(detail::priority_tag<1> /*unused*/) const noexcept(noexcept(
21699 JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>())))
21700 {
21701 return JSONSerializer<ValueType>::from_json(*this);
21702 }
21703
21704 /*!
21705 @brief get special-case overload
21706
21707 This overloads converts the current @ref basic_json in a different
21708 @ref basic_json type
21709
21710 @tparam BasicJsonType == @ref basic_json
21711
21712 @return a copy of *this, converted into @a BasicJsonType
21713
21714 @complexity Depending on the implementation of the called `from_json()`
21715 method.
21716
21717 @since version 3.2.0
21718 */
21719 template < typename BasicJsonType,
21720 detail::enable_if_t <
21721 detail::is_basic_json<BasicJsonType>::value,
21722 int > = 0 >
21723 BasicJsonType get_impl(detail::priority_tag<2> /*unused*/) const
21724 {
21725 return *this;
21726 }
21727
21728 /*!
21729 @brief get special-case overload
21730
21731 This overloads avoids a lot of template boilerplate, it can be seen as the
21732 identity method
21733
21734 @tparam BasicJsonType == @ref basic_json
21735
21736 @return a copy of *this
21737
21738 @complexity Constant.
21739
21740 @since version 2.1.0
21741 */
21742 template<typename BasicJsonType,
21743 detail::enable_if_t<
21744 std::is_same<BasicJsonType, basic_json_t>::value,
21745 int> = 0>
21746 basic_json get_impl(detail::priority_tag<3> /*unused*/) const
21747 {
21748 return *this;
21749 }
21750
21751 /*!
21752 @brief get a pointer value (explicit)
21753 @copydoc get()
21754 */
21755 template<typename PointerType,
21756 detail::enable_if_t<
21757 std::is_pointer<PointerType>::value,
21758 int> = 0>
21759 constexpr auto get_impl(detail::priority_tag<4> /*unused*/) const noexcept
21760 -> decltype(std::declval<const basic_json_t&>().template get_ptr<PointerType>())
21761 {
21762 // delegate the call to get_ptr
21763 return get_ptr<PointerType>();
21764 }
21765
21766 public:
21767 /*!
21768 @brief get a (pointer) value (explicit)
21769
21770 Performs explicit type conversion between the JSON value and a compatible value if required.
21771
21772 - If the requested type is a pointer to the internally stored JSON value that pointer is returned.
21773 No copies are made.
21774
21775 - If the requested type is the current @ref basic_json, or a different @ref basic_json convertible
21776 from the current @ref basic_json.
21777
21778 - Otherwise the value is converted by calling the @ref json_serializer<ValueType> `from_json()`
21779 method.
21780
21781 @tparam ValueTypeCV the provided value type
21782 @tparam ValueType the returned value type
21783
21784 @return copy of the JSON value, converted to @tparam ValueType if necessary
21785
21786 @throw what @ref json_serializer<ValueType> `from_json()` method throws if conversion is required
21787
21788 @since version 2.1.0
21789 */
21790 template < typename ValueTypeCV, typename ValueType = detail::uncvref_t<ValueTypeCV>>
21791 #if defined(JSON_HAS_CPP_14)
21792 constexpr
21793 #endif
21794 auto get() const noexcept(
21795 noexcept(std::declval<const basic_json_t&>().template get_impl<ValueType>(detail::priority_tag<4> {})))
21796 -> decltype(std::declval<const basic_json_t&>().template get_impl<ValueType>(detail::priority_tag<4> {}))
21797 {
21798 // we cannot static_assert on ValueTypeCV being non-const, because
21799 // there is support for get<const basic_json_t>(), which is why we
21800 // still need the uncvref
21801 static_assert(!std::is_reference<ValueTypeCV>::value,
21802 "get() cannot be used with reference types, you might want to use get_ref()");
21803 return get_impl<ValueType>(detail::priority_tag<4> {});
21804 }
21805
21806 /*!
21807 @brief get a pointer value (explicit)
21808
21809 Explicit pointer access to the internally stored JSON value. No copies are
21810 made.
21811
21812 @warning The pointer becomes invalid if the underlying JSON object
21813 changes.
21814
21815 @tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
21816 object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
21817 @ref number_unsigned_t, or @ref number_float_t.
21818
21819 @return pointer to the internally stored JSON value if the requested
21820 pointer type @a PointerType fits to the JSON value; `nullptr` otherwise
21821
21822 @complexity Constant.
21823
21824 @liveexample{The example below shows how pointers to internal values of a
21825 JSON value can be requested. Note that no type conversions are made and a
21826 `nullptr` is returned if the value and the requested pointer type does not
21827 match.,get__PointerType}
21828
21829 @sa see @ref get_ptr() for explicit pointer-member access
21830
21831 @since version 1.0.0
21832 */
21833 template<typename PointerType, typename std::enable_if<
21834 std::is_pointer<PointerType>::value, int>::type = 0>
21835 auto get() noexcept -> decltype(std::declval<basic_json_t&>().template get_ptr<PointerType>())
21836 {
21837 // delegate the call to get_ptr
21838 return get_ptr<PointerType>();
21839 }
21840
21841 /// @brief get a value (explicit)
21842 /// @sa https://json.nlohmann.me/api/basic_json/get_to/
21843 template < typename ValueType,
21844 detail::enable_if_t <
21845 !detail::is_basic_json<ValueType>::value&&
21846 detail::has_from_json<basic_json_t, ValueType>::value,
21847 int > = 0 >
21848 ValueType & get_to(ValueType& v) const noexcept(noexcept(
21849 JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), v)))
21850 {
21851 JSONSerializer<ValueType>::from_json(*this, v);
21852 return v;
21853 }
21854
21855 // specialization to allow calling get_to with a basic_json value
21856 // see https://github.com/nlohmann/json/issues/2175
21857 template<typename ValueType,
21858 detail::enable_if_t <
21859 detail::is_basic_json<ValueType>::value,
21860 int> = 0>
21861 ValueType & get_to(ValueType& v) const
21862 {
21863 v = *this;
21864 return v;
21865 }
21866
21867 template <
21868 typename T, std::size_t N,
21869 typename Array = T (&)[N], // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
21870 detail::enable_if_t <
21871 detail::has_from_json<basic_json_t, Array>::value, int > = 0 >
21872 Array get_to(T (&v)[N]) const // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
21873 noexcept(noexcept(JSONSerializer<Array>::from_json(
21874 std::declval<const basic_json_t&>(), v)))
21875 {
21876 JSONSerializer<Array>::from_json(*this, v);
21877 return v;
21878 }
21879
21880 /// @brief get a reference value (implicit)
21881 /// @sa https://json.nlohmann.me/api/basic_json/get_ref/
21882 template<typename ReferenceType, typename std::enable_if<
21883 std::is_reference<ReferenceType>::value, int>::type = 0>
21884 ReferenceType get_ref()
21885 {
21886 // delegate call to get_ref_impl
21887 return get_ref_impl<ReferenceType>(*this);
21888 }
21889
21890 /// @brief get a reference value (implicit)
21891 /// @sa https://json.nlohmann.me/api/basic_json/get_ref/
21892 template < typename ReferenceType, typename std::enable_if <
21893 std::is_reference<ReferenceType>::value&&
21894 std::is_const<typename std::remove_reference<ReferenceType>::type>::value, int >::type = 0 >
21895 ReferenceType get_ref() const
21896 {
21897 // delegate call to get_ref_impl
21898 return get_ref_impl<ReferenceType>(*this);
21899 }
21900
21901 /*!
21902 @brief get a value (implicit)
21903
21904 Implicit type conversion between the JSON value and a compatible value.
21905 The call is realized by calling @ref get() const.
21906
21907 @tparam ValueType non-pointer type compatible to the JSON value, for
21908 instance `int` for JSON integer numbers, `bool` for JSON booleans, or
21909 `std::vector` types for JSON arrays. The character type of @ref string_t
21910 as well as an initializer list of this type is excluded to avoid
21911 ambiguities as these types implicitly convert to `std::string`.
21912
21913 @return copy of the JSON value, converted to type @a ValueType
21914
21915 @throw type_error.302 in case passed type @a ValueType is incompatible
21916 to the JSON value type (e.g., the JSON value is of type boolean, but a
21917 string is requested); see example below
21918
21919 @complexity Linear in the size of the JSON value.
21920
21921 @liveexample{The example below shows several conversions from JSON values
21922 to other types. There a few things to note: (1) Floating-point numbers can
21923 be converted to integers\, (2) A JSON array can be converted to a standard
21924 `std::vector<short>`\, (3) A JSON object can be converted to C++
21925 associative containers such as `std::unordered_map<std::string\,
21926 json>`.,operator__ValueType}
21927
21928 @since version 1.0.0
21929 */
21930 template < typename ValueType, typename std::enable_if <
21931 detail::conjunction <
21932 detail::negation<std::is_pointer<ValueType>>,
21933 detail::negation<std::is_same<ValueType, std::nullptr_t>>,
21934 detail::negation<std::is_same<ValueType, detail::json_ref<basic_json>>>,
21935 detail::negation<std::is_same<ValueType, typename string_t::value_type>>,
21936 detail::negation<detail::is_basic_json<ValueType>>,
21937 detail::negation<std::is_same<ValueType, std::initializer_list<typename string_t::value_type>>>,
21938 #if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER <= 1914))
21939 detail::negation<std::is_same<ValueType, std::string_view>>,
21940 #endif
21941 #if defined(JSON_HAS_CPP_17) && JSON_HAS_STATIC_RTTI
21942 detail::negation<std::is_same<ValueType, std::any>>,
21943 #endif
21944 detail::is_detected_lazy<detail::get_template_function, const basic_json_t&, ValueType>
21945 >::value, int >::type = 0 >
21946 JSON_EXPLICIT operator ValueType() const
21947 {
21948 // delegate the call to get<>() const
21949 return get<ValueType>();
21950 }
21951
21952 /// @brief get a binary value
21953 /// @sa https://json.nlohmann.me/api/basic_json/get_binary/
21954 binary_t& get_binary()
21955 {
21956 if (!is_binary())
21957 {
21958 JSON_THROW(type_error::create(302, detail::concat("type must be binary, but is ", type_name()), this));
21959 }
21960
21961 return *get_ptr<binary_t*>();
21962 }
21963
21964 /// @brief get a binary value
21965 /// @sa https://json.nlohmann.me/api/basic_json/get_binary/
21966 const binary_t& get_binary() const
21967 {
21968 if (!is_binary())
21969 {
21970 JSON_THROW(type_error::create(302, detail::concat("type must be binary, but is ", type_name()), this));
21971 }
21972
21973 return *get_ptr<const binary_t*>();
21974 }
21975
21976 /// @}
21977
21978 ////////////////////
21979 // element access //
21980 ////////////////////
21981
21982 /// @name element access
21983 /// Access to the JSON value.
21984 /// @{
21985
21986 /// @brief access specified array element with bounds checking
21987 /// @sa https://json.nlohmann.me/api/basic_json/at/
21988 reference at(size_type idx)
21989 {
21990 // at only works for arrays
21991 if (JSON_HEDLEY_LIKELY(is_array()))
21992 {
21993 JSON_TRY
21994 {
21995 return set_parent(m_data.m_value.array->at(idx));
21996 }
21997 JSON_CATCH (std::out_of_range&)
21998 {
21999 // create better exception explanation
22000 JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
22001 } // cppcheck-suppress[missingReturn]
22002 }
22003 else
22004 {
22005 JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
22006 }
22007 }
22008
22009 /// @brief access specified array element with bounds checking
22010 /// @sa https://json.nlohmann.me/api/basic_json/at/
22011 const_reference at(size_type idx) const
22012 {
22013 // at only works for arrays
22014 if (JSON_HEDLEY_LIKELY(is_array()))
22015 {
22016 JSON_TRY
22017 {
22018 return m_data.m_value.array->at(idx);
22019 }
22020 JSON_CATCH (std::out_of_range&)
22021 {
22022 // create better exception explanation
22023 JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
22024 } // cppcheck-suppress[missingReturn]
22025 }
22026 else
22027 {
22028 JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
22029 }
22030 }
22031
22032 /// @brief access specified object element with bounds checking
22033 /// @sa https://json.nlohmann.me/api/basic_json/at/
22034 reference at(const typename object_t::key_type& key)
22035 {
22036 // at only works for objects
22037 if (JSON_HEDLEY_UNLIKELY(!is_object()))
22038 {
22039 JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
22040 }
22041
22042 auto it = m_data.m_value.object->find(key);
22043 if (it == m_data.m_value.object->end())
22044 {
22045 JSON_THROW(out_of_range::create(403, detail::concat("key '", key, "' not found"), this));
22046 }
22047 return set_parent(it->second);
22048 }
22049
22050 /// @brief access specified object element with bounds checking
22051 /// @sa https://json.nlohmann.me/api/basic_json/at/
22052 template<class KeyType, detail::enable_if_t<
22053 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
22054 reference at(KeyType && key)
22055 {
22056 // at only works for objects
22057 if (JSON_HEDLEY_UNLIKELY(!is_object()))
22058 {
22059 JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
22060 }
22061
22062 auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
22063 if (it == m_data.m_value.object->end())
22064 {
22065 JSON_THROW(out_of_range::create(403, detail::concat("key '", string_t(std::forward<KeyType>(key)), "' not found"), this));
22066 }
22067 return set_parent(it->second);
22068 }
22069
22070 /// @brief access specified object element with bounds checking
22071 /// @sa https://json.nlohmann.me/api/basic_json/at/
22072 const_reference at(const typename object_t::key_type& key) const
22073 {
22074 // at only works for objects
22075 if (JSON_HEDLEY_UNLIKELY(!is_object()))
22076 {
22077 JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
22078 }
22079
22080 auto it = m_data.m_value.object->find(key);
22081 if (it == m_data.m_value.object->end())
22082 {
22083 JSON_THROW(out_of_range::create(403, detail::concat("key '", key, "' not found"), this));
22084 }
22085 return it->second;
22086 }
22087
22088 /// @brief access specified object element with bounds checking
22089 /// @sa https://json.nlohmann.me/api/basic_json/at/
22090 template<class KeyType, detail::enable_if_t<
22091 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
22092 const_reference at(KeyType && key) const
22093 {
22094 // at only works for objects
22095 if (JSON_HEDLEY_UNLIKELY(!is_object()))
22096 {
22097 JSON_THROW(type_error::create(304, detail::concat("cannot use at() with ", type_name()), this));
22098 }
22099
22100 auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
22101 if (it == m_data.m_value.object->end())
22102 {
22103 JSON_THROW(out_of_range::create(403, detail::concat("key '", string_t(std::forward<KeyType>(key)), "' not found"), this));
22104 }
22105 return it->second;
22106 }
22107
22108 /// @brief access specified array element
22109 /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
22110 reference operator[](size_type idx)
22111 {
22112 // implicitly convert null value to an empty array
22113 if (is_null())
22114 {
22115 m_data.m_type = value_t::array;
22116 m_data.m_value.array = create<array_t>();
22117 assert_invariant();
22118 }
22119
22120 // operator[] only works for arrays
22121 if (JSON_HEDLEY_LIKELY(is_array()))
22122 {
22123 // fill up array with null values if given idx is outside range
22124 if (idx >= m_data.m_value.array->size())
22125 {
22126 #if JSON_DIAGNOSTICS
22127 // remember array size & capacity before resizing
22128 const auto old_size = m_data.m_value.array->size();
22129 const auto old_capacity = m_data.m_value.array->capacity();
22130 #endif
22131 m_data.m_value.array->resize(idx + 1);
22132
22133 #if JSON_DIAGNOSTICS
22134 if (JSON_HEDLEY_UNLIKELY(m_data.m_value.array->capacity() != old_capacity))
22135 {
22136 // capacity has changed: update all parents
22137 set_parents();
22138 }
22139 else
22140 {
22141 // set parent for values added above
22142 set_parents(begin() + static_cast<typename iterator::difference_type>(old_size), static_cast<typename iterator::difference_type>(idx + 1 - old_size));
22143 }
22144 #endif
22145 assert_invariant();
22146 }
22147
22148 return m_data.m_value.array->operator[](idx);
22149 }
22150
22151 JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a numeric argument with ", type_name()), this));
22152 }
22153
22154 /// @brief access specified array element
22155 /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
22156 const_reference operator[](size_type idx) const
22157 {
22158 // const operator[] only works for arrays
22159 if (JSON_HEDLEY_LIKELY(is_array()))
22160 {
22161 return m_data.m_value.array->operator[](idx);
22162 }
22163
22164 JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a numeric argument with ", type_name()), this));
22165 }
22166
22167 /// @brief access specified object element
22168 /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
22169 reference operator[](typename object_t::key_type key) // NOLINT(performance-unnecessary-value-param)
22170 {
22171 // implicitly convert null value to an empty object
22172 if (is_null())
22173 {
22174 m_data.m_type = value_t::object;
22175 m_data.m_value.object = create<object_t>();
22176 assert_invariant();
22177 }
22178
22179 // operator[] only works for objects
22180 if (JSON_HEDLEY_LIKELY(is_object()))
22181 {
22182 auto result = m_data.m_value.object->emplace(std::move(key), nullptr);
22183 return set_parent(result.first->second);
22184 }
22185
22186 JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
22187 }
22188
22189 /// @brief access specified object element
22190 /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
22191 const_reference operator[](const typename object_t::key_type& key) const
22192 {
22193 // const operator[] only works for objects
22194 if (JSON_HEDLEY_LIKELY(is_object()))
22195 {
22196 auto it = m_data.m_value.object->find(key);
22197 JSON_ASSERT(it != m_data.m_value.object->end());
22198 return it->second;
22199 }
22200
22201 JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
22202 }
22203
22204 // these two functions resolve a (const) char * ambiguity affecting Clang and MSVC
22205 // (they seemingly cannot be constrained to resolve the ambiguity)
22206 template<typename T>
22207 reference operator[](T* key)
22208 {
22209 return operator[](typename object_t::key_type(key));
22210 }
22211
22212 template<typename T>
22213 const_reference operator[](T* key) const
22214 {
22215 return operator[](typename object_t::key_type(key));
22216 }
22217
22218 /// @brief access specified object element
22219 /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
22220 template<class KeyType, detail::enable_if_t<
22221 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int > = 0 >
22222 reference operator[](KeyType && key)
22223 {
22224 // implicitly convert null value to an empty object
22225 if (is_null())
22226 {
22227 m_data.m_type = value_t::object;
22228 m_data.m_value.object = create<object_t>();
22229 assert_invariant();
22230 }
22231
22232 // operator[] only works for objects
22233 if (JSON_HEDLEY_LIKELY(is_object()))
22234 {
22235 auto result = m_data.m_value.object->emplace(std::forward<KeyType>(key), nullptr);
22236 return set_parent(result.first->second);
22237 }
22238
22239 JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
22240 }
22241
22242 /// @brief access specified object element
22243 /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
22244 template<class KeyType, detail::enable_if_t<
22245 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int > = 0 >
22246 const_reference operator[](KeyType && key) const
22247 {
22248 // const operator[] only works for objects
22249 if (JSON_HEDLEY_LIKELY(is_object()))
22250 {
22251 auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
22252 JSON_ASSERT(it != m_data.m_value.object->end());
22253 return it->second;
22254 }
22255
22256 JSON_THROW(type_error::create(305, detail::concat("cannot use operator[] with a string argument with ", type_name()), this));
22257 }
22258
22259 private:
22260 template<typename KeyType>
22261 using is_comparable_with_object_key = detail::is_comparable <
22262 object_comparator_t, const typename object_t::key_type&, KeyType >;
22263
22264 template<typename ValueType>
22265 using value_return_type = std::conditional <
22266 detail::is_c_string_uncvref<ValueType>::value,
22267 string_t, typename std::decay<ValueType>::type >;
22268
22269 public:
22270 /// @brief access specified object element with default value
22271 /// @sa https://json.nlohmann.me/api/basic_json/value/
22272 template < class ValueType, detail::enable_if_t <
22273 !detail::is_transparent<object_comparator_t>::value
22274 && detail::is_getable<basic_json_t, ValueType>::value
22275 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22276 ValueType value(const typename object_t::key_type& key, const ValueType& default_value) const
22277 {
22278 // value only works for objects
22279 if (JSON_HEDLEY_LIKELY(is_object()))
22280 {
22281 // if key is found, return value and given default value otherwise
22282 const auto it = find(key);
22283 if (it != end())
22284 {
22285 return it->template get<ValueType>();
22286 }
22287
22288 return default_value;
22289 }
22290
22291 JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
22292 }
22293
22294 /// @brief access specified object element with default value
22295 /// @sa https://json.nlohmann.me/api/basic_json/value/
22296 template < class ValueType, class ReturnType = typename value_return_type<ValueType>::type,
22297 detail::enable_if_t <
22298 !detail::is_transparent<object_comparator_t>::value
22299 && detail::is_getable<basic_json_t, ReturnType>::value
22300 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22301 ReturnType value(const typename object_t::key_type& key, ValueType && default_value) const
22302 {
22303 // value only works for objects
22304 if (JSON_HEDLEY_LIKELY(is_object()))
22305 {
22306 // if key is found, return value and given default value otherwise
22307 const auto it = find(key);
22308 if (it != end())
22309 {
22310 return it->template get<ReturnType>();
22311 }
22312
22313 return std::forward<ValueType>(default_value);
22314 }
22315
22316 JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
22317 }
22318
22319 /// @brief access specified object element with default value
22320 /// @sa https://json.nlohmann.me/api/basic_json/value/
22321 template < class ValueType, class KeyType, detail::enable_if_t <
22322 detail::is_transparent<object_comparator_t>::value
22323 && !detail::is_json_pointer<KeyType>::value
22324 && is_comparable_with_object_key<KeyType>::value
22325 && detail::is_getable<basic_json_t, ValueType>::value
22326 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22327 ValueType value(KeyType && key, const ValueType& default_value) const
22328 {
22329 // value only works for objects
22330 if (JSON_HEDLEY_LIKELY(is_object()))
22331 {
22332 // if key is found, return value and given default value otherwise
22333 const auto it = find(std::forward<KeyType>(key));
22334 if (it != end())
22335 {
22336 return it->template get<ValueType>();
22337 }
22338
22339 return default_value;
22340 }
22341
22342 JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
22343 }
22344
22345 /// @brief access specified object element via JSON Pointer with default value
22346 /// @sa https://json.nlohmann.me/api/basic_json/value/
22347 template < class ValueType, class KeyType, class ReturnType = typename value_return_type<ValueType>::type,
22348 detail::enable_if_t <
22349 detail::is_transparent<object_comparator_t>::value
22350 && !detail::is_json_pointer<KeyType>::value
22351 && is_comparable_with_object_key<KeyType>::value
22352 && detail::is_getable<basic_json_t, ReturnType>::value
22353 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22354 ReturnType value(KeyType && key, ValueType && default_value) const
22355 {
22356 // value only works for objects
22357 if (JSON_HEDLEY_LIKELY(is_object()))
22358 {
22359 // if key is found, return value and given default value otherwise
22360 const auto it = find(std::forward<KeyType>(key));
22361 if (it != end())
22362 {
22363 return it->template get<ReturnType>();
22364 }
22365
22366 return std::forward<ValueType>(default_value);
22367 }
22368
22369 JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
22370 }
22371
22372 /// @brief access specified object element via JSON Pointer with default value
22373 /// @sa https://json.nlohmann.me/api/basic_json/value/
22374 template < class ValueType, detail::enable_if_t <
22375 detail::is_getable<basic_json_t, ValueType>::value
22376 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22377 ValueType value(const json_pointer& ptr, const ValueType& default_value) const
22378 {
22379 // value only works for objects
22380 if (JSON_HEDLEY_LIKELY(is_object()))
22381 {
22382 // if pointer resolves a value, return it or use default value
22383 JSON_TRY
22384 {
22385 return ptr.get_checked(this).template get<ValueType>();
22386 }
22387 JSON_INTERNAL_CATCH (out_of_range&)
22388 {
22389 return default_value;
22390 }
22391 }
22392
22393 JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
22394 }
22395
22396 /// @brief access specified object element via JSON Pointer with default value
22397 /// @sa https://json.nlohmann.me/api/basic_json/value/
22398 template < class ValueType, class ReturnType = typename value_return_type<ValueType>::type,
22399 detail::enable_if_t <
22400 detail::is_getable<basic_json_t, ReturnType>::value
22401 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22402 ReturnType value(const json_pointer& ptr, ValueType && default_value) const
22403 {
22404 // value only works for objects
22405 if (JSON_HEDLEY_LIKELY(is_object()))
22406 {
22407 // if pointer resolves a value, return it or use default value
22408 JSON_TRY
22409 {
22410 return ptr.get_checked(this).template get<ReturnType>();
22411 }
22412 JSON_INTERNAL_CATCH (out_of_range&)
22413 {
22414 return std::forward<ValueType>(default_value);
22415 }
22416 }
22417
22418 JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
22419 }
22420
22421 template < class ValueType, class BasicJsonType, detail::enable_if_t <
22422 detail::is_basic_json<BasicJsonType>::value
22423 && detail::is_getable<basic_json_t, ValueType>::value
22424 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22425 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
22426 ValueType value(const ::nlohmann::json_pointer<BasicJsonType>& ptr, const ValueType& default_value) const
22427 {
22428 return value(ptr.convert(), default_value);
22429 }
22430
22431 template < class ValueType, class BasicJsonType, class ReturnType = typename value_return_type<ValueType>::type,
22432 detail::enable_if_t <
22433 detail::is_basic_json<BasicJsonType>::value
22434 && detail::is_getable<basic_json_t, ReturnType>::value
22435 && !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
22436 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
22437 ReturnType value(const ::nlohmann::json_pointer<BasicJsonType>& ptr, ValueType && default_value) const
22438 {
22439 return value(ptr.convert(), std::forward<ValueType>(default_value));
22440 }
22441
22442 /// @brief access the first element
22443 /// @sa https://json.nlohmann.me/api/basic_json/front/
22444 reference front()
22445 {
22446 return *begin();
22447 }
22448
22449 /// @brief access the first element
22450 /// @sa https://json.nlohmann.me/api/basic_json/front/
22451 const_reference front() const
22452 {
22453 return *cbegin();
22454 }
22455
22456 /// @brief access the last element
22457 /// @sa https://json.nlohmann.me/api/basic_json/back/
22458 reference back()
22459 {
22460 auto tmp = end();
22461 --tmp;
22462 return *tmp;
22463 }
22464
22465 /// @brief access the last element
22466 /// @sa https://json.nlohmann.me/api/basic_json/back/
22467 const_reference back() const
22468 {
22469 auto tmp = cend();
22470 --tmp;
22471 return *tmp;
22472 }
22473
22474 /// @brief remove element given an iterator
22475 /// @sa https://json.nlohmann.me/api/basic_json/erase/
22476 template < class IteratorType, detail::enable_if_t <
22477 std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
22478 std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int > = 0 >
22479 IteratorType erase(IteratorType pos) // NOLINT(performance-unnecessary-value-param)
22480 {
22481 // make sure iterator fits the current value
22482 if (JSON_HEDLEY_UNLIKELY(this != pos.m_object))
22483 {
22484 JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
22485 }
22486
22487 IteratorType result = end();
22488
22489 switch (m_data.m_type)
22490 {
22491 case value_t::boolean:
22492 case value_t::number_float:
22493 case value_t::number_integer:
22494 case value_t::number_unsigned:
22495 case value_t::string:
22496 case value_t::binary:
22497 {
22498 if (JSON_HEDLEY_UNLIKELY(!pos.m_it.primitive_iterator.is_begin()))
22499 {
22500 JSON_THROW(invalid_iterator::create(205, "iterator out of range", this));
22501 }
22502
22503 if (is_string())
22504 {
22505 AllocatorType<string_t> alloc;
22506 std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.string);
22507 std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.string, 1);
22508 m_data.m_value.string = nullptr;
22509 }
22510 else if (is_binary())
22511 {
22512 AllocatorType<binary_t> alloc;
22513 std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.binary);
22514 std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.binary, 1);
22515 m_data.m_value.binary = nullptr;
22516 }
22517
22518 m_data.m_type = value_t::null;
22519 assert_invariant();
22520 break;
22521 }
22522
22523 case value_t::object:
22524 {
22525 result.m_it.object_iterator = m_data.m_value.object->erase(pos.m_it.object_iterator);
22526 break;
22527 }
22528
22529 case value_t::array:
22530 {
22531 result.m_it.array_iterator = m_data.m_value.array->erase(pos.m_it.array_iterator);
22532 break;
22533 }
22534
22535 case value_t::null:
22536 case value_t::discarded:
22537 default:
22538 JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
22539 }
22540
22541 return result;
22542 }
22543
22544 /// @brief remove elements given an iterator range
22545 /// @sa https://json.nlohmann.me/api/basic_json/erase/
22546 template < class IteratorType, detail::enable_if_t <
22547 std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
22548 std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int > = 0 >
22549 IteratorType erase(IteratorType first, IteratorType last) // NOLINT(performance-unnecessary-value-param)
22550 {
22551 // make sure iterator fits the current value
22552 if (JSON_HEDLEY_UNLIKELY(this != first.m_object || this != last.m_object))
22553 {
22554 JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value", this));
22555 }
22556
22557 IteratorType result = end();
22558
22559 switch (m_data.m_type)
22560 {
22561 case value_t::boolean:
22562 case value_t::number_float:
22563 case value_t::number_integer:
22564 case value_t::number_unsigned:
22565 case value_t::string:
22566 case value_t::binary:
22567 {
22568 if (JSON_HEDLEY_LIKELY(!first.m_it.primitive_iterator.is_begin()
22569 || !last.m_it.primitive_iterator.is_end()))
22570 {
22571 JSON_THROW(invalid_iterator::create(204, "iterators out of range", this));
22572 }
22573
22574 if (is_string())
22575 {
22576 AllocatorType<string_t> alloc;
22577 std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.string);
22578 std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.string, 1);
22579 m_data.m_value.string = nullptr;
22580 }
22581 else if (is_binary())
22582 {
22583 AllocatorType<binary_t> alloc;
22584 std::allocator_traits<decltype(alloc)>::destroy(alloc, m_data.m_value.binary);
22585 std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_data.m_value.binary, 1);
22586 m_data.m_value.binary = nullptr;
22587 }
22588
22589 m_data.m_type = value_t::null;
22590 assert_invariant();
22591 break;
22592 }
22593
22594 case value_t::object:
22595 {
22596 result.m_it.object_iterator = m_data.m_value.object->erase(first.m_it.object_iterator,
22597 last.m_it.object_iterator);
22598 break;
22599 }
22600
22601 case value_t::array:
22602 {
22603 result.m_it.array_iterator = m_data.m_value.array->erase(first.m_it.array_iterator,
22604 last.m_it.array_iterator);
22605 break;
22606 }
22607
22608 case value_t::null:
22609 case value_t::discarded:
22610 default:
22611 JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
22612 }
22613
22614 return result;
22615 }
22616
22617 private:
22618 template < typename KeyType, detail::enable_if_t <
22619 detail::has_erase_with_key_type<basic_json_t, KeyType>::value, int > = 0 >
22620 size_type erase_internal(KeyType && key)
22621 {
22622 // this erase only works for objects
22623 if (JSON_HEDLEY_UNLIKELY(!is_object()))
22624 {
22625 JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
22626 }
22627
22628 return m_data.m_value.object->erase(std::forward<KeyType>(key));
22629 }
22630
22631 template < typename KeyType, detail::enable_if_t <
22632 !detail::has_erase_with_key_type<basic_json_t, KeyType>::value, int > = 0 >
22633 size_type erase_internal(KeyType && key)
22634 {
22635 // this erase only works for objects
22636 if (JSON_HEDLEY_UNLIKELY(!is_object()))
22637 {
22638 JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
22639 }
22640
22641 const auto it = m_data.m_value.object->find(std::forward<KeyType>(key));
22642 if (it != m_data.m_value.object->end())
22643 {
22644 m_data.m_value.object->erase(it);
22645 return 1;
22646 }
22647 return 0;
22648 }
22649
22650 public:
22651
22652 /// @brief remove element from a JSON object given a key
22653 /// @sa https://json.nlohmann.me/api/basic_json/erase/
22654 size_type erase(const typename object_t::key_type& key)
22655 {
22656 // the indirection via erase_internal() is added to avoid making this
22657 // function a template and thus de-rank it during overload resolution
22658 return erase_internal(key);
22659 }
22660
22661 /// @brief remove element from a JSON object given a key
22662 /// @sa https://json.nlohmann.me/api/basic_json/erase/
22663 template<class KeyType, detail::enable_if_t<
22664 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
22665 size_type erase(KeyType && key)
22666 {
22667 return erase_internal(std::forward<KeyType>(key));
22668 }
22669
22670 /// @brief remove element from a JSON array given an index
22671 /// @sa https://json.nlohmann.me/api/basic_json/erase/
22672 void erase(const size_type idx)
22673 {
22674 // this erase only works for arrays
22675 if (JSON_HEDLEY_LIKELY(is_array()))
22676 {
22677 if (JSON_HEDLEY_UNLIKELY(idx >= size()))
22678 {
22679 JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), this));
22680 }
22681
22682 m_data.m_value.array->erase(m_data.m_value.array->begin() + static_cast<difference_type>(idx));
22683 }
22684 else
22685 {
22686 JSON_THROW(type_error::create(307, detail::concat("cannot use erase() with ", type_name()), this));
22687 }
22688 }
22689
22690 /// @}
22691
22692 ////////////
22693 // lookup //
22694 ////////////
22695
22696 /// @name lookup
22697 /// @{
22698
22699 /// @brief find an element in a JSON object
22700 /// @sa https://json.nlohmann.me/api/basic_json/find/
22701 iterator find(const typename object_t::key_type& key)
22702 {
22703 auto result = end();
22704
22705 if (is_object())
22706 {
22707 result.m_it.object_iterator = m_data.m_value.object->find(key);
22708 }
22709
22710 return result;
22711 }
22712
22713 /// @brief find an element in a JSON object
22714 /// @sa https://json.nlohmann.me/api/basic_json/find/
22715 const_iterator find(const typename object_t::key_type& key) const
22716 {
22717 auto result = cend();
22718
22719 if (is_object())
22720 {
22721 result.m_it.object_iterator = m_data.m_value.object->find(key);
22722 }
22723
22724 return result;
22725 }
22726
22727 /// @brief find an element in a JSON object
22728 /// @sa https://json.nlohmann.me/api/basic_json/find/
22729 template<class KeyType, detail::enable_if_t<
22730 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
22731 iterator find(KeyType && key)
22732 {
22733 auto result = end();
22734
22735 if (is_object())
22736 {
22737 result.m_it.object_iterator = m_data.m_value.object->find(std::forward<KeyType>(key));
22738 }
22739
22740 return result;
22741 }
22742
22743 /// @brief find an element in a JSON object
22744 /// @sa https://json.nlohmann.me/api/basic_json/find/
22745 template<class KeyType, detail::enable_if_t<
22746 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
22747 const_iterator find(KeyType && key) const
22748 {
22749 auto result = cend();
22750
22751 if (is_object())
22752 {
22753 result.m_it.object_iterator = m_data.m_value.object->find(std::forward<KeyType>(key));
22754 }
22755
22756 return result;
22757 }
22758
22759 /// @brief returns the number of occurrences of a key in a JSON object
22760 /// @sa https://json.nlohmann.me/api/basic_json/count/
22761 size_type count(const typename object_t::key_type& key) const
22762 {
22763 // return 0 for all nonobject types
22764 return is_object() ? m_data.m_value.object->count(key) : 0;
22765 }
22766
22767 /// @brief returns the number of occurrences of a key in a JSON object
22768 /// @sa https://json.nlohmann.me/api/basic_json/count/
22769 template<class KeyType, detail::enable_if_t<
22770 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
22771 size_type count(KeyType && key) const
22772 {
22773 // return 0 for all nonobject types
22774 return is_object() ? m_data.m_value.object->count(std::forward<KeyType>(key)) : 0;
22775 }
22776
22777 /// @brief check the existence of an element in a JSON object
22778 /// @sa https://json.nlohmann.me/api/basic_json/contains/
22779 bool contains(const typename object_t::key_type& key) const
22780 {
22781 return is_object() && m_data.m_value.object->find(key) != m_data.m_value.object->end();
22782 }
22783
22784 /// @brief check the existence of an element in a JSON object
22785 /// @sa https://json.nlohmann.me/api/basic_json/contains/
22786 template<class KeyType, detail::enable_if_t<
22787 detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
22788 bool contains(KeyType && key) const
22789 {
22790 return is_object() && m_data.m_value.object->find(std::forward<KeyType>(key)) != m_data.m_value.object->end();
22791 }
22792
22793 /// @brief check the existence of an element in a JSON object given a JSON pointer
22794 /// @sa https://json.nlohmann.me/api/basic_json/contains/
22795 bool contains(const json_pointer& ptr) const
22796 {
22797 return ptr.contains(this);
22798 }
22799
22800 template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
22801 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
22802 bool contains(const typename ::nlohmann::json_pointer<BasicJsonType>& ptr) const
22803 {
22804 return ptr.contains(this);
22805 }
22806
22807 /// @}
22808
22809 ///////////////
22810 // iterators //
22811 ///////////////
22812
22813 /// @name iterators
22814 /// @{
22815
22816 /// @brief returns an iterator to the first element
22817 /// @sa https://json.nlohmann.me/api/basic_json/begin/
22818 iterator begin() noexcept
22819 {
22820 iterator result(this);
22821 result.set_begin();
22822 return result;
22823 }
22824
22825 /// @brief returns an iterator to the first element
22826 /// @sa https://json.nlohmann.me/api/basic_json/begin/
22827 const_iterator begin() const noexcept
22828 {
22829 return cbegin();
22830 }
22831
22832 /// @brief returns a const iterator to the first element
22833 /// @sa https://json.nlohmann.me/api/basic_json/cbegin/
22834 const_iterator cbegin() const noexcept
22835 {
22836 const_iterator result(this);
22837 result.set_begin();
22838 return result;
22839 }
22840
22841 /// @brief returns an iterator to one past the last element
22842 /// @sa https://json.nlohmann.me/api/basic_json/end/
22843 iterator end() noexcept
22844 {
22845 iterator result(this);
22846 result.set_end();
22847 return result;
22848 }
22849
22850 /// @brief returns an iterator to one past the last element
22851 /// @sa https://json.nlohmann.me/api/basic_json/end/
22852 const_iterator end() const noexcept
22853 {
22854 return cend();
22855 }
22856
22857 /// @brief returns an iterator to one past the last element
22858 /// @sa https://json.nlohmann.me/api/basic_json/cend/
22859 const_iterator cend() const noexcept
22860 {
22861 const_iterator result(this);
22862 result.set_end();
22863 return result;
22864 }
22865
22866 /// @brief returns an iterator to the reverse-beginning
22867 /// @sa https://json.nlohmann.me/api/basic_json/rbegin/
22868 reverse_iterator rbegin() noexcept
22869 {
22870 return reverse_iterator(end());
22871 }
22872
22873 /// @brief returns an iterator to the reverse-beginning
22874 /// @sa https://json.nlohmann.me/api/basic_json/rbegin/
22875 const_reverse_iterator rbegin() const noexcept
22876 {
22877 return crbegin();
22878 }
22879
22880 /// @brief returns an iterator to the reverse-end
22881 /// @sa https://json.nlohmann.me/api/basic_json/rend/
22882 reverse_iterator rend() noexcept
22883 {
22884 return reverse_iterator(begin());
22885 }
22886
22887 /// @brief returns an iterator to the reverse-end
22888 /// @sa https://json.nlohmann.me/api/basic_json/rend/
22889 const_reverse_iterator rend() const noexcept
22890 {
22891 return crend();
22892 }
22893
22894 /// @brief returns a const reverse iterator to the last element
22895 /// @sa https://json.nlohmann.me/api/basic_json/crbegin/
22896 const_reverse_iterator crbegin() const noexcept
22897 {
22898 return const_reverse_iterator(cend());
22899 }
22900
22901 /// @brief returns a const reverse iterator to one before the first
22902 /// @sa https://json.nlohmann.me/api/basic_json/crend/
22903 const_reverse_iterator crend() const noexcept
22904 {
22905 return const_reverse_iterator(cbegin());
22906 }
22907
22908 public:
22909 /// @brief wrapper to access iterator member functions in range-based for
22910 /// @sa https://json.nlohmann.me/api/basic_json/items/
22911 /// @deprecated This function is deprecated since 3.1.0 and will be removed in
22912 /// version 4.0.0 of the library. Please use @ref items() instead;
22913 /// that is, replace `json::iterator_wrapper(j)` with `j.items()`.
22914 JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items())
22915 static iteration_proxy<iterator> iterator_wrapper(reference ref) noexcept
22916 {
22917 return ref.items();
22918 }
22919
22920 /// @brief wrapper to access iterator member functions in range-based for
22921 /// @sa https://json.nlohmann.me/api/basic_json/items/
22922 /// @deprecated This function is deprecated since 3.1.0 and will be removed in
22923 /// version 4.0.0 of the library. Please use @ref items() instead;
22924 /// that is, replace `json::iterator_wrapper(j)` with `j.items()`.
22925 JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items())
22926 static iteration_proxy<const_iterator> iterator_wrapper(const_reference ref) noexcept
22927 {
22928 return ref.items();
22929 }
22930
22931 /// @brief helper to access iterator member functions in range-based for
22932 /// @sa https://json.nlohmann.me/api/basic_json/items/
22933 iteration_proxy<iterator> items() noexcept
22934 {
22935 return iteration_proxy<iterator>(*this);
22936 }
22937
22938 /// @brief helper to access iterator member functions in range-based for
22939 /// @sa https://json.nlohmann.me/api/basic_json/items/
22940 iteration_proxy<const_iterator> items() const noexcept
22941 {
22942 return iteration_proxy<const_iterator>(*this);
22943 }
22944
22945 /// @}
22946
22947 //////////////
22948 // capacity //
22949 //////////////
22950
22951 /// @name capacity
22952 /// @{
22953
22954 /// @brief checks whether the container is empty.
22955 /// @sa https://json.nlohmann.me/api/basic_json/empty/
22956 bool empty() const noexcept
22957 {
22958 switch (m_data.m_type)
22959 {
22960 case value_t::null:
22961 {
22962 // null values are empty
22963 return true;
22964 }
22965
22966 case value_t::array:
22967 {
22968 // delegate call to array_t::empty()
22969 return m_data.m_value.array->empty();
22970 }
22971
22972 case value_t::object:
22973 {
22974 // delegate call to object_t::empty()
22975 return m_data.m_value.object->empty();
22976 }
22977
22978 case value_t::string:
22979 case value_t::boolean:
22980 case value_t::number_integer:
22981 case value_t::number_unsigned:
22982 case value_t::number_float:
22983 case value_t::binary:
22984 case value_t::discarded:
22985 default:
22986 {
22987 // all other types are nonempty
22988 return false;
22989 }
22990 }
22991 }
22992
22993 /// @brief returns the number of elements
22994 /// @sa https://json.nlohmann.me/api/basic_json/size/
22995 size_type size() const noexcept
22996 {
22997 switch (m_data.m_type)
22998 {
22999 case value_t::null:
23000 {
23001 // null values are empty
23002 return 0;
23003 }
23004
23005 case value_t::array:
23006 {
23007 // delegate call to array_t::size()
23008 return m_data.m_value.array->size();
23009 }
23010
23011 case value_t::object:
23012 {
23013 // delegate call to object_t::size()
23014 return m_data.m_value.object->size();
23015 }
23016
23017 case value_t::string:
23018 case value_t::boolean:
23019 case value_t::number_integer:
23020 case value_t::number_unsigned:
23021 case value_t::number_float:
23022 case value_t::binary:
23023 case value_t::discarded:
23024 default:
23025 {
23026 // all other types have size 1
23027 return 1;
23028 }
23029 }
23030 }
23031
23032 /// @brief returns the maximum possible number of elements
23033 /// @sa https://json.nlohmann.me/api/basic_json/max_size/
23034 size_type max_size() const noexcept
23035 {
23036 switch (m_data.m_type)
23037 {
23038 case value_t::array:
23039 {
23040 // delegate call to array_t::max_size()
23041 return m_data.m_value.array->max_size();
23042 }
23043
23044 case value_t::object:
23045 {
23046 // delegate call to object_t::max_size()
23047 return m_data.m_value.object->max_size();
23048 }
23049
23050 case value_t::null:
23051 case value_t::string:
23052 case value_t::boolean:
23053 case value_t::number_integer:
23054 case value_t::number_unsigned:
23055 case value_t::number_float:
23056 case value_t::binary:
23057 case value_t::discarded:
23058 default:
23059 {
23060 // all other types have max_size() == size()
23061 return size();
23062 }
23063 }
23064 }
23065
23066 /// @}
23067
23068 ///////////////
23069 // modifiers //
23070 ///////////////
23071
23072 /// @name modifiers
23073 /// @{
23074
23075 /// @brief clears the contents
23076 /// @sa https://json.nlohmann.me/api/basic_json/clear/
23077 void clear() noexcept
23078 {
23079 switch (m_data.m_type)
23080 {
23081 case value_t::number_integer:
23082 {
23083 m_data.m_value.number_integer = 0;
23084 break;
23085 }
23086
23087 case value_t::number_unsigned:
23088 {
23089 m_data.m_value.number_unsigned = 0;
23090 break;
23091 }
23092
23093 case value_t::number_float:
23094 {
23095 m_data.m_value.number_float = 0.0;
23096 break;
23097 }
23098
23099 case value_t::boolean:
23100 {
23101 m_data.m_value.boolean = false;
23102 break;
23103 }
23104
23105 case value_t::string:
23106 {
23107 m_data.m_value.string->clear();
23108 break;
23109 }
23110
23111 case value_t::binary:
23112 {
23113 m_data.m_value.binary->clear();
23114 break;
23115 }
23116
23117 case value_t::array:
23118 {
23119 m_data.m_value.array->clear();
23120 break;
23121 }
23122
23123 case value_t::object:
23124 {
23125 m_data.m_value.object->clear();
23126 break;
23127 }
23128
23129 case value_t::null:
23130 case value_t::discarded:
23131 default:
23132 break;
23133 }
23134 }
23135
23136 /// @brief add an object to an array
23137 /// @sa https://json.nlohmann.me/api/basic_json/push_back/
23138 void push_back(basic_json&& val)
23139 {
23140 // push_back only works for null objects or arrays
23141 if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
23142 {
23143 JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
23144 }
23145
23146 // transform null object into an array
23147 if (is_null())
23148 {
23149 m_data.m_type = value_t::array;
23150 m_data.m_value = value_t::array;
23151 assert_invariant();
23152 }
23153
23154 // add element to array (move semantics)
23155 const auto old_capacity = m_data.m_value.array->capacity();
23156 m_data.m_value.array->push_back(std::move(val));
23157 set_parent(m_data.m_value.array->back(), old_capacity);
23158 // if val is moved from, basic_json move constructor marks it null, so we do not call the destructor
23159 }
23160
23161 /// @brief add an object to an array
23162 /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
23163 reference operator+=(basic_json&& val)
23164 {
23165 push_back(std::move(val));
23166 return *this;
23167 }
23168
23169 /// @brief add an object to an array
23170 /// @sa https://json.nlohmann.me/api/basic_json/push_back/
23171 void push_back(const basic_json& val)
23172 {
23173 // push_back only works for null objects or arrays
23174 if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
23175 {
23176 JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
23177 }
23178
23179 // transform null object into an array
23180 if (is_null())
23181 {
23182 m_data.m_type = value_t::array;
23183 m_data.m_value = value_t::array;
23184 assert_invariant();
23185 }
23186
23187 // add element to array
23188 const auto old_capacity = m_data.m_value.array->capacity();
23189 m_data.m_value.array->push_back(val);
23190 set_parent(m_data.m_value.array->back(), old_capacity);
23191 }
23192
23193 /// @brief add an object to an array
23194 /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
23195 reference operator+=(const basic_json& val)
23196 {
23197 push_back(val);
23198 return *this;
23199 }
23200
23201 /// @brief add an object to an object
23202 /// @sa https://json.nlohmann.me/api/basic_json/push_back/
23203 void push_back(const typename object_t::value_type& val)
23204 {
23205 // push_back only works for null objects or objects
23206 if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
23207 {
23208 JSON_THROW(type_error::create(308, detail::concat("cannot use push_back() with ", type_name()), this));
23209 }
23210
23211 // transform null object into an object
23212 if (is_null())
23213 {
23214 m_data.m_type = value_t::object;
23215 m_data.m_value = value_t::object;
23216 assert_invariant();
23217 }
23218
23219 // add element to object
23220 auto res = m_data.m_value.object->insert(val);
23221 set_parent(res.first->second);
23222 }
23223
23224 /// @brief add an object to an object
23225 /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
23226 reference operator+=(const typename object_t::value_type& val)
23227 {
23228 push_back(val);
23229 return *this;
23230 }
23231
23232 /// @brief add an object to an object
23233 /// @sa https://json.nlohmann.me/api/basic_json/push_back/
23234 void push_back(initializer_list_t init)
23235 {
23236 if (is_object() && init.size() == 2 && (*init.begin())->is_string())
23237 {
23238 basic_json&& key = init.begin()->moved_or_copied();
23239 push_back(typename object_t::value_type(
23240 std::move(key.get_ref<string_t&>()), (init.begin() + 1)->moved_or_copied()));
23241 }
23242 else
23243 {
23244 push_back(basic_json(init));
23245 }
23246 }
23247
23248 /// @brief add an object to an object
23249 /// @sa https://json.nlohmann.me/api/basic_json/operator+=/
23250 reference operator+=(initializer_list_t init)
23251 {
23252 push_back(init);
23253 return *this;
23254 }
23255
23256 /// @brief add an object to an array
23257 /// @sa https://json.nlohmann.me/api/basic_json/emplace_back/
23258 template<class... Args>
23259 reference emplace_back(Args&& ... args)
23260 {
23261 // emplace_back only works for null objects or arrays
23262 if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
23263 {
23264 JSON_THROW(type_error::create(311, detail::concat("cannot use emplace_back() with ", type_name()), this));
23265 }
23266
23267 // transform null object into an array
23268 if (is_null())
23269 {
23270 m_data.m_type = value_t::array;
23271 m_data.m_value = value_t::array;
23272 assert_invariant();
23273 }
23274
23275 // add element to array (perfect forwarding)
23276 const auto old_capacity = m_data.m_value.array->capacity();
23277 m_data.m_value.array->emplace_back(std::forward<Args>(args)...);
23278 return set_parent(m_data.m_value.array->back(), old_capacity);
23279 }
23280
23281 /// @brief add an object to an object if key does not exist
23282 /// @sa https://json.nlohmann.me/api/basic_json/emplace/
23283 template<class... Args>
23284 std::pair<iterator, bool> emplace(Args&& ... args)
23285 {
23286 // emplace only works for null objects or arrays
23287 if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
23288 {
23289 JSON_THROW(type_error::create(311, detail::concat("cannot use emplace() with ", type_name()), this));
23290 }
23291
23292 // transform null object into an object
23293 if (is_null())
23294 {
23295 m_data.m_type = value_t::object;
23296 m_data.m_value = value_t::object;
23297 assert_invariant();
23298 }
23299
23300 // add element to array (perfect forwarding)
23301 auto res = m_data.m_value.object->emplace(std::forward<Args>(args)...);
23302 set_parent(res.first->second);
23303
23304 // create result iterator and set iterator to the result of emplace
23305 auto it = begin();
23306 it.m_it.object_iterator = res.first;
23307
23308 // return pair of iterator and boolean
23309 return {it, res.second};
23310 }
23311
23312 /// Helper for insertion of an iterator
23313 /// @note: This uses std::distance to support GCC 4.8,
23314 /// see https://github.com/nlohmann/json/pull/1257
23315 template<typename... Args>
23316 iterator insert_iterator(const_iterator pos, Args&& ... args) // NOLINT(performance-unnecessary-value-param)
23317 {
23318 iterator result(this);
23319 JSON_ASSERT(m_data.m_value.array != nullptr);
23320
23321 auto insert_pos = std::distance(m_data.m_value.array->begin(), pos.m_it.array_iterator);
23322 m_data.m_value.array->insert(pos.m_it.array_iterator, std::forward<Args>(args)...);
23323 result.m_it.array_iterator = m_data.m_value.array->begin() + insert_pos;
23324
23325 // This could have been written as:
23326 // result.m_it.array_iterator = m_data.m_value.array->insert(pos.m_it.array_iterator, cnt, val);
23327 // but the return value of insert is missing in GCC 4.8, so it is written this way instead.
23328
23329 set_parents();
23330 return result;
23331 }
23332
23333 /// @brief inserts element into array
23334 /// @sa https://json.nlohmann.me/api/basic_json/insert/
23335 iterator insert(const_iterator pos, const basic_json& val) // NOLINT(performance-unnecessary-value-param)
23336 {
23337 // insert only works for arrays
23338 if (JSON_HEDLEY_LIKELY(is_array()))
23339 {
23340 // check if iterator pos fits to this JSON value
23341 if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
23342 {
23343 JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
23344 }
23345
23346 // insert to array and return iterator
23347 return insert_iterator(pos, val);
23348 }
23349
23350 JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
23351 }
23352
23353 /// @brief inserts element into array
23354 /// @sa https://json.nlohmann.me/api/basic_json/insert/
23355 iterator insert(const_iterator pos, basic_json&& val) // NOLINT(performance-unnecessary-value-param)
23356 {
23357 return insert(pos, val);
23358 }
23359
23360 /// @brief inserts copies of element into array
23361 /// @sa https://json.nlohmann.me/api/basic_json/insert/
23362 iterator insert(const_iterator pos, size_type cnt, const basic_json& val) // NOLINT(performance-unnecessary-value-param)
23363 {
23364 // insert only works for arrays
23365 if (JSON_HEDLEY_LIKELY(is_array()))
23366 {
23367 // check if iterator pos fits to this JSON value
23368 if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
23369 {
23370 JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
23371 }
23372
23373 // insert to array and return iterator
23374 return insert_iterator(pos, cnt, val);
23375 }
23376
23377 JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
23378 }
23379
23380 /// @brief inserts range of elements into array
23381 /// @sa https://json.nlohmann.me/api/basic_json/insert/
23382 iterator insert(const_iterator pos, const_iterator first, const_iterator last) // NOLINT(performance-unnecessary-value-param)
23383 {
23384 // insert only works for arrays
23385 if (JSON_HEDLEY_UNLIKELY(!is_array()))
23386 {
23387 JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
23388 }
23389
23390 // check if iterator pos fits to this JSON value
23391 if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
23392 {
23393 JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
23394 }
23395
23396 // check if range iterators belong to the same JSON object
23397 if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
23398 {
23399 JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
23400 }
23401
23402 if (JSON_HEDLEY_UNLIKELY(first.m_object == this))
23403 {
23404 JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container", this));
23405 }
23406
23407 // insert to array and return iterator
23408 return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator);
23409 }
23410
23411 /// @brief inserts elements from initializer list into array
23412 /// @sa https://json.nlohmann.me/api/basic_json/insert/
23413 iterator insert(const_iterator pos, initializer_list_t ilist) // NOLINT(performance-unnecessary-value-param)
23414 {
23415 // insert only works for arrays
23416 if (JSON_HEDLEY_UNLIKELY(!is_array()))
23417 {
23418 JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
23419 }
23420
23421 // check if iterator pos fits to this JSON value
23422 if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
23423 {
23424 JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", this));
23425 }
23426
23427 // insert to array and return iterator
23428 return insert_iterator(pos, ilist.begin(), ilist.end());
23429 }
23430
23431 /// @brief inserts range of elements into object
23432 /// @sa https://json.nlohmann.me/api/basic_json/insert/
23433 void insert(const_iterator first, const_iterator last) // NOLINT(performance-unnecessary-value-param)
23434 {
23435 // insert only works for objects
23436 if (JSON_HEDLEY_UNLIKELY(!is_object()))
23437 {
23438 JSON_THROW(type_error::create(309, detail::concat("cannot use insert() with ", type_name()), this));
23439 }
23440
23441 // check if range iterators belong to the same JSON object
23442 if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
23443 {
23444 JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
23445 }
23446
23447 // passed iterators must belong to objects
23448 if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()))
23449 {
23450 JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", this));
23451 }
23452
23453 m_data.m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator);
23454 set_parents();
23455 }
23456
23457 /// @brief updates a JSON object from another object, overwriting existing keys
23458 /// @sa https://json.nlohmann.me/api/basic_json/update/
23459 void update(const_reference j, bool merge_objects = false)
23460 {
23461 update(j.begin(), j.end(), merge_objects);
23462 }
23463
23464 /// @brief updates a JSON object from another object, overwriting existing keys
23465 /// @sa https://json.nlohmann.me/api/basic_json/update/
23466 void update(const_iterator first, const_iterator last, bool merge_objects = false) // NOLINT(performance-unnecessary-value-param)
23467 {
23468 // implicitly convert null value to an empty object
23469 if (is_null())
23470 {
23471 m_data.m_type = value_t::object;
23472 m_data.m_value.object = create<object_t>();
23473 assert_invariant();
23474 }
23475
23476 if (JSON_HEDLEY_UNLIKELY(!is_object()))
23477 {
23478 JSON_THROW(type_error::create(312, detail::concat("cannot use update() with ", type_name()), this));
23479 }
23480
23481 // check if range iterators belong to the same JSON object
23482 if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
23483 {
23484 JSON_THROW(invalid_iterator::create(210, "iterators do not fit", this));
23485 }
23486
23487 // passed iterators must belong to objects
23488 if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()))
23489 {
23490 JSON_THROW(type_error::create(312, detail::concat("cannot use update() with ", first.m_object->type_name()), first.m_object));
23491 }
23492
23493 for (auto it = first; it != last; ++it)
23494 {
23495 if (merge_objects && it.value().is_object())
23496 {
23497 auto it2 = m_data.m_value.object->find(it.key());
23498 if (it2 != m_data.m_value.object->end())
23499 {
23500 it2->second.update(it.value(), true);
23501 continue;
23502 }
23503 }
23504 m_data.m_value.object->operator[](it.key()) = it.value();
23505 #if JSON_DIAGNOSTICS
23506 m_data.m_value.object->operator[](it.key()).m_parent = this;
23507 #endif
23508 }
23509 }
23510
23511 /// @brief exchanges the values
23512 /// @sa https://json.nlohmann.me/api/basic_json/swap/
23513 void swap(reference other) noexcept (
23514 std::is_nothrow_move_constructible<value_t>::value&&
23515 std::is_nothrow_move_assignable<value_t>::value&&
23516 std::is_nothrow_move_constructible<json_value>::value&& // NOLINT(cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
23517 std::is_nothrow_move_assignable<json_value>::value
23518 )
23519 {
23520 std::swap(m_data.m_type, other.m_data.m_type);
23521 std::swap(m_data.m_value, other.m_data.m_value);
23522
23523 set_parents();
23524 other.set_parents();
23525 assert_invariant();
23526 }
23527
23528 /// @brief exchanges the values
23529 /// @sa https://json.nlohmann.me/api/basic_json/swap/
23530 friend void swap(reference left, reference right) noexcept (
23531 std::is_nothrow_move_constructible<value_t>::value&&
23532 std::is_nothrow_move_assignable<value_t>::value&&
23533 std::is_nothrow_move_constructible<json_value>::value&& // NOLINT(cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
23534 std::is_nothrow_move_assignable<json_value>::value
23535 )
23536 {
23537 left.swap(right);
23538 }
23539
23540 /// @brief exchanges the values
23541 /// @sa https://json.nlohmann.me/api/basic_json/swap/
23542 void swap(array_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
23543 {
23544 // swap only works for arrays
23545 if (JSON_HEDLEY_LIKELY(is_array()))
23546 {
23547 using std::swap;
23548 swap(*(m_data.m_value.array), other);
23549 }
23550 else
23551 {
23552 JSON_THROW(type_error::create(310, detail::concat("cannot use swap(array_t&) with ", type_name()), this));
23553 }
23554 }
23555
23556 /// @brief exchanges the values
23557 /// @sa https://json.nlohmann.me/api/basic_json/swap/
23558 void swap(object_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
23559 {
23560 // swap only works for objects
23561 if (JSON_HEDLEY_LIKELY(is_object()))
23562 {
23563 using std::swap;
23564 swap(*(m_data.m_value.object), other);
23565 }
23566 else
23567 {
23568 JSON_THROW(type_error::create(310, detail::concat("cannot use swap(object_t&) with ", type_name()), this));
23569 }
23570 }
23571
23572 /// @brief exchanges the values
23573 /// @sa https://json.nlohmann.me/api/basic_json/swap/
23574 void swap(string_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
23575 {
23576 // swap only works for strings
23577 if (JSON_HEDLEY_LIKELY(is_string()))
23578 {
23579 using std::swap;
23580 swap(*(m_data.m_value.string), other);
23581 }
23582 else
23583 {
23584 JSON_THROW(type_error::create(310, detail::concat("cannot use swap(string_t&) with ", type_name()), this));
23585 }
23586 }
23587
23588 /// @brief exchanges the values
23589 /// @sa https://json.nlohmann.me/api/basic_json/swap/
23590 void swap(binary_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
23591 {
23592 // swap only works for strings
23593 if (JSON_HEDLEY_LIKELY(is_binary()))
23594 {
23595 using std::swap;
23596 swap(*(m_data.m_value.binary), other);
23597 }
23598 else
23599 {
23600 JSON_THROW(type_error::create(310, detail::concat("cannot use swap(binary_t&) with ", type_name()), this));
23601 }
23602 }
23603
23604 /// @brief exchanges the values
23605 /// @sa https://json.nlohmann.me/api/basic_json/swap/
23606 void swap(typename binary_t::container_type& other) // NOLINT(bugprone-exception-escape)
23607 {
23608 // swap only works for strings
23609 if (JSON_HEDLEY_LIKELY(is_binary()))
23610 {
23611 using std::swap;
23612 swap(*(m_data.m_value.binary), other);
23613 }
23614 else
23615 {
23616 JSON_THROW(type_error::create(310, detail::concat("cannot use swap(binary_t::container_type&) with ", type_name()), this));
23617 }
23618 }
23619
23620 /// @}
23621
23622 //////////////////////////////////////////
23623 // lexicographical comparison operators //
23624 //////////////////////////////////////////
23625
23626 /// @name lexicographical comparison operators
23627 /// @{
23628
23629 // note parentheses around operands are necessary; see
23630 // https://github.com/nlohmann/json/issues/1530
23631 #define JSON_IMPLEMENT_OPERATOR(op, null_result, unordered_result, default_result) \
23632 const auto lhs_type = lhs.type(); \
23633 const auto rhs_type = rhs.type(); \
23634 \
23635 if (lhs_type == rhs_type) /* NOLINT(readability/braces) */ \
23636 { \
23637 switch (lhs_type) \
23638 { \
23639 case value_t::array: \
23640 return (*lhs.m_data.m_value.array) op (*rhs.m_data.m_value.array); \
23641 \
23642 case value_t::object: \
23643 return (*lhs.m_data.m_value.object) op (*rhs.m_data.m_value.object); \
23644 \
23645 case value_t::null: \
23646 return (null_result); \
23647 \
23648 case value_t::string: \
23649 return (*lhs.m_data.m_value.string) op (*rhs.m_data.m_value.string); \
23650 \
23651 case value_t::boolean: \
23652 return (lhs.m_data.m_value.boolean) op (rhs.m_data.m_value.boolean); \
23653 \
23654 case value_t::number_integer: \
23655 return (lhs.m_data.m_value.number_integer) op (rhs.m_data.m_value.number_integer); \
23656 \
23657 case value_t::number_unsigned: \
23658 return (lhs.m_data.m_value.number_unsigned) op (rhs.m_data.m_value.number_unsigned); \
23659 \
23660 case value_t::number_float: \
23661 return (lhs.m_data.m_value.number_float) op (rhs.m_data.m_value.number_float); \
23662 \
23663 case value_t::binary: \
23664 return (*lhs.m_data.m_value.binary) op (*rhs.m_data.m_value.binary); \
23665 \
23666 case value_t::discarded: \
23667 default: \
23668 return (unordered_result); \
23669 } \
23670 } \
23671 else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_float) \
23672 { \
23673 return static_cast<number_float_t>(lhs.m_data.m_value.number_integer) op rhs.m_data.m_value.number_float; \
23674 } \
23675 else if (lhs_type == value_t::number_float && rhs_type == value_t::number_integer) \
23676 { \
23677 return lhs.m_data.m_value.number_float op static_cast<number_float_t>(rhs.m_data.m_value.number_integer); \
23678 } \
23679 else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_float) \
23680 { \
23681 return static_cast<number_float_t>(lhs.m_data.m_value.number_unsigned) op rhs.m_data.m_value.number_float; \
23682 } \
23683 else if (lhs_type == value_t::number_float && rhs_type == value_t::number_unsigned) \
23684 { \
23685 return lhs.m_data.m_value.number_float op static_cast<number_float_t>(rhs.m_data.m_value.number_unsigned); \
23686 } \
23687 else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_integer) \
23688 { \
23689 return static_cast<number_integer_t>(lhs.m_data.m_value.number_unsigned) op rhs.m_data.m_value.number_integer; \
23690 } \
23691 else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_unsigned) \
23692 { \
23693 return lhs.m_data.m_value.number_integer op static_cast<number_integer_t>(rhs.m_data.m_value.number_unsigned); \
23694 } \
23695 else if(compares_unordered(lhs, rhs))\
23696 {\
23697 return (unordered_result);\
23698 }\
23699 \
23700 return (default_result);
23701
23702 JSON_PRIVATE_UNLESS_TESTED:
23703 // returns true if:
23704 // - any operand is NaN and the other operand is of number type
23705 // - any operand is discarded
23706 // in legacy mode, discarded values are considered ordered if
23707 // an operation is computed as an odd number of inverses of others
23708 static bool compares_unordered(const_reference lhs, const_reference rhs, bool inverse = false) noexcept
23709 {
23710 if ((lhs.is_number_float() && std::isnan(lhs.m_data.m_value.number_float) && rhs.is_number())
23711 || (rhs.is_number_float() && std::isnan(rhs.m_data.m_value.number_float) && lhs.is_number()))
23712 {
23713 return true;
23714 }
23715 #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
23716 return (lhs.is_discarded() || rhs.is_discarded()) && !inverse;
23717 #else
23718 static_cast<void>(inverse);
23719 return lhs.is_discarded() || rhs.is_discarded();
23720 #endif
23721 }
23722
23723 private:
23724 bool compares_unordered(const_reference rhs, bool inverse = false) const noexcept
23725 {
23726 return compares_unordered(*this, rhs, inverse);
23727 }
23728
23729 public:
23730 #if JSON_HAS_THREE_WAY_COMPARISON
23731 /// @brief comparison: equal
23732 /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
23733 bool operator==(const_reference rhs) const noexcept
23734 {
23735 #ifdef __GNUC__
23736 #pragma GCC diagnostic push
23737 #pragma GCC diagnostic ignored "-Wfloat-equal"
23738 #endif
23739 const_reference lhs = *this;
23740 JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
23741 #ifdef __GNUC__
23742 #pragma GCC diagnostic pop
23743 #endif
23744 }
23745
23746 /// @brief comparison: equal
23747 /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
23748 template<typename ScalarType>
23749 requires std::is_scalar_v<ScalarType>
23750 bool operator==(ScalarType rhs) const noexcept
23751 {
23752 return *this == basic_json(rhs);
23753 }
23754
23755 /// @brief comparison: not equal
23756 /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
23757 bool operator!=(const_reference rhs) const noexcept
23758 {
23759 if (compares_unordered(rhs, true))
23760 {
23761 return false;
23762 }
23763 return !operator==(rhs);
23764 }
23765
23766 /// @brief comparison: 3-way
23767 /// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
23768 std::partial_ordering operator<=>(const_reference rhs) const noexcept // *NOPAD*
23769 {
23770 const_reference lhs = *this;
23771 // default_result is used if we cannot compare values. In that case,
23772 // we compare types.
23773 JSON_IMPLEMENT_OPERATOR(<=>, // *NOPAD*
23774 std::partial_ordering::equivalent,
23775 std::partial_ordering::unordered,
23776 lhs_type <=> rhs_type) // *NOPAD*
23777 }
23778
23779 /// @brief comparison: 3-way
23780 /// @sa https://json.nlohmann.me/api/basic_json/operator_spaceship/
23781 template<typename ScalarType>
23782 requires std::is_scalar_v<ScalarType>
23783 std::partial_ordering operator<=>(ScalarType rhs) const noexcept // *NOPAD*
23784 {
23785 return *this <=> basic_json(rhs); // *NOPAD*
23786 }
23787
23788 #if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
23789 // all operators that are computed as an odd number of inverses of others
23790 // need to be overloaded to emulate the legacy comparison behavior
23791
23792 /// @brief comparison: less than or equal
23793 /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
23794 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON)
23795 bool operator<=(const_reference rhs) const noexcept
23796 {
23797 if (compares_unordered(rhs, true))
23798 {
23799 return false;
23800 }
23801 return !(rhs < *this);
23802 }
23803
23804 /// @brief comparison: less than or equal
23805 /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
23806 template<typename ScalarType>
23807 requires std::is_scalar_v<ScalarType>
23808 bool operator<=(ScalarType rhs) const noexcept
23809 {
23810 return *this <= basic_json(rhs);
23811 }
23812
23813 /// @brief comparison: greater than or equal
23814 /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
23815 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON)
23816 bool operator>=(const_reference rhs) const noexcept
23817 {
23818 if (compares_unordered(rhs, true))
23819 {
23820 return false;
23821 }
23822 return !(*this < rhs);
23823 }
23824
23825 /// @brief comparison: greater than or equal
23826 /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
23827 template<typename ScalarType>
23828 requires std::is_scalar_v<ScalarType>
23829 bool operator>=(ScalarType rhs) const noexcept
23830 {
23831 return *this >= basic_json(rhs);
23832 }
23833 #endif
23834 #else
23835 /// @brief comparison: equal
23836 /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
23837 friend bool operator==(const_reference lhs, const_reference rhs) noexcept
23838 {
23839 #ifdef __GNUC__
23840 #pragma GCC diagnostic push
23841 #pragma GCC diagnostic ignored "-Wfloat-equal"
23842 #endif
23843 JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
23844 #ifdef __GNUC__
23845 #pragma GCC diagnostic pop
23846 #endif
23847 }
23848
23849 /// @brief comparison: equal
23850 /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
23851 template<typename ScalarType, typename std::enable_if<
23852 std::is_scalar<ScalarType>::value, int>::type = 0>
23853 friend bool operator==(const_reference lhs, ScalarType rhs) noexcept
23854 {
23855 return lhs == basic_json(rhs);
23856 }
23857
23858 /// @brief comparison: equal
23859 /// @sa https://json.nlohmann.me/api/basic_json/operator_eq/
23860 template<typename ScalarType, typename std::enable_if<
23861 std::is_scalar<ScalarType>::value, int>::type = 0>
23862 friend bool operator==(ScalarType lhs, const_reference rhs) noexcept
23863 {
23864 return basic_json(lhs) == rhs;
23865 }
23866
23867 /// @brief comparison: not equal
23868 /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
23869 friend bool operator!=(const_reference lhs, const_reference rhs) noexcept
23870 {
23871 if (compares_unordered(lhs, rhs, true))
23872 {
23873 return false;
23874 }
23875 return !(lhs == rhs);
23876 }
23877
23878 /// @brief comparison: not equal
23879 /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
23880 template<typename ScalarType, typename std::enable_if<
23881 std::is_scalar<ScalarType>::value, int>::type = 0>
23882 friend bool operator!=(const_reference lhs, ScalarType rhs) noexcept
23883 {
23884 return lhs != basic_json(rhs);
23885 }
23886
23887 /// @brief comparison: not equal
23888 /// @sa https://json.nlohmann.me/api/basic_json/operator_ne/
23889 template<typename ScalarType, typename std::enable_if<
23890 std::is_scalar<ScalarType>::value, int>::type = 0>
23891 friend bool operator!=(ScalarType lhs, const_reference rhs) noexcept
23892 {
23893 return basic_json(lhs) != rhs;
23894 }
23895
23896 /// @brief comparison: less than
23897 /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
23898 friend bool operator<(const_reference lhs, const_reference rhs) noexcept
23899 {
23900 // default_result is used if we cannot compare values. In that case,
23901 // we compare types. Note we have to call the operator explicitly,
23902 // because MSVC has problems otherwise.
23903 JSON_IMPLEMENT_OPERATOR( <, false, false, operator<(lhs_type, rhs_type))
23904 }
23905
23906 /// @brief comparison: less than
23907 /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
23908 template<typename ScalarType, typename std::enable_if<
23909 std::is_scalar<ScalarType>::value, int>::type = 0>
23910 friend bool operator<(const_reference lhs, ScalarType rhs) noexcept
23911 {
23912 return lhs < basic_json(rhs);
23913 }
23914
23915 /// @brief comparison: less than
23916 /// @sa https://json.nlohmann.me/api/basic_json/operator_lt/
23917 template<typename ScalarType, typename std::enable_if<
23918 std::is_scalar<ScalarType>::value, int>::type = 0>
23919 friend bool operator<(ScalarType lhs, const_reference rhs) noexcept
23920 {
23921 return basic_json(lhs) < rhs;
23922 }
23923
23924 /// @brief comparison: less than or equal
23925 /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
23926 friend bool operator<=(const_reference lhs, const_reference rhs) noexcept
23927 {
23928 if (compares_unordered(lhs, rhs, true))
23929 {
23930 return false;
23931 }
23932 return !(rhs < lhs);
23933 }
23934
23935 /// @brief comparison: less than or equal
23936 /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
23937 template<typename ScalarType, typename std::enable_if<
23938 std::is_scalar<ScalarType>::value, int>::type = 0>
23939 friend bool operator<=(const_reference lhs, ScalarType rhs) noexcept
23940 {
23941 return lhs <= basic_json(rhs);
23942 }
23943
23944 /// @brief comparison: less than or equal
23945 /// @sa https://json.nlohmann.me/api/basic_json/operator_le/
23946 template<typename ScalarType, typename std::enable_if<
23947 std::is_scalar<ScalarType>::value, int>::type = 0>
23948 friend bool operator<=(ScalarType lhs, const_reference rhs) noexcept
23949 {
23950 return basic_json(lhs) <= rhs;
23951 }
23952
23953 /// @brief comparison: greater than
23954 /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
23955 friend bool operator>(const_reference lhs, const_reference rhs) noexcept
23956 {
23957 // double inverse
23958 if (compares_unordered(lhs, rhs))
23959 {
23960 return false;
23961 }
23962 return !(lhs <= rhs);
23963 }
23964
23965 /// @brief comparison: greater than
23966 /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
23967 template<typename ScalarType, typename std::enable_if<
23968 std::is_scalar<ScalarType>::value, int>::type = 0>
23969 friend bool operator>(const_reference lhs, ScalarType rhs) noexcept
23970 {
23971 return lhs > basic_json(rhs);
23972 }
23973
23974 /// @brief comparison: greater than
23975 /// @sa https://json.nlohmann.me/api/basic_json/operator_gt/
23976 template<typename ScalarType, typename std::enable_if<
23977 std::is_scalar<ScalarType>::value, int>::type = 0>
23978 friend bool operator>(ScalarType lhs, const_reference rhs) noexcept
23979 {
23980 return basic_json(lhs) > rhs;
23981 }
23982
23983 /// @brief comparison: greater than or equal
23984 /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
23985 friend bool operator>=(const_reference lhs, const_reference rhs) noexcept
23986 {
23987 if (compares_unordered(lhs, rhs, true))
23988 {
23989 return false;
23990 }
23991 return !(lhs < rhs);
23992 }
23993
23994 /// @brief comparison: greater than or equal
23995 /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
23996 template<typename ScalarType, typename std::enable_if<
23997 std::is_scalar<ScalarType>::value, int>::type = 0>
23998 friend bool operator>=(const_reference lhs, ScalarType rhs) noexcept
23999 {
24000 return lhs >= basic_json(rhs);
24001 }
24002
24003 /// @brief comparison: greater than or equal
24004 /// @sa https://json.nlohmann.me/api/basic_json/operator_ge/
24005 template<typename ScalarType, typename std::enable_if<
24006 std::is_scalar<ScalarType>::value, int>::type = 0>
24007 friend bool operator>=(ScalarType lhs, const_reference rhs) noexcept
24008 {
24009 return basic_json(lhs) >= rhs;
24010 }
24011 #endif
24012
24013 #undef JSON_IMPLEMENT_OPERATOR
24014
24015 /// @}
24016
24017 ///////////////////
24018 // serialization //
24019 ///////////////////
24020
24021 /// @name serialization
24022 /// @{
24023 #ifndef JSON_NO_IO
24024 /// @brief serialize to stream
24025 /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
24026 friend std::ostream& operator<<(std::ostream& o, const basic_json& j)
24027 {
24028 // read width member and use it as indentation parameter if nonzero
24029 const bool pretty_print = o.width() > 0;
24030 const auto indentation = pretty_print ? o.width() : 0;
24031
24032 // reset width to 0 for subsequent calls to this stream
24033 o.width(0);
24034
24035 // do the actual serialization
24036 serializer s(detail::output_adapter<char>(o), o.fill());
24037 s.dump(j, pretty_print, false, static_cast<unsigned int>(indentation));
24038 return o;
24039 }
24040
24041 /// @brief serialize to stream
24042 /// @sa https://json.nlohmann.me/api/basic_json/operator_ltlt/
24043 /// @deprecated This function is deprecated since 3.0.0 and will be removed in
24044 /// version 4.0.0 of the library. Please use
24045 /// operator<<(std::ostream&, const basic_json&) instead; that is,
24046 /// replace calls like `j >> o;` with `o << j;`.
24047 JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator<<(std::ostream&, const basic_json&))
24048 friend std::ostream& operator>>(const basic_json& j, std::ostream& o)
24049 {
24050 return o << j;
24051 }
24052 #endif // JSON_NO_IO
24053 /// @}
24054
24055 /////////////////////
24056 // deserialization //
24057 /////////////////////
24058
24059 /// @name deserialization
24060 /// @{
24061
24062 /// @brief deserialize from a compatible input
24063 /// @sa https://json.nlohmann.me/api/basic_json/parse/
24064 template<typename InputType>
24065 JSON_HEDLEY_WARN_UNUSED_RESULT
24066 static basic_json parse(InputType&& i,
24067 parser_callback_t cb = nullptr,
24068 const bool allow_exceptions = true,
24069 const bool ignore_comments = false)
24070 {
24071 basic_json result;
24072 parser(detail::input_adapter(std::forward<InputType>(i)), std::move(cb), allow_exceptions, ignore_comments).parse(true, result); // cppcheck-suppress[accessMoved,accessForwarded]
24073 return result;
24074 }
24075
24076 /// @brief deserialize from a pair of character iterators
24077 /// @sa https://json.nlohmann.me/api/basic_json/parse/
24078 template<typename IteratorType>
24079 JSON_HEDLEY_WARN_UNUSED_RESULT
24080 static basic_json parse(IteratorType first,
24081 IteratorType last,
24082 parser_callback_t cb = nullptr,
24083 const bool allow_exceptions = true,
24084 const bool ignore_comments = false)
24085 {
24086 basic_json result;
24087 parser(detail::input_adapter(std::move(first), std::move(last)), std::move(cb), allow_exceptions, ignore_comments).parse(true, result); // cppcheck-suppress[accessMoved]
24088 return result;
24089 }
24090
24091 JSON_HEDLEY_WARN_UNUSED_RESULT
24092 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, parse(ptr, ptr + len))
24093 static basic_json parse(detail::span_input_adapter&& i,
24094 parser_callback_t cb = nullptr,
24095 const bool allow_exceptions = true,
24096 const bool ignore_comments = false)
24097 {
24098 basic_json result;
24099 parser(i.get(), std::move(cb), allow_exceptions, ignore_comments).parse(true, result); // cppcheck-suppress[accessMoved]
24100 return result;
24101 }
24102
24103 /// @brief check if the input is valid JSON
24104 /// @sa https://json.nlohmann.me/api/basic_json/accept/
24105 template<typename InputType>
24106 static bool accept(InputType&& i,
24107 const bool ignore_comments = false)
24108 {
24109 return parser(detail::input_adapter(std::forward<InputType>(i)), nullptr, false, ignore_comments).accept(true);
24110 }
24111
24112 /// @brief check if the input is valid JSON
24113 /// @sa https://json.nlohmann.me/api/basic_json/accept/
24114 template<typename IteratorType>
24115 static bool accept(IteratorType first, IteratorType last,
24116 const bool ignore_comments = false)
24117 {
24118 return parser(detail::input_adapter(std::move(first), std::move(last)), nullptr, false, ignore_comments).accept(true);
24119 }
24120
24121 JSON_HEDLEY_WARN_UNUSED_RESULT
24122 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, accept(ptr, ptr + len))
24123 static bool accept(detail::span_input_adapter&& i,
24124 const bool ignore_comments = false)
24125 {
24126 return parser(i.get(), nullptr, false, ignore_comments).accept(true);
24127 }
24128
24129 /// @brief generate SAX events
24130 /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
24131 template <typename InputType, typename SAX>
24132 JSON_HEDLEY_NON_NULL(2)
24133 static bool sax_parse(InputType&& i, SAX* sax,
24134 input_format_t format = input_format_t::json,
24135 const bool strict = true,
24136 const bool ignore_comments = false)
24137 {
24138 auto ia = detail::input_adapter(std::forward<InputType>(i));
24139 return format == input_format_t::json
24140 ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
24141 : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
24142 }
24143
24144 /// @brief generate SAX events
24145 /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
24146 template<class IteratorType, class SAX>
24147 JSON_HEDLEY_NON_NULL(3)
24148 static bool sax_parse(IteratorType first, IteratorType last, SAX* sax,
24149 input_format_t format = input_format_t::json,
24150 const bool strict = true,
24151 const bool ignore_comments = false)
24152 {
24153 auto ia = detail::input_adapter(std::move(first), std::move(last));
24154 return format == input_format_t::json
24155 ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
24156 : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
24157 }
24158
24159 /// @brief generate SAX events
24160 /// @sa https://json.nlohmann.me/api/basic_json/sax_parse/
24161 /// @deprecated This function is deprecated since 3.8.0 and will be removed in
24162 /// version 4.0.0 of the library. Please use
24163 /// sax_parse(ptr, ptr + len) instead.
24164 template <typename SAX>
24165 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, sax_parse(ptr, ptr + len, ...))
24166 JSON_HEDLEY_NON_NULL(2)
24167 static bool sax_parse(detail::span_input_adapter&& i, SAX* sax,
24168 input_format_t format = input_format_t::json,
24169 const bool strict = true,
24170 const bool ignore_comments = false)
24171 {
24172 auto ia = i.get();
24173 return format == input_format_t::json
24174 // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
24175 ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
24176 // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
24177 : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia), format).sax_parse(format, sax, strict);
24178 }
24179 #ifndef JSON_NO_IO
24180 /// @brief deserialize from stream
24181 /// @sa https://json.nlohmann.me/api/basic_json/operator_gtgt/
24182 /// @deprecated This stream operator is deprecated since 3.0.0 and will be removed in
24183 /// version 4.0.0 of the library. Please use
24184 /// operator>>(std::istream&, basic_json&) instead; that is,
24185 /// replace calls like `j << i;` with `i >> j;`.
24186 JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator>>(std::istream&, basic_json&))
24187 friend std::istream& operator<<(basic_json& j, std::istream& i)
24188 {
24189 return operator>>(i, j);
24190 }
24191
24192 /// @brief deserialize from stream
24193 /// @sa https://json.nlohmann.me/api/basic_json/operator_gtgt/
24194 friend std::istream& operator>>(std::istream& i, basic_json& j)
24195 {
24196 parser(detail::input_adapter(i)).parse(false, j);
24197 return i;
24198 }
24199 #endif // JSON_NO_IO
24200 /// @}
24201
24202 ///////////////////////////
24203 // convenience functions //
24204 ///////////////////////////
24205
24206 /// @brief return the type as string
24207 /// @sa https://json.nlohmann.me/api/basic_json/type_name/
24208 JSON_HEDLEY_RETURNS_NON_NULL
24209 const char* type_name() const noexcept
24210 {
24211 switch (m_data.m_type)
24212 {
24213 case value_t::null:
24214 return "null";
24215 case value_t::object:
24216 return "object";
24217 case value_t::array:
24218 return "array";
24219 case value_t::string:
24220 return "string";
24221 case value_t::boolean:
24222 return "boolean";
24223 case value_t::binary:
24224 return "binary";
24225 case value_t::discarded:
24226 return "discarded";
24227 case value_t::number_integer:
24228 case value_t::number_unsigned:
24229 case value_t::number_float:
24230 default:
24231 return "number";
24232 }
24233 }
24234
24235 JSON_PRIVATE_UNLESS_TESTED:
24236 //////////////////////
24237 // member variables //
24238 //////////////////////
24239
24240 struct data
24241 {
24242 /// the type of the current element
24243 value_t m_type = value_t::null;
24244
24245 /// the value of the current element
24246 json_value m_value = {};
24247
24248 data(const value_t v)
24249 : m_type(v), m_value(v)
24250 {
24251 }
24252
24253 data(size_type cnt, const basic_json& val)
24254 : m_type(value_t::array)
24255 {
24256 m_value.array = create<array_t>(cnt, val);
24257 }
24258
24259 data() noexcept = default;
24260 data(data&&) noexcept = default;
24261 data(const data&) noexcept = delete;
24262 data& operator=(data&&) noexcept = delete;
24263 data& operator=(const data&) noexcept = delete;
24264
24265 ~data() noexcept
24266 {
24267 m_value.destroy(m_type);
24268 }
24269 };
24270
24271 data m_data = {};
24272
24273 #if JSON_DIAGNOSTICS
24274 /// a pointer to a parent value (for debugging purposes)
24275 basic_json* m_parent = nullptr;
24276 #endif
24277
24278 #if JSON_DIAGNOSTIC_POSITIONS
24279 /// the start position of the value
24280 std::size_t start_position = std::string::npos;
24281 /// the end position of the value
24282 std::size_t end_position = std::string::npos;
24283 public:
24284 constexpr std::size_t start_pos() const noexcept
24285 {
24286 return start_position;
24287 }
24288
24289 constexpr std::size_t end_pos() const noexcept
24290 {
24291 return end_position;
24292 }
24293 #endif
24294
24295 //////////////////////////////////////////
24296 // binary serialization/deserialization //
24297 //////////////////////////////////////////
24298
24299 /// @name binary serialization/deserialization support
24300 /// @{
24301
24302 public:
24303 /// @brief create a CBOR serialization of a given JSON value
24304 /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
24305 static std::vector<std::uint8_t> to_cbor(const basic_json& j)
24306 {
24307 std::vector<std::uint8_t> result;
24308 to_cbor(j, result);
24309 return result;
24310 }
24311
24312 /// @brief create a CBOR serialization of a given JSON value
24313 /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
24314 static void to_cbor(const basic_json& j, detail::output_adapter<std::uint8_t> o)
24315 {
24316 binary_writer<std::uint8_t>(o).write_cbor(j);
24317 }
24318
24319 /// @brief create a CBOR serialization of a given JSON value
24320 /// @sa https://json.nlohmann.me/api/basic_json/to_cbor/
24321 static void to_cbor(const basic_json& j, detail::output_adapter<char> o)
24322 {
24323 binary_writer<char>(o).write_cbor(j);
24324 }
24325
24326 /// @brief create a MessagePack serialization of a given JSON value
24327 /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
24328 static std::vector<std::uint8_t> to_msgpack(const basic_json& j)
24329 {
24330 std::vector<std::uint8_t> result;
24331 to_msgpack(j, result);
24332 return result;
24333 }
24334
24335 /// @brief create a MessagePack serialization of a given JSON value
24336 /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
24337 static void to_msgpack(const basic_json& j, detail::output_adapter<std::uint8_t> o)
24338 {
24339 binary_writer<std::uint8_t>(o).write_msgpack(j);
24340 }
24341
24342 /// @brief create a MessagePack serialization of a given JSON value
24343 /// @sa https://json.nlohmann.me/api/basic_json/to_msgpack/
24344 static void to_msgpack(const basic_json& j, detail::output_adapter<char> o)
24345 {
24346 binary_writer<char>(o).write_msgpack(j);
24347 }
24348
24349 /// @brief create a UBJSON serialization of a given JSON value
24350 /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
24351 static std::vector<std::uint8_t> to_ubjson(const basic_json& j,
24352 const bool use_size = false,
24353 const bool use_type = false)
24354 {
24355 std::vector<std::uint8_t> result;
24356 to_ubjson(j, result, use_size, use_type);
24357 return result;
24358 }
24359
24360 /// @brief create a UBJSON serialization of a given JSON value
24361 /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
24362 static void to_ubjson(const basic_json& j, detail::output_adapter<std::uint8_t> o,
24363 const bool use_size = false, const bool use_type = false)
24364 {
24365 binary_writer<std::uint8_t>(o).write_ubjson(j, use_size, use_type);
24366 }
24367
24368 /// @brief create a UBJSON serialization of a given JSON value
24369 /// @sa https://json.nlohmann.me/api/basic_json/to_ubjson/
24370 static void to_ubjson(const basic_json& j, detail::output_adapter<char> o,
24371 const bool use_size = false, const bool use_type = false)
24372 {
24373 binary_writer<char>(o).write_ubjson(j, use_size, use_type);
24374 }
24375
24376 /// @brief create a BJData serialization of a given JSON value
24377 /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
24378 static std::vector<std::uint8_t> to_bjdata(const basic_json& j,
24379 const bool use_size = false,
24380 const bool use_type = false,
24381 const bjdata_version_t version = bjdata_version_t::draft2)
24382 {
24383 std::vector<std::uint8_t> result;
24384 to_bjdata(j, result, use_size, use_type, version);
24385 return result;
24386 }
24387
24388 /// @brief create a BJData serialization of a given JSON value
24389 /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
24390 static void to_bjdata(const basic_json& j, detail::output_adapter<std::uint8_t> o,
24391 const bool use_size = false, const bool use_type = false,
24392 const bjdata_version_t version = bjdata_version_t::draft2)
24393 {
24394 binary_writer<std::uint8_t>(o).write_ubjson(j, use_size, use_type, true, true, version);
24395 }
24396
24397 /// @brief create a BJData serialization of a given JSON value
24398 /// @sa https://json.nlohmann.me/api/basic_json/to_bjdata/
24399 static void to_bjdata(const basic_json& j, detail::output_adapter<char> o,
24400 const bool use_size = false, const bool use_type = false,
24401 const bjdata_version_t version = bjdata_version_t::draft2)
24402 {
24403 binary_writer<char>(o).write_ubjson(j, use_size, use_type, true, true, version);
24404 }
24405
24406 /// @brief create a BSON serialization of a given JSON value
24407 /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
24408 static std::vector<std::uint8_t> to_bson(const basic_json& j)
24409 {
24410 std::vector<std::uint8_t> result;
24411 to_bson(j, result);
24412 return result;
24413 }
24414
24415 /// @brief create a BSON serialization of a given JSON value
24416 /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
24417 static void to_bson(const basic_json& j, detail::output_adapter<std::uint8_t> o)
24418 {
24419 binary_writer<std::uint8_t>(o).write_bson(j);
24420 }
24421
24422 /// @brief create a BSON serialization of a given JSON value
24423 /// @sa https://json.nlohmann.me/api/basic_json/to_bson/
24424 static void to_bson(const basic_json& j, detail::output_adapter<char> o)
24425 {
24426 binary_writer<char>(o).write_bson(j);
24427 }
24428
24429 /// @brief create a JSON value from an input in CBOR format
24430 /// @sa https://json.nlohmann.me/api/basic_json/from_cbor/
24431 template<typename InputType>
24432 JSON_HEDLEY_WARN_UNUSED_RESULT
24433 static basic_json from_cbor(InputType&& i,
24434 const bool strict = true,
24435 const bool allow_exceptions = true,
24436 const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
24437 {
24438 basic_json result;
24439 auto ia = detail::input_adapter(std::forward<InputType>(i));
24440 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24441 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]
24442 return res ? result : basic_json(value_t::discarded);
24443 }
24444
24445 /// @brief create a JSON value from an input in CBOR format
24446 /// @sa https://json.nlohmann.me/api/basic_json/from_cbor/
24447 template<typename IteratorType>
24448 JSON_HEDLEY_WARN_UNUSED_RESULT
24449 static basic_json from_cbor(IteratorType first, IteratorType last,
24450 const bool strict = true,
24451 const bool allow_exceptions = true,
24452 const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
24453 {
24454 basic_json result;
24455 auto ia = detail::input_adapter(std::move(first), std::move(last));
24456 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24457 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]
24458 return res ? result : basic_json(value_t::discarded);
24459 }
24460
24461 template<typename T>
24462 JSON_HEDLEY_WARN_UNUSED_RESULT
24463 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len))
24464 static basic_json from_cbor(const T* ptr, std::size_t len,
24465 const bool strict = true,
24466 const bool allow_exceptions = true,
24467 const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
24468 {
24469 return from_cbor(ptr, ptr + len, strict, allow_exceptions, tag_handler);
24470 }
24471
24472 JSON_HEDLEY_WARN_UNUSED_RESULT
24473 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len))
24474 static basic_json from_cbor(detail::span_input_adapter&& i,
24475 const bool strict = true,
24476 const bool allow_exceptions = true,
24477 const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
24478 {
24479 basic_json result;
24480 auto ia = i.get();
24481 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24482 // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
24483 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]
24484 return res ? result : basic_json(value_t::discarded);
24485 }
24486
24487 /// @brief create a JSON value from an input in MessagePack format
24488 /// @sa https://json.nlohmann.me/api/basic_json/from_msgpack/
24489 template<typename InputType>
24490 JSON_HEDLEY_WARN_UNUSED_RESULT
24491 static basic_json from_msgpack(InputType&& i,
24492 const bool strict = true,
24493 const bool allow_exceptions = true)
24494 {
24495 basic_json result;
24496 auto ia = detail::input_adapter(std::forward<InputType>(i));
24497 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24498 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]
24499 return res ? result : basic_json(value_t::discarded);
24500 }
24501
24502 /// @brief create a JSON value from an input in MessagePack format
24503 /// @sa https://json.nlohmann.me/api/basic_json/from_msgpack/
24504 template<typename IteratorType>
24505 JSON_HEDLEY_WARN_UNUSED_RESULT
24506 static basic_json from_msgpack(IteratorType first, IteratorType last,
24507 const bool strict = true,
24508 const bool allow_exceptions = true)
24509 {
24510 basic_json result;
24511 auto ia = detail::input_adapter(std::move(first), std::move(last));
24512 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24513 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]
24514 return res ? result : basic_json(value_t::discarded);
24515 }
24516
24517 template<typename T>
24518 JSON_HEDLEY_WARN_UNUSED_RESULT
24519 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len))
24520 static basic_json from_msgpack(const T* ptr, std::size_t len,
24521 const bool strict = true,
24522 const bool allow_exceptions = true)
24523 {
24524 return from_msgpack(ptr, ptr + len, strict, allow_exceptions);
24525 }
24526
24527 JSON_HEDLEY_WARN_UNUSED_RESULT
24528 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len))
24529 static basic_json from_msgpack(detail::span_input_adapter&& i,
24530 const bool strict = true,
24531 const bool allow_exceptions = true)
24532 {
24533 basic_json result;
24534 auto ia = i.get();
24535 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24536 // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
24537 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]
24538 return res ? result : basic_json(value_t::discarded);
24539 }
24540
24541 /// @brief create a JSON value from an input in UBJSON format
24542 /// @sa https://json.nlohmann.me/api/basic_json/from_ubjson/
24543 template<typename InputType>
24544 JSON_HEDLEY_WARN_UNUSED_RESULT
24545 static basic_json from_ubjson(InputType&& i,
24546 const bool strict = true,
24547 const bool allow_exceptions = true)
24548 {
24549 basic_json result;
24550 auto ia = detail::input_adapter(std::forward<InputType>(i));
24551 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24552 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]
24553 return res ? result : basic_json(value_t::discarded);
24554 }
24555
24556 /// @brief create a JSON value from an input in UBJSON format
24557 /// @sa https://json.nlohmann.me/api/basic_json/from_ubjson/
24558 template<typename IteratorType>
24559 JSON_HEDLEY_WARN_UNUSED_RESULT
24560 static basic_json from_ubjson(IteratorType first, IteratorType last,
24561 const bool strict = true,
24562 const bool allow_exceptions = true)
24563 {
24564 basic_json result;
24565 auto ia = detail::input_adapter(std::move(first), std::move(last));
24566 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24567 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]
24568 return res ? result : basic_json(value_t::discarded);
24569 }
24570
24571 template<typename T>
24572 JSON_HEDLEY_WARN_UNUSED_RESULT
24573 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len))
24574 static basic_json from_ubjson(const T* ptr, std::size_t len,
24575 const bool strict = true,
24576 const bool allow_exceptions = true)
24577 {
24578 return from_ubjson(ptr, ptr + len, strict, allow_exceptions);
24579 }
24580
24581 JSON_HEDLEY_WARN_UNUSED_RESULT
24582 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len))
24583 static basic_json from_ubjson(detail::span_input_adapter&& i,
24584 const bool strict = true,
24585 const bool allow_exceptions = true)
24586 {
24587 basic_json result;
24588 auto ia = i.get();
24589 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24590 // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
24591 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]
24592 return res ? result : basic_json(value_t::discarded);
24593 }
24594
24595 /// @brief create a JSON value from an input in BJData format
24596 /// @sa https://json.nlohmann.me/api/basic_json/from_bjdata/
24597 template<typename InputType>
24598 JSON_HEDLEY_WARN_UNUSED_RESULT
24599 static basic_json from_bjdata(InputType&& i,
24600 const bool strict = true,
24601 const bool allow_exceptions = true)
24602 {
24603 basic_json result;
24604 auto ia = detail::input_adapter(std::forward<InputType>(i));
24605 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24606 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]
24607 return res ? result : basic_json(value_t::discarded);
24608 }
24609
24610 /// @brief create a JSON value from an input in BJData format
24611 /// @sa https://json.nlohmann.me/api/basic_json/from_bjdata/
24612 template<typename IteratorType>
24613 JSON_HEDLEY_WARN_UNUSED_RESULT
24614 static basic_json from_bjdata(IteratorType first, IteratorType last,
24615 const bool strict = true,
24616 const bool allow_exceptions = true)
24617 {
24618 basic_json result;
24619 auto ia = detail::input_adapter(std::move(first), std::move(last));
24620 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24621 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]
24622 return res ? result : basic_json(value_t::discarded);
24623 }
24624
24625 /// @brief create a JSON value from an input in BSON format
24626 /// @sa https://json.nlohmann.me/api/basic_json/from_bson/
24627 template<typename InputType>
24628 JSON_HEDLEY_WARN_UNUSED_RESULT
24629 static basic_json from_bson(InputType&& i,
24630 const bool strict = true,
24631 const bool allow_exceptions = true)
24632 {
24633 basic_json result;
24634 auto ia = detail::input_adapter(std::forward<InputType>(i));
24635 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24636 const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict); // cppcheck-suppress[accessMoved]
24637 return res ? result : basic_json(value_t::discarded);
24638 }
24639
24640 /// @brief create a JSON value from an input in BSON format
24641 /// @sa https://json.nlohmann.me/api/basic_json/from_bson/
24642 template<typename IteratorType>
24643 JSON_HEDLEY_WARN_UNUSED_RESULT
24644 static basic_json from_bson(IteratorType first, IteratorType last,
24645 const bool strict = true,
24646 const bool allow_exceptions = true)
24647 {
24648 basic_json result;
24649 auto ia = detail::input_adapter(std::move(first), std::move(last));
24650 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24651 const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict); // cppcheck-suppress[accessMoved]
24652 return res ? result : basic_json(value_t::discarded);
24653 }
24654
24655 template<typename T>
24656 JSON_HEDLEY_WARN_UNUSED_RESULT
24657 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len))
24658 static basic_json from_bson(const T* ptr, std::size_t len,
24659 const bool strict = true,
24660 const bool allow_exceptions = true)
24661 {
24662 return from_bson(ptr, ptr + len, strict, allow_exceptions);
24663 }
24664
24665 JSON_HEDLEY_WARN_UNUSED_RESULT
24666 JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len))
24667 static basic_json from_bson(detail::span_input_adapter&& i,
24668 const bool strict = true,
24669 const bool allow_exceptions = true)
24670 {
24671 basic_json result;
24672 auto ia = i.get();
24673 detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
24674 // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
24675 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]
24676 return res ? result : basic_json(value_t::discarded);
24677 }
24678 /// @}
24679
24680 //////////////////////////
24681 // JSON Pointer support //
24682 //////////////////////////
24683
24684 /// @name JSON Pointer functions
24685 /// @{
24686
24687 /// @brief access specified element via JSON Pointer
24688 /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
24689 reference operator[](const json_pointer& ptr)
24690 {
24691 return ptr.get_unchecked(this);
24692 }
24693
24694 template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
24695 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
24696 reference operator[](const ::nlohmann::json_pointer<BasicJsonType>& ptr)
24697 {
24698 return ptr.get_unchecked(this);
24699 }
24700
24701 /// @brief access specified element via JSON Pointer
24702 /// @sa https://json.nlohmann.me/api/basic_json/operator%5B%5D/
24703 const_reference operator[](const json_pointer& ptr) const
24704 {
24705 return ptr.get_unchecked(this);
24706 }
24707
24708 template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
24709 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
24710 const_reference operator[](const ::nlohmann::json_pointer<BasicJsonType>& ptr) const
24711 {
24712 return ptr.get_unchecked(this);
24713 }
24714
24715 /// @brief access specified element via JSON Pointer
24716 /// @sa https://json.nlohmann.me/api/basic_json/at/
24717 reference at(const json_pointer& ptr)
24718 {
24719 return ptr.get_checked(this);
24720 }
24721
24722 template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
24723 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
24724 reference at(const ::nlohmann::json_pointer<BasicJsonType>& ptr)
24725 {
24726 return ptr.get_checked(this);
24727 }
24728
24729 /// @brief access specified element via JSON Pointer
24730 /// @sa https://json.nlohmann.me/api/basic_json/at/
24731 const_reference at(const json_pointer& ptr) const
24732 {
24733 return ptr.get_checked(this);
24734 }
24735
24736 template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
24737 JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
24738 const_reference at(const ::nlohmann::json_pointer<BasicJsonType>& ptr) const
24739 {
24740 return ptr.get_checked(this);
24741 }
24742
24743 /// @brief return flattened JSON value
24744 /// @sa https://json.nlohmann.me/api/basic_json/flatten/
24745 basic_json flatten() const
24746 {
24747 basic_json result(value_t::object);
24748 json_pointer::flatten("", *this, result);
24749 return result;
24750 }
24751
24752 /// @brief unflatten a previously flattened JSON value
24753 /// @sa https://json.nlohmann.me/api/basic_json/unflatten/
24754 basic_json unflatten() const
24755 {
24756 return json_pointer::unflatten(*this);
24757 }
24758
24759 /// @}
24760
24761 //////////////////////////
24762 // JSON Patch functions //
24763 //////////////////////////
24764
24765 /// @name JSON Patch functions
24766 /// @{
24767
24768 /// @brief applies a JSON patch in-place without copying the object
24769 /// @sa https://json.nlohmann.me/api/basic_json/patch/
24770 void patch_inplace(const basic_json& json_patch)
24771 {
24772 basic_json& result = *this;
24773 // the valid JSON Patch operations
24774 enum class patch_operations {add, remove, replace, move, copy, test, invalid};
24775
24776 const auto get_op = [](const string_t& op)
24777 {
24778 if (op == "add")
24779 {
24780 return patch_operations::add;
24781 }
24782 if (op == "remove")
24783 {
24784 return patch_operations::remove;
24785 }
24786 if (op == "replace")
24787 {
24788 return patch_operations::replace;
24789 }
24790 if (op == "move")
24791 {
24792 return patch_operations::move;
24793 }
24794 if (op == "copy")
24795 {
24796 return patch_operations::copy;
24797 }
24798 if (op == "test")
24799 {
24800 return patch_operations::test;
24801 }
24802
24803 return patch_operations::invalid;
24804 };
24805
24806 // wrapper for "add" operation; add value at ptr
24807 const auto operation_add = [&result](json_pointer & ptr, const basic_json & val)
24808 {
24809 // adding to the root of the target document means replacing it
24810 if (ptr.empty())
24811 {
24812 result = val;
24813 return;
24814 }
24815
24816 // make sure the top element of the pointer exists
24817 json_pointer const top_pointer = ptr.top();
24818 if (top_pointer != ptr)
24819 {
24820 result.at(top_pointer);
24821 }
24822
24823 // get reference to parent of JSON pointer ptr
24824 const auto last_path = ptr.back();
24825 ptr.pop_back();
24826 // parent must exist when performing patch add per RFC6902 specs
24827 basic_json& parent = result.at(ptr);
24828
24829 switch (parent.m_data.m_type)
24830 {
24831 case value_t::null:
24832 case value_t::object:
24833 {
24834 // use operator[] to add value
24835 parent[last_path] = val;
24836 break;
24837 }
24838
24839 case value_t::array:
24840 {
24841 if (last_path == "-")
24842 {
24843 // special case: append to back
24844 parent.push_back(val);
24845 }
24846 else
24847 {
24848 const auto idx = json_pointer::template array_index<basic_json_t>(last_path);
24849 if (JSON_HEDLEY_UNLIKELY(idx > parent.size()))
24850 {
24851 // avoid undefined behavior
24852 JSON_THROW(out_of_range::create(401, detail::concat("array index ", std::to_string(idx), " is out of range"), &parent));
24853 }
24854
24855 // default case: insert add offset
24856 parent.insert(parent.begin() + static_cast<difference_type>(idx), val);
24857 }
24858 break;
24859 }
24860
24861 // if there exists a parent it cannot be primitive
24862 case value_t::string: // LCOV_EXCL_LINE
24863 case value_t::boolean: // LCOV_EXCL_LINE
24864 case value_t::number_integer: // LCOV_EXCL_LINE
24865 case value_t::number_unsigned: // LCOV_EXCL_LINE
24866 case value_t::number_float: // LCOV_EXCL_LINE
24867 case value_t::binary: // LCOV_EXCL_LINE
24868 case value_t::discarded: // LCOV_EXCL_LINE
24869 default: // LCOV_EXCL_LINE
24870 JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
24871 }
24872 };
24873
24874 // wrapper for "remove" operation; remove value at ptr
24875 const auto operation_remove = [this, & result](json_pointer & ptr)
24876 {
24877 // get reference to parent of JSON pointer ptr
24878 const auto last_path = ptr.back();
24879 ptr.pop_back();
24880 basic_json& parent = result.at(ptr);
24881
24882 // remove child
24883 if (parent.is_object())
24884 {
24885 // perform range check
24886 auto it = parent.find(last_path);
24887 if (JSON_HEDLEY_LIKELY(it != parent.end()))
24888 {
24889 parent.erase(it);
24890 }
24891 else
24892 {
24893 JSON_THROW(out_of_range::create(403, detail::concat("key '", last_path, "' not found"), this));
24894 }
24895 }
24896 else if (parent.is_array())
24897 {
24898 // note erase performs range check
24899 parent.erase(json_pointer::template array_index<basic_json_t>(last_path));
24900 }
24901 };
24902
24903 // type check: top level value must be an array
24904 if (JSON_HEDLEY_UNLIKELY(!json_patch.is_array()))
24905 {
24906 JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", &json_patch));
24907 }
24908
24909 // iterate and apply the operations
24910 for (const auto& val : json_patch)
24911 {
24912 // wrapper to get a value for an operation
24913 const auto get_value = [&val](const string_t& op,
24914 const string_t& member,
24915 bool string_type) -> basic_json &
24916 {
24917 // find value
24918 auto it = val.m_data.m_value.object->find(member);
24919
24920 // context-sensitive error message
24921 const auto error_msg = (op == "op") ? "operation" : detail::concat("operation '", op, '\''); // NOLINT(bugprone-unused-local-non-trivial-variable)
24922
24923 // check if desired value is present
24924 if (JSON_HEDLEY_UNLIKELY(it == val.m_data.m_value.object->end()))
24925 {
24926 // NOLINTNEXTLINE(performance-inefficient-string-concatenation)
24927 JSON_THROW(parse_error::create(105, 0, detail::concat(error_msg, " must have member '", member, "'"), &val));
24928 }
24929
24930 // check if result is of type string
24931 if (JSON_HEDLEY_UNLIKELY(string_type && !it->second.is_string()))
24932 {
24933 // NOLINTNEXTLINE(performance-inefficient-string-concatenation)
24934 JSON_THROW(parse_error::create(105, 0, detail::concat(error_msg, " must have string member '", member, "'"), &val));
24935 }
24936
24937 // no error: return value
24938 return it->second;
24939 };
24940
24941 // type check: every element of the array must be an object
24942 if (JSON_HEDLEY_UNLIKELY(!val.is_object()))
24943 {
24944 JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", &val));
24945 }
24946
24947 // collect mandatory members
24948 const auto op = get_value("op", "op", true).template get<string_t>();
24949 const auto path = get_value(op, "path", true).template get<string_t>();
24950 json_pointer ptr(path);
24951
24952 switch (get_op(op))
24953 {
24954 case patch_operations::add:
24955 {
24956 operation_add(ptr, get_value("add", "value", false));
24957 break;
24958 }
24959
24960 case patch_operations::remove:
24961 {
24962 operation_remove(ptr);
24963 break;
24964 }
24965
24966 case patch_operations::replace:
24967 {
24968 // the "path" location must exist - use at()
24969 result.at(ptr) = get_value("replace", "value", false);
24970 break;
24971 }
24972
24973 case patch_operations::move:
24974 {
24975 const auto from_path = get_value("move", "from", true).template get<string_t>();
24976 json_pointer from_ptr(from_path);
24977
24978 // the "from" location must exist - use at()
24979 basic_json const v = result.at(from_ptr);
24980
24981 // The move operation is functionally identical to a
24982 // "remove" operation on the "from" location, followed
24983 // immediately by an "add" operation at the target
24984 // location with the value that was just removed.
24985 operation_remove(from_ptr);
24986 operation_add(ptr, v);
24987 break;
24988 }
24989
24990 case patch_operations::copy:
24991 {
24992 const auto from_path = get_value("copy", "from", true).template get<string_t>();
24993 const json_pointer from_ptr(from_path);
24994
24995 // the "from" location must exist - use at()
24996 basic_json const v = result.at(from_ptr);
24997
24998 // The copy is functionally identical to an "add"
24999 // operation at the target location using the value
25000 // specified in the "from" member.
25001 operation_add(ptr, v);
25002 break;
25003 }
25004
25005 case patch_operations::test:
25006 {
25007 bool success = false;
25008 JSON_TRY
25009 {
25010 // check if "value" matches the one at "path"
25011 // the "path" location must exist - use at()
25012 success = (result.at(ptr) == get_value("test", "value", false));
25013 }
25014 JSON_INTERNAL_CATCH (out_of_range&)
25015 {
25016 // ignore out of range errors: success remains false
25017 }
25018
25019 // throw an exception if test fails
25020 if (JSON_HEDLEY_UNLIKELY(!success))
25021 {
25022 JSON_THROW(other_error::create(501, detail::concat("unsuccessful: ", val.dump()), &val));
25023 }
25024
25025 break;
25026 }
25027
25028 case patch_operations::invalid:
25029 default:
25030 {
25031 // op must be "add", "remove", "replace", "move", "copy", or
25032 // "test"
25033 JSON_THROW(parse_error::create(105, 0, detail::concat("operation value '", op, "' is invalid"), &val));
25034 }
25035 }
25036 }
25037 }
25038
25039 /// @brief applies a JSON patch to a copy of the current object
25040 /// @sa https://json.nlohmann.me/api/basic_json/patch/
25041 basic_json patch(const basic_json& json_patch) const
25042 {
25043 basic_json result = *this;
25044 result.patch_inplace(json_patch);
25045 return result;
25046 }
25047
25048 /// @brief creates a diff as a JSON patch
25049 /// @sa https://json.nlohmann.me/api/basic_json/diff/
25050 JSON_HEDLEY_WARN_UNUSED_RESULT
25051 static basic_json diff(const basic_json& source, const basic_json& target,
25052 const string_t& path = "")
25053 {
25054 // the patch
25055 basic_json result(value_t::array);
25056
25057 // if the values are the same, return empty patch
25058 if (source == target)
25059 {
25060 return result;
25061 }
25062
25063 if (source.type() != target.type())
25064 {
25065 // different types: replace value
25066 result.push_back(
25067 {
25068 {"op", "replace"}, {"path", path}, {"value", target}
25069 });
25070 return result;
25071 }
25072
25073 switch (source.type())
25074 {
25075 case value_t::array:
25076 {
25077 // first pass: traverse common elements
25078 std::size_t i = 0;
25079 while (i < source.size() && i < target.size())
25080 {
25081 // recursive call to compare array values at index i
25082 auto temp_diff = diff(source[i], target[i], detail::concat<string_t>(path, '/', detail::to_string<string_t>(i)));
25083 result.insert(result.end(), temp_diff.begin(), temp_diff.end());
25084 ++i;
25085 }
25086
25087 // We now reached the end of at least one array
25088 // in a second pass, traverse the remaining elements
25089
25090 // remove my remaining elements
25091 const auto end_index = static_cast<difference_type>(result.size());
25092 while (i < source.size())
25093 {
25094 // add operations in reverse order to avoid invalid
25095 // indices
25096 result.insert(result.begin() + end_index, object(
25097 {
25098 {"op", "remove"},
25099 {"path", detail::concat<string_t>(path, '/', detail::to_string<string_t>(i))}
25100 }));
25101 ++i;
25102 }
25103
25104 // add other remaining elements
25105 while (i < target.size())
25106 {
25107 result.push_back(
25108 {
25109 {"op", "add"},
25110 {"path", detail::concat<string_t>(path, "/-")},
25111 {"value", target[i]}
25112 });
25113 ++i;
25114 }
25115
25116 break;
25117 }
25118
25119 case value_t::object:
25120 {
25121 // first pass: traverse this object's elements
25122 for (auto it = source.cbegin(); it != source.cend(); ++it)
25123 {
25124 // escape the key name to be used in a JSON patch
25125 const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
25126
25127 if (target.find(it.key()) != target.end())
25128 {
25129 // recursive call to compare object values at key it
25130 auto temp_diff = diff(it.value(), target[it.key()], path_key);
25131 result.insert(result.end(), temp_diff.begin(), temp_diff.end());
25132 }
25133 else
25134 {
25135 // found a key that is not in o -> remove it
25136 result.push_back(object(
25137 {
25138 {"op", "remove"}, {"path", path_key}
25139 }));
25140 }
25141 }
25142
25143 // second pass: traverse other object's elements
25144 for (auto it = target.cbegin(); it != target.cend(); ++it)
25145 {
25146 if (source.find(it.key()) == source.end())
25147 {
25148 // found a key that is not in this -> add it
25149 const auto path_key = detail::concat<string_t>(path, '/', detail::escape(it.key()));
25150 result.push_back(
25151 {
25152 {"op", "add"}, {"path", path_key},
25153 {"value", it.value()}
25154 });
25155 }
25156 }
25157
25158 break;
25159 }
25160
25161 case value_t::null:
25162 case value_t::string:
25163 case value_t::boolean:
25164 case value_t::number_integer:
25165 case value_t::number_unsigned:
25166 case value_t::number_float:
25167 case value_t::binary:
25168 case value_t::discarded:
25169 default:
25170 {
25171 // both primitive type: replace value
25172 result.push_back(
25173 {
25174 {"op", "replace"}, {"path", path}, {"value", target}
25175 });
25176 break;
25177 }
25178 }
25179
25180 return result;
25181 }
25182 /// @}
25183
25184 ////////////////////////////////
25185 // JSON Merge Patch functions //
25186 ////////////////////////////////
25187
25188 /// @name JSON Merge Patch functions
25189 /// @{
25190
25191 /// @brief applies a JSON Merge Patch
25192 /// @sa https://json.nlohmann.me/api/basic_json/merge_patch/
25193 void merge_patch(const basic_json& apply_patch)
25194 {
25195 if (apply_patch.is_object())
25196 {
25197 if (!is_object())
25198 {
25199 *this = object();
25200 }
25201 for (auto it = apply_patch.begin(); it != apply_patch.end(); ++it)
25202 {
25203 if (it.value().is_null())
25204 {
25205 erase(it.key());
25206 }
25207 else
25208 {
25209 operator[](it.key()).merge_patch(it.value());
25210 }
25211 }
25212 }
25213 else
25214 {
25215 *this = apply_patch;
25216 }
25217 }
25218
25219 /// @}
25220 };
25221
25222 /// @brief user-defined to_string function for JSON values
25223 /// @sa https://json.nlohmann.me/api/basic_json/to_string/
25224 NLOHMANN_BASIC_JSON_TPL_DECLARATION
25225 std::string to_string(const NLOHMANN_BASIC_JSON_TPL& j)
25226 {
25227 return j.dump();
25228 }
25229
25230 inline namespace literals
25231 {
25232 inline namespace json_literals
25233 {
25234
25235 /// @brief user-defined string literal for JSON values
25236 /// @sa https://json.nlohmann.me/api/basic_json/operator_literal_json/
25237 JSON_HEDLEY_NON_NULL(1)
25238 #if !defined(JSON_HEDLEY_GCC_VERSION) || JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0)
25239 inline nlohmann::json operator ""_json(const char* s, std::size_t n)
25240 #else
25241 inline nlohmann::json operator "" _json(const char* s, std::size_t n)
25242 #endif
25243 {
25244 return nlohmann::json::parse(s, s + n);
25245 }
25246
25247 /// @brief user-defined string literal for JSON pointer
25248 /// @sa https://json.nlohmann.me/api/basic_json/operator_literal_json_pointer/
25249 JSON_HEDLEY_NON_NULL(1)
25250 #if !defined(JSON_HEDLEY_GCC_VERSION) || JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0)
25251 inline nlohmann::json::json_pointer operator ""_json_pointer(const char* s, std::size_t n)
25252 #else
25253 inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std::size_t n)
25254 #endif
25255 {
25256 return nlohmann::json::json_pointer(std::string(s, n));
25257 }
25258
25259 } // namespace json_literals
25260 } // namespace literals
25261 NLOHMANN_JSON_NAMESPACE_END
25262
25263 ///////////////////////
25264 // nonmember support //
25265 ///////////////////////
25266
25267 namespace std // NOLINT(cert-dcl58-cpp)
25268 {
25269
25270 /// @brief hash value for JSON objects
25271 /// @sa https://json.nlohmann.me/api/basic_json/std_hash/
25272 NLOHMANN_BASIC_JSON_TPL_DECLARATION
25273 struct hash<nlohmann::NLOHMANN_BASIC_JSON_TPL> // NOLINT(cert-dcl58-cpp)
25274 {
25275 std::size_t operator()(const nlohmann::NLOHMANN_BASIC_JSON_TPL& j) const
25276 {
25277 return nlohmann::detail::hash(j);
25278 }
25279 };
25280
25281 // specialization for std::less<value_t>
25282 template<>
25283 struct less< ::nlohmann::detail::value_t> // do not remove the space after '<', see https://github.com/nlohmann/json/pull/679
25284 {
25285 /*!
25286 @brief compare two value_t enum values
25287 @since version 3.0.0
25288 */
25289 bool operator()(::nlohmann::detail::value_t lhs,
25290 ::nlohmann::detail::value_t rhs) const noexcept
25291 {
25292 #if JSON_HAS_THREE_WAY_COMPARISON
25293 return std::is_lt(lhs <=> rhs); // *NOPAD*
25294 #else
25295 return ::nlohmann::detail::operator<(lhs, rhs);
25296 #endif
25297 }
25298 };
25299
25300 // C++20 prohibit function specialization in the std namespace.
25301 #ifndef JSON_HAS_CPP_20
25302
25303 /// @brief exchanges the values of two JSON objects
25304 /// @sa https://json.nlohmann.me/api/basic_json/std_swap/
25305 NLOHMANN_BASIC_JSON_TPL_DECLARATION
25306 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)
25307 is_nothrow_move_constructible<nlohmann::NLOHMANN_BASIC_JSON_TPL>::value&& // NOLINT(misc-redundant-expression,cppcoreguidelines-noexcept-swap,performance-noexcept-swap)
25308 is_nothrow_move_assignable<nlohmann::NLOHMANN_BASIC_JSON_TPL>::value)
25309 {
25310 j1.swap(j2);
25311 }
25312
25313 #endif
25314
25315 } // namespace std
25316
25317 #if JSON_USE_GLOBAL_UDLS
25318 #if !defined(JSON_HEDLEY_GCC_VERSION) || JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0)
25319 using nlohmann::literals::json_literals::operator ""_json; // NOLINT(misc-unused-using-decls,google-global-names-in-headers)
25320 using nlohmann::literals::json_literals::operator ""_json_pointer; //NOLINT(misc-unused-using-decls,google-global-names-in-headers)
25321 #else
25322 using nlohmann::literals::json_literals::operator "" _json; // NOLINT(misc-unused-using-decls,google-global-names-in-headers)
25323 using nlohmann::literals::json_literals::operator "" _json_pointer; //NOLINT(misc-unused-using-decls,google-global-names-in-headers)
25324 #endif
25325 #endif
25326
25327 // #include <nlohmann/detail/macro_unscope.hpp>
25328 // __ _____ _____ _____
25329 // __| | __| | | | JSON for Modern C++
25330 // | | |__ | | | | | | version 3.12.0
25331 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
25332 //
25333 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
25334 // SPDX-License-Identifier: MIT
25335
25336
25337
25338 // restore clang diagnostic settings
25339 #if defined(__clang__)
25340 #pragma clang diagnostic pop
25341 #endif
25342
25343 // clean up
25344 #undef JSON_ASSERT
25345 #undef JSON_INTERNAL_CATCH
25346 #undef JSON_THROW
25347 #undef JSON_PRIVATE_UNLESS_TESTED
25348 #undef NLOHMANN_BASIC_JSON_TPL_DECLARATION
25349 #undef NLOHMANN_BASIC_JSON_TPL
25350 #undef JSON_EXPLICIT
25351 #undef NLOHMANN_CAN_CALL_STD_FUNC_IMPL
25352 #undef JSON_INLINE_VARIABLE
25353 #undef JSON_NO_UNIQUE_ADDRESS
25354 #undef JSON_DISABLE_ENUM_SERIALIZATION
25355 #undef JSON_USE_GLOBAL_UDLS
25356
25357 #ifndef JSON_TEST_KEEP_MACROS
25358 #undef JSON_CATCH
25359 #undef JSON_TRY
25360 #undef JSON_HAS_CPP_11
25361 #undef JSON_HAS_CPP_14
25362 #undef JSON_HAS_CPP_17
25363 #undef JSON_HAS_CPP_20
25364 #undef JSON_HAS_CPP_23
25365 #undef JSON_HAS_FILESYSTEM
25366 #undef JSON_HAS_EXPERIMENTAL_FILESYSTEM
25367 #undef JSON_HAS_THREE_WAY_COMPARISON
25368 #undef JSON_HAS_RANGES
25369 #undef JSON_HAS_STATIC_RTTI
25370 #undef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
25371 #endif
25372
25373 // #include <nlohmann/thirdparty/hedley/hedley_undef.hpp>
25374 // __ _____ _____ _____
25375 // __| | __| | | | JSON for Modern C++
25376 // | | |__ | | | | | | version 3.12.0
25377 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
25378 //
25379 // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
25380 // SPDX-License-Identifier: MIT
25381
25382
25383
25384 #undef JSON_HEDLEY_ALWAYS_INLINE
25385 #undef JSON_HEDLEY_ARM_VERSION
25386 #undef JSON_HEDLEY_ARM_VERSION_CHECK
25387 #undef JSON_HEDLEY_ARRAY_PARAM
25388 #undef JSON_HEDLEY_ASSUME
25389 #undef JSON_HEDLEY_BEGIN_C_DECLS
25390 #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE
25391 #undef JSON_HEDLEY_CLANG_HAS_BUILTIN
25392 #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE
25393 #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE
25394 #undef JSON_HEDLEY_CLANG_HAS_EXTENSION
25395 #undef JSON_HEDLEY_CLANG_HAS_FEATURE
25396 #undef JSON_HEDLEY_CLANG_HAS_WARNING
25397 #undef JSON_HEDLEY_COMPCERT_VERSION
25398 #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK
25399 #undef JSON_HEDLEY_CONCAT
25400 #undef JSON_HEDLEY_CONCAT3
25401 #undef JSON_HEDLEY_CONCAT3_EX
25402 #undef JSON_HEDLEY_CONCAT_EX
25403 #undef JSON_HEDLEY_CONST
25404 #undef JSON_HEDLEY_CONSTEXPR
25405 #undef JSON_HEDLEY_CONST_CAST
25406 #undef JSON_HEDLEY_CPP_CAST
25407 #undef JSON_HEDLEY_CRAY_VERSION
25408 #undef JSON_HEDLEY_CRAY_VERSION_CHECK
25409 #undef JSON_HEDLEY_C_DECL
25410 #undef JSON_HEDLEY_DEPRECATED
25411 #undef JSON_HEDLEY_DEPRECATED_FOR
25412 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
25413 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_
25414 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
25415 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
25416 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
25417 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
25418 #undef JSON_HEDLEY_DIAGNOSTIC_POP
25419 #undef JSON_HEDLEY_DIAGNOSTIC_PUSH
25420 #undef JSON_HEDLEY_DMC_VERSION
25421 #undef JSON_HEDLEY_DMC_VERSION_CHECK
25422 #undef JSON_HEDLEY_EMPTY_BASES
25423 #undef JSON_HEDLEY_EMSCRIPTEN_VERSION
25424 #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK
25425 #undef JSON_HEDLEY_END_C_DECLS
25426 #undef JSON_HEDLEY_FLAGS
25427 #undef JSON_HEDLEY_FLAGS_CAST
25428 #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE
25429 #undef JSON_HEDLEY_GCC_HAS_BUILTIN
25430 #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE
25431 #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE
25432 #undef JSON_HEDLEY_GCC_HAS_EXTENSION
25433 #undef JSON_HEDLEY_GCC_HAS_FEATURE
25434 #undef JSON_HEDLEY_GCC_HAS_WARNING
25435 #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK
25436 #undef JSON_HEDLEY_GCC_VERSION
25437 #undef JSON_HEDLEY_GCC_VERSION_CHECK
25438 #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE
25439 #undef JSON_HEDLEY_GNUC_HAS_BUILTIN
25440 #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE
25441 #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE
25442 #undef JSON_HEDLEY_GNUC_HAS_EXTENSION
25443 #undef JSON_HEDLEY_GNUC_HAS_FEATURE
25444 #undef JSON_HEDLEY_GNUC_HAS_WARNING
25445 #undef JSON_HEDLEY_GNUC_VERSION
25446 #undef JSON_HEDLEY_GNUC_VERSION_CHECK
25447 #undef JSON_HEDLEY_HAS_ATTRIBUTE
25448 #undef JSON_HEDLEY_HAS_BUILTIN
25449 #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE
25450 #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS
25451 #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE
25452 #undef JSON_HEDLEY_HAS_EXTENSION
25453 #undef JSON_HEDLEY_HAS_FEATURE
25454 #undef JSON_HEDLEY_HAS_WARNING
25455 #undef JSON_HEDLEY_IAR_VERSION
25456 #undef JSON_HEDLEY_IAR_VERSION_CHECK
25457 #undef JSON_HEDLEY_IBM_VERSION
25458 #undef JSON_HEDLEY_IBM_VERSION_CHECK
25459 #undef JSON_HEDLEY_IMPORT
25460 #undef JSON_HEDLEY_INLINE
25461 #undef JSON_HEDLEY_INTEL_CL_VERSION
25462 #undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK
25463 #undef JSON_HEDLEY_INTEL_VERSION
25464 #undef JSON_HEDLEY_INTEL_VERSION_CHECK
25465 #undef JSON_HEDLEY_IS_CONSTANT
25466 #undef JSON_HEDLEY_IS_CONSTEXPR_
25467 #undef JSON_HEDLEY_LIKELY
25468 #undef JSON_HEDLEY_MALLOC
25469 #undef JSON_HEDLEY_MCST_LCC_VERSION
25470 #undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK
25471 #undef JSON_HEDLEY_MESSAGE
25472 #undef JSON_HEDLEY_MSVC_VERSION
25473 #undef JSON_HEDLEY_MSVC_VERSION_CHECK
25474 #undef JSON_HEDLEY_NEVER_INLINE
25475 #undef JSON_HEDLEY_NON_NULL
25476 #undef JSON_HEDLEY_NO_ESCAPE
25477 #undef JSON_HEDLEY_NO_RETURN
25478 #undef JSON_HEDLEY_NO_THROW
25479 #undef JSON_HEDLEY_NULL
25480 #undef JSON_HEDLEY_PELLES_VERSION
25481 #undef JSON_HEDLEY_PELLES_VERSION_CHECK
25482 #undef JSON_HEDLEY_PGI_VERSION
25483 #undef JSON_HEDLEY_PGI_VERSION_CHECK
25484 #undef JSON_HEDLEY_PREDICT
25485 #undef JSON_HEDLEY_PRINTF_FORMAT
25486 #undef JSON_HEDLEY_PRIVATE
25487 #undef JSON_HEDLEY_PUBLIC
25488 #undef JSON_HEDLEY_PURE
25489 #undef JSON_HEDLEY_REINTERPRET_CAST
25490 #undef JSON_HEDLEY_REQUIRE
25491 #undef JSON_HEDLEY_REQUIRE_CONSTEXPR
25492 #undef JSON_HEDLEY_REQUIRE_MSG
25493 #undef JSON_HEDLEY_RESTRICT
25494 #undef JSON_HEDLEY_RETURNS_NON_NULL
25495 #undef JSON_HEDLEY_SENTINEL
25496 #undef JSON_HEDLEY_STATIC_ASSERT
25497 #undef JSON_HEDLEY_STATIC_CAST
25498 #undef JSON_HEDLEY_STRINGIFY
25499 #undef JSON_HEDLEY_STRINGIFY_EX
25500 #undef JSON_HEDLEY_SUNPRO_VERSION
25501 #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK
25502 #undef JSON_HEDLEY_TINYC_VERSION
25503 #undef JSON_HEDLEY_TINYC_VERSION_CHECK
25504 #undef JSON_HEDLEY_TI_ARMCL_VERSION
25505 #undef JSON_HEDLEY_TI_ARMCL_VERSION_CHECK
25506 #undef JSON_HEDLEY_TI_CL2000_VERSION
25507 #undef JSON_HEDLEY_TI_CL2000_VERSION_CHECK
25508 #undef JSON_HEDLEY_TI_CL430_VERSION
25509 #undef JSON_HEDLEY_TI_CL430_VERSION_CHECK
25510 #undef JSON_HEDLEY_TI_CL6X_VERSION
25511 #undef JSON_HEDLEY_TI_CL6X_VERSION_CHECK
25512 #undef JSON_HEDLEY_TI_CL7X_VERSION
25513 #undef JSON_HEDLEY_TI_CL7X_VERSION_CHECK
25514 #undef JSON_HEDLEY_TI_CLPRU_VERSION
25515 #undef JSON_HEDLEY_TI_CLPRU_VERSION_CHECK
25516 #undef JSON_HEDLEY_TI_VERSION
25517 #undef JSON_HEDLEY_TI_VERSION_CHECK
25518 #undef JSON_HEDLEY_UNAVAILABLE
25519 #undef JSON_HEDLEY_UNLIKELY
25520 #undef JSON_HEDLEY_UNPREDICTABLE
25521 #undef JSON_HEDLEY_UNREACHABLE
25522 #undef JSON_HEDLEY_UNREACHABLE_RETURN
25523 #undef JSON_HEDLEY_VERSION
25524 #undef JSON_HEDLEY_VERSION_DECODE_MAJOR
25525 #undef JSON_HEDLEY_VERSION_DECODE_MINOR
25526 #undef JSON_HEDLEY_VERSION_DECODE_REVISION
25527 #undef JSON_HEDLEY_VERSION_ENCODE
25528 #undef JSON_HEDLEY_WARNING
25529 #undef JSON_HEDLEY_WARN_UNUSED_RESULT
25530 #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG
25531 #undef JSON_HEDLEY_FALL_THROUGH
25532
25533
25534
25535 #endif // INCLUDE_NLOHMANN_JSON_HPP_