-
Notifications
You must be signed in to change notification settings - Fork 123
/
Pkg.Json.Mapper.pas
908 lines (760 loc) · 24.5 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
unit Pkg.Json.Mapper;
interface
uses FMX.TreeView, System.JSON, Rest.Json, RTTI, RegularExpressions, TypInfo,
SysUtils, classes, Generics.Collections, Generics.Defaults;
type
EJsonMapper = class(Exception);
TJsonType = (jtUnknown, jtObject, jtArray, jtString, jtTrue,
jtFalse, jtNumber, 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);
public
constructor Create(AParentClass: TStubClass; AItemName: string; AFieldType: TJsonType);
destructor Destroy; override;
property Name: string read FName write SetName;
property FieldName: string read FFieldName write FFieldName;
property PropertyName: string read FPropertyName write FPropertyName;
property FieldType: TJsonType read FFieldType write FFieldType;
function GetTypeAsString: string; overload; virtual;
class function GetTypeAsString(AType: TJsonType): string; overload;
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;
FArrayProperty: string;
procedure SortFields;
procedure SetName(const Value: string);
procedure SetPureClassName(const Value: string);
public
constructor Create(AParentClass: TStubClass; AClassName: string; AMapper: TPkgJsonMapper; AArrayProperty: string = '');
destructor Destroy; override;
property Name: string read FName write SetName;
property Items: TList<TStubField> read FItems write FItems;
function GetDeclarationPart: string;
function GetImplementationPart: string;
property PureClassName: string read FPureClassName write SetPureClassName;
property ArrayProperty: string read FArrayProperty write FArrayProperty;
end;
TPkgJsonMapper = class
private
FTreeView: TTreeView;
FClasses: TList<TStubClass>;
FRootClass: TStubClass;
FUnitName: string;
procedure SetUnitName(const Value: string);
protected
function GetJsonType(AJsonValue: TJsonValue): 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: 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.ToString);
end;
{ TPkgJsonMapper }
procedure TPkgJsonMapper.ProcessJsonObject(AJsonValue: TJsonValue; AParentClass: TStubClass);
var
LJsonObj: TJSONObject;
LJsonPair: TJSONPair;
LJsonVal,
LJsonVal2: TJSONValue;
LJsonType,
LJsonType2: TJsonType;
LClass: TStubClass;
begin
LJsonObj := AJsonValue as TJSONObject;
for LJsonPair in LJsonObj do
begin
LJsonVal := LJsonPair.JsonValue;
LJsonType := GetJsonType(LJsonVal);
case LJsonType of
jtObject:
begin
LClass := TStubClass.Create(AParentClass, LJsonPair.JsonString.Value, self);
TStubObjectField.Create(AParentClass, LJsonPair.JsonString.Value, LClass);
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
LClass := TStubClass.Create(AParentClass, LJsonPair.JsonString.Value, self);
ProcessJsonObject(LJsonVal2, LClass);
end;
jtArray: raise EJsonMapper.Create('Nested Arrays are not supported!');
end;
end;
TStubArrayField.Create(AParentClass, LJsonPair.JsonString.Value, LJsonType2, LClass);
end;
jtNumber,
jtString,
jtDate,
jtDateTime,
jtTrue,
jtFalse: TStubField.Create(AParentClass, LJsonPair.JsonString.Value, LJsonType);
end;
end;
AParentClass.SortFields;
end;
function TPkgJsonMapper.GenerateUnit: string;
var
LClass: TStubClass;
k: integer;
LList: TStringList;
begin
LList := TStringList.Create;
try
LList.Add('unit ' + FUnitName + ';');
LList.Add('');
LList.Add('// *************************************************');
LList.Add('// Generated By: JsonToDelphiClass - ' + FloatToStr(ProgramVersion, PointDsFormatSettings));
LList.Add('// Project link: https://github.com/PKGeorgiev/Delphi-JsonToDelphiClass');
LList.Add('// Generated On: ' + FormatDateTime('yyyy-mm-dd hh:nn:ss', now));
LList.Add('// *************************************************');
LList.Add('// Created By : Petar Georgiev - 2014');
LList.Add('// WebSite : http://pgeorgiev.com');
LList.Add('// *************************************************');
LList.Add('');
LList.Add('interface');
LList.Add('');
LList.Add('uses Generics.Collections, Rest.Json;');
LList.Add('');
LList.Add('type');
for k := FClasses.Count - 1 downto 0 do
begin
LClass := FClasses[k];
LList.Add(LClass.GetDeclarationPart.TrimRight);
end;
LList.Add('');
LList.Add('implementation');
for k := FClasses.Count - 1 downto 0 do
begin
LClass := FClasses[k];
LList.Add(LClass.GetImplementationPart.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): 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 := 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])$') 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;
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.Name + ': {} ' + LField.GetTypeAsString;
InternalVisualize(LItem, (LField as TStubObjectField).FieldClass, AItemStyleLookup);
end;
jtArray:
begin
LItem.Text := LField.Name + ': [] ' + LField.GetTypeAsString;
if (LField as TStubArrayField).ContainedType = jtObject then
begin
InternalVisualize(LItem, (LField as TStubArrayField).FieldClass, AItemStyleLookup);
end;
end;
else
begin
LItem.Text := LField.Name + ': ' + LField.GetTypeAsString;
end;
end;
ATreeViewItem.AddObject(LItem);
end;
end;
procedure TPkgJsonMapper.Parse(AJsonString: string; ARootClassName: string);
var
LJsonValue,
LJsonValue2: 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
LJsonType := jtUnknown;
LClass := nil;
LJsonValue2 := GetFirstArrayItem(LJsonValue);
if LJsonValue2 <> nil then
begin
LJsonType := GetJsonType(LJsonValue2);
LClass := TStubClass.Create(FRootClass, 'Item', self);
end;
FRootClass.ArrayProperty := 'Items';
TStubArrayField.Create(FRootClass, 'Items', LJsonType, LClass);
ProcessJsonObject(LJsonValue2, LClass);
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; AArrayProperty: string);
begin
inherited Create;
FMapper := AMapper;
Name := AClassName;
FItems := TList<TStubField>.Create;
FComplexItems := TList<TStubField>.Create;
FArrayItems := TList<TStubField>.Create;
FMapper.FClasses.Add(self);
FArrayProperty := AArrayProperty;
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.GetImplementationPart: string;
var
LLines: TStringList;
LString: string;
LClassName: string;
LItem: TStubField;
begin
LLines := TStringList.Create;
try
LClassName := format('%s', [FName]);
LLines.Add('');
LLines.Add(format('{%s}', [LClassName]));
LLines.Add('');
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(' L%sItem: %s;', [LItem.FName, (LItem as TStubContainerField).FFieldClass.Name]);
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 L%sItem in %s do', [LItem.FName, LItem.FieldName]));
LLines.Add(format(' L%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;
LLines.Add('');
LLines.Add(format('function %s.ToJsonString: string;', [LClassName]));
LLines.Add('begin');
LLines.Add(' result := TJson.ObjectToJsonString(self);');
LLines.Add('end;');
LLines.Add('');
LLines.Add(format('class function %s.FromJsonString(AJsonString: string): %s;', [LClassName, LClassName]));
LLines.Add('begin');
LLines.Add(format(' result := TJson.JsonToObject<%s>(AJsonString)', [LClassName]));
LLines.Add('end;');
result := LLines.Text;
finally
LLines.Free;
end;
end;
procedure TStubClass.SetName(const Value: string);
var
LName: string;
begin
FPureClassName := String(Copy(Value, 1, 1)).ToUpper + Copy(Value, 2);
LName := 'T' + FPureClassName + 'Class';
FName := FMapper.SuggestClassName(LName);
end;
procedure TStubClass.SetPureClassName(const Value: string);
begin
FPureClassName := Value;
end;
procedure TStubClass.SortFields;
begin
FItems.Sort(FComparer);
end;
function TStubClass.GetDeclarationPart: string;
var
LLines: TStringList;
LString: string;
LItem: TStubField;
begin
LLines := TStringList.Create;
try
LLines.Add('');
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');
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]);
LString := format(' property %s: %s read %s write %s;', [LItem.PropertyName, LItem.GetTypeAsString, LItem.FieldName, LItem.FieldName]);
LLines.Add(LString);
end;
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;
LLines.Add(' function ToJsonString: string;');
LLines.Add(format(' class function FromJsonString(AJsonString: string): %s;', [FName]));
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]);
FParentClass := AParentClass;
FFieldType := AFieldType;
Name := AItemName;
if FParentClass <> nil then
FParentClass.FItems.Add(self);
end;
destructor TStubField.Destroy;
begin
if FParentClass <> nil then
FParentClass.FItems.Remove(self);
inherited;
end;
class function TStubField.GetTypeAsString(AType: TJsonType): string;
begin
case AType of
jtUnknown: result := 'Unknown';
jtString: result := 'String';
jtTrue,
jtFalse: result := 'Boolean';
jtNumber: result := 'Extended';
jtDate: result := 'TDate';
jtDateTime: result := 'TDateTime';
jtBytes: result := 'Byte';
end;
end;
procedure TStubField.SetName(const Value: string);
begin
if (FParentClass.FArrayProperty <> '') AND (FParentClass.FArrayProperty = FName) then
FParentClass.FArrayProperty := Value;
FName := Value;
FFieldName := 'F' + String(Copy(Value, 1, 1)).ToUpper + Copy(Value, 2);
if ReservedWords.Contains(Value.ToLower) then
FPropertyName := '&' + Value
else
FPropertyName := Value;
end;
function TStubField.GetTypeAsString: string;
begin
result := GetTypeAsString(FFieldType);
end;
{ TArrayItem }
constructor TStubArrayField.Create(AClass: TStubClass; AItemName: string; AItemSubType: TJsonType; AItemClass: TStubClass);
begin
inherited Create(AClass, AItemName, jtArray);
FContainedType := AItemSubType;
FFieldClass := AItemClass;
if FContainedType = TJsonType.jtObject then
AClass.FArrayItems.Add(self);
end;
function TStubArrayField.GetTypeAsString: string;
var
LSubType: string;
begin
case FContainedType of
jtObject: LSubType := FFieldClass.Name;
jtArray: raise EJsonMapper.Create('Nested arrays are not supported!');
else
LSubType := GetTypeAsString(FContainedType);
end;
result := format('TArray<%s>', [LSubType]);
end;
{ TStubObjectField }
constructor TStubObjectField.Create(AParentClass: TStubClass; AItemName: string; AItemClass: TStubClass);
begin
inherited Create(AParentClass, AItemName, jtObject);
FFieldClass := AItemClass;
AParentClass.FComplexItems.Add(self);
FContainedType := jtObject;
end;
function TStubObjectField.GetTypeAsString: string;
begin
result := FFieldClass.Name;
end;
initialization
PointDsFormatSettings := TFormatSettings.Create();
PointDsFormatSettings.DecimalSeparator := '.';
ReservedWords := TList<string>.Create;
ReservedWords.Add('type');
ReservedWords.Add('for');
ReservedWords.Add('var');
ReservedWords.Add('begin');
ReservedWords.Add('end');
ReservedWords.Add('function');
ReservedWords.Add('procedure');
ReservedWords.Add('class');
ReservedWords.Add('record');
ReservedWords.Add('string');
ReservedWords.Add('initialization');
ReservedWords.Add('finalization');
finalization
FreeAndNil(ReservedWords);
end.