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 <stack>
15 # include <string>
16 # include <iostream>
17 
18 namespace Json {
19 
24  {
25  public:
26  typedef char Char;
27  typedef const Char *Location;
28 
32  Reader();
33 
37  Reader( const Features &features );
38 
49  bool parse( const std::string &document,
50  Value &root,
51  bool collectComments = true );
52 
65  bool parse( const char *beginDoc, const char *endDoc,
66  Value &root,
67  bool collectComments = true );
68 
71  bool parse( std::istream &is,
72  Value &root,
73  bool collectComments = true );
74 
81  JSONCPP_DEPRECATED("Use getFormattedErrorMessages instead")
82  std::string getFormatedErrorMessages() const;
83 
89  std::string getFormattedErrorMessages() const;
90 
91  private:
92  enum TokenType
93  {
94  tokenEndOfStream = 0,
95  tokenObjectBegin,
96  tokenObjectEnd,
97  tokenArrayBegin,
98  tokenArrayEnd,
99  tokenString,
100  tokenNumber,
101  tokenTrue,
102  tokenFalse,
103  tokenNull,
104  tokenArraySeparator,
105  tokenMemberSeparator,
106  tokenComment,
107  tokenError
108  };
109 
110  class Token
111  {
112  public:
113  TokenType type_;
114  Location start_;
115  Location end_;
116  };
117 
118  class ErrorInfo
119  {
120  public:
121  Token token_;
122  std::string message_;
123  Location extra_;
124  };
125 
126  typedef std::deque<ErrorInfo> Errors;
127 
128  bool expectToken( TokenType type, Token &token, const char *message );
129  bool readToken( Token &token );
130  void skipSpaces();
131  bool match( Location pattern,
132  int patternLength );
133  bool readComment();
134  bool readCStyleComment();
135  bool readCppStyleComment();
136  bool readString();
137  void readNumber();
138  bool readValue();
139  bool readObject( Token &token );
140  bool readArray( Token &token );
141  bool decodeNumber( Token &token );
142  bool decodeString( Token &token );
143  bool decodeString( Token &token, std::string &decoded );
144  bool decodeDouble( Token &token );
145  bool decodeUnicodeCodePoint( Token &token,
146  Location &current,
147  Location end,
148  unsigned int &unicode );
149  bool decodeUnicodeEscapeSequence( Token &token,
150  Location &current,
151  Location end,
152  unsigned int &unicode );
153  bool addError( const std::string &message,
154  Token &token,
155  Location extra = 0 );
156  bool recoverFromError( TokenType skipUntilToken );
157  bool addErrorAndRecover( const std::string &message,
158  Token &token,
159  TokenType skipUntilToken );
160  void skipUntilSpace();
161  Value &currentValue();
162  Char getNextChar();
163  void getLocationLineAndColumn( Location location,
164  int &line,
165  int &column ) const;
166  std::string getLocationLineAndColumn( Location location ) const;
167  void addComment( Location begin,
168  Location end,
169  CommentPlacement placement );
170  void skipCommentTokens( Token &token );
171 
172  typedef std::stack<Value *> Nodes;
173  Nodes nodes_;
174  Errors errors_;
175  std::string document_;
176  Location begin_;
177  Location end_;
178  Location current_;
179  Location lastValueEnd_;
180  Value *lastValue_;
181  std::string commentsBefore_;
182  Features features_;
183  bool collectComments_;
184  };
185 
210  std::istream& operator>>( std::istream&, Value& );
211 
212 } // namespace Json
213 
214 #endif // CPPTL_JSON_READER_H_INCLUDED
#define JSONCPP_DEPRECATED(message)
Definition: config.h:70
#define JSON_API
If defined, indicates that the source file is amalgated to prevent private header inclusion...
Definition: config.h:51
std::istream & operator>>(std::istream &, Value &)
Read from 'sin' into 'root'.
char Char
Definition: reader.h:26
STL namespace.
CommentPlacement
Definition: value.h:42
const Char * Location
Definition: reader.h:27
JSON (JavaScript Object Notation).
Definition: config.h:73
Represents a JSON value.
Definition: value.h:118
Unserialize a JSON document into a Value.
Definition: reader.h:23
Configuration passed to reader and writer.
Definition: features.h:19

SourceForge Logo hosts this site. Send comments to:
Json-cpp Developers