JsonCpp project page JsonCpp home page

reader.h
Go to the documentation of this file.
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
5 
6 #ifndef CPPTL_JSON_READER_H_INCLUDED
7 #define CPPTL_JSON_READER_H_INCLUDED
8 
9 #if !defined(JSON_IS_AMALGAMATION)
10 #include "features.h"
11 #include "value.h"
12 #endif // if !defined(JSON_IS_AMALGAMATION)
13 #include <deque>
14 #include <iosfwd>
15 #include <stack>
16 #include <string>
17 
18 // Disable warning C4251: <data member>: <type> needs to have dll-interface to
19 // be used by...
20 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
21 #pragma warning(push)
22 #pragma warning(disable : 4251)
23 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
24 
25 namespace Json {
26 
32 public:
33  typedef char Char;
34  typedef const Char* Location;
35 
42  struct StructuredError {
43  size_t offset_start;
44  size_t offset_limit;
45  std::string message;
46  };
47 
51  Reader();
52 
56  Reader(const Features& features);
57 
72  bool
73  parse(const std::string& document, Value& root, bool collectComments = true);
74 
93  bool parse(const char* beginDoc,
94  const char* endDoc,
95  Value& root,
96  bool collectComments = true);
97 
100  bool parse(std::istream& is, Value& root, bool collectComments = true);
101 
111  JSONCPP_DEPRECATED("Use getFormattedErrorMessages instead")
112  std::string getFormatedErrorMessages() const;
113 
122  std::string getFormattedErrorMessages() const;
123 
131  std::vector<StructuredError> getStructuredErrors() const;
132 
139  bool pushError(const Value& value, const std::string& message);
140 
148  bool pushError(const Value& value, const std::string& message, const Value& extra);
149 
154  bool good() const;
155 
156 private:
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 
190  bool expectToken(TokenType type, Token& token, const char* message);
191  bool readToken(Token& token);
192  void skipSpaces();
193  bool match(Location pattern, int patternLength);
194  bool readComment();
195  bool readCStyleComment();
196  bool readCppStyleComment();
197  bool readString();
198  void readNumber();
199  bool readValue();
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,
209  Location& current,
210  Location end,
211  unsigned int& unicode);
212  bool decodeUnicodeEscapeSequence(Token& token,
213  Location& current,
214  Location end,
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,
219  Token& token,
220  TokenType skipUntilToken);
221  void skipUntilSpace();
222  Value& currentValue();
223  Char getNextChar();
224  void
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);
229 
230  typedef std::stack<Value*> Nodes;
231  Nodes nodes_;
232  Errors errors_;
233  std::string document_;
234  Location begin_;
235  Location end_;
236  Location current_;
237  Location lastValueEnd_;
238  Value* lastValue_;
239  std::string commentsBefore_;
240  Features features_;
241  bool collectComments_;
242 };
243 
268 JSON_API std::istream& operator>>(std::istream&, Value&);
269 
270 } // namespace Json
271 
272 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
273 #pragma warning(pop)
274 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
275 
276 #endif // CPPTL_JSON_READER_H_INCLUDED
#define JSONCPP_DEPRECATED(message)
Definition: config.h:87
#define JSON_API
If defined, indicates that the source file is amalgated to prevent private header inclusion...
Definition: config.h:62
std::istream & operator>>(std::istream &, Value &)
Read from 'sin' into 'root'.
char Char
Definition: reader.h:33
An error tagged with where in the JSON text it was encountered.
Definition: reader.h:42
CommentPlacement
Definition: value.h:48
const Char * Location
Definition: reader.h:34
Represents a JSON value.
Definition: value.h:116
Unserialize a JSON document into a Value.
Definition: reader.h:31
Configuration passed to reader and writer.
Definition: features.h:19