Skip to content

Support for null values #28

@yonojoy

Description

@yonojoy

We have a server that delivers the following JSON (for TClient):

{
	"id": 6718,
	"first_name": "Hans",
	"last_name": "Bauer",
	"telephone": null,
	"email": null,
	"gender": null
}

If I read this into a TClient variable and then send this to the server again, this is translated to

{
	"id": 6718,
	"first_name": "Hans",
	"last_name": "Bauer",
	"telephone": "",
	"email": "",
	"gender": 0
}

which the server does not like: "0 is not a valid value for gender".

There are two solutions to this problem:

  1. Implement handling for null values
  2. Provide an option to treat null as not ...HasValue

While I think 1 is the correct solution, 2 can be implemented more easily by modifying OpenApiJson.pas, for example for USEDBX:

function TJsonWrapper.ObjContains(JObj: TJSONValue; const Name: string; out Value: TJSONValue): Boolean;
var
  Pair: TJSONPair;
begin
  Pair := TJSONObject(JObj).Get(Name);
  if Assigned(Pair) then
  begin
	Value := Pair.JsonValue;
	if Value.Null and FOptionTreatNullAsNotHasValue then
	  Value := nil;
  end
  else
	Value := nil;
end;

Solution 2 would result in the following JSON, which the server happily processes:

{
	"id": 6718,
	"first_name": "Hans",
	"last_name": "Bauer"
}

@wlandgraf What do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions