简体   繁体   中英

rapidjson document create nested object

I want to create nested json object using C++ rapdijson::document functions.

Here is JSON I want to write

{
  "a" :
  {
    "b" :
    {
      "value" : 1
    }
  }
}

Using writer I can create it like this:

#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>
using namespace rapidjson;
using namespace std;

void main() {
    StringBuffer s;
    Writer<StringBuffer> writer(s);

    writer.StartObject();
        writer.Key("a");
        writer.StartObject();
            writer.Key("b");
            writer.StartObject();
                writer.Key("value");
                writer.Int(1);
            writer.EndObject();
        writer.EndObject();
    writer.EndObject();
    cout << s.GetString() << endl;
}

But when I try to use document I cant create nested objects using functions like SetObject() and AddMember() only creates JSON-objects with one level.

Document d;
d.SetObject().AddMember("a", "b", d.GetAllocator());

How to add nested objects into document or values created by SetObject and combine em into complex JSON file using document style?

This code worked for me:

        Value valChannel;
        valChannel.SetObject();
        {
            valChannel.AddMember("sampler", c.sampler, w.mAl);

            Value valTarget;
            valTarget.SetObject();
            {
                valTarget.AddMember("id", StringRef(c.target.id->id), w.mAl);
                valTarget.AddMember("path", c.target.path, w.mAl);
            }
            valChannel.AddMember("target", valTarget, w.mAl);
        }

And last but not least add it to the document:

Document d;
d.CopyFrom(valChannel, d.GetAllocator());

This code is copied from the assimp-repo: Assimp-Repo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM