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