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 
133 private:
134  enum TokenType {
135  tokenEndOfStream = 0,
136  tokenObjectBegin,
137  tokenObjectEnd,
138  tokenArrayBegin,
139  tokenArrayEnd,
140  tokenString,
141  tokenNumber,
142  tokenTrue,
143  tokenFalse,
144  tokenNull,
145  tokenArraySeparator,
146  tokenMemberSeparator,
147  tokenComment,
148  tokenError
149  };
150 
151  class Token {
152  public:
153  TokenType type_;
154  Location start_;
155  Location end_;
156  };
157 
158  class ErrorInfo {
159  public:
160  Token token_;
161  std::string message_;
162  Location extra_;
163  };
164 
165  typedef std::deque<ErrorInfo> Errors;
166 
167  bool expectToken(TokenType type, Token &token, const char *message);
168  bool readToken(Token &token);
169  void skipSpaces();
170  bool match(Location pattern, int patternLength);
171  bool readComment();
172  bool readCStyleComment();
173  bool readCppStyleComment();
174  bool readString();
175  void readNumber();
176  bool readValue();
177  bool readObject(Token &token);
178  bool readArray(Token &token);
179  bool decodeNumber(Token &token);
180  bool decodeNumber(Token &token, Value &decoded);
181  bool decodeString(Token &token);
182  bool decodeString(Token &token, std::string &decoded);
183  bool decodeDouble(Token &token);
184  bool decodeDouble(Token &token, Value &decoded);
185  bool decodeUnicodeCodePoint(Token &token,
186  Location &current,
187  Location end,
188  unsigned int &unicode);
189  bool decodeUnicodeEscapeSequence(Token &token,
190  Location &current,
191  Location end,
192  unsigned int &unicode);
193  bool addError(const std::string &message, Token &token, Location extra = 0);
194  bool recoverFromError(TokenType skipUntilToken);
195  bool addErrorAndRecover(const std::string &message,
196  Token &token,
197  TokenType skipUntilToken);
198  void skipUntilSpace();
199  Value &currentValue();
200  Char getNextChar();
201  void
202  getLocationLineAndColumn(Location location, int &line, int &column) const;
203  std::string getLocationLineAndColumn(Location location) const;
204  void addComment(Location begin, Location end, CommentPlacement placement);
205  void skipCommentTokens(Token &token);
206 
207  typedef std::stack<Value *> Nodes;
208  Nodes nodes_;
209  Errors errors_;
210  std::string document_;
211  Location begin_;
212  Location end_;
213  Location current_;
214  Location lastValueEnd_;
215  Value *lastValue_;
216  std::string commentsBefore_;
217  Features features_;
218  bool collectComments_;
219 };
220 
245 JSON_API std::istream &operator>>(std::istream &, Value &);
246 
247 } // namespace Json
248 
249 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
250 #pragma warning(pop)
251 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
252 
253 #endif // CPPTL_JSON_READER_H_INCLUDED
Json::Reader::StructuredError::message
std::string message
Definition: reader.h:45
features.h
Json::Reader
Unserialize a JSON document into a Value.
Definition: reader.h:31
Json::operator>>
std::istream & operator>>(std::istream &, Value &)
Read from 'sin' into 'root'.
Definition: json_reader.cpp:831
Json::Reader::StructuredError::offset_start
size_t offset_start
Definition: reader.h:43
Json::Value
Represents a JSON value.
Definition: value.h:116
Json::Reader::Char
char Char
Definition: reader.h:33
Json::Reader::StructuredError::offset_limit
size_t offset_limit
Definition: reader.h:44
Json::CommentPlacement
CommentPlacement
Definition: value.h:48
JSONCPP_DEPRECATED
#define JSONCPP_DEPRECATED(message)
Definition: config.h:87
Json
JSON (JavaScript Object Notation).
Definition: config.h:90
JSON_API
#define JSON_API
If defined, indicates that the source file is amalgated to prevent private header inclusion.
Definition: config.h:62
Json::Reader::StructuredError
An error tagged with where in the JSON text it was encountered.
Definition: reader.h:42
Json::Reader::Location
const typedef Char * Location
Definition: reader.h:34
value.h
Json::Features
Configuration passed to reader and writer.
Definition: features.h:19