0

I am constructing json from tree data, but when i add node_level_3 from node_level_2 after adding node_level_2 from node_level_1, node_level_2 does not has information abour node_level_3. Here is my code.

node_level_1 = new Json::Value();
(*node_level_1)["data"] = first_value;

if (some_other_string != "")
{
    node_level_2 = new Json::Value();

    (*node_level_2)["data"] = some_other_string ;
    (*node_level_1)["child"].append(*node_level_2);
}

if (another_string!= "")
{
    node_level_3 = new Json::Value();

    (*node_level_3) ["data"] = another_string;
    (*node_level_2) ["child"].append(*node_level_3 );
}

I guess the problem is that 'Json::Value.append() function' only copy its data, not pointer or reference. So If i change data of node_level_2, it does not affect previously added node_level_2.

How can i solve this problem?? Should i have to traverse all the bottom nodes(level #3) of tree, and construct parent tree node (level #2) and finally add all the parent to root node(level #1)? Is this only solution With JsonCpp ?

Knowledge Drilling
  • 986
  • 1
  • 8
  • 22
  • Why do you use so much dynamic allocation and pointers? And did you expect that dereferencing one would result in a reference? It is not clear what you're trying to do. – Lightness Races in Orbit Jan 02 '19 at 17:23
  • I was trying to convert excel-like data(multiple rows & cols) to json format. I had to add each col's data in a row into each json level. So i don't know how much parent json node will add child node(at the same col level). – Knowledge Drilling Jan 03 '19 at 11:05
  • Anyway i implemented tree traversing mechanism from the bottom colum to root colum. So i don't need to change parent node anymore after adding child node. – Knowledge Drilling Jan 03 '19 at 11:10
  • You neither need nor want any of this dynamic allocation. You're leaking memory like a sieve! – Lightness Races in Orbit Jan 03 '19 at 11:17
  • Maybe i should delete all the appended data from Json tree after using it. Like deleting from child node to parent node, like recursively. – Knowledge Drilling Jan 04 '19 at 14:34
  • You can't. The tree copies your data and owns the copies. You have leaked the memory you `new`'d and can never get it back. Just don't use `new`. – Lightness Races in Orbit Jan 04 '19 at 15:16

0 Answers0