Rename TypeAdapter.Factory to TypeAdapterFactory.
This commit is contained in:
parent
2cd3f9707f
commit
d7fbac0384
@ -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);
|
||||
* }</pre>
|
||||
*/
|
||||
public final class RuntimeTypeAdapterFactory<T> implements TypeAdapter.Factory {
|
||||
public final class RuntimeTypeAdapterFactory<T> implements TypeAdapterFactory {
|
||||
private final Class<?> baseType;
|
||||
private final String typeFieldName;
|
||||
private final Map<String, Class<?>> labelToSubtype = new LinkedHashMap<String, Class<?>>();
|
||||
@ -209,7 +213,18 @@ public final class RuntimeTypeAdapterFactory<T> 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<T> 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<T> 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<T> implements TypeAdapter.Factory {
|
||||
}
|
||||
Streams.write(clone, out);
|
||||
}
|
||||
|
||||
// TODO: remove this when TypeAdapter.toJsonTree() is public
|
||||
private JsonElement toJsonTree(TypeAdapter<T> 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);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -117,7 +117,7 @@ public final class Gson {
|
||||
private final Map<TypeToken<?>, TypeAdapter<?>> typeTokenCache
|
||||
= Collections.synchronizedMap(new HashMap<TypeToken<?>, TypeAdapter<?>>());
|
||||
|
||||
private final List<TypeAdapter.Factory> factories;
|
||||
private final List<TypeAdapterFactory> factories;
|
||||
private final ConstructorConstructor constructorConstructor;
|
||||
|
||||
private final boolean serializeNulls;
|
||||
@ -179,7 +179,7 @@ public final class Gson {
|
||||
this(Excluder.DEFAULT, FieldNamingPolicy.IDENTITY,
|
||||
Collections.<Type, InstanceCreator<?>>emptyMap(), false, false, DEFAULT_JSON_NON_EXECUTABLE,
|
||||
true, false, false, LongSerializationPolicy.DEFAULT,
|
||||
Collections.<TypeAdapter.Factory>emptyList());
|
||||
Collections.<TypeAdapterFactory>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<TypeAdapter.Factory> typeAdapterFactories) {
|
||||
List<TypeAdapterFactory> 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<TypeAdapter.Factory> factories = new ArrayList<TypeAdapter.Factory>();
|
||||
List<TypeAdapterFactory> factories = new ArrayList<TypeAdapterFactory>();
|
||||
|
||||
// built-in type adapters that cannot be overridden
|
||||
factories.add(TypeAdapters.STRING_FACTORY);
|
||||
@ -348,7 +348,7 @@ public final class Gson {
|
||||
FutureTypeAdapter<T> call = new FutureTypeAdapter<T>();
|
||||
threadCalls.put(type, call);
|
||||
try {
|
||||
for (TypeAdapter.Factory factory : factories) {
|
||||
for (TypeAdapterFactory factory : factories) {
|
||||
TypeAdapter<T> 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 <T> TypeAdapter<T> getNextAdapter(TypeAdapter.Factory skipPast, TypeToken<T> type) {
|
||||
public <T> TypeAdapter<T> getNextAdapter(TypeAdapterFactory skipPast, TypeToken<T> type) {
|
||||
boolean skipPastFound = false;
|
||||
|
||||
for (TypeAdapter.Factory factory : factories) {
|
||||
for (TypeAdapterFactory factory : factories) {
|
||||
if (!skipPastFound) {
|
||||
if (factory == skipPast) {
|
||||
skipPastFound = true;
|
||||
|
@ -72,9 +72,9 @@ public final class GsonBuilder {
|
||||
private FieldNamingStrategy fieldNamingPolicy = FieldNamingPolicy.IDENTITY;
|
||||
private final Map<Type, InstanceCreator<?>> instanceCreators
|
||||
= new HashMap<Type, InstanceCreator<?>>();
|
||||
private final List<TypeAdapter.Factory> factories = new ArrayList<TypeAdapter.Factory>();
|
||||
private final List<TypeAdapterFactory> factories = new ArrayList<TypeAdapterFactory>();
|
||||
/** tree-style hierarchy factories. These come after factories for backwards compatibility. */
|
||||
private final List<TypeAdapter.Factory> hierarchyFactories = new ArrayList<TypeAdapter.Factory>();
|
||||
private final List<TypeAdapterFactory> hierarchyFactories = new ArrayList<TypeAdapterFactory>();
|
||||
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<TypeAdapter.Factory> factories = new ArrayList<TypeAdapter.Factory>();
|
||||
List<TypeAdapterFactory> factories = new ArrayList<TypeAdapterFactory>();
|
||||
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<TypeAdapter.Factory> factories) {
|
||||
List<TypeAdapterFactory> factories) {
|
||||
DefaultDateTypeAdapter dateTypeAdapter;
|
||||
if (datePattern != null && !"".equals(datePattern.trim())) {
|
||||
dateTypeAdapter = new DefaultDateTypeAdapter(datePattern);
|
||||
|
@ -33,13 +33,13 @@ final class TreeTypeAdapter<T> extends TypeAdapter<T> {
|
||||
private final JsonDeserializer<T> deserializer;
|
||||
private final Gson gson;
|
||||
private final TypeToken<T> 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<T> delegate;
|
||||
|
||||
private TreeTypeAdapter(JsonSerializer<T> serializer, JsonDeserializer<T> deserializer,
|
||||
Gson gson, TypeToken<T> typeToken, Factory skipPast) {
|
||||
Gson gson, TypeToken<T> typeToken, TypeAdapterFactory skipPast) {
|
||||
this.serializer = serializer;
|
||||
this.deserializer = deserializer;
|
||||
this.gson = gson;
|
||||
@ -81,7 +81,7 @@ final class TreeTypeAdapter<T> extends TypeAdapter<T> {
|
||||
/**
|
||||
* 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<T> extends TypeAdapter<T> {
|
||||
* 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<T> extends TypeAdapter<T> {
|
||||
* 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;
|
||||
|
@ -281,155 +281,4 @@ public abstract class TypeAdapter<T> {
|
||||
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.
|
||||
*
|
||||
* <h3>Example: Converting enums to lowercase</h3>
|
||||
* 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: <pre> {@code
|
||||
*
|
||||
* public class LowercaseEnumTypeAdapterFactory implements TypeAdapter.Factory {
|
||||
* public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
|
||||
* Class<T> rawType = (Class<T>) type.getRawType();
|
||||
* if (!rawType.isEnum()) {
|
||||
* return null;
|
||||
* }
|
||||
*
|
||||
* final Map<String, T> lowercaseToConstant = new HashMap<String, T>();
|
||||
* for (T constant : rawType.getEnumConstants()) {
|
||||
* lowercaseToConstant.put(toLowercase(constant), constant);
|
||||
* }
|
||||
*
|
||||
* return new TypeAdapter<T>() {
|
||||
* 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);
|
||||
* }
|
||||
* }
|
||||
* }</pre>
|
||||
*
|
||||
* <p>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.
|
||||
*
|
||||
* <p>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.
|
||||
*
|
||||
* <p>As with type adapters, factories must be <i>registered</i> with a {@link
|
||||
* GsonBuilder} for them to take effect: <pre> {@code
|
||||
*
|
||||
* GsonBuilder builder = new GsonBuilder();
|
||||
* builder.registerTypeAdapterFactory(new LowercaseEnumTypeAdapterFactory());
|
||||
* ...
|
||||
* Gson gson = builder.create();
|
||||
* }</pre>
|
||||
* If multiple factories support the same type, the factory registered earlier
|
||||
* takes precedence.
|
||||
*
|
||||
* <h3>Example: composing other type adapters</h3>
|
||||
* 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<String>} is different from the type adapter for {@code
|
||||
* Multiset<URL>}.
|
||||
*
|
||||
* <p>The type adapter <i>delegates</i> 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: <pre> {@code
|
||||
*
|
||||
* public class MultisetTypeAdapterFactory implements TypeAdapter.Factory {
|
||||
* public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> 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<T>) newMultisetAdapter(elementAdapter);
|
||||
* }
|
||||
*
|
||||
* private <E> TypeAdapter<Multiset<E>> newMultisetAdapter(
|
||||
* final TypeAdapter<E> elementAdapter) {
|
||||
* return new TypeAdapter<Multiset<E>>() {
|
||||
* public void write(JsonWriter out, Multiset<E> value) throws IOException {
|
||||
* if (value == null) {
|
||||
* out.nullValue();
|
||||
* return;
|
||||
* }
|
||||
*
|
||||
* out.beginArray();
|
||||
* for (Multiset.Entry<E> entry : value.entrySet()) {
|
||||
* out.value(entry.getCount());
|
||||
* elementAdapter.write(out, entry.getElement());
|
||||
* }
|
||||
* out.endArray();
|
||||
* }
|
||||
*
|
||||
* public Multiset<E> read(JsonReader in) throws IOException {
|
||||
* if (in.peek() == JsonToken.NULL) {
|
||||
* in.nextNull();
|
||||
* return null;
|
||||
* }
|
||||
*
|
||||
* Multiset<E> 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;
|
||||
* }
|
||||
* };
|
||||
* }
|
||||
* }
|
||||
* }</pre>
|
||||
* 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}.
|
||||
*/
|
||||
<T> TypeAdapter<T> create(Gson gson, TypeToken<T> type);
|
||||
}
|
||||
}
|
||||
|
170
gson/src/main/java/com/google/gson/TypeAdapterFactory.java
Normal file
170
gson/src/main/java/com/google/gson/TypeAdapterFactory.java
Normal file
@ -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.
|
||||
*
|
||||
* <h3>Example: Converting enums to lowercase</h3>
|
||||
* 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: <pre> {@code
|
||||
*
|
||||
* public class LowercaseEnumTypeAdapterFactory implements TypeAdapter.Factory {
|
||||
* public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
|
||||
* Class<T> rawType = (Class<T>) type.getRawType();
|
||||
* if (!rawType.isEnum()) {
|
||||
* return null;
|
||||
* }
|
||||
*
|
||||
* final Map<String, T> lowercaseToConstant = new HashMap<String, T>();
|
||||
* for (T constant : rawType.getEnumConstants()) {
|
||||
* lowercaseToConstant.put(toLowercase(constant), constant);
|
||||
* }
|
||||
*
|
||||
* return new TypeAdapter<T>() {
|
||||
* 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);
|
||||
* }
|
||||
* }
|
||||
* }</pre>
|
||||
*
|
||||
* <p>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.
|
||||
*
|
||||
* <p>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.
|
||||
*
|
||||
* <p>As with type adapters, factories must be <i>registered</i> with a {@link
|
||||
* com.google.gson.GsonBuilder} for them to take effect: <pre> {@code
|
||||
*
|
||||
* GsonBuilder builder = new GsonBuilder();
|
||||
* builder.registerTypeAdapterFactory(new LowercaseEnumTypeAdapterFactory());
|
||||
* ...
|
||||
* Gson gson = builder.create();
|
||||
* }</pre>
|
||||
* If multiple factories support the same type, the factory registered earlier
|
||||
* takes precedence.
|
||||
*
|
||||
* <h3>Example: composing other type adapters</h3>
|
||||
* 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<String>} is different from the type adapter for {@code
|
||||
* Multiset<URL>}.
|
||||
*
|
||||
* <p>The type adapter <i>delegates</i> 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: <pre> {@code
|
||||
*
|
||||
* public class MultisetTypeAdapterFactory implements TypeAdapter.Factory {
|
||||
* public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> 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<T>) newMultisetAdapter(elementAdapter);
|
||||
* }
|
||||
*
|
||||
* private <E> TypeAdapter<Multiset<E>> newMultisetAdapter(
|
||||
* final TypeAdapter<E> elementAdapter) {
|
||||
* return new TypeAdapter<Multiset<E>>() {
|
||||
* public void write(JsonWriter out, Multiset<E> value) throws IOException {
|
||||
* if (value == null) {
|
||||
* out.nullValue();
|
||||
* return;
|
||||
* }
|
||||
*
|
||||
* out.beginArray();
|
||||
* for (Multiset.Entry<E> entry : value.entrySet()) {
|
||||
* out.value(entry.getCount());
|
||||
* elementAdapter.write(out, entry.getElement());
|
||||
* }
|
||||
* out.endArray();
|
||||
* }
|
||||
*
|
||||
* public Multiset<E> read(JsonReader in) throws IOException {
|
||||
* if (in.peek() == JsonToken.NULL) {
|
||||
* in.nextNull();
|
||||
* return null;
|
||||
* }
|
||||
*
|
||||
* Multiset<E> 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;
|
||||
* }
|
||||
* };
|
||||
* }
|
||||
* }
|
||||
* }</pre>
|
||||
* 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}.
|
||||
*/
|
||||
<T> TypeAdapter<T> create(Gson gson, TypeToken<T> type);
|
||||
}
|
@ -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();
|
||||
|
||||
|
@ -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<E> extends TypeAdapter<Object> {
|
||||
public static final Factory FACTORY = new Factory() {
|
||||
public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
|
||||
Type type = typeToken.getType();
|
||||
|
@ -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) {
|
||||
|
@ -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<Date> {
|
||||
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 <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
|
||||
return typeToken.getRawType() == Date.class ? (TypeAdapter<T>) new DateTypeAdapter() : null;
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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<Object> {
|
||||
public static final Factory FACTORY = new Factory() {
|
||||
public static final TypeAdapterFactory FACTORY = new TypeAdapterFactory() {
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
|
||||
if (type.getRawType() == Object.class) {
|
||||
|
@ -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;
|
||||
|
@ -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<java.sql.Date> {
|
||||
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 <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
|
||||
return typeToken.getRawType() == java.sql.Date.class
|
||||
|
@ -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<Time> {
|
||||
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 <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
|
||||
return typeToken.getRawType() == Time.class ? (TypeAdapter<T>) new TimeTypeAdapter() : null;
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
package com.google.gson.internal.bind;
|
||||
|
||||
import com.google.gson.TypeAdapterFactory;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.URI;
|
||||
@ -67,7 +68,7 @@ public final class TypeAdapters {
|
||||
"Attempted to deserialize a java.lang.Class. Forgot to register a type adapter?");
|
||||
}
|
||||
};
|
||||
public static final TypeAdapter.Factory CLASS_FACTORY = newFactory(Class.class, CLASS);
|
||||
public static final TypeAdapterFactory CLASS_FACTORY = newFactory(Class.class, CLASS);
|
||||
|
||||
public static final TypeAdapter<BitSet> BIT_SET = new TypeAdapter<BitSet>() {
|
||||
public BitSet read(JsonReader in) throws IOException {
|
||||
@ -126,7 +127,7 @@ public final class TypeAdapters {
|
||||
}
|
||||
};
|
||||
|
||||
public static final TypeAdapter.Factory BIT_SET_FACTORY = newFactory(BitSet.class, BIT_SET);
|
||||
public static final TypeAdapterFactory BIT_SET_FACTORY = newFactory(BitSet.class, BIT_SET);
|
||||
|
||||
public static final TypeAdapter<Boolean> BOOLEAN = new TypeAdapter<Boolean>() {
|
||||
@Override
|
||||
@ -168,7 +169,7 @@ public final class TypeAdapters {
|
||||
}
|
||||
};
|
||||
|
||||
public static final TypeAdapter.Factory BOOLEAN_FACTORY
|
||||
public static final TypeAdapterFactory BOOLEAN_FACTORY
|
||||
= newFactory(boolean.class, Boolean.class, BOOLEAN);
|
||||
|
||||
public static final TypeAdapter<Number> BYTE = new TypeAdapter<Number>() {
|
||||
@ -191,7 +192,7 @@ public final class TypeAdapters {
|
||||
}
|
||||
};
|
||||
|
||||
public static final TypeAdapter.Factory BYTE_FACTORY
|
||||
public static final TypeAdapterFactory BYTE_FACTORY
|
||||
= newFactory(byte.class, Byte.class, BYTE);
|
||||
|
||||
public static final TypeAdapter<Number> SHORT = new TypeAdapter<Number>() {
|
||||
@ -213,7 +214,7 @@ public final class TypeAdapters {
|
||||
}
|
||||
};
|
||||
|
||||
public static final TypeAdapter.Factory SHORT_FACTORY
|
||||
public static final TypeAdapterFactory SHORT_FACTORY
|
||||
= newFactory(short.class, Short.class, SHORT);
|
||||
|
||||
public static final TypeAdapter<Number> INTEGER = new TypeAdapter<Number>() {
|
||||
@ -235,7 +236,7 @@ public final class TypeAdapters {
|
||||
}
|
||||
};
|
||||
|
||||
public static final TypeAdapter.Factory INTEGER_FACTORY
|
||||
public static final TypeAdapterFactory INTEGER_FACTORY
|
||||
= newFactory(int.class, Integer.class, INTEGER);
|
||||
|
||||
public static final TypeAdapter<Number> LONG = new TypeAdapter<Number>() {
|
||||
@ -307,7 +308,7 @@ public final class TypeAdapters {
|
||||
}
|
||||
};
|
||||
|
||||
public static final TypeAdapter.Factory NUMBER_FACTORY = newFactory(Number.class, NUMBER);
|
||||
public static final TypeAdapterFactory NUMBER_FACTORY = newFactory(Number.class, NUMBER);
|
||||
|
||||
public static final TypeAdapter<Character> CHARACTER = new TypeAdapter<Character>() {
|
||||
@Override
|
||||
@ -328,7 +329,7 @@ public final class TypeAdapters {
|
||||
}
|
||||
};
|
||||
|
||||
public static final TypeAdapter.Factory CHARACTER_FACTORY
|
||||
public static final TypeAdapterFactory CHARACTER_FACTORY
|
||||
= newFactory(char.class, Character.class, CHARACTER);
|
||||
|
||||
public static final TypeAdapter<String> STRING = new TypeAdapter<String>() {
|
||||
@ -351,7 +352,7 @@ public final class TypeAdapters {
|
||||
}
|
||||
};
|
||||
|
||||
public static final TypeAdapter.Factory STRING_FACTORY = newFactory(String.class, STRING);
|
||||
public static final TypeAdapterFactory STRING_FACTORY = newFactory(String.class, STRING);
|
||||
|
||||
public static final TypeAdapter<StringBuilder> STRING_BUILDER = new TypeAdapter<StringBuilder>() {
|
||||
@Override
|
||||
@ -368,7 +369,7 @@ public final class TypeAdapters {
|
||||
}
|
||||
};
|
||||
|
||||
public static final TypeAdapter.Factory STRING_BUILDER_FACTORY =
|
||||
public static final TypeAdapterFactory STRING_BUILDER_FACTORY =
|
||||
newFactory(StringBuilder.class, STRING_BUILDER);
|
||||
|
||||
public static final TypeAdapter<StringBuffer> STRING_BUFFER = new TypeAdapter<StringBuffer>() {
|
||||
@ -386,7 +387,7 @@ public final class TypeAdapters {
|
||||
}
|
||||
};
|
||||
|
||||
public static final TypeAdapter.Factory STRING_BUFFER_FACTORY =
|
||||
public static final TypeAdapterFactory STRING_BUFFER_FACTORY =
|
||||
newFactory(StringBuffer.class, STRING_BUFFER);
|
||||
|
||||
public static final TypeAdapter<URL> URL = new TypeAdapter<URL>() {
|
||||
@ -405,7 +406,7 @@ public final class TypeAdapters {
|
||||
}
|
||||
};
|
||||
|
||||
public static final TypeAdapter.Factory URL_FACTORY = newFactory(URL.class, URL);
|
||||
public static final TypeAdapterFactory URL_FACTORY = newFactory(URL.class, URL);
|
||||
|
||||
public static final TypeAdapter<URI> URI = new TypeAdapter<URI>() {
|
||||
@Override
|
||||
@ -427,7 +428,7 @@ public final class TypeAdapters {
|
||||
}
|
||||
};
|
||||
|
||||
public static final TypeAdapter.Factory URI_FACTORY = newFactory(URI.class, URI);
|
||||
public static final TypeAdapterFactory URI_FACTORY = newFactory(URI.class, URI);
|
||||
|
||||
public static final TypeAdapter<InetAddress> INET_ADDRESS = new TypeAdapter<InetAddress>() {
|
||||
@Override
|
||||
@ -445,7 +446,7 @@ public final class TypeAdapters {
|
||||
}
|
||||
};
|
||||
|
||||
public static final TypeAdapter.Factory INET_ADDRESS_FACTORY =
|
||||
public static final TypeAdapterFactory INET_ADDRESS_FACTORY =
|
||||
newTypeHierarchyFactory(InetAddress.class, INET_ADDRESS);
|
||||
|
||||
public static final TypeAdapter<UUID> UUID = new TypeAdapter<UUID>() {
|
||||
@ -463,9 +464,9 @@ public final class TypeAdapters {
|
||||
}
|
||||
};
|
||||
|
||||
public static final TypeAdapter.Factory UUID_FACTORY = newFactory(UUID.class, UUID);
|
||||
public static final TypeAdapterFactory UUID_FACTORY = newFactory(UUID.class, UUID);
|
||||
|
||||
public static final TypeAdapter.Factory TIMESTAMP_FACTORY = new TypeAdapter.Factory() {
|
||||
public static final TypeAdapterFactory TIMESTAMP_FACTORY = new TypeAdapterFactory() {
|
||||
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
|
||||
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
|
||||
if (typeToken.getRawType() != Timestamp.class) {
|
||||
@ -551,7 +552,7 @@ public final class TypeAdapters {
|
||||
}
|
||||
};
|
||||
|
||||
public static final TypeAdapter.Factory CALENDAR_FACTORY =
|
||||
public static final TypeAdapterFactory CALENDAR_FACTORY =
|
||||
newFactoryForMultipleTypes(Calendar.class, GregorianCalendar.class, CALENDAR);
|
||||
|
||||
public static final TypeAdapter<Locale> LOCALE = new TypeAdapter<Locale>() {
|
||||
@ -589,7 +590,7 @@ public final class TypeAdapters {
|
||||
}
|
||||
};
|
||||
|
||||
public static final TypeAdapter.Factory LOCALE_FACTORY = newFactory(Locale.class, LOCALE);
|
||||
public static final TypeAdapterFactory LOCALE_FACTORY = newFactory(Locale.class, LOCALE);
|
||||
|
||||
public static final TypeAdapter<JsonElement> JSON_ELEMENT = new TypeAdapter<JsonElement>() {
|
||||
@Override public JsonElement read(JsonReader in) throws IOException {
|
||||
@ -663,7 +664,7 @@ public final class TypeAdapters {
|
||||
}
|
||||
};
|
||||
|
||||
public static final TypeAdapter.Factory JSON_ELEMENT_FACTORY
|
||||
public static final TypeAdapterFactory JSON_ELEMENT_FACTORY
|
||||
= newFactory(JsonElement.class, JSON_ELEMENT);
|
||||
|
||||
private static final class EnumTypeAdapter<T extends Enum<T>> extends TypeAdapter<T> {
|
||||
@ -698,10 +699,10 @@ public final class TypeAdapters {
|
||||
}
|
||||
}
|
||||
|
||||
public static final TypeAdapter.Factory ENUM_FACTORY = newEnumTypeHierarchyFactory();
|
||||
public static final TypeAdapterFactory ENUM_FACTORY = newEnumTypeHierarchyFactory();
|
||||
|
||||
public static <TT> TypeAdapter.Factory newEnumTypeHierarchyFactory() {
|
||||
return new TypeAdapter.Factory() {
|
||||
public static <TT> TypeAdapterFactory newEnumTypeHierarchyFactory() {
|
||||
return new TypeAdapterFactory() {
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
|
||||
Class<? super T> rawType = typeToken.getRawType();
|
||||
@ -716,9 +717,9 @@ public final class TypeAdapters {
|
||||
};
|
||||
}
|
||||
|
||||
public static <TT> TypeAdapter.Factory newFactory(
|
||||
public static <TT> TypeAdapterFactory newFactory(
|
||||
final TypeToken<TT> type, final TypeAdapter<TT> typeAdapter) {
|
||||
return new TypeAdapter.Factory() {
|
||||
return new TypeAdapterFactory() {
|
||||
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
|
||||
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
|
||||
return typeToken.equals(type) ? (TypeAdapter<T>) typeAdapter : null;
|
||||
@ -726,9 +727,9 @@ public final class TypeAdapters {
|
||||
};
|
||||
}
|
||||
|
||||
public static <TT> TypeAdapter.Factory newFactory(
|
||||
public static <TT> TypeAdapterFactory newFactory(
|
||||
final Class<TT> type, final TypeAdapter<TT> typeAdapter) {
|
||||
return new TypeAdapter.Factory() {
|
||||
return new TypeAdapterFactory() {
|
||||
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
|
||||
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
|
||||
return typeToken.getRawType() == type ? (TypeAdapter<T>) typeAdapter : null;
|
||||
@ -739,9 +740,9 @@ public final class TypeAdapters {
|
||||
};
|
||||
}
|
||||
|
||||
public static <TT> TypeAdapter.Factory newFactory(
|
||||
public static <TT> TypeAdapterFactory newFactory(
|
||||
final Class<TT> unboxed, final Class<TT> boxed, final TypeAdapter<? super TT> typeAdapter) {
|
||||
return new TypeAdapter.Factory() {
|
||||
return new TypeAdapterFactory() {
|
||||
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
|
||||
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
|
||||
Class<? super T> rawType = typeToken.getRawType();
|
||||
@ -754,9 +755,9 @@ public final class TypeAdapters {
|
||||
};
|
||||
}
|
||||
|
||||
public static <TT> TypeAdapter.Factory newFactoryForMultipleTypes(
|
||||
final Class<TT> base, final Class<? extends TT> sub, final TypeAdapter<? super TT> typeAdapter) {
|
||||
return new TypeAdapter.Factory() {
|
||||
public static <TT> TypeAdapterFactory newFactoryForMultipleTypes(final Class<TT> base,
|
||||
final Class<? extends TT> sub, final TypeAdapter<? super TT> typeAdapter) {
|
||||
return new TypeAdapterFactory() {
|
||||
@SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
|
||||
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
|
||||
Class<? super T> rawType = typeToken.getRawType();
|
||||
@ -769,9 +770,9 @@ public final class TypeAdapters {
|
||||
};
|
||||
}
|
||||
|
||||
public static <TT> TypeAdapter.Factory newTypeHierarchyFactory(
|
||||
public static <TT> TypeAdapterFactory newTypeHierarchyFactory(
|
||||
final Class<TT> clazz, final TypeAdapter<TT> typeAdapter) {
|
||||
return new TypeAdapter.Factory() {
|
||||
return new TypeAdapterFactory() {
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
|
||||
return clazz.isAssignableFrom(typeToken.getRawType()) ? (TypeAdapter<T>) typeAdapter : null;
|
||||
|
Loading…
Reference in New Issue
Block a user