Skip to content

Commit d4a2092

Browse files
committed
Fix #1315
1 parent 61a829a commit d4a2092

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

release-notes/CREDITS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,3 +526,6 @@ Chris Jester-Young (cky@github)
526526
* Contributed #1335: Unconditionally call `TypeIdResolver.getDescForKnownTypeIds`
527527
(2.8.2)
528528

529+
Andrew Snare (asnare@github)
530+
* Reported #1315: Binding numeric values can BigDecimal lose precision
531+
(2.8.2)

release-notes/VERSION

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Project: jackson-databind
66

77
2.8.2 (not yet released)
88

9+
#1315: Binding numeric values can BigDecimal lose precision
10+
(reported by Andrew S)
11+
#1327: Class level `@JsonInclude(JsonInclude.Include.NON_EMPTY)` is ignored
12+
(reported by elruwen@github)
913
#1335: Unconditionally call `TypeIdResolver.getDescForKnownTypeIds`
1014
(contributed by Chris J-Y)
1115

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

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

33
import java.io.IOException;
4-
import java.math.BigDecimal;
54

65
import com.fasterxml.jackson.core.*;
76
import com.fasterxml.jackson.databind.*;
@@ -372,11 +371,14 @@ protected final JsonNode _fromFloat(JsonParser p, DeserializationContext ctxt,
372371
if (ctxt.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)) {
373372
// 20-May-2016, tatu: As per [databind#1028], need to be careful
374373
// (note: JDK 1.8 would have `Double.isFinite()`)
374+
// 21-Aug-2016, tatu: Not optimal, really, because this may result in
375+
// value getting parsed twice. But has to do for now, to resolve
376+
// [databind#1315]
375377
double d = p.getDoubleValue();
376378
if (Double.isInfinite(d) || Double.isNaN(d)) {
377379
return nodeFactory.numberNode(d);
378380
}
379-
return nodeFactory.numberNode(BigDecimal.valueOf(d));
381+
return nodeFactory.numberNode(p.getDecimalValue());
380382
}
381383
return nodeFactory.numberNode(p.getDoubleValue());
382384
}

src/test/java/com/fasterxml/jackson/databind/node/NotANumberConversionTest.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package com.fasterxml.jackson.databind.node;
22

3+
import java.math.BigDecimal;
4+
35
import com.fasterxml.jackson.databind.*;
46

57
public class NotANumberConversionTest extends BaseMapTest
68
{
7-
public void testBigDecimalWithNaN() throws Exception
9+
private final ObjectMapper m = new ObjectMapper();
810
{
9-
ObjectMapper m = new ObjectMapper();
1011
m.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
12+
}
1113

14+
public void testBigDecimalWithNaN() throws Exception
15+
{
1216
JsonNode tree = m.valueToTree(new DoubleWrapper(Double.NaN));
1317
assertNotNull(tree);
1418
String json = m.writeValueAsString(tree);
@@ -24,4 +28,14 @@ public void testBigDecimalWithNaN() throws Exception
2428
json = m.writeValueAsString(tree);
2529
assertNotNull(json);
2630
}
31+
32+
// for [databind#1315]: no accidental coercion to DoubleNode
33+
public void testBigDecimalWithoutNaN() throws Exception
34+
{
35+
BigDecimal input = new BigDecimal(Double.MIN_VALUE).divide(new BigDecimal(10L));
36+
JsonNode tree = m.readTree(input.toString());
37+
assertTrue(tree.isBigDecimal());
38+
BigDecimal output = tree.decimalValue();
39+
assertEquals(input, output);
40+
}
2741
}

0 commit comments

Comments
 (0)