1. Motivation
JSON is an internet standard, widely used for data transmission and storage, but the C++ standard library lacks support for JSON, which forces C++ users to choose among third-party libraries.
As the C++ standard evolves, C++ becomes more and more suitable for network programming, and C++'s high performance makes C++ equally suitable for processing large amounts of data stored by JSON, so adding JSON to the standard library is beneficial and harmless to C++.
There are many third-party libraries that provide JSON support for C++, but they have some drawbacks, such as:
-
They may have external dependencies, which can increase the complexity and size of the project, and introduce potential errors and incompatibilities.
-
They may have different interfaces, conventions, and styles, which can reduce the readability and consistency of the code, and increase the learning curve for new users.
-
They may have different levels of features, performance, and compatibility, which can make it hard to choose the best one for a specific use case, and may require switching between different libraries for different scenarios.
-
They may not follow the modern C++ standards and best practices, which can limit the usability and portability of the code, and prevent the use of new language features and idioms.
Therefore, this proposal aims to provide a minimal JSON support library for C++, which can address these issues, and offer the following benefits:
-
It does not have any external dependencies, and can be easily integrated into any C++ project, without affecting the existing code or environment.
-
It has a simple and consistent interface, which follows the existing C++ standard library conventions and styles, and can be easily learned and used by C++ programmers.
-
It has a sufficient level of features, performance, and compatibility, which can cover most common use cases of JSON data, and can work with any conforming C++ compiler and platform.
-
It is easy to implement, with only about 2200 lines of code, which avoids too much implementation details and problems.
2. Proposal
I propose to add a
header and the following classes (templates):
,
,
,
,
.
The
enumeration and the
exception class are used to report errors.
struct nulljson_t ; inline constexpr nulljson_t nulljson {}; template < typename Number , typename Integer , typename UInteger , typename Allocator > class basic_json_node ; template < typename Node , typename String , typename Array , typename Object , bool HasInteger , bool HasUInteger > class basic_json ; template < typename Node , typename String , typename Array , typename Object , bool HasInteger , bool HasUInteger > class basic_const_json_slice ; template < typename Node , typename String , typename Array , typename Object , bool HasInteger , bool HasUInteger > class basic_json_slice ; enum class errc ; class json_error ;
3. Design
Since JSON has a self-referential structure ([RFC8259]), type erasure must be used.
3.1. nulljson
/nulljson_t
is a type similar to
, used to indicate that the value of JSON is null.
is the type of
. It is a trivial, empty class with a explicit default constructor, like all other construction tag types in the standard library.
3.2. basic_json_node
has four template parameters:
,
,
, and
. Users can use these template parameters to customize their preferred types and allocators.
For example, some users may prefer to use fixed-length integer types, some users may prefer to use integer types written in by C++ keywords, and the same for floating-point types.
holds an allocator and a tagged union that stores one of a boolean, number (floating-point), signed integer, unsigned integer, string, array (
), or object (
), where the tag is represented by an enumeration value.
is a substitute for
, providing storage space for
in any situation where circular dependencies may occur.
is conceptually similar to a raw pointer. It does not always own memory, but can transfer memory through it.
Trivial copyability of
depends on
. If
is trivially copyable, then so is
, and
arrays will get faster copy speed.
3.3. basic_json
is a
type that represents ownership of a JSON structure. It can be implemented as storing a
as its only non-static data member, which makes
and
have the same size.
Its destructor is responsible for destructing all nodes and deallocate all dynamic storage held by the object. The copy constructor and copy assignment operator copy the JSON. The swap operation is provided as both a hidden friend function and a non-static member function.
The reason why the allocator is a template parameter of
rather than
is that
must have the same size as
, so
is usually used to instantiate the allocator (
type can be used after LWG issue [3917] is resolved),
and then rebind is used to allocate storage. Once a specialization of
is available,
can be instantiated.
has six template parameters:
,
,
,
,
,
.
must be a specialization of
, and since
provides type aliases to obtain the template arguments,
can extract these type aliases, rewrite the specialization of
, and compare it with
to ensure this.
For arithmetic types, they are directly stored in the union, and since map and array store
, pointers are needed to break the circular dependency,
and since the two types are not determined in
, these pointers are actually of type
. Conceptually,
is a hybrid of container adapters and containers.
Although string type does not have circular dependency problem,
is also used to save the space. The relationship between
and
is shown in the following figure:
Therefore, the
template parameter of
is not used directly, but always rebound to the allocators of string, array, and object.
Most of the member functions of
are constructors, which make C++ values easily convertible to a JSON type.
In addition,
also has a
-qualified to
conversion function, which can transfer memory from
to
, just like
.
The most special point of my proposal is to expose
, which allows users to implement their own serializer and deserializer in a non-intrusive way: if a basic_json object that stores a boolean or arithmetic type value is needed, then construct it directly through the constructor,
if a
object that stores an array or object type is needed, then users can construct array and object themselves, such as
and
, then construct
objects through the constructors of boolean or arithmetic types,
and then insert them into the container, finally, move
or
to the constructor of
, and get a
object that represents an array or map.
3.4. basic_json_slice
/basic_const_json_slice
and
are similar to iterators of containers.
is constructible from a possibly const
or
and holds a pointer to that
object. All non-static functions of
are
-qualified, and return either a value or a reference to a const object.
has all the member functions that
has, and can be converted to
. In addition,
also has modifiers (overload of assignment operators), which can modify the value without changing the type of the value stored in the node.
behaves like
in the Library Fundamentals TS ([LFTSv3]). Operations on a const
value also access the referred to node in a const manner, like a
. Only operations on a non-const
possibly modifies the node.
and
are trivially copyable, so copying a slice has low overhead. No operation on a slice produces a copy of the
object. For subscript operations, a new
or
is returned.
A default-constructed slice holds a null pointer, and thus all operations that need to queries or modifies the node results in undefined behavior on such a slice. The default-constructibility of slices is mainly used for default arguments.
3.5. Summary
This design makes the
template independent of the specific vector type, map type, string type. One can use
,
, or
if desires.
Additionally, the string type as the
of the Object and the
type as the
can be different, which makes
can be implemented with a dictionary that records all possible values.
This design does not care whether the string type has a charactor traits or allocator type.
In extreme cases, this design allows both strings to be
, such as when mapping the JSON byte stream to memory, each
references a part of the memory.
This makes it possible to not copy any strings (but still need to use dynamic memory to store maps and arrays).
4. Implementation experience
I have provided a simple implementation on Github, the source code of the document is available in the same repository.
Currently, stateful allocators are not properly supported, because I do not have much experience with allocators. I have not provided serialization and deserialization functions, because the C++ standard library currently does not have a good IO API, and different users may have their own serialization and deserialization needs, so this is not part of the proposal.
The design is feasible and stable, but I need some feedback to appropriately increase the usability of the library.
5. Use cases
#include "basic_json.hpp"#include <string_view>#include <cassert>int main () { using json = bizwen :: json ; using node = bizwen :: json_node ; using slice = bizwen :: json_slice ; using const_slice = bizwen :: const_json_slice ; auto null = bizwen :: nulljson ; using namespace std :: literals ; // A json object represents a json value. The default constructed json object does not hold any value, and its state is "undefined". json j01 {}; // Construct a json value with status "number" and value `1`. json j02 { 1. }; // Construct a json value with status "true_value" and value `true`. json j03 { true}; // Construct a json value with status "uinteger" and value `1`. This method is used to accurately store integers with more than 52 bits. json j04 { 1ull }; // Construct a json value with status "string" and value `"abcdef"`. json j05 { "abcdef" }; // Construct json with nullptr is forbidden because json does not have a pointer type and nullptr does not represent the empty string. // json j05{ nullptr }; // Construct a json value with status "null" and value `nulljson`. json j06 { null }; // Since initializer_list returns a reference to a const object, this method is inefficient. // json j07{ json::array_type{ json{0}, json{1} } }; // json j08{ json::object_type{ { "key0"s, json{ 0 } }, { "key1"s, json{ 1 } } } }; // Use the helper class templates json::array and json::object for easy and efficient construction of json arrays and json objects. // Constructs a json value with the state "array", containing two ordered values 0 and 1. json j07 { json :: array { 0 , 1 } }; // Construct a json value with state "object" such that `s08["key0"]==0` and `s08["key1"]==1` is true. json j08 { json :: object { "key0" s , 0 , "key1" s , 1 } }; // Copy a json object copies its stored state and values. auto j09 { j08 }; // slice is an accessor and modifier of json values, and the default constructed slice is not associated with any json. slice s01 {}; // Use empty() to test if slice is associated with a json. auto is_empty { s01 . empty () }; assert ( is_empty ); // does nothing slice s07 { j07 }; // s07 is associated to j07 auto is_array { s07 . array () }; assert ( is_array ); // does nothing // Convert a json value (s01) with state "undefined" to "integer" and set the value to `1`. s01 = 1 ; slice s02 { j02 }; // s02 is associated to j02 // Change the value of j02 to 2.f s02 = 2.f ; // Sets the state of j02 to "undefined" and destroys the previous value. s02 . reset (); long long iv { s07 [ 0 ] }; // Iterate j07 for ( auto i : s07 . as_array ()) { assert ( i . integer ()); // does nothing } // Append a value to j07 // Due to CWG1996, list initialization cannot be used here. json :: array_type & arr ( s07 ); arr . push_back ( json { 2 }); slice s08 { j08 }; // Iterate j08 for ( auto const & [ k , v ] : s08 . as_object ()) { assert ( v . integer ()); // does nothing } // Insert k-v pairs into j08 s08 [ "key3" ] = 2 ; // Copying a slice is trivial auto s08_1 { s08 }; // const_slice is similar to slice, but has only observers and never modifies the associated json. const_slice c01 { s01 }; // Unlike slice, if the key does not exist in the object then a json_error exception is thrown. try { c01 [ "keyn" ]; } catch ( bizwen :: json_error const & je ) { assert ( je . code () == bizwen :: json_errc :: key_not_found ); // does nothing } }
6. Library wording
6.1. JSON processing
[Drafting note: Add this subclause to [utilities]. -- end drafting note]
6.1.1. Header < json >
synopsis
namespace std { struct nulljson_t { explicit nulljson_t () = default ; }; inline constexpr nulljson_t nulljson ; template < class Number = double , class Integer = long long , class UInteger = unsigned long long , class Allocator = allocator < byte >> class basic_json_node ; template < class Node = basic_json_node <> , class String = string , class Array = vector < Node > , class Object = map < String , Node > , bool HasInteger = true, bool HasUInteger = true> class basic_json ; template < class Node = basic_json_node <> , class String = string , class Array = vector < Node > , class Object = map < String , Node > , bool HasInteger = true, bool HasUInteger = true> class basic_json_slice ; template < class Node = basic_json_node <> , class String = string , class Array = vector < Node > , class Object = map < String , Node > , bool HasInteger = true, bool HasUInteger = true> class basic_const_json_slice ; using json = basic_json <> ; using json_node = basic_json_node <> ; using json_slice = basic_json_slice <> ; using const_json_slice = basic_const_json_slice <> ; enum class json_errc : < i > unspecified </ i > ; class json_error ; namespace pmr { template < class Number = double , class Integer = long long , class UInteger = unsigned long long > using basic_json_node = std :: basic_json_node < Number , Integer , UInteger , polymorphic_allocator <>> ; template < class Node = basic_json_node <> , class String = string , class Array = vector < Node > , class Object = map < String , Node > , bool HasInteger = true, bool HasUInteger = true> using basic_json = std :: basic_json < Node , String , Array , Object , HasInteger , HasUInteger > ; template < class Node = basic_json_node <> , class String = string , class Array = vector < Node > , class Object = map < String , Node > , bool HasInteger = true, bool HasUInteger = true> using basic_json_slice = std :: basic_json_slice < Node , String , Array , Object , HasInteger , HasUInteger > ; template < class Node = basic_json_node <> , class String = string , class Array = vector < Node > , class Object = map < String , Node > , bool HasInteger = true, bool HasUInteger = true> using basic_const_json_slice = std :: basic_const_json_slice < Node , String , Array , Object , HasInteger , HasUInteger > ; using json = basic_json <> ; using json_node = basic_json_node <> ; using json_slice = basic_json_slice <> ; using const_json_slice = basic_const_json_slice <> ; } }
6.1.2. General
-
A
object is a JSON node. A valid node object is always in one of the following states: undefined, null, true, false, floating-point, signed integral, unsigned integral, string, array, and object.basic_json_node -
Some operations of
andbasic_json
dynamically allocate memory. This happens if and only if thebasic_json_slice
held bybasic_json_node
or referenced bybasic_json
is set to the string, array, or object state. In such a case, letbasic_json_slice -
be theA
of the node type,allocator_type -
be the allocator object of typea
stored in the node,A -
be the type for the state, which isX
for the string state,String
for the array state, orArray
for the object state.Object -
beAX
,allocator_traits < A >:: template rebind_alloc < X > -
be an lvalue of typeax
that denotes anAX
allocator object constructed fromAX
.a
Such an aforementioned operation allocates the dynamic storage with
, and then constructs theallocator_traits < AX >:: allocate ( ax , 1 )
object usingX
. If an exception is thrown when setting the state, the value of the node is unchanged.allocator_traits < AX >:: construct ( ax , p , args ...) -
-
Likewise, the assignment operators and the destructor of
destroy the object in the dynamic storage and then deallocate the storage when needed. Letbasic_json -
be thex
object to be destroyed,X -
bepp
,pointer_traits < allocator_traits < AX >:: pointer >:: pointer_to ( x )
is destroyed usingx
, and then the dynamic storage is deallocated withallocator_traits < AX >:: destroy ( ax , addressof ( x ))
.allocator_traits < AX >:: deallocate ( pp , 1 ) -
-
For the purpose of constraints of functions, the map type
is considered transparently comparable if and only ifObject -
qualified-ids
andObject :: key_compare
are all valid and denote types, orObject :: key_compare :: is_transparent -
qualified-ids
,Object :: key_equal
,Object :: key_equal :: is_transparent
, andObject :: hasher
are all valid and denote types.Object :: hasher :: is_transparent
-
6.1.3. Class template basic_json_node
namespace std { template < class Number = double , class Integer = long long , class UInteger = unsigned long long , class Allocator = allocator > class basic_json_node { private : using void - ptr = allocator_traits :: void_pointer ; // exposition only public : using number_type = Number ; using integer_type = Integer ; using uinteger_type = UInteger ; using allocator_type = Allocator ; constexpr basic_json_node () noexcept ( is_nothrow_default_constructible_v ) requires default_initializable < Allocator > ; constexpr explicit basic_json_node ( const Allocator & a ) noexcept ; constexpr basic_json_node ( const basic_json_node & other ) noexcept ; constexpr basic_json_node ( basic_json_node && other ) noexcept ; constexpr basic_json_node & operator = ( const basic_json_node & other ) noexcept ( is_nothrow_copy_assignable_v < Allocator > ); constexpr basic_json_node & operator = ( basic_json_node && other ) noexcept ( is_nothrow_move_assignable_v < Allocator > ); constexpr ~ basic_json_node () noexcept ; }; template < class Number , class Integer , class UInteger , class Allocator , class Allocator2 > struct uses_allocator < basic_json_node < Number , Integer , UInteger , Allocator > , Allocator2 > : false_type {}; }
-
Mandates:
-
,floating_point < Number >
, andsigned_integral < Integer >
are allunsigned_integral < UInteger > true
. -
meets the Cpp17Allocator requirements, no diagnostic is required for this.Allocator
-
-
If the node is in the floating-point, signed integral, or unsigned integral state, an object of type
,number_type
, orinteger_type
stored within the node. If the node is in the string, array, or object state, an object of the type corresponding to the state is dynamically allocated, and auinteger_type
to it is stored within the node. [Note: A single node type can refer to string, dynamic array, or map of different types. -- end note]void - ptr -
[Note: The
partial specialization indicates thatuses_allocator
should not be uses-allocator constructed. -- end note]basic_json_node
6.1.3.1. Construction, assignment, and destruction
constexpr basic_json_node () noexcept ( is_nothrow_default_constructible_v < Allocator > ) requires default_initializable < Allocator > ;
-
Effects: Initializes the node to the undefined state. Value-initializes the stored allocator.
constexpr explicit basic_json_node ( const Allocator & a ) noexcept ;
-
Effects: Initializes the node to the undefined state. Copy-constructs the stored allocator from
.a
constexpr basic_json_node ( const basic_json_node & other ) noexcept ; constexpr basic_json_node ( basic_json_node && other ) noexcept ;
-
Effects:
-
For the copy constructor, copy-constructs the stored allocator from that of
; for the move constructor, move construction is used instead.other -
Sets
in the same state as* this
.other -
If
stores a numeric value, stores the same value inother
.this -
Otherwise, if
stores aother
to an allocated object, for the copy constructor, copy-constructs that pointer intovoid - ptr
; for the move constructor, move construction is used instead.* this
-
-
Remarks:
-
The copy constructor is trivial if both
andis_trivially_copy_constructible_v < Allocator >
areis_trivially_copy_constructible_v < void - ptr > true
. -
The move constructor is trivial if both
andis_trivially_move_constructible_v < Allocator >
areis_trivially_move_constructible_v < void - ptr > true
.
-
constexpr basic_json_node & operator = ( const basic_json_node & other ) noexcept ( is_nothrow_copy_assignable_v < Allocator > ); constexpr basic_json_node & operator = ( basic_json_node && other ) noexcept ( is_nothrow_move_assignable_v < Allocator > );
-
Constraints: For the move assignment operator,
isis_move_assignable_v < Allocator > true
. -
Effects:
-
If
, does nothing.this == addressof ( other ) -
Otherwise, copy- or move-assign the stored allocator of
from that of* this
, for copy or move assignment operator respectively. And thenother -
if
is in the same state as* this
and stores a numeric value orother
, copy- or move-assign the stored numeric value orvoid - ptr
invoid - ptr
to that ofother
, for copy or move assignment operator respectively, otherwise,* this -
destroys the stored a numeric value or
, if any, and then copy- or move-constructs the numeric value orvoid - ptr
from that ofvoid - ptr
for copy or move assignment operator respectively, if any.other
-
-
-
Returns:
.* this -
Remarks:
-
The copy assignment operator is deleted if
isis_copy_assignable_v < Allocator > false
. -
The copy assignment operator is trivial if
,is_trivially_copy_assignable_v < Allocator >
,is_trivially_destructible_v < void - ptr >
, andis_trivially_copy_constructible_v < void - ptr >
are allis_trivially_copy_assignable_v < void - ptr > true
. -
The move assignment operator is trivial if
,is_trivially_move_assignable_v < Allocator >
,is_trivially_destructible_v < void - ptr >
, andis_trivially_move_constructible_v < void - ptr >
are allis_trivially_move_assignable_v < void - ptr > true
.
-
constexpr ~ basic_json_node () noexcept ;
-
Effects: Destroys the stored allocator, and the numeric value or the
, if any. [Note: No deallocation happens. -- end note]void - ptr -
Remarks: The destructor is trivial if both
andis_trivially_destructible_v < Allocator >
areis_trivially_destructible_v < void - ptr > true
.
6.1.4. Class template basic_json
namespace std { template < class Node = basic_json_node <> , class String = string , class Array = vector < Node > , class Object = map < String , Node > , bool HasInteger = true, bool HasUInteger = true> class basic_json { public : static constexpr bool has_integer = HasInteger ; static constexpr bool has_uinteger = HasUInteger ; using slice_type = basic_json_slice < Node , String , Array , Object , HasInteger , HasUInteger > ; using const_slice_type = basic_const_json_slice < Node , String , Array , Object , HasInteger , HasUInteger > ; using number_type = node_type :: number_type ; using integer_type = node_type :: integer_type ; using uinteger_type = node_type :: uinteger_type ; using char_type = string_type :: value_type ; using map_node_type = object_type :: value_type ; using allocator_type = node_type :: allocator_type ; using key_string_type = object_type :: key_type ; using key_char_type = key_string_type :: value_type ; constexpr basic_json () requires default_initializable = default ; constexpr explicit basic_json ( const allocator_type & a ) noexcept : node_ ( a ) {} constexpr basic_json ( const basic_json & rhs ); constexpr basic_json ( basic_json && rhs ) noexcept ; constexpr basic_json & operator = ( const basic_json & rhs ); constexpr basic_json & operator = ( basic_json && rhs ) noexcept ( allocator_traits :: propagate_on_container_move_assignment :: value || allocator_traits :: is_always_equal :: value ); constexpr ~ basic_json (); constexpr void swap ( basic_json & rhs ) noexcept ( allocator_traits :: propagate_on_container_swap :: value || allocator_traits :: is_always_equal :: value ); friend constexpr void swap ( basic_json & lhs , basic_json & rhs ) noexcept ( noexcept ( lhs . swap ( rhs ))); constexpr basic_json ( const basic_json & rhs , const allocator_type & a ); constexpr basic_json ( basic_json && rhs , const allocator_type & a ) noexcept ( allocator_traits :: is_always_equal :: value ); constexpr basic_json ( nulljson_t , const allocator_type & a = allocator_type ()) noexcept ; template < class T > constexpr basic_json ( T n , const allocator_type & a = allocator_type ()) noexcept ; constexpr explicit basic_json ( string_type v , const allocator_type & a = allocator_type ()); constexpr basic_json ( const char_type * first , const char_type * last , const allocator_type & a = allocator_type ()); constexpr basic_json ( const char_type * str , string_type :: size_type count , const allocator_type & a = allocator_type ()); constexpr explicit basic_json ( const char_type * str , const allocator_type & a = allocator_type ()); template < class StrLike > constexpr basic_json ( const StrLike & str , const allocator_type & a = allocator_type ()); constexpr explicit basic_json ( array_type arr , const allocator_type & a = allocator_type ()); constexpr explicit basic_json ( object_type obj , const allocator_type & a = allocator_type ()); constexpr explicit basic_json ( node_type && n ) noexcept ; constexpr explicit basic_json ( node_type && n , const allocator_type & a ) noexcept ; basic_json ( nullptr_t , const allocator_type & = allocator_type ()) = delete ; constexpr allocator_type get_allocator () const noexcept ; constexpr operator node_type () && noexcept ; constexpr slice_type slice () noexcept ; constexpr const_slice_type slice () const noexcept ; private : node_type node_ ; // exposition only // Helper Classes template < class ... Ts > struct array ; // exposition only template < size_t N > struct object ; // exposition only template < class ... Ts > object ( Ts && ...) -> object < see below > ; }; }
-
Mandates:
-
is a well-formed specialization ofNode
.basic_json_node -
qualified-id
is valid and denotes the same type asArray :: value_type
.Node -
qualified-id
is valid and denotes the same type asObject :: mapped_type
.Node
-
6.1.4.1. Construction and swap
-
Every constructor taking a
parameter constructsconst allocator_type &
from that parameter.node_ -
Every constructor or assignment operator taking a
parameter leaves the argument in a valid but unspecified state after construction or assignment. Except for the move constructor, each of these functions reuse the dynamic storage of the argument if and only if there is some dynamic storage allocated and the stored allocator in thebasic_json &&
is equal to that of the argument.node_ -
The copy constructors, move constructor, copy assignment operator, move assignment operator, and
functions propagate the allocator value as specified in [container.alloc.reqmts].swap
constexpr basic_json ( nulljson_t , const allocator_type & a = allocator_type ()) noexcept ;
-
Effects: Sets
to the null state.node_
template < class T > constexpr basic_json ( T n , const allocator_type & a = allocator_type ()) noexcept ;
-
Constraints:
isis_arithmetic_v < T > true
. -
Effects:
-
If
isT
, setsbool
to the true or the false state ifnode_
isn true
orfalse
, respectively. -
Otherwise, if
andsigned_integral < T >
are bothhas_integer true
, sets
to the signed integral state and storesnode_
.n -
Otherwise, if
andunsigned_integral < T >
are bothhas_uinteger true
, sets
to the unsigned integral state and storesnode_
.n -
Otherwise, sets
to the floating-point state and storesnode_
.static_cast < number_type > ( n )
-
constexpr explicit basic_json ( string_type v , const allocator_type & a = allocator_type ());
-
Effects: Sets
to the string state and stores a string constructed fromnode_
.std :: move ( v ) -
Throws: The exception thrown on allocation failure, or the exception thrown in the failed construction of the
.String
constexpr basic_json ( const char_type * first , const char_type * last , const allocator_type & a = allocator_type ());
-
Preconditions: [
,first
) is a valid range.last -
Effects: Sets
to the string state and stores a string containing characters in [node_
,first
).last -
Throws: The exception thrown on allocation failure, or the exception thrown in the failed construction of the
.String
constexpr basic_json ( const char_type * str , string_type :: size_type count , const allocator_type & a = allocator_type ());
-
Preconditions: [
,str
) is a valid range.str + count -
Effects: Sets
to the string state and stores a string containing characters in [node_
,str
).str + count -
Throws: The exception thrown on allocation failure, or the exception thrown in the failed construction of the
.String
constexpr explicit basic_json ( const char_type * str , const allocator_type & a = allocator_type ());
-
Preconditions:
points to a null-terminatedstr
sequence.char_type -
Effects: Sets
to the string state and stores a string containing characters in [node_
,str
), wherestr + count
is the number of characters in the null-terminated sequence.count -
Throws: The exception thrown on allocation failure, or the exception thrown in the failed construction of the
.String
template < class StrLike > constexpr basic_json ( const StrLike & str , const allocator_type & a = allocator_type ());
-
Constraints:
-
isconstructible_from < string_type , StrLike > true
, and -
isis_convertible_v < const KeyStrLike & , const key_char_type *> false
.
-
-
Effects: Sets
to the string state and stores a string constructed fromnode_
.str -
Throws: The exception thrown on allocation failure, or the exception thrown in by the construction of the
.String
constexpr explicit basic_json ( array_type arr , const allocator_type & a = allocator_type ());
-
Effects: Sets
to the array state and stores a dynamic array constructed fromnode_
.std :: move ( arr ) -
Throws: The exception thrown on allocation failure, or the exception thrown by the construction of the
.Array
constexpr explicit basic_json ( object_type obj , const allocator_type & a = allocator_type ());
-
Effects: Sets
to the object state and stores a map constructed fromnode_
.std :: move ( obj ) -
Throws: The exception thrown on allocation failure, or the exception thrown by the construction of the
.Object
constexpr explicit basic_json ( node_type && n ) noexcept ;
-
Effects: Constructs
fromnode_
and setsstd :: move ( n )
to the undefined state. The stored allocator inn
is unchanged.n
constexpr explicit basic_json ( node_type && n , const allocator_type & a ) noexcept ;
-
Effects: Constructs
fromnode_
, sets it to the same state asstd :: move ( a )
, and then setsn
to the undefined state. The stored allocator inn
is unchanged.n
constexpr void swap ( basic_json & rhs ) noexcept ( allocator_traits < Allocator >:: propagate_on_container_swap :: value || allocator_traits < Allocator >:: is_always_equal :: value ); friend constexpr void swap ( basic_json & lhs , basic_json & rhs ) noexcept ( noexcept ( lhs . swap ( rhs )));
-
Effects: Swaps the values of the
member of bothnode_
objects.basic_json
6.1.4.2. Observers
constexpr allocator_type get_allocator () const noexcept ;
-
Returns: The copy of the constructor stored in
.node_
6.1.4.3. Slicing
constexpr operator node_type () && noexcept ;
-
Effects: Sets
to the undefined state.node_ -
Returns: A
value holding the value ofnode_type
before modification.node_ -
Recommended practice: An implementation should emit a diagnostic message when the return value is discarded.
[Note: Discarding the return value leaks memory if
was originally in the string, array, or object state. -- end note]node_
constexpr slice_type slice () noexcept ;
-
Effects: Equivalent to
.return slice_type ( * this );
constexpr const_slice_type slice () const noexcept ;
-
Effects: Equivalent to
.return const_slice_type ( * this );
6.1.5. Class template basic_json_slice
namespace std { template < class Node = basic_json_node <> , class String = string , class Array = vector < Node > , class Object = map < String , Node > , bool HasInteger = true, bool HasUInteger = true> class basic_json_slice { public : static constexpr bool has_integer = HasInteger ; static constexpr bool has_uinteger = HasUInteger ; using node_type = Node ; using object_type = Object ; using value_type = Node ; using array_type = Array ; using string_type = String ; using slice_type = basic_json_slice ; using const_slice_type = basic_const_json_slice < Node , String , Array , Object , HasInteger , HasUInteger > ; using json_type = basic_json < Node , String , Array , Object , HasInteger , HasUInteger > ; using number_type = node_type :: number_type ; using integer_type = node_type :: integer_type ; using uinteger_type = node_type :: uinteger_type ; using char_type = string_type :: value_type ; using map_node_type = object_type :: value_type ; using allocator_type = node_type :: allocator_type ; using key_string_type = object_type :: key_type ; using key_char_type = key_string_type :: value_type ; constexpr basic_json_slice () noexcept = default ; constexpr basic_json_slice ( json_type & j ) noexcept ; constexpr basic_json_slice ( node_type & n ) noexcept ; constexpr void swap ( basic_json_slice & rhs ) noexcept ; friend constexpr void swap ( basic_json_slice & lhs , basic_json_slice & rhs ) noexcept ; constexpr basic_json_slice & operator = ( json_type & j ) noexcept ; constexpr basic_json_slice & operator = ( node_type & n ) noexcept ; constexpr bool empty () const noexcept ; constexpr bool undefined () const noexcept ; constexpr bool string () const noexcept ; constexpr bool null () const noexcept ; constexpr bool boolean () const noexcept ; constexpr bool number () const noexcept ; constexpr bool object () const noexcept ; constexpr bool array () const noexcept ; constexpr bool integer () const noexcept ; constexpr bool uinteger () const noexcept ; constexpr explicit operator bool () const ; constexpr explicit operator number_type () const ; constexpr explicit operator nulljson_t () const ; constexpr explicit operator const string_type & () const & ; constexpr explicit operator const array_type & () const & ; constexpr explicit operator const object_type & () const & ; constexpr explicit operator integer_type () const ; constexpr explicit operator uinteger_type () const ; template < integral I > constexpr basic_const_json_slice operator []( I pos ) const ; constexpr basic_const_json_slice operator []( const key_string_type & k ) const ; template < class KeyStrLike > constexpr basic_const_json_slice operator []( const KeyStrLike & k ) const ; constexpr basic_const_json_slice operator []( const key_char_type * ks ) const ; constexpr void reset () noexcept ; constexpr basic_json_slice & operator = ( nulljson_t ); template < class T > constexpr basic_json_slice & operator = ( T n ); constexpr basic_json_slice & operator = ( const string_type & str ); constexpr basic_json_slice & operator = ( string_type && str ); constexpr basic_json_slice & operator = ( const char_type * str ); template < class StrLike > constexpr basic_json_slice & operator = ( const StrLike & str ); template < integral I > constexpr basic_json_slice operator []( I pos ); constexpr basic_json_slice operator []( const key_string_type & k ); template < class KeyStrLike > constexpr basic_json_slice operator []( const KeyStrLike & k ); constexpr basic_json_slice operator []( const key_char_type * ks ); private : node_type * node_ = nullptr ; // exposition only }; template < class Node , class String , class Array , class Object , bool HasInteger , bool HasUInteger , class Allocator > struct uses_allocator < basic_json_slice < Node , String , Array , Object , HasInteger , HasUInteger > , Allocator2 > : false_type {}; }
-
Every specialization of
is a trivally copyable class that modelsbasic_json_slice
.semiregular -
A
is considered valid if itsbasic_json_slice
member points to anode_
object within its lifetime.node_type
[Note: A default-constructed
is not valid. -- end note]basic_json_slice -
[Note: The default constructor of
is intentionally used for default arguments. -- end note]basic_json_slice -
Whenever
is valid, its referenced node is the object denoted bybasic_json_slice
.* node_ -
[Note: The
partial specialization indicates thatuses_allocator
should not be uses-allocator constructed. -- end note]basic_json_slice
6.1.5.1. Construction and swap
constexpr basic_json_slice ( json_type & j ) noexcept ;
-
Effects: Initializes
withnode_
.addressof ( j . node_ )
constexpr basic_json_slice ( node_type & n ) noexcept ;
-
Effects: Initializes
withnode_
.addressof ( n )
constexpr void swap ( basic_json_slice & rhs ) noexcept ; friend constexpr void swap ( basic_json_slice & lhs , basic_json_slice & rhs ) noexcept ;
-
Effects: Swaps the values of the
member of bothnode_
objects.basic_json_slice
6.1.5.2. Rebinding
constexpr basic_json_slice & operator = ( json_type & j ) noexcept ;
-
Effects: Equivalent to
.return * this = basic_json_slice { j };
constexpr basic_json_slice & operator = ( node_type & n ) noexcept ;
-
Effects: Equivalent to
.return * this = basic_json_slice { n };
6.1.5.3. Observers
constexpr bool empty () const noexcept ; constexpr bool undefined () const noexcept ; constexpr bool string () const noexcept ; constexpr bool null () const noexcept ; constexpr bool boolean () const noexcept ; constexpr bool number () const noexcept ; constexpr bool object () const noexcept ; constexpr bool array () const noexcept ; constexpr bool integer () const noexcept ; constexpr bool uinteger () const noexcept ; constexpr explicit operator bool () const ; constexpr explicit operator number_type () const ; constexpr explicit operator nulljson_t () const ; constexpr explicit operator const string_type & () const & ; constexpr explicit operator const array_type & () const & ; constexpr explicit operator const object_type & () const & ; constexpr explicit operator integer_type () const ; constexpr explicit operator uinteger_type () const ; template < integral I > constexpr basic_const_json_slice operator []( I pos ) const ; constexpr basic_const_json_slice operator []( const key_string_type & k ) const ; template < class KeyStrLike > constexpr basic_const_json_slice operator []( const KeyStrLike & k ) const ; constexpr basic_const_json_slice operator []( const key_char_type * ks ) const ;
-
Every observer function of
has the same constraints and semantics as converting thebasic_json_slice
to itsbasic_json_slice
and then calling the corresponding member function ofconst_slice_type
.basic_const_json_slice
6.1.5.4. Modifiers
-
Except for
, a modifier function ofreset
may change the state of the referenced node, but only if the node was in the undefined state. Abasic_json_slice
exception is thrown if one attempts to change the established non-undefined state of the node.json_error
constexpr void reset () noexcept ;
-
Preconditions:
is valid.* this -
Effects: Sets the referenced node to the undefined state.
constexpr basic_json_slice & operator = ( nulljson_t );
-
Preconditions:
is valid.* this -
Effects:
-
If the referenced node is in the undefined state, sets it to the null state.
-
Otherwise, if the referenced node is in the null state, does nothing.
-
-
Returns:
.* this -
Throws: A
exception constructed fromjson_error
if the referenced node is not in the undefined or the null state.json_errc :: not_undefined_or_null
template < class T > constexpr basic_json_slice & operator = ( T n );
-
Constraints:
isis_arithmetic_v < T > true
. -
Preconditions:
is valid.* this -
Effects:
-
If the referenced node is in the undefined state, performs
.* node_ = json_type { n } -
Otherwise, if
-
isT
,bool
isn true
, and the referenced node is in the true state, or -
isT
,bool
isn false
, and the referenced node is in the false state,
then does nothing.
-
-
Otherwise, if
andHasInteger
are bothsigned_integral < T > true
and the referenced node is in the signed integral state, replaces the stored signed integer value with
.n -
Otherwise, if
andHasUInteger
are bothunsigned_integral < T > true
and the referenced node is in the unsigned integral state, replaces the stored unsigned integer value with
.n -
Otherwise, the referenced node is in the floating-point state, replaces the stored floating-point value with
.static_cast < number_type > ( n ) -
Otherwise, throws a
exception constructed fromjson_error
;json_errc :: not_undefined_or_number
-
-
Returns:
.* this
constexpr basic_json_slice & operator = ( const string_type & str );
-
Preconditions:
is valid.* this -
Effects:
-
If the referenced node is in the undefined state, sets it to the string state and stores a string constructed from
.str -
Otherwise, if the referenced node is in the string state, assigns
to the stored string.str
-
-
Returns:
.* this -
Throws:
-
A
exception constructed fromjson_error
if the referenced node is not in the undefined or the string state.json_errc :: not_undefined_or_string -
Otherwise, the exception thrown by the allocation, construction, or assignment of the
.String
-
constexpr basic_json_slice & operator = ( string_type && str );
-
Preconditions:
is valid.* this -
Effects:
-
If the referenced node is in the undefined state, sets it to the string state and stores a string constructed from
.std :: move ( str ) -
Otherwise, if the referenced node is in the string state, assigns
to the stored string.std :: move ( str )
-
-
Returns:
.* this -
Throws:
-
A
exception constructed fromjson_error
if the referenced node is not in the undefined or the string state.json_errc :: not_undefined_or_string -
Otherwise, the exception thrown by the allocation, construction, or assignment of the
.String
-
constexpr basic_json_slice & operator = ( const char_type * str );
-
Preconditions:
is valid. If the referenced node is in the undefined or the string state,* this
points to a null-terminatedstr
sequence.char_type -
Effects:
-
If the referenced node is in the undefined state, sets it to the string state and stores a string containing characters from
untilstr
, wherestr + count
is the number of characters in the null-terminated sequence.count -
Otherwise, if the referenced node is in the string state, replaces the contents of
with that of the null-terminated sequence.str
-
-
Returns:
.* this -
Throws:
-
A
exception constructed fromjson_error
if the referenced node is not in the undefined or the string state.json_errc :: not_undefined_or_string -
Otherwise, the exception thrown by the allocation, construction, or assignment of the
.String
-
template < class StrLike > constexpr basic_json_slice & operator = ( const StrLike & str );
-
Constraints:
-
isconstructible_from < string_type , StrLike > true
, and -
isis_convertible_v < const KeyStrLike & , const key_char_type *> false
.
-
-
Preconditions:
is valid.* this -
Effects:
-
If the referenced node is in the undefined state, sets it to the string state and stores a string constructed from
.str -
Otherwise, if the referenced node is in the string state, assigns
to the stored string.str
-
-
Returns:
.* this -
Throws:
-
A
exception constructed fromjson_error
if the referenced node is not in the undefined or the string state.json_errc :: not_undefined_or_string -
Otherwise, the exception thrown by the allocation, construction, or assignment of the
.String
-
template < integral I > constexpr basic_json_slice operator []( I pos );
-
Preconditions:
is valid. If the referenced node is in the array state,* this
is less than the length of the stored dynamic array.pos -
Returns: A
referencing the node at offsetbasic_json_slice
of the stored dynamic array if the referenced node is in the array state.pos -
Throws: A
exception constructed fromjson_error
if the referenced node is not in the array state.json_errc :: nonarray_indexing
constexpr basic_json_slice operator []( const key_string_type & k ); template < class KeyStrLike > constexpr basic_json_slice operator []( const KeyStrLike & k ); constexpr basic_json_slice operator []( const key_char_type * ks );
-
Constraints: For the template overload:
-
is transparently comparable, andObject -
isis_convertible_v < const KeyStrLike & , const key_char_type *> false
.
-
-
Preconditions:
is valid. For the overload taking a* this
, if the referenced node is in the object state,const key_char_type *
points to a null-terminatedks
sequence.key_char_type -
For the overload taking a
, letconst key_char_type *
be ak
value containing characters in [key_string_type
,ks
), whereks + count
is the number of characters in the null-terminated sequence.count -
Effects: If the referenced node is in the object state and no element with key
is found in its stored map, inserts an element with keyk
and a value-initialized node into the stored map.k -
Returns:
-
If the referenced node is in the object state and an element with key
is found in its stored map, ak
referencing the mapped value of that element.basic_json_slice -
Otherwise, if a new element
is inserted, ae
, abasic_json_slice
the mapped value of the inserted element.basic_json_slice
-
-
Throws:
-
A
exception constructed fromjson_error
the referenced node is not in the object state.json_errc :: nonobject_indexing -
Otherwise, the exception thrown when looking up the key, or by the allocation or construction of the inserted map element.
-
6.1.6. Class template basic_const_json_slice
namespace std { template < class Node = basic_json_node <> , class String = string , class Array = vector < Node > , class Object = map < String , Node > , bool HasInteger = true, bool HasUInteger = true> class basic_const_json_slice { public : static constexpr bool has_integer = HasInteger ; static constexpr bool has_uinteger = HasUInteger ; using node_type = Node ; using object_type = Object ; using value_type = Node ; using array_type = Array ; using string_type = String ; using slice_type = basic_json_slice < Node , String , Array , Object , HasInteger , HasUInteger > ; using const_slice_type = basic_const_json_slice ; using json_type = basic_json < Node , String , Array , Object , HasInteger , HasUInteger > ; using number_type = node_type :: number_type ; using integer_type = node_type :: integer_type ; using uinteger_type = node_type :: uinteger_type ; using char_type = string_type :: value_type ; using map_node_type = object_type :: value_type ; using allocator_type = node_type :: allocator_type ; using key_string_type = object_type :: key_type ; using key_char_type = key_string_type :: value_type ; constexpr basic_const_json_slice () noexcept = default ; constexpr basic_const_json_slice ( const json_type & j ) noexcept ; constexpr basic_const_json_slice ( const node_type & n ) noexcept ; constexpr basic_const_json_slice ( const slice_type & s ) noexcept ; constexpr void swap ( basic_const_json_slice & rhs ) noexcept ; friend constexpr void swap ( basic_const_json_slice & lhs , basic_const_json_slice & rhs ) noexcept ; constexpr bool undefined () const noexcept ; constexpr bool string () const noexcept ; constexpr bool null () const noexcept ; constexpr bool boolean () const noexcept ; constexpr bool number () const noexcept ; constexpr bool object () const noexcept ; constexpr bool array () const noexcept ; constexpr bool integer () const noexcept ; constexpr bool uinteger () const noexcept ; constexpr explicit operator bool () const ; constexpr explicit operator number_type () const ; constexpr explicit operator nulljson_t () const ; constexpr explicit operator const string_type & () const & ; constexpr explicit operator const array_type & () const & ; constexpr explicit operator const object_type & () const & ; constexpr explicit operator integer_type () const ; constexpr explicit operator uinteger_type () const ; template < integral I > constexpr basic_const_json_slice operator []( I pos ) const ; constexpr basic_const_json_slice operator []( const key_string_type & k ) const ; template < class KeyStrLike > constexpr basic_const_json_slice operator []( const KeyStrLike & k ) const ; constexpr basic_const_json_slice operator []( const key_char_type * ks ) const ; private : const node_type * node_ = nullptr ; // exposition only }; template < class Node , class String , class Array , class Object , bool HasInteger , bool HasUInteger , class Allocator > struct uses_allocator < basic_const_json_slice < Node , String , Array , Object , HasInteger , HasUInteger > , Allocator2 > : false_type {}; }
-
Every specialization of
is a trivally copyable class that modelsbasic_const_json_slice
.semiregular -
A
is considered valid if itsbasic_const_json_slice
member points to anode_
object within its lifetime.node_type
[Note: A default-constructed
is not valid. -- end note]basic_const_json_slice -
[Note: The default constructor of
is intentionally used for default arguments. -- end note]basic_const_json_slice -
Whenever
is valid, its referenced node is the object denoted bybasic_const_json_slice
.* node_ -
[Note: The
partial specialization indicates thatuses_allocator
should not be uses-allocator constructed. -- end note]basic_const_json_slice
6.1.6.1. Construction and swap
constexpr basic_const_json_slice ( const json_type & j ) noexcept ;
-
Effects: Initializes
withnode_
.addressof ( j . node_ )
constexpr basic_const_json_slice ( const node_type & n ) noexcept ;
-
Effects: Initializes
withnode_
.addressof ( n )
constexpr basic_const_json_slice ( const slice_type & s ) noexcept ;
-
Effects: Initializes
withnode_
.s . node_
[Note:
and* this
reference the same node after construction ifs
is valid. -- end note]s
constexpr void swap ( basic_const_json_slice & rhs ) noexcept ; friend constexpr void swap ( basic_const_json_slice & lhs , basic_const_json_slice & rhs ) noexcept ;
-
Effects: Swaps the values of the
member of bothnode_
objects.basic_const_json_slice
6.1.6.2. Observers
constexpr bool empty () const noexcept ;
-
Returns:
.bool ( node_ )
[Note:
is not valid when* this
returnsempty () true
. -- end note]
constexpr bool undefined () const noexcept ;
-
Preconditions:
is valid.* this -
Returns:
true
if the referenced node is in the undefined state,false
otherwise.
constexpr bool string () const noexcept ;
-
Preconditions:
is valid.* this -
Returns:
true
if the referenced node is in the string state,false
otherwise.
constexpr bool null () const noexcept ;
-
Preconditions:
is valid.* this -
Returns:
true
if the referenced node is in the null state,false
otherwise.
constexpr bool boolean () const noexcept ;
-
Preconditions:
is valid.* this -
Returns:
true
if the referenced node is in the true of the false state,false
otherwise.
constexpr bool number () const noexcept ;
-
Preconditions:
is valid.* this -
Returns:
true
if the referenced node is in the number state,false
otherwise.
constexpr bool object () const noexcept ;
-
Preconditions:
is valid.* this -
Returns:
true
if the referenced node is in the object state,false
otherwise.
constexpr bool array () const noexcept ;
-
Preconditions:
is valid.* this -
Returns:
true
if the referenced node is in the array state,false
otherwise.
constexpr bool integer () const noexcept ;
-
Constraints:
isHasInteger true
. -
Preconditions:
is valid.* this -
Returns:
true
if the referenced node is in the signed integral state,false
otherwise.
constexpr bool uinteger () const noexcept ;
-
Constraints:
isHasUInteger true
. -
Preconditions:
is valid.* this -
Returns:
true
if the referenced node is in the unsigned integral state,false
otherwise.
constexpr explicit operator bool () const ;
-
Preconditions:
is valid.* this -
Returns:
true
if the referenced node is in the true state,false
if the referenced node is in the false state. -
Throws: A
exception constructed fromjson_error
if the referenced node is not in the true or the false state.json_errc :: not_boolean
constexpr explicit operator number_type () const ;
-
Preconditions:
is valid.* this -
Returns: The stored numeric value converted to
if the referenced node is in the floating-point, the signed integral, or the unsigned integral state.number_type -
Throws: A
exception constructed fromjson_error
if the referenced node is not in the floating-point, the signed integral, or the unsigned integral state.json_errc :: not_number
constexpr explicit operator nulljson_t () const ;
-
Preconditions:
is valid.* this -
Returns:
if the referenced node is in the null state.nulljson_t {} -
Throws: A
exception constructed fromjson_error
if the referenced node is not in the null state.json_errc :: not_null
constexpr explicit operator const string_type & () const & ;
-
Preconditions:
is valid.* this -
Returns: The reference to the stored string if the referenced node is in the string state.
-
Throws: A
exception constructed fromjson_error
if the referenced node is not in the string state.json_errc :: not_string
constexpr explicit operator const array_type & () const & ;
-
Preconditions:
is valid.* this -
Returns: The reference to the stored dynamic array if the referenced node is in the array state.
-
Throws: A
exception constructed fromjson_error
if the referenced node is not in the array state.json_errc :: not_array
constexpr explicit operator const object_type & () const & ;
-
Preconditions:
is valid.* this -
Returns: The reference to the stored map if the referenced node is in the object state.
-
Throws: A
exception constructed fromjson_error
if the referenced node is not in the object state.json_errc :: not_object
constexpr explicit operator integer_type () const ;
-
Constraints:
isHasInteger true
. -
Preconditions:
is valid.* this -
Returns: The stored integer value if the referenced node is in the signed integral state.
-
Throws: A
exception constructed fromjson_error
if the referenced node is not in the signed integral state.json_errc :: not_integer
constexpr explicit operator uinteger_type () const ;
-
Constraints:
isHasUInteger true
. -
Preconditions:
is valid.* this -
Returns: The stored integer value if the referenced node is in the unsigned integral state.
-
Throws: A
exception constructed fromjson_error
if the referenced node is not in the unsigned integral state.json_errc :: not_uinteger
template < integral I > constexpr basic_const_json_slice operator []( I pos ) const ;
-
Preconditions:
is valid. If the referenced node is in the array state,* this
is less than the length of the stored dynamic array.pos -
Returns: A
referencing the node at offsetbasic_const_json_slice
of the stored dynamic array if the referenced node is in the array state.pos -
Throws: A
exception constructed fromjson_error
if the referenced node is not in the array state.json_errc :: nonarray_indexing
constexpr basic_const_json_slice operator []( const key_string_type & k ) const ; template < class KeyStrLike > constexpr basic_const_json_slice operator []( const KeyStrLike & k ) const ; constexpr basic_const_json_slice operator []( const key_char_type * ks ) const ;
-
Constraints: For the template overload:
-
is transparently comparable, andObject -
isis_convertible_v < const KeyStrLike & , const key_char_type *> false
.
-
-
Preconditions:
is valid. For the overload taking a* this
, if the referenced node is in the object state,const key_char_type *
points to a null-terminatedks
sequence.key_char_type -
For the overload taking a
, letconst key_char_type *
be ak
value containing characters in [key_string_type
,ks
), whereks + count
is the number of characters in the null-terminated sequence.count -
Returns: If the referenced node is in the object state and an element with key
is found in the stored map, ak
referencing the mapped value of that element.basic_const_json_slice -
Throws:
-
A
exception constructed fromjson_error
if the referenced node is not in the object state.json_errc :: nonobject_indexing -
Otherwise, the exception thrown when looking up the key or
exception constructed fromjson_error
if no element with keyjson_errc :: key_not_found
is found.k
-
6.1.7. Error code enumeration json_errc
namespace std { enum class json_errc : unspecified { // invalid access is_empty = see below , is_undefined = see below , not_null = see below , not_boolean = see below , not_number = see below , not_integer = see below , not_uinteger = see below , not_string = see below , not_array = see below , not_object = see below , nonarray_indexing = see below , nonobject_indexing = see below , key_not_found = see below , // invalid modification not_undefined_or_null = see below , not_undefined_or_boolean = see below , not_undefined_or_number = see below , not_undefined_or_string = see below , }; }
-
The enumeration type
is used for reporting JSON-related errors.json_errc -
The underlying type of
is at least as wide asjson_errc
. Each enumerator specified above has a distinct, nonzero value.int
6.1.8. Class json_error
namespace std { class json_error : public runtime_error { public : json_error ( json_errc ec ); json_errc code () const noexcept { return code_ ; } private : json_errc code_ ; }; }
-
The class
defines the exception type that reports errors from the JSON-related operations.format_error
json_error ( json_errc ec );
-
Postconditions:
.code_ == ec
6.2. Feature-test macro
Add a new feature-test macro to [version.syn].
#define __cpp_lib_json 202 ymm L // also in <json>
7. Acknowledgements
Thanks to An Jiang for the information about
, and the suggestions for
and constructor.
Thanks to ykiko for simplifying code using fold expressions.
Thanks to YanzuoLiu for patiently reviewing the code and providing a graceful implementation of constructing objects.