Skip to content

@JsonDeserialize(converter = ...) does not work with Records #3297

@gavlyukovskiy

Description

@gavlyukovskiy

Describe the bug
@JsonDeserialize(converter = ...) on Record's fields isn't picked up by Jackson during deserialization. I'm using this converter to parse time without zone information into Instant field.

Version information
Jackson: 2.13, 2.13.1-SNAPSHOT
JDK: 17

To Reproduce

public class JacksonTestCase {

    public static void main(String[] args) throws JsonProcessingException {
        var objectMapper = new ObjectMapper().registerModule(new JavaTimeModule());

        objectMapper.readValue("""
                               { "value": "test", "time": "2021-10-07T22:47:15" }
                                """, RecordEntity.class);
    }

    static record RecordEntity(
            String value,
            @JsonDeserialize(converter = InstantWithoutZoneConverter.class)
            Instant time
    ) {
    }

    static class InstantWithoutZoneConverter implements Converter<LocalDateTime, Instant> {
        @Override
        public Instant convert(LocalDateTime value) {
            return value.toInstant(ZoneOffset.UTC);
        }

        @Override
        public JavaType getInputType(TypeFactory typeFactory) {
            return typeFactory.constructType(LocalDateTime.class);
        }

        @Override
        public JavaType getOutputType(TypeFactory typeFactory) {
            return typeFactory.constructType(Instant.class);
        }
    }
}

Additional context
It looks similar to #2974 and workaround to add @JsonProperty("time") works here as well:

    static record RecordEntity(
            String value,
            @JsonProperty("time")
            @JsonDeserialize(converter = InstantWithoutZoneConverter.class)
            Instant time
    ) {
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    RecordIssue related to JDK17 java.lang.Record support

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions