Skip to content

Commit 615c858

Browse files
committed
read as object
1 parent 932fb23 commit 615c858

6 files changed

Lines changed: 45 additions & 26 deletions

File tree

src/main/java/com/jsoniter/IterImplArray.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ public static final boolean readArray(final JsonIterator iter) throws IOExceptio
2525
}
2626
}
2727

28-
public static final boolean readArrayCB(final JsonIterator iter, JsonIterator.ReadArrayCallback callback) throws IOException {
28+
public static final boolean readArrayCB(final JsonIterator iter, final JsonIterator.ReadArrayCallback callback, Object attachment) throws IOException {
2929
byte c = IterImpl.nextToken(iter);
3030
if (c == '[') {
3131
c = IterImpl.nextToken(iter);
3232
if (c != ']') {
3333
iter.unreadByte();
34-
if (!callback.handle(iter)) {
34+
if (!callback.handle(iter, attachment)) {
3535
return false;
3636
}
3737
while (IterImpl.nextToken(iter) == ',') {
38-
if (!callback.handle(iter)) {
38+
if (!callback.handle(iter, attachment)) {
3939
return false;
4040
}
4141
}

src/main/java/com/jsoniter/IterImplObject.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static final String readObject(JsonIterator iter) throws IOException {
3737
}
3838
}
3939

40-
public static final boolean readObjectCB(JsonIterator iter, JsonIterator.ReadObjectCallback cb) throws IOException {
40+
public static final boolean readObjectCB(JsonIterator iter, JsonIterator.ReadObjectCallback cb, Object attachment) throws IOException {
4141
byte c = IterImpl.nextToken(iter);
4242
if ('{' == c) {
4343
c = IterImpl.nextToken(iter);
@@ -47,15 +47,15 @@ public static final boolean readObjectCB(JsonIterator iter, JsonIterator.ReadObj
4747
if (IterImpl.nextToken(iter) != ':') {
4848
throw iter.reportError("readObject", "expect :");
4949
}
50-
if (!cb.handle(iter, field)) {
50+
if (!cb.handle(iter, field, attachment)) {
5151
return false;
5252
}
5353
while (IterImpl.nextToken(iter) == ',') {
5454
field = iter.readString();
5555
if (IterImpl.nextToken(iter) != ':') {
5656
throw iter.reportError("readObject", "expect :");
5757
}
58-
if (!cb.handle(iter, field)) {
58+
if (!cb.handle(iter, field, attachment)) {
5959
return false;
6060
}
6161
}

src/main/java/com/jsoniter/IterImplSkip.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class IterImplSkip {
66

7-
static final boolean[] breaks = new boolean[256];
7+
static final boolean[] breaks = new boolean[127];
88

99
static {
1010
breaks[' '] = true;

src/main/java/com/jsoniter/JsonIterator.java

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.math.BigInteger;
1313
import java.util.ArrayList;
1414
import java.util.HashMap;
15+
import java.util.List;
1516
import java.util.Map;
1617

1718
public 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);

src/test/java/com/jsoniter/TestArray.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ public void test_one_element() throws IOException {
5656
final List<Integer> values = new ArrayList<Integer>();
5757
iter.readArrayCB(new JsonIterator.ReadArrayCallback() {
5858
@Override
59-
public boolean handle(JsonIterator iter) throws IOException {
59+
public boolean handle(JsonIterator iter, Object attachment) throws IOException {
6060
values.add(iter.readInt());
6161
return true;
6262
}
63-
});
63+
}, null);
6464
assertEquals(Arrays.asList(1), values);
6565
}
6666

@@ -84,6 +84,8 @@ public void test_two_elements() throws IOException {
8484
assertEquals(1, iter.read(Any[].class)[0].toInt());
8585
iter.reset(iter.buf);
8686
assertEquals(1, iter.readAny().toInt(0));
87+
iter = JsonIterator.parse(" [ 1 , null, 2 ] ");
88+
assertEquals(Arrays.asList(1.0D, null, 2.0D), iter.read());
8789
}
8890

8991
public void test_three_elements() throws IOException {

src/test/java/com/jsoniter/TestObject.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public void test_one_field() throws IOException {
5656
Any any = iter.readAny();
5757
assertEquals("hello", any.toString("field1"));
5858
assertEquals(ValueType.INVALID, any.get("field2").valueType());
59+
iter.reset(iter.buf);
60+
assertEquals("hello", ((Map)iter.read()).get("field1"));
5961
}
6062

6163
public void test_two_fields() throws IOException {
@@ -77,12 +79,12 @@ public void test_two_fields() throws IOException {
7779
final ArrayList<String> fields = new ArrayList<String>();
7880
iter.readObjectCB(new JsonIterator.ReadObjectCallback() {
7981
@Override
80-
public boolean handle(JsonIterator iter, String field) throws IOException {
82+
public boolean handle(JsonIterator iter, String field, Object attachment) throws IOException {
8183
fields.add(field);
8284
iter.skip();
8385
return true;
8486
}
85-
});
87+
}, null);
8688
assertEquals(Arrays.asList("field1", "field2"), fields);
8789
}
8890

0 commit comments

Comments
 (0)