onryldz / x-superobject

Delphi Cross Platform Rapid JSON
316 stars 118 forks source link

There's an erro when json string data is null, such as : {"mystring": null} #6

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Issue:

There's an erro when json string data is null, such as : {"mystring": null}

solution:

modify 1:

function TBaseJSON<T, Typ>.GetValue<T>(const Name: Typ): T;
var
  myVal: TValue;
  jVal: TJSONValue;
begin
  myVal:= TValue.From<Typ>(Name);
  if FJSONObj is TJSONObject then
  begin
    jVal:= TJSONObject(FJSONObj).Get(myVal.AsString).JsonValue;
    if (nil<>jVal)and(not jVal.Null) then    // <-- Check if NULL
    begin
      Result:= jVal as T;
    end else begin
      Result:= nil;
    end;
  end else begin
    Result := TJSONArray(FJSONObj).Get(TValue.From<Typ>(Name).AsInteger) as T;
  end;
end;

modify 2:

function TBaseJSON<T, Typ>.GetString(V: Typ): String;
var
  m: TJSONValue;
begin
  Result := '';
  if Member(TValue.From<Typ>(V).AsVariant) then
  begin
    m:= GetValue<TJSONString>(V);
    if m = nil then                     // <-- Check if NULL
    begin
      Result:= '';
    end else begin
      Result := m.Value;
    end;
  end;
end;

Original issue reported on code.google.com by wkhurric...@163.com on 18 Oct 2013 at 2:00

GoogleCodeExporter commented 9 years ago

Original comment by onryld...@gmail.com on 18 Oct 2013 at 5:47

GoogleCodeExporter commented 9 years ago
This issue was closed by revision r20.

Original comment by onryld...@gmail.com on 18 Oct 2013 at 8:10

GoogleCodeExporter commented 9 years ago

Original comment by onryld...@gmail.com on 18 Oct 2013 at 8:11