mirror of
https://github.com/danieleteti/delphimvcframework.git
synced 2024-11-15 15:55:54 +01:00
URLEncoded Serializer is able to deserialize data as JSONObject
This commit is contained in:
parent
5f88ef2713
commit
11550c0056
@ -120,7 +120,7 @@ type
|
||||
implementation
|
||||
|
||||
uses
|
||||
System.NetEncoding, System.Math;
|
||||
System.NetEncoding, System.Math, JsonDataObjects;
|
||||
|
||||
{ TMVCURLEncodedDataSerializer }
|
||||
|
||||
@ -158,6 +158,9 @@ procedure TMVCURLEncodedDataSerializer.DeserializeObject(const ASerializedObject
|
||||
const AType: TMVCSerializationType; const AIgnoredAttributes: TMVCIgnoredList; const ARootNode: String);
|
||||
var
|
||||
SL: TStringList;
|
||||
lPieces: TArray<string>;
|
||||
I: Integer;
|
||||
lKeyValue: TArray<string>;
|
||||
begin
|
||||
if (ASerializedObject = EmptyStr) then
|
||||
raise EMVCException.Create(HTTP_STATUS.BadRequest, 'Invalid body');
|
||||
@ -168,8 +171,12 @@ begin
|
||||
SL := TStringList.Create;
|
||||
try
|
||||
try
|
||||
SL.Delimiter := '&';
|
||||
SL.DelimitedText := ASerializedObject;
|
||||
lPieces := ASerializedObject.Split(['&']);
|
||||
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
|
||||
begin
|
||||
RaiseNotImplemented;
|
||||
@ -264,6 +271,8 @@ var
|
||||
I: Integer;
|
||||
lArrValues: TArray<String>;
|
||||
lCurrIdx: Integer;
|
||||
lName: string;
|
||||
lTmp: string;
|
||||
const
|
||||
INITIAL_ARRAY_SIZE = 5;
|
||||
begin
|
||||
@ -272,6 +281,29 @@ begin
|
||||
Exit;
|
||||
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;
|
||||
lFld := nil;
|
||||
|
||||
|
@ -4034,7 +4034,7 @@ begin
|
||||
begin
|
||||
if ARaiseExceptionIfNotExists then
|
||||
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]);
|
||||
end
|
||||
else
|
||||
|
Loading…
Reference in New Issue
Block a user