Using jshell version 11.0.1 and I'm trying to test Jackson serialization of a java.time.OffsetDateTime. I want it to be serialized in ISO-8601 format, but it doesn't work as expected. Why is it still being serialized as an object instead of ISO 8601?
here's how I launch 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
Here's the code I run in 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());
Here's the output
| 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);
$6 ==> true
jshell> mapper.writeValueAsString(OffsetDateTime.now());
$7 ==> "{\"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}"
Why is it still being serialized as an object instead of ISO 8601?
After much stumbling I discovered that just because the mapper says it can serialize OffsetDateTime.class doesn't mean it will recognize it as a date/time class. I needed to add this to the mapper.
mapper = mapper.registerModule(new JavaTimeModule());
Now the output is
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);
$7 ==> true
jshell> mapper.writeValueAsString(OffsetDateTime.now());
$8 ==> "\"2019-09-27T16:41:07.921578-07:00\""
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With