@@ -20,32 +20,56 @@ public class JsonParserSequence extends JsonParserDelegate
20
20
* as delegate)
21
21
*/
22
22
protected final JsonParser [] _parsers ;
23
-
23
+
24
+ /**
25
+ * Configuration that determines whether state of parsers is first verified
26
+ * to see if parser already points to a token (that is,
27
+ * {@link JsonParser#hasCurrentToken()} returns <code>true</code>), and if so
28
+ * that token is first return before {@link JsonParser#nextToken} is called.
29
+ * If enabled, this check is made; if disabled, no check is made and
30
+ * {@link JsonParser#nextToken} is always called for all parsers.
31
+ *<p>
32
+ * Default setting is <code>false</code> (for backwards-compatibility)
33
+ * so that possible existing token is not considered for parsers.
34
+ *
35
+ * @since 2.8
36
+ */
37
+ protected final boolean _checkForExistingToken ;
38
+
24
39
/**
25
40
* Index of the next parser in {@link #_parsers}.
26
41
*/
27
- protected int _nextParser ;
42
+ protected int _nextParserIndex ;
28
43
29
44
/**
30
45
* Flag used to indicate that `JsonParser.nextToken()` should not be called,
31
46
* due to parser already pointing to a token.
32
47
*
33
48
* @since 2.8
34
49
*/
35
- protected boolean _suppressNextToken ;
36
-
50
+ protected boolean _hasToken ;
51
+
37
52
/*
38
53
*******************************************************
39
54
* Construction
40
55
*******************************************************
41
56
*/
42
57
43
- protected JsonParserSequence (JsonParser [] parsers )
58
+ @ Deprecated // since 2.8
59
+ protected JsonParserSequence (JsonParser [] parsers ) {
60
+ this (false , parsers );
61
+ }
62
+
63
+ /**
64
+ * @since 2.8
65
+ */
66
+ protected JsonParserSequence (boolean checkForExistingToken , JsonParser [] parsers )
44
67
{
45
68
super (parsers [0 ]);
46
- _suppressNextToken = delegate .hasCurrentToken ();
69
+ _checkForExistingToken = checkForExistingToken ;
70
+ _hasToken = checkForExistingToken && delegate .hasCurrentToken ();
47
71
_parsers = parsers ;
48
- _nextParser = 1 ;
72
+ _nextParserIndex = 1 ;
49
73
}
50
74
51
75
/**
@@ -57,11 +81,12 @@ protected JsonParserSequence(JsonParser[] parsers)
57
81
* within sequences. This is done to minimize delegation depth,
58
82
* ideally only having just a single level of delegation.
59
83
*/
60
- public static JsonParserSequence createFlattened (JsonParser first , JsonParser second )
84
+ public static JsonParserSequence createFlattened (boolean checkForExistingToken ,
85
+ JsonParser first , JsonParser second )
61
86
{
62
87
if (!(first instanceof JsonParserSequence || second instanceof JsonParserSequence )) {
63
- // simple:
64
- return new JsonParserSequence ( new JsonParser [] { first , second });
88
+ return new JsonParserSequence ( checkForExistingToken ,
89
+ new JsonParser [] { first , second });
65
90
}
66
91
ArrayList <JsonParser > p = new ArrayList <JsonParser >();
67
92
if (first instanceof JsonParserSequence ) {
@@ -74,29 +99,39 @@ public static JsonParserSequence createFlattened(JsonParser first, JsonParser se
74
99
} else {
75
100
p .add (second );
76
101
}
77
- return new JsonParserSequence (p .toArray (new JsonParser [p .size ()]));
102
+ return new JsonParserSequence (checkForExistingToken ,
103
+ p .toArray (new JsonParser [p .size ()]));
78
104
}
79
105
106
+ /**
107
+ * @deprecated Since 2.8 use {@link #createFlattened(boolean, JsonParser, JsonParser)}
108
+ * instead
109
+ */
110
+ @ Deprecated // since 2.8
111
+ public static JsonParserSequence createFlattened (JsonParser first , JsonParser second ) {
112
+ return createFlattened (false , first , second );
113
+ }
114
+
80
115
@ SuppressWarnings ("resource" )
81
- protected void addFlattenedActiveParsers (List <JsonParser > result )
116
+ protected void addFlattenedActiveParsers (List <JsonParser > listToAddIn )
82
117
{
83
- for (int i = _nextParser -1 , len = _parsers .length ; i < len ; ++i ) {
118
+ for (int i = _nextParserIndex -1 , len = _parsers .length ; i < len ; ++i ) {
84
119
JsonParser p = _parsers [i ];
85
120
if (p instanceof JsonParserSequence ) {
86
- ((JsonParserSequence ) p ).addFlattenedActiveParsers (result );
121
+ ((JsonParserSequence ) p ).addFlattenedActiveParsers (listToAddIn );
87
122
} else {
88
- result .add (p );
123
+ listToAddIn .add (p );
89
124
}
90
125
}
91
126
}
92
-
127
+
93
128
/*
94
- *******************************************************
95
- * Overridden methods, needed: cases where default
96
- * delegation does not work
97
- *******************************************************
129
+ / *******************************************************
130
+ / * Overridden methods, needed: cases where default
131
+ / * delegation does not work
132
+ / *******************************************************
98
133
*/
99
-
134
+
100
135
@ Override
101
136
public void close () throws IOException {
102
137
do { delegate .close (); } while (switchToNext ());
@@ -108,14 +143,13 @@ public JsonToken nextToken() throws IOException
108
143
if (delegate == null ) {
109
144
return null ;
110
145
}
111
- if (_suppressNextToken ) {
112
- _suppressNextToken = false ;
113
- return delegate .currentToken ();
146
+ if (_hasToken ) {
147
+ _hasToken = false ;
148
+ return delegate .currentToken ();
114
149
}
115
150
JsonToken t = delegate .nextToken ();
116
- while ((t == null ) && switchToNext ()) {
117
- t = delegate .hasCurrentToken ()
118
- ? delegate .currentToken () : delegate .nextToken ();
151
+ if (t == null ) {
152
+ return switchAndReturnNext ();
119
153
}
120
154
return t ;
121
155
}
@@ -134,27 +168,43 @@ public JsonToken nextToken() throws IOException
134
168
public int containedParsersCount () {
135
169
return _parsers .length ;
136
170
}
137
-
171
+
138
172
/*
139
173
/*******************************************************
140
174
/* Helper methods
141
175
/*******************************************************
142
176
*/
143
177
144
178
/**
145
- * Method that will switch active parser from the current one
146
- * to next parser in sequence, if there is another parser left,
147
- * making this the new delegate. Old delegate is returned if
148
- * switch succeeds.
179
+ * Method that will switch active delegate parser from the current one
180
+ * to the next parser in sequence, if there is another parser left:
181
+ * if so, the next parser will become the active delegate parser.
149
182
*
150
183
* @return True if switch succeeded; false otherwise
184
+ *
185
+ * @since 2.8
151
186
*/
152
187
protected boolean switchToNext ()
153
188
{
154
- if (_nextParser >= _parsers .length ) {
155
- return false ;
189
+ if (_nextParserIndex < _parsers .length ) {
190
+ delegate = _parsers [_nextParserIndex ++];
191
+ return true ;
192
+ }
193
+ return false ;
194
+ }
195
+
196
+ protected JsonToken switchAndReturnNext () throws IOException
197
+ {
198
+ while (_nextParserIndex < _parsers .length ) {
199
+ delegate = _parsers [_nextParserIndex ++];
200
+ if (_checkForExistingToken && delegate .hasCurrentToken ()) {
201
+ return delegate .getCurrentToken ();
202
+ }
203
+ JsonToken t = delegate .nextToken ();
204
+ if (t != null ) {
205
+ return t ;
206
+ }
156
207
}
157
- delegate = _parsers [_nextParser ++];
158
- return true ;
208
+ return null ;
159
209
}
160
210
}
0 commit comments