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  size_t offset_start;
46  size_t offset_limit;
47  std::string message;
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(std::istream& is, Value& root, bool collectComments = true);
103 
113  JSONCPP_DEPRECATED("Use getFormattedErrorMessages() instead.")
114  std::string getFormatedErrorMessages() const;
115 
124  std::string getFormattedErrorMessages() const;
125 
133  std::vector<StructuredError> getStructuredErrors() const;
134 
141  bool pushError(const Value& value, const std::string& message);
142 
150  bool pushError(const Value& value, const std::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  std::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, std::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 std::string& message, Token& token, Location extra = 0);
218  bool recoverFromError(TokenType skipUntilToken);
219  bool addErrorAndRecover(const std::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  std::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  std::string document_;
235  Location begin_;
236  Location end_;
237  Location current_;
238  Location lastValueEnd_;
239  Value* lastValue_;
240  std::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, std::string* errs) = 0;
270 
271  class Factory {
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.
331 
333  virtual ~CharReaderBuilder();
334 
335  virtual CharReader* newCharReader() const;
336 
340  bool validate(Json::Value* invalid) const;
341 
344  Value& operator[](std::string key);
345 
351  static void setDefaults(Json::Value* settings);
357  static void strictMode(Json::Value* settings);
358 };
359 
365  CharReader::Factory const&,
366  std::istream&,
367  Value* root, std::string* errs);
368 
393 JSON_API std::istream& operator>>(std::istream&, Value&);
394 
395 } // namespace Json
396 
397 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
398 #pragma warning(pop)
399 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
400 
401 #endif // CPPTL_JSON_READER_H_INCLUDED
#define JSONCPP_DEPRECATED(message)
Definition: config.h:84
#define JSON_API
If defined, indicates that the source file is amalgated to prevent private header inclusion...
Definition: config.h:51
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:330
std::istream & operator>>(std::istream &, Value &)
Read from 'sin' into 'root'.
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:74
const Char * Location
Definition: reader.h:36
virtual ~CharReader()
Definition: reader.h:249
JSON (JavaScript Object Notation).
Definition: config.h:87
Interface for reading JSON from a char array.
Definition: reader.h:247
Represents a JSON value.
Definition: value.h:147
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