Example usage for com.google.gson JsonElement getAsShort

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

Introduction

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

Prototype

public short getAsShort() 

Source Link

Document

convenience method to get this element as a primitive short 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  .  j av  a2 s  .  c o m*/
        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 shortProperty(PropertyName name, JsonObject context) throws IOException {
    JsonElement prop = property(context, name);
    if (prop == null) {
        return;//from w  ww .j  a  va2  s.c o m
    }
    builder.add(name, prop.getAsShort());
}

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

License:Apache License

public short getAsShort(String propPath) {
    JsonElement jsonElement = eval(propPath);
    return jsonElement.getAsShort();
}

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

License:Apache License

@Override
public N readNotNull(JsonElement element, DeserializationContext context) {
    Object toReturn;/*from   w  w w. j a  va  2s. co 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 checkShortArray(JsonObject source, String key, Map<String, Object> properties) {
    assertTrue(source.get(key) instanceof JsonArray);
    JsonArray jsonArray = source.get(key).getAsJsonArray();
    TreeSet<Short> esSet = new TreeSet();
    for (JsonElement element : jsonArray) {
        esSet.add(element.getAsShort());
    }/*from   w ww  . j  av a2  s.  co m*/
    TreeSet<Short> nodeSet = new TreeSet();
    short[] propertyArray = (short[]) properties.get(key);
    for (short 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 ww . ja va2  s  .com
 * 
 * @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 ava2 s .  c om*/
    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.structr.core.PropertySetGSONAdapter.java

License:Open Source License

private Object getTypedValue(JsonElement valueElement, String type) {

    Object value = null;/*from w w  w  .  java2  s .c o  m*/

    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.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 w  w .  ja  v  a  2  s  .com*/
                        } 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;
}

From source file:org.virtue.cache.tools.ItemDataPacker.java

License:Open Source License

/**
 * Encodes a single entry within the ObjType config
 * @param buffer The buffer to pack data into
 * @param param The name of the param to encode
 * @param value The value to encode// ww  w.jav  a 2s . c o  m
 * @throws IOException If something goes wrong in the encoding process
 */
private static void encode(DataOutputStream buffer, String param, JsonElement value) throws IOException {
    if ("name".equalsIgnoreCase(param)) {
        buffer.writeByte(2);
        BufferUtility.writeString(buffer, value.getAsString());
    } else if ("desc".equalsIgnoreCase(param)) {
        buffer.writeByte(3);
        BufferUtility.writeString(buffer, value.getAsString());
    } else if ("tradeable".equalsIgnoreCase(param) && value.getAsBoolean()) {
        buffer.writeByte(9);
    } else if ("weight".equalsIgnoreCase(param)) {
        buffer.writeByte(10);
        buffer.writeShort(value.getAsShort());
    }
}