since 0.174.0
| 115 | |
| 116 | // since 0.174.0 |
| 117 | void replace_example() |
| 118 | { |
| 119 | std::string json_string = R"( |
| 120 | {"books": [ |
| 121 | { "category": "reference", |
| 122 | "author": "Nigel Rees", |
| 123 | "title": "Sayings of the Century", |
| 124 | "price": 8.95 |
| 125 | }, |
| 126 | { "category": "fiction", |
| 127 | "author": "Evelyn Waugh", |
| 128 | "title": "Sword of Honour" |
| 129 | }, |
| 130 | { "category": "fiction", |
| 131 | "author": "Herman Melville", |
| 132 | "title": "Moby Dick", |
| 133 | "isbn": "0-553-21311-3" |
| 134 | } |
| 135 | ] |
| 136 | } |
| 137 | )"; |
| 138 | |
| 139 | json doc = json::parse(json_string); |
| 140 | |
| 141 | json new_price{13.0}; |
| 142 | |
| 143 | jsonpath::json_location loc0 = jsonpath::json_location::parse("$.books[0].price"); |
| 144 | auto result0 = jsonpath::replace(doc, loc0, new_price); |
| 145 | assert(result0.second); |
| 146 | assert(result0.first == std::addressof(doc.at("books").at(0).at("price"))); |
| 147 | assert(doc.at("books").at(0).at("price") == new_price); |
| 148 | |
| 149 | jsonpath::json_location loc1 = jsonpath::json_location::parse("$.books[1].price"); |
| 150 | auto result1 = jsonpath::replace(doc, loc1, new_price); |
| 151 | assert(!result1.second); |
| 152 | |
| 153 | // create_if_missing is true |
| 154 | result1 = jsonpath::replace(doc, loc1, new_price, true); |
| 155 | assert(result1.second); |
| 156 | assert(result1.first == std::addressof(doc.at("books").at(1).at("price"))); |
| 157 | assert(doc.at("books").at(1).at("price") == new_price); |
| 158 | |
| 159 | jsonpath::json_location loc2 = jsonpath::json_location::parse("$.books[2].kindle.price"); |
| 160 | auto result2 = jsonpath::replace(doc, loc2, new_price, true); |
| 161 | assert(result2.second); |
| 162 | assert(result2.first == std::addressof(doc.at("books").at(2).at("kindle").at("price"))); |
| 163 | assert(doc.at("books").at(2).at("kindle").at("price") == new_price); |
| 164 | |
| 165 | std::cout << pretty_print(doc) << "\n\n"; |
| 166 | } |
| 167 | |
| 168 | void convert_normalized_path_to_json_pointer() |
| 169 | { |
no test coverage detected