Commit 7398f66f authored by Milo Yip's avatar Milo Yip

Merge pull request #49 from pah/doc/doxygen-markdown

Integrate Markdown pages into Doxygen documentation
parents c12286a0 a213d2d8
...@@ -5,5 +5,6 @@ ...@@ -5,5 +5,6 @@
/build/*.exe /build/*.exe
/build/gmake /build/gmake
/build/vs*/ /build/vs*/
/doc/html
/thirdparty/lib /thirdparty/lib
/intermediate /intermediate
...@@ -52,7 +52,7 @@ PROJECT_LOGO = ...@@ -52,7 +52,7 @@ PROJECT_LOGO =
# If a relative path is entered, it will be relative to the location # If a relative path is entered, it will be relative to the location
# where doxygen was started. If left blank the current directory will be used. # where doxygen was started. If left blank the current directory will be used.
OUTPUT_DIRECTORY = ../doc OUTPUT_DIRECTORY = ./doc
# If the CREATE_SUBDIRS tag is set to YES, then doxygen will create # If the CREATE_SUBDIRS tag is set to YES, then doxygen will create
# 4096 sub-directories (in 2 levels) under the output directory of each output # 4096 sub-directories (in 2 levels) under the output directory of each output
...@@ -514,7 +514,7 @@ GENERATE_DEPRECATEDLIST= YES ...@@ -514,7 +514,7 @@ GENERATE_DEPRECATEDLIST= YES
# The ENABLED_SECTIONS tag can be used to enable conditional # The ENABLED_SECTIONS tag can be used to enable conditional
# documentation sections, marked by \if sectionname ... \endif. # documentation sections, marked by \if sectionname ... \endif.
ENABLED_SECTIONS = ENABLED_SECTIONS = $(RAPIDJSON_SECTIONS)
# The MAX_INITIALIZER_LINES tag determines the maximum number of lines # The MAX_INITIALIZER_LINES tag determines the maximum number of lines
# the initial value of a variable or macro consists of for it to appear in # the initial value of a variable or macro consists of for it to appear in
...@@ -548,7 +548,7 @@ SHOW_FILES = YES ...@@ -548,7 +548,7 @@ SHOW_FILES = YES
# Namespaces page. This will remove the Namespaces entry from the Quick Index # Namespaces page. This will remove the Namespaces entry from the Quick Index
# and from the Folder Tree View (if specified). The default is YES. # and from the Folder Tree View (if specified). The default is YES.
SHOW_NAMESPACES = YES SHOW_NAMESPACES = NO
# The FILE_VERSION_FILTER tag can be used to specify a program or script that # The FILE_VERSION_FILTER tag can be used to specify a program or script that
# doxygen should invoke to get the current version for each file (typically from # doxygen should invoke to get the current version for each file (typically from
...@@ -638,7 +638,9 @@ WARN_LOGFILE = ...@@ -638,7 +638,9 @@ WARN_LOGFILE =
# directories like "/usr/src/myproject". Separate the files or directories # directories like "/usr/src/myproject". Separate the files or directories
# with spaces. # with spaces.
INPUT = ../include/ INPUT = ./include/ \
./readme.md \
./doc/
# This tag can be used to specify the character encoding of the source files # This tag can be used to specify the character encoding of the source files
# that doxygen parses. Internally doxygen uses the UTF-8 encoding, which is # that doxygen parses. Internally doxygen uses the UTF-8 encoding, which is
...@@ -660,34 +662,12 @@ FILE_PATTERNS = *.c \ ...@@ -660,34 +662,12 @@ FILE_PATTERNS = *.c \
*.cc \ *.cc \
*.cxx \ *.cxx \
*.cpp \ *.cpp \
*.c++ \
*.d \
*.java \
*.ii \
*.ixx \
*.ipp \
*.i++ \
*.inl \
*.h \ *.h \
*.hh \ *.hh \
*.hxx \ *.hxx \
*.hpp \ *.hpp \
*.h++ \
*.idl \
*.odl \
*.cs \
*.php \
*.php3 \
*.inc \ *.inc \
*.m \ *.md
*.mm \
*.dox \
*.py \
*.f90 \
*.f \
*.for \
*.vhd \
*.vhdl
# The RECURSIVE tag can be used to turn specify whether or not subdirectories # The RECURSIVE tag can be used to turn specify whether or not subdirectories
# should be searched for input files as well. Possible values are YES and NO. # should be searched for input files as well. Possible values are YES and NO.
...@@ -748,7 +728,7 @@ EXAMPLE_RECURSIVE = NO ...@@ -748,7 +728,7 @@ EXAMPLE_RECURSIVE = NO
# directories that contain image that are included in the documentation (see # directories that contain image that are included in the documentation (see
# the \image command). # the \image command).
IMAGE_PATH = IMAGE_PATH = ./doc
# The INPUT_FILTER tag can be used to specify a program that doxygen should # The INPUT_FILTER tag can be used to specify a program that doxygen should
# invoke to filter for each input file. Doxygen will invoke the filter program # invoke to filter for each input file. Doxygen will invoke the filter program
...@@ -1479,13 +1459,13 @@ ENABLE_PREPROCESSING = YES ...@@ -1479,13 +1459,13 @@ ENABLE_PREPROCESSING = YES
# compilation will be performed. Macro expansion can be done in a controlled # compilation will be performed. Macro expansion can be done in a controlled
# way by setting EXPAND_ONLY_PREDEF to YES. # way by setting EXPAND_ONLY_PREDEF to YES.
MACRO_EXPANSION = NO MACRO_EXPANSION = YES
# If the EXPAND_ONLY_PREDEF and MACRO_EXPANSION tags are both set to YES # If the EXPAND_ONLY_PREDEF and MACRO_EXPANSION tags are both set to YES
# then the macro expansion is limited to the macros specified with the # then the macro expansion is limited to the macros specified with the
# PREDEFINED and EXPAND_AS_DEFINED tags. # PREDEFINED and EXPAND_AS_DEFINED tags.
EXPAND_ONLY_PREDEF = NO EXPAND_ONLY_PREDEF = YES
# If the SEARCH_INCLUDES tag is set to YES (the default) the includes files # If the SEARCH_INCLUDES tag is set to YES (the default) the includes files
# pointed to by INCLUDE_PATH will be searched when a #include is found. # pointed to by INCLUDE_PATH will be searched when a #include is found.
......
...@@ -39,10 +39,10 @@ ...@@ -39,10 +39,10 @@
## API styles ## API styles
* SAX (Simple API for XML) style API * SAX (Simple API for XML) style API
* 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.
* Support custom streams. * Support custom streams.
## Memory ## Memory
......
# RapidJSON Stream # RapidJSON Stream
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
...@@ -10,7 +10,7 @@ Memory streams store JSON in memory. ...@@ -10,7 +10,7 @@ Memory streams store JSON in memory.
`StringStream` is the most basic input stream. It represents a complete, read-only JSON stored in memory. It is defined in `rapidjson/rapidjson.h`. `StringStream` is the most basic input stream. It represents a complete, read-only JSON stored in memory. It is defined in `rapidjson/rapidjson.h`.
```cpp ~~~~~~~~~~cpp
#include "rapidjson/document.h" // will include "rapidjson/rapidjson.h" #include "rapidjson/document.h" // will include "rapidjson/rapidjson.h"
using namespace rapidjson; using namespace rapidjson;
...@@ -21,16 +21,16 @@ StringStream s(json); ...@@ -21,16 +21,16 @@ StringStream s(json);
Document d; Document d;
d.ParseStream(s); d.ParseStream(s);
``` ~~~~~~~~~~
Since this is very common usage, `Document::Parse(const char*)` is provided to do exactly the same as above: Since this is very common usage, `Document::Parse(const char*)` is provided to do exactly the same as above:
```cpp ~~~~~~~~~~cpp
// ... // ...
const char json[] = "[1, 2, 3, 4]"; const char json[] = "[1, 2, 3, 4]";
Document d; Document d;
d.Parse(json); d.Parse(json);
``` ~~~~~~~~~~
Note that, `StringStream` is a typedef of `GenericStringStream<UTF8<> >`, user may use another encodings to represent the character set of the stream. Note that, `StringStream` is a typedef of `GenericStringStream<UTF8<> >`, user may use another encodings to represent the character set of the stream.
...@@ -38,7 +38,7 @@ Note that, `StringStream` is a typedef of `GenericStringStream<UTF8<> >`, user m ...@@ -38,7 +38,7 @@ Note that, `StringStream` is a typedef of `GenericStringStream<UTF8<> >`, user m
`StringBuffer` is a simple output stream. It allocates a memory buffer for writing the whole JSON. Use `GetString()` to obtain the buffer. `StringBuffer` is a simple output stream. It allocates a memory buffer for writing the whole JSON. Use `GetString()` to obtain the buffer.
```cpp ~~~~~~~~~~cpp
#include "rapidjson/stringbuffer.h" #include "rapidjson/stringbuffer.h"
StringBuffer buffer; StringBuffer buffer;
...@@ -46,14 +46,14 @@ Writer<StringBuffer> writer(buffer); ...@@ -46,14 +46,14 @@ Writer<StringBuffer> writer(buffer);
d.Accept(writer); d.Accept(writer);
const char* output = buffer.GetString(); const char* output = buffer.GetString();
``` ~~~~~~~~~~
When the buffer is full, it will increases the capacity automatically. The default capacity is 256 characters (256 bytes for UTF8, 512 bytes for UTF16, etc.). User can provide an allocator and a initial capacity. When the buffer is full, it will increases the capacity automatically. The default capacity is 256 characters (256 bytes for UTF8, 512 bytes for UTF16, etc.). User can provide an allocator and a initial capacity.
```cpp ~~~~~~~~~~cpp
StringBuffer buffer1(0, 1024); // Use its allocator, initial size = 1024 StringBuffer buffer1(0, 1024); // Use its allocator, initial size = 1024
StringBuffer buffer2(allocator, 1024); StringBuffer buffer2(allocator, 1024);
``` ~~~~~~~~~~
By default, `StringBuffer` will instantiate an internal allocator. By default, `StringBuffer` will instantiate an internal allocator.
...@@ -69,10 +69,12 @@ However, if the JSON is big, or memory is limited, you can use `FileReadStream`. ...@@ -69,10 +69,12 @@ However, if the JSON is big, or memory is limited, you can use `FileReadStream`.
`FileReadStream` reads the file via a `FILE` pointer. And user need to provide a buffer. `FileReadStream` reads the file via a `FILE` pointer. And user need to provide a buffer.
```cpp ~~~~~~~~~~cpp
#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];
...@@ -82,7 +84,7 @@ Document d; ...@@ -82,7 +84,7 @@ Document d;
d.ParseStream(is); d.ParseStream(is);
fclose(fp); fclose(fp);
``` ~~~~~~~~~~
Different from string streams, `FileReadStream` is byte stream. It does not handle encodings. If the file is not UTF-8, the byte stream can be wrapped in a `EncodedInputStream`. It will be discussed very soon. Different from string streams, `FileReadStream` is byte stream. It does not handle encodings. If the file is not UTF-8, the byte stream can be wrapped in a `EncodedInputStream`. It will be discussed very soon.
...@@ -92,10 +94,12 @@ Apart from reading file, user can also use `FileReadStream` to read `stdin`. ...@@ -92,10 +94,12 @@ Apart from reading file, user can also use `FileReadStream` to read `stdin`.
`FileWriteStream` is buffered output stream. Its usage is very similar to `FileReadStream`. `FileWriteStream` is buffered output stream. Its usage is very similar to `FileReadStream`.
```cpp ~~~~~~~~~~cpp
#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);
// ... // ...
...@@ -109,7 +113,7 @@ Writer<FileWriteStream> writer(os); ...@@ -109,7 +113,7 @@ Writer<FileWriteStream> writer(os);
d.Accept(writer); d.Accept(writer);
fclose(fp); fclose(fp);
``` ~~~~~~~~~~
It can also directs the output to `stdout`. It can also directs the output to `stdout`.
...@@ -129,7 +133,7 @@ Note that, these encoded streams can be applied to streams other than file. For ...@@ -129,7 +133,7 @@ Note that, these encoded streams can be applied to streams other than file. For
`EncodedInputStream` has two template parameters. The first one is a `Encoding` class, such as `UTF8`, `UTF16LE`, defined in `rapidjson/encodings.h`. The second one is the class of stream to be wrapped. `EncodedInputStream` has two template parameters. The first one is a `Encoding` class, such as `UTF8`, `UTF16LE`, defined in `rapidjson/encodings.h`. The second one is the class of stream to be wrapped.
```cpp ~~~~~~~~~~cpp
#include "rapidjson/document.h" #include "rapidjson/document.h"
#include "rapidjson/filereadstream.h" // FileReadStream #include "rapidjson/filereadstream.h" // FileReadStream
#include "rapidjson/encodedstream.h" // EncodedInputStream #include "rapidjson/encodedstream.h" // EncodedInputStream
...@@ -148,13 +152,13 @@ Document d; // Document is GenericDocument<UTF8<> > ...@@ -148,13 +152,13 @@ Document d; // Document is GenericDocument<UTF8<> >
d.ParseStream<0, UTF16LE<> >(eis); // Parses UTF-16LE file into UTF-8 in memory d.ParseStream<0, UTF16LE<> >(eis); // Parses UTF-16LE file into UTF-8 in memory
fclose(fp); fclose(fp);
``` ~~~~~~~~~~
### EncodedOutputStream ### EncodedOutputStream
`EncodedOutputStream` is similar but it has a `bool putBOM` parameter in the constructor, controlling whether to write BOM into output byte stream. `EncodedOutputStream` is similar but it has a `bool putBOM` parameter in the constructor, controlling whether to write BOM into output byte stream.
```cpp ~~~~~~~~~~cpp
#include "rapidjson/filewritestream.h" // FileWriteStream #include "rapidjson/filewritestream.h" // FileWriteStream
#include "rapidjson/encodedstream.h" // EncodedOutputStream #include "rapidjson/encodedstream.h" // EncodedOutputStream
#include <cstdio> #include <cstdio>
...@@ -174,7 +178,7 @@ Writer<OutputStream, UTF32LE<>, UTF8<>> writer(eos); ...@@ -174,7 +178,7 @@ Writer<OutputStream, UTF32LE<>, UTF8<>> writer(eos);
d.Accept(writer); // This generates UTF32-LE file from UTF-8 in memory d.Accept(writer); // This generates UTF32-LE file from UTF-8 in memory
fclose(fp); fclose(fp);
``` ~~~~~~~~~~
### AutoUTFInputStream ### AutoUTFInputStream
...@@ -182,7 +186,7 @@ Sometimes an application may want to handle all supported JSON encoding. `AutoUT ...@@ -182,7 +186,7 @@ Sometimes an application may want to handle all supported JSON encoding. `AutoUT
Since the characters (code units) may be 8-bit, 16-bit or 32-bit. `AutoUTFInputStream` requires a character type which can hold at least 32-bit. We may use `unsigned`, as in the template parameter: Since the characters (code units) may be 8-bit, 16-bit or 32-bit. `AutoUTFInputStream` requires a character type which can hold at least 32-bit. We may use `unsigned`, as in the template parameter:
```cpp ~~~~~~~~~~cpp
#include "rapidjson/document.h" #include "rapidjson/document.h"
#include "rapidjson/filereadstream.h" // FileReadStream #include "rapidjson/filereadstream.h" // FileReadStream
#include "rapidjson/encodedstream.h" // AutoUTFInputStream #include "rapidjson/encodedstream.h" // AutoUTFInputStream
...@@ -201,7 +205,7 @@ Document d; // Document is GenericDocument<UTF8<> > ...@@ -201,7 +205,7 @@ Document d; // Document is GenericDocument<UTF8<> >
d.ParseStream<0, AutoUTF<unsigned> >(eis); // This parses any UTF file into UTF-8 in memory d.ParseStream<0, AutoUTF<unsigned> >(eis); // This parses any UTF file into UTF-8 in memory
fclose(fp); fclose(fp);
``` ~~~~~~~~~~
When specifying the encoding of stream, uses `AutoUTF<CharType>` as in `ParseStream()` above. When specifying the encoding of stream, uses `AutoUTF<CharType>` as in `ParseStream()` above.
...@@ -211,7 +215,9 @@ You can obtain the type of UTF via `UTFType GetType()`. And check whether a BOM ...@@ -211,7 +215,9 @@ 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.
```cpp ~~~~~~~~~~cpp
using namespace rapidjson;
void WriteJSONFile(FILE* fp, UTFType type, bool putBOM, const Document& d) { void WriteJSONFile(FILE* fp, UTFType type, bool putBOM, const Document& d) {
char writeBuffer[256]; char writeBuffer[256];
FileWriteStream bos(fp, writeBuffer, sizeof(writeBuffer)); FileWriteStream bos(fp, writeBuffer, sizeof(writeBuffer));
...@@ -222,7 +228,7 @@ void WriteJSONFile(FILE* fp, UTFType type, bool putBOM, const Document& d) { ...@@ -222,7 +228,7 @@ void WriteJSONFile(FILE* fp, UTFType type, bool putBOM, const Document& d) {
Writer<OutputStream, UTF8<>, AutoUTF<> > writer; Writer<OutputStream, UTF8<>, AutoUTF<> > writer;
d.Accept(writer); d.Accept(writer);
} }
``` ~~~~~~~~~~
`AutoUTFInputStream` and `AutoUTFOutputStream` is more convenient than `EncodedInputStream` and `EncodedOutputStream`. They just incur a little bit runtime overheads. `AutoUTFInputStream` and `AutoUTFOutputStream` is more convenient than `EncodedInputStream` and `EncodedOutputStream`. They just incur a little bit runtime overheads.
...@@ -232,7 +238,7 @@ In addition to memory/file streams, user can create their own stream classes whi ...@@ -232,7 +238,7 @@ In addition to memory/file streams, user can create their own stream classes whi
RapidJSON combines different types using templates. A class containing all required interface can be a stream. The Stream interface is defined in comments of `rapidjson/rapidjson.h`: RapidJSON combines different types using templates. A class containing all required interface can be a stream. The Stream interface is defined in comments of `rapidjson/rapidjson.h`:
```cpp ~~~~~~~~~~cpp
concept Stream { concept Stream {
typename Ch; //!< Character type of the stream. typename Ch; //!< Character type of the stream.
...@@ -261,7 +267,7 @@ concept Stream { ...@@ -261,7 +267,7 @@ concept Stream {
//! \return Number of characters written. //! \return Number of characters written.
size_t PutEnd(Ch* begin); size_t PutEnd(Ch* begin);
} }
``` ~~~~~~~~~~
For input stream, they must implement `Peek()`, `Take()` and `Tell()`. For input stream, they must implement `Peek()`, `Take()` and `Tell()`.
For output stream, they must implement `Put()` and `Flush()`. For output stream, they must implement `Put()` and `Flush()`.
...@@ -271,7 +277,7 @@ There are two special interface, `PutBegin()` and `PutEnd()`, which are only for ...@@ -271,7 +277,7 @@ There are two special interface, `PutBegin()` and `PutEnd()`, which are only for
The following example is a wrapper of `std::istream`, which only implements 3 functions. The following example is a wrapper of `std::istream`, which only implements 3 functions.
```cpp ~~~~~~~~~~cpp
class IStreamWrapper { class IStreamWrapper {
public: public:
typedef char Ch; typedef char Ch;
...@@ -302,18 +308,18 @@ private: ...@@ -302,18 +308,18 @@ private:
std::istream& is_; std::istream& is_;
}; };
``` ~~~~~~~~~~
User can use it to wrap instances of `std::stringstream`, `std::ifstream`. User can use it to wrap instances of `std::stringstream`, `std::ifstream`.
```cpp ~~~~~~~~~~cpp
const char* json = "[1,2,3,4]"; const char* json = "[1,2,3,4]";
std::stringstream ss(json); std::stringstream ss(json);
IStreamWrapper is(ss); IStreamWrapper is(ss);
Document d; Document d;
d.Parse(is); d.Parse(is);
``` ~~~~~~~~~~
Note that, this implementation may not be as efficient as RapidJSON's memory or file streams, due to internal overheads of the standard library. Note that, this implementation may not be as efficient as RapidJSON's memory or file streams, due to internal overheads of the standard library.
...@@ -321,7 +327,7 @@ Note that, this implementation may not be as efficient as RapidJSON's memory or ...@@ -321,7 +327,7 @@ Note that, this implementation may not be as efficient as RapidJSON's memory or
The following example is a wrapper of `std::istream`, which only implements 2 functions. The following example is a wrapper of `std::istream`, which only implements 2 functions.
```cpp ~~~~~~~~~~cpp
class OStreamWrapper { class OStreamWrapper {
public: public:
typedef char Ch; typedef char Ch;
...@@ -344,11 +350,11 @@ private: ...@@ -344,11 +350,11 @@ private:
std::ostream& os_; std::ostream& os_;
}; };
``` ~~~~~~~~~~
User can use it to wrap instances of `std::stringstream`, `std::ofstream`. User can use it to wrap instances of `std::stringstream`, `std::ofstream`.
```cpp ~~~~~~~~~~cpp
Document d; Document d;
// ... // ...
...@@ -357,7 +363,7 @@ OSStreamWrapper os(ss); ...@@ -357,7 +363,7 @@ OSStreamWrapper os(ss);
Writer<OStreamWrapper> writer(os); Writer<OStreamWrapper> writer(os);
d.Accept(writer); d.Accept(writer);
``` ~~~~~~~~~~
Note that, this implementation may not be as efficient as RapidJSON's memory or file streams, due to internal overheads of the standard library. Note that, this implementation may not be as efficient as RapidJSON's memory or file streams, due to internal overheads of the standard library.
......
This diff is collapsed.
...@@ -73,7 +73,7 @@ class GenericMemberIterator ...@@ -73,7 +73,7 @@ class GenericMemberIterator
public: public:
//! Iterator type itself //! Iterator type itself
typedef GenericMemberIterator Type; typedef GenericMemberIterator Iterator;
//! Constant iterator type //! Constant iterator type
typedef GenericMemberIterator<true,Encoding,Allocator> ConstType; typedef GenericMemberIterator<true,Encoding,Allocator> ConstType;
//! Non-constant iterator type //! Non-constant iterator type
...@@ -112,29 +112,29 @@ public: ...@@ -112,29 +112,29 @@ public:
//! @name stepping //! @name stepping
//@{ //@{
Type& operator++(){ ++ptr_; return *this; } Iterator& operator++(){ ++ptr_; return *this; }
Type& operator--(){ --ptr_; return *this; } Iterator& operator--(){ --ptr_; return *this; }
Type operator++(int){ Type old(*this); ++ptr_; return old; } Iterator operator++(int){ Iterator old(*this); ++ptr_; return old; }
Type operator--(int){ Type old(*this); --ptr_; return old; } Iterator operator--(int){ Iterator old(*this); --ptr_; return old; }
//@} //@}
//! @name increment/decrement //! @name increment/decrement
//@{ //@{
Type operator+(DifferenceType n) const { return Type(ptr_+n); } Iterator operator+(DifferenceType n) const { return Iterator(ptr_+n); }
Type operator-(DifferenceType n) const { return Type(ptr_-n); } Iterator operator-(DifferenceType n) const { return Iterator(ptr_-n); }
Type& operator+=(DifferenceType n) { ptr_+=n; return *this; } Iterator& operator+=(DifferenceType n) { ptr_+=n; return *this; }
Type& operator-=(DifferenceType n) { ptr_-=n; return *this; } Iterator& operator-=(DifferenceType n) { ptr_-=n; return *this; }
//@} //@}
//! @name relations //! @name relations
//@{ //@{
bool operator==(Type that) const { return ptr_ == that.ptr_; } bool operator==(Iterator that) const { return ptr_ == that.ptr_; }
bool operator!=(Type that) const { return ptr_ != that.ptr_; } bool operator!=(Iterator that) const { return ptr_ != that.ptr_; }
bool operator<=(Type that) const { return ptr_ <= that.ptr_; } bool operator<=(Iterator that) const { return ptr_ <= that.ptr_; }
bool operator>=(Type that) const { return ptr_ >= that.ptr_; } bool operator>=(Iterator that) const { return ptr_ >= that.ptr_; }
bool operator< (Type that) const { return ptr_ < that.ptr_; } bool operator< (Iterator that) const { return ptr_ < that.ptr_; }
bool operator> (Type that) const { return ptr_ > that.ptr_; } bool operator> (Iterator that) const { return ptr_ > that.ptr_; }
//@} //@}
//! @name dereference //! @name dereference
...@@ -145,7 +145,7 @@ public: ...@@ -145,7 +145,7 @@ public:
//@} //@}
//! Distance //! Distance
DifferenceType operator-(Type that) const { return ptr_-that.ptr_; } DifferenceType operator-(Iterator that) const { return ptr_-that.ptr_; }
private: private:
//! Internal constructor from plain pointer //! Internal constructor from plain pointer
...@@ -165,13 +165,13 @@ struct GenericMemberIterator; ...@@ -165,13 +165,13 @@ struct GenericMemberIterator;
template <typename Encoding, typename Allocator> template <typename Encoding, typename Allocator>
struct GenericMemberIterator<false,Encoding,Allocator> { struct GenericMemberIterator<false,Encoding,Allocator> {
//! use plain pointer as iterator type //! use plain pointer as iterator type
typedef GenericMember<Encoding,Allocator>* Type; typedef GenericMember<Encoding,Allocator>* Iterator;
}; };
//! const GenericMemberIterator //! const GenericMemberIterator
template <typename Encoding, typename Allocator> template <typename Encoding, typename Allocator>
struct GenericMemberIterator<true,Encoding,Allocator> { struct GenericMemberIterator<true,Encoding,Allocator> {
//! use plain const pointer as iterator type //! use plain const pointer as iterator type
typedef const GenericMember<Encoding,Allocator>* Type; typedef const GenericMember<Encoding,Allocator>* Iterator;
}; };
#endif // RAPIDJSON_NOMEMBERITERATORCLASS #endif // RAPIDJSON_NOMEMBERITERATORCLASS
...@@ -198,8 +198,8 @@ public: ...@@ -198,8 +198,8 @@ public:
typedef Encoding EncodingType; //!< Encoding type from template parameter. typedef Encoding EncodingType; //!< Encoding type from template parameter.
typedef Allocator AllocatorType; //!< Allocator type from template parameter. typedef Allocator AllocatorType; //!< Allocator type from template parameter.
typedef typename Encoding::Ch Ch; //!< Character type derived from Encoding. typedef typename Encoding::Ch Ch; //!< Character type derived from Encoding.
typedef typename GenericMemberIterator<false,Encoding,Allocator>::Type MemberIterator; //!< Member iterator for iterating in object. typedef typename GenericMemberIterator<false,Encoding,Allocator>::Iterator MemberIterator; //!< Member iterator for iterating in object.
typedef typename GenericMemberIterator<true,Encoding,Allocator>::Type ConstMemberIterator; //!< Constant member iterator for iterating in object. typedef typename GenericMemberIterator<true,Encoding,Allocator>::Iterator ConstMemberIterator; //!< Constant member iterator for iterating in object.
typedef GenericValue* ValueIterator; //!< Value iterator for iterating in array. typedef GenericValue* ValueIterator; //!< Value iterator for iterating in array.
typedef const GenericValue* ConstValueIterator; //!< Constant value iterator for iterating in array. typedef const GenericValue* ConstValueIterator; //!< Constant value iterator for iterating in array.
...@@ -937,7 +937,7 @@ private: ...@@ -937,7 +937,7 @@ private:
}; };
#pragma pack (pop) #pragma pack (pop)
//! Value with UTF8 encoding. //! GenericValue with UTF8 encoding
typedef GenericValue<UTF8<> > Value; typedef GenericValue<UTF8<> > Value;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
...@@ -948,6 +948,7 @@ typedef GenericValue<UTF8<> > Value; ...@@ -948,6 +948,7 @@ typedef GenericValue<UTF8<> > Value;
\note implements Handler concept \note implements Handler concept
\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.
*/ */
template <typename Encoding, typename Allocator = MemoryPoolAllocator<> > template <typename Encoding, typename Allocator = MemoryPoolAllocator<> >
class GenericDocument : public GenericValue<Encoding, Allocator> { class GenericDocument : public GenericValue<Encoding, Allocator> {
...@@ -962,8 +963,13 @@ public: ...@@ -962,8 +963,13 @@ public:
*/ */
GenericDocument(Allocator* allocator = 0, size_t stackCapacity = kDefaultStackCapacity) : stack_(allocator, stackCapacity), parseErrorCode_(kParseErrorNone), errorOffset_(0) {} GenericDocument(Allocator* allocator = 0, size_t stackCapacity = kDefaultStackCapacity) : stack_(allocator, stackCapacity), parseErrorCode_(kParseErrorNone), errorOffset_(0) {}
//! Parse JSON text from an input stream. //!@name Parse from stream
/*! \tparam parseFlags Combination of ParseFlag. //!@{
//! Parse JSON text from an input stream (with Encoding conversion)
/*! \tparam parseFlags Combination of \ref ParseFlag.
\tparam SourceEncoding Encoding of input stream
\tparam InputStream Type of input stream, implementing Stream concept
\param is Input stream to be parsed. \param is Input stream to be parsed.
\return The document itself for fluent API. \return The document itself for fluent API.
*/ */
...@@ -985,18 +991,34 @@ public: ...@@ -985,18 +991,34 @@ public:
return *this; return *this;
} }
//! Parse JSON text from an input stream
/*! \tparam parseFlags Combination of \ref ParseFlag.
\tparam InputStream Type of input stream, implementing Stream concept
\param is Input stream to be parsed.
\return The document itself for fluent API.
*/
template <unsigned parseFlags, typename InputStream> template <unsigned parseFlags, typename InputStream>
GenericDocument& ParseStream(InputStream& is) { GenericDocument& ParseStream(InputStream& is) {
return ParseStream<parseFlags,Encoding,InputStream>(is); return ParseStream<parseFlags,Encoding,InputStream>(is);
} }
//! Parse JSON text from an input stream (with \ref kParseDefaultFlags)
/*! \tparam InputStream Type of input stream, implementing Stream concept
\param is Input stream to be parsed.
\return The document itself for fluent API.
*/
template <typename InputStream> template <typename InputStream>
GenericDocument& ParseStream(InputStream& is) { GenericDocument& ParseStream(InputStream& is) {
return ParseStream<kParseDefaultFlags, Encoding, InputStream>(is); return ParseStream<kParseDefaultFlags, Encoding, InputStream>(is);
} }
//!@}
//!@name Parse in-place from mutable string
//!@{
//! Parse JSON text from a mutable string. //! Parse JSON text from a mutable string (with Encoding conversion)
/*! \tparam parseFlags Combination of ParseFlag. /*! \tparam parseFlags Combination of \ref ParseFlag.
\tparam SourceEncoding Transcoding from input Encoding
\param str Mutable zero-terminated string to be parsed. \param str Mutable zero-terminated string to be parsed.
\return The document itself for fluent API. \return The document itself for fluent API.
*/ */
...@@ -1006,17 +1028,31 @@ public: ...@@ -1006,17 +1028,31 @@ public:
return ParseStream<parseFlags | kParseInsituFlag, SourceEncoding>(s); return ParseStream<parseFlags | kParseInsituFlag, SourceEncoding>(s);
} }
//! Parse JSON text from a mutable string
/*! \tparam parseFlags Combination of \ref ParseFlag.
\param str Mutable zero-terminated string to be parsed.
\return The document itself for fluent API.
*/
template <unsigned parseFlags> template <unsigned parseFlags>
GenericDocument& ParseInsitu(Ch* str) { GenericDocument& ParseInsitu(Ch* str) {
return ParseInsitu<parseFlags, Encoding>(str); return ParseInsitu<parseFlags, Encoding>(str);
} }
//! Parse JSON text from a mutable string (with \ref kParseDefaultFlags)
/*! \param str Mutable zero-terminated string to be parsed.
\return The document itself for fluent API.
*/
GenericDocument& ParseInsitu(Ch* str) { GenericDocument& ParseInsitu(Ch* str) {
return ParseInsitu<kParseDefaultFlags, Encoding>(str); return ParseInsitu<kParseDefaultFlags, Encoding>(str);
} }
//!@}
//! Parse JSON text from a read-only string. //!@name Parse from read-only string
/*! \tparam parseFlags Combination of ParseFlag (must not contain kParseInsituFlag). //!@{
//! Parse JSON text from a read-only string (with Encoding conversion)
/*! \tparam parseFlags Combination of \ref ParseFlag (must not contain \ref kParseInsituFlag).
\tparam SourceEncoding Transcoding from input Encoding
\param str Read-only zero-terminated string to be parsed. \param str Read-only zero-terminated string to be parsed.
*/ */
template <unsigned parseFlags, typename SourceEncoding> template <unsigned parseFlags, typename SourceEncoding>
...@@ -1026,14 +1062,25 @@ public: ...@@ -1026,14 +1062,25 @@ public:
return ParseStream<parseFlags, SourceEncoding>(s); return ParseStream<parseFlags, SourceEncoding>(s);
} }
//! Parse JSON text from a read-only string
/*! \tparam parseFlags Combination of \ref ParseFlag (must not contain \ref kParseInsituFlag).
\param str Read-only zero-terminated string to be parsed.
*/
template <unsigned parseFlags> template <unsigned parseFlags>
GenericDocument& Parse(const Ch* str) { GenericDocument& Parse(const Ch* str) {
return Parse<parseFlags, Encoding>(str); return Parse<parseFlags, Encoding>(str);
} }
//! Parse JSON text from a read-only string (with \ref kParseDefaultFlags)
/*! \param str Read-only zero-terminated string to be parsed.
*/
GenericDocument& Parse(const Ch* str) { GenericDocument& Parse(const Ch* str) {
return Parse<kParseDefaultFlags>(str); return Parse<kParseDefaultFlags>(str);
} }
//!@}
//!@name Handling parse errors
//!@{
//! Whether a parse error was occured in the last parsing. //! Whether a parse error was occured in the last parsing.
bool HasParseError() const { return parseErrorCode_ != kParseErrorNone; } bool HasParseError() const { return parseErrorCode_ != kParseErrorNone; }
...@@ -1044,6 +1091,8 @@ public: ...@@ -1044,6 +1091,8 @@ public:
//! Get the offset in character of the parsing error. //! Get the offset in character of the parsing error.
size_t GetErrorOffset() const { return errorOffset_; } size_t GetErrorOffset() const { return errorOffset_; }
//!@}
//! Get the allocator of this document. //! Get the allocator of this document.
Allocator& GetAllocator() { return stack_.GetAllocator(); } Allocator& GetAllocator() { return stack_.GetAllocator(); }
...@@ -1086,7 +1135,7 @@ private: ...@@ -1086,7 +1135,7 @@ private:
} }
private: private:
// Prohibit assignment //! Prohibit assignment
GenericDocument& operator=(const GenericDocument&); GenericDocument& operator=(const GenericDocument&);
void ClearStack() { void ClearStack() {
...@@ -1103,6 +1152,7 @@ private: ...@@ -1103,6 +1152,7 @@ private:
size_t errorOffset_; size_t errorOffset_;
}; };
//! GenericDocument with UTF8 encoding
typedef GenericDocument<UTF8<> > Document; typedef GenericDocument<UTF8<> > Document;
// defined here due to the dependency on GenericDocument // defined here due to the dependency on GenericDocument
......
...@@ -24,9 +24,9 @@ public: ...@@ -24,9 +24,9 @@ public:
typedef typename Base::Ch Ch; typedef typename Base::Ch Ch;
//! Constructor //! Constructor
/*! \param os Output os. /*! \param os Output stream.
\param allocator User supplied allocator. If it is null, it will create a private one. \param allocator User supplied allocator. If it is null, it will create a private one.
\param levelDepth Initial capacity of \param levelDepth Initial capacity of stack.
*/ */
PrettyWriter(OutputStream& os, Allocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) : PrettyWriter(OutputStream& os, Allocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) :
Base(os, allocator, levelDepth), indentChar_(' '), indentCharCount_(4) {} Base(os, allocator, levelDepth), indentChar_(' '), indentCharCount_(4) {}
...@@ -46,7 +46,9 @@ public: ...@@ -46,7 +46,9 @@ public:
return *this; return *this;
} }
//@name Implementation of Handler. /*! @name Implementation of Handler
\see Handler
*/
//@{ //@{
PrettyWriter& Null() { PrettyPrefix(kNullType); Base::WriteNull(); return *this; } PrettyWriter& Null() { PrettyPrefix(kNullType); Base::WriteNull(); return *this; }
...@@ -56,11 +58,6 @@ public: ...@@ -56,11 +58,6 @@ public:
PrettyWriter& Int64(int64_t i64) { PrettyPrefix(kNumberType); Base::WriteInt64(i64); return *this; } PrettyWriter& Int64(int64_t i64) { PrettyPrefix(kNumberType); Base::WriteInt64(i64); return *this; }
PrettyWriter& Uint64(uint64_t u64) { PrettyPrefix(kNumberType); Base::WriteUint64(u64); return *this; } PrettyWriter& Uint64(uint64_t u64) { PrettyPrefix(kNumberType); Base::WriteUint64(u64); return *this; }
PrettyWriter& Double(double d) { PrettyPrefix(kNumberType); Base::WriteDouble(d); return *this; } PrettyWriter& Double(double d) { PrettyPrefix(kNumberType); Base::WriteDouble(d); return *this; }
//! Overridden for fluent API, see \ref Writer::Double()
PrettyWriter& Double(double d, int precision) {
int oldPrecision = Base::GetDoublePrecision();
return SetDoublePrecision(precision).Double(d).SetDoublePrecision(oldPrecision);
}
PrettyWriter& String(const Ch* str, SizeType length, bool copy = false) { PrettyWriter& String(const Ch* str, SizeType length, bool copy = false) {
(void)copy; (void)copy;
...@@ -117,9 +114,19 @@ public: ...@@ -117,9 +114,19 @@ public:
//@} //@}
/*! @name Convenience extensions */
//@{
//! Simpler but slower overload. //! Simpler but slower overload.
PrettyWriter& String(const Ch* str) { return String(str, internal::StrLen(str)); } PrettyWriter& String(const Ch* str) { return String(str, internal::StrLen(str)); }
//! Overridden for fluent API, see \ref Writer::Double()
PrettyWriter& Double(double d, int precision) {
int oldPrecision = Base::GetDoublePrecision();
return SetDoublePrecision(precision).Double(d).SetDoublePrecision(oldPrecision);
}
//@}
protected: protected:
void PrettyPrefix(Type type) { void PrettyPrefix(Type type) {
(void)type; (void)type;
......
...@@ -4,6 +4,16 @@ ...@@ -4,6 +4,16 @@
// Copyright (c) 2011 Milo Yip (miloyip@gmail.com) // Copyright (c) 2011 Milo Yip (miloyip@gmail.com)
// Version 0.1 // Version 0.1
/*!\file rapidjson.h
\brief common definitions and configuration
\todo Complete Doxygen documentation for configure macros.
*/
/*! \mainpage
Documentation can be found in \ref readme.md README.
*/
#include <cstdlib> // malloc(), realloc(), free() #include <cstdlib> // malloc(), realloc(), free()
#include <cstring> // memcpy() #include <cstring> // memcpy()
...@@ -14,6 +24,7 @@ ...@@ -14,6 +24,7 @@
// (U)INT64_C constant macros. // (U)INT64_C constant macros.
// If user have their own definition, can define RAPIDJSON_NO_INT64DEFINE to disable this. // If user have their own definition, can define RAPIDJSON_NO_INT64DEFINE to disable this.
#ifndef RAPIDJSON_NO_INT64DEFINE #ifndef RAPIDJSON_NO_INT64DEFINE
//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
#ifndef __STDC_CONSTANT_MACROS #ifndef __STDC_CONSTANT_MACROS
# define __STDC_CONSTANT_MACROS 1 // required by C++ standard # define __STDC_CONSTANT_MACROS 1 // required by C++ standard
#endif #endif
...@@ -25,6 +36,7 @@ ...@@ -25,6 +36,7 @@
#include <stdint.h> #include <stdint.h>
#include <inttypes.h> #include <inttypes.h>
#endif #endif
//!@endcond
#endif // RAPIDJSON_NO_INT64TYPEDEF #endif // RAPIDJSON_NO_INT64TYPEDEF
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
...@@ -46,7 +58,7 @@ ...@@ -46,7 +58,7 @@
//! Endianness of the machine. //! Endianness of the machine.
/*! GCC provided macro for detecting endianness of the target machine. But other /*! GCC provided macro for detecting endianness of the target machine. But other
compilers may not have this. User can define RAPIDJSON_ENDIAN to either compilers may not have this. User can define RAPIDJSON_ENDIAN to either
RAPIDJSON_LITTLEENDIAN or RAPIDJSON_BIGENDIAN. \ref RAPIDJSON_LITTLEENDIAN or \ref RAPIDJSON_BIGENDIAN.
*/ */
#ifndef RAPIDJSON_ENDIAN #ifndef RAPIDJSON_ENDIAN
#ifdef __BYTE_ORDER__ #ifdef __BYTE_ORDER__
...@@ -115,7 +127,9 @@ typedef unsigned SizeType; ...@@ -115,7 +127,9 @@ typedef unsigned SizeType;
// Adopt from boost // Adopt from boost
#ifndef RAPIDJSON_STATIC_ASSERT #ifndef RAPIDJSON_STATIC_ASSERT
//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
namespace rapidjson { namespace rapidjson {
template <bool x> struct STATIC_ASSERTION_FAILURE; template <bool x> struct STATIC_ASSERTION_FAILURE;
template <> struct STATIC_ASSERTION_FAILURE<true> { enum { value = 1 }; }; template <> struct STATIC_ASSERTION_FAILURE<true> { enum { value = 1 }; };
template<int x> struct StaticAssertTest {}; template<int x> struct StaticAssertTest {};
...@@ -130,7 +144,13 @@ template<int x> struct StaticAssertTest {}; ...@@ -130,7 +144,13 @@ template<int x> struct StaticAssertTest {};
#else #else
#define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE #define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE
#endif #endif
//!@endcond
/*! \def RAPIDJSON_STATIC_ASSERT
\brief (internal) macro to check for conditions at compile-time
\param x compile-time condition
\hideinitializer
*/
#define RAPIDJSON_STATIC_ASSERT(x) typedef ::rapidjson::StaticAssertTest<\ #define RAPIDJSON_STATIC_ASSERT(x) typedef ::rapidjson::StaticAssertTest<\
sizeof(::rapidjson::STATIC_ASSERTION_FAILURE<bool(x) >)>\ sizeof(::rapidjson::STATIC_ASSERTION_FAILURE<bool(x) >)>\
RAPIDJSON_JOIN(StaticAssertTypedef, __LINE__) RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE RAPIDJSON_JOIN(StaticAssertTypedef, __LINE__) RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE
...@@ -139,9 +159,11 @@ template<int x> struct StaticAssertTest {}; ...@@ -139,9 +159,11 @@ template<int x> struct StaticAssertTest {};
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Helpers // Helpers
//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
#define RAPIDJSON_MULTILINEMACRO_BEGIN do { #define RAPIDJSON_MULTILINEMACRO_BEGIN do {
#define RAPIDJSON_MULTILINEMACRO_END \ #define RAPIDJSON_MULTILINEMACRO_END \
} while((void)0, 0) } while((void)0, 0)
//!@endcond
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Allocators and Encodings // Allocators and Encodings
...@@ -149,6 +171,7 @@ template<int x> struct StaticAssertTest {}; ...@@ -149,6 +171,7 @@ template<int x> struct StaticAssertTest {};
#include "allocators.h" #include "allocators.h"
#include "encodings.h" #include "encodings.h"
//! main RapidJSON namespace
namespace rapidjson { namespace rapidjson {
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
...@@ -246,6 +269,7 @@ struct StreamTraits<GenericStringStream<Encoding> > { ...@@ -246,6 +269,7 @@ struct StreamTraits<GenericStringStream<Encoding> > {
enum { copyOptimization = 1 }; enum { copyOptimization = 1 };
}; };
//! String stream with UTF8 encoding.
typedef GenericStringStream<UTF8<> > StringStream; typedef GenericStringStream<UTF8<> > StringStream;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
...@@ -282,6 +306,7 @@ struct StreamTraits<GenericInsituStringStream<Encoding> > { ...@@ -282,6 +306,7 @@ struct StreamTraits<GenericInsituStringStream<Encoding> > {
enum { copyOptimization = 1 }; enum { copyOptimization = 1 };
}; };
//! Insitu string stream with UTF8 encoding.
typedef GenericInsituStringStream<UTF8<> > InsituStringStream; typedef GenericInsituStringStream<UTF8<> > InsituStringStream;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
......
...@@ -43,6 +43,8 @@ namespace rapidjson { ...@@ -43,6 +43,8 @@ namespace rapidjson {
// ParseFlag // ParseFlag
//! Combination of parseFlags //! Combination of parseFlags
/*! \see Reader::Parse, Document::Parse, Document::ParseInsitu, Document::ParseStream
*/
enum ParseFlag { enum ParseFlag {
kParseDefaultFlags = 0, //!< Default parse flags. Non-destructive parsing. Text strings are decoded into allocated buffer. kParseDefaultFlags = 0, //!< Default parse flags. Non-destructive parsing. Text strings are decoded into allocated buffer.
kParseInsituFlag = 1, //!< In-situ(destructive) parsing. kParseInsituFlag = 1, //!< In-situ(destructive) parsing.
...@@ -257,7 +259,7 @@ template<> inline void SkipWhitespace(StringStream& is) { ...@@ -257,7 +259,7 @@ template<> inline void SkipWhitespace(StringStream& is) {
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// GenericReader // GenericReader
//! SAX-style JSON parser. Use Reader for UTF8 encoding and default allocator. //! SAX-style JSON parser. Use \ref Reader for UTF8 encoding and default allocator.
/*! GenericReader parses JSON text from a stream, and send events synchronously to an /*! GenericReader parses JSON text from a stream, and send events synchronously to an
object implementing Handler concept. object implementing Handler concept.
...@@ -276,7 +278,7 @@ template<> inline void SkipWhitespace(StringStream& is) { ...@@ -276,7 +278,7 @@ template<> inline void SkipWhitespace(StringStream& is) {
template <typename SourceEncoding, typename TargetEncoding, typename Allocator = MemoryPoolAllocator<> > template <typename SourceEncoding, typename TargetEncoding, typename Allocator = MemoryPoolAllocator<> >
class GenericReader { class GenericReader {
public: public:
typedef typename SourceEncoding::Ch Ch; typedef typename SourceEncoding::Ch Ch; //!< SourceEncoding character type
//! Constructor. //! Constructor.
/*! \param allocator Optional allocator for allocating stack memory. (Only use for non-destructive parsing) /*! \param allocator Optional allocator for allocating stack memory. (Only use for non-destructive parsing)
...@@ -285,12 +287,12 @@ public: ...@@ -285,12 +287,12 @@ public:
GenericReader(Allocator* allocator = 0, size_t stackCapacity = kDefaultStackCapacity) : stack_(allocator, stackCapacity), parseErrorCode_(kParseErrorNone), errorOffset_(0) {} GenericReader(Allocator* allocator = 0, size_t stackCapacity = kDefaultStackCapacity) : stack_(allocator, stackCapacity), parseErrorCode_(kParseErrorNone), errorOffset_(0) {}
//! Parse JSON text. //! Parse JSON text.
/*! \tparam parseFlags Combination of ParseFlag. /*! \tparam parseFlags Combination of \ref ParseFlag.
\tparam InputStream Type of input stream. \tparam InputStream Type of input stream, implementing Stream concept.
\tparam Handler Type of handler which must implement Handler concept. \tparam Handler Type of handler, implementing Handler concept.
\param is Input stream to be parsed. \param is Input stream to be parsed.
\param handler The handler to receive events. \param handler The handler to receive events.
\return Whether the parsing is successful. \return Whether the parsing is successful.
*/ */
template <unsigned parseFlags, typename InputStream, typename Handler> template <unsigned parseFlags, typename InputStream, typename Handler>
bool Parse(InputStream& is, Handler& handler) { bool Parse(InputStream& is, Handler& handler) {
...@@ -321,6 +323,13 @@ public: ...@@ -321,6 +323,13 @@ public:
return !HasParseError(); return !HasParseError();
} }
//! Parse JSON text (with \ref kParseDefaultFlags)
/*! \tparam InputStream Type of input stream, implementing Stream concept
\tparam Handler Type of handler, implementing Handler concept.
\param is Input stream to be parsed.
\param handler The handler to receive events.
\return Whether the parsing is successful.
*/
template <typename InputStream, typename Handler> template <typename InputStream, typename Handler>
bool Parse(InputStream& is, Handler& handler) { bool Parse(InputStream& is, Handler& handler) {
return Parse<kParseDefaultFlags>(is, handler); return Parse<kParseDefaultFlags>(is, handler);
......
...@@ -37,6 +37,7 @@ struct GenericStringBuffer { ...@@ -37,6 +37,7 @@ struct GenericStringBuffer {
mutable internal::Stack<Allocator> stack_; mutable internal::Stack<Allocator> stack_;
}; };
//! String buffer with UTF8 encoding
typedef GenericStringBuffer<UTF8<> > StringBuffer; typedef GenericStringBuffer<UTF8<> > StringBuffer;
//! Implement specialized version of PutN() with memset() for better performance. //! Implement specialized version of PutN() with memset() for better performance.
......
...@@ -25,8 +25,9 @@ namespace rapidjson { ...@@ -25,8 +25,9 @@ namespace rapidjson {
for example Reader::Parse() and Document::Accept(). for example Reader::Parse() and Document::Accept().
\tparam OutputStream Type of output stream. \tparam OutputStream Type of output stream.
\tparam SourceEncoding Encoding of both source strings. \tparam SourceEncoding Encoding of source string.
\tparam TargetEncoding Encoding of and output stream. \tparam TargetEncoding Encoding of output stream.
\tparam Allocator Type of allocator for allocating memory of stack.
\note implements Handler concept \note implements Handler concept
*/ */
template<typename OutputStream, typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename Allocator = MemoryPoolAllocator<> > template<typename OutputStream, typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename Allocator = MemoryPoolAllocator<> >
...@@ -34,6 +35,11 @@ class Writer { ...@@ -34,6 +35,11 @@ class Writer {
public: public:
typedef typename SourceEncoding::Ch Ch; typedef typename SourceEncoding::Ch Ch;
//! Constructor
/*! \param os Output stream.
\param allocator User supplied allocator. If it is null, it will create a private one.
\param levelDepth Initial capacity of stack.
*/
Writer(OutputStream& os, Allocator* allocator = 0, size_t levelDepth = kDefaultLevelDepth) : Writer(OutputStream& os, Allocator* allocator = 0, size_t levelDepth = kDefaultLevelDepth) :
os_(os), level_stack_(allocator, levelDepth * sizeof(Level)), os_(os), level_stack_(allocator, levelDepth * sizeof(Level)),
doublePrecision_(kDefaultDoublePrecision) {} doublePrecision_(kDefaultDoublePrecision) {}
...@@ -53,8 +59,11 @@ public: ...@@ -53,8 +59,11 @@ public:
//! \see SetDoublePrecision() //! \see SetDoublePrecision()
int GetDoublePrecision() const { return doublePrecision_; } int GetDoublePrecision() const { return doublePrecision_; }
//@name Implementation of Handler /*!@name Implementation of Handler
\see Handler
*/
//@{ //@{
Writer& Null() { Prefix(kNullType); WriteNull(); return *this; } Writer& Null() { Prefix(kNullType); WriteNull(); return *this; }
Writer& Bool(bool b) { Prefix(b ? kTrueType : kFalseType); WriteBool(b); return *this; } Writer& Bool(bool b) { Prefix(b ? kTrueType : kFalseType); WriteBool(b); return *this; }
Writer& Int(int i) { Prefix(kNumberType); WriteInt(i); return *this; } Writer& Int(int i) { Prefix(kNumberType); WriteInt(i); return *this; }
...@@ -75,20 +84,6 @@ public: ...@@ -75,20 +84,6 @@ public:
*/ */
Writer& Double(double d) { Prefix(kNumberType); WriteDouble(d); return *this; } Writer& Double(double d) { Prefix(kNumberType); WriteDouble(d); return *this; }
//! Writes the given \c double value to the stream (explicit precision)
/*!
The currently set double precision is ignored in favor of the explicitly
given precision for this value.
\see Double(), SetDoublePrecision(), GetDoublePrecision()
\param d The value to be written
\param precision The number of significant digits for this value
\return The Writer itself for fluent API.
*/
Writer& Double(double d, int precision) {
int oldPrecision = GetDoublePrecision();
return SetDoublePrecision(precision).Double(d).SetDoublePrecision(oldPrecision);
}
Writer& String(const Ch* str, SizeType length, bool copy = false) { Writer& String(const Ch* str, SizeType length, bool copy = false) {
(void)copy; (void)copy;
Prefix(kStringType); Prefix(kStringType);
...@@ -133,9 +128,28 @@ public: ...@@ -133,9 +128,28 @@ public:
} }
//@} //@}
/*! @name Convenience extensions */
//@{
//! Writes the given \c double value to the stream (explicit precision)
/*!
The currently set double precision is ignored in favor of the explicitly
given precision for this value.
\see Double(), SetDoublePrecision(), GetDoublePrecision()
\param d The value to be written
\param precision The number of significant digits for this value
\return The Writer itself for fluent API.
*/
Writer& Double(double d, int precision) {
int oldPrecision = GetDoublePrecision();
return SetDoublePrecision(precision).Double(d).SetDoublePrecision(oldPrecision);
}
//! Simpler but slower overload. //! Simpler but slower overload.
Writer& String(const Ch* str) { return String(str, internal::StrLen(str)); } Writer& String(const Ch* str) { return String(str, internal::StrLen(str)); }
//@}
protected: protected:
//! Information for each nested level //! Information for each nested level
struct Level { struct Level {
......
...@@ -37,7 +37,7 @@ Users can build and run the unit tests on their platform/compiler. ...@@ -37,7 +37,7 @@ Users can build and run the unit tests on their platform/compiler.
## Installation ## Installation
RapidJSON is a header-only C++ library. Just copy the `rapidjson/include/rapidjson` folder to system or project's include path. RapidJSON is a header-only C++ library. Just copy the `include/rapidjson` folder to system or project's include path.
To build the tests and examples: To build the tests and examples:
...@@ -45,15 +45,21 @@ To build the tests and examples: ...@@ -45,15 +45,21 @@ To build the tests and examples:
2. Copy premake4 executable to RapidJSON/build (or system path) 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 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/` 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`) 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` 6. On success, the executable are generated at `rapidjson/bin`
To build the [Doxygen](http://doxygen.org) documentation:
1. Obtain and install [Doxygen](http://doxygen.org/download.html).
2. In the top-level directory, run `doxygen build/Doxyfile`.
3. Browse the generated documentation in `doc/html`.
## Usage at a glance ## 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. 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.
[simpledom.cpp](example/simpledom/simpledom.cpp) [simpledom.cpp](example/simpledom/simpledom.cpp)
```cpp ~~~~~~~~~~cpp
#include "rapidjson/document.h" #include "rapidjson/document.h"
#include "rapidjson/writer.h" #include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h" #include "rapidjson/stringbuffer.h"
...@@ -80,12 +86,12 @@ int main() { ...@@ -80,12 +86,12 @@ int main() {
std::cout << buffer.GetString() << std::endl; std::cout << buffer.GetString() << std::endl;
return 0; return 0;
} }
``` ~~~~~~~~~~
Note that this example did not handle potential errors. Note that this example did not handle potential errors.
The following diagram shows the process. The following diagram shows the process.
![simpledom](doc/diagram/simpledom.png?raw=true) ![simpledom](doc/diagram/simpledom.png)
More [examples](example/) are available. More [examples](example/) are available.
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