Open BluYous opened 3 years ago
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).
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
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 serializeOffsetDateTime
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