Example usage for com.google.gson.reflect TypeToken isAssignableFrom

List of usage examples for com.google.gson.reflect TypeToken isAssignableFrom

Introduction

In this page you can find the example usage for com.google.gson.reflect TypeToken isAssignableFrom.

Prototype

@Deprecated
public boolean isAssignableFrom(TypeToken<?> token) 

Source Link

Document

Check if this type is assignable from the given type token.

Usage

From source file:org.lanternpowered.server.script.json.ObjectTypeAdapterFactory.java

License:MIT License

@Nullable
@Override//from  ww w  .j a va2  s . c  o  m
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    if (!type.isAssignableFrom(this.typeToken)) {
        return null;
    }
    final TypeAdapter<JsonElement> jsonElementTypeAdapter = gson.getAdapter(JsonElement.class);
    final TypeToken theTypeToken = type;
    return new TypeAdapter<T>() {

        @SuppressWarnings("unchecked")
        @Override
        public void write(JsonWriter out, T value) throws IOException {
            final JsonElement element = serialize((TypeToken<V>) theTypeToken, (V) value, gson);
            jsonElementTypeAdapter.write(out, element);
        }

        @SuppressWarnings("unchecked")
        @Override
        public T read(JsonReader in) throws IOException {
            final JsonElement element = jsonElementTypeAdapter.read(in);
            return (T) deserialize(theTypeToken, element, gson);
        }
    };
}