6

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?

user2741831
  • 2,120
  • 2
  • 22
  • 43
  • First of all, why are you not passing it as reference? Either use `const rapidjson::Value& json`, or remove the const, but it makes no sense to declare it as const as you are copying it anyways – tobspr Nov 20 '16 at 14:56
  • I can't remove the const, I have o control ovre that part of the source – user2741831 Nov 20 '16 at 15:07
  • @tobspr: Why should the parameter not be const? Of course it is more important if it is a reference, but it still makes sense if the parameter is passed by value. E.g. This prevents (accidental) modification of the function parameter. E.g. json = foo when you mean to writer this->json = foo. IMHO it's a bad practice as well to use function parameters as local variables and modify their content. It generally changes their meaning which renders their name incorrect. – DrP3pp3r Aug 11 '20 at 11:16

1 Answers1

3

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;
brkeyal
  • 1,317
  • 1
  • 16
  • 22
  • I remember this question. The way I ended up solving it was creating my own JSON datastructure and then copying the rapidjson into that – user2741831 Jul 18 '19 at 16:20