Flow123d  3.9.0-9663d1cde
json.hpp
Go to the documentation of this file.
1 /*
2  __ _____ _____ _____
3  __| | __| | | | JSON for Modern C++
4 | | |__ | | | | | | version 2.0.0
5 |_____|_____|_____|_|___| https://github.com/nlohmann/json
6 
7 Licensed under the MIT License <http://opensource.org/licenses/MIT>.
8 Copyright (c) 2013-2016 Niels Lohmann <http://nlohmann.me>.
9 
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files (the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions:
16 
17 The above copyright notice and this permission notice shall be included in all
18 copies or substantial portions of the Software.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 SOFTWARE.
27 */
28 
29 #ifndef NLOHMANN_JSON_HPP
30 #define NLOHMANN_JSON_HPP
31 
32 #include <algorithm>
33 #include <array>
34 #include <cassert>
35 #include <ciso646>
36 #include <cmath>
37 #include <cstddef>
38 #include <cstdio>
39 #include <cstdlib>
40 #include <functional>
41 #include <initializer_list>
42 #include <iomanip>
43 #include <iostream>
44 #include <iterator>
45 #include <limits>
46 #include <map>
47 #include <memory>
48 #include <sstream>
49 #include <stdexcept>
50 #include <string>
51 #include <type_traits>
52 #include <utility>
53 #include <vector>
54 
55 // disable float-equal warnings on GCC/clang
56 #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
57  #pragma GCC diagnostic push
58  #pragma GCC diagnostic ignored "-Wfloat-equal"
59 #endif
60 
61 /*!
62 @brief namespace for Niels Lohmann
63 @see https://github.com/nlohmann
64 @since version 1.0.0
65 */
66 namespace nlohmann
67 {
68 
69 
70 /*!
71 @brief unnamed namespace with internal helper functions
72 @since version 1.0.0
73 */
74 namespace
75 {
76 /*!
77 @brief Helper to determine whether there's a key_type for T.
78 @sa http://stackoverflow.com/a/7728728/266378
79 */
80 template<typename T>
81 struct has_mapped_type
82 {
83  private:
84  template<typename C> static char test(typename C::mapped_type*);
85  template<typename C> static char (&test(...))[2];
86  public:
87  static constexpr bool value = sizeof(test<T>(0)) == 1;
88 };
89 
90 }
91 
92 /*!
93 @brief a class to store JSON values
94 
95 @tparam ObjectType type for JSON objects (`std::map` by default; will be used
96 in @ref object_t)
97 @tparam ArrayType type for JSON arrays (`std::vector` by default; will be used
98 in @ref array_t)
99 @tparam StringType type for JSON strings and object keys (`std::string` by
100 default; will be used in @ref string_t)
101 @tparam BooleanType type for JSON booleans (`bool` by default; will be used
102 in @ref boolean_t)
103 @tparam NumberIntegerType type for JSON integer numbers (`int64_t` by
104 default; will be used in @ref number_integer_t)
105 @tparam NumberUnsignedType type for JSON unsigned integer numbers (@c
106 `uint64_t` by default; will be used in @ref number_unsigned_t)
107 @tparam NumberFloatType type for JSON floating-point numbers (`double` by
108 default; will be used in @ref number_float_t)
109 @tparam AllocatorType type of the allocator to use (`std::allocator` by
110 default)
111 
112 @requirement The class satisfies the following concept requirements:
113 - Basic
114  - [DefaultConstructible](http://en.cppreference.com/w/cpp/concept/DefaultConstructible):
115  JSON values can be default constructed. The result will be a JSON null value.
116  - [MoveConstructible](http://en.cppreference.com/w/cpp/concept/MoveConstructible):
117  A JSON value can be constructed from an rvalue argument.
118  - [CopyConstructible](http://en.cppreference.com/w/cpp/concept/CopyConstructible):
119  A JSON value can be copy-constructed from an lvalue expression.
120  - [MoveAssignable](http://en.cppreference.com/w/cpp/concept/MoveAssignable):
121  A JSON value van be assigned from an rvalue argument.
122  - [CopyAssignable](http://en.cppreference.com/w/cpp/concept/CopyAssignable):
123  A JSON value can be copy-assigned from an lvalue expression.
124  - [Destructible](http://en.cppreference.com/w/cpp/concept/Destructible):
125  JSON values can be destructed.
126 - Layout
127  - [StandardLayoutType](http://en.cppreference.com/w/cpp/concept/StandardLayoutType):
128  JSON values have
129  [standard layout](http://en.cppreference.com/w/cpp/language/data_members#Standard_layout):
130  All non-static data members are private and standard layout types, the class
131  has no virtual functions or (virtual) base classes.
132 - Library-wide
133  - [EqualityComparable](http://en.cppreference.com/w/cpp/concept/EqualityComparable):
134  JSON values can be compared with `==`, see @ref
135  operator==(const_reference,const_reference).
136  - [LessThanComparable](http://en.cppreference.com/w/cpp/concept/LessThanComparable):
137  JSON values can be compared with `<`, see @ref
138  operator<(const_reference,const_reference).
139  - [Swappable](http://en.cppreference.com/w/cpp/concept/Swappable):
140  Any JSON lvalue or rvalue of can be swapped with any lvalue or rvalue of
141  other compatible types, using unqualified function call @ref swap().
142  - [NullablePointer](http://en.cppreference.com/w/cpp/concept/NullablePointer):
143  JSON values can be compared against `std::nullptr_t` objects which are used
144  to model the `null` value.
145 - Container
146  - [Container](http://en.cppreference.com/w/cpp/concept/Container):
147  JSON values can be used like STL containers and provide iterator access.
148  - [ReversibleContainer](http://en.cppreference.com/w/cpp/concept/ReversibleContainer);
149  JSON values can be used like STL containers and provide reverse iterator
150  access.
151 
152 @internal
153 @note ObjectType trick from http://stackoverflow.com/a/9860911
154 @endinternal
155 
156 @see [RFC 7159: The JavaScript Object Notation (JSON) Data Interchange
157 Format](http://rfc7159.net/rfc7159)
158 
159 @since version 1.0.0
160 
161 @nosubgrouping
162 */
163 template <
164  template<typename U, typename V, typename... Args> class ObjectType = std::map,
165  template<typename U, typename... Args> class ArrayType = std::vector,
166  class StringType = std::string,
167  class BooleanType = bool,
168  class NumberIntegerType = std::int64_t,
169  class NumberUnsignedType = std::uint64_t,
170  class NumberFloatType = double,
171  template<typename U> class AllocatorType = std::allocator
172  >
174 {
175  private:
176  /// workaround type for MSVC
177  using basic_json_t = basic_json<ObjectType,
178  ArrayType,
179  StringType,
180  BooleanType,
181  NumberIntegerType,
182  NumberUnsignedType,
183  NumberFloatType,
184  AllocatorType>;
185 
186  public:
187 
188  /////////////////////
189  // container types //
190  /////////////////////
191 
192  /// @name container types
193  /// @{
194 
195  /// the type of elements in a basic_json container
197 
198  /// the type of an element reference
200  /// the type of an element const reference
201  using const_reference = const value_type&;
202 
203  /// a type to represent differences between iterators
204  using difference_type = std::ptrdiff_t;
205  /// a type to represent container sizes
206  using size_type = std::size_t;
207 
208  /// the allocator type
209  using allocator_type = AllocatorType<basic_json>;
210 
211  /// the type of an element pointer
212  using pointer = typename std::allocator_traits<allocator_type>::pointer;
213  /// the type of an element const pointer
214  using const_pointer = typename std::allocator_traits<allocator_type>::const_pointer;
215 
216  // forward declaration
217  template<typename Base> class json_reverse_iterator;
218 
219  /// an iterator for a basic_json container
220  class iterator;
221  /// a const iterator for a basic_json container
222  class const_iterator;
223  /// a reverse iterator for a basic_json container
225  /// a const reverse iterator for a basic_json container
227 
228  /// @}
229 
230 
231  /*!
232  @brief returns the allocator associated with the container
233  */
235  {
236  return allocator_type();
237  }
238 
239 
240  ///////////////////////////
241  // JSON value data types //
242  ///////////////////////////
243 
244  /// @name JSON value data types
245  /// @{
246 
247  /*!
248  @brief a type for an object
249 
250  [RFC 7159](http://rfc7159.net/rfc7159) describes JSON objects as follows:
251  > An object is an unordered collection of zero or more name/value pairs,
252  > where a name is a string and a value is a string, number, boolean, null,
253  > object, or array.
254 
255  To store objects in C++, a type is defined by the template parameters
256  described below.
257 
258  @tparam ObjectType the container to store objects (e.g., `std::map` or
259  `std::unordered_map`)
260  @tparam StringType the type of the keys or names (e.g., `std::string`). The
261  comparison function `std::less<StringType>` is used to order elements
262  inside the container.
263  @tparam AllocatorType the allocator to use for objects (e.g.,
264  `std::allocator`)
265 
266  #### Default type
267 
268  With the default values for @a ObjectType (`std::map`), @a StringType
269  (`std::string`), and @a AllocatorType (`std::allocator`), the default value
270  for @a object_t is:
271 
272  @code {.cpp}
273  std::map<
274  std::string, // key_type
275  basic_json, // value_type
276  std::less<std::string>, // key_compare
277  std::allocator<std::pair<const std::string, basic_json>> // allocator_type
278  >
279  @endcode
280 
281  #### Behavior
282 
283  The choice of @a object_t influences the behavior of the JSON class. With
284  the default type, objects have the following behavior:
285 
286  - When all names are unique, objects will be interoperable in the sense
287  that all software implementations receiving that object will agree on the
288  name-value mappings.
289  - When the names within an object are not unique, later stored name/value
290  pairs overwrite previously stored name/value pairs, leaving the used
291  names unique. For instance, `{"key": 1}` and `{"key": 2, "key": 1}` will
292  be treated as equal and both stored as `{"key": 1}`.
293  - Internally, name/value pairs are stored in lexicographical order of the
294  names. Objects will also be serialized (see @ref dump) in this order. For
295  instance, `{"b": 1, "a": 2}` and `{"a": 2, "b": 1}` will be stored and
296  serialized as `{"a": 2, "b": 1}`.
297  - When comparing objects, the order of the name/value pairs is irrelevant.
298  This makes objects interoperable in the sense that they will not be
299  affected by these differences. For instance, `{"b": 1, "a": 2}` and
300  `{"a": 2, "b": 1}` will be treated as equal.
301 
302  #### Limits
303 
304  [RFC 7159](http://rfc7159.net/rfc7159) specifies:
305  > An implementation may set limits on the maximum depth of nesting.
306 
307  In this class, the object's limit of nesting is not constraint explicitly.
308  However, a maximum depth of nesting may be introduced by the compiler or
309  runtime environment. A theoretical limit can be queried by calling the @ref
310  max_size function of a JSON object.
311 
312  #### Storage
313 
314  Objects are stored as pointers in a @ref basic_json type. That is, for any
315  access to object values, a pointer of type `object_t*` must be dereferenced.
316 
317  @sa @ref array_t -- type for an array value
318 
319  @since version 1.0.0
320 
321  @note The order name/value pairs are added to the object is *not* preserved
322  by the library. Therefore, iterating an object may return name/value pairs
323  in a different order than they were originally stored. In fact, keys will
324  be traversed in alphabetical order as `std::map` with `std::less` is used
325  by default. Please note this behavior conforms to [RFC
326  7159](http://rfc7159.net/rfc7159), because any order implements the
327  specified "unordered" nature of JSON objects.
328  */
329  using object_t = ObjectType<StringType,
330  basic_json,
331  std::less<StringType>,
332  AllocatorType<std::pair<const StringType,
334 
335  /*!
336  @brief a type for an array
337 
338  [RFC 7159](http://rfc7159.net/rfc7159) describes JSON arrays as follows:
339  > An array is an ordered sequence of zero or more values.
340 
341  To store objects in C++, a type is defined by the template parameters
342  explained below.
343 
344  @tparam ArrayType container type to store arrays (e.g., `std::vector` or
345  `std::list`)
346  @tparam AllocatorType allocator to use for arrays (e.g., `std::allocator`)
347 
348  #### Default type
349 
350  With the default values for @a ArrayType (`std::vector`) and @a
351  AllocatorType (`std::allocator`), the default value for @a array_t is:
352 
353  @code {.cpp}
354  std::vector<
355  basic_json, // value_type
356  std::allocator<basic_json> // allocator_type
357  >
358  @endcode
359 
360  #### Limits
361 
362  [RFC 7159](http://rfc7159.net/rfc7159) specifies:
363  > An implementation may set limits on the maximum depth of nesting.
364 
365  In this class, the array's limit of nesting is not constraint explicitly.
366  However, a maximum depth of nesting may be introduced by the compiler or
367  runtime environment. A theoretical limit can be queried by calling the @ref
368  max_size function of a JSON array.
369 
370  #### Storage
371 
372  Arrays are stored as pointers in a @ref basic_json type. That is, for any
373  access to array values, a pointer of type `array_t*` must be dereferenced.
374 
375  @sa @ref object_t -- type for an object value
376 
377  @since version 1.0.0
378  */
379  using array_t = ArrayType<basic_json, AllocatorType<basic_json>>;
380 
381  /*!
382  @brief a type for a string
383 
384  [RFC 7159](http://rfc7159.net/rfc7159) describes JSON strings as follows:
385  > A string is a sequence of zero or more Unicode characters.
386 
387  To store objects in C++, a type is defined by the template parameter
388  described below. Unicode values are split by the JSON class into byte-sized
389  characters during deserialization.
390 
391  @tparam StringType the container to store strings (e.g., `std::string`).
392  Note this container is used for keys/names in objects, see @ref object_t.
393 
394  #### Default type
395 
396  With the default values for @a StringType (`std::string`), the default
397  value for @a string_t is:
398 
399  @code {.cpp}
400  std::string
401  @endcode
402 
403  #### String comparison
404 
405  [RFC 7159](http://rfc7159.net/rfc7159) states:
406  > Software implementations are typically required to test names of object
407  > members for equality. Implementations that transform the textual
408  > representation into sequences of Unicode code units and then perform the
409  > comparison numerically, code unit by code unit, are interoperable in the
410  > sense that implementations will agree in all cases on equality or
411  > inequality of two strings. For example, implementations that compare
412  > strings with escaped characters unconverted may incorrectly find that
413  > `"a\\b"` and `"a\u005Cb"` are not equal.
414 
415  This implementation is interoperable as it does compare strings code unit
416  by code unit.
417 
418  #### Storage
419 
420  String values are stored as pointers in a @ref basic_json type. That is,
421  for any access to string values, a pointer of type `string_t*` must be
422  dereferenced.
423 
424  @since version 1.0.0
425  */
426  using string_t = StringType;
427 
428  /*!
429  @brief a type for a boolean
430 
431  [RFC 7159](http://rfc7159.net/rfc7159) implicitly describes a boolean as a
432  type which differentiates the two literals `true` and `false`.
433 
434  To store objects in C++, a type is defined by the template parameter @a
435  BooleanType which chooses the type to use.
436 
437  #### Default type
438 
439  With the default values for @a BooleanType (`bool`), the default value for
440  @a boolean_t is:
441 
442  @code {.cpp}
443  bool
444  @endcode
445 
446  #### Storage
447 
448  Boolean values are stored directly inside a @ref basic_json type.
449 
450  @since version 1.0.0
451  */
452  using boolean_t = BooleanType;
453 
454  /*!
455  @brief a type for a number (integer)
456 
457  [RFC 7159](http://rfc7159.net/rfc7159) describes numbers as follows:
458  > The representation of numbers is similar to that used in most programming
459  > languages. A number is represented in base 10 using decimal digits. It
460  > contains an integer component that may be prefixed with an optional minus
461  > sign, which may be followed by a fraction part and/or an exponent part.
462  > Leading zeros are not allowed. (...) Numeric values that cannot be
463  > represented in the grammar below (such as Infinity and NaN) are not
464  > permitted.
465 
466  This description includes both integer and floating-point numbers. However,
467  C++ allows more precise storage if it is known whether the number is a
468  signed integer, an unsigned integer or a floating-point number. Therefore,
469  three different types, @ref number_integer_t, @ref number_unsigned_t and
470  @ref number_float_t are used.
471 
472  To store integer numbers in C++, a type is defined by the template
473  parameter @a NumberIntegerType which chooses the type to use.
474 
475  #### Default type
476 
477  With the default values for @a NumberIntegerType (`int64_t`), the default
478  value for @a number_integer_t is:
479 
480  @code {.cpp}
481  int64_t
482  @endcode
483 
484  #### Default behavior
485 
486  - The restrictions about leading zeros is not enforced in C++. Instead,
487  leading zeros in integer literals lead to an interpretation as octal
488  number. Internally, the value will be stored as decimal number. For
489  instance, the C++ integer literal `010` will be serialized to `8`. During
490  deserialization, leading zeros yield an error.
491  - Not-a-number (NaN) values will be serialized to `null`.
492 
493  #### Limits
494 
495  [RFC 7159](http://rfc7159.net/rfc7159) specifies:
496  > An implementation may set limits on the range and precision of numbers.
497 
498  When the default type is used, the maximal integer number that can be
499  stored is `9223372036854775807` (INT64_MAX) and the minimal integer number
500  that can be stored is `-9223372036854775808` (INT64_MIN). Integer numbers
501  that are out of range will yield over/underflow when used in a constructor.
502  During deserialization, too large or small integer numbers will be
503  automatically be stored as @ref number_unsigned_t or @ref number_float_t.
504 
505  [RFC 7159](http://rfc7159.net/rfc7159) further states:
506  > Note that when such software is used, numbers that are integers and are
507  > in the range \f$[-2^{53}+1, 2^{53}-1]\f$ are interoperable in the sense
508  > that implementations will agree exactly on their numeric values.
509 
510  As this range is a subrange of the exactly supported range [INT64_MIN,
511  INT64_MAX], this class's integer type is interoperable.
512 
513  #### Storage
514 
515  Integer number values are stored directly inside a @ref basic_json type.
516 
517  @sa @ref number_float_t -- type for number values (floating-point)
518 
519  @sa @ref number_unsigned_t -- type for number values (unsigned integer)
520 
521  @since version 1.0.0
522  */
523  using number_integer_t = NumberIntegerType;
524 
525  /*!
526  @brief a type for a number (unsigned)
527 
528  [RFC 7159](http://rfc7159.net/rfc7159) describes numbers as follows:
529  > The representation of numbers is similar to that used in most programming
530  > languages. A number is represented in base 10 using decimal digits. It
531  > contains an integer component that may be prefixed with an optional minus
532  > sign, which may be followed by a fraction part and/or an exponent part.
533  > Leading zeros are not allowed. (...) Numeric values that cannot be
534  > represented in the grammar below (such as Infinity and NaN) are not
535  > permitted.
536 
537  This description includes both integer and floating-point numbers. However,
538  C++ allows more precise storage if it is known whether the number is a
539  signed integer, an unsigned integer or a floating-point number. Therefore,
540  three different types, @ref number_integer_t, @ref number_unsigned_t and
541  @ref number_float_t are used.
542 
543  To store unsigned integer numbers in C++, a type is defined by the template
544  parameter @a NumberUnsignedType which chooses the type to use.
545 
546  #### Default type
547 
548  With the default values for @a NumberUnsignedType (`uint64_t`), the default
549  value for @a number_unsigned_t is:
550 
551  @code {.cpp}
552  uint64_t
553  @endcode
554 
555  #### Default behavior
556 
557  - The restrictions about leading zeros is not enforced in C++. Instead,
558  leading zeros in integer literals lead to an interpretation as octal
559  number. Internally, the value will be stored as decimal number. For
560  instance, the C++ integer literal `010` will be serialized to `8`. During
561  deserialization, leading zeros yield an error.
562  - Not-a-number (NaN) values will be serialized to `null`.
563 
564  #### Limits
565 
566  [RFC 7159](http://rfc7159.net/rfc7159) specifies:
567  > An implementation may set limits on the range and precision of numbers.
568 
569  When the default type is used, the maximal integer number that can be
570  stored is `18446744073709551615` (UINT64_MAX) and the minimal integer
571  number that can be stored is `0`. Integer numbers that are out of range
572  will yield over/underflow when used in a constructor. During
573  deserialization, too large or small integer numbers will be automatically
574  be stored as @ref number_integer_t or @ref number_float_t.
575 
576  [RFC 7159](http://rfc7159.net/rfc7159) further states:
577  > Note that when such software is used, numbers that are integers and are
578  > in the range \f$[-2^{53}+1, 2^{53}-1]\f$ are interoperable in the sense
579  > that implementations will agree exactly on their numeric values.
580 
581  As this range is a subrange (when considered in conjunction with the
582  number_integer_t type) of the exactly supported range [0, UINT64_MAX], this
583  class's integer type is interoperable.
584 
585  #### Storage
586 
587  Integer number values are stored directly inside a @ref basic_json type.
588 
589  @sa @ref number_float_t -- type for number values (floating-point)
590 
591  @sa @ref number_integer_t -- type for number values (integer)
592 
593  @since version 2.0.0
594  */
595  using number_unsigned_t = NumberUnsignedType;
596 
597  /*!
598  @brief a type for a number (floating-point)
599 
600  [RFC 7159](http://rfc7159.net/rfc7159) describes numbers as follows:
601  > The representation of numbers is similar to that used in most programming
602  > languages. A number is represented in base 10 using decimal digits. It
603  > contains an integer component that may be prefixed with an optional minus
604  > sign, which may be followed by a fraction part and/or an exponent part.
605  > Leading zeros are not allowed. (...) Numeric values that cannot be
606  > represented in the grammar below (such as Infinity and NaN) are not
607  > permitted.
608 
609  This description includes both integer and floating-point numbers. However,
610  C++ allows more precise storage if it is known whether the number is a
611  signed integer, an unsigned integer or a floating-point number. Therefore,
612  three different types, @ref number_integer_t, @ref number_unsigned_t and
613  @ref number_float_t are used.
614 
615  To store floating-point numbers in C++, a type is defined by the template
616  parameter @a NumberFloatType which chooses the type to use.
617 
618  #### Default type
619 
620  With the default values for @a NumberFloatType (`double`), the default
621  value for @a number_float_t is:
622 
623  @code {.cpp}
624  double
625  @endcode
626 
627  #### Default behavior
628 
629  - The restrictions about leading zeros is not enforced in C++. Instead,
630  leading zeros in floating-point literals will be ignored. Internally, the
631  value will be stored as decimal number. For instance, the C++
632  floating-point literal `01.2` will be serialized to `1.2`. During
633  deserialization, leading zeros yield an error.
634  - Not-a-number (NaN) values will be serialized to `null`.
635 
636  #### Limits
637 
638  [RFC 7159](http://rfc7159.net/rfc7159) states:
639  > This specification allows implementations to set limits on the range and
640  > precision of numbers accepted. Since software that implements IEEE
641  > 754-2008 binary64 (double precision) numbers is generally available and
642  > widely used, good interoperability can be achieved by implementations that
643  > expect no more precision or range than these provide, in the sense that
644  > implementations will approximate JSON numbers within the expected
645  > precision.
646 
647  This implementation does exactly follow this approach, as it uses double
648  precision floating-point numbers. Note values smaller than
649  `-1.79769313486232e+308` and values greater than `1.79769313486232e+308`
650  will be stored as NaN internally and be serialized to `null`.
651 
652  #### Storage
653 
654  Floating-point number values are stored directly inside a @ref basic_json
655  type.
656 
657  @sa @ref number_integer_t -- type for number values (integer)
658 
659  @sa @ref number_unsigned_t -- type for number values (unsigned integer)
660 
661  @since version 1.0.0
662  */
663  using number_float_t = NumberFloatType;
664 
665  /// @}
666 
667 
668  ///////////////////////////
669  // JSON type enumeration //
670  ///////////////////////////
671 
672  /*!
673  @brief the JSON type enumeration
674 
675  This enumeration collects the different JSON types. It is internally used
676  to distinguish the stored values, and the functions @ref is_null(), @ref
677  is_object(), @ref is_array(), @ref is_string(), @ref is_boolean(), @ref
678  is_number(), and @ref is_discarded() rely on it.
679 
680  @since version 1.0.0
681  */
682  enum class value_t : uint8_t
683  {
684  null, ///< null value
685  object, ///< object (unordered set of name/value pairs)
686  array, ///< array (ordered collection of values)
687  string, ///< string value
688  boolean, ///< boolean value
689  number_integer, ///< number value (integer)
690  number_unsigned, ///< number value (unsigned integer)
691  number_float, ///< number value (floating-point)
692  discarded ///< discarded by the the parser callback function
693  };
694 
695 
696  private:
697  /// helper for exception-safe object creation
698  template<typename T, typename... Args>
699  static T* create(Args&& ... args)
700  {
701  AllocatorType<T> alloc;
702  auto deleter = [&](T * object)
703  {
704  alloc.deallocate(object, 1);
705  };
706  std::unique_ptr<T, decltype(deleter)> object(alloc.allocate(1), deleter);
707  alloc.construct(object.get(), std::forward<Args>(args)...);
708  return object.release();
709  }
710 
711  ////////////////////////
712  // JSON value storage //
713  ////////////////////////
714 
715  /*!
716  @brief a JSON value
717 
718  The actual storage for a JSON value of the @ref basic_json class.
719 
720  @since version 1.0.0
721  */
723  {
724  /// object (stored with pointer to save storage)
726  /// array (stored with pointer to save storage)
728  /// string (stored with pointer to save storage)
730  /// boolean
732  /// number (integer)
734  /// number (unsigned integer)
736  /// number (floating-point)
738 
739  /// default constructor (for null values)
740  json_value() = default;
741  /// constructor for booleans
742  json_value(boolean_t v) noexcept : boolean(v) {}
743  /// constructor for numbers (integer)
745  /// constructor for numbers (unsigned)
747  /// constructor for numbers (floating-point)
749  /// constructor for empty values of a given type
751  {
752  switch (t)
753  {
754  case value_t::object:
755  {
756  object = create<object_t>();
757  break;
758  }
759 
760  case value_t::array:
761  {
762  array = create<array_t>();
763  break;
764  }
765 
766  case value_t::string:
767  {
768  string = create<string_t>("");
769  break;
770  }
771 
772  case value_t::boolean:
773  {
774  boolean = boolean_t(false);
775  break;
776  }
777 
779  {
781  break;
782  }
783 
785  {
787  break;
788  }
789 
791  {
793  break;
794  }
795 
796  default:
797  {
798  break;
799  }
800  }
801  }
802 
803  /// constructor for strings
805  {
806  string = create<string_t>(value);
807  }
808 
809  /// constructor for objects
811  {
812  object = create<object_t>(value);
813  }
814 
815  /// constructor for arrays
817  {
818  array = create<array_t>(value);
819  }
820  };
821 
822 
823  public:
824  //////////////////////////
825  // JSON parser callback //
826  //////////////////////////
827 
828  /*!
829  @brief JSON callback events
830 
831  This enumeration lists the parser events that can trigger calling a
832  callback function of type @ref parser_callback_t during parsing.
833 
834  @since version 1.0.0
835  */
836  enum class parse_event_t : uint8_t
837  {
838  /// the parser read `{` and started to process a JSON object
839  object_start,
840  /// the parser read `}` and finished processing a JSON object
841  object_end,
842  /// the parser read `[` and started to process a JSON array
843  array_start,
844  /// the parser read `]` and finished processing a JSON array
845  array_end,
846  /// the parser read a key of a value in an object
847  key,
848  /// the parser finished reading a JSON value
849  value
850  };
851 
852  /*!
853  @brief per-element parser callback type
854 
855  With a parser callback function, the result of parsing a JSON text can be
856  influenced. When passed to @ref parse(std::istream&, parser_callback_t) or
857  @ref parse(const string_t&, parser_callback_t), it is called on certain
858  events (passed as @ref parse_event_t via parameter @a event) with a set
859  recursion depth @a depth and context JSON value @a parsed. The return value
860  of the callback function is a boolean indicating whether the element that
861  emitted the callback shall be kept or not.
862 
863  We distinguish six scenarios (determined by the event type) in which the
864  callback function can be called. The following table describes the values
865  of the parameters @a depth, @a event, and @a parsed.
866 
867  parameter @a event | description | parameter @a depth | parameter @a parsed
868  ------------------ | ----------- | ------------------ | -------------------
869  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
870  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
871  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
872  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
873  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
874  parse_event_t::value | the parser finished reading a JSON value | depth of the value | the parsed JSON value
875 
876  Discarding a value (i.e., returning `false`) has different effects
877  depending on the context in which function was called:
878 
879  - Discarded values in structured types are skipped. That is, the parser
880  will behave as if the discarded value was never read.
881  - In case a value outside a structured type is skipped, it is replaced with
882  `null`. This case happens if the top-level element is skipped.
883 
884  @param[in] depth the depth of the recursion during parsing
885 
886  @param[in] event an event of type parse_event_t indicating the context in
887  the callback function has been called
888 
889  @param[in,out] parsed the current intermediate parse result; note that
890  writing to this value has no effect for parse_event_t::key events
891 
892  @return Whether the JSON value which called the function during parsing
893  should be kept (`true`) or not (`false`). In the latter case, it is either
894  skipped completely or replaced by an empty discarded object.
895 
896  @sa @ref parse(std::istream&, parser_callback_t) or
897  @ref parse(const string_t&, parser_callback_t) for examples
898 
899  @since version 1.0.0
900  */
901  using parser_callback_t = std::function<bool(int depth, parse_event_t event, basic_json& parsed)>;
902 
903 
904  //////////////////
905  // constructors //
906  //////////////////
907 
908  /// @name constructors and destructors
909  /// @{
910 
911  /*!
912  @brief create an empty value with a given type
913 
914  Create an empty JSON value with a given type. The value will be default
915  initialized with an empty value which depends on the type:
916 
917  Value type | initial value
918  ----------- | -------------
919  null | `null`
920  boolean | `false`
921  string | `""`
922  number | `0`
923  object | `{}`
924  array | `[]`
925 
926  @param[in] value_type the type of the value to create
927 
928  @complexity Constant.
929 
930  @throw std::bad_alloc if allocation for object, array, or string value
931  fails
932 
933  @liveexample{The following code shows the constructor for different @ref
934  value_t values,basic_json__value_t}
935 
936  @sa @ref basic_json(std::nullptr_t) -- create a `null` value
937  @sa @ref basic_json(boolean_t value) -- create a boolean value
938  @sa @ref basic_json(const string_t&) -- create a string value
939  @sa @ref basic_json(const object_t&) -- create a object value
940  @sa @ref basic_json(const array_t&) -- create a array value
941  @sa @ref basic_json(const number_float_t) -- create a number
942  (floating-point) value
943  @sa @ref basic_json(const number_integer_t) -- create a number (integer)
944  value
945  @sa @ref basic_json(const number_unsigned_t) -- create a number (unsigned)
946  value
947 
948  @since version 1.0.0
949  */
952  {}
953 
954  /*!
955  @brief create a null object (implicitly)
956 
957  Create a `null` JSON value. This is the implicit version of the `null`
958  value constructor as it takes no parameters.
959 
960  @complexity Constant.
961 
962  @exceptionsafety No-throw guarantee: this constructor never throws
963  exceptions.
964 
965  @requirement This function helps `basic_json` satisfying the
966  [Container](http://en.cppreference.com/w/cpp/concept/Container)
967  requirements:
968  - The complexity is constant.
969  - As postcondition, it holds: `basic_json().empty() == true`.
970 
971  @liveexample{The following code shows the constructor for a `null` JSON
972  value.,basic_json}
973 
974  @sa @ref basic_json(std::nullptr_t) -- create a `null` value
975 
976  @since version 1.0.0
977  */
978  basic_json() = default;
979 
980  /*!
981  @brief create a null object (explicitly)
982 
983  Create a `null` JSON value. This is the explicitly version of the `null`
984  value constructor as it takes a null pointer as parameter. It allows to
985  create `null` values by explicitly assigning a `nullptr` to a JSON value.
986  The passed null pointer itself is not read -- it is only used to choose the
987  right constructor.
988 
989  @complexity Constant.
990 
991  @exceptionsafety No-throw guarantee: this constructor never throws
992  exceptions.
993 
994  @liveexample{The following code shows the constructor with null pointer
995  parameter.,basic_json__nullptr_t}
996 
997  @sa @ref basic_json() -- default constructor (implicitly creating a `null`
998  value)
999 
1000  @since version 1.0.0
1001  */
1002  basic_json(std::nullptr_t) noexcept
1004  {}
1005 
1006  /*!
1007  @brief create an object (explicit)
1008 
1009  Create an object JSON value with a given content.
1010 
1011  @param[in] val a value for the object
1012 
1013  @complexity Linear in the size of the passed @a val.
1014 
1015  @throw std::bad_alloc if allocation for object value fails
1016 
1017  @liveexample{The following code shows the constructor with an @ref object_t
1018  parameter.,basic_json__object_t}
1019 
1020  @sa @ref basic_json(const CompatibleObjectType&) -- create an object value
1021  from a compatible STL container
1022 
1023  @since version 1.0.0
1024  */
1025  basic_json(const object_t& val)
1026  : m_type(value_t::object), m_value(val)
1027  {}
1028 
1029  /*!
1030  @brief create an object (implicit)
1031 
1032  Create an object JSON value with a given content. This constructor allows
1033  any type @a CompatibleObjectType that can be used to construct values of
1034  type @ref object_t.
1035 
1036  @tparam CompatibleObjectType An object type whose `key_type` and
1037  `value_type` is compatible to @ref object_t. Examples include `std::map`,
1038  `std::unordered_map`, `std::multimap`, and `std::unordered_multimap` with
1039  a `key_type` of `std::string`, and a `value_type` from which a @ref
1040  basic_json value can be constructed.
1041 
1042  @param[in] val a value for the object
1043 
1044  @complexity Linear in the size of the passed @a val.
1045 
1046  @throw std::bad_alloc if allocation for object value fails
1047 
1048  @liveexample{The following code shows the constructor with several
1049  compatible object type parameters.,basic_json__CompatibleObjectType}
1050 
1051  @sa @ref basic_json(const object_t&) -- create an object value
1052 
1053  @since version 1.0.0
1054  */
1055  template <class CompatibleObjectType, typename
1056  std::enable_if<
1059  = 0>
1060  basic_json(const CompatibleObjectType& val)
1061  : m_type(value_t::object)
1062  {
1063  using std::begin;
1064  using std::end;
1065  m_value.object = create<object_t>(begin(val), end(val));
1066  }
1067 
1068  /*!
1069  @brief create an array (explicit)
1070 
1071  Create an array JSON value with a given content.
1072 
1073  @param[in] val a value for the array
1074 
1075  @complexity Linear in the size of the passed @a val.
1076 
1077  @throw std::bad_alloc if allocation for array value fails
1078 
1079  @liveexample{The following code shows the constructor with an @ref array_t
1080  parameter.,basic_json__array_t}
1081 
1082  @sa @ref basic_json(const CompatibleArrayType&) -- create an array value
1083  from a compatible STL containers
1084 
1085  @since version 1.0.0
1086  */
1087  basic_json(const array_t& val)
1088  : m_type(value_t::array), m_value(val)
1089  {}
1090 
1091  /*!
1092  @brief create an array (implicit)
1093 
1094  Create an array JSON value with a given content. This constructor allows
1095  any type @a CompatibleArrayType that can be used to construct values of
1096  type @ref array_t.
1097 
1098  @tparam CompatibleArrayType An object type whose `value_type` is compatible
1099  to @ref array_t. Examples include `std::vector`, `std::deque`, `std::list`,
1100  `std::forward_list`, `std::array`, `std::set`, `std::unordered_set`,
1101  `std::multiset`, and `unordered_multiset` with a `value_type` from which a
1102  @ref basic_json value can be constructed.
1103 
1104  @param[in] val a value for the array
1105 
1106  @complexity Linear in the size of the passed @a val.
1107 
1108  @throw std::bad_alloc if allocation for array value fails
1109 
1110  @liveexample{The following code shows the constructor with several
1111  compatible array type parameters.,basic_json__CompatibleArrayType}
1112 
1113  @sa @ref basic_json(const array_t&) -- create an array value
1114 
1115  @since version 1.0.0
1116  */
1117  template <class CompatibleArrayType, typename
1118  std::enable_if<
1126  = 0>
1127  basic_json(const CompatibleArrayType& val)
1128  : m_type(value_t::array)
1129  {
1130  using std::begin;
1131  using std::end;
1132  m_value.array = create<array_t>(begin(val), end(val));
1133  }
1134 
1135  /*!
1136  @brief create a string (explicit)
1137 
1138  Create an string JSON value with a given content.
1139 
1140  @param[in] val a value for the string
1141 
1142  @complexity Linear in the size of the passed @a val.
1143 
1144  @throw std::bad_alloc if allocation for string value fails
1145 
1146  @liveexample{The following code shows the constructor with an @ref string_t
1147  parameter.,basic_json__string_t}
1148 
1149  @sa @ref basic_json(const typename string_t::value_type*) -- create a
1150  string value from a character pointer
1151  @sa @ref basic_json(const CompatibleStringType&) -- create a string value
1152  from a compatible string container
1153 
1154  @since version 1.0.0
1155  */
1156  basic_json(const string_t& val)
1157  : m_type(value_t::string), m_value(val)
1158  {}
1159 
1160  /*!
1161  @brief create a string (explicit)
1162 
1163  Create a string JSON value with a given content.
1164 
1165  @param[in] val a literal value for the string
1166 
1167  @complexity Linear in the size of the passed @a val.
1168 
1169  @throw std::bad_alloc if allocation for string value fails
1170 
1171  @liveexample{The following code shows the constructor with string literal
1172  parameter.,basic_json__string_t_value_type}
1173 
1174  @sa @ref basic_json(const string_t&) -- create a string value
1175  @sa @ref basic_json(const CompatibleStringType&) -- create a string value
1176  from a compatible string container
1177 
1178  @since version 1.0.0
1179  */
1180  basic_json(const typename string_t::value_type* val)
1181  : basic_json(string_t(val))
1182  {}
1183 
1184  /*!
1185  @brief create a string (implicit)
1186 
1187  Create a string JSON value with a given content.
1188 
1189  @param[in] val a value for the string
1190 
1191  @tparam CompatibleStringType an string type which is compatible to @ref
1192  string_t, for instance `std::string`.
1193 
1194  @complexity Linear in the size of the passed @a val.
1195 
1196  @throw std::bad_alloc if allocation for string value fails
1197 
1198  @liveexample{The following code shows the construction of a string value
1199  from a compatible type.,basic_json__CompatibleStringType}
1200 
1201  @sa @ref basic_json(const string_t&) -- create a string value
1202  @sa @ref basic_json(const typename string_t::value_type*) -- create a
1203  string value from a character pointer
1204 
1205  @since version 1.0.0
1206  */
1207  template <class CompatibleStringType, typename
1208  std::enable_if<
1210  = 0>
1211  basic_json(const CompatibleStringType& val)
1212  : basic_json(string_t(val))
1213  {}
1214 
1215  /*!
1216  @brief create a boolean (explicit)
1217 
1218  Creates a JSON boolean type from a given value.
1219 
1220  @param[in] val a boolean value to store
1221 
1222  @complexity Constant.
1223 
1224  @liveexample{The example below demonstrates boolean
1225  values.,basic_json__boolean_t}
1226 
1227  @since version 1.0.0
1228  */
1229  basic_json(boolean_t val) noexcept
1231  {}
1232 
1233  /*!
1234  @brief create an integer number (explicit)
1235 
1236  Create an integer number JSON value with a given content.
1237 
1238  @tparam T A helper type to remove this function via SFINAE in case @ref
1239  number_integer_t is the same as `int`. In this case, this constructor would
1240  have the same signature as @ref basic_json(const int value). Note the
1241  helper type @a T is not visible in this constructor's interface.
1242 
1243  @param[in] val an integer to create a JSON number from
1244 
1245  @complexity Constant.
1246 
1247  @liveexample{The example below shows the construction of an integer
1248  number value.,basic_json__number_integer_t}
1249 
1250  @sa @ref basic_json(const int) -- create a number value (integer)
1251  @sa @ref basic_json(const CompatibleNumberIntegerType) -- create a number
1252  value (integer) from a compatible number type
1253 
1254  @since version 1.0.0
1255  */
1256  template<typename T,
1257  typename std::enable_if<
1260  , int>::type
1261  = 0>
1262  basic_json(const number_integer_t val) noexcept
1264  {}
1265 
1266  /*!
1267  @brief create an integer number from an enum type (explicit)
1268 
1269  Create an integer number JSON value with a given content.
1270 
1271  @param[in] val an integer to create a JSON number from
1272 
1273  @note This constructor allows to pass enums directly to a constructor. As
1274  C++ has no way of specifying the type of an anonymous enum explicitly, we
1275  can only rely on the fact that such values implicitly convert to int. As
1276  int may already be the same type of number_integer_t, we may need to switch
1277  off the constructor @ref basic_json(const number_integer_t).
1278 
1279  @complexity Constant.
1280 
1281  @liveexample{The example below shows the construction of an integer
1282  number value from an anonymous enum.,basic_json__const_int}
1283 
1284  @sa @ref basic_json(const number_integer_t) -- create a number value
1285  (integer)
1286  @sa @ref basic_json(const CompatibleNumberIntegerType) -- create a number
1287  value (integer) from a compatible number type
1288 
1289  @since version 1.0.0
1290  */
1291  basic_json(const int val) noexcept
1293  m_value(static_cast<number_integer_t>(val))
1294  {}
1295 
1296  /*!
1297  @brief create an integer number (implicit)
1298 
1299  Create an integer number JSON value with a given content. This constructor
1300  allows any type @a CompatibleNumberIntegerType that can be used to
1301  construct values of type @ref number_integer_t.
1302 
1303  @tparam CompatibleNumberIntegerType An integer type which is compatible to
1304  @ref number_integer_t. Examples include the types `int`, `int32_t`, `long`,
1305  and `short`.
1306 
1307  @param[in] val an integer to create a JSON number from
1308 
1309  @complexity Constant.
1310 
1311  @liveexample{The example below shows the construction of several integer
1312  number values from compatible
1313  types.,basic_json__CompatibleIntegerNumberType}
1314 
1315  @sa @ref basic_json(const number_integer_t) -- create a number value
1316  (integer)
1317  @sa @ref basic_json(const int) -- create a number value (integer)
1318 
1319  @since version 1.0.0
1320  */
1321  template<typename CompatibleNumberIntegerType, typename
1322  std::enable_if<
1324  std::numeric_limits<CompatibleNumberIntegerType>::is_integer and
1325  std::numeric_limits<CompatibleNumberIntegerType>::is_signed,
1326  CompatibleNumberIntegerType>::type
1327  = 0>
1328  basic_json(const CompatibleNumberIntegerType val) noexcept
1330  m_value(static_cast<number_integer_t>(val))
1331  {}
1332 
1333  /*!
1334  @brief create an unsigned integer number (explicit)
1335 
1336  Create an unsigned integer number JSON value with a given content.
1337 
1338  @tparam T helper type to compare number_unsigned_t and unsigned int
1339  (not visible in) the interface.
1340 
1341  @param[in] val an integer to create a JSON number from
1342 
1343  @complexity Constant.
1344 
1345  @sa @ref basic_json(const CompatibleNumberUnsignedType) -- create a number
1346  value (unsigned integer) from a compatible number type
1347 
1348  @since version 2.0.0
1349  */
1350  template<typename T,
1351  typename std::enable_if<
1354  , int>::type
1355  = 0>
1356  basic_json(const number_unsigned_t val) noexcept
1358  {}
1359 
1360  /*!
1361  @brief create an unsigned number (implicit)
1362 
1363  Create an unsigned number JSON value with a given content. This constructor
1364  allows any type @a CompatibleNumberUnsignedType that can be used to
1365  construct values of type @ref number_unsigned_t.
1366 
1367  @tparam CompatibleNumberUnsignedType An integer type which is compatible to
1368  @ref number_unsigned_t. Examples may include the types `unsigned int`,
1369  `uint32_t`, or `unsigned short`.
1370 
1371  @param[in] val an unsigned integer to create a JSON number from
1372 
1373  @complexity Constant.
1374 
1375  @sa @ref basic_json(const number_unsigned_t) -- create a number value
1376  (unsigned)
1377 
1378  @since version 2.0.0
1379  */
1380  template < typename CompatibleNumberUnsignedType, typename
1381  std::enable_if <
1383  std::numeric_limits<CompatibleNumberUnsignedType>::is_integer and
1384  !std::numeric_limits<CompatibleNumberUnsignedType>::is_signed,
1385  CompatibleNumberUnsignedType >::type
1386  = 0 >
1387  basic_json(const CompatibleNumberUnsignedType val) noexcept
1389  m_value(static_cast<number_unsigned_t>(val))
1390  {}
1391 
1392  /*!
1393  @brief create a floating-point number (explicit)
1394 
1395  Create a floating-point number JSON value with a given content.
1396 
1397  @param[in] val a floating-point value to create a JSON number from
1398 
1399  @note [RFC 7159](http://www.rfc-editor.org/rfc/rfc7159.txt), section 6
1400  disallows NaN values:
1401  > Numeric values that cannot be represented in the grammar below (such
1402  > as Infinity and NaN) are not permitted.
1403  In case the parameter @a val is not a number, a JSON null value is
1404  created instead.
1405 
1406  @complexity Constant.
1407 
1408  @liveexample{The following example creates several floating-point
1409  values.,basic_json__number_float_t}
1410 
1411  @sa @ref basic_json(const CompatibleNumberFloatType) -- create a number
1412  value (floating-point) from a compatible number type
1413 
1414  @since version 1.0.0
1415  */
1416  basic_json(const number_float_t val) noexcept
1418  {
1419  // replace infinity and NAN by null
1420  if (not std::isfinite(val))
1421  {
1423  m_value = json_value();
1424  }
1425  }
1426 
1427  /*!
1428  @brief create an floating-point number (implicit)
1429 
1430  Create an floating-point number JSON value with a given content. This
1431  constructor allows any type @a CompatibleNumberFloatType that can be used
1432  to construct values of type @ref number_float_t.
1433 
1434  @tparam CompatibleNumberFloatType A floating-point type which is compatible
1435  to @ref number_float_t. Examples may include the types `float` or `double`.
1436 
1437  @param[in] val a floating-point to create a JSON number from
1438 
1439  @note [RFC 7159](http://www.rfc-editor.org/rfc/rfc7159.txt), section 6
1440  disallows NaN values:
1441  > Numeric values that cannot be represented in the grammar below (such
1442  > as Infinity and NaN) are not permitted.
1443  In case the parameter @a val is not a number, a JSON null value is
1444  created instead.
1445 
1446  @complexity Constant.
1447 
1448  @liveexample{The example below shows the construction of several
1449  floating-point number values from compatible
1450  types.,basic_json__CompatibleNumberFloatType}
1451 
1452  @sa @ref basic_json(const number_float_t) -- create a number value
1453  (floating-point)
1454 
1455  @since version 1.0.0
1456  */
1457  template<typename CompatibleNumberFloatType, typename = typename
1458  std::enable_if<
1461  >
1462  basic_json(const CompatibleNumberFloatType val) noexcept
1463  : basic_json(number_float_t(val))
1464  {}
1465 
1466  /*!
1467  @brief create a container (array or object) from an initializer list
1468 
1469  Creates a JSON value of type array or object from the passed initializer
1470  list @a init. In case @a type_deduction is `true` (default), the type of
1471  the JSON value to be created is deducted from the initializer list @a init
1472  according to the following rules:
1473 
1474  1. If the list is empty, an empty JSON object value `{}` is created.
1475  2. If the list consists of pairs whose first element is a string, a JSON
1476  object value is created where the first elements of the pairs are treated
1477  as keys and the second elements are as values.
1478  3. In all other cases, an array is created.
1479 
1480  The rules aim to create the best fit between a C++ initializer list and
1481  JSON values. The rationale is as follows:
1482 
1483  1. The empty initializer list is written as `{}` which is exactly an empty
1484  JSON object.
1485  2. C++ has now way of describing mapped types other than to list a list of
1486  pairs. As JSON requires that keys must be of type string, rule 2 is the
1487  weakest constraint one can pose on initializer lists to interpret them as
1488  an object.
1489  3. In all other cases, the initializer list could not be interpreted as
1490  JSON object type, so interpreting it as JSON array type is safe.
1491 
1492  With the rules described above, the following JSON values cannot be
1493  expressed by an initializer list:
1494 
1495  - the empty array (`[]`): use @ref array(std::initializer_list<basic_json>)
1496  with an empty initializer list in this case
1497  - arrays whose elements satisfy rule 2: use @ref
1498  array(std::initializer_list<basic_json>) with the same initializer list
1499  in this case
1500 
1501  @note When used without parentheses around an empty initializer list, @ref
1502  basic_json() is called instead of this function, yielding the JSON null
1503  value.
1504 
1505  @param[in] init initializer list with JSON values
1506 
1507  @param[in] type_deduction internal parameter; when set to `true`, the type
1508  of the JSON value is deducted from the initializer list @a init; when set
1509  to `false`, the type provided via @a manual_type is forced. This mode is
1510  used by the functions @ref array(std::initializer_list<basic_json>) and
1511  @ref object(std::initializer_list<basic_json>).
1512 
1513  @param[in] manual_type internal parameter; when @a type_deduction is set to
1514  `false`, the created JSON value will use the provided type (only @ref
1515  value_t::array and @ref value_t::object are valid); when @a type_deduction
1516  is set to `true`, this parameter has no effect
1517 
1518  @throw std::domain_error if @a type_deduction is `false`, @a manual_type is
1519  `value_t::object`, but @a init contains an element which is not a pair
1520  whose first element is a string; example: `"cannot create object from
1521  initializer list"`
1522 
1523  @complexity Linear in the size of the initializer list @a init.
1524 
1525  @liveexample{The example below shows how JSON values are created from
1526  initializer lists.,basic_json__list_init_t}
1527 
1528  @sa @ref array(std::initializer_list<basic_json>) -- create a JSON array
1529  value from an initializer list
1530  @sa @ref object(std::initializer_list<basic_json>) -- create a JSON object
1531  value from an initializer list
1532 
1533  @since version 1.0.0
1534  */
1535  basic_json(std::initializer_list<basic_json> init,
1536  bool type_deduction = true,
1537  value_t manual_type = value_t::array)
1538  {
1539  // the initializer list could describe an object
1540  bool is_an_object = true;
1541 
1542  // check if each element is an array with two elements whose first
1543  // element is a string
1544  for (const auto& element : init)
1545  {
1546  if (not element.is_array() or element.size() != 2
1547  or not element[0].is_string())
1548  {
1549  // we found an element that makes it impossible to use the
1550  // initializer list as object
1551  is_an_object = false;
1552  break;
1553  }
1554  }
1555 
1556  // adjust type if type deduction is not wanted
1557  if (not type_deduction)
1558  {
1559  // if array is wanted, do not create an object though possible
1560  if (manual_type == value_t::array)
1561  {
1562  is_an_object = false;
1563  }
1564 
1565  // if object is wanted but impossible, throw an exception
1566  if (manual_type == value_t::object and not is_an_object)
1567  {
1568  throw std::domain_error("cannot create object from initializer list");
1569  }
1570  }
1571 
1572  if (is_an_object)
1573  {
1574  // the initializer list is a list of pairs -> create object
1577 
1578  assert(m_value.object != nullptr);
1579 
1580  for (auto& element : init)
1581  {
1582  m_value.object->emplace(std::move(*(element[0].m_value.string)), std::move(element[1]));
1583  }
1584  }
1585  else
1586  {
1587  // the initializer list describes an array -> create array
1589  m_value.array = create<array_t>(std::move(init));
1590  }
1591  }
1592 
1593  /*!
1594  @brief explicitly create an array from an initializer list
1595 
1596  Creates a JSON array value from a given initializer list. That is, given a
1597  list of values `a, b, c`, creates the JSON value `[a, b, c]`. If the
1598  initializer list is empty, the empty array `[]` is created.
1599 
1600  @note This function is only needed to express two edge cases that cannot be
1601  realized with the initializer list constructor (@ref
1602  basic_json(std::initializer_list<basic_json>, bool, value_t)). These cases
1603  are:
1604  1. creating an array whose elements are all pairs whose first element is a
1605  string -- in this case, the initializer list constructor would create an
1606  object, taking the first elements as keys
1607  2. creating an empty array -- passing the empty initializer list to the
1608  initializer list constructor yields an empty object
1609 
1610  @param[in] init initializer list with JSON values to create an array from
1611  (optional)
1612 
1613  @return JSON array value
1614 
1615  @complexity Linear in the size of @a init.
1616 
1617  @liveexample{The following code shows an example for the `array`
1618  function.,array}
1619 
1620  @sa @ref basic_json(std::initializer_list<basic_json>, bool, value_t) --
1621  create a JSON value from an initializer list
1622  @sa @ref object(std::initializer_list<basic_json>) -- create a JSON object
1623  value from an initializer list
1624 
1625  @since version 1.0.0
1626  */
1627  static basic_json array(std::initializer_list<basic_json> init =
1628  std::initializer_list<basic_json>())
1629  {
1630  return basic_json(init, false, value_t::array);
1631  }
1632 
1633  /*!
1634  @brief explicitly create an object from an initializer list
1635 
1636  Creates a JSON object value from a given initializer list. The initializer
1637  lists elements must be pairs, and their first elements must be strings. If
1638  the initializer list is empty, the empty object `{}` is created.
1639 
1640  @note This function is only added for symmetry reasons. In contrast to the
1641  related function @ref array(std::initializer_list<basic_json>), there are
1642  no cases which can only be expressed by this function. That is, any
1643  initializer list @a init can also be passed to the initializer list
1644  constructor
1645  @ref basic_json(std::initializer_list<basic_json>, bool, value_t).
1646 
1647  @param[in] init initializer list to create an object from (optional)
1648 
1649  @return JSON object value
1650 
1651  @throw std::domain_error if @a init is not a pair whose first elements are
1652  strings; thrown by
1653  @ref basic_json(std::initializer_list<basic_json>, bool, value_t)
1654 
1655  @complexity Linear in the size of @a init.
1656 
1657  @liveexample{The following code shows an example for the `object`
1658  function.,object}
1659 
1660  @sa @ref basic_json(std::initializer_list<basic_json>, bool, value_t) --
1661  create a JSON value from an initializer list
1662  @sa @ref array(std::initializer_list<basic_json>) -- create a JSON array
1663  value from an initializer list
1664 
1665  @since version 1.0.0
1666  */
1667  static basic_json object(std::initializer_list<basic_json> init =
1668  std::initializer_list<basic_json>())
1669  {
1670  return basic_json(init, false, value_t::object);
1671  }
1672 
1673  /*!
1674  @brief construct an array with count copies of given value
1675 
1676  Constructs a JSON array value by creating @a cnt copies of a passed
1677  value. In case @a cnt is `0`, an empty array is created. As postcondition,
1678  `std::distance(begin(),end()) == cnt` holds.
1679 
1680  @param[in] cnt the number of JSON copies of @a val to create
1681  @param[in] val the JSON value to copy
1682 
1683  @complexity Linear in @a cnt.
1684 
1685  @liveexample{The following code shows examples for the @ref
1686  basic_json(size_type\, const basic_json&)
1687  constructor.,basic_json__size_type_basic_json}
1688 
1689  @since version 1.0.0
1690  */
1692  : m_type(value_t::array)
1693  {
1694  m_value.array = create<array_t>(cnt, val);
1695  }
1696 
1697  /*!
1698  @brief construct a JSON container given an iterator range
1699 
1700  Constructs the JSON value with the contents of the range `[first, last)`.
1701  The semantics depends on the different types a JSON value can have:
1702  - In case of primitive types (number, boolean, or string), @a first must
1703  be `begin()` and @a last must be `end()`. In this case, the value is
1704  copied. Otherwise, std::out_of_range is thrown.
1705  - In case of structured types (array, object), the constructor behaves
1706  as similar versions for `std::vector`.
1707  - In case of a null type, std::domain_error is thrown.
1708 
1709  @tparam InputIT an input iterator type (@ref iterator or @ref
1710  const_iterator)
1711 
1712  @param[in] first begin of the range to copy from (included)
1713  @param[in] last end of the range to copy from (excluded)
1714 
1715  @throw std::domain_error if iterators are not compatible; that is, do not
1716  belong to the same JSON value; example: `"iterators are not compatible"`
1717  @throw std::out_of_range if iterators are for a primitive type (number,
1718  boolean, or string) where an out of range error can be detected easily;
1719  example: `"iterators out of range"`
1720  @throw std::bad_alloc if allocation for object, array, or string fails
1721  @throw std::domain_error if called with a null value; example: `"cannot use
1722  construct with iterators from null"`
1723 
1724  @complexity Linear in distance between @a first and @a last.
1725 
1726  @liveexample{The example below shows several ways to create JSON values by
1727  specifying a subrange with iterators.,basic_json__InputIt_InputIt}
1728 
1729  @since version 1.0.0
1730  */
1731  template <class InputIT, typename
1732  std::enable_if<
1735  , int>::type
1736  = 0>
1737  basic_json(InputIT first, InputIT last) : m_type(first.m_object->m_type)
1738  {
1739  // make sure iterator fits the current value
1740  if (first.m_object != last.m_object)
1741  {
1742  throw std::domain_error("iterators are not compatible");
1743  }
1744 
1745  // check if iterator range is complete for primitive values
1746  switch (m_type)
1747  {
1748  case value_t::boolean:
1749  case value_t::number_float:
1752  case value_t::string:
1753  {
1754  if (not first.m_it.primitive_iterator.is_begin() or not last.m_it.primitive_iterator.is_end())
1755  {
1756  throw std::out_of_range("iterators out of range");
1757  }
1758  break;
1759  }
1760 
1761  default:
1762  {
1763  break;
1764  }
1765  }
1766 
1767  switch (m_type)
1768  {
1770  {
1771  assert(first.m_object != nullptr);
1772  m_value.number_integer = first.m_object->m_value.number_integer;
1773  break;
1774  }
1775 
1777  {
1778  assert(first.m_object != nullptr);
1779  m_value.number_unsigned = first.m_object->m_value.number_unsigned;
1780  break;
1781  }
1782 
1783  case value_t::number_float:
1784  {
1785  assert(first.m_object != nullptr);
1786  m_value.number_float = first.m_object->m_value.number_float;
1787  break;
1788  }
1789 
1790  case value_t::boolean:
1791  {
1792  assert(first.m_object != nullptr);
1793  m_value.boolean = first.m_object->m_value.boolean;
1794  break;
1795  }
1796 
1797  case value_t::string:
1798  {
1799  assert(first.m_object != nullptr);
1800  m_value = *first.m_object->m_value.string;
1801  break;
1802  }
1803 
1804  case value_t::object:
1805  {
1806  m_value.object = create<object_t>(first.m_it.object_iterator, last.m_it.object_iterator);
1807  break;
1808  }
1809 
1810  case value_t::array:
1811  {
1812  m_value.array = create<array_t>(first.m_it.array_iterator, last.m_it.array_iterator);
1813  break;
1814  }
1815 
1816  default:
1817  {
1818  assert(first.m_object != nullptr);
1819  throw std::domain_error("cannot use construct with iterators from " + first.m_object->type_name());
1820  }
1821  }
1822  }
1823 
1824  /*!
1825  @brief construct a JSON value given an input stream
1826 
1827  @param[in,out] i stream to read a serialized JSON value from
1828  @param[in] cb a parser callback function of type @ref parser_callback_t
1829  which is used to control the deserialization by filtering unwanted values
1830  (optional)
1831 
1832  @complexity Linear in the length of the input. The parser is a predictive
1833  LL(1) parser. The complexity can be higher if the parser callback function
1834  @a cb has a super-linear complexity.
1835 
1836  @note A UTF-8 byte order mark is silently ignored.
1837 
1838  @liveexample{The example below demonstrates constructing a JSON value from
1839  a `std::stringstream` with and without callback
1840  function.,basic_json__istream}
1841 
1842  @since version 2.0.0
1843  */
1844  explicit basic_json(std::istream& i, parser_callback_t cb = nullptr)
1845  {
1846  *this = parser(i, cb).parse();
1847  }
1848 
1849  ///////////////////////////////////////
1850  // other constructors and destructor //
1851  ///////////////////////////////////////
1852 
1853  /*!
1854  @brief copy constructor
1855 
1856  Creates a copy of a given JSON value.
1857 
1858  @param[in] other the JSON value to copy
1859 
1860  @complexity Linear in the size of @a other.
1861 
1862  @requirement This function helps `basic_json` satisfying the
1863  [Container](http://en.cppreference.com/w/cpp/concept/Container)
1864  requirements:
1865  - The complexity is linear.
1866  - As postcondition, it holds: `other == basic_json(other)`.
1867 
1868  @throw std::bad_alloc if allocation for object, array, or string fails.
1869 
1870  @liveexample{The following code shows an example for the copy
1871  constructor.,basic_json__basic_json}
1872 
1873  @since version 1.0.0
1874  */
1875  basic_json(const basic_json& other)
1876  : m_type(other.m_type)
1877  {
1878  switch (m_type)
1879  {
1880  case value_t::object:
1881  {
1882  assert(other.m_value.object != nullptr);
1883  m_value = *other.m_value.object;
1884  break;
1885  }
1886 
1887  case value_t::array:
1888  {
1889  assert(other.m_value.array != nullptr);
1890  m_value = *other.m_value.array;
1891  break;
1892  }
1893 
1894  case value_t::string:
1895  {
1896  assert(other.m_value.string != nullptr);
1897  m_value = *other.m_value.string;
1898  break;
1899  }
1900 
1901  case value_t::boolean:
1902  {
1903  m_value = other.m_value.boolean;
1904  break;
1905  }
1906 
1908  {
1909  m_value = other.m_value.number_integer;
1910  break;
1911  }
1912 
1914  {
1916  break;
1917  }
1918 
1919  case value_t::number_float:
1920  {
1921  m_value = other.m_value.number_float;
1922  break;
1923  }
1924 
1925  default:
1926  {
1927  break;
1928  }
1929  }
1930  }
1931 
1932  /*!
1933  @brief move constructor
1934 
1935  Move constructor. Constructs a JSON value with the contents of the given
1936  value @a other using move semantics. It "steals" the resources from @a
1937  other and leaves it as JSON null value.
1938 
1939  @param[in,out] other value to move to this object
1940 
1941  @post @a other is a JSON null value
1942 
1943  @complexity Constant.
1944 
1945  @liveexample{The code below shows the move constructor explicitly called
1946  via std::move.,basic_json__moveconstructor}
1947 
1948  @since version 1.0.0
1949  */
1950  basic_json(basic_json&& other) noexcept
1951  : m_type(std::move(other.m_type)),
1952  m_value(std::move(other.m_value))
1953  {
1954  // invalidate payload
1955  other.m_type = value_t::null;
1956  other.m_value = {};
1957  }
1958 
1959  /*!
1960  @brief copy assignment
1961 
1962  Copy assignment operator. Copies a JSON value via the "copy and swap"
1963  strategy: It is expressed in terms of the copy constructor, destructor, and
1964  the swap() member function.
1965 
1966  @param[in] other value to copy from
1967 
1968  @complexity Linear.
1969 
1970  @requirement This function helps `basic_json` satisfying the
1971  [Container](http://en.cppreference.com/w/cpp/concept/Container)
1972  requirements:
1973  - The complexity is linear.
1974 
1975  @liveexample{The code below shows and example for the copy assignment. It
1976  creates a copy of value `a` which is then swapped with `b`. Finally\, the
1977  copy of `a` (which is the null value after the swap) is
1978  destroyed.,basic_json__copyassignment}
1979 
1980  @since version 1.0.0
1981  */
1982  reference& operator=(basic_json other) noexcept (
1987  )
1988  {
1989  using std::swap;
1990  swap(m_type, other.m_type);
1991  swap(m_value, other.m_value);
1992  return *this;
1993  }
1994 
1995  /*!
1996  @brief destructor
1997 
1998  Destroys the JSON value and frees all allocated memory.
1999 
2000  @complexity Linear.
2001 
2002  @requirement This function helps `basic_json` satisfying the
2003  [Container](http://en.cppreference.com/w/cpp/concept/Container)
2004  requirements:
2005  - The complexity is linear.
2006  - All stored elements are destroyed and all memory is freed.
2007 
2008  @since version 1.0.0
2009  */
2011  {
2012  switch (m_type)
2013  {
2014  case value_t::object:
2015  {
2016  AllocatorType<object_t> alloc;
2017  alloc.destroy(m_value.object);
2018  alloc.deallocate(m_value.object, 1);
2019  break;
2020  }
2021 
2022  case value_t::array:
2023  {
2024  AllocatorType<array_t> alloc;
2025  alloc.destroy(m_value.array);
2026  alloc.deallocate(m_value.array, 1);
2027  break;
2028  }
2029 
2030  case value_t::string:
2031  {
2032  AllocatorType<string_t> alloc;
2033  alloc.destroy(m_value.string);
2034  alloc.deallocate(m_value.string, 1);
2035  break;
2036  }
2037 
2038  default:
2039  {
2040  // all other types need no specific destructor
2041  break;
2042  }
2043  }
2044  }
2045 
2046  /// @}
2047 
2048  public:
2049  ///////////////////////
2050  // object inspection //
2051  ///////////////////////
2052 
2053  /// @name object inspection
2054  /// @{
2055 
2056  /*!
2057  @brief serialization
2058 
2059  Serialization function for JSON values. The function tries to mimic
2060  Python's @p json.dumps() function, and currently supports its @p indent
2061  parameter.
2062 
2063  @param[in] indent if indent is nonnegative, then array elements and object
2064  members will be pretty-printed with that indent level. An indent level of 0
2065  will only insert newlines. -1 (the default) selects the most compact
2066  representation
2067 
2068  @return string containing the serialization of the JSON value
2069 
2070  @complexity Linear.
2071 
2072  @liveexample{The following example shows the effect of different @a indent
2073  parameters to the result of the serialization.,dump}
2074 
2075  @see https://docs.python.org/2/library/json.html#json.dump
2076 
2077  @since version 1.0.0
2078  */
2079  string_t dump(const int indent = -1) const
2080  {
2081  std::stringstream ss;
2082 
2083  if (indent >= 0)
2084  {
2085  dump(ss, true, static_cast<unsigned int>(indent));
2086  }
2087  else
2088  {
2089  dump(ss, false, 0);
2090  }
2091 
2092  return ss.str();
2093  }
2094 
2095  /*!
2096  @brief return the type of the JSON value (explicit)
2097 
2098  Return the type of the JSON value as a value from the @ref value_t
2099  enumeration.
2100 
2101  @return the type of the JSON value
2102 
2103  @complexity Constant.
2104 
2105  @exceptionsafety No-throw guarantee: this member function never throws
2106  exceptions.
2107 
2108  @liveexample{The following code exemplifies `type()` for all JSON
2109  types.,type}
2110 
2111  @since version 1.0.0
2112  */
2113  constexpr value_t type() const noexcept
2114  {
2115  return m_type;
2116  }
2117 
2118  /*!
2119  @brief return whether type is primitive
2120 
2121  This function returns true iff the JSON type is primitive (string, number,
2122  boolean, or null).
2123 
2124  @return `true` if type is primitive (string, number, boolean, or null),
2125  `false` otherwise.
2126 
2127  @complexity Constant.
2128 
2129  @exceptionsafety No-throw guarantee: this member function never throws
2130  exceptions.
2131 
2132  @liveexample{The following code exemplifies `is_primitive()` for all JSON
2133  types.,is_primitive}
2134 
2135  @sa @ref is_structured() -- returns whether JSON value is structured
2136  @sa @ref is_null() -- returns whether JSON value is `null`
2137  @sa @ref is_string() -- returns whether JSON value is a string
2138  @sa @ref is_boolean() -- returns whether JSON value is a boolean
2139  @sa @ref is_number() -- returns whether JSON value is a number
2140 
2141  @since version 1.0.0
2142  */
2143  constexpr bool is_primitive() const noexcept
2144  {
2145  return is_null() or is_string() or is_boolean() or is_number();
2146  }
2147 
2148  /*!
2149  @brief return whether type is structured
2150 
2151  This function returns true iff the JSON type is structured (array or
2152  object).
2153 
2154  @return `true` if type is structured (array or object), `false` otherwise.
2155 
2156  @complexity Constant.
2157 
2158  @exceptionsafety No-throw guarantee: this member function never throws
2159  exceptions.
2160 
2161  @liveexample{The following code exemplifies `is_structured()` for all JSON
2162  types.,is_structured}
2163 
2164  @sa @ref is_primitive() -- returns whether value is primitive
2165  @sa @ref is_array() -- returns whether value is an array
2166  @sa @ref is_object() -- returns whether value is an object
2167 
2168  @since version 1.0.0
2169  */
2170  constexpr bool is_structured() const noexcept
2171  {
2172  return is_array() or is_object();
2173  }
2174 
2175  /*!
2176  @brief return whether value is null
2177 
2178  This function returns true iff the JSON value is null.
2179 
2180  @return `true` if type is null, `false` otherwise.
2181 
2182  @complexity Constant.
2183 
2184  @exceptionsafety No-throw guarantee: this member function never throws
2185  exceptions.
2186 
2187  @liveexample{The following code exemplifies `is_null()` for all JSON
2188  types.,is_null}
2189 
2190  @since version 1.0.0
2191  */
2192  constexpr bool is_null() const noexcept
2193  {
2194  return m_type == value_t::null;
2195  }
2196 
2197  /*!
2198  @brief return whether value is a boolean
2199 
2200  This function returns true iff the JSON value is a boolean.
2201 
2202  @return `true` if type is boolean, `false` otherwise.
2203 
2204  @complexity Constant.
2205 
2206  @exceptionsafety No-throw guarantee: this member function never throws
2207  exceptions.
2208 
2209  @liveexample{The following code exemplifies `is_boolean()` for all JSON
2210  types.,is_boolean}
2211 
2212  @since version 1.0.0
2213  */
2214  constexpr bool is_boolean() const noexcept
2215  {
2216  return m_type == value_t::boolean;
2217  }
2218 
2219  /*!
2220  @brief return whether value is a number
2221 
2222  This function returns true iff the JSON value is a number. This includes
2223  both integer and floating-point values.
2224 
2225  @return `true` if type is number (regardless whether integer, unsigned
2226  integer or floating-type), `false` otherwise.
2227 
2228  @complexity Constant.
2229 
2230  @exceptionsafety No-throw guarantee: this member function never throws
2231  exceptions.
2232 
2233  @liveexample{The following code exemplifies `is_number()` for all JSON
2234  types.,is_number}
2235 
2236  @sa @ref is_number_integer() -- check if value is an integer or unsigned
2237  integer number
2238  @sa @ref is_number_unsigned() -- check if value is an unsigned integer
2239  number
2240  @sa @ref is_number_float() -- check if value is a floating-point number
2241 
2242  @since version 1.0.0
2243  */
2244  constexpr bool is_number() const noexcept
2245  {
2246  return is_number_integer() or is_number_float();
2247  }
2248 
2249  /*!
2250  @brief return whether value is an integer number
2251 
2252  This function returns true iff the JSON value is an integer or unsigned
2253  integer number. This excludes floating-point values.
2254 
2255  @return `true` if type is an integer or unsigned integer number, `false`
2256  otherwise.
2257 
2258  @complexity Constant.
2259 
2260  @exceptionsafety No-throw guarantee: this member function never throws
2261  exceptions.
2262 
2263  @liveexample{The following code exemplifies `is_number_integer()` for all
2264  JSON types.,is_number_integer}
2265 
2266  @sa @ref is_number() -- check if value is a number
2267  @sa @ref is_number_unsigned() -- check if value is an unsigned integer
2268  number
2269  @sa @ref is_number_float() -- check if value is a floating-point number
2270 
2271  @since version 1.0.0
2272  */
2273  constexpr bool is_number_integer() const noexcept
2274  {
2276  }
2277 
2278  /*!
2279  @brief return whether value is an unsigned integer number
2280 
2281  This function returns true iff the JSON value is an unsigned integer
2282  number. This excludes floating-point and (signed) integer values.
2283 
2284  @return `true` if type is an unsigned integer number, `false` otherwise.
2285 
2286  @complexity Constant.
2287 
2288  @exceptionsafety No-throw guarantee: this member function never throws
2289  exceptions.
2290 
2291  @liveexample{The following code exemplifies `is_number_unsigned()` for all
2292  JSON types.,is_number_unsigned}
2293 
2294  @sa @ref is_number() -- check if value is a number
2295  @sa @ref is_number_integer() -- check if value is an integer or unsigned
2296  integer number
2297  @sa @ref is_number_float() -- check if value is a floating-point number
2298 
2299  @since version 2.0.0
2300  */
2301  constexpr bool is_number_unsigned() const noexcept
2302  {
2303  return m_type == value_t::number_unsigned;
2304  }
2305 
2306  /*!
2307  @brief return whether value is a floating-point number
2308 
2309  This function returns true iff the JSON value is a floating-point number.
2310  This excludes integer and unsigned integer values.
2311 
2312  @return `true` if type is a floating-point number, `false` otherwise.
2313 
2314  @complexity Constant.
2315 
2316  @exceptionsafety No-throw guarantee: this member function never throws
2317  exceptions.
2318 
2319  @liveexample{The following code exemplifies `is_number_float()` for all
2320  JSON types.,is_number_float}
2321 
2322  @sa @ref is_number() -- check if value is number
2323  @sa @ref is_number_integer() -- check if value is an integer number
2324  @sa @ref is_number_unsigned() -- check if value is an unsigned integer
2325  number
2326 
2327  @since version 1.0.0
2328  */
2329  constexpr bool is_number_float() const noexcept
2330  {
2331  return m_type == value_t::number_float;
2332  }
2333 
2334  /*!
2335  @brief return whether value is an object
2336 
2337  This function returns true iff the JSON value is an object.
2338 
2339  @return `true` if type is object, `false` otherwise.
2340 
2341  @complexity Constant.
2342 
2343  @exceptionsafety No-throw guarantee: this member function never throws
2344  exceptions.
2345 
2346  @liveexample{The following code exemplifies `is_object()` for all JSON
2347  types.,is_object}
2348 
2349  @since version 1.0.0
2350  */
2351  constexpr bool is_object() const noexcept
2352  {
2353  return m_type == value_t::object;
2354  }
2355 
2356  /*!
2357  @brief return whether value is an array
2358 
2359  This function returns true iff the JSON value is an array.
2360 
2361  @return `true` if type is array, `false` otherwise.
2362 
2363  @complexity Constant.
2364 
2365  @exceptionsafety No-throw guarantee: this member function never throws
2366  exceptions.
2367 
2368  @liveexample{The following code exemplifies `is_array()` for all JSON
2369  types.,is_array}
2370 
2371  @since version 1.0.0
2372  */
2373  constexpr bool is_array() const noexcept
2374  {
2375  return m_type == value_t::array;
2376  }
2377 
2378  /*!
2379  @brief return whether value is a string
2380 
2381  This function returns true iff the JSON value is a string.
2382 
2383  @return `true` if type is string, `false` otherwise.
2384 
2385  @complexity Constant.
2386 
2387  @exceptionsafety No-throw guarantee: this member function never throws
2388  exceptions.
2389 
2390  @liveexample{The following code exemplifies `is_string()` for all JSON
2391  types.,is_string}
2392 
2393  @since version 1.0.0
2394  */
2395  constexpr bool is_string() const noexcept
2396  {
2397  return m_type == value_t::string;
2398  }
2399 
2400  /*!
2401  @brief return whether value is discarded
2402 
2403  This function returns true iff the JSON value was discarded during parsing
2404  with a callback function (see @ref parser_callback_t).
2405 
2406  @note This function will always be `false` for JSON values after parsing.
2407  That is, discarded values can only occur during parsing, but will be
2408  removed when inside a structured value or replaced by null in other cases.
2409 
2410  @return `true` if type is discarded, `false` otherwise.
2411 
2412  @complexity Constant.
2413 
2414  @exceptionsafety No-throw guarantee: this member function never throws
2415  exceptions.
2416 
2417  @liveexample{The following code exemplifies `is_discarded()` for all JSON
2418  types.,is_discarded}
2419 
2420  @since version 1.0.0
2421  */
2422  constexpr bool is_discarded() const noexcept
2423  {
2424  return m_type == value_t::discarded;
2425  }
2426 
2427  /*!
2428  @brief return the type of the JSON value (implicit)
2429 
2430  Implicitly return the type of the JSON value as a value from the @ref
2431  value_t enumeration.
2432 
2433  @return the type of the JSON value
2434 
2435  @complexity Constant.
2436 
2437  @exceptionsafety No-throw guarantee: this member function never throws
2438  exceptions.
2439 
2440  @liveexample{The following code exemplifies the @ref value_t operator for
2441  all JSON types.,operator__value_t}
2442 
2443  @since version 1.0.0
2444  */
2445  constexpr operator value_t() const noexcept
2446  {
2447  return m_type;
2448  }
2449 
2450  /// @}
2451 
2452  private:
2453  //////////////////
2454  // value access //
2455  //////////////////
2456 
2457  /// get an object (explicit)
2458  template <class T, typename
2459  std::enable_if<
2462  , int>::type = 0>
2463  T get_impl(T*) const
2464  {
2465  if (is_object())
2466  {
2467  assert(m_value.object != nullptr);
2468  return T(m_value.object->begin(), m_value.object->end());
2469  }
2470  else
2471  {
2472  throw std::domain_error("type must be object, but is " + type_name());
2473  }
2474  }
2475 
2476  /// get an object (explicit)
2478  {
2479  if (is_object())
2480  {
2481  assert(m_value.object != nullptr);
2482  return *(m_value.object);
2483  }
2484  else
2485  {
2486  throw std::domain_error("type must be object, but is " + type_name());
2487  }
2488  }
2489 
2490  /// get an array (explicit)
2491  template <class T, typename
2492  std::enable_if<
2498  , int>::type = 0>
2499  T get_impl(T*) const
2500  {
2501  if (is_array())
2502  {
2503  T to_vector;
2504  assert(m_value.array != nullptr);
2505  std::transform(m_value.array->begin(), m_value.array->end(),
2506  std::inserter(to_vector, to_vector.end()), [](basic_json i)
2507  {
2508  return i.get<typename T::value_type>();
2509  });
2510  return to_vector;
2511  }
2512  else
2513  {
2514  throw std::domain_error("type must be array, but is " + type_name());
2515  }
2516  }
2517 
2518  /// get an array (explicit)
2519  template <class T, typename
2520  std::enable_if<
2523  , int>::type = 0>
2525  {
2526  if (is_array())
2527  {
2528  std::vector<T> to_vector;
2529  assert(m_value.array != nullptr);
2530  to_vector.reserve(m_value.array->size());
2531  std::transform(m_value.array->begin(), m_value.array->end(),
2532  std::inserter(to_vector, to_vector.end()), [](basic_json i)
2533  {
2534  return i.get<T>();
2535  });
2536  return to_vector;
2537  }
2538  else
2539  {
2540  throw std::domain_error("type must be array, but is " + type_name());
2541  }
2542  }
2543 
2544  /// get an array (explicit)
2545  template <class T, typename
2546  std::enable_if<
2549  , int>::type = 0>
2550  T get_impl(T*) const
2551  {
2552  if (is_array())
2553  {
2554  assert(m_value.array != nullptr);
2555  return T(m_value.array->begin(), m_value.array->end());
2556  }
2557  else
2558  {
2559  throw std::domain_error("type must be array, but is " + type_name());
2560  }
2561  }
2562 
2563  /// get an array (explicit)
2565  {
2566  if (is_array())
2567  {
2568  assert(m_value.array != nullptr);
2569  return *(m_value.array);
2570  }
2571  else
2572  {
2573  throw std::domain_error("type must be array, but is " + type_name());
2574  }
2575  }
2576 
2577  /// get a string (explicit)
2578  template <typename T, typename
2579  std::enable_if<
2581  , int>::type = 0>
2582  T get_impl(T*) const
2583  {
2584  if (is_string())
2585  {
2586  assert(m_value.string != nullptr);
2587  return *m_value.string;
2588  }
2589  else
2590  {
2591  throw std::domain_error("type must be string, but is " + type_name());
2592  }
2593  }
2594 
2595  /// get a number (explicit)
2596  template<typename T, typename
2597  std::enable_if<
2599  , int>::type = 0>
2600  T get_impl(T*) const
2601  {
2602  switch (m_type)
2603  {
2605  {
2606  return static_cast<T>(m_value.number_integer);
2607  }
2608 
2610  {
2611  return static_cast<T>(m_value.number_unsigned);
2612  }
2613 
2614  case value_t::number_float:
2615  {
2616  return static_cast<T>(m_value.number_float);
2617  }
2618 
2619  default:
2620  {
2621  throw std::domain_error("type must be number, but is " + type_name());
2622  }
2623  }
2624  }
2625 
2626  /// get a boolean (explicit)
2627  constexpr boolean_t get_impl(boolean_t*) const
2628  {
2629  return is_boolean()
2630  ? m_value.boolean
2631  : throw std::domain_error("type must be boolean, but is " + type_name());
2632  }
2633 
2634  /// get a pointer to the value (object)
2636  {
2637  return is_object() ? m_value.object : nullptr;
2638  }
2639 
2640  /// get a pointer to the value (object)
2641  constexpr const object_t* get_impl_ptr(const object_t*) const noexcept
2642  {
2643  return is_object() ? m_value.object : nullptr;
2644  }
2645 
2646  /// get a pointer to the value (array)
2648  {
2649  return is_array() ? m_value.array : nullptr;
2650  }
2651 
2652  /// get a pointer to the value (array)
2653  constexpr const array_t* get_impl_ptr(const array_t*) const noexcept
2654  {
2655  return is_array() ? m_value.array : nullptr;
2656  }
2657 
2658  /// get a pointer to the value (string)
2660  {
2661  return is_string() ? m_value.string : nullptr;
2662  }
2663 
2664  /// get a pointer to the value (string)
2665  constexpr const string_t* get_impl_ptr(const string_t*) const noexcept
2666  {
2667  return is_string() ? m_value.string : nullptr;
2668  }
2669 
2670  /// get a pointer to the value (boolean)
2672  {
2673  return is_boolean() ? &m_value.boolean : nullptr;
2674  }
2675 
2676  /// get a pointer to the value (boolean)
2677  constexpr const boolean_t* get_impl_ptr(const boolean_t*) const noexcept
2678  {
2679  return is_boolean() ? &m_value.boolean : nullptr;
2680  }
2681 
2682  /// get a pointer to the value (integer number)
2684  {
2685  return is_number_integer() ? &m_value.number_integer : nullptr;
2686  }
2687 
2688  /// get a pointer to the value (integer number)
2689  constexpr const number_integer_t* get_impl_ptr(const number_integer_t*) const noexcept
2690  {
2691  return is_number_integer() ? &m_value.number_integer : nullptr;
2692  }
2693 
2694  /// get a pointer to the value (unsigned number)
2696  {
2697  return is_number_unsigned() ? &m_value.number_unsigned : nullptr;
2698  }
2699 
2700  /// get a pointer to the value (unsigned number)
2701  constexpr const number_unsigned_t* get_impl_ptr(const number_unsigned_t*) const noexcept
2702  {
2703  return is_number_unsigned() ? &m_value.number_unsigned : nullptr;
2704  }
2705 
2706  /// get a pointer to the value (floating-point number)
2708  {
2709  return is_number_float() ? &m_value.number_float : nullptr;
2710  }
2711 
2712  /// get a pointer to the value (floating-point number)
2713  constexpr const number_float_t* get_impl_ptr(const number_float_t*) const noexcept
2714  {
2715  return is_number_float() ? &m_value.number_float : nullptr;
2716  }
2717 
2718  /*!
2719  @brief helper function to implement get_ref()
2720 
2721  This funcion helps to implement get_ref() without code duplication for
2722  const and non-const overloads
2723 
2724  @tparam ThisType will be deduced as `basic_json` or `const basic_json`
2725 
2726  @throw std::domain_error if ReferenceType does not match underlying value
2727  type of the current JSON
2728  */
2729  template<typename ReferenceType, typename ThisType>
2730  static ReferenceType get_ref_impl(ThisType& obj)
2731  {
2732  // delegate the call to get_ptr<>()
2733  using PointerType = typename std::add_pointer<ReferenceType>::type;
2734  auto ptr = obj.template get_ptr<PointerType>();
2735 
2736  if (ptr != nullptr)
2737  {
2738  return *ptr;
2739  }
2740  else
2741  {
2742  throw std::domain_error("incompatible ReferenceType for get_ref, actual type is " +
2743  obj.type_name());
2744  }
2745  }
2746 
2747  public:
2748 
2749  /// @name value access
2750  /// @{
2751 
2752  /*!
2753  @brief get a value (explicit)
2754 
2755  Explicit type conversion between the JSON value and a compatible value.
2756 
2757  @tparam ValueType non-pointer type compatible to the JSON value, for
2758  instance `int` for JSON integer numbers, `bool` for JSON booleans, or
2759  `std::vector` types for JSON arrays
2760 
2761  @return copy of the JSON value, converted to type @a ValueType
2762 
2763  @throw std::domain_error in case passed type @a ValueType is incompatible
2764  to JSON; example: `"type must be object, but is null"`
2765 
2766  @complexity Linear in the size of the JSON value.
2767 
2768  @liveexample{The example below shows several conversions from JSON values
2769  to other types. There a few things to note: (1) Floating-point numbers can
2770  be converted to integers\, (2) A JSON array can be converted to a standard
2771  `std::vector<short>`\, (3) A JSON object can be converted to C++
2772  associative containers such as `std::unordered_map<std::string\,
2773  json>`.,get__ValueType_const}
2774 
2775  @internal
2776  The idea of using a casted null pointer to choose the correct
2777  implementation is from <http://stackoverflow.com/a/8315197/266378>.
2778  @endinternal
2779 
2780  @sa @ref operator ValueType() const for implicit conversion
2781  @sa @ref get() for pointer-member access
2782 
2783  @since version 1.0.0
2784  */
2785  template<typename ValueType, typename
2786  std::enable_if<
2788  , int>::type = 0>
2789  ValueType get() const
2790  {
2791  return get_impl(static_cast<ValueType*>(nullptr));
2792  }
2793 
2794  /*!
2795  @brief get a pointer value (explicit)
2796 
2797  Explicit pointer access to the internally stored JSON value. No copies are
2798  made.
2799 
2800  @warning The pointer becomes invalid if the underlying JSON object changes.
2801 
2802  @tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
2803  object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
2804  @ref number_unsigned_t, or @ref number_float_t.
2805 
2806  @return pointer to the internally stored JSON value if the requested
2807  pointer type @a PointerType fits to the JSON value; `nullptr` otherwise
2808 
2809  @complexity Constant.
2810 
2811  @liveexample{The example below shows how pointers to internal values of a
2812  JSON value can be requested. Note that no type conversions are made and a
2813  `nullptr` is returned if the value and the requested pointer type does not
2814  match.,get__PointerType}
2815 
2816  @sa @ref get_ptr() for explicit pointer-member access
2817 
2818  @since version 1.0.0
2819  */
2820  template<typename PointerType, typename
2821  std::enable_if<
2823  , int>::type = 0>
2824  PointerType get() noexcept
2825  {
2826  // delegate the call to get_ptr
2827  return get_ptr<PointerType>();
2828  }
2829 
2830  /*!
2831  @brief get a pointer value (explicit)
2832  @copydoc get()
2833  */
2834  template<typename PointerType, typename
2835  std::enable_if<
2837  , int>::type = 0>
2838  constexpr const PointerType get() const noexcept
2839  {
2840  // delegate the call to get_ptr
2841  return get_ptr<PointerType>();
2842  }
2843 
2844  /*!
2845  @brief get a pointer value (implicit)
2846 
2847  Implicit pointer access to the internally stored JSON value. No copies are
2848  made.
2849 
2850  @warning Writing data to the pointee of the result yields an undefined
2851  state.
2852 
2853  @tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
2854  object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
2855  @ref number_unsigned_t, or @ref number_float_t.
2856 
2857  @return pointer to the internally stored JSON value if the requested
2858  pointer type @a PointerType fits to the JSON value; `nullptr` otherwise
2859 
2860  @complexity Constant.
2861 
2862  @liveexample{The example below shows how pointers to internal values of a
2863  JSON value can be requested. Note that no type conversions are made and a
2864  `nullptr` is returned if the value and the requested pointer type does not
2865  match.,get_ptr}
2866 
2867  @since version 1.0.0
2868  */
2869  template<typename PointerType, typename
2870  std::enable_if<
2872  , int>::type = 0>
2873  PointerType get_ptr() noexcept
2874  {
2875  // delegate the call to get_impl_ptr<>()
2876  return get_impl_ptr(static_cast<PointerType>(nullptr));
2877  }
2878 
2879  /*!
2880  @brief get a pointer value (implicit)
2881  @copydoc get_ptr()
2882  */
2883  template<typename PointerType, typename
2884  std::enable_if<
2886  and std::is_const<typename std::remove_pointer<PointerType>::type>::value
2887  , int>::type = 0>
2888  constexpr const PointerType get_ptr() const noexcept
2889  {
2890  // delegate the call to get_impl_ptr<>() const
2891  return get_impl_ptr(static_cast<const PointerType>(nullptr));
2892  }
2893 
2894  /*!
2895  @brief get a reference value (implicit)
2896 
2897  Implict reference access to the internally stored JSON value. No copies are
2898  made.
2899 
2900  @warning Writing data to the referee of the result yields an undefined
2901  state.
2902 
2903  @tparam ReferenceType reference type; must be a reference to @ref array_t,
2904  @ref object_t, @ref string_t, @ref boolean_t, @ref number_integer_t, or
2905  @ref number_float_t.
2906 
2907  @return reference to the internally stored JSON value if the requested
2908  reference type @a ReferenceType fits to the JSON value; throws
2909  std::domain_error otherwise
2910 
2911  @throw std::domain_error in case passed type @a ReferenceType is
2912  incompatible with the stored JSON value
2913 
2914  @complexity Constant.
2915 
2916  @liveexample{The example shows several calls to `get_ref()`.,get_ref}
2917 
2918  @since version 1.1.0
2919  */
2920  template<typename ReferenceType, typename
2921  std::enable_if<
2923  , int>::type = 0>
2924  ReferenceType get_ref()
2925  {
2926  // delegate call to get_ref_impl
2927  return get_ref_impl<ReferenceType>(*this);
2928  }
2929 
2930  /*!
2931  @brief get a reference value (implicit)
2932  @copydoc get_ref()
2933  */
2934  template<typename ReferenceType, typename
2935  std::enable_if<
2937  and std::is_const<typename std::remove_reference<ReferenceType>::type>::value
2938  , int>::type = 0>
2939  ReferenceType get_ref() const
2940  {
2941  // delegate call to get_ref_impl
2942  return get_ref_impl<ReferenceType>(*this);
2943  }
2944 
2945  /*!
2946  @brief get a value (implicit)
2947 
2948  Implicit type conversion between the JSON value and a compatible value. The
2949  call is realized by calling @ref get() const.
2950 
2951  @tparam ValueType non-pointer type compatible to the JSON value, for
2952  instance `int` for JSON integer numbers, `bool` for JSON booleans, or
2953  `std::vector` types for JSON arrays. The character type of @ref string_t as
2954  well as an initializer list of this type is excluded to avoid ambiguities
2955  as these types implicitly convert to `std::string`.
2956 
2957  @return copy of the JSON value, converted to type @a ValueType
2958 
2959  @throw std::domain_error in case passed type @a ValueType is incompatible
2960  to JSON, thrown by @ref get() const
2961 
2962  @complexity Linear in the size of the JSON value.
2963 
2964  @liveexample{The example below shows several conversions from JSON values
2965  to other types. There a few things to note: (1) Floating-point numbers can
2966  be converted to integers\, (2) A JSON array can be converted to a standard
2967  `std::vector<short>`\, (3) A JSON object can be converted to C++
2968  associative containers such as `std::unordered_map<std::string\,
2969  json>`.,operator__ValueType}
2970 
2971  @since version 1.0.0
2972  */
2973  template < typename ValueType, typename
2974  std::enable_if <
2977 #ifndef _MSC_VER // Fix for issue #167 operator<< abiguity under VS2015
2978  and not std::is_same<ValueType, std::initializer_list<typename string_t::value_type>>::value
2979 #endif
2980  , int >::type = 0 >
2981  operator ValueType() const
2982  {
2983  // delegate the call to get<>() const
2984  return get<ValueType>();
2985  }
2986 
2987  /// @}
2988 
2989 
2990  ////////////////////
2991  // element access //
2992  ////////////////////
2993 
2994  /// @name element access
2995  /// @{
2996 
2997  /*!
2998  @brief access specified array element with bounds checking
2999 
3000  Returns a reference to the element at specified location @a idx, with
3001  bounds checking.
3002 
3003  @param[in] idx index of the element to access
3004 
3005  @return reference to the element at index @a idx
3006 
3007  @throw std::domain_error if the JSON value is not an array; example:
3008  `"cannot use at() with string"`
3009  @throw std::out_of_range if the index @a idx is out of range of the array;
3010  that is, `idx >= size()`; example: `"array index 7 is out of range"`
3011 
3012  @complexity Constant.
3013 
3014  @liveexample{The example below shows how array elements can be read and
3015  written using `at()`.,at__size_type}
3016 
3017  @since version 1.0.0
3018  */
3020  {
3021  // at only works for arrays
3022  if (is_array())
3023  {
3024  try
3025  {
3026  assert(m_value.array != nullptr);
3027  return m_value.array->at(idx);
3028  }
3029  catch (std::out_of_range&)
3030  {
3031  // create better exception explanation
3032  throw std::out_of_range("array index " + std::to_string(idx) + " is out of range");
3033  }
3034  }
3035  else
3036  {
3037  throw std::domain_error("cannot use at() with " + type_name());
3038  }
3039  }
3040 
3041  /*!
3042  @brief access specified array element with bounds checking
3043 
3044  Returns a const reference to the element at specified location @a idx, with
3045  bounds checking.
3046 
3047  @param[in] idx index of the element to access
3048 
3049  @return const reference to the element at index @a idx
3050 
3051  @throw std::domain_error if the JSON value is not an array; example:
3052  `"cannot use at() with string"`
3053  @throw std::out_of_range if the index @a idx is out of range of the array;
3054  that is, `idx >= size()`; example: `"array index 7 is out of range"`
3055 
3056  @complexity Constant.
3057 
3058  @liveexample{The example below shows how array elements can be read using
3059  `at()`.,at__size_type_const}
3060 
3061  @since version 1.0.0
3062  */
3064  {
3065  // at only works for arrays
3066  if (is_array())
3067  {
3068  try
3069  {
3070  assert(m_value.array != nullptr);
3071  return m_value.array->at(idx);
3072  }
3073  catch (std::out_of_range&)
3074  {
3075  // create better exception explanation
3076  throw std::out_of_range("array index " + std::to_string(idx) + " is out of range");
3077  }
3078  }
3079  else
3080  {
3081  throw std::domain_error("cannot use at() with " + type_name());
3082  }
3083  }
3084 
3085  /*!
3086  @brief access specified object element with bounds checking
3087 
3088  Returns a reference to the element at with specified key @a key, with
3089  bounds checking.
3090 
3091  @param[in] key key of the element to access
3092 
3093  @return reference to the element at key @a key
3094 
3095  @throw std::domain_error if the JSON value is not an object; example:
3096  `"cannot use at() with boolean"`
3097  @throw std::out_of_range if the key @a key is is not stored in the object;
3098  that is, `find(key) == end()`; example: `"key "the fast" not found"`
3099 
3100  @complexity Logarithmic in the size of the container.
3101 
3102  @liveexample{The example below shows how object elements can be read and
3103  written using `at()`.,at__object_t_key_type}
3104 
3105  @sa @ref operator[](const typename object_t::key_type&) for unchecked
3106  access by reference
3107  @sa @ref value() for access by value with a default value
3108 
3109  @since version 1.0.0
3110  */
3111  reference at(const typename object_t::key_type& key)
3112  {
3113  // at only works for objects
3114  if (is_object())
3115  {
3116  try
3117  {
3118  assert(m_value.object != nullptr);
3119  return m_value.object->at(key);
3120  }
3121  catch (std::out_of_range&)
3122  {
3123  // create better exception explanation
3124  throw std::out_of_range("key '" + key + "' not found");
3125  }
3126  }
3127  else
3128  {
3129  throw std::domain_error("cannot use at() with " + type_name());
3130  }
3131  }
3132 
3133  /*!
3134  @brief access specified object element with bounds checking
3135 
3136  Returns a const reference to the element at with specified key @a key, with
3137  bounds checking.
3138 
3139  @param[in] key key of the element to access
3140 
3141  @return const reference to the element at key @a key
3142 
3143  @throw std::domain_error if the JSON value is not an object; example:
3144  `"cannot use at() with boolean"`
3145  @throw std::out_of_range if the key @a key is is not stored in the object;
3146  that is, `find(key) == end()`; example: `"key "the fast" not found"`
3147 
3148  @complexity Logarithmic in the size of the container.
3149 
3150  @liveexample{The example below shows how object elements can be read using
3151  `at()`.,at__object_t_key_type_const}
3152 
3153  @sa @ref operator[](const typename object_t::key_type&) for unchecked
3154  access by reference
3155  @sa @ref value() for access by value with a default value
3156 
3157  @since version 1.0.0
3158  */
3159  const_reference at(const typename object_t::key_type& key) const
3160  {
3161  // at only works for objects
3162  if (is_object())
3163  {
3164  try
3165  {
3166  assert(m_value.object != nullptr);
3167  return m_value.object->at(key);
3168  }
3169  catch (std::out_of_range&)
3170  {
3171  // create better exception explanation
3172  throw std::out_of_range("key '" + key + "' not found");
3173  }
3174  }
3175  else
3176  {
3177  throw std::domain_error("cannot use at() with " + type_name());
3178  }
3179  }
3180 
3181  /*!
3182  @brief access specified array element
3183 
3184  Returns a reference to the element at specified location @a idx.
3185 
3186  @note If @a idx is beyond the range of the array (i.e., `idx >= size()`),
3187  then the array is silently filled up with `null` values to make `idx` a
3188  valid reference to the last stored element.
3189 
3190  @param[in] idx index of the element to access
3191 
3192  @return reference to the element at index @a idx
3193 
3194  @throw std::domain_error if JSON is not an array or null; example: `"cannot
3195  use operator[] with string"`
3196 
3197  @complexity Constant if @a idx is in the range of the array. Otherwise
3198  linear in `idx - size()`.
3199 
3200  @liveexample{The example below shows how array elements can be read and
3201  written using `[]` operator. Note the addition of `null`
3202  values.,operatorarray__size_type}
3203 
3204  @since version 1.0.0
3205  */
3207  {
3208  // implicitly convert null value to an empty array
3209  if (is_null())
3210  {
3212  m_value.array = create<array_t>();
3213  }
3214 
3215  // operator[] only works for arrays
3216  if (is_array())
3217  {
3218  // fill up array with null values until given idx is reached
3219  assert(m_value.array != nullptr);
3220  for (size_t i = m_value.array->size(); i <= idx; ++i)
3221  {
3222  m_value.array->push_back(basic_json());
3223  }
3224 
3225  return m_value.array->operator[](idx);
3226  }
3227  else
3228  {
3229  throw std::domain_error("cannot use operator[] with " + type_name());
3230  }
3231  }
3232 
3233  /*!
3234  @brief access specified array element
3235 
3236  Returns a const reference to the element at specified location @a idx.
3237 
3238  @param[in] idx index of the element to access
3239 
3240  @return const reference to the element at index @a idx
3241 
3242  @throw std::domain_error if JSON is not an array; example: `"cannot use
3243  operator[] with null"`
3244 
3245  @complexity Constant.
3246 
3247  @liveexample{The example below shows how array elements can be read using
3248  the `[]` operator.,operatorarray__size_type_const}
3249 
3250  @since version 1.0.0
3251  */
3253  {
3254  // const operator[] only works for arrays
3255  if (is_array())
3256  {
3257  assert(m_value.array != nullptr);
3258  return m_value.array->operator[](idx);
3259  }
3260  else
3261  {
3262  throw std::domain_error("cannot use operator[] with " + type_name());
3263  }
3264  }
3265 
3266  /*!
3267  @brief access specified object element
3268 
3269  Returns a reference to the element at with specified key @a key.
3270 
3271  @note If @a key is not found in the object, then it is silently added to
3272  the object and filled with a `null` value to make `key` a valid reference.
3273  In case the value was `null` before, it is converted to an object.
3274 
3275  @param[in] key key of the element to access
3276 
3277  @return reference to the element at key @a key
3278 
3279  @throw std::domain_error if JSON is not an object or null; example:
3280  `"cannot use operator[] with string"`
3281 
3282  @complexity Logarithmic in the size of the container.
3283 
3284  @liveexample{The example below shows how object elements can be read and
3285  written using the `[]` operator.,operatorarray__key_type}
3286 
3287  @sa @ref at(const typename object_t::key_type&) for access by reference
3288  with range checking
3289  @sa @ref value() for access by value with a default value
3290 
3291  @since version 1.0.0
3292  */
3293  reference operator[](const typename object_t::key_type& key)
3294  {
3295  // implicitly convert null value to an empty object
3296  if (is_null())
3297  {
3299  m_value.object = create<object_t>();
3300  }
3301 
3302  // operator[] only works for objects
3303  if (is_object())
3304  {
3305  assert(m_value.object != nullptr);
3306  return m_value.object->operator[](key);
3307  }
3308  else
3309  {
3310  throw std::domain_error("cannot use operator[] with " + type_name());
3311  }
3312  }
3313 
3314  /*!
3315  @brief read-only access specified object element
3316 
3317  Returns a const reference to the element at with specified key @a key. No
3318  bounds checking is performed.
3319 
3320  @warning If the element with key @a key does not exist, the behavior is
3321  undefined.
3322 
3323  @param[in] key key of the element to access
3324 
3325  @return const reference to the element at key @a key
3326 
3327  @throw std::domain_error if JSON is not an object; example: `"cannot use
3328  operator[] with null"`
3329 
3330  @complexity Logarithmic in the size of the container.
3331 
3332  @liveexample{The example below shows how object elements can be read using
3333  the `[]` operator.,operatorarray__key_type_const}
3334 
3335  @sa @ref at(const typename object_t::key_type&) for access by reference
3336  with range checking
3337  @sa @ref value() for access by value with a default value
3338 
3339  @since version 1.0.0
3340  */
3341  const_reference operator[](const typename object_t::key_type& key) const
3342  {
3343  // const operator[] only works for objects
3344  if (is_object())
3345  {
3346  assert(m_value.object != nullptr);
3347  assert(m_value.object->find(key) != m_value.object->end());
3348  return m_value.object->find(key)->second;
3349  }
3350  else
3351  {
3352  throw std::domain_error("cannot use operator[] with " + type_name());
3353  }
3354  }
3355 
3356  /*!
3357  @brief access specified object element
3358 
3359  Returns a reference to the element at with specified key @a key.
3360 
3361  @note If @a key is not found in the object, then it is silently added to
3362  the object and filled with a `null` value to make `key` a valid reference.
3363  In case the value was `null` before, it is converted to an object.
3364 
3365  @param[in] key key of the element to access
3366 
3367  @return reference to the element at key @a key
3368 
3369  @throw std::domain_error if JSON is not an object or null; example:
3370  `"cannot use operator[] with string"`
3371 
3372  @complexity Logarithmic in the size of the container.
3373 
3374  @liveexample{The example below shows how object elements can be read and
3375  written using the `[]` operator.,operatorarray__key_type}
3376 
3377  @sa @ref at(const typename object_t::key_type&) for access by reference
3378  with range checking
3379  @sa @ref value() for access by value with a default value
3380 
3381  @since version 1.0.0
3382  */
3383  template<typename T, std::size_t n>
3384  reference operator[](T * (&key)[n])
3385  {
3386  return operator[](static_cast<const T>(key));
3387  }
3388 
3389  /*!
3390  @brief read-only access specified object element
3391 
3392  Returns a const reference to the element at with specified key @a key. No
3393  bounds checking is performed.
3394 
3395  @warning If the element with key @a key does not exist, the behavior is
3396  undefined.
3397 
3398  @note This function is required for compatibility reasons with Clang.
3399 
3400  @param[in] key key of the element to access
3401 
3402  @return const reference to the element at key @a key
3403 
3404  @throw std::domain_error if JSON is not an object; example: `"cannot use
3405  operator[] with null"`
3406 
3407  @complexity Logarithmic in the size of the container.
3408 
3409  @liveexample{The example below shows how object elements can be read using
3410  the `[]` operator.,operatorarray__key_type_const}
3411 
3412  @sa @ref at(const typename object_t::key_type&) for access by reference
3413  with range checking
3414  @sa @ref value() for access by value with a default value
3415 
3416  @since version 1.0.0
3417  */
3418  template<typename T, std::size_t n>
3419  const_reference operator[](T * (&key)[n]) const
3420  {
3421  return operator[](static_cast<const T>(key));
3422  }
3423 
3424  /*!
3425  @brief access specified object element
3426 
3427  Returns a reference to the element at with specified key @a key.
3428 
3429  @note If @a key is not found in the object, then it is silently added to
3430  the object and filled with a `null` value to make `key` a valid reference.
3431  In case the value was `null` before, it is converted to an object.
3432 
3433  @param[in] key key of the element to access
3434 
3435  @return reference to the element at key @a key
3436 
3437  @throw std::domain_error if JSON is not an object or null; example:
3438  `"cannot use operator[] with string"`
3439 
3440  @complexity Logarithmic in the size of the container.
3441 
3442  @liveexample{The example below shows how object elements can be read and
3443  written using the `[]` operator.,operatorarray__key_type}
3444 
3445  @sa @ref at(const typename object_t::key_type&) for access by reference
3446  with range checking
3447  @sa @ref value() for access by value with a default value
3448 
3449  @since version 1.1.0
3450  */
3451  template<typename T>
3453  {
3454  // implicitly convert null to object
3455  if (is_null())
3456  {
3459  }
3460 
3461  // at only works for objects
3462  if (is_object())
3463  {
3464  assert(m_value.object != nullptr);
3465  return m_value.object->operator[](key);
3466  }
3467  else
3468  {
3469  throw std::domain_error("cannot use operator[] with " + type_name());
3470  }
3471  }
3472 
3473  /*!
3474  @brief read-only access specified object element
3475 
3476  Returns a const reference to the element at with specified key @a key. No
3477  bounds checking is performed.
3478 
3479  @warning If the element with key @a key does not exist, the behavior is
3480  undefined.
3481 
3482  @param[in] key key of the element to access
3483 
3484  @return const reference to the element at key @a key
3485 
3486  @throw std::domain_error if JSON is not an object; example: `"cannot use
3487  operator[] with null"`
3488 
3489  @complexity Logarithmic in the size of the container.
3490 
3491  @liveexample{The example below shows how object elements can be read using
3492  the `[]` operator.,operatorarray__key_type_const}
3493 
3494  @sa @ref at(const typename object_t::key_type&) for access by reference
3495  with range checking
3496  @sa @ref value() for access by value with a default value
3497 
3498  @since version 1.1.0
3499  */
3500  template<typename T>
3502  {
3503  // at only works for objects
3504  if (is_object())
3505  {
3506  assert(m_value.object != nullptr);
3507  assert(m_value.object->find(key) != m_value.object->end());
3508  return m_value.object->find(key)->second;
3509  }
3510  else
3511  {
3512  throw std::domain_error("cannot use operator[] with " + type_name());
3513  }
3514  }
3515 
3516  /*!
3517  @brief access specified object element with default value
3518 
3519  Returns either a copy of an object's element at the specified key @a key or
3520  a given default value if no element with key @a key exists.
3521 
3522  The function is basically equivalent to executing
3523  @code {.cpp}
3524  try {
3525  return at(key);
3526  } catch(std::out_of_range) {
3527  return default_value;
3528  }
3529  @endcode
3530 
3531  @note Unlike @ref at(const typename object_t::key_type&), this function
3532  does not throw if the given key @a key was not found.
3533 
3534  @note Unlike @ref operator[](const typename object_t::key_type& key), this
3535  function does not implicitly add an element to the position defined by @a
3536  key. This function is furthermore also applicable to const objects.
3537 
3538  @param[in] key key of the element to access
3539  @param[in] default_value the value to return if @a key is not found
3540 
3541  @tparam ValueType type compatible to JSON values, for instance `int` for
3542  JSON integer numbers, `bool` for JSON booleans, or `std::vector` types for
3543  JSON arrays. Note the type of the expected value at @a key and the default
3544  value @a default_value must be compatible.
3545 
3546  @return copy of the element at key @a key or @a default_value if @a key
3547  is not found
3548 
3549  @throw std::domain_error if JSON is not an object; example: `"cannot use
3550  value() with null"`
3551 
3552  @complexity Logarithmic in the size of the container.
3553 
3554  @liveexample{The example below shows how object elements can be queried
3555  with a default value.,basic_json__value}
3556 
3557  @sa @ref at(const typename object_t::key_type&) for access by reference
3558  with range checking
3559  @sa @ref operator[](const typename object_t::key_type&) for unchecked
3560  access by reference
3561 
3562  @since version 1.0.0
3563  */
3564  template <class ValueType, typename
3565  std::enable_if<
3567  , int>::type = 0>
3568  ValueType value(const typename object_t::key_type& key, ValueType default_value) const
3569  {
3570  // at only works for objects
3571  if (is_object())
3572  {
3573  // if key is found, return value and given default value otherwise
3574  const auto it = find(key);
3575  if (it != end())
3576  {
3577  return *it;
3578  }
3579  else
3580  {
3581  return default_value;
3582  }
3583  }
3584  else
3585  {
3586  throw std::domain_error("cannot use value() with " + type_name());
3587  }
3588  }
3589 
3590  /*!
3591  @brief overload for a default value of type const char*
3592  @copydoc basic_json::value()
3593  */
3594  string_t value(const typename object_t::key_type& key, const char* default_value) const
3595  {
3596  return value(key, string_t(default_value));
3597  }
3598 
3599  /*!
3600  @brief access the first element
3601 
3602  Returns a reference to the first element in the container. For a JSON
3603  container `c`, the expression `c.front()` is equivalent to `*c.begin()`.
3604 
3605  @return In case of a structured type (array or object), a reference to the
3606  first element is returned. In cast of number, string, or boolean values, a
3607  reference to the value is returned.
3608 
3609  @complexity Constant.
3610 
3611  @pre The JSON value must not be `null` (would throw `std::out_of_range`) or
3612  an empty array or object (undefined behavior, guarded by assertions).
3613  @post The JSON value remains unchanged.
3614 
3615  @throw std::out_of_range when called on `null` value
3616 
3617  @liveexample{The following code shows an example for `front()`.,front}
3618 
3619  @sa @ref back() -- access the last element
3620 
3621  @since version 1.0.0
3622  */
3624  {
3625  return *begin();
3626  }
3627 
3628  /*!
3629  @copydoc basic_json::front()
3630  */
3632  {
3633  return *cbegin();
3634  }
3635 
3636  /*!
3637  @brief access the last element
3638 
3639  Returns a reference to the last element in the container. For a JSON
3640  container `c`, the expression `c.back()` is equivalent to
3641  @code {.cpp}
3642  auto tmp = c.end();
3643  --tmp;
3644  return *tmp;
3645  @endcode
3646 
3647  @return In case of a structured type (array or object), a reference to the
3648  last element is returned. In cast of number, string, or boolean values, a
3649  reference to the value is returned.
3650 
3651  @complexity Constant.
3652 
3653  @pre The JSON value must not be `null` (would throw `std::out_of_range`) or
3654  an empty array or object (undefined behavior, guarded by assertions).
3655  @post The JSON value remains unchanged.
3656 
3657  @throw std::out_of_range when called on `null` value.
3658 
3659  @liveexample{The following code shows an example for `back()`.,back}
3660 
3661  @sa @ref front() -- access the first element
3662 
3663  @since version 1.0.0
3664  */
3666  {
3667  auto tmp = end();
3668  --tmp;
3669  return *tmp;
3670  }
3671 
3672  /*!
3673  @copydoc basic_json::back()
3674  */
3676  {
3677  auto tmp = cend();
3678  --tmp;
3679  return *tmp;
3680  }
3681 
3682  /*!
3683  @brief remove element given an iterator
3684 
3685  Removes the element specified by iterator @a pos. The iterator @a pos must
3686  be valid and dereferenceable. Thus the `end()` iterator (which is valid,
3687  but is not dereferenceable) cannot be used as a value for @a pos.
3688 
3689  If called on a primitive type other than `null`, the resulting JSON value
3690  will be `null`.
3691 
3692  @param[in] pos iterator to the element to remove
3693  @return Iterator following the last removed element. If the iterator @a pos
3694  refers to the last element, the `end()` iterator is returned.
3695 
3696  @tparam InteratorType an @ref iterator or @ref const_iterator
3697 
3698  @post Invalidates iterators and references at or after the point of the
3699  erase, including the `end()` iterator.
3700 
3701  @throw std::domain_error if called on a `null` value; example: `"cannot use
3702  erase() with null"`
3703  @throw std::domain_error if called on an iterator which does not belong to
3704  the current JSON value; example: `"iterator does not fit current value"`
3705  @throw std::out_of_range if called on a primitive type with invalid
3706  iterator (i.e., any iterator which is not `begin()`); example: `"iterator
3707  out of range"`
3708 
3709  @complexity The complexity depends on the type:
3710  - objects: amortized constant
3711  - arrays: linear in distance between pos and the end of the container
3712  - strings: linear in the length of the string
3713  - other types: constant
3714 
3715  @liveexample{The example shows the result of `erase()` for different JSON
3716  types.,erase__IteratorType}
3717 
3718  @sa @ref erase(InteratorType, InteratorType) -- removes the elements in the
3719  given range
3720  @sa @ref erase(const typename object_t::key_type&) -- removes the element
3721  from an object at the given key
3722  @sa @ref erase(const size_type) -- removes the element from an array at the
3723  given index
3724 
3725  @since version 1.0.0
3726  */
3727  template <class InteratorType, typename
3728  std::enable_if<
3731  , int>::type
3732  = 0>
3733  InteratorType erase(InteratorType pos)
3734  {
3735  // make sure iterator fits the current value
3736  if (this != pos.m_object)
3737  {
3738  throw std::domain_error("iterator does not fit current value");
3739  }
3740 
3741  InteratorType result = end();
3742 
3743  switch (m_type)
3744  {
3745  case value_t::boolean:
3746  case value_t::number_float:
3749  case value_t::string:
3750  {
3751  if (not pos.m_it.primitive_iterator.is_begin())
3752  {
3753  throw std::out_of_range("iterator out of range");
3754  }
3755 
3756  if (is_string())
3757  {
3758  delete m_value.string;
3759  m_value.string = nullptr;
3760  }
3761 
3763  break;
3764  }
3765 
3766  case value_t::object:
3767  {
3768  assert(m_value.object != nullptr);
3769  result.m_it.object_iterator = m_value.object->erase(pos.m_it.object_iterator);
3770  break;
3771  }
3772 
3773  case value_t::array:
3774  {
3775  assert(m_value.array != nullptr);
3776  result.m_it.array_iterator = m_value.array->erase(pos.m_it.array_iterator);
3777  break;
3778  }
3779 
3780  default:
3781  {
3782  throw std::domain_error("cannot use erase() with " + type_name());
3783  }
3784  }
3785 
3786  return result;
3787  }
3788 
3789  /*!
3790  @brief remove elements given an iterator range
3791 
3792  Removes the element specified by the range `[first; last)`. The iterator @a
3793  first does not need to be dereferenceable if `first == last`: erasing an
3794  empty range is a no-op.
3795 
3796  If called on a primitive type other than `null`, the resulting JSON value
3797  will be `null`.
3798 
3799  @param[in] first iterator to the beginning of the range to remove
3800  @param[in] last iterator past the end of the range to remove
3801  @return Iterator following the last removed element. If the iterator @a
3802  second refers to the last element, the `end()` iterator is returned.
3803 
3804  @tparam InteratorType an @ref iterator or @ref const_iterator
3805 
3806  @post Invalidates iterators and references at or after the point of the
3807  erase, including the `end()` iterator.
3808 
3809  @throw std::domain_error if called on a `null` value; example: `"cannot use
3810  erase() with null"`
3811  @throw std::domain_error if called on iterators which does not belong to
3812  the current JSON value; example: `"iterators do not fit current value"`
3813  @throw std::out_of_range if called on a primitive type with invalid
3814  iterators (i.e., if `first != begin()` and `last != end()`); example:
3815  `"iterators out of range"`
3816 
3817  @complexity The complexity depends on the type:
3818  - objects: `log(size()) + std::distance(first, last)`
3819  - arrays: linear in the distance between @a first and @a last, plus linear
3820  in the distance between @a last and end of the container
3821  - strings: linear in the length of the string
3822  - other types: constant
3823 
3824  @liveexample{The example shows the result of `erase()` for different JSON
3825  types.,erase__IteratorType_IteratorType}
3826 
3827  @sa @ref erase(InteratorType) -- removes the element at a given position
3828  @sa @ref erase(const typename object_t::key_type&) -- removes the element
3829  from an object at the given key
3830  @sa @ref erase(const size_type) -- removes the element from an array at the
3831  given index
3832 
3833  @since version 1.0.0
3834  */
3835  template <class InteratorType, typename
3836  std::enable_if<
3839  , int>::type
3840  = 0>
3841  InteratorType erase(InteratorType first, InteratorType last)
3842  {
3843  // make sure iterator fits the current value
3844  if (this != first.m_object or this != last.m_object)
3845  {
3846  throw std::domain_error("iterators do not fit current value");
3847  }
3848 
3849  InteratorType result = end();
3850 
3851  switch (m_type)
3852  {
3853  case value_t::boolean:
3854  case value_t::number_float:
3857  case value_t::string:
3858  {
3859  if (not first.m_it.primitive_iterator.is_begin() or not last.m_it.primitive_iterator.is_end())
3860  {
3861  throw std::out_of_range("iterators out of range");
3862  }
3863 
3864  if (is_string())
3865  {
3866  delete m_value.string;
3867  m_value.string = nullptr;
3868  }
3869 
3871  break;
3872  }
3873 
3874  case value_t::object:
3875  {
3876  assert(m_value.object != nullptr);
3877  result.m_it.object_iterator = m_value.object->erase(first.m_it.object_iterator,
3878  last.m_it.object_iterator);
3879  break;
3880  }
3881 
3882  case value_t::array:
3883  {
3884  assert(m_value.array != nullptr);
3885  result.m_it.array_iterator = m_value.array->erase(first.m_it.array_iterator,
3886  last.m_it.array_iterator);
3887  break;
3888  }
3889 
3890  default:
3891  {
3892  throw std::domain_error("cannot use erase() with " + type_name());
3893  }
3894  }
3895 
3896  return result;
3897  }
3898 
3899  /*!
3900  @brief remove element from a JSON object given a key
3901 
3902  Removes elements from a JSON object with the key value @a key.
3903 
3904  @param[in] key value of the elements to remove
3905 
3906  @return Number of elements removed. If @a ObjectType is the default
3907  `std::map` type, the return value will always be `0` (@a key was not found)
3908  or `1` (@a key was found).
3909 
3910  @post References and iterators to the erased elements are invalidated.
3911  Other references and iterators are not affected.
3912 
3913  @throw std::domain_error when called on a type other than JSON object;
3914  example: `"cannot use erase() with null"`
3915 
3916  @complexity `log(size()) + count(key)`
3917 
3918  @liveexample{The example shows the effect of `erase()`.,erase__key_type}
3919 
3920  @sa @ref erase(InteratorType) -- removes the element at a given position
3921  @sa @ref erase(InteratorType, InteratorType) -- removes the elements in the
3922  given range
3923  @sa @ref erase(const size_type) -- removes the element from an array at the
3924  given index
3925 
3926  @since version 1.0.0
3927  */
3928  size_type erase(const typename object_t::key_type& key)
3929  {
3930  // this erase only works for objects
3931  if (is_object())
3932  {
3933  assert(m_value.object != nullptr);
3934  return m_value.object->erase(key);
3935  }
3936  else
3937  {
3938  throw std::domain_error("cannot use erase() with " + type_name());
3939  }
3940  }
3941 
3942  /*!
3943  @brief remove element from a JSON array given an index
3944 
3945  Removes element from a JSON array at the index @a idx.
3946 
3947  @param[in] idx index of the element to remove
3948 
3949  @throw std::domain_error when called on a type other than JSON array;
3950  example: `"cannot use erase() with null"`
3951  @throw std::out_of_range when `idx >= size()`; example: `"index out of
3952  range"`
3953 
3954  @complexity Linear in distance between @a idx and the end of the container.
3955 
3956  @liveexample{The example shows the effect of `erase()`.,erase__size_type}
3957 
3958  @sa @ref erase(InteratorType) -- removes the element at a given position
3959  @sa @ref erase(InteratorType, InteratorType) -- removes the elements in the
3960  given range
3961  @sa @ref erase(const typename object_t::key_type&) -- removes the element
3962  from an object at the given key
3963 
3964  @since version 1.0.0
3965  */
3966  void erase(const size_type idx)
3967  {
3968  // this erase only works for arrays
3969  if (is_array())
3970  {
3971  if (idx >= size())
3972  {
3973  throw std::out_of_range("index out of range");
3974  }
3975 
3976  assert(m_value.array != nullptr);
3977  m_value.array->erase(m_value.array->begin() + static_cast<difference_type>(idx));
3978  }
3979  else
3980  {
3981  throw std::domain_error("cannot use erase() with " + type_name());
3982  }
3983  }
3984 
3985  /// @}
3986 
3987 
3988  ////////////
3989  // lookup //
3990  ////////////
3991 
3992  /// @name lookup
3993  /// @{
3994 
3995  /*!
3996  @brief find an element in a JSON object
3997 
3998  Finds an element in a JSON object with key equivalent to @a key. If the
3999  element is not found or the JSON value is not an object, end() is returned.
4000 
4001  @param[in] key key value of the element to search for
4002 
4003  @return Iterator to an element with key equivalent to @a key. If no such
4004  element is found, past-the-end (see end()) iterator is returned.
4005 
4006  @complexity Logarithmic in the size of the JSON object.
4007 
4008  @liveexample{The example shows how `find()` is used.,find__key_type}
4009 
4010  @since version 1.0.0
4011  */
4012  iterator find(typename object_t::key_type key)
4013  {
4014  auto result = end();
4015 
4016  if (is_object())
4017  {
4018  assert(m_value.object != nullptr);
4019  result.m_it.object_iterator = m_value.object->find(key);
4020  }
4021 
4022  return result;
4023  }
4024 
4025  /*!
4026  @brief find an element in a JSON object
4027  @copydoc find(typename object_t::key_type)
4028  */
4029  const_iterator find(typename object_t::key_type key) const
4030  {
4031  auto result = cend();
4032 
4033  if (is_object())
4034  {
4035  assert(m_value.object != nullptr);
4036  result.m_it.object_iterator = m_value.object->find(key);
4037  }
4038 
4039  return result;
4040  }
4041 
4042  /*!
4043  @brief returns the number of occurrences of a key in a JSON object
4044 
4045  Returns the number of elements with key @a key. If ObjectType is the
4046  default `std::map` type, the return value will always be `0` (@a key was
4047  not found) or `1` (@a key was found).
4048 
4049  @param[in] key key value of the element to count
4050 
4051  @return Number of elements with key @a key. If the JSON value is not an
4052  object, the return value will be `0`.
4053 
4054  @complexity Logarithmic in the size of the JSON object.
4055 
4056  @liveexample{The example shows how `count()` is used.,count}
4057 
4058  @since version 1.0.0
4059  */
4060  size_type count(typename object_t::key_type key) const
4061  {
4062  // return 0 for all nonobject types
4063  assert(not is_object() or m_value.object != nullptr);
4064  return is_object() ? m_value.object->count(key) : 0;
4065  }
4066 
4067  /// @}
4068 
4069 
4070  ///////////////
4071  // iterators //
4072  ///////////////
4073 
4074  /// @name iterators
4075  /// @{
4076 
4077  /*!
4078  @brief returns an iterator to the first element
4079 
4080  Returns an iterator to the first element.
4081 
4082  @image html range-begin-end.svg "Illustration from cppreference.com"
4083 
4084  @return iterator to the first element
4085 
4086  @complexity Constant.
4087 
4088  @requirement This function helps `basic_json` satisfying the
4089  [Container](http://en.cppreference.com/w/cpp/concept/Container)
4090  requirements:
4091  - The complexity is constant.
4092 
4093  @liveexample{The following code shows an example for `begin()`.,begin}
4094 
4095  @sa @ref cbegin() -- returns a const iterator to the beginning
4096  @sa @ref end() -- returns an iterator to the end
4097  @sa @ref cend() -- returns a const iterator to the end
4098 
4099  @since version 1.0.0
4100  */
4101  iterator begin() noexcept
4102  {
4103  iterator result(this);
4104  result.set_begin();
4105  return result;
4106  }
4107 
4108  /*!
4109  @copydoc basic_json::cbegin()
4110  */
4111  const_iterator begin() const noexcept
4112  {
4113  return cbegin();
4114  }
4115 
4116  /*!
4117  @brief returns a const iterator to the first element
4118 
4119  Returns a const iterator to the first element.
4120 
4121  @image html range-begin-end.svg "Illustration from cppreference.com"
4122 
4123  @return const iterator to the first element
4124 
4125  @complexity Constant.
4126 
4127  @requirement This function helps `basic_json` satisfying the
4128  [Container](http://en.cppreference.com/w/cpp/concept/Container)
4129  requirements:
4130  - The complexity is constant.
4131  - Has the semantics of `const_cast<const basic_json&>(*this).begin()`.
4132 
4133  @liveexample{The following code shows an example for `cbegin()`.,cbegin}
4134 
4135  @sa @ref begin() -- returns an iterator to the beginning
4136  @sa @ref end() -- returns an iterator to the end
4137  @sa @ref cend() -- returns a const iterator to the end
4138 
4139  @since version 1.0.0
4140  */
4141  const_iterator cbegin() const noexcept
4142  {
4143  const_iterator result(this);
4144  result.set_begin();
4145  return result;
4146  }
4147 
4148  /*!
4149  @brief returns an iterator to one past the last element
4150 
4151  Returns an iterator to one past the last element.
4152 
4153  @image html range-begin-end.svg "Illustration from cppreference.com"
4154 
4155  @return iterator one past the last element
4156 
4157  @complexity Constant.
4158 
4159  @requirement This function helps `basic_json` satisfying the
4160  [Container](http://en.cppreference.com/w/cpp/concept/Container)
4161  requirements:
4162  - The complexity is constant.
4163 
4164  @liveexample{The following code shows an example for `end()`.,end}
4165 
4166  @sa @ref cend() -- returns a const iterator to the end
4167  @sa @ref begin() -- returns an iterator to the beginning
4168  @sa @ref cbegin() -- returns a const iterator to the beginning
4169 
4170  @since version 1.0.0
4171  */
4172  iterator end() noexcept
4173  {
4174  iterator result(this);
4175  result.set_end();
4176  return result;
4177  }
4178 
4179  /*!
4180  @copydoc basic_json::cend()
4181  */
4182  const_iterator end() const noexcept
4183  {
4184  return cend();
4185  }
4186 
4187  /*!
4188  @brief returns a const iterator to one past the last element
4189 
4190  Returns a const iterator to one past the last element.
4191 
4192  @image html range-begin-end.svg "Illustration from cppreference.com"
4193 
4194  @return const iterator one past the last element
4195 
4196  @complexity Constant.
4197 
4198  @requirement This function helps `basic_json` satisfying the
4199  [Container](http://en.cppreference.com/w/cpp/concept/Container)
4200  requirements:
4201  - The complexity is constant.
4202  - Has the semantics of `const_cast<const basic_json&>(*this).end()`.
4203 
4204  @liveexample{The following code shows an example for `cend()`.,cend}
4205 
4206  @sa @ref end() -- returns an iterator to the end
4207  @sa @ref begin() -- returns an iterator to the beginning
4208  @sa @ref cbegin() -- returns a const iterator to the beginning
4209 
4210  @since version 1.0.0
4211  */
4212  const_iterator cend() const noexcept
4213  {
4214  const_iterator result(this);
4215  result.set_end();
4216  return result;
4217  }
4218 
4219  /*!
4220  @brief returns an iterator to the reverse-beginning
4221 
4222  Returns an iterator to the reverse-beginning; that is, the last element.
4223 
4224  @image html range-rbegin-rend.svg "Illustration from cppreference.com"
4225 
4226  @complexity Constant.
4227 
4228  @requirement This function helps `basic_json` satisfying the
4229  [ReversibleContainer](http://en.cppreference.com/w/cpp/concept/ReversibleContainer)
4230  requirements:
4231  - The complexity is constant.
4232  - Has the semantics of `reverse_iterator(end())`.
4233 
4234  @liveexample{The following code shows an example for `rbegin()`.,rbegin}
4235 
4236  @sa @ref crbegin() -- returns a const reverse iterator to the beginning
4237  @sa @ref rend() -- returns a reverse iterator to the end
4238  @sa @ref crend() -- returns a const reverse iterator to the end
4239 
4240  @since version 1.0.0
4241  */
4243  {
4244  return reverse_iterator(end());
4245  }
4246 
4247  /*!
4248  @copydoc basic_json::crbegin()
4249  */
4251  {
4252  return crbegin();
4253  }
4254 
4255  /*!
4256  @brief returns an iterator to the reverse-end
4257 
4258  Returns an iterator to the reverse-end; that is, one before the first
4259  element.
4260 
4261  @image html range-rbegin-rend.svg "Illustration from cppreference.com"
4262 
4263  @complexity Constant.
4264 
4265  @requirement This function helps `basic_json` satisfying the
4266  [ReversibleContainer](http://en.cppreference.com/w/cpp/concept/ReversibleContainer)
4267  requirements:
4268  - The complexity is constant.
4269  - Has the semantics of `reverse_iterator(begin())`.
4270 
4271  @liveexample{The following code shows an example for `rend()`.,rend}
4272 
4273  @sa @ref crend() -- returns a const reverse iterator to the end
4274  @sa @ref rbegin() -- returns a reverse iterator to the beginning
4275  @sa @ref crbegin() -- returns a const reverse iterator to the beginning
4276 
4277  @since version 1.0.0
4278  */
4280  {
4281  return reverse_iterator(begin());
4282  }
4283 
4284  /*!
4285  @copydoc basic_json::crend()
4286  */
4287  const_reverse_iterator rend() const noexcept
4288  {
4289  return crend();
4290  }
4291 
4292  /*!
4293  @brief returns a const reverse iterator to the last element
4294 
4295  Returns a const iterator to the reverse-beginning; that is, the last
4296  element.
4297 
4298  @image html range-rbegin-rend.svg "Illustration from cppreference.com"
4299 
4300  @complexity Constant.
4301 
4302  @requirement This function helps `basic_json` satisfying the
4303  [ReversibleContainer](http://en.cppreference.com/w/cpp/concept/ReversibleContainer)
4304  requirements:
4305  - The complexity is constant.
4306  - Has the semantics of `const_cast<const basic_json&>(*this).rbegin()`.
4307 
4308  @liveexample{The following code shows an example for `crbegin()`.,crbegin}
4309 
4310  @sa @ref rbegin() -- returns a reverse iterator to the beginning
4311  @sa @ref rend() -- returns a reverse iterator to the end
4312  @sa @ref crend() -- returns a const reverse iterator to the end
4313 
4314  @since version 1.0.0
4315  */
4317  {
4318  return const_reverse_iterator(cend());
4319  }
4320 
4321  /*!
4322  @brief returns a const reverse iterator to one before the first
4323 
4324  Returns a const reverse iterator to the reverse-end; that is, one before
4325  the first element.
4326 
4327  @image html range-rbegin-rend.svg "Illustration from cppreference.com"
4328 
4329  @complexity Constant.
4330 
4331  @requirement This function helps `basic_json` satisfying the
4332  [ReversibleContainer](http://en.cppreference.com/w/cpp/concept/ReversibleContainer)
4333  requirements:
4334  - The complexity is constant.
4335  - Has the semantics of `const_cast<const basic_json&>(*this).rend()`.
4336 
4337  @liveexample{The following code shows an example for `crend()`.,crend}
4338 
4339  @sa @ref rend() -- returns a reverse iterator to the end
4340  @sa @ref rbegin() -- returns a reverse iterator to the beginning
4341  @sa @ref crbegin() -- returns a const reverse iterator to the beginning
4342 
4343  @since version 1.0.0
4344  */
4346  {
4347  return const_reverse_iterator(cbegin());
4348  }
4349 
4350  private:
4351  // forward declaration
4352  template<typename IteratorType> class iteration_proxy;
4353 
4354  public:
4355  /*!
4356  @brief wrapper to access iterator member functions in range-based for
4357 
4358  This function allows to access @ref iterator::key() and @ref
4359  iterator::value() during range-based for loops. In these loops, a reference
4360  to the JSON values is returned, so there is no access to the underlying
4361  iterator.
4362 
4363  @note The name of this function is not yet final and may change in the
4364  future.
4365  */
4367  {
4368  return iteration_proxy<iterator>(cont);
4369  }
4370 
4371  /*!
4372  @copydoc iterator_wrapper(reference)
4373  */
4375  {
4376  return iteration_proxy<const_iterator>(cont);
4377  }
4378 
4379  /// @}
4380 
4381 
4382  //////////////
4383  // capacity //
4384  //////////////
4385 
4386  /// @name capacity
4387  /// @{
4388 
4389  /*!
4390  @brief checks whether the container is empty
4391 
4392  Checks if a JSON value has no elements.
4393 
4394  @return The return value depends on the different types and is
4395  defined as follows:
4396  Value type | return value
4397  ----------- | -------------
4398  null | `true`
4399  boolean | `false`
4400  string | `false`
4401  number | `false`
4402  object | result of function `object_t::empty()`
4403  array | result of function `array_t::empty()`
4404 
4405  @complexity Constant, as long as @ref array_t and @ref object_t satisfy the
4406  Container concept; that is, their `empty()` functions have constant
4407  complexity.
4408 
4409  @requirement This function helps `basic_json` satisfying the
4410  [Container](http://en.cppreference.com/w/cpp/concept/Container)
4411  requirements:
4412  - The complexity is constant.
4413  - Has the semantics of `begin() == end()`.
4414 
4415  @liveexample{The following code uses `empty()` to check if a JSON
4416  object contains any elements.,empty}
4417 
4418  @sa @ref size() -- returns the number of elements
4419 
4420  @since version 1.0.0
4421  */
4422  bool empty() const noexcept
4423  {
4424  switch (m_type)
4425  {
4426  case value_t::null:
4427  {
4428  // null values are empty
4429  return true;
4430  }
4431 
4432  case value_t::array:
4433  {
4434  assert(m_value.array != nullptr);
4435  return m_value.array->empty();
4436  }
4437 
4438  case value_t::object:
4439  {
4440  assert(m_value.object != nullptr);
4441  return m_value.object->empty();
4442  }
4443 
4444  default:
4445  {
4446  // all other types are nonempty
4447  return false;
4448  }
4449  }
4450  }
4451 
4452  /*!
4453  @brief returns the number of elements
4454 
4455  Returns the number of elements in a JSON value.
4456 
4457  @return The return value depends on the different types and is
4458  defined as follows:
4459  Value type | return value
4460  ----------- | -------------
4461  null | `0`
4462  boolean | `1`
4463  string | `1`
4464  number | `1`
4465  object | result of function object_t::size()
4466  array | result of function array_t::size()
4467 
4468  @complexity Constant, as long as @ref array_t and @ref object_t satisfy the
4469  Container concept; that is, their size() functions have constant complexity.
4470 
4471  @requirement This function helps `basic_json` satisfying the
4472  [Container](http://en.cppreference.com/w/cpp/concept/Container)
4473  requirements:
4474  - The complexity is constant.
4475  - Has the semantics of `std::distance(begin(), end())`.
4476 
4477  @liveexample{The following code calls `size()` on the different value
4478  types.,size}
4479 
4480  @sa @ref empty() -- checks whether the container is empty
4481  @sa @ref max_size() -- returns the maximal number of elements
4482 
4483  @since version 1.0.0
4484  */
4485  size_type size() const noexcept
4486  {
4487  switch (m_type)
4488  {
4489  case value_t::null:
4490  {
4491  // null values are empty
4492  return 0;
4493  }
4494 
4495  case value_t::array:
4496  {
4497  assert(m_value.array != nullptr);
4498  return m_value.array->size();
4499  }
4500 
4501  case value_t::object:
4502  {
4503  assert(m_value.object != nullptr);
4504  return m_value.object->size();
4505  }
4506 
4507  default:
4508  {
4509  // all other types have size 1
4510  return 1;
4511  }
4512  }
4513  }
4514 
4515  /*!
4516  @brief returns the maximum possible number of elements
4517 
4518  Returns the maximum number of elements a JSON value is able to hold due to
4519  system or library implementation limitations, i.e. `std::distance(begin(),
4520  end())` for the JSON value.
4521 
4522  @return The return value depends on the different types and is
4523  defined as follows:
4524  Value type | return value
4525  ----------- | -------------
4526  null | `0` (same as `size()`)
4527  boolean | `1` (same as `size()`)
4528  string | `1` (same as `size()`)
4529  number | `1` (same as `size()`)
4530  object | result of function `object_t::max_size()`
4531  array | result of function `array_t::max_size()`
4532 
4533  @complexity Constant, as long as @ref array_t and @ref object_t satisfy the
4534  Container concept; that is, their `max_size()` functions have constant
4535  complexity.
4536 
4537  @requirement This function helps `basic_json` satisfying the
4538  [Container](http://en.cppreference.com/w/cpp/concept/Container)
4539  requirements:
4540  - The complexity is constant.
4541  - Has the semantics of returning `b.size()` where `b` is the largest
4542  possible JSON value.
4543 
4544  @liveexample{The following code calls `max_size()` on the different value
4545  types. Note the output is implementation specific.,max_size}
4546 
4547  @sa @ref size() -- returns the number of elements
4548 
4549  @since version 1.0.0
4550  */
4551  size_type max_size() const noexcept
4552  {
4553  switch (m_type)
4554  {
4555  case value_t::array:
4556  {
4557  assert(m_value.array != nullptr);
4558  return m_value.array->max_size();
4559  }
4560 
4561  case value_t::object:
4562  {
4563  assert(m_value.object != nullptr);
4564  return m_value.object->max_size();
4565  }
4566 
4567  default:
4568  {
4569  // all other types have max_size() == size()
4570  return size();
4571  }
4572  }
4573  }
4574 
4575  /// @}
4576 
4577 
4578  ///////////////
4579  // modifiers //
4580  ///////////////
4581 
4582  /// @name modifiers
4583  /// @{
4584 
4585  /*!
4586  @brief clears the contents
4587 
4588  Clears the content of a JSON value and resets it to the default value as
4589  if @ref basic_json(value_t) would have been called:
4590 
4591  Value type | initial value
4592  ----------- | -------------
4593  null | `null`
4594  boolean | `false`
4595  string | `""`
4596  number | `0`
4597  object | `{}`
4598  array | `[]`
4599 
4600  @note Floating-point numbers are set to `0.0` which will be serialized to
4601  `0`. The vale type remains @ref number_float_t.
4602 
4603  @complexity Linear in the size of the JSON value.
4604 
4605  @liveexample{The example below shows the effect of `clear()` to different
4606  JSON types.,clear}
4607 
4608  @since version 1.0.0
4609  */
4610  void clear() noexcept
4611  {
4612  switch (m_type)
4613  {
4615  {
4616  m_value.number_integer = 0;
4617  break;
4618  }
4619 
4621  {
4623  break;
4624  }
4625 
4626  case value_t::number_float:
4627  {
4628  m_value.number_float = 0.0;
4629  break;
4630  }
4631 
4632  case value_t::boolean:
4633  {
4634  m_value.boolean = false;
4635  break;
4636  }
4637 
4638  case value_t::string:
4639  {
4640  assert(m_value.string != nullptr);
4641  m_value.string->clear();
4642  break;
4643  }
4644 
4645  case value_t::array:
4646  {
4647  assert(m_value.array != nullptr);
4648  m_value.array->clear();
4649  break;
4650  }
4651 
4652  case value_t::object:
4653  {
4654  assert(m_value.object != nullptr);
4655  m_value.object->clear();
4656  break;
4657  }
4658 
4659  default:
4660  {
4661  break;
4662  }
4663  }
4664  }
4665 
4666  /*!
4667  @brief add an object to an array
4668 
4669  Appends the given element @a val to the end of the JSON value. If the
4670  function is called on a JSON null value, an empty array is created before
4671  appending @a val.
4672 
4673  @param[in] val the value to add to the JSON array
4674 
4675  @throw std::domain_error when called on a type other than JSON array or
4676  null; example: `"cannot use push_back() with number"`
4677 
4678  @complexity Amortized constant.
4679 
4680  @liveexample{The example shows how `push_back()` and `+=` can be used to
4681  add elements to a JSON array. Note how the `null` value was silently
4682  converted to a JSON array.,push_back}
4683 
4684  @since version 1.0.0
4685  */
4686  void push_back(basic_json&& val)
4687  {
4688  // push_back only works for null objects or arrays
4689  if (not(is_null() or is_array()))
4690  {
4691  throw std::domain_error("cannot use push_back() with " + type_name());
4692  }
4693 
4694  // transform null object into an array
4695  if (is_null())
4696  {
4699  }
4700 
4701  // add element to array (move semantics)
4702  assert(m_value.array != nullptr);
4703  m_value.array->push_back(std::move(val));
4704  // invalidate object
4705  val.m_type = value_t::null;
4706  }
4707 
4708  /*!
4709  @brief add an object to an array
4710  @copydoc push_back(basic_json&&)
4711  */
4713  {
4714  push_back(std::move(val));
4715  return *this;
4716  }
4717 
4718  /*!
4719  @brief add an object to an array
4720  @copydoc push_back(basic_json&&)
4721  */
4722  void push_back(const basic_json& val)
4723  {
4724  // push_back only works for null objects or arrays
4725  if (not(is_null() or is_array()))
4726  {
4727  throw std::domain_error("cannot use push_back() with " + type_name());
4728  }
4729 
4730  // transform null object into an array
4731  if (is_null())
4732  {
4735  }
4736 
4737  // add element to array
4738  assert(m_value.array != nullptr);
4739  m_value.array->push_back(val);
4740  }
4741 
4742  /*!
4743  @brief add an object to an array
4744  @copydoc push_back(basic_json&&)
4745  */
4747  {
4748  push_back(val);
4749  return *this;
4750  }
4751 
4752  /*!
4753  @brief add an object to an object
4754 
4755  Inserts the given element @a val to the JSON object. If the function is
4756  called on a JSON null value, an empty object is created before inserting @a
4757  val.
4758 
4759  @param[in] val the value to add to the JSON object
4760 
4761  @throw std::domain_error when called on a type other than JSON object or
4762  null; example: `"cannot use push_back() with number"`
4763 
4764  @complexity Logarithmic in the size of the container, O(log(`size()`)).
4765 
4766  @liveexample{The example shows how `push_back()` and `+=` can be used to
4767  add elements to a JSON object. Note how the `null` value was silently
4768  converted to a JSON object.,push_back__object_t__value}
4769 
4770  @since version 1.0.0
4771  */
4772  void push_back(const typename object_t::value_type& val)
4773  {
4774  // push_back only works for null objects or objects
4775  if (not(is_null() or is_object()))
4776  {
4777  throw std::domain_error("cannot use push_back() with " + type_name());
4778  }
4779 
4780  // transform null object into an object
4781  if (is_null())
4782  {
4785  }
4786 
4787  // add element to array
4788  assert(m_value.object != nullptr);
4789  m_value.object->insert(val);
4790  }
4791 
4792  /*!
4793  @brief add an object to an object
4794  @copydoc push_back(const typename object_t::value_type&)
4795  */
4796  reference operator+=(const typename object_t::value_type& val)
4797  {
4798  push_back(val);
4799  return operator[](val.first);
4800  }
4801 
4802  /*!
4803  @brief inserts element
4804 
4805  Inserts element @a val before iterator @a pos.
4806 
4807  @param[in] pos iterator before which the content will be inserted; may be
4808  the end() iterator
4809  @param[in] val element to insert
4810  @return iterator pointing to the inserted @a val.
4811 
4812  @throw std::domain_error if called on JSON values other than arrays;
4813  example: `"cannot use insert() with string"`
4814  @throw std::domain_error if @a pos is not an iterator of *this; example:
4815  `"iterator does not fit current value"`
4816 
4817  @complexity Constant plus linear in the distance between pos and end of the
4818  container.
4819 
4820  @liveexample{The example shows how `insert()` is used.,insert}
4821 
4822  @since version 1.0.0
4823  */
4825  {
4826  // insert only works for arrays
4827  if (is_array())
4828  {
4829  // check if iterator pos fits to this JSON value
4830  if (pos.m_object != this)
4831  {
4832  throw std::domain_error("iterator does not fit current value");
4833  }
4834 
4835  // insert to array and return iterator
4836  iterator result(this);
4837  assert(m_value.array != nullptr);
4838  result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, val);
4839  return result;
4840  }
4841  else
4842  {
4843  throw std::domain_error("cannot use insert() with " + type_name());
4844  }
4845  }
4846 
4847  /*!
4848  @brief inserts element
4849  @copydoc insert(const_iterator, const basic_json&)
4850  */
4852  {
4853  return insert(pos, val);
4854  }
4855 
4856  /*!
4857  @brief inserts elements
4858 
4859  Inserts @a cnt copies of @a val before iterator @a pos.
4860 
4861  @param[in] pos iterator before which the content will be inserted; may be
4862  the end() iterator
4863  @param[in] cnt number of copies of @a val to insert
4864  @param[in] val element to insert
4865  @return iterator pointing to the first element inserted, or @a pos if
4866  `cnt==0`
4867 
4868  @throw std::domain_error if called on JSON values other than arrays;
4869  example: `"cannot use insert() with string"`
4870  @throw std::domain_error if @a pos is not an iterator of *this; example:
4871  `"iterator does not fit current value"`
4872 
4873  @complexity Linear in @a cnt plus linear in the distance between @a pos
4874  and end of the container.
4875 
4876  @liveexample{The example shows how `insert()` is used.,insert__count}
4877 
4878  @since version 1.0.0
4879  */
4881  {
4882  // insert only works for arrays
4883  if (is_array())
4884  {
4885  // check if iterator pos fits to this JSON value
4886  if (pos.m_object != this)
4887  {
4888  throw std::domain_error("iterator does not fit current value");
4889  }
4890 
4891  // insert to array and return iterator
4892  iterator result(this);
4893  assert(m_value.array != nullptr);
4894  result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, cnt, val);
4895  return result;
4896  }
4897  else
4898  {
4899  throw std::domain_error("cannot use insert() with " + type_name());
4900  }
4901  }
4902 
4903  /*!
4904  @brief inserts elements
4905 
4906  Inserts elements from range `[first, last)` before iterator @a pos.
4907 
4908  @param[in] pos iterator before which the content will be inserted; may be
4909  the end() iterator
4910  @param[in] first begin of the range of elements to insert
4911  @param[in] last end of the range of elements to insert
4912 
4913  @throw std::domain_error if called on JSON values other than arrays;
4914  example: `"cannot use insert() with string"`
4915  @throw std::domain_error if @a pos is not an iterator of *this; example:
4916  `"iterator does not fit current value"`
4917  @throw std::domain_error if @a first and @a last do not belong to the same
4918  JSON value; example: `"iterators do not fit"`
4919  @throw std::domain_error if @a first or @a last are iterators into
4920  container for which insert is called; example: `"passed iterators may not
4921  belong to container"`
4922 
4923  @return iterator pointing to the first element inserted, or @a pos if
4924  `first==last`
4925 
4926  @complexity Linear in `std::distance(first, last)` plus linear in the
4927  distance between @a pos and end of the container.
4928 
4929  @liveexample{The example shows how `insert()` is used.,insert__range}
4930 
4931  @since version 1.0.0
4932  */
4934  {
4935  // insert only works for arrays
4936  if (not is_array())
4937  {
4938  throw std::domain_error("cannot use insert() with " + type_name());
4939  }
4940 
4941  // check if iterator pos fits to this JSON value
4942  if (pos.m_object != this)
4943  {
4944  throw std::domain_error("iterator does not fit current value");
4945  }
4946 
4947  if (first.m_object != last.m_object)
4948  {
4949  throw std::domain_error("iterators do not fit");
4950  }
4951 
4952  if (first.m_object == this or last.m_object == this)
4953  {
4954  throw std::domain_error("passed iterators may not belong to container");
4955  }
4956 
4957  // insert to array and return iterator
4958  iterator result(this);
4959  assert(m_value.array != nullptr);
4960  result.m_it.array_iterator = m_value.array->insert(
4961  pos.m_it.array_iterator,
4962  first.m_it.array_iterator,
4963  last.m_it.array_iterator);
4964  return result;
4965  }
4966 
4967  /*!
4968  @brief inserts elements
4969 
4970  Inserts elements from initializer list @a ilist before iterator @a pos.
4971 
4972  @param[in] pos iterator before which the content will be inserted; may be
4973  the end() iterator
4974  @param[in] ilist initializer list to insert the values from
4975 
4976  @throw std::domain_error if called on JSON values other than arrays;
4977  example: `"cannot use insert() with string"`
4978  @throw std::domain_error if @a pos is not an iterator of *this; example:
4979  `"iterator does not fit current value"`
4980 
4981  @return iterator pointing to the first element inserted, or @a pos if
4982  `ilist` is empty
4983 
4984  @complexity Linear in `ilist.size()` plus linear in the distance between @a
4985  pos and end of the container.
4986 
4987  @liveexample{The example shows how `insert()` is used.,insert__ilist}
4988 
4989  @since version 1.0.0
4990  */
4991  iterator insert(const_iterator pos, std::initializer_list<basic_json> ilist)
4992  {
4993  // insert only works for arrays
4994  if (not is_array())
4995  {
4996  throw std::domain_error("cannot use insert() with " + type_name());
4997  }
4998 
4999  // check if iterator pos fits to this JSON value
5000  if (pos.m_object != this)
5001  {
5002  throw std::domain_error("iterator does not fit current value");
5003  }
5004 
5005  // insert to array and return iterator
5006  iterator result(this);
5007  assert(m_value.array != nullptr);
5008  result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, ilist);
5009  return result;
5010  }
5011 
5012  /*!
5013  @brief exchanges the values
5014 
5015  Exchanges the contents of the JSON value with those of @a other. Does not
5016  invoke any move, copy, or swap operations on individual elements. All
5017  iterators and references remain valid. The past-the-end iterator is
5018  invalidated.
5019 
5020  @param[in,out] other JSON value to exchange the contents with
5021 
5022  @complexity Constant.
5023 
5024  @liveexample{The example below shows how JSON values can be swapped with
5025  `swap()`.,swap__reference}
5026 
5027  @since version 1.0.0
5028  */
5029  void swap(reference other) noexcept (
5034  )
5035  {
5036  std::swap(m_type, other.m_type);
5037  std::swap(m_value, other.m_value);
5038  }
5039 
5040  /*!
5041  @brief exchanges the values
5042 
5043  Exchanges the contents of a JSON array with those of @a other. Does not
5044  invoke any move, copy, or swap operations on individual elements. All
5045  iterators and references remain valid. The past-the-end iterator is
5046  invalidated.
5047 
5048  @param[in,out] other array to exchange the contents with
5049 
5050  @throw std::domain_error when JSON value is not an array; example: `"cannot
5051  use swap() with string"`
5052 
5053  @complexity Constant.
5054 
5055  @liveexample{The example below shows how arrays can be swapped with
5056  `swap()`.,swap__array_t}
5057 
5058  @since version 1.0.0
5059  */
5060  void swap(array_t& other)
5061  {
5062  // swap only works for arrays
5063  if (is_array())
5064  {
5065  assert(m_value.array != nullptr);
5066  std::swap(*(m_value.array), other);
5067  }
5068  else
5069  {
5070  throw std::domain_error("cannot use swap() with " + type_name());
5071  }
5072  }
5073 
5074  /*!
5075  @brief exchanges the values
5076 
5077  Exchanges the contents of a JSON object with those of @a other. Does not
5078  invoke any move, copy, or swap operations on individual elements. All
5079  iterators and references remain valid. The past-the-end iterator is
5080  invalidated.
5081 
5082  @param[in,out] other object to exchange the contents with
5083 
5084  @throw std::domain_error when JSON value is not an object; example:
5085  `"cannot use swap() with string"`
5086 
5087  @complexity Constant.
5088 
5089  @liveexample{The example below shows how objects can be swapped with
5090  `swap()`.,swap__object_t}
5091 
5092  @since version 1.0.0
5093  */
5094  void swap(object_t& other)
5095  {
5096  // swap only works for objects
5097  if (is_object())
5098  {
5099  assert(m_value.object != nullptr);
5100  std::swap(*(m_value.object), other);
5101  }
5102  else
5103  {
5104  throw std::domain_error("cannot use swap() with " + type_name());
5105  }
5106  }
5107 
5108  /*!
5109  @brief exchanges the values
5110 
5111  Exchanges the contents of a JSON string with those of @a other. Does not
5112  invoke any move, copy, or swap operations on individual elements. All
5113  iterators and references remain valid. The past-the-end iterator is
5114  invalidated.
5115 
5116  @param[in,out] other string to exchange the contents with
5117 
5118  @throw std::domain_error when JSON value is not a string; example: `"cannot
5119  use swap() with boolean"`
5120 
5121  @complexity Constant.
5122 
5123  @liveexample{The example below shows how strings can be swapped with
5124  `swap()`.,swap__string_t}
5125 
5126  @since version 1.0.0
5127  */
5128  void swap(string_t& other)
5129  {
5130  // swap only works for strings
5131  if (is_string())
5132  {
5133  assert(m_value.string != nullptr);
5134  std::swap(*(m_value.string), other);
5135  }
5136  else
5137  {
5138  throw std::domain_error("cannot use swap() with " + type_name());
5139  }
5140  }
5141 
5142  /// @}
5143 
5144 
5145  //////////////////////////////////////////
5146  // lexicographical comparison operators //
5147  //////////////////////////////////////////
5148 
5149  /// @name lexicographical comparison operators
5150  /// @{
5151 
5152  private:
5153  /*!
5154  @brief comparison operator for JSON types
5155 
5156  Returns an ordering that is similar to Python:
5157  - order: null < boolean < number < object < array < string
5158  - furthermore, each type is not smaller than itself
5159 
5160  @since version 1.0.0
5161  */
5162  friend bool operator<(const value_t lhs, const value_t rhs) noexcept
5163  {
5164  static constexpr std::array<uint8_t, 8> order = {{
5165  0, // null
5166  3, // object
5167  4, // array
5168  5, // string
5169  1, // boolean
5170  2, // integer
5171  2, // unsigned
5172  2, // float
5173  }
5174  };
5175 
5176  // discarded values are not comparable
5177  if (lhs == value_t::discarded or rhs == value_t::discarded)
5178  {
5179  return false;
5180  }
5181 
5182  return order[static_cast<std::size_t>(lhs)] < order[static_cast<std::size_t>(rhs)];
5183  }
5184 
5185  public:
5186  /*!
5187  @brief comparison: equal
5188 
5189  Compares two JSON values for equality according to the following rules:
5190  - Two JSON values are equal if (1) they are from the same type and (2)
5191  their stored values are the same.
5192  - Integer and floating-point numbers are automatically converted before
5193  comparison. Floating-point numbers are compared indirectly: two
5194  floating-point numbers `f1` and `f2` are considered equal if neither
5195  `f1 > f2` nor `f2 > f1` holds.
5196  - Two JSON null values are equal.
5197 
5198  @param[in] lhs first JSON value to consider
5199  @param[in] rhs second JSON value to consider
5200  @return whether the values @a lhs and @a rhs are equal
5201 
5202  @complexity Linear.
5203 
5204  @liveexample{The example demonstrates comparing several JSON
5205  types.,operator__equal}
5206 
5207  @since version 1.0.0
5208  */
5209  friend bool operator==(const_reference lhs, const_reference rhs) noexcept
5210  {
5211  const auto lhs_type = lhs.type();
5212  const auto rhs_type = rhs.type();
5213 
5214  if (lhs_type == rhs_type)
5215  {
5216  switch (lhs_type)
5217  {
5218  case value_t::array:
5219  {
5220  assert(lhs.m_value.array != nullptr);
5221  assert(rhs.m_value.array != nullptr);
5222  return *lhs.m_value.array == *rhs.m_value.array;
5223  }
5224  case value_t::object:
5225  {
5226  assert(lhs.m_value.object != nullptr);
5227  assert(rhs.m_value.object != nullptr);
5228  return *lhs.m_value.object == *rhs.m_value.object;
5229  }
5230  case value_t::null:
5231  {
5232  return true;
5233  }
5234  case value_t::string:
5235  {
5236  assert(lhs.m_value.string != nullptr);
5237  assert(rhs.m_value.string != nullptr);
5238  return *lhs.m_value.string == *rhs.m_value.string;
5239  }
5240  case value_t::boolean:
5241  {
5242  return lhs.m_value.boolean == rhs.m_value.boolean;
5243  }
5245  {
5246  return lhs.m_value.number_integer == rhs.m_value.number_integer;
5247  }
5249  {
5250  return lhs.m_value.number_unsigned == rhs.m_value.number_unsigned;
5251  }
5252  case value_t::number_float:
5253  {
5254  return lhs.m_value.number_float == rhs.m_value.number_float;
5255  }
5256  default:
5257  {
5258  return false;
5259  }
5260  }
5261  }
5262  else if (lhs_type == value_t::number_integer and rhs_type == value_t::number_float)
5263  {
5264  return static_cast<number_float_t>(lhs.m_value.number_integer) == rhs.m_value.number_float;
5265  }
5266  else if (lhs_type == value_t::number_float and rhs_type == value_t::number_integer)
5267  {
5268  return lhs.m_value.number_float == static_cast<number_float_t>(rhs.m_value.number_integer);
5269  }
5270  else if (lhs_type == value_t::number_unsigned and rhs_type == value_t::number_float)
5271  {
5272  return static_cast<number_float_t>(lhs.m_value.number_unsigned) == rhs.m_value.number_float;
5273  }
5274  else if (lhs_type == value_t::number_float and rhs_type == value_t::number_unsigned)
5275  {
5276  return lhs.m_value.number_float == static_cast<number_float_t>(rhs.m_value.number_unsigned);
5277  }
5278  else if (lhs_type == value_t::number_unsigned and rhs_type == value_t::number_integer)
5279  {
5280  return static_cast<number_integer_t>(lhs.m_value.number_unsigned) == rhs.m_value.number_integer;
5281  }
5282  else if (lhs_type == value_t::number_integer and rhs_type == value_t::number_unsigned)
5283  {
5284  return lhs.m_value.number_integer == static_cast<number_integer_t>(rhs.m_value.number_unsigned);
5285  }
5286 
5287  return false;
5288  }
5289 
5290  /*!
5291  @brief comparison: equal
5292 
5293  The functions compares the given JSON value against a null pointer. As the
5294  null pointer can be used to initialize a JSON value to null, a comparison
5295  of JSON value @a v with a null pointer should be equivalent to call
5296  `v.is_null()`.
5297 
5298  @param[in] v JSON value to consider
5299  @return whether @a v is null
5300 
5301  @complexity Constant.
5302 
5303  @liveexample{The example compares several JSON types to the null pointer.
5304  ,operator__equal__nullptr_t}
5305 
5306  @since version 1.0.0
5307  */
5308  friend bool operator==(const_reference v, std::nullptr_t) noexcept
5309  {
5310  return v.is_null();
5311  }
5312 
5313  /*!
5314  @brief comparison: equal
5315  @copydoc operator==(const_reference, std::nullptr_t)
5316  */
5317  friend bool operator==(std::nullptr_t, const_reference v) noexcept
5318  {
5319  return v.is_null();
5320  }
5321 
5322  /*!
5323  @brief comparison: not equal
5324 
5325  Compares two JSON values for inequality by calculating `not (lhs == rhs)`.
5326 
5327  @param[in] lhs first JSON value to consider
5328  @param[in] rhs second JSON value to consider
5329  @return whether the values @a lhs and @a rhs are not equal
5330 
5331  @complexity Linear.
5332 
5333  @liveexample{The example demonstrates comparing several JSON
5334  types.,operator__notequal}
5335 
5336  @since version 1.0.0
5337  */
5338  friend bool operator!=(const_reference lhs, const_reference rhs) noexcept
5339  {
5340  return not (lhs == rhs);
5341  }
5342 
5343  /*!
5344  @brief comparison: not equal
5345 
5346  The functions compares the given JSON value against a null pointer. As the
5347  null pointer can be used to initialize a JSON value to null, a comparison
5348  of JSON value @a v with a null pointer should be equivalent to call
5349  `not v.is_null()`.
5350 
5351  @param[in] v JSON value to consider
5352  @return whether @a v is not null
5353 
5354  @complexity Constant.
5355 
5356  @liveexample{The example compares several JSON types to the null pointer.
5357  ,operator__notequal__nullptr_t}
5358 
5359  @since version 1.0.0
5360  */
5361  friend bool operator!=(const_reference v, std::nullptr_t) noexcept
5362  {
5363  return not v.is_null();
5364  }
5365 
5366  /*!
5367  @brief comparison: not equal
5368  @copydoc operator!=(const_reference, std::nullptr_t)
5369  */
5370  friend bool operator!=(std::nullptr_t, const_reference v) noexcept
5371  {
5372  return not v.is_null();
5373  }
5374 
5375  /*!
5376  @brief comparison: less than
5377 
5378  Compares whether one JSON value @a lhs is less than another JSON value @a
5379  rhs according to the following rules:
5380  - If @a lhs and @a rhs have the same type, the values are compared using
5381  the default `<` operator.
5382  - Integer and floating-point numbers are automatically converted before
5383  comparison
5384  - In case @a lhs and @a rhs have different types, the values are ignored
5385  and the order of the types is considered, see
5386  @ref operator<(const value_t, const value_t).
5387 
5388  @param[in] lhs first JSON value to consider
5389  @param[in] rhs second JSON value to consider
5390  @return whether @a lhs is less than @a rhs
5391 
5392  @complexity Linear.
5393 
5394  @liveexample{The example demonstrates comparing several JSON
5395  types.,operator__less}
5396 
5397  @since version 1.0.0
5398  */
5399  friend bool operator<(const_reference lhs, const_reference rhs) noexcept
5400  {
5401  const auto lhs_type = lhs.type();
5402  const auto rhs_type = rhs.type();
5403 
5404  if (lhs_type == rhs_type)
5405  {
5406  switch (lhs_type)
5407  {
5408  case value_t::array:
5409  {
5410  assert(lhs.m_value.array != nullptr);
5411  assert(rhs.m_value.array != nullptr);
5412  return *lhs.m_value.array < *rhs.m_value.array;
5413  }
5414  case value_t::object:
5415  {
5416  assert(lhs.m_value.object != nullptr);
5417  assert(rhs.m_value.object != nullptr);
5418  return *lhs.m_value.object < *rhs.m_value.object;
5419  }
5420  case value_t::null:
5421  {
5422  return false;
5423  }
5424  case value_t::string:
5425  {
5426  assert(lhs.m_value.string != nullptr);
5427  assert(rhs.m_value.string != nullptr);
5428  return *lhs.m_value.string < *rhs.m_value.string;
5429  }
5430  case value_t::boolean:
5431  {
5432  return lhs.m_value.boolean < rhs.m_value.boolean;
5433  }
5435  {
5436  return lhs.m_value.number_integer < rhs.m_value.number_integer;
5437  }
5439  {
5440  return lhs.m_value.number_unsigned < rhs.m_value.number_unsigned;
5441  }
5442  case value_t::number_float:
5443  {
5444  return lhs.m_value.number_float < rhs.m_value.number_float;
5445  }
5446  default:
5447  {
5448  return false;
5449  }
5450  }
5451  }
5452  else if (lhs_type == value_t::number_integer and rhs_type == value_t::number_float)
5453  {
5454  return static_cast<number_float_t>(lhs.m_value.number_integer) < rhs.m_value.number_float;
5455  }
5456  else if (lhs_type == value_t::number_float and rhs_type == value_t::number_integer)
5457  {
5458  return lhs.m_value.number_float < static_cast<number_float_t>(rhs.m_value.number_integer);
5459  }
5460  else if (lhs_type == value_t::number_unsigned and rhs_type == value_t::number_float)
5461  {
5462  return static_cast<number_float_t>(lhs.m_value.number_unsigned) < rhs.m_value.number_float;
5463  }
5464  else if (lhs_type == value_t::number_float and rhs_type == value_t::number_unsigned)
5465  {
5466  return lhs.m_value.number_float < static_cast<number_float_t>(rhs.m_value.number_unsigned);
5467  }
5468  else if (lhs_type == value_t::number_integer and rhs_type == value_t::number_unsigned)
5469  {
5470  return lhs.m_value.number_integer < static_cast<number_integer_t>(rhs.m_value.number_unsigned);
5471  }
5472  else if (lhs_type == value_t::number_unsigned and rhs_type == value_t::number_integer)
5473  {
5474  return static_cast<number_integer_t>(lhs.m_value.number_unsigned) < rhs.m_value.number_integer;
5475  }
5476 
5477  // We only reach this line if we cannot compare values. In that case,
5478  // we compare types. Note we have to call the operator explicitly,
5479  // because MSVC has problems otherwise.
5480  return operator<(lhs_type, rhs_type);
5481  }
5482 
5483  /*!
5484  @brief comparison: less than or equal
5485 
5486  Compares whether one JSON value @a lhs is less than or equal to another
5487  JSON value by calculating `not (rhs < lhs)`.
5488 
5489  @param[in] lhs first JSON value to consider
5490  @param[in] rhs second JSON value to consider
5491  @return whether @a lhs is less than or equal to @a rhs
5492 
5493  @complexity Linear.
5494 
5495  @liveexample{The example demonstrates comparing several JSON
5496  types.,operator__greater}
5497 
5498  @since version 1.0.0
5499  */
5500  friend bool operator<=(const_reference lhs, const_reference rhs) noexcept
5501  {
5502  return not (rhs < lhs);
5503  }
5504 
5505  /*!
5506  @brief comparison: greater than
5507 
5508  Compares whether one JSON value @a lhs is greater than another
5509  JSON value by calculating `not (lhs <= rhs)`.
5510 
5511  @param[in] lhs first JSON value to consider
5512  @param[in] rhs second JSON value to consider
5513  @return whether @a lhs is greater than to @a rhs
5514 
5515  @complexity Linear.
5516 
5517  @liveexample{The example demonstrates comparing several JSON
5518  types.,operator__lessequal}
5519 
5520  @since version 1.0.0
5521  */
5522  friend bool operator>(const_reference lhs, const_reference rhs) noexcept
5523  {
5524  return not (lhs <= rhs);
5525  }
5526 
5527  /*!
5528  @brief comparison: greater than or equal
5529 
5530  Compares whether one JSON value @a lhs is greater than or equal to another
5531  JSON value by calculating `not (lhs < rhs)`.
5532 
5533  @param[in] lhs first JSON value to consider
5534  @param[in] rhs second JSON value to consider
5535  @return whether @a lhs is greater than or equal to @a rhs
5536 
5537  @complexity Linear.
5538 
5539  @liveexample{The example demonstrates comparing several JSON
5540  types.,operator__greaterequal}
5541 
5542  @since version 1.0.0
5543  */
5544  friend bool operator>=(const_reference lhs, const_reference rhs) noexcept
5545  {
5546  return not (lhs < rhs);
5547  }
5548 
5549  /// @}
5550 
5551 
5552  ///////////////////
5553  // serialization //
5554  ///////////////////
5555 
5556  /// @name serialization
5557  /// @{
5558 
5559  /*!
5560  @brief serialize to stream
5561 
5562  Serialize the given JSON value @a j to the output stream @a o. The JSON
5563  value will be serialized using the @ref dump member function. The
5564  indentation of the output can be controlled with the member variable
5565  `width` of the output stream @a o. For instance, using the manipulator
5566  `std::setw(4)` on @a o sets the indentation level to `4` and the
5567  serialization result is the same as calling `dump(4)`.
5568 
5569  @param[in,out] o stream to serialize to
5570  @param[in] j JSON value to serialize
5571 
5572  @return the stream @a o
5573 
5574  @complexity Linear.
5575 
5576  @liveexample{The example below shows the serialization with different
5577  parameters to `width` to adjust the indentation level.,operator_serialize}
5578 
5579  @since version 1.0.0
5580  */
5581  friend std::ostream& operator<<(std::ostream& o, const basic_json& j)
5582  {
5583  // read width member and use it as indentation parameter if nonzero
5584  const bool pretty_print = (o.width() > 0);
5585  const auto indentation = (pretty_print ? o.width() : 0);
5586 
5587  // reset width to 0 for subsequent calls to this stream
5588  o.width(0);
5589 
5590  // do the actual serialization
5591  j.dump(o, pretty_print, static_cast<unsigned int>(indentation));
5592  return o;
5593  }
5594 
5595  /*!
5596  @brief serialize to stream
5597  @copydoc operator<<(std::ostream&, const basic_json&)
5598  */
5599  friend std::ostream& operator>>(const basic_json& j, std::ostream& o)
5600  {
5601  return o << j;
5602  }
5603 
5604  /// @}
5605 
5606 
5607  /////////////////////
5608  // deserialization //
5609  /////////////////////
5610 
5611  /// @name deserialization
5612  /// @{
5613 
5614  /*!
5615  @brief deserialize from string
5616 
5617  @param[in] s string to read a serialized JSON value from
5618  @param[in] cb a parser callback function of type @ref parser_callback_t
5619  which is used to control the deserialization by filtering unwanted values
5620  (optional)
5621 
5622  @return result of the deserialization
5623 
5624  @complexity Linear in the length of the input. The parser is a predictive
5625  LL(1) parser. The complexity can be higher if the parser callback function
5626  @a cb has a super-linear complexity.
5627 
5628  @note A UTF-8 byte order mark is silently ignored.
5629 
5630  @liveexample{The example below demonstrates the `parse()` function with and
5631  without callback function.,parse__string__parser_callback_t}
5632 
5633  @sa @ref parse(std::istream&, parser_callback_t) for a version that reads
5634  from an input stream
5635 
5636  @since version 1.0.0
5637  */
5638  static basic_json parse(const string_t& s, parser_callback_t cb = nullptr)
5639  {
5640  return parser(s, cb).parse();
5641  }
5642 
5643  /*!
5644  @brief deserialize from stream
5645 
5646  @param[in,out] i stream to read a serialized JSON value from
5647  @param[in] cb a parser callback function of type @ref parser_callback_t
5648  which is used to control the deserialization by filtering unwanted values
5649  (optional)
5650 
5651  @return result of the deserialization
5652 
5653  @complexity Linear in the length of the input. The parser is a predictive
5654  LL(1) parser. The complexity can be higher if the parser callback function
5655  @a cb has a super-linear complexity.
5656 
5657  @note A UTF-8 byte order mark is silently ignored.
5658 
5659  @liveexample{The example below demonstrates the `parse()` function with and
5660  without callback function.,parse__istream__parser_callback_t}
5661 
5662  @sa @ref parse(const string_t&, parser_callback_t) for a version that reads
5663  from a string
5664 
5665  @since version 1.0.0
5666  */
5667  static basic_json parse(std::istream& i, parser_callback_t cb = nullptr)
5668  {
5669  return parser(i, cb).parse();
5670  }
5671 
5672  /*!
5673  @copydoc parse(std::istream&, parser_callback_t)
5674  */
5675  static basic_json parse(std::istream&& i, parser_callback_t cb = nullptr)
5676  {
5677  return parser(i, cb).parse();
5678  }
5679 
5680  /*!
5681  @brief deserialize from stream
5682 
5683  Deserializes an input stream to a JSON value.
5684 
5685  @param[in,out] i input stream to read a serialized JSON value from
5686  @param[in,out] j JSON value to write the deserialized input to
5687 
5688  @throw std::invalid_argument in case of parse errors
5689 
5690  @complexity Linear in the length of the input. The parser is a predictive
5691  LL(1) parser.
5692 
5693  @note A UTF-8 byte order mark is silently ignored.
5694 
5695  @liveexample{The example below shows how a JSON value is constructed by
5696  reading a serialization from a stream.,operator_deserialize}
5697 
5698  @sa parse(std::istream&, parser_callback_t) for a variant with a parser
5699  callback function to filter values while parsing
5700 
5701  @since version 1.0.0
5702  */
5703  friend std::istream& operator<<(basic_json& j, std::istream& i)
5704  {
5705  j = parser(i).parse();
5706  return i;
5707  }
5708 
5709  /*!
5710  @brief deserialize from stream
5711  @copydoc operator<<(basic_json&, std::istream&)
5712  */
5713  friend std::istream& operator>>(std::istream& i, basic_json& j)
5714  {
5715  j = parser(i).parse();
5716  return i;
5717  }
5718 
5719  /// @}
5720 
5721 
5722  private:
5723  ///////////////////////////
5724  // convenience functions //
5725  ///////////////////////////
5726 
5727  /// return the type as string
5728  string_t type_name() const noexcept
5729  {
5730  switch (m_type)
5731  {
5732  case value_t::null:
5733  return "null";
5734  case value_t::object:
5735  return "object";
5736  case value_t::array:
5737  return "array";
5738  case value_t::string:
5739  return "string";
5740  case value_t::boolean:
5741  return "boolean";
5742  case value_t::discarded:
5743  return "discarded";
5744  default:
5745  return "number";
5746  }
5747  }
5748 
5749  /*!
5750  @brief calculates the extra space to escape a JSON string
5751 
5752  @param[in] s the string to escape
5753  @return the number of characters required to escape string @a s
5754 
5755  @complexity Linear in the length of string @a s.
5756  */
5757  static std::size_t extra_space(const string_t& s) noexcept
5758  {
5759  std::size_t result = 0;
5760 
5761  for (const auto& c : s)
5762  {
5763  switch (c)
5764  {
5765  case '"':
5766  case '\\':
5767  case '\b':
5768  case '\f':
5769  case '\n':
5770  case '\r':
5771  case '\t':
5772  {
5773  // from c (1 byte) to \x (2 bytes)
5774  result += 1;
5775  break;
5776  }
5777 
5778  default:
5779  {
5780  if (c >= 0x00 and c <= 0x1f)
5781  {
5782  // from c (1 byte) to \uxxxx (6 bytes)
5783  result += 5;
5784  }
5785  break;
5786  }
5787  }
5788  }
5789 
5790  return result;
5791  }
5792 
5793  /*!
5794  @brief escape a string
5795 
5796  Escape a string by replacing certain special characters by a sequence of an
5797  escape character (backslash) and another character and other control
5798  characters by a sequence of "\u" followed by a four-digit hex
5799  representation.
5800 
5801  @param[in] s the string to escape
5802  @return the escaped string
5803 
5804  @complexity Linear in the length of string @a s.
5805  */
5807  {
5808  const auto space = extra_space(s);
5809  if (space == 0)
5810  {
5811  return s;
5812  }
5813 
5814  // create a result string of necessary size
5815  string_t result(s.size() + space, '\\');
5816  std::size_t pos = 0;
5817 
5818  for (const auto& c : s)
5819  {
5820  switch (c)
5821  {
5822  // quotation mark (0x22)
5823  case '"':
5824  {
5825  result[pos + 1] = '"';
5826  pos += 2;
5827  break;
5828  }
5829 
5830  // reverse solidus (0x5c)
5831  case '\\':
5832  {
5833  // nothing to change
5834  pos += 2;
5835  break;
5836  }
5837 
5838  // backspace (0x08)
5839  case '\b':
5840  {
5841  result[pos + 1] = 'b';
5842  pos += 2;
5843  break;
5844  }
5845 
5846  // formfeed (0x0c)
5847  case '\f':
5848  {
5849  result[pos + 1] = 'f';
5850  pos += 2;
5851  break;
5852  }
5853 
5854  // newline (0x0a)
5855  case '\n':
5856  {
5857  result[pos + 1] = 'n';
5858  pos += 2;
5859  break;
5860  }
5861 
5862  // carriage return (0x0d)
5863  case '\r':
5864  {
5865  result[pos + 1] = 'r';
5866  pos += 2;
5867  break;
5868  }
5869 
5870  // horizontal tab (0x09)
5871  case '\t':
5872  {
5873  result[pos + 1] = 't';
5874  pos += 2;
5875  break;
5876  }
5877 
5878  default:
5879  {
5880  if (c >= 0x00 and c <= 0x1f)
5881  {
5882  // convert a number 0..15 to its hex representation
5883  // (0..f)
5884  auto hexify = [](const char v) -> char
5885  {
5886  return (v < 10) ? ('0' + v) : ('a' + v - 10);
5887  };
5888 
5889  // print character c as \uxxxx
5890  for (const char m :
5891  { 'u', '0', '0', hexify(c >> 4), hexify(c & 0x0f)
5892  })
5893  {
5894  result[++pos] = m;
5895  }
5896 
5897  ++pos;
5898  }
5899  else
5900  {
5901  // all other characters are added as-is
5902  result[pos++] = c;
5903  }
5904  break;
5905  }
5906  }
5907  }
5908 
5909  return result;
5910  }
5911 
5912  /*!
5913  @brief internal implementation of the serialization function
5914 
5915  This function is called by the public member function dump and organizes
5916  the serialization internally. The indentation level is propagated as
5917  additional parameter. In case of arrays and objects, the function is called
5918  recursively. Note that
5919 
5920  - strings and object keys are escaped using `escape_string()`
5921  - integer numbers are converted implicitly via `operator<<`
5922  - floating-point numbers are converted to a string using `"%g"` format
5923 
5924  @param[out] o stream to write to
5925  @param[in] pretty_print whether the output shall be pretty-printed
5926  @param[in] indent_step the indent level
5927  @param[in] current_indent the current indent level (only used internally)
5928  */
5929  void dump(std::ostream& o,
5930  const bool pretty_print,
5931  const unsigned int indent_step,
5932  const unsigned int current_indent = 0) const
5933  {
5934  // variable to hold indentation for recursive calls
5935  unsigned int new_indent = current_indent;
5936 
5937  switch (m_type)
5938  {
5939  case value_t::object:
5940  {
5941  assert(m_value.object != nullptr);
5942 
5943  if (m_value.object->empty())
5944  {
5945  o << "{}";
5946  return;
5947  }
5948 
5949  o << "{";
5950 
5951  // increase indentation
5952  if (pretty_print)
5953  {
5954  new_indent += indent_step;
5955  o << "\n";
5956  }
5957 
5958  for (auto i = m_value.object->cbegin(); i != m_value.object->cend(); ++i)
5959  {
5960  if (i != m_value.object->cbegin())
5961  {
5962  o << (pretty_print ? ",\n" : ",");
5963  }
5964  o << string_t(new_indent, ' ') << "\""
5965  << escape_string(i->first) << "\":"
5966  << (pretty_print ? " " : "");
5967  i->second.dump(o, pretty_print, indent_step, new_indent);
5968  }
5969 
5970  // decrease indentation
5971  if (pretty_print)
5972  {
5973  new_indent -= indent_step;
5974  o << "\n";
5975  }
5976 
5977  o << string_t(new_indent, ' ') + "}";
5978  return;
5979  }
5980 
5981  case value_t::array:
5982  {
5983  assert(m_value.array != nullptr);
5984 
5985  if (m_value.array->empty())
5986  {
5987  o << "[]";
5988  return;
5989  }
5990 
5991  o << "[";
5992 
5993  // increase indentation
5994  if (pretty_print)
5995  {
5996  new_indent += indent_step;
5997  o << "\n";
5998  }
5999 
6000  for (auto i = m_value.array->cbegin(); i != m_value.array->cend(); ++i)
6001  {
6002  if (i != m_value.array->cbegin())
6003  {
6004  o << (pretty_print ? ",\n" : ",");
6005  }
6006  o << string_t(new_indent, ' ');
6007  i->dump(o, pretty_print, indent_step, new_indent);
6008  }
6009 
6010  // decrease indentation
6011  if (pretty_print)
6012  {
6013  new_indent -= indent_step;
6014  o << "\n";
6015  }
6016 
6017  o << string_t(new_indent, ' ') << "]";
6018  return;
6019  }
6020 
6021  case value_t::string:
6022  {
6023  assert(m_value.string != nullptr);
6024  o << string_t("\"") << escape_string(*m_value.string) << "\"";
6025  return;
6026  }
6027 
6028  case value_t::boolean:
6029  {
6030  o << (m_value.boolean ? "true" : "false");
6031  return;
6032  }
6033 
6035  {
6036  o << m_value.number_integer;
6037  return;
6038  }
6039 
6041  {
6042  o << m_value.number_unsigned;
6043  return;
6044  }
6045 
6046  case value_t::number_float:
6047  {
6048  // If the number is an integer then output as a fixed with with
6049  // precision 1 to output "0.0", "1.0" etc as expected for some
6050  // round trip tests otherwise 15 digits of precision allows
6051  // round-trip IEEE 754 string->double->string; to be safe, we
6052  // read this value from
6053  // std::numeric_limits<number_float_t>::digits10
6054  if (std::fmod(m_value.number_float, 1) == 0)
6055  {
6056  o << std::fixed << std::setprecision(1);
6057  }
6058  else
6059  {
6060  // std::defaultfloat not supported in gcc version < 5
6061  o.unsetf(std::ios_base::floatfield);
6062  o << std::setprecision(std::numeric_limits<double>::digits10);
6063  }
6064  o << m_value.number_float;
6065  return;
6066  }
6067 
6068  case value_t::discarded:
6069  {
6070  o << "<discarded>";
6071  return;
6072  }
6073 
6074  case value_t::null:
6075  {
6076  o << "null";
6077  return;
6078  }
6079  }
6080  }
6081 
6082  private:
6083  //////////////////////
6084  // member variables //
6085  //////////////////////
6086 
6087  /// the type of the current element
6089 
6090  /// the value of the current element
6092 
6093 
6094  private:
6095  ///////////////
6096  // iterators //
6097  ///////////////
6098 
6099  /*!
6100  @brief an iterator for primitive JSON types
6101 
6102  This class models an iterator for primitive JSON types (boolean, number,
6103  string). It's only purpose is to allow the iterator/const_iterator classes
6104  to "iterate" over primitive values. Internally, the iterator is modeled by
6105  a `difference_type` variable. Value begin_value (`0`) models the begin,
6106  end_value (`1`) models past the end.
6107  */
6109  {
6110  public:
6111  /// set iterator to a defined beginning
6112  void set_begin() noexcept
6113  {
6114  m_it = begin_value;
6115  }
6116 
6117  /// set iterator to a defined past the end
6118  void set_end() noexcept
6119  {
6120  m_it = end_value;
6121  }
6122 
6123  /// return whether the iterator can be dereferenced
6124  constexpr bool is_begin() const noexcept
6125  {
6126  return (m_it == begin_value);
6127  }
6128 
6129  /// return whether the iterator is at end
6130  constexpr bool is_end() const noexcept
6131  {
6132  return (m_it == end_value);
6133  }
6134 
6135  /// return reference to the value to change and compare
6136  operator difference_type& () noexcept
6137  {
6138  return m_it;
6139  }
6140 
6141  /// return value to compare
6142  constexpr operator difference_type () const noexcept
6143  {
6144  return m_it;
6145  }
6146 
6147  private:
6148  static constexpr difference_type begin_value = 0;
6149  static constexpr difference_type end_value = begin_value + 1;
6150 
6151  /// iterator as signed integer type
6152  difference_type m_it = std::numeric_limits<std::ptrdiff_t>::denorm_min();
6153  };
6154 
6155  /*!
6156  @brief an iterator value
6157 
6158  @note This structure could easily be a union, but MSVC currently does not
6159  allow unions members with complex constructors, see
6160  https://github.com/nlohmann/json/pull/105.
6161  */
6163  {
6164  /// iterator for JSON objects
6165  typename object_t::iterator object_iterator;
6166  /// iterator for JSON arrays
6167  typename array_t::iterator array_iterator;
6168  /// generic iterator for all other types
6170 
6171  /// create an uninitialized internal_iterator
6174  {}
6175  };
6176 
6177  /// proxy class for the iterator_wrapper functions
6178  template<typename IteratorType>
6179  class iteration_proxy
6180  {
6181  private:
6182  /// helper class for iteration
6184  {
6185  private:
6186  /// the iterator
6187  IteratorType anchor;
6188  /// an index for arrays (used to create key names)
6189  size_t array_index = 0;
6190 
6191  public:
6192  explicit iteration_proxy_internal(IteratorType it) noexcept
6193  : anchor(it)
6194  {}
6195 
6196  /// dereference operator (needed for range-based for)
6198  {
6199  return *this;
6200  }
6201 
6202  /// increment operator (needed for range-based for)
6204  {
6205  ++anchor;
6206  ++array_index;
6207 
6208  return *this;
6209  }
6210 
6211  /// inequality operator (needed for range-based for)
6213  {
6214  return anchor != o.anchor;
6215  }
6216 
6217  /// return key of the iterator
6218  typename basic_json::string_t key() const
6219  {
6220  assert(anchor.m_object != nullptr);
6221 
6222  switch (anchor.m_object->type())
6223  {
6224  // use integer array index as key
6225  case value_t::array:
6226  {
6227  return std::to_string(array_index);
6228  }
6229 
6230  // use key from the object
6231  case value_t::object:
6232  {
6233  return anchor.key();
6234  }
6235 
6236  // use an empty key for all primitive types
6237  default:
6238  {
6239  return "";
6240  }
6241  }
6242  }
6243 
6244  /// return value of the iterator
6245  typename IteratorType::reference value() const
6246  {
6247  return anchor.value();
6248  }
6249  };
6250 
6251  /// the container to iterate
6252  typename IteratorType::reference container;
6253 
6254  public:
6255  /// construct iteration proxy from a container
6256  explicit iteration_proxy(typename IteratorType::reference cont)
6257  : container(cont)
6258  {}
6259 
6260  /// return iterator begin (needed for range-based for)
6262  {
6263  return iteration_proxy_internal(container.begin());
6264  }
6265 
6266  /// return iterator end (needed for range-based for)
6268  {
6269  return iteration_proxy_internal(container.end());
6270  }
6271  };
6272 
6273  public:
6274  /*!
6275  @brief a const random access iterator for the @ref basic_json class
6276 
6277  This class implements a const iterator for the @ref basic_json class. From
6278  this class, the @ref iterator class is derived.
6279 
6280  @requirement The class satisfies the following concept requirements:
6281  - [RandomAccessIterator](http://en.cppreference.com/w/cpp/concept/RandomAccessIterator):
6282  The iterator that can be moved to point (forward and backward) to any
6283  element in constant time.
6284 
6285  @since version 1.0.0
6286  */
6287  class const_iterator : public std::iterator<std::random_access_iterator_tag, const basic_json>
6288  {
6289  /// allow basic_json to access private members
6290  friend class basic_json;
6291 
6292  public:
6293  /// the type of the values when the iterator is dereferenced
6295  /// a type to represent differences between iterators
6297  /// defines a pointer to the type iterated over (value_type)
6299  /// defines a reference to the type iterated over (value_type)
6301  /// the category of the iterator
6302  using iterator_category = std::bidirectional_iterator_tag;
6303 
6304  /// default constructor
6305  const_iterator() = default;
6306 
6307  /// constructor for a given JSON instance
6308  explicit const_iterator(pointer object) noexcept
6309  : m_object(object)
6310  {
6311  assert(m_object != nullptr);
6312 
6313  switch (m_object->m_type)
6314  {
6316  {
6317  m_it.object_iterator = typename object_t::iterator();
6318  break;
6319  }
6320 
6322  {
6323  m_it.array_iterator = typename array_t::iterator();
6324  break;
6325  }
6326 
6327  default:
6328  {
6330  break;
6331  }
6332  }
6333  }
6334 
6335  /// copy constructor given a nonconst iterator
6336  explicit const_iterator(const iterator& other) noexcept
6337  : m_object(other.m_object)
6338  {
6339  assert(m_object != nullptr);
6340 
6341  switch (m_object->m_type)
6342  {
6344  {
6345  m_it.object_iterator = other.m_it.object_iterator;
6346  break;
6347  }
6348 
6350  {
6351  m_it.array_iterator = other.m_it.array_iterator;
6352  break;
6353  }
6354 
6355  default:
6356  {
6357  m_it.primitive_iterator = other.m_it.primitive_iterator;
6358  break;
6359  }
6360  }
6361  }
6362 
6363  /// copy constructor
6364  const_iterator(const const_iterator& other) noexcept
6365  : m_object(other.m_object), m_it(other.m_it)
6366  {}
6367 
6368  /// copy assignment
6374  )
6375  {
6376  std::swap(m_object, other.m_object);
6377  std::swap(m_it, other.m_it);
6378  return *this;
6379  }
6380 
6381  private:
6382  /// set the iterator to the first value
6383  void set_begin() noexcept
6384  {
6385  assert(m_object != nullptr);
6386 
6387  switch (m_object->m_type)
6388  {
6390  {
6391  assert(m_object->m_value.object != nullptr);
6392  m_it.object_iterator = m_object->m_value.object->begin();
6393  break;
6394  }
6395 
6397  {
6398  assert(m_object->m_value.array != nullptr);
6399  m_it.array_iterator = m_object->m_value.array->begin();
6400  break;
6401  }
6402 
6404  {
6405  // set to end so begin()==end() is true: null is empty
6407  break;
6408  }
6409 
6410  default:
6411  {
6413  break;
6414  }
6415  }
6416  }
6417 
6418  /// set the iterator past the last value
6419  void set_end() noexcept
6420  {
6421  assert(m_object != nullptr);
6422 
6423  switch (m_object->m_type)
6424  {
6426  {
6427  assert(m_object->m_value.object != nullptr);
6428  m_it.object_iterator = m_object->m_value.object->end();
6429  break;
6430  }
6431 
6433  {
6434  assert(m_object->m_value.array != nullptr);
6435  m_it.array_iterator = m_object->m_value.array->end();
6436  break;
6437  }
6438 
6439  default:
6440  {
6442  break;
6443  }
6444  }
6445  }
6446 
6447  public:
6448  /// return a reference to the value pointed to by the iterator
6450  {
6451  assert(m_object != nullptr);
6452 
6453  switch (m_object->m_type)
6454  {
6456  {
6457  assert(m_object->m_value.object);
6458  assert(m_it.object_iterator != m_object->m_value.object->end());
6459  return m_it.object_iterator->second;
6460  }
6461 
6463  {
6464  assert(m_object->m_value.array);
6465  assert(m_it.array_iterator != m_object->m_value.array->end());
6466  return *m_it.array_iterator;
6467  }
6468 
6470  {
6471  throw std::out_of_range("cannot get value");
6472  }
6473 
6474  default:
6475  {
6477  {
6478  return *m_object;
6479  }
6480  else
6481  {
6482  throw std::out_of_range("cannot get value");
6483  }
6484  }
6485  }
6486  }
6487 
6488  /// dereference the iterator
6490  {
6491  assert(m_object != nullptr);
6492 
6493  switch (m_object->m_type)
6494  {
6496  {
6497  assert(m_object->m_value.object);
6498  assert(m_it.object_iterator != m_object->m_value.object->end());
6499  return &(m_it.object_iterator->second);
6500  }
6501 
6503  {
6504  assert(m_object->m_value.array);
6505  assert(m_it.array_iterator != m_object->m_value.array->end());
6506  return &*m_it.array_iterator;
6507  }
6508 
6509  default:
6510  {
6512  {
6513  return m_object;
6514  }
6515  else
6516  {
6517  throw std::out_of_range("cannot get value");
6518  }
6519  }
6520  }
6521  }
6522 
6523  /// post-increment (it++)
6525  {
6526  auto result = *this;
6527  ++(*this);
6528  return result;
6529  }
6530 
6531  /// pre-increment (++it)
6533  {
6534  assert(m_object != nullptr);
6535 
6536  switch (m_object->m_type)
6537  {
6539  {
6541  break;
6542  }
6543 
6545  {
6546  ++m_it.array_iterator;
6547  break;
6548  }
6549 
6550  default:
6551  {
6553  break;
6554  }
6555  }
6556 
6557  return *this;
6558  }
6559 
6560  /// post-decrement (it--)
6562  {
6563  auto result = *this;
6564  --(*this);
6565  return result;
6566  }
6567 
6568  /// pre-decrement (--it)
6570  {
6571  assert(m_object != nullptr);
6572 
6573  switch (m_object->m_type)
6574  {
6576  {
6578  break;
6579  }
6580 
6582  {
6583  --m_it.array_iterator;
6584  break;
6585  }
6586 
6587  default:
6588  {
6590  break;
6591  }
6592  }
6593 
6594  return *this;
6595  }
6596 
6597  /// comparison: equal
6598  bool operator==(const const_iterator& other) const
6599  {
6600  // if objects are not the same, the comparison is undefined
6601  if (m_object != other.m_object)
6602  {
6603  throw std::domain_error("cannot compare iterators of different containers");
6604  }
6605 
6606  assert(m_object != nullptr);
6607 
6608  switch (m_object->m_type)
6609  {
6611  {
6612  return (m_it.object_iterator == other.m_it.object_iterator);
6613  }
6614 
6616  {
6617  return (m_it.array_iterator == other.m_it.array_iterator);
6618  }
6619 
6620  default:
6621  {
6622  return (m_it.primitive_iterator == other.m_it.primitive_iterator);
6623  }
6624  }
6625  }
6626 
6627  /// comparison: not equal
6628  bool operator!=(const const_iterator& other) const
6629  {
6630  return not operator==(other);
6631  }
6632 
6633  /// comparison: smaller
6634  bool operator<(const const_iterator& other) const
6635  {
6636  // if objects are not the same, the comparison is undefined
6637  if (m_object != other.m_object)
6638  {
6639  throw std::domain_error("cannot compare iterators of different containers");
6640  }
6641 
6642  assert(m_object != nullptr);
6643 
6644  switch (m_object->m_type)
6645  {
6647  {
6648  throw std::domain_error("cannot compare order of object iterators");
6649  }
6650 
6652  {
6653  return (m_it.array_iterator < other.m_it.array_iterator);
6654  }
6655 
6656  default:
6657  {
6659  }
6660  }
6661  }
6662 
6663  /// comparison: less than or equal
6664  bool operator<=(const const_iterator& other) const
6665  {
6666  return not other.operator < (*this);
6667  }
6668 
6669  /// comparison: greater than
6670  bool operator>(const const_iterator& other) const
6671  {
6672  return not operator<=(other);
6673  }
6674 
6675  /// comparison: greater than or equal
6676  bool operator>=(const const_iterator& other) const
6677  {
6678  return not operator<(other);
6679  }
6680 
6681  /// add to iterator
6683  {
6684  assert(m_object != nullptr);
6685 
6686  switch (m_object->m_type)
6687  {
6689  {
6690  throw std::domain_error("cannot use offsets with object iterators");
6691  }
6692 
6694  {
6695  m_it.array_iterator += i;
6696  break;
6697  }
6698 
6699  default:
6700  {
6701  m_it.primitive_iterator += i;
6702  break;
6703  }
6704  }
6705 
6706  return *this;
6707  }
6708 
6709  /// subtract from iterator
6711  {
6712  return operator+=(-i);
6713  }
6714 
6715  /// add to iterator
6717  {
6718  auto result = *this;
6719  result += i;
6720  return result;
6721  }
6722 
6723  /// subtract from iterator
6725  {
6726  auto result = *this;
6727  result -= i;
6728  return result;
6729  }
6730 
6731  /// return difference
6733  {
6734  assert(m_object != nullptr);
6735 
6736  switch (m_object->m_type)
6737  {
6739  {
6740  throw std::domain_error("cannot use offsets with object iterators");
6741  }
6742 
6744  {
6745  return m_it.array_iterator - other.m_it.array_iterator;
6746  }
6747 
6748  default:
6749  {
6751  }
6752  }
6753  }
6754 
6755  /// access to successor
6757  {
6758  assert(m_object != nullptr);
6759 
6760  switch (m_object->m_type)
6761  {
6763  {
6764  throw std::domain_error("cannot use operator[] for object iterators");
6765  }
6766 
6768  {
6769  return *(m_it.array_iterator + n);
6770  }
6771 
6773  {
6774  throw std::out_of_range("cannot get value");
6775  }
6776 
6777  default:
6778  {
6779  if (m_it.primitive_iterator == -n)
6780  {
6781  return *m_object;
6782  }
6783  else
6784  {
6785  throw std::out_of_range("cannot get value");
6786  }
6787  }
6788  }
6789  }
6790 
6791  /// return the key of an object iterator
6792  typename object_t::key_type key() const
6793  {
6794  assert(m_object != nullptr);
6795 
6796  if (m_object->is_object())
6797  {
6798  return m_it.object_iterator->first;
6799  }
6800  else
6801  {
6802  throw std::domain_error("cannot use key() for non-object iterators");
6803  }
6804  }
6805 
6806  /// return the value of an iterator
6808  {
6809  return operator*();
6810  }
6811 
6812  private:
6813  /// associated JSON instance
6814  pointer m_object = nullptr;
6815  /// the actual iterator of the associated instance
6817  };
6818 
6819  /*!
6820  @brief a mutable random access iterator for the @ref basic_json class
6821 
6822  @requirement The class satisfies the following concept requirements:
6823  - [RandomAccessIterator](http://en.cppreference.com/w/cpp/concept/RandomAccessIterator):
6824  The iterator that can be moved to point (forward and backward) to any
6825  element in constant time.
6826  - [OutputIterator](http://en.cppreference.com/w/cpp/concept/OutputIterator):
6827  It is possible to write to the pointed-to element.
6828 
6829  @since version 1.0.0
6830  */
6831  class iterator : public const_iterator
6832  {
6833  public:
6835  using pointer = typename basic_json::pointer;
6837 
6838  /// default constructor
6839  iterator() = default;
6840 
6841  /// constructor for a given JSON instance
6842  explicit iterator(pointer object) noexcept
6843  : base_iterator(object)
6844  {}
6845 
6846  /// copy constructor
6847  iterator(const iterator& other) noexcept
6848  : base_iterator(other)
6849  {}
6850 
6851  /// copy assignment
6852  iterator& operator=(iterator other) noexcept(
6857  )
6858  {
6859  base_iterator::operator=(other);
6860  return *this;
6861  }
6862 
6863  /// return a reference to the value pointed to by the iterator
6865  {
6866  return const_cast<reference>(base_iterator::operator*());
6867  }
6868 
6869  /// dereference the iterator
6871  {
6872  return const_cast<pointer>(base_iterator::operator->());
6873  }
6874 
6875  /// post-increment (it++)
6877  {
6878  iterator result = *this;
6880  return result;
6881  }
6882 
6883  /// pre-increment (++it)
6885  {
6887  return *this;
6888  }
6889 
6890  /// post-decrement (it--)
6892  {
6893  iterator result = *this;
6895  return result;
6896  }
6897 
6898  /// pre-decrement (--it)
6900  {
6902  return *this;
6903  }
6904 
6905  /// add to iterator
6907  {
6909  return *this;
6910  }
6911 
6912  /// subtract from iterator
6914  {
6916  return *this;
6917  }
6918 
6919  /// add to iterator
6921  {
6922  auto result = *this;
6923  result += i;
6924  return result;
6925  }
6926 
6927  /// subtract from iterator
6929  {
6930  auto result = *this;
6931  result -= i;
6932  return result;
6933  }
6934 
6935  /// return difference
6936  difference_type operator-(const iterator& other) const
6937  {
6938  return base_iterator::operator-(other);
6939  }
6940 
6941  /// access to successor
6943  {
6944  return const_cast<reference>(base_iterator::operator[](n));
6945  }
6946 
6947  /// return the value of an iterator
6949  {
6950  return const_cast<reference>(base_iterator::value());
6951  }
6952  };
6953 
6954  /*!
6955  @brief a template for a reverse iterator class
6956 
6957  @tparam Base the base iterator type to reverse. Valid types are @ref
6958  iterator (to create @ref reverse_iterator) and @ref const_iterator (to
6959  create @ref const_reverse_iterator).
6960 
6961  @requirement The class satisfies the following concept requirements:
6962  - [RandomAccessIterator](http://en.cppreference.com/w/cpp/concept/RandomAccessIterator):
6963  The iterator that can be moved to point (forward and backward) to any
6964  element in constant time.
6965  - [OutputIterator](http://en.cppreference.com/w/cpp/concept/OutputIterator):
6966  It is possible to write to the pointed-to element (only if @a Base is
6967  @ref iterator).
6968 
6969  @since version 1.0.0
6970  */
6971  template<typename Base>
6972  class json_reverse_iterator : public std::reverse_iterator<Base>
6973  {
6974  public:
6975  /// shortcut to the reverse iterator adaptor
6976  using base_iterator = std::reverse_iterator<Base>;
6977  /// the reference type for the pointed-to element
6978  using reference = typename Base::reference;
6979 
6980  /// create reverse iterator from iterator
6981  json_reverse_iterator(const typename base_iterator::iterator_type& it) noexcept
6982  : base_iterator(it)
6983  {}
6984 
6985  /// create reverse iterator from base class
6987  : base_iterator(it)
6988  {}
6989 
6990  /// post-increment (it++)
6992  {
6993  return base_iterator::operator++(1);
6994  }
6995 
6996  /// pre-increment (++it)
6998  {
6999  base_iterator::operator++();
7000  return *this;
7001  }
7002 
7003  /// post-decrement (it--)
7005  {
7006  return base_iterator::operator--(1);
7007  }
7008 
7009  /// pre-decrement (--it)
7011  {
7012  base_iterator::operator--();
7013  return *this;
7014  }
7015 
7016  /// add to iterator
7018  {
7019  base_iterator::operator+=(i);
7020  return *this;
7021  }
7022 
7023  /// add to iterator
7025  {
7026  auto result = *this;
7027  result += i;
7028  return result;
7029  }
7030 
7031  /// subtract from iterator
7033  {
7034  auto result = *this;
7035  result -= i;
7036  return result;
7037  }
7038 
7039  /// return difference
7041  {
7042  return this->base() - other.base();
7043  }
7044 
7045  /// access to successor
7047  {
7048  return *(this->operator+(n));
7049  }
7050 
7051  /// return the key of an object iterator
7052  typename object_t::key_type key() const
7053  {
7054  auto it = --this->base();
7055  return it.key();
7056  }
7057 
7058  /// return the value of an iterator
7060  {
7061  auto it = --this->base();
7062  return it.operator * ();
7063  }
7064  };
7065 
7066 
7067  private:
7068  //////////////////////
7069  // lexer and parser //
7070  //////////////////////
7071 
7072  /*!
7073  @brief lexical analysis
7074 
7075  This class organizes the lexical analysis during JSON deserialization. The
7076  core of it is a scanner generated by [re2c](http://re2c.org) that processes
7077  a buffer and recognizes tokens according to RFC 7159.
7078  */
7079  class lexer
7080  {
7081  public:
7082  /// token types for the parser
7083  enum class token_type
7084  {
7085  uninitialized, ///< indicating the scanner is uninitialized
7086  literal_true, ///< the "true" literal
7087  literal_false, ///< the "false" literal
7088  literal_null, ///< the "null" literal
7089  value_string, ///< a string -- use get_string() for actual value
7090  value_number, ///< a number -- use get_number() for actual value
7091  begin_array, ///< the character for array begin "["
7092  begin_object, ///< the character for object begin "{"
7093  end_array, ///< the character for array end "]"
7094  end_object, ///< the character for object end "}"
7095  name_separator, ///< the name separator ":"
7096  value_separator, ///< the value separator ","
7097  parse_error, ///< indicating a parse error
7098  end_of_input ///< indicating the end of the input buffer
7099  };
7100 
7101  /// the char type to use in the lexer
7102  using lexer_char_t = unsigned char;
7103 
7104  /// constructor with a given buffer
7105  explicit lexer(const string_t& s) noexcept
7106  : m_stream(nullptr), m_buffer(s)
7107  {
7108  m_content = reinterpret_cast<const lexer_char_t*>(s.c_str());
7109  assert(m_content != nullptr);
7111  m_limit = m_content + s.size();
7112  }
7113 
7114  /// constructor with a given stream
7115  explicit lexer(std::istream* s) noexcept
7116  : m_stream(s), m_buffer()
7117  {
7118  assert(m_stream != nullptr);
7119  getline(*m_stream, m_buffer);
7120  m_content = reinterpret_cast<const lexer_char_t*>(m_buffer.c_str());
7121  assert(m_content != nullptr);
7123  m_limit = m_content + m_buffer.size();
7124  }
7125 
7126  /// default constructor
7127  lexer() = default;
7128 
7129  // switch off unwanted functions
7130  lexer(const lexer&) = delete;
7131  lexer operator=(const lexer&) = delete;
7132 
7133  /*!
7134  @brief create a string from a Unicode code point
7135 
7136  @param[in] codepoint1 the code point (can be high surrogate)
7137  @param[in] codepoint2 the code point (can be low surrogate or 0)
7138 
7139  @return string representation of the code point
7140 
7141  @throw std::out_of_range if code point is >0x10ffff; example: `"code
7142  points above 0x10FFFF are invalid"`
7143  @throw std::invalid_argument if the low surrogate is invalid; example:
7144  `""missing or wrong low surrogate""`
7145 
7146  @see <http://en.wikipedia.org/wiki/UTF-8#Sample_code>
7147  */
7148  static string_t to_unicode(const std::size_t codepoint1,
7149  const std::size_t codepoint2 = 0)
7150  {
7151  // calculate the codepoint from the given code points
7152  std::size_t codepoint = codepoint1;
7153 
7154  // check if codepoint1 is a high surrogate
7155  if (codepoint1 >= 0xD800 and codepoint1 <= 0xDBFF)
7156  {
7157  // check if codepoint2 is a low surrogate
7158  if (codepoint2 >= 0xDC00 and codepoint2 <= 0xDFFF)
7159  {
7160  codepoint =
7161  // high surrogate occupies the most significant 22 bits
7162  (codepoint1 << 10)
7163  // low surrogate occupies the least significant 15 bits
7164  + codepoint2
7165  // there is still the 0xD800, 0xDC00 and 0x10000 noise
7166  // in the result so we have to subtract with:
7167  // (0xD800 << 10) + DC00 - 0x10000 = 0x35FDC00
7168  - 0x35FDC00;
7169  }
7170  else
7171  {
7172  throw std::invalid_argument("missing or wrong low surrogate");
7173  }
7174  }
7175 
7176  string_t result;
7177 
7178  if (codepoint < 0x80)
7179  {
7180  // 1-byte characters: 0xxxxxxx (ASCII)
7181  result.append(1, static_cast<typename string_t::value_type>(codepoint));
7182  }
7183  else if (codepoint <= 0x7ff)
7184  {
7185  // 2-byte characters: 110xxxxx 10xxxxxx
7186  result.append(1, static_cast<typename string_t::value_type>(0xC0 | ((codepoint >> 6) & 0x1F)));
7187  result.append(1, static_cast<typename string_t::value_type>(0x80 | (codepoint & 0x3F)));
7188  }
7189  else if (codepoint <= 0xffff)
7190  {
7191  // 3-byte characters: 1110xxxx 10xxxxxx 10xxxxxx
7192  result.append(1, static_cast<typename string_t::value_type>(0xE0 | ((codepoint >> 12) & 0x0F)));
7193  result.append(1, static_cast<typename string_t::value_type>(0x80 | ((codepoint >> 6) & 0x3F)));
7194  result.append(1, static_cast<typename string_t::value_type>(0x80 | (codepoint & 0x3F)));
7195  }
7196  else if (codepoint <= 0x10ffff)
7197  {
7198  // 4-byte characters: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
7199  result.append(1, static_cast<typename string_t::value_type>(0xF0 | ((codepoint >> 18) & 0x07)));
7200  result.append(1, static_cast<typename string_t::value_type>(0x80 | ((codepoint >> 12) & 0x3F)));
7201  result.append(1, static_cast<typename string_t::value_type>(0x80 | ((codepoint >> 6) & 0x3F)));
7202  result.append(1, static_cast<typename string_t::value_type>(0x80 | (codepoint & 0x3F)));
7203  }
7204  else
7205  {
7206  throw std::out_of_range("code points above 0x10FFFF are invalid");
7207  }
7208 
7209  return result;
7210  }
7211 
7212  /// return name of values of type token_type (only used for errors)
7213  static std::string token_type_name(token_type t)
7214  {
7215  switch (t)
7216  {
7218  return "<uninitialized>";
7220  return "true literal";
7222  return "false literal";
7224  return "null literal";
7226  return "string literal";
7228  return "number literal";
7230  return "'['";
7232  return "'{'";
7233  case token_type::end_array:
7234  return "']'";
7236  return "'}'";
7238  return "':'";
7240  return "','";
7242  return "<parse error>";
7244  return "end of input";
7245  default:
7246  {
7247  // catch non-enum values
7248  return "unknown token"; // LCOV_EXCL_LINE
7249  }
7250  }
7251  }
7252 
7253  /*!
7254  This function implements a scanner for JSON. It is specified using
7255  regular expressions that try to follow RFC 7159 as close as possible.
7256  These regular expressions are then translated into a minimized
7257  deterministic finite automaton (DFA) by the tool
7258  [re2c](http://re2c.org). As a result, the translated code for this
7259  function consists of a large block of code with `goto` jumps.
7260 
7261  @return the class of the next token read from the buffer
7262  */
7263  token_type scan() noexcept
7264  {
7265  // pointer for backtracking information
7266  m_marker = nullptr;
7267 
7268  // remember the begin of the token
7269  m_start = m_cursor;
7270  assert(m_start != nullptr);
7271 
7272 
7273  {
7274  lexer_char_t yych;
7275  unsigned int yyaccept = 0;
7276  static const unsigned char yybm[] =
7277  {
7278  0, 0, 0, 0, 0, 0, 0, 0,
7279  0, 32, 32, 0, 0, 32, 0, 0,
7280  128, 128, 128, 128, 128, 128, 128, 128,
7281  128, 128, 128, 128, 128, 128, 128, 128,
7282  160, 128, 0, 128, 128, 128, 128, 128,
7283  128, 128, 128, 128, 128, 128, 128, 128,
7284  192, 192, 192, 192, 192, 192, 192, 192,
7285  192, 192, 128, 128, 128, 128, 128, 128,
7286  128, 128, 128, 128, 128, 128, 128, 128,
7287  128, 128, 128, 128, 128, 128, 128, 128,
7288  128, 128, 128, 128, 128, 128, 128, 128,
7289  128, 128, 128, 128, 0, 128, 128, 128,
7290  128, 128, 128, 128, 128, 128, 128, 128,
7291  128, 128, 128, 128, 128, 128, 128, 128,
7292  128, 128, 128, 128, 128, 128, 128, 128,
7293  128, 128, 128, 128, 128, 128, 128, 128,
7294  128, 128, 128, 128, 128, 128, 128, 128,
7295  128, 128, 128, 128, 128, 128, 128, 128,
7296  128, 128, 128, 128, 128, 128, 128, 128,
7297  128, 128, 128, 128, 128, 128, 128, 128,
7298  128, 128, 128, 128, 128, 128, 128, 128,
7299  128, 128, 128, 128, 128, 128, 128, 128,
7300  128, 128, 128, 128, 128, 128, 128, 128,
7301  128, 128, 128, 128, 128, 128, 128, 128,
7302  128, 128, 128, 128, 128, 128, 128, 128,
7303  128, 128, 128, 128, 128, 128, 128, 128,
7304  128, 128, 128, 128, 128, 128, 128, 128,
7305  128, 128, 128, 128, 128, 128, 128, 128,
7306  128, 128, 128, 128, 128, 128, 128, 128,
7307  128, 128, 128, 128, 128, 128, 128, 128,
7308  128, 128, 128, 128, 128, 128, 128, 128,
7309  128, 128, 128, 128, 128, 128, 128, 128,
7310  };
7311  if ((m_limit - m_cursor) < 5)
7312  {
7313  yyfill(); // LCOV_EXCL_LINE;
7314  }
7315  yych = *m_cursor;
7316  if (yybm[0 + yych] & 32)
7317  {
7318  goto basic_json_parser_6;
7319  }
7320  if (yych <= '\\')
7321  {
7322  if (yych <= '-')
7323  {
7324  if (yych <= '"')
7325  {
7326  if (yych <= 0x00)
7327  {
7328  goto basic_json_parser_2;
7329  }
7330  if (yych <= '!')
7331  {
7332  goto basic_json_parser_4;
7333  }
7334  goto basic_json_parser_9;
7335  }
7336  else
7337  {
7338  if (yych <= '+')
7339  {
7340  goto basic_json_parser_4;
7341  }
7342  if (yych <= ',')
7343  {
7344  goto basic_json_parser_10;
7345  }
7346  goto basic_json_parser_12;
7347  }
7348  }
7349  else
7350  {
7351  if (yych <= '9')
7352  {
7353  if (yych <= '/')
7354  {
7355  goto basic_json_parser_4;
7356  }
7357  if (yych <= '0')
7358  {
7359  goto basic_json_parser_13;
7360  }
7361  goto basic_json_parser_15;
7362  }
7363  else
7364  {
7365  if (yych <= ':')
7366  {
7367  goto basic_json_parser_17;
7368  }
7369  if (yych == '[')
7370  {
7371  goto basic_json_parser_19;
7372  }
7373  goto basic_json_parser_4;
7374  }
7375  }
7376  }
7377  else
7378  {
7379  if (yych <= 't')
7380  {
7381  if (yych <= 'f')
7382  {
7383  if (yych <= ']')
7384  {
7385  goto basic_json_parser_21;
7386  }
7387  if (yych <= 'e')
7388  {
7389  goto basic_json_parser_4;
7390  }
7391  goto basic_json_parser_23;
7392  }
7393  else
7394  {
7395  if (yych == 'n')
7396  {
7397  goto basic_json_parser_24;
7398  }
7399  if (yych <= 's')
7400  {
7401  goto basic_json_parser_4;
7402  }
7403  goto basic_json_parser_25;
7404  }
7405  }
7406  else
7407  {
7408  if (yych <= '|')
7409  {
7410  if (yych == '{')
7411  {
7412  goto basic_json_parser_26;
7413  }
7414  goto basic_json_parser_4;
7415  }
7416  else
7417  {
7418  if (yych <= '}')
7419  {
7420  goto basic_json_parser_28;
7421  }
7422  if (yych == 0xEF)
7423  {
7424  goto basic_json_parser_30;
7425  }
7426  goto basic_json_parser_4;
7427  }
7428  }
7429  }
7430 basic_json_parser_2:
7431  ++m_cursor;
7432  {
7433  return token_type::end_of_input;
7434  }
7435 basic_json_parser_4:
7436  ++m_cursor;
7437 basic_json_parser_5:
7438  {
7439  return token_type::parse_error;
7440  }
7441 basic_json_parser_6:
7442  ++m_cursor;
7443  if (m_limit <= m_cursor)
7444  {
7445  yyfill(); // LCOV_EXCL_LINE;
7446  }
7447  yych = *m_cursor;
7448  if (yybm[0 + yych] & 32)
7449  {
7450  goto basic_json_parser_6;
7451  }
7452  {
7453  return scan();
7454  }
7455 basic_json_parser_9:
7456  yyaccept = 0;
7457  yych = *(m_marker = ++m_cursor);
7458  if (yych <= 0x0F)
7459  {
7460  goto basic_json_parser_5;
7461  }
7462  goto basic_json_parser_32;
7463 basic_json_parser_10:
7464  ++m_cursor;
7465  {
7467  }
7468 basic_json_parser_12:
7469  yych = *++m_cursor;
7470  if (yych <= '/')
7471  {
7472  goto basic_json_parser_5;
7473  }
7474  if (yych <= '0')
7475  {
7476  goto basic_json_parser_13;
7477  }
7478  if (yych <= '9')
7479  {
7480  goto basic_json_parser_15;
7481  }
7482  goto basic_json_parser_5;
7483 basic_json_parser_13:
7484  yyaccept = 1;
7485  yych = *(m_marker = ++m_cursor);
7486  if (yych <= 'D')
7487  {
7488  if (yych == '.')
7489  {
7490  goto basic_json_parser_37;
7491  }
7492  }
7493  else
7494  {
7495  if (yych <= 'E')
7496  {
7497  goto basic_json_parser_38;
7498  }
7499  if (yych == 'e')
7500  {
7501  goto basic_json_parser_38;
7502  }
7503  }
7504 basic_json_parser_14:
7505  {
7506  return token_type::value_number;
7507  }
7508 basic_json_parser_15:
7509  yyaccept = 1;
7510  m_marker = ++m_cursor;
7511  if ((m_limit - m_cursor) < 3)
7512  {
7513  yyfill(); // LCOV_EXCL_LINE;
7514  }
7515  yych = *m_cursor;
7516  if (yybm[0 + yych] & 64)
7517  {
7518  goto basic_json_parser_15;
7519  }
7520  if (yych <= 'D')
7521  {
7522  if (yych == '.')
7523  {
7524  goto basic_json_parser_37;
7525  }
7526  goto basic_json_parser_14;
7527  }
7528  else
7529  {
7530  if (yych <= 'E')
7531  {
7532  goto basic_json_parser_38;
7533  }
7534  if (yych == 'e')
7535  {
7536  goto basic_json_parser_38;
7537  }
7538  goto basic_json_parser_14;
7539  }
7540 basic_json_parser_17:
7541  ++m_cursor;
7542  {
7544  }
7545 basic_json_parser_19:
7546  ++m_cursor;
7547  {
7548  return token_type::begin_array;
7549  }
7550 basic_json_parser_21:
7551  ++m_cursor;
7552  {
7553  return token_type::end_array;
7554  }
7555 basic_json_parser_23:
7556  yyaccept = 0;
7557  yych = *(m_marker = ++m_cursor);
7558  if (yych == 'a')
7559  {
7560  goto basic_json_parser_39;
7561  }
7562  goto basic_json_parser_5;
7563 basic_json_parser_24:
7564  yyaccept = 0;
7565  yych = *(m_marker = ++m_cursor);
7566  if (yych == 'u')
7567  {
7568  goto basic_json_parser_40;
7569  }
7570  goto basic_json_parser_5;
7571 basic_json_parser_25:
7572  yyaccept = 0;
7573  yych = *(m_marker = ++m_cursor);
7574  if (yych == 'r')
7575  {
7576  goto basic_json_parser_41;
7577  }
7578  goto basic_json_parser_5;
7579 basic_json_parser_26:
7580  ++m_cursor;
7581  {
7582  return token_type::begin_object;
7583  }
7584 basic_json_parser_28:
7585  ++m_cursor;
7586  {
7587  return token_type::end_object;
7588  }
7589 basic_json_parser_30:
7590  yyaccept = 0;
7591  yych = *(m_marker = ++m_cursor);
7592  if (yych == 0xBB)
7593  {
7594  goto basic_json_parser_42;
7595  }
7596  goto basic_json_parser_5;
7597 basic_json_parser_31:
7598  ++m_cursor;
7599  if (m_limit <= m_cursor)
7600  {
7601  yyfill(); // LCOV_EXCL_LINE;
7602  }
7603  yych = *m_cursor;
7604 basic_json_parser_32:
7605  if (yybm[0 + yych] & 128)
7606  {
7607  goto basic_json_parser_31;
7608  }
7609  if (yych <= 0x0F)
7610  {
7611  goto basic_json_parser_33;
7612  }
7613  if (yych <= '"')
7614  {
7615  goto basic_json_parser_34;
7616  }
7617  goto basic_json_parser_36;
7618 basic_json_parser_33:
7619  m_cursor = m_marker;
7620  if (yyaccept == 0)
7621  {
7622  goto basic_json_parser_5;
7623  }
7624  else
7625  {
7626  goto basic_json_parser_14;
7627  }
7628 basic_json_parser_34:
7629  ++m_cursor;
7630  {
7631  return token_type::value_string;
7632  }
7633 basic_json_parser_36:
7634  ++m_cursor;
7635  if (m_limit <= m_cursor)
7636  {
7637  yyfill(); // LCOV_EXCL_LINE;
7638  }
7639  yych = *m_cursor;
7640  if (yych <= 'e')
7641  {
7642  if (yych <= '/')
7643  {
7644  if (yych == '"')
7645  {
7646  goto basic_json_parser_31;
7647  }
7648  if (yych <= '.')
7649  {
7650  goto basic_json_parser_33;
7651  }
7652  goto basic_json_parser_31;
7653  }
7654  else
7655  {
7656  if (yych <= '\\')
7657  {
7658  if (yych <= '[')
7659  {
7660  goto basic_json_parser_33;
7661  }
7662  goto basic_json_parser_31;
7663  }
7664  else
7665  {
7666  if (yych == 'b')
7667  {
7668  goto basic_json_parser_31;
7669  }
7670  goto basic_json_parser_33;
7671  }
7672  }
7673  }
7674  else
7675  {
7676  if (yych <= 'q')
7677  {
7678  if (yych <= 'f')
7679  {
7680  goto basic_json_parser_31;
7681  }
7682  if (yych == 'n')
7683  {
7684  goto basic_json_parser_31;
7685  }
7686  goto basic_json_parser_33;
7687  }
7688  else
7689  {
7690  if (yych <= 's')
7691  {
7692  if (yych <= 'r')
7693  {
7694  goto basic_json_parser_31;
7695  }
7696  goto basic_json_parser_33;
7697  }
7698  else
7699  {
7700  if (yych <= 't')
7701  {
7702  goto basic_json_parser_31;
7703  }
7704  if (yych <= 'u')
7705  {
7706  goto basic_json_parser_43;
7707  }
7708  goto basic_json_parser_33;
7709  }
7710  }
7711  }
7712 basic_json_parser_37:
7713  yych = *++m_cursor;
7714  if (yych <= '/')
7715  {
7716  goto basic_json_parser_33;
7717  }
7718  if (yych <= '9')
7719  {
7720  goto basic_json_parser_44;
7721  }
7722  goto basic_json_parser_33;
7723 basic_json_parser_38:
7724  yych = *++m_cursor;
7725  if (yych <= ',')
7726  {
7727  if (yych == '+')
7728  {
7729  goto basic_json_parser_46;
7730  }
7731  goto basic_json_parser_33;
7732  }
7733  else
7734  {
7735  if (yych <= '-')
7736  {
7737  goto basic_json_parser_46;
7738  }
7739  if (yych <= '/')
7740  {
7741  goto basic_json_parser_33;
7742  }
7743  if (yych <= '9')
7744  {
7745  goto basic_json_parser_47;
7746  }
7747  goto basic_json_parser_33;
7748  }
7749 basic_json_parser_39:
7750  yych = *++m_cursor;
7751  if (yych == 'l')
7752  {
7753  goto basic_json_parser_49;
7754  }
7755  goto basic_json_parser_33;
7756 basic_json_parser_40:
7757  yych = *++m_cursor;
7758  if (yych == 'l')
7759  {
7760  goto basic_json_parser_50;
7761  }
7762  goto basic_json_parser_33;
7763 basic_json_parser_41:
7764  yych = *++m_cursor;
7765  if (yych == 'u')
7766  {
7767  goto basic_json_parser_51;
7768  }
7769  goto basic_json_parser_33;
7770 basic_json_parser_42:
7771  yych = *++m_cursor;
7772  if (yych == 0xBF)
7773  {
7774  goto basic_json_parser_52;
7775  }
7776  goto basic_json_parser_33;
7777 basic_json_parser_43:
7778  ++m_cursor;
7779  if (m_limit <= m_cursor)
7780  {
7781  yyfill(); // LCOV_EXCL_LINE;
7782  }
7783  yych = *m_cursor;
7784  if (yych <= '@')
7785  {
7786  if (yych <= '/')
7787  {
7788  goto basic_json_parser_33;
7789  }
7790  if (yych <= '9')
7791  {
7792  goto basic_json_parser_54;
7793  }
7794  goto basic_json_parser_33;
7795  }
7796  else
7797  {
7798  if (yych <= 'F')
7799  {
7800  goto basic_json_parser_54;
7801  }
7802  if (yych <= '`')
7803  {
7804  goto basic_json_parser_33;
7805  }
7806  if (yych <= 'f')
7807  {
7808  goto basic_json_parser_54;
7809  }
7810  goto basic_json_parser_33;
7811  }
7812 basic_json_parser_44:
7813  yyaccept = 1;
7814  m_marker = ++m_cursor;
7815  if ((m_limit - m_cursor) < 3)
7816  {
7817  yyfill(); // LCOV_EXCL_LINE;
7818  }
7819  yych = *m_cursor;
7820  if (yych <= 'D')
7821  {
7822  if (yych <= '/')
7823  {
7824  goto basic_json_parser_14;
7825  }
7826  if (yych <= '9')
7827  {
7828  goto basic_json_parser_44;
7829  }
7830  goto basic_json_parser_14;
7831  }
7832  else
7833  {
7834  if (yych <= 'E')
7835  {
7836  goto basic_json_parser_38;
7837  }
7838  if (yych == 'e')
7839  {
7840  goto basic_json_parser_38;
7841  }
7842  goto basic_json_parser_14;
7843  }
7844 basic_json_parser_46:
7845  yych = *++m_cursor;
7846  if (yych <= '/')
7847  {
7848  goto basic_json_parser_33;
7849  }
7850  if (yych >= ':')
7851  {
7852  goto basic_json_parser_33;
7853  }
7854 basic_json_parser_47:
7855  ++m_cursor;
7856  if (m_limit <= m_cursor)
7857  {
7858  yyfill(); // LCOV_EXCL_LINE;
7859  }
7860  yych = *m_cursor;
7861  if (yych <= '/')
7862  {
7863  goto basic_json_parser_14;
7864  }
7865  if (yych <= '9')
7866  {
7867  goto basic_json_parser_47;
7868  }
7869  goto basic_json_parser_14;
7870 basic_json_parser_49:
7871  yych = *++m_cursor;
7872  if (yych == 's')
7873  {
7874  goto basic_json_parser_55;
7875  }
7876  goto basic_json_parser_33;
7877 basic_json_parser_50:
7878  yych = *++m_cursor;
7879  if (yych == 'l')
7880  {
7881  goto basic_json_parser_56;
7882  }
7883  goto basic_json_parser_33;
7884 basic_json_parser_51:
7885  yych = *++m_cursor;
7886  if (yych == 'e')
7887  {
7888  goto basic_json_parser_58;
7889  }
7890  goto basic_json_parser_33;
7891 basic_json_parser_52:
7892  ++m_cursor;
7893  {
7894  return scan();
7895  }
7896 basic_json_parser_54:
7897  ++m_cursor;
7898  if (m_limit <= m_cursor)
7899  {
7900  yyfill(); // LCOV_EXCL_LINE;
7901  }
7902  yych = *m_cursor;
7903  if (yych <= '@')
7904  {
7905  if (yych <= '/')
7906  {
7907  goto basic_json_parser_33;
7908  }
7909  if (yych <= '9')
7910  {
7911  goto basic_json_parser_60;
7912  }
7913  goto basic_json_parser_33;
7914  }
7915  else
7916  {
7917  if (yych <= 'F')
7918  {
7919  goto basic_json_parser_60;
7920  }
7921  if (yych <= '`')
7922  {
7923  goto basic_json_parser_33;
7924  }
7925  if (yych <= 'f')
7926  {
7927  goto basic_json_parser_60;
7928  }
7929  goto basic_json_parser_33;
7930  }
7931 basic_json_parser_55:
7932  yych = *++m_cursor;
7933  if (yych == 'e')
7934  {
7935  goto basic_json_parser_61;
7936  }
7937  goto basic_json_parser_33;
7938 basic_json_parser_56:
7939  ++m_cursor;
7940  {
7941  return token_type::literal_null;
7942  }
7943 basic_json_parser_58:
7944  ++m_cursor;
7945  {
7946  return token_type::literal_true;
7947  }
7948 basic_json_parser_60:
7949  ++m_cursor;
7950  if (m_limit <= m_cursor)
7951  {
7952  yyfill(); // LCOV_EXCL_LINE;
7953  }
7954  yych = *m_cursor;
7955  if (yych <= '@')
7956  {
7957  if (yych <= '/')
7958  {
7959  goto basic_json_parser_33;
7960  }
7961  if (yych <= '9')
7962  {
7963  goto basic_json_parser_63;
7964  }
7965  goto basic_json_parser_33;
7966  }
7967  else
7968  {
7969  if (yych <= 'F')
7970  {
7971  goto basic_json_parser_63;
7972  }
7973  if (yych <= '`')
7974  {
7975  goto basic_json_parser_33;
7976  }
7977  if (yych <= 'f')
7978  {
7979  goto basic_json_parser_63;
7980  }
7981  goto basic_json_parser_33;
7982  }
7983 basic_json_parser_61:
7984  ++m_cursor;
7985  {
7987  }
7988 basic_json_parser_63:
7989  ++m_cursor;
7990  if (m_limit <= m_cursor)
7991  {
7992  yyfill(); // LCOV_EXCL_LINE;
7993  }
7994  yych = *m_cursor;
7995  if (yych <= '@')
7996  {
7997  if (yych <= '/')
7998  {
7999  goto basic_json_parser_33;
8000  }
8001  if (yych <= '9')
8002  {
8003  goto basic_json_parser_31;
8004  }
8005  goto basic_json_parser_33;
8006  }
8007  else
8008  {
8009  if (yych <= 'F')
8010  {
8011  goto basic_json_parser_31;
8012  }
8013  if (yych <= '`')
8014  {
8015  goto basic_json_parser_33;
8016  }
8017  if (yych <= 'f')
8018  {
8019  goto basic_json_parser_31;
8020  }
8021  goto basic_json_parser_33;
8022  }
8023  }
8024 
8025  }
8026 
8027  /// append data from the stream to the internal buffer
8028  void yyfill() noexcept
8029  {
8030  if (m_stream == nullptr or not * m_stream)
8031  {
8032  return;
8033  }
8034 
8035  const auto offset_start = m_start - m_content;
8036  const auto offset_marker = m_marker - m_start;
8037  const auto offset_cursor = m_cursor - m_start;
8038 
8039  m_buffer.erase(0, static_cast<size_t>(offset_start));
8040  std::string line;
8041  assert(m_stream != nullptr);
8042  std::getline(*m_stream, line);
8043  m_buffer += "\n" + line; // add line with newline symbol
8044 
8045  m_content = reinterpret_cast<const lexer_char_t*>(m_buffer.c_str());
8046  assert(m_content != nullptr);
8047  m_start = m_content;
8048  m_marker = m_start + offset_marker;
8049  m_cursor = m_start + offset_cursor;
8050  m_limit = m_start + m_buffer.size() - 1;
8051  }
8052 
8053  /// return string representation of last read token
8055  {
8056  assert(m_start != nullptr);
8057  return string_t(reinterpret_cast<typename string_t::const_pointer>(m_start),
8058  static_cast<size_t>(m_cursor - m_start));
8059  }
8060 
8061  /*!
8062  @brief return string value for string tokens
8063 
8064  The function iterates the characters between the opening and closing
8065  quotes of the string value. The complete string is the range
8066  [m_start,m_cursor). Consequently, we iterate from m_start+1 to
8067  m_cursor-1.
8068 
8069  We differentiate two cases:
8070 
8071  1. Escaped characters. In this case, a new character is constructed
8072  according to the nature of the escape. Some escapes create new
8073  characters (e.g., `"\\n"` is replaced by `"\n"`), some are copied as
8074  is (e.g., `"\\\\"`). Furthermore, Unicode escapes of the shape
8075  `"\\uxxxx"` need special care. In this case, to_unicode takes care
8076  of the construction of the values.
8077  2. Unescaped characters are copied as is.
8078 
8079  @return string value of current token without opening and closing quotes
8080  @throw std::out_of_range if to_unicode fails
8081  */
8083  {
8084  string_t result;
8085  result.reserve(static_cast<size_t>(m_cursor - m_start - 2));
8086 
8087  // iterate the result between the quotes
8088  for (const lexer_char_t* i = m_start + 1; i < m_cursor - 1; ++i)
8089  {
8090  // process escaped characters
8091  if (*i == '\\')
8092  {
8093  // read next character
8094  ++i;
8095 
8096  switch (*i)
8097  {
8098  // the default escapes
8099  case 't':
8100  {
8101  result += "\t";
8102  break;
8103  }
8104  case 'b':
8105  {
8106  result += "\b";
8107  break;
8108  }
8109  case 'f':
8110  {
8111  result += "\f";
8112  break;
8113  }
8114  case 'n':
8115  {
8116  result += "\n";
8117  break;
8118  }
8119  case 'r':
8120  {
8121  result += "\r";
8122  break;
8123  }
8124  case '\\':
8125  {
8126  result += "\\";
8127  break;
8128  }
8129  case '/':
8130  {
8131  result += "/";
8132  break;
8133  }
8134  case '"':
8135  {
8136  result += "\"";
8137  break;
8138  }
8139 
8140  // unicode
8141  case 'u':
8142  {
8143  // get code xxxx from uxxxx
8144  auto codepoint = std::strtoul(std::string(reinterpret_cast<typename string_t::const_pointer>(i + 1),
8145  4).c_str(), nullptr, 16);
8146 
8147  // check if codepoint is a high surrogate
8148  if (codepoint >= 0xD800 and codepoint <= 0xDBFF)
8149  {
8150  // make sure there is a subsequent unicode
8151  if ((i + 6 >= m_limit) or * (i + 5) != '\\' or * (i + 6) != 'u')
8152  {
8153  throw std::invalid_argument("missing low surrogate");
8154  }
8155 
8156  // get code yyyy from uxxxx\uyyyy
8157  auto codepoint2 = std::strtoul(std::string(reinterpret_cast<typename string_t::const_pointer>
8158  (i + 7), 4).c_str(), nullptr, 16);
8159  result += to_unicode(codepoint, codepoint2);
8160  // skip the next 10 characters (xxxx\uyyyy)
8161  i += 10;
8162  }
8163  else
8164  {
8165  // add unicode character(s)
8166  result += to_unicode(codepoint);
8167  // skip the next four characters (xxxx)
8168  i += 4;
8169  }
8170  break;
8171  }
8172  }
8173  }
8174  else
8175  {
8176  // all other characters are just copied to the end of the
8177  // string
8178  result.append(1, static_cast<typename string_t::value_type>(*i));
8179  }
8180  }
8181 
8182  return result;
8183  }
8184 
8185  /*!
8186  @brief parse floating point number
8187 
8188  This function (and its overloads) serves to select the most approprate
8189  standard floating point number parsing function based on the type
8190  supplied via the first parameter. Set this to
8191  @a static_cast<number_float_t*>(nullptr).
8192 
8193  @param[in] type the @ref number_float_t in use
8194 
8195  @param[in,out] endptr recieves a pointer to the first character after
8196  the number
8197 
8198  @return the floating point number
8199 
8200  @bug This function uses `std::strtof`, `std::strtod`, or `std::strtold`
8201  which use the current C locale to determine which character is used as
8202  decimal point character. This may yield to parse errors if the locale
8203  does not used `.`.
8204  */
8205  long double str_to_float_t(long double* /* type */, char** endptr) const
8206  {
8207  return std::strtold(reinterpret_cast<typename string_t::const_pointer>(m_start), endptr);
8208  }
8209 
8210  /*!
8211  @brief parse floating point number
8212 
8213  This function (and its overloads) serves to select the most approprate
8214  standard floating point number parsing function based on the type
8215  supplied via the first parameter. Set this to
8216  @a static_cast<number_float_t*>(nullptr).
8217 
8218  @param[in] type the @ref number_float_t in use
8219 
8220  @param[in,out] endptr recieves a pointer to the first character after
8221  the number
8222 
8223  @return the floating point number
8224  */
8225  double str_to_float_t(double* /* type */, char** endptr) const
8226  {
8227  return std::strtod(reinterpret_cast<typename string_t::const_pointer>(m_start), endptr);
8228  }
8229 
8230  /*!
8231  @brief parse floating point number
8232 
8233  This function (and its overloads) serves to select the most approprate
8234  standard floating point number parsing function based on the type
8235  supplied via the first parameter. Set this to
8236  @a static_cast<number_float_t*>(nullptr).
8237 
8238  @param[in] type the @ref number_float_t in use
8239 
8240  @param[in,out] endptr recieves a pointer to the first character after
8241  the number
8242 
8243  @return the floating point number
8244  */
8245  float str_to_float_t(float* /* type */, char** endptr) const
8246  {
8247  return std::strtof(reinterpret_cast<typename string_t::const_pointer>(m_start), endptr);
8248  }
8249 
8250  /*!
8251  @brief static_cast between two types and indicate if it results in error
8252 
8253  This function performs a `static_cast` between @a source and @a dest.
8254  It then checks if a `static_cast` back to @a dest produces an error.
8255 
8256  @param[in] source the value to cast from
8257 
8258  @param[in, out] dest the value to cast to
8259 
8260  @return true iff the cast was performed without error
8261  */
8262  template <typename T_A, typename T_B>
8263  static bool attempt_cast(T_A source, T_B& dest)
8264  {
8265  dest = static_cast<T_B>(source);
8266  return (source == static_cast<T_A>(dest));
8267  }
8268 
8269  /*!
8270  @brief return number value for number tokens
8271 
8272  This function translates the last token into the most appropriate
8273  number type (either integer, unsigned integer or floating point), which
8274  is passed back to the caller via the result parameter. The pointer @a
8275  m_start points to the beginning of the parsed number. We first examine
8276  the first character to determine the sign of the number and then pass
8277  this pointer to either @a std::strtoull (if positive) or @a
8278  std::strtoll (if negative), both of which set @a endptr to the first
8279  character past the converted number. If this pointer is not the same as
8280  @a m_cursor, then either more or less characters have been used during
8281  the comparison.
8282 
8283  This can happen for inputs like "01" which will be treated like number
8284  0 followed by number 1. This will also occur for valid floating point
8285  inputs like "12e3" will be incorrectly read as 12. Numbers that are too
8286  large or too small for a signed/unsigned long long will cause a range
8287  error (@a errno set to ERANGE). The parsed number is cast to a @ref
8288  number_integer_t/@ref number_unsigned_t using the helper function @ref
8289  attempt_cast, which returns @a false if the cast could not be peformed
8290  without error.
8291 
8292  In any of these cases (more/less characters read, range error or a cast
8293  error) the pointer is passed to @a std:strtod, which also sets @a
8294  endptr to the first character past the converted number. The resulting
8295  @ref number_float_t is then cast to a @ref number_integer_t/@ref
8296  number_unsigned_t using @ref attempt_cast and if no error occurs is
8297  stored in that form, otherwise it is stored as a @ref number_float_t.
8298 
8299  A final comparison is made of @a endptr and if still not the same as
8300  @ref m_cursor a bad input is assumed and @a result parameter is set to
8301  NAN.
8302 
8303  @param[out] result @ref basic_json object to receive the number, or NAN
8304  if the conversion read past the current token. The latter case needs to
8305  be treated by the caller function.
8306  */
8307  void get_number(basic_json& result) const
8308  {
8309  typename string_t::value_type* endptr;
8310  assert(m_start != nullptr);
8311  errno = 0;
8312 
8313  // attempt to parse it as an integer - first checking for a
8314  // negative number
8315  if (*reinterpret_cast<typename string_t::const_pointer>(m_start) != '-')
8316  {
8317  // positive, parse with strtoull and attempt cast to
8318  // number_unsigned_t
8319  if (attempt_cast(std::strtoull(reinterpret_cast<typename string_t::const_pointer>(m_start), &endptr,
8320  10), result.m_value.number_unsigned))
8321  {
8323  }
8324  else
8325  {
8326  // cast failed due to overflow - store as float
8327  result.m_type = value_t::number_float;
8328  }
8329  }
8330  else
8331  {
8332  // Negative, parse with strtoll and attempt cast to
8333  // number_integer_t
8334  if (attempt_cast(std::strtoll(reinterpret_cast<typename string_t::const_pointer>(m_start), &endptr,
8335  10), result.m_value.number_integer))
8336  {
8338  }
8339  else
8340  {
8341  // cast failed due to overflow - store as float
8342  result.m_type = value_t::number_float;
8343  }
8344  }
8345 
8346  // check the end of the number was reached and no range error
8347  // occurred
8348  if (reinterpret_cast<lexer_char_t*>(endptr) != m_cursor || errno == ERANGE)
8349  {
8350  result.m_type = value_t::number_float;
8351  }
8352 
8353  if (result.m_type == value_t::number_float)
8354  {
8355  // either the number won't fit in an integer (range error from
8356  // strtoull/strtoll or overflow on cast) or there was something
8357  // else after the number, which could be an exponent
8358 
8359  // parse with strtod
8360  result.m_value.number_float = str_to_float_t(static_cast<number_float_t*>(nullptr), &endptr);
8361 
8362  // anything after the number is an error
8363  if (reinterpret_cast<lexer_char_t*>(endptr) != m_cursor)
8364  {
8365  throw std::invalid_argument(std::string("parse error - ") + get_token() + " is not a number");
8366  }
8367  }
8368  }
8369 
8370  private:
8371  /// optional input stream
8372  std::istream* m_stream = nullptr;
8373  /// the buffer
8375  /// the buffer pointer
8376  const lexer_char_t* m_content = nullptr;
8377  /// pointer to the beginning of the current symbol
8378  const lexer_char_t* m_start = nullptr;
8379  /// pointer for backtracking information
8380  const lexer_char_t* m_marker = nullptr;
8381  /// pointer to the current symbol
8382  const lexer_char_t* m_cursor = nullptr;
8383  /// pointer to the end of the buffer
8384  const lexer_char_t* m_limit = nullptr;
8385  };
8386 
8387  /*!
8388  @brief syntax analysis
8389 
8390  This class implements a recursive decent parser.
8391  */
8392  class parser
8393  {
8394  public:
8395  /// constructor for strings
8396  parser(const string_t& s, parser_callback_t cb = nullptr) noexcept
8397  : callback(cb), m_lexer(s)
8398  {
8399  // read first token
8400  get_token();
8401  }
8402 
8403  /// a parser reading from an input stream
8404  parser(std::istream& _is, parser_callback_t cb = nullptr) noexcept
8405  : callback(cb), m_lexer(&_is)
8406  {
8407  // read first token
8408  get_token();
8409  }
8410 
8411  /// public parser interface
8413  {
8414  basic_json result = parse_internal(true);
8415 
8417 
8418  // return parser result and replace it with null in case the
8419  // top-level value was discarded by the callback function
8420  return result.is_discarded() ? basic_json() : result;
8421  }
8422 
8423  private:
8424  /// the actual parser
8426  {
8427  auto result = basic_json(value_t::discarded);
8428 
8429  switch (last_token)
8430  {
8432  {
8433  if (keep and (not callback or (keep = callback(depth++, parse_event_t::object_start, result))))
8434  {
8435  // explicitly set result to object to cope with {}
8436  result.m_type = value_t::object;
8437  result.m_value = json_value(value_t::object);
8438  }
8439 
8440  // read next token
8441  get_token();
8442 
8443  // closing } -> we are done
8445  {
8446  get_token();
8447  if (keep and callback and not callback(--depth, parse_event_t::object_end, result))
8448  {
8449  result = basic_json(value_t::discarded);
8450  }
8451  return result;
8452  }
8453 
8454  // no comma is expected here
8456 
8457  // otherwise: parse key-value pairs
8458  do
8459  {
8460  // ugly, but could be fixed with loop reorganization
8462  {
8463  get_token();
8464  }
8465 
8466  // store key
8468  const auto key = m_lexer.get_string();
8469 
8470  bool keep_tag = false;
8471  if (keep)
8472  {
8473  if (callback)
8474  {
8475  basic_json k(key);
8476  keep_tag = callback(depth, parse_event_t::key, k);
8477  }
8478  else
8479  {
8480  keep_tag = true;
8481  }
8482  }
8483 
8484  // parse separator (:)
8485  get_token();
8487 
8488  // parse and add value
8489  get_token();
8490  auto value = parse_internal(keep);
8491  if (keep and keep_tag and not value.is_discarded())
8492  {
8493  result[key] = std::move(value);
8494  }
8495  }
8497 
8498  // closing }
8500  get_token();
8501  if (keep and callback and not callback(--depth, parse_event_t::object_end, result))
8502  {
8503  result = basic_json(value_t::discarded);
8504  }
8505 
8506  return result;
8507  }
8508 
8510  {
8511  if (keep and (not callback or (keep = callback(depth++, parse_event_t::array_start, result))))
8512  {
8513  // explicitly set result to object to cope with []
8514  result.m_type = value_t::array;
8515  result.m_value = json_value(value_t::array);
8516  }
8517 
8518  // read next token
8519  get_token();
8520 
8521  // closing ] -> we are done
8523  {
8524  get_token();
8525  if (callback and not callback(--depth, parse_event_t::array_end, result))
8526  {
8527  result = basic_json(value_t::discarded);
8528  }
8529  return result;
8530  }
8531 
8532  // no comma is expected here
8534 
8535  // otherwise: parse values
8536  do
8537  {
8538  // ugly, but could be fixed with loop reorganization
8540  {
8541  get_token();
8542  }
8543 
8544  // parse value
8545  auto value = parse_internal(keep);
8546  if (keep and not value.is_discarded())
8547  {
8548  result.push_back(std::move(value));
8549  }
8550  }
8552 
8553  // closing ]
8555  get_token();
8556  if (keep and callback and not callback(--depth, parse_event_t::array_end, result))
8557  {
8558  result = basic_json(value_t::discarded);
8559  }
8560 
8561  return result;
8562  }
8563 
8565  {
8566  get_token();
8567  result.m_type = value_t::null;
8568  break;
8569  }
8570 
8572  {
8573  const auto s = m_lexer.get_string();
8574  get_token();
8575  result = basic_json(s);
8576  break;
8577  }
8578 
8580  {
8581  get_token();
8582  result.m_type = value_t::boolean;
8583  result.m_value = true;
8584  break;
8585  }
8586 
8588  {
8589  get_token();
8590  result.m_type = value_t::boolean;
8591  result.m_value = false;
8592  break;
8593  }
8594 
8596  {
8597  m_lexer.get_number(result);
8598  get_token();
8599  break;
8600  }
8601 
8602  default:
8603  {
8604  // the last token was unexpected
8606  }
8607  }
8608 
8609  if (keep and callback and not callback(depth, parse_event_t::value, result))
8610  {
8611  result = basic_json(value_t::discarded);
8612  }
8613  return result;
8614  }
8615 
8616  /// get next token from lexer
8617  typename lexer::token_type get_token() noexcept
8618  {
8619  last_token = m_lexer.scan();
8620  return last_token;
8621  }
8622 
8623  void expect(typename lexer::token_type t) const
8624  {
8625  if (t != last_token)
8626  {
8627  std::string error_msg = "parse error - unexpected ";
8628  error_msg += (last_token == lexer::token_type::parse_error ? ("'" + m_lexer.get_token() + "'") :
8630  error_msg += "; expected " + lexer::token_type_name(t);
8631  throw std::invalid_argument(error_msg);
8632  }
8633  }
8634 
8635  void unexpect(typename lexer::token_type t) const
8636  {
8637  if (t == last_token)
8638  {
8639  std::string error_msg = "parse error - unexpected ";
8640  error_msg += (last_token == lexer::token_type::parse_error ? ("'" + m_lexer.get_token() + "'") :
8642  throw std::invalid_argument(error_msg);
8643  }
8644  }
8645 
8646  private:
8647  /// current level of recursion
8648  int depth = 0;
8649  /// callback function
8651  /// the type of the last read token
8653  /// the lexer
8655  };
8656 };
8657 
8658 
8659 /////////////
8660 // presets //
8661 /////////////
8662 
8663 /*!
8664 @brief default JSON class
8665 
8666 This type is the default specialization of the @ref basic_json class which uses
8667 the standard template types.
8668 
8669 @since version 1.0.0
8670 */
8672 }
8673 
8674 
8675 /////////////////////////
8676 // nonmember functions //
8677 /////////////////////////
8678 
8679 // specialization of std::swap, and std::hash
8680 namespace std
8681 {
8682 /*!
8683 @brief exchanges the values of two JSON objects
8684 
8685 @since version 1.0.0
8686 */
8687 template <>
8688 inline void swap(nlohmann::json& j1,
8689  nlohmann::json& j2) noexcept(
8692  )
8693 {
8694  j1.swap(j2);
8695 }
8696 
8697 /// hash value for JSON objects
8698 template <>
8699 struct hash<nlohmann::json>
8700 {
8701  /*!
8702  @brief return a hash value for a JSON object
8703 
8704  @since version 1.0.0
8705  */
8706  std::size_t operator()(const nlohmann::json& j) const
8707  {
8708  // a naive hashing via the string representation
8709  const auto& h = hash<nlohmann::json::string_t>();
8710  return h(j.dump());
8711  }
8712 };
8713 }
8714 
8715 /*!
8716 @brief user-defined string literal for JSON values
8717 
8718 This operator implements a user-defined string literal for JSON objects. It can
8719 be used by adding \p "_json" to a string literal and returns a JSON object if
8720 no parse error occurred.
8721 
8722 @param[in] s a string representation of a JSON object
8723 @return a JSON object
8724 
8725 @since version 1.0.0
8726 */
8727 inline nlohmann::json operator "" _json(const char* s, std::size_t)
8728 {
8729  return nlohmann::json::parse(reinterpret_cast<const nlohmann::json::string_t::value_type*>(s));
8730 }
8731 
8732 // restore GCC/clang diagnostic settings
8733 #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
8734  #pragma GCC diagnostic pop
8735 #endif
8736 
8737 #endif
nlohmann::basic_json::at
const_reference at(size_type idx) const
access specified array element with bounds checking
Definition: json.hpp:3063
nlohmann::basic_json::parse_event_t::value
@ value
the parser finished reading a JSON value
nlohmann::basic_json::iterator::operator=
iterator & operator=(iterator other) noexcept(std::is_nothrow_move_constructible< pointer >::value and std::is_nothrow_move_assignable< pointer >::value and std::is_nothrow_move_constructible< internal_iterator >::value and std::is_nothrow_move_assignable< internal_iterator >::value)
copy assignment
Definition: json.hpp:6852
nlohmann::basic_json::const_iterator::operator-=
const_iterator & operator-=(difference_type i)
subtract from iterator
Definition: json.hpp:6710
nlohmann::basic_json::lexer::token_type::literal_false
@ literal_false
the "false" literal
nlohmann::basic_json::get
constexpr const PointerType get() const noexcept
get a pointer value (explicit)
Definition: json.hpp:2838
nlohmann::basic_json::basic_json
basic_json(const value_t value_type)
create an empty value with a given type
Definition: json.hpp:950
nlohmann::basic_json::lexer::m_limit
const lexer_char_t * m_limit
pointer to the end of the buffer
Definition: json.hpp:8384
nlohmann::basic_json::erase
InteratorType erase(InteratorType first, InteratorType last)
remove elements given an iterator range
Definition: json.hpp:3841
nlohmann::basic_json::get_ref
ReferenceType get_ref()
get a reference value (implicit)
Definition: json.hpp:2924
nlohmann::basic_json::get_impl
array_t get_impl(array_t *) const
get an array (explicit)
Definition: json.hpp:2564
nlohmann::basic_json::iterator::iterator
iterator(const iterator &other) noexcept
copy constructor
Definition: json.hpp:6847
nlohmann::basic_json::parse_event_t::object_end
@ object_end
the parser read } and finished processing a JSON object
nlohmann::basic_json::const_iterator::pointer
typename basic_json::const_pointer pointer
defines a pointer to the type iterated over (value_type)
Definition: json.hpp:6298
nlohmann::basic_json::iteration_proxy::iteration_proxy_internal::value
IteratorType::reference value() const
return value of the iterator
Definition: json.hpp:6245
nlohmann::basic_json::front
const_reference front() const
access the first element
Definition: json.hpp:3631
nlohmann::basic_json::insert
iterator insert(const_iterator pos, basic_json &&val)
inserts element
Definition: json.hpp:4851
nlohmann::basic_json::parse
static basic_json parse(std::istream &i, parser_callback_t cb=nullptr)
deserialize from stream
Definition: json.hpp:5667
nlohmann::basic_json::is_discarded
constexpr bool is_discarded() const noexcept
return whether value is discarded
Definition: json.hpp:2422
nlohmann::basic_json::lexer::str_to_float_t
float str_to_float_t(float *, char **endptr) const
parse floating point number
Definition: json.hpp:8245
nlohmann::basic_json::primitive_iterator_t::is_end
constexpr bool is_end() const noexcept
return whether the iterator is at end
Definition: json.hpp:6130
nlohmann::basic_json::json_reverse_iterator::base_iterator
std::reverse_iterator< Base > base_iterator
shortcut to the reverse iterator adaptor
Definition: json.hpp:6976
nlohmann::basic_json::json_reverse_iterator::operator+=
json_reverse_iterator & operator+=(difference_type i)
add to iterator
Definition: json.hpp:7017
nlohmann::basic_json::lexer::to_unicode
static string_t to_unicode(const std::size_t codepoint1, const std::size_t codepoint2=0)
create a string from a Unicode code point
Definition: json.hpp:7148
nlohmann::basic_json::const_iterator::operator!=
bool operator!=(const const_iterator &other) const
comparison: not equal
Definition: json.hpp:6628
nlohmann::basic_json::count
size_type count(typename object_t::key_type key) const
returns the number of occurrences of a key in a JSON object
Definition: json.hpp:4060
nlohmann::basic_json::string_t
StringType string_t
a type for a string
Definition: json.hpp:426
nlohmann::basic_json::lexer::token_type::parse_error
@ parse_error
indicating a parse error
nlohmann::basic_json::get_impl_ptr
constexpr const number_unsigned_t * get_impl_ptr(const number_unsigned_t *) const noexcept
get a pointer to the value (unsigned number)
Definition: json.hpp:2701
nlohmann::basic_json::front
reference front()
access the first element
Definition: json.hpp:3623
nlohmann::basic_json::insert
iterator insert(const_iterator pos, size_type cnt, const basic_json &val)
inserts elements
Definition: json.hpp:4880
nlohmann::basic_json::iteration_proxy::begin
iteration_proxy_internal begin() noexcept
return iterator begin (needed for range-based for)
Definition: json.hpp:6261
nlohmann::basic_json::basic_json
basic_json(const number_float_t val) noexcept
create a floating-point number (explicit)
Definition: json.hpp:1416
nlohmann::basic_json::iterator::operator*
reference operator*()
return a reference to the value pointed to by the iterator
Definition: json.hpp:6864
nlohmann::basic_json::value_t::discarded
@ discarded
discarded by the the parser callback function
nlohmann::basic_json::json_value::json_value
json_value(value_t t)
constructor for empty values of a given type
Definition: json.hpp:750
nlohmann::basic_json::iteration_proxy::iteration_proxy_internal::operator!=
bool operator!=(const iteration_proxy_internal &o) const
inequality operator (needed for range-based for)
Definition: json.hpp:6212
nlohmann::basic_json::iterator::iterator
iterator(pointer object) noexcept
constructor for a given JSON instance
Definition: json.hpp:6842
nlohmann::basic_json::json_reverse_iterator::key
object_t::key_type key() const
return the key of an object iterator
Definition: json.hpp:7052
nlohmann::basic_json::iteration_proxy::iteration_proxy_internal::key
basic_json::string_t key() const
return key of the iterator
Definition: json.hpp:6218
nlohmann::basic_json::operator[]
const_reference operator[](T *key) const
read-only access specified object element
Definition: json.hpp:3501
nlohmann::basic_json::const_iterator::operator+=
const_iterator & operator+=(difference_type i)
add to iterator
Definition: json.hpp:6682
nlohmann::basic_json::const_reference
const value_type & const_reference
the type of an element const reference
Definition: json.hpp:201
nlohmann::basic_json::operator[]
reference operator[](T *(&key)[n])
access specified object element
Definition: json.hpp:3384
nlohmann::basic_json::get_impl_ptr
boolean_t * get_impl_ptr(boolean_t *) noexcept
get a pointer to the value (boolean)
Definition: json.hpp:2671
nlohmann::basic_json::const_iterator::operator=
const_iterator & operator=(const_iterator other) noexcept(std::is_nothrow_move_constructible< pointer >::value and std::is_nothrow_move_assignable< pointer >::value and std::is_nothrow_move_constructible< internal_iterator >::value and std::is_nothrow_move_assignable< internal_iterator >::value)
copy assignment
Definition: json.hpp:6369
nlohmann::basic_json::const_iterator::operator--
const_iterator operator--(int)
post-decrement (it–)
Definition: json.hpp:6561
nlohmann::basic_json::iteration_proxy::end
iteration_proxy_internal end() noexcept
return iterator end (needed for range-based for)
Definition: json.hpp:6267
nlohmann::basic_json::const_iterator::const_iterator
const_iterator(const iterator &other) noexcept
copy constructor given a nonconst iterator
Definition: json.hpp:6336
nlohmann::basic_json::push_back
void push_back(const typename object_t::value_type &val)
add an object to an object
Definition: json.hpp:4772
nlohmann::basic_json::lexer::token_type::end_array
@ end_array
the character for array end "]"
nlohmann::basic_json::const_iterator::operator+
const_iterator operator+(difference_type i)
add to iterator
Definition: json.hpp:6716
std::swap
void swap(nlohmann::json &j1, nlohmann::json &j2) noexcept(is_nothrow_move_constructible< nlohmann::json >::value and is_nothrow_move_assignable< nlohmann::json >::value)
exchanges the values of two JSON objects
Definition: json.hpp:8688
nlohmann::basic_json::number_unsigned_t
NumberUnsignedType number_unsigned_t
a type for a number (unsigned)
Definition: json.hpp:595
fmt::to_string
std::string to_string(const T &value)
Definition: string.h:29
nlohmann::basic_json::clear
void clear() noexcept
clears the contents
Definition: json.hpp:4610
nlohmann::basic_json::json_reverse_iterator::operator-
difference_type operator-(const json_reverse_iterator &other) const
return difference
Definition: json.hpp:7040
nlohmann::basic_json::get_ptr
PointerType get_ptr() noexcept
get a pointer value (implicit)
Definition: json.hpp:2873
nlohmann::basic_json::lexer::get_token
string_t get_token() const
return string representation of last read token
Definition: json.hpp:8054
nlohmann::basic_json::lexer::token_type::end_object
@ end_object
the character for object end "}"
nlohmann::basic_json::type
constexpr value_t type() const noexcept
return the type of the JSON value (explicit)
Definition: json.hpp:2113
nlohmann::basic_json::json_value::number_float
number_float_t number_float
number (floating-point)
Definition: json.hpp:737
nlohmann::basic_json::const_iterator::const_iterator
const_iterator(pointer object) noexcept
constructor for a given JSON instance
Definition: json.hpp:6308
nlohmann::basic_json::end
iterator end() noexcept
returns an iterator to one past the last element
Definition: json.hpp:4172
nlohmann::basic_json::operator!=
friend bool operator!=(std::nullptr_t, const_reference v) noexcept
comparison: not equal
Definition: json.hpp:5370
nlohmann::basic_json::value_t::array
@ array
array (ordered collection of values)
nlohmann::basic_json::operator<
friend bool operator<(const value_t lhs, const value_t rhs) noexcept
comparison operator for JSON types
Definition: json.hpp:5162
nlohmann::basic_json::basic_json
basic_json(const array_t &val)
create an array (explicit)
Definition: json.hpp:1087
nlohmann::basic_json::parser::parse_internal
basic_json parse_internal(bool keep)
the actual parser
Definition: json.hpp:8425
json_spirit::pretty_print
@ pretty_print
Definition: json_spirit_writer_options.h:15
nlohmann::basic_json::const_iterator::operator<=
bool operator<=(const const_iterator &other) const
comparison: less than or equal
Definition: json.hpp:6664
nlohmann::basic_json::is_number
constexpr bool is_number() const noexcept
return whether value is a number
Definition: json.hpp:2244
nlohmann::basic_json::parser::parse
basic_json parse()
public parser interface
Definition: json.hpp:8412
nlohmann::basic_json::lexer::m_buffer
string_t m_buffer
the buffer
Definition: json.hpp:8374
nlohmann::basic_json::get_impl_ptr
number_integer_t * get_impl_ptr(number_integer_t *) noexcept
get a pointer to the value (integer number)
Definition: json.hpp:2683
nlohmann::basic_json::find
iterator find(typename object_t::key_type key)
find an element in a JSON object
Definition: json.hpp:4012
nlohmann::basic_json::push_back
void push_back(const basic_json &val)
add an object to an array
Definition: json.hpp:4722
nlohmann::basic_json::json_reverse_iterator
a template for a reverse iterator class
Definition: json.hpp:217
nlohmann
namespace for Niels Lohmann
Definition: json.hpp:66
value
static constexpr bool value
Definition: json.hpp:87
nlohmann::basic_json::at
reference at(const typename object_t::key_type &key)
access specified object element with bounds checking
Definition: json.hpp:3111
nlohmann::basic_json::iterator::operator--
iterator operator--(int)
post-decrement (it–)
Definition: json.hpp:6891
nlohmann::basic_json::parser::parser
parser(const string_t &s, parser_callback_t cb=nullptr) noexcept
constructor for strings
Definition: json.hpp:8396
nlohmann::basic_json::get_impl
constexpr boolean_t get_impl(boolean_t *) const
get a boolean (explicit)
Definition: json.hpp:2627
nlohmann::basic_json::array_t
ArrayType< basic_json, AllocatorType< basic_json > > array_t
a type for an array
Definition: json.hpp:379
nlohmann::basic_json::get_impl_ptr
array_t * get_impl_ptr(array_t *) noexcept
get a pointer to the value (array)
Definition: json.hpp:2647
nlohmann::basic_json::parse_event_t
parse_event_t
JSON callback events.
Definition: json.hpp:836
nlohmann::basic_json::lexer::lexer
lexer(const string_t &s) noexcept
constructor with a given buffer
Definition: json.hpp:7105
nlohmann::basic_json::iteration_proxy::iteration_proxy_internal::operator*
iteration_proxy_internal & operator*()
dereference operator (needed for range-based for)
Definition: json.hpp:6197
nlohmann::basic_json::iterator::base_iterator
const_iterator base_iterator
Definition: json.hpp:6834
nlohmann::basic_json::json_value::json_value
json_value(number_float_t v) noexcept
constructor for numbers (floating-point)
Definition: json.hpp:748
nlohmann::basic_json::lexer::token_type::begin_object
@ begin_object
the character for object begin "{"
nlohmann::basic_json::operator[]
reference operator[](size_type idx)
access specified array element
Definition: json.hpp:3206
nlohmann::basic_json::get_impl_ptr
string_t * get_impl_ptr(string_t *) noexcept
get a pointer to the value (string)
Definition: json.hpp:2659
std::vector
Definition: doxy_dummy_defs.hh:7
nlohmann::basic_json::get
ValueType get() const
get a value (explicit)
Definition: json.hpp:2789
nlohmann::basic_json::primitive_iterator_t::end_value
static constexpr difference_type end_value
Definition: json.hpp:6149
nlohmann::basic_json::lexer::m_cursor
const lexer_char_t * m_cursor
pointer to the current symbol
Definition: json.hpp:8382
nlohmann::basic_json::parser::last_token
lexer::token_type last_token
the type of the last read token
Definition: json.hpp:8652
nlohmann::basic_json::end
const_iterator end() const noexcept
returns a const iterator to one past the last element
Definition: json.hpp:4182
nlohmann::basic_json::iterator::operator[]
reference operator[](difference_type n) const
access to successor
Definition: json.hpp:6942
nlohmann::basic_json::primitive_iterator_t::set_begin
void set_begin() noexcept
set iterator to a defined beginning
Definition: json.hpp:6112
nlohmann::basic_json::json_value::json_value
json_value(number_unsigned_t v) noexcept
constructor for numbers (unsigned)
Definition: json.hpp:746
nlohmann::basic_json::get_impl
std::vector< T > get_impl(std::vector< T > *) const
get an array (explicit)
Definition: json.hpp:2524
nlohmann::basic_json::value_type
basic_json value_type
the type of elements in a basic_json container
Definition: json.hpp:196
nlohmann::basic_json::is_object
constexpr bool is_object() const noexcept
return whether value is an object
Definition: json.hpp:2351
nlohmann::basic_json::json_reverse_iterator::json_reverse_iterator
json_reverse_iterator(const base_iterator &it) noexcept
create reverse iterator from base class
Definition: json.hpp:6986
nlohmann::basic_json::begin
iterator begin() noexcept
returns an iterator to the first element
Definition: json.hpp:4101
nlohmann::basic_json::number_float_t
NumberFloatType number_float_t
a type for a number (floating-point)
Definition: json.hpp:663
nlohmann::basic_json::value_t::string
@ string
string value
nlohmann::basic_json::operator<=
friend bool operator<=(const_reference lhs, const_reference rhs) noexcept
comparison: less than or equal
Definition: json.hpp:5500
nlohmann::basic_json::basic_json
basic_json(basic_json &&other) noexcept
move constructor
Definition: json.hpp:1950
nlohmann::basic_json::get_impl
object_t get_impl(object_t *) const
get an object (explicit)
Definition: json.hpp:2477
nlohmann::basic_json::const_iterator::iterator_category
std::bidirectional_iterator_tag iterator_category
the category of the iterator
Definition: json.hpp:6302
nlohmann::basic_json::const_iterator::value_type
typename basic_json::value_type value_type
the type of the values when the iterator is dereferenced
Definition: json.hpp:6294
nlohmann::basic_json::lexer::token_type::literal_true
@ literal_true
the "true" literal
nlohmann::basic_json::crend
const_reverse_iterator crend() const noexcept
returns a const reverse iterator to one before the first
Definition: json.hpp:4345
nlohmann::basic_json::basic_json
basic_json(const basic_json &other)
copy constructor
Definition: json.hpp:1875
nlohmann::basic_json::lexer::token_type::begin_array
@ begin_array
the character for array begin "["
nlohmann::basic_json::internal_iterator
an iterator value
Definition: json.hpp:6162
nlohmann::basic_json::operator=
reference & operator=(basic_json other) noexcept(std::is_nothrow_move_constructible< value_t >::value and std::is_nothrow_move_assignable< value_t >::value and std::is_nothrow_move_constructible< json_value >::value and std::is_nothrow_move_assignable< json_value >::value)
copy assignment
Definition: json.hpp:1982
nlohmann::basic_json::operator<<
friend std::istream & operator<<(basic_json &j, std::istream &i)
deserialize from stream
Definition: json.hpp:5703
nlohmann::basic_json::get_impl
T get_impl(T *) const
get an object (explicit)
Definition: json.hpp:2463
nlohmann::basic_json::iterator::iterator
iterator()=default
default constructor
nlohmann::basic_json::cend
const_iterator cend() const noexcept
returns a const iterator to one past the last element
Definition: json.hpp:4212
nlohmann::basic_json::const_iterator::operator++
const_iterator & operator++()
pre-increment (++it)
Definition: json.hpp:6532
nlohmann::basic_json::parse_event_t::object_start
@ object_start
the parser read { and started to process a JSON object
nlohmann::basic_json::json_reverse_iterator::operator-
json_reverse_iterator operator-(difference_type i) const
subtract from iterator
Definition: json.hpp:7032
nlohmann::basic_json::value_t::null
@ null
null value
nlohmann::basic_json::value_t::number_integer
@ number_integer
number value (integer)
nlohmann::basic_json::iterator::operator-=
iterator & operator-=(difference_type i)
subtract from iterator
Definition: json.hpp:6913
nlohmann::basic_json::lexer::token_type::literal_null
@ literal_null
the "null" literal
nlohmann::basic_json::pointer
typename std::allocator_traits< allocator_type >::pointer pointer
the type of an element pointer
Definition: json.hpp:212
nlohmann::basic_json::iterator_wrapper
static iteration_proxy< iterator > iterator_wrapper(reference cont)
wrapper to access iterator member functions in range-based for
Definition: json.hpp:4366
nlohmann::basic_json::parser_callback_t
std::function< bool(int depth, parse_event_t event, basic_json &parsed)> parser_callback_t
per-element parser callback type
Definition: json.hpp:901
nlohmann::basic_json::size
size_type size() const noexcept
returns the number of elements
Definition: json.hpp:4485
nlohmann::basic_json::operator[]
reference operator[](const typename object_t::key_type &key)
access specified object element
Definition: json.hpp:3293
nlohmann::basic_json::basic_json
basic_json(const CompatibleObjectType &val)
create an object (implicit)
Definition: json.hpp:1060
nlohmann::basic_json::lexer::token_type::name_separator
@ name_separator
the name separator ":"
nlohmann::basic_json::json_reverse_iterator::value
reference value() const
return the value of an iterator
Definition: json.hpp:7059
nlohmann::basic_json::primitive_iterator_t::is_begin
constexpr bool is_begin() const noexcept
return whether the iterator can be dereferenced
Definition: json.hpp:6124
nlohmann::basic_json::is_boolean
constexpr bool is_boolean() const noexcept
return whether value is a boolean
Definition: json.hpp:2214
nlohmann::basic_json::rbegin
reverse_iterator rbegin() noexcept
returns an iterator to the reverse-beginning
Definition: json.hpp:4242
nlohmann::basic_json::const_iterator::operator>
bool operator>(const const_iterator &other) const
comparison: greater than
Definition: json.hpp:6670
nlohmann::basic_json::number_integer_t
NumberIntegerType number_integer_t
a type for a number (integer)
Definition: json.hpp:523
nlohmann::basic_json::lexer::m_marker
const lexer_char_t * m_marker
pointer for backtracking information
Definition: json.hpp:8380
nlohmann::basic_json::basic_json
basic_json(InputIT first, InputIT last)
construct a JSON container given an iterator range
Definition: json.hpp:1737
nlohmann::basic_json::parse_event_t::array_end
@ array_end
the parser read ] and finished processing a JSON array
nlohmann::basic_json::boolean_t
BooleanType boolean_t
a type for a boolean
Definition: json.hpp:452
nlohmann::basic_json::value
ValueType value(const typename object_t::key_type &key, ValueType default_value) const
access specified object element with default value
Definition: json.hpp:3568
nlohmann::basic_json::erase
InteratorType erase(InteratorType pos)
remove element given an iterator
Definition: json.hpp:3733
nlohmann::basic_json::is_primitive
constexpr bool is_primitive() const noexcept
return whether type is primitive
Definition: json.hpp:2143
nlohmann::basic_json::value_t
value_t
the JSON type enumeration
Definition: json.hpp:682
nlohmann::basic_json::operator==
friend bool operator==(const_reference lhs, const_reference rhs) noexcept
comparison: equal
Definition: json.hpp:5209
nlohmann::basic_json::internal_iterator::array_iterator
array_t::iterator array_iterator
iterator for JSON arrays
Definition: json.hpp:6167
nlohmann::basic_json::operator[]
const_reference operator[](T *(&key)[n]) const
read-only access specified object element
Definition: json.hpp:3419
nlohmann::basic_json::operator!=
friend bool operator!=(const_reference v, std::nullptr_t) noexcept
comparison: not equal
Definition: json.hpp:5361
nlohmann::basic_json::const_iterator::operator==
bool operator==(const const_iterator &other) const
comparison: equal
Definition: json.hpp:6598
nlohmann::basic_json::iteration_proxy::iteration_proxy_internal
helper class for iteration
Definition: json.hpp:6183
nlohmann::basic_json::const_iterator::operator-
const_iterator operator-(difference_type i)
subtract from iterator
Definition: json.hpp:6724
nlohmann::basic_json::operator+=
reference operator+=(const basic_json &val)
add an object to an array
Definition: json.hpp:4746
nlohmann::basic_json::json_reverse_iterator::operator+
json_reverse_iterator operator+(difference_type i) const
add to iterator
Definition: json.hpp:7024
nlohmann::basic_json::is_null
constexpr bool is_null() const noexcept
return whether value is null
Definition: json.hpp:2192
nlohmann::basic_json::json_value::json_value
json_value(boolean_t v) noexcept
constructor for booleans
Definition: json.hpp:742
nlohmann::basic_json::internal_iterator::primitive_iterator
primitive_iterator_t primitive_iterator
generic iterator for all other types
Definition: json.hpp:6169
nlohmann::basic_json::json_value::number_unsigned
number_unsigned_t number_unsigned
number (unsigned integer)
Definition: json.hpp:735
nlohmann::basic_json::parse
static basic_json parse(std::istream &&i, parser_callback_t cb=nullptr)
deserialize from stream
Definition: json.hpp:5675
nlohmann::basic_json::difference_type
std::ptrdiff_t difference_type
a type to represent differences between iterators
Definition: json.hpp:204
nlohmann::basic_json::operator>
friend bool operator>(const_reference lhs, const_reference rhs) noexcept
comparison: greater than
Definition: json.hpp:5522
nlohmann::basic_json::get_ref
ReferenceType get_ref() const
get a reference value (implicit)
Definition: json.hpp:2939
nlohmann::basic_json::basic_json
basic_json(boolean_t val) noexcept
create a boolean (explicit)
Definition: json.hpp:1229
nlohmann::basic_json::get_allocator
static allocator_type get_allocator()
returns the allocator associated with the container
Definition: json.hpp:234
nlohmann::basic_json::operator>>
friend std::istream & operator>>(std::istream &i, basic_json &j)
deserialize from stream
Definition: json.hpp:5713
nlohmann::basic_json::basic_json
basic_json(const number_unsigned_t val) noexcept
create an unsigned integer number (explicit)
Definition: json.hpp:1356
nlohmann::basic_json::create
static T * create(Args &&... args)
helper for exception-safe object creation
Definition: json.hpp:699
nlohmann::basic_json::iterator::operator-
difference_type operator-(const iterator &other) const
return difference
Definition: json.hpp:6936
nlohmann::basic_json::insert
iterator insert(const_iterator pos, std::initializer_list< basic_json > ilist)
inserts elements
Definition: json.hpp:4991
nlohmann::basic_json::json_value::json_value
json_value(const object_t &value)
constructor for objects
Definition: json.hpp:810
nlohmann::basic_json::basic_json
basic_json(const CompatibleNumberFloatType val) noexcept
create an floating-point number (implicit)
Definition: json.hpp:1462
nlohmann::basic_json::operator+=
reference operator+=(const typename object_t::value_type &val)
add an object to an object
Definition: json.hpp:4796
nlohmann::basic_json::json_reverse_iterator::operator[]
reference operator[](difference_type n) const
access to successor
Definition: json.hpp:7046
nlohmann::basic_json::iteration_proxy::iteration_proxy
iteration_proxy(typename IteratorType::reference cont)
construct iteration proxy from a container
Definition: json.hpp:6256
nlohmann::basic_json::const_iterator::set_begin
void set_begin() noexcept
set the iterator to the first value
Definition: json.hpp:6383
nlohmann::basic_json::parse_event_t::array_start
@ array_start
the parser read [ and started to process a JSON array
nlohmann::basic_json::erase
void erase(const size_type idx)
remove element from a JSON array given an index
Definition: json.hpp:3966
nlohmann::basic_json::basic_json
basic_json(const object_t &val)
create an object (explicit)
Definition: json.hpp:1025
nlohmann::basic_json::lexer::lexer_char_t
unsigned char lexer_char_t
the char type to use in the lexer
Definition: json.hpp:7102
nlohmann::basic_json::const_iterator::value
reference value() const
return the value of an iterator
Definition: json.hpp:6807
nlohmann::basic_json::basic_json
basic_json(const CompatibleNumberIntegerType val) noexcept
create an integer number (implicit)
Definition: json.hpp:1328
nlohmann::basic_json::is_number_integer
constexpr bool is_number_integer() const noexcept
return whether value is an integer number
Definition: json.hpp:2273
nlohmann::basic_json::find
const_iterator find(typename object_t::key_type key) const
find an element in a JSON object
Definition: json.hpp:4029
std::move
fmt::BufferedFile & move(fmt::BufferedFile &f)
Definition: posix.h:380
nlohmann::basic_json::lexer::token_type
token_type
token types for the parser
Definition: json.hpp:7083
nlohmann::basic_json::rend
reverse_iterator rend() noexcept
returns an iterator to the reverse-end
Definition: json.hpp:4279
nlohmann::basic_json::iterator::operator++
iterator operator++(int)
post-increment (it++)
Definition: json.hpp:6876
nlohmann::basic_json::is_number_float
constexpr bool is_number_float() const noexcept
return whether value is a floating-point number
Definition: json.hpp:2329
nlohmann::basic_json::lexer::token_type::end_of_input
@ end_of_input
indicating the end of the input buffer
nlohmann::basic_json::value_t::number_float
@ number_float
number value (floating-point)
nlohmann::basic_json::get_impl_ptr
constexpr const number_float_t * get_impl_ptr(const number_float_t *) const noexcept
get a pointer to the value (floating-point number)
Definition: json.hpp:2713
nlohmann::basic_json::basic_json
basic_json()=default
create a null object (implicitly)
nlohmann::basic_json::value
string_t value(const typename object_t::key_type &key, const char *default_value) const
overload for a default value of type const char*
Definition: json.hpp:3594
nlohmann::basic_json::swap
void swap(object_t &other)
exchanges the values
Definition: json.hpp:5094
nlohmann::basic_json::lexer::m_content
const lexer_char_t * m_content
the buffer pointer
Definition: json.hpp:8376
nlohmann::basic_json::json_value::number_integer
number_integer_t number_integer
number (integer)
Definition: json.hpp:733
nlohmann::basic_json::primitive_iterator_t::set_end
void set_end() noexcept
set iterator to a defined past the end
Definition: json.hpp:6118
nlohmann::basic_json::object
static basic_json object(std::initializer_list< basic_json > init=std::initializer_list< basic_json >())
explicitly create an object from an initializer list
Definition: json.hpp:1667
nlohmann::basic_json::rbegin
const_reverse_iterator rbegin() const noexcept
returns a const reverse iterator to the last element
Definition: json.hpp:4250
nlohmann::basic_json::operator[]
reference operator[](T *key)
access specified object element
Definition: json.hpp:3452
nlohmann::basic_json::lexer::str_to_float_t
long double str_to_float_t(long double *, char **endptr) const
parse floating point number
Definition: json.hpp:8205
nlohmann::basic_json::json_value::array
array_t * array
array (stored with pointer to save storage)
Definition: json.hpp:727
nlohmann::basic_json::basic_json
basic_json(const CompatibleStringType &val)
create a string (implicit)
Definition: json.hpp:1211
nlohmann::basic_json::get_impl_ptr
number_float_t * get_impl_ptr(number_float_t *) noexcept
get a pointer to the value (floating-point number)
Definition: json.hpp:2707
nlohmann::basic_json::iterator::operator->
pointer operator->()
dereference the iterator
Definition: json.hpp:6870
nlohmann::basic_json::json_reverse_iterator::operator++
json_reverse_iterator & operator++()
pre-increment (++it)
Definition: json.hpp:6997
nlohmann::basic_json::basic_json
basic_json(std::istream &i, parser_callback_t cb=nullptr)
construct a JSON value given an input stream
Definition: json.hpp:1844
nlohmann::basic_json::lexer::token_type_name
static std::string token_type_name(token_type t)
return name of values of type token_type (only used for errors)
Definition: json.hpp:7213
nlohmann::basic_json::get_ptr
constexpr const PointerType get_ptr() const noexcept
get a pointer value (implicit)
Definition: json.hpp:2888
nlohmann::basic_json::get_impl_ptr
number_unsigned_t * get_impl_ptr(number_unsigned_t *) noexcept
get a pointer to the value (unsigned number)
Definition: json.hpp:2695
nlohmann::basic_json::object_t
ObjectType< StringType, basic_json, std::less< StringType >, AllocatorType< std::pair< const StringType, basic_json > >> object_t
a type for an object
Definition: json.hpp:333
nlohmann::basic_json::get_impl_ptr
object_t * get_impl_ptr(object_t *) noexcept
get a pointer to the value (object)
Definition: json.hpp:2635
nlohmann::basic_json::push_back
void push_back(basic_json &&val)
add an object to an array
Definition: json.hpp:4686
nlohmann::basic_json::get_impl_ptr
constexpr const number_integer_t * get_impl_ptr(const number_integer_t *) const noexcept
get a pointer to the value (integer number)
Definition: json.hpp:2689
nlohmann::basic_json::get_impl_ptr
constexpr const string_t * get_impl_ptr(const string_t *) const noexcept
get a pointer to the value (string)
Definition: json.hpp:2665
std::map
Definition: doxy_dummy_defs.hh:11
nlohmann::basic_json::~basic_json
~basic_json()
destructor
Definition: json.hpp:2010
nlohmann::basic_json::parse_event_t::key
@ key
the parser read a key of a value in an object
nlohmann::basic_json::primitive_iterator_t::m_it
difference_type m_it
iterator as signed integer type
Definition: json.hpp:6152
nlohmann::basic_json::iteration_proxy
proxy class for the iterator_wrapper functions
Definition: json.hpp:4352
nlohmann::basic_json::lexer::scan
token_type scan() noexcept
Definition: json.hpp:7263
nlohmann::basic_json::const_iterator::operator++
const_iterator operator++(int)
post-increment (it++)
Definition: json.hpp:6524
nlohmann::basic_json::lexer::m_stream
std::istream * m_stream
optional input stream
Definition: json.hpp:8372
nlohmann::basic_json::reference
value_type & reference
the type of an element reference
Definition: json.hpp:199
nlohmann::basic_json::basic_json
basic_json(const string_t &val)
create a string (explicit)
Definition: json.hpp:1156
nlohmann::basic_json::json_value::json_value
json_value(const string_t &value)
constructor for strings
Definition: json.hpp:804
nlohmann::basic_json::const_iterator::m_object
pointer m_object
associated JSON instance
Definition: json.hpp:6814
nlohmann::basic_json::get_ref_impl
static ReferenceType get_ref_impl(ThisType &obj)
helper function to implement get_ref()
Definition: json.hpp:2730
Input::Type
Definition: balance.hh:41
nlohmann::basic_json::lexer::token_type::uninitialized
@ uninitialized
indicating the scanner is uninitialized
nlohmann::basic_json::at
const_reference at(const typename object_t::key_type &key) const
access specified object element with bounds checking
Definition: json.hpp:3159
nlohmann::basic_json::json_value::json_value
json_value()=default
default constructor (for null values)
nlohmann::basic_json::json_reverse_iterator::operator--
json_reverse_iterator & operator--()
pre-decrement (–it)
Definition: json.hpp:7010
nlohmann::basic_json::max_size
size_type max_size() const noexcept
returns the maximum possible number of elements
Definition: json.hpp:4551
nlohmann::basic_json::lexer::get_string
string_t get_string() const
return string value for string tokens
Definition: json.hpp:8082
nlohmann::basic_json::size_type
std::size_t size_type
a type to represent container sizes
Definition: json.hpp:206
nlohmann::basic_json::iterator::operator++
iterator & operator++()
pre-increment (++it)
Definition: json.hpp:6884
nlohmann::basic_json::lexer::lexer
lexer(std::istream *s) noexcept
constructor with a given stream
Definition: json.hpp:7115
nlohmann::basic_json::parser::expect
void expect(typename lexer::token_type t) const
Definition: json.hpp:8623
nlohmann::basic_json::const_iterator::reference
typename basic_json::const_reference reference
defines a reference to the type iterated over (value_type)
Definition: json.hpp:6300
nlohmann::basic_json::iterator::operator+=
iterator & operator+=(difference_type i)
add to iterator
Definition: json.hpp:6906
nlohmann::basic_json::value_t::object
@ object
object (unordered set of name/value pairs)
nlohmann::basic_json::operator>=
friend bool operator>=(const_reference lhs, const_reference rhs) noexcept
comparison: greater than or equal
Definition: json.hpp:5544
nlohmann::basic_json::lexer::get_number
void get_number(basic_json &result) const
return number value for number tokens
Definition: json.hpp:8307
nlohmann::basic_json::parser::get_token
lexer::token_type get_token() noexcept
get next token from lexer
Definition: json.hpp:8617
nlohmann::basic_json::dump
string_t dump(const int indent=-1) const
serialization
Definition: json.hpp:2079
nlohmann::basic_json::internal_iterator::internal_iterator
internal_iterator() noexcept
create an uninitialized internal_iterator
Definition: json.hpp:6172
nlohmann::basic_json::json_reverse_iterator::operator--
json_reverse_iterator operator--(int)
post-decrement (it–)
Definition: json.hpp:7004
nlohmann::basic_json::is_structured
constexpr bool is_structured() const noexcept
return whether type is structured
Definition: json.hpp:2170
nlohmann::basic_json::json_value::object
object_t * object
object (stored with pointer to save storage)
Definition: json.hpp:725
nlohmann::basic_json::const_iterator::set_end
void set_end() noexcept
set the iterator past the last value
Definition: json.hpp:6419
nlohmann::basic_json::lexer::operator=
lexer operator=(const lexer &)=delete
nlohmann::basic_json::lexer::yyfill
void yyfill() noexcept
append data from the stream to the internal buffer
Definition: json.hpp:8028
nlohmann::basic_json::const_iterator::operator[]
reference operator[](difference_type n) const
access to successor
Definition: json.hpp:6756
nlohmann::basic_json::operator<
friend bool operator<(const_reference lhs, const_reference rhs) noexcept
comparison: less than
Definition: json.hpp:5399
nlohmann::basic_json::operator==
friend bool operator==(std::nullptr_t, const_reference v) noexcept
comparison: equal
Definition: json.hpp:5317
nlohmann::basic_json::lexer::m_start
const lexer_char_t * m_start
pointer to the beginning of the current symbol
Definition: json.hpp:8378
nlohmann::basic_json::back
const_reference back() const
access the last element
Definition: json.hpp:3675
nlohmann::basic_json
a class to store JSON values
Definition: json.hpp:173
nlohmann::basic_json::iteration_proxy::iteration_proxy_internal::anchor
IteratorType anchor
the iterator
Definition: json.hpp:6187
nlohmann::basic_json::crbegin
const_reverse_iterator crbegin() const noexcept
returns a const reverse iterator to the last element
Definition: json.hpp:4316
nlohmann::basic_json::array
static basic_json array(std::initializer_list< basic_json > init=std::initializer_list< basic_json >())
explicitly create an array from an initializer list
Definition: json.hpp:1627
nlohmann::basic_json::parser
syntax analysis
Definition: json.hpp:8392
nlohmann::basic_json::swap
void swap(array_t &other)
exchanges the values
Definition: json.hpp:5060
nlohmann::basic_json::get_impl_ptr
constexpr const array_t * get_impl_ptr(const array_t *) const noexcept
get a pointer to the value (array)
Definition: json.hpp:2653
nlohmann::basic_json::iterator::operator--
iterator & operator--()
pre-decrement (–it)
Definition: json.hpp:6899
nlohmann::basic_json::basic_json
basic_json(std::nullptr_t) noexcept
create a null object (explicitly)
Definition: json.hpp:1002
std
Definition: doxy_dummy_defs.hh:5
nlohmann::basic_json::iterator::value
reference value() const
return the value of an iterator
Definition: json.hpp:6948
nlohmann::basic_json::iteration_proxy::iteration_proxy_internal::array_index
size_t array_index
an index for arrays (used to create key names)
Definition: json.hpp:6189
nlohmann::basic_json::allocator_type
AllocatorType< basic_json > allocator_type
the allocator type
Definition: json.hpp:209
nlohmann::basic_json::operator!=
friend bool operator!=(const_reference lhs, const_reference rhs) noexcept
comparison: not equal
Definition: json.hpp:5338
nlohmann::basic_json::const_iterator::operator-
difference_type operator-(const const_iterator &other) const
return difference
Definition: json.hpp:6732
nlohmann::basic_json::json_reverse_iterator::json_reverse_iterator
json_reverse_iterator(const typename base_iterator::iterator_type &it) noexcept
create reverse iterator from iterator
Definition: json.hpp:6981
nlohmann::basic_json::value_t::number_unsigned
@ number_unsigned
number value (unsigned integer)
nlohmann::basic_json::json_reverse_iterator::reference
typename Base::reference reference
the reference type for the pointed-to element
Definition: json.hpp:6978
nlohmann::basic_json::dump
void dump(std::ostream &o, const bool pretty_print, const unsigned int indent_step, const unsigned int current_indent=0) const
internal implementation of the serialization function
Definition: json.hpp:5929
nlohmann::basic_json::swap
void swap(string_t &other)
exchanges the values
Definition: json.hpp:5128
nlohmann::basic_json::lexer::str_to_float_t
double str_to_float_t(double *, char **endptr) const
parse floating point number
Definition: json.hpp:8225
nlohmann::basic_json::erase
size_type erase(const typename object_t::key_type &key)
remove element from a JSON object given a key
Definition: json.hpp:3928
nlohmann::basic_json::basic_json
basic_json(const number_integer_t val) noexcept
create an integer number (explicit)
Definition: json.hpp:1262
nlohmann::basic_json::const_iterator::operator->
pointer operator->() const
dereference the iterator
Definition: json.hpp:6489
nlohmann::basic_json::iteration_proxy::container
IteratorType::reference container
the container to iterate
Definition: json.hpp:6252
nlohmann::basic_json::rend
const_reverse_iterator rend() const noexcept
returns a const reverse iterator to one before the first
Definition: json.hpp:4287
nlohmann::basic_json::basic_json
basic_json(size_type cnt, const basic_json &val)
construct an array with count copies of given value
Definition: json.hpp:1691
nlohmann::basic_json::operator+=
reference operator+=(basic_json &&val)
add an object to an array
Definition: json.hpp:4712
nlohmann::basic_json::const_iterator::operator<
bool operator<(const const_iterator &other) const
comparison: smaller
Definition: json.hpp:6634
nlohmann::basic_json::parser::callback
parser_callback_t callback
callback function
Definition: json.hpp:8650
nlohmann::basic_json::const_iterator::operator*
reference operator*() const
return a reference to the value pointed to by the iterator
Definition: json.hpp:6449
nlohmann::basic_json::parse
static basic_json parse(const string_t &s, parser_callback_t cb=nullptr)
deserialize from string
Definition: json.hpp:5638
nlohmann::basic_json::is_string
constexpr bool is_string() const noexcept
return whether value is a string
Definition: json.hpp:2395
nlohmann::basic_json::const_iterator::const_iterator
const_iterator()=default
default constructor
nlohmann::basic_json::insert
iterator insert(const_iterator pos, const basic_json &val)
inserts element
Definition: json.hpp:4824
nlohmann::basic_json::primitive_iterator_t
an iterator for primitive JSON types
Definition: json.hpp:6108
nlohmann::basic_json::extra_space
static std::size_t extra_space(const string_t &s) noexcept
calculates the extra space to escape a JSON string
Definition: json.hpp:5757
nlohmann::basic_json::get
PointerType get() noexcept
get a pointer value (explicit)
Definition: json.hpp:2824
nlohmann::basic_json::basic_json
basic_json(const CompatibleNumberUnsignedType val) noexcept
create an unsigned number (implicit)
Definition: json.hpp:1387
nlohmann::basic_json::iteration_proxy::iteration_proxy_internal::iteration_proxy_internal
iteration_proxy_internal(IteratorType it) noexcept
Definition: json.hpp:6192
nlohmann::basic_json::lexer::token_type::value_number
@ value_number
a number – use get_number() for actual value
nlohmann::basic_json::const_iterator::difference_type
typename basic_json::difference_type difference_type
a type to represent differences between iterators
Definition: json.hpp:6296
nlohmann::basic_json::escape_string
static string_t escape_string(const string_t &s)
escape a string
Definition: json.hpp:5806
nlohmann::basic_json::const_iterator
a const random access iterator for the basic_json class
Definition: json.hpp:6287
nlohmann::basic_json::type_name
string_t type_name() const noexcept
return the type as string
Definition: json.hpp:5728
nlohmann::basic_json::const_pointer
typename std::allocator_traits< allocator_type >::const_pointer const_pointer
the type of an element const pointer
Definition: json.hpp:214
nlohmann::basic_json::operator<<
friend std::ostream & operator<<(std::ostream &o, const basic_json &j)
serialize to stream
Definition: json.hpp:5581
nlohmann::basic_json::iterator
a mutable random access iterator for the basic_json class
Definition: json.hpp:6831
nlohmann::basic_json::operator>>
friend std::ostream & operator>>(const basic_json &j, std::ostream &o)
serialize to stream
Definition: json.hpp:5599
nlohmann::basic_json::value_t::boolean
@ boolean
boolean value
nlohmann::basic_json::parser::depth
int depth
current level of recursion
Definition: json.hpp:8648
nlohmann::basic_json::lexer::attempt_cast
static bool attempt_cast(T_A source, T_B &dest)
static_cast between two types and indicate if it results in error
Definition: json.hpp:8263
nlohmann::basic_json::internal_iterator::object_iterator
object_t::iterator object_iterator
iterator for JSON objects
Definition: json.hpp:6165
nlohmann::basic_json::get_impl_ptr
constexpr const object_t * get_impl_ptr(const object_t *) const noexcept
get a pointer to the value (object)
Definition: json.hpp:2641
nlohmann::basic_json::const_iterator::operator>=
bool operator>=(const const_iterator &other) const
comparison: greater than or equal
Definition: json.hpp:6676
nlohmann::basic_json::operator[]
const_reference operator[](size_type idx) const
access specified array element
Definition: json.hpp:3252
nlohmann::basic_json::cbegin
const_iterator cbegin() const noexcept
returns a const iterator to the first element
Definition: json.hpp:4141
nlohmann::basic_json::empty
bool empty() const noexcept
checks whether the container is empty
Definition: json.hpp:4422
nlohmann::basic_json::iteration_proxy::iteration_proxy_internal::operator++
iteration_proxy_internal & operator++()
increment operator (needed for range-based for)
Definition: json.hpp:6203
nlohmann::basic_json::primitive_iterator_t::begin_value
static constexpr difference_type begin_value
Definition: json.hpp:6148
nlohmann::basic_json::iterator::operator+
iterator operator+(difference_type i)
add to iterator
Definition: json.hpp:6920
nlohmann::basic_json::get_impl_ptr
constexpr const boolean_t * get_impl_ptr(const boolean_t *) const noexcept
get a pointer to the value (boolean)
Definition: json.hpp:2677
nlohmann::basic_json::json_value::string
string_t * string
string (stored with pointer to save storage)
Definition: json.hpp:729
nlohmann::basic_json::m_type
value_t m_type
the type of the current element
Definition: json.hpp:6088
nlohmann::basic_json::basic_json
basic_json(std::initializer_list< basic_json > init, bool type_deduction=true, value_t manual_type=value_t::array)
create a container (array or object) from an initializer list
Definition: json.hpp:1535
nlohmann::basic_json::operator[]
const_reference operator[](const typename object_t::key_type &key) const
read-only access specified object element
Definition: json.hpp:3341
nlohmann::basic_json::json_value::json_value
json_value(const array_t &value)
constructor for arrays
Definition: json.hpp:816
nlohmann::basic_json::const_iterator::key
object_t::key_type key() const
return the key of an object iterator
Definition: json.hpp:6792
nlohmann::basic_json::parser::unexpect
void unexpect(typename lexer::token_type t) const
Definition: json.hpp:8635
nlohmann::basic_json::const_iterator::m_it
internal_iterator m_it
the actual iterator of the associated instance
Definition: json.hpp:6816
nlohmann::basic_json::json_value
a JSON value
Definition: json.hpp:722
nlohmann::basic_json::json_value::boolean
boolean_t boolean
boolean
Definition: json.hpp:731
nlohmann::basic_json::is_array
constexpr bool is_array() const noexcept
return whether value is an array
Definition: json.hpp:2373
nlohmann::basic_json::const_reverse_iterator
json_reverse_iterator< typename basic_json::const_iterator > const_reverse_iterator
a const reverse iterator for a basic_json container
Definition: json.hpp:226
nlohmann::basic_json::is_number_unsigned
constexpr bool is_number_unsigned() const noexcept
return whether value is an unsigned integer number
Definition: json.hpp:2301
nlohmann::basic_json::const_iterator::operator--
const_iterator & operator--()
pre-decrement (–it)
Definition: json.hpp:6569
nlohmann::basic_json::operator==
friend bool operator==(const_reference v, std::nullptr_t) noexcept
comparison: equal
Definition: json.hpp:5308
nlohmann::basic_json::json_reverse_iterator::operator++
json_reverse_iterator operator++(int)
post-increment (it++)
Definition: json.hpp:6991
nlohmann::basic_json::const_iterator::const_iterator
const_iterator(const const_iterator &other) noexcept
copy constructor
Definition: json.hpp:6364
nlohmann::basic_json::lexer::lexer
lexer()=default
default constructor
nlohmann::basic_json::basic_json
basic_json(const typename string_t::value_type *val)
create a string (explicit)
Definition: json.hpp:1180
nlohmann::basic_json::insert
iterator insert(const_iterator pos, const_iterator first, const_iterator last)
inserts elements
Definition: json.hpp:4933
nlohmann::basic_json::m_value
json_value m_value
the value of the current element
Definition: json.hpp:6091
nlohmann::basic_json::lexer
lexical analysis
Definition: json.hpp:7079
nlohmann::basic_json::swap
void swap(reference other) noexcept(std::is_nothrow_move_constructible< value_t >::value and std::is_nothrow_move_assignable< value_t >::value and std::is_nothrow_move_constructible< json_value >::value and std::is_nothrow_move_assignable< json_value >::value)
exchanges the values
Definition: json.hpp:5029
nlohmann::basic_json::lexer::token_type::value_string
@ value_string
a string – use get_string() for actual value
nlohmann::basic_json::iterator::operator-
iterator operator-(difference_type i)
subtract from iterator
Definition: json.hpp:6928
nlohmann::basic_json::lexer::token_type::value_separator
@ value_separator
the value separator ","
nlohmann::basic_json::at
reference at(size_type idx)
access specified array element with bounds checking
Definition: json.hpp:3019
nlohmann::basic_json::parser::parser
parser(std::istream &_is, parser_callback_t cb=nullptr) noexcept
a parser reading from an input stream
Definition: json.hpp:8404
nlohmann::basic_json::parser::m_lexer
lexer m_lexer
the lexer
Definition: json.hpp:8654
nlohmann::basic_json::reverse_iterator
json_reverse_iterator< typename basic_json::iterator > reverse_iterator
a reverse iterator for a basic_json container
Definition: json.hpp:224
nlohmann::basic_json::begin
const_iterator begin() const noexcept
returns a const iterator to the first element
Definition: json.hpp:4111
nlohmann::basic_json::basic_json
basic_json(const int val) noexcept
create an integer number from an enum type (explicit)
Definition: json.hpp:1291
nlohmann::basic_json::basic_json
basic_json(const CompatibleArrayType &val)
create an array (implicit)
Definition: json.hpp:1127
nlohmann::basic_json::back
reference back()
access the last element
Definition: json.hpp:3665
nlohmann::basic_json::iterator_wrapper
static iteration_proxy< const_iterator > iterator_wrapper(const_reference cont)
wrapper to access iterator member functions in range-based for
Definition: json.hpp:4374
nlohmann::basic_json::json_value::json_value
json_value(number_integer_t v) noexcept
constructor for numbers (integer)
Definition: json.hpp:744
std::hash< nlohmann::json >::operator()
std::size_t operator()(const nlohmann::json &j) const
return a hash value for a JSON object
Definition: json.hpp:8706