diff --git a/build/premake4.lua b/build/premake4.lua index b4fdf8d..28a7138 100644 --- a/build/premake4.lua +++ b/build/premake4.lua @@ -176,3 +176,8 @@ solution "example" kind "ConsoleApp" files "../example/serialize/*" setTargetObjDir("../bin") + + project "simpledom" + kind "ConsoleApp" + files "../example/simpledom/*" + setTargetObjDir("../bin") diff --git a/doc/diagram/makefile b/doc/diagram/makefile new file mode 100644 index 0000000..4699438 --- /dev/null +++ b/doc/diagram/makefile @@ -0,0 +1,8 @@ +%.pdf: %.dot + dot $< -Tpdf -o $@ + +%.png: %.dot + dot $< -Tpng -o $@ + +DOTFILES = $(basename $(wildcard *.dot)) +all: $(addsuffix .png, $(DOTFILES)) #$(addsuffix .pdf, $(DOTFILES)) diff --git a/doc/diagram/simpledom.dot b/doc/diagram/simpledom.dot new file mode 100644 index 0000000..1e9edb5 --- /dev/null +++ b/doc/diagram/simpledom.dot @@ -0,0 +1,54 @@ +digraph { + compound=true + fontname="Inconsolata, Consolas" + fontsize=10 + margin="0,0" + ranksep=0.2 + penwidth=0.5 + + node [fontname="Inconsolata, Consolas", fontsize=10, penwidth=0.5] + edge [fontname="Inconsolata, Consolas", fontsize=10, arrowhead=normal] + + { + node [shape=record, fontsize="8", margin="0.04", height=0.2, color=gray] + srcjson [label="\{|p|r|o|j|e|c|t|\"|:|\"|r|a|p|i|d|j|s|o|n|\"|,|\"|s|t|a|r|s|\"|:|1|0|\}"] + dstjson [label="\{|p|r|o|j|e|c|t|\"|:|\"|r|a|p|i|d|j|s|o|n|\"|,|\"|s|t|a|r|s|\"|:|1|1|\}"] + } + + { + node [shape="box", style="filled", fillcolor="gray95"] + Document2 [label="(Modified) Document"] + Writer + } + + subgraph cluster1 { + margin="10,10" + labeljust="left" + label = "Document" + style=filled + fillcolor=gray95 + node [shape=Mrecord, style=filled, colorscheme=spectral7] + + root [label="{object|}", fillcolor=3] + + { + project [label="{string|\"project\"}", fillcolor=5] + rapidjson [label="{string|\"rapidjson\"}", fillcolor=5] + stars [label="{string|\"stars\"}", fillcolor=5] + ten [label="{number|10}", fillcolor=6] + } + + edge [arrowhead=vee] + root -> { project, stars } + + edge [arrowhead="none"] + project -> rapidjson + stars -> ten + } + + srcjson -> root [label=" Parse()", lhead="cluster1"] + + ten -> Document2 [label=" Increase \"stars\"", ltail="cluster1" ] + Document2 -> Writer [label=" Traverse DOM by Accept()"] + Writer -> dstjson [label=" Output to StringBuffer"] +} \ No newline at end of file diff --git a/doc/diagram/simpledom.png b/doc/diagram/simpledom.png new file mode 100644 index 0000000..dd9e9ae Binary files /dev/null and b/doc/diagram/simpledom.png differ diff --git a/example/simpledom/simpledom.cpp b/example/simpledom/simpledom.cpp new file mode 100644 index 0000000..bdcf6c8 --- /dev/null +++ b/example/simpledom/simpledom.cpp @@ -0,0 +1,28 @@ +// JSON simple example +// This example does not handle errors. + +#include "rapidjson/document.h" +#include "rapidjson/writer.h" +#include "rapidjson/stringbuffer.h" +#include + +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 writer(buffer); + d.Accept(writer); + + // Output {"project":"rapidjson","stars":10} + std::cout << buffer.GetString() << std::endl; + return 0; +} diff --git a/readme.md b/readme.md index 894fb44..18d8b95 100644 --- a/readme.md +++ b/readme.md @@ -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 + +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 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.