Skip to content

Commit b045cba

Browse files
committed
LongLazyAny for streaming
1 parent 97e3b04 commit b045cba

2 files changed

Lines changed: 40 additions & 10 deletions

File tree

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

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,28 @@ final static void skipUntilBreak(JsonIterator iter) throws IOException {
171171
}
172172
}
173173

174+
final static boolean skipNumber(JsonIterator iter) throws IOException {
175+
// true, false, null, number
176+
boolean dotFound = false;
177+
for (; ; ) {
178+
for (int i = iter.head; i < iter.tail; i++) {
179+
byte c = iter.buf[i];
180+
if (c == '.') {
181+
dotFound = true;
182+
continue;
183+
}
184+
if (IterImplSkip.breaks[c]) {
185+
iter.head = i;
186+
return dotFound;
187+
}
188+
}
189+
if (!loadMore(iter)) {
190+
iter.head = iter.tail;
191+
return dotFound;
192+
}
193+
}
194+
}
195+
174196
// read the bytes between " "
175197
final static Slice readSlice(JsonIterator iter) throws IOException {
176198
int end = IterImplString.findSliceEnd(iter);
@@ -287,10 +309,10 @@ final static byte readByte(JsonIterator iter) throws IOException {
287309
public static Any readAny(JsonIterator iter) throws IOException {
288310
// TODO: avoid small memory allocation
289311
iter.skipStartedAt = iter.head;
290-
byte c = IterImpl.nextToken(iter);
312+
byte c = nextToken(iter);
291313
switch (c) {
292314
case '"':
293-
IterImpl.skipString(iter);
315+
skipString(iter);
294316
byte[] copied = copySkippedBytes(iter);
295317
return Any.lazyString(copied, 0, copied.length);
296318
case '-':
@@ -304,27 +326,31 @@ public static Any readAny(JsonIterator iter) throws IOException {
304326
case '7':
305327
case '8':
306328
case '9':
307-
IterImpl.skipUntilBreak(iter);
308-
copied = copySkippedBytes(iter);
309-
return Any.lazyDouble(copied, 0, copied.length);
329+
if (skipNumber(iter)) {
330+
copied = copySkippedBytes(iter);
331+
return Any.lazyDouble(copied, 0, copied.length);
332+
} else {
333+
copied = copySkippedBytes(iter);
334+
return Any.lazyLong(copied, 0, copied.length);
335+
}
310336
case 't':
311-
IterImpl.skipUntilBreak(iter);
337+
skipUntilBreak(iter);
312338
iter.skipStartedAt = -1;
313339
return Any.wrap(true);
314340
case 'f':
315-
IterImpl.skipUntilBreak(iter);
341+
skipUntilBreak(iter);
316342
iter.skipStartedAt = -1;
317343
return Any.wrap(false);
318344
case 'n':
319-
IterImpl.skipUntilBreak(iter);
345+
skipUntilBreak(iter);
320346
iter.skipStartedAt = -1;
321347
return Any.wrap((Object)null);
322348
case '[':
323-
IterImpl.skipArray(iter);
349+
skipArray(iter);
324350
copied = copySkippedBytes(iter);
325351
return Any.lazyArray(copied, 0, copied.length);
326352
case '{':
327-
IterImpl.skipObject(iter);
353+
skipObject(iter);
328354
copied = copySkippedBytes(iter);
329355
return Any.lazyObject(copied, 0, copied.length);
330356
default:

src/test/java/com/jsoniter/TestReadAny.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
public class TestReadAny extends TestCase {
1515

16+
static {
17+
// JsonIterator.enableStreamingSupport();
18+
}
19+
1620
public void test_read_any() throws IOException {
1721
JsonIterator iter = JsonIterator.parse("[0,1,2,3]");
1822
assertEquals(3, iter.readAny().toInt(3));

0 commit comments

Comments
 (0)