3

When creating a SuperObject from a string, it might happen that the string is no valid JSon. Unfortunately the command SO doesn't raise an exception in that case. I end up with a object where I THINK I can store data in, but the "stored" data goes nowhere and is lost.

example:

 MySo:=SO('{}');
 MySO.S['ok']:='test';
 Memo1.Lines.Add(MySO.AsJSon(True, False));

 MySo:=SO('');
 MySO.S['fail']:='mimimi';
 Memo1.Lines.Add(MySO.AsJSon(True, False));  // returns '""' ??!??

How can I check if the string was converted successfully into a valid and working SuperObject?

ralfiii
  • 584
  • 4
  • 13
  • 1
    Use `Validate`: https://github.com/hgourvest/superobject/blob/master/tests/test_validate.dpr – whosrdaddy Jul 24 '18 at 09:14
  • What would be a proper syntax for validate? In the sample even the string "5" is a valid JSon. But if I create a JSon that way, I can't store anything in there. That's my problem described above. – ralfiii Jul 25 '18 at 10:19

1 Answers1

1

whorsyourdaddy's comment pointed into the right direction.

To be able to store INTO a JSon you need a stObject. You can check for that this way:

 if not MySo.IsType(stObject) then raise....
ralfiii
  • 584
  • 4
  • 13
  • In case of an invalid json the result is nil! This test results then in an access violation. `if (MySo= nil) or (not MySo.IsType(stObject)) then` is the way to go – mrabat Jul 19 '23 at 08:13