Example usage for com.google.gson JsonElement getAsFloat

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

Introduction

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

Prototype

public float getAsFloat() 

Source Link

Document

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

Usage

From source file:org.runbuddy.libtomahawk.infosystem.hatchet.Store.java

License:Open Source License

public float getAsFloat(JsonObject object, String memberName) throws IOException {
    JsonElement element = get(object, memberName);
    if (element != null && element.isJsonPrimitive()) {
        return element.getAsFloat();
    }//  w w  w  .  ja  v a 2s.c o m
    return -1;
}

From source file:org.sourcepit.osgifier.core.ee.impl.DefaultExecutionEnvironmentContributor.java

License:Apache License

private static float getFloatValue(JsonObject jsonObject, String property) {
    final JsonElement jsonElement = jsonObject.get(property);
    return jsonElement == null ? -1 : jsonElement.getAsFloat();
}

From source file:org.structr.core.PropertySetGSONAdapter.java

License:Open Source License

private Object getTypedValue(JsonElement valueElement, String type) {

    Object value = null;/*from www .  j a  va2 s. c  om*/

    if ((type == null) || type.equals("null")) {

        value = valueElement.getAsJsonNull();

    } else if (type.equals("String")) {

        value = valueElement.getAsString();

    } else if (type.equals("Number")) {

        value = valueElement.getAsNumber();

    } else if (type.equals("Boolean")) {

        value = valueElement.getAsBoolean();

    } else if (type.equals("JsonArray")) {

        value = valueElement.getAsJsonArray();

    } else if (type.equals("JsonObject")) {

        value = valueElement.getAsJsonObject();

    } else if (type.equals("Integer")) {

        value = valueElement.getAsInt();

    } else if (type.equals("Long")) {

        value = valueElement.getAsLong();

    } else if (type.equals("Double")) {

        value = valueElement.getAsDouble();

    } else if (type.equals("Float")) {

        value = valueElement.getAsFloat();

    } else if (type.equals("Byte")) {

        value = valueElement.getAsByte();

    } else if (type.equals("Short")) {

        value = valueElement.getAsShort();

    } else if (type.equals("Character")) {

        value = valueElement.getAsCharacter();

    } else if (type.equals("BigDecimal")) {

        value = valueElement.getAsBigDecimal();

    } else if (type.equals("BigInteger")) {

        value = valueElement.getAsBigInteger();

    }

    return value;
}

From source file:org.terasology.persistence.typeHandling.gson.GsonPersistedDataArray.java

License:Apache License

@Override
public TFloatList getAsFloatArray() {
    TFloatList result = new TFloatArrayList(size());
    for (JsonElement element : array) {
        result.add(element.getAsFloat());
    }/*from w w  w  . j a v  a2s . co  m*/
    return result;
}

From source file:org.uorm.serializer.DefaultJsonSerializer.java

License:Apache License

@Override
public Map<String, Object> deserialize2(Class<?> cls, String json) throws Exception {
    JsonStreamParser parser = new JsonStreamParser(json);
    if (parser.hasNext()) {
        JsonObject jsonobj = parser.next().getAsJsonObject();
        Set<Entry<String, JsonElement>> jset = jsonobj.entrySet();
        if (!jset.isEmpty()) {
            Map<String, PropertyDescriptor> propMap = ObjectMappingCache.getInstance()
                    .getObjectPropertyMap(cls);
            Map<String, Object> instance = new HashMap<String, Object>();
            for (Entry<String, JsonElement> entry : jset) {
                String name = entry.getKey();
                JsonElement val = entry.getValue();
                if (!val.isJsonNull()) {
                    PropertyDescriptor descriptor = propMap.get(name);
                    if (descriptor != null) {
                        Class<?> memberType = descriptor.getPropertyType();
                        if (memberType == String.class) {
                            instance.put(name, val.getAsString());
                        } else if (memberType == Integer.class || memberType == Integer.TYPE) {
                            instance.put(name, val.getAsInt());
                        } else if (memberType == Byte.class || memberType == Byte.TYPE) {
                            instance.put(name, val.getAsByte());
                        } else if (memberType == Double.class || memberType == Double.TYPE) {
                            instance.put(name, val.getAsDouble());
                        } else if (memberType == Float.class || memberType == Float.TYPE) {
                            instance.put(name, val.getAsFloat());
                        } else if (memberType == Long.class || memberType == Long.TYPE) {
                            instance.put(name, val.getAsLong());
                        } else if (memberType == Short.class || memberType == Short.TYPE) {
                            instance.put(name, val.getAsShort());
                        } else if (memberType == Boolean.class || memberType == Boolean.TYPE) {
                            instance.put(name, val.getAsBoolean());
                        } else if (memberType == BigDecimal.class) {
                            instance.put(name, val.getAsBigDecimal());
                        } else if (memberType == BigInteger.class) {
                            instance.put(name, val.getAsBigInteger());
                        } else if (memberType == byte[].class) {
                            instance.put(name, val.getAsString().getBytes());
                        } else if (memberType == java.sql.Timestamp.class) {
                            Long time = parserDate(val.getAsString());
                            if (time == null) {
                                instance.put(name, null);
                            } else {
                                instance.put(name, new java.sql.Timestamp(time));
                            }/*from  w ww  .  j  ava2 s  .co m*/
                        } else if (memberType == java.sql.Date.class) {
                            Long time = parserDate(val.getAsString());
                            if (time == null) {
                                instance.put(name, null);
                            } else {
                                instance.put(name, new java.sql.Date(time));
                            }
                        } else if (memberType == java.util.Date.class) {
                            Long time = parserDate(val.getAsString());
                            if (time == null) {
                                instance.put(name, null);
                            } else {
                                instance.put(name, new java.util.Date(time));
                            }
                        } else {//default String
                            instance.put(name, val.getAsString());
                        }
                    } else {//String
                        instance.put(name, val.getAsString());
                    }
                }
            }
            return instance;
        }
    }
    return null;
}