forked from PKGeorgiev/Delphi-JsonToDelphiClass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pkg.Json.Mapper.pas
1115 lines (955 loc) · 31.3 KB
/
Pkg.Json.Mapper.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
unit Pkg.Json.Mapper;
interface
uses
FMX.TreeView, System.JSON, Rest.Json, RTTI, RegularExpressions, TypInfo,
SysUtils, classes, Generics.Collections, Generics.Defaults;
type
TJsonLibrary = (jlDelphi, jlGrijjy, jlXSuper);
EJsonMapper = class(Exception);
TJsonType = (jtUnknown, jtObject, jtArray, jtString, jtTrue, jtFalse, jtInteger, jtInt64, jtExtended,
jtUnixTimestamp, jtDate, jtDateTime, jtBytes);
TStubClass = class;
TPkgJsonMapper = class;
TStubField = class
private
FName: string;
FPropertyName: string;
FFieldName: string;
FFieldType: TJsonType;
FParentClass: TStubClass;
procedure SetName(const Value: string);
function GetCorrectedName: string;
function GetAddNameAttrib: boolean;
public
constructor Create(AParentClass: TStubClass; AItemName: string; AFieldType: TJsonType);
destructor Destroy; override;
function GetTypeAsString: string; overload; virtual;
class function GetTypeAsString(AType: TJsonType): string; overload;
property Name: string read FName write SetName;
property CorrectedName: string read GetCorrectedName;
property FieldName: string read FFieldName write FFieldName;
property PropertyName: string read FPropertyName write FPropertyName;
property FieldType: TJsonType read FFieldType write FFieldType;
property AddNameAttrib: boolean read GetAddNameAttrib;
end;
TStubContainerField = class(TStubField)
private
FFieldClass: TStubClass;
FContainedType: TJsonType;
public
property ContainedType: TJsonType read FContainedType write FContainedType;
property FieldClass: TStubClass read FFieldClass write FFieldClass;
end;
TStubObjectField = class(TStubContainerField)
private
public
constructor Create(AParentClass: TStubClass; AItemName: string; AItemClass: TStubClass);
function GetTypeAsString: string; override;
end;
TStubArrayField = class(TStubContainerField)
private
public
constructor Create(AClass: TStubClass; AItemName: string; AItemSubType: TJsonType; AItemClass: TStubClass);
function GetTypeAsString: string; override;
end;
TStubClass = class
private
FItems,
FComplexItems,
FArrayItems: TList<TStubField>;
FName: string;
FComparison: TComparison<TStubField>;
FComparer: IComparer<TStubField>;
FParentClass: TStubClass;
FMapper: TPkgJsonMapper;
FPureClassName: string;
procedure SortFields;
procedure SetName(const Value: string);
procedure SetPureClassName(const Value: string);
function GetCorrectedName: string;
public
constructor Create(AParentClass: TStubClass; AClassName: string; AMapper: TPkgJsonMapper);
destructor Destroy; override;
function GetDeclarationPart(IncludeSerializers: Boolean; JsonLibrary: TJsonLibrary): string;
function GetImplementationPart(AJsonLibrary: TJsonLibrary; IncludeSerializers: Boolean = False): string;
function FindField(AFieldName: string): TStubField;
property Name: string read FName write SetName;
property CorrectedName: string read GetCorrectedName;
property Items: TList<TStubField> read FItems write FItems;
property PureClassName: string read FPureClassName write SetPureClassName;
end;
TPkgJsonMapper = class
private
FTreeView: TTreeView;
FClasses: TList<TStubClass>;
FRootClass: TStubClass;
FUnitName: string;
procedure SetUnitName(const Value: string);
protected
function GetJsonType(AJsonValue: TJsonValue; CurrentType: TJsonType = jtUnknown): TJsonType;
function GetJsonNumericType(AJsonValue: TJsonValue; CurrentType: TJsonType = jtUnknown): TJsonType;
function GetFirstArrayItem(AJsonValue: TJsonValue): TJsonValue;
procedure ProcessJsonObject(AJsonValue: TJsonValue; AParentClass: TStubClass);
procedure ClearClasses;
procedure InternalFormatTreeViewFields(AItem: TTreeViewItem);
procedure FormatFields(ATreeView: TTreeView);
procedure InternalVisualize(ATreeViewItem: TTreeViewItem; AClass: TStubClass; AItemStyleLookup: string);
function SuggestClassName(ASuggestedClassName: string): string;
public
constructor Create(ATreeView: TTreeView);
destructor Destroy; override;
// Parses a JSON string and creates internal stub class structure
procedure Parse(AJsonString: string; ARootClassName: string = 'Root');
// Generates resultant unit
function GenerateUnit(JsonLibrary: TJsonLibrary): string;
procedure Debug(ALines: TStrings);
// Visualizes stub class structure in a treeview
procedure Visualize(ATreeView: TTreeView; AItemStyleLookup: string);
property DestinationUnitName: string read FUnitName write SetUnitName;
end;
procedure PrettyPrintJSON(JSONValue: TJSONValue; OutputStrings: TStrings; indent: integer = 0);
var
PointDsFormatSettings: TFormatSettings;
implementation
uses
uUpdate;
var
ReservedWords: TList<string>;
const
INDENT_SIZE = 2;
// http://stackoverflow.com/a/12198174
procedure PrettyPrintPair(JSONValue: TJSONPair; OutputStrings: TStrings; last: boolean; indent: integer);
const TEMPLATE = '%s:%s';
var
line: string;
newList: TStringList;
begin
newList := TStringList.Create;
try
PrettyPrintJSON(JSONValue.JsonValue, newList, indent);
line := Format(TEMPLATE, [JSONValue.JsonString.ToString, Trim(newList.Text)]);
finally
newList.Free;
end;
line := StringOfChar(' ', indent + INDENT_SIZE) + line;
if not last then
line := line + ',';
OutputStrings.Add(line);
end;
procedure PrettyPrintArray(JSONValue: TJSONArray; OutputStrings: TStrings; last: boolean; indent: integer);
var i: integer;
begin
OutputStrings.Add(StringOfChar(' ', indent + INDENT_SIZE) + '[');
for i := 0 to JSONValue.Count - 1 do
begin
PrettyPrintJSON(JSONValue.Items[i], OutputStrings, indent);
if i < JSONValue.Count - 1 then
OutputStrings[OutputStrings.Count-1] := OutputStrings[OutputStrings.Count-1] + ',';
end;
OutputStrings.Add(StringOfChar(' ', indent + INDENT_SIZE - 2) + ']');
end;
procedure PrettyPrintJSON(JSONValue: TJSONValue; OutputStrings: TStrings; indent: integer = 0);
var
i: integer;
LIdent: integer;
begin
LIdent := indent + INDENT_SIZE;
i := 0;
if JSONValue is TJSONObject then
begin
OutputStrings.Add(StringOfChar(' ', LIdent) + '{');
for i := 0 to TJSONObject(JSONValue).Count - 1 do
PrettyPrintPair(TJSONObject(JSONValue).Pairs[i], OutputStrings, i = TJSONObject(JSONValue).Count - 1, LIdent);
OutputStrings.Add(StringOfChar(' ', LIdent) + '}');
end
else if JSONValue is TJSONArray then
PrettyPrintArray(TJSONArray(JSONValue), OutputStrings, i = TJSONObject(JSONValue).Count - 1, LIdent)
else OutputStrings.Add(StringOfChar(' ', LIdent) + JSONValue.ToJSON);
end;
{ TPkgJsonMapper }
procedure TPkgJsonMapper.ProcessJsonObject(AJsonValue: TJsonValue; AParentClass: TStubClass);
var
LJsonObj: TJSONObject;
LJsonPair: TJSONPair;
LJsonVal,
LJsonVal2: TJSONValue;
LJsonType,
LJsonType2: TJsonType;
LClass: TStubClass;
LField: TStubField;
LJsonItem: TJSONValue;
begin
LJsonObj := AJsonValue as TJSONObject;
for LJsonPair in LJsonObj do
begin
LJsonVal := LJsonPair.JsonValue;
LJsonType := GetJsonType(LJsonVal);
case LJsonType of
jtObject:
begin
LField := AParentClass.FindField(LJsonPair.JsonString.Value);
if not Assigned(LField) then begin
LClass := TStubClass.Create(AParentClass, LJsonPair.JsonString.Value, Self);
TStubObjectField.Create(AParentClass, LJsonPair.JsonString.Value, LClass);
end else begin
if LField is TStubContainerField then
LClass := TStubContainerField(LField).FieldClass;
end;
if Assigned(LClass) then
ProcessJsonObject(LJsonVal, LClass);
end;
jtArray:
begin
LClass := nil;
LJsonType2 := jtUnknown;
LJsonVal2 := GetFirstArrayItem(LJsonVal);
if LJsonVal2 <> nil then
begin
LJsonType2 := GetJsonType(LJsonVal2);
case LJsonType2 of
jtObject:
begin // needs to have a find here instead of just Create!
LField := AParentClass.FindField(LJsonPair.JsonString.Value);
if not Assigned(LField) then begin
LClass := TStubClass.Create(AParentClass, LJsonPair.JsonString.Value, Self);
end else begin
//LField is TStubObjectField, but we need the TStubClass
case LField.FieldType of
jtObject: LClass := (LField as TStubObjectField).FieldClass;
jtArray: LClass := (LField as TStubArrayField).FieldClass;
else raise EJsonMapper.Create('Elements must be of the same type!');
end;
end;
for LJsonItem in (LJsonVal as TJsonArray) do
ProcessJsonObject(LJsonItem, LClass);
end;
jtArray: raise EJsonMapper.Create('Nested Arrays are not supported!');
end;
end;
if LJsonType2 <> jtUnknown then begin
LField := AParentClass.FindField(LJsonPair.JsonString.Value);
if not Assigned(LField) then
TStubArrayField.Create(AParentClass, LJsonPair.JsonString.Value, LJsonType2, LClass);
end;
end;
jtInteger, jtInt64, jtUnixTimestamp, jtExtended,
jtString,
jtDate,
jtDateTime,
jtTrue,
jtFalse: begin
LField := AParentClass.FindField(LJsonPair.JsonString.Value);
if not Assigned(LField) then
TStubField.Create(AParentClass, LJsonPair.JsonString.Value, LJsonType);
end;
end;
end;
AParentClass.SortFields;
end;
function TPkgJsonMapper.GenerateUnit(JsonLibrary: TJsonLibrary): string;
var
LClass: TStubClass;
k: integer;
LList: TStringList;
libraryUses: string;
begin
LList := TStringList.Create;
try
case JsonLibrary of
jlDelphi: libraryUses := 'Rest.JSON';
jlGrijjy: libraryUses := 'Grijjy.Bson.Serialization';
jlXSuper: libraryUses := 'XSuperObject';
end;
LList.Add('unit ' + FUnitName + ';');
LList.Add('');
LList.Add('interface');
LList.Add('');
LList.Add('uses'+sLineBreak+
' Generics.Collections, '+ libraryUses +';');
LList.Add('');
LList.Add('type');
for k := FClasses.Count - 1 downto 0 do
begin
LClass := FClasses[k];
LList.Add(LClass.GetDeclarationPart(k = 0, JsonLibrary).TrimRight);
LList.Add('');
end;
LList.Add('implementation');
for k := FClasses.Count - 1 downto 0 do
begin
LClass := FClasses[k];
LList.Add(LClass.GetImplementationPart(JsonLibrary, k = 0).TrimRight);
end;
LList.Add('');
LList.Add('end.');
Result := LList.Text;
finally
LList.Free;
end;
end;
procedure TPkgJsonMapper.Visualize(ATreeView: TTreeView; AItemStyleLookup: string);
var
LItem: TTreeViewItem;
begin
ATreeView.Clear;
if FRootClass <> nil then
begin
ATreeView.BeginUpdate;
LItem := TTreeViewItem.Create(ATreeView);
LItem.Text := FRootClass.Name;
LItem.TagObject := FRootClass;
LItem.WordWrap := false;
ATreeView.AddObject(LItem);
InternalVisualize(LItem, FRootClass, AItemStyleLookup);
FormatFields(ATreeView);
ATreeView.EndUpdate;
ATreeView.ExpandAll;
end;
end;
function TPkgJsonMapper.GetFirstArrayItem(AJsonValue: TJsonValue): TJsonValue;
var
LJsonArray: TJsonArray;
LJsonValue: TJSONValue;
begin
Result := nil;
LJsonArray := AJsonValue as TJsonArray;
for LJsonValue in LJsonArray do
begin
Result := LJsonValue;
break;
end;
end;
procedure TPkgJsonMapper.ClearClasses;
var
LClass: TStubClass;
begin
for LClass in FClasses do
begin
LClass.Free;
end;
FClasses.Clear;
end;
constructor TPkgJsonMapper.Create(ATreeView: TTreeView);
begin
inherited Create;
FTreeView := ATreeView;
FClasses := TList<TStubClass>.Create;
end;
procedure TPkgJsonMapper.Debug(ALines: TStrings);
var
LClass: TStubClass;
LField: TStubField;
begin
ALines.Clear;
for LClass in FClasses do
begin
ALines.Add('-------');
ALines.Add(LClass.Name);
for LField in LClass.FItems do
begin
ALines.Add(Format('%-15s | %s', [LField.FieldName, LField.GetTypeAsString]));
end;
end;
end;
destructor TPkgJsonMapper.Destroy;
begin
ClearClasses;
FreeAndNil(FClasses);
inherited;
end;
procedure TPkgJsonMapper.FormatFields(ATreeView: TTreeView);
begin
if ATreeView.Count = 1 then
begin
InternalFormatTreeViewFields(ATreeView.Items[0]);
end;
end;
procedure TPkgJsonMapper.SetUnitName(const Value: string);
begin
FUnitName := Value;
end;
function TPkgJsonMapper.SuggestClassName(ASuggestedClassName: string): string;
var
LClass: TStubClass;
LMax, LVal: integer;
LString: string;
begin
Result := ASuggestedClassName;
LMax := 0;
for LClass in FClasses do
begin
if LClass.Name.StartsWith(ASuggestedClassName, true) then
begin
LString := Copy(LClass.Name, length(ASuggestedClassName) + 2);
if (LString.Length = 3) then
begin
if TryStrToInt(LString, LVal) then
begin
inc(LVal);
if LVal > LMax then
LMax := LVal;
end;
end
else
LMax := 1;
end;
end;
if LMax > 0 then
Result := Format('%s_%0.3d', [ASuggestedClassName, LMax]);
end;
function TPkgJsonMapper.GetJsonType(AJsonValue: TJsonValue; CurrentType: TJsonType = jtUnknown): TJsonType;
var
LJsonString: TJSONString;
begin
if AJsonValue is TJSONObject then
Result := jtObject
else if AJsonValue is TJSONArray then
Result := jtArray
else if (AJsonValue is TJSONNumber) then
Result := GetJsonNumericType(AJsonValue, CurrentType) // jtNumber
else if AJsonValue is TJSONTrue then
Result := jtTrue
else if AJsonValue is TJSONFalse then
Result := jtFalse
else if AJsonValue is TJSONString then begin
LJsonString := (AJsonValue as TJSONString);
if TRegEx.IsMatch(LJsonString.Value, '^([0-9]{4})-?(1[0-2]|0[1-9])-?(3[01]|0[1-9]|[12][0-9])(T| )' +
'(2[0-3]|[01][0-9]):?([0-5][0-9]):?([0-5][0-9])(Z|-[0-2][0-9]:[0-5][0-9])$') then
Result := jtDateTime
else if TRegEx.IsMatch(LJsonString.Value, '^([0-9]{4})(-?)(1[0-2]|0[1-9])\2(3[01]|0[1-9]|[12][0-9])$') then
Result := jtDate
else
Result := jtString
end else
Result := jtUnknown;
end;
function TPkgJsonMapper.GetJsonNumericType(AJsonValue: TJsonValue; CurrentType: TJsonType = jtUnknown): TJsonType;
function IsFloat(Source: string): Integer;
var
TReal: Double;
E: Integer;
begin
Source := Trim(Source);
Val(Source, TReal, E);
if E = 0 then
Result := 1
else
Result := 0;
if (Result = 1) and (Pos('.', Source) = 0) then
Result := 2; //definite maybe
if (Result = 2) and (TReal > 2147483640.0) then
Result := 0;
end;
function IsInteger(Source: string): Boolean;
var
TInt: Integer;
E: Integer;
begin
Source := Trim(Source);
Result := Pos('.', Source) = 0;
if Result then begin
Val(Source, TInt, E);
if TInt=1 then begin end;//Removes warning.
Result := (E = 0);
end;
end;
function IsInt64(Source: string): Boolean;
var
Value: Int64;
begin
Value := StrToInt64Def(Source, Low(Int64));
Result := (Value > Low(Int64)) and (Value > High(Integer));
end;
function IsUnixTimestamp(Source: string): Boolean;
// 473385600 473385600000 = January 1, 1985 12:00:00 AM
// 2556141803 2556141803000 = December 31, 2050 11:23:23 PM
var
Value: Int64;
begin
Value := StrToInt64Def(Source, Low(Int64));
Result := ((Value > 473385600000) and (Value < 2556141803000));
end;
var
intTemp: Integer;
fieldData: string;
begin
Result := CurrentType;
if Result = jtExtended then
Exit;
// detect type from simplest to most complex = jtInteger, jtInt64, jtUnixTimestamp, jtExtended
fieldData := AJsonValue.ToString;
if (Result in [jtUnknown, jtInteger]) and (Result <> jtExtended) then
try
if IsInt64(fieldData) then
Result := jtInt64
else
if IsInteger(fieldData) then
Result := jtInteger;
except
Result := jtUnknown;
end;
if Result in [jtUnknown, jtInteger, jtExtended] then
try
case IsFloat(fieldData) of
0: Result := jtUnknown; //definetly not a float
1: if Result in [jtUnknown, jtInteger] then
Result := jtExtended;
2: if Result = jtUnknown then
Result := jtInteger;
end;
except
if Result = jtExtended then
Result := jtUnknown;
end;
if (Result = jtInt64) and IsUnixTimestamp(fieldData) then
Result := jtUnixTimestamp;
if Result = jtUnknown then
Result := jtExtended;
// jtInteger, jtInt64, jtExtended, jtUnixTimestamp
end;
procedure TPkgJsonMapper.InternalFormatTreeViewFields(AItem: TTreeViewItem);
var
LItem: TTreeViewItem;
k: Integer;
LSize, LPos: integer;
begin
LSize := 0;
// Find max len
for k := 0 to AItem.Count - 1 do
begin
LItem := AItem.Items[k];
LPos := Pos(':', LItem.Text);
if (LPos > 0) AND (LPos > LSize) then
LSize := LPos;
end;
for k := 0 to AItem.Count - 1 do
begin
LItem := AItem.Items[k];
LPos := LSize - Pos(':', LItem.Text);
if (LPos > 0) then
LItem.Text := LItem.Text.Replace(':', StringOfChar(' ', LPos) + ':');
InternalFormatTreeViewFields(LItem);
end;
end;
procedure TPkgJsonMapper.InternalVisualize(ATreeViewItem: TTreeViewItem;
AClass: TStubClass; AItemStyleLookup: string);
var
LField: TStubField;
LItem: TTreeViewItem;
begin
for LField in AClass.FItems do
begin
LItem := TTreeViewItem.Create(ATreeViewItem);
LItem.StyleLookup := AItemStyleLookup;
LItem.TagObject := LField;
LItem.WordWrap := false;
case LField.FieldType of
jtObject:
begin
LItem.Text := LField.CorrectedName + ': {} ' + LField.GetTypeAsString;
InternalVisualize(LItem, (LField as TStubObjectField).FieldClass, AItemStyleLookup);
end;
jtArray:
begin
LItem.Text := LField.CorrectedName + ': [] ' + LField.GetTypeAsString;
if (LField as TStubArrayField).ContainedType = jtObject then
begin
InternalVisualize(LItem, (LField as TStubArrayField).FieldClass, AItemStyleLookup);
end;
end;
else
begin
LItem.Text := LField.CorrectedName + ': ' + LField.GetTypeAsString;
end;
end;
ATreeViewItem.AddObject(LItem);
end;
end;
procedure TPkgJsonMapper.Parse(AJsonString: string; ARootClassName: string);
var
LJsonValue,
LJsonValue2, LJsonItem: TJSONValue;
LJsonType: TJsonType;
LClass: TStubClass;
begin
ClearClasses;
LJsonValue := TJSONObject.ParseJSONValue(AJsonString);
if LJsonValue <> nil then
begin
try
FRootClass := TStubClass.Create(nil, ARootClassName, self);
case GetJsonType(LJsonValue) of
jtObject:
begin
ProcessJsonObject(LJsonValue, FRootClass);
end;
jtArray:
begin
LClass := nil;
LJsonType := jtUnknown;
LJsonValue2 := GetFirstArrayItem(LJsonValue);
if LJsonValue2 <> nil then
begin
LJsonType := GetJsonType(LJsonValue2);
LClass := TStubClass.Create(FRootClass, 'Item', Self);
end;
if LJsonType <> jtUnknown then
begin
TStubArrayField.Create(FRootClass, 'Items', LJsonType, LClass);
if LJsonType = jtObject then
begin
for LJsonItem in TJsonArray(LJsonValue) do
ProcessJsonObject(LJsonItem, LClass);
end else
ProcessJsonObject(LJsonValue2, LClass);
end;
end;
end;
finally
LJsonValue.Free;
end;
end
else
raise EJsonMapper.Create('Unable to parse the JSON String!');
FTreeView.ExpandAll;
end;
{ TVirtualClass }
constructor TStubClass.Create(AParentClass: TStubClass; AClassName: string; AMapper: TPkgJsonMapper);
begin
inherited Create;
FMapper := AMapper;
Name := AClassName;
FItems := TList<TStubField>.Create;
FComplexItems := TList<TStubField>.Create;
FArrayItems := TList<TStubField>.Create;
FMapper.FClasses.Add(self);
FParentClass := AParentClass;
FComparison :=
function(const Left, Right: TStubField): Integer
begin
if Left.FName > Right.FName then
Result := 1
else
if Left.FName < Right.FName then
Result := -1
else
Result := 0;
end;
FComparer := TComparer<TStubField>.Construct(FComparison);
end;
destructor TStubClass.Destroy;
var
LItem: TStubField;
begin
// ToArray is needed because stub field remove themselves from FItems
for LItem in FItems.ToArray do
begin
LItem.Free;
end;
FreeAndNil(FComplexItems);
FreeAndNil(FItems);
FreeAndNil(FArrayItems);
inherited;
end;
function TStubClass.FindField(AFieldName: string): TStubField;
var
i: Integer;
begin
Result := nil;
for i := 0 to Items.Count - 1 do
if SameText(Items[i].Name, AFieldName) then begin
Result := Items[i];
Break;
end;
end;
function TStubClass.GetImplementationPart(AJsonLibrary: TJsonLibrary; IncludeSerializers: Boolean = False): string;
var
LLines: TStringList;
LString: string;
LClassName: string;
LItem: TStubField;
toJsonString: string;
fromJsonString: string;
begin
LLines := TStringList.Create;
try
LClassName := Format('%s', [FName]);
if IncludeSerializers or (FComplexItems.Count + FArrayItems.Count > 0) then
begin
LLines.Add('');
LLines.Add(Format('{ %s }', [LClassName]));
LLines.Add('');
end;
if FComplexItems.Count > 0 then
begin
LLines.Add(Format('constructor %s.Create;', [LClassName]));
LLines.Add('begin');
LLines.Add(' inherited;');
for LItem in FComplexItems do
begin
LString := Format(' %s := %s.Create;', [LItem.FieldName, (LItem).GetTypeAsString]);
LLines.Add(LString);
end;
LLines.Add('end;');
LLines.Add('');
end;
if (FComplexItems.Count > 0) OR (FArrayItems.Count > 0) then
begin
LLines.Add(Format('destructor %s.Destroy;', [LClassName]));
if FArrayItems.Count > 0 then
begin
LLines.Add('var');
for LItem in FArrayItems do
begin
LString := Format(' %sItem: %s;', [LItem.FName, (LItem as TStubContainerField).FFieldClass.CorrectedName]);
LLines.Add(LString);
end;
end;
LLines.Add('begin');
if FArrayItems.Count > 0 then
begin
//LLines.Add('');
for LItem in FArrayItems do
begin
LLines.Add(Format(' for %sItem in %s do', [LItem.FName, LItem.FieldName]));
LLines.Add(Format(' %sItem.Free;', [LItem.FName]));
end;
LLines.Add('');
end;
for LItem in FComplexItems do
begin
LString := Format(' %s.Free;', [LItem.FieldName]);
LLines.Add(LString);
end;
LLines.Add(' inherited;');
LLines.Add('end;')
end;
if includeSerializers then
begin
case AJsonLibrary of
jlDelphi: begin
toJsonString := ' Result := TJson.ObjectToJsonString(self);';
fromJsonString := Format(' Result := TJson.JsonToObject<%s>(AJsonString)', [LClassName]);
end;
jlGrijjy: begin
toJsonString := ' TgoBsonSerializer.Serialize(Self, Result);';
fromJsonString := ' Result := nil;'#13#10 +
' TgoBsonSerializer.Deserialize(AJsonString, Result);';
end;
jlXSuper: begin
toJsonString := ' Result := Self.AsJSON;';
fromJsonString := ' Result := Self.FromJSON(AJsonString);';
end;
end;
LLines.Add('');
LLines.Add(Format('function %s.ToJsonString: string;', [LClassName]));
LLines.Add('begin');
LLines.Add(toJsonString);
LLines.Add('end;');
LLines.Add('');
LLines.Add(Format('class function %s.FromJsonString(const AJsonString: string): %s;', [LClassName, LClassName]));
LLines.Add('begin');
LLines.Add(fromJsonString);
LLines.Add('end;');
end;
Result := LLines.Text;
finally
LLines.Free;
end;
end;
procedure TStubClass.SetName(const Value: string);
var
LName: string;
begin
FPureClassName := Value;
if FPureClassName.Chars[0] = '@' then
Delete(FPureClassName, 1, 1); // handle @context item
FPureClassName := string(UpCase(FPureClassName.Chars[0])) + FPureClassName.Substring(1);
if FPureClassName.EndsWith('ies') then begin // remove plural element Summaries becomes Summary
SetLength(FPureClassName, FPureClassName.Length - 2);
FPureClassName[Length(FPureClassName)] := 'y';
end
else if FPureClassName.EndsWith('s') then // remove plural element
SetLength(FPureClassName, FPureClassName.Length - 1);
LName := 'T' + FPureClassName {+ 'Class'};
FName := FMapper.SuggestClassName(LName);
end;
procedure TStubClass.SetPureClassName(const Value: string);
begin
FPureClassName := Value;
end;
function TStubClass.GetCorrectedName: string;
begin
Result := Name;
if Result.Chars[0] = '@' then
Delete(Result, 1, 1); // handle @context item
end;
procedure TStubClass.SortFields;
begin
// FItems.Sort(FComparer);
end;
function TStubClass.GetDeclarationPart(IncludeSerializers: Boolean; JsonLibrary: TJsonLibrary): string;
var
LLines: TStringList;
LString: string;
LItem: TStubField;
begin
LLines := TStringList.Create;
try
LLines.Add(' ' + FName + ' = class');
LLines.Add(' private');
for LItem in FItems do
begin
LString := Format(' %s: %s;', [LItem.FieldName, LItem.GetTypeAsString]);
LLines.Add(' ' + LString);
end;
LLines.Add(' public');
if FComplexItems.Count > 0 then
begin
LLines.Add(' constructor Create;');
end;
if (FComplexItems.Count > 0) OR (FArrayItems.Count > 0) then
begin
LLines.Add(' destructor Destroy; override;');
end;
if includeSerializers then
begin
LLines.Add(' function ToJsonString: string;');
LLines.Add(Format(' class function FromJsonString(const AJsonString: string): %s;', [FName]));
end;
if IncludeSerializers or (FComplexItems.Count + FArrayItems.Count > 0) then
LLines.Add('');
for LItem in FItems do
begin
if (LItem.FieldType = jtUnknown) OR ((LItem is TStubContainerField) AND ((LItem as TStubContainerField).ContainedType = jtUnknown)) then
raise EJsonMapper.CreateFmt('The property [%s] has unknown type!', [LItem.PropertyName]);
if LItem.AddNameAttrib then begin
case JsonLibrary of
jlDelphi: LString := '[JsonName(''%s'')]';
jlGrijjy: LString := '[BsonElement(''%s'')]';
jlXSuper: LString := '[alias(''%s'')]';
end;
LLines.Add(' ' + Format(LString, [LItem.Name]));
end;
LString := Format(' property %s: %s read %s write %s;', [LItem.PropertyName, LItem.GetTypeAsString, LItem.FieldName, LItem.FieldName]);
LLines.Add(' ' + LString);
end;
LLines.Add(' end;');
Result := LLines.Text;
finally
LLines.Free;
end;
end;
{ TVirtualClassItemBase }
constructor TStubField.Create(AParentClass: TStubClass; AItemName: string; AFieldType: TJsonType);
begin
inherited Create;
if AItemName.Contains('-') then
raise EJsonMapper.CreateFmt('%s: Hyphens are not allowed!', [AItemName]);
FFieldType := AFieldType;
Name := AItemName;
FParentClass := AParentClass;
if FParentClass <> nil then
FParentClass.FItems.Add(self);
end;
destructor TStubField.Destroy;
begin
if FParentClass <> nil then
FParentClass.FItems.Remove(self);
inherited;
end;