Java Code Examples for com.google.gson.Gson#getAdapter()

The following examples show how to use com.google.gson.Gson#getAdapter() . 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: CollectionTypeAdapterFactory.java    From salt-netapi-client with MIT License 6 votes vote down vote up
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
    Type type = typeToken.getType();

    Class<? super T> rawType = typeToken.getRawType();
    if (!Collection.class.isAssignableFrom(rawType)) {
        return null;
    }
    Type elementType = $Gson$Types.getCollectionElementType(type, rawType);

    TypeAdapter<?> elementTypeAdapter = gson.getAdapter(TypeToken.get(elementType));
    ObjectConstructor<T> constructor = constructorConstructor.get(typeToken);

    @SuppressWarnings({ "unchecked", "rawtypes" })
    TypeAdapter<T> result = new Adapter(elementTypeAdapter, constructor);
    return result;
}
 
Example 2
Source File: XorTypeAdapterFactory.java    From salt-netapi-client with MIT License 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <A> TypeAdapter<A> create(Gson gson, TypeToken<A> typeToken) {
    Type type = typeToken.getType();
    boolean isXor = typeToken.getRawType() == Xor.class;
    boolean isParameterized = type instanceof ParameterizedType;
    boolean isSSHResult = ResultSSHResultTypeAdapterFactory.isResultSSHResult(type);
    if (isXor && isParameterized && !isSSHResult) {
        Type leftType = ((ParameterizedType) type).getActualTypeArguments()[0];
        Type rightType = ((ParameterizedType) type).getActualTypeArguments()[1];
        TypeAdapter<?> rightAdapter = gson.getAdapter(TypeToken.get(rightType));
        if (leftType.equals(SaltError.class)) {
            return (TypeAdapter<A>) errorAdapter(rightAdapter);
        }

        TypeAdapter<?> leftAdapter = gson.getAdapter(TypeToken.get(leftType));
        return (TypeAdapter<A>) xorAdapter(leftAdapter, rightAdapter);
    } else {
        return null;
    }
}
 
Example 3
Source File: MapTypeAdapterFactory.java    From framework with GNU Affero General Public License v3.0 6 votes vote down vote up
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
  Type type = typeToken.getType();

  Class<? super T> rawType = typeToken.getRawType();
  if (!Map.class.isAssignableFrom(rawType)) {
    return null;
  }

  Class<?> rawTypeOfSrc = $Gson$Types.getRawType(type);
  Type[] keyAndValueTypes = $Gson$Types.getMapKeyAndValueTypes(type, rawTypeOfSrc);
  TypeAdapter<?> keyAdapter = getKeyAdapter(gson, keyAndValueTypes[0]);
  TypeAdapter<?> valueAdapter = gson.getAdapter(TypeToken.get(keyAndValueTypes[1]));
  ObjectConstructor<T> constructor = constructorConstructor.get(typeToken);

  @SuppressWarnings({"unchecked", "rawtypes"})
  // we don't define a type parameter for the key or value types
  TypeAdapter<T> result = new Adapter(gson, keyAndValueTypes[0], keyAdapter,
      keyAndValueTypes[1], valueAdapter, constructor);
  return result;
}
 
Example 4
Source File: MapTypeAdapterFactory.java    From MiBandDecompiled with Apache License 2.0 5 votes vote down vote up
private TypeAdapter a(Gson gson, Type type)
{
    if (type == Boolean.TYPE || type == java/lang/Boolean)
    {
        return TypeAdapters.BOOLEAN_AS_STRING;
    } else
    {
        return gson.getAdapter(TypeToken.get(type));
    }
}
 
Example 5
Source File: ArrayTypeAdapter.java    From gson with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
  Type type = typeToken.getType();
  if (!(type instanceof GenericArrayType || type instanceof Class && ((Class<?>) type).isArray())) {
    return null;
  }

  Type componentType = $Gson$Types.getArrayComponentType(type);
  TypeAdapter<?> componentTypeAdapter = gson.getAdapter(TypeToken.get(componentType));
  return new ArrayTypeAdapter(
          gson, componentTypeAdapter, $Gson$Types.getRawType(componentType));
}
 
Example 6
Source File: AutoMatterTypeAdapter.java    From auto-matter with Apache License 2.0 5 votes vote down vote up
static <T> AutoMatterTypeAdapter<T> createForInterface(
    final Gson gson,
    final Class<T> cls,
    final Map<String, List<String>> serializedNameMethods
) {
  return new AutoMatterTypeAdapter<>(gson, gson.getAdapter(cls), serializedNameMethods);
}
 
Example 7
Source File: SerializableInterfaceAdapterFactory.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
  if (Serializable.class.equals(type.getRawType())) {
    return (TypeAdapter<T>) new SerializableAdapter(gson.getAdapter(Object.class));
  }
  return null;
}
 
Example 8
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);
}
 
Example 9
Source File: MapTypeAdapterFactory.java    From letv with Apache License 2.0 5 votes vote down vote up
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
    Type type = typeToken.getType();
    if (!Map.class.isAssignableFrom(typeToken.getRawType())) {
        return null;
    }
    Type[] keyAndValueTypes = C$Gson$Types.getMapKeyAndValueTypes(type, C$Gson$Types.getRawType(type));
    return new Adapter(gson, keyAndValueTypes[0], getKeyAdapter(gson, keyAndValueTypes[0]), keyAndValueTypes[1], gson.getAdapter(TypeToken.get(keyAndValueTypes[1])), this.constructorConstructor.getConstructor(typeToken));
}
 
