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

DateTimeSerializerBase doesn't use WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS with java.sql.Timestamp #1499

Closed
hadrienk opened this issue Jan 23, 2017 · 6 comments

Comments

@hadrienk
Copy link

When converted using DateTimeSerializerBase, java.sql.Timestamp instances are not written in nanoseconds, even if the WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS feature is enabled.

I found this error doing the following:

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true);
mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true);
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
mapper.convertValue(new Timestamp(1456412409000), Instant.class)
@cowtowncoder
Copy link
Member

cowtowncoder commented Jan 26, 2017

@hadrienk Unfortunately timestamp resolution with pre-Java8 JDK types is limited to milliseconds, and as such this feature is only supported by Java 8 date/time types. This is mentioned in Javadocs.

@cowtowncoder cowtowncoder changed the title DateTimeSerializerBase doesn't use WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS DateTimeSerializerBase doesn't use WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS with java.sql.Timestamp Jan 29, 2017
@cowtowncoder
Copy link
Member

Actually looking at javadocs for java.sql.Timestamp, it sounds like it could actually retain the additional part, so in theory perhaps this could be supportable.

The challenge here is that of choosing proper representation. Although in theory one could simply multiply value by 1 billion, this would likely cause problems with interoperability and/or backwards compatibility. It would seem preferable to either:

  1. Use floating point number, so that milliseconds would be stored in integral part, and fractional part would define fraction of milliseconds.
  2. Use a (JSON) Object with two properties
  3. Use a 2-element array.

I can't remember how exactly Java 8 date/time module (https://github.com/FasterXML/jackson-modules-java8) handles this, but it would probably be worth checking it out for guidance.

@hadrienk
Copy link
Author

hadrienk commented Feb 1, 2017

Actually I should have written this bug in the java 8 data/time module.

@hadrienk
Copy link
Author

hadrienk commented Feb 1, 2017

I am closing it, feel free to reopen it is you think it is worth a fix :)

@hadrienk hadrienk closed this as completed Feb 1, 2017
@cowtowncoder
Copy link
Member

Actually this is the right place since java.sql.Timestamp is part of core JDK, not something added in Java 8.
Further I think it should be quite possible to add support as requested.

@cowtowncoder
Copy link
Member

Rethinking this again since 2.9.0 release is looming. While it would be technically possible to add this, realized that there is no separate serializer for java.sql.Timestamp yet. And with usage of pre-Java8 datetime types going down addition for separate serializer is probably not worth the hassle.
So, closing.

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