question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inconsistent serialization/deserialization for OffsetDateTime

See original GitHub issue

With 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:closed
  • Created 6 years ago
  • Reactions:10
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

13reactions
oliverdozsacommented, Aug 21, 2018

Hello. Could it be the problem? DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE

5reactions
yngvarkcommented, Mar 11, 2019

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

Feature that specifies whether context provided TimeZone (DeserializationContext.getTimeZone() should be used to adjust Date/Time values on deserialization.

It does not state that it gets converted to UTC.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found