http://rapidjson.org/zh-cn/
用法一览
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| #include "rapidjson/document.h" #include "rapidjson/writer.h" #include "rapidjson/stringbuffer.h" #include <iostream> using namespace rapidjson; int main() { const char* json = "{\"project\":\"rapidjson\",\"stars\":10}"; Document d; d.Parse(json); Value& s = d["stars"]; s.SetInt(s.GetInt() + 1); StringBuffer buffer; Writer<StringBuffer> writer(buffer); d.Accept(writer); std::cout << buffer.GetString() << std::endl; return 0; }
|
JSON Pointer
1 2 3 4
| { "foo" : ["bar", "baz"], "pi" : 3.1416 }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| #include "rapidjson/pointer.h"
Document d;
Pointer("/project").Set(d, "RapidJSON"); Pointer("/stars").Set(d, 10);
if (Value* stars = Pointer("/stars").Get(d)) stars->SetInt(stars->GetInt() + 1);
Pointer("/a/b/0").Create(d);
Value& hello = Pointer("/hello").GetWithDefault(d, "world");
Value x("C++"); Pointer("/hello").Swap(d, x);
bool success = Pointer("/a").Erase(d); assert(success);
|
辅助函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| Document d; SetValueByPointer(d, "/project", "RapidJSON"); SetValueByPointer(d, "/stars", 10); if (Value* stars = GetValueByPointer(d, "/stars")) stars->SetInt(stars->GetInt() + 1); CreateValueByPointer(d, "/a/b/0"); Value& hello = GetValueByPointerWithDefault(d, "/hello", "world"); Value x("C++"); SwapValueByPointer(d, "/hello", x); bool success = EraseValueByPointer(d, "/a"); assert(success);
|