$extrastylesheet
JsonCpp project page Classes Namespace JsonCpp home page

include/json/reader.h
Go to the documentation of this file.
00001 // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
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 #pragma pack(push, 8)
00027 
00028 namespace Json {
00029 
00035 class JSONCPP_DEPRECATED("Use CharReader and CharReaderBuilder instead") JSON_API Reader {
00036 public:
00037   typedef char Char;
00038   typedef const Char* Location;
00039 
00046   struct StructuredError {
00047     ptrdiff_t offset_start;
00048     ptrdiff_t offset_limit;
00049     JSONCPP_STRING message;
00050   };
00051 
00055   Reader();
00056 
00060   Reader(const Features& features);
00061 
00076   bool
00077   parse(const std::string& document, Value& root, bool collectComments = true);
00078 
00097   bool parse(const char* beginDoc,
00098              const char* endDoc,
00099              Value& root,
00100              bool collectComments = true);
00101 
00104   bool parse(JSONCPP_ISTREAM& is, Value& root, bool collectComments = true);
00105 
00115   JSONCPP_DEPRECATED("Use getFormattedErrorMessages() instead.")
00116   JSONCPP_STRING getFormatedErrorMessages() const;
00117 
00126   JSONCPP_STRING getFormattedErrorMessages() const;
00127 
00135   std::vector<StructuredError> getStructuredErrors() const;
00136 
00143   bool pushError(const Value& value, const JSONCPP_STRING& message);
00144 
00152   bool pushError(const Value& value, const JSONCPP_STRING& message, const Value& extra);
00153 
00158   bool good() const;
00159 
00160 private:
00161   enum TokenType {
00162     tokenEndOfStream = 0,
00163     tokenObjectBegin,
00164     tokenObjectEnd,
00165     tokenArrayBegin,
00166     tokenArrayEnd,
00167     tokenString,
00168     tokenNumber,
00169     tokenTrue,
00170     tokenFalse,
00171     tokenNull,
00172     tokenArraySeparator,
00173     tokenMemberSeparator,
00174     tokenComment,
00175     tokenError
00176   };
00177 
00178   class Token {
00179   public:
00180     TokenType type_;
00181     Location start_;
00182     Location end_;
00183   };
00184 
00185   class ErrorInfo {
00186   public:
00187     Token token_;
00188     JSONCPP_STRING message_;
00189     Location extra_;
00190   };
00191 
00192   typedef std::deque<ErrorInfo> Errors;
00193 
00194   bool readToken(Token& token);
00195   void skipSpaces();
00196   bool match(Location pattern, int patternLength);
00197   bool readComment();
00198   bool readCStyleComment();
00199   bool readCppStyleComment();
00200   bool readString();
00201   void readNumber();
00202   bool readValue();
00203   bool readObject(Token& token);
00204   bool readArray(Token& token);
00205   bool decodeNumber(Token& token);
00206   bool decodeNumber(Token& token, Value& decoded);
00207   bool decodeString(Token& token);
00208   bool decodeString(Token& token, JSONCPP_STRING& decoded);
00209   bool decodeDouble(Token& token);
00210   bool decodeDouble(Token& token, Value& decoded);
00211   bool decodeUnicodeCodePoint(Token& token,
00212                               Location& current,
00213                               Location end,
00214                               unsigned int& unicode);
00215   bool decodeUnicodeEscapeSequence(Token& token,
00216                                    Location& current,
00217                                    Location end,
00218                                    unsigned int& unicode);
00219   bool addError(const JSONCPP_STRING& message, Token& token, Location extra = 0);
00220   bool recoverFromError(TokenType skipUntilToken);
00221   bool addErrorAndRecover(const JSONCPP_STRING& message,
00222                           Token& token,
00223                           TokenType skipUntilToken);
00224   void skipUntilSpace();
00225   Value& currentValue();
00226   Char getNextChar();
00227   void
00228   getLocationLineAndColumn(Location location, int& line, int& column) const;
00229   JSONCPP_STRING getLocationLineAndColumn(Location location) const;
00230   void addComment(Location begin, Location end, CommentPlacement placement);
00231   void skipCommentTokens(Token& token);
00232 
00233   static bool containsNewLine(Location begin, Location end);
00234   static JSONCPP_STRING normalizeEOL(Location begin, Location end);
00235 
00236   typedef std::stack<Value*> Nodes;
00237   Nodes nodes_;
00238   Errors errors_;
00239   JSONCPP_STRING document_;
00240   Location begin_;
00241   Location end_;
00242   Location current_;
00243   Location lastValueEnd_;
00244   Value* lastValue_;
00245   JSONCPP_STRING commentsBefore_;
00246   Features features_;
00247   bool collectComments_;
00248 };  // Reader
00249 
00252 class JSON_API CharReader {
00253 public:
00254   virtual ~CharReader() {}
00272   virtual bool parse(
00273       char const* beginDoc, char const* endDoc,
00274       Value* root, JSONCPP_STRING* errs) = 0;
00275 
00276   class JSON_API Factory {
00277   public:
00278     virtual ~Factory() {}
00282     virtual CharReader* newCharReader() const = 0;
00283   };  // Factory
00284 };  // CharReader
00285 
00298 class JSON_API CharReaderBuilder : public CharReader::Factory {
00299 public:
00300   // Note: We use a Json::Value so that we can add data-members to this class
00301   // without a major version bump.
00338   Json::Value settings_;
00339 
00340   CharReaderBuilder();
00341   ~CharReaderBuilder() JSONCPP_OVERRIDE;
00342 
00343   CharReader* newCharReader() const JSONCPP_OVERRIDE;
00344 
00348   bool validate(Json::Value* invalid) const;
00349 
00352   Value& operator[](JSONCPP_STRING key);
00353 
00359   static void setDefaults(Json::Value* settings);
00365   static void strictMode(Json::Value* settings);
00366 };
00367 
00372 bool JSON_API parseFromStream(
00373     CharReader::Factory const&,
00374     JSONCPP_ISTREAM&,
00375     Value* root, std::string* errs);
00376 
00401 JSON_API JSONCPP_ISTREAM& operator>>(JSONCPP_ISTREAM&, Value&);
00402 
00403 } // namespace Json
00404 
00405 #pragma pack(pop)
00406 
00407 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
00408 #pragma warning(pop)
00409 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
00410 
00411 #endif // CPPTL_JSON_READER_H_INCLUDED