* Add key_compare member to ordered_map
* Replace == with key_compare in ordered_map
* Expose the actual comparison function used by object_t
nlohmann::ordered_map uses a different comparison function than the one
provided via template parameter.
* Introduce a type trait to detect if object_t has a key_compare member.
* Rename object_comparator_t to default_object_comparator_t.
* Add object_comparator_t to be conditionally defined as
object_t::key_compare, if available, or default_object_comparator_t
otherwise.
* Update the documentation accordingly.
Co-authored-by: Niels Lohmann <niels.lohmann@gmail.com>
* Add type traits to check if a type is usable as object key
Add type trait to check:
* if a type is a specialization of a template.
* if a type is a json_pointer.
* if a type is a basic_json::{const_,}iterator.
* if two types are comparable using a given comparison functor.
* if a type is comparable to basic_json::object_t::key_type.
* if a type has a member type is_transparent.
* if a type is usable as object key.
* if a type has an erase() function accepting a given KeyType.
Co-authored-by: Niels Lohmann <niels.lohmann@gmail.com>
* Rework basic_json element access to accept more key types
Rework basic_json element access member functions and operators to
accept any type that meets the requirements defined by type trait
detail::is_usable_as_key_type.
Member functions and operators:
* at()
* operator[]
* value()
* erase()
* find()
* count()
* contains()
Update documentation to reflect these changes.
Add unit tests to excercise the new functions using std::string_view.
Co-authored-by: Niels Lohmann <niels.lohmann@gmail.com>
Co-authored-by: Niels Lohmann <niels.lohmann@gmail.com>
4.6 KiB
nlohmann::basic_json::value
// (1)
template<class ValueType>
ValueType value(const typename object_t::key_type& key,
ValueType&& default_value) const;
// (2)
template<class KeyType, class ValueType>
ValueType value(KeyType&& key,
ValueType&& default_value) const;
// (3)
template<class ValueType>
ValueType value(const json_pointer& ptr,
const ValueType& default_value) const;
-
Returns either a copy of an object's element at the specified key
keyor a given default value if no element with keykeyexists.The function is basically equivalent to executing
try { return at(key); } catch(out_of_range) { return default_value; } -
See 1. This overload is only available if
KeyTypeis comparable with#!cpp typename object_t::key_typeand#!cpp typename object_comparator_t::is_transparentdenotes a type. -
Returns either a copy of an object's element at the specified JSON pointer
ptror a given default value if no value atptrexists.The function is basically equivalent to executing
try { return at(ptr); } catch(out_of_range) { return default_value; }
!!! note "Differences to at and operator[]"
- Unlike [`at`](at.md), this function does not throw if the given `key`/`ptr` was not found.
- Unlike [`operator[]`](operator[].md), this function does not implicitly add an element to the position defined by
`key`/`ptr` key. This function is furthermore also applicable to const objects.
Template parameters
KeyType- A type for an object key other than
json_pointerthat is comparable withstring_tusingobject_comparator_t. This can also be a string view (C++17).ValueType - type compatible to JSON values, for instance
#!cpp intfor JSON integer numbers,#!cpp boolfor JSON booleans, or#!cpp std::vectortypes for JSON arrays. Note the type of the expected value atkey/ptrand the default valuedefault_valuemust be compatible.
Parameters
key(in)- key of the element to access
default_value(in)- the value to return if
key/ptrfound no value ptr(in)- a JSON pointer to the element to access
Return value
- copy of the element at key
keyordefault_valueifkeyis not found - copy of the element at key
keyordefault_valueifkeyis not found - copy of the element at JSON Pointer
ptrordefault_valueif no value forptris found
Exception safety
Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
Exceptions
- The function can throw the following exceptions:
- Throws
type_error.302ifdefault_valuedoes not match the type of the value atkey - Throws
type_error.306if the JSON value is not an object; in that case, usingvalue()with a key makes no sense.
- Throws
- See 1.
- The function can throw the following exceptions:
- Throws
type_error.302ifdefault_valuedoes not match the type of the value atptr - Throws
type_error.306if the JSON value is not an object; in that case, usingvalue()with a key makes no sense.
- Throws
Complexity
- Logarithmic in the size of the container.
- Logarithmic in the size of the container.
- Logarithmic in the size of the container.
Examples
??? example "Example (1): access specified object element with default value"
The example below shows how object elements can be queried with a default value.
```cpp
--8<-- "examples/basic_json__value.cpp"
```
Output:
```json
--8<-- "examples/basic_json__value.output"
```
??? example "Example (3): access specified object element via JSON Pointer with default value"
The example below shows how object elements can be queried with a default value.
```cpp
--8<-- "examples/basic_json__value_ptr.cpp"
```
Output:
```json
--8<-- "examples/basic_json__value_ptr.output"
```
See also
- see
atfor access by reference with range checking - see
operator[]for unchecked access by reference
Version history
- Added in version 1.0.0. Changed parameter
default_valuetype fromconst ValueType&toValueType&&in version 3.11.0. - Added in version 3.11.0.
- Added in version 2.0.2.