TerraForge3D  2.3.1
3D Terrain And Landscape Generator

◆ value() [4/4]

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>>
template<class ValueType , typename std::enable_if< detail::is_getable< basic_json_t, ValueType >::value &&!std::is_same< value_t, ValueType >::value, int >::type = 0>
ValueType nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType >::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;
}
detail::out_of_range out_of_range
exception indicating access out of the defined range
Definition: json.hpp:17953
reference at(size_type idx)
access specified array element with bounds checking
Definition: json.hpp:21186
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.

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

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

Definition at line 21682 of file json.hpp.

21683 {
21684 // at only works for objects
21685 if (JSON_HEDLEY_LIKELY(is_object()))
21686 {
21687 // if key is found, return value and given default value otherwise
21688 const auto it = find(key);
21689
21690 if (it != end())
21691 {
21692 return it->template get<ValueType>();
21693 }
21694
21695 return default_value;
21696 }
21697
21698 JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()), *this));
21699 }
constexpr bool is_object() const noexcept
return whether value is an object
Definition: json.hpp:20410
iterator end() noexcept
returns an iterator to one past the last element
Definition: json.hpp:22448
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:24883
iterator find(KeyT &&key)
find an element in a JSON object
Definition: json.hpp:22223