I am working on a c++ project where I need to compare two or more Json string that will be passed to me as arguments in a function and i have to return a bool accordingly. I am using Jsoncpp but I am unable to compare the entirety of the two Json datas. I want to know the best procedure to loop in the key and value and check the value with corresponding value of another json string (both String will be passed to the function and will be parsed using reader.parse() of jsoncpp and then i need to compare them both and return the bool value). Can anyone help me with this please? thank you in advance. The place where I am stuck:
class test {
public:
static bool isequalstring(const std::string &item1, const std::string
&item2, const std::string &temp) {
Document d1;
d1.Parse(item1.c_str());
Document d2;
d2.Parse(item2.c_str());
Document d3;
d3.Parse(temp.c_str());
bool matched = true;
//itr= iterate through the third json to get the keys and match the keys in first and second
for (auto itr = d3.MemberBegin(); itr != d3.MemberEnd(); itr++) {
if (d1.HasMember(itr->name) && d2.HasMember(itr->name)) { // if the member doesn't exist in both, break
if (d1[itr->name] != d2[itr->name]) {
// value doesn't match, then break
matched = false;
break;
}
} else {
matched = false;
break;
}
}
return matched;
}
};
bool testDeepNestedJson_should_succeed(){
bool expectedTestResult = true;
bool testResult;
// Input 1 JSON Object
const char* input1 = "{\"array\":[1,2,3],\"boolean\":true,\"null\":null,\"number\":123,\"object\":{\"a\":\"b\",\"c\":\"d\",\"e\":\"f\"},\"string\":\"Hello World\",\"object_array\":[{\"key\":\"value1\"},{\"key\":\"value2\"},{\"key\":\"value3\"}],\"deep_nested_array\":[{\"object_array\":[{\"key\":\"value1\"},{\"key\":\"value2\"},{\"key\":\"value3\"}]},{\"object_array\":[{\"key\":\"value4\"},{\"key\":\"value5\"},{\"key\":\"value6\"}]}]}";
const char* input2 = "{\"array\":[1,2,3],\"justsomedata\":true,\"boolean\":true,\"null\":null,\"object\":{\"a\":\"b\",\"c\":\"d\",\"e\":\"f\"},\"number\":123,\"object_array\":[{\"key\":\"value1\"},{\"key\":\"value2\"},{\"key\":\"value3\"}],\"deep_nested_array\":[{\"object_array\":[{\"key\":\"value1\"},{\"key\":\"value2\"},{\"key\":\"value3\"}]},{\"object_array\":[{\"key\":\"value4\"},{\"key\":\"value5\"},{\"key\":\"value6\",\"ignoreme\":12346}]}],\"string\":\"Hello World\"}";
const char* stencil = "{\"array\":[null],\"boolean\":null,\"null\":null,\"object\":{\"a\":null,\"c\":null,\"e\":null},\"number\":null,\"object_array\":[{\"key\":null}],\"deep_nested_array\":[{\"object_array\":[{\"key\":null}]}],\"string\":null}";
testResult = test::isequalstring(input1, input2, stencil);
if(testResult != expectedTestResult){
std::cout<<"testDeepNestedJson_should_succeed:"<<std::endl;
std::cout<<"Item1:"<<input1<<std::endl;
std::cout<<"Item2:"<<input2<<std::endl;
std::cout<<"Stencil:"<<stencil<<std::endl;
std::cout<<"Test Failed result is: False expected was: True"<<std::endl;
return false;
}
std::cout<<"PASSED: testDeepNestedJson_should_succeed"<<std::endl;
return true;
}
int main() {
testDeepNestedJson_should_succeed();
return 0;
}