JSON for Modern C++  2.1.1

◆ 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::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
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 "json.hpp"
2 
3 using json = nlohmann::json;
4 
5 int main()
6 {
7  // correct JSON pointers
8  json::json_pointer p1;
9  json::json_pointer p2("");
10  json::json_pointer p3("/");
11  json::json_pointer p4("//");
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  {
20  json::json_pointer p9("foo");
21  }
22  catch (json::parse_error& e)
23  {
24  std::cout << 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 (json::parse_error& e)
33  {
34  std::cout << 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 (json::parse_error& e)
43  {
44  std::cout << e.what() << '\n';
45  }
46 }
basic_json<> json
default JSON class
Definition: json.hpp:13933
detail::parse_error parse_error
exception indicating a parse error
Definition: json.hpp:1318
Output (play with this example online):
[json.exception.parse_error.107] parse error at 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 -Isrc doc/examples/json_pointer.cpp -o json_pointer 
Since
version 2.0.0

Definition at line 12577 of file json.hpp.