JSON for Modern C++  3.5.0
json.hpp
1 /*
2  __ _____ _____ _____
3  __| | __| | | | JSON for Modern C++
4 | | |__ | | | | | | version 3.5.0
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-2018 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 NLOHMANN_JSON_HPP
31 #define NLOHMANN_JSON_HPP
32 
33 #define NLOHMANN_JSON_VERSION_MAJOR 3
34 #define NLOHMANN_JSON_VERSION_MINOR 5
35 #define NLOHMANN_JSON_VERSION_PATCH 0
36 
37 #include <algorithm> // all_of, find, for_each
38 #include <cassert> // assert
39 #include <ciso646> // and, not, or
40 #include <cstddef> // nullptr_t, ptrdiff_t, size_t
41 #include <functional> // hash, less
42 #include <initializer_list> // initializer_list
43 #include <iosfwd> // istream, ostream
44 #include <iterator> // random_access_iterator_tag
45 #include <numeric> // accumulate
46 #include <string> // string, stoi, to_string
47 #include <utility> // declval, forward, move, pair, swap
48 
49 // #include <nlohmann/json_fwd.hpp>
50 #ifndef NLOHMANN_JSON_FWD_HPP
51 #define NLOHMANN_JSON_FWD_HPP
52 
53 #include <cstdint> // int64_t, uint64_t
54 #include <map> // map
55 #include <memory> // allocator
56 #include <string> // string
57 #include <vector> // vector
58 
59 /*!
60 @brief namespace for Niels Lohmann
61 @see https://github.com/nlohmann
62 @since version 1.0.0
63 */
64 namespace nlohmann
65 {
66 /*!
67 @brief default JSONSerializer template argument
68 
69 This serializer ignores the template arguments and uses ADL
70 ([argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl))
71 for serialization.
72 */
73 template<typename T = void, typename SFINAE = void>
74 struct adl_serializer;
75 
76 template<template<typename U, typename V, typename... Args> class ObjectType =
77  std::map,
78  template<typename U, typename... Args> class ArrayType = std::vector,
79  class StringType = std::string, class BooleanType = bool,
80  class NumberIntegerType = std::int64_t,
81  class NumberUnsignedType = std::uint64_t,
82  class NumberFloatType = double,
83  template<typename U> class AllocatorType = std::allocator,
84  template<typename T, typename SFINAE = void> class JSONSerializer =
86 class basic_json;
87 
88 /*!
89 @brief JSON Pointer
90 
91 A JSON pointer defines a string syntax for identifying a specific value
92 within a JSON document. It can be used with functions `at` and
93 `operator[]`. Furthermore, JSON pointers are the base for JSON patches.
94 
95 @sa [RFC 6901](https://tools.ietf.org/html/rfc6901)
96 
97 @since version 2.0.0
98 */
99 template<typename BasicJsonType>
100 class json_pointer;
101 
102 /*!
103 @brief default JSON class
104 
105 This type is the default specialization of the @ref basic_json class which
106 uses the standard template types.
107 
108 @since version 1.0.0
109 */
110 using json = basic_json<>;
111 } // namespace nlohmann
112 
113 #endif
114 
115 // #include <nlohmann/detail/macro_scope.hpp>
116 
117 
118 // This file contains all internal macro definitions
119 // You MUST include macro_unscope.hpp at the end of json.hpp to undef all of them
120 
121 // exclude unsupported compilers
122 #if !defined(JSON_SKIP_UNSUPPORTED_COMPILER_CHECK)
123  #if defined(__clang__)
124  #if (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) < 30400
125  #error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers"
126  #endif
127  #elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER))
128  #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800
129  #error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers"
130  #endif
131  #endif
132 #endif
133 
134 // disable float-equal warnings on GCC/clang
135 #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
136  #pragma GCC diagnostic push
137  #pragma GCC diagnostic ignored "-Wfloat-equal"
138 #endif
139 
140 // disable documentation warnings on clang
141 #if defined(__clang__)
142  #pragma GCC diagnostic push
143  #pragma GCC diagnostic ignored "-Wdocumentation"
144 #endif
145 
146 // allow for portable deprecation warnings
147 #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
148  #define JSON_DEPRECATED __attribute__((deprecated))
149 #elif defined(_MSC_VER)
150  #define JSON_DEPRECATED __declspec(deprecated)
151 #else
152  #define JSON_DEPRECATED
153 #endif
154 
155 // allow to disable exceptions
156 #if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION)
157  #define JSON_THROW(exception) throw exception
158  #define JSON_TRY try
159  #define JSON_CATCH(exception) catch(exception)
160  #define JSON_INTERNAL_CATCH(exception) catch(exception)
161 #else
162  #define JSON_THROW(exception) std::abort()
163  #define JSON_TRY if(true)
164  #define JSON_CATCH(exception) if(false)
165  #define JSON_INTERNAL_CATCH(exception) if(false)
166 #endif
167 
168 // override exception macros
169 #if defined(JSON_THROW_USER)
170  #undef JSON_THROW
171  #define JSON_THROW JSON_THROW_USER
172 #endif
173 #if defined(JSON_TRY_USER)
174  #undef JSON_TRY
175  #define JSON_TRY JSON_TRY_USER
176 #endif
177 #if defined(JSON_CATCH_USER)
178  #undef JSON_CATCH
179  #define JSON_CATCH JSON_CATCH_USER
180  #undef JSON_INTERNAL_CATCH
181  #define JSON_INTERNAL_CATCH JSON_CATCH_USER
182 #endif
183 #if defined(JSON_INTERNAL_CATCH_USER)
184  #undef JSON_INTERNAL_CATCH
185  #define JSON_INTERNAL_CATCH JSON_INTERNAL_CATCH_USER
186 #endif
187 
188 // manual branch prediction
189 #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
190  #define JSON_LIKELY(x) __builtin_expect(!!(x), 1)
191  #define JSON_UNLIKELY(x) __builtin_expect(!!(x), 0)
192 #else
193  #define JSON_LIKELY(x) x
194  #define JSON_UNLIKELY(x) x
195 #endif
196 
197 // C++ language standard detection
198 #if (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464
199  #define JSON_HAS_CPP_17
200  #define JSON_HAS_CPP_14
201 #elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1)
202  #define JSON_HAS_CPP_14
203 #endif
204 
205 /*!
206 @brief macro to briefly define a mapping between an enum and JSON
207 @def NLOHMANN_JSON_SERIALIZE_ENUM
208 @since version 3.4.0
209 */
210 #define NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...)
211  template<typename BasicJsonType>
212  inline void to_json(BasicJsonType& j, const ENUM_TYPE& e)
213  {
214  static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!");
215  static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__;
216  auto it = std::find_if(std::begin(m), std::end(m),
217  [e](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool
218  {
219  return ej_pair.first == e;
220  });
221  j = ((it != std::end(m)) ? it : std::begin(m))->second;
222  }
223  template<typename BasicJsonType>
224  inline void from_json(const BasicJsonType& j, ENUM_TYPE& e)
225  {
226  static_assert(std::is_enum<ENUM_TYPE>::value, #ENUM_TYPE " must be an enum!");
227  static const std::pair<ENUM_TYPE, BasicJsonType> m[] = __VA_ARGS__;
228  auto it = std::find_if(std::begin(m), std::end(m),
229  [j](const std::pair<ENUM_TYPE, BasicJsonType>& ej_pair) -> bool
230  {
231  return ej_pair.second == j;
232  });
233  e = ((it != std::end(m)) ? it : std::begin(m))->first;
234  }
235 
236 // Ugly macros to avoid uglier copy-paste when specializing basic_json. They
237 // may be removed in the future once the class is split.
238 
239 #define NLOHMANN_BASIC_JSON_TPL_DECLARATION
240  template<template<typename, typename, typename...> class ObjectType,
241  template<typename, typename...> class ArrayType,
242  class StringType, class BooleanType, class NumberIntegerType,
243  class NumberUnsignedType, class NumberFloatType,
244  template<typename> class AllocatorType,
245  template<typename, typename = void> class JSONSerializer>
246 
247 #define NLOHMANN_BASIC_JSON_TPL
248  basic_json<ObjectType, ArrayType, StringType, BooleanType,
249  NumberIntegerType, NumberUnsignedType, NumberFloatType,
250  AllocatorType, JSONSerializer>
251 
252 // #include <nlohmann/detail/meta/cpp_future.hpp>
253 
254 
255 #include <ciso646> // not
256 #include <cstddef> // size_t
257 #include <type_traits> // conditional, enable_if, false_type, integral_constant, is_constructible, is_integral, is_same, remove_cv, remove_reference, true_type
258 
259 namespace nlohmann
260 {
261 namespace detail
262 {
263 // alias templates to reduce boilerplate
264 template<bool B, typename T = void>
265 using enable_if_t = typename std::enable_if<B, T>::type;
266 
267 template<typename T>
268 using uncvref_t = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
269 
270 // implementation of C++14 index_sequence and affiliates
271 // source: https://stackoverflow.com/a/32223343
272 template<std::size_t... Ints>
273 struct index_sequence
274 {
275  using type = index_sequence;
276  using value_type = std::size_t;
277  static constexpr std::size_t size() noexcept
278  {
279  return sizeof...(Ints);
280  }
281 };
282 
283 template<class Sequence1, class Sequence2>
284 struct merge_and_renumber;
285 
286 template<std::size_t... I1, std::size_t... I2>
287 struct merge_and_renumber<index_sequence<I1...>, index_sequence<I2...>>
288  : index_sequence < I1..., (sizeof...(I1) + I2)... > {};
289 
290 template<std::size_t N>
291 struct make_index_sequence
292  : merge_and_renumber < typename make_index_sequence < N / 2 >::type,
293  typename make_index_sequence < N - N / 2 >::type > {};
294 
295 template<> struct make_index_sequence<0> : index_sequence<> {};
296 template<> struct make_index_sequence<1> : index_sequence<0> {};
297 
298 template<typename... Ts>
299 using index_sequence_for = make_index_sequence<sizeof...(Ts)>;
300 
301 // dispatch utility (taken from ranges-v3)
302 template<unsigned N> struct priority_tag : priority_tag < N - 1 > {};
303 template<> struct priority_tag<0> {};
304 
305 // taken from ranges-v3
306 template<typename T>
307 struct static_const
308 {
309  static constexpr T value{};
310 };
311 
312 template<typename T>
313 constexpr T static_const<T>::value;
314 } // namespace detail
315 } // namespace nlohmann
316 
317 // #include <nlohmann/detail/meta/type_traits.hpp>
318 
319 
320 #include <ciso646> // not
321 #include <limits> // numeric_limits
322 #include <type_traits> // false_type, is_constructible, is_integral, is_same, true_type
323 #include <utility> // declval
324 
325 // #include <nlohmann/json_fwd.hpp>
326 
327 // #include <nlohmann/detail/iterators/iterator_traits.hpp>
328 
329 
330 #include <iterator> // random_access_iterator_tag
331 
332 // #include <nlohmann/detail/meta/void_t.hpp>
333 
334 
335 namespace nlohmann
336 {
337 namespace detail
338 {
339 template <typename ...Ts> struct make_void
340 {
341  using type = void;
342 };
343 template <typename ...Ts> using void_t = typename make_void<Ts...>::type;
344 } // namespace detail
345 } // namespace nlohmann
346 
347 // #include <nlohmann/detail/meta/cpp_future.hpp>
348 
349 
350 namespace nlohmann
351 {
352 namespace detail
353 {
354 template <typename It, typename = void>
355 struct iterator_types {};
356 
357 template <typename It>
358 struct iterator_types <
359  It,
360  void_t<typename It::difference_type, typename It::value_type, typename It::pointer,
361  typename It::reference, typename It::iterator_category >>
362 {
363  using difference_type = typename It::difference_type;
364  using value_type = typename It::value_type;
365  using pointer = typename It::pointer;
366  using reference = typename It::reference;
367  using iterator_category = typename It::iterator_category;
368 };
369 
370 // This is required as some compilers implement std::iterator_traits in a way that
371 // doesn't work with SFINAE. See https://github.com/nlohmann/json/issues/1341.
372 template <typename T, typename = void>
373 struct iterator_traits
374 {
375 };
376 
377 template <typename T>
378 struct iterator_traits < T, enable_if_t < !std::is_pointer<T>::value >>
379  : iterator_types<T>
380 {
381 };
382 
383 template <typename T>
384 struct iterator_traits<T*, enable_if_t<std::is_object<T>::value>>
385 {
386  using iterator_category = std::random_access_iterator_tag;
387  using value_type = T;
388  using difference_type = ptrdiff_t;
389  using pointer = T*;
390  using reference = T&;
391 };
392 }
393 }
394 
395 // #include <nlohmann/detail/meta/cpp_future.hpp>
396 
397 // #include <nlohmann/detail/meta/detected.hpp>
398 
399 
400 #include <type_traits>
401 
402 // #include <nlohmann/detail/meta/void_t.hpp>
403 
404 
405 // http://en.cppreference.com/w/cpp/experimental/is_detected
406 namespace nlohmann
407 {
408 namespace detail
409 {
410 struct nonesuch
411 {
412  nonesuch() = delete;
413  ~nonesuch() = delete;
414  nonesuch(nonesuch const&) = delete;
415  void operator=(nonesuch const&) = delete;
416 };
417 
418 template <class Default,
419  class AlwaysVoid,
420  template <class...> class Op,
421  class... Args>
422 struct detector
423 {
424  using value_t = std::false_type;
425  using type = Default;
426 };
427 
428 template <class Default, template <class...> class Op, class... Args>
429 struct detector<Default, void_t<Op<Args...>>, Op, Args...>
430 {
431  using value_t = std::true_type;
432  using type = Op<Args...>;
433 };
434 
435 template <template <class...> class Op, class... Args>
436 using is_detected = typename detector<nonesuch, void, Op, Args...>::value_t;
437 
438 template <template <class...> class Op, class... Args>
439 using detected_t = typename detector<nonesuch, void, Op, Args...>::type;
440 
441 template <class Default, template <class...> class Op, class... Args>
442 using detected_or = detector<Default, void, Op, Args...>;
443 
444 template <class Default, template <class...> class Op, class... Args>
445 using detected_or_t = typename detected_or<Default, Op, Args...>::type;
446 
447 template <class Expected, template <class...> class Op, class... Args>
448 using is_detected_exact = std::is_same<Expected, detected_t<Op, Args...>>;
449 
450 template <class To, template <class...> class Op, class... Args>
451 using is_detected_convertible =
452  std::is_convertible<detected_t<Op, Args...>, To>;
453 } // namespace detail
454 } // namespace nlohmann
455 
456 // #include <nlohmann/detail/macro_scope.hpp>
457 
458 
459 namespace nlohmann
460 {
461 /*!
462 @brief detail namespace with internal helper functions
463 
464 This namespace collects functions that should not be exposed,
465 implementations of some @ref basic_json methods, and meta-programming helpers.
466 
467 @since version 2.1.0
468 */
469 namespace detail
470 {
471 /////////////
472 // helpers //
473 /////////////
474 
475 // Note to maintainers:
476 //
477 // Every trait in this file expects a non CV-qualified type.
478 // The only exceptions are in the 'aliases for detected' section
479 // (i.e. those of the form: decltype(T::member_function(std::declval<T>())))
480 //
481 // In this case, T has to be properly CV-qualified to constraint the function arguments
482 // (e.g. to_json(BasicJsonType&, const T&))
483 
484 template<typename> struct is_basic_json : std::false_type {};
485 
487 struct is_basic_json<NLOHMANN_BASIC_JSON_TPL> : std::true_type {};
488 
489 //////////////////////////
490 // aliases for detected //
491 //////////////////////////
492 
493 template <typename T>
494 using mapped_type_t = typename T::mapped_type;
495 
496 template <typename T>
497 using key_type_t = typename T::key_type;
498 
499 template <typename T>
500 using value_type_t = typename T::value_type;
501 
502 template <typename T>
503 using difference_type_t = typename T::difference_type;
504 
505 template <typename T>
506 using pointer_t = typename T::pointer;
507 
508 template <typename T>
509 using reference_t = typename T::reference;
510 
511 template <typename T>
512 using iterator_category_t = typename T::iterator_category;
513 
514 template <typename T>
515 using iterator_t = typename T::iterator;
516 
517 template <typename T, typename... Args>
518 using to_json_function = decltype(T::to_json(std::declval<Args>()...));
519 
520 template <typename T, typename... Args>
521 using from_json_function = decltype(T::from_json(std::declval<Args>()...));
522 
523 template <typename T, typename U>
524 using get_template_function = decltype(std::declval<T>().template get<U>());
525 
526 // trait checking if JSONSerializer<T>::from_json(json const&, udt&) exists
527 template <typename BasicJsonType, typename T, typename = void>
528 struct has_from_json : std::false_type {};
529 
530 template <typename BasicJsonType, typename T>
531 struct has_from_json<BasicJsonType, T,
532  enable_if_t<not is_basic_json<T>::value>>
533 {
534  using serializer = typename BasicJsonType::template json_serializer<T, void>;
535 
536  static constexpr bool value =
537  is_detected_exact<void, from_json_function, serializer,
538  const BasicJsonType&, T&>::value;
539 };
540 
541 // This trait checks if JSONSerializer<T>::from_json(json const&) exists
542 // this overload is used for non-default-constructible user-defined-types
543 template <typename BasicJsonType, typename T, typename = void>
544 struct has_non_default_from_json : std::false_type {};
545 
546 template<typename BasicJsonType, typename T>
547 struct has_non_default_from_json<BasicJsonType, T, enable_if_t<not is_basic_json<T>::value>>
548 {
549  using serializer = typename BasicJsonType::template json_serializer<T, void>;
550 
551  static constexpr bool value =
552  is_detected_exact<T, from_json_function, serializer,
553  const BasicJsonType&>::value;
554 };
555 
556 // This trait checks if BasicJsonType::json_serializer<T>::to_json exists
557 // Do not evaluate the trait when T is a basic_json type, to avoid template instantiation infinite recursion.
558 template <typename BasicJsonType, typename T, typename = void>
559 struct has_to_json : std::false_type {};
560 
561 template <typename BasicJsonType, typename T>
562 struct has_to_json<BasicJsonType, T, enable_if_t<not is_basic_json<T>::value>>
563 {
564  using serializer = typename BasicJsonType::template json_serializer<T, void>;
565 
566  static constexpr bool value =
567  is_detected_exact<void, to_json_function, serializer, BasicJsonType&,
568  T>::value;
569 };
570 
571 
572 ///////////////////
573 // is_ functions //
574 ///////////////////
575 
576 template <typename T, typename = void>
577 struct is_iterator_traits : std::false_type {};
578 
579 template <typename T>
580 struct is_iterator_traits<iterator_traits<T>>
581 {
582  private:
583  using traits = iterator_traits<T>;
584 
585  public:
586  static constexpr auto value =
587  is_detected<value_type_t, traits>::value &&
588  is_detected<difference_type_t, traits>::value &&
589  is_detected<pointer_t, traits>::value &&
590  is_detected<iterator_category_t, traits>::value &&
591  is_detected<reference_t, traits>::value;
592 };
593 
594 // source: https://stackoverflow.com/a/37193089/4116453
595 
596 template <typename T, typename = void>
597 struct is_complete_type : std::false_type {};
598 
599 template <typename T>
600 struct is_complete_type<T, decltype(void(sizeof(T)))> : std::true_type {};
601 
602 template <typename BasicJsonType, typename CompatibleObjectType,
603  typename = void>
604 struct is_compatible_object_type_impl : std::false_type {};
605 
606 template <typename BasicJsonType, typename CompatibleObjectType>
607 struct is_compatible_object_type_impl <
608  BasicJsonType, CompatibleObjectType,
609  enable_if_t<is_detected<mapped_type_t, CompatibleObjectType>::value and
610  is_detected<key_type_t, CompatibleObjectType>::value >>
611 {
612 
613  using object_t = typename BasicJsonType::object_t;
614 
615  // macOS's is_constructible does not play well with nonesuch...
616  static constexpr bool value =
617  std::is_constructible<typename object_t::key_type,
618  typename CompatibleObjectType::key_type>::value and
619  std::is_constructible<typename object_t::mapped_type,
620  typename CompatibleObjectType::mapped_type>::value;
621 };
622 
623 template <typename BasicJsonType, typename CompatibleObjectType>
624 struct is_compatible_object_type
625  : is_compatible_object_type_impl<BasicJsonType, CompatibleObjectType> {};
626 
627 template <typename BasicJsonType, typename ConstructibleObjectType,
628  typename = void>
629 struct is_constructible_object_type_impl : std::false_type {};
630 
631 template <typename BasicJsonType, typename ConstructibleObjectType>
632 struct is_constructible_object_type_impl <
633  BasicJsonType, ConstructibleObjectType,
634  enable_if_t<is_detected<mapped_type_t, ConstructibleObjectType>::value and
635  is_detected<key_type_t, ConstructibleObjectType>::value >>
636 {
637  using object_t = typename BasicJsonType::object_t;
638 
639  static constexpr bool value =
640  (std::is_constructible<typename ConstructibleObjectType::key_type, typename object_t::key_type>::value and
641  std::is_same<typename object_t::mapped_type, typename ConstructibleObjectType::mapped_type>::value) or
642  (has_from_json<BasicJsonType, typename ConstructibleObjectType::mapped_type>::value or
643  has_non_default_from_json<BasicJsonType, typename ConstructibleObjectType::mapped_type >::value);
644 };
645 
646 template <typename BasicJsonType, typename ConstructibleObjectType>
647 struct is_constructible_object_type
648  : is_constructible_object_type_impl<BasicJsonType,
649  ConstructibleObjectType> {};
650 
651 template <typename BasicJsonType, typename CompatibleStringType,
652  typename = void>
653 struct is_compatible_string_type_impl : std::false_type {};
654 
655 template <typename BasicJsonType, typename CompatibleStringType>
656 struct is_compatible_string_type_impl <
657  BasicJsonType, CompatibleStringType,
658  enable_if_t<is_detected_exact<typename BasicJsonType::string_t::value_type,
659  value_type_t, CompatibleStringType>::value >>
660 {
661  static constexpr auto value =
662  std::is_constructible<typename BasicJsonType::string_t, CompatibleStringType>::value;
663 };
664 
665 template <typename BasicJsonType, typename ConstructibleStringType>
666 struct is_compatible_string_type
667  : is_compatible_string_type_impl<BasicJsonType, ConstructibleStringType> {};
668 
669 template <typename BasicJsonType, typename ConstructibleStringType,
670  typename = void>
671 struct is_constructible_string_type_impl : std::false_type {};
672 
673 template <typename BasicJsonType, typename ConstructibleStringType>
674 struct is_constructible_string_type_impl <
675  BasicJsonType, ConstructibleStringType,
676  enable_if_t<is_detected_exact<typename BasicJsonType::string_t::value_type,
677  value_type_t, ConstructibleStringType>::value >>
678 {
679  static constexpr auto value =
680  std::is_constructible<ConstructibleStringType,
681  typename BasicJsonType::string_t>::value;
682 };
683 
684 template <typename BasicJsonType, typename ConstructibleStringType>
685 struct is_constructible_string_type
686  : is_constructible_string_type_impl<BasicJsonType, ConstructibleStringType> {};
687 
688 template <typename BasicJsonType, typename CompatibleArrayType, typename = void>
689 struct is_compatible_array_type_impl : std::false_type {};
690 
691 template <typename BasicJsonType, typename CompatibleArrayType>
692 struct is_compatible_array_type_impl <
693  BasicJsonType, CompatibleArrayType,
694  enable_if_t<is_detected<value_type_t, CompatibleArrayType>::value and
695  is_detected<iterator_t, CompatibleArrayType>::value and
696 // This is needed because json_reverse_iterator has a ::iterator type...
697 // Therefore it is detected as a CompatibleArrayType.
698 // The real fix would be to have an Iterable concept.
699  not is_iterator_traits<
700  iterator_traits<CompatibleArrayType>>::value >>
701 {
702  static constexpr bool value =
703  std::is_constructible<BasicJsonType,
704  typename CompatibleArrayType::value_type>::value;
705 };
706 
707 template <typename BasicJsonType, typename CompatibleArrayType>
708 struct is_compatible_array_type
709  : is_compatible_array_type_impl<BasicJsonType, CompatibleArrayType> {};
710 
711 template <typename BasicJsonType, typename ConstructibleArrayType, typename = void>
712 struct is_constructible_array_type_impl : std::false_type {};
713 
714 template <typename BasicJsonType, typename ConstructibleArrayType>
715 struct is_constructible_array_type_impl <
716  BasicJsonType, ConstructibleArrayType,
717  enable_if_t<std::is_same<ConstructibleArrayType,
718  typename BasicJsonType::value_type>::value >>
719  : std::true_type {};
720 
721 template <typename BasicJsonType, typename ConstructibleArrayType>
722 struct is_constructible_array_type_impl <
723  BasicJsonType, ConstructibleArrayType,
724  enable_if_t<not std::is_same<ConstructibleArrayType,
725  typename BasicJsonType::value_type>::value and
726  is_detected<value_type_t, ConstructibleArrayType>::value and
727  is_detected<iterator_t, ConstructibleArrayType>::value and
728  is_complete_type<
729  detected_t<value_type_t, ConstructibleArrayType>>::value >>
730 {
731  static constexpr bool value =
732  // This is needed because json_reverse_iterator has a ::iterator type,
733  // furthermore, std::back_insert_iterator (and other iterators) have a base class `iterator`...
734  // Therefore it is detected as a ConstructibleArrayType.
735  // The real fix would be to have an Iterable concept.
736  not is_iterator_traits <
737  iterator_traits<ConstructibleArrayType >>::value and
738 
739  (std::is_same<typename ConstructibleArrayType::value_type, typename BasicJsonType::array_t::value_type>::value or
740  has_from_json<BasicJsonType,
741  typename ConstructibleArrayType::value_type>::value or
742  has_non_default_from_json <
743  BasicJsonType, typename ConstructibleArrayType::value_type >::value);
744 };
745 
746 template <typename BasicJsonType, typename ConstructibleArrayType>
747 struct is_constructible_array_type
748  : is_constructible_array_type_impl<BasicJsonType, ConstructibleArrayType> {};
749 
750 template <typename RealIntegerType, typename CompatibleNumberIntegerType,
751  typename = void>
752 struct is_compatible_integer_type_impl : std::false_type {};
753 
754 template <typename RealIntegerType, typename CompatibleNumberIntegerType>
755 struct is_compatible_integer_type_impl <
756  RealIntegerType, CompatibleNumberIntegerType,
757  enable_if_t<std::is_integral<RealIntegerType>::value and
758  std::is_integral<CompatibleNumberIntegerType>::value and
759  not std::is_same<bool, CompatibleNumberIntegerType>::value >>
760 {
761  // is there an assert somewhere on overflows?
762  using RealLimits = std::numeric_limits<RealIntegerType>;
763  using CompatibleLimits = std::numeric_limits<CompatibleNumberIntegerType>;
764 
765  static constexpr auto value =
766  std::is_constructible<RealIntegerType,
767  CompatibleNumberIntegerType>::value and
768  CompatibleLimits::is_integer and
769  RealLimits::is_signed == CompatibleLimits::is_signed;
770 };
771 
772 template <typename RealIntegerType, typename CompatibleNumberIntegerType>
773 struct is_compatible_integer_type
774  : is_compatible_integer_type_impl<RealIntegerType,
775  CompatibleNumberIntegerType> {};
776 
777 template <typename BasicJsonType, typename CompatibleType, typename = void>
778 struct is_compatible_type_impl: std::false_type {};
779 
780 template <typename BasicJsonType, typename CompatibleType>
781 struct is_compatible_type_impl <
782  BasicJsonType, CompatibleType,
783  enable_if_t<is_complete_type<CompatibleType>::value >>
784 {
785  static constexpr bool value =
786  has_to_json<BasicJsonType, CompatibleType>::value;
787 };
788 
789 template <typename BasicJsonType, typename CompatibleType>
790 struct is_compatible_type
791  : is_compatible_type_impl<BasicJsonType, CompatibleType> {};
792 } // namespace detail
793 } // namespace nlohmann
794 
795 // #include <nlohmann/detail/exceptions.hpp>
796 
797 
798 #include <exception> // exception
799 #include <stdexcept> // runtime_error
800 #include <string> // to_string
801 
802 // #include <nlohmann/detail/input/position_t.hpp>
803 
804 
805 #include <cstddef> // size_t
806 
807 namespace nlohmann
808 {
809 namespace detail
810 {
811 /// struct to capture the start position of the current token
812 struct position_t
813 {
814  /// the total number of characters read
815  std::size_t chars_read_total = 0;
816  /// the number of characters read in the current line
817  std::size_t chars_read_current_line = 0;
818  /// the number of lines read
819  std::size_t lines_read = 0;
820 
821  /// conversion to size_t to preserve SAX interface
822  constexpr operator size_t() const
823  {
824  return chars_read_total;
825  }
826 };
827 
828 }
829 }
830 
831 
832 namespace nlohmann
833 {
834 namespace detail
835 {
836 ////////////////
837 // exceptions //
838 ////////////////
839 
840 /*!
841 @brief general exception of the @ref basic_json class
842 
843 This class is an extension of `std::exception` objects with a member @a id for
844 exception ids. It is used as the base class for all exceptions thrown by the
845 @ref basic_json class. This class can hence be used as "wildcard" to catch
846 exceptions.
847 
848 Subclasses:
849 - @ref parse_error for exceptions indicating a parse error
850 - @ref invalid_iterator for exceptions indicating errors with iterators
851 - @ref type_error for exceptions indicating executing a member function with
852  a wrong type
853 - @ref out_of_range for exceptions indicating access out of the defined range
854 - @ref other_error for exceptions indicating other library errors
855 
856 @internal
857 @note To have nothrow-copy-constructible exceptions, we internally use
858  `std::runtime_error` which can cope with arbitrary-length error messages.
859  Intermediate strings are built with static functions and then passed to
860  the actual constructor.
861 @endinternal
862 
863 @liveexample{The following code shows how arbitrary library exceptions can be
864 caught.,exception}
865 
866 @since version 3.0.0
867 */
868 class exception : public std::exception
869 {
870  public:
871  /// returns the explanatory string
872  const char* what() const noexcept override
873  {
874  return m.what();
875  }
876 
877  /// the id of the exception
878  const int id;
879 
880  protected:
881  exception(int id_, const char* what_arg) : id(id_), m(what_arg) {}
882 
883  static std::string name(const std::string& ename, int id_)
884  {
885  return "[json.exception." + ename + "." + std::to_string(id_) + "] ";
886  }
887 
888  private:
889  /// an exception object as storage for error messages
890  std::runtime_error m;
891 };
892 
893 /*!
894 @brief exception indicating a parse error
895 
896 This exception is thrown by the library when a parse error occurs. Parse errors
897 can occur during the deserialization of JSON text, CBOR, MessagePack, as well
898 as when using JSON Patch.
899 
900 Member @a byte holds the byte index of the last read character in the input
901 file.
902 
903 Exceptions have ids 1xx.
904 
905 name / id | example message | description
906 ------------------------------ | --------------- | -------------------------
907 json.exception.parse_error.101 | parse error at 2: unexpected end of input; expected string literal | This error indicates a syntax error while deserializing a JSON text. The error message describes that an unexpected token (character) was encountered, and the member @a byte indicates the error position.
908 json.exception.parse_error.102 | parse error at 14: missing or wrong low surrogate | JSON uses the `\uxxxx` format to describe Unicode characters. Code points above above 0xFFFF are split into two `\uxxxx` entries ("surrogate pairs"). This error indicates that the surrogate pair is incomplete or contains an invalid code point.
909 json.exception.parse_error.103 | parse error: code points above 0x10FFFF are invalid | Unicode supports code points up to 0x10FFFF. Code points above 0x10FFFF are invalid.
910 json.exception.parse_error.104 | parse error: JSON patch must be an array of objects | [RFC 6902](https://tools.ietf.org/html/rfc6902) requires a JSON Patch document to be a JSON document that represents an array of objects.
911 json.exception.parse_error.105 | parse error: operation must have string member 'op' | An operation of a JSON Patch document must contain exactly one "op" member, whose value indicates the operation to perform. Its value must be one of "add", "remove", "replace", "move", "copy", or "test"; other values are errors.
912 json.exception.parse_error.106 | parse error: array index '01' must not begin with '0' | An array index in a JSON Pointer ([RFC 6901](https://tools.ietf.org/html/rfc6901)) may be `0` or any number without a leading `0`.
913 json.exception.parse_error.107 | parse error: JSON pointer must be empty or begin with '/' - was: 'foo' | A JSON Pointer must be a Unicode string containing a sequence of zero or more reference tokens, each prefixed by a `/` character.
914 json.exception.parse_error.108 | parse error: escape character '~' must be followed with '0' or '1' | In a JSON Pointer, only `~0` and `~1` are valid escape sequences.
915 json.exception.parse_error.109 | parse error: array index 'one' is not a number | A JSON Pointer array index must be a number.
916 json.exception.parse_error.110 | parse error at 1: cannot read 2 bytes from vector | When parsing CBOR or MessagePack, the byte vector ends before the complete value has been read.
917 json.exception.parse_error.112 | parse error at 1: error reading CBOR; last byte: 0xF8 | Not all types of CBOR or MessagePack are supported. This exception occurs if an unsupported byte was read.
918 json.exception.parse_error.113 | parse error at 2: expected a CBOR string; last byte: 0x98 | While parsing a map key, a value that is not a string has been read.
919 json.exception.parse_error.114 | parse error: Unsupported BSON record type 0x0F | The parsing of the corresponding BSON record type is not implemented (yet).
920 
921 @note For an input with n bytes, 1 is the index of the first character and n+1
922  is the index of the terminating null byte or the end of file. This also
923  holds true when reading a byte vector (CBOR or MessagePack).
924 
925 @liveexample{The following code shows how a `parse_error` exception can be
926 caught.,parse_error}
927 
928 @sa @ref exception for the base class of the library exceptions
929 @sa @ref invalid_iterator for exceptions indicating errors with iterators
930 @sa @ref type_error for exceptions indicating executing a member function with
931  a wrong type
932 @sa @ref out_of_range for exceptions indicating access out of the defined range
933 @sa @ref other_error for exceptions indicating other library errors
934 
935 @since version 3.0.0
936 */
937 class parse_error : public exception
938 {
939  public:
940  /*!
941  @brief create a parse error exception
942  @param[in] id_ the id of the exception
943  @param[in] position the position where the error occurred (or with
944  chars_read_total=0 if the position cannot be
945  determined)
946  @param[in] what_arg the explanatory string
947  @return parse_error object
948  */
949  static parse_error create(int id_, const position_t& pos, const std::string& what_arg)
950  {
951  std::string w = exception::name("parse_error", id_) + "parse error" +
952  position_string(pos) + ": " + what_arg;
953  return parse_error(id_, pos.chars_read_total, w.c_str());
954  }
955 
956  static parse_error create(int id_, std::size_t byte_, const std::string& what_arg)
957  {
958  std::string w = exception::name("parse_error", id_) + "parse error" +
959  (byte_ != 0 ? (" at byte " + std::to_string(byte_)) : "") +
960  ": " + what_arg;
961  return parse_error(id_, byte_, w.c_str());
962  }
963 
964  /*!
965  @brief byte index of the parse error
966 
967  The byte index of the last read character in the input file.
968 
969  @note For an input with n bytes, 1 is the index of the first character and
970  n+1 is the index of the terminating null byte or the end of file.
971  This also holds true when reading a byte vector (CBOR or MessagePack).
972  */
973  const std::size_t byte;
974 
975  private:
976  parse_error(int id_, std::size_t byte_, const char* what_arg)
977  : exception(id_, what_arg), byte(byte_) {}
978 
979  static std::string position_string(const position_t& pos)
980  {
981  return " at line " + std::to_string(pos.lines_read + 1) +
982  ", column " + std::to_string(pos.chars_read_current_line);
983  }
984 };
985 
986 /*!
987 @brief exception indicating errors with iterators
988 
989 This exception is thrown if iterators passed to a library function do not match
990 the expected semantics.
991 
992 Exceptions have ids 2xx.
993 
994 name / id | example message | description
995 ----------------------------------- | --------------- | -------------------------
996 json.exception.invalid_iterator.201 | iterators are not compatible | The iterators passed to constructor @ref basic_json(InputIT first, InputIT last) are not compatible, meaning they do not belong to the same container. Therefore, the range (@a first, @a last) is invalid.
997 json.exception.invalid_iterator.202 | iterator does not fit current value | In an erase or insert function, the passed iterator @a pos does not belong to the JSON value for which the function was called. It hence does not define a valid position for the deletion/insertion.
998 json.exception.invalid_iterator.203 | iterators do not fit current value | Either iterator passed to function @ref erase(IteratorType first, IteratorType last) does not belong to the JSON value from which values shall be erased. It hence does not define a valid range to delete values from.
999 json.exception.invalid_iterator.204 | iterators out of range | When an iterator range for a primitive type (number, boolean, or string) is passed to a constructor or an erase function, this range has to be exactly (@ref begin(), @ref end()), because this is the only way the single stored value is expressed. All other ranges are invalid.
1000 json.exception.invalid_iterator.205 | iterator out of range | When an iterator for a primitive type (number, boolean, or string) is passed to an erase function, the iterator has to be the @ref begin() iterator, because it is the only way to address the stored value. All other iterators are invalid.
1001 json.exception.invalid_iterator.206 | cannot construct with iterators from null | The iterators passed to constructor @ref basic_json(InputIT first, InputIT last) belong to a JSON null value and hence to not define a valid range.
1002 json.exception.invalid_iterator.207 | cannot use key() for non-object iterators | The key() member function can only be used on iterators belonging to a JSON object, because other types do not have a concept of a key.
1003 json.exception.invalid_iterator.208 | cannot use operator[] for object iterators | The operator[] to specify a concrete offset cannot be used on iterators belonging to a JSON object, because JSON objects are unordered.
1004 json.exception.invalid_iterator.209 | cannot use offsets with object iterators | The offset operators (+, -, +=, -=) cannot be used on iterators belonging to a JSON object, because JSON objects are unordered.
1005 json.exception.invalid_iterator.210 | iterators do not fit | The iterator range passed to the insert function are not compatible, meaning they do not belong to the same container. Therefore, the range (@a first, @a last) is invalid.
1006 json.exception.invalid_iterator.211 | passed iterators may not belong to container | The iterator range passed to the insert function must not be a subrange of the container to insert to.
1007 json.exception.invalid_iterator.212 | cannot compare iterators of different containers | When two iterators are compared, they must belong to the same container.
1008 json.exception.invalid_iterator.213 | cannot compare order of object iterators | The order of object iterators cannot be compared, because JSON objects are unordered.
1009 json.exception.invalid_iterator.214 | cannot get value | Cannot get value for iterator: Either the iterator belongs to a null value or it is an iterator to a primitive type (number, boolean, or string), but the iterator is different to @ref begin().
1010 
1011 @liveexample{The following code shows how an `invalid_iterator` exception can be
1012 caught.,invalid_iterator}
1013 
1014 @sa @ref exception for the base class of the library exceptions
1015 @sa @ref parse_error for exceptions indicating a parse error
1016 @sa @ref type_error for exceptions indicating executing a member function with
1017  a wrong type
1018 @sa @ref out_of_range for exceptions indicating access out of the defined range
1019 @sa @ref other_error for exceptions indicating other library errors
1020 
1021 @since version 3.0.0
1022 */
1023 class invalid_iterator : public exception
1024 {
1025  public:
1026  static invalid_iterator create(int id_, const std::string& what_arg)
1027  {
1028  std::string w = exception::name("invalid_iterator", id_) + what_arg;
1029  return invalid_iterator(id_, w.c_str());
1030  }
1031 
1032  private:
1033  invalid_iterator(int id_, const char* what_arg)
1034  : exception(id_, what_arg) {}
1035 };
1036 
1037 /*!
1038 @brief exception indicating executing a member function with a wrong type
1039 
1040 This exception is thrown in case of a type error; that is, a library function is
1041 executed on a JSON value whose type does not match the expected semantics.
1042 
1043 Exceptions have ids 3xx.
1044 
1045 name / id | example message | description
1046 ----------------------------- | --------------- | -------------------------
1047 json.exception.type_error.301 | cannot create object from initializer list | To create an object from an initializer list, the initializer list must consist only of a list of pairs whose first element is a string. When this constraint is violated, an array is created instead.
1048 json.exception.type_error.302 | type must be object, but is array | During implicit or explicit value conversion, the JSON type must be compatible to the target type. For instance, a JSON string can only be converted into string types, but not into numbers or boolean types.
1049 json.exception.type_error.303 | incompatible ReferenceType for get_ref, actual type is object | To retrieve a reference to a value stored in a @ref basic_json object with @ref get_ref, the type of the reference must match the value type. For instance, for a JSON array, the @a ReferenceType must be @ref array_t&.
1050 json.exception.type_error.304 | cannot use at() with string | The @ref at() member functions can only be executed for certain JSON types.
1051 json.exception.type_error.305 | cannot use operator[] with string | The @ref operator[] member functions can only be executed for certain JSON types.
1052 json.exception.type_error.306 | cannot use value() with string | The @ref value() member functions can only be executed for certain JSON types.
1053 json.exception.type_error.307 | cannot use erase() with string | The @ref erase() member functions can only be executed for certain JSON types.
1054 json.exception.type_error.308 | cannot use push_back() with string | The @ref push_back() and @ref operator+= member functions can only be executed for certain JSON types.
1055 json.exception.type_error.309 | cannot use insert() with | The @ref insert() member functions can only be executed for certain JSON types.
1056 json.exception.type_error.310 | cannot use swap() with number | The @ref swap() member functions can only be executed for certain JSON types.
1057 json.exception.type_error.311 | cannot use emplace_back() with string | The @ref emplace_back() member function can only be executed for certain JSON types.
1058 json.exception.type_error.312 | cannot use update() with string | The @ref update() member functions can only be executed for certain JSON types.
1059 json.exception.type_error.313 | invalid value to unflatten | The @ref unflatten function converts an object whose keys are JSON Pointers back into an arbitrary nested JSON value. The JSON Pointers must not overlap, because then the resulting value would not be well defined.
1060 json.exception.type_error.314 | only objects can be unflattened | The @ref unflatten function only works for an object whose keys are JSON Pointers.
1061 json.exception.type_error.315 | values in object must be primitive | The @ref unflatten function only works for an object whose keys are JSON Pointers and whose values are primitive.
1062 json.exception.type_error.316 | invalid UTF-8 byte at index 10: 0x7E | The @ref dump function only works with UTF-8 encoded strings; that is, if you assign a `std::string` to a JSON value, make sure it is UTF-8 encoded. |
1063 json.exception.type_error.317 | JSON value cannot be serialized to requested format | The dynamic type of the object cannot be represented in the requested serialization format (e.g. a raw `true` or `null` JSON object cannot be serialized to BSON) |
1064 
1065 @liveexample{The following code shows how a `type_error` exception can be
1066 caught.,type_error}
1067 
1068 @sa @ref exception for the base class of the library exceptions
1069 @sa @ref parse_error for exceptions indicating a parse error
1070 @sa @ref invalid_iterator for exceptions indicating errors with iterators
1071 @sa @ref out_of_range for exceptions indicating access out of the defined range
1072 @sa @ref other_error for exceptions indicating other library errors
1073 
1074 @since version 3.0.0
1075 */
1076 class type_error : public exception
1077 {
1078  public:
1079  static type_error create(int id_, const std::string& what_arg)
1080  {
1081  std::string w = exception::name("type_error", id_) + what_arg;
1082  return type_error(id_, w.c_str());
1083  }
1084 
1085  private:
1086  type_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
1087 };
1088 
1089 /*!
1090 @brief exception indicating access out of the defined range
1091 
1092 This exception is thrown in case a library function is called on an input
1093 parameter that exceeds the expected range, for instance in case of array
1094 indices or nonexisting object keys.
1095 
1096 Exceptions have ids 4xx.
1097 
1098 name / id | example message | description
1099 ------------------------------- | --------------- | -------------------------
1100 json.exception.out_of_range.401 | array index 3 is out of range | The provided array index @a i is larger than @a size-1.
1101 json.exception.out_of_range.402 | array index '-' (3) is out of range | The special array index `-` in a JSON Pointer never describes a valid element of the array, but the index past the end. That is, it can only be used to add elements at this position, but not to read it.
1102 json.exception.out_of_range.403 | key 'foo' not found | The provided key was not found in the JSON object.
1103 json.exception.out_of_range.404 | unresolved reference token 'foo' | A reference token in a JSON Pointer could not be resolved.
1104 json.exception.out_of_range.405 | JSON pointer has no parent | The JSON Patch operations 'remove' and 'add' can not be applied to the root element of the JSON value.
1105 json.exception.out_of_range.406 | number overflow parsing '10E1000' | A parsed number could not be stored as without changing it to NaN or INF.
1106 json.exception.out_of_range.407 | number overflow serializing '9223372036854775808' | UBJSON and BSON only support integer numbers up to 9223372036854775807. |
1107 json.exception.out_of_range.408 | excessive array size: 8658170730974374167 | The size (following `#`) of an UBJSON array or object exceeds the maximal capacity. |
1108 json.exception.out_of_range.409 | BSON key cannot contain code point U+0000 (at byte 2) | Key identifiers to be serialized to BSON cannot contain code point U+0000, since the key is stored as zero-terminated c-string |
1109 
1110 @liveexample{The following code shows how an `out_of_range` exception can be
1111 caught.,out_of_range}
1112 
1113 @sa @ref exception for the base class of the library exceptions
1114 @sa @ref parse_error for exceptions indicating a parse error
1115 @sa @ref invalid_iterator for exceptions indicating errors with iterators
1116 @sa @ref type_error for exceptions indicating executing a member function with
1117  a wrong type
1118 @sa @ref other_error for exceptions indicating other library errors
1119 
1120 @since version 3.0.0
1121 */
1122 class out_of_range : public exception
1123 {
1124  public:
1125  static out_of_range create(int id_, const std::string& what_arg)
1126  {
1127  std::string w = exception::name("out_of_range", id_) + what_arg;
1128  return out_of_range(id_, w.c_str());
1129  }
1130 
1131  private:
1132  out_of_range(int id_, const char* what_arg) : exception(id_, what_arg) {}
1133 };
1134 
1135 /*!
1136 @brief exception indicating other library errors
1137 
1138 This exception is thrown in case of errors that cannot be classified with the
1139 other exception types.
1140 
1141 Exceptions have ids 5xx.
1142 
1143 name / id | example message | description
1144 ------------------------------ | --------------- | -------------------------
1145 json.exception.other_error.501 | unsuccessful: {"op":"test","path":"/baz", "value":"bar"} | A JSON Patch operation 'test' failed. The unsuccessful operation is also printed.
1146 
1147 @sa @ref exception for the base class of the library exceptions
1148 @sa @ref parse_error for exceptions indicating a parse error
1149 @sa @ref invalid_iterator for exceptions indicating errors with iterators
1150 @sa @ref type_error for exceptions indicating executing a member function with
1151  a wrong type
1152 @sa @ref out_of_range for exceptions indicating access out of the defined range
1153 
1154 @liveexample{The following code shows how an `other_error` exception can be
1155 caught.,other_error}
1156 
1157 @since version 3.0.0
1158 */
1159 class other_error : public exception
1160 {
1161  public:
1162  static other_error create(int id_, const std::string& what_arg)
1163  {
1164  std::string w = exception::name("other_error", id_) + what_arg;
1165  return other_error(id_, w.c_str());
1166  }
1167 
1168  private:
1169  other_error(int id_, const char* what_arg) : exception(id_, what_arg) {}
1170 };
1171 } // namespace detail
1172 } // namespace nlohmann
1173 
1174 // #include <nlohmann/detail/value_t.hpp>
1175 
1176 
1177 #include <array> // array
1178 #include <ciso646> // and
1179 #include <cstddef> // size_t
1180 #include <cstdint> // uint8_t
1181 
1182 namespace nlohmann
1183 {
1184 namespace detail
1185 {
1186 ///////////////////////////
1187 // JSON type enumeration //
1188 ///////////////////////////
1189 
1190 /*!
1191 @brief the JSON type enumeration
1192 
1193 This enumeration collects the different JSON types. It is internally used to
1194 distinguish the stored values, and the functions @ref basic_json::is_null(),
1195 @ref basic_json::is_object(), @ref basic_json::is_array(),
1196 @ref basic_json::is_string(), @ref basic_json::is_boolean(),
1197 @ref basic_json::is_number() (with @ref basic_json::is_number_integer(),
1198 @ref basic_json::is_number_unsigned(), and @ref basic_json::is_number_float()),
1199 @ref basic_json::is_discarded(), @ref basic_json::is_primitive(), and
1200 @ref basic_json::is_structured() rely on it.
1201 
1202 @note There are three enumeration entries (number_integer, number_unsigned, and
1203 number_float), because the library distinguishes these three types for numbers:
1204 @ref basic_json::number_unsigned_t is used for unsigned integers,
1205 @ref basic_json::number_integer_t is used for signed integers, and
1206 @ref basic_json::number_float_t is used for floating-point numbers or to
1207 approximate integers which do not fit in the limits of their respective type.
1208 
1209 @sa @ref basic_json::basic_json(const value_t value_type) -- create a JSON
1210 value with the default value for a given type
1211 
1212 @since version 1.0.0
1213 */
1214 enum class value_t : std::uint8_t
1215 {
1216  null, ///< null value
1217  object, ///< object (unordered set of name/value pairs)
1218  array, ///< array (ordered collection of values)
1219  string, ///< string value
1220  boolean, ///< boolean value
1221  number_integer, ///< number value (signed integer)
1222  number_unsigned, ///< number value (unsigned integer)
1223  number_float, ///< number value (floating-point)
1224  discarded ///< discarded by the the parser callback function
1225 };
1226 
1227 /*!
1228 @brief comparison operator for JSON types
1229 
1230 Returns an ordering that is similar to Python:
1231 - order: null < boolean < number < object < array < string
1232 - furthermore, each type is not smaller than itself
1233 - discarded values are not comparable
1234 
1235 @since version 1.0.0
1236 */
1237 inline bool operator<(const value_t lhs, const value_t rhs) noexcept
1238 {
1239  static constexpr std::array<std::uint8_t, 8> order = {{
1240  0 /* null */, 3 /* object */, 4 /* array */, 5 /* string */,
1241  1 /* boolean */, 2 /* integer */, 2 /* unsigned */, 2 /* float */
1242  }
1243  };
1244 
1245  const auto l_index = static_cast<std::size_t>(lhs);
1246  const auto r_index = static_cast<std::size_t>(rhs);
1247  return l_index < order.size() and r_index < order.size() and order[l_index] < order[r_index];
1248 }
1249 } // namespace detail
1250 } // namespace nlohmann
1251 
1252 // #include <nlohmann/detail/conversions/from_json.hpp>
1253 
1254 
1255 #include <algorithm> // transform
1256 #include <array> // array
1257 #include <ciso646> // and, not
1258 #include <forward_list> // forward_list
1259 #include <iterator> // inserter, front_inserter, end
1260 #include <map> // map
1261 #include <string> // string
1262 #include <tuple> // tuple, make_tuple
1263 #include <type_traits> // is_arithmetic, is_same, is_enum, underlying_type, is_convertible
1264 #include <unordered_map> // unordered_map
1265 #include <utility> // pair, declval
1266 #include <valarray> // valarray
1267 
1268 // #include <nlohmann/detail/exceptions.hpp>
1269 
1270 // #include <nlohmann/detail/macro_scope.hpp>
1271 
1272 // #include <nlohmann/detail/meta/cpp_future.hpp>
1273 
1274 // #include <nlohmann/detail/meta/type_traits.hpp>
1275 
1276 // #include <nlohmann/detail/value_t.hpp>
1277 
1278 
1279 namespace nlohmann
1280 {
1281 namespace detail
1282 {
1283 template<typename BasicJsonType>
1284 void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
1285 {
1286  if (JSON_UNLIKELY(not j.is_null()))
1287  {
1288  JSON_THROW(type_error::create(302, "type must be null, but is " + std::string(j.type_name())));
1289  }
1290  n = nullptr;
1291 }
1292 
1293 // overloads for basic_json template parameters
1294 template<typename BasicJsonType, typename ArithmeticType,
1295  enable_if_t<std::is_arithmetic<ArithmeticType>::value and
1296  not std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
1297  int> = 0>
1298 void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
1299 {
1300  switch (static_cast<value_t>(j))
1301  {
1302  case value_t::number_unsigned:
1303  {
1304  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
1305  break;
1306  }
1307  case value_t::number_integer:
1308  {
1309  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
1310  break;
1311  }
1312  case value_t::number_float:
1313  {
1314  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
1315  break;
1316  }
1317 
1318  default:
1319  JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name())));
1320  }
1321 }
1322 
1323 template<typename BasicJsonType>
1324 void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
1325 {
1326  if (JSON_UNLIKELY(not j.is_boolean()))
1327  {
1328  JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(j.type_name())));
1329  }
1330  b = *j.template get_ptr<const typename BasicJsonType::boolean_t*>();
1331 }
1332 
1333 template<typename BasicJsonType>
1334 void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
1335 {
1336  if (JSON_UNLIKELY(not j.is_string()))
1337  {
1338  JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name())));
1339  }
1340  s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
1341 }
1342 
1343 template <
1344  typename BasicJsonType, typename ConstructibleStringType,
1345  enable_if_t <
1346  is_constructible_string_type<BasicJsonType, ConstructibleStringType>::value and
1347  not std::is_same<typename BasicJsonType::string_t,
1348  ConstructibleStringType>::value,
1349  int > = 0 >
1350 void from_json(const BasicJsonType& j, ConstructibleStringType& s)
1351 {
1352  if (JSON_UNLIKELY(not j.is_string()))
1353  {
1354  JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name())));
1355  }
1356 
1357  s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
1358 }
1359 
1360 template<typename BasicJsonType>
1361 void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val)
1362 {
1363  get_arithmetic_value(j, val);
1364 }
1365 
1366 template<typename BasicJsonType>
1367 void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val)
1368 {
1369  get_arithmetic_value(j, val);
1370 }
1371 
1372 template<typename BasicJsonType>
1373 void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val)
1374 {
1375  get_arithmetic_value(j, val);
1376 }
1377 
1378 template<typename BasicJsonType, typename EnumType,
1379  enable_if_t<std::is_enum<EnumType>::value, int> = 0>
1380 void from_json(const BasicJsonType& j, EnumType& e)
1381 {
1382  typename std::underlying_type<EnumType>::type val;
1383  get_arithmetic_value(j, val);
1384  e = static_cast<EnumType>(val);
1385 }
1386 
1387 // forward_list doesn't have an insert method
1388 template<typename BasicJsonType, typename T, typename Allocator,
1389  enable_if_t<std::is_convertible<BasicJsonType, T>::value, int> = 0>
1390 void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
1391 {
1392  if (JSON_UNLIKELY(not j.is_array()))
1393  {
1394  JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name())));
1395  }
1396  std::transform(j.rbegin(), j.rend(),
1397  std::front_inserter(l), [](const BasicJsonType & i)
1398  {
1399  return i.template get<T>();
1400  });
1401 }
1402 
1403 // valarray doesn't have an insert method
1404 template<typename BasicJsonType, typename T,
1405  enable_if_t<std::is_convertible<BasicJsonType, T>::value, int> = 0>
1406 void from_json(const BasicJsonType& j, std::valarray<T>& l)
1407 {
1408  if (JSON_UNLIKELY(not j.is_array()))
1409  {
1410  JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name())));
1411  }
1412  l.resize(j.size());
1413  std::copy(j.m_value.array->begin(), j.m_value.array->end(), std::begin(l));
1414 }
1415 
1416 template<typename BasicJsonType>
1417 void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType::array_t& arr, priority_tag<3> /*unused*/)
1418 {
1419  arr = *j.template get_ptr<const typename BasicJsonType::array_t*>();
1420 }
1421 
1422 template <typename BasicJsonType, typename T, std::size_t N>
1423 auto from_json_array_impl(const BasicJsonType& j, std::array<T, N>& arr,
1424  priority_tag<2> /*unused*/)
1425 -> decltype(j.template get<T>(), void())
1426 {
1427  for (std::size_t i = 0; i < N; ++i)
1428  {
1429  arr[i] = j.at(i).template get<T>();
1430  }
1431 }
1432 
1433 template<typename BasicJsonType, typename ConstructibleArrayType>
1434 auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<1> /*unused*/)
1435 -> decltype(
1436  arr.reserve(std::declval<typename ConstructibleArrayType::size_type>()),
1437  j.template get<typename ConstructibleArrayType::value_type>(),
1438  void())
1439 {
1440  using std::end;
1441 
1442  arr.reserve(j.size());
1443  std::transform(j.begin(), j.end(),
1444  std::inserter(arr, end(arr)), [](const BasicJsonType & i)
1445  {
1446  // get<BasicJsonType>() returns *this, this won't call a from_json
1447  // method when value_type is BasicJsonType
1448  return i.template get<typename ConstructibleArrayType::value_type>();
1449  });
1450 }
1451 
1452 template <typename BasicJsonType, typename ConstructibleArrayType>
1453 void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr,
1454  priority_tag<0> /*unused*/)
1455 {
1456  using std::end;
1457 
1458  std::transform(
1459  j.begin(), j.end(), std::inserter(arr, end(arr)),
1460  [](const BasicJsonType & i)
1461  {
1462  // get<BasicJsonType>() returns *this, this won't call a from_json
1463  // method when value_type is BasicJsonType
1464  return i.template get<typename ConstructibleArrayType::value_type>();
1465  });
1466 }
1467 
1468 template <typename BasicJsonType, typename ConstructibleArrayType,
1469  enable_if_t <
1470  is_constructible_array_type<BasicJsonType, ConstructibleArrayType>::value and
1471  not is_constructible_object_type<BasicJsonType, ConstructibleArrayType>::value and
1472  not is_constructible_string_type<BasicJsonType, ConstructibleArrayType>::value and
1473  not is_basic_json<ConstructibleArrayType>::value,
1474  int > = 0 >
1475 
1476 auto from_json(const BasicJsonType& j, ConstructibleArrayType& arr)
1477 -> decltype(from_json_array_impl(j, arr, priority_tag<3> {}),
1478 j.template get<typename ConstructibleArrayType::value_type>(),
1479 void())
1480 {
1481  if (JSON_UNLIKELY(not j.is_array()))
1482  {
1483  JSON_THROW(type_error::create(302, "type must be array, but is " +
1484  std::string(j.type_name())));
1485  }
1486 
1487  from_json_array_impl(j, arr, priority_tag<3> {});
1488 }
1489 
1490 template<typename BasicJsonType, typename ConstructibleObjectType,
1491  enable_if_t<is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value, int> = 0>
1492 void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
1493 {
1494  if (JSON_UNLIKELY(not j.is_object()))
1495  {
1496  JSON_THROW(type_error::create(302, "type must be object, but is " + std::string(j.type_name())));
1497  }
1498 
1499  auto inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
1500  using value_type = typename ConstructibleObjectType::value_type;
1501  std::transform(
1502  inner_object->begin(), inner_object->end(),
1503  std::inserter(obj, obj.begin()),
1504  [](typename BasicJsonType::object_t::value_type const & p)
1505  {
1506  return value_type(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
1507  });
1508 }
1509 
1510 // overload for arithmetic types, not chosen for basic_json template arguments
1511 // (BooleanType, etc..); note: Is it really necessary to provide explicit
1512 // overloads for boolean_t etc. in case of a custom BooleanType which is not
1513 // an arithmetic type?
1514 template<typename BasicJsonType, typename ArithmeticType,
1515  enable_if_t <
1516  std::is_arithmetic<ArithmeticType>::value and
1517  not std::is_same<ArithmeticType, typename BasicJsonType::number_unsigned_t>::value and
1518  not std::is_same<ArithmeticType, typename BasicJsonType::number_integer_t>::value and
1519  not std::is_same<ArithmeticType, typename BasicJsonType::number_float_t>::value and
1520  not std::is_same<ArithmeticType, typename BasicJsonType::boolean_t>::value,
1521  int> = 0>
1522 void from_json(const BasicJsonType& j, ArithmeticType& val)
1523 {
1524  switch (static_cast<value_t>(j))
1525  {
1526  case value_t::number_unsigned:
1527  {
1528  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_unsigned_t*>());
1529  break;
1530  }
1531  case value_t::number_integer:
1532  {
1533  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_integer_t*>());
1534  break;
1535  }
1536  case value_t::number_float:
1537  {
1538  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
1539  break;
1540  }
1541  case value_t::boolean:
1542  {
1543  val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
1544  break;
1545  }
1546 
1547  default:
1548  JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name())));
1549  }
1550 }
1551 
1552 template<typename BasicJsonType, typename A1, typename A2>
1553 void from_json(const BasicJsonType& j, std::pair<A1, A2>& p)
1554 {
1555  p = {j.at(0).template get<A1>(), j.at(1).template get<A2>()};
1556 }
1557 
1558 template<typename BasicJsonType, typename Tuple, std::size_t... Idx>
1559 void from_json_tuple_impl(const BasicJsonType& j, Tuple& t, index_sequence<Idx...> /*unused*/)
1560 {
1561  t = std::make_tuple(j.at(Idx).template get<typename std::tuple_element<Idx, Tuple>::type>()...);
1562 }
1563 
1564 template<typename BasicJsonType, typename... Args>
1565 void from_json(const BasicJsonType& j, std::tuple<Args...>& t)
1566 {
1567  from_json_tuple_impl(j, t, index_sequence_for<Args...> {});
1568 }
1569 
1570 template <typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator,
1571  typename = enable_if_t<not std::is_constructible<
1572  typename BasicJsonType::string_t, Key>::value>>
1573 void from_json(const BasicJsonType& j, std::map<Key, Value, Compare, Allocator>& m)
1574 {
1575  if (JSON_UNLIKELY(not j.is_array()))
1576  {
1577  JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name())));
1578  }
1579  for (const auto& p : j)
1580  {
1581  if (JSON_UNLIKELY(not p.is_array()))
1582  {
1583  JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name())));
1584  }
1585  m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
1586  }
1587 }
1588 
1589 template <typename BasicJsonType, typename Key, typename Value, typename Hash, typename KeyEqual, typename Allocator,
1590  typename = enable_if_t<not std::is_constructible<
1591  typename BasicJsonType::string_t, Key>::value>>
1592 void from_json(const BasicJsonType& j, std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>& m)
1593 {
1594  if (JSON_UNLIKELY(not j.is_array()))
1595  {
1596  JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name())));
1597  }
1598  for (const auto& p : j)
1599  {
1600  if (JSON_UNLIKELY(not p.is_array()))
1601  {
1602  JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name())));
1603  }
1604  m.emplace(p.at(0).template get<Key>(), p.at(1).template get<Value>());
1605  }
1606 }
1607 
1608 struct from_json_fn
1609 {
1610  template<typename BasicJsonType, typename T>
1611  auto operator()(const BasicJsonType& j, T& val) const
1612  noexcept(noexcept(from_json(j, val)))
1613  -> decltype(from_json(j, val), void())
1614  {
1615  return from_json(j, val);
1616  }
1617 };
1618 } // namespace detail
1619 
1620 /// namespace to hold default `from_json` function
1621 /// to see why this is required:
1622 /// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html
1623 namespace
1624 {
1625 constexpr const auto& from_json = detail::static_const<detail::from_json_fn>::value;
1626 } // namespace
1627 } // namespace nlohmann
1628 
1629 // #include <nlohmann/detail/conversions/to_json.hpp>
1630 
1631 
1632 #include <ciso646> // or, and, not
1633 #include <iterator> // begin, end
1634 #include <tuple> // tuple, get
1635 #include <type_traits> // is_same, is_constructible, is_floating_point, is_enum, underlying_type
1636 #include <utility> // move, forward, declval, pair
1637 #include <valarray> // valarray
1638 #include <vector> // vector
1639 
1640 // #include <nlohmann/detail/meta/cpp_future.hpp>
1641 
1642 // #include <nlohmann/detail/meta/type_traits.hpp>
1643 
1644 // #include <nlohmann/detail/value_t.hpp>
1645 
1646 // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
1647 
1648 
1649 #include <cstddef> // size_t
1650 #include <string> // string, to_string
1651 #include <iterator> // input_iterator_tag
1652 #include <tuple> // tuple_size, get, tuple_element
1653 
1654 // #include <nlohmann/detail/value_t.hpp>
1655 
1656 // #include <nlohmann/detail/meta/type_traits.hpp>
1657 
1658 
1659 namespace nlohmann
1660 {
1661 namespace detail
1662 {
1663 template <typename IteratorType> class iteration_proxy_value
1664 {
1665  public:
1666  using difference_type = std::ptrdiff_t;
1667  using value_type = iteration_proxy_value;
1668  using pointer = value_type * ;
1669  using reference = value_type & ;
1670  using iterator_category = std::input_iterator_tag;
1671 
1672  private:
1673  /// the iterator
1674  IteratorType anchor;
1675  /// an index for arrays (used to create key names)
1676  std::size_t array_index = 0;
1677  /// last stringified array index
1678  mutable std::size_t array_index_last = 0;
1679  /// a string representation of the array index
1680  mutable std::string array_index_str = "0";
1681  /// an empty string (to return a reference for primitive values)
1682  const std::string empty_str = "";
1683 
1684  public:
1685  explicit iteration_proxy_value(IteratorType it) noexcept : anchor(it) {}
1686 
1687  /// dereference operator (needed for range-based for)
1688  iteration_proxy_value& operator*()
1689  {
1690  return *this;
1691  }
1692 
1693  /// increment operator (needed for range-based for)
1694  iteration_proxy_value& operator++()
1695  {
1696  ++anchor;
1697  ++array_index;
1698 
1699  return *this;
1700  }
1701 
1702  /// equality operator (needed for InputIterator)
1703  bool operator==(const iteration_proxy_value& o) const noexcept
1704  {
1705  return anchor == o.anchor;
1706  }
1707 
1708  /// inequality operator (needed for range-based for)
1709  bool operator!=(const iteration_proxy_value& o) const noexcept
1710  {
1711  return anchor != o.anchor;
1712  }
1713 
1714  /// return key of the iterator
1715  const std::string& key() const
1716  {
1717  assert(anchor.m_object != nullptr);
1718 
1719  switch (anchor.m_object->type())
1720  {
1721  // use integer array index as key
1722  case value_t::array:
1723  {
1724  if (array_index != array_index_last)
1725  {
1726  array_index_str = std::to_string(array_index);
1727  array_index_last = array_index;
1728  }
1729  return array_index_str;
1730  }
1731 
1732  // use key from the object
1733  case value_t::object:
1734  return anchor.key();
1735 
1736  // use an empty key for all primitive types
1737  default:
1738  return empty_str;
1739  }
1740  }
1741 
1742  /// return value of the iterator
1743  typename IteratorType::reference value() const
1744  {
1745  return anchor.value();
1746  }
1747 };
1748 
1749 /// proxy class for the items() function
1750 template<typename IteratorType> class iteration_proxy
1751 {
1752  private:
1753  /// the container to iterate
1754  typename IteratorType::reference container;
1755 
1756  public:
1757  /// construct iteration proxy from a container
1758  explicit iteration_proxy(typename IteratorType::reference cont) noexcept
1759  : container(cont) {}
1760 
1761  /// return iterator begin (needed for range-based for)
1762  iteration_proxy_value<IteratorType> begin() noexcept
1763  {
1764  return iteration_proxy_value<IteratorType>(container.begin());
1765  }
1766 
1767  /// return iterator end (needed for range-based for)
1768  iteration_proxy_value<IteratorType> end() noexcept
1769  {
1770  return iteration_proxy_value<IteratorType>(container.end());
1771  }
1772 };
1773 // Structured Bindings Support
1774 // For further reference see https://blog.tartanllama.xyz/structured-bindings/
1775 // And see https://github.com/nlohmann/json/pull/1391
1776 template <std::size_t N, typename IteratorType, enable_if_t<N == 0, int> = 0>
1777 auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.key())
1778 {
1779  return i.key();
1780 }
1781 // Structured Bindings Support
1782 // For further reference see https://blog.tartanllama.xyz/structured-bindings/
1783 // And see https://github.com/nlohmann/json/pull/1391
1784 template <std::size_t N, typename IteratorType, enable_if_t<N == 1, int> = 0>
1785 auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.value())
1786 {
1787  return i.value();
1788 }
1789 } // namespace detail
1790 } // namespace nlohmann
1791 
1792 // The Addition to the STD Namespace is required to add
1793 // Structured Bindings Support to the iteration_proxy_value class
1794 // For further reference see https://blog.tartanllama.xyz/structured-bindings/
1795 // And see https://github.com/nlohmann/json/pull/1391
1796 namespace std
1797 {
1798 template <typename IteratorType>
1799 class tuple_size<::nlohmann::detail::iteration_proxy_value<IteratorType>>
1800  : public std::integral_constant<std::size_t, 2> {};
1801 
1802 template <std::size_t N, typename IteratorType>
1803 class tuple_element<N, ::nlohmann::detail::iteration_proxy_value<IteratorType >>
1804 {
1805  public:
1806  using type = decltype(
1807  get<N>(std::declval <
1808  ::nlohmann::detail::iteration_proxy_value<IteratorType >> ()));
1809 };
1810 }
1811 
1812 namespace nlohmann
1813 {
1814 namespace detail
1815 {
1816 //////////////////
1817 // constructors //
1818 //////////////////
1819 
1820 template<value_t> struct external_constructor;
1821 
1822 template<>
1823 struct external_constructor<value_t::boolean>
1824 {
1825  template<typename BasicJsonType>
1826  static void construct(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept
1827  {
1828  j.m_type = value_t::boolean;
1829  j.m_value = b;
1830  j.assert_invariant();
1831  }
1832 };
1833 
1834 template<>
1835 struct external_constructor<value_t::string>
1836 {
1837  template<typename BasicJsonType>
1838  static void construct(BasicJsonType& j, const typename BasicJsonType::string_t& s)
1839  {
1840  j.m_type = value_t::string;
1841  j.m_value = s;
1842  j.assert_invariant();
1843  }
1844 
1845  template<typename BasicJsonType>
1846  static void construct(BasicJsonType& j, typename BasicJsonType::string_t&& s)
1847  {
1848  j.m_type = value_t::string;
1849  j.m_value = std::move(s);
1850  j.assert_invariant();
1851  }
1852 
1853  template<typename BasicJsonType, typename CompatibleStringType,
1854  enable_if_t<not std::is_same<CompatibleStringType, typename BasicJsonType::string_t>::value,
1855  int> = 0>
1856  static void construct(BasicJsonType& j, const CompatibleStringType& str)
1857  {
1858  j.m_type = value_t::string;
1859  j.m_value.string = j.template create<typename BasicJsonType::string_t>(str);
1860  j.assert_invariant();
1861  }
1862 };
1863 
1864 template<>
1865 struct external_constructor<value_t::number_float>
1866 {
1867  template<typename BasicJsonType>
1868  static void construct(BasicJsonType& j, typename BasicJsonType::number_float_t val) noexcept
1869  {
1870  j.m_type = value_t::number_float;
1871  j.m_value = val;
1872  j.assert_invariant();
1873  }
1874 };
1875 
1876 template<>
1877 struct external_constructor<value_t::number_unsigned>
1878 {
1879  template<typename BasicJsonType>
1880  static void construct(BasicJsonType& j, typename BasicJsonType::number_unsigned_t val) noexcept
1881  {
1882  j.m_type = value_t::number_unsigned;
1883  j.m_value = val;
1884  j.assert_invariant();
1885  }
1886 };
1887 
1888 template<>
1889 struct external_constructor<value_t::number_integer>
1890 {
1891  template<typename BasicJsonType>
1892  static void construct(BasicJsonType& j, typename BasicJsonType::number_integer_t val) noexcept
1893  {
1894  j.m_type = value_t::number_integer;
1895  j.m_value = val;
1896  j.assert_invariant();
1897  }
1898 };
1899 
1900 template<>
1901 struct external_constructor<value_t::array>
1902 {
1903  template<typename BasicJsonType>
1904  static void construct(BasicJsonType& j, const typename BasicJsonType::array_t& arr)
1905  {
1906  j.m_type = value_t::array;
1907  j.m_value = arr;
1908  j.assert_invariant();
1909  }
1910 
1911  template<typename BasicJsonType>
1912  static void construct(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
1913  {
1914  j.m_type = value_t::array;
1915  j.m_value = std::move(arr);
1916  j.assert_invariant();
1917  }
1918 
1919  template<typename BasicJsonType, typename CompatibleArrayType,
1920  enable_if_t<not std::is_same<CompatibleArrayType, typename BasicJsonType::array_t>::value,
1921  int> = 0>
1922  static void construct(BasicJsonType& j, const CompatibleArrayType& arr)
1923  {
1924  using std::begin;
1925  using std::end;
1926  j.m_type = value_t::array;
1927  j.m_value.array = j.template create<typename BasicJsonType::array_t>(begin(arr), end(arr));
1928  j.assert_invariant();
1929  }
1930 
1931  template<typename BasicJsonType>
1932  static void construct(BasicJsonType& j, const std::vector<bool>& arr)
1933  {
1934  j.m_type = value_t::array;
1935  j.m_value = value_t::array;
1936  j.m_value.array->reserve(arr.size());
1937  for (const bool x : arr)
1938  {
1939  j.m_value.array->push_back(x);
1940  }
1941  j.assert_invariant();
1942  }
1943 
1944  template<typename BasicJsonType, typename T,
1945  enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
1946  static void construct(BasicJsonType& j, const std::valarray<T>& arr)
1947  {
1948  j.m_type = value_t::array;
1949  j.m_value = value_t::array;
1950  j.m_value.array->resize(arr.size());
1951  std::copy(std::begin(arr), std::end(arr), j.m_value.array->begin());
1952  j.assert_invariant();
1953  }
1954 };
1955 
1956 template<>
1957 struct external_constructor<value_t::object>
1958 {
1959  template<typename BasicJsonType>
1960  static void construct(BasicJsonType& j, const typename BasicJsonType::object_t& obj)
1961  {
1962  j.m_type = value_t::object;
1963  j.m_value = obj;
1964  j.assert_invariant();
1965  }
1966 
1967  template<typename BasicJsonType>
1968  static void construct(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
1969  {
1970  j.m_type = value_t::object;
1971  j.m_value = std::move(obj);
1972  j.assert_invariant();
1973  }
1974 
1975  template<typename BasicJsonType, typename CompatibleObjectType,
1976  enable_if_t<not std::is_same<CompatibleObjectType, typename BasicJsonType::object_t>::value, int> = 0>
1977  static void construct(BasicJsonType& j, const CompatibleObjectType& obj)
1978  {
1979  using std::begin;
1980  using std::end;
1981 
1982  j.m_type = value_t::object;
1983  j.m_value.object = j.template create<typename BasicJsonType::object_t>(begin(obj), end(obj));
1984  j.assert_invariant();
1985  }
1986 };
1987 
1988 /////////////
1989 // to_json //
1990 /////////////
1991 
1992 template<typename BasicJsonType, typename T,
1993  enable_if_t<std::is_same<T, typename BasicJsonType::boolean_t>::value, int> = 0>
1994 void to_json(BasicJsonType& j, T b) noexcept
1995 {
1996  external_constructor<value_t::boolean>::construct(j, b);
1997 }
1998 
1999 template<typename BasicJsonType, typename CompatibleString,
2000  enable_if_t<std::is_constructible<typename BasicJsonType::string_t, CompatibleString>::value, int> = 0>
2001 void to_json(BasicJsonType& j, const CompatibleString& s)
2002 {
2003  external_constructor<value_t::string>::construct(j, s);
2004 }
2005 
2006 template<typename BasicJsonType>
2007 void to_json(BasicJsonType& j, typename BasicJsonType::string_t&& s)
2008 {
2009  external_constructor<value_t::string>::construct(j, std::move(s));
2010 }
2011 
2012 template<typename BasicJsonType, typename FloatType,
2013  enable_if_t<std::is_floating_point<FloatType>::value, int> = 0>
2014 void to_json(BasicJsonType& j, FloatType val) noexcept
2015 {
2016  external_constructor<value_t::number_float>::construct(j, static_cast<typename BasicJsonType::number_float_t>(val));
2017 }
2018 
2019 template<typename BasicJsonType, typename CompatibleNumberUnsignedType,
2020  enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_unsigned_t, CompatibleNumberUnsignedType>::value, int> = 0>
2021 void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept
2022 {
2023  external_constructor<value_t::number_unsigned>::construct(j, static_cast<typename BasicJsonType::number_unsigned_t>(val));
2024 }
2025 
2026 template<typename BasicJsonType, typename CompatibleNumberIntegerType,
2027  enable_if_t<is_compatible_integer_type<typename BasicJsonType::number_integer_t, CompatibleNumberIntegerType>::value, int> = 0>
2028 void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept
2029 {
2030  external_constructor<value_t::number_integer>::construct(j, static_cast<typename BasicJsonType::number_integer_t>(val));
2031 }
2032 
2033 template<typename BasicJsonType, typename EnumType,
2034  enable_if_t<std::is_enum<EnumType>::value, int> = 0>
2035 void to_json(BasicJsonType& j, EnumType e) noexcept
2036 {
2037  using underlying_type = typename std::underlying_type<EnumType>::type;
2038  external_constructor<value_t::number_integer>::construct(j, static_cast<underlying_type>(e));
2039 }
2040 
2041 template<typename BasicJsonType>
2042 void to_json(BasicJsonType& j, const std::vector<bool>& e)
2043 {
2044  external_constructor<value_t::array>::construct(j, e);
2045 }
2046 
2047 template <typename BasicJsonType, typename CompatibleArrayType,
2048  enable_if_t<is_compatible_array_type<BasicJsonType,
2049  CompatibleArrayType>::value and
2050  not is_compatible_object_type<
2051  BasicJsonType, CompatibleArrayType>::value and
2052  not is_compatible_string_type<BasicJsonType, CompatibleArrayType>::value and
2053  not is_basic_json<CompatibleArrayType>::value,
2054  int> = 0>
2055 void to_json(BasicJsonType& j, const CompatibleArrayType& arr)
2056 {
2057  external_constructor<value_t::array>::construct(j, arr);
2058 }
2059 
2060 template<typename BasicJsonType, typename T,
2061  enable_if_t<std::is_convertible<T, BasicJsonType>::value, int> = 0>
2062 void to_json(BasicJsonType& j, const std::valarray<T>& arr)
2063 {
2064  external_constructor<value_t::array>::construct(j, std::move(arr));
2065 }
2066 
2067 template<typename BasicJsonType>
2068 void to_json(BasicJsonType& j, typename BasicJsonType::array_t&& arr)
2069 {
2070  external_constructor<value_t::array>::construct(j, std::move(arr));
2071 }
2072 
2073 template<typename BasicJsonType, typename CompatibleObjectType,
2074  enable_if_t<is_compatible_object_type<BasicJsonType, CompatibleObjectType>::value and not is_basic_json<CompatibleObjectType>::value, int> = 0>
2075 void to_json(BasicJsonType& j, const CompatibleObjectType& obj)
2076 {
2077  external_constructor<value_t::object>::construct(j, obj);
2078 }
2079 
2080 template<typename BasicJsonType>
2081 void to_json(BasicJsonType& j, typename BasicJsonType::object_t&& obj)
2082 {
2083  external_constructor<value_t::object>::construct(j, std::move(obj));
2084 }
2085 
2086 template <
2087  typename BasicJsonType, typename T, std::size_t N,
2088  enable_if_t<not std::is_constructible<typename BasicJsonType::string_t,
2089  const T(&)[N]>::value,
2090  int> = 0 >
2091 void to_json(BasicJsonType& j, const T(&arr)[N])
2092 {
2093  external_constructor<value_t::array>::construct(j, arr);
2094 }
2095 
2096 template<typename BasicJsonType, typename... Args>
2097 void to_json(BasicJsonType& j, const std::pair<Args...>& p)
2098 {
2099  j = { p.first, p.second };
2100 }
2101 
2102 // for https://github.com/nlohmann/json/pull/1134
2103 template < typename BasicJsonType, typename T,
2104  enable_if_t<std::is_same<T, iteration_proxy_value<typename BasicJsonType::iterator>>::value, int> = 0>
2105 void to_json(BasicJsonType& j, const T& b)
2106 {
2107  j = { {b.key(), b.value()} };
2108 }
2109 
2110 template<typename BasicJsonType, typename Tuple, std::size_t... Idx>
2111 void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence<Idx...> /*unused*/)
2112 {
2113  j = { std::get<Idx>(t)... };
2114 }
2115 
2116 template<typename BasicJsonType, typename... Args>
2117 void to_json(BasicJsonType& j, const std::tuple<Args...>& t)
2118 {
2119  to_json_tuple_impl(j, t, index_sequence_for<Args...> {});
2120 }
2121 
2122 struct to_json_fn
2123 {
2124  template<typename BasicJsonType, typename T>
2125  auto operator()(BasicJsonType& j, T&& val) const noexcept(noexcept(to_json(j, std::forward<T>(val))))
2126  -> decltype(to_json(j, std::forward<T>(val)), void())
2127  {
2128  return to_json(j, std::forward<T>(val));
2129  }
2130 };
2131 } // namespace detail
2132 
2133 /// namespace to hold default `to_json` function
2134 namespace
2135 {
2136 constexpr const auto& to_json = detail::static_const<detail::to_json_fn>::value;
2137 } // namespace
2138 } // namespace nlohmann
2139 
2140 // #include <nlohmann/detail/input/input_adapters.hpp>
2141 
2142 
2143 #include <cassert> // assert
2144 #include <cstddef> // size_t
2145 #include <cstring> // strlen
2146 #include <istream> // istream
2147 #include <iterator> // begin, end, iterator_traits, random_access_iterator_tag, distance, next
2148 #include <memory> // shared_ptr, make_shared, addressof
2149 #include <numeric> // accumulate
2150 #include <string> // string, char_traits
2151 #include <type_traits> // enable_if, is_base_of, is_pointer, is_integral, remove_pointer
2152 #include <utility> // pair, declval
2153 #include <cstdio> //FILE *
2154 
2155 // #include <nlohmann/detail/macro_scope.hpp>
2156 
2157 
2158 namespace nlohmann
2159 {
2160 namespace detail
2161 {
2162 /// the supported input formats
2163 enum class input_format_t { json, cbor, msgpack, ubjson, bson };
2164 
2165 ////////////////////
2166 // input adapters //
2167 ////////////////////
2168 
2169 /*!
2170 @brief abstract input adapter interface
2171 
2172 Produces a stream of std::char_traits<char>::int_type characters from a
2173 std::istream, a buffer, or some other input type. Accepts the return of
2174 exactly one non-EOF character for future input. The int_type characters
2175 returned consist of all valid char values as positive values (typically
2176 unsigned char), plus an EOF value outside that range, specified by the value
2177 of the function std::char_traits<char>::eof(). This value is typically -1, but
2178 could be any arbitrary value which is not a valid char value.
2179 */
2180 struct input_adapter_protocol
2181 {
2182  /// get a character [0,255] or std::char_traits<char>::eof().
2183  virtual std::char_traits<char>::int_type get_character() = 0;
2184  virtual ~input_adapter_protocol() = default;
2185 };
2186 
2187 /// a type to simplify interfaces
2188 using input_adapter_t = std::shared_ptr<input_adapter_protocol>;
2189 
2190 /*!
2191 Input adapter for stdio file access. This adapter read only 1 byte and do not use any
2192  buffer. This adapter is a very low level adapter.
2193 */
2194 class file_input_adapter : public input_adapter_protocol
2195 {
2196  public:
2197  explicit file_input_adapter(std::FILE* f) noexcept
2198  : m_file(f)
2199  {}
2200 
2201  std::char_traits<char>::int_type get_character() noexcept override
2202  {
2203  return std::fgetc(m_file);
2204  }
2205  private:
2206  /// the file pointer to read from
2207  std::FILE* m_file;
2208 };
2209 
2210 
2211 /*!
2212 Input adapter for a (caching) istream. Ignores a UFT Byte Order Mark at
2213 beginning of input. Does not support changing the underlying std::streambuf
2214 in mid-input. Maintains underlying std::istream and std::streambuf to support
2215 subsequent use of standard std::istream operations to process any input
2216 characters following those used in parsing the JSON input. Clears the
2217 std::istream flags; any input errors (e.g., EOF) will be detected by the first
2218 subsequent call for input from the std::istream.
2219 */
2220 class input_stream_adapter : public input_adapter_protocol
2221 {
2222  public:
2223  ~input_stream_adapter() override
2224  {
2225  // clear stream flags; we use underlying streambuf I/O, do not
2226  // maintain ifstream flags, except eof
2227  is.clear(is.rdstate() & std::ios::eofbit);
2228  }
2229 
2230  explicit input_stream_adapter(std::istream& i)
2231  : is(i), sb(*i.rdbuf())
2232  {}
2233 
2234  // delete because of pointer members
2235  input_stream_adapter(const input_stream_adapter&) = delete;
2236  input_stream_adapter& operator=(input_stream_adapter&) = delete;
2237  input_stream_adapter(input_stream_adapter&&) = delete;
2238  input_stream_adapter& operator=(input_stream_adapter&&) = delete;
2239 
2240  // std::istream/std::streambuf use std::char_traits<char>::to_int_type, to
2241  // ensure that std::char_traits<char>::eof() and the character 0xFF do not
2242  // end up as the same value, eg. 0xFFFFFFFF.
2243  std::char_traits<char>::int_type get_character() override
2244  {
2245  auto res = sb.sbumpc();
2246  // set eof manually, as we don't use the istream interface.
2247  if (res == EOF)
2248  {
2249  is.clear(is.rdstate() | std::ios::eofbit);
2250  }
2251  return res;
2252  }
2253 
2254  private:
2255  /// the associated input stream
2256  std::istream& is;
2257  std::streambuf& sb;
2258 };
2259 
2260 /// input adapter for buffer input
2261 class input_buffer_adapter : public input_adapter_protocol
2262 {
2263  public:
2264  input_buffer_adapter(const char* b, const std::size_t l) noexcept
2265  : cursor(b), limit(b + l)
2266  {}
2267 
2268  // delete because of pointer members
2269  input_buffer_adapter(const input_buffer_adapter&) = delete;
2270  input_buffer_adapter& operator=(input_buffer_adapter&) = delete;
2271  input_buffer_adapter(input_buffer_adapter&&) = delete;
2272  input_buffer_adapter& operator=(input_buffer_adapter&&) = delete;
2273  ~input_buffer_adapter() override = default;
2274 
2275  std::char_traits<char>::int_type get_character() noexcept override
2276  {
2277  if (JSON_LIKELY(cursor < limit))
2278  {
2279  return std::char_traits<char>::to_int_type(*(cursor++));
2280  }
2281 
2282  return std::char_traits<char>::eof();
2283  }
2284 
2285  private:
2286  /// pointer to the current character
2287  const char* cursor;
2288  /// pointer past the last character
2289  const char* const limit;
2290 };
2291 
2292 template<typename WideStringType, size_t T>
2293 struct wide_string_input_helper
2294 {
2295  // UTF-32
2296  static void fill_buffer(const WideStringType& str, size_t& current_wchar, std::array<std::char_traits<char>::int_type, 4>& utf8_bytes, size_t& utf8_bytes_index, size_t& utf8_bytes_filled)
2297  {
2298  utf8_bytes_index = 0;
2299 
2300  if (current_wchar == str.size())
2301  {
2302  utf8_bytes[0] = std::char_traits<char>::eof();
2303  utf8_bytes_filled = 1;
2304  }
2305  else
2306  {
2307  // get the current character
2308  const auto wc = static_cast<int>(str[current_wchar++]);
2309 
2310  // UTF-32 to UTF-8 encoding
2311  if (wc < 0x80)
2312  {
2313  utf8_bytes[0] = wc;
2314  utf8_bytes_filled = 1;
2315  }
2316  else if (wc <= 0x7FF)
2317  {
2318  utf8_bytes[0] = 0xC0 | ((wc >> 6) & 0x1F);
2319  utf8_bytes[1] = 0x80 | (wc & 0x3F);
2320  utf8_bytes_filled = 2;
2321  }
2322  else if (wc <= 0xFFFF)
2323  {
2324  utf8_bytes[0] = 0xE0 | ((wc >> 12) & 0x0F);
2325  utf8_bytes[1] = 0x80 | ((wc >> 6) & 0x3F);
2326  utf8_bytes[2] = 0x80 | (wc & 0x3F);
2327  utf8_bytes_filled = 3;
2328  }
2329  else if (wc <= 0x10FFFF)
2330  {
2331  utf8_bytes[0] = 0xF0 | ((wc >> 18) & 0x07);
2332  utf8_bytes[1] = 0x80 | ((wc >> 12) & 0x3F);
2333  utf8_bytes[2] = 0x80 | ((wc >> 6) & 0x3F);
2334  utf8_bytes[3] = 0x80 | (wc & 0x3F);
2335  utf8_bytes_filled = 4;
2336  }
2337  else
2338  {
2339  // unknown character
2340  utf8_bytes[0] = wc;
2341  utf8_bytes_filled = 1;
2342  }
2343  }
2344  }
2345 };
2346 
2347 template<typename WideStringType>
2348 struct wide_string_input_helper<WideStringType, 2>
2349 {
2350  // UTF-16
2351  static void fill_buffer(const WideStringType& str, size_t& current_wchar, std::array<std::char_traits<char>::int_type, 4>& utf8_bytes, size_t& utf8_bytes_index, size_t& utf8_bytes_filled)
2352  {
2353  utf8_bytes_index = 0;
2354 
2355  if (current_wchar == str.size())
2356  {
2357  utf8_bytes[0] = std::char_traits<char>::eof();
2358  utf8_bytes_filled = 1;
2359  }
2360  else
2361  {
2362  // get the current character
2363  const auto wc = static_cast<int>(str[current_wchar++]);
2364 
2365  // UTF-16 to UTF-8 encoding
2366  if (wc < 0x80)
2367  {
2368  utf8_bytes[0] = wc;
2369  utf8_bytes_filled = 1;
2370  }
2371  else if (wc <= 0x7FF)
2372  {
2373  utf8_bytes[0] = 0xC0 | ((wc >> 6));
2374  utf8_bytes[1] = 0x80 | (wc & 0x3F);
2375  utf8_bytes_filled = 2;
2376  }
2377  else if (0xD800 > wc or wc >= 0xE000)
2378  {
2379  utf8_bytes[0] = 0xE0 | ((wc >> 12));
2380  utf8_bytes[1] = 0x80 | ((wc >> 6) & 0x3F);
2381  utf8_bytes[2] = 0x80 | (wc & 0x3F);
2382  utf8_bytes_filled = 3;
2383  }
2384  else
2385  {
2386  if (current_wchar < str.size())
2387  {
2388  const auto wc2 = static_cast<int>(str[current_wchar++]);
2389  const int charcode = 0x10000 + (((wc & 0x3FF) << 10) | (wc2 & 0x3FF));
2390  utf8_bytes[0] = 0xf0 | (charcode >> 18);
2391  utf8_bytes[1] = 0x80 | ((charcode >> 12) & 0x3F);
2392  utf8_bytes[2] = 0x80 | ((charcode >> 6) & 0x3F);
2393  utf8_bytes[3] = 0x80 | (charcode & 0x3F);
2394  utf8_bytes_filled = 4;
2395  }
2396  else
2397  {
2398  // unknown character
2399  ++current_wchar;
2400  utf8_bytes[0] = wc;
2401  utf8_bytes_filled = 1;
2402  }
2403  }
2404  }
2405  }
2406 };
2407 
2408 template<typename WideStringType>
2409 class wide_string_input_adapter : public input_adapter_protocol
2410 {
2411  public:
2412  explicit wide_string_input_adapter(const WideStringType& w) noexcept
2413  : str(w)
2414  {}
2415 
2416  std::char_traits<char>::int_type get_character() noexcept override
2417  {
2418  // check if buffer needs to be filled
2419  if (utf8_bytes_index == utf8_bytes_filled)
2420  {
2421  fill_buffer<sizeof(typename WideStringType::value_type)>();
2422 
2423  assert(utf8_bytes_filled > 0);
2424  assert(utf8_bytes_index == 0);
2425  }
2426 
2427  // use buffer
2428  assert(utf8_bytes_filled > 0);
2429  assert(utf8_bytes_index < utf8_bytes_filled);
2430  return utf8_bytes[utf8_bytes_index++];
2431  }
2432 
2433  private:
2434  template<size_t T>
2435  void fill_buffer()
2436  {
2437  wide_string_input_helper<WideStringType, T>::fill_buffer(str, current_wchar, utf8_bytes, utf8_bytes_index, utf8_bytes_filled);
2438  }
2439 
2440  /// the wstring to process
2441  const WideStringType& str;
2442 
2443  /// index of the current wchar in str
2444  std::size_t current_wchar = 0;
2445 
2446  /// a buffer for UTF-8 bytes
2447  std::array<std::char_traits<char>::int_type, 4> utf8_bytes = {{0, 0, 0, 0}};
2448 
2449  /// index to the utf8_codes array for the next valid byte
2450  std::size_t utf8_bytes_index = 0;
2451  /// number of valid bytes in the utf8_codes array
2452  std::size_t utf8_bytes_filled = 0;
2453 };
2454 
2455 class input_adapter
2456 {
2457  public:
2458  // native support
2459  input_adapter(std::FILE* file)
2460  : ia(std::make_shared<file_input_adapter>(file)) {}
2461  /// input adapter for input stream
2462  input_adapter(std::istream& i)
2463  : ia(std::make_shared<input_stream_adapter>(i)) {}
2464 
2465  /// input adapter for input stream
2466  input_adapter(std::istream&& i)
2467  : ia(std::make_shared<input_stream_adapter>(i)) {}
2468 
2469  input_adapter(const std::wstring& ws)
2470  : ia(std::make_shared<wide_string_input_adapter<std::wstring>>(ws)) {}
2471 
2472  input_adapter(const std::u16string& ws)
2473  : ia(std::make_shared<wide_string_input_adapter<std::u16string>>(ws)) {}
2474 
2475  input_adapter(const std::u32string& ws)
2476  : ia(std::make_shared<wide_string_input_adapter<std::u32string>>(ws)) {}
2477 
2478  /// input adapter for buffer
2479  template<typename CharT,
2480  typename std::enable_if<
2481  std::is_pointer<CharT>::value and
2482  std::is_integral<typename std::remove_pointer<CharT>::type>::value and
2483  sizeof(typename std::remove_pointer<CharT>::type) == 1,
2484  int>::type = 0>
2485  input_adapter(CharT b, std::size_t l)
2486  : ia(std::make_shared<input_buffer_adapter>(reinterpret_cast<const char*>(b), l)) {}
2487 
2488  // derived support
2489 
2490  /// input adapter for string literal
2491  template<typename CharT,
2492  typename std::enable_if<
2493  std::is_pointer<CharT>::value and
2494  std::is_integral<typename std::remove_pointer<CharT>::type>::value and
2495  sizeof(typename std::remove_pointer<CharT>::type) == 1,
2496  int>::type = 0>
2497  input_adapter(CharT b)
2498  : input_adapter(reinterpret_cast<const char*>(b),
2499  std::strlen(reinterpret_cast<const char*>(b))) {}
2500 
2501  /// input adapter for iterator range with contiguous storage
2502  template<class IteratorType,
2503  typename std::enable_if<
2504  std::is_same<typename iterator_traits<IteratorType>::iterator_category, std::random_access_iterator_tag>::value,
2505  int>::type = 0>
2506  input_adapter(IteratorType first, IteratorType last)
2507  {
2508 #ifndef NDEBUG
2509  // assertion to check that the iterator range is indeed contiguous,
2510  // see http://stackoverflow.com/a/35008842/266378 for more discussion
2511  const auto is_contiguous = std::accumulate(
2512  first, last, std::pair<bool, int>(true, 0),
2513  [&first](std::pair<bool, int> res, decltype(*first) val)
2514  {
2515  res.first &= (val == *(std::next(std::addressof(*first), res.second++)));
2516  return res;
2517  }).first;
2518  assert(is_contiguous);
2519 #endif
2520 
2521  // assertion to check that each element is 1 byte long
2522  static_assert(
2523  sizeof(typename iterator_traits<IteratorType>::value_type) == 1,
2524  "each element in the iterator range must have the size of 1 byte");
2525 
2526  const auto len = static_cast<size_t>(std::distance(first, last));
2527  if (JSON_LIKELY(len > 0))
2528  {
2529  // there is at least one element: use the address of first
2530  ia = std::make_shared<input_buffer_adapter>(reinterpret_cast<const char*>(&(*first)), len);
2531  }
2532  else
2533  {
2534  // the address of first cannot be used: use nullptr
2535  ia = std::make_shared<input_buffer_adapter>(nullptr, len);
2536  }
2537  }
2538 
2539  /// input adapter for array
2540  template<class T, std::size_t N>
2541  input_adapter(T (&array)[N])
2542  : input_adapter(std::begin(array), std::end(array)) {}
2543 
2544  /// input adapter for contiguous container
2545  template<class ContiguousContainer, typename
2546  std::enable_if<not std::is_pointer<ContiguousContainer>::value and
2547  std::is_base_of<std::random_access_iterator_tag, typename iterator_traits<decltype(std::begin(std::declval<ContiguousContainer const>()))>::iterator_category>::value,
2548  int>::type = 0>
2549  input_adapter(const ContiguousContainer& c)
2550  : input_adapter(std::begin(c), std::end(c)) {}
2551 
2552  operator input_adapter_t()
2553  {
2554  return ia;
2555  }
2556 
2557  private:
2558  /// the actual adapter
2559  input_adapter_t ia = nullptr;
2560 };
2561 } // namespace detail
2562 } // namespace nlohmann
2563 
2564 // #include <nlohmann/detail/input/lexer.hpp>
2565 
2566 
2567 #include <clocale> // localeconv
2568 #include <cstddef> // size_t
2569 #include <cstdlib> // strtof, strtod, strtold, strtoll, strtoull
2570 #include <cstdio> // snprintf
2571 #include <initializer_list> // initializer_list
2572 #include <string> // char_traits, string
2573 #include <vector> // vector
2574 
2575 // #include <nlohmann/detail/macro_scope.hpp>
2576 
2577 // #include <nlohmann/detail/input/input_adapters.hpp>
2578 
2579 // #include <nlohmann/detail/input/position_t.hpp>
2580 
2581 
2582 namespace nlohmann
2583 {
2584 namespace detail
2585 {
2586 ///////////
2587 // lexer //
2588 ///////////
2589 
2590 /*!
2591 @brief lexical analysis
2592 
2593 This class organizes the lexical analysis during JSON deserialization.
2594 */
2595 template<typename BasicJsonType>
2596 class lexer
2597 {
2598  using number_integer_t = typename BasicJsonType::number_integer_t;
2599  using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
2600  using number_float_t = typename BasicJsonType::number_float_t;
2601  using string_t = typename BasicJsonType::string_t;
2602 
2603  public:
2604  /// token types for the parser
2605  enum class token_type
2606  {
2607  uninitialized, ///< indicating the scanner is uninitialized
2608  literal_true, ///< the `true` literal
2609  literal_false, ///< the `false` literal
2610  literal_null, ///< the `null` literal
2611  value_string, ///< a string -- use get_string() for actual value
2612  value_unsigned, ///< an unsigned integer -- use get_number_unsigned() for actual value
2613  value_integer, ///< a signed integer -- use get_number_integer() for actual value
2614  value_float, ///< an floating point number -- use get_number_float() for actual value
2615  begin_array, ///< the character for array begin `[`
2616  begin_object, ///< the character for object begin `{`
2617  end_array, ///< the character for array end `]`
2618  end_object, ///< the character for object end `}`
2619  name_separator, ///< the name separator `:`
2620  value_separator, ///< the value separator `,`
2621  parse_error, ///< indicating a parse error
2622  end_of_input, ///< indicating the end of the input buffer
2623  literal_or_value ///< a literal or the begin of a value (only for diagnostics)
2624  };
2625 
2626  /// return name of values of type token_type (only used for errors)
2627  static const char* token_type_name(const token_type t) noexcept
2628  {
2629  switch (t)
2630  {
2631  case token_type::uninitialized:
2632  return "<uninitialized>";
2633  case token_type::literal_true:
2634  return "true literal";
2635  case token_type::literal_false:
2636  return "false literal";
2637  case token_type::literal_null:
2638  return "null literal";
2639  case token_type::value_string:
2640  return "string literal";
2641  case lexer::token_type::value_unsigned:
2642  case lexer::token_type::value_integer:
2643  case lexer::token_type::value_float:
2644  return "number literal";
2645  case token_type::begin_array:
2646  return "'['";
2647  case token_type::begin_object:
2648  return "'{'";
2649  case token_type::end_array:
2650  return "']'";
2651  case token_type::end_object:
2652  return "'}'";
2653  case token_type::name_separator:
2654  return "':'";
2655  case token_type::value_separator:
2656  return "','";
2657  case token_type::parse_error:
2658  return "<parse error>";
2659  case token_type::end_of_input:
2660  return "end of input";
2661  case token_type::literal_or_value:
2662  return "'[', '{', or a literal";
2663  // LCOV_EXCL_START
2664  default: // catch non-enum values
2665  return "unknown token";
2666  // LCOV_EXCL_STOP
2667  }
2668  }
2669 
2670  explicit lexer(detail::input_adapter_t&& adapter)
2671  : ia(std::move(adapter)), decimal_point_char(get_decimal_point()) {}
2672 
2673  // delete because of pointer members
2674  lexer(const lexer&) = delete;
2675  lexer(lexer&&) = delete;
2676  lexer& operator=(lexer&) = delete;
2677  lexer& operator=(lexer&&) = delete;
2678  ~lexer() = default;
2679 
2680  private:
2681  /////////////////////
2682  // locales
2683  /////////////////////
2684 
2685  /// return the locale-dependent decimal point
2686  static char get_decimal_point() noexcept
2687  {
2688  const auto loc = localeconv();
2689  assert(loc != nullptr);
2690  return (loc->decimal_point == nullptr) ? '.' : *(loc->decimal_point);
2691  }
2692 
2693  /////////////////////
2694  // scan functions
2695  /////////////////////
2696 
2697  /*!
2698  @brief get codepoint from 4 hex characters following `\u`
2699 
2700  For input "\u c1 c2 c3 c4" the codepoint is:
2701  (c1 * 0x1000) + (c2 * 0x0100) + (c3 * 0x0010) + c4
2702  = (c1 << 12) + (c2 << 8) + (c3 << 4) + (c4 << 0)
2703 
2704  Furthermore, the possible characters '0'..'9', 'A'..'F', and 'a'..'f'
2705  must be converted to the integers 0x0..0x9, 0xA..0xF, 0xA..0xF, resp. The
2706  conversion is done by subtracting the offset (0x30, 0x37, and 0x57)
2707  between the ASCII value of the character and the desired integer value.
2708 
2709  @return codepoint (0x0000..0xFFFF) or -1 in case of an error (e.g. EOF or
2710  non-hex character)
2711  */
2712  int get_codepoint()
2713  {
2714  // this function only makes sense after reading `\u`
2715  assert(current == 'u');
2716  int codepoint = 0;
2717 
2718  const auto factors = { 12, 8, 4, 0 };
2719  for (const auto factor : factors)
2720  {
2721  get();
2722 
2723  if (current >= '0' and current <= '9')
2724  {
2725  codepoint += ((current - 0x30) << factor);
2726  }
2727  else if (current >= 'A' and current <= 'F')
2728  {
2729  codepoint += ((current - 0x37) << factor);
2730  }
2731  else if (current >= 'a' and current <= 'f')
2732  {
2733  codepoint += ((current - 0x57) << factor);
2734  }
2735  else
2736  {
2737  return -1;
2738  }
2739  }
2740 
2741  assert(0x0000 <= codepoint and codepoint <= 0xFFFF);
2742  return codepoint;
2743  }
2744 
2745  /*!
2746  @brief check if the next byte(s) are inside a given range
2747 
2748  Adds the current byte and, for each passed range, reads a new byte and
2749  checks if it is inside the range. If a violation was detected, set up an
2750  error message and return false. Otherwise, return true.
2751 
2752  @param[in] ranges list of integers; interpreted as list of pairs of
2753  inclusive lower and upper bound, respectively
2754 
2755  @pre The passed list @a ranges must have 2, 4, or 6 elements; that is,
2756  1, 2, or 3 pairs. This precondition is enforced by an assertion.
2757 
2758  @return true if and only if no range violation was detected
2759  */
2760  bool next_byte_in_range(std::initializer_list<int> ranges)
2761  {
2762  assert(ranges.size() == 2 or ranges.size() == 4 or ranges.size() == 6);
2763  add(current);
2764 
2765  for (auto range = ranges.begin(); range != ranges.end(); ++range)
2766  {
2767  get();
2768  if (JSON_LIKELY(*range <= current and current <= *(++range)))
2769  {
2770  add(current);
2771  }
2772  else
2773  {
2774  error_message = "invalid string: ill-formed UTF-8 byte";
2775  return false;
2776  }
2777  }
2778 
2779  return true;
2780  }
2781 
2782  /*!
2783  @brief scan a string literal
2784 
2785  This function scans a string according to Sect. 7 of RFC 7159. While
2786  scanning, bytes are escaped and copied into buffer token_buffer. Then the
2787  function returns successfully, token_buffer is *not* null-terminated (as it
2788  may contain \0 bytes), and token_buffer.size() is the number of bytes in the
2789  string.
2790 
2791  @return token_type::value_string if string could be successfully scanned,
2792  token_type::parse_error otherwise
2793 
2794  @note In case of errors, variable error_message contains a textual
2795  description.
2796  */
2797  token_type scan_string()
2798  {
2799  // reset token_buffer (ignore opening quote)
2800  reset();
2801 
2802  // we entered the function by reading an open quote
2803  assert(current == '\"');
2804 
2805  while (true)
2806  {
2807  // get next character
2808  switch (get())
2809  {
2810  // end of file while parsing string
2811  case std::char_traits<char>::eof():
2812  {
2813  error_message = "invalid string: missing closing quote";
2814  return token_type::parse_error;
2815  }
2816 
2817  // closing quote
2818  case '\"':
2819  {
2820  return token_type::value_string;
2821  }
2822 
2823  // escapes
2824  case '\\':
2825  {
2826  switch (get())
2827  {
2828  // quotation mark
2829  case '\"':
2830  add('\"');
2831  break;
2832  // reverse solidus
2833  case '\\':
2834  add('\\');
2835  break;
2836  // solidus
2837  case '/':
2838  add('/');
2839  break;
2840  // backspace
2841  case 'b':
2842  add('\b');
2843  break;
2844  // form feed
2845  case 'f':
2846  add('\f');
2847  break;
2848  // line feed
2849  case 'n':
2850  add('\n');
2851  break;
2852  // carriage return
2853  case 'r':
2854  add('\r');
2855  break;
2856  // tab
2857  case 't':
2858  add('\t');
2859  break;
2860 
2861  // unicode escapes
2862  case 'u':
2863  {
2864  const int codepoint1 = get_codepoint();
2865  int codepoint = codepoint1; // start with codepoint1
2866 
2867  if (JSON_UNLIKELY(codepoint1 == -1))
2868  {
2869  error_message = "invalid string: '\\u' must be followed by 4 hex digits";
2870  return token_type::parse_error;
2871  }
2872 
2873  // check if code point is a high surrogate
2874  if (0xD800 <= codepoint1 and codepoint1 <= 0xDBFF)
2875  {
2876  // expect next \uxxxx entry
2877  if (JSON_LIKELY(get() == '\\' and get() == 'u'))
2878  {
2879  const int codepoint2 = get_codepoint();
2880 
2881  if (JSON_UNLIKELY(codepoint2 == -1))
2882  {
2883  error_message = "invalid string: '\\u' must be followed by 4 hex digits";
2884  return token_type::parse_error;
2885  }
2886 
2887  // check if codepoint2 is a low surrogate
2888  if (JSON_LIKELY(0xDC00 <= codepoint2 and codepoint2 <= 0xDFFF))
2889  {
2890  // overwrite codepoint
2891  codepoint =
2892  // high surrogate occupies the most significant 22 bits
2893  (codepoint1 << 10)
2894  // low surrogate occupies the least significant 15 bits
2895  + codepoint2
2896  // there is still the 0xD800, 0xDC00 and 0x10000 noise
2897  // in the result so we have to subtract with:
2898  // (0xD800 << 10) + DC00 - 0x10000 = 0x35FDC00
2899  - 0x35FDC00;
2900  }
2901  else
2902  {
2903  error_message = "invalid string: surrogate U+DC00..U+DFFF must be followed by U+DC00..U+DFFF";
2904  return token_type::parse_error;
2905  }
2906  }
2907  else
2908  {
2909  error_message = "invalid string: surrogate U+DC00..U+DFFF must be followed by U+DC00..U+DFFF";
2910  return token_type::parse_error;
2911  }
2912  }
2913  else
2914  {
2915  if (JSON_UNLIKELY(0xDC00 <= codepoint1 and codepoint1 <= 0xDFFF))
2916  {
2917  error_message = "invalid string: surrogate U+DC00..U+DFFF must follow U+D800..U+DBFF";
2918  return token_type::parse_error;
2919  }
2920  }
2921 
2922  // result of the above calculation yields a proper codepoint
2923  assert(0x00 <= codepoint and codepoint <= 0x10FFFF);
2924 
2925  // translate codepoint into bytes
2926  if (codepoint < 0x80)
2927  {
2928  // 1-byte characters: 0xxxxxxx (ASCII)
2929  add(codepoint);
2930  }
2931  else if (codepoint <= 0x7FF)
2932  {
2933  // 2-byte characters: 110xxxxx 10xxxxxx
2934  add(0xC0 | (codepoint >> 6));
2935  add(0x80 | (codepoint & 0x3F));
2936  }
2937  else if (codepoint <= 0xFFFF)
2938  {
2939  // 3-byte characters: 1110xxxx 10xxxxxx 10xxxxxx
2940  add(0xE0 | (codepoint >> 12));
2941  add(0x80 | ((codepoint >> 6) & 0x3F));
2942  add(0x80 | (codepoint & 0x3F));
2943  }
2944  else
2945  {
2946  // 4-byte characters: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
2947  add(0xF0 | (codepoint >> 18));
2948  add(0x80 | ((codepoint >> 12) & 0x3F));
2949  add(0x80 | ((codepoint >> 6) & 0x3F));
2950  add(0x80 | (codepoint & 0x3F));
2951  }
2952 
2953  break;
2954  }
2955 
2956  // other characters after escape
2957  default:
2958  error_message = "invalid string: forbidden character after backslash";
2959  return token_type::parse_error;
2960  }
2961 
2962  break;
2963  }
2964 
2965  // invalid control characters
2966  case 0x00:
2967  {
2968  error_message = "invalid string: control character U+0000 (NUL) must be escaped to \\u0000";
2969  return token_type::parse_error;
2970  }
2971 
2972  case 0x01:
2973  {
2974  error_message = "invalid string: control character U+0001 (SOH) must be escaped to \\u0001";
2975  return token_type::parse_error;
2976  }
2977 
2978  case 0x02:
2979  {
2980  error_message = "invalid string: control character U+0002 (STX) must be escaped to \\u0002";
2981  return token_type::parse_error;
2982  }
2983 
2984  case 0x03:
2985  {
2986  error_message = "invalid string: control character U+0003 (ETX) must be escaped to \\u0003";
2987  return token_type::parse_error;
2988  }
2989 
2990  case 0x04:
2991  {
2992  error_message = "invalid string: control character U+0004 (EOT) must be escaped to \\u0004";
2993  return token_type::parse_error;
2994  }
2995 
2996  case 0x05:
2997  {
2998  error_message = "invalid string: control character U+0005 (ENQ) must be escaped to \\u0005";
2999  return token_type::parse_error;
3000  }
3001 
3002  case 0x06:
3003  {
3004  error_message = "invalid string: control character U+0006 (ACK) must be escaped to \\u0006";
3005  return token_type::parse_error;
3006  }
3007 
3008  case 0x07:
3009  {
3010  error_message = "invalid string: control character U+0007 (BEL) must be escaped to \\u0007";
3011  return token_type::parse_error;
3012  }
3013 
3014  case 0x08:
3015  {
3016  error_message = "invalid string: control character U+0008 (BS) must be escaped to \\u0008 or \\b";
3017  return token_type::parse_error;
3018  }
3019 
3020  case 0x09:
3021  {
3022  error_message = "invalid string: control character U+0009 (HT) must be escaped to \\u0009 or \\t";
3023  return token_type::parse_error;
3024  }
3025 
3026  case 0x0A:
3027  {
3028  error_message = "invalid string: control character U+000A (LF) must be escaped to \\u000A or \\n";
3029  return token_type::parse_error;
3030  }
3031 
3032  case 0x0B:
3033  {
3034  error_message = "invalid string: control character U+000B (VT) must be escaped to \\u000B";
3035  return token_type::parse_error;
3036  }
3037 
3038  case 0x0C:
3039  {
3040  error_message = "invalid string: control character U+000C (FF) must be escaped to \\u000C or \\f";
3041  return token_type::parse_error;
3042  }
3043 
3044  case 0x0D:
3045  {
3046  error_message = "invalid string: control character U+000D (CR) must be escaped to \\u000D or \\r";
3047  return token_type::parse_error;
3048  }
3049 
3050  case 0x0E:
3051  {
3052  error_message = "invalid string: control character U+000E (SO) must be escaped to \\u000E";
3053  return token_type::parse_error;
3054  }
3055 
3056  case 0x0F:
3057  {
3058  error_message = "invalid string: control character U+000F (SI) must be escaped to \\u000F";
3059  return token_type::parse_error;
3060  }
3061 
3062  case 0x10:
3063  {
3064  error_message = "invalid string: control character U+0010 (DLE) must be escaped to \\u0010";
3065  return token_type::parse_error;
3066  }
3067 
3068  case 0x11:
3069  {
3070  error_message = "invalid string: control character U+0011 (DC1) must be escaped to \\u0011";
3071  return token_type::parse_error;
3072  }
3073 
3074  case 0x12:
3075  {
3076  error_message = "invalid string: control character U+0012 (DC2) must be escaped to \\u0012";
3077  return token_type::parse_error;
3078  }
3079 
3080  case 0x13:
3081  {
3082  error_message = "invalid string: control character U+0013 (DC3) must be escaped to \\u0013";
3083  return token_type::parse_error;
3084  }
3085 
3086  case 0x14:
3087  {
3088  error_message = "invalid string: control character U+0014 (DC4) must be escaped to \\u0014";
3089  return token_type::parse_error;
3090  }
3091 
3092  case 0x15:
3093  {
3094  error_message = "invalid string: control character U+0015 (NAK) must be escaped to \\u0015";
3095  return token_type::parse_error;
3096  }
3097 
3098  case 0x16:
3099  {
3100  error_message = "invalid string: control character U+0016 (SYN) must be escaped to \\u0016";
3101  return token_type::parse_error;
3102  }
3103 
3104  case 0x17:
3105  {
3106  error_message = "invalid string: control character U+0017 (ETB) must be escaped to \\u0017";
3107  return token_type::parse_error;
3108  }
3109 
3110  case 0x18:
3111  {
3112  error_message = "invalid string: control character U+0018 (CAN) must be escaped to \\u0018";
3113  return token_type::parse_error;
3114  }
3115 
3116  case 0x19:
3117  {
3118  error_message = "invalid string: control character U+0019 (EM) must be escaped to \\u0019";
3119  return token_type::parse_error;
3120  }
3121 
3122  case 0x1A:
3123  {
3124  error_message = "invalid string: control character U+001A (SUB) must be escaped to \\u001A";
3125  return token_type::parse_error;
3126  }
3127 
3128  case 0x1B:
3129  {
3130  error_message = "invalid string: control character U+001B (ESC) must be escaped to \\u001B";
3131  return token_type::parse_error;
3132  }
3133 
3134  case 0x1C:
3135  {
3136  error_message = "invalid string: control character U+001C (FS) must be escaped to \\u001C";
3137  return token_type::parse_error;
3138  }
3139 
3140  case 0x1D:
3141  {
3142  error_message = "invalid string: control character U+001D (GS) must be escaped to \\u001D";
3143  return token_type::parse_error;
3144  }
3145 
3146  case 0x1E:
3147  {
3148  error_message = "invalid string: control character U+001E (RS) must be escaped to \\u001E";
3149  return token_type::parse_error;
3150  }
3151 
3152  case 0x1F:
3153  {
3154  error_message = "invalid string: control character U+001F (US) must be escaped to \\u001F";
3155  return token_type::parse_error;
3156  }
3157 
3158  // U+0020..U+007F (except U+0022 (quote) and U+005C (backspace))
3159  case 0x20:
3160  case 0x21:
3161  case 0x23:
3162  case 0x24:
3163  case 0x25:
3164  case 0x26:
3165  case 0x27:
3166  case 0x28:
3167  case 0x29:
3168  case 0x2A:
3169  case 0x2B:
3170  case 0x2C:
3171  case 0x2D:
3172  case 0x2E:
3173  case 0x2F:
3174  case 0x30:
3175  case 0x31:
3176  case 0x32:
3177  case 0x33:
3178  case 0x34:
3179  case 0x35:
3180  case 0x36:
3181  case 0x37:
3182  case 0x38:
3183  case 0x39:
3184  case 0x3A:
3185  case 0x3B:
3186  case 0x3C:
3187  case 0x3D:
3188  case 0x3E:
3189  case 0x3F:
3190  case 0x40:
3191  case 0x41:
3192  case 0x42:
3193  case 0x43:
3194  case 0x44:
3195  case 0x45:
3196  case 0x46:
3197  case 0x47:
3198  case 0x48:
3199  case 0x49:
3200  case 0x4A:
3201  case 0x4B:
3202  case 0x4C:
3203  case 0x4D:
3204  case 0x4E:
3205  case 0x4F:
3206  case 0x50:
3207  case 0x51:
3208  case 0x52:
3209  case 0x53:
3210  case 0x54:
3211  case 0x55:
3212  case 0x56:
3213  case 0x57:
3214  case 0x58:
3215  case 0x59:
3216  case 0x5A:
3217  case 0x5B:
3218  case 0x5D:
3219  case 0x5E:
3220  case 0x5F:
3221  case 0x60:
3222  case 0x61:
3223  case 0x62:
3224  case 0x63:
3225  case 0x64:
3226  case 0x65:
3227  case 0x66:
3228  case 0x67:
3229  case 0x68:
3230  case 0x69:
3231  case 0x6A:
3232  case 0x6B:
3233  case 0x6C:
3234  case 0x6D:
3235  case 0x6E:
3236  case 0x6F:
3237  case 0x70:
3238  case 0x71:
3239  case 0x72:
3240  case 0x73:
3241  case 0x74:
3242  case 0x75:
3243  case 0x76:
3244  case 0x77:
3245  case 0x78:
3246  case 0x79:
3247  case 0x7A:
3248  case 0x7B:
3249  case 0x7C:
3250  case 0x7D:
3251  case 0x7E:
3252  case 0x7F:
3253  {
3254  add(current);
3255  break;
3256  }
3257 
3258  // U+0080..U+07FF: bytes C2..DF 80..BF
3259  case 0xC2:
3260  case 0xC3:
3261  case 0xC4:
3262  case 0xC5:
3263  case 0xC6:
3264  case 0xC7:
3265  case 0xC8:
3266  case 0xC9:
3267  case 0xCA:
3268  case 0xCB:
3269  case 0xCC:
3270  case 0xCD:
3271  case 0xCE:
3272  case 0xCF:
3273  case 0xD0:
3274  case 0xD1:
3275  case 0xD2:
3276  case 0xD3:
3277  case 0xD4:
3278  case 0xD5:
3279  case 0xD6:
3280  case 0xD7:
3281  case 0xD8:
3282  case 0xD9:
3283  case 0xDA:
3284  case 0xDB:
3285  case 0xDC:
3286  case 0xDD:
3287  case 0xDE:
3288  case 0xDF:
3289  {
3290  if (JSON_UNLIKELY(not next_byte_in_range({0x80, 0xBF})))
3291  {
3292  return token_type::parse_error;
3293  }
3294  break;
3295  }
3296 
3297  // U+0800..U+0FFF: bytes E0 A0..BF 80..BF
3298  case 0xE0:
3299  {
3300  if (JSON_UNLIKELY(not (next_byte_in_range({0xA0, 0xBF, 0x80, 0xBF}))))
3301  {
3302  return token_type::parse_error;
3303  }
3304  break;
3305  }
3306 
3307  // U+1000..U+CFFF: bytes E1..EC 80..BF 80..BF
3308  // U+E000..U+FFFF: bytes EE..EF 80..BF 80..BF
3309  case 0xE1:
3310  case 0xE2:
3311  case 0xE3:
3312  case 0xE4:
3313  case 0xE5:
3314  case 0xE6:
3315  case 0xE7:
3316  case 0xE8:
3317  case 0xE9:
3318  case 0xEA:
3319  case 0xEB:
3320  case 0xEC:
3321  case 0xEE:
3322  case 0xEF:
3323  {
3324  if (JSON_UNLIKELY(not (next_byte_in_range({0x80, 0xBF, 0x80, 0xBF}))))
3325  {
3326  return token_type::parse_error;
3327  }
3328  break;
3329  }
3330 
3331  // U+D000..U+D7FF: bytes ED 80..9F 80..BF
3332  case 0xED:
3333  {
3334  if (JSON_UNLIKELY(not (next_byte_in_range({0x80, 0x9F, 0x80, 0xBF}))))
3335  {
3336  return token_type::parse_error;
3337  }
3338  break;
3339  }
3340 
3341  // U+10000..U+3FFFF F0 90..BF 80..BF 80..BF
3342  case 0xF0:
3343  {
3344  if (JSON_UNLIKELY(not (next_byte_in_range({0x90, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
3345  {
3346  return token_type::parse_error;
3347  }
3348  break;
3349  }
3350 
3351  // U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF
3352  case 0xF1:
3353  case 0xF2:
3354  case 0xF3:
3355  {
3356  if (JSON_UNLIKELY(not (next_byte_in_range({0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF}))))
3357  {
3358  return token_type::parse_error;
3359  }
3360  break;
3361  }
3362 
3363  // U+100000..U+10FFFF F4 80..8F 80..BF 80..BF
3364  case 0xF4:
3365  {
3366  if (JSON_UNLIKELY(not (next_byte_in_range({0x80, 0x8F, 0x80, 0xBF, 0x80, 0xBF}))))
3367  {
3368  return token_type::parse_error;
3369  }
3370  break;
3371  }
3372 
3373  // remaining bytes (80..C1 and F5..FF) are ill-formed
3374  default:
3375  {
3376  error_message = "invalid string: ill-formed UTF-8 byte";
3377  return token_type::parse_error;
3378  }
3379  }
3380  }
3381  }
3382 
3383  static void strtof(float& f, const char* str, char** endptr) noexcept
3384  {
3385  f = std::strtof(str, endptr);
3386  }
3387 
3388  static void strtof(double& f, const char* str, char** endptr) noexcept
3389  {
3390  f = std::strtod(str, endptr);
3391  }
3392 
3393  static void strtof(long double& f, const char* str, char** endptr) noexcept
3394  {
3395  f = std::strtold(str, endptr);
3396  }
3397 
3398  /*!
3399  @brief scan a number literal
3400 
3401  This function scans a string according to Sect. 6 of RFC 7159.
3402 
3403  The function is realized with a deterministic finite state machine derived
3404  from the grammar described in RFC 7159. Starting in state "init", the
3405  input is read and used to determined the next state. Only state "done"
3406  accepts the number. State "error" is a trap state to model errors. In the
3407  table below, "anything" means any character but the ones listed before.
3408 
3409  state | 0 | 1-9 | e E | + | - | . | anything
3410  ---------|----------|----------|----------|---------|---------|----------|-----------
3411  init | zero | any1 | [error] | [error] | minus | [error] | [error]
3412  minus | zero | any1 | [error] | [error] | [error] | [error] | [error]
3413  zero | done | done | exponent | done | done | decimal1 | done
3414  any1 | any1 | any1 | exponent | done | done | decimal1 | done
3415  decimal1 | decimal2 | [error] | [error] | [error] | [error] | [error] | [error]
3416  decimal2 | decimal2 | decimal2 | exponent | done | done | done | done
3417  exponent | any2 | any2 | [error] | sign | sign | [error] | [error]
3418  sign | any2 | any2 | [error] | [error] | [error] | [error] | [error]
3419  any2 | any2 | any2 | done | done | done | done | done
3420 
3421  The state machine is realized with one label per state (prefixed with
3422  "scan_number_") and `goto` statements between them. The state machine
3423  contains cycles, but any cycle can be left when EOF is read. Therefore,
3424  the function is guaranteed to terminate.
3425 
3426  During scanning, the read bytes are stored in token_buffer. This string is
3427  then converted to a signed integer, an unsigned integer, or a
3428  floating-point number.
3429 
3430  @return token_type::value_unsigned, token_type::value_integer, or
3431  token_type::value_float if number could be successfully scanned,
3432  token_type::parse_error otherwise
3433 
3434  @note The scanner is independent of the current locale. Internally, the
3435  locale's decimal point is used instead of `.` to work with the
3436  locale-dependent converters.
3437  */
3438  token_type scan_number() // lgtm [cpp/use-of-goto]
3439  {
3440  // reset token_buffer to store the number's bytes
3441  reset();
3442 
3443  // the type of the parsed number; initially set to unsigned; will be
3444  // changed if minus sign, decimal point or exponent is read
3445  token_type number_type = token_type::value_unsigned;
3446 
3447  // state (init): we just found out we need to scan a number
3448  switch (current)
3449  {
3450  case '-':
3451  {
3452  add(current);
3453  goto scan_number_minus;
3454  }
3455 
3456  case '0':
3457  {
3458  add(current);
3459  goto scan_number_zero;
3460  }
3461 
3462  case '1':
3463  case '2':
3464  case '3':
3465  case '4':
3466  case '5':
3467  case '6':
3468  case '7':
3469  case '8':
3470  case '9':
3471  {
3472  add(current);
3473  goto scan_number_any1;
3474  }
3475 
3476  // LCOV_EXCL_START
3477  default:
3478  {
3479  // all other characters are rejected outside scan_number()
3480  assert(false);
3481  }
3482  // LCOV_EXCL_STOP
3483  }
3484 
3485 scan_number_minus:
3486  // state: we just parsed a leading minus sign
3487  number_type = token_type::value_integer;
3488  switch (get())
3489  {
3490  case '0':
3491  {
3492  add(current);
3493  goto scan_number_zero;
3494  }
3495 
3496  case '1':
3497  case '2':
3498  case '3':
3499  case '4':
3500  case '5':
3501  case '6':
3502  case '7':
3503  case '8':
3504  case '9':
3505  {
3506  add(current);
3507  goto scan_number_any1;
3508  }
3509 
3510  default:
3511  {
3512  error_message = "invalid number; expected digit after '-'";
3513  return token_type::parse_error;
3514  }
3515  }
3516 
3517 scan_number_zero:
3518  // state: we just parse a zero (maybe with a leading minus sign)
3519  switch (get())
3520  {
3521  case '.':
3522  {
3523  add(decimal_point_char);
3524  goto scan_number_decimal1;
3525  }
3526 
3527  case 'e':
3528  case 'E':
3529  {
3530  add(current);
3531  goto scan_number_exponent;
3532  }
3533 
3534  default:
3535  goto scan_number_done;
3536  }
3537 
3538 scan_number_any1:
3539  // state: we just parsed a number 0-9 (maybe with a leading minus sign)
3540  switch (get())
3541  {
3542  case '0':
3543  case '1':
3544  case '2':
3545  case '3':
3546  case '4':
3547  case '5':
3548  case '6':
3549  case '7':
3550  case '8':
3551  case '9':
3552  {
3553  add(current);
3554  goto scan_number_any1;
3555  }
3556 
3557  case '.':
3558  {
3559  add(decimal_point_char);
3560  goto scan_number_decimal1;
3561  }
3562 
3563  case 'e':
3564  case 'E':
3565  {
3566  add(current);
3567  goto scan_number_exponent;
3568  }
3569 
3570  default:
3571  goto scan_number_done;
3572  }
3573 
3574 scan_number_decimal1:
3575  // state: we just parsed a decimal point
3576  number_type = token_type::value_float;
3577  switch (get())
3578  {
3579  case '0':
3580  case '1':
3581  case '2':
3582  case '3':
3583  case '4':
3584  case '5':
3585  case '6':
3586  case '7':
3587  case '8':
3588  case '9':
3589  {
3590  add(current);
3591  goto scan_number_decimal2;
3592  }
3593 
3594  default:
3595  {
3596  error_message = "invalid number; expected digit after '.'";
3597  return token_type::parse_error;
3598  }
3599  }
3600 
3601 scan_number_decimal2:
3602  // we just parsed at least one number after a decimal point
3603  switch (get())
3604  {
3605  case '0':
3606  case '1':
3607  case '2':
3608  case '3':
3609  case '4':
3610  case '5':
3611  case '6':
3612  case '7':
3613  case '8':
3614  case '9':
3615  {
3616  add(current);
3617  goto scan_number_decimal2;
3618  }
3619 
3620  case 'e':
3621  case 'E':
3622  {
3623  add(current);
3624  goto scan_number_exponent;
3625  }
3626 
3627  default:
3628  goto scan_number_done;
3629  }
3630 
3631 scan_number_exponent:
3632  // we just parsed an exponent
3633  number_type = token_type::value_float;
3634  switch (get())
3635  {
3636  case '+':
3637  case '-':
3638  {
3639  add(current);
3640  goto scan_number_sign;
3641  }
3642 
3643  case '0':
3644  case '1':
3645  case '2':
3646  case '3':
3647  case '4':
3648  case '5':
3649  case '6':
3650  case '7':
3651  case '8':
3652  case '9':
3653  {
3654  add(current);
3655  goto scan_number_any2;
3656  }
3657 
3658  default:
3659  {
3660  error_message =
3661  "invalid number; expected '+', '-', or digit after exponent";
3662  return token_type::parse_error;
3663  }
3664  }
3665 
3666 scan_number_sign:
3667  // we just parsed an exponent sign
3668  switch (get())
3669  {
3670  case '0':
3671  case '1':
3672  case '2':
3673  case '3':
3674  case '4':
3675  case '5':
3676  case '6':
3677  case '7':
3678  case '8':
3679  case '9':
3680  {
3681  add(current);
3682  goto scan_number_any2;
3683  }
3684 
3685  default:
3686  {
3687  error_message = "invalid number; expected digit after exponent sign";
3688  return token_type::parse_error;
3689  }
3690  }
3691 
3692 scan_number_any2:
3693  // we just parsed a number after the exponent or exponent sign
3694  switch (get())
3695  {
3696  case '0':
3697  case '1':
3698  case '2':
3699  case '3':
3700  case '4':
3701  case '5':
3702  case '6':
3703  case '7':
3704  case '8':
3705  case '9':
3706  {
3707  add(current);
3708  goto scan_number_any2;
3709  }
3710 
3711  default:
3712  goto scan_number_done;
3713  }
3714 
3715 scan_number_done:
3716  // unget the character after the number (we only read it to know that
3717  // we are done scanning a number)
3718  unget();
3719 
3720  char* endptr = nullptr;
3721  errno = 0;
3722 
3723  // try to parse integers first and fall back to floats
3724  if (number_type == token_type::value_unsigned)
3725  {
3726  const auto x = std::strtoull(token_buffer.data(), &endptr, 10);
3727 
3728  // we checked the number format before
3729  assert(endptr == token_buffer.data() + token_buffer.size());
3730 
3731  if (errno == 0)
3732  {
3733  value_unsigned = static_cast<number_unsigned_t>(x);
3734  if (value_unsigned == x)
3735  {
3736  return token_type::value_unsigned;
3737  }
3738  }
3739  }
3740  else if (number_type == token_type::value_integer)
3741  {
3742  const auto x = std::strtoll(token_buffer.data(), &endptr, 10);
3743 
3744  // we checked the number format before
3745  assert(endptr == token_buffer.data() + token_buffer.size());
3746 
3747  if (errno == 0)
3748  {
3749  value_integer = static_cast<number_integer_t>(x);
3750  if (value_integer == x)
3751  {
3752  return token_type::value_integer;
3753  }
3754  }
3755  }
3756 
3757  // this code is reached if we parse a floating-point number or if an
3758  // integer conversion above failed
3759  strtof(value_float, token_buffer.data(), &endptr);
3760 
3761  // we checked the number format before
3762  assert(endptr == token_buffer.data() + token_buffer.size());
3763 
3764  return token_type::value_float;
3765  }
3766 
3767  /*!
3768  @param[in] literal_text the literal text to expect
3769  @param[in] length the length of the passed literal text
3770  @param[in] return_type the token type to return on success
3771  */
3772  token_type scan_literal(const char* literal_text, const std::size_t length,
3773  token_type return_type)
3774  {
3775  assert(current == literal_text[0]);
3776  for (std::size_t i = 1; i < length; ++i)
3777  {
3778  if (JSON_UNLIKELY(get() != literal_text[i]))
3779  {
3780  error_message = "invalid literal";
3781  return token_type::parse_error;
3782  }
3783  }
3784  return return_type;
3785  }
3786 
3787  /////////////////////
3788  // input management
3789  /////////////////////
3790 
3791  /// reset token_buffer; current character is beginning of token
3792  void reset() noexcept
3793  {
3794  token_buffer.clear();
3795  token_string.clear();
3796  token_string.push_back(std::char_traits<char>::to_char_type(current));
3797  }
3798 
3799  /*
3800  @brief get next character from the input
3801 
3802  This function provides the interface to the used input adapter. It does
3803  not throw in case the input reached EOF, but returns a
3804  `std::char_traits<char>::eof()` in that case. Stores the scanned characters
3805  for use in error messages.
3806 
3807  @return character read from the input
3808  */
3809  std::char_traits<char>::int_type get()
3810  {
3811  ++position.chars_read_total;
3812  ++position.chars_read_current_line;
3813 
3814  if (next_unget)
3815  {
3816  // just reset the next_unget variable and work with current
3817  next_unget = false;
3818  }
3819  else
3820  {
3821  current = ia->get_character();
3822  }
3823 
3824  if (JSON_LIKELY(current != std::char_traits<char>::eof()))
3825  {
3826  token_string.push_back(std::char_traits<char>::to_char_type(current));
3827  }
3828 
3829  if (current == '\n')
3830  {
3831  ++position.lines_read;
3832  ++position.chars_read_current_line = 0;
3833  }
3834 
3835  return current;
3836  }
3837 
3838  /*!
3839  @brief unget current character (read it again on next get)
3840 
3841  We implement unget by setting variable next_unget to true. The input is not
3842  changed - we just simulate ungetting by modifying chars_read_total,
3843  chars_read_current_line, and token_string. The next call to get() will
3844  behave as if the unget character is read again.
3845  */
3846  void unget()
3847  {
3848  next_unget = true;
3849 
3850  --position.chars_read_total;
3851 
3852  // in case we "unget" a newline, we have to also decrement the lines_read
3853  if (position.chars_read_current_line == 0)
3854  {
3855  if (position.lines_read > 0)
3856  {
3857  --position.lines_read;
3858  }
3859  }
3860  else
3861  {
3862  --position.chars_read_current_line;
3863  }
3864 
3865  if (JSON_LIKELY(current != std::char_traits<char>::eof()))
3866  {
3867  assert(token_string.size() != 0);
3868  token_string.pop_back();
3869  }
3870  }
3871 
3872  /// add a character to token_buffer
3873  void add(int c)
3874  {
3875  token_buffer.push_back(std::char_traits<char>::to_char_type(c));
3876  }
3877 
3878  public:
3879  /////////////////////
3880  // value getters
3881  /////////////////////
3882 
3883  /// return integer value
3884  constexpr number_integer_t get_number_integer() const noexcept
3885  {
3886  return value_integer;
3887  }
3888 
3889  /// return unsigned integer value
3890  constexpr number_unsigned_t get_number_unsigned() const noexcept
3891  {
3892  return value_unsigned;
3893  }
3894 
3895  /// return floating-point value
3896  constexpr number_float_t get_number_float() const noexcept
3897  {
3898  return value_float;
3899  }
3900 
3901  /// return current string value (implicitly resets the token; useful only once)
3902  string_t& get_string()
3903  {
3904  return token_buffer;
3905  }
3906 
3907  /////////////////////
3908  // diagnostics
3909  /////////////////////
3910 
3911  /// return position of last read token
3912  constexpr position_t get_position() const noexcept
3913  {
3914  return position;
3915  }
3916 
3917  /// return the last read token (for errors only). Will never contain EOF
3918  /// (an arbitrary value that is not a valid char value, often -1), because
3919  /// 255 may legitimately occur. May contain NUL, which should be escaped.
3920  std::string get_token_string() const
3921  {
3922  // escape control characters
3923  std::string result;
3924  for (const auto c : token_string)
3925  {
3926  if ('\x00' <= c and c <= '\x1F')
3927  {
3928  // escape control characters
3929  char cs[9];
3930  (std::snprintf)(cs, 9, "<U+%.4X>", static_cast<unsigned char>(c));
3931  result += cs;
3932  }
3933  else
3934  {
3935  // add character as is
3936  result.push_back(c);
3937  }
3938  }
3939 
3940  return result;
3941  }
3942 
3943  /// return syntax error message
3944  constexpr const char* get_error_message() const noexcept
3945  {
3946  return error_message;
3947  }
3948 
3949  /////////////////////
3950  // actual scanner
3951  /////////////////////
3952 
3953  /*!
3954  @brief skip the UTF-8 byte order mark
3955  @return true iff there is no BOM or the correct BOM has been skipped
3956  */
3957  bool skip_bom()
3958  {
3959  if (get() == 0xEF)
3960  {
3961  // check if we completely parse the BOM
3962  return get() == 0xBB and get() == 0xBF;
3963  }
3964 
3965  // the first character is not the beginning of the BOM; unget it to
3966  // process is later
3967  unget();
3968  return true;
3969  }
3970 
3971  token_type scan()
3972  {
3973  // initially, skip the BOM
3974  if (position.chars_read_total == 0 and not skip_bom())
3975  {
3976  error_message = "invalid BOM; must be 0xEF 0xBB 0xBF if given";
3977  return token_type::parse_error;
3978  }
3979 
3980  // read next character and ignore whitespace
3981  do
3982  {
3983  get();
3984  }
3985  while (current == ' ' or current == '\t' or current == '\n' or current == '\r');
3986 
3987  switch (current)
3988  {
3989  // structural characters
3990  case '[':
3991  return token_type::begin_array;
3992  case ']':
3993  return token_type::end_array;
3994  case '{':
3995  return token_type::begin_object;
3996  case '}':
3997  return token_type::end_object;
3998  case ':':
3999  return token_type::name_separator;
4000  case ',':
4001  return token_type::value_separator;
4002 
4003  // literals
4004  case 't':
4005  return scan_literal("true", 4, token_type::literal_true);
4006  case 'f':
4007  return scan_literal("false", 5, token_type::literal_false);
4008  case 'n':
4009  return scan_literal("null", 4, token_type::literal_null);
4010 
4011  // string
4012  case '\"':
4013  return scan_string();
4014 
4015  // number
4016  case '-':
4017  case '0':
4018  case '1':
4019  case '2':
4020  case '3':
4021  case '4':
4022  case '5':
4023  case '6':
4024  case '7':
4025  case '8':
4026  case '9':
4027  return scan_number();
4028 
4029  // end of input (the null byte is needed when parsing from
4030  // string literals)
4031  case '\0':
4032  case std::char_traits<char>::eof():
4033  return token_type::end_of_input;
4034 
4035  // error
4036  default:
4037  error_message = "invalid literal";
4038  return token_type::parse_error;
4039  }
4040  }
4041 
4042  private:
4043  /// input adapter
4044  detail::input_adapter_t ia = nullptr;
4045 
4046  /// the current character
4047  std::char_traits<char>::int_type current = std::char_traits<char>::eof();
4048 
4049  /// whether the next get() call should just return current
4050  bool next_unget = false;
4051 
4052  /// the start position of the current token
4053  position_t position;
4054 
4055  /// raw input token string (for error messages)
4056  std::vector<char> token_string {};
4057 
4058  /// buffer for variable-length tokens (numbers, strings)
4059  string_t token_buffer {};
4060 
4061  /// a description of occurred lexer errors
4062  const char* error_message = "";
4063 
4064  // number values
4065  number_integer_t value_integer = 0;
4066  number_unsigned_t value_unsigned = 0;
4067  number_float_t value_float = 0;
4068 
4069  /// the decimal point
4070  const char decimal_point_char = '.';
4071 };
4072 } // namespace detail
4073 } // namespace nlohmann
4074 
4075 // #include <nlohmann/detail/input/parser.hpp>
4076 
4077 
4078 #include <cassert> // assert
4079 #include <cmath> // isfinite
4080 #include <cstdint> // uint8_t
4081 #include <functional> // function
4082 #include <string> // string
4083 #include <utility> // move
4084 
4085 // #include <nlohmann/detail/exceptions.hpp>
4086 
4087 // #include <nlohmann/detail/macro_scope.hpp>
4088 
4089 // #include <nlohmann/detail/meta/is_sax.hpp>
4090 
4091 
4092 #include <cstdint> // size_t
4093 #include <utility> // declval
4094 
4095 // #include <nlohmann/detail/meta/detected.hpp>
4096 
4097 // #include <nlohmann/detail/meta/type_traits.hpp>
4098 
4099 
4100 namespace nlohmann
4101 {
4102 namespace detail
4103 {
4104 template <typename T>
4105 using null_function_t = decltype(std::declval<T&>().null());
4106 
4107 template <typename T>
4108 using boolean_function_t =
4109  decltype(std::declval<T&>().boolean(std::declval<bool>()));
4110 
4111 template <typename T, typename Integer>
4112 using number_integer_function_t =
4113  decltype(std::declval<T&>().number_integer(std::declval<Integer>()));
4114 
4115 template <typename T, typename Unsigned>
4116 using number_unsigned_function_t =
4117  decltype(std::declval<T&>().number_unsigned(std::declval<Unsigned>()));
4118 
4119 template <typename T, typename Float, typename String>
4120 using number_float_function_t = decltype(std::declval<T&>().number_float(
4121  std::declval<Float>(), std::declval<const String&>()));
4122 
4123 template <typename T, typename String>
4124 using string_function_t =
4125  decltype(std::declval<T&>().string(std::declval<String&>()));
4126 
4127 template <typename T>
4128 using start_object_function_t =
4129  decltype(std::declval<T&>().start_object(std::declval<std::size_t>()));
4130 
4131 template <typename T, typename String>
4132 using key_function_t =
4133  decltype(std::declval<T&>().key(std::declval<String&>()));
4134 
4135 template <typename T>
4136 using end_object_function_t = decltype(std::declval<T&>().end_object());
4137 
4138 template <typename T>
4139 using start_array_function_t =
4140  decltype(std::declval<T&>().start_array(std::declval<std::size_t>()));
4141 
4142 template <typename T>
4143 using end_array_function_t = decltype(std::declval<T&>().end_array());
4144 
4145 template <typename T, typename Exception>
4146 using parse_error_function_t = decltype(std::declval<T&>().parse_error(
4147  std::declval<std::size_t>(), std::declval<const std::string&>(),
4148  std::declval<const Exception&>()));
4149 
4150 template <typename SAX, typename BasicJsonType>
4151 struct is_sax
4152 {
4153  private:
4154  static_assert(is_basic_json<BasicJsonType>::value,
4155  "BasicJsonType must be of type basic_json<...>");
4156 
4157  using number_integer_t = typename BasicJsonType::number_integer_t;
4158  using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
4159  using number_float_t = typename BasicJsonType::number_float_t;
4160  using string_t = typename BasicJsonType::string_t;
4161  using exception_t = typename BasicJsonType::exception;
4162 
4163  public:
4164  static constexpr bool value =
4165  is_detected_exact<bool, null_function_t, SAX>::value &&
4166  is_detected_exact<bool, boolean_function_t, SAX>::value &&
4167  is_detected_exact<bool, number_integer_function_t, SAX,
4168  number_integer_t>::value &&
4169  is_detected_exact<bool, number_unsigned_function_t, SAX,
4170  number_unsigned_t>::value &&
4171  is_detected_exact<bool, number_float_function_t, SAX, number_float_t,
4172  string_t>::value &&
4173  is_detected_exact<bool, string_function_t, SAX, string_t>::value &&
4174  is_detected_exact<bool, start_object_function_t, SAX>::value &&
4175  is_detected_exact<bool, key_function_t, SAX, string_t>::value &&
4176  is_detected_exact<bool, end_object_function_t, SAX>::value &&
4177  is_detected_exact<bool, start_array_function_t, SAX>::value &&
4178  is_detected_exact<bool, end_array_function_t, SAX>::value &&
4179  is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value;
4180 };
4181 
4182 template <typename SAX, typename BasicJsonType>
4183 struct is_sax_static_asserts
4184 {
4185  private:
4186  static_assert(is_basic_json<BasicJsonType>::value,
4187  "BasicJsonType must be of type basic_json<...>");
4188 
4189  using number_integer_t = typename BasicJsonType::number_integer_t;
4190  using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
4191  using number_float_t = typename BasicJsonType::number_float_t;
4192  using string_t = typename BasicJsonType::string_t;
4193  using exception_t = typename BasicJsonType::exception;
4194 
4195  public:
4196  static_assert(is_detected_exact<bool, null_function_t, SAX>::value,
4197  "Missing/invalid function: bool null()");
4198  static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
4199  "Missing/invalid function: bool boolean(bool)");
4200  static_assert(is_detected_exact<bool, boolean_function_t, SAX>::value,
4201  "Missing/invalid function: bool boolean(bool)");
4202  static_assert(
4203  is_detected_exact<bool, number_integer_function_t, SAX,
4204  number_integer_t>::value,
4205  "Missing/invalid function: bool number_integer(number_integer_t)");
4206  static_assert(
4207  is_detected_exact<bool, number_unsigned_function_t, SAX,
4208  number_unsigned_t>::value,
4209  "Missing/invalid function: bool number_unsigned(number_unsigned_t)");
4210  static_assert(is_detected_exact<bool, number_float_function_t, SAX,
4211  number_float_t, string_t>::value,
4212  "Missing/invalid function: bool number_float(number_float_t, const string_t&)");
4213  static_assert(
4214  is_detected_exact<bool, string_function_t, SAX, string_t>::value,
4215  "Missing/invalid function: bool string(string_t&)");
4216  static_assert(is_detected_exact<bool, start_object_function_t, SAX>::value,
4217  "Missing/invalid function: bool start_object(std::size_t)");
4218  static_assert(is_detected_exact<bool, key_function_t, SAX, string_t>::value,
4219  "Missing/invalid function: bool key(string_t&)");
4220  static_assert(is_detected_exact<bool, end_object_function_t, SAX>::value,
4221  "Missing/invalid function: bool end_object()");
4222  static_assert(is_detected_exact<bool, start_array_function_t, SAX>::value,
4223  "Missing/invalid function: bool start_array(std::size_t)");
4224  static_assert(is_detected_exact<bool, end_array_function_t, SAX>::value,
4225  "Missing/invalid function: bool end_array()");
4226  static_assert(
4227  is_detected_exact<bool, parse_error_function_t, SAX, exception_t>::value,
4228  "Missing/invalid function: bool parse_error(std::size_t, const "
4229  "std::string&, const exception&)");
4230 };
4231 } // namespace detail
4232 } // namespace nlohmann
4233 
4234 // #include <nlohmann/detail/input/input_adapters.hpp>
4235 
4236 // #include <nlohmann/detail/input/json_sax.hpp>
4237 
4238 
4239 #include <cstddef>
4240 #include <string>
4241 #include <vector>
4242 
4243 // #include <nlohmann/detail/input/parser.hpp>
4244 
4245 // #include <nlohmann/detail/exceptions.hpp>
4246 
4247 
4248 namespace nlohmann
4249 {
4250 
4251 /*!
4252 @brief SAX interface
4253 
4254 This class describes the SAX interface used by @ref nlohmann::json::sax_parse.
4255 Each function is called in different situations while the input is parsed. The
4256 boolean return value informs the parser whether to continue processing the
4257 input.
4258 */
4259 template<typename BasicJsonType>
4260 struct json_sax
4261 {
4262  /// type for (signed) integers
4263  using number_integer_t = typename BasicJsonType::number_integer_t;
4264  /// type for unsigned integers
4265  using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
4266  /// type for floating-point numbers
4267  using number_float_t = typename BasicJsonType::number_float_t;
4268  /// type for strings
4269  using string_t = typename BasicJsonType::string_t;
4270 
4271  /*!
4272  @brief a null value was read
4273  @return whether parsing should proceed
4274  */
4275  virtual bool null() = 0;
4276 
4277  /*!
4278  @brief a boolean value was read
4279  @param[in] val boolean value
4280  @return whether parsing should proceed
4281  */
4282  virtual bool boolean(bool val) = 0;
4283 
4284  /*!
4285  @brief an integer number was read
4286  @param[in] val integer value
4287  @return whether parsing should proceed
4288  */
4289  virtual bool number_integer(number_integer_t val) = 0;
4290 
4291  /*!
4292  @brief an unsigned integer number was read
4293  @param[in] val unsigned integer value
4294  @return whether parsing should proceed
4295  */
4296  virtual bool number_unsigned(number_unsigned_t val) = 0;
4297 
4298  /*!
4299  @brief an floating-point number was read
4300  @param[in] val floating-point value
4301  @param[in] s raw token value
4302  @return whether parsing should proceed
4303  */
4304  virtual bool number_float(number_float_t val, const string_t& s) = 0;
4305 
4306  /*!
4307  @brief a string was read
4308  @param[in] val string value
4309  @return whether parsing should proceed
4310  @note It is safe to move the passed string.
4311  */
4312  virtual bool string(string_t& val) = 0;
4313 
4314  /*!
4315  @brief the beginning of an object was read
4316  @param[in] elements number of object elements or -1 if unknown
4317  @return whether parsing should proceed
4318  @note binary formats may report the number of elements
4319  */
4320  virtual bool start_object(std::size_t elements) = 0;
4321 
4322  /*!
4323  @brief an object key was read
4324  @param[in] val object key
4325  @return whether parsing should proceed
4326  @note It is safe to move the passed string.
4327  */
4328  virtual bool key(string_t& val) = 0;
4329 
4330  /*!
4331  @brief the end of an object was read
4332  @return whether parsing should proceed
4333  */
4334  virtual bool end_object() = 0;
4335 
4336  /*!
4337  @brief the beginning of an array was read
4338  @param[in] elements number of array elements or -1 if unknown
4339  @return whether parsing should proceed
4340  @note binary formats may report the number of elements
4341  */
4342  virtual bool start_array(std::size_t elements) = 0;
4343 
4344  /*!
4345  @brief the end of an array was read
4346  @return whether parsing should proceed
4347  */
4348  virtual bool end_array() = 0;
4349 
4350  /*!
4351  @brief a parse error occurred
4352  @param[in] position the position in the input where the error occurs
4353  @param[in] last_token the last read token
4354  @param[in] ex an exception object describing the error
4355  @return whether parsing should proceed (must return false)
4356  */
4357  virtual bool parse_error(std::size_t position,
4358  const std::string& last_token,
4359  const detail::exception& ex) = 0;
4360 
4361  virtual ~json_sax() = default;
4362 };
4363 
4364 
4365 namespace detail
4366 {
4367 /*!
4368 @brief SAX implementation to create a JSON value from SAX events
4369 
4370 This class implements the @ref json_sax interface and processes the SAX events
4371 to create a JSON value which makes it basically a DOM parser. The structure or
4372 hierarchy of the JSON value is managed by the stack `ref_stack` which contains
4373 a pointer to the respective array or object for each recursion depth.
4374 
4375 After successful parsing, the value that is passed by reference to the
4376 constructor contains the parsed value.
4377 
4378 @tparam BasicJsonType the JSON type
4379 */
4380 template<typename BasicJsonType>
4381 class json_sax_dom_parser
4382 {
4383  public:
4384  using number_integer_t = typename BasicJsonType::number_integer_t;
4385  using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
4386  using number_float_t = typename BasicJsonType::number_float_t;
4387  using string_t = typename BasicJsonType::string_t;
4388 
4389  /*!
4390  @param[in, out] r reference to a JSON value that is manipulated while
4391  parsing
4392  @param[in] allow_exceptions_ whether parse errors yield exceptions
4393  */
4394  explicit json_sax_dom_parser(BasicJsonType& r, const bool allow_exceptions_ = true)
4395  : root(r), allow_exceptions(allow_exceptions_)
4396  {}
4397 
4398  bool null()
4399  {
4400  handle_value(nullptr);
4401  return true;
4402  }
4403 
4404  bool boolean(bool val)
4405  {
4406  handle_value(val);
4407  return true;
4408  }
4409 
4410  bool number_integer(number_integer_t val)
4411  {
4412  handle_value(val);
4413  return true;
4414  }
4415 
4416  bool number_unsigned(number_unsigned_t val)
4417  {
4418  handle_value(val);
4419  return true;
4420  }
4421 
4422  bool number_float(number_float_t val, const string_t& /*unused*/)
4423  {
4424  handle_value(val);
4425  return true;
4426  }
4427 
4428  bool string(string_t& val)
4429  {
4430  handle_value(val);
4431  return true;
4432  }
4433 
4434  bool start_object(std::size_t len)
4435  {
4436  ref_stack.push_back(handle_value(BasicJsonType::value_t::object));
4437 
4438  if (JSON_UNLIKELY(len != std::size_t(-1) and len > ref_stack.back()->max_size()))
4439  {
4440  JSON_THROW(out_of_range::create(408,
4441  "excessive object size: " + std::to_string(len)));
4442  }
4443 
4444  return true;
4445  }
4446 
4447  bool key(string_t& val)
4448  {
4449  // add null at given key and store the reference for later
4450  object_element = &(ref_stack.back()->m_value.object->operator[](val));
4451  return true;
4452  }
4453 
4454  bool end_object()
4455  {
4456  ref_stack.pop_back();
4457  return true;
4458  }
4459 
4460  bool start_array(std::size_t len)
4461  {
4462  ref_stack.push_back(handle_value(BasicJsonType::value_t::array));
4463 
4464  if (JSON_UNLIKELY(len != std::size_t(-1) and len > ref_stack.back()->max_size()))
4465  {
4466  JSON_THROW(out_of_range::create(408,
4467  "excessive array size: " + std::to_string(len)));
4468  }
4469 
4470  return true;
4471  }
4472 
4473  bool end_array()
4474  {
4475  ref_stack.pop_back();
4476  return true;
4477  }
4478 
4479  bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
4480  const detail::exception& ex)
4481  {
4482  errored = true;
4483  if (allow_exceptions)
4484  {
4485  // determine the proper exception type from the id
4486  switch ((ex.id / 100) % 100)
4487  {
4488  case 1:
4489  JSON_THROW(*reinterpret_cast<const detail::parse_error*>(&ex));
4490  case 4:
4491  JSON_THROW(*reinterpret_cast<const detail::out_of_range*>(&ex));
4492  // LCOV_EXCL_START
4493  case 2:
4494  JSON_THROW(*reinterpret_cast<const detail::invalid_iterator*>(&ex));
4495  case 3:
4496  JSON_THROW(*reinterpret_cast<const detail::type_error*>(&ex));
4497  case 5:
4498  JSON_THROW(*reinterpret_cast<const detail::other_error*>(&ex));
4499  default:
4500  assert(false);
4501  // LCOV_EXCL_STOP
4502  }
4503  }
4504  return false;
4505  }
4506 
4507  constexpr bool is_errored() const
4508  {
4509  return errored;
4510  }
4511 
4512  private:
4513  /*!
4514  @invariant If the ref stack is empty, then the passed value will be the new
4515  root.
4516  @invariant If the ref stack contains a value, then it is an array or an
4517  object to which we can add elements
4518  */
4519  template<typename Value>
4520  BasicJsonType* handle_value(Value&& v)
4521  {
4522  if (ref_stack.empty())
4523  {
4524  root = BasicJsonType(std::forward<Value>(v));
4525  return &root;
4526  }
4527 
4528  assert(ref_stack.back()->is_array() or ref_stack.back()->is_object());
4529 
4530  if (ref_stack.back()->is_array())
4531  {
4532  ref_stack.back()->m_value.array->emplace_back(std::forward<Value>(v));
4533  return &(ref_stack.back()->m_value.array->back());
4534  }
4535  else
4536  {
4537  assert(object_element);
4538  *object_element = BasicJsonType(std::forward<Value>(v));
4539  return object_element;
4540  }
4541  }
4542 
4543  /// the parsed JSON value
4544  BasicJsonType& root;
4545  /// stack to model hierarchy of values
4546  std::vector<BasicJsonType*> ref_stack;
4547  /// helper to hold the reference for the next object element
4548  BasicJsonType* object_element = nullptr;
4549  /// whether a syntax error occurred
4550  bool errored = false;
4551  /// whether to throw exceptions in case of errors
4552  const bool allow_exceptions = true;
4553 };
4554 
4555 template<typename BasicJsonType>
4556 class json_sax_dom_callback_parser
4557 {
4558  public:
4559  using number_integer_t = typename BasicJsonType::number_integer_t;
4560  using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
4561  using number_float_t = typename BasicJsonType::number_float_t;
4562  using string_t = typename BasicJsonType::string_t;
4563  using parser_callback_t = typename BasicJsonType::parser_callback_t;
4564  using parse_event_t = typename BasicJsonType::parse_event_t;
4565 
4566  json_sax_dom_callback_parser(BasicJsonType& r,
4567  const parser_callback_t cb,
4568  const bool allow_exceptions_ = true)
4569  : root(r), callback(cb), allow_exceptions(allow_exceptions_)
4570  {
4571  keep_stack.push_back(true);
4572  }
4573 
4574  bool null()
4575  {
4576  handle_value(nullptr);
4577  return true;
4578  }
4579 
4580  bool boolean(bool val)
4581  {
4582  handle_value(val);
4583  return true;
4584  }
4585 
4586  bool number_integer(number_integer_t val)
4587  {
4588  handle_value(val);
4589  return true;
4590  }
4591 
4592  bool number_unsigned(number_unsigned_t val)
4593  {
4594  handle_value(val);
4595  return true;
4596  }
4597 
4598  bool number_float(number_float_t val, const string_t& /*unused*/)
4599  {
4600  handle_value(val);
4601  return true;
4602  }
4603 
4604  bool string(string_t& val)
4605  {
4606  handle_value(val);
4607  return true;
4608  }
4609 
4610  bool start_object(std::size_t len)
4611  {
4612  // check callback for object start
4613  const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::object_start, discarded);
4614  keep_stack.push_back(keep);
4615 
4616  auto val = handle_value(BasicJsonType::value_t::object, true);
4617  ref_stack.push_back(val.second);
4618 
4619  // check object limit
4620  if (ref_stack.back())
4621  {
4622  if (JSON_UNLIKELY(len != std::size_t(-1) and len > ref_stack.back()->max_size()))
4623  {
4624  JSON_THROW(out_of_range::create(408,
4625  "excessive object size: " + std::to_string(len)));
4626  }
4627  }
4628 
4629  return true;
4630  }
4631 
4632  bool key(string_t& val)
4633  {
4634  BasicJsonType k = BasicJsonType(val);
4635 
4636  // check callback for key
4637  const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::key, k);
4638  key_keep_stack.push_back(keep);
4639 
4640  // add discarded value at given key and store the reference for later
4641  if (keep and ref_stack.back())
4642  {
4643  object_element = &(ref_stack.back()->m_value.object->operator[](val) = discarded);
4644  }
4645 
4646  return true;
4647  }
4648 
4649  bool end_object()
4650  {
4651  if (ref_stack.back())
4652  {
4653  if (not callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back()))
4654  {
4655  // discard object
4656  *ref_stack.back() = discarded;
4657  }
4658  }
4659 
4660  assert(not ref_stack.empty());
4661  assert(not keep_stack.empty());
4662  ref_stack.pop_back();
4663  keep_stack.pop_back();
4664 
4665  if (not ref_stack.empty() and ref_stack.back())
4666  {
4667  // remove discarded value
4668  if (ref_stack.back()->is_object())
4669  {
4670  for (auto it = ref_stack.back()->begin(); it != ref_stack.back()->end(); ++it)
4671  {
4672  if (it->is_discarded())
4673  {
4674  ref_stack.back()->erase(it);
4675  break;
4676  }
4677  }
4678  }
4679  }
4680 
4681  return true;
4682  }
4683 
4684  bool start_array(std::size_t len)
4685  {
4686  const bool keep = callback(static_cast<int>(ref_stack.size()), parse_event_t::array_start, discarded);
4687  keep_stack.push_back(keep);
4688 
4689  auto val = handle_value(BasicJsonType::value_t::array, true);
4690  ref_stack.push_back(val.second);
4691 
4692  // check array limit
4693  if (ref_stack.back())
4694  {
4695  if (JSON_UNLIKELY(len != std::size_t(-1) and len > ref_stack.back()->max_size()))
4696  {
4697  JSON_THROW(out_of_range::create(408,
4698  "excessive array size: " + std::to_string(len)));
4699  }
4700  }
4701 
4702  return true;
4703  }
4704 
4705  bool end_array()
4706  {
4707  bool keep = true;
4708 
4709  if (ref_stack.back())
4710  {
4711  keep = callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back());
4712  if (not keep)
4713  {
4714  // discard array
4715  *ref_stack.back() = discarded;
4716  }
4717  }
4718 
4719  assert(not ref_stack.empty());
4720  assert(not keep_stack.empty());
4721  ref_stack.pop_back();
4722  keep_stack.pop_back();
4723 
4724  // remove discarded value
4725  if (not keep and not ref_stack.empty())
4726  {
4727  if (ref_stack.back()->is_array())
4728  {
4729  ref_stack.back()->m_value.array->pop_back();
4730  }
4731  }
4732 
4733  return true;
4734  }
4735 
4736  bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/,
4737  const detail::exception& ex)
4738  {
4739  errored = true;
4740  if (allow_exceptions)
4741  {
4742  // determine the proper exception type from the id
4743  switch ((ex.id / 100) % 100)
4744  {
4745  case 1:
4746  JSON_THROW(*reinterpret_cast<const detail::parse_error*>(&ex));
4747  case 4:
4748  JSON_THROW(*reinterpret_cast<const detail::out_of_range*>(&ex));
4749  // LCOV_EXCL_START
4750  case 2:
4751  JSON_THROW(*reinterpret_cast<const detail::invalid_iterator*>(&ex));
4752  case 3:
4753  JSON_THROW(*reinterpret_cast<const detail::type_error*>(&ex));
4754  case 5:
4755  JSON_THROW(*reinterpret_cast<const detail::other_error*>(&ex));
4756  default:
4757  assert(false);
4758  // LCOV_EXCL_STOP
4759  }
4760  }
4761  return false;
4762  }
4763 
4764  constexpr bool is_errored() const
4765  {
4766  return errored;
4767  }
4768 
4769  private:
4770  /*!
4771  @param[in] v value to add to the JSON value we build during parsing
4772  @param[in] skip_callback whether we should skip calling the callback
4773  function; this is required after start_array() and
4774  start_object() SAX events, because otherwise we would call the
4775  callback function with an empty array or object, respectively.
4776 
4777  @invariant If the ref stack is empty, then the passed value will be the new
4778  root.
4779  @invariant If the ref stack contains a value, then it is an array or an
4780  object to which we can add elements
4781 
4782  @return pair of boolean (whether value should be kept) and pointer (to the
4783  passed value in the ref_stack hierarchy; nullptr if not kept)
4784  */
4785  template<typename Value>
4786  std::pair<bool, BasicJsonType*> handle_value(Value&& v, const bool skip_callback = false)
4787  {
4788  assert(not keep_stack.empty());
4789 
4790  // do not handle this value if we know it would be added to a discarded
4791  // container
4792  if (not keep_stack.back())
4793  {
4794  return {false, nullptr};
4795  }
4796 
4797  // create value
4798  auto value = BasicJsonType(std::forward<Value>(v));
4799 
4800  // check callback
4801  const bool keep = skip_callback or callback(static_cast<int>(ref_stack.size()), parse_event_t::value, value);
4802 
4803  // do not handle this value if we just learnt it shall be discarded
4804  if (not keep)
4805  {
4806  return {false, nullptr};
4807  }
4808 
4809  if (ref_stack.empty())
4810  {
4811  root = std::move(value);
4812  return {true, &root};
4813  }
4814 
4815  // skip this value if we already decided to skip the parent
4816  // (https://github.com/nlohmann/json/issues/971#issuecomment-413678360)
4817  if (not ref_stack.back())
4818  {
4819  return {false, nullptr};
4820  }
4821 
4822  // we now only expect arrays and objects
4823  assert(ref_stack.back()->is_array() or ref_stack.back()->is_object());
4824 
4825  if (ref_stack.back()->is_array())
4826  {
4827  ref_stack.back()->m_value.array->push_back(std::move(value));
4828  return {true, &(ref_stack.back()->m_value.array->back())};
4829  }
4830  else
4831  {
4832  // check if we should store an element for the current key
4833  assert(not key_keep_stack.empty());
4834  const bool store_element = key_keep_stack.back();
4835  key_keep_stack.pop_back();
4836 
4837  if (not store_element)
4838  {
4839  return {false, nullptr};
4840  }
4841 
4842  assert(object_element);
4843  *object_element = std::move(value);
4844  return {true, object_element};
4845  }
4846  }
4847 
4848  /// the parsed JSON value
4849  BasicJsonType& root;
4850  /// stack to model hierarchy of values
4851  std::vector<BasicJsonType*> ref_stack;
4852  /// stack to manage which values to keep
4853  std::vector<bool> keep_stack;
4854  /// stack to manage which object keys to keep
4855  std::vector<bool> key_keep_stack;
4856  /// helper to hold the reference for the next object element
4857  BasicJsonType* object_element = nullptr;
4858  /// whether a syntax error occurred
4859  bool errored = false;
4860  /// callback function
4861  const parser_callback_t callback = nullptr;
4862  /// whether to throw exceptions in case of errors
4863  const bool allow_exceptions = true;
4864  /// a discarded value for the callback
4865  BasicJsonType discarded = BasicJsonType::value_t::discarded;
4866 };
4867 
4868 template<typename BasicJsonType>
4869 class json_sax_acceptor
4870 {
4871  public:
4872  using number_integer_t = typename BasicJsonType::number_integer_t;
4873  using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
4874  using number_float_t = typename BasicJsonType::number_float_t;
4875  using string_t = typename BasicJsonType::string_t;
4876 
4877  bool null()
4878  {
4879  return true;
4880  }
4881 
4882  bool boolean(bool /*unused*/)
4883  {
4884  return true;
4885  }
4886 
4887  bool number_integer(number_integer_t /*unused*/)
4888  {
4889  return true;
4890  }
4891 
4892  bool number_unsigned(number_unsigned_t /*unused*/)
4893  {
4894  return true;
4895  }
4896 
4897  bool number_float(number_float_t /*unused*/, const string_t& /*unused*/)
4898  {
4899  return true;
4900  }
4901 
4902  bool string(string_t& /*unused*/)
4903  {
4904  return true;
4905  }
4906 
4907  bool start_object(std::size_t /*unused*/ = std::size_t(-1))
4908  {
4909  return true;
4910  }
4911 
4912  bool key(string_t& /*unused*/)
4913  {
4914  return true;
4915  }
4916 
4917  bool end_object()
4918  {
4919  return true;
4920  }
4921 
4922  bool start_array(std::size_t /*unused*/ = std::size_t(-1))
4923  {
4924  return true;
4925  }
4926 
4927  bool end_array()
4928  {
4929  return true;
4930  }
4931 
4932  bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const detail::exception& /*unused*/)
4933  {
4934  return false;
4935  }
4936 };
4937 } // namespace detail
4938 
4939 } // namespace nlohmann
4940 
4941 // #include <nlohmann/detail/input/lexer.hpp>
4942 
4943 // #include <nlohmann/detail/value_t.hpp>
4944 
4945 
4946 namespace nlohmann
4947 {
4948 namespace detail
4949 {
4950 ////////////
4951 // parser //
4952 ////////////
4953 
4954 /*!
4955 @brief syntax analysis
4956 
4957 This class implements a recursive decent parser.
4958 */
4959 template<typename BasicJsonType>
4960 class parser
4961 {
4962  using number_integer_t = typename BasicJsonType::number_integer_t;
4963  using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
4964  using number_float_t = typename BasicJsonType::number_float_t;
4965  using string_t = typename BasicJsonType::string_t;
4966  using lexer_t = lexer<BasicJsonType>;
4967  using token_type = typename lexer_t::token_type;
4968 
4969  public:
4970  enum class parse_event_t : uint8_t
4971  {
4972  /// the parser read `{` and started to process a JSON object
4973  object_start,
4974  /// the parser read `}` and finished processing a JSON object
4975  object_end,
4976  /// the parser read `[` and started to process a JSON array
4977  array_start,
4978  /// the parser read `]` and finished processing a JSON array
4979  array_end,
4980  /// the parser read a key of a value in an object
4981  key,
4982  /// the parser finished reading a JSON value
4983  value
4984  };
4985 
4986  using parser_callback_t =
4987  std::function<bool(int depth, parse_event_t event, BasicJsonType& parsed)>;
4988 
4989  /// a parser reading from an input adapter
4990  explicit parser(detail::input_adapter_t&& adapter,
4991  const parser_callback_t cb = nullptr,
4992  const bool allow_exceptions_ = true)
4993  : callback(cb), m_lexer(std::move(adapter)), allow_exceptions(allow_exceptions_)
4994  {
4995  // read first token
4996  get_token();
4997  }
4998 
4999  /*!
5000  @brief public parser interface
5001 
5002  @param[in] strict whether to expect the last token to be EOF
5003  @param[in,out] result parsed JSON value
5004 
5005  @throw parse_error.101 in case of an unexpected token
5006  @throw parse_error.102 if to_unicode fails or surrogate error
5007  @throw parse_error.103 if to_unicode fails
5008  */
5009  void parse(const bool strict, BasicJsonType& result)
5010  {
5011  if (callback)
5012  {
5013  json_sax_dom_callback_parser<BasicJsonType> sdp(result, callback, allow_exceptions);
5014  sax_parse_internal(&sdp);
5015  result.assert_invariant();
5016 
5017  // in strict mode, input must be completely read
5018  if (strict and (get_token() != token_type::end_of_input))
5019  {
5020  sdp.parse_error(m_lexer.get_position(),
5021  m_lexer.get_token_string(),
5022  parse_error::create(101, m_lexer.get_position(),
5023  exception_message(token_type::end_of_input, "value")));
5024  }
5025 
5026  // in case of an error, return discarded value
5027  if (sdp.is_errored())
5028  {
5029  result = value_t::discarded;
5030  return;
5031  }
5032 
5033  // set top-level value to null if it was discarded by the callback
5034  // function
5035  if (result.is_discarded())
5036  {
5037  result = nullptr;
5038  }
5039  }
5040  else
5041  {
5042  json_sax_dom_parser<BasicJsonType> sdp(result, allow_exceptions);
5043  sax_parse_internal(&sdp);
5044  result.assert_invariant();
5045 
5046  // in strict mode, input must be completely read
5047  if (strict and (get_token() != token_type::end_of_input))
5048  {
5049  sdp.parse_error(m_lexer.get_position(),
5050  m_lexer.get_token_string(),
5051  parse_error::create(101, m_lexer.get_position(),
5052  exception_message(token_type::end_of_input, "value")));
5053  }
5054 
5055  // in case of an error, return discarded value
5056  if (sdp.is_errored())
5057  {
5058  result = value_t::discarded;
5059  return;
5060  }
5061  }
5062  }
5063 
5064  /*!
5065  @brief public accept interface
5066 
5067  @param[in] strict whether to expect the last token to be EOF
5068  @return whether the input is a proper JSON text
5069  */
5070  bool accept(const bool strict = true)
5071  {
5072  json_sax_acceptor<BasicJsonType> sax_acceptor;
5073  return sax_parse(&sax_acceptor, strict);
5074  }
5075 
5076  template <typename SAX>
5077  bool sax_parse(SAX* sax, const bool strict = true)
5078  {
5079  (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
5080  const bool result = sax_parse_internal(sax);
5081 
5082  // strict mode: next byte must be EOF
5083  if (result and strict and (get_token() != token_type::end_of_input))
5084  {
5085  return sax->parse_error(m_lexer.get_position(),
5086  m_lexer.get_token_string(),
5087  parse_error::create(101, m_lexer.get_position(),
5088  exception_message(token_type::end_of_input, "value")));
5089  }
5090 
5091  return result;
5092  }
5093 
5094  private:
5095  template <typename SAX>
5096  bool sax_parse_internal(SAX* sax)
5097  {
5098  // stack to remember the hierarchy of structured values we are parsing
5099  // true = array; false = object
5100  std::vector<bool> states;
5101  // value to avoid a goto (see comment where set to true)
5102  bool skip_to_state_evaluation = false;
5103 
5104  while (true)
5105  {
5106  if (not skip_to_state_evaluation)
5107  {
5108  // invariant: get_token() was called before each iteration
5109  switch (last_token)
5110  {
5111  case token_type::begin_object:
5112  {
5113  if (JSON_UNLIKELY(not sax->start_object(std::size_t(-1))))
5114  {
5115  return false;
5116  }
5117 
5118  // closing } -> we are done
5119  if (get_token() == token_type::end_object)
5120  {
5121  if (JSON_UNLIKELY(not sax->end_object()))
5122  {
5123  return false;
5124  }
5125  break;
5126  }
5127 
5128  // parse key
5129  if (JSON_UNLIKELY(last_token != token_type::value_string))
5130  {
5131  return sax->parse_error(m_lexer.get_position(),
5132  m_lexer.get_token_string(),
5133  parse_error::create(101, m_lexer.get_position(),
5134  exception_message(token_type::value_string, "object key")));
5135  }
5136  if (JSON_UNLIKELY(not sax->key(m_lexer.get_string())))
5137  {
5138  return false;
5139  }
5140 
5141  // parse separator (:)
5142  if (JSON_UNLIKELY(get_token() != token_type::name_separator))
5143  {
5144  return sax->parse_error(m_lexer.get_position(),
5145  m_lexer.get_token_string(),
5146  parse_error::create(101, m_lexer.get_position(),
5147  exception_message(token_type::name_separator, "object separator")));
5148  }
5149 
5150  // remember we are now inside an object
5151  states.push_back(false);
5152 
5153  // parse values
5154  get_token();
5155  continue;
5156  }
5157 
5158  case token_type::begin_array:
5159  {
5160  if (JSON_UNLIKELY(not sax->start_array(std::size_t(-1))))
5161  {
5162  return false;
5163  }
5164 
5165  // closing ] -> we are done
5166  if (get_token() == token_type::end_array)
5167  {
5168  if (JSON_UNLIKELY(not sax->end_array()))
5169  {
5170  return false;
5171  }
5172  break;
5173  }
5174 
5175  // remember we are now inside an array
5176  states.push_back(true);
5177 
5178  // parse values (no need to call get_token)
5179  continue;
5180  }
5181 
5182  case token_type::value_float:
5183  {
5184  const auto res = m_lexer.get_number_float();
5185 
5186  if (JSON_UNLIKELY(not std::isfinite(res)))
5187  {
5188  return sax->parse_error(m_lexer.get_position(),
5189  m_lexer.get_token_string(),
5190  out_of_range::create(406, "number overflow parsing '" + m_lexer.get_token_string() + "'"));
5191  }
5192  else
5193  {
5194  if (JSON_UNLIKELY(not sax->number_float(res, m_lexer.get_string())))
5195  {
5196  return false;
5197  }
5198  break;
5199  }
5200  }
5201 
5202  case token_type::literal_false:
5203  {
5204  if (JSON_UNLIKELY(not sax->boolean(false)))
5205  {
5206  return false;
5207  }
5208  break;
5209  }
5210 
5211  case token_type::literal_null:
5212  {
5213  if (JSON_UNLIKELY(not sax->null()))
5214  {
5215  return false;
5216  }
5217  break;
5218  }
5219 
5220  case token_type::literal_true:
5221  {
5222  if (JSON_UNLIKELY(not sax->boolean(true)))
5223  {
5224  return false;
5225  }
5226  break;
5227  }
5228 
5229  case token_type::value_integer:
5230  {
5231  if (JSON_UNLIKELY(not sax->number_integer(m_lexer.get_number_integer())))
5232  {
5233  return false;
5234  }
5235  break;
5236  }
5237 
5238  case token_type::value_string:
5239  {
5240  if (JSON_UNLIKELY(not sax->string(m_lexer.get_string())))
5241  {
5242  return false;
5243  }
5244  break;
5245  }
5246 
5247  case token_type::value_unsigned:
5248  {
5249  if (JSON_UNLIKELY(not sax->number_unsigned(m_lexer.get_number_unsigned())))
5250  {
5251  return false;
5252  }
5253  break;
5254  }
5255 
5256  case token_type::parse_error:
5257  {
5258  // using "uninitialized" to avoid "expected" message
5259  return sax->parse_error(m_lexer.get_position(),
5260  m_lexer.get_token_string(),
5261  parse_error::create(101, m_lexer.get_position(),
5262  exception_message(token_type::uninitialized, "value")));
5263  }
5264 
5265  default: // the last token was unexpected
5266  {
5267  return sax->parse_error(m_lexer.get_position(),
5268  m_lexer.get_token_string(),
5269  parse_error::create(101, m_lexer.get_position(),
5270  exception_message(token_type::literal_or_value, "value")));
5271  }
5272  }
5273  }
5274  else
5275  {
5276  skip_to_state_evaluation = false;
5277  }
5278 
5279  // we reached this line after we successfully parsed a value
5280  if (states.empty())
5281  {
5282  // empty stack: we reached the end of the hierarchy: done
5283  return true;
5284  }
5285  else
5286  {
5287  if (states.back()) // array
5288  {
5289  // comma -> next value
5290  if (get_token() == token_type::value_separator)
5291  {
5292  // parse a new value
5293  get_token();
5294  continue;
5295  }
5296 
5297  // closing ]
5298  if (JSON_LIKELY(last_token == token_type::end_array))
5299  {
5300  if (JSON_UNLIKELY(not sax->end_array()))
5301  {
5302  return false;
5303  }
5304 
5305  // We are done with this array. Before we can parse a
5306  // new value, we need to evaluate the new state first.
5307  // By setting skip_to_state_evaluation to false, we
5308  // are effectively jumping to the beginning of this if.
5309  assert(not states.empty());
5310  states.pop_back();
5311  skip_to_state_evaluation = true;
5312  continue;
5313  }
5314  else
5315  {
5316  return sax->parse_error(m_lexer.get_position(),
5317  m_lexer.get_token_string(),
5318  parse_error::create(101, m_lexer.get_position(),
5319  exception_message(token_type::end_array, "array")));
5320  }
5321  }
5322  else // object
5323  {
5324  // comma -> next value
5325  if (get_token() == token_type::value_separator)
5326  {
5327  // parse key
5328  if (JSON_UNLIKELY(get_token() != token_type::value_string))
5329  {
5330  return sax->parse_error(m_lexer.get_position(),
5331  m_lexer.get_token_string(),
5332  parse_error::create(101, m_lexer.get_position(),
5333  exception_message(token_type::value_string, "object key")));
5334  }
5335  else
5336  {
5337  if (JSON_UNLIKELY(not sax->key(m_lexer.get_string())))
5338  {
5339  return false;
5340  }
5341  }
5342 
5343  // parse separator (:)
5344  if (JSON_UNLIKELY(get_token() != token_type::name_separator))
5345  {
5346  return sax->parse_error(m_lexer.get_position(),
5347  m_lexer.get_token_string(),
5348  parse_error::create(101, m_lexer.get_position(),
5349  exception_message(token_type::name_separator, "object separator")));
5350  }
5351 
5352  // parse values
5353  get_token();
5354  continue;
5355  }
5356 
5357  // closing }
5358  if (JSON_LIKELY(last_token == token_type::end_object))
5359  {
5360  if (JSON_UNLIKELY(not sax->end_object()))
5361  {
5362  return false;
5363  }
5364 
5365  // We are done with this object. Before we can parse a
5366  // new value, we need to evaluate the new state first.
5367  // By setting skip_to_state_evaluation to false, we
5368  // are effectively jumping to the beginning of this if.
5369  assert(not states.empty());
5370  states.pop_back();
5371  skip_to_state_evaluation = true;
5372  continue;
5373  }
5374  else
5375  {
5376  return sax->parse_error(m_lexer.get_position(),
5377  m_lexer.get_token_string(),
5378  parse_error::create(101, m_lexer.get_position(),
5379  exception_message(token_type::end_object, "object")));
5380  }
5381  }
5382  }
5383  }
5384  }
5385 
5386  /// get next token from lexer
5387  token_type get_token()
5388  {
5389  return (last_token = m_lexer.scan());
5390  }
5391 
5392  std::string exception_message(const token_type expected, const std::string& context)
5393  {
5394  std::string error_msg = "syntax error ";
5395 
5396  if (not context.empty())
5397  {
5398  error_msg += "while parsing " + context + " ";
5399  }
5400 
5401  error_msg += "- ";
5402 
5403  if (last_token == token_type::parse_error)
5404  {
5405  error_msg += std::string(m_lexer.get_error_message()) + "; last read: '" +
5406  m_lexer.get_token_string() + "'";
5407  }
5408  else
5409  {
5410  error_msg += "unexpected " + std::string(lexer_t::token_type_name(last_token));
5411  }
5412 
5413  if (expected != token_type::uninitialized)
5414  {
5415  error_msg += "; expected " + std::string(lexer_t::token_type_name(expected));
5416  }
5417 
5418  return error_msg;
5419  }
5420 
5421  private:
5422  /// callback function
5423  const parser_callback_t callback = nullptr;
5424  /// the type of the last read token
5425  token_type last_token = token_type::uninitialized;
5426  /// the lexer
5427  lexer_t m_lexer;
5428  /// whether to throw exceptions in case of errors
5429  const bool allow_exceptions = true;
5430 };
5431 } // namespace detail
5432 } // namespace nlohmann
5433 
5434 // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
5435 
5436 
5437 #include <cstddef> // ptrdiff_t
5438 #include <limits> // numeric_limits
5439 
5440 namespace nlohmann
5441 {
5442 namespace detail
5443 {
5444 /*
5445 @brief an iterator for primitive JSON types
5446 
5447 This class models an iterator for primitive JSON types (boolean, number,
5448 string). It's only purpose is to allow the iterator/const_iterator classes
5449 to "iterate" over primitive values. Internally, the iterator is modeled by
5450 a `difference_type` variable. Value begin_value (`0`) models the begin,
5451 end_value (`1`) models past the end.
5452 */
5453 class primitive_iterator_t
5454 {
5455  private:
5456  using difference_type = std::ptrdiff_t;
5457  static constexpr difference_type begin_value = 0;
5458  static constexpr difference_type end_value = begin_value + 1;
5459 
5460  /// iterator as signed integer type
5461  difference_type m_it = (std::numeric_limits<std::ptrdiff_t>::min)();
5462 
5463  public:
5464  constexpr difference_type get_value() const noexcept
5465  {
5466  return m_it;
5467  }
5468 
5469  /// set iterator to a defined beginning
5470  void set_begin() noexcept
5471  {
5472  m_it = begin_value;
5473  }
5474 
5475  /// set iterator to a defined past the end
5476  void set_end() noexcept
5477  {
5478  m_it = end_value;
5479  }
5480 
5481  /// return whether the iterator can be dereferenced
5482  constexpr bool is_begin() const noexcept
5483  {
5484  return m_it == begin_value;
5485  }
5486 
5487  /// return whether the iterator is at end
5488  constexpr bool is_end() const noexcept
5489  {
5490  return m_it == end_value;
5491  }
5492 
5493  friend constexpr bool operator==(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
5494  {
5495  return lhs.m_it == rhs.m_it;
5496  }
5497 
5498  friend constexpr bool operator<(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
5499  {
5500  return lhs.m_it < rhs.m_it;
5501  }
5502 
5503  primitive_iterator_t operator+(difference_type n) noexcept
5504  {
5505  auto result = *this;
5506  result += n;
5507  return result;
5508  }
5509 
5510  friend constexpr difference_type operator-(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept
5511  {
5512  return lhs.m_it - rhs.m_it;
5513  }
5514 
5515  primitive_iterator_t& operator++() noexcept
5516  {
5517  ++m_it;
5518  return *this;
5519  }
5520 
5521  primitive_iterator_t const operator++(int) noexcept
5522  {
5523  auto result = *this;
5524  ++m_it;
5525  return result;
5526  }
5527 
5528  primitive_iterator_t& operator--() noexcept
5529  {
5530  --m_it;
5531  return *this;
5532  }
5533 
5534  primitive_iterator_t const operator--(int) noexcept
5535  {
5536  auto result = *this;
5537  --m_it;
5538  return result;
5539  }
5540 
5541  primitive_iterator_t& operator+=(difference_type n) noexcept
5542  {
5543  m_it += n;
5544  return *this;
5545  }
5546 
5547  primitive_iterator_t& operator-=(difference_type n) noexcept
5548  {
5549  m_it -= n;
5550  return *this;
5551  }
5552 };
5553 } // namespace detail
5554 } // namespace nlohmann
5555 
5556 // #include <nlohmann/detail/iterators/internal_iterator.hpp>
5557 
5558 
5559 // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
5560 
5561 
5562 namespace nlohmann
5563 {
5564 namespace detail
5565 {
5566 /*!
5567 @brief an iterator value
5568 
5569 @note This structure could easily be a union, but MSVC currently does not allow
5570 unions members with complex constructors, see https://github.com/nlohmann/json/pull/105.
5571 */
5572 template<typename BasicJsonType> struct internal_iterator
5573 {
5574  /// iterator for JSON objects
5575  typename BasicJsonType::object_t::iterator object_iterator {};
5576  /// iterator for JSON arrays
5577  typename BasicJsonType::array_t::iterator array_iterator {};
5578  /// generic iterator for all other types
5579  primitive_iterator_t primitive_iterator {};
5580 };
5581 } // namespace detail
5582 } // namespace nlohmann
5583 
5584 // #include <nlohmann/detail/iterators/iter_impl.hpp>
5585 
5586 
5587 #include <ciso646> // not
5588 #include <iterator> // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next
5589 #include <type_traits> // conditional, is_const, remove_const
5590 
5591 // #include <nlohmann/detail/exceptions.hpp>
5592 
5593 // #include <nlohmann/detail/iterators/internal_iterator.hpp>
5594 
5595 // #include <nlohmann/detail/iterators/primitive_iterator.hpp>
5596 
5597 // #include <nlohmann/detail/macro_scope.hpp>
5598 
5599 // #include <nlohmann/detail/meta/cpp_future.hpp>
5600 
5601 // #include <nlohmann/detail/value_t.hpp>
5602 
5603 
5604 namespace nlohmann
5605 {
5606 namespace detail
5607 {
5608 // forward declare, to be able to friend it later on
5609 template<typename IteratorType> class iteration_proxy;
5610 template<typename IteratorType> class iteration_proxy_value;
5611 
5612 /*!
5613 @brief a template for a bidirectional iterator for the @ref basic_json class
5614 This class implements a both iterators (iterator and const_iterator) for the
5615 @ref basic_json class.
5616 @note An iterator is called *initialized* when a pointer to a JSON value has
5617  been set (e.g., by a constructor or a copy assignment). If the iterator is
5618  default-constructed, it is *uninitialized* and most methods are undefined.
5619  **The library uses assertions to detect calls on uninitialized iterators.**
5620 @requirement The class satisfies the following concept requirements:
5621 -
5622 [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
5623  The iterator that can be moved can be moved in both directions (i.e.
5624  incremented and decremented).
5625 @since version 1.0.0, simplified in version 2.0.9, change to bidirectional
5626  iterators in version 3.0.0 (see https://github.com/nlohmann/json/issues/593)
5627 */
5628 template<typename BasicJsonType>
5629 class iter_impl
5630 {
5631  /// allow basic_json to access private members
5632  friend iter_impl<typename std::conditional<std::is_const<BasicJsonType>::value, typename std::remove_const<BasicJsonType>::type, const BasicJsonType>::type>;
5633  friend BasicJsonType;
5634  friend iteration_proxy<iter_impl>;
5635  friend iteration_proxy_value<iter_impl>;
5636 
5637  using object_t = typename BasicJsonType::object_t;
5638  using array_t = typename BasicJsonType::array_t;
5639  // make sure BasicJsonType is basic_json or const basic_json
5640  static_assert(is_basic_json<typename std::remove_const<BasicJsonType>::type>::value,
5641  "iter_impl only accepts (const) basic_json");
5642 
5643  public:
5644 
5645  /// The std::iterator class template (used as a base class to provide typedefs) is deprecated in C++17.
5646  /// The C++ Standard has never required user-defined iterators to derive from std::iterator.
5647  /// A user-defined iterator should provide publicly accessible typedefs named
5648  /// iterator_category, value_type, difference_type, pointer, and reference.
5649  /// Note that value_type is required to be non-const, even for constant iterators.
5650  using iterator_category = std::bidirectional_iterator_tag;
5651 
5652  /// the type of the values when the iterator is dereferenced
5653  using value_type = typename BasicJsonType::value_type;
5654  /// a type to represent differences between iterators
5655  using difference_type = typename BasicJsonType::difference_type;
5656  /// defines a pointer to the type iterated over (value_type)
5657  using pointer = typename std::conditional<std::is_const<BasicJsonType>::value,
5658  typename BasicJsonType::const_pointer,
5659  typename BasicJsonType::pointer>::type;
5660  /// defines a reference to the type iterated over (value_type)
5661  using reference =
5662  typename std::conditional<std::is_const<BasicJsonType>::value,
5663  typename BasicJsonType::const_reference,
5664  typename BasicJsonType::reference>::type;
5665 
5666  /// default constructor
5667  iter_impl() = default;
5668 
5669  /*!
5670  @brief constructor for a given JSON instance
5671  @param[in] object pointer to a JSON object for this iterator
5672  @pre object != nullptr
5673  @post The iterator is initialized; i.e. `m_object != nullptr`.
5674  */
5675  explicit iter_impl(pointer object) noexcept : m_object(object)
5676  {
5677  assert(m_object != nullptr);
5678 
5679  switch (m_object->m_type)
5680  {
5681  case value_t::object:
5682  {
5683  m_it.object_iterator = typename object_t::iterator();
5684  break;
5685  }
5686 
5687  case value_t::array:
5688  {
5689  m_it.array_iterator = typename array_t::iterator();
5690  break;
5691  }
5692 
5693  default:
5694  {
5695  m_it.primitive_iterator = primitive_iterator_t();
5696  break;
5697  }
5698  }
5699  }
5700 
5701  /*!
5702  @note The conventional copy constructor and copy assignment are implicitly
5703  defined. Combined with the following converting constructor and
5704  assignment, they support: (1) copy from iterator to iterator, (2)
5705  copy from const iterator to const iterator, and (3) conversion from
5706  iterator to const iterator. However conversion from const iterator
5707  to iterator is not defined.
5708  */
5709 
5710  /*!
5711  @brief converting constructor
5712  @param[in] other non-const iterator to copy from
5713  @note It is not checked whether @a other is initialized.
5714  */
5715  iter_impl(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept
5716  : m_object(other.m_object), m_it(other.m_it) {}
5717 
5718  /*!
5719  @brief converting assignment
5720  @param[in,out] other non-const iterator to copy from
5721  @return const/non-const iterator
5722  @note It is not checked whether @a other is initialized.
5723  */
5724  iter_impl& operator=(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept
5725  {
5726  m_object = other.m_object;
5727  m_it = other.m_it;
5728  return *this;
5729  }
5730 
5731  private:
5732  /*!
5733  @brief set the iterator to the first value
5734  @pre The iterator is initialized; i.e. `m_object != nullptr`.
5735  */
5736  void set_begin() noexcept
5737  {
5738  assert(m_object != nullptr);
5739 
5740  switch (m_object->m_type)
5741  {
5742  case value_t::object:
5743  {
5744  m_it.object_iterator = m_object->m_value.object->begin();
5745  break;
5746  }
5747 
5748  case value_t::array:
5749  {
5750  m_it.array_iterator = m_object->m_value.array->begin();
5751  break;
5752  }
5753 
5754  case value_t::null:
5755  {
5756  // set to end so begin()==end() is true: null is empty
5757  m_it.primitive_iterator.set_end();
5758  break;
5759  }
5760 
5761  default:
5762  {
5763  m_it.primitive_iterator.set_begin();
5764  break;
5765  }
5766  }
5767  }
5768 
5769  /*!
5770  @brief set the iterator past the last value
5771  @pre The iterator is initialized; i.e. `m_object != nullptr`.
5772  */
5773  void set_end() noexcept
5774  {
5775  assert(m_object != nullptr);
5776 
5777  switch (m_object->m_type)
5778  {
5779  case value_t::object:
5780  {
5781  m_it.object_iterator = m_object->m_value.object->end();
5782  break;
5783  }
5784 
5785  case value_t::array:
5786  {
5787  m_it.array_iterator = m_object->m_value.array->end();
5788  break;
5789  }
5790 
5791  default:
5792  {
5793  m_it.primitive_iterator.set_end();
5794  break;
5795  }
5796  }
5797  }
5798 
5799  public:
5800  /*!
5801  @brief return a reference to the value pointed to by the iterator
5802  @pre The iterator is initialized; i.e. `m_object != nullptr`.
5803  */
5804  reference operator*() const
5805  {
5806  assert(m_object != nullptr);
5807 
5808  switch (m_object->m_type)
5809  {
5810  case value_t::object:
5811  {
5812  assert(m_it.object_iterator != m_object->m_value.object->end());
5813  return m_it.object_iterator->second;
5814  }
5815 
5816  case value_t::array:
5817  {
5818  assert(m_it.array_iterator != m_object->m_value.array->end());
5819  return *m_it.array_iterator;
5820  }
5821 
5822  case value_t::null:
5823  JSON_THROW(invalid_iterator::create(214, "cannot get value"));
5824 
5825  default:
5826  {
5827  if (JSON_LIKELY(m_it.primitive_iterator.is_begin()))
5828  {
5829  return *m_object;
5830  }
5831 
5832  JSON_THROW(invalid_iterator::create(214, "cannot get value"));
5833  }
5834  }
5835  }
5836 
5837  /*!
5838  @brief dereference the iterator
5839  @pre The iterator is initialized; i.e. `m_object != nullptr`.
5840  */
5841  pointer operator->() const
5842  {
5843  assert(m_object != nullptr);
5844 
5845  switch (m_object->m_type)
5846  {
5847  case value_t::object:
5848  {
5849  assert(m_it.object_iterator != m_object->m_value.object->end());
5850  return &(m_it.object_iterator->second);
5851  }
5852 
5853  case value_t::array:
5854  {
5855  assert(m_it.array_iterator != m_object->m_value.array->end());
5856  return &*m_it.array_iterator;
5857  }
5858 
5859  default:
5860  {
5861  if (JSON_LIKELY(m_it.primitive_iterator.is_begin()))
5862  {
5863  return m_object;
5864  }
5865 
5866  JSON_THROW(invalid_iterator::create(214, "cannot get value"));
5867  }
5868  }
5869  }
5870 
5871  /*!
5872  @brief post-increment (it++)
5873  @pre The iterator is initialized; i.e. `m_object != nullptr`.
5874  */
5875  iter_impl const operator++(int)
5876  {
5877  auto result = *this;
5878  ++(*this);
5879  return result;
5880  }
5881 
5882  /*!
5883  @brief pre-increment (++it)
5884  @pre The iterator is initialized; i.e. `m_object != nullptr`.
5885  */
5886  iter_impl& operator++()
5887  {
5888  assert(m_object != nullptr);
5889 
5890  switch (m_object->m_type)
5891  {
5892  case value_t::object:
5893  {
5894  std::advance(m_it.object_iterator, 1);
5895  break;
5896  }
5897 
5898  case value_t::array:
5899  {
5900  std::advance(m_it.array_iterator, 1);
5901  break;
5902  }
5903 
5904  default:
5905  {
5906  ++m_it.primitive_iterator;
5907  break;
5908  }
5909  }
5910 
5911  return *this;
5912  }
5913 
5914  /*!
5915  @brief post-decrement (it--)
5916  @pre The iterator is initialized; i.e. `m_object != nullptr`.
5917  */
5918  iter_impl const operator--(int)
5919  {
5920  auto result = *this;
5921  --(*this);
5922  return result;
5923  }
5924 
5925  /*!
5926  @brief pre-decrement (--it)
5927  @pre The iterator is initialized; i.e. `m_object != nullptr`.
5928  */
5929  iter_impl& operator--()
5930  {
5931  assert(m_object != nullptr);
5932 
5933  switch (m_object->m_type)
5934  {
5935  case value_t::object:
5936  {
5937  std::advance(m_it.object_iterator, -1);
5938  break;
5939  }
5940 
5941  case value_t::array:
5942  {
5943  std::advance(m_it.array_iterator, -1);
5944  break;
5945  }
5946 
5947  default:
5948  {
5949  --m_it.primitive_iterator;
5950  break;
5951  }
5952  }
5953 
5954  return *this;
5955  }
5956 
5957  /*!
5958  @brief comparison: equal
5959  @pre The iterator is initialized; i.e. `m_object != nullptr`.
5960  */
5961  bool operator==(const iter_impl& other) const
5962  {
5963  // if objects are not the same, the comparison is undefined
5964  if (JSON_UNLIKELY(m_object != other.m_object))
5965  {
5966  JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers"));
5967  }
5968 
5969  assert(m_object != nullptr);
5970 
5971  switch (m_object->m_type)
5972  {
5973  case value_t::object:
5974  return (m_it.object_iterator == other.m_it.object_iterator);
5975 
5976  case value_t::array:
5977  return (m_it.array_iterator == other.m_it.array_iterator);
5978 
5979  default:
5980  return (m_it.primitive_iterator == other.m_it.primitive_iterator);
5981  }
5982  }
5983 
5984  /*!
5985  @brief comparison: not equal
5986  @pre The iterator is initialized; i.e. `m_object != nullptr`.
5987  */
5988  bool operator!=(const iter_impl& other) const
5989  {
5990  return not operator==(other);
5991  }
5992 
5993  /*!
5994  @brief comparison: smaller
5995  @pre The iterator is initialized; i.e. `m_object != nullptr`.
5996  */
5997  bool operator<(const iter_impl& other) const
5998  {
5999  // if objects are not the same, the comparison is undefined
6000  if (JSON_UNLIKELY(m_object != other.m_object))
6001  {
6002  JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers"));
6003  }
6004 
6005  assert(m_object != nullptr);
6006 
6007  switch (m_object->m_type)
6008  {
6009  case value_t::object:
6010  JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators"));
6011 
6012  case value_t::array:
6013  return (m_it.array_iterator < other.m_it.array_iterator);
6014 
6015  default:
6016  return (m_it.primitive_iterator < other.m_it.primitive_iterator);
6017  }
6018  }
6019 
6020  /*!
6021  @brief comparison: less than or equal
6022  @pre The iterator is initialized; i.e. `m_object != nullptr`.
6023  */
6024  bool operator<=(const iter_impl& other) const
6025  {
6026  return not other.operator < (*this);
6027  }
6028 
6029  /*!
6030  @brief comparison: greater than
6031  @pre The iterator is initialized; i.e. `m_object != nullptr`.
6032  */
6033  bool operator>(const iter_impl& other) const
6034  {
6035  return not operator<=(other);
6036  }
6037 
6038  /*!
6039  @brief comparison: greater than or equal
6040  @pre The iterator is initialized; i.e. `m_object != nullptr`.
6041  */
6042  bool operator>=(const iter_impl& other) const
6043  {
6044  return not operator<(other);
6045  }
6046 
6047  /*!
6048  @brief add to iterator
6049  @pre The iterator is initialized; i.e. `m_object != nullptr`.
6050  */
6051  iter_impl& operator+=(difference_type i)
6052  {
6053  assert(m_object != nullptr);
6054 
6055  switch (m_object->m_type)
6056  {
6057  case value_t::object:
6058  JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators"));
6059 
6060  case value_t::array:
6061  {
6062  std::advance(m_it.array_iterator, i);
6063  break;
6064  }
6065 
6066  default:
6067  {
6068  m_it.primitive_iterator += i;
6069  break;
6070  }
6071  }
6072 
6073  return *this;
6074  }
6075 
6076  /*!
6077  @brief subtract from iterator
6078  @pre The iterator is initialized; i.e. `m_object != nullptr`.
6079  */
6080  iter_impl& operator-=(difference_type i)
6081  {
6082  return operator+=(-i);
6083  }
6084 
6085  /*!
6086  @brief add to iterator
6087  @pre The iterator is initialized; i.e. `m_object != nullptr`.
6088  */
6089  iter_impl operator+(difference_type i) const
6090  {
6091  auto result = *this;
6092  result += i;
6093  return result;
6094  }
6095 
6096  /*!
6097  @brief addition of distance and iterator
6098  @pre The iterator is initialized; i.e. `m_object != nullptr`.
6099  */
6100  friend iter_impl operator+(difference_type i, const iter_impl& it)
6101  {
6102  auto result = it;
6103  result += i;
6104  return result;
6105  }
6106 
6107  /*!
6108  @brief subtract from iterator
6109  @pre The iterator is initialized; i.e. `m_object != nullptr`.
6110  */
6111  iter_impl operator-(difference_type i) const
6112  {
6113  auto result = *this;
6114  result -= i;
6115  return result;
6116  }
6117 
6118  /*!
6119  @brief return difference
6120  @pre The iterator is initialized; i.e. `m_object != nullptr`.
6121  */
6122  difference_type operator-(const iter_impl& other) const
6123  {
6124  assert(m_object != nullptr);
6125 
6126  switch (m_object->m_type)
6127  {
6128  case value_t::object:
6129  JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators"));
6130 
6131  case value_t::array:
6132  return m_it.array_iterator - other.m_it.array_iterator;
6133 
6134  default:
6135  return m_it.primitive_iterator - other.m_it.primitive_iterator;
6136  }
6137  }
6138 
6139  /*!
6140  @brief access to successor
6141  @pre The iterator is initialized; i.e. `m_object != nullptr`.
6142  */
6143  reference operator[](difference_type n) const
6144  {
6145  assert(m_object != nullptr);
6146 
6147  switch (m_object->m_type)
6148  {
6149  case value_t::object:
6150  JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators"));
6151 
6152  case value_t::array:
6153  return *std::next(m_it.array_iterator, n);
6154 
6155  case value_t::null:
6156  JSON_THROW(invalid_iterator::create(214, "cannot get value"));
6157 
6158  default:
6159  {
6160  if (JSON_LIKELY(m_it.primitive_iterator.get_value() == -n))
6161  {
6162  return *m_object;
6163  }
6164 
6165  JSON_THROW(invalid_iterator::create(214, "cannot get value"));
6166  }
6167  }
6168  }
6169 
6170  /*!
6171  @brief return the key of an object iterator
6172  @pre The iterator is initialized; i.e. `m_object != nullptr`.
6173  */
6174  const typename object_t::key_type& key() const
6175  {
6176  assert(m_object != nullptr);
6177 
6178  if (JSON_LIKELY(m_object->is_object()))
6179  {
6180  return m_it.object_iterator->first;
6181  }
6182 
6183  JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators"));
6184  }
6185 
6186  /*!
6187  @brief return the value of an iterator
6188  @pre The iterator is initialized; i.e. `m_object != nullptr`.
6189  */
6190  reference value() const
6191  {
6192  return operator*();
6193  }
6194 
6195  private:
6196  /// associated JSON instance
6197  pointer m_object = nullptr;
6198  /// the actual iterator of the associated instance
6199  internal_iterator<typename std::remove_const<BasicJsonType>::type> m_it;
6200 };
6201 } // namespace detail
6202 } // namespace nlohmann
6203 // #include <nlohmann/detail/iterators/iteration_proxy.hpp>
6204 
6205 // #include <nlohmann/detail/iterators/json_reverse_iterator.hpp>
6206 
6207 
6208 #include <cstddef> // ptrdiff_t
6209 #include <iterator> // reverse_iterator
6210 #include <utility> // declval
6211 
6212 namespace nlohmann
6213 {
6214 namespace detail
6215 {
6216 //////////////////////
6217 // reverse_iterator //
6218 //////////////////////
6219 
6220 /*!
6221 @brief a template for a reverse iterator class
6222 
6223 @tparam Base the base iterator type to reverse. Valid types are @ref
6224 iterator (to create @ref reverse_iterator) and @ref const_iterator (to
6225 create @ref const_reverse_iterator).
6226 
6227 @requirement The class satisfies the following concept requirements:
6228 -
6229 [BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
6230  The iterator that can be moved can be moved in both directions (i.e.
6231  incremented and decremented).
6232 - [OutputIterator](https://en.cppreference.com/w/cpp/named_req/OutputIterator):
6233  It is possible to write to the pointed-to element (only if @a Base is
6234  @ref iterator).
6235 
6236 @since version 1.0.0
6237 */
6238 template<typename Base>
6239 class json_reverse_iterator : public std::reverse_iterator<Base>
6240 {
6241  public:
6242  using difference_type = std::ptrdiff_t;
6243  /// shortcut to the reverse iterator adapter
6244  using base_iterator = std::reverse_iterator<Base>;
6245  /// the reference type for the pointed-to element
6246  using reference = typename Base::reference;
6247 
6248  /// create reverse iterator from iterator
6249  explicit json_reverse_iterator(const typename base_iterator::iterator_type& it) noexcept
6250  : base_iterator(it) {}
6251 
6252  /// create reverse iterator from base class
6253  explicit json_reverse_iterator(const base_iterator& it) noexcept : base_iterator(it) {}
6254 
6255  /// post-increment (it++)
6256  json_reverse_iterator const operator++(int)
6257  {
6258  return static_cast<json_reverse_iterator>(base_iterator::operator++(1));
6259  }
6260 
6261  /// pre-increment (++it)
6262  json_reverse_iterator& operator++()
6263  {
6264  return static_cast<json_reverse_iterator&>(base_iterator::operator++());
6265  }
6266 
6267  /// post-decrement (it--)
6268  json_reverse_iterator const operator--(int)
6269  {
6270  return static_cast<json_reverse_iterator>(base_iterator::operator--(1));
6271  }
6272 
6273  /// pre-decrement (--it)
6274  json_reverse_iterator& operator--()
6275  {
6276  return static_cast<json_reverse_iterator&>(base_iterator::operator--());
6277  }
6278 
6279  /// add to iterator
6280  json_reverse_iterator& operator+=(difference_type i)
6281  {
6282  return static_cast<json_reverse_iterator&>(base_iterator::operator+=(i));
6283  }
6284 
6285  /// add to iterator
6286  json_reverse_iterator operator+(difference_type i) const
6287  {
6288  return static_cast<json_reverse_iterator>(base_iterator::operator+(i));
6289  }
6290 
6291  /// subtract from iterator
6292  json_reverse_iterator operator-(difference_type i) const
6293  {
6294  return static_cast<json_reverse_iterator>(base_iterator::operator-(i));
6295  }
6296 
6297  /// return difference
6298  difference_type operator-(const json_reverse_iterator& other) const
6299  {
6300  return base_iterator(*this) - base_iterator(other);
6301  }
6302 
6303  /// access to successor
6304  reference operator[](difference_type n) const
6305  {
6306  return *(this->operator+(n));
6307  }
6308 
6309  /// return the key of an object iterator
6310  auto key() const -> decltype(std::declval<Base>().key())
6311  {
6312  auto it = --this->base();
6313  return it.key();
6314  }
6315 
6316  /// return the value of an iterator
6317  reference value() const
6318  {
6319  auto it = --this->base();
6320  return it.operator * ();
6321  }
6322 };
6323 } // namespace detail
6324 } // namespace nlohmann
6325 
6326 // #include <nlohmann/detail/output/output_adapters.hpp>
6327 
6328 
6329 #include <algorithm> // copy
6330 #include <cstddef> // size_t
6331 #include <ios> // streamsize
6332 #include <iterator> // back_inserter
6333 #include <memory> // shared_ptr, make_shared
6334 #include <ostream> // basic_ostream
6335 #include <string> // basic_string
6336 #include <vector> // vector
6337 
6338 namespace nlohmann
6339 {
6340 namespace detail
6341 {
6342 /// abstract output adapter interface
6343 template<typename CharType> struct output_adapter_protocol
6344 {
6345  virtual void write_character(CharType c) = 0;
6346  virtual void write_characters(const CharType* s, std::size_t length) = 0;
6347  virtual ~output_adapter_protocol() = default;
6348 };
6349 
6350 /// a type to simplify interfaces
6351 template<typename CharType>
6352 using output_adapter_t = std::shared_ptr<output_adapter_protocol<CharType>>;
6353 
6354 /// output adapter for byte vectors
6355 template<typename CharType>
6356 class output_vector_adapter : public output_adapter_protocol<CharType>
6357 {
6358  public:
6359  explicit output_vector_adapter(std::vector<CharType>& vec) noexcept
6360  : v(vec)
6361  {}
6362 
6363  void write_character(CharType c) override
6364  {
6365  v.push_back(c);
6366  }
6367 
6368  void write_characters(const CharType* s, std::size_t length) override
6369  {
6370  std::copy(s, s + length, std::back_inserter(v));
6371  }
6372 
6373  private:
6374  std::vector<CharType>& v;
6375 };
6376 
6377 /// output adapter for output streams
6378 template<typename CharType>
6379 class output_stream_adapter : public output_adapter_protocol<CharType>
6380 {
6381  public:
6382  explicit output_stream_adapter(std::basic_ostream<CharType>& s) noexcept
6383  : stream(s)
6384  {}
6385 
6386  void write_character(CharType c) override
6387  {
6388  stream.put(c);
6389  }
6390 
6391  void write_characters(const CharType* s, std::size_t length) override
6392  {
6393  stream.write(s, static_cast<std::streamsize>(length));
6394  }
6395 
6396  private:
6397  std::basic_ostream<CharType>& stream;
6398 };
6399 
6400 /// output adapter for basic_string
6401 template<typename CharType, typename StringType = std::basic_string<CharType>>
6402 class output_string_adapter : public output_adapter_protocol<CharType>
6403 {
6404  public:
6405  explicit output_string_adapter(StringType& s) noexcept
6406  : str(s)
6407  {}
6408 
6409  void write_character(CharType c) override
6410  {
6411  str.push_back(c);
6412  }
6413 
6414  void write_characters(const CharType* s, std::size_t length) override
6415  {
6416  str.append(s, length);
6417  }
6418 
6419  private:
6420  StringType& str;
6421 };
6422 
6423 template<typename CharType, typename StringType = std::basic_string<CharType>>
6424 class output_adapter
6425 {
6426  public:
6427  output_adapter(std::vector<CharType>& vec)
6428  : oa(std::make_shared<output_vector_adapter<CharType>>(vec)) {}
6429 
6430  output_adapter(std::basic_ostream<CharType>& s)
6431  : oa(std::make_shared<output_stream_adapter<CharType>>(s)) {}
6432 
6433  output_adapter(StringType& s)
6434  : oa(std::make_shared<output_string_adapter<CharType, StringType>>(s)) {}
6435 
6436  operator output_adapter_t<CharType>()
6437  {
6438  return oa;
6439  }
6440 
6441  private:
6442  output_adapter_t<CharType> oa = nullptr;
6443 };
6444 } // namespace detail
6445 } // namespace nlohmann
6446 
6447 // #include <nlohmann/detail/input/binary_reader.hpp>
6448 
6449 
6450 #include <algorithm> // generate_n
6451 #include <array> // array
6452 #include <cassert> // assert
6453 #include <cmath> // ldexp
6454 #include <cstddef> // size_t
6455 #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
6456 #include <cstdio> // snprintf
6457 #include <cstring> // memcpy
6458 #include <iterator> // back_inserter
6459 #include <limits> // numeric_limits
6460 #include <string> // char_traits, string
6461 #include <utility> // make_pair, move
6462 
6463 // #include <nlohmann/detail/input/input_adapters.hpp>
6464 
6465 // #include <nlohmann/detail/input/json_sax.hpp>
6466 
6467 // #include <nlohmann/detail/exceptions.hpp>
6468 
6469 // #include <nlohmann/detail/macro_scope.hpp>
6470 
6471 // #include <nlohmann/detail/meta/is_sax.hpp>
6472 
6473 // #include <nlohmann/detail/value_t.hpp>
6474 
6475 
6476 namespace nlohmann
6477 {
6478 namespace detail
6479 {
6480 ///////////////////
6481 // binary reader //
6482 ///////////////////
6483 
6484 /*!
6485 @brief deserialization of CBOR, MessagePack, and UBJSON values
6486 */
6487 template<typename BasicJsonType, typename SAX = json_sax_dom_parser<BasicJsonType>>
6488 class binary_reader
6489 {
6490  using number_integer_t = typename BasicJsonType::number_integer_t;
6491  using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
6492  using number_float_t = typename BasicJsonType::number_float_t;
6493  using string_t = typename BasicJsonType::string_t;
6494  using json_sax_t = SAX;
6495 
6496  public:
6497  /*!
6498  @brief create a binary reader
6499 
6500  @param[in] adapter input adapter to read from
6501  */
6502  explicit binary_reader(input_adapter_t adapter) : ia(std::move(adapter))
6503  {
6504  (void)detail::is_sax_static_asserts<SAX, BasicJsonType> {};
6505  assert(ia);
6506  }
6507 
6508  /*!
6509  @param[in] format the binary format to parse
6510  @param[in] sax_ a SAX event processor
6511  @param[in] strict whether to expect the input to be consumed completed
6512 
6513  @return
6514  */
6515  bool sax_parse(const input_format_t format,
6516  json_sax_t* sax_,
6517  const bool strict = true)
6518  {
6519  sax = sax_;
6520  bool result = false;
6521 
6522  switch (format)
6523  {
6524  case input_format_t::bson:
6525  result = parse_bson_internal();
6526  break;
6527 
6528  case input_format_t::cbor:
6529  result = parse_cbor_internal();
6530  break;
6531 
6532  case input_format_t::msgpack:
6533  result = parse_msgpack_internal();
6534  break;
6535 
6536  case input_format_t::ubjson:
6537  result = parse_ubjson_internal();
6538  break;
6539 
6540  // LCOV_EXCL_START
6541  default:
6542  assert(false);
6543  // LCOV_EXCL_STOP
6544  }
6545 
6546  // strict mode: next byte must be EOF
6547  if (result and strict)
6548  {
6549  if (format == input_format_t::ubjson)
6550  {
6551  get_ignore_noop();
6552  }
6553  else
6554  {
6555  get();
6556  }
6557 
6558  if (JSON_UNLIKELY(current != std::char_traits<char>::eof()))
6559  {
6560  return sax->parse_error(chars_read, get_token_string(),
6561  parse_error::create(110, chars_read, exception_message(format, "expected end of input; last byte: 0x" + get_token_string(), "value")));
6562  }
6563  }
6564 
6565  return result;
6566  }
6567 
6568  /*!
6569  @brief determine system byte order
6570 
6571  @return true if and only if system's byte order is little endian
6572 
6573  @note from http://stackoverflow.com/a/1001328/266378
6574  */
6575  static constexpr bool little_endianess(int num = 1) noexcept
6576  {
6577  return (*reinterpret_cast<char*>(&num) == 1);
6578  }
6579 
6580  private:
6581  //////////
6582  // BSON //
6583  //////////
6584 
6585  /*!
6586  @brief Reads in a BSON-object and passes it to the SAX-parser.
6587  @return whether a valid BSON-value was passed to the SAX parser
6588  */
6589  bool parse_bson_internal()
6590  {
6591  std::int32_t document_size;
6592  get_number<std::int32_t, true>(input_format_t::bson, document_size);
6593 
6594  if (JSON_UNLIKELY(not sax->start_object(std::size_t(-1))))
6595  {
6596  return false;
6597  }
6598 
6599  if (JSON_UNLIKELY(not parse_bson_element_list(/*is_array*/false)))
6600  {
6601  return false;
6602  }
6603 
6604  return sax->end_object();
6605  }
6606 
6607  /*!
6608  @brief Parses a C-style string from the BSON input.
6609  @param[in, out] result A reference to the string variable where the read
6610  string is to be stored.
6611  @return `true` if the \x00-byte indicating the end of the string was
6612  encountered before the EOF; false` indicates an unexpected EOF.
6613  */
6614  bool get_bson_cstr(string_t& result)
6615  {
6616  auto out = std::back_inserter(result);
6617  while (true)
6618  {
6619  get();
6620  if (JSON_UNLIKELY(not unexpect_eof(input_format_t::bson, "cstring")))
6621  {
6622  return false;
6623  }
6624  if (current == 0x00)
6625  {
6626  return true;
6627  }
6628  *out++ = static_cast<char>(current);
6629  }
6630 
6631  return true;
6632  }
6633 
6634  /*!
6635  @brief Parses a zero-terminated string of length @a len from the BSON
6636  input.
6637  @param[in] len The length (including the zero-byte at the end) of the
6638  string to be read.
6639  @param[in, out] result A reference to the string variable where the read
6640  string is to be stored.
6641  @tparam NumberType The type of the length @a len
6642  @pre len >= 1
6643  @return `true` if the string was successfully parsed
6644  */
6645  template<typename NumberType>
6646  bool get_bson_string(const NumberType len, string_t& result)
6647  {
6648  if (JSON_UNLIKELY(len < 1))
6649  {
6650  auto last_token = get_token_string();
6651  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")));
6652  }
6653 
6654  return get_string(input_format_t::bson, len - static_cast<NumberType>(1), result) and get() != std::char_traits<char>::eof();
6655  }
6656 
6657  /*!
6658  @brief Read a BSON document element of the given @a element_type.
6659  @param[in] element_type The BSON element type, c.f. http://bsonspec.org/spec.html
6660  @param[in] element_type_parse_position The position in the input stream,
6661  where the `element_type` was read.
6662  @warning Not all BSON element types are supported yet. An unsupported
6663  @a element_type will give rise to a parse_error.114:
6664  Unsupported BSON record type 0x...
6665  @return whether a valid BSON-object/array was passed to the SAX parser
6666  */
6667  bool parse_bson_element_internal(const int element_type,
6668  const std::size_t element_type_parse_position)
6669  {
6670  switch (element_type)
6671  {
6672  case 0x01: // double
6673  {
6674  double number;
6675  return get_number<double, true>(input_format_t::bson, number) and sax->number_float(static_cast<number_float_t>(number), "");
6676  }
6677 
6678  case 0x02: // string
6679  {
6680  std::int32_t len;
6681  string_t value;
6682  return get_number<std::int32_t, true>(input_format_t::bson, len) and get_bson_string(len, value) and sax->string(value);
6683  }
6684 
6685  case 0x03: // object
6686  {
6687  return parse_bson_internal();
6688  }
6689 
6690  case 0x04: // array
6691  {
6692  return parse_bson_array();
6693  }
6694 
6695  case 0x08: // boolean
6696  {
6697  return sax->boolean(get() != 0);
6698  }
6699 
6700  case 0x0A: // null
6701  {
6702  return sax->null();
6703  }
6704 
6705  case 0x10: // int32
6706  {
6707  std::int32_t value;
6708  return get_number<std::int32_t, true>(input_format_t::bson, value) and sax->number_integer(value);
6709  }
6710 
6711  case 0x12: // int64
6712  {
6713  std::int64_t value;
6714  return get_number<std::int64_t, true>(input_format_t::bson, value) and sax->number_integer(value);
6715  }
6716 
6717  default: // anything else not supported (yet)
6718  {
6719  char cr[3];
6720  (std::snprintf)(cr, sizeof(cr), "%.2hhX", static_cast<unsigned char>(element_type));
6721  return sax->parse_error(element_type_parse_position, std::string(cr), parse_error::create(114, element_type_parse_position, "Unsupported BSON record type 0x" + std::string(cr)));
6722  }
6723  }
6724  }
6725 
6726  /*!
6727  @brief Read a BSON element list (as specified in the BSON-spec)
6728 
6729  The same binary layout is used for objects and arrays, hence it must be
6730  indicated with the argument @a is_array which one is expected
6731  (true --> array, false --> object).
6732 
6733  @param[in] is_array Determines if the element list being read is to be
6734  treated as an object (@a is_array == false), or as an
6735  array (@a is_array == true).
6736  @return whether a valid BSON-object/array was passed to the SAX parser
6737  */
6738  bool parse_bson_element_list(const bool is_array)
6739  {
6740  string_t key;
6741  while (int element_type = get())
6742  {
6743  if (JSON_UNLIKELY(not unexpect_eof(input_format_t::bson, "element list")))
6744  {
6745  return false;
6746  }
6747 
6748  const std::size_t element_type_parse_position = chars_read;
6749  if (JSON_UNLIKELY(not get_bson_cstr(key)))
6750  {
6751  return false;
6752  }
6753 
6754  if (not is_array)
6755  {
6756  if (not sax->key(key))
6757  {
6758  return false;
6759  }
6760  }
6761 
6762  if (JSON_UNLIKELY(not parse_bson_element_internal(element_type, element_type_parse_position)))
6763  {
6764  return false;
6765  }
6766 
6767  // get_bson_cstr only appends
6768  key.clear();
6769  }
6770 
6771  return true;
6772  }
6773 
6774  /*!
6775  @brief Reads an array from the BSON input and passes it to the SAX-parser.
6776  @return whether a valid BSON-array was passed to the SAX parser
6777  */
6778  bool parse_bson_array()
6779  {
6780  std::int32_t document_size;
6781  get_number<std::int32_t, true>(input_format_t::bson, document_size);
6782 
6783  if (JSON_UNLIKELY(not sax->start_array(std::size_t(-1))))
6784  {
6785  return false;
6786  }
6787 
6788  if (JSON_UNLIKELY(not parse_bson_element_list(/*is_array*/true)))
6789  {
6790  return false;
6791  }
6792 
6793  return sax->end_array();
6794  }
6795 
6796  //////////
6797  // CBOR //
6798  //////////
6799 
6800  /*!
6801  @param[in] get_char whether a new character should be retrieved from the
6802  input (true, default) or whether the last read
6803  character should be considered instead
6804 
6805  @return whether a valid CBOR value was passed to the SAX parser
6806  */
6807  bool parse_cbor_internal(const bool get_char = true)
6808  {
6809  switch (get_char ? get() : current)
6810  {
6811  // EOF
6812  case std::char_traits<char>::eof():
6813  return unexpect_eof(input_format_t::cbor, "value");
6814 
6815  // Integer 0x00..0x17 (0..23)
6816  case 0x00:
6817  case 0x01:
6818  case 0x02:
6819  case 0x03:
6820  case 0x04:
6821  case 0x05:
6822  case 0x06:
6823  case 0x07:
6824  case 0x08:
6825  case 0x09:
6826  case 0x0A:
6827  case 0x0B:
6828  case 0x0C:
6829  case 0x0D:
6830  case 0x0E:
6831  case 0x0F:
6832  case 0x10:
6833  case 0x11:
6834  case 0x12:
6835  case 0x13:
6836  case 0x14:
6837  case 0x15:
6838  case 0x16:
6839  case 0x17:
6840  return sax->number_unsigned(static_cast<number_unsigned_t>(current));
6841 
6842  case 0x18: // Unsigned integer (one-byte uint8_t follows)
6843  {
6844  uint8_t number;
6845  return get_number(input_format_t::cbor, number) and sax->number_unsigned(number);
6846  }
6847 
6848  case 0x19: // Unsigned integer (two-byte uint16_t follows)
6849  {
6850  uint16_t number;
6851  return get_number(input_format_t::cbor, number) and sax->number_unsigned(number);
6852  }
6853 
6854  case 0x1A: // Unsigned integer (four-byte uint32_t follows)
6855  {
6856  uint32_t number;
6857  return get_number(input_format_t::cbor, number) and sax->number_unsigned(number);
6858  }
6859 
6860  case 0x1B: // Unsigned integer (eight-byte uint64_t follows)
6861  {
6862  uint64_t number;
6863  return get_number(input_format_t::cbor, number) and sax->number_unsigned(number);
6864  }
6865 
6866  // Negative integer -1-0x00..-1-0x17 (-1..-24)
6867  case 0x20:
6868  case 0x21:
6869  case 0x22:
6870  case 0x23:
6871  case 0x24:
6872  case 0x25:
6873  case 0x26:
6874  case 0x27:
6875  case 0x28:
6876  case 0x29:
6877  case 0x2A:
6878  case 0x2B:
6879  case 0x2C:
6880  case 0x2D:
6881  case 0x2E:
6882  case 0x2F:
6883  case 0x30:
6884  case 0x31:
6885  case 0x32:
6886  case 0x33:
6887  case 0x34:
6888  case 0x35:
6889  case 0x36:
6890  case 0x37:
6891  return sax->number_integer(static_cast<int8_t>(0x20 - 1 - current));
6892 
6893  case 0x38: // Negative integer (one-byte uint8_t follows)
6894  {
6895  uint8_t number;
6896  return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast<number_integer_t>(-1) - number);
6897  }
6898 
6899  case 0x39: // Negative integer -1-n (two-byte uint16_t follows)
6900  {
6901  uint16_t number;
6902  return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast<number_integer_t>(-1) - number);
6903  }
6904 
6905  case 0x3A: // Negative integer -1-n (four-byte uint32_t follows)
6906  {
6907  uint32_t number;
6908  return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast<number_integer_t>(-1) - number);
6909  }
6910 
6911  case 0x3B: // Negative integer -1-n (eight-byte uint64_t follows)
6912  {
6913  uint64_t number;
6914  return get_number(input_format_t::cbor, number) and sax->number_integer(static_cast<number_integer_t>(-1)
6915  - static_cast<number_integer_t>(number));
6916  }
6917 
6918  // UTF-8 string (0x00..0x17 bytes follow)
6919  case 0x60:
6920  case 0x61:
6921  case 0x62:
6922  case 0x63:
6923  case 0x64:
6924  case 0x65:
6925  case 0x66:
6926  case 0x67:
6927  case 0x68:
6928  case 0x69:
6929  case 0x6A:
6930  case 0x6B:
6931  case 0x6C:
6932  case 0x6D:
6933  case 0x6E:
6934  case 0x6F:
6935  case 0x70:
6936  case 0x71:
6937  case 0x72:
6938  case 0x73:
6939  case 0x74:
6940  case 0x75:
6941  case 0x76:
6942  case 0x77:
6943  case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
6944  case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
6945  case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
6946  case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
6947  case 0x7F: // UTF-8 string (indefinite length)
6948  {
6949  string_t s;
6950  return get_cbor_string(s) and sax->string(s);
6951  }
6952 
6953  // array (0x00..0x17 data items follow)
6954  case 0x80:
6955  case 0x81:
6956  case 0x82:
6957  case 0x83:
6958  case 0x84:
6959  case 0x85:
6960  case 0x86:
6961  case 0x87:
6962  case 0x88:
6963  case 0x89:
6964  case 0x8A:
6965  case 0x8B:
6966  case 0x8C:
6967  case 0x8D:
6968  case 0x8E:
6969  case 0x8F:
6970  case 0x90:
6971  case 0x91:
6972  case 0x92:
6973  case 0x93:
6974  case 0x94:
6975  case 0x95:
6976  case 0x96:
6977  case 0x97:
6978  return get_cbor_array(static_cast<std::size_t>(current & 0x1F));
6979 
6980  case 0x98: // array (one-byte uint8_t for n follows)
6981  {
6982  uint8_t len;
6983  return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast<std::size_t>(len));
6984  }
6985 
6986  case 0x99: // array (two-byte uint16_t for n follow)
6987  {
6988  uint16_t len;
6989  return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast<std::size_t>(len));
6990  }
6991 
6992  case 0x9A: // array (four-byte uint32_t for n follow)
6993  {
6994  uint32_t len;
6995  return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast<std::size_t>(len));
6996  }
6997 
6998  case 0x9B: // array (eight-byte uint64_t for n follow)
6999  {
7000  uint64_t len;
7001  return get_number(input_format_t::cbor, len) and get_cbor_array(static_cast<std::size_t>(len));
7002  }
7003 
7004  case 0x9F: // array (indefinite length)
7005  return get_cbor_array(std::size_t(-1));
7006 
7007  // map (0x00..0x17 pairs of data items follow)
7008  case 0xA0:
7009  case 0xA1:
7010  case 0xA2:
7011  case 0xA3:
7012  case 0xA4:
7013  case 0xA5:
7014  case 0xA6:
7015  case 0xA7:
7016  case 0xA8:
7017  case 0xA9:
7018  case 0xAA:
7019  case 0xAB:
7020  case 0xAC:
7021  case 0xAD:
7022  case 0xAE:
7023  case 0xAF:
7024  case 0xB0:
7025  case 0xB1:
7026  case 0xB2:
7027  case 0xB3:
7028  case 0xB4:
7029  case 0xB5:
7030  case 0xB6:
7031  case 0xB7:
7032  return get_cbor_object(static_cast<std::size_t>(current & 0x1F));
7033 
7034  case 0xB8: // map (one-byte uint8_t for n follows)
7035  {
7036  uint8_t len;
7037  return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast<std::size_t>(len));
7038  }
7039 
7040  case 0xB9: // map (two-byte uint16_t for n follow)
7041  {
7042  uint16_t len;
7043  return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast<std::size_t>(len));
7044  }
7045 
7046  case 0xBA: // map (four-byte uint32_t for n follow)
7047  {
7048  uint32_t len;
7049  return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast<std::size_t>(len));
7050  }
7051 
7052  case 0xBB: // map (eight-byte uint64_t for n follow)
7053  {
7054  uint64_t len;
7055  return get_number(input_format_t::cbor, len) and get_cbor_object(static_cast<std::size_t>(len));
7056  }
7057 
7058  case 0xBF: // map (indefinite length)
7059  return get_cbor_object(std::size_t(-1));
7060 
7061  case 0xF4: // false
7062  return sax->boolean(false);
7063 
7064  case 0xF5: // true
7065  return sax->boolean(true);
7066 
7067  case 0xF6: // null
7068  return sax->null();
7069 
7070  case 0xF9: // Half-Precision Float (two-byte IEEE 754)
7071  {
7072  const int byte1_raw = get();
7073  if (JSON_UNLIKELY(not unexpect_eof(input_format_t::cbor, "number")))
7074  {
7075  return false;
7076  }
7077  const int byte2_raw = get();
7078  if (JSON_UNLIKELY(not unexpect_eof(input_format_t::cbor, "number")))
7079  {
7080  return false;
7081  }
7082 
7083  const auto byte1 = static_cast<unsigned char>(byte1_raw);
7084  const auto byte2 = static_cast<unsigned char>(byte2_raw);
7085 
7086  // code from RFC 7049, Appendix D, Figure 3:
7087  // As half-precision floating-point numbers were only added
7088  // to IEEE 754 in 2008, today's programming platforms often
7089  // still only have limited support for them. It is very
7090  // easy to include at least decoding support for them even
7091  // without such support. An example of a small decoder for
7092  // half-precision floating-point numbers in the C language
7093  // is shown in Fig. 3.
7094  const int half = (byte1 << 8) + byte2;
7095  const double val = [&half]
7096  {
7097  const int exp = (half >> 10) & 0x1F;
7098  const int mant = half & 0x3FF;
7099  assert(0 <= exp and exp <= 32);
7100  assert(0 <= mant and mant <= 1024);
7101  switch (exp)
7102  {
7103  case 0:
7104  return std::ldexp(mant, -24);
7105  case 31:
7106  return (mant == 0)
7107  ? std::numeric_limits<double>::infinity()
7108  : std::numeric_limits<double>::quiet_NaN();
7109  default:
7110  return std::ldexp(mant + 1024, exp - 25);
7111  }
7112  }();
7113  return sax->number_float((half & 0x8000) != 0
7114  ? static_cast<number_float_t>(-val)
7115  : static_cast<number_float_t>(val), "");
7116  }
7117 
7118  case 0xFA: // Single-Precision Float (four-byte IEEE 754)
7119  {
7120  float number;
7121  return get_number(input_format_t::cbor, number) and sax->number_float(static_cast<number_float_t>(number), "");
7122  }
7123 
7124  case 0xFB: // Double-Precision Float (eight-byte IEEE 754)
7125  {
7126  double number;
7127  return get_number(input_format_t::cbor, number) and sax->number_float(static_cast<number_float_t>(number), "");
7128  }
7129 
7130  default: // anything else (0xFF is handled inside the other types)
7131  {
7132  auto last_token = get_token_string();
7133  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")));
7134  }
7135  }
7136  }
7137 
7138  /*!
7139  @brief reads a CBOR string
7140 
7141  This function first reads starting bytes to determine the expected
7142  string length and then copies this number of bytes into a string.
7143  Additionally, CBOR's strings with indefinite lengths are supported.
7144 
7145  @param[out] result created string
7146 
7147  @return whether string creation completed
7148  */
7149  bool get_cbor_string(string_t& result)
7150  {
7151  if (JSON_UNLIKELY(not unexpect_eof(input_format_t::cbor, "string")))
7152  {
7153  return false;
7154  }
7155 
7156  switch (current)
7157  {
7158  // UTF-8 string (0x00..0x17 bytes follow)
7159  case 0x60:
7160  case 0x61:
7161  case 0x62:
7162  case 0x63:
7163  case 0x64:
7164  case 0x65:
7165  case 0x66:
7166  case 0x67:
7167  case 0x68:
7168  case 0x69:
7169  case 0x6A:
7170  case 0x6B:
7171  case 0x6C:
7172  case 0x6D:
7173  case 0x6E:
7174  case 0x6F:
7175  case 0x70:
7176  case 0x71:
7177  case 0x72:
7178  case 0x73:
7179  case 0x74:
7180  case 0x75:
7181  case 0x76:
7182  case 0x77:
7183  {
7184  return get_string(input_format_t::cbor, current & 0x1F, result);
7185  }
7186 
7187  case 0x78: // UTF-8 string (one-byte uint8_t for n follows)
7188  {
7189  uint8_t len;
7190  return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result);
7191  }
7192 
7193  case 0x79: // UTF-8 string (two-byte uint16_t for n follow)
7194  {
7195  uint16_t len;
7196  return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result);
7197  }
7198 
7199  case 0x7A: // UTF-8 string (four-byte uint32_t for n follow)
7200  {
7201  uint32_t len;
7202  return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result);
7203  }
7204 
7205  case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow)
7206  {
7207  uint64_t len;
7208  return get_number(input_format_t::cbor, len) and get_string(input_format_t::cbor, len, result);
7209  }
7210 
7211  case 0x7F: // UTF-8 string (indefinite length)
7212  {
7213  while (get() != 0xFF)
7214  {
7215  string_t chunk;
7216  if (not get_cbor_string(chunk))
7217  {
7218  return false;
7219  }
7220  result.append(chunk);
7221  }
7222  return true;
7223  }
7224 
7225  default:
7226  {
7227  auto last_token = get_token_string();
7228  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")));
7229  }
7230  }
7231  }
7232 
7233  /*!
7234  @param[in] len the length of the array or std::size_t(-1) for an
7235  array of indefinite size
7236  @return whether array creation completed
7237  */
7238  bool get_cbor_array(const std::size_t len)
7239  {
7240  if (JSON_UNLIKELY(not sax->start_array(len)))
7241  {
7242  return false;
7243  }
7244 
7245  if (len != std::size_t(-1))
7246  {
7247  for (std::size_t i = 0; i < len; ++i)
7248  {
7249  if (JSON_UNLIKELY(not parse_cbor_internal()))
7250  {
7251  return false;
7252  }
7253  }
7254  }
7255  else
7256  {
7257  while (get() != 0xFF)
7258  {
7259  if (JSON_UNLIKELY(not parse_cbor_internal(false)))
7260  {
7261  return false;
7262  }
7263  }
7264  }
7265 
7266  return sax->end_array();
7267  }
7268 
7269  /*!
7270  @param[in] len the length of the object or std::size_t(-1) for an
7271  object of indefinite size
7272  @return whether object creation completed
7273  */
7274  bool get_cbor_object(const std::size_t len)
7275  {
7276  if (not JSON_UNLIKELY(sax->start_object(len)))
7277  {
7278  return false;
7279  }
7280 
7281  string_t key;
7282  if (len != std::size_t(-1))
7283  {
7284  for (std::size_t i = 0; i < len; ++i)
7285  {
7286  get();
7287  if (JSON_UNLIKELY(not get_cbor_string(key) or not sax->key(key)))
7288  {
7289  return false;
7290  }
7291 
7292  if (JSON_UNLIKELY(not parse_cbor_internal()))
7293  {
7294  return false;
7295  }
7296  key.clear();
7297  }
7298  }
7299  else
7300  {
7301  while (get() != 0xFF)
7302  {
7303  if (JSON_UNLIKELY(not get_cbor_string(key) or not sax->key(key)))
7304  {
7305  return false;
7306  }
7307 
7308  if (JSON_UNLIKELY(not parse_cbor_internal()))
7309  {
7310  return false;
7311  }
7312  key.clear();
7313  }
7314  }
7315 
7316  return sax->end_object();
7317  }
7318 
7319  /////////////
7320  // MsgPack //
7321  /////////////
7322 
7323  /*!
7324  @return whether a valid MessagePack value was passed to the SAX parser
7325  */
7326  bool parse_msgpack_internal()
7327  {
7328  switch (get())
7329  {
7330  // EOF
7331  case std::char_traits<char>::eof():
7332  return unexpect_eof(input_format_t::msgpack, "value");
7333 
7334  // positive fixint
7335  case 0x00:
7336  case 0x01:
7337  case 0x02:
7338  case 0x03:
7339  case 0x04:
7340  case 0x05:
7341  case 0x06:
7342  case 0x07:
7343  case 0x08:
7344  case 0x09:
7345  case 0x0A:
7346  case 0x0B:
7347  case 0x0C:
7348  case 0x0D:
7349  case 0x0E:
7350  case 0x0F:
7351  case 0x10:
7352  case 0x11:
7353  case 0x12:
7354  case 0x13:
7355  case 0x14:
7356  case 0x15:
7357  case 0x16:
7358  case 0x17:
7359  case 0x18:
7360  case 0x19:
7361  case 0x1A:
7362  case 0x1B:
7363  case 0x1C:
7364  case 0x1D:
7365  case 0x1E:
7366  case 0x1F:
7367  case 0x20:
7368  case 0x21:
7369  case 0x22:
7370  case 0x23:
7371  case 0x24:
7372  case 0x25:
7373  case 0x26:
7374  case 0x27:
7375  case 0x28:
7376  case 0x29:
7377  case 0x2A:
7378  case 0x2B:
7379  case 0x2C:
7380  case 0x2D:
7381  case 0x2E:
7382  case 0x2F:
7383  case 0x30:
7384  case 0x31:
7385  case 0x32:
7386  case 0x33:
7387  case 0x34:
7388  case 0x35:
7389  case 0x36:
7390  case 0x37:
7391  case 0x38:
7392  case 0x39:
7393  case 0x3A:
7394  case 0x3B:
7395  case 0x3C:
7396  case 0x3D:
7397  case 0x3E:
7398  case 0x3F:
7399  case 0x40:
7400  case 0x41:
7401  case 0x42:
7402  case 0x43:
7403  case 0x44:
7404  case 0x45:
7405  case 0x46:
7406  case 0x47:
7407  case 0x48:
7408  case 0x49:
7409  case 0x4A:
7410  case 0x4B:
7411  case 0x4C:
7412  case 0x4D:
7413  case 0x4E:
7414  case 0x4F:
7415  case 0x50:
7416  case 0x51:
7417  case 0x52:
7418  case 0x53:
7419  case 0x54:
7420  case 0x55:
7421  case 0x56:
7422  case 0x57:
7423  case 0x58:
7424  case 0x59:
7425  case 0x5A:
7426  case 0x5B:
7427  case 0x5C:
7428  case 0x5D:
7429  case 0x5E:
7430  case 0x5F:
7431  case 0x60:
7432  case 0x61:
7433  case 0x62:
7434  case 0x63:
7435  case 0x64:
7436  case 0x65:
7437  case 0x66:
7438  case 0x67:
7439  case 0x68:
7440  case 0x69:
7441  case 0x6A:
7442  case 0x6B:
7443  case 0x6C:
7444  case 0x6D:
7445  case 0x6E:
7446  case 0x6F:
7447  case 0x70:
7448  case 0x71:
7449  case 0x72:
7450  case 0x73:
7451  case 0x74:
7452  case 0x75:
7453  case 0x76:
7454  case 0x77:
7455  case 0x78:
7456  case 0x79:
7457  case 0x7A:
7458  case 0x7B:
7459  case 0x7C:
7460  case 0x7D:
7461  case 0x7E:
7462  case 0x7F:
7463  return sax->number_unsigned(static_cast<number_unsigned_t>(current));
7464 
7465  // fixmap
7466  case 0x80:
7467  case 0x81:
7468  case 0x82:
7469  case 0x83:
7470  case 0x84:
7471  case 0x85:
7472  case 0x86:
7473  case 0x87:
7474  case 0x88:
7475  case 0x89:
7476  case 0x8A:
7477  case 0x8B:
7478  case 0x8C:
7479  case 0x8D:
7480  case 0x8E:
7481  case 0x8F:
7482  return get_msgpack_object(static_cast<std::size_t>(current & 0x0F));
7483 
7484  // fixarray
7485  case 0x90:
7486  case 0x91:
7487  case 0x92:
7488  case 0x93:
7489  case 0x94:
7490  case 0x95:
7491  case 0x96:
7492  case 0x97:
7493  case 0x98:
7494  case 0x99:
7495  case 0x9A:
7496  case 0x9B:
7497  case 0x9C:
7498  case 0x9D:
7499  case 0x9E:
7500  case 0x9F:
7501  return get_msgpack_array(static_cast<std::size_t>(current & 0x0F));
7502 
7503  // fixstr
7504  case 0xA0:
7505  case 0xA1:
7506  case 0xA2:
7507  case 0xA3:
7508  case 0xA4:
7509  case 0xA5:
7510  case 0xA6:
7511  case 0xA7:
7512  case 0xA8:
7513  case 0xA9:
7514  case 0xAA:
7515  case 0xAB:
7516  case 0xAC:
7517  case 0xAD:
7518  case 0xAE:
7519  case 0xAF:
7520  case 0xB0:
7521  case 0xB1:
7522  case 0xB2:
7523  case 0xB3:
7524  case 0xB4:
7525  case 0xB5:
7526  case 0xB6:
7527  case 0xB7:
7528  case 0xB8:
7529  case 0xB9:
7530  case 0xBA:
7531  case 0xBB:
7532  case 0xBC:
7533  case 0xBD:
7534  case 0xBE:
7535  case 0xBF:
7536  {
7537  string_t s;
7538  return get_msgpack_string(s) and sax->string(s);
7539  }
7540 
7541  case 0xC0: // nil
7542  return sax->null();
7543 
7544  case 0xC2: // false
7545  return sax->boolean(false);
7546 
7547  case 0xC3: // true
7548  return sax->boolean(true);
7549 
7550  case 0xCA: // float 32
7551  {
7552  float number;
7553  return get_number(input_format_t::msgpack, number) and sax->number_float(static_cast<number_float_t>(number), "");
7554  }
7555 
7556  case 0xCB: // float 64
7557  {
7558  double number;
7559  return get_number(input_format_t::msgpack, number) and sax->number_float(static_cast<number_float_t>(number), "");
7560  }
7561 
7562  case 0xCC: // uint 8
7563  {
7564  uint8_t number;
7565  return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number);
7566  }
7567 
7568  case 0xCD: // uint 16
7569  {
7570  uint16_t number;
7571  return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number);
7572  }
7573 
7574  case 0xCE: // uint 32
7575  {
7576  uint32_t number;
7577  return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number);
7578  }
7579 
7580  case 0xCF: // uint 64
7581  {
7582  uint64_t number;
7583  return get_number(input_format_t::msgpack, number) and sax->number_unsigned(number);
7584  }
7585 
7586  case 0xD0: // int 8
7587  {
7588  int8_t number;
7589  return get_number(input_format_t::msgpack, number) and sax->number_integer(number);
7590  }
7591 
7592  case 0xD1: // int 16
7593  {
7594  int16_t number;
7595  return get_number(input_format_t::msgpack, number) and sax->number_integer(number);
7596  }
7597 
7598  case 0xD2: // int 32
7599  {
7600  int32_t number;
7601  return get_number(input_format_t::msgpack, number) and sax->number_integer(number);
7602  }
7603 
7604  case 0xD3: // int 64
7605  {
7606  int64_t number;
7607  return get_number(input_format_t::msgpack, number) and sax->number_integer(number);
7608  }
7609 
7610  case 0xD9: // str 8
7611  case 0xDA: // str 16
7612  case 0xDB: // str 32
7613  {
7614  string_t s;
7615  return get_msgpack_string(s) and sax->string(s);
7616  }
7617 
7618  case 0xDC: // array 16
7619  {
7620  uint16_t len;
7621  return get_number(input_format_t::msgpack, len) and get_msgpack_array(static_cast<std::size_t>(len));
7622  }
7623 
7624  case 0xDD: // array 32
7625  {
7626  uint32_t len;
7627  return get_number(input_format_t::msgpack, len) and get_msgpack_array(static_cast<std::size_t>(len));
7628  }
7629 
7630  case 0xDE: // map 16
7631  {
7632  uint16_t len;
7633  return get_number(input_format_t::msgpack, len) and get_msgpack_object(static_cast<std::size_t>(len));
7634  }
7635 
7636  case 0xDF: // map 32
7637  {
7638  uint32_t len;
7639  return get_number(input_format_t::msgpack, len) and get_msgpack_object(static_cast<std::size_t>(len));
7640  }
7641 
7642  // negative fixint
7643  case 0xE0:
7644  case 0xE1:
7645  case 0xE2:
7646  case 0xE3:
7647  case 0xE4:
7648  case 0xE5:
7649  case 0xE6:
7650  case 0xE7:
7651  case 0xE8:
7652  case 0xE9:
7653  case 0xEA:
7654  case 0xEB:
7655  case 0xEC:
7656  case 0xED:
7657  case 0xEE:
7658  case 0xEF:
7659  case 0xF0:
7660  case 0xF1:
7661  case 0xF2:
7662  case 0xF3:
7663  case 0xF4:
7664  case 0xF5:
7665  case 0xF6:
7666  case 0xF7:
7667  case 0xF8:
7668  case 0xF9:
7669  case 0xFA:
7670  case 0xFB:
7671  case 0xFC:
7672  case 0xFD:
7673  case 0xFE:
7674  case 0xFF:
7675  return sax->number_integer(static_cast<int8_t>(current));
7676 
7677  default: // anything else
7678  {
7679  auto last_token = get_token_string();
7680  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")));
7681  }
7682  }
7683  }
7684 
7685  /*!
7686  @brief reads a MessagePack string
7687 
7688  This function first reads starting bytes to determine the expected
7689  string length and then copies this number of bytes into a string.
7690 
7691  @param[out] result created string
7692 
7693  @return whether string creation completed
7694  */
7695  bool get_msgpack_string(string_t& result)
7696  {
7697  if (JSON_UNLIKELY(not unexpect_eof(input_format_t::msgpack, "string")))
7698  {
7699  return false;
7700  }
7701 
7702  switch (current)
7703  {
7704  // fixstr
7705  case 0xA0:
7706  case 0xA1:
7707  case 0xA2:
7708  case 0xA3:
7709  case 0xA4:
7710  case 0xA5:
7711  case 0xA6:
7712  case 0xA7:
7713  case 0xA8:
7714  case 0xA9:
7715  case 0xAA:
7716  case 0xAB:
7717  case 0xAC:
7718  case 0xAD:
7719  case 0xAE:
7720  case 0xAF:
7721  case 0xB0:
7722  case 0xB1:
7723  case 0xB2:
7724  case 0xB3:
7725  case 0xB4:
7726  case 0xB5:
7727  case 0xB6:
7728  case 0xB7:
7729  case 0xB8:
7730  case 0xB9:
7731  case 0xBA:
7732  case 0xBB:
7733  case 0xBC:
7734  case 0xBD:
7735  case 0xBE:
7736  case 0xBF:
7737  {
7738  return get_string(input_format_t::msgpack, current & 0x1F, result);
7739  }
7740 
7741  case 0xD9: // str 8
7742  {
7743  uint8_t len;
7744  return get_number(input_format_t::msgpack, len) and get_string(input_format_t::msgpack, len, result);
7745  }
7746 
7747  case 0xDA: // str 16
7748  {
7749  uint16_t len;
7750  return get_number(input_format_t::msgpack, len) and get_string(input_format_t::msgpack, len, result);
7751  }
7752 
7753  case 0xDB: // str 32
7754  {
7755  uint32_t len;
7756  return get_number(input_format_t::msgpack, len) and get_string(input_format_t::msgpack, len, result);
7757  }
7758 
7759  default:
7760  {
7761  auto last_token = get_token_string();
7762  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")));
7763  }
7764  }
7765  }
7766 
7767  /*!
7768  @param[in] len the length of the array
7769  @return whether array creation completed
7770  */
7771  bool get_msgpack_array(const std::size_t len)
7772  {
7773  if (JSON_UNLIKELY(not sax->start_array(len)))
7774  {
7775  return false;
7776  }
7777 
7778  for (std::size_t i = 0; i < len; ++i)
7779  {
7780  if (JSON_UNLIKELY(not parse_msgpack_internal()))
7781  {
7782  return false;
7783  }
7784  }
7785 
7786  return sax->end_array();
7787  }
7788 
7789  /*!
7790  @param[in] len the length of the object
7791  @return whether object creation completed
7792  */
7793  bool get_msgpack_object(const std::size_t len)
7794  {
7795  if (JSON_UNLIKELY(not sax->start_object(len)))
7796  {
7797  return false;
7798  }
7799 
7800  string_t key;
7801  for (std::size_t i = 0; i < len; ++i)
7802  {
7803  get();
7804  if (JSON_UNLIKELY(not get_msgpack_string(key) or not sax->key(key)))
7805  {
7806  return false;
7807  }
7808 
7809  if (JSON_UNLIKELY(not parse_msgpack_internal()))
7810  {
7811  return false;
7812  }
7813  key.clear();
7814  }
7815 
7816  return sax->end_object();
7817  }
7818 
7819  ////////////
7820  // UBJSON //
7821  ////////////
7822 
7823  /*!
7824  @param[in] get_char whether a new character should be retrieved from the
7825  input (true, default) or whether the last read
7826  character should be considered instead
7827 
7828  @return whether a valid UBJSON value was passed to the SAX parser
7829  */
7830  bool parse_ubjson_internal(const bool get_char = true)
7831  {
7832  return get_ubjson_value(get_char ? get_ignore_noop() : current);
7833  }
7834 
7835  /*!
7836  @brief reads a UBJSON string
7837 
7838  This function is either called after reading the 'S' byte explicitly
7839  indicating a string, or in case of an object key where the 'S' byte can be
7840  left out.
7841 
7842  @param[out] result created string
7843  @param[in] get_char whether a new character should be retrieved from the
7844  input (true, default) or whether the last read
7845  character should be considered instead
7846 
7847  @return whether string creation completed
7848  */
7849  bool get_ubjson_string(string_t& result, const bool get_char = true)
7850  {
7851  if (get_char)
7852  {
7853  get(); // TODO: may we ignore N here?
7854  }
7855 
7856  if (JSON_UNLIKELY(not unexpect_eof(input_format_t::ubjson, "value")))
7857  {
7858  return false;
7859  }
7860 
7861  switch (current)
7862  {
7863  case 'U':
7864  {
7865  uint8_t len;
7866  return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result);
7867  }
7868 
7869  case 'i':
7870  {
7871  int8_t len;
7872  return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result);
7873  }
7874 
7875  case 'I':
7876  {
7877  int16_t len;
7878  return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result);
7879  }
7880 
7881  case 'l':
7882  {
7883  int32_t len;
7884  return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result);
7885  }
7886 
7887  case 'L':
7888  {
7889  int64_t len;
7890  return get_number(input_format_t::ubjson, len) and get_string(input_format_t::ubjson, len, result);
7891  }
7892 
7893  default:
7894  auto last_token = get_token_string();
7895  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")));
7896  }
7897  }
7898 
7899  /*!
7900  @param[out] result determined size
7901  @return whether size determination completed
7902  */
7903  bool get_ubjson_size_value(std::size_t& result)
7904  {
7905  switch (get_ignore_noop())
7906  {
7907  case 'U':
7908  {
7909  uint8_t number;
7910  if (JSON_UNLIKELY(not get_number(input_format_t::ubjson, number)))
7911  {
7912  return false;
7913  }
7914  result = static_cast<std::size_t>(number);
7915  return true;
7916  }
7917 
7918  case 'i':
7919  {
7920  int8_t number;
7921  if (JSON_UNLIKELY(not get_number(input_format_t::ubjson, number)))
7922  {
7923  return false;
7924  }
7925  result = static_cast<std::size_t>(number);
7926  return true;
7927  }
7928 
7929  case 'I':
7930  {
7931  int16_t number;
7932  if (JSON_UNLIKELY(not get_number(input_format_t::ubjson, number)))
7933  {
7934  return false;
7935  }
7936  result = static_cast<std::size_t>(number);
7937  return true;
7938  }
7939 
7940  case 'l':
7941  {
7942  int32_t number;
7943  if (JSON_UNLIKELY(not get_number(input_format_t::ubjson, number)))
7944  {
7945  return false;
7946  }
7947  result = static_cast<std::size_t>(number);
7948  return true;
7949  }
7950 
7951  case 'L':
7952  {
7953  int64_t number;
7954  if (JSON_UNLIKELY(not get_number(input_format_t::ubjson, number)))
7955  {
7956  return false;
7957  }
7958  result = static_cast<std::size_t>(number);
7959  return true;
7960  }
7961 
7962  default:
7963  {
7964  auto last_token = get_token_string();
7965  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")));
7966  }
7967  }
7968  }
7969 
7970  /*!
7971  @brief determine the type and size for a container
7972 
7973  In the optimized UBJSON format, a type and a size can be provided to allow
7974  for a more compact representation.
7975 
7976  @param[out] result pair of the size and the type
7977 
7978  @return whether pair creation completed
7979  */
7980  bool get_ubjson_size_type(std::pair<std::size_t, int>& result)
7981  {
7982  result.first = string_t::npos; // size
7983  result.second = 0; // type
7984 
7985  get_ignore_noop();
7986 
7987  if (current == '$')
7988  {
7989  result.second = get(); // must not ignore 'N', because 'N' maybe the type
7990  if (JSON_UNLIKELY(not unexpect_eof(input_format_t::ubjson, "type")))
7991  {
7992  return false;
7993  }
7994 
7995  get_ignore_noop();
7996  if (JSON_UNLIKELY(current != '#'))
7997  {
7998  if (JSON_UNLIKELY(not unexpect_eof(input_format_t::ubjson, "value")))
7999  {
8000  return false;
8001  }
8002  auto last_token = get_token_string();
8003  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")));
8004  }
8005 
8006  return get_ubjson_size_value(result.first);
8007  }
8008  else if (current == '#')
8009  {
8010  return get_ubjson_size_value(result.first);
8011  }
8012  return true;
8013  }
8014 
8015  /*!
8016  @param prefix the previously read or set type prefix
8017  @return whether value creation completed
8018  */
8019  bool get_ubjson_value(const int prefix)
8020  {
8021  switch (prefix)
8022  {
8023  case std::char_traits<char>::eof(): // EOF
8024  return unexpect_eof(input_format_t::ubjson, "value");
8025 
8026  case 'T': // true
8027  return sax->boolean(true);
8028  case 'F': // false
8029  return sax->boolean(false);
8030 
8031  case 'Z': // null
8032  return sax->null();
8033 
8034  case 'U':
8035  {
8036  uint8_t number;
8037  return get_number(input_format_t::ubjson, number) and sax->number_unsigned(number);
8038  }
8039 
8040  case 'i':
8041  {
8042  int8_t number;
8043  return get_number(input_format_t::ubjson, number) and sax->number_integer(number);
8044  }
8045 
8046  case 'I':
8047  {
8048  int16_t number;
8049  return get_number(input_format_t::ubjson, number) and sax->number_integer(number);
8050  }
8051 
8052  case 'l':
8053  {
8054  int32_t number;
8055  return get_number(input_format_t::ubjson, number) and sax->number_integer(number);
8056  }
8057 
8058  case 'L':
8059  {
8060  int64_t number;
8061  return get_number(input_format_t::ubjson, number) and sax->number_integer(number);
8062  }
8063 
8064  case 'd':
8065  {
8066  float number;
8067  return get_number(input_format_t::ubjson, number) and sax->number_float(static_cast<number_float_t>(number), "");
8068  }
8069 
8070  case 'D':
8071  {
8072  double number;
8073  return get_number(input_format_t::ubjson, number) and sax->number_float(static_cast<number_float_t>(number), "");
8074  }
8075 
8076  case 'C': // char
8077  {
8078  get();
8079  if (JSON_UNLIKELY(not unexpect_eof(input_format_t::ubjson, "char")))
8080  {
8081  return false;
8082  }
8083  if (JSON_UNLIKELY(current > 127))
8084  {
8085  auto last_token = get_token_string();
8086  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")));
8087  }
8088  string_t s(1, static_cast<char>(current));
8089  return sax->string(s);
8090  }
8091 
8092  case 'S': // string
8093  {
8094  string_t s;
8095  return get_ubjson_string(s) and sax->string(s);
8096  }
8097 
8098  case '[': // array
8099  return get_ubjson_array();
8100 
8101  case '{': // object
8102  return get_ubjson_object();
8103 
8104  default: // anything else
8105  {
8106  auto last_token = get_token_string();
8107  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")));
8108  }
8109  }
8110  }
8111 
8112  /*!
8113  @return whether array creation completed
8114  */
8115  bool get_ubjson_array()
8116  {
8117  std::pair<std::size_t, int> size_and_type;
8118  if (JSON_UNLIKELY(not get_ubjson_size_type(size_and_type)))
8119  {
8120  return false;
8121  }
8122 
8123  if (size_and_type.first != string_t::npos)
8124  {
8125  if (JSON_UNLIKELY(not sax->start_array(size_and_type.first)))
8126  {
8127  return false;
8128  }
8129 
8130  if (size_and_type.second != 0)
8131  {
8132  if (size_and_type.second != 'N')
8133  {
8134  for (std::size_t i = 0; i < size_and_type.first; ++i)
8135  {
8136  if (JSON_UNLIKELY(not get_ubjson_value(size_and_type.second)))
8137  {
8138  return false;
8139  }
8140  }
8141  }
8142  }
8143  else
8144  {
8145  for (std::size_t i = 0; i < size_and_type.first; ++i)
8146  {
8147  if (JSON_UNLIKELY(not parse_ubjson_internal()))
8148  {
8149  return false;
8150  }
8151  }
8152  }
8153  }
8154  else
8155  {
8156  if (JSON_UNLIKELY(not sax->start_array(std::size_t(-1))))
8157  {
8158  return false;
8159  }
8160 
8161  while (current != ']')
8162  {
8163  if (JSON_UNLIKELY(not parse_ubjson_internal(false)))
8164  {
8165  return false;
8166  }
8167  get_ignore_noop();
8168  }
8169  }
8170 
8171  return sax->end_array();
8172  }
8173 
8174  /*!
8175  @return whether object creation completed
8176  */
8177  bool get_ubjson_object()
8178  {
8179  std::pair<std::size_t, int> size_and_type;
8180  if (JSON_UNLIKELY(not get_ubjson_size_type(size_and_type)))
8181  {
8182  return false;
8183  }
8184 
8185  string_t key;
8186  if (size_and_type.first != string_t::npos)
8187  {
8188  if (JSON_UNLIKELY(not sax->start_object(size_and_type.first)))
8189  {
8190  return false;
8191  }
8192 
8193  if (size_and_type.second != 0)
8194  {
8195  for (std::size_t i = 0; i < size_and_type.first; ++i)
8196  {
8197  if (JSON_UNLIKELY(not get_ubjson_string(key) or not sax->key(key)))
8198  {
8199  return false;
8200  }
8201  if (JSON_UNLIKELY(not get_ubjson_value(size_and_type.second)))
8202  {
8203  return false;
8204  }
8205  key.clear();
8206  }
8207  }
8208  else
8209  {
8210  for (std::size_t i = 0; i < size_and_type.first; ++i)
8211  {
8212  if (JSON_UNLIKELY(not get_ubjson_string(key) or not sax->key(key)))
8213  {
8214  return false;
8215  }
8216  if (JSON_UNLIKELY(not parse_ubjson_internal()))
8217  {
8218  return false;
8219  }
8220  key.clear();
8221  }
8222  }
8223  }
8224  else
8225  {
8226  if (JSON_UNLIKELY(not sax->start_object(std::size_t(-1))))
8227  {
8228  return false;
8229  }
8230 
8231  while (current != '}')
8232  {
8233  if (JSON_UNLIKELY(not get_ubjson_string(key, false) or not sax->key(key)))
8234  {
8235  return false;
8236  }
8237  if (JSON_UNLIKELY(not parse_ubjson_internal()))
8238  {
8239  return false;
8240  }
8241  get_ignore_noop();
8242  key.clear();
8243  }
8244  }
8245 
8246  return sax->end_object();
8247  }
8248 
8249  ///////////////////////
8250  // Utility functions //
8251  ///////////////////////
8252 
8253  /*!
8254  @brief get next character from the input
8255 
8256  This function provides the interface to the used input adapter. It does
8257  not throw in case the input reached EOF, but returns a -'ve valued
8258  `std::char_traits<char>::eof()` in that case.
8259 
8260  @return character read from the input
8261  */
8262  int get()
8263  {
8264  ++chars_read;
8265  return (current = ia->get_character());
8266  }
8267 
8268  /*!
8269  @return character read from the input after ignoring all 'N' entries
8270  */
8271  int get_ignore_noop()
8272  {
8273  do
8274  {
8275  get();
8276  }
8277  while (current == 'N');
8278 
8279  return current;
8280  }
8281 
8282  /*
8283  @brief read a number from the input
8284 
8285  @tparam NumberType the type of the number
8286  @param[in] format the current format (for diagnostics)
8287  @param[out] result number of type @a NumberType
8288 
8289  @return whether conversion completed
8290 
8291  @note This function needs to respect the system's endianess, because
8292  bytes in CBOR, MessagePack, and UBJSON are stored in network order
8293  (big endian) and therefore need reordering on little endian systems.
8294  */
8295  template<typename NumberType, bool InputIsLittleEndian = false>
8296  bool get_number(const input_format_t format, NumberType& result)
8297  {
8298  // step 1: read input into array with system's byte order
8299  std::array<uint8_t, sizeof(NumberType)> vec;
8300  for (std::size_t i = 0; i < sizeof(NumberType); ++i)
8301  {
8302  get();
8303  if (JSON_UNLIKELY(not unexpect_eof(format, "number")))
8304  {
8305  return false;
8306  }
8307 
8308  // reverse byte order prior to conversion if necessary
8309  if (is_little_endian && !InputIsLittleEndian)
8310  {
8311  vec[sizeof(NumberType) - i - 1] = static_cast<uint8_t>(current);
8312  }
8313  else
8314  {
8315  vec[i] = static_cast<uint8_t>(current); // LCOV_EXCL_LINE
8316  }
8317  }
8318 
8319  // step 2: convert array into number of type T and return
8320  std::memcpy(&result, vec.data(), sizeof(NumberType));
8321  return true;
8322  }
8323 
8324  /*!
8325  @brief create a string by reading characters from the input
8326 
8327  @tparam NumberType the type of the number
8328  @param[in] format the current format (for diagnostics)
8329  @param[in] len number of characters to read
8330  @param[out] result string created by reading @a len bytes
8331 
8332  @return whether string creation completed
8333 
8334  @note We can not reserve @a len bytes for the result, because @a len
8335  may be too large. Usually, @ref unexpect_eof() detects the end of
8336  the input before we run out of string memory.
8337  */
8338  template<typename NumberType>
8339  bool get_string(const input_format_t format,
8340  const NumberType len,
8341  string_t& result)
8342  {
8343  bool success = true;
8344  std::generate_n(std::back_inserter(result), len, [this, &success, &format]()
8345  {
8346  get();
8347  if (JSON_UNLIKELY(not unexpect_eof(format, "string")))
8348  {
8349  success = false;
8350  }
8351  return static_cast<char>(current);
8352  });
8353  return success;
8354  }
8355 
8356  /*!
8357  @param[in] format the current format (for diagnostics)
8358  @param[in] context further context information (for diagnostics)
8359  @return whether the last read character is not EOF
8360  */
8361  bool unexpect_eof(const input_format_t format, const char* context) const
8362  {
8363  if (JSON_UNLIKELY(current == std::char_traits<char>::eof()))
8364  {
8365  return sax->parse_error(chars_read, "<end of file>",
8366  parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context)));
8367  }
8368  return true;
8369  }
8370 
8371  /*!
8372  @return a string representation of the last read byte
8373  */
8374  std::string get_token_string() const
8375  {
8376  char cr[3];
8377  (std::snprintf)(cr, 3, "%.2hhX", static_cast<unsigned char>(current));
8378  return std::string{cr};
8379  }
8380 
8381  /*!
8382  @param[in] format the current format
8383  @param[in] detail a detailed error message
8384  @param[in] context further contect information
8385  @return a message string to use in the parse_error exceptions
8386  */
8387  std::string exception_message(const input_format_t format,
8388  const std::string& detail,
8389  const std::string& context) const
8390  {
8391  std::string error_msg = "syntax error while parsing ";
8392 
8393  switch (format)
8394  {
8395  case input_format_t::cbor:
8396  error_msg += "CBOR";
8397  break;
8398 
8399  case input_format_t::msgpack:
8400  error_msg += "MessagePack";
8401  break;
8402 
8403  case input_format_t::ubjson:
8404  error_msg += "UBJSON";
8405  break;
8406 
8407  case input_format_t::bson:
8408  error_msg += "BSON";
8409  break;
8410 
8411  // LCOV_EXCL_START
8412  default:
8413  assert(false);
8414  // LCOV_EXCL_STOP
8415  }
8416 
8417  return error_msg + " " + context + ": " + detail;
8418  }
8419 
8420  private:
8421  /// input adapter
8422  input_adapter_t ia = nullptr;
8423 
8424  /// the current character
8425  int current = std::char_traits<char>::eof();
8426 
8427  /// the number of characters read
8428  std::size_t chars_read = 0;
8429 
8430  /// whether we can assume little endianess
8431  const bool is_little_endian = little_endianess();
8432 
8433  /// the SAX parser
8434  json_sax_t* sax = nullptr;
8435 };
8436 } // namespace detail
8437 } // namespace nlohmann
8438 
8439 // #include <nlohmann/detail/output/binary_writer.hpp>
8440 
8441 
8442 #include <algorithm> // reverse
8443 #include <array> // array
8444 #include <cstdint> // uint8_t, uint16_t, uint32_t, uint64_t
8445 #include <cstring> // memcpy
8446 #include <limits> // numeric_limits
8447 
8448 // #include <nlohmann/detail/input/binary_reader.hpp>
8449 
8450 // #include <nlohmann/detail/output/output_adapters.hpp>
8451 
8452 
8453 namespace nlohmann
8454 {
8455 namespace detail
8456 {
8457 ///////////////////
8458 // binary writer //
8459 ///////////////////
8460 
8461 /*!
8462 @brief serialization to CBOR and MessagePack values
8463 */
8464 template<typename BasicJsonType, typename CharType>
8465 class binary_writer
8466 {
8467  using string_t = typename BasicJsonType::string_t;
8468 
8469  public:
8470  /*!
8471  @brief create a binary writer
8472 
8473  @param[in] adapter output adapter to write to
8474  */
8475  explicit binary_writer(output_adapter_t<CharType> adapter) : oa(adapter)
8476  {
8477  assert(oa);
8478  }
8479 
8480  /*!
8481  @param[in] j JSON value to serialize
8482  @pre j.type() == value_t::object
8483  */
8484  void write_bson(const BasicJsonType& j)
8485  {
8486  switch (j.type())
8487  {
8488  case value_t::object:
8489  {
8490  write_bson_object(*j.m_value.object);
8491  break;
8492  }
8493 
8494  default:
8495  {
8496  JSON_THROW(type_error::create(317, "to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name())));
8497  }
8498  }
8499  }
8500 
8501  /*!
8502  @param[in] j JSON value to serialize
8503  */
8504  void write_cbor(const BasicJsonType& j)
8505  {
8506  switch (j.type())
8507  {
8508  case value_t::null:
8509  {
8510  oa->write_character(to_char_type(0xF6));
8511  break;
8512  }
8513 
8514  case value_t::boolean:
8515  {
8516  oa->write_character(j.m_value.boolean
8517  ? to_char_type(0xF5)
8518  : to_char_type(0xF4));
8519  break;
8520  }
8521 
8522  case value_t::number_integer:
8523  {
8524  if (j.m_value.number_integer >= 0)
8525  {
8526  // CBOR does not differentiate between positive signed
8527  // integers and unsigned integers. Therefore, we used the
8528  // code from the value_t::number_unsigned case here.
8529  if (j.m_value.number_integer <= 0x17)
8530  {
8531  write_number(static_cast<uint8_t>(j.m_value.number_integer));
8532  }
8533  else if (j.m_value.number_integer <= (std::numeric_limits<uint8_t>::max)())
8534  {
8535  oa->write_character(to_char_type(0x18));
8536  write_number(static_cast<uint8_t>(j.m_value.number_integer));
8537  }
8538  else if (j.m_value.number_integer <= (std::numeric_limits<uint16_t>::max)())
8539  {
8540  oa->write_character(to_char_type(0x19));
8541  write_number(static_cast<uint16_t>(j.m_value.number_integer));
8542  }
8543  else if (j.m_value.number_integer <= (std::numeric_limits<uint32_t>::max)())
8544  {
8545  oa->write_character(to_char_type(0x1A));
8546  write_number(static_cast<uint32_t>(j.m_value.number_integer));
8547  }
8548  else
8549  {
8550  oa->write_character(to_char_type(0x1B));
8551  write_number(static_cast<uint64_t>(j.m_value.number_integer));
8552  }
8553  }
8554  else
8555  {
8556  // The conversions below encode the sign in the first
8557  // byte, and the value is converted to a positive number.
8558  const auto positive_number = -1 - j.m_value.number_integer;
8559  if (j.m_value.number_integer >= -24)
8560  {
8561  write_number(static_cast<uint8_t>(0x20 + positive_number));
8562  }
8563  else if (positive_number <= (std::numeric_limits<uint8_t>::max)())
8564  {
8565  oa->write_character(to_char_type(0x38));
8566  write_number(static_cast<uint8_t>(positive_number));
8567  }
8568  else if (positive_number <= (std::numeric_limits<uint16_t>::max)())
8569  {
8570  oa->write_character(to_char_type(0x39));
8571  write_number(static_cast<uint16_t>(positive_number));
8572  }
8573  else if (positive_number <= (std::numeric_limits<uint32_t>::max)())
8574  {
8575  oa->write_character(to_char_type(0x3A));
8576  write_number(static_cast<uint32_t>(positive_number));
8577  }
8578  else
8579  {
8580  oa->write_character(to_char_type(0x3B));
8581  write_number(static_cast<uint64_t>(positive_number));
8582  }
8583  }
8584  break;
8585  }
8586 
8587  case value_t::number_unsigned:
8588  {
8589  if (j.m_value.number_unsigned <= 0x17)
8590  {
8591  write_number(static_cast<uint8_t>(j.m_value.number_unsigned));
8592  }
8593  else if (j.m_value.number_unsigned <= (std::numeric_limits<uint8_t>::max)())
8594  {
8595  oa->write_character(to_char_type(0x18));
8596  write_number(static_cast<uint8_t>(j.m_value.number_unsigned));
8597  }
8598  else if (j.m_value.number_unsigned <= (std::numeric_limits<uint16_t>::max)())
8599  {
8600  oa->write_character(to_char_type(0x19));
8601  write_number(static_cast<uint16_t>(j.m_value.number_unsigned));
8602  }
8603  else if (j.m_value.number_unsigned <= (std::numeric_limits<uint32_t>::max)())
8604  {
8605  oa->write_character(to_char_type(0x1A));
8606  write_number(static_cast<uint32_t>(j.m_value.number_unsigned));
8607  }
8608  else
8609  {
8610  oa->write_character(to_char_type(0x1B));
8611  write_number(static_cast<uint64_t>(j.m_value.number_unsigned));
8612  }
8613  break;
8614  }
8615 
8616  case value_t::number_float:
8617  {
8618  oa->write_character(get_cbor_float_prefix(j.m_value.number_float));
8619  write_number(j.m_value.number_float);
8620  break;
8621  }
8622 
8623  case value_t::string:
8624  {
8625  // step 1: write control byte and the string length
8626  const auto N = j.m_value.string->size();
8627  if (N <= 0x17)
8628  {
8629  write_number(static_cast<uint8_t>(0x60 + N));
8630  }
8631  else if (N <= (std::numeric_limits<uint8_t>::max)())
8632  {
8633  oa->write_character(to_char_type(0x78));
8634  write_number(static_cast<uint8_t>(N));
8635  }
8636  else if (N <= (std::numeric_limits<uint16_t>::max)())
8637  {
8638  oa->write_character(to_char_type(0x79));
8639  write_number(static_cast<uint16_t>(N));
8640  }
8641  else if (N <= (std::numeric_limits<uint32_t>::max)())
8642  {
8643  oa->write_character(to_char_type(0x7A));
8644  write_number(static_cast<uint32_t>(N));
8645  }
8646  // LCOV_EXCL_START
8647  else if (N <= (std::numeric_limits<uint64_t>::max)())
8648  {
8649  oa->write_character(to_char_type(0x7B));
8650  write_number(static_cast<uint64_t>(N));
8651  }
8652  // LCOV_EXCL_STOP
8653 
8654  // step 2: write the string
8655  oa->write_characters(
8656  reinterpret_cast<const CharType*>(j.m_value.string->c_str()),
8657  j.m_value.string->size());
8658  break;
8659  }
8660 
8661  case value_t::array:
8662  {
8663  // step 1: write control byte and the array size
8664  const auto N = j.m_value.array->size();
8665  if (N <= 0x17)
8666  {
8667  write_number(static_cast<uint8_t>(0x80 + N));
8668  }
8669  else if (N <= (std::numeric_limits<uint8_t>::max)())
8670  {
8671  oa->write_character(to_char_type(0x98));
8672  write_number(static_cast<uint8_t>(N));
8673  }
8674  else if (N <= (std::numeric_limits<uint16_t>::max)())
8675  {
8676  oa->write_character(to_char_type(0x99));
8677  write_number(static_cast<uint16_t>(N));
8678  }
8679  else if (N <= (std::numeric_limits<uint32_t>::max)())
8680  {
8681  oa->write_character(to_char_type(0x9A));
8682  write_number(static_cast<uint32_t>(N));
8683  }
8684  // LCOV_EXCL_START
8685  else if (N <= (std::numeric_limits<uint64_t>::max)())
8686  {
8687  oa->write_character(to_char_type(0x9B));
8688  write_number(static_cast<uint64_t>(N));
8689  }
8690  // LCOV_EXCL_STOP
8691 
8692  // step 2: write each element
8693  for (const auto& el : *j.m_value.array)
8694  {
8695  write_cbor(el);
8696  }
8697  break;
8698  }
8699 
8700  case value_t::object:
8701  {
8702  // step 1: write control byte and the object size
8703  const auto N = j.m_value.object->size();
8704  if (N <= 0x17)
8705  {
8706  write_number(static_cast<uint8_t>(0xA0 + N));
8707  }
8708  else if (N <= (std::numeric_limits<uint8_t>::max)())
8709  {
8710  oa->write_character(to_char_type(0xB8));
8711  write_number(static_cast<uint8_t>(N));
8712  }
8713  else if (N <= (std::numeric_limits<uint16_t>::max)())
8714  {
8715  oa->write_character(to_char_type(0xB9));
8716  write_number(static_cast<uint16_t>(N));
8717  }
8718  else if (N <= (std::numeric_limits<uint32_t>::max)())
8719  {
8720  oa->write_character(to_char_type(0xBA));
8721  write_number(static_cast<uint32_t>(N));
8722  }
8723  // LCOV_EXCL_START
8724  else if (N <= (std::numeric_limits<uint64_t>::max)())
8725  {
8726  oa->write_character(to_char_type(0xBB));
8727  write_number(static_cast<uint64_t>(N));
8728  }
8729  // LCOV_EXCL_STOP
8730 
8731  // step 2: write each element
8732  for (const auto& el : *j.m_value.object)
8733  {
8734  write_cbor(el.first);
8735  write_cbor(el.second);
8736  }
8737  break;
8738  }
8739 
8740  default:
8741  break;
8742  }
8743  }
8744 
8745  /*!
8746  @param[in] j JSON value to serialize
8747  */
8748  void write_msgpack(const BasicJsonType& j)
8749  {
8750  switch (j.type())
8751  {
8752  case value_t::null: // nil
8753  {
8754  oa->write_character(to_char_type(0xC0));
8755  break;
8756  }
8757 
8758  case value_t::boolean: // true and false
8759  {
8760  oa->write_character(j.m_value.boolean
8761  ? to_char_type(0xC3)
8762  : to_char_type(0xC2));
8763  break;
8764  }
8765 
8766  case value_t::number_integer:
8767  {
8768  if (j.m_value.number_integer >= 0)
8769  {
8770  // MessagePack does not differentiate between positive
8771  // signed integers and unsigned integers. Therefore, we used
8772  // the code from the value_t::number_unsigned case here.
8773  if (j.m_value.number_unsigned < 128)
8774  {
8775  // positive fixnum
8776  write_number(static_cast<uint8_t>(j.m_value.number_integer));
8777  }
8778  else if (j.m_value.number_unsigned <= (std::numeric_limits<uint8_t>::max)())
8779  {
8780  // uint 8
8781  oa->write_character(to_char_type(0xCC));
8782  write_number(static_cast<uint8_t>(j.m_value.number_integer));
8783  }
8784  else if (j.m_value.number_unsigned <= (std::numeric_limits<uint16_t>::max)())
8785  {
8786  // uint 16
8787  oa->write_character(to_char_type(0xCD));
8788  write_number(static_cast<uint16_t>(j.m_value.number_integer));
8789  }
8790  else if (j.m_value.number_unsigned <= (std::numeric_limits<uint32_t>::max)())
8791  {
8792  // uint 32
8793  oa->write_character(to_char_type(0xCE));
8794  write_number(static_cast<uint32_t>(j.m_value.number_integer));
8795  }
8796  else if (j.m_value.number_unsigned <= (std::numeric_limits<uint64_t>::max)())
8797  {
8798  // uint 64
8799  oa->write_character(to_char_type(0xCF));
8800  write_number(static_cast<uint64_t>(j.m_value.number_integer));
8801  }
8802  }
8803  else
8804  {
8805  if (j.m_value.number_integer >= -32)
8806  {
8807  // negative fixnum
8808  write_number(static_cast<int8_t>(j.m_value.number_integer));
8809  }
8810  else if (j.m_value.number_integer >= (std::numeric_limits<int8_t>::min)() and
8811  j.m_value.number_integer <= (std::numeric_limits<int8_t>::max)())
8812  {
8813  // int 8
8814  oa->write_character(to_char_type(0xD0));
8815  write_number(static_cast<int8_t>(j.m_value.number_integer));
8816  }
8817  else if (j.m_value.number_integer >= (std::numeric_limits<int16_t>::min)() and
8818  j.m_value.number_integer <= (std::numeric_limits<int16_t>::max)())
8819  {
8820  // int 16
8821  oa->write_character(to_char_type(0xD1));
8822  write_number(static_cast<int16_t>(j.m_value.number_integer));
8823  }
8824  else if (j.m_value.number_integer >= (std::numeric_limits<int32_t>::min)() and
8825  j.m_value.number_integer <= (std::numeric_limits<int32_t>::max)())
8826  {
8827  // int 32
8828  oa->write_character(to_char_type(0xD2));
8829  write_number(static_cast<int32_t>(j.m_value.number_integer));
8830  }
8831  else if (j.m_value.number_integer >= (std::numeric_limits<int64_t>::min)() and
8832  j.m_value.number_integer <= (std::numeric_limits<int64_t>::max)())
8833  {
8834  // int 64
8835  oa->write_character(to_char_type(0xD3));
8836  write_number(static_cast<int64_t>(j.m_value.number_integer));
8837  }
8838  }
8839  break;
8840  }
8841 
8842  case value_t::number_unsigned:
8843  {
8844  if (j.m_value.number_unsigned < 128)
8845  {
8846  // positive fixnum
8847  write_number(static_cast<uint8_t>(j.m_value.number_integer));
8848  }
8849  else if (j.m_value.number_unsigned <= (std::numeric_limits<uint8_t>::max)())
8850  {
8851  // uint 8
8852  oa->write_character(to_char_type(0xCC));
8853  write_number(static_cast<uint8_t>(j.m_value.number_integer));
8854  }
8855  else if (j.m_value.number_unsigned <= (std::numeric_limits<uint16_t>::max)())
8856  {
8857  // uint 16
8858  oa->write_character(to_char_type(0xCD));
8859  write_number(static_cast<uint16_t>(j.m_value.number_integer));
8860  }
8861  else if (j.m_value.number_unsigned <= (std::numeric_limits<uint32_t>::max)())
8862  {
8863  // uint 32
8864  oa->write_character(to_char_type(0xCE));
8865  write_number(static_cast<uint32_t>(j.m_value.number_integer));
8866  }
8867  else if (j.m_value.number_unsigned <= (std::numeric_limits<uint64_t>::max)())
8868  {
8869  // uint 64
8870  oa->write_character(to_char_type(0xCF));
8871  write_number(static_cast<uint64_t>(j.m_value.number_integer));
8872  }
8873  break;
8874  }
8875 
8876  case value_t::number_float:
8877  {
8878  oa->write_character(get_msgpack_float_prefix(j.m_value.number_float));
8879  write_number(j.m_value.number_float);
8880  break;
8881  }
8882 
8883  case value_t::string:
8884  {
8885  // step 1: write control byte and the string length
8886  const auto N = j.m_value.string->size();
8887  if (N <= 31)
8888  {
8889  // fixstr
8890  write_number(static_cast<uint8_t>(0xA0 | N));
8891  }
8892  else if (N <= (std::numeric_limits<uint8_t>::max)())
8893  {
8894  // str 8
8895  oa->write_character(to_char_type(0xD9));
8896  write_number(static_cast<uint8_t>(N));
8897  }
8898  else if (N <= (std::numeric_limits<uint16_t>::max)())
8899  {
8900  // str 16
8901  oa->write_character(to_char_type(0xDA));
8902  write_number(static_cast<uint16_t>(N));
8903  }
8904  else if (N <= (std::numeric_limits<uint32_t>::max)())
8905  {
8906  // str 32
8907  oa->write_character(to_char_type(0xDB));
8908  write_number(static_cast<uint32_t>(N));
8909  }
8910 
8911  // step 2: write the string
8912  oa->write_characters(
8913  reinterpret_cast<const CharType*>(j.m_value.string->c_str()),
8914  j.m_value.string->size());
8915  break;
8916  }
8917 
8918  case value_t::array:
8919  {
8920  // step 1: write control byte and the array size
8921  const auto N = j.m_value.array->size();
8922  if (N <= 15)
8923  {
8924  // fixarray
8925  write_number(static_cast<uint8_t>(0x90 | N));
8926  }
8927  else if (N <= (std::numeric_limits<uint16_t>::max)())
8928  {
8929  // array 16
8930  oa->write_character(to_char_type(0xDC));
8931  write_number(static_cast<uint16_t>(N));
8932  }
8933  else if (N <= (std::numeric_limits<uint32_t>::max)())
8934  {
8935  // array 32
8936  oa->write_character(to_char_type(0xDD));
8937  write_number(static_cast<uint32_t>(N));
8938  }
8939 
8940  // step 2: write each element
8941  for (const auto& el : *j.m_value.array)
8942  {
8943  write_msgpack(el);
8944  }
8945  break;
8946  }
8947 
8948  case value_t::object:
8949  {
8950  // step 1: write control byte and the object size
8951  const auto N = j.m_value.object->size();
8952  if (N <= 15)
8953  {
8954  // fixmap
8955  write_number(static_cast<uint8_t>(0x80 | (N & 0xF)));
8956  }
8957  else if (N <= (std::numeric_limits<uint16_t>::max)())
8958  {
8959  // map 16
8960  oa->write_character(to_char_type(0xDE));
8961  write_number(static_cast<uint16_t>(N));
8962  }
8963  else if (N <= (std::numeric_limits<uint32_t>::max)())
8964  {
8965  // map 32
8966  oa->write_character(to_char_type(0xDF));
8967  write_number(static_cast<uint32_t>(N));
8968  }
8969 
8970  // step 2: write each element
8971  for (const auto& el : *j.m_value.object)
8972  {
8973  write_msgpack(el.first);
8974  write_msgpack(el.second);
8975  }
8976  break;
8977  }
8978 
8979  default:
8980  break;
8981  }
8982  }
8983 
8984  /*!
8985  @param[in] j JSON value to serialize
8986  @param[in] use_count whether to use '#' prefixes (optimized format)
8987  @param[in] use_type whether to use '$' prefixes (optimized format)
8988  @param[in] add_prefix whether prefixes need to be used for this value
8989  */
8990  void write_ubjson(const BasicJsonType& j, const bool use_count,
8991  const bool use_type, const bool add_prefix = true)
8992  {
8993  switch (j.type())
8994  {
8995  case value_t::null:
8996  {
8997  if (add_prefix)
8998  {
8999  oa->write_character(to_char_type('Z'));
9000  }
9001  break;
9002  }
9003 
9004  case value_t::boolean:
9005  {
9006  if (add_prefix)
9007  {
9008  oa->write_character(j.m_value.boolean
9009  ? to_char_type('T')
9010  : to_char_type('F'));
9011  }
9012  break;
9013  }
9014 
9015  case value_t::number_integer:
9016  {
9017  write_number_with_ubjson_prefix(j.m_value.number_integer, add_prefix);
9018  break;
9019  }
9020 
9021  case value_t::number_unsigned:
9022  {
9023  write_number_with_ubjson_prefix(j.m_value.number_unsigned, add_prefix);
9024  break;
9025  }
9026 
9027  case value_t::number_float:
9028  {
9029  write_number_with_ubjson_prefix(j.m_value.number_float, add_prefix);
9030  break;
9031  }
9032 
9033  case value_t::string:
9034  {
9035  if (add_prefix)
9036  {
9037  oa->write_character(to_char_type('S'));
9038  }
9039  write_number_with_ubjson_prefix(j.m_value.string->size(), true);
9040  oa->write_characters(
9041  reinterpret_cast<const CharType*>(j.m_value.string->c_str()),
9042  j.m_value.string->size());
9043  break;
9044  }
9045 
9046  case value_t::array:
9047  {
9048  if (add_prefix)
9049  {
9050  oa->write_character(to_char_type('['));
9051  }
9052 
9053  bool prefix_required = true;
9054  if (use_type and not j.m_value.array->empty())
9055  {
9056  assert(use_count);
9057  const CharType first_prefix = ubjson_prefix(j.front());
9058  const bool same_prefix = std::all_of(j.begin() + 1, j.end(),
9059  [this, first_prefix](const BasicJsonType & v)
9060  {
9061  return ubjson_prefix(v) == first_prefix;
9062  });
9063 
9064  if (same_prefix)
9065  {
9066  prefix_required = false;
9067  oa->write_character(to_char_type('$'));
9068  oa->write_character(first_prefix);
9069  }
9070  }
9071 
9072  if (use_count)
9073  {
9074  oa->write_character(to_char_type('#'));
9075  write_number_with_ubjson_prefix(j.m_value.array->size(), true);
9076  }
9077 
9078  for (const auto& el : *j.m_value.array)
9079  {
9080  write_ubjson(el, use_count, use_type, prefix_required);
9081  }
9082 
9083  if (not use_count)
9084  {
9085  oa->write_character(to_char_type(']'));
9086  }
9087 
9088  break;
9089  }
9090 
9091  case value_t::object:
9092  {
9093  if (add_prefix)
9094  {
9095  oa->write_character(to_char_type('{'));
9096  }
9097 
9098  bool prefix_required = true;
9099  if (use_type and not j.m_value.object->empty())
9100  {
9101  assert(use_count);
9102  const CharType first_prefix = ubjson_prefix(j.front());
9103  const bool same_prefix = std::all_of(j.begin(), j.end(),
9104  [this, first_prefix](const BasicJsonType & v)
9105  {
9106  return ubjson_prefix(v) == first_prefix;
9107  });
9108 
9109  if (same_prefix)
9110  {
9111  prefix_required = false;
9112  oa->write_character(to_char_type('$'));
9113  oa->write_character(first_prefix);
9114  }
9115  }
9116 
9117  if (use_count)
9118  {
9119  oa->write_character(to_char_type('#'));
9120  write_number_with_ubjson_prefix(j.m_value.object->size(), true);
9121  }
9122 
9123  for (const auto& el : *j.m_value.object)
9124  {
9125  write_number_with_ubjson_prefix(el.first.size(), true);
9126  oa->write_characters(
9127  reinterpret_cast<const CharType*>(el.first.c_str()),
9128  el.first.size());
9129  write_ubjson(el.second, use_count, use_type, prefix_required);
9130  }
9131 
9132  if (not use_count)
9133  {
9134  oa->write_character(to_char_type('}'));
9135  }
9136 
9137  break;
9138  }
9139 
9140  default:
9141  break;
9142  }
9143  }
9144 
9145  private:
9146  //////////
9147  // BSON //
9148  //////////
9149 
9150  /*!
9151  @return The size of a BSON document entry header, including the id marker
9152  and the entry name size (and its null-terminator).
9153  */
9154  static std::size_t calc_bson_entry_header_size(const string_t& name)
9155  {
9156  const auto it = name.find(static_cast<typename string_t::value_type>(0));
9157  if (JSON_UNLIKELY(it != BasicJsonType::string_t::npos))
9158  {
9159  JSON_THROW(out_of_range::create(409,
9160  "BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) + ")"));
9161  }
9162 
9163  return /*id*/ 1ul + name.size() + /*zero-terminator*/1u;
9164  }
9165 
9166  /*!
9167  @brief Writes the given @a element_type and @a name to the output adapter
9168  */
9169  void write_bson_entry_header(const string_t& name,
9170  const std::uint8_t element_type)
9171  {
9172  oa->write_character(to_char_type(element_type)); // boolean
9173  oa->write_characters(
9174  reinterpret_cast<const CharType*>(name.c_str()),
9175  name.size() + 1u);
9176  }
9177 
9178  /*!
9179  @brief Writes a BSON element with key @a name and boolean value @a value
9180  */
9181  void write_bson_boolean(const string_t& name,
9182  const bool value)
9183  {
9184  write_bson_entry_header(name, 0x08);
9185  oa->write_character(value ? to_char_type(0x01) : to_char_type(0x00));
9186  }
9187 
9188  /*!
9189  @brief Writes a BSON element with key @a name and double value @a value
9190  */
9191  void write_bson_double(const string_t& name,
9192  const double value)
9193  {
9194  write_bson_entry_header(name, 0x01);
9195  write_number<double, true>(value);
9196  }
9197 
9198  /*!
9199  @return The size of the BSON-encoded string in @a value
9200  */
9201  static std::size_t calc_bson_string_size(const string_t& value)
9202  {
9203  return sizeof(std::int32_t) + value.size() + 1ul;
9204  }
9205 
9206  /*!
9207  @brief Writes a BSON element with key @a name and string value @a value
9208  */
9209  void write_bson_string(const string_t& name,
9210  const string_t& value)
9211  {
9212  write_bson_entry_header(name, 0x02);
9213 
9214  write_number<std::int32_t, true>(static_cast<std::int32_t>(value.size() + 1ul));
9215  oa->write_characters(
9216  reinterpret_cast<const CharType*>(value.c_str()),
9217  value.size() + 1);
9218  }
9219 
9220  /*!
9221  @brief Writes a BSON element with key @a name and null value
9222  */
9223  void write_bson_null(const string_t& name)
9224  {
9225  write_bson_entry_header(name, 0x0A);
9226  }
9227 
9228  /*!
9229  @return The size of the BSON-encoded integer @a value
9230  */
9231  static std::size_t calc_bson_integer_size(const std::int64_t value)
9232  {
9233  if ((std::numeric_limits<std::int32_t>::min)() <= value and value <= (std::numeric_limits<std::int32_t>::max)())
9234  {
9235  return sizeof(std::int32_t);
9236  }
9237  else
9238  {
9239  return sizeof(std::int64_t);
9240  }
9241  }
9242 
9243  /*!
9244  @brief Writes a BSON element with key @a name and integer @a value
9245  */
9246  void write_bson_integer(const string_t& name,
9247  const std::int64_t value)
9248  {
9249  if ((std::numeric_limits<std::int32_t>::min)() <= value and value <= (std::numeric_limits<std::int32_t>::max)())
9250  {
9251  write_bson_entry_header(name, 0x10); // int32
9252  write_number<std::int32_t, true>(static_cast<std::int32_t>(value));
9253  }
9254  else
9255  {
9256  write_bson_entry_header(name, 0x12); // int64
9257  write_number<std::int64_t, true>(static_cast<std::int64_t>(value));
9258  }
9259  }
9260 
9261  /*!
9262  @return The size of the BSON-encoded unsigned integer in @a j
9263  */
9264  static constexpr std::size_t calc_bson_unsigned_size(const std::uint64_t value) noexcept
9265  {
9266  return (value <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
9267  ? sizeof(std::int32_t)
9268  : sizeof(std::int64_t);
9269  }
9270 
9271  /*!
9272  @brief Writes a BSON element with key @a name and unsigned @a value
9273  */
9274  void write_bson_unsigned(const string_t& name,
9275  const std::uint64_t value)
9276  {
9277  if (value <= static_cast<std::uint64_t>((std::numeric_limits<std::int32_t>::max)()))
9278  {
9279  write_bson_entry_header(name, 0x10 /* int32 */);
9280  write_number<std::int32_t, true>(static_cast<std::int32_t>(value));
9281  }
9282  else if (value <= static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()))
9283  {
9284  write_bson_entry_header(name, 0x12 /* int64 */);
9285  write_number<std::int64_t, true>(static_cast<std::int64_t>(value));
9286  }
9287  else
9288  {
9289  JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(value) + " cannot be represented by BSON as it does not fit int64"));
9290  }
9291  }
9292 
9293  /*!
9294  @brief Writes a BSON element with key @a name and object @a value
9295  */
9296  void write_bson_object_entry(const string_t& name,
9297  const typename BasicJsonType::object_t& value)
9298  {
9299  write_bson_entry_header(name, 0x03); // object
9300  write_bson_object(value);
9301  }
9302 
9303  /*!
9304  @return The size of the BSON-encoded array @a value
9305  */
9306  static std::size_t calc_bson_array_size(const typename BasicJsonType::array_t& value)
9307  {
9308  std::size_t embedded_document_size = 0ul;
9309  std::size_t array_index = 0ul;
9310 
9311  for (const auto& el : value)
9312  {
9313  embedded_document_size += calc_bson_element_size(std::to_string(array_index++), el);
9314  }
9315 
9316  return sizeof(std::int32_t) + embedded_document_size + 1ul;
9317  }
9318 
9319  /*!
9320  @brief Writes a BSON element with key @a name and array @a value
9321  */
9322  void write_bson_array(const string_t& name,
9323  const typename BasicJsonType::array_t& value)
9324  {
9325  write_bson_entry_header(name, 0x04); // array
9326  write_number<std::int32_t, true>(static_cast<std::int32_t>(calc_bson_array_size(value)));
9327 
9328  std::size_t array_index = 0ul;
9329 
9330  for (const auto& el : value)
9331  {
9332  write_bson_element(std::to_string(array_index++), el);
9333  }
9334 
9335  oa->write_character(to_char_type(0x00));
9336  }
9337 
9338  /*!
9339  @brief Calculates the size necessary to serialize the JSON value @a j with its @a name
9340  @return The calculated size for the BSON document entry for @a j with the given @a name.
9341  */
9342  static std::size_t calc_bson_element_size(const string_t& name,
9343  const BasicJsonType& j)
9344  {
9345  const auto header_size = calc_bson_entry_header_size(name);
9346  switch (j.type())
9347  {
9348  case value_t::object:
9349  return header_size + calc_bson_object_size(*j.m_value.object);
9350 
9351  case value_t::array:
9352  return header_size + calc_bson_array_size(*j.m_value.array);
9353 
9354  case value_t::boolean:
9355  return header_size + 1ul;
9356 
9357  case value_t::number_float:
9358  return header_size + 8ul;
9359 
9360  case value_t::number_integer:
9361  return header_size + calc_bson_integer_size(j.m_value.number_integer);
9362 
9363  case value_t::number_unsigned:
9364  return header_size + calc_bson_unsigned_size(j.m_value.number_unsigned);
9365 
9366  case value_t::string:
9367  return header_size + calc_bson_string_size(*j.m_value.string);
9368 
9369  case value_t::null:
9370  return header_size + 0ul;
9371 
9372  // LCOV_EXCL_START
9373  default:
9374  assert(false);
9375  return 0ul;
9376  // LCOV_EXCL_STOP
9377  };
9378  }
9379 
9380  /*!
9381  @brief Serializes the JSON value @a j to BSON and associates it with the
9382  key @a name.
9383  @param name The name to associate with the JSON entity @a j within the
9384  current BSON document
9385  @return The size of the BSON entry
9386  */
9387  void write_bson_element(const string_t& name,
9388  const BasicJsonType& j)
9389  {
9390  switch (j.type())
9391  {
9392  case value_t::object:
9393  return write_bson_object_entry(name, *j.m_value.object);
9394 
9395  case value_t::array:
9396  return write_bson_array(name, *j.m_value.array);
9397 
9398  case value_t::boolean:
9399  return write_bson_boolean(name, j.m_value.boolean);
9400 
9401  case value_t::number_float:
9402  return write_bson_double(name, j.m_value.number_float);
9403 
9404  case value_t::number_integer:
9405  return write_bson_integer(name, j.m_value.number_integer);
9406 
9407  case value_t::number_unsigned:
9408  return write_bson_unsigned(name, j.m_value.number_unsigned);
9409 
9410  case value_t::string:
9411  return write_bson_string(name, *j.m_value.string);
9412 
9413  case value_t::null:
9414  return write_bson_null(name);
9415 
9416  // LCOV_EXCL_START
9417  default:
9418  assert(false);
9419  return;
9420  // LCOV_EXCL_STOP
9421  };
9422  }
9423 
9424  /*!
9425  @brief Calculates the size of the BSON serialization of the given
9426  JSON-object @a j.
9427  @param[in] j JSON value to serialize
9428  @pre j.type() == value_t::object
9429  */
9430  static std::size_t calc_bson_object_size(const typename BasicJsonType::object_t& value)
9431  {
9432  std::size_t document_size = std::accumulate(value.begin(), value.end(), 0ul,
9433  [](size_t result, const typename BasicJsonType::object_t::value_type & el)
9434  {
9435  return result += calc_bson_element_size(el.first, el.second);
9436  });
9437 
9438  return sizeof(std::int32_t) + document_size + 1ul;
9439  }
9440 
9441  /*!
9442  @param[in] j JSON value to serialize
9443  @pre j.type() == value_t::object
9444  */
9445  void write_bson_object(const typename BasicJsonType::object_t& value)
9446  {
9447  write_number<std::int32_t, true>(static_cast<std::int32_t>(calc_bson_object_size(value)));
9448 
9449  for (const auto& el : value)
9450  {
9451  write_bson_element(el.first, el.second);
9452  }
9453 
9454  oa->write_character(to_char_type(0x00));
9455  }
9456 
9457  //////////
9458  // CBOR //
9459  //////////
9460 
9461  static constexpr CharType get_cbor_float_prefix(float /*unused*/)
9462  {
9463  return to_char_type(0xFA); // Single-Precision Float
9464  }
9465 
9466  static constexpr CharType get_cbor_float_prefix(double /*unused*/)
9467  {
9468  return to_char_type(0xFB); // Double-Precision Float
9469  }
9470 
9471  /////////////
9472  // MsgPack //
9473  /////////////
9474 
9475  static constexpr CharType get_msgpack_float_prefix(float /*unused*/)
9476  {
9477  return to_char_type(0xCA); // float 32
9478  }
9479 
9480  static constexpr CharType get_msgpack_float_prefix(double /*unused*/)
9481  {
9482  return to_char_type(0xCB); // float 64
9483  }
9484 
9485  ////////////
9486  // UBJSON //
9487  ////////////
9488 
9489  // UBJSON: write number (floating point)
9490  template<typename NumberType, typename std::enable_if<
9491  std::is_floating_point<NumberType>::value, int>::type = 0>
9492  void write_number_with_ubjson_prefix(const NumberType n,
9493  const bool add_prefix)
9494  {
9495  if (add_prefix)
9496  {
9497  oa->write_character(get_ubjson_float_prefix(n));
9498  }
9499  write_number(n);
9500  }
9501 
9502  // UBJSON: write number (unsigned integer)
9503  template<typename NumberType, typename std::enable_if<
9504  std::is_unsigned<NumberType>::value, int>::type = 0>
9505  void write_number_with_ubjson_prefix(const NumberType n,
9506  const bool add_prefix)
9507  {
9508  if (n <= static_cast<uint64_t>((std::numeric_limits<int8_t>::max)()))
9509  {
9510  if (add_prefix)
9511  {
9512  oa->write_character(to_char_type('i')); // int8
9513  }
9514  write_number(static_cast<uint8_t>(n));
9515  }
9516  else if (n <= (std::numeric_limits<uint8_t>::max)())
9517  {
9518  if (add_prefix)
9519  {
9520  oa->write_character(to_char_type('U')); // uint8
9521  }
9522  write_number(static_cast<uint8_t>(n));
9523  }
9524  else if (n <= static_cast<uint64_t>((std::numeric_limits<int16_t>::max)()))
9525  {
9526  if (add_prefix)
9527  {
9528  oa->write_character(to_char_type('I')); // int16
9529  }
9530  write_number(static_cast<int16_t>(n));
9531  }
9532  else if (n <= static_cast<uint64_t>((std::numeric_limits<int32_t>::max)()))
9533  {
9534  if (add_prefix)
9535  {
9536  oa->write_character(to_char_type('l')); // int32
9537  }
9538  write_number(static_cast<int32_t>(n));
9539  }
9540  else if (n <= static_cast<uint64_t>((std::numeric_limits<int64_t>::max)()))
9541  {
9542  if (add_prefix)
9543  {
9544  oa->write_character(to_char_type('L')); // int64
9545  }
9546  write_number(static_cast<int64_t>(n));
9547  }
9548  else
9549  {
9550  JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(n) + " cannot be represented by UBJSON as it does not fit int64"));
9551  }
9552  }
9553 
9554  // UBJSON: write number (signed integer)
9555  template<typename NumberType, typename std::enable_if<
9556  std::is_signed<NumberType>::value and
9557  not std::is_floating_point<NumberType>::value, int>::type = 0>
9558  void write_number_with_ubjson_prefix(const NumberType n,
9559  const bool add_prefix)
9560  {
9561  if ((std::numeric_limits<int8_t>::min)() <= n and n <= (std::numeric_limits<int8_t>::max)())
9562  {
9563  if (add_prefix)
9564  {
9565  oa->write_character(to_char_type('i')); // int8
9566  }
9567  write_number(static_cast<int8_t>(n));
9568  }
9569  else if (static_cast<int64_t>((std::numeric_limits<uint8_t>::min)()) <= n and n <= static_cast<int64_t>((std::numeric_limits<uint8_t>::max)()))
9570  {
9571  if (add_prefix)
9572  {
9573  oa->write_character(to_char_type('U')); // uint8
9574  }
9575  write_number(static_cast<uint8_t>(n));
9576  }
9577  else if ((std::numeric_limits<int16_t>::min)() <= n and n <= (std::numeric_limits<int16_t>::max)())
9578  {
9579  if (add_prefix)
9580  {
9581  oa->write_character(to_char_type('I')); // int16
9582  }
9583  write_number(static_cast<int16_t>(n));
9584  }
9585  else if ((std::numeric_limits<int32_t>::min)() <= n and n <= (std::numeric_limits<int32_t>::max)())
9586  {
9587  if (add_prefix)
9588  {
9589  oa->write_character(to_char_type('l')); // int32
9590  }
9591  write_number(static_cast<int32_t>(n));
9592  }
9593  else if ((std::numeric_limits<int64_t>::min)() <= n and n <= (std::numeric_limits<int64_t>::max)())
9594  {
9595  if (add_prefix)
9596  {
9597  oa->write_character(to_char_type('L')); // int64
9598  }
9599  write_number(static_cast<int64_t>(n));
9600  }
9601  // LCOV_EXCL_START
9602  else
9603  {
9604  JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(n) + " cannot be represented by UBJSON as it does not fit int64"));
9605  }
9606  // LCOV_EXCL_STOP
9607  }
9608 
9609  /*!
9610  @brief determine the type prefix of container values
9611 
9612  @note This function does not need to be 100% accurate when it comes to
9613  integer limits. In case a number exceeds the limits of int64_t,
9614  this will be detected by a later call to function
9615  write_number_with_ubjson_prefix. Therefore, we return 'L' for any
9616  value that does not fit the previous limits.
9617  */
9618  CharType ubjson_prefix(const BasicJsonType& j) const noexcept
9619  {
9620  switch (j.type())
9621  {
9622  case value_t::null:
9623  return 'Z';
9624 
9625  case value_t::boolean:
9626  return j.m_value.boolean ? 'T' : 'F';
9627 
9628  case value_t::number_integer:
9629  {
9630  if ((std::numeric_limits<int8_t>::min)() <= j.m_value.number_integer and j.m_value.number_integer <= (std::numeric_limits<int8_t>::max)())
9631  {
9632  return 'i';
9633  }
9634  if ((std::numeric_limits<uint8_t>::min)() <= j.m_value.number_integer and j.m_value.number_integer <= (std::numeric_limits<uint8_t>::max)())
9635  {
9636  return 'U';
9637  }
9638  if ((std::numeric_limits<int16_t>::min)() <= j.m_value.number_integer and j.m_value.number_integer <= (std::numeric_limits<int16_t>::max)())
9639  {
9640  return 'I';
9641  }
9642  if ((std::numeric_limits<int32_t>::min)() <= j.m_value.number_integer and j.m_value.number_integer <= (std::numeric_limits<int32_t>::max)())
9643  {
9644  return 'l';
9645  }
9646  // no check and assume int64_t (see note above)
9647  return 'L';
9648  }
9649 
9650  case value_t::number_unsigned:
9651  {
9652  if (j.m_value.number_unsigned <= (std::numeric_limits<int8_t>::max)())
9653  {
9654  return 'i';
9655  }
9656  if (j.m_value.number_unsigned <= (std::numeric_limits<uint8_t>::max)())
9657  {
9658  return 'U';
9659  }
9660  if (j.m_value.number_unsigned <= (std::numeric_limits<int16_t>::max)())
9661  {
9662  return 'I';
9663  }
9664  if (j.m_value.number_unsigned <= (std::numeric_limits<int32_t>::max)())
9665  {
9666  return 'l';
9667  }
9668  // no check and assume int64_t (see note above)
9669  return 'L';
9670  }
9671 
9672  case value_t::number_float:
9673  return get_ubjson_float_prefix(j.m_value.number_float);
9674 
9675  case value_t::string:
9676  return 'S';
9677 
9678  case value_t::array:
9679  return '[';
9680 
9681  case value_t::object:
9682  return '{';
9683 
9684  default: // discarded values
9685  return 'N';
9686  }
9687  }
9688 
9689  static constexpr CharType get_ubjson_float_prefix(float /*unused*/)
9690  {
9691  return 'd'; // float 32
9692  }
9693 
9694  static constexpr CharType get_ubjson_float_prefix(double /*unused*/)
9695  {
9696  return 'D'; // float 64
9697  }
9698 
9699  ///////////////////////
9700  // Utility functions //
9701  ///////////////////////
9702 
9703  /*
9704  @brief write a number to output input
9705  @param[in] n number of type @a NumberType
9706  @tparam NumberType the type of the number
9707  @tparam OutputIsLittleEndian Set to true if output data is
9708  required to be little endian
9709 
9710  @note This function needs to respect the system's endianess, because bytes
9711  in CBOR, MessagePack, and UBJSON are stored in network order (big
9712  endian) and therefore need reordering on little endian systems.
9713  */
9714  template<typename NumberType, bool OutputIsLittleEndian = false>
9715  void write_number(const NumberType n)
9716  {
9717  // step 1: write number to array of length NumberType
9718  std::array<CharType, sizeof(NumberType)> vec;
9719  std::memcpy(vec.data(), &n, sizeof(NumberType));
9720 
9721  // step 2: write array to output (with possible reordering)
9722  if (is_little_endian and not OutputIsLittleEndian)
9723  {
9724  // reverse byte order prior to conversion if necessary
9725  std::reverse(vec.begin(), vec.end());
9726  }
9727 
9728  oa->write_characters(vec.data(), sizeof(NumberType));
9729  }
9730 
9731  public:
9732  // The following to_char_type functions are implement the conversion
9733  // between uint8_t and CharType. In case CharType is not unsigned,
9734  // such a conversion is required to allow values greater than 128.
9735  // See <https://github.com/nlohmann/json/issues/1286> for a discussion.
9736  template < typename C = CharType,
9737  enable_if_t < std::is_signed<C>::value and std::is_signed<char>::value > * = nullptr >
9738  static constexpr CharType to_char_type(std::uint8_t x) noexcept
9739  {
9740  return *reinterpret_cast<char*>(&x);
9741  }
9742 
9743  template < typename C = CharType,
9744  enable_if_t < std::is_signed<C>::value and std::is_unsigned<char>::value > * = nullptr >
9745  static CharType to_char_type(std::uint8_t x) noexcept
9746  {
9747  static_assert(sizeof(std::uint8_t) == sizeof(CharType), "size of CharType must be equal to std::uint8_t");
9748  static_assert(std::is_pod<CharType>::value, "CharType must be POD");
9749  CharType result;
9750  std::memcpy(&result, &x, sizeof(x));
9751  return result;
9752  }
9753 
9754  template<typename C = CharType,
9755  enable_if_t<std::is_unsigned<C>::value>* = nullptr>
9756  static constexpr CharType to_char_type(std::uint8_t x) noexcept
9757  {
9758  return x;
9759  }
9760 
9761  template < typename InputCharType, typename C = CharType,
9762  enable_if_t <
9763  std::is_signed<C>::value and
9764  std::is_signed<char>::value and
9765  std::is_same<char, typename std::remove_cv<InputCharType>::type>::value
9766  > * = nullptr >
9767  static constexpr CharType to_char_type(InputCharType x) noexcept
9768  {
9769  return x;
9770  }
9771 
9772  private:
9773  /// whether we can assume little endianess
9774  const bool is_little_endian = binary_reader<BasicJsonType>::little_endianess();
9775 
9776  /// the output
9777  output_adapter_t<CharType> oa = nullptr;
9778 };
9779 } // namespace detail
9780 } // namespace nlohmann
9781 
9782 // #include <nlohmann/detail/output/serializer.hpp>
9783 
9784 
9785 #include <algorithm> // reverse, remove, fill, find, none_of
9786 #include <array> // array
9787 #include <cassert> // assert
9788 #include <ciso646> // and, or
9789 #include <clocale> // localeconv, lconv
9790 #include <cmath> // labs, isfinite, isnan, signbit
9791 #include <cstddef> // size_t, ptrdiff_t
9792 #include <cstdint> // uint8_t
9793 #include <cstdio> // snprintf
9794 #include <limits> // numeric_limits
9795 #include <string> // string
9796 #include <type_traits> // is_same
9797 
9798 // #include <nlohmann/detail/exceptions.hpp>
9799 
9800 // #include <nlohmann/detail/conversions/to_chars.hpp>
9801 
9802 
9803 #include <cassert> // assert
9804 #include <ciso646> // or, and, not
9805 #include <cmath> // signbit, isfinite
9806 #include <cstdint> // intN_t, uintN_t
9807 #include <cstring> // memcpy, memmove
9808 
9809 namespace nlohmann
9810 {
9811 namespace detail
9812 {
9813 
9814 /*!
9815 @brief implements the Grisu2 algorithm for binary to decimal floating-point
9816 conversion.
9817 
9818 This implementation is a slightly modified version of the reference
9819 implementation which may be obtained from
9820 http://florian.loitsch.com/publications (bench.tar.gz).
9821 
9822 The code is distributed under the MIT license, Copyright (c) 2009 Florian Loitsch.
9823 
9824 For a detailed description of the algorithm see:
9825 
9826 [1] Loitsch, "Printing Floating-Point Numbers Quickly and Accurately with
9827  Integers", Proceedings of the ACM SIGPLAN 2010 Conference on Programming
9828  Language Design and Implementation, PLDI 2010
9829 [2] Burger, Dybvig, "Printing Floating-Point Numbers Quickly and Accurately",
9830  Proceedings of the ACM SIGPLAN 1996 Conference on Programming Language
9831  Design and Implementation, PLDI 1996
9832 */
9833 namespace dtoa_impl
9834 {
9835 
9836 template <typename Target, typename Source>
9837 Target reinterpret_bits(const Source source)
9838 {
9839  static_assert(sizeof(Target) == sizeof(Source), "size mismatch");
9840 
9841  Target target;
9842  std::memcpy(&target, &source, sizeof(Source));
9843  return target;
9844 }
9845 
9846 struct diyfp // f * 2^e
9847 {
9848  static constexpr int kPrecision = 64; // = q
9849 
9850  uint64_t f = 0;
9851  int e = 0;
9852 
9853  constexpr diyfp(uint64_t f_, int e_) noexcept : f(f_), e(e_) {}
9854 
9855  /*!
9856  @brief returns x - y
9857  @pre x.e == y.e and x.f >= y.f
9858  */
9859  static diyfp sub(const diyfp& x, const diyfp& y) noexcept
9860  {
9861  assert(x.e == y.e);
9862  assert(x.f >= y.f);
9863 
9864  return {x.f - y.f, x.e};
9865  }
9866 
9867  /*!
9868  @brief returns x * y
9869  @note The result is rounded. (Only the upper q bits are returned.)
9870  */
9871  static diyfp mul(const diyfp& x, const diyfp& y) noexcept
9872  {
9873  static_assert(kPrecision == 64, "internal error");
9874 
9875  // Computes:
9876  // f = round((x.f * y.f) / 2^q)
9877  // e = x.e + y.e + q
9878 
9879  // Emulate the 64-bit * 64-bit multiplication:
9880  //
9881  // p = u * v
9882  // = (u_lo + 2^32 u_hi) (v_lo + 2^32 v_hi)
9883  // = (u_lo v_lo ) + 2^32 ((u_lo v_hi ) + (u_hi v_lo )) + 2^64 (u_hi v_hi )
9884  // = (p0 ) + 2^32 ((p1 ) + (p2 )) + 2^64 (p3 )
9885  // = (p0_lo + 2^32 p0_hi) + 2^32 ((p1_lo + 2^32 p1_hi) + (p2_lo + 2^32 p2_hi)) + 2^64 (p3 )
9886  // = (p0_lo ) + 2^32 (p0_hi + p1_lo + p2_lo ) + 2^64 (p1_hi + p2_hi + p3)
9887  // = (p0_lo ) + 2^32 (Q ) + 2^64 (H )
9888  // = (p0_lo ) + 2^32 (Q_lo + 2^32 Q_hi ) + 2^64 (H )
9889  //
9890  // (Since Q might be larger than 2^32 - 1)
9891  //
9892  // = (p0_lo + 2^32 Q_lo) + 2^64 (Q_hi + H)
9893  //
9894  // (Q_hi + H does not overflow a 64-bit int)
9895  //
9896  // = p_lo + 2^64 p_hi
9897 
9898  const uint64_t u_lo = x.f & 0xFFFFFFFF;
9899  const uint64_t u_hi = x.f >> 32;
9900  const uint64_t v_lo = y.f & 0xFFFFFFFF;
9901  const uint64_t v_hi = y.f >> 32;
9902 
9903  const uint64_t p0 = u_lo * v_lo;
9904  const uint64_t p1 = u_lo * v_hi;
9905  const uint64_t p2 = u_hi * v_lo;
9906  const uint64_t p3 = u_hi * v_hi;
9907 
9908  const uint64_t p0_hi = p0 >> 32;
9909  const uint64_t p1_lo = p1 & 0xFFFFFFFF;
9910  const uint64_t p1_hi = p1 >> 32;
9911  const uint64_t p2_lo = p2 & 0xFFFFFFFF;
9912  const uint64_t p2_hi = p2 >> 32;
9913 
9914  uint64_t Q = p0_hi + p1_lo + p2_lo;
9915 
9916  // The full product might now be computed as
9917  //
9918  // p_hi = p3 + p2_hi + p1_hi + (Q >> 32)
9919  // p_lo = p0_lo + (Q << 32)
9920  //
9921  // But in this particular case here, the full p_lo is not required.
9922  // Effectively we only need to add the highest bit in p_lo to p_hi (and
9923  // Q_hi + 1 does not overflow).
9924 
9925  Q += uint64_t{1} << (64 - 32 - 1); // round, ties up
9926 
9927  const uint64_t h = p3 + p2_hi + p1_hi + (Q >> 32);
9928 
9929  return {h, x.e + y.e + 64};
9930  }
9931 
9932  /*!
9933  @brief normalize x such that the significand is >= 2^(q-1)
9934  @pre x.f != 0
9935  */
9936  static diyfp normalize(diyfp x) noexcept
9937  {
9938  assert(x.f != 0);
9939 
9940  while ((x.f >> 63) == 0)
9941  {
9942  x.f <<= 1;
9943  x.e--;
9944  }
9945 
9946  return x;
9947  }
9948 
9949  /*!
9950  @brief normalize x such that the result has the exponent E
9951  @pre e >= x.e and the upper e - x.e bits of x.f must be zero.
9952  */
9953  static diyfp normalize_to(const diyfp& x, const int target_exponent) noexcept
9954  {
9955  const int delta = x.e - target_exponent;
9956 
9957  assert(delta >= 0);
9958  assert(((x.f << delta) >> delta) == x.f);
9959 
9960  return {x.f << delta, target_exponent};
9961  }
9962 };
9963 
9964 struct boundaries
9965 {
9966  diyfp w;
9967  diyfp minus;
9968  diyfp plus;
9969 };
9970 
9971 /*!
9972 Compute the (normalized) diyfp representing the input number 'value' and its
9973 boundaries.
9974 
9975 @pre value must be finite and positive
9976 */
9977 template <typename FloatType>
9978 boundaries compute_boundaries(FloatType value)
9979 {
9980  assert(std::isfinite(value));
9981  assert(value > 0);
9982 
9983  // Convert the IEEE representation into a diyfp.
9984  //
9985  // If v is denormal:
9986  // value = 0.F * 2^(1 - bias) = ( F) * 2^(1 - bias - (p-1))
9987  // If v is normalized:
9988  // value = 1.F * 2^(E - bias) = (2^(p-1) + F) * 2^(E - bias - (p-1))
9989 
9990  static_assert(std::numeric_limits<FloatType>::is_iec559,
9991  "internal error: dtoa_short requires an IEEE-754 floating-point implementation");
9992 
9993  constexpr int kPrecision = std::numeric_limits<FloatType>::digits; // = p (includes the hidden bit)
9994  constexpr int kBias = std::numeric_limits<FloatType>::max_exponent - 1 + (kPrecision - 1);
9995  constexpr int kMinExp = 1 - kBias;
9996  constexpr uint64_t kHiddenBit = uint64_t{1} << (kPrecision - 1); // = 2^(p-1)
9997 
9998  using bits_type = typename std::conditional< kPrecision == 24, uint32_t, uint64_t >::type;
9999 
10000  const uint64_t bits = reinterpret_bits<bits_type>(value);
10001  const uint64_t E = bits >> (kPrecision - 1);
10002  const uint64_t F = bits & (kHiddenBit - 1);
10003 
10004  const bool is_denormal = (E == 0);
10005  const diyfp v = is_denormal
10006  ? diyfp(F, kMinExp)
10007  : diyfp(F + kHiddenBit, static_cast<int>(E) - kBias);
10008 
10009  // Compute the boundaries m- and m+ of the floating-point value
10010  // v = f * 2^e.
10011  //
10012  // Determine v- and v+, the floating-point predecessor and successor if v,
10013  // respectively.
10014  //
10015  // v- = v - 2^e if f != 2^(p-1) or e == e_min (A)
10016  // = v - 2^(e-1) if f == 2^(p-1) and e > e_min (B)
10017  //
10018  // v+ = v + 2^e
10019  //
10020  // Let m- = (v- + v) / 2 and m+ = (v + v+) / 2. All real numbers _strictly_
10021  // between m- and m+ round to v, regardless of how the input rounding
10022  // algorithm breaks ties.
10023  //
10024  // ---+-------------+-------------+-------------+-------------+--- (A)
10025  // v- m- v m+ v+
10026  //
10027  // -----------------+------+------+-------------+-------------+--- (B)
10028  // v- m- v m+ v+
10029 
10030  const bool lower_boundary_is_closer = (F == 0 and E > 1);
10031  const diyfp m_plus = diyfp(2 * v.f + 1, v.e - 1);
10032  const diyfp m_minus = lower_boundary_is_closer
10033  ? diyfp(4 * v.f - 1, v.e - 2) // (B)
10034  : diyfp(2 * v.f - 1, v.e - 1); // (A)
10035 
10036  // Determine the normalized w+ = m+.
10037  const diyfp w_plus = diyfp::normalize(m_plus);
10038 
10039  // Determine w- = m- such that e_(w-) = e_(w+).
10040  const diyfp w_minus = diyfp::normalize_to(m_minus, w_plus.e);
10041 
10042  return {diyfp::normalize(v), w_minus, w_plus};
10043 }
10044 
10045 // Given normalized diyfp w, Grisu needs to find a (normalized) cached
10046 // power-of-ten c, such that the exponent of the product c * w = f * 2^e lies
10047 // within a certain range [alpha, gamma] (Definition 3.2 from [1])
10048 //
10049 // alpha <= e = e_c + e_w + q <= gamma
10050 //
10051 // or
10052 //
10053 // f_c * f_w * 2^alpha <= f_c 2^(e_c) * f_w 2^(e_w) * 2^q
10054 // <= f_c * f_w * 2^gamma
10055 //
10056 // Since c and w are normalized, i.e. 2^(q-1) <= f < 2^q, this implies
10057 //
10058 // 2^(q-1) * 2^(q-1) * 2^alpha <= c * w * 2^q < 2^q * 2^q * 2^gamma
10059 //
10060 // or
10061 //
10062 // 2^(q - 2 + alpha) <= c * w < 2^(q + gamma)
10063 //
10064 // The choice of (alpha,gamma) determines the size of the table and the form of
10065 // the digit generation procedure. Using (alpha,gamma)=(-60,-32) works out well
10066 // in practice:
10067 //
10068 // The idea is to cut the number c * w = f * 2^e into two parts, which can be
10069 // processed independently: An integral part p1, and a fractional part p2:
10070 //
10071 // f * 2^e = ( (f div 2^-e) * 2^-e + (f mod 2^-e) ) * 2^e
10072 // = (f div 2^-e) + (f mod 2^-e) * 2^e
10073 // = p1 + p2 * 2^e
10074 //
10075 // The conversion of p1 into decimal form requires a series of divisions and
10076 // modulos by (a power of) 10. These operations are faster for 32-bit than for
10077 // 64-bit integers, so p1 should ideally fit into a 32-bit integer. This can be
10078 // achieved by choosing
10079 //
10080 // -e >= 32 or e <= -32 := gamma
10081 //
10082 // In order to convert the fractional part
10083 //
10084 // p2 * 2^e = p2 / 2^-e = d[-1] / 10^1 + d[-2] / 10^2 + ...
10085 //
10086 // into decimal form, the fraction is repeatedly multiplied by 10 and the digits
10087 // d[-i] are extracted in order:
10088 //
10089 // (10 * p2) div 2^-e = d[-1]
10090 // (10 * p2) mod 2^-e = d[-2] / 10^1 + ...
10091 //
10092 // The multiplication by 10 must not overflow. It is sufficient to choose
10093 //
10094 // 10 * p2 < 16 * p2 = 2^4 * p2 <= 2^64.
10095 //
10096 // Since p2 = f mod 2^-e < 2^-e,
10097 //
10098 // -e <= 60 or e >= -60 := alpha
10099 
10100 constexpr int kAlpha = -60;
10101 constexpr int kGamma = -32;
10102 
10103 struct cached_power // c = f * 2^e ~= 10^k
10104 {
10105  uint64_t f;
10106  int e;
10107  int k;
10108 };
10109 
10110 /*!
10111 For a normalized diyfp w = f * 2^e, this function returns a (normalized) cached
10112 power-of-ten c = f_c * 2^e_c, such that the exponent of the product w * c
10113 satisfies (Definition 3.2 from [1])
10114 
10115  alpha <= e_c + e + q <= gamma.
10116 */
10117 inline cached_power get_cached_power_for_binary_exponent(int e)
10118 {
10119  // Now
10120  //
10121  // alpha <= e_c + e + q <= gamma (1)
10122  // ==> f_c * 2^alpha <= c * 2^e * 2^q
10123  //
10124  // and since the c's are normalized, 2^(q-1) <= f_c,
10125  //
10126  // ==> 2^(q - 1 + alpha) <= c * 2^(e + q)
10127  // ==> 2^(alpha - e - 1) <= c
10128  //
10129  // If c were an exakt power of ten, i.e. c = 10^k, one may determine k as
10130  //
10131  // k = ceil( log_10( 2^(alpha - e - 1) ) )
10132  // = ceil( (alpha - e - 1) * log_10(2) )
10133  //
10134  // From the paper:
10135  // "In theory the result of the procedure could be wrong since c is rounded,
10136  // and the computation itself is approximated [...]. In practice, however,
10137  // this simple function is sufficient."
10138  //
10139  // For IEEE double precision floating-point numbers converted into
10140  // normalized diyfp's w = f * 2^e, with q = 64,
10141  //
10142  // e >= -1022 (min IEEE exponent)
10143  // -52 (p - 1)
10144  // -52 (p - 1, possibly normalize denormal IEEE numbers)
10145  // -11 (normalize the diyfp)
10146  // = -1137
10147  //
10148  // and
10149  //
10150  // e <= +1023 (max IEEE exponent)
10151  // -52 (p - 1)
10152  // -11 (normalize the diyfp)
10153  // = 960
10154  //
10155  // This binary exponent range [-1137,960] results in a decimal exponent
10156  // range [-307,324]. One does not need to store a cached power for each
10157  // k in this range. For each such k it suffices to find a cached power
10158  // such that the exponent of the product lies in [alpha,gamma].
10159  // This implies that the difference of the decimal exponents of adjacent
10160  // table entries must be less than or equal to
10161  //
10162  // floor( (gamma - alpha) * log_10(2) ) = 8.
10163  //
10164  // (A smaller distance gamma-alpha would require a larger table.)
10165 
10166  // NB:
10167  // Actually this function returns c, such that -60 <= e_c + e + 64 <= -34.
10168 
10169  constexpr int kCachedPowersSize = 79;
10170  constexpr int kCachedPowersMinDecExp = -300;
10171  constexpr int kCachedPowersDecStep = 8;
10172 
10173  static constexpr cached_power kCachedPowers[] =
10174  {
10175  { 0xAB70FE17C79AC6CA, -1060, -300 },
10176  { 0xFF77B1FCBEBCDC4F, -1034, -292 },
10177  { 0xBE5691EF416BD60C, -1007, -284 },
10178  { 0x8DD01FAD907FFC3C, -980, -276 },
10179  { 0xD3515C2831559A83, -954, -268 },
10180  { 0x9D71AC8FADA6C9B5, -927, -260 },
10181  { 0xEA9C227723EE8BCB, -901, -252 },
10182  { 0xAECC49914078536D, -874, -244 },
10183  { 0x823C12795DB6CE57, -847, -236 },
10184  { 0xC21094364DFB5637, -821, -228 },
10185  { 0x9096EA6F3848984F, -794, -220 },
10186  { 0xD77485CB25823AC7, -768, -212 },
10187  { 0xA086CFCD97BF97F4, -741, -204 },
10188  { 0xEF340A98172AACE5, -715, -196 },
10189  { 0xB23867FB2A35B28E, -688, -188 },
10190  { 0x84C8D4DFD2C63F3B, -661, -180 },
10191  { 0xC5DD44271AD3CDBA, -635, -172 },
10192  { 0x936B9FCEBB25C996, -608, -164 },
10193  { 0xDBAC6C247D62A584, -582, -156 },
10194  { 0xA3AB66580D5FDAF6, -555, -148 },
10195  { 0xF3E2F893DEC3F126, -529, -140 },
10196  { 0xB5B5ADA8AAFF80B8, -502, -132 },
10197  { 0x87625F056C7C4A8B, -475, -124 },
10198  { 0xC9BCFF6034C13053, -449, -116 },
10199  { 0x964E858C91BA2655, -422, -108 },
10200  { 0xDFF9772470297EBD, -396, -100 },
10201  { 0xA6DFBD9FB8E5B88F, -369, -92 },
10202  { 0xF8A95FCF88747D94, -343, -84 },
10203  { 0xB94470938FA89BCF, -316, -76 },
10204  { 0x8A08F0F8BF0F156B, -289, -68 },
10205  { 0xCDB02555653131B6, -263, -60 },
10206  { 0x993FE2C6D07B7FAC, -236, -52 },
10207  { 0xE45C10C42A2B3B06, -210, -44 },
10208  { 0xAA242499697392D3, -183, -36 },
10209  { 0xFD87B5F28300CA0E, -157, -28 },
10210  { 0xBCE5086492111AEB, -130, -20 },
10211  { 0x8CBCCC096F5088CC, -103, -12 },
10212  { 0xD1B71758E219652C, -77, -4 },
10213  { 0x9C40000000000000, -50, 4 },
10214  { 0xE8D4A51000000000, -24, 12 },
10215  { 0xAD78EBC5AC620000, 3, 20 },
10216  { 0x813F3978F8940984, 30, 28 },
10217  { 0xC097CE7BC90715B3, 56, 36 },
10218  { 0x8F7E32CE7BEA5C70, 83, 44 },
10219  { 0xD5D238A4ABE98068, 109, 52 },
10220  { 0x9F4F2726179A2245, 136, 60 },
10221  { 0xED63A231D4C4FB27, 162, 68 },
10222  { 0xB0DE65388CC8ADA8, 189, 76 },
10223  { 0x83C7088E1AAB65DB, 216, 84 },
10224  { 0xC45D1DF942711D9A, 242, 92 },
10225  { 0x924D692CA61BE758, 269, 100 },
10226  { 0xDA01EE641A708DEA, 295, 108 },
10227  { 0xA26DA3999AEF774A, 322, 116 },
10228  { 0xF209787BB47D6B85, 348, 124 },
10229  { 0xB454E4A179DD1877, 375, 132 },
10230  { 0x865B86925B9BC5C2, 402, 140 },
10231  { 0xC83553C5C8965D3D, 428, 148 },
10232  { 0x952AB45CFA97A0B3, 455, 156 },
10233  { 0xDE469FBD99A05FE3, 481, 164 },
10234  { 0xA59BC234DB398C25, 508, 172 },
10235  { 0xF6C69A72A3989F5C, 534, 180 },
10236  { 0xB7DCBF5354E9BECE, 561, 188 },
10237  { 0x88FCF317F22241E2, 588, 196 },
10238  { 0xCC20CE9BD35C78A5, 614, 204 },
10239  { 0x98165AF37B2153DF, 641, 212 },
10240  { 0xE2A0B5DC971F303A, 667, 220 },
10241  { 0xA8D9D1535CE3B396, 694, 228 },
10242  { 0xFB9B7CD9A4A7443C, 720, 236 },
10243  { 0xBB764C4CA7A44410, 747, 244 },
10244  { 0x8BAB8EEFB6409C1A, 774, 252 },
10245  { 0xD01FEF10A657842C, 800, 260 },
10246  { 0x9B10A4E5E9913129, 827, 268 },
10247  { 0xE7109BFBA19C0C9D, 853, 276 },
10248  { 0xAC2820D9623BF429, 880, 284 },
10249  { 0x80444B5E7AA7CF85, 907, 292 },
10250  { 0xBF21E44003ACDD2D, 933, 300 },
10251  { 0x8E679C2F5E44FF8F, 960, 308 },
10252  { 0xD433179D9C8CB841, 986, 316 },
10253  { 0x9E19DB92B4E31BA9, 1013, 324 },
10254  };
10255 
10256  // This computation gives exactly the same results for k as
10257  // k = ceil((kAlpha - e - 1) * 0.30102999566398114)
10258  // for |e| <= 1500, but doesn't require floating-point operations.
10259  // NB: log_10(2) ~= 78913 / 2^18
10260  assert(e >= -1500);
10261  assert(e <= 1500);
10262  const int f = kAlpha - e - 1;
10263  const int k = (f * 78913) / (1 << 18) + static_cast<int>(f > 0);
10264 
10265  const int index = (-kCachedPowersMinDecExp + k + (kCachedPowersDecStep - 1)) / kCachedPowersDecStep;
10266  assert(index >= 0);
10267  assert(index < kCachedPowersSize);
10268  static_cast<void>(kCachedPowersSize); // Fix warning.
10269 
10270  const cached_power cached = kCachedPowers[index];
10271  assert(kAlpha <= cached.e + e + 64);
10272  assert(kGamma >= cached.e + e + 64);
10273 
10274  return cached;
10275 }
10276 
10277 /*!
10278 For n != 0, returns k, such that pow10 := 10^(k-1) <= n < 10^k.
10279 For n == 0, returns 1 and sets pow10 := 1.
10280 */
10281 inline int find_largest_pow10(const uint32_t n, uint32_t& pow10)
10282 {
10283  // LCOV_EXCL_START
10284  if (n >= 1000000000)
10285  {
10286  pow10 = 1000000000;
10287  return 10;
10288  }
10289  // LCOV_EXCL_STOP
10290  else if (n >= 100000000)
10291  {
10292  pow10 = 100000000;
10293  return 9;
10294  }
10295  else if (n >= 10000000)
10296  {
10297  pow10 = 10000000;
10298  return 8;
10299  }
10300  else if (n >= 1000000)
10301  {
10302  pow10 = 1000000;
10303  return 7;
10304  }
10305  else if (n >= 100000)
10306  {
10307  pow10 = 100000;
10308  return 6;
10309  }
10310  else if (n >= 10000)
10311  {
10312  pow10 = 10000;
10313  return 5;
10314  }
10315  else if (n >= 1000)
10316  {
10317  pow10 = 1000;
10318  return 4;
10319  }
10320  else if (n >= 100)
10321  {
10322  pow10 = 100;
10323  return 3;
10324  }
10325  else if (n >= 10)
10326  {
10327  pow10 = 10;
10328  return 2;
10329  }
10330  else
10331  {
10332  pow10 = 1;
10333  return 1;
10334  }
10335 }
10336 
10337 inline void grisu2_round(char* buf, int len, uint64_t dist, uint64_t delta,
10338  uint64_t rest, uint64_t ten_k)
10339 {
10340  assert(len >= 1);
10341  assert(dist <= delta);
10342  assert(rest <= delta);
10343  assert(ten_k > 0);
10344 
10345  // <--------------------------- delta ---->
10346  // <---- dist --------->
10347  // --------------[------------------+-------------------]--------------
10348  // M- w M+
10349  //
10350  // ten_k
10351  // <------>
10352  // <---- rest ---->
10353  // --------------[------------------+----+--------------]--------------
10354  // w V
10355  // = buf * 10^k
10356  //
10357  // ten_k represents a unit-in-the-last-place in the decimal representation
10358  // stored in buf.
10359  // Decrement buf by ten_k while this takes buf closer to w.
10360 
10361  // The tests are written in this order to avoid overflow in unsigned
10362  // integer arithmetic.
10363 
10364  while (rest < dist
10365  and delta - rest >= ten_k
10366  and (rest + ten_k < dist or dist - rest > rest + ten_k - dist))
10367  {
10368  assert(buf[len - 1] != '0');
10369  buf[len - 1]--;
10370  rest += ten_k;
10371  }
10372 }
10373 
10374 /*!
10375 Generates V = buffer * 10^decimal_exponent, such that M- <= V <= M+.
10376 M- and M+ must be normalized and share the same exponent -60 <= e <= -32.
10377 */
10378 inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent,
10379  diyfp M_minus, diyfp w, diyfp M_plus)
10380 {
10381  static_assert(kAlpha >= -60, "internal error");
10382  static_assert(kGamma <= -32, "internal error");
10383 
10384  // Generates the digits (and the exponent) of a decimal floating-point
10385  // number V = buffer * 10^decimal_exponent in the range [M-, M+]. The diyfp's
10386  // w, M- and M+ share the same exponent e, which satisfies alpha <= e <= gamma.
10387  //
10388  // <--------------------------- delta ---->
10389  // <---- dist --------->
10390  // --------------[------------------+-------------------]--------------
10391  // M- w M+
10392  //
10393  // Grisu2 generates the digits of M+ from left to right and stops as soon as
10394  // V is in [M-,M+].
10395 
10396  assert(M_plus.e >= kAlpha);
10397  assert(M_plus.e <= kGamma);
10398 
10399  uint64_t delta = diyfp::sub(M_plus, M_minus).f; // (significand of (M+ - M-), implicit exponent is e)
10400  uint64_t dist = diyfp::sub(M_plus, w ).f; // (significand of (M+ - w ), implicit exponent is e)
10401 
10402  // Split M+ = f * 2^e into two parts p1 and p2 (note: e < 0):
10403  //
10404  // M+ = f * 2^e
10405  // = ((f div 2^-e) * 2^-e + (f mod 2^-e)) * 2^e
10406  // = ((p1 ) * 2^-e + (p2 )) * 2^e
10407  // = p1 + p2 * 2^e
10408 
10409  const diyfp one(uint64_t{1} << -M_plus.e, M_plus.e);
10410 
10411  auto p1 = static_cast<uint32_t>(M_plus.f >> -one.e); // p1 = f div 2^-e (Since -e >= 32, p1 fits into a 32-bit int.)
10412  uint64_t p2 = M_plus.f & (one.f - 1); // p2 = f mod 2^-e
10413 
10414  // 1)
10415  //
10416  // Generate the digits of the integral part p1 = d[n-1]...d[1]d[0]
10417 
10418  assert(p1 > 0);
10419 
10420  uint32_t pow10;
10421  const int k = find_largest_pow10(p1, pow10);
10422 
10423  // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1)
10424  //
10425  // p1 = (p1 div 10^(k-1)) * 10^(k-1) + (p1 mod 10^(k-1))
10426  // = (d[k-1] ) * 10^(k-1) + (p1 mod 10^(k-1))
10427  //
10428  // M+ = p1 + p2 * 2^e
10429  // = d[k-1] * 10^(k-1) + (p1 mod 10^(k-1)) + p2 * 2^e
10430  // = d[k-1] * 10^(k-1) + ((p1 mod 10^(k-1)) * 2^-e + p2) * 2^e
10431  // = d[k-1] * 10^(k-1) + ( rest) * 2^e
10432  //
10433  // Now generate the digits d[n] of p1 from left to right (n = k-1,...,0)
10434  //
10435  // p1 = d[k-1]...d[n] * 10^n + d[n-1]...d[0]
10436  //
10437  // but stop as soon as
10438  //
10439  // rest * 2^e = (d[n-1]...d[0] * 2^-e + p2) * 2^e <= delta * 2^e
10440 
10441  int n = k;
10442  while (n > 0)
10443  {
10444  // Invariants:
10445  // M+ = buffer * 10^n + (p1 + p2 * 2^e) (buffer = 0 for n = k)
10446  // pow10 = 10^(n-1) <= p1 < 10^n
10447  //
10448  const uint32_t d = p1 / pow10; // d = p1 div 10^(n-1)
10449  const uint32_t r = p1 % pow10; // r = p1 mod 10^(n-1)
10450  //
10451  // M+ = buffer * 10^n + (d * 10^(n-1) + r) + p2 * 2^e
10452  // = (buffer * 10 + d) * 10^(n-1) + (r + p2 * 2^e)
10453  //
10454  assert(d <= 9);
10455  buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
10456  //
10457  // M+ = buffer * 10^(n-1) + (r + p2 * 2^e)
10458  //
10459  p1 = r;
10460  n--;
10461  //
10462  // M+ = buffer * 10^n + (p1 + p2 * 2^e)
10463  // pow10 = 10^n
10464  //
10465 
10466  // Now check if enough digits have been generated.
10467  // Compute
10468  //
10469  // p1 + p2 * 2^e = (p1 * 2^-e + p2) * 2^e = rest * 2^e
10470  //
10471  // Note:
10472  // Since rest and delta share the same exponent e, it suffices to
10473  // compare the significands.
10474  const uint64_t rest = (uint64_t{p1} << -one.e) + p2;
10475  if (rest <= delta)
10476  {
10477  // V = buffer * 10^n, with M- <= V <= M+.
10478 
10479  decimal_exponent += n;
10480 
10481  // We may now just stop. But instead look if the buffer could be
10482  // decremented to bring V closer to w.
10483  //
10484  // pow10 = 10^n is now 1 ulp in the decimal representation V.
10485  // The rounding procedure works with diyfp's with an implicit
10486  // exponent of e.
10487  //
10488  // 10^n = (10^n * 2^-e) * 2^e = ulp * 2^e
10489  //
10490  const uint64_t ten_n = uint64_t{pow10} << -one.e;
10491  grisu2_round(buffer, length, dist, delta, rest, ten_n);
10492 
10493  return;
10494  }
10495 
10496  pow10 /= 10;
10497  //
10498  // pow10 = 10^(n-1) <= p1 < 10^n
10499  // Invariants restored.
10500  }
10501 
10502  // 2)
10503  //
10504  // The digits of the integral part have been generated:
10505  //
10506  // M+ = d[k-1]...d[1]d[0] + p2 * 2^e
10507  // = buffer + p2 * 2^e
10508  //
10509  // Now generate the digits of the fractional part p2 * 2^e.
10510  //
10511  // Note:
10512  // No decimal point is generated: the exponent is adjusted instead.
10513  //
10514  // p2 actually represents the fraction
10515  //
10516  // p2 * 2^e
10517  // = p2 / 2^-e
10518  // = d[-1] / 10^1 + d[-2] / 10^2 + ...
10519  //
10520  // Now generate the digits d[-m] of p1 from left to right (m = 1,2,...)
10521  //
10522  // p2 * 2^e = d[-1]d[-2]...d[-m] * 10^-m
10523  // + 10^-m * (d[-m-1] / 10^1 + d[-m-2] / 10^2 + ...)
10524  //
10525  // using
10526  //
10527  // 10^m * p2 = ((10^m * p2) div 2^-e) * 2^-e + ((10^m * p2) mod 2^-e)
10528  // = ( d) * 2^-e + ( r)
10529  //
10530  // or
10531  // 10^m * p2 * 2^e = d + r * 2^e
10532  //
10533  // i.e.
10534  //
10535  // M+ = buffer + p2 * 2^e
10536  // = buffer + 10^-m * (d + r * 2^e)
10537  // = (buffer * 10^m + d) * 10^-m + 10^-m * r * 2^e
10538  //
10539  // and stop as soon as 10^-m * r * 2^e <= delta * 2^e
10540 
10541  assert(p2 > delta);
10542 
10543  int m = 0;
10544  for (;;)
10545  {
10546  // Invariant:
10547  // M+ = buffer * 10^-m + 10^-m * (d[-m-1] / 10 + d[-m-2] / 10^2 + ...) * 2^e
10548  // = buffer * 10^-m + 10^-m * (p2 ) * 2^e
10549  // = buffer * 10^-m + 10^-m * (1/10 * (10 * p2) ) * 2^e
10550  // = buffer * 10^-m + 10^-m * (1/10 * ((10*p2 div 2^-e) * 2^-e + (10*p2 mod 2^-e)) * 2^e
10551  //
10552  assert(p2 <= UINT64_MAX / 10);
10553  p2 *= 10;
10554  const uint64_t d = p2 >> -one.e; // d = (10 * p2) div 2^-e
10555  const uint64_t r = p2 & (one.f - 1); // r = (10 * p2) mod 2^-e
10556  //
10557  // M+ = buffer * 10^-m + 10^-m * (1/10 * (d * 2^-e + r) * 2^e
10558  // = buffer * 10^-m + 10^-m * (1/10 * (d + r * 2^e))
10559  // = (buffer * 10 + d) * 10^(-m-1) + 10^(-m-1) * r * 2^e
10560  //
10561  assert(d <= 9);
10562  buffer[length++] = static_cast<char>('0' + d); // buffer := buffer * 10 + d
10563  //
10564  // M+ = buffer * 10^(-m-1) + 10^(-m-1) * r * 2^e
10565  //
10566  p2 = r;
10567  m++;
10568  //
10569  // M+ = buffer * 10^-m + 10^-m * p2 * 2^e
10570  // Invariant restored.
10571 
10572  // Check if enough digits have been generated.
10573  //
10574  // 10^-m * p2 * 2^e <= delta * 2^e
10575  // p2 * 2^e <= 10^m * delta * 2^e
10576  // p2 <= 10^m * delta
10577  delta *= 10;
10578  dist *= 10;
10579  if (p2 <= delta)
10580  {
10581  break;
10582  }
10583  }
10584 
10585  // V = buffer * 10^-m, with M- <= V <= M+.
10586 
10587  decimal_exponent -= m;
10588 
10589  // 1 ulp in the decimal representation is now 10^-m.
10590  // Since delta and dist are now scaled by 10^m, we need to do the
10591  // same with ulp in order to keep the units in sync.
10592  //
10593  // 10^m * 10^-m = 1 = 2^-e * 2^e = ten_m * 2^e
10594  //
10595  const uint64_t ten_m = one.f;
10596  grisu2_round(buffer, length, dist, delta, p2, ten_m);
10597 
10598  // By construction this algorithm generates the shortest possible decimal
10599  // number (Loitsch, Theorem 6.2) which rounds back to w.
10600  // For an input number of precision p, at least
10601  //
10602  // N = 1 + ceil(p * log_10(2))
10603  //
10604  // decimal digits are sufficient to identify all binary floating-point
10605  // numbers (Matula, "In-and-Out conversions").
10606  // This implies that the algorithm does not produce more than N decimal
10607  // digits.
10608  //
10609  // N = 17 for p = 53 (IEEE double precision)
10610  // N = 9 for p = 24 (IEEE single precision)
10611 }
10612 
10613 /*!
10614 v = buf * 10^decimal_exponent
10615 len is the length of the buffer (number of decimal digits)
10616 The buffer must be large enough, i.e. >= max_digits10.
10617 */
10618 inline void grisu2(char* buf, int& len, int& decimal_exponent,
10619  diyfp m_minus, diyfp v, diyfp m_plus)
10620 {
10621  assert(m_plus.e == m_minus.e);
10622  assert(m_plus.e == v.e);
10623 
10624  // --------(-----------------------+-----------------------)-------- (A)
10625  // m- v m+
10626  //
10627  // --------------------(-----------+-----------------------)-------- (B)
10628  // m- v m+
10629  //
10630  // First scale v (and m- and m+) such that the exponent is in the range
10631  // [alpha, gamma].
10632 
10633  const cached_power cached = get_cached_power_for_binary_exponent(m_plus.e);
10634 
10635  const diyfp c_minus_k(cached.f, cached.e); // = c ~= 10^-k
10636 
10637  // The exponent of the products is = v.e + c_minus_k.e + q and is in the range [alpha,gamma]
10638  const diyfp w = diyfp::mul(v, c_minus_k);
10639  const diyfp w_minus = diyfp::mul(m_minus, c_minus_k);
10640  const diyfp w_plus = diyfp::mul(m_plus, c_minus_k);
10641 
10642  // ----(---+---)---------------(---+---)---------------(---+---)----
10643  // w- w w+
10644  // = c*m- = c*v = c*m+
10645  //
10646  // diyfp::mul rounds its result and c_minus_k is approximated too. w, w- and
10647  // w+ are now off by a small amount.
10648  // In fact:
10649  //
10650  // w - v * 10^k < 1 ulp
10651  //
10652  // To account for this inaccuracy, add resp. subtract 1 ulp.
10653  //
10654  // --------+---[---------------(---+---)---------------]---+--------
10655  // w- M- w M+ w+
10656  //
10657  // Now any number in [M-, M+] (bounds included) will round to w when input,
10658  // regardless of how the input rounding algorithm breaks ties.
10659  //
10660  // And digit_gen generates the shortest possible such number in [M-, M+].
10661  // Note that this does not mean that Grisu2 always generates the shortest
10662  // possible number in the interval (m-, m+).
10663  const diyfp M_minus(w_minus.f + 1, w_minus.e);
10664  const diyfp M_plus (w_plus.f - 1, w_plus.e );
10665 
10666  decimal_exponent = -cached.k; // = -(-k) = k
10667 
10668  grisu2_digit_gen(buf, len, decimal_exponent, M_minus, w, M_plus);
10669 }
10670 
10671 /*!
10672 v = buf * 10^decimal_exponent
10673 len is the length of the buffer (number of decimal digits)
10674 The buffer must be large enough, i.e. >= max_digits10.
10675 */
10676 template <typename FloatType>
10677 void grisu2(char* buf, int& len, int& decimal_exponent, FloatType value)
10678 {
10679  static_assert(diyfp::kPrecision >= std::numeric_limits<FloatType>::digits + 3,
10680  "internal error: not enough precision");
10681 
10682  assert(std::isfinite(value));
10683  assert(value > 0);
10684 
10685  // If the neighbors (and boundaries) of 'value' are always computed for double-precision
10686  // numbers, all float's can be recovered using strtod (and strtof). However, the resulting
10687  // decimal representations are not exactly "short".
10688  //
10689  // The documentation for 'std::to_chars' (https://en.cppreference.com/w/cpp/utility/to_chars)
10690  // says "value is converted to a string as if by std::sprintf in the default ("C") locale"
10691  // and since sprintf promotes float's to double's, I think this is exactly what 'std::to_chars'
10692  // does.
10693  // On the other hand, the documentation for 'std::to_chars' requires that "parsing the
10694  // representation using the corresponding std::from_chars function recovers value exactly". That
10695  // indicates that single precision floating-point numbers should be recovered using
10696  // 'std::strtof'.
10697  //
10698  // NB: If the neighbors are computed for single-precision numbers, there is a single float
10699  // (7.0385307e-26f) which can't be recovered using strtod. The resulting double precision
10700  // value is off by 1 ulp.
10701 #if 0
10702  const boundaries w = compute_boundaries(static_cast<double>(value));
10703 #else
10704  const boundaries w = compute_boundaries(value);
10705 #endif
10706 
10707  grisu2(buf, len, decimal_exponent, w.minus, w.w, w.plus);
10708 }
10709 
10710 /*!
10711 @brief appends a decimal representation of e to buf
10712 @return a pointer to the element following the exponent.
10713 @pre -1000 < e < 1000
10714 */
10715 inline char* append_exponent(char* buf, int e)
10716 {
10717  assert(e > -1000);
10718  assert(e < 1000);
10719 
10720  if (e < 0)
10721  {
10722  e = -e;
10723  *buf++ = '-';
10724  }
10725  else
10726  {
10727  *buf++ = '+';
10728  }
10729 
10730  auto k = static_cast<uint32_t>(e);
10731  if (k < 10)
10732  {
10733  // Always print at least two digits in the exponent.
10734  // This is for compatibility with printf("%g").
10735  *buf++ = '0';
10736  *buf++ = static_cast<char>('0' + k);
10737  }
10738  else if (k < 100)
10739  {
10740  *buf++ = static_cast<char>('0' + k / 10);
10741  k %= 10;
10742  *buf++ = static_cast<char>('0' + k);
10743  }
10744  else
10745  {
10746  *buf++ = static_cast<char>('0' + k / 100);
10747  k %= 100;
10748  *buf++ = static_cast<char>('0' + k / 10);
10749  k %= 10;
10750  *buf++ = static_cast<char>('0' + k);
10751  }
10752 
10753  return buf;
10754 }
10755 
10756 /*!
10757 @brief prettify v = buf * 10^decimal_exponent
10758 
10759 If v is in the range [10^min_exp, 10^max_exp) it will be printed in fixed-point
10760 notation. Otherwise it will be printed in exponential notation.
10761 
10762 @pre min_exp < 0
10763 @pre max_exp > 0
10764 */
10765 inline char* format_buffer(char* buf, int len, int decimal_exponent,
10766  int min_exp, int max_exp)
10767 {
10768  assert(min_exp < 0);
10769  assert(max_exp > 0);
10770 
10771  const int k = len;
10772  const int n = len + decimal_exponent;
10773 
10774  // v = buf * 10^(n-k)
10775  // k is the length of the buffer (number of decimal digits)
10776  // n is the position of the decimal point relative to the start of the buffer.
10777 
10778  if (k <= n and n <= max_exp)
10779  {
10780  // digits[000]
10781  // len <= max_exp + 2
10782 
10783  std::memset(buf + k, '0', static_cast<size_t>(n - k));
10784  // Make it look like a floating-point number (#362, #378)
10785  buf[n + 0] = '.';
10786  buf[n + 1] = '0';
10787  return buf + (n + 2);
10788  }
10789 
10790  if (0 < n and n <= max_exp)
10791  {
10792  // dig.its
10793  // len <= max_digits10 + 1
10794 
10795  assert(k > n);
10796 
10797  std::memmove(buf + (n + 1), buf + n, static_cast<size_t>(k - n));
10798  buf[n] = '.';
10799  return buf + (k + 1);
10800  }
10801 
10802  if (min_exp < n and n <= 0)
10803  {
10804  // 0.[000]digits
10805  // len <= 2 + (-min_exp - 1) + max_digits10
10806 
10807  std::memmove(buf + (2 + -n), buf, static_cast<size_t>(k));
10808  buf[0] = '0';
10809  buf[1] = '.';
10810  std::memset(buf + 2, '0', static_cast<size_t>(-n));
10811  return buf + (2 + (-n) + k);
10812  }
10813 
10814  if (k == 1)
10815  {
10816  // dE+123
10817  // len <= 1 + 5
10818 
10819  buf += 1;
10820  }
10821  else
10822  {
10823  // d.igitsE+123
10824  // len <= max_digits10 + 1 + 5
10825 
10826  std::memmove(buf + 2, buf + 1, static_cast<size_t>(k - 1));
10827  buf[1] = '.';
10828  buf += 1 + k;
10829  }
10830 
10831  *buf++ = 'e';
10832  return append_exponent(buf, n - 1);
10833 }
10834 
10835 } // namespace dtoa_impl
10836 
10837 /*!
10838 @brief generates a decimal representation of the floating-point number value in [first, last).
10839 
10840 The format of the resulting decimal representation is similar to printf's %g
10841 format. Returns an iterator pointing past-the-end of the decimal representation.
10842 
10843 @note The input number must be finite, i.e. NaN's and Inf's are not supported.
10844 @note The buffer must be large enough.
10845 @note The result is NOT null-terminated.
10846 */
10847 template <typename FloatType>
10848 char* to_chars(char* first, const char* last, FloatType value)
10849 {
10850  static_cast<void>(last); // maybe unused - fix warning
10851  assert(std::isfinite(value));
10852 
10853  // Use signbit(value) instead of (value < 0) since signbit works for -0.
10854  if (std::signbit(value))
10855  {
10856  value = -value;
10857  *first++ = '-';
10858  }
10859 
10860  if (value == 0) // +-0
10861  {
10862  *first++ = '0';
10863  // Make it look like a floating-point number (#362, #378)
10864  *first++ = '.';
10865  *first++ = '0';
10866  return first;
10867  }
10868 
10869  assert(last - first >= std::numeric_limits<FloatType>::max_digits10);
10870 
10871  // Compute v = buffer * 10^decimal_exponent.
10872  // The decimal digits are stored in the buffer, which needs to be interpreted
10873  // as an unsigned decimal integer.
10874  // len is the length of the buffer, i.e. the number of decimal digits.
10875  int len = 0;
10876  int decimal_exponent = 0;
10877  dtoa_impl::grisu2(first, len, decimal_exponent, value);
10878 
10879  assert(len <= std::numeric_limits<FloatType>::max_digits10);
10880 
10881  // Format the buffer like printf("%.*g", prec, value)
10882  constexpr int kMinExp = -4;
10883  // Use digits10 here to increase compatibility with version 2.
10884  constexpr int kMaxExp = std::numeric_limits<FloatType>::digits10;
10885 
10886  assert(last - first >= kMaxExp + 2);
10887  assert(last - first >= 2 + (-kMinExp - 1) + std::numeric_limits<FloatType>::max_digits10);
10888  assert(last - first >= std::numeric_limits<FloatType>::max_digits10 + 6);
10889 
10890  return dtoa_impl::format_buffer(first, len, decimal_exponent, kMinExp, kMaxExp);
10891 }
10892 
10893 } // namespace detail
10894 } // namespace nlohmann
10895 
10896 // #include <nlohmann/detail/macro_scope.hpp>
10897 
10898 // #include <nlohmann/detail/meta/cpp_future.hpp>
10899 
10900 // #include <nlohmann/detail/output/binary_writer.hpp>
10901 
10902 // #include <nlohmann/detail/output/output_adapters.hpp>
10903 
10904 // #include <nlohmann/detail/value_t.hpp>
10905 
10906 
10907 namespace nlohmann
10908 {
10909 namespace detail
10910 {
10911 ///////////////////
10912 // serialization //
10913 ///////////////////
10914 
10915 /// how to treat decoding errors
10916 enum class error_handler_t
10917 {
10918  strict, ///< throw a type_error exception in case of invalid UTF-8
10919  replace, ///< replace invalid UTF-8 sequences with U+FFFD
10920  ignore ///< ignore invalid UTF-8 sequences
10921 };
10922 
10923 template<typename BasicJsonType>
10924 class serializer
10925 {
10926  using string_t = typename BasicJsonType::string_t;
10927  using number_float_t = typename BasicJsonType::number_float_t;
10928  using number_integer_t = typename BasicJsonType::number_integer_t;
10929  using number_unsigned_t = typename BasicJsonType::number_unsigned_t;
10930  static constexpr uint8_t UTF8_ACCEPT = 0;
10931  static constexpr uint8_t UTF8_REJECT = 1;
10932 
10933  public:
10934  /*!
10935  @param[in] s output stream to serialize to
10936  @param[in] ichar indentation character to use
10937  @param[in] error_handler_ how to react on decoding errors
10938  */
10939  serializer(output_adapter_t<char> s, const char ichar,
10940  error_handler_t error_handler_ = error_handler_t::strict)
10941  : o(std::move(s))
10942  , loc(std::localeconv())
10943  , thousands_sep(loc->thousands_sep == nullptr ? '\0' : * (loc->thousands_sep))
10944  , decimal_point(loc->decimal_point == nullptr ? '\0' : * (loc->decimal_point))
10945  , indent_char(ichar)
10946  , indent_string(512, indent_char)
10947  , error_handler(error_handler_)
10948  {}
10949 
10950  // delete because of pointer members
10951  serializer(const serializer&) = delete;
10952  serializer& operator=(const serializer&) = delete;
10953  serializer(serializer&&) = delete;
10954  serializer& operator=(serializer&&) = delete;
10955  ~serializer() = default;
10956 
10957  /*!
10958  @brief internal implementation of the serialization function
10959 
10960  This function is called by the public member function dump and organizes
10961  the serialization internally. The indentation level is propagated as
10962  additional parameter. In case of arrays and objects, the function is
10963  called recursively.
10964 
10965  - strings and object keys are escaped using `escape_string()`
10966  - integer numbers are converted implicitly via `operator<<`
10967  - floating-point numbers are converted to a string using `"%g"` format
10968 
10969  @param[in] val value to serialize
10970  @param[in] pretty_print whether the output shall be pretty-printed
10971  @param[in] indent_step the indent level
10972  @param[in] current_indent the current indent level (only used internally)
10973  */
10974  void dump(const BasicJsonType& val, const bool pretty_print,
10975  const bool ensure_ascii,
10976  const unsigned int indent_step,
10977  const unsigned int current_indent = 0)
10978  {
10979  switch (val.m_type)
10980  {
10981  case value_t::object:
10982  {
10983  if (val.m_value.object->empty())
10984  {
10985  o->write_characters("{}", 2);
10986  return;
10987  }
10988 
10989  if (pretty_print)
10990  {
10991  o->write_characters("{\n", 2);
10992 
10993  // variable to hold indentation for recursive calls
10994  const auto new_indent = current_indent + indent_step;
10995  if (JSON_UNLIKELY(indent_string.size() < new_indent))
10996  {
10997  indent_string.resize(indent_string.size() * 2, ' ');
10998  }
10999 
11000  // first n-1 elements
11001  auto i = val.m_value.object->cbegin();
11002  for (std::size_t cnt = 0; cnt < val.m_value.object->size() - 1; ++cnt, ++i)
11003  {
11004  o->write_characters(indent_string.c_str(), new_indent);
11005  o->write_character('\"');
11006  dump_escaped(i->first, ensure_ascii);
11007  o->write_characters("\": ", 3);
11008  dump(i->second, true, ensure_ascii, indent_step, new_indent);
11009  o->write_characters(",\n", 2);
11010  }
11011 
11012  // last element
11013  assert(i != val.m_value.object->cend());
11014  assert(std::next(i) == val.m_value.object->cend());
11015  o->write_characters(indent_string.c_str(), new_indent);
11016  o->write_character('\"');
11017  dump_escaped(i->first, ensure_ascii);
11018  o->write_characters("\": ", 3);
11019  dump(i->second, true, ensure_ascii, indent_step, new_indent);
11020 
11021  o->write_character('\n');
11022  o->write_characters(indent_string.c_str(), current_indent);
11023  o->write_character('}');
11024  }
11025  else
11026  {
11027  o->write_character('{');
11028 
11029  // first n-1 elements
11030  auto i = val.m_value.object->cbegin();
11031  for (std::size_t cnt = 0; cnt < val.m_value.object->size() - 1; ++cnt, ++i)
11032  {
11033  o->write_character('\"');
11034  dump_escaped(i->first, ensure_ascii);
11035  o->write_characters("\":", 2);
11036  dump(i->second, false, ensure_ascii, indent_step, current_indent);
11037  o->write_character(',');
11038  }
11039 
11040  // last element
11041  assert(i != val.m_value.object->cend());
11042  assert(std::next(i) == val.m_value.object->cend());
11043  o->write_character('\"');
11044  dump_escaped(i->first, ensure_ascii);
11045  o->write_characters("\":", 2);
11046  dump(i->second, false, ensure_ascii, indent_step, current_indent);
11047 
11048  o->write_character('}');
11049  }
11050 
11051  return;
11052  }
11053 
11054  case value_t::array:
11055  {
11056  if (val.m_value.array->empty())
11057  {
11058  o->write_characters("[]", 2);
11059  return;
11060  }
11061 
11062  if (pretty_print)
11063  {
11064  o->write_characters("[\n", 2);
11065 
11066  // variable to hold indentation for recursive calls
11067  const auto new_indent = current_indent + indent_step;
11068  if (JSON_UNLIKELY(indent_string.size() < new_indent))
11069  {
11070  indent_string.resize(indent_string.size() * 2, ' ');
11071  }
11072 
11073  // first n-1 elements
11074  for (auto i = val.m_value.array->cbegin();
11075  i != val.m_value.array->cend() - 1; ++i)
11076  {
11077  o->write_characters(indent_string.c_str(), new_indent);
11078  dump(*i, true, ensure_ascii, indent_step, new_indent);
11079  o->write_characters(",\n", 2);
11080  }
11081 
11082  // last element
11083  assert(not val.m_value.array->empty());
11084  o->write_characters(indent_string.c_str(), new_indent);
11085  dump(val.m_value.array->back(), true, ensure_ascii, indent_step, new_indent);
11086 
11087  o->write_character('\n');
11088  o->write_characters(indent_string.c_str(), current_indent);
11089  o->write_character(']');
11090  }
11091  else
11092  {
11093  o->write_character('[');
11094 
11095  // first n-1 elements
11096  for (auto i = val.m_value.array->cbegin();
11097  i != val.m_value.array->cend() - 1; ++i)
11098  {
11099  dump(*i, false, ensure_ascii, indent_step, current_indent);
11100  o->write_character(',');
11101  }
11102 
11103  // last element
11104  assert(not val.m_value.array->empty());
11105  dump(val.m_value.array->back(), false, ensure_ascii, indent_step, current_indent);
11106 
11107  o->write_character(']');
11108  }
11109 
11110  return;
11111  }
11112 
11113  case value_t::string:
11114  {
11115  o->write_character('\"');
11116  dump_escaped(*val.m_value.string, ensure_ascii);
11117  o->write_character('\"');
11118  return;
11119  }
11120 
11121  case value_t::boolean:
11122  {
11123  if (val.m_value.boolean)
11124  {
11125  o->write_characters("true", 4);
11126  }
11127  else
11128  {
11129  o->write_characters("false", 5);
11130  }
11131  return;
11132  }
11133 
11134  case value_t::number_integer:
11135  {
11136  dump_integer(val.m_value.number_integer);
11137  return;
11138  }
11139 
11140  case value_t::number_unsigned:
11141  {
11142  dump_integer(val.m_value.number_unsigned);
11143  return;
11144  }
11145 
11146  case value_t::number_float:
11147  {
11148  dump_float(val.m_value.number_float);
11149  return;
11150  }
11151 
11152  case value_t::discarded:
11153  {
11154  o->write_characters("<discarded>", 11);
11155  return;
11156  }
11157 
11158  case value_t::null:
11159  {
11160  o->write_characters("null", 4);
11161  return;
11162  }
11163  }
11164  }
11165 
11166  private:
11167  /*!
11168  @brief dump escaped string
11169 
11170  Escape a string by replacing certain special characters by a sequence of an
11171  escape character (backslash) and another character and other control
11172  characters by a sequence of "\u" followed by a four-digit hex
11173  representation. The escaped string is written to output stream @a o.
11174 
11175  @param[in] s the string to escape
11176  @param[in] ensure_ascii whether to escape non-ASCII characters with
11177  \uXXXX sequences
11178 
11179  @complexity Linear in the length of string @a s.
11180  */
11181  void dump_escaped(const string_t& s, const bool ensure_ascii)
11182  {
11183  uint32_t codepoint;
11184  uint8_t state = UTF8_ACCEPT;
11185  std::size_t bytes = 0; // number of bytes written to string_buffer
11186 
11187  // number of bytes written at the point of the last valid byte
11188  std::size_t bytes_after_last_accept = 0;
11189  std::size_t undumped_chars = 0;
11190 
11191  for (std::size_t i = 0; i < s.size(); ++i)
11192  {
11193  const auto byte = static_cast<uint8_t>(s[i]);
11194 
11195  switch (decode(state, codepoint, byte))
11196  {
11197  case UTF8_ACCEPT: // decode found a new code point
11198  {
11199  switch (codepoint)
11200  {
11201  case 0x08: // backspace
11202  {
11203  string_buffer[bytes++] = '\\';
11204  string_buffer[bytes++] = 'b';
11205  break;
11206  }
11207 
11208  case 0x09: // horizontal tab
11209  {
11210  string_buffer[bytes++] = '\\';
11211  string_buffer[bytes++] = 't';
11212  break;
11213  }
11214 
11215  case 0x0A: // newline
11216  {
11217  string_buffer[bytes++] = '\\';
11218  string_buffer[bytes++] = 'n';
11219  break;
11220  }
11221 
11222  case 0x0C: // formfeed
11223  {
11224  string_buffer[bytes++] = '\\';
11225  string_buffer[bytes++] = 'f';
11226  break;
11227  }
11228 
11229  case 0x0D: // carriage return
11230  {
11231  string_buffer[bytes++] = '\\';
11232  string_buffer[bytes++] = 'r';
11233  break;
11234  }
11235 
11236  case 0x22: // quotation mark
11237  {
11238  string_buffer[bytes++] = '\\';
11239  string_buffer[bytes++] = '\"';
11240  break;
11241  }
11242 
11243  case 0x5C: // reverse solidus
11244  {
11245  string_buffer[bytes++] = '\\';
11246  string_buffer[bytes++] = '\\';
11247  break;
11248  }
11249 
11250  default:
11251  {
11252  // escape control characters (0x00..0x1F) or, if
11253  // ensure_ascii parameter is used, non-ASCII characters
11254  if ((codepoint <= 0x1F) or (ensure_ascii and (codepoint >= 0x7F)))
11255  {
11256  if (codepoint <= 0xFFFF)
11257  {
11258  (std::snprintf)(string_buffer.data() + bytes, 7, "\\u%04x",
11259  static_cast<uint16_t>(codepoint));
11260  bytes += 6;
11261  }
11262  else
11263  {
11264  (std::snprintf)(string_buffer.data() + bytes, 13, "\\u%04x\\u%04x",
11265  static_cast<uint16_t>(0xD7C0 + (codepoint >> 10)),
11266  static_cast<uint16_t>(0xDC00 + (codepoint & 0x3FF)));
11267  bytes += 12;
11268  }
11269  }
11270  else
11271  {
11272  // copy byte to buffer (all previous bytes
11273  // been copied have in default case above)
11274  string_buffer[bytes++] = s[i];
11275  }
11276  break;
11277  }
11278  }
11279 
11280  // write buffer and reset index; there must be 13 bytes
11281  // left, as this is the maximal number of bytes to be
11282  // written ("\uxxxx\uxxxx\0") for one code point
11283  if (string_buffer.size() - bytes < 13)
11284  {
11285  o->write_characters(string_buffer.data(), bytes);
11286  bytes = 0;
11287  }
11288 
11289  // remember the byte position of this accept
11290  bytes_after_last_accept = bytes;
11291  undumped_chars = 0;
11292  break;
11293  }
11294 
11295  case UTF8_REJECT: // decode found invalid UTF-8 byte
11296  {
11297  switch (error_handler)
11298  {
11299  case error_handler_t::strict:
11300  {
11301  std::string sn(3, '\0');
11302  (std::snprintf)(&sn[0], sn.size(), "%.2X", byte);
11303  JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn));
11304  }
11305 
11306  case error_handler_t::ignore:
11307  case error_handler_t::replace:
11308  {
11309  // in case we saw this character the first time, we
11310  // would like to read it again, because the byte
11311  // may be OK for itself, but just not OK for the
11312  // previous sequence
11313  if (undumped_chars > 0)
11314  {
11315  --i;
11316  }
11317 
11318  // reset length buffer to the last accepted index;
11319  // thus removing/ignoring the invalid characters
11320  bytes = bytes_after_last_accept;
11321 
11322  if (error_handler == error_handler_t::replace)
11323  {
11324  // add a replacement character
11325  if (ensure_ascii)
11326  {
11327  string_buffer[bytes++] = '\\';
11328  string_buffer[bytes++] = 'u';
11329  string_buffer[bytes++] = 'f';
11330  string_buffer[bytes++] = 'f';
11331  string_buffer[bytes++] = 'f';
11332  string_buffer[bytes++] = 'd';
11333  }
11334  else
11335  {
11336  string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xEF');
11337  string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBF');
11338  string_buffer[bytes++] = detail::binary_writer<BasicJsonType, char>::to_char_type('\xBD');
11339  }
11340  bytes_after_last_accept = bytes;
11341  }
11342 
11343  undumped_chars = 0;
11344 
11345  // continue processing the string
11346  state = UTF8_ACCEPT;
11347  break;
11348  }
11349  }
11350  break;
11351  }
11352 
11353  default: // decode found yet incomplete multi-byte code point
11354  {
11355  if (not ensure_ascii)
11356  {
11357  // code point will not be escaped - copy byte to buffer
11358  string_buffer[bytes++] = s[i];
11359  }
11360  ++undumped_chars;
11361  break;
11362  }
11363  }
11364  }
11365 
11366  // we finished processing the string
11367  if (JSON_LIKELY(state == UTF8_ACCEPT))
11368  {
11369  // write buffer
11370  if (bytes > 0)
11371  {
11372  o->write_characters(string_buffer.data(), bytes);
11373  }
11374  }
11375  else
11376  {
11377  // we finish reading, but do not accept: string was incomplete
11378  switch (error_handler)
11379  {
11380  case error_handler_t::strict:
11381  {
11382  std::string sn(3, '\0');
11383  (std::snprintf)(&sn[0], sn.size(), "%.2X", static_cast<uint8_t>(s.back()));
11384  JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + sn));
11385  }
11386 
11387  case error_handler_t::ignore:
11388  {
11389  // write all accepted bytes
11390  o->write_characters(string_buffer.data(), bytes_after_last_accept);
11391  break;
11392  }
11393 
11394  case error_handler_t::replace:
11395  {
11396  // write all accepted bytes
11397  o->write_characters(string_buffer.data(), bytes_after_last_accept);
11398  // add a replacement character
11399  if (ensure_ascii)
11400  {
11401  o->write_characters("\\ufffd", 6);
11402  }
11403  else
11404  {
11405  o->write_characters("\xEF\xBF\xBD", 3);
11406  }
11407  break;
11408  }
11409  }
11410  }
11411  }
11412 
11413  /*!
11414  @brief dump an integer
11415 
11416  Dump a given integer to output stream @a o. Works internally with
11417  @a number_buffer.
11418 
11419  @param[in] x integer number (signed or unsigned) to dump
11420  @tparam NumberType either @a number_integer_t or @a number_unsigned_t
11421  */
11422  template<typename NumberType, detail::enable_if_t<
11423  std::is_same<NumberType, number_unsigned_t>::value or
11424  std::is_same<NumberType, number_integer_t>::value,
11425  int> = 0>
11426  void dump_integer(NumberType x)
11427  {
11428  // special case for "0"
11429  if (x == 0)
11430  {
11431  o->write_character('0');
11432  return;
11433  }
11434 
11435  const bool is_negative = std::is_same<NumberType, number_integer_t>::value and not (x >= 0); // see issue #755
11436  std::size_t i = 0;
11437 
11438  while (x != 0)
11439  {
11440  // spare 1 byte for '\0'
11441  assert(i < number_buffer.size() - 1);
11442 
11443  const auto digit = std::labs(static_cast<long>(x % 10));
11444  number_buffer[i++] = static_cast<char>('0' + digit);
11445  x /= 10;
11446  }
11447 
11448  if (is_negative)
11449  {
11450  // make sure there is capacity for the '-'
11451  assert(i < number_buffer.size() - 2);
11452  number_buffer[i++] = '-';
11453  }
11454 
11455  std::reverse(number_buffer.begin(), number_buffer.begin() + i);
11456  o->write_characters(number_buffer.data(), i);
11457  }
11458 
11459  /*!
11460  @brief dump a floating-point number
11461 
11462  Dump a given floating-point number to output stream @a o. Works internally
11463  with @a number_buffer.
11464 
11465  @param[in] x floating-point number to dump
11466  */
11467  void dump_float(number_float_t x)
11468  {
11469  // NaN / inf
11470  if (not std::isfinite(x))
11471  {
11472  o->write_characters("null", 4);
11473  return;
11474  }
11475 
11476  // If number_float_t is an IEEE-754 single or double precision number,
11477  // use the Grisu2 algorithm to produce short numbers which are
11478  // guaranteed to round-trip, using strtof and strtod, resp.
11479  //
11480  // NB: The test below works if <long double> == <double>.
11481  static constexpr bool is_ieee_single_or_double
11482  = (std::numeric_limits<number_float_t>::is_iec559 and std::numeric_limits<number_float_t>::digits == 24 and std::numeric_limits<number_float_t>::max_exponent == 128) or
11483  (std::numeric_limits<number_float_t>::is_iec559 and std::numeric_limits<number_float_t>::digits == 53 and std::numeric_limits<number_float_t>::max_exponent == 1024);
11484 
11485  dump_float(x, std::integral_constant<bool, is_ieee_single_or_double>());
11486  }
11487 
11488  void dump_float(number_float_t x, std::true_type /*is_ieee_single_or_double*/)
11489  {
11490  char* begin = number_buffer.data();
11491  char* end = ::nlohmann::detail::to_chars(begin, begin + number_buffer.size(), x);
11492 
11493  o->write_characters(begin, static_cast<size_t>(end - begin));
11494  }
11495 
11496  void dump_float(number_float_t x, std::false_type /*is_ieee_single_or_double*/)
11497  {
11498  // get number of digits for a float -> text -> float round-trip
11499  static constexpr auto d = std::numeric_limits<number_float_t>::max_digits10;
11500 
11501  // the actual conversion
11502  std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*g", d, x);
11503 
11504  // negative value indicates an error
11505  assert(len > 0);
11506  // check if buffer was large enough
11507  assert(static_cast<std::size_t>(len) < number_buffer.size());
11508 
11509  // erase thousands separator
11510  if (thousands_sep != '\0')
11511  {
11512  const auto end = std::remove(number_buffer.begin(),
11513  number_buffer.begin() + len, thousands_sep);
11514  std::fill(end, number_buffer.end(), '\0');
11515  assert((end - number_buffer.begin()) <= len);
11516  len = (end - number_buffer.begin());
11517  }
11518 
11519  // convert decimal point to '.'
11520  if (decimal_point != '\0' and decimal_point != '.')
11521  {
11522  const auto dec_pos = std::find(number_buffer.begin(), number_buffer.end(), decimal_point);
11523  if (dec_pos != number_buffer.end())
11524  {
11525  *dec_pos = '.';
11526  }
11527  }
11528 
11529  o->write_characters(number_buffer.data(), static_cast<std::size_t>(len));
11530 
11531  // determine if need to append ".0"
11532  const bool value_is_int_like =
11533  std::none_of(number_buffer.begin(), number_buffer.begin() + len + 1,
11534  [](char c)
11535  {
11536  return (c == '.' or c == 'e');
11537  });
11538 
11539  if (value_is_int_like)
11540  {
11541  o->write_characters(".0", 2);
11542  }
11543  }
11544 
11545  /*!
11546  @brief check whether a string is UTF-8 encoded
11547 
11548  The function checks each byte of a string whether it is UTF-8 encoded. The
11549  result of the check is stored in the @a state parameter. The function must
11550  be called initially with state 0 (accept). State 1 means the string must
11551  be rejected, because the current byte is not allowed. If the string is
11552  completely processed, but the state is non-zero, the string ended
11553  prematurely; that is, the last byte indicated more bytes should have
11554  followed.
11555 
11556  @param[in,out] state the state of the decoding
11557  @param[in,out] codep codepoint (valid only if resulting state is UTF8_ACCEPT)
11558  @param[in] byte next byte to decode
11559  @return new state
11560 
11561  @note The function has been edited: a std::array is used.
11562 
11563  @copyright Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
11564  @sa http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
11565  */
11566  static uint8_t decode(uint8_t& state, uint32_t& codep, const uint8_t byte) noexcept
11567  {
11568  static const std::array<uint8_t, 400> utf8d =
11569  {
11570  {
11571  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
11572  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
11573  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
11574  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
11575  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
11576  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
11577  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
11578  0xA, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3, // E0..EF
11579  0xB, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, // F0..FF
11580  0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, 0x6, 0x1, 0x1, 0x1, 0x1, // s0..s0
11581  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
11582  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
11583  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
11584  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
11585  }
11586  };
11587 
11588  const uint8_t type = utf8d[byte];
11589 
11590  codep = (state != UTF8_ACCEPT)
11591  ? (byte & 0x3fu) | (codep << 6)
11592  : static_cast<uint32_t>(0xff >> type) & (byte);
11593 
11594  state = utf8d[256u + state * 16u + type];
11595  return state;
11596  }
11597 
11598  private:
11599  /// the output of the serializer
11600  output_adapter_t<char> o = nullptr;
11601 
11602  /// a (hopefully) large enough character buffer
11603  std::array<char, 64> number_buffer{{}};
11604 
11605  /// the locale
11606  const std::lconv* loc = nullptr;
11607  /// the locale's thousand separator character
11608  const char thousands_sep = '\0';
11609  /// the locale's decimal point character
11610  const char decimal_point = '\0';
11611 
11612  /// string buffer
11613  std::array<char, 512> string_buffer{{}};
11614 
11615  /// the indentation character
11616  const char indent_char;
11617  /// the indentation string
11618  string_t indent_string;
11619 
11620  /// error_handler how to react on decoding errors
11621  const error_handler_t error_handler;
11622 };
11623 } // namespace detail
11624 } // namespace nlohmann
11625 
11626 // #include <nlohmann/detail/json_ref.hpp>
11627 
11628 
11629 #include <initializer_list>
11630 #include <utility>
11631 
11632 // #include <nlohmann/detail/meta/type_traits.hpp>
11633 
11634 
11635 namespace nlohmann
11636 {
11637 namespace detail
11638 {
11639 template<typename BasicJsonType>
11640 class json_ref
11641 {
11642  public:
11643  using value_type = BasicJsonType;
11644 
11645  json_ref(value_type&& value)
11646  : owned_value(std::move(value)), value_ref(&owned_value), is_rvalue(true)
11647  {}
11648 
11649  json_ref(const value_type& value)
11650  : value_ref(const_cast<value_type*>(&value)), is_rvalue(false)
11651  {}
11652 
11653  json_ref(std::initializer_list<json_ref> init)
11654  : owned_value(init), value_ref(&owned_value), is_rvalue(true)
11655  {}
11656 
11657  template <
11658  class... Args,
11659  enable_if_t<std::is_constructible<value_type, Args...>::value, int> = 0 >
11660  json_ref(Args && ... args)
11661  : owned_value(std::forward<Args>(args)...), value_ref(&owned_value),
11662  is_rvalue(true) {}
11663 
11664  // class should be movable only
11665  json_ref(json_ref&&) = default;
11666  json_ref(const json_ref&) = delete;
11667  json_ref& operator=(const json_ref&) = delete;
11668  json_ref& operator=(json_ref&&) = delete;
11669  ~json_ref() = default;
11670 
11671  value_type moved_or_copied() const
11672  {
11673  if (is_rvalue)
11674  {
11675  return std::move(*value_ref);
11676  }
11677  return *value_ref;
11678  }
11679 
11680  value_type const& operator*() const
11681  {
11682  return *static_cast<value_type const*>(value_ref);
11683  }
11684 
11685  value_type const* operator->() const
11686  {
11687  return static_cast<value_type const*>(value_ref);
11688  }
11689 
11690  private:
11691  mutable value_type owned_value = nullptr;
11692  value_type* value_ref = nullptr;
11693  const bool is_rvalue;
11694 };
11695 } // namespace detail
11696 } // namespace nlohmann
11697 
11698 // #include <nlohmann/detail/json_pointer.hpp>
11699 
11700 
11701 #include <cassert> // assert
11702 #include <numeric> // accumulate
11703 #include <string> // string
11704 #include <vector> // vector
11705 
11706 // #include <nlohmann/detail/macro_scope.hpp>
11707 
11708 // #include <nlohmann/detail/exceptions.hpp>
11709 
11710 // #include <nlohmann/detail/value_t.hpp>
11711 
11712 
11713 namespace nlohmann
11714 {
11715 template<typename BasicJsonType>
11716 class json_pointer
11717 {
11718  // allow basic_json to access private members
11720  friend class basic_json;
11721 
11722  public:
11723  /*!
11724  @brief create JSON pointer
11725 
11726  Create a JSON pointer according to the syntax described in
11727  [Section 3 of RFC6901](https://tools.ietf.org/html/rfc6901#section-3).
11728 
11729  @param[in] s string representing the JSON pointer; if omitted, the empty
11730  string is assumed which references the whole JSON value
11731 
11732  @throw parse_error.107 if the given JSON pointer @a s is nonempty and does
11733  not begin with a slash (`/`); see example below
11734 
11735  @throw parse_error.108 if a tilde (`~`) in the given JSON pointer @a s is
11736  not followed by `0` (representing `~`) or `1` (representing `/`); see
11737  example below
11738 
11739  @liveexample{The example shows the construction several valid JSON pointers
11740  as well as the exceptional behavior.,json_pointer}
11741 
11742  @since version 2.0.0
11743  */
11744  explicit json_pointer(const std::string& s = "")
11746  {}
11747 
11748  /*!
11749  @brief return a string representation of the JSON pointer
11750 
11751  @invariant For each JSON pointer `ptr`, it holds:
11752  @code {.cpp}
11753  ptr == json_pointer(ptr.to_string());
11754  @endcode
11755 
11756  @return a string representation of the JSON pointer
11757 
11758  @liveexample{The example shows the result of `to_string`.,
11759  json_pointer__to_string}
11760 
11761  @since version 2.0.0
11762  */
11763  std::string to_string() const
11764  {
11765  return std::accumulate(reference_tokens.begin(), reference_tokens.end(),
11766  std::string{},
11767  [](const std::string & a, const std::string & b)
11768  {
11769  return a + "/" + escape(b);
11770  });
11771  }
11772 
11773  /// @copydoc to_string()
11774  operator std::string() const
11775  {
11776  return to_string();
11777  }
11778 
11779  /*!
11780  @param[in] s reference token to be converted into an array index
11781 
11782  @return integer representation of @a s
11783 
11784  @throw out_of_range.404 if string @a s could not be converted to an integer
11785  */
11786  static int array_index(const std::string& s)
11787  {
11788  std::size_t processed_chars = 0;
11789  const int res = std::stoi(s, &processed_chars);
11790 
11791  // check if the string was completely read
11792  if (JSON_UNLIKELY(processed_chars != s.size()))
11793  {
11794  JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'"));
11795  }
11796 
11797  return res;
11798  }
11799 
11800  private:
11801  /*!
11802  @brief remove and return last reference pointer
11803  @throw out_of_range.405 if JSON pointer has no parent
11804  */
11805  std::string pop_back()
11806  {
11807  if (JSON_UNLIKELY(is_root()))
11808  {
11809  JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent"));
11810  }
11811 
11812  auto last = reference_tokens.back();
11813  reference_tokens.pop_back();
11814  return last;
11815  }
11816 
11817  /// return whether pointer points to the root document
11818  bool is_root() const noexcept
11819  {
11820  return reference_tokens.empty();
11821  }
11822 
11823  json_pointer top() const
11824  {
11825  if (JSON_UNLIKELY(is_root()))
11826  {
11827  JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent"));
11828  }
11829 
11830  json_pointer result = *this;
11831  result.reference_tokens = {reference_tokens[0]};
11832  return result;
11833  }
11834 
11835  /*!
11836  @brief create and return a reference to the pointed to value
11837 
11838  @complexity Linear in the number of reference tokens.
11839 
11840  @throw parse_error.109 if array index is not a number
11841  @throw type_error.313 if value cannot be unflattened
11842  */
11843  BasicJsonType& get_and_create(BasicJsonType& j) const
11844  {
11845  using size_type = typename BasicJsonType::size_type;
11846  auto result = &j;
11847 
11848  // in case no reference tokens exist, return a reference to the JSON value
11849  // j which will be overwritten by a primitive value
11850  for (const auto& reference_token : reference_tokens)
11851  {
11852  switch (result->m_type)
11853  {
11854  case detail::value_t::null:
11855  {
11856  if (reference_token == "0")
11857  {
11858  // start a new array if reference token is 0
11859  result = &result->operator[](0);
11860  }
11861  else
11862  {
11863  // start a new object otherwise
11864  result = &result->operator[](reference_token);
11865  }
11866  break;
11867  }
11868 
11869  case detail::value_t::object:
11870  {
11871  // create an entry in the object
11872  result = &result->operator[](reference_token);
11873  break;
11874  }
11875 
11876  case detail::value_t::array:
11877  {
11878  // create an entry in the array
11879  JSON_TRY
11880  {
11881  result = &result->operator[](static_cast<size_type>(array_index(reference_token)));
11882  }
11883  JSON_CATCH(std::invalid_argument&)
11884  {
11885  JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number"));
11886  }
11887  break;
11888  }
11889 
11890  /*
11891  The following code is only reached if there exists a reference
11892  token _and_ the current value is primitive. In this case, we have
11893  an error situation, because primitive values may only occur as
11894  single value; that is, with an empty list of reference tokens.
11895  */
11896  default:
11897  JSON_THROW(detail::type_error::create(313, "invalid value to unflatten"));
11898  }
11899  }
11900 
11901  return *result;
11902  }
11903 
11904  /*!
11905  @brief return a reference to the pointed to value
11906 
11907  @note This version does not throw if a value is not present, but tries to
11908  create nested values instead. For instance, calling this function
11909  with pointer `"/this/that"` on a null value is equivalent to calling
11910  `operator[]("this").operator[]("that")` on that value, effectively
11911  changing the null value to an object.
11912 
11913  @param[in] ptr a JSON value
11914 
11915  @return reference to the JSON value pointed to by the JSON pointer
11916 
11917  @complexity Linear in the length of the JSON pointer.
11918 
11919  @throw parse_error.106 if an array index begins with '0'
11920  @throw parse_error.109 if an array index was not a number
11921  @throw out_of_range.404 if the JSON pointer can not be resolved
11922  */
11923  BasicJsonType& get_unchecked(BasicJsonType* ptr) const
11924  {
11925  using size_type = typename BasicJsonType::size_type;
11926  for (const auto& reference_token : reference_tokens)
11927  {
11928  // convert null values to arrays or objects before continuing
11929  if (ptr->m_type == detail::value_t::null)
11930  {
11931  // check if reference token is a number
11932  const bool nums =
11933  std::all_of(reference_token.begin(), reference_token.end(),
11934  [](const char x)
11935  {
11936  return (x >= '0' and x <= '9');
11937  });
11938 
11939  // change value to array for numbers or "-" or to object otherwise
11940  *ptr = (nums or reference_token == "-")
11941  ? detail::value_t::array
11942  : detail::value_t::object;
11943  }
11944 
11945  switch (ptr->m_type)
11946  {
11947  case detail::value_t::object:
11948  {
11949  // use unchecked object access
11950  ptr = &ptr->operator[](reference_token);
11951  break;
11952  }
11953 
11954  case detail::value_t::array:
11955  {
11956  // error condition (cf. RFC 6901, Sect. 4)
11957  if (JSON_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
11958  {
11959  JSON_THROW(detail::parse_error::create(106, 0,
11960  "array index '" + reference_token +
11961  "' must not begin with '0'"));
11962  }
11963 
11964  if (reference_token == "-")
11965  {
11966  // explicitly treat "-" as index beyond the end
11967  ptr = &ptr->operator[](ptr->m_value.array->size());
11968  }
11969  else
11970  {
11971  // convert array index to number; unchecked access
11972  JSON_TRY
11973  {
11974  ptr = &ptr->operator[](
11975  static_cast<size_type>(array_index(reference_token)));
11976  }
11977  JSON_CATCH(std::invalid_argument&)
11978  {
11979  JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number"));
11980  }
11981  }
11982  break;
11983  }
11984 
11985  default:
11986  JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'"));
11987  }
11988  }
11989 
11990  return *ptr;
11991  }
11992 
11993  /*!
11994  @throw parse_error.106 if an array index begins with '0'
11995  @throw parse_error.109 if an array index was not a number
11996  @throw out_of_range.402 if the array index '-' is used
11997  @throw out_of_range.404 if the JSON pointer can not be resolved
11998  */
11999  BasicJsonType& get_checked(BasicJsonType* ptr) const
12000  {
12001  using size_type = typename BasicJsonType::size_type;
12002  for (const auto& reference_token : reference_tokens)
12003  {
12004  switch (ptr->m_type)
12005  {
12006  case detail::value_t::object:
12007  {
12008  // note: at performs range check
12009  ptr = &ptr->at(reference_token);
12010  break;
12011  }
12012 
12013  case detail::value_t::array:
12014  {
12015  if (JSON_UNLIKELY(reference_token == "-"))
12016  {
12017  // "-" always fails the range check
12018  JSON_THROW(detail::out_of_range::create(402,
12019  "array index '-' (" + std::to_string(ptr->m_value.array->size()) +
12020  ") is out of range"));
12021  }
12022 
12023  // error condition (cf. RFC 6901, Sect. 4)
12024  if (JSON_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
12025  {
12026  JSON_THROW(detail::parse_error::create(106, 0,
12027  "array index '" + reference_token +
12028  "' must not begin with '0'"));
12029  }
12030 
12031  // note: at performs range check
12032  JSON_TRY
12033  {
12034  ptr = &ptr->at(static_cast<size_type>(array_index(reference_token)));
12035  }
12036  JSON_CATCH(std::invalid_argument&)
12037  {
12038  JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number"));
12039  }
12040  break;
12041  }
12042 
12043  default:
12044  JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'"));
12045  }
12046  }
12047 
12048  return *ptr;
12049  }
12050 
12051  /*!
12052  @brief return a const reference to the pointed to value
12053 
12054  @param[in] ptr a JSON value
12055 
12056  @return const reference to the JSON value pointed to by the JSON
12057  pointer
12058 
12059  @throw parse_error.106 if an array index begins with '0'
12060  @throw parse_error.109 if an array index was not a number
12061  @throw out_of_range.402 if the array index '-' is used
12062  @throw out_of_range.404 if the JSON pointer can not be resolved
12063  */
12064  const BasicJsonType& get_unchecked(const BasicJsonType* ptr) const
12065  {
12066  using size_type = typename BasicJsonType::size_type;
12067  for (const auto& reference_token : reference_tokens)
12068  {
12069  switch (ptr->m_type)
12070  {
12071  case detail::value_t::object:
12072  {
12073  // use unchecked object access
12074  ptr = &ptr->operator[](reference_token);
12075  break;
12076  }
12077 
12078  case detail::value_t::array:
12079  {
12080  if (JSON_UNLIKELY(reference_token == "-"))
12081  {
12082  // "-" cannot be used for const access
12083  JSON_THROW(detail::out_of_range::create(402,
12084  "array index '-' (" + std::to_string(ptr->m_value.array->size()) +
12085  ") is out of range"));
12086  }
12087 
12088  // error condition (cf. RFC 6901, Sect. 4)
12089  if (JSON_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
12090  {
12091  JSON_THROW(detail::parse_error::create(106, 0,
12092  "array index '" + reference_token +
12093  "' must not begin with '0'"));
12094  }
12095 
12096  // use unchecked array access
12097  JSON_TRY
12098  {
12099  ptr = &ptr->operator[](
12100  static_cast<size_type>(array_index(reference_token)));
12101  }
12102  JSON_CATCH(std::invalid_argument&)
12103  {
12104  JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number"));
12105  }
12106  break;
12107  }
12108 
12109  default:
12110  JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'"));
12111  }
12112  }
12113 
12114  return *ptr;
12115  }
12116 
12117  /*!
12118  @throw parse_error.106 if an array index begins with '0'
12119  @throw parse_error.109 if an array index was not a number
12120  @throw out_of_range.402 if the array index '-' is used
12121  @throw out_of_range.404 if the JSON pointer can not be resolved
12122  */
12123  const BasicJsonType& get_checked(const BasicJsonType* ptr) const
12124  {
12125  using size_type = typename BasicJsonType::size_type;
12126  for (const auto& reference_token : reference_tokens)
12127  {
12128  switch (ptr->m_type)
12129  {
12130  case detail::value_t::object:
12131  {
12132  // note: at performs range check
12133  ptr = &ptr->at(reference_token);
12134  break;
12135  }
12136 
12137  case detail::value_t::array:
12138  {
12139  if (JSON_UNLIKELY(reference_token == "-"))
12140  {
12141  // "-" always fails the range check
12142  JSON_THROW(detail::out_of_range::create(402,
12143  "array index '-' (" + std::to_string(ptr->m_value.array->size()) +
12144  ") is out of range"));
12145  }
12146 
12147  // error condition (cf. RFC 6901, Sect. 4)
12148  if (JSON_UNLIKELY(reference_token.size() > 1 and reference_token[0] == '0'))
12149  {
12150  JSON_THROW(detail::parse_error::create(106, 0,
12151  "array index '" + reference_token +
12152  "' must not begin with '0'"));
12153  }
12154 
12155  // note: at performs range check
12156  JSON_TRY
12157  {
12158  ptr = &ptr->at(static_cast<size_type>(array_index(reference_token)));
12159  }
12160  JSON_CATCH(std::invalid_argument&)
12161  {
12162  JSON_THROW(detail::parse_error::create(109, 0, "array index '" + reference_token + "' is not a number"));
12163  }
12164  break;
12165  }
12166 
12167  default:
12168  JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'"));
12169  }
12170  }
12171 
12172  return *ptr;
12173  }
12174 
12175  /*!
12176  @brief split the string input to reference tokens
12177 
12178  @note This function is only called by the json_pointer constructor.
12179  All exceptions below are documented there.
12180 
12181  @throw parse_error.107 if the pointer is not empty or begins with '/'
12182  @throw parse_error.108 if character '~' is not followed by '0' or '1'
12183  */
12184  static std::vector<std::string> split(const std::string& reference_string)
12185  {
12186  std::vector<std::string> result;
12187 
12188  // special case: empty reference string -> no reference tokens
12189  if (reference_string.empty())
12190  {
12191  return result;
12192  }
12193 
12194  // check if nonempty reference string begins with slash
12195  if (JSON_UNLIKELY(reference_string[0] != '/'))
12196  {
12197  JSON_THROW(detail::parse_error::create(107, 1,
12198  "JSON pointer must be empty or begin with '/' - was: '" +
12199  reference_string + "'"));
12200  }
12201 
12202  // extract the reference tokens:
12203  // - slash: position of the last read slash (or end of string)
12204  // - start: position after the previous slash
12205  for (
12206  // search for the first slash after the first character
12207  std::size_t slash = reference_string.find_first_of('/', 1),
12208  // set the beginning of the first reference token
12209  start = 1;
12210  // we can stop if start == 0 (if slash == std::string::npos)
12211  start != 0;
12212  // set the beginning of the next reference token
12213  // (will eventually be 0 if slash == std::string::npos)
12214  start = (slash == std::string::npos) ? 0 : slash + 1,
12215  // find next slash
12216  slash = reference_string.find_first_of('/', start))
12217  {
12218  // use the text between the beginning of the reference token
12219  // (start) and the last slash (slash).
12220  auto reference_token = reference_string.substr(start, slash - start);
12221 
12222  // check reference tokens are properly escaped
12223  for (std::size_t pos = reference_token.find_first_of('~');
12224  pos != std::string::npos;
12225  pos = reference_token.find_first_of('~', pos + 1))
12226  {
12227  assert(reference_token[pos] == '~');
12228 
12229  // ~ must be followed by 0 or 1
12230  if (JSON_UNLIKELY(pos == reference_token.size() - 1 or
12231  (reference_token[pos + 1] != '0' and
12232  reference_token[pos + 1] != '1')))
12233  {
12234  JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'"));
12235  }
12236  }
12237 
12238  // finally, store the reference token
12239  unescape(reference_token);
12240  result.push_back(reference_token);
12241  }
12242 
12243  return result;
12244  }
12245 
12246  /*!
12247  @brief replace all occurrences of a substring by another string
12248 
12249  @param[in,out] s the string to manipulate; changed so that all
12250  occurrences of @a f are replaced with @a t
12251  @param[in] f the substring to replace with @a t
12252  @param[in] t the string to replace @a f
12253 
12254  @pre The search string @a f must not be empty. **This precondition is
12255  enforced with an assertion.**
12256 
12257  @since version 2.0.0
12258  */
12259  static void replace_substring(std::string& s, const std::string& f,
12260  const std::string& t)
12261  {
12262  assert(not f.empty());
12263  for (auto pos = s.find(f); // find first occurrence of f
12264  pos != std::string::npos; // make sure f was found
12265  s.replace(pos, f.size(), t), // replace with t, and
12266  pos = s.find(f, pos + t.size())) // find next occurrence of f
12267  {}
12268  }
12269 
12270  /// escape "~" to "~0" and "/" to "~1"
12271  static std::string escape(std::string s)
12272  {
12273  replace_substring(s, "~", "~0");
12274  replace_substring(s, "/", "~1");
12275  return s;
12276  }
12277 
12278  /// unescape "~1" to tilde and "~0" to slash (order is important!)
12279  static void unescape(std::string& s)
12280  {
12281  replace_substring(s, "~1", "/");
12282  replace_substring(s, "~0", "~");
12283  }
12284 
12285  /*!
12286  @param[in] reference_string the reference string to the current value
12287  @param[in] value the value to consider
12288  @param[in,out] result the result object to insert values to
12289 
12290  @note Empty objects or arrays are flattened to `null`.
12291  */
12292  static void flatten(const std::string& reference_string,
12293  const BasicJsonType& value,
12294  BasicJsonType& result)
12295  {
12296  switch (value.m_type)
12297  {
12298  case detail::value_t::array:
12299  {
12300  if (value.m_value.array->empty())
12301  {
12302  // flatten empty array as null
12303  result[reference_string] = nullptr;
12304  }
12305  else
12306  {
12307  // iterate array and use index as reference string
12308  for (std::size_t i = 0; i < value.m_value.array->size(); ++i)
12309  {
12310  flatten(reference_string + "/" + std::to_string(i),
12311  value.m_value.array->operator[](i), result);
12312  }
12313  }
12314  break;
12315  }
12316 
12317  case detail::value_t::object:
12318  {
12319  if (value.m_value.object->empty())
12320  {
12321  // flatten empty object as null
12322  result[reference_string] = nullptr;
12323  }
12324  else
12325  {
12326  // iterate object and use keys as reference string
12327  for (const auto& element : *value.m_value.object)
12328  {
12329  flatten(reference_string + "/" + escape(element.first), element.second, result);
12330  }
12331  }
12332  break;
12333  }
12334 
12335  default:
12336  {
12337  // add primitive value with its reference string
12338  result[reference_string] = value;
12339  break;
12340  }
12341  }
12342  }
12343 
12344  /*!
12345  @param[in] value flattened JSON
12346 
12347  @return unflattened JSON
12348 
12349  @throw parse_error.109 if array index is not a number
12350  @throw type_error.314 if value is not an object
12351  @throw type_error.315 if object values are not primitive
12352  @throw type_error.313 if value cannot be unflattened
12353  */
12354  static BasicJsonType
12355  unflatten(const BasicJsonType& value)
12356  {
12357  if (JSON_UNLIKELY(not value.is_object()))
12358  {
12359  JSON_THROW(detail::type_error::create(314, "only objects can be unflattened"));
12360  }
12361 
12362  BasicJsonType result;
12363 
12364  // iterate the JSON object values
12365  for (const auto& element : *value.m_value.object)
12366  {
12367  if (JSON_UNLIKELY(not element.second.is_primitive()))
12368  {
12369  JSON_THROW(detail::type_error::create(315, "values in object must be primitive"));
12370  }
12371 
12372  // assign value to reference pointed to by JSON pointer; Note that if
12373  // the JSON pointer is "" (i.e., points to the whole value), function
12374  // get_and_create returns a reference to result itself. An assignment
12375  // will then create a primitive value.
12376  json_pointer(element.first).get_and_create(result) = element.second;
12377  }
12378 
12379  return result;
12380  }
12381 
12382  friend bool operator==(json_pointer const& lhs,
12383  json_pointer const& rhs) noexcept
12384  {
12385  return (lhs.reference_tokens == rhs.reference_tokens);
12386  }
12388  friend bool operator!=(json_pointer const& lhs,
12389  json_pointer const& rhs) noexcept
12390  {
12391  return not (lhs == rhs);
12392  }
12394  /// the reference tokens
12395  std::vector<std::string> reference_tokens;
12396 };
12397 } // namespace nlohmann
12398 
12399 // #include <nlohmann/adl_serializer.hpp>
12400 
12401 
12402 #include <utility>
12403 
12404 // #include <nlohmann/detail/conversions/from_json.hpp>
12405 
12406 // #include <nlohmann/detail/conversions/to_json.hpp>
12407 
12408 
12409 namespace nlohmann
12410 {
12411 
12412 template<typename, typename>
12413 struct adl_serializer
12414 {
12415  /*!
12416  @brief convert a JSON value to any value type
12417 
12418  This function is usually called by the `get()` function of the
12419  @ref basic_json class (either explicit or via conversion operators).
12420 
12421  @param[in] j JSON value to read from
12422  @param[in,out] val value to write to
12423  */
12424  template<typename BasicJsonType, typename ValueType>
12425  static auto from_json(BasicJsonType&& j, ValueType& val) noexcept(
12426  noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), val)))
12427  -> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), val), void())
12428  {
12429  ::nlohmann::from_json(std::forward<BasicJsonType>(j), val);
12430  }
12431 
12432  /*!
12433  @brief convert any value type to a JSON value
12434 
12435  This function is usually called by the constructors of the @ref basic_json
12436  class.
12437 
12438  @param[in,out] j JSON value to write to
12439  @param[in] val value to read from
12440  */
12441  template <typename BasicJsonType, typename ValueType>
12442  static auto to_json(BasicJsonType& j, ValueType&& val) noexcept(
12443  noexcept(::nlohmann::to_json(j, std::forward<ValueType>(val))))
12444  -> decltype(::nlohmann::to_json(j, std::forward<ValueType>(val)), void())
12445  {
12446  ::nlohmann::to_json(j, std::forward<ValueType>(val));
12447  }
12448 };
12449 
12450 } // namespace nlohmann
12451 
12452 
12453 /*!
12454 @brief namespace for Niels Lohmann
12455 @see https://github.com/nlohmann
12456 @since version 1.0.0
12457 */
12458 namespace nlohmann
12459 {
12460 
12461 /*!
12462 @brief a class to store JSON values
12463 
12464 @tparam ObjectType type for JSON objects (`std::map` by default; will be used
12465 in @ref object_t)
12466 @tparam ArrayType type for JSON arrays (`std::vector` by default; will be used
12467 in @ref array_t)
12468 @tparam StringType type for JSON strings and object keys (`std::string` by
12469 default; will be used in @ref string_t)
12470 @tparam BooleanType type for JSON booleans (`bool` by default; will be used
12471 in @ref boolean_t)
12472 @tparam NumberIntegerType type for JSON integer numbers (`int64_t` by
12473 default; will be used in @ref number_integer_t)
12474 @tparam NumberUnsignedType type for JSON unsigned integer numbers (@c
12475 `uint64_t` by default; will be used in @ref number_unsigned_t)
12476 @tparam NumberFloatType type for JSON floating-point numbers (`double` by
12477 default; will be used in @ref number_float_t)
12478 @tparam AllocatorType type of the allocator to use (`std::allocator` by
12479 default)
12480 @tparam JSONSerializer the serializer to resolve internal calls to `to_json()`
12481 and `from_json()` (@ref adl_serializer by default)
12482 
12483 @requirement The class satisfies the following concept requirements:
12484 - Basic
12485  - [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible):
12486  JSON values can be default constructed. The result will be a JSON null
12487  value.
12488  - [MoveConstructible](https://en.cppreference.com/w/cpp/named_req/MoveConstructible):
12489  A JSON value can be constructed from an rvalue argument.
12490  - [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible):
12491  A JSON value can be copy-constructed from an lvalue expression.
12492  - [MoveAssignable](https://en.cppreference.com/w/cpp/named_req/MoveAssignable):
12493  A JSON value van be assigned from an rvalue argument.
12494  - [CopyAssignable](https://en.cppreference.com/w/cpp/named_req/CopyAssignable):
12495  A JSON value can be copy-assigned from an lvalue expression.
12496  - [Destructible](https://en.cppreference.com/w/cpp/named_req/Destructible):
12497  JSON values can be destructed.
12498 - Layout
12499  - [StandardLayoutType](https://en.cppreference.com/w/cpp/named_req/StandardLayoutType):
12500  JSON values have
12501  [standard layout](https://en.cppreference.com/w/cpp/language/data_members#Standard_layout):
12502  All non-static data members are private and standard layout types, the
12503  class has no virtual functions or (virtual) base classes.
12504 - Library-wide
12505  - [EqualityComparable](https://en.cppreference.com/w/cpp/named_req/EqualityComparable):
12506  JSON values can be compared with `==`, see @ref
12507  operator==(const_reference,const_reference).
12508  - [LessThanComparable](https://en.cppreference.com/w/cpp/named_req/LessThanComparable):
12509  JSON values can be compared with `<`, see @ref
12510  operator<(const_reference,const_reference).
12511  - [Swappable](https://en.cppreference.com/w/cpp/named_req/Swappable):
12512  Any JSON lvalue or rvalue of can be swapped with any lvalue or rvalue of
12513  other compatible types, using unqualified function call @ref swap().
12514  - [NullablePointer](https://en.cppreference.com/w/cpp/named_req/NullablePointer):
12515  JSON values can be compared against `std::nullptr_t` objects which are used
12516  to model the `null` value.
12517 - Container
12518  - [Container](https://en.cppreference.com/w/cpp/named_req/Container):
12519  JSON values can be used like STL containers and provide iterator access.
12520  - [ReversibleContainer](https://en.cppreference.com/w/cpp/named_req/ReversibleContainer);
12521  JSON values can be used like STL containers and provide reverse iterator
12522  access.
12523 
12524 @invariant The member variables @a m_value and @a m_type have the following
12525 relationship:
12526 - If `m_type == value_t::object`, then `m_value.object != nullptr`.
12527 - If `m_type == value_t::array`, then `m_value.array != nullptr`.
12528 - If `m_type == value_t::string`, then `m_value.string != nullptr`.
12529 The invariants are checked by member function assert_invariant().
12530 
12531 @internal
12532 @note ObjectType trick from http://stackoverflow.com/a/9860911
12533 @endinternal
12534 
12535 @see [RFC 7159: The JavaScript Object Notation (JSON) Data Interchange
12536 Format](http://rfc7159.net/rfc7159)
12537 
12538 @since version 1.0.0
12539 
12540 @nosubgrouping
12541 */
12543 class basic_json
12544 {
12545  private:
12546  template<detail::value_t> friend struct detail::external_constructor;
12547  friend ::nlohmann::json_pointer<basic_json>;
12548  friend ::nlohmann::detail::parser<basic_json>;
12549  friend ::nlohmann::detail::serializer<basic_json>;
12550  template<typename BasicJsonType>
12551  friend class ::nlohmann::detail::iter_impl;
12552  template<typename BasicJsonType, typename CharType>
12553  friend class ::nlohmann::detail::binary_writer;
12554  template<typename BasicJsonType, typename SAX>
12555  friend class ::nlohmann::detail::binary_reader;
12556  template<typename BasicJsonType>
12557  friend class ::nlohmann::detail::json_sax_dom_parser;
12558  template<typename BasicJsonType>
12559  friend class ::nlohmann::detail::json_sax_dom_callback_parser;
12560 
12561  /// workaround type for MSVC
12563 
12564  // convenience aliases for types residing in namespace detail;
12565  using lexer = ::nlohmann::detail::lexer<basic_json>;
12566  using parser = ::nlohmann::detail::parser<basic_json>;
12567 
12568  using primitive_iterator_t = ::nlohmann::detail::primitive_iterator_t;
12569  template<typename BasicJsonType>
12570  using internal_iterator = ::nlohmann::detail::internal_iterator<BasicJsonType>;
12571  template<typename BasicJsonType>
12572  using iter_impl = ::nlohmann::detail::iter_impl<BasicJsonType>;
12573  template<typename Iterator>
12574  using iteration_proxy = ::nlohmann::detail::iteration_proxy<Iterator>;
12575  template<typename Base> using json_reverse_iterator = ::nlohmann::detail::json_reverse_iterator<Base>;
12576 
12577  template<typename CharType>
12579 
12581  template<typename CharType> using binary_writer = ::nlohmann::detail::binary_writer<basic_json, CharType>;
12582 
12583  using serializer = ::nlohmann::detail::serializer<basic_json>;
12584 
12585  public:
12586  using value_t = detail::value_t;
12587  /// JSON Pointer, see @ref nlohmann::json_pointer
12588  using json_pointer = ::nlohmann::json_pointer<basic_json>;
12589  template<typename T, typename SFINAE>
12590  using json_serializer = JSONSerializer<T, SFINAE>;
12591  /// how to treat decoding errors
12592  using error_handler_t = detail::error_handler_t;
12593  /// helper type for initializer lists of basic_json values
12594  using initializer_list_t = std::initializer_list<detail::json_ref<basic_json>>;
12596  using input_format_t = detail::input_format_t;
12597  /// SAX interface type, see @ref nlohmann::json_sax
12598  using json_sax_t = json_sax<basic_json>;
12600  ////////////////
12601  // exceptions //
12602  ////////////////
12604  /// @name exceptions
12605  /// Classes to implement user-defined exceptions.
12606  /// @{
12607 
12608  /// @copydoc detail::exception
12609  using exception = detail::exception;
12610  /// @copydoc detail::parse_error
12611  using parse_error = detail::parse_error;
12612  /// @copydoc detail::invalid_iterator
12613  using invalid_iterator = detail::invalid_iterator;
12614  /// @copydoc detail::type_error
12615  using type_error = detail::type_error;
12616  /// @copydoc detail::out_of_range
12617  using out_of_range = detail::out_of_range;
12618  /// @copydoc detail::other_error
12619  using other_error = detail::other_error;
12621  /// @}
12623 
12624  /////////////////////
12625  // container types //
12626  /////////////////////
12627 
12628  /// @name container types
12629  /// The canonic container types to use @ref basic_json like any other STL
12630  /// container.
12631  /// @{
12632 
12633  /// the type of elements in a basic_json container
12634  using value_type = basic_json;
12635 
12636  /// the type of an element reference
12637  using reference = value_type&;
12638  /// the type of an element const reference
12639  using const_reference = const value_type&;
12640 
12641  /// a type to represent differences between iterators
12642  using difference_type = std::ptrdiff_t;
12643  /// a type to represent container sizes
12644  using size_type = std::size_t;
12645 
12646  /// the allocator type
12647  using allocator_type = AllocatorType<basic_json>;
12648 
12649  /// the type of an element pointer
12650  using pointer = typename std::allocator_traits<allocator_type>::pointer;
12651  /// the type of an element const pointer
12653 
12654  /// an iterator for a basic_json container
12656  /// a const iterator for a basic_json container
12658  /// a reverse iterator for a basic_json container
12660  /// a const reverse iterator for a basic_json container
12663  /// @}
12665 
12666  /*!
12667  @brief returns the allocator associated with the container
12668  */
12669  static allocator_type get_allocator()
12670  {
12671  return allocator_type();
12672  }
12673 
12674  /*!
12675  @brief returns version information on the library
12676 
12677  This function returns a JSON object with information about the library,
12678  including the version number and information on the platform and compiler.
12679 
12680  @return JSON object holding version information
12681  key | description
12682  ----------- | ---------------
12683  `compiler` | Information on the used compiler. It is an object with the following keys: `c++` (the used C++ standard), `family` (the compiler family; possible values are `clang`, `icc`, `gcc`, `ilecpp`, `msvc`, `pgcpp`, `sunpro`, and `unknown`), and `version` (the compiler version).
12684  `copyright` | The copyright line for the library as string.
12685  `name` | The name of the library as string.
12686  `platform` | The used platform as string. Possible values are `win32`, `linux`, `apple`, `unix`, and `unknown`.
12687  `url` | The URL of the project as string.
12688  `version` | The version of the library. It is an object with the following keys: `major`, `minor`, and `patch` as defined by [Semantic Versioning](http://semver.org), and `string` (the version string).
12689 
12690  @liveexample{The following code shows an example output of the `meta()`
12691  function.,meta}
12692 
12693  @exceptionsafety Strong guarantee: if an exception is thrown, there are no
12694  changes to any JSON value.
12695 
12696  @complexity Constant.
12697 
12698  @since 2.1.0
12699  */
12700  static basic_json meta()
12701  {
12702  basic_json result;
12703 
12704  result["copyright"] = "(C) 2013-2017 Niels Lohmann";
12705  result["name"] = "JSON for Modern C++";
12706  result["url"] = "https://github.com/nlohmann/json";
12707  result["version"]["string"] =
12708  std::to_string(NLOHMANN_JSON_VERSION_MAJOR) + "." +
12709  std::to_string(NLOHMANN_JSON_VERSION_MINOR) + "." +
12710  std::to_string(NLOHMANN_JSON_VERSION_PATCH);
12711  result["version"]["major"] = NLOHMANN_JSON_VERSION_MAJOR;
12712  result["version"]["minor"] = NLOHMANN_JSON_VERSION_MINOR;
12713  result["version"]["patch"] = NLOHMANN_JSON_VERSION_PATCH;
12714 
12715 #ifdef _WIN32
12716  result["platform"] = "win32";
12717 #elif defined __linux__
12718  result["platform"] = "linux";
12719 #elif defined __APPLE__
12720  result["platform"] = "apple";
12721 #elif defined __unix__
12722  result["platform"] = "unix";
12723 #else
12724  result["platform"] = "unknown";
12725 #endif
12726 
12727 #if defined(__ICC) || defined(__INTEL_COMPILER)
12728  result["compiler"] = {{"family", "icc"}, {"version", __INTEL_COMPILER}};
12729 #elif defined(__clang__)
12730  result["compiler"] = {{"family", "clang"}, {"version", __clang_version__}};
12731 #elif defined(__GNUC__) || defined(__GNUG__)
12732  result["compiler"] = {{"family", "gcc"}, {"version", std::to_string(__GNUC__) + "." + std::to_string(__GNUC_MINOR__) + "." + std::to_string(__GNUC_PATCHLEVEL__)}};
12733 #elif defined(__HP_cc) || defined(__HP_aCC)
12734  result["compiler"] = "hp"
12735 #elif defined(__IBMCPP__)
12736  result["compiler"] = {{"family", "ilecpp"}, {"version", __IBMCPP__}};
12737 #elif defined(_MSC_VER)
12738  result["compiler"] = {{"family", "msvc"}, {"version", _MSC_VER}};
12739 #elif defined(__PGI)
12740  result["compiler"] = {{"family", "pgcpp"}, {"version", __PGI}};
12741 #elif defined(__SUNPRO_CC)
12742  result["compiler"] = {{"family", "sunpro"}, {"version", __SUNPRO_CC}};
12743 #else
12744  result["compiler"] = {{"family", "unknown"}, {"version", "unknown"}};
12745 #endif
12746 
12747 #ifdef __cplusplus
12748  result["compiler"]["c++"] = std::to_string(__cplusplus);
12749 #else
12750  result["compiler"]["c++"] = "unknown";
12751 #endif
12752  return result;
12753  }
12754 
12755 
12756  ///////////////////////////
12757  // JSON value data types //
12758  ///////////////////////////
12759 
12760  /// @name JSON value data types
12761  /// The data types to store a JSON value. These types are derived from
12762  /// the template arguments passed to class @ref basic_json.
12763  /// @{
12764 
12765 #if defined(JSON_HAS_CPP_14)
12766  // Use transparent comparator if possible, combined with perfect forwarding
12767  // on find() and count() calls prevents unnecessary string construction.
12768  using object_comparator_t = std::less<>;
12769 #else
12771 #endif
12772 
12773  /*!
12774  @brief a type for an object
12776  [RFC 7159](http://rfc7159.net/rfc7159) describes JSON objects as follows:
12777  > An object is an unordered collection of zero or more name/value pairs,
12778  > where a name is a string and a value is a string, number, boolean, null,
12779  > object, or array.
12780 
12781  To store objects in C++, a type is defined by the template parameters
12782  described below.
12783 
12784  @tparam ObjectType the container to store objects (e.g., `std::map` or
12785  `std::unordered_map`)
12786  @tparam StringType the type of the keys or names (e.g., `std::string`).
12787  The comparison function `std::less<StringType>` is used to order elements
12788  inside the container.
12789  @tparam AllocatorType the allocator to use for objects (e.g.,
12790  `std::allocator`)
12791 
12792  #### Default type
12793 
12794  With the default values for @a ObjectType (`std::map`), @a StringType
12795  (`std::string`), and @a AllocatorType (`std::allocator`), the default
12796  value for @a object_t is:
12797 
12798  @code {.cpp}
12799  std::map<
12800  std::string, // key_type
12801  basic_json, // value_type
12802  std::less<std::string>, // key_compare
12803  std::allocator<std::pair<const std::string, basic_json>> // allocator_type
12804  >
12805  @endcode
12806 
12807  #### Behavior
12808 
12809  The choice of @a object_t influences the behavior of the JSON class. With
12810  the default type, objects have the following behavior:
12811 
12812  - When all names are unique, objects will be interoperable in the sense
12813  that all software implementations receiving that object will agree on
12814  the name-value mappings.
12815  - When the names within an object are not unique, it is unspecified which
12816  one of the values for a given key will be chosen. For instance,
12817  `{"key": 2, "key": 1}` could be equal to either `{"key": 1}` or
12818  `{"key": 2}`.
12819  - Internally, name/value pairs are stored in lexicographical order of the
12820  names. Objects will also be serialized (see @ref dump) in this order.
12821  For instance, `{"b": 1, "a": 2}` and `{"a": 2, "b": 1}` will be stored
12822  and serialized as `{"a": 2, "b": 1}`.
12823  - When comparing objects, the order of the name/value pairs is irrelevant.
12824  This makes objects interoperable in the sense that they will not be
12825  affected by these differences. For instance, `{"b": 1, "a": 2}` and
12826  `{"a": 2, "b": 1}` will be treated as equal.
12827 
12828  #### Limits
12829 
12830  [RFC 7159](http://rfc7159.net/rfc7159) specifies:
12831  > An implementation may set limits on the maximum depth of nesting.
12832 
12833  In this class, the object's limit of nesting is not explicitly constrained.
12834  However, a maximum depth of nesting may be introduced by the compiler or
12835  runtime environment. A theoretical limit can be queried by calling the
12836  @ref max_size function of a JSON object.
12837 
12838  #### Storage
12839 
12840  Objects are stored as pointers in a @ref basic_json type. That is, for any
12841  access to object values, a pointer of type `object_t*` must be
12842  dereferenced.
12843 
12844  @sa @ref array_t -- type for an array value
12845 
12846  @since version 1.0.0
12847 
12848  @note The order name/value pairs are added to the object is *not*
12849  preserved by the library. Therefore, iterating an object may return
12850  name/value pairs in a different order than they were originally stored. In
12851  fact, keys will be traversed in alphabetical order as `std::map` with
12852  `std::less` is used by default. Please note this behavior conforms to [RFC
12853  7159](http://rfc7159.net/rfc7159), because any order implements the
12854  specified "unordered" nature of JSON objects.
12855  */
12856  using object_t = ObjectType<StringType,
12857  basic_json,
12860  basic_json>>>;
12861 
12862  /*!
12863  @brief a type for an array
12864 
12865  [RFC 7159](http://rfc7159.net/rfc7159) describes JSON arrays as follows:
12866  > An array is an ordered sequence of zero or more values.
12867 
12868  To store objects in C++, a type is defined by the template parameters
12869  explained below.
12870 
12871  @tparam ArrayType container type to store arrays (e.g., `std::vector` or
12872  `std::list`)
12873  @tparam AllocatorType allocator to use for arrays (e.g., `std::allocator`)
12874 
12875  #### Default type
12876 
12877  With the default values for @a ArrayType (`std::vector`) and @a
12878  AllocatorType (`std::allocator`), the default value for @a array_t is:
12879 
12880  @code {.cpp}
12881  std::vector<
12882  basic_json, // value_type
12883  std::allocator<basic_json> // allocator_type
12884  >
12885  @endcode
12886 
12887  #### Limits
12888 
12889  [RFC 7159](http://rfc7159.net/rfc7159) specifies:
12890  > An implementation may set limits on the maximum depth of nesting.
12891 
12892  In this class, the array's limit of nesting is not explicitly constrained.
12893  However, a maximum depth of nesting may be introduced by the compiler or
12894  runtime environment. A theoretical limit can be queried by calling the
12895  @ref max_size function of a JSON array.
12896 
12897  #### Storage
12898 
12899  Arrays are stored as pointers in a @ref basic_json type. That is, for any
12900  access to array values, a pointer of type `array_t*` must be dereferenced.
12901 
12902  @sa @ref object_t -- type for an object value
12903 
12904  @since version 1.0.0
12905  */
12906  using array_t = ArrayType<basic_json, AllocatorType<basic_json>>;
12907 
12908  /*!
12909  @brief a type for a string
12910 
12911  [RFC 7159](http://rfc7159.net/rfc7159) describes JSON strings as follows:
12912  > A string is a sequence of zero or more Unicode characters.
12913 
12914  To store objects in C++, a type is defined by the template parameter
12915  described below. Unicode values are split by the JSON class into
12916  byte-sized characters during deserialization.
12917 
12918  @tparam StringType the container to store strings (e.g., `std::string`).
12919  Note this container is used for keys/names in objects, see @ref object_t.
12920 
12921  #### Default type
12922 
12923  With the default values for @a StringType (`std::string`), the default
12924  value for @a string_t is:
12925 
12926  @code {.cpp}
12927  std::string
12928  @endcode
12929 
12930  #### Encoding
12931 
12932  Strings are stored in UTF-8 encoding. Therefore, functions like
12933  `std::string::size()` or `std::string::length()` return the number of
12934  bytes in the string rather than the number of characters or glyphs.
12935 
12936  #### String comparison
12937 
12938  [RFC 7159](http://rfc7159.net/rfc7159) states:
12939  > Software implementations are typically required to test names of object
12940  > members for equality. Implementations that transform the textual
12941  > representation into sequences of Unicode code units and then perform the
12942  > comparison numerically, code unit by code unit, are interoperable in the
12943  > sense that implementations will agree in all cases on equality or
12944  > inequality of two strings. For example, implementations that compare
12945  > strings with escaped characters unconverted may incorrectly find that
12946  > `"a\\b"` and `"a\u005Cb"` are not equal.
12947 
12948  This implementation is interoperable as it does compare strings code unit
12949  by code unit.
12950 
12951  #### Storage
12952 
12953  String values are stored as pointers in a @ref basic_json type. That is,
12954  for any access to string values, a pointer of type `string_t*` must be
12955  dereferenced.
12956 
12957  @since version 1.0.0
12958  */
12959  using string_t = StringType;
12960 
12961  /*!
12962  @brief a type for a boolean
12963 
12964  [RFC 7159](http://rfc7159.net/rfc7159) implicitly describes a boolean as a
12965  type which differentiates the two literals `true` and `false`.
12966 
12967  To store objects in C++, a type is defined by the template parameter @a
12968  BooleanType which chooses the type to use.
12969 
12970  #### Default type
12971 
12972  With the default values for @a BooleanType (`bool`), the default value for
12973  @a boolean_t is:
12974 
12975  @code {.cpp}
12976  bool
12977  @endcode
12978 
12979  #### Storage
12980 
12981  Boolean values are stored directly inside a @ref basic_json type.
12982 
12983  @since version 1.0.0
12984  */
12985  using boolean_t = BooleanType;
12986 
12987  /*!
12988  @brief a type for a number (integer)
12989 
12990  [RFC 7159](http://rfc7159.net/rfc7159) describes numbers as follows:
12991  > The representation of numbers is similar to that used in most
12992  > programming languages. A number is represented in base 10 using decimal
12993  > digits. It contains an integer component that may be prefixed with an
12994  > optional minus sign, which may be followed by a fraction part and/or an
12995  > exponent part. Leading zeros are not allowed. (...) Numeric values that
12996  > cannot be represented in the grammar below (such as Infinity and NaN)
12997  > are not permitted.
12998 
12999  This description includes both integer and floating-point numbers.
13000  However, C++ allows more precise storage if it is known whether the number
13001  is a signed integer, an unsigned integer or a floating-point number.
13002  Therefore, three different types, @ref number_integer_t, @ref
13003  number_unsigned_t and @ref number_float_t are used.
13004 
13005  To store integer numbers in C++, a type is defined by the template
13006  parameter @a NumberIntegerType which chooses the type to use.
13007 
13008  #### Default type
13009 
13010  With the default values for @a NumberIntegerType (`int64_t`), the default
13011  value for @a number_integer_t is:
13012 
13013  @code {.cpp}
13014  int64_t
13015  @endcode
13016 
13017  #### Default behavior
13018 
13019  - The restrictions about leading zeros is not enforced in C++. Instead,
13020  leading zeros in integer literals lead to an interpretation as octal
13021  number. Internally, the value will be stored as decimal number. For
13022  instance, the C++ integer literal `010` will be serialized to `8`.
13023  During deserialization, leading zeros yield an error.
13024  - Not-a-number (NaN) values will be serialized to `null`.
13025 
13026  #### Limits
13027 
13028  [RFC 7159](http://rfc7159.net/rfc7159) specifies:
13029  > An implementation may set limits on the range and precision of numbers.
13030 
13031  When the default type is used, the maximal integer number that can be
13032  stored is `9223372036854775807` (INT64_MAX) and the minimal integer number
13033  that can be stored is `-9223372036854775808` (INT64_MIN). Integer numbers
13034  that are out of range will yield over/underflow when used in a
13035  constructor. During deserialization, too large or small integer numbers
13036  will be automatically be stored as @ref number_unsigned_t or @ref
13037  number_float_t.
13038 
13039  [RFC 7159](http://rfc7159.net/rfc7159) further states:
13040  > Note that when such software is used, numbers that are integers and are
13041  > in the range \f$[-2^{53}+1, 2^{53}-1]\f$ are interoperable in the sense
13042  > that implementations will agree exactly on their numeric values.
13043 
13044  As this range is a subrange of the exactly supported range [INT64_MIN,
13045  INT64_MAX], this class's integer type is interoperable.
13046 
13047  #### Storage
13048 
13049  Integer number values are stored directly inside a @ref basic_json type.
13050 
13051  @sa @ref number_float_t -- type for number values (floating-point)
13052 
13053  @sa @ref number_unsigned_t -- type for number values (unsigned integer)
13054 
13055  @since version 1.0.0
13056  */
13057  using number_integer_t = NumberIntegerType;
13058 
13059  /*!
13060  @brief a type for a number (unsigned)
13061 
13062  [RFC 7159](http://rfc7159.net/rfc7159) describes numbers as follows:
13063  > The representation of numbers is similar to that used in most
13064  > programming languages. A number is represented in base 10 using decimal
13065  > digits. It contains an integer component that may be prefixed with an
13066  > optional minus sign, which may be followed by a fraction part and/or an
13067  > exponent part. Leading zeros are not allowed. (...) Numeric values that
13068  > cannot be represented in the grammar below (such as Infinity and NaN)
13069  > are not permitted.
13070 
13071  This description includes both integer and floating-point numbers.
13072  However, C++ allows more precise storage if it is known whether the number
13073  is a signed integer, an unsigned integer or a floating-point number.
13074  Therefore, three different types, @ref number_integer_t, @ref
13075  number_unsigned_t and @ref number_float_t are used.
13076 
13077  To store unsigned integer numbers in C++, a type is defined by the
13078  template parameter @a NumberUnsignedType which chooses the type to use.
13079 
13080  #### Default type
13081 
13082  With the default values for @a NumberUnsignedType (`uint64_t`), the
13083  default value for @a number_unsigned_t is:
13084 
13085  @code {.cpp}
13086  uint64_t
13087  @endcode
13088 
13089  #### Default behavior
13090 
13091  - The restrictions about leading zeros is not enforced in C++. Instead,
13092  leading zeros in integer literals lead to an interpretation as octal
13093  number. Internally, the value will be stored as decimal number. For
13094  instance, the C++ integer literal `010` will be serialized to `8`.
13095  During deserialization, leading zeros yield an error.
13096  - Not-a-number (NaN) values will be serialized to `null`.
13097 
13098  #### Limits
13099 
13100  [RFC 7159](http://rfc7159.net/rfc7159) specifies:
13101  > An implementation may set limits on the range and precision of numbers.
13102 
13103  When the default type is used, the maximal integer number that can be
13104  stored is `18446744073709551615` (UINT64_MAX) and the minimal integer
13105  number that can be stored is `0`. Integer numbers that are out of range
13106  will yield over/underflow when used in a constructor. During
13107  deserialization, too large or small integer numbers will be automatically
13108  be stored as @ref number_integer_t or @ref number_float_t.
13109 
13110  [RFC 7159](http://rfc7159.net/rfc7159) further states:
13111  > Note that when such software is used, numbers that are integers and are
13112  > in the range \f$[-2^{53}+1, 2^{53}-1]\f$ are interoperable in the sense
13113  > that implementations will agree exactly on their numeric values.
13114 
13115  As this range is a subrange (when considered in conjunction with the
13116  number_integer_t type) of the exactly supported range [0, UINT64_MAX],
13117  this class's integer type is interoperable.
13118 
13119  #### Storage
13120 
13121  Integer number values are stored directly inside a @ref basic_json type.
13122 
13123  @sa @ref number_float_t -- type for number values (floating-point)
13124  @sa @ref number_integer_t -- type for number values (integer)
13125 
13126  @since version 2.0.0
13127  */
13128  using number_unsigned_t = NumberUnsignedType;
13129 
13130  /*!
13131  @brief a type for a number (floating-point)
13132 
13133  [RFC 7159](http://rfc7159.net/rfc7159) describes numbers as follows:
13134  > The representation of numbers is similar to that used in most
13135  > programming languages. A number is represented in base 10 using decimal
13136  > digits. It contains an integer component that may be prefixed with an
13137  > optional minus sign, which may be followed by a fraction part and/or an
13138  > exponent part. Leading zeros are not allowed. (...) Numeric values that
13139  > cannot be represented in the grammar below (such as Infinity and NaN)
13140  > are not permitted.
13141 
13142  This description includes both integer and floating-point numbers.
13143  However, C++ allows more precise storage if it is known whether the number
13144  is a signed integer, an unsigned integer or a floating-point number.
13145  Therefore, three different types, @ref number_integer_t, @ref
13146  number_unsigned_t and @ref number_float_t are used.
13147 
13148  To store floating-point numbers in C++, a type is defined by the template
13149  parameter @a NumberFloatType which chooses the type to use.
13150 
13151  #### Default type
13152 
13153  With the default values for @a NumberFloatType (`double`), the default
13154  value for @a number_float_t is:
13155 
13156  @code {.cpp}
13157  double
13158  @endcode
13159 
13160  #### Default behavior
13161 
13162  - The restrictions about leading zeros is not enforced in C++. Instead,
13163  leading zeros in floating-point literals will be ignored. Internally,
13164  the value will be stored as decimal number. For instance, the C++
13165  floating-point literal `01.2` will be serialized to `1.2`. During
13166  deserialization, leading zeros yield an error.
13167  - Not-a-number (NaN) values will be serialized to `null`.
13168 
13169  #### Limits
13170 
13171  [RFC 7159](http://rfc7159.net/rfc7159) states:
13172  > This specification allows implementations to set limits on the range and
13173  > precision of numbers accepted. Since software that implements IEEE
13174  > 754-2008 binary64 (double precision) numbers is generally available and
13175  > widely used, good interoperability can be achieved by implementations
13176  > that expect no more precision or range than these provide, in the sense
13177  > that implementations will approximate JSON numbers within the expected
13178  > precision.
13179 
13180  This implementation does exactly follow this approach, as it uses double
13181  precision floating-point numbers. Note values smaller than
13182  `-1.79769313486232e+308` and values greater than `1.79769313486232e+308`
13183  will be stored as NaN internally and be serialized to `null`.
13184 
13185  #### Storage
13186 
13187  Floating-point number values are stored directly inside a @ref basic_json
13188  type.
13189 
13190  @sa @ref number_integer_t -- type for number values (integer)
13191 
13192  @sa @ref number_unsigned_t -- type for number values (unsigned integer)
13193 
13194  @since version 1.0.0
13195  */
13196  using number_float_t = NumberFloatType;
13197 
13198  /// @}
13199 
13200  private:
13202  /// helper for exception-safe object creation
13203  template<typename T, typename... Args>
13204  static T* create(Args&& ... args)
13205  {
13206  AllocatorType<T> alloc;
13207  using AllocatorTraits = std::allocator_traits<AllocatorType<T>>;
13208 
13209  auto deleter = [&](T * object)
13210  {
13211  AllocatorTraits::deallocate(alloc, object, 1);
13212  };
13213  std::unique_ptr<T, decltype(deleter)> object(AllocatorTraits::allocate(alloc, 1), deleter);
13214  AllocatorTraits::construct(alloc, object.get(), std::forward<Args>(args)...);
13215  assert(object != nullptr);
13216  return object.release();
13217  }
13218 
13219  ////////////////////////
13220  // JSON value storage //
13221  ////////////////////////
13222 
13223  /*!
13224  @brief a JSON value
13225 
13226  The actual storage for a JSON value of the @ref basic_json class. This
13227  union combines the different storage types for the JSON value types
13228  defined in @ref value_t.
13229 
13230  JSON type | value_t type | used type
13231  --------- | --------------- | ------------------------
13232  object | object | pointer to @ref object_t
13233  array | array | pointer to @ref array_t
13234  string | string | pointer to @ref string_t
13235  boolean | boolean | @ref boolean_t
13236  number | number_integer | @ref number_integer_t
13237  number | number_unsigned | @ref number_unsigned_t
13238  number | number_float | @ref number_float_t
13239  null | null | *no value is stored*
13240 
13241  @note Variable-length types (objects, arrays, and strings) are stored as
13242  pointers. The size of the union should not exceed 64 bits if the default
13243  value types are used.
13244 
13245  @since version 1.0.0
13246  */
13247  union json_value
13248  {
13249  /// object (stored with pointer to save storage)
13250  object_t* object;
13251  /// array (stored with pointer to save storage)
13252  array_t* array;
13253  /// string (stored with pointer to save storage)
13254  string_t* string;
13255  /// boolean
13256  boolean_t boolean;
13257  /// number (integer)
13258  number_integer_t number_integer;
13259  /// number (unsigned integer)
13260  number_unsigned_t number_unsigned;
13261  /// number (floating-point)
13262  number_float_t number_float;
13263 
13264  /// default constructor (for null values)
13265  json_value() = default;
13266  /// constructor for booleans
13267  json_value(boolean_t v) noexcept : boolean(v) {}
13268  /// constructor for numbers (integer)
13269  json_value(number_integer_t v) noexcept : number_integer(v) {}
13270  /// constructor for numbers (unsigned)
13271  json_value(number_unsigned_t v) noexcept : number_unsigned(v) {}
13272  /// constructor for numbers (floating-point)
13273  json_value(number_float_t v) noexcept : number_float(v) {}
13274  /// constructor for empty values of a given type
13275  json_value(value_t t)
13276  {
13277  switch (t)
13278  {
13279  case value_t::object:
13280  {
13281  object = create<object_t>();
13282  break;
13283  }
13284 
13285  case value_t::array:
13286  {
13287  array = create<array_t>();
13288  break;
13289  }
13290 
13291  case value_t::string:
13292  {
13293  string = create<string_t>("");
13294  break;
13295  }
13296 
13297  case value_t::boolean:
13298  {
13299  boolean = boolean_t(false);
13300  break;
13301  }
13302 
13303  case value_t::number_integer:
13304  {
13305  number_integer = number_integer_t(0);
13306  break;
13307  }
13308 
13309  case value_t::number_unsigned:
13310  {
13311  number_unsigned = number_unsigned_t(0);
13312  break;
13313  }
13314 
13315  case value_t::number_float:
13316  {
13317  number_float = number_float_t(0.0);
13318  break;
13319  }
13320 
13321  case value_t::null:
13322  {
13323  object = nullptr; // silence warning, see #821
13324  break;
13325  }
13326 
13327  default:
13328  {
13329  object = nullptr; // silence warning, see #821
13330  if (JSON_UNLIKELY(t == value_t::null))
13331  {
13332  JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.5.0")); // LCOV_EXCL_LINE
13333  }
13334  break;
13335  }
13336  }
13337  }
13338 
13339  /// constructor for strings
13340  json_value(const string_t& value)
13341  {
13342  string = create<string_t>(value);
13343  }
13344 
13345  /// constructor for rvalue strings
13346  json_value(string_t&& value)
13347  {
13348  string = create<string_t>(std::move(value));
13349  }
13350 
13351  /// constructor for objects
13352  json_value(const object_t& value)
13353  {
13354  object = create<object_t>(value);
13355  }
13356 
13357  /// constructor for rvalue objects
13358  json_value(object_t&& value)
13359  {
13360  object = create<object_t>(std::move(value));
13361  }
13362 
13363  /// constructor for arrays
13364  json_value(const array_t& value)
13365  {
13366  array = create<array_t>(value);
13367  }
13368 
13369  /// constructor for rvalue arrays
13370  json_value(array_t&& value)
13371  {
13372  array = create<array_t>(std::move(value));
13373  }
13374 
13375  void destroy(value_t t) noexcept
13376  {
13377  switch (t)
13378  {
13379  case value_t::object:
13380  {
13381  AllocatorType<object_t> alloc;
13382  std::allocator_traits<decltype(alloc)>::destroy(alloc, object);
13383  std::allocator_traits<decltype(alloc)>::deallocate(alloc, object, 1);
13384  break;
13385  }
13386 
13387  case value_t::array:
13388  {
13389  AllocatorType<array_t> alloc;
13390  std::allocator_traits<decltype(alloc)>::destroy(alloc, array);
13391  std::allocator_traits<decltype(alloc)>::deallocate(alloc, array, 1);
13392  break;
13393  }
13394 
13395  case value_t::string:
13396  {
13397  AllocatorType<string_t> alloc;
13398  std::allocator_traits<decltype(alloc)>::destroy(alloc, string);
13399  std::allocator_traits<decltype(alloc)>::deallocate(alloc, string, 1);
13400  break;
13401  }
13402 
13403  default:
13404  {
13405  break;
13406  }
13407  }
13408  }
13409  };
13410 
13411  /*!
13412  @brief checks the class invariants
13413 
13414  This function asserts the class invariants. It needs to be called at the
13415  end of every constructor to make sure that created objects respect the
13416  invariant. Furthermore, it has to be called each time the type of a JSON
13417  value is changed, because the invariant expresses a relationship between
13418  @a m_type and @a m_value.
13419  */
13420  void assert_invariant() const noexcept
13421  {
13422  assert(m_type != value_t::object or m_value.object != nullptr);
13423  assert(m_type != value_t::array or m_value.array != nullptr);
13424  assert(m_type != value_t::string or m_value.string != nullptr);
13425  }
13426 
13427  public:
13428  //////////////////////////
13429  // JSON parser callback //
13430  //////////////////////////
13431 
13432  /*!
13433  @brief parser event types
13434 
13435  The parser callback distinguishes the following events:
13436  - `object_start`: the parser read `{` and started to process a JSON object
13437  - `key`: the parser read a key of a value in an object
13438  - `object_end`: the parser read `}` and finished processing a JSON object
13439  - `array_start`: the parser read `[` and started to process a JSON array
13440  - `array_end`: the parser read `]` and finished processing a JSON array
13441  - `value`: the parser finished reading a JSON value
13442 
13443  @image html callback_events.png "Example when certain parse events are triggered"
13444 
13445  @sa @ref parser_callback_t for more information and examples
13446  */
13447  using parse_event_t = typename parser::parse_event_t;
13448 
13449  /*!
13450  @brief per-element parser callback type
13451 
13452  With a parser callback function, the result of parsing a JSON text can be
13453  influenced. When passed to @ref parse, it is called on certain events
13454  (passed as @ref parse_event_t via parameter @a event) with a set recursion
13455  depth @a depth and context JSON value @a parsed. The return value of the
13456  callback function is a boolean indicating whether the element that emitted
13457  the callback shall be kept or not.
13458 
13459  We distinguish six scenarios (determined by the event type) in which the
13460  callback function can be called. The following table describes the values
13461  of the parameters @a depth, @a event, and @a parsed.
13462 
13463  parameter @a event | description | parameter @a depth | parameter @a parsed
13464  ------------------ | ----------- | ------------------ | -------------------
13465  parse_event_t::object_start | the parser read `{` and started to process a JSON object | depth of the parent of the JSON object | a JSON value with type discarded
13466  parse_event_t::key | the parser read a key of a value in an object | depth of the currently parsed JSON object | a JSON string containing the key
13467  parse_event_t::object_end | the parser read `}` and finished processing a JSON object | depth of the parent of the JSON object | the parsed JSON object
13468  parse_event_t::array_start | the parser read `[` and started to process a JSON array | depth of the parent of the JSON array | a JSON value with type discarded
13469  parse_event_t::array_end | the parser read `]` and finished processing a JSON array | depth of the parent of the JSON array | the parsed JSON array
13470  parse_event_t::value | the parser finished reading a JSON value | depth of the value | the parsed JSON value
13471 
13472  @image html callback_events.png "Example when certain parse events are triggered"
13473 
13474  Discarding a value (i.e., returning `false`) has different effects
13475  depending on the context in which function was called:
13476 
13477  - Discarded values in structured types are skipped. That is, the parser
13478  will behave as if the discarded value was never read.
13479  - In case a value outside a structured type is skipped, it is replaced
13480  with `null`. This case happens if the top-level element is skipped.
13481 
13482  @param[in] depth the depth of the recursion during parsing
13483 
13484  @param[in] event an event of type parse_event_t indicating the context in
13485  the callback function has been called
13486 
13487  @param[in,out] parsed the current intermediate parse result; note that
13488  writing to this value has no effect for parse_event_t::key events
13489 
13490  @return Whether the JSON value which called the function during parsing
13491  should be kept (`true`) or not (`false`). In the latter case, it is either
13492  skipped completely or replaced by an empty discarded object.
13493 
13494  @sa @ref parse for examples
13495 
13496  @since version 1.0.0
13497  */
13498  using parser_callback_t = typename parser::parser_callback_t;
13499 
13500  //////////////////
13501  // constructors //
13502  //////////////////
13504  /// @name constructors and destructors
13505  /// Constructors of class @ref basic_json, copy/move constructor, copy
13506  /// assignment, static functions creating objects, and the destructor.
13507  /// @{
13508 
13509  /*!
13510  @brief create an empty value with a given type
13511 
13512  Create an empty JSON value with a given type. The value will be default
13513  initialized with an empty value which depends on the type:
13514 
13515  Value type | initial value
13516  ----------- | -------------
13517  null | `null`
13518  boolean | `false`
13519  string | `""`
13520  number | `0`
13521  object | `{}`
13522  array | `[]`
13523 
13524  @param[in] v the type of the value to create
13525 
13526  @complexity Constant.
13527 
13528  @exceptionsafety Strong guarantee: if an exception is thrown, there are no
13529  changes to any JSON value.
13530 
13531  @liveexample{The following code shows the constructor for different @ref
13532  value_t values,basic_json__value_t}
13533 
13534  @sa @ref clear() -- restores the postcondition of this constructor
13535 
13536  @since version 1.0.0
13537  */
13538  basic_json(const value_t v)
13539  : m_type(v), m_value(v)
13540  {
13541  assert_invariant();
13542  }
13544  /*!
13545  @brief create a null object
13546 
13547  Create a `null` JSON value. It either takes a null pointer as parameter
13548  (explicitly creating `null`) or no parameter (implicitly creating `null`).
13549  The passed null pointer itself is not read -- it is only used to choose
13550  the right constructor.
13551 
13552  @complexity Constant.
13553 
13554  @exceptionsafety No-throw guarantee: this constructor never throws
13555  exceptions.
13556 
13557  @liveexample{The following code shows the constructor with and without a
13558  null pointer parameter.,basic_json__nullptr_t}
13559 
13560  @since version 1.0.0
13561  */
13562  basic_json(std::nullptr_t = nullptr) noexcept
13563  : basic_json(value_t::null)
13564  {
13565  assert_invariant();
13566  }
13568  /*!
13569  @brief create a JSON value
13570 
13571  This is a "catch all" constructor for all compatible JSON types; that is,
13572  types for which a `to_json()` method exists. The constructor forwards the
13573  parameter @a val to that method (to `json_serializer<U>::to_json` method
13574  with `U = uncvref_t<CompatibleType>`, to be exact).
13575 
13576  Template type @a CompatibleType includes, but is not limited to, the
13577  following types:
13578  - **arrays**: @ref array_t and all kinds of compatible containers such as
13579  `std::vector`, `std::deque`, `std::list`, `std::forward_list`,
13580  `std::array`, `std::valarray`, `std::set`, `std::unordered_set`,
13581  `std::multiset`, and `std::unordered_multiset` with a `value_type` from
13582  which a @ref basic_json value can be constructed.
13583  - **objects**: @ref object_t and all kinds of compatible associative
13584  containers such as `std::map`, `std::unordered_map`, `std::multimap`,
13585  and `std::unordered_multimap` with a `key_type` compatible to
13586  @ref string_t and a `value_type` from which a @ref basic_json value can
13587  be constructed.
13588  - **strings**: @ref string_t, string literals, and all compatible string
13589  containers can be used.
13590  - **numbers**: @ref number_integer_t, @ref number_unsigned_t,
13591  @ref number_float_t, and all convertible number types such as `int`,
13592  `size_t`, `int64_t`, `float` or `double` can be used.
13593  - **boolean**: @ref boolean_t / `bool` can be used.
13594 
13595  See the examples below.
13596 
13597  @tparam CompatibleType a type such that:
13598  - @a CompatibleType is not derived from `std::istream`,
13599  - @a CompatibleType is not @ref basic_json (to avoid hijacking copy/move
13600  constructors),
13601  - @a CompatibleType is not a different @ref basic_json type (i.e. with different template arguments)
13602  - @a CompatibleType is not a @ref basic_json nested type (e.g.,
13603  @ref json_pointer, @ref iterator, etc ...)
13604  - @ref @ref json_serializer<U> has a
13605  `to_json(basic_json_t&, CompatibleType&&)` method
13606 
13607  @tparam U = `uncvref_t<CompatibleType>`
13608 
13609  @param[in] val the value to be forwarded to the respective constructor
13610 
13611  @complexity Usually linear in the size of the passed @a val, also
13612  depending on the implementation of the called `to_json()`
13613  method.
13614 
13615  @exceptionsafety Depends on the called constructor. For types directly
13616  supported by the library (i.e., all types for which no `to_json()` function
13617  was provided), strong guarantee holds: if an exception is thrown, there are
13618  no changes to any JSON value.
13619 
13620  @liveexample{The following code shows the constructor with several
13621  compatible types.,basic_json__CompatibleType}
13622 
13623  @since version 2.1.0
13624  */
13625  template <typename CompatibleType,
13626  typename U = detail::uncvref_t<CompatibleType>,
13627  detail::enable_if_t<
13628  not detail::is_basic_json<U>::value and detail::is_compatible_type<basic_json_t, U>::value, int> = 0>
13629  basic_json(CompatibleType && val) noexcept(noexcept(
13630  JSONSerializer<U>::to_json(std::declval<basic_json_t&>(),
13631  std::forward<CompatibleType>(val))))
13632  {
13633  JSONSerializer<U>::to_json(*this, std::forward<CompatibleType>(val));
13634  assert_invariant();
13635  }
13636 
13637  /*!
13638  @brief create a JSON value from an existing one
13639 
13640  This is a constructor for existing @ref basic_json types.
13641  It does not hijack copy/move constructors, since the parameter has different
13642  template arguments than the current ones.
13643 
13644  The constructor tries to convert the internal @ref m_value of the parameter.
13645 
13646  @tparam BasicJsonType a type such that:
13647  - @a BasicJsonType is a @ref basic_json type.
13648  - @a BasicJsonType has different template arguments than @ref basic_json_t.
13649 
13650  @param[in] val the @ref basic_json value to be converted.
13651 
13652  @complexity Usually linear in the size of the passed @a val, also
13653  depending on the implementation of the called `to_json()`
13654  method.
13655 
13656  @exceptionsafety Depends on the called constructor. For types directly
13657  supported by the library (i.e., all types for which no `to_json()` function
13658  was provided), strong guarantee holds: if an exception is thrown, there are
13659  no changes to any JSON value.
13660 
13661  @since version 3.2.0
13662  */
13663  template <typename BasicJsonType,
13664  detail::enable_if_t<
13665  detail::is_basic_json<BasicJsonType>::value and not std::is_same<basic_json, BasicJsonType>::value, int> = 0>
13666  basic_json(const BasicJsonType& val)
13667  {
13668  using other_boolean_t = typename BasicJsonType::boolean_t;
13669  using other_number_float_t = typename BasicJsonType::number_float_t;
13670  using other_number_integer_t = typename BasicJsonType::number_integer_t;
13671  using other_number_unsigned_t = typename BasicJsonType::number_unsigned_t;
13672  using other_string_t = typename BasicJsonType::string_t;
13673  using other_object_t = typename BasicJsonType::object_t;
13674  using other_array_t = typename BasicJsonType::array_t;
13675 
13676  switch (val.type())
13677  {
13678  case value_t::boolean:
13679  JSONSerializer<other_boolean_t>::to_json(*this, val.template get<other_boolean_t>());
13680  break;
13681  case value_t::number_float:
13682  JSONSerializer<other_number_float_t>::to_json(*this, val.template get<other_number_float_t>());
13683  break;
13684  case value_t::number_integer:
13685  JSONSerializer<other_number_integer_t>::to_json(*this, val.template get<other_number_integer_t>());
13686  break;
13687  case value_t::number_unsigned:
13688  JSONSerializer<other_number_unsigned_t>::to_json(*this, val.template get<other_number_unsigned_t>());
13689  break;
13690  case value_t::string:
13691  JSONSerializer<other_string_t>::to_json(*this, val.template get_ref<const other_string_t&>());
13692  break;
13693  case value_t::object:
13694  JSONSerializer<other_object_t>::to_json(*this, val.template get_ref<const other_object_t&>());
13695  break;
13696  case value_t::array:
13697  JSONSerializer<other_array_t>::to_json(*this, val.template get_ref<const other_array_t&>());
13698  break;
13699  case value_t::null:
13700  *this = nullptr;
13701  break;
13702  case value_t::discarded:
13703  m_type = value_t::discarded;
13704  break;
13705  }
13706  assert_invariant();
13707  }
13708 
13709  /*!
13710  @brief create a container (array or object) from an initializer list
13711 
13712  Creates a JSON value of type array or object from the passed initializer
13713  list @a init. In case @a type_deduction is `true` (default), the type of
13714  the JSON value to be created is deducted from the initializer list @a init
13715  according to the following rules:
13716 
13717  1. If the list is empty, an empty JSON object value `{}` is created.
13718  2. If the list consists of pairs whose first element is a string, a JSON
13719  object value is created where the first elements of the pairs are
13720  treated as keys and the second elements are as values.
13721  3. In all other cases, an array is created.
13722 
13723  The rules aim to create the best fit between a C++ initializer list and
13724  JSON values. The rationale is as follows:
13725 
13726  1. The empty initializer list is written as `{}` which is exactly an empty
13727  JSON object.
13728  2. C++ has no way of describing mapped types other than to list a list of
13729  pairs. As JSON requires that keys must be of type string, rule 2 is the
13730  weakest constraint one can pose on initializer lists to interpret them
13731  as an object.
13732  3. In all other cases, the initializer list could not be interpreted as
13733  JSON object type, so interpreting it as JSON array type is safe.
13734 
13735  With the rules described above, the following JSON values cannot be
13736  expressed by an initializer list:
13737 
13738  - the empty array (`[]`): use @ref array(initializer_list_t)
13739  with an empty initializer list in this case
13740  - arrays whose elements satisfy rule 2: use @ref
13741  array(initializer_list_t) with the same initializer list
13742  in this case
13743 
13744  @note When used without parentheses around an empty initializer list, @ref
13745  basic_json() is called instead of this function, yielding the JSON null
13746  value.
13747 
13748  @param[in] init initializer list with JSON values
13749 
13750  @param[in] type_deduction internal parameter; when set to `true`, the type
13751  of the JSON value is deducted from the initializer list @a init; when set
13752  to `false`, the type provided via @a manual_type is forced. This mode is
13753  used by the functions @ref array(initializer_list_t) and
13754  @ref object(initializer_list_t).
13755 
13756  @param[in] manual_type internal parameter; when @a type_deduction is set
13757  to `false`, the created JSON value will use the provided type (only @ref
13758  value_t::array and @ref value_t::object are valid); when @a type_deduction
13759  is set to `true`, this parameter has no effect
13760 
13761  @throw type_error.301 if @a type_deduction is `false`, @a manual_type is
13762  `value_t::object`, but @a init contains an element which is not a pair
13763  whose first element is a string. In this case, the constructor could not
13764  create an object. If @a type_deduction would have be `true`, an array
13765  would have been created. See @ref object(initializer_list_t)
13766  for an example.
13767 
13768  @complexity Linear in the size of the initializer list @a init.
13769 
13770  @exceptionsafety Strong guarantee: if an exception is thrown, there are no
13771  changes to any JSON value.
13772 
13773  @liveexample{The example below shows how JSON values are created from
13774  initializer lists.,basic_json__list_init_t}
13775 
13776  @sa @ref array(initializer_list_t) -- create a JSON array
13777  value from an initializer list
13778  @sa @ref object(initializer_list_t) -- create a JSON object
13779  value from an initializer list
13780 
13781  @since version 1.0.0
13782  */
13783  basic_json(initializer_list_t init,
13784  bool type_deduction = true,
13785  value_t manual_type = value_t::array)
13786  {
13787  // check if each element is an array with two elements whose first
13788  // element is a string
13789  bool is_an_object = std::all_of(init.begin(), init.end(),
13790  [](const detail::json_ref<basic_json>& element_ref)
13791  {
13792  return (element_ref->is_array() and element_ref->size() == 2 and (*element_ref)[0].is_string());
13793  });
13794 
13795  // adjust type if type deduction is not wanted
13796  if (not type_deduction)
13797  {
13798  // if array is wanted, do not create an object though possible
13799  if (manual_type == value_t::array)
13800  {
13801  is_an_object = false;
13802  }
13803 
13804  // if object is wanted but impossible, throw an exception
13805  if (JSON_UNLIKELY(manual_type == value_t::object and not is_an_object))
13806  {
13807  JSON_THROW(type_error::create(301, "cannot create object from initializer list"));
13808  }
13809  }
13810 
13811  if (is_an_object)
13812  {
13813  // the initializer list is a list of pairs -> create object
13814  m_type = value_t::object;
13815  m_value = value_t::object;
13816 
13817  std::for_each(init.begin(), init.end(), [this](const detail::json_ref<basic_json>& element_ref)
13818  {
13819  auto element = element_ref.moved_or_copied();
13820  m_value.object->emplace(
13821  std::move(*((*element.m_value.array)[0].m_value.string)),
13822  std::move((*element.m_value.array)[1]));
13823  });
13824  }
13825  else
13826  {
13827  // the initializer list describes an array -> create array
13828  m_type = value_t::array;
13829  m_value.array = create<array_t>(init.begin(), init.end());
13830  }
13831 
13832  assert_invariant();
13833  }
13834 
13835  /*!
13836  @brief explicitly create an array from an initializer list
13837 
13838  Creates a JSON array value from a given initializer list. That is, given a
13839  list of values `a, b, c`, creates the JSON value `[a, b, c]`. If the
13840  initializer list is empty, the empty array `[]` is created.
13841 
13842  @note This function is only needed to express two edge cases that cannot
13843  be realized with the initializer list constructor (@ref
13844  basic_json(initializer_list_t, bool, value_t)). These cases
13845  are:
13846  1. creating an array whose elements are all pairs whose first element is a
13847  string -- in this case, the initializer list constructor would create an
13848  object, taking the first elements as keys
13849  2. creating an empty array -- passing the empty initializer list to the
13850  initializer list constructor yields an empty object
13851 
13852  @param[in] init initializer list with JSON values to create an array from
13853  (optional)
13854 
13855  @return JSON array value
13856 
13857  @complexity Linear in the size of @a init.
13858 
13859  @exceptionsafety Strong guarantee: if an exception is thrown, there are no
13860  changes to any JSON value.
13861 
13862  @liveexample{The following code shows an example for the `array`
13863  function.,array}
13864 
13865  @sa @ref basic_json(initializer_list_t, bool, value_t) --
13866  create a JSON value from an initializer list
13867  @sa @ref object(initializer_list_t) -- create a JSON object
13868  value from an initializer list
13869 
13870  @since version 1.0.0
13871  */
13872  static basic_json array(initializer_list_t init = {})
13873  {
13874  return basic_json(init, false, value_t::array);
13875  }
13876 
13877  /*!
13878  @brief explicitly create an object from an initializer list
13879 
13880  Creates a JSON object value from a given initializer list. The initializer
13881  lists elements must be pairs, and their first elements must be strings. If
13882  the initializer list is empty, the empty object `{}` is created.
13883 
13884  @note This function is only added for symmetry reasons. In contrast to the
13885  related function @ref array(initializer_list_t), there are
13886  no cases which can only be expressed by this function. That is, any
13887  initializer list @a init can also be passed to the initializer list
13888  constructor @ref basic_json(initializer_list_t, bool, value_t).
13889 
13890  @param[in] init initializer list to create an object from (optional)
13891 
13892  @return JSON object value
13893 
13894  @throw type_error.301 if @a init is not a list of pairs whose first
13895  elements are strings. In this case, no object can be created. When such a
13896  value is passed to @ref basic_json(initializer_list_t, bool, value_t),
13897  an array would have been created from the passed initializer list @a init.
13898  See example below.
13899 
13900  @complexity Linear in the size of @a init.
13901 
13902  @exceptionsafety Strong guarantee: if an exception is thrown, there are no
13903  changes to any JSON value.
13904 
13905  @liveexample{The following code shows an example for the `object`
13906  function.,object}
13907 
13908  @sa @ref basic_json(initializer_list_t, bool, value_t) --
13909  create a JSON value from an initializer list
13910  @sa @ref array(initializer_list_t) -- create a JSON array
13911  value from an initializer list
13912 
13913  @since version 1.0.0
13914  */
13915  static basic_json object(initializer_list_t init = {})
13916  {
13917  return basic_json(init, false, value_t::object);
13918  }
13919 
13920  /*!
13921  @brief construct an array with count copies of given value
13922 
13923  Constructs a JSON array value by creating @a cnt copies of a passed value.
13924  In case @a cnt is `0`, an empty array is created.
13925 
13926  @param[in] cnt the number of JSON copies of @a val to create
13927  @param[in] val the JSON value to copy
13928 
13929  @post `std::distance(begin(),end()) == cnt` holds.
13930 
13931  @complexity Linear in @a cnt.
13932 
13933  @exceptionsafety Strong guarantee: if an exception is thrown, there are no
13934  changes to any JSON value.
13935 
13936  @liveexample{The following code shows examples for the @ref
13937  basic_json(size_type\, const basic_json&)
13938  constructor.,basic_json__size_type_basic_json}
13939 
13940  @since version 1.0.0
13941  */
13942  basic_json(size_type cnt, const basic_json& val)
13943  : m_type(value_t::array)
13944  {
13945  m_value.array = create<array_t>(cnt, val);
13946  assert_invariant();
13947  }
13948 
13949  /*!
13950  @brief construct a JSON container given an iterator range
13951 
13952  Constructs the JSON value with the contents of the range `[first, last)`.
13953  The semantics depends on the different types a JSON value can have:
13954  - In case of a null type, invalid_iterator.206 is thrown.
13955  - In case of other primitive types (number, boolean, or string), @a first
13956  must be `begin()` and @a last must be `end()`. In this case, the value is
13957  copied. Otherwise, invalid_iterator.204 is thrown.
13958  - In case of structured types (array, object), the constructor behaves as
13959  similar versions for `std::vector` or `std::map`; that is, a JSON array
13960  or object is constructed from the values in the range.
13961 
13962  @tparam InputIT an input iterator type (@ref iterator or @ref
13963  const_iterator)
13964 
13965  @param[in] first begin of the range to copy from (included)
13966  @param[in] last end of the range to copy from (excluded)
13967 
13968  @pre Iterators @a first and @a last must be initialized. **This
13969  precondition is enforced with an assertion (see warning).** If
13970  assertions are switched off, a violation of this precondition yields
13971  undefined behavior.
13972 
13973  @pre Range `[first, last)` is valid. Usually, this precondition cannot be
13974  checked efficiently. Only certain edge cases are detected; see the
13975  description of the exceptions below. A violation of this precondition
13976  yields undefined behavior.
13977 
13978  @warning A precondition is enforced with a runtime assertion that will
13979  result in calling `std::abort` if this precondition is not met.
13980  Assertions can be disabled by defining `NDEBUG` at compile time.
13981  See https://en.cppreference.com/w/cpp/error/assert for more
13982  information.
13983 
13984  @throw invalid_iterator.201 if iterators @a first and @a last are not
13985  compatible (i.e., do not belong to the same JSON value). In this case,
13986  the range `[first, last)` is undefined.
13987  @throw invalid_iterator.204 if iterators @a first and @a last belong to a
13988  primitive type (number, boolean, or string), but @a first does not point
13989  to the first element any more. In this case, the range `[first, last)` is
13990  undefined. See example code below.
13991  @throw invalid_iterator.206 if iterators @a first and @a last belong to a
13992  null value. In this case, the range `[first, last)` is undefined.
13993 
13994  @complexity Linear in distance between @a first and @a last.
13995 
13996  @exceptionsafety Strong guarantee: if an exception is thrown, there are no
13997  changes to any JSON value.
13998 
13999  @liveexample{The example below shows several ways to create JSON values by
14000  specifying a subrange with iterators.,basic_json__InputIt_InputIt}
14001 
14002  @since version 1.0.0
14003  */
14004  template<class InputIT, typename std::enable_if<
14005  std::is_same<InputIT, typename basic_json_t::iterator>::value or
14006  std::is_same<InputIT, typename basic_json_t::const_iterator>::value, int>::type = 0>
14007  basic_json(InputIT first, InputIT last)
14008  {
14009  assert(first.m_object != nullptr);
14010  assert(last.m_object != nullptr);
14011 
14012  // make sure iterator fits the current value
14013  if (JSON_UNLIKELY(first.m_object != last.m_object))
14014  {
14015  JSON_THROW(invalid_iterator::create(201, "iterators are not compatible"));
14016  }
14017 
14018  // copy type from first iterator
14019  m_type = first.m_object->m_type;
14020 
14021  // check if iterator range is complete for primitive values
14022  switch (m_type)
14023  {
14024  case value_t::boolean:
14025  case value_t::number_float:
14026  case value_t::number_integer:
14027  case value_t::number_unsigned:
14028  case value_t::string:
14029  {
14030  if (JSON_UNLIKELY(not first.m_it.primitive_iterator.is_begin()
14031  or not last.m_it.primitive_iterator.is_end()))
14032  {
14033  JSON_THROW(invalid_iterator::create(204, "iterators out of range"));
14034  }
14035  break;
14036  }
14037 
14038  default:
14039  break;
14040  }
14041 
14042  switch (m_type)
14043  {
14044  case value_t::number_integer:
14045  {
14046  m_value.number_integer = first.m_object->m_value.number_integer;
14047  break;
14048  }
14049 
14050  case value_t::number_unsigned:
14051  {
14052  m_value.number_unsigned = first.m_object->m_value.number_unsigned;
14053  break;
14054  }
14055 
14056  case value_t::number_float:
14057  {
14058  m_value.number_float = first.m_object->m_value.number_float;
14059  break;
14060  }
14061 
14062  case value_t::boolean:
14063  {
14064  m_value.boolean = first.m_object->m_value.boolean;
14065  break;
14066  }
14067 
14068  case value_t::string:
14069  {
14070  m_value = *first.m_object->m_value.string;
14071  break;
14072  }
14073 
14074  case value_t::object:
14075  {
14076  m_value.object = create<object_t>(first.m_it.object_iterator,
14077  last.m_it.object_iterator);
14078  break;
14079  }
14080 
14081  case value_t::array:
14082  {
14083  m_value.array = create<array_t>(first.m_it.array_iterator,
14084  last.m_it.array_iterator);
14085  break;
14086  }
14087 
14088  default:
14089  JSON_THROW(invalid_iterator::create(206, "cannot construct with iterators from " +
14090  std::string(first.m_object->type_name())));
14091  }
14092 
14093  assert_invariant();
14094  }
14095 
14096 
14097  ///////////////////////////////////////
14098  // other constructors and destructor //
14099  ///////////////////////////////////////
14100 
14101  /// @private
14102  basic_json(const detail::json_ref<basic_json>& ref)
14103  : basic_json(ref.moved_or_copied())
14104  {}
14105 
14106  /*!
14107  @brief copy constructor
14108 
14109  Creates a copy of a given JSON value.
14110 
14111  @param[in] other the JSON value to copy
14112 
14113  @post `*this == other`
14114 
14115  @complexity Linear in the size of @a other.
14116 
14117  @exceptionsafety Strong guarantee: if an exception is thrown, there are no
14118  changes to any JSON value.
14119 
14120  @requirement This function helps `basic_json` satisfying the
14121  [Container](https://en.cppreference.com/w/cpp/named_req/Container)
14122  requirements:
14123  - The complexity is linear.
14124  - As postcondition, it holds: `other == basic_json(other)`.
14125 
14126  @liveexample{The following code shows an example for the copy
14127  constructor.,basic_json__basic_json}
14128 
14129  @since version 1.0.0
14130  */
14131  basic_json(const basic_json& other)
14132  : m_type(other.m_type)
14133  {
14134  // check of passed value is valid
14135  other.assert_invariant();
14137  switch (m_type)
14138  {
14139  case value_t::object:
14140  {
14141  m_value = *other.m_value.object;
14142  break;
14143  }
14144 
14145  case value_t::array:
14146  {
14147  m_value = *other.m_value.array;
14148  break;
14149  }
14150 
14151  case value_t::string:
14152  {
14153  m_value = *other.m_value.string;
14154  break;
14155  }
14156 
14157  case value_t::boolean:
14158  {
14159  m_value = other.m_value.boolean;
14160  break;
14161  }
14162 
14163  case value_t::number_integer:
14164  {
14165  m_value = other.m_value.number_integer;
14166  break;
14167  }
14168 
14169  case value_t::number_unsigned:
14170  {
14171  m_value = other.m_value.number_unsigned;
14172  break;
14173  }
14174 
14175  case value_t::number_float:
14176  {
14177  m_value = other.m_value.number_float;
14178  break;
14179  }
14180 
14181  default:
14182  break;
14183  }
14184 
14185  assert_invariant();
14186  }
14187 
14188  /*!
14189  @brief move constructor
14190 
14191  Move constructor. Constructs a JSON value with the contents of the given
14192  value @a other using move semantics. It "steals" the resources from @a
14193  other and leaves it as JSON null value.
14194 
14195  @param[in,out] other value to move to this object
14196 
14197  @post `*this` has the same value as @a other before the call.
14198  @post @a other is a JSON null value.
14199 
14200  @complexity Constant.
14201 
14202  @exceptionsafety No-throw guarantee: this constructor never throws
14203  exceptions.
14204 
14205  @requirement This function helps `basic_json` satisfying the
14206  [MoveConstructible](https://en.cppreference.com/w/cpp/named_req/MoveConstructible)
14207  requirements.
14208 
14209  @liveexample{The code below shows the move constructor explicitly called
14210  via std::move.,basic_json__moveconstructor}
14211 
14212  @since version 1.0.0
14213  */
14214  basic_json(basic_json&& other) noexcept
14215  : m_type(std::move(other.m_type)),
14216  m_value(std::move(other.m_value))
14217  {
14218  // check that passed value is valid
14220 
14221  // invalidate payload
14222  other.m_type = value_t::null;
14223  other.m_value = {};
14224 
14225  assert_invariant();
14226  }
14227 
14228  /*!
14229  @brief copy assignment
14230 
14231  Copy assignment operator. Copies a JSON value via the "copy and swap"
14232  strategy: It is expressed in terms of the copy constructor, destructor,
14233  and the `swap()` member function.
14234 
14235  @param[in] other value to copy from
14236 
14237  @complexity Linear.
14238 
14239  @requirement This function helps `basic_json` satisfying the
14240  [Container](https://en.cppreference.com/w/cpp/named_req/Container)
14241  requirements:
14242  - The complexity is linear.
14243 
14244  @liveexample{The code below shows and example for the copy assignment. It
14245  creates a copy of value `a` which is then swapped with `b`. Finally\, the
14246  copy of `a` (which is the null value after the swap) is
14247  destroyed.,basic_json__copyassignment}
14248 
14249  @since version 1.0.0
14250  */
14251  basic_json& operator=(basic_json other) noexcept (
14256  )
14257  {
14258  // check that passed value is valid
14260 
14261  using std::swap;
14262  swap(m_type, other.m_type);
14264 
14265  assert_invariant();
14266  return *this;
14267  }
14268 
14269  /*!
14270  @brief destructor
14271 
14272  Destroys the JSON value and frees all allocated memory.
14273 
14274  @complexity Linear.
14275 
14276  @requirement This function helps `basic_json` satisfying the
14277  [Container](https://en.cppreference.com/w/cpp/named_req/Container)
14278  requirements:
14279  - The complexity is linear.
14280  - All stored elements are destroyed and all memory is freed.
14281 
14282  @since version 1.0.0
14283  */
14284  ~basic_json() noexcept
14285  {
14286  assert_invariant();
14288  }
14290  /// @}
14291 
14292  public:
14293  ///////////////////////
14294  // object inspection //
14295  ///////////////////////
14296 
14297  /// @name object inspection
14298  /// Functions to inspect the type of a JSON value.
14299  /// @{
14300 
14301  /*!
14302  @brief serialization
14303 
14304  Serialization function for JSON values. The function tries to mimic
14305  Python's `json.dumps()` function, and currently supports its @a indent
14306  and @a ensure_ascii parameters.
14307 
14308  @param[in] indent If indent is nonnegative, then array elements and object
14309  members will be pretty-printed with that indent level. An indent level of
14310  `0` will only insert newlines. `-1` (the default) selects the most compact
14311  representation.
14312  @param[in] indent_char The character to use for indentation if @a indent is
14313  greater than `0`. The default is ` ` (space).
14314  @param[in] ensure_ascii If @a ensure_ascii is true, all non-ASCII characters
14315  in the output are escaped with `\uXXXX` sequences, and the result consists
14316  of ASCII characters only.
14317  @param[in] error_handler how to react on decoding errors; there are three
14318  possible values: `strict` (throws and exception in case a decoding error
14319  occurs; default), `replace` (replace invalid UTF-8 sequences with U+FFFD),
14320  and `ignore` (ignore invalid UTF-8 sequences during serialization).
14321 
14322  @return string containing the serialization of the JSON value
14323 
14324  @throw type_error.316 if a string stored inside the JSON value is not
14325  UTF-8 encoded
14326 
14327  @complexity Linear.
14328 
14329  @exceptionsafety Strong guarantee: if an exception is thrown, there are no
14330  changes in the JSON value.
14331 
14332  @liveexample{The following example shows the effect of different @a indent\,
14333  @a indent_char\, and @a ensure_ascii parameters to the result of the
14334  serialization.,dump}
14335 
14336  @see https://docs.python.org/2/library/json.html#json.dump
14337 
14338  @since version 1.0.0; indentation character @a indent_char, option
14339  @a ensure_ascii and exceptions added in version 3.0.0; error
14340  handlers added in version 3.4.0.
14341  */
14342  string_t dump(const int indent = -1,
14343  const char indent_char = ' ',
14344  const bool ensure_ascii = false,
14346  {
14349 
14350  if (indent >= 0)
14351  {
14352  s.dump(*this, true, ensure_ascii, static_cast<unsigned int>(indent));
14353  }
14354  else
14355  {
14356  s.dump(*this, false, ensure_ascii, 0);
14357  }
14358 
14359  return result;
14360  }
14361 
14362  /*!
14363  @brief return the type of the JSON value (explicit)
14364 
14365  Return the type of the JSON value as a value from the @ref value_t
14366  enumeration.
14367 
14368  @return the type of the JSON value
14369  Value type | return value
14370  ------------------------- | -------------------------
14371  null | value_t::null
14372  boolean | value_t::boolean
14373  string | value_t::string
14374  number (integer) | value_t::number_integer
14375  number (unsigned integer) | value_t::number_unsigned
14376  number (floating-point) | value_t::number_float
14377  object | value_t::object
14378  array | value_t::array
14379  discarded | value_t::discarded
14380 
14381  @complexity Constant.
14382 
14383  @exceptionsafety No-throw guarantee: this member function never throws
14384  exceptions.
14385 
14386  @liveexample{The following code exemplifies `type()` for all JSON
14387  types.,type}
14388 
14389  @sa @ref operator value_t() -- return the type of the JSON value (implicit)
14390  @sa @ref type_name() -- return the type as string
14391 
14392  @since version 1.0.0
14393  */
14394  constexpr value_t type() const noexcept
14395  {
14396  return m_type;
14397  }
14398 
14399  /*!
14400  @brief return whether type is primitive
14401 
14402  This function returns true if and only if the JSON type is primitive
14403  (string, number, boolean, or null).
14404 
14405  @return `true` if type is primitive (string, number, boolean, or null),
14406  `false` otherwise.
14407 
14408  @complexity Constant.
14409 
14410  @exceptionsafety No-throw guarantee: this member function never throws
14411  exceptions.
14412 
14413  @liveexample{The following code exemplifies `is_primitive()` for all JSON
14414  types.,is_primitive}
14415 
14416  @sa @ref is_structured() -- returns whether JSON value is structured
14417  @sa @ref is_null() -- returns whether JSON value is `null`
14418  @sa @ref is_string() -- returns whether JSON value is a string
14419  @sa @ref is_boolean() -- returns whether JSON value is a boolean
14420  @sa @ref is_number() -- returns whether JSON value is a number
14421 
14422  @since version 1.0.0
14423  */
14424  constexpr bool is_primitive() const noexcept
14425  {
14426  return is_null() or is_string() or is_boolean() or is_number();
14427  }
14428 
14429  /*!
14430  @brief return whether type is structured
14431 
14432  This function returns true if and only if the JSON type is structured
14433  (array or object).
14434 
14435  @return `true` if type is structured (array or object), `false` otherwise.
14436 
14437  @complexity Constant.
14438 
14439  @exceptionsafety No-throw guarantee: this member function never throws
14440  exceptions.
14441 
14442  @liveexample{The following code exemplifies `is_structured()` for all JSON
14443  types.,is_structured}
14444 
14445  @sa @ref is_primitive() -- returns whether value is primitive
14446  @sa @ref is_array() -- returns whether value is an array
14447  @sa @ref is_object() -- returns whether value is an object
14448 
14449  @since version 1.0.0
14450  */
14451  constexpr bool is_structured() const noexcept
14452  {
14453  return is_array() or is_object();
14454  }
14455 
14456  /*!
14457  @brief return whether value is null
14458 
14459  This function returns true if and only if the JSON value is null.
14460 
14461  @return `true` if type is null, `false` otherwise.
14462 
14463  @complexity Constant.
14464 
14465  @exceptionsafety No-throw guarantee: this member function never throws
14466  exceptions.
14467 
14468  @liveexample{The following code exemplifies `is_null()` for all JSON
14469  types.,is_null}
14470 
14471  @since version 1.0.0
14472  */
14473  constexpr bool is_null() const noexcept
14474  {
14475  return (m_type == value_t::null);
14476  }
14477 
14478  /*!
14479  @brief return whether value is a boolean
14480 
14481  This function returns true if and only if the JSON value is a boolean.
14482 
14483  @return `true` if type is boolean, `false` otherwise.
14484 
14485  @complexity Constant.
14486 
14487  @exceptionsafety No-throw guarantee: this member function never throws
14488  exceptions.
14489 
14490  @liveexample{The following code exemplifies `is_boolean()` for all JSON
14491  types.,is_boolean}
14492 
14493  @since version 1.0.0
14494  */
14495  constexpr bool is_boolean() const noexcept
14496  {
14497  return (m_type == value_t::boolean);
14498  }
14499 
14500  /*!
14501  @brief return whether value is a number
14502 
14503  This function returns true if and only if the JSON value is a number. This
14504  includes both integer (signed and unsigned) and floating-point values.
14505 
14506  @return `true` if type is number (regardless whether integer, unsigned
14507  integer or floating-type), `false` otherwise.
14508 
14509  @complexity Constant.
14510 
14511  @exceptionsafety No-throw guarantee: this member function never throws
14512  exceptions.
14513 
14514  @liveexample{The following code exemplifies `is_number()` for all JSON
14515  types.,is_number}
14516 
14517  @sa @ref is_number_integer() -- check if value is an integer or unsigned
14518  integer number
14519  @sa @ref is_number_unsigned() -- check if value is an unsigned integer
14520  number
14521  @sa @ref is_number_float() -- check if value is a floating-point number
14522 
14523  @since version 1.0.0
14524  */
14525  constexpr bool is_number() const noexcept
14526  {
14527  return is_number_integer() or is_number_float();
14528  }
14529 
14530  /*!
14531  @brief return whether value is an integer number
14532 
14533  This function returns true if and only if the JSON value is a signed or
14534  unsigned integer number. This excludes floating-point values.
14535 
14536  @return `true` if type is an integer or unsigned integer number, `false`
14537  otherwise.
14538 
14539  @complexity Constant.
14540 
14541  @exceptionsafety No-throw guarantee: this member function never throws
14542  exceptions.
14543 
14544  @liveexample{The following code exemplifies `is_number_integer()` for all
14545  JSON types.,is_number_integer}
14546 
14547  @sa @ref is_number() -- check if value is a number
14548  @sa @ref is_number_unsigned() -- check if value is an unsigned integer
14549  number
14550  @sa @ref is_number_float() -- check if value is a floating-point number
14551 
14552  @since version 1.0.0
14553  */
14554  constexpr bool is_number_integer() const noexcept
14555  {
14557  }
14558 
14559  /*!
14560  @brief return whether value is an unsigned integer number
14561 
14562  This function returns true if and only if the JSON value is an unsigned
14563  integer number. This excludes floating-point and signed integer values.
14564 
14565  @return `true` if type is an unsigned integer number, `false` otherwise.
14566 
14567  @complexity Constant.
14568 
14569  @exceptionsafety No-throw guarantee: this member function never throws
14570  exceptions.
14571 
14572  @liveexample{The following code exemplifies `is_number_unsigned()` for all
14573  JSON types.,is_number_unsigned}
14574 
14575  @sa @ref is_number() -- check if value is a number
14576  @sa @ref is_number_integer() -- check if value is an integer or unsigned
14577  integer number
14578  @sa @ref is_number_float() -- check if value is a floating-point number
14579 
14580  @since version 2.0.0
14581  */
14582  constexpr bool is_number_unsigned() const noexcept
14583  {
14584  return (m_type == value_t::number_unsigned);
14585  }
14586 
14587  /*!
14588  @brief return whether value is a floating-point number
14589 
14590  This function returns true if and only if the JSON value is a
14591  floating-point number. This excludes signed and unsigned integer values.
14592 
14593  @return `true` if type is a floating-point number, `false` otherwise.
14594 
14595  @complexity Constant.
14596 
14597  @exceptionsafety No-throw guarantee: this member function never throws
14598  exceptions.
14599 
14600  @liveexample{The following code exemplifies `is_number_float()` for all
14601  JSON types.,is_number_float}
14602 
14603  @sa @ref is_number() -- check if value is number
14604  @sa @ref is_number_integer() -- check if value is an integer number
14605  @sa @ref is_number_unsigned() -- check if value is an unsigned integer
14606  number
14607 
14608  @since version 1.0.0
14609  */
14610  constexpr bool is_number_float() const noexcept
14611  {
14612  return (m_type == value_t::number_float);
14613  }
14614 
14615  /*!
14616  @brief return whether value is an object
14617 
14618  This function returns true if and only if the JSON value is an object.
14619 
14620  @return `true` if type is object, `false` otherwise.
14621 
14622  @complexity Constant.
14623 
14624  @exceptionsafety No-throw guarantee: this member function never throws
14625  exceptions.
14626 
14627  @liveexample{The following code exemplifies `is_object()` for all JSON
14628  types.,is_object}
14629 
14630  @since version 1.0.0
14631  */
14632  constexpr bool is_object() const noexcept
14633  {
14634  return (m_type == value_t::object);
14635  }
14636 
14637  /*!
14638  @brief return whether value is an array
14639 
14640  This function returns true if and only if the JSON value is an array.
14641 
14642  @return `true` if type is array, `false` otherwise.
14643 
14644  @complexity Constant.
14645 
14646  @exceptionsafety No-throw guarantee: this member function never throws
14647  exceptions.
14648 
14649  @liveexample{The following code exemplifies `is_array()` for all JSON
14650  types.,is_array}
14651 
14652  @since version 1.0.0
14653  */
14654  constexpr bool is_array() const noexcept
14655  {
14656  return (m_type == value_t::array);
14657  }
14658 
14659  /*!
14660  @brief return whether value is a string
14661 
14662  This function returns true if and only if the JSON value is a string.
14663 
14664  @return `true` if type is string, `false` otherwise.
14665 
14666  @complexity Constant.
14667 
14668  @exceptionsafety No-throw guarantee: this member function never throws
14669  exceptions.
14670 
14671  @liveexample{The following code exemplifies `is_string()` for all JSON
14672  types.,is_string}
14673 
14674  @since version 1.0.0
14675  */
14676  constexpr bool is_string() const noexcept
14677  {
14678  return (m_type == value_t::string);
14679  }
14680 
14681  /*!
14682  @brief return whether value is discarded
14683 
14684  This function returns true if and only if the JSON value was discarded
14685  during parsing with a callback function (see @ref parser_callback_t).
14686 
14687  @note This function will always be `false` for JSON values after parsing.
14688  That is, discarded values can only occur during parsing, but will be
14689  removed when inside a structured value or replaced by null in other cases.
14690 
14691  @return `true` if type is discarded, `false` otherwise.
14692 
14693  @complexity Constant.
14694 
14695  @exceptionsafety No-throw guarantee: this member function never throws
14696  exceptions.
14697 
14698  @liveexample{The following code exemplifies `is_discarded()` for all JSON
14699  types.,is_discarded}
14700 
14701  @since version 1.0.0
14702  */
14703  constexpr bool is_discarded() const noexcept
14704  {
14705  return (m_type == value_t::discarded);
14706  }
14707 
14708  /*!
14709  @brief return the type of the JSON value (implicit)
14710 
14711  Implicitly return the type of the JSON value as a value from the @ref
14712  value_t enumeration.
14713 
14714  @return the type of the JSON value
14715 
14716  @complexity Constant.
14717 
14718  @exceptionsafety No-throw guarantee: this member function never throws
14719  exceptions.
14720 
14721  @liveexample{The following code exemplifies the @ref value_t operator for
14722  all JSON types.,operator__value_t}
14723 
14724  @sa @ref type() -- return the type of the JSON value (explicit)
14725  @sa @ref type_name() -- return the type as string
14726 
14727  @since version 1.0.0
14728  */
14729  constexpr operator value_t() const noexcept
14730  {
14731  return m_type;
14732  }
14733 
14734  /// @}
14735 
14736  private:
14737  //////////////////
14738  // value access //
14739  //////////////////
14740 
14741  /// get a boolean (explicit)
14742  boolean_t get_impl(boolean_t* /*unused*/) const
14743  {
14744  if (JSON_LIKELY(is_boolean()))
14745  {
14746  return m_value.boolean;
14747  }
14748 
14749  JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(type_name())));
14750  }
14751 
14752  /// get a pointer to the value (object)
14753  object_t* get_impl_ptr(object_t* /*unused*/) noexcept
14754  {
14755  return is_object() ? m_value.object : nullptr;
14756  }
14757 
14758  /// get a pointer to the value (object)
14759  constexpr const object_t* get_impl_ptr(const object_t* /*unused*/) const noexcept
14760  {
14761  return is_object() ? m_value.object : nullptr;
14762  }
14763 
14764  /// get a pointer to the value (array)
14765  array_t* get_impl_ptr(array_t* /*unused*/) noexcept
14766  {
14767  return is_array() ? m_value.array : nullptr;
14768  }
14769 
14770  /// get a pointer to the value (array)
14771  constexpr const array_t* get_impl_ptr(const array_t* /*unused*/) const noexcept
14772  {
14773  return is_array() ? m_value.array : nullptr;
14774  }
14775 
14776  /// get a pointer to the value (string)
14777  string_t* get_impl_ptr(string_t* /*unused*/) noexcept
14778  {
14779  return is_string() ? m_value.string : nullptr;
14780  }
14781 
14782  /// get a pointer to the value (string)
14783  constexpr const string_t* get_impl_ptr(const string_t* /*unused*/) const noexcept
14784  {
14785  return is_string() ? m_value.string : nullptr;
14786  }
14787 
14788  /// get a pointer to the value (boolean)
14789  boolean_t* get_impl_ptr(boolean_t* /*unused*/) noexcept
14790  {
14791  return is_boolean() ? &m_value.boolean : nullptr;
14792  }
14793 
14794  /// get a pointer to the value (boolean)
14795  constexpr const boolean_t* get_impl_ptr(const boolean_t* /*unused*/) const noexcept
14796  {
14797  return is_boolean() ? &m_value.boolean : nullptr;
14798  }
14799 
14800  /// get a pointer to the value (integer number)
14801  number_integer_t* get_impl_ptr(number_integer_t* /*unused*/) noexcept
14802  {
14803  return is_number_integer() ? &m_value.number_integer : nullptr;
14804  }
14805 
14806  /// get a pointer to the value (integer number)
14807  constexpr const number_integer_t* get_impl_ptr(const number_integer_t* /*unused*/) const noexcept
14808  {
14809  return is_number_integer() ? &m_value.number_integer : nullptr;
14810  }
14811 
14812  /// get a pointer to the value (unsigned number)
14813  number_unsigned_t* get_impl_ptr(number_unsigned_t* /*unused*/) noexcept
14814  {
14815  return is_number_unsigned() ? &m_value.number_unsigned : nullptr;
14816  }
14817 
14818  /// get a pointer to the value (unsigned number)
14819  constexpr const number_unsigned_t* get_impl_ptr(const number_unsigned_t* /*unused*/) const noexcept
14820  {
14821  return is_number_unsigned() ? &m_value.number_unsigned : nullptr;
14822  }
14823 
14824  /// get a pointer to the value (floating-point number)
14825  number_float_t* get_impl_ptr(number_float_t* /*unused*/) noexcept
14826  {
14827  return is_number_float() ? &m_value.number_float : nullptr;
14828  }
14829 
14830  /// get a pointer to the value (floating-point number)
14831  constexpr const number_float_t* get_impl_ptr(const number_float_t* /*unused*/) const noexcept
14832  {
14833  return is_number_float() ? &m_value.number_float : nullptr;
14834  }
14835 
14836  /*!
14837  @brief helper function to implement get_ref()
14838 
14839  This function helps to implement get_ref() without code duplication for
14840  const and non-const overloads
14841 
14842  @tparam ThisType will be deduced as `basic_json` or `const basic_json`
14843 
14844  @throw type_error.303 if ReferenceType does not match underlying value
14845  type of the current JSON
14846  */
14847  template<typename ReferenceType, typename ThisType>
14849  {
14850  // delegate the call to get_ptr<>()
14851  auto ptr = obj.template get_ptr<typename std::add_pointer<ReferenceType>::type>();
14852 
14853  if (JSON_LIKELY(ptr != nullptr))
14854  {
14855  return *ptr;
14856  }
14857 
14858  JSON_THROW(type_error::create(303, "incompatible ReferenceType for get_ref, actual type is " + std::string(obj.type_name())));
14859  }
14860 
14861  public:
14862  /// @name value access
14863  /// Direct access to the stored value of a JSON value.
14864  /// @{
14865 
14866  /*!
14867  @brief get special-case overload
14868 
14869  This overloads avoids a lot of template boilerplate, it can be seen as the
14870  identity method
14871 
14872  @tparam BasicJsonType == @ref basic_json
14873 
14874  @return a copy of *this
14875 
14876  @complexity Constant.
14877 
14878  @since version 2.1.0
14879  */
14880  template<typename BasicJsonType, detail::enable_if_t<
14882  int> = 0>
14883  basic_json get() const
14884  {
14885  return *this;
14886  }
14887 
14888  /*!
14889  @brief get special-case overload
14890 
14891  This overloads converts the current @ref basic_json in a different
14892  @ref basic_json type
14893 
14894  @tparam BasicJsonType == @ref basic_json
14895 
14896  @return a copy of *this, converted into @tparam BasicJsonType
14897 
14898  @complexity Depending on the implementation of the called `from_json()`
14899  method.
14900 
14901  @since version 3.2.0
14902  */
14903  template<typename BasicJsonType, detail::enable_if_t<
14905  detail::is_basic_json<BasicJsonType>::value, int> = 0>
14906  BasicJsonType get() const
14907  {
14908  return *this;
14909  }
14910 
14911  /*!
14912  @brief get a value (explicit)
14913 
14914  Explicit type conversion between the JSON value and a compatible value
14915  which is [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
14916  and [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
14917  The value is converted by calling the @ref json_serializer<ValueType>
14918  `from_json()` method.
14919 
14920  The function is equivalent to executing
14921  @code {.cpp}
14922  ValueType ret;
14923  JSONSerializer<ValueType>::from_json(*this, ret);
14924  return ret;
14925  @endcode
14926 
14927  This overloads is chosen if:
14928  - @a ValueType is not @ref basic_json,
14929  - @ref json_serializer<ValueType> has a `from_json()` method of the form
14930  `void from_json(const basic_json&, ValueType&)`, and
14931  - @ref json_serializer<ValueType> does not have a `from_json()` method of
14932  the form `ValueType from_json(const basic_json&)`
14933 
14934  @tparam ValueTypeCV the provided value type
14935  @tparam ValueType the returned value type
14936 
14937  @return copy of the JSON value, converted to @a ValueType
14938 
14939  @throw what @ref json_serializer<ValueType> `from_json()` method throws
14940 
14941  @liveexample{The example below shows several conversions from JSON values
14942  to other types. There a few things to note: (1) Floating-point numbers can
14943  be converted to integers\, (2) A JSON array can be converted to a standard
14944  `std::vector<short>`\, (3) A JSON object can be converted to C++
14945  associative containers such as `std::unordered_map<std::string\,
14946  json>`.,get__ValueType_const}
14947 
14948  @since version 2.1.0
14949  */
14950  template<typename ValueTypeCV, typename ValueType = detail::uncvref_t<ValueTypeCV>,
14951  detail::enable_if_t <
14952  not detail::is_basic_json<ValueType>::value and
14955  int> = 0>
14956  ValueType get() const noexcept(noexcept(
14958  {
14959  // we cannot static_assert on ValueTypeCV being non-const, because
14960  // there is support for get<const basic_json_t>(), which is why we
14961  // still need the uncvref
14962  static_assert(not std::is_reference<ValueTypeCV>::value,
14963  "get() cannot be used with reference types, you might want to use get_ref()");
14964  static_assert(std::is_default_constructible<ValueType>::value,
14965  "types must be DefaultConstructible when used with get()");
14966 
14967  ValueType ret;
14969  return ret;
14970  }
14971 
14972  /*!
14973  @brief get a value (explicit); special case
14974 
14975  Explicit type conversion between the JSON value and a compatible value
14976  which is **not** [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible)
14977  and **not** [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible).
14978  The value is converted by calling the @ref json_serializer<ValueType>
14979  `from_json()` method.
14980 
14981  The function is equivalent to executing
14982  @code {.cpp}
14983  return JSONSerializer<ValueTypeCV>::from_json(*this);
14984  @endcode
14985 
14986  This overloads is chosen if:
14987  - @a ValueType is not @ref basic_json and
14988  - @ref json_serializer<ValueType> has a `from_json()` method of the form
14989  `ValueType from_json(const basic_json&)`
14990 
14991  @note If @ref json_serializer<ValueType> has both overloads of
14992  `from_json()`, this one is chosen.
14993 
14994  @tparam ValueTypeCV the provided value type
14995  @tparam ValueType the returned value type
14996 
14997  @return copy of the JSON value, converted to @a ValueType
14998 
14999  @throw what @ref json_serializer<ValueType> `from_json()` method throws
15000 
15001  @since version 2.1.0
15002  */
15003  template<typename ValueTypeCV, typename ValueType = detail::uncvref_t<ValueTypeCV>,
15006  int> = 0>
15007  ValueType get() const noexcept(noexcept(
15009  {
15010  static_assert(not std::is_reference<ValueTypeCV>::value,
15011  "get() cannot be used with reference types, you might want to use get_ref()");
15013  }
15014 
15015  /*!
15016  @brief get a value (explicit)
15017 
15018  Explicit type conversion between the JSON value and a compatible value.
15019  The value is filled into the input parameter by calling the @ref json_serializer<ValueType>
15020  `from_json()` method.
15021 
15022  The function is equivalent to executing
15023  @code {.cpp}
15024  ValueType v;
15025  JSONSerializer<ValueType>::from_json(*this, v);
15026  @endcode
15027 
15028  This overloads is chosen if:
15029  - @a ValueType is not @ref basic_json,
15030  - @ref json_serializer<ValueType> has a `from_json()` method of the form
15031  `void from_json(const basic_json&, ValueType&)`, and
15032 
15033  @tparam ValueType the input parameter type.
15034 
15035  @return the input parameter, allowing chaining calls.
15036 
15037  @throw what @ref json_serializer<ValueType> `from_json()` method throws
15038 
15039  @liveexample{The example below shows several conversions from JSON values
15040  to other types. There a few things to note: (1) Floating-point numbers can
15041  be converted to integers\, (2) A JSON array can be converted to a standard
15042  `std::vector<short>`\, (3) A JSON object can be converted to C++
15043  associative containers such as `std::unordered_map<std::string\,
15044  json>`.,get_to}
15045 
15046  @since version 3.3.0
15047  */
15048  template<typename ValueType,
15049  detail::enable_if_t <
15050  not detail::is_basic_json<ValueType>::value and
15052  int> = 0>
15053  ValueType & get_to(ValueType& v) const noexcept(noexcept(
15055  {
15056  JSONSerializer<ValueType>::from_json(*this, v);
15057  return v;
15058  }
15059 
15060 
15061  /*!
15062  @brief get a pointer value (implicit)
15063 
15064  Implicit pointer access to the internally stored JSON value. No copies are
15065  made.
15066 
15067  @warning Writing data to the pointee of the result yields an undefined
15068  state.
15069 
15070  @tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
15071  object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
15072  @ref number_unsigned_t, or @ref number_float_t. Enforced by a static
15073  assertion.
15074 
15075  @return pointer to the internally stored JSON value if the requested
15076  pointer type @a PointerType fits to the JSON value; `nullptr` otherwise
15077 
15078  @complexity Constant.
15079 
15080  @liveexample{The example below shows how pointers to internal values of a
15081  JSON value can be requested. Note that no type conversions are made and a
15082  `nullptr` is returned if the value and the requested pointer type does not
15083  match.,get_ptr}
15084 
15085  @since version 1.0.0
15086  */
15087  template<typename PointerType, typename std::enable_if<
15088  std::is_pointer<PointerType>::value, int>::type = 0>
15089  auto get_ptr() noexcept -> decltype(std::declval<basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
15090  {
15091  // delegate the call to get_impl_ptr<>()
15092  return get_impl_ptr(static_cast<PointerType>(nullptr));
15093  }
15095  /*!
15096  @brief get a pointer value (implicit)
15097  @copydoc get_ptr()
15098  */
15099  template<typename PointerType, typename std::enable_if<
15101  std::is_const<typename std::remove_pointer<PointerType>::type>::value, int>::type = 0>
15102  constexpr auto get_ptr() const noexcept -> decltype(std::declval<const basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
15103  {
15104  // delegate the call to get_impl_ptr<>() const
15105  return get_impl_ptr(static_cast<PointerType>(nullptr));
15106  }
15108  /*!
15109  @brief get a pointer value (explicit)
15110 
15111  Explicit pointer access to the internally stored JSON value. No copies are
15112  made.
15113 
15114  @warning The pointer becomes invalid if the underlying JSON object
15115  changes.
15116 
15117  @tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
15118  object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
15119  @ref number_unsigned_t, or @ref number_float_t.
15120 
15121  @return pointer to the internally stored JSON value if the requested
15122  pointer type @a PointerType fits to the JSON value; `nullptr` otherwise
15123 
15124  @complexity Constant.
15125 
15126  @liveexample{The example below shows how pointers to internal values of a
15127  JSON value can be requested. Note that no type conversions are made and a
15128  `nullptr` is returned if the value and the requested pointer type does not
15129  match.,get__PointerType}
15130 
15131  @sa @ref get_ptr() for explicit pointer-member access
15132 
15133  @since version 1.0.0
15134  */
15135  template<typename PointerType, typename std::enable_if<
15136  std::is_pointer<PointerType>::value, int>::type = 0>
15137  auto get() noexcept -> decltype(std::declval<basic_json_t&>().template get_ptr<PointerType>())
15138  {
15139  // delegate the call to get_ptr
15140  return get_ptr<PointerType>();
15141  }
15143  /*!
15144  @brief get a pointer value (explicit)
15145  @copydoc get()
15146  */
15147  template<typename PointerType, typename std::enable_if<
15148  std::is_pointer<PointerType>::value, int>::type = 0>
15149  constexpr auto get() const noexcept -> decltype(std::declval<const basic_json_t&>().template get_ptr<PointerType>())
15150  {
15151  // delegate the call to get_ptr
15152  return get_ptr<PointerType>();
15153  }
15155  /*!
15156  @brief get a reference value (implicit)
15157 
15158  Implicit reference access to the internally stored JSON value. No copies
15159  are made.
15160 
15161  @warning Writing data to the referee of the result yields an undefined
15162  state.
15163 
15164  @tparam ReferenceType reference type; must be a reference to @ref array_t,
15165  @ref object_t, @ref string_t, @ref boolean_t, @ref number_integer_t, or
15166  @ref number_float_t. Enforced by static assertion.
15167 
15168  @return reference to the internally stored JSON value if the requested
15169  reference type @a ReferenceType fits to the JSON value; throws
15170  type_error.303 otherwise
15171 
15172  @throw type_error.303 in case passed type @a ReferenceType is incompatible
15173  with the stored JSON value; see example below
15174 
15175  @complexity Constant.
15176 
15177  @liveexample{The example shows several calls to `get_ref()`.,get_ref}
15178 
15179  @since version 1.1.0
15180  */
15181  template<typename ReferenceType, typename std::enable_if<
15182  std::is_reference<ReferenceType>::value, int>::type = 0>
15184  {
15185  // delegate call to get_ref_impl
15186  return get_ref_impl<ReferenceType>(*this);
15187  }
15189  /*!
15190  @brief get a reference value (implicit)
15191  @copydoc get_ref()
15192  */
15193  template<typename ReferenceType, typename std::enable_if<
15195  std::is_const<typename std::remove_reference<ReferenceType>::type>::value, int>::type = 0>
15196  ReferenceType get_ref() const
15197  {
15198  // delegate call to get_ref_impl
15199  return get_ref_impl<ReferenceType>(*this);
15200  }
15202  /*!
15203  @brief get a value (implicit)
15204 
15205  Implicit type conversion between the JSON value and a compatible value.
15206  The call is realized by calling @ref get() const.
15207 
15208  @tparam ValueType non-pointer type compatible to the JSON value, for
15209  instance `int` for JSON integer numbers, `bool` for JSON booleans, or
15210  `std::vector` types for JSON arrays. The character type of @ref string_t
15211  as well as an initializer list of this type is excluded to avoid
15212  ambiguities as these types implicitly convert to `std::string`.
15213 
15214  @return copy of the JSON value, converted to type @a ValueType
15215 
15216  @throw type_error.302 in case passed type @a ValueType is incompatible
15217  to the JSON value type (e.g., the JSON value is of type boolean, but a
15218  string is requested); see example below
15219 
15220  @complexity Linear in the size of the JSON value.
15221 
15222  @liveexample{The example below shows several conversions from JSON values
15223  to other types. There a few things to note: (1) Floating-point numbers can
15224  be converted to integers\, (2) A JSON array can be converted to a standard
15225  `std::vector<short>`\, (3) A JSON object can be converted to C++
15226  associative containers such as `std::unordered_map<std::string\,
15227  json>`.,operator__ValueType}
15228 
15229  @since version 1.0.0
15230  */
15231  template < typename ValueType, typename std::enable_if <
15232  not std::is_pointer<ValueType>::value and
15234  not std::is_same<ValueType, typename string_t::value_type>::value and
15236 
15237 #ifndef _MSC_VER // fix for issue #167 operator<< ambiguity under VS2015
15239 #if defined(JSON_HAS_CPP_17) && defined(_MSC_VER) and _MSC_VER <= 1914
15240  and not std::is_same<ValueType, typename std::string_view>::value
15241 #endif
15242 #endif
15244  , int >::type = 0 >
15245  operator ValueType() const
15246  {
15247  // delegate the call to get<>() const
15248  return get<ValueType>();
15249  }
15251  /// @}
15252 
15253 
15254  ////////////////////
15255  // element access //
15256  ////////////////////
15257 
15258  /// @name element access
15259  /// Access to the JSON value.
15260  /// @{
15261 
15262  /*!
15263  @brief access specified array element with bounds checking
15264 
15265  Returns a reference to the element at specified location @a idx, with
15266  bounds checking.
15267 
15268  @param[in] idx index of the element to access
15269 
15270  @return reference to the element at index @a idx
15271 
15272  @throw type_error.304 if the JSON value is not an array; in this case,
15273  calling `at` with an index makes no sense. See example below.
15274  @throw out_of_range.401 if the index @a idx is out of range of the array;
15275  that is, `idx >= size()`. See example below.
15276 
15277  @exceptionsafety Strong guarantee: if an exception is thrown, there are no
15278  changes in the JSON value.
15279 
15280  @complexity Constant.
15281 
15282  @since version 1.0.0
15283 
15284  @liveexample{The example below shows how array elements can be read and
15285  written using `at()`. It also demonstrates the different exceptions that
15286  can be thrown.,at__size_type}
15287  */
15289  {
15290  // at only works for arrays
15291  if (JSON_LIKELY(is_array()))
15292  {
15294  {
15295  return m_value.array->at(idx);
15296  }
15298  {
15299  // create better exception explanation
15300  JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range"));
15301  }
15302  }
15303  else
15304  {
15305  JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name())));
15306  }
15307  }
15308 
15309  /*!
15310  @brief access specified array element with bounds checking
15311 
15312  Returns a const reference to the element at specified location @a idx,
15313  with bounds checking.
15314 
15315  @param[in] idx index of the element to access
15316 
15317  @return const reference to the element at index @a idx
15318 
15319  @throw type_error.304 if the JSON value is not an array; in this case,
15320  calling `at` with an index makes no sense. See example below.
15321  @throw out_of_range.401 if the index @a idx is out of range of the array;
15322  that is, `idx >= size()`. See example below.
15323 
15324  @exceptionsafety Strong guarantee: if an exception is thrown, there are no
15325  changes in the JSON value.
15326 
15327  @complexity Constant.
15328 
15329  @since version 1.0.0
15330 
15331  @liveexample{The example below shows how array elements can be read using
15332  `at()`. It also demonstrates the different exceptions that can be thrown.,
15333  at__size_type_const}
15334  */
15336  {
15337  // at only works for arrays
15338  if (JSON_LIKELY(is_array()))
15339  {
15340  JSON_TRY
15341  {
15342  return m_value.array->at(idx);
15343  }
15345  {
15346  // create better exception explanation
15347  JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range"));
15348  }
15349  }
15350  else
15351  {
15352  JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name())));
15353  }
15354  }
15355 
15356  /*!
15357  @brief access specified object element with bounds checking
15358 
15359  Returns a reference to the element at with specified key @a key, with
15360  bounds checking.
15361 
15362  @param[in] key key of the element to access
15363 
15364  @return reference to the element at key @a key
15365 
15366  @throw type_error.304 if the JSON value is not an object; in this case,
15367  calling `at` with a key makes no sense. See example below.
15368  @throw out_of_range.403 if the key @a key is is not stored in the object;
15369  that is, `find(key) == end()`. See example below.
15370 
15371  @exceptionsafety Strong guarantee: if an exception is thrown, there are no
15372  changes in the JSON value.
15373 
15374  @complexity Logarithmic in the size of the container.
15375 
15376  @sa @ref operator[](const typename object_t::key_type&) for unchecked
15377  access by reference
15378  @sa @ref value() for access by value with a default value
15379 
15380  @since version 1.0.0
15381 
15382  @liveexample{The example below shows how object elements can be read and
15383  written using `at()`. It also demonstrates the different exceptions that
15384  can be thrown.,at__object_t_key_type}
15385  */
15386  reference at(const typename object_t::key_type& key)
15387  {
15388  // at only works for objects
15389  if (JSON_LIKELY(is_object()))
15390  {
15391  JSON_TRY
15392  {
15393  return m_value.object->at(key);
15394  }
15396  {
15397  // create better exception explanation
15398  JSON_THROW(out_of_range::create(403, "key '" + key + "' not found"));
15399  }
15400  }
15401  else
15402  {
15403  JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name())));
15404  }
15405  }
15406 
15407  /*!
15408  @brief access specified object element with bounds checking
15409 
15410  Returns a const reference to the element at with specified key @a key,
15411  with bounds checking.
15412 
15413  @param[in] key key of the element to access
15414 
15415  @return const reference to the element at key @a key
15416 
15417  @throw type_error.304 if the JSON value is not an object; in this case,
15418  calling `at` with a key makes no sense. See example below.
15419  @throw out_of_range.403 if the key @a key is is not stored in the object;
15420  that is, `find(key) == end()`. See example below.
15421 
15422  @exceptionsafety Strong guarantee: if an exception is thrown, there are no
15423  changes in the JSON value.
15424 
15425  @complexity Logarithmic in the size of the container.
15426 
15427  @sa @ref operator[](const typename object_t::key_type&) for unchecked
15428  access by reference
15429  @sa @ref value() for access by value with a default value
15430 
15431  @since version 1.0.0
15432 
15433  @liveexample{The example below shows how object elements can be read using
15434  `at()`. It also demonstrates the different exceptions that can be thrown.,
15435  at__object_t_key_type_const}
15436  */
15437  const_reference at(const typename object_t::key_type& key) const
15438  {
15439  // at only works for objects
15440  if (JSON_LIKELY(is_object()))
15441  {
15442  JSON_TRY
15443  {
15444  return m_value.object->at(key);
15445  }
15447  {
15448  // create better exception explanation
15449  JSON_THROW(out_of_range::create(403, "key '" + key + "' not found"));
15450  }
15451  }
15452  else
15453  {
15454  JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name())));
15455  }
15456  }
15457 
15458  /*!
15459  @brief access specified array element
15460 
15461  Returns a reference to the element at specified location @a idx.
15462 
15463  @note If @a idx is beyond the range of the array (i.e., `idx >= size()`),
15464  then the array is silently filled up with `null` values to make `idx` a
15465  valid reference to the last stored element.
15466 
15467  @param[in] idx index of the element to access
15468 
15469  @return reference to the element at index @a idx
15470 
15471  @throw type_error.305 if the JSON value is not an array or null; in that
15472  cases, using the [] operator with an index makes no sense.
15473 
15474  @complexity Constant if @a idx is in the range of the array. Otherwise
15475  linear in `idx - size()`.
15476 
15477  @liveexample{The example below shows how array elements can be read and
15478  written using `[]` operator. Note the addition of `null`
15479  values.,operatorarray__size_type}
15480 
15481  @since version 1.0.0
15482  */
15484  {
15485  // implicitly convert null value to an empty array
15486  if (is_null())
15487  {
15488  m_type = value_t::array;
15489  m_value.array = create<array_t>();
15490  assert_invariant();
15491  }
15492 
15493  // operator[] only works for arrays
15494  if (JSON_LIKELY(is_array()))
15495  {
15496  // fill up array with null values if given idx is outside range
15497  if (idx >= m_value.array->size())
15498  {
15500  idx - m_value.array->size() + 1,
15501  basic_json());
15502  }
15503 
15504  return m_value.array->operator[](idx);
15505  }
15506 
15507  JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name())));
15508  }
15509 
15510  /*!
15511  @brief access specified array element
15512 
15513  Returns a const reference to the element at specified location @a idx.
15514 
15515  @param[in] idx index of the element to access
15516 
15517  @return const reference to the element at index @a idx
15518 
15519  @throw type_error.305 if the JSON value is not an array; in that case,
15520  using the [] operator with an index makes no sense.
15521 
15522  @complexity Constant.
15523 
15524  @liveexample{The example below shows how array elements can be read using
15525  the `[]` operator.,operatorarray__size_type_const}
15526 
15527  @since version 1.0.0
15528  */
15530  {
15531  // const operator[] only works for arrays
15532  if (JSON_LIKELY(is_array()))
15533  {
15534  return m_value.array->operator[](idx);
15535  }
15536 
15537  JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name())));
15538  }
15539 
15540  /*!
15541  @brief access specified object element
15542 
15543  Returns a reference to the element at with specified key @a key.
15545  @note If @a key is not found in the object, then it is silently added to
15546  the object and filled with a `null` value to make `key` a valid reference.
15547  In case the value was `null` before, it is converted to an object.
15548 
15549  @param[in] key key of the element to access
15550 
15551  @return reference to the element at key @a key
15552 
15553  @throw type_error.305 if the JSON value is not an object or null; in that
15554  cases, using the [] operator with a key makes no sense.
15555 
15556  @complexity Logarithmic in the size of the container.
15557 
15558  @liveexample{The example below shows how object elements can be read and
15559  written using the `[]` operator.,operatorarray__key_type}
15560 
15561  @sa @ref at(const typename object_t::key_type&) for access by reference
15562  with range checking
15563  @sa @ref value() for access by value with a default value
15564 
15565  @since version 1.0.0
15566  */
15567  reference operator[](const typename object_t::key_type& key)
15568  {
15569  // implicitly convert null value to an empty object
15570  if (is_null())
15571  {
15572  m_type = value_t::object;
15574  assert_invariant();
15575  }
15576 
15577  // operator[] only works for objects
15578  if (JSON_LIKELY(is_object()))
15579  {
15580  return m_value.object->operator[](key);
15581  }
15583  JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name())));
15584  }
15585 
15586  /*!
15587  @brief read-only access specified object element
15588 
15589  Returns a const reference to the element at with specified key @a key. No
15590  bounds checking is performed.
15591 
15592  @warning If the element with key @a key does not exist, the behavior is
15593  undefined.
15594 
15595  @param[in] key key of the element to access
15596 
15597  @return const reference to the element at key @a key
15598 
15599  @pre The element with key @a key must exist. **This precondition is
15600  enforced with an assertion.**
15601 
15602  @throw type_error.305 if the JSON value is not an object; in that case,
15603  using the [] operator with a key makes no sense.
15604 
15605  @complexity Logarithmic in the size of the container.
15606 
15607  @liveexample{The example below shows how object elements can be read using
15608  the `[]` operator.,operatorarray__key_type_const}
15609 
15610  @sa @ref at(const typename object_t::key_type&) for access by reference
15611  with range checking
15612  @sa @ref value() for access by value with a default value
15613 
15614  @since version 1.0.0
15615  */
15616  const_reference operator[](const typename object_t::key_type& key) const
15617  {
15618  // const operator[] only works for objects
15619  if (JSON_LIKELY(is_object()))
15620  {
15622  return m_value.object->find(key)->second;
15623  }
15624 
15625  JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name())));
15626  }
15627 
15628  /*!
15629  @brief access specified object element
15630 
15631  Returns a reference to the element at with specified key @a key.
15632 
15633  @note If @a key is not found in the object, then it is silently added to
15634  the object and filled with a `null` value to make `key` a valid reference.
15635  In case the value was `null` before, it is converted to an object.
15636 
15637  @param[in] key key of the element to access
15638 
15639  @return reference to the element at key @a key
15640 
15641  @throw type_error.305 if the JSON value is not an object or null; in that
15642  cases, using the [] operator with a key makes no sense.
15643 
15644  @complexity Logarithmic in the size of the container.
15645 
15646  @liveexample{The example below shows how object elements can be read and
15647  written using the `[]` operator.,operatorarray__key_type}
15648 
15649  @sa @ref at(const typename object_t::key_type&) for access by reference
15650  with range checking
15651  @sa @ref value() for access by value with a default value
15652 
15653  @since version 1.1.0
15654  */
15655  template<typename T>
15656  reference operator[](T* key)
15657  {
15658  // implicitly convert null to object
15659  if (is_null())
15660  {
15661  m_type = value_t::object;
15662  m_value = value_t::object;
15663  assert_invariant();
15664  }
15665 
15666  // at only works for objects
15667  if (JSON_LIKELY(is_object()))
15668  {
15669  return m_value.object->operator[](key);
15670  }
15672  JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name())));
15673  }
15674 
15675  /*!
15676  @brief read-only access specified object element
15677 
15678  Returns a const reference to the element at with specified key @a key. No
15679  bounds checking is performed.
15680 
15681  @warning If the element with key @a key does not exist, the behavior is
15682  undefined.
15683 
15684  @param[in] key key of the element to access
15685 
15686  @return const reference to the element at key @a key
15687 
15688  @pre The element with key @a key must exist. **This precondition is
15689  enforced with an assertion.**
15690 
15691  @throw type_error.305 if the JSON value is not an object; in that case,
15692  using the [] operator with a key makes no sense.
15693 
15694  @complexity Logarithmic in the size of the container.
15695 
15696  @liveexample{The example below shows how object elements can be read using
15697  the `[]` operator.,operatorarray__key_type_const}
15698 
15699  @sa @ref at(const typename object_t::key_type&) for access by reference
15700  with range checking
15701  @sa @ref value() for access by value with a default value
15702 
15703  @since version 1.1.0
15704  */
15705  template<typename T>
15706  const_reference operator[](T* key) const
15707  {
15708  // at only works for objects
15709  if (JSON_LIKELY(is_object()))
15710  {
15712  return m_value.object->find(key)->second;
15713  }
15714 
15715  JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name())));
15716  }
15717 
15718  /*!
15719  @brief access specified object element with default value
15720 
15721  Returns either a copy of an object's element at the specified key @a key
15722  or a given default value if no element with key @a key exists.
15723 
15724  The function is basically equivalent to executing
15725  @code {.cpp}
15726  try {
15727  return at(key);
15728  } catch(out_of_range) {
15729  return default_value;
15730  }
15731  @endcode
15732 
15733  @note Unlike @ref at(const typename object_t::key_type&), this function
15734  does not throw if the given key @a key was not found.
15735 
15736  @note Unlike @ref operator[](const typename object_t::key_type& key), this
15737  function does not implicitly add an element to the position defined by @a
15738  key. This function is furthermore also applicable to const objects.
15739 
15740  @param[in] key key of the element to access
15741  @param[in] default_value the value to return if @a key is not found
15742 
15743  @tparam ValueType type compatible to JSON values, for instance `int` for
15744  JSON integer numbers, `bool` for JSON booleans, or `std::vector` types for
15745  JSON arrays. Note the type of the expected value at @a key and the default
15746  value @a default_value must be compatible.
15747 
15748  @return copy of the element at key @a key or @a default_value if @a key
15749  is not found
15750 
15751  @throw type_error.306 if the JSON value is not an object; in that case,
15752  using `value()` with a key makes no sense.
15753 
15754  @complexity Logarithmic in the size of the container.
15755 
15756  @liveexample{The example below shows how object elements can be queried
15757  with a default value.,basic_json__value}
15758 
15759  @sa @ref at(const typename object_t::key_type&) for access by reference
15760  with range checking
15761  @sa @ref operator[](const typename object_t::key_type&) for unchecked
15762  access by reference
15763 
15764  @since version 1.0.0
15765  */
15766  template<class ValueType, typename std::enable_if<
15768  ValueType value(const typename object_t::key_type& key, const ValueType& default_value) const
15769  {
15770  // at only works for objects
15771  if (JSON_LIKELY(is_object()))
15772  {
15773  // if key is found, return value and given default value otherwise
15774  const auto it = find(key);
15775  if (it != end())
15776  {
15777  return *it;
15778  }
15779 
15780  return default_value;
15781  }
15782 
15783  JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name())));
15784  }
15785 
15786  /*!
15787  @brief overload for a default value of type const char*
15788  @copydoc basic_json::value(const typename object_t::key_type&, const ValueType&) const
15789  */
15790  string_t value(const typename object_t::key_type& key, const char* default_value) const
15791  {
15792  return value(key, string_t(default_value));
15793  }
15794 
15795  /*!
15796  @brief access specified object element via JSON Pointer with default value
15797 
15798  Returns either a copy of an object's element at the specified key @a key
15799  or a given default value if no element with key @a key exists.
15800 
15801  The function is basically equivalent to executing
15802  @code {.cpp}
15803  try {
15804  return at(ptr);
15805  } catch(out_of_range) {
15806  return default_value;
15807  }
15808  @endcode
15809 
15810  @note Unlike @ref at(const json_pointer&), this function does not throw
15811  if the given key @a key was not found.
15812 
15813  @param[in] ptr a JSON pointer to the element to access
15814  @param[in] default_value the value to return if @a ptr found no value
15815 
15816  @tparam ValueType type compatible to JSON values, for instance `int` for
15817  JSON integer numbers, `bool` for JSON booleans, or `std::vector` types for
15818  JSON arrays. Note the type of the expected value at @a key and the default
15819  value @a default_value must be compatible.
15820 
15821  @return copy of the element at key @a key or @a default_value if @a key
15822  is not found
15823 
15824  @throw type_error.306 if the JSON value is not an object; in that case,
15825  using `value()` with a key makes no sense.
15826 
15827  @complexity Logarithmic in the size of the container.
15828 
15829  @liveexample{The example below shows how object elements can be queried
15830  with a default value.,basic_json__value_ptr}
15831 
15832  @sa @ref operator[](const json_pointer&) for unchecked access by reference
15833 
15834  @since version 2.0.2
15835  */
15836  template<class ValueType, typename std::enable_if<
15838  ValueType value(const json_pointer& ptr, const ValueType& default_value) const
15839  {
15840  // at only works for objects
15841  if (JSON_LIKELY(is_object()))
15842  {
15843  // if pointer resolves a value, return it or use default value
15844  JSON_TRY
15845  {
15846  return ptr.get_checked(this);
15847  }
15849  {
15850  return default_value;
15851  }
15852  }
15854  JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name())));
15855  }
15856 
15857  /*!
15858  @brief overload for a default value of type const char*
15859  @copydoc basic_json::value(const json_pointer&, ValueType) const
15860  */
15861  string_t value(const json_pointer& ptr, const char* default_value) const
15862  {
15863  return value(ptr, string_t(default_value));
15864  }
15865 
15866  /*!
15867  @brief access the first element
15868 
15869  Returns a reference to the first element in the container. For a JSON
15870  container `c`, the expression `c.front()` is equivalent to `*c.begin()`.
15871 
15872  @return In case of a structured type (array or object), a reference to the
15873  first element is returned. In case of number, string, or boolean values, a
15874  reference to the value is returned.
15875 
15876  @complexity Constant.
15877 
15878  @pre The JSON value must not be `null` (would throw `std::out_of_range`)
15879  or an empty array or object (undefined behavior, **guarded by
15880  assertions**).
15881  @post The JSON value remains unchanged.
15882 
15883  @throw invalid_iterator.214 when called on `null` value
15884 
15885  @liveexample{The following code shows an example for `front()`.,front}
15886 
15887  @sa @ref back() -- access the last element
15888 
15889  @since version 1.0.0
15890  */
15891  reference front()
15892  {
15893  return *begin();
15894  }
15895 
15896  /*!
15897  @copydoc basic_json::front()
15898  */
15899  const_reference front() const
15900  {
15901  return *cbegin();
15902  }
15903 
15904  /*!
15905  @brief access the last element
15907  Returns a reference to the last element in the container. For a JSON
15908  container `c`, the expression `c.back()` is equivalent to
15909  @code {.cpp}
15910  auto tmp = c.end();
15911  --tmp;
15912  return *tmp;
15913  @endcode
15915  @return In case of a structured type (array or object), a reference to the
15916  last element is returned. In case of number, string, or boolean values, a
15917  reference to the value is returned.
15918 
15919  @complexity Constant.
15920 
15921  @pre The JSON value must not be `null` (would throw `std::out_of_range`)
15922  or an empty array or object (undefined behavior, **guarded by
15923  assertions**).
15924  @post The JSON value remains unchanged.
15925 
15926  @throw invalid_iterator.214 when called on a `null` value. See example
15927  below.
15928 
15929  @liveexample{The following code shows an example for `back()`.,back}
15930 
15931  @sa @ref front() -- access the first element
15932 
15933  @since version 1.0.0
15934  */
15935  reference back()
15936  {
15937  auto tmp = end();
15938  --tmp;
15939  return *tmp;
15940  }
15941 
15942  /*!
15943  @copydoc basic_json::back()
15944  */
15945  const_reference back() const
15946  {
15947  auto tmp = cend();
15948  --tmp;
15949  return *tmp;
15950  }
15951 
15952  /*!
15953  @brief remove element given an iterator
15954 
15955  Removes the element specified by iterator @a pos. The iterator @a pos must
15956  be valid and dereferenceable. Thus the `end()` iterator (which is valid,
15957  but is not dereferenceable) cannot be used as a value for @a pos.
15958 
15959  If called on a primitive type other than `null`, the resulting JSON value
15960  will be `null`.
15961 
15962  @param[in] pos iterator to the element to remove
15963  @return Iterator following the last removed element. If the iterator @a
15964  pos refers to the last element, the `end()` iterator is returned.
15965 
15966  @tparam IteratorType an @ref iterator or @ref const_iterator
15967 
15968  @post Invalidates iterators and references at or after the point of the
15969  erase, including the `end()` iterator.
15970 
15971  @throw type_error.307 if called on a `null` value; example: `"cannot use
15972  erase() with null"`
15973  @throw invalid_iterator.202 if called on an iterator which does not belong
15974  to the current JSON value; example: `"iterator does not fit current
15975  value"`
15976  @throw invalid_iterator.205 if called on a primitive type with invalid
15977  iterator (i.e., any iterator which is not `begin()`); example: `"iterator
15978  out of range"`
15979 
15980  @complexity The complexity depends on the type:
15981  - objects: amortized constant
15982  - arrays: linear in distance between @a pos and the end of the container
15983  - strings: linear in the length of the string
15984  - other types: constant
15985 
15986  @liveexample{The example shows the result of `erase()` for different JSON
15987  types.,erase__IteratorType}
15988 
15989  @sa @ref erase(IteratorType, IteratorType) -- removes the elements in
15990  the given range
15991  @sa @ref erase(const typename object_t::key_type&) -- removes the element
15992  from an object at the given key
15993  @sa @ref erase(const size_type) -- removes the element from an array at
15994  the given index
15995 
15996  @since version 1.0.0
15997  */
15998  template<class IteratorType, typename std::enable_if<
16001  = 0>
16003  {
16004  // make sure iterator fits the current value
16005  if (JSON_UNLIKELY(this != pos.m_object))
16006  {
16007  JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value"));
16008  }
16009 
16010  IteratorType result = end();
16011 
16012  switch (m_type)
16013  {
16014  case value_t::boolean:
16015  case value_t::number_float:
16016  case value_t::number_integer:
16018  case value_t::string:
16019  {
16021  {
16022  JSON_THROW(invalid_iterator::create(205, "iterator out of range"));
16023  }
16024 
16025  if (is_string())
16026  {
16030  m_value.string = nullptr;
16031  }
16032 
16033  m_type = value_t::null;
16034  assert_invariant();
16035  break;
16036  }
16037 
16038  case value_t::object:
16039  {
16041  break;
16042  }
16043 
16044  case value_t::array:
16045  {
16047  break;
16048  }
16049 
16050  default:
16051  JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name())));
16052  }
16053 
16054  return result;
16055  }
16056 
16057  /*!
16058  @brief remove elements given an iterator range
16059 
16060  Removes the element specified by the range `[first; last)`. The iterator
16061  @a first does not need to be dereferenceable if `first == last`: erasing
16062  an empty range is a no-op.
16063 
16064  If called on a primitive type other than `null`, the resulting JSON value
16065  will be `null`.
16066 
16067  @param[in] first iterator to the beginning of the range to remove
16068  @param[in] last iterator past the end of the range to remove
16069  @return Iterator following the last removed element. If the iterator @a
16070  second refers to the last element, the `end()` iterator is returned.
16071 
16072  @tparam IteratorType an @ref iterator or @ref const_iterator
16073 
16074  @post Invalidates iterators and references at or after the point of the
16075  erase, including the `end()` iterator.
16076 
16077  @throw type_error.307 if called on a `null` value; example: `"cannot use
16078  erase() with null"`
16079  @throw invalid_iterator.203 if called on iterators which does not belong
16080  to the current JSON value; example: `"iterators do not fit current value"`
16081  @throw invalid_iterator.204 if called on a primitive type with invalid
16082  iterators (i.e., if `first != begin()` and `last != end()`); example:
16083  `"iterators out of range"`
16084 
16085  @complexity The complexity depends on the type:
16086  - objects: `log(size()) + std::distance(first, last)`
16087  - arrays: linear in the distance between @a first and @a last, plus linear
16088  in the distance between @a last and end of the container
16089  - strings: linear in the length of the string
16090  - other types: constant
16091 
16092  @liveexample{The example shows the result of `erase()` for different JSON
16093  types.,erase__IteratorType_IteratorType}
16094 
16095  @sa @ref erase(IteratorType) -- removes the element at a given position
16096  @sa @ref erase(const typename object_t::key_type&) -- removes the element
16097  from an object at the given key
16098  @sa @ref erase(const size_type) -- removes the element from an array at
16099  the given index
16100 
16101  @since version 1.0.0
16102  */
16103  template<class IteratorType, typename std::enable_if<
16106  = 0>
16108  {
16109  // make sure iterator fits the current value
16110  if (JSON_UNLIKELY(this != first.m_object or this != last.m_object))
16111  {
16112  JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value"));
16113  }
16114 
16115  IteratorType result = end();
16116 
16117  switch (m_type)
16118  {
16119  case value_t::boolean:
16120  case value_t::number_float:
16121  case value_t::number_integer:
16123  case value_t::string:
16124  {
16126  or not last.m_it.primitive_iterator.is_end()))
16127  {
16128  JSON_THROW(invalid_iterator::create(204, "iterators out of range"));
16129  }
16130 
16131  if (is_string())
16132  {
16136  m_value.string = nullptr;
16137  }
16138 
16139  m_type = value_t::null;
16140  assert_invariant();
16141  break;
16142  }
16143 
16144  case value_t::object:
16145  {
16148  break;
16149  }
16150 
16151  case value_t::array:
16152  {
16155  break;
16156  }
16157 
16158  default:
16159  JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name())));
16160  }
16161 
16162  return result;
16163  }
16164 
16165  /*!
16166  @brief remove element from a JSON object given a key
16167 
16168  Removes elements from a JSON object with the key value @a key.
16169 
16170  @param[in] key value of the elements to remove
16171 
16172  @return Number of elements removed. If @a ObjectType is the default
16173  `std::map` type, the return value will always be `0` (@a key was not
16174  found) or `1` (@a key was found).
16175 
16176  @post References and iterators to the erased elements are invalidated.
16177  Other references and iterators are not affected.
16178 
16179  @throw type_error.307 when called on a type other than JSON object;
16180  example: `"cannot use erase() with null"`
16181 
16182  @complexity `log(size()) + count(key)`
16183 
16184  @liveexample{The example shows the effect of `erase()`.,erase__key_type}
16185 
16186  @sa @ref erase(IteratorType) -- removes the element at a given position
16187  @sa @ref erase(IteratorType, IteratorType) -- removes the elements in
16188  the given range
16189  @sa @ref erase(const size_type) -- removes the element from an array at
16190  the given index
16191 
16192  @since version 1.0.0
16193  */
16194  size_type erase(const typename object_t::key_type& key)
16195  {
16196  // this erase only works for objects
16197  if (JSON_LIKELY(is_object()))
16198  {
16199  return m_value.object->erase(key);
16200  }
16201 
16202  JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name())));
16203  }
16204 
16205  /*!
16206  @brief remove element from a JSON array given an index
16207 
16208  Removes element from a JSON array at the index @a idx.
16210  @param[in] idx index of the element to remove
16211 
16212  @throw type_error.307 when called on a type other than JSON object;
16213  example: `"cannot use erase() with null"`
16214  @throw out_of_range.401 when `idx >= size()`; example: `"array index 17
16215  is out of range"`
16216 
16217  @complexity Linear in distance between @a idx and the end of the container.
16218 
16219  @liveexample{The example shows the effect of `erase()`.,erase__size_type}
16220 
16221  @sa @ref erase(IteratorType) -- removes the element at a given position
16222  @sa @ref erase(IteratorType, IteratorType) -- removes the elements in
16223  the given range
16224  @sa @ref erase(const typename object_t::key_type&) -- removes the element
16225  from an object at the given key
16226 
16227  @since version 1.0.0
16228  */
16229  void erase(const size_type idx)
16230  {
16231  // this erase only works for arrays
16232  if (JSON_LIKELY(is_array()))
16233  {
16234  if (JSON_UNLIKELY(idx >= size()))
16235  {
16236  JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range"));
16237  }
16238 
16239  m_value.array->erase(m_value.array->begin() + static_cast<difference_type>(idx));
16240  }
16241  else
16242  {
16243  JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name())));
16244  }
16245  }
16246 
16247  /// @}
16248 
16249 
16250  ////////////
16251  // lookup //
16252  ////////////
16253 
16254  /// @name lookup
16255  /// @{
16256 
16257  /*!
16258  @brief find an element in a JSON object
16259 
16260  Finds an element in a JSON object with key equivalent to @a key. If the
16261  element is not found or the JSON value is not an object, end() is
16262  returned.
16263 
16264  @note This method always returns @ref end() when executed on a JSON type
16265  that is not an object.
16266 
16267  @param[in] key key value of the element to search for.
16268 
16269  @return Iterator to an element with key equivalent to @a key. If no such
16270  element is found or the JSON value is not an object, past-the-end (see
16271  @ref end()) iterator is returned.
16272 
16273  @complexity Logarithmic in the size of the JSON object.
16274 
16275  @liveexample{The example shows how `find()` is used.,find__key_type}
16276 
16277  @since version 1.0.0
16278  */
16279  template<typename KeyT>
16280  iterator find(KeyT&& key)
16281  {
16282  auto result = end();
16283 
16284  if (is_object())
16285  {
16287  }
16288 
16289  return result;
16290  }
16291 
16292  /*!
16293  @brief find an element in a JSON object
16294  @copydoc find(KeyT&&)
16295  */
16296  template<typename KeyT>
16297  const_iterator find(KeyT&& key) const
16298  {
16299  auto result = cend();
16300 
16301  if (is_object())
16302  {
16304  }
16305 
16306  return result;
16307  }
16308 
16309  /*!
16310  @brief returns the number of occurrences of a key in a JSON object
16311 
16312  Returns the number of elements with key @a key. If ObjectType is the
16313  default `std::map` type, the return value will always be `0` (@a key was
16314  not found) or `1` (@a key was found).
16315 
16316  @note This method always returns `0` when executed on a JSON type that is
16317  not an object.
16318 
16319  @param[in] key key value of the element to count
16320 
16321  @return Number of elements with key @a key. If the JSON value is not an
16322  object, the return value will be `0`.
16323 
16324  @complexity Logarithmic in the size of the JSON object.
16325 
16326  @liveexample{The example shows how `count()` is used.,count}
16327 
16328  @since version 1.0.0
16329  */
16330  template<typename KeyT>
16331  size_type count(KeyT&& key) const
16332  {
16333  // return 0 for all nonobject types
16334  return is_object() ? m_value.object->count(std::forward<KeyT>(key)) : 0;
16335  }
16336 
16337  /// @}
16338 
16339 
16340  ///////////////
16341  // iterators //
16342  ///////////////
16343 
16344  /// @name iterators
16345  /// @{
16347  /*!
16348  @brief returns an iterator to the first element
16349 
16350  Returns an iterator to the first element.
16351 
16352  @image html range-begin-end.svg "Illustration from cppreference.com"
16353 
16354  @return iterator to the first element
16355 
16356  @complexity Constant.
16357 
16358  @requirement This function helps `basic_json` satisfying the
16359  [Container](https://en.cppreference.com/w/cpp/named_req/Container)
16360  requirements:
16361  - The complexity is constant.
16362 
16363  @liveexample{The following code shows an example for `begin()`.,begin}
16364 
16365  @sa @ref cbegin() -- returns a const iterator to the beginning
16366  @sa @ref end() -- returns an iterator to the end
16367  @sa @ref cend() -- returns a const iterator to the end
16368 
16369  @since version 1.0.0
16370  */
16371  iterator begin() noexcept
16372  {
16373  iterator result(this);
16374  result.set_begin();
16375  return result;
16376  }
16377 
16378  /*!
16379  @copydoc basic_json::cbegin()
16380  */
16381  const_iterator begin() const noexcept
16382  {
16383  return cbegin();
16384  }
16385 
16386  /*!
16387  @brief returns a const iterator to the first element
16388 
16389  Returns a const iterator to the first element.
16390 
16391  @image html range-begin-end.svg "Illustration from cppreference.com"
16392 
16393  @return const iterator to the first element
16394 
16395  @complexity Constant.
16397  @requirement This function helps `basic_json` satisfying the
16398  [Container](https://en.cppreference.com/w/cpp/named_req/Container)
16399  requirements:
16400  - The complexity is constant.
16401  - Has the semantics of `const_cast<const basic_json&>(*this).begin()`.
16402 
16403  @liveexample{The following code shows an example for `cbegin()`.,cbegin}
16404 
16405  @sa @ref begin() -- returns an iterator to the beginning
16406  @sa @ref end() -- returns an iterator to the end
16407  @sa @ref cend() -- returns a const iterator to the end
16408 
16409  @since version 1.0.0
16410  */
16411  const_iterator cbegin() const noexcept
16412  {
16413  const_iterator result(this);
16414  result.set_begin();
16415  return result;
16416  }
16417 
16418  /*!
16419  @brief returns an iterator to one past the last element
16420 
16421  Returns an iterator to one past the last element.
16422 
16423  @image html range-begin-end.svg "Illustration from cppreference.com"
16424 
16425  @return iterator one past the last element
16427  @complexity Constant.
16428 
16429  @requirement This function helps `basic_json` satisfying the
16430  [Container](https://en.cppreference.com/w/cpp/named_req/Container)
16431  requirements:
16432  - The complexity is constant.
16433 
16434  @liveexample{The following code shows an example for `end()`.,end}
16435 
16436  @sa @ref cend() -- returns a const iterator to the end
16437  @sa @ref begin() -- returns an iterator to the beginning
16438  @sa @ref cbegin() -- returns a const iterator to the beginning
16439 
16440  @since version 1.0.0
16441  */
16442  iterator end() noexcept
16443  {
16444  iterator result(this);
16445  result.set_end();
16446  return result;
16447  }
16448 
16449  /*!
16450  @copydoc basic_json::cend()
16451  */
16452  const_iterator end() const noexcept
16453  {
16454  return cend();
16455  }
16456 
16457  /*!
16458  @brief returns a const iterator to one past the last element
16459 
16460  Returns a const iterator to one past the last element.
16461 
16462  @image html range-begin-end.svg "Illustration from cppreference.com"
16463 
16464  @return const iterator one past the last element
16465 
16466  @complexity Constant.
16468  @requirement This function helps `basic_json` satisfying the
16469  [Container](https://en.cppreference.com/w/cpp/named_req/Container)
16470  requirements:
16471  - The complexity is constant.
16472  - Has the semantics of `const_cast<const basic_json&>(*this).end()`.
16473 
16474  @liveexample{The following code shows an example for `cend()`.,cend}
16475 
16476  @sa @ref end() -- returns an iterator to the end
16477  @sa @ref begin() -- returns an iterator to the beginning
16478  @sa @ref cbegin() -- returns a const iterator to the beginning
16479 
16480  @since version 1.0.0
16481  */
16482  const_iterator cend() const noexcept
16483  {
16484  const_iterator result(this);
16485  result.set_end();
16486  return result;
16487  }
16488 
16489  /*!
16490  @brief returns an iterator to the reverse-beginning
16491 
16492  Returns an iterator to the reverse-beginning; that is, the last element.
16493 
16494  @image html range-rbegin-rend.svg "Illustration from cppreference.com"
16495 
16496  @complexity Constant.
16498  @requirement This function helps `basic_json` satisfying the
16499  [ReversibleContainer](https://en.cppreference.com/w/cpp/named_req/ReversibleContainer)
16500  requirements:
16501  - The complexity is constant.
16502  - Has the semantics of `reverse_iterator(end())`.
16503 
16504  @liveexample{The following code shows an example for `rbegin()`.,rbegin}
16505 
16506  @sa @ref crbegin() -- returns a const reverse iterator to the beginning
16507  @sa @ref rend() -- returns a reverse iterator to the end
16508  @sa @ref crend() -- returns a const reverse iterator to the end
16509 
16510  @since version 1.0.0
16511  */
16512  reverse_iterator rbegin() noexcept
16513  {
16514  return reverse_iterator(end());
16515  }
16516 
16517  /*!
16518  @copydoc basic_json::crbegin()
16519  */
16520  const_reverse_iterator rbegin() const noexcept
16521  {
16522  return crbegin();
16523  }
16524 
16525  /*!
16526  @brief returns an iterator to the reverse-end
16528  Returns an iterator to the reverse-end; that is, one before the first
16529  element.
16530 
16531  @image html range-rbegin-rend.svg "Illustration from cppreference.com"
16532 
16533  @complexity Constant.
16534 
16535  @requirement This function helps `basic_json` satisfying the
16536  [ReversibleContainer](https://en.cppreference.com/w/cpp/named_req/ReversibleContainer)
16537  requirements:
16538  - The complexity is constant.
16539  - Has the semantics of `reverse_iterator(begin())`.
16540 
16541  @liveexample{The following code shows an example for `rend()`.,rend}
16542 
16543  @sa @ref crend() -- returns a const reverse iterator to the end
16544  @sa @ref rbegin() -- returns a reverse iterator to the beginning
16545  @sa @ref crbegin() -- returns a const reverse iterator to the beginning
16546 
16547  @since version 1.0.0
16548  */
16549  reverse_iterator rend() noexcept
16550  {
16551  return reverse_iterator(begin());
16552  }
16553 
16554  /*!
16555  @copydoc basic_json::crend()
16556  */
16557  const_reverse_iterator rend() const noexcept
16558  {
16559  return crend();
16560  }
16561 
16562  /*!
16563  @brief returns a const reverse iterator to the last element
16565  Returns a const iterator to the reverse-beginning; that is, the last
16566  element.
16567 
16568  @image html range-rbegin-rend.svg "Illustration from cppreference.com"
16569 
16570  @complexity Constant.
16571 
16572  @requirement This function helps `basic_json` satisfying the
16573  [ReversibleContainer](https://en.cppreference.com/w/cpp/named_req/ReversibleContainer)
16574  requirements:
16575  - The complexity is constant.
16576  - Has the semantics of `const_cast<const basic_json&>(*this).rbegin()`.
16577 
16578  @liveexample{The following code shows an example for `crbegin()`.,crbegin}
16579 
16580  @sa @ref rbegin() -- returns a reverse iterator to the beginning
16581  @sa @ref rend() -- returns a reverse iterator to the end
16582  @sa @ref crend() -- returns a const reverse iterator to the end
16583 
16584  @since version 1.0.0
16585  */
16586  const_reverse_iterator crbegin() const noexcept
16587  {
16588  return const_reverse_iterator(cend());
16589  }
16590 
16591  /*!
16592  @brief returns a const reverse iterator to one before the first
16593 
16594  Returns a const reverse iterator to the reverse-end; that is, one before
16595  the first element.
16596 
16597  @image html range-rbegin-rend.svg "Illustration from cppreference.com"
16598 
16599  @complexity Constant.
16600 
16601  @requirement This function helps `basic_json` satisfying the
16602  [ReversibleContainer](https://en.cppreference.com/w/cpp/named_req/ReversibleContainer)
16603  requirements:
16604  - The complexity is constant.
16605  - Has the semantics of `const_cast<const basic_json&>(*this).rend()`.
16606 
16607  @liveexample{The following code shows an example for `crend()`.,crend}
16608 
16609  @sa @ref rend() -- returns a reverse iterator to the end
16610  @sa @ref rbegin() -- returns a reverse iterator to the beginning
16611  @sa @ref crbegin() -- returns a const reverse iterator to the beginning
16612 
16613  @since version 1.0.0
16614  */
16615  const_reverse_iterator crend() const noexcept
16616  {
16617  return const_reverse_iterator(cbegin());
16618  }
16619 
16620  public:
16621  /*!
16622  @brief wrapper to access iterator member functions in range-based for
16623 
16624  This function allows to access @ref iterator::key() and @ref
16625  iterator::value() during range-based for loops. In these loops, a
16626  reference to the JSON values is returned, so there is no access to the
16627  underlying iterator.
16628 
16629  For loop without iterator_wrapper:
16631  @code{cpp}
16632  for (auto it = j_object.begin(); it != j_object.end(); ++it)
16633  {
16634  std::cout << "key: " << it.key() << ", value:" << it.value() << '\n';
16635  }
16636  @endcode
16637 
16638  Range-based for loop without iterator proxy:
16639 
16640  @code{cpp}
16641  for (auto it : j_object)
16642  {
16643  // "it" is of type json::reference and has no key() member
16644  std::cout << "value: " << it << '\n';
16645  }
16646  @endcode
16647 
16648  Range-based for loop with iterator proxy:
16649 
16650  @code{cpp}
16651  for (auto it : json::iterator_wrapper(j_object))
16652  {
16653  std::cout << "key: " << it.key() << ", value:" << it.value() << '\n';
16654  }
16655  @endcode
16656 
16657  @note When iterating over an array, `key()` will return the index of the
16658  element as string (see example).
16659 
16660  @param[in] ref reference to a JSON value
16661  @return iteration proxy object wrapping @a ref with an interface to use in
16662  range-based for loops
16663 
16664  @liveexample{The following code shows how the wrapper is used,iterator_wrapper}
16665 
16666  @exceptionsafety Strong guarantee: if an exception is thrown, there are no
16667  changes in the JSON value.
16668 
16669  @complexity Constant.
16670 
16671  @note The name of this function is not yet final and may change in the
16672  future.
16673 
16674  @deprecated This stream operator is deprecated and will be removed in
16675  future 4.0.0 of the library. Please use @ref items() instead;
16676  that is, replace `json::iterator_wrapper(j)` with `j.items()`.
16677  */
16680  {
16681  return ref.items();
16682  }
16683 
16684  /*!
16685  @copydoc iterator_wrapper(reference)
16686  */
16689  {
16690  return ref.items();
16691  }
16692 
16693  /*!
16694  @brief helper to access iterator member functions in range-based for
16695 
16696  This function allows to access @ref iterator::key() and @ref
16697  iterator::value() during range-based for loops. In these loops, a
16698  reference to the JSON values is returned, so there is no access to the
16699  underlying iterator.
16700 
16701  For loop without `items()` function:
16702 
16703  @code{cpp}
16704  for (auto it = j_object.begin(); it != j_object.end(); ++it)
16705  {
16706  std::cout << "key: " << it.key() << ", value:" << it.value() << '\n';
16707  }
16708  @endcode
16709 
16710  Range-based for loop without `items()` function:
16711 
16712  @code{cpp}
16713  for (auto it : j_object)
16714  {
16715  // "it" is of type json::reference and has no key() member
16716  std::cout << "value: " << it << '\n';
16717  }
16718  @endcode
16719 
16720  Range-based for loop with `items()` function:
16721 
16722  @code{cpp}
16723  for (auto& el : j_object.items())
16724  {
16725  std::cout << "key: " << el.key() << ", value:" << el.value() << '\n';
16726  }
16727  @endcode
16728 
16729  The `items()` function also allows to use
16730  [structured bindings](https://en.cppreference.com/w/cpp/language/structured_binding)
16731  (C++17):
16732 
16733  @code{cpp}
16734  for (auto& [key, val] : j_object.items())
16735  {
16736  std::cout << "key: " << key << ", value:" << val << '\n';
16737  }
16738  @endcode
16739 
16740  @note When iterating over an array, `key()` will return the index of the
16741  element as string (see example). For primitive types (e.g., numbers),
16742  `key()` returns an empty string.
16743 
16744  @return iteration proxy object wrapping @a ref with an interface to use in
16745  range-based for loops
16746 
16747  @liveexample{The following code shows how the function is used.,items}
16748 
16749  @exceptionsafety Strong guarantee: if an exception is thrown, there are no
16750  changes in the JSON value.
16751 
16752  @complexity Constant.
16753 
16754  @since version 3.1.0, structured bindings support since 3.5.0.
16755  */
16756  iteration_proxy<iterator> items() noexcept
16757  {
16758  return iteration_proxy<iterator>(*this);
16759  }
16760 
16761  /*!
16762  @copydoc items()
16763  */
16764  iteration_proxy<const_iterator> items() const noexcept
16765  {
16766  return iteration_proxy<const_iterator>(*this);
16767  }
16768 
16769  /// @}
16770 
16772  //////////////
16773  // capacity //
16774  //////////////
16775 
16776  /// @name capacity
16777  /// @{
16778 
16779  /*!
16780  @brief checks whether the container is empty.
16781 
16782  Checks if a JSON value has no elements (i.e. whether its @ref size is `0`).
16783 
16784  @return The return value depends on the different types and is
16785  defined as follows:
16786  Value type | return value
16787  ----------- | -------------
16788  null | `true`
16789  boolean | `false`
16790  string | `false`
16791  number | `false`
16792  object | result of function `object_t::empty()`
16793  array | result of function `array_t::empty()`
16794 
16795  @liveexample{The following code uses `empty()` to check if a JSON
16796  object contains any elements.,empty}
16797 
16798  @complexity Constant, as long as @ref array_t and @ref object_t satisfy
16799  the Container concept; that is, their `empty()` functions have constant
16800  complexity.
16801 
16802  @iterators No changes.
16803 
16804  @exceptionsafety No-throw guarantee: this function never throws exceptions.
16805 
16806  @note This function does not return whether a string stored as JSON value
16807  is empty - it returns whether the JSON container itself is empty which is
16808  false in the case of a string.
16809 
16810  @requirement This function helps `basic_json` satisfying the
16811  [Container](https://en.cppreference.com/w/cpp/named_req/Container)
16812  requirements:
16813  - The complexity is constant.
16814  - Has the semantics of `begin() == end()`.
16815 
16816  @sa @ref size() -- returns the number of elements
16817 
16818  @since version 1.0.0
16819  */
16820  bool empty() const noexcept
16821  {
16822  switch (m_type)
16823  {
16824  case value_t::null:
16825  {
16826  // null values are empty
16827  return true;
16828  }
16829 
16830  case value_t::array:
16831  {
16832  // delegate call to array_t::empty()
16833  return m_value.array->empty();
16834  }
16836  case value_t::object:
16837  {
16838  // delegate call to object_t::empty()
16839  return m_value.object->empty();
16840  }
16841 
16842  default:
16843  {
16844  // all other types are nonempty
16845  return false;
16846  }
16847  }
16848  }
16849 
16850  /*!
16851  @brief returns the number of elements
16852 
16853  Returns the number of elements in a JSON value.
16854 
16855  @return The return value depends on the different types and is
16856  defined as follows:
16857  Value type | return value
16858  ----------- | -------------
16859  null | `0`
16860  boolean | `1`
16861  string | `1`
16862  number | `1`
16863  object | result of function object_t::size()
16864  array | result of function array_t::size()
16865 
16866  @liveexample{The following code calls `size()` on the different value
16867  types.,size}
16868 
16869  @complexity Constant, as long as @ref array_t and @ref object_t satisfy
16870  the Container concept; that is, their size() functions have constant
16871  complexity.
16872 
16873  @iterators No changes.
16874 
16875  @exceptionsafety No-throw guarantee: this function never throws exceptions.
16876 
16877  @note This function does not return the length of a string stored as JSON
16878  value - it returns the number of elements in the JSON value which is 1 in
16879  the case of a string.
16880 
16881  @requirement This function helps `basic_json` satisfying the
16882  [Container](https://en.cppreference.com/w/cpp/named_req/Container)
16883  requirements:
16884  - The complexity is constant.
16885  - Has the semantics of `std::distance(begin(), end())`.
16886 
16887  @sa @ref empty() -- checks whether the container is empty
16888  @sa @ref max_size() -- returns the maximal number of elements
16889 
16890  @since version 1.0.0
16891  */
16892  size_type size() const noexcept
16893  {
16894  switch (m_type)
16895  {
16896  case value_t::null:
16897  {
16898  // null values are empty
16899  return 0;
16900  }
16901 
16902  case value_t::array:
16903  {
16904  // delegate call to array_t::size()
16905  return m_value.array->size();
16906  }
16908  case value_t::object:
16909  {
16910  // delegate call to object_t::size()
16911  return m_value.object->size();
16912  }
16913 
16914  default:
16915  {
16916  // all other types have size 1
16917  return 1;
16918  }
16919  }
16920  }
16921 
16922  /*!
16923  @brief returns the maximum possible number of elements
16924 
16925  Returns the maximum number of elements a JSON value is able to hold due to
16926  system or library implementation limitations, i.e. `std::distance(begin(),
16927  end())` for the JSON value.
16928 
16929  @return The return value depends on the different types and is
16930  defined as follows:
16931  Value type | return value
16932  ----------- | -------------
16933  null | `0` (same as `size()`)
16934  boolean | `1` (same as `size()`)
16935  string | `1` (same as `size()`)
16936  number | `1` (same as `size()`)
16937  object | result of function `object_t::max_size()`
16938  array | result of function `array_t::max_size()`
16939 
16940  @liveexample{The following code calls `max_size()` on the different value
16941  types. Note the output is implementation specific.,max_size}
16942 
16943  @complexity Constant, as long as @ref array_t and @ref object_t satisfy
16944  the Container concept; that is, their `max_size()` functions have constant
16945  complexity.
16946 
16947  @iterators No changes.
16948 
16949  @exceptionsafety No-throw guarantee: this function never throws exceptions.
16950 
16951  @requirement This function helps `basic_json` satisfying the
16952  [Container](https://en.cppreference.com/w/cpp/named_req/Container)
16953  requirements:
16954  - The complexity is constant.
16955  - Has the semantics of returning `b.size()` where `b` is the largest
16956  possible JSON value.
16957 
16958  @sa @ref size() -- returns the number of elements
16959 
16960  @since version 1.0.0
16961  */
16962  size_type max_size() const noexcept
16963  {
16964  switch (m_type)
16965  {
16966  case value_t::array:
16967  {
16968  // delegate call to array_t::max_size()
16969  return m_value.array->max_size();
16970  }
16971 
16972  case value_t::object:
16973  {
16974  // delegate call to object_t::max_size()
16975  return m_value.object->max_size();
16976  }
16978  default:
16979  {
16980  // all other types have max_size() == size()
16981  return size();
16982  }
16983  }
16984  }
16985 
16986  /// @}
16987 
16988 
16989  ///////////////
16990  // modifiers //
16991  ///////////////
16992 
16993  /// @name modifiers
16994  /// @{
16995 
16996  /*!
16997  @brief clears the contents
16998 
16999  Clears the content of a JSON value and resets it to the default value as
17000  if @ref basic_json(value_t) would have been called with the current value
17001  type from @ref type():
17002 
17003  Value type | initial value
17004  ----------- | -------------
17005  null | `null`
17006  boolean | `false`
17007  string | `""`
17008  number | `0`
17009  object | `{}`
17010  array | `[]`
17011 
17012  @post Has the same effect as calling
17013  @code {.cpp}
17014  *this = basic_json(type());
17015  @endcode
17016 
17017  @liveexample{The example below shows the effect of `clear()` to different
17018  JSON types.,clear}
17019 
17020  @complexity Linear in the size of the JSON value.
17021 
17022  @iterators All iterators, pointers and references related to this container
17023  are invalidated.
17024 
17025  @exceptionsafety No-throw guarantee: this function never throws exceptions.
17026 
17027  @sa @ref basic_json(value_t) -- constructor that creates an object with the
17028  same value than calling `clear()`
17029 
17030  @since version 1.0.0
17031  */
17032  void clear() noexcept
17033  {
17034  switch (m_type)
17035  {
17036  case value_t::number_integer:
17037  {
17038  m_value.number_integer = 0;
17039  break;
17040  }
17041 
17042  case value_t::number_unsigned:
17043  {
17045  break;
17046  }
17048  case value_t::number_float:
17049  {
17050  m_value.number_float = 0.0;
17051  break;
17052  }
17053 
17054  case value_t::boolean:
17055  {
17056  m_value.boolean = false;
17057  break;
17058  }
17059 
17060  case value_t::string:
17061  {
17062  m_value.string->clear();
17063  break;
17064  }
17065 
17066  case value_t::array:
17067  {
17068  m_value.array->clear();
17069  break;
17070  }
17071 
17072  case value_t::object:
17073  {
17074  m_value.object->clear();
17075  break;
17076  }
17077 
17078  default:
17079  break;
17080  }
17081  }
17082 
17083  /*!
17084  @brief add an object to an array
17085 
17086  Appends the given element @a val to the end of the JSON value. If the
17087  function is called on a JSON null value, an empty array is created before
17088  appending @a val.
17089 
17090  @param[in] val the value to add to the JSON array
17091 
17092  @throw type_error.308 when called on a type other than JSON array or
17093  null; example: `"cannot use push_back() with number"`
17094 
17095  @complexity Amortized constant.
17096 
17097  @liveexample{The example shows how `push_back()` and `+=` can be used to
17098  add elements to a JSON array. Note how the `null` value was silently
17099  converted to a JSON array.,push_back}
17100 
17101  @since version 1.0.0
17102  */
17103  void push_back(basic_json&& val)
17104  {
17105  // push_back only works for null objects or arrays
17106  if (JSON_UNLIKELY(not(is_null() or is_array())))
17107  {
17108  JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name())));
17109  }
17110 
17111  // transform null object into an array
17112  if (is_null())
17113  {
17114  m_type = value_t::array;
17115  m_value = value_t::array;
17116  assert_invariant();
17117  }
17119  // add element to array (move semantics)
17121  // invalidate object
17122  val.m_type = value_t::null;
17123  }
17124 
17125  /*!
17126  @brief add an object to an array
17127  @copydoc push_back(basic_json&&)
17128  */
17130  {
17131  push_back(std::move(val));
17132  return *this;
17133  }
17134 
17135  /*!
17136  @brief add an object to an array
17137  @copydoc push_back(basic_json&&)
17138  */
17139  void push_back(const basic_json& val)
17140  {
17141  // push_back only works for null objects or arrays
17142  if (JSON_UNLIKELY(not(is_null() or is_array())))
17143  {
17144  JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name())));
17145  }
17146 
17147  // transform null object into an array
17148  if (is_null())
17149  {
17150  m_type = value_t::array;
17151  m_value = value_t::array;
17152  assert_invariant();
17153  }
17155  // add element to array
17157  }
17158 
17159  /*!
17160  @brief add an object to an array
17161  @copydoc push_back(basic_json&&)
17162  */
17163  reference operator+=(const basic_json& val)
17164  {
17165  push_back(val);
17166  return *this;
17167  }
17168 
17169  /*!
17170  @brief add an object to an object
17171 
17172  Inserts the given element @a val to the JSON object. If the function is
17173  called on a JSON null value, an empty object is created before inserting
17174  @a val.
17175 
17176  @param[in] val the value to add to the JSON object
17177 
17178  @throw type_error.308 when called on a type other than JSON object or
17179  null; example: `"cannot use push_back() with number"`
17180 
17181  @complexity Logarithmic in the size of the container, O(log(`size()`)).
17182 
17183  @liveexample{The example shows how `push_back()` and `+=` can be used to
17184  add elements to a JSON object. Note how the `null` value was silently
17185  converted to a JSON object.,push_back__object_t__value}
17186 
17187  @since version 1.0.0
17188  */
17189  void push_back(const typename object_t::value_type& val)
17190  {
17191  // push_back only works for null objects or objects
17192  if (JSON_UNLIKELY(not(is_null() or is_object())))
17193  {
17194  JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name())));
17195  }
17196 
17197  // transform null object into an object
17198  if (is_null())
17199  {
17200  m_type = value_t::object;
17201  m_value = value_t::object;
17202  assert_invariant();
17203  }
17205  // add element to array
17207  }
17208 
17209  /*!
17210  @brief add an object to an object
17211  @copydoc push_back(const typename object_t::value_type&)
17212  */
17213  reference operator+=(const typename object_t::value_type& val)
17214  {
17215  push_back(val);
17216  return *this;
17217  }
17218 
17219  /*!
17220  @brief add an object to an object
17221 
17222  This function allows to use `push_back` with an initializer list. In case
17223 
17224  1. the current value is an object,
17225  2. the initializer list @a init contains only two elements, and
17226  3. the first element of @a init is a string,
17227 
17228  @a init is converted into an object element and added using
17229  @ref push_back(const typename object_t::value_type&). Otherwise, @a init
17230  is converted to a JSON value and added using @ref push_back(basic_json&&).
17231 
17232  @param[in] init an initializer list
17233 
17234  @complexity Linear in the size of the initializer list @a init.
17235 
17236  @note This function is required to resolve an ambiguous overload error,
17237  because pairs like `{"key", "value"}` can be both interpreted as
17238  `object_t::value_type` or `std::initializer_list<basic_json>`, see
17239  https://github.com/nlohmann/json/issues/235 for more information.
17240 
17241  @liveexample{The example shows how initializer lists are treated as
17242  objects when possible.,push_back__initializer_list}
17243  */
17245  {
17246  if (is_object() and init.size() == 2 and (*init.begin())->is_string())
17247  {
17249  push_back(typename object_t::value_type(
17250  std::move(key.get_ref<string_t&>()), (init.begin() + 1)->moved_or_copied()));
17251  }
17252  else
17253  {
17255  }
17256  }
17257 
17258  /*!
17259  @brief add an object to an object
17260  @copydoc push_back(initializer_list_t)
17261  */
17263  {
17264  push_back(init);
17265  return *this;
17266  }
17267 
17268  /*!
17269  @brief add an object to an array
17270 
17271  Creates a JSON value from the passed parameters @a args to the end of the
17272  JSON value. If the function is called on a JSON null value, an empty array
17273  is created before appending the value created from @a args.
17274 
17275  @param[in] args arguments to forward to a constructor of @ref basic_json
17276  @tparam Args compatible types to create a @ref basic_json object
17278  @throw type_error.311 when called on a type other than JSON array or
17279  null; example: `"cannot use emplace_back() with number"`
17280 
17281  @complexity Amortized constant.
17282 
17283  @liveexample{The example shows how `push_back()` can be used to add
17284  elements to a JSON array. Note how the `null` value was silently converted
17285  to a JSON array.,emplace_back}
17286 
17287  @since version 2.0.8
17288  */
17289  template<class... Args>
17290  void emplace_back(Args&& ... args)
17291  {
17292  // emplace_back only works for null objects or arrays
17293  if (JSON_UNLIKELY(not(is_null() or is_array())))
17294  {
17295  JSON_THROW(type_error::create(311, "cannot use emplace_back() with " + std::string(type_name())));
17296  }
17297 
17298  // transform null object into an array
17299  if (is_null())
17300  {
17301  m_type = value_t::array;
17302  m_value = value_t::array;
17303  assert_invariant();
17304  }
17306  // add element to array (perfect forwarding)
17308  }
17309 
17310  /*!
17311  @brief add an object to an object if key does not exist
17312 
17313  Inserts a new element into a JSON object constructed in-place with the
17314  given @a args if there is no element with the key in the container. If the
17315  function is called on a JSON null value, an empty object is created before
17316  appending the value created from @a args.
17317 
17318  @param[in] args arguments to forward to a constructor of @ref basic_json
17319  @tparam Args compatible types to create a @ref basic_json object
17320 
17321  @return a pair consisting of an iterator to the inserted element, or the
17322  already-existing element if no insertion happened, and a bool
17323  denoting whether the insertion took place.
17324 
17325  @throw type_error.311 when called on a type other than JSON object or
17326  null; example: `"cannot use emplace() with number"`
17327 
17328  @complexity Logarithmic in the size of the container, O(log(`size()`)).
17329 
17330  @liveexample{The example shows how `emplace()` can be used to add elements
17331  to a JSON object. Note how the `null` value was silently converted to a
17332  JSON object. Further note how no value is added if there was already one
17333  value stored with the same key.,emplace}
17334 
17335  @since version 2.0.8
17336  */
17337  template<class... Args>
17338  std::pair<iterator, bool> emplace(Args&& ... args)
17339  {
17340  // emplace only works for null objects or arrays
17341  if (JSON_UNLIKELY(not(is_null() or is_object())))
17342  {
17343  JSON_THROW(type_error::create(311, "cannot use emplace() with " + std::string(type_name())));
17344  }
17345 
17346  // transform null object into an object
17347  if (is_null())
17348  {
17349  m_type = value_t::object;
17350  m_value = value_t::object;
17351  assert_invariant();
17352  }
17354  // add element to array (perfect forwarding)
17355  auto res = m_value.object->emplace(std::forward<Args>(args)...);
17356  // create result iterator and set iterator to the result of emplace
17357  auto it = begin();
17359 
17360  // return pair of iterator and boolean
17361  return {it, res.second};
17362  }
17363 
17364  /// Helper for insertion of an iterator
17365  /// @note: This uses std::distance to support GCC 4.8,
17366  /// see https://github.com/nlohmann/json/pull/1257
17367  template<typename... Args>
17369  {
17370  iterator result(this);
17371  assert(m_value.array != nullptr);
17372 
17376 
17377  // This could have been written as:
17378  // result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, cnt, val);
17379  // but the return value of insert is missing in GCC 4.8, so it is written this way instead.
17380 
17381  return result;
17382  }
17384  /*!
17385  @brief inserts element
17386 
17387  Inserts element @a val before iterator @a pos.
17388 
17389  @param[in] pos iterator before which the content will be inserted; may be
17390  the end() iterator
17391  @param[in] val element to insert
17392  @return iterator pointing to the inserted @a val.
17393 
17394  @throw type_error.309 if called on JSON values other than arrays;
17395  example: `"cannot use insert() with string"`
17396  @throw invalid_iterator.202 if @a pos is not an iterator of *this;
17397  example: `"iterator does not fit current value"`
17398 
17399  @complexity Constant plus linear in the distance between @a pos and end of
17400  the container.
17401 
17402  @liveexample{The example shows how `insert()` is used.,insert}
17403 
17404  @since version 1.0.0
17405  */
17407  {
17408  // insert only works for arrays
17409  if (JSON_LIKELY(is_array()))
17410  {
17411  // check if iterator pos fits to this JSON value
17412  if (JSON_UNLIKELY(pos.m_object != this))
17413  {
17414  JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value"));
17415  }
17416 
17417  // insert to array and return iterator
17418  return insert_iterator(pos, val);
17419  }
17420 
17421  JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name())));
17422  }
17423 
17424  /*!
17425  @brief inserts element
17426  @copydoc insert(const_iterator, const basic_json&)
17427  */
17429  {
17430  return insert(pos, val);
17431  }
17432 
17433  /*!
17434  @brief inserts elements
17435 
17436  Inserts @a cnt copies of @a val before iterator @a pos.
17437 
17438  @param[in] pos iterator before which the content will be inserted; may be
17439  the end() iterator
17440  @param[in] cnt number of copies of @a val to insert
17441  @param[in] val element to insert
17442  @return iterator pointing to the first element inserted, or @a pos if
17443  `cnt==0`
17444 
17445  @throw type_error.309 if called on JSON values other than arrays; example:
17446  `"cannot use insert() with string"`
17447  @throw invalid_iterator.202 if @a pos is not an iterator of *this;
17448  example: `"iterator does not fit current value"`
17449 
17450  @complexity Linear in @a cnt plus linear in the distance between @a pos
17451  and end of the container.
17452 
17453  @liveexample{The example shows how `insert()` is used.,insert__count}
17454 
17455  @since version 1.0.0
17456  */
17458  {
17459  // insert only works for arrays
17460  if (JSON_LIKELY(is_array()))
17461  {
17462  // check if iterator pos fits to this JSON value
17463  if (JSON_UNLIKELY(pos.m_object != this))
17464  {
17465  JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value"));
17466  }
17467 
17468  // insert to array and return iterator
17469  return insert_iterator(pos, cnt, val);
17470  }
17471 
17472  JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name())));
17473  }
17474 
17475  /*!
17476  @brief inserts elements
17477 
17478  Inserts elements from range `[first, last)` before iterator @a pos.
17479 
17480  @param[in] pos iterator before which the content will be inserted; may be
17481  the end() iterator
17482  @param[in] first begin of the range of elements to insert
17483  @param[in] last end of the range of elements to insert
17484 
17485  @throw type_error.309 if called on JSON values other than arrays; example:
17486  `"cannot use insert() with string"`
17487  @throw invalid_iterator.202 if @a pos is not an iterator of *this;
17488  example: `"iterator does not fit current value"`
17489  @throw invalid_iterator.210 if @a first and @a last do not belong to the
17490  same JSON value; example: `"iterators do not fit"`
17491  @throw invalid_iterator.211 if @a first or @a last are iterators into
17492  container for which insert is called; example: `"passed iterators may not
17493  belong to container"`
17494 
17495  @return iterator pointing to the first element inserted, or @a pos if
17496  `first==last`
17497 
17498  @complexity Linear in `std::distance(first, last)` plus linear in the
17499  distance between @a pos and end of the container.
17500 
17501  @liveexample{The example shows how `insert()` is used.,insert__range}
17502 
17503  @since version 1.0.0
17504  */
17506  {
17507  // insert only works for arrays
17508  if (JSON_UNLIKELY(not is_array()))
17509  {
17510  JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name())));
17511  }
17512 
17513  // check if iterator pos fits to this JSON value
17514  if (JSON_UNLIKELY(pos.m_object != this))
17515  {
17516  JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value"));
17517  }
17518 
17519  // check if range iterators belong to the same JSON object
17521  {
17522  JSON_THROW(invalid_iterator::create(210, "iterators do not fit"));
17523  }
17524 
17525  if (JSON_UNLIKELY(first.m_object == this))
17526  {
17527  JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container"));
17528  }
17529 
17530  // insert to array and return iterator
17532  }
17533 
17534  /*!
17535  @brief inserts elements
17536 
17537  Inserts elements from initializer list @a ilist before iterator @a pos.
17538 
17539  @param[in] pos iterator before which the content will be inserted; may be
17540  the end() iterator
17541  @param[in] ilist initializer list to insert the values from
17542 
17543  @throw type_error.309 if called on JSON values other than arrays; example:
17544  `"cannot use insert() with string"`
17545  @throw invalid_iterator.202 if @a pos is not an iterator of *this;
17546  example: `"iterator does not fit current value"`
17547 
17548  @return iterator pointing to the first element inserted, or @a pos if
17549  `ilist` is empty
17550 
17551  @complexity Linear in `ilist.size()` plus linear in the distance between
17552  @a pos and end of the container.
17553 
17554  @liveexample{The example shows how `insert()` is used.,insert__ilist}
17555 
17556  @since version 1.0.0
17557  */
17559  {
17560  // insert only works for arrays
17561  if (JSON_UNLIKELY(not is_array()))
17562  {
17563  JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name())));
17564  }
17565 
17566  // check if iterator pos fits to this JSON value
17567  if (JSON_UNLIKELY(pos.m_object != this))
17568  {
17569  JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value"));
17570  }
17571 
17572  // insert to array and return iterator
17574  }
17575 
17576  /*!
17577  @brief inserts elements
17578 
17579  Inserts elements from range `[first, last)`.
17580 
17581  @param[in] first begin of the range of elements to insert
17582  @param[in] last end of the range of elements to insert
17583 
17584  @throw type_error.309 if called on JSON values other than objects; example:
17585  `"cannot use insert() with string"`
17586  @throw invalid_iterator.202 if iterator @a first or @a last does does not
17587  point to an object; example: `"iterators first and last must point to
17588  objects"`
17589  @throw invalid_iterator.210 if @a first and @a last do not belong to the
17590  same JSON value; example: `"iterators do not fit"`
17591 
17592  @complexity Logarithmic: `O(N*log(size() + N))`, where `N` is the number
17593  of elements to insert.
17594 
17595  @liveexample{The example shows how `insert()` is used.,insert__range_object}
17596 
17597  @since version 3.0.0
17598  */
17600  {
17601  // insert only works for objects
17602  if (JSON_UNLIKELY(not is_object()))
17603  {
17604  JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name())));
17605  }
17606 
17607  // check if range iterators belong to the same JSON object
17609  {
17610  JSON_THROW(invalid_iterator::create(210, "iterators do not fit"));
17611  }
17612 
17613  // passed iterators must belong to objects
17615  {
17616  JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects"));
17617  }
17618 
17620  }
17621 
17622  /*!
17623  @brief updates a JSON object from another object, overwriting existing keys
17624 
17625  Inserts all values from JSON object @a j and overwrites existing keys.
17626 
17627  @param[in] j JSON object to read values from
17628 
17629  @throw type_error.312 if called on JSON values other than objects; example:
17630  `"cannot use update() with string"`
17631 
17632  @complexity O(N*log(size() + N)), where N is the number of elements to
17633  insert.
17634 
17635  @liveexample{The example shows how `update()` is used.,update}
17636 
17637  @sa https://docs.python.org/3.6/library/stdtypes.html#dict.update
17638 
17639  @since version 3.0.0
17640  */
17641  void update(const_reference j)
17642  {
17643  // implicitly convert null value to an empty object
17644  if (is_null())
17645  {
17646  m_type = value_t::object;
17648  assert_invariant();
17649  }
17650 
17651  if (JSON_UNLIKELY(not is_object()))
17652  {
17653  JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name())));
17654  }
17655  if (JSON_UNLIKELY(not j.is_object()))
17656  {
17657  JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(j.type_name())));
17658  }
17659 
17660  for (auto it = j.cbegin(); it != j.cend(); ++it)
17661  {
17662  m_value.object->operator[](it.key()) = it.value();
17663  }
17664  }
17665 
17666  /*!
17667  @brief updates a JSON object from another object, overwriting existing keys
17668 
17669  Inserts all values from from range `[first, last)` and overwrites existing
17670  keys.
17671 
17672  @param[in] first begin of the range of elements to insert
17673  @param[in] last end of the range of elements to insert
17674 
17675  @throw type_error.312 if called on JSON values other than objects; example:
17676  `"cannot use update() with string"`
17677  @throw invalid_iterator.202 if iterator @a first or @a last does does not
17678  point to an object; example: `"iterators first and last must point to
17679  objects"`
17680  @throw invalid_iterator.210 if @a first and @a last do not belong to the
17681  same JSON value; example: `"iterators do not fit"`
17682 
17683  @complexity O(N*log(size() + N)), where N is the number of elements to
17684  insert.
17685 
17686  @liveexample{The example shows how `update()` is used__range.,update}
17687 
17688  @sa https://docs.python.org/3.6/library/stdtypes.html#dict.update
17689 
17690  @since version 3.0.0
17691  */
17693  {
17694  // implicitly convert null value to an empty object
17695  if (is_null())
17696  {
17697  m_type = value_t::object;
17699  assert_invariant();
17700  }
17701 
17702  if (JSON_UNLIKELY(not is_object()))
17703  {
17704  JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name())));
17705  }
17706 
17707  // check if range iterators belong to the same JSON object
17709  {
17710  JSON_THROW(invalid_iterator::create(210, "iterators do not fit"));
17711  }
17712 
17713  // passed iterators must belong to objects
17715  or not last.m_object->is_object()))
17716  {
17717  JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects"));
17718  }
17719 
17720  for (auto it = first; it != last; ++it)
17721  {
17722  m_value.object->operator[](it.key()) = it.value();
17723  }
17724  }
17725 
17726  /*!
17727  @brief exchanges the values
17728 
17729  Exchanges the contents of the JSON value with those of @a other. Does not
17730  invoke any move, copy, or swap operations on individual elements. All
17731  iterators and references remain valid. The past-the-end iterator is
17732  invalidated.
17733 
17734  @param[in,out] other JSON value to exchange the contents with
17735 
17736  @complexity Constant.
17737 
17738  @liveexample{The example below shows how JSON values can be swapped with
17739  `swap()`.,swap__reference}
17740 
17741  @since version 1.0.0
17742  */
17743  void swap(reference other) noexcept (
17748  )
17749  {
17750  std::swap(m_type, other.m_type);
17752  assert_invariant();
17753  }
17754 
17755  /*!
17756  @brief exchanges the values
17757 
17758  Exchanges the contents of a JSON array with those of @a other. Does not
17759  invoke any move, copy, or swap operations on individual elements. All
17760  iterators and references remain valid. The past-the-end iterator is
17761  invalidated.
17762 
17763  @param[in,out] other array to exchange the contents with
17764 
17765  @throw type_error.310 when JSON value is not an array; example: `"cannot
17766  use swap() with string"`
17767 
17768  @complexity Constant.
17769 
17770  @liveexample{The example below shows how arrays can be swapped with
17771  `swap()`.,swap__array_t}
17772 
17773  @since version 1.0.0
17774  */
17775  void swap(array_t& other)
17776  {
17777  // swap only works for arrays
17778  if (JSON_LIKELY(is_array()))
17779  {
17780  std::swap(*(m_value.array), other);
17781  }
17782  else
17783  {
17784  JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name())));
17785  }
17786  }
17787 
17788  /*!
17789  @brief exchanges the values
17791  Exchanges the contents of a JSON object with those of @a other. Does not
17792  invoke any move, copy, or swap operations on individual elements. All
17793  iterators and references remain valid. The past-the-end iterator is
17794  invalidated.
17795 
17796  @param[in,out] other object to exchange the contents with
17797 
17798  @throw type_error.310 when JSON value is not an object; example:
17799  `"cannot use swap() with string"`
17800 
17801  @complexity Constant.
17802 
17803  @liveexample{The example below shows how objects can be swapped with
17804  `swap()`.,swap__object_t}
17805 
17806  @since version 1.0.0
17807  */
17808  void swap(object_t& other)
17809  {
17810  // swap only works for objects
17811  if (JSON_LIKELY(is_object()))
17812  {
17813  std::swap(*(m_value.object), other);
17814  }
17815  else
17816  {
17817  JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name())));
17818  }
17819  }
17820 
17821  /*!
17822  @brief exchanges the values
17824  Exchanges the contents of a JSON string with those of @a other. Does not
17825  invoke any move, copy, or swap operations on individual elements. All
17826  iterators and references remain valid. The past-the-end iterator is
17827  invalidated.
17828 
17829  @param[in,out] other string to exchange the contents with
17830 
17831  @throw type_error.310 when JSON value is not a string; example: `"cannot
17832  use swap() with boolean"`
17833 
17834  @complexity Constant.
17835 
17836  @liveexample{The example below shows how strings can be swapped with
17837  `swap()`.,swap__string_t}
17838 
17839  @since version 1.0.0
17840  */
17841  void swap(string_t& other)
17842  {
17843  // swap only works for strings
17844  if (JSON_LIKELY(is_string()))
17845  {
17846  std::swap(*(m_value.string), other);
17847  }
17848  else
17849  {
17850  JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name())));
17851  }
17852  }
17853 
17854  /// @}
17855 
17856  public:
17857  //////////////////////////////////////////
17858  // lexicographical comparison operators //
17859  //////////////////////////////////////////
17860 
17861  /// @name lexicographical comparison operators
17862  /// @{
17863 
17864  /*!
17865  @brief comparison: equal
17866 
17867  Compares two JSON values for equality according to the following rules:
17868  - Two JSON values are equal if (1) they are from the same type and (2)
17869  their stored values are the same according to their respective
17870  `operator==`.
17871  - Integer and floating-point numbers are automatically converted before
17872  comparison. Note than two NaN values are always treated as unequal.
17873  - Two JSON null values are equal.
17874 
17875  @note Floating-point inside JSON values numbers are compared with
17876  `json::number_float_t::operator==` which is `double::operator==` by
17877  default. To compare floating-point while respecting an epsilon, an alternative
17878  [comparison function](https://github.com/mariokonrad/marnav/blob/master/src/marnav/math/floatingpoint.hpp#L34-#L39)
17879  could be used, for instance
17880  @code {.cpp}
17881  template<typename T, typename = typename std::enable_if<std::is_floating_point<T>::value, T>::type>
17882  inline bool is_same(T a, T b, T epsilon = std::numeric_limits<T>::epsilon()) noexcept
17883  {
17884  return std::abs(a - b) <= epsilon;
17885  }
17886  @endcode
17887 
17888  @note NaN values never compare equal to themselves or to other NaN values.
17889 
17890  @param[in] lhs first JSON value to consider
17891  @param[in] rhs second JSON value to consider
17892  @return whether the values @a lhs and @a rhs are equal
17893 
17894  @exceptionsafety No-throw guarantee: this function never throws exceptions.
17895 
17896  @complexity Linear.
17897 
17898  @liveexample{The example demonstrates comparing several JSON
17899  types.,operator__equal}
17900 
17901  @since version 1.0.0
17902  */
17903  friend bool operator==(const_reference lhs, const_reference rhs) noexcept
17904  {
17905  const auto lhs_type = lhs.type();
17906  const auto rhs_type = rhs.type();
17907 
17908  if (lhs_type == rhs_type)
17909  {
17910  switch (lhs_type)
17911  {
17912  case value_t::array:
17913  return (*lhs.m_value.array == *rhs.m_value.array);
17914 
17915  case value_t::object:
17916  return (*lhs.m_value.object == *rhs.m_value.object);
17917 
17918  case value_t::null:
17919  return true;
17920 
17921  case value_t::string:
17922  return (*lhs.m_value.string == *rhs.m_value.string);
17923 
17924  case value_t::boolean:
17925  return (lhs.m_value.boolean == rhs.m_value.boolean);
17926 
17927  case value_t::number_integer:
17929 
17930  case value_t::number_unsigned:
17932 
17933  case value_t::number_float:
17935 
17936  default:
17937  return false;
17938  }
17939  }
17941  {
17942  return (static_cast<number_float_t>(lhs.m_value.number_integer) == rhs.m_value.number_float);
17943  }
17945  {
17946  return (lhs.m_value.number_float == static_cast<number_float_t>(rhs.m_value.number_integer));
17947  }
17949  {
17950  return (static_cast<number_float_t>(lhs.m_value.number_unsigned) == rhs.m_value.number_float);
17951  }
17953  {
17954  return (lhs.m_value.number_float == static_cast<number_float_t>(rhs.m_value.number_unsigned));
17955  }
17957  {
17959  }
17961  {
17963  }
17964 
17965  return false;
17966  }
17967 
17968  /*!
17969  @brief comparison: equal
17970  @copydoc operator==(const_reference, const_reference)
17971  */
17972  template<typename ScalarType, typename std::enable_if<
17973  std::is_scalar<ScalarType>::value, int>::type = 0>
17974  friend bool operator==(const_reference lhs, const ScalarType rhs) noexcept
17975  {
17976  return (lhs == basic_json(rhs));
17977  }
17978 
17979  /*!
17980  @brief comparison: equal
17981  @copydoc operator==(const_reference, const_reference)
17982  */
17983  template<typename ScalarType, typename std::enable_if<
17984  std::is_scalar<ScalarType>::value, int>::type = 0>
17985  friend bool operator==(const ScalarType lhs, const_reference rhs) noexcept
17986  {
17987  return (basic_json(lhs) == rhs);
17988  }
17990  /*!
17991  @brief comparison: not equal
17992 
17993  Compares two JSON values for inequality by calculating `not (lhs == rhs)`.
17994 
17995  @param[in] lhs first JSON value to consider
17996  @param[in] rhs second JSON value to consider
17997  @return whether the values @a lhs and @a rhs are not equal
17998 
17999  @complexity Linear.
18001  @exceptionsafety No-throw guarantee: this function never throws exceptions.
18002 
18003  @liveexample{The example demonstrates comparing several JSON
18004  types.,operator__notequal}
18005 
18006  @since version 1.0.0
18007  */
18008  friend bool operator!=(const_reference lhs, const_reference rhs) noexcept
18009  {
18010  return not (lhs == rhs);
18011  }
18012 
18013  /*!
18014  @brief comparison: not equal
18015  @copydoc operator!=(const_reference, const_reference)
18016  */
18017  template<typename ScalarType, typename std::enable_if<
18018  std::is_scalar<ScalarType>::value, int>::type = 0>
18019  friend bool operator!=(const_reference lhs, const ScalarType rhs) noexcept
18020  {
18021  return (lhs != basic_json(rhs));
18022  }
18024  /*!
18025  @brief comparison: not equal
18026  @copydoc operator!=(const_reference, const_reference)
18027  */
18028  template<typename ScalarType, typename std::enable_if<
18029  std::is_scalar<ScalarType>::value, int>::type = 0>
18030  friend bool operator!=(const ScalarType lhs, const_reference rhs) noexcept
18031  {
18032  return (basic_json(lhs) != rhs);
18033  }
18035  /*!
18036  @brief comparison: less than
18037 
18038  Compares whether one JSON value @a lhs is less than another JSON value @a
18039  rhs according to the following rules:
18040  - If @a lhs and @a rhs have the same type, the values are compared using
18041  the default `<` operator.
18042  - Integer and floating-point numbers are automatically converted before
18043  comparison
18044  - In case @a lhs and @a rhs have different types, the values are ignored
18045  and the order of the types is considered, see
18046  @ref operator<(const value_t, const value_t).
18047 
18048  @param[in] lhs first JSON value to consider
18049  @param[in] rhs second JSON value to consider
18050  @return whether @a lhs is less than @a rhs
18051 
18052  @complexity Linear.
18053 
18054  @exceptionsafety No-throw guarantee: this function never throws exceptions.
18055 
18056  @liveexample{The example demonstrates comparing several JSON
18057  types.,operator__less}
18058 
18059  @since version 1.0.0
18060  */
18061  friend bool operator<(const_reference lhs, const_reference rhs) noexcept
18062  {
18063  const auto lhs_type = lhs.type();
18064  const auto rhs_type = rhs.type();
18065 
18066  if (lhs_type == rhs_type)
18067  {
18068  switch (lhs_type)
18069  {
18070  case value_t::array:
18071  return (*lhs.m_value.array) < (*rhs.m_value.array);
18072 
18073  case value_t::object:
18074  return *lhs.m_value.object < *rhs.m_value.object;
18075 
18076  case value_t::null:
18077  return false;
18078 
18079  case value_t::string:
18080  return *lhs.m_value.string < *rhs.m_value.string;
18081 
18082  case value_t::boolean:
18083  return lhs.m_value.boolean < rhs.m_value.boolean;
18084 
18085  case value_t::number_integer:
18087 
18088  case value_t::number_unsigned:
18090 
18091  case value_t::number_float:
18093 
18094  default:
18095  return false;
18096  }
18097  }
18099  {
18101  }
18103  {
18105  }
18107  {
18109  }
18111  {
18113  }
18115  {
18117  }
18119  {
18121  }
18122 
18123  // We only reach this line if we cannot compare values. In that case,
18124  // we compare types. Note we have to call the operator explicitly,
18125  // because MSVC has problems otherwise.
18126  return operator<(lhs_type, rhs_type);
18127  }
18128 
18129  /*!
18130  @brief comparison: less than
18131  @copydoc operator<(const_reference, const_reference)
18132  */
18133  template<typename ScalarType, typename std::enable_if<
18134  std::is_scalar<ScalarType>::value, int>::type = 0>
18135  friend bool operator<(const_reference lhs, const ScalarType rhs) noexcept
18136  {
18137  return (lhs < basic_json(rhs));
18138  }
18139 
18140  /*!
18141  @brief comparison: less than
18142  @copydoc operator<(const_reference, const_reference)
18143  */
18144  template<typename ScalarType, typename std::enable_if<
18145  std::is_scalar<ScalarType>::value, int>::type = 0>
18146  friend bool operator<(const ScalarType lhs, const_reference rhs) noexcept
18147  {
18148  return (basic_json(lhs) < rhs);
18149  }
18151  /*!
18152  @brief comparison: less than or equal
18153 
18154  Compares whether one JSON value @a lhs is less than or equal to another
18155  JSON value by calculating `not (rhs < lhs)`.
18156 
18157  @param[in] lhs first JSON value to consider
18158  @param[in] rhs second JSON value to consider
18159  @return whether @a lhs is less than or equal to @a rhs
18160 
18161  @complexity Linear.
18162 
18163  @exceptionsafety No-throw guarantee: this function never throws exceptions.
18164 
18165  @liveexample{The example demonstrates comparing several JSON
18166  types.,operator__greater}
18167 
18168  @since version 1.0.0
18169  */
18170  friend bool operator<=(const_reference lhs, const_reference rhs) noexcept
18171  {
18172  return not (rhs < lhs);
18173  }
18174 
18175  /*!
18176  @brief comparison: less than or equal
18177  @copydoc operator<=(const_reference, const_reference)
18178  */
18179  template<typename ScalarType, typename std::enable_if<
18180  std::is_scalar<ScalarType>::value, int>::type = 0>
18181  friend bool operator<=(const_reference lhs, const ScalarType rhs) noexcept
18182  {
18183  return (lhs <= basic_json(rhs));
18184  }
18186  /*!
18187  @brief comparison: less than or equal
18188  @copydoc operator<=(const_reference, const_reference)
18189  */
18190  template<typename ScalarType, typename std::enable_if<
18191  std::is_scalar<ScalarType>::value, int>::type = 0>
18192  friend bool operator<=(const ScalarType lhs, const_reference rhs) noexcept
18193  {
18194  return (basic_json(lhs) <= rhs);
18195  }
18197  /*!
18198  @brief comparison: greater than
18199 
18200  Compares whether one JSON value @a lhs is greater than another
18201  JSON value by calculating `not (lhs <= rhs)`.
18202 
18203  @param[in] lhs first JSON value to consider
18204  @param[in] rhs second JSON value to consider
18205  @return whether @a lhs is greater than to @a rhs
18206 
18207  @complexity Linear.
18208 
18209  @exceptionsafety No-throw guarantee: this function never throws exceptions.
18210 
18211  @liveexample{The example demonstrates comparing several JSON
18212  types.,operator__lessequal}
18213 
18214  @since version 1.0.0
18215  */
18216  friend bool operator>(const_reference lhs, const_reference rhs) noexcept
18217  {
18218  return not (lhs <= rhs);
18219  }
18220 
18221  /*!
18222  @brief comparison: greater than
18223  @copydoc operator>(const_reference, const_reference)
18224  */
18225  template<typename ScalarType, typename std::enable_if<
18226  std::is_scalar<ScalarType>::value, int>::type = 0>
18227  friend bool operator>(const_reference lhs, const ScalarType rhs) noexcept
18228  {
18229  return (lhs > basic_json(rhs));
18230  }
18232  /*!
18233  @brief comparison: greater than
18234  @copydoc operator>(const_reference, const_reference)
18235  */
18236  template<typename ScalarType, typename std::enable_if<
18237  std::is_scalar<ScalarType>::value, int>::type = 0>
18238  friend bool operator>(const ScalarType lhs, const_reference rhs) noexcept
18239  {
18240  return (basic_json(lhs) > rhs);
18241  }
18243  /*!
18244  @brief comparison: greater than or equal
18245 
18246  Compares whether one JSON value @a lhs is greater than or equal to another
18247  JSON value by calculating `not (lhs < rhs)`.
18248 
18249  @param[in] lhs first JSON value to consider
18250  @param[in] rhs second JSON value to consider
18251  @return whether @a lhs is greater than or equal to @a rhs
18252 
18253  @complexity Linear.
18254 
18255  @exceptionsafety No-throw guarantee: this function never throws exceptions.
18256 
18257  @liveexample{The example demonstrates comparing several JSON
18258  types.,operator__greaterequal}
18259 
18260  @since version 1.0.0
18261  */
18262  friend bool operator>=(const_reference lhs, const_reference rhs) noexcept
18263  {
18264  return not (lhs < rhs);
18265  }
18266 
18267  /*!
18268  @brief comparison: greater than or equal
18269  @copydoc operator>=(const_reference, const_reference)
18270  */
18271  template<typename ScalarType, typename std::enable_if<
18272  std::is_scalar<ScalarType>::value, int>::type = 0>
18273  friend bool operator>=(const_reference lhs, const ScalarType rhs) noexcept
18274  {
18275  return (lhs >= basic_json(rhs));
18276  }
18278  /*!
18279  @brief comparison: greater than or equal
18280  @copydoc operator>=(const_reference, const_reference)
18281  */
18282  template<typename ScalarType, typename std::enable_if<
18283  std::is_scalar<ScalarType>::value, int>::type = 0>
18284  friend bool operator>=(const ScalarType lhs, const_reference rhs) noexcept
18285  {
18286  return (basic_json(lhs) >= rhs);
18287  }
18289  /// @}
18290 
18291  ///////////////////
18292  // serialization //
18293  ///////////////////
18294 
18295  /// @name serialization
18296  /// @{
18297 
18298  /*!
18299  @brief serialize to stream
18300 
18301  Serialize the given JSON value @a j to the output stream @a o. The JSON
18302  value will be serialized using the @ref dump member function.
18303 
18304  - The indentation of the output can be controlled with the member variable
18305  `width` of the output stream @a o. For instance, using the manipulator
18306  `std::setw(4)` on @a o sets the indentation level to `4` and the
18307  serialization result is the same as calling `dump(4)`.
18308 
18309  - The indentation character can be controlled with the member variable
18310  `fill` of the output stream @a o. For instance, the manipulator
18311  `std::setfill('\\t')` sets indentation to use a tab character rather than
18312  the default space character.
18313 
18314  @param[in,out] o stream to serialize to
18315  @param[in] j JSON value to serialize
18316 
18317  @return the stream @a o
18318 
18319  @throw type_error.316 if a string stored inside the JSON value is not
18320  UTF-8 encoded
18321 
18322  @complexity Linear.
18323 
18324  @liveexample{The example below shows the serialization with different
18325  parameters to `width` to adjust the indentation level.,operator_serialize}
18326 
18327  @since version 1.0.0; indentation character added in version 3.0.0
18328  */
18329  friend std::ostream& operator<<(std::ostream& o, const basic_json& j)
18330  {
18331  // read width member and use it as indentation parameter if nonzero
18332  const bool pretty_print = (o.width() > 0);
18333  const auto indentation = (pretty_print ? o.width() : 0);
18334 
18335  // reset width to 0 for subsequent calls to this stream
18336  o.width(0);
18337 
18338  // do the actual serialization
18339  serializer s(detail::output_adapter<char>(o), o.fill());
18340  s.dump(j, pretty_print, false, static_cast<unsigned int>(indentation));
18341  return o;
18342  }
18343 
18344  /*!
18345  @brief serialize to stream
18346  @deprecated This stream operator is deprecated and will be removed in
18347  future 4.0.0 of the library. Please use
18348  @ref operator<<(std::ostream&, const basic_json&)
18349  instead; that is, replace calls like `j >> o;` with `o << j;`.
18350  @since version 1.0.0; deprecated since version 3.0.0
18351  */
18353  friend std::ostream& operator>>(const basic_json& j, std::ostream& o)
18354  {
18355  return o << j;
18356  }
18357 
18358  /// @}
18359 
18360 
18361  /////////////////////
18362  // deserialization //
18363  /////////////////////
18364 
18365  /// @name deserialization
18366  /// @{
18367 
18368  /*!
18369  @brief deserialize from a compatible input
18370 
18371  This function reads from a compatible input. Examples are:
18372  - an array of 1-byte values
18373  - strings with character/literal type with size of 1 byte
18374  - input streams
18375  - container with contiguous storage of 1-byte values. Compatible container
18376  types include `std::vector`, `std::string`, `std::array`,
18377  `std::valarray`, and `std::initializer_list`. Furthermore, C-style
18378  arrays can be used with `std::begin()`/`std::end()`. User-defined
18379  containers can be used as long as they implement random-access iterators
18380  and a contiguous storage.
18381 
18382  @pre Each element of the container has a size of 1 byte. Violating this
18383  precondition yields undefined behavior. **This precondition is enforced
18384  with a static assertion.**
18385 
18386  @pre The container storage is contiguous. Violating this precondition
18387  yields undefined behavior. **This precondition is enforced with an
18388  assertion.**
18389  @pre Each element of the container has a size of 1 byte. Violating this
18390  precondition yields undefined behavior. **This precondition is enforced
18391  with a static assertion.**
18392 
18393  @warning There is no way to enforce all preconditions at compile-time. If
18394  the function is called with a noncompliant container and with
18395  assertions switched off, the behavior is undefined and will most
18396  likely yield segmentation violation.
18397 
18398  @param[in] i input to read from
18399  @param[in] cb a parser callback function of type @ref parser_callback_t
18400  which is used to control the deserialization by filtering unwanted values
18401  (optional)
18402  @param[in] allow_exceptions whether to throw exceptions in case of a
18403  parse error (optional, true by default)
18404 
18405  @return result of the deserialization
18406 
18407  @throw parse_error.101 if a parse error occurs; example: `""unexpected end
18408  of input; expected string literal""`
18409  @throw parse_error.102 if to_unicode fails or surrogate error
18410  @throw parse_error.103 if to_unicode fails
18411 
18412  @complexity Linear in the length of the input. The parser is a predictive
18413  LL(1) parser. The complexity can be higher if the parser callback function
18414  @a cb has a super-linear complexity.
18415 
18416  @note A UTF-8 byte order mark is silently ignored.
18417 
18418  @liveexample{The example below demonstrates the `parse()` function reading
18419  from an array.,parse__array__parser_callback_t}
18420 
18421  @liveexample{The example below demonstrates the `parse()` function with
18422  and without callback function.,parse__string__parser_callback_t}
18423 
18424  @liveexample{The example below demonstrates the `parse()` function with
18425  and without callback function.,parse__istream__parser_callback_t}
18426 
18427  @liveexample{The example below demonstrates the `parse()` function reading
18428  from a contiguous container.,parse__contiguouscontainer__parser_callback_t}
18429 
18430  @since version 2.0.3 (contiguous containers)
18431  */
18433  const parser_callback_t cb = nullptr,
18434  const bool allow_exceptions = true)
18435  {
18437  parser(i, cb, allow_exceptions).parse(true, result);
18438  return result;
18439  }
18440 
18441  static bool accept(detail::input_adapter&& i)
18442  {
18443  return parser(i).accept(true);
18444  }
18445 
18446  /*!
18447  @brief generate SAX events
18448 
18449  The SAX event lister must follow the interface of @ref json_sax.
18450 
18451  This function reads from a compatible input. Examples are:
18452  - an array of 1-byte values
18453  - strings with character/literal type with size of 1 byte
18454  - input streams
18455  - container with contiguous storage of 1-byte values. Compatible container
18456  types include `std::vector`, `std::string`, `std::array`,
18457  `std::valarray`, and `std::initializer_list`. Furthermore, C-style
18458  arrays can be used with `std::begin()`/`std::end()`. User-defined
18459  containers can be used as long as they implement random-access iterators
18460  and a contiguous storage.
18461 
18462  @pre Each element of the container has a size of 1 byte. Violating this
18463  precondition yields undefined behavior. **This precondition is enforced
18464  with a static assertion.**
18465 
18466  @pre The container storage is contiguous. Violating this precondition
18467  yields undefined behavior. **This precondition is enforced with an
18468  assertion.**
18469  @pre Each element of the container has a size of 1 byte. Violating this
18470  precondition yields undefined behavior. **This precondition is enforced
18471  with a static assertion.**
18472 
18473  @warning There is no way to enforce all preconditions at compile-time. If
18474  the function is called with a noncompliant container and with
18475  assertions switched off, the behavior is undefined and will most
18476  likely yield segmentation violation.
18477 
18478  @param[in] i input to read from
18479  @param[in,out] sax SAX event listener
18480  @param[in] format the format to parse (JSON, CBOR, MessagePack, or UBJSON)
18481  @param[in] strict whether the input has to be consumed completely
18482 
18483  @return return value of the last processed SAX event
18484 
18485  @throw parse_error.101 if a parse error occurs; example: `""unexpected end
18486  of input; expected string literal""`
18487  @throw parse_error.102 if to_unicode fails or surrogate error
18488  @throw parse_error.103 if to_unicode fails
18489 
18490  @complexity Linear in the length of the input. The parser is a predictive
18491  LL(1) parser. The complexity can be higher if the SAX consumer @a sax has
18492  a super-linear complexity.
18493 
18494  @note A UTF-8 byte order mark is silently ignored.
18495 
18496  @liveexample{The example below demonstrates the `sax_parse()` function
18497  reading from string and processing the events with a user-defined SAX
18498  event consumer.,sax_parse}
18499 
18500  @since version 3.2.0
18501  */
18502  template <typename SAX>
18503  static bool sax_parse(detail::input_adapter&& i, SAX* sax,
18505  const bool strict = true)
18506  {
18507  assert(sax);
18508  switch (format)
18509  {
18510  case input_format_t::json:
18511  return parser(std::move(i)).sax_parse(sax, strict);
18512  default:
18514  }
18515  }
18516 
18517  /*!
18518  @brief deserialize from an iterator range with contiguous storage
18519 
18520  This function reads from an iterator range of a container with contiguous
18521  storage of 1-byte values. Compatible container types include
18522  `std::vector`, `std::string`, `std::array`, `std::valarray`, and
18523  `std::initializer_list`. Furthermore, C-style arrays can be used with
18524  `std::begin()`/`std::end()`. User-defined containers can be used as long
18525  as they implement random-access iterators and a contiguous storage.
18526 
18527  @pre The iterator range is contiguous. Violating this precondition yields
18528  undefined behavior. **This precondition is enforced with an assertion.**
18529  @pre Each element in the range has a size of 1 byte. Violating this
18530  precondition yields undefined behavior. **This precondition is enforced
18531  with a static assertion.**
18532 
18533  @warning There is no way to enforce all preconditions at compile-time. If
18534  the function is called with noncompliant iterators and with
18535  assertions switched off, the behavior is undefined and will most
18536  likely yield segmentation violation.
18537 
18538  @tparam IteratorType iterator of container with contiguous storage
18539  @param[in] first begin of the range to parse (included)
18540  @param[in] last end of the range to parse (excluded)
18541  @param[in] cb a parser callback function of type @ref parser_callback_t
18542  which is used to control the deserialization by filtering unwanted values
18543  (optional)
18544  @param[in] allow_exceptions whether to throw exceptions in case of a
18545  parse error (optional, true by default)
18546 
18547  @return result of the deserialization
18548 
18549  @throw parse_error.101 in case of an unexpected token
18550  @throw parse_error.102 if to_unicode fails or surrogate error
18551  @throw parse_error.103 if to_unicode fails
18552 
18553  @complexity Linear in the length of the input. The parser is a predictive
18554  LL(1) parser. The complexity can be higher if the parser callback function
18555  @a cb has a super-linear complexity.
18556 
18557  @note A UTF-8 byte order mark is silently ignored.
18558 
18559  @liveexample{The example below demonstrates the `parse()` function reading
18560  from an iterator range.,parse__iteratortype__parser_callback_t}
18561 
18562  @since version 2.0.3
18563  */
18564  template<class IteratorType, typename std::enable_if<
18565  std::is_base_of<
18567  typename std::iterator_traits<IteratorType>::iterator_category>::value, int>::type = 0>
18569  const parser_callback_t cb = nullptr,
18570  const bool allow_exceptions = true)
18571  {
18574  return result;
18575  }
18576 
18577  template<class IteratorType, typename std::enable_if<
18578  std::is_base_of<
18580  typename std::iterator_traits<IteratorType>::iterator_category>::value, int>::type = 0>
18581  static bool accept(IteratorType first, IteratorType last)
18582  {
18584  }
18585 
18586  template<class IteratorType, class SAX, typename std::enable_if<
18587  std::is_base_of<
18589  typename std::iterator_traits<IteratorType>::iterator_category>::value, int>::type = 0>
18591  {
18593  }
18594 
18595  /*!
18596  @brief deserialize from stream
18597  @deprecated This stream operator is deprecated and will be removed in
18598  version 4.0.0 of the library. Please use
18599  @ref operator>>(std::istream&, basic_json&)
18600  instead; that is, replace calls like `j << i;` with `i >> j;`.
18601  @since version 1.0.0; deprecated since version 3.0.0
18602  */
18604  friend std::istream& operator<<(basic_json& j, std::istream& i)
18605  {
18606  return operator>>(i, j);
18607  }
18608 
18609  /*!
18610  @brief deserialize from stream
18611 
18612  Deserializes an input stream to a JSON value.
18613 
18614  @param[in,out] i input stream to read a serialized JSON value from
18615  @param[in,out] j JSON value to write the deserialized input to
18616 
18617  @throw parse_error.101 in case of an unexpected token
18618  @throw parse_error.102 if to_unicode fails or surrogate error
18619  @throw parse_error.103 if to_unicode fails
18620 
18621  @complexity Linear in the length of the input. The parser is a predictive
18622  LL(1) parser.
18623 
18624  @note A UTF-8 byte order mark is silently ignored.
18625 
18626  @liveexample{The example below shows how a JSON value is constructed by
18627  reading a serialization from a stream.,operator_deserialize}
18628 
18629  @sa parse(std::istream&, const parser_callback_t) for a variant with a
18630  parser callback function to filter values while parsing
18631 
18632  @since version 1.0.0
18633  */
18634  friend std::istream& operator>>(std::istream& i, basic_json& j)
18635  {
18636  parser(detail::input_adapter(i)).parse(false, j);
18637  return i;
18638  }
18639 
18640  /// @}
18641 
18642  ///////////////////////////
18643  // convenience functions //
18644  ///////////////////////////
18645 
18646  /*!
18647  @brief return the type as string
18648 
18649  Returns the type name as string to be used in error messages - usually to
18650  indicate that a function was called on a wrong JSON type.
18651 
18652  @return a string representation of a the @a m_type member:
18653  Value type | return value
18654  ----------- | -------------
18655  null | `"null"`
18656  boolean | `"boolean"`
18657  string | `"string"`
18658  number | `"number"` (for all number types)
18659  object | `"object"`
18660  array | `"array"`
18661  discarded | `"discarded"`
18662 
18663  @exceptionsafety No-throw guarantee: this function never throws exceptions.
18664 
18665  @complexity Constant.
18666 
18667  @liveexample{The following code exemplifies `type_name()` for all JSON
18668  types.,type_name}
18669 
18670  @sa @ref type() -- return the type of the JSON value
18671  @sa @ref operator value_t() -- return the type of the JSON value (implicit)
18672 
18673  @since version 1.0.0, public since 2.1.0, `const char*` and `noexcept`
18674  since 3.0.0
18675  */
18676  const char* type_name() const noexcept
18677  {
18678  {
18679  switch (m_type)
18680  {
18681  case value_t::null:
18682  return "null";
18683  case value_t::object:
18684  return "object";
18685  case value_t::array:
18686  return "array";
18687  case value_t::string:
18688  return "string";
18689  case value_t::boolean:
18690  return "boolean";
18692  return "discarded";
18693  default:
18694  return "number";
18695  }
18696  }
18697  }
18698 
18699 
18700  private:
18701  //////////////////////
18702  // member variables //
18703  //////////////////////
18704 
18705  /// the type of the current element
18707 
18708  /// the value of the current element
18709  json_value m_value = {};
18710 
18711  //////////////////////////////////////////
18712  // binary serialization/deserialization //
18713  //////////////////////////////////////////
18714 
18715  /// @name binary serialization/deserialization support
18716  /// @{
18717 
18718  public:
18719  /*!
18720  @brief create a CBOR serialization of a given JSON value
18721 
18722  Serializes a given JSON value @a j to a byte vector using the CBOR (Concise
18723  Binary Object Representation) serialization format. CBOR is a binary
18724  serialization format which aims to be more compact than JSON itself, yet
18725  more efficient to parse.
18726 
18727  The library uses the following mapping from JSON values types to
18728  CBOR types according to the CBOR specification (RFC 7049):
18729 
18730  JSON value type | value/range | CBOR type | first byte
18731  --------------- | ------------------------------------------ | ---------------------------------- | ---------------
18732  null | `null` | Null | 0xF6
18733  boolean | `true` | True | 0xF5
18734  boolean | `false` | False | 0xF4
18735  number_integer | -9223372036854775808..-2147483649 | Negative integer (8 bytes follow) | 0x3B
18736  number_integer | -2147483648..-32769 | Negative integer (4 bytes follow) | 0x3A
18737  number_integer | -32768..-129 | Negative integer (2 bytes follow) | 0x39
18738  number_integer | -128..-25 | Negative integer (1 byte follow) | 0x38
18739  number_integer | -24..-1 | Negative integer | 0x20..0x37
18740  number_integer | 0..23 | Integer | 0x00..0x17
18741  number_integer | 24..255 | Unsigned integer (1 byte follow) | 0x18
18742  number_integer | 256..65535 | Unsigned integer (2 bytes follow) | 0x19
18743  number_integer | 65536..4294967295 | Unsigned integer (4 bytes follow) | 0x1A
18744  number_integer | 4294967296..18446744073709551615 | Unsigned integer (8 bytes follow) | 0x1B
18745  number_unsigned | 0..23 | Integer | 0x00..0x17
18746  number_unsigned | 24..255 | Unsigned integer (1 byte follow) | 0x18
18747  number_unsigned | 256..65535 | Unsigned integer (2 bytes follow) | 0x19
18748  number_unsigned | 65536..4294967295 | Unsigned integer (4 bytes follow) | 0x1A
18749  number_unsigned | 4294967296..18446744073709551615 | Unsigned integer (8 bytes follow) | 0x1B
18750  number_float | *any value* | Double-Precision Float | 0xFB
18751  string | *length*: 0..23 | UTF-8 string | 0x60..0x77
18752  string | *length*: 23..255 | UTF-8 string (1 byte follow) | 0x78
18753  string | *length*: 256..65535 | UTF-8 string (2 bytes follow) | 0x79
18754  string | *length*: 65536..4294967295 | UTF-8 string (4 bytes follow) | 0x7A
18755  string | *length*: 4294967296..18446744073709551615 | UTF-8 string (8 bytes follow) | 0x7B
18756  array | *size*: 0..23 | array | 0x80..0x97
18757  array | *size*: 23..255 | array (1 byte follow) | 0x98
18758  array | *size*: 256..65535 | array (2 bytes follow) | 0x99
18759  array | *size*: 65536..4294967295 | array (4 bytes follow) | 0x9A
18760  array | *size*: 4294967296..18446744073709551615 | array (8 bytes follow) | 0x9B
18761  object | *size*: 0..23 | map | 0xA0..0xB7
18762  object | *size*: 23..255 | map (1 byte follow) | 0xB8
18763  object | *size*: 256..65535 | map (2 bytes follow) | 0xB9
18764  object | *size*: 65536..4294967295 | map (4 bytes follow) | 0xBA
18765  object | *size*: 4294967296..18446744073709551615 | map (8 bytes follow) | 0xBB
18766 
18767  @note The mapping is **complete** in the sense that any JSON value type
18768  can be converted to a CBOR value.
18769 
18770  @note If NaN or Infinity are stored inside a JSON number, they are
18771  serialized properly. This behavior differs from the @ref dump()
18772  function which serializes NaN or Infinity to `null`.
18773 
18774  @note The following CBOR types are not used in the conversion:
18775  - byte strings (0x40..0x5F)
18776  - UTF-8 strings terminated by "break" (0x7F)
18777  - arrays terminated by "break" (0x9F)
18778  - maps terminated by "break" (0xBF)
18779  - date/time (0xC0..0xC1)
18780  - bignum (0xC2..0xC3)
18781  - decimal fraction (0xC4)
18782  - bigfloat (0xC5)
18783  - tagged items (0xC6..0xD4, 0xD8..0xDB)
18784  - expected conversions (0xD5..0xD7)
18785  - simple values (0xE0..0xF3, 0xF8)
18786  - undefined (0xF7)
18787  - half and single-precision floats (0xF9-0xFA)
18788  - break (0xFF)
18789 
18790  @param[in] j JSON value to serialize
18791  @return MessagePack serialization as byte vector
18792 
18793  @complexity Linear in the size of the JSON value @a j.
18794 
18795  @liveexample{The example shows the serialization of a JSON value to a byte
18796  vector in CBOR format.,to_cbor}
18797 
18798  @sa http://cbor.io
18799  @sa @ref from_cbor(detail::input_adapter&&, const bool, const bool) for the
18800  analogous deserialization
18801  @sa @ref to_msgpack(const basic_json&) for the related MessagePack format
18802  @sa @ref to_ubjson(const basic_json&, const bool, const bool) for the
18803  related UBJSON format
18804 
18805  @since version 2.0.9
18806  */
18807  static std::vector<uint8_t> to_cbor(const basic_json& j)
18808  {
18809  std::vector<uint8_t> result;
18810  to_cbor(j, result);
18811  return result;
18812  }
18813 
18814  static void to_cbor(const basic_json& j, detail::output_adapter<uint8_t> o)
18815  {
18816  binary_writer<uint8_t>(o).write_cbor(j);
18817  }
18818 
18819  static void to_cbor(const basic_json& j, detail::output_adapter<char> o)
18820  {
18821  binary_writer<char>(o).write_cbor(j);
18822  }
18823 
18824  /*!
18825  @brief create a MessagePack serialization of a given JSON value
18826 
18827  Serializes a given JSON value @a j to a byte vector using the MessagePack
18828  serialization format. MessagePack is a binary serialization format which
18829  aims to be more compact than JSON itself, yet more efficient to parse.
18830 
18831  The library uses the following mapping from JSON values types to
18832  MessagePack types according to the MessagePack specification:
18833 
18834  JSON value type | value/range | MessagePack type | first byte
18835  --------------- | --------------------------------- | ---------------- | ----------
18836  null | `null` | nil | 0xC0
18837  boolean | `true` | true | 0xC3
18838  boolean | `false` | false | 0xC2
18839  number_integer | -9223372036854775808..-2147483649 | int64 | 0xD3
18840  number_integer | -2147483648..-32769 | int32 | 0xD2
18841  number_integer | -32768..-129 | int16 | 0xD1
18842  number_integer | -128..-33 | int8 | 0xD0
18843  number_integer | -32..-1 | negative fixint | 0xE0..0xFF
18844  number_integer | 0..127 | positive fixint | 0x00..0x7F
18845  number_integer | 128..255 | uint 8 | 0xCC
18846  number_integer | 256..65535 | uint 16 | 0xCD
18847  number_integer | 65536..4294967295 | uint 32 | 0xCE
18848  number_integer | 4294967296..18446744073709551615 | uint 64 | 0xCF
18849  number_unsigned | 0..127 | positive fixint | 0x00..0x7F
18850  number_unsigned | 128..255 | uint 8 | 0xCC
18851  number_unsigned | 256..65535 | uint 16 | 0xCD
18852  number_unsigned | 65536..4294967295 | uint 32 | 0xCE
18853  number_unsigned | 4294967296..18446744073709551615 | uint 64 | 0xCF
18854  number_float | *any value* | float 64 | 0xCB
18855  string | *length*: 0..31 | fixstr | 0xA0..0xBF
18856  string | *length*: 32..255 | str 8 | 0xD9
18857  string | *length*: 256..65535 | str 16 | 0xDA
18858  string | *length*: 65536..4294967295 | str 32 | 0xDB
18859  array | *size*: 0..15 | fixarray | 0x90..0x9F
18860  array | *size*: 16..65535 | array 16 | 0xDC
18861  array | *size*: 65536..4294967295 | array 32 | 0xDD
18862  object | *size*: 0..15 | fix map | 0x80..0x8F
18863  object | *size*: 16..65535 | map 16 | 0xDE
18864  object | *size*: 65536..4294967295 | map 32 | 0xDF
18865 
18866  @note The mapping is **complete** in the sense that any JSON value type
18867  can be converted to a MessagePack value.
18868 
18869  @note The following values can **not** be converted to a MessagePack value:
18870  - strings with more than 4294967295 bytes
18871  - arrays with more than 4294967295 elements
18872  - objects with more than 4294967295 elements
18873 
18874  @note The following MessagePack types are not used in the conversion:
18875  - bin 8 - bin 32 (0xC4..0xC6)
18876  - ext 8 - ext 32 (0xC7..0xC9)
18877  - float 32 (0xCA)
18878  - fixext 1 - fixext 16 (0xD4..0xD8)
18879 
18880  @note Any MessagePack output created @ref to_msgpack can be successfully
18881  parsed by @ref from_msgpack.
18882 
18883  @note If NaN or Infinity are stored inside a JSON number, they are
18884  serialized properly. This behavior differs from the @ref dump()
18885  function which serializes NaN or Infinity to `null`.
18886 
18887  @param[in] j JSON value to serialize
18888  @return MessagePack serialization as byte vector
18889 
18890  @complexity Linear in the size of the JSON value @a j.
18891 
18892  @liveexample{The example shows the serialization of a JSON value to a byte
18893  vector in MessagePack format.,to_msgpack}
18894 
18895  @sa http://msgpack.org
18896  @sa @ref from_msgpack for the analogous deserialization
18897  @sa @ref to_cbor(const basic_json& for the related CBOR format
18898  @sa @ref to_ubjson(const basic_json&, const bool, const bool) for the
18899  related UBJSON format
18900 
18901  @since version 2.0.9
18902  */
18903  static std::vector<uint8_t> to_msgpack(const basic_json& j)
18904  {
18905  std::vector<uint8_t> result;
18906  to_msgpack(j, result);
18907  return result;
18908  }
18909 
18910  static void to_msgpack(const basic_json& j, detail::output_adapter<uint8_t> o)
18911  {
18912  binary_writer<uint8_t>(o).write_msgpack(j);
18913  }
18914 
18915  static void to_msgpack(const basic_json& j, detail::output_adapter<char> o)
18916  {
18917  binary_writer<char>(o).write_msgpack(j);
18918  }
18919 
18920  /*!
18921  @brief create a UBJSON serialization of a given JSON value
18922 
18923  Serializes a given JSON value @a j to a byte vector using the UBJSON
18924  (Universal Binary JSON) serialization format. UBJSON aims to be more compact
18925  than JSON itself, yet more efficient to parse.
18926 
18927  The library uses the following mapping from JSON values types to
18928  UBJSON types according to the UBJSON specification:
18929 
18930  JSON value type | value/range | UBJSON type | marker
18931  --------------- | --------------------------------- | ----------- | ------
18932  null | `null` | null | `Z`
18933  boolean | `true` | true | `T`
18934  boolean | `false` | false | `F`
18935  number_integer | -9223372036854775808..-2147483649 | int64 | `L`
18936  number_integer | -2147483648..-32769 | int32 | `l`
18937  number_integer | -32768..-129 | int16 | `I`
18938  number_integer | -128..127 | int8 | `i`
18939  number_integer | 128..255 | uint8 | `U`
18940  number_integer | 256..32767 | int16 | `I`
18941  number_integer | 32768..2147483647 | int32 | `l`
18942  number_integer | 2147483648..9223372036854775807 | int64 | `L`
18943  number_unsigned | 0..127 | int8 | `i`
18944  number_unsigned | 128..255 | uint8 | `U`
18945  number_unsigned | 256..32767 | int16 | `I`
18946  number_unsigned | 32768..2147483647 | int32 | `l`
18947  number_unsigned | 2147483648..9223372036854775807 | int64 | `L`
18948  number_float | *any value* | float64 | `D`
18949  string | *with shortest length indicator* | string | `S`
18950  array | *see notes on optimized format* | array | `[`
18951  object | *see notes on optimized format* | map | `{`
18952 
18953  @note The mapping is **complete** in the sense that any JSON value type
18954  can be converted to a UBJSON value.
18955 
18956  @note The following values can **not** be converted to a UBJSON value:
18957  - strings with more than 9223372036854775807 bytes (theoretical)
18958  - unsigned integer numbers above 9223372036854775807
18959 
18960  @note The following markers are not used in the conversion:
18961  - `Z`: no-op values are not created.
18962  - `C`: single-byte strings are serialized with `S` markers.
18963 
18964  @note Any UBJSON output created @ref to_ubjson can be successfully parsed
18965  by @ref from_ubjson.
18966 
18967  @note If NaN or Infinity are stored inside a JSON number, they are
18968  serialized properly. This behavior differs from the @ref dump()
18969  function which serializes NaN or Infinity to `null`.
18970 
18971  @note The optimized formats for containers are supported: Parameter
18972  @a use_size adds size information to the beginning of a container and
18973  removes the closing marker. Parameter @a use_type further checks
18974  whether all elements of a container have the same type and adds the
18975  type marker to the beginning of the container. The @a use_type
18976  parameter must only be used together with @a use_size = true. Note
18977  that @a use_size = true alone may result in larger representations -
18978  the benefit of this parameter is that the receiving side is
18979  immediately informed on the number of elements of the container.
18980 
18981  @param[in] j JSON value to serialize
18982  @param[in] use_size whether to add size annotations to container types
18983  @param[in] use_type whether to add type annotations to container types
18984  (must be combined with @a use_size = true)
18985  @return UBJSON serialization as byte vector
18986 
18987  @complexity Linear in the size of the JSON value @a j.
18988 
18989  @liveexample{The example shows the serialization of a JSON value to a byte
18990  vector in UBJSON format.,to_ubjson}
18991 
18992  @sa http://ubjson.org
18993  @sa @ref from_ubjson(detail::input_adapter&&, const bool, const bool) for the
18994  analogous deserialization
18995  @sa @ref to_cbor(const basic_json& for the related CBOR format
18996  @sa @ref to_msgpack(const basic_json&) for the related MessagePack format
18997 
18998  @since version 3.1.0
18999  */
19000  static std::vector<uint8_t> to_ubjson(const basic_json& j,
19001  const bool use_size = false,
19002  const bool use_type = false)
19003  {
19004  std::vector<uint8_t> result;
19005  to_ubjson(j, result, use_size, use_type);
19006  return result;
19007  }
19008 
19009  static void to_ubjson(const basic_json& j, detail::output_adapter<uint8_t> o,
19010  const bool use_size = false, const bool use_type = false)
19011  {
19012  binary_writer<uint8_t>(o).write_ubjson(j, use_size, use_type);
19013  }
19014 
19015  static void to_ubjson(const basic_json& j, detail::output_adapter<char> o,
19016  const bool use_size = false, const bool use_type = false)
19017  {
19018  binary_writer<char>(o).write_ubjson(j, use_size, use_type);
19019  }
19020 
19021 
19022  /*!
19023  @brief Serializes the given JSON object `j` to BSON and returns a vector
19024  containing the corresponding BSON-representation.
19025 
19026  BSON (Binary JSON) is a binary format in which zero or more ordered key/value pairs are
19027  stored as a single entity (a so-called document).
19028 
19029  The library uses the following mapping from JSON values types to BSON types:
19031  JSON value type | value/range | BSON type | marker
19032  --------------- | --------------------------------- | ----------- | ------
19033  null | `null` | null | 0x0A
19034  boolean | `true`, `false` | boolean | 0x08
19035  number_integer | -9223372036854775808..-2147483649 | int64 | 0x12
19036  number_integer | -2147483648..2147483647 | int32 | 0x10
19037  number_integer | 2147483648..9223372036854775807 | int64 | 0x12
19038  number_unsigned | 0..2147483647 | int32 | 0x10
19039  number_unsigned | 2147483648..9223372036854775807 | int64 | 0x12
19040  number_unsigned | 9223372036854775808..18446744073709551615| -- | --
19041  number_float | *any value* | double | 0x01
19042  string | *any value* | string | 0x02
19043  array | *any value* | document | 0x04
19044  object | *any value* | document | 0x03
19045 
19046  @warning The mapping is **incomplete**, since only JSON-objects (and things
19047  contained therein) can be serialized to BSON.
19048  Also, integers larger than 9223372036854775807 cannot be serialized to BSON,
19049  and the keys may not contain U+0000, since they are serialized a
19050  zero-terminated c-strings.
19051 
19052  @throw out_of_range.407 if `j.is_number_unsigned() && j.get<std::uint64_t>() > 9223372036854775807`
19053  @throw out_of_range.409 if a key in `j` contains a NULL (U+0000)
19054  @throw type_error.317 if `!j.is_object()`
19055 
19056  @pre The input `j` is required to be an object: `j.is_object() == true`.
19057 
19058  @note Any BSON output created via @ref to_bson can be successfully parsed
19059  by @ref from_bson.
19060 
19061  @param[in] j JSON value to serialize
19062  @return BSON serialization as byte vector
19063 
19064  @complexity Linear in the size of the JSON value @a j.
19065 
19066  @liveexample{The example shows the serialization of a JSON value to a byte
19067  vector in BSON format.,to_bson}
19068 
19069  @sa http://bsonspec.org/spec.html
19070  @sa @ref from_bson(detail::input_adapter&&, const bool strict) for the
19071  analogous deserialization
19072  @sa @ref to_ubjson(const basic_json&, const bool, const bool) for the
19073  related UBJSON format
19074  @sa @ref to_cbor(const basic_json&) for the related CBOR format
19075  @sa @ref to_msgpack(const basic_json&) for the related MessagePack format
19076  */
19077  static std::vector<uint8_t> to_bson(const basic_json& j)
19078  {
19079  std::vector<uint8_t> result;
19080  to_bson(j, result);
19081  return result;
19082  }
19083 
19084  /*!
19085  @brief Serializes the given JSON object `j` to BSON and forwards the
19086  corresponding BSON-representation to the given output_adapter `o`.
19087  @param j The JSON object to convert to BSON.
19088  @param o The output adapter that receives the binary BSON representation.
19089  @pre The input `j` shall be an object: `j.is_object() == true`
19090  @sa @ref to_bson(const basic_json&)
19091  */
19092  static void to_bson(const basic_json& j, detail::output_adapter<uint8_t> o)
19093  {
19094  binary_writer<uint8_t>(o).write_bson(j);
19095  }
19096 
19097  /*!
19098  @copydoc to_bson(const basic_json&, detail::output_adapter<uint8_t>)
19099  */
19100  static void to_bson(const basic_json& j, detail::output_adapter<char> o)
19101  {
19102  binary_writer<char>(o).write_bson(j);
19103  }
19104 
19105 
19106  /*!
19107  @brief create a JSON value from an input in CBOR format
19108 
19109  Deserializes a given input @a i to a JSON value using the CBOR (Concise
19110  Binary Object Representation) serialization format.
19111 
19112  The library maps CBOR types to JSON value types as follows:
19113 
19114  CBOR type | JSON value type | first byte
19115  ---------------------- | --------------- | ----------
19116  Integer | number_unsigned | 0x00..0x17
19117  Unsigned integer | number_unsigned | 0x18
19118  Unsigned integer | number_unsigned | 0x19
19119  Unsigned integer | number_unsigned | 0x1A
19120  Unsigned integer | number_unsigned | 0x1B
19121  Negative integer | number_integer | 0x20..0x37
19122  Negative integer | number_integer | 0x38
19123  Negative integer | number_integer | 0x39
19124  Negative integer | number_integer | 0x3A
19125  Negative integer | number_integer | 0x3B
19126  Negative integer | number_integer | 0x40..0x57
19127  UTF-8 string | string | 0x60..0x77
19128  UTF-8 string | string | 0x78
19129  UTF-8 string | string | 0x79
19130  UTF-8 string | string | 0x7A
19131  UTF-8 string | string | 0x7B
19132  UTF-8 string | string | 0x7F
19133  array | array | 0x80..0x97
19134  array | array | 0x98
19135  array | array | 0x99
19136  array | array | 0x9A
19137  array | array | 0x9B
19138  array | array | 0x9F
19139  map | object | 0xA0..0xB7
19140  map | object | 0xB8
19141  map | object | 0xB9
19142  map | object | 0xBA
19143  map | object | 0xBB
19144  map | object | 0xBF
19145  False | `false` | 0xF4
19146  True | `true` | 0xF5
19147  Null | `null` | 0xF6
19148  Half-Precision Float | number_float | 0xF9
19149  Single-Precision Float | number_float | 0xFA
19150  Double-Precision Float | number_float | 0xFB
19151 
19152  @warning The mapping is **incomplete** in the sense that not all CBOR
19153  types can be converted to a JSON value. The following CBOR types
19154  are not supported and will yield parse errors (parse_error.112):
19155  - byte strings (0x40..0x5F)
19156  - date/time (0xC0..0xC1)
19157  - bignum (0xC2..0xC3)
19158  - decimal fraction (0xC4)
19159  - bigfloat (0xC5)
19160  - tagged items (0xC6..0xD4, 0xD8..0xDB)
19161  - expected conversions (0xD5..0xD7)
19162  - simple values (0xE0..0xF3, 0xF8)
19163  - undefined (0xF7)
19164 
19165  @warning CBOR allows map keys of any type, whereas JSON only allows
19166  strings as keys in object values. Therefore, CBOR maps with keys
19167  other than UTF-8 strings are rejected (parse_error.113).
19168 
19169  @note Any CBOR output created @ref to_cbor can be successfully parsed by
19170  @ref from_cbor.
19171 
19172  @param[in] i an input in CBOR format convertible to an input adapter
19173  @param[in] strict whether to expect the input to be consumed until EOF
19174  (true by default)
19175  @param[in] allow_exceptions whether to throw exceptions in case of a
19176  parse error (optional, true by default)
19177 
19178  @return deserialized JSON value
19179 
19180  @throw parse_error.110 if the given input ends prematurely or the end of
19181  file was not reached when @a strict was set to true
19182  @throw parse_error.112 if unsupported features from CBOR were
19183  used in the given input @a v or if the input is not valid CBOR
19184  @throw parse_error.113 if a string was expected as map key, but not found
19185 
19186  @complexity Linear in the size of the input @a i.
19187 
19188  @liveexample{The example shows the deserialization of a byte vector in CBOR
19189  format to a JSON value.,from_cbor}
19190 
19191  @sa http://cbor.io
19192  @sa @ref to_cbor(const basic_json&) for the analogous serialization
19193  @sa @ref from_msgpack(detail::input_adapter&&, const bool, const bool) for the
19194  related MessagePack format
19195  @sa @ref from_ubjson(detail::input_adapter&&, const bool, const bool) for the
19196  related UBJSON format
19197 
19198  @since version 2.0.9; parameter @a start_index since 2.1.1; changed to
19199  consume input adapters, removed start_index parameter, and added
19200  @a strict parameter since 3.0.0; added @a allow_exceptions parameter
19201  since 3.2.0
19202  */
19203  static basic_json from_cbor(detail::input_adapter&& i,
19204  const bool strict = true,
19205  const bool allow_exceptions = true)
19206  {
19207  basic_json result;
19208  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
19209  const bool res = binary_reader(detail::input_adapter(i)).sax_parse(input_format_t::cbor, &sdp, strict);
19210  return res ? result : basic_json(value_t::discarded);
19211  }
19212 
19213  /*!
19214  @copydoc from_cbor(detail::input_adapter&&, const bool, const bool)
19215  */
19216  template<typename A1, typename A2,
19217  detail::enable_if_t<std::is_constructible<detail::input_adapter, A1, A2>::value, int> = 0>
19218  static basic_json from_cbor(A1 && a1, A2 && a2,
19219  const bool strict = true,
19220  const bool allow_exceptions = true)
19221  {
19222  basic_json result;
19223  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
19224  const bool res = binary_reader(detail::input_adapter(std::forward<A1>(a1), std::forward<A2>(a2))).sax_parse(input_format_t::cbor, &sdp, strict);
19225  return res ? result : basic_json(value_t::discarded);
19226  }
19227 
19228  /*!
19229  @brief create a JSON value from an input in MessagePack format
19230 
19231  Deserializes a given input @a i to a JSON value using the MessagePack
19232  serialization format.
19234  The library maps MessagePack types to JSON value types as follows:
19235 
19236  MessagePack type | JSON value type | first byte
19237  ---------------- | --------------- | ----------
19238  positive fixint | number_unsigned | 0x00..0x7F
19239  fixmap | object | 0x80..0x8F
19240  fixarray | array | 0x90..0x9F
19241  fixstr | string | 0xA0..0xBF
19242  nil | `null` | 0xC0
19243  false | `false` | 0xC2
19244  true | `true` | 0xC3
19245  float 32 | number_float | 0xCA
19246  float 64 | number_float | 0xCB
19247  uint 8 | number_unsigned | 0xCC
19248  uint 16 | number_unsigned | 0xCD
19249  uint 32 | number_unsigned | 0xCE
19250  uint 64 | number_unsigned | 0xCF
19251  int 8 | number_integer | 0xD0
19252  int 16 | number_integer | 0xD1
19253  int 32 | number_integer | 0xD2
19254  int 64 | number_integer | 0xD3
19255  str 8 | string | 0xD9
19256  str 16 | string | 0xDA
19257  str 32 | string | 0xDB
19258  array 16 | array | 0xDC
19259  array 32 | array | 0xDD
19260  map 16 | object | 0xDE
19261  map 32 | object | 0xDF
19262  negative fixint | number_integer | 0xE0-0xFF
19263 
19264  @warning The mapping is **incomplete** in the sense that not all
19265  MessagePack types can be converted to a JSON value. The following
19266  MessagePack types are not supported and will yield parse errors:
19267  - bin 8 - bin 32 (0xC4..0xC6)
19268  - ext 8 - ext 32 (0xC7..0xC9)
19269  - fixext 1 - fixext 16 (0xD4..0xD8)
19270 
19271  @note Any MessagePack output created @ref to_msgpack can be successfully
19272  parsed by @ref from_msgpack.
19273 
19274  @param[in] i an input in MessagePack format convertible to an input
19275  adapter
19276  @param[in] strict whether to expect the input to be consumed until EOF
19277  (true by default)
19278  @param[in] allow_exceptions whether to throw exceptions in case of a
19279  parse error (optional, true by default)
19280 
19281  @return deserialized JSON value
19282 
19283  @throw parse_error.110 if the given input ends prematurely or the end of
19284  file was not reached when @a strict was set to true
19285  @throw parse_error.112 if unsupported features from MessagePack were
19286  used in the given input @a i or if the input is not valid MessagePack
19287  @throw parse_error.113 if a string was expected as map key, but not found
19288 
19289  @complexity Linear in the size of the input @a i.
19290 
19291  @liveexample{The example shows the deserialization of a byte vector in
19292  MessagePack format to a JSON value.,from_msgpack}
19293 
19294  @sa http://msgpack.org
19295  @sa @ref to_msgpack(const basic_json&) for the analogous serialization
19296  @sa @ref from_cbor(detail::input_adapter&&, const bool, const bool) for the
19297  related CBOR format
19298  @sa @ref from_ubjson(detail::input_adapter&&, const bool, const bool) for
19299  the related UBJSON format
19300  @sa @ref from_bson(detail::input_adapter&&, const bool, const bool) for
19301  the related BSON format
19302 
19303  @since version 2.0.9; parameter @a start_index since 2.1.1; changed to
19304  consume input adapters, removed start_index parameter, and added
19305  @a strict parameter since 3.0.0; added @a allow_exceptions parameter
19306  since 3.2.0
19307  */
19308  static basic_json from_msgpack(detail::input_adapter&& i,
19309  const bool strict = true,
19310  const bool allow_exceptions = true)
19311  {
19312  basic_json result;
19313  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
19314  const bool res = binary_reader(detail::input_adapter(i)).sax_parse(input_format_t::msgpack, &sdp, strict);
19315  return res ? result : basic_json(value_t::discarded);
19316  }
19317 
19318  /*!
19319  @copydoc from_msgpack(detail::input_adapter&&, const bool, const bool)
19320  */
19321  template<typename A1, typename A2,
19322  detail::enable_if_t<std::is_constructible<detail::input_adapter, A1, A2>::value, int> = 0>
19323  static basic_json from_msgpack(A1 && a1, A2 && a2,
19324  const bool strict = true,
19325  const bool allow_exceptions = true)
19326  {
19327  basic_json result;
19328  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
19329  const bool res = binary_reader(detail::input_adapter(std::forward<A1>(a1), std::forward<A2>(a2))).sax_parse(input_format_t::msgpack, &sdp, strict);
19330  return res ? result : basic_json(value_t::discarded);
19331  }
19332 
19333  /*!
19334  @brief create a JSON value from an input in UBJSON format
19335 
19336  Deserializes a given input @a i to a JSON value using the UBJSON (Universal
19337  Binary JSON) serialization format.
19339  The library maps UBJSON types to JSON value types as follows:
19340 
19341  UBJSON type | JSON value type | marker
19342  ----------- | --------------------------------------- | ------
19343  no-op | *no value, next value is read* | `N`
19344  null | `null` | `Z`
19345  false | `false` | `F`
19346  true | `true` | `T`
19347  float32 | number_float | `d`
19348  float64 | number_float | `D`
19349  uint8 | number_unsigned | `U`
19350  int8 | number_integer | `i`
19351  int16 | number_integer | `I`
19352  int32 | number_integer | `l`
19353  int64 | number_integer | `L`
19354  string | string | `S`
19355  char | string | `C`
19356  array | array (optimized values are supported) | `[`
19357  object | object (optimized values are supported) | `{`
19358 
19359  @note The mapping is **complete** in the sense that any UBJSON value can
19360  be converted to a JSON value.
19361 
19362  @param[in] i an input in UBJSON format convertible to an input adapter
19363  @param[in] strict whether to expect the input to be consumed until EOF
19364  (true by default)
19365  @param[in] allow_exceptions whether to throw exceptions in case of a
19366  parse error (optional, true by default)
19367 
19368  @return deserialized JSON value
19369 
19370  @throw parse_error.110 if the given input ends prematurely or the end of
19371  file was not reached when @a strict was set to true
19372  @throw parse_error.112 if a parse error occurs
19373  @throw parse_error.113 if a string could not be parsed successfully
19374 
19375  @complexity Linear in the size of the input @a i.
19376 
19377  @liveexample{The example shows the deserialization of a byte vector in
19378  UBJSON format to a JSON value.,from_ubjson}
19379 
19380  @sa http://ubjson.org
19381  @sa @ref to_ubjson(const basic_json&, const bool, const bool) for the
19382  analogous serialization
19383  @sa @ref from_cbor(detail::input_adapter&&, const bool, const bool) for the
19384  related CBOR format
19385  @sa @ref from_msgpack(detail::input_adapter&&, const bool, const bool) for
19386  the related MessagePack format
19387  @sa @ref from_bson(detail::input_adapter&&, const bool, const bool) for
19388  the related BSON format
19389 
19390  @since version 3.1.0; added @a allow_exceptions parameter since 3.2.0
19391  */
19392  static basic_json from_ubjson(detail::input_adapter&& i,
19393  const bool strict = true,
19394  const bool allow_exceptions = true)
19395  {
19396  basic_json result;
19397  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
19398  const bool res = binary_reader(detail::input_adapter(i)).sax_parse(input_format_t::ubjson, &sdp, strict);
19399  return res ? result : basic_json(value_t::discarded);
19400  }
19401 
19402  /*!
19403  @copydoc from_ubjson(detail::input_adapter&&, const bool, const bool)
19404  */
19405  template<typename A1, typename A2,
19406  detail::enable_if_t<std::is_constructible<detail::input_adapter, A1, A2>::value, int> = 0>
19407  static basic_json from_ubjson(A1 && a1, A2 && a2,
19408  const bool strict = true,
19409  const bool allow_exceptions = true)
19410  {
19411  basic_json result;
19412  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
19413  const bool res = binary_reader(detail::input_adapter(std::forward<A1>(a1), std::forward<A2>(a2))).sax_parse(input_format_t::ubjson, &sdp, strict);
19414  return res ? result : basic_json(value_t::discarded);
19415  }
19416 
19417  /*!
19418  @brief Create a JSON value from an input in BSON format
19419 
19420  Deserializes a given input @a i to a JSON value using the BSON (Binary JSON)
19421  serialization format.
19423  The library maps BSON record types to JSON value types as follows:
19424 
19425  BSON type | BSON marker byte | JSON value type
19426  --------------- | ---------------- | ---------------------------
19427  double | 0x01 | number_float
19428  string | 0x02 | string
19429  document | 0x03 | object
19430  array | 0x04 | array
19431  binary | 0x05 | still unsupported
19432  undefined | 0x06 | still unsupported
19433  ObjectId | 0x07 | still unsupported
19434  boolean | 0x08 | boolean
19435  UTC Date-Time | 0x09 | still unsupported
19436  null | 0x0A | null
19437  Regular Expr. | 0x0B | still unsupported
19438  DB Pointer | 0x0C | still unsupported
19439  JavaScript Code | 0x0D | still unsupported
19440  Symbol | 0x0E | still unsupported
19441  JavaScript Code | 0x0F | still unsupported
19442  int32 | 0x10 | number_integer
19443  Timestamp | 0x11 | still unsupported
19444  128-bit decimal float | 0x13 | still unsupported
19445  Max Key | 0x7F | still unsupported
19446  Min Key | 0xFF | still unsupported
19447 
19448  @warning The mapping is **incomplete**. The unsupported mappings
19449  are indicated in the table above.
19450 
19451  @param[in] i an input in BSON format convertible to an input adapter
19452  @param[in] strict whether to expect the input to be consumed until EOF
19453  (true by default)
19454  @param[in] allow_exceptions whether to throw exceptions in case of a
19455  parse error (optional, true by default)
19456 
19457  @return deserialized JSON value
19458 
19459  @throw parse_error.114 if an unsupported BSON record type is encountered
19460 
19461  @complexity Linear in the size of the input @a i.
19462 
19463  @liveexample{The example shows the deserialization of a byte vector in
19464  BSON format to a JSON value.,from_bson}
19465 
19466  @sa http://bsonspec.org/spec.html
19467  @sa @ref to_bson(const basic_json&) for the analogous serialization
19468  @sa @ref from_cbor(detail::input_adapter&&, const bool, const bool) for the
19469  related CBOR format
19470  @sa @ref from_msgpack(detail::input_adapter&&, const bool, const bool) for
19471  the related MessagePack format
19472  @sa @ref from_ubjson(detail::input_adapter&&, const bool, const bool) for the
19473  related UBJSON format
19474  */
19475  static basic_json from_bson(detail::input_adapter&& i,
19476  const bool strict = true,
19477  const bool allow_exceptions = true)
19478  {
19479  basic_json result;
19480  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
19481  const bool res = binary_reader(detail::input_adapter(i)).sax_parse(input_format_t::bson, &sdp, strict);
19482  return res ? result : basic_json(value_t::discarded);
19483  }
19484 
19485  /*!
19486  @copydoc from_bson(detail::input_adapter&&, const bool, const bool)
19487  */
19488  template<typename A1, typename A2,
19489  detail::enable_if_t<std::is_constructible<detail::input_adapter, A1, A2>::value, int> = 0>
19490  static basic_json from_bson(A1 && a1, A2 && a2,
19491  const bool strict = true,
19492  const bool allow_exceptions = true)
19493  {
19494  basic_json result;
19495  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
19496  const bool res = binary_reader(detail::input_adapter(std::forward<A1>(a1), std::forward<A2>(a2))).sax_parse(input_format_t::bson, &sdp, strict);
19497  return res ? result : basic_json(value_t::discarded);
19498  }
19499 
19500 
19501 
19502  /// @}
19503 
19504  //////////////////////////
19505  // JSON Pointer support //
19506  //////////////////////////
19507 
19508  /// @name JSON Pointer functions
19509  /// @{
19510 
19511  /*!
19512  @brief access specified element via JSON Pointer
19513 
19514  Uses a JSON pointer to retrieve a reference to the respective JSON value.
19515  No bound checking is performed. Similar to @ref operator[](const typename
19516  object_t::key_type&), `null` values are created in arrays and objects if
19517  necessary.
19518 
19519  In particular:
19520  - If the JSON pointer points to an object key that does not exist, it
19521  is created an filled with a `null` value before a reference to it
19522  is returned.
19523  - If the JSON pointer points to an array index that does not exist, it
19524  is created an filled with a `null` value before a reference to it
19525  is returned. All indices between the current maximum and the given
19526  index are also filled with `null`.
19527  - The special value `-` is treated as a synonym for the index past the
19528  end.
19529 
19530  @param[in] ptr a JSON pointer
19531 
19532  @return reference to the element pointed to by @a ptr
19533 
19534  @complexity Constant.
19535 
19536  @throw parse_error.106 if an array index begins with '0'
19537  @throw parse_error.109 if an array index was not a number
19538  @throw out_of_range.404 if the JSON pointer can not be resolved
19539 
19540  @liveexample{The behavior is shown in the example.,operatorjson_pointer}
19541 
19542  @since version 2.0.0
19543  */
19544  reference operator[](const json_pointer& ptr)
19545  {
19546  return ptr.get_unchecked(this);
19547  }
19548 
19549  /*!
19550  @brief access specified element via JSON Pointer
19551 
19552  Uses a JSON pointer to retrieve a reference to the respective JSON value.
19553  No bound checking is performed. The function does not change the JSON
19554  value; no `null` values are created. In particular, the the special value
19555  `-` yields an exception.
19556 
19557  @param[in] ptr JSON pointer to the desired element
19558 
19559  @return const reference to the element pointed to by @a ptr
19560 
19561  @complexity Constant.
19562 
19563  @throw parse_error.106 if an array index begins with '0'
19564  @throw parse_error.109 if an array index was not a number
19565  @throw out_of_range.402 if the array index '-' is used
19566  @throw out_of_range.404 if the JSON pointer can not be resolved
19567 
19568  @liveexample{The behavior is shown in the example.,operatorjson_pointer_const}
19569 
19570  @since version 2.0.0
19571  */
19572  const_reference operator[](const json_pointer& ptr) const
19573  {
19574  return ptr.get_unchecked(this);
19575  }
19576 
19577  /*!
19578  @brief access specified element via JSON Pointer
19579 
19580  Returns a reference to the element at with specified JSON pointer @a ptr,
19581  with bounds checking.
19582 
19583  @param[in] ptr JSON pointer to the desired element
19584 
19585  @return reference to the element pointed to by @a ptr
19586 
19587  @throw parse_error.106 if an array index in the passed JSON pointer @a ptr
19588  begins with '0'. See example below.
19589 
19590  @throw parse_error.109 if an array index in the passed JSON pointer @a ptr
19591  is not a number. See example below.
19592 
19593  @throw out_of_range.401 if an array index in the passed JSON pointer @a ptr
19594  is out of range. See example below.
19595 
19596  @throw out_of_range.402 if the array index '-' is used in the passed JSON
19597  pointer @a ptr. As `at` provides checked access (and no elements are
19598  implicitly inserted), the index '-' is always invalid. See example below.
19599 
19600  @throw out_of_range.403 if the JSON pointer describes a key of an object
19601  which cannot be found. See example below.
19602 
19603  @throw out_of_range.404 if the JSON pointer @a ptr can not be resolved.
19604  See example below.
19605 
19606  @exceptionsafety Strong guarantee: if an exception is thrown, there are no
19607  changes in the JSON value.
19608 
19609  @complexity Constant.
19610 
19611  @since version 2.0.0
19612 
19613  @liveexample{The behavior is shown in the example.,at_json_pointer}
19614  */
19615  reference at(const json_pointer& ptr)
19616  {
19617  return ptr.get_checked(this);
19618  }
19619 
19620  /*!
19621  @brief access specified element via JSON Pointer
19622 
19623  Returns a const reference to the element at with specified JSON pointer @a
19624  ptr, with bounds checking.
19625 
19626  @param[in] ptr JSON pointer to the desired element
19627 
19628  @return reference to the element pointed to by @a ptr
19629 
19630  @throw parse_error.106 if an array index in the passed JSON pointer @a ptr
19631  begins with '0'. See example below.
19632 
19633  @throw parse_error.109 if an array index in the passed JSON pointer @a ptr
19634  is not a number. See example below.
19635 
19636  @throw out_of_range.401 if an array index in the passed JSON pointer @a ptr
19637  is out of range. See example below.
19638 
19639  @throw out_of_range.402 if the array index '-' is used in the passed JSON
19640  pointer @a ptr. As `at` provides checked access (and no elements are
19641  implicitly inserted), the index '-' is always invalid. See example below.
19642 
19643  @throw out_of_range.403 if the JSON pointer describes a key of an object
19644  which cannot be found. See example below.
19645 
19646  @throw out_of_range.404 if the JSON pointer @a ptr can not be resolved.
19647  See example below.
19648 
19649  @exceptionsafety Strong guarantee: if an exception is thrown, there are no
19650  changes in the JSON value.
19651 
19652  @complexity Constant.
19653 
19654  @since version 2.0.0
19655 
19656  @liveexample{The behavior is shown in the example.,at_json_pointer_const}
19657  */
19658  const_reference at(const json_pointer& ptr) const
19659  {
19660  return ptr.get_checked(this);
19661  }
19662 
19663  /*!
19664  @brief return flattened JSON value
19665 
19666  The function creates a JSON object whose keys are JSON pointers (see [RFC
19667  6901](https://tools.ietf.org/html/rfc6901)) and whose values are all
19668  primitive. The original JSON value can be restored using the @ref
19669  unflatten() function.
19670 
19671  @return an object that maps JSON pointers to primitive values
19672 
19673  @note Empty objects and arrays are flattened to `null` and will not be
19674  reconstructed correctly by the @ref unflatten() function.
19675 
19676  @complexity Linear in the size the JSON value.
19677 
19678  @liveexample{The following code shows how a JSON object is flattened to an
19679  object whose keys consist of JSON pointers.,flatten}
19680 
19681  @sa @ref unflatten() for the reverse function
19682 
19683  @since version 2.0.0
19684  */
19685  basic_json flatten() const
19686  {
19687  basic_json result(value_t::object);
19688  json_pointer::flatten("", *this, result);
19689  return result;
19690  }
19691 
19692  /*!
19693  @brief unflatten a previously flattened JSON value
19694 
19695  The function restores the arbitrary nesting of a JSON value that has been
19696  flattened before using the @ref flatten() function. The JSON value must
19697  meet certain constraints:
19698  1. The value must be an object.
19699  2. The keys must be JSON pointers (see
19700  [RFC 6901](https://tools.ietf.org/html/rfc6901))
19701  3. The mapped values must be primitive JSON types.
19702 
19703  @return the original JSON from a flattened version
19704 
19705  @note Empty objects and arrays are flattened by @ref flatten() to `null`
19706  values and can not unflattened to their original type. Apart from
19707  this example, for a JSON value `j`, the following is always true:
19708  `j == j.flatten().unflatten()`.
19709 
19710  @complexity Linear in the size the JSON value.
19711 
19712  @throw type_error.314 if value is not an object
19713  @throw type_error.315 if object values are not primitive
19714 
19715  @liveexample{The following code shows how a flattened JSON object is
19716  unflattened into the original nested JSON object.,unflatten}
19717 
19718  @sa @ref flatten() for the reverse function
19719 
19720  @since version 2.0.0
19721  */
19722  basic_json unflatten() const
19723  {
19724  return json_pointer::unflatten(*this);
19725  }
19726 
19727  /// @}
19728 
19729  //////////////////////////
19730  // JSON Patch functions //
19731  //////////////////////////
19732 
19733  /// @name JSON Patch functions
19734  /// @{
19735 
19736  /*!
19737  @brief applies a JSON patch
19738 
19739  [JSON Patch](http://jsonpatch.com) defines a JSON document structure for
19740  expressing a sequence of operations to apply to a JSON) document. With
19741  this function, a JSON Patch is applied to the current JSON value by
19742  executing all operations from the patch.
19743 
19744  @param[in] json_patch JSON patch document
19745  @return patched document
19746 
19747  @note The application of a patch is atomic: Either all operations succeed
19748  and the patched document is returned or an exception is thrown. In
19749  any case, the original value is not changed: the patch is applied
19750  to a copy of the value.
19751 
19752  @throw parse_error.104 if the JSON patch does not consist of an array of
19753  objects
19754 
19755  @throw parse_error.105 if the JSON patch is malformed (e.g., mandatory
19756  attributes are missing); example: `"operation add must have member path"`
19757 
19758  @throw out_of_range.401 if an array index is out of range.
19759 
19760  @throw out_of_range.403 if a JSON pointer inside the patch could not be
19761  resolved successfully in the current JSON value; example: `"key baz not
19762  found"`
19763 
19764  @throw out_of_range.405 if JSON pointer has no parent ("add", "remove",
19765  "move")
19766 
19767  @throw other_error.501 if "test" operation was unsuccessful
19768 
19769  @complexity Linear in the size of the JSON value and the length of the
19770  JSON patch. As usually only a fraction of the JSON value is affected by
19771  the patch, the complexity can usually be neglected.
19772 
19773  @liveexample{The following code shows how a JSON patch is applied to a
19774  value.,patch}
19775 
19776  @sa @ref diff -- create a JSON patch by comparing two JSON values
19777 
19778  @sa [RFC 6902 (JSON Patch)](https://tools.ietf.org/html/rfc6902)
19779  @sa [RFC 6901 (JSON Pointer)](https://tools.ietf.org/html/rfc6901)
19780 
19781  @since version 2.0.0
19782  */
19783  basic_json patch(const basic_json& json_patch) const
19784  {
19785  // make a working copy to apply the patch to
19786  basic_json result = *this;
19787 
19788  // the valid JSON Patch operations
19789  enum class patch_operations {add, remove, replace, move, copy, test, invalid};
19790 
19791  const auto get_op = [](const std::string & op)
19792  {
19793  if (op == "add")
19794  {
19795  return patch_operations::add;
19796  }
19797  if (op == "remove")
19798  {
19799  return patch_operations::remove;
19800  }
19801  if (op == "replace")
19802  {
19803  return patch_operations::replace;
19804  }
19805  if (op == "move")
19806  {
19807  return patch_operations::move;
19808  }
19809  if (op == "copy")
19810  {
19811  return patch_operations::copy;
19812  }
19813  if (op == "test")
19814  {
19815  return patch_operations::test;
19816  }
19817 
19818  return patch_operations::invalid;
19819  };
19820 
19821  // wrapper for "add" operation; add value at ptr
19822  const auto operation_add = [&result](json_pointer & ptr, basic_json val)
19823  {
19824  // adding to the root of the target document means replacing it
19825  if (ptr.is_root())
19826  {
19827  result = val;
19828  }
19829  else
19830  {
19831  // make sure the top element of the pointer exists
19832  json_pointer top_pointer = ptr.top();
19833  if (top_pointer != ptr)
19834  {
19835  result.at(top_pointer);
19836  }
19837 
19838  // get reference to parent of JSON pointer ptr
19839  const auto last_path = ptr.pop_back();
19840  basic_json& parent = result[ptr];
19841 
19842  switch (parent.m_type)
19843  {
19844  case value_t::null:
19845  case value_t::object:
19846  {
19847  // use operator[] to add value
19848  parent[last_path] = val;
19849  break;
19850  }
19851 
19852  case value_t::array:
19853  {
19854  if (last_path == "-")
19855  {
19856  // special case: append to back
19857  parent.push_back(val);
19858  }
19859  else
19860  {
19861  const auto idx = json_pointer::array_index(last_path);
19862  if (JSON_UNLIKELY(static_cast<size_type>(idx) > parent.size()))
19863  {
19864  // avoid undefined behavior
19865  JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range"));
19866  }
19867 
19868  // default case: insert add offset
19869  parent.insert(parent.begin() + static_cast<difference_type>(idx), val);
19870  }
19871  break;
19872  }
19873 
19874  // LCOV_EXCL_START
19875  default:
19876  {
19877  // if there exists a parent it cannot be primitive
19878  assert(false);
19879  }
19880  // LCOV_EXCL_STOP
19881  }
19882  }
19883  };
19884 
19885  // wrapper for "remove" operation; remove value at ptr
19886  const auto operation_remove = [&result](json_pointer & ptr)
19887  {
19888  // get reference to parent of JSON pointer ptr
19889  const auto last_path = ptr.pop_back();
19890  basic_json& parent = result.at(ptr);
19891 
19892  // remove child
19893  if (parent.is_object())
19894  {
19895  // perform range check
19896  auto it = parent.find(last_path);
19897  if (JSON_LIKELY(it != parent.end()))
19898  {
19899  parent.erase(it);
19900  }
19901  else
19902  {
19903  JSON_THROW(out_of_range::create(403, "key '" + last_path + "' not found"));
19904  }
19905  }
19906  else if (parent.is_array())
19907  {
19908  // note erase performs range check
19909  parent.erase(static_cast<size_type>(json_pointer::array_index(last_path)));
19910  }
19911  };
19912 
19913  // type check: top level value must be an array
19914  if (JSON_UNLIKELY(not json_patch.is_array()))
19915  {
19916  JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects"));
19917  }
19918 
19919  // iterate and apply the operations
19920  for (const auto& val : json_patch)
19921  {
19922  // wrapper to get a value for an operation
19923  const auto get_value = [&val](const std::string & op,
19924  const std::string & member,
19925  bool string_type) -> basic_json &
19926  {
19927  // find value
19928  auto it = val.m_value.object->find(member);
19929 
19930  // context-sensitive error message
19931  const auto error_msg = (op == "op") ? "operation" : "operation '" + op + "'";
19932 
19933  // check if desired value is present
19934  if (JSON_UNLIKELY(it == val.m_value.object->end()))
19935  {
19936  JSON_THROW(parse_error::create(105, 0, error_msg + " must have member '" + member + "'"));
19937  }
19938 
19939  // check if result is of type string
19940  if (JSON_UNLIKELY(string_type and not it->second.is_string()))
19941  {
19942  JSON_THROW(parse_error::create(105, 0, error_msg + " must have string member '" + member + "'"));
19943  }
19944 
19945  // no error: return value
19946  return it->second;
19947  };
19948 
19949  // type check: every element of the array must be an object
19950  if (JSON_UNLIKELY(not val.is_object()))
19951  {
19952  JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects"));
19953  }
19954 
19955  // collect mandatory members
19956  const std::string op = get_value("op", "op", true);
19957  const std::string path = get_value(op, "path", true);
19958  json_pointer ptr(path);
19959 
19960  switch (get_op(op))
19961  {
19962  case patch_operations::add:
19963  {
19964  operation_add(ptr, get_value("add", "value", false));
19965  break;
19966  }
19967 
19968  case patch_operations::remove:
19969  {
19970  operation_remove(ptr);
19971  break;
19972  }
19973 
19974  case patch_operations::replace:
19975  {
19976  // the "path" location must exist - use at()
19977  result.at(ptr) = get_value("replace", "value", false);
19978  break;
19979  }
19980 
19981  case patch_operations::move:
19982  {
19983  const std::string from_path = get_value("move", "from", true);
19984  json_pointer from_ptr(from_path);
19985 
19986  // the "from" location must exist - use at()
19987  basic_json v = result.at(from_ptr);
19988 
19989  // The move operation is functionally identical to a
19990  // "remove" operation on the "from" location, followed
19991  // immediately by an "add" operation at the target
19992  // location with the value that was just removed.
19993  operation_remove(from_ptr);
19994  operation_add(ptr, v);
19995  break;
19996  }
19997 
19998  case patch_operations::copy:
19999  {
20000  const std::string from_path = get_value("copy", "from", true);
20001  const json_pointer from_ptr(from_path);
20002 
20003  // the "from" location must exist - use at()
20004  basic_json v = result.at(from_ptr);
20005 
20006  // The copy is functionally identical to an "add"
20007  // operation at the target location using the value
20008  // specified in the "from" member.
20009  operation_add(ptr, v);
20010  break;
20011  }
20012 
20013  case patch_operations::test:
20014  {
20015  bool success = false;
20016  JSON_TRY
20017  {
20018  // check if "value" matches the one at "path"
20019  // the "path" location must exist - use at()
20020  success = (result.at(ptr) == get_value("test", "value", false));
20021  }
20022  JSON_INTERNAL_CATCH (out_of_range&)
20023  {
20024  // ignore out of range errors: success remains false
20025  }
20026 
20027  // throw an exception if test fails
20028  if (JSON_UNLIKELY(not success))
20029  {
20030  JSON_THROW(other_error::create(501, "unsuccessful: " + val.dump()));
20031  }
20032 
20033  break;
20034  }
20035 
20036  case patch_operations::invalid:
20037  {
20038  // op must be "add", "remove", "replace", "move", "copy", or
20039  // "test"
20040  JSON_THROW(parse_error::create(105, 0, "operation value '" + op + "' is invalid"));
20041  }
20042  }
20043  }
20044 
20045  return result;
20046  }
20047 
20048  /*!
20049  @brief creates a diff as a JSON patch
20050 
20051  Creates a [JSON Patch](http://jsonpatch.com) so that value @a source can
20052  be changed into the value @a target by calling @ref patch function.
20053 
20054  @invariant For two JSON values @a source and @a target, the following code
20055  yields always `true`:
20056  @code {.cpp}
20057  source.patch(diff(source, target)) == target;
20058  @endcode
20059 
20060  @note Currently, only `remove`, `add`, and `replace` operations are
20061  generated.
20062 
20063  @param[in] source JSON value to compare from
20064  @param[in] target JSON value to compare against
20065  @param[in] path helper value to create JSON pointers
20066 
20067  @return a JSON patch to convert the @a source to @a target
20068 
20069  @complexity Linear in the lengths of @a source and @a target.
20070 
20071  @liveexample{The following code shows how a JSON patch is created as a
20072  diff for two JSON values.,diff}
20073 
20074  @sa @ref patch -- apply a JSON patch
20075  @sa @ref merge_patch -- apply a JSON Merge Patch
20076 
20077  @sa [RFC 6902 (JSON Patch)](https://tools.ietf.org/html/rfc6902)
20078 
20079  @since version 2.0.0
20080  */
20081  static basic_json diff(const basic_json& source, const basic_json& target,
20082  const std::string& path = "")
20083  {
20084  // the patch
20085  basic_json result(value_t::array);
20086 
20087  // if the values are the same, return empty patch
20088  if (source == target)
20089  {
20090  return result;
20091  }
20092 
20093  if (source.type() != target.type())
20094  {
20095  // different types: replace value
20096  result.push_back(
20097  {
20098  {"op", "replace"}, {"path", path}, {"value", target}
20099  });
20100  }
20101  else
20102  {
20103  switch (source.type())
20104  {
20105  case value_t::array:
20106  {
20107  // first pass: traverse common elements
20108  std::size_t i = 0;
20109  while (i < source.size() and i < target.size())
20110  {
20111  // recursive call to compare array values at index i
20112  auto temp_diff = diff(source[i], target[i], path + "/" + std::to_string(i));
20113  result.insert(result.end(), temp_diff.begin(), temp_diff.end());
20114  ++i;
20115  }
20116 
20117  // i now reached the end of at least one array
20118  // in a second pass, traverse the remaining elements
20119 
20120  // remove my remaining elements
20121  const auto end_index = static_cast<difference_type>(result.size());
20122  while (i < source.size())
20123  {
20124  // add operations in reverse order to avoid invalid
20125  // indices
20126  result.insert(result.begin() + end_index, object(
20127  {
20128  {"op", "remove"},
20129  {"path", path + "/" + std::to_string(i)}
20130  }));
20131  ++i;
20132  }
20133 
20134  // add other remaining elements
20135  while (i < target.size())
20136  {
20137  result.push_back(
20138  {
20139  {"op", "add"},
20140  {"path", path + "/" + std::to_string(i)},
20141  {"value", target[i]}
20142  });
20143  ++i;
20144  }
20145 
20146  break;
20147  }
20148 
20149  case value_t::object:
20150  {
20151  // first pass: traverse this object's elements
20152  for (auto it = source.cbegin(); it != source.cend(); ++it)
20153  {
20154  // escape the key name to be used in a JSON patch
20155  const auto key = json_pointer::escape(it.key());
20156 
20157  if (target.find(it.key()) != target.end())
20158  {
20159  // recursive call to compare object values at key it
20160  auto temp_diff = diff(it.value(), target[it.key()], path + "/" + key);
20161  result.insert(result.end(), temp_diff.begin(), temp_diff.end());
20162  }
20163  else
20164  {
20165  // found a key that is not in o -> remove it
20166  result.push_back(object(
20167  {
20168  {"op", "remove"}, {"path", path + "/" + key}
20169  }));
20170  }
20171  }
20172 
20173  // second pass: traverse other object's elements
20174  for (auto it = target.cbegin(); it != target.cend(); ++it)
20175  {
20176  if (source.find(it.key()) == source.end())
20177  {
20178  // found a key that is not in this -> add it
20179  const auto key = json_pointer::escape(it.key());
20180  result.push_back(
20181  {
20182  {"op", "add"}, {"path", path + "/" + key},
20183  {"value", it.value()}
20184  });
20185  }
20186  }
20187 
20188  break;
20189  }
20190 
20191  default:
20192  {
20193  // both primitive type: replace value
20194  result.push_back(
20195  {
20196  {"op", "replace"}, {"path", path}, {"value", target}
20197  });
20198  break;
20199  }
20200  }
20201  }
20202 
20203  return result;
20204  }
20205 
20206  /// @}
20207 
20208  ////////////////////////////////
20209  // JSON Merge Patch functions //
20210  ////////////////////////////////
20211 
20212  /// @name JSON Merge Patch functions
20213  /// @{
20214 
20215  /*!
20216  @brief applies a JSON Merge Patch
20217 
20218  The merge patch format is primarily intended for use with the HTTP PATCH
20219  method as a means of describing a set of modifications to a target
20220  resource's content. This function applies a merge patch to the current
20221  JSON value.
20222 
20223  The function implements the following algorithm from Section 2 of
20224  [RFC 7396 (JSON Merge Patch)](https://tools.ietf.org/html/rfc7396):
20225 
20226  ```
20227  define MergePatch(Target, Patch):
20228  if Patch is an Object:
20229  if Target is not an Object:
20230  Target = {} // Ignore the contents and set it to an empty Object
20231  for each Name/Value pair in Patch:
20232  if Value is null:
20233  if Name exists in Target:
20234  remove the Name/Value pair from Target
20235  else:
20236  Target[Name] = MergePatch(Target[Name], Value)
20237  return Target
20238  else:
20239  return Patch
20240  ```
20241 
20242  Thereby, `Target` is the current object; that is, the patch is applied to
20243  the current value.
20244 
20245  @param[in] apply_patch the patch to apply
20246 
20247  @complexity Linear in the lengths of @a patch.
20248 
20249  @liveexample{The following code shows how a JSON Merge Patch is applied to
20250  a JSON document.,merge_patch}
20251 
20252  @sa @ref patch -- apply a JSON patch
20253  @sa [RFC 7396 (JSON Merge Patch)](https://tools.ietf.org/html/rfc7396)
20254 
20255  @since version 3.0.0
20256  */
20257  void merge_patch(const basic_json& apply_patch)
20258  {
20259  if (apply_patch.is_object())
20260  {
20261  if (not is_object())
20262  {
20263  *this = object();
20264  }
20265  for (auto it = apply_patch.begin(); it != apply_patch.end(); ++it)
20266  {
20267  if (it.value().is_null())
20268  {
20269  erase(it.key());
20270  }
20271  else
20272  {
20273  operator[](it.key()).merge_patch(it.value());
20274  }
20275  }
20276  }
20277  else
20278  {
20279  *this = apply_patch;
20280  }
20281  }
20282 
20283  /// @}
20284 };
20285 } // namespace nlohmann
20286 
20287 ///////////////////////
20288 // nonmember support //
20289 ///////////////////////
20290 
20291 // specialization of std::swap, and std::hash
20292 namespace std
20293 {
20294 
20295 /// hash value for JSON objects
20296 template<>
20297 struct hash<nlohmann::json>
20298 {
20299  /*!
20300  @brief return a hash value for a JSON object
20301 
20302  @since version 1.0.0
20303  */
20304  std::size_t operator()(const nlohmann::json& j) const
20305  {
20306  // a naive hashing via the string representation
20307  const auto& h = hash<nlohmann::json::string_t>();
20308  return h(j.dump());
20309  }
20310 };
20311 
20312 /// specialization for std::less<value_t>
20313 /// @note: do not remove the space after '<',
20314 /// see https://github.com/nlohmann/json/pull/679
20315 template<>
20316 struct less< ::nlohmann::detail::value_t>
20317 {
20318  /*!
20319  @brief compare two value_t enum values
20320  @since version 3.0.0
20321  */
20322  bool operator()(nlohmann::detail::value_t lhs,
20323  nlohmann::detail::value_t rhs) const noexcept
20324  {
20325  return nlohmann::detail::operator<(lhs, rhs);
20326  }
20327 };
20328 
20329 /*!
20330 @brief exchanges the values of two JSON objects
20331 
20332 @since version 1.0.0
20333 */
20334 template<>
20335 inline void swap<nlohmann::json>(nlohmann::json& j1, nlohmann::json& j2) noexcept(
20336  is_nothrow_move_constructible<nlohmann::json>::value and
20337  is_nothrow_move_assignable<nlohmann::json>::value
20338 )
20339 {
20340  j1.swap(j2);
20341 }
20342 
20343 } // namespace std
20344 
20345 /*!
20346 @brief user-defined string literal for JSON values
20347 
20348 This operator implements a user-defined string literal for JSON objects. It
20349 can be used by adding `"_json"` to a string literal and returns a JSON object
20350 if no parse error occurred.
20351 
20352 @param[in] s a string representation of a JSON object
20353 @param[in] n the length of string @a s
20354 @return a JSON object
20355 
20356 @since version 1.0.0
20357 */
20358 inline nlohmann::json operator "" _json(const char* s, std::size_t n)
20359 {
20360  return nlohmann::json::parse(s, s + n);
20361 }
20362 
20363 /*!
20364 @brief user-defined string literal for JSON pointer
20365 
20366 This operator implements a user-defined string literal for JSON Pointers. It
20367 can be used by adding `"_json_pointer"` to a string literal and returns a JSON pointer
20368 object if no parse error occurred.
20369 
20370 @param[in] s a string representation of a JSON Pointer
20371 @param[in] n the length of string @a s
20372 @return a JSON pointer object
20373 
20374 @since version 2.0.0
20375 */
20376 inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std::size_t n)
20377 {
20378  return nlohmann::json::json_pointer(std::string(s, n));
20379 }
20380 
20381 // #include <nlohmann/detail/macro_unscope.hpp>
20382 
20383 
20384 // restore GCC/clang diagnostic settings
20385 #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
20386  #pragma GCC diagnostic pop
20387 #endif
20388 #if defined(__clang__)
20389  #pragma GCC diagnostic pop
20390 #endif
20391 
20392 // clean up
20393 #undef JSON_INTERNAL_CATCH
20394 #undef JSON_CATCH
20395 #undef JSON_THROW
20396 #undef JSON_TRY
20397 #undef JSON_LIKELY
20398 #undef JSON_UNLIKELY
20399 #undef JSON_DEPRECATED
20400 #undef JSON_HAS_CPP_14
20401 #undef JSON_HAS_CPP_17
20402 #undef NLOHMANN_BASIC_JSON_TPL_DECLARATION
20403 #undef NLOHMANN_BASIC_JSON_TPL
20404 
20405 
20406 #endif
#define NLOHMANN_BASIC_JSON_TPL_DECLARATION
Definition: json.hpp:239
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:19092
#define JSON_DEPRECATED
Definition: json.hpp:152
#define JSON_LIKELY(x)
Definition: json.hpp:193
virtual bool key(string_t &val)=0
an object key was read
virtual bool number_float(number_float_t val, const string_t &s)=0
an floating-point number was read
virtual bool end_object()=0
the end of an object was read
virtual bool null()=0
a null value was read
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:19015
#define JSON_UNLIKELY(x)
Definition: json.hpp:194
friend class basic_json
Definition: json.hpp:11720
#define NLOHMANN_JSON_VERSION_MAJOR
Definition: json.hpp:33
virtual bool number_unsigned(number_unsigned_t val)=0
an unsigned integer number was read
static basic_json from_msgpack(detail::input_adapter &&i, const bool strict=true, const bool allow_exceptions=true)
create a JSON value from an input in MessagePack format
Definition: json.hpp:19323
virtual bool start_object(std::size_t elements)=0
the beginning of an object was read
#define JSON_INTERNAL_CATCH(exception)
Definition: json.hpp:165
constexpr const auto & from_json
Definition: json.hpp:1625
virtual ~json_sax()=default
#define JSON_CATCH(exception)
Definition: json.hpp:164
static basic_json from_bson(detail::input_adapter &&i, const bool strict=true, const bool allow_exceptions=true)
Create a JSON value from an input in BSON format.
Definition: json.hpp:19490
namespace for Niels Lohmann
Definition: json.hpp:64
static basic_json from_cbor(detail::input_adapter &&i, const bool strict=true, const bool allow_exceptions=true)
create a JSON value from an input in CBOR format
Definition: json.hpp:19218
#define NLOHMANN_BASIC_JSON_TPL
Definition: json.hpp:247
#define NLOHMANN_JSON_VERSION_PATCH
Definition: json.hpp:35
virtual bool number_integer(number_integer_t val)=0
an integer number was read
virtual bool start_array(std::size_t elements)=0
the beginning of an array was read
#define JSON_TRY
Definition: json.hpp:163
#define NLOHMANN_JSON_VERSION_MINOR
Definition: json.hpp:34
#define JSON_THROW(exception)
Definition: json.hpp:162
constexpr const auto & to_json
Definition: json.hpp:2136
virtual bool end_array()=0
the end of an array was read
virtual bool string(string_t &val)=0
a string was read
virtual bool boolean(bool val)=0
a boolean value was read
virtual bool parse_error(std::size_t position, const std::string &last_token, const detail::exception &ex)=0
a parse error occurred
static basic_json from_ubjson(detail::input_adapter &&i, const bool strict=true, const bool allow_exceptions=true)
create a JSON value from an input in UBJSON format
Definition: json.hpp:19407
SAX interface.
Definition: json.hpp:4260
json_pointer(const std::string &s="")
create JSON pointer
Definition: json.hpp:11744