This repository has been archived by the owner on Nov 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEvilWorks.Web.JSON.pas
715 lines (598 loc) · 16.1 KB
/
EvilWorks.Web.JSON.pas
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
//
// EvilLibrary by Vedran Vuk 2010-2012
//
// Name: EvilWorks.Web.Json
// Description: Json class. Inspired by superobject which is cool, unlike interfaces.
// Designed as an intermediate container, with little management functions.
// File last change date: November 7th. 2012
// File version: Dev 0.0.0
// Licence: Free.
//
unit EvilWorks.Web.JSON;
interface
uses
System.Classes,
System.SysUtils,
EvilWorks.System.StrUtils,
EvilWorks.Generics.AVLTree,
EvilWorks.Generics.List;
type
{ Exceptions }
EJson = class(Exception);
EJsonMalformed = class(EJson); // Raised when parsing a malformed json input.
EJsonConvert = class(EJson); // Raised when casting Json to an incompatible type.
EJsonItemNotFound = class(EJson); // Raised when the path does not exist.
EJsonInvalidPath = class(EJson); // Raised when an invalid path is passed to O[aKey];
{ TJsonValueType }
{ Json data type contained in TJson. }
TJsonValueType = (jvtNull, jvtString, jvtInteger, jvtFloat, jvtBool, jvtArray, jvtObject);
{ TJson }
{ Main Json object. }
TJson = class
private
FOwner : TJson; // Owner.
FName : string; // Item key.
FValueType: TJsonValueType; // Json data type currently contained.
FValue : pointer; // Data for the value.
FValueSize: integer; // Size of the value data.
// Checks what value type is contained in this instance.
function GetIsNull: boolean;
function GetIsString: boolean;
function GetIsInteger: boolean;
function GetIsFloat: boolean;
function GetIsBool: boolean;
function GetIsArray: boolean;
function GetIsObject: boolean;
// (Re)initializes this instance to a new type.
procedure SetToString(const aLength: integer);
procedure SetToInteger;
procedure SetToFloat;
procedure SetToBool;
procedure SetToArray;
procedure SetToObject;
// Value getters.
function GetS: string;
function GetI: int64;
function GetF: double;
function GetB: boolean;
function GetA(const aIndex: integer): TJson;
function GetO(const aKey: string): TJson;
// Value setters.
procedure SetS(const aValue: string);
procedure SetI(const aValue: int64);
procedure SetF(const aValue: double);
procedure SetB(const aValue: boolean);
procedure SetA(const aIndex: integer; const aValue: TJson);
procedure SetO(const aKey: string; const aValue: TJson);
// Property getters/setters.
function GetCount: integer;
procedure SetName(const aValue: string);
protected
procedure Rename(const aOldKey, aNewKey: string);
function Tree: TAVLTree<string, TJson>;
public
constructor Create(aOwner: TJson);
destructor Destroy; override;
procedure Assign(aSource: TJson);
// Json is a JavaScript format which is CASE-SENSITIVE, so Name is as well.
property name: string read FName write SetName;
procedure Null;
// Management functions when TJson is an Array.
// Using these comverts this Json to array.
function Add: TJson;
function Insert(const aIndex: integer): TJson;
procedure Delete(const aIndex: integer);
// Check what type of json value is contained in this Json instance.
property ValueType: TJsonValueType read FValueType;
property IsNull: boolean read GetIsNull;
property IsString: boolean read GetIsString;
property IsInteger: boolean read GetIsInteger;
property IsFloat: boolean read GetIsFloat;
property IsBool: boolean read GetIsBool;
property IsArray: boolean read GetIsArray;
property IsObject: boolean read GetIsObject;
procedure AddS(const aKey: string; const aVal: string);
procedure AddI(const aKey: string; const aVal: integer);
procedure AddF(const aKey: string; const aVal: double);
procedure AddB(const aKey: string; const aVal: boolean);
procedure AddA(const aKey: string; const aVal: array of TJson);
procedure AddO(const aKey: string; const aVal: TJson);
// Access this instance value by casting to a specific type.
// An EJsonConvert is raised if casting to wrong type. Use preceeding Is* functions.
property S: string read GetS write SetS;
property I: int64 read GetI write SetI;
property F: double read GetF write SetF;
property B: boolean read GetB write SetB;
property A[const aIndex: integer]: TJson read GetA write SetA;
property O[const aPath: string]: TJson read GetO write SetO;
// Number of items contained if object or array.
property Count: integer read GetCount;
end;
function ParseJson(const aJson: string): TJson;
implementation
{ Creates a TJson instance from a string. Free it after you're done. }
function ParseJson(const aJson: string): TJson;
type
TJsonToken = (
jtObjectStart,
jtObjectEnd,
jtArrayStart,
jtArrayEnd,
jtStringQuote,
jtValColon,
jtValComma
);
TJsonTokens = set of TJsonToken;
TParseLocation = (
plStart,
plObject,
plArray,
plName,
plValue
);
var
i: integer;
procedure Throw;
begin
if (Result <> nil) then
FreeAndNil(Result);
raise EJsonMalformed.Create(Format('Malformed Json string at %d.', [i]));
end;
var
l: integer;
c: TJson;
k: TJsonTokens;
begin
Result := nil;
if (aJson = '') then
Throw;
Result := TJson.Create(nil);
c := Result;
i := 1;
l := Length(aJson);
k := [jtArrayStart, jtObjectStart];
while (i <= l) do
begin
case aJson[i] of
#8, #9, #10, #12, #13, #32:
begin
{ Skip spaces. }
end;
'{':
begin
end;
'}':
begin
end;
'[':
begin
end;
']':
begin
end;
'"':
begin
end;
':':
begin
end;
',':
begin
end;
else
begin
end;
end; { case }
Inc(i);
end;
end;
{ ===== }
{ TJson }
{ ===== }
{ Constructor. }
constructor TJson.Create(aOwner: TJson);
begin
FOwner := aOwner;
FValueType := jvtNull;
FValueSize := 0;
FValue := nil;
end;
{ Destructor. }
destructor TJson.Destroy;
begin
Null;
inherited;
end;
{ Assign. }
procedure TJson.Assign(aSource: TJson);
var
i: integer;
t: TAVLTree<string, TJson>;
j: TJson;
begin
Null;
name := aSource.name;
case aSource.ValueType of
jvtNull:
Exit;
jvtString:
Self.S := aSource.S;
jvtInteger:
Self.I := aSource.I;
jvtFloat:
Self.F := aSource.F;
jvtBool:
Self.B := aSource.B;
jvtArray:
for i := 0 to aSource.Count - 1 do
Self.Add.Assign(aSource.A[i]);
jvtObject:
begin
t := aSource.Tree;
for j in t do
O[j.name].Assign(j);
end;
end;
end;
{ Null this Json instance value and free all its sub-objects (if any). }
procedure TJson.Null;
begin
if (FValueType = jvtNull) then
Exit;
if (FValueType = jvtObject) then
begin
TAVLTree<string, TJson>(FValue).Free;
FValue := nil;
end;
if (FValueType = jvtArray) then
begin
TList<TJson>(FValue).Free;
FValue := nil;
end;
if (FValue <> nil) then
begin
FreeMem(FValue, FValueSize);
FValue := nil;
end;
FValueSize := 0;
FValueType := jvtNull;
end;
{ Change name of a json in the tree if this json is an object. Required for managing AVL tree. }
procedure TJson.Rename(const aOldKey, aNewKey: string);
begin
if (FValueType <> jvtObject) then
raise EJsonConvert.Create('Rename: value is not an object.');
TAVLTree<string, TJson>(FValue).ReKey(aOldKey, aNewKey);
end;
{ Returns the AVL tree for a Json of value type object. Raises EJsonConvert if Json value is not an object. }
function TJson.Tree: TAVLTree<string, TJson>;
begin
if (FValueType <> jvtObject) then
raise EJsonConvert.Create('Tree: value is not an object.');
Result := TAVLTree<string, TJson>(FValue);
end;
{ Converts this instance to an array if not already one then adds a new json }
{ to the array and returns it for modification. }
function TJson.Add: TJson;
begin
SetToArray;
Result := TList<TJson>(FValue).Add;
end;
{ Converts this instance to an array if not already one then inserts a new json }
{ to the array at aIndex and returns it for modification. }
function TJson.Insert(const aIndex: integer): TJson;
begin
SetToArray;
Result := TList<TJson>(FValue).Insert(aIndex);
end;
{ Deletes an item from the array if Json is an array, otherwise raises EJsonConvert. }
procedure TJson.Delete(const aIndex: integer);
begin
if (FValueType <> jvtArray) then
raise EJsonConvert.Create('Delete: value is not an array.');
TList<TJson>(FValue).Delete(aIndex);
end;
{ Checks if value is Null. }
function TJson.GetIsNull: boolean;
begin
Result := (FValueType = jvtNull);
end;
{ Checks if value is String. }
function TJson.GetIsString: boolean;
begin
Result := (FValueType = jvtString);
end;
{ Checks if value is Bool. }
function TJson.GetIsBool: boolean;
begin
Result := (FValueType = jvtBool);
end;
{ Checks if value is Integer. }
function TJson.GetIsInteger: boolean;
begin
Result := (FValueType = jvtInteger);
end;
{ Checks if value is Float. }
function TJson.GetIsFloat: boolean;
begin
Result := (FValueType = jvtFloat);
end;
{ Checks if value is Array. }
function TJson.GetIsArray: boolean;
begin
Result := (FValueType = jvtArray);
end;
{ Checks if value is Object. }
function TJson.GetIsObject: boolean;
begin
Result := (FValueType = jvtObject);
end;
{ Returns count of items if item is array or number of contained values if Object. }
function TJson.GetCount: integer;
begin
if (FValueType = jvtArray) then
Result := TList<TJson>(FValue).Count
else if (FValueType = jvtObject) then
Result := TAVLTree<string, TJson>(FValue).Count
else
Result := 0;
end;
{ Returns value as string. If value is not String raises EJsonConvert. }
function TJson.GetS: string;
begin
if (FValueType <> jvtString) then
raise EJsonConvert.Create('GetS: value is not a string.');
Result := string(PChar(FValue))
end;
{ Returns value as int64. If value is not int64 raises EJsonConvert. }
function TJson.GetI: int64;
begin
if (FValueType <> jvtInteger) then
raise EJsonConvert.Create('GetI: value is not an integer.');
if (FValueType = jvtInteger) then
Result := pint64(FValue)^;
end;
{ Returns value as double. If value is not double raises EJsonConvert. }
function TJson.GetF: double;
begin
if (FValueType <> jvtFloat) then
raise EJsonConvert.Create('GetF: value is not a float.');
if (FValueType = jvtFloat) then
Result := pdouble(FValue)^;
end;
{ Returns value as boolean. If value is not boolean raises EJsonConvert. }
function TJson.GetB: boolean;
begin
if (FValueType <> jvtBool) then
raise EJsonConvert.Create('GetB: value is not a boolean.');
Result := pboolean(FValue)^;
end;
{ Returns value as array. If value is not array raises EJsonConvert. }
function TJson.GetA(const aIndex: integer): TJson;
begin
if (FValueType <> jvtArray) then
raise EJsonConvert.Create('GetA: value is not an array.');
Result := TList<TJson>(FValue)[aIndex];
end;
{ Returns value as object. If value is not object raises EJsonConvert. }
function TJson.GetO(const aKey: string): TJson;
var
path: string;
begin
if (FValueType <> jvtObject) then
raise EJsonConvert.Create('GetO: value is not an object.');
if (TAVLTree<string, TJson>(FValue).Exists(aKey) = False) then
raise EJsonItemNotFound.Create('Item not found.');
Result := TAVLTree<string, TJson>(FValue).Items[aKey];
end;
{ Converts value of this Json to string. }
procedure TJson.SetToString(const aLength: integer);
begin
Null;
FValueType := jvtString;
FValueSize := ((aLength * SizeOf(char)) + 1);
FValue := AllocMem(FValueSize);
end;
{ Converts value of this Json to integer. }
procedure TJson.SetToInteger;
begin
if (FValueType = jvtInteger) then
Exit;
Null;
FValueType := jvtInteger;
FValueSize := SizeOf(int64);
FValue := GetMemory(FValueSize);
end;
{ Converts value of this Json to float. }
procedure TJson.SetToFloat;
begin
if (FValueType = jvtFloat) then
Exit;
Null;
FValueType := jvtFloat;
FValueSize := SizeOf(double);
FValue := GetMemory(FValueSize);
end;
{ Converts value of this Json to bool. }
procedure TJson.SetToBool;
begin
if (FValueType = jvtBool) then
Exit;
Null;
FValueType := jvtBool;
FValueSize := SizeOf(boolean);
FValue := GetMemory(FValueSize);
end;
{ Converts value of this Json to array. }
procedure TJson.SetToArray;
begin
if (FValueType = jvtArray) then
Exit;
Null;
FValueType := jvtArray;
FValueSize := SizeOf(TList<TJson>);
TList<TJson>(FValue) := TList<TJson>.Create(
function: TJson
begin
Result := TJson.Create(Self);
end,
procedure(var aItem: TJson)
begin
aItem.Free;
end,
procedure(const aFromItem: TJson; var aToItem: TJson)
begin
aToItem.Assign(aFromItem);
end,
function(const aItemA, aItemB: TJson): integer
begin
if (aItemA.Name < aItemB.Name) then
Result := - 1
else if (aItemA.Name > aItemB.Name) then
Result := + 1
else
Result := 0;
end
);
end;
{ Converts value of this Json to object. }
procedure TJson.SetToObject;
begin
if (FValueType = jvtObject) then
Exit;
Null;
FValueType := jvtObject;
FValueSize := SizeOf(TAVLTree<string, TJson>);
TAVLTree<string, TJson>(FValue) := TAVLTree<string, TJson>.Create(
function(const aKeyA, aKeyB: string): integer
begin
if (aKeyA < aKeyB) then
Result := - 1
else if (aKeyA > aKeyB) then
Result := + 1
else
Result := 0;
end,
procedure(var aKey: string)
begin
aKey := '';
end,
procedure(var aVal: TJson)
begin
aVal.Free;
end
);
end;
{ Converts value of this Json to string and sets its value. }
procedure TJson.SetS(const aValue: string);
begin
SetToString(Length(aValue));
Move(aValue[1], FValue^, FValueSize);
end;
{ Converts value of this Json to integer and sets its value. }
procedure TJson.SetI(const aValue: int64);
begin
SetToInteger;
PInt64(FValue)^ := aValue;
end;
{ Converts value of this Json to double and sets its value. }
procedure TJson.SetF(const aValue: double);
begin
SetToFloat;
pdouble(FValue)^ := aValue;
end;
{ Converts value of this Json to boolean and sets its value. }
procedure TJson.SetB(const aValue: boolean);
begin
SetToBool;
pboolean(FValue)^ := aValue;
end;
{ Converts value of this Json to array and sets its value. }
procedure TJson.SetA(const aIndex: integer; const aValue: TJson);
begin
SetToArray;
TList<TJson>(FValue)[aIndex].Assign(aValue);
end;
{ Converts value of this Json to object and sets its value. }
{ A complete path is created automatically if the target does not exist. }
procedure TJson.SetO(const aKey: string; const aValue: TJson);
begin
SetToObject;
TAVLTree<string, TJson>(FValue).Items[aKey].Assign(aValue);
end;
{ Renames this Json. }
procedure TJson.SetName(const aValue: string);
begin
if (FOwner <> nil) then
FOwner.Rename(FName, aValue);
FName := aValue;
end;
{ Sets the Json to an object and adds a string value. }
procedure TJson.AddS(const aKey: string; const aVal: string);
var
j: TJson;
begin
SetToObject;
j := TJson.Create(Self);
j.name := aKey;
j.S := aVal;
TAVLTree<string, TJson>(FValue).Insert(aKey, j);
end;
{ Sets the Json to an object and adds a integer value. }
procedure TJson.AddI(const aKey: string; const aVal: integer);
var
j: TJson;
begin
SetToObject;
j := TJson.Create(Self);
j.name := aKey;
j.I := aVal;
TAVLTree<string, TJson>(FValue).Insert(aKey, j);
end;
{ Sets the Json to an object and adds a double value. }
procedure TJson.AddF(const aKey: string; const aVal: double);
var
j: TJson;
begin
SetToObject;
j := TJson.Create(Self);
j.name := aKey;
j.F := aVal;
TAVLTree<string, TJson>(FValue).Insert(aKey, j);
end;
{ Sets the Json to an object and adds a boolean value. }
procedure TJson.AddB(const aKey: string; const aVal: boolean);
var
j: TJson;
begin
SetToObject;
j := TJson.Create(Self);
j.name := aKey;
j.B := aVal;
TAVLTree<string, TJson>(FValue).Insert(aKey, j);
end;
{ Sets the Json to an object and adds a array value. }
procedure TJson.AddA(const aKey: string; const aVal: array of TJson);
var
j: TJson;
i: integer;
begin
SetToObject;
j := TJson.Create(Self);
for i := 0 to high(aVal) do
begin
j.A[i].Assign(aVal[i]);
TAVLTree<string, TJson>(FValue).Insert(aKey, j);
end;
end;
{ Sets the Json to an object and adds a object value. }
procedure TJson.AddO(const aKey: string; const aVal: TJson);
var
j: TJson;
begin
SetToObject;
j := TJson.Create(Self);
j.Assign(aVal);
TAVLTree<string, TJson>(FValue).Insert(aKey, j);
end;
end.