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>>
access specified array element with bounds checking
Returns a const reference to the element at specified location idx, with bounds checking.
- Parameters
-
[in] | idx | index of the element to access |
- Returns
- const 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 using at()
. It also demonstrates the different exceptions that can be thrown., at__size_type_const}
Definition at line 21234 of file json.hpp.
21235 {
21236
21237 if (JSON_HEDLEY_LIKELY(
is_array()))
21238 {
21239 JSON_TRY
21240 {
21241 return m_value.array->at(idx);
21242 }
21243 JSON_CATCH (std::out_of_range &)
21244 {
21245
21246 JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", *this));
21247 }
21248 }
21249
21250 else
21251 {
21252 JSON_THROW(type_error::create(304,
"cannot use at() with " + std::string(
type_name()), *
this));
21253 }
21254 }
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