Example usage for com.google.gson JsonPrimitive isNumber

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

Introduction

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

Prototype

public boolean isNumber() 

Source Link

Document

Check whether this primitive contains a Number.

Usage

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;//from   w w  w.ja  v a  2 s.  c o  m

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

License:Open Source License

public static Element createPrimitiveElement(String name, JsonPrimitive primitive, LinearLayout layout,
        boolean output) {
    if (primitive.isBoolean()) {
        return new BoolValue(layout, name, primitive.getAsBoolean(), output);
    } else if (primitive.isNumber()) {
        return new NumberValue(layout, name, primitive.getAsNumber(), output);
    } else if (primitive.isString()) {
        return new StringValue(layout, name, primitive.getAsString(), output);
    }/*from  ww w  . j  a v  a  2 s . c o  m*/
    return null;
}

From source file:it.polimi.tower4clouds.data_analyzer.DAInputDataUnmarshaller.java

License:Apache License

@Override
public Model unmarshal(String inputData) throws Exception {
    //      logger.debug("Unmarshalling data");
    //      long startTime = System.currentTimeMillis();
    Model model = ModelFactory.createDefaultModel();

    JsonArray jsonData = jsonParser.parse(inputData).getAsJsonArray();
    //      logger.debug("{} monitoring datum json object(s) received",
    //            jsonData.size());

    for (JsonElement jsonElement : jsonData) {
        JsonObject jsonDatum = jsonElement.getAsJsonObject();
        Resource resourceDatum = model.createResource(UUID.randomUUID().toString()).addProperty(RDF.type,
                MO.MonitoringDatum);// w  ww . ja  v  a2 s .c om
        for (Entry<String, JsonElement> pair : jsonDatum.entrySet()) {
            String property = pair.getKey();
            JsonPrimitive value = pair.getValue().getAsJsonPrimitive();
            if (value.isBoolean()) {
                resourceDatum.addProperty(MO.makeProperty(property),
                        model.createTypedLiteral(value.getAsBoolean(), XSDDatatype.XSDboolean));
            } else if (value.isString()) {
                resourceDatum.addProperty(MO.makeProperty(property),
                        model.createTypedLiteral(value.getAsString(), XSDDatatype.XSDstring));
            } else if (value.isNumber()) {
                resourceDatum.addProperty(MO.makeProperty(property),
                        model.createTypedLiteral(value.getAsNumber().doubleValue(), XSDDatatype.XSDdouble));
            } else {
                logger.error("Unknown datum property: {}", value);
            }
        }
    }
    //      logger.debug("Unmarshalling completed in {} seconds",
    //            ((double) (System.currentTimeMillis() - startTime)) / 1000);

    return model;
}

From source file:jp.pay.model.EventDataDeserializer.java

License:Open Source License

private Object deserializeJsonPrimitive(JsonPrimitive element) {
    if (element.isBoolean()) {
        return element.getAsBoolean();
    } else if (element.isNumber()) {
        return element.getAsNumber();
    } else {/*from w  w  w  .ja  v  a2  s .c o  m*/
        return element.getAsString();
    }
}

From source file:jp.yokomark.utils.gson.utils.JsonElementUtils.java

License:Open Source License

public static long getAsLongOrThrow(JsonPrimitive primitive) throws JsonParseException {
    if (!primitive.isNumber()) {
        throw new JsonParseException("this primitive is not a json number: " + primitive);
    }/*from www . j  av a2s  .co m*/
    return primitive.getAsLong();
}

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 {//  w w w.j a  v  a  2  s  .  co  m
        return valueJson.getAsString();
    }
}

From source file:JsonParser.ParseJson.java

