Example usage for com.google.gson JsonObject getAsJsonObject

List of usage examples for com.google.gson JsonObject getAsJsonObject

Introduction

In this page you can find the example usage for com.google.gson JsonObject getAsJsonObject.

Prototype

public JsonObject getAsJsonObject(String memberName) 

Source Link

Document

Convenience method to get the specified member as a JsonObject.

Usage

From source file:com.adeebnqo.Thula.mmssms.Transaction.java

License:Apache License

private String fetchRnrSe(String authToken, Context context) throws ExecutionException, InterruptedException {
    JsonObject userInfo = Ion.with(context).load("https://www.google.com/voice/request/user")
            .setHeader("Authorization", "GoogleLogin auth=" + authToken).asJsonObject().get();

    String rnrse = userInfo.get("r").getAsString();

    try {//from  w  ww.  java 2 s.  c  o  m
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Activity.TELEPHONY_SERVICE);
        String number = tm.getLine1Number();
        if (number != null) {
            JsonObject phones = userInfo.getAsJsonObject("phones");
            for (Map.Entry<String, JsonElement> entry : phones.entrySet()) {
                JsonObject phone = entry.getValue().getAsJsonObject();
                if (!PhoneNumberUtils.compare(number, phone.get("phoneNumber").getAsString()))
                    continue;
                if (!phone.get("smsEnabled").getAsBoolean())
                    break;

                Ion.with(context).load("https://www.google.com/voice/settings/editForwardingSms/")
                        .setHeader("Authorization", "GoogleLogin auth=" + authToken)
                        .setBodyParameter("phoneId", entry.getKey()).setBodyParameter("enabled", "0")
                        .setBodyParameter("_rnr_se", rnrse).asJsonObject();
                break;
            }
        }
    } catch (Exception e) {

    }

    // broadcast so you can save it to your shared prefs or something so that it doesn't need to be retrieved every time
    Intent intent = new Intent(VOICE_TOKEN);
    intent.putExtra("_rnr_se", rnrse);
    context.sendBroadcast(intent);

    return rnrse;
}

From source file:com.adobe.acs.commons.json.JsonObjectUtil.java

License:Apache License

public static Optional<JsonObject> getOptionalObject(JsonObject obj, String prop) {
    return Optional.ofNullable(obj.getAsJsonObject(prop));
}

From source file:com.agateau.pixelwheels.stats.JsonGameStatsIO.java

License:Open Source License

@Override
public void load() {
    Assert.check(mGameStats != null, "setGameStats() has not been called");
    if (!mHandle.exists()) {
        return;//  w ww.  jav  a2s.c  o m
    }
    mGameStats.mTrackStats.clear();
    String json = mHandle.readString("UTF-8");
    JsonParser parser = new JsonParser();
    JsonObject root = parser.parse(json).getAsJsonObject();
    JsonObject trackStatsObject = root.getAsJsonObject("trackStats");
    for (Map.Entry<String, JsonElement> kv : trackStatsObject.entrySet()) {
        String trackId = kv.getKey();
        mGameStats.addTrack(trackId);
        loadTrackStats(mGameStats.getTrackStats(trackId), kv.getValue().getAsJsonObject());
    }
}

From source file:com.android.cloudfiles.cloudfilesforandroid.CloudFiles.java

License:Apache License

private void authentication(String userName, String apiKey) throws IOException {
    String json = generateToken(userName, apiKey);

    JsonParser jsonParser = new JsonParser();
    JsonObject jo = (JsonObject) jsonParser.parse(json);

    setToken(jo.getAsJsonObject("access").getAsJsonObject("generateToken").get("id").getAsString());
    JsonArray services = jo.getAsJsonObject("access").get("serviceCatalog").getAsJsonArray();

    for (JsonElement jsonElement : services) {
        JsonObject jobj = jsonElement.getAsJsonObject();

        if (jobj.get("name").getAsString().equals("cloudFiles")) {

            JsonArray endpoints = jobj.getAsJsonArray("endpoints");
            for (JsonElement jsonElement2 : endpoints) {

                urls.put(jsonElement2.getAsJsonObject().get("region").getAsString(),
                        jsonElement2.getAsJsonObject().get("publicURL").getAsString());

            }//from w w w . j ava 2 s. c  om
        }

        if (jobj.get("name").getAsString().equals("cloudFilesCDN")) {

            JsonArray endpoints = jobj.getAsJsonArray("endpoints");
            for (JsonElement jsonElement2 : endpoints) {

                cdnUrls.put(jsonElement2.getAsJsonObject().get("region").getAsString(),
                        jsonElement2.getAsJsonObject().get("publicURL").getAsString());

            }
        }

    }

}

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

License:Apache License

/**
 * Gathers Cluster and Node stats. Cluster stats as Map of CLUSTER_STATS_KEY
 * and Map of MetricName, MetricValue and NodeStats as Map of NodeName, Map
 * of MetricName, MetricValue//from w w w . ja  v  a 2  s  . c o  m
 * 
 * @param httpClient
 * @return
 */
public Map<String, Map<String, Double>> gatherClusterNodeMetrics(SimpleHttpClient httpClient) {
    JsonElement clusterNodeResponse = getResponse(httpClient, CLUSTER_NODE_URI);
    Map<String, Map<String, Double>> clusterNodeMetrics = new HashMap<String, Map<String, Double>>();
    if (clusterNodeResponse != null && clusterNodeResponse.isJsonObject()) {
        JsonObject clusterNodeJsonObject = clusterNodeResponse.getAsJsonObject();
        JsonObject storageTotals = clusterNodeJsonObject.getAsJsonObject("storageTotals");
        clusterNodeMetrics = populateClusterMetrics(clusterNodeMetrics, storageTotals);

        JsonArray nodes = (JsonArray) clusterNodeJsonObject.get("nodes");
        clusterNodeMetrics.putAll(populateNodeMetrics(nodes));
    }
    return clusterNodeMetrics;
}

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

