diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java index 4b6d2654..2fca11eb 100644 --- a/gson/src/main/java/com/google/gson/Gson.java +++ b/gson/src/main/java/com/google/gson/Gson.java @@ -100,7 +100,7 @@ import java.util.Map; */ public final class Gson { @SuppressWarnings("unchecked") - static final ParameterizedTypeHandlerMap EMPTY_MAP = + static final ParameterizedTypeHandlerMap EMPTY_MAP = new ParameterizedTypeHandlerMap().makeUnmodifiable(); static final boolean DEFAULT_JSON_NON_EXECUTABLE = false; @@ -254,8 +254,7 @@ public final class Gson { builder.factory(factory); } - builder.factory(new GsonToMiniGsonTypeAdapterFactory(serializers, deserializers, - new JsonDeserializationContext(this), new JsonSerializationContext(this), serializeNulls)) + builder.factory(new GsonToMiniGsonTypeAdapterFactory(this, serializers, deserializers, serializeNulls)) .factory(TypeAdapters.URL_FACTORY) .factory(TypeAdapters.URI_FACTORY) .factory(TypeAdapters.UUID_FACTORY) diff --git a/gson/src/main/java/com/google/gson/GsonToMiniGsonTypeAdapterFactory.java b/gson/src/main/java/com/google/gson/GsonToMiniGsonTypeAdapterFactory.java index 77fdd02a..69d383e5 100644 --- a/gson/src/main/java/com/google/gson/GsonToMiniGsonTypeAdapterFactory.java +++ b/gson/src/main/java/com/google/gson/GsonToMiniGsonTypeAdapterFactory.java @@ -32,15 +32,28 @@ final class GsonToMiniGsonTypeAdapterFactory implements TypeAdapter.Factory { private final JsonSerializationContext serializationContext; private final boolean serializeNulls; - GsonToMiniGsonTypeAdapterFactory(ParameterizedTypeHandlerMap> serializers, + public GsonToMiniGsonTypeAdapterFactory(final Gson gson, + ParameterizedTypeHandlerMap> serializers, ParameterizedTypeHandlerMap> deserializers, - JsonDeserializationContext deserializationContext, - JsonSerializationContext serializationContext, boolean serializeNulls) { + boolean serializeNulls) { this.serializers = serializers; this.deserializers = deserializers; this.serializeNulls = serializeNulls; - this.deserializationContext = deserializationContext; - this.serializationContext = serializationContext; + + this.deserializationContext = new JsonDeserializationContext() { + public T deserialize(JsonElement json, Type typeOfT) throws JsonParseException { + return gson.fromJson(json, typeOfT); + } + }; + + this.serializationContext = new JsonSerializationContext() { + public JsonElement serialize(Object src) { + return gson.toJsonTree(src); + } + public JsonElement serialize(Object src, Type typeOfSrc) { + return gson.toJsonTree(src, typeOfSrc); + } + }; } public TypeAdapter create(final MiniGson context, final TypeToken typeToken) { diff --git a/gson/src/main/java/com/google/gson/JsonDeserializationContext.java b/gson/src/main/java/com/google/gson/JsonDeserializationContext.java index 1e4263f8..00c75054 100644 --- a/gson/src/main/java/com/google/gson/JsonDeserializationContext.java +++ b/gson/src/main/java/com/google/gson/JsonDeserializationContext.java @@ -26,38 +26,19 @@ import java.lang.reflect.Type; * @author Inderjeet Singh * @author Joel Leitch */ -public class JsonDeserializationContext { - private final Gson gson; - - JsonDeserializationContext(Gson gson) { - this.gson = gson; - } - - /** - * TODO: remove this from the public API - */ - @Deprecated - public T construct(Type type) { - throw new UnsupportedOperationException(); - } - - public Object constructArray(Type type, int length) { - throw new UnsupportedOperationException(); - } +public interface JsonDeserializationContext { /** * Invokes default deserialization on the specified object. It should never be invoked on * the element received as a parameter of the * {@link JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext)} method. Doing * so will result in an infinite loop since Gson will in-turn call the custom deserializer again. - + * * @param json the parse tree. * @param typeOfT type of the expected return value. * @param The type of the deserialized object. * @return An object of type typeOfT. * @throws JsonParseException if the parse tree does not contain expected data. */ - public T deserialize(JsonElement json, Type typeOfT) throws JsonParseException { - return gson.fromJson(json, typeOfT); - } + public T deserialize(JsonElement json, Type typeOfT) throws JsonParseException; } \ No newline at end of file diff --git a/gson/src/main/java/com/google/gson/JsonSerializationContext.java b/gson/src/main/java/com/google/gson/JsonSerializationContext.java index 83f88776..ca3ec4f9 100644 --- a/gson/src/main/java/com/google/gson/JsonSerializationContext.java +++ b/gson/src/main/java/com/google/gson/JsonSerializationContext.java @@ -25,13 +25,7 @@ import java.lang.reflect.Type; * @author Inderjeet Singh * @author Joel Leitch */ -public class JsonSerializationContext { - - private final Gson gson; - - JsonSerializationContext(Gson gson) { - this.gson = gson; - } +public interface JsonSerializationContext { /** * Invokes default serialization on the specified object. @@ -39,9 +33,7 @@ public class JsonSerializationContext { * @param src the object that needs to be serialized. * @return a tree of {@link JsonElement}s corresponding to the serialized form of {@code src}. */ - public JsonElement serialize(Object src) { - return gson.toJsonTree(src); - } + public JsonElement serialize(Object src); /** * Invokes default serialization on the specified object passing the specific type information. @@ -53,7 +45,5 @@ public class JsonSerializationContext { * @param typeOfSrc the actual genericized type of src object. * @return a tree of {@link JsonElement}s corresponding to the serialized form of {@code src}. */ - public JsonElement serialize(Object src, Type typeOfSrc) { - return gson.toJsonTree(src, typeOfSrc); - } -} \ No newline at end of file + public JsonElement serialize(Object src, Type typeOfSrc); +}