Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bdb0307

Browse files
committed
Update the Json/Yaml TList<T> deserialization to work with other TList types, not only <TObject>
1 parent 4c1e8ac commit bdb0307

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

Quick.Json.Serializer.pas

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -620,10 +620,11 @@ function TRTTIJson.DeserializeList(aObject: TObject; const aName : string; const
620620
i : Integer;
621621
n : Integer;
622622
rProp : TRttiProperty;
623-
{$IFNDEF DELPHIRX10_UP}
623+
{$IFDEF DELPHIRX10_UP}
624+
rMethod: TRttiMethod;
625+
{$ELSE}
624626
rfield : TRttiField;
625627
{$ENDIF}
626-
genericType : TGenericListType;
627628
begin
628629
Result := aObject;
629630

@@ -654,17 +655,20 @@ function TRTTIJson.DeserializeList(aObject: TObject; const aName : string; const
654655
if not rValue.IsEmpty then
655656
begin
656657
{$IFDEF DELPHIRX10_UP}
657-
if (TObjectList<TObject>(aObject) <> nil) and (rvalue.IsArray) then
658+
if (aObject <> nil) and (rvalue.IsArray) then
658659
begin
659-
genericType := GetGenericListType(aObject);
660-
if genericType = TGenericListType.gtObjectList then TObjectList<TObject>(aObject).Clear
661-
else TList<TObject>(aObject).Clear;
660+
rMethod := ctx.GetType(aObject.ClassType).GetMethod('Clear');
661+
if rMethod = nil then
662+
raise EJsonDeserializeError.Create('Unable to find RTTI method');
663+
rMethod.Invoke(aObject, []);
664+
665+
rMethod := ctx.GetType(aObject.ClassType).GetMethod('Add');
666+
if rMethod = nil then
667+
raise EJsonDeserializeError.Create('Unable to find RTTI method');
668+
662669
n := rvalue.GetArrayLength - 1;
663670
for i := 0 to n do
664-
begin
665-
if genericType = TGenericListType.gtObjectList then TObjectList<TObject>(aObject).Add(rvalue.GetArrayElement(i).AsObject)
666-
else TList<TObject>(aObject).Add(rvalue.GetArrayElement(i).AsObject);
667-
end;
671+
rMethod.Invoke(aObject, [rvalue.GetArrayElement(i)]);
668672
end;
669673
{$ELSE}
670674
n := 0;
@@ -1256,7 +1260,7 @@ function TRTTIJson.SerializeObject(aObject: TObject): TJSONObject;
12561260
//get list array
12571261
propvalue := GetPropertyValueFromObject(aObject,'List');
12581262
{$IFDEF DELPHIRX10_UP}
1259-
Result := TJSONObject(SerializeDynArray(propvalue,TList<TObject>(aObject).Count));
1263+
Result := TJSONObject(SerializeDynArray(propvalue));
12601264
{$ELSE}
12611265
Result := TJSONObject(SerializeValue(propvalue));
12621266
{$ENDIF}

Quick.YAML.Serializer.pas

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,10 @@ function TRTTIYaml.DeserializeList(aObject: TObject; const aName : string; const
486486
rvalue : TValue;
487487
i : Integer;
488488
rProp : TRttiProperty;
489-
{$IFNDEF DELPHIRX103_UP}
489+
{$IFDEF DELPHIRX103_UP}
490+
rMethod: TRttiMethod;
491+
n: Integer;
492+
{$ELSE}
490493
rfield : TRttiField;
491494
{$ENDIF}
492495
begin
@@ -509,14 +512,20 @@ function TRTTIYaml.DeserializeList(aObject: TObject; const aName : string; const
509512
if not rValue.IsEmpty then
510513
begin
511514
{$IFDEF DELPHIRX103_UP}
512-
if (TObjectList<TObject>(aObject) <> nil) and (rvalue.IsArray) then
515+
if (aObject <> nil) and (rvalue.IsArray) then
513516
begin
514-
TObjectList<TObject>(aObject).Clear;
515-
TObjectList<TObject>(aObject).Capacity := rvalue.GetArrayLength;
516-
for i := 0 to rvalue.GetArrayLength - 1 do
517-
begin
518-
TObjectList<TObject>(aObject).Add(rvalue.GetArrayElement(i).AsObject);
519-
end;
517+
rMethod := ctx.GetType(aObject.ClassType).GetMethod('Clear');
518+
if rMethod = nil then
519+
raise EYamlDeserializeError.Create('Unable to find RTTI method');
520+
rMethod.Invoke(aObject, []);
521+
522+
rMethod := ctx.GetType(aObject.ClassType).GetMethod('Add');
523+
if rMethod = nil then
524+
raise EYamlDeserializeError.Create('Unable to find RTTI method');
525+
526+
n := rvalue.GetArrayLength - 1;
527+
for i := 0 to n do
528+
rMethod.Invoke(aObject, [rvalue.GetArrayElement(i)]);
520529
end;
521530
{$ELSE}
522531
for rfield in rType.GetFields do

0 commit comments

Comments
 (0)