-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJson.cpp
More file actions
438 lines (422 loc) · 8.66 KB
/
Copy pathJson.cpp
File metadata and controls
438 lines (422 loc) · 8.66 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#include "Json.h"
#include "Parser.h"
import <stdexcept>;
import <sstream>;
using namespace Seazzz::json;
Json::Json() :m_type(json_null)//空类型赋值
{
}
Json::Json(bool value) :m_type(json_bool)//bool赋值
{
m_value.m_bool = value;
}
Json::Json(int value) :m_type(json_int)//int赋值
{
m_value.m_int = value;
}
Json::Json(double value) : m_type(json_double)//double赋值
{
m_value.m_double = value;
}
Json::Json(const char* value) : m_type(json_string)//string赋值
{
m_value.m_string = new std::string(value);
}
Json::Json(const std::string& value) :m_type(json_string) //string赋值
{
m_value.m_string = new std::string(value);
}
Json::Json(const Json& other)
{
//浅拷贝提高效率 将Json文件赋值。
m_type = other.m_type;
switch (m_type)
{
case json_null:
break;
case json_bool:
m_value.m_bool = other.m_value.m_bool;
case json_int:
m_value.m_int = other.m_value.m_int;
case json_double:
m_value.m_double = other.m_value.m_double;
case json_string:
m_value.m_string = other.m_value.m_string;
case json_array:
m_value.m_array = other.m_value.m_array;
case json_object:
m_value.m_map = other.m_value.m_map;
default:
break;
}
//深拷贝,问题少
//////m_type = other.m_type;
//switch (m_type)
//{
//case json_null:
// break;
//case json_bool:
// m_value.m_bool = other.m_value.m_bool;
//case json_int:
// m_value.m_int = other.m_value.m_int;
//case json_double:
// m_value.m_double = other.m_value.m_double;
//case json_string:
//{
// m_value.m_string = new std::string("");
// m_value.m_string = other.m_value.m_string;
//}
//case json_array:
//{
// m_value.m_array = new std::vector<Json>();
// m_value.m_array = other.m_value.m_array;
//}
//case json_object:
//{
// m_value.m_map = new std::map<std::string, Json>();
// m_value.m_map = other.m_value.m_map;
//
//}
//
//default:
// break;
//}
}
//Json 初始化为所有变量赋值
Json::Json(Type type) : m_type(type)
{
switch (m_type)
{
case json_null:
break;
case json_bool:
m_value.m_bool = false;
case json_int:
m_value.m_int = 0;
case json_double:
m_value.m_double = 0.0;
case json_string:
m_value.m_string = new std::string("");
case json_array:
m_value.m_array = new std::vector<Json>();
case json_object:
m_value.m_map = new std::map<std::string, Json>();
default:
break;
}
}
Json::~Json()
{
}
//重载操作bool类型
Json::operator bool()
{
if (m_type!= json_bool)
{
throw new std::logic_error("type error, not bool");
}
return m_value.m_bool;
}
Json::operator int()
{
if (m_type != json_int)
{
throw new std::logic_error("type error, not int");
}
return m_value.m_int;
}
Json::operator double()
{
if (m_type != json_double)
{
throw new std::logic_error("type error, not double");
}
return m_value.m_double;
}
Json::operator std::string()
{
if (m_type != json_string)
{
throw new std::logic_error("type error, not string");
}
return *(m_value.m_string);
}
Json & Json:: operator[](int index)
{
if (m_type != json_array)
{
m_type = json_array;
m_value.m_array = new std::vector<Json>();
}
if (index < 0)
{
throw new std::logic_error("array index > 0");
}
int size = (m_value.m_array)->size();
if (index >= size)
{
for (int i = size; i <= index; i++)
{
(m_value.m_array)->push_back(Json());
}
return (m_value.m_array)->at(index);
}
}
void Json::clear()//浅拷贝遇到问题,需要先释放内存
{
switch (m_type)
{
case json_null:
break;
case json_bool:
m_value.m_bool = false;
case json_int:
m_value.m_int = 0;
case json_double:
m_value.m_double = 0.0;
case json_string:
{
delete m_value.m_string;
break;
}
case json_array:
{
for (auto it = (m_value.m_array)->begin(); it != (m_value.m_array)->end(); it++)
{
it->clear();
}
delete m_value.m_array;
break;
}
case json_object:
{
for (auto it = (m_value.m_map)->begin(); it != (m_value.m_map)->end(); it++)
{
(it->second).clear();
}
delete m_value.m_array;
break;
}
default :
break;
}
m_type = json_null;
}
void Json::copy(const Json& other)
{
m_type = other.m_type;
switch (m_type)
{
case json_null:
break;
case json_bool:
m_value.m_bool = other.m_value.m_bool;
case json_int:
m_value.m_int = other.m_value.m_int;
case json_double:
m_value.m_double = other.m_value.m_double;
case json_string:
m_value.m_string = other.m_value.m_string;
case json_array:
m_value.m_array = other.m_value.m_array;
case json_object:
m_value.m_map = other.m_value.m_map;
default:
break;
}
}
void Json::operator =(const Json& other)
{
copy(other);
}
bool Json::operator ==(const Json& other)
{
if (m_type != other.m_type)
return false;
switch (m_type)
{
case json_null:
return true;
case json_bool:
return m_value.m_bool == other.m_value.m_bool;
case json_int:
return m_value.m_int == other.m_value.m_int;
case json_double:
return m_value.m_double == other.m_value.m_double;
case json_string:
return *(m_value.m_string) == *(other.m_value.m_string);
case json_array:
{
int size1 = (other.m_value.m_array)->size();
int size = (m_value.m_array)->size();
if (size1 != size)
return false;
auto it1 = (other.m_value.m_array)->begin();
for (auto it = (m_value.m_array)->begin(); it != (m_value.m_array)->end(); it++)
{
if (it != it1)
return false;
it1++;
}
return true;
}
case json_object:
return m_value.m_map == other.m_value.m_map;
default:
break;
}
return false;
}
bool Json::operator !=(const Json& other)
{
return !((*this) == other);
}
Json& Json:: operator[](const char* key)
{
std::string name(key);
return (*(this))[name];
}
Json& Json:: operator[](const std::string& key)
{
if (m_type != json_object)
{
clear();
m_type = json_object;
m_value.m_map = new std::map<std::string, Json>();
}
return(*(m_value.m_map))[key];
}
void Json::append(const Json& other)//为数组添加元素
{
if (m_type != json_array)
{
clear();
m_type = json_array;
m_value.m_array = new std::vector<Json>();
}
(m_value.m_array)->push_back(other);
}
std::string Json::str()const
{
std::stringstream ss;
switch (m_type)
{
case json_null:
ss << "NULL";
break;
case json_bool:
if (m_value.m_bool)
{
ss << "true";
}
else
{
ss << "false";
}
break;
case json_int:
ss << m_value.m_int;
break;
case json_double:
ss << m_value.m_double;
break;
case json_string:
ss << '\"' << *(m_value.m_string) << '\"';
break;
case json_array:
ss << "[";
ss << '\"';
for (auto it = (m_value.m_array)->begin(); it != (m_value.m_array)->end(); it++)
{
if (it == (m_value.m_array)->begin())
{
ss<<it->str();
}
else ss << "," << it->str();
}
ss << '\"';
ss << "]";
break;
case json_object:
ss << "{";
for (auto it1 = (m_value.m_map)->begin(); it1 != (m_value.m_map)->end(); it1++)
{
ss << '\"' << it1->first << "," << it1->second.str() << ";";
}
ss << "}";
break;
default:
break;
}
return ss.str();
}
bool Json::asBool()const
{
if (m_type != json_bool)
throw new std::logic_error("type error.");
return m_value.m_bool;
}
int Json::asInt()const
{
if (m_type != json_int)
throw new std::logic_error("type error.");
return m_value.m_int;
}
double Json::asDouble()const {
if (m_type != json_double)
throw new std::logic_error("type error.");
return m_value.m_double;
}
std::string Json::asString()const
{
if (m_type != json_string)
throw new std::logic_error("type error.");
return *(m_value.m_string);
}
bool Json::has(int index)
{
if (m_type != json_array)
return false;
int size = (m_value.m_array)->size();
return (index >= 0 && index <= size);
}
bool Json::has(const char* key)
{
std::string name(key);
return has(name);
}
bool Json::has(const std::string& key)
{
if (m_type != json_object)
{
return false;
}
return ((m_value.m_map)->find(key) != m_value.m_map->end());
}
void Json::remove(int index) {
if (m_type != json_array)
return ;
int size = (m_value.m_array)->size();
if (index || index >= size)
return;
(m_value.m_array)->at(index).clear();
(m_value.m_array)->erase((m_value.m_array)->begin() + index);
}
void Json::remove(const char* key)
{
std::string name(key);
remove(name);
}
void Json::remove(const std::string& key)
{
auto it = m_value.m_map->find(key);
if (it == m_value.m_map->end())
return;
(*(m_value.m_map))[key].clear();
(m_value.m_map)->erase(key);
}
void Json::parse(const std::string& str)
{
Parser p;
p.load(str);
}