user2741831
user2741831

Reputation: 2398

how do I copy a rapidjson::value?

I'm trying to copy a rapidjson::value into a class member.

error: ‘rapidjson::GenericValue<Encoding, <template-parameter-1-2> >::GenericValue(const rapidjson::GenericValue<Encoding, <template-parameter-1-2> >&) [with Encoding = rapidjson::UTF8<char>; Allocator = rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>]’ is private

simply for execution the folowing line:

void setData(const rapidjson::Value json) {
    this->json =  json;

}

Any idea how I can simply copy a rapidjson object into a class member, so it can be parsed later?

Upvotes: 6

Views: 9519

Answers (1)

brkeyal
brkeyal

Reputation: 1385

To deep-copy rapidjson you should use CopyFrom. You should also provide an allocator for this copy. It makes sense then, that your class member will be the allocator, so make it of type rapidjson::Document rather than rapidjson::Value (Document inherits from Value).

Also, you better get the json parameter as reference rather than by value.

So, your function should look like that:

void setData(const rapidjson::Value& json) {
    this->json.CopyFrom(json, this->json.GetAllocator());
}

while your class member should be defined as:

rapidjson::Document json;

Upvotes: 4

Related Questions