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>>
const_reference nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType >::at |
( |
const typename object_t::key_type & |
key | ) |
const |
|
inline |
access specified object element with bounds checking
Returns a const reference to the element at with specified key key, with bounds checking.
- Parameters
-
[in] | key | key of the element to access |
- Returns
- const 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 using at()
. It also demonstrates the different exceptions that can be thrown., at__object_t_key_type_const}
Definition at line 21338 of file json.hpp.
21339 {
21340
21342 {
21343 JSON_TRY
21344 {
21345 return m_value.object->at(key);
21346 }
21347 JSON_CATCH (std::out_of_range &)
21348 {
21349
21350 JSON_THROW(out_of_range::create(403, "key '" + key + "' not found", *this));
21351 }
21352 }
21353
21354 else
21355 {
21356 JSON_THROW(type_error::create(304,
"cannot use at() with " + std::string(
type_name()), *
this));
21357 }
21358 }
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