All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Classes | Public Types | Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | Friends | List of all members
nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > Class Template Reference

a class to store JSON values More...

#include <json.hpp>

Classes

union  json_value
 a JSON value More...
 

Public Types

using value_t = detail::value_t
 
using json_pointer = ::nlohmann::json_pointer< basic_json >
 JSON Pointer, see nlohmann::json_pointer. More...
 
template<typename T , typename SFINAE >
using json_serializer = JSONSerializer< T, SFINAE >
 
using error_handler_t = detail::error_handler_t
 how to treat decoding errors More...
 
using initializer_list_t = std::initializer_list< detail::json_ref< basic_json >>
 helper type for initializer lists of basic_json values More...
 
using input_format_t = detail::input_format_t
 
using json_sax_t = json_sax< basic_json >
 SAX interface type, see nlohmann::json_sax. More...
 
using parse_event_t = typename parser::parse_event_t
 parser event types More...
 
using parser_callback_t = typename parser::parser_callback_t
 per-element parser callback type More...
 

Public Member Functions

JSON_HEDLEY_RETURNS_NON_NULL
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_HEDLEY_WARN_UNUSED_RESULT
basic_json 
meta ()
 returns version information on the library More...
 

Private Types

using basic_json_t = NLOHMANN_BASIC_JSON_TPL
 workaround type for MSVC More...
 
using lexer = ::nlohmann::detail::lexer< basic_json >
 
using parser = ::nlohmann::detail::parser< basic_json >
 
using primitive_iterator_t = ::nlohmann::detail::primitive_iterator_t
 
template<typename BasicJsonType >
using internal_iterator = ::nlohmann::detail::internal_iterator< BasicJsonType >
 
template<typename BasicJsonType >
using iter_impl = ::nlohmann::detail::iter_impl< BasicJsonType >
 
template<typename Iterator >
using iteration_proxy = ::nlohmann::detail::iteration_proxy< Iterator >
 
template<typename Base >
using json_reverse_iterator = ::nlohmann::detail::json_reverse_iterator< Base >
 
template<typename CharType >
using output_adapter_t = ::nlohmann::detail::output_adapter_t< CharType >
 
using binary_reader = ::nlohmann::detail::binary_reader< basic_json >
 
template<typename CharType >
using binary_writer = ::nlohmann::detail::binary_writer< basic_json, CharType >
 
using serializer = ::nlohmann::detail::serializer< basic_json >
 

Private Member Functions

void assert_invariant () const noexcept
 checks the class invariants More...
 
boolean_t get_impl (boolean_t *) const
 get a boolean (explicit) More...
 
object_tget_impl_ptr (object_t *) noexcept
 get a pointer to the value (object) More...
 
constexpr const object_tget_impl_ptr (const object_t *) const noexcept
 get a pointer to the value (object) More...
 
array_tget_impl_ptr (array_t *) noexcept
 get a pointer to the value (array) More...
 
constexpr const array_tget_impl_ptr (const array_t *) const noexcept
 get a pointer to the value (array) More...
 
string_tget_impl_ptr (string_t *) noexcept
 get a pointer to the value (string) More...
 
constexpr const string_tget_impl_ptr (const string_t *) const noexcept
 get a pointer to the value (string) More...
 
boolean_tget_impl_ptr (boolean_t *) noexcept
 get a pointer to the value (boolean) More...
 
constexpr const boolean_tget_impl_ptr (const boolean_t *) const noexcept
 get a pointer to the value (boolean) More...
 
number_integer_tget_impl_ptr (number_integer_t *) noexcept
 get a pointer to the value (integer number) More...
 
constexpr const number_integer_tget_impl_ptr (const number_integer_t *) const noexcept
 get a pointer to the value (integer number) More...
 
number_unsigned_tget_impl_ptr (number_unsigned_t *) noexcept
 get a pointer to the value (unsigned number) More...
 
constexpr const number_unsigned_tget_impl_ptr (const number_unsigned_t *) const noexcept
 get a pointer to the value (unsigned number) More...
 
number_float_tget_impl_ptr (number_float_t *) noexcept
 get a pointer to the value (floating-point number) More...
 
constexpr const number_float_tget_impl_ptr (const number_float_t *) const noexcept
 get a pointer to the value (floating-point number) More...
 

Static Private Member Functions

template<typename T , typename... Args>
static
JSON_HEDLEY_RETURNS_NON_NULL T * 
create (Args &&...args)
 helper for exception-safe object creation More...
 
template<typename ReferenceType , typename ThisType >
static ReferenceType get_ref_impl (ThisType &obj)
 helper function to implement get_ref() More...
 

Private Attributes

value_t m_type = value_t::null
 the type of the current element More...
 
json_value m_value = {}
 the value of the current element More...
 

Friends

template<detail::value_t >
struct detail::external_constructor
 
template<typename BasicJsonType >
class ::nlohmann::detail::iter_impl
 
template<typename BasicJsonType , typename CharType >
class ::nlohmann::detail::binary_writer
 
template<typename BasicJsonType , typename SAX >
class ::nlohmann::detail::binary_reader
 
template<typename BasicJsonType >
class ::nlohmann::detail::json_sax_dom_parser
 
template<typename BasicJsonType >
class ::nlohmann::detail::json_sax_dom_callback_parser
 

exceptions

Classes to implement user-defined exceptions.

using exception = detail::exception
 general exception of the basic_json class More...
 
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 basic_json like any other STL container.

using value_type = basic_json
 the type of elements in a basic_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 = AllocatorType< basic_json >
 the allocator type More...
 
using pointer = typename std::allocator_traits< allocator_type >::pointer
 the type of an element pointer More...
 
using const_pointer = typename std::allocator_traits< allocator_type >::const_pointer
 the type of an element const pointer More...
 
using iterator = iter_impl< basic_json >
 an iterator for a basic_json container More...
 
using const_iterator = iter_impl< const basic_json >
 a const iterator for a basic_json container More...
 
using reverse_iterator = json_reverse_iterator< typename basic_json::iterator >
 a reverse iterator for a basic_json container More...
 
using const_reverse_iterator = json_reverse_iterator< typename basic_json::const_iterator >
 a const reverse iterator for a basic_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 basic_json.

using object_comparator_t = std::less< StringType >
 
using object_t = ObjectType< StringType, basic_json, object_comparator_t, AllocatorType< std::pair< const StringType, basic_json >>>
 a type for an object More...
 
using array_t = ArrayType< basic_json, AllocatorType< basic_json >>
 a type for an array More...
 
using string_t = StringType
 a type for a string More...
 
using boolean_t = BooleanType
 a type for a boolean More...
 
using number_integer_t = NumberIntegerType
 a type for a number (integer) More...
 
using number_unsigned_t = NumberUnsignedType
 a type for a number (unsigned) More...
 
using number_float_t = NumberFloatType
 a type for a number (floating-point) More...
 

constructors and destructors

Constructors of class basic_json, copy/move constructor, copy assignment, static functions creating objects, and the destructor.

static
JSON_HEDLEY_WARN_UNUSED_RESULT
basic_json 
array (initializer_list_t init={})
 explicitly create an array from an initializer list More...
 
static
JSON_HEDLEY_WARN_UNUSED_RESULT
basic_json 
object (initializer_list_t init={})
 explicitly create an object from an initializer list More...
 
 basic_json (const detail::json_ref< basic_json > &ref)
 
 basic_json (const value_t v)
 create an empty value with a given type More...
 
 basic_json (std::nullptr_t=nullptr) noexcept
 create a null object More...
 
template<typename CompatibleType , typename U = detail::uncvref_t<CompatibleType>, detail::enable_if_t< not detail::is_basic_json< U >::value and detail::is_compatible_type< basic_json_t, U >::value, int > = 0>
 basic_json (CompatibleType &&val) noexcept(noexcept(JSONSerializer< U >::to_json(std::declval< basic_json_t & >(), std::forward< CompatibleType >(val))))
 create a JSON value More...
 
template<typename BasicJsonType , detail::enable_if_t< detail::is_basic_json< BasicJsonType >::value and not std::is_same< basic_json, BasicJsonType >::value, int > = 0>
 basic_json (const BasicJsonType &val)
 create a JSON value from an existing one More...
 
 basic_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...
 
 basic_json (size_type cnt, const basic_json &val)
 construct an array with count copies of given value More...
 
template<class InputIT , typename std::enable_if< std::is_same< InputIT, typename basic_json_t::iterator >::value orstd::is_same< InputIT, typename basic_json_t::const_iterator >::value, int >::type = 0>
 basic_json (InputIT first, InputIT last)
 construct a JSON container given an iterator range More...
 
 basic_json (const basic_json &other)
 copy constructor More...
 
 basic_json (basic_json &&other) noexcept
 move constructor More...
 
basic_jsonoperator= (basic_json other) noexcept(std::is_nothrow_move_constructible< value_t >::value andstd::is_nothrow_move_assignable< value_t >::value andstd::is_nothrow_move_constructible< json_value >::value andstd::is_nothrow_move_assignable< json_value >::value)
 copy assignment More...
 
 ~basic_json () noexcept
 destructor More...
 

object inspection

Functions to inspect the type of a JSON value.

string_t dump (const int indent=-1, const char indent_char= ' ', const bool ensure_ascii=false, const error_handler_t error_handler=error_handler_t::strict) const
 serialization More...
 
constexpr value_t type () const noexcept
 return the type of the JSON value (explicit) More...
 
constexpr bool is_primitive () const noexcept
 return whether type is primitive More...
 
constexpr bool is_structured () const noexcept
 return whether type is structured More...
 
constexpr bool is_null () const noexcept
 return whether value is null More...
 
constexpr bool is_boolean () const noexcept
 return whether value is a boolean More...
 
constexpr bool is_number () const noexcept
 return whether value is a number More...
 
constexpr bool is_number_integer () const noexcept
 return whether value is an integer number More...
 
constexpr bool is_number_unsigned () const noexcept
 return whether value is an unsigned integer number More...
 
constexpr bool is_number_float () const noexcept
 return whether value is a floating-point number More...
 
constexpr bool is_object () const noexcept
 return whether value is an object More...
 
constexpr bool is_array () const noexcept
 return whether value is an array More...
 
constexpr bool is_string () const noexcept
 return whether value is a string More...
 
constexpr bool is_discarded () const noexcept
 return whether value is discarded More...
 
constexpr 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, basic_json_t >::value, int > = 0>
basic_json get () const
 get special-case overload More...
 
template<typename BasicJsonType , detail::enable_if_t< not std::is_same< BasicJsonType, basic_json >::value anddetail::is_basic_json< BasicJsonType >::value, int > = 0>
BasicJsonType get () const
 get special-case overload More...
 
