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