Skip to content

Commit da290a1

Browse files
committedNov 26, 2023
Add json_location free function remove
1 parent 9229fb0 commit da290a1

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed
 

‎doc/ref/jsonpath/basic_json_location.md

+3-13
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ $['books'][0]
204204
#include <jsoncons/json.hpp>
205205
#include <jsoncons_ext/jsonpath/jsonpath.hpp>
206206
#include <fstream>
207+
#include <cassert>
207208
208209
using jsoncons::json;
209210
namespace jsonpath = jsoncons::jsonpath;
@@ -213,18 +214,16 @@ int main()
213214
std::ifstream is(/*path_to_books_file*/);
214215
json doc = json::parse(is);
215216
216-
std::size_t n = jsonpath::remove(doc, "$.books[?(@.category == 'fiction')]");
217+
std::size_t n = jsoncons::jsonpath::remove(doc, "$.books[1,1,3,3,0,0]");
217218
218-
std::cout << "Number of nodes removed: " << n << "\n\n";
219+
assert(n == 3);
219220
220221
std::cout << jsoncons::pretty_print(doc) << "\n\n";
221222
```
222223

223224
Output:
224225

225226
```
226-
Number of nodes removed: 3
227-
228227
{
229228
"books": [
230229
{
@@ -244,15 +243,6 @@ loc.append("store").append("book").append(1);
244243
245244
json* ptr = jsonpath::get(doc, loc);
246245
```
247-
248-
#### Remove the book at index 2
249-
250-
```
251-
jsonpath::json_location loc;
252-
loc.append("store").append("book").append(2);
253-
254-
jsonpath::remove(doc, loc);
255-
```
256246
257247
#### Convert a JSONPath normalized path into a JSONPointer
258248

‎test/jsonpath/src/jsonpath_expression_tests.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -355,4 +355,49 @@ TEST_CASE("jsonpath_expression::update tests")
355355
}
356356
}
357357

358+
TEST_CASE("jsonpath_expression remove")
359+
{
360+
std::string input = R"(
361+
{
362+
"books":
363+
[
364+
{
365+
"category": "fiction",
366+
"title" : "A Wild Sheep Chase",
367+
"author" : "Haruki Murakami",
368+
"price" : 22.72
369+
},
370+
{
371+
"category": "fiction",
372+
"title" : "The Night Watch",
373+
"author" : "Sergei Lukyanenko",
374+
"price" : 23.58
375+
},
376+
{
377+
"category": "fiction",
378+
"title" : "The Comedians",
379+
"author" : "Graham Greene",
380+
"price" : 21.99
381+
},
382+
{
383+
"category": "memoir",
384+
"title" : "The Night Watch",
385+
"author" : "Phillips, David Atlee"
386+
}
387+
]
388+
}
389+
)";
390+
391+
SECTION("test 1")
392+
{
393+
json doc = json::parse(input);
358394

395+
std::size_t n = jsoncons::jsonpath::remove(doc, "$.books[1,1,3,3,0,0]");
396+
397+
CHECK(n == 3);
398+
REQUIRE(doc.at("books").size() == 1);
399+
CHECK(doc.at("books")[0].at("title").as<std::string>() == "The Comedians");
400+
401+
std::cout << doc.at("books") << "\n";
402+
}
403+
}

0 commit comments

Comments
 (0)
Please sign in to comment.