如何在 RapidJSON 中创建和序列化文档
RapidJSON 是一个针对速度优化的 JSON 库 - 因此它缺乏一些便利性,也缺乏关于如何从零开始创建 JSON 文档的易用文档。
你可以这样创建一个 Document:
rapidjson_doc_snippet.cpp
// 生成文档:{"text": "Hello JSON!"}
Document doc;
doc.SetObject(); // 使 doc 成为对象!
doc.AddMember("text", "Hello JSON!", doc.GetAllocator());完整示例,打印到 cout:
rapidjson_full_example.cpp
#include <iostream>
#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <rapidjson/ostreamwrapper.h>
using namespace rapidjson;
using namespace std;
int main() {
// 生成文档:{"text": "Hello JSON!"}
Document doc;
doc.SetObject(); // 使 doc 成为对象!
doc.AddMember("text", "Hello JSON!", doc.GetAllocator());
// 写入 stdout
OStreamWrapper out(cout);
Writer<OStreamWrapper> writer(out);
doc.Accept(writer);
}Check out similar posts by category:
C/C++
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow