diff options
author | Milo Yip <miloyip@gmail.com> | 2014-06-28 15:44:11 +0400 |
---|---|---|
committer | Milo Yip <miloyip@gmail.com> | 2014-06-28 15:44:11 +0400 |
commit | cd144c3cfdc568278700484a4083a7b084d47dee (patch) | |
tree | f9097ebd3d62e5ec92a579af72fc774ea851146b /readme.md | |
parent | b2b12a6367bbaac77f5e54315fb9b8cbb2bee67d (diff) |
Added a simple example and a diagram showing the process.
Diffstat (limited to 'readme.md')
-rw-r--r-- | readme.md | 43 |
1 files changed, 42 insertions, 1 deletions
@@ -41,9 +41,50 @@ Rapidjson is a header-only C++ library. Just copy the `rapidjson/include/rapidjs To build the tests and examples: -1. Obtain [premake4] (http://industriousone.com/premake/download). +1. Obtain [premake4](http://industriousone.com/premake/download). 2. Copy premake4 executable to rapidjson/build (or system path) 3. Run `rapidjson/build/premake.bat` on Windows, `rapidjson/build/premake.sh` on Linux or other platforms 4. On Windows, build the solution at `rapidjson/build/vs2008/` or `/vs2010/` 5. On other platforms, run GNU make at `rapidjson/build/gmake/` (e.g., `make -f test.make config=release32`, `make -f example.make config=debug32`) 6. On success, the executable are generated at `rapidjson/bin` + +## Usage at a glance + +This simple example parses a JSON string into a document (DOM), make a simple modification of the DOM, and finally stringify the DOM to a JSON string. + +```cpp +// example/simpledom/simpledom.cpp +#include "rapidjson/document.h" +#include "rapidjson/writer.h" +#include "rapidjson/stringbuffer.h" +#include <iostream> + +using namespace rapidjson; + +int main() { + // 1. Parse a JSON string into DOM. + const char* json = "{\"project\":\"rapidjson\",\"stars\":10}"; + Document d; + d.Parse<0>(json); + + // 2. Modify it by DOM. + d["stars"].SetInt(d["stars"].GetInt() + 1); + + // 3. Stringify the DOM + StringBuffer buffer; + Writer<StringBuffer> writer(buffer); + d.Accept(writer); + + // Output {"project":"rapidjson","stars":10} + std::cout << buffer.GetString() << std::endl; + return 0; +} +``` + +Note that this exmample did not handle potential errors. + +The following diagram shows the process. + +[simpledom](doc/diagram/simpledom.png) + +More [examples](example/) are avaliable. |