Skip to content

Commit 975242c

Browse files
committed
fix #115 int/long should not have leading zero
1 parent 0771b92 commit 975242c

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/main/java/com/jsoniter/IterImplNumber.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3131
*/
3232
package com.jsoniter;
3333

34+
import com.jsoniter.spi.JsonException;
35+
3436
import java.io.IOException;
3537

3638
class IterImplNumber {
@@ -85,6 +87,8 @@ public static final int readInt(final JsonIterator iter) throws IOException {
8587
byte c = IterImpl.nextToken(iter);
8688
if (c == '-') {
8789
return -IterImpl.readPositiveInt(iter, IterImpl.readByte(iter));
90+
} else if (c == '0') {
91+
throw new JsonException("leading zero is invalid number");
8892
} else {
8993
return IterImpl.readPositiveInt(iter, c);
9094
}
@@ -94,6 +98,8 @@ public static final long readLong(JsonIterator iter) throws IOException {
9498
byte c = IterImpl.nextToken(iter);
9599
if (c == '-') {
96100
return -IterImpl.readPositiveLong(iter, IterImpl.readByte(iter));
101+
} else if (c == '0') {
102+
throw new JsonException("leading zero is invalid number");
97103
} else {
98104
return IterImpl.readPositiveLong(iter, c);
99105
}

src/test/java/com/jsoniter/TestInteger.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ public void test_leading_zero() throws IOException {
9292
fail();
9393
} catch (JsonException e) {
9494
}
95+
try {
96+
JsonIterator.deserialize("001", long.class);
97+
fail();
98+
} catch (JsonException e) {
99+
}
100+
try {
101+
JsonIterator.deserialize("001");
102+
fail();
103+
} catch (JsonException e) {
104+
}
95105
}
96106

97107
public void test_max_int() throws IOException {

0 commit comments

Comments
 (0)