Example usage for com.google.gson JsonElement getAsInt

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

Introduction

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

Prototype

public int getAsInt() 

Source Link

Document

convenience method to get this element as a primitive integer value.

Usage

From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java

License:Open Source License

/**
 * Sets the json element at the specified jpath.
 *
 * @param source the source//from  w  w w.ja v  a2 s  .co  m
 * @param jpath the fully qualified json path to the field required. eg set({'a': {'b': {'c': [1,
 *     2, 3, 4]}}}, ["a", "b" "c", 1, 5]) is {'a': {'b': {'c': [1, 5, 3, 4]}}}. Array indexes need
 *     to be specified as numerals. Strings are always presumed to be field names.
 * @param value the value
 * @return the json element
 */
public JsonElement set(JsonElement source, JsonArray jpath, JsonElement value) {
    JsonElement result = JsonNull.INSTANCE;
    if (isNotNull(jpath)) {
        result = source;
        JsonElement field = result;
        if (jpath.size() > 0) {
            JsonElement previousPath = null;
            JsonElement currentPath = null;
            Iterator<JsonElement> iterator = jpath.iterator();
            if (iterator.hasNext()) {
                previousPath = iterator.next();
            }

            while (iterator.hasNext()) {
                currentPath = iterator.next();
                // get the field
                field = getRepleteField(field, previousPath, currentPath);
                result = updateResult(result, field);
                field = isNumber(previousPath) ? field.getAsJsonArray().get(previousPath.getAsInt())
                        : field.getAsJsonObject().get(previousPath.getAsString());
                previousPath = currentPath;
            }

            field = setField(field, previousPath, value);
        }
    }
    return result;
}

From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java

License:Open Source License

/**
 * Returns the source json as an Integer if possible. Else returns the default integer
 *
 * @param source the source json element
 * @param defaultInt the default integer
 * @return the source json as an integer
 *//*  w w  w  . ja  v  a  2 s .c  o  m*/
public Integer asInt(JsonElement source, Integer defaultInt) {
    return isNumber(source) ? (Integer) source.getAsInt() : defaultInt;
}

From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java

License:Open Source License

/**
 * Sets the field.// w  ww . j  a  va  2  s .  co  m
 *
 * @param field the field
 * @param path the path
 * @param value the value
 * @return the json element
 */
protected JsonElement setField(JsonElement field, JsonElement path, JsonElement value) {
    if (isNumber(path)) {
        JsonArray jArray = asJsonArray(field.getAsJsonArray(), new JsonArray());
        int index = path.getAsInt();
        repleteArray(jArray, index, JsonNull.class);
        jArray.set(index, value);
        field = jArray;
    } else {
        JsonObject jObject = asJsonObject(field, new JsonObject());
        jObject.add(path.getAsString(), value);
        field = jObject;
    }
    return field;
}

From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java

License:Open Source License

/**
 * Gets the replete field.//  w w  w.  j  a  va  2s  .c  o m
 *
 * @param field the field
 * @param path the path
 * @param nextPath the next path
 * @return the replete field
 */
protected JsonElement getRepleteField(JsonElement field, JsonElement path, JsonElement nextPath) {
    if (isNumber(path)) {
        Integer index = path.getAsInt();
        if (!isArray(field)) {
            field = new JsonArray();
        }
        JsonArray fArray = repleteArray(field.getAsJsonArray(), index,
                isNumber(nextPath) ? JsonArray.class : JsonObject.class);
        field = fArray;
    } else {
        String fieldName = path.getAsString();
        if (!isObject(field)) {
            field = new JsonObject();
        }
        JsonObject fJson = repleteJson(field.getAsJsonObject(), fieldName,
                isNumber(nextPath) ? JsonArray.class : JsonObject.class);
        field = fJson;
    }
    return field;
}

From source file:com.bynder.sdk.util.BooleanTypeAdapter.java

License:Open Source License

