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

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

Introduction

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

Prototype

public final Class<? super T> getRawType() 

Source Link

Document

Returns the raw (non-generic) type for this type.

Usage

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

License:MIT License

@Nullable
@Override//  w  ww  . ja  v a 2s.co  m
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    final Class<? super T> raw = type.getRawType();
    if (!CatalogType.class.isAssignableFrom(raw)) {
        return null;
    }
    final TypeAdapter<JsonElement> jsonElementTypeAdapter = gson.getAdapter(JsonElement.class);
    return new TypeAdapter<T>() {
        @Override
        public void write(JsonWriter out, T value) throws IOException {
            jsonElementTypeAdapter.write(out, new JsonPrimitive(((CatalogType) value).getId()));
        }

        @Override
        public T read(JsonReader in) throws IOException {
            final String id = jsonElementTypeAdapter.read(in).getAsString();
            //noinspection unchecked
            return (T) Sponge.getRegistry().getType((Class<? extends CatalogType>) raw, id)
                    .orElseThrow(() -> new IllegalArgumentException(
                            "There does not exist a " + raw.getName() + " with id: " + id));
        }
    };
}

From source file:org.mule.runtime.extension.api.persistence.ExtensionModelJsonSerializer.java

License:Open Source License

private Gson buildGson() {
    final SerializationContext serializationContext = new SerializationContext();

    Gson gsonDelegate = gsonBuilder(serializationContext, prettyPrint).create();

    return gsonBuilder(serializationContext, prettyPrint).registerTypeAdapterFactory(new TypeAdapterFactory() {

        @Override//from   ww  w.j a  v a2  s  .  co  m
        public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
            if (ExtensionModel.class.isAssignableFrom(type.getRawType())) {
                return (TypeAdapter<T>) new ExtensionModelTypeAdapter(gsonDelegate, serializationContext);
            }

            return null;
        }
    }).create();
}

From source file:org.mule.runtime.extension.internal.persistence.DefaultImplementationTypeAdapterFactory.java

License:Open Source License

/**
 * @param gson                  The actual Gson serializer
 * @param type                  Implementation that GSON is trying to find a {@link TypeAdapter}
 * @param <C> type of objects that the {@link TypeAdapter} will create
 * @return if {@param type} is subclass of {@link #superClass} a {@link TypeAdapter}, that serializes and deserialize
 * {@link C} instances/*from  w  w  w .j a  va  2  s . c  o m*/
 */
@Override
public <C> TypeAdapter<C> create(Gson gson, TypeToken<C> type) {
    if (superClass.isAssignableFrom(type.getRawType())) {
        return gson.getDelegateAdapter(this, TypeToken.get((Class<C>) clazz));
    }
    return null;
}

From source file:org.mule.runtime.extension.internal.persistence.metadata.ComponentResultTypeAdapterFactory.java

License:Open Source License

@Override
public <C> TypeAdapter<C> create(Gson gson, TypeToken<C> type) {
    return type.getRawType().isAssignableFrom(ComponentMetadataResult.class)
            ? (TypeAdapter<C>) new ComponentResultTypeAdapter(gson)
            : null;//  w ww  .  java  2 s. c om
}

From source file:org.mule.runtime.extension.internal.persistence.metadata.FailureCodeTypeAdapterFactory.java

License:Open Source License

@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    Class<T> rawType = (Class<T>) type.getRawType();
    if (rawType.equals(FailureCode.class))
        return (TypeAdapter<T>) new TypeAdapter<FailureCode>() {

            @Override/*from  w  w w.  j ava 2s  .  c  o m*/
            public void write(JsonWriter out, FailureCode value) throws IOException {
                out.value(value.getName());
            }

            @Override
            public FailureCode read(JsonReader in) throws IOException {
                return new FailureCode(in.nextString());
            }
        };
    return null;
}

From source file:org.mule.runtime.extension.internal.persistence.OperationModelTypeAdapterFactory.java

License:Open Source License

