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.jclouds.json.internal.IgnoreNullMapTypeAdapterFactory.java

License:Apache License

@SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
    Type type = typeToken.getType();
    if (typeToken.getRawType() != Map.class || !(type instanceof ParameterizedType)) {
        return null;
    }/*w  w  w .jav  a2 s. c o  m*/

    Type keyType = ((ParameterizedType) type).getActualTypeArguments()[0];
    Type valueType = ((ParameterizedType) type).getActualTypeArguments()[1];
    TypeAdapter<?> keyAdapter = gson.getAdapter(TypeToken.get(keyType));
    TypeAdapter<?> valueAdapter = gson.getAdapter(TypeToken.get(valueType));
    return (TypeAdapter<T>) newMapAdapter(keyAdapter, valueAdapter);
}

From source file:org.jclouds.json.internal.IgnoreNullMultimapTypeAdapterFactory.java

License:Apache License

@SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
    Type type = typeToken.getType();
    if ((typeToken.getRawType() != Multimap.class) || !(type instanceof ParameterizedType)) {
        return null;
    }//from w w w  . j  a  va  2 s .co m

    Type keyType = ((ParameterizedType) type).getActualTypeArguments()[0];
    Type valueType = ((ParameterizedType) type).getActualTypeArguments()[1];
    TypeAdapter<?> keyAdapter = gson.getAdapter(TypeToken.get(keyType));
    TypeAdapter<?> valueAdapter = gson.getAdapter(TypeToken.get(valueType));
    return (TypeAdapter<T>) newMapAdapter(keyAdapter, valueAdapter);
}

From source file:org.jclouds.json.internal.IgnoreNullSetTypeAdapterFactory.java

License:Apache License

@SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
    Type type = typeToken.getType();
    if (typeToken.getRawType() != Set.class || !(type instanceof ParameterizedType)) {
        return null;
    }//www . j a  v a  2  s.c  o m

    Type elementType = ((ParameterizedType) type).getActualTypeArguments()[0];
    TypeAdapter<?> elementAdapter = gson.getAdapter(TypeToken.get(elementType));
    return (TypeAdapter<T>) newSetAdapter(elementAdapter);
}

From source file:org.jclouds.json.internal.NullFilteringTypeAdapterFactories.java

License:Apache License

static <T> TypeToken<?> resolve(TypeToken<T> ownerType, Type param) {
    return TypeToken.get(
            com.google.gson.internal.$Gson$Types.resolve(ownerType.getType(), ownerType.getRawType(), param));
}

From source file:org.jclouds.json.internal.OptionalTypeAdapterFactory.java

License:Apache License

@SuppressWarnings("unchecked")
@Override//from   w  w  w.ja va2s. com
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
    Type type = typeToken.getType();
    if (typeToken.getRawType() != Optional.class || !(type instanceof ParameterizedType)) {
        return null;
    }

    Type elementType = ((ParameterizedType) type).getActualTypeArguments()[0];
    TypeAdapter<?> elementAdapter = gson.getAdapter(TypeToken.get(elementType));
    return (TypeAdapter<T>) newOptionalAdapter(elementAdapter);
}

From source file:org.lanternpowered.server.script.function.action.json.ActionTypeAdapterFactory.java

License:MIT License

@Override
protected Action deserialize(TypeToken<Action> type, JsonElement element, Gson gson) {
    final Class<? super Action> raw = type.getRawType();
    if (ScriptFunction.class.isAssignableFrom(raw)) {
        return LanternScriptGameRegistry.get().compile(element.getAsString(), Action.class).get();
    } else if (ConditionalAction.class.isAssignableFrom(raw)) {
        return gson.getDelegateAdapter(this, TypeToken.get(ConditionalAction.class)).fromJsonTree(element);
    } else if (EmptyAction.class.isAssignableFrom(raw)) {
        return gson.getDelegateAdapter(this, TypeToken.get(EmptyAction.class)).fromJsonTree(element);
    } else if (MultiAction.class.isAssignableFrom(raw)) {
        return gson.getDelegateAdapter(this, TypeToken.get(MultiAction.class)).fromJsonTree(element);
    } else if (raw == Action.class) {
        // The actual action type isn't provided, we will try
        // to assume the right type based in the json format
        if (element.isJsonNull()) {
            return Action.empty();
        } else if (element.isJsonArray()) {
            return gson.getDelegateAdapter(this, TypeToken.get(MultiAction.class)).fromJsonTree(element);
        } else if (element.isJsonObject()) {
            final JsonObject obj = element.getAsJsonObject();
            if (obj.has(ConditionalActionJsonSerializer.ACTION)
                    && obj.has(ConditionalActionJsonSerializer.CONDITION)) {
                return gson.getDelegateAdapter(this, TypeToken.get(ConditionalAction.class))
                        .fromJsonTree(element);
            }/*ww  w  .  j a v  a  2s. c  om*/
        } else {
            final String value = element.getAsString();
            if (value.startsWith(SCRIPT_PREFIX)) {
                return LanternScriptGameRegistry.get().compile(value, Action.class).get();
            }
        }
    }
    return super.deserialize(type, element, gson);
}

From source file:org.lanternpowered.server.script.function.condition.json.ConditionTypeAdapterFactory.java

License:MIT License

