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, 8)
27 
28 namespace Json {
29 
37 public:
38  using Char = char;
39  using Location = const Char*;
40 
46  struct StructuredError {
47  ptrdiff_t offset_start;
48  ptrdiff_t offset_limit;
50  };
51 
55  Reader();
56 
60  Reader(const Features& features);
61 
76  bool parse(const std::string& document, Value& root,
77  bool collectComments = true);
78 
95  bool parse(const char* beginDoc, const char* endDoc, Value& root,
96  bool collectComments = true);
97 
100  bool parse(IStream& is, Value& root, bool collectComments = true);
101 
110  JSONCPP_DEPRECATED("Use getFormattedErrorMessages() instead.")
111  String getFormatedErrorMessages() const;
112 
120  String getFormattedErrorMessages() const;
121 
129  std::vector<StructuredError> getStructuredErrors() const;
130 
138  bool pushError(const Value& value, const String& message);
139 
148  bool pushError(const Value& value, const String& message, const Value& extra);
149 
155  bool good() const;
156 
157 private:
158  enum TokenType {
159  tokenEndOfStream = 0,
160  tokenObjectBegin,
161  tokenObjectEnd,
162  tokenArrayBegin,
163  tokenArrayEnd,
164  tokenString,
165  tokenNumber,
166  tokenTrue,
167  tokenFalse,
168  tokenNull,
169  tokenArraySeparator,
170  tokenMemberSeparator,
171  tokenComment,
172  tokenError
173  };
174 
175  class Token {
176  public:
177  TokenType type_;
178  Location start_;
179  Location end_;
180  };
181 
182  class ErrorInfo {
183  public:
184  Token token_;
185  String message_;
186  Location extra_;
187  };
188 
189  using Errors = std::deque<ErrorInfo>;
190 
191  bool readToken(Token& token);
192  void skipSpaces();
193  bool match(const Char* pattern, int patternLength);
194  bool readComment();
195  bool readCStyleComment();
196  bool readCppStyleComment();
197  bool readString();
198  void readNumber();
199  bool readValue();
200  bool readObject(Token& token);
201  bool readArray(Token& token);
202  bool decodeNumber(Token& token);
203  bool decodeNumber(Token& token, Value& decoded);
204  bool decodeString(Token& token);
205  bool decodeString(Token& token, String& decoded);
206  bool decodeDouble(Token& token);
207  bool decodeDouble(Token& token, Value& decoded);
208  bool decodeUnicodeCodePoint(Token& token, Location& current, Location end,
209  unsigned int& unicode);
210  bool decodeUnicodeEscapeSequence(Token& token, Location& current,
211  Location end, unsigned int& unicode);
212  bool addError(const String& message, Token& token, Location extra = nullptr);
213  bool recoverFromError(TokenType skipUntilToken);
214  bool addErrorAndRecover(const String& message, Token& token,
215  TokenType skipUntilToken);
216  void skipUntilSpace();
217  Value& currentValue();
218  Char getNextChar();
219  void getLocationLineAndColumn(Location location, int& line,
220  int& column) const;
221  String getLocationLineAndColumn(Location location) const;
222  void addComment(Location begin, Location end, CommentPlacement placement);
223  void skipCommentTokens(Token& token);
224 
225  static bool containsNewLine(Location begin, Location end);
226  static String normalizeEOL(Location begin, Location end);
227 
228  using Nodes = std::stack<Value*>;
229  Nodes nodes_;
230  Errors errors_;
231  String document_;
232  Location begin_{};
233  Location end_{};
234  Location current_{};
235  Location lastValueEnd_{};
236  Value* lastValue_{};
237  String commentsBefore_;
238  Features features_;
239  bool collectComments_{};
240 }; // Reader
241 
245 public:
246  virtual ~CharReader() = default;
263  virtual bool parse(char const* beginDoc, char const* endDoc, Value* root,
264  String* errs) = 0;
265 
267  public:
268  virtual ~Factory() = default;
272  virtual CharReader* newCharReader() const = 0;
273  }; // Factory
274 }; // CharReader
275 
289 public:
290  // Note: We use a Json::Value so that we can add data-members to this class
291  // without a major version bump.
335 
337  ~CharReaderBuilder() override;
338 
339  CharReader* newCharReader() const override;
340 
344  bool validate(Json::Value* invalid) const;
345 
348  Value& operator[](const String& key);
349 
355  static void setDefaults(Json::Value* settings);
361  static void strictMode(Json::Value* settings);
362 };
363 
369  String* errs);
370 
396 
397 } // namespace Json
398 
399 #pragma pack(pop)
400 
401 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
402 #pragma warning(pop)
403 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
404 
405 #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:334
STL namespace.
char Char
Definition: reader.h:38
An error tagged with where in the JSON text it was encountered.
Definition: reader.h:46
IStream & operator>>(IStream &, Value &)
Read from &#39;sin&#39; into &#39;root&#39;.
CommentPlacement
Definition: value.h:118
JSON (JavaScript Object Notation).
Definition: allocator.h:14
Interface for reading JSON from a char array.
Definition: reader.h:244
Represents a JSON value.
Definition: value.h:193
Unserialize a JSON document into a Value.
Definition: reader.h:36
std::istream IStream
Definition: config.h:139
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:288
Configuration passed to reader and writer.
Definition: json_features.h:21
std::basic_string< char, std::char_traits< char >, Allocator< char > > String
Definition: config.h:132
const Char * Location
Definition: reader.h:39