hydrobyte / McJSON

A Delphi / Lazarus / C++Builder simple and small class for fast JSON parsing.
MIT License
58 stars 20 forks source link

How check path exist by not throw error #23

Closed baxing closed 6 months ago

baxing commented 6 months ago

How to check nill of specific paht which throw to show error? such I want to check N.Path('o.k2') or N['o']['k2'] = nil ?

but now if path not exist McJSON show error automatically, but I don't want that.

I just want to be able to check for myself whether it's there or not. Even if there is, I will proceed with one thing. But if you don't have it, don't do another thing, for example.

if N['o']['k2']  = nil then
 // do something
else
 // do something else ;

or

if N['o']['k2'].ItemType  = jitNone then
 // do something
else
 // do something else ;

Note : for JsonDataObjects can do this

if N['o']['k2'].Typ = jdtNone then
 // do something
else
 // do something else ;

Please guide me for this Thank you

hydrobyte commented 6 months ago

Hi,

I need to improve McJSON to meet this demand. Thanks for getting in touch, I'll work on it.

hydrobyte commented 6 months ago

Hi, just committed a new version in order to manage nonexistent paths. Could you try it and report here?

baxing commented 6 months ago

Hi,

I'am not sure that should which method to check the existence of the path. So I test by

      if N.Path('o.k2') = nil then
       // do something
       ShowMessage('nil')
      else
       // do something else ;
       ShowMessage('ok');

but It still show error message that : Object reference is nil: has key "k2" It seems that Method : HasKey that is called within Method : Path is displaying this error.

or Did I do something wrong? Please advise.

Thank you

hydrobyte commented 6 months ago

Hi,

Today's commit has a new Test20 that tests this and it is working here with Lazarus/FPC. Could you check with Delphi? I'm not in my office today:

N.AsJSON := '{"o": {"k1":"v1", "k2":"v2"}}';
// get second object using path of keys.
// ...
M := N.Path('o.k3');
Result := Result and (M = nil);
baxing commented 6 months ago

Thank you for your quick reply. But if it's convenient for you or it's during your working hours, please help answer my questions here.

      N := TMcJsonItem.Create();
      N.AsJSON := '{"o": {"k1":"v1", "k2":"v2"}}';
      M := N.Path('o.k3'); //-- One
//      M := N.Path('p');  //-- Two
//      M := N.Path('ppp'); //-- Three
//      M := N.Path('ppp.k3'); //-- Four
      if M = nil then
       // do something
       ShowMessage('nil')
      else
       // do something else ;
       ShowMessage('ok');

Result for above statement :

I think case "Two" and "Four" incorrect

Thank

hydrobyte commented 6 months ago

Hi, You're right. I'll work on it.

hydrobyte commented 6 months ago

Please, test last commit and report here.

baxing commented 6 months ago

@hydrobyte Thank very much, For now I think it's great to be able to check the existence of a path via method Path. 👍

And if you have another method that gives the same result, please recommend me.

hydrobyte commented 6 months ago

@baxing, thanks for testing and reporting here. I think Path() returning nil is good enough to test invalid object paths.