Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/miloyip/rapidjson.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMilo Yip <miloyip@gmail.com>2014-07-12 21:17:38 +0400
committerMilo Yip <miloyip@gmail.com>2014-07-12 21:17:38 +0400
commita5c1324da99074b8c581a6a136341eb3edbe0faa (patch)
tree9be1ee25a2343d9aa5aa2a46c781f369f192b31f /example
parent9e3ed44e859a8768665aeb47ee70b6d3ff9b4f5d (diff)
Add messagereader example
Diffstat (limited to 'example')
-rw-r--r--example/messagereader/messagereader.cpp86
1 files changed, 86 insertions, 0 deletions
diff --git a/example/messagereader/messagereader.cpp b/example/messagereader/messagereader.cpp
new file mode 100644
index 00000000..344b170b
--- /dev/null
+++ b/example/messagereader/messagereader.cpp
@@ -0,0 +1,86 @@
+// Reading a message JSON with Reader (SAX-style API).
+// The JSON should be an object with key-string pairs.
+
+#include "rapidjson/reader.h"
+#include "rapidjson/error/en.h"
+#include <iostream>
+#include <string>
+#include <map>
+
+using namespace std;
+using namespace rapidjson;
+
+typedef map<string, string> MessageMap;
+
+struct MessageHandler : public BaseReaderHandler<> {
+ MessageHandler() : state_(kExpectObjectStart) {}
+
+ bool StartObject() {
+ switch (state_) {
+ case kExpectObjectStart:
+ state_ = kExpectNameOrObjectEnd;
+ return true;
+ default:
+ return false;
+ }
+ }
+
+ bool String(const char* str, SizeType length, bool) {
+ switch (state_) {
+ case kExpectNameOrObjectEnd:
+ name_ = string(str, length);
+ state_ = kExpectValue;
+ return true;
+ case kExpectValue:
+ messages_.insert(MessageMap::value_type(name_, string(str, length)));
+ state_ = kExpectNameOrObjectEnd;
+ return true;
+ default:
+ return false;
+ }
+ }
+
+ bool EndObject(SizeType) { return state_ == kExpectNameOrObjectEnd; }
+
+ bool Default() { return false; } // All other events are invalid.
+
+ MessageMap messages_;
+ enum State {
+ kExpectObjectStart,
+ kExpectNameOrObjectEnd,
+ kExpectValue,
+ }state_;
+ std::string name_;
+};
+
+void ParseMessages(const char* json, MessageMap& messages) {
+ Reader reader;
+ MessageHandler handler;
+ StringStream ss(json);
+ if (reader.Parse(ss, handler))
+ messages.swap(handler.messages_); // Only change it if success.
+ else {
+ ParseErrorCode e = reader.GetParseErrorCode();
+ size_t o = reader.GetErrorOffset();
+ cout << "Error: " << GetParseError_En(e) << endl;;
+ cout << " at offset " << o << " near '" << string(json).substr(o, 10) << "...'" << endl;
+ }
+}
+
+int main() {
+ MessageMap messages;
+
+ const char* json1 = "{ \"greeting\" : \"Hello!\", \"farewell\" : \"bye-bye!\" }";
+ cout << json1 << endl;
+ ParseMessages(json1, messages);
+
+ for (MessageMap::const_iterator itr = messages.begin(); itr != messages.end(); ++itr)
+ cout << itr->first << ": " << itr->second << endl;
+
+ cout << endl << "Parse a JSON with invalid schema." << endl;
+ const char* json2 = "{ \"greeting\" : \"Hello!\", \"farewell\" : \"bye-bye!\", \"foo\" : {} }";
+ cout << json2 << endl;
+ ParseMessages(json2, messages);
+
+ return 0;
+}