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

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

Introduction

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

Prototype

public final Type getType() 

Source Link

Document

Gets underlying Type instance.

Usage

From source file:com.androidnetworking.common.ANRequest.java

License:Apache License

public void getAsOkHttpResponseAndParsed(TypeToken typeToken,
        OkHttpResponseAndParsedRequestListener parsedRequestListener) {
    this.mType = typeToken.getType();
    this.mResponseType = ResponseType.PARSED;
    this.mOkHttpResponseAndParsedRequestListener = parsedRequestListener;
    ANRequestQueue.getInstance().addRequest(this);
}

From source file:com.androidnetworking.common.ANRequest.java

License:Apache License

public ANResponse executeForParsed(TypeToken typeToken) {
    this.mType = typeToken.getType();
    this.mResponseType = ResponseType.PARSED;
    return SynchronousCall.execute(this);
}

From source file:com.apothesource.pillfill.service.drug.impl.DefaultDrugAlertServiceImpl.java

License:Open Source License

private List<DrugAlertType> deserializeDrugAlerts(Collection<PrescriptionType> rxs, String msg) {
    try {//ww w.ja  va  2 s .  c o m
        JsonObject returnValue = new JsonParser().parse(msg).getAsJsonObject();
        if (!returnValue.has("fullInteractionTypeGroup")) {
            return Collections.emptyList();
        } else {
            Gson gson = new Gson();
            TypeToken<List<FullInteractionTypeGroup>> interactionGroupListType = new TypeToken<List<FullInteractionTypeGroup>>() {
            };
            List<FullInteractionTypeGroup> group = gson.fromJson(returnValue.get("fullInteractionTypeGroup"),
                    interactionGroupListType.getType());
            ArrayList<DrugAlertType> alerts = processDrugInteractions(rxs, group);
            return alerts;

        }
    } catch (Exception e) {
        log.log(Level.SEVERE, "Invalid drug interaction alert response.", e);
        throw new RuntimeException(e);
    }
}

From source file:com.appspot.bitlyminous.gateway.BaseGateway.java

License:Apache License

/**
 * Unmarshall./*from  w ww .  ja va2s  .  com*/
 * 
 * @param typeToken the type token
 * @param response the response
 * 
 * @return the t
 */
@SuppressWarnings("unchecked")
protected <T> T unmarshall(TypeToken<T> typeToken, JsonElement response) {
    Gson gson = getGsonBuilder().create();
    return (T) gson.fromJson(response, typeToken.getType());
}

From source file:com.datastore_android_sdk.serialization.ForeignCollectionTypeAdapterFactory.java

License:Apache License

@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
    Type type = typeToken.getType();

    Class<? super T> rawType = typeToken.getRawType();
    if (!ForeignCollection.class.isAssignableFrom(rawType)) {
        return null;
    }//from  w  w  w  . j  a  v  a 2s .  c  om

    final Type elementType = $Gson$Types.getCollectionElementType(type, rawType);
    TypeToken<?> elementTypeToken = TypeToken.get(elementType);

    // Specify a TypeAdapter for the element only if we can retreive a concrete type element type
    TypeAdapter<?> elementTypeAdapter = elementType instanceof TypeVariable ? null
            : gson.getAdapter(elementTypeToken);

    @SuppressWarnings({ "unchecked", "rawtypes" })
    TypeAdapter<T> result = (TypeAdapter<T>) new Adapter(gson, elementType, elementTypeAdapter) {

        @Override
        public ObjectConstructor<T> getConstructor(final ForeignCollectionDeserializationContext context) {
            return new ObjectConstructor<T>() {
                public T construct() {
                    if (creator == null) {
                        return null;
                    } else {
                        return (T) creator.createInstance(elementType, context.getParent(),
                                context.getColumnName(), context.getOrderColumnName(),
                                context.getOrderAscending());
                    }
                }
            };
        }

    };
    return result;
}

From source file:com.datastore_android_sdk.serialization.ModelTypeAdapterFactory.java

License:Apache License

