-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJSONStudy.h
More file actions
282 lines (239 loc) · 5.5 KB
/
Copy pathJSONStudy.h
File metadata and controls
282 lines (239 loc) · 5.5 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#pragma once
#include "NLOHMANN_JSON.h"
#include <iostream>
#include <fstream>
using namespace std;
using json = nlohmann::json;
//https://github.com/nlohmann/json
namespace JSONSTUDY {
void testJsonRead()
{
while (true)
{
json j; //创建json类
cin >> j; //从cin读入json对象
cout << j; //输出序列化的json
}
}
/*
{
"pi": 3.141,
"happy" : true,
"name" : "Niels",
"nothing" : null,
"answer" : {
"everything": 42
},
"list" : [1, 0, 2],
"object" : {
"currency": "USD",
"value" : 42.99
}
}
*/
void JsonCreateMethod1()
{
json j;
j["pi"] = 3.141;
j["happy"] = true;
j["name"] = "Niels";
j["nothing"] = nullptr;
j["answer"]["everything"] = 42;
j["list"] = { 1,2,3 };
j["object"] = { {"currency","USD"},{"value",42.99} };
//j.push_back(j);
cout << j.dump() << endl;;
//cout << j["pi"] << endl;
json::iterator it = j.begin();
cout << it.key() << " " << it.value() << endl;
if (j["dd"] == NULL)
cout << "NULL" << endl;
if (j["dd"] == nullptr)
cout << "nullptr" << endl;
}
void JsonCreateMethod2()
{
json j2 = {
{ "pi", 3.141 },
{ "happy" , true },
{ "name" , "Niels" },
{ "nothing",nullptr },
{ "answer",{
{ "everything",42 }
},
{ "list" ,{ 1, 0, 2 } },
{ "object" ,{
{ "currency", "USD" },
{ "value",42.99 }
}
}
}
};
//j2.push_back(j2);
cout<<j2.dump();
}
void JsonCreateMethod3()
{
// a way to express the empty array []
json empty_array_explicit = json::array();
// ways to express the empty object {}
json empty_object_implicit = json({});
json empty_object_explicit = json::object();
// a way to express an _array_ of key/value pairs [["currency", "USD"], ["value", 42.99]]
json array_not_object = json::array({ { "currency", "USD" },{ "value", 42.99 } });
}
void FromStringTOJsonObject()
{
// create object from string literal,
// if without _json, the stringliteral is not parsed to object.
json j = "{ \"happy\": true, \"pi\": 3.141 }"_json;
json j1 = json::parse("{ \"happy\": true, \"pi\": 3.141 }");
// or even nicer with a raw string literal
auto j2 = R"(
{
"happy": true,
"pi": 3.141
}
)"_json;
}
void stringSerialized()
{
json j = "this is a string";
// explicit conversion to string
std::string s = j.dump();
std::cout << j.dump(2) << std::endl;
std::string cpp_string2;
j.get_to(cpp_string2);
//assignmen
auto cpp_string = j.get<std::string>();
cout <<"j.dump="<< s << endl;
cout << "j.get_to()=" << cpp_string2 << endl;
cout << "j.get()=" << cpp_string << endl;
}
void fileDeserialize()
{
// read a JSON file
std::ifstream i("file.json");
json j;
i >> j;
// write prettified JSON to another file
std::ofstream o("pretty.json");
o << std::setw(4) << j << std::endl;
}
void iteratorRead()
{
json j2 = {
{ "pi", 3.141 },
{ "happy" , true },
{ "name" , "Niels" },
{ "nothing","nothing" },
{ "list" ,{ 1, 0, 2 } }
};
for (json::iterator it = j2.begin(); it != j2.end(); ++it) {
std::cout << it.key() << " : " << it.value() << "\n";
}
}
//自定义结构,需要重载如下函数方法进行序列化和反序列化
namespace nlohmann {
struct Person {
string name;
int age;
};
struct Persons
{
vector<Person> persons;
};
//重载如下函数方法进行序列化和反序列化
void to_json(json& j, const Person& p) {
j = json{ {"name",p.name},{"age",p.age} }; //设置 结构中的字段与json中的key对照关系,name和age可以随意改成我们想映射的名字,最终显示到json字符串里面。
}
//重载如下函数方法进行序列化和反序列化
void from_json(const json& j, Person& p) {
p.name = j.at("name").get<string>();
p.age = j.at("age").get<int>();
}
//重载如下函数方法进行序列化和反序列化
void to_json(json& j, const Persons& ps) {
j = json{ { "persons", ps.persons } };
}
//重载如下函数方法进行序列化和反序列化
void from_json(const json& j, Persons& ps) {
ps.persons = j.at("persons").get<vector<Person>>();
}
}
int TestObjectToSerial()
{
string jsonString = R"(
{
"name" : "Joe",
"age" : 12
}
)";
using namespace nlohmann;
json j = json::parse(jsonString);
Person p = j;
cout << "name: " << p.name << endl
<< "age: " << p.age << endl;
j = p;
cout << j << endl;
cout << j.dump(3) << endl;
return 0;
}
int TestObjectToSerial2()
{
string jsonString = R"({
"persons" : [
{
"name" : "Joe",
"age" : 12
},
{
"name" : "Joe2",
"age" : 122
}
]
})";
using namespace nlohmann;
json j = json::parse(jsonString);
Persons ps = j;
auto &p = ps.persons[0];
cout << "name: " << p.name << endl
<< "age: " << p.age << endl;
auto &p2 = ps.persons[1];
cout << "name: " << p2.name << endl
<< "age: " << p2.age << endl;
j = ps;
cout << j << endl;
cout << j.dump(2) << endl;
return 0;
}
struct Person2 {
string name;
int age;
};
void to_json(json& j, const Person2& p) {
j = json{ { "name",p.name },{ "age",p.age } }; //设置 结构中的字段与json中的key对照关系,name和age可以随意改成我们想映射的名字,最终显示到json字符串里面。
}
void from_json(const json& j, Person2& p) {
p.name = j.at("name").get<string>();
p.age = j.at("age").get<int>();
}
int TestObjectToSeria3()
{
string jsonString = R"(
{
"name" : "Joe",
"age" : 12
}
)";
//using namespace nlohmann;
json j = json::parse(jsonString);
Person2 p = j;
cout << "name: " << p.name << endl
<< "age: " << p.age << endl;
j = p;
cout << j << endl;
cout << j.dump(3) << endl;
return 0;
}
}