* Similar to [SAX](http://en.wikipedia.org/wiki/Simple_API_for_XML), RapidJSON provides a event sequential access parser API (`GenericReader`). It also provides a generator API (`GenericWriter`) which consumes the same set of events.
* Similar to [SAX](http://en.wikipedia.org/wiki/Simple_API_for_XML), RapidJSON provides a event sequential access parser API (`rapidjson::GenericReader`). It also provides a generator API (`rapidjson::Writer`) which consumes the same set of events.
* DOM (Document Object Model) style API
* DOM (Document Object Model) style API
* Similar to [DOM](http://en.wikipedia.org/wiki/Document_Object_Model) for HTML/XML, RapidJSON can parse JSON into a DOM representation (`GenericDocument`), for easy manipulation, and finally stringify back to JSON if needed.
* Similar to [DOM](http://en.wikipedia.org/wiki/Document_Object_Model) for HTML/XML, RapidJSON can parse JSON into a DOM representation (`rapidjson::GenericDocument`), for easy manipulation, and finally stringify back to JSON if needed.
* The DOM style API (`GenericDocument`) is actually implemented with SAX style API (`GenericReader`). SAX is faster but sometimes DOM is easier. Users can pick their choices according to scenarios.
* The DOM style API (`rapidjson::GenericDocument`) is actually implemented with SAX style API (`rapidjson::GenericReader`). SAX is faster but sometimes DOM is easier. Users can pick their choices according to scenarios.
## DOM (Document)
## DOM (Document)
...
@@ -59,13 +59,13 @@
...
@@ -59,13 +59,13 @@
## SAX (Writer)
## SAX (Writer)
* Support PrettyWriter for adding newlines and indentations.
* Support `rapidjson::PrettyWriter` for adding newlines and indentations.
* Support custom precision for floating point values.
* Support custom precision for floating point values.
## Stream
## Stream
* Support `GenericStringBuffer` for storing the output JSON as string.
* Support `rapidjson::GenericStringBuffer` for storing the output JSON as string.
* Support `FileReadStream`/`FileWriteStream` for input/output `FILE` object.
* Support `rapidjson::FileReadStream`/`rapidjson::FileWriteStream` for input/output `FILE` object.
In RapidJSON, `Stream` is a concept for reading/writing JSON. Here we first show how to use streams provided. And then see how to create a custom streams.
In RapidJSON, `rapidjson::Stream` is a concept for reading/writing JSON. Here we first show how to use streams provided. And then see how to create a custom streams.
## Memory Streams
## Memory Streams
...
@@ -73,6 +73,8 @@ However, if the JSON is big, or memory is limited, you can use `FileReadStream`.
...
@@ -73,6 +73,8 @@ However, if the JSON is big, or memory is limited, you can use `FileReadStream`.
#include "rapidjson/filereadstream.h"
#include "rapidjson/filereadstream.h"
#include <cstdio>
#include <cstdio>
using namespace rapidjson;
FILE* fp = fopen("big.json", "rb"); // non-Windows use "r"
FILE* fp = fopen("big.json", "rb"); // non-Windows use "r"
char readBuffer[65536];
char readBuffer[65536];
...
@@ -96,6 +98,8 @@ Apart from reading file, user can also use `FileReadStream` to read `stdin`.
...
@@ -96,6 +98,8 @@ Apart from reading file, user can also use `FileReadStream` to read `stdin`.
#include "rapidjson/filewritestream.h"
#include "rapidjson/filewritestream.h"
#include <cstdio>
#include <cstdio>
using namespace rapidjson;
Document d;
Document d;
d.Parse(json);
d.Parse(json);
// ...
// ...
...
@@ -212,6 +216,8 @@ You can obtain the type of UTF via `UTFType GetType()`. And check whether a BOM
...
@@ -212,6 +216,8 @@ You can obtain the type of UTF via `UTFType GetType()`. And check whether a BOM
Similarly, to choose encoding for output during runtime, we can use `AutoUTFOutputStream`. This class is not automatic *per se*. You need to specify the UTF type and whether to write BOM in runtime.
Similarly, to choose encoding for output during runtime, we can use `AutoUTFOutputStream`. This class is not automatic *per se*. You need to specify the UTF type and whether to write BOM in runtime.
\tparam Encoding encoding for both parsing and string storage.
\tparam Encoding encoding for both parsing and string storage.
\tparam Allocator allocator for allocating memory for the DOM, and the stack during parsing.
\tparam Allocator allocator for allocating memory for the DOM, and the stack during parsing.
\warning Although GenericDocument inherits from GenericValue, the API does \b not provide any virtual functions, especially no virtual destructors. To avoid memory leaks, do not \c delete a GenericDocument object via a pointer to a GenericValue.