template<typename ValueTypeCV , typename ValueType = detail::uncvref_t<ValueTypeCV>, detail::enable_if_t< not detail::is_basic_json< ValueType >::value anddetail::has_from_json< basic_json_t, ValueType >::value andnot detail::has_non_default_from_json< basic_json_t, ValueType >::value, int > = 0>
ValueType get () const noexcept(noexcept(JSONSerializer< ValueType >::from_json(std::declval< const basic_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< basic_json_t, ValueType >::value anddetail::has_non_default_from_json< basic_json_t, ValueType >::value, int > = 0>
ValueType get () const noexcept(noexcept(JSONSerializer< ValueType >::from_json(std::declval< const basic_json_t & >())))
 get a value (explicit); special case More...
 
template<typename ValueType , detail::enable_if_t< not detail::is_basic_json< ValueType >::value anddetail::has_from_json< basic_json_t, ValueType >::value, int > = 0>
ValueType & get_to (ValueType &v) const noexcept(noexcept(JSONSerializer< ValueType >::from_json(std::declval< const basic_json_t & >(), v)))
 get a value (explicit) More...
 
template<typename T , std::size_t N, typename Array = T (&)[N], detail::enable_if_t< detail::has_from_json< basic_json_t, Array >::value, int > = 0>
Array get_to (T(&v)[N]) const noexcept(noexcept(JSONSerializer< Array >::from_json(std::declval< const basic_json_t & >(), v)))
 
template<typename PointerType , typename std::enable_if< std::is_pointer< PointerType >::value, int >::type = 0>
auto get_ptr () noexcept-> decltype(std::declval< basic_json_t & >().get_impl_ptr(std::declval< PointerType >()))
 get a pointer value (implicit) More...
 
template<typename PointerType , typename std::enable_if< std::is_pointer< PointerType >::value andstd::is_const< typename std::remove_pointer< PointerType >::type >::value, int >::type = 0>
constexpr auto get_ptr () const noexcept-> decltype(std::declval< const basic_json_t & >().get_impl_ptr(std::declval< PointerType >()))
 get a pointer value (implicit) More...
 
template<typename PointerType , typename std::enable_if< std::is_pointer< PointerType >::value, int >::type = 0>
auto get () noexcept-> decltype(std::declval< basic_json_t & >().template get_ptr< PointerType >())
 get a pointer value (explicit) More...
 
template<typename PointerType , typename std::enable_if< std::is_pointer< PointerType >::value, int >::type = 0>
constexpr auto get () const noexcept-> decltype(std::declval< const basic_json_t & >().template get_ptr< PointerType >())
 get a pointer value (explicit) 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 andstd::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 andnot std::is_same< ValueType, detail::json_ref< basic_json >>::value andnot std::is_same< ValueType, typename string_t::value_type >::value andnot detail::is_basic_json< ValueType >::valueand not std::is_same< ValueType, std::initializer_list< typename string_t::value_type >>::valueand not std::is_same< ValueType, typename std::string_view >::valueand detail::is_detected< detail::get_template_function, const basic_json_t &, ValueType >::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 (const typename object_t::key_type &key)
 access specified object element with bounds checking More...
 
const_reference at (const typename object_t::key_type &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[] (const typename object_t::key_type &key)
 access specified object element More...
 
const_reference operator[] (const typename object_t::key_type &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< basic_json_t, ValueType >::value, int >::type = 0>
ValueType value (const typename object_t::key_type &key, const ValueType &default_value) const
 access specified object element with default value More...
 
string_t value (const typename object_t::key_type &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< basic_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...
 
string_t 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 basic_json_t::iterator >::value orstd::is_same< IteratorType, typename basic_json_t::const_iterator >::value, int >::type = 0>
IteratorType erase (IteratorType pos)
 remove element given an iterator More...
 
template<class IteratorType , typename std::enable_if< std::is_same< IteratorType, typename basic_json_t::iterator >::value orstd::is_same< IteratorType, typename basic_json_t::const_iterator >::value, int >::type = 0>
IteratorType erase (IteratorType first, IteratorType last)
 remove elements given an iterator range More...
 
size_type erase (const typename object_t::key_type &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

template<typename KeyT >
iterator find (KeyT &&key)
 find an element in a JSON object More...
 
template<typename KeyT >
const_iterator find (KeyT &&key) const
 find an element in a JSON object More...
 
template<typename KeyT >
size_type count (KeyT &&key) const
 returns the number of occurrences of a key in a JSON object More...
 
template<typename KeyT , typename std::enable_if< not std::is_same< typename std::decay< KeyT >::type, json_pointer >::value, int >::type = 0>
bool contains (KeyT &&key) const
 check the existence of an element in a JSON object More...
 
bool contains (const json_pointer &ptr) const
 check the existence of an element in a JSON object given a JSON pointer More...
 

iterators

static iteration_proxy< iteratoriterator_wrapper (reference ref) noexcept
 wrapper to access iterator member functions in range-based for More...
 
static iteration_proxy
< const_iterator
iterator_wrapper (const_reference ref) noexcept
 wrapper to access iterator member functions in range-based for More...
 
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< iteratoritems () noexcept
 helper to access iterator member functions in range-based for More...
 
iteration_proxy< const_iteratoritems () 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 (basic_json &&val)
 add an object to an array More...
 
reference operator+= (basic_json &&val)
 add an object to an array More...
 
void push_back (const basic_json &val)
 add an object to an array More...
 
reference operator+= (const basic_json &val)
 add an object to an array More...
 
void push_back (const typename object_t::value_type &val)
 add an object to an object More...
 
reference operator+= (const typename object_t::value_type &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>
reference emplace_back (Args &&...args)
 add an object to an array More...
 
template<class... Args>
std::pair< iterator, bool > emplace (Args &&...args)
 add an object to an object if key does not exist More...
 
template<typename... Args>
iterator insert_iterator (const_iterator pos, Args &&...args)
 
iterator insert (const_iterator pos, const basic_json &val)
 inserts element More...
 
iterator insert (const_iterator pos, basic_json &&val)
 inserts element More...
 
iterator insert (const_iterator pos, size_type cnt, const basic_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 andstd::is_nothrow_move_assignable< value_t >::value andstd::is_nothrow_move_constructible< json_value >::value andstd::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 (string_t &other)
 exchanges the values 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

std::ostream & operator<< (std::ostream &o, const basic_json &j)
 serialize to stream More...
 
std::ostream & operator>> (const basic_json &j, std::ostream &o)
 serialize to stream More...
 

deserialization

std::istream & operator<< (basic_json &j, std::istream &i)
 deserialize from stream More...
 
std::istream & operator>> (std::istream &i, basic_json &j)
 deserialize from stream More...
 
static
JSON_HEDLEY_WARN_UNUSED_RESULT
basic_json 
parse (detail::input_adapter &&i, const parser_callback_t cb=nullptr, const bool allow_exceptions=true)
 deserialize from a compatible input More...
 
static bool accept (detail::input_adapter &&i)
 
template<typename SAX >
static bool sax_parse (detail::input_adapter &&i, SAX *sax, input_format_t format=input_format_t::json, const bool strict=true)
 generate SAX events More...
 
template<class IteratorType , typename std::enable_if< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< IteratorType >::iterator_category >::value, int >::type = 0>
static basic_json parse (IteratorType first, IteratorType last, const parser_callback_t cb=nullptr, const bool allow_exceptions=true)
 deserialize from an iterator range with contiguous storage More...
 
template<class IteratorType , typename std::enable_if< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< IteratorType >::iterator_category >::value, int >::type = 0>
static bool accept (IteratorType first, IteratorType last)
 
template<class IteratorType , class SAX , typename std::enable_if< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< IteratorType >::iterator_category >::value, int >::type = 0>
static bool sax_parse (IteratorType first, IteratorType last, SAX *sax)
 

binary serialization/deserialization support

static std::vector< uint8_t > to_cbor (const basic_json &j)
 create a CBOR serialization of a given JSON value More...
 
static void to_cbor (const basic_json &j, detail::output_adapter< uint8_t > o)
 
static void to_cbor (const basic_json &j, detail::output_adapter< char > o)
 
static std::vector< uint8_t > to_msgpack (const basic_json &j)
 create a MessagePack serialization of a given JSON value More...
 
static void to_msgpack (const basic_json &j, detail::output_adapter< uint8_t > o)
 
static void to_msgpack (const basic_json &j, detail::output_adapter< char > o)
 
static std::vector< uint8_t > to_ubjson (const basic_json &j, const bool use_size=false, const bool use_type=false)
 create a UBJSON serialization of a given JSON value More...
 
static void to_ubjson (const basic_json &j, detail::output_adapter< uint8_t > o, const bool use_size=false, const bool use_type=false)
 
static void to_ubjson (const basic_json &j, detail::output_adapter< char > o, const bool use_size=false, const bool use_type=false)
 
static std::vector< uint8_t > to_bson (const basic_json &j)
 Serializes the given JSON object j to BSON and returns a vector containing the corresponding BSON-representation. More...
 
static void to_bson (const basic_json &j, detail::output_adapter< uint8_t > o)
 Serializes the given JSON object j to BSON and forwards the corresponding BSON-representation to the given output_adapter o. More...
 
static void to_bson (const basic_json &j, detail::output_adapter< char > o)
 Serializes the given JSON object j to BSON and forwards the corresponding BSON-representation to the given output_adapter o. More...
 
static
JSON_HEDLEY_WARN_UNUSED_RESULT
basic_json 
from_cbor (detail::input_adapter &&i, const bool strict=true, const bool allow_exceptions=true)
 create a JSON value from an input in CBOR format More...
 
template<typename A1 , typename A2 , detail::enable_if_t< std::is_constructible< detail::input_adapter, A1, A2 >::value, int > = 0>
static
JSON_HEDLEY_WARN_UNUSED_RESULT
basic_json 
from_cbor (A1 &&a1, A2 &&a2, const bool strict=true, const bool allow_exceptions=true)
 create a JSON value from an input in CBOR format More...
 
static
JSON_HEDLEY_WARN_UNUSED_RESULT
basic_json 
from_msgpack (detail::input_adapter &&i, const bool strict=true, const bool allow_exceptions=true)
 create a JSON value from an input in MessagePack format More...
 
template<typename A1 , typename A2 , detail::enable_if_t< std::is_constructible< detail::input_adapter, A1, A2 >::value, int > = 0>
static
JSON_HEDLEY_WARN_UNUSED_RESULT
basic_json 
from_msgpack (A1 &&a1, A2 &&a2, const bool strict=true, const bool allow_exceptions=true)
 create a JSON value from an input in MessagePack format More...
 
static
JSON_HEDLEY_WARN_UNUSED_RESULT
basic_json 
from_ubjson (detail::input_adapter &&i, const bool strict=true, const bool allow_exceptions=true)
 create a JSON value from an input in UBJSON format More...
 
template<typename A1 , typename A2 , detail::enable_if_t< std::is_constructible< detail::input_adapter, A1, A2 >::value, int > = 0>
static
JSON_HEDLEY_WARN_UNUSED_RESULT
basic_json 
from_ubjson (A1 &&a1, A2 &&a2, const bool strict=true, const bool allow_exceptions=true)
 create a JSON value from an input in UBJSON format More...
 
static
JSON_HEDLEY_WARN_UNUSED_RESULT
basic_json 
from_bson (detail::input_adapter &&i, const bool strict=true, const bool allow_exceptions=true)
 Create a JSON value from an input in BSON format. More...
 
template<typename A1 , typename A2 , detail::enable_if_t< std::is_constructible< detail::input_adapter, A1, A2 >::value, int > = 0>
static
JSON_HEDLEY_WARN_UNUSED_RESULT
basic_json 
from_bson (A1 &&a1, A2 &&a2, const bool strict=true, const bool allow_exceptions=true)
 Create a JSON value from an input in BSON format. 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...
 
basic_json flatten () const
 return flattened JSON value More...
 
basic_json unflatten () const
 unflatten a previously flattened JSON value More...
 

JSON Patch functions

static
JSON_HEDLEY_WARN_UNUSED_RESULT
basic_json 
diff (const basic_json &source, const basic_json &target, const std::string &path="")
 creates a diff as a JSON patch More...
 
basic_json patch (const basic_json &json_patch) const
 applies a JSON patch More...
 

JSON Merge Patch functions

void merge_patch (const basic_json &apply_patch)
 applies a JSON Merge Patch More...
 

Detailed Description

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
class nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >

a class to store JSON values

Template Parameters
ObjectTypetype for JSON objects (std::map by default; will be used in object_t)
ArrayTypetype for JSON arrays (std::vector by default; will be used in array_t)
StringTypetype for JSON strings and object keys (std::string by default; will be used in string_t)
BooleanTypetype for JSON booleans (bool by default; will be used in boolean_t)
NumberIntegerTypetype for JSON integer numbers (int64_t by default; will be used in number_integer_t)
NumberUnsignedTypetype for JSON unsigned integer numbers (uint64_t by default; will be used in number_unsigned_t)
NumberFloatTypetype for JSON floating-point numbers (double by default; will be used in number_float_t)
AllocatorTypetype of the allocator to use (std::allocator by default)
JSONSerializerthe serializer to resolve internal calls to to_json() and from_json() (adl_serializer by default)

The class satisfies the following concept requirements:

Invariant
The member variables m_value and m_type have the following relationship:
See Also
RFC 7159: The JavaScript Object Notation (JSON) Data Interchange Format
Since
version 1.0.0

Definition at line 2421 of file json.hpp.

Member Typedef Documentation

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::allocator_type = AllocatorType<basic_json>

the allocator type

Definition at line 14812 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::array_t = ArrayType<basic_json, AllocatorType<basic_json>>

a type for an array

RFC 7159 describes JSON arrays as follows:

An array is an ordered sequence of zero or more values.

To store objects in C++, a type is defined by the template parameters explained below.

Template Parameters
ArrayTypecontainer type to store arrays (e.g., std::vector or std::list)
AllocatorTypeallocator to use for arrays (e.g., std::allocator)

Default type

With the default values for ArrayType (std::vector) and AllocatorType (std::allocator), the default value for array_t is:

basic_json, // value_type
std::allocator<basic_json> // allocator_type
>

Limits

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.

Storage

Arrays are stored as pointers in a basic_json type. That is, for any access to array values, a pointer of type array_t* must be dereferenced.

See Also
object_ttype for an object value
Since
version 1.0.0

Definition at line 15072 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::basic_json_t = NLOHMANN_BASIC_JSON_TPL
private

workaround type for MSVC

Definition at line 14727 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::binary_reader = ::nlohmann::detail::binary_reader<basic_json>
private

Definition at line 14745 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename CharType >
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::binary_writer = ::nlohmann::detail::binary_writer<basic_json, CharType>
private

Definition at line 14746 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::boolean_t = BooleanType

a type for a boolean

RFC 7159 implicitly describes a boolean as a type which differentiates the two literals true and false.

To store objects in C++, a type is defined by the template parameter BooleanType which chooses the type to use.

Default type

With the default values for BooleanType (bool), the default value for boolean_t is:

bool

Storage

Boolean values are stored directly inside a basic_json type.

Since
version 1.0.0

Definition at line 15151 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::const_iterator = iter_impl<const basic_json>

a const iterator for a basic_json container

Definition at line 14822 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::const_pointer = typename std::allocator_traits<allocator_type>::const_pointer

the type of an element const pointer

Definition at line 14817 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::const_reference = const value_type&

the type of an element const reference

Definition at line 14804 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::const_reverse_iterator = json_reverse_iterator<typename basic_json::const_iterator>

a const reverse iterator for a basic_json container

Definition at line 14826 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::difference_type = std::ptrdiff_t

a type to represent differences between iterators

Definition at line 14807 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::error_handler_t = detail::error_handler_t

how to treat decoding errors

Definition at line 14757 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::exception = detail::exception

general exception of the basic_json class

This class is an extension of std::exception objects with a member id for exception ids. It is used as the base class for all exceptions thrown by the basic_json class. This class can hence be used as "wildcard" to catch exceptions.

Subclasses:

  • parse_error for exceptions indicating a parse error
  • invalid_iterator for exceptions indicating errors with iterators
  • type_error for exceptions indicating executing a member function with a wrong type
  • out_of_range for exceptions indicating access out of the defined range
  • other_error for exceptions indicating other library errors

{The following code shows how arbitrary library exceptions can be caught.,exception}

Since
version 3.0.0

Definition at line 14774 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::initializer_list_t = std::initializer_list<detail::json_ref<basic_json>>

helper type for initializer lists of basic_json values

Definition at line 14759 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::input_format_t = detail::input_format_t

Definition at line 14761 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename BasicJsonType >
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::internal_iterator = ::nlohmann::detail::internal_iterator<BasicJsonType>
private

Definition at line 14735 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::invalid_iterator = detail::invalid_iterator

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 basic_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 basic_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().

{The following code shows how an invalid_iterator exception can be caught.,invalid_iterator}

See Also
- exception for the base class of the library exceptions
- parse_error for exceptions indicating a parse error
- type_error for exceptions indicating executing a member function with a wrong type
- out_of_range for exceptions indicating access out of the defined range
- other_error for exceptions indicating other library errors
Since
version 3.0.0

Definition at line 14778 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename BasicJsonType >
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::iter_impl = ::nlohmann::detail::iter_impl<BasicJsonType>
private

Definition at line 14737 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename Iterator >
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::iteration_proxy = ::nlohmann::detail::iteration_proxy<Iterator>
private

Definition at line 14739 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::iterator = iter_impl<basic_json>

an iterator for a basic_json container

Definition at line 14820 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::json_pointer = ::nlohmann::json_pointer<basic_json>

JSON Pointer, see nlohmann::json_pointer.

Definition at line 14753 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename Base >
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::json_reverse_iterator = ::nlohmann::detail::json_reverse_iterator<Base>
private

Definition at line 14740 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::json_sax_t = json_sax<basic_json>

SAX interface type, see nlohmann::json_sax.

Definition at line 14763 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename T , typename SFINAE >
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::json_serializer = JSONSerializer<T, SFINAE>

Definition at line 14755 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::lexer = ::nlohmann::detail::lexer<basic_json>
private

Definition at line 14730 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::number_float_t = NumberFloatType

a type for a number (floating-point)

RFC 7159 describes numbers as follows:

The representation of numbers is similar to that used in most programming languages. A number is represented in base 10 using decimal digits. It contains an integer component that may be prefixed with an optional minus sign, which may be followed by a fraction part and/or an exponent part. Leading zeros are not allowed. (...) Numeric values that cannot be represented in the grammar below (such as Infinity and NaN) are not permitted.

This description includes both integer and floating-point numbers. However, C++ allows more precise storage if it is known whether the number is a signed integer, an unsigned integer or a floating-point number. Therefore, three different types, number_integer_t, number_unsigned_t and number_float_t are used.

To store floating-point numbers in C++, a type is defined by the template parameter NumberFloatType which chooses the type to use.

Default type

With the default values for NumberFloatType (double), the default value for number_float_t is:

double

Default behavior

  • The restrictions about leading zeros is not enforced in C++. Instead, leading zeros in floating-point literals will be ignored. Internally, the value will be stored as decimal number. For instance, the C++ floating-point literal 01.2 will be serialized to 1.2. During deserialization, leading zeros yield an error.
  • Not-a-number (NaN) values will be serialized to null.

Limits

RFC 7159 states:

This specification allows implementations to set limits on the range and precision of numbers accepted. Since software that implements IEEE 754-2008 binary64 (double precision) numbers is generally available and widely used, good interoperability can be achieved by implementations that expect no more precision or range than these provide, in the sense that implementations will approximate JSON numbers within the expected precision.

This implementation does exactly follow this approach, as it uses double precision floating-point numbers. Note values smaller than -1.79769313486232e+308 and values greater than 1.79769313486232e+308 will be stored as NaN internally and be serialized to null.

Storage

Floating-point number values are stored directly inside a basic_json type.

See Also
number_integer_ttype for number values (integer)
number_unsigned_ttype for number values (unsigned integer)
Since
version 1.0.0

Definition at line 15362 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::number_integer_t = NumberIntegerType

a type for a number (integer)

RFC 7159 describes numbers as follows:

The representation of numbers is similar to that used in most programming languages. A number is represented in base 10 using decimal digits. It contains an integer component that may be prefixed with an optional minus sign, which may be followed by a fraction part and/or an exponent part. Leading zeros are not allowed. (...) Numeric values that cannot be represented in the grammar below (such as Infinity and NaN) are not permitted.

This description includes both integer and floating-point numbers. However, C++ allows more precise storage if it is known whether the number is a signed integer, an unsigned integer or a floating-point number. Therefore, three different types, number_integer_t, number_unsigned_t and number_float_t are used.

To store integer numbers in C++, a type is defined by the template parameter NumberIntegerType which chooses the type to use.

Default type

With the default values for NumberIntegerType (int64_t), the default value for number_integer_t is:

int64_t

Default behavior

  • The restrictions about leading zeros is not enforced in C++. Instead, leading zeros in integer literals lead to an interpretation as octal number. Internally, the value will be stored as decimal number. For instance, the C++ integer literal 010 will be serialized to 8. During deserialization, leading zeros yield an error.
  • Not-a-number (NaN) values will be serialized to null.

Limits

RFC 7159 specifies:

An implementation may set limits on the range and precision of numbers.

When the default type is used, the maximal integer number that can be stored is 9223372036854775807 (INT64_MAX) and the minimal integer number that can be stored is -9223372036854775808 (INT64_MIN). Integer numbers that are out of range will yield over/underflow when used in a constructor. During deserialization, too large or small integer numbers will be automatically be stored as number_unsigned_t or number_float_t.

RFC 7159 further states:

Note that when such software is used, numbers that are integers and are in the range $[-2^{53}+1, 2^{53}-1]$ are interoperable in the sense that implementations will agree exactly on their numeric values.

As this range is a subrange of the exactly supported range [INT64_MIN, INT64_MAX], this class's integer type is interoperable.

Storage

Integer number values are stored directly inside a basic_json type.

See Also
number_float_ttype for number values (floating-point)
number_unsigned_ttype for number values (unsigned integer)
Since
version 1.0.0

Definition at line 15223 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::number_unsigned_t = NumberUnsignedType

a type for a number (unsigned)

RFC 7159 describes numbers as follows:

The representation of numbers is similar to that used in most programming languages. A number is represented in base 10 using decimal digits. It contains an integer component that may be prefixed with an optional minus sign, which may be followed by a fraction part and/or an exponent part. Leading zeros are not allowed. (...) Numeric values that cannot be represented in the grammar below (such as Infinity and NaN) are not permitted.

This description includes both integer and floating-point numbers. However, C++ allows more precise storage if it is known whether the number is a signed integer, an unsigned integer or a floating-point number. Therefore, three different types, number_integer_t, number_unsigned_t and number_float_t are used.

To store unsigned integer numbers in C++, a type is defined by the template parameter NumberUnsignedType which chooses the type to use.

Default type

With the default values for NumberUnsignedType (uint64_t), the default value for number_unsigned_t is:

uint64_t

Default behavior

  • The restrictions about leading zeros is not enforced in C++. Instead, leading zeros in integer literals lead to an interpretation as octal number. Internally, the value will be stored as decimal number. For instance, the C++ integer literal 010 will be serialized to 8. During deserialization, leading zeros yield an error.
  • Not-a-number (NaN) values will be serialized to null.

Limits

RFC 7159 specifies:

An implementation may set limits on the range and precision of numbers.

When the default type is used, the maximal integer number that can be stored is 18446744073709551615 (UINT64_MAX) and the minimal integer number that can be stored is 0. Integer numbers that are out of range will yield over/underflow when used in a constructor. During deserialization, too large or small integer numbers will be automatically be stored as number_integer_t or number_float_t.

RFC 7159 further states:

Note that when such software is used, numbers that are integers and are in the range $[-2^{53}+1, 2^{53}-1]$ are interoperable in the sense that implementations will agree exactly on their numeric values.

As this range is a subrange (when considered in conjunction with the number_integer_t type) of the exactly supported range [0, UINT64_MAX], this class's integer type is interoperable.

Storage

Integer number values are stored directly inside a basic_json type.

See Also
number_float_ttype for number values (floating-point)
number_integer_ttype for number values (integer)
Since
version 2.0.0

Definition at line 15294 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::object_comparator_t = std::less<StringType>

Definition at line 14936 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::object_t = ObjectType<StringType, basic_json, object_comparator_t, AllocatorType<std::pair<const StringType, basic_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.

To store objects in C++, a type is defined by the template parameters described below.

Template Parameters
ObjectTypethe container to store objects (e.g., std::map or std::unordered_map)
StringTypethe type of the keys or names (e.g., std::string). The comparison function std::less<StringType> is used to order elements inside the container.
AllocatorTypethe allocator to use for objects (e.g., std::allocator)

Default type

With the default values for ObjectType (std::map), StringType (std::string), and AllocatorType (std::allocator), the default value for object_t is:

std::map<
std::string, // key_type
basic_json, // value_type
std::less<std::string>, // key_compare
std::allocator<std::pair<const std::string, basic_json>> // allocator_type
>

Behavior

The choice of object_t influences the behavior of the JSON class. With the default type, objects have the following behavior:

  • When all names are unique, objects will be interoperable in the sense that all software implementations receiving that object will agree on the name-value mappings.
  • When the names within an object are not unique, it is unspecified which one of the values for a given key will be chosen. For instance, {"key": 2, "key": 1} could be equal to either {"key": 1} or {"key": 2}.
  • Internally, name/value pairs are stored in lexicographical order of the names. Objects will also be serialized (see dump) in this order. For instance, {"b": 1, "a": 2} and {"a": 2, "b": 1} will be stored and serialized as {"a": 2, "b": 1}.
  • When comparing objects, the order of the name/value pairs is irrelevant. This makes objects interoperable in the sense that they will not be affected by these differences. For instance, {"b": 1, "a": 2} and {"a": 2, "b": 1} will be treated as equal.

Limits

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.

Storage

Objects are stored as pointers in a basic_json type. That is, for any access to object values, a pointer of type object_t* must be dereferenced.

See Also
array_ttype for an array value
Since
version 1.0.0
Note
The order name/value pairs are added to the object is not preserved by the library. Therefore, iterating an object may return name/value pairs in a different order than they were originally stored. In fact, keys will be traversed in alphabetical order as 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.

Definition at line 15026 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::other_error = detail::other_error

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.
See Also
- exception for the base class of the library exceptions
- parse_error for exceptions indicating a parse error
- invalid_iterator for exceptions indicating errors with iterators
- type_error for exceptions indicating executing a member function with a wrong type
- out_of_range for exceptions indicating access out of the defined range

{The following code shows how an other_error exception can be caught.,other_error}

Since
version 3.0.0

Definition at line 14784 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::out_of_range = detail::out_of_range

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 and BSON only support integer 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.
json.exception.out_of_range.409 BSON key cannot contain code point U+0000 (at byte 2) Key identifiers to be serialized to BSON cannot contain code point U+0000, since the key is stored as zero-terminated c-string

{The following code shows how an out_of_range exception can be caught.,out_of_range}

See Also
- exception for the base class of the library exceptions
- parse_error for exceptions indicating a parse error
- invalid_iterator for exceptions indicating errors with iterators
- type_error for exceptions indicating executing a member function with a wrong type
- other_error for exceptions indicating other library errors
Since
version 3.0.0

Definition at line 14782 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename CharType >
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::output_adapter_t = ::nlohmann::detail::output_adapter_t<CharType>
private

Definition at line 14743 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::parse_error = detail::parse_error

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.
json.exception.parse_error.114 parse error: Unsupported BSON record type 0x0F The parsing of the corresponding BSON record type is not implemented (yet).
Note
For an input with n bytes, 1 is the index of the first character and n+1 is the index of the terminating null byte or the end of file. This also holds true when reading a byte vector (CBOR or MessagePack).

{The following code shows how a parse_error exception can be caught.,parse_error}

See Also
- exception for the base class of the library exceptions
- invalid_iterator for exceptions indicating errors with iterators
- type_error for exceptions indicating executing a member function with a wrong type
- out_of_range for exceptions indicating access out of the defined range
- other_error for exceptions indicating other library errors
Since
version 3.0.0

Definition at line 14776 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::parse_event_t = typename parser::parse_event_t

parser event types

The parser callback distinguishes the following events:

  • object_start: the parser read { and started to process a JSON object
  • key: the parser read a key of a value in an object
  • object_end: the parser read } and finished processing a JSON object
  • array_start: the parser read [ and started to process a JSON array
  • array_end: the parser read ] and finished processing a JSON array
  • value: the parser finished reading a JSON value
callback_events.png
Example when certain parse events are triggered
See Also
parser_callback_t for more information and examples

Definition at line 15661 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::parser = ::nlohmann::detail::parser<basic_json>
private

Definition at line 14731 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::parser_callback_t = typename parser::parser_callback_t

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
callback_events.png
Example when certain parse events are triggered

Discarding a value (i.e., returning false) has different effects depending on the context in which function was called:

  • Discarded values in structured types are skipped. That is, the parser will behave as if the discarded value was never read.
  • In case a value outside a structured type is skipped, it is replaced with null. This case happens if the top-level element is skipped.
Parameters
[in]depththe depth of the recursion during parsing
[in]eventan event of type parse_event_t indicating the context in the callback function has been called
[in,out]parsedthe current intermediate parse result; note that writing to this value has no effect for parse_event_t::key events
Returns
Whether the JSON value which called the function during parsing should be kept (true) or not (false). In the latter case, it is either skipped completely or replaced by an empty discarded object.
See Also
parse for examples
Since
version 1.0.0

Definition at line 15712 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::pointer = typename std::allocator_traits<allocator_type>::pointer

the type of an element pointer

Definition at line 14815 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::primitive_iterator_t = ::nlohmann::detail::primitive_iterator_t
private

Definition at line 14733 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::reference = value_type&

the type of an element reference

Definition at line 14802 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::reverse_iterator = json_reverse_iterator<typename basic_json::iterator>

a reverse iterator for a basic_json container

Definition at line 14824 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::serializer = ::nlohmann::detail::serializer<basic_json>
private

Definition at line 14748 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::size_type = std::size_t

a type to represent container sizes

Definition at line 14809 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::string_t = StringType

a type for a string

RFC 7159 describes JSON strings as follows:

A string is a sequence of zero or more Unicode characters.

To store objects in C++, a type is defined by the template parameter described below. Unicode values are split by the JSON class into byte-sized characters during deserialization.

Template Parameters
StringTypethe container to store strings (e.g., std::string). Note this container is used for keys/names in objects, see object_t.

Default type

With the default values for StringType (std::string), the default value for string_t is:

std::string

Encoding

Strings are stored in UTF-8 encoding. Therefore, functions like std::string::size() or std::string::length() return the number of bytes in the string rather than the number of characters or glyphs.

String comparison

RFC 7159 states:

Software implementations are typically required to test names of object members for equality. Implementations that transform the textual representation into sequences of Unicode code units and then perform the comparison numerically, code unit by code unit, are interoperable in the sense that implementations will agree in all cases on equality or inequality of two strings. For example, implementations that compare strings with escaped characters unconverted may incorrectly find that "a\\\\b" and "a\\u005Cb" are not equal.

This implementation is interoperable as it does compare strings code unit by code unit.

Storage

String values are stored as pointers in a basic_json type. That is, for any access to string values, a pointer of type string_t* must be dereferenced.

Since
version 1.0.0

Definition at line 15125 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::type_error = detail::type_error

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 basic_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.
json.exception.type_error.317 JSON value cannot be serialized to requested format The dynamic type of the object cannot be represented in the requested serialization format (e.g. a raw true or null JSON object cannot be serialized to BSON)

{The following code shows how a type_error exception can be caught.,type_error}

See Also
- exception for the base class of the library exceptions
- parse_error for exceptions indicating a parse error
- invalid_iterator for exceptions indicating errors with iterators
- out_of_range for exceptions indicating access out of the defined range
- other_error for exceptions indicating other library errors
Since
version 3.0.0

Definition at line 14780 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::value_t = detail::value_t

Definition at line 14751 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
using nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::value_type = basic_json

the type of elements in a basic_json container

Definition at line 14799 of file json.hpp.

Constructor & Destructor Documentation

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::basic_json ( const value_t  v)
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 []
Parameters
[in]vthe type of the value to create

Constant.

Strong guarantee: if an exception is thrown, there are no changes to any JSON value.

{The following code shows the constructor for different value_t values,basic_json__value_t}

See Also
clear() – restores the postcondition of this constructor
Since
version 1.0.0

Definition at line 15752 of file json.hpp.

15753  : m_type(v), m_value(v)
15754  {
15755  assert_invariant();
15756  }
value_t m_type
the type of the current element
Definition: json.hpp:21017
void assert_invariant() const noexcept
checks the class invariants
Definition: json.hpp:15634
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::basic_json ( std::nullptr_t  = nullptr)
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.

Constant.

No-throw guarantee: this constructor never throws exceptions.

{The following code shows the constructor with and without a null pointer parameter.,basic_json__nullptr_t}

Since
version 1.0.0

Definition at line 15776 of file json.hpp.

15778  {
15779  assert_invariant();
15780  }
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
void assert_invariant() const noexcept
checks the class invariants
Definition: json.hpp:15634
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename CompatibleType , typename U = detail::uncvref_t<CompatibleType>, detail::enable_if_t< not detail::is_basic_json< U >::value and detail::is_compatible_type< basic_json_t, U >::value, int > = 0>
nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::basic_json ( CompatibleType &&  val)
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:

  • arrays: array_t and all kinds of compatible containers such as std::vector, std::deque, std::list, std::forward_list, std::array, std::valarray, std::set, std::unordered_set, std::multiset, and std::unordered_multiset with a value_type from which a basic_json value can be constructed.
  • objects: object_t and all kinds of compatible associative containers such as std::map, std::unordered_map, std::multimap, and std::unordered_multimap with a key_type compatible to string_t and a value_type from which a basic_json value can be constructed.
  • strings: string_t, string literals, and all compatible string containers can be used.
  • numbers: number_integer_t, number_unsigned_t, number_float_t, and all convertible number types such as int, size_t, int64_t, float or double can be used.
  • boolean: boolean_t / bool can be used.

See the examples below.

Template Parameters
CompatibleTypea type such that:
  • CompatibleType is not derived from std::istream,
  • CompatibleType is not basic_json (to avoid hijacking copy/move constructors),
  • CompatibleType is not a different basic_json type (i.e. with different template arguments)
  • CompatibleType is not a basic_json nested type (e.g., json_pointer, iterator, etc ...)
  • json_serializer<U> has a to_json(basic_json_t&, CompatibleType&&) method
U= uncvref_t<CompatibleType>
Parameters
[in]valthe value to be forwarded to the respective constructor

Usually linear in the size of the passed val, also depending on the implementation of the called to_json() method.

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.

{The following code shows the constructor with several compatible types.,basic_json__CompatibleType}

Since
version 2.1.0

Definition at line 15843 of file json.hpp.

15846  {
15847  JSONSerializer<U>::to_json(*this, std::forward<CompatibleType>(val));
15848  assert_invariant();
15849  }
void to_json(BasicJsonType &j, T b) noexcept
Definition: json.hpp:3647
void assert_invariant() const noexcept
checks the class invariants
Definition: json.hpp:15634
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename BasicJsonType , detail::enable_if_t< detail::is_basic_json< BasicJsonType >::value and not std::is_same< basic_json, BasicJsonType >::value, int > = 0>
nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::basic_json ( const BasicJsonType &  val)
inline

create a JSON value from an existing one

This is a constructor for existing basic_json types. It does not hijack copy/move constructors, since the parameter has different template arguments than the current ones.

The constructor tries to convert the internal m_value of the parameter.

Template Parameters
BasicJsonTypea type such that:
Parameters
[in]valthe basic_json value to be converted.

Usually linear in the size of the passed val, also depending on the implementation of the called to_json() method.

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.

Since
version 3.2.0

Definition at line 15880 of file json.hpp.

15881  {
15882  using other_boolean_t = typename BasicJsonType::boolean_t;
15883  using other_number_float_t = typename BasicJsonType::number_float_t;
15884  using other_number_integer_t = typename BasicJsonType::number_integer_t;
15885  using other_number_unsigned_t = typename BasicJsonType::number_unsigned_t;
15886  using other_string_t = typename BasicJsonType::string_t;
15887  using other_object_t = typename BasicJsonType::object_t;
15888  using other_array_t = typename BasicJsonType::array_t;
15889 
15890  switch (val.type())
15891  {
15892  case value_t::boolean:
15893  JSONSerializer<other_boolean_t>::to_json(*this, val.template get<other_boolean_t>());
15894  break;
15895  case value_t::number_float:
15896  JSONSerializer<other_number_float_t>::to_json(*this, val.template get<other_number_float_t>());
15897  break;
15899  JSONSerializer<other_number_integer_t>::to_json(*this, val.template get<other_number_integer_t>());
15900  break;
15902  JSONSerializer<other_number_unsigned_t>::to_json(*this, val.template get<other_number_unsigned_t>());
15903  break;
15904  case value_t::string:
15905  JSONSerializer<other_string_t>::to_json(*this, val.template get_ref<const other_string_t&>());
15906  break;
15907  case value_t::object:
15908  JSONSerializer<other_object_t>::to_json(*this, val.template get_ref<const other_object_t&>());
15909  break;
15910  case value_t::array:
15911  JSONSerializer<other_array_t>::to_json(*this, val.template get_ref<const other_array_t&>());
15912  break;
15913  case value_t::null:
15914  *this = nullptr;
15915  break;
15916  case value_t::discarded:
15918  break;
15919  default: // LCOV_EXCL_LINE
15920  assert(false); // LCOV_EXCL_LINE
15921  }
15922  assert_invariant();
15923  }
array (ordered collection of values)
number value (signed integer)
void to_json(BasicJsonType &j, T b) noexcept
Definition: json.hpp:3647
object (unordered set of name/value pairs)
value_t m_type
the type of the current element
Definition: json.hpp:21017
void assert_invariant() const noexcept
checks the class invariants
Definition: json.hpp:15634
number value (unsigned integer)
number value (floating-point)
discarded by the the parser callback function
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::basic_json ( initializer_list_t  init,
bool  type_deduction = true,
value_t  manual_type = value_t::array 
)
inline

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:

  1. If the list is empty, an empty JSON object value {} is created.
  2. If the list consists of pairs whose first element is a string, a JSON object value is created where the first elements of the pairs are treated as keys and the second elements are as values.
  3. In all other cases, an array is created.

The rules aim to create the best fit between a C++ initializer list and JSON values. The rationale is as follows:

  1. The empty initializer list is written as {} which is exactly an empty JSON object.
  2. C++ has no way of describing mapped types other than to list a list of pairs. As JSON requires that keys must be of type string, rule 2 is the weakest constraint one can pose on initializer lists to interpret them as an object.
  3. In all other cases, the initializer list could not be interpreted as JSON object type, so interpreting it as JSON array type is safe.

With the rules described above, the following JSON values cannot be expressed by an initializer list:

Note
When used without parentheses around an empty initializer list, basic_json() is called instead of this function, yielding the JSON null value.
Parameters
[in]initinitializer list with JSON values
[in]type_deductioninternal 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_typeinternal 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
Exceptions
type_error.301if 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.

Linear in the size of the initializer list init.

Strong guarantee: if an exception is thrown, there are no changes to any JSON value.

{The example below shows how JSON values are created from initializer lists.,basic_json__list_init_t}

See Also
array(initializer_list_t)create a JSON array value from an initializer list
object(initializer_list_t)create a JSON object value from an initializer list
Since
version 1.0.0

Definition at line 15999 of file json.hpp.

16002  {
16003  // check if each element is an array with two elements whose first
16004  // element is a string
16005  bool is_an_object = std::all_of(init.begin(), init.end(),
16006  [](const detail::json_ref<basic_json>& element_ref)
16007  {
16008  return element_ref->is_array() and element_ref->size() == 2 and (*element_ref)[0].is_string();
16009  });
16010 
16011  // adjust type if type deduction is not wanted
16012  if (not type_deduction)
16013  {
16014  // if array is wanted, do not create an object though possible
16015  if (manual_type == value_t::array)
16016  {
16017  is_an_object = false;
16018  }
16019 
16020  // if object is wanted but impossible, throw an exception
16021  if (JSON_HEDLEY_UNLIKELY(manual_type == value_t::object and not is_an_object))
16022  {
16023  JSON_THROW(type_error::create(301, "cannot create object from initializer list"));
16024  }
16025  }
16026 
16027  if (is_an_object)
16028  {
16029  // the initializer list is a list of pairs -> create object
16032 
16033  std::for_each(init.begin(), init.end(), [this](const detail::json_ref<basic_json>& element_ref)
16034  {
16035  auto element = element_ref.moved_or_copied();
16036  m_value.object->emplace(
16037  std::move(*((*element.m_value.array)[0].m_value.string)),
16038  std::move((*element.m_value.array)[1]));
16039  });
16040  }
16041  else
16042  {
16043  // the initializer list describes an array -> create array
16045  m_value.array = create<array_t>(init.begin(), init.end());
16046  }
16047 
16048  assert_invariant();
16049  }
object_t * object
object (stored with pointer to save storage)
Definition: json.hpp:15417
array_t * array
array (stored with pointer to save storage)
Definition: json.hpp:15419
array (ordered collection of values)
#define JSON_THROW(exception)
Definition: json.hpp:1754
object (unordered set of name/value pairs)
value_t m_type
the type of the current element
Definition: json.hpp:21017
return match has_match and(match.match_pdg==11 or match.match_pdg==-11)
void assert_invariant() const noexcept
checks the class invariants
Definition: json.hpp:15634
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: json.hpp:1124
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::basic_json ( size_type  cnt,
const basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > &  val 
)
inline

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.

Parameters
[in]cntthe number of JSON copies of val to create
[in]valthe JSON value to copy
Postcondition
std::distance(begin(),end()) == cnt holds.

Linear in cnt.

Strong guarantee: if an exception is thrown, there are no changes to any JSON value.

{The following code shows examples for the basic_json(size_type\, const basic_json&) constructor.,basic_json__size_type_basic_json}

Since
version 1.0.0

Definition at line 16160 of file json.hpp.

16162  {
16163  m_value.array = create<array_t>(cnt, val);
16164  assert_invariant();
16165  }
array_t * array
array (stored with pointer to save storage)
Definition: json.hpp:15419
array (ordered collection of values)
value_t m_type
the type of the current element
Definition: json.hpp:21017
void assert_invariant() const noexcept
checks the class invariants
Definition: json.hpp:15634
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<class InputIT , typename std::enable_if< std::is_same< InputIT, typename basic_json_t::iterator >::value orstd::is_same< InputIT, typename basic_json_t::const_iterator >::value, int >::type = 0>
nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::basic_json ( InputIT  first,
InputIT  last 
)
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:

  • In case of a null type, invalid_iterator.206 is thrown.
  • In case of other primitive types (number, boolean, or string), first must be begin() and last must be end(). In this case, the value is copied. Otherwise, invalid_iterator.204 is thrown.
  • In case of structured types (array, object), the constructor behaves as similar versions for std::vector or std::map; that is, a JSON array or object is constructed from the values in the range.
Template Parameters
InputITan input iterator type (iterator or const_iterator)
Parameters
[in]firstbegin of the range to copy from (included)
[in]lastend of the range to copy from (excluded)
Precondition
Iterators first and last must be initialized. This precondition is enforced with an assertion (see warning). If assertions are switched off, a violation of this precondition yields undefined behavior.
Range [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.
Warning
A precondition is enforced with a runtime assertion that will result in calling std::abort if this precondition is not met. Assertions can be disabled by defining NDEBUG at compile time. See https://en.cppreference.com/w/cpp/error/assert for more information.
Exceptions
invalid_iterator.201if 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.204if 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.206if iterators first and last belong to a null value. In this case, the range [first, last) is undefined.

Linear in distance between first and last.

Strong guarantee: if an exception is thrown, there are no changes to any JSON value.

{The example below shows several ways to create JSON values by specifying a subrange with iterators.,basic_json__InputIt_InputIt}

Since
version 1.0.0

Definition at line 16225 of file json.hpp.

16226  {
16227  assert(first.m_object != nullptr);
16228  assert(last.m_object != nullptr);
16229 
16230  // make sure iterator fits the current value
16231  if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
16232  {
16233  JSON_THROW(invalid_iterator::create(201, "iterators are not compatible"));
16234  }
16235 
16236  // copy type from first iterator
16237  m_type = first.m_object->m_type;
16238 
16239  // check if iterator range is complete for primitive values
16240  switch (m_type)
16241  {
16242  case value_t::boolean:
16243  case value_t::number_float:
16246  case value_t::string:
16247  {
16248  if (JSON_HEDLEY_UNLIKELY(not first.m_it.primitive_iterator.is_begin()
16249  or not last.m_it.primitive_iterator.is_end()))
16250  {
16251  JSON_THROW(invalid_iterator::create(204, "iterators out of range"));
16252  }
16253  break;
16254  }
16255 
16256  default:
16257  break;
16258  }
16259 
16260  switch (m_type)
16261  {
16263  {
16264  m_value.number_integer = first.m_object->m_value.number_integer;
16265  break;
16266  }
16267 
16269  {
16270  m_value.number_unsigned = first.m_object->m_value.number_unsigned;
16271  break;
16272  }
16273 
16274  case value_t::number_float:
16275  {
16276  m_value.number_float = first.m_object->m_value.number_float;
16277  break;
16278  }
16279 
16280  case value_t::boolean:
16281  {
16282  m_value.boolean = first.m_object->m_value.boolean;
16283  break;
16284  }
16285 
16286  case value_t::string:
16287  {
16288  m_value = *first.m_object->m_value.string;
16289  break;
16290  }
16291 
16292  case value_t::object:
16293  {
16294  m_value.object = create<object_t>(first.m_it.object_iterator,
16295  last.m_it.object_iterator);
16296  break;
16297  }
16298 
16299  case value_t::array:
16300  {
16301  m_value.array = create<array_t>(first.m_it.array_iterator,
16302  last.m_it.array_iterator);
16303  break;
16304  }
16305 
16306  default:
16307  JSON_THROW(invalid_iterator::create(206, "cannot construct with iterators from " +
16308  std::string(first.m_object->type_name())));
16309  }
16310 
16311  assert_invariant();
16312  }
object_t * object
object (stored with pointer to save storage)
Definition: json.hpp:15417
array_t * array
array (stored with pointer to save storage)
Definition: json.hpp:15419
array (ordered collection of values)
boolean_t boolean
boolean
Definition: json.hpp:15423
number value (signed integer)
number_unsigned_t number_unsigned
number (unsigned integer)
Definition: json.hpp:15427
number_float_t number_float
number (floating-point)
Definition: json.hpp:15429
#define JSON_THROW(exception)
Definition: json.hpp:1754
object (unordered set of name/value pairs)
value_t m_type
the type of the current element
Definition: json.hpp:21017
void assert_invariant() const noexcept
checks the class invariants
Definition: json.hpp:15634
static invalid_iterator create(int id_, const std::string &what_arg)
Definition: json.hpp:2024
number_integer_t number_integer
number (integer)
Definition: json.hpp:15425
number value (unsigned integer)
number value (floating-point)
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: json.hpp:1124
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::basic_json ( const detail::json_ref< basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > > &  ref)
inlineprivate

Definition at line 16320 of file json.hpp.

16321  : basic_json(ref.moved_or_copied())
16322  {}
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::basic_json ( const basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > &  other)
inline

copy constructor

Creates a copy of a given JSON value.

Parameters
[in]otherthe JSON value to copy
Postcondition
*this == other

Linear in the size of other.

Strong guarantee: if an exception is thrown, there are no changes to any JSON value.

This function helps basic_json satisfying the Container requirements:

  • The complexity is linear.
  • As postcondition, it holds: other == basic_json(other).

{The following code shows an example for the copy constructor.,basic_json__basic_json}

Since
version 1.0.0

Definition at line 16349 of file json.hpp.

16350  : m_type(other.m_type)
16351  {
16352  // check of passed value is valid
16353  other.assert_invariant();
16354 
16355  switch (m_type)
16356  {
16357  case value_t::object:
16358  {
16359  m_value = *other.m_value.object;
16360  break;
16361  }
16362 
16363  case value_t::array:
16364  {
16365  m_value = *other.m_value.array;
16366  break;
16367  }
16368 
16369  case value_t::string:
16370  {
16371  m_value = *other.m_value.string;
16372  break;
16373  }
16374 
16375  case value_t::boolean:
16376  {
16377  m_value = other.m_value.boolean;
16378  break;
16379  }
16380 
16382  {
16383  m_value = other.m_value.number_integer;
16384  break;
16385  }
16386 
16388  {
16389  m_value = other.m_value.number_unsigned;
16390  break;
16391  }
16392 
16393  case value_t::number_float:
16394  {
16395  m_value = other.m_value.number_float;
16396  break;
16397  }
16398 
16399  default:
16400  break;
16401  }
16402 
16403  assert_invariant();
16404  }
object_t * object
object (stored with pointer to save storage)
Definition: json.hpp:15417
array_t * array
array (stored with pointer to save storage)
Definition: json.hpp:15419
string_t * string
string (stored with pointer to save storage)
Definition: json.hpp:15421
array (ordered collection of values)
boolean_t boolean
boolean
Definition: json.hpp:15423
number value (signed integer)
number_unsigned_t number_unsigned
number (unsigned integer)
Definition: json.hpp:15427
number_float_t number_float
number (floating-point)
Definition: json.hpp:15429
object (unordered set of name/value pairs)
value_t m_type
the type of the current element
Definition: json.hpp:21017
void assert_invariant() const noexcept
checks the class invariants
Definition: json.hpp:15634
number_integer_t number_integer
number (integer)
Definition: json.hpp:15425
number value (unsigned integer)
number value (floating-point)
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::basic_json ( basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > &&  other)
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.

Parameters
[in,out]othervalue to move to this object
Postcondition
*this has the same value as other before the call.
other is a JSON null value.

Constant.

No-throw guarantee: this constructor never throws exceptions.

This function helps basic_json satisfying the MoveConstructible requirements.

{The code below shows the move constructor explicitly called via std::move.,basic_json__moveconstructor}

Since
version 1.0.0

Definition at line 16432 of file json.hpp.

16433  : m_type(std::move(other.m_type)),
16434  m_value(std::move(other.m_value))
16435  {
16436  // check that passed value is valid
16437  other.assert_invariant();
16438 
16439  // invalidate payload
16440  other.m_type = value_t::null;
16441  other.m_value = {};
16442 
16443  assert_invariant();
16444  }
value_t m_type
the type of the current element
Definition: json.hpp:21017
void assert_invariant() const noexcept
checks the class invariants
Definition: json.hpp:15634
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::~basic_json ( )
inlinenoexcept

destructor

Destroys the JSON value and frees all allocated memory.

Linear.

This function helps basic_json satisfying the Container requirements:

  • The complexity is linear.
  • All stored elements are destroyed and all memory is freed.
Since
version 1.0.0

Definition at line 16502 of file json.hpp.

16503  {
16504  assert_invariant();
16506  }
void destroy(value_t t) noexcept
Definition: json.hpp:15542
value_t m_type
the type of the current element
Definition: json.hpp:21017
void assert_invariant() const noexcept
checks the class invariants
Definition: json.hpp:15634
json_value m_value
the value of the current element
Definition: json.hpp:21020

Member Function Documentation

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
static bool nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::accept ( detail::input_adapter &&  i)
inlinestatic

Definition at line 20754 of file json.hpp.

20755  {
20756  return parser(i).accept(true);
20757  }
::nlohmann::detail::parser< basic_json > parser
Definition: json.hpp:14731
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<class IteratorType , typename std::enable_if< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< IteratorType >::iterator_category >::value, int >::type = 0>
static bool nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::accept ( IteratorType  first,
IteratorType  last 
)
inlinestatic

Definition at line 20890 of file json.hpp.

20891  {
20892  return parser(detail::input_adapter(first, last)).accept(true);
20893  }
::nlohmann::detail::parser< basic_json > parser
Definition: json.hpp:14731
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::array ( initializer_list_t  init = {})
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.

Note
This function is only needed to express two edge cases that cannot be realized with the initializer list constructor (basic_json(initializer_list_t, bool, value_t)). These cases are:
  1. creating an array whose elements are all pairs whose first element is a string – in this case, the initializer list constructor would create an object, taking the first elements as keys
  2. creating an empty array – passing the empty initializer list to the initializer list constructor yields an empty object
Parameters
[in]initinitializer list with JSON values to create an array from (optional)
Returns
JSON array value

Linear in the size of init.

Strong guarantee: if an exception is thrown, there are no changes to any JSON value.

{The following code shows an example for the array function.,array}

See Also
basic_json(initializer_list_t, bool, value_t)create a JSON value from an initializer list
object(initializer_list_t)create a JSON object value from an initializer list
Since
version 1.0.0

Definition at line 16089 of file json.hpp.

16089  {})
16090  {
16091  return basic_json(init, false, value_t::array);
16092  }
array (ordered collection of values)
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
void nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::assert_invariant ( ) const
inlineprivatenoexcept

checks the class invariants

This function asserts the class invariants. It needs to be called at the end of every constructor to make sure that created objects respect the invariant. Furthermore, it has to be called each time the type of a JSON value is changed, because the invariant expresses a relationship between m_type and m_value.

Definition at line 15634 of file json.hpp.

15635  {
15636  assert(m_type != value_t::object or m_value.object != nullptr);
15637  assert(m_type != value_t::array or m_value.array != nullptr);
15638  assert(m_type != value_t::string or m_value.string != nullptr);
15639  }
object_t * object
object (stored with pointer to save storage)
Definition: json.hpp:15417
array_t * array
array (stored with pointer to save storage)
Definition: json.hpp:15419
string_t * string
string (stored with pointer to save storage)
Definition: json.hpp:15421
array (ordered collection of values)
object (unordered set of name/value pairs)
value_t m_type
the type of the current element
Definition: json.hpp:21017
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
reference nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::at ( size_type  idx)
inline

access specified array element with bounds checking

Returns a reference to the element at specified location idx, with bounds checking.

Parameters
[in]idxindex of the element to access
Returns
reference to the element at index idx
Exceptions
type_error.304if the JSON value is not an array; in this case, calling at with an index makes no sense. See example below.
out_of_range.401if the index idx is out of range of the array; that is, idx >= size(). See example below.

Strong guarantee: if an exception is thrown, there are no changes in the JSON value.

Constant.

Since
version 1.0.0

{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}

Definition at line 17519 of file json.hpp.

17520  {
17521  // at only works for arrays
17523  {
17524  JSON_TRY
17525  {
17526  return m_value.array->at(idx);
17527  }
17528  JSON_CATCH (std::out_of_range&)
17529  {
17530  // create better exception explanation
17531  JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range"));
17532  }
17533  }
17534  else
17535  {
17536  JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name())));
17537  }
17538  }
#define JSON_CATCH(exception)
Definition: json.hpp:1756
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
array_t * array
array (stored with pointer to save storage)
Definition: json.hpp:15419
#define JSON_THROW(exception)
Definition: json.hpp:1754
#define JSON_TRY
Definition: json.hpp:1755
#define JSON_HEDLEY_LIKELY(expr)
Definition: json.hpp:1123
std::string to_string(WindowPattern const &pattern)
constexpr bool is_array() const noexcept
return whether value is an array
Definition: json.hpp:16872
static out_of_range create(int id_, const std::string &what_arg)
Definition: json.hpp:2125
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
const_reference nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::at ( size_type  idx) const
inline

access specified array element with bounds checking

Returns a const reference to the element at specified location idx, with bounds checking.

Parameters
[in]idxindex of the element to access
Returns
const reference to the element at index idx
Exceptions
type_error.304if the JSON value is not an array; in this case, calling at with an index makes no sense. See example below.
out_of_range.401if the index idx is out of range of the array; that is, idx >= size(). See example below.

Strong guarantee: if an exception is thrown, there are no changes in the JSON value.

Constant.

Since
version 1.0.0

{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}

Definition at line 17566 of file json.hpp.

17567  {
17568  // at only works for arrays
17570  {
17571  JSON_TRY
17572  {
17573  return m_value.array->at(idx);
17574  }
17575  JSON_CATCH (std::out_of_range&)
17576  {
17577  // create better exception explanation
17578  JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range"));
17579  }
17580  }
17581  else
17582  {
17583  JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name())));
17584  }
17585  }
#define JSON_CATCH(exception)
Definition: json.hpp:1756
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
array_t * array
array (stored with pointer to save storage)
Definition: json.hpp:15419
#define JSON_THROW(exception)
Definition: json.hpp:1754
#define JSON_TRY
Definition: json.hpp:1755
#define JSON_HEDLEY_LIKELY(expr)
Definition: json.hpp:1123
std::string to_string(WindowPattern const &pattern)
constexpr bool is_array() const noexcept
return whether value is an array
Definition: json.hpp:16872
static out_of_range create(int id_, const std::string &what_arg)
Definition: json.hpp:2125
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
reference nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::at ( const typename object_t::key_type &  key)
inline

access specified object element with bounds checking

Returns a reference to the element at with specified key key, with bounds checking.

Parameters
[in]keykey of the element to access
Returns
reference to the element at key key
Exceptions
type_error.304if the JSON value is not an object; in this case, calling at with a key makes no sense. See example below.
out_of_range.403if the key key is is not stored in the object; that is, find(key) == end(). See example below.

Strong guarantee: if an exception is thrown, there are no changes in the JSON value.

Logarithmic in the size of the container.

See Also
operator[](const typename object_t::key_type&) for unchecked access by reference
value() for access by value with a default value
Since
version 1.0.0

{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}

Definition at line 17617 of file json.hpp.

17618  {
17619  // at only works for objects
17621  {
17622  JSON_TRY
17623  {
17624  return m_value.object->at(key);
17625  }
17626  JSON_CATCH (std::out_of_range&)
17627  {
17628  // create better exception explanation
17629  JSON_THROW(out_of_range::create(403, "key '" + key + "' not found"));
17630  }
17631  }
17632  else
17633  {
17634  JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name())));
17635  }
17636  }
object_t * object
object (stored with pointer to save storage)
Definition: json.hpp:15417
#define JSON_CATCH(exception)
Definition: json.hpp:1756
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
constexpr bool is_object() const noexcept
return whether value is an object
Definition: json.hpp:16850
#define JSON_THROW(exception)
Definition: json.hpp:1754
#define JSON_TRY
Definition: json.hpp:1755
#define JSON_HEDLEY_LIKELY(expr)
Definition: json.hpp:1123
static out_of_range create(int id_, const std::string &what_arg)
Definition: json.hpp:2125
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
const_reference nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::at ( const typename object_t::key_type &  key) const
inline

