diff --git a/pippo-content-type-parent/pippo-gson/pom.xml b/pippo-content-type-parent/pippo-gson/pom.xml index 005ff9bc0..684ae0e57 100644 --- a/pippo-content-type-parent/pippo-gson/pom.xml +++ b/pippo-content-type-parent/pippo-gson/pom.xml @@ -15,7 +15,7 @@ Google Gson integration - 2.3.1 + 2.8.6 @@ -46,4 +46,24 @@ + + + + src/test/resources + false + + + + src/test/java + + ** + + + **/*.java + + false + + + + diff --git a/pippo-content-type-parent/pippo-gson/src/main/java/ro/pippo/gson/GsonEngine.java b/pippo-content-type-parent/pippo-gson/src/main/java/ro/pippo/gson/GsonEngine.java index d5cecac53..1cd734036 100644 --- a/pippo-content-type-parent/pippo-gson/src/main/java/ro/pippo/gson/GsonEngine.java +++ b/pippo-content-type-parent/pippo-gson/src/main/java/ro/pippo/gson/GsonEngine.java @@ -17,25 +17,14 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; -import com.google.gson.JsonPrimitive; -import com.google.gson.JsonSerializationContext; -import com.google.gson.JsonSerializer; -import com.google.gson.JsonSyntaxException; + import org.kohsuke.MetaInfServices; import ro.pippo.core.Application; import ro.pippo.core.ContentTypeEngine; import ro.pippo.core.HttpConstants; -import java.lang.reflect.Type; import java.sql.Time; -import java.text.DateFormat; -import java.text.ParseException; -import java.text.SimpleDateFormat; import java.util.Date; -import java.util.Locale; /** * A JsonEngine based on Gson. @@ -49,11 +38,7 @@ public class GsonEngine implements ContentTypeEngine { @Override public void init(Application application) { - this.gson = new GsonBuilder() - .registerTypeAdapter(Date.class, new ISO8601DateTimeTypeAdapter()) - .registerTypeAdapter(Time.class, new ISO8601TimeTypeAdapter()) - .registerTypeAdapter(java.sql.Date.class, new ISO8601DateTypeAdapter()) - .create(); + this.gson = buildGson(application); } @Override @@ -71,93 +56,12 @@ public T fromString(String content, Class classOfT) { return gson.fromJson(content, classOfT); } - public static class ISO8601DateTypeAdapter implements JsonSerializer, JsonDeserializer { - private final DateFormat dateFormat; - - public ISO8601DateTypeAdapter() { - dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US); - } - - @Override - public synchronized JsonElement serialize(java.sql.Date date, Type type, - JsonSerializationContext jsonSerializationContext) { - synchronized (dateFormat) { - String dateFormatAsString = dateFormat.format(date); - return new JsonPrimitive(dateFormatAsString); - } - } - - @Override - public synchronized java.sql.Date deserialize(JsonElement jsonElement, Type type, - JsonDeserializationContext jsonDeserializationContext) { - try { - synchronized (dateFormat) { - Date date = dateFormat.parse(jsonElement.getAsString()); - return new java.sql.Date((date.getTime() / 1000) * 1000); - } - } catch (ParseException e) { - throw new JsonSyntaxException(jsonElement.getAsString(), e); - } - } - } - - public static class ISO8601TimeTypeAdapter implements JsonSerializer