Skip to content

Commit

Permalink
Removed Warnings
Browse files Browse the repository at this point in the history
Removed Warnings
Removed access to firends private fields. Simplified Array parsning
  • Loading branch information
JensBorrisholt committed Jan 13, 2020
1 parent afd84a1 commit c40a38e
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 145 deletions.
Binary file modified JSON_PAS.res
Binary file not shown.
5 changes: 3 additions & 2 deletions JsonToDelphiClass.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ uses
Pkg.Json.Mapper in 'Pkg.Json.Mapper.pas',
uSaveUnitForm in 'uSaveUnitForm.pas' {SaveUnitForm},
uGitHub in 'uGitHub.pas',
FMX.ConstrainedForm in 'FMX.ConstrainedForm.pas',
FMX.ConstrainedForm in 'FMX.ConstrainedForm.pas' {/ IdSSLOpenSSLHeaders,},
uUpdate in 'uUpdate.pas',
uUpdateForm in 'uUpdateForm.pas' {UpdateForm},
Pkg.Json.Visualizer in 'Pkg.Json.Visualizer.pas';
Pkg.Json.Visualizer in 'Pkg.Json.Visualizer.pas',
Pkg.Json.DTO in 'Pkg.Json.DTO.pas';

{$R *.res}

Expand Down
1 change: 1 addition & 0 deletions JsonToDelphiClass.dproj
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
<FormType>fmx</FormType>
</DCCReference>
<DCCReference Include="Pkg.Json.Visualizer.pas"/>
<DCCReference Include="Pkg.Json.DTO.pas"/>
<BuildConfiguration Include="Release">
<Key>Cfg_2</Key>
<CfgParent>Base</CfgParent>
Expand Down
52 changes: 7 additions & 45 deletions Pkg.Json.DTO.pas
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ TJsonDTO = class
FOptions: TJsonOptions;
function GetAsJson: string;
procedure SetAsJson(aValue: string);
class function GetSubTypeItemClassFromList(ObjectList: TObject): TClass;
class procedure PrettyPrintPair(aJSONValue: TJSONPair; aOutputStrings: TStrings; Last: Boolean; Indent: Integer);
class procedure PrettyPrintJSON(aJSONValue: TJsonValue; aOutputStrings: TStrings; Indent: Integer = 0); overload;
class procedure PrettyPrintArray(aJSONValue: TJSONArray; aOutputStrings: TStrings; Last: Boolean; Indent: Integer);
Expand Down Expand Up @@ -41,7 +40,6 @@ function TJsonDTO.GetAsJson: string;

const
INDENT_SIZE = 2;

class procedure TJsonDTO.PrettyPrintJSON(aJSONValue: TJsonValue; aOutputStrings: TStrings; Indent: Integer);
var
i: Integer;
Expand Down Expand Up @@ -122,61 +120,24 @@ class procedure TJsonDTO.PrettyPrintPair(aJSONValue: TJSONPair; aOutputStrings:
aOutputStrings.Add(Line);
end;

class function TJsonDTO.GetSubTypeItemClassFromList(ObjectList: TObject): TClass;
var
CtxRtti: TRttiContext;
TypeRtti: TRttiType;
MethodRtti: TRttiMethod;
ParameterRtti: TRttiParameter;
begin
Result := nil;
CtxRtti := TRttiContext.Create;
TypeRtti := CtxRtti.GetType(ObjectList.ClassType);
MethodRtti := TypeRtti.GetMethod('Add');

for ParameterRtti in MethodRtti.GetParameters do
if SameText(ParameterRtti.Name, 'Value') then
if ParameterRtti.ParamType.IsInstance then
Exit(ParameterRtti.ParamType.AsInstance.MetaclassType);
end;

procedure TJsonDTO.SetAsJson(aValue: string);
var
JSONValue: TJsonValue;
JSONObject: TJSONObject;
var
RttiField: TRttiField;
RttiContext: TRttiContext;

Value: TValue;
List: TObjectList<TObject>;
ElementType: TClass;
Element: TObject;
begin
RttiContext := TRttiContext.Create;
JSONValue := TJSONObject.ParseJSONValue(aValue);
try
if not Assigned(JSONValue) then
Exit;

if (JSONValue is TJSONArray) then
begin
RttiField := TRttiContext.Create.GetType(ClassInfo).GetField('FItems');
Value := RttiField.GetValue(Self);
List := TObjectList<TObject>(Value.AsObject);
ElementType := GetSubTypeItemClassFromList(Value.AsObject);

for JSONValue in (JSONValue as TJSONArray) do
begin
Element := ElementType.Create;
if (JSONValue is TJSONObject) then
begin
TJson.JsonToObject(Element, TJSONObject(JSONValue), FOptions);
List.Add(Element);
end
else
Element.Free;
end;
with TJSONUnMarshal.Create do
try
SetFieldArray(Self, 'Items', (JSONValue as TJSONArray));
finally
Free;
end;

Exit;
end;
Expand All @@ -199,3 +160,4 @@ procedure TJsonDTO.SetAsJson(aValue: string);
end;

end.

Loading

0 comments on commit c40a38e

Please sign in to comment.