-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOldRttiMarshal.pas
169 lines (143 loc) · 4.42 KB
/
OldRttiMarshal.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
unit OldRttiMarshal;
interface
uses TypInfo, SuperObject, Classes;
type
TOldRttiMarshal = class
private
function ToClass(AObject: TObject): ISuperObject;
function ToList(AList: TList): ISuperObject;
function ToInteger(AObject: TObject; APropInfo: PPropInfo): ISuperObject;
function ToInt64(AObject: TObject; APropInfo: PPropInfo): ISuperObject;
function ToFloat(AObject: TObject; APropInfo: PPropInfo): ISuperObject;
function ToJsonString(AObject: TObject; APropInfo: PPropInfo): ISuperObject;
function ToChar(AObject: TObject; APropInfo: PPropInfo): ISuperObject;
public
class function ToJson(AObject: TObject): ISuperObject;
end;
implementation
uses Variants, JsonListAdapter, SysUtils, RestJsonUtils;
{ TOldRttiMarshal }
function TOldRttiMarshal.ToChar(AObject: TObject;APropInfo: PPropInfo): ISuperObject;
begin
Result := TSuperObject.Create(Char(GetOrdProp(AObject, APropInfo)));
end;
function TOldRttiMarshal.ToClass(AObject: TObject): ISuperObject;
var
i: Integer;
vTypeInfo: PTypeInfo;
vTypeData: PTypeData;
vPropList: PPropList;
vPropInfo: PPropInfo;
value: ISuperObject;
vObjProp: TObject;
vAdapter: IJsonListAdapter;
begin
if AObject = nil then
begin
Result := SuperObject.SO();
Exit;
end;
if (AObject is TList) then
begin
Result := ToList(TList(AObject));
end
else if Supports(AObject, IJsonListAdapter, vAdapter) then
begin
Result := ToList(TList(vAdapter.UnWrapList));
end
else
begin
vTypeInfo := AObject.ClassInfo;
if vTypeInfo = nil then
begin
raise ENoSerializableClass.Create(AObject.ClassType);
end;
vTypeData := GetTypeData(vTypeInfo);
Result := SO();
New(vPropList);
try
GetPropList(vTypeInfo, tkProperties, vPropList);
for i := 0 to vTypeData^.PropCount-1 do
begin
vPropInfo := vPropList^[i];
value := nil;
case vPropInfo^.PropType^.Kind of
tkMethod: ;
tkSet, tkInteger, tkEnumeration: value := ToInteger(AObject, vPropInfo);
tkInt64: value := ToInt64(AObject, vPropInfo);
tkFloat: value := ToFloat(AObject, vPropInfo);
tkChar, tkWChar: value := ToChar(AObject, vPropInfo);
tkString, tkLString,
{$IFDEF UNICODE}
tkUString,
{$ENDIF}
tkWString: value := ToJsonString(AObject, vPropInfo);
tkClass: begin
value := nil;
vObjProp := GetObjectProp(AObject, vPropInfo);
if Assigned(vObjProp) then
begin
if vObjProp is TList then
value := ToList(TList(vObjProp))
else
value := ToClass(vObjProp);
end;
end;
end;
if Assigned(value) then
begin
Result.O[String(vPropInfo^.Name)] := value;
end;
end;
finally
Dispose(vPropList);
end;
end;
end;
function TOldRttiMarshal.ToFloat(AObject: TObject; APropInfo: PPropInfo): ISuperObject;
begin
if APropInfo^.PropType^ = System.TypeInfo(TDateTime) then
Result := TSuperObject.Create(DelphiToJsonDateTime(GetFloatProp(AObject, APropInfo)))
else
Result := TSuperObject.Create(GetFloatProp(AObject, APropInfo));
end;
function TOldRttiMarshal.ToInt64(AObject: TObject;APropInfo: PPropInfo): ISuperObject;
begin
Result := TSuperObject.Create(GetInt64Prop(AObject, APropInfo));
end;
function TOldRttiMarshal.ToInteger(AObject: TObject; APropInfo: PPropInfo): ISuperObject;
var
vIntValue: Integer;
begin
vIntValue := GetOrdProp(AObject, APropInfo);
if APropInfo^.PropType^ = TypeInfo(Boolean) then
Result := TSuperObject.Create(Boolean(vIntValue))
else
Result := TSuperObject.Create(vIntValue);
end;
class function TOldRttiMarshal.ToJson(AObject: TObject): ISuperObject;
var
vMarshal: TOldRttiMarshal;
begin
vMarshal := TOldRttiMarshal.Create;
try
Result := vMarshal.ToClass(AObject);
finally
vMarshal.Free;
end;
end;
function TOldRttiMarshal.ToJsonString(AObject: TObject;APropInfo: PPropInfo): ISuperObject;
begin
Result := TSuperObject.Create(GetWideStrProp(AObject, APropInfo));
end;
function TOldRttiMarshal.ToList(AList: TList): ISuperObject;
var
i: Integer;
begin
Result := TSuperObject.Create(stArray);
for i := 0 to AList.Count-1 do
begin
Result.AsArray.Add(ToClass(TObject(AList.Items[i])));
end;
end;
end.