-4

This is a valid json:

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

How do I create a TSuperObject from this string?

EProgrammerNotFound
  • 2,403
  • 4
  • 28
  • 59

2 Answers2

2

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;
Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
  • First line (after begin) raises Integer overflow, so as the second line – EProgrammerNotFound Sep 02 '14 at 14:50
  • Either those examples are deprecated or they are wrong. – EProgrammerNotFound Sep 02 '14 at 14:55
  • 1
    `Readme.html` is a perfect name for a file you should read, but maybe more people will read if the name is changed to `Secret-PleaseDoNotRead.html`. If you are against this complain with the author. You should also keep an eye on the [issue list](https://code.google.com/p/superobject/issues/list) because thats the point for being public (there is still an open issue dealing with that `EIntOverflow` exception). BTW The code works perfect on XE6 – Sir Rufo Sep 02 '14 at 15:31
  • Please define "this code does not work" precisely. In what way does it fail? – David Heffernan Sep 02 '14 at 19:45
  • @DavidHeffernan I have already said that. it raises EIntOverflow. – EProgrammerNotFound Sep 03 '14 at 18:29
  • I can't see where you said that. Your statement that *This code does not work for Delphi 6* is next to useless. – David Heffernan Sep 03 '14 at 18:31
  • @SirRufo Your answer is valid to Delphi XE6, but I've said in the question that I was using Delphi 6 (see tag). As you've said there is a bug in the Delphi 6, I answered with more details. Anyway, you showed the right way to parse the string, except that it will not work in delphi 6 because of the bug. – EProgrammerNotFound Oct 30 '14 at 17:59
  • The example in the readme file is: obj := TSuperObject.ParseStream(stream), but the code actually is: TSuperObject.ParseStream(stream: TStream; strict: Boolean; partial: Boolean; const this: TSuperObject; options: TSuperFindOptions; const put: ISuperObject; dt: TSuperType): ISuperObject - not quite close – SysJames Feb 03 '16 at 22:23
2

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+}
EProgrammerNotFound
  • 2,403
  • 4
  • 28
  • 59