#1 2013-06-27 07:51:49

DigDiver
Member
Registered: 2013-04-29
Posts: 137

JSONToObject and null string property

Is it correct that while decoding a string field that contains NULL in JSON, decoding must be interrupted? Probably it would be correct to continue decoding?

Type
 TLinkItem = class(TCollectionItem)
 private
  Fid          : integer;
  Fanchor      : String;
  Furl         : String;
 public
  constructor Create(Collection: TCollection); override;
  destructor  Destroy; override;
 published
  property Anchor       : String   read Fanchor      write Fanchor;
  property Id           : integer   read Fid          write Fid;
  property Url          : String   read Furl         write Furl;
 end;

Type
 TLinkItems = Class(TCollection)
 private
  function  GetItem(index: integer): TLinkItem;
  procedure SetItem(index: integer; value : TLinkItem);
 public
  constructor Create;
  function Add(_Fid : integer; _Fanchor : String; _Furl : String): TLinkItem;
  property Items[index: integer]: TLinkItem read GetItem write SetItem; default;
end;

JSON:

[
  {
   id: 107309,
   url: null,
   anchor: ""
  },
  {
   id: 107312,
   url: "http://www.google.com",
   anchor: "google"
  }
]

Offline

#2 2013-06-27 08:27:20

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: JSONToObject and null string property

Is not 'null' reserved for objects, in JSON?

In fact, there is no "type expectation" in JSON itself, but if you expect a string field to be a string, you expect "" and not null.
In Delphi, there is no nullable string, as it does exist with C# or Java.
I suspect it should be reflected in JSON also:

[
  {
   id: 107309,
   url: "",
   anchor: ""
  },
  {
   id: 107312,
   url: "http://www.google.com",
   anchor: "google"
  }
]

But we can change this behavior to let the JSONToObject() method be less rigid about its expectations.

Offline

#3 2013-06-28 07:49:15

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,571
Website

Re: JSONToObject and null string property

AB, you are absolutely right about null in JSON. Current implementation work in right way (http://json.org/  -  string MUST be in ""). Please - not change current realization.

Offline

Board footer

Powered by FluxBB