| 22 | namespace jsonpointer = jsoncons::jsonpointer; |
| 23 | |
| 24 | void json_query_examples() |
| 25 | { |
| 26 | std::ifstream is("./input/store.json"); |
| 27 | auto booklist = jsoncons::json::parse(is); |
| 28 | |
| 29 | // The authors of books that are cheaper than $10 |
| 30 | jsoncons::json result1 = jsonpath::json_query(booklist, "$.store.book[?(@.price < 10)].author"); |
| 31 | std::cout << "(1) " << result1 << "\n"; |
| 32 | |
| 33 | // The number of books |
| 34 | jsoncons::json result2 = jsonpath::json_query(booklist, "length($..book)"); |
| 35 | std::cout << "(2) " << result2 << "\n"; |
| 36 | |
| 37 | // The third book |
| 38 | jsoncons::json result3 = jsonpath::json_query(booklist, "$..book[2]"); |
| 39 | std::cout << "(3)\n" << pretty_print(result3) << "\n"; |
| 40 | |
| 41 | // All books whose author's name starts with Evelyn |
| 42 | jsoncons::json result4 = jsonpath::json_query(booklist, "$.store.book[?(@.author =~ /Evelyn.*?/)]"); |
| 43 | std::cout << "(4)\n" << pretty_print(result4) << "\n"; |
| 44 | |
| 45 | // The titles of all books that have isbn number |
| 46 | jsoncons::json result5 = jsonpath::json_query(booklist, "$..book[?(@.isbn)].title"); |
| 47 | std::cout << "(5) " << result5 << "\n"; |
| 48 | |
| 49 | // All authors and titles of books |
| 50 | jsoncons::json result6 = jsonpath::json_query(booklist, "$['store']['book']..['author','title']"); |
| 51 | std::cout << "(6)\n" << pretty_print(result6) << "\n"; |
| 52 | |
| 53 | // Union of two ranges of book titles |
| 54 | jsoncons::json result7 = jsonpath::json_query(booklist, "$..book[1:2,2:4].title"); |
| 55 | std::cout << "(7) " << result7 << "\n"; |
| 56 | |
| 57 | // Union of a subset of book titles identified by index |
| 58 | jsoncons::json result8 = jsonpath::json_query(booklist, "$.store[@.book[0].title,@.book[1].title,@.book[3].title]"); |
| 59 | std::cout << "(8) " << result8 << "\n"; |
| 60 | |
| 61 | // Union of third book title and all book titles with price > 10 |
| 62 | jsoncons::json result9 = jsonpath::json_query(booklist, "$.store[@.book[3].title,@.book[?(@.price > 10)].title]"); |
| 63 | std::cout << "(9) " << result9 << "\n"; |
| 64 | |
| 65 | // Intersection of book titles with category fiction and price < 15 |
| 66 | jsoncons::json result10 = jsonpath::json_query(booklist, "$.store.book[?(@.category == 'fiction' && @.price < 15)].title"); |
| 67 | std::cout << "(10) " << result10 << "\n"; |
| 68 | |
| 69 | // Normalized path expressions |
| 70 | jsoncons::json result11 = jsonpath::json_query(booklist, "$.store.book[?(@.author =~ /Evelyn.*?/)]", jsonpath::result_options::path); |
| 71 | std::cout << "(11) " << result11 << "\n"; |
| 72 | |
| 73 | // All titles whose author's second name is 'Waugh' |
| 74 | jsoncons::json result12 = jsonpath::json_query(booklist,"$.store.book[?(tokenize(@.author,'\\\\s+')[1] == 'Waugh')].title"); |
| 75 | std::cout << "(12) " << result12 << "\n"; |
| 76 | |
| 77 | // All keys in the second book |
| 78 | jsoncons::json result13 = jsonpath::json_query(booklist,"keys($.store.book[1])"); |
| 79 | std::cout << "(13) " << result13 << "\n"; |
| 80 | |
| 81 | jsoncons::json result14 = jsonpath::json_query(booklist,"$.store.book[?(ceil(@.price) == 9)]"); |
no test coverage detected