-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathJson.Interceptors.pas
333 lines (271 loc) · 9.36 KB
/
Json.Interceptors.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
unit Json.Interceptors;
interface
uses
REST.JsonReflect, System.Rtti, System.Types, System.UITypes;
{$M+}
type
TArrayHelper = record helper for TStringDynArray
public
function IndexOf(aString: string): Integer;
end;
EnumMemberAttribute = class(TCustomAttribute)
strict private
FNames: TStringDynArray;
public
constructor Create(const aNames: string; aSeparator: Char = ','); overload;
published
property Names: TStringDynArray read FNames;
end;
SetMembersAttribute = class(TCustomAttribute)
strict private
FName: string;
FSeparator: Char;
public
constructor Create(const aName: string; const aSeparator: Char = ','); overload;
function ToString: string; override;
property &Name: string read FName;
property Separator: Char read FSeparator;
end;
TBaseInterceptor<T> = class abstract(TJSONInterceptor)
strict private
FRttiContext: TRttiContext;
strict protected
function GetDataField(Data: TObject; Field: string): TRttiField; {$IFNDEF DEBUG} inline; {$ENDIF}
function GetValue(Data: TObject; Field: string): TValue; {$IFNDEF DEBUG} inline; {$ENDIF}
procedure SetValue(Data: TObject; Field: string; const AValue: TValue); {$IFNDEF DEBUG} inline; {$ENDIF}
function SplitString(const S, Delimiters: string): TStringDynArray;
function GetAttributes<U: TCustomAttribute>: TArray<U>; {$IFNDEF DEBUG} inline; {$ENDIF}
procedure ExpectTypeKind(ATypeKind: TTypeKind); {$IFNDEF DEBUG} inline; {$ENDIF}
end;
TColorInterceptor = class abstract(TBaseInterceptor<TColor>)
public
function StringConverter(Data: TObject; Field: string): string; override;
procedure StringReverter(Data: TObject; Field: string; Arg: string); override;
class function TryStrToColor(const AValue: string; out aColor: TColor): Boolean;
class function StrToColor(const AValue: string): TColor;
end;
TEnumInterceptor<T: record > = class abstract(TBaseInterceptor<T>)
strict private
function GetNames: TStringDynArray; {$IFNDEF DEBUG} inline; {$ENDIF}
public
function StringConverter(Data: TObject; Field: string): string; override;
procedure StringReverter(Data: TObject; Field: string; Arg: string); override;
end;
TSetInterceptor<T> = class abstract(TBaseInterceptor<T>)
strict private
function GetSetStrings: TStringDynArray;
public
function StringsConverter(Data: TObject; Field: string): TListOfStrings; override;
procedure StringsReverter(Data: TObject; Field: string; Args: TListOfStrings); override;
end;
TJSONInterceptorClass = class of TJSONInterceptor;
StringHandlerAttribute = class(JsonReflectAttribute)
public
constructor Create(aClass: TJSONInterceptorClass);
end;
StringsHandler = class(JsonReflectAttribute)
public
constructor Create(aClass: TJSONInterceptorClass);
end;
implementation
uses
System.TypInfo, System.SysUtils, System.StrUtils,
System.Generics.Collections, System.UIConsts;
function TArrayHelper.IndexOf(aString: string): Integer;
begin
for Result := low(Self) to high(Self) do
if Self[Result] = aString then
Exit;
Result := -1;
end;
{ TBaseEnumInterceptor<T> }
procedure TBaseInterceptor<T>.ExpectTypeKind(ATypeKind: TTypeKind);
begin
Assert(TypeInfo(T) <> nil, 'Type has no typeinfo!');
Assert(PTypeInfo(TypeInfo(T)).Kind = ATypeKind, 'Type is not expected type!');
end;
function TBaseInterceptor<T>.GetAttributes<U>: TArray<U>;
var
Attribute: TCustomAttribute;
Attributes: TArray<TCustomAttribute>;
ResultArray: TArray<U>;
begin
ResultArray := nil;
for Attribute in FRttiContext.GetType(ClassType).GetAttributes do
if Attribute is U then
begin
SetLength(ResultArray, Length(ResultArray) + 1);
ResultArray[Length(ResultArray) - 1] := Attribute as U;
end;
Result := ResultArray;
end;
function TBaseInterceptor<T>.GetDataField(Data: TObject; Field: string): TRttiField;
begin
Result := FRttiContext.GetType(Data.ClassType).GetField(Field);
end;
function TBaseInterceptor<T>.GetValue(Data: TObject; Field: string): TValue;
begin
Result := GetDataField(Data, Field).GetValue(Data);
end;
procedure TBaseInterceptor<T>.SetValue(Data: TObject; Field: string; const AValue: TValue);
begin
GetDataField(Data, Field).SetValue(Data, AValue);
end;
function TBaseInterceptor<T>.SplitString(const S, Delimiters: string): TStringDynArray;
var
Buffer: TStringDynArray;
I: Integer;
begin
Buffer := System.StrUtils.SplitString(S, Delimiters);
SetLength(Result, Length(Buffer));
for I := low(Result) to high(Result) do
Result[I] := Buffer[I].Trim;
end;
{ EnumMemberAttribute }
constructor EnumMemberAttribute.Create(const aNames: string; aSeparator: Char);
begin
inherited Create;
FNames := SplitString(aNames, aSeparator);
end;
{ TSmartInterceptor<T> }
function TEnumInterceptor<T>.GetNames: TStringDynArray;
var
Attribute: EnumMemberAttribute;
Name: string;
ResultArray: TStringDynArray;
begin
for Attribute in GetAttributes<EnumMemberAttribute> do
for name in Attribute.Names do
begin
SetLength(ResultArray, Length(ResultArray) + 1);
ResultArray[Length(ResultArray) - 1] := name.Trim;
end;
Result := ResultArray;
end;
function TEnumInterceptor<T>.StringConverter(Data: TObject; Field: string): string;
begin
ExpectTypeKind(tkEnumeration);
Result := GetNames[GetValue(Data, Field).AsOrdinal];
end;
procedure TEnumInterceptor<T>.StringReverter(Data: TObject; Field, Arg: string);
var
Enum: T;
AbsValue: Byte absolute Enum;
begin
ExpectTypeKind(tkEnumeration);
AbsValue := GetNames.IndexOf(Arg);
SetValue(Data, Field, TValue.From<T>(Enum));
end;
{ EnumHandlerAttribute }
constructor StringHandlerAttribute.Create(aClass: TJSONInterceptorClass);
begin
inherited Create(ctString, rtString, aClass, nil, True);
end;
{ TSmartSetInterceptor<T> }
function TSetInterceptor<T>.GetSetStrings: TStringDynArray;
var
AttributeArray: TArray<SetMembersAttribute>;
Attr: TCustomAttribute;
SetAttr: SetMembersAttribute absolute Attr;
begin
Result := nil;
AttributeArray := GetAttributes<SetMembersAttribute>;
if AttributeArray = nil then
Exit(nil);
Attr := AttributeArray[0];
Result := SplitString(SetAttr.Name, SetAttr.Separator);
end;
function TSetInterceptor<T>.StringsConverter(Data: TObject; Field: string): TListOfStrings;
var
SetStrings: TStringDynArray;
RttiField: TRttiField;
EnumType: TRttiEnumerationType;
SetValues: TIntegerSet;
EnumSet: T absolute SetValues;
SetType: TRttiSetType;
I: Integer;
begin
Result := nil;
ExpectTypeKind(tkSet);
SetStrings := GetSetStrings;
Assert(Length(SetStrings) > 0, Format('EnumSet attribute needs string parameters on %s!', [ClassName]));
RttiField := GetDataField(Data, Field);
if not(RttiField.FieldType is TRttiSetType) then
Exit;
SetType := TRttiSetType(RttiField.FieldType);
if not(SetType.ElementType is TRttiEnumerationType) then
Exit;
EnumSet := RttiField.GetValue(Data).AsType<T>;
EnumType := TRttiEnumerationType(SetType.ElementType);
(*
Common error scenarios
* Your Enum contains 3 elements, but you only provided 2 names
* You separated the strings with a pipe (|), but the separator is comma (,)
*)
Assert(EnumType.MaxValue <= high(SetStrings), 'Number of elements in set does not match, or separator mismatch!');
for I := EnumType.MinValue to EnumType.MaxValue do
if I in SetValues then
Result := Result + [SetStrings[I]];
end;
procedure TSetInterceptor<T>.StringsReverter(Data: TObject; Field: string; Args: TListOfStrings);
var
I: Integer;
RttiField: TRttiField;
SetValues: TIntegerSet;
EnumSet: T absolute SetValues;
Names: TStringDynArray;
Argument: string;
begin
ExpectTypeKind(tkSet);
Names := GetSetStrings;
SetValues := [];
for I := low(Names) to high(Names) do
for Argument in TArray<string>(Args) do
if Names[I] = Argument then
Include(SetValues, I);
SetValue(Data, Field, TValue.From<T>(EnumSet));
end;
{ SetMemberAttribute }
constructor SetMembersAttribute.Create(const aName: string; const aSeparator: Char);
begin
FName := aName;
FSeparator := aSeparator;
end;
function SetMembersAttribute.ToString: string;
begin
Result := FName;
end;
{ StringsHandler }
constructor StringsHandler.Create(aClass: TJSONInterceptorClass);
begin
inherited Create(ctStrings, rtStrings, aClass, nil, True)
end;
{ TColorInterceptor<T> }
function TColorInterceptor.StringConverter(Data: TObject; Field: string): string;
var
Color: TColor;
begin
Color := GetValue(Data, Field).AsInteger;
Result := '0x' + IntToHex(Color);
end;
procedure TColorInterceptor.StringReverter(Data: TObject; Field, Arg: string);
begin
SetValue(Data, Field, StrToColor(Arg));
end;
class function TColorInterceptor.StrToColor(const AValue: string): TColor;
begin
if not TryStrToColor(AValue, Result) then
Result := 0;
end;
class function TColorInterceptor.TryStrToColor(const AValue: string; out aColor: TColor): Boolean;
var
Int: Integer;
begin
Result := False;
if TryStrToInt(AValue, Int) or IdentToColor(AValue, Int) or IdentToColor('cl' + AValue, Int) then
begin
aColor := Int;
Result := True;
end;
end;
end.