What steps will reproduce the problem?
1. Run high levels of threads which do date serialization
2. DefaultDateTypeAdapter uses SimpleDateFormat statically
3. See lots of exceptions on random occasions
What is the expected output? What do you see instead?
Expect date serialization to work!
public JsonElement serialize(Date src, Type typeOfSrc,
JsonSerializationContext context) {
String dateFormatAsString = format.format(src);
return new JsonPrimitive(dateFormatAsString);
}
Changing the constructor to:
public DefaultDateTypeAdapter(final String datePattern) {
this.format = new ThreadLocal<DateFormat>() {
protected DateFormat initialValue() {
new SimpleDateFormat(datePattern);
};
}
public JsonElement serialize(Date src, Type typeOfSrc,
JsonSerializationContext context) {
String dateFormatAsString = format.get().format(src);
return new JsonPrimitive(dateFormatAsString);
}
public Date deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context)
throws JsonParseException {
if (!(json instanceof JsonPrimitive)) {
throw new JsonParseException("The date should be a string value");
}
try {
return format.get().parse(json.getAsString());
} catch (ParseException e) {
throw new JsonParseException(e);
}
}
Would be a simple fix.
What version of the product are you using? On what operating system?
Latest GSON release. Issue is using the date formatter statically, as
SimpleDateFormat isn't thread safe, so you'll get random results with the
date format. It will also randomly throw exceptions. See stack trace below.
Please provide any additional information below.
java.lang.ArrayIndexOutOfBoundsException: -28 at
sun.util.calendar.BaseCalendar.getCalendarDateFromFixedDate(BaseCalendar.java:43
6)
at
java.util.GregorianCalendar.computeFields(GregorianCalendar.java:2081) at
java.util.GregorianCalendar.computeFields(GregorianCalendar.java:1996) at
java.util.Calendar.setTimeInMillis(Calendar.java:1066) at
java.util.Calendar.setTime(Calendar.java:1032) at
java.text.SimpleDateFormat.format(SimpleDateFormat.java:785) at
java.text.SimpleDateFormat.format(SimpleDateFormat.java:778) at
java.text.DateFormat.format(DateFormat.java:314) at
com.google.gson.DefaultTypeAdapters$DefaultDateTypeAdapter.serialize(DefaultType
Adapters.java:254)
at
Original issue reported on code.google.com by
mcinto...@gmail.comon 25 Sep 2009 at 4:12