Skip to content
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

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

Closed
zmumi opened this issue Aug 12, 2019 · 4 comments · Fixed by #140
Closed

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

zmumi opened this issue Aug 12, 2019 · 4 comments · Fixed by #140
Labels
good first issue Issue that seems easy to resolve and is likely a good candidate for contributors new to project
Milestone

Comments

@zmumi
Copy link

zmumi commented Aug 12, 2019

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

@cowtowncoder
Copy link
Member

Key serializers/deserializers are different from value (de)serializers which probably explains discrepancy.

But just to make sure: which part (or both) is failing; key or value serializer?

@cowtowncoder cowtowncoder added 2.10 good first issue Issue that seems easy to resolve and is likely a good candidate for contributors new to project labels Aug 12, 2019
@cowtowncoder
Copy link
Member

Tagging with easy just to denote issues where new contributors could probably help most.

@zmumi
Copy link
Author

zmumi commented Aug 14, 2019

But just to make sure: which part (or both) is failing; key or value serializer?

Key serializer.

@cowtowncoder
Copy link
Member

@zmumi Thank you, makes sense, but just wanted to double-check.

@cowtowncoder cowtowncoder removed the good first issue Issue that seems easy to resolve and is likely a good candidate for contributors new to project label Oct 6, 2019
@cowtowncoder cowtowncoder added this to the 2.10.1 milestone Oct 6, 2019
@cowtowncoder cowtowncoder added the good first issue Issue that seems easy to resolve and is likely a good candidate for contributors new to project label Oct 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Issue that seems easy to resolve and is likely a good candidate for contributors new to project
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants