JSON for Modern C++ 3.10.4

◆ json_pointer()

template<typename BasicJsonType >
nlohmann::json_pointer< BasicJsonType >::json_pointer ( const std::string &  s = "")
inlineexplicit

Create a JSON pointer according to the syntax described in Section 3 of RFC6901.

Parameters
[in]sstring representing the JSON pointer; if omitted, the empty string is assumed which references the whole JSON value
Exceptions
parse_error.107if the given JSON pointer s is nonempty and does not begin with a slash (/); see example below
parse_error.108if a tilde (~) in the given JSON pointer s is not followed by 0 (representing ~) or 1 (representing /); see example below
Example
The example shows the construction several valid JSON pointers as well as the exceptional behavior.
1#include <iostream>
2#include <nlohmann/json.hpp>
3
4using json = nlohmann::json;
5
6int main()
7{
8 // correct JSON pointers
10 json::json_pointer p2("");
11 json::json_pointer p3("/");
12 json::json_pointer p4("//");
13 json::json_pointer p5("/foo/bar");
14 json::json_pointer p6("/foo/bar/-");
15 json::json_pointer p7("/foo/~0");
16 json::json_pointer p8("/foo/~1");
17
18 // error: JSON pointer does not begin with a slash
19 try
20 {
21 json::json_pointer p9("foo");
22 }
23 catch (json::parse_error& e)
24 {
25 std::cout << e.what() << '\n';
26 }
27
28 // error: JSON pointer uses escape symbol ~ not followed by 0 or 1
29 try
30 {
31 json::json_pointer p10("/foo/~");
32 }
33 catch (json::parse_error& e)
34 {
35 std::cout << e.what() << '\n';
36 }
37
38 // error: JSON pointer uses escape symbol ~ not followed by 0 or 1
39 try
40 {
41 json::json_pointer p11("/foo/~3");
42 }
43 catch (json::parse_error& e)
44 {
45 std::cout << e.what() << '\n';
46 }
47}
detail::parse_error parse_error
exception indicating a parse error
Definition: json.hpp:17765
::nlohmann::json_pointer< basic_json > json_pointer
JSON Pointer, see nlohmann::json_pointer.
Definition: json.hpp:17740
basic_json<> json
default JSON class
Definition: json.hpp:3472

Output (play with this example online):
[json.exception.parse_error.107] parse error at byte 1: JSON pointer must be empty or begin with '/' - was: 'foo'
[json.exception.parse_error.108] parse error: escape character '~' must be followed with '0' or '1'
[json.exception.parse_error.108] parse error: escape character '~' must be followed with '0' or '1'
The example code above can be translated with
g++ -std=c++11 -Isingle_include doc/examples/json_pointer.cpp -o json_pointer 
Since
version 2.0.0

Definition at line 12502 of file json.hpp.