• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# SAX
2
3The term "SAX" originated from [Simple API for XML](http://en.wikipedia.org/wiki/Simple_API_for_XML). We borrowed this term for JSON parsing and generation.
4
5In RapidJSON, `Reader` (typedef of `GenericReader<...>`) is the SAX-style parser for JSON, and `Writer` (typedef of `GenericWriter<...>`) is the SAX-style generator for JSON.
6
7[TOC]
8
9# Reader {#Reader}
10
11`Reader` parses a JSON from a stream. While it reads characters from the stream, it analyze the characters according to the syntax of JSON, and publish events to a handler.
12
13For example, here is a JSON.
14
15~~~~~~~~~~js
16{
17    "hello": "world",
18    "t": true ,
19    "f": false,
20    "n": null,
21    "i": 123,
22    "pi": 3.1416,
23    "a": [1, 2, 3, 4]
24}
25~~~~~~~~~~
26
27While a `Reader` parses this JSON, it publishes the following events to the handler sequentially:
28
29~~~~~~~~~~
30StartObject()
31Key("hello", 5, true)
32String("world", 5, true)
33Key("t", 1, true)
34Bool(true)
35Key("f", 1, true)
36Bool(false)
37Key("n", 1, true)
38Null()
39Key("i")
40UInt(123)
41Key("pi")
42Double(3.1416)
43Key("a")
44StartArray()
45Uint(1)
46Uint(2)
47Uint(3)
48Uint(4)
49EndArray(4)
50EndObject(7)
51~~~~~~~~~~
52
53These events can be easily matched with the JSON, except some event parameters need further explanation. Let's see the `simplereader` example which produces exactly the same output as above:
54
55~~~~~~~~~~cpp
56#include "rapidjson/reader.h"
57#include <iostream>
58
59using namespace rapidjson;
60using namespace std;
61
62struct MyHandler {
63    bool Null() { cout << "Null()" << endl; return true; }
64    bool Bool(bool b) { cout << "Bool(" << boolalpha << b << ")" << endl; return true; }
65    bool Int(int i) { cout << "Int(" << i << ")" << endl; return true; }
66    bool Uint(unsigned u) { cout << "Uint(" << u << ")" << endl; return true; }
67    bool Int64(int64_t i) { cout << "Int64(" << i << ")" << endl; return true; }
68    bool Uint64(uint64_t u) { cout << "Uint64(" << u << ")" << endl; return true; }
69    bool Double(double d) { cout << "Double(" << d << ")" << endl; return true; }
70    bool String(const char* str, SizeType length, bool copy) {
71        cout << "String(" << str << ", " << length << ", " << boolalpha << copy << ")" << endl;
72        return true;
73    }
74    bool StartObject() { cout << "StartObject()" << endl; return true; }
75    bool Key(const char* str, SizeType length, bool copy) {
76        cout << "Key(" << str << ", " << length << ", " << boolalpha << copy << ")" << endl;
77        return true;
78    }
79    bool EndObject(SizeType memberCount) { cout << "EndObject(" << memberCount << ")" << endl; return true; }
80    bool StartArray() { cout << "StartArray()" << endl; return true; }
81    bool EndArray(SizeType elementCount) { cout << "EndArray(" << elementCount << ")" << endl; return true; }
82};
83
84void main() {
85    const char json[] = " { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } ";
86
87    MyHandler handler;
88    Reader reader;
89    StringStream ss(json);
90    reader.Parse(ss, handler);
91}
92~~~~~~~~~~
93
94Note that, RapidJSON uses template to statically bind the `Reader` type and the handler type, instead of using class with virtual functions. This paradigm can improve the performance by inlining functions.
95
96## Handler {#Handler}
97
98As the previous example showed, user needs to implement a handler, which consumes the events (function calls) from `Reader`. The handler must contain the following member functions.
99
100~~~~~~~~~~cpp
101class Handler {
102    bool Null();
103    bool Bool(bool b);
104    bool Int(int i);
105    bool Uint(unsigned i);
106    bool Int64(int64_t i);
107    bool Uint64(uint64_t i);
108    bool Double(double d);
109    bool String(const Ch* str, SizeType length, bool copy);
110    bool StartObject();
111    bool Key(const Ch* str, SizeType length, bool copy);
112    bool EndObject(SizeType memberCount);
113    bool StartArray();
114    bool EndArray(SizeType elementCount);
115};
116~~~~~~~~~~
117
118`Null()` is called when the `Reader` encounters a JSON null value.
119
120`Bool(bool)` is called when the `Reader` encounters a JSON true or false value.
121
122When the `Reader` encounters a JSON number, it chooses a suitable C++ type mapping. And then it calls *one* function out of `Int(int)`, `Uint(unsigned)`, `Int64(int64_t)`, `Uint64(uint64_t)` and `Double(double)`.
123
124`String(const char* str, SizeType length, bool copy)` is called when the `Reader` encounters a string. The first parameter is pointer to the string. The second parameter is the length of the string (excluding the null terminator). Note that RapidJSON supports null character `'\0'` inside a string. If such situation happens, `strlen(str) < length`. The last `copy` indicates whether the handler needs to make a copy of the string. For normal parsing, `copy = true`. Only when *insitu* parsing is used, `copy = false`. And beware that, the character type depends on the target encoding, which will be explained later.
125
126When the `Reader` encounters the beginning of an object, it calls `StartObject()`. An object in JSON is a set of name-value pairs. If the object contains members it first calls `Key()` for the name of member, and then calls functions depending on the type of the value. These calls of name-value pairs repeats until calling `EndObject(SizeType memberCount)`. Note that the `memberCount` parameter is just an aid for the handler, user may not need this parameter.
127
128Array is similar to object but simpler. At the beginning of an array, the `Reader` calls `BeginArary()`. If there is elements, it calls functions according to the types of element. Similarly, in the last call `EndArray(SizeType elementCount)`, the parameter `elementCount` is just an aid for the handler.
129
130Every handler functions returns a `bool`. Normally it should returns `true`. If the handler encounters an error, it can return `false` to notify event publisher to stop further processing.
131
132For example, when we parse a JSON with `Reader` and the handler detected that the JSON does not conform to the required schema, then the handler can return `false` and let the `Reader` stop further parsing. And the `Reader` will be in error state with error code `kParseErrorTermination`.
133
134## GenericReader {#GenericReader}
135
136As mentioned before, `Reader` is a typedef of a template class `GenericReader`:
137
138~~~~~~~~~~cpp
139namespace rapidjson {
140
141template <typename SourceEncoding, typename TargetEncoding, typename Allocator = MemoryPoolAllocator<> >
142class GenericReader {
143    // ...
144};
145
146typedef GenericReader<UTF8<>, UTF8<> > Reader;
147
148} // namespace rapidjson
149~~~~~~~~~~
150
151The `Reader` uses UTF-8 as both source and target encoding. The source encoding means the encoding in the JSON stream. The target encoding means the encoding of the `str` parameter in `String()` calls. For example, to parse a UTF-8 stream and outputs UTF-16 string events, you can define a reader by:
152
153~~~~~~~~~~cpp
154GenericReader<UTF8<>, UTF16<> > reader;
155~~~~~~~~~~
156
157Note that, the default character type of `UTF16` is `wchar_t`. So this `reader`needs to call `String(const wchar_t*, SizeType, bool)` of the handler.
158
159The third template parameter `Allocator` is the allocator type for internal data structure (actually a stack).
160
161## Parsing {#Parsing}
162
163The one and only one function of `Reader` is to parse JSON.
164
165~~~~~~~~~~cpp
166template <unsigned parseFlags, typename InputStream, typename Handler>
167bool Parse(InputStream& is, Handler& handler);
168
169// with parseFlags = kDefaultParseFlags
170template <typename InputStream, typename Handler>
171bool Parse(InputStream& is, Handler& handler);
172~~~~~~~~~~
173
174If an error occurs during parsing, it will return `false`. User can also calls `bool HasParseEror()`, `ParseErrorCode GetParseErrorCode()` and `size_t GetErrorOffset()` to obtain the error states. Actually `Document` uses these `Reader` functions to obtain parse errors. Please refer to [DOM](doc/dom.md) for details about parse error.
175
176# Writer {#Writer}
177
178`Reader` converts (parses) JSON into events. `Writer` does exactly the opposite. It converts events into JSON.
179
180`Writer` is very easy to use. If your application only need to converts some data into JSON, it may be a good choice to use `Writer` directly, instead of building a `Document` and then stringifying it with a `Writer`.
181
182In `simplewriter` example, we do exactly the reverse of `simplereader`.
183
184~~~~~~~~~~cpp
185#include "rapidjson/writer.h"
186#include "rapidjson/stringbuffer.h"
187#include <iostream>
188
189using namespace rapidjson;
190using namespace std;
191
192void main() {
193    StringBuffer s;
194    Writer<StringBuffer> writer(s);
195
196    writer.StartObject();
197    writer.Key("hello");
198    writer.String("world");
199    writer.Key("t");
200    writer.Bool(true);
201    writer.Key("f");
202    writer.Bool(false);
203    writer.Key("n");
204    writer.Null();
205    writer.Key("i");
206    writer.Uint(123);
207    writer.Key("pi");
208    writer.Double(3.1416);
209    writer.Key("a");
210    writer.StartArray();
211    for (unsigned i = 0; i < 4; i++)
212        writer.Uint(i);
213    writer.EndArray();
214    writer.EndObject();
215
216    cout << s.GetString() << endl;
217}
218~~~~~~~~~~
219
220~~~~~~~~~~
221{"hello":"world","t":true,"f":false,"n":null,"i":123,"pi":3.1416,"a":[0,1,2,3]}
222~~~~~~~~~~
223
224There are two `String()` and `Key()` overloads. One is the same as defined in handler concept with 3 parameters. It can handle string with null characters. Another one is the simpler version used in the above example.
225
226Note that, the example code does not pass any parameters in `EndArray()` and `EndObject()`. An `SizeType` can be passed but it will be simply ignored by `Writer`.
227
228You may doubt that, why not just using `sprintf()` or `std::stringstream` to build a JSON?
229
230There are various reasons:
2311. `Writer` must output a well-formed JSON. If there is incorrect event sequence (e.g. `Int()` just after `StartObject()`), it generates assertion fail in debug mode.
2322. `Writer::String()` can handle string escaping (e.g. converting code point `U+000A` to `\n`) and Unicode transcoding.
2333. `Writer` handles number output consistently.
2344. `Writer` implements the event handler concept. It can be used to handle events from `Reader`, `Document` or other event publisher.
2355. `Writer` can be optimized for different platforms.
236
237Anyway, using `Writer` API is even simpler than generating a JSON by ad hoc methods.
238
239## Template {#WriterTemplate}
240
241`Writer` has a minor design difference to `Reader`. `Writer` is a template class, not a typedef. There is no `GenericWriter`. The following is the declaration.
242
243~~~~~~~~~~cpp
244namespace rapidjson {
245
246template<typename OutputStream, typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename Allocator = CrtAllocator<> >
247class Writer {
248public:
249    Writer(OutputStream& os, Allocator* allocator = 0, size_t levelDepth = kDefaultLevelDepth)
250// ...
251};
252
253} // namespace rapidjson
254~~~~~~~~~~
255
256The `OutputStream` template parameter is the type of output stream. It cannot be deduced and must be specified by user.
257
258The `SourceEncoding` template parameter specifies the encoding to be used in `String(const Ch*, ...)`.
259
260The `TargetEncoding` template parameter specifies the encoding in the output stream.
261
262The last one, `Allocator` is the type of allocator, which is used for allocating internal data structure (a stack).
263
264Besides, the constructor of `Writer` has a `levelDepth` parameter. This parameter affects the initial memory allocated for storing information per hierarchy level.
265
266## PrettyWriter {#PrettyWriter}
267
268While the output of `Writer` is the most condensed JSON without white-spaces, suitable for network transfer or storage, it is not easily readable by human.
269
270Therefore, RapidJSON provides a `PrettyWriter`, which adds indentation and line feeds in the output.
271
272The usage of `PrettyWriter` is exactly the same as `Writer`, expect that `PrettyWriter` provides a `SetIndent(Ch indentChar, unsigned indentCharCount)` function. The default is 4 spaces.
273
274## Completeness and Reset {#CompletenessReset}
275
276A `Writer` can only output a single JSON, which can be any JSON type at the root. Once the singular event for root (e.g. `String()`), or the last matching `EndObject()` or `EndArray()` event, is handled, the output JSON is well-formed and complete. User can detect this state by calling `Writer::IsComplete()`.
277
278When a JSON is complete, the `Writer` cannot accept any new events. Otherwise the output will be invalid (i.e. having more than one root). To reuse the `Writer` object, user can call `Writer::Reset(OutputStream& os)` to reset all internal states of the `Writer` with a new output stream.
279
280# Techniques {#Techniques}
281
282## Parsing JSON to Custom Data Structure {#CustomDataStructure}
283
284`Document`'s parsing capability is completely based on `Reader`. Actually `Document` is a handler which receives events from a reader to build a DOM during parsing.
285
286User may uses `Reader` to build other data structures directly. This eliminates building of DOM, thus reducing memory and improving performance.
287
288In the following `messagereader` example, `ParseMessages()` parses a JSON which should be an object with key-string pairs.
289
290~~~~~~~~~~cpp
291#include "rapidjson/reader.h"
292#include "rapidjson/error/en.h"
293#include <iostream>
294#include <string>
295#include <map>
296
297using namespace std;
298using namespace rapidjson;
299
300typedef map<string, string> MessageMap;
301
302struct MessageHandler
303    : public BaseReaderHandler<UTF8<>, MessageHandler> {
304    MessageHandler() : state_(kExpectObjectStart) {
305    }
306
307    bool StartObject() {
308        switch (state_) {
309        case kExpectObjectStart:
310            state_ = kExpectNameOrObjectEnd;
311            return true;
312        default:
313            return false;
314        }
315    }
316
317    bool String(const char* str, SizeType length, bool) {
318        switch (state_) {
319        case kExpectNameOrObjectEnd:
320            name_ = string(str, length);
321            state_ = kExpectValue;
322            return true;
323        case kExpectValue:
324            messages_.insert(MessageMap::value_type(name_, string(str, length)));
325            state_ = kExpectNameOrObjectEnd;
326            return true;
327        default:
328            return false;
329        }
330    }
331
332    bool EndObject(SizeType) { return state_ == kExpectNameOrObjectEnd; }
333
334    bool Default() { return false; } // All other events are invalid.
335
336    MessageMap messages_;
337    enum State {
338        kExpectObjectStart,
339        kExpectNameOrObjectEnd,
340        kExpectValue,
341    }state_;
342    std::string name_;
343};
344
345void ParseMessages(const char* json, MessageMap& messages) {
346    Reader reader;
347    MessageHandler handler;
348    StringStream ss(json);
349    if (reader.Parse(ss, handler))
350        messages.swap(handler.messages_);   // Only change it if success.
351    else {
352        ParseErrorCode e = reader.GetParseErrorCode();
353        size_t o = reader.GetErrorOffset();
354        cout << "Error: " << GetParseError_En(e) << endl;;
355        cout << " at offset " << o << " near '" << string(json).substr(o, 10) << "...'" << endl;
356    }
357}
358
359int main() {
360    MessageMap messages;
361
362    const char* json1 = "{ \"greeting\" : \"Hello!\", \"farewell\" : \"bye-bye!\" }";
363    cout << json1 << endl;
364    ParseMessages(json1, messages);
365
366    for (MessageMap::const_iterator itr = messages.begin(); itr != messages.end(); ++itr)
367        cout << itr->first << ": " << itr->second << endl;
368
369    cout << endl << "Parse a JSON with invalid schema." << endl;
370    const char* json2 = "{ \"greeting\" : \"Hello!\", \"farewell\" : \"bye-bye!\", \"foo\" : {} }";
371    cout << json2 << endl;
372    ParseMessages(json2, messages);
373
374    return 0;
375}
376~~~~~~~~~~
377
378~~~~~~~~~~
379{ "greeting" : "Hello!", "farewell" : "bye-bye!" }
380farewell: bye-bye!
381greeting: Hello!
382
383Parse a JSON with invalid schema.
384{ "greeting" : "Hello!", "farewell" : "bye-bye!", "foo" : {} }
385Error: Terminate parsing due to Handler error.
386 at offset 59 near '} }...'
387~~~~~~~~~~
388
389The first JSON (`json1`) was successfully parsed into `MessageMap`. Since `MessageMap` is a `std::map`, the printing order are sorted by the key. This order is different from the JSON's order.
390
391In the second JSON (`json2`), `foo`'s value is an empty object. As it is an object, `MessageHandler::StartObject()` will be called. However, at that moment `state_ = kExpectValue`, so that function returns `false` and cause the parsing process be terminated. The error code is `kParseErrorTermination`.
392
393## Filtering of JSON {#Filtering}
394
395As mentioned earlier, `Writer` can handle the events published by `Reader`. `condense` example simply set a `Writer` as handler of a `Reader`, so it can remove all white-spaces in JSON. `pretty` example uses the same relationship, but replacing `Writer` by `PrettyWriter`. So `pretty` can be used to reformat a JSON with indentation and line feed.
396
397Actually, we can add intermediate layer(s) to filter the contents of JSON via these SAX-style API. For example, `capitalize` example capitalize all strings in a JSON.
398
399~~~~~~~~~~cpp
400#include "rapidjson/reader.h"
401#include "rapidjson/writer.h"
402#include "rapidjson/filereadstream.h"
403#include "rapidjson/filewritestream.h"
404#include "rapidjson/error/en.h"
405#include <vector>
406#include <cctype>
407
408using namespace rapidjson;
409
410template<typename OutputHandler>
411struct CapitalizeFilter {
412    CapitalizeFilter(OutputHandler& out) : out_(out), buffer_() {
413    }
414
415    bool Null() { return out_.Null(); }
416    bool Bool(bool b) { return out_.Bool(b); }
417    bool Int(int i) { return out_.Int(i); }
418    bool Uint(unsigned u) { return out_.Uint(u); }
419    bool Int64(int64_t i) { return out_.Int64(i); }
420    bool Uint64(uint64_t u) { return out_.Uint64(u); }
421    bool Double(double d) { return out_.Double(d); }
422    bool String(const char* str, SizeType length, bool) {
423        buffer_.clear();
424        for (SizeType i = 0; i < length; i++)
425            buffer_.push_back(std::toupper(str[i]));
426        return out_.String(&buffer_.front(), length, true); // true = output handler need to copy the string
427    }
428    bool StartObject() { return out_.StartObject(); }
429    bool Key(const char* str, SizeType length, bool copy) { return String(str, length, copy); }
430    bool EndObject(SizeType memberCount) { return out_.EndObject(memberCount); }
431    bool StartArray() { return out_.StartArray(); }
432    bool EndArray(SizeType elementCount) { return out_.EndArray(elementCount); }
433
434    OutputHandler& out_;
435    std::vector<char> buffer_;
436};
437
438int main(int, char*[]) {
439    // Prepare JSON reader and input stream.
440    Reader reader;
441    char readBuffer[65536];
442    FileReadStream is(stdin, readBuffer, sizeof(readBuffer));
443
444    // Prepare JSON writer and output stream.
445    char writeBuffer[65536];
446    FileWriteStream os(stdout, writeBuffer, sizeof(writeBuffer));
447    Writer<FileWriteStream> writer(os);
448
449    // JSON reader parse from the input stream and let writer generate the output.
450    CapitalizeFilter<Writer<FileWriteStream> > filter(writer);
451    if (!reader.Parse(is, filter)) {
452        fprintf(stderr, "\nError(%u): %s\n", (unsigned)reader.GetErrorOffset(), GetParseError_En(reader.GetParseErrorCode()));
453        return 1;
454    }
455
456    return 0;
457}
458~~~~~~~~~~
459
460Note that, it is incorrect to simply capitalize the JSON as a string. For example:
461~~~~~~~~~~
462["Hello\nWorld"]
463~~~~~~~~~~
464
465Simply capitalizing the whole JSON would contain incorrect escape character:
466~~~~~~~~~~
467["HELLO\NWORLD"]
468~~~~~~~~~~
469
470The correct result by `capitalize`:
471~~~~~~~~~~
472["HELLO\nWORLD"]
473~~~~~~~~~~
474
475More complicated filters can be developed. However, since SAX-style API can only provide information about a single event at a time, user may need to book-keeping the contextual information (e.g. the path from root value, storage of other related values). Some processing may be easier to be implemented in DOM than SAX.
476