-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJSONSerialize.pas
More file actions
233 lines (214 loc) · 5.78 KB
/
JSONSerialize.pas
File metadata and controls
233 lines (214 loc) · 5.78 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
221
222
223
224
225
226
227
228
229
230
231
232
233
unit JSONSerialize;
interface
type
JSONPath = class(TCustomAttribute)
private
FName: string;
public
constructor Create(const AName: string);
property Name: string read FName;
end;
function JSONString(obj: TObject): string; overload;
function JSONString(intf: IInterface): string; overload;
function JSONString(ARecordPtr, TypeInfoPtr: Pointer): string; overload;
implementation
uses
{$IF CompilerVersion > 26} //Delphi XE6 or later.
System.JSON,
{$ELSE}
Data.DBXJSON,
System.TypInfo,
{$ENDIF}
System.Classes,
System.Generics.Collections,
System.Rtti,
System.SysUtils;
type
ToJSON = class
public
class function FromValue(InValue: TValue): TJSONValue;
end;
{ JSONName }
constructor JSONPath.Create(const AName: string);
begin
FName := AName;
end;
function JSONSerializeObj(ptr, typinf: Pointer): TJSONObject; forward;
function JSONSerializeInterface(intf: IInterface): TJSONObject; forward;
function TryGetJSONValue(const Root: TJSONObject; const Path: string; var Value: TJSONValue): Boolean;
{$IF CompilerVersion < 27} //Delphi XE5 or earlier
var
Pair: TJSONPair;
{$ENDIF}
begin
{$IF CompilerVersion > 26} //Delphi XE6 or later.
Result := Root.TryGetValue(Path, Value);
{$ELSE}
Pair := Root.Get(Path);
Result := Assigned(Pair);
if Result then
Value := Pair.JsonValue
else
Value := nil;
{$ENDIF}
end;
function GetJSONString(const Value: TJSONValue): string;
begin
{$IF CompilerVersion > 26} //Delphi XE6 or later.
Result := Value.ToJSON;
{$ELSE}
Result := Value.ToString;
{$ENDIF}
end;
class function ToJSON.FromValue(InValue: TValue): TJSONValue;
var
I: Integer;
AValue: TValue;
AJSONValue: TJSONValue;
begin
Result := nil;
case InValue.Kind of
tkChar, tkString, tkWChar, tkLString, tkWString, tkUString:
if InValue.AsString <> '' then
Result := TJSONString.Create(InValue.AsString);
tkInteger:
if InValue.AsInteger <> 0 then
Result := TJSONNumber.Create(InValue.AsInteger);
tkInt64:
if InValue.AsInt64 <> 0 then
Result := TJSONNumber.Create(InValue.AsInt64);
tkFloat:
if not ((InValue.AsExtended = 0) or (InValue.AsExtended = 25569)) then
Result := TJSONNumber.Create(InValue.AsExtended);
tkEnumeration:
if InValue.AsBoolean then
Result := TJSONTrue.Create;
tkClass:
begin
if (InValue.AsObject is TJSONValue) then
Result := ((InValue.AsObject as TJSONValue).Clone as TJSONValue)
else
Result := JSONSerializeObj(InValue.AsObject, InValue.TypeInfo);
if GetJSONString(Result) = '{}' then
begin
FreeAndNil(Result);
end;
end;
tkRecord:
begin
Result := JSONSerializeObj(InValue.GetReferenceToRawData, InValue.TypeInfo);
if GetJSONString(Result) = '{}' then
FreeAndNil(Result);
end;
tkInterface:
begin
Result := JSONSerializeInterface(InValue.AsInterface);
if GetJSONString(Result) = '{}' then
FreeAndNil(Result);
end;
tkArray, tkDynArray:
begin
Result := TJSONArray.Create;
for I := 0 to InValue.GetArrayLength - 1 do
begin
AValue := InValue.GetArrayElement(I);
AJSONValue := ToJSON.FromValue(AValue);
if Assigned(AJSONValue) then
(Result as TJSONArray).AddElement(AJSONValue);
end;
if GetJSONString(Result) = '[]' then
FreeAndNil(Result);
end;
else
Result := nil;
end;
end;
procedure JSONSerializeValue(Result: TJSONObject; TypeKind: TTypeKind; InValue: TValue; JSONName: string);
var
OutValue, Temp: TJSONValue;
Root: TJSONObject;
Names: TStringList;
I: Integer;
begin
OutValue := ToJSON.FromValue(InValue);
if Assigned(OutValue) then
begin
Root := Result;
names := TStringList.Create;
try
ExtractStrings(['\','/'], [], PWideChar(JSONName), names);
for I := 0 to Names.Count - 2 do
begin
if not TryGetJSONValue(Root, Names[I], Temp) then
begin
Temp := TJSONObject.Create;
Root.AddPair(Names[I], Temp);
end;
Root := Temp as TJSONObject;
end;
Root.AddPair(Names[Names.Count - 1], OutValue);
finally
FreeAndNil(Names);
end;
end;
end;
function JSONSerializeObj(ptr, typinf: Pointer): TJSONObject;
var
ctx: TRttiContext;
t : TRttiType;
p : TRttiProperty;
f : TRttiField;
a : TCustomAttribute;
begin
Result := TJSONObject.Create;
t := ctx.GetType(typinf);
for f in t.GetFields do
for a in f.GetAttributes do
if a is JSONPath then
JSONSerializeValue(Result, f.FieldType.TypeKind, f.GetValue(ptr), JSONPath(a).Name);
for p in t.GetProperties do
for a in p.GetAttributes do
if a is JSONPath then
JSONSerializeValue(Result, p.PropertyType.TypeKind, p.GetValue(ptr), JSONPath(a).Name);
end;
function JSONSerializeInterface(intf: IInterface): TJSONObject;
var
obj: TObject;
begin
obj := TObject(intf);
Result := JSONSerializeObj(obj, obj.ClassInfo);
end;
function JSONString(obj: TObject): string;
var
json: TJSONObject;
begin
json := JSONSerializeObj(obj, obj.ClassInfo);
try
Result := GetJSONString(json);
finally
FreeAndNil(json);
end;
end;
function JSONString(intf: IInterface): string;
var
json: TJSONObject;
begin
json := JSONSerializeInterface(intf);
try
Result := GetJSONString(json);
finally
FreeAndNil(json);
end;
end;
function JSONString(ARecordPtr, TypeInfoPtr: Pointer): string;
var
json: TJSONObject;
begin
json := JSONSerializeObj(ARecordPtr, TypeInfoPtr);
try
Result := GetJSONString(json);
finally
FreeAndNil(json);
end;
end;
end.