How to create a TSuperObject instance by parsing a json array?

1.3k Views Asked by At

This is a valid json:

[{"id":1, "name":"foo"}, {"id":2, "name":"bar"}]

How do I create a TSuperObject from this string?

2

There are 2 best solutions below

0
On BEST ANSWER

There is a bug for Delphi 6.

When the SO() function tries to convert the value of the string, it raises EIntOverflow.

The bug is due to this function:

class function TSuperAvlEntry.Hash(const k: SOString): Cardinal; 

This is the bug in the google's issue tracker

The workaround proposed by the bug's reporter is changing the function to this:

class function TSuperAvlEntry.Hash(const k: SOString): Cardinal;
var
  h: cardinal;
  i: Integer;
begin
  h := 0;
{$Q-}
  for i := 1 to Length(k) do
    h := Cardinal( h*129 + ord(k[i]) + $9e370001);
  Result := h;
end;
{$Q+}
8
On

If you open the readme.html inside a browser you will see at the very first beginning of that document:

Parsing a JSON data structure

var
  obj: ISuperObject;
begin
  obj := SO('{"foo": true}');
  obj := TSuperObject.ParseString('{"foo": true}');
  obj := TSuperObject.ParseStream(stream);
  obj := TSuperObject.ParseFile(FileName);
end;