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:angularBeans.remote.InvocationHandler.java

License:LGPL

private void update(Object o, JsonObject params) {

    if (params != null) {

        // boolean firstIn = false;

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

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

            if ((name.equals("sessionUID")) || (name.equals("args"))) {
                continue;
            }//ww w  .  j a va2  s . c om

            if ((value.isJsonObject()) && (!value.isJsonNull())) {

                String getName;
                try {
                    getName = CommonUtils.obtainGetter(o.getClass().getDeclaredField(name));

                    Method getter = o.getClass().getMethod(getName);

                    Object subObj = getter.invoke(o);

                    // logger.log(Level.INFO, "#entring sub object "+name);
                    update(subObj, value.getAsJsonObject());

                } catch (NoSuchFieldException | SecurityException | IllegalAccessException
                        | IllegalArgumentException | InvocationTargetException | NoSuchMethodException e) {

                    e.printStackTrace();
                }

            }
            // ------------------------------------
            if (value.isJsonArray()) {

                try {
                    String getter = CommonUtils.obtainGetter(o.getClass().getDeclaredField(name));

                    Method get = o.getClass().getDeclaredMethod(getter);

                    Type type = get.getGenericReturnType();
                    ParameterizedType pt = (ParameterizedType) type;
                    Type actType = pt.getActualTypeArguments()[0];

                    String className = actType.toString();

                    className = className.substring(className.indexOf("class") + 6);
                    Class clazz = Class.forName(className);

                    JsonArray array = value.getAsJsonArray();

                    Collection collection = (Collection) get.invoke(o);
                    Object elem;
                    for (JsonElement element : array) {
                        if (element.isJsonPrimitive()) {
                            JsonPrimitive primitive = element.getAsJsonPrimitive();

                            elem = element;
                            if (primitive.isBoolean())
                                elem = primitive.getAsBoolean();
                            if (primitive.isString()) {
                                elem = primitive.getAsString();
                            }
                            if (primitive.isNumber())
                                elem = primitive.isNumber();

                        } else {

                            elem = util.deserialise(clazz, element);
                        }

                        try {

                            if (collection instanceof List) {

                                if (collection.contains(elem))
                                    collection.remove(elem);
                            }

                            collection.add(elem);
                        } catch (UnsupportedOperationException e) {
                            Logger.getLogger("AngularBeans").log(java.util.logging.Level.WARNING,
                                    "trying to modify an immutable collection : " + name);
                        }

                    }

                } catch (Exception e) {
                    e.printStackTrace();

                }

            }

            // ------------------------------------------
            if (value.isJsonPrimitive() && (!name.equals("setSessionUID"))) {
                try {

                    if (!CommonUtils.hasSetter(o.getClass(), name)) {
                        continue;
                    }
                    name = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);

                    Class type = null;
                    for (Method set : o.getClass().getDeclaredMethods()) {
                        if (CommonUtils.isSetter(set)) {
                            if (set.getName().equals(name)) {
                                Class<?>[] pType = set.getParameterTypes();

                                type = pType[0];
                                break;

                            }
                        }

                    }

                    if (type.equals(LobWrapper.class))
                        continue;

                    Object param = null;
                    if ((params.entrySet().size() >= 1) && (type != null)) {

                        param = CommonUtils.convertFromString(value.getAsString(), type);

                    }

                    o.getClass().getMethod(name, type).invoke(o, param);

                } catch (Exception e) {
                    e.printStackTrace();

                }
            }

        }
    }

}

From source file:ar.com.ws.djnextension.config.GsonBuilderConfiguratorForTesting.java

License:Open Source License

/**
 * @param parent//from www . j ava2 s  .com
 * @param elementName
 * @return
 */
private static int getIntValue(JsonObject parent, String elementName) {
    assert parent != null;
    assert !StringUtils.isEmpty(elementName);

    JsonElement element = parent.get(elementName);
    if (!element.isJsonPrimitive()) {
        throw new JsonParseException("Element + '" + elementName + "' must be a valid integer");
    }
    JsonPrimitive primitiveElement = (JsonPrimitive) element;
    if (!primitiveElement.isNumber()) {
        throw new JsonParseException("Element + '" + elementName + "' must be a valid integer");
    }
    return primitiveElement.getAsInt();
}

From source file:at.ac.tuwien.infosys.jcloudscale.datastore.mapping.type.json.JsonDoubleTypeAdapter.java

License:Apache License

@Override
public Double deserialize(JsonElement element, TypeMetadata<JsonElement> typeMetadata) {
    JsonPrimitive jsonPrimitive = (JsonPrimitive) element;
    if (!jsonPrimitive.isNumber()) {
        throw new DatastoreException("Invalid value for double type.");
    }//www  . j  a va2  s .  c om
    return jsonPrimitive.getAsDouble();
}

From source file:at.ac.tuwien.infosys.jcloudscale.datastore.mapping.type.json.JsonFloatTypeAdapter.java

License:Apache License

