com.google.gson.annotations.JsonAdapter Java Examples

The following examples show how to use com.google.gson.annotations.JsonAdapter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JsonAdapterAnnotationTypeAdapterFactory.java    From framework with GNU Affero General Public License v3.0 6 votes vote down vote up
@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.");
}
 
Example #2
Source File: JsonAdapterAnnotationTypeAdapterFactory.java    From gson with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> targetType) {
  Class<? super T> rawType = targetType.getRawType();
  JsonAdapter annotation = rawType.getAnnotation(JsonAdapter.class);
  if (annotation == null) {
    return null;
  }
  return (TypeAdapter<T>) getTypeAdapter(constructorConstructor, gson, targetType, annotation);
}
 
Example #3
Source File: JsonAdapterAnnotationTypeAdapterFactory.java    From gson with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" }) // Casts guarded by conditionals.
TypeAdapter<?> getTypeAdapter(ConstructorConstructor constructorConstructor, Gson gson,
    TypeToken<?> type, JsonAdapter annotation) {
  Object instance = constructorConstructor.get(TypeToken.get(annotation.value())).construct();

  TypeAdapter<?> typeAdapter;
  if (instance instanceof TypeAdapter) {
    typeAdapter = (TypeAdapter<?>) instance;
  } else if (instance instanceof TypeAdapterFactory) {
    typeAdapter = ((TypeAdapterFactory) instance).create(gson, type);
  } else if (instance instanceof JsonSerializer || instance instanceof JsonDeserializer) {
    JsonSerializer<?> serializer = instance instanceof JsonSerializer
        ? (JsonSerializer) instance
        : null;
    JsonDeserializer<?> deserializer = instance instanceof JsonDeserializer
        ? (JsonDeserializer) instance
        : null;
    typeAdapter = new TreeTypeAdapter(serializer, deserializer, gson, type, null);
  } else {
    throw new IllegalArgumentException("Invalid attempt to bind an instance of "
        + instance.getClass().getName() + " as a @JsonAdapter for " + type.toString()
        + ". @JsonAdapter value must be a TypeAdapter, TypeAdapterFactory,"
        + " JsonSerializer or JsonDeserializer.");
  }

  if (typeAdapter != null && annotation.nullSafe()) {
    typeAdapter = typeAdapter.nullSafe();
  }

  return typeAdapter;
}
 
Example #4
Source File: JsonAdapterAnnotationTypeAdapterFactory.java    From framework with GNU Affero General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> targetType) {
  JsonAdapter annotation = targetType.getRawType().getAnnotation(JsonAdapter.class);
  if (annotation == null) {
    return null;
  }
  return (TypeAdapter<T>) getTypeAdapter(constructorConstructor, gson, targetType, annotation);
}
 
Example #5
Source File: ReflectiveTypeAdapterFactory.java    From framework with GNU Affero General Public License v3.0 5 votes vote down vote up
private TypeAdapter<?> getFieldAdapter(Gson gson, Field field, TypeToken<?> fieldType) {
  JsonAdapter annotation = field.getAnnotation(JsonAdapter.class);
  if (annotation != null) {
    TypeAdapter<?> adapter = getTypeAdapter(constructorConstructor, gson, fieldType, annotation);
    if (adapter != null) return adapter;
  }
  return gson.getAdapter(fieldType);
}