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.owncloud.android.utils.EncryptionUtils.java

License:Open Source License

public static <T> T deserializeJSON(String json, TypeToken<T> type) {
    return new Gson().fromJson(json, type.getType());
}

From source file:com.rx2androidnetworking.Rx2ANRequest.java

License:Apache License

public <T> Observable<T> getParseObservable(TypeToken<T> typeToken) {
    this.setType(typeToken.getType());
    this.setResponseAs(ResponseType.PARSED);
    if (this.getRequestType() == RequestType.SIMPLE) {
        return Rx2InternalNetworking.generateSimpleObservable(this);
    } else if (this.getRequestType() == RequestType.MULTIPART) {
        return Rx2InternalNetworking.generateMultipartObservable(this);
    } else {//from  w  w  w . j  a  v  a 2  s. co m
        return null;
    }
}

From source file:com.rxandroidnetworking.RxANRequest.java

License:Apache License

public <T> Observable<T> getParseObservable(TypeToken<T> typeToken) {
    this.setType(typeToken.getType());
    this.setResponseAs(ResponseType.PARSED);
    if (this.getRequestType() == RequestType.SIMPLE) {
        return RxInternalNetworking.generateSimpleObservable(this);
    } else if (this.getRequestType() == RequestType.MULTIPART) {
        return RxInternalNetworking.generateMultipartObservable(this);
    } else {/*from w w  w . j  a va 2  s .  c om*/
        return null;
    }
}

From source file:com.sbux.loyalty.nlp.topicservice.GrammarService.java

/**
 * Returns a map of topic counts/*  w  ww  .j a  v a2s  .co m*/
 * @param channel
 * @param namespace
 * @param modelName
 * @param date
 * @return
* @throws Exception 
* @throws IOException 
 */
protected Map<String, Map<String, Integer>> getTopicCount(String topicCOuntFolder)
        throws IOException, Exception {
    String json = DatasourceClient.getDefaultDatasourceClient()
            .readFileAsString(topicCOuntFolder + "/data.txt");
    TypeToken<Map<String, Map<String, Integer>>> typeToken = new TypeToken<Map<String, Map<String, Integer>>>() {
    };
    return JsonConvertor.getObjectFromJson(json, typeToken.getType());
}

From source file:com.shenit.commons.utils.GsonUtils.java

License:Apache License

/**
 * Parse to json//from w  w  w  .  j a  va 2  s . c  om
 * @param str Json string
 * @param type Type to cast
 * @param <T>
 * @return
 */
public static <T> T parse(String str, TypeToken<T> type) {
    try {
        return StringUtils.isEmpty(str) ? null : CUSTOMIZED.fromJson(str, type.getType());
    } catch (Throwable t) {
        if (LOG.isInfoEnabled())
            LOG.info("[parse] Could not parse string -> {}", str);
    }
    return null;
}

From source file:com.smartling.api.sdk.BaseApiClientAdapter.java

License:Apache License

protected <T extends Data> ApiResponse<T> parseApiResponse(final String response,
        final TypeToken<ApiResponseWrapper<T>> responseType) {
    final GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Date.class, new DateTypeAdapter());

    final Gson gson = builder.create();
    final ApiResponseWrapper<T> responseWrapper = gson.fromJson(response, responseType.getType());

    return responseWrapper.getResponse();
}

From source file:com.tesla.framework.common.util.json.JSONHelper.java

License:Apache License

/**
 * @param json//from   w  w  w  . ja  v a2  s  .c o  m
 * @param typeToken
 * @param <T>
 * @return ???null?????
 */
@Nullable
public static <T> T json2Bean(@NonNull JsonElement json, @NonNull TypeToken<T> typeToken) {
    Preconditions.checkNotNull(json);
    Preconditions.checkNotNull(typeToken);

    try {
        return getGson().fromJson(json, typeToken.getType());
    } catch (Exception e) {

        return null;
    }
}

From source file:com.tesla.framework.common.util.json.JSONHelper.java

License:Apache License

@Nullable
public static Map<String, Object> json2Map(@NonNull String json) {
    Preconditions.checkNotNull(json);/*  w  w  w .j ava 2 s.co  m*/

    if (TextUtils.isEmpty(json))
        return null;

    try {
        TypeToken<Map<String, Object>> token = new TypeToken<Map<String, Object>>() {
        };

        return getGson().fromJson(json, token.getType());
    } catch (Exception e) {
        e.printStackTrace();

        return null;
    }
}

From source file:com.tremolosecurity.unison.openshiftv3.OpenShiftTarget.java

License:Apache License

@Override
public User findUser(String userID, Set<String> attributes, Map<String, Object> request)
        throws ProvisioningException {
    try {/*from  www  .  j  av a2s  . c  o  m*/
        User user = null;
        String token = this.getAuthToken();

        //users aren't bound to groups and there's no way to directly lookup what groups a user has
        //so we need to read all groups and see if the user exists

        ArrayList<String> groupsForUser = new ArrayList<String>();
        HttpCon con = this.createClient();
        StringBuffer b = new StringBuffer();

        com.tremolosecurity.unison.openshiftv3.model.List<GroupItem> groupList = null;

        try {

            String json = callWS(token, con, "/oapi/v1/groups");
            Gson gson = new Gson();
            TypeToken<com.tremolosecurity.unison.openshiftv3.model.List<GroupItem>> tokenType = new TypeToken<com.tremolosecurity.unison.openshiftv3.model.List<GroupItem>>() {
            };
            groupList = gson.fromJson(json, tokenType.getType());

            b.append("/oapi/v1/users/").append(userID);
            json = callWS(token, con, b.toString());

            com.tremolosecurity.unison.openshiftv3.model.users.User osUser = gson.fromJson(json,
                    com.tremolosecurity.unison.openshiftv3.model.users.User.class);

            if (osUser.getKind().equalsIgnoreCase("User")) {

                user = new User(userID);

                for (String attrName : osUser.getMetadata().keySet()) {
                    if (!attrName.equalsIgnoreCase("fullName") && attributes.contains(attrName)) {
                        user.getAttribs().put(attrName,
                                new Attribute(attrName, osUser.getMetadata().get(attrName)));
                    }
                }

                if (attributes.contains("fullName") && osUser.getFullName() != null) {
                    user.getAttribs().put("fullName", new Attribute("fullName", osUser.getFullName()));
                }
            }

        } finally {
            if (con != null) {
                con.getBcm().shutdown();
            }
        }

        for (GroupItem group : groupList.getItems()) {
            if (group.getUsers() != null && group.getUsers().contains(userID)) {
                groupsForUser.add((String) group.getMetadata().get("name"));
            }
        }

        if (groupsForUser.isEmpty()) {
            return user;
        } else {
            if (user == null) {
                //user = new User(userID);
                return null;
            }
            user.getGroups().addAll(groupsForUser);
            return user;
        }
    } catch (Exception e) {
        throw new ProvisioningException("Could not load " + userID, e);
    }
}

From source file:com.tremolosecurity.unison.openshiftv3.wf.OpenShiftWorkflows.java

License:Apache License

@Override
public List<Map<String, String>> generateWorkflows(WorkflowType wf, ConfigManager cfg,
        HashMap<String, Attribute> params) throws ProvisioningException {
    ArrayList<Map<String, String>> wfData = new ArrayList<Map<String, String>>();

    String targetName = params.get("target").getValues().get(0);

    HashSet<String> nameFilter = new HashSet<String>();
    Attribute attr = params.get("filter");
    if (attr != null) {
        nameFilter.addAll(attr.getValues());
    }//from   ww  w . ja v a  2 s.co m

    OpenShiftTarget target = (OpenShiftTarget) cfg.getProvisioningEngine().getTarget(targetName).getProvider();

    String kind = params.get("kind").getValues().get(0);

    try {
        String token = target.getAuthToken();

        HttpCon con = target.createClient();

        try {

            String json = target.callWS(token, con, kind);

            Gson gson = new Gson();
            TypeToken<com.tremolosecurity.unison.openshiftv3.model.List<Item>> tokenType = new TypeToken<com.tremolosecurity.unison.openshiftv3.model.List<Item>>() {
            };
            com.tremolosecurity.unison.openshiftv3.model.List<Item> list = gson.fromJson(json,
                    tokenType.getType());

            for (Item item : list.getItems()) {
                HashMap<String, String> wfParams = new HashMap<String, String>();
                String name = (String) item.getMetadata().get("name");

                if (nameFilter.contains(name)) {
                    continue;
                }

                wfParams.put("name", name);
                if (item.getMetadata().containsKey("annotations")) {
                    com.google.gson.internal.LinkedTreeMap annotations = (com.google.gson.internal.LinkedTreeMap) item
                            .getMetadata().get("annotations");
                    for (Object key : annotations.keySet()) {
                        String keyName = (String) key;
                        keyName = keyName.replace("-", "_");
                        keyName = keyName.replace(".", "_");
                        wfParams.put((String) keyName, (String) annotations.get(key));
                    }
                }

                wfData.add(wfParams);
            }

        } finally {
            con.getBcm().close();
            con.getHttp().close();
        }
    } catch (Exception e) {
        throw new ProvisioningException("Could not load", e);
    }
    return wfData;
}