Home > Back-end >  JsonCPP throwing a logic error:requires objectValue or nullValue
JsonCPP throwing a logic error:requires objectValue or nullValue

Time:10-03

void exp::example(std::string &a, std::string &b)
{   
    if (m_root.isObject() && m_root.isMember(a))
    {
        if (m_root[a].isMember(b))
        {
            m_root[a].append(b);
        }

    }
    else
    {  
        m_root[a] = Json::arrayValue;
        m_root[a].append(b);

    }
}

(m_root is defind in the hpp)

When I'm running this code I get the logic error: in Json::Value::find(key, end, found): requires objectValue or nullValue. I found out that I got this error from this if: if (m_root[a].isMember(b))

I don't understand why do I get this error there, I used the same function in the if above him and I didn't get this error.

P.S the function is working until it enter the nested if, example:

a b m_root
"hey" "a1" {"hey":["a1"]}
"bye" "a2" {"hey":["a1"], "bye":["b1"]}
"cye" "a3" {"hey":["a1"], "bye":["b1"], "cye":["a3"]}
"hey" "a4" error: in Json::Value::find(key, end, found): requires objectValue or nullValue

I get the error only in the 4th call. I appreciate your help!

CodePudding user response:

On the 4th iteration you access the m_root["hey"] object which is of type arrayValue. Those values are not supported by the isMember method.

You'll have to find the value in the array in another way. I suggest iterating over the array, in something like:

bool is_inside_array(const Json::Value &json_array, const string& value_to_find) 
{
      for (const Json::Value& array_value: json_array) {
           if (array_value.asString() == value_to_find) {
                return true;
           }
      }
      return false;
}

Then replace the inner if with:

if (is_inside_array(m_root[a], b))
  • Related