Jackson 2 - Deserialize String directly to date with REST API update on Android
I am working on deserializing json containing java.util.Date as String in "yyyy-MM-dd HH: mm: ss" format. My goal is to parse it into a date, so I can access it later as java.util.Date.
My POJO contains the following:
@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
"id",
"dateTime",
"currentPeriod",
(...)
})
public class MatchItem {
@JsonProperty("id")
private int id;
@JsonProperty("dateTime")
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss",
private Date dateTime;
@JsonProperty("currentPeriod")
private int currentPeriod;
(...)
@JsonProperty("dateTime")
public Date getDateTime() {
return dateTime;
}
@JsonProperty("dateTime")
public void setDateTime(Date dateTime) {
this.dateTime = dateTime;
}
Then I use REST API (Retrofit) to directly deserialize responses to json server. Leaving the date in string format works, but when I try to deserialize it to java.util.Date directly, doesn't it?
How can I get it to work?
Thanks for the help guys.
+3
source to share
1 answer
In the end, I didn't find a way to do what I wanted to do with Jackson's annotations only, I fixed it by implementing my own converter for such a modification:
JacksonConverter matchItemConverter = new JacksonConverter(Utils.getHwObjectMapper());
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(URL)
.setConverter(matchItemConverter)
.build();
When creating the objectmapper, be sure to add the date format:
public static ObjectMapper getHwObjectMapper() {
ObjectMapper hWObjectMapper = new ObjectMapper();
hWObjectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH));
return hWObjectMapper;
}
And make this Jackson based converter:
public class JacksonConverter implements Converter {
private final ObjectMapper mapper;
public JacksonConverter(ObjectMapper mapper) {
this.mapper = mapper;
}
@Override public Object fromBody(TypedInput body, Type type) throws ConversionException
{
String charset = "UTF-8";
if (body.mimeType() != null) {
charset = MimeUtil.parseCharset(body.mimeType());
}
InputStreamReader isr = null;
try {
isr = new InputStreamReader(body.in(), charset);
return mapper.readValue(isr, TypeFactory.rawClass(type));
} catch (IOException e) {
throw new ConversionException(e);
} finally {
if (isr != null) {
try {
isr.close();
} catch (IOException ignored) {
ignored.printStackTrace();
}
}
}
}
@Override public TypedOutput toBody(Object object) {
try {
return new JsonTypedOutput(mapper.writeValueAsBytes(object));
} catch (JsonProcessingException e) {
throw new AssertionError(e);
}
}
private static class JsonTypedOutput implements TypedOutput {
private final byte[] jsonBytes;
JsonTypedOutput(byte[] jsonBytes) {
this.jsonBytes = jsonBytes;
}
@Override public String fileName() {
return null;
}
@Override public String mimeType() {
return "application/json; charset=UTF-8";
}
@Override public long length() {
return jsonBytes.length;
}
@Override public void writeTo(OutputStream out) throws IOException {
out.write(jsonBytes);
}
}
}
+2
source to share