Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

When deserializing JSON allow fields to be missed in JSON body making partial deserialization possible #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions superobject.pas
Original file line number Diff line number Diff line change
Expand Up @@ -4444,7 +4444,7 @@ procedure TSuperArray.Insert(index: Integer; const value: ISuperObject);
if (index < FLength) then
begin
if FLength = FSize then
Expand(FLength);
Expand(index);
if Index < FLength then
Move(FArray^[index], FArray^[index + 1],
(FLength - index) * SizeOf(Pointer));
Expand Down Expand Up @@ -6114,6 +6114,7 @@ function TSuperRttiContext.FromJson(TypeInfo: PTypeInfo; const obj: ISuperObject
f: TRttiField;
p: Pointer;
v: TValue;
fieldObj:ISuperObject;
begin
Result := True;
TValue.Make(nil, TypeInfo, Value);
Expand All @@ -6126,13 +6127,18 @@ function TSuperRttiContext.FromJson(TypeInfo: PTypeInfo; const obj: ISuperObject
{$ELSE}
p := TValueData(Value).FValueData.GetReferenceToRawData;
{$ENDIF}
Result := FromJson(f.FieldType.Handle, GetFieldDefault(f, obj.AsObject[GetFieldName(f)]), v);
if Result then
f.SetValue(p, v) else
begin
//Writeln(f.Name);
Exit;
end;
fieldObj:=obj.AsObject[GetFieldName(f)];
//ATRLP: 20160708: optional (no value in JSON) fields are allowed
if fieldObj<>nil then
begin
Result := FromJson(f.FieldType.Handle, GetFieldDefault(f, fieldObj), v);
if Result then
f.SetValue(p, v) else
begin
//Writeln(f.Name);
Exit;
end;
end;
end else
begin
Result := False;
Expand Down