JSON for Modern C++ 2.1.1
Loading...
Searching...
No Matches

◆ json_pointer()

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>
nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer >::json_pointer::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
std::domain_errorif reference token is nonempty and does not begin with a slash (/); example: "JSON pointer must be empty or begin with /"
std::domain_errorif a tilde (~) is not followed by 0 (representing ~) or 1 (representing /); example: "escape error: ~ must be followed with 0 or 1"
Example\n The example shows the construction several valid JSON
pointers as well as the exceptional behavior.
1#include <json.hpp>
2
3using json = nlohmann::json;
4
5int main()
6{
7 // correct JSON pointers
12 json::json_pointer p5("/foo/bar");
13 json::json_pointer p6("/foo/bar/-");
14 json::json_pointer p7("/foo/~0");
15 json::json_pointer p8("/foo/~1");
16
17 // error: JSON pointer does not begin with a slash
18 try
19 {
21 }
22 catch (std::domain_error& e)
23 {
24 std::cout << "domain_error: " << e.what() << '\n';
25 }
26
27 // error: JSON pointer uses escape symbol ~ not followed by 0 or 1
28 try
29 {
30 json::json_pointer p10("/foo/~");
31 }
32 catch (std::domain_error& e)
33 {
34 std::cout << "domain_error: " << e.what() << '\n';
35 }
36
37 // error: JSON pointer uses escape symbol ~ not followed by 0 or 1
38 try
39 {
40 json::json_pointer p11("/foo/~3");
41 }
42 catch (std::domain_error& e)
43 {
44 std::cout << "domain_error: " << e.what() << '\n';
45 }
46}
static basic_json array(std::initializer_list< basic_json > init=std::initializer_list< basic_json >())
explicitly create an array from an initializer list
Definition json.hpp:2165
a class to store JSON values
Definition json.hpp:1040
basic_json<> json
default JSON class
Definition json.hpp:12369

Output (play with this example online):
domain_error: JSON pointer must be empty or begin with '/'
domain_error: escape error: '~' must be followed with '0' or '1'
domain_error: escape error: '~' must be followed with '0' or '1'

The example code above can be translated with
g++ -std=c++11 -Isrc doc/examples/json_pointer.cpp -o json_pointer 
Since
version 2.0.0

Definition at line 11127 of file json.hpp.