Skip to content

ZonedDateTime in map keys ignores option to write Zone IDs  #127

@zmumi

Description

@zmumi

As of v2.9.9, the following fails:

class Sample {
    private Map<ZonedDateTime, ZonedDateTime> map;
    private ZonedDateTime dateTime;

    public Sample() {
    }

    public ZonedDateTime getDateTime() {
        return dateTime;
    }

    public void setDateTime(ZonedDateTime dateTime) {
        this.dateTime = dateTime;
    }

    public Map<ZonedDateTime, ZonedDateTime> getMap() {
        return map;
    }

    public void setMap(Map<ZonedDateTime, ZonedDateTime> map) {
        this.map = map;
    }
}

public class JacksonTest {

    @Test
    public void shouldSerializeZoneIdInMapKeys() throws IOException {
        // given
        final ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new Jdk8Module());
        objectMapper.registerModule(new JavaTimeModule());
        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        objectMapper.enable(SerializationFeature.WRITE_DATES_WITH_ZONE_ID);

        final ZonedDateTime datetime = ZonedDateTime.parse("2007-12-03T10:15:30+01:00[Europe/Warsaw]");
        final HashMap<ZonedDateTime, ZonedDateTime> map = new HashMap<>();
        map.put(datetime, datetime);
        final Sample sample = new Sample();
        sample.setMap(map);
        sample.setDateTime(datetime);

        // when
        final String value = objectMapper.writeValueAsString(sample);

        // then
        assertThat(value).isEqualTo("{\"map\":" +
                "{\"2007-12-03T10:15:30+01:00[Europe/Warsaw]\":\"2007-12-03T10:15:30+01:00[Europe/Warsaw]\"}," +
                "\"dateTime\":\"2007-12-03T10:15:30+01:00[Europe/Warsaw]\"" +
                "}");
    }
}

I'd expect that Zone ID is written not only in map keys but also in map values.
Currently SerializationFeature.WRITE_DATES_WITH_ZONE_ID has no impact on map keys.

Sample project:
test.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueIssue that seems easy to resolve and is likely a good candidate for contributors new to project

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions