简体   繁体   English

Poco :: JSON :: Object的深层副本

[英]Deep copy of Poco::JSON::Object

I'm trying to create deep copy of Poco::JSON::Object . 我正在尝试创建Poco::JSON::Object深层副本。

Internally Poco::JSON::Object uses pointers and copy constructor just copy those pointers. 内部Poco::JSON::Object使用指针,而复制构造函数仅复制这些指针。 Is there a clever solution how to copy this structure using Poco framework? 是否有一个聪明的解决方案,如何使用Poco框架复制此结构?

I don't know of any generic solution, but for small JSON objects where performance is not critical the following function does the job well enough: 我不知道任何通用的解决方案,但是对于性能不重要的小型JSON对象,以下功能可以很好地完成工作:

Poco::JSON::Object::Ptr cloneJsonObject(Poco::JSON::Object::Ptr obj) {
    if (obj) {
        std::stringstream streamedObj;
        obj->stringify(streamedObj);
        Poco::JSON::Parser parser;
        return parser.parse(streamedObj).extract<Poco::JSON::Object::Ptr>();
    } else {
        return nullptr;
    }
}

could not find good documentation. 找不到好的文档。 so here is another way of copying. 所以这是另一种复制方式。 you can check if the key and value need further deeper copy like using c_str(). 您可以检查键和值是否需要更深的副本,例如使用c_str()。 for me this works fine. 对我来说,这很好。

Poco::JSON::Object::Ptr cloneJsonObject(Poco::JSON::Object::Ptr obj)
{
    Poco::JSON::Object::Iterator it;
    Poco::JSON::Object *ptr = new Poco::JSON::Object;
    // loop and copy
    for(it = obj->begin(); it != obj->end(); it++)
        ptr->set(it->first, it->second);

    return ptr;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM