forked from jwcpp/jwEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJson.cpp
More file actions
378 lines (340 loc) · 5.65 KB
/
Copy pathJson.cpp
File metadata and controls
378 lines (340 loc) · 5.65 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
#include "Json.h"
using namespace jwEngine;
void writeDepth(std::string & str, int depth)
{
for (int i = 0; i < depth; ++i)
{
str.append("\t");
}
}
JBaseObj::JBaseObj()
{
}
JBaseObj::~JBaseObj()
{
}
JValue::JValue()
{
}
JValue::~JValue()
{
}
void JValue::setInt(int v)
{
type = eJ_INT;
iv = v;
}
void JValue::setFloat(float v)
{
type = eJ_FLOAT;
fv = v;
}
void JValue::setStr(const char * v)
{
type = eJ_STRING;
sv = v;
}
void JValue::setBool(bool v)
{
type = eJ_BOOL;
bv = v;
}
void JValue::setNull()
{
type = eJ_NULL;
}
void JValue::write(std::string & str, bool layout, int depth)
{
switch (type)
{
case eJ_INT:
{
str.append(std::to_string(iv));
}
break;
case eJ_FLOAT:
{
str.append(std::to_string(fv));
}
break;
case eJ_BOOL:
{
if (bv)
{
str.append("true");
}
else
{
str.append("false");
}
}
break;
case eJ_NULL:
{
str.append("null");
}
break;
case eJ_STRING:
{
str.append("\"" + sv + "\"");
}
break;
default:
break;
}
}
JArray::JArray()
{
type = (int)eJ_ARRAY;
}
JArray::~JArray()
{
for (auto obj : items)
{
delete obj;
}
}
void JArray::pushInt(int v)
{
auto item = new JValue;
item->setInt(v);
items.push_back(item);
}
void JArray::pushFloat(float v)
{
auto item = new JValue;
item->setFloat(v);
items.push_back(item);
}
void JArray::pushStr(const char * v)
{
auto item = new JValue;
item->setStr(v);
items.push_back(item);
}
void JArray::pushBool(bool v)
{
auto item = new JValue;
item->setBool(v);
items.push_back(item);
}
void JArray::pushNull()
{
auto item = new JValue;
item->setNull();
items.push_back(item);
}
int JArray::getInt(int idx)
{
return static_cast<JValue *>(items[idx])->iv;
}
float JArray::getFloat(int idx)
{
return static_cast<JValue *>(items[idx])->fv;
}
bool JArray::getBool(int idx)
{
return static_cast<JValue *>(items[idx])->bv;
}
const char * JArray::getStr(int idx)
{
return static_cast<JValue *>(items[idx])->sv.c_str();
}
bool JArray::isNull(int idx)
{
return items[idx]->type == eJ_NULL;
}
void JArray::setInt(int idx, int v)
{
static_cast<JValue *>(items[idx])->setInt(v);
}
void JArray::setFloat(int idx, float v)
{
static_cast<JValue *>(items[idx])->setFloat(v);
}
void JArray::setStr(int idx, const char * v)
{
static_cast<JValue *>(items[idx])->setStr(v);
}
void JArray::setBool(int idx, bool v)
{
static_cast<JValue *>(items[idx])->setBool(v);
}
void JArray::setNull(int idx)
{
if (items[idx]->type == eJ_ARRAY || items[idx]->type == eJ_OBJ)
{
// need delete
delete items[idx];
items[idx] = new JValue;
}
static_cast<JValue *>(items[idx])->setNull();
}
void JArray::remove(int idx)
{
auto it = items.begin() + idx;
if (it != items.end())
{
delete *it;
items.erase(it);
}
}
int JArray::size()
{
return items.size();
}
JArray * JArray::createArray()
{
auto p = new JArray;
items.push_back(p);
return p;
}
JSonObj * JArray::createObj()
{
auto p = new JSonObj;
items.push_back(p);
return p;
}
JBaseObj * JArray::getJsonObj(int idx)
{
auto it = items.begin() + idx;
return *it;
}
void JArray::write(std::string & str, bool layout, int depth)
{
str.append("[");
if (layout) str.append("\n");
int count = 0, maxcount = items.size();;
for (auto & obj : items)
{
if (layout) writeDepth(str, depth + 1);
obj->write(str, layout, depth + 1);
if (++count < maxcount) {
str.append(",");
}
if (layout) str.append("\n");
}
if (layout) writeDepth(str, depth);
str.append("]");
}
JSonObj::JSonObj()
{
type = eJ_OBJ;
}
JSonObj::~JSonObj()
{
for (auto &[key, value] : maps)
{
delete value;
}
}
int JSonObj::getInt(std::string key)
{
return static_cast<JValue *>(maps[key])->iv;
}
float JSonObj::getFloat(std::string key)
{
return static_cast<JValue *>(maps[key])->fv;
}
bool JSonObj::getBool(std::string key)
{
return static_cast<JValue *>(maps[key])->bv;
}
const char * JSonObj::getStr(std::string key)
{
return static_cast<JValue *>(maps[key])->sv.c_str();
}
bool JSonObj::isNull(std::string key)
{
return maps[key]->type == eJ_NULL;
}
void JSonObj::setInt(std::string key, int v)
{
if (!maps[key])
{
maps[key] = new JValue;
}
static_cast<JValue *>(maps[key])->setInt(v);
}
void JSonObj::setFloat(std::string key, float v)
{
if (!maps[key])
{
maps[key] = new JValue;
}
static_cast<JValue *>(maps[key])->setFloat(v);
}
void JSonObj::setStr(std::string key, const char * v)
{
if (!maps[key])
{
maps[key] = new JValue;
}
static_cast<JValue *>(maps[key])->setStr(v);
}
void JSonObj::setBool(std::string key, bool v)
{
if (!maps[key])
{
maps[key] = new JValue;
}
static_cast<JValue *>(maps[key])->setBool(v);
}
void JSonObj::setNull(std::string key)
{
if (!maps[key])
{
maps[key] = new JValue;
}
else if (maps[key]->type == eJ_ARRAY || maps[key]->type == eJ_OBJ)
{
// need delete
delete maps[key];
maps[key] = new JValue;
}
static_cast<JValue *>(maps[key])->setNull();
}
void JSonObj::remove(std::string key)
{
auto it = maps.find(key);
if (it != maps.end())
{
delete it->second;
maps.erase(it);
}
}
JArray * JSonObj::createArray(std::string key)
{
auto p = new JArray;
maps[key] = p;
return p;
}
JSonObj * JSonObj::createObj(std::string key)
{
auto p = new JSonObj;
maps[key] = p;
return p;
}
JBaseObj * JSonObj::getJsonObj(std::string key)
{
return maps[key];
}
void JSonObj::write(std::string & str, bool layout, int depth)
{
str.append("{");
if (layout) str.append("\n");
int count = 0, maxcount = maps.size();
for (auto &[key, obj] : maps)
{
if (layout) writeDepth(str, depth + 1);
str.append("\"" + key + "\"" + ":");
obj->write(str, layout, depth + 1);
if (++count < maxcount)
{
str.append(",");
}
if (layout) str.append("\n");
}
if (layout) writeDepth(str, depth);
str.append("}");
}