-
Notifications
You must be signed in to change notification settings - Fork 117
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
Comments
One quick note: everything configured via You can change Above may or may not explain what you are seeing but I wanted to point it out first; code needs to use either |
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);
|
I'm using jackson 2.12.5.
When I tried to serialize
OffsetDateTime
without callingObjectMapper.setTimeZone()
, the serializer will use the offset inOffsetDateTime
.But when I tried to serialize
OffsetDateTime
with callingObjectMapper.setTimeZone()
and make them be different time zone, the serializer will use the offset inObjectMapper.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:
Output
The text was updated successfully, but these errors were encountered: