7
7
8
8
import com .fasterxml .jackson .core .*;
9
9
import com .fasterxml .jackson .core .base .ParserMinimalBase ;
10
-
11
10
import com .fasterxml .jackson .databind .JsonNode ;
12
11
13
12
/**
@@ -37,18 +36,6 @@ public class TreeTraversingParser extends ParserMinimalBase
37
36
/**********************************************************
38
37
*/
39
38
40
- /**
41
- * Sometimes parser needs to buffer a single look-ahead token; if so,
42
- * it'll be stored here. This is currently used for handling
43
- */
44
- protected JsonToken _nextToken ;
45
-
46
- /**
47
- * Flag needed to handle recursion into contents of child
48
- * Array/Object nodes.
49
- */
50
- protected boolean _startContainer ;
51
-
52
39
/**
53
40
* Flag that indicates whether parser is closed or not. Gets
54
41
* set when parser is either closed by explicit call
@@ -68,15 +55,7 @@ public TreeTraversingParser(JsonNode n, ObjectCodec codec)
68
55
{
69
56
super (0 );
70
57
_objectCodec = codec ;
71
- if (n .isArray ()) {
72
- _nextToken = JsonToken .START_ARRAY ;
73
- _nodeCursor = new NodeCursor .ArrayCursor (n , null );
74
- } else if (n .isObject ()) {
75
- _nextToken = JsonToken .START_OBJECT ;
76
- _nodeCursor = new NodeCursor .ObjectCursor (n , null );
77
- } else { // value node
78
- _nodeCursor = new NodeCursor .RootCursor (n , null );
79
- }
58
+ _nodeCursor = new NodeCursor .RootCursor (n , null );
80
59
}
81
60
82
61
@ Override
@@ -119,57 +98,37 @@ public void close() throws IOException
119
98
@ Override
120
99
public JsonToken nextToken () throws IOException , JsonParseException
121
100
{
122
- if (_nextToken != null ) {
123
- _currToken = _nextToken ;
124
- _nextToken = null ;
125
- return _currToken ;
126
- }
127
- // are we to descend to a container child?
128
- if (_startContainer ) {
129
- _startContainer = false ;
130
- // minor optimization: empty containers can be skipped
131
- if (!_nodeCursor .currentHasChildren ()) {
132
- _currToken = (_currToken == JsonToken .START_OBJECT ) ?
133
- JsonToken .END_OBJECT : JsonToken .END_ARRAY ;
134
- return _currToken ;
135
- }
136
- _nodeCursor = _nodeCursor .iterateChildren ();
137
- _currToken = _nodeCursor .nextToken ();
138
- if (_currToken == JsonToken .START_OBJECT || _currToken == JsonToken .START_ARRAY ) {
139
- _startContainer = true ;
140
- }
141
- return _currToken ;
142
- }
143
- // No more content?
144
- if (_nodeCursor == null ) {
101
+ _currToken = _nodeCursor .nextToken ();
102
+ if (_currToken == null ) {
145
103
_closed = true ; // if not already set
146
104
return null ;
147
105
}
148
- // Otherwise, next entry from current cursor
149
- _currToken = _nodeCursor .nextToken ();
150
- if (_currToken != null ) {
151
- if (_currToken == JsonToken .START_OBJECT || _currToken == JsonToken .START_ARRAY ) {
152
- _startContainer = true ;
153
- }
154
- return _currToken ;
106
+ switch (_currToken ) {
107
+ case START_OBJECT :
108
+ _nodeCursor = _nodeCursor .startObject ();
109
+ break ;
110
+ case START_ARRAY :
111
+ _nodeCursor = _nodeCursor .startArray ();
112
+ break ;
113
+ case END_OBJECT :
114
+ case END_ARRAY :
115
+ _nodeCursor = _nodeCursor .getParent ();
116
+ default :
155
117
}
156
- // null means no more children; need to return end marker
157
- _currToken = _nodeCursor .endToken ();
158
- _nodeCursor = _nodeCursor .getParent ();
159
118
return _currToken ;
160
119
}
161
-
120
+
162
121
// default works well here:
163
- //public JsonToken nextValue() throws IOException, JsonParseException
122
+ //public JsonToken nextValue() throws IOException
164
123
165
124
@ Override
166
- public JsonParser skipChildren () throws IOException , JsonParseException
125
+ public JsonParser skipChildren () throws IOException
167
126
{
168
127
if (_currToken == JsonToken .START_OBJECT ) {
169
- _startContainer = false ;
128
+ _nodeCursor = _nodeCursor . getParent () ;
170
129
_currToken = JsonToken .END_OBJECT ;
171
130
} else if (_currToken == JsonToken .START_ARRAY ) {
172
- _startContainer = false ;
131
+ _nodeCursor = _nodeCursor . getParent () ;
173
132
_currToken = JsonToken .END_ARRAY ;
174
133
}
175
134
return this ;
@@ -186,19 +145,24 @@ public boolean isClosed() {
186
145
/**********************************************************
187
146
*/
188
147
189
- @ Override
190
- public String getCurrentName () {
191
- return (_nodeCursor == null ) ? null : _nodeCursor .getCurrentName ();
148
+ @ Override public String getCurrentName () {
149
+ NodeCursor crsr = _nodeCursor ;
150
+ if (_currToken == JsonToken .START_OBJECT || _currToken == JsonToken .START_ARRAY ) {
151
+ crsr = crsr .getParent ();
152
+ }
153
+ return (crsr == null ) ? null : crsr .getCurrentName ();
192
154
}
193
155
194
- @ Override
195
- public void overrideCurrentName (String name )
196
- {
197
- if (_nodeCursor != null ) {
198
- _nodeCursor .overrideCurrentName (name );
156
+ @ Override public void overrideCurrentName (String name ) {
157
+ NodeCursor crsr = _nodeCursor ;
158
+ if (_currToken == JsonToken .START_OBJECT || _currToken == JsonToken .START_ARRAY ) {
159
+ crsr = crsr .getParent ();
160
+ }
161
+ if (crsr != null ) {
162
+ crsr .overrideCurrentName (name );
199
163
}
200
164
}
201
-
165
+
202
166
@ Override
203
167
public JsonStreamContext getParsingContext () {
204
168
return _nodeCursor ;
0 commit comments