77
88import com .fasterxml .jackson .core .*;
99import com .fasterxml .jackson .core .base .ParserMinimalBase ;
10-
1110import com .fasterxml .jackson .databind .JsonNode ;
1211
1312/**
@@ -37,18 +36,6 @@ public class TreeTraversingParser extends ParserMinimalBase
3736 /**********************************************************
3837 */
3938
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-
5239 /**
5340 * Flag that indicates whether parser is closed or not. Gets
5441 * set when parser is either closed by explicit call
@@ -68,15 +55,7 @@ public TreeTraversingParser(JsonNode n, ObjectCodec codec)
6855 {
6956 super (0 );
7057 _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 );
8059 }
8160
8261 @ Override
@@ -119,57 +98,37 @@ public void close() throws IOException
11998 @ Override
12099 public JsonToken nextToken () throws IOException , JsonParseException
121100 {
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 ) {
145103 _closed = true ; // if not already set
146104 return null ;
147105 }
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 :
155117 }
156- // null means no more children; need to return end marker
157- _currToken = _nodeCursor .endToken ();
158- _nodeCursor = _nodeCursor .getParent ();
159118 return _currToken ;
160119 }
161-
120+
162121 // default works well here:
163- //public JsonToken nextValue() throws IOException, JsonParseException
122+ //public JsonToken nextValue() throws IOException
164123
165124 @ Override
166- public JsonParser skipChildren () throws IOException , JsonParseException
125+ public JsonParser skipChildren () throws IOException
167126 {
168127 if (_currToken == JsonToken .START_OBJECT ) {
169- _startContainer = false ;
128+ _nodeCursor = _nodeCursor . getParent () ;
170129 _currToken = JsonToken .END_OBJECT ;
171130 } else if (_currToken == JsonToken .START_ARRAY ) {
172- _startContainer = false ;
131+ _nodeCursor = _nodeCursor . getParent () ;
173132 _currToken = JsonToken .END_ARRAY ;
174133 }
175134 return this ;
@@ -186,19 +145,24 @@ public boolean isClosed() {
186145 /**********************************************************
187146 */
188147
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 ();
192154 }
193155
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 );
199163 }
200164 }
201-
165+
202166 @ Override
203167 public JsonStreamContext getParsingContext () {
204168 return _nodeCursor ;
0 commit comments