Skip to content

Commit c809c0c

Browse files
committed
Fix #1028
1 parent 073fc19 commit c809c0c

File tree

5 files changed

+51
-2
lines changed

5 files changed

+51
-2
lines changed

release-notes/CREDITS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,3 +478,7 @@ Maarten Billemont (lhunath@github)
478478
* Suggested #1184: Allow overriding of `transient` with explicit inclusion with `@JsonProperty`
479479
(2.8.0)
480480

481+
Vladimir Kulev (lightoze@github)
482+
* Reported #1028: Ignore USE_BIG_DECIMAL_FOR_FLOATS for NaN/Infinity
483+
(2.8.0)
484+

release-notes/VERSION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Project: jackson-databind
1717
(contributed by mkokho@github)
1818
#1017: Add new mapping exception type ('InvalidTypeIdException') for subtype resolution errors
1919
(suggested by natnan@github)
20+
#1028: Ignore USE_BIG_DECIMAL_FOR_FLOATS for NaN/Infinity
21+
(reported by Vladimir K, lightoze@github)
2022
#1082: Can not use static Creator factory methods for `Enum`s, with JsonCreator.Mode.PROPERTIES
2123
(contributed by Lokesh K)
2224
#1084: Change `TypeDeserializerBase` to take `JavaType` for `defaultImpl`, NOT `Class`

src/main/java/com/fasterxml/jackson/databind/deser/std/JsonNodeDeserializer.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.fasterxml.jackson.databind.deser.std;
22

33
import java.io.IOException;
4+
import java.math.BigDecimal;
45

56
import com.fasterxml.jackson.core.*;
67
import com.fasterxml.jackson.databind.*;
@@ -364,10 +365,18 @@ protected final JsonNode _fromFloat(JsonParser p, DeserializationContext ctxt,
364365
final JsonNodeFactory nodeFactory) throws IOException
365366
{
366367
JsonParser.NumberType nt = p.getNumberType();
367-
if (nt == JsonParser.NumberType.BIG_DECIMAL
368-
|| ctxt.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)) {
368+
if (nt == JsonParser.NumberType.BIG_DECIMAL) {
369369
return nodeFactory.numberNode(p.getDecimalValue());
370370
}
371+
if (ctxt.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)) {
372+
// 20-May-2016, tatu: As per [databind#1028], need to be careful
373+
// (note: JDK 1.8 would have `Double.isFinite()`)
374+
double d = p.getDoubleValue();
375+
if (Double.isInfinite(d) || Double.isNaN(d)) {
376+
return nodeFactory.numberNode(d);
377+
}
378+
return nodeFactory.numberNode(BigDecimal.valueOf(d));
379+
}
371380
return nodeFactory.numberNode(p.getDoubleValue());
372381
}
373382

src/test/java/com/fasterxml/jackson/databind/BaseMapTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ protected static class LongWrapper {
5252
public LongWrapper() { }
5353
public LongWrapper(long value) { l = value; }
5454
}
55+
56+
protected static class DoubleWrapper {
57+
public double d;
58+
59+
public DoubleWrapper() { }
60+
public DoubleWrapper(double value) { d = value; }
61+
}
5562

5663
/**
5764
* Simple wrapper around String type, usually to test value
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fasterxml.jackson.databind.node;
2+
3+
import com.fasterxml.jackson.databind.*;
4+
5+
public class NotANumberConversionTest extends BaseMapTest
6+
{
7+
public void testBigDecimalWithNaN() throws Exception
8+
{
9+
ObjectMapper m = new ObjectMapper();
10+
m.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
11+
12+
JsonNode tree = m.valueToTree(new DoubleWrapper(Double.NaN));
13+
assertNotNull(tree);
14+
String json = m.writeValueAsString(tree);
15+
assertNotNull(json);
16+
17+
tree = m.valueToTree(new DoubleWrapper(Double.NEGATIVE_INFINITY));
18+
assertNotNull(tree);
19+
json = m.writeValueAsString(tree);
20+
assertNotNull(json);
21+
22+
tree = m.valueToTree(new DoubleWrapper(Double.POSITIVE_INFINITY));
23+
assertNotNull(tree);
24+
json = m.writeValueAsString(tree);
25+
assertNotNull(json);
26+
}
27+
}

0 commit comments

Comments
 (0)