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