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:io.thinger.thinger.DeviceResourceDescription.java

License:Open Source License

public DeviceResourceDescription(String resourceName, JsonElement resourceDescription) {
    this.resourceName = resourceName;
    this.resourceType = ResourceType.NONE;
    if (resourceDescription.isJsonObject()) {
        JsonObject object = resourceDescription.getAsJsonObject();
        if (object.has("fn")) {
            JsonElement function = object.get("fn");
            if (function.isJsonPrimitive()) {
                JsonPrimitive value = function.getAsJsonPrimitive();
                if (value.isNumber()) {
                    resourceType = ResourceType.get(value.getAsInt());
                }/*from   w w  w.  ja  v a 2  s.c  o m*/
            }
        }
    }
}

From source file:io.thinger.thinger.views.DeviceResource.java

License:Open Source License

public DeviceResource(String resourceName, JsonElement resourceDescription) {
    this.resourceName = resourceName;

    resourceType = ResourceType.NONE;/* w ww .  j a v a  2 s  .c  om*/

    if (resourceDescription.isJsonObject()) {
        JsonObject object = resourceDescription.getAsJsonObject();
        if (object.has("fn")) {
            JsonElement function = object.get("fn");
            if (function.isJsonPrimitive()) {
                JsonPrimitive value = function.getAsJsonPrimitive();
                if (value.isNumber()) {
                    resourceType = ResourceType.get(value.getAsInt());
                }
            }
        }
    }
}

From source file:json.GraphSONNodeDeserializer.java

License:Apache License

private Object getTypedValue(JsonPrimitive valueJson) {
    if (valueJson.isBoolean()) {
        return valueJson.getAsBoolean();
    } else if (valueJson.isNumber()) {
        return valueJson.getAsInt();
    } else {/*from  w w w . ja v  a  2  s  . c  om*/
        return valueJson.getAsString();
    }
}

From source file:me.boomboompower.togglechat.utils.BetterJsonObject.java

License:Open Source License

/**
 * The optional int method, returns the default value if
 * the key is null, empty or the data does not contain
 * the key. This will also return the default value if
 * the data value is not a number//from   w w  w  .ja v  a  2s.  co m
 *
 * @param key the key the value will be loaded from
 * @return the value in the json data set or the default if the key cannot be found
 */
public int optInt(String key, int value) {
    if (key == null || key.isEmpty() || !has(key)) {
        return value;
    }

    JsonPrimitive primitive = asPrimitive(get(key));

    try {
        if (primitive != null && primitive.isNumber()) {
            return primitive.getAsInt();
        }
    } catch (NumberFormatException ignored) {
    }
    return value;
}

From source file:net.ilexiconn.magister.adapter.type.AppointmentTypeAdapter.java

License:Apache License

@Override
public AppointmentType read(JsonReader in) throws IOException {
    JsonPrimitive primitive = gson.getAdapter(JsonPrimitive.class).read(in);
    int id = primitive.getAsInt();
    return AppointmentType.getTypeById(id);
}

From source file:net.ilexiconn.magister.adapter.type.DisplayTypeAdapter.java

License:Apache License

@Override
public DisplayType read(JsonReader in) throws IOException {
    JsonPrimitive primitive = gson.getAdapter(JsonPrimitive.class).read(in);
    int id = primitive.getAsInt();
    return DisplayType.getTypeById(id);
}

From source file:net.ilexiconn.magister.adapter.type.InfoTypeAdapter.java

License:Apache License

@Override
public InfoType read(JsonReader in) throws IOException {
    JsonPrimitive primitive = gson.getAdapter(JsonPrimitive.class).read(in);
    int id = primitive.getAsInt();
    return InfoType.getTypeById(id);
}

From source file:net.ilexiconn.magister.adapter.type.RowTypeAdapter.java

License:Apache License

@Override
public RowType read(JsonReader in) throws IOException {
    JsonPrimitive primitive = gson.getAdapter(JsonPrimitive.class).read(in);
    int id = primitive.getAsInt();
    return RowType.getTypeById(id);
}

From source file:org.eclim.Eclim.java

License:Open Source License

public static Object toType(JsonElement json) {
    // null//ww  w .  java 2  s.  co m
    if (json.isJsonNull()) {
        return null;

        // int, double, boolean, String
    } else if (json.isJsonPrimitive()) {
        JsonPrimitive prim = json.getAsJsonPrimitive();
        if (prim.isBoolean()) {
            return prim.getAsBoolean();
        } else if (prim.isString()) {
            return prim.getAsString();
        } else if (prim.isNumber()) {
            if (prim.getAsString().indexOf('.') != -1) {
                return prim.getAsDouble();
            }
            return prim.getAsInt();
        }

        // List
    } else if (json.isJsonArray()) {
        ArrayList<Object> type = new ArrayList<Object>();
        for (JsonElement element : json.getAsJsonArray()) {
            type.add(toType(element));
        }
        return type;

        // Map
    } else if (json.isJsonObject()) {
        HashMap<String, Object> type = new HashMap<String, Object>();
        for (Map.Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) {
            type.put(entry.getKey(), toType(entry.getValue()));
        }
        return type;
    }

    return null;
}

From source file:org.eclipse.ice.core.internal.Core.java

License:Open Source License

/**
 * This private operation creates an instance of the Message class from a
 * string using a JSON parser.//  ww w .  ja  v a2 s . c  o  m
 *
 * This operation is synchronized so that the core can't be overloaded.
 *
 * @param messageString
 *            The original message, as a string
 * @return list list of built messages.
 */
private ArrayList<Message> buildMessagesFromString(String messageString) {

    // Create the ArrayList of messages
    ArrayList<Message> messages = new ArrayList<Message>();

    // Create the parser and gson utility
    JsonParser parser = new JsonParser();
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.create();

    // Catch any exceptions and return the empty list
    try {

        // Make the string a json string
        JsonElement messageJson = parser.parse(messageString);
        JsonObject messageJsonObject = messageJson.getAsJsonObject();

        // Get the Item id from the json
        JsonPrimitive itemIdJson = messageJsonObject.getAsJsonPrimitive("item_id");
        int itemId = itemIdJson.getAsInt();

        // Get the array of posts from the message
        JsonArray jsonMessagesList = messageJsonObject.getAsJsonArray("posts");

        // Load the list
        for (int i = 0; i < jsonMessagesList.size(); i++) {
            // Get the message as a json element
            JsonElement jsonMessage = jsonMessagesList.get(i);
            // Marshal it into a message
            Message tmpMessage = gson.fromJson(jsonMessage, Message.class);
            // Set the item id
            if (tmpMessage != null) {
                tmpMessage.setItemId(itemId);
                // Put it in the list
                messages.add(tmpMessage);
            }
        }
    } catch (JsonParseException e) {
        // Log the message
        String err = "Core Message: " + "JSON parsing failed for message " + messageString;
        logger.error(getClass().getName() + " Exception!", e);
        logger.error(err);
    }

    return messages;
}