jackson-databind
jackson-databind copied to clipboard
`InvalidFormatException` despite `ACCEPT_FLOAT_AS_INT` if `Float` is in `String`
Describe the bug
DeserializationFeature.ACCEPT_FLOAT_AS_INT does not work when floating point value is in a JSON string.
Version information 2.13.2
To Reproduce
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Foo {
static class C {
public int x;
}
public static void main(String[] args) throws JsonMappingException, JsonProcessingException {
C a = new ObjectMapper()
.enable(DeserializationFeature.ACCEPT_FLOAT_AS_INT)
.readValue("{ \"x\": 123.0 }", C.class) // good
;
System.out.println(a.x);
C b = new ObjectMapper()
.enable(DeserializationFeature.ACCEPT_FLOAT_AS_INT)
.readValue("{ \"x\": \"123.0\" }", C.class) // bad
;
System.out.println(b.x);
}
}
Expected behavior Expected output:
123
123
Additional context Actual output:
Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `int` from String "123.0": not a valid `int` value
Hmmh.
First of all, thank you for reporting this issue.
Not quite sure how to feel about this as it requires two coercions -- from String to Float; from Float to Int -- so not sure if this should work at all; and if so, what kind of settings should be required. (in this case I could argue that conversion is not from Float to Int but from String that is not Int to Int). My concern is that such chains of conversions/coercions can get very elaborate.
On short term it may make sense to add a custom deserializer if this functionality is needed; fix, if any, would take a while and would likely be in a new minor version.