Example usage for com.google.gson JsonPrimitive getAsInt

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

Introduction

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

Prototype

@Override
public int getAsInt() 

Source Link

Document

convenience method to get this element as a primitive integer.

Usage

From source file:ar.com.ws.djnextension.config.GsonBuilderConfiguratorForTesting.java

License:Open Source License

/**
 * @param parent// ww  w  .  j  av  a 2s.com
 * @param elementName
 * @return
 */
private static int getIntValue(JsonObject parent, String elementName) {
    assert parent != null;
    assert !StringUtils.isEmpty(elementName);

    JsonElement element = parent.get(elementName);
    if (!element.isJsonPrimitive()) {
        throw new JsonParseException("Element + '" + elementName + "' must be a valid integer");
    }
    JsonPrimitive primitiveElement = (JsonPrimitive) element;
    if (!primitiveElement.isNumber()) {
        throw new JsonParseException("Element + '" + elementName + "' must be a valid integer");
    }
    return primitiveElement.getAsInt();
}

From source file:at.ac.tuwien.infosys.jcloudscale.datastore.mapping.type.json.JsonIntegerTypeAdapter.java

License:Apache License

@Override
public Integer deserialize(JsonElement element, TypeMetadata<JsonElement> typeMetadata) {
    JsonPrimitive jsonPrimitive = (JsonPrimitive) element;
    if (!jsonPrimitive.isNumber()) {
        throw new DatastoreException("Invalid value for integer type.");
    }//from   w w w.  j  a  v a 2  s.  c o  m
    return jsonPrimitive.getAsInt();
}

From source file:com.builtbroken.builder.html.parts.HTMLPartHeader.java

@Override
public String process(JsonElement value) {
    try {//w ww .j av a 2s.com
        if (value.isJsonObject() && !value.isJsonPrimitive()) {
            JsonObject h = value.getAsJsonObject();
            String text = h.getAsJsonPrimitive("text").getAsString();
            int size = 2;
            if (h.has("size")) {
                JsonPrimitive p = h.getAsJsonPrimitive("size");
                if (p.isString()) {
                    String s = p.getAsString().toLowerCase();
                    if (s.equals("small")) {
                        size = 3;
                    } else if (s.equals("medium")) {
                        size = 2;
                    } else if (s.equals("large")) {
                        size = 1;
                    }
                } else {
                    size = p.getAsInt();
                }
            }
            if (h.has("link")) {
                String link = h.getAsJsonPrimitive("link").getAsString();
                if (link.startsWith("url")) {
                    return "<h" + size + "><a href=\"" + link + "\">" + text + "</a></h" + size + ">";
                } else if (link.endsWith(".json")) {
                    return "<h" + size + "><a href=\"#PageRef:" + link + "#\">" + text + "</a></h" + size + ">";
                } else {
                    return "<h" + size + "><a href=\"#" + link + "#\">" + text + "</a></h" + size + ">";
                }
            }
            return "<h" + size + ">" + text + "</h" + size + ">";
        }
        return "<h2>" + value.getAsString() + "</h2>";
    } catch (Exception e) {
        throw new RuntimeException("Unexpected error while parsing header tag " + value, e);
    }
}

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()));

        }/*ww  w  .ja v a  2  s .co 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.getperka.flatpack.codexes.CharacterCodex.java

License:Apache License

@Override
public Character readNotNull(JsonElement element, DeserializationContext context) throws Exception {
    if (element.isJsonPrimitive()) {
        JsonPrimitive primitive = element.getAsJsonPrimitive();
        // Treat a number as a BMP code point
        if (primitive.isNumber()) {
            return (char) primitive.getAsInt();
        } else {/*  ww w. ja  v  a2s .com*/
            return primitive.getAsCharacter();
        }
    }
    throw new IllegalArgumentException("Cannot convert " + element.toString() + " to a char");
}

From source file:com.google.iosched.model.validator.IntegerToStringConverter.java

License:Open Source License

@Override
public JsonPrimitive convert(JsonPrimitive value) {
    if (value == null) {
        return null;
    }/*from   w  w  w  .java2s  .co  m*/
    if (value.isNumber()) {
        return new JsonPrimitive(String.valueOf(value.getAsInt()));
    }
    try {
        Integer.parseInt(value.getAsString());
        // if it parses correctly, it is validated:
        return value;
    } catch (NumberFormatException ex) {
        throw new ConverterException(value, this);
    }
}

