Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:26:25

0001 #ifndef CPPTL_JSON_READER_H_INCLUDED
0002 #define CPPTL_JSON_READER_H_INCLUDED
0003 
0004 #include "features.h"
0005 #include "value.h"
0006 #include <deque>
0007 #include <stack>
0008 #include <string>
0009 #include <iostream>
0010 
0011 namespace Json {
0012 
0013   /** \brief Unserialize a <a HREF="http://www.json.org">JSON</a> document into a Value.
0014     *
0015     */
0016   class JSON_API Reader {
0017   public:
0018     typedef char Char;
0019     typedef const Char *Location;
0020 
0021     /** \brief Constructs a Reader allowing all features
0022        * for parsing.
0023        */
0024     Reader();
0025 
0026     /** \brief Constructs a Reader allowing the specified feature set
0027        * for parsing.
0028        */
0029     Reader(const Features &features);
0030 
0031     /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> document.
0032        * \param document UTF-8 encoded string containing the document to read.
0033        * \param root [out] Contains the root value of the document if it was
0034        *             successfully parsed.
0035        * \param collectComments \c true to collect comment and allow writing them back during
0036        *                        serialization, \c false to discard comments.
0037        *                        This parameter is ignored if Features::allowComments_
0038        *                        is \c false.
0039        * \return \c true if the document was successfully parsed, \c false if an error occurred.
0040        */
0041     bool parse(const std::string &document, Value &root, bool collectComments = true);
0042 
0043     /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> document.
0044        * \param document UTF-8 encoded string containing the document to read.
0045        * \param root [out] Contains the root value of the document if it was
0046        *             successfully parsed.
0047        * \param collectComments \c true to collect comment and allow writing them back during
0048        *                        serialization, \c false to discard comments.
0049        *                        This parameter is ignored if Features::allowComments_
0050        *                        is \c false.
0051        * \return \c true if the document was successfully parsed, \c false if an error occurred.
0052        */
0053     bool parse(const char *beginDoc, const char *endDoc, Value &root, bool collectComments = true);
0054 
0055     /// \brief Parse from input stream.
0056     /// \see Json::operator>>(std::istream&, Json::Value&).
0057     bool parse(std::istream &is, Value &root, bool collectComments = true);
0058 
0059     /** \brief Returns a user friendly string that list errors in the parsed document.
0060        * \return Formatted error message with the list of errors with their location in 
0061        *         the parsed document. An empty string is returned if no error occurred
0062        *         during parsing.
0063        */
0064     std::string getFormatedErrorMessages() const;
0065 
0066   private:
0067     enum TokenType {
0068       tokenEndOfStream = 0,
0069       tokenObjectBegin,
0070       tokenObjectEnd,
0071       tokenArrayBegin,
0072       tokenArrayEnd,
0073       tokenString,
0074       tokenNumber,
0075       tokenTrue,
0076       tokenFalse,
0077       tokenNull,
0078       tokenArraySeparator,
0079       tokenMemberSeparator,
0080       tokenComment,
0081       tokenError
0082     };
0083 
0084     class Token {
0085     public:
0086       TokenType type_;
0087       Location start_;
0088       Location end_;
0089     };
0090 
0091     class ErrorInfo {
0092     public:
0093       Token token_;
0094       std::string message_;
0095       Location extra_;
0096     };
0097 
0098     typedef std::deque<ErrorInfo> Errors;
0099 
0100     bool expectToken(TokenType type, Token &token, const char *message);
0101     bool readToken(Token &token);
0102     void skipSpaces();
0103     bool match(Location pattern, int patternLength);
0104     bool readComment();
0105     bool readCStyleComment();
0106     bool readCppStyleComment();
0107     bool readString();
0108     void readNumber();
0109     bool readValue();
0110     bool readObject(Token &token);
0111     bool readArray(Token &token);
0112     bool decodeNumber(Token &token);
0113     bool decodeString(Token &token);
0114     bool decodeString(Token &token, std::string &decoded);
0115     bool decodeDouble(Token &token);
0116     bool decodeUnicodeCodePoint(Token &token, Location &current, Location end, unsigned int &unicode);
0117     bool decodeUnicodeEscapeSequence(Token &token, Location &current, Location end, unsigned int &unicode);
0118     bool addError(const std::string &message, Token &token, Location extra = nullptr);
0119     bool recoverFromError(TokenType skipUntilToken);
0120     bool addErrorAndRecover(const std::string &message, Token &token, TokenType skipUntilToken);
0121     void skipUntilSpace();
0122     Value &currentValue();
0123     Char getNextChar();
0124     void getLocationLineAndColumn(Location location, int &line, int &column) const;
0125     std::string getLocationLineAndColumn(Location location) const;
0126     void addComment(Location begin, Location end, CommentPlacement placement);
0127     void skipCommentTokens(Token &token);
0128 
0129     typedef std::stack<Value *> Nodes;
0130     Nodes nodes_;
0131     Errors errors_;
0132     std::string document_;
0133     Location begin_;
0134     Location end_;
0135     Location current_;
0136     Location lastValueEnd_;
0137     Value *lastValue_;
0138     std::string commentsBefore_;
0139     Features features_;
0140     bool collectComments_;
0141   };
0142 
0143   /** \brief Read from 'sin' into 'root'.
0144 
0145     Always keep comments from the input JSON.
0146 
0147     This can be used to read a file into a particular sub-object.
0148     For example:
0149     \code
0150     Json::Value root;
0151     cin >> root["dir"]["file"];
0152     cout << root;
0153     \endcode
0154     Result:
0155     \verbatim
0156     {
0157     "dir": {
0158         "file": {
0159         // The input stream JSON would be nested here.
0160         }
0161     }
0162     }
0163     \endverbatim
0164     \throw std::exception on parse error.
0165     \see Json::operator<<()
0166    */
0167   std::istream &operator>>(std::istream &, Value &);
0168 
0169 }  // namespace Json
0170 
0171 #endif  // CPPTL_JSON_READER_H_INCLUDED