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 #include <istream>
18 
19 // Disable warning C4251: <data member>: <type> needs to have dll-interface to
20 // be used by...
21 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
22 #pragma warning(push)
23 #pragma warning(disable : 4251)
24 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
25 
26 namespace Json {
27 
34 public:
35  typedef char Char;
36  typedef const Char* Location;
37 
44  struct StructuredError {
45  ptrdiff_t offset_start;
46  ptrdiff_t offset_limit;
48  };
49 
53  Reader();
54 
58  Reader(const Features& features);
59 
74  bool
75  parse(const std::string& document, Value& root, bool collectComments = true);
76 
95  bool parse(const char* beginDoc,
96  const char* endDoc,
97  Value& root,
98  bool collectComments = true);
99 
102  bool parse(JSONCPP_ISTREAM& is, Value& root, bool collectComments = true);
103 
113  JSONCPP_DEPRECATED("Use getFormattedErrorMessages() instead.")
114  JSONCPP_STRING getFormatedErrorMessages() const;
115 
124  JSONCPP_STRING getFormattedErrorMessages() const;
125 
133  std::vector<StructuredError> getStructuredErrors() const;
134 
141  bool pushError(const Value& value, const JSONCPP_STRING& message);
142 
150  bool pushError(const Value& value, const JSONCPP_STRING& message, const Value& extra);
151 
156  bool good() const;
157 
158 private:
159  enum TokenType {
160  tokenEndOfStream = 0,
161  tokenObjectBegin,
162  tokenObjectEnd,
163  tokenArrayBegin,
164  tokenArrayEnd,
165  tokenString,
166  tokenNumber,
167  tokenTrue,
168  tokenFalse,
169  tokenNull,
170  tokenArraySeparator,
171  tokenMemberSeparator,
172  tokenComment,
173  tokenError
174  };
175 
176  class Token {
177  public:
178  TokenType type_;
179  Location start_;
180  Location end_;
181  };
182 
183  class ErrorInfo {
184  public:
185  Token token_;
186  JSONCPP_STRING message_;
187  Location extra_;
188  };
189 
190  typedef std::deque<ErrorInfo> Errors;
191 
192  bool readToken(Token& token);
193  void skipSpaces();
194  bool match(Location pattern, int patternLength);
195  bool readComment();
196  bool readCStyleComment();
197  bool readCppStyleComment();
198  bool readString();
199  void readNumber();
200  bool readValue();
201  bool readObject(Token& token);
202  bool readArray(Token& token);
203  bool decodeNumber(Token& token);
204  bool decodeNumber(Token& token, Value& decoded);
205  bool decodeString(Token& token);
206  bool decodeString(Token& token, JSONCPP_STRING& decoded);
207  bool decodeDouble(Token& token);
208  bool decodeDouble(Token& token, Value& decoded);
209  bool decodeUnicodeCodePoint(Token& token,
210  Location& current,
211  Location end,
212  unsigned int& unicode);
213  bool decodeUnicodeEscapeSequence(Token& token,
214  Location& current,
215  Location end,
216  unsigned int& unicode);
217  bool addError(const JSONCPP_STRING& message, Token& token, Location extra = 0);
218  bool recoverFromError(TokenType skipUntilToken);
219  bool addErrorAndRecover(const JSONCPP_STRING& message,
220  Token& token,
221  TokenType skipUntilToken);
222  void skipUntilSpace();
223  Value& currentValue();
224  Char getNextChar();
225  void
226  getLocationLineAndColumn(Location location, int& line, int& column) const;
227  JSONCPP_STRING getLocationLineAndColumn(Location location) const;
228  void addComment(Location begin, Location end, CommentPlacement placement);
229  void skipCommentTokens(Token& token);
230 
231  typedef std::stack<Value*> Nodes;
232  Nodes nodes_;
233  Errors errors_;
234  JSONCPP_STRING document_;
235  Location begin_;
236  Location end_;
237  Location current_;
238  Location lastValueEnd_;
239  Value* lastValue_;
240  JSONCPP_STRING commentsBefore_;
241  Features features_;
242  bool collectComments_;
243 }; // Reader
244 
248 public:
249  virtual ~CharReader() {}
267  virtual bool parse(
268  char const* beginDoc, char const* endDoc,
269  Value* root, JSONCPP_STRING* errs) = 0;
270 
272  public:
273  virtual ~Factory() {}
277  virtual CharReader* newCharReader() const = 0;
278  }; // Factory
279 }; // CharReader
280 
294 public:
295  // Note: We use a Json::Value so that we can add data-members to this class
296  // without a major version bump.
334 
337 
338  CharReader* newCharReader() const JSONCPP_OVERRIDE;
339 
343  bool validate(Json::Value* invalid) const;
344 
347  Value& operator[](JSONCPP_STRING key);
348 
354  static void setDefaults(Json::Value* settings);
360  static void strictMode(Json::Value* settings);
361 };
362 
368  CharReader::Factory const&,
370  Value* root, std::string* errs);
371 
396 JSON_API JSONCPP_ISTREAM& operator>>(JSONCPP_ISTREAM&, Value&);
397 
398 } // namespace Json
399 
400 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
401 #pragma warning(pop)
402 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
403 
404 #endif // CPPTL_JSON_READER_H_INCLUDED
#define JSONCPP_OVERRIDE
Definition: config.h:89
#define JSONCPP_DEPRECATED(message)
Definition: config.h:126
#define JSONCPP_ISTREAM
Definition: config.h:174
#define JSON_API
If defined, indicates that the source file is amalgated to prevent private header inclusion...
Definition: config.h:54
bool parseFromStream(CharReader::Factory const &, std::istream &, Value *root, std::string *errs)
Consume entire stream and use its begin/end.
Json::Value settings_
Configuration of this builder.
Definition: reader.h:333
#define JSONCPP_STRING
Definition: config.h:170
char Char
Definition: reader.h:35
STL namespace.
An error tagged with where in the JSON text it was encountered.
Definition: reader.h:44
CommentPlacement
Definition: value.h:102
const Char * Location
Definition: reader.h:36
virtual ~CharReader()
Definition: reader.h:249
JSON (JavaScript Object Notation).
Definition: allocator.h:12
Interface for reading JSON from a char array.
Definition: reader.h:247
Represents a JSON value.
Definition: value.h:175
Unserialize a JSON document into a Value.
Definition: reader.h:33
Build a CharReader implementation.
Definition: reader.h:293
Configuration passed to reader and writer.
Definition: features.h:19