From 23a635d2ab36b91eb89dafc406671e26fd6e7034 Mon Sep 17 00:00:00 2001 From: Dave Moten Date: Thu, 28 Mar 2024 21:54:33 +1100 Subject: [PATCH] add int to double and long to double conversions to StdValueInstantiator #4453 --- .../deser/std/StdValueInstantiator.java | 24 ++++++++++++++++- .../deser/std/StdValueInstantiatorTest.java | 27 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/StdValueInstantiator.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/StdValueInstantiator.java index 09630b36abf..03fc9f6673e 100644 --- a/src/main/java/com/fasterxml/jackson/databind/deser/std/StdValueInstantiator.java +++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/StdValueInstantiator.java @@ -383,7 +383,18 @@ arg, rewrapCtorProblem(ctxt, t0) ); } } - + + if (_fromDoubleCreator != null) { + Object arg = Double.valueOf(value); + try { + return _fromDoubleCreator.call1(arg); + } catch (Exception t0) { + return ctxt.handleInstantiationProblem(_fromDoubleCreator.getDeclaringClass(), + arg, rewrapCtorProblem(ctxt, t0) + ); + } + } + return super.createFromInt(ctxt, value); } @@ -411,6 +422,17 @@ arg, rewrapCtorProblem(ctxt, t0) ); } } + + if (_fromDoubleCreator != null) { + Object arg = Double.valueOf(value); + try { + return _fromDoubleCreator.call1(arg); + } catch (Exception t0) { + return ctxt.handleInstantiationProblem(_fromDoubleCreator.getDeclaringClass(), + arg, rewrapCtorProblem(ctxt, t0) + ); + } + } return super.createFromLong(ctxt, value); } diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/std/StdValueInstantiatorTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/std/StdValueInstantiatorTest.java index 9e9909a113e..8c8f609657b 100644 --- a/src/test/java/com/fasterxml/jackson/databind/deser/std/StdValueInstantiatorTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/deser/std/StdValueInstantiatorTest.java @@ -4,6 +4,10 @@ import org.junit.jupiter.api.Test; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; @@ -23,4 +27,27 @@ public void testDoubleValidation_invalid() { BigDecimal value = BigDecimal.valueOf(Double.MAX_VALUE).add(BigDecimal.valueOf(Double.MAX_VALUE)); assertNull(StdValueInstantiator.tryConvertToDouble(value)); } + + @Test + public void testJsonIntToDouble() throws JsonMappingException, JsonProcessingException { + ObjectMapper m = new ObjectMapper(); + Stuff a = m.readValue("5", Stuff.class); + assertEquals(5, a.value); + } + + @Test + public void testJsonLongToDouble() throws JsonMappingException, JsonProcessingException { + ObjectMapper m = new ObjectMapper(); + long x = 12345678901L; + Stuff a = m.readValue(x + "", Stuff.class); + assertEquals(x, a.value); + } + + public static class Stuff { + public final double value; + + public Stuff(double value) { + this.value = value; + } + } }