Merge pull request #416 from osaris/feature_rootnode_bodyaslistof

Handle RootNode when deserializing a collection of objects (BodyAsListOf)
This commit is contained in:
Daniele Teti 2020-09-08 16:22:28 +02:00 committed by GitHub
commit a0aea669ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 7 deletions

View File

@ -134,7 +134,8 @@ type
const AList: TObject;
const AClazz: TClass;
const AType: TMVCSerializationType = stDefault;
const AIgnoredAttributes: TMVCIgnoredList = nil
const AIgnoredAttributes: TMVCIgnoredList = nil;
const ARootNode: String = ''
); overload;
procedure DeserializeCollection(

View File

@ -142,7 +142,8 @@ type
const AType: TMVCSerializationType = stDefault; const AIgnoredAttributes: TMVCIgnoredList = []); overload;
procedure DeserializeCollection(const ASerializedList: string; const AList: TObject; const AClazz: TClass;
const AType: TMVCSerializationType = stDefault; const AIgnoredAttributes: TMVCIgnoredList = []); overload;
const AType: TMVCSerializationType = stDefault; const AIgnoredAttributes: TMVCIgnoredList = [];
const ARootNode: string = ''); overload;
procedure DeserializeCollection(const ASerializedList: string; const AList: IInterface; const AClazz: TClass;
const AType: TMVCSerializationType = stDefault; const AIgnoredAttributes: TMVCIgnoredList = []); overload;
@ -763,9 +764,12 @@ begin
end;
procedure TMVCJsonDataObjectsSerializer.DeserializeCollection(const ASerializedList: string; const AList: TObject;
const AClazz: TClass; const AType: TMVCSerializationType; const AIgnoredAttributes: TMVCIgnoredList);
const AClazz: TClass; const AType: TMVCSerializationType; const AIgnoredAttributes: TMVCIgnoredList;
const ARootNode: string);
var
JsonArray: TJDOJsonArray;
JsonBase: TJDOJsonBaseObject;
JSONObject : TJDOJsonObject;
ObjList: IMVCList;
begin
if (ASerializedList = EmptyStr) then
@ -777,7 +781,28 @@ begin
ObjList := TDuckTypedList.Wrap(AList);
if Assigned(ObjList) then
begin
JsonArray := TJDOJsonArray.Parse(ASerializedList) as TJDOJsonArray;
if ARootNode.IsEmpty then
begin
JsonArray := TJDOJsonArray.Parse(ASerializedList) as TJDOJsonArray;
end
else
begin
try
JsonBase := TJDOJsonObject.Parse(ASerializedList);
if not(JsonBase is TJDOJsonObject) then
begin
raise EMVCSerializationException.CreateFmt('Invalid JSON. Expected %s got %s',
[TJDOJsonObject.ClassName, JsonBase.ClassName]);
end;
JSONObject := TJDOJsonObject(JsonBase);
except
on E: EJsonParserException do
begin
raise EMVCException.Create(HTTP_STATUS.BadRequest, E.Message);
end;
end;
JsonArray := JSONObject.A[ARootNode] as TJDOJsonArray;
end;
try
JsonArrayToList(JsonArray, ObjList, AClazz, AType, AIgnoredAttributes);
finally

View File

@ -360,7 +360,7 @@ type
function Cookie(const AName: string): string;
function Body: string;
function BodyAs<T: class, constructor>(const RootNode: string = ''): T;
function BodyAsListOf<T: class, constructor>: TObjectList<T>;
function BodyAsListOf<T: class, constructor>(const RootNode: string = ''): TObjectList<T>;
procedure BodyFor<T: class, constructor>(const AObject: T);
procedure BodyForListOf<T: class, constructor>(const AObjectList: TObjectList<T>);
// function HeaderNames: TArray<String>;
@ -1171,7 +1171,7 @@ begin
raise EMVCDeserializationException.CreateFmt('Body ContentType "%s" not supported', [ContentType]);
end;
function TMVCWebRequest.BodyAsListOf<T>: TObjectList<T>;
function TMVCWebRequest.BodyAsListOf<T>(const RootNode: string): TObjectList<T>;
var
List: TObjectList<T>;
lSerializer: IMVCSerializer;
@ -1181,7 +1181,7 @@ begin
begin
List := TObjectList<T>.Create(True);
try
lSerializer.DeserializeCollection(Body, List, T);
lSerializer.DeserializeCollection(Body, List, T, TMVCSerializationType.stDefault, nil, RootNode);
Result := List;
except
FreeAndNil(List);