OffsetDateTime 的 Jackson 序列化为 ISO-8601 在 jshell 中不起作用
Jackson serialization of OffsetDateTime as ISO-8601 not working in jshell
使用 jshell 版本 11.0.1,我正在尝试测试 java.time.OffsetDateTime
的 Jackson 序列化。我希望它以 ISO-8601 格式序列化,但它没有按预期工作。 为什么它仍然被序列化为对象而不是 ISO 8601?
这是我启动 jshell 的方式
jshell --class-path ~/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.8.9/jackson-databind-2.8.9.jar:~/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.8.9/jackson-core-2.8.9.jar:~/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.8.9/jackson-annotations-2.8.9.jar:~/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jsr310/2.8.11/jackson-datatype-jsr310-2.8.11.jar
这是我在jshell运行中的代码
import com.fasterxml.jackson.databind.*;
import java.time.*;
import com.fasterxml.jackson.datatype.jsr310.*;
ObjectMapper mapper = new ObjectMapper();
mapper = mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.canSerialize(OffsetDateTime.class);
mapper.writeValueAsString(OffsetDateTime.now());
这是输出
| Welcome to JShell -- Version 11.0.1
| For an introduction type: /help intro
jshell> import com.fasterxml.jackson.databind.*;
jshell> import java.time.*;
jshell> import com.fasterxml.jackson.datatype.jsr310.*;
jshell> ObjectMapper mapper = new ObjectMapper();
mapper ==> com.fasterxml.jackson.databind.ObjectMapper@7fc229ab
jshell> mapper = mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper ==> com.fasterxml.jackson.databind.ObjectMapper@7fc229ab
jshell> mapper.canSerialize(OffsetDateTime.class);
==> true
jshell> mapper.writeValueAsString(OffsetDateTime.now());
==> "{\"offset\":{\"totalSeconds\":-25200,\"id\":\"-07:00\",\"rules\":{\"fixedOffset\":true,\"transitions\":[],\"transitionRules\":[]}},\"month\":\"SEPTEMBER\",\"dayOfWeek\":\"FRIDAY\",\"dayOfYear\":270,\"nano\":911405000,\"year\":2019,\"monthValue\":9,\"dayOfMonth\":27,\"hour\":16,\"minute\":33,\"second\":6}"
为什么它仍然被序列化为对象而不是 ISO 8601?
经过多次跌跌撞撞后,我发现仅仅因为映射器说它可以序列化 OffsetDateTime.class 并不意味着它将把它识别为 date/time class。我需要将它添加到映射器中。
mapper = mapper.registerModule(new JavaTimeModule());
现在输出是
jshell> import com.fasterxml.jackson.databind.*;
jshell> import java.time.*;
jshell> import com.fasterxml.jackson.datatype.jsr310.*;
jshell> ObjectMapper mapper = new ObjectMapper();
mapper ==> com.fasterxml.jackson.databind.ObjectMapper@7fc229ab
jshell> mapper = mapper.registerModule(new JavaTimeModule());
mapper ==> com.fasterxml.jackson.databind.ObjectMapper@7fc229ab
jshell> mapper = mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper ==> com.fasterxml.jackson.databind.ObjectMapper@7fc229ab
jshell> mapper.canSerialize(OffsetDateTime.class);
==> true
jshell> mapper.writeValueAsString(OffsetDateTime.now());
==> "\"2019-09-27T16:41:07.921578-07:00\""
使用 jshell 版本 11.0.1,我正在尝试测试 java.time.OffsetDateTime
的 Jackson 序列化。我希望它以 ISO-8601 格式序列化,但它没有按预期工作。 为什么它仍然被序列化为对象而不是 ISO 8601?
这是我启动 jshell 的方式
jshell --class-path ~/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.8.9/jackson-databind-2.8.9.jar:~/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.8.9/jackson-core-2.8.9.jar:~/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.8.9/jackson-annotations-2.8.9.jar:~/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jsr310/2.8.11/jackson-datatype-jsr310-2.8.11.jar
这是我在jshell运行中的代码
import com.fasterxml.jackson.databind.*;
import java.time.*;
import com.fasterxml.jackson.datatype.jsr310.*;
ObjectMapper mapper = new ObjectMapper();
mapper = mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.canSerialize(OffsetDateTime.class);
mapper.writeValueAsString(OffsetDateTime.now());
这是输出
| Welcome to JShell -- Version 11.0.1
| For an introduction type: /help intro
jshell> import com.fasterxml.jackson.databind.*;
jshell> import java.time.*;
jshell> import com.fasterxml.jackson.datatype.jsr310.*;
jshell> ObjectMapper mapper = new ObjectMapper();
mapper ==> com.fasterxml.jackson.databind.ObjectMapper@7fc229ab
jshell> mapper = mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper ==> com.fasterxml.jackson.databind.ObjectMapper@7fc229ab
jshell> mapper.canSerialize(OffsetDateTime.class);
==> true
jshell> mapper.writeValueAsString(OffsetDateTime.now());
==> "{\"offset\":{\"totalSeconds\":-25200,\"id\":\"-07:00\",\"rules\":{\"fixedOffset\":true,\"transitions\":[],\"transitionRules\":[]}},\"month\":\"SEPTEMBER\",\"dayOfWeek\":\"FRIDAY\",\"dayOfYear\":270,\"nano\":911405000,\"year\":2019,\"monthValue\":9,\"dayOfMonth\":27,\"hour\":16,\"minute\":33,\"second\":6}"
为什么它仍然被序列化为对象而不是 ISO 8601?
经过多次跌跌撞撞后,我发现仅仅因为映射器说它可以序列化 OffsetDateTime.class 并不意味着它将把它识别为 date/time class。我需要将它添加到映射器中。
mapper = mapper.registerModule(new JavaTimeModule());
现在输出是
jshell> import com.fasterxml.jackson.databind.*;
jshell> import java.time.*;
jshell> import com.fasterxml.jackson.datatype.jsr310.*;
jshell> ObjectMapper mapper = new ObjectMapper();
mapper ==> com.fasterxml.jackson.databind.ObjectMapper@7fc229ab
jshell> mapper = mapper.registerModule(new JavaTimeModule());
mapper ==> com.fasterxml.jackson.databind.ObjectMapper@7fc229ab
jshell> mapper = mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper ==> com.fasterxml.jackson.databind.ObjectMapper@7fc229ab
jshell> mapper.canSerialize(OffsetDateTime.class);
==> true
jshell> mapper.writeValueAsString(OffsetDateTime.now());
==> "\"2019-09-27T16:41:07.921578-07:00\""