JsonCpp project page Classes Namespace JsonCpp home page

reader.h
Go to the documentation of this file.
1 // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
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 JSON_READER_H_INCLUDED
7 #define JSON_READER_H_INCLUDED
8 
9 #if !defined(JSON_IS_AMALGAMATION)
10 #include "json_features.h"
11 #include "value.h"
12 #endif // if !defined(JSON_IS_AMALGAMATION)
13 #include <deque>
14 #include <iosfwd>
15 #include <istream>
16 #include <stack>
17 #include <string>
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 #pragma pack(push)
27 #pragma pack()
28 
29 namespace Json {
30 
38 public:
39  using Char = char;
40  using Location = const Char*;
41 
47  struct StructuredError {
48  ptrdiff_t offset_start;
49  ptrdiff_t offset_limit;
51  };
52 
56  Reader();
57 
61  Reader(const Features& features);
62 
77  bool parse(const std::string& document, Value& root,
78  bool collectComments = true);
79 
99  bool parse(const char* beginDoc, const char* endDoc, Value& root,
100  bool collectComments = true);
101 
104  bool parse(IStream& is, Value& root, bool collectComments = true);
105 
114  JSONCPP_DEPRECATED("Use getFormattedErrorMessages() instead.")
115  String getFormatedErrorMessages() const;
116 
124  String getFormattedErrorMessages() const;
125 
133  std::vector<StructuredError> getStructuredErrors() const;
134 
142  bool pushError(const Value& value, const String& message);
143 
152  bool pushError(const Value& value, const String& message, const Value& extra);
153 
159  bool good() const;
160 
161 private:
162  enum TokenType {
163  tokenEndOfStream = 0,
164  tokenObjectBegin,
165  tokenObjectEnd,
166  tokenArrayBegin,
167  tokenArrayEnd,
168  tokenString,
169  tokenNumber,
170  tokenTrue,
171  tokenFalse,
172  tokenNull,
173  tokenArraySeparator,
174  tokenMemberSeparator,
175  tokenComment,
176  tokenError
177  };
178 
179  class Token {
180  public:
181  TokenType type_;
182  Location start_;
183  Location end_;
184  };
185 
186  class ErrorInfo {
187  public:
188  Token token_;
189  String message_;
190  Location extra_;
191  };
192 
193  using Errors = std::deque<ErrorInfo>;
194 
195  bool readToken(Token& token);
196  bool readTokenSkippingComments(Token& token);
197  void skipSpaces();
198  bool match(const Char* pattern, int patternLength);
199  bool readComment();
200  bool readCStyleComment();
201  bool readCppStyleComment();
202  bool readString();
203  void readNumber();
204  bool readValue();
205  bool readObject(Token& token);
206  bool readArray(Token& token);
207  bool decodeNumber(Token& token);
208  bool decodeNumber(Token& token, Value& decoded);
209  bool decodeString(Token& token);
210  bool decodeString(Token& token, String& decoded);
211  bool decodeDouble(Token& token);
212  bool decodeDouble(Token& token, Value& decoded);
213  bool decodeUnicodeCodePoint(Token& token, Location& current, Location end,
214  unsigned int& unicode);
215  bool decodeUnicodeEscapeSequence(Token& token, Location& current,
216  Location end, unsigned int& unicode);
217  bool addError(const String& message, Token& token, Location extra = nullptr);
218  bool recoverFromError(TokenType skipUntilToken);
219  bool addErrorAndRecover(const String& message, Token& token,
220  TokenType skipUntilToken);
221  void skipUntilSpace();
222  Value& currentValue();
223  Char getNextChar();
224  void getLocationLineAndColumn(Location location, int& line,
225  int& column) const;
226  String getLocationLineAndColumn(Location location) const;
227  void addComment(Location begin, Location end, CommentPlacement placement);
228 
229  static bool containsNewLine(Location begin, Location end);
230  static String normalizeEOL(Location begin, Location end);
231 
232  using Nodes = std::stack<Value*>;
233  Nodes nodes_;
234  Errors errors_;
235  String document_;
236  Location begin_{};
237  Location end_{};
238  Location current_{};
239  Location lastValueEnd_{};
240  Value* lastValue_{};
241  String commentsBefore_;
242  Features features_;
243  bool collectComments_{};
244 }; // Reader
245 
249 public:
251  ptrdiff_t offset_start;
252  ptrdiff_t offset_limit;
254  };
255 
256  virtual ~CharReader() = default;
273  virtual bool parse(char const* beginDoc, char const* endDoc, Value* root,
274  String* errs);
275 
279  std::vector<StructuredError> getStructuredErrors() const;
280 
282  public:
283  virtual ~Factory() = default;
287  virtual CharReader* newCharReader() const = 0;
288  }; // Factory
289 
290 protected:
291  class Impl {
292  public:
293  virtual ~Impl() = default;
294  virtual bool parse(char const* beginDoc, char const* endDoc, Value* root,
295  String* errs) = 0;
296  virtual std::vector<StructuredError> getStructuredErrors() const = 0;
297  };
298 
299  explicit CharReader(std::unique_ptr<Impl> impl) : _impl(std::move(impl)) {}
300 
301 private:
302  std::unique_ptr<Impl> _impl;
303 }; // CharReader
304 
318 public:
319  // Note: We use a Json::Value so that we can add data-members to this class
320  // without a major version bump.
364 
366  ~CharReaderBuilder() override;
367 
368  CharReader* newCharReader() const override;
369 
373  bool validate(Json::Value* invalid) const;
374 
377  Value& operator[](const String& key);
378 
384  static void setDefaults(Json::Value* settings);
390  static void strictMode(Json::Value* settings);
396  static void ecma404Mode(Json::Value* settings);
397 };
398 
404  String* errs);
405 
431 
432 } // namespace Json
433 
434 #pragma pack(pop)
435 
436 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
437 #pragma warning(pop)
438 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
439 
440 #endif // JSON_READER_H_INCLUDED
#define JSONCPP_DEPRECATED(message)
Definition: config.h:89
#define JSON_API
If defined, indicates that the source file is amalgamated to prevent private header inclusion...
Definition: config.h:50
Json::Value settings_
Configuration of this builder.
Definition: reader.h:363
CharReader(std::unique_ptr< Impl > impl)
Definition: reader.h:299
STL namespace.
char Char
Definition: reader.h:39
std::basic_string< char, std::char_traits< char >, Allocator< char >> String
Definition: config.h:135
An error tagged with where in the JSON text it was encountered.
Definition: reader.h:47
IStream & operator>>(IStream &, Value &)
Read from 'sin' into 'root'.
CommentPlacement
Definition: value.h:132
JSON (JavaScript Object Notation).
Definition: allocator.h:16
Interface for reading JSON from a char array.
Definition: reader.h:248
Represents a JSON value.
Definition: value.h:207
Unserialize a JSON document into a Value.
Definition: reader.h:37
std::istream IStream
Definition: config.h:142
bool parseFromStream(CharReader::Factory const &, IStream &, Value *root, String *errs)
Consume entire stream and use its begin/end.
Build a CharReader implementation.
Definition: reader.h:317
Configuration passed to reader and writer.
Definition: json_features.h:22
const Char * Location
Definition: reader.h:40