1212import java .math .BigInteger ;
1313import java .util .ArrayList ;
1414import java .util .HashMap ;
15+ import java .util .List ;
1516import java .util .Map ;
1617
1718public class JsonIterator implements Closeable {
@@ -184,11 +185,11 @@ public final boolean readArray() throws IOException {
184185 }
185186
186187 public static interface ReadArrayCallback {
187- boolean handle (JsonIterator iter ) throws IOException ;
188+ boolean handle (JsonIterator iter , Object attachment ) throws IOException ;
188189 }
189190
190- public final boolean readArrayCB (ReadArrayCallback callback ) throws IOException {
191- return IterImplArray .readArrayCB (this , callback );
191+ public final boolean readArrayCB (ReadArrayCallback callback , Object attachment ) throws IOException {
192+ return IterImplArray .readArrayCB (this , callback , attachment );
192193 }
193194
194195 public final String readString () throws IOException {
@@ -204,11 +205,11 @@ public final String readObject() throws IOException {
204205 }
205206
206207 public static interface ReadObjectCallback {
207- boolean handle (JsonIterator iter , String field ) throws IOException ;
208+ boolean handle (JsonIterator iter , String field , Object attachment ) throws IOException ;
208209 }
209210
210- public final void readObjectCB (ReadObjectCallback cb ) throws IOException {
211- IterImplObject .readObjectCB (this , cb );
211+ public final void readObjectCB (ReadObjectCallback cb , Object attachment ) throws IOException {
212+ IterImplObject .readObjectCB (this , cb , attachment );
212213 }
213214
214215 public final float readFloat () throws IOException {
@@ -239,6 +240,24 @@ public final Any readAny() throws IOException {
239240 return IterImpl .readAny (this );
240241 }
241242
243+ private final static ReadArrayCallback fillArray = new ReadArrayCallback () {
244+ @ Override
245+ public boolean handle (JsonIterator iter , Object attachment ) throws IOException {
246+ List list = (List ) attachment ;
247+ list .add (iter .read ());
248+ return true ;
249+ }
250+ };
251+
252+ private final static ReadObjectCallback fillObject = new ReadObjectCallback () {
253+ @ Override
254+ public boolean handle (JsonIterator iter , String field , Object attachment ) throws IOException {
255+ Map map = (Map ) attachment ;
256+ map .put (field , iter .read ());
257+ return true ;
258+ }
259+ };
260+
242261 public final Object read () throws IOException {
243262 ValueType valueType = whatIsNext ();
244263 switch (valueType ) {
@@ -247,21 +266,17 @@ public final Object read() throws IOException {
247266 case NUMBER :
248267 return readDouble ();
249268 case NULL :
250- IterImpl .skipFixedBytes (this , 3 );
269+ IterImpl .skipFixedBytes (this , 4 );
251270 return null ;
252271 case BOOLEAN :
253272 return readBoolean ();
254273 case ARRAY :
255- ArrayList list = new ArrayList ();
256- while (readArray ()) {
257- list .add (read ());
258- }
274+ ArrayList list = new ArrayList (4 );
275+ readArrayCB (fillArray , list );
259276 return list ;
260277 case OBJECT :
261- Map map = new HashMap ();
262- for (String field = readObject (); field != null ; field = readObject ()) {
263- map .put (field , read ());
264- }
278+ Map map = new HashMap (4 );
279+ readObjectCB (fillObject , map );
265280 return map ;
266281 default :
267282 throw reportError ("read" , "unexpected value type: " + valueType );
0 commit comments