access specified object element with bounds checking

Returns a const reference to the element at with specified key key, with bounds checking.

Parameters
[in]keykey of the element to access
Returns
const reference to the element at key key
Exceptions
type_error.304if the JSON value is not an object; in this case, calling at with a key makes no sense. See example below.
out_of_range.403if the key key is is not stored in the object; that is, find(key) == end(). See example below.

Strong guarantee: if an exception is thrown, there are no changes in the JSON value.

Logarithmic in the size of the container.

See Also
operator[](const typename object_t::key_type&) for unchecked access by reference
value() for access by value with a default value
Since
version 1.0.0

{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}

Definition at line 17668 of file json.hpp.

17669  {
17670  // at only works for objects
17672  {
17673  JSON_TRY
17674  {
17675  return m_value.object->at(key);
17676  }
17677  JSON_CATCH (std::out_of_range&)
17678  {
17679  // create better exception explanation
17680  JSON_THROW(out_of_range::create(403, "key '" + key + "' not found"));
17681  }
17682  }
17683  else
17684  {
17685  JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name())));
17686  }
17687  }
object_t * object
object (stored with pointer to save storage)
Definition: json.hpp:15417
#define JSON_CATCH(exception)
Definition: json.hpp:1756
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
constexpr bool is_object() const noexcept
return whether value is an object
Definition: json.hpp:16850
#define JSON_THROW(exception)
Definition: json.hpp:1754
#define JSON_TRY
Definition: json.hpp:1755
#define JSON_HEDLEY_LIKELY(expr)
Definition: json.hpp:1123
static out_of_range create(int id_, const std::string &what_arg)
Definition: json.hpp:2125
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
reference nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::at ( const json_pointer ptr)
inline

access specified element via JSON Pointer

Returns a reference to the element at with specified JSON pointer ptr, with bounds checking.

Parameters
[in]ptrJSON pointer to the desired element
Returns
reference to the element pointed to by ptr
Exceptions
parse_error.106if an array index in the passed JSON pointer ptr begins with '0'. See example below.
parse_error.109if an array index in the passed JSON pointer ptr is not a number. See example below.
out_of_range.401if an array index in the passed JSON pointer ptr is out of range. See example below.
out_of_range.402if 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.403if the JSON pointer describes a key of an object which cannot be found. See example below.
out_of_range.404if the JSON pointer ptr can not be resolved. See example below.

Strong guarantee: if an exception is thrown, there are no changes in the JSON value.

Constant.

Since
version 2.0.0

{The behavior is shown in the example.,at_json_pointer}

Definition at line 21942 of file json.hpp.

21943  {
21944  return ptr.get_checked(this);
21945  }
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
const_reference nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::at ( const json_pointer ptr) const
inline

access specified element via JSON Pointer

Returns a const reference to the element at with specified JSON pointer ptr, with bounds checking.

Parameters
[in]ptrJSON pointer to the desired element
Returns
reference to the element pointed to by ptr
Exceptions
parse_error.106if an array index in the passed JSON pointer ptr begins with '0'. See example below.
parse_error.109if an array index in the passed JSON pointer ptr is not a number. See example below.
out_of_range.401if an array index in the passed JSON pointer ptr is out of range. See example below.
out_of_range.402if 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.403if the JSON pointer describes a key of an object which cannot be found. See example below.
out_of_range.404if the JSON pointer ptr can not be resolved. See example below.

Strong guarantee: if an exception is thrown, there are no changes in the JSON value.

Constant.

Since
version 2.0.0

{The behavior is shown in the example.,at_json_pointer_const}

Definition at line 21985 of file json.hpp.

21986  {
21987  return ptr.get_checked(this);
21988  }
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
reference nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::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

auto tmp = c.end();
--tmp;
return *tmp;
Returns
In case of a structured type (array or object), a reference to the last element is returned. In case of number, string, or boolean values, a reference to the value is returned.

Constant.

Precondition
The JSON value must not be null (would throw std::out_of_range) or an empty array or object (undefined behavior, guarded by assertions).
Postcondition
The JSON value remains unchanged.
Exceptions
invalid_iterator.214when called on a null value. See example below.

{The following code shows an example for back().,back}

See Also
front() – access the first element
Since
version 1.0.0

Definition at line 18173 of file json.hpp.

18174  {
18175  auto tmp = end();
18176  --tmp;
18177  return *tmp;
18178  }
iterator end() noexcept
returns an iterator to one past the last element
Definition: json.hpp:18745
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
const_reference nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::back ( ) 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

auto tmp = c.end();
--tmp;
return *tmp;
Returns
In case of a structured type (array or object), a reference to the last element is returned. In case of number, string, or boolean values, a reference to the value is returned.

Constant.

Precondition
The JSON value must not be null (would throw std::out_of_range) or an empty array or object (undefined behavior, guarded by assertions).
Postcondition
The JSON value remains unchanged.
Exceptions
invalid_iterator.214when called on a null value. See example below.

{The following code shows an example for back().,back}

See Also
front() – access the first element
Since
version 1.0.0

Definition at line 18183 of file json.hpp.

18184  {
18185  auto tmp = cend();
18186  --tmp;
18187  return *tmp;
18188  }
const_iterator cend() const noexcept
returns a const iterator to one past the last element
Definition: json.hpp:18785
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
iterator nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::begin ( )
inlinenoexcept

returns an iterator to the first element

Returns an iterator to the first element.

range-begin-end.svg
Illustration from cppreference.com
Returns
iterator to the first element

Constant.

This function helps basic_json satisfying the Container requirements:

  • The complexity is constant.

{The following code shows an example for begin().,begin}

See Also
cbegin() – returns a const iterator to the beginning
end() – returns an iterator to the end
cend() – returns a const iterator to the end
Since
version 1.0.0

Definition at line 18674 of file json.hpp.

18675  {
18676  iterator result(this);
18677  result.set_begin();
18678  return result;
18679  }
iter_impl< basic_json > iterator
an iterator for a basic_json container
Definition: json.hpp:14820
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
const_iterator nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::begin ( ) const
inlinenoexcept

returns a const iterator to the first element

Returns a const iterator to the first element.

range-begin-end.svg
Illustration from cppreference.com
Returns
const iterator to the first element

Constant.

This function helps basic_json satisfying the Container requirements:

  • The complexity is constant.
  • Has the semantics of const_cast<const basic_json&>(*this).begin().

{The following code shows an example for cbegin().,cbegin}

See Also
begin() – returns an iterator to the beginning
end() – returns an iterator to the end
cend() – returns a const iterator to the end
Since
version 1.0.0

Definition at line 18684 of file json.hpp.

18685  {
18686  return cbegin();
18687  }
const_iterator cbegin() const noexcept
returns a const iterator to the first element
Definition: json.hpp:18714
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
const_iterator nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::cbegin ( ) const
inlinenoexcept

returns a const iterator to the first element

Returns a const iterator to the first element.

range-begin-end.svg
Illustration from cppreference.com
Returns
const iterator to the first element

Constant.

This function helps basic_json satisfying the Container requirements:

  • The complexity is constant.
  • Has the semantics of const_cast<const basic_json&>(*this).begin().

{The following code shows an example for cbegin().,cbegin}

See Also
begin() – returns an iterator to the beginning
end() – returns an iterator to the end
cend() – returns a const iterator to the end
Since
version 1.0.0

Definition at line 18714 of file json.hpp.

18715  {
18716  const_iterator result(this);
18717  result.set_begin();
18718  return result;
18719  }
iter_impl< const basic_json > const_iterator
a const iterator for a basic_json container
Definition: json.hpp:14822
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
const_iterator nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::cend ( ) const
inlinenoexcept

returns a const iterator to one past the last element

Returns a const iterator to one past the last element.

range-begin-end.svg
Illustration from cppreference.com
Returns
const iterator one past the last element

Constant.

This function helps basic_json satisfying the Container requirements:

  • The complexity is constant.
  • Has the semantics of const_cast<const basic_json&>(*this).end().

{The following code shows an example for cend().,cend}

See Also
end() – returns an iterator to the end
begin() – returns an iterator to the beginning
cbegin() – returns a const iterator to the beginning
Since
version 1.0.0

Definition at line 18785 of file json.hpp.

18786  {
18787  const_iterator result(this);
18788  result.set_end();
18789  return result;
18790  }
iter_impl< const basic_json > const_iterator
a const iterator for a basic_json container
Definition: json.hpp:14822
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
void nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::clear ( )
inlinenoexcept

clears the contents

Clears the content of a JSON value and resets it to the default value as if basic_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 []
Postcondition
Has the same effect as calling
this = basic_json(type());

{The example below shows the effect of clear() to different JSON types.,clear}

Linear in the size of the JSON value.

All iterators, pointers and references related to this container are invalidated.

No-throw guarantee: this function never throws exceptions.

See Also
basic_json(value_t) – constructor that creates an object with the same value than calling clear()
Since
version 1.0.0

Definition at line 19335 of file json.hpp.

19336  {
19337  switch (m_type)
19338  {
19340  {
19341  m_value.number_integer = 0;
19342  break;
19343  }
19344 
19346  {
19348  break;
19349  }
19350 
19351  case value_t::number_float:
19352  {
19353  m_value.number_float = 0.0;
19354  break;
19355  }
19356 
19357  case value_t::boolean:
19358  {
19359  m_value.boolean = false;
19360  break;
19361  }
19362 
19363  case value_t::string:
19364  {
19365  m_value.string->clear();
19366  break;
19367  }
19368 
19369  case value_t::array:
19370  {
19371  m_value.array->clear();
19372  break;
19373  }
19374 
19375  case value_t::object:
19376  {
19377  m_value.object->clear();
19378  break;
19379  }
19380 
19381  default:
19382  break;
19383  }
19384  }
object_t * object
object (stored with pointer to save storage)
Definition: json.hpp:15417
array_t * array
array (stored with pointer to save storage)
Definition: json.hpp:15419
string_t * string
string (stored with pointer to save storage)
Definition: json.hpp:15421
array (ordered collection of values)
boolean_t boolean
boolean
Definition: json.hpp:15423
number value (signed integer)
number_unsigned_t number_unsigned
number (unsigned integer)
Definition: json.hpp:15427
number_float_t number_float
number (floating-point)
Definition: json.hpp:15429
object (unordered set of name/value pairs)
value_t m_type
the type of the current element
Definition: json.hpp:21017
number_integer_t number_integer
number (integer)
Definition: json.hpp:15425
number value (unsigned integer)
number value (floating-point)
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename KeyT , typename std::enable_if< not std::is_same< typename std::decay< KeyT >::type, json_pointer >::value, int >::type = 0>
bool nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::contains ( KeyT &&  key) const
inline

check the existence of an element in a JSON object

Check whether an element exists in a JSON object with key equivalent to key. If the element is not found or the JSON value is not an object, false is returned.

Note
This method always returns false when executed on a JSON type that is not an object.
Parameters
[in]keykey value to check its existence.
Returns
true if an element with specified key exists. If no such element with such key is found or the JSON value is not an object, false is returned.

Logarithmic in the size of the JSON object.

{The following code shows an example for contains().,contains}

See Also
find(KeyT&&) – returns an iterator to an object element
contains(const json_pointer&) const – checks the existence for a JSON pointer
Since
version 3.6.0

Definition at line 18604 of file json.hpp.

18605  {
18606  return is_object() and m_value.object->find(std::forward<KeyT>(key)) != m_value.object->end();
18607  }
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
Definition: UtilFunc.cxx:42
iterator end() noexcept
returns an iterator to one past the last element
Definition: json.hpp:18745
constexpr bool is_object() const noexcept
return whether value is an object
Definition: json.hpp:16850
iterator find(KeyT &&key)
find an element in a JSON object
Definition: json.hpp:18520
return match has_match and(match.match_pdg==11 or match.match_pdg==-11)
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
bool nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::contains ( const json_pointer ptr) const
inline

check the existence of an element in a JSON object given a JSON pointer

Check whether the given JSON pointer ptr can be resolved in the current JSON value.

Note
This method can be executed on any JSON value type.
Parameters
[in]ptrJSON pointer to check its existence.
Returns
true if the JSON pointer can be resolved to a stored value, false otherwise.
Postcondition
If j.contains(ptr) returns true, it is safe to call j[ptr].
Exceptions
parse_error.106if an array index begins with '0'
parse_error.109if an array index was not a number

Logarithmic in the size of the JSON object.

{The following code shows an example for contains().,contains_json_pointer}

See Also
contains(KeyT &&) const – checks the existence of a key
Since
version 3.7.0

Definition at line 18635 of file json.hpp.

18636  {
18637  return ptr.contains(this);
18638  }
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename KeyT >
size_type nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::count ( KeyT &&  key) const
inline

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).

Note
This method always returns 0 when executed on a JSON type that is not an object.
Parameters
[in]keykey value of the element to count
Returns
Number of elements with key key. If the JSON value is not an object, the return value will be 0.

Logarithmic in the size of the JSON object.

{The example shows how count() is used.,count}

Since
version 1.0.0

Definition at line 18571 of file json.hpp.

18572  {
18573  // return 0 for all nonobject types
18574  return is_object() ? m_value.object->count(std::forward<KeyT>(key)) : 0;
18575  }
object_t * object
object (stored with pointer to save storage)
Definition: json.hpp:15417
constexpr bool is_object() const noexcept
return whether value is an object
Definition: json.hpp:16850
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
const_reverse_iterator nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::crbegin ( ) const
inlinenoexcept

returns a const reverse iterator to the last element

Returns a const iterator to the reverse-beginning; that is, the last element.

range-rbegin-rend.svg
Illustration from cppreference.com

Constant.

This function helps basic_json satisfying the ReversibleContainer requirements:

  • The complexity is constant.
  • Has the semantics of const_cast<const basic_json&>(*this).rbegin().

{The following code shows an example for crbegin().,crbegin}

See Also
rbegin() – returns a reverse iterator to the beginning
rend() – returns a reverse iterator to the end
crend() – returns a const reverse iterator to the end
Since
version 1.0.0

Definition at line 18889 of file json.hpp.

18890  {
18891  return const_reverse_iterator(cend());
18892  }
json_reverse_iterator< typename basic_json::const_iterator > const_reverse_iterator
a const reverse iterator for a basic_json container
Definition: json.hpp:14826
const_iterator cend() const noexcept
returns a const iterator to one past the last element
Definition: json.hpp:18785
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename T , typename... Args>
static JSON_HEDLEY_RETURNS_NON_NULL T* nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::create ( Args &&...  args)
inlinestaticprivate

helper for exception-safe object creation

Definition at line 15371 of file json.hpp.

15372  {
15373  AllocatorType<T> alloc;
15374  using AllocatorTraits = std::allocator_traits<AllocatorType<T>>;
15375 
15376  auto deleter = [&](T * object)
15377  {
15378  AllocatorTraits::deallocate(alloc, object, 1);
15379  };
15380  std::unique_ptr<T, decltype(deleter)> object(AllocatorTraits::allocate(alloc, 1), deleter);
15381  AllocatorTraits::construct(alloc, object.get(), std::forward<Args>(args)...);
15382  assert(object != nullptr);
15383  return object.release();
15384  }
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json object(initializer_list_t init={})
explicitly create an object from an initializer list
Definition: json.hpp:16133
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
const_reverse_iterator nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::crend ( ) const
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.

range-rbegin-rend.svg
Illustration from cppreference.com

Constant.

This function helps basic_json satisfying the ReversibleContainer requirements:

  • The complexity is constant.
  • Has the semantics of const_cast<const basic_json&>(*this).rend().

{The following code shows an example for crend().,crend}

See Also
rend() – returns a reverse iterator to the end
rbegin() – returns a reverse iterator to the beginning
crbegin() – returns a const reverse iterator to the beginning
Since
version 1.0.0

Definition at line 18918 of file json.hpp.

18919  {
18920  return const_reverse_iterator(cbegin());
18921  }
json_reverse_iterator< typename basic_json::const_iterator > const_reverse_iterator
a const reverse iterator for a basic_json container
Definition: json.hpp:14826
const_iterator cbegin() const noexcept
returns a const iterator to the first element
Definition: json.hpp:18714
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::diff ( const basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > &  source,
const basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > &  target,
const std::string &  path = "" 
)
inlinestatic

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.

Invariant
For two JSON values source and target, the following code yields always true:
source.patch(diff(source, target)) == target;
Note
Currently, only remove, add, and replace operations are generated.
Parameters
[in]sourceJSON value to compare from
[in]targetJSON value to compare against
[in]pathhelper value to create JSON pointers
Returns
a JSON patch to convert the source to target

Linear in the lengths of source and target.

{The following code shows how a JSON patch is created as a diff for two JSON values.,diff}

See Also
patch – apply a JSON patch
merge_patch – apply a JSON Merge Patch
RFC 6902 (JSON Patch)
Since
version 2.0.0

Definition at line 22406 of file json.hpp.

22408  {
22409  // the patch
22410  basic_json result(value_t::array);
22411 
22412  // if the values are the same, return empty patch
22413  if (source == target)
22414  {
22415  return result;
22416  }
22417 
22418  if (source.type() != target.type())
22419  {
22420  // different types: replace value
22421  result.push_back(
22422  {
22423  {"op", "replace"}, {"path", path}, {"value", target}
22424  });
22425  return result;
22426  }
22427 
22428  switch (source.type())
22429  {
22430  case value_t::array:
22431  {
22432  // first pass: traverse common elements
22433  std::size_t i = 0;
22434  while (i < source.size() and i < target.size())
22435  {
22436  // recursive call to compare array values at index i
22437  auto temp_diff = diff(source[i], target[i], path + "/" + std::to_string(i));
22438  result.insert(result.end(), temp_diff.begin(), temp_diff.end());
22439  ++i;
22440  }
22441 
22442  // i now reached the end of at least one array
22443  // in a second pass, traverse the remaining elements
22444 
22445  // remove my remaining elements
22446  const auto end_index = static_cast<difference_type>(result.size());
22447  while (i < source.size())
22448  {
22449  // add operations in reverse order to avoid invalid
22450  // indices
22451  result.insert(result.begin() + end_index, object(
22452  {
22453  {"op", "remove"},
22454  {"path", path + "/" + std::to_string(i)}
22455  }));
22456  ++i;
22457  }
22458 
22459  // add other remaining elements
22460  while (i < target.size())
22461  {
22462  result.push_back(
22463  {
22464  {"op", "add"},
22465  {"path", path + "/" + std::to_string(i)},
22466  {"value", target[i]}
22467  });
22468  ++i;
22469  }
22470 
22471  break;
22472  }
22473 
22474  case value_t::object:
22475  {
22476  // first pass: traverse this object's elements
22477  for (auto it = source.cbegin(); it != source.cend(); ++it)
22478  {
22479  // escape the key name to be used in a JSON patch
22480  const auto key = json_pointer::escape(it.key());
22481 
22482  if (target.find(it.key()) != target.end())
22483  {
22484  // recursive call to compare object values at key it
22485  auto temp_diff = diff(it.value(), target[it.key()], path + "/" + key);
22486  result.insert(result.end(), temp_diff.begin(), temp_diff.end());
22487  }
22488  else
22489  {
22490  // found a key that is not in o -> remove it
22491  result.push_back(object(
22492  {
22493  {"op", "remove"}, {"path", path + "/" + key}
22494  }));
22495  }
22496  }
22497 
22498  // second pass: traverse other object's elements
22499  for (auto it = target.cbegin(); it != target.cend(); ++it)
22500  {
22501  if (source.find(it.key()) == source.end())
22502  {
22503  // found a key that is not in this -> add it
22504  const auto key = json_pointer::escape(it.key());
22505  result.push_back(
22506  {
22507  {"op", "add"}, {"path", path + "/" + key},
22508  {"value", it.value()}
22509  });
22510  }
22511  }
22512 
22513  break;
22514  }
22515 
22516  default:
22517  {
22518  // both primitive type: replace value
22519  result.push_back(
22520  {
22521  {"op", "replace"}, {"path", path}, {"value", target}
22522  });
22523  break;
22524  }
22525  }
22526 
22527  return result;
22528  }
array (ordered collection of values)
do source
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
static std::string escape(std::string s)
escape &quot;~&quot; to &quot;~0&quot; and &quot;/&quot; to &quot;~1&quot;
Definition: json.hpp:10937
std::ptrdiff_t difference_type
a type to represent differences between iterators
Definition: json.hpp:14807
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json object(initializer_list_t init={})
explicitly create an object from an initializer list
Definition: json.hpp:16133
BEGIN_PROLOG triggeremu_data_config_icarus settings PMTADCthresholds sequence::icarus_stage0_multiTPC_TPC physics sequence::icarus_stage0_EastHits_TPC physics sequence::icarus_stage0_WestHits_TPC physics producers purityana0 caloskimCalorimetryCryoE physics caloskimCalorimetryCryoW physics path
object (unordered set of name/value pairs)
return match has_match and(match.match_pdg==11 or match.match_pdg==-11)
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json diff(const basic_json &source, const basic_json &target, const std::string &path="")
creates a diff as a JSON patch
Definition: json.hpp:22406
std::string to_string(WindowPattern const &pattern)
iterator insert(const_iterator pos, const basic_json &val)
inserts element
Definition: json.hpp:19717
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
string_t nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::dump ( const int  indent = -1,
const char  indent_char = ' ',
const bool  ensure_ascii = false,
const error_handler_t  error_handler = error_handler_t::strict 
) const
inline

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.

Parameters
[in]indentIf 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_charThe character to use for indentation if indent is greater than 0. The default is (space).
[in]ensure_asciiIf ensure_ascii is true, all non-ASCII characters in the output are escaped with \uXXXX sequences, and the result consists of ASCII characters only.
[in]error_handlerhow to react on decoding errors; there are three possible values: strict (throws and exception in case a decoding error occurs; default), replace (replace invalid UTF-8 sequences with U+FFFD), and ignore (ignore invalid UTF-8 sequences during serialization).
Returns
string containing the serialization of the JSON value
Exceptions
type_error.316if a string stored inside the JSON value is not UTF-8 encoded

Linear.

Strong guarantee: if an exception is thrown, there are no changes in the JSON value.

{The following example shows the effect of different indent\, indent_char\, and ensure_ascii parameters to the result of the serialization.,dump}

See Also
https://docs.python.org/2/library/json.html#json.dump
Since
version 1.0.0; indentation character indent_char, option ensure_ascii and exceptions added in version 3.0.0; error handlers added in version 3.4.0.

Definition at line 16560 of file json.hpp.

16564  {
16565  string_t result;
16566  serializer s(detail::output_adapter<char, string_t>(result), indent_char, error_handler);
16567 
16568  if (indent >= 0)
16569  {
16570  s.dump(*this, true, ensure_ascii, static_cast<unsigned int>(indent));
16571  }
16572  else
16573  {
16574  s.dump(*this, false, ensure_ascii, 0);
16575  }
16576 
16577  return result;
16578  }
::nlohmann::detail::serializer< basic_json > serializer
Definition: json.hpp:14748
StringType string_t
a type for a string
Definition: json.hpp:15125
then echo File list $list not found else cat $list while read file do echo $file sed s
Definition: file_to_url.sh:60
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<class... Args>
std::pair<iterator, bool> nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::emplace ( Args &&...  args)
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.

Parameters
[in]argsarguments to forward to a constructor of basic_json
Template Parameters
Argscompatible types to create a basic_json object
Returns
a pair consisting of an iterator to the inserted element, or the already-existing element if no insertion happened, and a bool denoting whether the insertion took place.
Exceptions
type_error.311when called on a type other than JSON object or null; example: "cannot use emplace() with number"

Logarithmic in the size of the container, O(log(size())).

{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}

Since
version 2.0.8

Definition at line 19649 of file json.hpp.

19650  {
19651  // emplace only works for null objects or arrays
19652  if (JSON_HEDLEY_UNLIKELY(not(is_null() or is_object())))
19653  {
19654  JSON_THROW(type_error::create(311, "cannot use emplace() with " + std::string(type_name())));
19655  }
19656 
19657  // transform null object into an object
19658  if (is_null())
19659  {
19662  assert_invariant();
19663  }
19664 
19665  // add element to array (perfect forwarding)
19666  auto res = m_value.object->emplace(std::forward<Args>(args)...);
19667  // create result iterator and set iterator to the result of emplace
19668  auto it = begin();
19669  it.m_it.object_iterator = res.first;
19670 
19671  // return pair of iterator and boolean
19672  return {it, res.second};
19673  }
object_t * object
object (stored with pointer to save storage)
Definition: json.hpp:15417
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
iterator begin() noexcept
returns an iterator to the first element
Definition: json.hpp:18674
constexpr bool is_object() const noexcept
return whether value is an object
Definition: json.hpp:16850
constexpr bool is_null() const noexcept
return whether value is null
Definition: json.hpp:16691
#define JSON_THROW(exception)
Definition: json.hpp:1754
object (unordered set of name/value pairs)
value_t m_type
the type of the current element
Definition: json.hpp:21017
void assert_invariant() const noexcept
checks the class invariants
Definition: json.hpp:15634
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: json.hpp:1124
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<class... Args>
reference nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::emplace_back ( Args &&...  args)
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.

Parameters
[in]argsarguments to forward to a constructor of basic_json
Template Parameters
Argscompatible types to create a basic_json object
Returns
reference to the inserted element
Exceptions
type_error.311when called on a type other than JSON array or null; example: "cannot use emplace_back() with number"

Amortized constant.

{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}

Since
version 2.0.8, returns reference since 3.7.0

Definition at line 19596 of file json.hpp.

19597  {
19598  // emplace_back only works for null objects or arrays
19599  if (JSON_HEDLEY_UNLIKELY(not(is_null() or is_array())))
19600  {
19601  JSON_THROW(type_error::create(311, "cannot use emplace_back() with " + std::string(type_name())));
19602  }
19603 
19604  // transform null object into an array
19605  if (is_null())
19606  {
19609  assert_invariant();
19610  }
19611 
19612  // add element to array (perfect forwarding)
19613 #ifdef JSON_HAS_CPP_17
19614  return m_value.array->emplace_back(std::forward<Args>(args)...);
19615 #else
19616  m_value.array->emplace_back(std::forward<Args>(args)...);
19617  return m_value.array->back();
19618 #endif
19619  }
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
array_t * array
array (stored with pointer to save storage)
Definition: json.hpp:15419
array (ordered collection of values)
constexpr bool is_null() const noexcept
return whether value is null
Definition: json.hpp:16691
#define JSON_THROW(exception)
Definition: json.hpp:1754
value_t m_type
the type of the current element
Definition: json.hpp:21017
void assert_invariant() const noexcept
checks the class invariants
Definition: json.hpp:15634
constexpr bool is_array() const noexcept
return whether value is an array
Definition: json.hpp:16872
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: json.hpp:1124
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
bool nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::empty ( ) const
inlinenoexcept

checks whether the container is empty.

Checks if a JSON value has no elements (i.e. whether its size is 0).

Returns
The return value depends on the different types and is defined as follows:
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()
{The following code uses empty() to check if a JSON object contains any elements.,empty}

Constant, as long as array_t and object_t satisfy the Container concept; that is, their empty() functions have constant complexity.

No changes.

No-throw guarantee: this function never throws exceptions.

Note
This function does not return whether a string stored as JSON value is empty - it returns whether the JSON container itself is empty which is false in the case of a string.

This function helps basic_json satisfying the Container requirements:

  • The complexity is constant.
  • Has the semantics of begin() == end().
See Also
size() – returns the number of elements
Since
version 1.0.0

Definition at line 19123 of file json.hpp.

19124  {
19125  switch (m_type)
19126  {
19127  case value_t::null:
19128  {
19129  // null values are empty
19130  return true;
19131  }
19132 
19133  case value_t::array:
19134  {
19135  // delegate call to array_t::empty()
19136  return m_value.array->empty();
19137  }
19138 
19139  case value_t::object:
19140  {
19141  // delegate call to object_t::empty()
19142  return m_value.object->empty();
19143  }
19144 
19145  default:
19146  {
19147  // all other types are nonempty
19148  return false;
19149  }
19150  }
19151  }
object_t * object
object (stored with pointer to save storage)
Definition: json.hpp:15417
array_t * array
array (stored with pointer to save storage)
Definition: json.hpp:15419
array (ordered collection of values)
object (unordered set of name/value pairs)
value_t m_type
the type of the current element
Definition: json.hpp:21017
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
iterator nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::end ( void  )
inlinenoexcept

returns an iterator to one past the last element

Returns an iterator to one past the last element.

range-begin-end.svg
Illustration from cppreference.com
Returns
iterator one past the last element

Constant.

This function helps basic_json satisfying the Container requirements:

  • The complexity is constant.

{The following code shows an example for end().,end}

See Also
cend() – returns a const iterator to the end
begin() – returns an iterator to the beginning
cbegin() – returns a const iterator to the beginning
Since
version 1.0.0

Definition at line 18745 of file json.hpp.

18746  {
18747  iterator result(this);
18748  result.set_end();
18749  return result;
18750  }
iter_impl< basic_json > iterator
an iterator for a basic_json container
Definition: json.hpp:14820
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
const_iterator nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::end ( void  ) const
inlinenoexcept

returns a const iterator to one past the last element

Returns a const iterator to one past the last element.

range-begin-end.svg
Illustration from cppreference.com
Returns
const iterator one past the last element

Constant.

This function helps basic_json satisfying the Container requirements:

  • The complexity is constant.
  • Has the semantics of const_cast<const basic_json&>(*this).end().

{The following code shows an example for cend().,cend}

See Also
end() – returns an iterator to the end
begin() – returns an iterator to the beginning
cbegin() – returns a const iterator to the beginning
Since
version 1.0.0

Definition at line 18755 of file json.hpp.

18756  {
18757  return cend();
18758  }
const_iterator cend() const noexcept
returns a const iterator to one past the last element
Definition: json.hpp:18785
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<class IteratorType , typename std::enable_if< std::is_same< IteratorType, typename basic_json_t::iterator >::value orstd::is_same< IteratorType, typename basic_json_t::const_iterator >::value, int >::type = 0>
IteratorType nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::erase ( IteratorType  pos)
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.

Parameters
[in]positerator to the element to remove
Returns
Iterator following the last removed element. If the iterator pos refers to the last element, the end() iterator is returned.
Template Parameters
IteratorTypean iterator or const_iterator
Postcondition
Invalidates iterators and references at or after the point of the erase, including the end() iterator.
Exceptions
type_error.307if called on a null value; example: "cannot use erase() with null"
invalid_iterator.202if called on an iterator which does not belong to the current JSON value; example: "iterator does not fit current value"
invalid_iterator.205if called on a primitive type with invalid iterator (i.e., any iterator which is not begin()); example: "iterator out of range"

The complexity depends on the type:

  • objects: amortized constant
  • arrays: linear in distance between pos and the end of the container
  • strings: linear in the length of the string
  • other types: constant

{The example shows the result of erase() for different JSON types.,erase__IteratorType}

See Also
erase(IteratorType, IteratorType) – removes the elements in the given range
erase(const typename object_t::key_type&) – removes the element from an object at the given key
erase(const size_type) – removes the element from an array at the given index
Since
version 1.0.0

Definition at line 18240 of file json.hpp.

18241  {
18242  // make sure iterator fits the current value
18243  if (JSON_HEDLEY_UNLIKELY(this != pos.m_object))
18244  {
18245  JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value"));
18246  }
18247 
18248  IteratorType result = end();
18249 
18250  switch (m_type)
18251  {
18252  case value_t::boolean:
18253  case value_t::number_float:
18256  case value_t::string:
18257  {
18258  if (JSON_HEDLEY_UNLIKELY(not pos.m_it.primitive_iterator.is_begin()))
18259  {
18260  JSON_THROW(invalid_iterator::create(205, "iterator out of range"));
18261  }
18262 
18263  if (is_string())
18264  {
18265  AllocatorType<string_t> alloc;
18266  std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.string);
18267  std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.string, 1);
18268  m_value.string = nullptr;
18269  }
18270 
18272  assert_invariant();
18273  break;
18274  }
18275 
18276  case value_t::object:
18277  {
18278  result.m_it.object_iterator = m_value.object->erase(pos.m_it.object_iterator);
18279  break;
18280  }
18281 
18282  case value_t::array:
18283  {
18284  result.m_it.array_iterator = m_value.array->erase(pos.m_it.array_iterator);
18285  break;
18286  }
18287 
18288  default:
18289  JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name())));
18290  }
18291 
18292  return result;
18293  }
object_t * object
object (stored with pointer to save storage)
Definition: json.hpp:15417
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
array_t * array
array (stored with pointer to save storage)
Definition: json.hpp:15419
string_t * string
string (stored with pointer to save storage)
Definition: json.hpp:15421
array (ordered collection of values)
iterator end() noexcept
returns an iterator to one past the last element
Definition: json.hpp:18745
number value (signed integer)
#define JSON_THROW(exception)
Definition: json.hpp:1754
constexpr bool is_string() const noexcept
return whether value is a string
Definition: json.hpp:16894
object (unordered set of name/value pairs)
value_t m_type
the type of the current element
Definition: json.hpp:21017
void assert_invariant() const noexcept
checks the class invariants
Definition: json.hpp:15634
static invalid_iterator create(int id_, const std::string &what_arg)
Definition: json.hpp:2024
number value (unsigned integer)
number value (floating-point)
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: json.hpp:1124
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<class IteratorType , typename std::enable_if< std::is_same< IteratorType, typename basic_json_t::iterator >::value orstd::is_same< IteratorType, typename basic_json_t::const_iterator >::value, int >::type = 0>
IteratorType nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::erase ( IteratorType  first,
IteratorType  last 
)
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.

