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 # include "json_features.h"
10 # include "json_value.h"
18 /** \brief Unserialize a <a HREF="http://www.json.org">JSON</a> document into a Value.
25 typedef const Char *Location;
27 /** \brief Constructs a Reader allowing all features
32 /** \brief Constructs a Reader allowing the specified feature set
35 Reader( const Features &features );
37 /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> document.
38 * \param document UTF-8 encoded string containing the document to read.
39 * \param root [out] Contains the root value of the document if it was
40 * successfully parsed.
41 * \param collectComments \c true to collect comment and allow writing them back during
42 * serialization, \c false to discard comments.
43 * This parameter is ignored if Features::allowComments_
45 * \return \c true if the document was successfully parsed, \c false if an error occurred.
47 bool parse( const std::string &document,
49 bool collectComments = true );
51 /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> document.
52 * \param document UTF-8 encoded string containing the document to read.
53 * \param root [out] Contains the root value of the document if it was
54 * successfully parsed.
55 * \param collectComments \c true to collect comment and allow writing them back during
56 * serialization, \c false to discard comments.
57 * This parameter is ignored if Features::allowComments_
59 * \return \c true if the document was successfully parsed, \c false if an error occurred.
61 bool parse( const char *beginDoc, const char *endDoc,
63 bool collectComments = true );
65 /// \brief Parse from input stream.
66 /// \see Json::operator>>(std::istream&, Json::Value&).
67 bool parse( std::istream &is,
69 bool collectComments = true );
71 /** \brief Returns a user friendly string that list errors in the parsed document.
72 * \return Formatted error message with the list of errors with their location in
73 * the parsed document. An empty string is returned if no error occurred
76 std::string getFormatedErrorMessages() const;
109 std::string message_;
113 typedef std::deque<ErrorInfo> Errors;
115 bool expectToken( TokenType type, Token &token, const char *message );
116 bool readToken( Token &token );
118 bool match( Location pattern,
121 bool readCStyleComment();
122 bool readCppStyleComment();
126 bool readObject( Token &token );
127 bool readArray( Token &token );
128 bool decodeNumber( Token &token );
129 bool decodeString( Token &token );
130 bool decodeString( Token &token, std::string &decoded );
131 bool decodeDouble( Token &token );
132 bool decodeUnicodeCodePoint( Token &token,
135 unsigned int &unicode );
136 bool decodeUnicodeEscapeSequence( Token &token,
139 unsigned int &unicode );
140 bool addError( const std::string &message,
142 Location extra = 0 );
143 bool recoverFromError( TokenType skipUntilToken );
144 bool addErrorAndRecover( const std::string &message,
146 TokenType skipUntilToken );
147 void skipUntilSpace();
148 Value ¤tValue();
150 void getLocationLineAndColumn( Location location,
153 std::string getLocationLineAndColumn( Location location ) const;
154 void addComment( Location begin,
156 CommentPlacement placement );
157 void skipCommentTokens( Token &token );
159 typedef std::stack<Value *> Nodes;
162 std::string document_;
166 Location lastValueEnd_;
168 std::string commentsBefore_;
170 bool collectComments_;
173 /** \brief Read from 'sin' into 'root'.
175 Always keep comments from the input JSON.
177 This can be used to read a file into a particular sub-object.
181 cin >> root["dir"]["file"];
189 // The input stream JSON would be nested here.
194 \throw std::exception on parse error.
195 \see Json::operator<<()
197 std::istream& operator>>( std::istream&, Value& );
201 #endif // CPPTL_JSON_READER_H_INCLUDED