Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create empty json array with jsoncpp

Tags:

I have the following code:

void MyClass::myMethod(Json::Value& jsonValue_ref)
{
    for (int i = 0; i <= m_stringList.size(); i++)
    {
        if (m_boolMarkerList[i])
        {
            jsonValue_ref.append(stringList[i]);
        }
    }
}

void MyClass::myOuterMethod()
{
    Json::Value jsonRoot;
    Json::Value jsonValue;
    
    myMethod(jsonValue);

    jsonRoot["somevalue"] = jsonValue;
    Json::StyledWriter writer;
    std::string out_string = writer.write(jsonRoot);
}
    

If all markers in m_boolMarkerList are false, the out_string is { "somevalue" : null }, but I want it to be an empty array: { "somevalue" : [ ] }

Does anybody know how to achieve this?

Thank you very much!

like image 551
Martin Meeser Avatar asked Nov 08 '12 16:11

Martin Meeser


1 Answers

Here are two ways you can do it:

jsonRootValue["emptyArray"] = Json::Value(Json::arrayValue);
// or 
jsonRootValue["emptyArray"] = Json::arrayValue;
like image 110
Michal Sabo Avatar answered Sep 20 '22 13:09

Michal Sabo