License:Apache License

/**
 * Gathers Bucket Replication stats from /pools/default/buckets/<bucket_name>/stats
 * //www .  jav  a2 s  .  co m
 * Response contains all other stats but this only filters any stats that starts with 'replications', 
 * e.g. replications/03cd3332434401c64594f47eeeabbb79/beer-sample/gamesim-sample/wtavg_meta_latency
 * 
 * @param buckets
 * @param httpClient
 * @return Map<String, Map<String, Double>>
 */
public Map<String, Map<String, Double>> gatherOtherBucketMetrics(Set<String> buckets,
        SimpleHttpClient httpClient) {
    Map<String, Map<String, Double>> otherBucketMetrics = new HashMap<String, Map<String, Double>>();

    if (buckets != null) {
        for (String bucket : buckets) {
            JsonElement bucketResponse = getResponse(httpClient, String.format(BUCKET_STATS_URI, bucket));

            if (bucketResponse != null && bucketResponse.isJsonObject()) {
                JsonObject bucketStats = bucketResponse.getAsJsonObject();
                JsonObject op = bucketStats.getAsJsonObject("op");
                JsonObject samples = op.getAsJsonObject("samples");
                populateOtherBucketMetrics(bucket, samples.entrySet().iterator(), otherBucketMetrics);
            }
        }
    }

    return otherBucketMetrics;
}

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

License:Apache License

/**
 * Populates the cluster metrics hashmap
 * /*  w w w . j  a v a  2s .  c o m*/
 * @param nodeMetrics
 * @param clusterStats
 * @return
 */
private Map<String, Map<String, Double>> populateClusterMetrics(Map<String, Map<String, Double>> nodeMetrics,
        JsonObject clusterStats) {
    Map<String, Double> clusterMetrics = new HashMap<String, Double>();
    if (clusterStats != null) {
        JsonObject ramStats = clusterStats.getAsJsonObject("ram");
        Iterator<Entry<String, JsonElement>> iterator = ramStats.entrySet().iterator();
        populateMetricsMapHelper(iterator, clusterMetrics, "ram_");

        JsonObject hddStats = clusterStats.getAsJsonObject("hdd");
        iterator = hddStats.entrySet().iterator();
        populateMetricsMapHelper(iterator, clusterMetrics, "hdd_");
    }
    nodeMetrics.put(CLUSTER_STATS_KEY, clusterMetrics);
    return nodeMetrics;
}

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

License:Apache License

/**
 * Populates the node metrics hashmap//from ww w . j ava 2  s. c o m
 * 
 * @param nodes
 * @return
 */
private Map<String, Map<String, Double>> populateNodeMetrics(JsonArray nodes) {
    Map<String, Map<String, Double>> nodeMetrics = new HashMap<String, Map<String, Double>>();
    for (JsonElement node : nodes) {
        JsonObject nodeObject = node.getAsJsonObject();
        Map<String, Double> metrics = new HashMap<String, Double>();
        nodeMetrics.put(nodeObject.get("hostname").getAsString(), metrics);

        JsonObject interestingStats = nodeObject.getAsJsonObject("interestingStats");
        Iterator<Entry<String, JsonElement>> iterator = interestingStats.entrySet().iterator();
        populateMetricsMapHelper(iterator, metrics, "");

        JsonObject systemStats = nodeObject.getAsJsonObject("systemStats");
        iterator = systemStats.entrySet().iterator();
        populateMetricsMapHelper(iterator, metrics, "");

        iterator = nodeObject.entrySet().iterator();
        populateMetricsMapHelper(iterator, metrics, "");
    }
    return nodeMetrics;
}

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

License:Apache License

/**
 * Populates the bucket metrics hashmap//w  w w  .  ja v  a 2s  .c  o  m
 * 
 * @param buckets
 * @return
 */
private Map<String, Map<String, Double>> populateBucketMetrics(JsonArray buckets) {
    Map<String, Map<String, Double>> bucketMetrics = new HashMap<String, Map<String, Double>>();
    for (JsonElement bucket : buckets) {
        JsonObject bucketObject = bucket.getAsJsonObject();
        Map<String, Double> metrics = new HashMap<String, Double>();
        bucketMetrics.put(bucketObject.get("name").getAsString(), metrics);

        JsonObject interestingStats = bucketObject.getAsJsonObject("quota");
        Iterator<Entry<String, JsonElement>> iterator = interestingStats.entrySet().iterator();
        populateMetricsMapHelper(iterator, metrics, "");

        JsonObject systemStats = bucketObject.getAsJsonObject("basicStats");
        iterator = systemStats.entrySet().iterator();
        populateMetricsMapHelper(iterator, metrics, "");
    }
    return bucketMetrics;
}

From source file:com.arangodb.entity.EntityDeserializers.java

License:Apache License

private static JsonObject getFirstResultAsJsonObject(JsonObject obj) {
    if (obj.has("result")) {
        if (obj.get("result").isJsonArray()) {
            JsonArray result = obj.getAsJsonArray("result");

            if (result.size() > 0) {
                JsonElement jsonElement = result.get(0);
                if (jsonElement.isJsonObject()) {
                    return jsonElement.getAsJsonObject();
                }/* w  w w  .  ja v  a2  s . c o m*/
            }
        } else if (obj.get("result").isJsonObject()) {
            return obj.getAsJsonObject("result");
        }

    }
    return null;
}