JSON for Modern C++  2.1.1

◆ at() [5/6]

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>
reference nlohmann::basic_json::at ( const json_pointer ptr)
inline

Returns a reference to the element at with specified JSON pointer ptr, with bounds checking.

Parameters
[in]ptrJSON pointer to the desired element
Returns
reference to the element pointed to by ptr
Exceptions
parse_error.106if an array index in the passed JSON pointer ptr begins with '0'. See example below.
parse_error.109if an array index in the passed JSON pointer ptr is not a number. See example below.
out_of_range.401if an array index in the passed JSON pointer ptr is out of range. See example below.
out_of_range.402if the array index '-' is used in the passed JSON pointer ptr. As at provides checked access (and no elements are implicitly inserted), the index '-' is always invalid. See example below.
out_of_range.404if the JSON pointer ptr can not be resolved. See example below.
Exception safety
Strong guarantee: if an exception is thrown, there are no changes in the JSON value.
Complexity
Constant.
Since
version 2.0.0
Example
The behavior is shown in the example.
1 #include "json.hpp"
2 
3 using json = nlohmann::json;
4 
5 int main()
6 {
7  // create a JSON value
8  json j =
9  {
10  {"number", 1}, {"string", "foo"}, {"array", {1, 2}}
11  };
12 
13  // read-only access
14 
15  // output element with JSON pointer "/number"
16  std::cout << j.at("/number"_json_pointer) << '\n';
17  // output element with JSON pointer "/string"
18  std::cout << j.at("/string"_json_pointer) << '\n';
19  // output element with JSON pointer "/array"
20  std::cout << j.at("/array"_json_pointer) << '\n';
21  // output element with JSON pointer "/array/1"
22  std::cout << j.at("/array/1"_json_pointer) << '\n';
23 
24  // writing access
25 
26  // change the string
27  j.at("/string"_json_pointer) = "bar";
28  // output the changed string
29  std::cout << j["string"] << '\n';
30 
31  // change an array element
32  j.at("/array/1"_json_pointer) = 21;
33  // output the changed array
34  std::cout << j["array"] << '\n';
35 
36 
37  // out_of_range.106
38  try
39  {
40  // try to use an array index with leading '0'
41  json::reference ref = j.at("/array/01"_json_pointer);
42  }
43  catch (json::parse_error& e)
44  {
45  std::cout << e.what() << '\n';
46  }
47 
48  // out_of_range.109
49  try
50  {
51  // try to use an array index that is not a number
52  json::reference ref = j.at("/array/one"_json_pointer);
53  }
54  catch (json::parse_error& e)
55  {
56  std::cout << e.what() << '\n';
57  }
58 
59  // out_of_range.401
60  try
61  {
62  // try to use a an invalid array index
63  json::reference ref = j.at("/array/4"_json_pointer);
64  }
65  catch (json::out_of_range& e)
66  {
67  std::cout << e.what() << '\n';
68  }
69 
70  // out_of_range.402
71  try
72  {
73  // try to use the array index '-'
74  json::reference ref = j.at("/array/-"_json_pointer);
75  }
76  catch (json::out_of_range& e)
77  {
78  std::cout << e.what() << '\n';
79  }
80 
81  // out_of_range.404
82  try
83  {
84  // try to use a JSON pointer that cannot be resolved
85  json::reference ref = j.at("/number/foo"_json_pointer);
86  }
87  catch (json::out_of_range& e)
88  {
89  std::cout << e.what() << '\n';
90  }
91 }
basic_json<> json
default JSON class
Definition: json.hpp:13933
reference at(size_type idx)
access specified array element with bounds checking
Definition: json.hpp:3780
detail::out_of_range out_of_range
exception indicating access out of the defined range
Definition: json.hpp:1324
detail::parse_error parse_error
exception indicating a parse error
Definition: json.hpp:1318
value_type & reference
the type of an element reference
Definition: json.hpp:1344
Output (play with this example online):
1
"foo"
[1,2]
2
"bar"
[1,21]
[json.exception.parse_error.106] parse error: array index '01' must not begin with '0'
[json.exception.parse_error.109] parse error: array index 'one' is not a number
[json.exception.out_of_range.401] array index 4 is out of range
[json.exception.out_of_range.402] array index '-' (2) is out of range
[json.exception.out_of_range.404] unresolved reference token 'foo'
The example code above can be translated with
g++ -std=c++11 -Isrc doc/examples/at_json_pointer.cpp -o at_json_pointer 

Definition at line 13329 of file json.hpp.