Search code examples
c++jsonjsoncpp

jsoncpp compilation error for Json::Value type


This is the schema:

{
"definitions": {
    "properties": {
        "Count": {
            "type": [
                "number",
                "null"
            ]
        }
    }
}

} I want to read members in "type"

I tried to attempt in many ways, e.g.

if(val["definitions"]["properties"]["Count"]["type"][0] == "number" and 
(val["definitions"]["properties"]["Count"]["type"][1] == "null"))
{
    //code here
}

This leads to the following error

error : terminate called after throwing an instance of 'Json::LogicError' what(): in Json::Value::operator: requires arrayValue Aborted (core dumped)

And for this piece of code

if (val["definitions"]["properties"]["Count"]["type"][0].isMember("number") and 
(val["definitions"]["properties"]["Count"]["type"][0].isMember("null"))){
    //code here
}

I get

error: terminate called after throwing an instance of 'Json::LogicError' what(): in Json::Value::find(key, end, found): requires objectValue or nullValue Aborted (core dumped)


Solution

  •  const Json::Value obj=val["definitions"]["properties"]["count"]["type"];
        if (std::find(obj.begin(),obj.end(),"string")!=obj.end() and 
     std::find(obj.begin(),obj.end(),"null")!=obj.end()){
        // code here;
        }