Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
messagereader.cpp
Go to the documentation of this file.
1// Reading a message JSON with Reader (SAX-style API).
2// The JSON should be an object with key-string pairs.
3
4#include "rapidjson/reader.h"
6#include <iostream>
7#include <string>
8#include <map>
9
10using namespace std;
11using namespace rapidjson;
12
13typedef map<string, string> MessageMap;
14
15#if defined(__GNUC__)
16RAPIDJSON_DIAG_PUSH
17RAPIDJSON_DIAG_OFF(effc++)
18#endif
19
20#ifdef __clang__
21RAPIDJSON_DIAG_PUSH
22RAPIDJSON_DIAG_OFF(switch-enum)
23#endif
24
26 : public BaseReaderHandler<UTF8<>, MessageHandler> {
28
29 bool StartObject() {
30 switch (state_) {
33 return true;
34 default:
35 return false;
36 }
37 }
38
39 bool String(const char* str, SizeType length, bool) {
40 switch (state_) {
42 name_ = string(str, length);
44 return true;
45 case kExpectValue:
46 messages_.insert(MessageMap::value_type(name_, string(str, length)));
48 return true;
49 default:
50 return false;
51 }
52 }
53
55
56 bool Default() { return false; } // All other events are invalid.
57
64 std::string name_;
65};
66
67#if defined(__GNUC__)
68RAPIDJSON_DIAG_POP
69#endif
70
71#ifdef __clang__
72RAPIDJSON_DIAG_POP
73#endif
74
75static void ParseMessages(const char* json, MessageMap& messages) {
76 Reader reader;
77 MessageHandler handler;
78 StringStream ss(json);
79 if (reader.Parse(ss, handler))
80 messages.swap(handler.messages_); // Only change it if success.
81 else {
83 size_t o = reader.GetErrorOffset();
84 cout << "Error: " << GetParseError_En(e) << endl;;
85 cout << " at offset " << o << " near '" << string(json).substr(o, 10) << "...'" << endl;
86 }
87}
88
89int main() {
90 MessageMap messages;
91
92 const char* json1 = "{ \"greeting\" : \"Hello!\", \"farewell\" : \"bye-bye!\" }";
93 cout << json1 << endl;
94 ParseMessages(json1, messages);
95
96 for (MessageMap::const_iterator itr = messages.begin(); itr != messages.end(); ++itr)
97 cout << itr->first << ": " << itr->second << endl;
98
99 cout << endl << "Parse a JSON with invalid schema." << endl;
100 const char* json2 = "{ \"greeting\" : \"Hello!\", \"farewell\" : \"bye-bye!\", \"foo\" : {} }";
101 cout << json2 << endl;
102 ParseMessages(json2, messages);
103
104 return 0;
105}
ParseResult Parse(InputStream &is, Handler &handler)
Parse JSON text.
Definition reader.h:557
ParseErrorCode GetParseErrorCode() const
Get the ParseErrorCode of last parsing.
Definition reader.h:683
size_t GetErrorOffset() const
Get the position of last parsing error in input, 0 otherwise.
Definition reader.h:686
RAPIDJSON_NAMESPACE_BEGIN const RAPIDJSON_ERROR_CHARTYPE * GetParseError_En(ParseErrorCode parseErrorCode)
Maps error code of parsing into error message.
Definition en.h:36
ParseErrorCode
Error code of parsing.
Definition error.h:64
map< string, string > MessageMap
int main()
main RapidJSON namespace
Definition name.hpp:106
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition rapidjson.h:384
Read-only string stream.
Definition stream.h:154
MessageMap messages_
std::string name_
enum MessageHandler::State state_
bool EndObject(SizeType)
bool String(const char *str, SizeType length, bool)