Closed GoogleCodeExporter closed 9 years ago
I'm seeing the same issue (I think)...out of curiosity, have you tried this
with gson 1.6 yet?
Original comment by snag...@yahoo.com
on 30 Nov 2010 at 10:26
The same problem occurs in Gson 1.6, the main issue is that I think you should
be able to deserialize the data with the same GsonBuilder that was used to
serialize it, and that's currently not the case.
Original comment by dmitri.sotnikov@gmail.com
on 30 Nov 2010 at 11:24
The work around I'm using currently is to write custom serializers and
deserializers for dates:
private static final String dateFormat = "MM/dd/yyyy hh:mm:ss a";
private static SimpleDateFormat format = new SimpleDateFormat(dateFormat);
private static class DateTimeSerializer implements JsonSerializer<Date> {
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(format.format(src));
}
}
private static class DateTimeDeserializer implements JsonDeserializer<Date> {
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
try {
return format.parse(json.getAsJsonPrimitive().getAsString());
} catch (ParseException e) {
throw new JsonParseException(e.getMessage());
}
}
}
Original comment by dmitri.sotnikov@gmail.com
on 30 Nov 2010 at 11:26
Cool, thanks. It turned out the problem I was having was that my object model
had a java.sql.Timestamp (derived from the database), which is a subclass of
Date and therefore skipped the custom serializer. By registering all the
subclasses (including java.sql.Date, etc.) with the same serializer, it works
fine now.
BTW, is there no way to configure the GsonBuilder so that subclasses use the
same custom serializer? It seems like that would be desired, unless a more
specific serializer applied.
Original comment by snag...@yahoo.com
on 1 Dec 2010 at 5:26
Fixed by r762.
Original comment by limpbizkit
on 29 Mar 2011 at 9:53
Original issue reported on code.google.com by
dmitri.sotnikov@gmail.com
on 23 Nov 2010 at 10:42