@Override
protected Condition deserialize(TypeToken<Condition> type, JsonElement element, Gson gson) {
    final Class<? super Condition> raw = type.getRawType();
    if (AndCondition.class.isAssignableFrom(raw)) {
        return gson.getDelegateAdapter(this, TypeToken.get(AndCondition.class)).fromJsonTree(element);
    } else if (OrCondition.class.isAssignableFrom(raw)) {
        return gson.getDelegateAdapter(this, TypeToken.get(OrCondition.class)).fromJsonTree(element);
    } else if (raw == Condition.class) {
        // The actual condition type isn't provided, we will try
        // to assume the right type based in the json format
        if (element.isJsonArray()) {
            return gson.getAdapter(AndCondition.class).fromJsonTree(element);
        } else if (element.isJsonPrimitive()) {
            final String value = element.getAsString();
            if (value.startsWith(SCRIPT_PREFIX)) {
                return LanternScriptGameRegistry.get().compile(value, Condition.class).get();
            }/*from  w ww.  ja v a 2s. c o  m*/
        }
    }
    return super.deserialize(type, element, gson);
}

From source file:org.lanternpowered.server.script.function.value.json.DoubleValueProviderTypeAdapterFactory.java

License:MIT License

@Override
protected DoubleValueProvider deserialize(TypeToken<DoubleValueProvider> type, JsonElement element, Gson gson) {
    final Class<? super DoubleValueProvider> raw = type.getRawType();
    if (FloatValueProvider.Constant.class.isAssignableFrom(raw)) {
        return gson.getDelegateAdapter(this, TypeToken.get(DoubleValueProvider.Constant.class))
                .fromJsonTree(element);/*from   www .  j  a va  2 s.  c o m*/
    } else if (FloatValueProvider.Range.class.isAssignableFrom(raw)) {
        return gson.getDelegateAdapter(this, TypeToken.get(DoubleValueProvider.Range.class))
                .fromJsonTree(element);
    } else if (raw == DoubleValueProvider.class) {
        // The actual condition type isn't provided, we will try
        // to assume the right type based in the json format
        if (element.isJsonPrimitive()) {
            final String value = element.getAsString();
            if (value.startsWith(SCRIPT_PREFIX)) {
                return LanternScriptGameRegistry.get().compile(value, DoubleValueProvider.class).get();
            }
            return gson.getDelegateAdapter(this, TypeToken.get(DoubleValueProvider.Constant.class))
                    .fromJsonTree(element);
        }
    }
    return super.deserialize(type, element, gson);
}

From source file:org.lanternpowered.server.script.function.value.json.FloatValueProviderTypeAdapterFactory.java

License:MIT License

@Override
protected FloatValueProvider deserialize(TypeToken<FloatValueProvider> type, JsonElement element, Gson gson) {
    final Class<? super FloatValueProvider> raw = type.getRawType();
    if (FloatValueProvider.Constant.class.isAssignableFrom(raw)) {
        return gson.getDelegateAdapter(this, TypeToken.get(FloatValueProvider.Constant.class))
                .fromJsonTree(element);//ww w .  j a v  a 2 s.  co m
    } else if (FloatValueProvider.Range.class.isAssignableFrom(raw)) {
        return gson.getDelegateAdapter(this, TypeToken.get(FloatValueProvider.Range.class))
                .fromJsonTree(element);
    } else if (raw == FloatValueProvider.class) {
        // The actual condition type isn't provided, we will try
        // to assume the right type based in the json format
        if (element.isJsonPrimitive()) {
            final String value = element.getAsString();
            if (value.startsWith(SCRIPT_PREFIX)) {
                return LanternScriptGameRegistry.get().compile(value, FloatValueProvider.class).get();
            }
            return gson.getDelegateAdapter(this, TypeToken.get(FloatValueProvider.Constant.class))
                    .fromJsonTree(element);
        }
    }
    return super.deserialize(type, element, gson);
}

From source file:org.lanternpowered.server.script.function.value.json.IntValueProviderTypeAdapterFactory.java

License:MIT License

@Override
protected IntValueProvider deserialize(TypeToken<IntValueProvider> type, JsonElement element, Gson gson) {
    final Class<? super IntValueProvider> raw = type.getRawType();
    if (IntValueProvider.Constant.class.isAssignableFrom(raw)) {
        return gson.getDelegateAdapter(this, TypeToken.get(IntValueProvider.Constant.class))
                .fromJsonTree(element);// w  w  w  . j  a va  2  s  . c om
    } else if (IntValueProvider.Range.class.isAssignableFrom(raw)) {
        return gson.getDelegateAdapter(this, TypeToken.get(IntValueProvider.Range.class)).fromJsonTree(element);
    } else if (raw == IntValueProvider.class) {
        // The actual condition type isn't provided, we will try
        // to assume the right type based in the json format
        if (element.isJsonPrimitive()) {
            final String value = element.getAsString();
            if (value.startsWith(SCRIPT_PREFIX)) {
                return LanternScriptGameRegistry.get().compile(value, IntValueProvider.class).get();
            }
            return gson.getDelegateAdapter(this, TypeToken.get(IntValueProvider.Constant.class))
                    .fromJsonTree(element);
        }
    }
    return super.deserialize(type, element, gson);
}