Parameters
[in]firstiterator to the beginning of the range to remove
[in]lastiterator past the end of the range to remove
Returns
Iterator following the last removed element. If the iterator second refers to the last element, the end() iterator is returned.
Template Parameters
IteratorTypean iterator or const_iterator
Postcondition
Invalidates iterators and references at or after the point of the erase, including the end() iterator.
Exceptions
type_error.307if called on a null value; example: "cannot use erase() with null"
invalid_iterator.203if called on iterators which does not belong to the current JSON value; example: "iterators do not fit current value"
invalid_iterator.204if called on a primitive type with invalid iterators (i.e., if first != begin() and last != end()); example: "iterators out of range"

The complexity depends on the type:

  • objects: log(size()) + std::distance(first, last)
  • arrays: linear in the distance between first and last, plus linear in the distance between last and end of the container
  • strings: linear in the length of the string
  • other types: constant

{The example shows the result of erase() for different JSON types.,erase__IteratorType_IteratorType}

See Also
erase(IteratorType) – removes the element at a given position
erase(const typename object_t::key_type&) – removes the element from an object at the given key
erase(const size_type) – removes the element from an array at the given index
Since
version 1.0.0

Definition at line 18345 of file json.hpp.

18346  {
18347  // make sure iterator fits the current value
18348  if (JSON_HEDLEY_UNLIKELY(this != first.m_object or this != last.m_object))
18349  {
18350  JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value"));
18351  }
18352 
18353  IteratorType result = end();
18354 
18355  switch (m_type)
18356  {
18357  case value_t::boolean:
18358  case value_t::number_float:
18361  case value_t::string:
18362  {
18363  if (JSON_HEDLEY_LIKELY(not first.m_it.primitive_iterator.is_begin()
18364  or not last.m_it.primitive_iterator.is_end()))
18365  {
18366  JSON_THROW(invalid_iterator::create(204, "iterators out of range"));
18367  }
18368 
18369  if (is_string())
18370  {
18371  AllocatorType<string_t> alloc;
18372  std::allocator_traits<decltype(alloc)>::destroy(alloc, m_value.string);
18373  std::allocator_traits<decltype(alloc)>::deallocate(alloc, m_value.string, 1);
18374  m_value.string = nullptr;
18375  }
18376 
18378  assert_invariant();
18379  break;
18380  }
18381 
18382  case value_t::object:
18383  {
18384  result.m_it.object_iterator = m_value.object->erase(first.m_it.object_iterator,
18385  last.m_it.object_iterator);
18386  break;
18387  }
18388 
18389  case value_t::array:
18390  {
18391  result.m_it.array_iterator = m_value.array->erase(first.m_it.array_iterator,
18392  last.m_it.array_iterator);
18393  break;
18394  }
18395 
18396  default:
18397  JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name())));
18398  }
18399 
18400  return result;
18401  }
object_t * object
object (stored with pointer to save storage)
Definition: json.hpp:15417
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
array_t * array
array (stored with pointer to save storage)
Definition: json.hpp:15419
string_t * string
string (stored with pointer to save storage)
Definition: json.hpp:15421
array (ordered collection of values)
iterator end() noexcept
returns an iterator to one past the last element
Definition: json.hpp:18745
number value (signed integer)
#define JSON_THROW(exception)
Definition: json.hpp:1754
constexpr bool is_string() const noexcept
return whether value is a string
Definition: json.hpp:16894
object (unordered set of name/value pairs)
value_t m_type
the type of the current element
Definition: json.hpp:21017
void assert_invariant() const noexcept
checks the class invariants
Definition: json.hpp:15634
static invalid_iterator create(int id_, const std::string &what_arg)
Definition: json.hpp:2024
#define JSON_HEDLEY_LIKELY(expr)
Definition: json.hpp:1123
number value (unsigned integer)
number value (floating-point)
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: json.hpp:1124
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
size_type nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::erase ( const typename object_t::key_type &  key)
inline

remove element from a JSON object given a key

Removes elements from a JSON object with the key value key.

Parameters
[in]keyvalue of the elements to remove
Returns
Number of elements removed. If ObjectType is the default std::map type, the return value will always be 0 (key was not found) or 1 (key was found).
Postcondition
References and iterators to the erased elements are invalidated. Other references and iterators are not affected.
Exceptions
type_error.307when called on a type other than JSON object; example: "cannot use erase() with null"

log(size()) + count(key)

{The example shows the effect of erase().,erase__key_type}

See Also
erase(IteratorType) – removes the element at a given position
erase(IteratorType, IteratorType) – removes the elements in the given range
erase(const size_type) – removes the element from an array at the given index
Since
version 1.0.0

Definition at line 18432 of file json.hpp.

18433  {
18434  // this erase only works for objects
18436  {
18437  return m_value.object->erase(key);
18438  }
18439 
18440  JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name())));
18441  }
object_t * object
object (stored with pointer to save storage)
Definition: json.hpp:15417
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
constexpr bool is_object() const noexcept
return whether value is an object
Definition: json.hpp:16850
#define JSON_THROW(exception)
Definition: json.hpp:1754
#define JSON_HEDLEY_LIKELY(expr)
Definition: json.hpp:1123
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
void nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::erase ( const size_type  idx)
inline

remove element from a JSON array given an index

Removes element from a JSON array at the index idx.

Parameters
[in]idxindex of the element to remove
Exceptions
type_error.307when called on a type other than JSON object; example: "cannot use erase() with null"
out_of_range.401when idx >= size(); example: "array index 17 is out of range"

Linear in distance between idx and the end of the container.

{The example shows the effect of erase().,erase__size_type}

See Also
erase(IteratorType) – removes the element at a given position
erase(IteratorType, IteratorType) – removes the elements in the given range
erase(const typename object_t::key_type&) – removes the element from an object at the given key
Since
version 1.0.0

Definition at line 18467 of file json.hpp.

18468  {
18469  // this erase only works for arrays
18471  {
18472  if (JSON_HEDLEY_UNLIKELY(idx >= size()))
18473  {
18474  JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range"));
18475  }
18476 
18477  m_value.array->erase(m_value.array->begin() + static_cast<difference_type>(idx));
18478  }
18479  else
18480  {
18481  JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name())));
18482  }
18483  }
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
array_t * array
array (stored with pointer to save storage)
Definition: json.hpp:15419
std::ptrdiff_t difference_type
a type to represent differences between iterators
Definition: json.hpp:14807
#define JSON_THROW(exception)
Definition: json.hpp:1754
size_type size() const noexcept
returns the number of elements
Definition: json.hpp:19195
#define JSON_HEDLEY_LIKELY(expr)
Definition: json.hpp:1123
std::string to_string(WindowPattern const &pattern)
constexpr bool is_array() const noexcept
return whether value is an array
Definition: json.hpp:16872
static out_of_range create(int id_, const std::string &what_arg)
Definition: json.hpp:2125
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: json.hpp:1124
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename KeyT >
iterator nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::find ( KeyT &&  key)
inline

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.

Note
This method always returns end() when executed on a JSON type that is not an object.
Parameters
[in]keykey value of the element to search for.
Returns
Iterator to an element with key equivalent to key. If no such element is found or the JSON value is not an object, past-the-end (see end()) iterator is returned.

Logarithmic in the size of the JSON object.

{The example shows how find() is used.,find__key_type}

See Also
contains(KeyT&&) const – checks whether a key exists
Since
version 1.0.0

Definition at line 18520 of file json.hpp.

18521  {
18522  auto result = end();
18523 
18524  if (is_object())
18525  {
18526  result.m_it.object_iterator = m_value.object->find(std::forward<KeyT>(key));
18527  }
18528 
18529  return result;
18530  }
object_t * object
object (stored with pointer to save storage)
Definition: json.hpp:15417
iterator end() noexcept
returns an iterator to one past the last element
Definition: json.hpp:18745
constexpr bool is_object() const noexcept
return whether value is an object
Definition: json.hpp:16850
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename KeyT >
const_iterator nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::find ( KeyT &&  key) const
inline

find an element in a JSON object

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.

Note
This method always returns end() when executed on a JSON type that is not an object.
Parameters
[in]keykey value of the element to search for.
Returns
Iterator to an element with key equivalent to key. If no such element is found or the JSON value is not an object, past-the-end (see end()) iterator is returned.

Logarithmic in the size of the JSON object.

{The example shows how find() is used.,find__key_type}

See Also
contains(KeyT&&) const – checks whether a key exists
Since
version 1.0.0

Definition at line 18537 of file json.hpp.

18538  {
18539  auto result = cend();
18540 
18541  if (is_object())
18542  {
18543  result.m_it.object_iterator = m_value.object->find(std::forward<KeyT>(key));
18544  }
18545 
18546  return result;
18547  }
object_t * object
object (stored with pointer to save storage)
Definition: json.hpp:15417
constexpr bool is_object() const noexcept
return whether value is an object
Definition: json.hpp:16850
const_iterator cend() const noexcept
returns a const iterator to one past the last element
Definition: json.hpp:18785
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
basic_json nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::flatten ( ) const
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.

Returns
an object that maps JSON pointers to primitive values
Note
Empty objects and arrays are flattened to null and will not be reconstructed correctly by the unflatten() function.

Linear in the size the JSON value.

{The following code shows how a JSON object is flattened to an object whose keys consist of JSON pointers.,flatten}

See Also
unflatten() for the reverse function
Since
version 2.0.0

Definition at line 22012 of file json.hpp.

22013  {
22014  basic_json result(value_t::object);
22015  json_pointer::flatten("", *this, result);
22016  return result;
22017  }
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
object (unordered set of name/value pairs)
static void flatten(const std::string &reference_string, const BasicJsonType &value, BasicJsonType &result)
Definition: json.hpp:10958
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::from_bson ( detail::input_adapter &&  i,
const bool  strict = true,
const bool  allow_exceptions = true 
)
inlinestatic

Create a JSON value from an input in BSON format.

Deserializes a given input i to a JSON value using the BSON (Binary JSON) serialization format.

The library maps BSON record types to JSON value types as follows:

BSON type BSON marker byte JSON value type
double 0x01 number_float
string 0x02 string
document 0x03 object
array 0x04 array
binary 0x05 still unsupported
undefined 0x06 still unsupported
ObjectId 0x07 still unsupported
boolean 0x08 boolean
UTC Date-Time 0x09 still unsupported
null 0x0A null
Regular Expr. 0x0B still unsupported
DB Pointer 0x0C still unsupported
JavaScript Code 0x0D still unsupported
Symbol 0x0E still unsupported
JavaScript Code 0x0F still unsupported
int32 0x10 number_integer
Timestamp 0x11 still unsupported
128-bit decimal float 0x13 still unsupported
Max Key 0x7F still unsupported
Min Key 0xFF still unsupported
Warning
The mapping is incomplete. The unsupported mappings are indicated in the table above.
Parameters
[in]ian input in BSON format convertible to an input adapter
[in]strictwhether to expect the input to be consumed until EOF (true by default)
[in]allow_exceptionswhether to throw exceptions in case of a parse error (optional, true by default)
Returns
deserialized JSON value; in case of a parse error and allow_exceptions set to false, the return value will be value_t::discarded.
Exceptions
parse_error.114if an unsupported BSON record type is encountered

Linear in the size of the input i.

{The example shows the deserialization of a byte vector in BSON format to a JSON value.,from_bson}

See Also
http://bsonspec.org/spec.html
to_bson(const basic_json&) for the analogous serialization
from_cbor(detail::input_adapter&&, const bool, const bool) for the related CBOR format
from_msgpack(detail::input_adapter&&, const bool, const bool) for the related MessagePack format
from_ubjson(detail::input_adapter&&, const bool, const bool) for the related UBJSON format

Definition at line 21801 of file json.hpp.

21804  {
21805  basic_json result;
21806  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
21807  const bool res = binary_reader(detail::input_adapter(i)).sax_parse(input_format_t::bson, &sdp, strict);
21808  return res ? result : basic_json(value_t::discarded);
21809  }
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
::nlohmann::detail::binary_reader< basic_json > binary_reader
Definition: json.hpp:14745
discarded by the the parser callback function
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename A1 , typename A2 , detail::enable_if_t< std::is_constructible< detail::input_adapter, A1, A2 >::value, int > = 0>
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::from_bson ( A1 &&  a1,
A2 &&  a2,
const bool  strict = true,
const bool  allow_exceptions = true 
)
inlinestatic

Create a JSON value from an input in BSON format.

Deserializes a given input i to a JSON value using the BSON (Binary JSON) serialization format.

The library maps BSON record types to JSON value types as follows:

BSON type BSON marker byte JSON value type
double 0x01 number_float
string 0x02 string
document 0x03 object
array 0x04 array
binary 0x05 still unsupported
undefined 0x06 still unsupported
ObjectId 0x07 still unsupported
boolean 0x08 boolean
UTC Date-Time 0x09 still unsupported
null 0x0A null
Regular Expr. 0x0B still unsupported
DB Pointer 0x0C still unsupported
JavaScript Code 0x0D still unsupported
Symbol 0x0E still unsupported
JavaScript Code 0x0F still unsupported
int32 0x10 number_integer
Timestamp 0x11 still unsupported
128-bit decimal float 0x13 still unsupported
Max Key 0x7F still unsupported
Min Key 0xFF still unsupported
Warning
The mapping is incomplete. The unsupported mappings are indicated in the table above.
Parameters
[in]ian input in BSON format convertible to an input adapter
[in]strictwhether to expect the input to be consumed until EOF (true by default)
[in]allow_exceptionswhether to throw exceptions in case of a parse error (optional, true by default)
Returns
deserialized JSON value; in case of a parse error and allow_exceptions set to false, the return value will be value_t::discarded.
Exceptions
parse_error.114if an unsupported BSON record type is encountered

Linear in the size of the input i.

{The example shows the deserialization of a byte vector in BSON format to a JSON value.,from_bson}

See Also
http://bsonspec.org/spec.html
to_bson(const basic_json&) for the analogous serialization
from_cbor(detail::input_adapter&&, const bool, const bool) for the related CBOR format
from_msgpack(detail::input_adapter&&, const bool, const bool) for the related MessagePack format
from_ubjson(detail::input_adapter&&, const bool, const bool) for the related UBJSON format

Definition at line 21817 of file json.hpp.

21820  {
21821  basic_json result;
21822  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
21823  const bool res = binary_reader(detail::input_adapter(std::forward<A1>(a1), std::forward<A2>(a2))).sax_parse(input_format_t::bson, &sdp, strict);
21824  return res ? result : basic_json(value_t::discarded);
21825  }
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
#define a2
::nlohmann::detail::binary_reader< basic_json > binary_reader
Definition: json.hpp:14745
discarded by the the parser callback function
#define a1
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::from_cbor ( detail::input_adapter &&  i,
const bool  strict = true,
const bool  allow_exceptions = true 
)
inlinestatic

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
Null null 0xF6
Half-Precision Float number_float 0xF9
Single-Precision Float number_float 0xFA
Double-Precision Float number_float 0xFB
Warning
The mapping is incomplete in the sense that not all CBOR types can be converted to a JSON value. The following CBOR types are not supported and will yield parse errors (parse_error.112):
  • byte strings (0x40..0x5F)
  • date/time (0xC0..0xC1)
  • bignum (0xC2..0xC3)
  • decimal fraction (0xC4)
  • bigfloat (0xC5)
  • tagged items (0xC6..0xD4, 0xD8..0xDB)
  • expected conversions (0xD5..0xD7)
  • simple values (0xE0..0xF3, 0xF8)
  • undefined (0xF7)
CBOR allows map keys of any type, whereas JSON only allows strings as keys in object values. Therefore, CBOR maps with keys other than UTF-8 strings are rejected (parse_error.113).
Note
Any CBOR output created to_cbor can be successfully parsed by from_cbor.
Parameters
[in]ian input in CBOR format convertible to an input adapter
[in]strictwhether to expect the input to be consumed until EOF (true by default)
[in]allow_exceptionswhether to throw exceptions in case of a parse error (optional, true by default)
Returns
deserialized JSON value; in case of a parse error and allow_exceptions set to false, the return value will be value_t::discarded.
Exceptions
parse_error.110if the given input ends prematurely or the end of file was not reached when strict was set to true
parse_error.112if unsupported features from CBOR were used in the given input v or if the input is not valid CBOR
parse_error.113if a string was expected as map key, but not found

Linear in the size of the input i.

{The example shows the deserialization of a byte vector in CBOR format to a JSON value.,from_cbor}

See Also
http://cbor.io
to_cbor(const basic_json&) for the analogous serialization
from_msgpack(detail::input_adapter&&, const bool, const bool) for the related MessagePack format
from_ubjson(detail::input_adapter&&, const bool, const bool) for the related UBJSON format
Since
version 2.0.9; parameter start_index since 2.1.1; changed to consume input adapters, removed start_index parameter, and added strict parameter since 3.0.0; added allow_exceptions parameter since 3.2.0

Definition at line 21517 of file json.hpp.

21520  {
21521  basic_json result;
21522  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
21523  const bool res = binary_reader(detail::input_adapter(i)).sax_parse(input_format_t::cbor, &sdp, strict);
21524  return res ? result : basic_json(value_t::discarded);
21525  }
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
::nlohmann::detail::binary_reader< basic_json > binary_reader
Definition: json.hpp:14745
discarded by the the parser callback function
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename A1 , typename A2 , detail::enable_if_t< std::is_constructible< detail::input_adapter, A1, A2 >::value, int > = 0>
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::from_cbor ( A1 &&  a1,
A2 &&  a2,
const bool  strict = true,
const bool  allow_exceptions = true 
)
inlinestatic

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
Null null 0xF6
Half-Precision Float number_float 0xF9
Single-Precision Float number_float 0xFA
Double-Precision Float number_float 0xFB
Warning
The mapping is incomplete in the sense that not all CBOR types can be converted to a JSON value. The following CBOR types are not supported and will yield parse errors (parse_error.112):
  • byte strings (0x40..0x5F)
  • date/time (0xC0..0xC1)
  • bignum (0xC2..0xC3)
  • decimal fraction (0xC4)
  • bigfloat (0xC5)
  • tagged items (0xC6..0xD4, 0xD8..0xDB)
  • expected conversions (0xD5..0xD7)
  • simple values (0xE0..0xF3, 0xF8)
  • undefined (0xF7)
CBOR allows map keys of any type, whereas JSON only allows strings as keys in object values. Therefore, CBOR maps with keys other than UTF-8 strings are rejected (parse_error.113).
Note
Any CBOR output created to_cbor can be successfully parsed by from_cbor.
Parameters
[in]ian input in CBOR format convertible to an input adapter
[in]strictwhether to expect the input to be consumed until EOF (true by default)
[in]allow_exceptionswhether to throw exceptions in case of a parse error (optional, true by default)
Returns
deserialized JSON value; in case of a parse error and allow_exceptions set to false, the return value will be value_t::discarded.
Exceptions
parse_error.110if the given input ends prematurely or the end of file was not reached when strict was set to true
parse_error.112if unsupported features from CBOR were used in the given input v or if the input is not valid CBOR
parse_error.113if a string was expected as map key, but not found

Linear in the size of the input i.

{The example shows the deserialization of a byte vector in CBOR format to a JSON value.,from_cbor}

See Also
http://cbor.io
to_cbor(const basic_json&) for the analogous serialization
from_msgpack(detail::input_adapter&&, const bool, const bool) for the related MessagePack format
from_ubjson(detail::input_adapter&&, const bool, const bool) for the related UBJSON format
Since
version 2.0.9; parameter start_index since 2.1.1; changed to consume input adapters, removed start_index parameter, and added strict parameter since 3.0.0; added allow_exceptions parameter since 3.2.0

Definition at line 21533 of file json.hpp.

21536  {
21537  basic_json result;
21538  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
21539  const bool res = binary_reader(detail::input_adapter(std::forward<A1>(a1), std::forward<A2>(a2))).sax_parse(input_format_t::cbor, &sdp, strict);
21540  return res ? result : basic_json(value_t::discarded);
21541  }
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
#define a2
::nlohmann::detail::binary_reader< basic_json > binary_reader
Definition: json.hpp:14745
discarded by the the parser callback function
#define a1
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::from_msgpack ( detail::input_adapter &&  i,
const bool  strict = true,
const bool  allow_exceptions = true 
)
inlinestatic

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
Warning
The mapping is incomplete in the sense that not all MessagePack types can be converted to a JSON value. The following MessagePack types are not supported and will yield parse errors:
  • bin 8 - bin 32 (0xC4..0xC6)
  • ext 8 - ext 32 (0xC7..0xC9)
  • fixext 1 - fixext 16 (0xD4..0xD8)
Note
Any MessagePack output created to_msgpack can be successfully parsed by from_msgpack.
Parameters
[in]ian input in MessagePack format convertible to an input adapter
[in]strictwhether to expect the input to be consumed until EOF (true by default)
[in]allow_exceptionswhether to throw exceptions in case of a parse error (optional, true by default)
Returns
deserialized JSON value; in case of a parse error and allow_exceptions set to false, the return value will be value_t::discarded.
Exceptions
parse_error.110if the given input ends prematurely or the end of file was not reached when strict was set to true
parse_error.112if unsupported features from MessagePack were used in the given input i or if the input is not valid MessagePack
parse_error.113if a string was expected as map key, but not found

Linear in the size of the input i.

{The example shows the deserialization of a byte vector in MessagePack format to a JSON value.,from_msgpack}

See Also
http://msgpack.org
to_msgpack(const basic_json&) for the analogous serialization
from_cbor(detail::input_adapter&&, const bool, const bool) for the related CBOR format
from_ubjson(detail::input_adapter&&, const bool, const bool) for the related UBJSON format
from_bson(detail::input_adapter&&, const bool, const bool) for the related BSON format
Since
version 2.0.9; parameter start_index since 2.1.1; changed to consume input adapters, removed start_index parameter, and added strict parameter since 3.0.0; added allow_exceptions parameter since 3.2.0

Definition at line 21626 of file json.hpp.

21629  {
21630  basic_json result;
21631  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
21632  const bool res = binary_reader(detail::input_adapter(i)).sax_parse(input_format_t::msgpack, &sdp, strict);
21633  return res ? result : basic_json(value_t::discarded);
21634  }
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
::nlohmann::detail::binary_reader< basic_json > binary_reader
Definition: json.hpp:14745
discarded by the the parser callback function
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename A1 , typename A2 , detail::enable_if_t< std::is_constructible< detail::input_adapter, A1, A2 >::value, int > = 0>
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::from_msgpack ( A1 &&  a1,
A2 &&  a2,
const bool  strict = true,
const bool  allow_exceptions = true 
)
inlinestatic

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
Warning
The mapping is incomplete in the sense that not all MessagePack types can be converted to a JSON value. The following MessagePack types are not supported and will yield parse errors:
  • bin 8 - bin 32 (0xC4..0xC6)
  • ext 8 - ext 32 (0xC7..0xC9)
  • fixext 1 - fixext 16 (0xD4..0xD8)
Note
Any MessagePack output created to_msgpack can be successfully parsed by from_msgpack.
Parameters
[in]ian input in MessagePack format convertible to an input adapter
[in]strictwhether to expect the input to be consumed until EOF (true by default)
[in]allow_exceptionswhether to throw exceptions in case of a parse error (optional, true by default)
Returns
deserialized JSON value; in case of a parse error and allow_exceptions set to false, the return value will be value_t::discarded.
Exceptions
parse_error.110if the given input ends prematurely or the end of file was not reached when strict was set to true
parse_error.112if unsupported features from MessagePack were used in the given input i or if the input is not valid MessagePack
parse_error.113if a string was expected as map key, but not found

Linear in the size of the input i.

{The example shows the deserialization of a byte vector in MessagePack format to a JSON value.,from_msgpack}

See Also
http://msgpack.org
to_msgpack(const basic_json&) for the analogous serialization
from_cbor(detail::input_adapter&&, const bool, const bool) for the related CBOR format
from_ubjson(detail::input_adapter&&, const bool, const bool) for the related UBJSON format
from_bson(detail::input_adapter&&, const bool, const bool) for the related BSON format
Since
version 2.0.9; parameter start_index since 2.1.1; changed to consume input adapters, removed start_index parameter, and added strict parameter since 3.0.0; added allow_exceptions parameter since 3.2.0

Definition at line 21642 of file json.hpp.

21645  {
21646  basic_json result;
21647  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
21648  const bool res = binary_reader(detail::input_adapter(std::forward<A1>(a1), std::forward<A2>(a2))).sax_parse(input_format_t::msgpack, &sdp, strict);
21649  return res ? result : basic_json(value_t::discarded);
21650  }
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
#define a2
::nlohmann::detail::binary_reader< basic_json > binary_reader
Definition: json.hpp:14745
discarded by the the parser callback function
#define a1
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::from_ubjson ( detail::input_adapter &&  i,
const bool  strict = true,
const bool  allow_exceptions = true 
)
inlinestatic

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) {
Note
The mapping is complete in the sense that any UBJSON value can be converted to a JSON value.
Parameters
[in]ian input in UBJSON format convertible to an input adapter
[in]strictwhether to expect the input to be consumed until EOF (true by default)
[in]allow_exceptionswhether to throw exceptions in case of a parse error (optional, true by default)
Returns
deserialized JSON value; in case of a parse error and allow_exceptions set to false, the return value will be value_t::discarded.
Exceptions
parse_error.110if the given input ends prematurely or the end of file was not reached when strict was set to true
parse_error.112if a parse error occurs
parse_error.113if a string could not be parsed successfully

Linear in the size of the input i.

{The example shows the deserialization of a byte vector in UBJSON format to a JSON value.,from_ubjson}

See Also
http://ubjson.org
to_ubjson(const basic_json&, const bool, const bool) for the analogous serialization
from_cbor(detail::input_adapter&&, const bool, const bool) for the related CBOR format
from_msgpack(detail::input_adapter&&, const bool, const bool) for the related MessagePack format
from_bson(detail::input_adapter&&, const bool, const bool) for the related BSON format
Since
version 3.1.0; added allow_exceptions parameter since 3.2.0

Definition at line 21714 of file json.hpp.

21717  {
21718  basic_json result;
21719  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
21720  const bool res = binary_reader(detail::input_adapter(i)).sax_parse(input_format_t::ubjson, &sdp, strict);
21721  return res ? result : basic_json(value_t::discarded);
21722  }
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
::nlohmann::detail::binary_reader< basic_json > binary_reader
Definition: json.hpp:14745
discarded by the the parser callback function
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename A1 , typename A2 , detail::enable_if_t< std::is_constructible< detail::input_adapter, A1, A2 >::value, int > = 0>
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::from_ubjson ( A1 &&  a1,
A2 &&  a2,
const bool  strict = true,
const bool  allow_exceptions = true 
)
inlinestatic

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) {
Note
The mapping is complete in the sense that any UBJSON value can be converted to a JSON value.
Parameters
[in]ian input in UBJSON format convertible to an input adapter
[in]strictwhether to expect the input to be consumed until EOF (true by default)
[in]allow_exceptionswhether to throw exceptions in case of a parse error (optional, true by default)
Returns
deserialized JSON value; in case of a parse error and allow_exceptions set to false, the return value will be value_t::discarded.
Exceptions
parse_error.110if the given input ends prematurely or the end of file was not reached when strict was set to true
parse_error.112if a parse error occurs
parse_error.113if a string could not be parsed successfully

Linear in the size of the input i.

{The example shows the deserialization of a byte vector in UBJSON format to a JSON value.,from_ubjson}

See Also
http://ubjson.org
to_ubjson(const basic_json&, const bool, const bool) for the analogous serialization
from_cbor(detail::input_adapter&&, const bool, const bool) for the related CBOR format
from_msgpack(detail::input_adapter&&, const bool, const bool) for the related MessagePack format
from_bson(detail::input_adapter&&, const bool, const bool) for the related BSON format
Since
version 3.1.0; added allow_exceptions parameter since 3.2.0

Definition at line 21730 of file json.hpp.

21733  {
21734  basic_json result;
21735  detail::json_sax_dom_parser<basic_json> sdp(result, allow_exceptions);
21736  const bool res = binary_reader(detail::input_adapter(std::forward<A1>(a1), std::forward<A2>(a2))).sax_parse(input_format_t::ubjson, &sdp, strict);
21737  return res ? result : basic_json(value_t::discarded);
21738  }
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
#define a2
::nlohmann::detail::binary_reader< basic_json > binary_reader
Definition: json.hpp:14745
discarded by the the parser callback function
#define a1
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
reference nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::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().

Returns
In case of a structured type (array or object), a reference to the first element is returned. In case of number, string, or boolean values, a reference to the value is returned.

Constant.

Precondition
The JSON value must not be null (would throw std::out_of_range) or an empty array or object (undefined behavior, guarded by assertions).
Postcondition
The JSON value remains unchanged.
Exceptions
invalid_iterator.214when called on null value

{The following code shows an example for front().,front}

See Also
back() – access the last element
Since
version 1.0.0

Definition at line 18129 of file json.hpp.

18130  {
18131  return *begin();
18132  }
iterator begin() noexcept
returns an iterator to the first element
Definition: json.hpp:18674
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
const_reference nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::front ( ) const
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().

Returns
In case of a structured type (array or object), a reference to the first element is returned. In case of number, string, or boolean values, a reference to the value is returned.

Constant.

Precondition
The JSON value must not be null (would throw std::out_of_range) or an empty array or object (undefined behavior, guarded by assertions).
Postcondition
The JSON value remains unchanged.
Exceptions
invalid_iterator.214when called on null value

{The following code shows an example for front().,front}

See Also
back() – access the last element
Since
version 1.0.0

Definition at line 18137 of file json.hpp.

18138  {
18139  return *cbegin();
18140  }
const_iterator cbegin() const noexcept
returns a const iterator to the first element
Definition: json.hpp:18714
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename BasicJsonType , detail::enable_if_t< std::is_same< typename std::remove_const< BasicJsonType >::type, basic_json_t >::value, int > = 0>
basic_json nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::get ( ) const
inline

get special-case overload

This overloads avoids a lot of template boilerplate, it can be seen as the identity method

Template Parameters
BasicJsonType== basic_json
Returns
a copy of *this

Constant.

Since
version 2.1.0

Definition at line 17101 of file json.hpp.

17102  {
17103  return *this;
17104  }
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename BasicJsonType , detail::enable_if_t< not std::is_same< BasicJsonType, basic_json >::value anddetail::is_basic_json< BasicJsonType >::value, int > = 0>
BasicJsonType nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::get ( ) const
inline

get special-case overload

This overloads converts the current basic_json in a different basic_json type

Template Parameters
BasicJsonType== basic_json
Returns
a copy of *this, converted into
Template Parameters
BasicJsonTypeDepending on the implementation of the called from_json() method.
Since
version 3.2.0

Definition at line 17124 of file json.hpp.

17125  {
17126  return *this;
17127  }
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename ValueTypeCV , typename ValueType = detail::uncvref_t<ValueTypeCV>, detail::enable_if_t< not detail::is_basic_json< ValueType >::value anddetail::has_from_json< basic_json_t, ValueType >::value andnot detail::has_non_default_from_json< basic_json_t, ValueType >::value, int > = 0>
ValueType nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::get ( ) const
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

return ret;

This overloads is chosen if:

Template Parameters
ValueTypeCVthe provided value type
ValueTypethe returned value type
Returns
copy of the JSON value, converted to ValueType
Exceptions
whatjson_serializer<ValueType> from_json() method throws

{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}

Since
version 2.1.0

Definition at line 17174 of file json.hpp.

17176  {
17177  // we cannot static_assert on ValueTypeCV being non-const, because
17178  // there is support for get<const basic_json_t>(), which is why we
17179  // still need the uncvref
17180  static_assert(not std::is_reference<ValueTypeCV>::value,
17181  "get() cannot be used with reference types, you might want to use get_ref()");
17183  "types must be DefaultConstructible when used with get()");
17184 
17185  ValueType ret;
17187  return ret;
17188  }
void from_json(const BasicJsonType &j, typename std::nullptr_t &n)
Definition: json.hpp:2898
temporary value
ValueType
Definition: value.h:9
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename ValueTypeCV , typename ValueType = detail::uncvref_t<ValueTypeCV>, detail::enable_if_t< not std::is_same< basic_json_t, ValueType >::value anddetail::has_non_default_from_json< basic_json_t, ValueType >::value, int > = 0>
ValueType nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::get ( ) 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:

Note
If json_serializer<ValueType> has both overloads of from_json(), this one is chosen.
Template Parameters
ValueTypeCVthe provided value type
ValueTypethe returned value type
Returns
copy of the JSON value, converted to ValueType
Exceptions
whatjson_serializer<ValueType> from_json() method throws
Since
version 2.1.0

Definition at line 17225 of file json.hpp.

17227  {
17228  static_assert(not std::is_reference<ValueTypeCV>::value,
17229  "get() cannot be used with reference types, you might want to use get_ref()");
17231  }
void from_json(const BasicJsonType &j, typename std::nullptr_t &n)
Definition: json.hpp:2898
temporary value
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename PointerType , typename std::enable_if< std::is_pointer< PointerType >::value, int >::type = 0>
auto nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::get ( ) -> decltype(std::declval<basic_json_t&>().template get_ptr<PointerType>())
inlinenoexcept

get a pointer value (explicit)

Explicit pointer access to the internally stored JSON value. No copies are made.

