本文整理汇总了Java中com.google.gson.TypeAdapterFactory类的典型用法代码示例。如果您正苦于以下问题:Java TypeAdapterFactory类的具体用法?Java TypeAdapterFactory怎么用?Java TypeAdapterFactory使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TypeAdapterFactory类属于com.google.gson包,在下文中一共展示了TypeAdapterFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getTypeAdapter
import com.google.gson.TypeAdapterFactory; //导入依赖的package包/类
@SuppressWarnings("unchecked") // Casts guarded by conditionals.
static TypeAdapter<?> getTypeAdapter(ConstructorConstructor constructorConstructor, Gson gson,
TypeToken<?> fieldType, JsonAdapter annotation) {
Class<?> value = annotation.value();
if (TypeAdapter.class.isAssignableFrom(value)) {
Class<TypeAdapter<?>> typeAdapter = (Class<TypeAdapter<?>>) value;
return constructorConstructor.get(TypeToken.get(typeAdapter)).construct();
}
if (TypeAdapterFactory.class.isAssignableFrom(value)) {
Class<TypeAdapterFactory> typeAdapterFactory = (Class<TypeAdapterFactory>) value;
return constructorConstructor.get(TypeToken.get(typeAdapterFactory))
.construct()
.create(gson, fieldType);
}
throw new IllegalArgumentException(
"@JsonAdapter value must be TypeAdapter or TypeAdapterFactory reference.");
}
示例2: buildGson
import com.google.gson.TypeAdapterFactory; //导入依赖的package包/类
public static Gson buildGson() {
final GsonBuilder gson = new GsonBuilder();
//gson.setPrettyPrinting();
gson.serializeNulls();
gson.excludeFieldsWithModifiers(Modifier.STATIC, Modifier.TRANSIENT, Modifier.VOLATILE);
// register the type adapter factories
final TypeAdapterFactoryCreator creator = new TypeAdapterFactoryCreator();
for (final TypeAdapterFactory factory : creator.getAdapters()) {
gson.registerTypeAdapterFactory(factory);
}
return gson.create();
}
示例3: getTypeAdapter
import com.google.gson.TypeAdapterFactory; //导入依赖的package包/类
@SuppressWarnings("unchecked") // Casts guarded by conditionals.
static TypeAdapter<?> getTypeAdapter(ConstructorConstructor constructorConstructor, Gson gson,
TypeToken<?> fieldType, JsonAdapter annotation) {
Class<?> value = annotation.value();
TypeAdapter<?> typeAdapter;
if (TypeAdapter.class.isAssignableFrom(value)) {
Class<TypeAdapter<?>> typeAdapterClass = (Class<TypeAdapter<?>>) value;
typeAdapter = constructorConstructor.get(TypeToken.get(typeAdapterClass)).construct();
} else if (TypeAdapterFactory.class.isAssignableFrom(value)) {
Class<TypeAdapterFactory> typeAdapterFactory = (Class<TypeAdapterFactory>) value;
typeAdapter = constructorConstructor.get(TypeToken.get(typeAdapterFactory))
.construct()
.create(gson, fieldType);
} else {
throw new IllegalArgumentException(
"@JsonAdapter value must be TypeAdapter or TypeAdapterFactory reference.");
}
if (typeAdapter != null) {
typeAdapter = typeAdapter.nullSafe();
}
return typeAdapter;
}
示例4: generateBuilder
import com.google.gson.TypeAdapterFactory; //导入依赖的package包/类
protected static GsonBuilder generateBuilder() {
GsonBuilder builder = new GsonBuilder();
synchronized (typeAdapters) {
for (Entry<Class<?>, Object> adapter : typeAdapters.entrySet()) {
builder.registerTypeAdapter(adapter.getKey(), adapter.getValue());
}
}
synchronized (typeAdapterFactories) {
for (TypeAdapterFactory factory : typeAdapterFactories) {
builder.registerTypeAdapterFactory(factory);
}
}
return builder;
}
示例5: newFactory
import com.google.gson.TypeAdapterFactory; //导入依赖的package包/类
public static <TT> TypeAdapterFactory newFactory(Class<TT> paramClass, final TypeAdapter<TT> paramTypeAdapter)
{
new TypeAdapterFactory()
{
public final <T> TypeAdapter<T> create(Gson paramAnonymousGson, TypeToken<T> paramAnonymousTypeToken)
{
if (paramAnonymousTypeToken.rawType == this.val$type) {
return paramTypeAdapter;
}
return null;
}
public final String toString()
{
return "Factory[type=" + this.val$type.getName() + ",adapter=" + paramTypeAdapter + "]";
}
};
}
示例6: DefaultRuntimeTypeAdapterFactory
import com.google.gson.TypeAdapterFactory; //导入依赖的package包/类
/**
* @param context
* @param baseClass
* @param defaultClass Setting this to null will cause a ParseException if the runtime type information is incorrect or unavailable.
*/
public DefaultRuntimeTypeAdapterFactory(Context context, Class<E> baseClass, Class<? extends E> defaultClass, TypeAdapterFactory delegateFactory) {
assert context != null && baseClass != null;
if (defaultClass != null && !isInstantiable(defaultClass)) {
throw new RuntimeException("Default class does not have a default contructor.");
}
this.context = context;
this.baseClass = baseClass;
this.defaultClass = defaultClass;
if (delegateFactory == null) {
this.delegateFactory = new ReflectiveTypeAdapterFactory(
new ConstructorConstructor(Collections.<Type, InstanceCreator<?>>emptyMap()),
FieldNamingPolicy.IDENTITY,
Excluder.DEFAULT);
} else {
this.delegateFactory = delegateFactory;
}
}
示例7: createGson
import com.google.gson.TypeAdapterFactory; //导入依赖的package包/类
private static Gson createGson() {
GsonBuilder gsonBuilder = new GsonBuilder();
for (TypeAdapterFactory factory : ServiceLoader.load(TypeAdapterFactory.class)) {
gsonBuilder.registerTypeAdapterFactory(factory);
}
return gsonBuilder.create();
}
示例8: gson
import com.google.gson.TypeAdapterFactory; //导入依赖的package包/类
/**
* the fully configured gson instance.
* @return the gson instanse
*/
@Value.Default
public Gson gson() {
GsonBuilder gsonBuilder = new GsonBuilder();
for (TypeAdapterFactory factory : ServiceLoader.load(TypeAdapterFactory.class)) {
gsonBuilder.registerTypeAdapterFactory(factory);
}
return gsonBuilder.create();
}
示例9: gsonBuilder
import com.google.gson.TypeAdapterFactory; //导入依赖的package包/类
@Provides
GsonBuilder gsonBuilder(Set<TypeAdapterFactory> factories, Map<Type, Object> adapters, Map<Class, Object> hiearchyAdapters) {
GsonBuilder builder = new GsonBuilder()
.setDateFormat(ISO8601_DATE_FORMAT)
.serializeSpecialFloatingPointValues() // Infinity and NaN
.serializeNulls(); // Needed so we can clear fields in PartialModel document updates
factories.forEach(builder::registerTypeAdapterFactory);
adapters.forEach(builder::registerTypeAdapter);
hiearchyAdapters.forEach(builder::registerTypeHierarchyAdapter);
return builder;
}
示例10: newEnumTypeHierarchyFactory
import com.google.gson.TypeAdapterFactory; //导入依赖的package包/类
public static <TT> TypeAdapterFactory newEnumTypeHierarchyFactory() {
return new TypeAdapterFactory() {
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
Class<? super T> rawType = typeToken.getRawType();
if (!Enum.class.isAssignableFrom(rawType) || rawType == Enum.class) {
return null;
}
if (!rawType.isEnum()) {
rawType = rawType.getSuperclass();
}
return new EnumTypeAdapter(rawType);
}
};
}
示例11: newFactory
import com.google.gson.TypeAdapterFactory; //导入依赖的package包/类
public static <TT> TypeAdapterFactory newFactory(final TypeToken<TT> type, final TypeAdapter<TT> typeAdapter) {
return new TypeAdapterFactory() {
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
return typeToken.equals(type) ? typeAdapter : null;
}
};
}
示例12: newFactoryForMultipleTypes
import com.google.gson.TypeAdapterFactory; //导入依赖的package包/类
public static <TT> TypeAdapterFactory newFactoryForMultipleTypes(final Class<TT> base, final Class<? extends TT> sub, final TypeAdapter<? super TT> typeAdapter) {
return new TypeAdapterFactory() {
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
Class<? super T> rawType = typeToken.getRawType();
return (rawType == base || rawType == sub) ? typeAdapter : null;
}
public String toString() {
return "Factory[type=" + base.getName() + "+" + sub.getName() + ",adapter=" + typeAdapter + "]";
}
};
}
示例13: newTypeHierarchyFactory
import com.google.gson.TypeAdapterFactory; //导入依赖的package包/类
public static <TT> TypeAdapterFactory newTypeHierarchyFactory(final Class<TT> clazz, final TypeAdapter<TT> typeAdapter) {
return new TypeAdapterFactory() {
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
return clazz.isAssignableFrom(typeToken.getRawType()) ? typeAdapter : null;
}
public String toString() {
return "Factory[typeHierarchy=" + clazz.getName() + ",adapter=" + typeAdapter + "]";
}
};
}
示例14: newFactory
import com.google.gson.TypeAdapterFactory; //导入依赖的package包/类
public static <TT> TypeAdapterFactory newFactory(
final TypeToken<TT> type, final TypeAdapter<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) {
return typeToken.equals(type) ? (TypeAdapter<T>) typeAdapter : null;
}
};
}
示例15: newFactoryForMultipleTypes
import com.google.gson.TypeAdapterFactory; //导入依赖的package包/类
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();
return (rawType == base || rawType == sub) ? (TypeAdapter<T>) typeAdapter : null;
}
@Override public String toString() {
return "Factory[type=" + base.getName()
+ "+" + sub.getName() + ",adapter=" + typeAdapter + "]";
}
};
}