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 
41  Reader();
42 
46  Reader(const Features& features);
47 
62  bool
63  parse(const std::string& document, Value& root, bool collectComments = true);
64 
83  bool parse(const char* beginDoc,
84  const char* endDoc,
85  Value& root,
86  bool collectComments = true);
87 
90  bool parse(std::istream& is, Value& root, bool collectComments = true);
91 
101  JSONCPP_DEPRECATED("Use getFormattedErrorMessages() instead.")
102  std::string getFormatedErrorMessages() const;
103 
112  std::string getFormattedErrorMessages() const;
113 
114 private:
115  enum TokenType {
116  tokenEndOfStream = 0,
117  tokenObjectBegin,
118  tokenObjectEnd,
119  tokenArrayBegin,
120  tokenArrayEnd,
121  tokenString,
122  tokenNumber,
123  tokenTrue,
124  tokenFalse,
125  tokenNull,
126  tokenArraySeparator,
127  tokenMemberSeparator,
128  tokenComment,
129  tokenError
130  };
131 
132  class Token {
133  public:
134  TokenType type_;
135  Location start_;
136  Location end_;
137  };
138 
139  class ErrorInfo {
140  public:
141  Token token_;
142  std::string message_;
143  Location extra_;
144  };
145 
146  typedef std::deque<ErrorInfo> Errors;
147 
148  bool readToken(Token& token);
149  void skipSpaces();
150  bool match(Location pattern, int patternLength);
151  bool readComment();
152  bool readCStyleComment();
153  bool readCppStyleComment();
154  bool readString();
155  void readNumber();
156  bool readValue();
157  bool readObject(Token& token);
158  bool readArray(Token& token);
159  bool decodeNumber(Token& token);
160  bool decodeNumber(Token& token, Value& decoded);
161  bool decodeString(Token& token);
162  bool decodeString(Token& token, std::string& decoded);
163  bool decodeDouble(Token& token);
164  bool decodeDouble(Token& token, Value& decoded);
165  bool decodeUnicodeCodePoint(Token& token,
166  Location& current,
167  Location end,
168  unsigned int& unicode);
169  bool decodeUnicodeEscapeSequence(Token& token,
170  Location& current,
171  Location end,
172  unsigned int& unicode);
173  bool addError(const std::string& message, Token& token, Location extra = 0);
174  bool recoverFromError(TokenType skipUntilToken);
175  bool addErrorAndRecover(const std::string& message,
176  Token& token,
177  TokenType skipUntilToken);
178  void skipUntilSpace();
179  Value& currentValue();
180  Char getNextChar();
181  void
182  getLocationLineAndColumn(Location location, int& line, int& column) const;
183  std::string getLocationLineAndColumn(Location location) const;
184  void addComment(Location begin, Location end, CommentPlacement placement);
185  void skipCommentTokens(Token& token);
186 
187  typedef std::stack<Value*> Nodes;
188  Nodes nodes_;
189  Errors errors_;
190  std::string document_;
191  Location begin_;
192  Location end_;
193  Location current_;
194  Location lastValueEnd_;
195  Value* lastValue_;
196  std::string commentsBefore_;
197  Features features_;
198  bool collectComments_;
199 }; // Reader
200 
204 public:
205  virtual ~CharReader() {}
223  virtual bool parse(
224  char const* beginDoc, char const* endDoc,
225  Value* root, std::string* errs) = 0;
226 
227  class Factory {
228  public:
229  virtual ~Factory() {}
233  virtual CharReader* newCharReader() const = 0;
234  }; // Factory
235 }; // CharReader
236 
250 public:
251  // Note: We use a Json::Value so that we can add data-members to this class
252  // without a major version bump.
287 
289  virtual ~CharReaderBuilder();
290 
291  virtual CharReader* newCharReader() const;
292 
296  bool validate(Json::Value* invalid) const;
297 
300  Value& operator[](std::string key);
301 
307  static void setDefaults(Json::Value* settings);
313  static void strictMode(Json::Value* settings);
314 };
315 
321  CharReader::Factory const&,
322  std::istream&,
323  Value* root, std::string* errs);
324 
349 JSON_API std::istream& operator>>(std::istream&, Value&);
350 
351 } // namespace Json
352 
353 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
354 #pragma warning(pop)
355 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
356 
357 #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:286
std::istream & operator>>(std::istream &, Value &)
Read from 'sin' into 'root'.
char Char
Definition: reader.h:35
STL namespace.
CommentPlacement
Definition: value.h:89
const Char * Location
Definition: reader.h:36
virtual ~CharReader()
Definition: reader.h:205
JSON (JavaScript Object Notation).
Definition: config.h:87
Interface for reading JSON from a char array.
Definition: reader.h:203
Represents a JSON value.
Definition: value.h:162
Unserialize a JSON document into a Value.
Definition: reader.h:33
Build a CharReader implementation.
Definition: reader.h:249
Configuration passed to reader and writer.
Definition: features.h:19