Skip to content

Commit

Permalink
Bug Fix for serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
JensBorrisholt committed Jul 31, 2020
1 parent ee2727a commit a66c7e4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
Binary file modified Generator GUI/JSON_PAS.res
Binary file not shown.
30 changes: 30 additions & 0 deletions Lib/Pkg.Json.DTO.pas
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ TJsonDTO = class(TArrayMapper)
property AsJson: string read GetAsJson write SetAsJson;
end;

GenericListReflectAttribute = class(JsonReflectAttribute)
public
constructor Create;
end;

implementation

uses
Expand Down Expand Up @@ -188,5 +193,30 @@ function TArrayMapper.ObjectList<T>(var aList: TObjectList<T>; aSource: TArray<T
Exit(aList);
end;

type
TGenericListFieldInterceptor = class(TJSONInterceptor)
public
function ObjectsConverter(Data: TObject; Field: string): TListOfObjects; override;
end;

{ TListFieldInterceptor }

function TGenericListFieldInterceptor.ObjectsConverter(Data: TObject; Field: string): TListOfObjects;
var
ctx: TRttiContext;
List: TList<TObject>;
RttiProperty: TRttiProperty;
begin
RttiProperty := ctx.GetType(Data.ClassInfo).GetProperty(Copy(Field, 2, MAXINT));
List := TList<TObject>(RttiProperty.GetValue(Data).AsObject);
Result := TListOfObjects(List.List);
SetLength(Result, List.Count);
end;

constructor GenericListReflectAttribute.Create;
begin
inherited Create(ctObjects, rtObjects, TGenericListFieldInterceptor, nil, false);
end;

end.

Expand Down
5 changes: 3 additions & 2 deletions Lib/Pkg.Json.StubField.pas
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ function TStubClass.GetImplementationPart: string;
Lines.Add('');
Lines.AddFormat('function %s.Get%s: TObjectList<%s>;', [Name, StubField.PropertyName, StubArrayField.TypeAsString]);
Lines.Add('begin');
Lines.AddFormat(' Result := ObjectList<%s>(%s, %sArray);', [StubArrayField.TypeAsString, StubField.FieldName, StubField.FieldName]);
Lines.AddFormat(' Result := ObjectList<%s>(%s, %sArray);', [StubArrayField.TypeAsString, StubField.FieldName, StubField.FieldName]);
Lines.Add('end;');
end;

Expand Down Expand Up @@ -249,8 +249,9 @@ Lines.Add(Name + ' = class' + IfThen(BaseClass = '', '', '(' + BaseClass + '
begin
StubArrayField := StubField as TStubArrayField;
Lines.Add(' ' + StubField.NameAttribute);
Lines.AddFormat(' %sArray: TArray<%s>;', [StubField.FieldName, StubField.TypeAsString]);
Lines.Add(' [JSONMarshalled(False)]');
Lines.AddFormat(' %sArray: TArray<%s>;', [StubField.FieldName, StubField.TypeAsString]);
Lines.Add(' [GenericListReflect]');
Lines.AddFormat(' %s: TObjectList<%s>;', [StubField.FieldName, StubArrayField.TypeAsString]);
end
else
Expand Down

0 comments on commit a66c7e4

Please sign in to comment.