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:augsburg.se.alltagsguide.utilities.Helper.java

License:Open Source License

public static float getFloatOrDefault(@NonNull JsonElement elem, float defaultValue) {
    try {//w w  w.  j  a  va  2s . c  om
        return elem.getAsFloat();
    } catch (Exception e) {
        Ln.d(e);
        return defaultValue;
    }
}

From source file:be.iminds.iot.dianne.jsonrpc.DianneRequestHandler.java

License:Open Source License

private Tensor asTensor(JsonArray array) {
    // support up to 3 dim input atm
    int dim0 = 1;
    int dim1 = 1;
    int dim2 = 1;

    int dims = 1;
    dim0 = array.size();/*from  ww w  .  j  a va  2  s .  co m*/
    if (array.get(0).isJsonArray()) {
        dims = 2;
        JsonArray a = array.get(0).getAsJsonArray();
        dim1 = a.size();
        if (a.get(0).isJsonArray()) {
            dims = 3;
            dim2 = a.get(0).getAsJsonArray().size();
        }
    }

    int size = dim0 * dim1 * dim2;
    float[] data = new float[size];
    int k = 0;
    for (int i = 0; i < dim0; i++) {
        for (int j = 0; j < dim1; j++) {
            for (int l = 0; l < dim2; l++) {
                JsonElement e = array.get(i);
                if (e.isJsonArray()) {
                    e = e.getAsJsonArray().get(j);
                    if (e.isJsonArray()) {
                        e = e.getAsJsonArray().get(l);
                    }
                }
                data[k++] = e.getAsFloat();
            }
        }
    }

    int[] d = new int[dims];
    d[0] = dim0;
    if (dims > 1)
        d[1] = dim1;
    if (dims > 2)
        d[2] = dim2;

    return new Tensor(data, d);
}

From source file:br.ufg.inf.es.saep.sandbox.dominio.infraestrutura.ValorDeserializer.java

License:Creative Commons License

@Override
public Valor deserialize(JsonElement jsonElement, Type type,
        JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
    JsonObject jsonObject = jsonElement.getAsJsonObject();
    byte tipo = jsonObject.get("tipo").getAsByte();
    JsonElement valor = jsonObject.get("valor");

    if (tipo == Valor.STRING) {
        return new Valor(valor.getAsString());
    } else if (tipo == REAL) {
        return new Valor(valor.getAsFloat());
    } else if (tipo == LOGICO) {
        return new Valor(valor.getAsBoolean());
    } else if (tipo == DATA) {
        return Valor.dataFromString(valor.getAsString());
    }/*w w  w .  j a  v a2 s .co  m*/

    return null;
}

From source file:brooklyn.event.feed.http.JsonFunctions.java

License:Apache License

@SuppressWarnings("unchecked")
public static <T> Function<JsonElement, T> cast(final Class<T> expected) {
    return new Function<JsonElement, T>() {
        @Override//from w ww .  j  a v a2 s .com
        public T apply(JsonElement input) {
            if (input == null) {
                return (T) null;
            } else if (input.isJsonNull()) {
                return (T) null;
            } else if (expected == boolean.class || expected == Boolean.class) {
                return (T) (Boolean) input.getAsBoolean();
            } else if (expected == char.class || expected == Character.class) {
                return (T) (Character) input.getAsCharacter();
            } else if (expected == byte.class || expected == Byte.class) {
                return (T) (Byte) input.getAsByte();
            } else if (expected == short.class || expected == Short.class) {
                return (T) (Short) input.getAsShort();
            } else if (expected == int.class || expected == Integer.class) {
                return (T) (Integer) input.getAsInt();
            } else if (expected == long.class || expected == Long.class) {
                return (T) (Long) input.getAsLong();
            } else if (expected == float.class || expected == Float.class) {
                return (T) (Float) input.getAsFloat();
            } else if (expected == double.class || expected == Double.class) {
                return (T) (Double) input.getAsDouble();
            } else if (expected == BigDecimal.class) {
                return (T) input.getAsBigDecimal();
            } else if (expected == BigInteger.class) {
                return (T) input.getAsBigInteger();
            } else if (Number.class.isAssignableFrom(expected)) {
                // TODO Will result in a class-cast if it's an unexpected sub-type of Number not handled above
                return (T) input.getAsNumber();
            } else if (expected == String.class) {
                return (T) input.getAsString();
            } else if (expected.isArray()) {
                JsonArray array = input.getAsJsonArray();
                Class<?> componentType = expected.getComponentType();
                if (JsonElement.class.isAssignableFrom(componentType)) {
                    JsonElement[] result = new JsonElement[array.size()];
                    for (int i = 0; i < array.size(); i++) {
                        result[i] = array.get(i);
                    }
                    return (T) result;
                } else {
                    Object[] result = (Object[]) Array.newInstance(componentType, array.size());
                    for (int i = 0; i < array.size(); i++) {
                        result[i] = cast(componentType).apply(array.get(i));
                    }
                    return (T) result;
                }
            } else {
                throw new IllegalArgumentException("Cannot cast json element to type " + expected);
            }
        }
    };
}

From source file:ch.cern.db.flume.sink.kite.parser.JSONtoAvroParser.java

License:GNU General Public License

