7

I have this json string:

{
  "startDate" : "2014-12-17T14:31:40Z",
  "name" : "Izek",
  "age" : 12
}

When I convert it with Jackson to Map[String, Object] the type of startDate is String how I can tell Jackson to convert it to DateTime type?

igreenfield
  • 1,618
  • 19
  • 36

3 Answers3

4

You need to explicitly set the data format in the objectMapper. You could refer Date format Mapping to JSON Jackson for more details. Alternately, you could do it as http://java.dzone.com/articles/how-serialize-javautildate

Community
  • 1
  • 1
Balaji Katika
  • 2,835
  • 2
  • 21
  • 19
1

Have you considered a custom map deserializer? You can try to parse the date in there. If not known in advance, you'll probably hit a performance hit here.

YaOg
  • 1,748
  • 5
  • 24
  • 43
1

I found a way to do it. define my own UntypedObjectDeserializer and extend std.UntypedObjectDeserializer in the deserialize method:

if (currentToken == JsonToken.VALUE_STRING) {
   if (_stringDeserializer != null) {
      return _stringDeserializer.deserialize(jp, ctxt)
   }
   String text = jp.getText();
   if (dateTimeFormatRegex.match(text) {
      return toDateTimeObject(text);
   } else {
      return text;
   }
}
return super.deserialize(jp, ctxt)

public DateTime toDateTimeObject(String text) {
     dateTimeFormatter.parseDateTime(text)
}
igreenfield
  • 1,618
  • 19
  • 36