1#include <iostream>
2#include <nlohmann/json.hpp>
3
4using json = nlohmann::json;
5using namespace nlohmann::literals;
6
7int main()
8{
9    // create a JSON value
10    json j =
11    {
12        {"number", 1}, {"string", "foo"}, {"array", {1, 2}}
13    };
14
15    std::cout << std::boolalpha
16              << j.contains("/number"_json_pointer) << '\n'
17              << j.contains("/string"_json_pointer) << '\n'
18              << j.contains("/array"_json_pointer) << '\n'
19              << j.contains("/array/1"_json_pointer) << '\n'
20              << j.contains("/array/-"_json_pointer) << '\n'
21              << j.contains("/array/4"_json_pointer) << '\n'
22              << j.contains("/baz"_json_pointer) << std::endl;
23
24    try
25    {
26        // try to use an array index with leading '0'
27        j.contains("/array/01"_json_pointer);
28    }
29    catch (json::parse_error& e)
30    {
31        std::cout << e.what() << '\n';
32    }
33
34    try
35    {
36        // try to use an array index that is not a number
37        j.contains("/array/one"_json_pointer);
38    }
39    catch (json::parse_error& e)
40    {
41        std::cout << e.what() << '\n';
42    }
43}
44