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

Timestamp keys from ZonedDateTime #128

Closed
zmumi opened this issue Aug 12, 2019 · 2 comments · Fixed by #141
Closed

Timestamp keys from ZonedDateTime #128

zmumi opened this issue Aug 12, 2019 · 2 comments · Fixed by #141
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 shouldSerializeAsTimestampInMapKeys() throws IOException {
        // given
        final ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new Jdk8Module());
        objectMapper.registerModule(new JavaTimeModule());
        objectMapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        objectMapper.enable(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS);

        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\":{\"1196673330.000000000\":1196673330.000000000},\"dateTime\":1196673330.000000000}");
    }
}

Option SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS has no impact on ZonedDateTime. Maybe this option should work in this configuration? (I see in docs it only works for java.util.Date).

@cowtowncoder
Copy link
Member

Sounds like it should work with that type too.

I do not have time to work on this in near term myself, but could help getting a PR merged for 2.10 if you (or anyone else) has time.

@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 Aug 13, 2019
@cowtowncoder
Copy link
Member

Seems reasonable, and I think this would be relatively easy.

If implemented, needs to be in new minor version (since it changes observed behavior, not just simple bug fix): as of now that would be 2.10 (or 3.0, master).

@cowtowncoder cowtowncoder added this to the 2.11.0 milestone Oct 8, 2019
@cowtowncoder cowtowncoder changed the title Timestamp keys from ZonedDateTime Timestamp keys from ZonedDateTime Oct 8, 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