Skip to content

Configured date/time format not considered when serializing Joda Instant  #60

@ThorstenPlatz

Description

@ThorstenPlatz

The configured date/time format is not considered for org.joda.time.Instant since version 2.4.4 and newer. In earlier versions Instants were serialized using the configured formatter.

Reproduce with the follwing code snippet:

  @Test
  public void testInstantConversion() throws Exception {
    ObjectMapper om = new ObjectMapper();
    om.registerModule(new JodaModule());

    // Configure Date Formatting
    om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"));

    // Create an instant and serialize and additonally serialize the instant as DateTime to demonstrate the difference
    org.joda.time.Instant now = new Instant();
    String jsonInstant = om.writeValueAsString(now);
    String jsonDateTime = om.writeValueAsString(now.toDateTime());

    // just to show the version that was used when running the demo
    System.out.println("packageVersion: " + com.fasterxml.jackson.datatype.joda.PackageVersion.VERSION);

    System.out.println("jsonInstant: " + jsonInstant);
    System.out.println("jsonDateTime: " + jsonDateTime);

    assertThat(jsonInstant).isEqualTo(jsonDateTime); // will fail for jackson-datatype-joda > 2.4.4
  }

Output using v2.4.3:

packageVersion: 2.4.3
jsonInstant: "2015-05-11T13:40:14.541Z"
jsonDateTime: "2015-05-11T13:40:14.541Z"

Output using v2.4.4:

packageVersion: 2.4.4
jsonInstant: "2015-05-11T13:46:41.081Z"
jsonDateTime: "2015-05-11T13:46:41.081Z"

Output using v2.5.0:

packageVersion: 2.5.0
jsonInstant: 1431352169061
jsonDateTime: "2015-05-11T13:49:29.061Z"

org.junit.ComparisonFailure: 
Expected :'["2015-05-11T13:49:29.061Z"]'
Actual   :'[1431352169061]':

Output using v2.5.3:

packageVersion: 2.5.3
jsonInstant: 1431352294592
jsonDateTime: "2015-05-11T13:51:34.592Z"

org.junit.ComparisonFailure: 
Expected :'["2015-05-11T13:51:34.592Z"]'
Actual   :'[1431352294592]'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions