2

I'm having some difficulty trying to get Jackson to serialize/deserialize JSON date strings sent from an ASP.NET service. The string is in the following format:

/Date(1234567890123)/

A simple example of the type of output I get from the server is like the following:

{
    "name" : "Bob Marley",
    "birthdate" : "/Date(1234567890123)/"
}

How can I get Jackson to convert the date to a Java date object? Is there a way to do it without having to write a custom serializer/deserializer?

Andrew
  • 95
  • 1
  • 6
  • What do you want to do instead of writing a custom deserializer? It is not that much work: http://stackoverflow.com/questions/9184477/gson-throws-exception-parsing-empty-date-field/9185368#9185368 You could alter the resulting object, deserialize as `String` and write a getter that parses the String at access. I don't think that is more elegant. – Hauke Ingmar Schmidt Feb 07 '12 at 23:45
  • 1
    If anyone needs code, look at this question: http://stackoverflow.com/questions/14906981/parsing-asp-net-mvc-returned-date-using-jackson-json-library-in-java. Although eventually I've switched to faster JSON library on the server that also gives you ISO8601 format - http://stackoverflow.com/questions/14973286/asp-net-mvc-json-datetime-serialization-conversion-to-utc – nikib3ro Mar 27 '13 at 13:08

2 Answers2

3

Your best bet is to write a custom deserializer. Or alternatively, to store the string representation of the date in your bean, but provide an alternative getter that converts the string to a date using a DateFormat instance. The first option is cleaner and more efficient.

See question previously asked here on SO.

Community
  • 1
  • 1
Perception
  • 79,279
  • 19
  • 185
  • 195
1

I think the ASP.NET service is producing a strange value for the date and would change that if I could. If I cannot, then writing a customer deserializer is the way to go.

Henrik Barratt Due
  • 5,614
  • 1
  • 21
  • 22