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

java.util.Date can not be serialized as long, only (non-configurable) textual #29

Closed
gpiancastelli opened this issue Oct 27, 2015 · 2 comments
Milestone

Comments

@gpiancastelli
Copy link

I need to serialize dates in their canonical milliseconds representation; however, when an object with a java.util.Date property is serialized, the produced JSON contains dates as formatted strings, e.g. "Tue Oct 27 17:48:29 CET 2015". I checked out the documentation to see if there is some kind of feature to activate, or some kind of annotation to put on the relevant getter method, but, alas, no luck.

Is there a way to obtain the milliseconds representation for dates in Jackson Jr.?

Note that deserialization of milliseconds into java.util.Date objects work just fine.

@cowtowncoder
Copy link
Member

Sounds like this could be a new JSON.Feature (WRITE_DATES_AS_LONG?): currently date.toString() is indeed used. Deserializer however accepts longs.
Adding the new feature should be easy enough and would apply to java.util.Date and java.util.Calendar.

Change to default formatting would be separate possible addition; it would be bit more work, so I'd have to see if and how to do that. Sounds like a useful thing to have, although also opens the way for more complicated handling (what about TimeZone used?), perhaps ability for custom handlers.
And at some point it's necessary to define where to stop addition of features, to keep library small.
But one step at a time makes sense, starting with ability to serialize dates as timestamps.

@cowtowncoder cowtowncoder added this to the 2.7.0-rc2 milestone Dec 2, 2015
@cowtowncoder cowtowncoder changed the title Date is not serialized as long and there is no way to change the default formatting java.util.Date can not be serialized as long, only (non-configurable) textual Dec 2, 2015
@cowtowncoder
Copy link
Member

First things first: added JSON.Feature.WRITE_DATES_AS_TIMESTAMP, enabling of which allows timestamp serialization.

Question of if and how to configure DateFormat to use is trickier, and I am not sure how far to even go there. java.util.Date is flawed by design unfortunately, and a better model would probably be to allow some way of registering custom writer/reader so calling code could choose how it wants to handle 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