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 | 2c1197c | 2015-01-29 14:29:40 -0600 | [diff] [blame] | 17 | #include <istream> |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 18 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 19 | // Disable warning C4251: <data member>: <type> needs to have dll-interface to |
| 20 | // be used by... |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 21 | #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 22 | #pragma warning(push) |
| 23 | #pragma warning(disable : 4251) |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 24 | #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
| 25 | |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 26 | namespace Json { |
| 27 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 28 | /** \brief Unserialize a <a HREF="http://www.json.org">JSON</a> document into a |
| 29 | *Value. |
| 30 | * |
Christopher Dunn | 8df98f6 | 2015-02-09 11:15:39 -0600 | [diff] [blame^] | 31 | * \deprecated Use CharReader and CharReaderBuilder. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 32 | */ |
| 33 | class JSON_API Reader { |
| 34 | public: |
| 35 | typedef char Char; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 36 | typedef const Char* Location; |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 37 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 38 | /** \brief An error tagged with where in the JSON text it was encountered. |
| 39 | * |
| 40 | * The offsets give the [start, limit) range of bytes within the text. Note |
| 41 | * that this is bytes, not codepoints. |
| 42 | * |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 43 | */ |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 44 | struct StructuredError { |
| 45 | size_t offset_start; |
| 46 | size_t offset_limit; |
| 47 | std::string message; |
| 48 | }; |
| 49 | |
| 50 | /** \brief Constructs a Reader allowing all features |
| 51 | * for parsing. |
| 52 | */ |
| 53 | Reader(); |
| 54 | |
| 55 | /** \brief Constructs a Reader allowing the specified feature set |
| 56 | * for parsing. |
| 57 | */ |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 58 | Reader(const Features& features); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 59 | |
| 60 | /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> |
| 61 | * document. |
| 62 | * \param document UTF-8 encoded string containing the document to read. |
| 63 | * \param root [out] Contains the root value of the document if it was |
| 64 | * successfully parsed. |
| 65 | * \param collectComments \c true to collect comment and allow writing them |
| 66 | * back during |
| 67 | * serialization, \c false to discard comments. |
| 68 | * This parameter is ignored if |
| 69 | * Features::allowComments_ |
| 70 | * is \c false. |
| 71 | * \return \c true if the document was successfully parsed, \c false if an |
| 72 | * error occurred. |
| 73 | */ |
| 74 | bool |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 75 | parse(const std::string& document, Value& root, bool collectComments = true); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 76 | |
| 77 | /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> |
| 78 | document. |
| 79 | * \param beginDoc Pointer on the beginning of the UTF-8 encoded string of the |
| 80 | document to read. |
| 81 | * \param endDoc Pointer on the end of the UTF-8 encoded string of the |
| 82 | document to read. |
Christopher Dunn | 2c1197c | 2015-01-29 14:29:40 -0600 | [diff] [blame] | 83 | * Must be >= beginDoc. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 84 | * \param root [out] Contains the root value of the document if it was |
| 85 | * successfully parsed. |
| 86 | * \param collectComments \c true to collect comment and allow writing them |
| 87 | back during |
| 88 | * serialization, \c false to discard comments. |
| 89 | * This parameter is ignored if |
| 90 | Features::allowComments_ |
| 91 | * is \c false. |
| 92 | * \return \c true if the document was successfully parsed, \c false if an |
| 93 | error occurred. |
| 94 | */ |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 95 | bool parse(const char* beginDoc, |
| 96 | const char* endDoc, |
| 97 | Value& root, |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 98 | bool collectComments = true); |
| 99 | |
| 100 | /// \brief Parse from input stream. |
| 101 | /// \see Json::operator>>(std::istream&, Json::Value&). |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 102 | bool parse(std::istream& is, Value& root, bool collectComments = true); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 103 | |
| 104 | /** \brief Returns a user friendly string that list errors in the parsed |
| 105 | * document. |
| 106 | * \return Formatted error message with the list of errors with their location |
| 107 | * in |
| 108 | * the parsed document. An empty string is returned if no error |
| 109 | * occurred |
| 110 | * during parsing. |
| 111 | * \deprecated Use getFormattedErrorMessages() instead (typo fix). |
| 112 | */ |
| 113 | JSONCPP_DEPRECATED("Use getFormattedErrorMessages instead") |
| 114 | std::string getFormatedErrorMessages() const; |
| 115 | |
| 116 | /** \brief Returns a user friendly string that list errors in the parsed |
| 117 | * document. |
| 118 | * \return Formatted error message with the list of errors with their location |
| 119 | * in |
| 120 | * the parsed document. An empty string is returned if no error |
| 121 | * occurred |
| 122 | * during parsing. |
| 123 | */ |
| 124 | std::string getFormattedErrorMessages() const; |
| 125 | |
| 126 | /** \brief Returns a vector of structured erros encounted while parsing. |
| 127 | * \return A (possibly empty) vector of StructuredError objects. Currently |
| 128 | * only one error can be returned, but the caller should tolerate |
| 129 | * multiple |
| 130 | * errors. This can occur if the parser recovers from a non-fatal |
| 131 | * parse error and then encounters additional errors. |
| 132 | */ |
| 133 | std::vector<StructuredError> getStructuredErrors() const; |
| 134 | |
Mara Kim | b84a39c | 2014-10-23 02:03:43 -0500 | [diff] [blame] | 135 | /** \brief Add a semantic error message. |
| 136 | * \param value JSON Value location associated with the error |
| 137 | * \param message The error message. |
| 138 | * \return \c true if the error was successfully added, \c false if the |
| 139 | * Value offset exceeds the document size. |
| 140 | */ |
| 141 | bool pushError(const Value& value, const std::string& message); |
| 142 | |
| 143 | /** \brief Add a semantic error message with extra context. |
| 144 | * \param value JSON Value location associated with the error |
| 145 | * \param message The error message. |
| 146 | * \param extra Additional JSON Value location to contextualize the error |
| 147 | * \return \c true if the error was successfully added, \c false if either |
| 148 | * Value offset exceeds the document size. |
| 149 | */ |
| 150 | bool pushError(const Value& value, const std::string& message, const Value& extra); |
| 151 | |
| 152 | /** \brief Return whether there are any errors. |
| 153 | * \return \c true if there are no errors to report \c false if |
| 154 | * errors have occurred. |
| 155 | */ |
| 156 | bool good() const; |
| 157 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 158 | private: |
| 159 | enum TokenType { |
| 160 | tokenEndOfStream = 0, |
| 161 | tokenObjectBegin, |
| 162 | tokenObjectEnd, |
| 163 | tokenArrayBegin, |
| 164 | tokenArrayEnd, |
| 165 | tokenString, |
| 166 | tokenNumber, |
| 167 | tokenTrue, |
| 168 | tokenFalse, |
| 169 | tokenNull, |
| 170 | tokenArraySeparator, |
| 171 | tokenMemberSeparator, |
| 172 | tokenComment, |
| 173 | tokenError |
| 174 | }; |
| 175 | |
| 176 | class Token { |
| 177 | public: |
| 178 | TokenType type_; |
| 179 | Location start_; |
| 180 | Location end_; |
| 181 | }; |
| 182 | |
| 183 | class ErrorInfo { |
| 184 | public: |
| 185 | Token token_; |
| 186 | std::string message_; |
| 187 | Location extra_; |
| 188 | }; |
| 189 | |
| 190 | typedef std::deque<ErrorInfo> Errors; |
| 191 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 192 | bool readToken(Token& token); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 193 | void skipSpaces(); |
| 194 | bool match(Location pattern, int patternLength); |
| 195 | bool readComment(); |
| 196 | bool readCStyleComment(); |
| 197 | bool readCppStyleComment(); |
| 198 | bool readString(); |
| 199 | void readNumber(); |
| 200 | bool readValue(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 201 | bool readObject(Token& token); |
| 202 | bool readArray(Token& token); |
| 203 | bool decodeNumber(Token& token); |
| 204 | bool decodeNumber(Token& token, Value& decoded); |
| 205 | bool decodeString(Token& token); |
| 206 | bool decodeString(Token& token, std::string& decoded); |
| 207 | bool decodeDouble(Token& token); |
| 208 | bool decodeDouble(Token& token, Value& decoded); |
| 209 | bool decodeUnicodeCodePoint(Token& token, |
| 210 | Location& current, |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 211 | Location end, |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 212 | unsigned int& unicode); |
| 213 | bool decodeUnicodeEscapeSequence(Token& token, |
| 214 | Location& current, |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 215 | Location end, |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 216 | unsigned int& unicode); |
| 217 | bool addError(const std::string& message, Token& token, Location extra = 0); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 218 | bool recoverFromError(TokenType skipUntilToken); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 219 | bool addErrorAndRecover(const std::string& message, |
| 220 | Token& token, |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 221 | TokenType skipUntilToken); |
| 222 | void skipUntilSpace(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 223 | Value& currentValue(); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 224 | Char getNextChar(); |
| 225 | void |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 226 | getLocationLineAndColumn(Location location, int& line, int& column) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 227 | std::string getLocationLineAndColumn(Location location) const; |
| 228 | void addComment(Location begin, Location end, CommentPlacement placement); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 229 | void skipCommentTokens(Token& token); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 230 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 231 | typedef std::stack<Value*> Nodes; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 232 | Nodes nodes_; |
| 233 | Errors errors_; |
| 234 | std::string document_; |
| 235 | Location begin_; |
| 236 | Location end_; |
| 237 | Location current_; |
| 238 | Location lastValueEnd_; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 239 | Value* lastValue_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 240 | std::string commentsBefore_; |
| 241 | Features features_; |
| 242 | bool collectComments_; |
Christopher Dunn | 2c1197c | 2015-01-29 14:29:40 -0600 | [diff] [blame] | 243 | }; // Reader |
| 244 | |
| 245 | /** Interface for reading JSON from a char array. |
| 246 | */ |
| 247 | class JSON_API CharReader { |
| 248 | public: |
| 249 | virtual ~CharReader() {} |
| 250 | /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> |
| 251 | document. |
| 252 | * The document must be a UTF-8 encoded string containing the document to read. |
| 253 | * |
| 254 | * \param beginDoc Pointer on the beginning of the UTF-8 encoded string of the |
| 255 | document to read. |
| 256 | * \param endDoc Pointer on the end of the UTF-8 encoded string of the |
| 257 | document to read. |
| 258 | * Must be >= beginDoc. |
| 259 | * \param root [out] Contains the root value of the document if it was |
| 260 | * successfully parsed. |
| 261 | * \param errs [out] Formatted error messages (if not NULL) |
| 262 | * a user friendly string that lists errors in the parsed |
| 263 | * document. |
| 264 | * \return \c true if the document was successfully parsed, \c false if an |
| 265 | error occurred. |
| 266 | */ |
| 267 | virtual bool parse( |
| 268 | char const* beginDoc, char const* endDoc, |
| 269 | Value* root, std::string* errs) = 0; |
| 270 | |
| 271 | class Factory { |
| 272 | public: |
| 273 | /// \brief Allocate a CharReader via operator new(). |
| 274 | virtual CharReader* newCharReader() const = 0; |
| 275 | }; // Factory |
| 276 | }; // CharReader |
| 277 | |
Christopher Dunn | 66a8ba2 | 2015-02-09 01:29:43 -0600 | [diff] [blame] | 278 | /** \brief Build a CharReader implementation. |
| 279 | |
| 280 | Usage: |
| 281 | \code |
| 282 | using namespace Json; |
| 283 | CharReaderBuilder builder; |
Christopher Dunn | 8df98f6 | 2015-02-09 11:15:39 -0600 | [diff] [blame^] | 284 | builder.collectComments_ = false; |
Christopher Dunn | 66a8ba2 | 2015-02-09 01:29:43 -0600 | [diff] [blame] | 285 | Value value; |
| 286 | std::string errs; |
Christopher Dunn | 8df98f6 | 2015-02-09 11:15:39 -0600 | [diff] [blame^] | 287 | bool ok = parseFromStream(builder, std::cin, &value, &errs); |
Christopher Dunn | 66a8ba2 | 2015-02-09 01:29:43 -0600 | [diff] [blame] | 288 | \endcode |
| 289 | */ |
Christopher Dunn | 2c1197c | 2015-01-29 14:29:40 -0600 | [diff] [blame] | 290 | class CharReaderBuilder : public CharReader::Factory { |
Christopher Dunn | 66a8ba2 | 2015-02-09 01:29:43 -0600 | [diff] [blame] | 291 | public: |
Christopher Dunn | 8df98f6 | 2015-02-09 11:15:39 -0600 | [diff] [blame^] | 292 | /** default: true |
| 293 | * |
| 294 | * It is possible to "allow" comments but still not "collect" them. |
| 295 | */ |
Christopher Dunn | 2c1197c | 2015-01-29 14:29:40 -0600 | [diff] [blame] | 296 | bool collectComments_; |
Christopher Dunn | 8df98f6 | 2015-02-09 11:15:39 -0600 | [diff] [blame^] | 297 | /** default: all() |
| 298 | * |
| 299 | * For historical reasons, Features is a separate structure. |
| 300 | */ |
Christopher Dunn | 2c1197c | 2015-01-29 14:29:40 -0600 | [diff] [blame] | 301 | Features features_; |
Christopher Dunn | 66a8ba2 | 2015-02-09 01:29:43 -0600 | [diff] [blame] | 302 | |
Christopher Dunn | 2c1197c | 2015-01-29 14:29:40 -0600 | [diff] [blame] | 303 | CharReaderBuilder(); |
Christopher Dunn | 66a8ba2 | 2015-02-09 01:29:43 -0600 | [diff] [blame] | 304 | virtual ~CharReaderBuilder(); |
Christopher Dunn | 2c1197c | 2015-01-29 14:29:40 -0600 | [diff] [blame] | 305 | |
| 306 | virtual CharReader* newCharReader() const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 307 | }; |
| 308 | |
Christopher Dunn | 2c1197c | 2015-01-29 14:29:40 -0600 | [diff] [blame] | 309 | /** Consume entire stream and use its begin/end. |
| 310 | * Someday we might have a real StreamReader, but for now this |
| 311 | * is convenient. |
| 312 | */ |
| 313 | bool parseFromStream( |
| 314 | CharReader::Factory const&, |
| 315 | std::istream&, |
| 316 | Value* root, std::string* errs); |
| 317 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 318 | /** \brief Read from 'sin' into 'root'. |
| 319 | |
| 320 | Always keep comments from the input JSON. |
| 321 | |
| 322 | This can be used to read a file into a particular sub-object. |
| 323 | For example: |
| 324 | \code |
| 325 | Json::Value root; |
| 326 | cin >> root["dir"]["file"]; |
| 327 | cout << root; |
| 328 | \endcode |
| 329 | Result: |
| 330 | \verbatim |
| 331 | { |
| 332 | "dir": { |
| 333 | "file": { |
| 334 | // The input stream JSON would be nested here. |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | \endverbatim |
| 339 | \throw std::exception on parse error. |
| 340 | \see Json::operator<<() |
| 341 | */ |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 342 | JSON_API std::istream& operator>>(std::istream&, Value&); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 343 | |
| 344 | } // namespace Json |
| 345 | |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 346 | #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 347 | #pragma warning(pop) |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 348 | #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
| 349 | |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 350 | #endif // CPPTL_JSON_READER_H_INCLUDED |