mirror of
https://github.com/nlohmann/json.git
synced 2025-07-13 20:21:48 +03:00
fixed #135: operator[] now only works on nonconst JSON objects
This commit is contained in:
@ -2691,33 +2691,6 @@ class basic_json
|
||||
|
||||
Returns a reference to the element at with specified key @a key.
|
||||
|
||||
@param[in] key key of the element to access
|
||||
|
||||
@return reference to the element at key @a key
|
||||
|
||||
@throw std::domain_error if JSON is not an object or null
|
||||
|
||||
@complexity Logarithmic in the size of the container.
|
||||
|
||||
@liveexample{The example below shows how object elements can be read using
|
||||
the [] operator.,operatorarray__key_type_const}
|
||||
*/
|
||||
const_reference operator[](const typename object_t::key_type& key) const
|
||||
{
|
||||
// at only works for objects
|
||||
if (m_type != value_t::object)
|
||||
{
|
||||
throw std::domain_error("cannot use operator[] with " + type_name());
|
||||
}
|
||||
|
||||
return m_value.object->operator[](key);
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief access specified object element
|
||||
|
||||
Returns a reference to the element at with specified key @a key.
|
||||
|
||||
@note If @a 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.
|
||||
@ -2754,36 +2727,6 @@ class basic_json
|
||||
return m_value.object->operator[](key);
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief access specified object element
|
||||
|
||||
Returns a reference to the element at with specified key @a key.
|
||||
|
||||
@note This function is required for compatibility reasons with Clang.
|
||||
|
||||
@param[in] key key of the element to access
|
||||
|
||||
@return reference to the element at key @a key
|
||||
|
||||
@throw std::domain_error if JSON is not an object or null
|
||||
|
||||
@complexity Logarithmic in the size of the container.
|
||||
|
||||
@liveexample{The example below shows how object elements can be read using
|
||||
the [] operator.,operatorarray__key_type_const}
|
||||
*/
|
||||
template<typename T, std::size_t n>
|
||||
const_reference operator[](const T (&key)[n]) const
|
||||
{
|
||||
// at only works for objects
|
||||
if (m_type != value_t::object)
|
||||
{
|
||||
throw std::domain_error("cannot use operator[] with " + type_name());
|
||||
}
|
||||
|
||||
return m_value.object->operator[](key);
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief access the first element
|
||||
|
||||
|
Reference in New Issue
Block a user