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
6
#ifndef CPPTL_JSON_READER_H_INCLUDED
7
#define CPPTL_JSON_READER_H_INCLUDED
9
#if !defined(JSON_IS_AMALGAMATION)
12
#endif // if !defined(JSON_IS_AMALGAMATION)
18
// Disable warning C4251: <data member>: <type> needs to have dll-interface to
20
#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
22
#pragma warning(disable : 4251)
23
#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
27
/** \brief Unserialize a <a HREF="http://www.json.org">JSON</a> document into a
31
class JSON_API Reader {
34
typedef const Char* Location;
36
/** \brief An error tagged with where in the JSON text it was encountered.
38
* The offsets give the [start, limit) range of bytes within the text. Note
39
* that this is bytes, not codepoints.
42
struct StructuredError {
48
/** \brief Constructs a Reader allowing all features
53
/** \brief Constructs a Reader allowing the specified feature set
56
Reader(const Features& features);
58
/** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a>
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
65
* serialization, \c false to discard comments.
66
* This parameter is ignored if
67
* Features::allowComments_
69
* \return \c true if the document was successfully parsed, \c false if an
73
parse(const std::string& document, Value& root, bool collectComments = true);
75
/** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a>
77
* \param beginDoc Pointer on the beginning of the UTF-8 encoded string of the
79
* \param endDoc Pointer on the end of the UTF-8 encoded string of the
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
86
* serialization, \c false to discard comments.
87
* This parameter is ignored if
88
Features::allowComments_
90
* \return \c true if the document was successfully parsed, \c false if an
93
bool parse(const char* beginDoc,
96
bool collectComments = true);
98
/// \brief Parse from input stream.
99
/// \see Json::operator>>(std::istream&, Json::Value&).
100
bool parse(std::istream& is, Value& root, bool collectComments = true);
102
/** \brief Returns a user friendly string that list errors in the parsed
104
* \return Formatted error message with the list of errors with their location
106
* the parsed document. An empty string is returned if no error
109
* \deprecated Use getFormattedErrorMessages() instead (typo fix).
111
JSONCPP_DEPRECATED("Use getFormattedErrorMessages instead")
112
std::string getFormatedErrorMessages() const;
114
/** \brief Returns a user friendly string that list errors in the parsed
116
* \return Formatted error message with the list of errors with their location
118
* the parsed document. An empty string is returned if no error
122
std::string getFormattedErrorMessages() const;
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
128
* errors. This can occur if the parser recovers from a non-fatal
129
* parse error and then encounters additional errors.
131
std::vector<StructuredError> getStructuredErrors() const;
133
/** \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.
139
bool pushError(const Value& value, const std::string& message);
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.
148
bool pushError(const Value& value, const std::string& message, const Value& extra);
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.
158
tokenEndOfStream = 0,
169
tokenMemberSeparator,
184
std::string message_;
188
typedef std::deque<ErrorInfo> Errors;
190
bool expectToken(TokenType type, Token& token, const char* message);
191
bool readToken(Token& token);
193
bool match(Location pattern, int patternLength);
195
bool readCStyleComment();
196
bool readCppStyleComment();
200
bool readObject(Token& token);
201
bool readArray(Token& token);
202
bool decodeNumber(Token& token);
203
bool decodeNumber(Token& token, Value& decoded);
204
bool decodeString(Token& token);
205
bool decodeString(Token& token, std::string& decoded);
206
bool decodeDouble(Token& token);
207
bool decodeDouble(Token& token, Value& decoded);
208
bool decodeUnicodeCodePoint(Token& token,
211
unsigned int& unicode);
212
bool decodeUnicodeEscapeSequence(Token& token,
215
unsigned int& unicode);
216
bool addError(const std::string& message, Token& token, Location extra = 0);
217
bool recoverFromError(TokenType skipUntilToken);
218
bool addErrorAndRecover(const std::string& message,
220
TokenType skipUntilToken);
221
void skipUntilSpace();
222
Value& currentValue();
225
getLocationLineAndColumn(Location location, int& line, int& column) const;
226
std::string getLocationLineAndColumn(Location location) const;
227
void addComment(Location begin, Location end, CommentPlacement placement);
228
void skipCommentTokens(Token& token);
230
typedef std::stack<Value*> Nodes;
233
std::string document_;
237
Location lastValueEnd_;
239
std::string commentsBefore_;
241
bool collectComments_;
244
/** \brief Read from 'sin' into 'root'.
246
Always keep comments from the input JSON.
248
This can be used to read a file into a particular sub-object.
252
cin >> root["dir"]["file"];
260
// The input stream JSON would be nested here.
265
\throw std::exception on parse error.
266
\see Json::operator<<()
268
JSON_API std::istream& operator>>(std::istream&, Value&);
272
#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
274
#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
276
#endif // CPPTL_JSON_READER_H_INCLUDED