Skip to content

Commit c1db5fb

Browse files
committed
fix #176 parse infinity
1 parent 9768493 commit c1db5fb

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

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

+10
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,16 @@ static int readIntSlowPath(final JsonIterator iter, int value) throws IOExceptio
540540
public static final double readDoubleSlowPath(final JsonIterator iter) throws IOException {
541541
try {
542542
numberChars numberChars = readNumber(iter);
543+
if (numberChars.charsLength == 0 && iter.whatIsNext() == ValueType.STRING) {
544+
String possibleInf = iter.readString();
545+
if ("infinity".equals(possibleInf)) {
546+
return Double.POSITIVE_INFINITY;
547+
}
548+
if ("-infinity".equals(possibleInf)) {
549+
return Double.NEGATIVE_INFINITY;
550+
}
551+
throw iter.reportError("readDoubleSlowPath", "expect number but found string: " + possibleInf);
552+
}
543553
return Double.valueOf(new String(numberChars.chars, 0, numberChars.charsLength));
544554
} catch (NumberFormatException e) {
545555
throw iter.reportError("readDoubleSlowPath", e.toString());

‎src/main/java/com/jsoniter/output/StreamImplNumber.java

+16
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,18 @@ public static final void writeLong(final JsonStream stream, long value) throws I
212212

213213
public static final void writeFloat(JsonStream stream, float val) throws IOException {
214214
if (val < 0) {
215+
if (val == Float.NEGATIVE_INFINITY) {
216+
stream.writeVal("-Infinity");
217+
return;
218+
}
215219
stream.write('-');
216220
val = -val;
217221
}
218222
if (val > 0x4ffffff) {
223+
if (val == Float.POSITIVE_INFINITY) {
224+
stream.writeVal("Infinity");
225+
return;
226+
}
219227
stream.writeRaw(Float.toString(val));
220228
return;
221229
}
@@ -240,10 +248,18 @@ public static final void writeFloat(JsonStream stream, float val) throws IOExcep
240248

241249
public static final void writeDouble(JsonStream stream, double val) throws IOException {
242250
if (val < 0) {
251+
if (val == Double.NEGATIVE_INFINITY) {
252+
stream.writeVal("-Infinity");
253+
return;
254+
}
243255
val = -val;
244256
stream.write('-');
245257
}
246258
if (val > 0x4ffffff) {
259+
if (val == Double.POSITIVE_INFINITY) {
260+
stream.writeVal("Infinity");
261+
return;
262+
}
247263
stream.writeRaw(Double.toString(val));
248264
return;
249265
}

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.jsoniter;
22

3-
import com.fasterxml.jackson.databind.ObjectMapper;
43
import junit.framework.TestCase;
54
import org.junit.experimental.categories.Category;
65

@@ -90,4 +89,12 @@ public void testChooseDouble() {
9089
number = JsonIterator.deserialize("1.0", Object.class);
9190
assertEquals(1.0, number);
9291
}
92+
93+
public void testInfinity() {
94+
assertTrue(JsonIterator.deserialize("\"-infinity\"", Double.class) == Double.NEGATIVE_INFINITY);
95+
assertTrue(JsonIterator.deserialize("\"-infinity\"", Float.class) == Float.NEGATIVE_INFINITY);
96+
assertTrue(JsonIterator.deserialize("\"infinity\"", Double.class) == Double.POSITIVE_INFINITY);
97+
assertTrue(JsonIterator.deserialize("\"infinity\"", Float.class) == Float.POSITIVE_INFINITY);
98+
}
99+
93100
}

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

+6
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,10 @@ public class TestFloat extends TestCase {
88
public void testBigDecimal() {
99
assertEquals("100.1", JsonStream.serialize(new BigDecimal("100.1")));
1010
}
11+
public void test_infinity() {
12+
assertEquals("\"Infinity\"", JsonStream.serialize(Double.POSITIVE_INFINITY));
13+
assertEquals("\"Infinity\"", JsonStream.serialize(Float.POSITIVE_INFINITY));
14+
assertEquals("\"-Infinity\"", JsonStream.serialize(Double.NEGATIVE_INFINITY));
15+
assertEquals("\"-Infinity\"", JsonStream.serialize(Float.NEGATIVE_INFINITY));
16+
}
1117
}

0 commit comments

Comments
 (0)