1
#ifndef CPPTL_JSON_READER_H_INCLUDED
2
# define CPPTL_JSON_READER_H_INCLUDED
13
/** @brief Unserialize a <a HREF="http://www.json.org">JSON</a> document into a Value.
20
typedef const Char *Location;
22
/** @brief Constructs a Reader allowing all features
27
/** @brief Constructs a Reader allowing the specified feature set
30
Reader( const Features &features );
32
/** @brief Read a Value from a <a HREF="http://www.json.org">JSON</a> document.
33
* \param document UTF-8 encoded string containing the document to read.
34
* \param root [out] Contains the root value of the document if it was
35
* successfully parsed.
36
* \param collectComments \c true to collect comment and allow writing them back during
37
* serialization, \c false to discard comments.
38
* This parameter is ignored if Features::allowComments_
40
* \return \c true if the document was successfully parsed, \c false if an error occurred.
42
bool parse( const std::string &document,
44
bool collectComments = true );
46
/** @brief Read a Value from a <a HREF="http://www.json.org">JSON</a> document.
47
* \param document UTF-8 encoded string containing the document to read.
48
* \param root [out] Contains the root value of the document if it was
49
* successfully parsed.
50
* \param collectComments \c true to collect comment and allow writing them back during
51
* serialization, \c false to discard comments.
52
* This parameter is ignored if Features::allowComments_
54
* \return \c true if the document was successfully parsed, \c false if an error occurred.
56
bool parse( const char *beginDoc, const char *endDoc,
58
bool collectComments = true );
60
/// @brief Parse from input stream.
61
/// \see Json::operator>>(std::istream&, Json::Value&).
62
bool parse( std::istream &is,
64
bool collectComments = true );
66
/** @brief Returns a user friendly string that list errors in the parsed document.
67
* \return Formatted error message with the list of errors with their location in
68
* the parsed document. An empty string is returned if no error occurred
71
std::string getFormatedErrorMessages() const;
104
std::string message_;
108
typedef std::deque<ErrorInfo> Errors;
110
bool expectToken( TokenType type, Token &token, const char *message );
111
bool readToken( Token &token );
113
bool match( Location pattern,
116
bool readCStyleComment();
117
bool readCppStyleComment();
121
bool readObject( Token &token );
122
bool readArray( Token &token );
123
bool decodeNumber( Token &token );
124
bool decodeString( Token &token );
125
bool decodeString( Token &token, std::string &decoded );
126
bool decodeDouble( Token &token );
127
bool decodeUnicodeCodePoint( Token &token,
130
unsigned int &unicode );
131
bool decodeUnicodeEscapeSequence( Token &token,
134
unsigned int &unicode );
135
bool addError( const std::string &message,
137
Location extra = 0 );
138
bool recoverFromError( TokenType skipUntilToken );
139
bool addErrorAndRecover( const std::string &message,
141
TokenType skipUntilToken );
142
void skipUntilSpace();
143
Value ¤tValue();
145
void getLocationLineAndColumn( Location location,
148
std::string getLocationLineAndColumn( Location location ) const;
149
void addComment( Location begin,
151
CommentPlacement placement );
152
void skipCommentTokens( Token &token );
154
typedef std::stack<Value *> Nodes;
157
std::string document_;
161
Location lastValueEnd_;
163
std::string commentsBefore_;
165
bool collectComments_;
168
/** @brief Read from 'sin' into 'root'.
170
Always keep comments from the input JSON.
172
This can be used to read a file into a particular sub-object.
176
cin >> root["dir"]["file"];
184
// The input stream JSON would be nested here.
189
\throw std::exception on parse error.
190
\see Json::operator<<()
192
std::istream& operator>>( std::istream&, Value& );
196
#endif // CPPTL_JSON_READER_H_INCLUDED