sitemule / noxDB

Not only XML. SQL,JSON and XML made easy for IBM i
MIT License
41 stars 20 forks source link

Deep JSON copy example doesn't copy? #66

Open Helaas opened 2 years ago

Helaas commented 2 years ago

Hello,

Firstly, thanks again for the xml_Locate change from last week.

I have a issue/question about the JSON copy example. Is my understanding correct that this code should duplicate the entire content of the from object to the destination objects?

// copy all elements: This is deep copy json_copyValue ( pTo : '' : pFrom : '');

Because when I try this, my Content2 variable is empty in the firsty attempt, however, when I use mergeObject, it works just fine.

pFrom = json_parseString ('{ -
     "s" : "Life is a gift",   -
     "a" : [1,2,3,4],          -
     "o" : {                   -
        "x"  : 100,            -
        "y"  : 200             -
     }                         -
  }');

// "to" is just an empty object at begining
pTo = json_newObject();
pTo2 = json_newObject();

// copy all elements: This is deep copy
json_copyValue ( pTo  : '' : pFrom : '');

Content = json_AsJsonText(pFrom);
Content2 = json_AsJsonText(pTo); //This will be empty!

// copy all elements: This is deep copy
//json_NodeCopy(pTo2:pFrom:json_FIRST_CHILD);
json_MergeObjects(pTo2:pFrom:json_REPLACE);

Content = json_AsJsonText(pFrom);
Content2 = json_AsJsonText(pTo2); //This will contain the same as Content

json_delete(pTo);
json_delete(pTo2);
json_delete(pFrom);
*inlr = *on;     

What is the preferred way to copy a whole node to an other node?

NielsLiisberg commented 2 years ago

First - please notice that "MergeObjects" it the one to use. The older "NodeMerge" is deprecated, but have strang behaviour - but is used and cannot be removed.

next: json_copyValue is - as implied - values only

Let me just come back with an example of a deep merge..