public static void dumpJSONElement(JsonElement element, String type) {
    if (element.isJsonObject()) {
        //System.out.println("Is an object");
        JsonObject obj = element.getAsJsonObject();
        java.util.Set<java.util.Map.Entry<String, JsonElement>> entries = obj.entrySet();
        java.util.Iterator<java.util.Map.Entry<String, JsonElement>> iter = entries.iterator();
        while (iter.hasNext()) {
            java.util.Map.Entry<String, JsonElement> entry = iter.next();
            //  System.out.println("Key: " + entry.getKey());
            if (entry.getKey().toString().equals("instances")) {
                System.out.println("............Topic: ");
                dumpJSONElement(entry.getValue(), "topic");

            }//from w  w  w  .  j  a  v  a2  s.  com
            if (entry.getKey().toString().equals("aspects")) {
                System.out.println("............aspects: ");
                dumpJSONElement(entry.getValue(), "aspect");

            } else if (entry.getKey().toString().equals("positives")) {
                System.out.println(".............Positive Words ");
                dumpJSONElement(entry.getValue(), "positive");
            } else if (entry.getKey().toString().equals("negatives")) {
                System.out.println(".............negatives Words ");
                dumpJSONElement(entry.getValue(), "negative");
            } else {
                dumpJSONElement(entry.getValue(), "");
            }

        }

    } else if (element.isJsonArray()) {
        JsonArray array = element.getAsJsonArray();
        //System.out.println("Is an array. Number of values: " + array.size());
        java.util.Iterator<JsonElement> iter = array.iterator();
        while (iter.hasNext()) {
            JsonElement entry = iter.next();

            dumpJSONElement(entry, "");
        }
    } else if (element.isJsonPrimitive()) {
        //System.out.println("Is a primitive");
        JsonPrimitive value = element.getAsJsonPrimitive();
        if (value.isBoolean()) {
            //System.out.println("Is boolean: " + value.getAsBoolean());
        } else if (value.isNumber()) {
            //  System.out.println("Is number: " + value.getAsNumber());
        } else if (value.isString()) {
            //if(!value.getAsString().equals("empty"))
            //{
            //if(type.equals("topic"))
            //{
            System.out.println(type + " :" + value.getAsString());
            //}
        }
    } else if (element.isJsonNull()) {
        System.out.println("Is NULL");
    } else {
        System.out.println("Error. Unknown type of element");
    }
}

From source file:leola.web.WebLeolaLibrary.java

License:Open Source License

/**
 * Converts the {@link JsonElement} into the equivalent {@link LeoObject}
 * /*  ww w.j a  v  a2 s.co m*/
 * @param element
 * @return the {@link LeoObject}
 */
private static LeoObject toLeoObject(JsonElement element) {
    if (element == null || element.isJsonNull()) {
        return LeoObject.NULL;
    }

    if (element.isJsonArray()) {
        JsonArray array = element.getAsJsonArray();
        LeoArray leoArray = new LeoArray(array.size());
        array.forEach(e -> leoArray.add(toLeoObject(e)));
        return leoArray;
    }

    if (element.isJsonObject()) {
        JsonObject object = element.getAsJsonObject();
        LeoMap leoMap = new LeoMap();
        object.entrySet().forEach(entry -> {
            leoMap.putByString(entry.getKey(), toLeoObject(entry.getValue()));
        });

        return leoMap;
    }

    if (element.isJsonPrimitive()) {
        JsonPrimitive primitive = element.getAsJsonPrimitive();
        if (primitive.isBoolean()) {
            return LeoObject.valueOf(primitive.getAsBoolean());
        }

        if (primitive.isNumber()) {
            return LeoObject.valueOf(primitive.getAsDouble());
        }

        if (primitive.isString()) {
            return LeoString.valueOf(primitive.getAsString());
        }
    }

    return LeoObject.NULL;
}

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   www .j av a 2  s  .  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:me.boomboompower.togglechat.utils.BetterJsonObject.java

License:Open Source License

/**
 * The optional double 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 .  j a v  a2 s .c  o 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 double optDouble(String key, double value) {
    if (key == null || key.isEmpty() || !has(key)) {
        return value;
    }

    JsonPrimitive primitive = asPrimitive(get(key));

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