From source file:com.hbm.devices.jet.JetPeer.java

License:Open Source License

private void handleSingleJsonMessage(JsonObject object) {
    JsonPrimitive fetchId = getFetchId(object);
    if (fetchId != null) {
        handleFetch(fetchId.getAsInt(), object);
        return;/*from w  w  w  .  jav a2  s.c o m*/
    }

    if (isResponse(object)) {
        handleResponse(object);
        return;
    }

    handleStateOrMethodCallbacks(object);
}

From source file:com.hbm.devices.jet.JetPeer.java

License:Open Source License

private void handleResponse(JsonObject object) {
    JsonPrimitive token = object.getAsJsonPrimitive("id");
    int id = token.getAsInt();
    synchronized (openRequests) {
        JetMethod method = openRequests.get(id);
        if (method == null) {
            return;
        }/*from ww  w . j a v  a2 s .c o m*/
        openRequests.remove(id);
        method.getFuture().cancel(true);
        method.callResponseCallback(true, object);
    }
}

From source file:com.helion3.safeguard.util.DataUtil.java

License:MIT License

/**
 * Convert JSON to a DataContainer.//from w ww.ja  va 2  s.  c  o  m
 * @param json JsonObject
 * @return DataContainer
 */
public static DataContainer jsonToDataContainer(JsonObject json) {
    DataContainer result = new MemoryDataContainer();

    for (Entry<String, JsonElement> entry : json.entrySet()) {
        DataQuery keyQuery = DataQuery.of(entry.getKey());
        Object object = entry.getValue();

        if (object instanceof JsonObject) {
            result.set(keyQuery, jsonToDataContainer((JsonObject) object));
        } else if (object instanceof JsonArray) {
            JsonArray array = (JsonArray) object;
            Iterator<JsonElement> iterator = array.iterator();

            List<Object> list = new ArrayList<Object>();
            while (iterator.hasNext()) {
                Object child = iterator.next();

                if (child instanceof JsonPrimitive) {
                    JsonPrimitive primitive = (JsonPrimitive) child;

                    if (primitive.isString()) {
                        list.add(primitive.getAsString());
                    } else if (primitive.isNumber()) {
                        // @todo may be wrong format. how do we handle?
                        list.add(primitive.getAsInt());
                    } else if (primitive.isBoolean()) {
                        list.add(primitive.getAsBoolean());
                    } else {
                        SafeGuard.getLogger()
                                .error("Unhandled list JsonPrimitive data type: " + primitive.toString());
                    }
                }
            }

            result.set(keyQuery, list);
        } else {
            if (object instanceof JsonPrimitive) {
                JsonPrimitive primitive = (JsonPrimitive) object;

                if (primitive.isString()) {
                    result.set(keyQuery, primitive.getAsString());
                } else if (primitive.isNumber()) {
                    // @todo may be wrong format. how do we handle?
                    result.set(keyQuery, primitive.getAsInt());
                } else if (primitive.isBoolean()) {
                    result.set(keyQuery, primitive.getAsBoolean());
                } else {
                    SafeGuard.getLogger().error("Unhandled JsonPrimitive data type: " + primitive.toString());
                }
            }
        }
    }

    return result;
}

From source file:com.javacreed.examples.gson.part3.AuthorDeserializer.java

License:Apache License

@Override
public Author deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)
        throws JsonParseException {

    // Only the ID is available
    if (json.isJsonPrimitive()) {
        final JsonPrimitive primitive = json.getAsJsonPrimitive();
        return getOrCreate(primitive.getAsInt());
    }/*from   ww  w .  java2  s .  c om*/

    // The whole object is available
    if (json.isJsonObject()) {
        final JsonObject jsonObject = json.getAsJsonObject();

        final Author author = getOrCreate(jsonObject.get("id").getAsInt());
        author.setName(jsonObject.get("name").getAsString());
        return author;
    }

    throw new JsonParseException("Unexpected JSON type: " + json.getClass().getSimpleName());
}