Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ThorstenPlatz opened this issue May 11, 2015 · 4 comments
Milestone

Comments

@ThorstenPlatz
Copy link

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
Copy link
Member

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
Copy link
Author

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
Copy link
Member

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 added a commit that referenced this issue May 13, 2015
@cowtowncoder cowtowncoder added this to the 2.5.4 milestone May 13, 2015
@cowtowncoder
Copy link
Member

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants