19
19
//
20
20
// After calling EnterObject, you retrieve keys via NextObjectKey() and values via
21
21
// the normal getters. When NextObjectKey() returns null, you have exited the
22
- // object, or you can call ExitObject () to skip to the end of the object
22
+ // object, or you can call SkipObject () to skip to the end of the object
23
23
// immediately. If you fetch the entire object (i.e. NextObjectKey() returned null),
24
- // you should not call ExitObject ().
24
+ // you should not call SkipObject ().
25
25
//
26
26
// After calling EnterArray(), you must alternate between calling NextArrayValue()
27
27
// to see if the array has more data, and then retrieving values via the normal
28
- // getters. You can call ExitArray () to skip to the end of the array immediately.
28
+ // getters. You can call SkipArray () to skip to the end of the array immediately.
29
29
// If you fetch the entire array (i.e. NextArrayValue() returned null),
30
- // you should not call ExitArray ().
30
+ // you should not call SkipArray ().
31
31
//
32
32
// This parser uses in-situ strings, so the JSON buffer will be altered during the
33
33
// parse.
@@ -37,15 +37,15 @@ using namespace rapidjson;
37
37
38
38
class LookaheadParserHandler {
39
39
public:
40
- bool Null () { st_ = kHasValue ; v_.SetNull (); return true ; }
41
- bool Bool (bool b) { st_ = kHasValue ; v_.SetBool (b); return true ; }
42
- bool Int (int i) { st_ = kHasValue ; v_.SetInt (i); return true ; }
43
- bool Uint (unsigned u) { st_ = kHasValue ; v_.SetUint (u); return true ; }
44
- bool Int64 (int64_t i) { st_ = kHasValue ; v_.SetInt64 (i); return true ; }
45
- bool Uint64 (uint64_t u) { st_ = kHasValue ; v_.SetUint64 (u); return true ; }
46
- bool Double (double d) { st_ = kHasValue ; v_.SetDouble (d); return true ; }
40
+ bool Null () { st_ = kHasNull ; v_.SetNull (); return true ; }
41
+ bool Bool (bool b) { st_ = kHasBool ; v_.SetBool (b); return true ; }
42
+ bool Int (int i) { st_ = kHasNumber ; v_.SetInt (i); return true ; }
43
+ bool Uint (unsigned u) { st_ = kHasNumber ; v_.SetUint (u); return true ; }
44
+ bool Int64 (int64_t i) { st_ = kHasNumber ; v_.SetInt64 (i); return true ; }
45
+ bool Uint64 (uint64_t u) { st_ = kHasNumber ; v_.SetUint64 (u); return true ; }
46
+ bool Double (double d) { st_ = kHasNumber ; v_.SetDouble (d); return true ; }
47
47
bool RawNumber (const char *, SizeType, bool ) { return false ; }
48
- bool String (const char * str, SizeType length, bool ) { st_ = kHasValue ; v_.SetString (str, length); return true ; }
48
+ bool String (const char * str, SizeType length, bool ) { st_ = kHasString ; v_.SetString (str, length); return true ; }
49
49
bool StartObject () { st_ = kEnteringObject ; return true ; }
50
50
bool Key (const char * str, SizeType length, bool ) { st_ = kHasKey ; v_.SetString (str, length); return true ; }
51
51
bool EndObject (SizeType) { st_ = kExitingObject ; return true ; }
@@ -58,8 +58,12 @@ class LookaheadParserHandler {
58
58
59
59
protected:
60
60
enum LookaheadParsingState {
61
+ kInit ,
61
62
kError ,
62
- kHasValue ,
63
+ kHasNull ,
64
+ kHasBool ,
65
+ kHasNumber ,
66
+ kHasString ,
63
67
kHasKey ,
64
68
kEnteringObject ,
65
69
kExitingObject ,
@@ -75,7 +79,7 @@ class LookaheadParserHandler {
75
79
static const int parseFlags = kParseDefaultFlags | kParseInsituFlag ;
76
80
};
77
81
78
- LookaheadParserHandler::LookaheadParserHandler (char * str) : ss_(str) {
82
+ LookaheadParserHandler::LookaheadParserHandler (char * str) : v_(), st_( kInit ), r_(), ss_(str) {
79
83
r_.IterativeParseInit ();
80
84
ParseNext ();
81
85
}
@@ -93,10 +97,8 @@ class LookaheadParser : protected LookaheadParserHandler {
93
97
public:
94
98
LookaheadParser (char * str) : LookaheadParserHandler(str) {}
95
99
96
- void EnterObject ();
97
- void EnterArray ();
98
- void ExitObject ();
99
- void ExitArray ();
100
+ bool EnterObject ();
101
+ bool EnterArray ();
100
102
const char * NextObjectKey ();
101
103
bool NextArrayValue ();
102
104
int GetInt ();
@@ -105,57 +107,52 @@ class LookaheadParser : protected LookaheadParserHandler {
105
107
bool GetBool ();
106
108
void GetNull ();
107
109
110
+ void SkipObject ();
111
+ void SkipArray ();
108
112
void SkipValue ();
109
113
Value* PeekValue ();
110
114
int PeekType (); // returns a rapidjson::Type, or -1 for no value (at end of object/array)
111
115
112
116
bool IsValid () { return st_ != kError ; }
117
+
118
+ protected:
119
+ void SkipOut (int depth);
113
120
};
114
121
115
- void LookaheadParser::EnterObject () {
122
+ bool LookaheadParser::EnterObject () {
116
123
if (st_ != kEnteringObject ) {
117
124
st_ = kError ;
118
- return ;
125
+ return false ;
119
126
}
120
127
121
128
ParseNext ();
129
+ return true ;
122
130
}
123
131
124
- void LookaheadParser::EnterArray () {
132
+ bool LookaheadParser::EnterArray () {
125
133
if (st_ != kEnteringArray ) {
126
134
st_ = kError ;
127
- return ;
135
+ return false ;
128
136
}
129
137
130
138
ParseNext ();
131
- }
132
-
133
- void LookaheadParser::ExitObject () {
134
- while (NextObjectKey ()) {
135
- SkipValue ();
136
- }
137
- }
138
-
139
- void LookaheadParser::ExitArray () {
140
- while (NextArrayValue ()) {
141
- SkipValue ();
142
- }
139
+ return true ;
143
140
}
144
141
145
142
const char * LookaheadParser::NextObjectKey () {
146
- if (st_ == kExitingObject ) {
143
+ if (st_ == kHasKey ) {
144
+ const char * result = v_.GetString ();
147
145
ParseNext ();
148
- return 0 ;
146
+ return result ;
149
147
}
150
-
151
- if (st_ != kHasKey || !v_. IsString () ) {
148
+
149
+ if (st_ != kExitingObject ) {
152
150
st_ = kError ;
153
151
return 0 ;
154
152
}
155
153
156
- const char * result = v_.GetString ();
157
154
ParseNext ();
158
- return result ;
155
+ return 0 ;
159
156
}
160
157
161
158
bool LookaheadParser::NextArrayValue () {
@@ -164,11 +161,16 @@ bool LookaheadParser::NextArrayValue() {
164
161
return false ;
165
162
}
166
163
164
+ if (st_ == kError || st_ == kExitingObject || st_ == kHasKey ) {
165
+ st_ = kError ;
166
+ return false ;
167
+ }
168
+
167
169
return true ;
168
170
}
169
171
170
172
int LookaheadParser::GetInt () {
171
- if (st_ != kHasValue || !v_.IsInt ()) {
173
+ if (st_ != kHasNumber || !v_.IsInt ()) {
172
174
st_ = kError ;
173
175
return 0 ;
174
176
}
@@ -179,7 +181,7 @@ int LookaheadParser::GetInt() {
179
181
}
180
182
181
183
double LookaheadParser::GetDouble () {
182
- if (st_ != kHasValue || !v_. IsNumber () ) {
184
+ if (st_ != kHasNumber ) {
183
185
st_ = kError ;
184
186
return 0 .;
185
187
}
@@ -190,7 +192,7 @@ double LookaheadParser::GetDouble() {
190
192
}
191
193
192
194
bool LookaheadParser::GetBool () {
193
- if (st_ != kHasValue || !v_. IsBool () ) {
195
+ if (st_ != kHasBool ) {
194
196
st_ = kError ;
195
197
return false ;
196
198
}
@@ -201,7 +203,7 @@ bool LookaheadParser::GetBool() {
201
203
}
202
204
203
205
void LookaheadParser::GetNull () {
204
- if (st_ != kHasValue || !v_. IsNull () ) {
206
+ if (st_ != kHasNull ) {
205
207
st_ = kError ;
206
208
return ;
207
209
}
@@ -210,7 +212,7 @@ void LookaheadParser::GetNull() {
210
212
}
211
213
212
214
const char * LookaheadParser::GetString () {
213
- if (st_ != kHasValue || !v_. IsString () ) {
215
+ if (st_ != kHasString ) {
214
216
st_ = kError ;
215
217
return 0 ;
216
218
}
@@ -220,58 +222,57 @@ const char* LookaheadParser::GetString() {
220
222
return result;
221
223
}
222
224
223
- void LookaheadParser::SkipValue () {
224
- int depth = 0 ;
225
+ void LookaheadParser::SkipOut (int depth) {
225
226
do {
226
- switch (st_) {
227
- case kEnteringArray :
228
- case kEnteringObject :
229
- ++depth;
230
- break ;
231
-
232
- case kExitingArray :
233
- case kExitingObject :
234
- --depth;
235
- break ;
236
-
237
- case kError :
238
- return ;
239
-
240
- case kHasKey :
241
- case kHasValue :
242
- break ;
227
+ if (st_ == kEnteringArray || st_ == kEnteringObject ) {
228
+ ++depth;
243
229
}
230
+ else if (st_ == kExitingArray || st_ == kExitingObject ) {
231
+ --depth;
232
+ }
233
+ else if (st_ == kError ) {
234
+ return ;
235
+ }
236
+
244
237
ParseNext ();
245
238
}
246
239
while (depth > 0 );
247
240
}
248
241
242
+ void LookaheadParser::SkipValue () {
243
+ SkipOut (0 );
244
+ }
245
+
246
+ void LookaheadParser::SkipArray () {
247
+ SkipOut (1 );
248
+ }
249
+
250
+ void LookaheadParser::SkipObject () {
251
+ SkipOut (1 );
252
+ }
253
+
249
254
Value* LookaheadParser::PeekValue () {
250
- if (st_ == kHasValue || st_ = = kHasKey ) {
255
+ if (st_ >= kHasNull && st_ < = kHasKey ) {
251
256
return &v_;
252
257
}
253
258
254
259
return 0 ;
255
260
}
256
261
257
262
int LookaheadParser::PeekType () {
258
- switch (st_) {
259
- case kHasValue :
260
- case kHasKey :
261
- return v_.GetType ();
262
-
263
- case kEnteringArray :
264
- return kArrayType ;
265
-
266
- case kEnteringObject :
267
- return kObjectType ;
268
-
269
- case kExitingArray :
270
- case kExitingObject :
271
- case kError :
272
- default :
273
- return -1 ;
263
+ if (st_ >= kHasNull && st_ <= kHasKey ) {
264
+ return v_.GetType ();
274
265
}
266
+
267
+ if (st_ == kEnteringArray ) {
268
+ return kArrayType ;
269
+ }
270
+
271
+ if (st_ == kEnteringObject ) {
272
+ return kObjectType ;
273
+ }
274
+
275
+ return -1 ;
275
276
}
276
277
277
278
// -------------------------------------------------------------------------
@@ -325,7 +326,7 @@ int main() {
325
326
cout << r.GetString () << " " ;
326
327
}
327
328
else {
328
- r.ExitArray ();
329
+ r.SkipArray ();
329
330
break ;
330
331
}
331
332
}
0 commit comments