I have a JSON date which I'm attempting to deserialise:
{
"created_at":"2015-05-26 10:14:32"
}
However, the space appears to be causing an issue with GSON and it throws the following error:
com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 160 path $.created_at
com.google.gson.JsonSyntaxException: 2015-05-26
Caused by: java.text.ParseException: Unparseable date: "2015-05-26" (at offset 10)
Here is my GSON configuration:
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd HH:mm:ss")
.setExclusionStrategies(new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes f) {
return f.getDeclaringClass().equals(RealmObject.class);
}
@Override
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
})
.create();
My model:
public class MyModel extends ReamlObject {
@SerializedName("created_at")
private Date createdAt = new Date(System.currentTimeMillis());
}
Finally
gson.fromJson(json, MyModel.class);
Does anyone have any solutions that allow for the space within date? I've tried a custom date deserialiser but that didn't make any difference either.