Commit 05811e73 authored by Milo Yip's avatar Milo Yip

Update SAX documentation

parent 2579eca7
...@@ -50,7 +50,7 @@ EndArray(4) ...@@ -50,7 +50,7 @@ EndArray(4)
EndObject(7) EndObject(7)
~~~~~~~~~~ ~~~~~~~~~~
These events can be easily match up with the JSON, except some event parameters need further explanation. Let's see the code which produces exactly the same output as above: These events can be easily match up with the JSON, except some event parameters need further explanation. Let's see the simplereader example which produces exactly the same output as above:
~~~~~~~~~~cpp ~~~~~~~~~~cpp
#include "rapidjson/reader.h" #include "rapidjson/reader.h"
...@@ -60,26 +60,28 @@ using namespace rapidjson; ...@@ -60,26 +60,28 @@ using namespace rapidjson;
using namespace std; using namespace std;
struct MyHandler { struct MyHandler {
void Null() { cout << "Null()" << endl; } bool Null() { cout << "Null()" << endl; return true; }
void Bool(bool b) { cout << "Bool(" << (b ? "true" : "false") << ")" << endl; } bool Bool(bool b) { cout << "Bool(" << boolalpha << b << ")" << endl; return true; }
void Int(int i) { cout << "Int(" << i << ")" << endl; } bool Int(int i) { cout << "Int(" << i << ")" << endl; return true; }
void Uint(unsigned u) { cout << "Uint(" << u << ")" << endl; } bool Uint(unsigned u) { cout << "Uint(" << u << ")" << endl; return true; }
void Int64(int64_t i) { cout << "Int64(" << i << ")" << endl; } bool Int64(int64_t i) { cout << "Int64(" << i << ")" << endl; return true; }
void Uint64(uint64_t u) { cout << "Uint64(" << u << ")" << endl; } bool Uint64(uint64_t u) { cout << "Uint64(" << u << ")" << endl; return true; }
void Double(double d) { { cout << "Double(" << d << ")" << endl; } bool Double(double d) { cout << "Double(" << d << ")" << endl; return true; }
void String(const char* str, SizeType length, bool copy) { bool String(const char* str, SizeType length, bool copy) {
cout << "String(" << str << ", " << length << ", " << (b ? "true" : "false") << ")" << endl; } cout << "String(" << str << ", " << length << ", " << boolalpha << copy << ")" << endl;
void StartObject() { cout << "StartObject()" << endl; } return true;
void EndObject(SizeType memberCount) { cout << "EndObject(" << memberCount << ")" << endl; } }
void StartArray() { cout << "StartArray()" << endl; } bool StartObject() { cout << "StartObject()" << endl; return true; }
void EndArray(SizeType elementCount) { cout << "EndArray(" << elementCount << ")" << endl; } bool EndObject(SizeType memberCount) { cout << "EndObject(" << memberCount << ")" << endl; return true; }
bool StartArray() { cout << "StartArray()" << endl; return true; }
bool EndArray(SizeType elementCount) { cout << "EndArray(" << elementCount << ")" << endl; return true; }
}; };
void main() { void main() {
const char* json = "..."; const char json[] = " { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } ";
MyHandler handler; MyHandler handler;
Reader<MyHandler> reader; Reader reader;
StringStream ss(json); StringStream ss(json);
reader.Parse(ss, handler); reader.Parse(ss, handler);
} }
...@@ -93,20 +95,18 @@ As the previous example showed, user needs to implement a handler, which consume ...@@ -93,20 +95,18 @@ As the previous example showed, user needs to implement a handler, which consume
~~~~~~~~~~cpp ~~~~~~~~~~cpp
concept Handler { concept Handler {
typename Ch; bool Null();
bool Bool(bool b);
void Null(); bool Int(int i);
void Bool(bool b); bool Uint(unsigned i);
void Int(int i); bool Int64(int64_t i);
void Uint(unsigned i); bool Uint64(uint64_t i);
void Int64(int64_t i); bool Double(double d);
void Uint64(uint64_t i); bool String(const Ch* str, SizeType length, bool copy);
void Double(double d); bool StartObject();
void String(const Ch* str, SizeType length, bool copy); bool EndObject(SizeType memberCount);
void StartObject(); bool StartArray();
void EndObject(SizeType memberCount); bool EndArray(SizeType elementCount);
void StartArray();
void EndArray(SizeType elementCount);
}; };
~~~~~~~~~~ ~~~~~~~~~~
...@@ -122,6 +122,10 @@ When the `Reader` encounters the beginning of an object, it calls `StartObject() ...@@ -122,6 +122,10 @@ When the `Reader` encounters the beginning of an object, it calls `StartObject()
Array 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. Array 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.
Every 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.
For 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`.
## GenericReader {#GenericReader} ## GenericReader {#GenericReader}
As mentioned before, `Reader` is a typedef of a template class `GenericReader`: As mentioned before, `Reader` is a typedef of a template class `GenericReader`:
......
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