fixed #135: operator[] now only works on nonconst JSON objects
This commit is contained in:
parent
c767f464bb
commit
a70a7a8001
4 changed files with 3 additions and 145 deletions
src
57
src/json.hpp
57
src/json.hpp
|
@ -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
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue