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

Function basics_wjson_example1

examples/src/basics_wexamples.cpp:12–133  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10using namespace jsoncons;
11
12void basics_wjson_example1()
13{
14 // Construct a book object
15 wjson book1;
16
17 book1[L"category"] = L"Fiction";
18 book1[L"title"] = L"A Wild Sheep Chase: A Novel";
19 book1[L"author"] = L"Haruki Murakami";
20 book1[L"date"] = L"2002-04-09";
21 book1[L"price"] = 9.01;
22 book1[L"isbn"] = L"037571894X";
23
24 // Construct another using the member function insert_or_assign
25 wjson book2;
26
27 book2.insert_or_assign(L"category", L"History");
28 book2.insert_or_assign(L"title", L"Charlie Wilson's War");
29 book2.insert_or_assign(L"author", L"George Crile");
30 book2.insert_or_assign(L"date", L"2007-11-06");
31 book2.insert_or_assign(L"price", 10.50);
32 book2.insert_or_assign(L"isbn", L"0802143415");
33
34 // Use insert_or_assign again, but more efficiently
35 wjson book3;
36
37 // Reserve memory, to avoid reallocations
38 book3.reserve(6);
39
40 // Insert in name alphabetical order
41 // Give insert_or_assign a hint where to insert the next member
42 auto hint = book3.insert_or_assign(book3.object_range().begin(), L"author", L"Haruki Murakami");
43 hint = book3.insert_or_assign(hint, L"category", L"Fiction");
44 hint = book3.insert_or_assign(hint, L"date", L"2006-01-03");
45 hint = book3.insert_or_assign(hint, L"isbn", L"1400079276");
46 hint = book3.insert_or_assign(hint, L"price", 13.45);
47 hint = book3.insert_or_assign(hint, L"title", L"Kafka on the Shore");
48
49 // Construct a fourth from a string
50
51 wjson book4 = wjson::parse(LR"(
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 // Construct a booklist array
62 wjson booklist(json_array_arg);
63
64 // For efficiency, reserve memory, to avoid reallocations
65 booklist.reserve(4);
66
67 // For efficency, tell jsoncons to move the contents
68 // of the four book objects into the array
69 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