Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deep JSON copy example doesn't copy? #66

Open
Helaas opened this issue Apr 5, 2022 · 1 comment
Open

Deep JSON copy example doesn't copy? #66

Helaas opened this issue Apr 5, 2022 · 1 comment

Comments

@Helaas
Copy link

Helaas commented Apr 5, 2022

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
Copy link
Collaborator

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..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants