-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.cpp
78 lines (60 loc) · 1.9 KB
/
example.cpp
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <YJson/YJson.cpp>
using namespace std;
std::string rootPath = "Y:\\C++\\YJson\\";
void DeserializeExample()
{
// 读取并反序列化 Read & Deserialize
auto obj = YJson::deserializeFromFile(rootPath + "example.json");
cout << obj << endl;
cout << obj["key_1"] << endl;
cout << obj["key_2"] << endl;
cout << obj["key_3"] << endl;
cout << obj["key_4"] << endl;
cout << obj["key_5"] << endl;
cout << obj["key_4"][0] << endl;
cout << obj["key_4"][1] << endl;
cout << obj["key_4"][2] << endl;
cout << obj["key_4"][3] << endl;
cout << obj["key_4"][4] << endl;
cout << obj["key_5"]["subkey_1"] << endl;
cout << obj["key_5"]["subkey_2"] << endl;
cout << obj["key_5"]["subkey_3"] << endl;
bool b1 = obj["key_1"].isNull();
cout << (b1 ? "true" : "false") << endl;
bool b2 = obj["key_2"].isNull();
cout << (b2 ? "true" : "false") << endl;
auto list = obj["key_4"].as<YJson::List>(); // typedef std::vector<Object> List;
for (auto item : list)
{
cout << item << " ";
}
cout << endl;
auto dict = obj["key_5"].as<YJson::Dict>(); // typedef std::map<std::string, Object> Dict;
for (auto item : dict)
{
cout << item.first << " ";
}
cout << endl;
}
void SerializeExample()
{
YJson::Dict dict;
YJson::Dict dict2;
YJson::List list;
dict["key_1"] = 3.14;
dict["key_2"] = YJson::Object(); // null
dict["key_3"] = "Hello World";
list.push_back(YJson::Object(233.));
list.push_back(YJson::Object((YJson::String)"你好"));
dict["key_4"] = list;
dict2["subkey"] = true;
dict["key_5"] = dict2;
YJson::serializeToFile(rootPath + "output.json", YJson::Object(dict), 4);
}
int main()
{
system("chcp 65001"); // 将控制台编码设置为utf-8 Set the console encoding to utf-8
DeserializeExample();
SerializeExample();
}