lookaheadparser.cpp 9.24 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
#include "rapidjson/reader.h"
#include "rapidjson/document.h"
#include <iostream>

RAPIDJSON_DIAG_PUSH
#ifdef __GNUC__
RAPIDJSON_DIAG_OFF(effc++)
#endif

// This example demonstrates JSON token-by-token parsing with an API that is
// more direct; you don't need to design your logic around a handler object and
// callbacks. Instead, you retrieve values from the JSON stream by calling
// GetInt(), GetDouble(), GetString() and GetBool(), traverse into structures
// by calling EnterObject() and EnterArray(), and skip over unwanted data by
// calling SkipValue(). When you know your JSON's structure, this can be quite
// convenient.
//
// If you aren't sure of what's next in the JSON data, you can use PeekType() and
// PeekValue() to look ahead to the next object before reading it.
//
// If you call the wrong retrieval method--e.g. GetInt when the next JSON token is
// not an int, EnterObject or EnterArray when there isn't actually an object or array
// to read--the stream parsing will end immediately and no more data will be delivered.
//
// After calling EnterObject, you retrieve keys via NextObjectKey() and values via
// the normal getters. When NextObjectKey() returns null, you have exited the
// object, or you can call SkipObject() to skip to the end of the object
// immediately. If you fetch the entire object (i.e. NextObjectKey() returned  null),
// you should not call SkipObject().
//
// After calling EnterArray(), you must alternate between calling NextArrayValue()
// to see if the array has more data, and then retrieving values via the normal
// getters. You can call SkipArray() to skip to the end of the array immediately.
// If you fetch the entire array (i.e. NextArrayValue() returned null),
// you should not call SkipArray().
//
// This parser uses in-situ strings, so the JSON buffer will be altered during the
// parse.

using namespace rapidjson;


class LookaheadParserHandler {
public:
    bool Null() { st_ = kHasNull; v_.SetNull(); return true; }
    bool Bool(bool b) { st_ = kHasBool; v_.SetBool(b); return true; }
    bool Int(int i) { st_ = kHasNumber; v_.SetInt(i); return true; }
    bool Uint(unsigned u) { st_ = kHasNumber; v_.SetUint(u); return true; }
    bool Int64(int64_t i) { st_ = kHasNumber; v_.SetInt64(i); return true; }
    bool Uint64(uint64_t u) { st_ = kHasNumber; v_.SetUint64(u); return true; }
    bool Double(double d) { st_ = kHasNumber; v_.SetDouble(d); return true; }
    bool RawNumber(const char*, SizeType, bool) { return false; }
    bool String(const char* str, SizeType length, bool) { st_ = kHasString; v_.SetString(str, length); return true; }
    bool StartObject() { st_ = kEnteringObject; return true; }
    bool Key(const char* str, SizeType length, bool) { st_ = kHasKey; v_.SetString(str, length); return true; }
    bool EndObject(SizeType) { st_ = kExitingObject; return true; }
    bool StartArray() { st_ = kEnteringArray; return true; }
    bool EndArray(SizeType) { st_ = kExitingArray; return true; }

protected:
    LookaheadParserHandler(char* str);
    void ParseNext();

protected:
    enum LookaheadParsingState {
        kInit,
        kError,
        kHasNull,
        kHasBool,
        kHasNumber,
        kHasString,
        kHasKey,
        kEnteringObject,
        kExitingObject,
        kEnteringArray,
        kExitingArray
    };
    
    Value v_;
    LookaheadParsingState st_;
    Reader r_;
    InsituStringStream ss_;
    
    static const int parseFlags = kParseDefaultFlags | kParseInsituFlag;
};

LookaheadParserHandler::LookaheadParserHandler(char* str) : v_(), st_(kInit), r_(), ss_(str) {
    r_.IterativeParseInit();
    ParseNext();
}

void LookaheadParserHandler::ParseNext() {
    if (r_.HasParseError()) {
        st_ = kError;
        return;
    }
    
    r_.IterativeParseNext<parseFlags>(ss_, *this);
}

class LookaheadParser : protected LookaheadParserHandler {
public:
    LookaheadParser(char* str) : LookaheadParserHandler(str) {}
    
    bool EnterObject();
    bool EnterArray();
    const char* NextObjectKey();
    bool NextArrayValue();
    int GetInt();
    double GetDouble();
    const char* GetString();
    bool GetBool();
    void GetNull();

    void SkipObject();
    void SkipArray();
    void SkipValue();
    Value* PeekValue();
    int PeekType(); // returns a rapidjson::Type, or -1 for no value (at end of object/array)
    
    bool IsValid() { return st_ != kError; }
    
protected:
    void SkipOut(int depth);
};

bool LookaheadParser::EnterObject() {
    if (st_ != kEnteringObject) {
        st_  = kError;
        return false;
    }
    
    ParseNext();
    return true;
}

bool LookaheadParser::EnterArray() {
    if (st_ != kEnteringArray) {
        st_  = kError;
        return false;
    }
    
    ParseNext();
    return true;
}

