Example usage for com.google.gwt.json.client JSONNumber doubleValue

List of usage examples for com.google.gwt.json.client JSONNumber doubleValue

Introduction

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

Prototype

public double doubleValue() 

Source Link

Document

Gets the double value this JSONNumber represents.

Usage

From source file:edu.ucsb.eucalyptus.admin.client.extensions.store.JSONUtil.java

License:Open Source License

static int asInt(JSONValue value, int defaultResult) {
    if (value != null) {
        JSONNumber number = value.isNumber();
        if (number != null) {
            return (int) number.doubleValue();
        }/*  w w  w. j a v  a 2 s . c o  m*/
    }
    return defaultResult;
}

From source file:es.deusto.weblab.client.comm.CommonSerializerJSON.java

License:Open Source License

protected double json2double(JSONValue value) throws SerializationException {
    if (value == null)
        throw new SerializationException("Double expected, found null");
    final JSONNumber jsonnumber = value.isNumber();
    if (jsonnumber == null)
        throw new SerializationException("Double expected, found: " + value);
    return jsonnumber.doubleValue();
}

From source file:es.deusto.weblab.client.comm.CommonSerializerJSON.java

License:Open Source License

protected int json2int(JSONValue value) throws SerializationException {
    if (value == null)
        throw new SerializationException("Int expected, found null");
    final JSONNumber jsonnumber = value.isNumber();
    if (jsonnumber == null)
        throw new SerializationException("Int expected, found: " + value);
    return (int) jsonnumber.doubleValue();
}

From source file:es.deusto.weblab.client.configuration.ConfigurationRetriever.java

License:Open Source License

/**
 * Retrieves an integer property. Will throw if the property is not found
 * after searching the retriever and its parent.
 * @param key String which uniquely identifies the property
 *//*from  w ww .  ja  v a 2  s. c o m*/
@Override
public int getIntProperty(String key)
        throws ConfigurationKeyNotFoundException, InvalidConfigurationValueException {
    final JSONValue value = this.getJSONProperty(key);
    if (value == null)
        throw new ConfigurationKeyNotFoundException("Configuration key: " + key + " not found");

    final JSONNumber numberValue = value.isNumber();
    if (numberValue == null)
        throw new InvalidConfigurationValueException("Invalid number format for key " + key);
    return (int) numberValue.doubleValue();
}

From source file:es.deusto.weblab.client.configuration.ConfigurationRetriever.java

License:Open Source License

/**
 * Retrieves an integer property.//  www .j  av a 2  s  .  co m
 * @param key String which uniquely identifies the property
 */
@Override
public int getIntProperty(String key, int def) {
    final JSONValue value = this.getJSONProperty(key);
    if (value == null)
        return def;

    final JSONNumber numberValue = value.isNumber();
    if (numberValue == null)
        return def;

    return (int) numberValue.doubleValue();
}

From source file:fast.servicescreen.client.gui.RuleUtil.java

License:Open Source License

/**
 * Recursive method for retrieving all jsonvalues for a specified tagName
 * *///from www . j  a  v a  2  s .c o m
public static void jsonValuesByTagName(JSONArray elements, JSONValue root, String tagName) {
    //if object search on first layer. on failure begin depth-search
    JSONObject object = root.isObject();
    if (object != null) {
        //get (first layer) keys contained in the JSONValue
        Set<String> keys = object.keySet();

        //seek on first layer
        for (Iterator<String> iterator = keys.iterator(); iterator.hasNext();) {
            String key = (String) iterator.next();

            //found - add it and stop
            if (key.equals(tagName)) {
                int index = elements.size();
                elements.set(index, object.get(key));
                //stop - key can occur only once on first layer
                break;
            }
            //nothing found - depth-search
            else {
                jsonValuesByTagName(elements, object.get(key), tagName);
            }
        }
    }

    //if it's an array, search among it's children by calling recursive method
    //for every child
    JSONArray jsonArray = root.isArray();
    if (jsonArray != null) {
        for (int i = 0; i < jsonArray.size(); i++) {
            jsonValuesByTagName(elements, jsonArray.get(i), tagName);
        }
    }

    //if it's a matching boolean, number or string: add it
    JSONBoolean jsonBoolean = root.isBoolean();
    if (jsonBoolean != null && tagName.equals(jsonBoolean.booleanValue())) {
        int index = elements.size();
        elements.set(index, jsonBoolean);
    }
    JSONNumber jsonNumber = root.isNumber();
    if (jsonNumber != null && tagName.equals(jsonNumber.doubleValue())) {
        int index = elements.size();
        elements.set(index, jsonNumber);
    }
    JSONString jsonString = root.isString();
    if (jsonString != null && tagName.equals(jsonString.stringValue())) {
        int index = elements.size();
        elements.set(index, jsonString);
    }
}

From source file:fast.servicescreen.client.gui.RuleUtil.java

License:Open Source License

/**
 * Returns the String value of given JSONValue, or "" if
 * value was emtpy/*ww w.j a va 2 s. co m*/
 * */
