-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUrsJSONCommonSerializers.pas
More file actions
240 lines (202 loc) · 6.02 KB
/
UrsJSONCommonSerializers.pas
File metadata and controls
240 lines (202 loc) · 6.02 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
234
235
236
237
238
239
240
unit UrsJSONCommonSerializers;
interface
uses
System.Rtti,
UrsJSONUtils;
type
TJsonBooleanDefaultAttribute = class(TJsonFieldAttribute)
protected
function IsDefault(const AValue: TValue): Boolean; override;
end;
TJsonDateTimeAttribute = class(TJsonFieldAttribute)
strict protected
function SerializeToString(const AValue: TValue): string; virtual;
function DeserializeString(const AStr: string): TValue;
protected
function Serialize(const AValue: TValue; out AJson: TJSONValue): Boolean; override;
function Deserialize(AContext, AJson: TJSONValue; out AValue: TValue): Boolean; override;
end;
TJsonISODateTimeAttribute = class(TJsonDateTimeAttribute)
strict private
const
CDTDelim = 'T';
strict protected
function SerializeToString(const AValue: TValue): string; override;
protected
function Deserialize(AContext, AJson: TJSONValue; out AValue: TValue): Boolean; override;
end;
TJsonRawAttribute = class(TJsonFieldAttribute)
protected
function Serialize(const AValue: TValue; out AJson: TJSONValue): Boolean; override;
end;
TJsonStringAttribute = class(TJsonFieldAttribute)
protected
function Deserialize(AContext, AJson: TJSONValue; out AValue: TValue): Boolean; override;
end;
TJsonStringDefAttribute = class(TJsonStringAttribute)
protected
function IsDefault(const AValue: TValue): Boolean; override;
function GetDefault(out AVal: TValue): Boolean; override;
end;
TJsonGUIDAttribute = class(TJsonFieldAttribute)
strict private
const
CNULL_GUID: TGUID = (
D1: 0;
D2: 0;
D3: 0;
D4: (0, 0, 0, 0, 0, 0, 0, 0);
);
protected
function Serialize(const AValue: TValue; out AJson: TJSONValue): Boolean; override;
function Deserialize(AContext, AJson: TJSONValue; out AValue: TValue): Boolean; override;
function IsDefault(const AValue: TValue): Boolean; override;
function GetDefault(out AVal: TValue): Boolean; override;
end;
implementation
uses
{$IF CompilerVersion > 26} // XE5
System.JSON,
{$ELSE}
Data.DBXJSON, UrsJSONPortable,
{$ENDIF}
System.SysUtils;
type
TDateTimeFormatter = class abstract
strict private
const
CDateFormat = 'yyyy-mm-dd';
CTimeFormat = 'hh:nn:ss';
class var
FFormatSettings: TFormatSettings;
private
const
CDTSepPos = Length(CDateFormat);
strict private
class constructor Create;
public
class property FormatSettings: TFormatSettings read FFormatSettings;
end;
{ TDateTimeFormatter }
class constructor TDateTimeFormatter.Create;
begin
FFormatSettings := TFormatSettings.Create;
FFormatSettings.DateSeparator := '-';
FFormatSettings.TimeSeparator := ':';
FFormatSettings.DecimalSeparator := '.';
FFormatSettings.ShortDateFormat := CDateFormat;
FFormatSettings.LongDateFormat := CDateFormat + ' ' + CTimeFormat;
FFormatSettings.ShortTimeFormat := CTimeFormat;
FFormatSettings.LongTimeFormat := CTimeFormat;
end;
{ TJsonBooleanDefaultAttribute }
function TJsonBooleanDefaultAttribute.IsDefault(const AValue: TValue): Boolean;
begin
Result := not AValue.AsBoolean;
end;
{ TJsonDateTimeAttribute }
function TJsonDateTimeAttribute.SerializeToString(const AValue: TValue): string;
begin
Result := DateTimeToStr(AValue.AsType<TDateTime>, TDateTimeFormatter.FormatSettings);
end;
function TJsonDateTimeAttribute.DeserializeString(
const AStr: string): TValue;
begin
Result := TValue.From<TDateTime>(
StrToDateTime(AStr, TDateTimeFormatter.FormatSettings));
end;
function TJsonDateTimeAttribute.Serialize(const AValue: TValue;
out AJson: TJSONValue): Boolean;
begin
AJson := TJSONString.Create(SerializeToString(AValue));
Result := True;
end;
function TJsonDateTimeAttribute.Deserialize(AContext, AJson: TJSONValue;
out AValue: TValue): Boolean;
begin
AValue := DeserializeString(AJson.Value);
Result := True;
end;
{ TJsonISODateTimeAttribute }
function TJsonISODateTimeAttribute.SerializeToString(
const AValue: TValue): string;
begin
Result := inherited SerializeToString(AValue);
Result[TDateTimeFormatter.CDTSepPos] := CDTDelim;
end;
function TJsonISODateTimeAttribute.Deserialize(AContext, AJson: TJSONValue;
out AValue: TValue): Boolean;
var
LStr: string;
LTPos: Integer;
LEndPos: Integer;
Li: Integer;
begin
LStr := AJson.Value;
LTPos := -1;
LEndPos := -1;
for Li := 1 to Length(LStr) do begin
case LStr[Li] of
CDTDelim: LTPos := Li;
' ', '+', '-': begin
if LTPos <> -1 then begin
LEndPos := Li - 1;
Break;
end;
end;
end;
end;
if LEndPos <> -1 then
SetLength(LStr, LEndPos);
if LTPos <> -1 then
LStr[LTPos] := ' ';
AValue := DeserializeString(LStr);
Result := True;
end;
{ TJsonRawAttribute }
function TJsonRawAttribute.Serialize(const AValue: TValue;
out AJson: TJSONValue): Boolean;
begin
AJson := TJSONRaw.Create(AValue.AsType<TArray<Byte>>);
Result := True;
end;
{ TJsonStringAttribute }
function TJsonStringAttribute.Deserialize(AContext, AJson: TJSONValue;
out AValue: TValue): Boolean;
begin
AValue := AJson.ToString;
Result := True;
end;
{ TJsonStringDefAttribute }
function TJsonStringDefAttribute.IsDefault(const AValue: TValue): Boolean;
begin
Result := AValue.AsString = '';
end;
function TJsonStringDefAttribute.GetDefault(out AVal: TValue): Boolean;
begin
AVal := TValue.From<string>('');
Result := True;
end;
{ TJsonGUIDAttribute }
function TJsonGUIDAttribute.Serialize(const AValue: TValue;
out AJson: TJSONValue): Boolean;
begin
AJson := TJSONString.Create(GUIDToString(AValue.AsType<TGUID>));
Result := True;
end;
function TJsonGUIDAttribute.Deserialize(AContext, AJson: TJSONValue;
out AValue: TValue): Boolean;
begin
AValue := TValue.From<TGUID>(StringToGUID(AJson.Value));
Result := True;
end;
function TJsonGUIDAttribute.IsDefault(const AValue: TValue): Boolean;
begin
Result := IsEqualGUID(AValue.AsType<TGUID>, CNULL_GUID);
end;
function TJsonGUIDAttribute.GetDefault(out AVal: TValue): Boolean;
begin
AVal := TValue.From<TGUID>(CNULL_GUID);
Result := True;
end;
end.