Twitch SDK (Internal)
reader.h
Go to the documentation of this file.
1 #ifndef CPPTL_JSON_READER_H_INCLUDED
2 # define CPPTL_JSON_READER_H_INCLUDED
3 
4 # include "features.h"
5 # include "value.h"
6 # include <deque>
7 # include <stack>
8 # include <string>
9 # include <iostream>
10 
11 namespace ttv {
12 
13  namespace json {
14 
19  {
20  public:
21  typedef char Char;
22  typedef const Char *Location;
23 
27  Reader();
28 
32  Reader( const Features &features );
33 
44  bool parse( const std::string &document,
45  Value &root,
46  bool collectComments = true );
47 
58  bool parse( const char *beginDoc, const char *endDoc,
59  Value &root,
60  bool collectComments = true );
61 
64  bool parse( std::istream &is,
65  Value &root,
66  bool collectComments = true );
67 
73  std::string getFormatedErrorMessages() const;
74 
75  private:
76  enum TokenType
77  {
78  tokenEndOfStream = 0,
91  tokenError
92  };
93 
94  class Token
95  {
96  public:
98  Location start_;
99  Location end_;
100  };
101 
102  class ErrorInfo
103  {
104  public:
106  std::string message_;
107  Location extra_;
108  };
109 
110  typedef std::deque<ErrorInfo> Errors;
111 
112  bool expectToken( TokenType type, Token &token, const char *message );
113  bool readToken( Token &token );
114  void skipSpaces();
115  bool match( Location pattern,
116  int patternLength );
117  bool readComment();
118  bool readCStyleComment();
119  bool readCppStyleComment();
120  bool readString();
121  void readNumber();
122  bool readValue();
123  bool readObject( Token &token );
124  bool readArray( Token &token );
125  bool decodeNumber( Token &token );
126  bool decodeString( Token &token );
127  bool decodeString( Token &token, std::string &decoded );
128  bool decodeDouble( Token &token );
129  bool decodeUnicodeCodePoint( Token &token,
130  Location &current,
131  Location end,
132  unsigned int &unicode );
133  bool decodeUnicodeEscapeSequence( Token &token,
134  Location &current,
135  Location end,
136  unsigned int &unicode );
137  bool addError( const std::string &message,
138  Token &token,
139  Location extra = 0 );
140  bool recoverFromError( TokenType skipUntilToken );
141  bool addErrorAndRecover( const std::string &message,
142  Token &token,
143  TokenType skipUntilToken );
144  void skipUntilSpace();
145  Value &currentValue();
146  Char getNextChar();
147  void getLocationLineAndColumn( Location location,
148  int &line,
149  int &column ) const;
150  std::string getLocationLineAndColumn( Location location ) const;
151  void addComment( Location begin,
152  Location end,
153  CommentPlacement placement );
154  void skipCommentTokens( Token &token );
155 
156  typedef std::stack<Value *> Nodes;
157  Nodes nodes_;
158  Errors errors_;
159  std::string document_;
160  Location begin_;
161  Location end_;
162  Location current_;
163  Location lastValueEnd_;
165  std::string commentsBefore_;
168  };
169 
194  std::istream& operator>>( std::istream&, Value& );
195 
196  } // namespace json
197 
198 } // namespace ttv
199 
200 #endif // CPPTL_JSON_READER_H_INCLUDED
Location extra_
Definition: reader.h:107
const Char * Location
Definition: reader.h:22
#define JSON_API
Definition: config.h:40
Location start_
Definition: reader.h:98
Definition: reader.h:84
TokenType type_
Definition: reader.h:97
Definition: reader.h:86
char Char
Definition: reader.h:21
std::stack< Value * > Nodes
Definition: reader.h:156
Definition: reader.h:85
Location end_
Definition: reader.h:161
Location end_
Definition: reader.h:99
Configuration passed to reader and writer. This configuration object can be used to force the Reader ...
Definition: features.h:14
Definition: reader.h:83
JSON (JavaScript Object Notation).
Definition: adsapi.h:16
Definition: reader.h:82
Location current_
Definition: reader.h:162
Nodes nodes_
Definition: reader.h:157
Represents a JSON value.
Definition: value.h:114
Unserialize a JSON document into a Value.
Definition: reader.h:18
std::string document_
Definition: reader.h:159
TokenType
Definition: reader.h:76
std::string message_
Definition: reader.h:106
bool collectComments_
Definition: reader.h:167
Token token_
Definition: reader.h:105
Definition: reader.h:94
Errors errors_
Definition: reader.h:158
std::deque< ErrorInfo > Errors
Definition: reader.h:110
Definition: reader.h:81
Definition: reader.h:80
std::string commentsBefore_
Definition: reader.h:165
Definition: reader.h:87
Location begin_
Definition: reader.h:160
Definition: reader.h:90
Features features_
Definition: reader.h:166
Definition: reader.h:102
std::istream & operator>>(std::istream &, Value &)
Read from &#39;sin&#39; into &#39;root&#39;.
CommentPlacement
Definition: value.h:38
Location lastValueEnd_
Definition: reader.h:163
Value * lastValue_
Definition: reader.h:164