Go to the documentation of this file.00001 #ifndef CPPTL_JSON_READER_H_INCLUDED
00002 # define CPPTL_JSON_READER_H_INCLUDED
00003
00004 # include "features.h"
00005 # include "value.h"
00006 # include <deque>
00007 # include <stack>
00008 # include <string>
00009 # include <iostream>
00010
00011 namespace Json {
00012
00016 class JSON_API Reader
00017 {
00018 public:
00019 typedef char Char;
00020 typedef const Char *Location;
00021
00025 Reader();
00026
00030 Reader( const Features &features );
00031
00042 bool parse( const std::string &document,
00043 Value &root,
00044 bool collectComments = true );
00045
00056 bool parse( const char *beginDoc, const char *endDoc,
00057 Value &root,
00058 bool collectComments = true );
00059
00062 bool parse( std::istream &is,
00063 Value &root,
00064 bool collectComments = true );
00065
00071 std::string getFormatedErrorMessages() const;
00072
00073 private:
00074 enum TokenType
00075 {
00076 tokenEndOfStream = 0,
00077 tokenObjectBegin,
00078 tokenObjectEnd,
00079 tokenArrayBegin,
00080 tokenArrayEnd,
00081 tokenString,
00082 tokenNumber,
00083 tokenTrue,
00084 tokenFalse,
00085 tokenNull,
00086 tokenArraySeparator,
00087 tokenMemberSeparator,
00088 tokenComment,
00089 tokenError
00090 };
00091
00092 class Token
00093 {
00094 public:
00095 TokenType type_;
00096 Location start_;
00097 Location end_;
00098 };
00099
00100 class ErrorInfo
00101 {
00102 public:
00103 Token token_;
00104 std::string message_;
00105 Location extra_;
00106 };
00107
00108 typedef std::deque<ErrorInfo> Errors;
00109
00110 bool expectToken( TokenType type, Token &token, const char *message );
00111 bool readToken( Token &token );
00112 void skipSpaces();
00113 bool match( Location pattern,
00114 int patternLength );
00115 bool readComment();
00116 bool readCStyleComment();
00117 bool readCppStyleComment();
00118 bool readString();
00119 void readNumber();
00120 bool readValue();
00121 bool readObject( Token &token );
00122 bool readArray( Token &token );
00123 bool decodeNumber( Token &token );
00124 bool decodeString( Token &token );
00125 bool decodeString( Token &token, std::string &decoded );
00126 bool decodeDouble( Token &token );
00127 bool decodeUnicodeCodePoint( Token &token,
00128 Location ¤t,
00129 Location end,
00130 unsigned int &unicode );
00131 bool decodeUnicodeEscapeSequence( Token &token,
00132 Location ¤t,
00133 Location end,
00134 unsigned int &unicode );
00135 bool addError( const std::string &message,
00136 Token &token,
00137 Location extra = 0 );
00138 bool recoverFromError( TokenType skipUntilToken );
00139 bool addErrorAndRecover( const std::string &message,
00140 Token &token,
00141 TokenType skipUntilToken );
00142 void skipUntilSpace();
00143 Value ¤tValue();
00144 Char getNextChar();
00145 void getLocationLineAndColumn( Location location,
00146 int &line,
00147 int &column ) const;
00148 std::string getLocationLineAndColumn( Location location ) const;
00149 void addComment( Location begin,
00150 Location end,
00151 CommentPlacement placement );
00152 void skipCommentTokens( Token &token );
00153
00154 typedef std::stack<Value *> Nodes;
00155 Nodes nodes_;
00156 Errors errors_;
00157 std::string document_;
00158 Location begin_;
00159 Location end_;
00160 Location current_;
00161 Location lastValueEnd_;
00162 Value *lastValue_;
00163 std::string commentsBefore_;
00164 Features features_;
00165 bool collectComments_;
00166 };
00167
00192 std::istream& operator>>( std::istream&, Value& );
00193
00194 }
00195
00196 #endif // CPPTL_JSON_READER_H_INCLUDED