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

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

Introduction

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

Prototype

public static <T> TypeToken<T> get(Class<T> type) 

Source Link

Document

Gets type literal for the given Class instance.

Usage

From source file:frank.basis.http.retrofit.gson.GsonConverterFactory.java

License:Apache License

@Override
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations,
        Annotation[] methodAnnotations, Retrofit retrofit) {
    TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
    return new frank.basis.http.retrofit.gson.GsonRequestBodyConverter<>(gson, adapter);
}

From source file:gr.kokeroulis.jsonapiparser.JsonApiConverter.java

License:Apache License

private static String fromJsonApi(String json, Type type) throws UnsupportedEncodingException {
    Gson gson = new Gson();

    HashMap<String, Object> responseHash;
    try {// w ww . ja  va  2s.c om
        responseHash = objectToMap(json);
    } catch (Exception e) {
        responseHash = null;
    }

    // if our response is an error response,
    // then we don't want to parse it
    if (responseHash != null && responseHash.get("errors") != null) {
        return json;
    }

    JsonApiResponse response = gson.fromJson(json, JsonApiResponse.class);
    List<Map<String, Object>> data = new ArrayList<>();

    response.data().doOnNext(stringObjectMap -> {
        stringObjectMap.putAll((Map<String, Object>) stringObjectMap.get(ATTRIBUTES_KEY));
        stringObjectMap.remove(ATTRIBUTES_KEY);

        // We don't need to parse Authentication object!
        if (stringObjectMap.get(RELATIONSHIP_KEY) == null) {
            return;
        }
        Observable.from(((Map<String, Object>) stringObjectMap.get(RELATIONSHIP_KEY)).keySet()).filter(
                key -> ((Map<String, Object>) stringObjectMap.get(RELATIONSHIP_KEY)).get(key) instanceof Map
                        || ((Map<String, Object>) stringObjectMap.get(RELATIONSHIP_KEY))
                                .get(key) instanceof List)
                .subscribe(key -> {
                    stringObjectMap.put(key,
                            ((Map<String, Object>) ((Map<String, Object>) stringObjectMap.get(RELATIONSHIP_KEY))
                                    .get(key)).get("data"));

                    Observable<Map<String, Object>> inner;
                    Object includedLinks = stringObjectMap.get(key);

                    if (includedLinks instanceof List)
                        inner = Observable.from((List<Map<String, Object>>) includedLinks);
                    else
                        inner = Observable.just((Map<String, Object>) includedLinks);

                    inner.forEach(
                            link -> response.included()
                                    .filter(included -> included.get("type").equals(link.get("type"))
                                            && included.get("id").equals(link.get("id")))
                                    .last().subscribe(included -> {
                                        link.putAll((Map<String, Object>) included.get(ATTRIBUTES_KEY));
                                    }, error -> {
                                        // catch exception. We need this one!
                                        // look at the comments of JsonApiResponse
                                    }));
                });
        stringObjectMap.remove(RELATIONSHIP_KEY);
    }).subscribe(datum -> data.add(datum));

    String formatted;

    // List must preserve their structure when they
    // are a single item.
    // TL;DR don't convert the list into to a string
    // when it has only 1 item!
    String listTypeToString = TypeToken.get(type).getRawType().toString();
    boolean isInstanceOfList = listTypeToString.equals("interface java.util.List");
    if (data.size() == 1 && !isInstanceOfList) {
        formatted = data.get(0) == null ? json : gson.toJson(data.get(0));
    } else {
        formatted = gson.toJson(data);
    }

    return formatted;
}

From source file:io.apptik.comm.jus.converter.GsonConverterFactory.java

License:Apache License

@Override
public Converter<NetworkResponse, ?> fromResponse(Type type, Annotation[] annotations) {
    TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
    return new GsonResponseConverter<>(adapter);
}

From source file:io.apptik.comm.jus.converter.GsonConverterFactory.java

License:Apache License

@Override
public Converter<?, NetworkRequest> toRequest(Type type, Annotation[] annotations) {
    TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
    return new GsonRequestConverter<>(gson, adapter);
}

From source file:io.apptik.comm.jus.request.GsonRequest.java

License:Apache License

public <R> GsonRequest<T> setRequestData(R requestData, Gson gson) {
    TypeAdapter<?> adapter;/* www.  j  a v a  2 s  .  co m*/
    if (requestData == null) {
        adapter = gson.getAdapter(Object.class);
    } else {
        adapter = gson.getAdapter(TypeToken.get(requestData.getClass()));
    }
    return setRequestData(requestData, gson, adapter);
}

From source file:io.getlime.security.powerauth.networking.endpoints.PA2ActivationStatusEndpoint.java

License:Apache License

@Override
public TypeToken<ActivationStatusResponse> getResponseType() {
    return TypeToken.get(ActivationStatusResponse.class);
}

From source file:io.getlime.security.powerauth.networking.endpoints.PA2CreateActivationEndpoint.java

License:Apache License

@Override
public TypeToken<ActivationCreateResponse> getResponseType() {
    return TypeToken.get(ActivationCreateResponse.class);
}

From source file:io.getlime.security.powerauth.networking.endpoints.PA2NonPersonalizedEncryptedEndpoint.java

License:Apache License

@Override
public TypeToken<NonPersonalizedEncryptedPayloadModel> getResponseType() {
    return TypeToken.get(NonPersonalizedEncryptedPayloadModel.class);
}

From source file:io.getlime.security.powerauth.networking.endpoints.PA2VaultUnlockEndpoint.java

License:Apache License

@Override
public TypeToken<VaultUnlockResponse> getResponseType() {
    return TypeToken.get(VaultUnlockResponse.class);
}

From source file:io.plaidapp.core.data.api.DenvelopingConverter.java

License:Apache License

@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
        Retrofit retrofit) {/*  ww  w.j  a v a2s. c  o  m*/

    // This converter requires an annotation providing the name of the payload in the envelope;
    // if one is not supplied then return null to continue down the converter chain.
    final String payloadName = getPayloadName(annotations);
    if (payloadName == null)
        return null;

    final TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
    return (Converter<ResponseBody, Object>) body -> {
        try (JsonReader jsonReader = gson.newJsonReader(body.charStream())) {
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                if (payloadName.equals(jsonReader.nextName())) {
                    return adapter.read(jsonReader);
                } else {
                    jsonReader.skipValue();
                }
            }
            return null;
        } finally {
            body.close();
        }
    };
}