forked from jackdp/MFPC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MFPC.fpJSON.pp
3251 lines (2722 loc) · 80.6 KB
/
MFPC.fpJSON.pp
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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
This file is part of the Free Component Library
JSON Data structures
Copyright (c) 2007 by Michael Van Canneyt michael@freepascal.org
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
{$mode objfpc}
{$h+}
unit MFPC.fpJSON;
interface
uses
variants,
SysUtils,
MFPC.Classes.SHARED, MFPC.Classes.Streams,
//classes,
MFPC.Contnrs;
type
TJSONtype = (jtUnknown, jtNumber, jtString, jtBoolean, jtNull, jtArray, jtObject);
TJSONInstanceType = (jitUnknown, jitNumberInteger,jitNumberInt64,jitNumberQWord,jitNumberFloat,
jitString, jitBoolean, jitNull, jitArray, jitObject);
TJSONFloat = Double;
TJSONStringType = UTF8String;
TJSONUnicodeStringType = Unicodestring;
TJSONCharType = AnsiChar;
PJSONCharType = ^TJSONCharType;
TFormatOption = (foSingleLineArray, // Array without CR/LF : all on one line
foSingleLineObject, // Object without CR/LF : all on one line
foDoNotQuoteMembers, // Do not quote object member names.
foUseTabchar, // Use tab characters instead of spaces.
foSkipWhiteSpace); // Do not use whitespace at all
TFormatOptions = set of TFormatOption;
Const
DefaultIndentSize = 2;
DefaultFormat = [];
AsJSONFormat = [foSingleLineArray,foSingleLineObject]; // These options make FormatJSON behave as AsJSON
AsCompressedJSON = [foSingleLineArray,foSingleLineObject,foskipWhiteSpace]; // These options make FormatJSON behave as AsJSON with TJSONData.CompressedJSON=True
AsCompactJSON = [foSingleLineArray,foSingleLineObject,foskipWhiteSpace,foDoNotQuoteMembers]; // These options make FormatJSON behave as AsJSON with TJSONData.CompressedJSON=True and TJSONObject.UnquotedMemberNames=True
ValueJSONTypes = [jtNumber, jtString, jtBoolean, jtNull];
ActualValueJSONTypes = ValueJSONTypes - [jtNull];
StructuredJSONTypes = [jtArray,jtObject];
Type
TJSONData = Class;
{ TMJBaseObjectEnumerator }
TJSONEnum = Record
Key : TJSONStringType;
KeyNum : Integer;
Value : TJSONData;
end;
TBaseJSONEnumerator = class
public
function GetCurrent: TJSONEnum; virtual; abstract;
function MoveNext : Boolean; virtual; abstract;
property Current: TJSONEnum read GetCurrent;
end;
{ TMJObjectEnumerator }
{ TJSONData }
TJSONData = class(TObject)
private
Const
ElementSeps : Array[Boolean] of TJSONStringType = (', ',',');
Class Var FCompressedJSON : Boolean;
Class Var FElementSep : TJSONStringType;
class procedure DetermineElementSeparators;
class function GetCompressedJSON: Boolean; static;
class procedure SetCompressedJSON(AValue: Boolean); static;
protected
Class Procedure DoError(Const Msg : String);
Class Procedure DoError(Const Fmt : String; const Args : Array of const);
Function DoFindPath(Const APath : TJSONStringType; Out NotFound : TJSONStringType) : TJSONdata; virtual;
function GetAsBoolean: Boolean; virtual; abstract;
function GetAsFloat: TJSONFloat; virtual; abstract;
function GetAsInteger: Integer; virtual; abstract;
function GetAsInt64: Int64; virtual; abstract;
function GetAsQWord: QWord; virtual; abstract;
function GetIsNull: Boolean; virtual;
procedure SetAsBoolean(const AValue: Boolean); virtual; abstract;
procedure SetAsFloat(const AValue: TJSONFloat); virtual; abstract;
procedure SetAsInteger(const AValue: Integer); virtual; abstract;
procedure SetAsInt64(const AValue: Int64); virtual; abstract;
procedure SetAsQword(const AValue: QWord); virtual; abstract;
function GetAsJSON: TJSONStringType; virtual; abstract;
function GetAsString: TJSONStringType; virtual; abstract;
procedure SetAsString(const AValue: TJSONStringType); virtual; abstract;
function GetAsUnicodeString: TJSONUnicodeStringType; virtual;
procedure SetAsUnicodeString(const AValue: TJSONUnicodeStringType); virtual;
function GetValue: variant; virtual; abstract;
procedure SetValue(const AValue: variant); virtual; abstract;
function GetItem(Index : Integer): TJSONData; virtual;
procedure SetItem(Index : Integer; const AValue: TJSONData); virtual;
Function DoFormatJSON(Options : TFormatOptions; CurrentIndent, Indent : Integer) : TJSONStringType; virtual;
function GetCount: Integer; virtual;
Public
Class function JSONType: TJSONType; virtual;
Class Property CompressedJSON : Boolean Read GetCompressedJSON Write SetCompressedJSON;
public
Constructor Create; virtual;
Procedure Clear; virtual; Abstract;
Procedure DumpJSON(S : TM_Stream);
// Get enumerator
function GetEnumerator: TBaseJSONEnumerator; virtual;
Function FindPath(Const APath : TJSONStringType) : TJSONdata;
Function GetPath(Const APath : TJSONStringType) : TJSONdata;
Function Clone : TJSONData; virtual; abstract;
Function FormatJSON(Options : TFormatOptions = DefaultFormat; Indentsize : Integer = DefaultIndentSize) : TJSONStringType;
property Count: Integer read GetCount;
property Items[Index: Integer]: TJSONData read GetItem write SetItem;
property Value: variant read GetValue write SetValue;
Property AsString : TJSONStringType Read GetAsString Write SetAsString;
Property AsUnicodeString : TJSONUnicodeStringType Read GetAsUnicodeString Write SetAsUnicodeString;
Property AsFloat : TJSONFloat Read GetAsFloat Write SetAsFloat;
Property AsInteger : Integer Read GetAsInteger Write SetAsInteger;
Property AsInt64 : Int64 Read GetAsInt64 Write SetAsInt64;
Property AsQWord : QWord Read GetAsQWord Write SetAsQword;
Property AsBoolean : Boolean Read GetAsBoolean Write SetAsBoolean;
Property IsNull : Boolean Read GetIsNull;
Property AsJSON : TJSONStringType Read GetAsJSON;
end;
TJSONDataClass = Class of TJSONData;
TJSONNumberType = (ntFloat,ntInteger,ntInt64,ntQWord);
TJSONNumber = class(TJSONData)
protected
public
class function JSONType: TJSONType; override;
class function NumberType : TJSONNumberType; virtual; abstract;
end;
{ TJSONFloatNumber }
TJSONFloatNumber = class(TJSONNumber)
Private
FValue : TJSONFloat;
protected
function GetAsBoolean: Boolean; override;
function GetAsFloat: TJSONFloat; override;
function GetAsInteger: Integer; override;
function GetAsInt64: Int64; override;
function GetAsQWord: QWord; override;
procedure SetAsBoolean(const AValue: Boolean); override;
procedure SetAsFloat(const AValue: TJSONFloat); override;
procedure SetAsInteger(const AValue: Integer); override;
procedure SetAsInt64(const AValue: Int64); override;
procedure SetAsQword(const AValue: QWord); override;
function GetAsJSON: TJSONStringType; override;
function GetAsString: TJSONStringType; override;
procedure SetAsString(const AValue: TJSONStringType); override;
function GetValue: variant; override;
procedure SetValue(const AValue: variant); override;
public
Constructor Create(AValue : TJSONFloat); reintroduce;
class function NumberType : TJSONNumberType; override;
Procedure Clear; override;
Function Clone : TJSONData; override;
end;
TJSONFloatNumberClass = Class of TJSONFloatNumber;
{ TJSONIntegerNumber }
TJSONIntegerNumber = class(TJSONNumber)
Private
FValue : Integer;
protected
function GetAsBoolean: Boolean; override;
function GetAsFloat: TJSONFloat; override;
function GetAsInteger: Integer; override;
function GetAsInt64: Int64; override;
function GetAsQWord: QWord; override;
procedure SetAsBoolean(const AValue: Boolean); override;
procedure SetAsFloat(const AValue: TJSONFloat); override;
procedure SetAsInteger(const AValue: Integer); override;
procedure SetAsInt64(const AValue: Int64); override;
procedure SetAsQword(const AValue: QWord); override;
function GetAsJSON: TJSONStringType; override;
function GetAsString: TJSONStringType; override;
procedure SetAsString(const AValue: TJSONStringType); override;
function GetValue: variant; override;
procedure SetValue(const AValue: variant); override;
public
Constructor Create(AValue : Integer); reintroduce;
class function NumberType : TJSONNumberType; override;
Procedure Clear; override;
Function Clone : TJSONData; override;
end;
TJSONIntegerNumberClass = Class of TJSONIntegerNumber;
{ TJSONInt64Number }
TJSONInt64Number = class(TJSONNumber)
Private
FValue : Int64;
protected
function GetAsBoolean: Boolean; override;
function GetAsFloat: TJSONFloat; override;
function GetAsInteger: Integer; override;
function GetAsInt64: Int64; override;
function GetAsQWord: QWord; override;
procedure SetAsBoolean(const AValue: Boolean); override;
procedure SetAsFloat(const AValue: TJSONFloat); override;
procedure SetAsInteger(const AValue: Integer); override;
procedure SetAsInt64(const AValue: Int64); override;
procedure SetAsQword(const AValue: QWord); override;
function GetAsJSON: TJSONStringType; override;
function GetAsString: TJSONStringType; override;
procedure SetAsString(const AValue: TJSONStringType); override;
function GetValue: variant; override;
procedure SetValue(const AValue: variant); override;
public
Constructor Create(AValue : Int64); reintroduce;
class function NumberType : TJSONNumberType; override;
Procedure Clear; override;
Function Clone : TJSONData; override;
end;
TJSONInt64NumberClass = Class of TJSONInt64Number;
{ TJSONQWordNumber }
TJSONQWordNumber = class(TJSONNumber)
Private
FValue : Qword;
protected
function GetAsBoolean: Boolean; override;
function GetAsFloat: TJSONFloat; override;
function GetAsInteger: Integer; override;
function GetAsInt64: Int64; override;
function GetAsQWord: QWord; override;
procedure SetAsBoolean(const AValue: Boolean); override;
procedure SetAsFloat(const AValue: TJSONFloat); override;
procedure SetAsInteger(const AValue: Integer); override;
procedure SetAsInt64(const AValue: Int64); override;
procedure SetAsQword(const AValue: QWord); override;
function GetAsJSON: TJSONStringType; override;
function GetAsString: TJSONStringType; override;
procedure SetAsString(const AValue: TJSONStringType); override;
function GetValue: variant; override;
procedure SetValue(const AValue: variant); override;
public
Constructor Create(AValue : QWord); reintroduce;
class function NumberType : TJSONNumberType; override;
Procedure Clear; override;
Function Clone : TJSONData; override;
end;
TJSONQWordNumberClass = Class of TJSONQWordNumber;
{ TJSONString }
TJSONString = class(TJSONData)
Private
FValue: TJSONStringType;
protected
function GetValue: Variant; override;
procedure SetValue(const AValue: Variant); override;
function GetAsBoolean: Boolean; override;
function GetAsFloat: TJSONFloat; override;
function GetAsInteger: Integer; override;
function GetAsInt64: Int64; override;
function GetAsQWord: QWord; override;
procedure SetAsBoolean(const AValue: Boolean); override;
procedure SetAsFloat(const AValue: TJSONFloat); override;
procedure SetAsInteger(const AValue: Integer); override;
procedure SetAsInt64(const AValue: Int64); override;
procedure SetAsQword(const AValue: QWord); override;
function GetAsJSON: TJSONStringType; override;
function GetAsString: TJSONStringType; override;
procedure SetAsString(const AValue: TJSONStringType); override;
Public
Class var StrictEscaping : Boolean;
public
Constructor Create(const AValue : TJSONStringType); reintroduce;
Constructor Create(const AValue : TJSONUnicodeStringType); reintroduce;
class function JSONType: TJSONType; override;
Procedure Clear; override;
Function Clone : TJSONData; override;
end;
TJSONStringClass = Class of TJSONString;
{ TJSONboolean }
TJSONBoolean = class(TJSONData)
Private
FValue: Boolean;
protected
function GetValue: Variant; override;
procedure SetValue(const AValue: Variant); override;
function GetAsBoolean: Boolean; override;
function GetAsFloat: TJSONFloat; override;
function GetAsInteger: Integer; override;
function GetAsInt64: Int64; override;
function GetAsQWord: QWord; override;
procedure SetAsBoolean(const AValue: Boolean); override;
procedure SetAsFloat(const AValue: TJSONFloat); override;
procedure SetAsInteger(const AValue: Integer); override;
procedure SetAsInt64(const AValue: Int64); override;
procedure SetAsQword(const AValue: QWord); override;
function GetAsJSON: TJSONStringType; override;
function GetAsString: TJSONStringType; override;
procedure SetAsString(const AValue: TJSONStringType); override;
public
Constructor Create(AValue : Boolean); reintroduce;
class function JSONType: TJSONType; override;
Procedure Clear; override;
Function Clone : TJSONData; override;
end;
TJSONBooleanClass = Class of TJSONBoolean;
{ TJSONnull }
TJSONNull = class(TJSONData)
protected
Procedure Converterror(From : Boolean);
function GetAsBoolean: Boolean; override;
function GetAsFloat: TJSONFloat; override;
function GetAsInteger: Integer; override;
function GetAsInt64: Int64; override;
function GetAsQWord: QWord; override;
function GetIsNull: Boolean; override;
procedure SetAsBoolean(const AValue: Boolean); override;
procedure SetAsFloat(const AValue: TJSONFloat); override;
procedure SetAsInteger(const AValue: Integer); override;
procedure SetAsInt64(const AValue: Int64); override;
procedure SetAsQword(const AValue: QWord); override;
function GetAsJSON: TJSONStringType; override;
function GetAsString: TJSONStringType; override;
procedure SetAsString(const AValue: TJSONStringType); override;
function GetValue: variant; override;
procedure SetValue(const AValue: variant); override;
public
class function JSONType: TJSONType; override;
Procedure Clear; override;
Function Clone : TJSONData; override;
end;
TJSONNullClass = Class of TJSONNull;
TJSONArrayIterator = procedure(Item: TJSONData; Data: TObject; var Continue: Boolean) of object;
{ TJSONArray }
TJSONObject = Class;
TJSONArray = class(TJSONData)
Private
FList : TM_FPObjectList;
function GetArrays(Index : Integer): TJSONArray;
function GetBooleans(Index : Integer): Boolean;
function GetFloats(Index : Integer): TJSONFloat;
function GetIntegers(Index : Integer): Integer;
function GetInt64s(Index : Integer): Int64;
function GetNulls(Index : Integer): Boolean;
function GetObjects(Index : Integer): TJSONObject;
function GetQWords(Index : Integer): QWord;
function GetStrings(Index : Integer): TJSONStringType;
function GetUnicodeStrings(Index : Integer): TJSONUnicodeStringType;
function GetTypes(Index : Integer): TJSONType;
procedure SetArrays(Index : Integer; const AValue: TJSONArray);
procedure SetBooleans(Index : Integer; const AValue: Boolean);
procedure SetFloats(Index : Integer; const AValue: TJSONFloat);
procedure SetIntegers(Index : Integer; const AValue: Integer);
procedure SetInt64s(Index : Integer; const AValue: Int64);
procedure SetObjects(Index : Integer; const AValue: TJSONObject);
procedure SetQWords(Index : Integer; AValue: QWord);
procedure SetStrings(Index : Integer; const AValue: TJSONStringType);
procedure SetUnicodeStrings(Index : Integer; const AValue: TJSONUnicodeStringType);
protected
Function DoFindPath(Const APath : TJSONStringType; Out NotFound : TJSONStringType) : TJSONdata; override;
Procedure Converterror(From : Boolean);
function GetAsBoolean: Boolean; override;
function GetAsFloat: TJSONFloat; override;
function GetAsInteger: Integer; override;
function GetAsInt64: Int64; override;
function GetAsQWord: QWord; override;
procedure SetAsBoolean(const AValue: Boolean); override;
procedure SetAsFloat(const AValue: TJSONFloat); override;
procedure SetAsInteger(const AValue: Integer); override;
procedure SetAsInt64(const AValue: Int64); override;
procedure SetAsQword(const AValue: QWord); override;
function GetAsJSON: TJSONStringType; override;
function GetAsString: TJSONStringType; override;
procedure SetAsString(const AValue: TJSONStringType); override;
function GetValue: variant; override;
procedure SetValue(const AValue: variant); override;
function GetCount: Integer; override;
function GetItem(Index : Integer): TJSONData; override;
procedure SetItem(Index : Integer; const AValue: TJSONData); override;
Function DoFormatJSON(Options : TFormatOptions; CurrentIndent, Indent : Integer) : TJSONStringType; override;
public
Constructor Create; overload; reintroduce;
Constructor Create(const Elements : Array of Const); overload;
Destructor Destroy; override;
class function JSONType: TJSONType; override;
Function Clone : TJSONData; override;
// Examine
procedure Iterate(Iterator : TJSONArrayIterator; Data: TObject);
function IndexOf(obj: TJSONData): Integer;
function GetEnumerator: TBaseJSONEnumerator; override;
// Manipulate
Procedure Clear; override;
function Add(Item : TJSONData): Integer;
function Add(I : Integer): Integer;
function Add(I : Int64): Int64;
function Add(I : QWord): QWord;
function Add(const S : String): Integer;
function Add(const S : UnicodeString): Integer;
function Add: Integer;
function Add(F : TJSONFloat): Integer;
function Add(B : Boolean): Integer;
function Add(AnArray : TJSONArray): Integer;
function Add(AnObject: TJSONObject): Integer;
Procedure Delete(Index : Integer);
procedure Exchange(Index1, Index2: Integer);
function Extract(Item: TJSONData): TJSONData;
function Extract(Index : Integer): TJSONData;
procedure Insert(Index: Integer);
procedure Insert(Index: Integer; Item : TJSONData);
procedure Insert(Index: Integer; I : Integer);
procedure Insert(Index: Integer; I : Int64);
procedure Insert(Index: Integer; I : QWord);
procedure Insert(Index: Integer; const S : String);
procedure Insert(Index: Integer; const S : UnicodeString);
procedure Insert(Index: Integer; F : TJSONFloat);
procedure Insert(Index: Integer; B : Boolean);
procedure Insert(Index: Integer; AnArray : TJSONArray);
procedure Insert(Index: Integer; AnObject: TJSONObject);
procedure Move(CurIndex, NewIndex: Integer);
Procedure Remove(Item : TJSONData);
Procedure Sort(Compare: TListSortCompare);
// Easy Access Properties.
property Items;default;
Property Types[Index : Integer] : TJSONType Read GetTypes;
Property Nulls[Index : Integer] : Boolean Read GetNulls;
Property Integers[Index : Integer] : Integer Read GetIntegers Write SetIntegers;
Property Int64s[Index : Integer] : Int64 Read GetInt64s Write SetInt64s;
Property QWords[Index : Integer] : QWord Read GetQWords Write SetQWords;
Property Strings[Index : Integer] : TJSONStringType Read GetStrings Write SetStrings;
Property UnicodeStrings[Index : Integer] : TJSONUnicodeStringType Read GetUnicodeStrings Write SetUnicodeStrings;
Property Floats[Index : Integer] : TJSONFloat Read GetFloats Write SetFloats;
Property Booleans[Index : Integer] : Boolean Read GetBooleans Write SetBooleans;
Property Arrays[Index : Integer] : TJSONArray Read GetArrays Write SetArrays;
Property Objects[Index : Integer] : TJSONObject Read GetObjects Write SetObjects;
end;
TJSONArrayClass = Class of TJSONArray;
TJSONObjectIterator = procedure(Const AName : TJSONStringType; Item: TJSONData; Data: TObject; var Continue: Boolean) of object;
{ TJSONObject }
TJSONObject = class(TJSONData)
private
Const
ElementStart : Array[Boolean] of TJSONStringType = ('"','');
SpacedQuoted : Array[Boolean] of TJSONStringType = ('" : ',' : ');
UnSpacedQuoted : Array[Boolean] of TJSONStringType = ('":',':');
ObjStartSeps : Array[Boolean] of TJSONStringType = ('{ ','{');
ObjEndSeps : Array[Boolean] of TJSONStringType = (' }','}');
Class var FUnquotedMemberNames: Boolean;
Class var FObjStartSep,FObjEndSep,FElementEnd,FElementStart : TJSONStringType;
Class procedure DetermineElementQuotes;
Private
FHash : TM_FPHashObjectList; // Careful : Names limited to 255 chars.
function GetArrays(const AName : String): TJSONArray;
function GetBooleans(const AName : String): Boolean;
function GetElements(const AName: string): TJSONData;
function GetFloats(const AName : String): TJSONFloat;
function GetIntegers(const AName : String): Integer;
function GetInt64s(const AName : String): Int64;
function GetIsNull(const AName : String): Boolean; reintroduce;
function GetNameOf(Index : Integer): TJSONStringType;
function GetObjects(const AName : String): TJSONObject;
function GetQWords(AName : String): QWord;
function GetStrings(const AName : String): TJSONStringType;
function GetUnicodeStrings(const AName : String): TJSONUnicodeStringType;
function GetTypes(const AName : String): TJSONType;
procedure SetArrays(const AName : String; const AValue: TJSONArray);
procedure SetBooleans(const AName : String; const AValue: Boolean);
procedure SetElements(const AName: string; const AValue: TJSONData);
procedure SetFloats(const AName : String; const AValue: TJSONFloat);
procedure SetIntegers(const AName : String; const AValue: Integer);
procedure SetInt64s(const AName : String; const AValue: Int64);
procedure SetIsNull(const AName : String; const AValue: Boolean);
procedure SetObjects(const AName : String; const AValue: TJSONObject);
procedure SetQWords(AName : String; AValue: QWord);
procedure SetStrings(const AName : String; const AValue: TJSONStringType);
procedure SetUnicodeStrings(const AName : String; const AValue: TJSONUnicodeStringType);
class function GetUnquotedMemberNames: Boolean; static;
class procedure SetUnquotedMemberNames(AValue: Boolean); static;
protected
Function DoFindPath(Const APath : TJSONStringType; Out NotFound : TJSONStringType) : TJSONdata; override;
Procedure Converterror(From : Boolean);
function GetAsBoolean: Boolean; override;
function GetAsFloat: TJSONFloat; override;
function GetAsInteger: Integer; override;
function GetAsInt64: Int64; override;
function GetAsQWord: QWord; override;
procedure SetAsBoolean(const AValue: Boolean); override;
procedure SetAsFloat(const AValue: TJSONFloat); override;
procedure SetAsInteger(const AValue: Integer); override;
procedure SetAsInt64(const AValue: Int64); override;
procedure SetAsQword(const AValue: QWord); override;
function GetAsJSON: TJSONStringType; override;
function GetAsString: TJSONStringType; override;
procedure SetAsString(const AValue: TJSONStringType); override;
function GetValue: variant; override;
procedure SetValue(const AValue: variant); override;
function GetCount: Integer; override;
function GetItem(Index : Integer): TJSONData; override;
procedure SetItem(Index : Integer; const AValue: TJSONData); override;
Function DoFormatJSON(Options : TFormatOptions; CurrentIndent, Indent : Integer) : TJSONStringType; override;
public
constructor Create; reintroduce;
Constructor Create(const Elements : Array of Const); overload;
destructor Destroy; override;
class function JSONType: TJSONType; override;
Class Property UnquotedMemberNames : Boolean Read GetUnquotedMemberNames Write SetUnquotedMemberNames;
Function Clone : TJSONData; override;
function GetEnumerator: TBaseJSONEnumerator; override;
// Examine
procedure Iterate(Iterator : TJSONObjectIterator; Data: TObject);
function IndexOf(Item: TJSONData): Integer;
Function IndexOfName(const AName: TJSONStringType; CaseInsensitive : Boolean = False): Integer;
Function Find(Const AName : String) : TJSONData; overload;
Function Find(Const AName : String; AType : TJSONType) : TJSONData; overload;
function Find(const key: TJSONStringType; out AValue: TJSONData): boolean;
function Find(const key: TJSONStringType; out AValue: TJSONObject): boolean;
function Find(const key: TJSONStringType; out AValue: TJSONArray): boolean;
function Find(const key: TJSONStringType; out AValue: TJSONString): boolean;
function Find(const key: TJSONStringType; out AValue: TJSONBoolean): boolean;
function Find(const key: TJSONStringType; out AValue: TJSONNumber): boolean;
Function Get(Const AName : String) : Variant;
Function Get(Const AName : String; ADefault : TJSONFloat) : TJSONFloat;
Function Get(Const AName : String; ADefault : Integer) : Integer;
Function Get(Const AName : String; ADefault : Int64) : Int64;
Function Get(Const AName : String; ADefault : QWord) : QWord;
Function Get(Const AName : String; ADefault : Boolean) : Boolean;
Function Get(Const AName : String; ADefault : TJSONStringType) : TJSONStringType;
Function Get(Const AName : String; ADefault : TJSONUnicodeStringType) : TJSONUnicodeStringType;
Function Get(Const AName : String; ADefault : TJSONArray) : TJSONArray;
Function Get(Const AName : String; ADefault : TJSONObject) : TJSONObject;
// Manipulate
Procedure Clear; override;
function Add(const AName: TJSONStringType; AValue: TJSONData): Integer; overload;
function Add(const AName: TJSONStringType; AValue: Boolean): Integer; overload;
function Add(const AName: TJSONStringType; AValue: TJSONFloat): Integer; overload;
function Add(const AName, AValue: TJSONStringType): Integer; overload;
function Add(const AName : String; AValue: TJSONUnicodeStringType): Integer; overload;
function Add(const AName: TJSONStringType; Avalue: Integer): Integer; overload;
function Add(const AName: TJSONStringType; Avalue: Int64): Integer; overload;
function Add(const AName: TJSONStringType; Avalue: QWord): Integer; overload;
function Add(const AName: TJSONStringType): Integer; overload;
function Add(const AName: TJSONStringType; AValue : TJSONArray): Integer; overload;
procedure Delete(Index : Integer);
procedure Delete(Const AName : string);
procedure Remove(Item : TJSONData);
Function Extract(Index : Integer) : TJSONData;
Function Extract(Const AName : string) : TJSONData;
// Easy access properties.
property Names[Index : Integer] : TJSONStringType read GetNameOf;
property Elements[AName: string] : TJSONData read GetElements write SetElements; default;
Property Types[AName : String] : TJSONType Read GetTypes;
Property Nulls[AName : String] : Boolean Read GetIsNull Write SetIsNull;
Property Floats[AName : String] : TJSONFloat Read GetFloats Write SetFloats;
Property Integers[AName : String] : Integer Read GetIntegers Write SetIntegers;
Property Int64s[AName : String] : Int64 Read GetInt64s Write SetInt64s;
Property QWords[AName : String] : QWord Read GetQWords Write SetQWords;
Property Strings[AName : String] : TJSONStringType Read GetStrings Write SetStrings;
Property UnicodeStrings[AName : String] : TJSONUnicodeStringType Read GetUnicodeStrings Write SetUnicodeStrings;
Property Booleans[AName : String] : Boolean Read GetBooleans Write SetBooleans;
Property Arrays[AName : String] : TJSONArray Read GetArrays Write SetArrays;
Property Objects[AName : String] : TJSONObject Read GetObjects Write SetObjects;
end;
TJSONObjectClass = Class of TJSONObject;
EJSON = Class(Exception);
TJSONParserHandler = Procedure(AStream : TM_Stream; Const AUseUTF8 : Boolean; Out Data : TJSONData);
Function SetJSONInstanceType(AType : TJSONInstanceType; AClass : TJSONDataClass) : TJSONDataClass;
Function GetJSONInstanceType(AType : TJSONInstanceType) : TJSONDataClass;
Function StringToJSONString(const S : TJSONStringType; Strict : Boolean = False) : TJSONStringType;
Function JSONStringToString(const S : TJSONStringType) : TJSONStringType;
Function JSONTypeName(JSONType : TJSONType) : String;
// These functions create JSONData structures, taking into account the instance types
Function CreateJSON : TJSONNull;
Function CreateJSON(Data : Boolean) : TJSONBoolean;
Function CreateJSON(Data : Integer) : TJSONIntegerNumber;
Function CreateJSON(Data : Int64) : TJSONInt64Number;
Function CreateJSON(Data : QWord) : TJSONQWordNumber;
Function CreateJSON(Data : TJSONFloat) : TJSONFloatNumber;
Function CreateJSON(const Data : TJSONStringType) : TJSONString;
Function CreateJSON(const Data : TJSONUnicodeStringType) : TJSONString;
Function CreateJSONArray(const Data : Array of const) : TJSONArray;
Function CreateJSONObject(const Data : Array of const) : TJSONObject;
// These functions rely on a callback. If the callback is not set, they will raise an error.
// When the jsonparser unit is included in the project, the callback is automatically set.
Function GetJSON(Const JSON : TJSONStringType; Const UseUTF8 : Boolean = True) : TJSONData;
Function GetJSON(Const JSON : TM_Stream; Const UseUTF8 : Boolean = True) : TJSONData;
Function SetJSONParserHandler(AHandler : TJSONParserHandler) : TJSONParserHandler;
Function GetJSONParserHandler : TJSONParserHandler;
implementation
Uses typinfo;
Resourcestring
SErrCannotConvertFromNull = 'Cannot convert data from Null value';
SErrCannotConvertToNull = 'Cannot convert data to Null value';
SErrCannotConvertFromArray = 'Cannot convert data from array value';
SErrCannotConvertToArray = 'Cannot convert data to array value';
SErrCannotConvertFromObject = 'Cannot convert data from object value';
SErrCannotConvertToObject = 'Cannot convert data to object value';
SErrInvalidFloat = 'Invalid float value : %s';
SErrCannotSetNotIsNull = 'IsNull cannot be set to False';
SErrCannotAddArrayTwice = 'Adding an array object to an array twice is not allowed';
SErrCannotAddObjectTwice = 'Adding an object to an array twice is not allowed';
SErrUnknownTypeInConstructor = 'Unknown type in JSON%s constructor: %d';
SErrNotJSONData = 'Cannot add object of type %s to TJSON%s';
SErrPointerNotNil = 'Cannot add non-nil pointer to JSON%s';
SErrOddNumber = 'TJSONObject must be constructed with name,value pairs';
SErrNameMustBeString = 'TJSONObject constructor element name at pos %d is not a string';
SErrNonexistentElement = 'Unknown object member: "%s"';
SErrPathElementNotFound = 'Path "%s" invalid: element "%s" not found.';
SErrWrongInstanceClass = 'Cannot set instance class: %s does not descend from %s.';
SErrNoParserHandler = 'No JSON parser handler installed. Recompile your project with the jsonparser unit included';
Var
DefaultJSONInstanceTypes :
Array [TJSONInstanceType] of TJSONDataClass = (TJSONData, TJSONIntegerNumber,
TJSONInt64Number, TJSONQWordNumber, TJSONFloatNumber, TJSONString, TJSONBoolean, TJSONNull, TJSONArray,
TJSONObject);
Const
MinJSONInstanceTypes :
Array [TJSONInstanceType] of TJSONDataClass = (TJSONData, TJSONIntegerNumber,
TJSONInt64Number, TJSONQWordNumber, TJSONFloatNumber, TJSONString, TJSONBoolean, TJSONNull, TJSONArray,
TJSONObject);
function SetJSONInstanceType(AType: TJSONInstanceType; AClass: TJSONDataClass): TJSONDataClass;
begin
if AClass=Nil then
TJSONData.DoError(SErrWrongInstanceClass,['Nil',MinJSONInstanceTypes[AType].ClassName]);
if Not AClass.InheritsFrom(MinJSONINstanceTypes[AType]) then
TJSONData.DoError(SErrWrongInstanceClass,[AClass.ClassName,MinJSONInstanceTypes[AType].ClassName]);
Result:=DefaultJSONInstanceTypes[AType];
DefaultJSONINstanceTypes[AType]:=AClass;
end;
function GetJSONInstanceType(AType: TJSONInstanceType): TJSONDataClass;
begin
Result:=DefaultJSONInstanceTypes[AType]
end;
function StringToJSONString(const S: TJSONStringType; Strict : Boolean = False): TJSONStringType;
Var
I,J,L : Integer;
P : PJSONCharType;
C : AnsiChar;
begin
I:=1;
J:=1;
Result:='';
L:=Length(S);
P:=PJSONCharType(S);
While I<=L do
begin
C:=AnsiChar(P^);
if (C in ['"','/','\',#0..#31]) then
begin
Result:=Result+Copy(S,J,I-J);
Case C of
'\' : Result:=Result+'\\';
'/' : if Strict then
Result:=Result+'\/'
else
Result:=Result+'/';
'"' : Result:=Result+'\"';
#8 : Result:=Result+'\b';
#9 : Result:=Result+'\t';
#10 : Result:=Result+'\n';
#12 : Result:=Result+'\f';
#13 : Result:=Result+'\r';
else
Result:=Result+'\u'+HexStr(Ord(C),4);
end;
J:=I+1;
end;
Inc(I);
Inc(P);
end;
Result:=Result+Copy(S,J,I-1);
end;
function JSONStringToString(const S: TJSONStringType): TJSONStringType;
Var
I,J,L : Integer;
P : PJSONCharType;
w : String;
begin
I:=1;
J:=1;
L:=Length(S);
Result:='';
P:=PJSONCharType(S);
While (I<=L) do
begin
if (P^='\') then
begin
Result:=Result+Copy(S,J,I-J);
Inc(P);
If (P^<>#0) then
begin
Inc(I);
Case AnsiChar(P^) of
'\','"','/'
: Result:=Result+P^;
'b' : Result:=Result+#8;
't' : Result:=Result+#9;
'n' : Result:=Result+#10;
'f' : Result:=Result+#12;
'r' : Result:=Result+#13;
'u' : begin
W:=Copy(S,I+1,4);
Inc(I,4);
Inc(P,4);
Result:=Result+WideChar(StrToInt('$'+W));
end;
end;
end;
J:=I+1;
end;
Inc(I);
Inc(P);
end;
Result:=Result+Copy(S,J,I-J+1);
end;
function JSONTypeName(JSONType: TJSONType): String;
begin
Result:=GetEnumName(TypeInfo(TJSONType),Ord(JSONType));
end;
function CreateJSON: TJSONNull;
begin
Result:=TJSONNullClass(DefaultJSONInstanceTypes[jitNull]).Create
end;
function CreateJSON(Data: Boolean): TJSONBoolean;
begin
Result:=TJSONBooleanClass(DefaultJSONInstanceTypes[jitBoolean]).Create(Data);
end;
function CreateJSON(Data: Integer): TJSONIntegerNumber;
begin
Result:=TJSONIntegerNumberCLass(DefaultJSONInstanceTypes[jitNumberInteger]).Create(Data);
end;
function CreateJSON(Data: Int64): TJSONInt64Number;
begin
Result:=TJSONInt64NumberCLass(DefaultJSONInstanceTypes[jitNumberInt64]).Create(Data);
end;
function CreateJSON(Data: QWord): TJSONQWordNumber;
begin
Result:=TJSONQWordNumberClass(DefaultJSONInstanceTypes[jitNumberQWord]).Create(Data);
end;
function CreateJSON(Data: TJSONFloat): TJSONFloatNumber;
begin
Result:=TJSONFloatNumberCLass(DefaultJSONInstanceTypes[jitNumberFloat]).Create(Data);
end;
function CreateJSON(const Data: TJSONStringType): TJSONString;
begin
Result:=TJSONStringCLass(DefaultJSONInstanceTypes[jitString]).Create(Data);
end;
function CreateJSON(const Data: TJSONUnicodeStringType): TJSONString;
begin
Result:=TJSONStringCLass(DefaultJSONInstanceTypes[jitString]).Create(Data);
end;
function CreateJSONArray(const Data: array of const): TJSONArray;
begin
Result:=TJSONArrayCLass(DefaultJSONInstanceTypes[jitArray]).Create(Data);
end;
function CreateJSONObject(const Data: array of const): TJSONObject;
begin
Result:=TJSONObjectClass(DefaultJSONInstanceTypes[jitObject]).Create(Data);
end;
Var
JPH : TJSONParserHandler;
function GetJSON(const JSON: TJSONStringType; const UseUTF8: Boolean): TJSONData;
Var
SS : TM_StringStream;
begin
SS:=TM_StringStream.Create(JSON);
try
Result:=GetJSON(SS,UseUTF8);
finally
SS.Free;
end;
end;
function GetJSON(const JSON: TM_Stream; const UseUTF8: Boolean): TJSONData;
begin
Result:=Nil;
If (JPH=Nil) then
TJSONData.DoError(SErrNoParserHandler);
JPH(JSON,UseUTF8,Result);
end;
function SetJSONParserHandler(AHandler: TJSONParserHandler): TJSONParserHandler;
begin
Result:=JPH;
JPH:=AHandler;
end;
function GetJSONParserHandler: TJSONParserHandler;
begin
Result:=JPH;
end;
Type
{ TJSONEnumerator }
TJSONEnumerator = class(TBaseJSONEnumerator)
Private
FData : TJSONData;
public
Constructor Create(AData : TJSONData);
function GetCurrent: TJSONEnum; override;
function MoveNext : Boolean; override;
end;
{ TJSONArrayEnumerator }
TJSONArrayEnumerator = class(TBaseJSONEnumerator)
Private
FData : TJSONArray;
FCurrent : Integer;
public
Constructor Create(AData : TJSONArray);
function GetCurrent: TJSONEnum; override;
function MoveNext : Boolean; override;
end;
{ TJSONObjectEnumerator }
TJSONObjectEnumerator = class(TBaseJSONEnumerator)
Private
FData : TJSONObject;
FCurrent : Integer;
public
Constructor Create(AData : TJSONObject);
function GetCurrent: TJSONEnum; override;
function MoveNext : Boolean; override;
end;
{ TJSONQWordNumber }
function TJSONQWordNumber.GetAsBoolean: Boolean;
begin
Result:=FValue<>0;
end;
function TJSONQWordNumber.GetAsFloat: TJSONFloat;
begin
Result:= FValue;
end;
function TJSONQWordNumber.GetAsInteger: Integer;
begin
Result := FValue;
end;
function TJSONQWordNumber.GetAsInt64: Int64;
begin
Result := FValue;
end;
function TJSONQWordNumber.GetAsQWord: QWord;
begin
Result := FValue;
end;
procedure TJSONQWordNumber.SetAsBoolean(const AValue: Boolean);
begin
FValue:=Ord(AValue);
end;
procedure TJSONQWordNumber.SetAsFloat(const AValue: TJSONFloat);
begin
FValue:=Round(AValue);
end;
procedure TJSONQWordNumber.SetAsInteger(const AValue: Integer);
begin
FValue:=AValue;
end;
procedure TJSONQWordNumber.SetAsInt64(const AValue: Int64);
begin
FValue := AValue;
end;
procedure TJSONQWordNumber.SetAsQword(const AValue: QWord);
begin
FValue:=AValue;
end;
function TJSONQWordNumber.GetAsJSON: TJSONStringType;
begin
Result:=AsString;
end;
function TJSONQWordNumber.GetAsString: TJSONStringType;
begin
Result:=IntToStr(FValue);
end;
procedure TJSONQWordNumber.SetAsString(const AValue: TJSONStringType);
begin
FValue:=StrToQWord(AValue);
end;
function TJSONQWordNumber.GetValue: variant;
begin
Result:=FValue;
end;
procedure TJSONQWordNumber.SetValue(const AValue: variant);
begin
FValue:=AValue;
end;
constructor TJSONQWordNumber.Create(AValue: QWord);
begin
FValue := AValue;
end;
class function TJSONQWordNumber.NumberType: TJSONNumberType;
begin
Result:=ntQWord;
end;
procedure TJSONQWordNumber.Clear;
begin
FValue:=0;
end;
function TJSONQWordNumber.Clone: TJSONData;
begin
Result:=TJSONQWordNumberClass(ClassType).Create(Self.FValue);
end;
constructor TJSONObjectEnumerator.Create(AData: TJSONObject);
begin
FData:=AData;
FCurrent:=-1;
end;
function TJSONObjectEnumerator.GetCurrent: TJSONEnum;
begin
Result.KeyNum:=FCurrent;
Result.Key:=FData.Names[FCurrent];
Result.Value:=FData.Items[FCurrent];
end;
function TJSONObjectEnumerator.MoveNext: Boolean;
begin