FasterXML / jackson-modules-java8

Set of support modules for Java 8 datatypes (Optionals, date/time) and features (parameter names)
Apache License 2.0
400 stars 116 forks source link

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

Open BluYous opened 3 years ago

BluYous commented 3 years ago

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 commented 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).

BluYous commented 3 years ago

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