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 |
( |
size_type |
idx | ) |
|
|
inline |
access specified array element with bounds checking
Returns a reference to the element at specified location idx, with bounds checking.
- Parameters
-
[in] | idx | index of the element to access |
- Returns
- reference to the element at index idx
- Exceptions
-
type_error.304 | if the JSON value is not an array; in this case, calling at with an index makes no sense. See example below. |
out_of_range.401 | if the index idx is out of range of the array; that is, idx >= size() . See example below. |
@exceptionsafety Strong guarantee: if an exception is thrown, there are no changes in the JSON value.
@complexity Constant.
- Since
- version 1.0.0
@liveexample{The example below shows how array elements can be read and written using at()
. It also demonstrates the different exceptions that can be thrown.,at__size_type}
Definition at line 21186 of file json.hpp.
21187 {
21188
21189 if (JSON_HEDLEY_LIKELY(
is_array()))
21190 {
21191 JSON_TRY
21192 {
21193 return set_parent(
m_value.array->at(idx));
21194 }
21195 JSON_CATCH (std::out_of_range &)
21196 {
21197
21198 JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", *this));
21199 }
21200 }
21201
21202 else
21203 {
21204 JSON_THROW(type_error::create(304,
"cannot use at() with " + std::string(
type_name()), *
this));
21205 }
21206 }
json_value m_value
the value of the current element
constexpr bool is_array() const noexcept
return whether value is an array
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string