Created Serialization and Deserialization of Array (only string and integer, need complete for another types)

Included a test unit "TestSerializeEntityWithArray" and "TestDeserializeEntityWithArray"
This commit is contained in:
Pedro 2019-05-17 11:15:18 -03:00
parent b433abc9a3
commit a3aa3a8897
3 changed files with 111 additions and 3 deletions

View File

@ -96,6 +96,7 @@ type
const AIgnoredFields: TMVCIgnoredList; const ANameCase: TMVCNameCase);
procedure JsonArrayToDataSet(const AJsonArray: TJDOJsonArray; const ADataSet: TDataSet;
const AIgnoredFields: TMVCIgnoredList; const ANameCase: TMVCNameCase);
function JsonArrayToArray(const AJsonArray: TJDOJsonArray):TValue;
{ IMVCSerializer }
function SerializeObject(const AObject: TObject; const AType: TMVCSerializationType = stDefault;
const AIgnoredAttributes: TMVCIgnoredList = [];
@ -189,6 +190,7 @@ var
ChildList: IMVCList;
ValueTypeAtt: MVCValueAsTypeAttribute;
CastValue, CastedValue: TValue;
i:integer;
begin
if AValue.IsEmpty then
begin
@ -340,8 +342,23 @@ begin
tkArray, tkDynArray:
begin
raise EMVCSerializationException.CreateFmt
('Cannot serialize %s of TypeKind tkArray or tkDynArray.', [AName]);
if aValue.getarraylength>0 then
Begin
for i := 0 to aValue.getarraylength-1 do
Begin
case aValue.GetArrayElement(i).Kind of
tkChar, tkString, tkWChar, tkLString, tkWString, tkUString:
AJsonObject.A[AName].Add(aValue.GetArrayElement(i).AsString);
tkInteger:
AJsonObject.A[AName].Add(aValue.GetArrayElement(i).AsInteger);
tkInt64:
AJsonObject.A[AName].Add(aValue.GetArrayElement(i).AsInt64);
else
raise EMVCSerializationException.CreateFmt
('Cannot serialize %s of TypeKind tkArray or tkDynArray.', [AName]);
end;
End;
End;
end;
tkUnknown:
@ -571,6 +588,24 @@ begin
end;
end;
function TMVCJsonDataObjectsSerializer.JsonArrayToArray(
const AJsonArray: TJDOJsonArray):TValue;
var i:integer;
lStrArr:TArray<String>;
lIntArr:TArray<Integer>;
begin
for I := 0 to Pred(AJsonArray.Count) do
case AJsonArray.types[0] of
jdtString : lStrArr := lStrArr + [AJsonArray.Items[i].Value];
jdtInt : lIntArr := lIntArr + [AJsonArray.Items[i].Value.ToInteger];
end;
if Length(lStrArr)>0 then
result := TValue.From<TArray<String>>(lStrArr)
else
result := TValue.From<TArray<Integer>>(lIntArr);
end;
procedure TMVCJsonDataObjectsSerializer.JsonArrayToDataSet(const AJsonArray: TJDOJsonArray;
const ADataSet: TDataSet;
const AIgnoredFields: TMVCIgnoredList; const ANameCase: TMVCNameCase);
@ -716,7 +751,11 @@ begin
raise EMVCDeserializationException.CreateFmt
('You can not deserialize a list %s without the MVCListOf attribute.', [AName]);
end;
end;
end
else if AValue.isArray then
Begin
AValue := JsonArrayToArray(AJsonObject.A[AName]);
End;
end;
end;
end;

View File

@ -273,6 +273,17 @@ type
property NotesAsString: TStringStream read FNotesAsString write FNotesAsString;
end;
TEntityWithArray = class
private
FId: Int64;
FNames: TArray<String>;
FValues: TArray<Integer>;
public
property Id: Int64 read FId write FId;
property Names: TArray<String> read FNames write FNames;
property Values: TArray<Integer> read FValues write FValues;
end;
implementation

View File

@ -77,6 +77,8 @@ type
procedure TestSerializeCollection;
[Test]
procedure TestSerializeDataSet;
[Test]
procedure TestSerializeEntityWithArray;
{ deserialize declarations }
[Test]
procedure TestDeserializeEntity;
@ -92,12 +94,15 @@ type
procedure TestDeserializeCollection;
[Test]
procedure TestDeserializeDataSet;
[Test]
procedure TestDeserializeEntityWithArray;
{ full cycle }
[Test]
procedure TestSerializeDeSerializeEntityWithEnums;
[Test]
procedure TestStringDictionary;
end;
TMVCEntityCustomSerializerJsonDataObjects = class(TInterfacedObject, IMVCTypeSerializer)
@ -615,6 +620,34 @@ begin
end;
end;
procedure TMVCTestSerializerJsonDataObjects.TestDeserializeEntityWithArray;
procedure CheckObject(const AEntity: TEntityWithArray);
begin
Assert.isTrue(AEntity.Id = 1);
Assert.isTrue(AEntity.Names[0] = 'Pedro');
Assert.isTrue(AEntity.Names[1] = 'Oliveira');
Assert.isTrue(AEntity.Values[0] = 1);
Assert.isTrue(AEntity.Values[1] = 2);
end;
const
JSON_WITH_ARRAY =
'{' +
'"Id":1,' +
'"Names":["Pedro","Oliveira"],' +
'"Values":[1,2]' +
'}';
var
O: TEntityWithArray;
begin
O := TEntityWithArray.Create;
try
FSerializer.DeserializeObject(JSON_WITH_ARRAY, O);
CheckObject(O);
finally
O.Free;
end;
end;
procedure TMVCTestSerializerJsonDataObjects.TestSerializeCollection;
const
JSON =
@ -1196,6 +1229,31 @@ begin
end;
end;
procedure TMVCTestSerializerJsonDataObjects.TestSerializeEntityWithArray;
const
JSON_WITH_ARRAY =
'{' +
'"Id":1,' +
'"Names":["Pedro","Oliveira"],' +
'"Values":[1,2]' +
'}';
var
O: TEntityWithArray;
S: string;
begin
O := TEntityWithArray.Create;
try
O.Id := 1;
O.Names := ['Pedro','Oliveira'];
O.Values := [1,2];
S := FSerializer.SerializeObject(O);
Assert.areEqual(JSON_WITH_ARRAY, S);
finally
O.Free;
end;
end;
procedure TMVCTestSerializerJsonDataObjects.TestSerializeNil;
begin
Assert.areEqual('null', FSerializer.SerializeObject(nil));