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

Function constructor_examples

examples/src/json_constructor_examples.cpp:16–76  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

14using namespace jsoncons;
15
16void constructor_examples()
17{
18 json j1; // An empty object
19 std::cout << "(1) " << j1 << "\n";
20
21 json j2(json_object_arg, {{"baz", "qux"}, {"foo", "bar"}}); // An object
22 std::cout << "(2) " << j2 << "\n";
23
24 json j3(json_array_arg, {"bar", "baz"}); // An array
25 std::cout << "(3) " << j3 << "\n";
26
27 json j4(json::null()); // A null value
28 std::cout << "(4) " << j4 << "\n";
29
30 json j5(true); // A boolean value
31 std::cout << "(5) " << j5 << "\n";
32
33 double x = 1.0/7.0;
34
35 json j6(x); // A double value
36 std::cout << "(6) " << j6 << "\n";
37
38 json j8("Hello"); // A text string
39 std::cout << "(8) " << j8 << "\n";
40
41 std::vector<int> v = {10,20,30};
42 json j9 = v; // From a sequence container
43 std::cout << "(9) " << j9 << "\n";
44
45 std::map<std::string, int> m{ {"one", 1}, {"two", 2}, {"three", 3} };
46 json j10 = m; // From an associative container
47 std::cout << "(10) " << j10 << "\n";
48
49 std::vector<uint8_t> bytes = {'H','e','l','l','o'};
50 json j11(byte_string_arg, bytes); // A byte string
51 std::cout << "(11) " << j11 << "\n";
52
53 json j12(half_arg, 0x3bff);
54 std::cout << "(12) " << j12.as_double() << "\n";
55
56 // An object value with four members
57 json obj;
58 obj["first_name"] = "Jane";
59 obj["last_name"] = "Roe";
60 obj["events_attended"] = 10;
61 obj["accept_waiver_of_liability"] = true;
62
63 std::string first_name = obj["first_name"].as<std::string>();
64 std::string last_name = obj.at("last_name").as<std::string>();
65 int events_attended = obj["events_attended"].as<int>();
66 bool accept_waiver_of_liability = obj["accept_waiver_of_liability"].as<bool>();
67
68 // An array value with four elements
69 json arr(json_array_arg);
70 arr.push_back(j1);
71 arr.push_back(j2);
72 arr.push_back(j3);
73 arr.push_back(j4);

Callers 1

mainFunction · 0.85

Calls 4

pretty_printFunction · 0.85
as_doubleMethod · 0.80
atMethod · 0.80
push_backMethod · 0.45

Tested by

no test coverage detected