@Override
public Float deserialize(JsonElement element, TypeMetadata<JsonElement> typeMetadata) {
    JsonPrimitive jsonPrimitive = (JsonPrimitive) element;
    if (!jsonPrimitive.isNumber()) {
        throw new DatastoreException("Invalid value for float type.");
    }/*from ww w  .  ja v  a2s.  co  m*/
    return jsonPrimitive.getAsFloat();
}

From source file:at.ac.tuwien.infosys.jcloudscale.datastore.mapping.type.json.JsonIntegerTypeAdapter.java

License:Apache License

@Override
public Integer deserialize(JsonElement element, TypeMetadata<JsonElement> typeMetadata) {
    JsonPrimitive jsonPrimitive = (JsonPrimitive) element;
    if (!jsonPrimitive.isNumber()) {
        throw new DatastoreException("Invalid value for integer type.");
    }/*www  . j ava2s.c o  m*/
    return jsonPrimitive.getAsInt();
}

From source file:at.ac.tuwien.infosys.jcloudscale.datastore.mapping.type.json.JsonLongTypeAdapter.java

License:Apache License

@Override
public Long deserialize(JsonElement element, TypeMetadata<JsonElement> typeMetadata) {
    JsonPrimitive jsonPrimitive = (JsonPrimitive) element;
    if (!jsonPrimitive.isNumber()) {
        throw new DatastoreException("Invalid value for long type.");
    }//from   ww w  . j a v a2 s  .  c  o m
    return jsonPrimitive.getAsLong();
}

From source file:at.ac.tuwien.infosys.jcloudscale.datastore.mapping.type.json.JsonShortTypeAdapter.java

License:Apache License

@Override
public Short deserialize(JsonElement element, TypeMetadata<JsonElement> typeMetadata) {
    JsonPrimitive jsonPrimitive = (JsonPrimitive) element;
    if (!jsonPrimitive.isNumber()) {
        throw new DatastoreException("Invalid value for short type.");
    }/*from  w  w w  . j av a2 s .c  o m*/
    return jsonPrimitive.getAsShort();
}

From source file:at.orz.arangodb.util.JsonUtils.java

License:Apache License

public static double toDouble(JsonElement elem) {
    if (elem != null && !elem.isJsonNull()) {
        JsonPrimitive primitive = elem.getAsJsonPrimitive();
        if (primitive.isNumber()) {
            return primitive.getAsDouble();
        } else if (primitive.isString()) {
            if ("INF".equals(primitive.getAsString())) {
                return Double.POSITIVE_INFINITY;
            } else if ("NaN".equals(primitive.getAsString())) {
                return Double.NaN;
            }/*from w  ww  .  j  a  va 2 s  .  c om*/
        }
    }
    return Double.NaN;
}

From source file:bind.JsonTreeReader.java

License:Apache License

@Override
public JsonToken peek() throws IOException {
    if (stack.isEmpty()) {
        return JsonToken.END_DOCUMENT;
    }/*from  w  w w.j a v  a 2  s  . c o m*/

    Object o = peekStack();
    if (o instanceof Iterator) {
        Object secondToTop = stack.get(stack.size() - 2);
        boolean isObject = secondToTop instanceof JsonElement && ((JsonElement) secondToTop).isJsonObject();
        Iterator<?> iterator = (Iterator<?>) o;
        if (iterator.hasNext()) {
            if (isObject) {
                return JsonToken.NAME;
            } else {
                stack.add(iterator.next());
                return peek();
            }
        } else {
            return isObject ? JsonToken.END_OBJECT : JsonToken.END_ARRAY;
        }
    } else if (o instanceof JsonElement) {
        JsonElement el = (JsonElement) o;
        if (el.isJsonObject()) {
            return JsonToken.BEGIN_OBJECT;
        } else if (el.isJsonArray()) {
            return JsonToken.BEGIN_ARRAY;
        } else if (el.isJsonPrimitive()) {
            JsonPrimitive primitive = (JsonPrimitive) o;
            if (primitive.isString()) {
                return JsonToken.STRING;
            } else if (primitive.isBoolean()) {
                return JsonToken.BOOLEAN;
            } else if (primitive.isNumber()) {
                return JsonToken.NUMBER;
            } else {
                throw new AssertionError();
            }
        } else if (el.isJsonNull()) {
            return JsonToken.NULL;
        }
        throw new AssertionError();
    } else if (o == SENTINEL_CLOSED) {
        throw new IllegalStateException("JsonReader is closed");
    } else {
        throw new AssertionError();
    }
}

From source file:ccm.pay2spawn.util.JsonNBTHelper.java

License:Open Source License

public static JsonPrimitive fixNulls(JsonPrimitive primitive) {
    if (primitive.isBoolean())
        return new JsonPrimitive(primitive.getAsBoolean());
    if (primitive.isNumber())
        return new JsonPrimitive(primitive.getAsNumber());
    if (primitive.isString())
        return new JsonPrimitive(primitive.getAsString());
    return JSON_PARSER.parse(primitive.toString()).getAsJsonPrimitive();
}