forked from onryldz/x-superobject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XSuperObject.pas
3469 lines (3052 loc) · 103 KB
/
XSuperObject.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
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
(*
* XSuperObject - Simple JSON Framework
*
* The MIT License (MIT)
* Copyright (c) 2015 Onur YILDIZ
*
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall
* be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
* THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*)
unit XSuperObject;
interface
(*
* Marshalling Options
*)
{$DEFINE SP_DATASET}
{$DEFINE SP_STREAM}
// ** Zero Based Strings Definations...
{$UNDEF XE2UP}
{$IFDEF DCC}
{$IF CompilerVersion >= 24}
{$DEFINE XE2UP}
{$ENDIF}
{$ENDIF}
uses
Classes,
Variants,
SysUtils,
Character,
XSuperJSON,
RTTI,
TypInfo,
Generics.Collections
{$IFDEF SP_DATASET}
,DB
{$ENDIF}
{$IFDEF SP_STREAM}
,IdGlobal
,IdCoderMIME
{$ENDIF}
;
{$IFDEF XE2UP}
const CharIndex = Low(String);
{$ELSE}
const CharIndex = 1;
{$ENDIF}
type
SOException = class(Exception) end;
SOInvalidDate = class(SOException) end;
ESerializeError = class(Exception) end;
ISuperObject = interface;
ISuperArray = interface;
ICast = Interface;
IMember = ICast;
TSuperObject = class;
TSuperArray = class;
TMemberStatus = (jUnAssigned, jNull, jAssigned);
TJSONType = (jtObject, jtArray);
Alias = class(TCustomAttribute)
private
FName: String;
public
constructor Create(const AName: String);
property Name: String read FName write FName;
end;
TRevalOption = (roNone, roEmptyArrayToNull);
REVAL = class(TCustomAttribute)
private
FOption: TRevalOption;
FEqual: Variant;
FValue: Variant;
public
constructor Create(EQVal: String; NewVal: String); overload;
constructor Create(EQVal: Integer; NewVal: Integer); overload;
constructor Create(EQVal: Boolean; NewVal: Boolean); overload;
constructor Create(EQVal: Double; NewVal: Double); overload;
constructor Create(EQVal: String); overload;
constructor Create(EQVal: Integer); overload;
constructor Create(EQVal: Double); overload;
constructor Create(EQVal: Boolean); overload;
constructor Create(Option: TRevalOption); overload;
function CheckEQ(Val: TValue): Boolean;
property Equal: Variant read FEqual;
property Value: Variant read FValue;
property Option: TRevalOption read FOption;
end;
DISABLE = class(TCustomAttribute)
end;
DISABLEREAD = class(TCustomAttribute)
end;
DISABLEWRITE = class(TCustomAttribute)
end;
IBase = interface
['{872FA14E-9276-4F86-A8D8-832CF39DACE6}']
function AsObject: ISuperObject;
function AsArray: ISuperArray;
end;
TBase = class(TInterfacedObject, IBase)
function AsObject: ISuperObject; virtual;
function AsArray: ISuperArray; virtual;
end;
IBaseJSON<T, Typ> = interface(IBase)
['{EBD49266-BEF2-4B79-9BAF-329F725E0568}']
function GetBoolean(V: Typ): Boolean;
function GetInteger(V: Typ): Int64;
function GetString(V: Typ): String;
procedure SetBoolean(V: Typ; const Value: Boolean);
procedure SetInteger(V: Typ; const Value: Int64);
procedure SetString(V: Typ; const Value: String);
function GetObject(V: Typ): ISuperObject;
procedure SetObject(V: Typ; const Value: ISuperObject);
function GetArray(V: Typ): ISuperArray;
procedure SetArray(V: Typ; const Value: ISuperArray);
function GetDouble(V: Typ): Double;
procedure SetDouble(V: Typ; const Value: Double);
function GetVariant(V: Typ): Variant;
procedure SetVariant(V: Typ; const Value: Variant);
function GetDateTime(V: Typ): TDateTime;
procedure SetDateTime(V: Typ; const Value: TDateTime);
function GetDate(V: Typ): TDate;
procedure SetDate(V: Typ; const Value: TDate);
function GetTime(V: Typ): TTime;
procedure SetTime(V: Typ; const Value: TTime);
function GetSelf: T;
function GetAncestor(V: Typ): IJSONAncestor;
function GetNull(V: Typ): TMemberStatus;
procedure SetNull(V: Typ; const Value: TMemberStatus);
function GetDataType: TDataType;
property Null[V: Typ]: TMemberStatus read GetNull write SetNull;
property S[V: Typ]: String read GetString write SetString;
property I[V: Typ]: Int64 read GetInteger write SetInteger;
property B[V: Typ]: Boolean read GetBoolean write SetBoolean;
property F[V: Typ]: Double read GetDouble write SetDouble;
property O[V: Typ]: ISuperObject read GetObject write SetObject;
property A[V: Typ]: ISuperArray read GetArray write SetArray;
property V[V: Typ]: Variant read GetVariant write SetVariant;
property D[V: Typ]: TDateTime read GetDateTime write SetDateTime;
property Date[V: Typ]: TDate read GetDate write SetDate;
property Time[V: Typ]: TTime read GetTime write SetTime;
property Ancestor[V: Typ]: IJSONAncestor read GetAncestor;
function Contains(Key: Typ): Boolean;
function GetType(Key: Typ): TVarType;
procedure Sort(Comparison: TJSONComparison<IMember>);
procedure SaveTo(Stream: TStream; const Ident: Boolean = false; const UniversalTime : Boolean = false); overload;
procedure SaveTo(AFile: String; const Ident: Boolean = false; const UniversalTime : Boolean = false); overload;
function AsJSON(const Ident: Boolean = False; const UniversalTime: Boolean = False): String;
property Self: T read GetSelf;
property DataType: TDataType read GetDataType;
end;
TJSONValueHelper = class helper for TJSONAncestor
public
function ValueEx<T>: Variant;
end;
TCondCallBack<T> = reference to function(Arg: T): Boolean;
TBaseJSON<T, Typ> = class(TBase, IBaseJSON<T, Typ>)
protected
FJSONObj: T;
FCasted: IJSONAncestor;
FInterface: IInterface;
FCheckDate: Boolean;
function ContainsEx(Key: Typ; out Value: IJSONAncestor): Boolean;
function DefaultValueClass<TT: Class>(const Value): TT;
procedure Member<MT: Class; TValue>(const Name: Typ; const Value: TValue); overload;
function Member(const Name: Typ): Boolean; overload;
function GetValue<C: Class>(const Name: Typ): C;
function GetSelf: T;
function GetData(Key: Typ): IJSONAncestor;
function GetVariant(V: Typ): Variant;
procedure SetVariant(V: Typ; const Value: Variant);
function GetDataType: TDataType;
protected
function GetObject(V: Typ): ISuperObject; virtual;
function GetArray(V: Typ): ISuperArray; virtual;
function GetBoolean(V: Typ): Boolean; virtual;
function GetInteger(V: Typ): Int64; virtual;
function GetString(V: Typ): String; virtual;
function GetDouble(V: Typ): Double; virtual;
function GetAncestor(V: Typ): IJSONAncestor; inline;
function GetNull(V: Typ): TMemberStatus; virtual;
function GetDateTime(V: Typ): TDateTime; virtual;
function GetDate(V: Typ): TDate; virtual;
function GetTime(V: Typ): TTime; virtual;
procedure SetDate(V: Typ; const Value: TDate); virtual;
procedure SetTime(V: Typ; const Value: TTime); virtual;
procedure SetDateTime(V: Typ; const Value: TDateTime); virtual;
procedure SetObject(V: Typ; const Value: ISuperObject); virtual;
procedure SetArray(V: Typ; const Value: ISuperArray); virtual;
procedure SetBoolean(V: Typ; const Value: Boolean); virtual;
procedure SetInteger(V: Typ; const Value: Int64); virtual;
procedure SetString(V: Typ; const Value: String); virtual;
procedure SetDouble(V: Typ; const Value: Double); virtual;
procedure SetNull(V: Typ; const Value: TMemberStatus); virtual;
public
constructor Create(JSON: String = '{}'; const CheckDate: Boolean = True); overload;
constructor Create(JSON: T; const CheckDate: Boolean = True); overload;
constructor CreateCasted(Value: IJSONAncestor; const CheckDate: Boolean = True);
constructor CreateWithEscape(JSON: String = '{}'; const CheckDate: Boolean = True);
destructor Destroy; override;
property Null[V: Typ]: TMemberStatus read GetNull write SetNull;
property S[V: Typ]: String read GetString write SetString;
property I[V: Typ]: Int64 read GetInteger write SetInteger;
property B[V: Typ]: Boolean read GetBoolean write SetBoolean;
property F[V: Typ]: Double read GetDouble write SetDouble;
property O[V: Typ]: ISuperObject read GetObject write SetObject;
property A[V: Typ]: ISuperArray read GetArray write SetArray;
property V[V: Typ]: Variant read GetVariant write SetVariant;
property D[V: Typ]: TDateTime read GetDateTime write SetDateTime;
property Date[V: Typ]: TDate read GetDate write SetDate;
property Time[V: Typ]: TTime read GetTime write SetTime;
property Ancestor[V: Typ]: IJSONAncestor read GetAncestor;
function Contains(Key: Typ): Boolean;
function GetType(Key: Typ): TVarType;
procedure Sort(Comparison: TJSONComparison<IMember>); virtual; abstract;
procedure SaveTo(Stream: TStream; const Ident: Boolean = false; const UniversalTime : Boolean = false); overload; virtual; abstract;
procedure SaveTo(AFile: String; const Ident: Boolean = false; const UniversalTime : Boolean = false); overload; virtual; abstract;
function AsJSON(const Ident: Boolean = False; const UniversalTime: Boolean = False): String; inline;
property Self: T read GetSelf;
property DataType: TDataType read GetDataType;
end;
ICast = interface
['{0F5387AB-C1C9-4229-921D-226960332271}']
function GetArray: ISuperArray;
function GetBoolean: Boolean;
function GetDataType: TDataType;
function GetFloat: Double;
function GetInteger: Int64;
function GetObject: ISuperObject;
function GetString: String;
function GetName: String;
function GetVariant: Variant;
function GetDate: TDate;
function GetDateTime: TDateTime;
function GetTime: TTime;
procedure SetDate(const Value: TDate);
procedure SetDateTime(const Value: TDateTime);
procedure SetTime(const Value: TTime);
procedure SetBoolean(const Value: Boolean);
procedure SetFloat(const Value: Double);
procedure SetInteger(const Value: Int64);
procedure SetString(const Value: String);
procedure SetVariant(const Value: Variant);
property AsObject: ISuperObject read GetObject;
property AsArray: ISuperArray read GetArray;
property AsString: String read GetString write SetString;
property AsInteger: Int64 read GetInteger write SetInteger;
property AsFloat: Double read GetFloat write SetFloat;
property AsBoolean: Boolean read GetBoolean write SetBoolean;
property AsVariant: Variant read GetVariant write SetVariant;
property AsDateTime: TDateTime read GetDateTime write SetDateTime;
property AsDate: TDate read GetDate write SetDate;
property AsTime: TTime read GetTime write SetTime;
property DataType: TDataType read GetDataType;
property Name: String read GetName;
function ToString(const Ident: Boolean = False; const UniversalTime: Boolean = False): String;
end;
TCast = class(TInterfacedObject, ICast)
private
FJSON: IJSONAncestor;
FName: String;
function GetArray: ISuperArray;
function GetBoolean: Boolean;
function GetDataType: TDataType;
function GetFloat: Double;
function GetInteger: Int64;
function GetObject: ISuperObject;
function GetString: String;
procedure SetBoolean(const Value: Boolean);
procedure SetFloat(const Value: Double);
procedure SetInteger(const Value: Int64);
procedure SetString(const Value: String);
function GetName: String;
function GetVariant: Variant;
procedure SetVariant(const Value: Variant);
function GetDate: TDate;
function GetDateTime: TDateTime;
function GetTime: TTime;
procedure SetDate(const Value: TDate);
procedure SetDateTime(const Value: TDateTime);
procedure SetTime(const Value: TTime);
public
constructor Create(Base: IJSONAncestor); overload;
constructor Create(Base: IJSONPair); overload;
class function CreateFrom<T>(Base: T): ICast;
destructor Destroy; override;
property AsObject: ISuperObject read GetObject;
property AsArray: ISuperArray read GetArray;
property AsString: String read GetString write SetString;
property AsInteger: Int64 read GetInteger write SetInteger;
property AsFloat: Double read GetFloat write SetFloat;
property AsBoolean: Boolean read GetBoolean write SetBoolean;
property AsVariant: Variant read GetVariant write SetVariant;
property AsDateTime: TDateTime read GetDateTime write SetDateTime;
property AsDate: TDate read GetDate write SetDate;
property AsTime: TTime read GetTime write SetTime;
property DataType: TDataType read GetDataType;
property Name: String read GetName;
function ToString(const Ident: Boolean = False; const UniversalTime: Boolean = False): String; reintroduce;
end;
ISuperExpression = interface(ICast)
['{58366F15-0D83-4BC5-85D5-238E78E73247}']
end;
TSuperExpression = class(TCast, ISuperExpression)
private
FInterpreter: TJSONInterpreter;
public
constructor Create(Base: IJSONAncestor; const Expr: String; const BlockException: Boolean = False);
destructor Destroy; override;
end;
TSuperEnumerator<T> = record
Index : Integer;
List : TJSONEnumerator<T>;
function MoveNext : Boolean;
function GetCurrent : ICast;
property Current : ICast read GetCurrent;
end;
ISuperObject = interface(IBaseJSON<IJSONObject, String>)
['{B7E271F3-205B-4172-8532-BE03F2A6EDE7}']
procedure First;
procedure Next;
function GetEoF: Boolean;
function GetCount: Integer;
function GetCurrentKey: String;
function GetCurrentValue: IJSONAncestor;
function GetOffset: Integer;
function GetExpr(const Code: String): ISuperExpression;
function GetRaw(V: String): String;
procedure SetRaw(V: String; Value: String);
procedure Add(const Key: String; const Data: IJSONAncestor);
procedure SetData(V: String; Data: Variant); overload;
procedure SetData(V: String; Data: Variant; AFormatSettings: TFormatSettings); overload;
procedure Remove(Key: String);
function Check(const Expr: String): Boolean;
property Expression[const Code: String]: ISuperExpression read GetExpr; default;
property Count: Integer read GetCount;
property EoF: Boolean read GetEoF;
property CurrentKey: String read GetCurrentKey;
property CurrentValue: IJSONAncestor read GetCurrentValue;
property Offset: Integer read GetOffset;
function Clone: ISuperObject;
function GetEnumerator: TSuperEnumerator<IJSONPair>;
function T: TSuperObject;
function Where(const Cond: TCondCallBack<IMember>): ISuperObject;
function Delete(const Cond: TCondCallBack<IMember>): ISuperObject;
function Cast: ICast;
property Raw[V: String]: String read GetRaw write SetRaw;
end;
TSuperObject = class(TBaseJSON<IJSONObject, String>, ISuperObject)
private
FOffset: Integer;
function GetEoF: Boolean;
function GetCount: Integer;
function GetCurrentKey: String;
function GetCurrentValue: IJSONAncestor;
function GetOffset: Integer;
function GetExpr(const Code: String): ISuperExpression;
function GetRaw(V: String): String;
procedure SetRaw(V: String; Value: String);
protected
function GetString(V: String): String; override;
procedure SetNull(V: String; const Value: TMemberStatus); override;
public
procedure First;
procedure Next;
procedure Add(const Key: String; const Data: IJSONAncestor);
procedure SetData(V: String; Data: Variant); overload; inline;
procedure SetData(V: String; Data: Variant; AFormatSettings: TFormatSettings); overload;
class function ParseStream(Stream: TStream; CheckDate: Boolean = True): TSuperObject;
class function ParseFile(FileName: String; CheckDate: Boolean = True): TSuperObject;
procedure SaveTo(Stream: TStream; const Ident: Boolean = false; const UniversalTime : Boolean = false); overload; override;
procedure SaveTo(AFile: String; const Ident: Boolean = false; const UniversalTime : Boolean = false); overload; override;
procedure Remove(Key: String);
function Check(const Expr: String): Boolean;
property Expression[const Code: String]: ISuperExpression read GetExpr; default;
property Count: Integer read GetCount;
property Offset: Integer read GetOffset;
property EoF: Boolean read GetEoF;
property CurrentKey: String read GetCurrentKey;
property CurrentValue: IJSONAncestor read GetCurrentValue;
function GetEnumerator: TSuperEnumerator<IJSONPair>;
function AsType<T>: T;
function T: TSuperObject; inline;
function Clone: ISuperObject;
function AsObject: ISuperObject; override;
function AsArray: ISuperArray; override;
procedure Sort(Comparison: TJSONComparison<IMember>); override;
function Where(const Cond: TCondCallBack<IMember>): ISuperObject;
function Delete(const Cond: TCondCallBack<IMember>): ISuperObject;
function Cast: ICast;
end;
ISuperArray = interface(IBaseJSON<IJSONArray, Integer>)
['{41A2D578-CFAB-4924-8F15-0D0227F35412}']
function GetLength: Integer;
property Length: Integer read GetLength;
procedure Add(Value: IJSONAncestor); overload;
procedure Add(Value: ISuperArray); overload;
procedure Add(Value: ISuperObject); overload;
procedure Add(Value: Variant; DateFormat: TFormatSettings); overload;
procedure Add(Value: Variant); overload;
procedure Delete(Index: Integer); overload;
procedure Clear;
function Clone: ISuperArray;
function GetEnumerator: TSuperEnumerator<IJSONAncestor>;
function T: TSuperArray;
function Where(const Cond: TCondCallBack<IMember>): ISuperArray;
function Delete(const Cond: TCondCallBack<IMember>): ISuperArray; overload;
end;
TSuperArray = class(TBaseJSON<IJSONArray, Integer>, ISuperArray)
private
function GetLength: Integer;
protected
procedure SetNull(V: Integer; const aValue: TMemberStatus); override;
public
procedure Add(Value: IJSONAncestor); overload;
procedure Add(Value: ISuperObject); overload;
procedure Add(Value: ISuperArray); overload;
procedure Add(Value: Variant; DateFormat: TFormatSettings); overload;
procedure Add(Value: Variant); overload;
procedure Delete(Index: Integer); overload;
function Delete(const Cond: TCondCallBack<IMember>): ISuperArray; overload;
procedure Clear;
property Length: Integer read GetLength;
function GetEnumerator: TSuperEnumerator<IJSONAncestor>;
procedure SaveTo(Stream: TStream; const Ident: Boolean = false; const UniversalTime : Boolean = false); overload; override;
procedure SaveTo(AFile: String; const Ident: Boolean = false; const UniversalTime : Boolean = false); overload; override;
procedure Sort(Comparison: TJSONComparison<IMember>); override;
function Clone: ISuperArray;
function AsArray: ISuperArray; override;
function AsObject: ISuperObject; override;
function Where(const Cond: TCondCallBack<IMember>): ISuperArray;
function T: TSuperArray; inline;
function AsType<T>: T;
end;
TSuperProperty = class(TRttiProperty)
public
ArrayRawData: Pointer;
end;
TSuperField = class(TRttiField)
public
ArrayRawData: Pointer;
end;
TSuperDynArr = class(TRttiDynamicArrayType)
public
ArrayRawData: Pointer;
end;
TSuperArr = class(TRttiArrayType)
public
ArrayRawData: Pointer;
end;
TGenericsType = (gtNil, gtList, gtObjectList);
TGenericsInfo = class
private
FContext: TRttiContext;
FType: TRttiType;
FAddMethod: TRttiMethod;
FCountProperty: TRttiProperty;
FGetItemMethod: TRttiIndexedProperty;
public
IsGeneric: Boolean;
Typ: TRttiType;
CreateArgs: TArray<TValue>;
procedure AddVal(Instance: TObject; Val: TValue);
function Count(Instance: TObject): Integer;
function Item(Instance: TObject; const Index: Integer): TObject;
constructor Create(GenericClass: TClass; const AIsGeneric: Boolean; AType: TRttiType);
destructor Destroy; override;
end;
TAttributeClass = class of TCustomAttribute;
TPropertyGetterType = (pgtField, pgtMethod);
TSerializeParse = class
private
class var FGenericsCache: TObjectDictionary<TClass, TGenericsInfo>;
class function GetAttribute(AttributeType: TAttributeClass; Attributes: TArray<TCustomAttribute>): TCustomAttribute;
class procedure GetAliasName(const Attributes: TArray<TCustomAttribute>; var Result: String);
class function GetREVAL(const Attributues: TArray<TCustomAttribute>): REVAL;
class function IsDisabled(const Attributes: TArray<TCustomAttribute>): Boolean; inline;
class function IsDisabledRead(const Attributes: TArray<TCustomAttribute>): Boolean; inline;
class function IsDisabledWrite(const Attributes: TArray<TCustomAttribute>): Boolean; inline;
class function PropGetterType(Prop: TRttiProperty): TPropertyGetterType;
public
class constructor Create;
class destructor Destroy;
class function IsGenerics(Cls: TRttiType): Boolean; overload;
class function IsGenerics(Cls: TClass): Boolean; overload;
class function IsCollection(Cls: TRttiType): Boolean; overload;
class function IsCollection(Cls: TClass): Boolean; overload; inline;
class function GetGenericType(Cls: TClass): TGenericsType;
class function GetGenericsCreateArgs(Cls: TRttiType): TArray<TValue>;
// ** Read
{$IFDEF SP_STREAM}
class procedure ReadStream(AStream: TStream; IResult: IJSONAncestor);
{$ENDIF}
class procedure ReadGeneric(AObject: TObject; IResult: ISuperArray);
class procedure ReadCollection(ACollection: TCollection; IResult: ISuperArray);
class procedure ReadObject(AObject: TObject; IResult: ISuperObject);
class procedure ReadRecord(Info: PTypeInfo; ARecord: Pointer; IResult: ISuperObject);
class function ReadRecordEx<T>(Rec: T): ISuperObject;
class procedure ReadMembers(Data: Pointer; aType: TRttiType; IJsonData: ISuperObject);
class procedure ReadMember<T, Typ>(Member: Typ; RType: PTypeInfo; MemberValue: TValue; IJsonData: IBaseJSON<T, Typ>);
class procedure ReadSet(Val: TValue; IJsonData: ISuperArray);
class procedure ReadVariantOfArray(Val: Variant; IJsonData: ISuperArray);
class procedure ReadTValueOfArray(Val: TValue; IJsonData: ISuperArray);
class procedure ReadVariantOfObject(Val: Variant; const Name: String; IJsonData: ISuperObject);
// ** Write
{$IFDEF SP_STREAM}
class procedure WriteStream(AStream: TStream; IData: IJSONAncestor);
{$ENDIF}
class procedure WriteGeneric(AObject: TObject; IData: ISuperArray);
class procedure WriteCollection(ACollection: TCollection; IData: ISuperArray);
class procedure WriteObject(AObject: TObject; IData: ISuperObject);
class procedure WriteRecord(Info: PTypeInfo; ARecord: Pointer; IData: ISuperObject);
class procedure WriteRecordEx<T>(Rec: T; IData: ISuperObject);
class procedure WriteMembers(Data: Pointer; aType: TRttiType; IJsonData: ISuperObject);
class procedure WriteMember<T, Typ>(Data: Pointer; Member: Typ; RType: PTypeInfo; MemberValue: TRttiObject; IJsonData: IBaseJSON<T, Typ>);
class procedure WriteSet(Data: Pointer; Member: TRttiObject; IJSONData: ISuperArray);
class procedure SetValue<Typ>(var Data: Pointer; Member: TRttiObject; MIdx: Typ; Val: TValue);
class function GetValue<Typ>(Data: Pointer; Member: TRttiObject; MIdx: Typ): TValue;
class function GetMemberTypeInfo(Member: TRttiObject; const GetArray: Boolean = true): PTypeInfo; inline;
class function GetMemberType(Member: TRttiObject; const GetArray: Boolean = true): TRttiType; //inline;
class function GetArrayRawData(Member: TRttiObject): Pointer;
class procedure SetArrayRawData(Member: TRttiObject; RawData: Pointer);
class procedure ClearArrayRawData(Member: TRttiObject);
class function ObjectConstructorParamCount(Instance: TClass): Integer;
class function ObjectConstructor(Instance: TClass): TObject;
class function CheckObject<Typ>(Data: Pointer; Member: TRttiObject; MIdx: Typ; var Obj: TObject): Boolean;
class property GenericsCache: TObjectDictionary<TClass, TGenericsInfo> read FGenericsCache;
end;
TMemberVisibilities = set of TMemberVisibility;
TSerializeParseOptions = class
private
class var FVisibilities: TMemberVisibilities;
public
class constructor Create;
class property Visibilities: TMemberVisibilities read FVisibilities write FVisibilities;
end;
TSuperObjectHelper = class helper for TObject
public
function AsJSON(const Ident: Boolean = False; const UniversalTime: Boolean = False): String;
function AsJSONObject: ISuperObject;
procedure AssignFromJSON(const JSON: String); overload;
procedure AssignFromJSON(JSON: ISuperObject); overload;
constructor FromJSON(const JSON: String); overload;
constructor FromJSON(JSON: ISuperObject); overload;
constructor FromJSON(const JSON: String; CreateArgs: Array of TValue; const ConstructMethod: String = 'Create'); overload;
constructor FromJSON(const JSON: ISuperObject; CreateArgs: Array of TValue; const ConstructMethod: String = 'Create'); overload;
end;
TBaseSuperRecord<T> = class
public
class function AsJSON(Rec: T): String;
class function AsJSONObject(Rec: T): ISuperObject;
class function FromJSON(JSON: String): T; overload;
class function FromJSON(JSON: ISuperObject): T; overload;
end;
TSuperRecord<T: Record> = class(TBaseSuperRecord<T>);
TJSON = class
public
class function Parse<T>(const Value: String): T; overload;
class function Parse<T>(JSON: ISuperObject): T; overload;
class function Parse<T>(JSON: ISuperArray): T; overload;
class function SuperObject<T>(Value: T): ISuperObject; overload;
{$IFDEF SP_DATASET}
class function SuperObject(Value: TDataSet): ISuperObject; overload;
class function Stringify(Value: TDataSet): String; overload;
{$ENDIF}
class function SuperObject(Value: TValue): ISuperObject; overload;
class function Stringify<T>(Value: T; Indent: Boolean = False; UniversalTime: Boolean = True): String; overload;
class function Stringify(Value: TValue; Indent: Boolean = False; UniversalTime: Boolean = True): String; overload;
end;
function SO(JSON: String = '{}'): ISuperObject; overload;
function SO(const Args: array of const): ISuperObject; overload;
function SA(JSON: String = '[]'): ISuperArray; overload;
function SA(const Args: array of const): ISuperArray; overload;
implementation
var GenericsUnit : String;
function SO(JSON: String): ISuperObject;
begin
if JSON = '' then JSON := '{}';
Result := TSuperObject.Create(JSON);
end;
function SO(const Args: array of const): ISuperObject;
var
I: Integer;
Members: ISuperArray;
begin
Result := TSuperObject.Create;
Members := SA(Args);
if Odd(Members.Length) then
Assert(False);
for I := 0 to (Members.Length div 2) - 1 do
Result.Add(Members.S[I*2], Members.Ancestor[(I*2)+1]);
end;
function SA(JSON: String): ISuperArray;
begin
Result := TSuperArray.Create(JSON);
end;
function SA(const Args: array of const): ISuperArray;
var
I: Integer;
SArray: ISuperArray;
SObject: ISuperObject;
begin
Result := TSuperArray.Create;
for I := 0 to High(Args) do
case PVarRec(@Args[I]).VType of
vtInteger : Result.Add(TJSONInteger.Create(PVarRec(@Args[I]).VInteger));
vtInt64 : Result.Add(TJSONInteger.Create(PVarRec(@Args[I]).VInt64^));
vtBoolean : Result.Add(TJSONBoolean.Create(PVarRec(@Args[I]).VBoolean));
{$IFNDEF NEXTGEN}
vtChar : Result.Add(TJSONString.Create(PVarRec(@Args[I]).VWideChar));
vtString : Result.Add(TJSONString.Create(String(PVarRec(@Args[I]).VString^)));
vtPChar : Result.Add(TJSONString.Create(Char(PVarRec(@Args[I]).VPChar^)));
vtAnsiString: Result.Add(TJSONString.Create(String(PVarRec(@Args[I]).VAnsiString)));
{$ENDIF}
vtWideChar: Result.Add(TJSONString.Create(PVarRec(@Args[I]).VWideChar));
vtExtended: Result.Add(TJSONFloat.Create(PVarRec(@Args[I]).VExtended^));
vtCurrency: Result.Add(TJSONFloat.Create(PVarRec(@Args[I]).VCurrency^));
vtWideString: Result.Add(TJSONString.Create(PWideChar(PVarRec(@Args[I]).VWideString)));
vtUnicodeString: Result.Add(TJSONString.Create(String(PVarRec(@Args[I]).VUnicodeString)));
vtInterface:
if PVarRec(@Args[I]).VInterface = nil then
Result.Add(TJSONNull.Create(False))
else if IInterface(PVarRec(@Args[I]).VInterface).QueryInterface(ISuperObject, SObject) = 0 then
Result.Add(SObject)
else if IInterface(PVarRec(@Args[I]).VInterface).QueryInterface(ISuperArray, SArray) = 0 then
Result.Add(SArray)
else
Assert(False);
vtPointer :
if PVarRec(@Args[I]).VPointer = nil then
Result.Add(TJSONNull.Create(False))
else
Result.Add(TJSONInteger.Create(NativeInt(PVarRec(@Args[I]).VPointer)));
vtVariant:
Result.Add(PVarRec(@Args[I]).VVariant^);
vtObject:
if PVarRec(@Args[I]).VPointer = nil then
Result.Add(TJSONNull.Create(False))
else
Result.Add(TJSONInteger.Create(NativeInt(PVarRec(@Args[I]).VPointer)));
vtClass:
if PVarRec(@Args[I]).VPointer = nil then
Result.Add(TJSONNull.Create(False))
else
Result.Add(TJSONInteger.Create(NativeInt(PVarRec(@Args[I]).VPointer)));
else
Assert(false);
end;
end;
{ TSuperObject }
constructor TBaseJSON<T, Typ>.Create(JSON: String; const CheckDate: Boolean);
type PInterface = ^IInterface;
var
JVal: IJSONAncestor;
PIntf: PInterface;
begin
FCheckDate := CheckDate;
if (Self.InheritsFrom(TSuperArray)) and (Trim(JSON) = '{}') then JSON := '[]';
JVal := TJSONObject.ParseJSONValue(JSON, FCheckDate);
if JVal.QueryInterface(GetTypeData(TypeInfo(T)).Guid, FJSONObj) = S_OK then
FInterface := TValue.From<T>(FJSONObj).AsInterface
else
FCasted := JVal
end;
function TBaseJSON<T, Typ>.GetValue<C>(const Name: Typ): C;
begin
if Self.InheritsFrom(TSuperObject) then
with TJSONObject(FInterface).Get(PString(@Name)^) do
if JsonValue is TJSONNull then
Result := Nil
else
Result := JSonValue as C
else
if Self.InheritsFrom(TSuperArray) then
Result := TJSONArray(FInterface).Get(PInteger(@Name)^) as C
else
Result := Nil;
end;
function TBaseJSON<T, Typ>.GetVariant(V: Typ): Variant;
begin
case GetType(V) of
varString: Result := S[V];
varInt64: Result := I[V];
varDouble: Result := F[V];
varBoolean: Result := B[V];
varDate: Result := D[V];
else
Result := Variants.Null;
end;
end;
function TBaseJSON<T, Typ>.Member(const Name: Typ): Boolean;
begin
if Self.InheritsFrom(TSuperObject) then
Result := Assigned(TJSONObject(FInterface).Get(PString(@Name)^))
else
Result := Assigned(TJSONArray(FInterface).Get(PInteger(@Name)^))
end;
procedure TBaseJSON<T, Typ>.Member<MT, TValue>(const Name: Typ; const Value: TValue);
var
Pair: IJSONPair;
begin
if Self.InheritsFrom(TSuperObject) then
begin
Pair := TJSONObject(FInterface).Get(PString(@Name)^);
if not Assigned(Pair) then
begin
TJSONObject(FInterface).AddPair(PString(@Name)^, DefaultValueClass<MT>(Value) as TJSONAncestor );
Exit;
end;
if Assigned(Pair.JsonValue) then
Pair.JsonValue := Nil;
Pair.JsonValue := DefaultValueClass<MT>(Value) as TJSONAncestor;
end
else
begin
if TJSONArray(FInterface).Count - 1 < PInteger(@Name)^ then
while TJSONArray(FInterface).Count - 1 < PInteger(@Name)^ do
TJSONArray(FInterface).Add(DefaultValueClass<MT>(Value) as TJSONAncestor)
else
TJSONArray(FInterface).Index[PInteger(@Name)^] := DefaultValueClass<MT>(Value) as TJSONAncestor
end;
end;
function TBaseJSON<T, Typ>.AsJSON(const Ident, UniversalTime: Boolean): String;
var
SBuild: TJSONWriter;
begin
try
SBuild := TJSONWriter.Create(Ident, UniversalTime);
if Assigned(FCasted) then
FCasted.AsJSONString(SBuild)
else
TJSONAncestor(FInterface).AsJSONString(SBuild);
Result := SBuild.ToString;
finally
SBuild.Free;
end;
end;
function TBaseJSON<T, Typ>.Contains(Key: Typ): Boolean;
begin
Result := GetData(Key) <> Nil;
end;
function TBaseJSON<T, Typ>.ContainsEx(Key: Typ; out Value: IJSONAncestor): Boolean;
begin
Value := GetData(Key);
Result := Value <> Nil;
end;
constructor TBaseJSON<T, Typ>.Create(JSON: T; const CheckDate: Boolean = True);
begin
FJSONObj := JSON;
FCasted := nil;
FCheckDate := CheckDate;
FInterface := TValue.From<T>(JSON).AsInterface;
end;
constructor TBaseJSON<T, Typ>.CreateCasted(Value: IJSONAncestor; const CheckDate: Boolean);
begin
// FJSONObj := Nil;
FInterface := Nil;
FCasted := Value;
FCheckDate := CheckDate;
end;
constructor TBaseJSON<T, Typ>.CreateWithEscape(JSON: String; const CheckDate: Boolean);
begin
Create(LimitedStrToUTF16(JSON), CheckDate);
end;
function TBaseJSON<T, Typ>.DefaultValueClass<TT>(const Value): TT;
var
r: TRttiContext;
ty: TRttiType;
begin
if TJSONString.InheritsFrom(TT) then
Result := TJSONString.Create(String(Value)) as TT
else if TJSONInteger.InheritsFrom(TT) then
Result := TJSONInteger.Create(Int64(Value)) as TT
else if TJSONFloat.InheritsFrom(TT) then
Result := TJSONFloat.Create(Double(Value)) as TT
else if TJSONBoolean.InheritsFrom(TT) then
Result := TJSONBoolean.Create(Boolean(Value)) as TT
else if TJSONNull.InheritsFrom(TT) then
Result := TJSONNull.Create(Boolean(Value)) as TT
else if TJSONDateTime.InheritsFrom(TT) then
Result := TJSONDateTime.Create(TDateTime(Value)) as TT
else if TJSONDate.InheritsFrom(TT) then
Result := TJSONDate.Create(TDate(Value)) as TT
else if TJSONTime.InheritsFrom(TT) then
Result := TJSONTime.Create(TTime(Value)) as TT
else if TJSONRaw.InheritsFrom(TT) then
Result := TJSONRaw.Create(String(Value)) as TT
else if TJSONArray.InheritsFrom(TT) then
begin
if Pointer(Value) <> Nil then
Exit(TJSONArray(ISuperArray(Value)) as TT);
Result := TJSONArray.Create as TT;
end
else if TJSONObject.InheritsFrom(TT) then
begin
if Pointer(Value) <> Nil then
Exit(TJSONObject(ISuperObject(Value)) as TT);
Result := TJSONObject.Create as TT;
end
else
begin
r := TRttiContext.Create;
ty := r.GetType(TClass(TT));
if ty = nil then
exit(Nil);
try
Result := TT(ty.GetMethod('Create').Invoke(ty.AsInstance.MetaclassType, []).AsObject);
except
if Assigned(ty) then
ty.Free;
raise;
end;
r.Free;
end;
end;
destructor TBaseJSON<T, Typ>.Destroy;
begin
inherited;
end;
function TBaseJSON<T, Typ>.GetBoolean(V: Typ): Boolean;
begin
Result := False;
if Member(V) then
Result := GetValue<TJSONAncestor>(V).ValueEx<Boolean>;
end;
function TBaseJSON<T, Typ>.GetData(Key: Typ): IJSONAncestor;
var
P: IJsonPair;
begin
if Self.InheritsFrom(TSuperObject) then
begin
P := TJSONObject(FInterface).Get(PString(@Key)^);
if Assigned(P) then
Result := P.JsonValue
else
Result := Nil
end
else
if Self.InheritsFrom(TSuperArray) then
Result := TJSONArray(FInterface).Get(PInteger(@Key)^);
end;
function TBaseJSON<T, Typ>.GetDataType: TDataType;
var
Cast: ICast;
begin
if TValue.From<T>(FJSONObj).AsInterface <> nil then
Cast := TCast.CreateFrom<T>(FJSONObj)
else
if Assigned(FCasted) then
Cast := TCast.Create(FCasted)
else
Exit(dtNil);
Result := Cast.DataType
end;
function TBaseJSON<T, Typ>.GetDate(V: Typ): TDate;
begin
Result := 0;
if Member(V) then
Result := GetValue<TJSONDate>(V).Value;
end;
function TBaseJSON<T, Typ>.GetDateTime(V: Typ): TDateTime;
begin
Result := 0;
if Member(V) then
Result := GetValue<TJSONDateTime>(V).Value;
end;
function TBaseJSON<T, Typ>.GetDouble(V: Typ): Double;
begin
Result := 0;
if Member(V) then
if GetType(V) = varInt64 then
Result := GetValue<TJSONInteger>(V).ValueEx<Int64>
else
Result := GetValue<TJSONFloat>(V).ValueEx<Double>;
end;
function TBaseJSON<T, Typ>.GetInteger(V: Typ): Int64;
begin
Result := 0;
if Member(V) then
Result := GetValue<TJSONInteger>(V).ValueEx<Int64>;
end;
function TBaseJSON<T, Typ>.GetNull(V: Typ): TMemberStatus;
var
Val: IJSONAncestor;
begin
if ContainsEx(V, Val) then begin
if Val is TJSONNull then
Result := jNull
else
Result := jAssigned
end else
Result := jUnAssigned;
end;