Make a copy of the Document rapidjson object

I am making a class and I want to return my class inside the method. My class has a rapidjson::Document object.

You can see previous issues here: LNK2019: "Unresolved external character" with rapidjson

As I discovered, quickjson prevents you from executing any copy of the Document object, and then the default copy of the class containing the Document object failed. I am trying to define my own copy constructor, but I need to execute a copy of the object. I saw a way to hypothetically copy an object using the .Accept() method, but it returns me a lot of errors inside the rapidjson::Document class:

error C2248: 'cannot access the private member declared in the class `rapidjson :: GenericDocument'

This is my copy constructor:

 jsonObj::jsonObj(jsonObj& other) { jsonStr = other.jsonStr; message = other.message; //doc = other.doc; doc.Accept(other.doc); validMsg = other.validMsg; } 

I found in the library code (line 52-54) that " Copy constructor is not permitted ".

This is my class:

 class jsonObj { string jsonStr; Document doc; public: jsonObj(string json); jsonObj(jsonObj& other); string getJsonStr(); }; 

Method:

 jsonObj testOBJ() { string json = "{error:null, message:None, errorMessage:MoreNone}"; jsonObj b(json); return b; //It fails here if I return a nested class with a rapidjson::Document in it. Returning NULL works } 

So how to make a copy of the Document element?

+4
source share
4 answers

Repository https://github.com/rjeczalik/rapidjson DeepCopy Patch , which can help you copy one document to another.

+1
source

Use the CopyFrom method for a new document:

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

I tested this approach, and the changes made to the output did not affect the input. Make sure the CopyFrom method is assigned an output separator.

+8
source

I created this method to copy a 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
source

should use the (const) link as the return type (try saving new documents in the creator's class), you cannot copy documents, that is, you cannot return by value, since you are implicitly trying to use the disabled copy constructor

0
source

Source: https://habr.com/ru/post/1443706/


All Articles