/* */ #ifndef D_JSON_PARSER_H #define D_JSON_PARSER_H #include "common.h" #include namespace aria2 { class StructParserStateMachine; namespace json { enum JsonError { ERR_UNEXPECTED_CHAR_BEFORE_VAL = -1, ERR_UNEXPECTED_CHAR_BEFORE_OBJ_KEY = -2, ERR_UNEXPECTED_CHAR_BEFORE_OBJ_VAL = -3, ERR_UNEXPECTED_CHAR_BEFORE_OBJ_SEP = -4, ERR_INVALID_UNICODE_POINT = -5, ERR_INVALID_NUMBER = -6, ERR_NUMBER_OUT_OF_RANGE = -7, ERR_UNEXPECTED_CHAR_BEFORE_ARRAY_SEP = -8, ERR_UNEXPECTED_LITERAL = -9, ERR_PREMATURE_DATA = -10, ERR_STRUCTURE_TOO_DEEP = -11 }; class JsonParser { public: JsonParser(StructParserStateMachine* psm); ~JsonParser(); // Parses |size| bytes of data |data| and returns the number of // bytes processed. On error, one of the negative error codes is // returned. ssize_t parseUpdate(const char* data, size_t size); // Parses |size| bytes of data |data| and returns the number of // bytes processed. On error, one of the negative error codes is // returned. Call this function to signal the parser that this is // the last piece of data. This function does NOT reset the internal // state. ssize_t parseFinal(const char* data, size_t size); // Resets the internal state of the parser and makes it ready for // reuse. void reset(); private: int pushState(int state); int stateTop() const; int popState(); void runBeginCallback(int elementType); void runEndCallback(int elementType); void runCharactersCallback(const char* data, size_t len); void runNumberCallback(int64_t number, int frac, int exp); void runBoolCallback(bool bval); void onStringEnd(); void onNumberEnd(); void onObjectEnd(); void onArrayEnd(); void onValueEnd(); void onBoolEnd(); void onNullEnd(); void consumeUnicode(char c); int consumeLowSurrogate(char c); StructParserStateMachine* psm_; std::stack stateStack_; int currentState_; // Unicode codepoint uint16_t codepoint_; // For low-surrogate codepoint uint16_t codepoint2_; int numberSign_; int64_t number_; int frac_; int expSign_; int exp_; size_t numConsumed_; int lastError_; }; } // namespace json } // namespace aria2 #endif // D_JSON_PARSER_H