ChineSouad / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

GSON default date serializer is locale-specific #281

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
This has caused problems for Caliper, which was relying on the default date 
serializer:
http://code.google.com/p/caliper/issues/detail?id=113

Work-around the problem by registering a date type adapter like the following:

  private static class DateTypeAdapter implements JsonSerializer<Date>, JsonDeserializer<Date> {
    private final DateFormat dateFormat;

    private DateTypeAdapter() {
      dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz", Locale.US);
      dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    }

    @Override public synchronized JsonElement serialize(Date date, Type type,
        JsonSerializationContext jsonSerializationContext) {
      return new JsonPrimitive(dateFormat.format(date));
    }

    @Override public synchronized Date deserialize(JsonElement jsonElement, Type type,
        JsonDeserializationContext jsonDeserializationContext) {
      try {
        return dateFormat.parse(jsonElement.getAsString());
      } catch (ParseException e) {
        throw new JsonParseException(e);
      }
    }
  }

Original issue reported on code.google.com by limpbizkit on 30 Jan 2011 at 1:01

GoogleCodeExporter commented 9 years ago
Fixed by r721.

Original comment by limpbizkit on 10 Feb 2011 at 1:07

GoogleCodeExporter commented 9 years ago
It will be great if we can configure GsonBuilder to force used of ISO-8601 
format with UTC timezone, for serialization and deserialization.

Thanks

Original comment by boillodm...@gmail.com on 14 Jun 2011 at 10:03

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
private static class GmtDateTypeAdapter implements JsonSerializer<Date>, 
JsonDeserializer<Date> {
        private final DateFormat dateFormat;

        private GmtDateTypeAdapter() {
            dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
            dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        }

        @Override
        public synchronized JsonElement serialize(Date date, Type type,
                JsonSerializationContext jsonSerializationContext) {
            synchronized (dateFormat) {
                String dateFormatAsString = dateFormat.format(date);
                return new JsonPrimitive(dateFormatAsString);
            }
        }

        @Override
        public synchronized Date deserialize(JsonElement jsonElement, Type type,
                JsonDeserializationContext jsonDeserializationContext) {
            try {
                synchronized (dateFormat) {
                    return dateFormat.parse(jsonElement.getAsString());
                }
            } catch (ParseException e) {
                throw new JsonSyntaxException(jsonElement.getAsString(), e);
            }
        }
    }

Original comment by boillodm...@gmail.com on 14 Jun 2011 at 12:36

GoogleCodeExporter commented 9 years ago
+1 ! And it would be nice to be able to specify the DateFormat and not just 
only the SDF pattern on the GsonBuilder

Original comment by lorber.s...@gmail.com on 15 Mar 2013 at 2:46

GoogleCodeExporter commented 9 years ago
Which version of gson has this fix ?

Original comment by misramay...@gmail.com on 3 Jul 2014 at 9:48