Baptiste Lepilleur | 7469f1d | 2010-04-20 21:35:19 +0000 | [diff] [blame] | 1 | // Copyright 2007-2010 Baptiste Lepilleur |
| 2 | // Distributed under MIT license, or public domain if desired and |
| 3 | // recognized in your jurisdiction. |
| 4 | // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE |
| 5 | |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 6 | #ifndef CPPTL_JSON_READER_H_INCLUDED |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 7 | #define CPPTL_JSON_READER_H_INCLUDED |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 8 | |
Baptiste Lepilleur | eadc478 | 2011-05-02 21:09:30 +0000 | [diff] [blame] | 9 | #if !defined(JSON_IS_AMALGAMATION) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 10 | #include "features.h" |
| 11 | #include "value.h" |
Baptiste Lepilleur | eadc478 | 2011-05-02 21:09:30 +0000 | [diff] [blame] | 12 | #endif // if !defined(JSON_IS_AMALGAMATION) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 13 | #include <deque> |
| 14 | #include <iosfwd> |
| 15 | #include <stack> |
| 16 | #include <string> |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 17 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 18 | // Disable warning C4251: <data member>: <type> needs to have dll-interface to |
| 19 | // be used by... |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 20 | #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 21 | #pragma warning(push) |
| 22 | #pragma warning(disable : 4251) |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 23 | #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
| 24 | |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 25 | namespace Json { |
| 26 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 27 | /** \brief Unserialize a <a HREF="http://www.json.org">JSON</a> document into a |
| 28 | *Value. |
| 29 | * |
| 30 | */ |
| 31 | class JSON_API Reader { |
| 32 | public: |
| 33 | typedef char Char; |
| 34 | typedef const Char *Location; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 35 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 36 | /** \brief An error tagged with where in the JSON text it was encountered. |
| 37 | * |
| 38 | * The offsets give the [start, limit) range of bytes within the text. Note |
| 39 | * that this is bytes, not codepoints. |
| 40 | * |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 41 | */ |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 42 | struct StructuredError { |
| 43 | size_t offset_start; |
| 44 | size_t offset_limit; |
| 45 | std::string message; |
| 46 | }; |
| 47 | |
| 48 | /** \brief Constructs a Reader allowing all features |
| 49 | * for parsing. |
| 50 | */ |
| 51 | Reader(); |
| 52 | |
| 53 | /** \brief Constructs a Reader allowing the specified feature set |
| 54 | * for parsing. |
| 55 | */ |
| 56 | Reader(const Features &features); |
| 57 | |
| 58 | /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> |
| 59 | * document. |
| 60 | * \param document UTF-8 encoded string containing the document to read. |
| 61 | * \param root [out] Contains the root value of the document if it was |
| 62 | * successfully parsed. |
| 63 | * \param collectComments \c true to collect comment and allow writing them |
| 64 | * back during |
| 65 | * serialization, \c false to discard comments. |
| 66 | * This parameter is ignored if |
| 67 | * Features::allowComments_ |
| 68 | * is \c false. |
| 69 | * \return \c true if the document was successfully parsed, \c false if an |
| 70 | * error occurred. |
| 71 | */ |
| 72 | bool |
| 73 | parse(const std::string &document, Value &root, bool collectComments = true); |
| 74 | |
| 75 | /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> |
| 76 | document. |
| 77 | * \param beginDoc Pointer on the beginning of the UTF-8 encoded string of the |
| 78 | document to read. |
| 79 | * \param endDoc Pointer on the end of the UTF-8 encoded string of the |
| 80 | document to read. |
| 81 | \ Must be >= beginDoc. |
| 82 | * \param root [out] Contains the root value of the document if it was |
| 83 | * successfully parsed. |
| 84 | * \param collectComments \c true to collect comment and allow writing them |
| 85 | back during |
| 86 | * serialization, \c false to discard comments. |
| 87 | * This parameter is ignored if |
| 88 | Features::allowComments_ |
| 89 | * is \c false. |
| 90 | * \return \c true if the document was successfully parsed, \c false if an |
| 91 | error occurred. |
| 92 | */ |
| 93 | bool parse(const char *beginDoc, |
| 94 | const char *endDoc, |
| 95 | Value &root, |
| 96 | bool collectComments = true); |
| 97 | |
| 98 | /// \brief Parse from input stream. |
| 99 | /// \see Json::operator>>(std::istream&, Json::Value&). |
| 100 | bool parse(std::istream &is, Value &root, bool collectComments = true); |
| 101 | |
| 102 | /** \brief Returns a user friendly string that list errors in the parsed |
| 103 | * document. |
| 104 | * \return Formatted error message with the list of errors with their location |
| 105 | * in |
| 106 | * the parsed document. An empty string is returned if no error |
| 107 | * occurred |
| 108 | * during parsing. |
| 109 | * \deprecated Use getFormattedErrorMessages() instead (typo fix). |
| 110 | */ |
| 111 | JSONCPP_DEPRECATED("Use getFormattedErrorMessages instead") |
| 112 | std::string getFormatedErrorMessages() const; |
| 113 | |
| 114 | /** \brief Returns a user friendly string that list errors in the parsed |
| 115 | * document. |
| 116 | * \return Formatted error message with the list of errors with their location |
| 117 | * in |
| 118 | * the parsed document. An empty string is returned if no error |
| 119 | * occurred |
| 120 | * during parsing. |
| 121 | */ |
| 122 | std::string getFormattedErrorMessages() const; |
| 123 | |
| 124 | /** \brief Returns a vector of structured erros encounted while parsing. |
| 125 | * \return A (possibly empty) vector of StructuredError objects. Currently |
| 126 | * only one error can be returned, but the caller should tolerate |
| 127 | * multiple |
| 128 | * errors. This can occur if the parser recovers from a non-fatal |
| 129 | * parse error and then encounters additional errors. |
| 130 | */ |
| 131 | std::vector<StructuredError> getStructuredErrors() const; |
| 132 | |
| 133 | private: |
| 134 | enum TokenType { |
| 135 | tokenEndOfStream = 0, |
| 136 | tokenObjectBegin, |
| 137 | tokenObjectEnd, |
| 138 | tokenArrayBegin, |
| 139 | tokenArrayEnd, |
| 140 | tokenString, |
| 141 | tokenNumber, |
| 142 | tokenTrue, |
| 143 | tokenFalse, |
| 144 | tokenNull, |
| 145 | tokenArraySeparator, |
| 146 | tokenMemberSeparator, |
| 147 | tokenComment, |
| 148 | tokenError |
| 149 | }; |
| 150 | |
| 151 | class Token { |
| 152 | public: |
| 153 | TokenType type_; |
| 154 | Location start_; |
| 155 | Location end_; |
| 156 | }; |
| 157 | |
| 158 | class ErrorInfo { |
| 159 | public: |
| 160 | Token token_; |
| 161 | std::string message_; |
| 162 | Location extra_; |
| 163 | }; |
| 164 | |
| 165 | typedef std::deque<ErrorInfo> Errors; |
| 166 | |
| 167 | bool expectToken(TokenType type, Token &token, const char *message); |
| 168 | bool readToken(Token &token); |
| 169 | void skipSpaces(); |
| 170 | bool match(Location pattern, int patternLength); |
| 171 | bool readComment(); |
| 172 | bool readCStyleComment(); |
| 173 | bool readCppStyleComment(); |
| 174 | bool readString(); |
| 175 | void readNumber(); |
| 176 | bool readValue(); |
| 177 | bool readObject(Token &token); |
| 178 | bool readArray(Token &token); |
| 179 | bool decodeNumber(Token &token); |
| 180 | bool decodeNumber(Token &token, Value &decoded); |
| 181 | bool decodeString(Token &token); |
| 182 | bool decodeString(Token &token, std::string &decoded); |
| 183 | bool decodeDouble(Token &token); |
| 184 | bool decodeDouble(Token &token, Value &decoded); |
| 185 | bool decodeUnicodeCodePoint(Token &token, |
| 186 | Location ¤t, |
| 187 | Location end, |
| 188 | unsigned int &unicode); |
| 189 | bool decodeUnicodeEscapeSequence(Token &token, |
| 190 | Location ¤t, |
| 191 | Location end, |
| 192 | unsigned int &unicode); |
| 193 | bool addError(const std::string &message, Token &token, Location extra = 0); |
| 194 | bool recoverFromError(TokenType skipUntilToken); |
| 195 | bool addErrorAndRecover(const std::string &message, |
| 196 | Token &token, |
| 197 | TokenType skipUntilToken); |
| 198 | void skipUntilSpace(); |
| 199 | Value ¤tValue(); |
| 200 | Char getNextChar(); |
| 201 | void |
| 202 | getLocationLineAndColumn(Location location, int &line, int &column) const; |
| 203 | std::string getLocationLineAndColumn(Location location) const; |
| 204 | void addComment(Location begin, Location end, CommentPlacement placement); |
| 205 | void skipCommentTokens(Token &token); |
| 206 | |
| 207 | typedef std::stack<Value *> Nodes; |
| 208 | Nodes nodes_; |
| 209 | Errors errors_; |
| 210 | std::string document_; |
| 211 | Location begin_; |
| 212 | Location end_; |
| 213 | Location current_; |
| 214 | Location lastValueEnd_; |
| 215 | Value *lastValue_; |
| 216 | std::string commentsBefore_; |
| 217 | Features features_; |
| 218 | bool collectComments_; |
| 219 | }; |
| 220 | |
| 221 | /** \brief Read from 'sin' into 'root'. |
| 222 | |
| 223 | Always keep comments from the input JSON. |
| 224 | |
| 225 | This can be used to read a file into a particular sub-object. |
| 226 | For example: |
| 227 | \code |
| 228 | Json::Value root; |
| 229 | cin >> root["dir"]["file"]; |
| 230 | cout << root; |
| 231 | \endcode |
| 232 | Result: |
| 233 | \verbatim |
| 234 | { |
| 235 | "dir": { |
| 236 | "file": { |
| 237 | // The input stream JSON would be nested here. |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | \endverbatim |
| 242 | \throw std::exception on parse error. |
| 243 | \see Json::operator<<() |
| 244 | */ |
| 245 | JSON_API std::istream &operator>>(std::istream &, Value &); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 246 | |
| 247 | } // namespace Json |
| 248 | |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 249 | #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 250 | #pragma warning(pop) |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 251 | #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
| 252 | |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 253 | #endif // CPPTL_JSON_READER_H_INCLUDED |