| 134 | } |
| 135 | |
| 136 | void basics_json_example2() |
| 137 | { |
| 138 | // Deserialize the booklist |
| 139 | std::ifstream is("./output/store.json"); |
| 140 | json booklist; |
| 141 | is >> booklist; |
| 142 | |
| 143 | // Use a JSONPath expression to find |
| 144 | |
| 145 | // (1) The authors of books that cost less than $12 |
| 146 | json result = json_query(booklist, "$[*][?(@.price < 12)].author"); |
| 147 | std::cout << "(1) " << result << '\n'; |
| 148 | |
| 149 | // (2) The number of books |
| 150 | result = json_query(booklist, "$.length"); |
| 151 | std::cout << "(2) " << result << '\n'; |
| 152 | |
| 153 | // (3) The third book |
| 154 | result = json_query(booklist, "$[2]"); |
| 155 | std::cout << "(3) " << '\n' << pretty_print(result) << '\n'; |
| 156 | |
| 157 | // (4) The authors of books that were published in 2004 |
| 158 | result = json_query(booklist, "$[*][?(@.date =~ /2004.*?/)].author"); |
| 159 | std::cout << "(4) " << result << '\n'; |
| 160 | |
| 161 | // (5) The titles of all books that have ratings |
| 162 | result = json_query(booklist, "$[*][?(@.ratings)].title"); |
| 163 | std::cout << "(5) " << result << '\n'; |
| 164 | } |
| 165 | |
| 166 | int main() |
| 167 | { |
no test coverage detected