Skip to content

Allow JSON Integer to deserialize into a single-arg constructor of parameter type double #4453

@davidmoten

Description

@davidmoten

Is your feature request related to a problem? Please describe.

This test fails with a MismatchedInputException:

    @Test
    public void testJsonValueIntToDouble() throws JsonMappingException, JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        Stuff a = mapper.readValue("5", Stuff.class);
        assertEquals(5, a.value);
    }
    
    public static final class Stuff {
        public final double value;

        public Stuff(double value) {
            this.value = value;
        }
    }

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 type int or long, 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 an ObjectMapper 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    2.18Issues planned at 2.18 or laterpr-welcomeIssue for which progress most likely if someone submits a Pull Request

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions