Inconsistent serialization/deserialization for OffsetDateTime
See original GitHub issueWith WRITE_DATES_AS_TIMESTAMPS disabled, the serialization of OffsetDateTime preserves the offset, while the deserialization discards it (makes the offset 0 as UTC). The unit test below converts an a OffsetDateTime to JSON and back to OffsetDateTime. It fails because of this inconsistency.
NOTE: The OffsetDateTime corresponds to the same Instant, but it’s not equal to the original OffsetDateTime as defined by equals(), which takes the offset into account.
@Test
public void offsetDateTimeToJsonAndBack() throws IOException {
ObjectMapper objectMapper = new ObjectMapper()
.registerModule(new JavaTimeModule())
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
OffsetDateTime expected = OffsetDateTime.now();
String actualString = objectMapper.writeValueAsString(expected);
System.out.println(actualString); // 2018-02-06T08:37:33.557-08:00
OffsetDateTime actual = objectMapper.readValue(actualString, OffsetDateTime.class);
assertEquals(expected, actual);
}
Failure:
java.lang.AssertionError:
Expected :2018-02-06T08:37:33.557-08:00
Actual :2018-02-06T16:37:33.557Z
A similar test using parse() and toString() works fine:
@Test
public void offsetDateTimeToStringAndBack() {
OffsetDateTime expected = OffsetDateTime.now();
String actualString = expected.toString();
System.out.println(actualString);
OffsetDateTime actual = OffsetDateTime.parse(actualString);
assertEquals(expected, actual);
}
Issue Analytics
- State:
- Created 6 years ago
- Reactions:10
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Why OffsetDateTime serializations/deserialization results have ...
The values that appear incongruent are basically the same moment in time ( Instant ), but represented at an offset of -18:00 at...
Read more >Definitive Guide to Jackson ObjectMapper - Serialize and ...
In this detailed guide - learn everything you need to know about ObjectMapper. Convert JSON to and from Java POJOs, implement custom ...
Read more >Jackson Date - Baeldung
In this tutorial, we'll serialize dates with Jackson. We'll start by serializing a simple java.util.Date, then Joda-Time, and finally, ...
Read more >cannot deserialize value of type `java.time.localdatetime` from ...
ts interface Serializable { serialize(): string; ... Is this an incompatible implementation of a future JavaScript (i.e. ES6/ES7/later) feature? - No.
Read more >JSON-B: Java™ API for JSON Binding - Software Download
You may wish to report any ambiguities, inconsistencies or inaccuracies ... Support binding (serialization and deserialization) for all RFC ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hello. Could it be the problem? DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE
Is this by design or a bug? It seems to me as a bug.
To me, having ADJUST_DATES_TO_CONTEXT_TIME_ZONE enabled, I would assume (given the name of that constant) that if my code reads a datetime in timezone +5, but my local timezone is +4, it would get converted to +4. But as @bcalmac shows, it gets converted to +0 / UTC.
Also, the javadoc (https://jar-download.com/javaDoc/com.fasterxml.jackson.core/jackson-databind/2.9.5/com/fasterxml/jackson/databind/DeserializationFeature.html#ADJUST_DATES_TO_CONTEXT_TIME_ZONE) states that
It does not state that it gets converted to UTC.