Devin Jeanpierre | 59e4d35 | 2017-07-21 03:44:36 -0700 | [diff] [blame] | 1 | // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors |
Baptiste Lepilleur | 7469f1d | 2010-04-20 21:35:19 +0000 | [diff] [blame] | 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) |
Jordan Bayles | 00b979f | 2019-09-25 14:04:53 -0700 | [diff] [blame] | 10 | #include "json_features.h" |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 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> |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 15 | #include <istream> |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 16 | #include <stack> |
| 17 | #include <string> |
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 | |
Sergiy80 | d6e666f | 2016-12-03 22:29:14 +0200 | [diff] [blame] | 26 | #pragma pack(push, 8) |
| 27 | |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 28 | namespace Json { |
| 29 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 30 | /** \brief Unserialize a <a HREF="http://www.json.org">JSON</a> document into a |
Billy Donahue | 483eba8 | 2019-07-14 18:41:48 -0400 | [diff] [blame] | 31 | * Value. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 32 | * |
Christopher Dunn | 8df98f6 | 2015-02-09 11:15:39 -0600 | [diff] [blame] | 33 | * \deprecated Use CharReader and CharReaderBuilder. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 34 | */ |
Jordan Bayles | f34bf24 | 2019-10-11 11:19:00 -0700 | [diff] [blame] | 35 | |
| 36 | class JSONCPP_DEPRECATED( |
| 37 | "Use CharReader and CharReaderBuilder instead.") JSON_API Reader { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 38 | public: |
| 39 | typedef char Char; |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 40 | typedef const Char* Location; |
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 | /** \brief An error tagged with where in the JSON text it was encountered. |
| 43 | * |
| 44 | * The offsets give the [start, limit) range of bytes within the text. Note |
| 45 | * that this is bytes, not codepoints. |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 46 | */ |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 47 | struct StructuredError { |
Christopher Dunn | d4513fc | 2016-02-06 09:25:20 -0600 | [diff] [blame] | 48 | ptrdiff_t offset_start; |
| 49 | ptrdiff_t offset_limit; |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 50 | String message; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 51 | }; |
| 52 | |
Billy Donahue | 483eba8 | 2019-07-14 18:41:48 -0400 | [diff] [blame] | 53 | /** \brief Constructs a Reader allowing all features for parsing. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 54 | */ |
Jordan Bayles | f34bf24 | 2019-10-11 11:19:00 -0700 | [diff] [blame] | 55 | JSONCPP_DEPRECATED("Use CharReader and CharReaderBuilder instead") |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 56 | Reader(); |
| 57 | |
Billy Donahue | 483eba8 | 2019-07-14 18:41:48 -0400 | [diff] [blame] | 58 | /** \brief Constructs a Reader allowing the specified feature set for parsing. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 59 | */ |
Jordan Bayles | f34bf24 | 2019-10-11 11:19:00 -0700 | [diff] [blame] | 60 | JSONCPP_DEPRECATED("Use CharReader and CharReaderBuilder instead") |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 61 | Reader(const Features& features); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 62 | |
| 63 | /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> |
| 64 | * document. |
Billy Donahue | 483eba8 | 2019-07-14 18:41:48 -0400 | [diff] [blame] | 65 | * |
| 66 | * \param document UTF-8 encoded string containing the document |
| 67 | * to read. |
| 68 | * \param[out] root Contains the root value of the document if it |
| 69 | * was successfully parsed. |
| 70 | * \param collectComments \c true to collect comment and allow writing |
| 71 | * them back during serialization, \c false to |
| 72 | * discard comments. This parameter is ignored |
| 73 | * if Features::allowComments_ is \c false. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 74 | * \return \c true if the document was successfully parsed, \c false if an |
| 75 | * error occurred. |
| 76 | */ |
Jordan Bayles | 81ae1d5 | 2019-09-16 12:37:14 -0700 | [diff] [blame] | 77 | bool parse(const std::string& document, Value& root, |
| 78 | bool collectComments = true); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 79 | |
| 80 | /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> |
Billy Donahue | 483eba8 | 2019-07-14 18:41:48 -0400 | [diff] [blame] | 81 | * document. |
| 82 | * |
| 83 | * \param beginDoc Pointer on the beginning of the UTF-8 encoded |
| 84 | * string of the document to read. |
| 85 | * \param endDoc Pointer on the end of the UTF-8 encoded string |
| 86 | * of the document to read. Must be >= beginDoc. |
| 87 | * \param[out] root Contains the root value of the document if it |
| 88 | * was successfully parsed. |
| 89 | * \param collectComments \c true to collect comment and allow writing |
| 90 | * them back during serialization, \c false to |
| 91 | * discard comments. This parameter is ignored |
| 92 | * if Features::allowComments_ is \c false. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 93 | * \return \c true if the document was successfully parsed, \c false if an |
Billy Donahue | 483eba8 | 2019-07-14 18:41:48 -0400 | [diff] [blame] | 94 | * error occurred. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 95 | */ |
Jordan Bayles | 81ae1d5 | 2019-09-16 12:37:14 -0700 | [diff] [blame] | 96 | bool parse(const char* beginDoc, const char* endDoc, Value& root, |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 97 | bool collectComments = true); |
| 98 | |
| 99 | /// \brief Parse from input stream. |
| 100 | /// \see Json::operator>>(std::istream&, Json::Value&). |
Jordan Bayles | f34bf24 | 2019-10-11 11:19:00 -0700 | [diff] [blame] | 101 | bool parse(IStream& is, Value& root, bool collectComments = true); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 102 | |
| 103 | /** \brief Returns a user friendly string that list errors in the parsed |
| 104 | * document. |
Billy Donahue | 483eba8 | 2019-07-14 18:41:48 -0400 | [diff] [blame] | 105 | * |
| 106 | * \return Formatted error message with the list of errors with their |
| 107 | * location in the parsed document. An empty string is returned if no error |
| 108 | * occurred during parsing. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 109 | * \deprecated Use getFormattedErrorMessages() instead (typo fix). |
| 110 | */ |
Jordan Bayles | f34bf24 | 2019-10-11 11:19:00 -0700 | [diff] [blame] | 111 | JSONCPP_DEPRECATED("Use getFormattedErrorMessages() instead.") |
| 112 | String getFormatedErrorMessages() const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 113 | |
| 114 | /** \brief Returns a user friendly string that list errors in the parsed |
| 115 | * document. |
Billy Donahue | 483eba8 | 2019-07-14 18:41:48 -0400 | [diff] [blame] | 116 | * |
| 117 | * \return Formatted error message with the list of errors with their |
| 118 | * location in the parsed document. An empty string is returned if no error |
| 119 | * occurred during parsing. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 120 | */ |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 121 | String getFormattedErrorMessages() const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 122 | |
aliha | 472adb6 | 2019-08-26 15:37:05 -0400 | [diff] [blame] | 123 | /** \brief Returns a vector of structured errors encountered while parsing. |
Billy Donahue | 483eba8 | 2019-07-14 18:41:48 -0400 | [diff] [blame] | 124 | * |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 125 | * \return A (possibly empty) vector of StructuredError objects. Currently |
Billy Donahue | 483eba8 | 2019-07-14 18:41:48 -0400 | [diff] [blame] | 126 | * only one error can be returned, but the caller should tolerate multiple |
| 127 | * errors. This can occur if the parser recovers from a non-fatal parse |
| 128 | * error and then encounters additional errors. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 129 | */ |
| 130 | std::vector<StructuredError> getStructuredErrors() const; |
| 131 | |
Mara Kim | b84a39c | 2014-10-23 02:03:43 -0500 | [diff] [blame] | 132 | /** \brief Add a semantic error message. |
Billy Donahue | 483eba8 | 2019-07-14 18:41:48 -0400 | [diff] [blame] | 133 | * |
| 134 | * \param value JSON Value location associated with the error |
Mara Kim | b84a39c | 2014-10-23 02:03:43 -0500 | [diff] [blame] | 135 | * \param message The error message. |
Billy Donahue | 483eba8 | 2019-07-14 18:41:48 -0400 | [diff] [blame] | 136 | * \return \c true if the error was successfully added, \c false if the Value |
| 137 | * offset exceeds the document size. |
Mara Kim | b84a39c | 2014-10-23 02:03:43 -0500 | [diff] [blame] | 138 | */ |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 139 | bool pushError(const Value& value, const String& message); |
Mara Kim | b84a39c | 2014-10-23 02:03:43 -0500 | [diff] [blame] | 140 | |
| 141 | /** \brief Add a semantic error message with extra context. |
Billy Donahue | 483eba8 | 2019-07-14 18:41:48 -0400 | [diff] [blame] | 142 | * |
| 143 | * \param value JSON Value location associated with the error |
Mara Kim | b84a39c | 2014-10-23 02:03:43 -0500 | [diff] [blame] | 144 | * \param message The error message. |
Billy Donahue | 483eba8 | 2019-07-14 18:41:48 -0400 | [diff] [blame] | 145 | * \param extra Additional JSON Value location to contextualize the error |
Mara Kim | b84a39c | 2014-10-23 02:03:43 -0500 | [diff] [blame] | 146 | * \return \c true if the error was successfully added, \c false if either |
| 147 | * Value offset exceeds the document size. |
| 148 | */ |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 149 | bool pushError(const Value& value, const String& message, const Value& extra); |
Mara Kim | b84a39c | 2014-10-23 02:03:43 -0500 | [diff] [blame] | 150 | |
| 151 | /** \brief Return whether there are any errors. |
Billy Donahue | 483eba8 | 2019-07-14 18:41:48 -0400 | [diff] [blame] | 152 | * |
| 153 | * \return \c true if there are no errors to report \c false if errors have |
| 154 | * occurred. |
Mara Kim | b84a39c | 2014-10-23 02:03:43 -0500 | [diff] [blame] | 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_; |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 186 | String message_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 187 | Location extra_; |
| 188 | }; |
| 189 | |
| 190 | typedef std::deque<ErrorInfo> Errors; |
| 191 | |
Jordan Bayles | f34bf24 | 2019-10-11 11:19:00 -0700 | [diff] [blame] | 192 | bool readToken(Token& token); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 193 | void skipSpaces(); |
dota17 | db61dba | 2019-09-17 01:35:48 +0800 | [diff] [blame] | 194 | bool match(const Char* pattern, int patternLength); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 195 | bool readComment(); |
| 196 | bool readCStyleComment(); |
| 197 | bool readCppStyleComment(); |
| 198 | bool readString(); |
| 199 | void readNumber(); |
| 200 | bool readValue(); |
Jordan Bayles | f34bf24 | 2019-10-11 11:19:00 -0700 | [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, String& decoded); |
| 207 | bool decodeDouble(Token& token); |
| 208 | bool decodeDouble(Token& token, Value& decoded); |
| 209 | bool decodeUnicodeCodePoint(Token& token, Location& current, Location end, |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 210 | unsigned int& unicode); |
Jordan Bayles | f34bf24 | 2019-10-11 11:19:00 -0700 | [diff] [blame] | 211 | bool decodeUnicodeEscapeSequence(Token& token, Location& current, |
Jordan Bayles | 81ae1d5 | 2019-09-16 12:37:14 -0700 | [diff] [blame] | 212 | Location end, unsigned int& unicode); |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 213 | bool addError(const String& message, Token& token, Location extra = nullptr); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 214 | bool recoverFromError(TokenType skipUntilToken); |
Jordan Bayles | 81ae1d5 | 2019-09-16 12:37:14 -0700 | [diff] [blame] | 215 | bool addErrorAndRecover(const String& message, Token& token, |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 216 | TokenType skipUntilToken); |
| 217 | void skipUntilSpace(); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 218 | Value& currentValue(); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 219 | Char getNextChar(); |
Jordan Bayles | f34bf24 | 2019-10-11 11:19:00 -0700 | [diff] [blame] | 220 | void getLocationLineAndColumn(Location location, int& line, |
| 221 | int& column) const; |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 222 | String getLocationLineAndColumn(Location location) const; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 223 | void addComment(Location begin, Location end, CommentPlacement placement); |
Jordan Bayles | f34bf24 | 2019-10-11 11:19:00 -0700 | [diff] [blame] | 224 | void skipCommentTokens(Token& token); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 225 | |
damiram | ef16a35 | 2017-08-02 22:44:42 -0700 | [diff] [blame] | 226 | static bool containsNewLine(Location begin, Location end); |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 227 | static String normalizeEOL(Location begin, Location end); |
damiram | ef16a35 | 2017-08-02 22:44:42 -0700 | [diff] [blame] | 228 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 229 | typedef std::stack<Value*> Nodes; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 230 | Nodes nodes_; |
| 231 | Errors errors_; |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 232 | String document_; |
Hans Johnson | e817e4f | 2019-01-14 17:09:22 -0600 | [diff] [blame] | 233 | Location begin_{}; |
| 234 | Location end_{}; |
| 235 | Location current_{}; |
| 236 | Location lastValueEnd_{}; |
| 237 | Value* lastValue_{}; |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 238 | String commentsBefore_; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 239 | Features features_; |
Hans Johnson | e817e4f | 2019-01-14 17:09:22 -0600 | [diff] [blame] | 240 | bool collectComments_{}; |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 241 | }; // Reader |
Christopher Dunn | 2c1197c | 2015-01-29 14:29:40 -0600 | [diff] [blame] | 242 | |
| 243 | /** Interface for reading JSON from a char array. |
| 244 | */ |
| 245 | class JSON_API CharReader { |
| 246 | public: |
Hans Johnson | e3e05c7 | 2019-01-14 17:09:26 -0600 | [diff] [blame] | 247 | virtual ~CharReader() = default; |
Christopher Dunn | 2c1197c | 2015-01-29 14:29:40 -0600 | [diff] [blame] | 248 | /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> |
Billy Donahue | 483eba8 | 2019-07-14 18:41:48 -0400 | [diff] [blame] | 249 | * document. The document must be a UTF-8 encoded string containing the |
| 250 | * document to read. |
Christopher Dunn | 2c1197c | 2015-01-29 14:29:40 -0600 | [diff] [blame] | 251 | * |
Billy Donahue | 483eba8 | 2019-07-14 18:41:48 -0400 | [diff] [blame] | 252 | * \param beginDoc Pointer on the beginning of the UTF-8 encoded string |
| 253 | * of the document to read. |
| 254 | * \param endDoc Pointer on the end of the UTF-8 encoded string of the |
| 255 | * document to read. Must be >= beginDoc. |
| 256 | * \param[out] root Contains the root value of the document if it was |
| 257 | * successfully parsed. |
| 258 | * \param[out] errs Formatted error messages (if not NULL) a user |
| 259 | * friendly string that lists errors in the parsed |
| 260 | * document. |
Christopher Dunn | 2c1197c | 2015-01-29 14:29:40 -0600 | [diff] [blame] | 261 | * \return \c true if the document was successfully parsed, \c false if an |
Billy Donahue | 483eba8 | 2019-07-14 18:41:48 -0400 | [diff] [blame] | 262 | * error occurred. |
Christopher Dunn | 2c1197c | 2015-01-29 14:29:40 -0600 | [diff] [blame] | 263 | */ |
Jordan Bayles | f34bf24 | 2019-10-11 11:19:00 -0700 | [diff] [blame] | 264 | virtual bool parse(char const* beginDoc, char const* endDoc, Value* root, |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 265 | String* errs) = 0; |
Christopher Dunn | 2c1197c | 2015-01-29 14:29:40 -0600 | [diff] [blame] | 266 | |
Ben Boeckel | 80def66 | 2015-09-28 15:45:11 -0400 | [diff] [blame] | 267 | class JSON_API Factory { |
Christopher Dunn | 2c1197c | 2015-01-29 14:29:40 -0600 | [diff] [blame] | 268 | public: |
Hans Johnson | e3e05c7 | 2019-01-14 17:09:26 -0600 | [diff] [blame] | 269 | virtual ~Factory() = default; |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 270 | /** \brief Allocate a CharReader via operator new(). |
| 271 | * \throw std::exception if something goes wrong (e.g. invalid settings) |
| 272 | */ |
Christopher Dunn | 2c1197c | 2015-01-29 14:29:40 -0600 | [diff] [blame] | 273 | virtual CharReader* newCharReader() const = 0; |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 274 | }; // Factory |
| 275 | }; // CharReader |
Christopher Dunn | 2c1197c | 2015-01-29 14:29:40 -0600 | [diff] [blame] | 276 | |
Christopher Dunn | 66a8ba2 | 2015-02-09 01:29:43 -0600 | [diff] [blame] | 277 | /** \brief Build a CharReader implementation. |
Billy Donahue | 483eba8 | 2019-07-14 18:41:48 -0400 | [diff] [blame] | 278 | * |
| 279 | * Usage: |
| 280 | * \code |
| 281 | * using namespace Json; |
| 282 | * CharReaderBuilder builder; |
| 283 | * builder["collectComments"] = false; |
| 284 | * Value value; |
| 285 | * String errs; |
| 286 | * bool ok = parseFromStream(builder, std::cin, &value, &errs); |
| 287 | * \endcode |
| 288 | */ |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 289 | class JSON_API CharReaderBuilder : public CharReader::Factory { |
Christopher Dunn | 66a8ba2 | 2015-02-09 01:29:43 -0600 | [diff] [blame] | 290 | public: |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 291 | // Note: We use a Json::Value so that we can add data-members to this class |
| 292 | // without a major version bump. |
| 293 | /** Configuration of this builder. |
Billy Donahue | 483eba8 | 2019-07-14 18:41:48 -0400 | [diff] [blame] | 294 | * These are case-sensitive. |
| 295 | * Available settings (case-sensitive): |
| 296 | * - `"collectComments": false or true` |
| 297 | * - true to collect comment and allow writing them back during |
| 298 | * serialization, false to discard comments. This parameter is ignored |
| 299 | * if allowComments is false. |
| 300 | * - `"allowComments": false or true` |
| 301 | * - true if comments are allowed. |
| 302 | * - `"strictRoot": false or true` |
| 303 | * - true if root must be either an array or an object value |
| 304 | * - `"allowDroppedNullPlaceholders": false or true` |
| 305 | * - true if dropped null placeholders are allowed. (See |
| 306 | * StreamWriterBuilder.) |
| 307 | * - `"allowNumericKeys": false or true` |
| 308 | * - true if numeric object keys are allowed. |
| 309 | * - `"allowSingleQuotes": false or true` |
| 310 | * - true if '' are allowed for strings (both keys and values) |
| 311 | * - `"stackLimit": integer` |
| 312 | * - Exceeding stackLimit (recursive depth of `readValue()`) will cause an |
| 313 | * exception. |
| 314 | * - This is a security issue (seg-faults caused by deeply nested JSON), so |
| 315 | * the default is low. |
| 316 | * - `"failIfExtra": false or true` |
| 317 | * - If true, `parse()` returns false when extra non-whitespace trails the |
| 318 | * JSON value in the input string. |
| 319 | * - `"rejectDupKeys": false or true` |
| 320 | * - If true, `parse()` returns false when a key is duplicated within an |
| 321 | * object. |
| 322 | * - `"allowSpecialFloats": false or true` |
| 323 | * - If true, special float values (NaNs and infinities) are allowed and |
| 324 | * their values are lossfree restorable. |
| 325 | * |
| 326 | * You can examine 'settings_` yourself to see the defaults. You can also |
| 327 | * write and read them just like any JSON Value. |
| 328 | * \sa setDefaults() |
| 329 | */ |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 330 | Json::Value settings_; |
Christopher Dunn | 66a8ba2 | 2015-02-09 01:29:43 -0600 | [diff] [blame] | 331 | |
Christopher Dunn | 2c1197c | 2015-01-29 14:29:40 -0600 | [diff] [blame] | 332 | CharReaderBuilder(); |
Hans Johnson | 2853b1c | 2019-01-11 13:58:53 -0600 | [diff] [blame] | 333 | ~CharReaderBuilder() override; |
Christopher Dunn | 2c1197c | 2015-01-29 14:29:40 -0600 | [diff] [blame] | 334 | |
Hans Johnson | 2853b1c | 2019-01-11 13:58:53 -0600 | [diff] [blame] | 335 | CharReader* newCharReader() const override; |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 336 | |
Christopher Dunn | f757c18 | 2015-02-09 18:24:56 -0600 | [diff] [blame] | 337 | /** \return true if 'settings' are legal and consistent; |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 338 | * otherwise, indicate bad settings via 'invalid'. |
| 339 | */ |
| 340 | bool validate(Json::Value* invalid) const; |
Christopher Dunn | c312dd5 | 2015-03-04 14:56:37 -0600 | [diff] [blame] | 341 | |
| 342 | /** A simple way to update a specific setting. |
| 343 | */ |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 344 | Value& operator[](const String& key); |
Christopher Dunn | c312dd5 | 2015-03-04 14:56:37 -0600 | [diff] [blame] | 345 | |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 346 | /** Called by ctor, but you can use this to reset settings_. |
| 347 | * \pre 'settings' != NULL (but Json::null is fine) |
Christopher Dunn | 3cf9175 | 2015-02-09 18:16:24 -0600 | [diff] [blame] | 348 | * \remark Defaults: |
Christopher Dunn | 56650e8 | 2015-04-20 13:10:31 -0700 | [diff] [blame] | 349 | * \snippet src/lib_json/json_reader.cpp CharReaderBuilderDefaults |
Christopher Dunn | a9e1ab3 | 2015-02-09 17:22:28 -0600 | [diff] [blame] | 350 | */ |
| 351 | static void setDefaults(Json::Value* settings); |
Christopher Dunn | 3cf9175 | 2015-02-09 18:16:24 -0600 | [diff] [blame] | 352 | /** Same as old Features::strictMode(). |
| 353 | * \pre 'settings' != NULL (but Json::null is fine) |
| 354 | * \remark Defaults: |
Christopher Dunn | 56650e8 | 2015-04-20 13:10:31 -0700 | [diff] [blame] | 355 | * \snippet src/lib_json/json_reader.cpp CharReaderBuilderStrictMode |
Christopher Dunn | 3cf9175 | 2015-02-09 18:16:24 -0600 | [diff] [blame] | 356 | */ |
| 357 | static void strictMode(Json::Value* settings); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 358 | }; |
| 359 | |
Christopher Dunn | 2c1197c | 2015-01-29 14:29:40 -0600 | [diff] [blame] | 360 | /** Consume entire stream and use its begin/end. |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 361 | * Someday we might have a real StreamReader, but for now this |
| 362 | * is convenient. |
| 363 | */ |
Jordan Bayles | f34bf24 | 2019-10-11 11:19:00 -0700 | [diff] [blame] | 364 | bool JSON_API parseFromStream(CharReader::Factory const&, IStream&, Value* root, |
Jordan Bayles | 645250b | 2019-07-10 18:56:30 -0700 | [diff] [blame] | 365 | String* errs); |
Christopher Dunn | 2c1197c | 2015-01-29 14:29:40 -0600 | [diff] [blame] | 366 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 367 | /** \brief Read from 'sin' into 'root'. |
Billy Donahue | 483eba8 | 2019-07-14 18:41:48 -0400 | [diff] [blame] | 368 | * |
| 369 | * Always keep comments from the input JSON. |
Jordan Bayles | 7b28698 | 2019-08-13 22:41:43 -0700 | [diff] [blame] | 370 | * |
Billy Donahue | 483eba8 | 2019-07-14 18:41:48 -0400 | [diff] [blame] | 371 | * This can be used to read a file into a particular sub-object. |
| 372 | * For example: |
| 373 | * \code |
| 374 | * Json::Value root; |
| 375 | * cin >> root["dir"]["file"]; |
| 376 | * cout << root; |
| 377 | * \endcode |
| 378 | * Result: |
| 379 | * \verbatim |
| 380 | * { |
| 381 | * "dir": { |
| 382 | * "file": { |
| 383 | * // The input stream JSON would be nested here. |
| 384 | * } |
| 385 | * } |
| 386 | * } |
| 387 | * \endverbatim |
| 388 | * \throw std::exception on parse error. |
| 389 | * \see Json::operator<<() |
| 390 | */ |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 391 | JSON_API IStream& operator>>(IStream&, Value&); |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 392 | |
| 393 | } // namespace Json |
| 394 | |
Sergiy80 | d6e666f | 2016-12-03 22:29:14 +0200 | [diff] [blame] | 395 | #pragma pack(pop) |
| 396 | |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 397 | #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 398 | #pragma warning(pop) |
Baptiste Lepilleur | eafd702 | 2013-05-08 20:21:11 +0000 | [diff] [blame] | 399 | #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) |
| 400 | |
Christopher Dunn | 6d135cb | 2007-06-13 15:51:04 +0000 | [diff] [blame] | 401 | #endif // CPPTL_JSON_READER_H_INCLUDED |