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 BinaryType = std::vector<std::uint8_t>>
ValueType nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType >::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 default_value;
}
detail::out_of_range out_of_range
exception indicating access out of the defined range
reference at(size_type idx)
access specified array element with bounds checking
- Note
- Unlike at(const json_pointer&), this function does not throw if the given key key was not found.
- Parameters
-
[in] | ptr | a JSON pointer to the element to access |
[in] | default_value | the value to return if ptr found no value |
- Template Parameters
-
ValueType | type compatible to JSON values, for instance int for JSON integer numbers, bool for JSON booleans, or std::vector types for JSON arrays. Note the type of the expected value at key and the default value default_value must be compatible. |
- Returns
- copy of the element at key key or default_value if key is not found
- Exceptions
-
type_error.302 | if default_value does not match the type of the value at ptr |
type_error.306 | if the JSON value is not an object; in that case, using value() with a key makes no sense. |
@complexity Logarithmic in the size of the container.
@liveexample{The example below shows how object elements can be queried with a default value.,basic_json__value_ptr}
- See also
- see operator[](const json_pointer&) for unchecked access by reference
- Since
- version 2.0.2
Definition at line 21755 of file json.hpp.
21756 {
21757
21759 {
21760
21761 JSON_TRY
21762 {
21763 return ptr.get_checked(this).template get<ValueType>();
21764 }
21766 {
21767 return default_value;
21768 }
21769 }
21770
21771 JSON_THROW(type_error::create(306,
"cannot use value() with " + std::string(
type_name()), *
this));
21772 }
constexpr bool is_object() const noexcept
return whether value is an object
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string