Example usage for com.google.gson JsonElement isJsonPrimitive

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

Introduction

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

Prototype

public boolean isJsonPrimitive() 

Source Link

Document

provides check for verifying if this element is a primitive or not.

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.  j  av a  2  s. c  om
    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);
    }//from   w  w w . java  2  s.c  o 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.RebalanceInfo.java

License:Open Source License

public RebalanceInfo(JsonObject obj) throws RestApiException {
    JsonElement e = obj.get("status");
    if (e == null || e.isJsonPrimitive() == false) {
        throw new RestApiException("Expected status string", obj);
    }//from  w  ww. ja va2s  . co m
    String sStatus = e.getAsString();
    if (sStatus.equals("none")) {
        completed = true;

    } else if (sStatus.equals("running")) {
        for (Entry<String, JsonElement> ent : obj.entrySet()) {
            if (ent.getKey().equals("status")) {
                continue;
            }
            JsonObject progressObj;
            if (ent.getValue().isJsonObject() == false) {
                throw new RestApiException("Expected object", ent.getValue());
            }
            progressObj = ent.getValue().getAsJsonObject();
            JsonElement progressNum = progressObj.get("progress");
            if (progressNum.isJsonPrimitive() == false
                    || progressNum.getAsJsonPrimitive().isNumber() == false) {
                throw new RestApiException("Expected 'progress' to be number", progressNum);
            }
            details.put(ent.getKey(), progressNum.getAsNumber().floatValue() * 100);
        }
    }
}

From source file:com.couchbase.plugin.basement.api.Client.java

License:Open Source License

private void addJsonElement(String key, JsonElement value, HashMap<String, Object> map) {

    if (value.isJsonArray()) {
        JsonArray jsonArray = value.getAsJsonArray();

        ArrayList listAsValue = new ArrayList<HashMap<String, Object>>();

        HashMap<String, Object> listElementMap = new HashMap<String, Object>();

        Iterator<JsonElement> jsonArrayIterator = jsonArray.iterator();
        while (jsonArrayIterator.hasNext()) {
            JsonElement jsonElement = jsonArrayIterator.next();

            listAsValue.add(parse(jsonElement.toString()));

        }//from  w ww.  j ava2s .c o m

        map.put(key, listAsValue);

    } else if (value.isJsonPrimitive()) {
        // check the type using JsonPrimitive
        // TODO: check all types
        JsonPrimitive jsonPrimitive = value.getAsJsonPrimitive();
        if (jsonPrimitive.isNumber()) {
            map.put(key, jsonPrimitive.getAsInt());
        } else {
            map.put(key, jsonPrimitive.getAsString());
        }
    } else {
        map.put(key, parse(value.toString()));
    }

}

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

License:Apache License

/**
 * @param fieldName//from ww  w.  java2 s . c  o  m
 *            - name of field if applicable or null
 * @param index
 *            - index of element in the array or -1 if not part of an array
 * @param jsonElement
 *            - element to represent
 */
public JsonJTreeNode(String fieldName, int index, JsonElement jsonElement) {
    this.index = index;
    this.fieldName = fieldName;
    if (jsonElement.isJsonArray()) {
        this.dataType = DataType.ARRAY;
        this.value = jsonElement.toString();
        populateChildren(jsonElement);
    } else if (jsonElement.isJsonObject()) {
        this.dataType = DataType.OBJECT;
        this.value = jsonElement.toString();
        populateChildren(jsonElement);
    } else if (jsonElement.isJsonPrimitive()) {
        this.dataType = DataType.VALUE;
        this.value = jsonElement.toString();
    } else if (jsonElement.isJsonNull()) {
        this.dataType = DataType.VALUE;
        this.value = jsonElement.toString();
    } else {
        throw new IllegalArgumentException("jsonElement is an unknown element type.");
    }

}

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

License:Apache License

/**
 * @param fieldName//from   ww w  .j  a v a 2  s  .  c  om
 *            - name of field if applicable or null
 * @param index
 *            - index of element in the array or -1 if not part of an array
 * @param jsonElement
 *            - element to represent
 */