Warning
The pointer becomes invalid if the underlying JSON object changes.
Template Parameters
PointerTypepointer type; must be a pointer to array_t, object_t, string_t, boolean_t, number_integer_t, number_unsigned_t, or number_float_t.
Returns
pointer to the internally stored JSON value if the requested pointer type PointerType fits to the JSON value; nullptr otherwise

Constant.

{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}

See Also
get_ptr() for explicit pointer-member access
Since
version 1.0.0

Definition at line 17368 of file json.hpp.

17369  {
17370  // delegate the call to get_ptr
17371  return get_ptr<PointerType>();
17372  }
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename PointerType , typename std::enable_if< std::is_pointer< PointerType >::value, int >::type = 0>
constexpr auto nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::get ( ) const -> decltype(std::declval<const basic_json_t&>().template get_ptr<PointerType>())
inlinenoexcept

get a pointer value (explicit)

get a pointer value (explicit) Explicit pointer access to the internally stored JSON value. No copies are made.

Warning
The pointer becomes invalid if the underlying JSON object changes.
Template Parameters
PointerTypepointer type; must be a pointer to array_t, object_t, string_t, boolean_t, number_integer_t, number_unsigned_t, or number_float_t.
Returns
pointer to the internally stored JSON value if the requested pointer type PointerType fits to the JSON value; nullptr otherwise

Constant.

{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}

See Also
get_ptr() for explicit pointer-member access
Since
version 1.0.0

Definition at line 17380 of file json.hpp.

17381  {
17382  // delegate the call to get_ptr
17383  return get_ptr<PointerType>();
17384  }
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
static allocator_type nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::get_allocator ( )
inlinestatic

returns the allocator associated with the container

Definition at line 14834 of file json.hpp.

14835  {
14836  return allocator_type();
14837  }
AllocatorType< basic_json > allocator_type
the allocator type
Definition: json.hpp:14812
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
boolean_t nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::get_impl ( boolean_t ) const
inlineprivate

get a boolean (explicit)

Definition at line 16960 of file json.hpp.

16961  {
16963  {
16964  return m_value.boolean;
16965  }
16966 
16967  JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(type_name())));
16968  }
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
boolean_t boolean
boolean
Definition: json.hpp:15423
#define JSON_THROW(exception)
Definition: json.hpp:1754
constexpr bool is_boolean() const noexcept
return whether value is a boolean
Definition: json.hpp:16713
#define JSON_HEDLEY_LIKELY(expr)
Definition: json.hpp:1123
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
object_t* nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::get_impl_ptr ( object_t )
inlineprivatenoexcept

get a pointer to the value (object)

Definition at line 16971 of file json.hpp.

16972  {
16973  return is_object() ? m_value.object : nullptr;
16974  }
object_t * object
object (stored with pointer to save storage)
Definition: json.hpp:15417
constexpr bool is_object() const noexcept
return whether value is an object
Definition: json.hpp:16850
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
constexpr const object_t* nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::get_impl_ptr ( const object_t ) const
inlineprivatenoexcept

get a pointer to the value (object)

Definition at line 16977 of file json.hpp.

16978  {
16979  return is_object() ? m_value.object : nullptr;
16980  }
object_t * object
object (stored with pointer to save storage)
Definition: json.hpp:15417
constexpr bool is_object() const noexcept
return whether value is an object
Definition: json.hpp:16850
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
array_t* nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::get_impl_ptr ( array_t )
inlineprivatenoexcept

get a pointer to the value (array)

Definition at line 16983 of file json.hpp.

16984  {
16985  return is_array() ? m_value.array : nullptr;
16986  }
array_t * array
array (stored with pointer to save storage)
Definition: json.hpp:15419
constexpr bool is_array() const noexcept
return whether value is an array
Definition: json.hpp:16872
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
constexpr const array_t* nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::get_impl_ptr ( const array_t ) const
inlineprivatenoexcept

get a pointer to the value (array)

Definition at line 16989 of file json.hpp.

16990  {
16991  return is_array() ? m_value.array : nullptr;
16992  }
array_t * array
array (stored with pointer to save storage)
Definition: json.hpp:15419
constexpr bool is_array() const noexcept
return whether value is an array
Definition: json.hpp:16872
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
string_t* nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::get_impl_ptr ( string_t )
inlineprivatenoexcept

get a pointer to the value (string)

Definition at line 16995 of file json.hpp.

16996  {
16997  return is_string() ? m_value.string : nullptr;
16998  }
string_t * string
string (stored with pointer to save storage)
Definition: json.hpp:15421
constexpr bool is_string() const noexcept
return whether value is a string
Definition: json.hpp:16894
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
constexpr const string_t* nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::get_impl_ptr ( const string_t ) const
inlineprivatenoexcept

get a pointer to the value (string)

Definition at line 17001 of file json.hpp.

17002  {
17003  return is_string() ? m_value.string : nullptr;
17004  }
string_t * string
string (stored with pointer to save storage)
Definition: json.hpp:15421
constexpr bool is_string() const noexcept
return whether value is a string
Definition: json.hpp:16894
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
boolean_t* nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::get_impl_ptr ( boolean_t )
inlineprivatenoexcept

get a pointer to the value (boolean)

Definition at line 17007 of file json.hpp.

17008  {
17009  return is_boolean() ? &m_value.boolean : nullptr;
17010  }
boolean_t boolean
boolean
Definition: json.hpp:15423
constexpr bool is_boolean() const noexcept
return whether value is a boolean
Definition: json.hpp:16713
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
constexpr const boolean_t* nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::get_impl_ptr ( const boolean_t ) const
inlineprivatenoexcept

get a pointer to the value (boolean)

Definition at line 17013 of file json.hpp.

17014  {
17015  return is_boolean() ? &m_value.boolean : nullptr;
17016  }
boolean_t boolean
boolean
Definition: json.hpp:15423
constexpr bool is_boolean() const noexcept
return whether value is a boolean
Definition: json.hpp:16713
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
number_integer_t* nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::get_impl_ptr ( number_integer_t )
inlineprivatenoexcept

get a pointer to the value (integer number)

Definition at line 17019 of file json.hpp.

17020  {
17021  return is_number_integer() ? &m_value.number_integer : nullptr;
17022  }
constexpr bool is_number_integer() const noexcept
return whether value is an integer number
Definition: json.hpp:16772
number_integer_t number_integer
number (integer)
Definition: json.hpp:15425
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
constexpr const number_integer_t* nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::get_impl_ptr ( const number_integer_t ) const
inlineprivatenoexcept

get a pointer to the value (integer number)

Definition at line 17025 of file json.hpp.

17026  {
17027  return is_number_integer() ? &m_value.number_integer : nullptr;
17028  }
constexpr bool is_number_integer() const noexcept
return whether value is an integer number
Definition: json.hpp:16772
number_integer_t number_integer
number (integer)
Definition: json.hpp:15425
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
number_unsigned_t* nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::get_impl_ptr ( number_unsigned_t )
inlineprivatenoexcept

get a pointer to the value (unsigned number)

Definition at line 17031 of file json.hpp.

17032  {
17033  return is_number_unsigned() ? &m_value.number_unsigned : nullptr;
17034  }
number_unsigned_t number_unsigned
number (unsigned integer)
Definition: json.hpp:15427
constexpr bool is_number_unsigned() const noexcept
return whether value is an unsigned integer number
Definition: json.hpp:16800
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
constexpr const number_unsigned_t* nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::get_impl_ptr ( const number_unsigned_t ) const
inlineprivatenoexcept

get a pointer to the value (unsigned number)

Definition at line 17037 of file json.hpp.

17038  {
17039  return is_number_unsigned() ? &m_value.number_unsigned : nullptr;
17040  }
number_unsigned_t number_unsigned
number (unsigned integer)
Definition: json.hpp:15427
constexpr bool is_number_unsigned() const noexcept
return whether value is an unsigned integer number
Definition: json.hpp:16800
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
number_float_t* nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::get_impl_ptr ( number_float_t )
inlineprivatenoexcept

get a pointer to the value (floating-point number)

Definition at line 17043 of file json.hpp.

17044  {
17045  return is_number_float() ? &m_value.number_float : nullptr;
17046  }
number_float_t number_float
number (floating-point)
Definition: json.hpp:15429
constexpr bool is_number_float() const noexcept
return whether value is a floating-point number
Definition: json.hpp:16828
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
constexpr const number_float_t* nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::get_impl_ptr ( const number_float_t ) const
inlineprivatenoexcept

get a pointer to the value (floating-point number)

Definition at line 17049 of file json.hpp.

17050  {
17051  return is_number_float() ? &m_value.number_float : nullptr;
17052  }
number_float_t number_float
number (floating-point)
Definition: json.hpp:15429
constexpr bool is_number_float() const noexcept
return whether value is a floating-point number
Definition: json.hpp:16828
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename PointerType , typename std::enable_if< std::is_pointer< PointerType >::value, int >::type = 0>
auto nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::get_ptr ( ) -> decltype(std::declval<basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
inlinenoexcept

get a pointer value (implicit)

Implicit pointer access to the internally stored JSON value. No copies are made.

Warning
Writing data to the pointee of the result yields an undefined state.
Template Parameters
PointerTypepointer type; must be a pointer to array_t, object_t, string_t, boolean_t, number_integer_t, number_unsigned_t, or number_float_t. Enforced by a static assertion.
Returns
pointer to the internally stored JSON value if the requested pointer type PointerType fits to the JSON value; nullptr otherwise

Constant.

{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}

Since
version 1.0.0

Definition at line 17320 of file json.hpp.

17321  {
17322  // delegate the call to get_impl_ptr<>()
17323  return get_impl_ptr(static_cast<PointerType>(nullptr));
17324  }
object_t * get_impl_ptr(object_t *) noexcept
get a pointer to the value (object)
Definition: json.hpp:16971
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename PointerType , typename std::enable_if< std::is_pointer< PointerType >::value andstd::is_const< typename std::remove_pointer< PointerType >::type >::value, int >::type = 0>
constexpr auto nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::get_ptr ( ) const -> decltype(std::declval<const basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
inlinenoexcept

get a pointer value (implicit)

get a pointer value (implicit) Implicit pointer access to the internally stored JSON value. No copies are made.

Warning
Writing data to the pointee of the result yields an undefined state.
Template Parameters
PointerTypepointer type; must be a pointer to array_t, object_t, string_t, boolean_t, number_integer_t, number_unsigned_t, or number_float_t. Enforced by a static assertion.
Returns
pointer to the internally stored JSON value if the requested pointer type PointerType fits to the JSON value; nullptr otherwise

Constant.

{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}

Since
version 1.0.0

Definition at line 17333 of file json.hpp.

17334  {
17335  // delegate the call to get_impl_ptr<>() const
17336  return get_impl_ptr(static_cast<PointerType>(nullptr));
17337  }
object_t * get_impl_ptr(object_t *) noexcept
get a pointer to the value (object)
Definition: json.hpp:16971
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename ReferenceType , typename std::enable_if< std::is_reference< ReferenceType >::value, int >::type = 0>
ReferenceType nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::get_ref ( )
inline

get a reference value (implicit)

Implicit reference access to the internally stored JSON value. No copies are made.

Warning
Writing data to the referee of the result yields an undefined state.
Template Parameters
ReferenceTypereference type; must be a reference to array_t, object_t, string_t, boolean_t, number_integer_t, or number_float_t. Enforced by static assertion.
Returns
reference to the internally stored JSON value if the requested reference type ReferenceType fits to the JSON value; throws type_error.303 otherwise
Exceptions
type_error.303in case passed type ReferenceType is incompatible with the stored JSON value; see example below

Constant.

{The example shows several calls to get_ref().,get_ref}

Since
version 1.1.0

Definition at line 17414 of file json.hpp.

17415  {
17416  // delegate call to get_ref_impl
17417  return get_ref_impl<ReferenceType>(*this);
17418  }
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename ReferenceType , typename std::enable_if< std::is_reference< ReferenceType >::value andstd::is_const< typename std::remove_reference< ReferenceType >::type >::value, int >::type = 0>
ReferenceType nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::get_ref ( ) const
inline

get a reference value (implicit)

get a reference value (implicit) Implicit reference access to the internally stored JSON value. No copies are made.

Warning
Writing data to the referee of the result yields an undefined state.
Template Parameters
ReferenceTypereference type; must be a reference to array_t, object_t, string_t, boolean_t, number_integer_t, or number_float_t. Enforced by static assertion.
Returns
reference to the internally stored JSON value if the requested reference type ReferenceType fits to the JSON value; throws type_error.303 otherwise
Exceptions
type_error.303in case passed type ReferenceType is incompatible with the stored JSON value; see example below

Constant.

{The example shows several calls to get_ref().,get_ref}

Since
version 1.1.0

Definition at line 17427 of file json.hpp.

17428  {
17429  // delegate call to get_ref_impl
17430  return get_ref_impl<ReferenceType>(*this);
17431  }
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename ReferenceType , typename ThisType >
static ReferenceType nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::get_ref_impl ( ThisType &  obj)
inlinestaticprivate

helper function to implement get_ref()

This function helps to implement get_ref() without code duplication for const and non-const overloads

Template Parameters
ThisTypewill be deduced as basic_json or const basic_json
Exceptions
type_error.303if ReferenceType does not match underlying value type of the current JSON

Definition at line 17066 of file json.hpp.

17067  {
17068  // delegate the call to get_ptr<>()
17069  auto ptr = obj.template get_ptr<typename std::add_pointer<ReferenceType>::type>();
17070 
17071  if (JSON_HEDLEY_LIKELY(ptr != nullptr))
17072  {
17073  return *ptr;
17074  }
17075 
17076  JSON_THROW(type_error::create(303, "incompatible ReferenceType for get_ref, actual type is " + std::string(obj.type_name())));
17077  }
#define JSON_THROW(exception)
Definition: json.hpp:1754
#define JSON_HEDLEY_LIKELY(expr)
Definition: json.hpp:1123
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename ValueType , detail::enable_if_t< not detail::is_basic_json< ValueType >::value anddetail::has_from_json< basic_json_t, ValueType >::value, int > = 0>
ValueType& nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::get_to ( ValueType &  v) const
inlinenoexcept

get a value (explicit)

Explicit type conversion between the JSON value and a compatible value. The value is filled into the input parameter by calling the json_serializer<ValueType> from_json() method.

The function is equivalent to executing

This overloads is chosen if:

Template Parameters
ValueTypethe input parameter type.
Returns
the input parameter, allowing chaining calls.
Exceptions
whatjson_serializer<ValueType> from_json() method throws

{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_to}

Since
version 3.3.0

Definition at line 17271 of file json.hpp.

17273  {
17275  return v;
17276  }
void from_json(const BasicJsonType &j, typename std::nullptr_t &n)
Definition: json.hpp:2898
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename T , std::size_t N, typename Array = T (&)[N], detail::enable_if_t< detail::has_from_json< basic_json_t, Array >::value, int > = 0>
Array nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::get_to ( T(&)  v[N]) const
inlinenoexcept

Definition at line 17283 of file json.hpp.

17286  {
17288  return v;
17289  }
void from_json(const BasicJsonType &j, typename std::nullptr_t &n)
Definition: json.hpp:2898
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
iterator nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::insert ( const_iterator  pos,
const basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > &  val 
)
inline

inserts element

Inserts element val before iterator pos.

Parameters
[in]positerator before which the content will be inserted; may be the end() iterator
[in]valelement to insert
Returns
iterator pointing to the inserted val.
Exceptions
type_error.309if called on JSON values other than arrays; example: "cannot use insert() with string"
invalid_iterator.202if pos is not an iterator of *this; example: "iterator does not fit current value"

Constant plus linear in the distance between pos and end of the container.

{The example shows how insert() is used.,insert}

Since
version 1.0.0

Definition at line 19717 of file json.hpp.

19718  {
19719  // insert only works for arrays
19721  {
19722  // check if iterator pos fits to this JSON value
19723  if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
19724  {
19725  JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value"));
19726  }
19727 
19728  // insert to array and return iterator
19729  return insert_iterator(pos, val);
19730  }
19731 
19732  JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name())));
19733  }
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
#define JSON_THROW(exception)
Definition: json.hpp:1754
iterator insert_iterator(const_iterator pos, Args &&...args)
Definition: json.hpp:19679
static invalid_iterator create(int id_, const std::string &what_arg)
Definition: json.hpp:2024
#define JSON_HEDLEY_LIKELY(expr)
Definition: json.hpp:1123
constexpr bool is_array() const noexcept
return whether value is an array
Definition: json.hpp:16872
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: json.hpp:1124
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
iterator nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::insert ( const_iterator  pos,
basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > &&  val 
)
inline

inserts element

inserts element Inserts element val before iterator pos.

Parameters
[in]positerator before which the content will be inserted; may be the end() iterator
[in]valelement to insert
Returns
iterator pointing to the inserted val.
Exceptions
type_error.309if called on JSON values other than arrays; example: "cannot use insert() with string"
invalid_iterator.202if pos is not an iterator of *this; example: "iterator does not fit current value"

Constant plus linear in the distance between pos and end of the container.

{The example shows how insert() is used.,insert}

Since
version 1.0.0

Definition at line 19739 of file json.hpp.

19740  {
19741  return insert(pos, val);
19742  }
iterator insert(const_iterator pos, const basic_json &val)
inserts element
Definition: json.hpp:19717
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
iterator nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::insert ( const_iterator  pos,
size_type  cnt,
const basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > &  val 
)
inline

inserts elements

Inserts cnt copies of val before iterator pos.

Parameters
[in]positerator before which the content will be inserted; may be the end() iterator
[in]cntnumber of copies of val to insert
[in]valelement to insert
Returns
iterator pointing to the first element inserted, or pos if cnt==0
Exceptions
type_error.309if called on JSON values other than arrays; example: "cannot use insert() with string"
invalid_iterator.202if pos is not an iterator of *this; example: "iterator does not fit current value"

Linear in cnt plus linear in the distance between pos and end of the container.

{The example shows how insert() is used.,insert__count}

Since
version 1.0.0

Definition at line 19768 of file json.hpp.

19769  {
19770  // insert only works for arrays
19772  {
19773  // check if iterator pos fits to this JSON value
19774  if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
19775  {
19776  JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value"));
19777  }
19778 
19779  // insert to array and return iterator
19780  return insert_iterator(pos, cnt, val);
19781  }
19782 
19783  JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name())));
19784  }
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
#define JSON_THROW(exception)
Definition: json.hpp:1754
iterator insert_iterator(const_iterator pos, Args &&...args)
Definition: json.hpp:19679
static invalid_iterator create(int id_, const std::string &what_arg)
Definition: json.hpp:2024
#define JSON_HEDLEY_LIKELY(expr)
Definition: json.hpp:1123
constexpr bool is_array() const noexcept
return whether value is an array
Definition: json.hpp:16872
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: json.hpp:1124
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
iterator nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::insert ( const_iterator  pos,
const_iterator  first,
const_iterator  last 
)
inline

inserts elements

Inserts elements from range [first, last) before iterator pos.

Parameters
[in]positerator before which the content will be inserted; may be the end() iterator
[in]firstbegin of the range of elements to insert
[in]lastend of the range of elements to insert
Exceptions
type_error.309if called on JSON values other than arrays; example: "cannot use insert() with string"
invalid_iterator.202if pos is not an iterator of *this; example: "iterator does not fit current value"
invalid_iterator.210if first and last do not belong to the same JSON value; example: "iterators do not fit"
invalid_iterator.211if first or last are iterators into container for which insert is called; example: "passed iterators may not belong to container"
Returns
iterator pointing to the first element inserted, or pos if first==last

Linear in std::distance(first, last) plus linear in the distance between pos and end of the container.

{The example shows how insert() is used.,insert__range}

Since
version 1.0.0

Definition at line 19816 of file json.hpp.

19817  {
19818  // insert only works for arrays
19819  if (JSON_HEDLEY_UNLIKELY(not is_array()))
19820  {
19821  JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name())));
19822  }
19823 
19824  // check if iterator pos fits to this JSON value
19825  if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
19826  {
19827  JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value"));
19828  }
19829 
19830  // check if range iterators belong to the same JSON object
19831  if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
19832  {
19833  JSON_THROW(invalid_iterator::create(210, "iterators do not fit"));
19834  }
19835 
19836  if (JSON_HEDLEY_UNLIKELY(first.m_object == this))
19837  {
19838  JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container"));
19839  }
19840 
19841  // insert to array and return iterator
19842  return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator);
19843  }
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
#define JSON_THROW(exception)
Definition: json.hpp:1754
iterator insert_iterator(const_iterator pos, Args &&...args)
Definition: json.hpp:19679
static invalid_iterator create(int id_, const std::string &what_arg)
Definition: json.hpp:2024
constexpr bool is_array() const noexcept
return whether value is an array
Definition: json.hpp:16872
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: json.hpp:1124
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
iterator nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::insert ( const_iterator  pos,
initializer_list_t  ilist 
)
inline

inserts elements

Inserts elements from initializer list ilist before iterator pos.

Parameters
[in]positerator before which the content will be inserted; may be the end() iterator
[in]ilistinitializer list to insert the values from
Exceptions
type_error.309if called on JSON values other than arrays; example: "cannot use insert() with string"
invalid_iterator.202if pos is not an iterator of *this; example: "iterator does not fit current value"
Returns
iterator pointing to the first element inserted, or pos if ilist is empty

Linear in ilist.size() plus linear in the distance between pos and end of the container.

{The example shows how insert() is used.,insert__ilist}

Since
version 1.0.0

Definition at line 19869 of file json.hpp.

19870  {
19871  // insert only works for arrays
19872  if (JSON_HEDLEY_UNLIKELY(not is_array()))
19873  {
19874  JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name())));
19875  }
19876 
19877  // check if iterator pos fits to this JSON value
19878  if (JSON_HEDLEY_UNLIKELY(pos.m_object != this))
19879  {
19880  JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value"));
19881  }
19882 
19883  // insert to array and return iterator
19884  return insert_iterator(pos, ilist.begin(), ilist.end());
19885  }
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
#define JSON_THROW(exception)
Definition: json.hpp:1754
iterator insert_iterator(const_iterator pos, Args &&...args)
Definition: json.hpp:19679
static invalid_iterator create(int id_, const std::string &what_arg)
Definition: json.hpp:2024
constexpr bool is_array() const noexcept
return whether value is an array
Definition: json.hpp:16872
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: json.hpp:1124
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
void nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::insert ( const_iterator  first,
const_iterator  last 
)
inline

inserts elements

Inserts elements from range [first, last).

Parameters
[in]firstbegin of the range of elements to insert
[in]lastend of the range of elements to insert
Exceptions
type_error.309if called on JSON values other than objects; example: "cannot use insert() with string"
invalid_iterator.202if iterator first or last does does not point to an object; example: "iterators first and last must point to objects"
invalid_iterator.210if first and last do not belong to the same JSON value; example: "iterators do not fit"

Logarithmic: O(N*log(size() + N)), where N is the number of elements to insert.

{The example shows how insert() is used.,insert__range_object}

Since
version 3.0.0

Definition at line 19910 of file json.hpp.

19911  {
19912  // insert only works for objects
19913  if (JSON_HEDLEY_UNLIKELY(not is_object()))
19914  {
19915  JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name())));
19916  }
19917 
19918  // check if range iterators belong to the same JSON object
19919  if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
19920  {
19921  JSON_THROW(invalid_iterator::create(210, "iterators do not fit"));
19922  }
19923 
19924  // passed iterators must belong to objects
19925  if (JSON_HEDLEY_UNLIKELY(not first.m_object->is_object()))
19926  {
19927  JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects"));
19928  }
19929 
19930  m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator);
19931  }
object_t * object
object (stored with pointer to save storage)
Definition: json.hpp:15417
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
constexpr bool is_object() const noexcept
return whether value is an object
Definition: json.hpp:16850
#define JSON_THROW(exception)
Definition: json.hpp:1754
static invalid_iterator create(int id_, const std::string &what_arg)
Definition: json.hpp:2024
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: json.hpp:1124
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename... Args>
iterator nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::insert_iterator ( const_iterator  pos,
Args &&...  args 
)
inline

Helper for insertion of an iterator

Note
: This uses std::distance to support GCC 4.8, see https://github.com/nlohmann/json/pull/1257

Definition at line 19679 of file json.hpp.

19680  {
19681  iterator result(this);
19682  assert(m_value.array != nullptr);
19683 
19684  auto insert_pos = std::distance(m_value.array->begin(), pos.m_it.array_iterator);
19685  m_value.array->insert(pos.m_it.array_iterator, std::forward<Args>(args)...);
19686  result.m_it.array_iterator = m_value.array->begin() + insert_pos;
19687 
19688  // This could have been written as:
19689  // result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, cnt, val);
19690  // but the return value of insert is missing in GCC 4.8, so it is written this way instead.
19691 
19692  return result;
19693  }
iter_impl< basic_json > iterator
an iterator for a basic_json container
Definition: json.hpp:14820
array_t * array
array (stored with pointer to save storage)
Definition: json.hpp:15419
double distance(geo::Point_t const &point, CathodeDesc_t const &cathode)
Returns the distance of a point from the cathode.
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
constexpr bool nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::is_array ( ) const
inlinenoexcept

return whether value is an array

This function returns true if and only if the JSON value is an array.

Returns
true if type is array, false otherwise.

Constant.

No-throw guarantee: this member function never throws exceptions.

{The following code exemplifies is_array() for all JSON types.,is_array}

Since
version 1.0.0

Definition at line 16872 of file json.hpp.

16873  {
16874  return m_type == value_t::array;
16875  }
array (ordered collection of values)
value_t m_type
the type of the current element
Definition: json.hpp:21017
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
constexpr bool nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::is_boolean ( ) const
inlinenoexcept

return whether value is a boolean

This function returns true if and only if the JSON value is a boolean.

Returns
true if type is boolean, false otherwise.

Constant.

No-throw guarantee: this member function never throws exceptions.

{The following code exemplifies is_boolean() for all JSON types.,is_boolean}

Since
version 1.0.0

Definition at line 16713 of file json.hpp.

16714  {
16715  return m_type == value_t::boolean;
16716  }
value_t m_type
the type of the current element
Definition: json.hpp:21017
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
constexpr bool nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::is_discarded ( ) const
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).

Note
This function will always be 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.
Returns
true if type is discarded, false otherwise.

Constant.

No-throw guarantee: this member function never throws exceptions.

{The following code exemplifies is_discarded() for all JSON types.,is_discarded}

Since
version 1.0.0

Definition at line 16921 of file json.hpp.

16922  {
16923  return m_type == value_t::discarded;
16924  }
value_t m_type
the type of the current element
Definition: json.hpp:21017
discarded by the the parser callback function
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
constexpr bool nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::is_null ( ) const
inlinenoexcept

return whether value is null

This function returns true if and only if the JSON value is null.

Returns
true if type is null, false otherwise.

Constant.

No-throw guarantee: this member function never throws exceptions.

{The following code exemplifies is_null() for all JSON types.,is_null}

Since
version 1.0.0

Definition at line 16691 of file json.hpp.

16692  {
16693  return m_type == value_t::null;
16694  }
value_t m_type
the type of the current element
Definition: json.hpp:21017
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
constexpr bool nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::is_number ( ) const
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.

Returns
true if type is number (regardless whether integer, unsigned integer or floating-type), false otherwise.

Constant.

No-throw guarantee: this member function never throws exceptions.

{The following code exemplifies is_number() for all JSON types.,is_number}

See Also
is_number_integer()check if value is an integer or unsigned integer number
is_number_unsigned()check if value is an unsigned integer number
is_number_float()check if value is a floating-point number
Since
version 1.0.0

Definition at line 16743 of file json.hpp.

16744  {
16745  return is_number_integer() or is_number_float();
16746  }
constexpr bool is_number_float() const noexcept
return whether value is a floating-point number
Definition: json.hpp:16828
constexpr bool is_number_integer() const noexcept
return whether value is an integer number
Definition: json.hpp:16772
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
constexpr bool nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::is_number_float ( ) const
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.

Returns
true if type is a floating-point number, false otherwise.

Constant.

No-throw guarantee: this member function never throws exceptions.

{The following code exemplifies is_number_float() for all JSON types.,is_number_float}

See Also
is_number()check if value is number
is_number_integer()check if value is an integer number
is_number_unsigned()check if value is an unsigned integer number
Since
version 1.0.0

Definition at line 16828 of file json.hpp.

16829  {
16830  return m_type == value_t::number_float;
16831  }
value_t m_type
the type of the current element
Definition: json.hpp:21017
number value (floating-point)
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
constexpr bool nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::is_number_integer ( ) const
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.

Returns
true if type is an integer or unsigned integer number, false otherwise.

Constant.

No-throw guarantee: this member function never throws exceptions.

{The following code exemplifies is_number_integer() for all JSON types.,is_number_integer}

See Also
is_number()check if value is a number
is_number_unsigned()check if value is an unsigned integer number
is_number_float()check if value is a floating-point number
Since
version 1.0.0

Definition at line 16772 of file json.hpp.

16773  {
16775  }
number value (signed integer)
value_t m_type
the type of the current element
Definition: json.hpp:21017
number value (unsigned integer)
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
constexpr bool nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::is_number_unsigned ( ) const
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.

Returns
true if type is an unsigned integer number, false otherwise.

Constant.

No-throw guarantee: this member function never throws exceptions.

{The following code exemplifies is_number_unsigned() for all JSON types.,is_number_unsigned}

See Also
is_number()check if value is a number
is_number_integer()check if value is an integer or unsigned integer number
is_number_float()check if value is a floating-point number
Since
version 2.0.0

Definition at line 16800 of file json.hpp.

16801  {
16802  return m_type == value_t::number_unsigned;
16803  }
value_t m_type
the type of the current element
Definition: json.hpp:21017
number value (unsigned integer)
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
constexpr bool nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::is_object ( ) const
inlinenoexcept

return whether value is an object

This function returns true if and only if the JSON value is an object.

Returns
true if type is object, false otherwise.

Constant.

No-throw guarantee: this member function never throws exceptions.

{The following code exemplifies is_object() for all JSON types.,is_object}

Since
version 1.0.0

Definition at line 16850 of file json.hpp.

16851  {
16852  return m_type == value_t::object;
16853  }
object (unordered set of name/value pairs)
value_t m_type
the type of the current element
Definition: json.hpp:21017
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
constexpr bool nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::is_primitive ( ) const
inlinenoexcept

return whether type is primitive

This function returns true if and only if the JSON type is primitive (string, number, boolean, or null).

Returns
true if type is primitive (string, number, boolean, or null), false otherwise.

Constant.

No-throw guarantee: this member function never throws exceptions.

{The following code exemplifies is_primitive() for all JSON types.,is_primitive}

See Also
is_structured() – returns whether JSON value is structured
is_null() – returns whether JSON value is null
is_string() – returns whether JSON value is a string
is_boolean() – returns whether JSON value is a boolean
is_number() – returns whether JSON value is a number
Since
version 1.0.0

Definition at line 16642 of file json.hpp.

16643  {
16644  return is_null() or is_string() or is_boolean() or is_number();
16645  }
constexpr bool is_null() const noexcept
return whether value is null
Definition: json.hpp:16691
constexpr bool is_number() const noexcept
return whether value is a number
Definition: json.hpp:16743
constexpr bool is_boolean() const noexcept
return whether value is a boolean
Definition: json.hpp:16713
constexpr bool is_string() const noexcept
return whether value is a string
Definition: json.hpp:16894
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
constexpr bool nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::is_string ( ) const
inlinenoexcept

return whether value is a string

This function returns true if and only if the JSON value is a string.

Returns
true if type is string, false otherwise.

Constant.

No-throw guarantee: this member function never throws exceptions.

{The following code exemplifies is_string() for all JSON types.,is_string}

Since
version 1.0.0

Definition at line 16894 of file json.hpp.

16895  {
16896  return m_type == value_t::string;
16897  }
value_t m_type
the type of the current element
Definition: json.hpp:21017
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
constexpr bool nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::is_structured ( ) const
inlinenoexcept

return whether type is structured

This function returns true if and only if the JSON type is structured (array or object).

Returns
true if type is structured (array or object), false otherwise.

Constant.

No-throw guarantee: this member function never throws exceptions.

{The following code exemplifies is_structured() for all JSON types.,is_structured}

See Also
is_primitive() – returns whether value is primitive
is_array() – returns whether value is an array
is_object() – returns whether value is an object
Since
version 1.0.0

Definition at line 16669 of file json.hpp.

16670  {
16671  return is_array() or is_object();
16672  }
constexpr bool is_object() const noexcept
return whether value is an object
Definition: json.hpp:16850
constexpr bool is_array() const noexcept
return whether value is an array
Definition: json.hpp:16872
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
iteration_proxy<iterator> nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::items ( )
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:

for (auto it = j_object.begin(); it != j_object.end(); ++it)
{
std::cout << "key: " << it.key() << ", value:" << it.value() << '\n';
}

Range-based for loop without items() function:

for (auto it : j_object)
{
// "it" is of type json::reference and has no key() member
std::cout << "value: " << it << '\n';
}

Range-based for loop with items() function:

for (auto& el : j_object.items())
{
std::cout << "key: " << el.key() << ", value:" << el.value() << '\n';
}

The items() function also allows to use structured bindings (C++17):

for (auto& [key, val] : j_object.items())
{
std::cout << "key: " << key << ", value:" << val << '\n';
}
Note
When iterating over an array, key() will return the index of the element as string (see example). For primitive types (e.g., numbers), key() returns an empty string.
Returns
iteration proxy object wrapping ref with an interface to use in range-based for loops

{The following code shows how the function is used.,items}

Strong guarantee: if an exception is thrown, there are no changes in the JSON value.

Constant.

Since
version 3.1.0, structured bindings support since 3.5.0.

Definition at line 19059 of file json.hpp.

19060  {
19061  return iteration_proxy<iterator>(*this);
19062  }
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
iteration_proxy<const_iterator> nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::items ( ) const
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:

for (auto it = j_object.begin(); it != j_object.end(); ++it)
{
std::cout << "key: " << it.key() << ", value:" << it.value() << '\n';
}

