Skip to content

Commit 98777f6

Browse files
committed
Minor tweaking wrt #2978
1 parent c1e8b70 commit 98777f6

File tree

3 files changed

+35
-24
lines changed

3 files changed

+35
-24
lines changed

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -463,13 +463,15 @@ value, rewrapCtorProblem(ctxt, t)
463463
// with less precision as doubles. When written to a TokenBuffer for polymorphic
464464
// deserialization the most specific type is recorded, though a less precise
465465
// floating point value may be needed.
466-
if(_fromDoubleCreator != null && canConvertToDouble(value)) {
467-
Object arg = value.doubleValue();
468-
try {
469-
return _fromDoubleCreator.call1(arg);
470-
} catch (Throwable t0) {
471-
return ctxt.handleInstantiationProblem(_fromDoubleCreator.getDeclaringClass(),
472-
arg, rewrapCtorProblem(ctxt, t0));
466+
if (_fromDoubleCreator != null) {
467+
Double dbl = tryConvertToDouble(value);
468+
if (dbl != null) {
469+
try {
470+
return _fromDoubleCreator.call1(dbl);
471+
} catch (Throwable t0) {
472+
return ctxt.handleInstantiationProblem(_fromDoubleCreator.getDeclaringClass(),
473+
dbl, rewrapCtorProblem(ctxt, t0));
474+
}
473475
}
474476
}
475477

@@ -478,9 +480,11 @@ value, rewrapCtorProblem(ctxt, t)
478480

479481
// BigDecimal cannot represent special values NaN, positive infinity, or negative infinity.
480482
// When the value cannot be represented as a double, positive or negative infinity is returned.
481-
static boolean canConvertToDouble(BigDecimal value) {
483+
//
484+
// @since 2.12.1
485+
static Double tryConvertToDouble(BigDecimal value) {
482486
double doubleValue = value.doubleValue();
483-
return !Double.isInfinite(doubleValue);
487+
return Double.isInfinite(doubleValue) ? null : doubleValue;
484488
}
485489

486490
@Override

src/test/java/com/fasterxml/jackson/databind/deser/std/StdValueInstantiatorTest.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44

55
import java.math.BigDecimal;
66

7-
public class StdValueInstantiatorTest extends BaseMapTest {
8-
7+
// [databind#2978]
8+
public class StdValueInstantiatorTest extends BaseMapTest
9+
{
910
public void testDoubleValidation_valid() {
10-
assertTrue(StdValueInstantiator.canConvertToDouble(BigDecimal.ZERO));
11-
assertTrue(StdValueInstantiator.canConvertToDouble(BigDecimal.ONE));
12-
assertTrue(StdValueInstantiator.canConvertToDouble(BigDecimal.TEN));
13-
assertTrue(StdValueInstantiator.canConvertToDouble(BigDecimal.valueOf(-1.5D)));
11+
assertEquals(0d, StdValueInstantiator.tryConvertToDouble(BigDecimal.ZERO));
12+
assertEquals(1d, StdValueInstantiator.tryConvertToDouble(BigDecimal.ONE));
13+
assertEquals(10d, StdValueInstantiator.tryConvertToDouble(BigDecimal.TEN));
14+
assertEquals(-1.5d, StdValueInstantiator.tryConvertToDouble(BigDecimal.valueOf(-1.5d)));
1415
}
1516

1617
public void testDoubleValidation_invalid() {
1718
BigDecimal value = BigDecimal.valueOf(Double.MAX_VALUE).add(BigDecimal.valueOf(Double.MAX_VALUE));
18-
assertFalse(StdValueInstantiator.canConvertToDouble(value));
19+
assertNull(StdValueInstantiator.tryConvertToDouble(value));
1920
}
20-
}
21+
}

src/test/java/com/fasterxml/jackson/databind/jsontype/TestDoubleJsonCreator.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@
1212
import com.fasterxml.jackson.annotation.JsonValue;
1313
import com.fasterxml.jackson.core.type.TypeReference;
1414
import com.fasterxml.jackson.databind.BaseMapTest;
15+
import com.fasterxml.jackson.databind.ObjectMapper;
1516

1617
import java.io.IOException;
1718
import java.util.HashMap;
1819
import java.util.Map;
1920
import java.util.Objects;
2021

21-
public class TestDoubleJsonCreator extends BaseMapTest {
22-
23-
public static final class UnionExample {
22+
// For [databind#2978]
23+
public class TestDoubleJsonCreator extends BaseMapTest
24+
{
25+
static final class UnionExample {
2426
private final Base value;
2527

2628
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
@@ -78,7 +80,7 @@ private static final class DoubleWrapper implements Base {
7880
private final AliasDouble value;
7981

8082
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
81-
private DoubleWrapper(@JsonSetter("double") AliasDouble value) {
83+
DoubleWrapper(@JsonSetter("double") AliasDouble value) {
8284
Objects.requireNonNull(value, "double cannot be null");
8385
this.value = value;
8486
}
@@ -176,7 +178,7 @@ public String toString() {
176178
}
177179
}
178180

179-
public static final class AliasDouble {
181+
static final class AliasDouble {
180182
private final double value;
181183

182184
private AliasDouble(double value) {
@@ -211,17 +213,21 @@ public static AliasDouble of(double value) {
211213
}
212214
}
213215

216+
private final ObjectMapper MAPPER = newJsonMapper();
217+
218+
// [databind#2978]
214219
public void testDeserializationTypeFieldLast() throws IOException {
215220
UnionExample expected = UnionExample.double_(AliasDouble.of(2.0D));
216-
UnionExample actual = objectMapper().readValue(
221+
UnionExample actual = MAPPER.readValue(
217222
a2q("{'double': 2.0,'type':'double'}"),
218223
new TypeReference<UnionExample>() {});
219224
assertEquals(expected, actual);
220225
}
221226

227+
// [databind#2978]
222228
public void testDeserializationTypeFieldFirst() throws IOException {
223229
UnionExample expected = UnionExample.double_(AliasDouble.of(2.0D));
224-
UnionExample actual = objectMapper().readValue(
230+
UnionExample actual = MAPPER.readValue(
225231
a2q("{'type':'double','double': 2.0}"),
226232
new TypeReference<UnionExample>() {});
227233
assertEquals(expected, actual);

0 commit comments

Comments
 (0)