Example usage for com.google.gson JsonElement getAsString

List of usage examples for com.google.gson JsonElement getAsString

Introduction

In this page you can find the example usage for com.google.gson JsonElement getAsString.

Prototype

public String getAsString() 

Source Link

Document

convenience method to get this element as a string value.

Usage

From source file:com.android.tools.lint.checks.AppLinksAutoVerifyDetector.java

License:Apache License

/**
 * Gets the package names of all the apps from the Digital Asset Links JSON file.
 *
 * @param element The JsonElement of the json file.
 * @return All the package names.//from www.  j ava2s  .c  o m
 */
private static List<String> getPackageNameFromJson(JsonElement element) {
    List<String> packageNames = Lists.newArrayList();
    if (element instanceof JsonArray) {
        JsonArray jsonArray = (JsonArray) element;
        for (int i = 0; i < jsonArray.size(); i++) {
            JsonElement app = jsonArray.get(i);
            if (app instanceof JsonObject) {
                JsonObject target = ((JsonObject) app).getAsJsonObject("target");
                if (target != null) {
                    // Checks namespace to ensure it is an app statement.
                    JsonElement namespace = target.get("namespace");
                    JsonElement packageName = target.get("package_name");
                    if (namespace != null && namespace.getAsString().equals("android_app")
                            && packageName != null) {
                        packageNames.add(packageName.getAsString());
                    }
                }
            }
        }
    }
    return packageNames;
}

From source file:com.android.volley.DateFormatter.java

License:Open Source License

public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonParseException exception = null;
    final String value = json.getAsString();
    for (DateFormat format : formats)
        try {/*from www.ja v  a 2s.co  m*/
            synchronized (format) {
                return format.parse(value);
            }
        } catch (ParseException e) {
            exception = new JsonParseException(e);
        }
    throw exception;
}

From source file:com.apcb.utils.utils.HtmlTemplateReader.java

private SectionMatch readJson(JsonElement jsonElement, String from, SectionMatch sectionMatch) {
    //log.info(jsonElement.toString());
    //SectionMatch sectionMatchChild = new SectionMatch(from);
    if (jsonElement.isJsonArray()) {
        //log.info("JsonArray");
        JsonArray jsonArray = jsonElement.getAsJsonArray();
        for (int i = 0; i < jsonArray.size(); i++) {
            SectionMatch sectionMatchArrayLevel = new SectionMatch(sectionMatch.getName());
            sectionMatchArrayLevel.setIndex(i);
            sectionMatch.putChildens(readJson(jsonArray.get(i), from + "[" + i + "]", sectionMatchArrayLevel));
        }// ww  w .  j  a  v  a 2s .c  o  m
    } else if (jsonElement.isJsonObject()) {
        //log.info("JsonObject");
        JsonObject jsonObject = jsonElement.getAsJsonObject(); //since you know it's a JsonObject
        Set<Map.Entry<String, JsonElement>> entries = jsonObject.entrySet();//will return members of your object
        for (Map.Entry<String, JsonElement> entry : entries) {
            //log.info(entry.getKey());
            sectionMatch.putChildens(readJson(jsonObject.get(entry.getKey()), from + "." + entry.getKey(),
                    new SectionMatch(entry.getKey())));
        }
    } else if (jsonElement.isJsonNull()) {
        log.info("JsonNull");
    } else if (jsonElement.isJsonPrimitive()) {
        //log.info("JsonPrimitive");
        String jsonVal = jsonElement.getAsString();
        //from+= "."+entry.getKey(); 
        sectionMatch.setValue(jsonVal);
        log.info(from + "=" + jsonVal);
    }

    return sectionMatch;
}

From source file:com.apifest.AccessTokenValidator.java

License:Apache License

protected static String extractTokenScope(String tokenContent) {
    String scope = null;//from w  ww.java 2 s. co  m
    JsonParser parser = new JsonParser();
    JsonObject jsonObject = parser.parse(tokenContent).getAsJsonObject();
    JsonElement scopeElement = jsonObject.get("scope");
    String rs = (scopeElement != null && !scopeElement.isJsonNull()) ? scopeElement.getAsString() : null;
    if (rs != null && !rs.toString().equals("null")) {
        scope = rs;
    }
    return scope;
}

