c++ Poco::JSON::Array error in method stringify (Can not convert to std::string)

15 Views Asked by At

I need help with my code. I need to pack a list of objects into Poco::JSON::Object. Here's some class

struct ProcessInfo {
        std::string name;
        int pid;
        std::string path;

        std::string toString() {
            std::string str;
            str += fmt::format("Name: {0}\n PID: {1}", name, pid);
            return str;
        }
    };

in the method I have a list of objects like this:

std::list<ProcessInfo> list

I have a method in a class where I want to serialize my object to Poco::JSON::Object this is how I do it:

Poco::JSON::Object* ProcessTask::serializeList(std::list<ProcessInfo> list)
    {
        Poco::JSON::Array* array = new Poco::JSON::Array();

        for (auto item: list)
            array->add(this->serializeItem(item));

        std::stringstream ss;
        array->stringify(ss);** //This is where I get the error!**
        std::cout<< "serialize to array process list: " << ss.str() << std::endl;

        Poco::JSON::Object* data = new Poco::JSON::Object();
        data->set("processList", array);
        data->stringify(ss);
        std::cout<< "object process list: " << ss.str() << std::endl;
        return data;
    }

    Poco::JSON::Object* ProcessTask::serializeItem(ProcessInfo process)
    {
        Poco::JSON::Object* data = new Poco::JSON::Object();
        data->set("name", process.name);
        data->set("pid", process.pid);
        return data;
    }

On the line of code array->stringify(ss); I get a runtime exception: **Can not convert to std::string**

please tell me how can I fix my code? Perhaps there is a more convenient way to give std::list<ProcessInfo> to Poco::JSON::Object* ? I will be glad if they show me this way!

0

There are 0 best solutions below