Example usage for com.google.gwt.json.client JSONValue isNumber

List of usage examples for com.google.gwt.json.client JSONValue isNumber

Introduction

In this page you can find the example usage for com.google.gwt.json.client JSONValue isNumber.

Prototype

public JSONNumber isNumber() 

Source Link

Document

Returns a non-null reference if this JSONValue is really a JSONNumber.

Usage

From source file:org.spiffyui.client.JSONUtil.java

License:Apache License

/**
 * Get a java.util.Date from the JSON object as an epoch time in milliseconds
 * or return null if it doesn't exist or
 * cannot be converted from epoch to Date
 * //from   www  . j ava 2s.  com
 * @param obj    the object with the value
 * @param key    the key for the object
 * 
 * @return the value or null it could not be decoded/converted
 */
public static Date getDateValue(JSONObject obj, String key) {
    JSONValue v = obj.get(key);
    if (v != null) {
        JSONNumber n = v.isNumber();
        if (n != null) {
            return new Date(Double.valueOf(n.doubleValue()).longValue());
        } else {
            String s = getStringValue(obj, key);
            if (s != null) {
                return new Date(Long.parseLong(s));
            }
        }
    }

    return null;
}

From source file:org.talend.mdm.webapp.browserecords.client.rest.ExplainRestServiceHandler.java

License:Open Source License

private String getStringValue(JSONValue jsonValue) {
    String value = null;/*from   w  ww .j a v a 2s. com*/
    if (jsonValue != null) {
        if (jsonValue.isString() != null) {
            value = jsonValue.isString().toString().replaceAll("\"", ""); //$NON-NLS-1$ //$NON-NLS-2$
        } else if (jsonValue.isBoolean() != null) {
            value = jsonValue.isBoolean().toString();
        } else if (jsonValue.isNumber() != null) {
            value = jsonValue.isNumber().toString();
        }
    }
    return value;
}

From source file:org.talend.mdm.webapp.browserecords.client.rest.ExplainRestServiceHandler.java

License:Open Source License

private String getScoreValue(JSONValue jsonValue) {
    String value = null;//from   w  ww. j  a  v  a 2  s.  c  o  m
    if (jsonValue != null && jsonValue.isNumber() != null) {
        NumberFormat numberFormat = NumberFormat.getFormat("0.00"); //$NON-NLS-1$
        value = numberFormat.format(jsonValue.isNumber().doubleValue() * 100) + "%"; //$NON-NLS-1$
    }
    return value;
}

From source file:org.thechiselgroup.biomixer.client.json.JsJsonParser.java

License:Apache License

@Override
public String asString(Object jsonValue) {
    if (null == jsonValue) {
        return null;
    }//from ww  w  . j  ava  2 s.c o  m
    JSONValue node = (JSONValue) jsonValue;

    if (node.isString() != null) {
        return node.isString().stringValue();
    }

    if (node.isNumber() != null) {
        return node.isNumber().toString();
    }

    return null;
}

From source file:org.utgenome.gwt.utgb.client.db.datatype.IntegerType.java

License:Apache License

public String toString(JSONValue value) {
    JSONNumber n = value.isNumber();
    if (n != null)
        return Integer.toString((int) n.doubleValue());
    else {//  ww w .  j a  v a  2s .co m
        return super.toString(value);
    }

}

From source file:org.waveprotocol.wave.client.gadget.renderer.GadgetMetadata.java

License:Apache License

/**
 * Helper function to extract a long value from given JSON object.
 *
 * @param json JSON object to extract the value from.
 * @param key key of the value to extract.
 * @return the Long object extracted from JSON (can be null if the value does
 *         not exist or is invalid./*from   w  w w .j av  a  2  s.  c om*/
 */
private static Long getJsonLongValue(JSONObject json, String key) {
    JSONValue value = json.get(key);
    JSONNumber number = (value == null) ? null : value.isNumber();
    if (number != null) {
        return Math.round(number.doubleValue());
    } else {
        return null;
    }
}

From source file:rocket.json.client.JsonSerializer.java

License:Apache License

/**
 * Null safe method to retrieve the double value from a jsonValue which may
 * or may not be a JSONNumber. If its null or not a JSONNumber 0 is
 * returned.//from   w  w  w.  j ava2 s.  c  o  m
 * 
 * @param jsonValue
 * @return
 */
double readDouble(final JSONValue jsonValue) {
    double value = 0;

    while (true) {
        if (null == jsonValue) {
            break;
        }
        final JSONNumber number = jsonValue.isNumber();
        if (null == number) {
            break;
        }

        value = number.getValue();
        break;
    }

    return value;
}

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

License:Open Source License

private Object fromJSONValue(JSONValue jsonValue, Type expected) throws NoSuitableSerializableFactory {

    if (jsonValue == null) {
        return null;
    }// w  ww  .  j a v  a2  s . co  m

    // Null
    JSONNull asNull = jsonValue.isNull();
    if (asNull != null) {
        return null;
    }

    // Boolean
    JSONBoolean asBoolean = jsonValue.isBoolean();
    if (asBoolean != null) {
        return asBoolean.booleanValue();
    }

    // Integer
    // Long
    // Float
    // Double
    JSONNumber asNumber = jsonValue.isNumber();
    if (asNumber != null) {
        double value = asNumber.doubleValue();

        if (expected.isInteger()) {
            return (int) value;
        }

        if (expected.isLong()) {
            return (long) value;
        }

        if (expected.isFloat()) {
            return (float) value;
        }

        if (expected.isDouble()) {
            return value;
        }
    }

    // String
    // Enum
    JSONString asString = jsonValue.isString();
    if (asString != null) {
        if (expected.isEnum()) {
            String value = asString.stringValue();
            return Enum.valueOf((Class) expected.getTypeClass(), value);
        } else {
            return asString.stringValue();
        }
    }

    // Map
    // Serializable
    JSONObject asObject = jsonValue.isObject();
    if (asObject != null) {
        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 (String key : asObject.keySet()) {
                JSONValue value = asObject.get(key);

                if (keyType.isString()) {
                    map.put(key, fromJSONValue(value, valueType));
                }

                if (keyType.isEnum()) {
                    map.put(Enum.valueOf((Class) keyType.getTypeClass(), key), fromJSONValue(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();

                JSONValue value = asObject.get(field);
                object.set(field, fromJSONValue(value, fieldType));
            }

            return object;
        }
    }

    // List
    JSONArray asArray = jsonValue.isArray();
    if (asArray != null) {
        int size = asArray.size();

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

        for (int i = 0; i < size; i++) {
            JSONValue value = asArray.get(i);
            list.add(fromJSONValue(value, itemType));
        }

        return list;
    }

    return null;
}

From source file:stroom.util.client.JSONUtil.java

License:Apache License

public static Integer getInteger(final JSONValue v) {
    if (v != null) {
        final JSONNumber jsonNumber = v.isNumber();
        if (jsonNumber != null) {
            return Integer.valueOf((int) jsonNumber.doubleValue());
        }//from w  ww  .j  a  va2  s . com
    }
    return null;
}

From source file:stroom.util.client.JSONUtil.java

License:Apache License

public static Double getDouble(final JSONValue v) {
    if (v != null) {
        final JSONNumber jsonNumber = v.isNumber();
        if (jsonNumber != null) {
            return Double.valueOf(jsonNumber.doubleValue());
        }//from   w  ww  . j a v a  2  s  .com
    }
    return null;
}