Poco C++ building nested JSON objects Poco C++ building nested JSON objects json json

Poco C++ building nested JSON objects


Poco::JSON Objects and Arrays are held as shared pointers internally by default (optimization to avoid values copying) and everything is Dynamic::Var, so it works for both pointers and values. When you insert an Object as value it works because Dynamic::Var will hold pretty much anything, but the problem you experience when inspecting it comes from the fact that internal comparison does not return true for Object values because it compares only with default type - Poco::SharedPtr<Poco::JSON::Object>.

Here's a workaround:

void build_object (Poco::JSON::Object * const result){    // smart pointer, so don't worry about cleaning up    Poco::JSON::Object::Ptr inner = new Poco::JSON::Object;    inner->set("some_number", 5);    inner->set("some_string", "xyz");    std::string key = "new_object";    result->set(key, inner);    printf("isObject: %i\n", result->isObject(key)); // true}

I have opened a github issue to alleviate this caveat.


I have been trying to create json file having nested object using poco library. Finally able to do with Poco::Json::Array.

Please find the posted code sinippet. Hope it will help. Json output attached with post.

#include "Poco\JSON\JSON.h"#include "Poco\JSON\Stringifier.h"#include "Poco\JSON\Object.h"#include "Poco\Dynamic\Var.h"using namespace std;using Poco::JSON::Stringifier;using Poco::JSON::Object;using Poco::JSON::Array;        void makeJsonNestedObject()    {        Object RootObj(true);        Array FLArray;        for(int i=0; i<3; i++)        {                   Object::Ptr FirstLevelArrayNode = new Poco::JSON::Object(true);            TCHAR strNameBuff[15];            _stprintf(strNameBuff, _T("%s_%d"),_T("Servername"),i);            std::basic_string<TCHAR> strName = strNameBuff;            FirstLevelArrayNode->set("HostName", strName);            FirstLevelArrayNode->set("Overall Impact", "Dummy Data");            Array SLArray;            for(int j=0; j<3;j++)            {                Object::Ptr SecondLevelArrayNode = new Poco::JSON::Object(true);                TCHAR attr1NameBuff[15];                TCHAR attr2NameBuff[15];                _stprintf(attr1NameBuff, _T("%s_%d"),_T("AttrOne"),j);                _stprintf(attr2NameBuff, _T("%s_%d"),_T("AttrTwo"),j);                std::basic_string<TCHAR> attr1Name = attr1NameBuff;                std::basic_string<TCHAR> attr2Name = attr2NameBuff;                SecondLevelArrayNode->set("Attribute", attr1Name);                SecondLevelArrayNode->set("SubAttribute", attr2Name);                Poco::Dynamic::Var obj(SecondLevelArrayNode);                SLArray.add(obj);            }               FirstLevelArrayNode->set("Attribute_Details",SLArray);            Poco::Dynamic::Var FLArrayNodeobj(FirstLevelArrayNode);            FLArray.add(FLArrayNodeobj);        }        std::ostringstream os;        std::cout <<"before stringlify.." << std::endl;        FLArray.stringify(os, 2);        std::cout << os.str() << std::endl;    }

Json output:

    [  {    "HostName" : "Servername_0",    "Overall Impact" : "Dummy Data",    "Attribute_Details" : [      {        "Attribute" : "AttrOne_0",        "SubAttribute" : "AttrTwo_0"      },      {        "Attribute" : "AttrOne_1",        "SubAttribute" : "AttrTwo_1"      },      {        "Attribute" : "AttrOne_2",        "SubAttribute" : "AttrTwo_2"      }    ]  },  {    "HostName" : "Servername_1",    "Overall Impact" : "Dummy Data",    "Attribute_Details" : [      {        "Attribute" : "AttrOne_0",        "SubAttribute" : "AttrTwo_0"      },      {        "Attribute" : "AttrOne_1",        "SubAttribute" : "AttrTwo_1"      },      {        "Attribute" : "AttrOne_2",        "SubAttribute" : "AttrTwo_2"      }    ]  },  {    "HostName" : "Servername_2",    "Overall Impact" : "Dummy Data",    "Attribute_Details" : [      {        "Attribute" : "AttrOne_0",        "SubAttribute" : "AttrTwo_0"      },      {        "Attribute" : "AttrOne_1",        "SubAttribute" : "AttrTwo_1"      },      {        "Attribute" : "AttrOne_2",        "SubAttribute" : "AttrTwo_2"      }    ]  }]