Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program TestDMVCSerUnSerWithNulls;
- {$APPTYPE CONSOLE}
- {$R *.res}
- uses
- MVCFramework,
- MVCFramework.Serializer.Commons,
- MVCFramework.Nullables,
- MVCFramework.Serializer.JsonDataObjects,
- JsonDataObjects;
- type
- {$M+}
- TObjB = class
- private
- FAnInt: integer;
- public
- property AnInt: integer read FAnInt write FAnInt;
- end;
- TObjA = class
- private
- FObjB: TObjB;
- FX: NullableString;
- FY: NullableString;
- public
- property Y: NullableString read FY write FY;
- property ObjB: TObjB read FObjB write FObjB;
- property X: NullableString read FX write FX;
- end;
- TObjC = class
- private
- FObjA: TObjA;
- fPropStr: String;
- public
- property PropStr: String read fPropStr write fPropStr;
- property ObjA: TObjA read FObjA write FObjA;
- end;
- procedure Test;
- var
- s: string;
- lSer: TMVCJsonDataObjectsSerializer;
- j: TJsonObject;
- oo: TObjC;
- begin
- s := '{"PropStr":"This is a value", "ObjA": {"Y": "Hello There","ObjB": null,"X": null}}';
- j := TMVCJsonDataObjectsSerializer.ParseObject(s);
- oo := TObjC.Create;
- oo.ObjA := nil;
- lSer := TMVCJsonDataObjectsSerializer.Create;
- lSer.JsonObjectToObject(j, oo, stProperties, []);
- //"oo.ObjA" has not been instantiated, it is still null
- if (not Assigned(oo.ObjA)) and (not j.IsNull('ObjA')) then
- begin
- oo.ObjA := TObjA.Create;
- lSer.JsonObjectToObject(j.O['ObjA'], oo.ObjA, stProperties, []);
- end;
- if oo.ObjA = nil then
- begin
- WriteLn('ObjA is nil');
- end
- else
- begin
- WriteLn('String Value is: ', oo.PropStr);
- WriteLn('oo.ObjA.Y is: ', oo.ObjA.Y.ValueOrDefault);
- end;
- end;
- begin
- Test;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement