參考資訊:
https://rapidjson.org/index.html
main.cpp
#include <rapidjson/document.h>
#include <rapidjson/rapidjson.h>
#include <rapidjson/writer.h>
#include <rapidjson/pointer.h>
#include <rapidjson/stringbuffer.h>
#include <iostream>
using namespace rapidjson;
int main(int argc, char **argv)
{
const char *p0 = "0";
const char *p1 = "1";
Document jsondoc;
Value arr(kArrayType);
Value tmp(kArrayType);
Value obj(kObjectType);
Pointer("/r0/s0").Set(obj, "1", jsondoc.GetAllocator());
arr.PushBack(obj, jsondoc.GetAllocator());
tmp.PushBack(StringRef(p0), jsondoc.GetAllocator());
tmp.PushBack(StringRef(p1), jsondoc.GetAllocator());
tmp.PushBack("2", jsondoc.GetAllocator());
tmp.PushBack("3", jsondoc.GetAllocator());
Pointer("/r1").Set(obj, tmp, jsondoc.GetAllocator());
arr.PushBack(obj, jsondoc.GetAllocator());
Pointer("/tags").Set(jsondoc, arr);
StringBuffer sb;
Writer<StringBuffer> writer(sb);
jsondoc.Accept(writer);
printf("%s\n", sb.GetString());
return 0;
}
編譯、執行
$ sudo apt-get install cmake jq -y
$ cd
$ git clone https://github.com/Tencent/rapidjson
$ cd rapidjson
$ git submodule update --init --recursive
$ mkdir build
$ cd build
$ cmake ..
$ make -j4
$ sudo make install
$ cd
$ g++ main.cpp -o test
$ ./test | jq
{
"tags": [
{
"r0": {
"s0": "1"
}
},
{
"r1": [
"0",
"1",
"2",
"3"
]
}
]
}