-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
57 lines (52 loc) · 1.43 KB
/
main.cpp
File metadata and controls
57 lines (52 loc) · 1.43 KB
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
/***********************************************
File Name: main.cpp
Author: Abby Cin
Mail: abbytsing@gmail.com
Created Time: 12/29/20 2:22 PM
***********************************************/
#include "json.h"
#include <iostream>
int main()
{
std::string s { R"~(
{
"name": "elder",
"age": 1926.8,
"hobby": {
"mo": ["too young", "too simple"],
"ha": "+1s",
"profile": [1926, 8, "naive!", true]
},
"male": true
}
)~" };
auto r = nm::json::parse(s);
if (!r) {
std::cout << r.trace() << '\n';
return 1;
}
std::cout << "is object: " << std::boolalpha << r.is_object() << '\n';
auto o = r.as<nm::json::object_t>();
std::cout << "object size: " << o->size() << '\n';
auto &elder = r.get<nm::json::object_t>()["name"];
std::cout << "name: " << elder.get<nm::json::string_t>() << '\n';
std::cout
<< "age: "
<< r.get<nm::json::object_t>()["age"].get<nm::json::number_t>()
<< '\n';
r["male"] = nm::json::array_t { "he is male?", true };
std::cout << "stringify: \n";
std::cout << r.to_string(2) << '\n';
std::cout << "-------------------------------\n";
nm::json::JsonValue j {
{ { "name", "elder" },
{ "age", 1926.800090 },
{ "\tmotto\t",
nm::json::array_t { "too young too simple\t",
"sometimes naive!" } } }
};
std::cout << j.to_string() << '\n';
std::cout << std::setprecision(9) << j["age"] << '\n';
std::cout << j["\tmotto\t"][0] << j["\tmotto\t"][1] << '\n';
return 0;
}