Rapidjson 是一個 C++ 的快速 JSON 解析器和生成器,使用 SAX/DOM 風格的 API 設計。ios
示例代碼:json
// rapidjson/example/simpledom/simpledom.cpp`#include "rapidjson/document.h"#include "rapidjson/writer.h"#include "rapidjson/stringbuffer.h"#include <iostream>usingnamespacerapidjson;intmain() {// 1. Parse a JSON string into DOM.constchar* json = "{\"project\":\"rapidjson\",\"stars\":10}";Document d;d.Parse(json);// 2. Modify it by DOM.Value& s = d["stars"];s.SetInt(s.GetInt() + 1);// 3. Stringify the DOMStringBuffer buffer;Writer<StringBuffer> writer(buffer);d.Accept(writer);// Output {"project":"rapidjson","stars":11}std::cout << buffer.GetString() << std::endl;return0;}
介紹 PPT 下載:http://www.oschina.net/doc/5711 api
主要特色:dom
RapidJSON是一個C++的JSON解析器及生成器。它的靈感來自RapidXml。
性能
RapidJSON小而全。它同時支持SAX和DOM風格的API。SAX解析器只有約500行代碼。編碼
RapidJSON快。它的性能可與strlen()相比。可支持SSE2/SSE4.1加速。spa
RapidJSON獨立。它不依賴於BOOST等外部庫。它甚至不依賴於STL。.net
RapidJSON對內存友好。在大部分32/64位機器上,每一個JSON值只佔16或20字節(除字符串外)。它預設使用一個快速的內存分配器,令分析器能夠緊湊地分配內存。設計
RapidJSON對Unicode友好。它支持UTF-八、UTF-1六、UTF-32 (大端序/小端序),並內部支持這些編碼的檢測、校驗及轉碼。例如,RapidJSON能夠在分析一個UTF-8文件至DOM時,把當中的JSON字符串轉碼至UTF-16。它也支持代理對(surrogate pair)及"\\u0000"(空字符)。代理