Range-based for loop without items() function:

for (auto it : j_object)
{
// "it" is of type json::reference and has no key() member
std::cout << "value: " << it << '\n';
}

Range-based for loop with items() function:

for (auto& el : j_object.items())
{
std::cout << "key: " << el.key() << ", value:" << el.value() << '\n';
}

The items() function also allows to use structured bindings (C++17):

for (auto& [key, val] : j_object.items())
{
std::cout << "key: " << key << ", value:" << val << '\n';
}
Note
When iterating over an array, key() will return the index of the element as string (see example). For primitive types (e.g., numbers), key() returns an empty string.
Returns
iteration proxy object wrapping ref with an interface to use in range-based for loops

{The following code shows how the function is used.,items}

Strong guarantee: if an exception is thrown, there are no changes in the JSON value.

Constant.

Since
version 3.1.0, structured bindings support since 3.5.0.

Definition at line 19067 of file json.hpp.

19068  {
19069  return iteration_proxy<const_iterator>(*this);
19070  }
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
static iteration_proxy<iterator> nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::iterator_wrapper ( reference  ref)
inlinestaticnoexcept

wrapper 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 iterator_wrapper:

for (auto it = j_object.begin(); it != j_object.end(); ++it)
{
std::cout << "key: " << it.key() << ", value:" << it.value() << '\n';
}

Range-based for loop without iterator proxy:

for (auto it : j_object)
{
// "it" is of type json::reference and has no key() member
std::cout << "value: " << it << '\n';
}

Range-based for loop with iterator proxy:

for (auto it : json::iterator_wrapper(j_object))
{
std::cout << "key: " << it.key() << ", value:" << it.value() << '\n';
}
Note
When iterating over an array, key() will return the index of the element as string (see example).
Parameters
[in]refreference to a JSON value
Returns
iteration proxy object wrapping ref with an interface to use in range-based for loops

{The following code shows how the wrapper is used,iterator_wrapper}

Strong guarantee: if an exception is thrown, there are no changes in the JSON value.

Constant.

Note
The name of this function is not yet final and may change in the future.
Deprecated:
This stream operator is deprecated and will be removed in future 4.0.0 of the library. Please use items() instead; that is, replace json::iterator_wrapper(j) with j.items().

Definition at line 18982 of file json.hpp.

18983  {
18984  return ref.items();
18985  }
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
static iteration_proxy<const_iterator> nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::iterator_wrapper ( const_reference  ref)
inlinestaticnoexcept

wrapper 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 iterator_wrapper:

for (auto it = j_object.begin(); it != j_object.end(); ++it)
{
std::cout << "key: " << it.key() << ", value:" << it.value() << '\n';
}

Range-based for loop without iterator proxy:

for (auto it : j_object)
{
// "it" is of type json::reference and has no key() member
std::cout << "value: " << it << '\n';
}

Range-based for loop with iterator proxy:

for (auto it : json::iterator_wrapper(j_object))
{
std::cout << "key: " << it.key() << ", value:" << it.value() << '\n';
}
Note
When iterating over an array, key() will return the index of the element as string (see example).
Parameters
[in]refreference to a JSON value
Returns
iteration proxy object wrapping ref with an interface to use in range-based for loops

{The following code shows how the wrapper is used,iterator_wrapper}

Strong guarantee: if an exception is thrown, there are no changes in the JSON value.

Constant.

Note
The name of this function is not yet final and may change in the future.
Deprecated:
This stream operator is deprecated and will be removed in future 4.0.0 of the library. Please use items() instead; that is, replace json::iterator_wrapper(j) with j.items().

Definition at line 18991 of file json.hpp.

18992  {
18993  return ref.items();
18994  }
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
size_type nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::max_size ( ) const
inlinenoexcept

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.

Returns
The return value depends on the different types and is defined as follows:
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()
{The following code calls max_size() on the different value types. Note the output is implementation specific.,max_size}

Constant, as long as array_t and object_t satisfy the Container concept; that is, their max_size() functions have constant complexity.

No changes.

No-throw guarantee: this function never throws exceptions.

This function helps basic_json satisfying the Container requirements:

  • The complexity is constant.
  • Has the semantics of returning b.size() where b is the largest possible JSON value.
See Also
size() – returns the number of elements
Since
version 1.0.0

Definition at line 19265 of file json.hpp.

19266  {
19267  switch (m_type)
19268  {
19269  case value_t::array:
19270  {
19271  // delegate call to array_t::max_size()
19272  return m_value.array->max_size();
19273  }
19274 
19275  case value_t::object:
19276  {
19277  // delegate call to object_t::max_size()
19278  return m_value.object->max_size();
19279  }
19280 
19281  default:
19282  {
19283  // all other types have max_size() == size()
19284  return size();
19285  }
19286  }
19287  }
object_t * object
object (stored with pointer to save storage)
Definition: json.hpp:15417
array_t * array
array (stored with pointer to save storage)
Definition: json.hpp:15419
array (ordered collection of values)
size_type size() const noexcept
returns the number of elements
Definition: json.hpp:19195
object (unordered set of name/value pairs)
value_t m_type
the type of the current element
Definition: json.hpp:21017
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
void nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::merge_patch ( const basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > &  apply_patch)
inline

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):

``` define MergePatch(Target, Patch): if Patch is an Object: if Target is not an Object: Target = {} // Ignore the contents and set it to an empty Object for each Name/Value pair in Patch: if Value is null: if Name exists in Target: remove the Name/Value pair from Target else: Target[Name] = MergePatch(Target[Name], Value) return Target else: return Patch ```

Thereby, Target is the current object; that is, the patch is applied to the current value.

Parameters
[in]apply_patchthe patch to apply

Linear in the lengths of patch.

{The following code shows how a JSON Merge Patch is applied to a JSON document.,merge_patch}

See Also
patch – apply a JSON patch
RFC 7396 (JSON Merge Patch)
Since
version 3.0.0

Definition at line 22581 of file json.hpp.

22582  {
22583  if (apply_patch.is_object())
22584  {
22585  if (not is_object())
22586  {
22587  *this = object();
22588  }
22589  for (auto it = apply_patch.begin(); it != apply_patch.end(); ++it)
22590  {
22591  if (it.value().is_null())
22592  {
22593  erase(it.key());
22594  }
22595  else
22596  {
22597  operator[](it.key()).merge_patch(it.value());
22598  }
22599  }
22600  }
22601  else
22602  {
22603  *this = apply_patch;
22604  }
22605  }
IteratorType erase(IteratorType pos)
remove element given an iterator
Definition: json.hpp:18240
void merge_patch(const basic_json &apply_patch)
applies a JSON Merge Patch
Definition: json.hpp:22581
constexpr bool is_object() const noexcept
return whether value is an object
Definition: json.hpp:16850
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json object(initializer_list_t init={})
explicitly create an object from an initializer list
Definition: json.hpp:16133
reference operator[](size_type idx)
access specified array element
Definition: json.hpp:17714
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::meta ( )
inlinestatic

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.

Returns
JSON object holding version information
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).
{The following code shows an example output of the meta() function.,meta}

Strong guarantee: if an exception is thrown, there are no changes to any JSON value.

Constant.

Since
2.1.0

Definition at line 14866 of file json.hpp.

14867  {
14868  basic_json result;
14869 
14870  result["copyright"] = "(C) 2013-2017 Niels Lohmann";
14871  result["name"] = "JSON for Modern C++";
14872  result["url"] = "https://github.com/nlohmann/json";
14873  result["version"]["string"] =
14877  result["version"]["major"] = NLOHMANN_JSON_VERSION_MAJOR;
14878  result["version"]["minor"] = NLOHMANN_JSON_VERSION_MINOR;
14879  result["version"]["patch"] = NLOHMANN_JSON_VERSION_PATCH;
14880 
14881 #ifdef _WIN32
14882  result["platform"] = "win32";
14883 #elif defined __linux__
14884  result["platform"] = "linux";
14885 #elif defined __APPLE__
14886  result["platform"] = "apple";
14887 #elif defined __unix__
14888  result["platform"] = "unix";
14889 #else
14890  result["platform"] = "unknown";
14891 #endif
14892 
14893 #if defined(__ICC) || defined(__INTEL_COMPILER)
14894  result["compiler"] = {{"family", "icc"}, {"version", __INTEL_COMPILER}};
14895 #elif defined(__clang__)
14896  result["compiler"] = {{"family", "clang"}, {"version", __clang_version__}};
14897 #elif defined(__GNUC__) || defined(__GNUG__)
14898  result["compiler"] = {{"family", "gcc"}, {"version", std::to_string(__GNUC__) + "." + std::to_string(__GNUC_MINOR__) + "." + std::to_string(__GNUC_PATCHLEVEL__)}};
14899 #elif defined(__HP_cc) || defined(__HP_aCC)
14900  result["compiler"] = "hp"
14901 #elif defined(__IBMCPP__)
14902  result["compiler"] = {{"family", "ilecpp"}, {"version", __IBMCPP__}};
14903 #elif defined(_MSC_VER)
14904  result["compiler"] = {{"family", "msvc"}, {"version", _MSC_VER}};
14905 #elif defined(__PGI)
14906  result["compiler"] = {{"family", "pgcpp"}, {"version", __PGI}};
14907 #elif defined(__SUNPRO_CC)
14908  result["compiler"] = {{"family", "sunpro"}, {"version", __SUNPRO_CC}};
14909 #else
14910  result["compiler"] = {{"family", "unknown"}, {"version", "unknown"}};
14911 #endif
14912 
14913 #ifdef __cplusplus
14914  result["compiler"]["c++"] = std::to_string(__cplusplus);
14915 #else
14916  result["compiler"]["c++"] = "unknown";
14917 #endif
14918  return result;
14919  }
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
#define NLOHMANN_JSON_VERSION_PATCH
Definition: json.hpp:35
#define NLOHMANN_JSON_VERSION_MINOR
Definition: json.hpp:34
std::string to_string(WindowPattern const &pattern)
#define NLOHMANN_JSON_VERSION_MAJOR
Definition: json.hpp:33
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::object ( initializer_list_t  init = {})
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.

Note
This function is only added for symmetry reasons. In contrast to the related function array(initializer_list_t), there are no cases which can only be expressed by this function. That is, any initializer list init can also be passed to the initializer list constructor basic_json(initializer_list_t, bool, value_t).
Parameters
[in]initinitializer list to create an object from (optional)
Returns
JSON object value
Exceptions
type_error.301if 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 basic_json(initializer_list_t, bool, value_t), an array would have been created from the passed initializer list init. See example below.

Linear in the size of init.

Strong guarantee: if an exception is thrown, there are no changes to any JSON value.

{The following code shows an example for the object function.,object}

See Also
basic_json(initializer_list_t, bool, value_t)create a JSON value from an initializer list
array(initializer_list_t)create a JSON array value from an initializer list
Since
version 1.0.0

Definition at line 16133 of file json.hpp.

16133  {})
16134  {
16135  return basic_json(init, false, value_t::object);
16136  }
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
object (unordered set of name/value pairs)
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
constexpr nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::operator value_t ( ) const
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.

Returns
the type of the JSON value

Constant.

No-throw guarantee: this member function never throws exceptions.

{The following code exemplifies the value_t operator for all JSON types.,operator__value_t}

See Also
type() – return the type of the JSON value (explicit)
type_name() – return the type as string
Since
version 1.0.0

Definition at line 16947 of file json.hpp.

16948  {
16949  return m_type;
16950  }
value_t m_type
the type of the current element
Definition: json.hpp:21017
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename ValueType , typename std::enable_if< not std::is_pointer< ValueType >::value andnot std::is_same< ValueType, detail::json_ref< basic_json >>::value andnot std::is_same< ValueType, typename string_t::value_type >::value andnot detail::is_basic_json< ValueType >::valueand not std::is_same< ValueType, std::initializer_list< typename string_t::value_type >>::valueand not std::is_same< ValueType, typename std::string_view >::valueand detail::is_detected< detail::get_template_function, const basic_json_t &, ValueType >::value, int >::type = 0>
nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::operator ValueType ( ) const
inline

get a value (implicit)

Implicit type conversion between the JSON value and a compatible value. The call is realized by calling get() const.

Template Parameters
ValueTypenon-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 string_t as well as an initializer list of this type is excluded to avoid ambiguities as these types implicitly convert to std::string.
Returns
copy of the JSON value, converted to type ValueType
Exceptions
type_error.302in 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

Linear in the size of the JSON value.

{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}

Since
version 1.0.0

Definition at line 17476 of file json.hpp.

17477  {
17478  // delegate the call to get<>() const
17479  return get<ValueType>();
17480  }
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
reference nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::operator+= ( basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > &&  val)
inline

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.

Parameters
[in]valthe value to add to the JSON array
Exceptions
type_error.308when called on a type other than JSON array or null; example: "cannot use push_back() with number"

Amortized constant.

{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}

Since
version 1.0.0

Definition at line 19433 of file json.hpp.

19434  {
19435  push_back(std::move(val));
19436  return *this;
19437  }
void push_back(basic_json &&val)
add an object to an array
Definition: json.hpp:19406
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
reference nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::operator+= ( const basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > &  val)
inline

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.

Parameters
[in]valthe value to add to the JSON array
Exceptions
type_error.308when called on a type other than JSON array or null; example: "cannot use push_back() with number"

Amortized constant.

{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}

Since
version 1.0.0

Definition at line 19467 of file json.hpp.

19468  {
19469  push_back(val);
19470  return *this;
19471  }
void push_back(basic_json &&val)
add an object to an array
Definition: json.hpp:19406
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
reference nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::operator+= ( const typename object_t::value_type &  val)
inline

add an object to an object

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.

Parameters
[in]valthe value to add to the JSON object
Exceptions
type_error.308when called on a type other than JSON object or null; example: "cannot use push_back() with number"

Logarithmic in the size of the container, O(log(size())).

{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}

Since
version 1.0.0

Definition at line 19517 of file json.hpp.

19518  {
19519  push_back(val);
19520  return *this;
19521  }
void push_back(basic_json &&val)
add an object to an array
Definition: json.hpp:19406
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
reference nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::operator+= ( initializer_list_t  init)
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

  1. the current value is an object,
  2. the initializer list init contains only two elements, and
  3. the first element of init is a string,

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(basic_json&&).

Parameters
[in]initan initializer list

Linear in the size of the initializer list init.

Note
This function is required to resolve an ambiguous overload error, because pairs like {"key", "value"} can be both interpreted as object_t::value_type or std::initializer_list<basic_json>, see https://github.com/nlohmann/json/issues/235 for more information.

{The example shows how initializer lists are treated as objects when possible.,push_back__initializer_list}

Definition at line 19566 of file json.hpp.

19567  {
19568  push_back(init);
19569  return *this;
19570  }
void push_back(basic_json &&val)
add an object to an array
Definition: json.hpp:19406
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
basic_json& nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::operator= ( basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >  other)
inlinenoexcept

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.

Parameters
[in]othervalue to copy from

Linear.

This function helps basic_json satisfying the Container requirements:

  • The complexity is linear.

{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.,basic_json__copyassignment}

Since
version 1.0.0

Definition at line 16469 of file json.hpp.

16475  {
16476  // check that passed value is valid
16477  other.assert_invariant();
16478 
16479  using std::swap;
16480  swap(m_type, other.m_type);
16481  swap(m_value, other.m_value);
16482 
16483  assert_invariant();
16484  return *this;
16485  }
void swap(reference other) noexcept(std::is_nothrow_move_constructible< value_t >::value andstd::is_nothrow_move_assignable< value_t >::value andstd::is_nothrow_move_constructible< json_value >::value andstd::is_nothrow_move_assignable< json_value >::value)
exchanges the values
Definition: json.hpp:20054
value_t m_type
the type of the current element
Definition: json.hpp:21017
void assert_invariant() const noexcept
checks the class invariants
Definition: json.hpp:15634
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
reference nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::operator[] ( size_type  idx)
inline

access specified array element

Returns a reference to the element at specified location idx.

Note
If idx is beyond the range of the array (i.e., idx >= size()), then the array is silently filled up with null values to make idx a valid reference to the last stored element.
Parameters
[in]idxindex of the element to access
Returns
reference to the element at index idx
Exceptions
type_error.305if the JSON value is not an array or null; in that cases, using the [] operator with an index makes no sense.

Constant if idx is in the range of the array. Otherwise linear in idx - size().

{The example below shows how array elements can be read and written using [] operator. Note the addition of null values.,operatorarray__size_type}

Since
version 1.0.0

Definition at line 17714 of file json.hpp.

17715  {
17716  // implicitly convert null value to an empty array
17717  if (is_null())
17718  {
17720  m_value.array = create<array_t>();
17721  assert_invariant();
17722  }
17723 
17724  // operator[] only works for arrays
17726  {
17727  // fill up array with null values if given idx is outside range
17728  if (idx >= m_value.array->size())
17729  {
17730  m_value.array->insert(m_value.array->end(),
17731  idx - m_value.array->size() + 1,
17732  basic_json());
17733  }
17734 
17735  return m_value.array->operator[](idx);
17736  }
17737 
17738  JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name())));
17739  }
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
array_t * array
array (stored with pointer to save storage)
Definition: json.hpp:15419
array (ordered collection of values)
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
constexpr bool is_null() const noexcept
return whether value is null
Definition: json.hpp:16691
#define JSON_THROW(exception)
Definition: json.hpp:1754
value_t m_type
the type of the current element
Definition: json.hpp:21017
void assert_invariant() const noexcept
checks the class invariants
Definition: json.hpp:15634
#define JSON_HEDLEY_LIKELY(expr)
Definition: json.hpp:1123
constexpr bool is_array() const noexcept
return whether value is an array
Definition: json.hpp:16872
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
const_reference nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::operator[] ( size_type  idx) const
inline

access specified array element

Returns a const reference to the element at specified location idx.

Parameters
[in]idxindex of the element to access
Returns
const reference to the element at index idx
Exceptions
type_error.305if the JSON value is not an array; in that case, using the [] operator with an index makes no sense.

Constant.

{The example below shows how array elements can be read using the [] operator.,operatorarray__size_type_const}

Since
version 1.0.0

Definition at line 17760 of file json.hpp.

17761  {
17762  // const operator[] only works for arrays
17764  {
17765  return m_value.array->operator[](idx);
17766  }
17767 
17768  JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name())));
17769  }
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
array_t * array
array (stored with pointer to save storage)
Definition: json.hpp:15419
#define JSON_THROW(exception)
Definition: json.hpp:1754
#define JSON_HEDLEY_LIKELY(expr)
Definition: json.hpp:1123
constexpr bool is_array() const noexcept
return whether value is an array
Definition: json.hpp:16872
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
reference nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::operator[] ( const typename object_t::key_type &  key)
inline

access specified object element

Returns a reference to the element at with specified key key.

Note
If key is not found in the object, then it is silently added to the object and filled with a null value to make key a valid reference. In case the value was null before, it is converted to an object.
Parameters
[in]keykey of the element to access
Returns
reference to the element at key key
Exceptions
type_error.305if the JSON value is not an object or null; in that cases, using the [] operator with a key makes no sense.

Logarithmic in the size of the container.

{The example below shows how object elements can be read and written using the [] operator.,operatorarray__key_type}

See Also
at(const typename object_t::key_type&) for access by reference with range checking
value() for access by value with a default value
Since
version 1.0.0

Definition at line 17798 of file json.hpp.

17799  {
17800  // implicitly convert null value to an empty object
17801  if (is_null())
17802  {
17804  m_value.object = create<object_t>();
17805  assert_invariant();
17806  }
17807 
17808  // operator[] only works for objects
17810  {
17811  return m_value.object->operator[](key);
17812  }
17813 
17814  JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name())));
17815  }
object_t * object
object (stored with pointer to save storage)
Definition: json.hpp:15417
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
constexpr bool is_object() const noexcept
return whether value is an object
Definition: json.hpp:16850
constexpr bool is_null() const noexcept
return whether value is null
Definition: json.hpp:16691
#define JSON_THROW(exception)
Definition: json.hpp:1754
object (unordered set of name/value pairs)
value_t m_type
the type of the current element
Definition: json.hpp:21017
void assert_invariant() const noexcept
checks the class invariants
Definition: json.hpp:15634
#define JSON_HEDLEY_LIKELY(expr)
Definition: json.hpp:1123
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
const_reference nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::operator[] ( const typename object_t::key_type &  key) const
inline

read-only access specified object element

Returns a const reference to the element at with specified key key. No bounds checking is performed.

Warning
If the element with key key does not exist, the behavior is undefined.
Parameters
[in]keykey of the element to access
Returns
const reference to the element at key key
Precondition
The element with key key must exist. This precondition is enforced with an assertion.
Exceptions
type_error.305if the JSON value is not an object; in that case, using the [] operator with a key makes no sense.

Logarithmic in the size of the container.

{The example below shows how object elements can be read using the [] operator.,operatorarray__key_type_const}

See Also
at(const typename object_t::key_type&) for access by reference with range checking
value() for access by value with a default value
Since
version 1.0.0

Definition at line 17847 of file json.hpp.

17848  {
17849  // const operator[] only works for objects
17851  {
17852  assert(m_value.object->find(key) != m_value.object->end());
17853  return m_value.object->find(key)->second;
17854  }
17855 
17856  JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name())));
17857  }
object_t * object
object (stored with pointer to save storage)
Definition: json.hpp:15417
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
constexpr bool is_object() const noexcept
return whether value is an object
Definition: json.hpp:16850
#define JSON_THROW(exception)
Definition: json.hpp:1754
#define JSON_HEDLEY_LIKELY(expr)
Definition: json.hpp:1123
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename T >
reference nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::operator[] ( T *  key)
inline

access specified object element

Returns a reference to the element at with specified key key.

Note
If key is not found in the object, then it is silently added to the object and filled with a null value to make key a valid reference. In case the value was null before, it is converted to an object.
Parameters
[in]keykey of the element to access
Returns
reference to the element at key key
Exceptions
type_error.305if the JSON value is not an object or null; in that cases, using the [] operator with a key makes no sense.

Logarithmic in the size of the container.

{The example below shows how object elements can be read and written using the [] operator.,operatorarray__key_type}

See Also
at(const typename object_t::key_type&) for access by reference with range checking
value() for access by value with a default value
Since
version 1.1.0

Definition at line 17888 of file json.hpp.

17889  {
17890  // implicitly convert null to object
17891  if (is_null())
17892  {
17895  assert_invariant();
17896  }
17897 
17898  // at only works for objects
17900  {
17901  return m_value.object->operator[](key);
17902  }
17903 
17904  JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name())));
17905  }
object_t * object
object (stored with pointer to save storage)
Definition: json.hpp:15417
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
constexpr bool is_object() const noexcept
return whether value is an object
Definition: json.hpp:16850
constexpr bool is_null() const noexcept
return whether value is null
Definition: json.hpp:16691
#define JSON_THROW(exception)
Definition: json.hpp:1754
object (unordered set of name/value pairs)
value_t m_type
the type of the current element
Definition: json.hpp:21017
void assert_invariant() const noexcept
checks the class invariants
Definition: json.hpp:15634
#define JSON_HEDLEY_LIKELY(expr)
Definition: json.hpp:1123
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename T >
const_reference nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::operator[] ( T *  key) const
inline

read-only access specified object element

Returns a const reference to the element at with specified key key. No bounds checking is performed.

Warning
If the element with key key does not exist, the behavior is undefined.
Parameters
[in]keykey of the element to access
Returns
const reference to the element at key key
Precondition
The element with key key must exist. This precondition is enforced with an assertion.
Exceptions
type_error.305if the JSON value is not an object; in that case, using the [] operator with a key makes no sense.

Logarithmic in the size of the container.

{The example below shows how object elements can be read using the [] operator.,operatorarray__key_type_const}

See Also
at(const typename object_t::key_type&) for access by reference with range checking
value() for access by value with a default value
Since
version 1.1.0

Definition at line 17939 of file json.hpp.

17940  {
17941  // at only works for objects
17943  {
17944  assert(m_value.object->find(key) != m_value.object->end());
17945  return m_value.object->find(key)->second;
17946  }
17947 
17948  JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name())));
17949  }
object_t * object
object (stored with pointer to save storage)
Definition: json.hpp:15417
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
constexpr bool is_object() const noexcept
return whether value is an object
Definition: json.hpp:16850
#define JSON_THROW(exception)
Definition: json.hpp:1754
#define JSON_HEDLEY_LIKELY(expr)
Definition: json.hpp:1123
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
reference nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::operator[] ( const json_pointer ptr)
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:

  • If the JSON pointer points to an object key that does not exist, it is created an filled with a null value before a reference to it is returned.
  • If the JSON pointer points to an array index that does not exist, it is created an filled with a null value before a reference to it is returned. All indices between the current maximum and the given index are also filled with null.
  • The special value - is treated as a synonym for the index past the end.
Parameters
[in]ptra JSON pointer
Returns
reference to the element pointed to by ptr

Constant.

Exceptions
parse_error.106if an array index begins with '0'
parse_error.109if an array index was not a number
out_of_range.404if the JSON pointer can not be resolved

{The behavior is shown in the example.,operatorjson_pointer}

Since
version 2.0.0

Definition at line 21871 of file json.hpp.

21872  {
21873  return ptr.get_unchecked(this);
21874  }
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
const_reference nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::operator[] ( const json_pointer ptr) const
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.

Parameters
[in]ptrJSON pointer to the desired element
Returns
const reference to the element pointed to by ptr

Constant.

Exceptions
parse_error.106if an array index begins with '0'
parse_error.109if an array index was not a number
out_of_range.402if the array index '-' is used
out_of_range.404if the JSON pointer can not be resolved

{The behavior is shown in the example.,operatorjson_pointer_const}

Since
version 2.0.0

Definition at line 21899 of file json.hpp.

21900  {
21901  return ptr.get_unchecked(this);
21902  }
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
static JSON_HEDLEY_WARN_UNUSED_RESULT basic_json nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::parse ( detail::input_adapter &&  i,
const parser_callback_t  cb = nullptr,
const bool  allow_exceptions = true 
)
inlinestatic

deserialize from a compatible input

This function reads from a compatible input. Examples are:

  • an array of 1-byte values
  • strings with character/literal type with size of 1 byte
  • input streams
  • container with contiguous storage of 1-byte values. Compatible container types include std::vector, std::string, std::array, std::valarray, 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.
Precondition
Each element of the container has a size of 1 byte. Violating this precondition yields undefined behavior. This precondition is enforced with a static assertion.
The container storage is contiguous. Violating this precondition yields undefined behavior. This precondition is enforced with an assertion.
Warning
There is no way to enforce all preconditions at compile-time. If the function is called with a noncompliant container and with assertions switched off, the behavior is undefined and will most likely yield segmentation violation.
Parameters
[in]iinput to read from
[in]cba parser callback function of type parser_callback_t which is used to control the deserialization by filtering unwanted values (optional)
[in]allow_exceptionswhether to throw exceptions in case of a parse error (optional, true by default)
Returns
deserialized JSON value; in case of a parse error and allow_exceptions set to false, the return value will be value_t::discarded.
Exceptions
parse_error.101if a parse error occurs; example: ""unexpected end of input; expected string literal""
parse_error.102if to_unicode fails or surrogate error
parse_error.103if to_unicode fails

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.

Note
A UTF-8 byte order mark is silently ignored.

{The example below demonstrates the parse() function reading from an array.,parse__array__parser_callback_t}

{The example below demonstrates the parse() function with and without callback function.,parse__string__parser_callback_t}

{The example below demonstrates the parse() function with and without callback function.,parse__istream__parser_callback_t}

{The example below demonstrates the parse() function reading from a contiguous container.,parse__contiguouscontainer__parser_callback_t}

Since
version 2.0.3 (contiguous containers)

Definition at line 20745 of file json.hpp.

20748  {
20749  basic_json result;
20750  parser(i, cb, allow_exceptions).parse(true, result);
20751  return result;
20752  }
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
::nlohmann::detail::parser< basic_json > parser
Definition: json.hpp:14731
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<class IteratorType , typename std::enable_if< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< IteratorType >::iterator_category >::value, int >::type = 0>
static basic_json nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::parse ( IteratorType  first,
IteratorType  last,
const parser_callback_t  cb = nullptr,
const bool  allow_exceptions = true 
)
inlinestatic

deserialize from an iterator range with contiguous storage

This function reads from an iterator range of a container with contiguous storage of 1-byte values. Compatible container types include std::vector, std::string, std::array, std::valarray, 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.

Precondition
The iterator range is contiguous. Violating this precondition yields undefined behavior. This precondition is enforced with an assertion.
Each element in the range has a size of 1 byte. Violating this precondition yields undefined behavior. This precondition is enforced with a static assertion.
Warning
There is no way to enforce all preconditions at compile-time. If the function is called with noncompliant iterators and with assertions switched off, the behavior is undefined and will most likely yield segmentation violation.
Template Parameters
IteratorTypeiterator of container with contiguous storage
Parameters
[in]firstbegin of the range to parse (included)
[in]lastend of the range to parse (excluded)
[in]cba parser callback function of type parser_callback_t which is used to control the deserialization by filtering unwanted values (optional)
[in]allow_exceptionswhether to throw exceptions in case of a parse error (optional, true by default)
Returns
deserialized JSON value; in case of a parse error and allow_exceptions set to false, the return value will be value_t::discarded.
Exceptions
parse_error.101in case of an unexpected token
parse_error.102if to_unicode fails or surrogate error
parse_error.103if to_unicode fails

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.

Note
A UTF-8 byte order mark is silently ignored.

{The example below demonstrates the parse() function reading from an iterator range.,parse__iteratortype__parser_callback_t}

Since
version 2.0.3

Definition at line 20877 of file json.hpp.

20880  {
20881  basic_json result;
20882  parser(detail::input_adapter(first, last), cb, allow_exceptions).parse(true, result);
20883  return result;
20884  }
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
::nlohmann::detail::parser< basic_json > parser
Definition: json.hpp:14731
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
basic_json nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::patch ( const basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > &  json_patch) const
inline

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.

Parameters
[in]json_patchJSON patch document
Returns
patched document
Note
The application of a patch is atomic: Either all operations succeed and the patched document is returned or an exception is thrown. In any case, the original value is not changed: the patch is applied to a copy of the value.
Exceptions
parse_error.104if the JSON patch does not consist of an array of objects
parse_error.105if the JSON patch is malformed (e.g., mandatory attributes are missing); example: "operation add must have member path"
out_of_range.401if an array index is out of range.
out_of_range.403if a JSON pointer inside the patch could not be resolved successfully in the current JSON value; example: "key baz not found"
out_of_range.405if JSON pointer has no parent ("add", "remove", "move")
other_error.501if "test" operation was unsuccessful

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.

{The following code shows how a JSON patch is applied to a value.,patch}

See Also
diffcreate a JSON patch by comparing two JSON values
RFC 6902 (JSON Patch)
RFC 6901 (JSON Pointer)
Since
version 2.0.0

Definition at line 22110 of file json.hpp.

