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.sonar.api.web.gwt.client.webservices.JsonUtils.java

License:Open Source License

public static String getString(JSONObject json, String field) {
    JSONValue jsonValue;
    JSONString jsonString;//  w w w  .j  a  v a  2s .  c  om
    if ((jsonValue = json.get(field)) == null) {
        return null;
    }
    if ((jsonString = jsonValue.isString()) == null) {
        JSONNumber jsonNumber = jsonValue.isNumber();
        return jsonNumber != null ? jsonNumber.toString() : null;
    }
    return jsonString.stringValue();
}

From source file:org.sonar.api.web.gwt.client.webservices.JsonUtils.java

License:Open Source License

public static Double getDouble(JSONObject json, String field) {
    JSONValue jsonValue;
    JSONNumber jsonNumber;/*from  ww  w  .  j  a  v a2 s . c o m*/
    if ((jsonValue = json.get(field)) == null) {
        return null;
    }
    if ((jsonNumber = jsonValue.isNumber()) == null) {
        return null;
    }
    return jsonNumber.doubleValue();
}

From source file:org.sonar.api.web.gwt.client.webservices.ViolationsQuery.java

License:Open Source License

private Violations parseJSON(JavaScriptObject obj) {
    Violations result = new Violations();
    JSONArray jsonArray = new JSONArray(obj);
    for (int i = 0; i < jsonArray.size(); i++) {
        JSONObject jsViolation = jsonArray.get(i).isObject();
        if (jsViolation == null) {
            continue;
        }/*from   ww w  .  jav  a  2 s .  c  o  m*/
        JSONString message = jsViolation.get("message").isString();
        JSONString priority = jsViolation.get("priority").isString();
        JSONValue lineJson = jsViolation.get("line");
        int lineIndex = 0;
        if (lineJson != null) {
            lineIndex = (int) lineJson.isNumber().doubleValue();
        }

        JSONObject ruleObj = jsViolation.get("rule").isObject();
        Rule rule = new Rule(JsonUtils.getString(ruleObj, "key"), JsonUtils.getString(ruleObj, "name"));

        result.add(new Violation(message.stringValue(), priority.stringValue(), lineIndex, rule, null));
    }
    return result;
}

From source file:org.sonar.gwt.JsonUtils.java

License:Open Source License

public static String getAsString(JSONValue jsonValue) {
    if (jsonValue == null) {
        return null;
    }/*  w w  w  .ja  v a  2 s .c om*/
    JSONString jsonString;
    if ((jsonString = jsonValue.isString()) == null) {
        JSONNumber jsonNumber = jsonValue.isNumber();
        return jsonNumber != null ? jsonNumber.toString() : null;
    }
    return jsonString.stringValue();
}

From source file:org.sonar.gwt.JsonUtils.java

License:Open Source License

public static Double getAsDouble(JSONValue jsonValue) {
    if (jsonValue == null) {
        return null;
    }//from  ww w  .jav  a 2 s.com
    JSONNumber jsonNumber;
    if ((jsonNumber = jsonValue.isNumber()) == null) {
        JSONString jsonString = jsonValue.isString();
        return jsonString != null ? Double.parseDouble(jsonString.toString()) : null;
    }
    return jsonNumber.doubleValue();
}

From source file:org.sonatype.nexus.gwt.ui.client.data.JSONResourceParser.java

License:Open Source License

private Object toObject(JSONValue json) {
    Object obj = null;/*from  w  w w .ja  v a2 s .  com*/

    if (json instanceof JSONBoolean) {
        obj = Boolean.valueOf(json.isBoolean().booleanValue());
    } else if (json instanceof JSONString) {
        obj = json.isString().stringValue();
    } else if (json instanceof JSONNumber) {
        obj = new Double(json.isNumber().doubleValue());
    }

    return obj;
}

From source file:org.sonatype.nexus.gwt.ui.client.table.JSONArrayTableModel.java

License:Open Source License

