Example usage for com.google.gson JsonPrimitive getAsDouble

List of usage examples for com.google.gson JsonPrimitive getAsDouble

Introduction

In this page you can find the example usage for com.google.gson JsonPrimitive getAsDouble.

Prototype

@Override
public double getAsDouble() 

Source Link

Document

convenience method to get this element as a primitive double.

Usage

From source file:rpc.server.data.JSONSerializer.java

License:Open Source License

private Object fromJsonElement(JsonElement jsonElement, Type expected) throws NoSuitableSerializableFactory {

    if (jsonElement == null) {
        return null;
    }/*www  .  ja  v  a2  s.c om*/

    // Null
    if (jsonElement.isJsonNull()) {
        return null;
    }

    // Boolean
    // Integer
    // Long
    // Float
    // Double
    // String
    // Enum
    if (jsonElement.isJsonPrimitive()) {
        JsonPrimitive asJsonPrimitive = jsonElement.getAsJsonPrimitive();

        if (asJsonPrimitive.isBoolean()) {
            return asJsonPrimitive.getAsBoolean();
        }

        if (asJsonPrimitive.isNumber()) {
            if (expected.isInteger()) {
                return asJsonPrimitive.getAsInt();
            }

            if (expected.isLong()) {
                return asJsonPrimitive.getAsLong();
            }

            if (expected.isFloat()) {
                return asJsonPrimitive.getAsFloat();
            }

            if (expected.isDouble()) {
                return asJsonPrimitive.getAsDouble();
            }

            return asJsonPrimitive.getAsNumber();
        }

        if (asJsonPrimitive.isString()) {
            if (expected.isEnum()) {
                String value = asJsonPrimitive.getAsString();
                return Enum.valueOf((Class) expected.getTypeClass(), value);
            } else {
                return asJsonPrimitive.getAsString();
            }
        }
    }

    // Map
    // Serializable
    if (jsonElement.isJsonObject()) {
        JsonObject asJsonObject = jsonElement.getAsJsonObject();

        if (expected.isMap()) {
            Map<Object, Object> map = new HashMap<Object, Object>();

            Type keyType = expected.getParameterized(0);
            Type valueType = expected.getParameterized(1);

            if (!(keyType.isString() || keyType.isEnum())) {
                return null;
            }

            for (Map.Entry<String, JsonElement> entry : asJsonObject.entrySet()) {

                String key = entry.getKey();
                JsonElement value = entry.getValue();

                if (keyType.isString()) {
                    map.put(entry.getKey(), fromJsonElement(value, valueType));
                }

                if (keyType.isEnum()) {
                    map.put(Enum.valueOf((Class) keyType.getTypeClass(), key),
                            fromJsonElement(value, valueType));
                }
            }

            return map;
        } else {
            if (provider == null) {
                throw new NoSuitableSerializableFactory();
            }

            Serializable object = provider.make(expected);

            for (Map.Entry<String, Type> entry : object.fields().entrySet()) {

                String field = entry.getKey();
                Type fieldType = entry.getValue();

                JsonElement value = asJsonObject.get(field);
                object.set(field, fromJsonElement(value, fieldType));
            }

            return object;
        }
    }

    // List
    if (jsonElement.isJsonArray()) {
        JsonArray asJsonArray = jsonElement.getAsJsonArray();

        int size = asJsonArray.size();

        List<Object> list = new ArrayList<Object>();
        Type itemType = expected.getParameterized(0);

        for (int i = 0; i < size; i++) {
            JsonElement value = asJsonArray.get(i);
            list.add(fromJsonElement(value, itemType));
        }

        return list;
    }

    return null;
}