Skip to content

Commit c40a38e

Browse files
Removed Warnings
Removed Warnings Removed access to firends private fields. Simplified Array parsning
1 parent afd84a1 commit c40a38e

6 files changed

Lines changed: 134 additions & 145 deletions

File tree

JSON_PAS.res

-1.24 KB
Binary file not shown.

JsonToDelphiClass.dpr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ uses
99
Pkg.Json.Mapper in 'Pkg.Json.Mapper.pas',
1010
uSaveUnitForm in 'uSaveUnitForm.pas' {SaveUnitForm},
1111
uGitHub in 'uGitHub.pas',
12-
FMX.ConstrainedForm in 'FMX.ConstrainedForm.pas',
12+
FMX.ConstrainedForm in 'FMX.ConstrainedForm.pas' {/ IdSSLOpenSSLHeaders,},
1313
uUpdate in 'uUpdate.pas',
1414
uUpdateForm in 'uUpdateForm.pas' {UpdateForm},
15-
Pkg.Json.Visualizer in 'Pkg.Json.Visualizer.pas';
15+
Pkg.Json.Visualizer in 'Pkg.Json.Visualizer.pas',
16+
Pkg.Json.DTO in 'Pkg.Json.DTO.pas';
1617

1718
{$R *.res}
1819

JsonToDelphiClass.dproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
<FormType>fmx</FormType>
146146
</DCCReference>
147147
<DCCReference Include="Pkg.Json.Visualizer.pas"/>
148+
<DCCReference Include="Pkg.Json.DTO.pas"/>
148149
<BuildConfiguration Include="Release">
149150
<Key>Cfg_2</Key>
150151
<CfgParent>Base</CfgParent>

Pkg.Json.DTO.pas

Lines changed: 7 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ TJsonDTO = class
1111
FOptions: TJsonOptions;
1212
function GetAsJson: string;
1313
procedure SetAsJson(aValue: string);
14-
class function GetSubTypeItemClassFromList(ObjectList: TObject): TClass;
1514
class procedure PrettyPrintPair(aJSONValue: TJSONPair; aOutputStrings: TStrings; Last: Boolean; Indent: Integer);
1615
class procedure PrettyPrintJSON(aJSONValue: TJsonValue; aOutputStrings: TStrings; Indent: Integer = 0); overload;
1716
class procedure PrettyPrintArray(aJSONValue: TJSONArray; aOutputStrings: TStrings; Last: Boolean; Indent: Integer);
@@ -41,7 +40,6 @@ function TJsonDTO.GetAsJson: string;
4140

4241
const
4342
INDENT_SIZE = 2;
44-
4543
class procedure TJsonDTO.PrettyPrintJSON(aJSONValue: TJsonValue; aOutputStrings: TStrings; Indent: Integer);
4644
var
4745
i: Integer;
@@ -122,61 +120,24 @@ class procedure TJsonDTO.PrettyPrintPair(aJSONValue: TJSONPair; aOutputStrings:
122120
aOutputStrings.Add(Line);
123121
end;
124122

125-
class function TJsonDTO.GetSubTypeItemClassFromList(ObjectList: TObject): TClass;
126-
var
127-
CtxRtti: TRttiContext;
128-
TypeRtti: TRttiType;
129-
MethodRtti: TRttiMethod;
130-
ParameterRtti: TRttiParameter;
131-
begin
132-
Result := nil;
133-
CtxRtti := TRttiContext.Create;
134-
TypeRtti := CtxRtti.GetType(ObjectList.ClassType);
135-
MethodRtti := TypeRtti.GetMethod('Add');
136-
137-
for ParameterRtti in MethodRtti.GetParameters do
138-
if SameText(ParameterRtti.Name, 'Value') then
139-
if ParameterRtti.ParamType.IsInstance then
140-
Exit(ParameterRtti.ParamType.AsInstance.MetaclassType);
141-
end;
142-
143123
procedure TJsonDTO.SetAsJson(aValue: string);
144124
var
145125
JSONValue: TJsonValue;
146126
JSONObject: TJSONObject;
147-
var
148-
RttiField: TRttiField;
149-
RttiContext: TRttiContext;
150-
151-
Value: TValue;
152-
List: TObjectList<TObject>;
153-
ElementType: TClass;
154-
Element: TObject;
155127
begin
156-
RttiContext := TRttiContext.Create;
157128
JSONValue := TJSONObject.ParseJSONValue(aValue);
158129
try
159130
if not Assigned(JSONValue) then
160131
Exit;
161132

162133
if (JSONValue is TJSONArray) then
163134
begin
164-
RttiField := TRttiContext.Create.GetType(ClassInfo).GetField('FItems');
165-
Value := RttiField.GetValue(Self);
166-
List := TObjectList<TObject>(Value.AsObject);
167-
ElementType := GetSubTypeItemClassFromList(Value.AsObject);
168-
169-
for JSONValue in (JSONValue as TJSONArray) do
170-
begin
171-
Element := ElementType.Create;
172-
if (JSONValue is TJSONObject) then
173-
begin
174-
TJson.JsonToObject(Element, TJSONObject(JSONValue), FOptions);
175-
List.Add(Element);
176-
end
177-
else
178-
Element.Free;
179-
end;
135+
with TJSONUnMarshal.Create do
136+
try
137+
SetFieldArray(Self, 'Items', (JSONValue as TJSONArray));
138+
finally
139+
Free;
140+
end;
180141

181142
Exit;
182143
end;
@@ -199,3 +160,4 @@ procedure TJsonDTO.SetAsJson(aValue: string);
199160
end;
200161

201162
end.
163+

0 commit comments

Comments
 (0)