Skip to content

DateTime can't be serialized with its own zone (WRITE_DATES_WITH_CONTEXT_TIME_ZONE not respected) #146

Open
@pvgoran

Description

@pvgoran

(This issue is about the same problem as #92. However, I decided to create it because the old issue describes the problem in an indirect way, and because WRITE_DATES_WITH_CONTEXT_TIME_ZONE was added to https://github.com/FasterXML/jackson-databind since then.)

In version 2.18.1, DateTime instances are always serialized using ObjectMapper's time zone (even if it was not explicitly specified - UTC is used in this case), rather than with DateTime's own zone. Disabling the WRITE_DATES_WITH_CONTEXT_TIME_ZONE serialization feature (which was added for FasterXML/jackson-modules-java8#222 to address the same problem but with JSR-310 date-time types) does not help.

Test code:

package local;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.datatype.joda.JodaModule;
import org.joda.time.*;

public class App
{
    public static void main(String[] args) throws Exception
    {
        DateTime dateTime = DateTime.parse("2024-12-01T00:00:00+02:00");
        System.out.println("DateTime: " + dateTime);

        ObjectMapper mapper = _objectMapper();
        String json = mapper.writeValueAsString(dateTime);
        System.out.println("Serialized: " + json);
    }

    private static ObjectMapper _objectMapper()
    {
      ObjectMapper mapper = new ObjectMapper();
      mapper.registerModule(new JodaModule());
      mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
      mapper.configure(SerializationFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE, false);
      return mapper;
    }
}

Output:

DateTime: 2024-12-01T00:00:00.000+02:00
Serialized: "2024-11-30T22:00:00.000Z"

Expected:

DateTime: 2024-12-01T00:00:00.000+02:00
Serialized: "2024-12-01T00:00:00.000+02:00"

Metadata

Metadata

Assignees

No one assigned

    Labels

    2.18has-failing-testIndicates that there exists a test case (under `failing/`) to reproduce the issuepr-welcomeIssue for which progress most likely if someone submits a Pull Request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions