Daniele Teti 2024-04-29 12:50:25 +02:00
parent 5bda685228
commit dc29941164

View File

@ -160,7 +160,7 @@ type
const SerializationMetaInfo: TSerializationMetaInfo); overload; const SerializationMetaInfo: TSerializationMetaInfo); overload;
procedure JsonArrayToDataSet(const AJsonArray: TJDOJsonArray; const ADataSet: TDataSet; procedure JsonArrayToDataSet(const AJsonArray: TJDOJsonArray; const ADataSet: TDataSet;
const AIgnoredFields: TMVCIgnoredList; const ANameCase: TMVCNameCase); const AIgnoredFields: TMVCIgnoredList; const ANameCase: TMVCNameCase);
function JsonArrayToArray(const AJsonArray: TJDOJsonArray): TValue; function JsonArrayToArray(const AJsonArray: TJDOJsonArray; const ATypeInfo: PTypeInfo): TValue;
{ IMVCSerializer } { IMVCSerializer }
function SerializeObject(const AObject: TObject; const AType: TMVCSerializationType = stDefault; function SerializeObject(const AObject: TObject; const AType: TMVCSerializationType = stDefault;
const AIgnoredAttributes: TMVCIgnoredList = []; const ASerializationAction: TMVCSerializationAction = nil) const AIgnoredAttributes: TMVCIgnoredList = []; const ASerializationAction: TMVCSerializationAction = nil)
@ -912,6 +912,7 @@ var
LJObj: TJDOJsonObject; LJObj: TJDOJsonObject;
lDataSetFields: TMVCDataSetFields; lDataSetFields: TMVCDataSetFields;
begin begin
ADataSet.First;
lDataSetFields := GetDataSetFields(ADataSet, AIgnoredFields, ANameCase); lDataSetFields := GetDataSetFields(ADataSet, AIgnoredFields, ANameCase);
try try
while not ADataSet.Eof do while not ADataSet.Eof do
@ -931,6 +932,7 @@ var
LJArr: TJDOJsonArray; LJArr: TJDOJsonArray;
lDataSetFields: TMVCDataSetFields; lDataSetFields: TMVCDataSetFields;
begin begin
ADataSet.First;
lDataSetFields := GetDataSetFields(ADataSet, AIgnoredFields, ncAsIs); lDataSetFields := GetDataSetFields(ADataSet, AIgnoredFields, ncAsIs);
try try
while not ADataSet.Eof do while not ADataSet.Eof do
@ -1231,13 +1233,16 @@ begin
DeserializeObject(ASerializedObject, TObject(AObject), AType, AIgnoredAttributes); DeserializeObject(ASerializedObject, TObject(AObject), AType, AIgnoredAttributes);
end; end;
function TMVCJsonDataObjectsSerializer.JsonArrayToArray(const AJsonArray: TJDOJsonArray): TValue; function TMVCJsonDataObjectsSerializer.JsonArrayToArray(
const AJsonArray: TJDOJsonArray;
const ATypeInfo: PTypeInfo): TValue;
type type
TSetOfTypeElement = (xString, xInt, xLong, xFloat, xBool); TSetOfTypeElement = (xString, xByte, xInt, xLong, xFloat, xBool);
TSetOfType = set of TSetOfTypeElement; TSetOfType = set of TSetOfTypeElement;
var var
I: Integer; I: Integer;
lStrArr: TArray<string>; lStrArr: TArray<string>;
lByteArr: TArray<Byte>;
lIntArr: TArray<Integer>; lIntArr: TArray<Integer>;
lLongArr: TArray<Int64>; lLongArr: TArray<Int64>;
lDoubleArr: TArray<Double>; lDoubleArr: TArray<Double>;
@ -1255,15 +1260,24 @@ begin
Include(lSetOfType, xString); Include(lSetOfType, xString);
lStrArr := lStrArr + [AJsonArray.Items[I].Value]; lStrArr := lStrArr + [AJsonArray.Items[I].Value];
end; end;
jdtInt: jdtInt, jdtLong:
begin
if ATypeInfo = TypeInfo(TArray<Int64>) then
begin
Include(lSetOfType, xLong);
lLongArr := lLongArr + [AJsonArray.Items[I].LongValue];
end
else
if ATypeInfo = TypeInfo(TArray<Byte>) then
begin
Include(lSetOfType, xByte);
lByteArr := lByteArr + [AJsonArray.Items[I].IntValue];
end
else
begin begin
Include(lSetOfType, xInt); Include(lSetOfType, xInt);
lIntArr := lIntArr + [AJsonArray.Items[I].IntValue]; lIntArr := lIntArr + [AJsonArray.Items[I].IntValue];
end; end;
jdtLong:
begin
Include(lSetOfType, xLong);
lLongArr := lLongArr + [AJsonArray.Items[I].LongValue];
end; end;
jdtFloat: jdtFloat:
begin begin
@ -1290,6 +1304,8 @@ begin
if Length(lStrArr) > 0 then if Length(lStrArr) > 0 then
Exit(TValue.From < TArray < string >> (lStrArr)); Exit(TValue.From < TArray < string >> (lStrArr));
if Length(lByteArr) > 0 then
Exit(TValue.From < TArray < Byte >> (lByteArr));
if Length(lIntArr) > 0 then if Length(lIntArr) > 0 then
Exit(TValue.From < TArray < Integer >> (lIntArr)); Exit(TValue.From < TArray < Integer >> (lIntArr));
if Length(lLongArr) > 0 then if Length(lLongArr) > 0 then
@ -1683,7 +1699,7 @@ begin
end end
else if AValue.isArray then else if AValue.isArray then
begin begin
AValue := JsonArrayToArray(AJSONObject.A[APropertyName]); AValue := JsonArrayToArray(AJSONObject.A[APropertyName], AValue.TypeInfo);
end; end;
end; end;
end; end;