2

I'm trying to parse this string using SuperObject in Delphi 7.

procedure TForm1.btn1Click(Sender: TObject);
var
 obj: ISuperObject;
 fw:string;
begin
  fw:= '{"type":"normal","info":{"Name":"frank","Number":"01","Age":"21","registered":"Yes","Support":"Expired"}}';
  obj := TSuperObject.ParseString(PWideChar(fw), false);
  mmo1.lines.Add(obj.AsJSon(true,false));
end;

But result in the memo is like this:

"????????????????????????????????????????????????????}"

What am I doing wrong?

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
dudey
  • 87
  • 5
  • 13

2 Answers2

3

fw is a string which in Delphi 7 is 8 bit ANSI encoded. The cast to PWideChar is thus incorrect. It will treat the 8 bit text as if it were UTF-16 encoded.

If the function you are calling really does receive PWideChar then you need to convert to UTF-16 first. For example like this:

PWideChar(WideString(fw))

You also report a separate problem that arises in the super object code. Specifically this line of code:

h := h*129 + ord(k[i]) + $9e370001;

raises an overflow error.

That happens because your project has the overflow checking option enabled (good practice to do so), but the super object code has been written under the assumption that the option is disabled. This is really a flaw in the super object code. You can solve it by disabling overflow checking in the super object code by adding {$OVERFLOWCHECKS OFF}. Ideally this would be disabled very locally for just the code that intentionally overflows. However, unless you fully understand the code it may just be easier to stuff {$OVERFLOWCHECKS OFF} at the top of the unit and move on.

Now, I'm looking at the very latest super object code and right at the top of the unit is {$OVERFLOWCHECKS OFF}. So I wonder if you are perhaps using an out of date version of the code. Pull the latest version from the repo.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • i m getting now 'integer overflow' error by adding PWideChar(WideString(fw)) where i m doing wrong ? – dudey Sep 20 '13 at 12:02
  • With the exact code in the question, and nothing else? And which line of code raises the exception? – David Heffernan Sep 20 '13 at 12:04
  • There is no msgbox in your code. It feels like you are asking me to fix code that I cannot see. I think I answered the question that you asked. – David Heffernan Sep 20 '13 at 12:25
  • 1
    its full code not hiding anything when i click button it just shows this msgbox http://img542.imageshack.us/img542/290/uqkg.png not show line which raise exception maybe this is not correct way to parse ? – dudey Sep 20 '13 at 12:32
  • Run the program under the debugger and configure the debugger to break on exceptions. Then tell us where the exception occurs. Until you can do basic debugging yourself, it's going to be exceptionally painful. – David Heffernan Sep 20 '13 at 12:35
  • ok thx it gives error on this line of unit superobject at function TSuperAvlEntry.Hash(const k: SOString): Cardinal; line : h := h*129 + ord(k[i]) + $9e370001; error : integer overflow – dudey Sep 20 '13 at 13:55
  • OK, that's a different question. But it looks to me like your compiler options have the **Overflow checking** option enabled. But the super object code is designed to be used with that option disabled. Check that is the case by temporarily disabling overflow checking in your compiler options. If that solves the problem then you'll want a better fix. Specifically to disable the option only in the super object code. Do that by placing `{$OVERFLOWCHECKS OFF}` at the top of the super object unit, or in the super object .inc file (if it has one). – David Heffernan Sep 20 '13 at 14:00
  • 1
    So, I really do think that I've answered the question. Is that so? – David Heffernan Sep 20 '13 at 14:53
  • no i still cannot solve it did both methods as u said i get same error i just want to parse that string (fw) and show data in memo can u suggest or show me example of doing in any other way ? really appreciate your help – dudey Sep 20 '13 at 15:09
  • 1
    I cannot debug what I cannot see. Stack Overflow is a place where you ask specific questions. I answered the question. How can you expect me to debug your code when I cannot see it? Anyway, I guarantee that the error you report is because `OVERFLOWCHECKS` is `ON`. If you would switch it off like I said then you'd solve that problem. I can see old versions of super object that did not `OVERFLOWCHECKS` to `OFF`. And the latest versions do. Did you pull the latest version from the repo like I said? Don't ask for another way to parse JSON, fix it this way! – David Heffernan Sep 20 '13 at 15:16
  • So you got to the bottom of it in the end? – David Heffernan Sep 20 '13 at 15:33
  • 1
    yes David your are right the problem was on my side its solved and sorry for my stupid questions :) – dudey Sep 20 '13 at 15:36
  • That's OK. I was quite sure of myself here. – David Heffernan Sep 20 '13 at 15:38
  • As far as I've seen there is only one section in the SuperObject code that requires Overflow checks off, that would be the question you report (I'm not at work now so I can't check). Use David's suggestion and if you want to be very nice about it you can conserve your compiler setting with the trick {$IFOPT O+}{$DEFINE OVCHECKWASON}{$ENDIF}{$O-}Relevant code{$IFDEF OVCHECKWASON}{$O+}{$ENDIF} – Jan Doggen Sep 21 '13 at 18:54
  • @JanDoggen Only very old versions of super object fail to set overflow checking. Getting the latest from the repo solves the issue. – David Heffernan Sep 21 '13 at 18:55
  • @David Not so. The current Oct 2012 version which I downloaded last August (http://stackoverflow.com/questions/18445280/superobject-cannot-handle-null-string) still has a few lines. – Jan Doggen Sep 21 '13 at 18:59
  • @JanDoggen Right at the top of superobject.pas from 38d45f284666 is this: `{$OVERFLOWCHECKS OFF}` – David Heffernan Sep 21 '13 at 19:05
2

I had the same problem using SuperObjects1.2.4 in delphi 7. As many people said, the solution is: to get the latest version. So, to get the lastest SuperObjects version from repository do: 1. Install git (http://git-scm.com/) 2. Right click in some Folder, choose 'Git bash' and paste the following

git clone https://code.google.com/r/steve-superobject/

Done!

Polux
  • 21
  • 1