public static String getJSONValue_AsString(JSONValue jsonValue) {
    if (jsonValue != null) {
        //Define JSON valuetype. Get its sting value 
        String nodeValue = "";
        JSONBoolean attrBoolean = jsonValue.isBoolean();
        JSONNumber attrNumber = jsonValue.isNumber();
        JSONString attrString = jsonValue.isString();

        if (attrBoolean != null) {
            nodeValue = "" + attrBoolean.booleanValue();
        } else if (attrNumber != null) {
            nodeValue = "" + attrNumber.doubleValue();
        } else if (attrString != null) {
            String stringValue = attrString.stringValue();
            if (stringValue != null && !"".equals(stringValue.trim())) {
                nodeValue = "" + stringValue;
            } else {
                nodeValue = "";
            }
        }

        return nodeValue;
    }

    return "";
}

From source file:gov.nist.spectrumbrowser.admin.JSONViewer.java

License:Open Source License

private TreeItem populate(TreeItem root, JSONObject jsonObject) {
    Set<String> keySet = jsonObject.keySet();
    for (String key : keySet) {
        JSONValue jsonValue = jsonObject.get(key);
        JSONString jsonString = jsonValue.isString();
        JSONNumber jsonNumber = jsonValue.isNumber();
        TreeItem treeItem = root.addTextItem(key);
        if (jsonString != null) {
            String stringValue = jsonString.stringValue();
            treeItem.addTextItem(stringValue);
        } else if (jsonNumber != null) {
            String stringValue = Double.toString(jsonNumber.doubleValue());
            treeItem.addTextItem(stringValue);
        } else if (jsonValue.isObject() != null) {
            populate(treeItem, jsonValue.isObject());
        }/*  w  w w. j  a v a2  s . c om*/
    }

    return root;

}

From source file:gov.nist.spectrumbrowser.client.JSONViewer.java

License:Open Source License

private void populate(TreeItem root, JSONObject jsonObject) {
    Set<String> keySet = jsonObject.keySet();
    for (String key : keySet) {
        JSONValue jsonValue = jsonObject.get(key);
        JSONString jsonString = jsonValue.isString();
        JSONNumber jsonNumber = jsonValue.isNumber();
        TreeItem treeItem = root.addTextItem(key);
        if (jsonString != null) {
            String stringValue = jsonString.stringValue();
            treeItem.addTextItem(stringValue);
        } else if (jsonNumber != null) {
            String stringValue = Double.toString(jsonNumber.doubleValue());
            treeItem.addTextItem(stringValue);
        } else if (jsonValue.isObject() != null) {
            populate(treeItem, jsonValue.isObject());
        }//www  .  ja  v  a  2  s.c  o  m
    }

}

From source file:lh.api.showcase.client.HttpErrorUtils.java

License:Apache License

public static void handleHttpErrorException(Throwable err, BasicFormResultDisplay display) {
    if (err == null) {
        return;//from  w w  w  . j a  va  2  s  .  c  om
    }
    if (err instanceof HttpErrorResponseException) {
        HttpErrorResponseException httpError = (HttpErrorResponseException) err;
        StringBuilder sb = new StringBuilder();
        if (httpError.getStatusCode() <= 299) {
            return;
        } else if (httpError.getStatusCode() == 400 || httpError.getStatusCode() == 404
                || httpError.getStatusCode() == 405) {
            sb.append(Messages.Util.INSTANCE.get().badRequest());
        } else if (httpError.getStatusCode() == 403) {
            sb.append(Messages.Util.INSTANCE.get().serverProblem());
        } else {
            String body = httpError.getResponseBody();
            if (body == null || body.isEmpty()) {
                sb.append(err.getMessage());
            } else {
                try {
                    JSONValue jsv = JSONParser.parseStrict(body);
                    JSONObject jobj = jsv.isObject();

                    JSONObject jsProcessingErrorsObj = jobj.get("ProcessingErrors").isObject();
                    JSONObject jsProcessingErrorObj = jsProcessingErrorsObj.get("ProcessingError").isObject();

                    @SuppressWarnings("unused")
                    JSONBoolean jsIsRetryable = jsProcessingErrorObj.get("@RetryIndicator").isBoolean();
                    @SuppressWarnings("unused")
                    JSONString jsType = jsProcessingErrorObj.get("Type").isString();
                    @SuppressWarnings("unused")
                    JSONString jsDescription = jsProcessingErrorObj.get("Description").isString();
                    @SuppressWarnings("unused")
                    JSONString jsInfoUrl = jsProcessingErrorObj.get("InfoURL").isString();
                    JSONNumber jsCode = jsProcessingErrorObj.get("Code").isNumber();

                    int code = (int) jsCode.doubleValue();
                    if (code >= 2000 && code < 3000) {
                        sb.append(Messages.Util.INSTANCE.get().badRequest());
                    }
                    if (code >= 3000 && code < 4000) {
                        sb.append(Messages.Util.INSTANCE.get().resourceNotFound());
                    } else {
                        sb.append(err.getMessage());
                    }
                } catch (IllegalArgumentException | NullPointerException e) {
                    sb.append(err.getMessage());
                }
            }
        }

        if (display != null) {
            display.setJson((httpError.getResponseBody() != null) ? (httpError.getResponseBody()) : (""), null);
            display.setMessage(sb.toString(), MessageType.ERROR);
        }
    }
}