blob: d2bd140344499df071340102d3fc3c4ce76c6526 [file] [log] [blame]
Baptiste Lepilleur7469f1d2010-04-20 21:35:19 +00001// 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 Dunn6d135cb2007-06-13 15:51:04 +00006#ifndef CPPTL_JSON_READER_H_INCLUDED
Aaron Jacobs9fa4e842014-07-01 08:48:54 +10007#define CPPTL_JSON_READER_H_INCLUDED
Christopher Dunn6d135cb2007-06-13 15:51:04 +00008
Baptiste Lepilleureadc4782011-05-02 21:09:30 +00009#if !defined(JSON_IS_AMALGAMATION)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100010#include "features.h"
11#include "value.h"
Baptiste Lepilleureadc4782011-05-02 21:09:30 +000012#endif // if !defined(JSON_IS_AMALGAMATION)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100013#include <deque>
14#include <iosfwd>
15#include <stack>
16#include <string>
Christopher Dunn2c1197c2015-01-29 14:29:40 -060017#include <istream>
Christopher Dunn6d135cb2007-06-13 15:51:04 +000018
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100019// Disable warning C4251: <data member>: <type> needs to have dll-interface to
20// be used by...
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000021#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100022#pragma warning(push)
23#pragma warning(disable : 4251)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000024#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
25
Christopher Dunn6d135cb2007-06-13 15:51:04 +000026namespace Json {
27
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100028/** \brief Unserialize a <a HREF="http://www.json.org">JSON</a> document into a
29 *Value.
30 *
Christopher Dunn8df98f62015-02-09 11:15:39 -060031 * \deprecated Use CharReader and CharReaderBuilder.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100032 */
33class JSON_API Reader {
34public:
35 typedef char Char;
Aaron Jacobs11086dd2014-09-15 10:15:29 +100036 typedef const Char* Location;
Christopher Dunn6d135cb2007-06-13 15:51:04 +000037
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100038 /** \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 Dunn6d135cb2007-06-13 15:51:04 +000043 */
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100044 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 Jacobs11086dd2014-09-15 10:15:29 +100058 Reader(const Features& features);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100059
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 Jacobs11086dd2014-09-15 10:15:29 +100075 parse(const std::string& document, Value& root, bool collectComments = true);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100076
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 Dunn2c1197c2015-01-29 14:29:40 -060083 * Must be >= beginDoc.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100084 * \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 Jacobs11086dd2014-09-15 10:15:29 +100095 bool parse(const char* beginDoc,
96 const char* endDoc,
97 Value& root,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100098 bool collectComments = true);
99
100 /// \brief Parse from input stream.
101 /// \see Json::operator>>(std::istream&, Json::Value&).
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000102 bool parse(std::istream& is, Value& root, bool collectComments = true);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000103
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 Kimb84a39c2014-10-23 02:03:43 -0500135 /** \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 Jacobs9fa4e842014-07-01 08:48:54 +1000158private:
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 Jacobs11086dd2014-09-15 10:15:29 +1000192 bool readToken(Token& token);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000193 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 Jacobs11086dd2014-09-15 10:15:29 +1000201 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 Jacobs9fa4e842014-07-01 08:48:54 +1000211 Location end,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000212 unsigned int& unicode);
213 bool decodeUnicodeEscapeSequence(Token& token,
214 Location& current,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000215 Location end,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000216 unsigned int& unicode);
217 bool addError(const std::string& message, Token& token, Location extra = 0);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000218 bool recoverFromError(TokenType skipUntilToken);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000219 bool addErrorAndRecover(const std::string& message,
220 Token& token,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000221 TokenType skipUntilToken);
222 void skipUntilSpace();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000223 Value& currentValue();
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000224 Char getNextChar();
225 void
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000226 getLocationLineAndColumn(Location location, int& line, int& column) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000227 std::string getLocationLineAndColumn(Location location) const;
228 void addComment(Location begin, Location end, CommentPlacement placement);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000229 void skipCommentTokens(Token& token);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000230
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000231 typedef std::stack<Value*> Nodes;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000232 Nodes nodes_;
233 Errors errors_;
234 std::string document_;
235 Location begin_;
236 Location end_;
237 Location current_;
238 Location lastValueEnd_;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000239 Value* lastValue_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000240 std::string commentsBefore_;
241 Features features_;
242 bool collectComments_;
Christopher Dunn2c1197c2015-01-29 14:29:40 -0600243}; // Reader
244
245/** Interface for reading JSON from a char array.
246 */
247class JSON_API CharReader {
248public:
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:
273 /// \brief Allocate a CharReader via operator new().
274 virtual CharReader* newCharReader() const = 0;
275 }; // Factory
276}; // CharReader
277
Christopher Dunn66a8ba22015-02-09 01:29:43 -0600278/** \brief Build a CharReader implementation.
279
Christopher Dunn1357cdd2015-02-09 11:46:25 -0600280 \deprecated This is experimental and will be altered before the next release.
281
Christopher Dunn66a8ba22015-02-09 01:29:43 -0600282Usage:
283\code
284 using namespace Json;
285 CharReaderBuilder builder;
Christopher Dunn8df98f62015-02-09 11:15:39 -0600286 builder.collectComments_ = false;
Christopher Dunn66a8ba22015-02-09 01:29:43 -0600287 Value value;
288 std::string errs;
Christopher Dunn8df98f62015-02-09 11:15:39 -0600289 bool ok = parseFromStream(builder, std::cin, &value, &errs);
Christopher Dunn66a8ba22015-02-09 01:29:43 -0600290\endcode
291*/
Christopher Dunn2c1197c2015-01-29 14:29:40 -0600292class CharReaderBuilder : public CharReader::Factory {
Christopher Dunn66a8ba22015-02-09 01:29:43 -0600293public:
Christopher Dunn8df98f62015-02-09 11:15:39 -0600294 /** default: true
295 *
296 * It is possible to "allow" comments but still not "collect" them.
297 */
Christopher Dunn2c1197c2015-01-29 14:29:40 -0600298 bool collectComments_;
Christopher Dunn8df98f62015-02-09 11:15:39 -0600299 /** default: all()
300 *
301 * For historical reasons, Features is a separate structure.
302 */
Christopher Dunn2c1197c2015-01-29 14:29:40 -0600303 Features features_;
Christopher Dunn66a8ba22015-02-09 01:29:43 -0600304
Christopher Dunn2c1197c2015-01-29 14:29:40 -0600305 CharReaderBuilder();
Christopher Dunn66a8ba22015-02-09 01:29:43 -0600306 virtual ~CharReaderBuilder();
Christopher Dunn2c1197c2015-01-29 14:29:40 -0600307
308 virtual CharReader* newCharReader() const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000309};
310
Christopher Dunn2c1197c2015-01-29 14:29:40 -0600311/** Consume entire stream and use its begin/end.
312 * Someday we might have a real StreamReader, but for now this
313 * is convenient.
314 */
315bool parseFromStream(
316 CharReader::Factory const&,
317 std::istream&,
318 Value* root, std::string* errs);
319
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000320/** \brief Read from 'sin' into 'root'.
321
322 Always keep comments from the input JSON.
323
324 This can be used to read a file into a particular sub-object.
325 For example:
326 \code
327 Json::Value root;
328 cin >> root["dir"]["file"];
329 cout << root;
330 \endcode
331 Result:
332 \verbatim
333 {
334 "dir": {
335 "file": {
336 // The input stream JSON would be nested here.
337 }
338 }
339 }
340 \endverbatim
341 \throw std::exception on parse error.
342 \see Json::operator<<()
343*/
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000344JSON_API std::istream& operator>>(std::istream&, Value&);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000345
346} // namespace Json
347
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000348#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000349#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000350#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
351
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000352#endif // CPPTL_JSON_READER_H_INCLUDED