Added support for deserializing generic lists without MVCListOf attribute.

This commit is contained in:
Daniele Teti 2019-10-15 09:33:38 +02:00
parent 2830a7e27f
commit cbbf2956f4

View File

@ -110,6 +110,8 @@ type
[Test] [Test]
procedure TestSerializeDeserializeGenericEntity; procedure TestSerializeDeserializeGenericEntity;
[Test]
procedure TestSerializeDeserializeMultipleGenericEntity;
end; end;
TMVCEntityCustomSerializerJsonDataObjects = class(TInterfacedObject, IMVCTypeSerializer) TMVCEntityCustomSerializerJsonDataObjects = class(TInterfacedObject, IMVCTypeSerializer)
@ -1410,6 +1412,83 @@ begin
end; end;
end; end;
procedure TMVCTestSerializerJsonDataObjects.TestSerializeDeserializeMultipleGenericEntity;
const
JSON =
'{' +
'"Code":1,' +
'"Description":"General Description",' +
'"Items":[' +
'{"Description":"Description 01"},' +
'{"Description":"Description 02"},' +
'{"Description":"Description 03"},' +
'{"Description":"Description 04"},' +
'{"Description":"Description 05"}' +
'],'+
'"Items2":[' +
'{"Description":"Description2 01"},' +
'{"Description":"Description2 02"},' +
'{"Description":"Description2 03"},' +
'{"Description":"Description2 04"},' +
'{"Description":"Description2 05"}' +
']'+
'}';
var
LGenericEntity: TMultipleGenericEntity<TNote, TNote>;
LJson: string;
begin
LGenericEntity := TMultipleGenericEntity<TNote, TNote>.Create;
try
LGenericEntity.Code := 1;
LGenericEntity.Description := 'General Description';
LGenericEntity.Items.Add(TNote.Create('Description 01'));
LGenericEntity.Items.Add(TNote.Create('Description 02'));
LGenericEntity.Items.Add(TNote.Create('Description 03'));
LGenericEntity.Items.Add(TNote.Create('Description 04'));
LGenericEntity.Items.Add(TNote.Create('Description 05'));
LGenericEntity.Items2.Add(TNote.Create('Description2 01'));
LGenericEntity.Items2.Add(TNote.Create('Description2 02'));
LGenericEntity.Items2.Add(TNote.Create('Description2 03'));
LGenericEntity.Items2.Add(TNote.Create('Description2 04'));
LGenericEntity.Items2.Add(TNote.Create('Description2 05'));
LJson := FSerializer.SerializeObject(LGenericEntity);
Assert.AreEqual(JSON, LJson);
finally
LGenericEntity.Free;
end;
LGenericEntity := TMultipleGenericEntity<TNote, TNote>.Create;
try
FSerializer.DeserializeObject(LJson, LGenericEntity);
Assert.AreEqual(Integer(1), LGenericEntity.Code);
Assert.AreEqual('General Description', LGenericEntity.Description);
Assert.AreEqual(Integer(5), LGenericEntity.Items.Count);
Assert.AreEqual('Description 01', LGenericEntity.Items[0].Description);
Assert.AreEqual('Description 02', LGenericEntity.Items[1].Description);
Assert.AreEqual('Description 03', LGenericEntity.Items[2].Description);
Assert.AreEqual('Description 04', LGenericEntity.Items[3].Description);
Assert.AreEqual('Description 05', LGenericEntity.Items[4].Description);
Assert.AreEqual(Integer(5), LGenericEntity.Items2.Count);
Assert.AreEqual('Description2 01', LGenericEntity.Items2[0].Description);
Assert.AreEqual('Description2 02', LGenericEntity.Items2[1].Description);
Assert.AreEqual('Description2 03', LGenericEntity.Items2[2].Description);
Assert.AreEqual('Description2 04', LGenericEntity.Items2[3].Description);
Assert.AreEqual('Description2 05', LGenericEntity.Items2[4].Description);
finally
LGenericEntity.Free;
end;
end;
procedure TMVCTestSerializerJsonDataObjects.TestSerializeNil; procedure TMVCTestSerializerJsonDataObjects.TestSerializeNil;
begin begin
Assert.areEqual('null', FSerializer.SerializeObject(nil)); Assert.areEqual('null', FSerializer.SerializeObject(nil));