| 85 | } |
| 86 | |
| 87 | void jsonpointer_select_author() |
| 88 | { |
| 89 | auto j = json::parse(R"( |
| 90 | [ |
| 91 | { "category": "reference", |
| 92 | "author": "Nigel Rees", |
| 93 | "title": "Sayings of the Century", |
| 94 | "price": 8.95 |
| 95 | }, |
| 96 | { "category": "fiction", |
| 97 | "author": "Evelyn Waugh", |
| 98 | "title": "Sword of Honour", |
| 99 | "price": 12.99 |
| 100 | } |
| 101 | ] |
| 102 | )"); |
| 103 | |
| 104 | // Using exceptions to report errors |
| 105 | try |
| 106 | { |
| 107 | json result = jsonpointer::get(j, "/1/author"); |
| 108 | std::cout << "(1) " << result << '\n'; |
| 109 | } |
| 110 | catch (const jsonpointer::jsonpointer_error& e) |
| 111 | { |
| 112 | std::cout << e.what() << '\n'; |
| 113 | } |
| 114 | |
| 115 | // Using error codes to report errors |
| 116 | std::error_code ec; |
| 117 | const json& result = jsonpointer::get(j, "/0/title", ec); |
| 118 | |
| 119 | if (ec) |
| 120 | { |
| 121 | std::cout << ec.message() << '\n'; |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | std::cout << "(2) " << result << '\n'; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void jsonpointer_add_member_to_object() |
| 130 | { |