-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtJson.cpp
More file actions
731 lines (687 loc) · 21.1 KB
/
ExtJson.cpp
File metadata and controls
731 lines (687 loc) · 21.1 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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
#include "ExtJson.h"
#ifdef IHCore
#include "..\..\IHCore\ExtFile.h"
#include "..\..\IHCore\ExtLog.h"
#endif
bool _IH_IsTrueString(const std::string& s1)
{
return s1 == "true" || s1 == "yes" || s1 == "1" || s1 == "T" || s1 == "True" || s1 == "Yes" || s1 == "t" || s1 == "Y" || s1 == "y";
}
std::vector<int> cJSON_GetVectorInt(cJSON* Array)
{
int N = cJSON_GetArraySize(Array);
cJSON* Item = cJSON_GetArrayItem(Array, 0);
std::vector<int> Ret;
Ret.reserve(N);
for (int i = 0; i < N; i++)
{
Ret.push_back(Item->valueint);
Item = Item->next;
}
return Ret;
}
std::vector<uint8_t> cJSON_GetVectorBool(cJSON* Array)
{
int N = cJSON_GetArraySize(Array);
cJSON* Item = cJSON_GetArrayItem(Array, 0);
std::vector<uint8_t> Ret;
Ret.reserve(N);
for (int i = 0; i < N; i++)
{
Ret.push_back(((Item->type & 0xFF) == cJSON_True) ? true : false);
Item = Item->next;
}
return Ret;
}
std::vector<std::string> cJSON_GetVectorString(cJSON* Array)
{
int N = cJSON_GetArraySize(Array);
cJSON* Item = cJSON_GetArrayItem(Array, 0);
std::vector<std::string> Ret;
Ret.reserve(N);
for (int i = 0; i < N; i++)
{
Ret.push_back(Item->valuestring);
Item = Item->next;
}
return Ret;
}
std::vector<cJSON*> cJSON_GetVectorObject(cJSON* Array)
{
int N = cJSON_GetArraySize(Array);
cJSON* Item = cJSON_GetArrayItem(Array, 0);
std::vector<cJSON*> Ret;
Ret.reserve(N);
for (int i = 0; i < N; i++)
{
Ret.push_back(Item);
Item = Item->next;
}
return Ret;
}
void cJson_SwapData(cJSON* A, cJSON* B)
{
if (A == nullptr || B == nullptr)return;
std::swap(*A, *B);
std::swap(A->next, B->next);
}
#define ____GET____ auto Obj = GetObjectItem(Str)
int JsonObject::ItemInt(const std::string& Str) const { ____GET____; return Obj.GetInt(); }
double JsonObject::ItemDouble(const std::string& Str) const { ____GET____; return Obj.GetDouble(); }
std::string JsonObject::ItemString(const std::string& Str) const { ____GET____; return Obj.GetString(); }
const char* JsonObject::ItemCString(const std::string& Str) const { ____GET____; return Obj.GetCString(); }
bool JsonObject::ItemBool(const std::string& Str) const { ____GET____; return Obj.GetBool(); }
bool JsonObject::ItemStrBool(const std::string& Str) const { ____GET____; return Obj.GetStrBool(); }
size_t JsonObject::ItemArraySize(const std::string& Str) const { ____GET____; return Obj.GetArraySize(); }
std::vector<int> JsonObject::ItemArrayInt(const std::string& Str) const { ____GET____; return Obj.GetArrayInt(); }
std::vector<uint8_t> JsonObject::ItemArrayBool(const std::string& Str) const { ____GET____; return Obj.GetArrayBool(); }
std::vector<std::string> JsonObject::ItemArrayString(const std::string& Str) const { ____GET____; return Obj.GetArrayString(); }
std::vector<JsonObject> JsonObject::ItemArrayObject(const std::string& Str) const { ____GET____; return Obj.GetArrayObject();}
#undef ____GET____
JsonObject JsonObject::GetObjectItem(const std::string& Str) const
{
auto pObj = cJSON_GetObjectItem(Object, Str.c_str());
return { pObj };
}
std::string JsonObject::GetText() const
{
if (Object == nullptr) return "";
char* CStr = cJSON_Print(Object);
std::string Str = CStr;
cJSON_Free(CStr);
return Str;
}
std::string JsonObject::GetCompactText() const
{
if (Object == nullptr) return "";
char* CStr = cJSON_PrintUnformatted(Object);
std::string Str = CStr;
cJSON_Free(CStr);
return Str;
}
std::string ProcessJsonText(const std::string& json)
{
std::string result = "\033[1;33m";
bool inString = false; // 是否在字符串内部
int backslashCount = 0; // 连续反斜杠的数量
for (size_t i = 0; i < json.size(); ++i) {
char current = json[i];
// 处理反斜杠转义
if (current == '\\') {
if (inString) {
backslashCount++;
}
result.push_back(current);
continue;
}
// 重置反斜杠计数(只有在字符串内部时才考虑转义)
if (inString) {
backslashCount = backslashCount % 2 == 0 ? backslashCount / 2 : (backslashCount + 1) / 2;
}
else {
backslashCount = 0;
}
// 处理引号
if (current == '"') {
// 检查是否被转义(奇数个反斜杠表示被转义)
if (backslashCount % 2 == 0) {
// 未被转义的引号
if (!inString) {
// 左引号
result += "\033[1;32m";
inString = true;
}
else {
// 右引号
inString = false;
result += current;
result += "\033[1;33m";
backslashCount = 0; // 重置反斜杠计数
continue; // 跳过添加当前引号
}
}
else {
// 被转义的引号
backslashCount = 0; // 重置反斜杠计数
}
}
result.push_back(current);
backslashCount = 0; // 重置反斜杠计数
}
result += "\033[0m";
return result;
}
std::string JsonObject::GetTextEx() const
{
return ProcessJsonText(GetText());
}
std::vector<JsonObject> JsonObject::GetArrayObject() const
{
int N = cJSON_GetArraySize(Object);
cJSON* Item = cJSON_GetArrayItem(Object, 0);
std::vector<JsonObject> Ret;
Ret.reserve(N);
for (int i = 0; i < N; i++)
{
Ret.push_back(JsonObject{ Item });
Item = Item->next;
}
return Ret;
}
std::unordered_map<std::string, JsonObject> JsonObject::GetMapObject() const
{
std::unordered_map<std::string, JsonObject> Ret;
JsonObject Node = GetChildItem();
while (Node.Available())
{
Ret.insert({ Node.GetName(),Node });
Node = Node.GetNextItem();
}
return Ret;
}
std::unordered_map<std::string, double> JsonObject::GetMapDouble() const
{
std::unordered_map<std::string, double> Ret;
JsonObject Node = GetChildItem();
while (Node.Available())
{
Ret.insert({ Node.GetName(),Node.GetDouble() });
Node = Node.GetNextItem();
}
return Ret;
}
std::unordered_map<std::string, int> JsonObject::GetMapInt() const
{
std::unordered_map<std::string, int> Ret;
JsonObject Node = GetChildItem();
while (Node.Available())
{
Ret.insert({ Node.GetName(),Node.GetInt() });
Node = Node.GetNextItem();
}
return Ret;
}
std::unordered_map<std::string, bool> JsonObject::GetMapBool() const
{
std::unordered_map<std::string, bool> Ret;
JsonObject Node = GetChildItem();
while (Node.Available())
{
Ret.insert({ Node.GetName(),Node.GetBool() });
Node = Node.GetNextItem();
}
return Ret;
}
std::unordered_map<std::string, std::string> JsonObject::GetMapString() const
{
std::unordered_map<std::string, std::string> Ret;
JsonObject Node = GetChildItem();
while (Node.Available())
{
Ret.insert({ Node.GetName(),Node.GetString() });
Node = Node.GetNextItem();
}
return Ret;
}
std::vector < std::pair<std::string, JsonObject>> JsonObject::GetPVObject() const
{
std::vector < std::pair<std::string, JsonObject>> Ret;
JsonObject Node = GetChildItem();
while (Node.Available())
{
Ret.push_back({ Node.GetName(),Node });
Node = Node.GetNextItem();
}
return Ret;
}
std::vector < std::pair<std::string, double>> JsonObject::GetPVDouble() const
{
std::vector < std::pair<std::string, double>> Ret;
JsonObject Node = GetChildItem();
while (Node.Available())
{
Ret.push_back({ Node.GetName(),Node.GetDouble() });
Node = Node.GetNextItem();
}
return Ret;
}
std::vector < std::pair<std::string, int>> JsonObject::GetPVInt() const
{
std::vector < std::pair<std::string, int>> Ret;
JsonObject Node = GetChildItem();
while (Node.Available())
{
Ret.push_back({ Node.GetName(),Node.GetInt() });
Node = Node.GetNextItem();
}
return Ret;
}
std::vector < std::pair<std::string, bool>> JsonObject::GetPVBool() const
{
std::vector < std::pair<std::string, bool>> Ret;
JsonObject Node = GetChildItem();
while (Node.Available())
{
Ret.push_back({ Node.GetName(),Node.GetBool() });
Node = Node.GetNextItem();
}
return Ret;
}
std::vector < std::pair<std::string, std::string>> JsonObject::GetPVString() const
{
std::vector < std::pair<std::string, std::string>> Ret;
JsonObject Node = GetChildItem();
while (Node.Available())
{
Ret.push_back({ Node.GetName(),Node.GetString() });
Node = Node.GetNextItem();
}
return Ret;
}
void JsonFile::Parse(std::string Str)
{
ParseTmp(std::move(Str));
}
std::string JsonFile::ParseChecked(std::string Str, const std::string& ErrorTip, int* ErrorPosition)
{
return std::move(ParseTmpChecked(std::move(Str), ErrorTip, ErrorPosition));
}
void JsonFile::ParseWithOpts(std::string Str, const char** ReturnParseEnd, int RequireNullTerminated)
{
ParseTmpWithOpts(std::move(Str), ReturnParseEnd, RequireNullTerminated);
}
void JsonFile::ParseTmp(std::string&& Str)
{
/*
if (EnableLogEx)
{
GlobalLogB.AddLog_CurTime(false);
sprintf_s(LogBufB, "JsonFile::Parse <- std::string Str=%s", Str.c_str()); GlobalLogB.AddLog(LogBufB);
}
*/
cJSON_Minify(Str.data());
Clear();
File = cJSON_Parse(Str.data());
}
std::string JsonFile::ParseTmpChecked(std::string&& Str, const std::string& ErrorTip, int* ErrorPosition)
{
const char* ErrorPtr = nullptr;
std::string Str2 = Str;
cJSON_Minify(Str.data());
File = cJSON_Parse(Str.data());
if (!File)ErrorPtr = cJSON_GetErrorPtr();
if (!File && ErrorPtr)
{
for (size_t i = 0, j = 0; i < Str2.size() && Str[j]; i++)
{
if (Str2[i] == Str[j])
{
if (Str.c_str() + j + 1 == ErrorPtr)
{
Str2.insert(i + 1, ErrorTip);
if (ErrorPosition)*ErrorPosition = i + 1;
break;
}
++j;
}
}
return Str2;
}
else
{
return "";
}
}
void JsonFile::ParseTmpWithOpts(std::string&& Str, const char** ReturnParseEnd, int RequireNullTerminated)
{
/*
if (EnableLogEx)
{
GlobalLogB.AddLog_CurTime(false);
sprintf_s(LogBufB, "JsonFile::ParseWithOpts <- std::string Str=%s, int RequireNullTerminated=%d", Str.c_str(), RequireNullTerminated); GlobalLogB.AddLog(LogBufB);
}
*/
cJSON_Minify(Str.data());
Clear();
File = cJSON_ParseWithOpts(Str.data(), ReturnParseEnd, RequireNullTerminated);
}
#ifdef IHCore
void JsonFile::ParseFromFile(const char* FileName)
{
/*
if (EnableLogEx)
{
GlobalLogB.AddLog_CurTime(false);
sprintf_s(LogBufB, "JsonFile::ParseFromFile <- char* FileName=%s", FileName); GlobalLogB.AddLog(LogBufB);
}
*/
Clear();
ExtFileClass Fp;
if (!Fp.Open(FileName, "r"))return;
Fp.Seek(0, SEEK_END);
int Pos = Fp.Position();
char* FileStr;
FileStr = new char[Pos + 50];
if (FileStr == nullptr)return;
Fp.Seek(0, SEEK_SET);
Fp.Read(FileStr, 1, Pos);
FileStr[Pos] = 0;
Fp.Close();
int iPos = 0;
while (FileStr[iPos] != '{' && iPos < Pos)iPos++;
if (iPos == Pos) { delete[]FileStr; return; }
cJSON_Minify(FileStr + iPos);
File = cJSON_Parse(FileStr + iPos);
/*
if (EnableLogEx)
{
GlobalLogB.AddLog_CurTime(false);
GlobalLogB.AddLog("JsonFile::ParseFromFile : JSON文本:");
GlobalLogB.AddLog_CurTime(false);
GlobalLogB.AddLog("\"", false);
GlobalLogB.AddLog(UTF8toANSI(FileStr + iPos).c_str(), false);
GlobalLogB.AddLog("\"");
}*/
delete[]FileStr;
}
void JsonFile::ParseFromFileWithOpts(const char* FileName, int RequireNullTerminated)
{
/*if (EnableLogEx)
{
GlobalLogB.AddLog_CurTime(false);
sprintf_s(LogBufB, "JsonFile::ParseFromFile <- char* FileName=%s, int RequireNullTerminated=%d", FileName, RequireNullTerminated); GlobalLogB.AddLog(LogBufB);
}*/
Clear();
ExtFileClass Fp;
if (!Fp.Open(FileName, "r"))return;
Fp.Seek(0, SEEK_END);
int Pos = Fp.Position();
char* FileStr;
FileStr = new char[Pos + 50];
if (FileStr == nullptr)return;
Fp.Seek(0, SEEK_SET);
Fp.Read(FileStr, 1, Pos);
FileStr[Pos] = 0;
Fp.Close();
int iPos = 0;
while (FileStr[iPos] != '{' && iPos < Pos)iPos++;
if (iPos == Pos) { delete[]FileStr; return; }
const char* ReturnParseEnd;
cJSON_Minify(FileStr + iPos);
File = cJSON_ParseWithOpts(FileStr + iPos, &ReturnParseEnd, RequireNullTerminated);
/*if (EnableLogEx)
{
GlobalLogB.AddLog_CurTime(false);
GlobalLogB.AddLog("JsonFile::ParseFromFileWithOpts : JSON文本:");
GlobalLogB.AddLog_CurTime(false);
GlobalLogB.AddLog("\"", false);
GlobalLogB.AddLog(UTF8toANSI(FileStr + iPos).c_str(), false);
GlobalLogB.AddLog("\"");
}*/
delete[]FileStr;
}
#endif
const char* const StrBool_TrueList[]
{
"true","True","TRUE",
"yes","Yes","YES",
"t","T","y","Y","1"
};
const char* const StrBool_FalseList[]
{
"false","False","FALSE",
"no","No","NO",
"f","F","n","N","0"
};
std::string StrBoolImpl(bool Val, StrBoolType Type)
{
return std::string{ CStrBoolImpl(Val, Type) };
}
const char* CStrBoolImpl(bool Val, StrBoolType Type)
{
return (Val ? StrBool_TrueList : StrBool_FalseList)[(size_t)Type];
}
JsonObject JsonObject::CreateObjectItem(const std::string& Str) const
{
auto ObjPtr = cJSON_CreateNull();
cJSON_AddItemToObject(Object, Str.c_str(), ObjPtr);
return JsonObject(ObjPtr);
}
void JsonObject::AddObjectItem(const std::string& Str, JsonObject Child, bool NeedsCopy) const
{
cJSON_AddItemToObject(Object, Str.c_str(), (NeedsCopy ? cJSON_Duplicate(Child.GetRaw(), true) : Child.GetRaw()));
}
void JsonObject::AddObjectItem(const std::string& Str, JsonFile&& File) const
{
cJSON_AddItemToObject(Object, Str.c_str(), File.Release());
}
void JsonObject::AddNull(const std::string& Str) const
{
cJSON_AddNullToObject(Object, Str.c_str());
}
void JsonObject::AddInt(const std::string& Str, int Val) const
{
cJSON_AddIntegerToObject(Object, Str.c_str(), Val);
}
void JsonObject::AddDouble(const std::string& Str, double Val) const
{
cJSON_AddNumberToObject(Object, Str.c_str(), Val);
}
void JsonObject::AddString(const std::string& Str, const std::string& Val) const
{
cJSON_AddStringToObject(Object, Str.c_str(), Val.c_str());
}
void JsonObject::AddBool(const std::string& Str, bool Val) const
{
cJSON_AddBoolToObject(Object, Str.c_str(), Val);
}
void JsonObject::AddStrBool(const std::string& Str, bool Val, StrBoolType Type) const
{
cJSON_AddStringToObject(Object, Str.c_str(), CStrBoolImpl(Val, Type));
}
void JsonObject::AddArrayString(const std::string& Str, const std::vector<std::string>& Vec) const
{
std::vector<const char*> CStrs;
CStrs.reserve(Vec.size());
for (const auto& s : Vec)
{
CStrs.push_back(s.c_str());
}
auto Obj = cJSON_CreateStringArray(CStrs.data(), (int)Vec.size());
cJSON_AddItemToObject(Object, Str.c_str(), Obj);
}
void JsonObject::AddArrayObject(const std::string& Str, std::vector<JsonFile>&& Vec) const
{
auto Obj = cJSON_CreateObjectArray(reinterpret_cast<cJSON**>(Vec.data()), static_cast<int>(Vec.size()));
for (auto& f : Vec) f.Release();
Vec.clear();
cJSON_AddItemToObject(Object, Str.c_str(), Obj);
}
JsonFile JsonObject::SwapNull() const
{
JsonFile F(cJSON_CreateNull());
cJson_SwapData(F.GetRaw(), Object);
return F;
}
JsonFile JsonObject::SwapInt(int Val) const
{
JsonFile F(cJSON_CreateInteger(Val));
cJson_SwapData(F.GetRaw(), Object);
return F;
}
JsonFile JsonObject::SwapDouble(double Val) const
{
JsonFile F(cJSON_CreateNumber(Val));
cJson_SwapData(F.GetRaw(), Object);
return F;
}
JsonFile JsonObject::SwapString(const std::string& Val) const
{
JsonFile F(cJSON_CreateString(Val.c_str()));
cJson_SwapData(F.GetRaw(), Object);
return F;
}
JsonFile JsonObject::SwapBool(bool Val) const
{
JsonFile F(cJSON_CreateBool(Val));
cJson_SwapData(F.GetRaw(), Object);
return F;
}
JsonFile JsonObject::SwapStrBool(bool Val, StrBoolType Type) const
{
JsonFile F(cJSON_CreateString(CStrBoolImpl(Val, Type)));
cJson_SwapData(F.GetRaw(), Object);
return F;
}
JsonFile JsonObject::CopyAndSwap(JsonObject Obj, bool Recurse) const
{
JsonFile F(cJSON_Duplicate(Obj.GetRaw(), Recurse));
cJson_SwapData(F.GetRaw(), Object);
return F;
}
JsonFile JsonObject::RedirectAndSwap(JsonObject Obj)
{
JsonFile F(this->GetRaw());
Object = Obj.GetRaw();
return F;
}
void JsonObject::SwapObject(JsonObject Obj) const
{ cJson_SwapData(Obj.GetRaw(), Object); }
void JsonObject::SetNull() const
{ SwapNull(); }
void JsonObject::SetInt(int Val) const
{ SwapInt(Val); }
void JsonObject::SetDouble(double Val) const
{ SwapDouble(Val); }
void JsonObject::SetString(const std::string& Val) const
{ SwapString(Val); }
void JsonObject::SetBool(bool Val) const
{ SwapBool(Val); }
void JsonObject::SetStrBool(bool Val, StrBoolType Type) const
{ SwapStrBool(Val, Type); }
void JsonObject::CopyObject(JsonObject Obj, bool Recurse) const
{ CopyAndSwap(Obj, Recurse); }
void JsonObject::RedirectObject(JsonObject Obj)
{ RedirectAndSwap(Obj); }
void JsonObject::SetObject()
{
JsonFile F(cJSON_CreateObject());
cJson_SwapData(F.GetRaw(), Object);
}
//给空节点的非const函数
void JsonObject::CreateNull()
{
Object = cJSON_CreateNull();
}
void JsonObject::CreateInt(int Val)
{
Object = cJSON_CreateInteger(Val);
}
void JsonObject::CreateDouble(double Val)
{
Object = cJSON_CreateNumber(Val);
}
void JsonObject::CreateString(const std::string& Val)
{
Object = cJSON_CreateString(Val.c_str());
}
void JsonObject::CreateBool(bool Val)
{
Object = cJSON_CreateBool(Val);
}
void JsonObject::CreateStrBool(bool Val, StrBoolType Type)
{
Object = cJSON_CreateString(CStrBoolImpl(Val, Type));
}
void JsonObject::CreateCopy(JsonObject Obj, bool Recurse)
{
Object = cJSON_Duplicate(Obj.GetRaw(), Recurse);
}
void JsonObject::CreateObject()
{
Object = cJSON_CreateObject();
}
// 不知道是否为空的函数
void JsonObject::SetOrCreateNull()
{
if (Available())SetNull();
else CreateNull();
}
void JsonObject::SetOrCreateInt(int Val)
{
if (Available())SetInt(Val);
else CreateInt(Val);
}
void JsonObject::SetOrCreateDouble(double Val)
{
if (Available())SetDouble(Val);
else CreateDouble(Val);
}
void JsonObject::SetOrCreateString(const std::string& Val)
{
if (Available())SetString(Val);
else CreateString(Val);
}
void JsonObject::SetOrCreateBool(bool Val)
{
if (Available())SetBool(Val);
else CreateBool(Val);
}
void JsonObject::SetOrCreateStrBool(bool Val, StrBoolType Type)
{
if (Available())SetStrBool(Val, Type);
else CreateStrBool(Val, Type);
}
void JsonObject::SetOrCreateCopy(JsonObject Obj, bool Recurse)
{
if (Available())CopyObject(Obj, Recurse);
else CreateCopy(Obj, Recurse);
}
void JsonObject::SetOrCreateObject()
{
if (Available())SetObject();
else CreateObject();
}
JsonFile JsonObject::DetachObjectItem(const std::string& Str) { return cJSON_DetachItemFromObject(Object, Str.c_str()); }
JsonFile JsonObject::DetachArrayItem(int Index) { return cJSON_DetachItemFromArray(Object, Index); }
void JsonObject::Merge(JsonObject Obj)
{
if (!Obj.Available())return;
if (!Available())
{
CreateCopy(Obj, true);
return;
}
if (!Obj.IsTypeObject())
{
CopyObject(Obj, true);
}
else
{
JsonObject Node = Obj.GetChildItem();
while (Node.Available())
{
auto Item = GetObjectItem(Node.GetName());
bool PrevAvailable = Item.Available();
Item.Merge(Node);
if (!PrevAvailable)AddObjectItem(Node.GetName(), Item, false);
Node = Node.GetNextItem();
}
}
}
extern "C" char* print_string_raw(const char* str);
std::string EscapeString(const std::string& str)
{
auto cs = print_string_raw(str.c_str());
std::string ret;
if (cs)
{
ret = cs;
cJSON_Free(cs);
}
else
{
ret = str;
}
return ret;
}