Perform a copy of Document object of rapidjson

后端 未结 4 592
小鲜肉
小鲜肉 2020-12-20 20:32

I\'m making a class and I want to return my class inside a method. My class has a rapidjson::Document object.

You can see the previous problems here: L

相关标签:
4条回答
  • 2020-12-20 20:35

    Repository https://github.com/rjeczalik/rapidjson have the DeepCopy patch which might help you copy one document into another.

    0 讨论(0)
  • 2020-12-20 20:36

    must use (const) reference as return type(try to store new documents in creator class), you can't copy documents, i.e. can't return by value, since implicitly you try to use disabled copy constructor

    0 讨论(0)
  • 2020-12-20 20:49

    I created this method to copy document object and it works fine for me:

    static void copyDocument(rapidjson::Document & newDocument, rapidjson::Document & copiedDocument) {
        rapidjson::StringBuffer strbuf;
        rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
        newDocument.Accept(writer);
        std::string str = strbuf.GetString();
        copiedDocument.Parse<0>(str.c_str());
    }
    
    0 讨论(0)
  • 2020-12-20 21:01

    Use the CopyFrom method on a new Document:

    rapidjson::Document inDoc;    // source document
    rapidjson::Document outDoc;   // destination document
    outDoc.CopyFrom(inDoc, outDoc.GetAllocator());
    

    I tested this approach and changes made to the output document had no effect on the input document. Make sure the CopyFrom method is given the output document's allocator.

    0 讨论(0)
提交回复
热议问题