-
Notifications
You must be signed in to change notification settings - Fork 27
/
Pkg.Json.Mapper.pas
325 lines (276 loc) · 8.42 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
unit Pkg.Json.Mapper;
interface
uses
System.Json, Rest.Json, System.SysUtils, System.Classes, System.Generics.Collections, System.Generics.Defaults,
Pkg.Json.JSONName, Pkg.Json.StubField, Pkg.Json.JsonValueHelper;
{$M+}
type
TPkgJsonMapper = class
strict private
FStubClasses: TStubClassList;
FRootClass: TStubClass;
FUnitName: string;
FClassName: string;
strict private
FJsonString: string;
protected
function GetJsonType(aJsonValue: TJsonValue): TJsonType;
function GetFirstArrayItem(aJSONArray: TJSONArray): TJsonValue;
procedure ProcessJsonObject(aJsonValue: TJsonValue; aParentClass: TStubClass);
public
constructor Create;
destructor Destroy; override;
// Parses a JSON string and creates internal stub class structure
function Parse(aJsonString: string): TPkgJsonMapper;
function IsValid(aJsonString: string): boolean;
function LoadFormFile(aJsonFile: string): TPkgJsonMapper;
// Generates result unit
function GenerateUnit: string;
function SuggestClassName(aSuggestedClassName: string): string;
procedure Debug(aLines: TStrings);
procedure SaveToFile(aFileName: string);
published
property DestinationClassName: string read FClassName write FClassName;
property DestinationUnitName: string read FUnitName write FUnitName;
property RootClass: TStubClass read FRootClass;
property StubClasses: TStubClassList read FStubClasses;
property JsonString: string read FJsonString;
end;
implementation
uses
System.StrUtils, System.Character, System.IOUtils,
Pkg.Json.ReservedWords, Pkg.Json.SubTypes;
const
INDENT_SIZE = 2;
{ TPkgJsonMapper }
procedure TPkgJsonMapper.ProcessJsonObject(aJsonValue: TJsonValue; aParentClass: TStubClass);
var
JSONObject: TJSONObject;
JsonPair: TJSONPair;
JSONValue: TJsonValue;
JsonType: TJsonType;
StubClass: TStubClass;
JsonArray: TJSONArray;
begin
if aJsonValue = nil then
exit;
JSONObject := aJsonValue as TJSONObject;
for JsonPair in JSONObject do
begin
JSONValue := JsonPair.JSONValue;
JsonType := GetJsonType(JSONValue);
case JsonType of
jtObject:
begin
StubClass := TStubClass.Construct(aParentClass, JsonPair.JsonString.Value, Self.FStubClasses);
TStubObjectField.Create(aParentClass, JsonPair.JsonString.Value, StubClass);
ProcessJsonObject(JSONValue, StubClass);
end;
jtArray:
begin
JsonArray := TJSONArray(JSONValue);
JSONValue := GetFirstArrayItem(JsonArray);
JsonType := GetJsonType(JSONValue);
StubClass := TStubClass.Construct(aParentClass, JsonPair.JsonString.Value, Self.FStubClasses, '', JsonType = jtObject);
TStubArrayField.Create(aParentClass, JsonPair.JsonString.Value, JsonType, StubClass);
for JSONValue in JsonArray do
ProcessJsonObject(JSONValue, StubClass);
end;
else
TStubField.Create(aParentClass, JsonPair.JsonString.Value, JsonType);
end;
end;
aParentClass.SortFields;
end;
function TPkgJsonMapper.GenerateUnit: string;
var
StringList: TStringList;
StubClass: TStubClass;
Tmp: string;
i: Integer;
SubClasslist: TStringList;
begin
TStubClass.ClearNames;
StringList := TStringList.Create;
SubClasslist := TStringList.Create;;
SubClasslist.Duplicates := TDuplicates.dupIgnore;
SubClasslist.Sorted := True;
try
StringList.TrailingLineBreak := False;
StringList.Add('unit ' + FUnitName + ';');
StringList.Add('');
StringList.Add('interface');
StringList.Add('');
StringList.Add('uses');
StringList.Add(' Pkg.Json.DTO, System.Generics.Collections, REST.Json.Types;');
StringList.Add('');
StringList.Add('{$M+}');
StringList.Add('');
StringList.Add('type');
for i := FStubClasses.Count - 1 downto 1 do
SubTypes.AddSubTypes(FStubClasses[i], SubClasslist);
if SubClasslist.Count > 1 then
begin
for Tmp in SubClasslist do
StringList.Addformat(' %s = class;', [Tmp]);
StringList.Add('');
end;
SubClasslist.Free;
for i := FStubClasses.Count - 1 downto 1 do
begin
StubClass := FStubClasses[i];
Tmp := IfThen(StubClass.ArrayItems.Count > 0, 'TJsonDTO');
StringList.AddIfNotEmpty(StubClass.GetDeclarationPart(Tmp));
end;
StringList.Add(FStubClasses.First.GetDeclarationPart('TJsonDTO'));
StringList.Add('implementation');
for i := FStubClasses.Count - 1 downto 0 do
StringList.AddIfNotEmpty(FStubClasses[i].GetImplementationPart);
StringList.Add('');
StringList.Add('end.');
Result := StringList.Text;
finally
StringList.Free;
end;
end;
function TPkgJsonMapper.GetFirstArrayItem(aJSONArray: TJSONArray): TJsonValue;
begin
if (aJSONArray = nil) or (aJSONArray.Count = 0) then
exit(nil);
Result := aJSONArray.Items[0];
end;
constructor TPkgJsonMapper.Create;
begin
inherited Create;
FStubClasses := TStubClassList.Create;
FClassName := 'Root';
end;
procedure TPkgJsonMapper.Debug(aLines: TStrings);
var
StubClass: TStubClass;
StubField: TStubField;
begin
aLines.Clear;
for StubClass in FStubClasses do
begin
aLines.Add('-------');
aLines.Add(StubClass.Name);
for StubField in StubClass.Items do
aLines.Addformat('%-15s | %s', [StubField.FieldName, StubField.TypeAsString]);
end;
end;
destructor TPkgJsonMapper.Destroy;
begin
FreeAndNil(FStubClasses);
inherited;
end;
procedure TPkgJsonMapper.SaveToFile(aFileName: string);
var
ResourceStream: TResourceStream;
Buffer: TStringList;
begin
with TStringList.Create do
try
Text := GenerateUnit;
SaveToFile(aFileName);
finally
Free;
end;
Buffer := TStringList.Create;
ResourceStream := TResourceStream.Create(HInstance, 'JsonDTO', 'PAS');
try
ResourceStream.Position := 0;
Buffer.LoadFromStream(ResourceStream);
Buffer.SaveToFile(TPath.GetDirectoryName(aFileName) + TPath.DirectorySeparatorChar + 'Pkg.Json.DTO.pas');
finally
ResourceStream.Free;
Buffer.Free;
end;
end;
function TPkgJsonMapper.SuggestClassName(aSuggestedClassName: string): string;
var
StubClass: TStubClass;
MaxValue, i: Integer;
s: string;
begin
Result := aSuggestedClassName;
MaxValue := 0;
for StubClass in FStubClasses do
if StubClass.Name.StartsWith(aSuggestedClassName, True) then
begin
s := Copy(StubClass.Name, Length(aSuggestedClassName) + 2);
if (s.Length = 3) then
begin
if TryStrToInt(s, i) then
begin
inc(i);
if i > MaxValue then
MaxValue := i;
end;
end
else
MaxValue := 1;
end;
if MaxValue > 0 then
Result := Format('%s_%0.3d', [aSuggestedClassName, MaxValue]);
end;
function TPkgJsonMapper.GetJsonType(aJsonValue: TJsonValue): TJsonType;
begin
exit(TJsonValueHelper.GetJsonType(aJsonValue))
end;
function TPkgJsonMapper.IsValid(aJsonString: string): boolean;
var
Value: TJsonValue;
begin
Value := TJSONObject.ParseJSONValue(aJsonString);
Result := Value <> nil;
if Result then
Value.Free;
end;
function TPkgJsonMapper.LoadFormFile(aJsonFile: string): TPkgJsonMapper;
var
StringList: TStringList;
begin
StringList := TStringList.Create;
StringList.LoadFromFile(aJsonFile);
Result := Parse(StringList.Text);
StringList.Free;
end;
function TPkgJsonMapper.Parse(aJsonString: string): TPkgJsonMapper;
var
JSONValue: TJsonValue;
JsonType: TJsonType;
StubClass: TStubClass;
JsonArray: TJSONArray;
begin
Result := Self;
FStubClasses.Clear;
FJsonString := aJsonString;
TStubClass.ClearNames;
JSONValue := TJSONObject.ParseJSONValue(aJsonString);
if JSONValue <> nil then
begin
try
FRootClass := TStubClass.Construct(nil, FClassName, Self.FStubClasses);
case GetJsonType(JSONValue) of
jtObject:
ProcessJsonObject(JSONValue, FRootClass);
jtArray:
begin
JsonArray := TJSONArray(JSONValue);
FRootClass.ArrayProperty := 'Items';
StubClass := TStubClass.Construct(FRootClass, FRootClass.ArrayProperty, Self.FStubClasses);
JsonType := GetJsonType(GetFirstArrayItem(JsonArray));
TStubArrayField.Create(FRootClass, FRootClass.ArrayProperty, JsonType, StubClass);
for JSONValue in JsonArray do
ProcessJsonObject(JSONValue, StubClass);
end;
end;
finally
JSONValue.Free;
end;
end
else
raise EJsonMapper.Create('Unable to parse the JSON String!');
end;
end.