/**
 * Check {@link JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext)} for
 * more information.//  w  w w . ja  va  2  s .  com
 */
@Override
public Boolean deserialize(final JsonElement json, final Type typeOfT,
        final JsonDeserializationContext context) {
    if (Arrays.asList(Boolean.TRUE.toString(), Boolean.FALSE.toString()).contains(json.toString())) {
        return json.getAsBoolean();
    } else {
        try {
            int code = json.getAsInt();

            if (code == 0) {
                return false;
            } else if (code == 1) {
                return true;
            } else {
                return null;
            }
        } catch (NumberFormatException e) {
            return null;
        }
    }
}

From source file:com.cloud.bridge.util.JsonAccessor.java

License:Apache License

public int getAsInt(String propPath) {
    JsonElement jsonElement = eval(propPath);
    return jsonElement.getAsInt();
}

From source file:com.cognifide.aet.job.common.comparators.w3chtml5.parser.W3cHtml5IssueDeserializer.java

License:Apache License

private int getAsInt(JsonObject jsonObject, String key) {
    JsonElement element = jsonObject.get(key);
    if (element != null) {
        return element.getAsInt();
    }/*from w w  w.  j  a va 2 s .  c  om*/
    return 0;
}

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

License:Open Source License

public Node(JsonObject def) throws RestApiException {
    JsonElement eTmp;
    String sTmp;//from ww  w  .ja va 2 s. c  o  m
    eTmp = def.get("hostname");
    if (eTmp == null) {
        throw new RestApiException("Expected 'hostname'", def);
    }
    sTmp = eTmp.getAsString();

    try {
        restUrl = new URL("http://" + sTmp + "/");
    } catch (MalformedURLException ex) {
        throw new RuntimeException(ex);
    }

    eTmp = def.get("couchApiBase");
    if (eTmp != null) {
        try {
            sTmp = eTmp.getAsString();
            couchUrl = new URL(sTmp);
        } catch (MalformedURLException ex) {
            throw new RuntimeException(ex);
        }
    }

    eTmp = def.get("version");
    if (eTmp == null) {
        throw new RestApiException("Expected 'version' in nodes JSON", def);
    }
    versionString = eTmp.getAsString();

    eTmp = def.get("otpNode");
    if (eTmp == null) {
        throw new RestApiException("Expected 'otpNode'", def);
    }
    NSOtpNode = eTmp.getAsString();

    eTmp = def.get("clusterCompatibility");
    if (eTmp != null) {
        clusterCompatVersion = eTmp.getAsInt();
    }

    eTmp = def.get("clusterMembership");
    if (eTmp != null) {
        sTmp = eTmp.getAsString();

        if (sTmp.equals("active")) {
            membership = Membership.ACTIVE;
        } else if (sTmp.equals("inactiveAdded")) {
            membership = Membership.INACTIVE_ADDED;

        } else if (sTmp.equals("inactiveFailed")) {
            membership = Membership.INACTIVE_FAILED;
        }
    }

    eTmp = def.get("status");
    if (eTmp != null) {
        sTmp = eTmp.getAsString();
        if (sTmp.equals("healthy")) {
            status = Status.HEALTHY;
        } else if (sTmp.equals("unhealthy")) {
            status = Status.UNHEALTHY;
        } else if (sTmp.equals("warmup")) {
            status = Status.WARMUP;
        }
    }
}

From source file:com.devamatre.core.JSONHelper.java

License:Open Source License

/**
 * /*from  ww w  .j a  v a2s. co m*/
 * @param jsonObject
 * @param propertName
 * @return
 */
public static Integer valueForKeyAsInteger(JsonObject jsonObject, String propertName) {
    JsonElement jsonElement = jsonObject.get(propertName);
    return (jsonElement == null ? null : jsonElement.getAsInt());
}

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

License:Open Source License

/**
 * Forms a {@link Map} with attachment//  w ww. j  a  va  2s .  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;
}