-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
222 lines (188 loc) · 5.27 KB
/
main.cpp
File metadata and controls
222 lines (188 loc) · 5.27 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <iostream>
#include <cassert>
#include <iomanip>
#include <fstream>
#include "single_include/nlohmann/json.hpp"
using namespace std;
using json = nlohmann::json;
/*
1. 创建如下的 json 对象:
{
"pi": 3.141,
"happy": true,
"name": "Niels",
"nothing": null,
"answer": {
"everything": 42
},
"list": [1, 0, 2],
"object": {
"currency": "USD",
"value": 42.99
}
}
*/
static void create_first_json()
{
json j;
j["pi"] = 3.141;
j["happy"] = true;
j["name"] = "Niels";
j["nothing"] = nullptr;
j["answer"]["everything"] = 42;
j["list"] = {1, 0, 2};
j["object"] = {{"currency", "USD"}, {"value", 42.99}};
json j2 = {
{"pi", 3.141},
{"happy", true},
{"name", "Niels"},
{"nothing", nullptr},
{"answer", {{"everything", 42}}},
{"list", {1, 0, 2}},
{"object", {{"currency", "USD"}, {"value", 42.99}}}};
assert(j == j2);
cout << j << endl;
}
/*
2. 创建确定类型的对象
json::array() & json::object()
*/
static void create_explicit_cases()
{
json array_not_object = json::array({{"currency", "USD"}, {"value", 42.99}});
cout << "array_not_object: " << array_not_object << endl;
json empty_array_explicit = json::array();
cout << "empty_array: " << empty_array_explicit << endl;
json empty_object_implicit = json({});
json empty_object_explicit = json::object();
assert(empty_object_explicit == empty_object_implicit);
cout << "empty_object: " << empty_object_implicit << endl;
json null_object_implicit = json{};
json null_object_explicit = json();
assert(null_object_explicit == null_object_implicit);
cout << "null_boject: " << null_object_implicit << endl;
}
/*
3. 反序列化(from string to json)
_json & json::parse()
*/
static json deserialize()
{
json j = "{ \"happy\": true, \"pi\": 3.141 }"_json;
auto j2 = R"(
{
"happy": true,
"pi": 3.141
}
)"_json;
auto j3 = json::parse("{ \"happy\": true, \"pi\": 3.141 }");
assert(j == j2 && j == j3);
cout << "deserialization: " << j << endl;
return j;
}
/*
4. 序列化(from json to string)
j.dump() 接收参数为缩进量,重载 << 运算符调用了 dump()
一般情况下无须特地调用 dump() 序列化,直接调用 << 即可,
dump() 可看作主要用于格式化输出。
*/
static void serialize(const json &j)
{
string s = j.dump();
cout << "serialization: " << s << endl;
cout << "serialization with pretty printing: " << j.dump(4) << endl;
}
/*
5. 序列化 value 与原始 string 的区别
serialized value: j_string & serialized_string
original string: cpp_string & cpp_string2 & j_string.get<std::string>()
*/
static void diff_serialization_and_assignment()
{
json j_string = "this is a string";
string serialized_string = j_string.dump();
cout << "serialized value: " << j_string << " == " << serialized_string << endl;
auto cpp_string = j_string.get<string>();
string cpp_string2;
j_string.get_to(cpp_string2);
cout << "original string: " << cpp_string << " == " << cpp_string2 << " == " << j_string.get<string>() << endl;
}
/*
6. 流序列化(一)
from istream to json & from json to ostream
*/
static void interact_with_iostream()
{
json j;
cin >> j;
// 重载 setw() 操纵器,同 j.dump(4)
cout << setw(4) << j << endl;
}
/*
7. 流序列化(二)
from ifstream to json & from json to ofstream
*/
static void interact_with_fstream()
{
ifstream i("file.json");
json j;
i >> j;
ofstream o("pretty.json");
o << setw(4) << j << endl;
}
/*
8. Read from iterator range
*/
static void interact_with_iterator()
{
vector<uint8_t> v = {'t', 'r', 'u', 'e'};
json j = json::parse(v.begin(), v.end());
json j2 = json::parse(v);
assert(j == j2);
cout << j << endl;
}
/*
9. 自定义类型的序列化
*/
namespace ns
{
struct person
{
string name;
string address;
int age;
bool operator==(const person &p) { return name == p.name && address == p.address && age == p.age; }
};
void to_json(json &j, const person &p)
{
j = json{{"name", p.name}, {"address", p.address}, {"age", p.age}};
}
void from_json(const json &j, person &p)
{
j.at("name").get_to(p.name);
j.at("address").get_to(p.address);
j.at("age").get_to(p.age);
}
} // namespace ns
static void arbitrary_type_convert()
{
ns::person p{"Ned Flanders", "744 Evergreen Terrace", 60};
// conversion: person -> json
json j = p;
cout << j << endl;
// conversion: json -> person
auto p2 = j.get<ns::person>();
assert(p == p2);
}
int main()
{
create_first_json();
create_explicit_cases();
json j = deserialize();
serialize(j);
diff_serialization_and_assignment();
interact_with_iostream();
interact_with_fstream();
interact_with_iterator();
arbitrary_type_convert();
}