ZonedDateTimeDeserializer is missing in jackson jsr310

ε祈祈猫儿з 提交于 2019-12-05 09:09:41

The 2 values 2017-01-01T01:01:01.000000001Z and 2017-01-01T01:01:01.000000001Z[UTC] actually represent the same instant, so they are equivalent and can be used without a problem (at least there should be no problems as they represent the same instant).

The only detail is that Jackson, for some reason, sets the ZoneId value to "UTC" when deserializing, which is redundant in this case (the Z already tells that the offset is "UTC"). But it shouldn't affect the date value itself.


A very simple way to get rid of this [UTC] part is to convert this object to OffsetDateTime (so it keeps the Z offset and don't use the [UTC] zone) and then back again to ZonedDateTime:

ZonedDateTime z = // object with 2017-01-01T01:01:01.000000001Z[UTC] value
z = z.toOffsetDateTime().toZonedDateTime();
System.out.println(z); // 2017-01-01T01:01:01.000000001Z

After that, the value of z variable will be 2017-01-01T01:01:01.000000001Z (without the [UTC] part).

But of course this is not ideal as you'd have to do it manually for all dates. A better approach is to write a custom deserializer (by extending com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer) that don't set the timezone when it's UTC:

public class CustomZonedDateTimeDeserializer extends InstantDeserializer<ZonedDateTime> {
    public CustomZonedDateTimeDeserializer() {
        // most parameters are the same used by InstantDeserializer
        super(ZonedDateTime.class,
              DateTimeFormatter.ISO_ZONED_DATE_TIME,
              ZonedDateTime::from,
              // when zone id is "UTC", use the ZoneOffset.UTC constant instead of the zoneId object
              a -> ZonedDateTime.ofInstant(Instant.ofEpochMilli(a.value), a.zoneId.getId().equals("UTC") ? ZoneOffset.UTC : a.zoneId),
              // when zone id is "UTC", use the ZoneOffset.UTC constant instead of the zoneId object
              a -> ZonedDateTime.ofInstant(Instant.ofEpochSecond(a.integer, a.fraction), a.zoneId.getId().equals("UTC") ? ZoneOffset.UTC : a.zoneId),
              // the same is equals to InstantDeserializer
              ZonedDateTime::withZoneSameInstant, false);
    }
}

Then you have to register this deserializer. If you use ObjectMapper, you need to add this to the JavaTimeModule:

ObjectMapper objectMapper = new ObjectMapper();
JavaTimeModule module = new JavaTimeModule();
// add my custom deserializer (this will affect all ZonedDateTime deserialization)
module.addDeserializer(ZonedDateTime.class, new CustomZonedDateTimeDeserializer());
objectMapper.registerModule(module);

If you configure it in Spring, the config will be something like this (not tested):

<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean" id="pnxObjectMapper">
    <property name="deserializersByType">
        <map key-type="java.lang.Class">
            <entry>
                <key>
                    <value>java.time.ZonedDateTime</value>
                </key>
                <bean class="your.app.CustomZonedDateTimeDeserializer">
                </bean>
            </entry>
        </map>
    </property>
</bean>

I use this :

        JavaTimeModule javaTimeModule = new JavaTimeModule();
    javaTimeModule.addSerializer(ZonedDateTime.class, new ZonedDateTimeSerializer(DateTimeFormatter.ISO_DATE_TIME));
    javaTimeModule.addDeserializer(ZonedDateTime.class, InstantDeserializer.ZONED_DATE_TIME);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!