@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."); }
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(); }
@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; }
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; }
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 + "]"; } }; }
public static <TT> TypeAdapterFactory newFactory(Class<TT> paramClass1, final Class<TT> paramClass2, final TypeAdapter<? super TT> paramTypeAdapter) { new TypeAdapterFactory() { public final <T> TypeAdapter<T> create(Gson paramAnonymousGson, TypeToken<T> paramAnonymousTypeToken) { Class localClass = paramAnonymousTypeToken.rawType; if ((localClass == this.val$unboxed) || (localClass == paramClass2)) { return paramTypeAdapter; } return null; } public final String toString() { return "Factory[type=" + paramClass2.getName() + "+" + this.val$unboxed.getName() + ",adapter=" + paramTypeAdapter + "]"; } }; }
/** * @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; } }
private static Gson createGson() { GsonBuilder gsonBuilder = new GsonBuilder(); for (TypeAdapterFactory factory : ServiceLoader.load(TypeAdapterFactory.class)) { gsonBuilder.registerTypeAdapterFactory(factory); } return gsonBuilder.create(); }
/** * 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(); }
@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; }
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); } }; }
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; } }; }
public static <TT> TypeAdapterFactory newFactory(final Class<TT> type, final TypeAdapter<TT> typeAdapter) { return new TypeAdapterFactory() { public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { return typeToken.getRawType() == type ? typeAdapter : null; } public String toString() { return "Factory[type=" + type.getName() + ",adapter=" + typeAdapter + "]"; } }; }
public static <TT> TypeAdapterFactory newFactory(final Class<TT> unboxed, final Class<TT> boxed, 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 == unboxed || rawType == boxed) ? typeAdapter : null; } public String toString() { return "Factory[type=" + boxed.getName() + "+" + unboxed.getName() + ",adapter=" + typeAdapter + "]"; } }; }
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 + "]"; } }; }
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 + "]"; } }; }
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; } }; }
public static <TT> TypeAdapterFactory newFactory( final Class<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.getRawType() == type ? (TypeAdapter<T>) typeAdapter : null; } @Override public String toString() { return "Factory[type=" + type.getName() + ",adapter=" + typeAdapter + "]"; } }; }
public static <TT> TypeAdapterFactory newFactory( final Class<TT> unboxed, final Class<TT> boxed, 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 == unboxed || rawType == boxed) ? (TypeAdapter<T>) typeAdapter : null; } @Override public String toString() { return "Factory[type=" + boxed.getName() + "+" + unboxed.getName() + ",adapter=" + typeAdapter + "]"; } }; }
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 + "]"; } }; }
public static <TT> TypeAdapterFactory newTypeHierarchyFactory( final Class<TT> clazz, final TypeAdapter<TT> typeAdapter) { return new TypeAdapterFactory() { @SuppressWarnings("unchecked") public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) { return clazz.isAssignableFrom(typeToken.getRawType()) ? (TypeAdapter<T>) typeAdapter : null; } @Override public String toString() { return "Factory[typeHierarchy=" + clazz.getName() + ",adapter=" + typeAdapter + "]"; } }; }
public static TypeAdapterFactory newEnumTypeHierarchyFactory() { return new TypeAdapterFactory() { @SuppressWarnings({"rawtypes", "unchecked"}) 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(); // handle anonymous subclasses } return (TypeAdapter<T>) new EnumTypeAdapter(rawType); } }; }
public static 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); } }; }
public static <TT> TypeAdapterFactory newFactory(final Class<TT> unboxed, final Class<TT> boxed, 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 == unboxed || rawType == boxed) ? typeAdapter : null; } public String toString() { return "Factory[type=" + boxed.getName() + SocializeConstants.OP_DIVIDER_PLUS + unboxed.getName() + ",adapter=" + typeAdapter + "]"; } }; }
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() + SocializeConstants.OP_DIVIDER_PLUS + sub.getName() + ",adapter=" + typeAdapter + "]"; } }; }
static TypeAdapter<?> getTypeAdapter(ConstructorConstructor constructorConstructor, Gson gson, TypeToken<?> fieldType, JsonAdapter annotation) { Class<?> value = annotation.value(); if (TypeAdapter.class.isAssignableFrom(value)) { return (TypeAdapter) constructorConstructor.get(TypeToken.get((Class) value)) .construct(); } if (TypeAdapterFactory.class.isAssignableFrom(value)) { return ((TypeAdapterFactory) constructorConstructor.get(TypeToken.get((Class) value)) .construct()).create(gson, fieldType); } throw new IllegalArgumentException("@JsonAdapter value must be TypeAdapter or " + "TypeAdapterFactory reference."); }
public static TypeAdapterFactory factory(EncryptorProviderService encryptorProviderService) { return new TypeAdapterFactory() { @SuppressWarnings("unchecked") @Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { if (Categories.class.isAssignableFrom(type.getRawType())) { return (TypeAdapter<T>) new CategoriesTypeAdapter(gson, encryptorProviderService); } return null; } }; }
public static TypeAdapterFactory factory(EncryptorProviderService encryptorProviderService) { return new TypeAdapterFactory() { @SuppressWarnings("unchecked") @Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { if (Transactions.class.isAssignableFrom(type.getRawType())) { return (TypeAdapter<T>) new TransactionsTypeAdapter(gson, encryptorProviderService); } return null; } }; }
public static TypeAdapterFactory create() { final TypeAdapterFactory factory = new AutoValueGson_AutoGsonAdapterFactory(); return new TypeAdapterFactory() { @Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { TypeAdapter<T> typeAdapter = factory.create(gson, type); return typeAdapter != null ? typeAdapter.nullSafe() : null; } }; }