URLEncoded Serializer is able to deserialize data as JSONObject

This commit is contained in:
Daniele Teti 2023-10-07 00:02:20 +02:00
parent 5f88ef2713
commit 11550c0056
2 changed files with 38 additions and 6 deletions

View File

@ -120,7 +120,7 @@ type
implementation implementation
uses uses
System.NetEncoding, System.Math; System.NetEncoding, System.Math, JsonDataObjects;
{ TMVCURLEncodedDataSerializer } { TMVCURLEncodedDataSerializer }
@ -158,6 +158,9 @@ procedure TMVCURLEncodedDataSerializer.DeserializeObject(const ASerializedObject
const AType: TMVCSerializationType; const AIgnoredAttributes: TMVCIgnoredList; const ARootNode: String); const AType: TMVCSerializationType; const AIgnoredAttributes: TMVCIgnoredList; const ARootNode: String);
var var
SL: TStringList; SL: TStringList;
lPieces: TArray<string>;
I: Integer;
lKeyValue: TArray<string>;
begin begin
if (ASerializedObject = EmptyStr) then if (ASerializedObject = EmptyStr) then
raise EMVCException.Create(HTTP_STATUS.BadRequest, 'Invalid body'); raise EMVCException.Create(HTTP_STATUS.BadRequest, 'Invalid body');
@ -168,8 +171,12 @@ begin
SL := TStringList.Create; SL := TStringList.Create;
try try
try try
SL.Delimiter := '&'; lPieces := ASerializedObject.Split(['&']);
SL.DelimitedText := ASerializedObject; for I := 0 to High(lPieces) do
begin
lKeyValue := lPieces[I].Split(['=']);
SL.AddPair(lKeyValue[0], URLDecode(lKeyValue[1]));
end;
if GetTypeSerializers.ContainsKey(AObject.ClassInfo) then if GetTypeSerializers.ContainsKey(AObject.ClassInfo) then
begin begin
RaiseNotImplemented; RaiseNotImplemented;
@ -264,6 +271,8 @@ var
I: Integer; I: Integer;
lArrValues: TArray<String>; lArrValues: TArray<String>;
lCurrIdx: Integer; lCurrIdx: Integer;
lName: string;
lTmp: string;
const const
INITIAL_ARRAY_SIZE = 5; INITIAL_ARRAY_SIZE = 5;
begin begin
@ -272,6 +281,29 @@ begin
Exit; Exit;
end; end;
if TypeInfo(tjsonobject) = AObject.ClassInfo then
begin
for I := 0 to Data.Count-1 do
begin
lName := Data.Names[I];
if TJsonObject(AObject).IsNull(lName) then
begin
TJsonObject(AObject).S[lName] := Data.ValueFromIndex[I];
end
else
begin
if TJsonObject(AObject).Types[lName] = jdtString then
begin
lTmp := TJsonObject(AObject).S[lName];
TJsonObject(AObject).Remove(lName);
TJsonObject(AObject).A[lName].Add(lTmp);
end;
TJsonObject(AObject).A[lName].Add(Data.ValueFromIndex[I]);
end;
end;
Exit;
end;
lProp := nil; lProp := nil;
lFld := nil; lFld := nil;

View File

@ -4034,7 +4034,7 @@ begin
begin begin
if ARaiseExceptionIfNotExists then if ARaiseExceptionIfNotExists then
begin begin
raise EMVCException.CreateFmt('The serializer for %s could not be found.', raise EMVCException.CreateFmt('The serializer for %s could not be found. [HINT] Register on TMVCEngine instance using "AddSerializer" method.',
[lContentMediaType]); [lContentMediaType]);
end end
else else