Skip to content

Commit

Permalink
Merge pull request ArcBees#34 from ArcBees/mmc-dateadapter-threadsafe
Browse files Browse the repository at this point in the history
Fix for ArcBees#33 : GsonDateTypeAdapter isn't thread safe
  • Loading branch information
meriouma committed Oct 21, 2015
2 parents 7d08c22 + 7becf62 commit 7466f89
Showing 1 changed file with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.Date;
import java.util.List;

import com.google.common.collect.Lists;
import com.google.common.collect.ImmutableList;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
Expand All @@ -37,16 +37,18 @@ public class GsonDateTypeAdapter implements JsonDeserializer<Date> {
"yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX");
private static final DateFormat CUSTOM_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssXXX");
private static final List<DateFormat> dateFormats =
Lists.newArrayList(DATE_TIME_LONG_FORMAT_ALT2, DATE_TIME_LONG_FORMAT, DATE_TIME_FORMAT, CUSTOM_DATE_FORMAT,
DATE_TIME_LONG_FORMAT_ALT);
ImmutableList.of(DATE_TIME_LONG_FORMAT_ALT2, DATE_TIME_LONG_FORMAT, DATE_TIME_FORMAT,
CUSTOM_DATE_FORMAT, DATE_TIME_LONG_FORMAT_ALT);

@Override
public Date deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
String value = json.getAsJsonPrimitive().getAsString();
for (DateFormat dateFormat : dateFormats) {
try {
return dateFormat.parse(value);
} catch (ParseException e) {
synchronized (dateFormats) {
for (DateFormat dateFormat : dateFormats) {
try {
return dateFormat.parse(value);
} catch (ParseException e) {
}
}
}

Expand Down

0 comments on commit 7466f89

Please sign in to comment.