-1

I am a little stuck here can anyone help please.

#include <iostream>
#include "include/rapidjson/document.h"
#include "include/rapidjson/writer.h"
#include "include/rapidjson/prettywriter.h"
//#include "include/rapidjson/stringbuffer.h"
using namespace std;
using namespace rapidjson;

class test {
 public:
static bool isEqual(const string &item1, const string &item2, const string &temp) {
    Document d1;
    d1.Parse(item1.c_str());
    Document d2;
    d2.Parse(item2.c_str());
    Document d3;
    d3.Parse(temp.c_str());
    bool a = true;
    bool b = isJsonEqual(d1, d2, d3, a);

}

static bool isJsonEqual(Value &v, Value &v1, Value &v2, bool &a) {
/*
    StringBuffer buffer;


    PrettyWriter<StringBuffer> writer(buffer);


    v.Accept(writer);
    //cout<<buffer.GetString()<<endl;
    StringBuffer b1;
    PrettyWriter<StringBuffer> writer1(b1);
    v1.Accept(writer1);
    //cout<<b1.GetString()<<endl;
    StringBuffer b2;
    PrettyWriter<StringBuffer> writer2(b2);
    v2.Accept(writer2);
     //cout<<b2.GetString()<<endl;
  */

    for (auto itr = v2.MemberBegin(); itr != v2.MemberEnd(); itr++) {
        if (itr->value.IsArray()) {
           StringBuffer b3;
            PrettyWriter<StringBuffer> writer3(b3);
            v2[itr->name].Accept(writer3);
            cout << b3.GetString() << endl;
            auto c = itr->name.GetString();

            cout << c << endl;

     //isJsonEqual(v[itr->name],v1[itr->name],v2[itr->name],a);
        } else if (v.HasMember(itr->name) && v1.HasMember(itr->name)) {
            // cout<<itr->name.GetString()<<endl;
            if ((v[itr->name]) != v1[itr->name]) {


                a = false;
                break;
            }
        }
    }

}


};

 int main() {
  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_array1\": [     {\"key\": 
 \"value1\" },     {\"key\": \"value2\" },     {\"key\": \"value3\" }    
 ]},    {\"object_array2\": [     {\"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\": [     {\"whatever\": \"test\", 
 \"key\": \"value1\" },     {\"key\": \"value2\" },     {\"key\": 
 \"value3\" }    ],    \"deep_nested_array\": [        
 {\"object_array1\": [     {\"key\": \"value1\" },     {\"key\": 
 \"value2\" },     {\"key\": \"value3\" }    ]},    
 {\"object_array2\": [     {\"key\": \"value4\" },     {\"key\": 
 \"value5\" },     {\"key\": \"value6\", \"ignoreme\": 12346 }    ]}      
  ],  \"string\": \"Hello World\"}";
  const char *temp = "{  \"array\": [    null  ],  \"boolean\": null,  
 \"null\": null,  \"object\": {    \"a\": null,    \"c\": null,    
 \"e\": null  },  \"number\": null,  \"object_array\": [     {\"key\": 
 null }    ],    \"deep_nested_array\": [        {\"object_array1\": [     
 {\"key\": null },     {\"key\": null },     {\"key\": null }    ]}      
 ],  \"string\": null}";

bool a = test::isEqual(input1, input2, temp);
if (a) {
    cout << "True";
    //std::cout << "Verify again" << std::endl;
} else {
    cout << "check again";
}
       }

//isJsonEqual(v[itr->name],v1[itr->name],v2[itr->name],a); The problem lies here after finding the key is an array I want to use recursion to go deep into nested array and iterate in it(ie after I encounter its an array I want to recurse the entire array as a parsed json to loop inside and compare each keys. Or can we create a new json for the arrays in the existing json and recurse the value to check it?please need some ideas with this.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
Bishal Sahoo
  • 71
  • 2
  • 11
  • Unclear what your method `isEqual` should do. `temp` suggests temporary but it isn't... `ref` seems to be a better name. – Jarod42 Apr 20 '18 at 09:19
  • the temp is a json schema based on which the comparison of input1 and input2 will be done .isEqual() will be used to compare the string ,but the comparison will be done in the isJsonEqual() as I want to use recursion to compare deeper nested array – Bishal Sahoo Apr 20 '18 at 09:27

1 Answers1

0

As I understand, the code should be something like:

bool isJsonEqual(const Json::Value &reference, Json::Value &v1, Json::Value &v2)
{
    switch (reference.GetType()) {
        case Json::Value::booleanValue: {
             if (reference.asBool()) {
                 return v1 == v2;
             } else {
                 return true; // don't check comparison
             }
        }
        case Json::Value::nullValue : return true; // don't check comparison
        case Json::Value::arrayValue: {
            if (v1.GetType() != Json::Value::arrayValue
             || v2.GetType() != Json::Value::arrayValue) {
                 return false;
            }
            const auto size = reference.ArraySize();
            if (v1.ArraySize() != size || v2.ArraySize() != size) {
                 return false;
            }
            // Recurse on array
            for (std::size_t i = 0; i != size; ++i) {
                 if (!isJsonEqual(reference[i], v1[i], v2[i])) {
                     return false;
                 }
            }
            return true;
        }
        case Json::Value::objectValue: {
            if (v1.GetType() != Json::Value::objectValue
             || v2.GetType() != Json::Value::objectValue) {
                 return false;
            }
            // Recurse on members
            for (const auto& member : reference.getMemberNames()) {
                 if (!isJsonEqual(reference[member], v1[member], v2[member])) {
                     return false;
                 }
            }
            return true;
        }
        default: {
            throw std::runtime_error("Invalid reference");
        }
    }
}
user2807083
  • 2,962
  • 4
  • 29
  • 37
Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • Is this using jsoncpp? – Bishal Sahoo Apr 21 '18 at 14:32
  • Yes. I didn't compile code, so there might be some errors/typos. – Jarod42 Apr 21 '18 at 14:41
  • Yes using jsoncpp I am getting errors , but the logic looks good and hopefully it will work(just want to compare input1 and input2 based on schema or reference json , was able to do it but was unable to get the keys those are nested that was the bug the logic will fail if some deeper array keys are missing in referce json but are not required it will say the input1 and input2 are not equal but according to the logic it should be) . I can make it work on rapidjson hopefully if they have the functionality like jsoncpp.Thank you. – Bishal Sahoo Apr 21 '18 at 14:48
  • It's comparing the type but I want to get the inner keys and iterate to check their values – Bishal Sahoo Apr 22 '18 at 03:51