bourne
bourne

Reputation: 1243

Json util method returning invalid result

i need help understanding what i am missing in the logic for my json utility method that is returning an array of elements. I am not that familiar with rapidjson and can only add debug prints in my code. So my json.h file looks like below

template <>
std::vector<std::string> fetchStringArray<std::vector<std::string>>(const rapidjson::Value& value){
    std::cout<<"Enter\n";
    std::vector<std::string> elements;
    if (!value.IsArray()) {
        std::cout<<"Early Exit\n";
        return elements;
    }

    for (auto& item : value.GetArray()) {
        if (item.IsString()) {
            elements.push_back(item.GetString());
        }
    }
    std::cout<<"Success\n";
    return elements;
}

template <>
vector<string> fetchStringArray<vector<string>>(const string& jsonString, const string& key){
       std::cout<<"2\n";
       rapidjson::Document document;
       std::cout <<"JsonString "<<jsonString<<"\n";
       document.Parse(jsonString);
       if(document.HasMember("key")){
           std::cout <<"key found "<<"\n";
       }
    
       if (document.HasParseError()) {
           return std::vector<string>();
       }
    
     return fetchStringArray<vector<string>>(document);
}

template <class CollectionT>  
CollectionT fetchStringArray(const string& jsonString, const string& key) {
     std::cout<<"4\n";
     auto values = fetchStringArray<std::vector<std::string>>(jsonString, key);
     return CollectionT{values.begin(), values.end()};
}

The jsontests.cpp test file looks like below

/// test fixture.
class JsonTests : public ::testing::Test {};

/**
 * Test fetchStringArray
 */
TEST_F(JsonTests, test_fetchStringArray) {
    char key[] = "key";
    auto entries = fetchStringArray<std::set<std::string>>(R"({"key":["ONE","TWO"]})", key);
    ASSERT_EQ(entries.size(), 2u);
    EXPECT_NE(entries.find("ONE"), entries.end());
    EXPECT_NE(entries.find("TWO"), entries.end());
}

But when i run the test i get thE FOLLOWING OUTPUT

4
2
JsonString {"key":["ONE","TWO"]}
key found 
Enter
Early Exit
/Foo/test/JsonTestS.cpp:23: Failure
      Expected: entries.size()
      Which is: 0
To be equal to: 2u
      Which is: 2
[  FAILED  ] JsonTests.test_fetchStringArray (0 ms)
    

Any help or pointers will be greatly appreciated.

Upvotes: 0

Views: 45

Answers (0)

Related Questions