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

Serialize OffsetDateTime is always using the time zone in ObjectMapper if I set it in advance. #228

Open
BluYous opened this issue Oct 8, 2021 · 2 comments

Comments

@BluYous
Copy link

BluYous commented Oct 8, 2021

I'm using jackson 2.12.5.
When I tried to serialize OffsetDateTime without calling ObjectMapper.setTimeZone(), the serializer will use the offset in OffsetDateTime.
But when I tried to serialize OffsetDateTime with calling ObjectMapper.setTimeZone() and make them be different time zone, the serializer will use the offset in ObjectMapper.setTimeZone().

That's not my expected. I suppose that use object's timezone first even if different one is set in ObjectMapper.
Not sure if it's a bug or a feature.

Here is my demo:

System.out.println("default time zone = " + TimeZone.getDefault().getID());
ObjectMapper objectMapper = new ObjectMapper()
        .registerModule(new JavaTimeModule())
        .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
        .disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)
        .enable(SerializationFeature.WRITE_DATES_WITH_ZONE_ID);
final OffsetDateTime expected = OffsetDateTime.now(ZoneId.of("US/Pacific"));
String actualStringWithoutSetTimeZone = objectMapper.writeValueAsString(expected);
System.out.println("actualString without setting time zone: " + actualStringWithoutSetTimeZone);
OffsetDateTime actualWithoutSetTimeZone = objectMapper.readValue(actualStringWithoutSetTimeZone, OffsetDateTime.class);
Assertions.assertEquals(expected, actualWithoutSetTimeZone);

// set time zone
objectMapper.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
String actualString = objectMapper.writeValueAsString(expected);
System.out.println("actualString after setting time zone to Asia/Shanghai: " + actualString);
OffsetDateTime actual = objectMapper.readValue(actualString, OffsetDateTime.class);
Assertions.assertEquals(expected, actual);

Output

default time zone = Asia/Shanghai
actualString without setting time zone: "2021-10-07T02:08:29.1089765-07:00"
actualString after setting time zone to Asia/Shanghai: "2021-10-07T17:08:29.1089765+08:00"

org.opentest4j.AssertionFailedError: 
Expected :2021-10-07T02:08:29.108976500-07:00
Actual   :2021-10-07T17:08:29.108976500+08:00
@cowtowncoder
Copy link
Member

One quick note: everything configured via ObjectMapper MUST be configured BEFORE ANY use. Configuration SHOULD NOT be changed after construction and use; any changes may or may not affect operations.
Jackson 3.0 will change it so that various setters do not exist (construction is done using Builder), but with 2.x this is the pattern to follow.

You can change TimeZone setting, however, by creating ObjectWriter instances; they have various with() methods for creating differently configured writers to use.

Above may or may not explain what you are seeing but I wanted to point it out first; code needs to use either ObjectWriter for reconfiguration, or newly construct mappers (but doing that is much more expensive for performance).

@BluYous
Copy link
Author

BluYous commented Oct 9, 2021

Thanks. I rewrite the demo but the issue still exists.

System.out.println("default time zone = " + TimeZone.getDefault().getID());
ObjectMapper objectMapper = new ObjectMapper()
        .registerModule(new JavaTimeModule())
        .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
        .disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)
        .enable(SerializationFeature.WRITE_DATES_WITH_ZONE_ID);
final OffsetDateTime expected = OffsetDateTime.now(ZoneId.of("US/Pacific"));
String actualStringWithoutSetTimeZone = objectMapper.writeValueAsString(expected);
System.out.println("actualString without setting time zone: " + actualStringWithoutSetTimeZone);
OffsetDateTime actualWithoutSetTimeZone = objectMapper.readValue(actualStringWithoutSetTimeZone, OffsetDateTime.class);
Assertions.assertEquals(expected, actualWithoutSetTimeZone);

// new ObjectMapper and set time zone
objectMapper = new ObjectMapper()
        .registerModule(new JavaTimeModule())
        .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
        .disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)
        .enable(SerializationFeature.WRITE_DATES_WITH_ZONE_ID).
        setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
String actualString = objectMapper.writeValueAsString(expected);
System.out.println("actualString with setting time zone to Asia/Shanghai: " + actualString);
OffsetDateTime actual = objectMapper.readValue(actualString, OffsetDateTime.class);
Assertions.assertEquals(expected, actual);
default time zone = Asia/Shanghai
actualString without setting time zone: "2021-10-09T02:57:37.1749122-07:00"
actualString with setting time zone to Asia/Shanghai: "2021-10-09T17:57:37.1749122+08:00"

org.opentest4j.AssertionFailedError: 
Expected :2021-10-09T02:57:37.174912200-07:00
Actual   :2021-10-09T17:57:37.174912200+08:00

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants