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 >::at |
( |
const typename object_t::key_type & |
key | ) |
|
|
inline |
access specified object element with bounds checking
Returns a reference to the element at with specified key key, with bounds checking.
- Parameters
-
[in] | key | key of the element to access |
- Returns
- reference to the element at key key
- Exceptions
-
type_error.304 | if the JSON value is not an object; in this case, calling at with a key makes no sense. See example below. |
out_of_range.403 | if the key key is is not stored in the object; that is, find(key) == end() . See example below. |
@exceptionsafety Strong guarantee: if an exception is thrown, there are no changes in the JSON value.
@complexity Logarithmic in the size of the container.
- See also
- see operator[](const typename object_t::key_type&) for unchecked access by reference
-
see value() for access by value with a default value
- Since
- version 1.0.0
@liveexample{The example below shows how object elements can be read and written using at()
. It also demonstrates the different exceptions that can be thrown.,at__object_t_key_type}
Definition at line 21286 of file json.hpp.
21287 {
21288
21290 {
21291 JSON_TRY
21292 {
21293 return set_parent(
m_value.object->at(key));
21294 }
21295 JSON_CATCH (std::out_of_range &)
21296 {
21297
21298 JSON_THROW(out_of_range::create(403, "key '" + key + "' not found", *this));
21299 }
21300 }
21301
21302 else
21303 {
21304 JSON_THROW(type_error::create(304,
"cannot use at() with " + std::string(
type_name()), *
this));
21305 }
21306 }
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