22111  {
22112  // make a working copy to apply the patch to
22113  basic_json result = *this;
22114 
22115  // the valid JSON Patch operations
22116  enum class patch_operations {add, remove, replace, move, copy, test, invalid};
22117 
22118  const auto get_op = [](const std::string & op)
22119  {
22120  if (op == "add")
22121  {
22122  return patch_operations::add;
22123  }
22124  if (op == "remove")
22125  {
22126  return patch_operations::remove;
22127  }
22128  if (op == "replace")
22129  {
22130  return patch_operations::replace;
22131  }
22132  if (op == "move")
22133  {
22134  return patch_operations::move;
22135  }
22136  if (op == "copy")
22137  {
22138  return patch_operations::copy;
22139  }
22140  if (op == "test")
22141  {
22142  return patch_operations::test;
22143  }
22144 
22145  return patch_operations::invalid;
22146  };
22147 
22148  // wrapper for "add" operation; add value at ptr
22149  const auto operation_add = [&result](json_pointer & ptr, basic_json val)
22150  {
22151  // adding to the root of the target document means replacing it
22152  if (ptr.empty())
22153  {
22154  result = val;
22155  return;
22156  }
22157 
22158  // make sure the top element of the pointer exists
22159  json_pointer top_pointer = ptr.top();
22160  if (top_pointer != ptr)
22161  {
22162  result.at(top_pointer);
22163  }
22164 
22165  // get reference to parent of JSON pointer ptr
22166  const auto last_path = ptr.back();
22167  ptr.pop_back();
22168  basic_json& parent = result[ptr];
22169 
22170  switch (parent.m_type)
22171  {
22172  case value_t::null:
22173  case value_t::object:
22174  {
22175  // use operator[] to add value
22176  parent[last_path] = val;
22177  break;
22178  }
22179 
22180  case value_t::array:
22181  {
22182  if (last_path == "-")
22183  {
22184  // special case: append to back
22185  parent.push_back(val);
22186  }
22187  else
22188  {
22189  const auto idx = json_pointer::array_index(last_path);
22190  if (JSON_HEDLEY_UNLIKELY(static_cast<size_type>(idx) > parent.size()))
22191  {
22192  // avoid undefined behavior
22193  JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range"));
22194  }
22195 
22196  // default case: insert add offset
22197  parent.insert(parent.begin() + static_cast<difference_type>(idx), val);
22198  }
22199  break;
22200  }
22201 
22202  // if there exists a parent it cannot be primitive
22203  default: // LCOV_EXCL_LINE
22204  assert(false); // LCOV_EXCL_LINE
22205  }
22206  };
22207 
22208  // wrapper for "remove" operation; remove value at ptr
22209  const auto operation_remove = [&result](json_pointer & ptr)
22210  {
22211  // get reference to parent of JSON pointer ptr
22212  const auto last_path = ptr.back();
22213  ptr.pop_back();
22214  basic_json& parent = result.at(ptr);
22215 
22216  // remove child
22217  if (parent.is_object())
22218  {
22219  // perform range check
22220  auto it = parent.find(last_path);
22221  if (JSON_HEDLEY_LIKELY(it != parent.end()))
22222  {
22223  parent.erase(it);
22224  }
22225  else
22226  {
22227  JSON_THROW(out_of_range::create(403, "key '" + last_path + "' not found"));
22228  }
22229  }
22230  else if (parent.is_array())
22231  {
22232  // note erase performs range check
22233  parent.erase(static_cast<size_type>(json_pointer::array_index(last_path)));
22234  }
22235  };
22236 
22237  // type check: top level value must be an array
22238  if (JSON_HEDLEY_UNLIKELY(not json_patch.is_array()))
22239  {
22240  JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects"));
22241  }
22242 
22243  // iterate and apply the operations
22244  for (const auto& val : json_patch)
22245  {
22246  // wrapper to get a value for an operation
22247  const auto get_value = [&val](const std::string & op,
22248  const std::string & member,
22249  bool string_type) -> basic_json &
22250  {
22251  // find value
22252  auto it = val.m_value.object->find(member);
22253 
22254  // context-sensitive error message
22255  const auto error_msg = (op == "op") ? "operation" : "operation '" + op + "'";
22256 
22257  // check if desired value is present
22258  if (JSON_HEDLEY_UNLIKELY(it == val.m_value.object->end()))
22259  {
22260  JSON_THROW(parse_error::create(105, 0, error_msg + " must have member '" + member + "'"));
22261  }
22262 
22263  // check if result is of type string
22264  if (JSON_HEDLEY_UNLIKELY(string_type and not it->second.is_string()))
22265  {
22266  JSON_THROW(parse_error::create(105, 0, error_msg + " must have string member '" + member + "'"));
22267  }
22268 
22269  // no error: return value
22270  return it->second;
22271  };
22272 
22273  // type check: every element of the array must be an object
22274  if (JSON_HEDLEY_UNLIKELY(not val.is_object()))
22275  {
22276  JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects"));
22277  }
22278 
22279  // collect mandatory members
22280  const std::string op = get_value("op", "op", true);
22281  const std::string path = get_value(op, "path", true);
22282  json_pointer ptr(path);
22283 
22284  switch (get_op(op))
22285  {
22286  case patch_operations::add:
22287  {
22288  operation_add(ptr, get_value("add", "value", false));
22289  break;
22290  }
22291 
22293  {
22294  operation_remove(ptr);
22295  break;
22296  }
22297 
22298  case patch_operations::replace:
22299  {
22300  // the "path" location must exist - use at()
22301  result.at(ptr) = get_value("replace", "value", false);
22302  break;
22303  }
22304 
22305  case patch_operations::move:
22306  {
22307  const std::string from_path = get_value("move", "from", true);
22308  json_pointer from_ptr(from_path);
22309 
22310  // the "from" location must exist - use at()
22311  basic_json v = result.at(from_ptr);
22312 
22313  // The move operation is functionally identical to a
22314  // "remove" operation on the "from" location, followed
22315  // immediately by an "add" operation at the target
22316  // location with the value that was just removed.
22317  operation_remove(from_ptr);
22318  operation_add(ptr, v);
22319  break;
22320  }
22321 
22323  {
22324  const std::string from_path = get_value("copy", "from", true);
22325  const json_pointer from_ptr(from_path);
22326 
22327  // the "from" location must exist - use at()
22328  basic_json v = result.at(from_ptr);
22329 
22330  // The copy is functionally identical to an "add"
22331  // operation at the target location using the value
22332  // specified in the "from" member.
22333  operation_add(ptr, v);
22334  break;
22335  }
22336 
22338  {
22339  bool success = false;
22340  JSON_TRY
22341  {
22342  // check if "value" matches the one at "path"
22343  // the "path" location must exist - use at()
22344  success = (result.at(ptr) == get_value("test", "value", false));
22345  }
22347  {
22348  // ignore out of range errors: success remains false
22349  }
22350 
22351  // throw an exception if test fails
22352  if (JSON_HEDLEY_UNLIKELY(not success))
22353  {
22354  JSON_THROW(other_error::create(501, "unsuccessful: " + val.dump()));
22355  }
22356 
22357  break;
22358  }
22359 
22360  default:
22361  {
22362  // op must be "add", "remove", "replace", "move", "copy", or
22363  // "test"
22364  JSON_THROW(parse_error::create(105, 0, "operation value '" + op + "' is invalid"));
22365  }
22366  }
22367  }
22368 
22369  return result;
22370  }
array (ordered collection of values)
static other_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2163
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
#define JSON_INTERNAL_CATCH(exception)
Definition: json.hpp:1757
std::ptrdiff_t difference_type
a type to represent differences between iterators
Definition: json.hpp:14807
BEGIN_PROLOG triggeremu_data_config_icarus settings PMTADCthresholds sequence::icarus_stage0_multiTPC_TPC physics sequence::icarus_stage0_EastHits_TPC physics sequence::icarus_stage0_WestHits_TPC physics producers purityana0 caloskimCalorimetryCryoE physics caloskimCalorimetryCryoW physics path
#define JSON_THROW(exception)
Definition: json.hpp:1754
object (unordered set of name/value pairs)
return match has_match and(match.match_pdg==11 or match.match_pdg==-11)
::nlohmann::json_pointer< basic_json > json_pointer
JSON Pointer, see nlohmann::json_pointer.
Definition: json.hpp:14753
static int array_index(const std::string &s)
Definition: json.hpp:10404
#define JSON_TRY
Definition: json.hpp:1755
#define JSON_HEDLEY_LIKELY(expr)
Definition: json.hpp:1123
detail::out_of_range out_of_range
exception indicating access out of the defined range
Definition: json.hpp:14782
std::string to_string(WindowPattern const &pattern)
replace invalid UTF-8 sequences with U+FFFD
static out_of_range create(int id_, const std::string &what_arg)
Definition: json.hpp:2125
T copy(T const &v)
BEGIN_PROLOG hitmakerfive clustermakerfour pfparticlemakerthree showermakertwo END_PROLOG hitmakerfive clustermakerfour pfparticlemakerthree sequence::inline_paths sequence::inline_paths sequence::inline_paths showermakers test
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: json.hpp:1124
static parse_error create(int id_, const position_t &pos, const std::string &what_arg)
create a parse error exception
Definition: json.hpp:1947
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
void nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::push_back ( basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > &&  val)
inline

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.

Parameters
[in]valthe value to add to the JSON array
Exceptions
type_error.308when called on a type other than JSON array or null; example: "cannot use push_back() with number"

Amortized constant.

{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}

Since
version 1.0.0

Definition at line 19406 of file json.hpp.

19407  {
19408  // push_back only works for null objects or arrays
19409  if (JSON_HEDLEY_UNLIKELY(not(is_null() or is_array())))
19410  {
19411  JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name())));
19412  }
19413 
19414  // transform null object into an array
19415  if (is_null())
19416  {
19419  assert_invariant();
19420  }
19421 
19422  // add element to array (move semantics)
19423  m_value.array->push_back(std::move(val));
19424  // invalidate object: mark it null so we do not call the destructor
19425  // cppcheck-suppress accessMoved
19426  val.m_type = value_t::null;
19427  }
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
array_t * array
array (stored with pointer to save storage)
Definition: json.hpp:15419
array (ordered collection of values)
constexpr bool is_null() const noexcept
return whether value is null
Definition: json.hpp:16691
#define JSON_THROW(exception)
Definition: json.hpp:1754
value_t m_type
the type of the current element
Definition: json.hpp:21017
void assert_invariant() const noexcept
checks the class invariants
Definition: json.hpp:15634
constexpr bool is_array() const noexcept
return whether value is an array
Definition: json.hpp:16872
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: json.hpp:1124
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
void nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::push_back ( const basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > &  val)
inline

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.

Parameters
[in]valthe value to add to the JSON array
Exceptions
type_error.308when called on a type other than JSON array or null; example: "cannot use push_back() with number"

Amortized constant.

{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}

Since
version 1.0.0

Definition at line 19443 of file json.hpp.

19444  {
19445  // push_back only works for null objects or arrays
19446  if (JSON_HEDLEY_UNLIKELY(not(is_null() or is_array())))
19447  {
19448  JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name())));
19449  }
19450 
19451  // transform null object into an array
19452  if (is_null())
19453  {
19456  assert_invariant();
19457  }
19458 
19459  // add element to array
19460  m_value.array->push_back(val);
19461  }
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
array_t * array
array (stored with pointer to save storage)
Definition: json.hpp:15419
array (ordered collection of values)
constexpr bool is_null() const noexcept
return whether value is null
Definition: json.hpp:16691
#define JSON_THROW(exception)
Definition: json.hpp:1754
value_t m_type
the type of the current element
Definition: json.hpp:21017
void assert_invariant() const noexcept
checks the class invariants
Definition: json.hpp:15634
constexpr bool is_array() const noexcept
return whether value is an array
Definition: json.hpp:16872
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: json.hpp:1124
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
void nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::push_back ( const typename object_t::value_type &  val)
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.

Parameters
[in]valthe value to add to the JSON object
Exceptions
type_error.308when called on a type other than JSON object or null; example: "cannot use push_back() with number"

Logarithmic in the size of the container, O(log(size())).

{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}

Since
version 1.0.0

Definition at line 19493 of file json.hpp.

19494  {
19495  // push_back only works for null objects or objects
19496  if (JSON_HEDLEY_UNLIKELY(not(is_null() or is_object())))
19497  {
19498  JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name())));
19499  }
19500 
19501  // transform null object into an object
19502  if (is_null())
19503  {
19506  assert_invariant();
19507  }
19508 
19509  // add element to array
19510  m_value.object->insert(val);
19511  }
object_t * object
object (stored with pointer to save storage)
Definition: json.hpp:15417
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
constexpr bool is_object() const noexcept
return whether value is an object
Definition: json.hpp:16850
constexpr bool is_null() const noexcept
return whether value is null
Definition: json.hpp:16691
#define JSON_THROW(exception)
Definition: json.hpp:1754
object (unordered set of name/value pairs)
value_t m_type
the type of the current element
Definition: json.hpp:21017
void assert_invariant() const noexcept
checks the class invariants
Definition: json.hpp:15634
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: json.hpp:1124
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
void nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::push_back ( initializer_list_t  init)
inline

add an object to an object

This function allows to use push_back with an initializer list. In case

  1. the current value is an object,
  2. the initializer list init contains only two elements, and
  3. the first element of init is a string,

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(basic_json&&).

Parameters
[in]initan initializer list

Linear in the size of the initializer list init.

Note
This function is required to resolve an ambiguous overload error, because pairs like {"key", "value"} can be both interpreted as object_t::value_type or std::initializer_list<basic_json>, see https://github.com/nlohmann/json/issues/235 for more information.

{The example shows how initializer lists are treated as objects when possible.,push_back__initializer_list}

Definition at line 19548 of file json.hpp.

19549  {
19550  if (is_object() and init.size() == 2 and (*init.begin())->is_string())
19551  {
19552  basic_json&& key = init.begin()->moved_or_copied();
19553  push_back(typename object_t::value_type(
19554  std::move(key.get_ref<string_t&>()), (init.begin() + 1)->moved_or_copied()));
19555  }
19556  else
19557  {
19558  push_back(basic_json(init));
19559  }
19560  }
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
StringType string_t
a type for a string
Definition: json.hpp:15125
constexpr bool is_object() const noexcept
return whether value is an object
Definition: json.hpp:16850
constexpr bool is_string() const noexcept
return whether value is a string
Definition: json.hpp:16894
return match has_match and(match.match_pdg==11 or match.match_pdg==-11)
void push_back(basic_json &&val)
add an object to an array
Definition: json.hpp:19406
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
reverse_iterator nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::rbegin ( )
inlinenoexcept

returns an iterator to the reverse-beginning

Returns an iterator to the reverse-beginning; that is, the last element.

range-rbegin-rend.svg
Illustration from cppreference.com

Constant.

This function helps basic_json satisfying the ReversibleContainer requirements:

  • The complexity is constant.
  • Has the semantics of reverse_iterator(end()).

{The following code shows an example for rbegin().,rbegin}

See Also
crbegin() – returns a const reverse iterator to the beginning
rend() – returns a reverse iterator to the end
crend() – returns a const reverse iterator to the end
Since
version 1.0.0

Definition at line 18815 of file json.hpp.

18816  {
18817  return reverse_iterator(end());
18818  }
iterator end() noexcept
returns an iterator to one past the last element
Definition: json.hpp:18745
json_reverse_iterator< typename basic_json::iterator > reverse_iterator
a reverse iterator for a basic_json container
Definition: json.hpp:14824
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
const_reverse_iterator nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::rbegin ( ) const
inlinenoexcept

returns a const reverse iterator to the last element

Returns a const iterator to the reverse-beginning; that is, the last element.

range-rbegin-rend.svg
Illustration from cppreference.com

Constant.

This function helps basic_json satisfying the ReversibleContainer requirements:

  • The complexity is constant.
  • Has the semantics of const_cast<const basic_json&>(*this).rbegin().

{The following code shows an example for crbegin().,crbegin}

See Also
rbegin() – returns a reverse iterator to the beginning
rend() – returns a reverse iterator to the end
crend() – returns a const reverse iterator to the end
Since
version 1.0.0

Definition at line 18823 of file json.hpp.

18824  {
18825  return crbegin();
18826  }
const_reverse_iterator crbegin() const noexcept
returns a const reverse iterator to the last element
Definition: json.hpp:18889
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
reverse_iterator nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::rend ( )
inlinenoexcept

returns an iterator to the reverse-end

Returns an iterator to the reverse-end; that is, one before the first element.

range-rbegin-rend.svg
Illustration from cppreference.com

Constant.

This function helps basic_json satisfying the ReversibleContainer requirements:

  • The complexity is constant.
  • Has the semantics of reverse_iterator(begin()).

{The following code shows an example for rend().,rend}

See Also
crend() – returns a const reverse iterator to the end
rbegin() – returns a reverse iterator to the beginning
crbegin() – returns a const reverse iterator to the beginning
Since
version 1.0.0

Definition at line 18852 of file json.hpp.

18853  {
18854  return reverse_iterator(begin());
18855  }
iterator begin() noexcept
returns an iterator to the first element
Definition: json.hpp:18674
json_reverse_iterator< typename basic_json::iterator > reverse_iterator
a reverse iterator for a basic_json container
Definition: json.hpp:14824
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
const_reverse_iterator nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::rend ( ) const
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.

range-rbegin-rend.svg
Illustration from cppreference.com

Constant.

This function helps basic_json satisfying the ReversibleContainer requirements:

  • The complexity is constant.
  • Has the semantics of const_cast<const basic_json&>(*this).rend().

{The following code shows an example for crend().,crend}

See Also
rend() – returns a reverse iterator to the end
rbegin() – returns a reverse iterator to the beginning
crbegin() – returns a const reverse iterator to the beginning
Since
version 1.0.0

Definition at line 18860 of file json.hpp.

18861  {
18862  return crend();
18863  }
const_reverse_iterator crend() const noexcept
returns a const reverse iterator to one before the first
Definition: json.hpp:18918
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename SAX >
static bool nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::sax_parse ( detail::input_adapter &&  i,
SAX *  sax,
input_format_t  format = input_format_t::json,
const bool  strict = true 
)
inlinestatic

generate SAX events

The SAX event lister must follow the interface of json_sax.

This function reads from a compatible input. Examples are:

  • an array of 1-byte values
  • strings with character/literal type with size of 1 byte
  • input streams
  • container with contiguous storage of 1-byte values. Compatible container types include std::vector, std::string, std::array, std::valarray, 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.
Precondition
Each element of the container has a size of 1 byte. Violating this precondition yields undefined behavior. This precondition is enforced with a static assertion.
The container storage is contiguous. Violating this precondition yields undefined behavior. This precondition is enforced with an assertion.
Warning
There is no way to enforce all preconditions at compile-time. If the function is called with a noncompliant container and with assertions switched off, the behavior is undefined and will most likely yield segmentation violation.
Parameters
[in]iinput to read from
[in,out]saxSAX event listener
[in]formatthe format to parse (JSON, CBOR, MessagePack, or UBJSON)
[in]strictwhether the input has to be consumed completely
Returns
return value of the last processed SAX event
Exceptions
parse_error.101if a parse error occurs; example: ""unexpected end of input; expected string literal""
parse_error.102if to_unicode fails or surrogate error
parse_error.103if to_unicode fails

Linear in the length of the input. The parser is a predictive LL(1) parser. The complexity can be higher if the SAX consumer sax has a super-linear complexity.

Note
A UTF-8 byte order mark is silently ignored.

{The example below demonstrates the sax_parse() function reading from string and processing the events with a user-defined SAX event consumer.,sax_parse}

Since
version 3.2.0

Definition at line 20814 of file json.hpp.

20817  {
20818  assert(sax);
20819  return format == input_format_t::json
20820  ? parser(std::move(i)).sax_parse(sax, strict)
20821  : detail::binary_reader<basic_json, SAX>(std::move(i)).sax_parse(format, sax, strict);
20822  }
static std::string format(PyObject *obj, unsigned int pos, unsigned int indent, unsigned int maxlen, unsigned int depth)
Definition: fclmodule.cxx:374
static bool sax_parse(detail::input_adapter &&i, SAX *sax, input_format_t format=input_format_t::json, const bool strict=true)
generate SAX events
Definition: json.hpp:20814
basic_json<> json
default JSON class
Definition: json.hpp:2445
::nlohmann::detail::parser< basic_json > parser
Definition: json.hpp:14731
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<class IteratorType , class SAX , typename std::enable_if< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< IteratorType >::iterator_category >::value, int >::type = 0>
static bool nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::sax_parse ( IteratorType  first,
IteratorType  last,
SAX *  sax 
)
inlinestatic

Definition at line 20900 of file json.hpp.

20901  {
20902  return parser(detail::input_adapter(first, last)).sax_parse(sax);
20903  }
::nlohmann::detail::parser< basic_json > parser
Definition: json.hpp:14731
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
size_type nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::size ( void  ) const
inlinenoexcept

returns the number of elements

Returns the number of elements in a JSON value.

Returns
The return value depends on the different types and is defined as follows:
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()
{The following code calls size() on the different value types.,size}

Constant, as long as array_t and object_t satisfy the Container concept; that is, their size() functions have constant complexity.

No changes.

No-throw guarantee: this function never throws exceptions.

Note
This function does not return the length of a string stored as JSON value - it returns the number of elements in the JSON value which is 1 in the case of a string.

This function helps basic_json satisfying the Container requirements:

  • The complexity is constant.
  • Has the semantics of std::distance(begin(), end()).
See Also
empty() – checks whether the container is empty
max_size() – returns the maximal number of elements
Since
version 1.0.0

Definition at line 19195 of file json.hpp.

19196  {
19197  switch (m_type)
19198  {
19199  case value_t::null:
19200  {
19201  // null values are empty
19202  return 0;
19203  }
19204 
19205  case value_t::array:
19206  {
19207  // delegate call to array_t::size()
19208  return m_value.array->size();
19209  }
19210 
19211  case value_t::object:
19212  {
19213  // delegate call to object_t::size()
19214  return m_value.object->size();
19215  }
19216 
19217  default:
19218  {
19219  // all other types have size 1
19220  return 1;
19221  }
19222  }
19223  }
object_t * object
object (stored with pointer to save storage)
Definition: json.hpp:15417
array_t * array
array (stored with pointer to save storage)
Definition: json.hpp:15419
array (ordered collection of values)
object (unordered set of name/value pairs)
value_t m_type
the type of the current element
Definition: json.hpp:21017
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
void nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::swap ( reference  other)
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.

Parameters
[in,out]otherJSON value to exchange the contents with

Constant.

{The example below shows how JSON values can be swapped with swap().,swap__reference}

Since
version 1.0.0

Definition at line 20054 of file json.hpp.

20060  {
20061  std::swap(m_type, other.m_type);
20062  std::swap(m_value, other.m_value);
20063  assert_invariant();
20064  }
value_t m_type
the type of the current element
Definition: json.hpp:21017
void assert_invariant() const noexcept
checks the class invariants
Definition: json.hpp:15634
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
void nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::swap ( array_t other)
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.

Parameters
[in,out]otherarray to exchange the contents with
Exceptions
type_error.310when JSON value is not an array; example: "cannot use swap() with string"

Constant.

{The example below shows how arrays can be swapped with swap().,swap__array_t}

Since
version 1.0.0

Definition at line 20086 of file json.hpp.

20087  {
20088  // swap only works for arrays
20090  {
20091  std::swap(*(m_value.array), other);
20092  }
20093  else
20094  {
20095  JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name())));
20096  }
20097  }
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
array_t * array
array (stored with pointer to save storage)
Definition: json.hpp:15419
#define JSON_THROW(exception)
Definition: json.hpp:1754
#define JSON_HEDLEY_LIKELY(expr)
Definition: json.hpp:1123
constexpr bool is_array() const noexcept
return whether value is an array
Definition: json.hpp:16872
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
void nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::swap ( object_t other)
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.

Parameters
[in,out]otherobject to exchange the contents with
Exceptions
type_error.310when JSON value is not an object; example: "cannot use swap() with string"

Constant.

{The example below shows how objects can be swapped with swap().,swap__object_t}

Since
version 1.0.0

Definition at line 20119 of file json.hpp.

20120  {
20121  // swap only works for objects
20123  {
20124  std::swap(*(m_value.object), other);
20125  }
20126  else
20127  {
20128  JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name())));
20129  }
20130  }
object_t * object
object (stored with pointer to save storage)
Definition: json.hpp:15417
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
constexpr bool is_object() const noexcept
return whether value is an object
Definition: json.hpp:16850
#define JSON_THROW(exception)
Definition: json.hpp:1754
#define JSON_HEDLEY_LIKELY(expr)
Definition: json.hpp:1123
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
void nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::swap ( string_t other)
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.

Parameters
[in,out]otherstring to exchange the contents with
Exceptions
type_error.310when JSON value is not a string; example: "cannot use swap() with boolean"

Constant.

{The example below shows how strings can be swapped with swap().,swap__string_t}

Since
version 1.0.0

Definition at line 20152 of file json.hpp.

20153  {
20154  // swap only works for strings
20156  {
20157  std::swap(*(m_value.string), other);
20158  }
20159  else
20160  {
20161  JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name())));
20162  }
20163  }
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
string_t * string
string (stored with pointer to save storage)
Definition: json.hpp:15421
#define JSON_THROW(exception)
Definition: json.hpp:1754
constexpr bool is_string() const noexcept
return whether value is a string
Definition: json.hpp:16894
#define JSON_HEDLEY_LIKELY(expr)
Definition: json.hpp:1123
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
static std::vector<uint8_t> nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::to_bson ( const basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > &  j)
inlinestatic

Serializes the given JSON object j to BSON and returns a vector containing the corresponding BSON-representation.

BSON (Binary JSON) is a binary format in which zero or more ordered key/value pairs are stored as a single entity (a so-called document).

The library uses the following mapping from JSON values types to BSON types:

JSON value type value/range BSON type marker
null null null 0x0A
boolean true, false boolean 0x08
number_integer -9223372036854775808..-2147483649 int64 0x12
number_integer -2147483648..2147483647 int32 0x10
number_integer 2147483648..9223372036854775807 int64 0x12
number_unsigned 0..2147483647 int32 0x10
number_unsigned 2147483648..9223372036854775807 int64 0x12
number_unsigned 9223372036854775808..18446744073709551615
number_float any value double 0x01
string any value string 0x02
array any value document 0x04
object any value document 0x03
Warning
The mapping is incomplete, since only JSON-objects (and things contained therein) can be serialized to BSON. Also, integers larger than 9223372036854775807 cannot be serialized to BSON, and the keys may not contain U+0000, since they are serialized a zero-terminated c-strings.
Exceptions
out_of_range.407if j.is_number_unsigned() && j.get<std::uint64_t>() > 9223372036854775807
out_of_range.409if a key in j contains a NULL (U+0000)
type_error.317if !j.is_object()
Precondition
The input j is required to be an object: j.is_object() == true.
Note
Any BSON output created via to_bson can be successfully parsed by from_bson.
Parameters
[in]jJSON value to serialize
Returns
BSON serialization as byte vector

Linear in the size of the JSON value j.

{The example shows the serialization of a JSON value to a byte vector in BSON format.,to_bson}

See Also
http://bsonspec.org/spec.html
from_bson(detail::input_adapter&&, const bool strict) for the analogous deserialization
to_ubjson(const basic_json&, const bool, const bool) for the related UBJSON format
to_cbor(const basic_json&) for the related CBOR format
to_msgpack(const basic_json&) for the related MessagePack format

Definition at line 21388 of file json.hpp.

21389  {
21390  std::vector<uint8_t> result;
21391  to_bson(j, result);
21392  return result;
21393  }
static std::vector< uint8_t > to_bson(const basic_json &j)
Serializes the given JSON object j to BSON and returns a vector containing the corresponding BSON-rep...
Definition: json.hpp:21388
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
static void nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::to_bson ( const basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > &  j,
detail::output_adapter< uint8_t >  o 
)
inlinestatic

Serializes the given JSON object j to BSON and forwards the corresponding BSON-representation to the given output_adapter o.

Parameters
jThe JSON object to convert to BSON.
oThe output adapter that receives the binary BSON representation.
Precondition
The input j shall be an object: j.is_object() == true
See Also
to_bson(const basic_json&)

Definition at line 21403 of file json.hpp.

21404  {
21405  binary_writer<uint8_t>(o).write_bson(j);
21406  }
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
static void nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::to_bson ( const basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > &  j,
detail::output_adapter< char >  o 
)
inlinestatic

Serializes the given JSON object j to BSON and forwards the corresponding BSON-representation to the given output_adapter o.

Parameters
jThe JSON object to convert to BSON.
oThe output adapter that receives the binary BSON representation.
Precondition
The input j shall be an object: j.is_object() == true
See Also
to_bson(const basic_json&)

Definition at line 21411 of file json.hpp.

21412  {
21413  binary_writer<char>(o).write_bson(j);
21414  }
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
static std::vector<uint8_t> nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::to_cbor ( const basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > &  j)
inlinestatic

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
Note
The mapping is complete in the sense that any JSON value type can be converted to a CBOR value.
If NaN or Infinity are stored inside a JSON number, they are serialized properly. This behavior differs from the dump() function which serializes NaN or Infinity to null.
The following CBOR types are not used in the conversion:
  • byte strings (0x40..0x5F)
  • UTF-8 strings terminated by "break" (0x7F)
  • arrays terminated by "break" (0x9F)
  • maps terminated by "break" (0xBF)
  • date/time (0xC0..0xC1)
  • bignum (0xC2..0xC3)
  • decimal fraction (0xC4)
  • bigfloat (0xC5)
  • tagged items (0xC6..0xD4, 0xD8..0xDB)
  • expected conversions (0xD5..0xD7)
  • simple values (0xE0..0xF3, 0xF8)
  • undefined (0xF7)
  • half and single-precision floats (0xF9-0xFA)
  • break (0xFF)
Parameters
[in]jJSON value to serialize
Returns
MessagePack serialization as byte vector

Linear in the size of the JSON value j.

{The example shows the serialization of a JSON value to a byte vector in CBOR format.,to_cbor}

See Also
http://cbor.io
from_cbor(detail::input_adapter&&, const bool, const bool) for the analogous deserialization
to_msgpack(const basic_json&) for the related MessagePack format
to_ubjson(const basic_json&, const bool, const bool) for the related UBJSON format
Since
version 2.0.9

Definition at line 21118 of file json.hpp.

21119  {
21120  std::vector<uint8_t> result;
21121  to_cbor(j, result);
21122  return result;
21123  }
static std::vector< uint8_t > to_cbor(const basic_json &j)
create a CBOR serialization of a given JSON value
Definition: json.hpp:21118
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
static void nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::to_cbor ( const basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > &  j,
detail::output_adapter< uint8_t >  o 
)
inlinestatic

Definition at line 21125 of file json.hpp.

21126  {
21127  binary_writer<uint8_t>(o).write_cbor(j);
21128  }
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
static void nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::to_cbor ( const basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > &  j,
detail::output_adapter< char >  o 
)
inlinestatic

Definition at line 21130 of file json.hpp.

21131  {
21132  binary_writer<char>(o).write_cbor(j);
21133  }
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
static std::vector<uint8_t> nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::to_msgpack ( const basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > &  j)
inlinestatic

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
Note
The mapping is complete in the sense that any JSON value type can be converted to a MessagePack value.
The following values can not be converted to a MessagePack value:
  • strings with more than 4294967295 bytes
  • arrays with more than 4294967295 elements
  • objects with more than 4294967295 elements
The following MessagePack types are not used in the conversion:
  • bin 8 - bin 32 (0xC4..0xC6)
  • ext 8 - ext 32 (0xC7..0xC9)
  • float 32 (0xCA)
  • fixext 1 - fixext 16 (0xD4..0xD8)
Any MessagePack output created to_msgpack can be successfully parsed by from_msgpack.
If NaN or Infinity are stored inside a JSON number, they are serialized properly. This behavior differs from the dump() function which serializes NaN or Infinity to null.
Parameters
[in]jJSON value to serialize
Returns
MessagePack serialization as byte vector

Linear in the size of the JSON value j.

{The example shows the serialization of a JSON value to a byte vector in MessagePack format.,to_msgpack}

See Also
http://msgpack.org
from_msgpack for the analogous deserialization
to_cbor(const basic_json& for the related CBOR format
to_ubjson(const basic_json&, const bool, const bool) for the related UBJSON format
Since
version 2.0.9

Definition at line 21214 of file json.hpp.

21215  {
21216  std::vector<uint8_t> result;
21217  to_msgpack(j, result);
21218  return result;
21219  }
static std::vector< uint8_t > to_msgpack(const basic_json &j)
create a MessagePack serialization of a given JSON value
Definition: json.hpp:21214
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
static void nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::to_msgpack ( const basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > &  j,
detail::output_adapter< uint8_t >  o 
)
inlinestatic

Definition at line 21221 of file json.hpp.

21222  {
21223  binary_writer<uint8_t>(o).write_msgpack(j);
21224  }
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
static void nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::to_msgpack ( const basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > &  j,
detail::output_adapter< char >  o 
)
inlinestatic

Definition at line 21226 of file json.hpp.

21227  {
21228  binary_writer<char>(o).write_msgpack(j);
21229  }
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
static std::vector<uint8_t> nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::to_ubjson ( const basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > &  j,
const bool  use_size = false,
const bool  use_type = false 
)
inlinestatic

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 {
Note
The mapping is complete in the sense that any JSON value type can be converted to a UBJSON value.
The following values can not be converted to a UBJSON value:
  • strings with more than 9223372036854775807 bytes (theoretical)
  • unsigned integer numbers above 9223372036854775807
The following markers are not used in the conversion:
  • Z: no-op values are not created.
  • C: single-byte strings are serialized with S markers.
Any UBJSON output created to_ubjson can be successfully parsed by from_ubjson.
If NaN or Infinity are stored inside a JSON number, they are serialized properly. This behavior differs from the dump() function which serializes NaN or Infinity to null.
The optimized formats for containers are supported: Parameter use_size adds size information to the beginning of a container and removes the closing marker. Parameter use_type further checks whether all elements of a container have the same type and adds the type marker to the beginning of the container. The use_type parameter must only be used together with use_size = true. Note that use_size = true alone may result in larger representations - the benefit of this parameter is that the receiving side is immediately informed on the number of elements of the container.
Parameters
[in]jJSON value to serialize
[in]use_sizewhether to add size annotations to container types
[in]use_typewhether to add type annotations to container types (must be combined with use_size = true)
Returns
UBJSON serialization as byte vector

Linear in the size of the JSON value j.

{The example shows the serialization of a JSON value to a byte vector in UBJSON format.,to_ubjson}

See Also
http://ubjson.org
from_ubjson(detail::input_adapter&&, const bool, const bool) for the analogous deserialization
to_cbor(const basic_json& for the related CBOR format
to_msgpack(const basic_json&) for the related MessagePack format
Since
version 3.1.0

Definition at line 21311 of file json.hpp.

21314  {
21315  std::vector<uint8_t> result;
21316  to_ubjson(j, result, use_size, use_type);
21317  return result;
21318  }
static std::vector< uint8_t > to_ubjson(const basic_json &j, const bool use_size=false, const bool use_type=false)
create a UBJSON serialization of a given JSON value
Definition: json.hpp:21311
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
static void nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::to_ubjson ( const basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > &  j,
detail::output_adapter< uint8_t >  o,
const bool  use_size = false,
const bool  use_type = false 
)
inlinestatic

Definition at line 21320 of file json.hpp.

21322  {
21323  binary_writer<uint8_t>(o).write_ubjson(j, use_size, use_type);
21324  }
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
static void nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::to_ubjson ( const basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > &  j,
detail::output_adapter< char >  o,
const bool  use_size = false,
const bool  use_type = false 
)
inlinestatic

Definition at line 21326 of file json.hpp.

21328  {
21329  binary_writer<char>(o).write_ubjson(j, use_size, use_type);
21330  }
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
constexpr value_t nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::type ( ) const
inlinenoexcept

return the type of the JSON value (explicit)

Return the type of the JSON value as a value from the value_t enumeration.

Returns
the type of the JSON value
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
Constant.

No-throw guarantee: this member function never throws exceptions.

{The following code exemplifies type() for all JSON types.,type}

See Also
operator value_t() – return the type of the JSON value (implicit)
type_name() – return the type as string
Since
version 1.0.0

Definition at line 16612 of file json.hpp.

16613  {
16614  return m_type;
16615  }
value_t m_type
the type of the current element
Definition: json.hpp:21017
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
JSON_HEDLEY_RETURNS_NON_NULL const char* nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::type_name ( ) const
inlinenoexcept

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.

Returns
a string representation of a the m_type member:
Value type return value
null "null"
boolean "boolean"
string "string"
number "number" (for all number types)
object "object"
array "array"
discarded "discarded"
No-throw guarantee: this function never throws exceptions.

Constant.

{The following code exemplifies type_name() for all JSON types.,type_name}

See Also
type() – return the type of the JSON value
operator value_t() – return the type of the JSON value (implicit)
Since
version 1.0.0, public since 2.1.0, const char* and noexcept since 3.0.0

Definition at line 20987 of file json.hpp.

20988  {
20989  {
20990  switch (m_type)
20991  {
20992  case value_t::null:
20993  return "null";
20994  case value_t::object:
20995  return "object";
20996  case value_t::array:
20997  return "array";
20998  case value_t::string:
20999  return "string";
21000  case value_t::boolean:
21001  return "boolean";
21002  case value_t::discarded:
21003  return "discarded";
21004  default:
21005  return "number";
21006  }
21007  }
21008  }
array (ordered collection of values)
object (unordered set of name/value pairs)
value_t m_type
the type of the current element
Definition: json.hpp:21017
discarded by the the parser callback function
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
basic_json nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::unflatten ( ) const
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:

  1. The value must be an object.
  2. The keys must be JSON pointers (see RFC 6901)
  3. The mapped values must be primitive JSON types.
Returns
the original JSON from a flattened version
Note
Empty objects and arrays are flattened by flatten() to 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().

Linear in the size the JSON value.

Exceptions
type_error.314if value is not an object
type_error.315if object values are not primitive

{The following code shows how a flattened JSON object is unflattened into the original nested JSON object.,unflatten}

See Also
flatten() for the reverse function
Since
version 2.0.0

Definition at line 22049 of file json.hpp.

22050  {
22051  return json_pointer::unflatten(*this);
22052  }
static BasicJsonType unflatten(const BasicJsonType &value)
Definition: json.hpp:11021
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
void nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::update ( const_reference  j)
inline

updates a JSON object from another object, overwriting existing keys

Inserts all values from JSON object j and overwrites existing keys.

Parameters
[in]jJSON object to read values from
Exceptions
type_error.312if called on JSON values other than objects; example: "cannot use update() with string"

O(N*log(size() + N)), where N is the number of elements to insert.

{The example shows how update() is used.,update}

See Also
https://docs.python.org/3.6/library/stdtypes.html#dict.update
Since
version 3.0.0

Definition at line 19952 of file json.hpp.

19953  {
19954  // implicitly convert null value to an empty object
19955  if (is_null())
19956  {
19958  m_value.object = create<object_t>();
19959  assert_invariant();
19960  }
19961 
19962  if (JSON_HEDLEY_UNLIKELY(not is_object()))
19963  {
19964  JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name())));
19965  }
19966  if (JSON_HEDLEY_UNLIKELY(not j.is_object()))
19967  {
19968  JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(j.type_name())));
19969  }
19970 
19971  for (auto it = j.cbegin(); it != j.cend(); ++it)
19972  {
19973  m_value.object->operator[](it.key()) = it.value();
19974  }
19975  }
object_t * object
object (stored with pointer to save storage)
Definition: json.hpp:15417
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
constexpr bool is_object() const noexcept
return whether value is an object
Definition: json.hpp:16850
constexpr bool is_null() const noexcept
return whether value is null
Definition: json.hpp:16691
#define JSON_THROW(exception)
Definition: json.hpp:1754
object (unordered set of name/value pairs)
value_t m_type
the type of the current element
Definition: json.hpp:21017
void assert_invariant() const noexcept
checks the class invariants
Definition: json.hpp:15634
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: json.hpp:1124
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
void nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::update ( const_iterator  first,
const_iterator  last 
)
inline