private Object getElementAsType(Schema schema, JsonElement element) {
    if (element == null || element.isJsonNull())
        return null;

    switch (schema.getType()) {
    case BOOLEAN:
        return element.getAsBoolean();
    case DOUBLE:/* w w  w  .j a va 2s .  c  o m*/
        return element.getAsDouble();
    case FLOAT:
        return element.getAsFloat();
    case INT:
        return element.getAsInt();
    case LONG:
        return element.getAsLong();
    case NULL:
        return null;
    case UNION:
        return getElementAsType(schema.getTypes().get(0), element);
    //      case FIXED:
    //      case ARRAY:
    //      case BYTES:
    //      case ENUM:
    //      case MAP:
    //      case RECORD:
    //      case STRING:
    default:
        return element.getAsString();
    }
}

From source file:com.asakusafw.testdriver.json.JsonObjectDriver.java

License:Apache License

@Override
public void floatProperty(PropertyName name, JsonObject context) throws IOException {
    JsonElement prop = property(context, name);
    if (prop == null) {
        return;/*from  w  w  w. ja  v a 2  s  .com*/
    }
    builder.add(name, prop.getAsFloat());
}

From source file:com.b12kab.tmdblibrary.AccountStateDeserializer.java

License:Apache License

@Override
public AccountState deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    AccountState accountState = new AccountState();
    String convertVariable = "";
    JsonObject obj = (JsonObject) json;//from   www .j  a  va  2 s  .c o m
    JsonElement element;
    JsonElement ratingElement;
    JsonPrimitive rating;
    try {
        convertVariable = "id";
        element = obj.get("id");
        if (element != null) {
            if (element.isJsonPrimitive()) {
                if (element.getAsJsonPrimitive().isNumber()) {
                    accountState.setId(element.getAsInt());
                }
            }
        }

        convertVariable = "favorite";
        element = obj.get("favorite");
        if (element != null) {
            if (element.isJsonPrimitive()) {
                if (element.getAsJsonPrimitive().isBoolean()) {
                    accountState.setFavorite(element.getAsBoolean());
                }
            }
        }

        accountState.setRated(null);
        convertVariable = "rated";
        element = obj.get("rated");
        if (element != null) {
            if (element.isJsonObject()) {
                if (element.getAsJsonObject().has("value")) {
                    ratingElement = element.getAsJsonObject().get("value");
                    if (ratingElement.isJsonPrimitive()) {
                        //                        rating
                        if (ratingElement.getAsJsonPrimitive().isNumber()) {
                            accountState.setRated(ratingElement.getAsFloat());
                        }
                    }
                }
            }
        }

        convertVariable = "watchlist";
        element = obj.get("watchlist");
        if (element != null) {
            if (element.isJsonPrimitive()) {
                if (element.getAsJsonPrimitive().isBoolean()) {
                    accountState.setWatchlist(element.getAsBoolean());
                }
            }
        }
    } catch (NullPointerException npe) {
        Log.e(TAG, "Processing " + convertVariable + " response from Tmdb, NullPointerException occurred"
                + npe.toString());
        return null;
    } catch (NumberFormatException npe) {
        Log.e(TAG, "Processing " + convertVariable + " response from Tmdb, NumberFormatException occurred"
                + npe.toString());
        return null;
    } catch (IllegalStateException ise) {
        Log.e(TAG, "Processing " + convertVariable + " response from Tmdb, IllegalStateException occurred"
                + ise.toString());
    }

    return accountState;
}

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

License:Apache License

public float getAsFloat(String propPath) {
    JsonElement jsonElement = eval(propPath);
    return jsonElement.getAsFloat();
}

From source file:com.getperka.flatpack.codexes.NumberCodex.java

License:Apache License

@Override
public N readNotNull(JsonElement element, DeserializationContext context) {
    Object toReturn;//from www.  ja v a 2  s . c o m
    if (BigDecimal.class.equals(clazz)) {
        toReturn = element.getAsBigDecimal();
    } else if (BigInteger.class.equals(clazz)) {
        toReturn = element.getAsBigInteger();
    } else if (Byte.class.equals(clazz)) {
        toReturn = element.getAsByte();
    } else if (Double.class.equals(clazz)) {
        toReturn = element.getAsDouble();
    } else if (Float.class.equals(clazz)) {
        toReturn = element.getAsFloat();
    } else if (Integer.class.equals(clazz)) {
        toReturn = element.getAsInt();
    } else if (Long.class.equals(clazz)) {
        toReturn = element.getAsLong();
    } else if (Short.class.equals(clazz)) {
        toReturn = element.getAsShort();
    } else {
        throw new UnsupportedOperationException("Unimplemented Number type " + clazz.getName());
    }
    return clazz.cast(toReturn);
}

From source file:com.github.kyriosdata.regras.infraestrutura.ValorDeserializer.java

License:Creative Commons License

@Override
public Valor deserialize(JsonElement jsonElement, Type type,
        JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
    JsonObject jsonObject = jsonElement.getAsJsonObject();
    byte tipo = jsonObject.get("tipo").getAsByte();
    JsonElement valor = jsonObject.get("valor");

    if (tipo == Valor.STRING) {
        return new Valor(valor.getAsString());
    } else if (tipo == Valor.REAL) {
        return new Valor(valor.getAsFloat());
    } else if (tipo == Valor.LOGICO) {
        return new Valor(valor.getAsBoolean());
    } else if (tipo == Valor.DATA) {
        return Valor.dataFromString(valor.getAsString());
    }/*ww  w.  j  a  v  a2  s. c o  m*/

    return null;
}