Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

order of creating nodes

Anonymous
2012-11-07
2013-06-12

  • Anonymous
    2012-11-07

    found a problem with the libjson, creating a tree of nodes.. For example:

    json nodes a,b,c,d,e

    a, b, c == {} (empty nodes)
    d == "test" : "me"
    e == "foo" : true

    The order that these are constructed matter, and not sure why.. these two examples should result in the same json, but they do not:

    a.push_back(b);
    b.push_back(c);
    c.push_back(d);
    c.push_back(e);
    

    This order results in json that is incorrect, missing nodes c, d and e:

    {
       "b":{}
    }
    

    Constructing these in reverse however does work:

    c.push_back(e);
    c.push_back(d);
    b.push_back(c);
    a.push_back(b);
    

    resulting json, which is correct:

    {
       "b":{
          "c":{
             "foo":true,
             "test":"me"
          }
       }
    }
    

    Not sure what's happening here, but should be fixed..

     
  • a.push_back(b);
    b.push_back(c);
    c.push_back(d);
    c.push_back(e);

    You push c onto b, then you alter c, of course it isn't going to work that way.  push_back, like with every other container, is a copy function, create c, then push it.

     

  • Anonymous
    2013-01-25

    sorry for the late followup..

    If this is the case, how can you add something to a node that is deeper than the root in a tree?? If its a copy, then you can't modify it after it has been added, then there's no way to add nodes deeper than the root node..

    In other words, how would you add new nodes to an already created tree, if push_back (and the  or at() are creating copies of the nodes in the tree??

    given this example,

    {
       "case": {
          "whiskey": {
             "isgood": true,
          }
       }
    }
    

    say I wanted to add a string node "name":"makersmark" under the whiskey node, how would that be done?

     

  • Anonymous
    2013-01-25

    Nevermind.. Instead of using at() and push_back() to insert the node, using JSONNode::iterator and insert() I'm able to add the node to the correct parent..