Commit cd144c3c authored by Milo Yip's avatar Milo Yip

Added a simple example and a diagram showing the process.

parent b2b12a63
......@@ -176,3 +176,8 @@ solution "example"
kind "ConsoleApp"
files "../example/serialize/*"
setTargetObjDir("../bin")
project "simpledom"
kind "ConsoleApp"
files "../example/simpledom/*"
setTargetObjDir("../bin")
%.pdf: %.dot
dot $< -Tpdf -o $@
%.png: %.dot
dot $< -Tpng -o $@
DOTFILES = $(basename $(wildcard *.dot))
all: $(addsuffix .png, $(DOTFILES)) #$(addsuffix .pdf, $(DOTFILES))
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
// JSON simple example
// This example does not handle errors.
#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;
}
......@@ -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.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment