ADTF  3.18.2
workspace/conan/dev_essential/1.3.3/dw/stable/package/37682420cd166e229516a41c8d6a139a0b13e1e1/include/json/reader.h
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 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 #pragma pack(push, 8)
27 
28 namespace Json {
29 
35 class JSONCPP_DEPRECATED("Use CharReader and CharReaderBuilder instead") JSON_API Reader {
36 public:
37  typedef char Char;
38  typedef const Char* Location;
39 
46  struct StructuredError {
47  ptrdiff_t offset_start;
48  ptrdiff_t offset_limit;
49  JSONCPP_STRING message;
50  };
51 
55  Reader();
56 
60  Reader(const Features& features);
61 
76  bool
77  parse(const std::string& document, Value& root, bool collectComments = true);
78 
97  bool parse(const char* beginDoc,
98  const char* endDoc,
99  Value& root,
100  bool collectComments = true);
101 
104  bool parse(JSONCPP_ISTREAM& is, Value& root, bool collectComments = true);
105 
115  JSONCPP_DEPRECATED("Use getFormattedErrorMessages() instead.")
116  JSONCPP_STRING getFormatedErrorMessages() const;
117 
126  JSONCPP_STRING getFormattedErrorMessages() const;
127 
135  std::vector<StructuredError> getStructuredErrors() const;
136 
143  bool pushError(const Value& value, const JSONCPP_STRING& message);
144 
152  bool pushError(const Value& value, const JSONCPP_STRING& message, const Value& extra);
153 
158  bool good() const;
159 
160 private:
161  enum TokenType {
162  tokenEndOfStream = 0,
163  tokenObjectBegin,
164  tokenObjectEnd,
165  tokenArrayBegin,
166  tokenArrayEnd,
167  tokenString,
168  tokenNumber,
169  tokenTrue,
170  tokenFalse,
171  tokenNull,
172  tokenArraySeparator,
173  tokenMemberSeparator,
174  tokenComment,
175  tokenError
176  };
177 
178  class Token {
179  public:
180  TokenType type_;
181  Location start_;
182  Location end_;
183  };
184 
185  class ErrorInfo {
186  public:
187  Token token_;
188  JSONCPP_STRING message_;
189  Location extra_;
190  };
191 
192  typedef std::deque<ErrorInfo> Errors;
193 
194  bool readToken(Token& token);
195  void skipSpaces();
196  bool match(Location pattern, int patternLength);
197  bool readComment();
198  bool readCStyleComment();
199  bool readCppStyleComment();
200  bool readString();
201  void readNumber();
202  bool readValue();
203  bool readObject(Token& token);
204  bool readArray(Token& token);
205  bool decodeNumber(Token& token);
206  bool decodeNumber(Token& token, Value& decoded);
207  bool decodeString(Token& token);
208  bool decodeString(Token& token, JSONCPP_STRING& decoded);
209  bool decodeDouble(Token& token);
210  bool decodeDouble(Token& token, Value& decoded);
211  bool decodeUnicodeCodePoint(Token& token,
212  Location& current,
213  Location end,
214  unsigned int& unicode);
215  bool decodeUnicodeEscapeSequence(Token& token,
216  Location& current,
217  Location end,
218  unsigned int& unicode);
219  bool addError(const JSONCPP_STRING& message, Token& token, Location extra = 0);
220  bool recoverFromError(TokenType skipUntilToken);
221  bool addErrorAndRecover(const JSONCPP_STRING& message,
222  Token& token,
223  TokenType skipUntilToken);
224  void skipUntilSpace();
225  Value& currentValue();
226  Char getNextChar();
227  void
228  getLocationLineAndColumn(Location location, int& line, int& column) const;
229  JSONCPP_STRING getLocationLineAndColumn(Location location) const;
230  void addComment(Location begin, Location end, CommentPlacement placement);
231  void skipCommentTokens(Token& token);
232 
233  static bool containsNewLine(Location begin, Location end);
234  static JSONCPP_STRING normalizeEOL(Location begin, Location end);
235 
236  typedef std::stack<Value*> Nodes;
237  Nodes nodes_;
238  Errors errors_;
239  JSONCPP_STRING document_;
240  Location begin_;
241  Location end_;
242  Location current_;
243  Location lastValueEnd_;
244  Value* lastValue_;
245  JSONCPP_STRING commentsBefore_;
246  Features features_;
247  bool collectComments_;
248 }; // Reader
249 
252 class JSON_API CharReader {
253 public:
254  virtual ~CharReader() {}
272  virtual bool parse(
273  char const* beginDoc, char const* endDoc,
274  Value* root, JSONCPP_STRING* errs) = 0;
275 
276  class JSON_API Factory {
277  public:
278  virtual ~Factory() {}
282  virtual CharReader* newCharReader() const = 0;
283  }; // Factory
284 }; // CharReader
285 
298 class JSON_API CharReaderBuilder : public CharReader::Factory {
299 public:
300  // Note: We use a Json::Value so that we can add data-members to this class
301  // without a major version bump.
339 
341  ~CharReaderBuilder() JSONCPP_OVERRIDE;
342 
343  CharReader* newCharReader() const JSONCPP_OVERRIDE;
344 
348  bool validate(Json::Value* invalid) const;
349 
352  Value& operator[](JSONCPP_STRING key);
353 
359  static void setDefaults(Json::Value* settings);
365  static void strictMode(Json::Value* settings);
366 };
367 
372 bool JSON_API parseFromStream(
373  CharReader::Factory const&,
374  JSONCPP_ISTREAM&,
375  Value* root, std::string* errs);
376 
401 JSON_API JSONCPP_ISTREAM& operator>>(JSONCPP_ISTREAM&, Value&);
402 
403 } // namespace Json
404 
405 #pragma pack(pop)
406 
407 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
408 #pragma warning(pop)
409 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
410 
411 #endif // CPPTL_JSON_READER_H_INCLUDED
virtual CharReader * newCharReader() const =0
Allocate a CharReader via operator new().
virtual bool parse(char const *beginDoc, char const *endDoc, Value *root, JSONCPP_STRING *errs)=0
Read a Value from a JSON document.
Configuration passed to reader and writer.
Definition: features.h:21
Represents a JSON value.
Definition: value.h:177
JSON (JavaScript Object Notation).
Definition: allocator.h:14
class JSONCPP_DEPRECATED("Use CharReader and CharReaderBuilder instead") JSON_API Reader
Unserialize a JSON document into a Value.
CommentPlacement
Definition: value.h:104
bool JSON_API parseFromStream(CharReader::Factory const &, JSONCPP_ISTREAM &, Value *root, std::string *errs)
Consume entire stream and use its begin/end.