Skip to content

Commit 932fb23

Browse files
committed
test skip string in streaming mode
1 parent 0add6f5 commit 932fb23

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ public static final boolean readArrayCB(final JsonIterator iter, JsonIterator.Re
3434
if (!callback.handle(iter)) {
3535
return false;
3636
}
37-
c = IterImpl.nextToken(iter);
38-
while (c == ',') {
37+
while (IterImpl.nextToken(iter) == ',') {
3938
if (!callback.handle(iter)) {
4039
return false;
4140
}

src/main/java/com/jsoniter/IterImplForStreaming.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,10 @@ final static void skipString(JsonIterator iter) throws IOException {
125125
if (end == -1) {
126126
int j = iter.tail - 1;
127127
boolean escaped = true;
128+
// can not just look the last byte is \
129+
// because it could be \\ or \\\
128130
for (; ; ) {
131+
// walk backward until head
129132
if (j < iter.head || iter.buf[j] != '\\') {
130133
// even number of backslashes
131134
// either end of buffer, or " found
@@ -142,10 +145,10 @@ final static void skipString(JsonIterator iter) throws IOException {
142145

143146
}
144147
if (!loadMore(iter)) {
145-
return;
148+
throw iter.reportError("skipString", "incomplete string");
146149
}
147150
if (escaped) {
148-
iter.head = 1; // skip the first char as last char readAny is \
151+
iter.head = 1; // skip the first char as last char is \
149152
}
150153
} else {
151154
iter.head = end;

src/test/java/com/jsoniter/TestSkip.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.jsoniter;
22

3+
import com.jsoniter.spi.JsonException;
34
import junit.framework.TestCase;
5+
import org.junit.experimental.categories.Category;
46

7+
import java.io.ByteArrayInputStream;
58
import java.io.IOException;
69

710
public class TestSkip extends TestCase {
@@ -24,6 +27,27 @@ public void test_skip_string() throws IOException {
2427
assertFalse(iter.readArray());
2528
}
2629

30+
@Category(StreamingCategory.class)
31+
public void test_skip_string_streaming() throws IOException {
32+
JsonIterator iter = JsonIterator.parse(new ByteArrayInputStream("\"hello".getBytes()), 2);
33+
try {
34+
iter.skip();
35+
fail();
36+
} catch (JsonException e) {
37+
}
38+
iter = JsonIterator.parse(new ByteArrayInputStream("\"hello\"".getBytes()), 2);
39+
iter.skip();
40+
iter = JsonIterator.parse(new ByteArrayInputStream("\"hello\"1".getBytes()), 2);
41+
iter.skip();
42+
assertEquals(1, iter.readInt());
43+
iter = JsonIterator.parse(new ByteArrayInputStream("\"h\\\"ello\"1".getBytes()), 3);
44+
iter.skip();
45+
assertEquals(1, iter.readInt());
46+
iter = JsonIterator.parse(new ByteArrayInputStream("\"\\\\\"1".getBytes()), 3);
47+
iter.skip();
48+
assertEquals(1, iter.readInt());
49+
}
50+
2751
public void test_skip_object() throws IOException {
2852
JsonIterator iter = JsonIterator.parse("[{'hello': {'world': 'a'}},2]".replace('\'', '"'));
2953
assertTrue(iter.readArray());

0 commit comments

Comments
 (0)