-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow JSON Integer to deserialize into a single-arg constructor of parameter type double
#4453
Comments
Hello, @davidmoten, would this solution help your case? Note that I modified @Test
public void testJsonValueIntToDouble() throws JsonMappingException, JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(cusstomModule());
Stuff a = mapper.readValue("5", Stuff.class);
assertEquals(5.0, a.value, 0.0);
}
private Module cusstomModule() {
return new SimpleModule()
.addValueInstantiator(Stuff.class, new ValueInstantiator() {
@Override
public Object createFromInt(DeserializationContext ctxt, int value) throws IOException {
return new Stuff(Double.valueOf(value));
}
});
}
public static final class Stuff {
public final double value;
public Stuff(double value) {
this.value = value;
}
} |
Thanks @JooHyukKim, I'm aware of that sort of fix but I'm after something more general. In effect I'd like to add use of jackson-databind/src/main/java/com/fasterxml/jackson/databind/deser/std/StdValueInstantiator.java Lines 353 to 388 in 199d3ac
|
Sounds reasonable... although in case of So possibly would need a new PR welcome! |
Not loss of range but can lose precision, yep. If a user types something as
Thanks, yep I'll sort out a PR. |
@cowtowncoder, when I use a JsonProperty there is no deserialization problem. On a consistency basis shouldn't we just fix StdValueInstantiator? For example, this test passes: @Test
public void testJsonValueIntToDoubleMulti() throws JsonMappingException, JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
Stuff2 a = mapper.readValue("{\"value\":5}", Stuff2.class);
assertEquals(5, a.value);
}
public static final class Stuff2 {
public final double value;
public Stuff2(@JsonProperty("value") double value) {
this.value = value;
}
} |
Strange, wonder if it is intended behavior 🤔 |
@davidmoten good point, was thinking about that after writing what I did. |
I've had a brief look. I'd modify I've noticed that JsonProperty annotated values will deserialize to a |
Ideally separate one I think, if we are talking about functionality changes? (Javadoc/comment changes are fine to be tagged on into one PR) |
It seems like we almost had it done via davidmoten@23a635d. LMK if you need any help with anything, @davidmoten!😆 |
Hi, yep I'll get on to that this week. Extended Easter hols have prevented me. |
PR up for review #4474 |
double
Thanks for review and merge. I did some tests and noticed that @JsonProperty will happily deserialize integer JSON values to float, short and byte. For consistency we should do the same with single-arg constructors too. Would you like me to prepare a PR? |
Let's add ones to |
Cool, makes sense. When I get the float change approved right then can apply the same pattern to other types. I'll do some more testing on existing JsonProperty deser behaviour too, see what happens when range exceeded (throws or not). |
Sounds good! I think some accessors from |
Re these two issues:
I think the first issue is something that a user should wear if they choose a float for a high precision JSON decimal value for instance. So IMO this is acceptable loss. The second case varies a bit in effect across byte/short/int/long and float/double but is where throwing might be reasonable with the exception where underflow (to 0) is just like a loss of precision. Anyway, I'll first document the JsonProperty deser behaviour for these, see where we end up and we can discuss. |
@davidmoten Agreed. |
Is your feature request related to a problem? Please describe.
This test fails with a
MismatchedInputException
:If the value being deserialized was
5.0
then it works.5
does not work. I'm not interested in workarounds like adding extra constructors of typeint
orlong
, this should just work.Normally there would be a
@JsonValue
annotation on the field for serialization but that's not relevant for this issue so I've left it out.I'm finding this hideous to solve in terms of overriding Jackson's default behaviour. Ideally Jackson would do it for me as default behaviour (or configured).
I'd like to override the default
ValueInstantiator
used by anObjectMapper
but that does not seem possible. Is it?Can we change Jackson's default behaviour to support this use case? Or support a new
DeserializationFeature
for this?Describe the solution you'd like
see above
Usage example
see above
Additional context
No response
The text was updated successfully, but these errors were encountered: