JsonCpp project page JsonCpp home page

include/json/reader.h

Go to the documentation of this file.
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 
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 // Disable warning C4251: <data member>: <type> needs to have dll-interface to
00020 // be used by...
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 
00044   struct StructuredError {
00045     size_t offset_start;
00046     size_t offset_limit;
00047     std::string message;
00048   };
00049 
00053   Reader();
00054 
00058   Reader(const Features& features);
00059 
00074   bool
00075   parse(const std::string& document, Value& root, bool collectComments = true);
00076 
00095   bool parse(const char* beginDoc,
00096              const char* endDoc,
00097              Value& root,
00098              bool collectComments = true);
00099 
00102   bool parse(std::istream& is, Value& root, bool collectComments = true);
00103 
00113   JSONCPP_DEPRECATED("Use getFormattedErrorMessages() instead.")
00114   std::string getFormatedErrorMessages() const;
00115 
00124   std::string getFormattedErrorMessages() const;
00125 
00133   std::vector<StructuredError> getStructuredErrors() const;
00134 
00141   bool pushError(const Value& value, const std::string& message);
00142 
00150   bool pushError(const Value& value, const std::string& message, const Value& extra);
00151 
00156   bool good() const;
00157 
00158 private:
00159   enum TokenType {
00160     tokenEndOfStream = 0,
00161     tokenObjectBegin,
00162     tokenObjectEnd,
00163     tokenArrayBegin,
00164     tokenArrayEnd,
00165     tokenString,
00166     tokenNumber,
00167     tokenTrue,
00168     tokenFalse,
00169     tokenNull,
00170     tokenArraySeparator,
00171     tokenMemberSeparator,
00172     tokenComment,
00173     tokenError
00174   };
00175 
00176   class Token {
00177   public:
00178     TokenType type_;
00179     Location start_;
00180     Location end_;
00181   };
00182 
00183   class ErrorInfo {
00184   public:
00185     Token token_;
00186     std::string message_;
00187     Location extra_;
00188   };
00189 
00190   typedef std::deque<ErrorInfo> Errors;
00191 
00192   bool readToken(Token& token);
00193   void skipSpaces();
00194   bool match(Location pattern, int patternLength);
00195   bool readComment();
00196   bool readCStyleComment();
00197   bool readCppStyleComment();
00198   bool readString();
00199   void readNumber();
00200   bool readValue();
00201   bool readObject(Token& token);
00202   bool readArray(Token& token);
00203   bool decodeNumber(Token& token);
00204   bool decodeNumber(Token& token, Value& decoded);
00205   bool decodeString(Token& token);
00206   bool decodeString(Token& token, std::string& decoded);
00207   bool decodeDouble(Token& token);
00208   bool decodeDouble(Token& token, Value& decoded);
00209   bool decodeUnicodeCodePoint(Token& token,
00210                               Location& current,
00211                               Location end,
00212                               unsigned int& unicode);
00213   bool decodeUnicodeEscapeSequence(Token& token,
00214                                    Location& current,
00215                                    Location end,
00216                                    unsigned int& unicode);
00217   bool addError(const std::string& message, Token& token, Location extra = 0);
00218   bool recoverFromError(TokenType skipUntilToken);
00219   bool addErrorAndRecover(const std::string& message,
00220                           Token& token,
00221                           TokenType skipUntilToken);
00222   void skipUntilSpace();
00223   Value& currentValue();
00224   Char getNextChar();
00225   void
00226   getLocationLineAndColumn(Location location, int& line, int& column) const;
00227   std::string getLocationLineAndColumn(Location location) const;
00228   void addComment(Location begin, Location end, CommentPlacement placement);
00229   void skipCommentTokens(Token& token);
00230 
00231   typedef std::stack<Value*> Nodes;
00232   Nodes nodes_;
00233   Errors errors_;
00234   std::string document_;
00235   Location begin_;
00236   Location end_;
00237   Location current_;
00238   Location lastValueEnd_;
00239   Value* lastValue_;
00240   std::string commentsBefore_;
00241   Features features_;
00242   bool collectComments_;
00243 };  // Reader
00244 
00247 class JSON_API CharReader {
00248 public:
00249   virtual ~CharReader() {}
00267   virtual bool parse(
00268       char const* beginDoc, char const* endDoc,
00269       Value* root, std::string* errs) = 0;
00270 
00271   class Factory {
00272   public:
00273     virtual ~Factory() {}
00277     virtual CharReader* newCharReader() const = 0;
00278   };  // Factory
00279 };  // CharReader
00280 
00293 class JSON_API CharReaderBuilder : public CharReader::Factory {
00294 public:
00295   // Note: We use a Json::Value so that we can add data-members to this class
00296   // without a major version bump.
00330   Json::Value settings_;
00331 
00332   CharReaderBuilder();
00333   virtual ~CharReaderBuilder();
00334 
00335   virtual CharReader* newCharReader() const;
00336 
00340   bool validate(Json::Value* invalid) const;
00341 
00344   Value& operator[](std::string key);
00345 
00351   static void setDefaults(Json::Value* settings);
00357   static void strictMode(Json::Value* settings);
00358 };
00359 
00364 bool JSON_API parseFromStream(
00365     CharReader::Factory const&,
00366     std::istream&,
00367     Value* root, std::string* errs);
00368 
00393 JSON_API std::istream& operator>>(std::istream&, Value&);
00394 
00395 } // namespace Json
00396 
00397 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
00398 #pragma warning(pop)
00399 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
00400 
00401 #endif // CPPTL_JSON_READER_H_INCLUDED