RTBKit
0.9
Open-source framework to create real-time ad bidding systems.
|
00001 // Copyright 2007-2010 Baptiste Lepilleur 00002 // Distributed under MIT license, or public domain if desired and 00003 // recognized in your jurisdiction. 00004 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE 00005 #ifndef CPPTL_JSON_READER_H_INCLUDED 00006 # define CPPTL_JSON_READER_H_INCLUDED 00007 00008 # include "features.h" 00009 # include "value.h" 00010 # include <deque> 00011 # include <stack> 00012 # include <string> 00013 # include <iostream> 00014 00015 namespace Json { 00016 00017 class Exception : public std::exception { 00018 public: 00019 Exception(const std::string & msg); 00020 virtual ~Exception() throw(); 00021 00022 virtual const char * what() const throw(); 00023 00024 private: 00025 std::string message; 00026 }; 00027 Json::Value parse(const std::string &jsonStr); 00031 class JSON_API Reader 00032 { 00033 public: 00034 typedef char Char; 00035 typedef const Char *Location; 00036 00040 Reader(); 00041 00045 Reader( const Features &features ); 00046 00057 bool parse( const std::string &document, 00058 Value &root, 00059 bool collectComments = true ); 00060 00071 bool parse( const char *beginDoc, const char *endDoc, 00072 Value &root, 00073 bool collectComments = true ); 00074 00077 bool parse( std::istream &is, 00078 Value &root, 00079 bool collectComments = true ); 00080 00086 std::string getFormattedErrorMessages() const; 00087 00088 private: 00089 enum TokenType 00090 { 00091 tokenEndOfStream = 0, 00092 tokenObjectBegin, 00093 tokenObjectEnd, 00094 tokenArrayBegin, 00095 tokenArrayEnd, 00096 tokenString, 00097 tokenNumber, 00098 tokenTrue, 00099 tokenFalse, 00100 tokenNull, 00101 tokenArraySeparator, 00102 tokenMemberSeparator, 00103 tokenComment, 00104 tokenError 00105 }; 00106 00107 class Token 00108 { 00109 public: 00110 TokenType type_; 00111 Location start_; 00112 Location end_; 00113 }; 00114 00115 class ErrorInfo 00116 { 00117 public: 00118 Token token_; 00119 std::string message_; 00120 Location extra_; 00121 }; 00122 00123 typedef std::deque<ErrorInfo> Errors; 00124 00125 bool expectToken( TokenType type, Token &token, const char *message ); 00126 bool readToken( Token &token ); 00127 void skipSpaces(); 00128 bool match( Location pattern, 00129 int patternLength ); 00130 bool readComment(); 00131 bool readCStyleComment(); 00132 bool readCppStyleComment(); 00133 bool readString(Char delim); 00134 void readNumber(); 00135 bool readValue(); 00136 bool readObject( Token &token ); 00137 bool readArray( Token &token ); 00138 bool decodeNumber( Token &token ); 00139 bool decodeString( Token &token ); 00140 bool decodeString( Token &token, std::string &decoded ); 00141 bool decodeDouble( Token &token ); 00142 bool decodeUnicodeCodePoint( Token &token, 00143 Location ¤t, 00144 Location end, 00145 unsigned int &unicode ); 00146 bool decodeUnicodeEscapeSequence( Token &token, 00147 Location ¤t, 00148 Location end, 00149 unsigned int &unicode ); 00150 bool addError( const std::string &message, 00151 Token &token, 00152 Location extra = 0 ); 00153 bool recoverFromError( TokenType skipUntilToken ); 00154 bool addErrorAndRecover( const std::string &message, 00155 Token &token, 00156 TokenType skipUntilToken ); 00157 void skipUntilSpace(); 00158 Value ¤tValue(); 00159 Char getNextChar(); 00160 void getLocationLineAndColumn( Location location, 00161 int &line, 00162 int &column ) const; 00163 std::string getLocationLineAndColumn( Location location ) const; 00164 void addComment( Location begin, 00165 Location end, 00166 CommentPlacement placement ); 00167 void skipCommentTokens( Token &token ); 00168 00169 typedef std::stack<Value *> Nodes; 00170 Nodes nodes_; 00171 Errors errors_; 00172 std::string document_; 00173 Location begin_; 00174 Location end_; 00175 Location current_; 00176 Location lastValueEnd_; 00177 Value *lastValue_; 00178 std::string commentsBefore_; 00179 Features features_; 00180 bool collectComments_; 00181 }; 00182 00207 std::istream& operator>>( std::istream&, Value& ); 00208 00209 } // namespace Json 00210 00211 #endif // CPPTL_JSON_READER_H_INCLUDED