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>>
reference nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType >::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] | key | key of the element to access |
- Returns
- reference to the element at key key
- Exceptions
-
type_error.305 | if the JSON value is not an object or null; in that cases, using the [] operator with a key makes no sense. |
@complexity Logarithmic in the size of the container.
@liveexample{The example below shows how object elements can be read and written using the []
operator.,operatorarray__key_type}
- See also
- see at(const typename object_t::key_type&) for access by reference with range checking
-
see value() for access by value with a default value
- Since
- version 1.0.0
Definition at line 21475 of file json.hpp.
21476 {
21477
21479 {
21481 m_value.object = create<object_t>();
21483 }
21484
21485
21487 {
21488 return set_parent(
m_value.object->operator[](key));
21489 }
21490
21491 JSON_THROW(type_error::create(305,
"cannot use operator[] with a string argument with " + std::string(
type_name()), *
this));
21492 }
void assert_invariant(bool check_parents=true) const noexcept
checks the class invariants
constexpr bool is_object() const noexcept
return whether value is an object
json_value m_value
the value of the current element
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
constexpr bool is_null() const noexcept
return whether value is null
@ object
object (unordered set of name/value pairs)