1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
|
## Examples
The examples below illustrate the use of the [json](../ref/basic_json.md) class and [json_query](../ref/jsonpath/json_query.md) function.
### json construction
```cpp
#include <jsoncons/json.hpp>
// For convenience
using jsoncons::json;
// Construct a book object
json book1;
book1["category"] = "Fiction";
book1["title"] = "A Wild Sheep Chase: A Novel";
book1["author"] = "Haruki Murakami";
book1["date"] = "2002-04-09";
book1["price"] = 9.01;
book1["isbn"] = "037571894X";
// Construct another using the insert_or_assign function
json book2;
book2.insert_or_assign("category", "History");
book2.insert_or_assign("title", "Charlie Wilson's War");
book2.insert_or_assign("author", "George Crile");
book2.insert_or_assign("date", "2007-11-06");
book2.insert_or_assign("price", 10.50);
book2.insert_or_assign("isbn", "0802143415");
// Use insert_or_assign again, but more efficiently
json book3;
// Reserve memory, to avoid reallocations
book3.reserve(6);
// Insert in name alphabetical order
// Give insert_or_assign a hint where to insert the next member
auto hint = book3.insert_or_assign(book3.object_range().begin(),"author", "Haruki Murakami");
hint = book3.insert_or_assign(hint, "category", "Fiction");
hint = book3.insert_or_assign(hint, "date", "2006-01-03");
hint = book3.insert_or_assign(hint, "isbn", "1400079276");
hint = book3.insert_or_assign(hint, "price", 13.45);
hint = book3.insert_or_assign(hint, "title", "Kafka on the Shore");
// Construct a fourth from a string
json book4 = json::parse(R"(
{
"category" : "Fiction",
"title" : "Pulp",
"author" : "Charles Bukowski",
"date" : "2004-07-08",
"price" : 22.48,
"isbn" : "1852272007"
}
)");
// Construct a booklist array
json booklist(json_array_arg);
// For efficiency, reserve memory, to avoid reallocations
booklist.reserve(4);
// For efficency, tell jsoncons to move the contents
// of the four book objects into the array
booklist.add(std::move(book1));
booklist.add(std::move(book2));
// Add the third book to the front
auto pos = booklist.add(booklist.array_range().begin(),std::move(book3));
// and the last one immediately after
booklist.add(pos+1,std::move(book4));
// See what's left of book1, 2, 3 and 4 (expect nulls)
std::cout << book1 << "," << book2 << "," << book3 << "," << book4 << '\n';
++
//Loop through the booklist elements using a range-based for loop
for (const auto& book : booklist.array_range())
{
std::cout << book["title"].as<std::string>()
<< ","
<< book["price"].as<double>() << '\n';
}
// The second book
json& book = booklist[1];
//Loop through the book's name-value pairs using a range-based for loop
for (const auto& member : book.object_range())
{
std::cout << member.key()
<< ","
<< member.value() << '\n';
}
auto it = book.find("author");
if (it != book.object_range().end())
{
// member "author" found
}
if (book.contains("author"))
{
// book has a member "author"
}
book.get("author", "author unknown").as<std::string>();
// Returns author if found, otherwise "author unknown"
try
{
book["ratings"].as<std::string>();
}
catch (const std::out_of_range& e)
{
// member "ratings" not found
}
// Add ratings
book["ratings"]["*****"] = 4;
book["ratings"]["*"] = 2;
// Delete one-star ratings
book["ratings"].erase("*");
```
```cpp
// Serialize the booklist to a file
std::ofstream os("store.json");
os << pretty_print(booklist);
```
The JSON output `store.json`
```json
[
{
"author":"Haruki Murakami",
"category":"Fiction",
"date":"2006-01-03",
"isbn":"1400079276",
"price":13.45,
"title":"Kafka on the Shore"
},
{
"author":"Charles Bukowski",
"category":"Fiction",
"date":"2004-07-08",
"isbn":"1852272007",
"price":22.48,
"ratings":
{
"*****":4
},
"title":"Pulp"
},
{
"author":"Haruki Murakami",
"category":"Fiction",
"date":"2002-04-09",
"isbn":"037571894X",
"price":9.01,
"title":"A Wild Sheep Chase: A Novel"
},
{
"author":"George Crile",
"category":"History",
"date":"2007-11-06",
"isbn":"0802143415",
"price":10.5,
"title":"Charlie Wilson's War"
}
]
```
### json query
```cpp
#include <fstream>
#include <jsoncons/json.hpp>
#include <jsoncons_ext/jsonpath/jsonpath.hpp>
// For convenience
using jsoncons::json;
using jsoncons::jsonpath::json_query;
// Deserialize the booklist
std::ifstream is("store.json");
json booklist;
is >> booklist;
// Use a JSONPath expression to find
//
// (1) The authors of books that cost less than $12
json result = json_query(booklist, "$[*][?(@.price < 12)].author");
std::cout << result << '\n';
// (2) The number of books
result = json_query(booklist, "$.length");
std::cout << result << '\n';
// (3) The third book
result = json_query(booklist, "$[2]");
std::cout << '\n' << pretty_print(result) << '\n';
// (4) The authors of books that were published in 2004
result = json_query(booklist, "$[*][?(@.date =~ /2004.*?/)].author");
std::cout << result << '\n';
// (5) The titles of all books that have ratings
result = json_query(booklist, "$[*][?(@.ratings)].title");
std::cout << result << '\n';
// (6) All authors and titles of books
result = json_query(booklist, "$..['author','title']");
std::cout << pretty_print(result) << '\n';
```
Result:
```json
(1) ["Haruki Murakami","George Crile"]
(2) [4]
(3)
[
{
"author":"Haruki Murakami",
"category":"Fiction",
"date":"2002-04-09",
"isbn":"037571894X",
"price":9.01,
"title":"A Wild Sheep Chase: A Novel"
}
]
(4) ["Charles Bukowski"]
(5) ["Pulp"]
(6)
[
"Nigel Rees",
"Sayings of the Century",
"Evelyn Waugh",
"Sword of Honour",
"Herman Melville",
"Moby Dick",
"J. R. R. Tolkien",
"The Lord of the Rings"
]
```
## Once again, this time with wide characters
### wjson construction
```cpp
#include <jsoncons/json.hpp>
// For convenience
using jsoncons::wjson;
// Construct a book object
wjson book1;
book1[L"category"] = L"Fiction";
book1[L"title"] = L"A Wild Sheep Chase: A Novel";
book1[L"author"] = L"Haruki Murakami";
book1[L"date"] = L"2002-04-09";
book1[L"price"] = 9.01;
book1[L"isbn"] = L"037571894X";
// Construct another using the insert_or_assign function
wjson book2;
book2.insert_or_assign(L"category", L"History");
book2.insert_or_assign(L"title", L"Charlie Wilson's War");
book2.insert_or_assign(L"author", L"George Crile");
book2.insert_or_assign(L"date", L"2007-11-06");
book2.insert_or_assign(L"price", 10.50);
book2.insert_or_assign(L"isbn", L"0802143415");
// Use insert_or_assign again, but more efficiently
wjson book3;
// Reserve memory, to avoid reallocations
book3.reserve(6);
// Insert in name alphabetical order
// Give insert_or_assign a hint where to insert the next member
auto hint = book3.insert_or_assign(book3.object_range().begin(), L"author", L"Haruki Murakami");
hint = book3.insert_or_assign(hint, L"category", L"Fiction");
hint = book3.insert_or_assign(hint, L"date", L"2006-01-03");
hint = book3.insert_or_assign(hint, L"isbn", L"1400079276");
hint = book3.insert_or_assign(hint, L"price", 13.45);
hint = book3.insert_or_assign(hint, L"title", L"Kafka on the Shore");
// Construct a fourth from a string
wjson book4 = wjson::parse(LR"(
{
"category" : "Fiction",
"title" : "Pulp",
"author" : "Charles Bukowski",
"date" : "2004-07-08",
"price" : 22.48,
"isbn" : "1852272007"
}
)");
// Construct a booklist array
wjson booklist(json_array_arg);
// For efficiency, reserve memory, to avoid reallocations
booklist.reserve(4);
// For efficency, tell jsoncons to move the contents
// of the four book objects into the array
booklist.add(std::move(book1));
booklist.add(std::move(book2));
// Add the third book to the front
auto pos = booklist.add(booklist.array_range().begin(),std::move(book3));
// and the last one immediately after
booklist.add(pos+1,std::move(book4));
// See what's left of book1, 2, 3 and 4 (expect nulls)
std::wcout << book1 << L"," << book2 << L"," << book3 << L"," << book4 << '\n';
++
//Loop through the booklist elements using a range-based for loop
for (const auto& book : booklist.array_range())
{
std::wcout << book[L"title"].as<std::wstring>()
<< L","
<< book[L"price"].as<double>() << '\n';
}
// The second book
wjson& book = booklist[1];
//Loop through the book's name-value pairs using a range-based for loop
for (const auto& member : book.object_range())
{
std::wcout << member.key()
<< L","
<< member.value() << '\n';
}
auto it = book.find(L"author");
if (it != book.object_range().end())
{
// member "author" found
}
if (book.contains(L"author"))
{
// book has a member "author"
}
book.get(L"author", L"author unknown").as<std::wstring>();
// Returns author if found, otherwise "author unknown"
try
{
book[L"ratings"].as<std::wstring>();
}
catch (const std::out_of_range& e)
{
// member "ratings" not found
}
// Add ratings
book[L"ratings"][L"*****"] = 4;
book[L"ratings"][L"*"] = 2;
// Delete one-star ratings
book[L"ratings"].erase(L"*");
```
```cpp
// Serialize the booklist to a file
std::wofstream os("booklist2.json");
os << pretty_print(booklist);
```
### wjson query
```cpp
// Deserialize the booklist
std::wifstream is("booklist2.json");
wjson booklist;
is >> booklist;
// Use a JSONPath expression to find
//
// (1) The authors of books that cost less than $12
wjson result = json_query(booklist, L"$[*][?(@.price < 12)].author");
std::wcout << result << '\n';
// (2) The number of books
result = json_query(booklist, L"$.length");
std::wcout << result << '\n';
// (3) The third book
result = json_query(booklist, L"$[2]");
std::wcout << pretty_print(result) << '\n';
// (4) The authors of books that were published in 2004
result = json_query(booklist, L"$[*][?(@.date =~ /2004.*?/)].author");
std::wcout << result << '\n';
// (5) The titles of all books that have ratings
result = json_query(booklist, L"$[*][?(@.ratings)].title");
std::wcout << result << '\n';
// (6) All authors and titles of books
result = json_query(booklist, L"$..['author','title']");
std::wcout << pretty_print(result) << '\n';
```
Result:
```json
(1) ["Haruki Murakami","George Crile"]
(2) [4]
(3)
[
{
"author":"Haruki Murakami",
"category":"Fiction",
"date":"2002-04-09",
"isbn":"037571894X",
"price":9.01,
"title":"A Wild Sheep Chase: A Novel"
}
]
(4) ["Charles Bukowski"]
(5) ["Pulp"]
(6)
[
"Nigel Rees",
"Sayings of the Century",
"Evelyn Waugh",
"Sword of Honour",
"Herman Melville",
"Moby Dick",
"J. R. R. Tolkien",
"The Lord of the Rings"
]
```
|