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