Skip to content

Commit 636f1b3

Browse files
authored
issue-1770 - big number not returned as DecimalNode (#4191)
1 parent 604cdbd commit 636f1b3

File tree

3 files changed

+47
-31
lines changed

3 files changed

+47
-31
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -755,12 +755,11 @@ protected final JsonNode _fromFloat(JsonParser p, DeserializationContext ctxt,
755755
return _fromBigDecimal(ctxt, nodeFactory, p.getDecimalValue());
756756
}
757757
if (ctxt.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)) {
758-
// 20-May-2016, tatu: As per [databind#1028], need to be careful
759-
// (note: JDK 1.8 would have `Double.isFinite()`)
760-
if (p.isNaN()) {
761-
return nodeFactory.numberNode(p.getDoubleValue());
758+
try {
759+
return _fromBigDecimal(ctxt, nodeFactory, p.getDecimalValue());
760+
} catch (NumberFormatException nfe) {
761+
// fall through - BigDecimal does not support values like NaN
762762
}
763-
return _fromBigDecimal(ctxt, nodeFactory, p.getDecimalValue());
764763
}
765764
if (nt == JsonParser.NumberType.FLOAT) {
766765
return nodeFactory.numberNode(p.getFloatValue());
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.fasterxml.jackson.databind;
2+
3+
import com.fasterxml.jackson.core.JsonFactory;
4+
import com.fasterxml.jackson.core.json.JsonReadFeature;
5+
import com.fasterxml.jackson.databind.json.JsonMapper;
6+
7+
import java.math.BigDecimal;
8+
9+
/**
10+
* Basic tests for {@link JsonNode} implementations that
11+
* contain numeric values.
12+
*/
13+
public class NumberNodes1770Test extends BaseMapTest
14+
{
15+
private final ObjectMapper MAPPER = newJsonMapper();
16+
17+
// For to [databind#1770] (broken due to fix for #1028): `JsonNodeDeserializer`
18+
// would coerce ok but does `parser.isNaN()` check which ends up parsing
19+
// as Double, gets `POSITIVE_INFINITY` and returns `true`: this results in
20+
// `DoubleNode` being used even tho `BigDecimal` could fit the number.
21+
public void testBigDecimalCoercion() throws Exception
22+
{
23+
final String value = "7976931348623157e309";
24+
final JsonNode jsonNode = MAPPER.reader()
25+
.with(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)
26+
.readTree(value);
27+
assertTrue("Expected DecimalNode, got: "+jsonNode.getClass().getName()+": "+jsonNode, jsonNode.isBigDecimal());
28+
assertEquals(new BigDecimal(value), jsonNode.decimalValue());
29+
}
30+
31+
public void testBigDecimalCoercionInf() throws Exception
32+
{
33+
final String value = "+INF";
34+
JsonFactory factory = JsonFactory.builder()
35+
.enable(JsonReadFeature.ALLOW_NON_NUMERIC_NUMBERS)
36+
.build();
37+
final JsonNode jsonNode = new JsonMapper(factory).reader()
38+
.with(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)
39+
.readTree(value);
40+
assertTrue("Expected DoubleNode, got: "+jsonNode.getClass().getName()+": "+jsonNode, jsonNode.isDouble());
41+
assertEquals(Double.POSITIVE_INFINITY, jsonNode.doubleValue());
42+
}
43+
}

src/test/java/com/fasterxml/jackson/failing/NumberNodes1770Test.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)