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