forked from rzaripov1990/JSON2DelphiRecord
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFMX.JSON2Record.pas
More file actions
220 lines (189 loc) · 6.09 KB
/
Copy pathFMX.JSON2Record.pas
File metadata and controls
220 lines (189 loc) · 6.09 KB
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
unit FMX.JSON2Record;
{
author: ZuBy
email : rzaripov1990@gmail.com
http://blog.rzaripov.kz
https://github.com/rzaripov1990
}
interface
uses
System.Classes, System.SysUtils, System.Generics.Collections,
XSuperObject, XSuperJSON;
type
TmyTypeJSONObj = TDictionary<string, ISuperObject>;
TmyTypeRecordString = TDictionary<string, string>;
TmyJSON2Record = record
private
class var FFoundObjs: TStringList;
class var FArrays: TStringList;
class var FObjects: TmyTypeJSONObj;
class var FOutText: TmyTypeRecordString;
class var FErrorText: string;
class procedure EnumObj(const aValue: ISuperObject); static;
class procedure EnumFields(const aKey: string; const aValue: ISuperObject); static;
public
class function GetFoundObjectsList: string; static;
class function GetLastError: string; static;
class function Generate(const aJSON: string): string; static;
class function JSONView(const aJSON: string): string; static;
end;
implementation
const
TypeToField: array [TDataType] of string = ('nil', 'null', 'TObject', 'TArray', 'string', 'integer', 'float',
'boolean', 'string', 'string', 'string');
cSign = 'Record';
cIndent = ' ';
cRecordBegin = 'TmyTypeRecord = record';
cRecordSub = 'TmyType%s = record';
cRecordEnd = 'end;';
cRecordField = cIndent + '%s: %s;';
cFindObj = cIndent + '%s: TObject;';
cFindArr = cIndent + '%s: TArray;';
cReplaceObj = cIndent + '%s: TmyType%s;';
cReplaceArr = cIndent + '%s: TArray<TmyType%s>;';
{ TmyJSON2Record }
class procedure TmyJSON2Record.EnumFields(const aKey: string; const aValue: ISuperObject);
var
LEnum: TSuperEnumerator<IJSONPair>;
LKey, LKeyUp: string;
begin
// if (aValue.DataType = TDataType.dtNull) or (aValue.DataType = TDataType.dtNil) or (aValue = nil) then
// exit;
LKey := aKey;
LKeyUp := LKey;
LKeyUp[Low(string)] := AnsiUppercase(LKey)[low(string)];
TmyJSON2Record.FOutText.AddOrSetValue(LKey, Format(cRecordSub, [LKeyUp]));
LEnum := aValue.GetEnumerator;
while LEnum.MoveNext do
begin
// if (LEnum.GetCurrent.DataType = TDataType.dtNull) or (LEnum.GetCurrent.DataType = TDataType.dtNil) then
// Continue;
TmyJSON2Record.FOutText.AddOrSetValue(LKey, TmyJSON2Record.FOutText.Items[LKey] + sLineBreak + Format(cRecordField,
[LEnum.GetCurrent.Name, TypeToField[LEnum.GetCurrent.DataType]]));
end;
TmyJSON2Record.FOutText.AddOrSetValue(LKey, TmyJSON2Record.FOutText.Items[LKey] + sLineBreak + Format(cRecordEnd,
[LKey]) + sLineBreak + sLineBreak);
end;
class procedure TmyJSON2Record.EnumObj(const aValue: ISuperObject);
var
LEnum: TSuperEnumerator<IJSONPair>;
J: integer;
begin
LEnum := aValue.GetEnumerator;
while LEnum.MoveNext do
begin
if LEnum.GetCurrent.DataType = TDataType.dtObject then
begin
TmyJSON2Record.FObjects.AddOrSetValue(LEnum.GetCurrent.Name, LEnum.GetCurrent.AsObject);
TmyJSON2Record.EnumObj(LEnum.GetCurrent.AsObject);
end
else if LEnum.GetCurrent.DataType = TDataType.dtArray then
begin
TmyJSON2Record.FArrays.Add(LEnum.GetCurrent.Name);
with LEnum.GetCurrent.AsArray do
begin
for J := 0 to Length - 1 do
begin
if O[J].DataType = TDataType.dtObject then
begin
TmyJSON2Record.EnumFields(LEnum.GetCurrent.Name, O[J]);
TmyJSON2Record.EnumObj(O[J]);
end
else
TmyJSON2Record.EnumFields(LEnum.GetCurrent.Name, O[J]);
end;
end;
end;
end;
end;
class function TmyJSON2Record.Generate(const aJSON: string): string;
var
LObj: ISuperObject;
LKey, LKeyUp: string;
LValue: string;
I: integer;
begin
Result := '';
TmyJSON2Record.FArrays.Clear;
TmyJSON2Record.FOutText.Clear;
TmyJSON2Record.FObjects.Clear;
TmyJSON2Record.FFoundObjs.Clear;
TmyJSON2Record.FErrorText := 'OK';
if aJSON.IsEmpty then
begin
TmyJSON2Record.FErrorText := '(empty)';
exit;
end;
try
LObj := SO(aJSON);
except
TmyJSON2Record.FErrorText := 'INVALID JSON';
exit;
end;
try
TmyJSON2Record.EnumObj(LObj);
TmyJSON2Record.EnumFields(cSign, LObj);
for LKey in TmyJSON2Record.FObjects.Keys do
begin
if LKey.IsEmpty then
Continue;
TmyJSON2Record.FFoundObjs.Add(LKey);
EnumFields(LKey, TmyJSON2Record.FObjects.Items[LKey]);
TmyJSON2Record.FObjects.Remove(LKey);
end;
LValue := '';
for LKey in TmyJSON2Record.FOutText.Keys do
begin
if LKey <> cSign then
LValue := LValue + TmyJSON2Record.FOutText.Items[LKey];
end;
LValue := LValue + TmyJSON2Record.FOutText.Items[cSign];
for LKey in TmyJSON2Record.FOutText.Keys do
begin
LKeyUp := LKey;
LKeyUp[Low(string)] := AnsiUppercase(LKey)[low(string)];
LValue := StringReplace(LValue, Format(cFindObj, [LKey]), Format(cReplaceObj, [LKey, LKeyUp]), []);
end;
for I := 0 to TmyJSON2Record.FArrays.Count - 1 do
begin
LKey := TmyJSON2Record.FArrays.Strings[I];
LKeyUp := LKey;
LKeyUp[Low(string)] := AnsiUppercase(LKey)[low(string)];
LValue := StringReplace(LValue, Format(cFindArr, [LKey]), Format(cReplaceArr, [LKey, LKeyUp]), []);
end;
TmyJSON2Record.FArrays.Clear;
TmyJSON2Record.FOutText.Clear;
TmyJSON2Record.FObjects.Clear;
Result := Trim(LValue);
except
TmyJSON2Record.FErrorText := 'JSON contains unsupported data';
end;
end;
class function TmyJSON2Record.GetFoundObjectsList: string;
begin
Result := TmyJSON2Record.FFoundObjs.Text;
end;
class function TmyJSON2Record.GetLastError: string;
begin
Result := TmyJSON2Record.FErrorText;
end;
class function TmyJSON2Record.JSONView(const aJSON: string): string;
begin
try
Result := SO(aJSON).AsJSON(True);
except
Result := '';
end;
end;
initialization
TmyJSON2Record.FErrorText := 'OK';
TmyJSON2Record.FArrays := TStringList.Create;
TmyJSON2Record.FOutText := TmyTypeRecordString.Create;
TmyJSON2Record.FObjects := TmyTypeJSONObj.Create;
TmyJSON2Record.FFoundObjs := TStringList.Create;
finalization
TmyJSON2Record.FArrays.Free;
TmyJSON2Record.FOutText.Free;
TmyJSON2Record.FObjects.Free;
TmyJSON2Record.FFoundObjs.Free;
end.