JSON for Modern C++  3.9.1
json.hpp
1 /*
2  __ _____ _____ _____
3  __| | __| | | | JSON for Modern C++
4 | | |__ | | | | | | version 3.9.1
5 |_____|_____|_____|_|___| https://github.com/nlohmann/json
6 
7 Licensed under the MIT License <http://opensource.org/licenses/MIT>.
8 SPDX-License-Identifier: MIT
9 Copyright (c) 2013-2019 Niels Lohmann <http://nlohmann.me>.
10 
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is
16 furnished to do so, subject to the following conditions:
17 
18 The above copyright notice and this permission notice shall be included in all
19 copies or substantial portions of the Software.
20 
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 SOFTWARE.
28 */
29 
30 #ifndef INCLUDE_NLOHMANN_JSON_HPP_
31 #define INCLUDE_NLOHMANN_JSON_HPP_
32 
33 #define NLOHMANN_JSON_VERSION_MAJOR 3
34 #define NLOHMANN_JSON_VERSION_MINOR 9
35 #define NLOHMANN_JSON_VERSION_PATCH 1
36 
37 #include <algorithm> // all_of, find, for_each
38 #include <cstddef> // nullptr_t, ptrdiff_t, size_t
39 #include <functional> // hash, less
40 #include <initializer_list> // initializer_list
41 #include <iosfwd> // istream, ostream
42 #include <iterator> // random_access_iterator_tag
43 #include <memory> // unique_ptr
44 #include <numeric> // accumulate
45 #include <string> // string, stoi, to_string
46 #include <utility> // declval, forward, move, pair, swap
47 #include <vector> // vector
48 
49 // #include <nlohmann/adl_serializer.hpp>
50 
51 
52 #include <utility>
53 
54 // #include <nlohmann/detail/conversions/from_json.hpp>
55 
56 
57 #include <algorithm> // transform
58 #include <array> // array
59 #include <forward_list> // forward_list
60 #include <iterator> // inserter, front_inserter, end
61 #include <map> // map
62 #include <string> // string
63 #include <tuple> // tuple, make_tuple
64 #include <type_traits> // is_arithmetic, is_same, is_enum, underlying_type, is_convertible
65 #include <unordered_map> // unordered_map
66 #include <utility> // pair, declval
67 #include <valarray> // valarray
68 
69 // #include <nlohmann/detail/exceptions.hpp>
70 
71 
72 #include <exception> // exception
73 #include <stdexcept> // runtime_error
74 #include <string> // to_string
75 
76 // #include <nlohmann/detail/value_t.hpp>
77 
78 
79 #include <array> // array
80 #include <cstddef> // size_t
81 #include <cstdint> // uint8_t
82 #include <string> // string
83 
84 namespace nlohmann
85 {
86 namespace detail
87 {
89 // JSON type enumeration //
91 
116 enum class value_t : std::uint8_t
117 {
118  null,
119  object,
120  array,
121  string,
122  boolean,
123  number_integer,
124  number_unsigned,
125  number_float,
126  binary,
127  discarded
128 };
129 
143 inline bool operator<(const value_t lhs, const value_t rhs) noexcept
144 {
145  static constexpr std::array<std::uint8_t, 9> order = {{
146  0 /* null */, 3 /* object */, 4 /* array */, 5 /* string */,
147  1 /* boolean */, 2 /* integer */, 2 /* unsigned */, 2 /* float */,
148  6 /* binary */
149  }
150  };
151 
152  const auto l_index = static_cast<std::size_t>(lhs);
153  const auto r_index = static_cast<std::size_t>(rhs);
154  return l_index < order.size() && r_index < order.size() && order[l_index] < order[r_index];
155 }
156 } // namespace detail
157 } // namespace nlohmann
158 
159 // #include <nlohmann/detail/string_escape.hpp>
160 
161 
162 #include <string>
163 // #include <nlohmann/detail/macro_scope.hpp>
164 
165 
166 #include <utility> // pair
167 // #include <nlohmann/thirdparty/hedley/hedley.hpp>
168 /* Hedley - https://nemequ.github.io/hedley
169  * Created by Evan Nemerson <evan@nemerson.com>
170  *
171  * To the extent possible under law, the author(s) have dedicated all
172  * copyright and related and neighboring rights to this software to
173  * the public domain worldwide. This software is distributed without
174  * any warranty.
175  *
176  * For details, see <http://creativecommons.org/publicdomain/zero/1.0/>.
177  * SPDX-License-Identifier: CC0-1.0
178  */
179 
180 #if !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < 15)
181 #if defined(JSON_HEDLEY_VERSION)
182  #undef JSON_HEDLEY_VERSION
183 #endif
184 #define JSON_HEDLEY_VERSION 15
185 
186 #if defined(JSON_HEDLEY_STRINGIFY_EX)
187  #undef JSON_HEDLEY_STRINGIFY_EX
188 #endif
189 #define JSON_HEDLEY_STRINGIFY_EX(x) #x
190 
191 #if defined(JSON_HEDLEY_STRINGIFY)
192  #undef JSON_HEDLEY_STRINGIFY
193 #endif
194 #define JSON_HEDLEY_STRINGIFY(x) JSON_HEDLEY_STRINGIFY_EX(x)
195 
196 #if defined(JSON_HEDLEY_CONCAT_EX)
197  #undef JSON_HEDLEY_CONCAT_EX
198 #endif
199 #define JSON_HEDLEY_CONCAT_EX(a,b) a##b
200 
201 #if defined(JSON_HEDLEY_CONCAT)
202  #undef JSON_HEDLEY_CONCAT
203 #endif
204 #define JSON_HEDLEY_CONCAT(a,b) JSON_HEDLEY_CONCAT_EX(a,b)
205 
206 #if defined(JSON_HEDLEY_CONCAT3_EX)
207  #undef JSON_HEDLEY_CONCAT3_EX
208 #endif
209 #define JSON_HEDLEY_CONCAT3_EX(a,b,c) a##b##c
210 
211 #if defined(JSON_HEDLEY_CONCAT3)
212  #undef JSON_HEDLEY_CONCAT3
213 #endif
214 #define JSON_HEDLEY_CONCAT3(a,b,c) JSON_HEDLEY_CONCAT3_EX(a,b,c)
215 
216 #if defined(JSON_HEDLEY_VERSION_ENCODE)
217  #undef JSON_HEDLEY_VERSION_ENCODE
218 #endif
219 #define JSON_HEDLEY_VERSION_ENCODE(major,minor,revision) (((major) * 1000000) + ((minor) * 1000) + (revision))
220 
221 #if defined(JSON_HEDLEY_VERSION_DECODE_MAJOR)
222  #undef JSON_HEDLEY_VERSION_DECODE_MAJOR
223 #endif
224 #define JSON_HEDLEY_VERSION_DECODE_MAJOR(version) ((version) / 1000000)
225 
226 #if defined(JSON_HEDLEY_VERSION_DECODE_MINOR)
227  #undef JSON_HEDLEY_VERSION_DECODE_MINOR
228 #endif
229 #define JSON_HEDLEY_VERSION_DECODE_MINOR(version) (((version) % 1000000) / 1000)
230 
231 #if defined(JSON_HEDLEY_VERSION_DECODE_REVISION)
232  #undef JSON_HEDLEY_VERSION_DECODE_REVISION
233 #endif
234 #define JSON_HEDLEY_VERSION_DECODE_REVISION(version) ((version) % 1000)
235 
236 #if defined(JSON_HEDLEY_GNUC_VERSION)
237  #undef JSON_HEDLEY_GNUC_VERSION
238 #endif
239 #if defined(__GNUC__) && defined(__GNUC_PATCHLEVEL__)
240  #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
241 #elif defined(__GNUC__)
242  #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, 0)
243 #endif
244 
245 #if defined(JSON_HEDLEY_GNUC_VERSION_CHECK)
246  #undef JSON_HEDLEY_GNUC_VERSION_CHECK
247 #endif
248 #if defined(JSON_HEDLEY_GNUC_VERSION)
249  #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GNUC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
250 #else
251  #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (0)
252 #endif
253 
254 #if defined(JSON_HEDLEY_MSVC_VERSION)
255  #undef JSON_HEDLEY_MSVC_VERSION
256 #endif
257 #if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 140000000) && !defined(__ICL)
258  #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 10000000, (_MSC_FULL_VER % 10000000) / 100000, (_MSC_FULL_VER % 100000) / 100)
259 #elif defined(_MSC_FULL_VER) && !defined(__ICL)
260  #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 1000000, (_MSC_FULL_VER % 1000000) / 10000, (_MSC_FULL_VER % 10000) / 10)
261 #elif defined(_MSC_VER) && !defined(__ICL)
262  #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_VER / 100, _MSC_VER % 100, 0)
263 #endif
264 
265 #if defined(JSON_HEDLEY_MSVC_VERSION_CHECK)
266  #undef JSON_HEDLEY_MSVC_VERSION_CHECK
267 #endif
268 #if !defined(JSON_HEDLEY_MSVC_VERSION)
269  #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (0)
270 #elif defined(_MSC_VER) && (_MSC_VER >= 1400)
271  #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 10000000) + (minor * 100000) + (patch)))
272 #elif defined(_MSC_VER) && (_MSC_VER >= 1200)
273  #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 1000000) + (minor * 10000) + (patch)))
274 #else
275  #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_VER >= ((major * 100) + (minor)))
276 #endif
277 
278 #if defined(JSON_HEDLEY_INTEL_VERSION)
279  #undef JSON_HEDLEY_INTEL_VERSION
280 #endif
281 #if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && !defined(__ICL)
282  #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, __INTEL_COMPILER_UPDATE)
283 #elif defined(__INTEL_COMPILER) && !defined(__ICL)
284  #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, 0)
285 #endif
286 
287 #if defined(JSON_HEDLEY_INTEL_VERSION_CHECK)
288  #undef JSON_HEDLEY_INTEL_VERSION_CHECK
289 #endif
290 #if defined(JSON_HEDLEY_INTEL_VERSION)
291  #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
292 #else
293  #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (0)
294 #endif
295 
296 #if defined(JSON_HEDLEY_INTEL_CL_VERSION)
297  #undef JSON_HEDLEY_INTEL_CL_VERSION
298 #endif
299 #if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && defined(__ICL)
300  #define JSON_HEDLEY_INTEL_CL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER, __INTEL_COMPILER_UPDATE, 0)
301 #endif
302 
303 #if defined(JSON_HEDLEY_INTEL_CL_VERSION_CHECK)
304  #undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK
305 #endif
306 #if defined(JSON_HEDLEY_INTEL_CL_VERSION)
307  #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_CL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
308 #else
309  #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (0)
310 #endif
311 
312 #if defined(JSON_HEDLEY_PGI_VERSION)
313  #undef JSON_HEDLEY_PGI_VERSION
314 #endif
315 #if defined(__PGI) && defined(__PGIC__) && defined(__PGIC_MINOR__) && defined(__PGIC_PATCHLEVEL__)
316  #define JSON_HEDLEY_PGI_VERSION JSON_HEDLEY_VERSION_ENCODE(__PGIC__, __PGIC_MINOR__, __PGIC_PATCHLEVEL__)
317 #endif
318 
319 #if defined(JSON_HEDLEY_PGI_VERSION_CHECK)
320  #undef JSON_HEDLEY_PGI_VERSION_CHECK
321 #endif
322 #if defined(JSON_HEDLEY_PGI_VERSION)
323  #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PGI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
324 #else
325  #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (0)
326 #endif
327 
328 #if defined(JSON_HEDLEY_SUNPRO_VERSION)
329  #undef JSON_HEDLEY_SUNPRO_VERSION
330 #endif
331 #if defined(__SUNPRO_C) && (__SUNPRO_C > 0x1000)
332  #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)
333 #elif defined(__SUNPRO_C)
334  #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_C >> 8) & 0xf, (__SUNPRO_C >> 4) & 0xf, (__SUNPRO_C) & 0xf)
335 #elif defined(__SUNPRO_CC) && (__SUNPRO_CC > 0x1000)
336  #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)
337 #elif defined(__SUNPRO_CC)
338  #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_CC >> 8) & 0xf, (__SUNPRO_CC >> 4) & 0xf, (__SUNPRO_CC) & 0xf)
339 #endif
340 
341 #if defined(JSON_HEDLEY_SUNPRO_VERSION_CHECK)
342  #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK
343 #endif
344 #if defined(JSON_HEDLEY_SUNPRO_VERSION)
345  #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_SUNPRO_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
346 #else
347  #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (0)
348 #endif
349 
350 #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION)
351  #undef JSON_HEDLEY_EMSCRIPTEN_VERSION
352 #endif
353 #if defined(__EMSCRIPTEN__)
354  #define JSON_HEDLEY_EMSCRIPTEN_VERSION JSON_HEDLEY_VERSION_ENCODE(__EMSCRIPTEN_major__, __EMSCRIPTEN_minor__, __EMSCRIPTEN_tiny__)
355 #endif
356 
357 #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK)
358  #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK
359 #endif
360 #if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION)
361  #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_EMSCRIPTEN_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
362 #else
363  #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (0)
364 #endif
365 
366 #if defined(JSON_HEDLEY_ARM_VERSION)
367  #undef JSON_HEDLEY_ARM_VERSION
368 #endif
369 #if defined(__CC_ARM) && defined(__ARMCOMPILER_VERSION)
370  #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCOMPILER_VERSION / 1000000, (__ARMCOMPILER_VERSION % 1000000) / 10000, (__ARMCOMPILER_VERSION % 10000) / 100)
371 #elif defined(__CC_ARM) && defined(__ARMCC_VERSION)
372  #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCC_VERSION / 1000000, (__ARMCC_VERSION % 1000000) / 10000, (__ARMCC_VERSION % 10000) / 100)
373 #endif
374 
375 #if defined(JSON_HEDLEY_ARM_VERSION_CHECK)
376  #undef JSON_HEDLEY_ARM_VERSION_CHECK
377 #endif
378 #if defined(JSON_HEDLEY_ARM_VERSION)
379  #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_ARM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
380 #else
381  #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (0)
382 #endif
383 
384 #if defined(JSON_HEDLEY_IBM_VERSION)
385  #undef JSON_HEDLEY_IBM_VERSION
386 #endif
387 #if defined(__ibmxl__)
388  #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ibmxl_version__, __ibmxl_release__, __ibmxl_modification__)
389 #elif defined(__xlC__) && defined(__xlC_ver__)
390  #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8, __xlC__ & 0xff, (__xlC_ver__ >> 8) & 0xff)
391 #elif defined(__xlC__)
392  #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8, __xlC__ & 0xff, 0)
393 #endif
394 
395 #if defined(JSON_HEDLEY_IBM_VERSION_CHECK)
396  #undef JSON_HEDLEY_IBM_VERSION_CHECK
397 #endif
398 #if defined(JSON_HEDLEY_IBM_VERSION)
399  #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IBM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
400 #else
401  #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (0)
402 #endif
403 
404 #if defined(JSON_HEDLEY_TI_VERSION)
405  #undef JSON_HEDLEY_TI_VERSION
406 #endif
407 #if \
408  defined(__TI_COMPILER_VERSION__) && \
409  ( \
410  defined(__TMS470__) || defined(__TI_ARM__) || \
411  defined(__MSP430__) || \
412  defined(__TMS320C2000__) \
413  )
414 #if (__TI_COMPILER_VERSION__ >= 16000000)
415  #define JSON_HEDLEY_TI_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
416 #endif
417 #endif
418 
419 #if defined(JSON_HEDLEY_TI_VERSION_CHECK)
420  #undef JSON_HEDLEY_TI_VERSION_CHECK
421 #endif
422 #if defined(JSON_HEDLEY_TI_VERSION)
423  #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
424 #else
425  #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (0)
426 #endif
427 
428 #if defined(JSON_HEDLEY_TI_CL2000_VERSION)
429  #undef JSON_HEDLEY_TI_CL2000_VERSION
430 #endif
431 #if defined(__TI_COMPILER_VERSION__) && defined(__TMS320C2000__)
432  #define JSON_HEDLEY_TI_CL2000_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
433 #endif
434 
435 #if defined(JSON_HEDLEY_TI_CL2000_VERSION_CHECK)
436  #undef JSON_HEDLEY_TI_CL2000_VERSION_CHECK
437 #endif
438 #if defined(JSON_HEDLEY_TI_CL2000_VERSION)
439  #define JSON_HEDLEY_TI_CL2000_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL2000_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
440 #else
441  #define JSON_HEDLEY_TI_CL2000_VERSION_CHECK(major,minor,patch) (0)
442 #endif
443 
444 #if defined(JSON_HEDLEY_TI_CL430_VERSION)
445  #undef JSON_HEDLEY_TI_CL430_VERSION
446 #endif
447 #if defined(__TI_COMPILER_VERSION__) && defined(__MSP430__)
448  #define JSON_HEDLEY_TI_CL430_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
449 #endif
450 
451 #if defined(JSON_HEDLEY_TI_CL430_VERSION_CHECK)
452  #undef JSON_HEDLEY_TI_CL430_VERSION_CHECK
453 #endif
454 #if defined(JSON_HEDLEY_TI_CL430_VERSION)
455  #define JSON_HEDLEY_TI_CL430_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL430_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
456 #else
457  #define JSON_HEDLEY_TI_CL430_VERSION_CHECK(major,minor,patch) (0)
458 #endif
459 
460 #if defined(JSON_HEDLEY_TI_ARMCL_VERSION)
461  #undef JSON_HEDLEY_TI_ARMCL_VERSION
462 #endif
463 #if defined(__TI_COMPILER_VERSION__) && (defined(__TMS470__) || defined(__TI_ARM__))
464  #define JSON_HEDLEY_TI_ARMCL_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
465 #endif
466 
467 #if defined(JSON_HEDLEY_TI_ARMCL_VERSION_CHECK)
468  #undef JSON_HEDLEY_TI_ARMCL_VERSION_CHECK
469 #endif
470 #if defined(JSON_HEDLEY_TI_ARMCL_VERSION)
471  #define JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_ARMCL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
472 #else
473  #define JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(major,minor,patch) (0)
474 #endif
475 
476 #if defined(JSON_HEDLEY_TI_CL6X_VERSION)
477  #undef JSON_HEDLEY_TI_CL6X_VERSION
478 #endif
479 #if defined(__TI_COMPILER_VERSION__) && defined(__TMS320C6X__)
480  #define JSON_HEDLEY_TI_CL6X_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
481 #endif
482 
483 #if defined(JSON_HEDLEY_TI_CL6X_VERSION_CHECK)
484  #undef JSON_HEDLEY_TI_CL6X_VERSION_CHECK
485 #endif
486 #if defined(JSON_HEDLEY_TI_CL6X_VERSION)
487  #define JSON_HEDLEY_TI_CL6X_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL6X_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
488 #else
489  #define JSON_HEDLEY_TI_CL6X_VERSION_CHECK(major,minor,patch) (0)
490 #endif
491 
492 #if defined(JSON_HEDLEY_TI_CL7X_VERSION)
493  #undef JSON_HEDLEY_TI_CL7X_VERSION
494 #endif
495 #if defined(__TI_COMPILER_VERSION__) && defined(__C7000__)
496  #define JSON_HEDLEY_TI_CL7X_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
497 #endif
498 
499 #if defined(JSON_HEDLEY_TI_CL7X_VERSION_CHECK)
500  #undef JSON_HEDLEY_TI_CL7X_VERSION_CHECK
501 #endif
502 #if defined(JSON_HEDLEY_TI_CL7X_VERSION)
503  #define JSON_HEDLEY_TI_CL7X_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL7X_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
504 #else
505  #define JSON_HEDLEY_TI_CL7X_VERSION_CHECK(major,minor,patch) (0)
506 #endif
507 
508 #if defined(JSON_HEDLEY_TI_CLPRU_VERSION)
509  #undef JSON_HEDLEY_TI_CLPRU_VERSION
510 #endif
511 #if defined(__TI_COMPILER_VERSION__) && defined(__PRU__)
512  #define JSON_HEDLEY_TI_CLPRU_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000))
513 #endif
514 
515 #if defined(JSON_HEDLEY_TI_CLPRU_VERSION_CHECK)
516  #undef JSON_HEDLEY_TI_CLPRU_VERSION_CHECK
517 #endif
518 #if defined(JSON_HEDLEY_TI_CLPRU_VERSION)
519  #define JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CLPRU_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
520 #else
521  #define JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(major,minor,patch) (0)
522 #endif
523 
524 #if defined(JSON_HEDLEY_CRAY_VERSION)
525  #undef JSON_HEDLEY_CRAY_VERSION
526 #endif
527 #if defined(_CRAYC)
528  #if defined(_RELEASE_PATCHLEVEL)
529  #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, _RELEASE_PATCHLEVEL)
530  #else
531  #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, 0)
532  #endif
533 #endif
534 
535 #if defined(JSON_HEDLEY_CRAY_VERSION_CHECK)
536  #undef JSON_HEDLEY_CRAY_VERSION_CHECK
537 #endif
538 #if defined(JSON_HEDLEY_CRAY_VERSION)
539  #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_CRAY_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
540 #else
541  #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (0)
542 #endif
543 
544 #if defined(JSON_HEDLEY_IAR_VERSION)
545  #undef JSON_HEDLEY_IAR_VERSION
546 #endif
547 #if defined(__IAR_SYSTEMS_ICC__)
548  #if __VER__ > 1000
549  #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE((__VER__ / 1000000), ((__VER__ / 1000) % 1000), (__VER__ % 1000))
550  #else
551  #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE(__VER__ / 100, __VER__ % 100, 0)
552  #endif
553 #endif
554 
555 #if defined(JSON_HEDLEY_IAR_VERSION_CHECK)
556  #undef JSON_HEDLEY_IAR_VERSION_CHECK
557 #endif
558 #if defined(JSON_HEDLEY_IAR_VERSION)
559  #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IAR_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
560 #else
561  #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (0)
562 #endif
563 
564 #if defined(JSON_HEDLEY_TINYC_VERSION)
565  #undef JSON_HEDLEY_TINYC_VERSION
566 #endif
567 #if defined(__TINYC__)
568  #define JSON_HEDLEY_TINYC_VERSION JSON_HEDLEY_VERSION_ENCODE(__TINYC__ / 1000, (__TINYC__ / 100) % 10, __TINYC__ % 100)
569 #endif
570 
571 #if defined(JSON_HEDLEY_TINYC_VERSION_CHECK)
572  #undef JSON_HEDLEY_TINYC_VERSION_CHECK
573 #endif
574 #if defined(JSON_HEDLEY_TINYC_VERSION)
575  #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TINYC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
576 #else
577  #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (0)
578 #endif
579 
580 #if defined(JSON_HEDLEY_DMC_VERSION)
581  #undef JSON_HEDLEY_DMC_VERSION
582 #endif
583 #if defined(__DMC__)
584  #define JSON_HEDLEY_DMC_VERSION JSON_HEDLEY_VERSION_ENCODE(__DMC__ >> 8, (__DMC__ >> 4) & 0xf, __DMC__ & 0xf)
585 #endif
586 
587 #if defined(JSON_HEDLEY_DMC_VERSION_CHECK)
588  #undef JSON_HEDLEY_DMC_VERSION_CHECK
589 #endif
590 #if defined(JSON_HEDLEY_DMC_VERSION)
591  #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_DMC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
592 #else
593  #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (0)
594 #endif
595 
596 #if defined(JSON_HEDLEY_COMPCERT_VERSION)
597  #undef JSON_HEDLEY_COMPCERT_VERSION
598 #endif
599 #if defined(__COMPCERT_VERSION__)
600  #define JSON_HEDLEY_COMPCERT_VERSION JSON_HEDLEY_VERSION_ENCODE(__COMPCERT_VERSION__ / 10000, (__COMPCERT_VERSION__ / 100) % 100, __COMPCERT_VERSION__ % 100)
601 #endif
602 
603 #if defined(JSON_HEDLEY_COMPCERT_VERSION_CHECK)
604  #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK
605 #endif
606 #if defined(JSON_HEDLEY_COMPCERT_VERSION)
607  #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_COMPCERT_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
608 #else
609  #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (0)
610 #endif
611 
612 #if defined(JSON_HEDLEY_PELLES_VERSION)
613  #undef JSON_HEDLEY_PELLES_VERSION
614 #endif
615 #if defined(__POCC__)
616  #define JSON_HEDLEY_PELLES_VERSION JSON_HEDLEY_VERSION_ENCODE(__POCC__ / 100, __POCC__ % 100, 0)
617 #endif
618 
619 #if defined(JSON_HEDLEY_PELLES_VERSION_CHECK)
620  #undef JSON_HEDLEY_PELLES_VERSION_CHECK
621 #endif
622 #if defined(JSON_HEDLEY_PELLES_VERSION)
623  #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PELLES_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
624 #else
625  #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (0)
626 #endif
627 
628 #if defined(JSON_HEDLEY_MCST_LCC_VERSION)
629  #undef JSON_HEDLEY_MCST_LCC_VERSION
630 #endif
631 #if defined(__LCC__) && defined(__LCC_MINOR__)
632  #define JSON_HEDLEY_MCST_LCC_VERSION JSON_HEDLEY_VERSION_ENCODE(__LCC__ / 100, __LCC__ % 100, __LCC_MINOR__)
633 #endif
634 
635 #if defined(JSON_HEDLEY_MCST_LCC_VERSION_CHECK)
636  #undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK
637 #endif
638 #if defined(JSON_HEDLEY_MCST_LCC_VERSION)
639  #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_MCST_LCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
640 #else
641  #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (0)
642 #endif
643 
644 #if defined(JSON_HEDLEY_GCC_VERSION)
645  #undef JSON_HEDLEY_GCC_VERSION
646 #endif
647 #if \
648  defined(JSON_HEDLEY_GNUC_VERSION) && \
649  !defined(__clang__) && \
650  !defined(JSON_HEDLEY_INTEL_VERSION) && \
651  !defined(JSON_HEDLEY_PGI_VERSION) && \
652  !defined(JSON_HEDLEY_ARM_VERSION) && \
653  !defined(JSON_HEDLEY_CRAY_VERSION) && \
654  !defined(JSON_HEDLEY_TI_VERSION) && \
655  !defined(JSON_HEDLEY_TI_ARMCL_VERSION) && \
656  !defined(JSON_HEDLEY_TI_CL430_VERSION) && \
657  !defined(JSON_HEDLEY_TI_CL2000_VERSION) && \
658  !defined(JSON_HEDLEY_TI_CL6X_VERSION) && \
659  !defined(JSON_HEDLEY_TI_CL7X_VERSION) && \
660  !defined(JSON_HEDLEY_TI_CLPRU_VERSION) && \
661  !defined(__COMPCERT__) && \
662  !defined(JSON_HEDLEY_MCST_LCC_VERSION)
663  #define JSON_HEDLEY_GCC_VERSION JSON_HEDLEY_GNUC_VERSION
664 #endif
665 
666 #if defined(JSON_HEDLEY_GCC_VERSION_CHECK)
667  #undef JSON_HEDLEY_GCC_VERSION_CHECK
668 #endif
669 #if defined(JSON_HEDLEY_GCC_VERSION)
670  #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch))
671 #else
672  #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (0)
673 #endif
674 
675 #if defined(JSON_HEDLEY_HAS_ATTRIBUTE)
676  #undef JSON_HEDLEY_HAS_ATTRIBUTE
677 #endif
678 #if \
679  defined(__has_attribute) && \
680  ( \
681  (!defined(JSON_HEDLEY_IAR_VERSION) || JSON_HEDLEY_IAR_VERSION_CHECK(8,5,9)) \
682  )
683 # define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) __has_attribute(attribute)
684 #else
685 # define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) (0)
686 #endif
687 
688 #if defined(JSON_HEDLEY_GNUC_HAS_ATTRIBUTE)
689  #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE
690 #endif
691 #if defined(__has_attribute)
692  #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
693 #else
694  #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
695 #endif
696 
697 #if defined(JSON_HEDLEY_GCC_HAS_ATTRIBUTE)
698  #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE
699 #endif
700 #if defined(__has_attribute)
701  #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
702 #else
703  #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
704 #endif
705 
706 #if defined(JSON_HEDLEY_HAS_CPP_ATTRIBUTE)
707  #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE
708 #endif
709 #if \
710  defined(__has_cpp_attribute) && \
711  defined(__cplusplus) && \
712  (!defined(JSON_HEDLEY_SUNPRO_VERSION) || JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0))
713  #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) __has_cpp_attribute(attribute)
714 #else
715  #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) (0)
716 #endif
717 
718 #if defined(JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS)
719  #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS
720 #endif
721 #if !defined(__cplusplus) || !defined(__has_cpp_attribute)
722  #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) (0)
723 #elif \
724  !defined(JSON_HEDLEY_PGI_VERSION) && \
725  !defined(JSON_HEDLEY_IAR_VERSION) && \
726  (!defined(JSON_HEDLEY_SUNPRO_VERSION) || JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0)) && \
727  (!defined(JSON_HEDLEY_MSVC_VERSION) || JSON_HEDLEY_MSVC_VERSION_CHECK(19,20,0))
728  #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) JSON_HEDLEY_HAS_CPP_ATTRIBUTE(ns::attribute)
729 #else
730  #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) (0)
731 #endif
732 
733 #if defined(JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE)
734  #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE
735 #endif
736 #if defined(__has_cpp_attribute) && defined(__cplusplus)
737  #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute)
738 #else
739  #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
740 #endif
741 
742 #if defined(JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE)
743  #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE
744 #endif
745 #if defined(__has_cpp_attribute) && defined(__cplusplus)
746  #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute)
747 #else
748  #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
749 #endif
750 
751 #if defined(JSON_HEDLEY_HAS_BUILTIN)
752  #undef JSON_HEDLEY_HAS_BUILTIN
753 #endif
754 #if defined(__has_builtin)
755  #define JSON_HEDLEY_HAS_BUILTIN(builtin) __has_builtin(builtin)
756 #else
757  #define JSON_HEDLEY_HAS_BUILTIN(builtin) (0)
758 #endif
759 
760 #if defined(JSON_HEDLEY_GNUC_HAS_BUILTIN)
761  #undef JSON_HEDLEY_GNUC_HAS_BUILTIN
762 #endif
763 #if defined(__has_builtin)
764  #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin)
765 #else
766  #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
767 #endif
768 
769 #if defined(JSON_HEDLEY_GCC_HAS_BUILTIN)
770  #undef JSON_HEDLEY_GCC_HAS_BUILTIN
771 #endif
772 #if defined(__has_builtin)
773  #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin)
774 #else
775  #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
776 #endif
777 
778 #if defined(JSON_HEDLEY_HAS_FEATURE)
779  #undef JSON_HEDLEY_HAS_FEATURE
780 #endif
781 #if defined(__has_feature)
782  #define JSON_HEDLEY_HAS_FEATURE(feature) __has_feature(feature)
783 #else
784  #define JSON_HEDLEY_HAS_FEATURE(feature) (0)
785 #endif
786 
787 #if defined(JSON_HEDLEY_GNUC_HAS_FEATURE)
788  #undef JSON_HEDLEY_GNUC_HAS_FEATURE
789 #endif
790 #if defined(__has_feature)
791  #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature)
792 #else
793  #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
794 #endif
795 
796 #if defined(JSON_HEDLEY_GCC_HAS_FEATURE)
797  #undef JSON_HEDLEY_GCC_HAS_FEATURE
798 #endif
799 #if defined(__has_feature)
800  #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature)
801 #else
802  #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
803 #endif
804 
805 #if defined(JSON_HEDLEY_HAS_EXTENSION)
806  #undef JSON_HEDLEY_HAS_EXTENSION
807 #endif
808 #if defined(__has_extension)
809  #define JSON_HEDLEY_HAS_EXTENSION(extension) __has_extension(extension)
810 #else
811  #define JSON_HEDLEY_HAS_EXTENSION(extension) (0)
812 #endif
813 
814 #if defined(JSON_HEDLEY_GNUC_HAS_EXTENSION)
815  #undef JSON_HEDLEY_GNUC_HAS_EXTENSION
816 #endif
817 #if defined(__has_extension)
818  #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension)
819 #else
820  #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
821 #endif
822 
823 #if defined(JSON_HEDLEY_GCC_HAS_EXTENSION)
824  #undef JSON_HEDLEY_GCC_HAS_EXTENSION
825 #endif
826 #if defined(__has_extension)
827  #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension)
828 #else
829  #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
830 #endif
831 
832 #if defined(JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE)
833  #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE
834 #endif
835 #if defined(__has_declspec_attribute)
836  #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) __has_declspec_attribute(attribute)
837 #else
838  #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) (0)
839 #endif
840 
841 #if defined(JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE)
842  #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE
843 #endif
844 #if defined(__has_declspec_attribute)
845  #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute)
846 #else
847  #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
848 #endif
849 
850 #if defined(JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE)
851  #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE
852 #endif
853 #if defined(__has_declspec_attribute)
854  #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute)
855 #else
856  #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
857 #endif
858 
859 #if defined(JSON_HEDLEY_HAS_WARNING)
860  #undef JSON_HEDLEY_HAS_WARNING
861 #endif
862 #if defined(__has_warning)
863  #define JSON_HEDLEY_HAS_WARNING(warning) __has_warning(warning)
864 #else
865  #define JSON_HEDLEY_HAS_WARNING(warning) (0)
866 #endif
867 
868 #if defined(JSON_HEDLEY_GNUC_HAS_WARNING)
869  #undef JSON_HEDLEY_GNUC_HAS_WARNING
870 #endif
871 #if defined(__has_warning)
872  #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning)
873 #else
874  #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch)
875 #endif
876 
877 #if defined(JSON_HEDLEY_GCC_HAS_WARNING)
878  #undef JSON_HEDLEY_GCC_HAS_WARNING
879 #endif
880 #if defined(__has_warning)
881  #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning)
882 #else
883  #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
884 #endif
885 
886 #if \
887  (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
888  defined(__clang__) || \
889  JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \
890  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
891  JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \
892  JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \
893  JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
894  JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
895  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \
896  JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \
897  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \
898  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,0,0) || \
899  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
900  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
901  JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0) || \
902  JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,17) || \
903  JSON_HEDLEY_SUNPRO_VERSION_CHECK(8,0,0) || \
904  (JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) && defined(__C99_PRAGMA_OPERATOR))
905  #define JSON_HEDLEY_PRAGMA(value) _Pragma(#value)
906 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
907  #define JSON_HEDLEY_PRAGMA(value) __pragma(value)
908 #else
909  #define JSON_HEDLEY_PRAGMA(value)
910 #endif
911 
912 #if defined(JSON_HEDLEY_DIAGNOSTIC_PUSH)
913  #undef JSON_HEDLEY_DIAGNOSTIC_PUSH
914 #endif
915 #if defined(JSON_HEDLEY_DIAGNOSTIC_POP)
916  #undef JSON_HEDLEY_DIAGNOSTIC_POP
917 #endif
918 #if defined(__clang__)
919  #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("clang diagnostic push")
920  #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("clang diagnostic pop")
921 #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
922  #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)")
923  #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)")
924 #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0)
925  #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push")
926  #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("GCC diagnostic pop")
927 #elif \
928  JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \
929  JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
930  #define JSON_HEDLEY_DIAGNOSTIC_PUSH __pragma(warning(push))
931  #define JSON_HEDLEY_DIAGNOSTIC_POP __pragma(warning(pop))
932 #elif JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0)
933  #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("push")
934  #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("pop")
935 #elif \
936  JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
937  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
938  JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,4,0) || \
939  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \
940  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
941  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0)
942  #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("diag_push")
943  #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("diag_pop")
944 #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0)
945  #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)")
946  #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)")
947 #else
948  #define JSON_HEDLEY_DIAGNOSTIC_PUSH
949  #define JSON_HEDLEY_DIAGNOSTIC_POP
950 #endif
951 
952 /* JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_ is for
953  HEDLEY INTERNAL USE ONLY. API subject to change without notice. */
954 #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_)
955  #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_
956 #endif
957 #if defined(__cplusplus)
958 # if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat")
959 # if JSON_HEDLEY_HAS_WARNING("-Wc++17-extensions")
960 # if JSON_HEDLEY_HAS_WARNING("-Wc++1z-extensions")
961 # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
962  JSON_HEDLEY_DIAGNOSTIC_PUSH \
963  _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
964  _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \
965  _Pragma("clang diagnostic ignored \"-Wc++1z-extensions\"") \
966  xpr \
967  JSON_HEDLEY_DIAGNOSTIC_POP
968 # else
969 # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
970  JSON_HEDLEY_DIAGNOSTIC_PUSH \
971  _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
972  _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \
973  xpr \
974  JSON_HEDLEY_DIAGNOSTIC_POP
975 # endif
976 # else
977 # define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \
978  JSON_HEDLEY_DIAGNOSTIC_PUSH \
979  _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \
980  xpr \
981  JSON_HEDLEY_DIAGNOSTIC_POP
982 # endif
983 # endif
984 #endif
985 #if !defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_)
986  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(x) x
987 #endif
988 
989 #if defined(JSON_HEDLEY_CONST_CAST)
990  #undef JSON_HEDLEY_CONST_CAST
991 #endif
992 #if defined(__cplusplus)
993 # define JSON_HEDLEY_CONST_CAST(T, expr) (const_cast<T>(expr))
994 #elif \
995  JSON_HEDLEY_HAS_WARNING("-Wcast-qual") || \
996  JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0) || \
997  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
998 # define JSON_HEDLEY_CONST_CAST(T, expr) (__extension__ ({ \
999  JSON_HEDLEY_DIAGNOSTIC_PUSH \
1000  JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL \
1001  ((T) (expr)); \
1002  JSON_HEDLEY_DIAGNOSTIC_POP \
1003  }))
1004 #else
1005 # define JSON_HEDLEY_CONST_CAST(T, expr) ((T) (expr))
1006 #endif
1007 
1008 #if defined(JSON_HEDLEY_REINTERPRET_CAST)
1009  #undef JSON_HEDLEY_REINTERPRET_CAST
1010 #endif
1011 #if defined(__cplusplus)
1012  #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) (reinterpret_cast<T>(expr))
1013 #else
1014  #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) ((T) (expr))
1015 #endif
1016 
1017 #if defined(JSON_HEDLEY_STATIC_CAST)
1018  #undef JSON_HEDLEY_STATIC_CAST
1019 #endif
1020 #if defined(__cplusplus)
1021  #define JSON_HEDLEY_STATIC_CAST(T, expr) (static_cast<T>(expr))
1022 #else
1023  #define JSON_HEDLEY_STATIC_CAST(T, expr) ((T) (expr))
1024 #endif
1025 
1026 #if defined(JSON_HEDLEY_CPP_CAST)
1027  #undef JSON_HEDLEY_CPP_CAST
1028 #endif
1029 #if defined(__cplusplus)
1030 # if JSON_HEDLEY_HAS_WARNING("-Wold-style-cast")
1031 # define JSON_HEDLEY_CPP_CAST(T, expr) \
1032  JSON_HEDLEY_DIAGNOSTIC_PUSH \
1033  _Pragma("clang diagnostic ignored \"-Wold-style-cast\"") \
1034  ((T) (expr)) \
1035  JSON_HEDLEY_DIAGNOSTIC_POP
1036 # elif JSON_HEDLEY_IAR_VERSION_CHECK(8,3,0)
1037 # define JSON_HEDLEY_CPP_CAST(T, expr) \
1038  JSON_HEDLEY_DIAGNOSTIC_PUSH \
1039  _Pragma("diag_suppress=Pe137") \
1040  JSON_HEDLEY_DIAGNOSTIC_POP
1041 # else
1042 # define JSON_HEDLEY_CPP_CAST(T, expr) ((T) (expr))
1043 # endif
1044 #else
1045 # define JSON_HEDLEY_CPP_CAST(T, expr) (expr)
1046 #endif
1047 
1048 #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED)
1049  #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
1050 #endif
1051 #if JSON_HEDLEY_HAS_WARNING("-Wdeprecated-declarations")
1052  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")
1053 #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
1054  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warning(disable:1478 1786)")
1055 #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1056  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:1478 1786))
1057 #elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0)
1058  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1216,1444,1445")
1059 #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
1060  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444")
1061 #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0)
1062  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
1063 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
1064  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:4996))
1065 #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1066  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444")
1067 #elif \
1068  JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1069  (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1070  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1071  (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1072  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1073  (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1074  JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1075  (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1076  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1077  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1078  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0)
1079  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1291,1718")
1080 #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) && !defined(__cplusplus)
1081  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,E_DEPRECATED_ATT,E_DEPRECATED_ATT_MESS)")
1082 #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) && defined(__cplusplus)
1083  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,symdeprecated,symdeprecated2)")
1084 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
1085  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress=Pe1444,Pe1215")
1086 #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0)
1087  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warn(disable:2241)")
1088 #else
1089  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
1090 #endif
1091 
1092 #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS)
1093  #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
1094 #endif
1095 #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
1096  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("clang diagnostic ignored \"-Wunknown-pragmas\"")
1097 #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
1098  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("warning(disable:161)")
1099 #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1100  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:161))
1101 #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
1102  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 1675")
1103 #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0)
1104  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("GCC diagnostic ignored \"-Wunknown-pragmas\"")
1105 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0)
1106  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:4068))
1107 #elif \
1108  JSON_HEDLEY_TI_VERSION_CHECK(16,9,0) || \
1109  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \
1110  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1111  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0)
1112  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163")
1113 #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0)
1114  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163")
1115 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
1116  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress=Pe161")
1117 #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1118  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 161")
1119 #else
1120  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
1121 #endif
1122 
1123 #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES)
1124  #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
1125 #endif
1126 #if JSON_HEDLEY_HAS_WARNING("-Wunknown-attributes")
1127  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("clang diagnostic ignored \"-Wunknown-attributes\"")
1128 #elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0)
1129  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
1130 #elif JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0)
1131  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("warning(disable:1292)")
1132 #elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1133  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:1292))
1134 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,0)
1135  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:5030))
1136 #elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0)
1137  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097,1098")
1138 #elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0)
1139  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097")
1140 #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus)
1141  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("error_messages(off,attrskipunsup)")
1142 #elif \
1143  JSON_HEDLEY_TI_VERSION_CHECK(18,1,0) || \
1144  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \
1145  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0)
1146  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1173")
1147 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
1148  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress=Pe1097")
1149 #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1150  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097")
1151 #else
1152  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
1153 #endif
1154 
1155 #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL)
1156  #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
1157 #endif
1158 #if JSON_HEDLEY_HAS_WARNING("-Wcast-qual")
1159  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("clang diagnostic ignored \"-Wcast-qual\"")
1160 #elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
1161  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("warning(disable:2203 2331)")
1162 #elif JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0)
1163  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("GCC diagnostic ignored \"-Wcast-qual\"")
1164 #else
1165  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
1166 #endif
1167 
1168 #if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION)
1169  #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
1170 #endif
1171 #if JSON_HEDLEY_HAS_WARNING("-Wunused-function")
1172  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("clang diagnostic ignored \"-Wunused-function\"")
1173 #elif JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0)
1174  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("GCC diagnostic ignored \"-Wunused-function\"")
1175 #elif JSON_HEDLEY_MSVC_VERSION_CHECK(1,0,0)
1176  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION __pragma(warning(disable:4505))
1177 #elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1178  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("diag_suppress 3142")
1179 #else
1180  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
1181 #endif
1182 
1183 #if defined(JSON_HEDLEY_DEPRECATED)
1184  #undef JSON_HEDLEY_DEPRECATED
1185 #endif
1186 #if defined(JSON_HEDLEY_DEPRECATED_FOR)
1187  #undef JSON_HEDLEY_DEPRECATED_FOR
1188 #endif
1189 #if \
1190  JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
1191  JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1192  #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated("Since " # since))
1193  #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated("Since " #since "; use " #replacement))
1194 #elif \
1195  (JSON_HEDLEY_HAS_EXTENSION(attribute_deprecated_with_message) && !defined(JSON_HEDLEY_IAR_VERSION)) || \
1196  JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \
1197  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1198  JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \
1199  JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) || \
1200  JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
1201  JSON_HEDLEY_TI_VERSION_CHECK(18,1,0) || \
1202  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(18,1,0) || \
1203  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \
1204  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1205  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0) || \
1206  JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1207  #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__("Since " #since)))
1208  #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__("Since " #since "; use " #replacement)))
1209 #elif defined(__cplusplus) && (__cplusplus >= 201402L)
1210  #define JSON_HEDLEY_DEPRECATED(since) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since)]])
1211  #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since "; use " #replacement)]])
1212 #elif \
1213  JSON_HEDLEY_HAS_ATTRIBUTE(deprecated) || \
1214  JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
1215  JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1216  JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1217  (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1218  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1219  (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1220  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1221  (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1222  JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1223  (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1224  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1225  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1226  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1227  JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
1228  JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
1229  #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__))
1230  #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__))
1231 #elif \
1232  JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
1233  JSON_HEDLEY_PELLES_VERSION_CHECK(6,50,0) || \
1234  JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1235  #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated)
1236  #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated)
1237 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
1238  #define JSON_HEDLEY_DEPRECATED(since) _Pragma("deprecated")
1239  #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) _Pragma("deprecated")
1240 #else
1241  #define JSON_HEDLEY_DEPRECATED(since)
1242  #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement)
1243 #endif
1244 
1245 #if defined(JSON_HEDLEY_UNAVAILABLE)
1246  #undef JSON_HEDLEY_UNAVAILABLE
1247 #endif
1248 #if \
1249  JSON_HEDLEY_HAS_ATTRIBUTE(warning) || \
1250  JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0) || \
1251  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1252  JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1253  #define JSON_HEDLEY_UNAVAILABLE(available_since) __attribute__((__warning__("Not available until " #available_since)))
1254 #else
1255  #define JSON_HEDLEY_UNAVAILABLE(available_since)
1256 #endif
1257 
1258 #if defined()
1259  #undef
1260 #endif
1261 #if defined(_MSG)
1262  #undef _MSG
1263 #endif
1264 #if \
1265  JSON_HEDLEY_HAS_ATTRIBUTE(warn_unused_result) || \
1266  JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
1267  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1268  JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1269  (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1270  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1271  (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1272  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1273  (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1274  JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1275  (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1276  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1277  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1278  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1279  (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \
1280  JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
1281  JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1282  #define __attribute__((__warn_unused_result__))
1283  #define _MSG(msg) __attribute__((__warn_unused_result__))
1284 #elif (JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard) >= 201907L)
1285  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
1286  #define _MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard(msg)]])
1287 #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard)
1288  #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
1289  #define _MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]])
1290 #elif defined(_Check_return_) /* SAL */
1291  #define _Check_return_
1292  #define _MSG(msg) _Check_return_
1293 #else
1294  #define
1295  #define _MSG(msg)
1296 #endif
1297 
1298 #if defined(JSON_HEDLEY_SENTINEL)
1299  #undef JSON_HEDLEY_SENTINEL
1300 #endif
1301 #if \
1302  JSON_HEDLEY_HAS_ATTRIBUTE(sentinel) || \
1303  JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
1304  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1305  JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) || \
1306  JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1307  #define JSON_HEDLEY_SENTINEL(position) __attribute__((__sentinel__(position)))
1308 #else
1309  #define JSON_HEDLEY_SENTINEL(position)
1310 #endif
1311 
1312 #if defined(JSON_HEDLEY_NO_RETURN)
1313  #undef JSON_HEDLEY_NO_RETURN
1314 #endif
1315 #if JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
1316  #define JSON_HEDLEY_NO_RETURN __noreturn
1317 #elif \
1318  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1319  JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1320  #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__))
1321 #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
1322  #define JSON_HEDLEY_NO_RETURN _Noreturn
1323 #elif defined(__cplusplus) && (__cplusplus >= 201103L)
1324  #define JSON_HEDLEY_NO_RETURN JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[noreturn]])
1325 #elif \
1326  JSON_HEDLEY_HAS_ATTRIBUTE(noreturn) || \
1327  JSON_HEDLEY_GCC_VERSION_CHECK(3,2,0) || \
1328  JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
1329  JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1330  JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1331  JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1332  (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1333  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1334  (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1335  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1336  (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1337  JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1338  (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1339  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1340  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1341  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1342  JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
1343  #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__))
1344 #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
1345  #define JSON_HEDLEY_NO_RETURN _Pragma("does_not_return")
1346 #elif \
1347  JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
1348  JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1349  #define JSON_HEDLEY_NO_RETURN __declspec(noreturn)
1350 #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus)
1351  #define JSON_HEDLEY_NO_RETURN _Pragma("FUNC_NEVER_RETURNS;")
1352 #elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3,2,0)
1353  #define JSON_HEDLEY_NO_RETURN __attribute((noreturn))
1354 #elif JSON_HEDLEY_PELLES_VERSION_CHECK(9,0,0)
1355  #define JSON_HEDLEY_NO_RETURN __declspec(noreturn)
1356 #else
1357  #define JSON_HEDLEY_NO_RETURN
1358 #endif
1359 
1360 #if defined(JSON_HEDLEY_NO_ESCAPE)
1361  #undef JSON_HEDLEY_NO_ESCAPE
1362 #endif
1363 #if JSON_HEDLEY_HAS_ATTRIBUTE(noescape)
1364  #define JSON_HEDLEY_NO_ESCAPE __attribute__((__noescape__))
1365 #else
1366  #define JSON_HEDLEY_NO_ESCAPE
1367 #endif
1368 
1369 #if defined(JSON_HEDLEY_UNREACHABLE)
1370  #undef JSON_HEDLEY_UNREACHABLE
1371 #endif
1372 #if defined(JSON_HEDLEY_UNREACHABLE_RETURN)
1373  #undef JSON_HEDLEY_UNREACHABLE_RETURN
1374 #endif
1375 #if defined(JSON_HEDLEY_ASSUME)
1376  #undef JSON_HEDLEY_ASSUME
1377 #endif
1378 #if \
1379  JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
1380  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1381  JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1382  #define JSON_HEDLEY_ASSUME(expr) __assume(expr)
1383 #elif JSON_HEDLEY_HAS_BUILTIN(__builtin_assume)
1384  #define JSON_HEDLEY_ASSUME(expr) __builtin_assume(expr)
1385 #elif \
1386  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
1387  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0)
1388  #if defined(__cplusplus)
1389  #define JSON_HEDLEY_ASSUME(expr) std::_nassert(expr)
1390  #else
1391  #define JSON_HEDLEY_ASSUME(expr) _nassert(expr)
1392  #endif
1393 #endif
1394 #if \
1395  (JSON_HEDLEY_HAS_BUILTIN(__builtin_unreachable) && (!defined(JSON_HEDLEY_ARM_VERSION))) || \
1396  JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \
1397  JSON_HEDLEY_PGI_VERSION_CHECK(18,10,0) || \
1398  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1399  JSON_HEDLEY_IBM_VERSION_CHECK(13,1,5) || \
1400  JSON_HEDLEY_CRAY_VERSION_CHECK(10,0,0) || \
1401  JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1402  #define JSON_HEDLEY_UNREACHABLE() __builtin_unreachable()
1403 #elif defined(JSON_HEDLEY_ASSUME)
1404  #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0)
1405 #endif
1406 #if !defined(JSON_HEDLEY_ASSUME)
1407  #if defined(JSON_HEDLEY_UNREACHABLE)
1408  #define JSON_HEDLEY_ASSUME(expr) JSON_HEDLEY_STATIC_CAST(void, ((expr) ? 1 : (JSON_HEDLEY_UNREACHABLE(), 1)))
1409  #else
1410  #define JSON_HEDLEY_ASSUME(expr) JSON_HEDLEY_STATIC_CAST(void, expr)
1411  #endif
1412 #endif
1413 #if defined(JSON_HEDLEY_UNREACHABLE)
1414  #if \
1415  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
1416  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0)
1417  #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return (JSON_HEDLEY_STATIC_CAST(void, JSON_HEDLEY_ASSUME(0)), (value))
1418  #else
1419  #define JSON_HEDLEY_UNREACHABLE_RETURN(value) JSON_HEDLEY_UNREACHABLE()
1420  #endif
1421 #else
1422  #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return (value)
1423 #endif
1424 #if !defined(JSON_HEDLEY_UNREACHABLE)
1425  #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0)
1426 #endif
1427 
1428 JSON_HEDLEY_DIAGNOSTIC_PUSH
1429 #if JSON_HEDLEY_HAS_WARNING("-Wpedantic")
1430  #pragma clang diagnostic ignored "-Wpedantic"
1431 #endif
1432 #if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat-pedantic") && defined(__cplusplus)
1433  #pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
1434 #endif
1435 #if JSON_HEDLEY_GCC_HAS_WARNING("-Wvariadic-macros",4,0,0)
1436  #if defined(__clang__)
1437  #pragma clang diagnostic ignored "-Wvariadic-macros"
1438  #elif defined(JSON_HEDLEY_GCC_VERSION)
1439  #pragma GCC diagnostic ignored "-Wvariadic-macros"
1440  #endif
1441 #endif
1442 #if defined(JSON_HEDLEY_NON_NULL)
1443  #undef JSON_HEDLEY_NON_NULL
1444 #endif
1445 #if \
1446  JSON_HEDLEY_HAS_ATTRIBUTE(nonnull) || \
1447  JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
1448  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1449  JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0)
1450  #define JSON_HEDLEY_NON_NULL(...) __attribute__((__nonnull__(__VA_ARGS__)))
1451 #else
1452  #define JSON_HEDLEY_NON_NULL(...)
1453 #endif
1454 JSON_HEDLEY_DIAGNOSTIC_POP
1455 
1456 #if defined(JSON_HEDLEY_PRINTF_FORMAT)
1457  #undef JSON_HEDLEY_PRINTF_FORMAT
1458 #endif
1459 #if defined(__MINGW32__) && JSON_HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && !defined(__USE_MINGW_ANSI_STDIO)
1460  #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(ms_printf, string_idx, first_to_check)))
1461 #elif defined(__MINGW32__) && JSON_HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && defined(__USE_MINGW_ANSI_STDIO)
1462  #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(gnu_printf, string_idx, first_to_check)))
1463 #elif \
1464  JSON_HEDLEY_HAS_ATTRIBUTE(format) || \
1465  JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
1466  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1467  JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \
1468  JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1469  JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1470  (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1471  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1472  (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1473  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1474  (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1475  JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1476  (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1477  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1478  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1479  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1480  JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1481  #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(__printf__, string_idx, first_to_check)))
1482 #elif JSON_HEDLEY_PELLES_VERSION_CHECK(6,0,0)
1483  #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __declspec(vaformat(printf,string_idx,first_to_check))
1484 #else
1485  #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check)
1486 #endif
1487 
1488 #if defined(JSON_HEDLEY_CONSTEXPR)
1489  #undef JSON_HEDLEY_CONSTEXPR
1490 #endif
1491 #if defined(__cplusplus)
1492  #if __cplusplus >= 201103L
1493  #define JSON_HEDLEY_CONSTEXPR JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(constexpr)
1494  #endif
1495 #endif
1496 #if !defined(JSON_HEDLEY_CONSTEXPR)
1497  #define JSON_HEDLEY_CONSTEXPR
1498 #endif
1499 
1500 #if defined(JSON_HEDLEY_PREDICT)
1501  #undef JSON_HEDLEY_PREDICT
1502 #endif
1503 #if defined(JSON_HEDLEY_LIKELY)
1504  #undef JSON_HEDLEY_LIKELY
1505 #endif
1506 #if defined(JSON_HEDLEY_UNLIKELY)
1507  #undef JSON_HEDLEY_UNLIKELY
1508 #endif
1509 #if defined(JSON_HEDLEY_UNPREDICTABLE)
1510  #undef JSON_HEDLEY_UNPREDICTABLE
1511 #endif
1512 #if JSON_HEDLEY_HAS_BUILTIN(__builtin_unpredictable)
1513  #define JSON_HEDLEY_UNPREDICTABLE(expr) __builtin_unpredictable((expr))
1514 #endif
1515 #if \
1516  (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect_with_probability) && !defined(JSON_HEDLEY_PGI_VERSION)) || \
1517  JSON_HEDLEY_GCC_VERSION_CHECK(9,0,0) || \
1518  JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1519 # define JSON_HEDLEY_PREDICT(expr, value, probability) __builtin_expect_with_probability( (expr), (value), (probability))
1520 # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) __builtin_expect_with_probability(!!(expr), 1 , (probability))
1521 # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) __builtin_expect_with_probability(!!(expr), 0 , (probability))
1522 # define JSON_HEDLEY_LIKELY(expr) __builtin_expect (!!(expr), 1 )
1523 # define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect (!!(expr), 0 )
1524 #elif \
1525  (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \
1526  JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \
1527  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1528  (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \
1529  JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1530  JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1531  JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1532  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \
1533  JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \
1534  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \
1535  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
1536  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1537  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1538  JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,27) || \
1539  JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
1540  JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1541 # define JSON_HEDLEY_PREDICT(expr, expected, probability) \
1542  (((probability) >= 0.9) ? __builtin_expect((expr), (expected)) : (JSON_HEDLEY_STATIC_CAST(void, expected), (expr)))
1543 # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) \
1544  (__extension__ ({ \
1545  double hedley_probability_ = (probability); \
1546  ((hedley_probability_ >= 0.9) ? __builtin_expect(!!(expr), 1) : ((hedley_probability_ <= 0.1) ? __builtin_expect(!!(expr), 0) : !!(expr))); \
1547  }))
1548 # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) \
1549  (__extension__ ({ \
1550  double hedley_probability_ = (probability); \
1551  ((hedley_probability_ >= 0.9) ? __builtin_expect(!!(expr), 0) : ((hedley_probability_ <= 0.1) ? __builtin_expect(!!(expr), 1) : !!(expr))); \
1552  }))
1553 # define JSON_HEDLEY_LIKELY(expr) __builtin_expect(!!(expr), 1)
1554 # define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect(!!(expr), 0)
1555 #else
1556 # define JSON_HEDLEY_PREDICT(expr, expected, probability) (JSON_HEDLEY_STATIC_CAST(void, expected), (expr))
1557 # define JSON_HEDLEY_PREDICT_TRUE(expr, probability) (!!(expr))
1558 # define JSON_HEDLEY_PREDICT_FALSE(expr, probability) (!!(expr))
1559 # define JSON_HEDLEY_LIKELY(expr) (!!(expr))
1560 # define JSON_HEDLEY_UNLIKELY(expr) (!!(expr))
1561 #endif
1562 #if !defined(JSON_HEDLEY_UNPREDICTABLE)
1563  #define JSON_HEDLEY_UNPREDICTABLE(expr) JSON_HEDLEY_PREDICT(expr, 1, 0.5)
1564 #endif
1565 
1566 #if defined(JSON_HEDLEY_MALLOC)
1567  #undef JSON_HEDLEY_MALLOC
1568 #endif
1569 #if \
1570  JSON_HEDLEY_HAS_ATTRIBUTE(malloc) || \
1571  JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
1572  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1573  JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
1574  JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1575  JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \
1576  JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1577  (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1578  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1579  (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1580  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1581  (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1582  JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1583  (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1584  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1585  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1586  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1587  JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1588  #define JSON_HEDLEY_MALLOC __attribute__((__malloc__))
1589 #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
1590  #define JSON_HEDLEY_MALLOC _Pragma("returns_new_memory")
1591 #elif \
1592  JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
1593  JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1594  #define JSON_HEDLEY_MALLOC __declspec(restrict)
1595 #else
1596  #define JSON_HEDLEY_MALLOC
1597 #endif
1598 
1599 #if defined(JSON_HEDLEY_PURE)
1600  #undef JSON_HEDLEY_PURE
1601 #endif
1602 #if \
1603  JSON_HEDLEY_HAS_ATTRIBUTE(pure) || \
1604  JSON_HEDLEY_GCC_VERSION_CHECK(2,96,0) || \
1605  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1606  JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
1607  JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1608  JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1609  JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1610  (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1611  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1612  (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1613  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1614  (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1615  JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1616  (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1617  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1618  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1619  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1620  JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
1621  JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1622 # define JSON_HEDLEY_PURE __attribute__((__pure__))
1623 #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
1624 # define JSON_HEDLEY_PURE _Pragma("does_not_write_global_data")
1625 #elif defined(__cplusplus) && \
1626  ( \
1627  JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \
1628  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0) || \
1629  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) \
1630  )
1631 # define JSON_HEDLEY_PURE _Pragma("FUNC_IS_PURE;")
1632 #else
1633 # define JSON_HEDLEY_PURE
1634 #endif
1635 
1636 #if defined(JSON_HEDLEY_CONST)
1637  #undef JSON_HEDLEY_CONST
1638 #endif
1639 #if \
1640  JSON_HEDLEY_HAS_ATTRIBUTE(const) || \
1641  JSON_HEDLEY_GCC_VERSION_CHECK(2,5,0) || \
1642  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1643  JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
1644  JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1645  JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1646  JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1647  (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1648  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1649  (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1650  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1651  (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1652  JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1653  (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1654  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1655  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1656  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1657  JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
1658  JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1659  #define JSON_HEDLEY_CONST __attribute__((__const__))
1660 #elif \
1661  JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0)
1662  #define JSON_HEDLEY_CONST _Pragma("no_side_effect")
1663 #else
1664  #define JSON_HEDLEY_CONST JSON_HEDLEY_PURE
1665 #endif
1666 
1667 #if defined(JSON_HEDLEY_RESTRICT)
1668  #undef JSON_HEDLEY_RESTRICT
1669 #endif
1670 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && !defined(__cplusplus)
1671  #define JSON_HEDLEY_RESTRICT restrict
1672 #elif \
1673  JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \
1674  JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \
1675  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1676  JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
1677  JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1678  JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1679  JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \
1680  JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1681  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,4) || \
1682  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \
1683  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1684  (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus)) || \
1685  JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \
1686  defined(__clang__) || \
1687  JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1688  #define JSON_HEDLEY_RESTRICT __restrict
1689 #elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,3,0) && !defined(__cplusplus)
1690  #define JSON_HEDLEY_RESTRICT _Restrict
1691 #else
1692  #define JSON_HEDLEY_RESTRICT
1693 #endif
1694 
1695 #if defined(JSON_HEDLEY_INLINE)
1696  #undef JSON_HEDLEY_INLINE
1697 #endif
1698 #if \
1699  (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \
1700  (defined(__cplusplus) && (__cplusplus >= 199711L))
1701  #define JSON_HEDLEY_INLINE inline
1702 #elif \
1703  defined(JSON_HEDLEY_GCC_VERSION) || \
1704  JSON_HEDLEY_ARM_VERSION_CHECK(6,2,0)
1705  #define JSON_HEDLEY_INLINE __inline__
1706 #elif \
1707  JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \
1708  JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
1709  JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1710  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,1,0) || \
1711  JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \
1712  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \
1713  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \
1714  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1715  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1716  JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1717  #define JSON_HEDLEY_INLINE __inline
1718 #else
1719  #define JSON_HEDLEY_INLINE
1720 #endif
1721 
1722 #if defined(JSON_HEDLEY_ALWAYS_INLINE)
1723  #undef JSON_HEDLEY_ALWAYS_INLINE
1724 #endif
1725 #if \
1726  JSON_HEDLEY_HAS_ATTRIBUTE(always_inline) || \
1727  JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
1728  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1729  JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
1730  JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1731  JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1732  JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1733  (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1734  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1735  (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1736  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1737  (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1738  JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1739  (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1740  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1741  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1742  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1743  JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
1744  JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
1745 # define JSON_HEDLEY_ALWAYS_INLINE __attribute__((__always_inline__)) JSON_HEDLEY_INLINE
1746 #elif \
1747  JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \
1748  JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1749 # define JSON_HEDLEY_ALWAYS_INLINE __forceinline
1750 #elif defined(__cplusplus) && \
1751  ( \
1752  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1753  JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1754  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1755  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
1756  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1757  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) \
1758  )
1759 # define JSON_HEDLEY_ALWAYS_INLINE _Pragma("FUNC_ALWAYS_INLINE;")
1760 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
1761 # define JSON_HEDLEY_ALWAYS_INLINE _Pragma("inline=forced")
1762 #else
1763 # define JSON_HEDLEY_ALWAYS_INLINE JSON_HEDLEY_INLINE
1764 #endif
1765 
1766 #if defined(JSON_HEDLEY_NEVER_INLINE)
1767  #undef JSON_HEDLEY_NEVER_INLINE
1768 #endif
1769 #if \
1770  JSON_HEDLEY_HAS_ATTRIBUTE(noinline) || \
1771  JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \
1772  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1773  JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
1774  JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1775  JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
1776  JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \
1777  (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1778  JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \
1779  (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1780  JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \
1781  (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1782  JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \
1783  (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1784  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \
1785  JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \
1786  JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \
1787  JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \
1788  JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0)
1789  #define JSON_HEDLEY_NEVER_INLINE __attribute__((__noinline__))
1790 #elif \
1791  JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \
1792  JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
1793  #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline)
1794 #elif JSON_HEDLEY_PGI_VERSION_CHECK(10,2,0)
1795  #define JSON_HEDLEY_NEVER_INLINE _Pragma("noinline")
1796 #elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus)
1797  #define JSON_HEDLEY_NEVER_INLINE _Pragma("FUNC_CANNOT_INLINE;")
1798 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
1799  #define JSON_HEDLEY_NEVER_INLINE _Pragma("inline=never")
1800 #elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3,2,0)
1801  #define JSON_HEDLEY_NEVER_INLINE __attribute((noinline))
1802 #elif JSON_HEDLEY_PELLES_VERSION_CHECK(9,0,0)
1803  #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline)
1804 #else
1805  #define JSON_HEDLEY_NEVER_INLINE
1806 #endif
1807 
1808 #if defined(JSON_HEDLEY_PRIVATE)
1809  #undef JSON_HEDLEY_PRIVATE
1810 #endif
1811 #if defined(JSON_HEDLEY_PUBLIC)
1812  #undef JSON_HEDLEY_PUBLIC
1813 #endif
1814 #if defined(JSON_HEDLEY_IMPORT)
1815  #undef JSON_HEDLEY_IMPORT
1816 #endif
1817 #if defined(_WIN32) || defined(__CYGWIN__)
1818 # define JSON_HEDLEY_PRIVATE
1819 # define JSON_HEDLEY_PUBLIC __declspec(dllexport)
1820 # define JSON_HEDLEY_IMPORT __declspec(dllimport)
1821 #else
1822 # if \
1823  JSON_HEDLEY_HAS_ATTRIBUTE(visibility) || \
1824  JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
1825  JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
1826  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1827  JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1828  JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
1829  ( \
1830  defined(__TI_EABI__) && \
1831  ( \
1832  (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \
1833  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) \
1834  ) \
1835  ) || \
1836  JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1837 # define JSON_HEDLEY_PRIVATE __attribute__((__visibility__("hidden")))
1838 # define JSON_HEDLEY_PUBLIC __attribute__((__visibility__("default")))
1839 # else
1840 # define JSON_HEDLEY_PRIVATE
1841 # define JSON_HEDLEY_PUBLIC
1842 # endif
1843 # define JSON_HEDLEY_IMPORT extern
1844 #endif
1845 
1846 #if defined(JSON_HEDLEY_NO_THROW)
1847  #undef JSON_HEDLEY_NO_THROW
1848 #endif
1849 #if \
1850  JSON_HEDLEY_HAS_ATTRIBUTE(nothrow) || \
1851  JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \
1852  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1853  JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1854  #define JSON_HEDLEY_NO_THROW __attribute__((__nothrow__))
1855 #elif \
1856  JSON_HEDLEY_MSVC_VERSION_CHECK(13,1,0) || \
1857  JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \
1858  JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0)
1859  #define JSON_HEDLEY_NO_THROW __declspec(nothrow)
1860 #else
1861  #define JSON_HEDLEY_NO_THROW
1862 #endif
1863 
1864 #if defined(JSON_HEDLEY_FALL_THROUGH)
1865  #undef JSON_HEDLEY_FALL_THROUGH
1866 #endif
1867 #if \
1868  JSON_HEDLEY_HAS_ATTRIBUTE(fallthrough) || \
1869  JSON_HEDLEY_GCC_VERSION_CHECK(7,0,0) || \
1870  JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1871  #define JSON_HEDLEY_FALL_THROUGH __attribute__((__fallthrough__))
1872 #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(clang,fallthrough)
1873  #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[clang::fallthrough]])
1874 #elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(fallthrough)
1875  #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[fallthrough]])
1876 #elif defined(__fallthrough) /* SAL */
1877  #define JSON_HEDLEY_FALL_THROUGH __fallthrough
1878 #else
1879  #define JSON_HEDLEY_FALL_THROUGH
1880 #endif
1881 
1882 #if defined()
1883  #undef
1884 #endif
1885 #if \
1886  JSON_HEDLEY_HAS_ATTRIBUTE(returns_nonnull) || \
1887  JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \
1888  JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1889  #define __attribute__((__returns_nonnull__))
1890 #elif defined(_Ret_notnull_) /* SAL */
1891  #define _Ret_notnull_
1892 #else
1893  #define
1894 #endif
1895 
1896 #if defined(JSON_HEDLEY_ARRAY_PARAM)
1897  #undef JSON_HEDLEY_ARRAY_PARAM
1898 #endif
1899 #if \
1900  defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \
1901  !defined(__STDC_NO_VLA__) && \
1902  !defined(__cplusplus) && \
1903  !defined(JSON_HEDLEY_PGI_VERSION) && \
1904  !defined(JSON_HEDLEY_TINYC_VERSION)
1905  #define JSON_HEDLEY_ARRAY_PARAM(name) (name)
1906 #else
1907  #define JSON_HEDLEY_ARRAY_PARAM(name)
1908 #endif
1909 
1910 #if defined(JSON_HEDLEY_IS_CONSTANT)
1911  #undef JSON_HEDLEY_IS_CONSTANT
1912 #endif
1913 #if defined(JSON_HEDLEY_REQUIRE_CONSTEXPR)
1914  #undef JSON_HEDLEY_REQUIRE_CONSTEXPR
1915 #endif
1916 /* JSON_HEDLEY_IS_CONSTEXPR_ is for
1917  HEDLEY INTERNAL USE ONLY. API subject to change without notice. */
1918 #if defined(JSON_HEDLEY_IS_CONSTEXPR_)
1919  #undef JSON_HEDLEY_IS_CONSTEXPR_
1920 #endif
1921 #if \
1922  JSON_HEDLEY_HAS_BUILTIN(__builtin_constant_p) || \
1923  JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
1924  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1925  JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,19) || \
1926  JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
1927  JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
1928  JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \
1929  (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) && !defined(__cplusplus)) || \
1930  JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
1931  JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10)
1932  #define JSON_HEDLEY_IS_CONSTANT(expr) __builtin_constant_p(expr)
1933 #endif
1934 #if !defined(__cplusplus)
1935 # if \
1936  JSON_HEDLEY_HAS_BUILTIN(__builtin_types_compatible_p) || \
1937  JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \
1938  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
1939  JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \
1940  JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \
1941  JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) || \
1942  JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,24)
1943 #if defined(__INTPTR_TYPE__)
1944  #define JSON_HEDLEY_IS_CONSTEXPR_(expr) __builtin_types_compatible_p(__typeof__((1 ? (void*) ((__INTPTR_TYPE__) ((expr) * 0)) : (int*) 0)), int*)
1945 #else
1946  #include <stdint.h>
1947  #define JSON_HEDLEY_IS_CONSTEXPR_(expr) __builtin_types_compatible_p(__typeof__((1 ? (void*) ((intptr_t) ((expr) * 0)) : (int*) 0)), int*)
1948 #endif
1949 # elif \
1950  ( \
1951  defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) && \
1952  !defined(JSON_HEDLEY_SUNPRO_VERSION) && \
1953  !defined(JSON_HEDLEY_PGI_VERSION) && \
1954  !defined(JSON_HEDLEY_IAR_VERSION)) || \
1955  (JSON_HEDLEY_HAS_EXTENSION(c_generic_selections) && !defined(JSON_HEDLEY_IAR_VERSION)) || \
1956  JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \
1957  JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0) || \
1958  JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \
1959  JSON_HEDLEY_ARM_VERSION_CHECK(5,3,0)
1960 #if defined(__INTPTR_TYPE__)
1961  #define JSON_HEDLEY_IS_CONSTEXPR_(expr) _Generic((1 ? (void*) ((__INTPTR_TYPE__) ((expr) * 0)) : (int*) 0), int*: 1, void*: 0)
1962 #else
1963  #include <stdint.h>
1964  #define JSON_HEDLEY_IS_CONSTEXPR_(expr) _Generic((1 ? (void*) ((intptr_t) * 0) : (int*) 0), int*: 1, void*: 0)
1965 #endif
1966 # elif \
1967  defined(JSON_HEDLEY_GCC_VERSION) || \
1968  defined(JSON_HEDLEY_INTEL_VERSION) || \
1969  defined(JSON_HEDLEY_TINYC_VERSION) || \
1970  defined(JSON_HEDLEY_TI_ARMCL_VERSION) || \
1971  JSON_HEDLEY_TI_CL430_VERSION_CHECK(18,12,0) || \
1972  defined(JSON_HEDLEY_TI_CL2000_VERSION) || \
1973  defined(JSON_HEDLEY_TI_CL6X_VERSION) || \
1974  defined(JSON_HEDLEY_TI_CL7X_VERSION) || \
1975  defined(JSON_HEDLEY_TI_CLPRU_VERSION) || \
1976  defined(__clang__)
1977 # define JSON_HEDLEY_IS_CONSTEXPR_(expr) ( \
1978  sizeof(void) != \
1979  sizeof(*( \
1980  1 ? \
1981  ((void*) ((expr) * 0L) ) : \
1982 ((struct { char v[sizeof(void) * 2]; } *) 1) \
1983  ) \
1984  ) \
1985  )
1986 # endif
1987 #endif
1988 #if defined(JSON_HEDLEY_IS_CONSTEXPR_)
1989  #if !defined(JSON_HEDLEY_IS_CONSTANT)
1990  #define JSON_HEDLEY_IS_CONSTANT(expr) JSON_HEDLEY_IS_CONSTEXPR_(expr)
1991  #endif
1992  #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (JSON_HEDLEY_IS_CONSTEXPR_(expr) ? (expr) : (-1))
1993 #else
1994  #if !defined(JSON_HEDLEY_IS_CONSTANT)
1995  #define JSON_HEDLEY_IS_CONSTANT(expr) (0)
1996  #endif
1997  #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (expr)
1998 #endif
1999 
2000 #if defined(JSON_HEDLEY_BEGIN_C_DECLS)
2001  #undef JSON_HEDLEY_BEGIN_C_DECLS
2002 #endif
2003 #if defined(JSON_HEDLEY_END_C_DECLS)
2004  #undef JSON_HEDLEY_END_C_DECLS
2005 #endif
2006 #if defined(JSON_HEDLEY_C_DECL)
2007  #undef JSON_HEDLEY_C_DECL
2008 #endif
2009 #if defined(__cplusplus)
2010  #define JSON_HEDLEY_BEGIN_C_DECLS extern "C" {
2011  #define JSON_HEDLEY_END_C_DECLS }
2012  #define JSON_HEDLEY_C_DECL extern "C"
2013 #else
2014  #define JSON_HEDLEY_BEGIN_C_DECLS
2015  #define JSON_HEDLEY_END_C_DECLS
2016  #define JSON_HEDLEY_C_DECL
2017 #endif
2018 
2019 #if defined(JSON_HEDLEY_STATIC_ASSERT)
2020  #undef JSON_HEDLEY_STATIC_ASSERT
2021 #endif
2022 #if \
2023  !defined(__cplusplus) && ( \
2024  (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)) || \
2025  (JSON_HEDLEY_HAS_FEATURE(c_static_assert) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \
2026  JSON_HEDLEY_GCC_VERSION_CHECK(6,0,0) || \
2027  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \
2028  defined(_Static_assert) \
2029  )
2030 # define JSON_HEDLEY_STATIC_ASSERT(expr, message) _Static_assert(expr, message)
2031 #elif \
2032  (defined(__cplusplus) && (__cplusplus >= 201103L)) || \
2033  JSON_HEDLEY_MSVC_VERSION_CHECK(16,0,0) || \
2034  JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
2035 # define JSON_HEDLEY_STATIC_ASSERT(expr, message) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(static_assert(expr, message))
2036 #else
2037 # define JSON_HEDLEY_STATIC_ASSERT(expr, message)
2038 #endif
2039 
2040 #if defined(JSON_HEDLEY_NULL)
2041  #undef JSON_HEDLEY_NULL
2042 #endif
2043 #if defined(__cplusplus)
2044  #if __cplusplus >= 201103L
2045  #define JSON_HEDLEY_NULL JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(nullptr)
2046  #elif defined(NULL)
2047  #define JSON_HEDLEY_NULL NULL
2048  #else
2049  #define JSON_HEDLEY_NULL JSON_HEDLEY_STATIC_CAST(void*, 0)
2050  #endif
2051 #elif defined(NULL)
2052  #define JSON_HEDLEY_NULL NULL
2053 #else
2054  #define JSON_HEDLEY_NULL ((void*) 0)
2055 #endif
2056 
2057 #if defined(JSON_HEDLEY_MESSAGE)
2058  #undef JSON_HEDLEY_MESSAGE
2059 #endif
2060 #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
2061 # define JSON_HEDLEY_MESSAGE(msg) \
2062  JSON_HEDLEY_DIAGNOSTIC_PUSH \
2063  JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS \
2064  JSON_HEDLEY_PRAGMA(message msg) \
2065  JSON_HEDLEY_DIAGNOSTIC_POP
2066 #elif \
2067  JSON_HEDLEY_GCC_VERSION_CHECK(4,4,0) || \
2068  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
2069 # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message msg)
2070 #elif JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0)
2071 # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(_CRI message msg)
2072 #elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0)
2073 # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg))
2074 #elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,0,0)
2075 # define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg))
2076 #else
2077 # define JSON_HEDLEY_MESSAGE(msg)
2078 #endif
2079 
2080 #if defined(JSON_HEDLEY_WARNING)
2081  #undef JSON_HEDLEY_WARNING
2082 #endif
2083 #if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas")
2084 # define JSON_HEDLEY_WARNING(msg) \
2085  JSON_HEDLEY_DIAGNOSTIC_PUSH \
2086  JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS \
2087  JSON_HEDLEY_PRAGMA(clang warning msg) \
2088  JSON_HEDLEY_DIAGNOSTIC_POP
2089 #elif \
2090  JSON_HEDLEY_GCC_VERSION_CHECK(4,8,0) || \
2091  JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \
2092  JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0)
2093 # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(GCC warning msg)
2094 #elif \
2095  JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \
2096  JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
2097 # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(message(msg))
2098 #else
2099 # define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_MESSAGE(msg)
2100 #endif
2101 
2102 #if defined(JSON_HEDLEY_REQUIRE)
2103  #undef JSON_HEDLEY_REQUIRE
2104 #endif
2105 #if defined(JSON_HEDLEY_REQUIRE_MSG)
2106  #undef JSON_HEDLEY_REQUIRE_MSG
2107 #endif
2108 #if JSON_HEDLEY_HAS_ATTRIBUTE(diagnose_if)
2109 # if JSON_HEDLEY_HAS_WARNING("-Wgcc-compat")
2110 # define JSON_HEDLEY_REQUIRE(expr) \
2111  JSON_HEDLEY_DIAGNOSTIC_PUSH \
2112  _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \
2113  __attribute__((diagnose_if(!(expr), #expr, "error"))) \
2114  JSON_HEDLEY_DIAGNOSTIC_POP
2115 # define JSON_HEDLEY_REQUIRE_MSG(expr,msg) \
2116  JSON_HEDLEY_DIAGNOSTIC_PUSH \
2117  _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \
2118  __attribute__((diagnose_if(!(expr), msg, "error"))) \
2119  JSON_HEDLEY_DIAGNOSTIC_POP
2120 # else
2121 # define JSON_HEDLEY_REQUIRE(expr) __attribute__((diagnose_if(!(expr), #expr, "error")))
2122 # define JSON_HEDLEY_REQUIRE_MSG(expr,msg) __attribute__((diagnose_if(!(expr), msg, "error")))
2123 # endif
2124 #else
2125 # define JSON_HEDLEY_REQUIRE(expr)
2126 # define JSON_HEDLEY_REQUIRE_MSG(expr,msg)
2127 #endif
2128 
2129 #if defined(JSON_HEDLEY_FLAGS)
2130  #undef JSON_HEDLEY_FLAGS
2131 #endif
2132 #if JSON_HEDLEY_HAS_ATTRIBUTE(flag_enum) && (!defined(__cplusplus) || JSON_HEDLEY_HAS_WARNING("-Wbitfield-enum-conversion"))
2133  #define JSON_HEDLEY_FLAGS __attribute__((__flag_enum__))
2134 #else
2135  #define JSON_HEDLEY_FLAGS
2136 #endif
2137 
2138 #if defined(JSON_HEDLEY_FLAGS_CAST)
2139  #undef JSON_HEDLEY_FLAGS_CAST
2140 #endif
2141 #if JSON_HEDLEY_INTEL_VERSION_CHECK(19,0,0)
2142 # define JSON_HEDLEY_FLAGS_CAST(T, expr) (__extension__ ({ \
2143  JSON_HEDLEY_DIAGNOSTIC_PUSH \
2144  _Pragma("warning(disable:188)") \
2145  ((T) (expr)); \
2146  JSON_HEDLEY_DIAGNOSTIC_POP \
2147  }))
2148 #else
2149 # define JSON_HEDLEY_FLAGS_CAST(T, expr) JSON_HEDLEY_STATIC_CAST(T, expr)
2150 #endif
2151 
2152 #if defined(JSON_HEDLEY_EMPTY_BASES)
2153  #undef JSON_HEDLEY_EMPTY_BASES
2154 #endif
2155 #if \
2156  (JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,23918) && !JSON_HEDLEY_MSVC_VERSION_CHECK(20,0,0)) || \
2157  JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0)
2158  #define JSON_HEDLEY_EMPTY_BASES __declspec(empty_bases)
2159 #else
2160  #define JSON_HEDLEY_EMPTY_BASES
2161 #endif
2162 
2163 /* Remaining macros are deprecated. */
2164 
2165 #if defined(JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK)
2166  #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK
2167 #endif
2168 #if defined(__clang__)
2169  #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) (0)
2170 #else
2171  #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch)
2172 #endif
2173 
2174 #if defined(JSON_HEDLEY_CLANG_HAS_ATTRIBUTE)
2175  #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE
2176 #endif
2177 #define JSON_HEDLEY_CLANG_HAS_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_ATTRIBUTE(attribute)
2178 
2179 #if defined(JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE)
2180  #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE
2181 #endif
2182 #define JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute)
2183 
2184 #if defined(JSON_HEDLEY_CLANG_HAS_BUILTIN)
2185  #undef JSON_HEDLEY_CLANG_HAS_BUILTIN
2186 #endif
2187 #define JSON_HEDLEY_CLANG_HAS_BUILTIN(builtin) JSON_HEDLEY_HAS_BUILTIN(builtin)
2188 
2189 #if defined(JSON_HEDLEY_CLANG_HAS_FEATURE)
2190  #undef JSON_HEDLEY_CLANG_HAS_FEATURE
2191 #endif
2192 #define JSON_HEDLEY_CLANG_HAS_FEATURE(feature) JSON_HEDLEY_HAS_FEATURE(feature)
2193 
2194 #if defined(JSON_HEDLEY_CLANG_HAS_EXTENSION)
2195  #undef JSON_HEDLEY_CLANG_HAS_EXTENSION
2196 #endif
2197 #define JSON_HEDLEY_CLANG_HAS_EXTENSION(extension) JSON_HEDLEY_HAS_EXTENSION(extension)
2198 
2199 #if defined(JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE)
2200  #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE
2201 #endif
2202 #define JSON_HEDLEY_CLANG_HAS_DECLSPEC_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute)
2203 
2204 #if defined(JSON_HEDLEY_CLANG_HAS_WARNING)
2205  #undef JSON_HEDLEY_CLANG_HAS_WARNING
2206 #endif
2207 #define JSON_HEDLEY_CLANG_HAS_WARNING(warning) JSON_HEDLEY_HAS_WARNING(warning)
2208 
2209 #endif /* !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < X) */
2210 
2211 
2212 // This file contains all internal macro definitions
2213 // You MUST include macro_unscope.hpp at the end of json.hpp to undef all of them
2214 
2215 // exclude unsupported compilers
2216 #if !defined(JSON_SKIP_UNSUPPORTED_COMPILER_CHECK)
2217  #if defined(__clang__)
2218  #if (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) < 30400
2219  #error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers"
2220  #endif
2221  #elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER))
2222  #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800
2223  #error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers"
2224  #endif
2225  #endif
2226 #endif
2227 
2228 // C++ language standard detection
2229 #if (defined(__cplusplus) && __cplusplus >= 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L)
2230  #define JSON_HAS_CPP_20
2231  #define JSON_HAS_CPP_17
2232  #define JSON_HAS_CPP_14
2233 #elif (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464
2234  #define JSON_HAS_CPP_17
2235  #define JSON_HAS_CPP_14
2236 #elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1)
2237  #define JSON_HAS_CPP_14
2238 #endif
2239 
2240 // disable float-equal warnings on GCC/clang
2241 #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
2242  #pragma GCC diagnostic push
2243  #pragma GCC diagnostic ignored "-Wfloat-equal"
2244 #endif
2245 
2246 // disable documentation warnings on clang
2247 #if defined(__clang__)
2248  #pragma GCC diagnostic push
2249  #pragma GCC diagnostic ignored "-Wdocumentation"
2250 #endif
2251 
2252 // allow to disable exceptions
2253 #if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION)
2254  #define JSON_THROW(exception) throw exception
2255  #define JSON_TRY try
2256  #define JSON_CATCH(exception) catch(exception)
2257  #define JSON_INTERNAL_CATCH(exception) catch(exception)
2258 #else
2259  #include <cstdlib>
2260  #define JSON_THROW(exception) std::abort()
2261  #define JSON_TRY if(true)
2262  #define JSON_CATCH(exception) if(false)
2263  #define JSON_INTERNAL_CATCH(exception) if(false)
2264 #endif
2265 
2266 // override exception macros
2267 #if defined(JSON_THROW_USER)
2268  #undef JSON_THROW
2269  #define JSON_THROW JSON_THROW_USER
2270 #endif
2271 #if defined(JSON_TRY_USER)
2272  #undef JSON_TRY
2273  #define JSON_TRY JSON_TRY_USER
2274 #endif
2275 #if defined(JSON_CATCH_USER)
2276  #undef JSON_CATCH
2277  #define JSON_CATCH JSON_CATCH_USER
2278  #undef JSON_INTERNAL_CATCH
2279  #define JSON_INTERNAL_CATCH JSON_CATCH_USER
2280 #endif
2281 #if defined(JSON_INTERNAL_CATCH_USER)
2282  #undef JSON_INTERNAL_CATCH
2283  #define JSON_INTERNAL_CATCH JSON_INTERNAL_CATCH_USER
2284 #endif
2285 
2286 // allow to override assert
2287 #if !defined(JSON_ASSERT)
2288  #include <cassert> // assert
2289  #define JSON_ASSERT(x) assert(x)
2290 #endif
2291 
2292 // allow to access some private functions (needed by the test suite)
2293 #if defined(JSON_TESTS_PRIVATE)
2294  #define JSON_PRIVATE_UNLESS_TESTED public
2295 #else
2296  #define JSON_PRIVATE_UNLESS_TESTED private
2297 #endif
2298 
2304 #define NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...) \
2305  template<typename BasicJsonType> \
2306  inline void to_json(BasicJsonType& j, const ENUM_TYPE& e) \
2307  { \
2308  static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
2309  static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
2310  auto it = std::find_if(std::begin(m), std::end(m), \
2311  [e](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
2312  { \
2313  return ej_pair.first == e; \
2314  }); \
2315  j = ((it != std::end(m)) ? it : std::begin(m))->second; \
2316  } \
2317  template<typename BasicJsonType> \
2318  inline void from_json(const BasicJsonType& j, ENUM_TYPE& e) \
2319  { \
2320  static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!"); \
2321  static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__; \
2322  auto it = std::find_if(std::begin(m), std::end(m), \
2323  [&j](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool \
2324  { \
2325  return ej_pair.second == j; \
2326  }); \
2327  e = ((it != std::end(m)) ? it : std::begin(m))->first; \
2328  }
2329 
2330 // Ugly macros to avoid uglier copy-paste when specializing basic_json. They
2331 // may be removed in the future once the class is split.
2332 
2333 #define NLOHMANN_BASIC_JSON_TPL_DECLARATION \
2334  template<template<typename, typename, typename...> class ObjectType, \
2335  template<typename, typename...> class ArrayType, \
2336  class StringType, class BooleanType, class NumberIntegerType, \
2337  class NumberUnsignedType, class NumberFloatType, \
2338  template<typename> class AllocatorType, \
2339  template<typename, typename = void> class JSONSerializer, \
2340  class BinaryType>
2341 
2342 #define NLOHMANN_BASIC_JSON_TPL \
2343  basic_json<ObjectType, ArrayType, StringType, BooleanType, \
2344  NumberIntegerType, NumberUnsignedType, NumberFloatType, \
2345  AllocatorType, JSONSerializer, BinaryType>
2346 
2347 // Macros to simplify conversion from/to types
2348 
2349 #define NLOHMANN_JSON_EXPAND( x ) x
2350 #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
2351 #define NLOHMANN_JSON_PASTE(...) NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_GET_MACRO(__VA_ARGS__, \
2352  NLOHMANN_JSON_PASTE64, \
2353  NLOHMANN_JSON_PASTE63, \
2354  NLOHMANN_JSON_PASTE62, \
2355  NLOHMANN_JSON_PASTE61, \
2356  NLOHMANN_JSON_PASTE60, \
2357  NLOHMANN_JSON_PASTE59, \
2358  NLOHMANN_JSON_PASTE58, \
2359  NLOHMANN_JSON_PASTE57, \
2360  NLOHMANN_JSON_PASTE56, \
2361  NLOHMANN_JSON_PASTE55, \
2362  NLOHMANN_JSON_PASTE54, \
2363  NLOHMANN_JSON_PASTE53, \
2364  NLOHMANN_JSON_PASTE52, \
2365  NLOHMANN_JSON_PASTE51, \
2366  NLOHMANN_JSON_PASTE50, \
2367  NLOHMANN_JSON_PASTE49, \
2368  NLOHMANN_JSON_PASTE48, \
2369  NLOHMANN_JSON_PASTE47, \
2370  NLOHMANN_JSON_PASTE46, \
2371  NLOHMANN_JSON_PASTE45, \
2372  NLOHMANN_JSON_PASTE44, \
2373  NLOHMANN_JSON_PASTE43, \
2374  NLOHMANN_JSON_PASTE42, \
2375  NLOHMANN_JSON_PASTE41, \
2376  NLOHMANN_JSON_PASTE40, \
2377  NLOHMANN_JSON_PASTE39, \
2378  NLOHMANN_JSON_PASTE38, \
2379  NLOHMANN_JSON_PASTE37, \
2380  NLOHMANN_JSON_PASTE36, \
2381  NLOHMANN_JSON_PASTE35, \
2382  NLOHMANN_JSON_PASTE34, \
2383  NLOHMANN_JSON_PASTE33, \
2384  NLOHMANN_JSON_PASTE32, \
2385  NLOHMANN_JSON_PASTE31, \
2386  NLOHMANN_JSON_PASTE30, \
2387  NLOHMANN_JSON_PASTE29, \
2388  NLOHMANN_JSON_PASTE28, \
2389  NLOHMANN_JSON_PASTE27, \
2390  NLOHMANN_JSON_PASTE26, \
2391  NLOHMANN_JSON_PASTE25, \
2392  NLOHMANN_JSON_PASTE24, \
2393  NLOHMANN_JSON_PASTE23, \
2394  NLOHMANN_JSON_PASTE22, \
2395  NLOHMANN_JSON_PASTE21, \
2396  NLOHMANN_JSON_PASTE20, \
2397  NLOHMANN_JSON_PASTE19, \
2398  NLOHMANN_JSON_PASTE18, \
2399  NLOHMANN_JSON_PASTE17, \
2400  NLOHMANN_JSON_PASTE16, \
2401  NLOHMANN_JSON_PASTE15, \
2402  NLOHMANN_JSON_PASTE14, \
2403  NLOHMANN_JSON_PASTE13, \
2404  NLOHMANN_JSON_PASTE12, \
2405  NLOHMANN_JSON_PASTE11, \
2406  NLOHMANN_JSON_PASTE10, \
2407  NLOHMANN_JSON_PASTE9, \
2408  NLOHMANN_JSON_PASTE8, \
2409  NLOHMANN_JSON_PASTE7, \
2410  NLOHMANN_JSON_PASTE6, \
2411  NLOHMANN_JSON_PASTE5, \
2412  NLOHMANN_JSON_PASTE4, \
2413  NLOHMANN_JSON_PASTE3, \
2414  NLOHMANN_JSON_PASTE2, \
2415  NLOHMANN_JSON_PASTE1)(__VA_ARGS__))
2416 #define NLOHMANN_JSON_PASTE2(func, v1) func(v1)
2417 #define NLOHMANN_JSON_PASTE3(func, v1, v2) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE2(func, v2)
2418 #define NLOHMANN_JSON_PASTE4(func, v1, v2, v3) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE3(func, v2, v3)
2419 #define NLOHMANN_JSON_PASTE5(func, v1, v2, v3, v4) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE4(func, v2, v3, v4)
2420 #define NLOHMANN_JSON_PASTE6(func, v1, v2, v3, v4, v5) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE5(func, v2, v3, v4, v5)
2421 #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)
2422 #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)
2423 #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)
2424 #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)
2425 #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)
2426 #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)
2427 #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)
2428 #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)
2429 #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)
2430 #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)
2431 #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)
2432 #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)
2433 #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)
2434 #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)
2435 #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)
2436 #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)
2437 #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)
2438 #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)
2439 #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)
2440 #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)
2441 #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)
2442 #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)
2443 #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)
2444 #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)
2445 #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)
2446 #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)
2447 #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)
2448 #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)
2449 #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)
2450 #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)
2451 #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)
2452 #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)
2453 #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)
2454 #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)
2455 #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)
2456 #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)
2457 #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)
2458 #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)
2459 #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)
2460 #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)
2461 #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)
2462 #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)
2463 #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)
2464 #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)
2465 #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)
2466 #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)
2467 #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)
2468 #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)
2469 #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)
2470 #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)
2471 #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)
2472 #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)
2473 #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)
2474 #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)
2475 #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)
2476 #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)
2477 #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)
2478 #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)
2479 
2480 #define NLOHMANN_JSON_TO(v1) nlohmann_json_j[#v1] = nlohmann_json_t.v1;
2481 #define NLOHMANN_JSON_FROM(v1) nlohmann_json_j.at(#v1).get_to(nlohmann_json_t.v1);
2482 
2488 #define NLOHMANN_DEFINE_TYPE_INTRUSIVE(Type, ...) \
2489  friend void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
2490  friend void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }
2491 
2497 #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Type, ...) \
2498  inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \
2499  inline void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }
2500 
2501 #ifndef JSON_USE_IMPLICIT_CONVERSIONS
2502  #define JSON_USE_IMPLICIT_CONVERSIONS 1
2503 #endif
2504 
2505 #if JSON_USE_IMPLICIT_CONVERSIONS
2506  #define JSON_EXPLICIT
2507 #else
2508  #define JSON_EXPLICIT explicit
2509 #endif
2510 
2511 
2512 namespace nlohmann
2513 {
2514 namespace detail
2515 {
2516 
2530 inline void replace_substring(std::string& s, const std::string& f,
2531  const std::string& t)
2532 {
2533  JSON_ASSERT(!f.empty());
2534  for (auto pos = s.find(f); // find first occurrence of f
2535  pos != std::string::npos; // make sure f was found
2536  s.replace(pos, f.size(), t), // replace with t, and
2537  pos = s.find(f, pos + t.size())) // find next occurrence of f
2538  {}
2539 }
2540 
2548 inline std::string escape(std::string s)
2549 {
2550  replace_substring(s, "~", "~0");
2551  replace_substring(s, "/", "~1");
2552  return s;
2553 }
2554 
2562 static void unescape(std::string& s)
2563 {
2564  replace_substring(s, "~1", "/");
2565  replace_substring(s, "~0", "~");
2566 }
2567 
2568 } // namespace detail
2569 } // namespace nlohmann
2570 
2571 // #include <nlohmann/detail/input/position_t.hpp>
2572 
2573 
2574 #include <cstddef> // size_t
2575 
2576 namespace nlohmann
2577 {
2578 namespace detail
2579 {
2581 struct position_t
2582 {
2584  std::size_t chars_read_total = 0;
2586  std::size_t chars_read_current_line = 0;
2588  std::size_t lines_read = 0;
2589 
2591  constexpr operator size_t() const
2592  {
2593  return chars_read_total;
2594  }
2595 };
2596 
2597 } // namespace detail
2598 } // namespace nlohmann
2599 
2600 // #include <nlohmann/detail/macro_scope.hpp>
2601 
2602 
2603 namespace nlohmann
2604 {
2605 namespace detail
2606 {
2608 // exceptions //
2610 
2639 class exception : public std::exception
2640 {
2641  public:
2643  const char* what() const noexcept override
2644  {
2645  return m.what();
2646  }
2647 
2649  const int id;
2650 
2651  protected:
2652  JSON_HEDLEY_NON_NULL(3)
2653  exception(int id_, const char* what_arg) : id(id_), m(what_arg) {}
2654 
2655  static std::string name(const std::string& ename, int id_)
2656  {
2657  return "[json.exception." + ename + "." + std::to_string(id_) + "] ";
2658  }
2659 
2660  template<typename BasicJsonType>
2661  static std::string diagnostics(const BasicJsonType& leaf_element)
2662  {
2663 #if JSON_DIAGNOSTICS
2664  std::vector<std::string> tokens;
2665  for (const auto* current = &leaf_element; current->m_parent != nullptr; current = current->m_parent)
2666  {
2667  switch (current->m_parent->type())
2668  {
2669  case value_t::array:
2670  {
2671  for (std::size_t i = 0; i < current->m_parent->m_value.array->size(); ++i)
2672  {
2673  if (&current->m_parent->m_value.array->operator[](i) == current)
2674  {
2675  tokens.emplace_back(std::to_string(i));
2676  break;
2677  }
2678  }
2679  break;
2680  }
2681 
2682  case value_t::object:
2683  {
2684  for (const auto& element : *current->m_parent->m_value.object)
2685  {
2686  if (&element.second == current)
2687  {
2688  tokens.emplace_back(element.first.c_str());
2689  break;
2690  }
2691  }
2692  break;
2693  }
2694 
2695  default: // LCOV_EXCL_LINE
2696  break; // LCOV_EXCL_LINE
2697  }
2698  }
2699 
2700  if (tokens.empty())
2701  {
2702  return "";
2703  }
2704 
2705  return "(" + std::accumulate(tokens.rbegin(), tokens.rend(), std::string{},
2706  [](const std::string & a, const std::string & b)
2707  {
2708  return a + "/" + detail::escape(b);
2709  }) + ") ";
2710 #else
2711  return "";
2712 #endif
2713  }
2714 
2715  private:
2717  std::runtime_error m;
2718 };
2719 
2765 class parse_error : public exception
2766 {
2767  public:
2777  template<typename BasicJsonType>
2778  static parse_error create(int id_, const position_t& pos, const std::string& what_arg, const BasicJsonType& context)
2779  {
2780  std::string w = exception::name("parse_error", id_) + "parse error" +
2781  position_string(pos) + ": " + exception::diagnostics(context) + what_arg;
2782  return parse_error(id_, pos.chars_read_total, w.c_str());
2783  }
2784 
2785  template<typename BasicJsonType>
2786  static parse_error create(int id_, std::size_t byte_, const std::string& what_arg, const BasicJsonType& context)
2787  {
2788  std::string w = exception::name("parse_error", id_) + "parse error" +
2789  (byte_ != 0 ? (" at byte " + std::to_string(byte_)) : "") +
2790  ": " + exception::diagnostics(context) + what_arg;
2791  return parse_error(id_, byte_, w.c_str());
2792  }
2793 
2803  const std::size_t byte;
2804 
2805  private:
2806  parse_error(int id_, std::size_t byte_, const char* what_arg)
2807  : exception(id_, what_arg), byte(byte_) {}
2808 
2809  static std::string position_string(const position_t& pos)
2810  {
2811  return " at line " + std::to_string(pos.lines_read + 1) +
2812  ", column " + std::to_string(pos.chars_read_current_line);
2813  }
2814 };
2815 
2853 class invalid_iterator : public exception
2854 {
2855  public:
2856  template<typename BasicJsonType>
2857  static invalid_iterator create(int id_, const std::string& what_arg, const BasicJsonType& context)
2858  {
2859  std::string w = exception::name("invalid_iterator", id_) + exception::diagnostics(context) + what_arg;
2860  return invalid_iterator(id_, w.c_str());
2861  }
2862 
2863  private:
2864  JSON_HEDLEY_NON_NULL(3)
2865  invalid_iterator(int id_, const char* what_arg)
2866  : exception(id_, what_arg) {}
2867 };
2868 
2908 class type_error : public exception
2909 {
2910  public:
2911  template<typename BasicJsonType>
2912  static type_error create(int id_, const std::string& what_arg, const BasicJsonType& context)
2913  {
2914  std::string w = exception::name("type_error", id_) + exception::diagnostics(context) + what_arg;
2915  return type_error(id_, w.c_str());
2916  }
2917 
2918  private:
2919  JSON_HEDLEY_NON_NULL(3)
2920  type_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
2921 };
2922 
2956 class out_of_range : public exception
2957 {
2958  public:
2959  template<typename BasicJsonType>
2960  static out_of_range create(int id_, const std::string& what_arg, const BasicJsonType& context)
2961  {
2962  std::string w = exception::name("out_of_range", id_) + exception::diagnostics(context) + what_arg;
2963  return out_of_range(id_, w.c_str());
2964  }
2965 
2966  private:
2967  JSON_HEDLEY_NON_NULL(3)
2968  out_of_range(int id_, const char* what_arg) : exception(id_, what_arg) {}
2969 };
2970 
2995 class other_error : public exception
2996 {
2997  public:
2998  template<typename BasicJsonType>
2999  static other_error create(int id_, const std::string& what_arg, const BasicJsonType& context)
3000  {
3001  std::string w = exception::name("other_error", id_) + exception::diagnostics(context) + what_arg;
3002  return other_error(id_, w.c_str());
3003  }
3004 
3005  private:
3006  JSON_HEDLEY_NON_NULL(3)
3007  other_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
3008 };
3009 } // namespace detail
3010 } // namespace nlohmann
3011 
3012 // #include <nlohmann/detail/macro_scope.hpp>
3013 
3014 // #include <nlohmann/detail/meta/cpp_future.hpp>
3015 
3016 
3017 #include <cstddef> // size_t
3018 #include <type_traits> // conditional, enable_if, false_type, integral_constant, is_constructible, is_integral, is_same, remove_cv, remove_reference, true_type
3019 #include <utility> // index_sequence, make_index_sequence, index_sequence_for
3020 
3021 // #include <nlohmann/detail/macro_scope.hpp>
3022 
3023 
3024 namespace nlohmann
3025 {
3026 namespace detail
3027 {
3028 
3029 template<typename T>
3030 using uncvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
3031 
3032 #ifdef JSON_HAS_CPP_14
3033 
3034 // the following utilities are natively available in C++14
3035 using std::enable_if_t;
3036 using std::index_sequence;
3037 using std::make_index_sequence;
3038 using std::index_sequence_for;
3039 
3040 #else
3041 
3042 // alias templates to reduce boilerplate
3043 template<bool B, typename T = void>
3044 using enable_if_t = typename std::enable_if<B, T>::type;
3045 
3046 // source: https://stackoverflow.com/a/32223343
3047 template<std::size_t... Ints>
3048 struct index_sequence
3049 {
3050  using type = index_sequence;
3051  using value_type = std::size_t;
3052  static constexpr std::size_t size() noexcept
3053  {
3054  return sizeof...(Ints);
3055  }
3056 };
3057 
3058 template<class Sequence1, class Sequence2>
3059 struct merge_and_renumber;
3060 
3061 template<std::size_t... I1, std::size_t... I2>
3062 struct merge_and_renumber<index_sequence<I1...>, index_sequence<I2...>>
3063  : index_sequence < I1..., (sizeof...(I1) + I2)... > {};
3064 
3065 template<std::size_t N>
3066 struct make_index_sequence
3067  : merge_and_renumber < typename make_index_sequence < N / 2 >::type,
3068  typename make_index_sequence < N - N / 2 >::type > {};
3069 
3070 template<> struct make_index_sequence<0> : index_sequence<> {};
3071 template<> struct make_index_sequence<1> : index_sequence<0> {};
3072 
3073 template<typename... Ts>
3074 using index_sequence_for = make_index_sequence<sizeof...(Ts)>;
3075 
3076 #endif
3077 
3078 // dispatch utility (taken from ranges-v3)
3079 template<unsigned N> struct priority_tag : priority_tag < N - 1 > {};
3080 template<> struct priority_tag<0> {};
3081 
3082 // taken from ranges-v3
3083 template<typename T>
3084 struct static_const
3085 {
3086  static constexpr T value{};
3087 };
3088 
3089 template<typename T>
3090 constexpr T static_const<T>::value;
3091 
3092 } // namespace detail
3093 } // namespace nlohmann
3094 
3095 // #include <nlohmann/detail/meta/type_traits.hpp>
3096 
3097 
3098 #include <limits> // numeric_limits
3099 #include <type_traits> // false_type, is_constructible, is_integral, is_same, true_type
3100 #include <utility> // declval
3101 #include <tuple> // tuple
3102 
3103 // #include <nlohmann/detail/iterators/iterator_traits.hpp>
3104 
3105 
3106 #include <iterator> // random_access_iterator_tag
3107 
3108 // #include <nlohmann/detail/meta/void_t.hpp>
3109 
3110 
3111 namespace nlohmann
3112 {
3113 namespace detail
3114 {
3115 template<typename ...Ts> struct make_void
3116 {
3117  using type = void;
3118 };
3119 template<typename ...Ts> using void_t = typename make_void<Ts...>::type;
3120 } // namespace detail
3121 } // namespace nlohmann
3122 
3123 // #include <nlohmann/detail/meta/cpp_future.hpp>
3124 
3125 
3126 namespace nlohmann
3127 {
3128 namespace detail
3129 {
3130 template<typename It, typename = void>
3131 struct iterator_types {};
3132 
3133 template<typename It>
3134 struct iterator_types <
3135  It,
3136  void_t<typename It::difference_type, typename It::value_type, typename It::pointer,
3137  typename It::reference, typename It::iterator_category >>
3138 {
3139  using difference_type = typename It::difference_type;
3140  using value_type = typename It::value_type;
3141  using pointer = typename It::pointer;
3142  using reference = typename It::reference;
3143  using iterator_category = typename It::iterator_category;
3144 };
3145 
3146 // This is required as some compilers implement std::iterator_traits in a way that
3147 // doesn't work with SFINAE. See https://github.com/nlohmann/json/issues/1341.
3148 template<typename T, typename = void>
3149 struct iterator_traits
3150 {
3151 };
3152 
3153 template<typename T>
3154 struct iterator_traits < T, enable_if_t < !std::is_pointer<T>::value >>
3155  : iterator_types<T>
3156 {
3157 };
3158 
3159 template<typename T>
3160 struct iterator_traits<T*, enable_if_t<std::is_object<T>::value>>
3161 {
3162  using iterator_category = std::random_access_iterator_tag;
3163  using value_type = T;
3164  using difference_type = ptrdiff_t;
3165  using pointer = T*;
3166  using reference = T&;
3167 };
3168 } // namespace detail
3169 } // namespace nlohmann
3170 
3171 // #include <nlohmann/detail/macro_scope.hpp>
3172 
3173 // #include <nlohmann/detail/meta/cpp_future.hpp>
3174 
3175 // #include <nlohmann/detail/meta/detected.hpp>
3176 
3177 
3178 #include <type_traits>
3179 
3180 // #include <nlohmann/detail/meta/void_t.hpp>
3181 
3182 
3183 // https://en.cppreference.com/w/cpp/experimental/is_detected
3184 namespace nlohmann
3185 {
3186 namespace detail
3187 {
3188 struct nonesuch
3189 {
3190  nonesuch() = delete;
3191  ~nonesuch() = delete;
3192  nonesuch(nonesuch const&) = delete;
3193  nonesuch(nonesuch const&&) = delete;
3194  void operator=(nonesuch const&) = delete;
3195  void operator=(nonesuch&&) = delete;
3196 };
3197 
3198 template<class Default,
3199  class AlwaysVoid,
3200  template<class...> class Op,
3201  class... Args>
3202 struct detector
3203 {
3204  using value_t = std::false_type;
3205  using type = Default;
3206 };
3207 
3208 template<class Default, template<class...> class Op, class... Args>
3209 struct detector<Default, void_t<Op<Args...>>, Op, Args...>
3210 {
3211  using value_t = std::true_type;
3212  using type = Op<Args...>;
3213 };
3214 
3215 template<template<class...> class Op, class... Args>
3216 using is_detected = typename detector<nonesuch, void, Op, Args...>::value_t;
3217 
3218 template<template<class...> class Op, class... Args>
3219 using detected_t = typename detector<nonesuch, void, Op, Args...>::type;
3220 
3221 template<class Default, template<class...> class Op, class... Args>
3222 using detected_or = detector<Default, void, Op, Args...>;
3223 
3224 template<class Default, template<class...> class Op, class... Args>
3225 using detected_or_t = typename detected_or<Default, Op, Args...>::type;
3226 
3227 template<class Expected, template<class...> class Op, class... Args>
3228 using is_detected_exact = std::is_same<Expected, detected_t<Op, Args...>>;
3229 
3230 template<class To, template<class...> class Op, class... Args>
3231 using is_detected_convertible =
3232  std::is_convertible<detected_t<Op, Args...>, To>;
3233 } // namespace detail
3234 } // namespace nlohmann
3235 
3236 // #include <nlohmann/json_fwd.hpp>
3237 #ifndef INCLUDE_NLOHMANN_JSON_FWD_HPP_
3238 #define INCLUDE_NLOHMANN_JSON_FWD_HPP_
3239 
3240 #include <cstdint> // int64_t, uint64_t
3241 #include <map> // map
3242 #include <memory> // allocator
3243 #include <string> // string
3244 #include <vector> // vector
3245 
3251 namespace nlohmann
3252 {
3260 template<typename T = void, typename SFINAE = void>
3261 struct adl_serializer;
3262 
3263 template<template<typename U, typename V, typename... Args> class ObjectType =
3264  std::map,
3265  template<typename U, typename... Args> class ArrayType = std::vector,
3266  class StringType = std::string, class BooleanType = bool,
3267  class NumberIntegerType = std::int64_t,
3268  class NumberUnsignedType = std::uint64_t,
3269  class NumberFloatType = double,
3270  template<typename U> class AllocatorType = std::allocator,
3271  template<typename T, typename SFINAE = void> class JSONSerializer =
3272  adl_serializer,
3273  class BinaryType = std::vector<std::uint8_t>>
3274 class basic_json;
3275 
3287 template<typename BasicJsonType>
3288 class json_pointer;
3289 
3299 
3300 template<class Key, class T, class IgnoredLess, class Allocator>
3301 struct ordered_map;
3302 
3311 
3312 } // namespace nlohmann
3313 
3314 #endif // INCLUDE_NLOHMANN_JSON_FWD_HPP_
3315 
3316 
3317 namespace nlohmann
3318 {
3327 namespace detail
3328 {
3330 // helpers //
3332 
3333 // Note to maintainers:
3334 //
3335 // Every trait in this file expects a non CV-qualified type.
3336 // The only exceptions are in the 'aliases for detected' section
3337 // (i.e. those of the form: decltype(T::member_function(std::declval<T>())))
3338 //
3339 // In this case, T has to be properly CV-qualified to constraint the function arguments
3340 // (e.g. to_json(BasicJsonType&, const T&))
3341 
3342 template<typename> struct is_basic_json : std::false_type {};
3343 
3344 NLOHMANN_BASIC_JSON_TPL_DECLARATION
3345 struct is_basic_json<NLOHMANN_BASIC_JSON_TPL> : std::true_type {};
3346 
3348 // json_ref helpers //
3350 
3351 template<typename>
3352 class json_ref;
3353 
3354 template<typename>
3355 struct is_json_ref : std::false_type {};
3356 
3357 template<typename T>
3358 struct is_json_ref<json_ref<T>> : std::true_type {};
3359 
3361 // aliases for detected //
3363 
3364 template<typename T>
3365 using mapped_type_t = typename T::mapped_type;
3366 
3367 template<typename T>
3368 using key_type_t = typename T::key_type;
3369 
3370 template<typename T>
3371 using value_type_t = typename T::value_type;
3372 
3373 template<typename T>
3374 using difference_type_t = typename T::difference_type;
3375 
3376 template<typename T>
3377 using pointer_t = typename T::pointer;
3378 
3379 template<typename T>
3380 using reference_t = typename T::reference;
3381 
3382 template<typename T>
3383 using iterator_category_t = typename T::iterator_category;
3384 
3385 template<typename T>
3386 using iterator_t = typename T::iterator;
3387 
3388 template<typename T, typename... Args>
3389 using to_json_function = decltype(T::to_json(std::declval<Args>()...));
3390 
3391 template<typename T, typename... Args>
3392 using from_json_function = decltype(T::from_json(std::declval<Args>()...));
3393 
3394 template<typename T, typename U>
3395 using get_template_function = decltype(std::declval<T>().template get<U>());
3396 
3397 // trait checking if JSONSerializer<T>::from_json(json const&, udt&) exists
3398 template<typename BasicJsonType, typename T, typename = void>
3399 struct has_from_json : std::false_type {};
3400 
3401 // trait checking if j.get<T> is valid
3402 // use this trait instead of std::is_constructible or std::is_convertible,
3403 // both rely on, or make use of implicit conversions, and thus fail when T
3404 // has several constructors/operator= (see https://github.com/nlohmann/json/issues/958)
3405 template <typename BasicJsonType, typename T>
3406 struct is_getable
3407 {
3408  static constexpr bool value = is_detected<get_template_function, const BasicJsonType&, T>::value;
3409 };
3410 
3411 template<typename BasicJsonType, typename T>
3412 struct has_from_json < BasicJsonType, T,
3413  enable_if_t < !is_basic_json<T>::value >>
3414 {
3415  using serializer = typename BasicJsonType::template json_serializer<T, void>;
3416 
3417  static constexpr bool value =
3418  is_detected_exact<void, from_json_function, serializer,
3419  const BasicJsonType&, T&>::value;
3420 };
3421 
3422 // This trait checks if JSONSerializer<T>::from_json(json const&) exists
3423 // this overload is used for non-default-constructible user-defined-types
3424 template<typename BasicJsonType, typename T, typename = void>
3425 struct has_non_default_from_json : std::false_type {};
3426 
3427 template<typename BasicJsonType, typename T>
3428 struct has_non_default_from_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
3429 {
3430  using serializer = typename BasicJsonType::template json_serializer<T, void>;
3431 
3432  static constexpr bool value =
3433  is_detected_exact<T, from_json_function, serializer,
3434  const BasicJsonType&>::value;
3435 };
3436 
3437 // This trait checks if BasicJsonType::json_serializer<T>::to_json exists
3438 // Do not evaluate the trait when T is a basic_json type, to avoid template instantiation infinite recursion.
3439 template<typename BasicJsonType, typename T, typename = void>
3440 struct has_to_json : std::false_type {};
3441 
3442 template<typename BasicJsonType, typename T>
3443 struct has_to_json < BasicJsonType, T, enable_if_t < !is_basic_json<T>::value >>
3444 {
3445  using serializer = typename BasicJsonType::template json_serializer<T, void>;
3446 
3447  static constexpr bool value =
3448  is_detected_exact<void, to_json_function, serializer, BasicJsonType&,
3449  T>::value;
3450 };
3451 
3452 
3454 // is_ functions //
3456 
3457 template<typename T, typename = void>
3458 struct is_iterator_traits : std::false_type {};
3459 
3460 template<typename T>
3461 struct is_iterator_traits<iterator_traits<T>>
3462 {
3463  private:
3464  using traits = iterator_traits<T>;
3465 
3466  public:
3467  static constexpr auto value =
3468  is_detected<value_type_t, traits>::value &&
3469  is_detected<difference_type_t, traits>::value &&
3470  is_detected<pointer_t, traits>::value &&
3471  is_detected<iterator_category_t, traits>::value &&
3472  is_detected<reference_t, traits>::value;
3473 };
3474 
3475 // The following implementation of is_complete_type is taken from
3476 // https://blogs.msdn.microsoft.com/vcblog/2015/12/02/partial-support-for-expression-sfinae-in-vs-2015-update-1/
3477 // and is written by Xiang Fan who agreed to using it in this library.
3478 
3479 template<typename T, typename = void>
3480 struct is_complete_type : std::false_type {};
3481 
3482 template<typename T>
3483 struct is_complete_type<T, decltype(void(sizeof(T)))> : std::true_type {};
3484 
3485 template<typename BasicJsonType, typename CompatibleObjectType,
3486  typename = void>
3487 struct is_compatible_object_type_impl : std::false_type {};
3488 
3489 template<typename BasicJsonType, typename CompatibleObjectType>
3490 struct is_compatible_object_type_impl <
3491  BasicJsonType, CompatibleObjectType,
3492  enable_if_t < is_detected<mapped_type_t, CompatibleObjectType>::value&&
3493  is_detected<key_type_t, CompatibleObjectType>::value >>
3494 {
3495  using object_t = typename BasicJsonType::object_t;
3496 
3497  // macOS's is_constructible does not play well with nonesuch...
3498  static constexpr bool value =
3499  std::is_constructible<typename object_t::key_type,
3500  typename CompatibleObjectType::key_type>::value &&
3501  std::is_constructible<typename object_t::mapped_type,
3502  typename CompatibleObjectType::mapped_type>::value;
3503 };
3504 
3505 template<typename BasicJsonType, typename CompatibleObjectType>
3506 struct is_compatible_object_type
3507  : is_compatible_object_type_impl<BasicJsonType, CompatibleObjectType> {};
3508 
3509 template<typename BasicJsonType, typename ConstructibleObjectType,
3510  typename = void>
3511 struct is_constructible_object_type_impl : std::false_type {};
3512 
3513 template<typename BasicJsonType, typename ConstructibleObjectType>
3514 struct is_constructible_object_type_impl <
3515  BasicJsonType, ConstructibleObjectType,
3516  enable_if_t < is_detected<mapped_type_t, ConstructibleObjectType>::value&&
3517  is_detected<key_type_t, ConstructibleObjectType>::value >>
3518 {
3519  using object_t = typename BasicJsonType::object_t;
3520 
3521  static constexpr bool value =
3522  (std::is_default_constructible<ConstructibleObjectType>::value &&
3523  (std::is_move_assignable<ConstructibleObjectType>::value ||
3524  std::is_copy_assignable<ConstructibleObjectType>::value) &&
3525  (std::is_constructible<typename ConstructibleObjectType::key_type,
3526  typename object_t::key_type>::value &&
3527  std::is_same <
3528  typename object_t::mapped_type,
3529  typename ConstructibleObjectType::mapped_type >::value)) ||
3530  (has_from_json<BasicJsonType,
3531  typename ConstructibleObjectType::mapped_type>::value ||
3532  has_non_default_from_json <
3533  BasicJsonType,
3534  typename ConstructibleObjectType::mapped_type >::value);
3535 };
3536 
3537 template<typename BasicJsonType, typename ConstructibleObjectType>
3538 struct is_constructible_object_type
3539  : is_constructible_object_type_impl<BasicJsonType,
3540  ConstructibleObjectType> {};
3541 
3542 template<typename BasicJsonType, typename CompatibleStringType,
3543  typename = void>
3544 struct is_compatible_string_type_impl : std::false_type {};
3545 
3546 template<typename BasicJsonType, typename CompatibleStringType>
3547 struct is_compatible_string_type_impl <
3548  BasicJsonType, CompatibleStringType,
3549  enable_if_t<is_detected_exact<typename BasicJsonType::string_t::value_type,
3550  value_type_t, CompatibleStringType>::value >>
3551 {
3552  static constexpr auto value =
3553  std::is_constructible<typename BasicJsonType::string_t, CompatibleStringType>::value;
3554 };
3555 
3556 template<typename BasicJsonType, typename ConstructibleStringType>
3557 struct is_compatible_string_type
3558  : is_compatible_string_type_impl<BasicJsonType, ConstructibleStringType> {};
3559 
3560 template<typename BasicJsonType, typename ConstructibleStringType,
3561  typename = void>
3562 struct is_constructible_string_type_impl : std::false_type {};
3563 
3564 template<typename BasicJsonType, typename ConstructibleStringType>
3565 struct is_constructible_string_type_impl <
3566  BasicJsonType, ConstructibleStringType,
3567  enable_if_t<is_detected_exact<typename BasicJsonType::string_t::value_type,
3568  value_type_t, ConstructibleStringType>::value >>
3569 {
3570  static constexpr auto value =
3571  std::is_constructible<ConstructibleStringType,
3572  typename BasicJsonType::string_t>::value;
3573 };
3574 
3575 template<typename BasicJsonType, typename ConstructibleStringType>
3576 struct is_constructible_string_type
3577  : is_constructible_string_type_impl<BasicJsonType, ConstructibleStringType> {};
3578 
3579 template<typename BasicJsonType, typename CompatibleArrayType, typename = void>
3580 struct is_compatible_array_type_impl : std::false_type {};
3581 
3582 template<typename BasicJsonType, typename CompatibleArrayType>
3583 struct is_compatible_array_type_impl <
3584  BasicJsonType, CompatibleArrayType,
3585  enable_if_t < is_detected<value_type_t, CompatibleArrayType>::value&&
3586  is_detected<iterator_t, CompatibleArrayType>::value&&
3587 // This is needed because json_reverse_iterator has a ::iterator type...
3588 // Therefore it is detected as a CompatibleArrayType.
3589 // The real fix would be to have an Iterable concept.
3590  !is_iterator_traits <
3591  iterator_traits<CompatibleArrayType >>::value >>
3592 {
3593  static constexpr bool value =
3594  std::is_constructible<BasicJsonType,
3595  typename CompatibleArrayType::value_type>::value;
3596 };
3597 
3598 template<typename BasicJsonType, typename CompatibleArrayType>
3599 struct is_compatible_array_type
3600  : is_compatible_array_type_impl<BasicJsonType, CompatibleArrayType> {};
3601 
3602 template<typename BasicJsonType, typename ConstructibleArrayType, typename = void>
3603 struct is_constructible_array_type_impl : std::false_type {};
3604 
3605 template<typename BasicJsonType, typename ConstructibleArrayType>
3606 struct is_constructible_array_type_impl <
3607  BasicJsonType, ConstructibleArrayType,
3608  enable_if_t<std::is_same<ConstructibleArrayType,
3609  typename BasicJsonType::value_type>::value >>
3610  : std::true_type {};
3611 
3612 template<typename BasicJsonType, typename ConstructibleArrayType>
3613 struct is_constructible_array_type_impl <
3614  BasicJsonType, ConstructibleArrayType,
3615  enable_if_t < !std::is_same<ConstructibleArrayType,
3616  typename BasicJsonType::value_type>::value&&
3617  std::is_default_constructible<ConstructibleArrayType>::value&&
3618 (std::is_move_assignable<ConstructibleArrayType>::value ||
3619  std::is_copy_assignable<ConstructibleArrayType>::value)&&
3620 is_detected<value_type_t, ConstructibleArrayType>::value&&
3621 is_detected<iterator_t, ConstructibleArrayType>::value&&
3622 is_complete_type <
3623 detected_t<value_type_t, ConstructibleArrayType >>::value >>
3624 {
3625  static constexpr bool value =
3626  // This is needed because json_reverse_iterator has a ::iterator type,
3627  // furthermore, std::back_insert_iterator (and other iterators) have a
3628  // base class `iterator`... Therefore it is detected as a
3629  // ConstructibleArrayType. The real fix would be to have an Iterable
3630  // concept.
3631  !is_iterator_traits<iterator_traits<ConstructibleArrayType>>::value &&
3632 
3633  (std::is_same<typename ConstructibleArrayType::value_type,
3634  typename BasicJsonType::array_t::value_type>::value ||
3635  has_from_json<BasicJsonType,
3636  typename ConstructibleArrayType::value_type>::value ||
3637  has_non_default_from_json <
3638  BasicJsonType, typename ConstructibleArrayType::value_type >::value);
3639 };
3640 
3641 template<typename BasicJsonType, typename ConstructibleArrayType>
3642 struct is_constructible_array_type
3643  : is_constructible_array_type_impl<BasicJsonType, ConstructibleArrayType> {};
3644 
3645 template<typename RealIntegerType, typename CompatibleNumberIntegerType,
3646  typename = void>
3647 struct is_compatible_integer_type_impl : std::false_type {};
3648 
3649 template<typename RealIntegerType, typename CompatibleNumberIntegerType>
3650 struct is_compatible_integer_type_impl <
3651  RealIntegerType, CompatibleNumberIntegerType,
3652  enable_if_t < std::is_integral<RealIntegerType>::value&&
3653  std::is_integral<CompatibleNumberIntegerType>::value&&
3654  !std::is_same<bool, CompatibleNumberIntegerType>::value >>
3655 {
3656  // is there an assert somewhere on overflows?
3657  using RealLimits = std::numeric_limits<RealIntegerType>;
3658  using CompatibleLimits = std::numeric_limits<CompatibleNumberIntegerType>;
3659 
3660  static constexpr auto value =
3661  std::is_constructible<RealIntegerType,
3662  CompatibleNumberIntegerType>::value &&
3663  CompatibleLimits::is_integer &&
3664  RealLimits::is_signed == CompatibleLimits::is_signed;
3665 };
3666 
3667 template<typename RealIntegerType, typename CompatibleNumberIntegerType>
3668 struct is_compatible_integer_type
3669  : is_compatible_integer_type_impl<RealIntegerType,
3670  CompatibleNumberIntegerType> {};
3671 
3672 template<typename BasicJsonType, typename CompatibleType, typename = void>
3673 struct is_compatible_type_impl: std::false_type {};
3674 
3675 template<typename BasicJsonType, typename CompatibleType>
3676 struct is_compatible_type_impl <
3677  BasicJsonType, CompatibleType,
3678  enable_if_t<is_complete_type<CompatibleType>::value >>
3679 {
3680  static constexpr bool value =
3681  has_to_json<BasicJsonType, CompatibleType>::value;
3682 };
3683 
3684 template<typename BasicJsonType, typename CompatibleType>
3685 struct is_compatible_type
3686  : is_compatible_type_impl<BasicJsonType, CompatibleType> {};
3687 
3688 // https://en.cppreference.com/w/cpp/types/conjunction
3689 template<class...> struct conjunction : std::true_type { };
3690 template<class B1> struct conjunction<B1> : B1 { };
3691 template<class B1, class... Bn>
3692 struct conjunction<B1, Bn...>
3693 : std::conditional<bool(B1::value), conjunction<Bn...>, B1>::type {};
3694 
3695 template<typename T1, typename T2>
3696 struct is_constructible_tuple : std::false_type {};
3697 
3698 template<typename T1, typename... Args>
3699 struct is_constructible_tuple<T1, std::tuple<Args...>> : conjunction<std::is_constructible<T1, Args>...> {};
3700 } // namespace detail
3701 } // namespace nlohmann
3702 
3703 // #include <nlohmann/detail/value_t.hpp>
3704 
3705 
3706 namespace nlohmann
3707 {
3708 namespace detail
3709 {
3710 template<typename BasicJsonType>
3711 void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
3712 {
3713  if (JSON_HEDLEY_UNLIKELY(!j.is_null()))
3714  {
3715  JSON_THROW(type_error::create(302, "type must be null, but is " + std::string(j.type_name()), j));
3716  }
3717  n = nullptr;
3718 }
3719 
3720 // overloads for basic_json template parameters
3721 template < typename BasicJsonType, typename ArithmeticType,
3722  enable_if_t < std::is_arithmetic<ArithmeticType>::value&&
3723  !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
3724  int > = 0 >
3725 void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
3726 {
3727  switch (static_cast<value_t>(j))
3728  {
3729  case value_t::number_unsigned:
3730  {
3731  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
3732  break;
3733  }
3734  case value_t::number_integer:
3735  {
3736  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
3737  break;
3738  }
3739  case value_t::number_float:
3740  {
3741  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
3742  break;
3743  }
3744 
3745  default:
3746  JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), j));
3747  }
3748 }
3749 
3750 template<typename BasicJsonType>
3751 void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
3752 {
3753  if (JSON_HEDLEY_UNLIKELY(!j.is_boolean()))
3754  {
3755  JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(j.type_name()), j));
3756  }
3757  b = *j.template get_ptr<const typename BasicJsonType::boolean_t*>();
3758 }
3759 
3760 template<typename BasicJsonType>
3761 void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
3762 {
3763  if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
3764  {
3765  JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), j));
3766  }
3767  s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
3768 }
3769 
3770 template <
3771  typename BasicJsonType, typename ConstructibleStringType,
3772  enable_if_t <
3773  is_constructible_string_type<BasicJsonType, ConstructibleStringType>::value&&
3774  !std::is_same<typename BasicJsonType::string_t,
3775  ConstructibleStringType>::value,
3776  int > = 0 >
3777 void from_json(const BasicJsonType& j, ConstructibleStringType& s)
3778 {
3779  if (JSON_HEDLEY_UNLIKELY(!j.is_string()))
3780  {
3781  JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), j));
3782  }
3783 
3784  s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
3785 }
3786 
3787 template<typename BasicJsonType>
3788 void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val)
3789 {
3790  get_arithmetic_value(j, val);
3791 }
3792 
3793 template<typename BasicJsonType>
3794 void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val)
3795 {
3796  get_arithmetic_value(j, val);
3797 }
3798 
3799 template<typename BasicJsonType>
3800 void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val)
3801 {
3802  get_arithmetic_value(j, val);
3803 }
3804 
3805 template<typename BasicJsonType, typename EnumType,
3806  enable_if_t<std::is_enum<EnumType>::value, int> = 0>
3807 void from_json(const BasicJsonType& j, EnumType& e)
3808 {
3809  typename std::underlying_type<EnumType>::type val;
3810  get_arithmetic_value(j, val);
3811  e = static_cast<EnumType>(val);
3812 }
3813 
3814 // forward_list doesn't have an insert method
3815 template<typename BasicJsonType, typename T, typename Allocator,
3816  enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
3817 void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
3818 {
3819  if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
3820  {
3821  JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
3822  }
3823  l.clear();
3824  std::transform(j.rbegin(), j.rend(),
3825  std::front_inserter(l), [](const BasicJsonType & i)
3826  {
3827  return i.template get<T>();
3828  });
3829 }
3830 
3831 // valarray doesn't have an insert method
3832 template<typename BasicJsonType, typename T,
3833  enable_if_t<is_getable<BasicJsonType, T>::value, int> = 0>
3834 void from_json(const BasicJsonType& j, std::valarray<T>& l)
3835 {
3836  if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
3837  {
3838  JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
3839  }
3840  l.resize(j.size());
3841  std::transform(j.begin(), j.end(), std::begin(l),
3842  [](const BasicJsonType & elem)
3843  {
3844  return elem.template get<T>();
3845  });
3846 }
3847 
3848 template<typename BasicJsonType, typename T, std::size_t N>
3849 auto from_json(const BasicJsonType& j, T (&arr)[N])
3850 -> decltype(j.template get<T>(), void())
3851 {
3852  for (std::size_t i = 0; i < N; ++i)
3853  {
3854  arr[i] = j.at(i).template get<T>();
3855  }
3856 }
3857 
3858 template<typename BasicJsonType>
3859 void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType::array_t& arr, priority_tag<3> /*unused*/)
3860 {
3861  arr = *j.template get_ptr<const typename BasicJsonType::array_t*>();
3862 }
3863 
3864 template<typename BasicJsonType, typename T, std::size_t N>
3865 auto from_json_array_impl(const BasicJsonType& j, std::array<T, N>& arr,
3866  priority_tag<2> /*unused*/)
3867 -> decltype(j.template get<T>(), void())
3868 {
3869  for (std::size_t i = 0; i < N; ++i)
3870  {
3871  arr[i] = j.at(i).template get<T>();
3872  }
3873 }
3874 
3875 template<typename BasicJsonType, typename ConstructibleArrayType>
3876 auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<1> /*unused*/)
3877 -> decltype(
3878  arr.reserve(std::declval<typename ConstructibleArrayType::size_type>()),
3879  j.template get<typename ConstructibleArrayType::value_type>(),
3880  void())
3881 {
3882  using std::end;
3883 
3884  ConstructibleArrayType ret;
3885  ret.reserve(j.size());
3886  std::transform(j.begin(), j.end(),
3887  std::inserter(ret, end(ret)), [](const BasicJsonType & i)
3888  {
3889  // get<BasicJsonType>() returns *this, this won't call a from_json
3890  // method when value_type is BasicJsonType
3891  return i.template get<typename ConstructibleArrayType::value_type>();
3892  });
3893  arr = std::move(ret);
3894 }
3895 
3896 template<typename BasicJsonType, typename ConstructibleArrayType>
3897 void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr,
3898  priority_tag<0> /*unused*/)
3899 {
3900  using std::end;
3901 
3902  ConstructibleArrayType ret;
3903  std::transform(
3904  j.begin(), j.end(), std::inserter(ret, end(ret)),
3905  [](const BasicJsonType & i)
3906  {
3907  // get<BasicJsonType>() returns *this, this won't call a from_json
3908  // method when value_type is BasicJsonType
3909  return i.template get<typename ConstructibleArrayType::value_type>();
3910  });
3911  arr = std::move(ret);
3912 }
3913 
3914 template < typename BasicJsonType, typename ConstructibleArrayType,
3915  enable_if_t <
3916  is_constructible_array_type<BasicJsonType, ConstructibleArrayType>::value&&
3917  !is_constructible_object_type<BasicJsonType, ConstructibleArrayType>::value&&
3918  !is_constructible_string_type<BasicJsonType, ConstructibleArrayType>::value&&
3919  !std::is_same<ConstructibleArrayType, typename BasicJsonType::binary_t>::value&&
3920  !is_basic_json<ConstructibleArrayType>::value,
3921  int > = 0 >
3922 auto from_json(const BasicJsonType& j, ConstructibleArrayType& arr)
3923 -> decltype(from_json_array_impl(j, arr, priority_tag<3> {}),
3924 j.template get<typename ConstructibleArrayType::value_type>(),
3925 void())
3926 {
3927  if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
3928  {
3929  JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
3930  }
3931 
3932  from_json_array_impl(j, arr, priority_tag<3> {});
3933 }
3934 
3935 template<typename BasicJsonType>
3936 void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin)
3937 {
3938  if (JSON_HEDLEY_UNLIKELY(!j.is_binary()))
3939  {
3940  JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(j.type_name()), j));
3941  }
3942 
3943  bin = *j.template get_ptr<const typename BasicJsonType::binary_t*>();
3944 }
3945 
3946 template<typename BasicJsonType, typename ConstructibleObjectType,
3947  enable_if_t<is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value, int> = 0>
3948 void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
3949 {
3950  if (JSON_HEDLEY_UNLIKELY(!j.is_object()))
3951  {
3952  JSON_THROW(type_error::create(302, "type must be object, but is " + std::string(j.type_name()), j));
3953  }
3954 
3955  ConstructibleObjectType ret;
3956  auto inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
3957  using value_type = typename ConstructibleObjectType::value_type;
3958  std::transform(
3959  inner_object->begin(), inner_object->end(),
3960  std::inserter(ret, ret.begin()),
3961  [](typename BasicJsonType::object_t::value_type const & p)
3962  {
3963  return value_type(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
3964  });
3965  obj = std::move(ret);
3966 }
3967 
3968 // overload for arithmetic types, not chosen for basic_json template arguments
3969 // (BooleanType, etc..); note: Is it really necessary to provide explicit
3970 // overloads for boolean_t etc. in case of a custom BooleanType which is not
3971 // an arithmetic type?
3972 template < typename BasicJsonType, typename ArithmeticType,
3973  enable_if_t <
3974  std::is_arithmetic<ArithmeticType>::value&&
3975  !std::is_same<ArithmeticType, typename BasicJsonType::number_unsigned_t>::value&&
3976  !std::is_same<ArithmeticType, typename BasicJsonType::number_integer_t>::value&&
3977  !std::is_same<ArithmeticType, typename BasicJsonType::number_float_t>::value&&
3978  !std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
3979  int > = 0 >
3980 void from_json(const BasicJsonType& j, ArithmeticType& val)
3981 {
3982  switch (static_cast<value_t>(j))
3983  {
3984  case value_t::number_unsigned:
3985  {
3986  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
3987  break;
3988  }
3989  case value_t::number_integer:
3990  {
3991  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
3992  break;
3993  }
3994  case value_t::number_float:
3995  {
3996  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
3997  break;
3998  }
3999  case value_t::boolean:
4000  {
4001  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
4002  break;
4003  }
4004 
4005  default:
4006  JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), j));
4007  }
4008 }
4009 
4010 template<typename BasicJsonType, typename A1, typename A2>
4011 void from_json(const BasicJsonType& j, std::pair<A1, A2>& p)
4012 {
4013  p = {j.at(0).template get<A1>(), j.at(1).template get<A2>()};
4014 }
4015 
4016 template<typename BasicJsonType, typename Tuple, std::size_t... Idx>
4017 void from_json_tuple_impl(const BasicJsonType& j, Tuple& t, index_sequence<Idx...> /*unused*/)
4018 {
4019  t = std::make_tuple(j.at(Idx).template get<typename std::tuple_element<Idx, Tuple>::type>()...);
4020 }
4021 
4022 template<typename BasicJsonType, typename... Args>
4023 void from_json(const BasicJsonType& j, std::tuple<Args...>& t)
4024 {
4025  from_json_tuple_impl(j, t, index_sequence_for<Args...> {});
4026 }
4027 
4028 template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator,
4029  typename = enable_if_t < !std::is_constructible <
4030  typename BasicJsonType::string_t, Key >::value >>
4031 void from_json(const BasicJsonType& j, std::map<Key, Value, Compare, Allocator>& m)
4032 {
4033  if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
4034  {
4035  JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
4036  }
4037  m.clear();
4038  for (const auto& p : j)
4039  {
4040  if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
4041  {
4042  JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()), j));
4043  }
4044  m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
4045  }
4046 }
4047 
4048 template < typename BasicJsonType, typename Key, typename Value, typename Hash, typename KeyEqual, typename Allocator,
4049  typename = enable_if_t < !std::is_constructible <
4050  typename BasicJsonType::string_t, Key >::value >>
4051 void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>& m)
4052 {
4053  if (JSON_HEDLEY_UNLIKELY(!j.is_array()))
4054  {
4055  JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j));
4056  }
4057  m.clear();
4058  for (const auto& p : j)
4059  {
4060  if (JSON_HEDLEY_UNLIKELY(!p.is_array()))
4061  {
4062  JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()), j));
4063  }
4064  m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
4065  }
4066 }
4067 
4068 struct from_json_fn
4069 {
4070  template<typename BasicJsonType, typename T>
4071  auto operator()(const BasicJsonType& j, T& val) const
4072  noexcept(noexcept(from_json(j, val)))
4073  -> decltype(from_json(j, val), void())
4074  {
4075  return from_json(j, val);
4076  }
4077 };
4078 } // namespace detail
4079 
4083 namespace
4084 {
4085 constexpr const auto& from_json = detail::static_const<detail::from_json_fn>::value;
4086 } // namespace
4087 } // namespace nlohmann
4088 
4089 // #include <nlohmann/detail/conversions/to_json.hpp>
4090 
4091 
4092 #include <algorithm> // copy
4093 #include <iterator> // begin, end
4094 #include <string> // string
4095 #include <tuple> // tuple, get
4096 #include <type_traits> // is_same, is_constructible, is_floating_point, is_enum, underlying_type
4097 #include <utility> // move, forward, declval, pair
4098 #include <valarray> // valarray
4099 #include <vector> // vector
4100 
4101 // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
4102 
4103 
4104 #include <cstddef> // size_t
4105 #include <iterator> // input_iterator_tag
4106 #include <string> // string, to_string
4107 #include <tuple> // tuple_size, get, tuple_element
4108 
4109 // #include <nlohmann/detail/meta/type_traits.hpp>
4110 
4111 // #include <nlohmann/detail/value_t.hpp>
4112 
4113 
4114 namespace nlohmann
4115 {
4116 namespace detail
4117 {
4118 template<typename string_type>
4119 void int_to_string( string_type& target, std::size_t value )
4120 {
4121  // For ADL
4122  using std::to_string;
4123  target = to_string(value);
4124 }
4125 template<typename IteratorType> class iteration_proxy_value
4126 {
4127  public:
4128  using difference_type = std::ptrdiff_t;
4129  using value_type = iteration_proxy_value;
4130  using pointer = value_type * ;
4131  using reference = value_type & ;
4132  using iterator_category = std::input_iterator_tag;
4133  using string_type = typename std::remove_cv< typename std::remove_reference<decltype( std::declval<IteratorType>().key() ) >::type >::type;
4134 
4135  private:
4137  IteratorType anchor;
4139  std::size_t array_index = 0;
4141  mutable std::size_t array_index_last = 0;
4143  mutable string_type array_index_str = "0";
4145  const string_type empty_str{};
4146 
4147  public:
4148  explicit iteration_proxy_value(IteratorType it) noexcept : anchor(it) {}
4149 
4151  iteration_proxy_value& operator*()
4152  {
4153  return *this;
4154  }
4155 
4157  iteration_proxy_value& operator++()
4158  {
4159  ++anchor;
4160  ++array_index;
4161 
4162  return *this;
4163  }
4164 
4166  bool operator==(const iteration_proxy_value& o) const
4167  {
4168  return anchor == o.anchor;
4169  }
4170 
4172  bool operator!=(const iteration_proxy_value& o) const
4173  {
4174  return anchor != o.anchor;
4175  }
4176 
4178  const string_type& key() const
4179  {
4180  JSON_ASSERT(anchor.m_object != nullptr);
4181 
4182  switch (anchor.m_object->type())
4183  {
4184  // use integer array index as key
4185  case value_t::array:
4186  {
4187  if (array_index != array_index_last)
4188  {
4189  int_to_string( array_index_str, array_index );
4190  array_index_last = array_index;
4191  }
4192  return array_index_str;
4193  }
4194 
4195  // use key from the object
4196  case value_t::object:
4197  return anchor.key();
4198 
4199  // use an empty key for all primitive types
4200  default:
4201  return empty_str;
4202  }
4203  }
4204 
4206  typename IteratorType::reference value() const
4207  {
4208  return anchor.value();
4209  }
4210 };
4211 
4213 template<typename IteratorType> class iteration_proxy
4214 {
4215  private:
4217  typename IteratorType::reference container;
4218 
4219  public:
4221  explicit iteration_proxy(typename IteratorType::reference cont) noexcept
4222  : container(cont) {}
4223 
4225  iteration_proxy_value<IteratorType> begin() noexcept
4226  {
4227  return iteration_proxy_value<IteratorType>(container.begin());
4228  }
4229 
4231  iteration_proxy_value<IteratorType> end() noexcept
4232  {
4233  return iteration_proxy_value<IteratorType>(container.end());
4234  }
4235 };
4236 // Structured Bindings Support
4237 // For further reference see https://blog.tartanllama.xyz/structured-bindings/
4238 // And see https://github.com/nlohmann/json/pull/1391
4239 template<std::size_t N, typename IteratorType, enable_if_t<N == 0, int> = 0>
4240 auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.key())
4241 {
4242  return i.key();
4243 }
4244 // Structured Bindings Support
4245 // For further reference see https://blog.tartanllama.xyz/structured-bindings/
4246 // And see https://github.com/nlohmann/json/pull/1391
4247 template<std::size_t N, typename IteratorType, enable_if_t<N == 1, int> = 0>
4248 auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.value())
4249 {
4250  return i.value();
4251 }
4252 } // namespace detail
4253 } // namespace nlohmann
4254 
4255 // The Addition to the STD Namespace is required to add
4256 // Structured Bindings Support to the iteration_proxy_value class
4257 // For further reference see https://blog.tartanllama.xyz/structured-bindings/
4258 // And see https://github.com/nlohmann/json/pull/1391
4259 namespace std
4260 {
4261 #if defined(__clang__)
4262  // Fix: https://github.com/nlohmann/json/issues/1401
4263  #pragma clang diagnostic push
4264  #pragma clang diagnostic ignored "-Wmismatched-tags"
4265 #endif
4266 template<typename IteratorType>
4267 class tuple_size<::nlohmann::detail::iteration_proxy_value<IteratorType>>
4268  : public std::integral_constant<std::size_t, 2> {};
4269 
4270 template<std::size_t N, typename IteratorType>
4271 class tuple_element<N, ::nlohmann::detail::iteration_proxy_value<IteratorType >>
4272 {
4273  public:
4274  using type = decltype(
4275  get<N>(std::declval <
4276  ::nlohmann::detail::iteration_proxy_value<IteratorType >> ()));
4277 };
4278 #if defined(__clang__)
4279  #pragma clang diagnostic pop
4280 #endif
4281 } // namespace std
4282 
4283 // #include <nlohmann/detail/meta/cpp_future.hpp>
4284 
4285 // #include <nlohmann/detail/meta/type_traits.hpp>
4286 
4287 // #include <nlohmann/detail/value_t.hpp>
4288 
4289 
4290 namespace nlohmann
4291 {
4292 namespace detail
4293 {
4295 // constructors //
4297 
4298 template<value_t> struct external_constructor;
4299 
4300 template<>
4301 struct external_constructor<value_t::boolean>
4302 {
4303  template<typename BasicJsonType>
4304  static void construct(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept
4305  {
4306  j.m_type = value_t::boolean;
4307  j.m_value = b;
4308  j.assert_invariant();
4309  }
4310 };
4311 
4312 template<>
4313 struct external_constructor<value_t::string>
4314 {
4315  template<typename BasicJsonType>
4316  static void construct(BasicJsonType& j, const typename BasicJsonType::string_t& s)
4317  {
4318  j.m_type = value_t::string;
4319  j.m_value = s;
4320  j.assert_invariant();
4321  }
4322 
4323  template<typename BasicJsonType>
4324  static void construct(BasicJsonType& j, typename BasicJsonType::string_t&& s)
4325  {
4326  j.m_type = value_t::string;
4327  j.m_value = std::move(s);
4328  j.assert_invariant();
4329  }
4330 
4331  template < typename BasicJsonType, typename CompatibleStringType,
4332  enable_if_t < !std::is_same<CompatibleStringType, typename BasicJsonType::string_t>::value,
4333  int > = 0 >
4334  static void construct(BasicJsonType& j, const CompatibleStringType& str)
4335  {
4336  j.m_type = value_t::string;
4337  j.m_value.string = j.template create<typename BasicJsonType::string_t>(str);
4338  j.assert_invariant();
4339  }
4340 };
4341 
4342 template<>
4343 struct external_constructor<value_t::binary>
4344 {
4345  template<typename BasicJsonType>
4346  static void construct(BasicJsonType& j, const typename BasicJsonType::binary_t& b)
4347  {
4348  j.m_type = value_t::binary;
4349  typename BasicJsonType::binary_t value{b};
4350  j.m_value = value;
4351  j.assert_invariant();
4352  }
4353 
4354  template<typename BasicJsonType>
4355  static void construct(BasicJsonType& j, typename BasicJsonType::binary_t&& b)
4356  {
4357  j.m_type = value_t::binary;
4358  typename BasicJsonType::binary_t value{std::move(b)};
4359  j.m_value = value;
4360  j.assert_invariant();
4361  }
4362 };
4363 
4364 template<>
4365 struct external_constructor<value_t::number_float>
4366 {
4367  template<typename BasicJsonType>
4368  static void construct(BasicJsonType& j, typename BasicJsonType::number_float_t val) noexcept
4369  {
4370  j.m_type = value_t::number_float;
4371  j.m_value = val;
4372  j.assert_invariant();
4373  }
4374 };
4375 
4376 template<>
4377 struct external_constructor<value_t::number_unsigned>
4378 {
4379  template<typename BasicJsonType>
4380  static void construct(BasicJsonType& j, typename BasicJsonType::number_unsigned_t val) noexcept
4381  {
4382  j.m_type = value_t::number_unsigned;
4383  j.m_value = val;
4384  j.assert_invariant();
4385  }
4386 };
4387 
4388 template<>
4389 struct external_constructor<value_t::number_integer>
4390 {
4391  template<typename BasicJsonType>
4392  static void construct(BasicJsonType& j, typename BasicJsonType::number_integer_t val) noexcept
4393  {
4394  j.m_type = value_t::number_integer;
4395  j.m_value = val;
4396  j.assert_invariant();
4397  }
4398 };
4399 
4400 template<>
4401 struct external_constructor<value_t::array>
4402 {
4403  template<typename BasicJsonType>
4404  static void construct(BasicJsonType& j, const typename BasicJsonType::array_t& arr)
4405  {
4406  j.m_type = value_t::array;
4407  j.m_value = arr;
4408  j.set_parents();
4409  j.assert_invariant();
4410  }
4411 
4412  template<typename BasicJsonType>
4413  static void construct(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
4414  {
4415  j.m_type = value_t::array;
4416  j.m_value = std::move(arr);
4417  j.set_parents();
4418  j.assert_invariant();
4419  }
4420 
4421  template < typename BasicJsonType, typename CompatibleArrayType,
4422  enable_if_t < !std::is_same<CompatibleArrayType, typename BasicJsonType::array_t>::value,
4423  int > = 0 >
4424  static void construct(BasicJsonType& j, const CompatibleArrayType& arr)
4425  {
4426  using std::begin;
4427  using std::end;
4428  j.m_type = value_t::array;
4429  j.m_value.array = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
4430  j.set_parents();
4431  j.assert_invariant();
4432  }
4433 
4434  template<typename BasicJsonType>
4435  static void construct(BasicJsonType& j, const std::vector<bool>& arr)
4436  {
4437  j.m_type = value_t::array;
4438  j.m_value = value_t::array;
4439  j.m_value.array->reserve(arr.size());
4440  for (const bool x : arr)
4441  {
4442  j.m_value.array->push_back(x);
4443  j.set_parent(j.m_value.array->back());
4444  }
4445  j.assert_invariant();
4446  }
4447 
4448  template<typename BasicJsonType, typename T,
4449  enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
4450  static void construct(BasicJsonType& j, const std::valarray<T>& arr)
4451  {
4452  j.m_type = value_t::array;
4453  j.m_value = value_t::array;
4454  j.m_value.array->resize(arr.size());
4455  if (arr.size() > 0)
4456  {
4457  std::copy(std::begin(arr), std::end(arr), j.m_value.array->begin());
4458  }
4459  j.set_parents();
4460  j.assert_invariant();
4461  }
4462 };
4463 
4464 template<>
4465 struct external_constructor<value_t::object>
4466 {
4467  template<typename BasicJsonType>
4468  static void construct(BasicJsonType& j, const typename BasicJsonType::object_t& obj)
4469  {
4470  j.m_type = value_t::object;
4471  j.m_value = obj;
4472  j.set_parents();
4473  j.assert_invariant();
4474  }
4475 
4476  template<typename BasicJsonType>
4477  static void construct(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
4478  {
4479  j.m_type = value_t::object;
4480  j.m_value = std::move(obj);
4481  j.set_parents();
4482  j.assert_invariant();
4483  }
4484 
4485  template < typename BasicJsonType, typename CompatibleObjectType,
4486  enable_if_t < !std::is_same<CompatibleObjectType, typename BasicJsonType::object_t>::value, int > = 0 >
4487  static void construct(BasicJsonType& j, const CompatibleObjectType& obj)
4488  {
4489  using std::begin;
4490  using std::end;
4491 
4492  j.m_type = value_t::object;
4493  j.m_value.object = j.template create<typename BasicJsonType::object_t>(begin(obj), end(obj));
4494  j.set_parents();
4495  j.assert_invariant();
4496  }
4497 };
4498 
4500 // to_json //
4502 
4503 template<typename BasicJsonType, typename T,
4504  enable_if_t<std::is_same<T, typename BasicJsonType::boolean_t>::value, int> = 0>
4505 void to_json(BasicJsonType& j, T b) noexcept
4506 {
4507  external_constructor<value_t::boolean>::construct(j, b);
4508 }
4509 
4510 template<typename BasicJsonType, typename CompatibleString,
4511  enable_if_t<std::is_constructible<typename BasicJsonType::string_t, CompatibleString>::value, int> = 0>
4512 void to_json(BasicJsonType& j, const CompatibleString& s)
4513 {
4514  external_constructor<value_t::string>::construct(j, s);
4515 }
4516 
4517 template<typename BasicJsonType>
4518 void to_json(BasicJsonType& j, typename BasicJsonType::string_t&& s)
4519 {
4520  external_constructor<value_t::string>::construct(j, std::move(s));
4521 }
4522 
4523 template<typename BasicJsonType, typename FloatType,
4524  enable_if_t<std::is_floating_point<FloatType>::value, int> = 0>
4525 void to_json(BasicJsonType& j, FloatType val) noexcept
4526 {
4527  external_constructor<value_t::number_float>::construct(j, static_cast<typename BasicJsonType::number_float_t>(val));
4528 }
4529 
4530 template<typename BasicJsonType, typename CompatibleNumberUnsignedType,
4531  enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_unsigned_t, CompatibleNumberUnsignedType>::value, int> = 0>
4532 void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept
4533 {
4534  external_constructor<value_t::number_unsigned>::construct(j, static_cast<typename BasicJsonType::number_unsigned_t>(val));
4535 }
4536 
4537 template<typename BasicJsonType, typename CompatibleNumberIntegerType,
4538  enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_integer_t, CompatibleNumberIntegerType>::value, int> = 0>
4539 void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept
4540 {
4541  external_constructor<value_t::number_integer>::construct(j, static_cast<typename BasicJsonType::number_integer_t>(val));
4542 }
4543 
4544 template<typename BasicJsonType, typename EnumType,
4545  enable_if_t<std::is_enum<EnumType>::value, int> = 0>
4546 void to_json(BasicJsonType& j, EnumType e) noexcept
4547 {
4548  using underlying_type = typename std::underlying_type<EnumType>::type;
4549  external_constructor<value_t::number_integer>::construct(j, static_cast<underlying_type>(e));
4550 }
4551 
4552 template<typename BasicJsonType>
4553 void to_json(BasicJsonType& j, const std::vector<bool>& e)
4554 {
4555  external_constructor<value_t::array>::construct(j, e);
4556 }
4557 
4558 template < typename BasicJsonType, typename CompatibleArrayType,
4559  enable_if_t < is_compatible_array_type<BasicJsonType,
4560  CompatibleArrayType>::value&&
4561  !is_compatible_object_type<BasicJsonType, CompatibleArrayType>::value&&
4562  !is_compatible_string_type<BasicJsonType, CompatibleArrayType>::value&&
4563  !std::is_same<typename BasicJsonType::binary_t, CompatibleArrayType>::value&&
4564  !is_basic_json<CompatibleArrayType>::value,
4565  int > = 0 >
4566 void to_json(BasicJsonType& j, const CompatibleArrayType& arr)
4567 {
4568  external_constructor<value_t::array>::construct(j, arr);
4569 }
4570 
4571 template<typename BasicJsonType>
4572 void to_json(BasicJsonType& j, const typename BasicJsonType::binary_t& bin)
4573 {
4574  external_constructor<value_t::binary>::construct(j, bin);
4575 }
4576 
4577 template<typename BasicJsonType, typename T,
4578  enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
4579 void to_json(BasicJsonType& j, const std::valarray<T>& arr)
4580 {
4581  external_constructor<value_t::array>::construct(j, std::move(arr));
4582 }
4583 
4584 template<typename BasicJsonType>
4585 void to_json(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
4586 {
4587  external_constructor<value_t::array>::construct(j, std::move(arr));
4588 }
4589 
4590 template < typename BasicJsonType, typename CompatibleObjectType,
4591  enable_if_t < is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value&& !is_basic_json<CompatibleObjectType>::value, int > = 0 >
4592 void to_json(BasicJsonType& j, const CompatibleObjectType& obj)
4593 {
4594  external_constructor<value_t::object>::construct(j, obj);
4595 }
4596 
4597 template<typename BasicJsonType>
4598 void to_json(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
4599 {
4600  external_constructor<value_t::object>::construct(j, std::move(obj));
4601 }
4602 
4603 template <
4604  typename BasicJsonType, typename T, std::size_t N,
4605  enable_if_t < !std::is_constructible<typename BasicJsonType::string_t,
4606  const T(&)[N]>::value,
4607  int > = 0 >
4608 void to_json(BasicJsonType& j, const T(&arr)[N])
4609 {
4610  external_constructor<value_t::array>::construct(j, arr);
4611 }
4612 
4613 template < typename BasicJsonType, typename T1, typename T2, enable_if_t < std::is_constructible<BasicJsonType, T1>::value&& std::is_constructible<BasicJsonType, T2>::value, int > = 0 >
4614 void to_json(BasicJsonType& j, const std::pair<T1, T2>& p)
4615 {
4616  j = { p.first, p.second };
4617 }
4618 
4619 // for https://github.com/nlohmann/json/pull/1134
4620 template<typename BasicJsonType, typename T,
4621  enable_if_t<std::is_same<T, iteration_proxy_value<typename BasicJsonType::iterator>>::value, int> = 0>
4622 void to_json(BasicJsonType& j, const T& b)
4623 {
4624  j = { {b.key(), b.value()} };
4625 }
4626 
4627 template<typename BasicJsonType, typename Tuple, std::size_t... Idx>
4628 void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence<Idx...> /*unused*/)
4629 {
4630  j = { std::get<Idx>(t)... };
4631 }
4632 
4633 template<typename BasicJsonType, typename T, enable_if_t<is_constructible_tuple<BasicJsonType, T>::value, int > = 0>
4634 void to_json(BasicJsonType& j, const T& t)
4635 {
4636  to_json_tuple_impl(j, t, make_index_sequence<std::tuple_size<T>::value> {});
4637 }
4638 
4639 struct to_json_fn
4640 {
4641  template<typename BasicJsonType, typename T>
4642  auto operator()(BasicJsonType& j, T&& val) const noexcept(noexcept(to_json(j, std::forward<T>(val))))
4643  -> decltype(to_json(j, std::forward<T>(val)), void())
4644  {
4645  return to_json(j, std::forward<T>(val));
4646  }
4647 };
4648 } // namespace detail
4649 
4651 namespace
4652 {
4653 constexpr const auto& to_json = detail::static_const<detail::to_json_fn>::value;
4654 } // namespace
4655 } // namespace nlohmann
4656 
4657 
4658 namespace nlohmann
4659 {
4660 
4661 template<typename, typename>
4663 {
4673  template<typename BasicJsonType, typename ValueType>
4674  static auto from_json(BasicJsonType&& j, ValueType& val) noexcept(
4675  noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), val)))
4676  -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), val), void())
4677  {
4678  ::nlohmann::from_json(std::forward<BasicJsonType>(j), val);
4679  }
4680 
4690  template<typename BasicJsonType, typename ValueType>
4691  static auto to_json(BasicJsonType& j, ValueType&& val) noexcept(
4692  noexcept(::nlohmann::to_json(j, std::forward<ValueType>(val))))
4693  -> decltype(::nlohmann::to_json(j, std::forward<ValueType>(val)), void())
4694  {
4695  ::nlohmann::to_json(j, std::forward<ValueType>(val));
4696  }
4697 };
4698 
4699 } // namespace nlohmann
4700 
4701 // #include <nlohmann/byte_container_with_subtype.hpp>
4702 
4703 
4704 #include <cstdint> // uint8_t
4705 #include <tuple> // tie
4706 #include <utility> // move
4707 
4708 namespace nlohmann
4709 {
4710 
4724 template<typename BinaryType>
4725 class byte_container_with_subtype : public BinaryType
4726 {
4727  public:
4729  using container_type = BinaryType;
4730 
4732  : container_type()
4733  {}
4734 
4736  : container_type(b)
4737  {}
4738 
4739  byte_container_with_subtype(container_type&& b) noexcept(noexcept(container_type(std::move(b))))
4740  : container_type(std::move(b))
4741  {}
4742 
4743  byte_container_with_subtype(const container_type& b, std::uint8_t subtype_) noexcept(noexcept(container_type(b)))
4744  : container_type(b)
4745  , m_subtype(subtype_)
4746  , m_has_subtype(true)
4747  {}
4748 
4749  byte_container_with_subtype(container_type&& b, std::uint8_t subtype_) noexcept(noexcept(container_type(std::move(b))))
4750  : container_type(std::move(b))
4751  , m_subtype(subtype_)
4752  , m_has_subtype(true)
4753  {}
4754 
4756  {
4757  return std::tie(static_cast<const BinaryType&>(*this), m_subtype, m_has_subtype) ==
4758  std::tie(static_cast<const BinaryType&>(rhs), rhs.m_subtype, rhs.m_has_subtype);
4759  }
4760 
4762  {
4763  return !(rhs == *this);
4764  }
4765 
4784  void set_subtype(std::uint8_t subtype_) noexcept
4785  {
4786  m_subtype = subtype_;
4787  m_has_subtype = true;
4788  }
4789 
4811  constexpr std::uint8_t subtype() const noexcept
4812  {
4813  return m_subtype;
4814  }
4815 
4832  constexpr bool has_subtype() const noexcept
4833  {
4834  return m_has_subtype;
4835  }
4836 
4856  void clear_subtype() noexcept
4857  {
4858  m_subtype = 0;
4859  m_has_subtype = false;
4860  }
4861 
4862  private:
4863  std::uint8_t m_subtype = 0;
4864  bool m_has_subtype = false;
4865 };
4866 
4867 } // namespace nlohmann
4868 
4869 // #include <nlohmann/detail/conversions/from_json.hpp>
4870 
4871 // #include <nlohmann/detail/conversions/to_json.hpp>
4872 
4873 // #include <nlohmann/detail/exceptions.hpp>
4874 
4875 // #include <nlohmann/detail/hash.hpp>
4876 
4877 
4878 #include <cstddef> // size_t, uint8_t
4879 #include <functional> // hash
4880 
4881 // #include <nlohmann/detail/macro_scope.hpp>
4882 
4883 
4884 namespace nlohmann
4885 {
4886 namespace detail
4887 {
4888 
4889 // boost::hash_combine
4890 inline std::size_t combine(std::size_t seed, std::size_t h) noexcept
4891 {
4892  seed ^= h + 0x9e3779b9 + (seed << 6U) + (seed >> 2U);
4893  return seed;
4894 }
4895 
4907 template<typename BasicJsonType>
4908 std::size_t hash(const BasicJsonType& j)
4909 {
4910  using string_t = typename BasicJsonType::string_t;
4911  using number_integer_t = typename BasicJsonType::number_integer_t;
4912  using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
4913  using number_float_t = typename BasicJsonType::number_float_t;
4914 
4915  const auto type = static_cast<std::size_t>(j.type());
4916  switch (j.type())
4917  {
4918  case BasicJsonType::value_t::null:
4919  case BasicJsonType::value_t::discarded:
4920  {
4921  return combine(type, 0);
4922  }
4923 
4924  case BasicJsonType::value_t::object:
4925  {
4926  auto seed = combine(type, j.size());
4927  for (const auto& element : j.items())
4928  {
4929  const auto h = std::hash<string_t> {}(element.key());
4930  seed = combine(seed, h);
4931  seed = combine(seed, hash(element.value()));
4932  }
4933  return seed;
4934  }
4935 
4936  case BasicJsonType::value_t::array:
4937  {
4938  auto seed = combine(type, j.size());
4939  for (const auto& element : j)
4940  {
4941  seed = combine(seed, hash(element));
4942  }
4943  return seed;
4944  }
4945 
4946  case BasicJsonType::value_t::string:
4947  {
4948  const auto h = std::hash<string_t> {}(j.template get_ref<const string_t&>());
4949  return combine(type, h);
4950  }
4951 
4952  case BasicJsonType::value_t::boolean:
4953  {
4954  const auto h = std::hash<bool> {}(j.template get<bool>());
4955  return combine(type, h);
4956  }
4957 
4958  case BasicJsonType::value_t::number_integer:
4959  {
4960  const auto h = std::hash<number_integer_t> {}(j.template get<number_integer_t>());
4961  return combine(type, h);
4962  }
4963 
4964  case BasicJsonType::value_t::number_unsigned:
4965  {
4966  const auto h = std::hash<number_unsigned_t> {}(j.template get<number_unsigned_t>());
4967  return combine(type, h);
4968  }
4969 
4970  case BasicJsonType::value_t::number_float:
4971  {
4972  const auto h = std::hash<number_float_t> {}(j.template get<number_float_t>());
4973  return combine(type, h);
4974  }
4975 
4976  case BasicJsonType::value_t::binary:
4977  {
4978  auto seed = combine(type, j.get_binary().size());
4979  const auto h = std::hash<bool> {}(j.get_binary().has_subtype());
4980  seed = combine(seed, h);
4981  seed = combine(seed, j.get_binary().subtype());
4982  for (const auto byte : j.get_binary())
4983  {
4984  seed = combine(seed, std::hash<std::uint8_t> {}(byte));
4985  }
4986  return seed;
4987  }
4988 
4989  default: // LCOV_EXCL_LINE
4990  JSON_ASSERT(false); // LCOV_EXCL_LINE
4991  return 0; // LCOV_EXCL_LINE
4992  }
4993 }
4994 
4995 } // namespace detail
4996 } // namespace nlohmann
4997 
4998 // #include <nlohmann/detail/input/binary_reader.hpp>
4999 
5000 
5001 #include <algorithm> // generate_n
5002 #include <array> // array
5003 #include <cmath> // ldexp
5004 #include <cstddef> // size_t
5005 #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
5006 #include <cstdio> // snprintf
5007 #include <cstring> // memcpy
5008 #include <iterator> // back_inserter
5009 #include <limits> // numeric_limits
5010 #include <string> // char_traits, string
5011 #include <utility> // make_pair, move
5012 #include <vector> // vector
5013 
5014 // #include <nlohmann/detail/exceptions.hpp>
5015 
5016 // #include <nlohmann/detail/input/input_adapters.hpp>
5017 
5018 
5019 #include <array> // array
5020 #include <cstddef> // size_t
5021 #include <cstdio> //FILE *
5022 #include <cstring> // strlen
5023 #include <istream> // istream
5024 #include <iterator> // begin, end, iterator_traits, random_access_iterator_tag, distance, next
5025 #include <memory> // shared_ptr, make_shared, addressof
5026 #include <numeric> // accumulate
5027 #include <string> // string, char_traits
5028 #include <type_traits> // enable_if, is_base_of, is_pointer, is_integral, remove_pointer
5029 #include <utility> // pair, declval
5030 
5031 // #include <nlohmann/detail/iterators/iterator_traits.hpp>
5032 
5033 // #include <nlohmann/detail/macro_scope.hpp>
5034 
5035 
5036 namespace nlohmann
5037 {
5038 namespace detail
5039 {
5041 enum class input_format_t { json, cbor, msgpack, ubjson, bson };
5042 
5044 // input adapters //
5046 
5051 class file_input_adapter
5052 {
5053  public:
5054  using char_type = char;
5055 
5056  JSON_HEDLEY_NON_NULL(2)
5057  explicit file_input_adapter(std::FILE* f) noexcept
5058  : m_file(f)
5059  {}
5060 
5061  // make class move-only
5062  file_input_adapter(const file_input_adapter&) = delete;
5063  file_input_adapter(file_input_adapter&&) = default;
5064  file_input_adapter& operator=(const file_input_adapter&) = delete;
5065  file_input_adapter& operator=(file_input_adapter&&) = delete;
5066 
5067  std::char_traits<char>::int_type get_character() noexcept
5068  {
5069  return std::fgetc(m_file);
5070  }
5071 
5072  private:
5074  std::FILE* m_file;
5075 };
5076 
5077 
5087 class input_stream_adapter
5088 {
5089  public:
5090  using char_type = char;
5091 
5092  ~input_stream_adapter()
5093  {
5094  // clear stream flags; we use underlying streambuf I/O, do not
5095  // maintain ifstream flags, except eof
5096  if (is != nullptr)
5097  {
5098  is->clear(is->rdstate() & std::ios::eofbit);
5099  }
5100  }
5101 
5102  explicit input_stream_adapter(std::istream& i)
5103  : is(&i), sb(i.rdbuf())
5104  {}
5105 
5106  // delete because of pointer members
5107  input_stream_adapter(const input_stream_adapter&) = delete;
5108  input_stream_adapter& operator=(input_stream_adapter&) = delete;
5109  input_stream_adapter& operator=(input_stream_adapter&& rhs) = delete;
5110 
5111  input_stream_adapter(input_stream_adapter&& rhs) noexcept : is(rhs.is), sb(rhs.sb)
5112  {
5113  rhs.is = nullptr;
5114  rhs.sb = nullptr;
5115  }
5116 
5117  // std::istream/std::streambuf use std::char_traits<char>::to_int_type, to
5118  // ensure that std::char_traits<char>::eof() and the character 0xFF do not
5119  // end up as the same value, eg. 0xFFFFFFFF.
5120  std::char_traits<char>::int_type get_character()
5121  {
5122  auto res = sb->sbumpc();
5123  // set eof manually, as we don't use the istream interface.
5124  if (JSON_HEDLEY_UNLIKELY(res == EOF))
5125  {
5126  is->clear(is->rdstate() | std::ios::eofbit);
5127  }
5128  return res;
5129  }
5130 
5131  private:
5133  std::istream* is = nullptr;
5134  std::streambuf* sb = nullptr;
5135 };
5136 
5137 // General-purpose iterator-based adapter. It might not be as fast as
5138 // theoretically possible for some containers, but it is extremely versatile.
5139 template<typename IteratorType>
5140 class iterator_input_adapter
5141 {
5142  public:
5143  using char_type = typename std::iterator_traits<IteratorType>::value_type;
5144 
5145  iterator_input_adapter(IteratorType first, IteratorType last)
5146  : current(std::move(first)), end(std::move(last)) {}
5147 
5148  typename std::char_traits<char_type>::int_type get_character()
5149  {
5150  if (JSON_HEDLEY_LIKELY(current != end))
5151  {
5152  auto result = std::char_traits<char_type>::to_int_type(*current);
5153  std::advance(current, 1);
5154  return result;
5155  }
5156 
5157  return std::char_traits<char_type>::eof();
5158  }
5159 
5160  private:
5161  IteratorType current;
5162  IteratorType end;
5163 
5164  template<typename BaseInputAdapter, size_t T>
5165  friend struct wide_string_input_helper;
5166 
5167  bool empty() const
5168  {
5169  return current == end;
5170  }
5171 
5172 };
5173 
5174 
5175 template<typename BaseInputAdapter, size_t T>
5176 struct wide_string_input_helper;
5177 
5178 template<typename BaseInputAdapter>
5179 struct wide_string_input_helper<BaseInputAdapter, 4>
5180 {
5181  // UTF-32
5182  static void fill_buffer(BaseInputAdapter& input,
5183  std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
5184  size_t& utf8_bytes_index,
5185  size_t& utf8_bytes_filled)
5186  {
5187  utf8_bytes_index = 0;
5188 
5189  if (JSON_HEDLEY_UNLIKELY(input.empty()))
5190  {
5191  utf8_bytes[0] = std::char_traits<char>::eof();
5192  utf8_bytes_filled = 1;
5193  }
5194  else
5195  {
5196  // get the current character
5197  const auto wc = input.get_character();
5198 
5199  // UTF-32 to UTF-8 encoding
5200  if (wc < 0x80)
5201  {
5202  utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
5203  utf8_bytes_filled = 1;
5204  }
5205  else if (wc <= 0x7FF)
5206  {
5207  utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((static_cast<unsigned int>(wc) >> 6u) & 0x1Fu));
5208  utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
5209  utf8_bytes_filled = 2;
5210  }
5211  else if (wc <= 0xFFFF)
5212  {
5213  utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((static_cast<unsigned int>(wc) >> 12u) & 0x0Fu));
5214  utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
5215  utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
5216  utf8_bytes_filled = 3;
5217  }
5218  else if (wc <= 0x10FFFF)
5219  {
5220  utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | ((static_cast<unsigned int>(wc) >> 18u) & 0x07u));
5221  utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 12u) & 0x3Fu));
5222  utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
5223  utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
5224  utf8_bytes_filled = 4;
5225  }
5226  else
5227  {
5228  // unknown character
5229  utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
5230  utf8_bytes_filled = 1;
5231  }
5232  }
5233  }
5234 };
5235 
5236 template<typename BaseInputAdapter>
5237 struct wide_string_input_helper<BaseInputAdapter, 2>
5238 {
5239  // UTF-16
5240  static void fill_buffer(BaseInputAdapter& input,
5241  std::array<std::char_traits<char>::int_type, 4>& utf8_bytes,
5242  size_t& utf8_bytes_index,
5243  size_t& utf8_bytes_filled)
5244  {
5245  utf8_bytes_index = 0;
5246 
5247  if (JSON_HEDLEY_UNLIKELY(input.empty()))
5248  {
5249  utf8_bytes[0] = std::char_traits<char>::eof();
5250  utf8_bytes_filled = 1;
5251  }
5252  else
5253  {
5254  // get the current character
5255  const auto wc = input.get_character();
5256 
5257  // UTF-16 to UTF-8 encoding
5258  if (wc < 0x80)
5259  {
5260  utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
5261  utf8_bytes_filled = 1;
5262  }
5263  else if (wc <= 0x7FF)
5264  {
5265  utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xC0u | ((static_cast<unsigned int>(wc) >> 6u)));
5266  utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
5267  utf8_bytes_filled = 2;
5268  }
5269  else if (0xD800 > wc || wc >= 0xE000)
5270  {
5271  utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xE0u | ((static_cast<unsigned int>(wc) >> 12u)));
5272  utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((static_cast<unsigned int>(wc) >> 6u) & 0x3Fu));
5273  utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | (static_cast<unsigned int>(wc) & 0x3Fu));
5274  utf8_bytes_filled = 3;
5275  }
5276  else
5277  {
5278  if (JSON_HEDLEY_UNLIKELY(!input.empty()))
5279  {
5280  const auto wc2 = static_cast<unsigned int>(input.get_character());
5281  const auto charcode = 0x10000u + (((static_cast<unsigned int>(wc) & 0x3FFu) << 10u) | (wc2 & 0x3FFu));
5282  utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(0xF0u | (charcode >> 18u));
5283  utf8_bytes[1] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 12u) & 0x3Fu));
5284  utf8_bytes[2] = static_cast<std::char_traits<char>::int_type>(0x80u | ((charcode >> 6u) & 0x3Fu));
5285  utf8_bytes[3] = static_cast<std::char_traits<char>::int_type>(0x80u | (charcode & 0x3Fu));
5286  utf8_bytes_filled = 4;
5287  }
5288  else
5289  {
5290  utf8_bytes[0] = static_cast<std::char_traits<char>::int_type>(wc);
5291  utf8_bytes_filled = 1;
5292  }
5293  }
5294  }
5295  }
5296 };
5297 
5298 // Wraps another input apdater to convert wide character types into individual bytes.
5299 template<typename BaseInputAdapter, typename WideCharType>
5300 class wide_string_input_adapter
5301 {
5302  public:
5303  using char_type = char;
5304 
5305  wide_string_input_adapter(BaseInputAdapter base)
5306  : base_adapter(base) {}
5307 
5308  typename std::char_traits<char>::int_type get_character() noexcept
5309  {
5310  // check if buffer needs to be filled
5311  if (utf8_bytes_index == utf8_bytes_filled)
5312  {
5313  fill_buffer<sizeof(WideCharType)>();
5314 
5315  JSON_ASSERT(utf8_bytes_filled > 0);
5316  JSON_ASSERT(utf8_bytes_index == 0);
5317  }
5318 
5319  // use buffer
5320  JSON_ASSERT(utf8_bytes_filled > 0);
5321  JSON_ASSERT(utf8_bytes_index < utf8_bytes_filled);
5322  return utf8_bytes[utf8_bytes_index++];
5323  }
5324 
5325  private:
5326  BaseInputAdapter base_adapter;
5327 
5328  template<size_t T>
5329  void fill_buffer()
5330  {
5331  wide_string_input_helper<BaseInputAdapter, T>::fill_buffer(base_adapter, utf8_bytes, utf8_bytes_index, utf8_bytes_filled);
5332  }
5333 
5335  std::array<std::char_traits<char>::int_type, 4> utf8_bytes = {{0, 0, 0, 0}};
5336 
5338  std::size_t utf8_bytes_index = 0;
5340  std::size_t utf8_bytes_filled = 0;
5341 };
5342 
5343 
5344 template<typename IteratorType, typename Enable = void>
5345 struct iterator_input_adapter_factory
5346 {
5347  using iterator_type = IteratorType;
5348  using char_type = typename std::iterator_traits<iterator_type>::value_type;
5349  using adapter_type = iterator_input_adapter<iterator_type>;
5350 
5351  static adapter_type create(IteratorType first, IteratorType last)
5352  {
5353  return adapter_type(std::move(first), std::move(last));
5354  }
5355 };
5356 
5357 template<typename T>
5358 struct is_iterator_of_multibyte
5359 {
5360  using value_type = typename std::iterator_traits<T>::value_type;
5361  enum
5362  {
5363  value = sizeof(value_type) > 1
5364  };
5365 };
5366 
5367 template<typename IteratorType>
5368 struct iterator_input_adapter_factory<IteratorType, enable_if_t<is_iterator_of_multibyte<IteratorType>::value>>
5369 {
5370  using iterator_type = IteratorType;
5371  using char_type = typename std::iterator_traits<iterator_type>::value_type;
5372  using base_adapter_type = iterator_input_adapter<iterator_type>;
5373  using adapter_type = wide_string_input_adapter<base_adapter_type, char_type>;
5374 
5375  static adapter_type create(IteratorType first, IteratorType last)
5376  {
5377  return adapter_type(base_adapter_type(std::move(first), std::move(last)));
5378  }
5379 };
5380 
5381 // General purpose iterator-based input
5382 template<typename IteratorType>
5383 typename iterator_input_adapter_factory<IteratorType>::adapter_type input_adapter(IteratorType first, IteratorType last)
5384 {
5385  using factory_type = iterator_input_adapter_factory<IteratorType>;
5386  return factory_type::create(first, last);
5387 }
5388 
5389 // Convenience shorthand from container to iterator
5390 // Enables ADL on begin(container) and end(container)
5391 // Encloses the using declarations in namespace for not to leak them to outside scope
5392 
5393 namespace container_input_adapter_factory_impl
5394 {
5395 
5396 using std::begin;
5397 using std::end;
5398 
5399 template<typename ContainerType, typename Enable = void>
5400 struct container_input_adapter_factory {};
5401 
5402 template<typename ContainerType>
5403 struct container_input_adapter_factory< ContainerType,
5404  void_t<decltype(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>()))>>
5405  {
5406  using adapter_type = decltype(input_adapter(begin(std::declval<ContainerType>()), end(std::declval<ContainerType>())));
5407 
5408  static adapter_type create(const ContainerType& container)
5409 {
5410  return input_adapter(begin(container), end(container));
5411 }
5412  };
5413 
5414 }
5415 
5416 template<typename ContainerType>
5417 typename container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::adapter_type input_adapter(const ContainerType& container)
5418 {
5419  return container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::create(container);
5420 }
5421 
5422 // Special cases with fast paths
5423 inline file_input_adapter input_adapter(std::FILE* file)
5424 {
5425  return file_input_adapter(file);
5426 }
5427 
5428 inline input_stream_adapter input_adapter(std::istream& stream)
5429 {
5430  return input_stream_adapter(stream);
5431 }
5432 
5433 inline input_stream_adapter input_adapter(std::istream&& stream)
5434 {
5435  return input_stream_adapter(stream);
5436 }
5437 
5438 using contiguous_bytes_input_adapter = decltype(input_adapter(std::declval<const char*>(), std::declval<const char*>()));
5439 
5440 // Null-delimited strings, and the like.
5441 template < typename CharT,
5442  typename std::enable_if <
5443  std::is_pointer<CharT>::value&&
5444  !std::is_array<CharT>::value&&
5445  std::is_integral<typename std::remove_pointer<CharT>::type>::value&&
5446  sizeof(typename std::remove_pointer<CharT>::type) == 1,
5447  int >::type = 0 >
5448 contiguous_bytes_input_adapter input_adapter(CharT b)
5449 {
5450  auto length = std::strlen(reinterpret_cast<const char*>(b));
5451  const auto* ptr = reinterpret_cast<const char*>(b);
5452  return input_adapter(ptr, ptr + length);
5453 }
5454 
5455 template<typename T, std::size_t N>
5456 auto input_adapter(T (&array)[N]) -> decltype(input_adapter(array, array + N))
5457 {
5458  return input_adapter(array, array + N);
5459 }
5460 
5461 // This class only handles inputs of input_buffer_adapter type.
5462 // It's required so that expressions like {ptr, len} can be implicitely casted
5463 // to the correct adapter.
5464 class span_input_adapter
5465 {
5466  public:
5467  template < typename CharT,
5468  typename std::enable_if <
5469  std::is_pointer<CharT>::value&&
5470  std::is_integral<typename std::remove_pointer<CharT>::type>::value&&
5471  sizeof(typename std::remove_pointer<CharT>::type) == 1,
5472  int >::type = 0 >
5473  span_input_adapter(CharT b, std::size_t l)
5474  : ia(reinterpret_cast<const char*>(b), reinterpret_cast<const char*>(b) + l) {}
5475 
5476  template<class IteratorType,
5477  typename std::enable_if<
5478  std::is_same<typename iterator_traits<IteratorType>::iterator_category, std::random_access_iterator_tag>::value,
5479  int>::type = 0>
5480  span_input_adapter(IteratorType first, IteratorType last)
5481  : ia(input_adapter(first, last)) {}
5482 
5483  contiguous_bytes_input_adapter&& get()
5484  {
5485  return std::move(ia);
5486  }
5487 
5488  private:
5489  contiguous_bytes_input_adapter ia;
5490 };
5491 } // namespace detail
5492 } // namespace nlohmann
5493 
5494 // #include <nlohmann/detail/input/json_sax.hpp>
5495 
5496 
5497 #include <cstddef>
5498 #include <string> // string
5499 #include <utility> // move
5500 #include <vector> // vector
5501 
5502 // #include <nlohmann/detail/exceptions.hpp>
5503 
5504 // #include <nlohmann/detail/macro_scope.hpp>
5505 
5506 
5507 namespace nlohmann
5508 {
5509 
5518 template<typename BasicJsonType>
5519 struct json_sax
5520 {
5521  using number_integer_t = typename BasicJsonType::number_integer_t;
5522  using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
5523  using number_float_t = typename BasicJsonType::number_float_t;
5524  using string_t = typename BasicJsonType::string_t;
5525  using binary_t = typename BasicJsonType::binary_t;
5526 
5531  virtual bool null() = 0;
5532 
5538  virtual bool boolean(bool val) = 0;
5539 
5545  virtual bool number_integer(number_integer_t val) = 0;
5546 
5552  virtual bool number_unsigned(number_unsigned_t val) = 0;
5553 
5560  virtual bool number_float(number_float_t val, const string_t& s) = 0;
5561 
5568  virtual bool string(string_t& val) = 0;
5569 
5576  virtual bool binary(binary_t& val) = 0;
5577 
5584  virtual bool start_object(std::size_t elements) = 0;
5585 
5592  virtual bool key(string_t& val) = 0;
5593 
5598  virtual bool end_object() = 0;
5599 
5606  virtual bool start_array(std::size_t elements) = 0;
5607 
5612  virtual bool end_array() = 0;
5613 
5621  virtual bool parse_error(std::size_t position,
5622  const std::string& last_token,
5623  const detail::exception& ex) = 0;
5624 
5625  virtual ~json_sax() = default;
5626 };
5627 
5628 
5629 namespace detail
5630 {
5644 template<typename BasicJsonType>
5645 class json_sax_dom_parser
5646 {
5647  public:
5648  using number_integer_t = typename BasicJsonType::number_integer_t;
5649  using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
5650  using number_float_t = typename BasicJsonType::number_float_t;
5651  using string_t = typename BasicJsonType::string_t;
5652  using binary_t = typename BasicJsonType::binary_t;
5653 
5659  explicit json_sax_dom_parser(BasicJsonType& r, const bool allow_exceptions_ = true)
5660  : root(r), allow_exceptions(allow_exceptions_)
5661  {}
5662 
5663  // make class move-only
5664  json_sax_dom_parser(const json_sax_dom_parser&) = delete;
5665  json_sax_dom_parser(json_sax_dom_parser&&) = default;
5666  json_sax_dom_parser& operator=(const json_sax_dom_parser&) = delete;
5667  json_sax_dom_parser& operator=(json_sax_dom_parser&&) = default;
5668  ~json_sax_dom_parser() = default;
5669 
5670  bool null()
5671  {
5672  handle_value(nullptr);
5673  return true;
5674  }
5675 
5676  bool boolean(bool val)
5677  {
5678  handle_value(val);
5679  return true;
5680  }
5681 
5682  bool number_integer(number_integer_t val)
5683  {
5684  handle_value(val);
5685  return true;
5686  }
5687 
5688  bool number_unsigned(number_unsigned_t val)
5689  {
5690  handle_value(val);
5691  return true;
5692  }
5693 
5694  bool number_float(number_float_t val, const string_t& /*unused*/)
5695  {
5696  handle_value(val);
5697  return true;
5698  }
5699 
5700  bool string(string_t& val)
5701  {
5702  handle_value(val);
5703  return true;
5704  }
5705 
5706  bool binary(binary_t& val)
5707  {
5708  handle_value(std::move(val));
5709  return true;
5710  }
5711 
5712  bool start_object(std::size_t len)
5713  {
5714  ref_stack.push_back(handle_value(BasicJsonType::value_t::object));
5715 
5716  if (JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size()))
5717  {
5718  JSON_THROW(out_of_range::create(408, "excessive object size: " + std::to_string(len), *ref_stack.back()));
5719  }
5720 
5721  return true;
5722  }
5723 
5724  bool key(string_t& val)
5725  {
5726  // add null at given key and store the reference for later
5727  object_element = &(ref_stack.back()->m_value.object->operator[](val));
5728  return true;
5729  }
5730 
5731  bool end_object()
5732  {
5733  ref_stack.back()->set_parents();
5734  ref_stack.pop_back();
5735  return true;
5736  }
5737 
5738  bool start_array(std::size_t len)
5739  {
5740  ref_stack.push_back(handle_value(BasicJsonType::value_t::array));
5741 
5742  if (JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size()))
5743  {
5744  JSON_THROW(out_of_range::create(408, "excessive array size: " + std::to_string(len), *ref_stack.back()));
5745  }
5746 
5747  return true;
5748  }
5749 
5750  bool end_array()
5751  {
5752  ref_stack.back()->set_parents();
5753  ref_stack.pop_back();
5754  return true;
5755  }
5756 
5757  template<class Exception>
5758  bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
5759  const Exception& ex)
5760  {
5761  errored = true;
5762  static_cast<void>(ex);
5763  if (allow_exceptions)
5764  {
5765  JSON_THROW(ex);
5766  }
5767  return false;
5768  }
5769 
5770  constexpr bool is_errored() const
5771  {
5772  return errored;
5773  }
5774 
5775  private:
5782  template<typename Value>
5783 
5784  BasicJsonType* handle_value(Value&& v)
5785  {
5786  if (ref_stack.empty())
5787  {
5788  root = BasicJsonType(std::forward<Value>(v));
5789  return &root;
5790  }
5791 
5792  JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
5793 
5794  if (ref_stack.back()->is_array())
5795  {
5796  ref_stack.back()->m_value.array->emplace_back(std::forward<Value>(v));
5797  return &(ref_stack.back()->m_value.array->back());
5798  }
5799 
5800  JSON_ASSERT(ref_stack.back()->is_object());
5801  JSON_ASSERT(object_element);
5802  *object_element = BasicJsonType(std::forward<Value>(v));
5803  return object_element;
5804  }
5805 
5807  BasicJsonType& root;
5809  std::vector<BasicJsonType*> ref_stack {};
5811  BasicJsonType* object_element = nullptr;
5813  bool errored = false;
5815  const bool allow_exceptions = true;
5816 };
5817 
5818 template<typename BasicJsonType>
5819 class json_sax_dom_callback_parser
5820 {
5821  public:
5822  using number_integer_t = typename BasicJsonType::number_integer_t;
5823  using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
5824  using number_float_t = typename BasicJsonType::number_float_t;
5825  using string_t = typename BasicJsonType::string_t;
5826  using binary_t = typename BasicJsonType::binary_t;
5827  using parser_callback_t = typename BasicJsonType::parser_callback_t;
5828  using parse_event_t = typename BasicJsonType::parse_event_t;
5829 
5830  json_sax_dom_callback_parser(BasicJsonType& r,
5831  const parser_callback_t cb,
5832  const bool allow_exceptions_ = true)
5833  : root(r), callback(cb), allow_exceptions(allow_exceptions_)
5834  {
5835  keep_stack.push_back(true);
5836  }
5837 
5838  // make class move-only
5839  json_sax_dom_callback_parser(const json_sax_dom_callback_parser&) = delete;
5840  json_sax_dom_callback_parser(json_sax_dom_callback_parser&&) = default;
5841  json_sax_dom_callback_parser& operator=(const json_sax_dom_callback_parser&) = delete;
5842  json_sax_dom_callback_parser& operator=(json_sax_dom_callback_parser&&) = default;
5843  ~json_sax_dom_callback_parser() = default;
5844 
5845  bool null()
5846  {
5847  handle_value(nullptr);
5848  return true;
5849  }
5850 
5851  bool boolean(bool val)
5852  {
5853  handle_value(val);
5854  return true;
5855  }
5856 
5857  bool number_integer(number_integer_t val)
5858  {
5859  handle_value(val);
5860  return true;
5861  }
5862 
5863  bool number_unsigned(number_unsigned_t val)
5864  {
5865  handle_value(val);
5866  return true;
5867  }
5868 
5869  bool number_float(number_float_t val, const string_t& /*unused*/)
5870  {
5871  handle_value(val);
5872  return true;
5873  }
5874 
5875  bool string(string_t& val)
5876  {
5877  handle_value(val);
5878  return true;
5879  }
5880 
5881  bool binary(binary_t& val)
5882  {
5883  handle_value(std::move(val));
5884  return true;
5885  }
5886 
5887  bool start_object(std::size_t len)
5888  {
5889  // check callback for object start
5890  const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::object_start, discarded);
5891  keep_stack.push_back(keep);
5892 
5893  auto val = handle_value(BasicJsonType::value_t::object, true);
5894  ref_stack.push_back(val.second);
5895 
5896  // check object limit
5897  if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size()))
5898  {
5899  JSON_THROW(out_of_range::create(408, "excessive object size: " + std::to_string(len), *ref_stack.back()));
5900  }
5901 
5902  return true;
5903  }
5904 
5905  bool key(string_t& val)
5906  {
5907  BasicJsonType k = BasicJsonType(val);
5908 
5909  // check callback for key
5910  const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::key, k);
5911  key_keep_stack.push_back(keep);
5912 
5913  // add discarded value at given key and store the reference for later
5914  if (keep && ref_stack.back())
5915  {
5916  object_element = &(ref_stack.back()->m_value.object->operator[](val) = discarded);
5917  }
5918 
5919  return true;
5920  }
5921 
5922  bool end_object()
5923  {
5924  if (ref_stack.back())
5925  {
5926  if (!callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back()))
5927  {
5928  // discard object
5929  *ref_stack.back() = discarded;
5930  }
5931  else
5932  {
5933  ref_stack.back()->set_parents();
5934  }
5935  }
5936 
5937  JSON_ASSERT(!ref_stack.empty());
5938  JSON_ASSERT(!keep_stack.empty());
5939  ref_stack.pop_back();
5940  keep_stack.pop_back();
5941 
5942  if (!ref_stack.empty() && ref_stack.back() && ref_stack.back()->is_structured())
5943  {
5944  // remove discarded value
5945  for (auto it = ref_stack.back()->begin(); it != ref_stack.back()->end(); ++it)
5946  {
5947  if (it->is_discarded())
5948  {
5949  ref_stack.back()->erase(it);
5950  break;
5951  }
5952  }
5953  }
5954 
5955  return true;
5956  }
5957 
5958  bool start_array(std::size_t len)
5959  {
5960  const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::array_start, discarded);
5961  keep_stack.push_back(keep);
5962 
5963  auto val = handle_value(BasicJsonType::value_t::array, true);
5964  ref_stack.push_back(val.second);
5965 
5966  // check array limit
5967  if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size()))
5968  {
5969  JSON_THROW(out_of_range::create(408, "excessive array size: " + std::to_string(len), *ref_stack.back()));
5970  }
5971 
5972  return true;
5973  }
5974 
5975  bool end_array()
5976  {
5977  bool keep = true;
5978 
5979  if (ref_stack.back())
5980  {
5981  keep = callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back());
5982  if (keep)
5983  {
5984  ref_stack.back()->set_parents();
5985  }
5986  else
5987  {
5988  // discard array
5989  *ref_stack.back() = discarded;
5990  }
5991  }
5992 
5993  JSON_ASSERT(!ref_stack.empty());
5994  JSON_ASSERT(!keep_stack.empty());
5995  ref_stack.pop_back();
5996  keep_stack.pop_back();
5997 
5998  // remove discarded value
5999  if (!keep && !ref_stack.empty() && ref_stack.back()->is_array())
6000  {
6001  ref_stack.back()->m_value.array->pop_back();
6002  }
6003 
6004  return true;
6005  }
6006 
6007  template<class Exception>
6008  bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
6009  const Exception& ex)
6010  {
6011  errored = true;
6012  static_cast<void>(ex);
6013  if (allow_exceptions)
6014  {
6015  JSON_THROW(ex);
6016  }
6017  return false;
6018  }
6019 
6020  constexpr bool is_errored() const
6021  {
6022  return errored;
6023  }
6024 
6025  private:
6041  template<typename Value>
6042  std::pair<bool, BasicJsonType*> handle_value(Value&& v, const bool skip_callback = false)
6043  {
6044  JSON_ASSERT(!keep_stack.empty());
6045 
6046  // do not handle this value if we know it would be added to a discarded
6047  // container
6048  if (!keep_stack.back())
6049  {
6050  return {false, nullptr};
6051  }
6052 
6053  // create value
6054  auto value = BasicJsonType(std::forward<Value>(v));
6055 
6056  // check callback
6057  const bool keep = skip_callback || callback(static_cast<int>(ref_stack.size()), parse_event_t::value, value);
6058 
6059  // do not handle this value if we just learnt it shall be discarded
6060  if (!keep)
6061  {
6062  return {false, nullptr};
6063  }
6064 
6065  if (ref_stack.empty())
6066  {
6067  root = std::move(value);
6068  return {true, &root};
6069  }
6070 
6071  // skip this value if we already decided to skip the parent
6072  // (https://github.com/nlohmann/json/issues/971#issuecomment-413678360)
6073  if (!ref_stack.back())
6074  {
6075  return {false, nullptr};
6076  }
6077 
6078  // we now only expect arrays and objects
6079  JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object());
6080 
6081  // array
6082  if (ref_stack.back()->is_array())
6083  {
6084  ref_stack.back()->m_value.array->emplace_back(std::move(value));
6085  return {true, &(ref_stack.back()->m_value.array->back())};
6086  }
6087 
6088  // object
6089  JSON_ASSERT(ref_stack.back()->is_object());
6090  // check if we should store an element for the current key
6091  JSON_ASSERT(!key_keep_stack.empty());
6092  const bool store_element = key_keep_stack.back();
6093  key_keep_stack.pop_back();
6094 
6095  if (!store_element)
6096  {
6097  return {false, nullptr};
6098  }
6099 
6100  JSON_ASSERT(object_element);
6101  *object_element = std::move(value);
6102  return {true, object_element};
6103  }
6104 
6106  BasicJsonType& root;
6108  std::vector<BasicJsonType*> ref_stack {};
6110  std::vector<bool> keep_stack {};
6112  std::vector<bool> key_keep_stack {};
6114  BasicJsonType* object_element = nullptr;
6116  bool errored = false;
6118  const parser_callback_t callback = nullptr;
6120  const bool allow_exceptions = true;
6122  BasicJsonType discarded = BasicJsonType::value_t::discarded;
6123 };
6124 
6125 template<typename BasicJsonType>
6126 class json_sax_acceptor
6127 {
6128  public:
6129  using number_integer_t = typename BasicJsonType::number_integer_t;
6130  using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
6131  using number_float_t = typename BasicJsonType::number_float_t;
6132  using string_t = typename BasicJsonType::string_t;
6133  using binary_t = typename BasicJsonType::binary_t;
6134 
6135  bool null()
6136  {
6137  return true;
6138  }
6139 
6140  bool boolean(bool /*unused*/)
6141  {
6142  return true;
6143  }
6144 
6145  bool number_integer(number_integer_t /*unused*/)
6146  {
6147  return true;
6148  }
6149 
6150  bool number_unsigned(number_unsigned_t /*unused*/)
6151  {
6152  return true;
6153  }
6154 
6155  bool number_float(number_float_t /*unused*/, const string_t& /*unused*/)
6156  {
6157  return true;
6158  }
6159 
6160  bool string(string_t& /*unused*/)
6161  {
6162  return true;
6163  }
6164 
6165  bool binary(binary_t& /*unused*/)
6166  {
6167  return true;
6168  }
6169 
6170  bool start_object(std::size_t /*unused*/ = std::size_t(-1))
6171  {
6172  return true;
6173  }
6174 
6175  bool key(string_t& /*unused*/)
6176  {
6177  return true;
6178  }
6179 
6180  bool end_object()
6181  {
6182  return true;
6183  }
6184 
6185  bool start_array(std::size_t /*unused*/ = std::size_t(-1))
6186  {
6187  return true;
6188  }
6189 
6190  bool end_array()
6191  {
6192  return true;
6193  }
6194 
6195  bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const detail::exception& /*unused*/)
6196  {
6197  return false;
6198  }
6199 };
6200 } // namespace detail
6201 
6202 } // namespace nlohmann
6203 
6204 // #include <nlohmann/detail/input/lexer.hpp>
6205 
6206 
6207 #include <array> // array
6208 #include <clocale> // localeconv
6209 #include <cstddef> // size_t
6210 #include <cstdio> // snprintf
6211 #include <cstdlib> // strtof, strtod, strtold, strtoll, strtoull
6212 #include <initializer_list> // initializer_list
6213 #include <string> // char_traits, string
6214 #include <utility> // move
6215 #include <vector> // vector
6216 
6217 // #include <nlohmann/detail/input/input_adapters.hpp>
6218 
6219 // #include <nlohmann/detail/input/position_t.hpp>
6220 
6221 // #include <nlohmann/detail/macro_scope.hpp>
6222 
6223 
6224 namespace nlohmann
6225 {
6226 namespace detail
6227 {
6229 // lexer //
6231 
6232 template<typename BasicJsonType>
6233 class lexer_base
6234 {
6235  public:
6237  enum class token_type
6238  {
6239  uninitialized,
6240  literal_true,
6241  literal_false,
6242  literal_null,
6243  value_string,
6244  value_unsigned,
6245  value_integer,
6246  value_float,
6247  begin_array,
6248  begin_object,
6249  end_array,
6250  end_object,
6251  name_separator,
6252  value_separator,
6253  parse_error,
6254  end_of_input,
6255  literal_or_value
6256  };
6257 
6259 
6260  JSON_HEDLEY_CONST
6261  static const char* token_type_name(const token_type t) noexcept
6262  {
6263  switch (t)
6264  {
6265  case token_type::uninitialized:
6266  return "<uninitialized>";
6267  case token_type::literal_true:
6268  return "true literal";
6269  case token_type::literal_false:
6270  return "false literal";
6271  case token_type::literal_null:
6272  return "null literal";
6273  case token_type::value_string:
6274  return "string literal";
6275  case token_type::value_unsigned:
6276  case token_type::value_integer:
6277  case token_type::value_float:
6278  return "number literal";
6279  case token_type::begin_array:
6280  return "'['";
6281  case token_type::begin_object:
6282  return "'{'";
6283  case token_type::end_array:
6284  return "']'";
6285  case token_type::end_object:
6286  return "'}'";
6287  case token_type::name_separator:
6288  return "':'";
6289  case token_type::value_separator:
6290  return "','";
6291  case token_type::parse_error:
6292  return "<parse error>";
6293  case token_type::end_of_input:
6294  return "end of input";
6295  case token_type::literal_or_value:
6296  return "'[', '{', or a literal";
6297  // LCOV_EXCL_START
6298  default: // catch non-enum values
6299  return "unknown token";
6300  // LCOV_EXCL_STOP
6301  }
6302  }
6303 };
6309 template<typename BasicJsonType, typename InputAdapterType>
6310 class lexer : public lexer_base<BasicJsonType>
6311 {
6312  using number_integer_t = typename BasicJsonType::number_integer_t;
6313  using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
6314  using number_float_t = typename BasicJsonType::number_float_t;
6315  using string_t = typename BasicJsonType::string_t;
6316  using char_type = typename InputAdapterType::char_type;
6317  using char_int_type = typename std::char_traits<char_type>::int_type;
6318 
6319  public:
6320  using token_type = typename lexer_base<BasicJsonType>::token_type;
6321 
6322  explicit lexer(InputAdapterType&& adapter, bool ignore_comments_ = false)
6323  : ia(std::move(adapter))
6324  , ignore_comments(ignore_comments_)
6325  , decimal_point_char(static_cast<char_int_type>(get_decimal_point()))
6326  {}
6327 
6328  // delete because of pointer members
6329  lexer(const lexer&) = delete;
6330  lexer(lexer&&) = default;
6331  lexer& operator=(lexer&) = delete;
6332  lexer& operator=(lexer&&) = default;
6333  ~lexer() = default;
6334 
6335  private:
6337  // locales
6339 
6341  JSON_HEDLEY_PURE
6342  static char get_decimal_point() noexcept
6343  {
6344  const auto* loc = localeconv();
6345  JSON_ASSERT(loc != nullptr);
6346  return (loc->decimal_point == nullptr) ? '.' : *(loc->decimal_point);
6347  }
6348 
6350  // scan functions
6352 
6368  int get_codepoint()
6369  {
6370  // this function only makes sense after reading `\u`
6371  JSON_ASSERT(current == 'u');
6372  int codepoint = 0;
6373 
6374  const auto factors = { 12u, 8u, 4u, 0u };
6375  for (const auto factor : factors)
6376  {
6377  get();
6378 
6379  if (current >= '0' && current <= '9')
6380  {
6381  codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x30u) << factor);
6382  }
6383  else if (current >= 'A' && current <= 'F')
6384  {
6385  codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x37u) << factor);
6386  }
6387  else if (current >= 'a' && current <= 'f')
6388  {
6389  codepoint += static_cast<int>((static_cast<unsigned int>(current) - 0x57u) << factor);
6390  }
6391  else
6392  {
6393  return -1;
6394  }
6395  }
6396 
6397  JSON_ASSERT(0x0000 <= codepoint && codepoint <= 0xFFFF);
6398  return codepoint;
6399  }
6400 
6416  bool next_byte_in_range(std::initializer_list<char_int_type> ranges)
6417  {
6418  JSON_ASSERT(ranges.size() == 2 || ranges.size() == 4 || ranges.size() == 6);
6419  add(current);
6420 
6421  for (auto range = ranges.begin(); range != ranges.end(); ++range)
6422  {
6423  get();
6424  if (JSON_HEDLEY_LIKELY(*range <= current && current <= *(++range)))
6425  {
6426  add(current);
6427  }
6428  else
6429  {
6430  error_message = "invalid string: ill-formed UTF-8 byte";
6431  return false;
6432  }
6433  }
6434 
6435  return true;
6436  }
6437 
6453  token_type scan_string()
6454  {
6455  // reset token_buffer (ignore opening quote)
6456  reset();
6457 
6458  // we entered the function by reading an open quote
6459  JSON_ASSERT(current == '\"');
6460 
6461  while (true)
6462  {
6463  // get next character
6464  switch (get())
6465  {
6466  // end of file while parsing string
6467  case std::char_traits<char_type>::eof():
6468  {
6469  error_message = "invalid string: missing closing quote";
6470  return token_type::parse_error;
6471  }
6472 
6473  // closing quote
6474  case '\"':
6475  {
6476  return token_type::value_string;
6477  }
6478 
6479  // escapes
6480  case '\\':
6481  {
6482  switch (get())
6483  {
6484  // quotation mark
6485  case '\"':
6486  add('\"');
6487  break;
6488  // reverse solidus
6489  case '\\':
6490  add('\\');
6491  break;
6492  // solidus
6493  case '/':
6494  add('/');
6495  break;
6496  // backspace
6497  case 'b':
6498  add('\b');
6499  break;
6500  // form feed
6501  case 'f':
6502  add('\f');
6503  break;
6504  // line feed
6505  case 'n':
6506  add('\n');
6507  break;
6508  // carriage return
6509  case 'r':
6510  add('\r');
6511  break;
6512  // tab
6513  case 't':
6514  add('\t');
6515  break;
6516 
6517  // unicode escapes
6518  case 'u':
6519  {
6520  const int codepoint1 = get_codepoint();
6521  int codepoint = codepoint1; // start with codepoint1
6522 
6523  if (JSON_HEDLEY_UNLIKELY(codepoint1 == -1))
6524  {
6525  error_message = "invalid string: '\\u' must be followed by 4 hex digits";
6526  return token_type::parse_error;
6527  }
6528 
6529  // check if code point is a high surrogate
6530  if (0xD800 <= codepoint1 && codepoint1 <= 0xDBFF)
6531  {
6532  // expect next \uxxxx entry
6533  if (JSON_HEDLEY_LIKELY(get() == '\\' && get() == 'u'))
6534  {
6535  const int codepoint2 = get_codepoint();
6536 
6537  if (JSON_HEDLEY_UNLIKELY(codepoint2 == -1))
6538  {
6539  error_message = "invalid string: '\\u' must be followed by 4 hex digits";
6540  return token_type::parse_error;
6541  }
6542 
6543  // check if codepoint2 is a low surrogate
6544  if (JSON_HEDLEY_LIKELY(0xDC00 <= codepoint2 && codepoint2 <= 0xDFFF))
6545  {
6546  // overwrite codepoint
6547  codepoint = static_cast<int>(
6548  // high surrogate occupies the most significant 22 bits
6549  (static_cast<unsigned int>(codepoint1) << 10u)
6550  // low surrogate occupies the least significant 15 bits
6551  + static_cast<unsigned int>(codepoint2)
6552  // there is still the 0xD800, 0xDC00 and 0x10000 noise
6553  // in the result so we have to subtract with:
6554  // (0xD800 << 10) + DC00 - 0x10000 = 0x35FDC00
6555  - 0x35FDC00u);
6556  }
6557  else
6558  {
6559  error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF";
6560  return token_type::parse_error;
6561  }
6562  }
6563  else
6564  {
6565  error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF";
6566  return token_type::parse_error;
6567  }
6568  }
6569  else
6570  {
6571  if (JSON_HEDLEY_UNLIKELY(0xDC00 <= codepoint1 && codepoint1 <= 0xDFFF))
6572  {
6573  error_message = "invalid string: surrogate U+DC00..U+DFFF must follow U+D800..U+DBFF";
6574  return token_type::parse_error;
6575  }
6576  }
6577 
6578  // result of the above calculation yields a proper codepoint
6579  JSON_ASSERT(0x00 <= codepoint && codepoint <= 0x10FFFF);
6580 
6581  // translate codepoint into bytes
6582  if (codepoint < 0x80)
6583  {
6584  // 1-byte characters: 0xxxxxxx (ASCII)
6585  add(static_cast<char_int_type>(codepoint));
6586  }
6587  else if (codepoint <= 0x7FF)
6588  {
6589  // 2-byte characters: 110xxxxx 10xxxxxx
6590  add(static_cast<char_int_type>(0xC0u | (static_cast<unsigned int>(codepoint) >> 6u)));
6591  add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
6592  }
6593  else if (codepoint <= 0xFFFF)
6594  {
6595  // 3-byte characters: 1110xxxx 10xxxxxx 10xxxxxx
6596  add(static_cast<char_int_type>(0xE0u | (static_cast<unsigned int>(codepoint) >> 12u)));
6597  add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
6598  add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
6599  }
6600  else
6601  {
6602  // 4-byte characters: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
6603  add(static_cast<char_int_type>(0xF0u | (static_cast<unsigned int>(codepoint) >> 18u)));
6604  add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 12u) & 0x3Fu)));
6605  add(static_cast<char_int_type>(0x80u | ((static_cast<unsigned int>(codepoint) >> 6u) & 0x3Fu)));
6606  add(static_cast<char_int_type>(0x80u | (static_cast<unsigned int>(codepoint) & 0x3Fu)));
6607  }
6608 
6609  break;
6610  }
6611 
6612  // other characters after escape
6613  default:
6614  error_message = "invalid string: forbidden character after backslash";
6615  return token_type::parse_error;
6616  }
6617 
6618  break;
6619  }
6620 
6621  // invalid control characters
6622  case 0x00:
6623  {
6624  error_message = "invalid string: control character U+0000 (NUL) must be escaped to \\u0000";
6625  return token_type::parse_error;
6626  }
6627 
6628  case 0x01:
6629  {
6630  error_message = "invalid string: control character U+0001 (SOH) must be escaped to \\u0001";
6631  return token_type::parse_error;
6632  }
6633 
6634  case 0x02:
6635  {
6636  error_message = "invalid string: control character U+0002 (STX) must be escaped to \\u0002";
6637  return token_type::parse_error;
6638  }
6639 
6640  case 0x03:
6641  {
6642  error_message = "invalid string: control character U+0003 (ETX) must be escaped to \\u0003";
6643  return token_type::parse_error;
6644  }
6645 
6646  case 0x04:
6647  {
6648  error_message = "invalid string: control character U+0004 (EOT) must be escaped to \\u0004";
6649  return token_type::parse_error;
6650  }
6651 
6652  case 0x05:
6653  {
6654  error_message = "invalid string: control character U+0005 (ENQ) must be escaped to \\u0005";
6655  return token_type::parse_error;
6656  }
6657 
6658  case 0x06:
6659  {
6660  error_message = "invalid string: control character U+0006 (ACK) must be escaped to \\u0006";
6661  return token_type::parse_error;
6662  }
6663 
6664  case 0x07:
6665  {
6666  error_message = "invalid string: control character U+0007 (BEL) must be escaped to \\u0007";
6667  return token_type::parse_error;
6668  }
6669 
6670  case 0x08:
6671  {
6672  error_message = "invalid string: control character U+0008 (BS) must be escaped to \\u0008 or \\b";
6673  return token_type::parse_error;
6674  }
6675 
6676  case 0x09:
6677  {
6678  error_message = "invalid string: control character U+0009 (HT) must be escaped to \\u0009 or \\t";
6679  return token_type::parse_error;
6680  }
6681 
6682  case 0x0A:
6683  {
6684  error_message = "invalid string: control character U+000A (LF) must be escaped to \\u000A or \\n";
6685  return token_type::parse_error;
6686  }
6687 
6688  case 0x0B:
6689  {
6690  error_message = "invalid string: control character U+000B (VT) must be escaped to \\u000B";
6691  return token_type::parse_error;
6692  }
6693 
6694  case 0x0C:
6695  {
6696  error_message = "invalid string: control character U+000C (FF) must be escaped to \\u000C or \\f";
6697  return token_type::parse_error;
6698  }
6699 
6700  case 0x0D:
6701  {
6702  error_message = "invalid string: control character U+000D (CR) must be escaped to \\u000D or \\r";
6703  return token_type::parse_error;
6704  }
6705 
6706  case 0x0E:
6707  {
6708  error_message = "invalid string: control character U+000E (SO) must be escaped to \\u000E";
6709  return token_type::parse_error;
6710  }
6711 
6712  case 0x0F:
6713  {
6714  error_message = "invalid string: control character U+000F (SI) must be escaped to \\u000F";
6715  return token_type::parse_error;
6716  }
6717 
6718  case 0x10:
6719  {
6720  error_message = "invalid string: control character U+0010 (DLE) must be escaped to \\u0010";
6721  return token_type::parse_error;
6722  }
6723 
6724  case 0x11:
6725  {
6726  error_message = "invalid string: control character U+0011 (DC1) must be escaped to \\u0011";
6727  return token_type::parse_error;
6728  }
6729 
6730  case 0x12:
6731  {
6732  error_message = "invalid string: control character U+0012 (DC2) must be escaped to \\u0012";
6733  return token_type::parse_error;
6734  }
6735 
6736  case 0x13:
6737  {
6738  error_message = "invalid string: control character U+0013 (DC3) must be escaped to \\u0013";
6739  return token_type::parse_error;
6740  }
6741 
6742  case 0x14:
6743  {
6744  error_message = "invalid string: control character U+0014 (DC4) must be escaped to \\u0014";
6745  return token_type::parse_error;
6746  }
6747 
6748  case 0x15:
6749  {
6750  error_message = "invalid string: control character U+0015 (NAK) must be escaped to \\u0015";
6751  return token_type::parse_error;
6752  }
6753 
6754  case 0x16:
6755  {
6756  error_message = "invalid string: control character U+0016 (SYN) must be escaped to \\u0016";
6757  return token_type::parse_error;
6758  }
6759 
6760  case 0x17:
6761  {
6762  error_message = "invalid string: control character U+0017 (ETB) must be escaped to \\u0017";
6763  return token_type::parse_error;
6764  }
6765 
6766  case 0x18:
6767  {
6768  error_message = "invalid string: control character U+0018 (CAN) must be escaped to \\u0018";
6769  return token_type::parse_error;
6770  }
6771 
6772  case 0x19:
6773  {
6774  error_message = "invalid string: control character U+0019 (EM) must be escaped to \\u0019";
6775  return token_type::parse_error;
6776  }
6777 
6778  case 0x1A:
6779  {
6780  error_message = "invalid string: control character U+001A (SUB) must be escaped to \\u001A";
6781  return token_type::parse_error;
6782  }
6783 
6784  case 0x1B:
6785  {
6786  error_message = "invalid string: control character U+001B (ESC) must be escaped to \\u001B";
6787  return token_type::parse_error;
6788  }
6789 
6790  case 0x1C:
6791  {
6792  error_message = "invalid string: control character U+001C (FS) must be escaped to \\u001C";
6793  return token_type::parse_error;
6794  }
6795 
6796  case 0x1D:
6797  {
6798  error_message = "invalid string: control character U+001D (GS) must be escaped to \\u001D";
6799  return token_type::parse_error;
6800  }
6801 
6802  case 0x1E:
6803  {
6804  error_message = "invalid string: control character U+001E (RS) must be escaped to \\u001E";
6805  return token_type::parse_error;
6806  }
6807 
6808  case 0x1F:
6809  {
6810  error_message = "invalid string: control character U+001F (US) must be escaped to \\u001F";
6811  return token_type::parse_error;
6812  }
6813 
6814  // U+0020..U+007F (except U+0022 (quote) and U+005C (backspace))
6815  case 0x20:
6816  case 0x21:
6817  case 0x23:
6818  case 0x24:
6819  case 0x25:
6820  case 0x26:
6821  case 0x27:
6822  case 0x28:
6823  case 0x29:
6824  case 0x2A:
6825  case 0x2B:
6826  case 0x2C:
6827  case 0x2D:
6828  case 0x2E:
6829  case 0x2F:
6830  case 0x30:
6831  case 0x31:
6832  case 0x32:
6833  case 0x33:
6834  case 0x34:
6835  case 0x35:
6836  case 0x36:
6837  case 0x37:
6838  case 0x38:
6839  case 0x39:
6840  case 0x3A:
6841  case 0x3B:
6842  case 0x3C:
6843  case 0x3D:
6844  case 0x3E:
6845  case 0x3F:
6846  case 0x40:
6847  case 0x41:
6848  case 0x42:
6849  case 0x43:
6850  case 0x44:
6851  case 0x45:
6852  case 0x46:
6853  case 0x47:
6854  case 0x48:
6855  case 0x49:
6856  case 0x4A:
6857  case 0x4B:
6858  case 0x4C:
6859  case 0x4D:
6860  case 0x4E:
6861  case 0x4F:
6862  case 0x50:
6863  case 0x51:
6864  case 0x52:
6865  case 0x53:
6866  case 0x54:
6867  case 0x55:
6868  case 0x56:
6869  case 0x57:
6870  case 0x58:
6871  case 0x59:
6872  case 0x5A:
6873  case 0x5B:
6874  case 0x5D:
6875  case 0x5E:
6876  case 0x5F:
6877  case 0x60:
6878  case 0x61:
6879  case 0x62:
6880  case 0x63:
6881  case 0x64:
6882  case 0x65:
6883  case 0x66:
6884  case 0x67:
6885  case 0x68:
6886  case 0x69:
6887  case 0x6A:
6888  case 0x6B:
6889  case 0x6C:
6890  case 0x6D:
6891  case 0x6E:
6892  case 0x6F:
6893  case 0x70:
6894  case 0x71:
6895  case 0x72:
6896  case 0x73:
6897  case 0x74:
6898  case 0x75:
6899  case 0x76:
6900  case 0x77:
6901  case 0x78:
6902  case 0x79:
6903  case 0x7A:
6904  case 0x7B:
6905  case 0x7C:
6906  case 0x7D:
6907  case 0x7E:
6908  case 0x7F:
6909  {
6910  add(current);
6911  break;
6912  }
6913 
6914  // U+0080..U+07FF: bytes C2..DF 80..BF
6915  case 0xC2:
6916  case 0xC3:
6917  case 0xC4:
6918  case 0xC5:
6919  case 0xC6:
6920  case 0xC7:
6921  case 0xC8:
6922  case 0xC9:
6923  case 0xCA:
6924  case 0xCB:
6925  case 0xCC:
6926  case 0xCD:
6927  case 0xCE:
6928  case 0xCF:
6929  case 0xD0:
6930  case 0xD1:
6931  case 0xD2:
6932  case 0xD3:
6933  case 0xD4:
6934  case 0xD5:
6935  case 0xD6:
6936  case 0xD7:
6937  case 0xD8:
6938  case 0xD9:
6939  case 0xDA:
6940  case 0xDB:
6941  case 0xDC:
6942  case 0xDD:
6943  case 0xDE:
6944  case 0xDF:
6945  {
6946  if (JSON_HEDLEY_UNLIKELY(!next_byte_in_range({0x80, 0xBF})))
6947  {
6948  return token_type::parse_error;
6949  }
6950  break;
6951  }
6952 
6953  // U+0800..U+0FFF: bytes E0 A0..BF 80..BF
6954  case 0xE0:
6955  {
6956  if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0xA0, 0xBF, 0x80, 0xBF}))))
6957  {
6958  return token_type::parse_error;
6959  }
6960  break;
6961  }
6962 
6963  // U+1000..U+CFFF: bytes E1..EC 80..BF 80..BF
6964  // U+E000..U+FFFF: bytes EE..EF 80..BF 80..BF
6965  case 0xE1:
6966  case 0xE2:
6967  case 0xE3:
6968  case 0xE4:
6969  case 0xE5:
6970  case 0xE6:
6971  case 0xE7:
6972  case 0xE8:
6973  case 0xE9:
6974  case 0xEA:
6975  case 0xEB:
6976  case 0xEC:
6977  case 0xEE:
6978  case 0xEF:
6979  {
6980  if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF}))))
6981  {
6982  return token_type::parse_error;
6983  }
6984  break;
6985  }
6986 
6987  // U+D000..U+D7FF: bytes ED 80..9F 80..BF
6988  case 0xED:
6989  {
6990  if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x9F, 0x80, 0xBF}))))
6991  {
6992  return token_type::parse_error;
6993  }
6994  break;
6995  }
6996 
6997  // U+10000..U+3FFFF F0 90..BF 80..BF 80..BF
6998  case 0xF0:
6999  {
7000  if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x90, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
7001  {
7002  return token_type::parse_error;
7003  }
7004  break;
7005  }
7006 
7007  // U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF
7008  case 0xF1:
7009  case 0xF2:
7010  case 0xF3:
7011  {
7012  if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
7013  {
7014  return token_type::parse_error;
7015  }
7016  break;
7017  }
7018 
7019  // U+100000..U+10FFFF F4 80..8F 80..BF 80..BF
7020  case 0xF4:
7021  {
7022  if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x8F, 0x80, 0xBF, 0x80, 0xBF}))))
7023  {
7024  return token_type::parse_error;
7025  }
7026  break;
7027  }
7028 
7029  // remaining bytes (80..C1 and F5..FF) are ill-formed
7030  default:
7031  {
7032  error_message = "invalid string: ill-formed UTF-8 byte";
7033  return token_type::parse_error;
7034  }
7035  }
7036  }
7037  }
7038 
7043  bool scan_comment()
7044  {
7045  switch (get())
7046  {
7047  // single-line comments skip input until a newline or EOF is read
7048  case '/':
7049  {
7050  while (true)
7051  {
7052  switch (get())
7053  {
7054  case '\n':
7055  case '\r':
7056  case std::char_traits<char_type>::eof():
7057  case '\0':
7058  return true;
7059 
7060  default:
7061  break;
7062  }
7063  }
7064  }
7065 
7066  // multi-line comments skip input until */ is read
7067  case '*':
7068  {
7069  while (true)
7070  {
7071  switch (get())
7072  {
7073  case std::char_traits<char_type>::eof():
7074  case '\0':
7075  {
7076  error_message = "invalid comment; missing closing '*/'";
7077  return false;
7078  }
7079 
7080  case '*':
7081  {
7082  switch (get())
7083  {
7084  case '/':
7085  return true;
7086 
7087  default:
7088  {
7089  unget();
7090  continue;
7091  }
7092  }
7093  }
7094 
7095  default:
7096  continue;
7097  }
7098  }
7099  }
7100 
7101  // unexpected character after reading '/'
7102  default:
7103  {
7104  error_message = "invalid comment; expecting '/' or '*' after '/'";
7105  return false;
7106  }
7107  }
7108  }
7109 
7110  JSON_HEDLEY_NON_NULL(2)
7111  static void strtof(float& f, const char* str, char** endptr) noexcept
7112  {
7113  f = std::strtof(str, endptr);
7114  }
7115 
7116  JSON_HEDLEY_NON_NULL(2)
7117  static void strtof(double& f, const char* str, char** endptr) noexcept
7118  {
7119  f = std::strtod(str, endptr);
7120  }
7121 
7122  JSON_HEDLEY_NON_NULL(2)
7123  static void strtof(long double& f, const char* str, char** endptr) noexcept
7124  {
7125  f = std::strtold(str, endptr);
7126  }
7127 
7168  token_type scan_number() // lgtm [cpp/use-of-goto]
7169  {
7170  // reset token_buffer to store the number's bytes
7171  reset();
7172 
7173  // the type of the parsed number; initially set to unsigned; will be
7174  // changed if minus sign, decimal point or exponent is read
7175  token_type number_type = token_type::value_unsigned;
7176 
7177  // state (init): we just found out we need to scan a number
7178  switch (current)
7179  {
7180  case '-':
7181  {
7182  add(current);
7183  goto scan_number_minus;
7184  }
7185 
7186  case '0':
7187  {
7188  add(current);
7189  goto scan_number_zero;
7190  }
7191 
7192  case '1':
7193  case '2':
7194  case '3':
7195  case '4':
7196  case '5':
7197  case '6':
7198  case '7':
7199  case '8':
7200  case '9':
7201  {
7202  add(current);
7203  goto scan_number_any1;
7204  }
7205 
7206  // all other characters are rejected outside scan_number()
7207  default: // LCOV_EXCL_LINE
7208  JSON_ASSERT(false); // LCOV_EXCL_LINE
7209  }
7210 
7211 scan_number_minus:
7212  // state: we just parsed a leading minus sign
7213  number_type = token_type::value_integer;
7214  switch (get())
7215  {
7216  case '0':
7217  {
7218  add(current);
7219  goto scan_number_zero;
7220  }
7221 
7222  case '1':
7223  case '2':
7224  case '3':
7225  case '4':
7226  case '5':
7227  case '6':
7228  case '7':
7229  case '8':
7230  case '9':
7231  {
7232  add(current);
7233  goto scan_number_any1;
7234  }
7235 
7236  default:
7237  {
7238  error_message = "invalid number; expected digit after '-'";
7239  return token_type::parse_error;
7240  }
7241  }
7242 
7243 scan_number_zero:
7244  // state: we just parse a zero (maybe with a leading minus sign)
7245  switch (get())
7246  {
7247  case '.':
7248  {
7249  add(decimal_point_char);
7250  goto scan_number_decimal1;
7251  }
7252 
7253  case 'e':
7254  case 'E':
7255  {
7256  add(current);
7257  goto scan_number_exponent;
7258  }
7259 
7260  default:
7261  goto scan_number_done;
7262  }
7263 
7264 scan_number_any1:
7265  // state: we just parsed a number 0-9 (maybe with a leading minus sign)
7266  switch (get())
7267  {
7268  case '0':
7269  case '1':
7270  case '2':
7271  case '3':
7272  case '4':
7273  case '5':
7274  case '6':
7275  case '7':
7276  case '8':
7277  case '9':
7278  {
7279  add(current);
7280  goto scan_number_any1;
7281  }
7282 
7283  case '.':
7284  {
7285  add(decimal_point_char);
7286  goto scan_number_decimal1;
7287  }
7288 
7289  case 'e':
7290  case 'E':
7291  {
7292  add(current);
7293  goto scan_number_exponent;
7294  }
7295 
7296  default:
7297  goto scan_number_done;
7298  }
7299 
7300 scan_number_decimal1:
7301  // state: we just parsed a decimal point
7302  number_type = token_type::value_float;
7303  switch (get())
7304  {
7305  case '0':
7306  case '1':
7307  case '2':
7308  case '3':
7309  case '4':
7310  case '5':
7311  case '6':
7312  case '7':
7313  case '8':
7314  case '9':
7315  {
7316  add(current);
7317  goto scan_number_decimal2;
7318  }
7319 
7320  default:
7321  {
7322  error_message = "invalid number; expected digit after '.'";
7323  return token_type::parse_error;
7324  }
7325  }
7326 
7327 scan_number_decimal2:
7328  // we just parsed at least one number after a decimal point
7329  switch (get())
7330  {
7331  case '0':
7332  case '1':
7333  case '2':
7334  case '3':
7335  case '4':
7336  case '5':
7337  case '6':
7338  case '7':
7339  case '8':
7340  case '9':
7341  {
7342  add(current);
7343  goto scan_number_decimal2;
7344  }
7345 
7346  case 'e':
7347  case 'E':
7348  {
7349  add(current);
7350  goto scan_number_exponent;
7351  }
7352 
7353  default:
7354  goto scan_number_done;
7355  }
7356 
7357 scan_number_exponent:
7358  // we just parsed an exponent
7359  number_type = token_type::value_float;
7360  switch (get())
7361  {
7362  case '+':
7363  case '-':
7364  {
7365  add(current);
7366  goto scan_number_sign;
7367  }
7368 
7369  case '0':
7370  case '1':
7371  case '2':
7372  case '3':
7373  case '4':
7374  case '5':
7375  case '6':
7376  case '7':
7377  case '8':
7378  case '9':
7379  {
7380  add(current);
7381  goto scan_number_any2;
7382  }
7383 
7384  default:
7385  {
7386  error_message =
7387  "invalid number; expected '+', '-', or digit after exponent";
7388  return token_type::parse_error;
7389  }
7390  }
7391 
7392 scan_number_sign:
7393  // we just parsed an exponent sign
7394  switch (get())
7395  {
7396  case '0':
7397  case '1':
7398  case '2':
7399  case '3':
7400  case '4':
7401  case '5':
7402  case '6':
7403  case '7':
7404  case '8':
7405  case '9':
7406  {
7407  add(current);
7408  goto scan_number_any2;
7409  }
7410 
7411  default:
7412  {
7413  error_message = "invalid number; expected digit after exponent sign";
7414  return token_type::parse_error;
7415  }
7416  }
7417 
7418 scan_number_any2:
7419  // we just parsed a number after the exponent or exponent sign
7420  switch (get())
7421  {
7422  case '0':
7423  case '1':
7424  case '2':
7425  case '3':
7426  case '4':
7427  case '5':
7428  case '6':
7429  case '7':
7430  case '8':
7431  case '9':
7432  {
7433  add(current);
7434  goto scan_number_any2;
7435  }
7436 
7437  default:
7438  goto scan_number_done;
7439  }
7440 
7441 scan_number_done:
7442  // unget the character after the number (we only read it to know that
7443  // we are done scanning a number)
7444  unget();
7445 
7446  char* endptr = nullptr;
7447  errno = 0;
7448 
7449  // try to parse integers first and fall back to floats
7450  if (number_type == token_type::value_unsigned)
7451  {
7452  const auto x = std::strtoull(token_buffer.data(), &endptr, 10);
7453 
7454  // we checked the number format before
7455  JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
7456 
7457  if (errno == 0)
7458  {
7459  value_unsigned = static_cast<number_unsigned_t>(x);
7460  if (value_unsigned == x)
7461  {
7462  return token_type::value_unsigned;
7463  }
7464  }
7465  }
7466  else if (number_type == token_type::value_integer)
7467  {
7468  const auto x = std::strtoll(token_buffer.data(), &endptr, 10);
7469 
7470  // we checked the number format before
7471  JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
7472 
7473  if (errno == 0)
7474  {
7475  value_integer = static_cast<number_integer_t>(x);
7476  if (value_integer == x)
7477  {
7478  return token_type::value_integer;
7479  }
7480  }
7481  }
7482 
7483  // this code is reached if we parse a floating-point number or if an
7484  // integer conversion above failed
7485  strtof(value_float, token_buffer.data(), &endptr);
7486 
7487  // we checked the number format before
7488  JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
7489 
7490  return token_type::value_float;
7491  }
7492 
7498  JSON_HEDLEY_NON_NULL(2)
7499  token_type scan_literal(const char_type* literal_text, const std::size_t length,
7500  token_type return_type)
7501  {
7502  JSON_ASSERT(std::char_traits<char_type>::to_char_type(current) == literal_text[0]);
7503  for (std::size_t i = 1; i < length; ++i)
7504  {
7505  if (JSON_HEDLEY_UNLIKELY(std::char_traits<char_type>::to_char_type(get()) != literal_text[i]))
7506  {
7507  error_message = "invalid literal";
7508  return token_type::parse_error;
7509  }
7510  }
7511  return return_type;
7512  }
7513 
7515  // input management
7517 
7519  void reset() noexcept
7520  {
7521  token_buffer.clear();
7522  token_string.clear();
7523  token_string.push_back(std::char_traits<char_type>::to_char_type(current));
7524  }
7525 
7526  /*
7527  @brief get next character from the input
7528 
7529  This function provides the interface to the used input adapter. It does
7530  not throw in case the input reached EOF, but returns a
7531  `std::char_traits<char>::eof()` in that case. Stores the scanned characters
7532  for use in error messages.
7533 
7534  @return character read from the input
7535  */
7536  char_int_type get()
7537  {
7538  ++position.chars_read_total;
7539  ++position.chars_read_current_line;
7540 
7541  if (next_unget)
7542  {
7543  // just reset the next_unget variable and work with current
7544  next_unget = false;
7545  }
7546  else
7547  {
7548  current = ia.get_character();
7549  }
7550 
7551  if (JSON_HEDLEY_LIKELY(current != std::char_traits<char_type>::eof()))
7552  {
7553  token_string.push_back(std::char_traits<char_type>::to_char_type(current));
7554  }
7555 
7556  if (current == '\n')
7557  {
7558  ++position.lines_read;
7559  position.chars_read_current_line = 0;
7560  }
7561 
7562  return current;
7563  }
7564 
7573  void unget()
7574  {
7575  next_unget = true;
7576 
7577  --position.chars_read_total;
7578 
7579  // in case we "unget" a newline, we have to also decrement the lines_read
7580  if (position.chars_read_current_line == 0)
7581  {
7582  if (position.lines_read > 0)
7583  {
7584  --position.lines_read;
7585  }
7586  }
7587  else
7588  {
7589  --position.chars_read_current_line;
7590  }
7591 
7592  if (JSON_HEDLEY_LIKELY(current != std::char_traits<char_type>::eof()))
7593  {
7594  JSON_ASSERT(!token_string.empty());
7595  token_string.pop_back();
7596  }
7597  }
7598 
7600  void add(char_int_type c)
7601  {
7602  token_buffer.push_back(static_cast<typename string_t::value_type>(c));
7603  }
7604 
7605  public:
7607  // value getters
7609 
7611  constexpr number_integer_t get_number_integer() const noexcept
7612  {
7613  return value_integer;
7614  }
7615 
7617  constexpr number_unsigned_t get_number_unsigned() const noexcept
7618  {
7619  return value_unsigned;
7620  }
7621 
7623  constexpr number_float_t get_number_float() const noexcept
7624  {
7625  return value_float;
7626  }
7627 
7629  string_t& get_string()
7630  {
7631  return token_buffer;
7632  }
7633 
7635  // diagnostics
7637 
7639  constexpr position_t get_position() const noexcept
7640  {
7641  return position;
7642  }
7643 
7647  std::string get_token_string() const
7648  {
7649  // escape control characters
7650  std::string result;
7651  for (const auto c : token_string)
7652  {
7653  if (static_cast<unsigned char>(c) <= '\x1F')
7654  {
7655  // escape control characters
7656  std::array<char, 9> cs{{}};
7657  (std::snprintf)(cs.data(), cs.size(), "<U+%.4X>", static_cast<unsigned char>(c));
7658  result += cs.data();
7659  }
7660  else
7661  {
7662  // add character as is
7663  result.push_back(static_cast<std::string::value_type>(c));
7664  }
7665  }
7666 
7667  return result;
7668  }
7669 
7671 
7672  constexpr const char* get_error_message() const noexcept
7673  {
7674  return error_message;
7675  }
7676 
7678  // actual scanner
7680 
7685  bool skip_bom()
7686  {
7687  if (get() == 0xEF)
7688  {
7689  // check if we completely parse the BOM
7690  return get() == 0xBB && get() == 0xBF;
7691  }
7692 
7693  // the first character is not the beginning of the BOM; unget it to
7694  // process is later
7695  unget();
7696  return true;
7697  }
7698 
7699  void skip_whitespace()
7700  {
7701  do
7702  {
7703  get();
7704  }
7705  while (current == ' ' || current == '\t' || current == '\n' || current == '\r');
7706  }
7707 
7708  token_type scan()
7709  {
7710  // initially, skip the BOM
7711  if (position.chars_read_total == 0 && !skip_bom())
7712  {
7713  error_message = "invalid BOM; must be 0xEF 0xBB 0xBF if given";
7714  return token_type::parse_error;
7715  }
7716 
7717  // read next character and ignore whitespace
7718  skip_whitespace();
7719 
7720  // ignore comments
7721  while (ignore_comments && current == '/')
7722  {
7723  if (!scan_comment())
7724  {
7725  return token_type::parse_error;
7726  }
7727 
7728  // skip following whitespace
7729  skip_whitespace();
7730  }
7731 
7732  switch (current)
7733  {
7734  // structural characters
7735  case '[':
7736  return token_type::begin_array;
7737  case ']':
7738  return token_type::end_array;
7739  case '{':
7740  return token_type::begin_object;
7741  case '}':
7742  return token_type::end_object;
7743  case ':':
7744  return token_type::name_separator;
7745  case ',':
7746  return token_type::value_separator;
7747 
7748  // literals
7749  case 't':
7750  {
7751  std::array<char_type, 4> true_literal = {{char_type('t'), char_type('r'), char_type('u'), char_type('e')}};
7752  return scan_literal(true_literal.data(), true_literal.size(), token_type::literal_true);
7753  }
7754  case 'f':
7755  {
7756  std::array<char_type, 5> false_literal = {{char_type('f'), char_type('a'), char_type('l'), char_type('s'), char_type('e')}};
7757  return scan_literal(false_literal.data(), false_literal.size(), token_type::literal_false);
7758  }
7759  case 'n':
7760  {
7761  std::array<char_type, 4> null_literal = {{char_type('n'), char_type('u'), char_type('l'), char_type('l')}};
7762  return scan_literal(null_literal.data(), null_literal.size(), token_type::literal_null);
7763  }
7764 
7765  // string
7766  case '\"':
7767  return scan_string();
7768 
7769  // number
7770  case '-':
7771  case '0':
7772  case '1':
7773  case '2':
7774  case '3':
7775  case '4':
7776  case '5':
7777  case '6':
7778  case '7':
7779  case '8':
7780  case '9':
7781  return scan_number();
7782 
7783  // end of input (the null byte is needed when parsing from
7784  // string literals)
7785  case '\0':
7786  case std::char_traits<char_type>::eof():
7787  return token_type::end_of_input;
7788 
7789  // error
7790  default:
7791  error_message = "invalid literal";
7792  return token_type::parse_error;
7793  }
7794  }
7795 
7796  private:
7798  InputAdapterType ia;
7799 
7801  const bool ignore_comments = false;
7802 
7804  char_int_type current = std::char_traits<char_type>::eof();
7805 
7807  bool next_unget = false;
7808 
7810  position_t position {};
7811 
7813  std::vector<char_type> token_string {};
7814 
7816  string_t token_buffer {};
7817 
7819  const char* error_message = "";
7820 
7821  // number values
7822  number_integer_t value_integer = 0;
7823  number_unsigned_t value_unsigned = 0;
7824  number_float_t value_float = 0;
7825 
7827  const char_int_type decimal_point_char = '.';
7828 };
7829 } // namespace detail
7830 } // namespace nlohmann
7831 
7832 // #include <nlohmann/detail/macro_scope.hpp>
7833 
7834 // #include <nlohmann/detail/meta/is_sax.hpp>
7835 
7836 
7837 #include <cstdint> // size_t
7838 #include <utility> // declval
7839 #include <string> // string
7840 
7841 // #include <nlohmann/detail/meta/detected.hpp>
7842 
7843 // #include <nlohmann/detail/meta/type_traits.hpp>
7844 
7845 
7846 namespace nlohmann
7847 {
7848 namespace detail
7849 {
7850 template<typename T>
7851 using null_function_t = decltype(std::declval<T&>().null());
7852 
7853 template<typename T>
7854 using boolean_function_t =
7855  decltype(std::declval<T&>().boolean(std::declval<bool>()));
7856 
7857 template<typename T, typename Integer>
7858 using number_integer_function_t =
7859  decltype(std::declval<T&>().number_integer(std::declval<Integer>()));
7860 
7861 template<typename T, typename Unsigned>
7862 using number_unsigned_function_t =
7863  decltype(std::declval<T&>().number_unsigned(std::declval<Unsigned>()));
7864 
7865 template<typename T, typename Float, typename String>
7866 using number_float_function_t = decltype(std::declval<T&>().number_float(
7867  std::declval<Float>(), std::declval<const String&>()));
7868 
7869 template<typename T, typename String>
7870 using string_function_t =
7871  decltype(std::declval<T&>().string(std::declval<String&>()));
7872 
7873 template<typename T, typename Binary>
7874 using binary_function_t =
7875  decltype(std::declval<T&>().binary(std::declval<Binary&>()));
7876 
7877 template<typename T>
7878 using start_object_function_t =
7879  decltype(std::declval<T&>().start_object(std::declval<std::size_t>()));
7880 
7881 template<typename T, typename String>
7882 using key_function_t =
7883  decltype(std::declval<T&>().key(std::declval<String&>()));
7884 
7885 template<typename T>
7886 using end_object_function_t = decltype(std::declval<T&>().end_object());
7887 
7888 template<typename T>
7889 using start_array_function_t =
7890  decltype(std::declval<T&>().start_array(std::declval<std::size_t>()));
7891 
7892 template<typename T>
7893 using end_array_function_t = decltype(std::declval<T&>().end_array());
7894 
7895 template<typename T, typename Exception>
7896 using parse_error_function_t = decltype(std::declval<T&>().parse_error(
7897  std::declval<std::size_t>(), std::declval<const std::string&>(),
7898  std::declval<const Exception&>()));
7899 
7900 template<typename SAX, typename BasicJsonType>
7901 struct is_sax
7902 {
7903  private:
7904  static_assert(is_basic_json<BasicJsonType>::value,
7905  "BasicJsonType must be of type basic_json<...>");
7906 
7907  using number_integer_t = typename BasicJsonType::number_integer_t;
7908  using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
7909  using number_float_t = typename BasicJsonType::number_float_t;
7910  using string_t = typename BasicJsonType::string_t;
7911  using binary_t = typename BasicJsonType::binary_t;
7912  using exception_t = typename BasicJsonType::exception;
7913 
7914  public:
7915  static constexpr bool value =
7916  is_detected_exact<bool, null_function_t, SAX>::value &&
7917  is_detected_exact<bool, boolean_function_t, SAX>::value &&
7918  is_detected_exact<bool, number_integer_function_t, SAX, number_integer_t>::value &&
7919  is_detected_exact<bool, number_unsigned_function_t, SAX, number_unsigned_t>::value &&
7920  is_detected_exact<bool, number_float_function_t, SAX, number_float_t, string_t>::value &&
7921  is_detected_exact<bool, string_function_t, SAX, string_t>::value &&
7922  is_detected_exact<bool, binary_function_t, SAX, binary_t>::value &&
7923  is_detected_exact<bool, start_object_function_t, SAX>::value &&
7924  is_detected_exact<bool, key_function_t, SAX, string_t>::value &&
7925  is_detected_exact<bool, end_object_function_t, SAX>::value &&
7926  is_detected_exact<bool, start_array_function_t, SAX>::value &&
7927  is_detected_exact<bool, end_array_function_t, SAX>::value &&
7928  is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value;
7929 };
7930 
7931 template<typename SAX, typename BasicJsonType>
7932 struct is_sax_static_asserts
7933 {
7934  private:
7935  static_assert(is_basic_json<BasicJsonType>::value,
7936  "BasicJsonType must be of type basic_json<...>");
7937 
7938  using number_integer_t = typename BasicJsonType::number_integer_t;
7939  using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
7940  using number_float_t = typename BasicJsonType::number_float_t;
7941  using string_t = typename BasicJsonType::string_t;
7942  using binary_t = typename BasicJsonType::binary_t;
7943  using exception_t = typename BasicJsonType::exception;
7944 
7945  public:
7946  static_assert(is_detected_exact<bool, null_function_t, SAX>::value,
7947  "Missing/invalid function: bool null()");
7948  static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
7949  "Missing/invalid function: bool boolean(bool)");
7950  static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
7951  "Missing/invalid function: bool boolean(bool)");
7952  static_assert(
7953  is_detected_exact<bool, number_integer_function_t, SAX,
7954  number_integer_t>::value,
7955  "Missing/invalid function: bool number_integer(number_integer_t)");
7956  static_assert(
7957  is_detected_exact<bool, number_unsigned_function_t, SAX,
7958  number_unsigned_t>::value,
7959  "Missing/invalid function: bool number_unsigned(number_unsigned_t)");
7960  static_assert(is_detected_exact<bool, number_float_function_t, SAX,
7961  number_float_t, string_t>::value,
7962  "Missing/invalid function: bool number_float(number_float_t, const string_t&)");
7963  static_assert(
7964  is_detected_exact<bool, string_function_t, SAX, string_t>::value,
7965  "Missing/invalid function: bool string(string_t&)");
7966  static_assert(
7967  is_detected_exact<bool, binary_function_t, SAX, binary_t>::value,
7968  "Missing/invalid function: bool binary(binary_t&)");
7969  static_assert(is_detected_exact<bool, start_object_function_t, SAX>::value,
7970  "Missing/invalid function: bool start_object(std::size_t)");
7971  static_assert(is_detected_exact<bool, key_function_t, SAX, string_t>::value,
7972  "Missing/invalid function: bool key(string_t&)");
7973  static_assert(is_detected_exact<bool, end_object_function_t, SAX>::value,
7974  "Missing/invalid function: bool end_object()");
7975  static_assert(is_detected_exact<bool, start_array_function_t, SAX>::value,
7976  "Missing/invalid function: bool start_array(std::size_t)");
7977  static_assert(is_detected_exact<bool, end_array_function_t, SAX>::value,
7978  "Missing/invalid function: bool end_array()");
7979  static_assert(
7980  is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value,
7981  "Missing/invalid function: bool parse_error(std::size_t, const "
7982  "std::string&, const exception&)");
7983 };
7984 } // namespace detail
7985 } // namespace nlohmann
7986 
7987 // #include <nlohmann/detail/value_t.hpp>
7988 
7989 
7990 namespace nlohmann
7991 {
7992 namespace detail
7993 {
7994 
7996 enum class cbor_tag_handler_t
7997 {
7998  error,
7999  ignore
8000 };
8001 
8009 static inline bool little_endianess(int num = 1) noexcept
8010 {
8011  return *reinterpret_cast<char*>(&num) == 1;
8012 }
8013 
8014 
8016 // binary reader //
8018 
8022 template<typename BasicJsonType, typename InputAdapterType, typename SAX = json_sax_dom_parser<BasicJsonType>>
8023 class binary_reader
8024 {
8025  using number_integer_t = typename BasicJsonType::number_integer_t;
8026  using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
8027  using number_float_t = typename BasicJsonType::number_float_t;
8028  using string_t = typename BasicJsonType::string_t;
8029  using binary_t = typename BasicJsonType::binary_t;
8030  using json_sax_t = SAX;
8031  using char_type = typename InputAdapterType::char_type;
8032  using char_int_type = typename std::char_traits<char_type>::int_type;
8033 
8034  public:
8040  explicit binary_reader(InputAdapterType&& adapter) : ia(std::move(adapter))
8041  {
8042  (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
8043  }
8044 
8045  // make class move-only
8046  binary_reader(const binary_reader&) = delete;
8047  binary_reader(binary_reader&&) = default;
8048  binary_reader& operator=(const binary_reader&) = delete;
8049  binary_reader& operator=(binary_reader&&) = default;
8050  ~binary_reader() = default;
8051 
8060  JSON_HEDLEY_NON_NULL(3)
8061  bool sax_parse(const input_format_t format,
8062  json_sax_t* sax_,
8063  const bool strict = true,
8064  const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
8065  {
8066  sax = sax_;
8067  bool result = false;
8068 
8069  switch (format)
8070  {
8071  case input_format_t::bson:
8072  result = parse_bson_internal();
8073  break;
8074 
8075  case input_format_t::cbor:
8076  result = parse_cbor_internal(true, tag_handler);
8077  break;
8078 
8079  case input_format_t::msgpack:
8080  result = parse_msgpack_internal();
8081  break;
8082 
8083  case input_format_t::ubjson:
8084  result = parse_ubjson_internal();
8085  break;
8086 
8087  default: // LCOV_EXCL_LINE
8088  JSON_ASSERT(false); // LCOV_EXCL_LINE
8089  }
8090 
8091  // strict mode: next byte must be EOF
8092  if (result && strict)
8093  {
8094  if (format == input_format_t::ubjson)
8095  {
8096  get_ignore_noop();
8097  }
8098  else
8099  {
8100  get();
8101  }
8102 
8103  if (JSON_HEDLEY_UNLIKELY(current != std::char_traits<char_type>::eof()))
8104  {
8105  return sax->parse_error(chars_read, get_token_string(),
8106  parse_error::create(110, chars_read, exception_message(format, "expected end of input; last byte: 0x" + get_token_string(), "value"), BasicJsonType()));
8107  }
8108  }
8109 
8110  return result;
8111  }
8112 
8113  private:
8115  // BSON //
8117 
8122  bool parse_bson_internal()
8123  {
8124  std::int32_t document_size{};
8125  get_number<std::int32_t, true>(input_format_t::bson, document_size);
8126 
8127  if (JSON_HEDLEY_UNLIKELY(!sax->start_object(std::size_t(-1))))
8128  {
8129  return false;
8130  }
8131 
8132  if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/false)))
8133  {
8134  return false;
8135  }
8136 
8137  return sax->end_object();
8138  }
8139 
8147  bool get_bson_cstr(string_t& result)
8148  {
8149  auto out = std::back_inserter(result);
8150  while (true)
8151  {
8152  get();
8153  if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "cstring")))
8154  {
8155  return false;
8156  }
8157  if (current == 0x00)
8158  {
8159  return true;
8160  }
8161  *out++ = static_cast<typename string_t::value_type>(current);
8162  }
8163  }
8164 
8176  template<typename NumberType>
8177  bool get_bson_string(const NumberType len, string_t& result)
8178  {
8179  if (JSON_HEDLEY_UNLIKELY(len < 1))
8180  {
8181  auto last_token = get_token_string();
8182  return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "string length must be at least 1, is " + std::to_string(len), "string"), BasicJsonType()));
8183  }
8184 
8185  return get_string(input_format_t::bson, len - static_cast<NumberType>(1), result) && get() != std::char_traits<char_type>::eof();
8186  }
8187 
8197  template<typename NumberType>
8198  bool get_bson_binary(const NumberType len, binary_t& result)
8199  {
8200  if (JSON_HEDLEY_UNLIKELY(len < 0))
8201  {
8202  auto last_token = get_token_string();
8203  return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "byte array length cannot be negative, is " + std::to_string(len), "binary"), BasicJsonType()));
8204  }
8205 
8206  // All BSON binary values have a subtype
8207  std::uint8_t subtype{};
8208  get_number<std::uint8_t>(input_format_t::bson, subtype);
8209  result.set_subtype(subtype);
8210 
8211  return get_binary(input_format_t::bson, len, result);
8212  }
8213 
8224  bool parse_bson_element_internal(const char_int_type element_type,
8225  const std::size_t element_type_parse_position)
8226  {
8227  switch (element_type)
8228  {
8229  case 0x01: // double
8230  {
8231  double number{};
8232  return get_number<double, true>(input_format_t::bson, number) && sax->number_float(static_cast<number_float_t>(number), "");
8233  }
8234 
8235  case 0x02: // string
8236  {
8237  std::int32_t len{};
8238  string_t value;
8239  return get_number<std::int32_t, true>(input_format_t::bson, len) && get_bson_string(len, value) && sax->string(value);
8240  }
8241 
8242  case 0x03: // object
8243  {
8244  return parse_bson_internal();
8245  }
8246 
8247  case 0x04: // array
8248  {
8249  return parse_bson_array();
8250  }
8251 
8252  case 0x05: // binary
8253  {
8254  std::int32_t len{};
8255  binary_t value;
8256  return get_number<std::int32_t, true>(input_format_t::bson, len) && get_bson_binary(len, value) && sax->binary(value);
8257  }
8258 
8259  case 0x08: // boolean
8260  {
8261  return sax->boolean(get() != 0);
8262  }
8263 
8264  case 0x0A: // null
8265  {
8266  return sax->null();
8267  }
8268 
8269  case 0x10: // int32
8270  {
8271  std::int32_t value{};
8272  return get_number<std::int32_t, true>(input_format_t::bson, value) && sax->number_integer(value);
8273  }
8274 
8275  case 0x12: // int64
8276  {
8277  std::int64_t value{};
8278  return get_number<std::int64_t, true>(input_format_t::bson, value) && sax->number_integer(value);
8279  }
8280 
8281  default: // anything else not supported (yet)
8282  {
8283  std::array<char, 3> cr{{}};
8284  (std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(element_type));
8285  return sax->parse_error(element_type_parse_position, std::string(cr.data()), parse_error::create(114, element_type_parse_position, "Unsupported BSON record type 0x" + std::string(cr.data()), BasicJsonType()));
8286  }
8287  }
8288  }
8289 
8302  bool parse_bson_element_list(const bool is_array)
8303  {
8304  string_t key;
8305 
8306  while (auto element_type = get())
8307  {
8308  if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "element list")))
8309  {
8310  return false;
8311  }
8312 
8313  const std::size_t element_type_parse_position = chars_read;
8314  if (JSON_HEDLEY_UNLIKELY(!get_bson_cstr(key)))
8315  {
8316  return false;
8317  }
8318 
8319  if (!is_array && !sax->key(key))
8320  {
8321  return false;
8322  }
8323 
8324  if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_internal(element_type, element_type_parse_position)))
8325  {
8326  return false;
8327  }
8328 
8329  // get_bson_cstr only appends
8330  key.clear();
8331  }
8332 
8333  return true;
8334  }
8335 
8340  bool parse_bson_array()
8341  {
8342  std::int32_t document_size{};
8343  get_number<std::int32_t, true>(input_format_t::bson, document_size);
8344 
8345  if (JSON_HEDLEY_UNLIKELY(!sax->start_array(std::size_t(-1))))
8346  {
8347  return false;
8348  }
8349 
8350  if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/true)))
8351  {
8352  return false;
8353  }
8354 
8355  return sax->end_array();
8356  }
8357 
8359  // CBOR //
8361 
8370  bool parse_cbor_internal(const bool get_char,
8371  const cbor_tag_handler_t tag_handler)
8372  {
8373  switch (get_char ? get() : current)
8374  {
8375  // EOF
8376  case std::char_traits<char_type>::eof():
8377  return unexpect_eof(input_format_t::cbor, "value");
8378 
8379  // Integer 0x00..0x17 (0..23)
8380  case 0x00:
8381  case 0x01:
8382  case 0x02:
8383  case 0x03:
8384  case 0x04:
8385  case 0x05:
8386  case 0x06:
8387  case 0x07:
8388  case 0x08:
8389  case 0x09:
8390  case 0x0A:
8391  case 0x0B:
8392  case 0x0C:
8393  case 0x0D:
8394  case 0x0E:
8395  case 0x0F:
8396  case 0x10:
8397  case 0x11:
8398  case 0x12:
8399  case 0x13:
8400  case 0x14:
8401  case 0x15:
8402  case 0x16:
8403  case 0x17:
8404  return sax->number_unsigned(static_cast<number_unsigned_t>(current));
8405 
8406  case 0x18: // Unsigned integer (one-byte uint8_t follows)
8407  {
8408  std::uint8_t number{};
8409  return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
8410  }
8411 
8412  case 0x19: // Unsigned integer (two-byte uint16_t follows)
8413  {
8414  std::uint16_t number{};
8415  return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
8416  }
8417 
8418  case 0x1A: // Unsigned integer (four-byte uint32_t follows)
8419  {
8420  std::uint32_t number{};
8421  return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
8422  }
8423 
8424  case 0x1B: // Unsigned integer (eight-byte uint64_t follows)
8425  {
8426  std::uint64_t number{};
8427  return get_number(input_format_t::cbor, number) && sax->number_unsigned(number);
8428  }
8429 
8430  // Negative integer -1-0x00..-1-0x17 (-1..-24)
8431  case 0x20:
8432  case 0x21:
8433  case 0x22:
8434  case 0x23:
8435  case 0x24:
8436  case 0x25:
8437  case 0x26:
8438  case 0x27:
8439  case 0x28:
8440  case 0x29:
8441  case 0x2A:
8442  case 0x2B:
8443  case 0x2C:
8444  case 0x2D:
8445  case 0x2E:
8446  case 0x2F:
8447  case 0x30:
8448  case 0x31:
8449  case 0x32:
8450  case 0x33:
8451  case 0x34:
8452  case 0x35:
8453  case 0x36:
8454  case 0x37:
8455  return sax->number_integer(static_cast<std::int8_t>(0x20 - 1 - current));
8456 
8457  case 0x38: // Negative integer (one-byte uint8_t follows)
8458  {
8459  std::uint8_t number{};
8460  return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
8461  }
8462 
8463  case 0x39: // Negative integer -1-n (two-byte uint16_t follows)
8464  {
8465  std::uint16_t number{};
8466  return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
8467  }
8468 
8469  case 0x3A: // Negative integer -1-n (four-byte uint32_t follows)
8470  {
8471  std::uint32_t number{};
8472  return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1) - number);
8473  }
8474 
8475  case 0x3B: // Negative integer -1-n (eight-byte uint64_t follows)
8476  {
8477  std::uint64_t number{};
8478  return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast<number_integer_t>(-1)
8479  - static_cast<number_integer_t>(number));
8480  }
8481 
8482  // Binary data (0x00..0x17 bytes follow)
8483  case 0x40:
8484  case 0x41:
8485  case 0x42:
8486  case 0x43:
8487  case 0x44:
8488  case 0x45:
8489  case 0x46:
8490  case 0x47:
8491  case 0x48:
8492  case 0x49:
8493  case 0x4A:
8494  case 0x4B:
8495  case 0x4C:
8496  case 0x4D:
8497  case 0x4E:
8498  case 0x4F:
8499  case 0x50:
8500  case 0x51:
8501  case 0x52:
8502  case 0x53:
8503  case 0x54:
8504  case 0x55:
8505  case 0x56:
8506  case 0x57:
8507  case 0x58: // Binary data (one-byte uint8_t for n follows)
8508  case 0x59: // Binary data (two-byte uint16_t for n follow)
8509  case 0x5A: // Binary data (four-byte uint32_t for n follow)
8510  case 0x5B: // Binary data (eight-byte uint64_t for n follow)
8511  case 0x5F: // Binary data (indefinite length)
8512  {
8513  binary_t b;
8514  return get_cbor_binary(b) && sax->binary(b);
8515  }
8516 
8517  // UTF-8 string (0x00..0x17 bytes follow)
8518  case 0x60:
8519  case 0x61:
8520  case 0x62:
8521  case 0x63:
8522  case 0x64:
8523  case 0x65:
8524  case 0x66:
8525  case 0x67:
8526  case 0x68:
8527  case 0x69:
8528  case 0x6A:
8529  case 0x6B:
8530  case 0x6C:
8531  case 0x6D:
8532  case 0x6E:
8533  case 0x6F:
8534  case 0x70:
8535  case 0x71:
8536  case 0x72:
8537  case 0x73:
8538  case 0x74:
8539  case 0x75:
8540  case 0x76:
8541  case 0x77:
8542  case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
8543  case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
8544  case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
8545  case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
8546  case 0x7F: // UTF-8 string (indefinite length)
8547  {
8548  string_t s;
8549  return get_cbor_string(s) && sax->string(s);
8550  }
8551 
8552  // array (0x00..0x17 data items follow)
8553  case 0x80:
8554  case 0x81:
8555  case 0x82:
8556  case 0x83:
8557  case 0x84:
8558  case 0x85:
8559  case 0x86:
8560  case 0x87:
8561  case 0x88:
8562  case 0x89:
8563  case 0x8A:
8564  case 0x8B:
8565  case 0x8C:
8566  case 0x8D:
8567  case 0x8E:
8568  case 0x8F:
8569  case 0x90:
8570  case 0x91:
8571  case 0x92:
8572  case 0x93:
8573  case 0x94:
8574  case 0x95:
8575  case 0x96:
8576  case 0x97:
8577  return get_cbor_array(static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
8578 
8579  case 0x98: // array (one-byte uint8_t for n follows)
8580  {
8581  std::uint8_t len{};
8582  return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
8583  }
8584 
8585  case 0x99: // array (two-byte uint16_t for n follow)
8586  {
8587  std::uint16_t len{};
8588  return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
8589  }
8590 
8591  case 0x9A: // array (four-byte uint32_t for n follow)
8592  {
8593  std::uint32_t len{};
8594  return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
8595  }
8596 
8597  case 0x9B: // array (eight-byte uint64_t for n follow)
8598  {
8599  std::uint64_t len{};
8600  return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast<std::size_t>(len), tag_handler);
8601  }
8602 
8603  case 0x9F: // array (indefinite length)
8604  return get_cbor_array(std::size_t(-1), tag_handler);
8605 
8606  // map (0x00..0x17 pairs of data items follow)
8607  case 0xA0:
8608  case 0xA1:
8609  case 0xA2:
8610  case 0xA3:
8611  case 0xA4:
8612  case 0xA5:
8613  case 0xA6:
8614  case 0xA7:
8615  case 0xA8:
8616  case 0xA9:
8617  case 0xAA:
8618  case 0xAB:
8619  case 0xAC:
8620  case 0xAD:
8621  case 0xAE:
8622  case 0xAF:
8623  case 0xB0:
8624  case 0xB1:
8625  case 0xB2:
8626  case 0xB3:
8627  case 0xB4:
8628  case 0xB5:
8629  case 0xB6:
8630  case 0xB7:
8631  return get_cbor_object(static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x1Fu), tag_handler);
8632 
8633  case 0xB8: // map (one-byte uint8_t for n follows)
8634  {
8635  std::uint8_t len{};
8636  return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
8637  }
8638 
8639  case 0xB9: // map (two-byte uint16_t for n follow)
8640  {
8641  std::uint16_t len{};
8642  return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
8643  }
8644 
8645  case 0xBA: // map (four-byte uint32_t for n follow)
8646  {
8647  std::uint32_t len{};
8648  return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
8649  }
8650 
8651  case 0xBB: // map (eight-byte uint64_t for n follow)
8652  {
8653  std::uint64_t len{};
8654  return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast<std::size_t>(len), tag_handler);
8655  }
8656 
8657  case 0xBF: // map (indefinite length)
8658  return get_cbor_object(std::size_t(-1), tag_handler);
8659 
8660  case 0xC6: // tagged item
8661  case 0xC7:
8662  case 0xC8:
8663  case 0xC9:
8664  case 0xCA:
8665  case 0xCB:
8666  case 0xCC:
8667  case 0xCD:
8668  case 0xCE:
8669  case 0xCF:
8670  case 0xD0:
8671  case 0xD1:
8672  case 0xD2:
8673  case 0xD3:
8674  case 0xD4:
8675  case 0xD8: // tagged item (1 bytes follow)
8676  case 0xD9: // tagged item (2 bytes follow)
8677  case 0xDA: // tagged item (4 bytes follow)
8678  case 0xDB: // tagged item (8 bytes follow)
8679  {
8680  switch (tag_handler)
8681  {
8682  case cbor_tag_handler_t::error:
8683  {
8684  auto last_token = get_token_string();
8685  return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"), BasicJsonType()));
8686  }
8687 
8688  case cbor_tag_handler_t::ignore:
8689  {
8690  switch (current)
8691  {
8692  case 0xD8:
8693  {
8694  std::uint8_t len{};
8695  get_number(input_format_t::cbor, len);
8696  break;
8697  }
8698  case 0xD9:
8699  {
8700  std::uint16_t len{};
8701  get_number(input_format_t::cbor, len);
8702  break;
8703  }
8704  case 0xDA:
8705  {
8706  std::uint32_t len{};
8707  get_number(input_format_t::cbor, len);
8708  break;
8709  }
8710  case 0xDB:
8711  {
8712  std::uint64_t len{};
8713  get_number(input_format_t::cbor, len);
8714  break;
8715  }
8716  default:
8717  break;
8718  }
8719  return parse_cbor_internal(true, tag_handler);
8720  }
8721 
8722  default: // LCOV_EXCL_LINE
8723  JSON_ASSERT(false); // LCOV_EXCL_LINE
8724  return false; // LCOV_EXCL_LINE
8725  }
8726  }
8727 
8728  case 0xF4: // false
8729  return sax->boolean(false);
8730 
8731  case 0xF5: // true
8732  return sax->boolean(true);
8733 
8734  case 0xF6: // null
8735  return sax->null();
8736 
8737  case 0xF9: // Half-Precision Float (two-byte IEEE 754)
8738  {
8739  const auto byte1_raw = get();
8740  if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
8741  {
8742  return false;
8743  }
8744  const auto byte2_raw = get();
8745  if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number")))
8746  {
8747  return false;
8748  }
8749 
8750  const auto byte1 = static_cast<unsigned char>(byte1_raw);
8751  const auto byte2 = static_cast<unsigned char>(byte2_raw);
8752 
8753  // code from RFC 7049, Appendix D, Figure 3:
8754  // As half-precision floating-point numbers were only added
8755  // to IEEE 754 in 2008, today's programming platforms often
8756  // still only have limited support for them. It is very
8757  // easy to include at least decoding support for them even
8758  // without such support. An example of a small decoder for
8759  // half-precision floating-point numbers in the C language
8760  // is shown in Fig. 3.
8761  const auto half = static_cast<unsigned int>((byte1 << 8u) + byte2);
8762  const double val = [&half]
8763  {
8764  const int exp = (half >> 10u) & 0x1Fu;
8765  const unsigned int mant = half & 0x3FFu;
8766  JSON_ASSERT(0 <= exp&& exp <= 32);
8767  JSON_ASSERT(mant <= 1024);
8768  switch (exp)
8769  {
8770  case 0:
8771  return std::ldexp(mant, -24);
8772  case 31:
8773  return (mant == 0)
8774  ? std::numeric_limits<double>::infinity()
8775  : std::numeric_limits<double>::quiet_NaN();
8776  default:
8777  return std::ldexp(mant + 1024, exp - 25);
8778  }
8779  }();
8780  return sax->number_float((half & 0x8000u) != 0
8781  ? static_cast<number_float_t>(-val)
8782  : static_cast<number_float_t>(val), "");
8783  }
8784 
8785  case 0xFA: // Single-Precision Float (four-byte IEEE 754)
8786  {
8787  float number{};
8788  return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
8789  }
8790 
8791  case 0xFB: // Double-Precision Float (eight-byte IEEE 754)
8792  {
8793  double number{};
8794  return get_number(input_format_t::cbor, number) && sax->number_float(static_cast<number_float_t>(number), "");
8795  }
8796 
8797  default: // anything else (0xFF is handled inside the other types)
8798  {
8799  auto last_token = get_token_string();
8800  return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"), BasicJsonType()));
8801  }
8802  }
8803  }
8804 
8816  bool get_cbor_string(string_t& result)
8817  {
8818  if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "string")))
8819  {
8820  return false;
8821  }
8822 
8823  switch (current)
8824  {
8825  // UTF-8 string (0x00..0x17 bytes follow)
8826  case 0x60:
8827  case 0x61:
8828  case 0x62:
8829  case 0x63:
8830  case 0x64:
8831  case 0x65:
8832  case 0x66:
8833  case 0x67:
8834  case 0x68:
8835  case 0x69:
8836  case 0x6A:
8837  case 0x6B:
8838  case 0x6C:
8839  case 0x6D:
8840  case 0x6E:
8841  case 0x6F:
8842  case 0x70:
8843  case 0x71:
8844  case 0x72:
8845  case 0x73:
8846  case 0x74:
8847  case 0x75:
8848  case 0x76:
8849  case 0x77:
8850  {
8851  return get_string(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
8852  }
8853 
8854  case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
8855  {
8856  std::uint8_t len{};
8857  return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
8858  }
8859 
8860  case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
8861  {
8862  std::uint16_t len{};
8863  return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
8864  }
8865 
8866  case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
8867  {
8868  std::uint32_t len{};
8869  return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
8870  }
8871 
8872  case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
8873  {
8874  std::uint64_t len{};
8875  return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
8876  }
8877 
8878  case 0x7F: // UTF-8 string (indefinite length)
8879  {
8880  while (get() != 0xFF)
8881  {
8882  string_t chunk;
8883  if (!get_cbor_string(chunk))
8884  {
8885  return false;
8886  }
8887  result.append(chunk);
8888  }
8889  return true;
8890  }
8891 
8892  default:
8893  {
8894  auto last_token = get_token_string();
8895  return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x" + last_token, "string"), BasicJsonType()));
8896  }
8897  }
8898  }
8899 
8911  bool get_cbor_binary(binary_t& result)
8912  {
8913  if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "binary")))
8914  {
8915  return false;
8916  }
8917 
8918  switch (current)
8919  {
8920  // Binary data (0x00..0x17 bytes follow)
8921  case 0x40:
8922  case 0x41:
8923  case 0x42:
8924  case 0x43:
8925  case 0x44:
8926  case 0x45:
8927  case 0x46:
8928  case 0x47:
8929  case 0x48:
8930  case 0x49:
8931  case 0x4A:
8932  case 0x4B:
8933  case 0x4C:
8934  case 0x4D:
8935  case 0x4E:
8936  case 0x4F:
8937  case 0x50:
8938  case 0x51:
8939  case 0x52:
8940  case 0x53:
8941  case 0x54:
8942  case 0x55:
8943  case 0x56:
8944  case 0x57:
8945  {
8946  return get_binary(input_format_t::cbor, static_cast<unsigned int>(current) & 0x1Fu, result);
8947  }
8948 
8949  case 0x58: // Binary data (one-byte uint8_t for n follows)
8950  {
8951  std::uint8_t len{};
8952  return get_number(input_format_t::cbor, len) &&
8953  get_binary(input_format_t::cbor, len, result);
8954  }
8955 
8956  case 0x59: // Binary data (two-byte uint16_t for n follow)
8957  {
8958  std::uint16_t len{};
8959  return get_number(input_format_t::cbor, len) &&
8960  get_binary(input_format_t::cbor, len, result);
8961  }
8962 
8963  case 0x5A: // Binary data (four-byte uint32_t for n follow)
8964  {
8965  std::uint32_t len{};
8966  return get_number(input_format_t::cbor, len) &&
8967  get_binary(input_format_t::cbor, len, result);
8968  }
8969 
8970  case 0x5B: // Binary data (eight-byte uint64_t for n follow)
8971  {
8972  std::uint64_t len{};
8973  return get_number(input_format_t::cbor, len) &&
8974  get_binary(input_format_t::cbor, len, result);
8975  }
8976 
8977  case 0x5F: // Binary data (indefinite length)
8978  {
8979  while (get() != 0xFF)
8980  {
8981  binary_t chunk;
8982  if (!get_cbor_binary(chunk))
8983  {
8984  return false;
8985  }
8986  result.insert(result.end(), chunk.begin(), chunk.end());
8987  }
8988  return true;
8989  }
8990 
8991  default:
8992  {
8993  auto last_token = get_token_string();
8994  return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x40-0x5B) or indefinite binary array type (0x5F); last byte: 0x" + last_token, "binary"), BasicJsonType()));
8995  }
8996  }
8997  }
8998 
9005  bool get_cbor_array(const std::size_t len,
9006  const cbor_tag_handler_t tag_handler)
9007  {
9008  if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
9009  {
9010  return false;
9011  }
9012 
9013  if (len != std::size_t(-1))
9014  {
9015  for (std::size_t i = 0; i < len; ++i)
9016  {
9017  if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
9018  {
9019  return false;
9020  }
9021  }
9022  }
9023  else
9024  {
9025  while (get() != 0xFF)
9026  {
9027  if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(false, tag_handler)))
9028  {
9029  return false;
9030  }
9031  }
9032  }
9033 
9034  return sax->end_array();
9035  }
9036 
9043  bool get_cbor_object(const std::size_t len,
9044  const cbor_tag_handler_t tag_handler)
9045  {
9046  if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
9047  {
9048  return false;
9049  }
9050 
9051  string_t key;
9052  if (len != std::size_t(-1))
9053  {
9054  for (std::size_t i = 0; i < len; ++i)
9055  {
9056  get();
9057  if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
9058  {
9059  return false;
9060  }
9061 
9062  if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
9063  {
9064  return false;
9065  }
9066  key.clear();
9067  }
9068  }
9069  else
9070  {
9071  while (get() != 0xFF)
9072  {
9073  if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
9074  {
9075  return false;
9076  }
9077 
9078  if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
9079  {
9080  return false;
9081  }
9082  key.clear();
9083  }
9084  }
9085 
9086  return sax->end_object();
9087  }
9088 
9090  // MsgPack //
9092 
9096  bool parse_msgpack_internal()
9097  {
9098  switch (get())
9099  {
9100  // EOF
9101  case std::char_traits<char_type>::eof():
9102  return unexpect_eof(input_format_t::msgpack, "value");
9103 
9104  // positive fixint
9105  case 0x00:
9106  case 0x01:
9107  case 0x02:
9108  case 0x03:
9109  case 0x04:
9110  case 0x05:
9111  case 0x06:
9112  case 0x07:
9113  case 0x08:
9114  case 0x09:
9115  case 0x0A:
9116  case 0x0B:
9117  case 0x0C:
9118  case 0x0D:
9119  case 0x0E:
9120  case 0x0F:
9121  case 0x10:
9122  case 0x11:
9123  case 0x12:
9124  case 0x13:
9125  case 0x14:
9126  case 0x15:
9127  case 0x16:
9128  case 0x17:
9129  case 0x18:
9130  case 0x19:
9131  case 0x1A:
9132  case 0x1B:
9133  case 0x1C:
9134  case 0x1D:
9135  case 0x1E:
9136  case 0x1F:
9137  case 0x20:
9138  case 0x21:
9139  case 0x22:
9140  case 0x23:
9141  case 0x24:
9142  case 0x25:
9143  case 0x26:
9144  case 0x27:
9145  case 0x28:
9146  case 0x29:
9147  case 0x2A:
9148  case 0x2B:
9149  case 0x2C:
9150  case 0x2D:
9151  case 0x2E:
9152  case 0x2F:
9153  case 0x30:
9154  case 0x31:
9155  case 0x32:
9156  case 0x33:
9157  case 0x34:
9158  case 0x35:
9159  case 0x36:
9160  case 0x37:
9161  case 0x38:
9162  case 0x39:
9163  case 0x3A:
9164  case 0x3B:
9165  case 0x3C:
9166  case 0x3D:
9167  case 0x3E:
9168  case 0x3F:
9169  case 0x40:
9170  case 0x41:
9171  case 0x42:
9172  case 0x43:
9173  case 0x44:
9174  case 0x45:
9175  case 0x46:
9176  case 0x47:
9177  case 0x48:
9178  case 0x49:
9179  case 0x4A:
9180  case 0x4B:
9181  case 0x4C:
9182  case 0x4D:
9183  case 0x4E:
9184  case 0x4F:
9185  case 0x50:
9186  case 0x51:
9187  case 0x52:
9188  case 0x53:
9189  case 0x54:
9190  case 0x55:
9191  case 0x56:
9192  case 0x57:
9193  case 0x58:
9194  case 0x59:
9195  case 0x5A:
9196  case 0x5B:
9197  case 0x5C:
9198  case 0x5D:
9199  case 0x5E:
9200  case 0x5F:
9201  case 0x60:
9202  case 0x61:
9203  case 0x62:
9204  case 0x63:
9205  case 0x64:
9206  case 0x65:
9207  case 0x66:
9208  case 0x67:
9209  case 0x68:
9210  case 0x69:
9211  case 0x6A:
9212  case 0x6B:
9213  case 0x6C:
9214  case 0x6D:
9215  case 0x6E:
9216  case 0x6F:
9217  case 0x70:
9218  case 0x71:
9219  case 0x72:
9220  case 0x73:
9221  case 0x74:
9222  case 0x75:
9223  case 0x76:
9224  case 0x77:
9225  case 0x78:
9226  case 0x79:
9227  case 0x7A:
9228  case 0x7B:
9229  case 0x7C:
9230  case 0x7D:
9231  case 0x7E:
9232  case 0x7F:
9233  return sax->number_unsigned(static_cast<number_unsigned_t>(current));
9234 
9235  // fixmap
9236  case 0x80:
9237  case 0x81:
9238  case 0x82:
9239  case 0x83:
9240  case 0x84:
9241  case 0x85:
9242  case 0x86:
9243  case 0x87:
9244  case 0x88:
9245  case 0x89:
9246  case 0x8A:
9247  case 0x8B:
9248  case 0x8C:
9249  case 0x8D:
9250  case 0x8E:
9251  case 0x8F:
9252  return get_msgpack_object(static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
9253 
9254  // fixarray
9255  case 0x90:
9256  case 0x91:
9257  case 0x92:
9258  case 0x93:
9259  case 0x94:
9260  case 0x95:
9261  case 0x96:
9262  case 0x97:
9263  case 0x98:
9264  case 0x99:
9265  case 0x9A:
9266  case 0x9B:
9267  case 0x9C:
9268  case 0x9D:
9269  case 0x9E:
9270  case 0x9F:
9271  return get_msgpack_array(static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
9272 
9273  // fixstr
9274  case 0xA0:
9275  case 0xA1:
9276  case 0xA2:
9277  case 0xA3:
9278  case 0xA4:
9279  case 0xA5:
9280  case 0xA6:
9281  case 0xA7:
9282  case 0xA8:
9283  case 0xA9:
9284  case 0xAA:
9285  case 0xAB:
9286  case 0xAC:
9287  case 0xAD:
9288  case 0xAE:
9289  case 0xAF:
9290  case 0xB0:
9291  case 0xB1:
9292  case 0xB2:
9293  case 0xB3:
9294  case 0xB4:
9295  case 0xB5:
9296  case 0xB6:
9297  case 0xB7:
9298  case 0xB8:
9299  case 0xB9:
9300  case 0xBA:
9301  case 0xBB:
9302  case 0xBC:
9303  case 0xBD:
9304  case 0xBE:
9305  case 0xBF:
9306  case 0xD9: // str 8
9307  case 0xDA: // str 16
9308  case 0xDB: // str 32
9309  {
9310  string_t s;
9311  return get_msgpack_string(s) && sax->string(s);
9312  }
9313 
9314  case 0xC0: // nil
9315  return sax->null();
9316 
9317  case 0xC2: // false
9318  return sax->boolean(false);
9319 
9320  case 0xC3: // true
9321  return sax->boolean(true);
9322 
9323  case 0xC4: // bin 8
9324  case 0xC5: // bin 16
9325  case 0xC6: // bin 32
9326  case 0xC7: // ext 8
9327  case 0xC8: // ext 16
9328  case 0xC9: // ext 32
9329  case 0xD4: // fixext 1
9330  case 0xD5: // fixext 2
9331  case 0xD6: // fixext 4
9332  case 0xD7: // fixext 8
9333  case 0xD8: // fixext 16
9334  {
9335  binary_t b;
9336  return get_msgpack_binary(b) && sax->binary(b);
9337  }
9338 
9339  case 0xCA: // float 32
9340  {
9341  float number{};
9342  return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast<number_float_t>(number), "");
9343  }
9344 
9345  case 0xCB: // float 64
9346  {
9347  double number{};
9348  return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast<number_float_t>(number), "");
9349  }
9350 
9351  case 0xCC: // uint 8
9352  {
9353  std::uint8_t number{};
9354  return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
9355  }
9356 
9357  case 0xCD: // uint 16
9358  {
9359  std::uint16_t number{};
9360  return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
9361  }
9362 
9363  case 0xCE: // uint 32
9364  {
9365  std::uint32_t number{};
9366  return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
9367  }
9368 
9369  case 0xCF: // uint 64
9370  {
9371  std::uint64_t number{};
9372  return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number);
9373  }
9374 
9375  case 0xD0: // int 8
9376  {
9377  std::int8_t number{};
9378  return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
9379  }
9380 
9381  case 0xD1: // int 16
9382  {
9383  std::int16_t number{};
9384  return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
9385  }
9386 
9387  case 0xD2: // int 32
9388  {
9389  std::int32_t number{};
9390  return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
9391  }
9392 
9393  case 0xD3: // int 64
9394  {
9395  std::int64_t number{};
9396  return get_number(input_format_t::msgpack, number) && sax->number_integer(number);
9397  }
9398 
9399  case 0xDC: // array 16
9400  {
9401  std::uint16_t len{};
9402  return get_number(input_format_t::msgpack, len) && get_msgpack_array(static_cast<std::size_t>(len));
9403  }
9404 
9405  case 0xDD: // array 32
9406  {
9407  std::uint32_t len{};
9408  return get_number(input_format_t::msgpack, len) && get_msgpack_array(static_cast<std::size_t>(len));
9409  }
9410 
9411  case 0xDE: // map 16
9412  {
9413  std::uint16_t len{};
9414  return get_number(input_format_t::msgpack, len) && get_msgpack_object(static_cast<std::size_t>(len));
9415  }
9416 
9417  case 0xDF: // map 32
9418  {
9419  std::uint32_t len{};
9420  return get_number(input_format_t::msgpack, len) && get_msgpack_object(static_cast<std::size_t>(len));
9421  }
9422 
9423  // negative fixint
9424  case 0xE0:
9425  case 0xE1:
9426  case 0xE2:
9427  case 0xE3:
9428  case 0xE4:
9429  case 0xE5:
9430  case 0xE6:
9431  case 0xE7:
9432  case 0xE8:
9433  case 0xE9:
9434  case 0xEA:
9435  case 0xEB:
9436  case 0xEC:
9437  case 0xED:
9438  case 0xEE:
9439  case 0xEF:
9440  case 0xF0:
9441  case 0xF1:
9442  case 0xF2:
9443  case 0xF3:
9444  case 0xF4:
9445  case 0xF5:
9446  case 0xF6:
9447  case 0xF7:
9448  case 0xF8:
9449  case 0xF9:
9450  case 0xFA:
9451  case 0xFB:
9452  case 0xFC:
9453  case 0xFD:
9454  case 0xFE:
9455  case 0xFF:
9456  return sax->number_integer(static_cast<std::int8_t>(current));
9457 
9458  default: // anything else
9459  {
9460  auto last_token = get_token_string();
9461  return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::msgpack, "invalid byte: 0x" + last_token, "value"), BasicJsonType()));
9462  }
9463  }
9464  }
9465 
9476  bool get_msgpack_string(string_t& result)
9477  {
9478  if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::msgpack, "string")))
9479  {
9480  return false;
9481  }
9482 
9483  switch (current)
9484  {
9485  // fixstr
9486  case 0xA0:
9487  case 0xA1:
9488  case 0xA2:
9489  case 0xA3:
9490  case 0xA4:
9491  case 0xA5:
9492  case 0xA6:
9493  case 0xA7:
9494  case 0xA8:
9495  case 0xA9:
9496  case 0xAA:
9497  case 0xAB:
9498  case 0xAC:
9499  case 0xAD:
9500  case 0xAE:
9501  case 0xAF:
9502  case 0xB0:
9503  case 0xB1:
9504  case 0xB2:
9505  case 0xB3:
9506  case 0xB4:
9507  case 0xB5:
9508  case 0xB6:
9509  case 0xB7:
9510  case 0xB8:
9511  case 0xB9:
9512  case 0xBA:
9513  case 0xBB:
9514  case 0xBC:
9515  case 0xBD:
9516  case 0xBE:
9517  case 0xBF:
9518  {
9519  return get_string(input_format_t::msgpack, static_cast<unsigned int>(current) & 0x1Fu, result);
9520  }
9521 
9522  case 0xD9: // str 8
9523  {
9524  std::uint8_t len{};
9525  return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
9526  }
9527 
9528  case 0xDA: // str 16
9529  {
9530  std::uint16_t len{};
9531  return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
9532  }
9533 
9534  case 0xDB: // str 32
9535  {
9536  std::uint32_t len{};
9537  return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result);
9538  }
9539 
9540  default:
9541  {
9542  auto last_token = get_token_string();
9543  return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::msgpack, "expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x" + last_token, "string"), BasicJsonType()));
9544  }
9545  }
9546  }
9547 
9558  bool get_msgpack_binary(binary_t& result)
9559  {
9560  // helper function to set the subtype
9561  auto assign_and_return_true = [&result](std::int8_t subtype)
9562  {
9563  result.set_subtype(static_cast<std::uint8_t>(subtype));
9564  return true;
9565  };
9566 
9567  switch (current)
9568  {
9569  case 0xC4: // bin 8
9570  {
9571  std::uint8_t len{};
9572  return get_number(input_format_t::msgpack, len) &&
9573  get_binary(input_format_t::msgpack, len, result);
9574  }
9575 
9576  case 0xC5: // bin 16
9577  {
9578  std::uint16_t len{};
9579  return get_number(input_format_t::msgpack, len) &&
9580  get_binary(input_format_t::msgpack, len, result);
9581  }
9582 
9583  case 0xC6: // bin 32
9584  {
9585  std::uint32_t len{};
9586  return get_number(input_format_t::msgpack, len) &&
9587  get_binary(input_format_t::msgpack, len, result);
9588  }
9589 
9590  case 0xC7: // ext 8
9591  {
9592  std::uint8_t len{};
9593  std::int8_t subtype{};
9594  return get_number(input_format_t::msgpack, len) &&
9595  get_number(input_format_t::msgpack, subtype) &&
9596  get_binary(input_format_t::msgpack, len, result) &&
9597  assign_and_return_true(subtype);
9598  }
9599 
9600  case 0xC8: // ext 16
9601  {
9602  std::uint16_t len{};
9603  std::int8_t subtype{};
9604  return get_number(input_format_t::msgpack, len) &&
9605  get_number(input_format_t::msgpack, subtype) &&
9606  get_binary(input_format_t::msgpack, len, result) &&
9607  assign_and_return_true(subtype);
9608  }
9609 
9610  case 0xC9: // ext 32
9611  {
9612  std::uint32_t len{};
9613  std::int8_t subtype{};
9614  return get_number(input_format_t::msgpack, len) &&
9615  get_number(input_format_t::msgpack, subtype) &&
9616  get_binary(input_format_t::msgpack, len, result) &&
9617  assign_and_return_true(subtype);
9618  }
9619 
9620  case 0xD4: // fixext 1
9621  {
9622  std::int8_t subtype{};
9623  return get_number(input_format_t::msgpack, subtype) &&
9624  get_binary(input_format_t::msgpack, 1, result) &&
9625  assign_and_return_true(subtype);
9626  }
9627 
9628  case 0xD5: // fixext 2
9629  {
9630  std::int8_t subtype{};
9631  return get_number(input_format_t::msgpack, subtype) &&
9632  get_binary(input_format_t::msgpack, 2, result) &&
9633  assign_and_return_true(subtype);
9634  }
9635 
9636  case 0xD6: // fixext 4
9637  {
9638  std::int8_t subtype{};
9639  return get_number(input_format_t::msgpack, subtype) &&
9640  get_binary(input_format_t::msgpack, 4, result) &&
9641  assign_and_return_true(subtype);
9642  }
9643 
9644  case 0xD7: // fixext 8
9645  {
9646  std::int8_t subtype{};
9647  return get_number(input_format_t::msgpack, subtype) &&
9648  get_binary(input_format_t::msgpack, 8, result) &&
9649  assign_and_return_true(subtype);
9650  }
9651 
9652  case 0xD8: // fixext 16
9653  {
9654  std::int8_t subtype{};
9655  return get_number(input_format_t::msgpack, subtype) &&
9656  get_binary(input_format_t::msgpack, 16, result) &&
9657  assign_and_return_true(subtype);
9658  }
9659 
9660  default: // LCOV_EXCL_LINE
9661  return false; // LCOV_EXCL_LINE
9662  }
9663  }
9664 
9669  bool get_msgpack_array(const std::size_t len)
9670  {
9671  if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
9672  {
9673  return false;
9674  }
9675 
9676  for (std::size_t i = 0; i < len; ++i)
9677  {
9678  if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
9679  {
9680  return false;
9681  }
9682  }
9683 
9684  return sax->end_array();
9685  }
9686 
9691  bool get_msgpack_object(const std::size_t len)
9692  {
9693  if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
9694  {
9695  return false;
9696  }
9697 
9698  string_t key;
9699  for (std::size_t i = 0; i < len; ++i)
9700  {
9701  get();
9702  if (JSON_HEDLEY_UNLIKELY(!get_msgpack_string(key) || !sax->key(key)))
9703  {
9704  return false;
9705  }
9706 
9707  if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
9708  {
9709  return false;
9710  }
9711  key.clear();
9712  }
9713 
9714  return sax->end_object();
9715  }
9716 
9718  // UBJSON //
9720 
9728  bool parse_ubjson_internal(const bool get_char = true)
9729  {
9730  return get_ubjson_value(get_char ? get_ignore_noop() : current);
9731  }
9732 
9747  bool get_ubjson_string(string_t& result, const bool get_char = true)
9748  {
9749  if (get_char)
9750  {
9751  get(); // TODO(niels): may we ignore N here?
9752  }
9753 
9754  if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::ubjson, "value")))
9755  {
9756  return false;
9757  }
9758 
9759  switch (current)
9760  {
9761  case 'U':
9762  {
9763  std::uint8_t len{};
9764  return get_number(input_format_t::ubjson, len) && get_string(input_format_t::ubjson, len, result);
9765  }
9766 
9767  case 'i':
9768  {
9769  std::int8_t len{};
9770  return get_number(input_format_t::ubjson, len) && get_string(input_format_t::ubjson, len, result);
9771  }
9772 
9773  case 'I':
9774  {
9775  std::int16_t len{};
9776  return get_number(input_format_t::ubjson, len) && get_string(input_format_t::ubjson, len, result);
9777  }
9778 
9779  case 'l':
9780  {
9781  std::int32_t len{};
9782  return get_number(input_format_t::ubjson, len) && get_string(input_format_t::ubjson, len, result);
9783  }
9784 
9785  case 'L':
9786  {
9787  std::int64_t len{};
9788  return get_number(input_format_t::ubjson, len) && get_string(input_format_t::ubjson, len, result);
9789  }
9790 
9791  default:
9792  auto last_token = get_token_string();
9793  return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token, "string"), BasicJsonType()));
9794  }
9795  }
9796 
9801  bool get_ubjson_size_value(std::size_t& result)
9802  {
9803  switch (get_ignore_noop())
9804  {
9805  case 'U':
9806  {
9807  std::uint8_t number{};
9808  if (JSON_HEDLEY_UNLIKELY(!get_number(input_format_t::ubjson, number)))
9809  {
9810  return false;
9811  }
9812  result = static_cast<std::size_t>(number);
9813  return true;
9814  }
9815 
9816  case 'i':
9817  {
9818  std::int8_t number{};
9819  if (JSON_HEDLEY_UNLIKELY(!get_number(input_format_t::ubjson, number)))
9820  {
9821  return false;
9822  }
9823  result = static_cast<std::size_t>(number);
9824  return true;
9825  }
9826 
9827  case 'I':
9828  {
9829  std::int16_t number{};
9830  if (JSON_HEDLEY_UNLIKELY(!get_number(input_format_t::ubjson, number)))
9831  {
9832  return false;
9833  }
9834  result = static_cast<std::size_t>(number);
9835  return true;
9836  }
9837 
9838  case 'l':
9839  {
9840  std::int32_t number{};
9841  if (JSON_HEDLEY_UNLIKELY(!get_number(input_format_t::ubjson, number)))
9842  {
9843  return false;
9844  }
9845  result = static_cast<std::size_t>(number);
9846  return true;
9847  }
9848 
9849  case 'L':
9850  {
9851  std::int64_t number{};
9852  if (JSON_HEDLEY_UNLIKELY(!get_number(input_format_t::ubjson, number)))
9853  {
9854  return false;
9855  }
9856  result = static_cast<std::size_t>(number);
9857  return true;
9858  }
9859 
9860  default:
9861  {
9862  auto last_token = get_token_string();
9863  return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token, "size"), BasicJsonType()));
9864  }
9865  }
9866  }
9867 
9878  bool get_ubjson_size_type(std::pair<std::size_t, char_int_type>& result)
9879  {
9880  result.first = string_t::npos; // size
9881  result.second = 0; // type
9882 
9883  get_ignore_noop();
9884 
9885  if (current == '$')
9886  {
9887  result.second = get(); // must not ignore 'N', because 'N' maybe the type
9888  if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::ubjson, "type")))
9889  {
9890  return false;
9891  }
9892 
9893  get_ignore_noop();
9894  if (JSON_HEDLEY_UNLIKELY(current != '#'))
9895  {
9896  if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::ubjson, "value")))
9897  {
9898  return false;
9899  }
9900  auto last_token = get_token_string();
9901  return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "expected '#' after type information; last byte: 0x" + last_token, "size"), BasicJsonType()));
9902  }
9903 
9904  return get_ubjson_size_value(result.first);
9905  }
9906 
9907  if (current == '#')
9908  {
9909  return get_ubjson_size_value(result.first);
9910  }
9911 
9912  return true;
9913  }
9914 
9919  bool get_ubjson_value(const char_int_type prefix)
9920  {
9921  switch (prefix)
9922  {
9923  case std::char_traits<char_type>::eof(): // EOF
9924  return unexpect_eof(input_format_t::ubjson, "value");
9925 
9926  case 'T': // true
9927  return sax->boolean(true);
9928  case 'F': // false
9929  return sax->boolean(false);
9930 
9931  case 'Z': // null
9932  return sax->null();
9933 
9934  case 'U':
9935  {
9936  std::uint8_t number{};
9937  return get_number(input_format_t::ubjson, number) && sax->number_unsigned(number);
9938  }
9939 
9940  case 'i':
9941  {
9942  std::int8_t number{};
9943  return get_number(input_format_t::ubjson, number) && sax->number_integer(number);
9944  }
9945 
9946  case 'I':
9947  {
9948  std::int16_t number{};
9949  return get_number(input_format_t::ubjson, number) && sax->number_integer(number);
9950  }
9951 
9952  case 'l':
9953  {
9954  std::int32_t number{};
9955  return get_number(input_format_t::ubjson, number) && sax->number_integer(number);
9956  }
9957 
9958  case 'L':
9959  {
9960  std::int64_t number{};
9961  return get_number(input_format_t::ubjson, number) && sax->number_integer(number);
9962  }
9963 
9964  case 'd':
9965  {
9966  float number{};
9967  return get_number(input_format_t::ubjson, number) && sax->number_float(static_cast<number_float_t>(number), "");
9968  }
9969 
9970  case 'D':
9971  {
9972  double number{};
9973  return get_number(input_format_t::ubjson, number) && sax->number_float(static_cast<number_float_t>(number), "");
9974  }
9975 
9976  case 'H':
9977  {
9978  return get_ubjson_high_precision_number();
9979  }
9980 
9981  case 'C': // char
9982  {
9983  get();
9984  if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::ubjson, "char")))
9985  {
9986  return false;
9987  }
9988  if (JSON_HEDLEY_UNLIKELY(current > 127))
9989  {
9990  auto last_token = get_token_string();
9991  return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "byte after 'C' must be in range 0x00..0x7F; last byte: 0x" + last_token, "char"), BasicJsonType()));
9992  }
9993  string_t s(1, static_cast<typename string_t::value_type>(current));
9994  return sax->string(s);
9995  }
9996 
9997  case 'S': // string
9998  {
9999  string_t s;
10000  return get_ubjson_string(s) && sax->string(s);
10001  }
10002 
10003  case '[': // array
10004  return get_ubjson_array();
10005 
10006  case '{': // object
10007  return get_ubjson_object();
10008 
10009  default: // anything else
10010  {
10011  auto last_token = get_token_string();
10012  return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "invalid byte: 0x" + last_token, "value"), BasicJsonType()));
10013  }
10014  }
10015  }
10016 
10020  bool get_ubjson_array()
10021  {
10022  std::pair<std::size_t, char_int_type> size_and_type;
10023  if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type)))
10024  {
10025  return false;
10026  }
10027 
10028  if (size_and_type.first != string_t::npos)
10029  {
10030  if (JSON_HEDLEY_UNLIKELY(!sax->start_array(size_and_type.first)))
10031  {
10032  return false;
10033  }
10034 
10035  if (size_and_type.second != 0)
10036  {
10037  if (size_and_type.second != 'N')
10038  {
10039  for (std::size_t i = 0; i < size_and_type.first; ++i)
10040  {
10041  if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
10042  {
10043  return false;
10044  }
10045  }
10046  }
10047  }
10048  else
10049  {
10050  for (std::size_t i = 0; i < size_and_type.first; ++i)
10051  {
10052  if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
10053  {
10054  return false;
10055  }
10056  }
10057  }
10058  }
10059  else
10060  {
10061  if (JSON_HEDLEY_UNLIKELY(!sax->start_array(std::size_t(-1))))
10062  {
10063  return false;
10064  }
10065 
10066  while (current != ']')
10067  {
10068  if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal(false)))
10069  {
10070  return false;
10071  }
10072  get_ignore_noop();
10073  }
10074  }
10075 
10076  return sax->end_array();
10077  }
10078 
10082  bool get_ubjson_object()
10083  {
10084  std::pair<std::size_t, char_int_type> size_and_type;
10085  if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type)))
10086  {
10087  return false;
10088  }
10089 
10090  string_t key;
10091  if (size_and_type.first != string_t::npos)
10092  {
10093  if (JSON_HEDLEY_UNLIKELY(!sax->start_object(size_and_type.first)))
10094  {
10095  return false;
10096  }
10097 
10098  if (size_and_type.second != 0)
10099  {
10100  for (std::size_t i = 0; i < size_and_type.first; ++i)
10101  {
10102  if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
10103  {
10104  return false;
10105  }
10106  if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second)))
10107  {
10108  return false;
10109  }
10110  key.clear();
10111  }
10112  }
10113  else
10114  {
10115  for (std::size_t i = 0; i < size_and_type.first; ++i)
10116  {
10117  if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key)))
10118  {
10119  return false;
10120  }
10121  if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
10122  {
10123  return false;
10124  }
10125  key.clear();
10126  }
10127  }
10128  }
10129  else
10130  {
10131  if (JSON_HEDLEY_UNLIKELY(!sax->start_object(std::size_t(-1))))
10132  {
10133  return false;
10134  }
10135 
10136  while (current != '}')
10137  {
10138  if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key, false) || !sax->key(key)))
10139  {
10140  return false;
10141  }
10142  if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal()))
10143  {
10144  return false;
10145  }
10146  get_ignore_noop();
10147  key.clear();
10148  }
10149  }
10150 
10151  return sax->end_object();
10152  }
10153 
10154  // Note, no reader for UBJSON binary types is implemented because they do
10155  // not exist
10156 
10157  bool get_ubjson_high_precision_number()
10158  {
10159  // get size of following number string
10160  std::size_t size{};
10161  auto res = get_ubjson_size_value(size);
10162  if (JSON_HEDLEY_UNLIKELY(!res))
10163  {
10164  return res;
10165  }
10166 
10167  // get number string
10168  std::vector<char> number_vector;
10169  for (std::size_t i = 0; i < size; ++i)
10170  {
10171  get();
10172  if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::ubjson, "number")))
10173  {
10174  return false;
10175  }
10176  number_vector.push_back(static_cast<char>(current));
10177  }
10178 
10179  // parse number string
10180  auto number_ia = detail::input_adapter(std::forward<decltype(number_vector)>(number_vector));
10181  auto number_lexer = detail::lexer<BasicJsonType, decltype(number_ia)>(std::move(number_ia), false);
10182  const auto result_number = number_lexer.scan();
10183  const auto number_string = number_lexer.get_token_string();
10184  const auto result_remainder = number_lexer.scan();
10185 
10186  using token_type = typename detail::lexer_base<BasicJsonType>::token_type;
10187 
10188  if (JSON_HEDLEY_UNLIKELY(result_remainder != token_type::end_of_input))
10189  {
10190  return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"), BasicJsonType()));
10191  }
10192 
10193  switch (result_number)
10194  {
10195  case token_type::value_integer:
10196  return sax->number_integer(number_lexer.get_number_integer());
10197  case token_type::value_unsigned:
10198  return sax->number_unsigned(number_lexer.get_number_unsigned());
10199  case token_type::value_float:
10200  return sax->number_float(number_lexer.get_number_float(), std::move(number_string));
10201  default:
10202  return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"), BasicJsonType()));
10203  }
10204  }
10205 
10207  // Utility functions //
10209 
10219  char_int_type get()
10220  {
10221  ++chars_read;
10222  return current = ia.get_character();
10223  }
10224 
10228  char_int_type get_ignore_noop()
10229  {
10230  do
10231  {
10232  get();
10233  }
10234  while (current == 'N');
10235 
10236  return current;
10237  }
10238 
10239  /*
10240  @brief read a number from the input
10241 
10242  @tparam NumberType the type of the number
10243  @param[in] format the current format (for diagnostics)
10244  @param[out] result number of type @a NumberType
10245 
10246  @return whether conversion completed
10247 
10248  @note This function needs to respect the system's endianess, because
10249  bytes in CBOR, MessagePack, and UBJSON are stored in network order
10250  (big endian) and therefore need reordering on little endian systems.
10251  */
10252  template<typename NumberType, bool InputIsLittleEndian = false>
10253  bool get_number(const input_format_t format, NumberType& result)
10254  {
10255  // step 1: read input into array with system's byte order
10256  std::array<std::uint8_t, sizeof(NumberType)> vec;
10257  for (std::size_t i = 0; i < sizeof(NumberType); ++i)
10258  {
10259  get();
10260  if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "number")))
10261  {
10262  return false;
10263  }
10264 
10265  // reverse byte order prior to conversion if necessary
10266  if (is_little_endian != InputIsLittleEndian)
10267  {
10268  vec[sizeof(NumberType) - i - 1] = static_cast<std::uint8_t>(current);
10269  }
10270  else
10271  {
10272  vec[i] = static_cast<std::uint8_t>(current); // LCOV_EXCL_LINE
10273  }
10274  }
10275 
10276  // step 2: convert array into number of type T and return
10277  std::memcpy(&result, vec.data(), sizeof(NumberType));
10278  return true;
10279  }
10280 
10295  template<typename NumberType>
10296  bool get_string(const input_format_t format,
10297  const NumberType len,
10298  string_t& result)
10299  {
10300  bool success = true;
10301  for (NumberType i = 0; i < len; i++)
10302  {
10303  get();
10304  if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "string")))
10305  {
10306  success = false;
10307  break;
10308  }
10309  result.push_back(static_cast<typename string_t::value_type>(current));
10310  }
10311  return success;
10312  }
10313 
10328  template<typename NumberType>
10329  bool get_binary(const input_format_t format,
10330  const NumberType len,
10331  binary_t& result)
10332  {
10333  bool success = true;
10334  for (NumberType i = 0; i < len; i++)
10335  {
10336  get();
10337  if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "binary")))
10338  {
10339  success = false;
10340  break;
10341  }
10342  result.push_back(static_cast<std::uint8_t>(current));
10343  }
10344  return success;
10345  }
10346 
10352  JSON_HEDLEY_NON_NULL(3)
10353  bool unexpect_eof(const input_format_t format, const char* context) const
10354  {
10355  if (JSON_HEDLEY_UNLIKELY(current == std::char_traits<char_type>::eof()))
10356  {
10357  return sax->parse_error(chars_read, "<end of file>",
10358  parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), BasicJsonType()));
10359  }
10360  return true;
10361  }
10362 
10366  std::string get_token_string() const
10367  {
10368  std::array<char, 3> cr{{}};
10369  (std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast<unsigned char>(current));
10370  return std::string{cr.data()};
10371  }
10372 
10379  std::string exception_message(const input_format_t format,
10380  const std::string& detail,
10381  const std::string& context) const
10382  {
10383  std::string error_msg = "syntax error while parsing ";
10384 
10385  switch (format)
10386  {
10387  case input_format_t::cbor:
10388  error_msg += "CBOR";
10389  break;
10390 
10391  case input_format_t::msgpack:
10392  error_msg += "MessagePack";
10393  break;
10394 
10395  case input_format_t::ubjson:
10396  error_msg += "UBJSON";
10397  break;
10398 
10399  case input_format_t::bson:
10400  error_msg += "BSON";
10401  break;
10402 
10403  default: // LCOV_EXCL_LINE
10404  JSON_ASSERT(false); // LCOV_EXCL_LINE
10405  }
10406 
10407  return error_msg + " " + context + ": " + detail;
10408  }
10409 
10410  private:
10412  InputAdapterType ia;
10413 
10415  char_int_type current = std::char_traits<char_type>::eof();
10416 
10418  std::size_t chars_read = 0;
10419 
10421  const bool is_little_endian = little_endianess();
10422 
10424  json_sax_t* sax = nullptr;
10425 };
10426 } // namespace detail
10427 } // namespace nlohmann
10428 
10429 // #include <nlohmann/detail/input/input_adapters.hpp>
10430 
10431 // #include <nlohmann/detail/input/lexer.hpp>
10432 
10433 // #include <nlohmann/detail/input/parser.hpp>
10434 
10435 
10436 #include <cmath> // isfinite
10437 #include <cstdint> // uint8_t
10438 #include <functional> // function
10439 #include <string> // string
10440 #include <utility> // move
10441 #include <vector> // vector
10442 
10443 // #include <nlohmann/detail/exceptions.hpp>
10444 
10445 // #include <nlohmann/detail/input/input_adapters.hpp>
10446 
10447 // #include <nlohmann/detail/input/json_sax.hpp>
10448 
10449 // #include <nlohmann/detail/input/lexer.hpp>
10450 
10451 // #include <nlohmann/detail/macro_scope.hpp>
10452 
10453 // #include <nlohmann/detail/meta/is_sax.hpp>
10454 
10455 // #include <nlohmann/detail/value_t.hpp>
10456 
10457 
10458 namespace nlohmann
10459 {
10460 namespace detail
10461 {
10463 // parser //
10465 
10466 enum class parse_event_t : uint8_t
10467 {
10469  object_start,
10471  object_end,
10473  array_start,
10475  array_end,
10477  key,
10479  value
10480 };
10481 
10482 template<typename BasicJsonType>
10483 using parser_callback_t =
10484  std::function<bool(int depth, parse_event_t event, BasicJsonType& parsed)>;
10485 
10491 template<typename BasicJsonType, typename InputAdapterType>
10492 class parser
10493 {
10494  using number_integer_t = typename BasicJsonType::number_integer_t;
10495  using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
10496  using number_float_t = typename BasicJsonType::number_float_t;
10497  using string_t = typename BasicJsonType::string_t;
10498  using lexer_t = lexer<BasicJsonType, InputAdapterType>;
10499  using token_type = typename lexer_t::token_type;
10500 
10501  public:
10503  explicit parser(InputAdapterType&& adapter,
10504  const parser_callback_t<BasicJsonType> cb = nullptr,
10505  const bool allow_exceptions_ = true,
10506  const bool skip_comments = false)
10507  : callback(cb)
10508  , m_lexer(std::move(adapter), skip_comments)
10509  , allow_exceptions(allow_exceptions_)
10510  {
10511  // read first token
10512  get_token();
10513  }
10514 
10525  void parse(const bool strict, BasicJsonType& result)
10526  {
10527  if (callback)
10528  {
10529  json_sax_dom_callback_parser<BasicJsonType> sdp(result, callback, allow_exceptions);
10530  sax_parse_internal(&sdp);
10531 
10532  // in strict mode, input must be completely read
10533  if (strict && (get_token() != token_type::end_of_input))
10534  {
10535  sdp.parse_error(m_lexer.get_position(),
10536  m_lexer.get_token_string(),
10537  parse_error::create(101, m_lexer.get_position(),
10538  exception_message(token_type::end_of_input, "value"), BasicJsonType()));
10539  }
10540 
10541  // in case of an error, return discarded value
10542  if (sdp.is_errored())
10543  {
10544  result = value_t::discarded;
10545  return;
10546  }
10547 
10548  // set top-level value to null if it was discarded by the callback
10549  // function
10550  if (result.is_discarded())
10551  {
10552  result = nullptr;
10553  }
10554  }
10555  else
10556  {
10557  json_sax_dom_parser<BasicJsonType> sdp(result, allow_exceptions);
10558  sax_parse_internal(&sdp);
10559 
10560  // in strict mode, input must be completely read
10561  if (strict && (get_token() != token_type::end_of_input))
10562  {
10563  sdp.parse_error(m_lexer.get_position(),
10564  m_lexer.get_token_string(),
10565  parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), BasicJsonType()));
10566  }
10567 
10568  // in case of an error, return discarded value
10569  if (sdp.is_errored())
10570  {
10571  result = value_t::discarded;
10572  return;
10573  }
10574  }
10575 
10576  result.assert_invariant();
10577  }
10578 
10585  bool accept(const bool strict = true)
10586  {
10587  json_sax_acceptor<BasicJsonType> sax_acceptor;
10588  return sax_parse(&sax_acceptor, strict);
10589  }
10590 
10591  template<typename SAX>
10592  JSON_HEDLEY_NON_NULL(2)
10593  bool sax_parse(SAX* sax, const bool strict = true)
10594  {
10595  (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
10596  const bool result = sax_parse_internal(sax);
10597 
10598  // strict mode: next byte must be EOF
10599  if (result && strict && (get_token() != token_type::end_of_input))
10600  {
10601  return sax->parse_error(m_lexer.get_position(),
10602  m_lexer.get_token_string(),
10603  parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), BasicJsonType()));
10604  }
10605 
10606  return result;
10607  }
10608 
10609  private:
10610  template<typename SAX>
10611  JSON_HEDLEY_NON_NULL(2)
10612  bool sax_parse_internal(SAX* sax)
10613  {
10614  // stack to remember the hierarchy of structured values we are parsing
10615  // true = array; false = object
10616  std::vector<bool> states;
10617  // value to avoid a goto (see comment where set to true)
10618  bool skip_to_state_evaluation = false;
10619 
10620  while (true)
10621  {
10622  if (!skip_to_state_evaluation)
10623  {
10624  // invariant: get_token() was called before each iteration
10625  switch (last_token)
10626  {
10627  case token_type::begin_object:
10628  {
10629  if (JSON_HEDLEY_UNLIKELY(!sax->start_object(std::size_t(-1))))
10630  {
10631  return false;
10632  }
10633 
10634  // closing } -> we are done
10635  if (get_token() == token_type::end_object)
10636  {
10637  if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
10638  {
10639  return false;
10640  }
10641  break;
10642  }
10643 
10644  // parse key
10645  if (JSON_HEDLEY_UNLIKELY(last_token != token_type::value_string))
10646  {
10647  return sax->parse_error(m_lexer.get_position(),
10648  m_lexer.get_token_string(),
10649  parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), BasicJsonType()));
10650  }
10651  if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
10652  {
10653  return false;
10654  }
10655 
10656  // parse separator (:)
10657  if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
10658  {
10659  return sax->parse_error(m_lexer.get_position(),
10660  m_lexer.get_token_string(),
10661  parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), BasicJsonType()));
10662  }
10663 
10664  // remember we are now inside an object
10665  states.push_back(false);
10666 
10667  // parse values
10668  get_token();
10669  continue;
10670  }
10671 
10672  case token_type::begin_array:
10673  {
10674  if (JSON_HEDLEY_UNLIKELY(!sax->start_array(std::size_t(-1))))
10675  {
10676  return false;
10677  }
10678 
10679  // closing ] -> we are done
10680  if (get_token() == token_type::end_array)
10681  {
10682  if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
10683  {
10684  return false;
10685  }
10686  break;
10687  }
10688 
10689  // remember we are now inside an array
10690  states.push_back(true);
10691 
10692  // parse values (no need to call get_token)
10693  continue;
10694  }
10695 
10696  case token_type::value_float:
10697  {
10698  const auto res = m_lexer.get_number_float();
10699 
10700  if (JSON_HEDLEY_UNLIKELY(!std::isfinite(res)))
10701  {
10702  return sax->parse_error(m_lexer.get_position(),
10703  m_lexer.get_token_string(),
10704  out_of_range::create(406, "number overflow parsing '" + m_lexer.get_token_string() + "'", BasicJsonType()));
10705  }
10706 
10707  if (JSON_HEDLEY_UNLIKELY(!sax->number_float(res, m_lexer.get_string())))
10708  {
10709  return false;
10710  }
10711 
10712  break;
10713  }
10714 
10715  case token_type::literal_false:
10716  {
10717  if (JSON_HEDLEY_UNLIKELY(!sax->boolean(false)))
10718  {
10719  return false;
10720  }
10721  break;
10722  }
10723 
10724  case token_type::literal_null:
10725  {
10726  if (JSON_HEDLEY_UNLIKELY(!sax->null()))
10727  {
10728  return false;
10729  }
10730  break;
10731  }
10732 
10733  case token_type::literal_true:
10734  {
10735  if (JSON_HEDLEY_UNLIKELY(!sax->boolean(true)))
10736  {
10737  return false;
10738  }
10739  break;
10740  }
10741 
10742  case token_type::value_integer:
10743  {
10744  if (JSON_HEDLEY_UNLIKELY(!sax->number_integer(m_lexer.get_number_integer())))
10745  {
10746  return false;
10747  }
10748  break;
10749  }
10750 
10751  case token_type::value_string:
10752  {
10753  if (JSON_HEDLEY_UNLIKELY(!sax->string(m_lexer.get_string())))
10754  {
10755  return false;
10756  }
10757  break;
10758  }
10759 
10760  case token_type::value_unsigned:
10761  {
10762  if (JSON_HEDLEY_UNLIKELY(!sax->number_unsigned(m_lexer.get_number_unsigned())))
10763  {
10764  return false;
10765  }
10766  break;
10767  }
10768 
10769  case token_type::parse_error:
10770  {
10771  // using "uninitialized" to avoid "expected" message
10772  return sax->parse_error(m_lexer.get_position(),
10773  m_lexer.get_token_string(),
10774  parse_error::create(101, m_lexer.get_position(), exception_message(token_type::uninitialized, "value"), BasicJsonType()));
10775  }
10776 
10777  default: // the last token was unexpected
10778  {
10779  return sax->parse_error(m_lexer.get_position(),
10780  m_lexer.get_token_string(),
10781  parse_error::create(101, m_lexer.get_position(), exception_message(token_type::literal_or_value, "value"), BasicJsonType()));
10782  }
10783  }
10784  }
10785  else
10786  {
10787  skip_to_state_evaluation = false;
10788  }
10789 
10790  // we reached this line after we successfully parsed a value
10791  if (states.empty())
10792  {
10793  // empty stack: we reached the end of the hierarchy: done
10794  return true;
10795  }
10796 
10797  if (states.back()) // array
10798  {
10799  // comma -> next value
10800  if (get_token() == token_type::value_separator)
10801  {
10802  // parse a new value
10803  get_token();
10804  continue;
10805  }
10806 
10807  // closing ]
10808  if (JSON_HEDLEY_LIKELY(last_token == token_type::end_array))
10809  {
10810  if (JSON_HEDLEY_UNLIKELY(!sax->end_array()))
10811  {
10812  return false;
10813  }
10814 
10815  // We are done with this array. Before we can parse a
10816  // new value, we need to evaluate the new state first.
10817  // By setting skip_to_state_evaluation to false, we
10818  // are effectively jumping to the beginning of this if.
10819  JSON_ASSERT(!states.empty());
10820  states.pop_back();
10821  skip_to_state_evaluation = true;
10822  continue;
10823  }
10824 
10825  return sax->parse_error(m_lexer.get_position(),
10826  m_lexer.get_token_string(),
10827  parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_array, "array"), BasicJsonType()));
10828  }
10829 
10830  // states.back() is false -> object
10831 
10832  // comma -> next value
10833  if (get_token() == token_type::value_separator)
10834  {
10835  // parse key
10836  if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::value_string))
10837  {
10838  return sax->parse_error(m_lexer.get_position(),
10839  m_lexer.get_token_string(),
10840  parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), BasicJsonType()));
10841  }
10842 
10843  if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string())))
10844  {
10845  return false;
10846  }
10847 
10848  // parse separator (:)
10849  if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator))
10850  {
10851  return sax->parse_error(m_lexer.get_position(),
10852  m_lexer.get_token_string(),
10853  parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), BasicJsonType()));
10854  }
10855 
10856  // parse values
10857  get_token();
10858  continue;
10859  }
10860 
10861  // closing }
10862  if (JSON_HEDLEY_LIKELY(last_token == token_type::end_object))
10863  {
10864  if (JSON_HEDLEY_UNLIKELY(!sax->end_object()))
10865  {
10866  return false;
10867  }
10868 
10869  // We are done with this object. Before we can parse a
10870  // new value, we need to evaluate the new state first.
10871  // By setting skip_to_state_evaluation to false, we
10872  // are effectively jumping to the beginning of this if.
10873  JSON_ASSERT(!states.empty());
10874  states.pop_back();
10875  skip_to_state_evaluation = true;
10876  continue;
10877  }
10878 
10879  return sax->parse_error(m_lexer.get_position(),
10880  m_lexer.get_token_string(),
10881  parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_object, "object"), BasicJsonType()));
10882  }
10883  }
10884 
10886  token_type get_token()
10887  {
10888  return last_token = m_lexer.scan();
10889  }
10890 
10891  std::string exception_message(const token_type expected, const std::string& context)
10892  {
10893  std::string error_msg = "syntax error ";
10894 
10895  if (!context.empty())
10896  {
10897  error_msg += "while parsing " + context + " ";
10898  }
10899 
10900  error_msg += "- ";
10901 
10902  if (last_token == token_type::parse_error)
10903  {
10904  error_msg += std::string(m_lexer.get_error_message()) + "; last read: '" +
10905  m_lexer.get_token_string() + "'";
10906  }
10907  else
10908  {
10909  error_msg += "unexpected " + std::string(lexer_t::token_type_name(last_token));
10910  }
10911 
10912  if (expected != token_type::uninitialized)
10913  {
10914  error_msg += "; expected " + std::string(lexer_t::token_type_name(expected));
10915  }
10916 
10917  return error_msg;
10918  }
10919 
10920  private:
10922  const parser_callback_t<BasicJsonType> callback = nullptr;
10924  token_type last_token = token_type::uninitialized;
10926  lexer_t m_lexer;
10928  const bool allow_exceptions = true;
10929 };
10930 } // namespace detail
10931 } // namespace nlohmann
10932 
10933 // #include <nlohmann/detail/iterators/internal_iterator.hpp>
10934 
10935 
10936 // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
10937 
10938 
10939 #include <cstddef> // ptrdiff_t
10940 #include <limits> // numeric_limits
10941 
10942 // #include <nlohmann/detail/macro_scope.hpp>
10943 
10944 
10945 namespace nlohmann
10946 {
10947 namespace detail
10948 {
10949 /*
10950 @brief an iterator for primitive JSON types
10951 
10952 This class models an iterator for primitive JSON types (boolean, number,
10953 string). It's only purpose is to allow the iterator/const_iterator classes
10954 to "iterate" over primitive values. Internally, the iterator is modeled by
10955 a `difference_type` variable. Value begin_value (`0`) models the begin,
10956 end_value (`1`) models past the end.
10957 */
10958 class primitive_iterator_t
10959 {
10960  private:
10961  using difference_type = std::ptrdiff_t;
10962  static constexpr difference_type begin_value = 0;
10963  static constexpr difference_type end_value = begin_value + 1;
10964 
10965  JSON_PRIVATE_UNLESS_TESTED:
10967  difference_type m_it = (std::numeric_limits<std::ptrdiff_t>::min)();
10968 
10969  public:
10970  constexpr difference_type get_value() const noexcept
10971  {
10972  return m_it;
10973  }
10974 
10976  void set_begin() noexcept
10977  {
10978  m_it = begin_value;
10979  }
10980 
10982  void set_end() noexcept
10983  {
10984  m_it = end_value;
10985  }
10986 
10988  constexpr bool is_begin() const noexcept
10989  {
10990  return m_it == begin_value;
10991  }
10992 
10994  constexpr bool is_end() const noexcept
10995  {
10996  return m_it == end_value;
10997  }
10998 
10999  friend constexpr bool operator==(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
11000  {
11001  return lhs.m_it == rhs.m_it;
11002  }
11003 
11004  friend constexpr bool operator<(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
11005  {
11006  return lhs.m_it < rhs.m_it;
11007  }
11008 
11009  primitive_iterator_t operator+(difference_type n) noexcept
11010  {
11011  auto result = *this;
11012  result += n;
11013  return result;
11014  }
11015 
11016  friend constexpr difference_type operator-(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
11017  {
11018  return lhs.m_it - rhs.m_it;
11019  }
11020 
11021  primitive_iterator_t& operator++() noexcept
11022  {
11023  ++m_it;
11024  return *this;
11025  }
11026 
11027  primitive_iterator_t const operator++(int) noexcept
11028  {
11029  auto result = *this;
11030  ++m_it;
11031  return result;
11032  }
11033 
11034  primitive_iterator_t& operator--() noexcept
11035  {
11036  --m_it;
11037  return *this;
11038  }
11039 
11040  primitive_iterator_t const operator--(int) noexcept
11041  {
11042  auto result = *this;
11043  --m_it;
11044  return result;
11045  }
11046 
11047  primitive_iterator_t& operator+=(difference_type n) noexcept
11048  {
11049  m_it += n;
11050  return *this;
11051  }
11052 
11053  primitive_iterator_t& operator-=(difference_type n) noexcept
11054  {
11055  m_it -= n;
11056  return *this;
11057  }
11058 };
11059 } // namespace detail
11060 } // namespace nlohmann
11061 
11062 
11063 namespace nlohmann
11064 {
11065 namespace detail
11066 {
11073 template<typename BasicJsonType> struct internal_iterator
11074 {
11076  typename BasicJsonType::object_t::iterator object_iterator {};
11078  typename BasicJsonType::array_t::iterator array_iterator {};
11080  primitive_iterator_t primitive_iterator {};
11081 };
11082 } // namespace detail
11083 } // namespace nlohmann
11084 
11085 // #include <nlohmann/detail/iterators/iter_impl.hpp>
11086 
11087 
11088 #include <iterator> // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next
11089 #include <type_traits> // conditional, is_const, remove_const
11090 
11091 // #include <nlohmann/detail/exceptions.hpp>
11092 
11093 // #include <nlohmann/detail/iterators/internal_iterator.hpp>
11094 
11095 // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
11096 
11097 // #include <nlohmann/detail/macro_scope.hpp>
11098 
11099 // #include <nlohmann/detail/meta/cpp_future.hpp>
11100 
11101 // #include <nlohmann/detail/meta/type_traits.hpp>
11102 
11103 // #include <nlohmann/detail/value_t.hpp>
11104 
11105 
11106 namespace nlohmann
11107 {
11108 namespace detail
11109 {
11110 // forward declare, to be able to friend it later on
11111 template<typename IteratorType> class iteration_proxy;
11112 template<typename IteratorType> class iteration_proxy_value;
11113 
11130 template<typename BasicJsonType>
11131 class iter_impl
11132 {
11134  using other_iter_impl = iter_impl<typename std::conditional<std::is_const<BasicJsonType>::value, typename std::remove_const<BasicJsonType>::type, const BasicJsonType>::type>;
11136  friend other_iter_impl;
11137  friend BasicJsonType;
11138  friend iteration_proxy<iter_impl>;
11139  friend iteration_proxy_value<iter_impl>;
11140 
11141  using object_t = typename BasicJsonType::object_t;
11142  using array_t = typename BasicJsonType::array_t;
11143  // make sure BasicJsonType is basic_json or const basic_json
11144  static_assert(is_basic_json<typename std::remove_const<BasicJsonType>::type>::value,
11145  "iter_impl only accepts (const) basic_json");
11146 
11147  public:
11148 
11154  using iterator_category = std::bidirectional_iterator_tag;
11155 
11157  using value_type = typename BasicJsonType::value_type;
11159  using difference_type = typename BasicJsonType::difference_type;
11161  using pointer = typename std::conditional<std::is_const<BasicJsonType>::value,
11162  typename BasicJsonType::const_pointer,
11163  typename BasicJsonType::pointer>::type;
11165  using reference =
11166  typename std::conditional<std::is_const<BasicJsonType>::value,
11167  typename BasicJsonType::const_reference,
11168  typename BasicJsonType::reference>::type;
11169 
11171  iter_impl() = default;
11172 
11179  explicit iter_impl(pointer object) noexcept : m_object(object)
11180  {
11181  JSON_ASSERT(m_object != nullptr);
11182 
11183  switch (m_object->m_type)
11184  {
11185  case value_t::object:
11186  {
11187  m_it.object_iterator = typename object_t::iterator();
11188  break;
11189  }
11190 
11191  case value_t::array:
11192  {
11193  m_it.array_iterator = typename array_t::iterator();
11194  break;
11195  }
11196 
11197  default:
11198  {
11199  m_it.primitive_iterator = primitive_iterator_t();
11200  break;
11201  }
11202  }
11203  }
11204 
11221  iter_impl(const iter_impl<const BasicJsonType>& other) noexcept
11222  : m_object(other.m_object), m_it(other.m_it)
11223  {}
11224 
11231  iter_impl& operator=(const iter_impl<const BasicJsonType>& other) noexcept
11232  {
11233  m_object = other.m_object;
11234  m_it = other.m_it;
11235  return *this;
11236  }
11237 
11243  iter_impl(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept
11244  : m_object(other.m_object), m_it(other.m_it)
11245  {}
11246 
11253  iter_impl& operator=(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept
11254  {
11255  m_object = other.m_object;
11256  m_it = other.m_it;
11257  return *this;
11258  }
11259 
11260  JSON_PRIVATE_UNLESS_TESTED:
11265  void set_begin() noexcept
11266  {
11267  JSON_ASSERT(m_object != nullptr);
11268 
11269  switch (m_object->m_type)
11270  {
11271  case value_t::object:
11272  {
11273  m_it.object_iterator = m_object->m_value.object->begin();
11274  break;
11275  }
11276 
11277  case value_t::array:
11278  {
11279  m_it.array_iterator = m_object->m_value.array->begin();
11280  break;
11281  }
11282 
11283  case value_t::null:
11284  {
11285  // set to end so begin()==end() is true: null is empty
11286  m_it.primitive_iterator.set_end();
11287  break;
11288  }
11289 
11290  default:
11291  {
11292  m_it.primitive_iterator.set_begin();
11293  break;
11294  }
11295  }
11296  }
11297 
11302  void set_end() noexcept
11303  {
11304  JSON_ASSERT(m_object != nullptr);
11305 
11306  switch (m_object->m_type)
11307  {
11308  case value_t::object:
11309  {
11310  m_it.object_iterator = m_object->m_value.object->end();
11311  break;
11312  }
11313 
11314  case value_t::array:
11315  {
11316  m_it.array_iterator = m_object->m_value.array->end();
11317  break;
11318  }
11319 
11320  default:
11321  {
11322  m_it.primitive_iterator.set_end();
11323  break;
11324  }
11325  }
11326  }
11327 
11328  public:
11333  reference operator*() const
11334  {
11335  JSON_ASSERT(m_object != nullptr);
11336 
11337  switch (m_object->m_type)
11338  {
11339  case value_t::object:
11340  {
11341  JSON_ASSERT(m_it.object_iterator != m_object->m_value.object->end());
11342  return m_it.object_iterator->second;
11343  }
11344 
11345  case value_t::array:
11346  {
11347  JSON_ASSERT(m_it.array_iterator != m_object->m_value.array->end());
11348  return *m_it.array_iterator;
11349  }
11350 
11351  case value_t::null:
11352  JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object));
11353 
11354  default:
11355  {
11356  if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
11357  {
11358  return *m_object;
11359  }
11360 
11361  JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object));
11362  }
11363  }
11364  }
11365 
11370  pointer operator->() const
11371  {
11372  JSON_ASSERT(m_object != nullptr);
11373 
11374  switch (m_object->m_type)
11375  {
11376  case value_t::object:
11377  {
11378  JSON_ASSERT(m_it.object_iterator != m_object->m_value.object->end());
11379  return &(m_it.object_iterator->second);
11380  }
11381 
11382  case value_t::array:
11383  {
11384  JSON_ASSERT(m_it.array_iterator != m_object->m_value.array->end());
11385  return &*m_it.array_iterator;
11386  }
11387 
11388  default:
11389  {
11390  if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
11391  {
11392  return m_object;
11393  }
11394 
11395  JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object));
11396  }
11397  }
11398  }
11399 
11404  iter_impl const operator++(int)
11405  {
11406  auto result = *this;
11407  ++(*this);
11408  return result;
11409  }
11410 
11415  iter_impl& operator++()
11416  {
11417  JSON_ASSERT(m_object != nullptr);
11418 
11419  switch (m_object->m_type)
11420  {
11421  case value_t::object:
11422  {
11423  std::advance(m_it.object_iterator, 1);
11424  break;
11425  }
11426 
11427  case value_t::array:
11428  {
11429  std::advance(m_it.array_iterator, 1);
11430  break;
11431  }
11432 
11433  default:
11434  {
11435  ++m_it.primitive_iterator;
11436  break;
11437  }
11438  }
11439 
11440  return *this;
11441  }
11442 
11447  iter_impl const operator--(int)
11448  {
11449  auto result = *this;
11450  --(*this);
11451  return result;
11452  }
11453 
11458  iter_impl& operator--()
11459  {
11460  JSON_ASSERT(m_object != nullptr);
11461 
11462  switch (m_object->m_type)
11463  {
11464  case value_t::object:
11465  {
11466  std::advance(m_it.object_iterator, -1);
11467  break;
11468  }
11469 
11470  case value_t::array:
11471  {
11472  std::advance(m_it.array_iterator, -1);
11473  break;
11474  }
11475 
11476  default:
11477  {
11478  --m_it.primitive_iterator;
11479  break;
11480  }
11481  }
11482 
11483  return *this;
11484  }
11485 
11490  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 >
11491  bool operator==(const IterImpl& other) const
11492  {
11493  // if objects are not the same, the comparison is undefined
11494  if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
11495  {
11496  JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", *m_object));
11497  }
11498 
11499  JSON_ASSERT(m_object != nullptr);
11500 
11501  switch (m_object->m_type)
11502  {
11503  case value_t::object:
11504  return (m_it.object_iterator == other.m_it.object_iterator);
11505 
11506  case value_t::array:
11507  return (m_it.array_iterator == other.m_it.array_iterator);
11508 
11509  default:
11510  return (m_it.primitive_iterator == other.m_it.primitive_iterator);
11511  }
11512  }
11513 
11518  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 >
11519  bool operator!=(const IterImpl& other) const
11520  {
11521  return !operator==(other);
11522  }
11523 
11528  bool operator<(const iter_impl& other) const
11529  {
11530  // if objects are not the same, the comparison is undefined
11531  if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
11532  {
11533  JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", *m_object));
11534  }
11535 
11536  JSON_ASSERT(m_object != nullptr);
11537 
11538  switch (m_object->m_type)
11539  {
11540  case value_t::object:
11541  JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators", *m_object));
11542 
11543  case value_t::array:
11544  return (m_it.array_iterator < other.m_it.array_iterator);
11545 
11546  default:
11547  return (m_it.primitive_iterator < other.m_it.primitive_iterator);
11548  }
11549  }
11550 
11555  bool operator<=(const iter_impl& other) const
11556  {
11557  return !other.operator < (*this);
11558  }
11559 
11564  bool operator>(const iter_impl& other) const
11565  {
11566  return !operator<=(other);
11567  }
11568 
11573  bool operator>=(const iter_impl& other) const
11574  {
11575  return !operator<(other);
11576  }
11577 
11582  iter_impl& operator+=(difference_type i)
11583  {
11584  JSON_ASSERT(m_object != nullptr);
11585 
11586  switch (m_object->m_type)
11587  {
11588  case value_t::object:
11589  JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", *m_object));
11590 
11591  case value_t::array:
11592  {
11593  std::advance(m_it.array_iterator, i);
11594  break;
11595  }
11596 
11597  default:
11598  {
11599  m_it.primitive_iterator += i;
11600  break;
11601  }
11602  }
11603 
11604  return *this;
11605  }
11606 
11611  iter_impl& operator-=(difference_type i)
11612  {
11613  return operator+=(-i);
11614  }
11615 
11620  iter_impl operator+(difference_type i) const
11621  {
11622  auto result = *this;
11623  result += i;
11624  return result;
11625  }
11626 
11631  friend iter_impl operator+(difference_type i, const iter_impl& it)
11632  {
11633  auto result = it;
11634  result += i;
11635  return result;
11636  }
11637 
11642  iter_impl operator-(difference_type i) const
11643  {
11644  auto result = *this;
11645  result -= i;
11646  return result;
11647  }
11648 
11653  difference_type operator-(const iter_impl& other) const
11654  {
11655  JSON_ASSERT(m_object != nullptr);
11656 
11657  switch (m_object->m_type)
11658  {
11659  case value_t::object:
11660  JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", *m_object));
11661 
11662  case value_t::array:
11663  return m_it.array_iterator - other.m_it.array_iterator;
11664 
11665  default:
11666  return m_it.primitive_iterator - other.m_it.primitive_iterator;
11667  }
11668  }
11669 
11674  reference operator[](difference_type n) const
11675  {
11676  JSON_ASSERT(m_object != nullptr);
11677 
11678  switch (m_object->m_type)
11679  {
11680  case value_t::object:
11681  JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators", *m_object));
11682 
11683  case value_t::array:
11684  return *std::next(m_it.array_iterator, n);
11685 
11686  case value_t::null:
11687  JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object));
11688 
11689  default:
11690  {
11691  if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.get_value() == -n))
11692  {
11693  return *m_object;
11694  }
11695 
11696  JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object));
11697  }
11698  }
11699  }
11700 
11705  const typename object_t::key_type& key() const
11706  {
11707  JSON_ASSERT(m_object != nullptr);
11708 
11709  if (JSON_HEDLEY_LIKELY(m_object->is_object()))
11710  {
11711  return m_it.object_iterator->first;
11712  }
11713 
11714  JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators", *m_object));
11715  }
11716 
11721  reference value() const
11722  {
11723  return operator*();
11724  }
11725 
11726  JSON_PRIVATE_UNLESS_TESTED:
11728  pointer m_object = nullptr;
11730  internal_iterator<typename std::remove_const<BasicJsonType>::type> m_it {};
11731 };
11732 } // namespace detail
11733 } // namespace nlohmann
11734 
11735 // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
11736 
11737 // #include <nlohmann/detail/iterators/json_reverse_iterator.hpp>
11738 
11739 
11740 #include <cstddef> // ptrdiff_t
11741 #include <iterator> // reverse_iterator
11742 #include <utility> // declval
11743 
11744 namespace nlohmann
11745 {
11746 namespace detail
11747 {
11749 // reverse_iterator //
11751 
11770 template<typename Base>
11771 class json_reverse_iterator : public std::reverse_iterator<Base>
11772 {
11773  public:
11774  using difference_type = std::ptrdiff_t;
11776  using base_iterator = std::reverse_iterator<Base>;
11778  using reference = typename Base::reference;
11779 
11781  explicit json_reverse_iterator(const typename base_iterator::iterator_type& it) noexcept
11782  : base_iterator(it) {}
11783 
11785  explicit json_reverse_iterator(const base_iterator& it) noexcept : base_iterator(it) {}
11786 
11788  json_reverse_iterator const operator++(int)
11789  {
11790  return static_cast<json_reverse_iterator>(base_iterator::operator++(1));
11791  }
11792 
11794  json_reverse_iterator& operator++()
11795  {
11796  return static_cast<json_reverse_iterator&>(base_iterator::operator++());
11797  }
11798 
11800  json_reverse_iterator const operator--(int)
11801  {
11802  return static_cast<json_reverse_iterator>(base_iterator::operator--(1));
11803  }
11804 
11806  json_reverse_iterator& operator--()
11807  {
11808  return static_cast<json_reverse_iterator&>(base_iterator::operator--());
11809  }
11810 
11812  json_reverse_iterator& operator+=(difference_type i)
11813  {
11814  return static_cast<json_reverse_iterator&>(base_iterator::operator+=(i));
11815  }
11816 
11818  json_reverse_iterator operator+(difference_type i) const
11819  {
11820  return static_cast<json_reverse_iterator>(base_iterator::operator+(i));
11821  }
11822 
11824  json_reverse_iterator operator-(difference_type i) const
11825  {
11826  return static_cast<json_reverse_iterator>(base_iterator::operator-(i));
11827  }
11828 
11830  difference_type operator-(const json_reverse_iterator& other) const
11831  {
11832  return base_iterator(*this) - base_iterator(other);
11833  }
11834 
11836  reference operator[](difference_type n) const
11837  {
11838  return *(this->operator+(n));
11839  }
11840 
11842  auto key() const -> decltype(std::declval<Base>().key())
11843  {
11844  auto it = --this->base();
11845  return it.key();
11846  }
11847 
11849  reference value() const
11850  {
11851  auto it = --this->base();
11852  return it.operator * ();
11853  }
11854 };
11855 } // namespace detail
11856 } // namespace nlohmann
11857 
11858 // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
11859 
11860 // #include <nlohmann/detail/json_pointer.hpp>
11861 
11862 
11863 #include <algorithm> // all_of
11864 #include <cctype> // isdigit
11865 #include <limits> // max
11866 #include <numeric> // accumulate
11867 #include <string> // string
11868 #include <utility> // move
11869 #include <vector> // vector
11870 
11871 // #include <nlohmann/detail/exceptions.hpp>
11872 
11873 // #include <nlohmann/detail/macro_scope.hpp>
11874 
11875 // #include <nlohmann/detail/string_escape.hpp>
11876 
11877 // #include <nlohmann/detail/value_t.hpp>
11878 
11879 
11880 namespace nlohmann
11881 {
11882 template<typename BasicJsonType>
11884 {
11885  // allow basic_json to access private members
11886  NLOHMANN_BASIC_JSON_TPL_DECLARATION
11887  friend class basic_json;
11888 
11889  public:
11911  explicit json_pointer(const std::string& s = "")
11912  : reference_tokens(split(s))
11913  {}
11914 
11929  std::string to_string() const
11930  {
11931  return std::accumulate(reference_tokens.begin(), reference_tokens.end(),
11932  std::string{},
11933  [](const std::string & a, const std::string & b)
11934  {
11935  return a + "/" + detail::escape(b);
11936  });
11937  }
11938 
11940  operator std::string() const
11941  {
11942  return to_string();
11943  }
11944 
11962  {
11963  reference_tokens.insert(reference_tokens.end(),
11964  ptr.reference_tokens.begin(),
11965  ptr.reference_tokens.end());
11966  return *this;
11967  }
11968 
11985  json_pointer& operator/=(std::string token)
11986  {
11987  push_back(std::move(token));
11988  return *this;
11989  }
11990 
12007  json_pointer& operator/=(std::size_t array_idx)
12008  {
12009  return *this /= std::to_string(array_idx);
12010  }
12011 
12028  const json_pointer& rhs)
12029  {
12030  return json_pointer(lhs) /= rhs;
12031  }
12032 
12048  friend json_pointer operator/(const json_pointer& ptr, std::string token)
12049  {
12050  return json_pointer(ptr) /= std::move(token);
12051  }
12052 
12068  friend json_pointer operator/(const json_pointer& ptr, std::size_t array_idx)
12069  {
12070  return json_pointer(ptr) /= array_idx;
12071  }
12072 
12087  {
12088  if (empty())
12089  {
12090  return *this;
12091  }
12092 
12093  json_pointer res = *this;
12094  res.pop_back();
12095  return res;
12096  }
12097 
12111  void pop_back()
12112  {
12113  if (JSON_HEDLEY_UNLIKELY(empty()))
12114  {
12115  JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", BasicJsonType()));
12116  }
12117 
12118  reference_tokens.pop_back();
12119  }
12120 
12135  const std::string& back() const
12136  {
12137  if (JSON_HEDLEY_UNLIKELY(empty()))
12138  {
12139  JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", BasicJsonType()));
12140  }
12141 
12142  return reference_tokens.back();
12143  }
12144 
12157  void push_back(const std::string& token)
12158  {
12159  reference_tokens.push_back(token);
12160  }
12161 
12163  void push_back(std::string&& token)
12164  {
12165  reference_tokens.push_back(std::move(token));
12166  }
12167 
12182  bool empty() const noexcept
12183  {
12184  return reference_tokens.empty();
12185  }
12186 
12187  private:
12198  static typename BasicJsonType::size_type array_index(const std::string& s)
12199  {
12200  using size_type = typename BasicJsonType::size_type;
12201 
12202  // error condition (cf. RFC 6901, Sect. 4)
12203  if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && s[0] == '0'))
12204  {
12205  JSON_THROW(detail::parse_error::create(106, 0, "array index '" + s + "' must not begin with '0'", BasicJsonType()));
12206  }
12207 
12208  // error condition (cf. RFC 6901, Sect. 4)
12209  if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && !(s[0] >= '1' && s[0] <= '9')))
12210  {
12211  JSON_THROW(detail::parse_error::create(109, 0, "array index '" + s + "' is not a number", BasicJsonType()));
12212  }
12213 
12214  std::size_t processed_chars = 0;
12215  unsigned long long res = 0;
12216  JSON_TRY
12217  {
12218  res = std::stoull(s, &processed_chars);
12219  }
12220  JSON_CATCH(std::out_of_range&)
12221  {
12222  JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'", BasicJsonType()));
12223  }
12224 
12225  // check if the string was completely read
12226  if (JSON_HEDLEY_UNLIKELY(processed_chars != s.size()))
12227  {
12228  JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'", BasicJsonType()));
12229  }
12230 
12231  // only triggered on special platforms (like 32bit), see also
12232  // https://github.com/nlohmann/json/pull/2203
12233  if (res >= static_cast<unsigned long long>((std::numeric_limits<size_type>::max)()))
12234  {
12235  JSON_THROW(detail::out_of_range::create(410, "array index " + s + " exceeds size_type", BasicJsonType())); // LCOV_EXCL_LINE
12236  }
12237 
12238  return static_cast<size_type>(res);
12239  }
12240 
12241  JSON_PRIVATE_UNLESS_TESTED:
12242  json_pointer top() const
12243  {
12244  if (JSON_HEDLEY_UNLIKELY(empty()))
12245  {
12246  JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", BasicJsonType()));
12247  }
12248 
12249  json_pointer result = *this;
12250  result.reference_tokens = {reference_tokens[0]};
12251  return result;
12252  }
12253 
12254  private:
12263  BasicJsonType& get_and_create(BasicJsonType& j) const
12264  {
12265  auto result = &j;
12266 
12267  // in case no reference tokens exist, return a reference to the JSON value
12268  // j which will be overwritten by a primitive value
12269  for (const auto& reference_token : reference_tokens)
12270  {
12271  switch (result->type())
12272  {
12273  case detail::value_t::null:
12274  {
12275  if (reference_token == "0")
12276  {
12277  // start a new array if reference token is 0
12278  result = &result->operator[](0);
12279  }
12280  else
12281  {
12282  // start a new object otherwise
12283  result = &result->operator[](reference_token);
12284  }
12285  break;
12286  }
12287 
12288  case detail::value_t::object:
12289  {
12290  // create an entry in the object
12291  result = &result->operator[](reference_token);
12292  break;
12293  }
12294 
12295  case detail::value_t::array:
12296  {
12297  // create an entry in the array
12298  result = &result->operator[](array_index(reference_token));
12299  break;
12300  }
12301 
12302  /*
12303  The following code is only reached if there exists a reference
12304  token _and_ the current value is primitive. In this case, we have
12305  an error situation, because primitive values may only occur as
12306  single value; that is, with an empty list of reference tokens.
12307  */
12308  default:
12309  JSON_THROW(detail::type_error::create(313, "invalid value to unflatten", j));
12310  }
12311  }
12312 
12313  return *result;
12314  }
12315 
12335  BasicJsonType& get_unchecked(BasicJsonType* ptr) const
12336  {
12337  for (const auto& reference_token : reference_tokens)
12338  {
12339  // convert null values to arrays or objects before continuing
12340  if (ptr->is_null())
12341  {
12342  // check if reference token is a number
12343  const bool nums =
12344  std::all_of(reference_token.begin(), reference_token.end(),
12345  [](const unsigned char x)
12346  {
12347  return std::isdigit(x);
12348  });
12349 
12350  // change value to array for numbers or "-" or to object otherwise
12351  *ptr = (nums || reference_token == "-")
12352  ? detail::value_t::array
12353  : detail::value_t::object;
12354  }
12355 
12356  switch (ptr->type())
12357  {
12358  case detail::value_t::object:
12359  {
12360  // use unchecked object access
12361  ptr = &ptr->operator[](reference_token);
12362  break;
12363  }
12364 
12365  case detail::value_t::array:
12366  {
12367  if (reference_token == "-")
12368  {
12369  // explicitly treat "-" as index beyond the end
12370  ptr = &ptr->operator[](ptr->m_value.array->size());
12371  }
12372  else
12373  {
12374  // convert array index to number; unchecked access
12375  ptr = &ptr->operator[](array_index(reference_token));
12376  }
12377  break;
12378  }
12379 
12380  default:
12381  JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr));
12382  }
12383  }
12384 
12385  return *ptr;
12386  }
12387 
12394  BasicJsonType& get_checked(BasicJsonType* ptr) const
12395  {
12396  for (const auto& reference_token : reference_tokens)
12397  {
12398  switch (ptr->type())
12399  {
12400  case detail::value_t::object:
12401  {
12402  // note: at performs range check
12403  ptr = &ptr->at(reference_token);
12404  break;
12405  }
12406 
12407  case detail::value_t::array:
12408  {
12409  if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
12410  {
12411  // "-" always fails the range check
12412  JSON_THROW(detail::out_of_range::create(402,
12413  "array index '-' (" + std::to_string(ptr->m_value.array->size()) +
12414  ") is out of range", *ptr));
12415  }
12416 
12417  // note: at performs range check
12418  ptr = &ptr->at(array_index(reference_token));
12419  break;
12420  }
12421 
12422  default:
12423  JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr));
12424  }
12425  }
12426 
12427  return *ptr;
12428  }
12429 
12443  const BasicJsonType& get_unchecked(const BasicJsonType* ptr) const
12444  {
12445  for (const auto& reference_token : reference_tokens)
12446  {
12447  switch (ptr->type())
12448  {
12449  case detail::value_t::object:
12450  {
12451  // use unchecked object access
12452  ptr = &ptr->operator[](reference_token);
12453  break;
12454  }
12455 
12456  case detail::value_t::array:
12457  {
12458  if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
12459  {
12460  // "-" cannot be used for const access
12461  JSON_THROW(detail::out_of_range::create(402, "array index '-' (" + std::to_string(ptr->m_value.array->size()) + ") is out of range", *ptr));
12462  }
12463 
12464  // use unchecked array access
12465  ptr = &ptr->operator[](array_index(reference_token));
12466  break;
12467  }
12468 
12469  default:
12470  JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr));
12471  }
12472  }
12473 
12474  return *ptr;
12475  }
12476 
12483  const BasicJsonType& get_checked(const BasicJsonType* ptr) const
12484  {
12485  for (const auto& reference_token : reference_tokens)
12486  {
12487  switch (ptr->type())
12488  {
12489  case detail::value_t::object:
12490  {
12491  // note: at performs range check
12492  ptr = &ptr->at(reference_token);
12493  break;
12494  }
12495 
12496  case detail::value_t::array:
12497  {
12498  if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
12499  {
12500  // "-" always fails the range check
12501  JSON_THROW(detail::out_of_range::create(402,
12502  "array index '-' (" + std::to_string(ptr->m_value.array->size()) +
12503  ") is out of range", *ptr));
12504  }
12505 
12506  // note: at performs range check
12507  ptr = &ptr->at(array_index(reference_token));
12508  break;
12509  }
12510 
12511  default:
12512  JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr));
12513  }
12514  }
12515 
12516  return *ptr;
12517  }
12518 
12523  bool contains(const BasicJsonType* ptr) const
12524  {
12525  for (const auto& reference_token : reference_tokens)
12526  {
12527  switch (ptr->type())
12528  {
12529  case detail::value_t::object:
12530  {
12531  if (!ptr->contains(reference_token))
12532  {
12533  // we did not find the key in the object
12534  return false;
12535  }
12536 
12537  ptr = &ptr->operator[](reference_token);
12538  break;
12539  }
12540 
12541  case detail::value_t::array:
12542  {
12543  if (JSON_HEDLEY_UNLIKELY(reference_token == "-"))
12544  {
12545  // "-" always fails the range check
12546  return false;
12547  }
12548  if (JSON_HEDLEY_UNLIKELY(reference_token.size() == 1 && !("0" <= reference_token && reference_token <= "9")))
12549  {
12550  // invalid char
12551  return false;
12552  }
12553  if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1))
12554  {
12555  if (JSON_HEDLEY_UNLIKELY(!('1' <= reference_token[0] && reference_token[0] <= '9')))
12556  {
12557  // first char should be between '1' and '9'
12558  return false;
12559  }
12560  for (std::size_t i = 1; i < reference_token.size(); i++)
12561  {
12562  if (JSON_HEDLEY_UNLIKELY(!('0' <= reference_token[i] && reference_token[i] <= '9')))
12563  {
12564  // other char should be between '0' and '9'
12565  return false;
12566  }
12567  }
12568  }
12569 
12570  const auto idx = array_index(reference_token);
12571  if (idx >= ptr->size())
12572  {
12573  // index out of range
12574  return false;
12575  }
12576 
12577  ptr = &ptr->operator[](idx);
12578  break;
12579  }
12580 
12581  default:
12582  {
12583  // we do not expect primitive values if there is still a
12584  // reference token to process
12585  return false;
12586  }
12587  }
12588  }
12589 
12590  // no reference token left means we found a primitive value
12591  return true;
12592  }
12593 
12603  static std::vector<std::string> split(const std::string& reference_string)
12604  {
12605  std::vector<std::string> result;
12606 
12607  // special case: empty reference string -> no reference tokens
12608  if (reference_string.empty())
12609  {
12610  return result;
12611  }
12612 
12613  // check if nonempty reference string begins with slash
12614  if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/'))
12615  {
12616  JSON_THROW(detail::parse_error::create(107, 1, "JSON pointer must be empty or begin with '/' - was: '" + reference_string + "'", BasicJsonType()));
12617  }
12618 
12619  // extract the reference tokens:
12620  // - slash: position of the last read slash (or end of string)
12621  // - start: position after the previous slash
12622  for (
12623  // search for the first slash after the first character
12624  std::size_t slash = reference_string.find_first_of('/', 1),
12625  // set the beginning of the first reference token
12626  start = 1;
12627  // we can stop if start == 0 (if slash == std::string::npos)
12628  start != 0;
12629  // set the beginning of the next reference token
12630  // (will eventually be 0 if slash == std::string::npos)
12631  start = (slash == std::string::npos) ? 0 : slash + 1,
12632  // find next slash
12633  slash = reference_string.find_first_of('/', start))
12634  {
12635  // use the text between the beginning of the reference token
12636  // (start) and the last slash (slash).
12637  auto reference_token = reference_string.substr(start, slash - start);
12638 
12639  // check reference tokens are properly escaped
12640  for (std::size_t pos = reference_token.find_first_of('~');
12641  pos != std::string::npos;
12642  pos = reference_token.find_first_of('~', pos + 1))
12643  {
12644  JSON_ASSERT(reference_token[pos] == '~');
12645 
12646  // ~ must be followed by 0 or 1
12647  if (JSON_HEDLEY_UNLIKELY(pos == reference_token.size() - 1 ||
12648  (reference_token[pos + 1] != '0' &&
12649  reference_token[pos + 1] != '1')))
12650  {
12651  JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'", BasicJsonType()));
12652  }
12653  }
12654 
12655  // finally, store the reference token
12656  detail::unescape(reference_token);
12657  result.push_back(reference_token);
12658  }
12659 
12660  return result;
12661  }
12662 
12663  private:
12671  static void flatten(const std::string& reference_string,
12672  const BasicJsonType& value,
12673  BasicJsonType& result)
12674  {
12675  switch (value.type())
12676  {
12677  case detail::value_t::array:
12678  {
12679  if (value.m_value.array->empty())
12680  {
12681  // flatten empty array as null
12682  result[reference_string] = nullptr;
12683  }
12684  else
12685  {
12686  // iterate array and use index as reference string
12687  for (std::size_t i = 0; i < value.m_value.array->size(); ++i)
12688  {
12689  flatten(reference_string + "/" + std::to_string(i),
12690  value.m_value.array->operator[](i), result);
12691  }
12692  }
12693  break;
12694  }
12695 
12696  case detail::value_t::object:
12697  {
12698  if (value.m_value.object->empty())
12699  {
12700  // flatten empty object as null
12701  result[reference_string] = nullptr;
12702  }
12703  else
12704  {
12705  // iterate object and use keys as reference string
12706  for (const auto& element : *value.m_value.object)
12707  {
12708  flatten(reference_string + "/" + detail::escape(element.first), element.second, result);
12709  }
12710  }
12711  break;
12712  }
12713 
12714  default:
12715  {
12716  // add primitive value with its reference string
12717  result[reference_string] = value;
12718  break;
12719  }
12720  }
12721  }
12722 
12733  static BasicJsonType
12734  unflatten(const BasicJsonType& value)
12735  {
12736  if (JSON_HEDLEY_UNLIKELY(!value.is_object()))
12737  {
12738  JSON_THROW(detail::type_error::create(314, "only objects can be unflattened", value));
12739  }
12740 
12741  BasicJsonType result;
12742 
12743  // iterate the JSON object values
12744  for (const auto& element : *value.m_value.object)
12745  {
12746  if (JSON_HEDLEY_UNLIKELY(!element.second.is_primitive()))
12747  {
12748  JSON_THROW(detail::type_error::create(315, "values in object must be primitive", element.second));
12749  }
12750 
12751  // assign value to reference pointed to by JSON pointer; Note that if
12752  // the JSON pointer is "" (i.e., points to the whole value), function
12753  // get_and_create returns a reference to result itself. An assignment
12754  // will then create a primitive value.
12755  json_pointer(element.first).get_and_create(result) = element.second;
12756  }
12757 
12758  return result;
12759  }
12760 
12772  friend bool operator==(json_pointer const& lhs,
12773  json_pointer const& rhs) noexcept
12774  {
12775  return lhs.reference_tokens == rhs.reference_tokens;
12776  }
12777 
12789  friend bool operator!=(json_pointer const& lhs,
12790  json_pointer const& rhs) noexcept
12791  {
12792  return !(lhs == rhs);
12793  }
12794 
12796  std::vector<std::string> reference_tokens;
12797 };
12798 } // namespace nlohmann
12799 
12800 // #include <nlohmann/detail/json_ref.hpp>
12801 
12802 
12803 #include <initializer_list>
12804 #include <utility>
12805 
12806 // #include <nlohmann/detail/meta/type_traits.hpp>
12807 
12808 
12809 namespace nlohmann
12810 {
12811 namespace detail
12812 {
12813 template<typename BasicJsonType>
12814 class json_ref
12815 {
12816  public:
12817  using value_type = BasicJsonType;
12818 
12819  json_ref(value_type&& value)
12820  : owned_value(std::move(value))
12821  {}
12822 
12823  json_ref(const value_type& value)
12824  : value_ref(&value)
12825  {}
12826 
12827  json_ref(std::initializer_list<json_ref> init)
12828  : owned_value(init)
12829  {}
12830 
12831  template <
12832  class... Args,
12833  enable_if_t<std::is_constructible<value_type, Args...>::value, int> = 0 >
12834  json_ref(Args && ... args)
12835  : owned_value(std::forward<Args>(args)...)
12836  {}
12837 
12838  // class should be movable only
12839  json_ref(json_ref&&) = default;
12840  json_ref(const json_ref&) = delete;
12841  json_ref& operator=(const json_ref&) = delete;
12842  json_ref& operator=(json_ref&&) = delete;
12843  ~json_ref() = default;
12844 
12845  value_type moved_or_copied() const
12846  {
12847  if (value_ref == nullptr)
12848  {
12849  return std::move(owned_value);
12850  }
12851  return *value_ref;
12852  }
12853 
12854  value_type const& operator*() const
12855  {
12856  return value_ref ? *value_ref : owned_value;
12857  }
12858 
12859  value_type const* operator->() const
12860  {
12861  return &** this;
12862  }
12863 
12864  private:
12865  mutable value_type owned_value = nullptr;
12866  value_type const* value_ref = nullptr;
12867 };
12868 } // namespace detail
12869 } // namespace nlohmann
12870 
12871 // #include <nlohmann/detail/macro_scope.hpp>
12872 
12873 // #include <nlohmann/detail/meta/cpp_future.hpp>
12874 
12875 // #include <nlohmann/detail/meta/type_traits.hpp>
12876 
12877 // #include <nlohmann/detail/output/binary_writer.hpp>
12878 
12879 
12880 #include <algorithm> // reverse
12881 #include <array> // array
12882 #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
12883 #include <cstring> // memcpy
12884 #include <limits> // numeric_limits
12885 #include <string> // string
12886 #include <cmath> // isnan, isinf
12887 
12888 // #include <nlohmann/detail/input/binary_reader.hpp>
12889 
12890 // #include <nlohmann/detail/macro_scope.hpp>
12891 
12892 // #include <nlohmann/detail/output/output_adapters.hpp>
12893 
12894 
12895 #include <algorithm> // copy
12896 #include <cstddef> // size_t
12897 #include <ios> // streamsize
12898 #include <iterator> // back_inserter
12899 #include <memory> // shared_ptr, make_shared
12900 #include <ostream> // basic_ostream
12901 #include <string> // basic_string
12902 #include <vector> // vector
12903 // #include <nlohmann/detail/macro_scope.hpp>
12904 
12905 
12906 namespace nlohmann
12907 {
12908 namespace detail
12909 {
12911 template<typename CharType> struct output_adapter_protocol
12912 {
12913  virtual void write_character(CharType c) = 0;
12914  virtual void write_characters(const CharType* s, std::size_t length) = 0;
12915  virtual ~output_adapter_protocol() = default;
12916 };
12917 
12919 template<typename CharType>
12920 using output_adapter_t = std::shared_ptr<output_adapter_protocol<CharType>>;
12921 
12923 template<typename CharType>
12924 class output_vector_adapter : public output_adapter_protocol<CharType>
12925 {
12926  public:
12927  explicit output_vector_adapter(std::vector<CharType>& vec) noexcept
12928  : v(vec)
12929  {}
12930 
12931  void write_character(CharType c) override
12932  {
12933  v.push_back(c);
12934  }
12935 
12936  JSON_HEDLEY_NON_NULL(2)
12937  void write_characters(const CharType* s, std::size_t length) override
12938  {
12939  std::copy(s, s + length, std::back_inserter(v));
12940  }
12941 
12942  private:
12943  std::vector<CharType>& v;
12944 };
12945 
12947 template<typename CharType>
12948 class output_stream_adapter : public output_adapter_protocol<CharType>
12949 {
12950  public:
12951  explicit output_stream_adapter(std::basic_ostream<CharType>& s) noexcept
12952  : stream(s)
12953  {}
12954 
12955  void write_character(CharType c) override
12956  {
12957  stream.put(c);
12958  }
12959 
12960  JSON_HEDLEY_NON_NULL(2)
12961  void write_characters(const CharType* s, std::size_t length) override
12962  {
12963  stream.write(s, static_cast<std::streamsize>(length));
12964  }
12965 
12966  private:
12967  std::basic_ostream<CharType>& stream;
12968 };
12969 
12971 template<typename CharType, typename StringType = std::basic_string<CharType>>
12972 class output_string_adapter : public output_adapter_protocol<CharType>
12973 {
12974  public:
12975  explicit output_string_adapter(StringType& s) noexcept
12976  : str(s)
12977  {}
12978 
12979  void write_character(CharType c) override
12980  {
12981  str.push_back(c);
12982  }
12983 
12984  JSON_HEDLEY_NON_NULL(2)
12985  void write_characters(const CharType* s, std::size_t length) override
12986  {
12987  str.append(s, length);
12988  }
12989 
12990  private:
12991  StringType& str;
12992 };
12993 
12994 template<typename CharType, typename StringType = std::basic_string<CharType>>
12995 class output_adapter
12996 {
12997  public:
12998  output_adapter(std::vector<CharType>& vec)
12999  : oa(std::make_shared<output_vector_adapter<CharType>>(vec)) {}
13000 
13001  output_adapter(std::basic_ostream<CharType>& s)
13002  : oa(std::make_shared<output_stream_adapter<CharType>>(s)) {}
13003 
13004  output_adapter(StringType& s)
13005  : oa(std::make_shared<output_string_adapter<CharType, StringType>>(s)) {}
13006 
13007  operator output_adapter_t<CharType>()
13008  {
13009  return oa;
13010  }
13011 
13012  private:
13013  output_adapter_t<CharType> oa = nullptr;
13014 };
13015 } // namespace detail
13016 } // namespace nlohmann
13017 
13018 
13019 namespace nlohmann
13020 {
13021 namespace detail
13022 {
13024 // binary writer //
13026 
13030 template<typename BasicJsonType, typename CharType>
13031 class binary_writer
13032 {
13033  using string_t = typename BasicJsonType::string_t;
13034  using binary_t = typename BasicJsonType::binary_t;
13035  using number_float_t = typename BasicJsonType::number_float_t;
13036 
13037  public:
13043  explicit binary_writer(output_adapter_t<CharType> adapter) : oa(adapter)
13044  {
13045  JSON_ASSERT(oa);
13046  }
13047 
13052  void write_bson(const BasicJsonType& j)
13053  {
13054  switch (j.type())
13055  {
13056  case value_t::object:
13057  {
13058  write_bson_object(*j.m_value.object);
13059  break;
13060  }
13061 
13062  default:
13063  {
13064  JSON_THROW(type_error::create(317, "to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name()), j));;
13065  }
13066  }
13067  }
13068 
13072  void write_cbor(const BasicJsonType& j)
13073  {
13074  switch (j.type())
13075  {
13076  case value_t::null:
13077  {
13078  oa->write_character(to_char_type(0xF6));
13079  break;
13080  }
13081 
13082  case value_t::boolean:
13083  {
13084  oa->write_character(j.m_value.boolean
13085  ? to_char_type(0xF5)
13086  : to_char_type(0xF4));
13087  break;
13088  }
13089 
13090  case value_t::number_integer:
13091  {
13092  if (j.m_value.number_integer >= 0)
13093  {
13094  // CBOR does not differentiate between positive signed
13095  // integers and unsigned integers. Therefore, we used the
13096  // code from the value_t::number_unsigned case here.
13097  if (j.m_value.number_integer <= 0x17)
13098  {
13099  write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
13100  }
13101  else if (j.m_value.number_integer <= (std::numeric_limits<std::uint8_t>::max)())
13102  {
13103  oa->write_character(to_char_type(0x18));
13104  write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
13105  }
13106  else if (j.m_value.number_integer <= (std::numeric_limits<std::uint16_t>::max)())
13107  {
13108  oa->write_character(to_char_type(0x19));
13109  write_number(static_cast<std::uint16_t>(j.m_value.number_integer));
13110  }
13111  else if (j.m_value.number_integer <= (std::numeric_limits<std::uint32_t>::max)())
13112  {
13113  oa->write_character(to_char_type(0x1A));
13114  write_number(static_cast<std::uint32_t>(j.m_value.number_integer));
13115  }
13116  else
13117  {
13118  oa->write_character(to_char_type(0x1B));
13119  write_number(static_cast<std::uint64_t>(j.m_value.number_integer));
13120  }
13121  }
13122  else
13123  {
13124  // The conversions below encode the sign in the first
13125  // byte, and the value is converted to a positive number.
13126  const auto positive_number = -1 - j.m_value.number_integer;
13127  if (j.m_value.number_integer >= -24)
13128  {
13129  write_number(static_cast<std::uint8_t>(0x20 + positive_number));
13130  }
13131  else if (positive_number <= (std::numeric_limits<std::uint8_t>::max)())
13132  {
13133  oa->write_character(to_char_type(0x38));
13134  write_number(static_cast<std::uint8_t>(positive_number));
13135  }
13136  else if (positive_number <= (std::numeric_limits<std::uint16_t>::max)())
13137  {
13138  oa->write_character(to_char_type(0x39));
13139  write_number(static_cast<std::uint16_t>(positive_number));
13140  }
13141  else if (positive_number <= (std::numeric_limits<std::uint32_t>::max)())
13142  {
13143  oa->write_character(to_char_type(0x3A));
13144  write_number(static_cast<std::uint32_t>(positive_number));
13145  }
13146  else
13147  {
13148  oa->write_character(to_char_type(0x3B));
13149  write_number(static_cast<std::uint64_t>(positive_number));
13150  }
13151  }
13152  break;
13153  }
13154 
13155  case value_t::number_unsigned:
13156  {
13157  if (j.m_value.number_unsigned <= 0x17)
13158  {
13159  write_number(static_cast<std::uint8_t>(j.m_value.number_unsigned));
13160  }
13161  else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
13162  {
13163  oa->write_character(to_char_type(0x18));
13164  write_number(static_cast<std::uint8_t>(j.m_value.number_unsigned));
13165  }
13166  else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
13167  {
13168  oa->write_character(to_char_type(0x19));
13169  write_number(static_cast<std::uint16_t>(j.m_value.number_unsigned));
13170  }
13171  else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
13172  {
13173  oa->write_character(to_char_type(0x1A));
13174  write_number(static_cast<std::uint32_t>(j.m_value.number_unsigned));
13175  }
13176  else
13177  {
13178  oa->write_character(to_char_type(0x1B));
13179  write_number(static_cast<std::uint64_t>(j.m_value.number_unsigned));
13180  }
13181  break;
13182  }
13183 
13184  case value_t::number_float:
13185  {
13186  if (std::isnan(j.m_value.number_float))
13187  {
13188  // NaN is 0xf97e00 in CBOR
13189  oa->write_character(to_char_type(0xF9));
13190  oa->write_character(to_char_type(0x7E));
13191  oa->write_character(to_char_type(0x00));
13192  }
13193  else if (std::isinf(j.m_value.number_float))
13194  {
13195  // Infinity is 0xf97c00, -Infinity is 0xf9fc00
13196  oa->write_character(to_char_type(0xf9));
13197  oa->write_character(j.m_value.number_float > 0 ? to_char_type(0x7C) : to_char_type(0xFC));
13198  oa->write_character(to_char_type(0x00));
13199  }
13200  else
13201  {
13202  write_compact_float(j.m_value.number_float, detail::input_format_t::cbor);
13203  }
13204  break;
13205  }
13206 
13207  case value_t::string:
13208  {
13209  // step 1: write control byte and the string length
13210  const auto N = j.m_value.string->size();
13211  if (N <= 0x17)
13212  {
13213  write_number(static_cast<std::uint8_t>(0x60 + N));
13214  }
13215  else if (N <= (std::numeric_limits<std::uint8_t>::max)())
13216  {
13217  oa->write_character(to_char_type(0x78));
13218  write_number(static_cast<std::uint8_t>(N));
13219  }
13220  else if (N <= (std::numeric_limits<std::uint16_t>::max)())
13221  {
13222  oa->write_character(to_char_type(0x79));
13223  write_number(static_cast<std::uint16_t>(N));
13224  }
13225  else if (N <= (std::numeric_limits<std::uint32_t>::max)())
13226  {
13227  oa->write_character(to_char_type(0x7A));
13228  write_number(static_cast<std::uint32_t>(N));
13229  }
13230  // LCOV_EXCL_START
13231  else if (N <= (std::numeric_limits<std::uint64_t>::max)())
13232  {
13233  oa->write_character(to_char_type(0x7B));
13234  write_number(static_cast<std::uint64_t>(N));
13235  }
13236  // LCOV_EXCL_STOP
13237 
13238  // step 2: write the string
13239  oa->write_characters(
13240  reinterpret_cast<const CharType*>(j.m_value.string->c_str()),
13241  j.m_value.string->size());
13242  break;
13243  }
13244 
13245  case value_t::array:
13246  {
13247  // step 1: write control byte and the array size
13248  const auto N = j.m_value.array->size();
13249  if (N <= 0x17)
13250  {
13251  write_number(static_cast<std::uint8_t>(0x80 + N));
13252  }
13253  else if (N <= (std::numeric_limits<std::uint8_t>::max)())
13254  {
13255  oa->write_character(to_char_type(0x98));
13256  write_number(static_cast<std::uint8_t>(N));
13257  }
13258  else if (N <= (std::numeric_limits<std::uint16_t>::max)())
13259  {
13260  oa->write_character(to_char_type(0x99));
13261  write_number(static_cast<std::uint16_t>(N));
13262  }
13263  else if (N <= (std::numeric_limits<std::uint32_t>::max)())
13264  {
13265  oa->write_character(to_char_type(0x9A));
13266  write_number(static_cast<std::uint32_t>(N));
13267  }
13268  // LCOV_EXCL_START
13269  else if (N <= (std::numeric_limits<std::uint64_t>::max)())
13270  {
13271  oa->write_character(to_char_type(0x9B));
13272  write_number(static_cast<std::uint64_t>(N));
13273  }
13274  // LCOV_EXCL_STOP
13275 
13276  // step 2: write each element
13277  for (const auto& el : *j.m_value.array)
13278  {
13279  write_cbor(el);
13280  }
13281  break;
13282  }
13283 
13284  case value_t::binary:
13285  {
13286  if (j.m_value.binary->has_subtype())
13287  {
13288  write_number(static_cast<std::uint8_t>(0xd8));
13289  write_number(j.m_value.binary->subtype());
13290  }
13291 
13292  // step 1: write control byte and the binary array size
13293  const auto N = j.m_value.binary->size();
13294  if (N <= 0x17)
13295  {
13296  write_number(static_cast<std::uint8_t>(0x40 + N));
13297  }
13298  else if (N <= (std::numeric_limits<std::uint8_t>::max)())
13299  {
13300  oa->write_character(to_char_type(0x58));
13301  write_number(static_cast<std::uint8_t>(N));
13302  }
13303  else if (N <= (std::numeric_limits<std::uint16_t>::max)())
13304  {
13305  oa->write_character(to_char_type(0x59));
13306  write_number(static_cast<std::uint16_t>(N));
13307  }
13308  else if (N <= (std::numeric_limits<std::uint32_t>::max)())
13309  {
13310  oa->write_character(to_char_type(0x5A));
13311  write_number(static_cast<std::uint32_t>(N));
13312  }
13313  // LCOV_EXCL_START
13314  else if (N <= (std::numeric_limits<std::uint64_t>::max)())
13315  {
13316  oa->write_character(to_char_type(0x5B));
13317  write_number(static_cast<std::uint64_t>(N));
13318  }
13319  // LCOV_EXCL_STOP
13320 
13321  // step 2: write each element
13322  oa->write_characters(
13323  reinterpret_cast<const CharType*>(j.m_value.binary->data()),
13324  N);
13325 
13326  break;
13327  }
13328 
13329  case value_t::object:
13330  {
13331  // step 1: write control byte and the object size
13332  const auto N = j.m_value.object->size();
13333  if (N <= 0x17)
13334  {
13335  write_number(static_cast<std::uint8_t>(0xA0 + N));
13336  }
13337  else if (N <= (std::numeric_limits<std::uint8_t>::max)())
13338  {
13339  oa->write_character(to_char_type(0xB8));
13340  write_number(static_cast<std::uint8_t>(N));
13341  }
13342  else if (N <= (std::numeric_limits<std::uint16_t>::max)())
13343  {
13344  oa->write_character(to_char_type(0xB9));
13345  write_number(static_cast<std::uint16_t>(N));
13346  }
13347  else if (N <= (std::numeric_limits<std::uint32_t>::max)())
13348  {
13349  oa->write_character(to_char_type(0xBA));
13350  write_number(static_cast<std::uint32_t>(N));
13351  }
13352  // LCOV_EXCL_START
13353  else if (N <= (std::numeric_limits<std::uint64_t>::max)())
13354  {
13355  oa->write_character(to_char_type(0xBB));
13356  write_number(static_cast<std::uint64_t>(N));
13357  }
13358  // LCOV_EXCL_STOP
13359 
13360  // step 2: write each element
13361  for (const auto& el : *j.m_value.object)
13362  {
13363  write_cbor(el.first);
13364  write_cbor(el.second);
13365  }
13366  break;
13367  }
13368 
13369  default:
13370  break;
13371  }
13372  }
13373 
13377  void write_msgpack(const BasicJsonType& j)
13378  {
13379  switch (j.type())
13380  {
13381  case value_t::null: // nil
13382  {
13383  oa->write_character(to_char_type(0xC0));
13384  break;
13385  }
13386 
13387  case value_t::boolean: // true and false
13388  {
13389  oa->write_character(j.m_value.boolean
13390  ? to_char_type(0xC3)
13391  : to_char_type(0xC2));
13392  break;
13393  }
13394 
13395  case value_t::number_integer:
13396  {
13397  if (j.m_value.number_integer >= 0)
13398  {
13399  // MessagePack does not differentiate between positive
13400  // signed integers and unsigned integers. Therefore, we used
13401  // the code from the value_t::number_unsigned case here.
13402  if (j.m_value.number_unsigned < 128)
13403  {
13404  // positive fixnum
13405  write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
13406  }
13407  else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
13408  {
13409  // uint 8
13410  oa->write_character(to_char_type(0xCC));
13411  write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
13412  }
13413  else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
13414  {
13415  // uint 16
13416  oa->write_character(to_char_type(0xCD));
13417  write_number(static_cast<std::uint16_t>(j.m_value.number_integer));
13418  }
13419  else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
13420  {
13421  // uint 32
13422  oa->write_character(to_char_type(0xCE));
13423  write_number(static_cast<std::uint32_t>(j.m_value.number_integer));
13424  }
13425  else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
13426  {
13427  // uint 64
13428  oa->write_character(to_char_type(0xCF));
13429  write_number(static_cast<std::uint64_t>(j.m_value.number_integer));
13430  }
13431  }
13432  else
13433  {
13434  if (j.m_value.number_integer >= -32)
13435  {
13436  // negative fixnum
13437  write_number(static_cast<std::int8_t>(j.m_value.number_integer));
13438  }
13439  else if (j.m_value.number_integer >= (std::numeric_limits<std::int8_t>::min)() &&
13440  j.m_value.number_integer <= (std::numeric_limits<std::int8_t>::max)())
13441  {
13442  // int 8
13443  oa->write_character(to_char_type(0xD0));
13444  write_number(static_cast<std::int8_t>(j.m_value.number_integer));
13445  }
13446  else if (j.m_value.number_integer >= (std::numeric_limits<std::int16_t>::min)() &&
13447  j.m_value.number_integer <= (std::numeric_limits<std::int16_t>::max)())
13448  {
13449  // int 16
13450  oa->write_character(to_char_type(0xD1));
13451  write_number(static_cast<std::int16_t>(j.m_value.number_integer));
13452  }
13453  else if (j.m_value.number_integer >= (std::numeric_limits<std::int32_t>::min)() &&
13454  j.m_value.number_integer <= (std::numeric_limits<std::int32_t>::max)())
13455  {
13456  // int 32
13457  oa->write_character(to_char_type(0xD2));
13458  write_number(static_cast<std::int32_t>(j.m_value.number_integer));
13459  }
13460  else if (j.m_value.number_integer >= (std::numeric_limits<std::int64_t>::min)() &&
13461  j.m_value.number_integer <= (std::numeric_limits<std::int64_t>::max)())
13462  {
13463  // int 64
13464  oa->write_character(to_char_type(0xD3));
13465  write_number(static_cast<std::int64_t>(j.m_value.number_integer));
13466  }
13467  }
13468  break;
13469  }
13470 
13471  case value_t::number_unsigned:
13472  {
13473  if (j.m_value.number_unsigned < 128)
13474  {
13475  // positive fixnum
13476  write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
13477  }
13478  else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
13479  {
13480  // uint 8
13481  oa->write_character(to_char_type(0xCC));
13482  write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
13483  }
13484  else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
13485  {
13486  // uint 16
13487  oa->write_character(to_char_type(0xCD));
13488  write_number(static_cast<std::uint16_t>(j.m_value.number_integer));
13489  }
13490  else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
13491  {
13492  // uint 32
13493  oa->write_character(to_char_type(0xCE));
13494  write_number(static_cast<std::uint32_t>(j.m_value.number_integer));
13495  }
13496  else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
13497  {
13498  // uint 64
13499  oa->write_character(to_char_type(0xCF));
13500  write_number(static_cast<std::uint64_t>(j.m_value.number_integer));
13501  }
13502  break;
13503  }
13504 
13505  case value_t::number_float:
13506  {
13507  write_compact_float(j.m_value.number_float, detail::input_format_t::msgpack);
13508  break;
13509  }
13510 
13511  case value_t::string:
13512  {
13513  // step 1: write control byte and the string length
13514  const auto N = j.m_value.string->size();
13515  if (N <= 31)
13516  {
13517  // fixstr
13518  write_number(static_cast<std::uint8_t>(0xA0 | N));
13519  }
13520  else if (N <= (std::numeric_limits<std::uint8_t>::max)())
13521  {
13522  // str 8
13523  oa->write_character(to_char_type(0xD9));
13524  write_number(static_cast<std::uint8_t>(N));
13525  }
13526  else if (N <= (std::numeric_limits<std::uint16_t>::max)())
13527  {
13528  // str 16
13529  oa->write_character(to_char_type(0xDA));
13530  write_number(static_cast<std::uint16_t>(N));
13531  }
13532  else if (N <= (std::numeric_limits<std::uint32_t>::max)())
13533  {
13534  // str 32
13535  oa->write_character(to_char_type(0xDB));
13536  write_number(static_cast<std::uint32_t>(N));
13537  }
13538 
13539  // step 2: write the string
13540  oa->write_characters(
13541  reinterpret_cast<const CharType*>(j.m_value.string->c_str()),
13542  j.m_value.string->size());
13543  break;
13544  }
13545 
13546  case value_t::array:
13547  {
13548  // step 1: write control byte and the array size
13549  const auto N = j.m_value.array->size();
13550  if (N <= 15)
13551  {
13552  // fixarray
13553  write_number(static_cast<std::uint8_t>(0x90 | N));
13554  }
13555  else if (N <= (std::numeric_limits<std::uint16_t>::max)())
13556  {
13557  // array 16
13558  oa->write_character(to_char_type(0xDC));
13559  write_number(static_cast<std::uint16_t>(N));
13560  }
13561  else if (N <= (std::numeric_limits<std::uint32_t>::max)())
13562  {
13563  // array 32
13564  oa->write_character(to_char_type(0xDD));
13565  write_number(static_cast<std::uint32_t>(N));
13566  }
13567 
13568  // step 2: write each element
13569  for (const auto& el : *j.m_value.array)
13570  {
13571  write_msgpack(el);
13572  }
13573  break;
13574  }
13575 
13576  case value_t::binary:
13577  {
13578  // step 0: determine if the binary type has a set subtype to
13579  // determine whether or not to use the ext or fixext types
13580  const bool use_ext = j.m_value.binary->has_subtype();
13581 
13582  // step 1: write control byte and the byte string length
13583  const auto N = j.m_value.binary->size();
13584  if (N <= (std::numeric_limits<std::uint8_t>::max)())
13585  {
13586  std::uint8_t output_type{};
13587  bool fixed = true;
13588  if (use_ext)
13589  {
13590  switch (N)
13591  {
13592  case 1:
13593  output_type = 0xD4; // fixext 1
13594  break;
13595  case 2:
13596  output_type = 0xD5; // fixext 2
13597  break;
13598  case 4:
13599  output_type = 0xD6; // fixext 4
13600  break;
13601  case 8:
13602  output_type = 0xD7; // fixext 8
13603  break;
13604  case 16:
13605  output_type = 0xD8; // fixext 16
13606  break;
13607  default:
13608  output_type = 0xC7; // ext 8
13609  fixed = false;
13610  break;
13611  }
13612 
13613  }
13614  else
13615  {
13616  output_type = 0xC4; // bin 8
13617  fixed = false;
13618  }
13619 
13620  oa->write_character(to_char_type(output_type));
13621  if (!fixed)
13622  {
13623  write_number(static_cast<std::uint8_t>(N));
13624  }
13625  }
13626  else if (N <= (std::numeric_limits<std::uint16_t>::max)())
13627  {
13628  std::uint8_t output_type = use_ext
13629  ? 0xC8 // ext 16
13630  : 0xC5; // bin 16
13631 
13632  oa->write_character(to_char_type(output_type));
13633  write_number(static_cast<std::uint16_t>(N));
13634  }
13635  else if (N <= (std::numeric_limits<std::uint32_t>::max)())
13636  {
13637  std::uint8_t output_type = use_ext
13638  ? 0xC9 // ext 32
13639  : 0xC6; // bin 32
13640 
13641  oa->write_character(to_char_type(output_type));
13642  write_number(static_cast<std::uint32_t>(N));
13643  }
13644 
13645  // step 1.5: if this is an ext type, write the subtype
13646  if (use_ext)
13647  {
13648  write_number(static_cast<std::int8_t>(j.m_value.binary->subtype()));
13649  }
13650 
13651  // step 2: write the byte string
13652  oa->write_characters(
13653  reinterpret_cast<const CharType*>(j.m_value.binary->data()),
13654  N);
13655 
13656  break;
13657  }
13658 
13659  case value_t::object:
13660  {
13661  // step 1: write control byte and the object size
13662  const auto N = j.m_value.object->size();
13663  if (N <= 15)
13664  {
13665  // fixmap
13666  write_number(static_cast<std::uint8_t>(0x80 | (N & 0xF)));
13667  }
13668  else if (N <= (std::numeric_limits<std::uint16_t>::max)())
13669  {
13670  // map 16
13671  oa->write_character(to_char_type(0xDE));
13672  write_number(static_cast<std::uint16_t>(N));
13673  }
13674  else if (N <= (std::numeric_limits<std::uint32_t>::max)())
13675  {
13676  // map 32
13677  oa->write_character(to_char_type(0xDF));
13678  write_number(static_cast<std::uint32_t>(N));
13679  }
13680 
13681  // step 2: write each element
13682  for (const auto& el : *j.m_value.object)
13683  {
13684  write_msgpack(el.first);
13685  write_msgpack(el.second);
13686  }
13687  break;
13688  }
13689 
13690  default:
13691  break;
13692  }
13693  }
13694 
13701  void write_ubjson(const BasicJsonType& j, const bool use_count,
13702  const bool use_type, const bool add_prefix = true)
13703  {
13704  switch (j.type())
13705  {
13706  case value_t::null:
13707  {
13708  if (add_prefix)
13709  {
13710  oa->write_character(to_char_type('Z'));
13711  }
13712  break;
13713  }
13714 
13715  case value_t::boolean:
13716  {
13717  if (add_prefix)
13718  {
13719  oa->write_character(j.m_value.boolean
13720  ? to_char_type('T')
13721  : to_char_type('F'));
13722  }
13723  break;
13724  }
13725 
13726  case value_t::number_integer:
13727  {
13728  write_number_with_ubjson_prefix(j.m_value.number_integer, add_prefix);
13729  break;
13730  }
13731 
13732  case value_t::number_unsigned:
13733  {
13734  write_number_with_ubjson_prefix(j.m_value.number_unsigned, add_prefix);
13735  break;
13736  }
13737 
13738  case value_t::number_float:
13739  {
13740  write_number_with_ubjson_prefix(j.m_value.number_float, add_prefix);
13741  break;
13742  }
13743 
13744  case value_t::string:
13745  {
13746  if (add_prefix)
13747  {
13748  oa->write_character(to_char_type('S'));
13749  }
13750  write_number_with_ubjson_prefix(j.m_value.string->size(), true);
13751  oa->write_characters(
13752  reinterpret_cast<const CharType*>(j.m_value.string->c_str()),
13753  j.m_value.string->size());
13754  break;
13755  }
13756 
13757  case value_t::array:
13758  {
13759  if (add_prefix)
13760  {
13761  oa->write_character(to_char_type('['));
13762  }
13763 
13764  bool prefix_required = true;
13765  if (use_type && !j.m_value.array->empty())
13766  {
13767  JSON_ASSERT(use_count);
13768  const CharType first_prefix = ubjson_prefix(j.front());
13769  const bool same_prefix = std::all_of(j.begin() + 1, j.end(),
13770  [this, first_prefix](const BasicJsonType & v)
13771  {
13772  return ubjson_prefix(v) == first_prefix;
13773  });
13774 
13775  if (same_prefix)
13776  {
13777  prefix_required = false;
13778  oa->write_character(to_char_type('$'));
13779  oa->write_character(first_prefix);
13780  }
13781  }
13782 
13783  if (use_count)
13784  {
13785  oa->write_character(to_char_type('#'));
13786  write_number_with_ubjson_prefix(j.m_value.array->size(), true);
13787  }
13788 
13789  for (const auto& el : *j.m_value.array)
13790  {
13791  write_ubjson(el, use_count, use_type, prefix_required);
13792  }
13793 
13794  if (!use_count)
13795  {
13796  oa->write_character(to_char_type(']'));
13797  }
13798 
13799  break;
13800  }
13801 
13802  case value_t::binary:
13803  {
13804  if (add_prefix)
13805  {
13806  oa->write_character(to_char_type('['));
13807  }
13808 
13809  if (use_type && !j.m_value.binary->empty())
13810  {
13811  JSON_ASSERT(use_count);
13812  oa->write_character(to_char_type('$'));
13813  oa->write_character('U');
13814  }
13815 
13816  if (use_count)
13817  {
13818  oa->write_character(to_char_type('#'));
13819  write_number_with_ubjson_prefix(j.m_value.binary->size(), true);
13820  }
13821 
13822  if (use_type)
13823  {
13824  oa->write_characters(
13825  reinterpret_cast<const CharType*>(j.m_value.binary->data()),
13826  j.m_value.binary->size());
13827  }
13828  else
13829  {
13830  for (size_t i = 0; i < j.m_value.binary->size(); ++i)
13831  {
13832  oa->write_character(to_char_type('U'));
13833  oa->write_character(j.m_value.binary->data()[i]);
13834  }
13835  }
13836 
13837  if (!use_count)
13838  {
13839  oa->write_character(to_char_type(']'));
13840  }
13841 
13842  break;
13843  }
13844 
13845  case value_t::object:
13846  {
13847  if (add_prefix)
13848  {
13849  oa->write_character(to_char_type('{'));
13850  }
13851 
13852  bool prefix_required = true;
13853  if (use_type && !j.m_value.object->empty())
13854  {
13855  JSON_ASSERT(use_count);
13856  const CharType first_prefix = ubjson_prefix(j.front());
13857  const bool same_prefix = std::all_of(j.begin(), j.end(),
13858  [this, first_prefix](const BasicJsonType & v)
13859  {
13860  return ubjson_prefix(v) == first_prefix;
13861  });
13862 
13863  if (same_prefix)
13864  {
13865  prefix_required = false;
13866  oa->write_character(to_char_type('$'));
13867  oa->write_character(first_prefix);
13868  }
13869  }
13870 
13871  if (use_count)
13872  {
13873  oa->write_character(to_char_type('#'));
13874  write_number_with_ubjson_prefix(j.m_value.object->size(), true);
13875  }
13876 
13877  for (const auto& el : *j.m_value.object)
13878  {
13879  write_number_with_ubjson_prefix(el.first.size(), true);
13880  oa->write_characters(
13881  reinterpret_cast<const CharType*>(el.first.c_str()),
13882  el.first.size());
13883  write_ubjson(el.second, use_count, use_type, prefix_required);
13884  }
13885 
13886  if (!use_count)
13887  {
13888  oa->write_character(to_char_type('}'));
13889  }
13890 
13891  break;
13892  }
13893 
13894  default:
13895  break;
13896  }
13897  }
13898 
13899  private:
13901  // BSON //
13903 
13908  static std::size_t calc_bson_entry_header_size(const string_t& name, const BasicJsonType& j)
13909  {
13910  const auto it = name.find(static_cast<typename string_t::value_type>(0));
13911  if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos))
13912  {
13913  JSON_THROW(out_of_range::create(409, "BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) + ")", j));
13914  }
13915 
13916  return /*id*/ 1ul + name.size() + /*zero-terminator*/1u;
13917  }
13918 
13922  void write_bson_entry_header(const string_t& name,
13923  const std::uint8_t element_type)
13924  {
13925  oa->write_character(to_char_type(element_type)); // boolean
13926  oa->write_characters(
13927  reinterpret_cast<const CharType*>(name.c_str()),
13928  name.size() + 1u);
13929  }
13930 
13934  void write_bson_boolean(const string_t& name,
13935  const bool value)
13936  {
13937  write_bson_entry_header(name, 0x08);
13938  oa->write_character(value ? to_char_type(0x01) : to_char_type(0x00));
13939  }
13940 
13944  void write_bson_double(const string_t& name,
13945  const double value)
13946  {
13947  write_bson_entry_header(name, 0x01);
13948  write_number<double, true>(value);
13949  }
13950 
13954  static std::size_t calc_bson_string_size(const string_t& value)
13955  {
13956  return sizeof(std::int32_t) + value.size() + 1ul;
13957  }
13958 
13962  void write_bson_string(const string_t& name,
13963  const string_t& value)
13964  {
13965  write_bson_entry_header(name, 0x02);
13966 
13967  write_number<std::int32_t, true>(static_cast<std::int32_t>(value.size() + 1ul));
13968  oa->write_characters(
13969  reinterpret_cast<const CharType*>(value.c_str()),
13970  value.size() + 1);
13971  }
13972 
13976  void write_bson_null(const string_t& name)
13977  {
13978  write_bson_entry_header(name, 0x0A);
13979  }
13980 
13984  static std::size_t calc_bson_integer_size(const std::int64_t value)
13985  {
13986  return (std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)()
13987  ? sizeof(std::int32_t)
13988  : sizeof(std::int64_t);
13989  }
13990 
13994  void write_bson_integer(const string_t& name,
13995  const std::int64_t value)
13996  {
13997  if ((std::numeric_limits<std::int32_t>::min)() <= value && value <= (std::numeric_limits<std::int32_t>::max)())
13998  {
13999  write_bson_entry_header(name, 0x10); // int32
14000  write_number<std::int32_t, true>(static_cast<std::int32_t>(value));
14001  }
14002  else
14003  {
14004  write_bson_entry_header(name, 0x12); // int64
14005  write_number<std::int64_t, true>(static_cast<std::int64_t>(value));
14006  }
14007  }
14008 
14012  static constexpr std::size_t calc_bson_unsigned_size(const std::uint64_t value) noexcept
14013  {
14014  return (value <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
14015  ? sizeof(std::int32_t)
14016  : sizeof(std::int64_t);
14017  }
14018 
14022  void write_bson_unsigned(const string_t& name,
14023  const BasicJsonType& j)
14024  {
14025  if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
14026  {
14027  write_bson_entry_header(name, 0x10 /* int32 */);
14028  write_number<std::int32_t, true>(static_cast<std::int32_t>(j.m_value.number_unsigned));
14029  }
14030  else if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
14031  {
14032  write_bson_entry_header(name, 0x12 /* int64 */);
14033  write_number<std::int64_t, true>(static_cast<std::int64_t>(j.m_value.number_unsigned));
14034  }
14035  else
14036  {
14037  JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(j.m_value.number_unsigned) + " cannot be represented by BSON as it does not fit int64", j));
14038  }
14039  }
14040 
14044  void write_bson_object_entry(const string_t& name,
14045  const typename BasicJsonType::object_t& value)
14046  {
14047  write_bson_entry_header(name, 0x03); // object
14048  write_bson_object(value);
14049  }
14050 
14054  static std::size_t calc_bson_array_size(const typename BasicJsonType::array_t& value)
14055  {
14056  std::size_t array_index = 0ul;
14057 
14058  const std::size_t embedded_document_size = std::accumulate(std::begin(value), std::end(value), std::size_t(0), [&array_index](std::size_t result, const typename BasicJsonType::array_t::value_type & el)
14059  {
14060  return result + calc_bson_element_size(std::to_string(array_index++), el);
14061  });
14062 
14063  return sizeof(std::int32_t) + embedded_document_size + 1ul;
14064  }
14065 
14069  static std::size_t calc_bson_binary_size(const typename BasicJsonType::binary_t& value)
14070  {
14071  return sizeof(std::int32_t) + value.size() + 1ul;
14072  }
14073 
14077  void write_bson_array(const string_t& name,
14078  const typename BasicJsonType::array_t& value)
14079  {
14080  write_bson_entry_header(name, 0x04); // array
14081  write_number<std::int32_t, true>(static_cast<std::int32_t>(calc_bson_array_size(value)));
14082 
14083  std::size_t array_index = 0ul;
14084 
14085  for (const auto& el : value)
14086  {
14087  write_bson_element(std::to_string(array_index++), el);
14088  }
14089 
14090  oa->write_character(to_char_type(0x00));
14091  }
14092 
14096  void write_bson_binary(const string_t& name,
14097  const binary_t& value)
14098  {
14099  write_bson_entry_header(name, 0x05);
14100 
14101  write_number<std::int32_t, true>(static_cast<std::int32_t>(value.size()));
14102  write_number(value.has_subtype() ? value.subtype() : std::uint8_t(0x00));
14103 
14104  oa->write_characters(reinterpret_cast<const CharType*>(value.data()), value.size());
14105  }
14106 
14111  static std::size_t calc_bson_element_size(const string_t& name,
14112  const BasicJsonType& j)
14113  {
14114  const auto header_size = calc_bson_entry_header_size(name, j);
14115  switch (j.type())
14116  {
14117  case value_t::object:
14118  return header_size + calc_bson_object_size(*j.m_value.object);
14119 
14120  case value_t::array:
14121  return header_size + calc_bson_array_size(*j.m_value.array);
14122 
14123  case value_t::binary:
14124  return header_size + calc_bson_binary_size(*j.m_value.binary);
14125 
14126  case value_t::boolean:
14127  return header_size + 1ul;
14128 
14129  case value_t::number_float:
14130  return header_size + 8ul;
14131 
14132  case value_t::number_integer:
14133  return header_size + calc_bson_integer_size(j.m_value.number_integer);
14134 
14135  case value_t::number_unsigned:
14136  return header_size + calc_bson_unsigned_size(j.m_value.number_unsigned);
14137 
14138  case value_t::string:
14139  return header_size + calc_bson_string_size(*j.m_value.string);
14140 
14141  case value_t::null:
14142  return header_size + 0ul;
14143 
14144  // LCOV_EXCL_START
14145  default:
14146  JSON_ASSERT(false);
14147  return 0ul;
14148  // LCOV_EXCL_STOP
14149  }
14150  }
14151 
14159  void write_bson_element(const string_t& name,
14160  const BasicJsonType& j)
14161  {
14162  switch (j.type())
14163  {
14164  case value_t::object:
14165  return write_bson_object_entry(name, *j.m_value.object);
14166 
14167  case value_t::array:
14168  return write_bson_array(name, *j.m_value.array);
14169 
14170  case value_t::binary:
14171  return write_bson_binary(name, *j.m_value.binary);
14172 
14173  case value_t::boolean:
14174  return write_bson_boolean(name, j.m_value.boolean);
14175 
14176  case value_t::number_float:
14177  return write_bson_double(name, j.m_value.number_float);
14178 
14179  case value_t::number_integer:
14180  return write_bson_integer(name, j.m_value.number_integer);
14181 
14182  case value_t::number_unsigned:
14183  return write_bson_unsigned(name, j);
14184 
14185  case value_t::string:
14186  return write_bson_string(name, *j.m_value.string);
14187 
14188  case value_t::null:
14189  return write_bson_null(name);
14190 
14191  // LCOV_EXCL_START
14192  default:
14193  JSON_ASSERT(false);
14194  return;
14195  // LCOV_EXCL_STOP
14196  }
14197  }
14198 
14205  static std::size_t calc_bson_object_size(const typename BasicJsonType::object_t& value)
14206  {
14207  std::size_t document_size = std::accumulate(value.begin(), value.end(), std::size_t(0),
14208  [](size_t result, const typename BasicJsonType::object_t::value_type & el)
14209  {
14210  return result += calc_bson_element_size(el.first, el.second);
14211  });
14212 
14213  return sizeof(std::int32_t) + document_size + 1ul;
14214  }
14215 
14220  void write_bson_object(const typename BasicJsonType::object_t& value)
14221  {
14222  write_number<std::int32_t, true>(static_cast<std::int32_t>(calc_bson_object_size(value)));
14223 
14224  for (const auto& el : value)
14225  {
14226  write_bson_element(el.first, el.second);
14227  }
14228 
14229  oa->write_character(to_char_type(0x00));
14230  }
14231 
14233  // CBOR //
14235 
14236  static constexpr CharType get_cbor_float_prefix(float /*unused*/)
14237  {
14238  return to_char_type(0xFA); // Single-Precision Float
14239  }
14240 
14241  static constexpr CharType get_cbor_float_prefix(double /*unused*/)
14242  {
14243  return to_char_type(0xFB); // Double-Precision Float
14244  }
14245 
14247  // MsgPack //
14249 
14250  static constexpr CharType get_msgpack_float_prefix(float /*unused*/)
14251  {
14252  return to_char_type(0xCA); // float 32
14253  }
14254 
14255  static constexpr CharType get_msgpack_float_prefix(double /*unused*/)
14256  {
14257  return to_char_type(0xCB); // float 64
14258  }
14259 
14261  // UBJSON //
14263 
14264  // UBJSON: write number (floating point)
14265  template<typename NumberType, typename std::enable_if<
14266  std::is_floating_point<NumberType>::value, int>::type = 0>
14267  void write_number_with_ubjson_prefix(const NumberType n,
14268  const bool add_prefix)
14269  {
14270  if (add_prefix)
14271  {
14272  oa->write_character(get_ubjson_float_prefix(n));
14273  }
14274  write_number(n);
14275  }
14276 
14277  // UBJSON: write number (unsigned integer)
14278  template<typename NumberType, typename std::enable_if<
14279  std::is_unsigned<NumberType>::value, int>::type = 0>
14280  void write_number_with_ubjson_prefix(const NumberType n,
14281  const bool add_prefix)
14282  {
14283  if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
14284  {
14285  if (add_prefix)
14286  {
14287  oa->write_character(to_char_type('i')); // int8
14288  }
14289  write_number(static_cast<std::uint8_t>(n));
14290  }
14291  else if (n <= (std::numeric_limits<std::uint8_t>::max)())
14292  {
14293  if (add_prefix)
14294  {
14295  oa->write_character(to_char_type('U')); // uint8
14296  }
14297  write_number(static_cast<std::uint8_t>(n));
14298  }
14299  else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
14300  {
14301  if (add_prefix)
14302  {
14303  oa->write_character(to_char_type('I')); // int16
14304  }
14305  write_number(static_cast<std::int16_t>(n));
14306  }
14307  else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
14308  {
14309  if (add_prefix)
14310  {
14311  oa->write_character(to_char_type('l')); // int32
14312  }
14313  write_number(static_cast<std::int32_t>(n));
14314  }
14315  else if (n <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
14316  {
14317  if (add_prefix)
14318  {
14319  oa->write_character(to_char_type('L')); // int64
14320  }
14321  write_number(static_cast<std::int64_t>(n));
14322  }
14323  else
14324  {
14325  if (add_prefix)
14326  {
14327  oa->write_character(to_char_type('H')); // high-precision number
14328  }
14329 
14330  const auto number = BasicJsonType(n).dump();
14331  write_number_with_ubjson_prefix(number.size(), true);
14332  for (std::size_t i = 0; i < number.size(); ++i)
14333  {
14334  oa->write_character(to_char_type(static_cast<std::uint8_t>(number[i])));
14335  }
14336  }
14337  }
14338 
14339  // UBJSON: write number (signed integer)
14340  template < typename NumberType, typename std::enable_if <
14341  std::is_signed<NumberType>::value&&
14342  !std::is_floating_point<NumberType>::value, int >::type = 0 >
14343  void write_number_with_ubjson_prefix(const NumberType n,
14344  const bool add_prefix)
14345  {
14346  if ((std::numeric_limits<std::int8_t>::min)() <= n && n <= (std::numeric_limits<std::int8_t>::max)())
14347  {
14348  if (add_prefix)
14349  {
14350  oa->write_character(to_char_type('i')); // int8
14351  }
14352  write_number(static_cast<std::int8_t>(n));
14353  }
14354  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)()))
14355  {
14356  if (add_prefix)
14357  {
14358  oa->write_character(to_char_type('U')); // uint8
14359  }
14360  write_number(static_cast<std::uint8_t>(n));
14361  }
14362  else if ((std::numeric_limits<std::int16_t>::min)() <= n && n <= (std::numeric_limits<std::int16_t>::max)())
14363  {
14364  if (add_prefix)
14365  {
14366  oa->write_character(to_char_type('I')); // int16
14367  }
14368  write_number(static_cast<std::int16_t>(n));
14369  }
14370  else if ((std::numeric_limits<std::int32_t>::min)() <= n && n <= (std::numeric_limits<std::int32_t>::max)())
14371  {
14372  if (add_prefix)
14373  {
14374  oa->write_character(to_char_type('l')); // int32
14375  }
14376  write_number(static_cast<std::int32_t>(n));
14377  }
14378  else if ((std::numeric_limits<std::int64_t>::min)() <= n && n <= (std::numeric_limits<std::int64_t>::max)())
14379  {
14380  if (add_prefix)
14381  {
14382  oa->write_character(to_char_type('L')); // int64
14383  }
14384  write_number(static_cast<std::int64_t>(n));
14385  }
14386  // LCOV_EXCL_START
14387  else
14388  {
14389  if (add_prefix)
14390  {
14391  oa->write_character(to_char_type('H')); // high-precision number
14392  }
14393 
14394  const auto number = BasicJsonType(n).dump();
14395  write_number_with_ubjson_prefix(number.size(), true);
14396  for (std::size_t i = 0; i < number.size(); ++i)
14397  {
14398  oa->write_character(to_char_type(static_cast<std::uint8_t>(number[i])));
14399  }
14400  }
14401  // LCOV_EXCL_STOP
14402  }
14403 
14407  CharType ubjson_prefix(const BasicJsonType& j) const noexcept
14408  {
14409  switch (j.type())
14410  {
14411  case value_t::null:
14412  return 'Z';
14413 
14414  case value_t::boolean:
14415  return j.m_value.boolean ? 'T' : 'F';
14416 
14417  case value_t::number_integer:
14418  {
14419  if ((std::numeric_limits<std::int8_t>::min)() <= j.m_value.number_integer && j.m_value.number_integer <= (std::numeric_limits<std::int8_t>::max)())
14420  {
14421  return 'i';
14422  }
14423  if ((std::numeric_limits<std::uint8_t>::min)() <= j.m_value.number_integer && j.m_value.number_integer <= (std::numeric_limits<std::uint8_t>::max)())
14424  {
14425  return 'U';
14426  }
14427  if ((std::numeric_limits<std::int16_t>::min)() <= j.m_value.number_integer && j.m_value.number_integer <= (std::numeric_limits<std::int16_t>::max)())
14428  {
14429  return 'I';
14430  }
14431  if ((std::numeric_limits<std::int32_t>::min)() <= j.m_value.number_integer && j.m_value.number_integer <= (std::numeric_limits<std::int32_t>::max)())
14432  {
14433  return 'l';
14434  }
14435  if ((std::numeric_limits<std::int64_t>::min)() <= j.m_value.number_integer && j.m_value.number_integer <= (std::numeric_limits<std::int64_t>::max)())
14436  {
14437  return 'L';
14438  }
14439  // anything else is treated as high-precision number
14440  return 'H'; // LCOV_EXCL_LINE
14441  }
14442 
14443  case value_t::number_unsigned:
14444  {
14445  if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int8_t>::max)()))
14446  {
14447  return 'i';
14448  }
14449  if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::uint8_t>::max)()))
14450  {
14451  return 'U';
14452  }
14453  if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int16_t>::max)()))
14454  {
14455  return 'I';
14456  }
14457  if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
14458  {
14459  return 'l';
14460  }
14461  if (j.m_value.number_unsigned <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
14462  {
14463  return 'L';
14464  }
14465  // anything else is treated as high-precision number
14466  return 'H'; // LCOV_EXCL_LINE
14467  }
14468 
14469  case value_t::number_float:
14470  return get_ubjson_float_prefix(j.m_value.number_float);
14471 
14472  case value_t::string:
14473  return 'S';
14474 
14475  case value_t::array: // fallthrough
14476  case value_t::binary:
14477  return '[';
14478 
14479  case value_t::object:
14480  return '{';
14481 
14482  default: // discarded values
14483  return 'N';
14484  }
14485  }
14486 
14487  static constexpr CharType get_ubjson_float_prefix(float /*unused*/)
14488  {
14489  return 'd'; // float 32
14490  }
14491 
14492  static constexpr CharType get_ubjson_float_prefix(double /*unused*/)
14493  {
14494  return 'D'; // float 64
14495  }
14496 
14498  // Utility functions //
14500 
14501  /*
14502  @brief write a number to output input
14503  @param[in] n number of type @a NumberType
14504  @tparam NumberType the type of the number
14505  @tparam OutputIsLittleEndian Set to true if output data is
14506  required to be little endian
14507 
14508  @note This function needs to respect the system's endianess, because bytes
14509  in CBOR, MessagePack, and UBJSON are stored in network order (big
14510  endian) and therefore need reordering on little endian systems.
14511  */
14512  template<typename NumberType, bool OutputIsLittleEndian = false>
14513  void write_number(const NumberType n)
14514  {
14515  // step 1: write number to array of length NumberType
14516  std::array<CharType, sizeof(NumberType)> vec;
14517  std::memcpy(vec.data(), &n, sizeof(NumberType));
14518 
14519  // step 2: write array to output (with possible reordering)
14520  if (is_little_endian != OutputIsLittleEndian)
14521  {
14522  // reverse byte order prior to conversion if necessary
14523  std::reverse(vec.begin(), vec.end());
14524  }
14525 
14526  oa->write_characters(vec.data(), sizeof(NumberType));
14527  }
14528 
14529  void write_compact_float(const number_float_t n, detail::input_format_t format)
14530  {
14531  if (static_cast<double>(n) >= static_cast<double>(std::numeric_limits<float>::lowest()) &&
14532  static_cast<double>(n) <= static_cast<double>((std::numeric_limits<float>::max)()) &&
14533  static_cast<double>(static_cast<float>(n)) == static_cast<double>(n))
14534  {
14535  oa->write_character(format == detail::input_format_t::cbor
14536  ? get_cbor_float_prefix(static_cast<float>(n))
14537  : get_msgpack_float_prefix(static_cast<float>(n)));
14538  write_number(static_cast<float>(n));
14539  }
14540  else
14541  {
14542  oa->write_character(format == detail::input_format_t::cbor
14543  ? get_cbor_float_prefix(n)
14544  : get_msgpack_float_prefix(n));
14545  write_number(n);
14546  }
14547  }
14548 
14549  public:
14550  // The following to_char_type functions are implement the conversion
14551  // between uint8_t and CharType. In case CharType is not unsigned,
14552  // such a conversion is required to allow values greater than 128.
14553  // See <https://github.com/nlohmann/json/issues/1286> for a discussion.
14554  template < typename C = CharType,
14555  enable_if_t < std::is_signed<C>::value && std::is_signed<char>::value > * = nullptr >
14556  static constexpr CharType to_char_type(std::uint8_t x) noexcept
14557  {
14558  return *reinterpret_cast<char*>(&x);
14559  }
14560 
14561  template < typename C = CharType,
14562  enable_if_t < std::is_signed<C>::value && std::is_unsigned<char>::value > * = nullptr >
14563  static CharType to_char_type(std::uint8_t x) noexcept
14564  {
14565  static_assert(sizeof(std::uint8_t) == sizeof(CharType), "size of CharType must be equal to std::uint8_t");
14566  static_assert(std::is_trivial<CharType>::value, "CharType must be trivial");
14567  CharType result;
14568  std::memcpy(&result, &x, sizeof(x));
14569  return result;
14570  }
14571 
14572  template<typename C = CharType,
14573  enable_if_t<std::is_unsigned<C>::value>* = nullptr>
14574  static constexpr CharType to_char_type(std::uint8_t x) noexcept
14575  {
14576  return x;
14577  }
14578 
14579  template < typename InputCharType, typename C = CharType,
14580  enable_if_t <
14581  std::is_signed<C>::value &&
14582  std::is_signed<char>::value &&
14583  std::is_same<char, typename std::remove_cv<InputCharType>::type>::value
14584  > * = nullptr >
14585  static constexpr CharType to_char_type(InputCharType x) noexcept
14586  {
14587  return x;
14588  }
14589 
14590  private:
14592  const bool is_little_endian = little_endianess();
14593 
14595  output_adapter_t<CharType> oa = nullptr;
14596 };
14597 } // namespace detail
14598 } // namespace nlohmann
14599 
14600 // #include <nlohmann/detail/output/output_adapters.hpp>
14601 
14602 // #include <nlohmann/detail/output/serializer.hpp>
14603 
14604 
14605 #include <algorithm> // reverse, remove, fill, find, none_of
14606 #include <array> // array
14607 #include <clocale> // localeconv, lconv
14608 #include <cmath> // labs, isfinite, isnan, signbit
14609 #include <cstddef> // size_t, ptrdiff_t
14610 #include <cstdint> // uint8_t
14611 #include <cstdio> // snprintf
14612 #include <limits> // numeric_limits
14613 #include <string> // string, char_traits
14614 #include <type_traits> // is_same
14615 #include <utility> // move
14616 
14617 // #include <nlohmann/detail/conversions/to_chars.hpp>
14618 
14619 
14620 #include <array> // array
14621 #include <cmath> // signbit, isfinite
14622 #include <cstdint> // intN_t, uintN_t
14623 #include <cstring> // memcpy, memmove
14624 #include <limits> // numeric_limits
14625 #include <type_traits> // conditional
14626 
14627 // #include <nlohmann/detail/macro_scope.hpp>
14628 
14629 
14630 namespace nlohmann
14631 {
14632 namespace detail
14633 {
14634 
14654 namespace dtoa_impl
14655 {
14656 
14657 template<typename Target, typename Source>
14658 Target reinterpret_bits(const Source source)
14659 {
14660  static_assert(sizeof(Target) == sizeof(Source), "size mismatch");
14661 
14662  Target target;
14663  std::memcpy(&target, &source, sizeof(Source));
14664  return target;
14665 }
14666 
14667 struct diyfp // f * 2^e
14668 {
14669  static constexpr int kPrecision = 64; // = q
14670 
14671  std::uint64_t f = 0;
14672  int e = 0;
14673 
14674  constexpr diyfp(std::uint64_t f_, int e_) noexcept : f(f_), e(e_) {}
14675 
14680  static diyfp sub(const diyfp& x, const diyfp& y) noexcept
14681  {
14682  JSON_ASSERT(x.e == y.e);
14683  JSON_ASSERT(x.f >= y.f);
14684 
14685  return {x.f - y.f, x.e};
14686  }
14687 
14692  static diyfp mul(const diyfp& x, const diyfp& y) noexcept
14693  {
14694  static_assert(kPrecision == 64, "internal error");
14695 
14696  // Computes:
14697  // f = round((x.f * y.f) / 2^q)
14698  // e = x.e + y.e + q
14699 
14700  // Emulate the 64-bit * 64-bit multiplication:
14701  //
14702  // p = u * v
14703  // = (u_lo + 2^32 u_hi) (v_lo + 2^32 v_hi)
14704  // = (u_lo v_lo ) + 2^32 ((u_lo v_hi ) + (u_hi v_lo )) + 2^64 (u_hi v_hi )
14705  // = (p0 ) + 2^32 ((p1 ) + (p2 )) + 2^64 (p3 )
14706  // = (p0_lo + 2^32 p0_hi) + 2^32 ((p1_lo + 2^32 p1_hi) + (p2_lo + 2^32 p2_hi)) + 2^64 (p3 )
14707  // = (p0_lo ) + 2^32 (p0_hi + p1_lo + p2_lo ) + 2^64 (p1_hi + p2_hi + p3)
14708  // = (p0_lo ) + 2^32 (Q ) + 2^64 (H )
14709  // = (p0_lo ) + 2^32 (Q_lo + 2^32 Q_hi ) + 2^64 (H )
14710  //
14711  // (Since Q might be larger than 2^32 - 1)
14712  //
14713  // = (p0_lo + 2^32 Q_lo) + 2^64 (Q_hi + H)
14714  //
14715  // (Q_hi + H does not overflow a 64-bit int)
14716  //
14717  // = p_lo + 2^64 p_hi
14718 
14719  const std::uint64_t u_lo = x.f & 0xFFFFFFFFu;
14720  const std::uint64_t u_hi = x.f >> 32u;
14721  const std::uint64_t v_lo = y.f & 0xFFFFFFFFu;
14722  const std::uint64_t v_hi = y.f >> 32u;
14723 
14724  const std::uint64_t p0 = u_lo * v_lo;
14725  const std::uint64_t p1 = u_lo * v_hi;
14726  const std::uint64_t p2 = u_hi * v_lo;
14727  const std::uint64_t p3 = u_hi * v_hi;
14728 
14729  const std::uint64_t p0_hi = p0 >> 32u;
14730  const std::uint64_t p1_lo = p1 & 0xFFFFFFFFu;
14731  const std::uint64_t p1_hi = p1 >> 32u;
14732  const std::uint64_t p2_lo = p2 & 0xFFFFFFFFu;
14733  const std::uint64_t p2_hi = p2 >> 32u;
14734 
14735  std::uint64_t Q = p0_hi + p1_lo + p2_lo;
14736 
14737  // The full product might now be computed as
14738  //
14739  // p_hi = p3 + p2_hi + p1_hi + (Q >> 32)
14740  // p_lo = p0_lo + (Q << 32)
14741  //
14742  // But in this particular case here, the full p_lo is not required.
14743  // Effectively we only need to add the highest bit in p_lo to p_hi (and
14744  // Q_hi + 1 does not overflow).
14745 
14746  Q += std::uint64_t{1} << (64u - 32u - 1u); // round, ties up
14747 
14748  const std::uint64_t h = p3 + p2_hi + p1_hi + (Q >> 32u);
14749 
14750  return {h, x.e + y.e + 64};
14751  }
14752 
14757  static diyfp normalize(diyfp x) noexcept
14758  {
14759  JSON_ASSERT(x.f != 0);
14760 
14761  while ((x.f >> 63u) == 0)
14762  {
14763  x.f <<= 1u;
14764  x.e--;
14765  }
14766 
14767  return x;
14768  }
14769 
14774  static diyfp normalize_to(const diyfp& x, const int target_exponent) noexcept
14775  {
14776  const int delta = x.e - target_exponent;
14777 
14778  JSON_ASSERT(delta >= 0);
14779  JSON_ASSERT(((x.f << delta) >> delta) == x.f);
14780 
14781  return {x.f << delta, target_exponent};
14782  }
14783 };
14784 
14785 struct boundaries
14786 {
14787  diyfp w;
14788  diyfp minus;
14789  diyfp plus;
14790 };
14791 
14798 template<typename FloatType>
14799 boundaries compute_boundaries(FloatType value)
14800 {
14801  JSON_ASSERT(std::isfinite(value));
14802  JSON_ASSERT(value > 0);
14803 
14804  // Convert the IEEE representation into a diyfp.
14805  //
14806  // If v is denormal:
14807  // value = 0.F * 2^(1 - bias) = ( F) * 2^(1 - bias - (p-1))
14808  // If v is normalized:
14809  // value = 1.F * 2^(E - bias) = (2^(p-1) + F) * 2^(E - bias - (p-1))
14810 
14811  static_assert(std::numeric_limits<FloatType>::is_iec559,
14812  "internal error: dtoa_short requires an IEEE-754 floating-point implementation");
14813 
14814  constexpr int kPrecision = std::numeric_limits<FloatType>::digits; // = p (includes the hidden bit)
14815  constexpr int kBias = std::numeric_limits<FloatType>::max_exponent - 1 + (kPrecision - 1);
14816  constexpr int kMinExp = 1 - kBias;
14817  constexpr std::uint64_t kHiddenBit = std::uint64_t{1} << (kPrecision - 1); // = 2^(p-1)
14818 
14819  using bits_type = typename std::conditional<kPrecision == 24, std::uint32_t, std::uint64_t >::type;
14820 
14821  const std::uint64_t bits = reinterpret_bits<bits_type>(value);
14822  const std::uint64_t E = bits >> (kPrecision - 1);
14823  const std::uint64_t F = bits & (kHiddenBit - 1);
14824 
14825  const bool is_denormal = E == 0;
14826  const diyfp v = is_denormal
14827  ? diyfp(F, kMinExp)
14828  : diyfp(F + kHiddenBit, static_cast<int>(E) - kBias);
14829 
14830  // Compute the boundaries m- and m+ of the floating-point value
14831  // v = f * 2^e.
14832  //
14833  // Determine v- and v+, the floating-point predecessor and successor if v,
14834  // respectively.
14835  //
14836  // v- = v - 2^e if f != 2^(p-1) or e == e_min (A)
14837  // = v - 2^(e-1) if f == 2^(p-1) and e > e_min (B)
14838  //
14839  // v+ = v + 2^e
14840  //
14841  // Let m- = (v- + v) / 2 and m+ = (v + v+) / 2. All real numbers _strictly_
14842  // between m- and m+ round to v, regardless of how the input rounding
14843  // algorithm breaks ties.
14844  //
14845  // ---+-------------+-------------+-------------+-------------+--- (A)
14846  // v- m- v m+ v+
14847  //
14848  // -----------------+------+------+-------------+-------------+--- (B)
14849  // v- m- v m+ v+
14850 
14851  const bool lower_boundary_is_closer = F == 0 && E > 1;
14852  const diyfp m_plus = diyfp(2 * v.f + 1, v.e - 1);
14853  const diyfp m_minus = lower_boundary_is_closer
14854  ? diyfp(4 * v.f - 1, v.e - 2) // (B)
14855  : diyfp(2 * v.f - 1, v.e - 1); // (A)
14856 
14857  // Determine the normalized w+ = m+.
14858  const diyfp w_plus = diyfp::normalize(m_plus);
14859 
14860  // Determine w- = m- such that e_(w-) = e_(w+).
14861  const diyfp w_minus = diyfp::normalize_to(m_minus, w_plus.e);
14862 
14863  return {diyfp::normalize(v), w_minus, w_plus};
14864 }
14865 
14866 // Given normalized diyfp w, Grisu needs to find a (normalized) cached
14867 // power-of-ten c, such that the exponent of the product c * w = f * 2^e lies
14868 // within a certain range [alpha, gamma] (Definition 3.2 from [1])
14869 //
14870 // alpha <= e = e_c + e_w + q <= gamma
14871 //
14872 // or
14873 //
14874 // f_c * f_w * 2^alpha <= f_c 2^(e_c) * f_w 2^(e_w) * 2^q
14875 // <= f_c * f_w * 2^gamma
14876 //
14877 // Since c and w are normalized, i.e. 2^(q-1) <= f < 2^q, this implies
14878 //
14879 // 2^(q-1) * 2^(q-1) * 2^alpha <= c * w * 2^q < 2^q * 2^q * 2^gamma
14880 //
14881 // or
14882 //
14883 // 2^(q - 2 + alpha) <= c * w < 2^(q + gamma)
14884 //
14885 // The choice of (alpha,gamma) determines the size of the table and the form of
14886 // the digit generation procedure. Using (alpha,gamma)=(-60,-32) works out well
14887 // in practice:
14888 //
14889 // The idea is to cut the number c * w = f * 2^e into two parts, which can be
14890 // processed independently: An integral part p1, and a fractional part p2:
14891 //
14892 // f * 2^e = ( (f div 2^-e) * 2^-e + (f mod 2^-e) ) * 2^e
14893 // = (f div 2^-e) + (f mod 2^-e) * 2^e
14894 // = p1 + p2 * 2^e
14895 //
14896 // The conversion of p1 into decimal form requires a series of divisions and
14897 // modulos by (a power of) 10. These operations are faster for 32-bit than for
14898 // 64-bit integers, so p1 should ideally fit into a 32-bit integer. This can be
14899 // achieved by choosing
14900 //
14901 // -e >= 32 or e <= -32 := gamma
14902 //
14903 // In order to convert the fractional part
14904 //
14905 // p2 * 2^e = p2 / 2^-e = d[-1] / 10^1 + d[-2] / 10^2 + ...
14906 //
14907 // into decimal form, the fraction is repeatedly multiplied by 10 and the digits
14908 // d[-i] are extracted in order:
14909 //
14910 // (10 * p2) div 2^-e = d[-1]
14911 // (10 * p2) mod 2^-e = d[-2] / 10^1 + ...
14912 //
14913 // The multiplication by 10 must not overflow. It is sufficient to choose
14914 //
14915 // 10 * p2 < 16 * p2 = 2^4 * p2 <= 2^64.
14916 //
14917 // Since p2 = f mod 2^-e < 2^-e,
14918 //
14919 // -e <= 60 or e >= -60 := alpha
14920 
14921 constexpr int kAlpha = -60;
14922 constexpr int kGamma = -32;
14923 
14924 struct cached_power // c = f * 2^e ~= 10^k
14925 {
14926  std::uint64_t f;
14927  int e;
14928  int k;
14929 };
14930 
14938 inline cached_power get_cached_power_for_binary_exponent(int e)
14939 {
14940  // Now
14941  //
14942  // alpha <= e_c + e + q <= gamma (1)
14943  // ==> f_c * 2^alpha <= c * 2^e * 2^q
14944  //
14945  // and since the c's are normalized, 2^(q-1) <= f_c,
14946  //
14947  // ==> 2^(q - 1 + alpha) <= c * 2^(e + q)
14948  // ==> 2^(alpha - e - 1) <= c
14949  //
14950  // If c were an exact power of ten, i.e. c = 10^k, one may determine k as
14951  //
14952  // k = ceil( log_10( 2^(alpha - e - 1) ) )
14953  // = ceil( (alpha - e - 1) * log_10(2) )
14954  //
14955  // From the paper:
14956  // "In theory the result of the procedure could be wrong since c is rounded,
14957  // and the computation itself is approximated [...]. In practice, however,
14958  // this simple function is sufficient."
14959  //
14960  // For IEEE double precision floating-point numbers converted into
14961  // normalized diyfp's w = f * 2^e, with q = 64,
14962  //
14963  // e >= -1022 (min IEEE exponent)
14964  // -52 (p - 1)
14965  // -52 (p - 1, possibly normalize denormal IEEE numbers)
14966  // -11 (normalize the diyfp)
14967  // = -1137
14968  //
14969  // and
14970  //
14971  // e <= +1023 (max IEEE exponent)
14972  // -52 (p - 1)
14973  // -11 (normalize the diyfp)
14974  // = 960
14975  //
14976  // This binary exponent range [-1137,960] results in a decimal exponent
14977  // range [-307,324]. One does not need to store a cached power for each
14978  // k in this range. For each such k it suffices to find a cached power
14979  // such that the exponent of the product lies in [alpha,gamma].
14980  // This implies that the difference of the decimal exponents of adjacent
14981  // table entries must be less than or equal to
14982  //
14983  // floor( (gamma - alpha) * log_10(2) ) = 8.
14984  //
14985  // (A smaller distance gamma-alpha would require a larger table.)
14986 
14987  // NB:
14988  // Actually this function returns c, such that -60 <= e_c + e + 64 <= -34.
14989 
14990  constexpr int kCachedPowersMinDecExp = -300;
14991  constexpr int kCachedPowersDecStep = 8;
14992 
14993  static constexpr std::array<cached_power, 79> kCachedPowers =
14994  {
14995  {
14996  { 0xAB70FE17C79AC6CA, -1060, -300 },
14997  { 0xFF77B1FCBEBCDC4F, -1034, -292 },
14998  { 0xBE5691EF416BD60C, -1007, -284 },
14999  { 0x8DD01FAD907FFC3C, -980, -276 },
15000  { 0xD3515C2831559A83, -954, -268 },
15001  { 0x9D71AC8FADA6C9B5, -927, -260 },
15002  { 0xEA9C227723EE8BCB, -901, -252 },
15003  { 0xAECC49914078536D, -874, -244 },
15004  { 0x823C12795DB6CE57, -847, -236 },
15005  { 0xC21094364DFB5637, -821, -228 },
15006  { 0x9096EA6F3848984F, -794, -220 },
15007  { 0xD77485CB25823AC7, -768, -212 },
15008  { 0xA086CFCD97BF97F4, -741, -204 },
15009  { 0xEF340A98172AACE5, -715, -196 },
15010  { 0xB23867FB2A35B28E, -688, -188 },
15011  { 0x84C8D4DFD2C63F3B, -661, -180 },
15012  { 0xC5DD44271AD3CDBA, -635, -172 },
15013  { 0x936B9FCEBB25C996, -608, -164 },
15014  { 0xDBAC6C247D62A584, -582, -156 },
15015  { 0xA3AB66580D5FDAF6, -555, -148 },
15016  { 0xF3E2F893DEC3F126, -529, -140 },
15017  { 0xB5B5ADA8AAFF80B8, -502, -132 },
15018  { 0x87625F056C7C4A8B, -475, -124 },
15019  { 0xC9BCFF6034C13053, -449, -116 },
15020  { 0x964E858C91BA2655, -422, -108 },
15021  { 0xDFF9772470297EBD, -396, -100 },
15022  { 0xA6DFBD9FB8E5B88F, -369, -92 },
15023  { 0xF8A95FCF88747D94, -343, -84 },
15024  { 0xB94470938FA89BCF, -316, -76 },
15025  { 0x8A08F0F8BF0F156B, -289, -68 },
15026  { 0xCDB02555653131B6, -263, -60 },
15027  { 0x993FE2C6D07B7FAC, -236, -52 },
15028  { 0xE45C10C42A2B3B06, -210, -44 },
15029  { 0xAA242499697392D3, -183, -36 },
15030  { 0xFD87B5F28300CA0E, -157, -28 },
15031  { 0xBCE5086492111AEB, -130, -20 },
15032  { 0x8CBCCC096F5088CC, -103, -12 },
15033  { 0xD1B71758E219652C, -77, -4 },
15034  { 0x9C40000000000000, -50, 4 },
15035  { 0xE8D4A51000000000, -24, 12 },
15036  { 0xAD78EBC5AC620000, 3, 20 },
15037  { 0x813F3978F8940984, 30, 28 },
15038  { 0xC097CE7BC90715B3, 56, 36 },
15039  { 0x8F7E32CE7BEA5C70, 83, 44 },
15040  { 0xD5D238A4ABE98068, 109, 52 },
15041  { 0x9F4F2726179A2245, 136, 60 },
15042  { 0xED63A231D4C4FB27, 162, 68 },
15043  { 0xB0DE65388CC8ADA8, 189, 76 },
15044  { 0x83C7088E1AAB65DB, 216, 84 },
15045  { 0xC45D1DF942711D9A, 242, 92 },
15046  { 0x924D692CA61BE758, 269, 100 },
15047  { 0xDA01EE641A708DEA, 295, 108 },
15048  { 0xA26DA3999AEF774A, 322, 116 },
15049  { 0xF209787BB47D6B85, 348, 124 },
15050  { 0xB454E4A179DD1877, 375, 132 },
15051  { 0x865B86925B9BC5C2, 402, 140 },
15052  { 0xC83553C5C8965D3D, 428, 148 },
15053  { 0x952AB45CFA97A0B3, 455, 156 },
15054  { 0xDE469FBD99A05FE3, 481, 164 },
15055  { 0xA59BC234DB398C25, 508, 172 },
15056  { 0xF6C69A72A3989F5C, 534, 180 },
15057  { 0xB7DCBF5354E9BECE, 561, 188 },
15058  { 0x88FCF317F22241E2, 588, 196 },
15059  { 0xCC20CE9BD35C78A5, 614, 204 },
15060  { 0x98165AF37B2153DF, 641, 212 },
15061  { 0xE2A0B5DC971F303A, 667, 220 },
15062  { 0xA8D9D1535CE3B396, 694, 228 },
15063  { 0xFB9B7CD9A4A7443C, 720, 236 },
15064  { 0xBB764C4CA7A44410, 747, 244 },
15065  { 0x8BAB8EEFB6409C1A, 774, 252 },
15066  { 0xD01FEF10A657842C, 800, 260 },
15067  { 0x9B10A4E5E9913129, 827, 268 },
15068  { 0xE7109BFBA19C0C9D, 853, 276 },
15069  { 0xAC2820D9623BF429, 880, 284 },
15070  { 0x80444B5E7AA7CF85, 907, 292 },
15071  { 0xBF21E44003ACDD2D, 933, 300 },
15072  { 0x8E679C2F5E44FF8F, 960, 308 },
15073  { 0xD433179D9C8CB841, 986, 316 },
15074  { 0x9E19DB92B4E31BA9, 1013, 324 },
15075  }
15076  };
15077 
15078  // This computation gives exactly the same results for k as
15079  // k = ceil((kAlpha - e - 1) * 0.30102999566398114)
15080  // for |e| <= 1500, but doesn't require floating-point operations.
15081  // NB: log_10(2) ~= 78913 / 2^18
15082  JSON_ASSERT(e >= -1500);
15083  JSON_ASSERT(e <= 1500);
15084  const int f = kAlpha - e - 1;
15085  const int k = (f * 78913) / (1 << 18) + static_cast<int>(f > 0);
15086 
15087  const int index = (-kCachedPowersMinDecExp + k + (kCachedPowersDecStep - 1)) / kCachedPowersDecStep;
15088  JSON_ASSERT(index >= 0);
15089  JSON_ASSERT(static_cast<std::size_t>(index) < kCachedPowers.size());
15090 
15091  const cached_power cached = kCachedPowers[static_cast<std::size_t>(index)];
15092  JSON_ASSERT(kAlpha <= cached.e + e + 64);
15093  JSON_ASSERT(kGamma >= cached.e + e + 64);
15094 
15095  return cached;
15096 }
15097 
15102 inline int find_largest_pow10(const std::uint32_t n, std::uint32_t& pow10)
15103 {
15104  // LCOV_EXCL_START
15105  if (n >= 1000000000)
15106  {
15107  pow10 = 1000000000;
15108  return 10;
15109  }
15110  // LCOV_EXCL_STOP
15111  if (n >= 100000000)
15112  {
15113  pow10 = 100000000;
15114  return 9;
15115  }
15116  if (n >= 10000000)
15117  {
15118  pow10 = 10000000;
15119  return 8;
15120  }
15121  if (n >= 1000000)
15122  {
15123  pow10 = 1000000;
15124  return 7;
15125  }
15126  if (n >= 100000)
15127  {
15128  pow10 = 100000;
15129  return 6;
15130  }
15131  if (n >= 10000)
15132  {
15133  pow10 = 10000;
15134  return 5;
15135  }
15136  if (n >= 1000)
15137  {
15138  pow10 = 1000;
15139  return 4;
15140  }
15141  if (n >= 100)
15142  {
15143  pow10 = 100;
15144  return 3;
15145  }
15146  if (n >= 10)
15147  {
15148  pow10 = 10;
15149  return 2;
15150  }
15151 
15152  pow10 = 1;
15153  return 1;
15154 }
15155 
15156 inline void grisu2_round(char* buf, int len, std::uint64_t dist, std::uint64_t delta,
15157  std::uint64_t rest, std::uint64_t ten_k)
15158 {
15159  JSON_ASSERT(len >= 1);
15160  JSON_ASSERT(dist <= delta);
15161  JSON_ASSERT(rest <= delta);
15162  JSON_ASSERT(ten_k > 0);
15163 
15164  // <--------------------------- delta ---->
15165  // <---- dist --------->
15166  // --------------[------------------+-------------------]--------------
15167  // M- w M+
15168  //
15169  // ten_k
15170  // <------>
15171  // <---- rest ---->
15172  // --------------[------------------+----+--------------]--------------
15173  // w V
15174  // = buf * 10^k
15175  //
15176  // ten_k represents a unit-in-the-last-place in the decimal representation
15177  // stored in buf.
15178  // Decrement buf by ten_k while this takes buf closer to w.
15179 
15180  // The tests are written in this order to avoid overflow in unsigned
15181  // integer arithmetic.
15182 
15183  while (rest < dist
15184  && delta - rest >= ten_k
15185  && (rest + ten_k < dist || dist - rest > rest + ten_k - dist))
15186  {
15187  JSON_ASSERT(buf[len - 1] != '0');
15188  buf[len - 1]--;
15189  rest += ten_k;
15190  }
15191 }
15192 
15197 inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent,
15198  diyfp M_minus, diyfp w, diyfp M_plus)
15199 {
15200  static_assert(kAlpha >= -60, "internal error");
15201  static_assert(kGamma <= -32, "internal error");
15202 
15203  // Generates the digits (and the exponent) of a decimal floating-point
15204  // number V = buffer * 10^decimal_exponent in the range [M-, M+]. The diyfp's
15205  // w, M- and M+ share the same exponent e, which satisfies alpha <= e <= gamma.
15206  //
15207  // <--------------------------- delta ---->
15208  // <---- dist --------->
15209  // --------------[------------------+-------------------]--------------
15210  // M- w M+
15211  //
15212  // Grisu2 generates the digits of M+ from left to right and stops as soon as
15213  // V is in [M-,M+].
15214 
15215  JSON_ASSERT(M_plus.e >= kAlpha);
15216  JSON_ASSERT(M_plus.e <= kGamma);
15217 
15218  std::uint64_t delta = diyfp::sub(M_plus, M_minus).f; // (significand of (M+ - M-), implicit exponent is e)
15219  std::uint64_t dist = diyfp::sub(M_plus, w ).f; // (significand of (M+ - w ), implicit exponent is e)
15220 
15221  // Split M+ = f * 2^e into two parts p1 and p2 (note: e < 0):
15222  //
15223  // M+ = f * 2^e
15224  // = ((f div 2^-e) * 2^-e + (f mod 2^-e)) * 2^e
15225  // = ((p1 ) * 2^-e + (p2 )) * 2^e
15226  // = p1 + p2 * 2^e
15227 
15228  const diyfp one(std::uint64_t{1} << -M_plus.e, M_plus.e);
15229 
15230  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.)
15231  std::uint64_t p2 = M_plus.f & (one.f - 1); // p2 = f mod 2^-e
15232 
15233  // 1)
15234  //
15235  // Generate the digits of the integral part p1 = d[n-1]...d[1]d[0]
15236 
15237  JSON_ASSERT(p1 > 0);
15238 
15239  std::uint32_t pow10;
15240  const int k = find_largest_pow10(p1, pow10);
15241 
15242  // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1)
15243  //
15244  // p1 = (p1 div 10^(k-1)) * 10^(k-1) + (p1 mod 10^(k-1))
15245  // = (d[k-1] ) * 10^(k-1) + (p1 mod 10^(k-1))
15246  //
15247  // M+ = p1 + p2 * 2^e
15248  // = d[k-1] * 10^(k-1) + (p1 mod 10^(k-1)) + p2 * 2^e
15249  // = d[k-1] * 10^(k-1) + ((p1 mod 10^(k-1)) * 2^-e + p2) * 2^e
15250  // = d[k-1] * 10^(k-1) + ( rest) * 2^e
15251  //
15252  // Now generate the digits d[n] of p1 from left to right (n = k-1,...,0)
15253  //
15254  // p1 = d[k-1]...d[n] * 10^n + d[n-1]...d[0]
15255  //
15256  // but stop as soon as
15257  //
15258  // rest * 2^e = (d[n-1]...d[0] * 2^-e + p2) * 2^e <= delta * 2^e
15259 
15260  int n = k;
15261  while (n > 0)
15262  {
15263  // Invariants:
15264  // M+ = buffer * 10^n + (p1 + p2 * 2^e) (buffer = 0 for n = k)
15265  // pow10 = 10^(n-1) <= p1 < 10^n
15266  //
15267  const std::uint32_t d = p1 / pow10; // d = p1 div 10^(n-1)
15268  const std::uint32_t r = p1 % pow10; // r = p1 mod 10^(n-1)
15269  //
15270  // M+ = buffer * 10^n + (d * 10^(n-1) + r) + p2 * 2^e
15271  // = (buffer * 10 + d) * 10^(n-1) + (r + p2 * 2^e)
15272  //
15273  JSON_ASSERT(d <= 9);
15274  buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
15275  //
15276  // M+ = buffer * 10^(n-1) + (r + p2 * 2^e)
15277  //
15278  p1 = r;
15279  n--;
15280  //
15281  // M+ = buffer * 10^n + (p1 + p2 * 2^e)
15282  // pow10 = 10^n
15283  //
15284 
15285  // Now check if enough digits have been generated.
15286  // Compute
15287  //
15288  // p1 + p2 * 2^e = (p1 * 2^-e + p2) * 2^e = rest * 2^e
15289  //
15290  // Note:
15291  // Since rest and delta share the same exponent e, it suffices to
15292  // compare the significands.
15293  const std::uint64_t rest = (std::uint64_t{p1} << -one.e) + p2;
15294  if (rest <= delta)
15295  {
15296  // V = buffer * 10^n, with M- <= V <= M+.
15297 
15298  decimal_exponent += n;
15299 
15300  // We may now just stop. But instead look if the buffer could be
15301  // decremented to bring V closer to w.
15302  //
15303  // pow10 = 10^n is now 1 ulp in the decimal representation V.
15304  // The rounding procedure works with diyfp's with an implicit
15305  // exponent of e.
15306  //
15307  // 10^n = (10^n * 2^-e) * 2^e = ulp * 2^e
15308  //
15309  const std::uint64_t ten_n = std::uint64_t{pow10} << -one.e;
15310  grisu2_round(buffer, length, dist, delta, rest, ten_n);
15311 
15312  return;
15313  }
15314 
15315  pow10 /= 10;
15316  //
15317  // pow10 = 10^(n-1) <= p1 < 10^n
15318  // Invariants restored.
15319  }
15320 
15321  // 2)
15322  //
15323  // The digits of the integral part have been generated:
15324  //
15325  // M+ = d[k-1]...d[1]d[0] + p2 * 2^e
15326  // = buffer + p2 * 2^e
15327  //
15328  // Now generate the digits of the fractional part p2 * 2^e.
15329  //
15330  // Note:
15331  // No decimal point is generated: the exponent is adjusted instead.
15332  //
15333  // p2 actually represents the fraction
15334  //
15335  // p2 * 2^e
15336  // = p2 / 2^-e
15337  // = d[-1] / 10^1 + d[-2] / 10^2 + ...
15338  //
15339  // Now generate the digits d[-m] of p1 from left to right (m = 1,2,...)
15340  //
15341  // p2 * 2^e = d[-1]d[-2]...d[-m] * 10^-m
15342  // + 10^-m * (d[-m-1] / 10^1 + d[-m-2] / 10^2 + ...)
15343  //
15344  // using
15345  //
15346  // 10^m * p2 = ((10^m * p2) div 2^-e) * 2^-e + ((10^m * p2) mod 2^-e)
15347  // = ( d) * 2^-e + ( r)
15348  //
15349  // or
15350  // 10^m * p2 * 2^e = d + r * 2^e
15351  //
15352  // i.e.
15353  //
15354  // M+ = buffer + p2 * 2^e
15355  // = buffer + 10^-m * (d + r * 2^e)
15356  // = (buffer * 10^m + d) * 10^-m + 10^-m * r * 2^e
15357  //
15358  // and stop as soon as 10^-m * r * 2^e <= delta * 2^e
15359 
15360  JSON_ASSERT(p2 > delta);
15361 
15362  int m = 0;
15363  for (;;)
15364  {
15365  // Invariant:
15366  // M+ = buffer * 10^-m + 10^-m * (d[-m-1] / 10 + d[-m-2] / 10^2 + ...) * 2^e
15367  // = buffer * 10^-m + 10^-m * (p2 ) * 2^e
15368  // = buffer * 10^-m + 10^-m * (1/10 * (10 * p2) ) * 2^e
15369  // = buffer * 10^-m + 10^-m * (1/10 * ((10*p2 div 2^-e) * 2^-e + (10*p2 mod 2^-e)) * 2^e
15370  //
15371  JSON_ASSERT(p2 <= (std::numeric_limits<std::uint64_t>::max)() / 10);
15372  p2 *= 10;
15373  const std::uint64_t d = p2 >> -one.e; // d = (10 * p2) div 2^-e
15374  const std::uint64_t r = p2 & (one.f - 1); // r = (10 * p2) mod 2^-e
15375  //
15376  // M+ = buffer * 10^-m + 10^-m * (1/10 * (d * 2^-e + r) * 2^e
15377  // = buffer * 10^-m + 10^-m * (1/10 * (d + r * 2^e))
15378  // = (buffer * 10 + d) * 10^(-m-1) + 10^(-m-1) * r * 2^e
15379  //
15380  JSON_ASSERT(d <= 9);
15381  buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
15382  //
15383  // M+ = buffer * 10^(-m-1) + 10^(-m-1) * r * 2^e
15384  //
15385  p2 = r;
15386  m++;
15387  //
15388  // M+ = buffer * 10^-m + 10^-m * p2 * 2^e
15389  // Invariant restored.
15390 
15391  // Check if enough digits have been generated.
15392  //
15393  // 10^-m * p2 * 2^e <= delta * 2^e
15394  // p2 * 2^e <= 10^m * delta * 2^e
15395  // p2 <= 10^m * delta
15396  delta *= 10;
15397  dist *= 10;
15398  if (p2 <= delta)
15399  {
15400  break;
15401  }
15402  }
15403 
15404  // V = buffer * 10^-m, with M- <= V <= M+.
15405 
15406  decimal_exponent -= m;
15407 
15408  // 1 ulp in the decimal representation is now 10^-m.
15409  // Since delta and dist are now scaled by 10^m, we need to do the
15410  // same with ulp in order to keep the units in sync.
15411  //
15412  // 10^m * 10^-m = 1 = 2^-e * 2^e = ten_m * 2^e
15413  //
15414  const std::uint64_t ten_m = one.f;
15415  grisu2_round(buffer, length, dist, delta, p2, ten_m);
15416 
15417  // By construction this algorithm generates the shortest possible decimal
15418  // number (Loitsch, Theorem 6.2) which rounds back to w.
15419  // For an input number of precision p, at least
15420  //
15421  // N = 1 + ceil(p * log_10(2))
15422  //
15423  // decimal digits are sufficient to identify all binary floating-point
15424  // numbers (Matula, "In-and-Out conversions").
15425  // This implies that the algorithm does not produce more than N decimal
15426  // digits.
15427  //
15428  // N = 17 for p = 53 (IEEE double precision)
15429  // N = 9 for p = 24 (IEEE single precision)
15430 }
15431 
15437 JSON_HEDLEY_NON_NULL(1)
15438 inline void grisu2(char* buf, int& len, int& decimal_exponent,
15439  diyfp m_minus, diyfp v, diyfp m_plus)
15440 {
15441  JSON_ASSERT(m_plus.e == m_minus.e);
15442  JSON_ASSERT(m_plus.e == v.e);
15443 
15444  // --------(-----------------------+-----------------------)-------- (A)
15445  // m- v m+
15446  //
15447  // --------------------(-----------+-----------------------)-------- (B)
15448  // m- v m+
15449  //
15450  // First scale v (and m- and m+) such that the exponent is in the range
15451  // [alpha, gamma].
15452 
15453  const cached_power cached = get_cached_power_for_binary_exponent(m_plus.e);
15454 
15455  const diyfp c_minus_k(cached.f, cached.e); // = c ~= 10^-k
15456 
15457  // The exponent of the products is = v.e + c_minus_k.e + q and is in the range [alpha,gamma]
15458  const diyfp w = diyfp::mul(v, c_minus_k);
15459  const diyfp w_minus = diyfp::mul(m_minus, c_minus_k);
15460  const diyfp w_plus = diyfp::mul(m_plus, c_minus_k);
15461 
15462  // ----(---+---)---------------(---+---)---------------(---+---)----
15463  // w- w w+
15464  // = c*m- = c*v = c*m+
15465  //
15466  // diyfp::mul rounds its result and c_minus_k is approximated too. w, w- and
15467  // w+ are now off by a small amount.
15468  // In fact:
15469  //
15470  // w - v * 10^k < 1 ulp
15471  //
15472  // To account for this inaccuracy, add resp. subtract 1 ulp.
15473  //
15474  // --------+---[---------------(---+---)---------------]---+--------
15475  // w- M- w M+ w+
15476  //
15477  // Now any number in [M-, M+] (bounds included) will round to w when input,
15478  // regardless of how the input rounding algorithm breaks ties.
15479  //
15480  // And digit_gen generates the shortest possible such number in [M-, M+].
15481  // Note that this does not mean that Grisu2 always generates the shortest
15482  // possible number in the interval (m-, m+).
15483  const diyfp M_minus(w_minus.f + 1, w_minus.e);
15484  const diyfp M_plus (w_plus.f - 1, w_plus.e );
15485 
15486  decimal_exponent = -cached.k; // = -(-k) = k
15487 
15488  grisu2_digit_gen(buf, len, decimal_exponent, M_minus, w, M_plus);
15489 }
15490 
15496 template<typename FloatType>
15497 JSON_HEDLEY_NON_NULL(1)
15498 void grisu2(char* buf, int& len, int& decimal_exponent, FloatType value)
15499 {
15500  static_assert(diyfp::kPrecision >= std::numeric_limits<FloatType>::digits + 3,
15501  "internal error: not enough precision");
15502 
15503  JSON_ASSERT(std::isfinite(value));
15504  JSON_ASSERT(value > 0);
15505 
15506  // If the neighbors (and boundaries) of 'value' are always computed for double-precision
15507  // numbers, all float's can be recovered using strtod (and strtof). However, the resulting
15508  // decimal representations are not exactly "short".
15509  //
15510  // The documentation for 'std::to_chars' (https://en.cppreference.com/w/cpp/utility/to_chars)
15511  // says "value is converted to a string as if by std::sprintf in the default ("C") locale"
15512  // and since sprintf promotes float's to double's, I think this is exactly what 'std::to_chars'
15513  // does.
15514  // On the other hand, the documentation for 'std::to_chars' requires that "parsing the
15515  // representation using the corresponding std::from_chars function recovers value exactly". That
15516  // indicates that single precision floating-point numbers should be recovered using
15517  // 'std::strtof'.
15518  //
15519  // NB: If the neighbors are computed for single-precision numbers, there is a single float
15520  // (7.0385307e-26f) which can't be recovered using strtod. The resulting double precision
15521  // value is off by 1 ulp.
15522 #if 0
15523  const boundaries w = compute_boundaries(static_cast<double>(value));
15524 #else
15525  const boundaries w = compute_boundaries(value);
15526 #endif
15527 
15528  grisu2(buf, len, decimal_exponent, w.minus, w.w, w.plus);
15529 }
15530 
15536 JSON_HEDLEY_NON_NULL(1)
15537 
15538 inline char* append_exponent(char* buf, int e)
15539 {
15540  JSON_ASSERT(e > -1000);
15541  JSON_ASSERT(e < 1000);
15542 
15543  if (e < 0)
15544  {
15545  e = -e;
15546  *buf++ = '-';
15547  }
15548  else
15549  {
15550  *buf++ = '+';
15551  }
15552 
15553  auto k = static_cast<std::uint32_t>(e);
15554  if (k < 10)
15555  {
15556  // Always print at least two digits in the exponent.
15557  // This is for compatibility with printf("%g").
15558  *buf++ = '0';
15559  *buf++ = static_cast<char>('0' + k);
15560  }
15561  else if (k < 100)
15562  {
15563  *buf++ = static_cast<char>('0' + k / 10);
15564  k %= 10;
15565  *buf++ = static_cast<char>('0' + k);
15566  }
15567  else
15568  {
15569  *buf++ = static_cast<char>('0' + k / 100);
15570  k %= 100;
15571  *buf++ = static_cast<char>('0' + k / 10);
15572  k %= 10;
15573  *buf++ = static_cast<char>('0' + k);
15574  }
15575 
15576  return buf;
15577 }
15578 
15588 JSON_HEDLEY_NON_NULL(1)
15589 
15590 inline char* format_buffer(char* buf, int len, int decimal_exponent,
15591  int min_exp, int max_exp)
15592 {
15593  JSON_ASSERT(min_exp < 0);
15594  JSON_ASSERT(max_exp > 0);
15595 
15596  const int k = len;
15597  const int n = len + decimal_exponent;
15598 
15599  // v = buf * 10^(n-k)
15600  // k is the length of the buffer (number of decimal digits)
15601  // n is the position of the decimal point relative to the start of the buffer.
15602 
15603  if (k <= n && n <= max_exp)
15604  {
15605  // digits[000]
15606  // len <= max_exp + 2
15607 
15608  std::memset(buf + k, '0', static_cast<size_t>(n) - static_cast<size_t>(k));
15609  // Make it look like a floating-point number (#362, #378)
15610  buf[n + 0] = '.';
15611  buf[n + 1] = '0';
15612  return buf + (static_cast<size_t>(n) + 2);
15613  }
15614 
15615  if (0 < n && n <= max_exp)
15616  {
15617  // dig.its
15618  // len <= max_digits10 + 1
15619 
15620  JSON_ASSERT(k > n);
15621 
15622  std::memmove(buf + (static_cast<size_t>(n) + 1), buf + n, static_cast<size_t>(k) - static_cast<size_t>(n));
15623  buf[n] = '.';
15624  return buf + (static_cast<size_t>(k) + 1U);
15625  }
15626 
15627  if (min_exp < n && n <= 0)
15628  {
15629  // 0.[000]digits
15630  // len <= 2 + (-min_exp - 1) + max_digits10
15631 
15632  std::memmove(buf + (2 + static_cast<size_t>(-n)), buf, static_cast<size_t>(k));
15633  buf[0] = '0';
15634  buf[1] = '.';
15635  std::memset(buf + 2, '0', static_cast<size_t>(-n));
15636  return buf + (2U + static_cast<size_t>(-n) + static_cast<size_t>(k));
15637  }
15638 
15639  if (k == 1)
15640  {
15641  // dE+123
15642  // len <= 1 + 5
15643 
15644  buf += 1;
15645  }
15646  else
15647  {
15648  // d.igitsE+123
15649  // len <= max_digits10 + 1 + 5
15650 
15651  std::memmove(buf + 2, buf + 1, static_cast<size_t>(k) - 1);
15652  buf[1] = '.';
15653  buf += 1 + static_cast<size_t>(k);
15654  }
15655 
15656  *buf++ = 'e';
15657  return append_exponent(buf, n - 1);
15658 }
15659 
15660 } // namespace dtoa_impl
15661 
15672 template<typename FloatType>
15673 JSON_HEDLEY_NON_NULL(1, 2)
15674 
15675 char* to_chars(char* first, const char* last, FloatType value)
15676 {
15677  static_cast<void>(last); // maybe unused - fix warning
15678  JSON_ASSERT(std::isfinite(value));
15679 
15680  // Use signbit(value) instead of (value < 0) since signbit works for -0.
15681  if (std::signbit(value))
15682  {
15683  value = -value;
15684  *first++ = '-';
15685  }
15686 
15687  if (value == 0) // +-0
15688  {
15689  *first++ = '0';
15690  // Make it look like a floating-point number (#362, #378)
15691  *first++ = '.';
15692  *first++ = '0';
15693  return first;
15694  }
15695 
15696  JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10);
15697 
15698  // Compute v = buffer * 10^decimal_exponent.
15699  // The decimal digits are stored in the buffer, which needs to be interpreted
15700  // as an unsigned decimal integer.
15701  // len is the length of the buffer, i.e. the number of decimal digits.
15702  int len = 0;
15703  int decimal_exponent = 0;
15704  dtoa_impl::grisu2(first, len, decimal_exponent, value);
15705 
15706  JSON_ASSERT(len <= std::numeric_limits<FloatType>::max_digits10);
15707 
15708  // Format the buffer like printf("%.*g", prec, value)
15709  constexpr int kMinExp = -4;
15710  // Use digits10 here to increase compatibility with version 2.
15711  constexpr int kMaxExp = std::numeric_limits<FloatType>::digits10;
15712 
15713  JSON_ASSERT(last - first >= kMaxExp + 2);
15714  JSON_ASSERT(last - first >= 2 + (-kMinExp - 1) + std::numeric_limits<FloatType>::max_digits10);
15715  JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10 + 6);
15716 
15717  return dtoa_impl::format_buffer(first, len, decimal_exponent, kMinExp, kMaxExp);
15718 }
15719 
15720 } // namespace detail
15721 } // namespace nlohmann
15722 
15723 // #include <nlohmann/detail/exceptions.hpp>
15724 
15725 // #include <nlohmann/detail/macro_scope.hpp>
15726 
15727 // #include <nlohmann/detail/meta/cpp_future.hpp>
15728 
15729 // #include <nlohmann/detail/output/binary_writer.hpp>
15730 
15731 // #include <nlohmann/detail/output/output_adapters.hpp>
15732 
15733 // #include <nlohmann/detail/value_t.hpp>
15734 
15735 
15736 namespace nlohmann
15737 {
15738 namespace detail
15739 {
15741 // serialization //
15743 
15745 enum class error_handler_t
15746 {
15747  strict,
15748  replace,
15749  ignore
15750 };
15751 
15752 template<typename BasicJsonType>
15753 class serializer
15754 {
15755  using string_t = typename BasicJsonType::string_t;
15756  using number_float_t = typename BasicJsonType::number_float_t;
15757  using number_integer_t = typename BasicJsonType::number_integer_t;
15758  using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
15759  using binary_char_t = typename BasicJsonType::binary_t::value_type;
15760  static constexpr std::uint8_t UTF8_ACCEPT = 0;
15761  static constexpr std::uint8_t UTF8_REJECT = 1;
15762 
15763  public:
15769  serializer(output_adapter_t<char> s, const char ichar,
15770  error_handler_t error_handler_ = error_handler_t::strict)
15771  : o(std::move(s))
15772  , loc(std::localeconv())
15773  , thousands_sep(loc->thousands_sep == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->thousands_sep)))
15774  , decimal_point(loc->decimal_point == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->decimal_point)))
15775  , indent_char(ichar)
15776  , indent_string(512, indent_char)
15777  , error_handler(error_handler_)
15778  {}
15779 
15780  // delete because of pointer members
15781  serializer(const serializer&) = delete;
15782  serializer& operator=(const serializer&) = delete;
15783  serializer(serializer&&) = delete;
15784  serializer& operator=(serializer&&) = delete;
15785  ~serializer() = default;
15786 
15809  void dump(const BasicJsonType& val,
15810  const bool pretty_print,
15811  const bool ensure_ascii,
15812  const unsigned int indent_step,
15813  const unsigned int current_indent = 0)
15814  {
15815  switch (val.m_type)
15816  {
15817  case value_t::object:
15818  {
15819  if (val.m_value.object->empty())
15820  {
15821  o->write_characters("{}", 2);
15822  return;
15823  }
15824 
15825  if (pretty_print)
15826  {
15827  o->write_characters("{\n", 2);
15828 
15829  // variable to hold indentation for recursive calls
15830  const auto new_indent = current_indent + indent_step;
15831  if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
15832  {
15833  indent_string.resize(indent_string.size() * 2, ' ');
15834  }
15835 
15836  // first n-1 elements
15837  auto i = val.m_value.object->cbegin();
15838  for (std::size_t cnt = 0; cnt < val.m_value.object->size() - 1; ++cnt, ++i)
15839  {
15840  o->write_characters(indent_string.c_str(), new_indent);
15841  o->write_character('\"');
15842  dump_escaped(i->first, ensure_ascii);
15843  o->write_characters("\": ", 3);
15844  dump(i->second, true, ensure_ascii, indent_step, new_indent);
15845  o->write_characters(",\n", 2);
15846  }
15847 
15848  // last element
15849  JSON_ASSERT(i != val.m_value.object->cend());
15850  JSON_ASSERT(std::next(i) == val.m_value.object->cend());
15851  o->write_characters(indent_string.c_str(), new_indent);
15852  o->write_character('\"');
15853  dump_escaped(i->first, ensure_ascii);
15854  o->write_characters("\": ", 3);
15855  dump(i->second, true, ensure_ascii, indent_step, new_indent);
15856 
15857  o->write_character('\n');
15858  o->write_characters(indent_string.c_str(), current_indent);
15859  o->write_character('}');
15860  }
15861  else
15862  {
15863  o->write_character('{');
15864 
15865  // first n-1 elements
15866  auto i = val.m_value.object->cbegin();
15867  for (std::size_t cnt = 0; cnt < val.m_value.object->size() - 1; ++cnt, ++i)
15868  {
15869  o->write_character('\"');
15870  dump_escaped(i->first, ensure_ascii);
15871  o->write_characters("\":", 2);
15872  dump(i->second, false, ensure_ascii, indent_step, current_indent);
15873  o->write_character(',');
15874  }
15875 
15876  // last element
15877  JSON_ASSERT(i != val.m_value.object->cend());
15878  JSON_ASSERT(std::next(i) == val.m_value.object->cend());
15879  o->write_character('\"');
15880  dump_escaped(i->first, ensure_ascii);
15881  o->write_characters("\":", 2);
15882  dump(i->second, false, ensure_ascii, indent_step, current_indent);
15883 
15884  o->write_character('}');
15885  }
15886 
15887  return;
15888  }
15889 
15890  case value_t::array:
15891  {
15892  if (val.m_value.array->empty())
15893  {
15894  o->write_characters("[]", 2);
15895  return;
15896  }
15897 
15898  if (pretty_print)
15899  {
15900  o->write_characters("[\n", 2);
15901 
15902  // variable to hold indentation for recursive calls
15903  const auto new_indent = current_indent + indent_step;
15904  if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
15905  {
15906  indent_string.resize(indent_string.size() * 2, ' ');
15907  }
15908 
15909  // first n-1 elements
15910  for (auto i = val.m_value.array->cbegin();
15911  i != val.m_value.array->cend() - 1; ++i)
15912  {
15913  o->write_characters(indent_string.c_str(), new_indent);
15914  dump(*i, true, ensure_ascii, indent_step, new_indent);
15915  o->write_characters(",\n", 2);
15916  }
15917 
15918  // last element
15919  JSON_ASSERT(!val.m_value.array->empty());
15920  o->write_characters(indent_string.c_str(), new_indent);
15921  dump(val.m_value.array->back(), true, ensure_ascii, indent_step, new_indent);
15922 
15923  o->write_character('\n');
15924  o->write_characters(indent_string.c_str(), current_indent);
15925  o->write_character(']');
15926  }
15927  else
15928  {
15929  o->write_character('[');
15930 
15931  // first n-1 elements
15932  for (auto i = val.m_value.array->cbegin();
15933  i != val.m_value.array->cend() - 1; ++i)
15934  {
15935  dump(*i, false, ensure_ascii, indent_step, current_indent);
15936  o->write_character(',');
15937  }
15938 
15939  // last element
15940  JSON_ASSERT(!val.m_value.array->empty());
15941  dump(val.m_value.array->back(), false, ensure_ascii, indent_step, current_indent);
15942 
15943  o->write_character(']');
15944  }
15945 
15946  return;
15947  }
15948 
15949  case value_t::string:
15950  {
15951  o->write_character('\"');
15952  dump_escaped(*val.m_value.string, ensure_ascii);
15953  o->write_character('\"');
15954  return;
15955  }
15956 
15957  case value_t::binary:
15958  {
15959  if (pretty_print)
15960  {
15961  o->write_characters("{\n", 2);
15962 
15963  // variable to hold indentation for recursive calls
15964  const auto new_indent = current_indent + indent_step;
15965  if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent))
15966  {
15967  indent_string.resize(indent_string.size() * 2, ' ');
15968  }
15969 
15970  o->write_characters(indent_string.c_str(), new_indent);
15971 
15972  o->write_characters("\"bytes\": [", 10);
15973 
15974  if (!val.m_value.binary->empty())
15975  {
15976  for (auto i = val.m_value.binary->cbegin();
15977  i != val.m_value.binary->cend() - 1; ++i)
15978  {
15979  dump_integer(*i);
15980  o->write_characters(", ", 2);
15981  }
15982  dump_integer(val.m_value.binary->back());
15983  }
15984 
15985  o->write_characters("],\n", 3);
15986  o->write_characters(indent_string.c_str(), new_indent);
15987 
15988  o->write_characters("\"subtype\": ", 11);
15989  if (val.m_value.binary->has_subtype())
15990  {
15991  dump_integer(val.m_value.binary->subtype());
15992  }
15993  else
15994  {
15995  o->write_characters("null", 4);
15996  }
15997  o->write_character('\n');
15998  o->write_characters(indent_string.c_str(), current_indent);
15999  o->write_character('}');
16000  }
16001  else
16002  {
16003  o->write_characters("{\"bytes\":[", 10);
16004 
16005  if (!val.m_value.binary->empty())
16006  {
16007  for (auto i = val.m_value.binary->cbegin();
16008  i != val.m_value.binary->cend() - 1; ++i)
16009  {
16010  dump_integer(*i);
16011  o->write_character(',');
16012  }
16013  dump_integer(val.m_value.binary->back());
16014  }
16015 
16016  o->write_characters("],\"subtype\":", 12);
16017  if (val.m_value.binary->has_subtype())
16018  {
16019  dump_integer(val.m_value.binary->subtype());
16020  o->write_character('}');
16021  }
16022  else
16023  {
16024  o->write_characters("null}", 5);
16025  }
16026  }
16027  return;
16028  }
16029 
16030  case value_t::boolean:
16031  {
16032  if (val.m_value.boolean)
16033  {
16034  o->write_characters("true", 4);
16035  }
16036  else
16037  {
16038  o->write_characters("false", 5);
16039  }
16040  return;
16041  }
16042 
16043  case value_t::number_integer:
16044  {
16045  dump_integer(val.m_value.number_integer);
16046  return;
16047  }
16048 
16049  case value_t::number_unsigned:
16050  {
16051  dump_integer(val.m_value.number_unsigned);
16052  return;
16053  }
16054 
16055  case value_t::number_float:
16056  {
16057  dump_float(val.m_value.number_float);
16058  return;
16059  }
16060 
16061  case value_t::discarded:
16062  {
16063  o->write_characters("<discarded>", 11);
16064  return;
16065  }
16066 
16067  case value_t::null:
16068  {
16069  o->write_characters("null", 4);
16070  return;
16071  }
16072 
16073  default: // LCOV_EXCL_LINE
16074  JSON_ASSERT(false); // LCOV_EXCL_LINE
16075  }
16076  }
16077 
16078  JSON_PRIVATE_UNLESS_TESTED:
16093  void dump_escaped(const string_t& s, const bool ensure_ascii)
16094  {
16095  std::uint32_t codepoint;
16096  std::uint8_t state = UTF8_ACCEPT;
16097  std::size_t bytes = 0; // number of bytes written to string_buffer
16098 
16099  // number of bytes written at the point of the last valid byte
16100  std::size_t bytes_after_last_accept = 0;
16101  std::size_t undumped_chars = 0;
16102 
16103  for (std::size_t i = 0; i < s.size(); ++i)
16104  {
16105  const auto byte = static_cast<uint8_t>(s[i]);
16106 
16107  switch (decode(state, codepoint, byte))
16108  {
16109  case UTF8_ACCEPT: // decode found a new code point
16110  {
16111  switch (codepoint)
16112  {
16113  case 0x08: // backspace
16114  {
16115  string_buffer[bytes++] = '\\';
16116  string_buffer[bytes++] = 'b';
16117  break;
16118  }
16119 
16120  case 0x09: // horizontal tab
16121  {
16122  string_buffer[bytes++] = '\\';
16123  string_buffer[bytes++] = 't';
16124  break;
16125  }
16126 
16127  case 0x0A: // newline
16128  {
16129  string_buffer[bytes++] = '\\';
16130  string_buffer[bytes++] = 'n';
16131  break;
16132  }
16133 
16134  case 0x0C: // formfeed
16135  {
16136  string_buffer[bytes++] = '\\';
16137  string_buffer[bytes++] = 'f';
16138  break;
16139  }
16140 
16141  case 0x0D: // carriage return
16142  {
16143  string_buffer[bytes++] = '\\';
16144  string_buffer[bytes++] = 'r';
16145  break;
16146  }
16147 
16148  case 0x22: // quotation mark
16149  {
16150  string_buffer[bytes++] = '\\';
16151  string_buffer[bytes++] = '\"';
16152  break;
16153  }
16154 
16155  case 0x5C: // reverse solidus
16156  {
16157  string_buffer[bytes++] = '\\';
16158  string_buffer[bytes++] = '\\';
16159  break;
16160  }
16161 
16162  default:
16163  {
16164  // escape control characters (0x00..0x1F) or, if
16165  // ensure_ascii parameter is used, non-ASCII characters
16166  if ((codepoint <= 0x1F) || (ensure_ascii && (codepoint >= 0x7F)))
16167  {
16168  if (codepoint <= 0xFFFF)
16169  {
16170  (std::snprintf)(string_buffer.data() + bytes, 7, "\\u%04x",
16171  static_cast<std::uint16_t>(codepoint));
16172  bytes += 6;
16173  }
16174  else
16175  {
16176  (std::snprintf)(string_buffer.data() + bytes, 13, "\\u%04x\\u%04x",
16177  static_cast<std::uint16_t>(0xD7C0u + (codepoint >> 10u)),
16178  static_cast<std::uint16_t>(0xDC00u + (codepoint & 0x3FFu)));
16179  bytes += 12;
16180  }
16181  }
16182  else
16183  {
16184  // copy byte to buffer (all previous bytes
16185  // been copied have in default case above)
16186  string_buffer[bytes++] = s[i];
16187  }
16188  break;
16189  }
16190  }
16191 
16192  // write buffer and reset index; there must be 13 bytes
16193  // left, as this is the maximal number of bytes to be
16194  // written ("\uxxxx\uxxxx\0") for one code point
16195  if (string_buffer.size() - bytes < 13)
16196  {
16197  o->write_characters(string_buffer.data(), bytes);
16198  bytes = 0;
16199  }
16200 
16201  // remember the byte position of this accept
16202  bytes_after_last_accept = bytes;
16203  undumped_chars = 0;
16204  break;
16205  }
16206 
16207  case UTF8_REJECT: // decode found invalid UTF-8 byte
16208  {
16209  switch (error_handler)
16210  {
16211  case error_handler_t::strict:
16212  {
16213  std::string sn(3, '\0');
16214  (std::snprintf)(&sn[0], sn.size(), "%.2X", byte);
16215  JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn, BasicJsonType()));
16216  }
16217 
16218  case error_handler_t::ignore:
16219  case error_handler_t::replace:
16220  {
16221  // in case we saw this character the first time, we
16222  // would like to read it again, because the byte
16223  // may be OK for itself, but just not OK for the
16224  // previous sequence
16225  if (undumped_chars > 0)
16226  {
16227  --i;
16228  }
16229 
16230  // reset length buffer to the last accepted index;
16231  // thus removing/ignoring the invalid characters
16232  bytes = bytes_after_last_accept;
16233 
16234  if (error_handler == error_handler_t::replace)
16235  {
16236  // add a replacement character
16237  if (ensure_ascii)
16238  {
16239  string_buffer[bytes++] = '\\';
16240  string_buffer[bytes++] = 'u';
16241  string_buffer[bytes++] = 'f';
16242  string_buffer[bytes++] = 'f';
16243  string_buffer[bytes++] = 'f';
16244  string_buffer[bytes++] = 'd';
16245  }
16246  else
16247  {
16248  string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xEF');
16249  string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBF');
16250  string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBD');
16251  }
16252 
16253  // write buffer and reset index; there must be 13 bytes
16254  // left, as this is the maximal number of bytes to be
16255  // written ("\uxxxx\uxxxx\0") for one code point
16256  if (string_buffer.size() - bytes < 13)
16257  {
16258  o->write_characters(string_buffer.data(), bytes);
16259  bytes = 0;
16260  }
16261 
16262  bytes_after_last_accept = bytes;
16263  }
16264 
16265  undumped_chars = 0;
16266 
16267  // continue processing the string
16268  state = UTF8_ACCEPT;
16269  break;
16270  }
16271 
16272  default: // LCOV_EXCL_LINE
16273  JSON_ASSERT(false); // LCOV_EXCL_LINE
16274  }
16275  break;
16276  }
16277 
16278  default: // decode found yet incomplete multi-byte code point
16279  {
16280  if (!ensure_ascii)
16281  {
16282  // code point will not be escaped - copy byte to buffer
16283  string_buffer[bytes++] = s[i];
16284  }
16285  ++undumped_chars;
16286  break;
16287  }
16288  }
16289  }
16290 
16291  // we finished processing the string
16292  if (JSON_HEDLEY_LIKELY(state == UTF8_ACCEPT))
16293  {
16294  // write buffer
16295  if (bytes > 0)
16296  {
16297  o->write_characters(string_buffer.data(), bytes);
16298  }
16299  }
16300  else
16301  {
16302  // we finish reading, but do not accept: string was incomplete
16303  switch (error_handler)
16304  {
16305  case error_handler_t::strict:
16306  {
16307  std::string sn(3, '\0');
16308  (std::snprintf)(&sn[0], sn.size(), "%.2X", static_cast<std::uint8_t>(s.back()));
16309  JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + sn, BasicJsonType()));
16310  }
16311 
16312  case error_handler_t::ignore:
16313  {
16314  // write all accepted bytes
16315  o->write_characters(string_buffer.data(), bytes_after_last_accept);
16316  break;
16317  }
16318 
16319  case error_handler_t::replace:
16320  {
16321  // write all accepted bytes
16322  o->write_characters(string_buffer.data(), bytes_after_last_accept);
16323  // add a replacement character
16324  if (ensure_ascii)
16325  {
16326  o->write_characters("\\ufffd", 6);
16327  }
16328  else
16329  {
16330  o->write_characters("\xEF\xBF\xBD", 3);
16331  }
16332  break;
16333  }
16334 
16335  default: // LCOV_EXCL_LINE
16336  JSON_ASSERT(false); // LCOV_EXCL_LINE
16337  }
16338  }
16339  }
16340 
16341  private:
16350  inline unsigned int count_digits(number_unsigned_t x) noexcept
16351  {
16352  unsigned int n_digits = 1;
16353  for (;;)
16354  {
16355  if (x < 10)
16356  {
16357  return n_digits;
16358  }
16359  if (x < 100)
16360  {
16361  return n_digits + 1;
16362  }
16363  if (x < 1000)
16364  {
16365  return n_digits + 2;
16366  }
16367  if (x < 10000)
16368  {
16369  return n_digits + 3;
16370  }
16371  x = x / 10000u;
16372  n_digits += 4;
16373  }
16374  }
16375 
16385  template < typename NumberType, detail::enable_if_t <
16386  std::is_same<NumberType, number_unsigned_t>::value ||
16387  std::is_same<NumberType, number_integer_t>::value ||
16388  std::is_same<NumberType, binary_char_t>::value,
16389  int > = 0 >
16390  void dump_integer(NumberType x)
16391  {
16392  static constexpr std::array<std::array<char, 2>, 100> digits_to_99
16393  {
16394  {
16395  {{'0', '0'}}, {{'0', '1'}}, {{'0', '2'}}, {{'0', '3'}}, {{'0', '4'}}, {{'0', '5'}}, {{'0', '6'}}, {{'0', '7'}}, {{'0', '8'}}, {{'0', '9'}},
16396  {{'1', '0'}}, {{'1', '1'}}, {{'1', '2'}}, {{'1', '3'}}, {{'1', '4'}}, {{'1', '5'}}, {{'1', '6'}}, {{'1', '7'}}, {{'1', '8'}}, {{'1', '9'}},
16397  {{'2', '0'}}, {{'2', '1'}}, {{'2', '2'}}, {{'2', '3'}}, {{'2', '4'}}, {{'2', '5'}}, {{'2', '6'}}, {{'2', '7'}}, {{'2', '8'}}, {{'2', '9'}},
16398  {{'3', '0'}}, {{'3', '1'}}, {{'3', '2'}}, {{'3', '3'}}, {{'3', '4'}}, {{'3', '5'}}, {{'3', '6'}}, {{'3', '7'}}, {{'3', '8'}}, {{'3', '9'}},
16399  {{'4', '0'}}, {{'4', '1'}}, {{'4', '2'}}, {{'4', '3'}}, {{'4', '4'}}, {{'4', '5'}}, {{'4', '6'}}, {{'4', '7'}}, {{'4', '8'}}, {{'4', '9'}},
16400  {{'5', '0'}}, {{'5', '1'}}, {{'5', '2'}}, {{'5', '3'}}, {{'5', '4'}}, {{'5', '5'}}, {{'5', '6'}}, {{'5', '7'}}, {{'5', '8'}}, {{'5', '9'}},
16401  {{'6', '0'}}, {{'6', '1'}}, {{'6', '2'}}, {{'6', '3'}}, {{'6', '4'}}, {{'6', '5'}}, {{'6', '6'}}, {{'6', '7'}}, {{'6', '8'}}, {{'6', '9'}},
16402  {{'7', '0'}}, {{'7', '1'}}, {{'7', '2'}}, {{'7', '3'}}, {{'7', '4'}}, {{'7', '5'}}, {{'7', '6'}}, {{'7', '7'}}, {{'7', '8'}}, {{'7', '9'}},
16403  {{'8', '0'}}, {{'8', '1'}}, {{'8', '2'}}, {{'8', '3'}}, {{'8', '4'}}, {{'8', '5'}}, {{'8', '6'}}, {{'8', '7'}}, {{'8', '8'}}, {{'8', '9'}},
16404  {{'9', '0'}}, {{'9', '1'}}, {{'9', '2'}}, {{'9', '3'}}, {{'9', '4'}}, {{'9', '5'}}, {{'9', '6'}}, {{'9', '7'}}, {{'9', '8'}}, {{'9', '9'}},
16405  }
16406  };
16407 
16408  // special case for "0"
16409  if (x == 0)
16410  {
16411  o->write_character('0');
16412  return;
16413  }
16414 
16415  // use a pointer to fill the buffer
16416  auto buffer_ptr = number_buffer.begin();
16417 
16418  const bool is_negative = std::is_same<NumberType, number_integer_t>::value && !(x >= 0); // see issue #755
16419  number_unsigned_t abs_value;
16420 
16421  unsigned int n_chars;
16422 
16423  if (is_negative)
16424  {
16425  *buffer_ptr = '-';
16426  abs_value = remove_sign(static_cast<number_integer_t>(x));
16427 
16428  // account one more byte for the minus sign
16429  n_chars = 1 + count_digits(abs_value);
16430  }
16431  else
16432  {
16433  abs_value = static_cast<number_unsigned_t>(x);
16434  n_chars = count_digits(abs_value);
16435  }
16436 
16437  // spare 1 byte for '\0'
16438  JSON_ASSERT(n_chars < number_buffer.size() - 1);
16439 
16440  // jump to the end to generate the string from backward
16441  // so we later avoid reversing the result
16442  buffer_ptr += n_chars;
16443 
16444  // Fast int2ascii implementation inspired by "Fastware" talk by Andrei Alexandrescu
16445  // See: https://www.youtube.com/watch?v=o4-CwDo2zpg
16446  while (abs_value >= 100)
16447  {
16448  const auto digits_index = static_cast<unsigned>((abs_value % 100));
16449  abs_value /= 100;
16450  *(--buffer_ptr) = digits_to_99[digits_index][1];
16451  *(--buffer_ptr) = digits_to_99[digits_index][0];
16452  }
16453 
16454  if (abs_value >= 10)
16455  {
16456  const auto digits_index = static_cast<unsigned>(abs_value);
16457  *(--buffer_ptr) = digits_to_99[digits_index][1];
16458  *(--buffer_ptr) = digits_to_99[digits_index][0];
16459  }
16460  else
16461  {
16462  *(--buffer_ptr) = static_cast<char>('0' + abs_value);
16463  }
16464 
16465  o->write_characters(number_buffer.data(), n_chars);
16466  }
16467 
16476  void dump_float(number_float_t x)
16477  {
16478  // NaN / inf
16479  if (!std::isfinite(x))
16480  {
16481  o->write_characters("null", 4);
16482  return;
16483  }
16484 
16485  // If number_float_t is an IEEE-754 single or double precision number,
16486  // use the Grisu2 algorithm to produce short numbers which are
16487  // guaranteed to round-trip, using strtof and strtod, resp.
16488  //
16489  // NB: The test below works if <long double> == <double>.
16490  static constexpr bool is_ieee_single_or_double
16491  = (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) ||
16492  (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);
16493 
16494  dump_float(x, std::integral_constant<bool, is_ieee_single_or_double>());
16495  }
16496 
16497  void dump_float(number_float_t x, std::true_type /*is_ieee_single_or_double*/)
16498  {
16499  char* begin = number_buffer.data();
16500  char* end = ::nlohmann::detail::to_chars(begin, begin + number_buffer.size(), x);
16501 
16502  o->write_characters(begin, static_cast<size_t>(end - begin));
16503  }
16504 
16505  void dump_float(number_float_t x, std::false_type /*is_ieee_single_or_double*/)
16506  {
16507  // get number of digits for a float -> text -> float round-trip
16508  static constexpr auto d = std::numeric_limits<number_float_t>::max_digits10;
16509 
16510  // the actual conversion
16511  std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*g", d, x);
16512 
16513  // negative value indicates an error
16514  JSON_ASSERT(len > 0);
16515  // check if buffer was large enough
16516  JSON_ASSERT(static_cast<std::size_t>(len) < number_buffer.size());
16517 
16518  // erase thousands separator
16519  if (thousands_sep != '\0')
16520  {
16521  const auto end = std::remove(number_buffer.begin(),
16522  number_buffer.begin() + len, thousands_sep);
16523  std::fill(end, number_buffer.end(), '\0');
16524  JSON_ASSERT((end - number_buffer.begin()) <= len);
16525  len = (end - number_buffer.begin());
16526  }
16527 
16528  // convert decimal point to '.'
16529  if (decimal_point != '\0' && decimal_point != '.')
16530  {
16531  const auto dec_pos = std::find(number_buffer.begin(), number_buffer.end(), decimal_point);
16532  if (dec_pos != number_buffer.end())
16533  {
16534  *dec_pos = '.';
16535  }
16536  }
16537 
16538  o->write_characters(number_buffer.data(), static_cast<std::size_t>(len));
16539 
16540  // determine if need to append ".0"
16541  const bool value_is_int_like =
16542  std::none_of(number_buffer.begin(), number_buffer.begin() + len + 1,
16543  [](char c)
16544  {
16545  return c == '.' || c == 'e';
16546  });
16547 
16548  if (value_is_int_like)
16549  {
16550  o->write_characters(".0", 2);
16551  }
16552  }
16553 
16575  static std::uint8_t decode(std::uint8_t& state, std::uint32_t& codep, const std::uint8_t byte) noexcept
16576  {
16577  static const std::array<std::uint8_t, 400> utf8d =
16578  {
16579  {
16580  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
16581  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
16582  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
16583  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
16584  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
16585  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
16586  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
16587  0xA, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3, // E0..EF
16588  0xB, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, // F0..FF
16589  0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, 0x6, 0x1, 0x1, 0x1, 0x1, // s0..s0
16590  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
16591  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
16592  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
16593  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
16594  }
16595  };
16596 
16597  JSON_ASSERT(byte < utf8d.size());
16598  const std::uint8_t type = utf8d[byte];
16599 
16600  codep = (state != UTF8_ACCEPT)
16601  ? (byte & 0x3fu) | (codep << 6u)
16602  : (0xFFu >> type) & (byte);
16603 
16604  std::size_t index = 256u + static_cast<size_t>(state) * 16u + static_cast<size_t>(type);
16605  JSON_ASSERT(index < 400);
16606  state = utf8d[index];
16607  return state;
16608  }
16609 
16610  /*
16611  * Overload to make the compiler happy while it is instantiating
16612  * dump_integer for number_unsigned_t.
16613  * Must never be called.
16614  */
16615  number_unsigned_t remove_sign(number_unsigned_t x)
16616  {
16617  JSON_ASSERT(false); // LCOV_EXCL_LINE
16618  return x; // LCOV_EXCL_LINE
16619  }
16620 
16621  /*
16622  * Helper function for dump_integer
16623  *
16624  * This function takes a negative signed integer and returns its absolute
16625  * value as unsigned integer. The plus/minus shuffling is necessary as we can
16626  * not directly remove the sign of an arbitrary signed integer as the
16627  * absolute values of INT_MIN and INT_MAX are usually not the same. See
16628  * #1708 for details.
16629  */
16630  inline number_unsigned_t remove_sign(number_integer_t x) noexcept
16631  {
16632  JSON_ASSERT(x < 0 && x < (std::numeric_limits<number_integer_t>::max)());
16633  return static_cast<number_unsigned_t>(-(x + 1)) + 1;
16634  }
16635 
16636  private:
16638  output_adapter_t<char> o = nullptr;
16639 
16641  std::array<char, 64> number_buffer{{}};
16642 
16644  const std::lconv* loc = nullptr;
16646  const char thousands_sep = '\0';
16648  const char decimal_point = '\0';
16649 
16651  std::array<char, 512> string_buffer{{}};
16652 
16654  const char indent_char;
16656  string_t indent_string;
16657 
16659  const error_handler_t error_handler;
16660 };
16661 } // namespace detail
16662 } // namespace nlohmann
16663 
16664 // #include <nlohmann/detail/value_t.hpp>
16665 
16666 // #include <nlohmann/json_fwd.hpp>
16667 
16668 // #include <nlohmann/ordered_map.hpp>
16669 
16670 
16671 #include <functional> // less
16672 #include <memory> // allocator
16673 #include <utility> // pair
16674 #include <vector> // vector
16675 
16676 // #include <nlohmann/detail/macro_scope.hpp>
16677 
16678 
16679 namespace nlohmann
16680 {
16681 
16684 template <class Key, class T, class IgnoredLess = std::less<Key>,
16685  class Allocator = std::allocator<std::pair<const Key, T>>>
16686  struct ordered_map : std::vector<std::pair<const Key, T>, Allocator>
16687 {
16688  using key_type = Key;
16689  using mapped_type = T;
16690  using Container = std::vector<std::pair<const Key, T>, Allocator>;
16691  using typename Container::iterator;
16692  using typename Container::const_iterator;
16693  using typename Container::size_type;
16694  using typename Container::value_type;
16695 
16696  // Explicit constructors instead of `using Container::Container`
16697  // otherwise older compilers choke on it (GCC <= 5.5, xcode <= 9.4)
16698  ordered_map(const Allocator& alloc = Allocator()) : Container{alloc} {}
16699  template <class It>
16700  ordered_map(It first, It last, const Allocator& alloc = Allocator())
16701  : Container{first, last, alloc} {}
16702  ordered_map(std::initializer_list<T> init, const Allocator& alloc = Allocator() )
16703  : Container{init, alloc} {}
16704 
16705  std::pair<iterator, bool> emplace(const key_type& key, T&& t)
16706  {
16707  for (auto it = this->begin(); it != this->end(); ++it)
16708  {
16709  if (it->first == key)
16710  {
16711  return {it, false};
16712  }
16713  }
16714  Container::emplace_back(key, t);
16715  return {--this->end(), true};
16716  }
16717 
16718  T& operator[](const Key& key)
16719  {
16720  return emplace(key, T{}).first->second;
16721  }
16722 
16723  const T& operator[](const Key& key) const
16724  {
16725  return at(key);
16726  }
16727 
16728  T& at(const Key& key)
16729  {
16730  for (auto it = this->begin(); it != this->end(); ++it)
16731  {
16732  if (it->first == key)
16733  {
16734  return it->second;
16735  }
16736  }
16737 
16738  JSON_THROW(std::out_of_range("key not found"));
16739  }
16740 
16741  const T& at(const Key& key) const
16742  {
16743  for (auto it = this->begin(); it != this->end(); ++it)
16744  {
16745  if (it->first == key)
16746  {
16747  return it->second;
16748  }
16749  }
16750 
16751  JSON_THROW(std::out_of_range("key not found"));
16752  }
16753 
16754  size_type erase(const Key& key)
16755  {
16756  for (auto it = this->begin(); it != this->end(); ++it)
16757  {
16758  if (it->first == key)
16759  {
16760  // Since we cannot move const Keys, re-construct them in place
16761  for (auto next = it; ++next != this->end(); ++it)
16762  {
16763  it->~value_type(); // Destroy but keep allocation
16764  new (&*it) value_type{std::move(*next)};
16765  }
16766  Container::pop_back();
16767  return 1;
16768  }
16769  }
16770  return 0;
16771  }
16772 
16773  iterator erase(iterator pos)
16774  {
16775  auto it = pos;
16776 
16777  // Since we cannot move const Keys, re-construct them in place
16778  for (auto next = it; ++next != this->end(); ++it)
16779  {
16780  it->~value_type(); // Destroy but keep allocation
16781  new (&*it) value_type{std::move(*next)};
16782  }
16783  Container::pop_back();
16784  return pos;
16785  }
16786 
16787  size_type count(const Key& key) const
16788  {
16789  for (auto it = this->begin(); it != this->end(); ++it)
16790  {
16791  if (it->first == key)
16792  {
16793  return 1;
16794  }
16795  }
16796  return 0;
16797  }
16798 
16799  iterator find(const Key& key)
16800  {
16801  for (auto it = this->begin(); it != this->end(); ++it)
16802  {
16803  if (it->first == key)
16804  {
16805  return it;
16806  }
16807  }
16808  return Container::end();
16809  }
16810 
16811  const_iterator find(const Key& key) const
16812  {
16813  for (auto it = this->begin(); it != this->end(); ++it)
16814  {
16815  if (it->first == key)
16816  {
16817  return it;
16818  }
16819  }
16820  return Container::end();
16821  }
16822 
16823  std::pair<iterator, bool> insert( value_type&& value )
16824  {
16825  return emplace(value.first, std::move(value.second));
16826  }
16827 
16828  std::pair<iterator, bool> insert( const value_type& value )
16829  {
16830  for (auto it = this->begin(); it != this->end(); ++it)
16831  {
16832  if (it->first == value.first)
16833  {
16834  return {it, false};
16835  }
16836  }
16837  Container::push_back(value);
16838  return {--this->end(), true};
16839  }
16840 
16841  template<typename InputIt>
16842  using require_input_iter = typename std::enable_if<std::is_convertible<typename std::iterator_traits<InputIt>::iterator_category,
16843  std::input_iterator_tag>::value>::type;
16844 
16845  template<typename InputIt, typename = require_input_iter<InputIt>>
16846  void insert(InputIt first, InputIt last)
16847  {
16848  for (auto it = first; it != last; ++it)
16849  {
16850  insert(*it);
16851  }
16852  }
16853 };
16854 
16855 } // namespace nlohmann
16856 
16857 
16858 #if defined(JSON_HAS_CPP_17)
16859  #include <string_view>
16860 #endif
16861 
16867 namespace nlohmann
16868 {
16869 
16954 NLOHMANN_BASIC_JSON_TPL_DECLARATION
16956 {
16957  private:
16958  template<detail::value_t> friend struct detail::external_constructor;
16959  friend ::nlohmann::json_pointer<basic_json>;
16960 
16961  template<typename BasicJsonType, typename InputType>
16962  friend class ::nlohmann::detail::parser;
16963  friend ::nlohmann::detail::serializer<basic_json>;
16964  template<typename BasicJsonType>
16965  friend class ::nlohmann::detail::iter_impl;
16966  template<typename BasicJsonType, typename CharType>
16967  friend class ::nlohmann::detail::binary_writer;
16968  template<typename BasicJsonType, typename InputType, typename SAX>
16969  friend class ::nlohmann::detail::binary_reader;
16970  template<typename BasicJsonType>
16971  friend class ::nlohmann::detail::json_sax_dom_parser;
16972  template<typename BasicJsonType>
16973  friend class ::nlohmann::detail::json_sax_dom_callback_parser;
16974  friend class ::nlohmann::detail::exception;
16975 
16977  using basic_json_t = NLOHMANN_BASIC_JSON_TPL;
16978 
16979  JSON_PRIVATE_UNLESS_TESTED:
16980  // convenience aliases for types residing in namespace detail;
16981  using lexer = ::nlohmann::detail::lexer_base<basic_json>;
16982 
16983  template<typename InputAdapterType>
16984  static ::nlohmann::detail::parser<basic_json, InputAdapterType> parser(
16985  InputAdapterType adapter,
16986  detail::parser_callback_t<basic_json>cb = nullptr,
16987  const bool allow_exceptions = true,
16988  const bool ignore_comments = false
16989  )
16990  {
16991  return ::nlohmann::detail::parser<basic_json, InputAdapterType>(std::move(adapter),
16992  std::move(cb), allow_exceptions, ignore_comments);
16993  }
16994 
16995  private:
16996  using primitive_iterator_t = ::nlohmann::detail::primitive_iterator_t;
16997  template<typename BasicJsonType>
16998  using internal_iterator = ::nlohmann::detail::internal_iterator<BasicJsonType>;
16999  template<typename BasicJsonType>
17000  using iter_impl = ::nlohmann::detail::iter_impl<BasicJsonType>;
17001  template<typename Iterator>
17002  using iteration_proxy = ::nlohmann::detail::iteration_proxy<Iterator>;
17003  template<typename Base> using json_reverse_iterator = ::nlohmann::detail::json_reverse_iterator<Base>;
17004 
17005  template<typename CharType>
17006  using output_adapter_t = ::nlohmann::detail::output_adapter_t<CharType>;
17007 
17008  template<typename InputType>
17009  using binary_reader = ::nlohmann::detail::binary_reader<basic_json, InputType>;
17010  template<typename CharType> using binary_writer = ::nlohmann::detail::binary_writer<basic_json, CharType>;
17011 
17012  JSON_PRIVATE_UNLESS_TESTED:
17013  using serializer = ::nlohmann::detail::serializer<basic_json>;
17014 
17015  public:
17016  using value_t = detail::value_t;
17019  template<typename T, typename SFINAE>
17020  using json_serializer = JSONSerializer<T, SFINAE>;
17022  using error_handler_t = detail::error_handler_t;
17024  using cbor_tag_handler_t = detail::cbor_tag_handler_t;
17026  using initializer_list_t = std::initializer_list<detail::json_ref<basic_json>>;
17027 
17028  using input_format_t = detail::input_format_t;
17031 
17033  // exceptions //
17035 
17039 
17041  using exception = detail::exception;
17043  using parse_error = detail::parse_error;
17045  using invalid_iterator = detail::invalid_iterator;
17047  using type_error = detail::type_error;
17049  using out_of_range = detail::out_of_range;
17051  using other_error = detail::other_error;
17052 
17054 
17055 
17057  // container types //
17059 
17064 
17067 
17071  using const_reference = const value_type&;
17072 
17074  using difference_type = std::ptrdiff_t;
17076  using size_type = std::size_t;
17077 
17079  using allocator_type = AllocatorType<basic_json>;
17080 
17082  using pointer = typename std::allocator_traits<allocator_type>::pointer;
17084  using const_pointer = typename std::allocator_traits<allocator_type>::const_pointer;
17085 
17087  using iterator = iter_impl<basic_json>;
17089  using const_iterator = iter_impl<const basic_json>;
17091  using reverse_iterator = json_reverse_iterator<typename basic_json::iterator>;
17093  using const_reverse_iterator = json_reverse_iterator<typename basic_json::const_iterator>;
17094 
17096 
17097 
17102  {
17103  return allocator_type();
17104  }
17105 
17132 
17133  static basic_json meta()
17134  {
17135  basic_json result;
17136 
17137  result["copyright"] = "(C) 2013-2021 Niels Lohmann";
17138  result["name"] = "JSON for Modern C++";
17139  result["url"] = "https://github.com/nlohmann/json";
17140  result["version"]["string"] =
17141  std::to_string(NLOHMANN_JSON_VERSION_MAJOR) + "." +
17142  std::to_string(NLOHMANN_JSON_VERSION_MINOR) + "." +
17143  std::to_string(NLOHMANN_JSON_VERSION_PATCH);
17144  result["version"]["major"] = NLOHMANN_JSON_VERSION_MAJOR;
17145  result["version"]["minor"] = NLOHMANN_JSON_VERSION_MINOR;
17146  result["version"]["patch"] = NLOHMANN_JSON_VERSION_PATCH;
17147 
17148 #ifdef _WIN32
17149  result["platform"] = "win32";
17150 #elif defined __linux__
17151  result["platform"] = "linux";
17152 #elif defined __APPLE__
17153  result["platform"] = "apple";
17154 #elif defined __unix__
17155  result["platform"] = "unix";
17156 #else
17157  result["platform"] = "unknown";
17158 #endif
17159 
17160 #if defined(__ICC) || defined(__INTEL_COMPILER)
17161  result["compiler"] = {{"family", "icc"}, {"version", __INTEL_COMPILER}};
17162 #elif defined(__clang__)
17163  result["compiler"] = {{"family", "clang"}, {"version", __clang_version__}};
17164 #elif defined(__GNUC__) || defined(__GNUG__)
17165  result["compiler"] = {{"family", "gcc"}, {"version", std::to_string(__GNUC__) + "." + std::to_string(__GNUC_MINOR__) + "." + std::to_string(__GNUC_PATCHLEVEL__)}};
17166 #elif defined(__HP_cc) || defined(__HP_aCC)
17167  result["compiler"] = "hp"
17168 #elif defined(__IBMCPP__)
17169  result["compiler"] = {{"family", "ilecpp"}, {"version", __IBMCPP__}};
17170 #elif defined(_MSC_VER)
17171  result["compiler"] = {{"family", "msvc"}, {"version", _MSC_VER}};
17172 #elif defined(__PGI)
17173  result["compiler"] = {{"family", "pgcpp"}, {"version", __PGI}};
17174 #elif defined(__SUNPRO_CC)
17175  result["compiler"] = {{"family", "sunpro"}, {"version", __SUNPRO_CC}};
17176 #else
17177  result["compiler"] = {{"family", "unknown"}, {"version", "unknown"}};
17178 #endif
17179 
17180 #ifdef __cplusplus
17181  result["compiler"]["c++"] = std::to_string(__cplusplus);
17182 #else
17183  result["compiler"]["c++"] = "unknown";
17184 #endif
17185  return result;
17186  }
17187 
17188 
17190  // JSON value data types //
17192 
17197 
17198 #if defined(JSON_HAS_CPP_14)
17199  // Use transparent comparator if possible, combined with perfect forwarding
17200  // on find() and count() calls prevents unnecessary string construction.
17201  using object_comparator_t = std::less<>;
17202 #else
17203  using object_comparator_t = std::less<StringType>;
17204 #endif
17205 
17289  using object_t = ObjectType<StringType,
17290  basic_json,
17292  AllocatorType<std::pair<const StringType,
17293  basic_json>>>;
17294 
17339  using array_t = ArrayType<basic_json, AllocatorType<basic_json>>;
17340 
17392  using string_t = StringType;
17393 
17418  using boolean_t = BooleanType;
17419 
17490  using number_integer_t = NumberIntegerType;
17491 
17561  using number_unsigned_t = NumberUnsignedType;
17562 
17629  using number_float_t = NumberFloatType;
17630 
17702 
17703  private:
17704 
17706  template<typename T, typename... Args>
17707 
17708  static T* create(Args&& ... args)
17709  {
17710  AllocatorType<T> alloc;
17711  using AllocatorTraits = std::allocator_traits<AllocatorType<T>>;
17712 
17713  auto deleter = [&](T * obj)
17714  {
17715  AllocatorTraits::deallocate(alloc, obj, 1);
17716  };
17717  std::unique_ptr<T, decltype(deleter)> obj(AllocatorTraits::allocate(alloc, 1), deleter);
17718  AllocatorTraits::construct(alloc, obj.get(), std::forward<Args>(args)...);
17719  JSON_ASSERT(obj != nullptr);
17720  return obj.release();
17721  }
17722 
17724  // JSON value storage //
17726 
17727  JSON_PRIVATE_UNLESS_TESTED:
17753  union json_value
17754  {
17756  object_t* object;
17758  array_t* array;
17760  string_t* string;
17762  binary_t* binary;
17764  boolean_t boolean;
17766  number_integer_t number_integer;
17768  number_unsigned_t number_unsigned;
17770  number_float_t number_float;
17771 
17773  json_value() = default;
17775  json_value(boolean_t v) noexcept : boolean(v) {}
17777  json_value(number_integer_t v) noexcept : number_integer(v) {}
17779  json_value(number_unsigned_t v) noexcept : number_unsigned(v) {}
17781  json_value(number_float_t v) noexcept : number_float(v) {}
17783  json_value(value_t t)
17784  {
17785  switch (t)
17786  {
17787  case value_t::object:
17788  {
17789  object = create<object_t>();
17790  break;
17791  }
17792 
17793  case value_t::array:
17794  {
17795  array = create<array_t>();
17796  break;
17797  }
17798 
17799  case value_t::string:
17800  {
17801  string = create<string_t>("");
17802  break;
17803  }
17804 
17805  case value_t::binary:
17806  {
17807  binary = create<binary_t>();
17808  break;
17809  }
17810 
17811  case value_t::boolean:
17812  {
17813  boolean = boolean_t(false);
17814  break;
17815  }
17816 
17817  case value_t::number_integer:
17818  {
17819  number_integer = number_integer_t(0);
17820  break;
17821  }
17822 
17823  case value_t::number_unsigned:
17824  {
17825  number_unsigned = number_unsigned_t(0);
17826  break;
17827  }
17828 
17829  case value_t::number_float:
17830  {
17831  number_float = number_float_t(0.0);
17832  break;
17833  }
17834 
17835  case value_t::null:
17836  {
17837  object = nullptr; // silence warning, see #821
17838  break;
17839  }
17840 
17841  default:
17842  {
17843  object = nullptr; // silence warning, see #821
17844  if (JSON_HEDLEY_UNLIKELY(t == value_t::null))
17845  {
17846  JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.9.1", basic_json())); // LCOV_EXCL_LINE
17847  }
17848  break;
17849  }
17850  }
17851  }
17852 
17854  json_value(const string_t& value)
17855  {
17856  string = create<string_t>(value);
17857  }
17858 
17860  json_value(string_t&& value)
17861  {
17862  string = create<string_t>(std::move(value));
17863  }
17864 
17866  json_value(const object_t& value)
17867  {
17868  object = create<object_t>(value);
17869  }
17870 
17872  json_value(object_t&& value)
17873  {
17874  object = create<object_t>(std::move(value));
17875  }
17876 
17878  json_value(const array_t& value)
17879  {
17880  array = create<array_t>(value);
17881  }
17882 
17884  json_value(array_t&& value)
17885  {
17886  array = create<array_t>(std::move(value));
17887  }
17888 
17890  json_value(const typename binary_t::container_type& value)
17891  {
17892  binary = create<binary_t>(value);
17893  }
17894 
17896  json_value(typename binary_t::container_type&& value)
17897  {
17898  binary = create<binary_t>(std::move(value));
17899  }
17900 
17902  json_value(const binary_t& value)
17903  {
17904  binary = create<binary_t>(value);
17905  }
17906 
17908  json_value(binary_t&& value)
17909  {
17910  binary = create<binary_t>(std::move(value));
17911  }
17912 
17913  void destroy(value_t t) noexcept
17914  {
17915  // flatten the current json_value to a heap-allocated stack
17916  std::vector<basic_json> stack;
17917 
17918  // move the top-level items to stack
17919  if (t == value_t::array)
17920  {
17921  stack.reserve(array->size());
17922  std::move(array->begin(), array->end(), std::back_inserter(stack));
17923  }
17924  else if (t == value_t::object)
17925  {
17926  stack.reserve(object->size());
17927  for (auto&& it : *object)
17928  {
17929  stack.push_back(std::move(it.second));
17930  }
17931  }
17932 
17933  while (!stack.empty())
17934  {
17935  // move the last item to local variable to be processed
17936  basic_json current_item(std::move(stack.back()));
17937  stack.pop_back();
17938 
17939  // if current_item is array/object, move
17940  // its children to the stack to be processed later
17941  if (current_item.is_array())
17942  {
17943  std::move(current_item.m_value.array->begin(), current_item.m_value.array->end(),
17944  std::back_inserter(stack));
17945 
17946  current_item.m_value.array->clear();
17947  }
17948  else if (current_item.is_object())
17949  {
17950  for (auto&& it : *current_item.m_value.object)
17951  {
17952  stack.push_back(std::move(it.second));
17953  }
17954 
17955  current_item.m_value.object->clear();
17956  }
17957 
17958  // it's now safe that current_item get destructed
17959  // since it doesn't have any children
17960  }
17961 
17962  switch (t)
17963  {
17964  case value_t::object:
17965  {
17966  AllocatorType<object_t> alloc;
17967  std::allocator_traits<decltype(alloc)>::destroy(alloc, object);
17968  std::allocator_traits<decltype(alloc)>::deallocate(alloc, object, 1);
17969  break;
17970  }
17971 
17972  case value_t::array:
17973  {
17974  AllocatorType<array_t> alloc;
17975  std::allocator_traits<decltype(alloc)>::destroy(alloc, array);
17976  std::allocator_traits<decltype(alloc)>::deallocate(alloc, array, 1);
17977  break;
17978  }
17979 
17980  case value_t::string:
17981  {
17982  AllocatorType<string_t> alloc;
17983  std::allocator_traits<decltype(alloc)>::destroy(alloc, string);
17984  std::allocator_traits<decltype(alloc)>::deallocate(alloc, string, 1);
17985  break;
17986  }
17987 
17988  case value_t::binary:
17989  {
17990  AllocatorType<binary_t> alloc;
17991  std::allocator_traits<decltype(alloc)>::destroy(alloc, binary);
17992  std::allocator_traits<decltype(alloc)>::deallocate(alloc, binary, 1);
17993  break;
17994  }
17995 
17996  default:
17997  {
17998  break;
17999  }
18000  }
18001  }
18002  };
18003 
18004  private:
18023  void assert_invariant(bool check_parents = true) const noexcept
18024  {
18025  JSON_ASSERT(m_type != value_t::object || m_value.object != nullptr);
18026  JSON_ASSERT(m_type != value_t::array || m_value.array != nullptr);
18027  JSON_ASSERT(m_type != value_t::string || m_value.string != nullptr);
18028  JSON_ASSERT(m_type != value_t::binary || m_value.binary != nullptr);
18029 
18030 #if JSON_DIAGNOSTICS
18031  JSON_ASSERT(!check_parents || !is_structured() || std::all_of(begin(), end(), [this](const basic_json & j)
18032  {
18033  return j.m_parent == this;
18034  }));
18035 #else
18036  static_cast<void>(check_parents);
18037 #endif
18038  }
18039 
18040  void set_parents()
18041  {
18042 #if JSON_DIAGNOSTICS
18043  switch (m_type)
18044  {
18045  case value_t::array:
18046  {
18047  for (auto& element : *m_value.array)
18048  {
18049  element.m_parent = this;
18050  }
18051  break;
18052  }
18053 
18054  case value_t::object:
18055  {
18056  for (auto& element : *m_value.object)
18057  {
18058  element.second.m_parent = this;
18059  }
18060  break;
18061  }
18062 
18063  default:
18064  break;
18065  }
18066 #endif
18067  }
18068 
18069  iterator set_parents(iterator it, typename iterator::difference_type count)
18070  {
18071 #if JSON_DIAGNOSTICS
18072  for (typename iterator::difference_type i = 0; i < count; ++i)
18073  {
18074  (it + i)->m_parent = this;
18075  }
18076 #else
18077  static_cast<void>(count);
18078 #endif
18079  return it;
18080  }
18081 
18082  reference set_parent(reference j)
18083  {
18084 #if JSON_DIAGNOSTICS
18085  j.m_parent = this;
18086 #else
18087  static_cast<void>(j);
18088 #endif
18089  return j;
18090  }
18091 
18092  public:
18094  // JSON parser callback //
18096 
18112  using parse_event_t = detail::parse_event_t;
18113 
18163  using parser_callback_t = detail::parser_callback_t<basic_json>;
18164 
18166  // constructors //
18168 
18173 
18204  basic_json(const value_t v)
18205  : m_type(v), m_value(v)
18206  {
18207  assert_invariant();
18208  }
18209 
18228  basic_json(std::nullptr_t = nullptr) noexcept
18229  : basic_json(value_t::null)
18230  {
18231  assert_invariant();
18232  }
18233 
18297  template < typename CompatibleType,
18298  typename U = detail::uncvref_t<CompatibleType>,
18299  detail::enable_if_t <
18300  !detail::is_basic_json<U>::value && detail::is_compatible_type<basic_json_t, U>::value, int > = 0 >
18301  basic_json(CompatibleType && val) noexcept(noexcept(
18302  JSONSerializer<U>::to_json(std::declval<basic_json_t&>(),
18303  std::forward<CompatibleType>(val))))
18304  {
18305  JSONSerializer<U>::to_json(*this, std::forward<CompatibleType>(val));
18306  set_parents();
18307  assert_invariant();
18308  }
18309 
18336  template < typename BasicJsonType,
18337  detail::enable_if_t <
18338  detail::is_basic_json<BasicJsonType>::value&& !std::is_same<basic_json, BasicJsonType>::value, int > = 0 >
18339  basic_json(const BasicJsonType& val)
18340  {
18341  using other_boolean_t = typename BasicJsonType::boolean_t;
18342  using other_number_float_t = typename BasicJsonType::number_float_t;
18343  using other_number_integer_t = typename BasicJsonType::number_integer_t;
18344  using other_number_unsigned_t = typename BasicJsonType::number_unsigned_t;
18345  using other_string_t = typename BasicJsonType::string_t;
18346  using other_object_t = typename BasicJsonType::object_t;
18347  using other_array_t = typename BasicJsonType::array_t;
18348  using other_binary_t = typename BasicJsonType::binary_t;
18349 
18350  switch (val.type())
18351  {
18352  case value_t::boolean:
18353  JSONSerializer<other_boolean_t>::to_json(*this, val.template get<other_boolean_t>());
18354  break;
18355  case value_t::number_float:
18356  JSONSerializer<other_number_float_t>::to_json(*this, val.template get<other_number_float_t>());
18357  break;
18358  case value_t::number_integer:
18359  JSONSerializer<other_number_integer_t>::to_json(*this, val.template get<other_number_integer_t>());
18360  break;
18361  case value_t::number_unsigned:
18362  JSONSerializer<other_number_unsigned_t>::to_json(*this, val.template get<other_number_unsigned_t>());
18363  break;
18364  case value_t::string:
18365  JSONSerializer<other_string_t>::to_json(*this, val.template get_ref<const other_string_t&>());
18366  break;
18367  case value_t::object:
18368  JSONSerializer<other_object_t>::to_json(*this, val.template get_ref<const other_object_t&>());
18369  break;
18370  case value_t::array:
18371  JSONSerializer<other_array_t>::to_json(*this, val.template get_ref<const other_array_t&>());
18372  break;
18373  case value_t::binary:
18374  JSONSerializer<other_binary_t>::to_json(*this, val.template get_ref<const other_binary_t&>());
18375  break;
18376  case value_t::null:
18377  *this = nullptr;
18378  break;
18379  case value_t::discarded:
18380  m_type = value_t::discarded;
18381  break;
18382  default: // LCOV_EXCL_LINE
18383  JSON_ASSERT(false); // LCOV_EXCL_LINE
18384  }
18385  set_parents();
18386  assert_invariant();
18387  }
18388 
18464  bool type_deduction = true,
18465  value_t manual_type = value_t::array)
18466  {
18467  // check if each element is an array with two elements whose first
18468  // element is a string
18469  bool is_an_object = std::all_of(init.begin(), init.end(),
18470  [](const detail::json_ref<basic_json>& element_ref)
18471  {
18472  return element_ref->is_array() && element_ref->size() == 2 && (*element_ref)[0].is_string();
18473  });
18474 
18475  // adjust type if type deduction is not wanted
18476  if (!type_deduction)
18477  {
18478  // if array is wanted, do not create an object though possible
18479  if (manual_type == value_t::array)
18480  {
18481  is_an_object = false;
18482  }
18483 
18484  // if object is wanted but impossible, throw an exception
18485  if (JSON_HEDLEY_UNLIKELY(manual_type == value_t::object && !is_an_object))
18486  {
18487  JSON_THROW(type_error::create(301, "cannot create object from initializer list", basic_json()));
18488  }
18489  }
18490 
18491  if (is_an_object)
18492  {
18493  // the initializer list is a list of pairs -> create object
18494  m_type = value_t::object;
18495  m_value = value_t::object;
18496 
18497  for (auto& element_ref : init)
18498  {
18499  auto element = element_ref.moved_or_copied();
18500  m_value.object->emplace(
18501  std::move(*((*element.m_value.array)[0].m_value.string)),
18502  std::move((*element.m_value.array)[1]));
18503  }
18504  }
18505  else
18506  {
18507  // the initializer list describes an array -> create array
18508  m_type = value_t::array;
18509  m_value.array = create<array_t>(init.begin(), init.end());
18510  }
18511 
18512  set_parents();
18513  assert_invariant();
18514  }
18515 
18543 
18544  static basic_json binary(const typename binary_t::container_type& init)
18545  {
18546  auto res = basic_json();
18547  res.m_type = value_t::binary;
18548  res.m_value = init;
18549  return res;
18550  }
18551 
18580 
18581  static basic_json binary(const typename binary_t::container_type& init, std::uint8_t subtype)
18582  {
18583  auto res = basic_json();
18584  res.m_type = value_t::binary;
18585  res.m_value = binary_t(init, subtype);
18586  return res;
18587  }
18588 
18590 
18592  {
18593  auto res = basic_json();
18594  res.m_type = value_t::binary;
18595  res.m_value = std::move(init);
18596  return res;
18597  }
18598 
18600 
18601  static basic_json binary(typename binary_t::container_type&& init, std::uint8_t subtype)
18602  {
18603  auto res = basic_json();
18604  res.m_type = value_t::binary;
18605  res.m_value = binary_t(std::move(init), subtype);
18606  return res;
18607  }
18608 
18646 
18648  {
18649  return basic_json(init, false, value_t::array);
18650  }
18651 
18690 
18692  {
18693  return basic_json(init, false, value_t::object);
18694  }
18695 
18719  : m_type(value_t::array)
18720  {
18721  m_value.array = create<array_t>(cnt, val);
18722  set_parents();
18723  assert_invariant();
18724  }
18725 
18781  template < class InputIT, typename std::enable_if <
18782  std::is_same<InputIT, typename basic_json_t::iterator>::value ||
18783  std::is_same<InputIT, typename basic_json_t::const_iterator>::value, int >::type = 0 >
18784  basic_json(InputIT first, InputIT last)
18785  {
18786  JSON_ASSERT(first.m_object != nullptr);
18787  JSON_ASSERT(last.m_object != nullptr);
18788 
18789  // make sure iterator fits the current value
18790  if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
18791  {
18792  JSON_THROW(invalid_iterator::create(201, "iterators are not compatible", basic_json()));
18793  }
18794 
18795  // copy type from first iterator
18796  m_type = first.m_object->m_type;
18797 
18798  // check if iterator range is complete for primitive values
18799  switch (m_type)
18800  {
18801  case value_t::boolean:
18802  case value_t::number_float:
18803  case value_t::number_integer:
18804  case value_t::number_unsigned:
18805  case value_t::string:
18806  {
18807  if (JSON_HEDLEY_UNLIKELY(!first.m_it.primitive_iterator.is_begin()
18808  || !last.m_it.primitive_iterator.is_end()))
18809  {
18810  JSON_THROW(invalid_iterator::create(204, "iterators out of range", *first.m_object));
18811  }
18812  break;
18813  }
18814 
18815  default:
18816  break;
18817  }
18818 
18819  switch (m_type)
18820  {
18821  case value_t::number_integer:
18822  {
18823  m_value.number_integer = first.m_object->m_value.number_integer;
18824  break;
18825  }
18826 
18827  case value_t::number_unsigned:
18828  {
18829  m_value.number_unsigned = first.m_object->m_value.number_unsigned;
18830  break;
18831  }
18832 
18833  case value_t::number_float:
18834  {
18835  m_value.number_float = first.m_object->m_value.number_float;
18836  break;
18837  }
18838 
18839  case value_t::boolean:
18840  {
18841  m_value.boolean = first.m_object->m_value.boolean;
18842  break;
18843  }
18844 
18845  case value_t::string:
18846  {
18847  m_value = *first.m_object->m_value.string;
18848  break;
18849  }
18850 
18851  case value_t::object:
18852  {
18853  m_value.object = create<object_t>(first.m_it.object_iterator,
18854  last.m_it.object_iterator);
18855  break;
18856  }
18857 
18858  case value_t::array:
18859  {
18860  m_value.array = create<array_t>(first.m_it.array_iterator,
18861  last.m_it.array_iterator);
18862  break;
18863  }
18864 
18865  case value_t::binary:
18866  {
18867  m_value = *first.m_object->m_value.binary;
18868  break;
18869  }
18870 
18871  default:
18872  JSON_THROW(invalid_iterator::create(206, "cannot construct with iterators from " + std::string(first.m_object->type_name()), *first.m_object));
18873  }
18874 
18875  set_parents();
18876  assert_invariant();
18877  }
18878 
18879 
18881  // other constructors and destructor //
18883 
18884  template<typename JsonRef,
18885  detail::enable_if_t<detail::conjunction<detail::is_json_ref<JsonRef>,
18886  std::is_same<typename JsonRef::value_type, basic_json>>::value, int> = 0 >
18887  basic_json(const JsonRef& ref) : basic_json(ref.moved_or_copied()) {}
18888 
18914  basic_json(const basic_json& other)
18915  : m_type(other.m_type)
18916  {
18917  // check of passed value is valid
18918  other.assert_invariant();
18919 
18920  switch (m_type)
18921  {
18922  case value_t::object:
18923  {
18924  m_value = *other.m_value.object;
18925  break;
18926  }
18927 
18928  case value_t::array:
18929  {
18930  m_value = *other.m_value.array;
18931  break;
18932  }
18933 
18934  case value_t::string:
18935  {
18936  m_value = *other.m_value.string;
18937  break;
18938  }
18939 
18940  case value_t::boolean:
18941  {
18942  m_value = other.m_value.boolean;
18943  break;
18944  }
18945 
18946  case value_t::number_integer:
18947  {
18948  m_value = other.m_value.number_integer;
18949  break;
18950  }
18951 
18952  case value_t::number_unsigned:
18953  {
18954  m_value = other.m_value.number_unsigned;
18955  break;
18956  }
18957 
18958  case value_t::number_float:
18959  {
18960  m_value = other.m_value.number_float;
18961  break;
18962  }
18963 
18964  case value_t::binary:
18965  {
18966  m_value = *other.m_value.binary;
18967  break;
18968  }
18969 
18970  default:
18971  break;
18972  }
18973 
18974  set_parents();
18975  assert_invariant();
18976  }
18977 
19004  basic_json(basic_json&& other) noexcept
19005  : m_type(std::move(other.m_type)),
19006  m_value(std::move(other.m_value))
19007  {
19008  // check that passed value is valid
19009  other.assert_invariant(false);
19010 
19011  // invalidate payload
19012  other.m_type = value_t::null;
19013  other.m_value = {};
19014 
19015  set_parents();
19016  assert_invariant();
19017  }
19018 
19042  basic_json& operator=(basic_json other) noexcept (
19043  std::is_nothrow_move_constructible<value_t>::value&&
19044  std::is_nothrow_move_assignable<value_t>::value&&
19045  std::is_nothrow_move_constructible<json_value>::value&&
19046  std::is_nothrow_move_assignable<json_value>::value
19047  )
19048  {
19049  // check that passed value is valid
19050  other.assert_invariant();
19051 
19052  using std::swap;
19053  swap(m_type, other.m_type);
19054  swap(m_value, other.m_value);
19055 
19056  set_parents();
19057  assert_invariant();
19058  return *this;
19059  }
19060 
19076  ~basic_json() noexcept
19077  {
19078  assert_invariant(false);
19079  m_value.destroy(m_type);
19080  }
19081 
19083 
19084  public:
19086  // object inspection //
19088 
19092 
19140  string_t dump(const int indent = -1,
19141  const char indent_char = ' ',
19142  const bool ensure_ascii = false,
19143  const error_handler_t error_handler = error_handler_t::strict) const
19144  {
19145  string_t result;
19146  serializer s(detail::output_adapter<char, string_t>(result), indent_char, error_handler);
19147 
19148  if (indent >= 0)
19149  {
19150  s.dump(*this, true, ensure_ascii, static_cast<unsigned int>(indent));
19151  }
19152  else
19153  {
19154  s.dump(*this, false, ensure_ascii, 0);
19155  }
19156 
19157  return result;
19158  }
19159 
19193  constexpr value_t type() const noexcept
19194  {
19195  return m_type;
19196  }
19197 
19224  constexpr bool is_primitive() const noexcept
19225  {
19226  return is_null() || is_string() || is_boolean() || is_number() || is_binary();
19227  }
19228 
19251  constexpr bool is_structured() const noexcept
19252  {
19253  return is_array() || is_object();
19254  }
19255 
19273  constexpr bool is_null() const noexcept
19274  {
19275  return m_type == value_t::null;
19276  }
19277 
19295  constexpr bool is_boolean() const noexcept
19296  {
19297  return m_type == value_t::boolean;
19298  }
19299 
19325  constexpr bool is_number() const noexcept
19326  {
19327  return is_number_integer() || is_number_float();
19328  }
19329 
19354  constexpr bool is_number_integer() const noexcept
19355  {
19356  return m_type == value_t::number_integer || m_type == value_t::number_unsigned;
19357  }
19358 
19382  constexpr bool is_number_unsigned() const noexcept
19383  {
19384  return m_type == value_t::number_unsigned;
19385  }
19386 
19410  constexpr bool is_number_float() const noexcept
19411  {
19412  return m_type == value_t::number_float;
19413  }
19414 
19432  constexpr bool is_object() const noexcept
19433  {
19434  return m_type == value_t::object;
19435  }
19436 
19454  constexpr bool is_array() const noexcept
19455  {
19456  return m_type == value_t::array;
19457  }
19458 
19476  constexpr bool is_string() const noexcept
19477  {
19478  return m_type == value_t::string;
19479  }
19480 
19498  constexpr bool is_binary() const noexcept
19499  {
19500  return m_type == value_t::binary;
19501  }
19502 
19525  constexpr bool is_discarded() const noexcept
19526  {
19527  return m_type == value_t::discarded;
19528  }
19529 
19551  constexpr operator value_t() const noexcept
19552  {
19553  return m_type;
19554  }
19555 
19557 
19558  private:
19560  // value access //
19562 
19564  boolean_t get_impl(boolean_t* /*unused*/) const
19565  {
19566  if (JSON_HEDLEY_LIKELY(is_boolean()))
19567  {
19568  return m_value.boolean;
19569  }
19570 
19571  JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(type_name()), *this));
19572  }
19573 
19575  object_t* get_impl_ptr(object_t* /*unused*/) noexcept
19576  {
19577  return is_object() ? m_value.object : nullptr;
19578  }
19579 
19581  constexpr const object_t* get_impl_ptr(const object_t* /*unused*/) const noexcept
19582  {
19583  return is_object() ? m_value.object : nullptr;
19584  }
19585 
19587  array_t* get_impl_ptr(array_t* /*unused*/) noexcept
19588  {
19589  return is_array() ? m_value.array : nullptr;
19590  }
19591 
19593  constexpr const array_t* get_impl_ptr(const array_t* /*unused*/) const noexcept
19594  {
19595  return is_array() ? m_value.array : nullptr;
19596  }
19597 
19599  string_t* get_impl_ptr(string_t* /*unused*/) noexcept
19600  {
19601  return is_string() ? m_value.string : nullptr;
19602  }
19603 
19605  constexpr const string_t* get_impl_ptr(const string_t* /*unused*/) const noexcept
19606  {
19607  return is_string() ? m_value.string : nullptr;
19608  }
19609 
19611  boolean_t* get_impl_ptr(boolean_t* /*unused*/) noexcept
19612  {
19613  return is_boolean() ? &m_value.boolean : nullptr;
19614  }
19615 
19617  constexpr const boolean_t* get_impl_ptr(const boolean_t* /*unused*/) const noexcept
19618  {
19619  return is_boolean() ? &m_value.boolean : nullptr;
19620  }
19621 
19623  number_integer_t* get_impl_ptr(number_integer_t* /*unused*/) noexcept
19624  {
19625  return is_number_integer() ? &m_value.number_integer : nullptr;
19626  }
19627 
19629  constexpr const number_integer_t* get_impl_ptr(const number_integer_t* /*unused*/) const noexcept
19630  {
19631  return is_number_integer() ? &m_value.number_integer : nullptr;
19632  }
19633 
19635  number_unsigned_t* get_impl_ptr(number_unsigned_t* /*unused*/) noexcept
19636  {
19637  return is_number_unsigned() ? &m_value.number_unsigned : nullptr;
19638  }
19639 
19641  constexpr const number_unsigned_t* get_impl_ptr(const number_unsigned_t* /*unused*/) const noexcept
19642  {
19643  return is_number_unsigned() ? &m_value.number_unsigned : nullptr;
19644  }
19645 
19647  number_float_t* get_impl_ptr(number_float_t* /*unused*/) noexcept
19648  {
19649  return is_number_float() ? &m_value.number_float : nullptr;
19650  }
19651 
19653  constexpr const number_float_t* get_impl_ptr(const number_float_t* /*unused*/) const noexcept
19654  {
19655  return is_number_float() ? &m_value.number_float : nullptr;
19656  }
19657 
19659  binary_t* get_impl_ptr(binary_t* /*unused*/) noexcept
19660  {
19661  return is_binary() ? m_value.binary : nullptr;
19662  }
19663 
19665  constexpr const binary_t* get_impl_ptr(const binary_t* /*unused*/) const noexcept
19666  {
19667  return is_binary() ? m_value.binary : nullptr;
19668  }
19669 
19681  template<typename ReferenceType, typename ThisType>
19682  static ReferenceType get_ref_impl(ThisType& obj)
19683  {
19684  // delegate the call to get_ptr<>()
19685  auto ptr = obj.template get_ptr<typename std::add_pointer<ReferenceType>::type>();
19686 
19687  if (JSON_HEDLEY_LIKELY(ptr != nullptr))
19688  {
19689  return *ptr;
19690  }
19691 
19692  JSON_THROW(type_error::create(303, "incompatible ReferenceType for get_ref, actual type is " + std::string(obj.type_name()), obj));
19693  }
19694 
19695  public:
19699 
19714  template<typename BasicJsonType, detail::enable_if_t<
19715  std::is_same<typename std::remove_const<BasicJsonType>::type, basic_json_t>::value,
19716  int> = 0>
19717  basic_json get() const
19718  {
19719  return *this;
19720  }
19721 
19737  template < typename BasicJsonType, detail::enable_if_t <
19738  !std::is_same<BasicJsonType, basic_json>::value&&
19739  detail::is_basic_json<BasicJsonType>::value, int > = 0 >
19740  BasicJsonType get() const
19741  {
19742  return *this;
19743  }
19744 
19784  template < typename ValueTypeCV, typename ValueType = detail::uncvref_t<ValueTypeCV>,
19785  detail::enable_if_t <
19786  !detail::is_basic_json<ValueType>::value &&
19787  detail::has_from_json<basic_json_t, ValueType>::value &&
19788  !detail::has_non_default_from_json<basic_json_t, ValueType>::value,
19789  int > = 0 >
19790  ValueType get() const noexcept(noexcept(
19791  JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), std::declval<ValueType&>())))
19792  {
19793  // we cannot static_assert on ValueTypeCV being non-const, because
19794  // there is support for get<const basic_json_t>(), which is why we
19795  // still need the uncvref
19796  static_assert(!std::is_reference<ValueTypeCV>::value,
19797  "get() cannot be used with reference types, you might want to use get_ref()");
19798  static_assert(std::is_default_constructible<ValueType>::value,
19799  "types must be DefaultConstructible when used with get()");
19800 
19801  ValueType ret;
19802  JSONSerializer<ValueType>::from_json(*this, ret);
19803  return ret;
19804  }
19805 
19837  template < typename ValueTypeCV, typename ValueType = detail::uncvref_t<ValueTypeCV>,
19838  detail::enable_if_t < !std::is_same<basic_json_t, ValueType>::value &&
19839  detail::has_non_default_from_json<basic_json_t, ValueType>::value,
19840  int > = 0 >
19841  ValueType get() const noexcept(noexcept(
19842  JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>())))
19843  {
19844  static_assert(!std::is_reference<ValueTypeCV>::value,
19845  "get() cannot be used with reference types, you might want to use get_ref()");
19846  return JSONSerializer<ValueType>::from_json(*this);
19847  }
19848 
19882  template < typename ValueType,
19883  detail::enable_if_t <
19884  !detail::is_basic_json<ValueType>::value&&
19885  detail::has_from_json<basic_json_t, ValueType>::value,
19886  int > = 0 >
19887  ValueType & get_to(ValueType& v) const noexcept(noexcept(
19888  JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), v)))
19889  {
19890  JSONSerializer<ValueType>::from_json(*this, v);
19891  return v;
19892  }
19893 
19894  // specialization to allow to call get_to with a basic_json value
19895  // see https://github.com/nlohmann/json/issues/2175
19896  template<typename ValueType,
19897  detail::enable_if_t <
19898  detail::is_basic_json<ValueType>::value,
19899  int> = 0>
19900  ValueType & get_to(ValueType& v) const
19901  {
19902  v = *this;
19903  return v;
19904  }
19905 
19906  template <
19907  typename T, std::size_t N,
19908  typename Array = T (&)[N],
19909  detail::enable_if_t <
19910  detail::has_from_json<basic_json_t, Array>::value, int > = 0 >
19911  Array get_to(T (&v)[N]) const
19912  noexcept(noexcept(JSONSerializer<Array>::from_json(
19913  std::declval<const basic_json_t&>(), v)))
19914  {
19915  JSONSerializer<Array>::from_json(*this, v);
19916  return v;
19917  }
19918 
19919 
19946  template<typename PointerType, typename std::enable_if<
19947  std::is_pointer<PointerType>::value, int>::type = 0>
19948  auto get_ptr() noexcept -> decltype(std::declval<basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
19949  {
19950  // delegate the call to get_impl_ptr<>()
19951  return get_impl_ptr(static_cast<PointerType>(nullptr));
19952  }
19953 
19958  template < typename PointerType, typename std::enable_if <
19959  std::is_pointer<PointerType>::value&&
19960  std::is_const<typename std::remove_pointer<PointerType>::type>::value, int >::type = 0 >
19961  constexpr auto get_ptr() const noexcept -> decltype(std::declval<const basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
19962  {
19963  // delegate the call to get_impl_ptr<>() const
19964  return get_impl_ptr(static_cast<PointerType>(nullptr));
19965  }
19966 
19994  template<typename PointerType, typename std::enable_if<
19995  std::is_pointer<PointerType>::value, int>::type = 0>
19996  auto get() noexcept -> decltype(std::declval<basic_json_t&>().template get_ptr<PointerType>())
19997  {
19998  // delegate the call to get_ptr
19999  return get_ptr<PointerType>();
20000  }
20001 
20006  template<typename PointerType, typename std::enable_if<
20007  std::is_pointer<PointerType>::value, int>::type = 0>
20008  constexpr auto get() const noexcept -> decltype(std::declval<const basic_json_t&>().template get_ptr<PointerType>())
20009  {
20010  // delegate the call to get_ptr
20011  return get_ptr<PointerType>();
20012  }
20013 
20040  template<typename ReferenceType, typename std::enable_if<
20041  std::is_reference<ReferenceType>::value, int>::type = 0>
20042  ReferenceType get_ref()
20043  {
20044  // delegate call to get_ref_impl
20045  return get_ref_impl<ReferenceType>(*this);
20046  }
20047 
20052  template < typename ReferenceType, typename std::enable_if <
20053  std::is_reference<ReferenceType>::value&&
20054  std::is_const<typename std::remove_reference<ReferenceType>::type>::value, int >::type = 0 >
20055  ReferenceType get_ref() const
20056  {
20057  // delegate call to get_ref_impl
20058  return get_ref_impl<ReferenceType>(*this);
20059  }
20060 
20090  template < typename ValueType, typename std::enable_if <
20091  !std::is_pointer<ValueType>::value&&
20092  !std::is_same<ValueType, detail::json_ref<basic_json>>::value&&
20093  !std::is_same<ValueType, typename string_t::value_type>::value&&
20094  !detail::is_basic_json<ValueType>::value
20095  && !std::is_same<ValueType, std::initializer_list<typename string_t::value_type>>::value
20096 #if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER <= 1914))
20097  && !std::is_same<ValueType, typename std::string_view>::value
20098 #endif
20099  && detail::is_detected<detail::get_template_function, const basic_json_t&, ValueType>::value
20100  , int >::type = 0 >
20101  JSON_EXPLICIT operator ValueType() const
20102  {
20103  // delegate the call to get<>() const
20104  return get<ValueType>();
20105  }
20106 
20117  {
20118  if (!is_binary())
20119  {
20120  JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()), *this));
20121  }
20122 
20123  return *get_ptr<binary_t*>();
20124  }
20125 
20127  const binary_t& get_binary() const
20128  {
20129  if (!is_binary())
20130  {
20131  JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()), *this));
20132  }
20133 
20134  return *get_ptr<const binary_t*>();
20135  }
20136 
20138 
20139 
20141  // element access //
20143 
20147 
20175  {
20176  // at only works for arrays
20177  if (JSON_HEDLEY_LIKELY(is_array()))
20178  {
20179  JSON_TRY
20180  {
20181  return set_parent(m_value.array->at(idx));
20182  }
20183  JSON_CATCH (std::out_of_range&)
20184  {
20185  // create better exception explanation
20186  JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", *this));
20187  }
20188  }
20189  else
20190  {
20191  JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this));
20192  }
20193  }
20194 
20221  const_reference at(size_type idx) const
20222  {
20223  // at only works for arrays
20224  if (JSON_HEDLEY_LIKELY(is_array()))
20225  {
20226  JSON_TRY
20227  {
20228  return m_value.array->at(idx);
20229  }
20230  JSON_CATCH (std::out_of_range&)
20231  {
20232  // create better exception explanation
20233  JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", *this));
20234  }
20235  }
20236  else
20237  {
20238  JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this));
20239  }
20240  }
20241 
20272  reference at(const typename object_t::key_type& key)
20273  {
20274  // at only works for objects
20275  if (JSON_HEDLEY_LIKELY(is_object()))
20276  {
20277  JSON_TRY
20278  {
20279  return set_parent(m_value.object->at(key));
20280  }
20281  JSON_CATCH (std::out_of_range&)
20282  {
20283  // create better exception explanation
20284  JSON_THROW(out_of_range::create(403, "key '" + key + "' not found", *this));
20285  }
20286  }
20287  else
20288  {
20289  JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this));
20290  }
20291  }
20292 
20323  const_reference at(const typename object_t::key_type& key) const
20324  {
20325  // at only works for objects
20326  if (JSON_HEDLEY_LIKELY(is_object()))
20327  {
20328  JSON_TRY
20329  {
20330  return m_value.object->at(key);
20331  }
20332  JSON_CATCH (std::out_of_range&)
20333  {
20334  // create better exception explanation
20335  JSON_THROW(out_of_range::create(403, "key '" + key + "' not found", *this));
20336  }
20337  }
20338  else
20339  {
20340  JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this));
20341  }
20342  }
20343 
20370  {
20371  // implicitly convert null value to an empty array
20372  if (is_null())
20373  {
20374  m_type = value_t::array;
20375  m_value.array = create<array_t>();
20376  assert_invariant();
20377  }
20378 
20379  // operator[] only works for arrays
20380  if (JSON_HEDLEY_LIKELY(is_array()))
20381  {
20382  // fill up array with null values if given idx is outside range
20383  if (idx >= m_value.array->size())
20384  {
20385 #if JSON_DIAGNOSTICS
20386  // remember array size before resizing
20387  const auto previous_size = m_value.array->size();
20388 #endif
20389  m_value.array->resize(idx + 1);
20390 
20391 #if JSON_DIAGNOSTICS
20392  // set parent for values added above
20393  set_parents(begin() + static_cast<typename iterator::difference_type>(previous_size), static_cast<typename iterator::difference_type>(idx + 1 - previous_size));
20394 #endif
20395  }
20396 
20397  return m_value.array->operator[](idx);
20398  }
20399 
20400  JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()), *this));
20401  }
20402 
20423  {
20424  // const operator[] only works for arrays
20425  if (JSON_HEDLEY_LIKELY(is_array()))
20426  {
20427  return m_value.array->operator[](idx);
20428  }
20429 
20430  JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()), *this));
20431  }
20460  reference operator[](const typename object_t::key_type& key)
20461  {
20462  // implicitly convert null value to an empty object
20463  if (is_null())
20464  {
20465  m_type = value_t::object;
20466  m_value.object = create<object_t>();
20467  assert_invariant();
20468  }
20469 
20470  // operator[] only works for objects
20471  if (JSON_HEDLEY_LIKELY(is_object()))
20472  {
20473  return set_parent(m_value.object->operator[](key));
20474  }
20475 
20476  JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this));
20477  }
20478 
20509  const_reference operator[](const typename object_t::key_type& key) const
20510  {
20511  // const operator[] only works for objects
20512  if (JSON_HEDLEY_LIKELY(is_object()))
20513  {
20514  JSON_ASSERT(m_value.object->find(key) != m_value.object->end());
20515  return m_value.object->find(key)->second;
20516  }
20517 
20518  JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this));
20519  }
20520 
20548  template<typename T>
20549  JSON_HEDLEY_NON_NULL(2)
20550  reference operator[](T* key)
20551  {
20552  // implicitly convert null to object
20553  if (is_null())
20554  {
20555  m_type = value_t::object;
20556  m_value = value_t::object;
20557  assert_invariant();
20558  }
20559 
20560  // at only works for objects
20561  if (JSON_HEDLEY_LIKELY(is_object()))
20562  {
20563  return set_parent(m_value.object->operator[](key));
20564  }
20565 
20566  JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this));
20567  }
20568 
20599  template<typename T>
20600  JSON_HEDLEY_NON_NULL(2)
20601  const_reference operator[](T* key) const
20602  {
20603  // at only works for objects
20604  if (JSON_HEDLEY_LIKELY(is_object()))
20605  {
20606  JSON_ASSERT(m_value.object->find(key) != m_value.object->end());
20607  return m_value.object->find(key)->second;
20608  }
20609 
20610  JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this));
20611  }
20612 
20663  // using std::is_convertible in a std::enable_if will fail when using explicit conversions
20664  template < class ValueType, typename std::enable_if <
20665  detail::is_getable<basic_json_t, ValueType>::value
20666  && !std::is_same<value_t, ValueType>::value, int >::type = 0 >
20667  ValueType value(const typename object_t::key_type& key, const ValueType& default_value) const
20668  {
20669  // at only works for objects
20670  if (JSON_HEDLEY_LIKELY(is_object()))
20671  {
20672  // if key is found, return value and given default value otherwise
20673  const auto it = find(key);
20674  if (it != end())
20675  {
20676  return it->template get<ValueType>();
20677  }
20678 
20679  return default_value;
20680  }
20681 
20682  JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()), *this));
20683  }
20684 
20689  string_t value(const typename object_t::key_type& key, const char* default_value) const
20690  {
20691  return value(key, string_t(default_value));
20692  }
20693 
20737  template<class ValueType, typename std::enable_if<
20738  detail::is_getable<basic_json_t, ValueType>::value, int>::type = 0>
20739  ValueType value(const json_pointer& ptr, const ValueType& default_value) const
20740  {
20741  // at only works for objects
20742  if (JSON_HEDLEY_LIKELY(is_object()))
20743  {
20744  // if pointer resolves a value, return it or use default value
20745  JSON_TRY
20746  {
20747  return ptr.get_checked(this).template get<ValueType>();
20748  }
20749  JSON_INTERNAL_CATCH (out_of_range&)
20750  {
20751  return default_value;
20752  }
20753  }
20754 
20755  JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()), *this));
20756  }
20757 
20762  JSON_HEDLEY_NON_NULL(3)
20763  string_t value(const json_pointer& ptr, const char* default_value) const
20764  {
20765  return value(ptr, string_t(default_value));
20766  }
20767 
20793  reference front()
20794  {
20795  return *begin();
20796  }
20797 
20801  const_reference front() const
20802  {
20803  return *cbegin();
20804  }
20805 
20837  reference back()
20838  {
20839  auto tmp = end();
20840  --tmp;
20841  return *tmp;
20842  }
20843 
20848  {
20849  auto tmp = cend();
20850  --tmp;
20851  return *tmp;
20852  }
20853 
20900  template < class IteratorType, typename std::enable_if <
20901  std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
20902  std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int >::type
20903  = 0 >
20904  IteratorType erase(IteratorType pos)
20905  {
20906  // make sure iterator fits the current value
20907  if (JSON_HEDLEY_UNLIKELY(this != pos.m_object))
20908  {
20909  JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", *this));
20910  }
20911 
20912  IteratorType result = end();
20913 
20914  switch (m_type)
20915  {
20916  case value_t::boolean:
20917  case value_t::number_float:
20918  case value_t::number_integer:
20919  case value_t::number_unsigned:
20920  case value_t::string:
20921  case value_t::binary:
20922  {
20923  if (JSON_HEDLEY_UNLIKELY(!pos.m_it.primitive_iterator.is_begin()))
20924  {
20925  JSON_THROW(invalid_iterator::create(205, "iterator out of range", *this));
20926  }
20927 
20928  if (is_string())
20929  {
20930  AllocatorType<string_t> alloc;
20931  std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.string);
20932  std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.string, 1);
20933  m_value.string = nullptr;
20934  }
20935  else if (is_binary())
20936  {
20937  AllocatorType<binary_t> alloc;
20938  std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.binary);
20939  std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.binary, 1);
20940  m_value.binary = nullptr;
20941  }
20942 
20943  m_type = value_t::null;
20944  assert_invariant();
20945  break;
20946  }
20947 
20948  case value_t::object:
20949  {
20950  result.m_it.object_iterator = m_value.object->erase(pos.m_it.object_iterator);
20951  break;
20952  }
20953 
20954  case value_t::array:
20955  {
20956  result.m_it.array_iterator = m_value.array->erase(pos.m_it.array_iterator);
20957  break;
20958  }
20959 
20960  default:
20961  JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this));
20962  }
20963 
20964  return result;
20965  }
20966 
21013  template < class IteratorType, typename std::enable_if <
21014  std::is_same<IteratorType, typename basic_json_t::iterator>::value ||
21015  std::is_same<IteratorType, typename basic_json_t::const_iterator>::value, int >::type
21016  = 0 >
21017  IteratorType erase(IteratorType first, IteratorType last)
21018  {
21019  // make sure iterator fits the current value
21020  if (JSON_HEDLEY_UNLIKELY(this != first.m_object || this != last.m_object))
21021  {
21022  JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value", *this));
21023  }
21024 
21025  IteratorType result = end();
21026 
21027  switch (m_type)
21028  {
21029  case value_t::boolean:
21030  case value_t::number_float:
21031  case value_t::number_integer:
21032  case value_t::number_unsigned:
21033  case value_t::string:
21034  case value_t::binary:
21035  {
21036  if (JSON_HEDLEY_LIKELY(!first.m_it.primitive_iterator.is_begin()
21037  || !last.m_it.primitive_iterator.is_end()))
21038  {
21039  JSON_THROW(invalid_iterator::create(204, "iterators out of range", *this));
21040  }
21041 
21042  if (is_string())
21043  {
21044  AllocatorType<string_t> alloc;
21045  std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.string);
21046  std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.string, 1);
21047  m_value.string = nullptr;
21048  }
21049  else if (is_binary())
21050  {
21051  AllocatorType<binary_t> alloc;
21052  std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.binary);
21053  std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.binary, 1);
21054  m_value.binary = nullptr;
21055  }
21056 
21057  m_type = value_t::null;
21058  assert_invariant();
21059  break;
21060  }
21061 
21062  case value_t::object:
21063  {
21064  result.m_it.object_iterator = m_value.object->erase(first.m_it.object_iterator,
21065  last.m_it.object_iterator);
21066  break;
21067  }
21068 
21069  case value_t::array:
21070  {
21071  result.m_it.array_iterator = m_value.array->erase(first.m_it.array_iterator,
21072  last.m_it.array_iterator);
21073  break;
21074  }
21075 
21076  default:
21077  JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this));
21078  }
21079 
21080  return result;
21081  }
21082 
21112  size_type erase(const typename object_t::key_type& key)
21113  {
21114  // this erase only works for objects
21115  if (JSON_HEDLEY_LIKELY(is_object()))
21116  {
21117  return m_value.object->erase(key);
21118  }
21119 
21120  JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this));
21121  }
21147  void erase(const size_type idx)
21148  {
21149  // this erase only works for arrays
21150  if (JSON_HEDLEY_LIKELY(is_array()))
21151  {
21152  if (JSON_HEDLEY_UNLIKELY(idx >= size()))
21153  {
21154  JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", *this));
21155  }
21156 
21157  m_value.array->erase(m_value.array->begin() + static_cast<difference_type>(idx));
21158  }
21159  else
21160  {
21161  JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this));
21162  }
21163  }
21164 
21166 
21167 
21169  // lookup //
21171 
21174 
21199  template<typename KeyT>
21200  iterator find(KeyT&& key)
21201  {
21202  auto result = end();
21203 
21204  if (is_object())
21205  {
21206  result.m_it.object_iterator = m_value.object->find(std::forward<KeyT>(key));
21207  }
21208 
21209  return result;
21210  }
21211 
21216  template<typename KeyT>
21217  const_iterator find(KeyT&& key) const
21218  {
21219  auto result = cend();
21220 
21221  if (is_object())
21222  {
21223  result.m_it.object_iterator = m_value.object->find(std::forward<KeyT>(key));
21224  }
21225 
21226  return result;
21227  }
21228 
21250  template<typename KeyT>
21251  size_type count(KeyT&& key) const
21252  {
21253  // return 0 for all nonobject types
21254  return is_object() ? m_value.object->count(std::forward<KeyT>(key)) : 0;
21255  }
21256 
21282  template < typename KeyT, typename std::enable_if <
21283  !std::is_same<typename std::decay<KeyT>::type, json_pointer>::value, int >::type = 0 >
21284  bool contains(KeyT && key) const
21285  {
21286  return is_object() && m_value.object->find(std::forward<KeyT>(key)) != m_value.object->end();
21287  }
21288 
21315  bool contains(const json_pointer& ptr) const
21316  {
21317  return ptr.contains(this);
21318  }
21319 
21321 
21322 
21324  // iterators //
21326 
21329 
21354  iterator begin() noexcept
21355  {
21356  iterator result(this);
21357  result.set_begin();
21358  return result;
21359  }
21360 
21364  const_iterator begin() const noexcept
21365  {
21366  return cbegin();
21367  }
21368 
21394  const_iterator cbegin() const noexcept
21395  {
21396  const_iterator result(this);
21397  result.set_begin();
21398  return result;
21399  }
21400 
21425  iterator end() noexcept
21426  {
21427  iterator result(this);
21428  result.set_end();
21429  return result;
21430  }
21431 
21435  const_iterator end() const noexcept
21436  {
21437  return cend();
21438  }
21439 
21465  const_iterator cend() const noexcept
21466  {
21467  const_iterator result(this);
21468  result.set_end();
21469  return result;
21470  }
21471 
21495  reverse_iterator rbegin() noexcept
21496  {
21497  return reverse_iterator(end());
21498  }
21499 
21503  const_reverse_iterator rbegin() const noexcept
21504  {
21505  return crbegin();
21506  }
21507 
21532  reverse_iterator rend() noexcept
21533  {
21534  return reverse_iterator(begin());
21535  }
21536 
21540  const_reverse_iterator rend() const noexcept
21541  {
21542  return crend();
21543  }
21544 
21569  const_reverse_iterator crbegin() const noexcept
21570  {
21571  return const_reverse_iterator(cend());
21572  }
21573 
21598  const_reverse_iterator crend() const noexcept
21599  {
21600  return const_reverse_iterator(cbegin());
21601  }
21602 
21603  public:
21661  JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items())
21662  static iteration_proxy<iterator> iterator_wrapper(reference ref) noexcept
21663  {
21664  return ref.items();
21665  }
21666 
21670  JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items())
21671  static iteration_proxy<const_iterator> iterator_wrapper(const_reference ref) noexcept
21672  {
21673  return ref.items();
21674  }
21675 
21744  iteration_proxy<iterator> items() noexcept
21745  {
21746  return iteration_proxy<iterator>(*this);
21747  }
21748 
21752  iteration_proxy<const_iterator> items() const noexcept
21753  {
21754  return iteration_proxy<const_iterator>(*this);
21755  }
21756 
21758 
21759 
21761  // capacity //
21763 
21766 
21809  bool empty() const noexcept
21810  {
21811  switch (m_type)
21812  {
21813  case value_t::null:
21814  {
21815  // null values are empty
21816  return true;
21817  }
21818 
21819  case value_t::array:
21820  {
21821  // delegate call to array_t::empty()
21822  return m_value.array->empty();
21823  }
21824 
21825  case value_t::object:
21826  {
21827  // delegate call to object_t::empty()
21828  return m_value.object->empty();
21829  }
21830 
21831  default:
21832  {
21833  // all other types are nonempty
21834  return false;
21835  }
21836  }
21837  }
21838 
21882  size_type size() const noexcept
21883  {
21884  switch (m_type)
21885  {
21886  case value_t::null:
21887  {
21888  // null values are empty
21889  return 0;
21890  }
21891 
21892  case value_t::array:
21893  {
21894  // delegate call to array_t::size()
21895  return m_value.array->size();
21896  }
21897 
21898  case value_t::object:
21899  {
21900  // delegate call to object_t::size()
21901  return m_value.object->size();
21902  }
21903 
21904  default:
21905  {
21906  // all other types have size 1
21907  return 1;
21908  }
21909  }
21910  }
21911 
21953  size_type max_size() const noexcept
21954  {
21955  switch (m_type)
21956  {
21957  case value_t::array:
21958  {
21959  // delegate call to array_t::max_size()
21960  return m_value.array->max_size();
21961  }
21962 
21963  case value_t::object:
21964  {
21965  // delegate call to object_t::max_size()
21966  return m_value.object->max_size();
21967  }
21968 
21969  default:
21970  {
21971  // all other types have max_size() == size()
21972  return size();
21973  }
21974  }
21975  }
21976 
21978 
21979 
21981  // modifiers //
21983 
21986 
22024  void clear() noexcept
22025  {
22026  switch (m_type)
22027  {
22028  case value_t::number_integer:
22029  {
22030  m_value.number_integer = 0;
22031  break;
22032  }
22033 
22034  case value_t::number_unsigned:
22035  {
22036  m_value.number_unsigned = 0;
22037  break;
22038  }
22039 
22040  case value_t::number_float:
22041  {
22042  m_value.number_float = 0.0;
22043  break;
22044  }
22045 
22046  case value_t::boolean:
22047  {
22048  m_value.boolean = false;
22049  break;
22050  }
22051 
22052  case value_t::string:
22053  {
22054  m_value.string->clear();
22055  break;
22056  }
22057 
22058  case value_t::binary:
22059  {
22060  m_value.binary->clear();
22061  break;
22062  }
22063 
22064  case value_t::array:
22065  {
22066  m_value.array->clear();
22067  break;
22068  }
22069 
22070  case value_t::object:
22071  {
22072  m_value.object->clear();
22073  break;
22074  }
22075 
22076  default:
22077  break;
22078  }
22079  }
22080 
22101  void push_back(basic_json&& val)
22102  {
22103  // push_back only works for null objects or arrays
22104  if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
22105  {
22106  JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), *this));
22107  }
22108 
22109  // transform null object into an array
22110  if (is_null())
22111  {
22112  m_type = value_t::array;
22113  m_value = value_t::array;
22114  assert_invariant();
22115  }
22116 
22117  // add element to array (move semantics)
22118  m_value.array->push_back(std::move(val));
22119  set_parent(m_value.array->back());
22120  // if val is moved from, basic_json move constructor marks it null so we do not call the destructor
22121  }
22122 
22128  {
22129  push_back(std::move(val));
22130  return *this;
22131  }
22132 
22137  void push_back(const basic_json& val)
22138  {
22139  // push_back only works for null objects or arrays
22140  if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
22141  {
22142  JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), *this));
22143  }
22144 
22145  // transform null object into an array
22146  if (is_null())
22147  {
22148  m_type = value_t::array;
22149  m_value = value_t::array;
22150  assert_invariant();
22151  }
22152 
22153  // add element to array
22154  m_value.array->push_back(val);
22155  set_parent(m_value.array->back());
22156  }
22157 
22162  reference operator+=(const basic_json& val)
22163  {
22164  push_back(val);
22165  return *this;
22166  }
22167 
22188  void push_back(const typename object_t::value_type& val)
22189  {
22190  // push_back only works for null objects or objects
22191  if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
22192  {
22193  JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), *this));
22194  }
22195 
22196  // transform null object into an object
22197  if (is_null())
22198  {
22199  m_type = value_t::object;
22200  m_value = value_t::object;
22201  assert_invariant();
22202  }
22203 
22204  // add element to object
22205  auto res = m_value.object->insert(val);
22206  set_parent(res.first->second);
22207  }
22208 
22213  reference operator+=(const typename object_t::value_type& val)
22214  {
22215  push_back(val);
22216  return *this;
22217  }
22218 
22244  void push_back(initializer_list_t init)
22245  {
22246  if (is_object() && init.size() == 2 && (*init.begin())->is_string())
22247  {
22248  basic_json&& key = init.begin()->moved_or_copied();
22249  push_back(typename object_t::value_type(
22250  std::move(key.get_ref<string_t&>()), (init.begin() + 1)->moved_or_copied()));
22251  }
22252  else
22253  {
22255  }
22256  }
22257 
22263  {
22264  push_back(init);
22265  return *this;
22266  }
22267 
22291  template<class... Args>
22292  reference emplace_back(Args&& ... args)
22293  {
22294  // emplace_back only works for null objects or arrays
22295  if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
22296  {
22297  JSON_THROW(type_error::create(311, "cannot use emplace_back() with " + std::string(type_name()), *this));
22298  }
22299 
22300  // transform null object into an array
22301  if (is_null())
22302  {
22303  m_type = value_t::array;
22304  m_value = value_t::array;
22305  assert_invariant();
22306  }
22307 
22308  // add element to array (perfect forwarding)
22309 #ifdef JSON_HAS_CPP_17
22310  return set_parent(m_value.array->emplace_back(std::forward<Args>(args)...));
22311 #else
22312  m_value.array->emplace_back(std::forward<Args>(args)...);
22313  return set_parent(m_value.array->back());
22314 #endif
22315  }
22316 
22344  template<class... Args>
22345  std::pair<iterator, bool> emplace(Args&& ... args)
22346  {
22347  // emplace only works for null objects or arrays
22348  if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object())))
22349  {
22350  JSON_THROW(type_error::create(311, "cannot use emplace() with " + std::string(type_name()), *this));
22351  }
22352 
22353  // transform null object into an object
22354  if (is_null())
22355  {
22356  m_type = value_t::object;
22357  m_value = value_t::object;
22358  assert_invariant();
22359  }
22360 
22361  // add element to array (perfect forwarding)
22362  auto res = m_value.object->emplace(std::forward<Args>(args)...);
22363  set_parent(res.first->second);
22364 
22365  // create result iterator and set iterator to the result of emplace
22366  auto it = begin();
22367  it.m_it.object_iterator = res.first;
22368 
22369  // return pair of iterator and boolean
22370  return {it, res.second};
22371  }
22372 
22376  template<typename... Args>
22377  iterator insert_iterator(const_iterator pos, Args&& ... args)
22378  {
22379  iterator result(this);
22380  JSON_ASSERT(m_value.array != nullptr);
22381 
22382  auto insert_pos = std::distance(m_value.array->begin(), pos.m_it.array_iterator);
22383  m_value.array->insert(pos.m_it.array_iterator, std::forward<Args>(args)...);
22384  result.m_it.array_iterator = m_value.array->begin() + insert_pos;
22385 
22386  // This could have been written as:
22387  // result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, cnt, val);
22388  // but the return value of insert is missing in GCC 4.8, so it is written this way instead.
22389 
22390  return result;
22391  }
22392 
22415  iterator insert(const_iterator pos, const basic_json& val)
22416  {
22417  // insert only works for arrays
22418  if (JSON_HEDLEY_LIKELY(is_array()))
22419  {
22420  // check if iterator pos fits to this JSON value
22421  if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
22422  {
22423  JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", *this));
22424  }
22426  // insert to array and return iterator
22427  return set_parents(insert_iterator(pos, val), static_cast<typename iterator::difference_type>(1));
22428  }
22429 
22430  JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this));
22431  }
22432 
22438  {
22439  return insert(pos, val);
22440  }
22441 
22466  iterator insert(const_iterator pos, size_type cnt, const basic_json& val)
22467  {
22468  // insert only works for arrays
22469  if (JSON_HEDLEY_LIKELY(is_array()))
22470  {
22471  // check if iterator pos fits to this JSON value
22472  if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
22473  {
22474  JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", *this));
22475  }
22477  // insert to array and return iterator
22478  return set_parents(insert_iterator(pos, cnt, val), static_cast<typename iterator::difference_type>(cnt));
22479  }
22480 
22481  JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this));
22482  }
22483 
22515  {
22516  // insert only works for arrays
22517  if (JSON_HEDLEY_UNLIKELY(!is_array()))
22518  {
22519  JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this));
22520  }
22521 
22522  // check if iterator pos fits to this JSON value
22523  if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
22524  {
22525  JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", *this));
22526  }
22527 
22528  // check if range iterators belong to the same JSON object
22529  if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
22530  {
22531  JSON_THROW(invalid_iterator::create(210, "iterators do not fit", *this));
22532  }
22533 
22534  if (JSON_HEDLEY_UNLIKELY(first.m_object == this))
22535  {
22536  JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container", *this));
22537  }
22538 
22539  // insert to array and return iterator
22540  return set_parents(insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator), std::distance(first, last));
22541  }
22542 
22568  {
22569  // insert only works for arrays
22570  if (JSON_HEDLEY_UNLIKELY(!is_array()))
22571  {
22572  JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this));
22573  }
22574 
22575  // check if iterator pos fits to this JSON value
22576  if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
22577  {
22578  JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", *this));
22579  }
22580 
22581  // insert to array and return iterator
22582  return set_parents(insert_iterator(pos, ilist.begin(), ilist.end()), static_cast<typename iterator::difference_type>(ilist.size()));
22583  }
22584 
22608  void insert(const_iterator first, const_iterator last)
22609  {
22610  // insert only works for objects
22611  if (JSON_HEDLEY_UNLIKELY(!is_object()))
22612  {
22613  JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this));
22614  }
22615 
22616  // check if range iterators belong to the same JSON object
22617  if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
22618  {
22619  JSON_THROW(invalid_iterator::create(210, "iterators do not fit", *this));
22620  }
22621 
22622  // passed iterators must belong to objects
22623  if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()))
22624  {
22625  JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", *this));
22626  }
22627 
22628  m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator);
22629  }
22630 
22650  void update(const_reference j)
22651  {
22652  // implicitly convert null value to an empty object
22653  if (is_null())
22654  {
22655  m_type = value_t::object;
22656  m_value.object = create<object_t>();
22657  assert_invariant();
22658  }
22659 
22660  if (JSON_HEDLEY_UNLIKELY(!is_object()))
22661  {
22662  JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()), *this));
22663  }
22664  if (JSON_HEDLEY_UNLIKELY(!j.is_object()))
22665  {
22666  JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(j.type_name()), *this));
22667  }
22668 
22669  for (auto it = j.cbegin(); it != j.cend(); ++it)
22670  {
22671  m_value.object->operator[](it.key()) = it.value();
22672  }
22673  }
22674 
22701  void update(const_iterator first, const_iterator last)
22702  {
22703  // implicitly convert null value to an empty object
22704  if (is_null())
22705  {
22706  m_type = value_t::object;
22707  m_value.object = create<object_t>();
22708  assert_invariant();
22709  }
22710 
22711  if (JSON_HEDLEY_UNLIKELY(!is_object()))
22712  {
22713  JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()), *this));
22714  }
22715 
22716  // check if range iterators belong to the same JSON object
22717  if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
22718  {
22719  JSON_THROW(invalid_iterator::create(210, "iterators do not fit", *this));
22720  }
22721 
22722  // passed iterators must belong to objects
22723  if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object()
22724  || !last.m_object->is_object()))
22725  {
22726  JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", *this));
22727  }
22728 
22729  for (auto it = first; it != last; ++it)
22730  {
22731  m_value.object->operator[](it.key()) = it.value();
22732  }
22733  }
22734 
22752  void swap(reference other) noexcept (
22753  std::is_nothrow_move_constructible<value_t>::value&&
22754  std::is_nothrow_move_assignable<value_t>::value&&
22755  std::is_nothrow_move_constructible<json_value>::value&&
22756  std::is_nothrow_move_assignable<json_value>::value
22757  )
22758  {
22759  std::swap(m_type, other.m_type);
22760  std::swap(m_value, other.m_value);
22761 
22762  set_parents();
22763  other.set_parents();
22764  assert_invariant();
22765  }
22766 
22785  friend void swap(reference left, reference right) noexcept (
22786  std::is_nothrow_move_constructible<value_t>::value&&
22787  std::is_nothrow_move_assignable<value_t>::value&&
22788  std::is_nothrow_move_constructible<json_value>::value&&
22789  std::is_nothrow_move_assignable<json_value>::value
22790  )
22791  {
22792  left.swap(right);
22793  }
22794 
22815  void swap(array_t& other)
22816  {
22817  // swap only works for arrays
22818  if (JSON_HEDLEY_LIKELY(is_array()))
22819  {
22820  std::swap(*(m_value.array), other);
22821  }
22822  else
22823  {
22824  JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), *this));
22825  }
22826  }
22827 
22848  void swap(object_t& other)
22849  {
22850  // swap only works for objects
22851  if (JSON_HEDLEY_LIKELY(is_object()))
22852  {
22853  std::swap(*(m_value.object), other);
22854  }
22855  else
22856  {
22857  JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), *this));
22858  }
22859  }
22860 
22881  void swap(string_t& other)
22882  {
22883  // swap only works for strings
22884  if (JSON_HEDLEY_LIKELY(is_string()))
22885  {
22886  std::swap(*(m_value.string), other);
22887  }
22888  else
22889  {
22890  JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), *this));
22891  }
22892  }
22893 
22914  void swap(binary_t& other)
22915  {
22916  // swap only works for strings
22917  if (JSON_HEDLEY_LIKELY(is_binary()))
22918  {
22919  std::swap(*(m_value.binary), other);
22920  }
22921  else
22922  {
22923  JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), *this));
22924  }
22925  }
22926 
22928  void swap(typename binary_t::container_type& other)
22929  {
22930  // swap only works for strings
22931  if (JSON_HEDLEY_LIKELY(is_binary()))
22932  {
22933  std::swap(*(m_value.binary), other);
22934  }
22935  else
22936  {
22937  JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), *this));
22938  }
22939  }
22940 
22942 
22943  public:
22945  // lexicographical comparison operators //
22947 
22950 
23006  friend bool operator==(const_reference lhs, const_reference rhs) noexcept
23007  {
23008  const auto lhs_type = lhs.type();
23009  const auto rhs_type = rhs.type();
23010 
23011  if (lhs_type == rhs_type)
23012  {
23013  switch (lhs_type)
23014  {
23015  case value_t::array:
23016  return *lhs.m_value.array == *rhs.m_value.array;
23017 
23018  case value_t::object:
23019  return *lhs.m_value.object == *rhs.m_value.object;
23020 
23021  case value_t::null:
23022  return true;
23023 
23024  case value_t::string:
23025  return *lhs.m_value.string == *rhs.m_value.string;
23026 
23027  case value_t::boolean:
23028  return lhs.m_value.boolean == rhs.m_value.boolean;
23029 
23030  case value_t::number_integer:
23031  return lhs.m_value.number_integer == rhs.m_value.number_integer;
23032 
23033  case value_t::number_unsigned:
23034  return lhs.m_value.number_unsigned == rhs.m_value.number_unsigned;
23035 
23036  case value_t::number_float:
23037  return lhs.m_value.number_float == rhs.m_value.number_float;
23038 
23039  case value_t::binary:
23040  return *lhs.m_value.binary == *rhs.m_value.binary;
23041 
23042  default:
23043  return false;
23044  }
23045  }
23046  else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_float)
23047  {
23048  return static_cast<number_float_t>(lhs.m_value.number_integer) == rhs.m_value.number_float;
23049  }
23050  else if (lhs_type == value_t::number_float && rhs_type == value_t::number_integer)
23051  {
23052  return lhs.m_value.number_float == static_cast<number_float_t>(rhs.m_value.number_integer);
23053  }
23054  else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_float)
23055  {
23056  return static_cast<number_float_t>(lhs.m_value.number_unsigned) == rhs.m_value.number_float;
23057  }
23058  else if (lhs_type == value_t::number_float && rhs_type == value_t::number_unsigned)
23059  {
23060  return lhs.m_value.number_float == static_cast<number_float_t>(rhs.m_value.number_unsigned);
23061  }
23062  else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_integer)
23063  {
23064  return static_cast<number_integer_t>(lhs.m_value.number_unsigned) == rhs.m_value.number_integer;
23065  }
23066  else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_unsigned)
23067  {
23068  return lhs.m_value.number_integer == static_cast<number_integer_t>(rhs.m_value.number_unsigned);
23069  }
23070 
23071  return false;
23072  }
23073 
23078  template<typename ScalarType, typename std::enable_if<
23079  std::is_scalar<ScalarType>::value, int>::type = 0>
23080  friend bool operator==(const_reference lhs, const ScalarType rhs) noexcept
23081  {
23082  return lhs == basic_json(rhs);
23083  }
23084 
23089  template<typename ScalarType, typename std::enable_if<
23090  std::is_scalar<ScalarType>::value, int>::type = 0>
23091  friend bool operator==(const ScalarType lhs, const_reference rhs) noexcept
23092  {
23093  return basic_json(lhs) == rhs;
23094  }
23095 
23114  friend bool operator!=(const_reference lhs, const_reference rhs) noexcept
23115  {
23116  return !(lhs == rhs);
23117  }
23118 
23123  template<typename ScalarType, typename std::enable_if<
23124  std::is_scalar<ScalarType>::value, int>::type = 0>
23125  friend bool operator!=(const_reference lhs, const ScalarType rhs) noexcept
23126  {
23127  return lhs != basic_json(rhs);
23128  }
23129 
23134  template<typename ScalarType, typename std::enable_if<
23135  std::is_scalar<ScalarType>::value, int>::type = 0>
23136  friend bool operator!=(const ScalarType lhs, const_reference rhs) noexcept
23137  {
23138  return basic_json(lhs) != rhs;
23139  }
23140 
23167  friend bool operator<(const_reference lhs, const_reference rhs) noexcept
23168  {
23169  const auto lhs_type = lhs.type();
23170  const auto rhs_type = rhs.type();
23171 
23172  if (lhs_type == rhs_type)
23173  {
23174  switch (lhs_type)
23175  {
23176  case value_t::array:
23177  // note parentheses are necessary, see
23178  // https://github.com/nlohmann/json/issues/1530
23179  return (*lhs.m_value.array) < (*rhs.m_value.array);
23180 
23181  case value_t::object:
23182  return (*lhs.m_value.object) < (*rhs.m_value.object);
23183 
23184  case value_t::null:
23185  return false;
23186 
23187  case value_t::string:
23188  return (*lhs.m_value.string) < (*rhs.m_value.string);
23189 
23190  case value_t::boolean:
23191  return (lhs.m_value.boolean) < (rhs.m_value.boolean);
23192 
23193  case value_t::number_integer:
23194  return (lhs.m_value.number_integer) < (rhs.m_value.number_integer);
23195 
23196  case value_t::number_unsigned:
23197  return (lhs.m_value.number_unsigned) < (rhs.m_value.number_unsigned);
23198 
23199  case value_t::number_float:
23200  return (lhs.m_value.number_float) < (rhs.m_value.number_float);
23201 
23202  case value_t::binary:
23203  return (*lhs.m_value.binary) < (*rhs.m_value.binary);
23204 
23205  default:
23206  return false;
23207  }
23208  }
23209  else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_float)
23210  {
23211  return static_cast<number_float_t>(lhs.m_value.number_integer) < rhs.m_value.number_float;
23212  }
23213  else if (lhs_type == value_t::number_float && rhs_type == value_t::number_integer)
23214  {
23215  return lhs.m_value.number_float < static_cast<number_float_t>(rhs.m_value.number_integer);
23216  }
23217  else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_float)
23218  {
23219  return static_cast<number_float_t>(lhs.m_value.number_unsigned) < rhs.m_value.number_float;
23220  }
23221  else if (lhs_type == value_t::number_float && rhs_type == value_t::number_unsigned)
23222  {
23223  return lhs.m_value.number_float < static_cast<number_float_t>(rhs.m_value.number_unsigned);
23224  }
23225  else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_unsigned)
23226  {
23227  return lhs.m_value.number_integer < static_cast<number_integer_t>(rhs.m_value.number_unsigned);
23228  }
23229  else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_integer)
23230  {
23231  return static_cast<number_integer_t>(lhs.m_value.number_unsigned) < rhs.m_value.number_integer;
23232  }
23233 
23234  // We only reach this line if we cannot compare values. In that case,
23235  // we compare types. Note we have to call the operator explicitly,
23236  // because MSVC has problems otherwise.
23237  return operator<(lhs_type, rhs_type);
23238  }
23239 
23244  template<typename ScalarType, typename std::enable_if<
23245  std::is_scalar<ScalarType>::value, int>::type = 0>
23246  friend bool operator<(const_reference lhs, const ScalarType rhs) noexcept
23247  {
23248  return lhs < basic_json(rhs);
23249  }
23250 
23255  template<typename ScalarType, typename std::enable_if<
23256  std::is_scalar<ScalarType>::value, int>::type = 0>
23257  friend bool operator<(const ScalarType lhs, const_reference rhs) noexcept
23258  {
23259  return basic_json(lhs) < rhs;
23260  }
23261 
23281  friend bool operator<=(const_reference lhs, const_reference rhs) noexcept
23282  {
23283  return !(rhs < lhs);
23284  }
23285 
23290  template<typename ScalarType, typename std::enable_if<
23291  std::is_scalar<ScalarType>::value, int>::type = 0>
23292  friend bool operator<=(const_reference lhs, const ScalarType rhs) noexcept
23293  {
23294  return lhs <= basic_json(rhs);
23295  }
23296 
23301  template<typename ScalarType, typename std::enable_if<
23302  std::is_scalar<ScalarType>::value, int>::type = 0>
23303  friend bool operator<=(const ScalarType lhs, const_reference rhs) noexcept
23304  {
23305  return basic_json(lhs) <= rhs;
23306  }
23307 
23327  friend bool operator>(const_reference lhs, const_reference rhs) noexcept
23328  {
23329  return !(lhs <= rhs);
23330  }
23331 
23336  template<typename ScalarType, typename std::enable_if<
23337  std::is_scalar<ScalarType>::value, int>::type = 0>
23338  friend bool operator>(const_reference lhs, const ScalarType rhs) noexcept
23339  {
23340  return lhs > basic_json(rhs);
23341  }
23342 
23347  template<typename ScalarType, typename std::enable_if<
23348  std::is_scalar<ScalarType>::value, int>::type = 0>
23349  friend bool operator>(const ScalarType lhs, const_reference rhs) noexcept
23350  {
23351  return basic_json(lhs) > rhs;
23352  }
23353 
23373  friend bool operator>=(const_reference lhs, const_reference rhs) noexcept
23374  {
23375  return !(lhs < rhs);
23376  }
23377 
23382  template<typename ScalarType, typename std::enable_if<
23383  std::is_scalar<ScalarType>::value, int>::type = 0>
23384  friend bool operator>=(const_reference lhs, const ScalarType rhs) noexcept
23385  {
23386  return lhs >= basic_json(rhs);
23387  }
23388 
23393  template<typename ScalarType, typename std::enable_if<
23394  std::is_scalar<ScalarType>::value, int>::type = 0>
23395  friend bool operator>=(const ScalarType lhs, const_reference rhs) noexcept
23396  {
23397  return basic_json(lhs) >= rhs;
23398  }
23399 
23401 
23403  // serialization //
23408 
23440  friend std::ostream& operator<<(std::ostream& o, const basic_json& j)
23441  {
23442  // read width member and use it as indentation parameter if nonzero
23443  const bool pretty_print = o.width() > 0;
23444  const auto indentation = pretty_print ? o.width() : 0;
23445 
23446  // reset width to 0 for subsequent calls to this stream
23447  o.width(0);
23448 
23449  // do the actual serialization
23450  serializer s(detail::output_adapter<char>(o), o.fill());
23451  s.dump(j, pretty_print, false, static_cast<unsigned int>(indentation));
23452  return o;
23453  }
23454 
23463  JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator<<(std::ostream&, const basic_json&))
23464  friend std::ostream& operator>>(const basic_json& j, std::ostream& o)
23465  {
23466  return o << j;
23467  }
23468 
23470 
23471 
23473  // deserialization //
23475 
23478 
23530  template<typename InputType>
23531 
23532  static basic_json parse(InputType&& i,
23533  const parser_callback_t cb = nullptr,
23534  const bool allow_exceptions = true,
23535  const bool ignore_comments = false)
23536  {
23537  basic_json result;
23538  parser(detail::input_adapter(std::forward<InputType>(i)), cb, allow_exceptions, ignore_comments).parse(true, result);
23539  return result;
23540  }
23541 
23568  template<typename IteratorType>
23569 
23570  static basic_json parse(IteratorType first,
23571  IteratorType last,
23572  const parser_callback_t cb = nullptr,
23573  const bool allow_exceptions = true,
23574  const bool ignore_comments = false)
23575  {
23576  basic_json result;
23577  parser(detail::input_adapter(std::move(first), std::move(last)), cb, allow_exceptions, ignore_comments).parse(true, result);
23578  return result;
23579  }
23581 
23582  JSON_HEDLEY_DEPRECATED_FOR(3.8.0, parse(ptr, ptr + len))
23583  static basic_json parse(detail::span_input_adapter&& i,
23584  const parser_callback_t cb = nullptr,
23585  const bool allow_exceptions = true,
23586  const bool ignore_comments = false)
23587  {
23588  basic_json result;
23589  parser(i.get(), cb, allow_exceptions, ignore_comments).parse(true, result);
23590  return result;
23591  }
23592 
23623  template<typename InputType>
23624  static bool accept(InputType&& i,
23625  const bool ignore_comments = false)
23626  {
23627  return parser(detail::input_adapter(std::forward<InputType>(i)), nullptr, false, ignore_comments).accept(true);
23628  }
23629 
23630  template<typename IteratorType>
23631  static bool accept(IteratorType first, IteratorType last,
23632  const bool ignore_comments = false)
23633  {
23634  return parser(detail::input_adapter(std::move(first), std::move(last)), nullptr, false, ignore_comments).accept(true);
23635  }
23636 
23637 
23638  JSON_HEDLEY_DEPRECATED_FOR(3.8.0, accept(ptr, ptr + len))
23639  static bool accept(detail::span_input_adapter&& i,
23640  const bool ignore_comments = false)
23641  {
23642  return parser(i.get(), nullptr, false, ignore_comments).accept(true);
23643  }
23644 
23685  template <typename InputType, typename SAX>
23686  JSON_HEDLEY_NON_NULL(2)
23687  static bool sax_parse(InputType&& i, SAX* sax,
23689  const bool strict = true,
23690  const bool ignore_comments = false)
23691  {
23692  auto ia = detail::input_adapter(std::forward<InputType>(i));
23693  return format == input_format_t::json
23694  ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
23695  : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia)).sax_parse(format, sax, strict);
23696  }
23698  template<class IteratorType, class SAX>
23699  JSON_HEDLEY_NON_NULL(3)
23700  static bool sax_parse(IteratorType first, IteratorType last, SAX* sax,
23701  input_format_t format = input_format_t::json,
23702  const bool strict = true,
23703  const bool ignore_comments = false)
23704  {
23705  auto ia = detail::input_adapter(std::move(first), std::move(last));
23706  return format == input_format_t::json
23707  ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
23708  : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia)).sax_parse(format, sax, strict);
23709  }
23711  template <typename SAX>
23712  JSON_HEDLEY_DEPRECATED_FOR(3.8.0, sax_parse(ptr, ptr + len, ...))
23713  JSON_HEDLEY_NON_NULL(2)
23714  static bool sax_parse(detail::span_input_adapter&& i, SAX* sax,
23715  input_format_t format = input_format_t::json,
23716  const bool strict = true,
23717  const bool ignore_comments = false)
23718  {
23719  auto ia = i.get();
23720  return format == input_format_t::json
23721  ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict)
23722  : detail::binary_reader<basic_json, decltype(ia), SAX>(std::move(ia)).sax_parse(format, sax, strict);
23723  }
23733  JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator>>(std::istream&, basic_json&))
23734  friend std::istream& operator<<(basic_json& j, std::istream& i)
23735  {
23736  return operator>>(i, j);
23737  }
23738 
23764  friend std::istream& operator>>(std::istream& i, basic_json& j)
23765  {
23766  parser(detail::input_adapter(i)).parse(false, j);
23767  return i;
23768  }
23769 
23771 
23773  // convenience functions //
23775 
23807 
23808  const char* type_name() const noexcept
23809  {
23810  {
23811  switch (m_type)
23812  {
23813  case value_t::null:
23814  return "null";
23815  case value_t::object:
23816  return "object";
23817  case value_t::array:
23818  return "array";
23819  case value_t::string:
23820  return "string";
23821  case value_t::boolean:
23822  return "boolean";
23823  case value_t::binary:
23824  return "binary";
23825  case value_t::discarded:
23826  return "discarded";
23827  default:
23828  return "number";
23829  }
23830  }
23831  }
23832 
23833 
23834  JSON_PRIVATE_UNLESS_TESTED:
23836  // member variables //
23838 
23840  value_t m_type = value_t::null;
23841 
23843  json_value m_value = {};
23845 #if JSON_DIAGNOSTICS
23847  basic_json* m_parent = nullptr;
23848 #endif
23849 
23851  // binary serialization/deserialization //
23856 
23857  public:
23952  static std::vector<uint8_t> to_cbor(const basic_json& j)
23953  {
23954  std::vector<uint8_t> result;
23955  to_cbor(j, result);
23956  return result;
23957  }
23958 
23959  static void to_cbor(const basic_json& j, detail::output_adapter<uint8_t> o)
23960  {
23961  binary_writer<uint8_t>(o).write_cbor(j);
23962  }
23963 
23964  static void to_cbor(const basic_json& j, detail::output_adapter<char> o)
23965  {
23966  binary_writer<char>(o).write_cbor(j);
23967  }
23968 
24047  static std::vector<uint8_t> to_msgpack(const basic_json& j)
24048  {
24049  std::vector<uint8_t> result;
24050  to_msgpack(j, result);
24051  return result;
24052  }
24053 
24054  static void to_msgpack(const basic_json& j, detail::output_adapter<uint8_t> o)
24055  {
24056  binary_writer<uint8_t>(o).write_msgpack(j);
24057  }
24058 
24059  static void to_msgpack(const basic_json& j, detail::output_adapter<char> o)
24060  {
24061  binary_writer<char>(o).write_msgpack(j);
24062  }
24063 
24150  static std::vector<uint8_t> to_ubjson(const basic_json& j,
24151  const bool use_size = false,
24152  const bool use_type = false)
24153  {
24154  std::vector<uint8_t> result;
24155  to_ubjson(j, result, use_size, use_type);
24156  return result;
24157  }
24158 
24159  static void to_ubjson(const basic_json& j, detail::output_adapter<uint8_t> o,
24160  const bool use_size = false, const bool use_type = false)
24161  {
24162  binary_writer<uint8_t>(o).write_ubjson(j, use_size, use_type);
24163  }
24164 
24165  static void to_ubjson(const basic_json& j, detail::output_adapter<char> o,
24166  const bool use_size = false, const bool use_type = false)
24167  {
24168  binary_writer<char>(o).write_ubjson(j, use_size, use_type);
24169  }
24170 
24171 
24228  static std::vector<uint8_t> to_bson(const basic_json& j)
24229  {
24230  std::vector<uint8_t> result;
24231  to_bson(j, result);
24232  return result;
24233  }
24234 
24243  static void to_bson(const basic_json& j, detail::output_adapter<uint8_t> o)
24244  {
24245  binary_writer<uint8_t>(o).write_bson(j);
24246  }
24247 
24251  static void to_bson(const basic_json& j, detail::output_adapter<char> o)
24252  {
24253  binary_writer<char>(o).write_bson(j);
24254  }
24255 
24256 
24359  template<typename InputType>
24360 
24361  static basic_json from_cbor(InputType&& i,
24362  const bool strict = true,
24363  const bool allow_exceptions = true,
24364  const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
24365  {
24366  basic_json result;
24367  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
24368  auto ia = detail::input_adapter(std::forward<InputType>(i));
24369  const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler);
24370  return res ? result : basic_json(value_t::discarded);
24371  }
24372 
24376  template<typename IteratorType>
24377 
24378  static basic_json from_cbor(IteratorType first, IteratorType last,
24379  const bool strict = true,
24380  const bool allow_exceptions = true,
24381  const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
24382  {
24383  basic_json result;
24384  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
24385  auto ia = detail::input_adapter(std::move(first), std::move(last));
24386  const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler);
24387  return res ? result : basic_json(value_t::discarded);
24388  }
24389 
24390  template<typename T>
24391 
24392  JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len))
24393  static basic_json from_cbor(const T* ptr, std::size_t len,
24394  const bool strict = true,
24395  const bool allow_exceptions = true,
24396  const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
24397  {
24398  return from_cbor(ptr, ptr + len, strict, allow_exceptions, tag_handler);
24399  }
24400 
24401 
24402 
24403  JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len))
24404  static basic_json from_cbor(detail::span_input_adapter&& i,
24405  const bool strict = true,
24406  const bool allow_exceptions = true,
24407  const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
24408  {
24409  basic_json result;
24410  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
24411  auto ia = i.get();
24412  const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler);
24413  return res ? result : basic_json(value_t::discarded);
24414  }
24415 
24502  template<typename InputType>
24503 
24504  static basic_json from_msgpack(InputType&& i,
24505  const bool strict = true,
24506  const bool allow_exceptions = true)
24507  {
24508  basic_json result;
24509  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
24510  auto ia = detail::input_adapter(std::forward<InputType>(i));
24511  const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::msgpack, &sdp, strict);
24512  return res ? result : basic_json(value_t::discarded);
24513  }
24518  template<typename IteratorType>
24519 
24520  static basic_json from_msgpack(IteratorType first, IteratorType last,
24521  const bool strict = true,
24522  const bool allow_exceptions = true)
24523  {
24524  basic_json result;
24525  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
24526  auto ia = detail::input_adapter(std::move(first), std::move(last));
24527  const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::msgpack, &sdp, strict);
24528  return res ? result : basic_json(value_t::discarded);
24529  }
24531 
24532  template<typename T>
24533 
24534  JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len))
24535  static basic_json from_msgpack(const T* ptr, std::size_t len,
24536  const bool strict = true,
24537  const bool allow_exceptions = true)
24538  {
24539  return from_msgpack(ptr, ptr + len, strict, allow_exceptions);
24540  }
24541 
24542 
24543  JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len))
24544  static basic_json from_msgpack(detail::span_input_adapter&& i,
24545  const bool strict = true,
24546  const bool allow_exceptions = true)
24547  {
24548  basic_json result;
24549  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
24550  auto ia = i.get();
24551  const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::msgpack, &sdp, strict);
24552  return res ? result : basic_json(value_t::discarded);
24553  }
24555 
24618  template<typename InputType>
24619 
24620  static basic_json from_ubjson(InputType&& i,
24621  const bool strict = true,
24622  const bool allow_exceptions = true)
24623  {
24624  basic_json result;
24625  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
24626  auto ia = detail::input_adapter(std::forward<InputType>(i));
24627  const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::ubjson, &sdp, strict);
24628  return res ? result : basic_json(value_t::discarded);
24629  }
24634  template<typename IteratorType>
24635 
24636  static basic_json from_ubjson(IteratorType first, IteratorType last,
24637  const bool strict = true,
24638  const bool allow_exceptions = true)
24639  {
24640  basic_json result;
24641  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
24642  auto ia = detail::input_adapter(std::move(first), std::move(last));
24643  const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::ubjson, &sdp, strict);
24644  return res ? result : basic_json(value_t::discarded);
24645  }
24647  template<typename T>
24648 
24649  JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len))
24650  static basic_json from_ubjson(const T* ptr, std::size_t len,
24651  const bool strict = true,
24652  const bool allow_exceptions = true)
24653  {
24654  return from_ubjson(ptr, ptr + len, strict, allow_exceptions);
24655  }
24656 
24657 
24658  JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len))
24659  static basic_json from_ubjson(detail::span_input_adapter&& i,
24660  const bool strict = true,
24661  const bool allow_exceptions = true)
24662  {
24663  basic_json result;
24664  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
24665  auto ia = i.get();
24666  const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::ubjson, &sdp, strict);
24667  return res ? result : basic_json(value_t::discarded);
24668  }
24670 
24731  template<typename InputType>
24732 
24733  static basic_json from_bson(InputType&& i,
24734  const bool strict = true,
24735  const bool allow_exceptions = true)
24736  {
24737  basic_json result;
24738  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
24739  auto ia = detail::input_adapter(std::forward<InputType>(i));
24740  const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::bson, &sdp, strict);
24741  return res ? result : basic_json(value_t::discarded);
24742  }
24747  template<typename IteratorType>
24748 
24749  static basic_json from_bson(IteratorType first, IteratorType last,
24750  const bool strict = true,
24751  const bool allow_exceptions = true)
24752  {
24753  basic_json result;
24754  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
24755  auto ia = detail::input_adapter(std::move(first), std::move(last));
24756  const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::bson, &sdp, strict);
24757  return res ? result : basic_json(value_t::discarded);
24758  }
24760  template<typename T>
24761 
24762  JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len))
24763  static basic_json from_bson(const T* ptr, std::size_t len,
24764  const bool strict = true,
24765  const bool allow_exceptions = true)
24766  {
24767  return from_bson(ptr, ptr + len, strict, allow_exceptions);
24768  }
24769 
24770 
24771  JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len))
24772  static basic_json from_bson(detail::span_input_adapter&& i,
24773  const bool strict = true,
24774  const bool allow_exceptions = true)
24775  {
24776  basic_json result;
24777  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
24778  auto ia = i.get();
24779  const bool res = binary_reader<decltype(ia)>(std::move(ia)).sax_parse(input_format_t::bson, &sdp, strict);
24780  return res ? result : basic_json(value_t::discarded);
24781  }
24783 
24785  // JSON Pointer support //
24787 
24790 
24824  reference operator[](const json_pointer& ptr)
24825  {
24826  return ptr.get_unchecked(this);
24827  }
24828 
24852  const_reference operator[](const json_pointer& ptr) const
24853  {
24854  return ptr.get_unchecked(this);
24855  }
24856 
24895  reference at(const json_pointer& ptr)
24896  {
24897  return ptr.get_checked(this);
24898  }
24899 
24938  const_reference at(const json_pointer& ptr) const
24939  {
24940  return ptr.get_checked(this);
24941  }
24942 
24965  basic_json flatten() const
24966  {
24967  basic_json result(value_t::object);
24968  json_pointer::flatten("", *this, result);
24969  return result;
24970  }
24971 
25002  basic_json unflatten() const
25003  {
25004  return json_pointer::unflatten(*this);
25005  }
25006 
25008 
25010  // JSON Patch functions //
25015 
25063  basic_json patch(const basic_json& json_patch) const
25064  {
25065  // make a working copy to apply the patch to
25066  basic_json result = *this;
25067 
25068  // the valid JSON Patch operations
25069  enum class patch_operations {add, remove, replace, move, copy, test, invalid};
25070 
25071  const auto get_op = [](const std::string & op)
25072  {
25073  if (op == "add")
25074  {
25075  return patch_operations::add;
25076  }
25077  if (op == "remove")
25078  {
25079  return patch_operations::remove;
25080  }
25081  if (op == "replace")
25082  {
25083  return patch_operations::replace;
25084  }
25085  if (op == "move")
25086  {
25087  return patch_operations::move;
25088  }
25089  if (op == "copy")
25090  {
25091  return patch_operations::copy;
25092  }
25093  if (op == "test")
25094  {
25095  return patch_operations::test;
25096  }
25097 
25098  return patch_operations::invalid;
25099  };
25100 
25101  // wrapper for "add" operation; add value at ptr
25102  const auto operation_add = [&result](json_pointer & ptr, basic_json val)
25103  {
25104  // adding to the root of the target document means replacing it
25105  if (ptr.empty())
25106  {
25107  result = val;
25108  return;
25109  }
25110 
25111  // make sure the top element of the pointer exists
25112  json_pointer top_pointer = ptr.top();
25113  if (top_pointer != ptr)
25114  {
25115  result.at(top_pointer);
25116  }
25117 
25118  // get reference to parent of JSON pointer ptr
25119  const auto last_path = ptr.back();
25120  ptr.pop_back();
25121  basic_json& parent = result[ptr];
25122 
25123  switch (parent.m_type)
25124  {
25125  case value_t::null:
25126  case value_t::object:
25127  {
25128  // use operator[] to add value
25129  parent[last_path] = val;
25130  break;
25131  }
25132 
25133  case value_t::array:
25134  {
25135  if (last_path == "-")
25136  {
25137  // special case: append to back
25138  parent.push_back(val);
25139  }
25140  else
25141  {
25142  const auto idx = json_pointer::array_index(last_path);
25143  if (JSON_HEDLEY_UNLIKELY(idx > parent.size()))
25144  {
25145  // avoid undefined behavior
25146  JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", parent));
25147  }
25148 
25149  // default case: insert add offset
25150  parent.insert(parent.begin() + static_cast<difference_type>(idx), val);
25151  }
25152  break;
25153  }
25154 
25155  // if there exists a parent it cannot be primitive
25156  default: // LCOV_EXCL_LINE
25157  JSON_ASSERT(false); // LCOV_EXCL_LINE
25158  }
25159  };
25160 
25161  // wrapper for "remove" operation; remove value at ptr
25162  const auto operation_remove = [this, &result](json_pointer & ptr)
25163  {
25164  // get reference to parent of JSON pointer ptr
25165  const auto last_path = ptr.back();
25166  ptr.pop_back();
25167  basic_json& parent = result.at(ptr);
25168 
25169  // remove child
25170  if (parent.is_object())
25171  {
25172  // perform range check
25173  auto it = parent.find(last_path);
25174  if (JSON_HEDLEY_LIKELY(it != parent.end()))
25175  {
25176  parent.erase(it);
25177  }
25178  else
25179  {
25180  JSON_THROW(out_of_range::create(403, "key '" + last_path + "' not found", *this));
25181  }
25182  }
25183  else if (parent.is_array())
25184  {
25185  // note erase performs range check
25186  parent.erase(json_pointer::array_index(last_path));
25187  }
25188  };
25189 
25190  // type check: top level value must be an array
25191  if (JSON_HEDLEY_UNLIKELY(!json_patch.is_array()))
25192  {
25193  JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", json_patch));
25194  }
25195 
25196  // iterate and apply the operations
25197  for (const auto& val : json_patch)
25198  {
25199  // wrapper to get a value for an operation
25200  const auto get_value = [&val](const std::string & op,
25201  const std::string & member,
25202  bool string_type) -> basic_json &
25203  {
25204  // find value
25205  auto it = val.m_value.object->find(member);
25206 
25207  // context-sensitive error message
25208  const auto error_msg = (op == "op") ? "operation" : "operation '" + op + "'";
25209 
25210  // check if desired value is present
25211  if (JSON_HEDLEY_UNLIKELY(it == val.m_value.object->end()))
25212  {
25213  JSON_THROW(parse_error::create(105, 0, error_msg + " must have member '" + member + "'", val));
25214  }
25215 
25216  // check if result is of type string
25217  if (JSON_HEDLEY_UNLIKELY(string_type && !it->second.is_string()))
25218  {
25219  JSON_THROW(parse_error::create(105, 0, error_msg + " must have string member '" + member + "'", val));
25220  }
25221 
25222  // no error: return value
25223  return it->second;
25224  };
25225 
25226  // type check: every element of the array must be an object
25227  if (JSON_HEDLEY_UNLIKELY(!val.is_object()))
25228  {
25229  JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", val));
25230  }
25231 
25232  // collect mandatory members
25233  const auto op = get_value("op", "op", true).template get<std::string>();
25234  const auto path = get_value(op, "path", true).template get<std::string>();
25235  json_pointer ptr(path);
25236 
25237  switch (get_op(op))
25238  {
25239  case patch_operations::add:
25240  {
25241  operation_add(ptr, get_value("add", "value", false));
25242  break;
25243  }
25244 
25245  case patch_operations::remove:
25246  {
25247  operation_remove(ptr);
25248  break;
25249  }
25250 
25251  case patch_operations::replace:
25252  {
25253  // the "path" location must exist - use at()
25254  result.at(ptr) = get_value("replace", "value", false);
25255  break;
25256  }
25257 
25258  case patch_operations::move:
25259  {
25260  const auto from_path = get_value("move", "from", true).template get<std::string>();
25261  json_pointer from_ptr(from_path);
25262 
25263  // the "from" location must exist - use at()
25264  basic_json v = result.at(from_ptr);
25265 
25266  // The move operation is functionally identical to a
25267  // "remove" operation on the "from" location, followed
25268  // immediately by an "add" operation at the target
25269  // location with the value that was just removed.
25270  operation_remove(from_ptr);
25271  operation_add(ptr, v);
25272  break;
25273  }
25274 
25275  case patch_operations::copy:
25276  {
25277  const auto from_path = get_value("copy", "from", true).template get<std::string>();
25278  const json_pointer from_ptr(from_path);
25279 
25280  // the "from" location must exist - use at()
25281  basic_json v = result.at(from_ptr);
25282 
25283  // The copy is functionally identical to an "add"
25284  // operation at the target location using the value
25285  // specified in the "from" member.
25286  operation_add(ptr, v);
25287  break;
25288  }
25289 
25290  case patch_operations::test:
25291  {
25292  bool success = false;
25293  JSON_TRY
25294  {
25295  // check if "value" matches the one at "path"
25296  // the "path" location must exist - use at()
25297  success = (result.at(ptr) == get_value("test", "value", false));
25298  }
25299  JSON_INTERNAL_CATCH (out_of_range&)
25300  {
25301  // ignore out of range errors: success remains false
25302  }
25303 
25304  // throw an exception if test fails
25305  if (JSON_HEDLEY_UNLIKELY(!success))
25306  {
25307  JSON_THROW(other_error::create(501, "unsuccessful: " + val.dump(), val));
25308  }
25309 
25310  break;
25311  }
25312 
25313  default:
25314  {
25315  // op must be "add", "remove", "replace", "move", "copy", or
25316  // "test"
25317  JSON_THROW(parse_error::create(105, 0, "operation value '" + op + "' is invalid", val));
25318  }
25319  }
25320  }
25321 
25322  return result;
25323  }
25324 
25358 
25359  static basic_json diff(const basic_json& source, const basic_json& target,
25360  const std::string& path = "")
25361  {
25362  // the patch
25363  basic_json result(value_t::array);
25364 
25365  // if the values are the same, return empty patch
25366  if (source == target)
25367  {
25368  return result;
25369  }
25370 
25371  if (source.type() != target.type())
25372  {
25373  // different types: replace value
25374  result.push_back(
25375  {
25376  {"op", "replace"}, {"path", path}, {"value", target}
25377  });
25378  return result;
25379  }
25380 
25381  switch (source.type())
25382  {
25383  case value_t::array:
25384  {
25385  // first pass: traverse common elements
25386  std::size_t i = 0;
25387  while (i < source.size() && i < target.size())
25388  {
25389  // recursive call to compare array values at index i
25390  auto temp_diff = diff(source[i], target[i], path + "/" + std::to_string(i));
25391  result.insert(result.end(), temp_diff.begin(), temp_diff.end());
25392  ++i;
25393  }
25394 
25395  // i now reached the end of at least one array
25396  // in a second pass, traverse the remaining elements
25397 
25398  // remove my remaining elements
25399  const auto end_index = static_cast<difference_type>(result.size());
25400  while (i < source.size())
25401  {
25402  // add operations in reverse order to avoid invalid
25403  // indices
25404  result.insert(result.begin() + end_index, object(
25405  {
25406  {"op", "remove"},
25407  {"path", path + "/" + std::to_string(i)}
25408  }));
25409  ++i;
25410  }
25411 
25412  // add other remaining elements
25413  while (i < target.size())
25414  {
25415  result.push_back(
25416  {
25417  {"op", "add"},
25418  {"path", path + "/-"},
25419  {"value", target[i]}
25420  });
25421  ++i;
25422  }
25423 
25424  break;
25425  }
25426 
25427  case value_t::object:
25428  {
25429  // first pass: traverse this object's elements
25430  for (auto it = source.cbegin(); it != source.cend(); ++it)
25431  {
25432  // escape the key name to be used in a JSON patch
25433  const auto key = detail::escape(it.key());
25434 
25435  if (target.find(it.key()) != target.end())
25436  {
25437  // recursive call to compare object values at key it
25438  auto temp_diff = diff(it.value(), target[it.key()], path + "/" + key);
25439  result.insert(result.end(), temp_diff.begin(), temp_diff.end());
25440  }
25441  else
25442  {
25443  // found a key that is not in o -> remove it
25444  result.push_back(object(
25445  {
25446  {"op", "remove"}, {"path", path + "/" + key}
25447  }));
25448  }
25449  }
25450 
25451  // second pass: traverse other object's elements
25452  for (auto it = target.cbegin(); it != target.cend(); ++it)
25453  {
25454  if (source.find(it.key()) == source.end())
25455  {
25456  // found a key that is not in this -> add it
25457  const auto key = detail::escape(it.key());
25458  result.push_back(
25459  {
25460  {"op", "add"}, {"path", path + "/" + key},
25461  {"value", it.value()}
25462  });
25463  }
25464  }
25465 
25466  break;
25467  }
25468 
25469  default:
25470  {
25471  // both primitive type: replace value
25472  result.push_back(
25473  {
25474  {"op", "replace"}, {"path", path}, {"value", target}
25475  });
25476  break;
25477  }
25478  }
25479 
25480  return result;
25481  }
25482 
25484 
25486  // JSON Merge Patch functions //
25488 
25491 
25534  void merge_patch(const basic_json& apply_patch)
25535  {
25536  if (apply_patch.is_object())
25537  {
25538  if (!is_object())
25539  {
25540  *this = object();
25541  }
25542  for (auto it = apply_patch.begin(); it != apply_patch.end(); ++it)
25543  {
25544  if (it.value().is_null())
25545  {
25546  erase(it.key());
25547  }
25548  else
25549  {
25550  operator[](it.key()).merge_patch(it.value());
25551  }
25552  }
25553  }
25554  else
25555  {
25556  *this = apply_patch;
25557  }
25558  }
25559 
25561 };
25562 
25572 NLOHMANN_BASIC_JSON_TPL_DECLARATION
25573 std::string to_string(const NLOHMANN_BASIC_JSON_TPL& j)
25574 {
25575  return j.dump();
25576 }
25577 } // namespace nlohmann
25578 
25580 // nonmember support //
25582 
25583 // specialization of std::swap, and std::hash
25584 namespace std
25585 {
25586 
25588 template<>
25589 struct hash<nlohmann::json>
25590 {
25596  std::size_t operator()(const nlohmann::json& j) const
25597  {
25598  return nlohmann::detail::hash(j);
25599  }
25600 };
25601 
25605 template<>
25606 struct less<::nlohmann::detail::value_t>
25607 {
25612  bool operator()(nlohmann::detail::value_t lhs,
25613  nlohmann::detail::value_t rhs) const noexcept
25614  {
25615  return nlohmann::detail::operator<(lhs, rhs);
25616  }
25617 };
25618 
25619 // C++20 prohibit function specialization in the std namespace.
25620 #ifndef JSON_HAS_CPP_20
25621 
25627 template<>
25628 inline void swap<nlohmann::json>(nlohmann::json& j1, nlohmann::json& j2) noexcept(
25629  is_nothrow_move_constructible<nlohmann::json>::value&&
25630  is_nothrow_move_assignable<nlohmann::json>::value
25631  )
25632 {
25633  j1.swap(j2);
25634 }
25635 
25636 #endif
25637 
25638 } // namespace std
25639 
25653 JSON_HEDLEY_NON_NULL(1)
25654 inline nlohmann::json operator "" _json(const char* s, std::size_t n)
25655 {
25656  return nlohmann::json::parse(s, s + n);
25657 }
25658 
25672 JSON_HEDLEY_NON_NULL(1)
25673 inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std::size_t n)
25674 {
25675  return nlohmann::json::json_pointer(std::string(s, n));
25676 }
25677 
25678 // #include <nlohmann/detail/macro_unscope.hpp>
25679 
25680 
25681 // restore GCC/clang diagnostic settings
25682 #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
25683  #pragma GCC diagnostic pop
25684 #endif
25685 #if defined(__clang__)
25686  #pragma GCC diagnostic pop
25687 #endif
25688 
25689 // clean up
25690 #undef JSON_ASSERT
25691 #undef JSON_INTERNAL_CATCH
25692 #undef JSON_CATCH
25693 #undef JSON_THROW
25694 #undef JSON_TRY
25695 #undef JSON_PRIVATE_UNLESS_TESTED
25696 #undef JSON_HAS_CPP_14
25697 #undef JSON_HAS_CPP_17
25698 #undef NLOHMANN_BASIC_JSON_TPL_DECLARATION
25699 #undef NLOHMANN_BASIC_JSON_TPL
25700 #undef JSON_EXPLICIT
25701 
25702 // #include <nlohmann/thirdparty/hedley/hedley_undef.hpp>
25703 #undef JSON_HEDLEY_ALWAYS_INLINE
25704 #undef JSON_HEDLEY_ARM_VERSION
25705 #undef JSON_HEDLEY_ARM_VERSION_CHECK
25706 #undef JSON_HEDLEY_ARRAY_PARAM
25707 #undef JSON_HEDLEY_ASSUME
25708 #undef JSON_HEDLEY_BEGIN_C_DECLS
25709 #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE
25710 #undef JSON_HEDLEY_CLANG_HAS_BUILTIN
25711 #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE
25712 #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE
25713 #undef JSON_HEDLEY_CLANG_HAS_EXTENSION
25714 #undef JSON_HEDLEY_CLANG_HAS_FEATURE
25715 #undef JSON_HEDLEY_CLANG_HAS_WARNING
25716 #undef JSON_HEDLEY_COMPCERT_VERSION
25717 #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK
25718 #undef JSON_HEDLEY_CONCAT
25719 #undef JSON_HEDLEY_CONCAT3
25720 #undef JSON_HEDLEY_CONCAT3_EX
25721 #undef JSON_HEDLEY_CONCAT_EX
25722 #undef JSON_HEDLEY_CONST
25723 #undef JSON_HEDLEY_CONSTEXPR
25724 #undef JSON_HEDLEY_CONST_CAST
25725 #undef JSON_HEDLEY_CPP_CAST
25726 #undef JSON_HEDLEY_CRAY_VERSION
25727 #undef JSON_HEDLEY_CRAY_VERSION_CHECK
25728 #undef JSON_HEDLEY_C_DECL
25729 #undef JSON_HEDLEY_DEPRECATED
25730 #undef JSON_HEDLEY_DEPRECATED_FOR
25731 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL
25732 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_
25733 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED
25734 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES
25735 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
25736 #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION
25737 #undef JSON_HEDLEY_DIAGNOSTIC_POP
25738 #undef JSON_HEDLEY_DIAGNOSTIC_PUSH
25739 #undef JSON_HEDLEY_DMC_VERSION
25740 #undef JSON_HEDLEY_DMC_VERSION_CHECK
25741 #undef JSON_HEDLEY_EMPTY_BASES
25742 #undef JSON_HEDLEY_EMSCRIPTEN_VERSION
25743 #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK
25744 #undef JSON_HEDLEY_END_C_DECLS
25745 #undef JSON_HEDLEY_FLAGS
25746 #undef JSON_HEDLEY_FLAGS_CAST
25747 #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE
25748 #undef JSON_HEDLEY_GCC_HAS_BUILTIN
25749 #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE
25750 #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE
25751 #undef JSON_HEDLEY_GCC_HAS_EXTENSION
25752 #undef JSON_HEDLEY_GCC_HAS_FEATURE
25753 #undef JSON_HEDLEY_GCC_HAS_WARNING
25754 #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK
25755 #undef JSON_HEDLEY_GCC_VERSION
25756 #undef JSON_HEDLEY_GCC_VERSION_CHECK
25757 #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE
25758 #undef JSON_HEDLEY_GNUC_HAS_BUILTIN
25759 #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE
25760 #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE
25761 #undef JSON_HEDLEY_GNUC_HAS_EXTENSION
25762 #undef JSON_HEDLEY_GNUC_HAS_FEATURE
25763 #undef JSON_HEDLEY_GNUC_HAS_WARNING
25764 #undef JSON_HEDLEY_GNUC_VERSION
25765 #undef JSON_HEDLEY_GNUC_VERSION_CHECK
25766 #undef JSON_HEDLEY_HAS_ATTRIBUTE
25767 #undef JSON_HEDLEY_HAS_BUILTIN
25768 #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE
25769 #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS
25770 #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE
25771 #undef JSON_HEDLEY_HAS_EXTENSION
25772 #undef JSON_HEDLEY_HAS_FEATURE
25773 #undef JSON_HEDLEY_HAS_WARNING
25774 #undef JSON_HEDLEY_IAR_VERSION
25775 #undef JSON_HEDLEY_IAR_VERSION_CHECK
25776 #undef JSON_HEDLEY_IBM_VERSION
25777 #undef JSON_HEDLEY_IBM_VERSION_CHECK
25778 #undef JSON_HEDLEY_IMPORT
25779 #undef JSON_HEDLEY_INLINE
25780 #undef JSON_HEDLEY_INTEL_CL_VERSION
25781 #undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK
25782 #undef JSON_HEDLEY_INTEL_VERSION
25783 #undef JSON_HEDLEY_INTEL_VERSION_CHECK
25784 #undef JSON_HEDLEY_IS_CONSTANT
25785 #undef JSON_HEDLEY_IS_CONSTEXPR_
25786 #undef JSON_HEDLEY_LIKELY
25787 #undef JSON_HEDLEY_MALLOC
25788 #undef JSON_HEDLEY_MCST_LCC_VERSION
25789 #undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK
25790 #undef JSON_HEDLEY_MESSAGE
25791 #undef JSON_HEDLEY_MSVC_VERSION
25792 #undef JSON_HEDLEY_MSVC_VERSION_CHECK
25793 #undef JSON_HEDLEY_NEVER_INLINE
25794 #undef JSON_HEDLEY_NON_NULL
25795 #undef JSON_HEDLEY_NO_ESCAPE
25796 #undef JSON_HEDLEY_NO_RETURN
25797 #undef JSON_HEDLEY_NO_THROW
25798 #undef JSON_HEDLEY_NULL
25799 #undef JSON_HEDLEY_PELLES_VERSION
25800 #undef JSON_HEDLEY_PELLES_VERSION_CHECK
25801 #undef JSON_HEDLEY_PGI_VERSION
25802 #undef JSON_HEDLEY_PGI_VERSION_CHECK
25803 #undef JSON_HEDLEY_PREDICT
25804 #undef JSON_HEDLEY_PRINTF_FORMAT
25805 #undef JSON_HEDLEY_PRIVATE
25806 #undef JSON_HEDLEY_PUBLIC
25807 #undef JSON_HEDLEY_PURE
25808 #undef JSON_HEDLEY_REINTERPRET_CAST
25809 #undef JSON_HEDLEY_REQUIRE
25810 #undef JSON_HEDLEY_REQUIRE_CONSTEXPR
25811 #undef JSON_HEDLEY_REQUIRE_MSG
25812 #undef JSON_HEDLEY_RESTRICT
25813 #undef
25814 #undef JSON_HEDLEY_SENTINEL
25815 #undef JSON_HEDLEY_STATIC_ASSERT
25816 #undef JSON_HEDLEY_STATIC_CAST
25817 #undef JSON_HEDLEY_STRINGIFY
25818 #undef JSON_HEDLEY_STRINGIFY_EX
25819 #undef JSON_HEDLEY_SUNPRO_VERSION
25820 #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK
25821 #undef JSON_HEDLEY_TINYC_VERSION
25822 #undef JSON_HEDLEY_TINYC_VERSION_CHECK
25823 #undef JSON_HEDLEY_TI_ARMCL_VERSION
25824 #undef JSON_HEDLEY_TI_ARMCL_VERSION_CHECK
25825 #undef JSON_HEDLEY_TI_CL2000_VERSION
25826 #undef JSON_HEDLEY_TI_CL2000_VERSION_CHECK
25827 #undef JSON_HEDLEY_TI_CL430_VERSION
25828 #undef JSON_HEDLEY_TI_CL430_VERSION_CHECK
25829 #undef JSON_HEDLEY_TI_CL6X_VERSION
25830 #undef JSON_HEDLEY_TI_CL6X_VERSION_CHECK
25831 #undef JSON_HEDLEY_TI_CL7X_VERSION
25832 #undef JSON_HEDLEY_TI_CL7X_VERSION_CHECK
25833 #undef JSON_HEDLEY_TI_CLPRU_VERSION
25834 #undef JSON_HEDLEY_TI_CLPRU_VERSION_CHECK
25835 #undef JSON_HEDLEY_TI_VERSION
25836 #undef JSON_HEDLEY_TI_VERSION_CHECK
25837 #undef JSON_HEDLEY_UNAVAILABLE
25838 #undef JSON_HEDLEY_UNLIKELY
25839 #undef JSON_HEDLEY_UNPREDICTABLE
25840 #undef JSON_HEDLEY_UNREACHABLE
25841 #undef JSON_HEDLEY_UNREACHABLE_RETURN
25842 #undef JSON_HEDLEY_VERSION
25843 #undef JSON_HEDLEY_VERSION_DECODE_MAJOR
25844 #undef JSON_HEDLEY_VERSION_DECODE_MINOR
25845 #undef JSON_HEDLEY_VERSION_DECODE_REVISION
25846 #undef JSON_HEDLEY_VERSION_ENCODE
25847 #undef JSON_HEDLEY_WARNING
25848 #undef
25849 #undef _MSG
25850 #undef JSON_HEDLEY_FALL_THROUGH
25851 
25852 
25853 
25854 #endif // INCLUDE_NLOHMANN_JSON_HPP_
static basic_json binary(typename binary_t::container_type &&init, std::uint8_t subtype)
explicitly create a binary array (with subtype)
Definition: json.hpp:18601
constexpr auto get() const noexcept -> decltype(std::declval< const basic_json_t & >().template get_ptr< PointerType >())
get a pointer value (explicit)
Definition: json.hpp:20008
detail::parser_callback_t< basic_json > parser_callback_t
per-element parser callback type
Definition: json.hpp:18163
bool contains(KeyT &&key) const
check the existence of an element in a JSON object
Definition: json.hpp:21294
const_reverse_iterator crbegin() const noexcept
returns a const reverse iterator to the last element
Definition: json.hpp:21579
basic_json get() const
get special-case overload
Definition: json.hpp:19717
ValueType value(const typename object_t::key_type &key, const ValueType &default_value) const
access specified object element with default value
Definition: json.hpp:20677
constexpr bool is_number_float() const noexcept
return whether value is a floating-point number
Definition: json.hpp:19410
NumberIntegerType number_integer_t
a type for a number (integer)
Definition: json.hpp:17490
friend bool operator==(const_reference lhs, const_reference rhs) noexcept
comparison: equal
Definition: json.hpp:23016
static bool sax_parse(InputType &&i, SAX *sax, input_format_t format=input_format_t::json, const bool strict=true, const bool ignore_comments=false)
generate SAX events
Definition: json.hpp:23697
detail::exception exception
general exception of the basic_json class
Definition: json.hpp:17041
ReferenceType get_ref()
get a reference value (implicit)
Definition: json.hpp:20042
static basic_json parse(InputType &&i, const parser_callback_t cb=nullptr, const bool allow_exceptions=true, const bool ignore_comments=false)
deserialize from a compatible input
Definition: json.hpp:23542
reference emplace_back(Args &&... args)
add an object to an array
Definition: json.hpp:22302
static basic_json from_cbor(const T *ptr, std::size_t len, const bool strict=true, const bool allow_exceptions=true, const cbor_tag_handler_t tag_handler=cbor_tag_handler_t::error)
Definition: json.hpp:24403
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:18204
basic_json & operator=(basic_json other) noexcept(std::is_nothrow_move_constructible< value_t >::value &&std::is_nothrow_move_assignable< value_t >::value &&std::is_nothrow_move_constructible< json_value >::value &&std::is_nothrow_move_assignable< json_value >::value)
copy assignment
Definition: json.hpp:19042
size_type max_size() const noexcept
returns the maximum possible number of elements
Definition: json.hpp:21963
basic_json(CompatibleType &&val) noexcept(noexcept(JSONSerializer< U >::to_json(std::declval< basic_json_t & >(), std::forward< CompatibleType >(val))))
create a JSON value
Definition: json.hpp:18301
static basic_json diff(const basic_json &source, const basic_json &target, const std::string &path="")
creates a diff as a JSON patch
Definition: json.hpp:25369
detail::input_format_t input_format_t
Definition: json.hpp:17028
value_type & reference
the type of an element reference
Definition: json.hpp:17069
const_reverse_iterator crend() const noexcept
returns a const reverse iterator to one before the first
Definition: json.hpp:21608
detail::out_of_range out_of_range
exception indicating access out of the defined range
Definition: json.hpp:17049
static iteration_proxy< iterator > iterator_wrapper(reference ref) noexcept
wrapper to access iterator member functions in range-based for
Definition: json.hpp:21672
iterator begin() noexcept
returns an iterator to the first element
Definition: json.hpp:21364
basic_json(InputIT first, InputIT last)
construct a JSON container given an iterator range
Definition: json.hpp:18784
static std::vector< uint8_t > to_ubjson(const basic_json &j, const bool use_size=false, const bool use_type=false)
create a UBJSON serialization of a given JSON value
Definition: json.hpp:24160
basic_json(const JsonRef &ref)
Definition: json.hpp:18887
static basic_json array(initializer_list_t init={})
explicitly create an array from an initializer list
Definition: json.hpp:18647
const_iterator cend() const noexcept
returns a const iterator to one past the last element
Definition: json.hpp:21475
reference back()
access the last element
Definition: json.hpp:20847
static bool accept(InputType &&i, const bool ignore_comments=false)
check if the input is valid JSON
Definition: json.hpp:23634
StringType string_t
a type for a string
Definition: json.hpp:17392
size_type size() const noexcept
returns the number of elements
Definition: json.hpp:21892
friend std::ostream & operator>>(const basic_json &j, std::ostream &o)
serialize to stream
Definition: json.hpp:23474
static basic_json meta()
returns version information on the library
Definition: json.hpp:17133
void update(const_reference j)
updates a JSON object from another object, overwriting existing keys
Definition: json.hpp:22660
std::size_t size_type
a type to represent container sizes
Definition: json.hpp:17076
std::ptrdiff_t difference_type
a type to represent differences between iterators
Definition: json.hpp:17074
static basic_json binary(const typename binary_t::container_type &init)
explicitly create a binary array (without subtype)
Definition: json.hpp:18544
reference operator+=(basic_json &&val)
add an object to an array
Definition: json.hpp:22137
basic_json(const BasicJsonType &val)
create a JSON value from an existing one
Definition: json.hpp:18339
typename std::allocator_traits< allocator_type >::const_pointer const_pointer
the type of an element const pointer
Definition: json.hpp:17084
typename std::allocator_traits< allocator_type >::pointer pointer
the type of an element pointer
Definition: json.hpp:17082
static basic_json from_cbor(InputType &&i, const bool strict=true, const bool allow_exceptions=true, const cbor_tag_handler_t tag_handler=cbor_tag_handler_t::error)
create a JSON value from an input in CBOR format
Definition: json.hpp:24371
BooleanType boolean_t
a type for a boolean
Definition: json.hpp:17418
void push_back(initializer_list_t init)
add an object to an object
Definition: json.hpp:22254
const char * type_name() const noexcept
return the type as string
Definition: json.hpp:23818
string_t dump(const int indent=-1, const char indent_char=' ', const bool ensure_ascii=false, const error_handler_t error_handler=error_handler_t::strict) const
serialization
Definition: json.hpp:19140
IteratorType erase(IteratorType pos)
remove element given an iterator
Definition: json.hpp:20914
static basic_json from_bson(InputType &&i, const bool strict=true, const bool allow_exceptions=true)
Create a JSON value from an input in BSON format.
Definition: json.hpp:24743
constexpr bool is_structured() const noexcept
return whether type is structured
Definition: json.hpp:19251
reference at(size_type idx)
access specified array element with bounds checking
Definition: json.hpp:20174
reference front()
access the first element
Definition: json.hpp:20803
constexpr bool is_primitive() const noexcept
return whether type is primitive
Definition: json.hpp:19224
constexpr bool is_number_unsigned() const noexcept
return whether value is an unsigned integer number
Definition: json.hpp:19382
detail::cbor_tag_handler_t cbor_tag_handler_t
how to treat CBOR tags
Definition: json.hpp:17024
detail::parse_error parse_error
exception indicating a parse error
Definition: json.hpp:17043
constexpr bool is_object() const noexcept
return whether value is an object
Definition: json.hpp:19432
constexpr value_t type() const noexcept
return the type of the JSON value (explicit)
Definition: json.hpp:19193
NumberFloatType number_float_t
a type for a number (floating-point)
Definition: json.hpp:17629
json_reverse_iterator< typename basic_json::iterator > reverse_iterator
a reverse iterator for a basic_json container
Definition: json.hpp:17091
friend bool operator<=(const_reference lhs, const_reference rhs) noexcept
comparison: less than or equal
Definition: json.hpp:23291
bool empty() const noexcept
checks whether the container is empty.
Definition: json.hpp:21819
friend std::ostream & operator<<(std::ostream &o, const basic_json &j)
serialize to stream
Definition: json.hpp:23450
basic_json(const basic_json &other)
copy constructor
Definition: json.hpp:18914
~basic_json() noexcept
destructor
Definition: json.hpp:19076
friend struct detail::external_constructor
Definition: json.hpp:16958
BasicJsonType get() const
get special-case overload
Definition: json.hpp:19740
basic_json(basic_json &&other) noexcept
move constructor
Definition: json.hpp:19004
detail::invalid_iterator invalid_iterator
exception indicating errors with iterators
Definition: json.hpp:17045
friend bool operator!=(const_reference lhs, const_reference rhs) noexcept
comparison: not equal
Definition: json.hpp:23124
detail::other_error other_error
exception indicating other library errors
Definition: json.hpp:17051
json_value m_value
the value of the current element
Definition: json.hpp:23853
friend bool operator>=(const_reference lhs, const_reference rhs) noexcept
comparison: greater than or equal
Definition: json.hpp:23383
reverse_iterator rend() noexcept
returns an iterator to the reverse-end
Definition: json.hpp:21542
ReferenceType get_ref() const
get a reference value (implicit)
Definition: json.hpp:20055
auto get() noexcept -> decltype(std::declval< basic_json_t & >().template get_ptr< PointerType >())
get a pointer value (explicit)
Definition: json.hpp:19996
void merge_patch(const basic_json &apply_patch)
applies a JSON Merge Patch
Definition: json.hpp:25544
auto get_ptr() noexcept -> decltype(std::declval< basic_json_t & >().get_impl_ptr(std::declval< PointerType >()))
get a pointer value (implicit)
Definition: json.hpp:19948
ArrayType< basic_json, AllocatorType< basic_json > > array_t
a type for an array
Definition: json.hpp:17339
Array get_to(T(&v)[N]) const noexcept(noexcept(JSONSerializer< Array >::from_json(std::declval< const basic_json_t & >(), v)))
Definition: json.hpp:19911
friend bool operator>(const_reference lhs, const_reference rhs) noexcept
comparison: greater than
Definition: json.hpp:23337
ValueType get() const noexcept(noexcept(JSONSerializer< ValueType >::from_json(std::declval< const basic_json_t & >(), std::declval< ValueType & >())))
get a value (explicit)
Definition: json.hpp:19790
constexpr bool is_boolean() const noexcept
return whether value is a boolean
Definition: json.hpp:19295
iteration_proxy< iterator > items() noexcept
helper to access iterator member functions in range-based for
Definition: json.hpp:21754
iterator end() noexcept
returns an iterator to one past the last element
Definition: json.hpp:21435
void swap(reference other) noexcept(std::is_nothrow_move_constructible< value_t >::value &&std::is_nothrow_move_assignable< value_t >::value &&std::is_nothrow_move_constructible< json_value >::value &&std::is_nothrow_move_assignable< json_value >::value)
exchanges the values
Definition: json.hpp:22762
void clear() noexcept
clears the contents
Definition: json.hpp:22034
constexpr bool is_binary() const noexcept
return whether value is a binary array
Definition: json.hpp:19498
static std::vector< uint8_t > to_msgpack(const basic_json &j)
create a MessagePack serialization of a given JSON value
Definition: json.hpp:24057
static basic_json object(initializer_list_t init={})
explicitly create an object from an initializer list
Definition: json.hpp:18691
reference operator[](size_type idx)
access specified array element
Definition: json.hpp:20379
ValueType & get_to(ValueType &v) const noexcept(noexcept(JSONSerializer< ValueType >::from_json(std::declval< const basic_json_t & >(), v)))
get a value (explicit)
Definition: json.hpp:19887
iter_impl< basic_json > iterator
an iterator for a basic_json container
Definition: json.hpp:17087
static std::vector< uint8_t > to_bson(const basic_json &j)
Serializes the given JSON object j to BSON and returns a vector containing the corresponding BSON-rep...
Definition: json.hpp:24238
json_reverse_iterator< typename basic_json::const_iterator > const_reverse_iterator
a const reverse iterator for a basic_json container
Definition: json.hpp:17093
static basic_json from_ubjson(InputType &&i, const bool strict=true, const bool allow_exceptions=true)
create a JSON value from an input in UBJSON format
Definition: json.hpp:24630
::nlohmann::json_pointer< basic_json > json_pointer
JSON Pointer, see nlohmann::json_pointer.
Definition: json.hpp:17018
binary_t & get_binary()
Definition: json.hpp:20116
friend bool operator<(const_reference lhs, const_reference rhs) noexcept
comparison: less than
Definition: json.hpp:23177
static basic_json binary(typename binary_t::container_type &&init)
explicitly create a binary array (without subtype)
Definition: json.hpp:18591
constexpr bool is_string() const noexcept
return whether value is a string
Definition: json.hpp:19476
constexpr bool is_array() const noexcept
return whether value is an array
Definition: json.hpp:19454
iterator insert_iterator(const_iterator pos, Args &&... args)
Definition: json.hpp:22387
basic_json flatten() const
return flattened JSON value
Definition: json.hpp:24975
const value_type & const_reference
the type of an element const reference
Definition: json.hpp:17071
void push_back(basic_json &&val)
add an object to an array
Definition: json.hpp:22111
size_type count(KeyT &&key) const
returns the number of occurrences of a key in a JSON object
Definition: json.hpp:21261
const binary_t & get_binary() const
Definition: json.hpp:20127
constexpr bool is_number() const noexcept
return whether value is a number
Definition: json.hpp:19325
std::less< StringType > object_comparator_t
Definition: json.hpp:17203
std::pair< iterator, bool > emplace(Args &&... args)
add an object to an object if key does not exist
Definition: json.hpp:22355
constexpr bool is_number_integer() const noexcept
return whether value is an integer number
Definition: json.hpp:19354
std::initializer_list< detail::json_ref< basic_json > > initializer_list_t
helper type for initializer lists of basic_json values
Definition: json.hpp:17026
detail::value_t value_t
Definition: json.hpp:17016
ValueType & get_to(ValueType &v) const
Definition: json.hpp:19900
static basic_json binary(const typename binary_t::container_type &init, std::uint8_t subtype)
explicitly create a binary array (with subtype)
Definition: json.hpp:18581
iterator find(KeyT &&key)
find an element in a JSON object
Definition: json.hpp:21210
detail::type_error type_error
exception indicating executing a member function with a wrong type
Definition: json.hpp:17047
basic_json(std::nullptr_t=nullptr) noexcept
create a null object
Definition: json.hpp:18228
AllocatorType< basic_json > allocator_type
the allocator type
Definition: json.hpp:17079
nlohmann::byte_container_with_subtype< BinaryType > binary_t
a type for a packed binary type
Definition: json.hpp:17700
JSONSerializer< T, SFINAE > json_serializer
Definition: json.hpp:17020
ValueType get() const noexcept(noexcept(JSONSerializer< ValueType >::from_json(std::declval< const basic_json_t & >())))
get a value (explicit); special case
Definition: json.hpp:19841
static std::vector< uint8_t > to_cbor(const basic_json &j)
create a CBOR serialization of a given JSON value
Definition: json.hpp:23962
static basic_json from_msgpack(InputType &&i, const bool strict=true, const bool allow_exceptions=true)
create a JSON value from an input in MessagePack format
Definition: json.hpp:24514
basic_json patch(const basic_json &json_patch) const
applies a JSON patch
Definition: json.hpp:25073
basic_json unflatten() const
unflatten a previously flattened JSON value
Definition: json.hpp:25012
NumberUnsignedType number_unsigned_t
a type for a number (unsigned)
Definition: json.hpp:17561
const_iterator cbegin() const noexcept
returns a const iterator to the first element
Definition: json.hpp:21404
basic_json(initializer_list_t init, bool type_deduction=true, value_t manual_type=value_t::array)
create a container (array or object) from an initializer list
Definition: json.hpp:18463
iterator insert(const_iterator pos, const basic_json &val)
inserts element
Definition: json.hpp:22425
iter_impl< const basic_json > const_iterator
a const iterator for a basic_json container
Definition: json.hpp:17089
constexpr bool is_discarded() const noexcept
return whether value is discarded
Definition: json.hpp:19525
constexpr bool is_null() const noexcept
return whether value is null
Definition: json.hpp:19273
ObjectType< StringType, basic_json, object_comparator_t, AllocatorType< std::pair< const StringType, basic_json > >> object_t
a type for an object
Definition: json.hpp:17293
basic_json(size_type cnt, const basic_json &val)
construct an array with count copies of given value
Definition: json.hpp:18718
static allocator_type get_allocator()
returns the allocator associated with the container
Definition: json.hpp:17101
constexpr auto get_ptr() const noexcept -> decltype(std::declval< const basic_json_t & >().get_impl_ptr(std::declval< PointerType >()))
get a pointer value (implicit)
Definition: json.hpp:19961
reverse_iterator rbegin() noexcept
returns an iterator to the reverse-beginning
Definition: json.hpp:21505
a class to store JSON values
Definition: json.hpp:16956
BinaryType container_type
the type of the underlying container
Definition: json.hpp:4729
void set_subtype(std::uint8_t subtype_) noexcept
sets the binary subtype
Definition: json.hpp:4784
byte_container_with_subtype(const container_type &b) noexcept(noexcept(container_type(b)))
Definition: json.hpp:4735
byte_container_with_subtype(container_type &&b) noexcept(noexcept(container_type(std::move(b))))
Definition: json.hpp:4739
bool operator!=(const byte_container_with_subtype &rhs) const
Definition: json.hpp:4761
void clear_subtype() noexcept
clears the binary subtype
Definition: json.hpp:4856
byte_container_with_subtype() noexcept(noexcept(container_type()))
Definition: json.hpp:4731
byte_container_with_subtype(const container_type &b, std::uint8_t subtype_) noexcept(noexcept(container_type(b)))
Definition: json.hpp:4743
constexpr bool has_subtype() const noexcept
return whether the value has a subtype
Definition: json.hpp:4832
byte_container_with_subtype(container_type &&b, std::uint8_t subtype_) noexcept(noexcept(container_type(std::move(b))))
Definition: json.hpp:4749
constexpr std::uint8_t subtype() const noexcept
return the binary subtype
Definition: json.hpp:4811
bool operator==(const byte_container_with_subtype &rhs) const
Definition: json.hpp:4755
an internal type for a backed binary type
Definition: json.hpp:4726
const std::string & back() const
return last reference token
Definition: json.hpp:12135
std::string to_string() const
return a string representation of the JSON pointer
Definition: json.hpp:11929
friend bool operator==(json_pointer const &lhs, json_pointer const &rhs) noexcept
compares two JSON pointers for equality
Definition: json.hpp:12772
void pop_back()
remove last reference token
Definition: json.hpp:12111
bool empty() const noexcept
return whether pointer points to the root document
Definition: json.hpp:12182
friend bool operator!=(json_pointer const &lhs, json_pointer const &rhs) noexcept
compares two JSON pointers for inequality
Definition: json.hpp:12789
void push_back(const std::string &token)
append an unescaped token at the end of the reference pointer
Definition: json.hpp:12157
json_pointer & operator/=(const json_pointer &ptr)
append another JSON pointer at the end of this JSON pointer
Definition: json.hpp:11961
json_pointer & operator/=(std::size_t array_idx)
append an array index at the end of this JSON pointer
Definition: json.hpp:12007
json_pointer(const std::string &s="")
create JSON pointer
Definition: json.hpp:11911
friend json_pointer operator/(const json_pointer &lhs, const json_pointer &rhs)
create a new JSON pointer by appending the right JSON pointer at the end of the left JSON pointer
Definition: json.hpp:12027
friend json_pointer operator/(const json_pointer &ptr, std::string token)
create a new JSON pointer by appending the unescaped token at the end of the JSON pointer
Definition: json.hpp:12048
json_pointer & operator/=(std::string token)
append an unescaped reference token at the end of this JSON pointer
Definition: json.hpp:11985
void push_back(std::string &&token)
append an unescaped token at the end of the reference pointer
Definition: json.hpp:12163
friend json_pointer operator/(const json_pointer &ptr, std::size_t array_idx)
create a new JSON pointer by appending the array-index-token at the end of the JSON pointer
Definition: json.hpp:12068
json_pointer parent_pointer() const
returns the parent of this JSON pointer
Definition: json.hpp:12086
JSON Pointer.
Definition: json.hpp:11884
constexpr const auto & to_json
Definition: json.hpp:4653
constexpr const auto & from_json
Definition: json.hpp:4085
basic_json<> json
default JSON class
Definition: json.hpp:3298
NLOHMANN_BASIC_JSON_TPL_DECLARATION std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j)
user-defined to_string function for JSON values
Definition: json.hpp:25583
namespace for Niels Lohmann
Definition: json.hpp:85
static auto to_json(BasicJsonType &j, ValueType &&val) noexcept(noexcept(::nlohmann::to_json(j, std::forward< ValueType >(val)))) -> decltype(::nlohmann::to_json(j, std::forward< ValueType >(val)), void())
convert any value type to a JSON value
Definition: json.hpp:4691
static auto from_json(BasicJsonType &&j, ValueType &val) noexcept(noexcept(::nlohmann::from_json(std::forward< BasicJsonType >(j), val))) -> decltype(::nlohmann::from_json(std::forward< BasicJsonType >(j), val), void())
convert a JSON value to any value type
Definition: json.hpp:4674
default JSONSerializer template argument
Definition: json.hpp:4663
virtual bool start_object(std::size_t elements)=0
the beginning of an object was read
virtual bool string(string_t &val)=0
a string was read
typename BasicJsonType::number_integer_t number_integer_t
Definition: json.hpp:5521
typename BasicJsonType::binary_t binary_t
Definition: json.hpp:5525
virtual bool end_array()=0
the end of an array was read
virtual bool key(string_t &val)=0
an object key was read
typename BasicJsonType::number_unsigned_t number_unsigned_t
Definition: json.hpp:5522
virtual bool binary(binary_t &val)=0
a binary string was read
typename BasicJsonType::number_float_t number_float_t
Definition: json.hpp:5523
virtual bool start_array(std::size_t elements)=0
the beginning of an array was read
virtual bool parse_error(std::size_t position, const std::string &last_token, const detail::exception &ex)=0
a parse error occurred
virtual bool boolean(bool val)=0
a boolean value was read
virtual bool end_object()=0
the end of an object was read
virtual bool number_unsigned(number_unsigned_t val)=0
an unsigned integer number was read
typename BasicJsonType::string_t string_t
Definition: json.hpp:5524
virtual bool number_float(number_float_t val, const string_t &s)=0
an floating-point number was read
virtual ~json_sax()=default
virtual bool number_integer(number_integer_t val)=0
an integer number was read
SAX interface.
Definition: json.hpp:5520
std::pair< iterator, bool > insert(const value_type &value)
Definition: json.hpp:16828
ordered_map(std::initializer_list< T > init, const Allocator &alloc=Allocator())
Definition: json.hpp:16702
std::vector< std::pair< const Key, T >, Allocator > Container
Definition: json.hpp:16690
iterator find(const Key &key)
Definition: json.hpp:16799
iterator erase(iterator pos)
Definition: json.hpp:16773
void insert(InputIt first, InputIt last)
Definition: json.hpp:16846
std::pair< iterator, bool > emplace(const key_type &key, T &&t)
Definition: json.hpp:16705
const_iterator find(const Key &key) const
Definition: json.hpp:16811
std::pair< iterator, bool > insert(value_type &&value)
Definition: json.hpp:16823
size_type erase(const Key &key)
Definition: json.hpp:16754
const T & operator[](const Key &key) const
Definition: json.hpp:16723
ordered_map(const Allocator &alloc=Allocator())
Definition: json.hpp:16698
typename std::enable_if< std::is_convertible< typename std::iterator_traits< InputIt >::iterator_category, std::input_iterator_tag >::value >::type require_input_iter
Definition: json.hpp:16843
const T & at(const Key &key) const
Definition: json.hpp:16741
ordered_map(It first, It last, const Allocator &alloc=Allocator())
Definition: json.hpp:16700
T & at(const Key &key)
Definition: json.hpp:16728
T & operator[](const Key &key)
Definition: json.hpp:16718
size_type count(const Key &key) const
Definition: json.hpp:16787