forked from pult/SuperObject.Delphi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuMarshalSuper.pas
194 lines (172 loc) · 4.68 KB
/
uMarshalSuper.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
{ uMarshalSuper.pas } // version: 2021.0116.1923
unit uMarshalSuper;
{$IFDEF FPC}
{$mode delphi}
{$ENDIF}
interface
uses
{%H-}Types, SysUtils, Classes, {%H-}Rtti,
supertypes, superobject;
type
EJsonSerializer = class(ESuperObject);
JsonSerializer = record
public
class function Marshal<T>(const Data: T): string; overload; static;
class function Unmarshal<T>(Text: string; out Data:T): Boolean; overload; static;
end;
{ TSerialContext }
TSerialContext = class(TSuperRttiContext)
public
class procedure InitializeGlobal;
constructor Create; override;
end;
implementation
uses
{$IFDEF MSWINDOWS}
Windows,
{$ENDIF}
TypInfo, DateUtils, superdate;
const
NullDateTime = -328716;
function SerialToDateTime({%H-}ctx: TSuperRttiContext;
var Value: TValue; const {%H-}Index: ISuperObject): ISuperObject;
var
LDateTime: TDateTime;
sDateTime: string;
begin
Result := nil;
if Value.IsEmpty then
Exit;
LDateTime := Value.AsExtended;
sDateTime := FloatToStr(LDateTime);
if (sDateTime <> FloatToStr(0))
and (sDateTime <> FloatToStr(NullDateTime)) then
begin
Result := TSuperObject.Create( DelphiDateToISO8601(LDateTime) );
end;
end;
function SerialFromDateTime({%H-}ctx: TSuperRttiContext;
const obj: ISuperObject; var Value: TValue): Boolean;
var
S: string;
D: TDateTime;
begin
if Assigned(obj) then
S := Trim(string(obj.AsString));
Result := S = '';
if Result then begin
Value := TValue.From<TDateTime>(NullDateTime);
end else begin
Result := TryISO8601ToDelphiDate(SOString(S), D);
if Result then
Value := TValue.From<TDateTime>(D);
end;
end;
{ JsonSerializer }
class function JsonSerializer.Marshal<T>(const Data: T): string;
var
ctxowned : Boolean;
ctx : TSuperRttiContext; //TSerialContext;
obj : ISuperObject;
sError : string;
begin
Result := ''; sError := '';
if @Data <> nil then begin
ctxowned := False;
ctx := SuperRttiContextDefault;
if ctx = nil then begin
ctx := TSerialContext.Create;
ctxowned := True;
end;
try
obj := ctx.AsJson<T>(Data);
if Assigned(obj) then begin
Result := string(obj.AsJSon( {indent:}True, {escape:}False ));
//Result := string( ISuperObject( ctx.AsJson<T>(Data) ).AsJSon);
end else begin
sError := 'Failed json serialization (exists unhandled types)';
{$IFDEF MSWINDOWS}
OutputDebugString(PChar('ERROR: '+sError));
{$ENDIF}
//sError := '';
end;
except
on e: Exception do
begin
sError := e.Message;
{$IFDEF MSWINDOWS}
OutputDebugString(PChar('EXCEPTION: '+e.ClassName+': '+sError));
{$ENDIF}
sError := #13#10+sError;
//Result := ''; { optional }
end;
end;
if ctxowned then
ctx.Free;
if (Result = '') then begin
if (sError <> '') then
raise EJsonSerializer.Create('JsonSerializer.Marshal: '+sError)
else
raise EJsonSerializer.Create('JsonSerializer.Marshal: Empty');
end;
end;
end;
class function JsonSerializer.Unmarshal<T>(Text: string; out Data: T): Boolean;
var
ctxowned : Boolean;
ctx : TSuperRttiContext; //TSerialContext;
sError : string;
begin
ctxowned := False;
ctx := SuperRttiContextDefault;
if ctx = nil then begin
ctx := TSerialContext.Create;
ctxowned := True;
end;
try
Data := ctx.AsType<T>(SO(SOString(Text)));
Result := True;
except
on e: Exception do
begin
sError := e.Message;
{$IFDEF MSWINDOWS}
OutputDebugString(PChar('EXCEPTION: '+e.ClassName+': '+sError));
{$ENDIF}
sError := #13#10+sError;
Result := False;
Data := Default(T);
end;
end;
if ctxowned then
ctx.Free;
if (not Result) then
raise EJsonSerializer.Create('JsonSerializer.Unmarshal:'+#13#10+Text{+sError});
end;
{ TSerialContext }
var
sc_Initialized: Boolean;
class procedure TSerialContext.InitializeGlobal;
begin
if sc_Initialized then
Exit;
sc_Initialized := True;
{$if declared(SuperRttiContextClassDefault)} // optional: default json marshaling class
SuperRttiContextClassDefault := TSerialContext;
{$ifend}
{$if declared(SuperRttiContextDefault)} // optional: default json global marshaling object
SuperRttiContextDefault.Free;
SuperRttiContextDefault := TSerialContext.Create;
{$ifend}
end;
constructor TSerialContext.Create;
begin
inherited;
SuperDateTimeZoneHandling := sdzUTC;
SuperDateFormatHandling := sdfISO; // OLD: sdfJava
SerialFromJson.AddOrSetValue(TypeInfo(TDateTime), SerialFromDateTime);
SerialToJson.AddOrSetValue(TypeInfo(TDateTime), SerialToDateTime);
end;
initialization
//TSerialContext.InitializeGlobal; // optional
end.