JSON for Modern C++  3.7.0

◆ at() [6/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>
const_reference nlohmann::basic_json::at ( const json_pointer ptr) const
inline

Returns a const 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.403if the JSON pointer describes a key of an object which cannot be found. 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 <iostream>
2 #include <nlohmann/json.hpp>
3 
4 using json = nlohmann::json;
5 
6 int main()
7 {
8  // create a JSON value
9  const json j =
10  {
11  {"number", 1}, {"string", "foo"}, {"array", {1, 2}}
12  };
13 
14  // read-only access
15 
16  // output element with JSON pointer "/number"
17  std::cout << j.at("/number"_json_pointer) << '\n';
18  // output element with JSON pointer "/string"
19  std::cout << j.at("/string"_json_pointer) << '\n';
20  // output element with JSON pointer "/array"
21  std::cout << j.at("/array"_json_pointer) << '\n';
22  // output element with JSON pointer "/array/1"
23  std::cout << j.at("/array/1"_json_pointer) << '\n';
24 
25  // out_of_range.109
26  try
27  {
28  // try to use an array index that is not a number
29  json::const_reference ref = j.at("/array/one"_json_pointer);
30  }
31  catch (json::parse_error& e)
32  {
33  std::cout << e.what() << '\n';
34  }
35 
36  // out_of_range.401
37  try
38  {
39  // try to use a an invalid array index
40  json::const_reference ref = j.at("/array/4"_json_pointer);
41  }
42  catch (json::out_of_range& e)
43  {
44  std::cout << e.what() << '\n';
45  }
46 
47  // out_of_range.402
48  try
49  {
50  // try to use the array index '-'
51  json::const_reference ref = j.at("/array/-"_json_pointer);
52  }
53  catch (json::out_of_range& e)
54  {
55  std::cout << e.what() << '\n';
56  }
57 
58  // out_of_range.403
59  try
60  {
61  // try to use a JSON pointer to an nonexistent object key
62  json::const_reference ref = j.at("/foo"_json_pointer);
63  }
64  catch (json::out_of_range& e)
65  {
66  std::cout << e.what() << '\n';
67  }
68 
69  // out_of_range.404
70  try
71  {
72  // try to use a JSON pointer that cannot be resolved
73  json::const_reference ref = j.at("/number/foo"_json_pointer);
74  }
75  catch (json::out_of_range& e)
76  {
77  std::cout << e.what() << '\n';
78  }
79 }
basic_json<> json
default JSON class
Definition: json.hpp:2355
reference at(size_type idx)
access specified array element with bounds checking
Definition: json.hpp:17335
const value_type & const_reference
the type of an element const reference
Definition: json.hpp:14667
detail::out_of_range out_of_range
exception indicating access out of the defined range
Definition: json.hpp:14645
detail::parse_error parse_error
exception indicating a parse error
Definition: json.hpp:14639

Output (play with this example online):
1
"foo"
[1,2]
2
[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.403] key 'foo' not found
[json.exception.out_of_range.404] unresolved reference token 'foo'
The example code above can be translated with
g++ -std=c++11 -Isingle_include doc/examples/at_json_pointer_const.cpp -o at_json_pointer_const 

Definition at line 21811 of file json.hpp.