@@ -112,7 +112,9 @@ public void allowDuplicateKeys(boolean allowDuplicateKeys) {
112112
113113 @ Override
114114 public Token nextToken () throws IOException {
115- if (closed ) return null ;
115+ if (closed ) {
116+ return null ;
117+ }
116118
117119 // Clear value state
118120 currentValue = null ;
@@ -132,7 +134,8 @@ public Token nextToken() throws IOException {
132134
133135 case OBJECT_FIELD_NAME :
134136 if (currentContainer .fieldsRemaining == 0 ) {
135- Token endToken = currentContainer .type == ContainerContext .Type .OBJECT ? Token .END_OBJECT : Token .END_ARRAY ;
137+ assert currentContainer .type == ContainerContext .Type .OBJECT ;
138+ Token endToken = Token .END_OBJECT ;
136139 containerStack .pop ();
137140 currentContainer = containerStack .isEmpty () ? null : containerStack .peek ();
138141 updateStateAfterEnd ();
@@ -143,8 +146,18 @@ public Token nextToken() throws IOException {
143146 return currentToken = Token .FIELD_NAME ;
144147 }
145148 case OBJECT_VALUE :
146- case ARRAY_VALUE :
147149 return emitValue (keyArray .get (currentIndex ++));
150+ case ARRAY_VALUE :
151+ if (currentContainer .fieldsRemaining == 0 ) {
152+ assert currentContainer .type == ContainerContext .Type .ARRAY ;
153+ Token endToken = Token .END_ARRAY ;
154+ containerStack .pop ();
155+ currentContainer = containerStack .isEmpty () ? null : containerStack .peek ();
156+ updateStateAfterEnd ();
157+ return currentToken = endToken ;
158+ } else {
159+ return emitValue (keyArray .get (currentIndex ++));
160+ }
148161 case DONE :
149162 return null ;
150163 }
@@ -168,19 +181,21 @@ private Token emitValue(ESONSource.KeyEntry entry) {
168181 case ESONSource .KeyEntry .TYPE_OBJECT -> {
169182 containerStack .push (new ContainerContext (ContainerContext .Type .OBJECT , ((ESONSource .ObjectEntry ) entry ).fieldCount ));
170183 currentToken = Token .START_OBJECT ;
184+ state = State .OBJECT_FIELD_NAME ;
171185 }
172186 case ESONSource .KeyEntry .TYPE_ARRAY -> {
173187 containerStack .push (new ContainerContext (ContainerContext .Type .ARRAY , ((ESONSource .ArrayEntry ) entry ).elementCount ));
174188 currentToken = Token .START_ARRAY ;
189+ state = State .ARRAY_VALUE ;
175190 }
176191 case ESONSource .KeyEntry .TYPE_FIELD -> {
177192 currentType = ((ESONSource .FieldEntry ) entry ).type ;
178193 currentToken = determineValueToken (currentType );
194+ state = State .OBJECT_FIELD_NAME ;
179195 }
180196 default -> throw new IllegalStateException ("Unknown key entry type: " + entry .type ());
181197 }
182198
183- currentIndex ++;
184199 if (currentContainer != null ) {
185200 currentContainer .fieldsRemaining --;
186201 }
0 commit comments