@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    if (OperationModel.class.isAssignableFrom(type.getRawType())) {
        return (TypeAdapter<T>) new OperationModelTypeAdapter(this, gson);
    }/*  www . j  av  a  2  s. c  om*/

    return null;
}

From source file:org.mule.runtime.extension.internal.persistence.RestrictiveTypeAdapterFactory.java

License:Open Source License

/**
 * @param gson The actual Gson serializer
 * @param type Implementation that GSON is trying to find a {@link TypeAdapter}
 * @param <T>  type of objects that the {@link TypeAdapter} will create
 * @return if {@param type} is a {@link #clazz} a {@link TypeAdapter}, that serializes and deserialize
 * {@link T} instances/*  w w w .  j  a v a 2s  . c o  m*/
 */
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    if (clazz.equals(type.getRawType())) {
        return gson.getDelegateAdapter(this, TypeToken.get((Class<T>) implementationClazz));
    }
    return null;
}

From source file:org.objectpocket.gson.CustomTypeAdapterFactory.java

License:Apache License

@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {

    TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);

    // @Entity//from w ww  . j a va2  s .c o m
    if (type.getRawType().getAnnotation(Entity.class) != null) {
        return new TypeAdapter<T>() {

            // SERIALIZE
            public void write(JsonWriter out, T obj) throws IOException {
                if (obj != null) {
                    String id = objectPocket.getIdForObject(obj);
                    // normalize
                    if (!objectPocket.isSerializeAsRoot(obj)) {
                        gson.toJson(new ProxyOut(obj.getClass().getTypeName(), id), ProxyOut.class, out);
                        return;
                    } else {
                        objectPocket.setSerializeAsRoot(obj, false);
                    }
                }
                // default serialization
                delegate.write(out, obj);
            };

            // DESERIALIZE
            @SuppressWarnings("unchecked")
            @Override
            public T read(JsonReader in) throws IOException {
                if (in.getPath().length() > 2) {
                    in.beginObject();
                    in.nextName();
                    StringBuilder sb = new StringBuilder(in.nextString());
                    String id = sb.substring(0, sb.indexOf("@"));
                    in.endObject();
                    T obj = null;
                    try {
                        obj = (T) ReflectionUtil.instantiateDefaultConstructor(type.getRawType());
                    } catch (InstantiationException | IllegalAccessException | ClassNotFoundException
                            | NoSuchMethodException | InvocationTargetException e) {
                        throw new IOException("Could not instantiate class " + type.getRawType().getName()
                                + "\n" + "Might be that the class has no default constructor!", e);
                    }
                    objectPocket.addIdFromReadObject(obj, id);
                    return obj;
                } else {
                    T obj = delegate.read(in);
                    return obj;
                }
            }
        };
    }
    // All other
    else {
        return delegate;
    }
}

From source file:org.openhab.binding.mqtt.homeassistant.internal.ChannelConfigurationTypeAdapterFactory.java

License:Open Source License

@Override
@Nullable//from w  ww .  j a va  2  s . c  o m
public <T> TypeAdapter<T> create(@Nullable Gson gson, @Nullable TypeToken<T> type) {
    if (gson == null || type == null) {
        return null;
    }
    if (BaseChannelConfiguration.class.isAssignableFrom(type.getRawType())) {
        return createHAConfig(gson, type);
    }
    if (BaseChannelConfiguration.Device.class.isAssignableFrom(type.getRawType())) {
        return createHADevice(gson, type);
    }
    return null;
}

From source file:org.smartparam.manager.json.vendor.gson.LevelKeySerializer.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    if (LevelKey.class.isAssignableFrom(type.getRawType())) {
        return (TypeAdapter) new TypeAdapter<LevelKey>() {

            @Override/* ww w .j  ava2s.c om*/
            public void write(JsonWriter out, LevelKey value) throws IOException {
                out.value(value.value());
            }

            @Override
            public LevelKey read(JsonReader in) throws IOException {
                return new SimpleLevelKey(in.nextString());
            }
        };
    }
    return null;
}