diff --git a/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java b/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java index c788ef54..201441f7 100644 --- a/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java +++ b/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java @@ -18,11 +18,15 @@ package com.google.gson.typeadapters; import com.google.gson.Gson; import com.google.gson.JsonElement; +import com.google.gson.JsonIOException; import com.google.gson.JsonObject; import com.google.gson.JsonParseException; import com.google.gson.JsonPrimitive; import com.google.gson.TypeAdapter; +import com.google.gson.TypeAdapterFactory; import com.google.gson.internal.Streams; +import com.google.gson.internal.bind.JsonElementWriter; +import com.google.gson.internal.bind.JsonTreeReader; import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; @@ -119,7 +123,7 @@ import java.util.Map; * .registerSubtype(Diamond.class); * } */ -public final class RuntimeTypeAdapterFactory implements TypeAdapter.Factory { +public final class RuntimeTypeAdapterFactory implements TypeAdapterFactory { private final Class baseType; private final String typeFieldName; private final Map> labelToSubtype = new LinkedHashMap>(); @@ -209,7 +213,18 @@ public final class RuntimeTypeAdapterFactory implements TypeAdapter.Factory { throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label + "; did you forget to register a subtype?"); } - return delegate.fromJsonTree(jsonElement); + return fromJsonTree(delegate, jsonElement); + } + + // TODO: remove this when TypeAdapter.fromJsonTree() is public + private T fromJsonTree(TypeAdapter delegate, JsonElement jsonTree) { + try { + JsonReader jsonReader = new JsonTreeReader(jsonTree); + jsonReader.setLenient(true); + return delegate.read(jsonReader); + } catch (IOException e) { + throw new JsonIOException(e); + } } @Override public void write(JsonWriter out, T value) throws IOException { @@ -221,7 +236,7 @@ public final class RuntimeTypeAdapterFactory implements TypeAdapter.Factory { throw new JsonParseException("cannot serialize " + srcType.getName() + "; did you forget to register a subtype?"); } - JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject(); + JsonObject jsonObject = toJsonTree(delegate, value).getAsJsonObject(); if (jsonObject.has(typeFieldName)) { throw new JsonParseException("cannot serialize " + srcType.getName() + " because it already defines a field named " + typeFieldName); @@ -233,6 +248,18 @@ public final class RuntimeTypeAdapterFactory implements TypeAdapter.Factory { } Streams.write(clone, out); } + + // TODO: remove this when TypeAdapter.toJsonTree() is public + private JsonElement toJsonTree(TypeAdapter delegate, T value) { + try { + JsonElementWriter jsonWriter = new JsonElementWriter(); + jsonWriter.setLenient(true); + delegate.write(jsonWriter, value); + return jsonWriter.get(); + } catch (IOException e) { + throw new JsonIOException(e); + } + } }; } } diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java index 83e21958..2a9eebdb 100644 --- a/gson/src/main/java/com/google/gson/Gson.java +++ b/gson/src/main/java/com/google/gson/Gson.java @@ -117,7 +117,7 @@ public final class Gson { private final Map, TypeAdapter> typeTokenCache = Collections.synchronizedMap(new HashMap, TypeAdapter>()); - private final List factories; + private final List factories; private final ConstructorConstructor constructorConstructor; private final boolean serializeNulls; @@ -179,7 +179,7 @@ public final class Gson { this(Excluder.DEFAULT, FieldNamingPolicy.IDENTITY, Collections.>emptyMap(), false, false, DEFAULT_JSON_NON_EXECUTABLE, true, false, false, LongSerializationPolicy.DEFAULT, - Collections.emptyList()); + Collections.emptyList()); } Gson(final Excluder excluder, final FieldNamingStrategy fieldNamingPolicy, @@ -187,18 +187,18 @@ public final class Gson { boolean complexMapKeySerialization, boolean generateNonExecutableGson, boolean htmlSafe, boolean prettyPrinting, boolean serializeSpecialFloatingPointValues, LongSerializationPolicy longSerializationPolicy, - List typeAdapterFactories) { + List typeAdapterFactories) { this.constructorConstructor = new ConstructorConstructor(instanceCreators); this.serializeNulls = serializeNulls; this.generateNonExecutableJson = generateNonExecutableGson; this.htmlSafe = htmlSafe; this.prettyPrinting = prettyPrinting; - TypeAdapter.Factory reflectiveTypeAdapterFactory = new ReflectiveTypeAdapterFactory( + TypeAdapterFactory reflectiveTypeAdapterFactory = new ReflectiveTypeAdapterFactory( constructorConstructor, fieldNamingPolicy, excluder); ConstructorConstructor constructorConstructor = new ConstructorConstructor(); - List factories = new ArrayList(); + List factories = new ArrayList(); // built-in type adapters that cannot be overridden factories.add(TypeAdapters.STRING_FACTORY); @@ -348,7 +348,7 @@ public final class Gson { FutureTypeAdapter call = new FutureTypeAdapter(); threadCalls.put(type, call); try { - for (TypeAdapter.Factory factory : factories) { + for (TypeAdapterFactory factory : factories) { TypeAdapter candidate = factory.create(this, type); if (candidate != null) { call.setDelegate(candidate); @@ -370,10 +370,10 @@ public final class Gson { * deserialize {@code type}. * @since 2.1 */ - public TypeAdapter getNextAdapter(TypeAdapter.Factory skipPast, TypeToken type) { + public TypeAdapter getNextAdapter(TypeAdapterFactory skipPast, TypeToken type) { boolean skipPastFound = false; - for (TypeAdapter.Factory factory : factories) { + for (TypeAdapterFactory factory : factories) { if (!skipPastFound) { if (factory == skipPast) { skipPastFound = true; diff --git a/gson/src/main/java/com/google/gson/GsonBuilder.java b/gson/src/main/java/com/google/gson/GsonBuilder.java index 008a2d3f..8d2f4b89 100644 --- a/gson/src/main/java/com/google/gson/GsonBuilder.java +++ b/gson/src/main/java/com/google/gson/GsonBuilder.java @@ -72,9 +72,9 @@ public final class GsonBuilder { private FieldNamingStrategy fieldNamingPolicy = FieldNamingPolicy.IDENTITY; private final Map> instanceCreators = new HashMap>(); - private final List factories = new ArrayList(); + private final List factories = new ArrayList(); /** tree-style hierarchy factories. These come after factories for backwards compatibility. */ - private final List hierarchyFactories = new ArrayList(); + private final List hierarchyFactories = new ArrayList(); private boolean serializeNulls; private String datePattern; private int dateStyle = DateFormat.DEFAULT; @@ -472,7 +472,7 @@ public final class GsonBuilder { * * @since 2.1 */ - public GsonBuilder registerTypeAdapterFactory(TypeAdapter.Factory factory) { + public GsonBuilder registerTypeAdapterFactory(TypeAdapterFactory factory) { factories.add(factory); return this; } @@ -540,7 +540,7 @@ public final class GsonBuilder { * @return an instance of Gson configured with the options currently set in this builder */ public Gson create() { - List factories = new ArrayList(); + List factories = new ArrayList(); factories.addAll(this.factories); Collections.reverse(factories); factories.addAll(this.hierarchyFactories); @@ -553,7 +553,7 @@ public final class GsonBuilder { } private void addTypeAdaptersForDate(String datePattern, int dateStyle, int timeStyle, - List factories) { + List factories) { DefaultDateTypeAdapter dateTypeAdapter; if (datePattern != null && !"".equals(datePattern.trim())) { dateTypeAdapter = new DefaultDateTypeAdapter(datePattern); diff --git a/gson/src/main/java/com/google/gson/TreeTypeAdapter.java b/gson/src/main/java/com/google/gson/TreeTypeAdapter.java index 035ef052..05f6f3ee 100644 --- a/gson/src/main/java/com/google/gson/TreeTypeAdapter.java +++ b/gson/src/main/java/com/google/gson/TreeTypeAdapter.java @@ -33,13 +33,13 @@ final class TreeTypeAdapter extends TypeAdapter { private final JsonDeserializer deserializer; private final Gson gson; private final TypeToken typeToken; - private final Factory skipPast; + private final TypeAdapterFactory skipPast; /** The delegate is lazily created because it may not be needed, and creating it may fail. */ private TypeAdapter delegate; private TreeTypeAdapter(JsonSerializer serializer, JsonDeserializer deserializer, - Gson gson, TypeToken typeToken, Factory skipPast) { + Gson gson, TypeToken typeToken, TypeAdapterFactory skipPast) { this.serializer = serializer; this.deserializer = deserializer; this.gson = gson; @@ -81,7 +81,7 @@ final class TreeTypeAdapter extends TypeAdapter { /** * Returns a new factory that will match each type against {@code exactType}. */ - public static Factory newFactory(TypeToken exactType, Object typeAdapter) { + public static TypeAdapterFactory newFactory(TypeToken exactType, Object typeAdapter) { return new SingleTypeFactory(typeAdapter, exactType, false, null); } @@ -89,7 +89,8 @@ final class TreeTypeAdapter extends TypeAdapter { * Returns a new factory that will match each type and its raw type against * {@code exactType}. */ - public static Factory newFactoryWithMatchRawType(TypeToken exactType, Object typeAdapter) { + public static TypeAdapterFactory newFactoryWithMatchRawType( + TypeToken exactType, Object typeAdapter) { // only bother matching raw types if exact type is a raw type boolean matchRawType = exactType.getType() == exactType.getRawType(); return new SingleTypeFactory(typeAdapter, exactType, matchRawType, null); @@ -99,11 +100,12 @@ final class TreeTypeAdapter extends TypeAdapter { * Returns a new factory that will match each type's raw type for assignability * to {@code hierarchyType}. */ - public static Factory newTypeHierarchyFactory(Class hierarchyType, Object typeAdapter) { + public static TypeAdapterFactory newTypeHierarchyFactory( + Class hierarchyType, Object typeAdapter) { return new SingleTypeFactory(typeAdapter, null, false, hierarchyType); } - private static class SingleTypeFactory implements TypeAdapter.Factory { + private static class SingleTypeFactory implements TypeAdapterFactory { private final TypeToken exactType; private final boolean matchRawType; private final Class hierarchyType; diff --git a/gson/src/main/java/com/google/gson/TypeAdapter.java b/gson/src/main/java/com/google/gson/TypeAdapter.java index 6477b55d..4ca939b5 100644 --- a/gson/src/main/java/com/google/gson/TypeAdapter.java +++ b/gson/src/main/java/com/google/gson/TypeAdapter.java @@ -281,155 +281,4 @@ public abstract class TypeAdapter { throw new JsonIOException(e); } } - - /** - * Creates type adapters for set of related types. Type adapter factories are - * most useful when several types share similar structure in their JSON form. - * - *