const char* LookaheadParser::NextObjectKey() {
    if (st_ == kHasKey) {
        const char* result = v_.GetString();
        ParseNext();
        return result;
    }
    
    if (st_ != kExitingObject) {
        st_ = kError;
        return 0;
    }
    
    ParseNext();
    return 0;
}

bool LookaheadParser::NextArrayValue() {
    if (st_ == kExitingArray) {
        ParseNext();
        return false;
    }
    
    if (st_ == kError || st_ == kExitingObject || st_ == kHasKey) {
        st_ = kError;
        return false;
    }

    return true;
}

int LookaheadParser::GetInt() {
    if (st_ != kHasNumber || !v_.IsInt()) {
        st_ = kError;
        return 0;
    }

    int result = v_.GetInt();
    ParseNext();
    return result;
}

double LookaheadParser::GetDouble() {
    if (st_ != kHasNumber) {
        st_  = kError;
        return 0.;
    }
    
    double result = v_.GetDouble();
    ParseNext();
    return result;
}

bool LookaheadParser::GetBool() {
    if (st_ != kHasBool) {
        st_  = kError;
        return false;
    }
    
    bool result = v_.GetBool();
    ParseNext();
    return result;
}

void LookaheadParser::GetNull() {
    if (st_ != kHasNull) {
        st_  = kError;
        return;
    }

    ParseNext();
}

const char* LookaheadParser::GetString() {
    if (st_ != kHasString) {
        st_  = kError;
        return 0;
    }
    
    const char* result = v_.GetString();
    ParseNext();
    return result;
}

void LookaheadParser::SkipOut(int depth) {
    do {
        if (st_ == kEnteringArray || st_ == kEnteringObject) {
            ++depth;
        }
        else if (st_ == kExitingArray || st_ == kExitingObject) {
            --depth;
        }
        else if (st_ == kError) {
            return;
        }

        ParseNext();
    }
    while (depth > 0);
}

void LookaheadParser::SkipValue() {
    SkipOut(0);
}

void LookaheadParser::SkipArray() {
    SkipOut(1);
}

void LookaheadParser::SkipObject() {
    SkipOut(1);
}

Value* LookaheadParser::PeekValue() {
    if (st_ >= kHasNull && st_ <= kHasKey) {
        return &v_;
    }
    
    return 0;
}

int LookaheadParser::PeekType() {
    if (st_ >= kHasNull && st_ <= kHasKey) {
        return v_.GetType();
    }
    
    if (st_ == kEnteringArray) {
        return kArrayType;
    }
    
    if (st_ == kEnteringObject) {
        return kObjectType;
    }

    return -1;
}

//-------------------------------------------------------------------------

int main() {
    using namespace std;

    char json[] = " { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null,"
        "\"i\":123, \"pi\": 3.1416, \"a\":[-1, 2, 3, 4, \"array\", []], \"skipArrays\":[1, 2, [[[3]]]], "
        "\"skipObject\":{ \"i\":0, \"t\":true, \"n\":null, \"d\":123.45 }, "
        "\"skipNested\":[[[[{\"\":0}, {\"\":[-9.87]}]]], [], []], "
        "\"skipString\":\"zzz\", \"reachedEnd\":null, \"t\":true }";

    LookaheadParser r(json);
    
    RAPIDJSON_ASSERT(r.PeekType() == kObjectType);

    r.EnterObject();
    while (const char* key = r.NextObjectKey()) {
        if (0 == strcmp(key, "hello")) {
            RAPIDJSON_ASSERT(r.PeekType() == kStringType);
            cout << key << ":" << r.GetString() << endl;
        }
        else if (0 == strcmp(key, "t") || 0 == strcmp(key, "f")) {
            RAPIDJSON_ASSERT(r.PeekType() == kTrueType || r.PeekType() == kFalseType);
            cout << key << ":" << r.GetBool() << endl;
            continue;
        }
        else if (0 == strcmp(key, "n")) {
            RAPIDJSON_ASSERT(r.PeekType() == kNullType);
            r.GetNull();
            cout << key << endl;
            continue;
        }
        else if (0 == strcmp(key, "pi")) {
            RAPIDJSON_ASSERT(r.PeekType() == kNumberType);
            cout << key << ":" << r.GetDouble() << endl;
            continue;
        }
        else if (0 == strcmp(key, "a")) {
            RAPIDJSON_ASSERT(r.PeekType() == kArrayType);
            
            r.EnterArray();
            
            cout << key << ":[ ";
            while (r.NextArrayValue()) {
                if (r.PeekType() == kNumberType) {
                    cout << r.GetDouble() << " ";
                }
                else if (r.PeekType() == kStringType) {
                    cout << r.GetString() << " ";
                }
                else {
                    r.SkipArray();
                    break;
                }
            }
            
            cout << "]" << endl;
        }
        else {
            cout << key << ":skipped" << endl;
            r.SkipValue();
        }
    }
    
    return 0;
}

RAPIDJSON_DIAG_POP