| 11 | using namespace jsoncons::jsonpath; |
| 12 | |
| 13 | void basics_json_example1() |
| 14 | { |
| 15 | // Construct a book object |
| 16 | json book1; |
| 17 | |
| 18 | book1["category"] = "Fiction"; |
| 19 | book1["title"] = "A Wild Sheep Chase: A Novel"; |
| 20 | book1["author"] = "Haruki Murakami"; |
| 21 | book1["date"] = "2002-04-09"; |
| 22 | book1["price"] = 9.01; |
| 23 | book1["isbn"] = "037571894X"; |
| 24 | |
| 25 | // Construct another using the member function insert_or_assign |
| 26 | json book2; |
| 27 | |
| 28 | book2.insert_or_assign("category", "History"); |
| 29 | book2.insert_or_assign("title", "Charlie Wilson's War"); |
| 30 | book2.insert_or_assign("author", "George Crile"); |
| 31 | book2.insert_or_assign("date", "2007-11-06"); |
| 32 | book2.insert_or_assign("price", 10.50); |
| 33 | book2.insert_or_assign("isbn", "0802143415"); |
| 34 | |
| 35 | // Use insert_or_assign again, but more efficiently |
| 36 | json book3; |
| 37 | |
| 38 | // Reserve memory, to avoid reallocations |
| 39 | book3.reserve(6); |
| 40 | |
| 41 | // Insert in name alphabetical order |
| 42 | // Give insert_or_assign a hint where to insert the next member |
| 43 | auto hint = book3.insert_or_assign(book3.object_range().begin(),"author", "Haruki Murakami"); |
| 44 | hint = book3.insert_or_assign(hint, "category", "Fiction"); |
| 45 | hint = book3.insert_or_assign(hint, "date", "2006-01-03"); |
| 46 | hint = book3.insert_or_assign(hint, "isbn", "1400079276"); |
| 47 | hint = book3.insert_or_assign(hint, "price", 13.45); |
| 48 | hint = book3.insert_or_assign(hint, "title", "Kafka on the Shore"); |
| 49 | |
| 50 | // Construct a fourth from a string |
| 51 | json book4 = json::parse(R"( |
| 52 | { |
| 53 | "category" : "Fiction", |
| 54 | "title" : "Pulp", |
| 55 | "author" : "Charles Bukowski", |
| 56 | "date" : "2004-07-08", |
| 57 | "price" : 22.48, |
| 58 | "isbn" : "1852272007" |
| 59 | } |
| 60 | )"); |
| 61 | |
| 62 | // Construct a booklist array |
| 63 | json booklist(json_array_arg); |
| 64 | |
| 65 | // For efficiency, reserve memory, to avoid reallocations |
| 66 | booklist.reserve(4); |
| 67 | |
| 68 | // For efficency, tell jsoncons to move the contents |
| 69 | // of the four book objects into the array |
| 70 | booklist.push_back(std::move(book1)); |
no test coverage detected