I've got some JSON that has timestamps in milliseconds:
{"foo":"bar","timestamp":1386280997}
Asking Jackson to deserialize this into an object with a DateTime field for the timestamp results in 1970-01-17T01:11:25.983Z
, a time shortly after the epoch because Jackson is assuming it to be in nanoseconds. Aside from ripping apart the JSON and adding some zeros, how might I get Jackson to understand the millisecond timestamp?
Edit—some test code that shows this (Groovy syntax):
class TimestampThing {
DateTime timestamp
@JsonCreator
public TimestampThing(@JsonProperty('timestamp') DateTime timestamp) {
this.timestamp = timestamp
}
}
And a couple of tests, the first in milliseconds, the second in microseconds:
class TimestampTest extends GroovyTestCase {
private ObjectMapper mapper = new ObjectMapper()
void testMillisecondTimestamp() {
String json = '{"timestamp":1386280997}'
TimestampThing thing = mapper.readValue(json, TimestampThing.class)
println thing.getTimestamp()
}
void testNanosecondTimestamp() {
String json = '{"timestamp":1386280997000}'
TimestampThing thing = mapper.readValue(json, TimestampThing.class)
println thing.getTimestamp()
}
}
The out from running the test class is:
1970-01-16T20:04:40.997-05:00
2013-12-05T17:03:17.000-05:00