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?

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

This not what I ask. The mapping to POJO is good. The problem is with the Map type
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.

Comments

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)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.