Example usage for com.google.gson JsonElement getAsBigInteger

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

Introduction

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

Prototype

public BigInteger getAsBigInteger() 

Source Link

Document

convenience method to get this element as a BigInteger .

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 ww.ja  v  a  2 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 integerProperty(PropertyName name, JsonObject context) throws IOException {
    JsonElement prop = property(context, name);
    if (prop == null) {
        return;//from w w  w  . jav  a2 s  .c  o m
    }
    builder.add(name, prop.getAsBigInteger());
}

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

License:Apache License

public BigInteger getAsBigInteger(String propPath) {
    JsonElement jsonElement = eval(propPath);
    return jsonElement.getAsBigInteger();
}

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

License:Apache License

@Override
public N readNotNull(JsonElement element, DeserializationContext context) {
    Object toReturn;//  ww w .  j av  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:io.robusta.rra.representation.implementation.GsonRepresentation.java

License:Apache License

/**
 * convert a jsonElement into Object//from  www .  j  ava2  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:org.structr.core.PropertySetGSONAdapter.java

License:Open Source License

private Object getTypedValue(JsonElement valueElement, String type) {

    Object value = null;/*from   w ww .  ja v a 2 s . co 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));
                            }/*ww  w. j  ava  2s. c om*/
                        } 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;
}