= `value` JSON documents are represented in memory as instances of <<ref_value>>: a {req_Regular} type which satisfies {req_DefaultConstructible}, {req_CopyConstructible}, {req_CopyAssignable}, {req_MoveConstructible}, {req_MoveAssignable}, and many of the requirements of allocator-aware containers. It is implemented as a https://en.wikipedia.org/wiki/Tagged_union[__variant__] internally, and can dynamically store any of the six defined JSON value types:
The member function <<ref_value_kind,`value::kind`>> may be used to query the kind stored in the value. Alternatively, member functions like <<ref_value_is_object,`value::is_object`>> <<ref_value_is_number,`value::is_number`>> may be used to check whether or not the value is a particular kind:
Functions like <<ref_value_if_object,`value::if_object`>> actually return a pointer to the object if the value is an object, otherwise they return null. This allows them to be used both in boolean contexts as above, and in assignments or conditional expressions to capture the value of the pointer:
After a <<ref_value>> is constructed, its kind can change depending on what is assigned to it, or by calling functions such as <<ref_value_emplace_array,`value::emplace_array`>> or <<ref_value_emplace_bool,`value::emplace_bool`>>. If the assignment is successful, in other words it completes without any exceptions then the value is replaced. Otherwise, the value is unchanged. All operations which can fail to modify a value offer the strong exception safety guarantee.
The emplace members of <<ref_value>> return a typed reference to the underlying representation. For example, the call to <<ref_value_emplace_string,`value::emplace_string`>> in the previous example returns a <<ref_string,`string&`>>. This table shows the underlying type for each kind:
If the <<ref_kind>> of a <<ref_value>> is known, functions such as <<ref_value_as_bool,`value::as_bool`>> or <<ref_value_as_string,`value::as_string`>> may be used to obtain a reference to the underlying representation without changing the existing value:
However, as shown above these functions throw an exception if the kind in the <<ref_value>> does not match the kind denoted by the function signature. This can be used as a concise form of validation: access values as if they were the right type, but handle the resulting exception indicating if the schema of the JSON is not valid.
We can query a value for its underlying representation of a particular kind in a way that does not throw exceptions by requesting a pointer which may be null, instead of a reference. Here we use <<ref_value_if_string,`value::if_string`>> to conditionally perform an assignment without using exceptions: