ahausladen / JsonDataObjects

JSON parser for Delphi 2009 and newer
MIT License
413 stars 160 forks source link

Support for null json values #70

Closed fastbike closed 2 years ago

fastbike commented 2 years ago

The json spec allows for null values. However trying to deserialise a json string to a native delphi type causes an error when the json contains a null.

E.g. given a class

 TMyClass = class
  private
    FValue: TDateTime;  
  published
    property Value; TDateTime read FValue write FValue;
  end;
/// and some code
procedure Parse(data: string);
var
  Json: TJsonObject;
  Obj: TMyClass;
begin
  Json := TJsonObject.Parse(data) as TJsonObject;
  Obj := TMyClass.Create;
  Json.ToSimpleObject(Obj, false);
end;

and pass in {"Value":null} returns an error Cannot cast object into DateTime.

fastbike commented 2 years ago

Just found this JsonSerializationConfig.NullConvertsToValueTypes := True;

Will close - but leave issue in case somebody else has similar issue