Example usage for com.google.gson JsonElement getAsByte

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

Introduction

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

Prototype

public byte getAsByte() 

Source Link

Document

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

Usage

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// w  w  w  .  jav  a  2  s . c  om
        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:com.asakusafw.testdriver.json.JsonObjectDriver.java

License:Apache License

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

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

License:Apache License

public byte getAsByte(String propPath) {
    JsonElement jsonElement = eval(propPath);
    return jsonElement.getAsByte();
}

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

License:Apache License

@Override
public N readNotNull(JsonElement element, DeserializationContext context) {
    Object toReturn;// w w  w.  j  a v  a2  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.graphaware.module.es.Neo4jElasticVerifier.java

License:Open Source License

private void checkByteArray(JsonObject source, String key, Map<String, Object> properties) {
    assertTrue(source.get(key) instanceof JsonArray);
    JsonArray jsonArray = source.get(key).getAsJsonArray();
    TreeSet<Byte> esSet = new TreeSet();
    for (JsonElement element : jsonArray) {
        esSet.add(element.getAsByte());
    }//from w  w w . ja  v a 2 s .  com
    TreeSet<Byte> nodeSet = new TreeSet();
    byte[] propertyArray = (byte[]) properties.get(key);
    for (byte element : propertyArray) {
        nodeSet.add(element);
    }
    assertEquals(esSet, nodeSet);
}

From source file:io.robusta.rra.representation.implementation.GsonRepresentation.java

License:Apache License

/**
 * convert a jsonElement into Object//from   w  w w .  j  a  v a 2 s.c  o  m
 * 
 * @param type
 * @param element
 * @return
 * @throws RepresentationException
 */
protected <T> T get(Class<T> type, JsonElement element) throws RepresentationException {

    if (type == Long.class) {
        return (T) (Long) element.getAsLong();
    } else if (type == Integer.class) {
        return (T) (Integer) element.getAsInt();
    } else if (type == Short.class) {
        return (T) (Short) element.getAsShort();
    } else if (type == Byte.class) {
        return (T) (Byte) element.getAsByte();
    } else if (type == BigInteger.class) {
        return (T) (BigInteger) element.getAsBigInteger();
    } else if (type == Double.class) {
        return (T) (Double) element.getAsDouble();
    } else if (type == Float.class) {
        return (T) (Float) element.getAsFloat();
    } else if (type == BigDecimal.class) {
        return (T) (BigDecimal) element.getAsBigDecimal();
    } else if (type == Boolean.class) {
        return (T) (Boolean) element.getAsBoolean();
    } else if (type == String.class) {
        return (T) element.getAsString();
    } else {
        return (T) gson.fromJson(element, type);
    }
}

From source file:net.doubledoordev.backend.util.TypeHellhole.java

License:Open Source License

public static void set(Field field, Object object, JsonElement value) throws Exception {
    if (field.getType() == byte.class)
        field.setByte(object, value.getAsByte());
    else if (field.getType() == short.class)
        field.setShort(object, value.getAsShort());
    else if (field.getType() == int.class)
        field.setInt(object, value.getAsInt());
    else if (field.getType() == long.class)
        field.setLong(object, value.getAsLong());
    else if (field.getType() == float.class)
        field.setFloat(object, value.getAsFloat());
    else if (field.getType() == double.class)
        field.setDouble(object, value.getAsDouble());
    else if (field.getType() == boolean.class)
        field.setBoolean(object, value.getAsBoolean());
    else if (field.getType() == char.class)
        field.setChar(object, value.getAsCharacter());
    ///*from  w  ww  . j  a v a  2  s .c o m*/
    else if (field.getType() == Byte.class)
        field.set(object, value.getAsByte());
    else if (field.getType() == Short.class)
        field.set(object, value.getAsShort());
    else if (field.getType() == Integer.class)
        field.set(object, value.getAsInt());
    else if (field.getType() == Long.class)
        field.set(object, value.getAsLong());
    else if (field.getType() == Float.class)
        field.set(object, value.getAsFloat());
    else if (field.getType() == Double.class)
        field.set(object, value.getAsDouble());
    else if (field.getType() == Boolean.class)
        field.set(object, value.getAsBoolean());
    else if (field.getType() == Character.class)
        field.set(object, value.getAsCharacter());
    //
    else if (field.getType() == String.class)
        field.set(object, value.getAsString());
    else {
        String m = String.format("Unknown type! Field type: %s Json value: %s Data class: %s", field.getType(),
                value.toString(), object.getClass().getSimpleName());
        Main.LOGGER.error(m);
        throw new Exception(m);
    }
}

From source file:org.greenrobot.eventbus.EventBus.java

License:Apache License

private Object getValue(Method.Data data, JsonObject json) {
    if (data == null)
        return null;

    String id = data.getId();//from   w ww . jav  a2s. co  m
    Class<?> clazz = data.getType();
    boolean isNull = data.getNull();

    JsonElement value = json == null ? new JsonNull() : json.get(id);
    if (!isNull && value.isJsonNull())
        throw new EventBusException(
                "Method.Data[" + data + "], the id[" + id + "] has null value to give " + data.getType());

    if (!value.isJsonNull()) {
        if (clazz == Boolean.class) {
            return value.getAsBoolean();
        } else if (clazz == Integer.class) {
            return value.getAsInt();
        } else if (clazz == Long.class) {
            return value.getAsLong();
        } else if (clazz == Float.class) {
            return value.getAsFloat();
        } else if (clazz == Double.class) {
            return value.getAsDouble();
        } else if (clazz == String.class) {
            return value.getAsString();
        } else if (clazz == Byte.class) {
            return value.getAsByte();
        } else if (clazz == char.class) {
            return value.getAsCharacter();
        } else {
            return new Gson().fromJson(value, clazz);
        }
    }
    return null;
}

From source file:org.greenrobot.eventbus.EventBus.java

License:Apache License

private Bundle getBundleByPage(Page page, JsonObject json) throws Exception {
    if (page != null && json != null && page.getBundleList().size() > 0) {
        Bundle bundle = new Bundle();
        for (Page.Bundle item : page.getBundleList()) {
            String id = item.getId();
            String key = item.getKey();
            Class<? extends Serializable> clazz = item.getType();
            boolean isNull = item.getNull();

            JsonElement value = json.get(id);
            if (!isNull && value.isJsonNull())
                throw new EventBusException(
                        "Page.Bundle[" + item + "], the id[" + id + "] has null value to give " + key);

            if (!value.isJsonNull()) {
                if (clazz == Boolean.class) {
                    bundle.putBoolean(key, value.getAsBoolean());
                } else if (clazz == Integer.class) {
                    bundle.putInt(key, value.getAsInt());
                } else if (clazz == Long.class) {
                    bundle.putLong(key, value.getAsLong());
                } else if (clazz == Float.class) {
                    bundle.putFloat(key, value.getAsFloat());
                } else if (clazz == Double.class) {
                    bundle.putDouble(key, value.getAsDouble());
                } else if (clazz == String.class) {
                    bundle.putString(key, value.getAsString());
                } else if (clazz == Byte.class) {
                    bundle.putByte(key, value.getAsByte());
                } else if (clazz == char.class) {
                    bundle.putChar(key, value.getAsCharacter());
                } else {
                    bundle.putSerializable(key, new Gson().fromJson(value, clazz));
                }//  www . j a  va  2s.com
            }
        }
        return bundle;
    }
    return null;
}

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

License:Open Source License

private Object getTypedValue(JsonElement valueElement, String type) {

    Object value = null;/*from www . java  2 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;
}