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