Skip to content

Commit 97e3b04

Browse files
committed
DoubleLazyAny and LongLazyAny
1 parent 40ec548 commit 97e3b04

9 files changed

Lines changed: 126 additions & 14 deletions

File tree

src/main/java/com/jsoniter/CodegenAccess.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,8 @@ public static void staticGenDecoders(TypeLiteral[] typeLiterals) {
179179
public static int head(JsonIterator iter) {
180180
return iter.head;
181181
}
182+
183+
public static void unreadByte(JsonIterator iter) throws IOException {
184+
iter.unreadByte();
185+
}
182186
}

src/main/java/com/jsoniter/IterImpl.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,24 @@ final static void skipUntilBreak(JsonIterator iter) throws IOException {
110110
iter.head = iter.tail;
111111
}
112112

113+
final static boolean skipNumber(JsonIterator iter) throws IOException {
114+
// true, false, null, number
115+
boolean dotFound = false;
116+
for (int i = iter.head; i < iter.tail; i++) {
117+
byte c = iter.buf[i];
118+
if (c == '.') {
119+
dotFound = true;
120+
continue;
121+
}
122+
if (IterImplSkip.breaks[c]) {
123+
iter.head = i;
124+
return dotFound;
125+
}
126+
}
127+
iter.head = iter.tail;
128+
return dotFound;
129+
}
130+
113131
// read the bytes between " "
114132
final static Slice readSlice(JsonIterator iter) throws IOException {
115133
int end = IterImplString.findSliceEnd(iter);
@@ -173,9 +191,11 @@ public static Any readAny(JsonIterator iter) throws IOException {
173191
case '7':
174192
case '8':
175193
case '9':
176-
skipUntilBreak(iter);
177-
// TODO: LongLazyAny or DoubleLazyAny
178-
return Any.lazyNumber(iter.buf, start, iter.head);
194+
if (skipNumber(iter)) {
195+
return Any.lazyDouble(iter.buf, start, iter.head);
196+
} else {
197+
return Any.lazyLong(iter.buf, start, iter.head);
198+
}
179199
case 't':
180200
skipUntilBreak(iter);
181201
return Any.wrap(true);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ public static Any readAny(JsonIterator iter) throws IOException {
306306
case '9':
307307
IterImpl.skipUntilBreak(iter);
308308
copied = copySkippedBytes(iter);
309-
return Any.lazyNumber(copied, 0, copied.length);
309+
return Any.lazyDouble(copied, 0, copied.length);
310310
case 't':
311311
IterImpl.skipUntilBreak(iter);
312312
iter.skipStartedAt = -1;

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

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

3-
import com.jsoniter.any.*;
4-
53
import java.io.IOException;
64

75
class IterImplSkip {

src/main/java/com/jsoniter/any/Any.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.jsoniter.ValueType;
66
import com.jsoniter.output.JsonStream;
77
import com.jsoniter.spi.Encoder;
8-
import com.jsoniter.spi.JsoniterSpi;
98
import com.jsoniter.spi.TypeLiteral;
109

1110
import java.io.IOException;
@@ -34,7 +33,7 @@ public Any wrap(Object obj) {
3433
JsonStream.registerNativeEncoder(IntAny.class, anyEncoder);
3534
JsonStream.registerNativeEncoder(LongAny.class, anyEncoder);
3635
JsonStream.registerNativeEncoder(NullAny.class, anyEncoder);
37-
JsonStream.registerNativeEncoder(NumberLazyAny.class, anyEncoder);
36+
JsonStream.registerNativeEncoder(DoubleLazyAny.class, anyEncoder);
3837
JsonStream.registerNativeEncoder(ObjectLazyAny.class, anyEncoder);
3938
JsonStream.registerNativeEncoder(StringAny.class, anyEncoder);
4039
JsonStream.registerNativeEncoder(StringLazyAny.class, anyEncoder);
@@ -285,8 +284,12 @@ public static Any lazyString(byte[] data, int head, int tail) {
285284
return new StringLazyAny(data, head, tail);
286285
}
287286

288-
public static Any lazyNumber(byte[] data, int head, int tail) {
289-
return new NumberLazyAny(data, head, tail);
287+
public static Any lazyDouble(byte[] data, int head, int tail) {
288+
return new DoubleLazyAny(data, head, tail);
289+
}
290+
291+
public static Any lazyLong(byte[] data, int head, int tail) {
292+
return new LongLazyAny(data, head, tail);
290293
}
291294

292295
public static Any lazyArray(byte[] data, int head, int tail) {

src/main/java/com/jsoniter/any/NumberLazyAny.java renamed to src/main/java/com/jsoniter/any/DoubleLazyAny.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
import java.io.IOException;
77

8-
class NumberLazyAny extends LazyAny {
8+
class DoubleLazyAny extends LazyAny {
99

1010
private boolean isCached;
1111
private double cache;
1212

13-
public NumberLazyAny(byte[] data, int head, int tail) {
13+
public DoubleLazyAny(byte[] data, int head, int tail) {
1414
super(data, head, tail);
1515
}
1616

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.jsoniter.any;
2+
3+
import com.jsoniter.JsonException;
4+
import com.jsoniter.ValueType;
5+
6+
import java.io.IOException;
7+
8+
class LongLazyAny extends LazyAny {
9+
10+
private boolean isCached;
11+
private long cache;
12+
13+
public LongLazyAny(byte[] data, int head, int tail) {
14+
super(data, head, tail);
15+
}
16+
17+
@Override
18+
public ValueType valueType() {
19+
return ValueType.NUMBER;
20+
}
21+
22+
@Override
23+
public Object object() {
24+
fillCache();
25+
return cache;
26+
}
27+
28+
@Override
29+
public boolean toBoolean() {
30+
fillCache();
31+
return cache != 0;
32+
}
33+
34+
@Override
35+
public int toInt() {
36+
fillCache();
37+
return (int) cache;
38+
}
39+
40+
@Override
41+
public long toLong() {
42+
fillCache();
43+
return cache;
44+
}
45+
46+
@Override
47+
public float toFloat() {
48+
fillCache();
49+
return cache;
50+
}
51+
52+
@Override
53+
public double toDouble() {
54+
fillCache();
55+
return cache;
56+
}
57+
58+
private void fillCache() {
59+
if (!isCached) {
60+
try {
61+
cache = parse().readLong();
62+
} catch (IOException e) {
63+
throw new JsonException(e);
64+
}
65+
isCached = true;
66+
}
67+
}
68+
}

src/main/java/com/jsoniter/spi/Decoder.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,24 @@ public short decodeShort(JsonIterator iter) throws IOException {
5050
}
5151
}
5252

53+
class MaybeStringShortDecoder extends ShortDecoder {
54+
55+
@Override
56+
public short decodeShort(JsonIterator iter) throws IOException {
57+
byte c = CodegenAccess.nextToken(iter);
58+
if (c != '"') {
59+
CodegenAccess.unreadByte(iter);
60+
return iter.readShort();
61+
}
62+
short val = iter.readShort();
63+
c = CodegenAccess.nextToken(iter);
64+
if (c != '"') {
65+
throw iter.reportError("StringShortDecoder", "expect \", but found: " + (char) c);
66+
}
67+
return val;
68+
}
69+
}
70+
5371
abstract class IntDecoder implements Decoder {
5472
@Override
5573
public Object decode(JsonIterator iter) throws IOException {
@@ -76,6 +94,8 @@ public int decodeInt(JsonIterator iter) throws IOException {
7694
}
7795
}
7896

97+
// TODO: add MaybeStringIntDecoder
98+
7999
abstract class LongDecoder implements Decoder {
80100
@Override
81101
public Object decode(JsonIterator iter) throws IOException {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ public void test_read_int() throws IOException {
3535
assertEquals(100, JsonIterator.deserialize("\"100\"").toInt());
3636
assertEquals(1, JsonIterator.deserialize("true").toInt());
3737
Any any = JsonIterator.deserialize("100");
38-
assertEquals(Double.valueOf(100), any.object());
39-
assertEquals(Double.valueOf(100), any.object());
38+
assertEquals(Long.valueOf(100), any.object());
4039
assertEquals(100, any.toInt());
4140
assertEquals(100L, any.toLong());
4241
assertEquals(100F, any.toFloat());

0 commit comments

Comments
 (0)