Reputation: 2807
I have two json strings parsed by Rapidjson library.
Json 1:
{
"jKey1":{
"jVal1Key1":{
"mystr":["str1","str2"]
},
"jVal2Key2":["data1","data2"]
},
"jKey2":"Title"
}
Json 2:
{
"jVal1Key1":{
"mystr":["str1","str2"]
}
}
I just want to replace "jVal1Key1" of Json 1 with "jVal1Key1" of Json 2.
So I tried the following code.
Document doc1;
doc1.Parse<0>(json1.c_str()).HasParseError();
doc1.Parse(json1.c_str());
Document doc2;
doc2.Parse<0>(json2.c_str()).HasParseError();
doc2.Parse(json2.c_str());
if(doc1.HasMember("jKey1"))
{
if(doc1["jKey1"].HasMember("jVal1Key1"))
{
if(doc2.HasMember("jVal1Key1"))
{
doc1["jKey1"]["jVal1Key1"] = doc2["jVal1Key1"]; // I failed here
}
}
}
In my program, this below line,
doc1["jKey1"]["jVal1Key1"] = doc2["jVal1Key1"]; // I failed here
compiled successfully. But It fails at runtime. My question is, How can I copy the value of that key 'jVal1Key1' of 'doc2' to 'doc1'.
Upvotes: 1
Views: 3166
Reputation: 2121
According to the RapidJson Documentation, you can do a deep copy of the DOM Tree with CopyFrom
. Another option is to swap the values with Swap which is faster if speed is an issue:
Sample Code:
doc1["jKey1"]["jVal1Key1"].CopyFrom(doc2["jVal1Key1"], doc2.GetAllocator());
What I think you are doing right now is moving the value from doc2 to doc1. Look at the Move Semantics section for more information to see if that could cause the error you are seeing.
Upvotes: 4