Example usage for com.google.gson JsonPrimitive isNumber

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

Introduction

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

Prototype

public boolean isNumber() 

Source Link

Document

Check whether this primitive contains a Number.

Usage

From source file:pI.generator.JsonJavaMapper.java

License:Open Source License

public Object json2JavaPrimitive(JsonPrimitive prim) {
    if (prim.isBoolean()) {
        return prim.getAsBoolean();
    } else if (prim.isString()) {
        return prim.getAsString();
    } else if (prim.isNumber()) {
        return prim.getAsNumber();
    } else {//from ww w  .  j  av a  2s.  com
        throw new IllegalStateException();
    }
}

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;
    }/*from   w  ww  .j a  v a 2 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;
}

From source file:tk.breezy64.pantex.core.ConfigManager.java

private static Object jsonElementToObject(JsonElement e) {
    if (e.isJsonObject()) {
        return jsonObjectToMap(e.getAsJsonObject());
    } else if (e.isJsonPrimitive()) {
        JsonPrimitive p = e.getAsJsonPrimitive();
        if (p.isNumber()) {
            return p.getAsInt();
        } else if (p.isString()) {
            return p.getAsString();
        } else if (p.isBoolean()) {
            return p.getAsBoolean();
        }//w  w w .  ja  v  a2 s . c om
    } else if (e.isJsonArray()) {
        List<Object> list = new ArrayList<>();
        e.getAsJsonArray().forEach((x) -> list.add(jsonElementToObject(x)));
        return list;
    }
    return null;
}