WPILibC++ 2023.4.3
|
a class to store JSON values More...
#include </home/runner/work/allwpilib/allwpilib/wpiutil/src/main/native/thirdparty/json/include/wpi/json.h>
Classes | |
class | serializer |
Public Types | |
enum class | parse_event_t : uint8_t { object_start , object_end , array_start , array_end , key , value } |
parser event types More... | |
using | value_t = detail::value_t |
using | json_pointer = ::wpi::json_pointer |
template<typename T , typename SFINAE > | |
using | json_serializer = adl_serializer< T, SFINAE > |
using | initializer_list_t = std::initializer_list< detail::json_ref< json > > |
helper type for initializer lists of json values More... | |
using | parser_callback_t = std::function< bool(int depth, parse_event_t event, json &parsed)> |
per-element parser callback type More... | |
Public Member Functions | |
const char * | type_name () const noexcept |
return the type as string More... | |
Static Public Member Functions | |
static allocator_type | get_allocator () |
returns the allocator associated with the container More... | |
static json | meta () |
returns version information on the library More... | |
Friends | |
template<detail::value_t > | |
struct | detail::external_constructor |
template<typename BasicJsonType > | |
class | ::wpi::detail::iter_impl |
class | JsonTest |
exceptions | |
Classes to implement user-defined exceptions. | |
using | exception = detail::exception |
using | parse_error = detail::parse_error |
exception indicating a parse error More... | |
using | invalid_iterator = detail::invalid_iterator |
exception indicating errors with iterators More... | |
using | type_error = detail::type_error |
exception indicating executing a member function with a wrong type More... | |
using | out_of_range = detail::out_of_range |
exception indicating access out of the defined range More... | |
using | other_error = detail::other_error |
exception indicating other library errors More... | |
container types | |
The canonic container types to use json like any other STL container. | |
using | value_type = json |
the type of elements in a json container More... | |
using | reference = value_type & |
the type of an element reference More... | |
using | const_reference = const value_type & |
the type of an element const reference More... | |
using | difference_type = std::ptrdiff_t |
a type to represent differences between iterators More... | |
using | size_type = std::size_t |
a type to represent container sizes More... | |
using | allocator_type = std::allocator< json > |
the allocator type More... | |
using | pointer = json * |
the type of an element pointer More... | |
using | const_pointer = const json * |
the type of an element const pointer More... | |
using | iterator = iter_impl< json > |
an iterator for a json container More... | |
using | const_iterator = iter_impl< const json > |
a const iterator for a json container More... | |
using | reverse_iterator = json_reverse_iterator< typename json::iterator > |
a reverse iterator for a json container More... | |
using | const_reverse_iterator = json_reverse_iterator< typename json::const_iterator > |
a const reverse iterator for a json container More... | |
JSON value data types | |
The data types to store a JSON value. These types are derived from the template arguments passed to class json. | |
using | object_comparator_t = std::less<> |
using | object_t = StringMap< json > |
a type for an object More... | |
using | array_t = std::vector< json > |
a type for an array More... | |
lexicographical comparison operators | |
bool | operator== (const_reference lhs, const_reference rhs) noexcept |
comparison: equal More... | |
template<typename ScalarType , typename std::enable_if< std::is_scalar< ScalarType >::value, int >::type = 0> | |
bool | operator== (const_reference lhs, const ScalarType rhs) noexcept |
comparison: equal More... | |
template<typename ScalarType , typename std::enable_if< std::is_scalar< ScalarType >::value, int >::type = 0> | |
bool | operator== (const ScalarType lhs, const_reference rhs) noexcept |
comparison: equal More... | |
bool | operator!= (const_reference lhs, const_reference rhs) noexcept |
comparison: not equal More... | |
template<typename ScalarType , typename std::enable_if< std::is_scalar< ScalarType >::value, int >::type = 0> | |
bool | operator!= (const_reference lhs, const ScalarType rhs) noexcept |
comparison: not equal More... | |
template<typename ScalarType , typename std::enable_if< std::is_scalar< ScalarType >::value, int >::type = 0> | |
bool | operator!= (const ScalarType lhs, const_reference rhs) noexcept |
comparison: not equal More... | |
bool | operator< (const_reference lhs, const_reference rhs) noexcept |
comparison: less than More... | |
template<typename ScalarType , typename std::enable_if< std::is_scalar< ScalarType >::value, int >::type = 0> | |
bool | operator< (const_reference lhs, const ScalarType rhs) noexcept |
comparison: less than More... | |
template<typename ScalarType , typename std::enable_if< std::is_scalar< ScalarType >::value, int >::type = 0> | |
bool | operator< (const ScalarType lhs, const_reference rhs) noexcept |
comparison: less than More... | |
bool | operator<= (const_reference lhs, const_reference rhs) noexcept |
comparison: less than or equal More... | |
template<typename ScalarType , typename std::enable_if< std::is_scalar< ScalarType >::value, int >::type = 0> | |
bool | operator<= (const_reference lhs, const ScalarType rhs) noexcept |
comparison: less than or equal More... | |
template<typename ScalarType , typename std::enable_if< std::is_scalar< ScalarType >::value, int >::type = 0> | |
bool | operator<= (const ScalarType lhs, const_reference rhs) noexcept |
comparison: less than or equal More... | |
bool | operator> (const_reference lhs, const_reference rhs) noexcept |
comparison: greater than More... | |
template<typename ScalarType , typename std::enable_if< std::is_scalar< ScalarType >::value, int >::type = 0> | |
bool | operator> (const_reference lhs, const ScalarType rhs) noexcept |
comparison: greater than More... | |
template<typename ScalarType , typename std::enable_if< std::is_scalar< ScalarType >::value, int >::type = 0> | |
bool | operator> (const ScalarType lhs, const_reference rhs) noexcept |
comparison: greater than More... | |
bool | operator>= (const_reference lhs, const_reference rhs) noexcept |
comparison: greater than or equal More... | |
template<typename ScalarType , typename std::enable_if< std::is_scalar< ScalarType >::value, int >::type = 0> | |
bool | operator>= (const_reference lhs, const ScalarType rhs) noexcept |
comparison: greater than or equal More... | |
template<typename ScalarType , typename std::enable_if< std::is_scalar< ScalarType >::value, int >::type = 0> | |
bool | operator>= (const ScalarType lhs, const_reference rhs) noexcept |
comparison: greater than or equal More... | |
serialization | |
raw_ostream & | operator<< (raw_ostream &o, const json &j) |
serialize to stream More... | |
deserialization | |
raw_istream & | operator>> (raw_istream &i, json &j) |
deserialize from stream More... | |
static json | parse (std::string_view s, const parser_callback_t cb=nullptr, const bool allow_exceptions=true) |
deserialize from a compatible input More... | |
static json | parse (std::span< const uint8_t > arr, const parser_callback_t cb=nullptr, const bool allow_exceptions=true) |
static json | parse (raw_istream &i, const parser_callback_t cb=nullptr, const bool allow_exceptions=true) |
create an empty value with a given type parse(raw_istream&, const parser_callback_t) More... | |
static bool | accept (std::string_view s) |
static bool | accept (std::span< const uint8_t > arr) |
static bool | accept (raw_istream &i) |
constructors and destructors | |
Constructors of class json, copy/move constructor, copy assignment, static functions creating objects, and the destructor. | |
static json | array (initializer_list_t init={}) |
explicitly create an array from an initializer list More... | |
static json | object (initializer_list_t init={}) |
explicitly create an object from an initializer list More... | |
json (const value_t v) | |
create an empty value with a given type More... | |
json (std::nullptr_t=nullptr) noexcept | |
create a null object More... | |
template<typename CompatibleType , typename U = detail::uncvref_t<CompatibleType>, detail::enable_if_t< detail::is_compatible_type< json_t, U >::value, int > = 0> | |
json (CompatibleType &&val) noexcept(noexcept(adl_serializer< U, void >::to_json(std::declval< json_t & >(), std::forward< CompatibleType >(val)))) | |
create a JSON value More... | |
json (initializer_list_t init, bool type_deduction=true, value_t manual_type=value_t::array) | |
create a container (array or object) from an initializer list More... | |
json (size_type cnt, const json &val) | |
construct an array with count copies of given value More... | |
template<class InputIT , typename std::enable_if< std::is_same< InputIT, typename json_t::iterator >::value or std::is_same< InputIT, typename json_t::const_iterator >::value, int >::type = 0> | |
json (InputIT first, InputIT last) | |
construct a JSON container given an iterator range More... | |
json (const json &other) | |
copy constructor More... | |
json (json &&other) noexcept | |
move constructor More... | |
reference & | operator= (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 More... | |
~json () noexcept | |
destructor More... | |
binary serialization/deserialization support | |
static std::vector< uint8_t > | to_cbor (const json &j) |
create a CBOR serialization of a given JSON value More... | |
static std::span< uint8_t > | to_cbor (const json &j, std::vector< uint8_t > &buf) |
static std::span< uint8_t > | to_cbor (const json &j, SmallVectorImpl< uint8_t > &buf) |
static void | to_cbor (raw_ostream &os, const json &j) |
static std::vector< uint8_t > | to_msgpack (const json &j) |
create a MessagePack serialization of a given JSON value More... | |
static std::span< uint8_t > | to_msgpack (const json &j, std::vector< uint8_t > &buf) |
static std::span< uint8_t > | to_msgpack (const json &j, SmallVectorImpl< uint8_t > &buf) |
static void | to_msgpack (raw_ostream &os, const json &j) |
static std::vector< uint8_t > | to_ubjson (const json &j, const bool use_size=false, const bool use_type=false) |
create a UBJSON serialization of a given JSON value More... | |
static std::span< uint8_t > | to_ubjson (const json &j, std::vector< uint8_t > &buf, const bool use_size=false, const bool use_type=false) |
static std::span< uint8_t > | to_ubjson (const json &j, SmallVectorImpl< uint8_t > &buf, const bool use_size=false, const bool use_type=false) |
static void | to_ubjson (raw_ostream &os, const json &j, const bool use_size=false, const bool use_type=false) |
static json | from_cbor (raw_istream &is, const bool strict=true) |
create a JSON value from an input in CBOR format More... | |
static json | from_cbor (std::span< const uint8_t > arr, const bool strict=true) |
create a JSON value from an input in CBOR format More... | |
static json | from_msgpack (raw_istream &is, const bool strict=true) |
create a JSON value from an input in MessagePack format More... | |
static json | from_msgpack (std::span< const uint8_t > arr, const bool strict=true) |
create a JSON value from an input in MessagePack format More... | |
static json | from_ubjson (raw_istream &is, const bool strict=true) |
create a JSON value from an input in UBJSON format More... | |
static json | from_ubjson (std::span< const uint8_t > arr, const bool strict=true) |
JSON Patch functions | |
static json | diff (const json &source, const json &target, const std::string &path="") |
creates a diff as a JSON patch More... | |
json | patch (const json &json_patch) const |
applies a JSON patch More... | |
object inspection | |
Functions to inspect the type of a JSON value. | |
std::string | dump (const int indent=-1, const char indent_char=' ', const bool ensure_ascii=false) const |
serialization More... | |
void | dump (raw_ostream &os, int indent=-1, const char indent_char=' ', const bool ensure_ascii=false) const |
value_t | type () const noexcept |
return the type of the JSON value (explicit) More... | |
bool | is_primitive () const noexcept |
return whether type is primitive More... | |
bool | is_structured () const noexcept |
return whether type is structured More... | |
bool | is_null () const noexcept |
return whether value is null More... | |
bool | is_boolean () const noexcept |
return whether value is a boolean More... | |
bool | is_number () const noexcept |
return whether value is a number More... | |
bool | is_number_integer () const noexcept |
return whether value is an integer number More... | |
bool | is_number_unsigned () const noexcept |
return whether value is an unsigned integer number More... | |
bool | is_number_float () const noexcept |
return whether value is a floating-point number More... | |
bool | is_object () const noexcept |
return whether value is an object More... | |
bool | is_array () const noexcept |
return whether value is an array More... | |
bool | is_string () const noexcept |
return whether value is a string More... | |
bool | is_discarded () const noexcept |
return whether value is discarded More... | |
operator value_t () const noexcept | |
return the type of the JSON value (implicit) More... | |
value access | |
Direct access to the stored value of a JSON value. | |
template<typename BasicJsonType , detail::enable_if_t< std::is_same< typename std::remove_const< BasicJsonType >::type, json_t >::value, int > = 0> | |
json | get () const |
get special-case overload More... | |
template<typename ValueTypeCV , typename ValueType = detail::uncvref_t<ValueTypeCV>, detail::enable_if_t< not detail::is_json< ValueType >::value and detail::has_from_json< json_t, ValueType >::value and not detail::has_non_default_from_json< json_t, ValueType >::value, int > = 0> | |
ValueType | get () const noexcept(noexcept(adl_serializer< ValueType, void >::from_json(std::declval< const json_t & >(), std::declval< ValueType & >()))) |
get a value (explicit) More... | |
template<typename ValueTypeCV , typename ValueType = detail::uncvref_t<ValueTypeCV>, detail::enable_if_t< not std::is_same< json_t, ValueType >::value and detail::has_non_default_from_json< json_t, ValueType >::value, int > = 0> | |
ValueType | get () const noexcept(noexcept(adl_serializer< ValueTypeCV, void >::from_json(std::declval< const json_t & >()))) |
get a value (explicit); special case More... | |
template<typename PointerType , typename std::enable_if< std::is_pointer< PointerType >::value, int >::type = 0> | |
PointerType | get () noexcept |
get a pointer value (explicit) More... | |
template<typename PointerType , typename std::enable_if< std::is_pointer< PointerType >::value, int >::type = 0> | |
const PointerType | get () const noexcept |
get a pointer value (explicit) More... | |
template<typename PointerType , typename std::enable_if< std::is_pointer< PointerType >::value, int >::type = 0> | |
PointerType | get_ptr () noexcept |
get a pointer value (implicit) More... | |
template<typename PointerType , typename std::enable_if< std::is_pointer< PointerType >::value and std::is_const< typename std::remove_pointer< PointerType >::type >::value, int >::type = 0> | |
const PointerType | get_ptr () const noexcept |
get a pointer value (implicit) More... | |
template<typename ReferenceType , typename std::enable_if< std::is_reference< ReferenceType >::value, int >::type = 0> | |
ReferenceType | get_ref () |
get a reference value (implicit) More... | |
template<typename ReferenceType , typename std::enable_if< std::is_reference< ReferenceType >::value and std::is_const< typename std::remove_reference< ReferenceType >::type >::value, int >::type = 0> | |
ReferenceType | get_ref () const |
get a reference value (implicit) More... | |
template<typename ValueType , typename std::enable_if< not std::is_pointer< ValueType >::value and not std::is_same< ValueType, detail::json_ref< json > >::value and not std::is_same< ValueType, std::string::value_type >::value and not detail::is_json< ValueType >::value and not std::is_same< ValueType, std::initializer_list< std::string::value_type > >::value and not std::is_same< ValueType, typename std::string_view >::value, int >::type = 0> | |
operator ValueType () const | |
get a value (implicit) More... | |
element access | |
Access to the JSON value. | |
reference | at (size_type idx) |
access specified array element with bounds checking More... | |
const_reference | at (size_type idx) const |
access specified array element with bounds checking More... | |
reference | at (std::string_view key) |
access specified object element with bounds checking More... | |
const_reference | at (std::string_view key) const |
access specified object element with bounds checking More... | |
reference | operator[] (size_type idx) |
access specified array element More... | |
const_reference | operator[] (size_type idx) const |
access specified array element More... | |
reference | operator[] (std::string_view key) |
access specified object element More... | |
const_reference | operator[] (std::string_view key) const |
read-only access specified object element More... | |
template<typename T > | |
reference | operator[] (T *key) |
access specified object element More... | |
template<typename T > | |
const_reference | operator[] (T *key) const |
read-only access specified object element More... | |
template<class ValueType , typename std::enable_if< std::is_convertible< json_t, ValueType >::value, int >::type = 0> | |
ValueType | value (std::string_view key, const ValueType &default_value) const |
access specified object element with default value More... | |
std::string | value (std::string_view key, const char *default_value) const |
overload for a default value of type const char* More... | |
template<class ValueType , typename std::enable_if< std::is_convertible< json_t, ValueType >::value, int >::type = 0> | |
ValueType | value (const json_pointer &ptr, const ValueType &default_value) const |
access specified object element via JSON Pointer with default value More... | |
std::string | value (const json_pointer &ptr, const char *default_value) const |
overload for a default value of type const char* More... | |
reference | front () |
access the first element More... | |
const_reference | front () const |
access the first element More... | |
reference | back () |
access the last element More... | |
const_reference | back () const |
access the last element More... | |
template<class IteratorType , typename std::enable_if< std::is_same< IteratorType, typename json_t::iterator >::value or std::is_same< IteratorType, typename json_t::const_iterator >::value, int >::type = 0> | |
void | erase (IteratorType pos) |
remove element given an iterator More... | |
template<class IteratorType , typename std::enable_if< std::is_same< IteratorType, typename json_t::iterator >::value or std::is_same< IteratorType, typename json_t::const_iterator >::value, int >::type = 0> | |
IteratorType | erase (IteratorType first, IteratorType last) |
remove elements given an iterator range More... | |
size_type | erase (std::string_view key) |
remove element from a JSON object given a key More... | |
void | erase (const size_type idx) |
remove element from a JSON array given an index More... | |
lookup | |
iterator | find (std::string_view key) |
find an element in a JSON object More... | |
const_iterator | find (std::string_view key) const |
find an element in a JSON object More... | |
size_type | count (std::string_view key) const |
returns the number of occurrences of a key in a JSON object More... | |
iterators | |
iterator | begin () noexcept |
returns an iterator to the first element More... | |
const_iterator | begin () const noexcept |
returns a const iterator to the first element More... | |
const_iterator | cbegin () const noexcept |
returns a const iterator to the first element More... | |
iterator | end () noexcept |
returns an iterator to one past the last element More... | |
const_iterator | end () const noexcept |
returns a const iterator to one past the last element More... | |
const_iterator | cend () const noexcept |
returns a const iterator to one past the last element More... | |
reverse_iterator | rbegin () noexcept |
returns an iterator to the reverse-beginning More... | |
const_reverse_iterator | rbegin () const noexcept |
returns a const reverse iterator to the last element More... | |
reverse_iterator | rend () noexcept |
returns an iterator to the reverse-end More... | |
const_reverse_iterator | rend () const noexcept |
returns a const reverse iterator to one before the first More... | |
const_reverse_iterator | crbegin () const noexcept |
returns a const reverse iterator to the last element More... | |
const_reverse_iterator | crend () const noexcept |
returns a const reverse iterator to one before the first More... | |
iteration_proxy< iterator > | items () noexcept |
helper to access iterator member functions in range-based for More... | |
iteration_proxy< const_iterator > | items () const noexcept |
helper to access iterator member functions in range-based for More... | |
capacity | |
bool | empty () const noexcept |
checks whether the container is empty. More... | |
size_type | size () const noexcept |
returns the number of elements More... | |
size_type | max_size () const noexcept |
returns the maximum possible number of elements More... | |
modifiers | |
void | clear () noexcept |
clears the contents More... | |
void | push_back (json &&val) |
add an object to an array More... | |
reference | operator+= (json &&val) |
add an object to an array More... | |
void | push_back (const json &val) |
add an object to an array More... | |
reference | operator+= (const json &val) |
add an object to an array More... | |
template<typename T , typename U > | |
void | push_back (const std::pair< T, U > &val) |
add an object to an object More... | |
template<typename T , typename U > | |
reference | operator+= (const std::pair< T, U > &val) |
add an object to an object More... | |
void | push_back (initializer_list_t init) |
add an object to an object More... | |
reference | operator+= (initializer_list_t init) |
add an object to an object More... | |
template<class... Args> | |
void | emplace_back (Args &&... args) |
add an object to an array More... | |
template<class... Args> | |
std::pair< iterator, bool > | emplace (std::string_view key, Args &&... args) |
add an object to an object if key does not exist More... | |
iterator | insert (const_iterator pos, const json &val) |
inserts element More... | |
iterator | insert (const_iterator pos, json &&val) |
inserts element More... | |
iterator | insert (const_iterator pos, size_type cnt, const json &val) |
inserts elements More... | |
iterator | insert (const_iterator pos, const_iterator first, const_iterator last) |
inserts elements More... | |
iterator | insert (const_iterator pos, initializer_list_t ilist) |
inserts elements More... | |
void | insert (const_iterator first, const_iterator last) |
inserts elements More... | |
void | update (const_reference j) |
updates a JSON object from another object, overwriting existing keys More... | |
void | update (const_iterator first, const_iterator last) |
updates a JSON object from another object, overwriting existing keys More... | |
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 More... | |
void | swap (array_t &other) |
exchanges the values More... | |
void | swap (object_t &other) |
exchanges the values More... | |
void | swap (std::string &other) |
exchanges the values More... | |
JSON Pointer functions | |
reference | operator[] (const json_pointer &ptr) |
access specified element via JSON Pointer More... | |
const_reference | operator[] (const json_pointer &ptr) const |
access specified element via JSON Pointer More... | |
reference | at (const json_pointer &ptr) |
access specified element via JSON Pointer More... | |
const_reference | at (const json_pointer &ptr) const |
access specified element via JSON Pointer More... | |
json | flatten () const |
return flattened JSON value More... | |
json | unflatten () const |
unflatten a previously flattened JSON value More... | |
JSON Merge Patch functions | |
void | merge_patch (const json &patch) |
applies a JSON Merge Patch More... | |
a class to store JSON values
@requirement The class satisfies the following concept requirements:
==
, see operator==(const_reference,const_reference).<
, see operator<(const_reference,const_reference).std::nullptr_t
objects which are used to model the null
value.m_type == value_t::object
, then m_value.object != nullptr
.m_type == value_t::array
, then m_value.array != nullptr
.m_type == value_t::string
, then m_value.string != nullptr
. The invariants are checked by member function assert_invariant().using wpi::json::allocator_type = std::allocator<json> |
the allocator type
using wpi::json::array_t = std::vector<json> |
a type for an array
RFC 7159 describes JSON arrays as follows:
An array is an ordered sequence of zero or more values.
RFC 7159 specifies:
An implementation may set limits on the maximum depth of nesting.
In this class, the array's limit of nesting is not explicitly constrained. However, a maximum depth of nesting may be introduced by the compiler or runtime environment. A theoretical limit can be queried by calling the max_size function of a JSON array.
Arrays are stored as pointers in a json type. That is, for any access to array values, a pointer of type array_t*
must be dereferenced.
using wpi::json::const_iterator = iter_impl<const json> |
a const iterator for a json container
using wpi::json::const_pointer = const json* |
the type of an element const pointer
using wpi::json::const_reference = const value_type& |
the type of an element const reference
using wpi::json::const_reverse_iterator = json_reverse_iterator<typename json::const_iterator> |
a const reverse iterator for a json container
using wpi::json::difference_type = std::ptrdiff_t |
a type to represent differences between iterators
using wpi::json::initializer_list_t = std::initializer_list<detail::json_ref<json> > |
helper type for initializer lists of json values
exception indicating errors with iterators
This exception is thrown if iterators passed to a library function do not match the expected semantics.
Exceptions have ids 2xx.
name / id | example message | description |
---|---|---|
json.exception.invalid_iterator.201 | iterators are not compatible | The iterators passed to constructor json(InputIT first, InputIT last) are not compatible, meaning they do not belong to the same container. Therefore, the range (first, last) is invalid. |
json.exception.invalid_iterator.202 | iterator does not fit current value | In an erase or insert function, the passed iterator pos does not belong to the JSON value for which the function was called. It hence does not define a valid position for the deletion/insertion. |
json.exception.invalid_iterator.203 | iterators do not fit current value | Either iterator passed to function erase(IteratorType first, IteratorType last) does not belong to the JSON value from which values shall be erased. It hence does not define a valid range to delete values from. |
json.exception.invalid_iterator.204 | iterators out of range | When an iterator range for a primitive type (number, boolean, or string) is passed to a constructor or an erase function, this range has to be exactly (begin(), end()), because this is the only way the single stored value is expressed. All other ranges are invalid. |
json.exception.invalid_iterator.205 | iterator out of range | When an iterator for a primitive type (number, boolean, or string) is passed to an erase function, the iterator has to be the begin() iterator, because it is the only way to address the stored value. All other iterators are invalid. |
json.exception.invalid_iterator.206 | cannot construct with iterators from null | The iterators passed to constructor json(InputIT first, InputIT last) belong to a JSON null value and hence to not define a valid range. |
json.exception.invalid_iterator.207 | cannot use key() for non-object iterators | The key() member function can only be used on iterators belonging to a JSON object, because other types do not have a concept of a key. |
json.exception.invalid_iterator.208 | cannot use operator[] for object iterators | The operator[] to specify a concrete offset cannot be used on iterators belonging to a JSON object, because JSON objects are unordered. |
json.exception.invalid_iterator.209 | cannot use offsets with object iterators | The offset operators (+, -, +=, -=) cannot be used on iterators belonging to a JSON object, because JSON objects are unordered. |
json.exception.invalid_iterator.210 | iterators do not fit | The iterator range passed to the insert function are not compatible, meaning they do not belong to the same container. Therefore, the range (first, last) is invalid. |
json.exception.invalid_iterator.211 | passed iterators may not belong to container | The iterator range passed to the insert function must not be a subrange of the container to insert to. |
json.exception.invalid_iterator.212 | cannot compare iterators of different containers | When two iterators are compared, they must belong to the same container. |
json.exception.invalid_iterator.213 | cannot compare order of object iterators | The order of object iterators cannot be compared, because JSON objects are unordered. |
json.exception.invalid_iterator.214 | cannot get value | Cannot get value for iterator: Either the iterator belongs to a null value or it is an iterator to a primitive type (number, boolean, or string), but the iterator is different to begin(). |
@liveexample{The following code shows how an invalid_iterator
exception can be caught.,invalid_iterator}
using wpi::json::iterator = iter_impl<json> |
an iterator for a json container
using wpi::json::json_serializer = adl_serializer<T, SFINAE> |
using wpi::json::object_comparator_t = std::less<> |
using wpi::json::object_t = StringMap<json> |
a type for an object
RFC 7159 describes JSON objects as follows:
An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.
The choice of object_t influences the behavior of the JSON class. With the default type, objects have the following behavior:
{"key": 2, "key": 1}
could be equal to either {"key": 1}
or {"key": 2}
.{"b": 1, "a": 2}
and {"a": 2, "b": 1}
will be stored and serialized as {"a": 2, "b": 1}
.{"b": 1, "a": 2}
and {"a": 2, "b": 1}
will be treated as equal.RFC 7159 specifies:
An implementation may set limits on the maximum depth of nesting.
In this class, the object's limit of nesting is not explicitly constrained. However, a maximum depth of nesting may be introduced by the compiler or runtime environment. A theoretical limit can be queried by calling the max_size function of a JSON object.
Objects are stored as pointers in a json type. That is, for any access to object values, a pointer of type object_t*
must be dereferenced.
std::map
with std::less
is used by default. Please note this behavior conforms to RFC 7159, because any order implements the specified "unordered" nature of JSON objects. exception indicating other library errors
This exception is thrown in case of errors that cannot be classified with the other exception types.
Exceptions have ids 5xx.
name / id | example message | description |
---|---|---|
json.exception.other_error.501 | unsuccessful: {"op":"test","path":"/baz", "value":"bar"} | A JSON Patch operation 'test' failed. The unsuccessful operation is also printed. |
@liveexample{The following code shows how an other_error
exception can be caught.,other_error}
exception indicating access out of the defined range
This exception is thrown in case a library function is called on an input parameter that exceeds the expected range, for instance in case of array indices or nonexisting object keys.
Exceptions have ids 4xx.
name / id | example message | description |
---|---|---|
json.exception.out_of_range.401 | array index 3 is out of range | The provided array index i is larger than size-1. |
json.exception.out_of_range.402 | array index '-' (3) is out of range | The special array index - in a JSON Pointer never describes a valid element of the array, but the index past the end. That is, it can only be used to add elements at this position, but not to read it. |
json.exception.out_of_range.403 | key 'foo' not found | The provided key was not found in the JSON object. |
json.exception.out_of_range.404 | unresolved reference token 'foo' | A reference token in a JSON Pointer could not be resolved. |
json.exception.out_of_range.405 | JSON pointer has no parent | The JSON Patch operations 'remove' and 'add' can not be applied to the root element of the JSON value. |
json.exception.out_of_range.406 | number overflow parsing '10E1000' | A parsed number could not be stored as without changing it to NaN or INF. |
json.exception.out_of_range.407 | number overflow serializing '9223372036854775808' | UBJSON only supports integers numbers up to 9223372036854775807. |
json.exception.out_of_range.408 | excessive array size: 8658170730974374167 | The size (following # ) of an UBJSON array or object exceeds the maximal capacity. |
@liveexample{The following code shows how an out_of_range
exception can be caught.,out_of_range}
exception indicating a parse error
This exception is thrown by the library when a parse error occurs. Parse errors can occur during the deserialization of JSON text, CBOR, MessagePack, as well as when using JSON Patch.
Member byte holds the byte index of the last read character in the input file.
Exceptions have ids 1xx.
name / id | example message | description |
---|---|---|
json.exception.parse_error.101 | parse error at 2: unexpected end of input; expected string literal | This error indicates a syntax error while deserializing a JSON text. The error message describes that an unexpected token (character) was encountered, and the member byte indicates the error position. |
json.exception.parse_error.102 | parse error at 14: missing or wrong low surrogate | JSON uses the \uxxxx format to describe Unicode characters. Code points above above 0xFFFF are split into two \uxxxx entries ("surrogate pairs"). This error indicates that the surrogate pair is incomplete or contains an invalid code point. |
json.exception.parse_error.103 | parse error: code points above 0x10FFFF are invalid | Unicode supports code points up to 0x10FFFF. Code points above 0x10FFFF are invalid. |
json.exception.parse_error.104 | parse error: JSON patch must be an array of objects | RFC 6902 requires a JSON Patch document to be a JSON document that represents an array of objects. |
json.exception.parse_error.105 | parse error: operation must have string member 'op' | An operation of a JSON Patch document must contain exactly one "op" member, whose value indicates the operation to perform. Its value must be one of "add", "remove", "replace", "move", "copy", or "test"; other values are errors. |
json.exception.parse_error.106 | parse error: array index '01' must not begin with '0' | An array index in a JSON Pointer (RFC 6901) may be 0 or any number without a leading 0 . |
json.exception.parse_error.107 | parse error: JSON pointer must be empty or begin with '/' - was: 'foo' | A JSON Pointer must be a Unicode string containing a sequence of zero or more reference tokens, each prefixed by a / character. |
json.exception.parse_error.108 | parse error: escape character '~' must be followed with '0' or '1' | In a JSON Pointer, only ~0 and ~1 are valid escape sequences. |
json.exception.parse_error.109 | parse error: array index 'one' is not a number | A JSON Pointer array index must be a number. |
json.exception.parse_error.110 | parse error at 1: cannot read 2 bytes from vector | When parsing CBOR or MessagePack, the byte vector ends before the complete value has been read. |
json.exception.parse_error.112 | parse error at 1: error reading CBOR; last byte: 0xF8 | Not all types of CBOR or MessagePack are supported. This exception occurs if an unsupported byte was read. |
json.exception.parse_error.113 | parse error at 2: expected a CBOR string; last byte: 0x98 | While parsing a map key, a value that is not a string has been read. |
@liveexample{The following code shows how a parse_error
exception can be caught.,parse_error}
using wpi::json::parser_callback_t = std::function<bool(int depth, parse_event_t event, json& parsed)> |
per-element parser callback type
With a parser callback function, the result of parsing a JSON text can be influenced. When passed to parse, it is called on certain events (passed as parse_event_t via parameter event) with a set recursion depth depth and context JSON value parsed. The return value of the callback function is a boolean indicating whether the element that emitted the callback shall be kept or not.
We distinguish six scenarios (determined by the event type) in which the callback function can be called. The following table describes the values of the parameters depth, event, and parsed.
parameter event | description | parameter depth | parameter parsed |
---|---|---|---|
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 |
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 |
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 |
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 |
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 |
parse_event_t::value | the parser finished reading a JSON value | depth of the value | the parsed JSON value |
Discarding a value (i.e., returning false
) has different effects depending on the context in which function was called:
null
. This case happens if the top-level element is skipped.[in] | depth | the depth of the recursion during parsing |
[in] | event | an event of type parse_event_t indicating the context in the callback function has been called |
[in,out] | parsed | the current intermediate parse result; note that writing to this value has no effect for parse_event_t::key events |
true
) or not (false
). In the latter case, it is either skipped completely or replaced by an empty discarded object.using wpi::json::pointer = json* |
the type of an element pointer
using wpi::json::reference = value_type& |
the type of an element reference
using wpi::json::reverse_iterator = json_reverse_iterator<typename json::iterator> |
a reverse iterator for a json container
using wpi::json::size_type = std::size_t |
a type to represent container sizes
exception indicating executing a member function with a wrong type
This exception is thrown in case of a type error; that is, a library function is executed on a JSON value whose type does not match the expected semantics.
Exceptions have ids 3xx.
name / id | example message | description |
---|---|---|
json.exception.type_error.301 | cannot create object from initializer list | To create an object from an initializer list, the initializer list must consist only of a list of pairs whose first element is a string. When this constraint is violated, an array is created instead. |
json.exception.type_error.302 | type must be object, but is array | During implicit or explicit value conversion, the JSON type must be compatible to the target type. For instance, a JSON string can only be converted into string types, but not into numbers or boolean types. |
json.exception.type_error.303 | incompatible ReferenceType for get_ref, actual type is object | To retrieve a reference to a value stored in a json object with get_ref, the type of the reference must match the value type. For instance, for a JSON array, the ReferenceType must be array_t&. |
json.exception.type_error.304 | cannot use at() with string | The at() member functions can only be executed for certain JSON types. |
json.exception.type_error.305 | cannot use operator[] with string | The operator[] member functions can only be executed for certain JSON types. |
json.exception.type_error.306 | cannot use value() with string | The value() member functions can only be executed for certain JSON types. |
json.exception.type_error.307 | cannot use erase() with string | The erase() member functions can only be executed for certain JSON types. |
json.exception.type_error.308 | cannot use push_back() with string | The push_back() and operator+= member functions can only be executed for certain JSON types. |
json.exception.type_error.309 | cannot use insert() with | The insert() member functions can only be executed for certain JSON types. |
json.exception.type_error.310 | cannot use swap() with number | The swap() member functions can only be executed for certain JSON types. |
json.exception.type_error.311 | cannot use emplace_back() with string | The emplace_back() member function can only be executed for certain JSON types. |
json.exception.type_error.312 | cannot use update() with string | The update() member functions can only be executed for certain JSON types. |
json.exception.type_error.313 | invalid value to unflatten | The unflatten function converts an object whose keys are JSON Pointers back into an arbitrary nested JSON value. The JSON Pointers must not overlap, because then the resulting value would not be well defined. |
json.exception.type_error.314 | only objects can be unflattened | The unflatten function only works for an object whose keys are JSON Pointers. |
json.exception.type_error.315 | values in object must be primitive | The unflatten function only works for an object whose keys are JSON Pointers and whose values are primitive. |
json.exception.type_error.316 | invalid UTF-8 byte at index 10: 0x7E | The dump function only works with UTF-8 encoded strings; that is, if you assign a std::string to a JSON value, make sure it is UTF-8 encoded. |
@liveexample{The following code shows how a type_error
exception can be caught.,type_error}
using wpi::json::value_t = detail::value_t |
using wpi::json::value_type = json |
the type of elements in a json container
|
strong |
parser event types
The parser callback distinguishes the following events:
object_start
: the parser read {
and started to process a JSON objectkey
: the parser read a key of a value in an objectobject_end
: the parser read }
and finished processing a JSON objectarray_start
: the parser read [
and started to process a JSON arrayarray_end
: the parser read ]
and finished processing a JSON arrayvalue
: the parser finished reading a JSON value
|
inline |
create an empty value with a given type
Create an empty JSON value with a given type. The value will be default initialized with an empty value which depends on the type:
Value type | initial value |
---|---|
null | null |
boolean | false |
string | "" |
number | 0 |
object | {} |
array | [] |
[in] | v | the type of the value to create |
@complexity Constant.
@exceptionsafety Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
@liveexample{The following code shows the constructor for different value_t values,json__value_t}
|
inlinenoexcept |
create a null object
Create a null
JSON value. It either takes a null pointer as parameter (explicitly creating null
) or no parameter (implicitly creating null
). The passed null pointer itself is not read – it is only used to choose the right constructor.
@complexity Constant.
@exceptionsafety No-throw guarantee: this constructor never throws exceptions.
@liveexample{The following code shows the constructor with and without a null pointer parameter.,json__nullptr_t}
|
inlinenoexcept |
create a JSON value
This is a "catch all" constructor for all compatible JSON types; that is, types for which a to_json()
method exists. The constructor forwards the parameter val to that method (to json_serializer<U>::to_json
method with U = uncvref_t<CompatibleType>
, to be exact).
Template type CompatibleType includes, but is not limited to, the following types:
std::vector
, std::deque
, std::list
, std::array
, std::set
, std::unordered_set
, std::multiset
, and std::unordered_multiset
with a value_type
from which a json value can be constructed.std::map
, std::unordered_map
, std::multimap
, and std::unordered_multimap
with a key_type
compatible to std::string
and a value_type
from which a json value can be constructed.std::string
, string literals, and all compatible string containers can be used.int
, size_t
, int64_t
, float
or double
can be used.bool
can be used.See the examples below.
CompatibleType | a type such that:
|
U | = uncvref_t<CompatibleType> |
[in] | val | the value to be forwarded to the respective constructor |
@complexity Usually linear in the size of the passed val, also depending on the implementation of the called to_json()
method.
@exceptionsafety Depends on the called constructor. For types directly supported by the library (i.e., all types for which no to_json()
function was provided), strong guarantee holds: if an exception is thrown, there are no changes to any JSON value.
@liveexample{The following code shows the constructor with several compatible types.,json__CompatibleType}
wpi::json::json | ( | initializer_list_t | init, |
bool | type_deduction = true , |
||
value_t | manual_type = value_t::array |
||
) |
create a container (array or object) from an initializer list
Creates a JSON value of type array or object from the passed initializer list init. In case type_deduction is true
(default), the type of the JSON value to be created is deducted from the initializer list init according to the following rules:
{}
is created.The rules aim to create the best fit between a C++ initializer list and JSON values. The rationale is as follows:
{}
which is exactly an empty JSON object.With the rules described above, the following JSON values cannot be expressed by an initializer list:
[]
): use array(initializer_list_t) with an empty initializer list in this case[in] | init | initializer list with JSON values |
[in] | type_deduction | internal parameter; when set to true , the type of the JSON value is deducted from the initializer list init; when set to false , the type provided via manual_type is forced. This mode is used by the functions array(initializer_list_t) and object(initializer_list_t). |
[in] | manual_type | internal parameter; when type_deduction is set to false , the created JSON value will use the provided type (only value_t::array and value_t::object are valid); when type_deduction is set to true , this parameter has no effect |
type_error.301 | if type_deduction is false , manual_type is value_t::object , but init contains an element which is not a pair whose first element is a string. In this case, the constructor could not create an object. If type_deduction would have be true , an array would have been created. See object(initializer_list_t) for an example. |
@complexity Linear in the size of the initializer list init.
@exceptionsafety Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
@liveexample{The example below shows how JSON values are created from initializer lists.,json__list_init_t}
construct an array with count copies of given value
Constructs a JSON array value by creating cnt copies of a passed value. In case cnt is 0
, an empty array is created.
[in] | cnt | the number of JSON copies of val to create |
[in] | val | the JSON value to copy |
@complexity Linear in cnt.
@exceptionsafety Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
@liveexample{The following code shows examples for the json(size_type\, const json&) constructor.,json__size_type_json}
|
inline |
construct a JSON container given an iterator range
Constructs the JSON value with the contents of the range [first, last)
. The semantics depends on the different types a JSON value can have:
begin()
and last must be end()
. In this case, the value is copied. Otherwise, invalid_iterator.204 is thrown.std::vector
or std::map
; that is, a JSON array or object is constructed from the values in the range.InputIT | an input iterator type (iterator or const_iterator) |
[in] | first | begin of the range to copy from (included) |
[in] | last | end of the range to copy from (excluded) |
[first, last)
is valid. Usually, this precondition cannot be checked efficiently. Only certain edge cases are detected; see the description of the exceptions below. A violation of this precondition yields undefined behavior.std::abort
if this precondition is not met. Assertions can be disabled by defining NDEBUG
at compile time. See http://en.cppreference.com/w/cpp/error/assert for more information.invalid_iterator.201 | if iterators first and last are not compatible (i.e., do not belong to the same JSON value). In this case, the range [first, last) is undefined. |
invalid_iterator.204 | if iterators first and last belong to a primitive type (number, boolean, or string), but first does not point to the first element any more. In this case, the range [first, last) is undefined. See example code below. |
invalid_iterator.206 | if iterators first and last belong to a null value. In this case, the range [first, last) is undefined. |
@complexity Linear in distance between first and last.
@exceptionsafety Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
@liveexample{The example below shows several ways to create JSON values by specifying a subrange with iterators.,json__InputIt_InputIt}
wpi::json::json | ( | const json & | other | ) |
copy constructor
Creates a copy of a given JSON value.
[in] | other | the JSON value to copy |
*this == other
@complexity Linear in the size of other.
@exceptionsafety Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
@requirement This function helps json
satisfying the Container requirements:
other == json(other)
.@liveexample{The following code shows an example for the copy constructor.,json__json}
|
inlinenoexcept |
move constructor
Move constructor. Constructs a JSON value with the contents of the given value other using move semantics. It "steals" the resources from other and leaves it as JSON null value.
[in,out] | other | value to move to this object |
*this
has the same value as other before the call. @complexity Constant.
@exceptionsafety No-throw guarantee: this constructor never throws exceptions.
@requirement This function helps json
satisfying the MoveConstructible requirements.
@liveexample{The code below shows the move constructor explicitly called via std::move.,json__moveconstructor}
|
inlinenoexcept |
destructor
Destroys the JSON value and frees all allocated memory.
@complexity Linear.
@requirement This function helps json
satisfying the Container requirements:
|
static |
|
static |
|
static |
|
inlinestatic |
explicitly create an array from an initializer list
Creates a JSON array value from a given initializer list. That is, given a list of values a, b, c
, creates the JSON value [a, b, c]
. If the initializer list is empty, the empty array []
is created.
[in] | init | initializer list with JSON values to create an array from (optional) |
@complexity Linear in the size of init.
@exceptionsafety Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
@liveexample{The following code shows an example for the array
function.,array}
|
inline |
access specified element via JSON Pointer
Returns a reference to the element at with specified JSON pointer ptr, with bounds checking.
[in] | ptr | JSON pointer to the desired element |
parse_error.106 | if an array index in the passed JSON pointer ptr begins with '0'. See example below. |
parse_error.109 | if an array index in the passed JSON pointer ptr is not a number. See example below. |
out_of_range.401 | if an array index in the passed JSON pointer ptr is out of range. See example below. |
out_of_range.402 | if the array index '-' is used in the passed JSON pointer ptr. As at provides checked access (and no elements are implicitly inserted), the index '-' is always invalid. See example below. |
out_of_range.403 | if the JSON pointer describes a key of an object which cannot be found. See example below. |
out_of_range.404 | if the JSON pointer ptr can not be resolved. See example below. |
@exceptionsafety Strong guarantee: if an exception is thrown, there are no changes in the JSON value.
@complexity Constant.
@liveexample{The behavior is shown in the example.,at_json_pointer}
|
inline |
access specified element via JSON Pointer
Returns a const reference to the element at with specified JSON pointer ptr, with bounds checking.
[in] | ptr | JSON pointer to the desired element |
parse_error.106 | if an array index in the passed JSON pointer ptr begins with '0'. See example below. |
parse_error.109 | if an array index in the passed JSON pointer ptr is not a number. See example below. |
out_of_range.401 | if an array index in the passed JSON pointer ptr is out of range. See example below. |
out_of_range.402 | if the array index '-' is used in the passed JSON pointer ptr. As at provides checked access (and no elements are implicitly inserted), the index '-' is always invalid. See example below. |
out_of_range.403 | if the JSON pointer describes a key of an object which cannot be found. See example below. |
out_of_range.404 | if the JSON pointer ptr can not be resolved. See example below. |
@exceptionsafety Strong guarantee: if an exception is thrown, there are no changes in the JSON value.
@complexity Constant.
@liveexample{The behavior is shown in the example.,at_json_pointer_const}
access specified array element with bounds checking
Returns a reference to the element at specified location idx, with bounds checking.
[in] | idx | index of the element to access |
type_error.304 | if the JSON value is not an array; in this case, calling at with an index makes no sense. See example below. |
out_of_range.401 | if the index idx is out of range of the array; that is, idx >= size() . See example below. |
@exceptionsafety Strong guarantee: if an exception is thrown, there are no changes in the JSON value.
@complexity Constant.
@liveexample{The example below shows how array elements can be read and written using at()
. It also demonstrates the different exceptions that can be thrown.,at__size_type}
const_reference wpi::json::at | ( | size_type | idx | ) | const |
access specified array element with bounds checking
Returns a const reference to the element at specified location idx, with bounds checking.
[in] | idx | index of the element to access |
type_error.304 | if the JSON value is not an array; in this case, calling at with an index makes no sense. See example below. |
out_of_range.401 | if the index idx is out of range of the array; that is, idx >= size() . See example below. |
@exceptionsafety Strong guarantee: if an exception is thrown, there are no changes in the JSON value.
@complexity Constant.
@liveexample{The example below shows how array elements can be read using at()
. It also demonstrates the different exceptions that can be thrown., at__size_type_const}
reference wpi::json::at | ( | std::string_view | key | ) |
access specified object element with bounds checking
Returns a reference to the element at with specified key key, with bounds checking.
[in] | key | key of the element to access |
type_error.304 | if the JSON value is not an object; in this case, calling at with a key makes no sense. See example below. |
out_of_range.403 | if the key key is is not stored in the object; that is, find(key) == end() . See example below. |
@exceptionsafety Strong guarantee: if an exception is thrown, there are no changes in the JSON value.
@complexity Logarithmic in the size of the container.
@liveexample{The example below shows how object elements can be read and written using at()
. It also demonstrates the different exceptions that can be thrown.,at__object_t_key_type}
const_reference wpi::json::at | ( | std::string_view | key | ) | const |
access specified object element with bounds checking
Returns a const reference to the element at with specified key key, with bounds checking.
[in] | key | key of the element to access |
type_error.304 | if the JSON value is not an object; in this case, calling at with a key makes no sense. See example below. |
out_of_range.403 | if the key key is is not stored in the object; that is, find(key) == end() . See example below. |
@exceptionsafety Strong guarantee: if an exception is thrown, there are no changes in the JSON value.
@complexity Logarithmic in the size of the container.
@liveexample{The example below shows how object elements can be read using at()
. It also demonstrates the different exceptions that can be thrown., at__object_t_key_type_const}
|
inline |
access the last element
Returns a reference to the last element in the container. For a JSON container c
, the expression c.back()
is equivalent to
@complexity Constant.
null
(would throw std::out_of_range
) or an empty array or object (undefined behavior, guarded by assertions). invalid_iterator.214 | when called on a null value. See example below. |
@liveexample{The following code shows an example for back()
.,back}
|
inline |
access the last element
Returns a reference to the last element in the container. For a JSON container c
, the expression c.back()
is equivalent to
@complexity Constant.
null
(would throw std::out_of_range
) or an empty array or object (undefined behavior, guarded by assertions). invalid_iterator.214 | when called on a null value. See example below. |
@liveexample{The following code shows an example for back()
.,back}
|
inlinenoexcept |
returns a const iterator to the first element
Returns a const iterator to the first element.
@complexity Constant.
@requirement This function helps json
satisfying the Container requirements:
const_cast<const json&>(*this).begin()
.@liveexample{The following code shows an example for cbegin()
.,cbegin}
|
inlinenoexcept |
returns an iterator to the first element
Returns an iterator to the first element.
@complexity Constant.
@requirement This function helps json
satisfying the Container requirements:
@liveexample{The following code shows an example for begin()
.,begin}
|
inlinenoexcept |
returns a const iterator to the first element
Returns a const iterator to the first element.
@complexity Constant.
@requirement This function helps json
satisfying the Container requirements:
const_cast<const json&>(*this).begin()
.@liveexample{The following code shows an example for cbegin()
.,cbegin}
|
inlinenoexcept |
returns a const iterator to one past the last element
Returns a const iterator to one past the last element.
@complexity Constant.
@requirement This function helps json
satisfying the Container requirements:
const_cast<const json&>(*this).end()
.@liveexample{The following code shows an example for cend()
.,cend}
|
noexcept |
clears the contents
Clears the content of a JSON value and resets it to the default value as if json(value_t) would have been called with the current value type from type():
Value type | initial value |
---|---|
null | null |
boolean | false |
string | "" |
number | 0 |
object | {} |
array | [] |
@liveexample{The example below shows the effect of clear()
to different JSON types.,clear}
@complexity Linear in the size of the JSON value.
@iterators All iterators, pointers and references related to this container are invalidated.
@exceptionsafety No-throw guarantee: this function never throws exceptions.
clear()
size_type wpi::json::count | ( | std::string_view | key | ) | const |
returns the number of occurrences of a key in a JSON object
Returns the number of elements with key key. If ObjectType is the default std::map
type, the return value will always be 0
(key was not found) or 1
(key was found).
0
when executed on a JSON type that is not an object.[in] | key | key value of the element to count |
0
.@complexity Logarithmic in the size of the JSON object.
@liveexample{The example shows how count()
is used.,count}
|
inlinenoexcept |
returns a const reverse iterator to the last element
Returns a const iterator to the reverse-beginning; that is, the last element.
@complexity Constant.
@requirement This function helps json
satisfying the ReversibleContainer requirements:
const_cast<const json&>(*this).rbegin()
.@liveexample{The following code shows an example for crbegin()
.,crbegin}
|
inlinenoexcept |
returns a const reverse iterator to one before the first
Returns a const reverse iterator to the reverse-end; that is, one before the first element.
@complexity Constant.
@requirement This function helps json
satisfying the ReversibleContainer requirements:
const_cast<const json&>(*this).rend()
.@liveexample{The following code shows an example for crend()
.,crend}
|
static |
creates a diff as a JSON patch
Creates a JSON Patch so that value source can be changed into the value target by calling patch function.
true
: remove
, add
, and replace
operations are generated.[in] | source | JSON value to compare from |
[in] | target | JSON value to compare against |
[in] | path | helper value to create JSON pointers |
@complexity Linear in the lengths of source and target.
@liveexample{The following code shows how a JSON patch is created as a diff for two JSON values.,diff}
std::string wpi::json::dump | ( | const int | indent = -1 , |
const char | indent_char = ' ' , |
||
const bool | ensure_ascii = false |
||
) | const |
serialization
Serialization function for JSON values. The function tries to mimic Python's json.dumps()
function, and currently supports its indent and ensure_ascii parameters.
[in] | indent | If indent is nonnegative, then array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. -1 (the default) selects the most compact representation. |
[in] | indent_char | The character to use for indentation if indent is greater than 0 . The default is (space). |
[in] | ensure_ascii | If ensure_ascii is true, all non-ASCII characters in the output are escaped with \uXXXX sequences, and the result consists of ASCII characters only. |
type_error.316 | if a string stored inside the JSON value is not UTF-8 encoded |
@complexity Linear.
@exceptionsafety Strong guarantee: if an exception is thrown, there are no changes in the JSON value.
@liveexample{The following example shows the effect of different indent\, indent_char\, and ensure_ascii parameters to the result of the serialization.,dump}
void wpi::json::dump | ( | raw_ostream & | os, |
int | indent = -1 , |
||
const char | indent_char = ' ' , |
||
const bool | ensure_ascii = false |
||
) | const |
|
inline |
add an object to an object if key does not exist
Inserts a new element into a JSON object constructed in-place with the given args if there is no element with the key in the container. If the function is called on a JSON null value, an empty object is created before appending the value created from args.
[in] | args | arguments to forward to a constructor of json |
Args | compatible types to create a json object |
type_error.311 | when called on a type other than JSON object or null; example: "cannot use emplace() with number" |
@complexity Logarithmic in the size of the container, O(log(size()
)).
@liveexample{The example shows how emplace()
can be used to add elements to a JSON object. Note how the null
value was silently converted to a JSON object. Further note how no value is added if there was already one value stored with the same key.,emplace}
|
inline |
add an object to an array
Creates a JSON value from the passed parameters args to the end of the JSON value. If the function is called on a JSON null value, an empty array is created before appending the value created from args.
[in] | args | arguments to forward to a constructor of json |
Args | compatible types to create a json object |
type_error.311 | when called on a type other than JSON array or null; example: "cannot use emplace_back() with number" |
@complexity Amortized constant.
@liveexample{The example shows how push_back()
can be used to add elements to a JSON array. Note how the null
value was silently converted to a JSON array.,emplace_back}
|
noexcept |
checks whether the container is empty.
Checks if a JSON value has no elements (i.e. whether its size is 0
).
Value type | return value |
---|---|
null | true |
boolean | false |
string | false |
number | false |
object | result of function object_t::empty() |
array | result of function array_t::empty() |
@liveexample{The following code uses empty()
to check if a JSON object contains any elements.,empty}
@complexity Constant, as long as array_t and object_t satisfy the Container concept; that is, their empty()
functions have constant complexity.
@iterators No changes.
@exceptionsafety No-throw guarantee: this function never throws exceptions.
@requirement This function helps json
satisfying the Container requirements:
|
inlinenoexcept |
returns a const iterator to one past the last element
Returns a const iterator to one past the last element.
@complexity Constant.
@requirement This function helps json
satisfying the Container requirements:
const_cast<const json&>(*this).end()
.@liveexample{The following code shows an example for cend()
.,cend}
|
inlinenoexcept |
returns an iterator to one past the last element
Returns an iterator to one past the last element.
@complexity Constant.
@requirement This function helps json
satisfying the Container requirements:
@liveexample{The following code shows an example for end()
.,end}
void wpi::json::erase | ( | const size_type | idx | ) |
remove element from a JSON array given an index
Removes element from a JSON array at the index idx.
[in] | idx | index of the element to remove |
type_error.307 | when called on a type other than JSON object; example: "cannot use erase() with null" |
out_of_range.401 | when idx >= size() ; example: "array index 17
is out of range" |
@complexity Linear in distance between idx and the end of the container.
@liveexample{The example shows the effect of erase()
.,erase__size_type}
|
inline |
remove elements given an iterator range
Removes the element specified by the range [first; last)
. The iterator first does not need to be dereferenceable if first == last
: erasing an empty range is a no-op.
If called on a primitive type other than null
, the resulting JSON value will be null
.
[in] | first | iterator to the beginning of the range to remove |
[in] | last | iterator past the end of the range to remove |
end()
iterator is returned.IteratorType | an iterator or const_iterator |
end()
iterator.type_error.307 | if called on a null value; example: "cannot use
erase() with null" |
invalid_iterator.203 | if called on iterators which does not belong to the current JSON value; example: "iterators do not fit current value" |
invalid_iterator.204 | if called on a primitive type with invalid iterators (i.e., if first != begin() and last != end() ); example: "iterators out of range" |
@complexity The complexity depends on the type:
log(size()) + std::distance(first, last)
@liveexample{The example shows the result of erase()
for different JSON types.,erase__IteratorType_IteratorType}
|
inline |
remove element given an iterator
Removes the element specified by iterator pos. The iterator pos must be valid and dereferenceable. Thus the end()
iterator (which is valid, but is not dereferenceable) cannot be used as a value for pos.
If called on a primitive type other than null
, the resulting JSON value will be null
.
[in] | pos | iterator to the element to remove |
IteratorType | an iterator or const_iterator |
end()
iterator.type_error.307 | if called on a null value; example: "cannot use
erase() with null" |
invalid_iterator.202 | if called on an iterator which does not belong to the current JSON value; example: "iterator does not fit current
value" |
invalid_iterator.205 | if called on a primitive type with invalid iterator (i.e., any iterator which is not begin() ); example: "iterator
out of range" |
@complexity The complexity depends on the type:
@liveexample{The example shows the result of erase()
for different JSON types.,erase__IteratorType}
size_type wpi::json::erase | ( | std::string_view | key | ) |
remove element from a JSON object given a key
Removes elements from a JSON object with the key value key.
[in] | key | value of the elements to remove |
std::map
type, the return value will always be 0
(key was not found) or 1
(key was found).type_error.307 | when called on a type other than JSON object; example: "cannot use erase() with null" |
@complexity log(size()) + count(key)
@liveexample{The example shows the effect of erase()
.,erase__key_type}
iterator wpi::json::find | ( | std::string_view | key | ) |
find an element in a JSON object
Finds an element in a JSON object with key equivalent to key. If the element is not found or the JSON value is not an object, end() is returned.
[in] | key | key value of the element to search for. |
@complexity Logarithmic in the size of the JSON object.
@liveexample{The example shows how find()
is used.,find__key_type}
const_iterator wpi::json::find | ( | std::string_view | key | ) | const |
find an element in a JSON object
|
inline |
return flattened JSON value
The function creates a JSON object whose keys are JSON pointers (see RFC 6901) and whose values are all primitive. The original JSON value can be restored using the unflatten() function.
null
and will not be reconstructed correctly by the unflatten() function.@complexity Linear in the size the JSON value.
@liveexample{The following code shows how a JSON object is flattened to an object whose keys consist of JSON pointers.,flatten}
|
static |
create a JSON value from an input in CBOR format
Deserializes a given input i to a JSON value using the CBOR (Concise Binary Object Representation) serialization format.
The library maps CBOR types to JSON value types as follows:
CBOR type | JSON value type | first byte |
---|---|---|
Integer | number_unsigned | 0x00..0x17 |
Unsigned integer | number_unsigned | 0x18 |
Unsigned integer | number_unsigned | 0x19 |
Unsigned integer | number_unsigned | 0x1A |
Unsigned integer | number_unsigned | 0x1B |
Negative integer | number_integer | 0x20..0x37 |
Negative integer | number_integer | 0x38 |
Negative integer | number_integer | 0x39 |
Negative integer | number_integer | 0x3A |
Negative integer | number_integer | 0x3B |
Negative integer | number_integer | 0x40..0x57 |
UTF-8 string | string | 0x60..0x77 |
UTF-8 string | string | 0x78 |
UTF-8 string | string | 0x79 |
UTF-8 string | string | 0x7A |
UTF-8 string | string | 0x7B |
UTF-8 string | string | 0x7F |
array | array | 0x80..0x97 |
array | array | 0x98 |
array | array | 0x99 |
array | array | 0x9A |
array | array | 0x9B |
array | array | 0x9F |
map | object | 0xA0..0xB7 |
map | object | 0xB8 |
map | object | 0xB9 |
map | object | 0xBA |
map | object | 0xBB |
map | object | 0xBF |
False | false | 0xF4 |
True | true | 0xF5 |
Nill | null | 0xF6 |
Half-Precision Float | number_float | 0xF9 |
Single-Precision Float | number_float | 0xFA |
Double-Precision Float | number_float | 0xFB |
[in] | i | an input in CBOR format convertible to an input adapter |
[in] | strict | whether to expect the input to be consumed until EOF (true by default) |
parse_error.110 | if the given input ends prematurely or the end of file was not reached when strict was set to true |
parse_error.112 | if unsupported features from CBOR were used in the given input v or if the input is not valid CBOR |
parse_error.113 | if a string was expected as map key, but not found |
@complexity Linear in the size of the input i.
@liveexample{The example shows the deserialization of a byte vector in CBOR format to a JSON value.,from_cbor}
|
static |
create a JSON value from an input in CBOR format
Deserializes a given input i to a JSON value using the CBOR (Concise Binary Object Representation) serialization format.
The library maps CBOR types to JSON value types as follows:
CBOR type | JSON value type | first byte |
---|---|---|
Integer | number_unsigned | 0x00..0x17 |
Unsigned integer | number_unsigned | 0x18 |
Unsigned integer | number_unsigned | 0x19 |
Unsigned integer | number_unsigned | 0x1A |
Unsigned integer | number_unsigned | 0x1B |
Negative integer | number_integer | 0x20..0x37 |
Negative integer | number_integer | 0x38 |
Negative integer | number_integer | 0x39 |
Negative integer | number_integer | 0x3A |
Negative integer | number_integer | 0x3B |
Negative integer | number_integer | 0x40..0x57 |
UTF-8 string | string | 0x60..0x77 |
UTF-8 string | string | 0x78 |
UTF-8 string | string | 0x79 |
UTF-8 string | string | 0x7A |
UTF-8 string | string | 0x7B |
UTF-8 string | string | 0x7F |
array | array | 0x80..0x97 |
array | array | 0x98 |
array | array | 0x99 |
array | array | 0x9A |
array | array | 0x9B |
array | array | 0x9F |
map | object | 0xA0..0xB7 |
map | object | 0xB8 |
map | object | 0xB9 |
map | object | 0xBA |
map | object | 0xBB |
map | object | 0xBF |
False | false | 0xF4 |
True | true | 0xF5 |
Nill | null | 0xF6 |
Half-Precision Float | number_float | 0xF9 |
Single-Precision Float | number_float | 0xFA |
Double-Precision Float | number_float | 0xFB |
[in] | i | an input in CBOR format convertible to an input adapter |
[in] | strict | whether to expect the input to be consumed until EOF (true by default) |
parse_error.110 | if the given input ends prematurely or the end of file was not reached when strict was set to true |
parse_error.112 | if unsupported features from CBOR were used in the given input v or if the input is not valid CBOR |
parse_error.113 | if a string was expected as map key, but not found |
@complexity Linear in the size of the input i.
@liveexample{The example shows the deserialization of a byte vector in CBOR format to a JSON value.,from_cbor}
|
static |
create a JSON value from an input in MessagePack format
Deserializes a given input i to a JSON value using the MessagePack serialization format.
The library maps MessagePack types to JSON value types as follows:
MessagePack type | JSON value type | first byte |
---|---|---|
positive fixint | number_unsigned | 0x00..0x7F |
fixmap | object | 0x80..0x8F |
fixarray | array | 0x90..0x9F |
fixstr | string | 0xA0..0xBF |
nil | null | 0xC0 |
false | false | 0xC2 |
true | true | 0xC3 |
float 32 | number_float | 0xCA |
float 64 | number_float | 0xCB |
uint 8 | number_unsigned | 0xCC |
uint 16 | number_unsigned | 0xCD |
uint 32 | number_unsigned | 0xCE |
uint 64 | number_unsigned | 0xCF |
int 8 | number_integer | 0xD0 |
int 16 | number_integer | 0xD1 |
int 32 | number_integer | 0xD2 |
int 64 | number_integer | 0xD3 |
str 8 | string | 0xD9 |
str 16 | string | 0xDA |
str 32 | string | 0xDB |
array 16 | array | 0xDC |
array 32 | array | 0xDD |
map 16 | object | 0xDE |
map 32 | object | 0xDF |
negative fixint | number_integer | 0xE0-0xFF |
[in] | i | an input in MessagePack format convertible to an input adapter |
[in] | strict | whether to expect the input to be consumed until EOF (true by default) |
parse_error.110 | if the given input ends prematurely or the end of file was not reached when strict was set to true |
parse_error.112 | if unsupported features from MessagePack were used in the given input i or if the input is not valid MessagePack |
parse_error.113 | if a string was expected as map key, but not found |
@complexity Linear in the size of the input i.
@liveexample{The example shows the deserialization of a byte vector in MessagePack format to a JSON value.,from_msgpack}
|
static |
create a JSON value from an input in MessagePack format
Deserializes a given input i to a JSON value using the MessagePack serialization format.
The library maps MessagePack types to JSON value types as follows:
MessagePack type | JSON value type | first byte |
---|---|---|
positive fixint | number_unsigned | 0x00..0x7F |
fixmap | object | 0x80..0x8F |
fixarray | array | 0x90..0x9F |
fixstr | string | 0xA0..0xBF |
nil | null | 0xC0 |
false | false | 0xC2 |
true | true | 0xC3 |
float 32 | number_float | 0xCA |
float 64 | number_float | 0xCB |
uint 8 | number_unsigned | 0xCC |
uint 16 | number_unsigned | 0xCD |
uint 32 | number_unsigned | 0xCE |
uint 64 | number_unsigned | 0xCF |
int 8 | number_integer | 0xD0 |
int 16 | number_integer | 0xD1 |
int 32 | number_integer | 0xD2 |
int 64 | number_integer | 0xD3 |
str 8 | string | 0xD9 |
str 16 | string | 0xDA |
str 32 | string | 0xDB |
array 16 | array | 0xDC |
array 32 | array | 0xDD |
map 16 | object | 0xDE |
map 32 | object | 0xDF |
negative fixint | number_integer | 0xE0-0xFF |
[in] | i | an input in MessagePack format convertible to an input adapter |
[in] | strict | whether to expect the input to be consumed until EOF (true by default) |
parse_error.110 | if the given input ends prematurely or the end of file was not reached when strict was set to true |
parse_error.112 | if unsupported features from MessagePack were used in the given input i or if the input is not valid MessagePack |
parse_error.113 | if a string was expected as map key, but not found |
@complexity Linear in the size of the input i.
@liveexample{The example shows the deserialization of a byte vector in MessagePack format to a JSON value.,from_msgpack}
|
static |
create a JSON value from an input in UBJSON format
Deserializes a given input i to a JSON value using the UBJSON (Universal Binary JSON) serialization format.
The library maps UBJSON types to JSON value types as follows:
UBJSON type | JSON value type | marker |
---|---|---|
no-op | no value, next value is read | N |
null | null | Z |
false | false | F |
true | true | T |
float32 | number_float | d |
float64 | number_float | D |
uint8 | number_unsigned | U |
int8 | number_integer | i |
int16 | number_integer | I |
int32 | number_integer | l |
int64 | number_integer | L |
string | string | S |
char | string | C |
array | array (optimized values are supported) | [ |
object | object (optimized values are supported) | { |
[in] | i | an input in UBJSON format convertible to an input adapter |
[in] | strict | whether to expect the input to be consumed until EOF (true by default) |
parse_error.110 | if the given input ends prematurely or the end of file was not reached when strict was set to true |
parse_error.112 | if a parse error occurs |
parse_error.113 | if a string could not be parsed successfully |
@complexity Linear in the size of the input i.
@liveexample{The example shows the deserialization of a byte vector in UBJSON format to a JSON value.,from_ubjson}
|
static |
|
inline |
access the first element
Returns a reference to the first element in the container. For a JSON container c
, the expression c.front()
is equivalent to *c.begin()
.
@complexity Constant.
null
(would throw std::out_of_range
) or an empty array or object (undefined behavior, guarded by assertions). invalid_iterator.214 | when called on null value |
@liveexample{The following code shows an example for front()
.,front}
|
inline |
access the first element
Returns a reference to the first element in the container. For a JSON container c
, the expression c.front()
is equivalent to *c.begin()
.
@complexity Constant.
null
(would throw std::out_of_range
) or an empty array or object (undefined behavior, guarded by assertions). invalid_iterator.214 | when called on null value |
@liveexample{The following code shows an example for front()
.,front}
|
inline |
get special-case overload
This overloads avoids a lot of template boilerplate, it can be seen as the identity method
BasicJsonType | == json |
@complexity Constant.
|
inlinenoexcept |
get a pointer value (explicit)
get a pointer value (explicit)
Explicit pointer access to the internally stored JSON value. No copies are made.
PointerType | pointer type; must be a pointer to array_t, object_t, std::string , bool, int64_t, uint64_t, or double. |
nullptr
otherwise@complexity Constant.
@liveexample{The example below shows how pointers to internal values of a JSON value can be requested. Note that no type conversions are made and a nullptr
is returned if the value and the requested pointer type does not match.,get__PointerType}
|
inlinenoexcept |
get a value (explicit)
Explicit type conversion between the JSON value and a compatible value which is CopyConstructible and DefaultConstructible. The value is converted by calling the json_serializer<ValueType> from_json()
method.
The function is equivalent to executing
This overloads is chosen if:
from_json()
method of the form void from_json(const json&, ValueType&)
, andfrom_json()
method of the form ValueType from_json(const json&)
ValueTypeCV | the provided value type |
ValueType | the returned value type |
what | json_serializer<ValueType> from_json() method throws |
@liveexample{The example below shows several conversions from JSON values to other types. There a few things to note: (1) Floating-point numbers can be converted to integers\, (2) A JSON array can be converted to a standard std::vector<short>
\, (3) A JSON object can be converted to C++ associative containers such as std::unordered_map<std::string\, json>
.,get__ValueType_const}
|
inlinenoexcept |
get a value (explicit); special case
Explicit type conversion between the JSON value and a compatible value which is not CopyConstructible and not DefaultConstructible. The value is converted by calling the json_serializer<ValueType> from_json()
method.
The function is equivalent to executing
This overloads is chosen if:
from_json()
method of the form ValueType from_json(const json&)
from_json()
, this one is chosen.ValueTypeCV | the provided value type |
ValueType | the returned value type |
what | json_serializer<ValueType> from_json() method throws |
|
inlinenoexcept |
get a pointer value (explicit)
Explicit pointer access to the internally stored JSON value. No copies are made.
PointerType | pointer type; must be a pointer to array_t, object_t, std::string , bool, int64_t, uint64_t, or double. |
nullptr
otherwise@complexity Constant.
@liveexample{The example below shows how pointers to internal values of a JSON value can be requested. Note that no type conversions are made and a nullptr
is returned if the value and the requested pointer type does not match.,get__PointerType}
|
inlinestatic |
returns the allocator associated with the container
|
inlinenoexcept |
get a pointer value (implicit)
get a pointer value (implicit)
Implicit pointer access to the internally stored JSON value. No copies are made.
PointerType | pointer type; must be a pointer to array_t, object_t, std::string , bool, int64_t, uint64_t, or double. Enforced by a static assertion. |
nullptr
otherwise@complexity Constant.
@liveexample{The example below shows how pointers to internal values of a JSON value can be requested. Note that no type conversions are made and a nullptr
is returned if the value and the requested pointer type does not match.,get_ptr}
|
inlinenoexcept |
get a pointer value (implicit)
Implicit pointer access to the internally stored JSON value. No copies are made.
PointerType | pointer type; must be a pointer to array_t, object_t, std::string , bool, int64_t, uint64_t, or double. Enforced by a static assertion. |
nullptr
otherwise@complexity Constant.
@liveexample{The example below shows how pointers to internal values of a JSON value can be requested. Note that no type conversions are made and a nullptr
is returned if the value and the requested pointer type does not match.,get_ptr}
|
inline |
get a reference value (implicit)
Implicit reference access to the internally stored JSON value. No copies are made.
ReferenceType | reference type; must be a reference to array_t, object_t, std::string, bool, int64_t, or double. Enforced by static assertion. |
type_error.303 | in case passed type ReferenceType is incompatible with the stored JSON value; see example below |
@complexity Constant.
@liveexample{The example shows several calls to get_ref()
.,get_ref}
|
inline |
get a reference value (implicit)
get a reference value (implicit)
Implicit reference access to the internally stored JSON value. No copies are made.
ReferenceType | reference type; must be a reference to array_t, object_t, std::string, bool, int64_t, or double. Enforced by static assertion. |
type_error.303 | in case passed type ReferenceType is incompatible with the stored JSON value; see example below |
@complexity Constant.
@liveexample{The example shows several calls to get_ref()
.,get_ref}
void wpi::json::insert | ( | const_iterator | first, |
const_iterator | last | ||
) |
inserts elements
Inserts elements from range [first, last)
.
[in] | first | begin of the range of elements to insert |
[in] | last | end of the range of elements to insert |
type_error.309 | if called on JSON values other than objects; example: "cannot use insert() with string" |
invalid_iterator.202 | if iterator first or last does does not point to an object; example: "iterators first and last must point to
objects" |
invalid_iterator.210 | if first and last do not belong to the same JSON value; example: "iterators do not fit" |
@complexity Logarithmic: O(N*log(size() + N))
, where N
is the number of elements to insert.
@liveexample{The example shows how insert()
is used.,insert__range_object}
iterator wpi::json::insert | ( | const_iterator | pos, |
const json & | val | ||
) |
inserts element
Inserts element val before iterator pos.
[in] | pos | iterator before which the content will be inserted; may be the end() iterator |
[in] | val | element to insert |
type_error.309 | if called on JSON values other than arrays; example: "cannot use insert() with string" |
invalid_iterator.202 | if pos is not an iterator of *this; example: "iterator does not fit current value" |
@complexity Constant plus linear in the distance between pos and end of the container.
@liveexample{The example shows how insert()
is used.,insert}
iterator wpi::json::insert | ( | const_iterator | pos, |
const_iterator | first, | ||
const_iterator | last | ||
) |
inserts elements
Inserts elements from range [first, last)
before iterator pos.
[in] | pos | iterator before which the content will be inserted; may be the end() iterator |
[in] | first | begin of the range of elements to insert |
[in] | last | end of the range of elements to insert |
type_error.309 | if called on JSON values other than arrays; example: "cannot use insert() with string" |
invalid_iterator.202 | if pos is not an iterator of *this; example: "iterator does not fit current value" |
invalid_iterator.210 | if first and last do not belong to the same JSON value; example: "iterators do not fit" |
invalid_iterator.211 | if first or last are iterators into container for which insert is called; example: "passed iterators may not
belong to container" |
first==last
@complexity Linear in std::distance(first, last)
plus linear in the distance between pos and end of the container.
@liveexample{The example shows how insert()
is used.,insert__range}
iterator wpi::json::insert | ( | const_iterator | pos, |
initializer_list_t | ilist | ||
) |
inserts elements
Inserts elements from initializer list ilist before iterator pos.
[in] | pos | iterator before which the content will be inserted; may be the end() iterator |
[in] | ilist | initializer list to insert the values from |
type_error.309 | if called on JSON values other than arrays; example: "cannot use insert() with string" |
invalid_iterator.202 | if pos is not an iterator of *this; example: "iterator does not fit current value" |
ilist
is empty@complexity Linear in ilist.size()
plus linear in the distance between pos and end of the container.
@liveexample{The example shows how insert()
is used.,insert__ilist}
|
inline |
inserts element
inserts element
Inserts element val before iterator pos.
[in] | pos | iterator before which the content will be inserted; may be the end() iterator |
[in] | val | element to insert |
type_error.309 | if called on JSON values other than arrays; example: "cannot use insert() with string" |
invalid_iterator.202 | if pos is not an iterator of *this; example: "iterator does not fit current value" |
@complexity Constant plus linear in the distance between pos and end of the container.
@liveexample{The example shows how insert()
is used.,insert}
iterator wpi::json::insert | ( | const_iterator | pos, |
size_type | cnt, | ||
const json & | val | ||
) |
inserts elements
Inserts cnt copies of val before iterator pos.
[in] | pos | iterator before which the content will be inserted; may be the end() iterator |
[in] | cnt | number of copies of val to insert |
[in] | val | element to insert |
cnt==0
type_error.309 | if called on JSON values other than arrays; example: "cannot use insert() with string" |
invalid_iterator.202 | if pos is not an iterator of *this; example: "iterator does not fit current value" |
@complexity Linear in cnt plus linear in the distance between pos and end of the container.
@liveexample{The example shows how insert()
is used.,insert__count}
|
inlinenoexcept |
return whether value is an array
This function returns true if and only if the JSON value is an array.
true
if type is array, false
otherwise.@complexity Constant.
@exceptionsafety No-throw guarantee: this member function never throws exceptions.
@liveexample{The following code exemplifies is_array()
for all JSON types.,is_array}
|
inlinenoexcept |
return whether value is a boolean
This function returns true if and only if the JSON value is a boolean.
true
if type is boolean, false
otherwise.@complexity Constant.
@exceptionsafety No-throw guarantee: this member function never throws exceptions.
@liveexample{The following code exemplifies is_boolean()
for all JSON types.,is_boolean}
|
inlinenoexcept |
return whether value is discarded
This function returns true if and only if the JSON value was discarded during parsing with a callback function (see parser_callback_t).
false
for JSON values after parsing. That is, discarded values can only occur during parsing, but will be removed when inside a structured value or replaced by null in other cases.true
if type is discarded, false
otherwise.@complexity Constant.
@exceptionsafety No-throw guarantee: this member function never throws exceptions.
@liveexample{The following code exemplifies is_discarded()
for all JSON types.,is_discarded}
|
inlinenoexcept |
return whether value is null
This function returns true if and only if the JSON value is null.
true
if type is null, false
otherwise.@complexity Constant.
@exceptionsafety No-throw guarantee: this member function never throws exceptions.
@liveexample{The following code exemplifies is_null()
for all JSON types.,is_null}
|
inlinenoexcept |
return whether value is a number
This function returns true if and only if the JSON value is a number. This includes both integer (signed and unsigned) and floating-point values.
true
if type is number (regardless whether integer, unsigned integer or floating-type), false
otherwise.@complexity Constant.
@exceptionsafety No-throw guarantee: this member function never throws exceptions.
@liveexample{The following code exemplifies is_number()
for all JSON types.,is_number}
|
inlinenoexcept |
return whether value is a floating-point number
This function returns true if and only if the JSON value is a floating-point number. This excludes signed and unsigned integer values.
true
if type is a floating-point number, false
otherwise.@complexity Constant.
@exceptionsafety No-throw guarantee: this member function never throws exceptions.
@liveexample{The following code exemplifies is_number_float()
for all JSON types.,is_number_float}
|
inlinenoexcept |
return whether value is an integer number
This function returns true if and only if the JSON value is a signed or unsigned integer number. This excludes floating-point values.
true
if type is an integer or unsigned integer number, false
otherwise.@complexity Constant.
@exceptionsafety No-throw guarantee: this member function never throws exceptions.
@liveexample{The following code exemplifies is_number_integer()
for all JSON types.,is_number_integer}
|
inlinenoexcept |
return whether value is an unsigned integer number
This function returns true if and only if the JSON value is an unsigned integer number. This excludes floating-point and signed integer values.
true
if type is an unsigned integer number, false
otherwise.@complexity Constant.
@exceptionsafety No-throw guarantee: this member function never throws exceptions.
@liveexample{The following code exemplifies is_number_unsigned()
for all JSON types.,is_number_unsigned}
|
inlinenoexcept |
return whether value is an object
This function returns true if and only if the JSON value is an object.
true
if type is object, false
otherwise.@complexity Constant.
@exceptionsafety No-throw guarantee: this member function never throws exceptions.
@liveexample{The following code exemplifies is_object()
for all JSON types.,is_object}
|
inlinenoexcept |
return whether type is primitive
This function returns true if and only if the JSON type is primitive (string, number, boolean, or null).
true
if type is primitive (string, number, boolean, or null), false
otherwise.@complexity Constant.
@exceptionsafety No-throw guarantee: this member function never throws exceptions.
@liveexample{The following code exemplifies is_primitive()
for all JSON types.,is_primitive}
null
|
inlinenoexcept |
return whether value is a string
This function returns true if and only if the JSON value is a string.
true
if type is string, false
otherwise.@complexity Constant.
@exceptionsafety No-throw guarantee: this member function never throws exceptions.
@liveexample{The following code exemplifies is_string()
for all JSON types.,is_string}
|
inlinenoexcept |
return whether type is structured
This function returns true if and only if the JSON type is structured (array or object).
true
if type is structured (array or object), false
otherwise.@complexity Constant.
@exceptionsafety No-throw guarantee: this member function never throws exceptions.
@liveexample{The following code exemplifies is_structured()
for all JSON types.,is_structured}
|
inlinenoexcept |
helper to access iterator member functions in range-based for
This function allows to access iterator::key() and iterator::value() during range-based for loops. In these loops, a reference to the JSON values is returned, so there is no access to the underlying iterator.
For loop without items()
function:
Range-based for loop without items()
function:
Range-based for loop with items()
function:
key()
will return the index of the element as string (see example). For primitive types (e.g., numbers), key()
returns an empty string.@liveexample{The following code shows how the function is used.,items}
@exceptionsafety Strong guarantee: if an exception is thrown, there are no changes in the JSON value.
@complexity Constant.
|
inlinenoexcept |
helper to access iterator member functions in range-based for
This function allows to access iterator::key() and iterator::value() during range-based for loops. In these loops, a reference to the JSON values is returned, so there is no access to the underlying iterator.
For loop without items()
function:
Range-based for loop without items()
function:
Range-based for loop with items()
function:
key()
will return the index of the element as string (see example). For primitive types (e.g., numbers), key()
returns an empty string.@liveexample{The following code shows how the function is used.,items}
@exceptionsafety Strong guarantee: if an exception is thrown, there are no changes in the JSON value.
@complexity Constant.
|
noexcept |
returns the maximum possible number of elements
Returns the maximum number of elements a JSON value is able to hold due to system or library implementation limitations, i.e. std::distance(begin(), end())
for the JSON value.
Value type | return value |
---|---|
null | 0 (same as size() ) |
boolean | 1 (same as size() ) |
string | 1 (same as size() ) |
number | 1 (same as size() ) |
object | result of function object_t::max_size() |
array | result of function array_t::max_size() |
@liveexample{The following code calls max_size()
on the different value types. Note the output is implementation specific.,max_size}
@complexity Constant, as long as array_t and object_t satisfy the Container concept; that is, their max_size()
functions have constant complexity.
@iterators No changes.
@exceptionsafety No-throw guarantee: this function never throws exceptions.
@requirement This function helps json
satisfying the Container requirements:
b.size()
where b
is the largest possible JSON value.void wpi::json::merge_patch | ( | const json & | patch | ) |
applies a JSON Merge Patch
The merge patch format is primarily intended for use with the HTTP PATCH method as a means of describing a set of modifications to a target resource's content. This function applies a merge patch to the current JSON value.
The function implements the following algorithm from Section 2 of RFC 7396 (JSON Merge Patch):
Thereby, Target
is the current object; that is, the patch is applied to the current value.
[in] | patch | the patch to apply |
@complexity Linear in the lengths of patch.
@liveexample{The following code shows how a JSON Merge Patch is applied to a JSON document.,merge_patch}
|
static |
returns version information on the library
This function returns a JSON object with information about the library, including the version number and information on the platform and compiler.
key | description |
---|---|
compiler | Information on the used compiler. It is an object with the following keys: c++ (the used C++ standard), family (the compiler family; possible values are clang , icc , gcc , ilecpp , msvc , pgcpp , sunpro , and unknown ), and version (the compiler version). |
copyright | The copyright line for the library as string. |
name | The name of the library as string. |
platform | The used platform as string. Possible values are win32 , linux , apple , unix , and unknown . |
url | The URL of the project as string. |
version | The version of the library. It is an object with the following keys: major , minor , and patch as defined by Semantic Versioning, and string (the version string). |
@liveexample{The following code shows an example output of the meta()
function.,meta}
@exceptionsafety Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
@complexity Constant.
|
inlinestatic |
explicitly create an object from an initializer list
Creates a JSON object value from a given initializer list. The initializer lists elements must be pairs, and their first elements must be strings. If the initializer list is empty, the empty object {}
is created.
[in] | init | initializer list to create an object from (optional) |
type_error.301 | if init is not a list of pairs whose first elements are strings. In this case, no object can be created. When such a value is passed to json(initializer_list_t, bool, value_t), an array would have been created from the passed initializer list init. See example below. |
@complexity Linear in the size of init.
@exceptionsafety Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
@liveexample{The following code shows an example for the object
function.,object}
|
inlinenoexcept |
return the type of the JSON value (implicit)
Implicitly return the type of the JSON value as a value from the value_t enumeration.
@complexity Constant.
@exceptionsafety No-throw guarantee: this member function never throws exceptions.
@liveexample{The following code exemplifies the value_t operator for all JSON types.,operator__value_t}
|
inline |
get a value (implicit)
Implicit type conversion between the JSON value and a compatible value. The call is realized by calling get() const.
ValueType | non-pointer type compatible to the JSON value, for instance int for JSON integer numbers, bool for JSON booleans, or std::vector types for JSON arrays. The character type of std::string as well as an initializer list of this type is excluded to avoid ambiguities as these types implicitly convert to std::string . |
type_error.302 | in case passed type ValueType is incompatible to the JSON value type (e.g., the JSON value is of type boolean, but a string is requested); see example below |
@complexity Linear in the size of the JSON value.
@liveexample{The example below shows several conversions from JSON values to other types. There a few things to note: (1) Floating-point numbers can be converted to integers\, (2) A JSON array can be converted to a standard std::vector<short>
\, (3) A JSON object can be converted to C++ associative containers such as std::unordered_map<std::string\, json>
.,operator__ValueType}
add an object to an array
add an object to an array
Appends the given element val to the end of the JSON value. If the function is called on a JSON null value, an empty array is created before appending val.
[in] | val | the value to add to the JSON array |
type_error.308 | when called on a type other than JSON array or null; example: "cannot use push_back() with number" |
@complexity Amortized constant.
@liveexample{The example shows how push_back()
and +=
can be used to add elements to a JSON array. Note how the null
value was silently converted to a JSON array.,push_back}
|
inline |
add an object to an object
|
inline |
add an object to an object
add an object to an object
This function allows to use push_back
with an initializer list. In case
init is converted into an object element and added using push_back(const typename object_t::value_type&). Otherwise, init is converted to a JSON value and added using push_back(json&&).
[in] | init | an initializer list |
@complexity Linear in the size of the initializer list init.
{"key", "value"}
can be both interpreted as object_t::value_type
or std::initializer_list<json>
, see https://github.com/nlohmann/json/issues/235 for more information.@liveexample{The example shows how initializer lists are treated as objects when possible.,push_back__initializer_list}
add an object to an array
add an object to an array
Appends the given element val to the end of the JSON value. If the function is called on a JSON null value, an empty array is created before appending val.
[in] | val | the value to add to the JSON array |
type_error.308 | when called on a type other than JSON array or null; example: "cannot use push_back() with number" |
@complexity Amortized constant.
@liveexample{The example shows how push_back()
and +=
can be used to add elements to a JSON array. Note how the null
value was silently converted to a JSON array.,push_back}
copy assignment
Copy assignment operator. Copies a JSON value via the "copy and swap" strategy: It is expressed in terms of the copy constructor, destructor, and the swap()
member function.
[in] | other | value to copy from |
@complexity Linear.
@requirement This function helps json
satisfying the Container requirements:
@liveexample{The code below shows and example for the copy assignment. It creates a copy of value a
which is then swapped with b
. Finally\, the copy of a
(which is the null value after the swap) is destroyed.,json__copyassignment}
|
inline |
access specified element via JSON Pointer
Uses a JSON pointer to retrieve a reference to the respective JSON value. No bound checking is performed. Similar to operator[](const typename
object_t::key_type&), null
values are created in arrays and objects if necessary.
In particular:
null
value before a reference to it is returned.null
value before a reference to it is returned. All indices between the current maximum and the given index are also filled with null
.-
is treated as a synonym for the index past the end.[in] | ptr | a JSON pointer |
@complexity Constant.
parse_error.106 | if an array index begins with '0' |
parse_error.109 | if an array index was not a number |
out_of_range.404 | if the JSON pointer can not be resolved |
@liveexample{The behavior is shown in the example.,operatorjson_pointer}
|
inline |
access specified element via JSON Pointer
Uses a JSON pointer to retrieve a reference to the respective JSON value. No bound checking is performed. The function does not change the JSON value; no null
values are created. In particular, the the special value -
yields an exception.
[in] | ptr | JSON pointer to the desired element |
@complexity Constant.
parse_error.106 | if an array index begins with '0' |
parse_error.109 | if an array index was not a number |
out_of_range.402 | if the array index '-' is used |
out_of_range.404 | if the JSON pointer can not be resolved |
@liveexample{The behavior is shown in the example.,operatorjson_pointer_const}
access specified array element
Returns a reference to the element at specified location idx.
idx >= size()
), then the array is silently filled up with null
values to make idx
a valid reference to the last stored element.[in] | idx | index of the element to access |
type_error.305 | if the JSON value is not an array or null; in that cases, using the [] operator with an index makes no sense. |
@complexity Constant if idx is in the range of the array. Otherwise linear in idx - size()
.
@liveexample{The example below shows how array elements can be read and written using []
operator. Note the addition of null
values.,operatorarray__size_type}
const_reference wpi::json::operator[] | ( | size_type | idx | ) | const |
access specified array element
Returns a const reference to the element at specified location idx.
[in] | idx | index of the element to access |
type_error.305 | if the JSON value is not an array; in that case, using the [] operator with an index makes no sense. |
@complexity Constant.
@liveexample{The example below shows how array elements can be read using the []
operator.,operatorarray__size_type_const}
reference wpi::json::operator[] | ( | std::string_view | key | ) |
access specified object element
Returns a reference to the element at with specified key key.
null
value to make key
a valid reference. In case the value was null
before, it is converted to an object.[in] | key | key of the element to access |
type_error.305 | if the JSON value is not an object or null; in that cases, using the [] operator with a key makes no sense. |
@complexity Logarithmic in the size of the container.
@liveexample{The example below shows how object elements can be read and written using the []
operator.,operatorarray__key_type}
const_reference wpi::json::operator[] | ( | std::string_view | key | ) | const |
read-only access specified object element
Returns a const reference to the element at with specified key key. No bounds checking is performed.
[in] | key | key of the element to access |
type_error.305 | if the JSON value is not an object; in that case, using the [] operator with a key makes no sense. |
@complexity Logarithmic in the size of the container.
@liveexample{The example below shows how object elements can be read using the []
operator.,operatorarray__key_type_const}
|
inline |
access specified object element
Returns a reference to the element at with specified key key.
null
value to make key
a valid reference. In case the value was null
before, it is converted to an object.[in] | key | key of the element to access |
type_error.305 | if the JSON value is not an object or null; in that cases, using the [] operator with a key makes no sense. |
@complexity Logarithmic in the size of the container.
@liveexample{The example below shows how object elements can be read and written using the []
operator.,operatorarray__key_type}
|
inline |
read-only access specified object element
Returns a const reference to the element at with specified key key. No bounds checking is performed.
[in] | key | key of the element to access |
type_error.305 | if the JSON value is not an object; in that case, using the [] operator with a key makes no sense. |
@complexity Logarithmic in the size of the container.
@liveexample{The example below shows how object elements can be read using the []
operator.,operatorarray__key_type_const}
|
static |
create an empty value with a given type parse(raw_istream&, const parser_callback_t)
Create an empty JSON value with a given type. The value will be default initialized with an empty value which depends on the type:
Value type | initial value |
---|---|
null | null |
boolean | false |
string | "" |
number | 0 |
object | {} |
array | [] |
[in] | v | the type of the value to create |
@complexity Constant.
@exceptionsafety Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
@liveexample{The following code shows the constructor for different value_t values,json__value_t}
|
static |
|
static |
deserialize from a compatible input
This function reads from a compatible input. Examples are:
std::vector
, std::string
, std::array
, and std::initializer_list
. Furthermore, C-style arrays can be used with std::begin()
/std::end()
. User-defined containers can be used as long as they implement random-access iterators and a contiguous storage.[in] | i | input to read from |
[in] | cb | a parser callback function of type parser_callback_t which is used to control the deserialization by filtering unwanted values (optional) |
parse_error.101 | if a parse error occurs; example: ""unexpected end of input; expected string literal"" |
parse_error.102 | if to_unicode fails or surrogate error |
parse_error.103 | if to_unicode fails |
@complexity Linear in the length of the input. The parser is a predictive LL(1) parser. The complexity can be higher if the parser callback function cb has a super-linear complexity.
@liveexample{The example below demonstrates the parse()
function reading from an array.,parse__array__parser_callback_t}
@liveexample{The example below demonstrates the parse()
function with and without callback function.,parse__string__parser_callback_t}
@liveexample{The example below demonstrates the parse()
function with and without callback function.,parse__istream__parser_callback_t}
@liveexample{The example below demonstrates the parse()
function reading from a contiguous container.,parse__contiguouscontainer__parser_callback_t}
applies a JSON patch
JSON Patch defines a JSON document structure for expressing a sequence of operations to apply to a JSON) document. With this function, a JSON Patch is applied to the current JSON value by executing all operations from the patch.
[in] | json_patch | JSON patch document |
parse_error.104 | if the JSON patch does not consist of an array of objects |
parse_error.105 | if the JSON patch is malformed (e.g., mandatory attributes are missing); example: "operation add must have member path" |
out_of_range.401 | if an array index is out of range. |
out_of_range.403 | if a JSON pointer inside the patch could not be resolved successfully in the current JSON value; example: "key baz not
found" |
out_of_range.405 | if JSON pointer has no parent ("add", "remove", "move") |
other_error.501 | if "test" operation was unsuccessful |
@complexity Linear in the size of the JSON value and the length of the JSON patch. As usually only a fraction of the JSON value is affected by the patch, the complexity can usually be neglected.
@liveexample{The following code shows how a JSON patch is applied to a value.,patch}
void wpi::json::push_back | ( | const json & | val | ) |
add an object to an array
add an object to an array
Appends the given element val to the end of the JSON value. If the function is called on a JSON null value, an empty array is created before appending val.
[in] | val | the value to add to the JSON array |
type_error.308 | when called on a type other than JSON array or null; example: "cannot use push_back() with number" |
@complexity Amortized constant.
@liveexample{The example shows how push_back()
and +=
can be used to add elements to a JSON array. Note how the null
value was silently converted to a JSON array.,push_back}
|
inline |
add an object to an object
Inserts the given element val to the JSON object. If the function is called on a JSON null value, an empty object is created before inserting val.
[in] | val | the value to add to the JSON object |
type_error.308 | when called on a type other than JSON object or null; example: "cannot use push_back() with number" |
@complexity Logarithmic in the size of the container, O(log(size()
)).
@liveexample{The example shows how push_back()
and +=
can be used to add elements to a JSON object. Note how the null
value was silently converted to a JSON object.,push_back__object_t__value}
void wpi::json::push_back | ( | initializer_list_t | init | ) |
add an object to an object
This function allows to use push_back
with an initializer list. In case
init is converted into an object element and added using push_back(const typename object_t::value_type&). Otherwise, init is converted to a JSON value and added using push_back(json&&).
[in] | init | an initializer list |
@complexity Linear in the size of the initializer list init.
{"key", "value"}
can be both interpreted as object_t::value_type
or std::initializer_list<json>
, see https://github.com/nlohmann/json/issues/235 for more information.@liveexample{The example shows how initializer lists are treated as objects when possible.,push_back__initializer_list}
void wpi::json::push_back | ( | json && | val | ) |
add an object to an array
Appends the given element val to the end of the JSON value. If the function is called on a JSON null value, an empty array is created before appending val.
[in] | val | the value to add to the JSON array |
type_error.308 | when called on a type other than JSON array or null; example: "cannot use push_back() with number" |
@complexity Amortized constant.
@liveexample{The example shows how push_back()
and +=
can be used to add elements to a JSON array. Note how the null
value was silently converted to a JSON array.,push_back}
|
inlinenoexcept |
returns a const reverse iterator to the last element
Returns a const iterator to the reverse-beginning; that is, the last element.
@complexity Constant.
@requirement This function helps json
satisfying the ReversibleContainer requirements:
const_cast<const json&>(*this).rbegin()
.@liveexample{The following code shows an example for crbegin()
.,crbegin}
|
inlinenoexcept |
returns an iterator to the reverse-beginning
Returns an iterator to the reverse-beginning; that is, the last element.
@complexity Constant.
@requirement This function helps json
satisfying the ReversibleContainer requirements:
reverse_iterator(end())
.@liveexample{The following code shows an example for rbegin()
.,rbegin}
|
inlinenoexcept |
returns a const reverse iterator to one before the first
Returns a const reverse iterator to the reverse-end; that is, one before the first element.
@complexity Constant.
@requirement This function helps json
satisfying the ReversibleContainer requirements:
const_cast<const json&>(*this).rend()
.@liveexample{The following code shows an example for crend()
.,crend}
|
inlinenoexcept |
returns an iterator to the reverse-end
Returns an iterator to the reverse-end; that is, one before the first element.
@complexity Constant.
@requirement This function helps json
satisfying the ReversibleContainer requirements:
reverse_iterator(begin())
.@liveexample{The following code shows an example for rend()
.,rend}
|
noexcept |
returns the number of elements
Returns the number of elements in a JSON value.
Value type | return value |
---|---|
null | 0 |
boolean | 1 |
string | 1 |
number | 1 |
object | result of function object_t::size() |
array | result of function array_t::size() |
@liveexample{The following code calls size()
on the different value types.,size}
@complexity Constant, as long as array_t and object_t satisfy the Container concept; that is, their size() functions have constant complexity.
@iterators No changes.
@exceptionsafety No-throw guarantee: this function never throws exceptions.
@requirement This function helps json
satisfying the Container requirements:
|
inline |
exchanges the values
Exchanges the contents of a JSON array with those of other. Does not invoke any move, copy, or swap operations on individual elements. All iterators and references remain valid. The past-the-end iterator is invalidated.
[in,out] | other | array to exchange the contents with |
type_error.310 | when JSON value is not an array; example: "cannot
use swap() with string" |
@complexity Constant.
@liveexample{The example below shows how arrays can be swapped with swap()
.,swap__std_vector_json}
|
inline |
exchanges the values
Exchanges the contents of a JSON object with those of other. Does not invoke any move, copy, or swap operations on individual elements. All iterators and references remain valid. The past-the-end iterator is invalidated.
[in,out] | other | object to exchange the contents with |
type_error.310 | when JSON value is not an object; example: "cannot use swap() with string" |
@complexity Constant.
@liveexample{The example below shows how objects can be swapped with swap()
.,swap__object_t}
|
inlinenoexcept |
exchanges the values
Exchanges the contents of the JSON value with those of other. Does not invoke any move, copy, or swap operations on individual elements. All iterators and references remain valid. The past-the-end iterator is invalidated.
[in,out] | other | JSON value to exchange the contents with |
@complexity Constant.
@liveexample{The example below shows how JSON values can be swapped with swap()
.,swap__reference}
|
inline |
exchanges the values
Exchanges the contents of a JSON string with those of other. Does not invoke any move, copy, or swap operations on individual elements. All iterators and references remain valid. The past-the-end iterator is invalidated.
[in,out] | other | string to exchange the contents with |
type_error.310 | when JSON value is not a string; example: "cannot
use swap() with boolean" |
@complexity Constant.
@liveexample{The example below shows how strings can be swapped with swap()
.,swap__std_string}
|
static |
create a CBOR serialization of a given JSON value
Serializes a given JSON value j to a byte vector using the CBOR (Concise Binary Object Representation) serialization format. CBOR is a binary serialization format which aims to be more compact than JSON itself, yet more efficient to parse.
The library uses the following mapping from JSON values types to CBOR types according to the CBOR specification (RFC 7049):
JSON value type | value/range | CBOR type | first byte |
---|---|---|---|
null | null | Null | 0xF6 |
boolean | true | True | 0xF5 |
boolean | false | False | 0xF4 |
number_integer | -9223372036854775808..-2147483649 | Negative integer (8 bytes follow) | 0x3B |
number_integer | -2147483648..-32769 | Negative integer (4 bytes follow) | 0x3A |
number_integer | -32768..-129 | Negative integer (2 bytes follow) | 0x39 |
number_integer | -128..-25 | Negative integer (1 byte follow) | 0x38 |
number_integer | -24..-1 | Negative integer | 0x20..0x37 |
number_integer | 0..23 | Integer | 0x00..0x17 |
number_integer | 24..255 | Unsigned integer (1 byte follow) | 0x18 |
number_integer | 256..65535 | Unsigned integer (2 bytes follow) | 0x19 |
number_integer | 65536..4294967295 | Unsigned integer (4 bytes follow) | 0x1A |
number_integer | 4294967296..18446744073709551615 | Unsigned integer (8 bytes follow) | 0x1B |
number_unsigned | 0..23 | Integer | 0x00..0x17 |
number_unsigned | 24..255 | Unsigned integer (1 byte follow) | 0x18 |
number_unsigned | 256..65535 | Unsigned integer (2 bytes follow) | 0x19 |
number_unsigned | 65536..4294967295 | Unsigned integer (4 bytes follow) | 0x1A |
number_unsigned | 4294967296..18446744073709551615 | Unsigned integer (8 bytes follow) | 0x1B |
number_float | any value | Double-Precision Float | 0xFB |
string | length: 0..23 | UTF-8 string | 0x60..0x77 |
string | length: 23..255 | UTF-8 string (1 byte follow) | 0x78 |
string | length: 256..65535 | UTF-8 string (2 bytes follow) | 0x79 |
string | length: 65536..4294967295 | UTF-8 string (4 bytes follow) | 0x7A |
string | length: 4294967296..18446744073709551615 | UTF-8 string (8 bytes follow) | 0x7B |
array | size: 0..23 | array | 0x80..0x97 |
array | size: 23..255 | array (1 byte follow) | 0x98 |
array | size: 256..65535 | array (2 bytes follow) | 0x99 |
array | size: 65536..4294967295 | array (4 bytes follow) | 0x9A |
array | size: 4294967296..18446744073709551615 | array (8 bytes follow) | 0x9B |
object | size: 0..23 | map | 0xA0..0xB7 |
object | size: 23..255 | map (1 byte follow) | 0xB8 |
object | size: 256..65535 | map (2 bytes follow) | 0xB9 |
object | size: 65536..4294967295 | map (4 bytes follow) | 0xBA |
object | size: 4294967296..18446744073709551615 | map (8 bytes follow) | 0xBB |
null
.[in] | j | JSON value to serialize |
@complexity Linear in the size of the JSON value j.
@liveexample{The example shows the serialization of a JSON value to a byte vector in CBOR format.,to_cbor}
|
static |
|
static |
|
static |
|
static |
create a MessagePack serialization of a given JSON value
Serializes a given JSON value j to a byte vector using the MessagePack serialization format. MessagePack is a binary serialization format which aims to be more compact than JSON itself, yet more efficient to parse.
The library uses the following mapping from JSON values types to MessagePack types according to the MessagePack specification:
JSON value type | value/range | MessagePack type | first byte |
---|---|---|---|
null | null | nil | 0xC0 |
boolean | true | true | 0xC3 |
boolean | false | false | 0xC2 |
number_integer | -9223372036854775808..-2147483649 | int64 | 0xD3 |
number_integer | -2147483648..-32769 | int32 | 0xD2 |
number_integer | -32768..-129 | int16 | 0xD1 |
number_integer | -128..-33 | int8 | 0xD0 |
number_integer | -32..-1 | negative fixint | 0xE0..0xFF |
number_integer | 0..127 | positive fixint | 0x00..0x7F |
number_integer | 128..255 | uint 8 | 0xCC |
number_integer | 256..65535 | uint 16 | 0xCD |
number_integer | 65536..4294967295 | uint 32 | 0xCE |
number_integer | 4294967296..18446744073709551615 | uint 64 | 0xCF |
number_unsigned | 0..127 | positive fixint | 0x00..0x7F |
number_unsigned | 128..255 | uint 8 | 0xCC |
number_unsigned | 256..65535 | uint 16 | 0xCD |
number_unsigned | 65536..4294967295 | uint 32 | 0xCE |
number_unsigned | 4294967296..18446744073709551615 | uint 64 | 0xCF |
number_float | any value | float 64 | 0xCB |
string | length: 0..31 | fixstr | 0xA0..0xBF |
string | length: 32..255 | str 8 | 0xD9 |
string | length: 256..65535 | str 16 | 0xDA |
string | length: 65536..4294967295 | str 32 | 0xDB |
array | size: 0..15 | fixarray | 0x90..0x9F |
array | size: 16..65535 | array 16 | 0xDC |
array | size: 65536..4294967295 | array 32 | 0xDD |
object | size: 0..15 | fix map | 0x80..0x8F |
object | size: 16..65535 | map 16 | 0xDE |
object | size: 65536..4294967295 | map 32 | 0xDF |
null
.[in] | j | JSON value to serialize |
@complexity Linear in the size of the JSON value j.
@liveexample{The example shows the serialization of a JSON value to a byte vector in MessagePack format.,to_msgpack}
|
static |
|
static |
|
static |
|
static |
create a UBJSON serialization of a given JSON value
Serializes a given JSON value j to a byte vector using the UBJSON (Universal Binary JSON) serialization format. UBJSON aims to be more compact than JSON itself, yet more efficient to parse.
The library uses the following mapping from JSON values types to UBJSON types according to the UBJSON specification:
JSON value type | value/range | UBJSON type | marker |
---|---|---|---|
null | null | null | Z |
boolean | true | true | T |
boolean | false | false | F |
number_integer | -9223372036854775808..-2147483649 | int64 | L |
number_integer | -2147483648..-32769 | int32 | l |
number_integer | -32768..-129 | int16 | I |
number_integer | -128..127 | int8 | i |
number_integer | 128..255 | uint8 | U |
number_integer | 256..32767 | int16 | I |
number_integer | 32768..2147483647 | int32 | l |
number_integer | 2147483648..9223372036854775807 | int64 | L |
number_unsigned | 0..127 | int8 | i |
number_unsigned | 128..255 | uint8 | U |
number_unsigned | 256..32767 | int16 | I |
number_unsigned | 32768..2147483647 | int32 | l |
number_unsigned | 2147483648..9223372036854775807 | int64 | L |
number_float | any value | float64 | D |
string | with shortest length indicator | string | S |
array | see notes on optimized format | array | [ |
object | see notes on optimized format | map | { |
Z
: no-op values are not created.C
: single-byte strings are serialized with S
markers.null
.[in] | j | JSON value to serialize |
[in] | use_size | whether to add size annotations to container types |
[in] | use_type | whether to add type annotations to container types (must be combined with use_size = true) |
@complexity Linear in the size of the JSON value j.
@liveexample{The example shows the serialization of a JSON value to a byte vector in UBJSON format.,to_ubjson}
|
static |
|
static |
|
static |
|
inlinenoexcept |
return the type of the JSON value (explicit)
Return the type of the JSON value as a value from the value_t enumeration.
Value type | return value |
---|---|
null | value_t::null |
boolean | value_t::boolean |
string | value_t::string |
number (integer) | value_t::number_integer |
number (unsigned integer) | value_t::number_unsigned |
number (floating-point) | value_t::number_float |
object | value_t::object |
array | value_t::array |
discarded | value_t::discarded |
@complexity Constant.
@exceptionsafety No-throw guarantee: this member function never throws exceptions.
@liveexample{The following code exemplifies type()
for all JSON types.,type}
|
noexcept |
return the type as string
Returns the type name as string to be used in error messages - usually to indicate that a function was called on a wrong JSON type.
Value type | return value |
---|---|
null | "null" |
boolean | "boolean" |
string | "string" |
number | "number" (for all number types) |
object | "object" |
array | "array" |
discarded | "discarded" |
@exceptionsafety No-throw guarantee: this function never throws exceptions.
@complexity Constant.
@liveexample{The following code exemplifies type_name()
for all JSON types.,type_name}
const char*
and noexcept
since 3.0.0
|
inline |
unflatten a previously flattened JSON value
The function restores the arbitrary nesting of a JSON value that has been flattened before using the flatten() function. The JSON value must meet certain constraints:
null
values and can not unflattened to their original type. Apart from this example, for a JSON value j
, the following is always true: j == j.flatten().unflatten()
.@complexity Linear in the size the JSON value.
type_error.314 | if value is not an object |
type_error.315 | if object values are not primitive |
@liveexample{The following code shows how a flattened JSON object is unflattened into the original nested JSON object.,unflatten}
void wpi::json::update | ( | const_iterator | first, |
const_iterator | last | ||
) |
updates a JSON object from another object, overwriting existing keys
Inserts all values from from range [first, last)
and overwrites existing keys.
[in] | first | begin of the range of elements to insert |
[in] | last | end of the range of elements to insert |
type_error.312 | if called on JSON values other than objects; example: "cannot use update() with string" |
invalid_iterator.202 | if iterator first or last does does not point to an object; example: "iterators first and last must point to
objects" |
invalid_iterator.210 | if first and last do not belong to the same JSON value; example: "iterators do not fit" |
@complexity O(N*log(size() + N)), where N is the number of elements to insert.
@liveexample{The example shows how update()
is used__range.,update}
void wpi::json::update | ( | const_reference | j | ) |
updates a JSON object from another object, overwriting existing keys
Inserts all values from JSON object j and overwrites existing keys.
[in] | j | JSON object to read values from |
type_error.312 | if called on JSON values other than objects; example: "cannot use update() with string" |
@complexity O(N*log(size() + N)), where N is the number of elements to insert.
@liveexample{The example shows how update()
is used.,update}
|
inline |
overload for a default value of type const char*
|
inline |
access specified object element via JSON Pointer with default value
Returns either a copy of an object's element at the specified key key or a given default value if no element with key key exists.
The function is basically equivalent to executing
[in] | ptr | a JSON pointer to the element to access |
[in] | default_value | the value to return if ptr found no value |
ValueType | type compatible to JSON values, for instance int for JSON integer numbers, bool for JSON booleans, or std::vector types for JSON arrays. Note the type of the expected value at key and the default value default_value must be compatible. |
type_error.306 | if the JSON value is not an object; in that case, using value() with a key makes no sense. |
@complexity Logarithmic in the size of the container.
@liveexample{The example below shows how object elements can be queried with a default value.,json__value_ptr}
|
inline |
overload for a default value of type const char*
|
inline |
access specified object element with default value
Returns either a copy of an object's element at the specified key key or a given default value if no element with key key exists.
The function is basically equivalent to executing
[in] | key | key of the element to access |
[in] | default_value | the value to return if key is not found |
ValueType | type compatible to JSON values, for instance int for JSON integer numbers, bool for JSON booleans, or std::vector types for JSON arrays. Note the type of the expected value at key and the default value default_value must be compatible. |
type_error.306 | if the JSON value is not an object; in that case, using value() with a key makes no sense. |
@complexity Logarithmic in the size of the container.
@liveexample{The example below shows how object elements can be queried with a default value.,json__value}
|
friend |
|
friend |
|
friend |
|
friend |
comparison: not equal
comparison: not equal
Compares two JSON values for inequality by calculating not (lhs == rhs)
.
[in] | lhs | first JSON value to consider |
[in] | rhs | second JSON value to consider |
@complexity Linear.
@exceptionsafety No-throw guarantee: this function never throws exceptions.
@liveexample{The example demonstrates comparing several JSON types.,operator__notequal}
|
friend |
comparison: not equal
comparison: not equal
Compares two JSON values for inequality by calculating not (lhs == rhs)
.
[in] | lhs | first JSON value to consider |
[in] | rhs | second JSON value to consider |
@complexity Linear.
@exceptionsafety No-throw guarantee: this function never throws exceptions.
@liveexample{The example demonstrates comparing several JSON types.,operator__notequal}
|
friend |
comparison: not equal
Compares two JSON values for inequality by calculating not (lhs == rhs)
.
[in] | lhs | first JSON value to consider |
[in] | rhs | second JSON value to consider |
@complexity Linear.
@exceptionsafety No-throw guarantee: this function never throws exceptions.
@liveexample{The example demonstrates comparing several JSON types.,operator__notequal}
|
friend |
comparison: less than
comparison: less than
Compares whether one JSON value lhs is less than another JSON value rhs according to the following rules:
<
operator.[in] | lhs | first JSON value to consider |
[in] | rhs | second JSON value to consider |
@complexity Linear.
@exceptionsafety No-throw guarantee: this function never throws exceptions.
@liveexample{The example demonstrates comparing several JSON types.,operator__less}
|
friend |
comparison: less than
comparison: less than
Compares whether one JSON value lhs is less than another JSON value rhs according to the following rules:
<
operator.[in] | lhs | first JSON value to consider |
[in] | rhs | second JSON value to consider |
@complexity Linear.
@exceptionsafety No-throw guarantee: this function never throws exceptions.
@liveexample{The example demonstrates comparing several JSON types.,operator__less}
|
friend |
comparison: less than
Compares whether one JSON value lhs is less than another JSON value rhs according to the following rules:
<
operator.[in] | lhs | first JSON value to consider |
[in] | rhs | second JSON value to consider |
@complexity Linear.
@exceptionsafety No-throw guarantee: this function never throws exceptions.
@liveexample{The example demonstrates comparing several JSON types.,operator__less}
|
friend |
serialize to stream
Serialize the given JSON value j to the output stream o. The JSON value will be serialized using the dump member function.
width
of the output stream o. For instance, using the manipulator std::setw(4)
on o sets the indentation level to 4
and the serialization result is the same as calling dump(4)
.fill
of the output stream o. For instance, the manipulator ‘std::setfill(’\t')` sets indentation to use a tab character rather than the default space character.[in,out] | o | stream to serialize to |
[in] | j | JSON value to serialize |
type_error.316 | if a string stored inside the JSON value is not UTF-8 encoded |
@complexity Linear.
@liveexample{The example below shows the serialization with different parameters to width
to adjust the indentation level.,operator_serialize}
|
friend |
comparison: less than or equal
comparison: less than or equal
Compares whether one JSON value lhs is less than or equal to another JSON value by calculating not (rhs < lhs)
.
[in] | lhs | first JSON value to consider |
[in] | rhs | second JSON value to consider |
@complexity Linear.
@exceptionsafety No-throw guarantee: this function never throws exceptions.
@liveexample{The example demonstrates comparing several JSON types.,operator__greater}
|
friend |
comparison: less than or equal
comparison: less than or equal
Compares whether one JSON value lhs is less than or equal to another JSON value by calculating not (rhs < lhs)
.
[in] | lhs | first JSON value to consider |
[in] | rhs | second JSON value to consider |
@complexity Linear.
@exceptionsafety No-throw guarantee: this function never throws exceptions.
@liveexample{The example demonstrates comparing several JSON types.,operator__greater}
|
friend |
comparison: less than or equal
Compares whether one JSON value lhs is less than or equal to another JSON value by calculating not (rhs < lhs)
.
[in] | lhs | first JSON value to consider |
[in] | rhs | second JSON value to consider |
@complexity Linear.
@exceptionsafety No-throw guarantee: this function never throws exceptions.
@liveexample{The example demonstrates comparing several JSON types.,operator__greater}
|
friend |
comparison: equal
comparison: equal
Compares two JSON values for equality according to the following rules:
operator==
.double::operator==
. To compare floating-point while respecting an epsilon, an alternative comparison function could be used, for instance [in] | lhs | first JSON value to consider |
[in] | rhs | second JSON value to consider |
@exceptionsafety No-throw guarantee: this function never throws exceptions.
@complexity Linear.
@liveexample{The example demonstrates comparing several JSON types.,operator__equal}
|
friend |
comparison: equal
comparison: equal
Compares two JSON values for equality according to the following rules:
operator==
.double::operator==
. To compare floating-point while respecting an epsilon, an alternative comparison function could be used, for instance [in] | lhs | first JSON value to consider |
[in] | rhs | second JSON value to consider |
@exceptionsafety No-throw guarantee: this function never throws exceptions.
@complexity Linear.
@liveexample{The example demonstrates comparing several JSON types.,operator__equal}
|
friend |
comparison: equal
Compares two JSON values for equality according to the following rules:
operator==
.double::operator==
. To compare floating-point while respecting an epsilon, an alternative comparison function could be used, for instance [in] | lhs | first JSON value to consider |
[in] | rhs | second JSON value to consider |
@exceptionsafety No-throw guarantee: this function never throws exceptions.
@complexity Linear.
@liveexample{The example demonstrates comparing several JSON types.,operator__equal}
|
friend |
comparison: greater than
comparison: greater than
Compares whether one JSON value lhs is greater than another JSON value by calculating not (lhs <= rhs)
.
[in] | lhs | first JSON value to consider |
[in] | rhs | second JSON value to consider |
@complexity Linear.
@exceptionsafety No-throw guarantee: this function never throws exceptions.
@liveexample{The example demonstrates comparing several JSON types.,operator__lessequal}
|
friend |
comparison: greater than
comparison: greater than
Compares whether one JSON value lhs is greater than another JSON value by calculating not (lhs <= rhs)
.
[in] | lhs | first JSON value to consider |
[in] | rhs | second JSON value to consider |
@complexity Linear.
@exceptionsafety No-throw guarantee: this function never throws exceptions.
@liveexample{The example demonstrates comparing several JSON types.,operator__lessequal}
|
friend |
comparison: greater than
Compares whether one JSON value lhs is greater than another JSON value by calculating not (lhs <= rhs)
.
[in] | lhs | first JSON value to consider |
[in] | rhs | second JSON value to consider |
@complexity Linear.
@exceptionsafety No-throw guarantee: this function never throws exceptions.
@liveexample{The example demonstrates comparing several JSON types.,operator__lessequal}
|
friend |
comparison: greater than or equal
comparison: greater than or equal
Compares whether one JSON value lhs is greater than or equal to another JSON value by calculating not (lhs < rhs)
.
[in] | lhs | first JSON value to consider |
[in] | rhs | second JSON value to consider |
@complexity Linear.
@exceptionsafety No-throw guarantee: this function never throws exceptions.
@liveexample{The example demonstrates comparing several JSON types.,operator__greaterequal}
|
friend |
comparison: greater than or equal
comparison: greater than or equal
Compares whether one JSON value lhs is greater than or equal to another JSON value by calculating not (lhs < rhs)
.
[in] | lhs | first JSON value to consider |
[in] | rhs | second JSON value to consider |
@complexity Linear.
@exceptionsafety No-throw guarantee: this function never throws exceptions.
@liveexample{The example demonstrates comparing several JSON types.,operator__greaterequal}
|
friend |
comparison: greater than or equal
Compares whether one JSON value lhs is greater than or equal to another JSON value by calculating not (lhs < rhs)
.
[in] | lhs | first JSON value to consider |
[in] | rhs | second JSON value to consider |
@complexity Linear.
@exceptionsafety No-throw guarantee: this function never throws exceptions.
@liveexample{The example demonstrates comparing several JSON types.,operator__greaterequal}
|
friend |
deserialize from stream
Deserializes an input stream to a JSON value.
[in,out] | i | input stream to read a serialized JSON value from |
[in,out] | j | JSON value to write the deserialized input to |
parse_error.101 | in case of an unexpected token |
parse_error.102 | if to_unicode fails or surrogate error |
parse_error.103 | if to_unicode fails |
@complexity Linear in the length of the input. The parser is a predictive LL(1) parser.
@liveexample{The example below shows how a JSON value is constructed by reading a serialization from a stream.,operator_deserialize}