public Object getCell(int rowIndex, int colIndex) {
    JSONObject obj = (JSONObject) getRow(rowIndex);

    JSONValue val = JSONUtil.getValue(obj, props[colIndex]);

    if (val != null) {
        if (val.isString() != null) {
            return val.isString().stringValue();
        } else if (val.isBoolean() != null) {
            return Boolean.valueOf(val.isBoolean().booleanValue());
        } else if (val.isNumber() != null) {
            return new Double(val.isNumber().doubleValue());
        } else if (val.isNull() != null) {
            return null;
        } else {//from w  w w.j  a  v a2  s.  c om
            return val;
        }
    }

    return null;
}

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

License:Apache License

/**
 * Get an int from the JSON object or the defaultValue if it doesn't exist or
 * isn't an int.  This will handle JSON numbers like this:
 * /*from   w  w w.  ja  va  2s .  c om*/
 *      "val": 5
 * 
 * It will also handle numbers in strings like:
 * 
 *      "val": "5"
 * 
 * @param obj    the object with the value
 * @param key    the key for the object 
 * @param defaultValue the default value if the specified key isn't found. 
 * 
 * @return the value or the defaultValue if it could not be decoded
 */
public static int getIntValue(JSONObject obj, String key, int defaultValue) {
    if (!obj.containsKey(key)) {
        return defaultValue;
    }

    try {
        JSONValue v = obj.get(key);
        if (v != null) {
            JSONNumber n = v.isNumber();
            if (n != null) {
                return (int) n.doubleValue();
            } else {
                /*
                 * If this isn't a number, then it might be a string
                 * like "5" so we try to parse it as a number.
                 */
                return Integer.parseInt(getStringValue(obj, key));
            }
        }
    } catch (Exception e) {
        JSUtil.println(e.getMessage());
    }

    return defaultValue;
}

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

License:Apache License

/**
 * Get a double from the JSON object or the defaultValue if it doesn't exist or
 * isn't a double.  This will handle JSON numbers like this:
 *
 *      "val": 0.5 or "val": -0.5 or "val": +0.5
 *
 * It will also handle numbers in strings like:
 *
 *      "val": "0.5" or "val": "-0.5" or "val": "+0.5"
 *
 * @param obj    the object with the value
 * @param key    the key for the object/*from w  ww  . j ava 2 s . c o  m*/
 * @param defaultValue the default value if the specified key isn't found.
 *
 * @return the value or the defaultValue if it could not be decoded
 */
public static double getDoubleValue(JSONObject obj, String key, double defaultValue) {
    if (!obj.containsKey(key)) {
        return defaultValue;
    }

    try {
        JSONValue v = obj.get(key);
        if (v != null) {
            JSONNumber n = v.isNumber();
            if (n != null) {
                return n.doubleValue();
            } else {
                /*
                 * If this isn't a number, then it might be a string
                 * like "5" so we try to parse it as a number.
                 */
                return Double.parseDouble(getStringValue(obj, key));
            }
        }
    } catch (Exception e) {
        JSUtil.println(e.getMessage());
    }

    return defaultValue;
}

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

License:Apache License

/**
 * Get a long from the JSON object or the defaultValue if it doesn't exist or
 * isn't a long.  This will handle JSON numbers like this:
 * //from  w w  w.  ja  v  a  2 s  . com
 *      "val": 5
 * 
 * It will also handle numbers in strings like:
 * 
 *      "val": "5"
 * 
 * @param obj    the object with the value
 * @param key    the key for the object 
 * @param defaultValue the default value if the specified key isn't found. 
 * 
 * @return the value or the defaultValue if it could not be decoded
 */
public static long getLongValue(JSONObject obj, String key, long defaultValue) {
    if (!obj.containsKey(key)) {
        return defaultValue;
    }

    try {
        JSONValue v = obj.get(key);
        if (v != null) {
            JSONNumber n = v.isNumber();
            if (n != null) {
                return (long) n.doubleValue();
            } else {
                /*
                 * If this isn't a number, then it might be a string
                 * like "5" so we try to parse it as a number.
                 */
                return Long.parseLong(getStringValue(obj, key));
            }
        }
    } catch (Exception e) {
        JSUtil.println(e.getMessage());
    }

    return defaultValue;
}