public JSONJTreeNode(String fieldName, int index, JsonElement jsonElement) {
    this.index = index;
    this.fieldName = fieldName;
    if (jsonElement.isJsonArray()) {
        this.dataType = DataType.ARRAY;
        this.value = jsonElement.toString();
        populateChildren(jsonElement);
    } else if (jsonElement.isJsonObject()) {
        this.dataType = DataType.OBJECT;
        this.value = jsonElement.toString();
        populateChildren(jsonElement);
    } else if (jsonElement.isJsonPrimitive()) {
        this.dataType = DataType.VALUE;
        this.value = jsonElement.toString();
    } else if (jsonElement.isJsonNull()) {
        this.dataType = DataType.VALUE;
        this.value = jsonElement.toString();
    } else {
        throw new IllegalArgumentException("jsonElement is an unknown element type.");
    }

}

From source file:com.devicehive.websockets.WebSocketRequestProcessor.java

License:Apache License

private WebsocketAction getAction(JsonObject request) {
    JsonElement action = request.get(JsonMessageBuilder.ACTION);
    if (action == null || !action.isJsonPrimitive()) {
        return WebsocketAction.EMPTY;
    }//from  ww  w  . ja  v  a2  s .  com
    return WebsocketAction.forName(action.getAsString());
}

From source file:com.diversityarrays.dalclient.DalUtil.java

License:Open Source License

static public DalResponseRecord createFrom(String requestUrl, String tagName, JsonObject input) {
    DalResponseRecord result = new DalResponseRecord(requestUrl, tagName);
    for (Map.Entry<String, JsonElement> entry : input.entrySet()) {
        String key = entry.getKey();
        JsonElement value = entry.getValue();
        if (value == null || value.isJsonNull()) {
            result.rowdata.put(key, "");
        } else if (value.isJsonArray()) {
            JsonArray array = (JsonArray) value;
            int count = 0;
            for (JsonElement elt : array) {
                if (elt != null && elt.isJsonObject()) {
                    JsonObject child = (JsonObject) elt;
                    Map<String, String> childMap = asRowdata(child);
                    if (childMap != null) {
                        result.addNestedData(key, childMap);
                    }/*  w  w  w . j av a 2 s .  c om*/
                } else {
                    result.warnings.add(String.format("unexpected value-type for '%s'[%d] :%s", //$NON-NLS-1$
                            key, count, elt == null ? "null" : elt.getClass().getName()));
                }
                ++count;
            }
        } else if (value.isJsonPrimitive()) {
            JsonPrimitive prim = (JsonPrimitive) value;
            result.rowdata.put(key, prim.getAsString());
        } else if (value.isJsonObject()) {
            // ?? perhaps
            Map<String, String> childMap = asRowdata((JsonObject) value);
            if (childMap != null) {
                result.addNestedData(key, childMap);
            }
        } else {
            result.warnings.add(String.format("unexpected value-type for '%s' :%s", //$NON-NLS-1$
                    key, value.getClass().getName()));
        }
    }
    return result;
}

From source file:com.driver733.vkuploader.wallpost.attachment.support.PropertiesUpdate.java

License:Open Source License

/**
 * Forms a {@link Map} with attachment/*w w  w.  j a  v  a2  s.c  o  m*/
 *  attachmentStrings with corresponding indexes.
 * @return A {@link Map} with attachment
 *  attachmentStrings with corresponding indexes.
 */
private Map<Integer, String> resStrings() {
    final List<Integer> integers = new ArrayList<>(this.ids.keySet());
    final Map<Integer, String> results = new HashMap<>();
    int index = 0;
    for (final JsonElement element : this.root) {
        if (element.isJsonPrimitive()) {
            results.put(integers.get(index), String.valueOf(element.getAsInt()));
            index += 1;
        }
    }
    return results;
}

From source file:com.edduarte.argus.reader.JsonReader.java

License:Apache License

private void readRecursive(JsonElement root, MutableString collector) {
    if (root.isJsonObject()) {
        JsonObject object = (JsonObject) root;
        object.entrySet().forEach(e -> {
            collector.append(e.getKey());
            collector.append(' ');
            readRecursive(e.getValue(), collector);
            collector.append(' ');
        });/*from  w  w w. j  a v a2s. c  o m*/
    } else if (root.isJsonArray()) {
        JsonArray array = (JsonArray) root;
        array.forEach(child -> {
            readRecursive(child, collector);
            collector.append(' ');
        });
    } else if (root.isJsonPrimitive()) {
        JsonPrimitive primitive = (JsonPrimitive) root;
        collector.append(primitive.getAsString());
        collector.append(' ');
    }
}