updates a JSON object from another object, overwriting existing keys

Inserts all values from from range [first, last) and overwrites existing keys.

Parameters
[in]firstbegin of the range of elements to insert
[in]lastend of the range of elements to insert
Exceptions
type_error.312if called on JSON values other than objects; example: "cannot use update() with string"
invalid_iterator.202if iterator first or last does does not point to an object; example: "iterators first and last must point to objects"
invalid_iterator.210if first and last do not belong to the same JSON value; example: "iterators do not fit"

O(N*log(size() + N)), where N is the number of elements to insert.

{The example shows how update() is used__range.,update}

See Also
https://docs.python.org/3.6/library/stdtypes.html#dict.update
Since
version 3.0.0

Definition at line 20003 of file json.hpp.

20004  {
20005  // implicitly convert null value to an empty object
20006  if (is_null())
20007  {
20009  m_value.object = create<object_t>();
20010  assert_invariant();
20011  }
20012 
20013  if (JSON_HEDLEY_UNLIKELY(not is_object()))
20014  {
20015  JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name())));
20016  }
20017 
20018  // check if range iterators belong to the same JSON object
20019  if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object))
20020  {
20021  JSON_THROW(invalid_iterator::create(210, "iterators do not fit"));
20022  }
20023 
20024  // passed iterators must belong to objects
20025  if (JSON_HEDLEY_UNLIKELY(not first.m_object->is_object()
20026  or not last.m_object->is_object()))
20027  {
20028  JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects"));
20029  }
20030 
20031  for (auto it = first; it != last; ++it)
20032  {
20033  m_value.object->operator[](it.key()) = it.value();
20034  }
20035  }
object_t * object
object (stored with pointer to save storage)
Definition: json.hpp:15417
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
constexpr bool is_object() const noexcept
return whether value is an object
Definition: json.hpp:16850
constexpr bool is_null() const noexcept
return whether value is null
Definition: json.hpp:16691
#define JSON_THROW(exception)
Definition: json.hpp:1754
object (unordered set of name/value pairs)
value_t m_type
the type of the current element
Definition: json.hpp:21017
void assert_invariant() const noexcept
checks the class invariants
Definition: json.hpp:15634
static invalid_iterator create(int id_, const std::string &what_arg)
Definition: json.hpp:2024
#define JSON_HEDLEY_UNLIKELY(expr)
Definition: json.hpp:1124
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
json_value m_value
the value of the current element
Definition: json.hpp:21020
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<class ValueType , typename std::enable_if< std::is_convertible< basic_json_t, ValueType >::value, int >::type = 0>
ValueType nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::value ( const typename object_t::key_type &  key,
const ValueType &  default_value 
) const
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

try {
return at(key);
} catch(out_of_range) {
return default_value;
}
Note
Unlike at(const typename object_t::key_type&), this function does not throw if the given key key was not found.
Unlike operator[](const typename object_t::key_type& key), this function does not implicitly add an element to the position defined by key. This function is furthermore also applicable to const objects.
Parameters
[in]keykey of the element to access
[in]default_valuethe value to return if key is not found
Template Parameters
ValueTypetype 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.
Returns
copy of the element at key key or default_value if key is not found
Exceptions
type_error.302if default_value does not match the type of the value at key
type_error.306if the JSON value is not an object; in that case, using value() with a key makes no sense.

Logarithmic in the size of the container.

{The example below shows how object elements can be queried with a default value.,basic_json__value}

See Also
at(const typename object_t::key_type&) for access by reference with range checking
operator[](const typename object_t::key_type&) for unchecked access by reference
Since
version 1.0.0

Definition at line 18003 of file json.hpp.

18004  {
18005  // at only works for objects
18007  {
18008  // if key is found, return value and given default value otherwise
18009  const auto it = find(key);
18010  if (it != end())
18011  {
18012  return *it;
18013  }
18014 
18015  return default_value;
18016  }
18017 
18018  JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name())));
18019  }
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
iterator end() noexcept
returns an iterator to one past the last element
Definition: json.hpp:18745
constexpr bool is_object() const noexcept
return whether value is an object
Definition: json.hpp:16850
iterator find(KeyT &&key)
find an element in a JSON object
Definition: json.hpp:18520
#define JSON_THROW(exception)
Definition: json.hpp:1754
#define JSON_HEDLEY_LIKELY(expr)
Definition: json.hpp:1123
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
string_t nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::value ( const typename object_t::key_type &  key,
const char *  default_value 
) const
inline

overload for a default value of type const char*

const const

Definition at line 18025 of file json.hpp.

18026  {
18027  return value(key, string_t(default_value));
18028  }
ValueType value(const typename object_t::key_type &key, const ValueType &default_value) const
access specified object element with default value
Definition: json.hpp:18003
StringType string_t
a type for a string
Definition: json.hpp:15125
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<class ValueType , typename std::enable_if< std::is_convertible< basic_json_t, ValueType >::value, int >::type = 0>
ValueType nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::value ( const json_pointer ptr,
const ValueType &  default_value 
) const
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

try {
return at(ptr);
} catch(out_of_range) {
return default_value;
}
Note
Unlike at(const json_pointer&), this function does not throw if the given key key was not found.
Parameters
[in]ptra JSON pointer to the element to access
[in]default_valuethe value to return if ptr found no value
Template Parameters
ValueTypetype 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.
Returns
copy of the element at key key or default_value if key is not found
Exceptions
type_error.302if default_value does not match the type of the value at ptr
type_error.306if the JSON value is not an object; in that case, using value() with a key makes no sense.

Logarithmic in the size of the container.

{The example below shows how object elements can be queried with a default value.,basic_json__value_ptr}

See Also
operator[](const json_pointer&) for unchecked access by reference
Since
version 2.0.2

Definition at line 18075 of file json.hpp.

18076  {
18077  // at only works for objects
18079  {
18080  // if pointer resolves a value, return it or use default value
18081  JSON_TRY
18082  {
18083  return ptr.get_checked(this);
18084  }
18086  {
18087  return default_value;
18088  }
18089  }
18090 
18091  JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name())));
18092  }
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:20987
#define JSON_INTERNAL_CATCH(exception)
Definition: json.hpp:1757
constexpr bool is_object() const noexcept
return whether value is an object
Definition: json.hpp:16850
#define JSON_THROW(exception)
Definition: json.hpp:1754
#define JSON_TRY
Definition: json.hpp:1755
#define JSON_HEDLEY_LIKELY(expr)
Definition: json.hpp:1123
detail::out_of_range out_of_range
exception indicating access out of the defined range
Definition: json.hpp:14782
static type_error create(int id_, const std::string &what_arg)
Definition: json.hpp:2078
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
string_t nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::value ( const json_pointer ptr,
const char *  default_value 
) const
inline

overload for a default value of type const char*

const const

Definition at line 18099 of file json.hpp.

18100  {
18101  return value(ptr, string_t(default_value));
18102  }
ValueType value(const typename object_t::key_type &key, const ValueType &default_value) const
access specified object element with default value
Definition: json.hpp:18003
StringType string_t
a type for a string
Definition: json.hpp:15125

Friends And Related Function Documentation

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename BasicJsonType , typename SAX >
friend class ::nlohmann::detail::binary_reader
friend

Definition at line 14720 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename BasicJsonType , typename CharType >
friend class ::nlohmann::detail::binary_writer
friend

Definition at line 14718 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename BasicJsonType >
friend class ::nlohmann::detail::iter_impl
friend

Definition at line 14716 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename BasicJsonType >
friend class ::nlohmann::detail::json_sax_dom_callback_parser
friend

Definition at line 14724 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename BasicJsonType >
friend class ::nlohmann::detail::json_sax_dom_parser
friend

Definition at line 14722 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<detail::value_t >
friend struct detail::external_constructor
friend

Definition at line 14711 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
bool operator!= ( const_reference  lhs,
const_reference  rhs 
)
friend

comparison: not equal

Compares two JSON values for inequality by calculating not (lhs == rhs).

Parameters
[in]lhsfirst JSON value to consider
[in]rhssecond JSON value to consider
Returns
whether the values lhs and rhs are not equal

Linear.

No-throw guarantee: this function never throws exceptions.

{The example demonstrates comparing several JSON types.,operator__notequal}

Since
version 1.0.0

Definition at line 20319 of file json.hpp.

20320  {
20321  return not (lhs == rhs);
20322  }
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename ScalarType , typename std::enable_if< std::is_scalar< ScalarType >::value, int >::type = 0>
bool operator!= ( const_reference  lhs,
const ScalarType  rhs 
)
friend

comparison: not equal

comparison: not equal Compares two JSON values for inequality by calculating not (lhs == rhs).

Parameters
[in]lhsfirst JSON value to consider
[in]rhssecond JSON value to consider
Returns
whether the values lhs and rhs are not equal

Linear.

No-throw guarantee: this function never throws exceptions.

{The example demonstrates comparing several JSON types.,operator__notequal}

Since
version 1.0.0

Definition at line 20330 of file json.hpp.

20331  {
20332  return lhs != basic_json(rhs);
20333  }
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename ScalarType , typename std::enable_if< std::is_scalar< ScalarType >::value, int >::type = 0>
bool operator!= ( const ScalarType  lhs,
const_reference  rhs 
)
friend

comparison: not equal

comparison: not equal Compares two JSON values for inequality by calculating not (lhs == rhs).

Parameters
[in]lhsfirst JSON value to consider
[in]rhssecond JSON value to consider
Returns
whether the values lhs and rhs are not equal

Linear.

No-throw guarantee: this function never throws exceptions.

{The example demonstrates comparing several JSON types.,operator__notequal}

Since
version 1.0.0

Definition at line 20341 of file json.hpp.

20342  {
20343  return basic_json(lhs) != rhs;
20344  }
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
bool operator< ( const_reference  lhs,
const_reference  rhs 
)
friend

comparison: less than

Compares whether one JSON value lhs is less than another JSON value rhs according to the following rules:

  • If lhs and rhs have the same type, the values are compared using the default < operator.
  • Integer and floating-point numbers are automatically converted before comparison
  • In case lhs and rhs have different types, the values are ignored and the order of the types is considered, see operator<(const value_t, const value_t).
Parameters
[in]lhsfirst JSON value to consider
[in]rhssecond JSON value to consider
Returns
whether lhs is less than rhs

Linear.

No-throw guarantee: this function never throws exceptions.

{The example demonstrates comparing several JSON types.,operator__less}

Since
version 1.0.0

Definition at line 20372 of file json.hpp.

20373  {
20374  const auto lhs_type = lhs.type();
20375  const auto rhs_type = rhs.type();
20376 
20377  if (lhs_type == rhs_type)
20378  {
20379  switch (lhs_type)
20380  {
20381  case value_t::array:
20382  // note parentheses are necessary, see
20383  // https://github.com/nlohmann/json/issues/1530
20384  return (*lhs.m_value.array) < (*rhs.m_value.array);
20385 
20386  case value_t::object:
20387  return (*lhs.m_value.object) < (*rhs.m_value.object);
20388 
20389  case value_t::null:
20390  return false;
20391 
20392  case value_t::string:
20393  return (*lhs.m_value.string) < (*rhs.m_value.string);
20394 
20395  case value_t::boolean:
20396  return (lhs.m_value.boolean) < (rhs.m_value.boolean);
20397 
20399  return (lhs.m_value.number_integer) < (rhs.m_value.number_integer);
20400 
20402  return (lhs.m_value.number_unsigned) < (rhs.m_value.number_unsigned);
20403 
20404  case value_t::number_float:
20405  return (lhs.m_value.number_float) < (rhs.m_value.number_float);
20406 
20407  default:
20408  return false;
20409  }
20410  }
20411  else if (lhs_type == value_t::number_integer and rhs_type == value_t::number_float)
20412  {
20413  return static_cast<number_float_t>(lhs.m_value.number_integer) < rhs.m_value.number_float;
20414  }
20415  else if (lhs_type == value_t::number_float and rhs_type == value_t::number_integer)
20416  {
20417  return lhs.m_value.number_float < static_cast<number_float_t>(rhs.m_value.number_integer);
20418  }
20419  else if (lhs_type == value_t::number_unsigned and rhs_type == value_t::number_float)
20420  {
20421  return static_cast<number_float_t>(lhs.m_value.number_unsigned) < rhs.m_value.number_float;
20422  }
20423  else if (lhs_type == value_t::number_float and rhs_type == value_t::number_unsigned)
20424  {
20425  return lhs.m_value.number_float < static_cast<number_float_t>(rhs.m_value.number_unsigned);
20426  }
20427  else if (lhs_type == value_t::number_integer and rhs_type == value_t::number_unsigned)
20428  {
20429  return lhs.m_value.number_integer < static_cast<number_integer_t>(rhs.m_value.number_unsigned);
20430  }
20431  else if (lhs_type == value_t::number_unsigned and rhs_type == value_t::number_integer)
20432  {
20433  return static_cast<number_integer_t>(lhs.m_value.number_unsigned) < rhs.m_value.number_integer;
20434  }
20435 
20436  // We only reach this line if we cannot compare values. In that case,
20437  // we compare types. Note we have to call the operator explicitly,
20438  // because MSVC has problems otherwise.
20439  return operator<(lhs_type, rhs_type);
20440  }
friend bool operator<(const_reference lhs, const_reference rhs) noexcept
comparison: less than
Definition: json.hpp:20372
NumberFloatType number_float_t
a type for a number (floating-point)
Definition: json.hpp:15362
array (ordered collection of values)
NumberIntegerType number_integer_t
a type for a number (integer)
Definition: json.hpp:15223
number value (signed integer)
object (unordered set of name/value pairs)
return match has_match and(match.match_pdg==11 or match.match_pdg==-11)
number value (unsigned integer)
number value (floating-point)
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename ScalarType , typename std::enable_if< std::is_scalar< ScalarType >::value, int >::type = 0>
bool operator< ( const_reference  lhs,
const ScalarType  rhs 
)
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:

  • If lhs and rhs have the same type, the values are compared using the default < operator.
  • Integer and floating-point numbers are automatically converted before comparison
  • In case lhs and rhs have different types, the values are ignored and the order of the types is considered, see operator<(const value_t, const value_t).
Parameters
[in]lhsfirst JSON value to consider
[in]rhssecond JSON value to consider
Returns
whether lhs is less than rhs

Linear.

No-throw guarantee: this function never throws exceptions.

{The example demonstrates comparing several JSON types.,operator__less}

Since
version 1.0.0

Definition at line 20448 of file json.hpp.

20449  {
20450  return lhs < basic_json(rhs);
20451  }
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename ScalarType , typename std::enable_if< std::is_scalar< ScalarType >::value, int >::type = 0>
bool operator< ( const ScalarType  lhs,
const_reference  rhs 
)
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:

  • If lhs and rhs have the same type, the values are compared using the default < operator.
  • Integer and floating-point numbers are automatically converted before comparison
  • In case lhs and rhs have different types, the values are ignored and the order of the types is considered, see operator<(const value_t, const value_t).
Parameters
[in]lhsfirst JSON value to consider
[in]rhssecond JSON value to consider
Returns
whether lhs is less than rhs

Linear.

No-throw guarantee: this function never throws exceptions.

{The example demonstrates comparing several JSON types.,operator__less}

Since
version 1.0.0

Definition at line 20459 of file json.hpp.

20460  {
20461  return basic_json(lhs) < rhs;
20462  }
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
std::ostream& operator<< ( std::ostream &  o,
const basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > &  j 
)
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.

  • The indentation of the output can be controlled with the member variable 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).
  • The indentation character can be controlled with the member variable 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.
Parameters
[in,out]ostream to serialize to
[in]jJSON value to serialize
Returns
the stream o
Exceptions
type_error.316if a string stored inside the JSON value is not UTF-8 encoded

Linear.

{The example below shows the serialization with different parameters to width to adjust the indentation level.,operator_serialize}

Since
version 1.0.0; indentation character added in version 3.0.0

Definition at line 20642 of file json.hpp.

20643  {
20644  // read width member and use it as indentation parameter if nonzero
20645  const bool pretty_print = o.width() > 0;
20646  const auto indentation = pretty_print ? o.width() : 0;
20647 
20648  // reset width to 0 for subsequent calls to this stream
20649  o.width(0);
20650 
20651  // do the actual serialization
20652  serializer s(detail::output_adapter<char>(o), o.fill());
20653  s.dump(j, pretty_print, false, static_cast<unsigned int>(indentation));
20654  return o;
20655  }
::nlohmann::detail::serializer< basic_json > serializer
Definition: json.hpp:14748
then echo File list $list not found else cat $list while read file do echo $file sed s
Definition: file_to_url.sh:60
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
std::istream& operator<< ( basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > &  j,
std::istream &  i 
)
friend

deserialize from stream

Deprecated:
This stream operator is deprecated and will be removed in version 4.0.0 of the library. Please use operator>>(std::istream&, basic_json&) instead; that is, replace calls like j << i; with i >> j;.
Since
version 1.0.0; deprecated since version 3.0.0

Definition at line 20914 of file json.hpp.

20915  {
20916  return operator>>(i, j);
20917  }
friend std::ostream & operator>>(const basic_json &j, std::ostream &o)
serialize to stream
Definition: json.hpp:20666
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
bool operator<= ( const_reference  lhs,
const_reference  rhs 
)
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).

Parameters
[in]lhsfirst JSON value to consider
[in]rhssecond JSON value to consider
Returns
whether lhs is less than or equal to rhs

Linear.

No-throw guarantee: this function never throws exceptions.

{The example demonstrates comparing several JSON types.,operator__greater}

Since
version 1.0.0

Definition at line 20483 of file json.hpp.

20484  {
20485  return not (rhs < lhs);
20486  }
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename ScalarType , typename std::enable_if< std::is_scalar< ScalarType >::value, int >::type = 0>
bool operator<= ( const_reference  lhs,
const ScalarType  rhs 
)
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).

Parameters
[in]lhsfirst JSON value to consider
[in]rhssecond JSON value to consider
Returns
whether lhs is less than or equal to rhs

Linear.

No-throw guarantee: this function never throws exceptions.

{The example demonstrates comparing several JSON types.,operator__greater}

Since
version 1.0.0

Definition at line 20494 of file json.hpp.

20495  {
20496  return lhs <= basic_json(rhs);
20497  }
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename ScalarType , typename std::enable_if< std::is_scalar< ScalarType >::value, int >::type = 0>
bool operator<= ( const ScalarType  lhs,
const_reference  rhs 
)
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).

Parameters
[in]lhsfirst JSON value to consider
[in]rhssecond JSON value to consider
Returns
whether lhs is less than or equal to rhs

Linear.

No-throw guarantee: this function never throws exceptions.

{The example demonstrates comparing several JSON types.,operator__greater}

Since
version 1.0.0

Definition at line 20505 of file json.hpp.

20506  {
20507  return basic_json(lhs) <= rhs;
20508  }
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
bool operator== ( const_reference  lhs,
const_reference  rhs 
)
friend

comparison: equal

Compares two JSON values for equality according to the following rules:

  • Two JSON values are equal if (1) they are from the same type and (2) their stored values are the same according to their respective operator==.
  • Integer and floating-point numbers are automatically converted before comparison. Note than two NaN values are always treated as unequal.
  • Two JSON null values are equal.
Note
Floating-point inside JSON values numbers are compared with json::number_float_t::operator== which is double::operator== by default. To compare floating-point while respecting an epsilon, an alternative comparison function could be used, for instance
inline bool is_same(T a, T b, T epsilon = std::numeric_limits<T>::epsilon()) noexcept
{
return std::abs(a - b) <= epsilon;
}
NaN values never compare equal to themselves or to other NaN values.
Parameters
[in]lhsfirst JSON value to consider
[in]rhssecond JSON value to consider
Returns
whether the values lhs and rhs are equal

No-throw guarantee: this function never throws exceptions.

Linear.

{The example demonstrates comparing several JSON types.,operator__equal}

Since
version 1.0.0

Definition at line 20214 of file json.hpp.

20215  {
20216  const auto lhs_type = lhs.type();
20217  const auto rhs_type = rhs.type();
20218 
20219  if (lhs_type == rhs_type)
20220  {
20221  switch (lhs_type)
20222  {
20223  case value_t::array:
20224  return *lhs.m_value.array == *rhs.m_value.array;
20225 
20226  case value_t::object:
20227  return *lhs.m_value.object == *rhs.m_value.object;
20228 
20229  case value_t::null:
20230  return true;
20231 
20232  case value_t::string:
20233  return *lhs.m_value.string == *rhs.m_value.string;
20234 
20235  case value_t::boolean:
20236  return lhs.m_value.boolean == rhs.m_value.boolean;
20237 
20239  return lhs.m_value.number_integer == rhs.m_value.number_integer;
20240 
20242  return lhs.m_value.number_unsigned == rhs.m_value.number_unsigned;
20243 
20244  case value_t::number_float:
20245  return lhs.m_value.number_float == rhs.m_value.number_float;
20246 
20247  default:
20248  return false;
20249  }
20250  }
20251  else if (lhs_type == value_t::number_integer and rhs_type == value_t::number_float)
20252  {
20253  return static_cast<number_float_t>(lhs.m_value.number_integer) == rhs.m_value.number_float;
20254  }
20255  else if (lhs_type == value_t::number_float and rhs_type == value_t::number_integer)
20256  {
20257  return lhs.m_value.number_float == static_cast<number_float_t>(rhs.m_value.number_integer);
20258  }
20259  else if (lhs_type == value_t::number_unsigned and rhs_type == value_t::number_float)
20260  {
20261  return static_cast<number_float_t>(lhs.m_value.number_unsigned) == rhs.m_value.number_float;
20262  }
20263  else if (lhs_type == value_t::number_float and rhs_type == value_t::number_unsigned)
20264  {
20265  return lhs.m_value.number_float == static_cast<number_float_t>(rhs.m_value.number_unsigned);
20266  }
20267  else if (lhs_type == value_t::number_unsigned and rhs_type == value_t::number_integer)
20268  {
20269  return static_cast<number_integer_t>(lhs.m_value.number_unsigned) == rhs.m_value.number_integer;
20270  }
20271  else if (lhs_type == value_t::number_integer and rhs_type == value_t::number_unsigned)
20272  {
20273  return lhs.m_value.number_integer == static_cast<number_integer_t>(rhs.m_value.number_unsigned);
20274  }
20275 
20276  return false;
20277  }
NumberFloatType number_float_t
a type for a number (floating-point)
Definition: json.hpp:15362
array (ordered collection of values)
NumberIntegerType number_integer_t
a type for a number (integer)
Definition: json.hpp:15223
number value (signed integer)
object (unordered set of name/value pairs)
return match has_match and(match.match_pdg==11 or match.match_pdg==-11)
number value (unsigned integer)
number value (floating-point)
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename ScalarType , typename std::enable_if< std::is_scalar< ScalarType >::value, int >::type = 0>
bool operator== ( const_reference  lhs,
const ScalarType  rhs 
)
friend

comparison: equal

comparison: equal Compares two JSON values for equality according to the following rules:

  • Two JSON values are equal if (1) they are from the same type and (2) their stored values are the same according to their respective operator==.
  • Integer and floating-point numbers are automatically converted before comparison. Note than two NaN values are always treated as unequal.
  • Two JSON null values are equal.
Note
Floating-point inside JSON values numbers are compared with json::number_float_t::operator== which is double::operator== by default. To compare floating-point while respecting an epsilon, an alternative comparison function could be used, for instance
inline bool is_same(T a, T b, T epsilon = std::numeric_limits<T>::epsilon()) noexcept
{
return std::abs(a - b) <= epsilon;
}
NaN values never compare equal to themselves or to other NaN values.
Parameters
[in]lhsfirst JSON value to consider
[in]rhssecond JSON value to consider
Returns
whether the values lhs and rhs are equal

No-throw guarantee: this function never throws exceptions.

Linear.

{The example demonstrates comparing several JSON types.,operator__equal}

Since
version 1.0.0

Definition at line 20285 of file json.hpp.

20286  {
20287  return lhs == basic_json(rhs);
20288  }
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename ScalarType , typename std::enable_if< std::is_scalar< ScalarType >::value, int >::type = 0>
bool operator== ( const ScalarType  lhs,
const_reference  rhs 
)
friend

comparison: equal

comparison: equal Compares two JSON values for equality according to the following rules:

  • Two JSON values are equal if (1) they are from the same type and (2) their stored values are the same according to their respective operator==.
  • Integer and floating-point numbers are automatically converted before comparison. Note than two NaN values are always treated as unequal.
  • Two JSON null values are equal.
Note
Floating-point inside JSON values numbers are compared with json::number_float_t::operator== which is double::operator== by default. To compare floating-point while respecting an epsilon, an alternative comparison function could be used, for instance
inline bool is_same(T a, T b, T epsilon = std::numeric_limits<T>::epsilon()) noexcept
{
return std::abs(a - b) <= epsilon;
}
NaN values never compare equal to themselves or to other NaN values.
Parameters
[in]lhsfirst JSON value to consider
[in]rhssecond JSON value to consider
Returns
whether the values lhs and rhs are equal

No-throw guarantee: this function never throws exceptions.

Linear.

{The example demonstrates comparing several JSON types.,operator__equal}

Since
version 1.0.0

Definition at line 20296 of file json.hpp.

20297  {
20298  return basic_json(lhs) == rhs;
20299  }
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
bool operator> ( const_reference  lhs,
const_reference  rhs 
)
friend

comparison: greater than

Compares whether one JSON value lhs is greater than another JSON value by calculating not (lhs <= rhs).

Parameters
[in]lhsfirst JSON value to consider
[in]rhssecond JSON value to consider
Returns
whether lhs is greater than to rhs

Linear.

No-throw guarantee: this function never throws exceptions.

{The example demonstrates comparing several JSON types.,operator__lessequal}

Since
version 1.0.0

Definition at line 20529 of file json.hpp.

20530  {
20531  return not (lhs <= rhs);
20532  }
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename ScalarType , typename std::enable_if< std::is_scalar< ScalarType >::value, int >::type = 0>
bool operator> ( const_reference  lhs,
const ScalarType  rhs 
)
friend

comparison: greater than

comparison: greater than Compares whether one JSON value lhs is greater than another JSON value by calculating not (lhs <= rhs).

Parameters
[in]lhsfirst JSON value to consider
[in]rhssecond JSON value to consider
Returns
whether lhs is greater than to rhs

Linear.

No-throw guarantee: this function never throws exceptions.

{The example demonstrates comparing several JSON types.,operator__lessequal}

Since
version 1.0.0

Definition at line 20540 of file json.hpp.

20541  {
20542  return lhs > basic_json(rhs);
20543  }
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename ScalarType , typename std::enable_if< std::is_scalar< ScalarType >::value, int >::type = 0>
bool operator> ( const ScalarType  lhs,
const_reference  rhs 
)
friend

comparison: greater than

comparison: greater than Compares whether one JSON value lhs is greater than another JSON value by calculating not (lhs <= rhs).

Parameters
[in]lhsfirst JSON value to consider
[in]rhssecond JSON value to consider
Returns
whether lhs is greater than to rhs

Linear.

No-throw guarantee: this function never throws exceptions.

{The example demonstrates comparing several JSON types.,operator__lessequal}

Since
version 1.0.0

Definition at line 20551 of file json.hpp.

20552  {
20553  return basic_json(lhs) > rhs;
20554  }
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
bool operator>= ( const_reference  lhs,
const_reference  rhs 
)
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).

Parameters
[in]lhsfirst JSON value to consider
[in]rhssecond JSON value to consider
Returns
whether lhs is greater than or equal to rhs

Linear.

No-throw guarantee: this function never throws exceptions.

{The example demonstrates comparing several JSON types.,operator__greaterequal}

Since
version 1.0.0

Definition at line 20575 of file json.hpp.

20576  {
20577  return not (lhs < rhs);
20578  }
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename ScalarType , typename std::enable_if< std::is_scalar< ScalarType >::value, int >::type = 0>
bool operator>= ( const_reference  lhs,
const ScalarType  rhs 
)
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).

Parameters
[in]lhsfirst JSON value to consider
[in]rhssecond JSON value to consider
Returns
whether lhs is greater than or equal to rhs

Linear.

No-throw guarantee: this function never throws exceptions.

{The example demonstrates comparing several JSON types.,operator__greaterequal}

Since
version 1.0.0

Definition at line 20586 of file json.hpp.

20587  {
20588  return lhs >= basic_json(rhs);
20589  }
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
template<typename ScalarType , typename std::enable_if< std::is_scalar< ScalarType >::value, int >::type = 0>
bool operator>= ( const ScalarType  lhs,
const_reference  rhs 
)
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).

Parameters
[in]lhsfirst JSON value to consider
[in]rhssecond JSON value to consider
Returns
whether lhs is greater than or equal to rhs

Linear.

No-throw guarantee: this function never throws exceptions.

{The example demonstrates comparing several JSON types.,operator__greaterequal}

Since
version 1.0.0

Definition at line 20597 of file json.hpp.

20598  {
20599  return basic_json(lhs) >= rhs;
20600  }
basic_json(const value_t v)
create an empty value with a given type
Definition: json.hpp:15752
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
std::ostream& operator>> ( const basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > &  j,
std::ostream &  o 
)
friend

serialize to stream

Deprecated:
This stream operator is deprecated and will be removed in future 4.0.0 of the library. Please use operator<<(std::ostream&, const basic_json&) instead; that is, replace calls like j >> o; with o << j;.
Since
version 1.0.0; deprecated since version 3.0.0

Definition at line 20666 of file json.hpp.

20667  {
20668  return o << j;
20669  }
template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
std::istream& operator>> ( std::istream &  i,
basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer > &  j 
)
friend

deserialize from stream

Deserializes an input stream to a JSON value.

Parameters
[in,out]iinput stream to read a serialized JSON value from
[in,out]jJSON value to write the deserialized input to
Exceptions
parse_error.101in case of an unexpected token
parse_error.102if to_unicode fails or surrogate error
parse_error.103if to_unicode fails

Linear in the length of the input. The parser is a predictive LL(1) parser.

Note
A UTF-8 byte order mark is silently ignored.

{The example below shows how a JSON value is constructed by reading a serialization from a stream.,operator_deserialize}

See Also
parse(std::istream&, const parser_callback_t) for a variant with a parser callback function to filter values while parsing
Since
version 1.0.0

Definition at line 20944 of file json.hpp.

20945  {
20946  parser(detail::input_adapter(i)).parse(false, j);
20947  return i;
20948  }
::nlohmann::detail::parser< basic_json > parser
Definition: json.hpp:14731

Member Data Documentation

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
value_t nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::m_type = value_t::null
private

the type of the current element

Definition at line 21017 of file json.hpp.

template<template< typename U, typename V, typename...Args > class ObjectType = std::map, template< typename U, typename...Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer>
json_value nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::m_value = {}
private

the value of the current element

Definition at line 21020 of file json.hpp.


The documentation for this class was generated from the following file: