-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package com.fasterxml.jackson.databind.deser.std; | ||
|
||
import java.io.IOException; | ||
import java.math.BigDecimal; | ||
|
||
import com.fasterxml.jackson.core.*; | ||
import com.fasterxml.jackson.databind.*; | ||
|
@@ -364,10 +365,18 @@ protected final JsonNode _fromFloat(JsonParser p, DeserializationContext ctxt, | |
final JsonNodeFactory nodeFactory) throws IOException | ||
{ | ||
JsonParser.NumberType nt = p.getNumberType(); | ||
if (nt == JsonParser.NumberType.BIG_DECIMAL | ||
|| ctxt.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)) { | ||
if (nt == JsonParser.NumberType.BIG_DECIMAL) { | ||
return nodeFactory.numberNode(p.getDecimalValue()); | ||
} | ||
if (ctxt.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)) { | ||
// 20-May-2016, tatu: As per [databind#1028], need to be careful | ||
// (note: JDK 1.8 would have `Double.isFinite()`) | ||
double d = p.getDoubleValue(); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
cowtowncoder
Author
Member
|
||
if (Double.isInfinite(d) || Double.isNaN(d)) { | ||
return nodeFactory.numberNode(d); | ||
} | ||
return nodeFactory.numberNode(BigDecimal.valueOf(d)); | ||
} | ||
return nodeFactory.numberNode(p.getDoubleValue()); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.fasterxml.jackson.databind.node; | ||
|
||
import com.fasterxml.jackson.databind.*; | ||
|
||
public class NotANumberConversionTest extends BaseMapTest | ||
{ | ||
public void testBigDecimalWithNaN() throws Exception | ||
{ | ||
ObjectMapper m = new ObjectMapper(); | ||
m.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS); | ||
|
||
JsonNode tree = m.valueToTree(new DoubleWrapper(Double.NaN)); | ||
assertNotNull(tree); | ||
String json = m.writeValueAsString(tree); | ||
assertNotNull(json); | ||
|
||
tree = m.valueToTree(new DoubleWrapper(Double.NEGATIVE_INFINITY)); | ||
assertNotNull(tree); | ||
json = m.writeValueAsString(tree); | ||
assertNotNull(json); | ||
|
||
tree = m.valueToTree(new DoubleWrapper(Double.POSITIVE_INFINITY)); | ||
assertNotNull(tree); | ||
json = m.writeValueAsString(tree); | ||
assertNotNull(json); | ||
} | ||
} |
This line introduces a bug, I believe. In particular, it causes a loss of precision that you're probably trying to avoid if you're using
BigDecimal
.