00001
00002
00003
00004
00005
00006 #ifndef CPPTL_JSON_READER_H_INCLUDED
00007 #define CPPTL_JSON_READER_H_INCLUDED
00008
00009 #if !defined(JSON_IS_AMALGAMATION)
00010 #include "features.h"
00011 #include "value.h"
00012 #endif // if !defined(JSON_IS_AMALGAMATION)
00013 #include <deque>
00014 #include <iosfwd>
00015 #include <stack>
00016 #include <string>
00017 #include <istream>
00018
00019
00020
00021 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
00022 #pragma warning(push)
00023 #pragma warning(disable : 4251)
00024 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
00025
00026 namespace Json {
00027
00033 class JSON_API Reader {
00034 public:
00035 typedef char Char;
00036 typedef const Char* Location;
00037
00041 Reader();
00042
00046 Reader(const Features& features);
00047
00062 bool
00063 parse(const std::string& document, Value& root, bool collectComments = true);
00064
00083 bool parse(const char* beginDoc,
00084 const char* endDoc,
00085 Value& root,
00086 bool collectComments = true);
00087
00090 bool parse(std::istream& is, Value& root, bool collectComments = true);
00091
00101 JSONCPP_DEPRECATED("Use getFormattedErrorMessages() instead.")
00102 std::string getFormatedErrorMessages() const;
00103
00112 std::string getFormattedErrorMessages() const;
00113
00114 private:
00115 enum TokenType {
00116 tokenEndOfStream = 0,
00117 tokenObjectBegin,
00118 tokenObjectEnd,
00119 tokenArrayBegin,
00120 tokenArrayEnd,
00121 tokenString,
00122 tokenNumber,
00123 tokenTrue,
00124 tokenFalse,
00125 tokenNull,
00126 tokenArraySeparator,
00127 tokenMemberSeparator,
00128 tokenComment,
00129 tokenError
00130 };
00131
00132 class Token {
00133 public:
00134 TokenType type_;
00135 Location start_;
00136 Location end_;
00137 };
00138
00139 class ErrorInfo {
00140 public:
00141 Token token_;
00142 std::string message_;
00143 Location extra_;
00144 };
00145
00146 typedef std::deque<ErrorInfo> Errors;
00147
00148 bool readToken(Token& token);
00149 void skipSpaces();
00150 bool match(Location pattern, int patternLength);
00151 bool readComment();
00152 bool readCStyleComment();
00153 bool readCppStyleComment();
00154 bool readString();
00155 void readNumber();
00156 bool readValue();
00157 bool readObject(Token& token);
00158 bool readArray(Token& token);
00159 bool decodeNumber(Token& token);
00160 bool decodeNumber(Token& token, Value& decoded);
00161 bool decodeString(Token& token);
00162 bool decodeString(Token& token, std::string& decoded);
00163 bool decodeDouble(Token& token);
00164 bool decodeDouble(Token& token, Value& decoded);
00165 bool decodeUnicodeCodePoint(Token& token,
00166 Location& current,
00167 Location end,
00168 unsigned int& unicode);
00169 bool decodeUnicodeEscapeSequence(Token& token,
00170 Location& current,
00171 Location end,
00172 unsigned int& unicode);
00173 bool addError(const std::string& message, Token& token, Location extra = 0);
00174 bool recoverFromError(TokenType skipUntilToken);
00175 bool addErrorAndRecover(const std::string& message,
00176 Token& token,
00177 TokenType skipUntilToken);
00178 void skipUntilSpace();
00179 Value& currentValue();
00180 Char getNextChar();
00181 void
00182 getLocationLineAndColumn(Location location, int& line, int& column) const;
00183 std::string getLocationLineAndColumn(Location location) const;
00184 void addComment(Location begin, Location end, CommentPlacement placement);
00185 void skipCommentTokens(Token& token);
00186
00187 typedef std::stack<Value*> Nodes;
00188 Nodes nodes_;
00189 Errors errors_;
00190 std::string document_;
00191 Location begin_;
00192 Location end_;
00193 Location current_;
00194 Location lastValueEnd_;
00195 Value* lastValue_;
00196 std::string commentsBefore_;
00197 Features features_;
00198 bool collectComments_;
00199 };
00200
00203 class JSON_API CharReader {
00204 public:
00205 virtual ~CharReader() {}
00223 virtual bool parse(
00224 char const* beginDoc, char const* endDoc,
00225 Value* root, std::string* errs) = 0;
00226
00227 class Factory {
00228 public:
00229 virtual ~Factory() {}
00233 virtual CharReader* newCharReader() const = 0;
00234 };
00235 };
00236
00249 class JSON_API CharReaderBuilder : public CharReader::Factory {
00250 public:
00251
00252
00286 Json::Value settings_;
00287
00288 CharReaderBuilder();
00289 virtual ~CharReaderBuilder();
00290
00291 virtual CharReader* newCharReader() const;
00292
00296 bool validate(Json::Value* invalid) const;
00297
00300 Value& operator[](std::string key);
00301
00307 static void setDefaults(Json::Value* settings);
00313 static void strictMode(Json::Value* settings);
00314 };
00315
00320 bool JSON_API parseFromStream(
00321 CharReader::Factory const&,
00322 std::istream&,
00323 Value* root, std::string* errs);
00324
00349 JSON_API std::istream& operator>>(std::istream&, Value&);
00350
00351 }
00352
00353 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
00354 #pragma warning(pop)
00355 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
00356
00357 #endif // CPPTL_JSON_READER_H_INCLUDED