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

ObjectMapper#setTimeZone ignored by jsr-310/datetime types during serialization when using @JsonFormat annotation #175

Closed
ErwanLeroux opened this issue May 19, 2020 · 4 comments
Labels
good first issue Issue that seems easy to resolve and is likely a good candidate for contributors new to project hacktoberfest Issue related to Hactoberfest2020 activities, eligible for additional rewards
Milestone

Comments

@ErwanLeroux
Copy link

ErwanLeroux commented May 19, 2020

Hello, i have an issue regarding the serialization of OffsetDateTime, the timezone i set in the object mapper is not used

With the following code

public class POJO {
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
    private OffsetDateTime offsetDateTimeWithoutTimeZone;
}
public static void setUp() {
    mapperWithTimezone = new ObjectMapper()
            .registerModule(new JavaTimeModule())
            .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
            .setTimeZone(TimeZone.getTimeZone("Europe/Paris"))
            .enable(SerializationFeature.INDENT_OUTPUT);
}
final OffsetDateTime firstJune2020AtNoonOffsetUTC = OffsetDateTime.of(
        LocalDate.of(2020, Month.JUNE, 1),
        LocalTime.NOON,
        ZoneOffset.UTC);

final POJO pojoOffsetUTC = new POJO();
pojoOffsetUTC.setOffsetDateTimeWithoutTimeZone(firstJune2020AtNoonOffsetUTC);
final String strPojoOffSetUTC = mapperWithTimezone.writeValueAsString(pojoOffsetUTC);

I'm expecting that the output is

{"offsetDateTimeWithoutTimeZone" : "2020-06-01T14:00:00.000+02:00"}

but i get

{"offsetDateTimeWithoutTimeZone" : "2020-06-01T12:00:00.000Z"}

The javadoc of com.fasterxml.jackson.annotation.JsonFormat#timezone tells me that the default is the one specified in the objectMapper, but UTC is used instead.

I search a bit in the code, and i didn't see anything regarding the use of ObjectMapper#setTimezone in the method com.fasterxml.jackson.datatype.jsr310.ser.JSR310FormattedSerializerBase#createContextual

When i specify the timezone in the JsonFormat annotation, it work as intended, but i would prefer to have a global setting rather specifying it for each field.

I created a repo with a working code to illustrate my question : https://github.com/ErwanLeroux/jackson-issue

@cowtowncoder
Copy link
Member

Sounds like a bug, thank you for reporting this.
I assume behavior is reproducible with 2.11.0?

@ErwanLeroux
Copy link
Author

yes, i used the last version : implementation group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.11.0'

@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 Jun 10, 2020
@cowtowncoder
Copy link
Member

cowtowncoder commented Jun 24, 2020

Looking at Joda (jackson-datatype-joda) serializer implementation, it seems to use default timezone unless overridden by format annotation (or config override to same effect), see f.ex DateTimeSerializer (via JacksonJodaDateFormat).

But this is missing from jsr-310/datetime module, so it would need to be added.
I can help with this but probably will not have time in near future to tackle it myself.
Note, too, that it is not enough to handle this in createContextual() as that is not called on per-writeValue basis -- context timezone needs to be applied on per-call basis.

@kupci Not sure if this is something you are aware of.

@kupci
Copy link
Member

kupci commented Jun 24, 2020

But this is missing from jsr-310/datetime module, so it would need to be added.
Thanks for the details and ideas to fix, and thanks to Erwan for reporting and the test case. I'll edit the title to reflect the fact this impacts other types besides the OffsetDateTime type.

@kupci kupci changed the title ObjectMapper#setTimeZone ignored during serialization when using JsonFormat annotation on an OffsetDateTime field ObjectMapper#setTimeZone ignored by jsr-310/datetime types during serialization when using JsonFormat annotation Jun 24, 2020
ferenc-csaky added a commit to ferenc-csaky/jackson-modules-java8 that referenced this issue Oct 3, 2020
ferenc-csaky added a commit to ferenc-csaky/jackson-modules-java8 that referenced this issue Oct 5, 2020
@cowtowncoder cowtowncoder changed the title ObjectMapper#setTimeZone ignored by jsr-310/datetime types during serialization when using JsonFormat annotation ObjectMapper#setTimeZone ignored by jsr-310/datetime types during serialization when using @JsonFormat annotation Oct 8, 2020
@cowtowncoder cowtowncoder added the hacktoberfest Issue related to Hactoberfest2020 activities, eligible for additional rewards label Oct 8, 2020
cowtowncoder added a commit that referenced this issue Oct 8, 2020
@cowtowncoder cowtowncoder added this to the 2.12.0 milestone Oct 20, 2020
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 hacktoberfest Issue related to Hactoberfest2020 activities, eligible for additional rewards
Projects
None yet
Development

No branches or pull requests

3 participants