How to deserialize Jackson date format: epoch milliseconds, no enclosing Date()?

2,456 views
Skip to first unread message

Llessur

unread,
Jul 9, 2012, 10:51:11 AM7/9/12
to res...@googlegroups.com
I am consuming a service created using Jackson (JAX-RS JSON processor), which appears to be very popular in the Java community. The Jackson default date format is epoch milliseconds as a number. I don't control the service so cannot change the service to use a non-default format. Sample:
 
{ "myDate": 1234567890123 }
 
RestKit automatically maps ISO 8601 and ASP.NET JSON date formats. ASP.NET uses the same epoch milliseconds number but encloses it, presumably to allow eval.
 
{ "myDate": "\/Date(1234567890123)\/" }
 
This must be a common issue. Before I jump in to figuring out how to map this myself, is there a best practice?

Llessur

unread,
Jul 9, 2012, 11:44:45 PM7/9/12
to res...@googlegroups.com
I figured out the obvious problem, NSDate dateWithTimeIntervalSince1970 accepts seconds not milliseconds. RestKit converts to milliseconds in RKDotNetDateFormatter but not in RKObjectMappingOperation. I presume this is on purpose.

sharpnk

unread,
Jul 23, 2012, 9:10:02 AM7/23/12
to res...@googlegroups.com
I came across this issue today. My work-around was to convert milliseconds to seconds in 
RKObjectMappingOperation before the object mapping. 

More to that, I am on main branch. The default code parses the epoch time in intValue, which causes an overflow when server returns something in milliseconds. I ended up changing it to: 

return [NSDate dateWithTimeIntervalSince1970:[((NSNumber*)value) longLongValue]/1000]; 
Reply all
Reply to author
Forward
0 new messages