Example: Converting enums to lowercase

- * In this example, we implement a factory that creates type adapters for all - * enums. The type adapters will write enums in lowercase, despite the fact - * that they're defined in {@code CONSTANT_CASE} in the corresponding Java - * model:
   {@code
-   *
-   *   public class LowercaseEnumTypeAdapterFactory implements TypeAdapter.Factory {
-   *     public  TypeAdapter create(Gson gson, TypeToken type) {
-   *       Class rawType = (Class) type.getRawType();
-   *       if (!rawType.isEnum()) {
-   *         return null;
-   *       }
-   *
-   *       final Map lowercaseToConstant = new HashMap();
-   *       for (T constant : rawType.getEnumConstants()) {
-   *         lowercaseToConstant.put(toLowercase(constant), constant);
-   *       }
-   *
-   *       return new TypeAdapter() {
-   *         public void write(JsonWriter out, T value) throws IOException {
-   *           if (value == null) {
-   *             out.nullValue();
-   *           } else {
-   *             out.value(toLowercase(value));
-   *           }
-   *         }
-   *
-   *         public T read(JsonReader reader) throws IOException {
-   *           if (reader.peek() == JsonToken.NULL) {
-   *             reader.nextNull();
-   *             return null;
-   *           } else {
-   *             return lowercaseToConstant.get(reader.nextString());
-   *           }
-   *         }
-   *       };
-   *     }
-   *
-   *     private String toLowercase(Object o) {
-   *       return o.toString().toLowerCase(Locale.US);
-   *     }
-   *   }
-   * }
- * - *

