ian@0
|
1 // Copyright 2007-2010 Baptiste Lepilleur
|
ian@0
|
2 // Distributed under MIT license, or public domain if desired and
|
ian@0
|
3 // recognized in your jurisdiction.
|
ian@0
|
4 // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
|
ian@0
|
5
|
ian@0
|
6 #ifndef CPPTL_JSON_READER_H_INCLUDED
|
ian@0
|
7 # define CPPTL_JSON_READER_H_INCLUDED
|
ian@0
|
8
|
ian@0
|
9 #if !defined(JSON_IS_AMALGAMATION)
|
ian@0
|
10 # include "features.h"
|
ian@0
|
11 # include "value.h"
|
ian@0
|
12 #endif // if !defined(JSON_IS_AMALGAMATION)
|
ian@0
|
13 # include <deque>
|
ian@0
|
14 # include <stack>
|
ian@0
|
15 # include <string>
|
ian@0
|
16 # include <iostream>
|
ian@0
|
17
|
ian@0
|
18 namespace Json {
|
ian@0
|
19
|
ian@0
|
20 /** \brief Unserialize a <a HREF="http://www.json.org">JSON</a> document into a Value.
|
ian@0
|
21 *
|
ian@0
|
22 */
|
ian@0
|
23 class JSON_API Reader
|
ian@0
|
24 {
|
ian@0
|
25 public:
|
ian@0
|
26 typedef char Char;
|
ian@0
|
27 typedef const Char *Location;
|
ian@0
|
28
|
ian@0
|
29 /** \brief Constructs a Reader allowing all features
|
ian@0
|
30 * for parsing.
|
ian@0
|
31 */
|
ian@0
|
32 Reader();
|
ian@0
|
33
|
ian@0
|
34 /** \brief Constructs a Reader allowing the specified feature set
|
ian@0
|
35 * for parsing.
|
ian@0
|
36 */
|
ian@0
|
37 Reader( const Features &features );
|
ian@0
|
38
|
ian@0
|
39 /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> document.
|
ian@0
|
40 * \param document UTF-8 encoded string containing the document to read.
|
ian@0
|
41 * \param root [out] Contains the root value of the document if it was
|
ian@0
|
42 * successfully parsed.
|
ian@0
|
43 * \param collectComments \c true to collect comment and allow writing them back during
|
ian@0
|
44 * serialization, \c false to discard comments.
|
ian@0
|
45 * This parameter is ignored if Features::allowComments_
|
ian@0
|
46 * is \c false.
|
ian@0
|
47 * \return \c true if the document was successfully parsed, \c false if an error occurred.
|
ian@0
|
48 */
|
ian@0
|
49 bool parse( const std::string &document,
|
ian@0
|
50 Value &root,
|
ian@0
|
51 bool collectComments = true );
|
ian@0
|
52
|
ian@0
|
53 /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> document.
|
ian@0
|
54 * \param beginDoc Pointer on the beginning of the UTF-8 encoded string of the document to read.
|
ian@0
|
55 * \param endDoc Pointer on the end of the UTF-8 encoded string of the document to read.
|
ian@0
|
56 \ Must be >= beginDoc.
|
ian@0
|
57 * \param root [out] Contains the root value of the document if it was
|
ian@0
|
58 * successfully parsed.
|
ian@0
|
59 * \param collectComments \c true to collect comment and allow writing them back during
|
ian@0
|
60 * serialization, \c false to discard comments.
|
ian@0
|
61 * This parameter is ignored if Features::allowComments_
|
ian@0
|
62 * is \c false.
|
ian@0
|
63 * \return \c true if the document was successfully parsed, \c false if an error occurred.
|
ian@0
|
64 */
|
ian@0
|
65 bool parse( const char *beginDoc, const char *endDoc,
|
ian@0
|
66 Value &root,
|
ian@0
|
67 bool collectComments = true );
|
ian@0
|
68
|
ian@0
|
69 /// \brief Parse from input stream.
|
ian@0
|
70 /// \see Json::operator>>(std::istream&, Json::Value&).
|
ian@0
|
71 bool parse( std::istream &is,
|
ian@0
|
72 Value &root,
|
ian@0
|
73 bool collectComments = true );
|
ian@0
|
74
|
ian@0
|
75 /** \brief Returns a user friendly string that list errors in the parsed document.
|
ian@0
|
76 * \return Formatted error message with the list of errors with their location in
|
ian@0
|
77 * the parsed document. An empty string is returned if no error occurred
|
ian@0
|
78 * during parsing.
|
ian@0
|
79 * \deprecated Use getFormattedErrorMessages() instead (typo fix).
|
ian@0
|
80 */
|
ian@0
|
81 JSONCPP_DEPRECATED("Use getFormattedErrorMessages instead")
|
ian@0
|
82 std::string getFormatedErrorMessages() const;
|
ian@0
|
83
|
ian@0
|
84 /** \brief Returns a user friendly string that list errors in the parsed document.
|
ian@0
|
85 * \return Formatted error message with the list of errors with their location in
|
ian@0
|
86 * the parsed document. An empty string is returned if no error occurred
|
ian@0
|
87 * during parsing.
|
ian@0
|
88 */
|
ian@0
|
89 std::string getFormattedErrorMessages() const;
|
ian@0
|
90
|
ian@0
|
91 private:
|
ian@0
|
92 enum TokenType
|
ian@0
|
93 {
|
ian@0
|
94 tokenEndOfStream = 0,
|
ian@0
|
95 tokenObjectBegin,
|
ian@0
|
96 tokenObjectEnd,
|
ian@0
|
97 tokenArrayBegin,
|
ian@0
|
98 tokenArrayEnd,
|
ian@0
|
99 tokenString,
|
ian@0
|
100 tokenNumber,
|
ian@0
|
101 tokenTrue,
|
ian@0
|
102 tokenFalse,
|
ian@0
|
103 tokenNull,
|
ian@0
|
104 tokenArraySeparator,
|
ian@0
|
105 tokenMemberSeparator,
|
ian@0
|
106 tokenComment,
|
ian@0
|
107 tokenError
|
ian@0
|
108 };
|
ian@0
|
109
|
ian@0
|
110 class Token
|
ian@0
|
111 {
|
ian@0
|
112 public:
|
ian@0
|
113 TokenType type_;
|
ian@0
|
114 Location start_;
|
ian@0
|
115 Location end_;
|
ian@0
|
116 };
|
ian@0
|
117
|
ian@0
|
118 class ErrorInfo
|
ian@0
|
119 {
|
ian@0
|
120 public:
|
ian@0
|
121 Token token_;
|
ian@0
|
122 std::string message_;
|
ian@0
|
123 Location extra_;
|
ian@0
|
124 };
|
ian@0
|
125
|
ian@0
|
126 typedef std::deque<ErrorInfo> Errors;
|
ian@0
|
127
|
ian@0
|
128 bool expectToken( TokenType type, Token &token, const char *message );
|
ian@0
|
129 bool readToken( Token &token );
|
ian@0
|
130 void skipSpaces();
|
ian@0
|
131 bool match( Location pattern,
|
ian@0
|
132 int patternLength );
|
ian@0
|
133 bool readComment();
|
ian@0
|
134 bool readCStyleComment();
|
ian@0
|
135 bool readCppStyleComment();
|
ian@0
|
136 bool readString();
|
ian@0
|
137 void readNumber();
|
ian@0
|
138 bool readValue();
|
ian@0
|
139 bool readObject( Token &token );
|
ian@0
|
140 bool readArray( Token &token );
|
ian@0
|
141 bool decodeNumber( Token &token );
|
ian@0
|
142 bool decodeString( Token &token );
|
ian@0
|
143 bool decodeString( Token &token, std::string &decoded );
|
ian@0
|
144 bool decodeDouble( Token &token );
|
ian@0
|
145 bool decodeUnicodeCodePoint( Token &token,
|
ian@0
|
146 Location ¤t,
|
ian@0
|
147 Location end,
|
ian@0
|
148 unsigned int &unicode );
|
ian@0
|
149 bool decodeUnicodeEscapeSequence( Token &token,
|
ian@0
|
150 Location ¤t,
|
ian@0
|
151 Location end,
|
ian@0
|
152 unsigned int &unicode );
|
ian@0
|
153 bool addError( const std::string &message,
|
ian@0
|
154 Token &token,
|
ian@0
|
155 Location extra = 0 );
|
ian@0
|
156 bool recoverFromError( TokenType skipUntilToken );
|
ian@0
|
157 bool addErrorAndRecover( const std::string &message,
|
ian@0
|
158 Token &token,
|
ian@0
|
159 TokenType skipUntilToken );
|
ian@0
|
160 void skipUntilSpace();
|
ian@0
|
161 Value ¤tValue();
|
ian@0
|
162 Char getNextChar();
|
ian@0
|
163 void getLocationLineAndColumn( Location location,
|
ian@0
|
164 int &line,
|
ian@0
|
165 int &column ) const;
|
ian@0
|
166 std::string getLocationLineAndColumn( Location location ) const;
|
ian@0
|
167 void addComment( Location begin,
|
ian@0
|
168 Location end,
|
ian@0
|
169 CommentPlacement placement );
|
ian@0
|
170 void skipCommentTokens( Token &token );
|
ian@0
|
171
|
ian@0
|
172 typedef std::stack<Value *> Nodes;
|
ian@0
|
173 Nodes nodes_;
|
ian@0
|
174 Errors errors_;
|
ian@0
|
175 std::string document_;
|
ian@0
|
176 Location begin_;
|
ian@0
|
177 Location end_;
|
ian@0
|
178 Location current_;
|
ian@0
|
179 Location lastValueEnd_;
|
ian@0
|
180 Value *lastValue_;
|
ian@0
|
181 std::string commentsBefore_;
|
ian@0
|
182 Features features_;
|
ian@0
|
183 bool collectComments_;
|
ian@0
|
184 };
|
ian@0
|
185
|
ian@0
|
186 /** \brief Read from 'sin' into 'root'.
|
ian@0
|
187
|
ian@0
|
188 Always keep comments from the input JSON.
|
ian@0
|
189
|
ian@0
|
190 This can be used to read a file into a particular sub-object.
|
ian@0
|
191 For example:
|
ian@0
|
192 \code
|
ian@0
|
193 Json::Value root;
|
ian@0
|
194 cin >> root["dir"]["file"];
|
ian@0
|
195 cout << root;
|
ian@0
|
196 \endcode
|
ian@0
|
197 Result:
|
ian@0
|
198 \verbatim
|
ian@0
|
199 {
|
ian@0
|
200 "dir": {
|
ian@0
|
201 "file": {
|
ian@0
|
202 // The input stream JSON would be nested here.
|
ian@0
|
203 }
|
ian@0
|
204 }
|
ian@0
|
205 }
|
ian@0
|
206 \endverbatim
|
ian@0
|
207 \throw std::exception on parse error.
|
ian@0
|
208 \see Json::operator<<()
|
ian@0
|
209 */
|
ian@0
|
210 std::istream& operator>>( std::istream&, Value& );
|
ian@0
|
211
|
ian@0
|
212 } // namespace Json
|
ian@0
|
213
|
ian@0
|
214 #endif // CPPTL_JSON_READER_H_INCLUDED
|