blob: bd2204be481e03e4c4094c22e9668b4a8c91c4f2 [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 Dunn6d135cb2007-06-13 15:51:04 +000017
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100018// Disable warning C4251: <data member>: <type> needs to have dll-interface to
19// be used by...
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000020#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100021#pragma warning(push)
22#pragma warning(disable : 4251)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +000023#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
24
Christopher Dunn6d135cb2007-06-13 15:51:04 +000025namespace Json {
26
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100027/** \brief Unserialize a <a HREF="http://www.json.org">JSON</a> document into a
28 *Value.
29 *
30 */
31class JSON_API Reader {
32public:
33 typedef char Char;
Aaron Jacobs11086dd2014-09-15 10:15:29 +100034 typedef const Char* Location;
Christopher Dunn6d135cb2007-06-13 15:51:04 +000035
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100036 /** \brief An error tagged with where in the JSON text it was encountered.
37 *
38 * The offsets give the [start, limit) range of bytes within the text. Note
39 * that this is bytes, not codepoints.
40 *
Christopher Dunn6d135cb2007-06-13 15:51:04 +000041 */
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100042 struct StructuredError {
43 size_t offset_start;
44 size_t offset_limit;
45 std::string message;
46 };
47
48 /** \brief Constructs a Reader allowing all features
49 * for parsing.
50 */
51 Reader();
52
53 /** \brief Constructs a Reader allowing the specified feature set
54 * for parsing.
55 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +100056 Reader(const Features& features);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100057
58 /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a>
59 * document.
60 * \param document UTF-8 encoded string containing the document to read.
61 * \param root [out] Contains the root value of the document if it was
62 * successfully parsed.
63 * \param collectComments \c true to collect comment and allow writing them
64 * back during
65 * serialization, \c false to discard comments.
66 * This parameter is ignored if
67 * Features::allowComments_
68 * is \c false.
69 * \return \c true if the document was successfully parsed, \c false if an
70 * error occurred.
71 */
72 bool
Aaron Jacobs11086dd2014-09-15 10:15:29 +100073 parse(const std::string& document, Value& root, bool collectComments = true);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100074
75 /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a>
76 document.
77 * \param beginDoc Pointer on the beginning of the UTF-8 encoded string of the
78 document to read.
79 * \param endDoc Pointer on the end of the UTF-8 encoded string of the
80 document to read.
81 \ Must be >= beginDoc.
82 * \param root [out] Contains the root value of the document if it was
83 * successfully parsed.
84 * \param collectComments \c true to collect comment and allow writing them
85 back during
86 * serialization, \c false to discard comments.
87 * This parameter is ignored if
88 Features::allowComments_
89 * is \c false.
90 * \return \c true if the document was successfully parsed, \c false if an
91 error occurred.
92 */
Aaron Jacobs11086dd2014-09-15 10:15:29 +100093 bool parse(const char* beginDoc,
94 const char* endDoc,
95 Value& root,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100096 bool collectComments = true);
97
98 /// \brief Parse from input stream.
99 /// \see Json::operator>>(std::istream&, Json::Value&).
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000100 bool parse(std::istream& is, Value& root, bool collectComments = true);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000101
102 /** \brief Returns a user friendly string that list errors in the parsed
103 * document.
104 * \return Formatted error message with the list of errors with their location
105 * in
106 * the parsed document. An empty string is returned if no error
107 * occurred
108 * during parsing.
109 * \deprecated Use getFormattedErrorMessages() instead (typo fix).
110 */
111 JSONCPP_DEPRECATED("Use getFormattedErrorMessages instead")
112 std::string getFormatedErrorMessages() const;
113
114 /** \brief Returns a user friendly string that list errors in the parsed
115 * document.
116 * \return Formatted error message with the list of errors with their location
117 * in
118 * the parsed document. An empty string is returned if no error
119 * occurred
120 * during parsing.
121 */
122 std::string getFormattedErrorMessages() const;
123
124 /** \brief Returns a vector of structured erros encounted while parsing.
125 * \return A (possibly empty) vector of StructuredError objects. Currently
126 * only one error can be returned, but the caller should tolerate
127 * multiple
128 * errors. This can occur if the parser recovers from a non-fatal
129 * parse error and then encounters additional errors.
130 */
131 std::vector<StructuredError> getStructuredErrors() const;
132
Mara Kimb84a39c2014-10-23 02:03:43 -0500133 /** \brief Add a semantic error message.
134 * \param value JSON Value location associated with the error
135 * \param message The error message.
136 * \return \c true if the error was successfully added, \c false if the
137 * Value offset exceeds the document size.
138 */
139 bool pushError(const Value& value, const std::string& message);
140
141 /** \brief Add a semantic error message with extra context.
142 * \param value JSON Value location associated with the error
143 * \param message The error message.
144 * \param extra Additional JSON Value location to contextualize the error
145 * \return \c true if the error was successfully added, \c false if either
146 * Value offset exceeds the document size.
147 */
148 bool pushError(const Value& value, const std::string& message, const Value& extra);
149
150 /** \brief Return whether there are any errors.
151 * \return \c true if there are no errors to report \c false if
152 * errors have occurred.
153 */
154 bool good() const;
155
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000156private:
157 enum TokenType {
158 tokenEndOfStream = 0,
159 tokenObjectBegin,
160 tokenObjectEnd,
161 tokenArrayBegin,
162 tokenArrayEnd,
163 tokenString,
164 tokenNumber,
165 tokenTrue,
166 tokenFalse,
167 tokenNull,
168 tokenArraySeparator,
169 tokenMemberSeparator,
170 tokenComment,
171 tokenError
172 };
173
174 class Token {
175 public:
176 TokenType type_;
177 Location start_;
178 Location end_;
179 };
180
181 class ErrorInfo {
182 public:
183 Token token_;
184 std::string message_;
185 Location extra_;
186 };
187
188 typedef std::deque<ErrorInfo> Errors;
189
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000190 bool readToken(Token& token);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000191 void skipSpaces();
192 bool match(Location pattern, int patternLength);
193 bool readComment();
194 bool readCStyleComment();
195 bool readCppStyleComment();
196 bool readString();
197 void readNumber();
198 bool readValue();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000199 bool readObject(Token& token);
200 bool readArray(Token& token);
201 bool decodeNumber(Token& token);
202 bool decodeNumber(Token& token, Value& decoded);
203 bool decodeString(Token& token);
204 bool decodeString(Token& token, std::string& decoded);
205 bool decodeDouble(Token& token);
206 bool decodeDouble(Token& token, Value& decoded);
207 bool decodeUnicodeCodePoint(Token& token,
208 Location& current,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000209 Location end,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000210 unsigned int& unicode);
211 bool decodeUnicodeEscapeSequence(Token& token,
212 Location& current,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000213 Location end,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000214 unsigned int& unicode);
215 bool addError(const std::string& message, Token& token, Location extra = 0);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000216 bool recoverFromError(TokenType skipUntilToken);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000217 bool addErrorAndRecover(const std::string& message,
218 Token& token,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000219 TokenType skipUntilToken);
220 void skipUntilSpace();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000221 Value& currentValue();
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000222 Char getNextChar();
223 void
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000224 getLocationLineAndColumn(Location location, int& line, int& column) const;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000225 std::string getLocationLineAndColumn(Location location) const;
226 void addComment(Location begin, Location end, CommentPlacement placement);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000227 void skipCommentTokens(Token& token);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000228
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000229 typedef std::stack<Value*> Nodes;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000230 Nodes nodes_;
231 Errors errors_;
232 std::string document_;
233 Location begin_;
234 Location end_;
235 Location current_;
236 Location lastValueEnd_;
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000237 Value* lastValue_;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000238 std::string commentsBefore_;
239 Features features_;
240 bool collectComments_;
241};
242
243/** \brief Read from 'sin' into 'root'.
244
245 Always keep comments from the input JSON.
246
247 This can be used to read a file into a particular sub-object.
248 For example:
249 \code
250 Json::Value root;
251 cin >> root["dir"]["file"];
252 cout << root;
253 \endcode
254 Result:
255 \verbatim
256 {
257 "dir": {
258 "file": {
259 // The input stream JSON would be nested here.
260 }
261 }
262 }
263 \endverbatim
264 \throw std::exception on parse error.
265 \see Json::operator<<()
266*/
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000267JSON_API std::istream& operator>>(std::istream&, Value&);
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000268
269} // namespace Json
270
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000271#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000272#pragma warning(pop)
Baptiste Lepilleureafd7022013-05-08 20:21:11 +0000273#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
274
Christopher Dunn6d135cb2007-06-13 15:51:04 +0000275#endif // CPPTL_JSON_READER_H_INCLUDED