/**
 * Searches the registered serialization strategies to find the best matching
 * strategy for the given {@code type}.//from   ww w.  j  a  va  2s  . c  o  m
 * 
 * @param type
 *          The raw type of the object to find a serialization strategy
 * @return The best matching serialization strategy or {@code null} if
 *         serialization strategy registered for the given {@code type}
 */
private ModelSerializationStrategy findSerializationStrategy(Class<?> type) {
    if (type != null) {
        Class<?> rawType = type;
        TypeToken<?> typeToken = TypeToken.get(rawType);

        // Find if there is a serialization strategy registered for the super
        // classes
        while (rawType != Object.class) {
            if (serializationStrategies.containsKey(rawType)) {
                return serializationStrategies.get(rawType);
            }
            typeToken = TypeToken
                    .get($Gson$Types.resolve(typeToken.getType(), rawType, rawType.getGenericSuperclass()));
            rawType = typeToken.getRawType();
        }

        // Find if there is a serialization strategy registered for an interface
        // the type implement
        Type[] interfaces = type.getGenericInterfaces();
        for (Type i : interfaces) {
            if (serializationStrategies.containsKey(i)) {
                return serializationStrategies.get(i);
            }
        }
    }
    return null;
}

From source file:com.datastore_android_sdk.serialization.ModelTypeAdapterFactory.java

License:Apache License

/**
 * Creates the bound fields for the given type.
 * /*from ww w.j a v a 2  s. co m*/
 * @param context the Gson context
 * @param type the type of the class
 * @param raw the raw type of the class
 * @return a map of bound fields
 */
protected Map<String, ModelBoundField> getBoundFields(Gson context, TypeToken<?> type, Class<?> raw) {
    Map<String, ModelBoundField> result = new LinkedHashMap<String, ModelBoundField>();
    if (raw.isInterface()) {
        return result;
    }

    Class<?> rawType = raw;
    TypeToken<?> typeToken = type;
    Type declaredType = typeToken.getType();

    while (rawType != Object.class) {
        Field[] fields = rawType.getDeclaredFields();
        for (Field field : fields) {
            boolean serialize = includeField(field, true);
            boolean deserialize = includeField(field, false);

            if (!serialize && !deserialize) {
                continue;
            }

            field.setAccessible(true);
            Type fieldType = $Gson$Types.resolve(typeToken.getType(), rawType, field.getGenericType());
            ModelBoundField boundField = createBoundField(context, field, getFieldName(field),
                    TypeToken.get(fieldType), serialize, deserialize);

            BoundField previous = result.put(boundField.name, boundField);
            if (previous != null) {
                throw new IllegalArgumentException(
                        declaredType + " declared multiple JSON fields named " + previous.name);
            }
        }
        typeToken = TypeToken
                .get($Gson$Types.resolve(typeToken.getType(), rawType, rawType.getGenericSuperclass()));
        rawType = typeToken.getRawType();
    }
    return result;
}

From source file:com.demo.FinalFetch.java

License:Apache License

public FinalFetch(IFetch<T> ifetch, MicroRequestParams para, TypeToken<List<T>> TypeToken) {
    // TODO Auto-generated constructor stub
    super(ifetch, para, "");

    type = TypeToken.getType();

    doGet();/*from w w  w.  ja va  2 s.  c om*/
}

From source file:com.demo.FinalFetch.java

License:Apache License

public FinalFetch(IFetch<T> ifetch, MicroRequestParams para, TypeToken<List<T>> TypeToken, int Method) {
    // TODO Auto-generated constructor stub
    super(ifetch, para, "");

    type = TypeToken.getType();

    if (Method == 0)
        doGet();//  ww  w .j a v  a 2 s .  c om

    if (Method == 1)
        doPost();
}

From source file:com.demo.FinalFetch.java

License:Apache License

public FinalFetch(IFetch<T> ifetch, MicroRequestParams para, TypeToken<List<T>> TypeToken, String action) {
    // TODO Auto-generated constructor stub
    super(ifetch, para, action == null ? "" : action);

    type = TypeToken.getType();

    doGet();//  w  w w. j  a  va 2s. c  om
}