Repository navigation
Expand file tree
/
Copy pathJson.h
More file actions
683 lines (589 loc) · 10.8 KB
/
Copy pathJson.h
File metadata and controls
683 lines (589 loc) · 10.8 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
#ifndef _LJSON_H_
#define _LJSON_H_
#include "lgi/common/Array.h"
#include "lgi/common/StringClass.h"
#define SkipWs(s) while (*s && strchr(" \t\r\n", *s)) s++;
class LJson
{
constexpr static const char *EscapeChars = "\"\\\r\n\t\b";
struct Key
{
// Option name for value
LString Name;
// One of these should be valid:
// 1) A string value
LString Str;
// 2) An object definition
LArray<Key> Obj;
// 3) An array of something
LArray<Key> Array;
// end
Key *Get(const char *name, bool create = false)
{
if (Name.Equals(name))
return this;
for (unsigned i=0; i<Obj.Length(); i++)
{
if (Obj[i].Get(name))
return Obj.AddressOf(i);
}
if (create)
{
char *ArrStart = strchr(name, '[');
char *ArrEnd = ArrStart ? strchr(ArrStart+1, ']') : NULL;
if (ArrStart && ArrEnd)
{
int Idx = atoi(ArrStart + 1);
if (Idx < 0)
return NULL;
LString n;
n.Set(name, ArrStart - name);
if (n.IsEmpty())
{
return &Array[Idx];
}
for (unsigned i=0; i<Obj.Length(); i++)
{
if (Obj[i].Get(n))
return &(Obj[i].Array[Idx]);
}
Key &k = Obj.New();
k.Name = n;
return &k.Array[Idx];
}
Key &k = Obj.New();
k.Name = name;
return &k;
}
return NULL;
}
void Empty()
{
Name.Empty();
Str.Empty();
Obj.Empty();
Array.Empty();
}
LString MakeIndent(int d)
{
return LString(" ") * (d<<1);
}
LString Print(int Depth = 0)
{
LStringPipe r(512);
LString indent = MakeIndent(Depth);
if (Name)
{
r.Print("%s\"%s\": ", indent.Get(), Name.Get());
}
if (!Str && !Obj.Length() && !Array.Length())
{
r.Print("null");
}
else if (Str)
{
auto q = LString::Escape(Str, Str.Length(), EscapeChars, 'u');
r.Print("\"%s\"", q.Get());
}
else if (Obj.Length())
{
r.Print("%s{\n", indent.Get());
Depth++;
for (size_t i=0; i<Obj.Length(); i++)
{
if (i)
r.Print(",\n");
auto &e = Obj[i];
r.Write(e.Print(Depth));
}
r.Print("\n%s}", indent.Get());
Depth--;
}
else
{
// bool isObjArray = Array.Length() ? Array[0].Obj.Length() > 0 : false;
if (Array.Length() == 0)
{
r.Write("[]", 2);
}
else if (Array[0].Obj.Length())
{
// Print array of objects
r.Print("[\n");
for (size_t i=0; i<Array.Length(); i++)
{
if (i) r.Print(",\n");
r.Write(Array[i].Print(Depth+1));
}
r.Print("\n%s]", indent.Get());
}
else if (Array.Length() == 1)
{
// Print single string
r.Print("[ ");
r.Write(Array[0].Print(0));
r.Print(" ]");
}
else
{
// Print multiple strings, each on their own line.
r.Write("[", 1);
auto arrIndent = MakeIndent(Depth + 1);
for (unsigned i=0; i<Array.Length(); i++)
{
r.Print("\n%s", arrIndent.Get());
auto &e = Array[i];
r.Write(e.Print(Depth));
}
r.Print("\n%s]", indent.Get());
}
}
return r.NewLStr();
}
Key *Deref(LString Addr, bool Create)
{
LString::Array p = Addr.SplitDelimit(".");
Key *k = this;
for (unsigned i=0; k && i<p.Length(); i++)
{
auto n = p[i];
k = k->Get(n, Create);
}
return k;
}
};
Key Root;
const char *Start;
bool ParseString(LString &s, const char *&c)
{
SkipWs(c);
if (*c != '\"')
return false;
c++;
const char *e = c;
while (*e)
{
if (*e == '\\')
e += 2;
else if (*e == '\"')
break;
else
e++;
}
if (*e != '\"')
return false;
s = LString::UnEscape(c, e - c);
c = e + 1;
return true;
}
bool ParseChar(char ch, const char *&c)
{
SkipWs(c);
if (*c != ch)
return false;
c++;
SkipWs(c);
return true;
}
bool IsNumeric(char s)
{
return IsDigit(s) || strchr("-.e", s) != NULL;
}
bool ParseArray(LArray<Key> &Array, const char *&c)
{
if (*c != '[')
{
LAssert(0);
return false;
}
c++;
SkipWs(c);
while (*c != ']')
{
auto Idx = Array.Length();
Key &a = Array[Idx];
if (!Parse(a, c))
{
Array.Length(Idx); // Delete failed object
return false;
}
SkipWs(c);
if (*c == ',')
{
c++;
SkipWs(c);
}
}
if (*c != ']')
{
LAssert(!"Unexpected token.");
return false;
}
c++;
return true;
}
// Value parser: can be one of:
// - Object
// - Array
// - String
// - Number
// - Boolean (true or false)
// - NULL
bool Parse(Key &k, const char *&c)
{
SkipWs(c);
if (*c == '\"')
{
// String
return ParseString(k.Str, c);
}
else if (*c == '{')
{
// Objects
c++;
SkipWs(c);
while (*c && *c != '}')
{
LString n;
if (!ParseString(n, c))
return false;
if (!ParseChar(':', c))
return false;
auto Idx = k.Obj.Length();
auto &Member = k.Obj[Idx];
Member.Name = n;
if (!Parse(Member, c))
{
k.Obj.Length(Idx); // Delete incomplete obj.
return false;
}
SkipWs(c);
if (*c == ',')
{
c++;
SkipWs(c);
}
}
if (*c == '}')
{
c++;
return true;
}
}
else if (*c == '[')
{
// Parse array
return ParseArray(k.Array, c);
}
else if (IsNumeric(*c))
{
const char *e = c;
while (*e && IsNumeric(*e))
e++;
k.Str.Set(c, e - c);
c = e;
return true;
}
else if (IsAlpha(*c))
{
// Boolean?
const char *e = c;
while (*e && IsAlpha(*e))
e++;
k.Str.Set(c, e - c);
c = e;
return true;
}
else if (!*c)
return true;
return false;
}
Key *Deref(LString Addr, bool Create)
{
return Root.Deref(Addr, Create);
}
public:
LJson(const char *c = NULL)
{
Start = NULL;
if (c)
SetJson(c);
}
void Empty()
{
Root.Empty();
}
bool SetJson(const char *c)
{
Empty();
if (!c)
return false;
Start = c;
bool b = Parse(Root, c);
if (!b)
LgiTrace("%s:%i - Error at char " LPrintfSizeT "\n", _FL, c - Start);
return b;
}
LString GetJson()
{
LString s = Root.Print(0);
#ifdef _DEBUG
LAssert(LIsUtf8(s));
#endif
return s;
}
LString Get(LString Addr)
{
Key *k = Deref(Addr, false);
return k ? k->Str : LString();
}
struct Pair
{
LString key;
LString value;
Pair() { }
Pair(LString k, LString v) : key(k), value(v) { }
};
class Iter
{
LJson *j;
LArray<Key> *a;
public:
struct IterPos
{
Iter *It;
size_t Pos;
IterPos(Iter *i, size_t p)
{
It = i;
Pos = p;
}
bool operator !=(const IterPos &i) const
{
bool Eq = It == i.It && Pos == i.Pos;
return !Eq;
}
IterPos &operator *()
{
return *this;
}
operator LString()
{
return (*It->a)[Pos].Str;
}
IterPos &operator++()
{
Pos++;
return *this;
}
Key *Deref(LString &addr, bool create)
{
if (!It->a->IdxCheck(Pos))
return nullptr;
return (*It->a)[Pos].Deref(addr, create);
}
LString GetJson()
{
if (!It->a->IdxCheck(Pos))
return LString();
return (*It->a)[Pos].Print(0);
}
LString Get(LString Addr)
{
if (auto k = Deref(Addr, false))
return k->Str;
return LString();
}
Pair Get()
{
auto &a = *It->a;
if (!a.IdxCheck(Pos))
return Pair();
Key &k = a[Pos];
if (k.Name || k.Str)
return Pair(k.Name, k.Str);
if (k.Obj.Length())
return Pair(k.Obj[0].Name, k.Obj[0].Str);
return Pair();
}
Iter GetArray(LString Addr)
{
Iter a(It->j);
auto &Arr = *It->a;
if (Pos < Arr.Length())
{
Key *k = Arr[Pos].Deref(Addr, false);
if (k)
a.Set(&k->Array);
}
return a;
}
bool Set(LString Addr, const char *Val)
{
if (auto k = Deref(Addr, true))
{
k->Str = Val;
return true;
}
return false;
}
bool Set(LString Addr, bool b)
{
if (auto k = Deref(Addr, true))
return k->Str.Printf("%i", b) > 0;
return false;
}
bool Set(LString Addr, int64_t Int)
{
if (auto k = Deref(Addr, true))
return k->Str.Printf(LPrintfInt64, Int) > 0;
return false;
}
bool Set(LString Addr, double Dbl)
{
if (auto k = Deref(Addr, true))
return k->Str.Printf("%f", Dbl) > 0;
return false;
}
bool Set(LString Addr, const LArray<LString> &Array)
{
if (auto k = Deref(Addr, true))
{
for (size_t i=0; i<Array.Length(); i++)
k->Array.New().Str = Array.ItemAt(i);
return true;
}
return false;
}
bool Set(LString Addr, const LArray<LJson> &Array)
{
if (auto k = Deref(Addr, true))
{
for (size_t i=0; i<Array.Length(); i++)
{
auto &out = k->Array.New();
out = Array.ItemAt(i).Root;
}
return true;
}
return false;
}
};
Iter(LJson *J = NULL) : j(J)
{
a = NULL;
}
void Set(LJson *J)
{
j = J;
}
void Set(LArray<Key> *arr)
{
a = arr;
}
size_t Length()
{
return a ? a->Length() : 0;
}
IterPos begin()
{
return IterPos(this, 0);
}
IterPos end()
{
return IterPos(this, a ? a->Length() : 0);
}
};
Iter GetArray(LString Addr)
{
Iter a(this);
auto k = Deref(Addr, false);
if (k)
a.Set(&k->Array);
return a;
}
bool Set(LString Addr, const char *Val)
{
auto k = Deref(Addr, true);
if (!k)
return false;
k->Str = Val;
return true;
}
bool Set(LString Addr, bool b)
{
char s[32];
sprintf_s(s, sizeof(s), "%i", b);
return Set(Addr, s);
}
bool Set(LString Addr, int64_t Int)
{
char s[32];
sprintf_s(s, sizeof(s), LPrintfInt64, Int);
return Set(Addr, s);
}
bool Set(LString Addr, uint64_t Int)
{
char s[32];
sprintf_s(s, sizeof(s), LPrintfUInt64, Int);
return Set(Addr, s);
}
bool Set(LString Addr, double Dbl)
{
char s[32];
sprintf_s(s, sizeof(s), "%f", Dbl);
return Set(Addr, s);
}
bool Set(LString Addr, const LArray<LString> &Array)
{
auto k = Deref(Addr, true);
if (!k)
return false;
for (size_t i=0; i<Array.Length(); i++)
k->Array.New().Str = Array.ItemAt(i);
return true;
}
bool Set(LString Addr, const LArray<LJson> &Array)
{
auto k = Deref(Addr, true);
if (!k)
return false;
for (size_t i=0; i<Array.Length(); i++)
{
auto &out = k->Array.New();
out = Array.ItemAt(i).Root;
}
return true;
}
LString::Array GetKeys(const char *Addr = nullptr)
{
LString::Array keys;
if (auto k = Deref(Addr, true))
for (auto &i: k->Obj)
keys.Add(i.Name);
return keys;
}
// Testing
static LString PrintTest()
{
LJson j;
LArray<LJson> a;
j.Set("noElem", a);
a[0] = "one";
j.Set("oneStr", a);
a.Add("two");
a.Add("three");
j.Set("threeStr", a);
LArray<LJson> objs;
auto &first = objs.New();
first.Set("field", "val");
first.Set("field2", "val2");
j.Set("oneObj", objs);
auto &second = objs.New();
second.Set("field", "val");
second.Set("field2", "val2");
j.Set("twoObj", objs);
return j.GetJson();
}
};
#endif