| 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" |
| 5 |
#include "rapidjson/error/en.h" |
| 6 |
#include <iostream> |
| 7 |
#include <string> |
| 8 |
#include <map> |
| 9 |
|
| 10 |
using namespace std; |
| 11 |
using namespace rapidjson; |
| 12 |
|
| 13 |
typedef map<string, string> MessageMap; |
| 14 |
|
| 15 |
#if defined(__GNUC__) |
| 16 |
RAPIDJSON_DIAG_PUSH |
| 17 |
RAPIDJSON_DIAG_OFF(effc++) |
| 18 |
#endif |
| 19 |
|
| 20 |
#ifdef __clang__ |
| 21 |
RAPIDJSON_DIAG_PUSH |
| 22 |
RAPIDJSON_DIAG_OFF(switch-enum) |
| 23 |
#endif |
| 24 |
|
| 25 |
struct MessageHandler |
| 26 |
: public BaseReaderHandler<UTF8<>, MessageHandler> { |
| 27 |
MessageHandler() : messages_(), state_(kExpectObjectStart), name_() {} |
| 28 |
|
| 29 |
bool StartObject() { |
| 30 |
switch (state_) { |
| 31 |
case kExpectObjectStart: |
| 32 |
state_ = kExpectNameOrObjectEnd; |
| 33 |
return true; |
| 34 |
default: |
| 35 |
return false; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
bool String(const char* str, SizeType length, bool) { |
| 40 |
switch (state_) { |
| 41 |
case kExpectNameOrObjectEnd: |
| 42 |
name_ = string(str, length); |
| 43 |
state_ = kExpectValue; |
| 44 |
return true; |
| 45 |
case kExpectValue: |
| 46 |
messages_.insert(MessageMap::value_type(name_, string(str, length))); |
| 47 |
state_ = kExpectNameOrObjectEnd; |
| 48 |
return true; |
| 49 |
default: |
| 50 |
return false; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
bool EndObject(SizeType) { return state_ == kExpectNameOrObjectEnd; } |
| 55 |
|
| 56 |
bool Default() { return false; } // All other events are invalid. |
| 57 |
|
| 58 |
MessageMap messages_; |
| 59 |
enum State { |
| 60 |
kExpectObjectStart, |
| 61 |
kExpectNameOrObjectEnd, |
| 62 |
kExpectValue |
| 63 |
}state_; |
| 64 |
std::string name_; |
| 65 |
}; |
| 66 |
|
| 67 |
#if defined(__GNUC__) |
| 68 |
RAPIDJSON_DIAG_POP |
| 69 |
#endif |
| 70 |
|
| 71 |
#ifdef __clang__ |
| 72 |
RAPIDJSON_DIAG_POP |
| 73 |
#endif |
| 74 |
|
| 75 |
static 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 { |
| 82 |
ParseErrorCode e = reader.GetParseErrorCode(); |
| 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 |
|
| 89 |
int 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 |
} |