Example 10
Source File: AutoValueTypeAdapterFactory.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
  Class<? super T> rawType = type.getRawType();

  AutoGson annotation = rawType.getAnnotation(AutoGson.class);
  // Only deserialize classes decorated with @AutoGson.
  if (annotation == null) {
    return null;
  }

  return (TypeAdapter<T>) gson.getAdapter(annotation.autoValueClass());
}
 
Example 11
Source File: CustomTypeAdapterFactory.java    From yawp with MIT License 5 votes vote down vote up
private TypeAdapter<T> createAdapter(Gson gson, TypeToken<T> _type) {
    final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, _type);
    final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
    return new TypeAdapter<T>() {
        @Override
        public void write(JsonWriter out, T value) throws IOException {
            CustomTypeAdapterFactory.this.write(out, value, elementAdapter, delegate);
        }

        @Override
        public T read(JsonReader in) throws IOException {
            return CustomTypeAdapterFactory.this.read(in, elementAdapter, delegate);
        }
    };
}
 
Example 12
Source File: AlwaysListTypeAdapterFactory.java    From homeassist with Apache License 2.0 5 votes vote down vote up
@Override
public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> typeToken) {
    // If it's not a List -- just delegate the job to Gson and let it pick the best type adapter itself
    if (!List.class.isAssignableFrom(typeToken.getRawType())) {
        return null;
    }
    // Resolving the list parameter type
    final Type elementType = resolveTypeArgument(typeToken.getType());
    @SuppressWarnings("unchecked") final TypeAdapter<E> elementTypeAdapter = (TypeAdapter<E>) gson.getAdapter(TypeToken.get(elementType));
    // Note that the always-list type adapter is made null-safe, so we don't have to check nulls ourselves
    @SuppressWarnings("unchecked") final TypeAdapter<T> alwaysListTypeAdapter = (TypeAdapter<T>) new AlwaysListTypeAdapter<>(elementTypeAdapter).nullSafe();
    return alwaysListTypeAdapter;
}
 
Example 13
Source File: MapTypeAdapterFactory.java    From letv with Apache License 2.0 4 votes vote down vote up
private TypeAdapter<?> getKeyAdapter(Gson context, Type keyType) {
    return (keyType == Boolean.TYPE || keyType == Boolean.class) ? TypeAdapters.BOOLEAN_AS_STRING : context.getAdapter(TypeToken.get(keyType));
}
 
Example 14
Source File: GsonRequest.java    From jus with Apache License 2.0 4 votes vote down vote up
public GsonRequest(String method, String url, Class<T> tClass, Gson gson) {
    this(method, url, gson.getAdapter(tClass));
}
 
Example 15
Source File: VoidRequest.java    From Auth0.Android with MIT License 4 votes vote down vote up
public VoidRequest(HttpUrl url, OkHttpClient client, Gson gson, String httpMethod, ErrorBuilder<U> errorBuilder) {
    super(url, client, gson, gson.getAdapter(Void.class), errorBuilder);
    this.httpMethod = httpMethod;
}
 
Example 16
Source File: FeatureCollectionAdapter.java    From geogson with Apache License 2.0 4 votes vote down vote up
public FeatureCollectionAdapter(Gson gson) {
    this.gson = gson;
    this.featureAdapter = gson.getAdapter(Feature.class);
}
 
Example 17
Source File: GeometryAdapterFactory.java    From geogson with Apache License 2.0 4 votes vote down vote up
private GeometryAdapter(Gson gson) {
    this.geometryAdapter = gson.getAdapter(Geometry.class);
    this.positionsAdapter = gson.getAdapter(Positions.class);
}
 
Example 18
Source File: SimpleRequest.java    From Auth0.Android with MIT License 4 votes vote down vote up
public SimpleRequest(HttpUrl url, OkHttpClient client, Gson gson, String httpMethod, ErrorBuilder<U> errorBuilder) {
    super(url, client, gson, gson.getAdapter(new TypeToken<T>() {
    }), errorBuilder);
    this.method = httpMethod;
}
 
Example 19
Source File: MapTypeAdapterFactory.java    From gson with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a type adapter that writes the value as a string.
 */
private TypeAdapter<?> getKeyAdapter(Gson context, Type keyType) {
  return (keyType == boolean.class || keyType == Boolean.class)
      ? TypeAdapters.BOOLEAN_AS_STRING
      : context.getAdapter(TypeToken.get(keyType));
}
 
Example 20
Source File: TupleTypeAdapters.java    From lsp4j with Eclipse Public License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public TwoTypeAdapter(Gson gson, TypeToken<Two<F, S>> typeToken) {
	Type[] elementTypes = TypeUtils.getElementTypes(typeToken, Two.class);
	this.first = gson.getAdapter((TypeToken<F>) TypeToken.get(elementTypes[0]));
	this.second = gson.getAdapter((TypeToken<S>) TypeToken.get(elementTypes[1]));
}