2

I want to do what I consider very basic in rapidjson. I want to start a new object and then add members to that object - but I have a hard time finding out how to do it properly. I start like this:

rapidjson::Value obj;
obj.SetObject();
rapidjson::Value jName(name, this->rapDoc.GetAllocator());
this->rapCurrent->AddMember((jName).Move(), obj, this->rapDoc.GetAllocator());

next I want to make the this->rapCurrent point to the new created object member. The only way I found to make this work is

rapidjson::Value *lastAdded;
rapidjson::Value::ConstMemberIterator it;
for (rapidjson::Value::ConstMemberIterator it = this->rapCurrent->MemberBegin(); 
        it != this->rapCurrent->MemberEnd(); it++) {
    lastAdded = (rapidjson::Value *)&it->value;
}
this->rapCurrent = lastAdded;

which looks very complicated to me for such an easy task. Does anybody perhaps know how to this more simple?

Thanks a lot!

Timo

  • I too find the fact that AddMember simply returns the same Value right back to you useless. Never have I found it useful to chain multiple additions on the same line, but what I have often wanted to do was modify the new Value which was just added. – Dwayne Robinson Apr 19 '18 at 10:16

1 Answers1

0

Instead of looping by yourself, I think a simple way is to use FindMember(name) if you ensure there is no duplicate names.

I am not sure what is your situation, but using pointer (or iterator) can be a problem as they become invalid when its container is modified.

If you are constructing a tree structure, you can firstly construct the sub-tree and then add it to the root. The AddMember() will move the value (subtree) into the root in constant time.

Milo Yip
  • 4,902
  • 2
  • 25
  • 27