Skip to content

Commit 1192faf

Browse files
committed
#76 fix E at the end of double
1 parent 43477e3 commit 1192faf

3 files changed

Lines changed: 29 additions & 21 deletions

File tree

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

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -415,30 +415,32 @@ static final long readPositiveLong(final JsonIterator iter, byte c) throws IOExc
415415
static final double readPositiveDouble(final JsonIterator iter) throws IOException {
416416
int oldHead = iter.head;
417417
try {
418-
long value = IterImplNumber.readLong(iter); // without the dot
419-
if (iter.head == iter.tail) {
420-
return value;
421-
}
422-
byte c = iter.buf[iter.head];
423-
if (c == '.') {
424-
iter.head++;
425-
int start = iter.head;
426-
c = iter.buf[iter.head++];
427-
long decimalPart = readPositiveLong(iter, c);
428-
int decimalPlaces = iter.head - start;
429-
if (decimalPlaces > 0 && decimalPlaces < IterImplNumber.POW10.length && (iter.head - oldHead) < 10) {
430-
value = value * IterImplNumber.POW10[decimalPlaces] + decimalPart;
431-
return value / (double) IterImplNumber.POW10[decimalPlaces];
418+
try {
419+
long value = IterImplNumber.readLong(iter); // without the dot
420+
if (iter.head == iter.tail) {
421+
return value;
422+
}
423+
byte c = iter.buf[iter.head];
424+
if (c == '.') {
425+
iter.head++;
426+
int start = iter.head;
427+
c = iter.buf[iter.head++];
428+
long decimalPart = readPositiveLong(iter, c);
429+
int decimalPlaces = iter.head - start;
430+
if (decimalPlaces > 0 && decimalPlaces < IterImplNumber.POW10.length && (iter.head - oldHead) < 10) {
431+
value = value * IterImplNumber.POW10[decimalPlaces] + decimalPart;
432+
return value / (double) IterImplNumber.POW10[decimalPlaces];
433+
} else {
434+
iter.head = oldHead;
435+
return IterImplForStreaming.readDoubleSlowPath(iter);
436+
}
432437
} else {
433-
iter.head = oldHead;
434-
return IterImplForStreaming.readDoubleSlowPath(iter);
438+
return value;
435439
}
436-
} else {
437-
if (iter.head < iter.tail && iter.buf[iter.head] == 'e') {
440+
} finally {
441+
if (iter.head < iter.tail && (iter.buf[iter.head] == 'e' || iter.buf[iter.head] == 'E')) {
438442
iter.head = oldHead;
439443
return IterImplForStreaming.readDoubleSlowPath(iter);
440-
} else {
441-
return value;
442444
}
443445
}
444446
} catch (JsonException e) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,8 @@ static int readIntSlowPath(JsonIterator iter, int value) throws IOException {
524524

525525
public static final double readDoubleSlowPath(final JsonIterator iter) throws IOException {
526526
try {
527-
return Double.valueOf(readNumber(iter));
527+
String numberAsStr = readNumber(iter);
528+
return Double.valueOf(numberAsStr);
528529
} catch (NumberFormatException e) {
529530
throw iter.reportError("readDoubleSlowPath", e.toString());
530531
}

src/test/java/com/jsoniter/TestFloat.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public void test_decimal_places() throws IOException {
4444
assertEquals(720368.54775807d, parseDouble("720368.547758075,"), 0.01f);
4545
}
4646

47+
public void test_combination_of_dot_and_exponent() throws IOException {
48+
double v = JsonIterator.parse("8.37377E9").readFloat();
49+
assertEquals(Double.valueOf("8.37377E9"), v, 1000d);
50+
}
51+
4752
@Category(StreamingCategory.class)
4853
public void test_streaming() throws IOException {
4954
isStreaming = true;

0 commit comments

Comments
 (0)