From source file:com.apifest.api.BasicAction.java

License:Apache License

/**
 * Extracts userId from tokenValidationResponse.
 * @param response the response received after access token is validate
 * @return userId associated with a token
 *//*from w ww  .ja v a 2  s . c  om*/
public static String getUserId(HttpResponse tokenValidationResponse) {
    String userId = null;
    if (tokenValidationResponse != null) {
        JsonParser parser = new JsonParser();
        JsonObject json = parser.parse(tokenValidationResponse.getContent().toString(CharsetUtil.UTF_8))
                .getAsJsonObject();
        JsonElement userIdElement = json.get("userId");
        userId = (userIdElement != null && !userIdElement.isJsonNull()) ? userIdElement.getAsString() : null;
    }
    return userId;
}

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

License:Open Source License

/**
 * <a href="http://rxnav.nlm.nih.gov/RxNormAPIs.html#">NIH Service</a>: Get avaliable RxNorm IDs associated with an 11 digit National Drug Code (NDC).
 *
 * @param ndc The 11-digit NDC without dashes or spaces
 * @return A list of RxNorm IDs associated with this NDC (should normally return 0 or 1)
 *//*w ww.j  a va 2s. c o  m*/
@Override
public Observable<String> getRxNormIdForDrugNdc(String ndc) {
    return subscribeIoObserveImmediate(subscriber -> {
        String urlStr = String.format(URL_RXNORM_BRAND_NAME_BY_NDC, ndc);
        try {
            String responseStr = PFNetworkManager.doPinnedGetForUrl(urlStr);
            JsonParser parser = new JsonParser();
            JsonObject response = parser.parse(responseStr).getAsJsonObject().get("idGroup").getAsJsonObject();
            if (response.has("rxnormId")) {
                JsonArray array = response.getAsJsonArray("rxnormId");
                for (JsonElement e : array) {
                    subscriber.onNext(e.getAsString());
                }
                subscriber.onCompleted();
            } else {
                Timber.e("No rxnormIds found for NDC: %s", ndc);
                subscriber.onError(new RuntimeException("Could not find NDC->RxNorm for NDC."));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    });

}

From source file:com.appdynamics.extensions.couchbase.CouchBaseWrapper.java

License:Apache License

/**
 * Populates an empty map with values retrieved from the entry set of a Json
 * Object//from  w ww.  j a v  a  2 s .co m
 * 
 * @param iterator
 *            An entry set iterator for the json object
 * @param metrics
 *            Initially empty map that is populated based on the values
 *            retrieved from entry set
 * @param prefix
 *            Optional prefix for the metric name to distinguish duplicate
 *            metric names
 */
private void populateMetricsMapHelper(Iterator<Entry<String, JsonElement>> iterator,
        Map<String, Double> metrics, String prefix) {
    while (iterator.hasNext()) {
        Entry<String, JsonElement> entry = iterator.next();
        String metricName = (String) entry.getKey();
        JsonElement value = (JsonElement) entry.getValue();
        if (value instanceof JsonPrimitive && NumberUtils.isNumber(value.getAsString())) {
            Double val = value.getAsDouble();
            metrics.put(prefix + metricName, val);
        }
    }
}

From source file:com.appslandia.common.json.DateAdapter.java

License:Open Source License

@Override
public java.util.Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    synchronized (this.parMutex) {
        try {//www .  j av a 2 s .c om
            return new java.util.Date(this.parser.parse(json.getAsString()).getTime());
        } catch (ParseException ex) {
            throw new JsonParseException(ex);
        }
    }
}

From source file:com.appslandia.common.json.LocalDateAdapter.java

License:Open Source License

@Override
public LocalDate deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    return LocalDate.parse(json.getAsString(), Jdk8DateUtils.DATE_FORMATTER);
}

From source file:com.appslandia.common.json.LocalDateTimeAdapter.java

License:Open Source License

@Override
public LocalDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    return LocalDateTime.parse(json.getAsString(), Jdk8DateUtils.DATETIME_FORMATTER);
}