TerraForge3D  2.3.1
3D Terrain And Landscape Generator

◆ operator[]() [3/8]

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

@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 // implicitly convert null value to an empty object
21478 if (is_null())
21479 {
21480 m_type = value_t::object;
21481 m_value.object = create<object_t>();
21483 }
21484
21485 // operator[] only works for objects
21486 if (JSON_HEDLEY_LIKELY(is_object()))
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
Definition: json.hpp:18935
constexpr bool is_object() const noexcept
return whether value is an object
Definition: json.hpp:20410
json_value m_value
the value of the current element
Definition: json.hpp:24928
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:24883
constexpr bool is_null() const noexcept
return whether value is null
Definition: json.hpp:20251
@ object
object (unordered set of name/value pairs)