messagereader.cpp 2.62 KB
Newer Older
Milo Yip's avatar
Milo Yip committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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;

15 16 17 18 19
#if defined(__GNUC__)
RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(effc++)
#endif

20 21
struct MessageHandler
    : public BaseReaderHandler<UTF8<>, MessageHandler> {
22
    MessageHandler() : messages_(), state_(kExpectObjectStart), name_() {}
Milo Yip's avatar
Milo Yip committed
23 24

    bool StartObject() {
25 26 27 28 29 30 31
        switch (state_) {
        case kExpectObjectStart:
            state_ = kExpectNameOrObjectEnd;
            return true;
        default:
            return false;
        }
Milo Yip's avatar
Milo Yip committed
32 33 34
    }

    bool String(const char* str, SizeType length, bool) {
35 36
        switch (state_) {
        case kExpectNameOrObjectEnd:
Milo Yip's avatar
Milo Yip committed
37
            name_ = string(str, length);
38 39 40 41 42 43 44 45 46
            state_ = kExpectValue;
            return true;
        case kExpectValue:
            messages_.insert(MessageMap::value_type(name_, string(str, length)));
            state_ = kExpectNameOrObjectEnd;
            return true;
        default:
            return false;
        }
Milo Yip's avatar
Milo Yip committed
47 48 49 50
    }

    bool EndObject(SizeType) { return state_ == kExpectNameOrObjectEnd; }

51
    bool Default() { return false; } // All other events are invalid.
Milo Yip's avatar
Milo Yip committed
52

53
    MessageMap messages_;
Milo Yip's avatar
Milo Yip committed
54 55 56
    enum State {
        kExpectObjectStart,
        kExpectNameOrObjectEnd,
57
        kExpectValue
Milo Yip's avatar
Milo Yip committed
58 59 60 61
    }state_;
    std::string name_;
};

62 63 64 65
#if defined(__GNUC__)
RAPIDJSON_DIAG_POP
#endif

Milo Yip's avatar
Milo Yip committed
66 67 68 69
void ParseMessages(const char* json, MessageMap& messages) {
    Reader reader;
    MessageHandler handler;
    StringStream ss(json);
70 71 72 73 74 75 76 77
    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;
    }
Milo Yip's avatar
Milo Yip committed
78 79 80 81 82
}

int main() {
    MessageMap messages;

83 84
    const char* json1 = "{ \"greeting\" : \"Hello!\", \"farewell\" : \"bye-bye!\" }";
    cout << json1 << endl;
Milo Yip's avatar
Milo Yip committed
85 86 87 88 89
    ParseMessages(json1, messages);

    for (MessageMap::const_iterator itr = messages.begin(); itr != messages.end(); ++itr)
        cout << itr->first << ": " << itr->second << endl;

90 91 92 93
    cout << endl << "Parse a JSON with invalid schema." << endl;
    const char* json2 = "{ \"greeting\" : \"Hello!\", \"farewell\" : \"bye-bye!\", \"foo\" : {} }";
    cout << json2 << endl;
    ParseMessages(json2, messages);
Milo Yip's avatar
Milo Yip committed
94 95 96

    return 0;
}