Skip to content

Commit 78adeb5

Browse files
committed
fix leading zero
1 parent 0238f6c commit 78adeb5

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,10 @@ public static int updateStringCopyBound(final JsonIterator iter, final int bound
325325

326326
static final int readPositiveInt(final JsonIterator iter, byte c) throws IOException {
327327
int ind = IterImplNumber.intDigits[c];
328+
if (ind == 0) {
329+
IterImplForStreaming.assertNotLeadingZero(iter);
330+
return 0;
331+
}
328332
if (ind == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
329333
throw iter.reportError("readPositiveInt", "expect 0~9");
330334
}
@@ -335,9 +339,6 @@ static final int readPositiveInt(final JsonIterator iter, byte c) throws IOExcep
335339
iter.head = i;
336340
return ind;
337341
}
338-
if (ind == 0) {
339-
throw iter.reportError("readPositiveInt", "leading zero is invalid for int");
340-
}
341342
int ind3 = IterImplNumber.intDigits[iter.buf[++i]];
342343
if (ind3 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
343344
iter.head = i;
@@ -380,6 +381,10 @@ static final int readPositiveInt(final JsonIterator iter, byte c) throws IOExcep
380381

381382
static final long readPositiveLong(final JsonIterator iter, byte c) throws IOException {
382383
long ind = IterImplNumber.intDigits[c];
384+
if (ind == 0) {
385+
IterImplForStreaming.assertNotLeadingZero(iter);
386+
return 0;
387+
}
383388
if (ind == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
384389
throw iter.reportError("readPositiveLong", "expect 0~9");
385390
}
@@ -390,9 +395,6 @@ static final long readPositiveLong(final JsonIterator iter, byte c) throws IOExc
390395
iter.head = i;
391396
return ind;
392397
}
393-
if (ind == 0) {
394-
throw iter.reportError("readPositiveLong", "leading zero is invalid for long");
395-
}
396398
int ind3 = IterImplNumber.intDigits[iter.buf[++i]];
397399
if (ind3 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
398400
iter.head = i;

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,7 @@ static final double readPositiveDouble(final JsonIterator iter) throws IOExcepti
591591
static final long readPositiveLong(final JsonIterator iter, byte c) throws IOException {
592592
long ind = IterImplNumber.intDigits[c];
593593
if (ind == 0) {
594+
assertNotLeadingZero(iter);
594595
return 0;
595596
}
596597
if (ind == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
@@ -602,11 +603,27 @@ static final long readPositiveLong(final JsonIterator iter, byte c) throws IOExc
602603
static final int readPositiveInt(final JsonIterator iter, byte c) throws IOException {
603604
int ind = IterImplNumber.intDigits[c];
604605
if (ind == 0) {
606+
assertNotLeadingZero(iter);
605607
return 0;
606608
}
607609
if (ind == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
608610
throw iter.reportError("readPositiveInt", "expect 0~9");
609611
}
610612
return IterImplForStreaming.readIntSlowPath(iter, ind);
611613
}
614+
615+
static void assertNotLeadingZero(JsonIterator iter) throws IOException {
616+
try {
617+
byte nextByte = IterImpl.readByte(iter);
618+
iter.unreadByte();
619+
int ind2 = IterImplNumber.intDigits[nextByte];
620+
if (ind2 == IterImplNumber.INVALID_CHAR_FOR_NUMBER) {
621+
return;
622+
}
623+
throw iter.reportError("readPositiveInt", "leading zero is invalid");
624+
} catch (ArrayIndexOutOfBoundsException e) {
625+
iter.head = iter.tail;
626+
return;
627+
}
628+
}
612629
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ public void test_streaming() throws IOException {
9393
}
9494

9595
public void test_leading_zero() throws IOException {
96+
assertEquals(Integer.valueOf(0), JsonIterator.deserialize("0", int.class));
97+
assertEquals(Long.valueOf(0), JsonIterator.deserialize("0", long.class));
9698
try {
9799
JsonIterator.deserialize("01", int.class);
98100
fail();

0 commit comments

Comments
 (0)