-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
CloudAPI.Json.Converters.pas
50 lines (41 loc) · 1.46 KB
/
CloudAPI.Json.Converters.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
unit CloudAPI.Json.Converters;
interface
uses
System.Json.Readers,
System.Json.Serializers,
System.Json.Writers,
System.Rtti,
System.TypInfo;
type
// --------------------------------------------------------------------- //
// Converter for UnixTime
// --------------------------------------------------------------------- //
TJsonUnixTimeConverter = class(TJsonConverter)
public
procedure WriteJson(const AWriter: TJsonWriter; const AValue: TValue; const ASerializer: TJsonSerializer); override;
function ReadJson(const AReader: TJsonReader; ATypeInf: PTypeInfo; const AExistingValue: TValue;
const ASerializer: TJsonSerializer): TValue; override;
function CanConvert(ATypeInf: PTypeInfo): Boolean; override;
end;
implementation
uses
System.DateUtils;
{ TJsonUnixTimeConverter }
function TJsonUnixTimeConverter.CanConvert(ATypeInf: PTypeInfo): Boolean;
begin
Result := ATypeInf^.Kind = tkInteger;
end;
function TJsonUnixTimeConverter.ReadJson(const AReader: TJsonReader; ATypeInf: PTypeInfo; const AExistingValue: TValue;
const ASerializer: TJsonSerializer): TValue;
var
LEnumValue: TDateTime;
begin
LEnumValue := UnixToDateTime(AReader.Value.AsInteger());
TValue.Make(@LEnumValue, ATypeInf, Result);
end;
procedure TJsonUnixTimeConverter.WriteJson(const AWriter: TJsonWriter; const AValue: TValue;
const ASerializer: TJsonSerializer);
begin
AWriter.WriteValue(DateTimeToUnix(AValue.AsType<TDateTime>(), True));
end;
end.