MCPcopy Create free account
hub / github.com/danielaparker/jsoncons / basics_json_example1

Function basics_json_example1

examples/src/basics_examples.cpp:13–134  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

11using namespace jsoncons::jsonpath;
12
13void 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));

Callers 1

mainFunction · 0.85

Calls 13

parseFunction · 0.85
pretty_printFunction · 0.85
insert_or_assignMethod · 0.45
reserveMethod · 0.45
beginMethod · 0.45
push_backMethod · 0.45
insertMethod · 0.45
keyMethod · 0.45
valueMethod · 0.45
findMethod · 0.45
endMethod · 0.45
containsMethod · 0.45

Tested by

no test coverage detected