Type adapter factories select which types they provide type adapters - * for. If a factory cannot support a given type, it must return null when - * that type is passed to {@link #create}. Factories should expect {@code - * create()} to be called on them for many types and should return null for - * most of those types. In the above example the factory returns null for - * calls to {@code create()} where {@code type} is not an enum. - * - *

A factory is typically called once per type, but the returned type - * adapter may be used many times. It is most efficient to do expensive work - * like reflection in {@code create()} so that the type adapter's {@code - * read()} and {@code write()} methods can be very fast. In this example the - * mapping from lowercase name to enum value is computed eagerly. - * - *

As with type adapters, factories must be registered with a {@link - * GsonBuilder} for them to take effect:

   {@code
-   *
-   *  GsonBuilder builder = new GsonBuilder();
-   *  builder.registerTypeAdapterFactory(new LowercaseEnumTypeAdapterFactory());
-   *  ...
-   *  Gson gson = builder.create();
-   * }
- * If multiple factories support the same type, the factory registered earlier - * takes precedence. - * - *

Example: composing other type adapters

- * In this example we implement a factory for Guava's {@code Multiset} - * collection type. The factory can be used to create type adapters for - * multisets of any element type: the type adapter for {@code - * Multiset} is different from the type adapter for {@code - * Multiset}. - * - *

The type adapter delegates to another type adapter for the - * multiset elements. It figures out the element type by reflecting on the - * multiset's type token. A {@code Gson} is passed in to {@code create} for - * just this purpose:

   {@code
-   *
-   *   public class MultisetTypeAdapterFactory implements TypeAdapter.Factory {
-   *     public  TypeAdapter create(Gson gson, TypeToken typeToken) {
-   *       Type type = typeToken.getType();
-   *       if (typeToken.getRawType() != Multiset.class
-   *           || !(type instanceof ParameterizedType)) {
-   *         return null;
-   *       }
-   *
-   *       Type elementType = ((ParameterizedType) type).getActualTypeArguments()[0];
-   *       TypeAdapter elementAdapter = gson.getAdapter(TypeToken.get(elementType));
-   *       return (TypeAdapter) newMultisetAdapter(elementAdapter);
-   *     }
-   *
-   *     private  TypeAdapter> newMultisetAdapter(
-   *         final TypeAdapter elementAdapter) {
-   *       return new TypeAdapter>() {
-   *         public void write(JsonWriter out, Multiset value) throws IOException {
-   *           if (value == null) {
-   *             out.nullValue();
-   *             return;
-   *           }
-   *
-   *           out.beginArray();
-   *           for (Multiset.Entry entry : value.entrySet()) {
-   *             out.value(entry.getCount());
-   *             elementAdapter.write(out, entry.getElement());
-   *           }
-   *           out.endArray();
-   *         }
-   *
-   *         public Multiset read(JsonReader in) throws IOException {
-   *           if (in.peek() == JsonToken.NULL) {
-   *             in.nextNull();
-   *             return null;
-   *           }
-   *
-   *           Multiset result = LinkedHashMultiset.create();
-   *           in.beginArray();
-   *           while (in.hasNext()) {
-   *             int count = in.nextInt();
-   *             E element = elementAdapter.read(in);
-   *             result.add(element, count);
-   *           }
-   *           in.endArray();
-   *           return result;
-   *         }
-   *       };
-   *     }
-   *   }
-   * }
- * Delegating from one type adapter to another is extremely powerful; it's - * the foundation of how Gson converts Java objects and collections. Whenever - * possible your factory should retrieve its delegate type adapter in the - * {@code create()} method; this ensures potentially-expensive type adapter - * creation happens only once. - * - * @since 2.1 - */ - public interface Factory { - - /** - * Returns a type adapter for {@code type}, or null if this factory doesn't - * support {@code type}. - */ - TypeAdapter create(Gson gson, TypeToken type); - } } diff --git a/gson/src/main/java/com/google/gson/TypeAdapterFactory.java b/gson/src/main/java/com/google/gson/TypeAdapterFactory.java new file mode 100644 index 00000000..536427f4 --- /dev/null +++ b/gson/src/main/java/com/google/gson/TypeAdapterFactory.java @@ -0,0 +1,170 @@ +/* + * Copyright (C) 2011 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.gson; + +import com.google.gson.reflect.TypeToken; + +/** + * Creates type adapters for set of related types. Type adapter factories are + * most useful when several types share similar structure in their JSON form. + * + *

Example: Converting enums to lowercase

+ * In this example, we implement a factory that creates type adapters for all + * enums. The type adapters will write enums in lowercase, despite the fact + * that they're defined in {@code CONSTANT_CASE} in the corresponding Java + * model:
   {@code
+ *
+ *   public class LowercaseEnumTypeAdapterFactory implements TypeAdapter.Factory {
+ *     public  TypeAdapter create(Gson gson, TypeToken type) {
+ *       Class rawType = (Class) type.getRawType();
+ *       if (!rawType.isEnum()) {
+ *         return null;
+ *       }
+ *
+ *       final Map lowercaseToConstant = new HashMap();
+ *       for (T constant : rawType.getEnumConstants()) {
+ *         lowercaseToConstant.put(toLowercase(constant), constant);
+ *       }
+ *
+ *       return new TypeAdapter() {
+ *         public void write(JsonWriter out, T value) throws IOException {
+ *           if (value == null) {
+ *             out.nullValue();
+ *           } else {
+ *             out.value(toLowercase(value));
+ *           }
+ *         }
+ *
+ *         public T read(JsonReader reader) throws IOException {
+ *           if (reader.peek() == JsonToken.NULL) {
+ *             reader.nextNull();
+ *             return null;
+ *           } else {
+ *             return lowercaseToConstant.get(reader.nextString());
+ *           }
+ *         }
+ *       };
+ *     }
+ *
+ *     private String toLowercase(Object o) {
+ *       return o.toString().toLowerCase(Locale.US);
+ *     }
+ *   }
+ * }
+ * + *

Type adapter factories select which types they provide type adapters + * for. If a factory cannot support a given type, it must return null when + * that type is passed to {@link #create}. Factories should expect {@code + * create()} to be called on them for many types and should return null for + * most of those types. In the above example the factory returns null for + * calls to {@code create()} where {@code type} is not an enum. + * + *

A factory is typically called once per type, but the returned type + * adapter may be used many times. It is most efficient to do expensive work + * like reflection in {@code create()} so that the type adapter's {@code + * read()} and {@code write()} methods can be very fast. In this example the + * mapping from lowercase name to enum value is computed eagerly. + * + *

As with type adapters, factories must be registered with a {@link + * com.google.gson.GsonBuilder} for them to take effect:

   {@code
+ *
+ *  GsonBuilder builder = new GsonBuilder();
+ *  builder.registerTypeAdapterFactory(new LowercaseEnumTypeAdapterFactory());
+ *  ...
+ *  Gson gson = builder.create();
+ * }
+ * If multiple factories support the same type, the factory registered earlier + * takes precedence. + * + *

Example: composing other type adapters

+ * In this example we implement a factory for Guava's {@code Multiset} + * collection type. The factory can be used to create type adapters for + * multisets of any element type: the type adapter for {@code + * Multiset} is different from the type adapter for {@code + * Multiset}. + * + *

The type adapter delegates to another type adapter for the + * multiset elements. It figures out the element type by reflecting on the + * multiset's type token. A {@code Gson} is passed in to {@code create} for + * just this purpose:

   {@code
+ *
+ *   public class MultisetTypeAdapterFactory implements TypeAdapter.Factory {
+ *     public  TypeAdapter create(Gson gson, TypeToken typeToken) {
+ *       Type type = typeToken.getType();
+ *       if (typeToken.getRawType() != Multiset.class
+ *           || !(type instanceof ParameterizedType)) {
+ *         return null;
+ *       }
+ *
+ *       Type elementType = ((ParameterizedType) type).getActualTypeArguments()[0];
+ *       TypeAdapter elementAdapter = gson.getAdapter(TypeToken.get(elementType));
+ *       return (TypeAdapter) newMultisetAdapter(elementAdapter);
+ *     }
+ *
+ *     private  TypeAdapter> newMultisetAdapter(
+ *         final TypeAdapter elementAdapter) {
+ *       return new TypeAdapter>() {
+ *         public void write(JsonWriter out, Multiset value) throws IOException {
+ *           if (value == null) {
+ *             out.nullValue();
+ *             return;
+ *           }
+ *
+ *           out.beginArray();
+ *           for (Multiset.Entry entry : value.entrySet()) {
+ *             out.value(entry.getCount());
+ *             elementAdapter.write(out, entry.getElement());
+ *           }
+ *           out.endArray();
+ *         }
+ *
+ *         public Multiset read(JsonReader in) throws IOException {
+ *           if (in.peek() == JsonToken.NULL) {
+ *             in.nextNull();
+ *             return null;
+ *           }
+ *
+ *           Multiset result = LinkedHashMultiset.create();
+ *           in.beginArray();
+ *           while (in.hasNext()) {
+ *             int count = in.nextInt();
+ *             E element = elementAdapter.read(in);
+ *             result.add(element, count);
+ *           }
+ *           in.endArray();
+ *           return result;
+ *         }
+ *       };
+ *     }
+ *   }
+ * }
+ * Delegating from one type adapter to another is extremely powerful; it's + * the foundation of how Gson converts Java objects and collections. Whenever + * possible your factory should retrieve its delegate type adapter in the + * {@code create()} method; this ensures potentially-expensive type adapter + * creation happens only once. + * + * @since 2.1 + */ +public interface TypeAdapterFactory { + + /** + * Returns a type adapter for {@code type}, or null if this factory doesn't + * support {@code type}. + */ + TypeAdapter create(Gson gson, TypeToken type); +} diff --git a/gson/src/main/java/com/google/gson/internal/Excluder.java b/gson/src/main/java/com/google/gson/internal/Excluder.java index 70dad864..dce4f1d7 100644 --- a/gson/src/main/java/com/google/gson/internal/Excluder.java +++ b/gson/src/main/java/com/google/gson/internal/Excluder.java @@ -20,6 +20,7 @@ import com.google.gson.ExclusionStrategy; import com.google.gson.FieldAttributes; import com.google.gson.Gson; import com.google.gson.TypeAdapter; +import com.google.gson.TypeAdapterFactory; import com.google.gson.annotations.Expose; import com.google.gson.annotations.Since; import com.google.gson.annotations.Until; @@ -46,7 +47,7 @@ import java.util.List; * @author Joel Leitch * @author Jesse Wilson */ -public final class Excluder implements TypeAdapter.Factory, Cloneable { +public final class Excluder implements TypeAdapterFactory, Cloneable { private static final double IGNORE_VERSIONS = -1.0d; public static final Excluder DEFAULT = new Excluder(); diff --git a/gson/src/main/java/com/google/gson/internal/bind/ArrayTypeAdapter.java b/gson/src/main/java/com/google/gson/internal/bind/ArrayTypeAdapter.java index 6820a107..39ab7628 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/ArrayTypeAdapter.java +++ b/gson/src/main/java/com/google/gson/internal/bind/ArrayTypeAdapter.java @@ -18,6 +18,7 @@ package com.google.gson.internal.bind; import com.google.gson.Gson; import com.google.gson.TypeAdapter; +import com.google.gson.TypeAdapterFactory; import java.io.IOException; import java.lang.reflect.Array; import java.lang.reflect.GenericArrayType; @@ -35,7 +36,7 @@ import com.google.gson.stream.JsonWriter; * Adapt an array of objects. */ public final class ArrayTypeAdapter extends TypeAdapter { - public static final Factory FACTORY = new Factory() { + public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() { @SuppressWarnings({"unchecked", "rawtypes"}) public TypeAdapter create(Gson gson, TypeToken typeToken) { Type type = typeToken.getType(); diff --git a/gson/src/main/java/com/google/gson/internal/bind/CollectionTypeAdapterFactory.java b/gson/src/main/java/com/google/gson/internal/bind/CollectionTypeAdapterFactory.java index c635e279..d1bed753 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/CollectionTypeAdapterFactory.java +++ b/gson/src/main/java/com/google/gson/internal/bind/CollectionTypeAdapterFactory.java @@ -18,6 +18,7 @@ package com.google.gson.internal.bind; import com.google.gson.Gson; import com.google.gson.TypeAdapter; +import com.google.gson.TypeAdapterFactory; import com.google.gson.internal.$Gson$Types; import com.google.gson.internal.ConstructorConstructor; import com.google.gson.internal.ObjectConstructor; @@ -32,7 +33,7 @@ import java.util.Collection; /** * Adapt a homogeneous collection of objects. */ -public final class CollectionTypeAdapterFactory implements TypeAdapter.Factory { +public final class CollectionTypeAdapterFactory implements TypeAdapterFactory { private final ConstructorConstructor constructorConstructor; public CollectionTypeAdapterFactory(ConstructorConstructor constructorConstructor) { diff --git a/gson/src/main/java/com/google/gson/internal/bind/DateTypeAdapter.java b/gson/src/main/java/com/google/gson/internal/bind/DateTypeAdapter.java index bb17a886..f2571724 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/DateTypeAdapter.java +++ b/gson/src/main/java/com/google/gson/internal/bind/DateTypeAdapter.java @@ -19,6 +19,7 @@ package com.google.gson.internal.bind; import com.google.gson.Gson; import com.google.gson.JsonSyntaxException; import com.google.gson.TypeAdapter; +import com.google.gson.TypeAdapterFactory; import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonToken; @@ -38,7 +39,7 @@ import java.util.TimeZone; * to synchronize its read and write methods. */ public final class DateTypeAdapter extends TypeAdapter { - public static final Factory FACTORY = new Factory() { + public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() { @SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal public TypeAdapter create(Gson gson, TypeToken typeToken) { return typeToken.getRawType() == Date.class ? (TypeAdapter) new DateTypeAdapter() : null; diff --git a/gson/src/main/java/com/google/gson/internal/bind/MapTypeAdapterFactory.java b/gson/src/main/java/com/google/gson/internal/bind/MapTypeAdapterFactory.java index 7e395089..27be1d70 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/MapTypeAdapterFactory.java +++ b/gson/src/main/java/com/google/gson/internal/bind/MapTypeAdapterFactory.java @@ -22,6 +22,7 @@ import com.google.gson.JsonIOException; import com.google.gson.JsonPrimitive; import com.google.gson.JsonSyntaxException; import com.google.gson.TypeAdapter; +import com.google.gson.TypeAdapterFactory; import com.google.gson.internal.$Gson$Types; import com.google.gson.internal.ConstructorConstructor; import com.google.gson.internal.JsonReaderInternalAccess; @@ -102,7 +103,7 @@ import java.util.Map; * This format will serialize and deserialize just fine as long as this adapter * is registered. */ -public final class MapTypeAdapterFactory implements TypeAdapter.Factory { +public final class MapTypeAdapterFactory implements TypeAdapterFactory { private final ConstructorConstructor constructorConstructor; private final boolean complexMapKeySerialization; diff --git a/gson/src/main/java/com/google/gson/internal/bind/ObjectTypeAdapter.java b/gson/src/main/java/com/google/gson/internal/bind/ObjectTypeAdapter.java index 76e643d3..6d36778c 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/ObjectTypeAdapter.java +++ b/gson/src/main/java/com/google/gson/internal/bind/ObjectTypeAdapter.java @@ -18,6 +18,7 @@ package com.google.gson.internal.bind; import com.google.gson.Gson; import com.google.gson.TypeAdapter; +import com.google.gson.TypeAdapterFactory; import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonToken; @@ -33,7 +34,7 @@ import java.util.Map; * serialization and a primitive/Map/List on deserialization. */ public final class ObjectTypeAdapter extends TypeAdapter { - public static final Factory FACTORY = new Factory() { + public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() { @SuppressWarnings("unchecked") public TypeAdapter create(Gson gson, TypeToken type) { if (type.getRawType() == Object.class) { diff --git a/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapterFactory.java b/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapterFactory.java index 0a2b5c68..8ec8974a 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapterFactory.java +++ b/gson/src/main/java/com/google/gson/internal/bind/ReflectiveTypeAdapterFactory.java @@ -20,6 +20,7 @@ import com.google.gson.FieldNamingStrategy; import com.google.gson.Gson; import com.google.gson.JsonSyntaxException; import com.google.gson.TypeAdapter; +import com.google.gson.TypeAdapterFactory; import com.google.gson.annotations.SerializedName; import com.google.gson.internal.$Gson$Types; import com.google.gson.internal.ConstructorConstructor; @@ -40,7 +41,7 @@ import java.util.Map; /** * Type adapter that reflects over the fields and methods of a class. */ -public final class ReflectiveTypeAdapterFactory implements TypeAdapter.Factory { +public final class ReflectiveTypeAdapterFactory implements TypeAdapterFactory { private final ConstructorConstructor constructorConstructor; private final FieldNamingStrategy fieldNamingPolicy; private final Excluder excluder; diff --git a/gson/src/main/java/com/google/gson/internal/bind/SqlDateTypeAdapter.java b/gson/src/main/java/com/google/gson/internal/bind/SqlDateTypeAdapter.java index 8a4c6582..5ecc2e96 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/SqlDateTypeAdapter.java +++ b/gson/src/main/java/com/google/gson/internal/bind/SqlDateTypeAdapter.java @@ -19,6 +19,7 @@ package com.google.gson.internal.bind; import com.google.gson.Gson; import com.google.gson.JsonSyntaxException; import com.google.gson.TypeAdapter; +import com.google.gson.TypeAdapterFactory; import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonToken; @@ -35,7 +36,7 @@ import java.text.SimpleDateFormat; * to synchronize its read and write methods. */ public final class SqlDateTypeAdapter extends TypeAdapter { - public static final Factory FACTORY = new Factory() { + public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() { @SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal public TypeAdapter create(Gson gson, TypeToken typeToken) { return typeToken.getRawType() == java.sql.Date.class diff --git a/gson/src/main/java/com/google/gson/internal/bind/TimeTypeAdapter.java b/gson/src/main/java/com/google/gson/internal/bind/TimeTypeAdapter.java index deaa6eac..bbbb4d97 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/TimeTypeAdapter.java +++ b/gson/src/main/java/com/google/gson/internal/bind/TimeTypeAdapter.java @@ -19,6 +19,7 @@ package com.google.gson.internal.bind; import com.google.gson.Gson; import com.google.gson.JsonSyntaxException; import com.google.gson.TypeAdapter; +import com.google.gson.TypeAdapterFactory; import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonToken; @@ -37,7 +38,7 @@ import java.util.Date; * to synchronize its read and write methods. */ public final class TimeTypeAdapter extends TypeAdapter