Example usage for com.google.gson JsonElement getAsJsonObject

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

Introduction

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

Prototype

public JsonObject getAsJsonObject() 

Source Link

Document

convenience method to get this element as a JsonObject .

Usage

From source file:com.couchbase.cbadmin.assets.NodeGroupList.java

License:Open Source License

public NodeGroupList(JsonObject json) throws RestApiException {
    JsonElement e = json.get("uri");
    if (e == null || e.isJsonPrimitive() == false) {
        throw new RestApiException("Expected modification URI", json);
    }/*from  w w w .java 2  s.  c  o  m*/
    assignmentUri = URI.create(e.getAsString());

    e = json.get("groups");
    if (e == null || e.isJsonArray() == false) {
        throw new RestApiException("Expected 'groups'", e);
    }

    groups = new ArrayList<NodeGroup>();
    for (JsonElement groupElem : e.getAsJsonArray()) {
        if (groupElem.isJsonObject() == false) {
            throw new RestApiException("Expected object for group", groupElem);
        }
        groups.add(new NodeGroup(groupElem.getAsJsonObject()));
    }
}

From source file:com.couchbase.cbadmin.client.ConnectionInfo.java

License:Open Source License

public ConnectionInfo(JsonObject poolsJson) throws RestApiException {
    JsonElement eTmp;
    eTmp = poolsJson.get("implementationVersion");
    if (eTmp == null) {
        throw new RestApiException("implementationVersion missing", poolsJson);
    }// w w  w . j  a va  2s. co m
    clusterVersion = eTmp.getAsString();

    eTmp = poolsJson.get("pools");
    if (eTmp == null || eTmp.isJsonArray() == false) {
        throw new RestApiException("pools missing", poolsJson);
    }

    if (eTmp.getAsJsonArray().size() > 0) {
        clusterActive = true;
        eTmp = eTmp.getAsJsonArray().get(0);

        if (eTmp.isJsonObject() == false) {
            throw new RestApiException("Expected object in pools entry", eTmp);
        }

        eTmp = eTmp.getAsJsonObject().get("uri");
        if (eTmp == null || eTmp.isJsonPrimitive() == false) {
            throw new RestApiException("uri missing or malformed", eTmp);
        }

        clusterId = eTmp.getAsString();

    } else {
        clusterActive = false;
        clusterId = null;
    }

    eTmp = poolsJson.get("isAdminCreds");
    adminCreds = eTmp != null && eTmp.getAsBoolean();
}

From source file:com.couchbase.cbadmin.client.CouchbaseAdmin.java

License:Open Source License

private JsonElement extractResponse(HttpResponse res, HttpRequestBase req, int expectCode)
        throws RestApiException, IOException {

    JsonElement ret;
    HttpEntity entity;//from w  ww . j  av  a 2s.c  o m
    entity = res.getEntity();
    if (entity == null) {
        ret = new JsonObject();

    } else {
        Header contentType = entity.getContentType();
        if (contentType == null || contentType.getValue().contains("json") == false) {
            ret = new JsonObject();
            ret.getAsJsonObject().addProperty("__raw_response", IOUtils.toString(entity.getContent()));
        } else {
            JsonReader reader = new JsonReader(new InputStreamReader(entity.getContent()));
            ret = gs.fromJson(reader, JsonObject.class);
        }
    }
    if (res.getStatusLine().getStatusCode() != expectCode) {
        throw new RestApiException(ret, res.getStatusLine(), req);
    }

    return ret;
}

From source file:com.couchbase.cbadmin.client.CouchbaseAdmin.java

License:Open Source License

@Override
public Map<String, Bucket> getBuckets() throws RestApiException {
    JsonElement e;// www  .j  a va  2s  .c  o  m
    Map<String, Bucket> ret = new HashMap<String, Bucket>();

    try {
        e = getJson(P_BUCKETS);
    } catch (IOException ex) {
        throw new RestApiException(ex);
    }

    JsonArray arr;
    if (!e.isJsonArray()) {
        throw new RestApiException("Expected JsonObject", e);
    }

    arr = e.getAsJsonArray();
    for (int i = 0; i < arr.size(); i++) {
        JsonElement tmpElem = arr.get(i);
        if (!tmpElem.isJsonObject()) {
            throw new RestApiException("Expected JsonObject", tmpElem);
        }

        Bucket bucket = new Bucket(tmpElem.getAsJsonObject());
        ret.put(bucket.getName(), bucket);
    }
    return ret;
}

From source file:com.couchbase.cbadmin.client.CouchbaseAdmin.java

License:Open Source License

@Override
public NodeGroupList getGroupList() throws RestApiException {
    JsonElement e;
    Map<String, NodeGroup> ret = new HashMap<String, NodeGroup>();
    try {/*  ww  w .  j a  va2  s. c  om*/
        e = getJson(_P_SERVERGROUPS);
    } catch (IOException ex) {
        throw new RestApiException(ex);
    }

    if (!e.isJsonObject()) {
        throw new RestApiException("Expected JSON object", e);
    }
    return new NodeGroupList(e.getAsJsonObject());
}

From source file:com.couchbase.cbadmin.client.CouchbaseAdmin.java

License:Open Source License

@Override
public List<Node> getNodes() throws RestApiException {
    List<Node> ret = new ArrayList<Node>();
    JsonElement e;
    try {/*from   w ww . j  a v  a  2 s.  co m*/
        e = getJson(P_POOL_NODES);
    } catch (IOException ex) {
        throw new RestApiException(ex);
    }

    if (!e.isJsonObject()) {
        throw new RestApiException("Expected JsonObject", e);
    }

    JsonObject obj = e.getAsJsonObject();
    JsonArray nodesArr;
    e = obj.get("nodes");

    if (e == null) {
        throw new RestApiException("Expected 'nodes' array", obj);
    }

    nodesArr = e.getAsJsonArray();
    for (int i = 0; i < nodesArr.size(); i++) {
        e = nodesArr.get(i);
        JsonObject nObj;
        if (!e.isJsonObject()) {
            throw new RestApiException("Malformed node entry", e);
        }
        nObj = e.getAsJsonObject();
        Node n = new Node(nObj);
        ret.add(n);
    }

    return ret;
}

From source file:com.couchbase.cbadmin.client.CouchbaseAdmin.java

License:Open Source License

@Override
public RebalanceInfo getRebalanceStatus() throws RestApiException {
    JsonElement js;

    try {//  w w  w.ja  v a2 s  .c  o  m
        js = getResponseJson(new HttpGet(), P_REBALANCE_PROGRESS, 200);
    } catch (IOException ex) {
        throw new RestApiException(ex);
    }

    if (!js.isJsonObject()) {
        throw new RestApiException("Expected JSON object", js);
    }
    return new RebalanceInfo(js.getAsJsonObject());
}

From source file:com.couchbase.cbadmin.client.CouchbaseAdmin.java

License:Open Source License

@Override
public ConnectionInfo getInfo() throws RestApiException {
    try {//from  ww  w.j a va 2 s .  c  o  m
        JsonElement js = getResponseJson(new HttpGet(), P_POOLS, 200);
        if (!js.isJsonObject()) {
            throw new RestApiException("Expected JSON Object", js);
        }

        return new ConnectionInfo(js.getAsJsonObject());

    } catch (IOException ex) {
        throw new RestApiException(ex);
    }
}

From source file:com.crypticbit.diff.demo.swing.contacts.JsonJTreeNode.java

License:Apache License

private void populateChildren(JsonElement myJsonElement) {
    switch (dataType) {
    case ARRAY:/* w w w  . ja v a2  s.co m*/
        int index = 0;
        Iterator<JsonElement> it = myJsonElement.getAsJsonArray().iterator();
        while (it.hasNext()) {
            JsonElement element = it.next();
            JsonJTreeNode childNode = new JsonJTreeNode(null, index, element);
            this.add(childNode);
            index++;
        }
        break;
    case OBJECT:
        for (Entry<String, JsonElement> entry : myJsonElement.getAsJsonObject().entrySet()) {
            JsonJTreeNode childNode = new JsonJTreeNode(entry.getKey(), -1, entry.getValue());
            this.add(childNode);
        }
        break;
    default:
        throw new IllegalStateException("Internal coding error this should never happen.");
    }
}

From source file:com.crypticbit.diff.demo.swing.JSONJTreeNode.java

License:Apache License

private void populateChildren(JsonElement myJsonElement) {
    switch (dataType) {
    case ARRAY:/*from   www.  j av a2  s  . co m*/
        int index = 0;
        Iterator<JsonElement> it = myJsonElement.getAsJsonArray().iterator();
        while (it.hasNext()) {
            JsonElement element = it.next();
            JSONJTreeNode childNode = new JSONJTreeNode(null, index, element);
            this.add(childNode);
            index++;
        }
        break;
    case OBJECT:
        for (Entry<String, JsonElement> entry : myJsonElement.getAsJsonObject().entrySet()) {
            JSONJTreeNode childNode = new JSONJTreeNode(entry.getKey(), -1, entry.getValue());
            this.add(childNode);
        }
        break;
    default:
        throw new IllegalStateException("Internal coding error this should never happen.");
    }
}