FasterXML / jackson-datatype-joda

Extension module to properly support full datatype set of Joda datetime library
Apache License 2.0
140 stars 82 forks source link

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

Closed ThorstenPlatz closed 9 years ago

ThorstenPlatz commented 9 years ago

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]'
cowtowncoder commented 9 years ago

Thank you for investigating with different versions, this is helpful. If it is easy enough, would it be possible to run one more test, with 2.4.6? While it is likely the problem exists (given that 2.5.3 fails, which has same fixes as 2.4.6), due to 2.4.4 causing breakage, there is a chance that fixes in 2.4.5 and 2.4.6 might be relevant. Breakage in question was related to incorrect caching of complex types like Maps, so it is not immediately obvious that this problem would be related. But timing does appear suspicious.

ThorstenPlatz commented 9 years ago

Your guess was right!

The test passes with 2.4.6:

packageVersion: 2.4.6
jsonInstant: "2015-05-12T05:00:22.134Z"
jsonDateTime: "2015-05-12T05:00:22.134Z"

And also passes with 2.4.5:

packageVersion: 2.4.5
jsonInstant: "2015-05-12T05:08:09.180Z"
jsonDateTime: "2015-05-12T05:08:09.180Z"

I did not tried this version before, because it was not listed in the release notes.

cowtowncoder commented 9 years ago

Looks like I hadn't merged from 2.4. The actual fix is in jackson-databind however, I think.

But this does raise the question of why 2.5.3 fails. It should have similar fixes...

cowtowncoder commented 9 years ago

So this was a regression due to changes meant to let Duration serializer use feature WRITE_DURATIONS_AS_TIMESTAMPS: accidentally was also using that for Instant values, which should instead use older WRITE_DATES_AS_TIMESTAMPS SerializationFeature.

Thank you for reporting this!