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.emfjson.gwt.map.ValueSerializer.java

License:Open Source License

public void setOrAdd(EObject owner, EAttribute attribute, JSONValue value) {
    String asString = null;//from www.j a  v a2  s .co  m
    JSONString stringValue = value.isString();
    if (stringValue != null) {
        asString = stringValue.stringValue();
    }

    JSONBoolean booleanValue = value.isBoolean();
    if (booleanValue != null) {
        asString = booleanValue.toString();
    }

    JSONNumber numberValue = value.isNumber();
    if (numberValue != null) {
        asString = numberValue.toString();
    }

    if (asString != null) {
        Object converted = EcoreUtil.createFromString(attribute.getEAttributeType(), asString);
        EObjects.setOrAdd(owner, attribute, converted);
    }
}

From source file:org.freemedsoftware.gwt.client.JsonUtil.java

License:Open Source License

@SuppressWarnings({ "unchecked", "rawtypes" })
public static synchronized Object shoehornJson(JSONValue r, String t) {
    if (r == null || r.toString().equals("null"))
        return null;
    if (t.equals("HashMap<String,HashMap<String,String>[]>")) {
        HashMap<String, HashMap<String, String>[]> oResult = new HashMap<String, HashMap<String, String>[]>();
        JSONObject oA = r.isObject();//from w ww . j  a v a2 s  .  c  o  m
        if (oA != null) {
            Iterator<String> outerIter = oA.keySet().iterator();
            while (outerIter.hasNext()) {
                String innerKey = outerIter.next();
                List<HashMap<?, ?>> result = new ArrayList<HashMap<?, ?>>();
                JSONArray a = oA.get(innerKey).isArray();
                for (int oIter = 0; oIter < a.size(); oIter++) {
                    HashMap<String, String> item = new HashMap<String, String>();
                    JSONObject obj = a.get(oIter).isObject();
                    Iterator<String> iter = obj.keySet().iterator();
                    while (iter.hasNext()) {
                        String k = iter.next();
                        if (obj.get(k).isString() != null) {
                            item.put(k, obj.get(k).isString().stringValue());
                        }
                    }
                    result.add(oIter, item);
                }
                oResult.put(innerKey, (HashMap<String, String>[]) result.toArray(new HashMap<?, ?>[0]));
            }
        }
        return (HashMap<String, HashMap<String, String>[]>) oResult;
    }
    if (t.equals("HashMap<String,String>[]")) {
        List<HashMap<?, ?>> result = new ArrayList<HashMap<?, ?>>();
        JSONArray a = r.isArray();
        for (int oIter = 0; oIter < a.size(); oIter++) {
            HashMap<String, String> item = new HashMap<String, String>();
            JSONObject obj = a.get(oIter).isObject();
            Iterator<String> iter = obj.keySet().iterator();
            while (iter.hasNext()) {
                String k = iter.next();
                if (obj.get(k).isString() != null) {
                    item.put(k, obj.get(k).isString().stringValue());
                }
            }
            result.add(oIter, item);
        }
        return (HashMap<String, String>[]) result.toArray(new HashMap<?, ?>[0]);
    }
    if (t.equals("HashMap<String,Object>[]")) {
        List<HashMap<?, ?>> result = new ArrayList<HashMap<?, ?>>();
        JSONArray a = r.isArray();
        for (int oIter = 0; oIter < a.size(); oIter++) {
            HashMap<String, Object> item = new HashMap<String, Object>();
            JSONObject obj = a.get(oIter).isObject();
            Iterator<String> iter = obj.keySet().iterator();
            while (iter.hasNext()) {
                String k = iter.next();
                if (obj.get(k).isString() != null) {
                    item.put(k, obj.get(k));
                }
            }
            result.add(oIter, item);
        }
        return (HashMap<String, String>[]) result.toArray(new HashMap<?, ?>[0]);
    }
    if (t.equals("HashMap<String,String>[][]")) {
        List<HashMap<?, ?>[]> result = new ArrayList<HashMap<?, ?>[]>();
        JSONArray oArray = r.isArray();
        for (int wayOuterIter = 0; wayOuterIter < oArray.size(); wayOuterIter++) {
            List<HashMap<?, ?>> innerResult = new ArrayList<HashMap<?, ?>>();
            JSONArray a = r.isArray();
            for (int oIter = 0; oIter < a.size(); oIter++) {
                HashMap<String, String> item = new HashMap<String, String>();
                JSONObject obj = a.get(oIter).isObject();
                Iterator<String> iter = obj.keySet().iterator();
                while (iter.hasNext()) {
                    String k = iter.next();
                    if (obj.get(k).isString() != null) {
                        item.put(k, obj.get(k).isString().stringValue());
                    }
                }
                innerResult.add(oIter, item);
            }
            result.add(wayOuterIter, innerResult.toArray(new HashMap<?, ?>[0]));
        }
        return (HashMap<String, String>[][]) result.toArray(new HashMap<?, ?>[0][0]);
    }
    if (t.equals("HashMap<String,String>")) {
        JSONObject obj = r.isObject();
        HashMap<String, String> result = new HashMap<String, String>();
        Iterator<String> iter = obj.keySet().iterator();
        while (iter.hasNext()) {
            String k = iter.next();
            if (obj.get(k).isString() != null) {
                result.put(k, obj.get(k).isString().stringValue());
            }
        }
        return (HashMap<String, String>) result;
    }
    if (t.equals("HashMap<String,Object>")) {
        JSONObject obj = r.isObject();
        HashMap<String, Object> result = new HashMap<String, Object>();
        Iterator<String> iter = obj.keySet().iterator();
        while (iter.hasNext()) {
            String k = iter.next();
            if (obj.get(k) != null) {
                result.put(k, obj.get(k));
            }
        }
        return (HashMap<String, Object>) result;
    }
    if (t.equals("HashMap<String,HashMap<String,String>>")) {
        HashMap<String, HashMap<String, String>> oResult = new HashMap<String, HashMap<String, String>>();
        JSONObject oA = r.isObject();
        if (oA != null) {
            Iterator<String> outerIter = oA.keySet().iterator();
            while (outerIter.hasNext()) {
                String innerKey = outerIter.next();
                HashMap<String, String> item = new HashMap<String, String>();
                JSONObject obj = oA.get(innerKey).isObject();
                Iterator<String> iter = obj.keySet().iterator();
                while (iter.hasNext()) {
                    String k = iter.next();
                    if (obj.get(k).isString() != null) {
                        item.put(k, obj.get(k).isString().stringValue());
                    }
                }
                oResult.put(innerKey, (HashMap<String, String>) item);
            }
        }
        return (HashMap<String, HashMap<String, String>>) oResult;
    }
    if (t.equals("HashMap<String,HashMap<String,Integer>>")) {
        HashMap<String, HashMap<String, Integer>> oResult = new HashMap<String, HashMap<String, Integer>>();
        JSONObject oA = r.isObject();
        if (oA != null) {
            Iterator<String> outerIter = oA.keySet().iterator();
            while (outerIter.hasNext()) {
                String innerKey = outerIter.next();
                HashMap<String, Integer> item = new HashMap<String, Integer>();
                JSONObject obj = oA.get(innerKey).isObject();
                Iterator<String> iter = obj.keySet().iterator();
                while (iter.hasNext()) {
                    String k = iter.next();
                    if (obj.get(k).isNumber() != null) {
                        item.put(k, (int) obj.get(k).isNumber().doubleValue());
                    }
                }
                oResult.put(innerKey, (HashMap<String, Integer>) item);
            }
        }
        return (HashMap<String, HashMap<String, Integer>>) oResult;
    }
    if (t.equals("HashMap<Integer,String>")) {
        JSONObject obj = r.isObject();
        HashMap<Integer, String> result = new HashMap<Integer, String>();
        Iterator<String> iter = obj.keySet().iterator();
        while (iter.hasNext()) {
            String k = iter.next();
            if (obj.get(k).isString() != null) {
                result.put(Integer.valueOf(k), obj.get(k).isString().stringValue());
            }
        }
        return (HashMap<Integer, String>) result;
    }
    if (t.equals("HashMap<String,Integer>")) {
        JSONObject obj = r.isObject();
        HashMap<String, Integer> result = new HashMap<String, Integer>();
        Iterator<String> iter = obj.keySet().iterator();
        while (iter.hasNext()) {
            String k = iter.next();
            if (obj.get(k).isNumber() != null) {
                result.put(k, (int) obj.get(k).isNumber().doubleValue());
            }
        }
        return (HashMap<String, Integer>) result;
    }
    if (t.equals("String[][]")) {
        JSONArray outer = r.isArray();
        List<String[]> x = new ArrayList<String[]>();
        if (r.isArray() != null) {
            for (int oIter = 0; oIter < outer.size(); oIter++) {
                if (outer.get(oIter).isArray() != null) {
                    JSONArray inner = outer.get(oIter).isArray();
                    List<String> xI = new ArrayList<String>();
                    if (inner.isArray() != null) {
                        for (int iIter = 0; iIter < inner.size(); iIter++) {
                            if (inner.get(iIter).isString() != null) {
                                xI.add(iIter, inner.get(iIter).isString().stringValue());
                            } else if (inner.get(iIter).isNumber() != null) {
                                xI.add(iIter, inner.get(iIter).isNumber().toString());
                            }
                        }
                    }
                    x.add((String[]) xI.toArray(new String[0]));
                }
            }
            return (String[][]) x.toArray(new String[0][0]);
        }
    }
    if (t.equals("String[]")) {
        JSONArray a = r.isArray();
        List<String> x = new ArrayList<String>();
        if (r.isArray() != null) {
            for (int iter = 0; iter < a.size(); iter++) {
                if (a.get(iter).isString() != null) {
                    x.add(iter, a.get(iter).isString().stringValue());
                }
            }
        }
        return (String[]) x.toArray(new String[0]);
    }
    if (t.compareToIgnoreCase("HashMap<String,String[]>") == 0) {
        HashMap<String, String[]> oResult = new HashMap<String, String[]>();
        JSONObject oA = r.isObject();
        if (oA != null) {
            Iterator<String> outerIter = oA.keySet().iterator();
            while (outerIter.hasNext()) {
                String innerKey = outerIter.next();
                JSONArray a = oA.get(innerKey).isArray();
                String[] x = new String[a.size()];
                if (a.isArray() != null) {
                    for (int iter = 0; iter < a.size(); iter++) {
                        if (a.get(iter).isString() != null) {
                            // x.add(iter,
                            // a.get(iter).isString().stringValue());
                            x[iter] = a.get(iter).isString().stringValue();
                        }
                    }
                }
                oResult.put(innerKey, x);
            }
        }
        return (HashMap<String, String[]>) oResult;
    }

    if (t.compareToIgnoreCase("HashMap<String,List>") == 0) {
        HashMap<String, List> oResult = new HashMap<String, List>();
        JSONObject oA = r.isObject();
        if (oA != null) {
            Iterator<String> outerIter = oA.keySet().iterator();
            while (outerIter.hasNext()) {
                String innerKey = outerIter.next();
                JSONArray a = oA.get(innerKey).isArray();
                List x = new ArrayList();
                if (a.isArray() != null) {
                    for (int iter = 0; iter < a.size(); iter++) {
                        if (a.get(iter).isString() != null) {
                            // x.add(iter,
                            // a.get(iter).isString().stringValue());
                            x.add(a.get(iter).isString().stringValue());
                        }
                    }
                }
                oResult.put(innerKey, x);
            }
        }
        return (HashMap<String, List>) oResult;
    }
    if (t.compareToIgnoreCase("String") == 0) {
        if (r.isString() != null) {
            return (String) r.isString().stringValue();
        }
    }
    if (t.compareToIgnoreCase("Integer") == 0) {
        if (r.isNumber() != null) {
            return (Integer) new Integer((int) r.isNumber().doubleValue());
        }
    }
    if (t.compareToIgnoreCase("Float") == 0) {
        if (r.isNumber() != null) {
            return (Float) new Float((float) r.isNumber().doubleValue());
        }
    }
    if (t.compareToIgnoreCase("Boolean") == 0) {
        if (r.isBoolean() != null) {
            return (Boolean) r.isBoolean().booleanValue();
        }
    }

    // If anything else bombs out...
    GWT.log("Could not parse type " + t, null);
    return null;
}

From source file:org.fusesource.restygwt.client.AbstractJsonEncoderDecoder.java

License:Apache License

static public BigDecimal toBigDecimal(JSONValue value) {
    JSONNumber number = value.isNumber();
    if (number == null) {
        throw new DecodingException("Expected a json number, but was given: " + value);
    }//from w w w  .j  av a  2s .  co  m
    return new BigDecimal(value.toString());
}

From source file:org.geomajas.gwt2.client.map.feature.JsonFeatureFactory.java

License:Open Source License

private Feature createFeature(JSONObject jsonObject, FeaturesSupported layer) {
    String id = null;/*from www  . ja  va  2s  . com*/
    // id is not mandatory !
    if (jsonObject.containsKey("id")) {
        id = JsonService.getStringValue(jsonObject, "id");
    } else {
        id = Document.get().createUniqueId();
    }
    JSONObject properties = JsonService.getChild(jsonObject, "properties");
    Map<String, Attribute<?>> attributes = new HashMap<String, Attribute<?>>();
    if (layer != null && layer.getAttributeDescriptors().size() > 0) {
        for (AttributeDescriptor descr : layer.getAttributeDescriptors()) {
            AttributeType type = descr.getType();
            String name = descr.getName();
            Attribute attribute;
            if (Boolean.class.equals(type.getBinding())) {
                attribute = new AttributeImpl(JsonService.getBooleanValue(properties, name));
            } else if (Short.class.equals(type.getBinding())) {
                attribute = new AttributeImpl(JsonService.getShortValue(properties, name));
            } else if (Integer.class.equals(type.getBinding())) {
                attribute = new AttributeImpl(JsonService.getIntValue(properties, name));
            } else if (Long.class.equals(type.getBinding())) {
                attribute = new AttributeImpl(JsonService.getLongValue(properties, name));
            } else if (Double.class.equals(type.getBinding())) {
                attribute = new AttributeImpl(JsonService.getDoubleValue(properties, name));
            } else if (Date.class.equals(type.getBinding())) {
                attribute = new AttributeImpl(JsonService.getDateValue(properties, name));
            } else if (Geometry.class.equals(type.getBinding())) {
                attribute = new AttributeImpl(JsonService.getGeometryValue(properties, name));
            } else {
                attribute = new AttributeImpl(JsonService.getStringValue(properties, name));
            }
            attributes.put(name, attribute);
        }
    } else {
        for (String name : properties.keySet()) {
            JSONValue value = properties.get(name);
            Attribute attribute;
            if (value.isBoolean() != null) {
                attribute = new AttributeImpl(JsonService.getBooleanValue(properties, name));
            } else if (value.isNumber() != null) {
                attribute = new AttributeImpl(JsonService.getDoubleValue(properties, name));
            } else if (value.isObject() != null) {
                attribute = new AttributeImpl(JsonService.getGeometryValue(properties, name));
            } else {
                attribute = new AttributeImpl(JsonService.getStringValue(properties, name));
            }
            attributes.put(name, attribute);
        }
    }
    Geometry geometry = JsonService.getGeometryValue(jsonObject, "geometry");
    return new FeatureImpl(layer, id, attributes, geometry, id);
}

From source file:org.geomajas.gwt2.client.service.JsonService.java

License:Open Source License

/**
 * Get a integer value from a {@link JSONObject}.
 * //from w ww. j a v a 2  s  .c o m
 * @param jsonObject The object to get the key value from.
 * @param key The name of the key to search the value for.
 * @return Returns the value for the key in the object .
 * @throws JSONException Thrown in case the key could not be found in the JSON object.
 */
public static Integer getIntValue(JSONObject jsonObject, String key) throws JSONException {
    checkArguments(jsonObject, key);
    JSONValue value = jsonObject.get(key);
    if (value != null && value.isNumber() != null) {
        double number = ((JSONNumber) value).doubleValue();
        return new Integer((int) number);
    }
    return null;
}

From source file:org.geomajas.gwt2.client.service.JsonService.java

License:Open Source License

/**
 * Get a double value from a {@link JSONObject}.
 * /*from w w w .  j a va2  s .c  o m*/
 * @param jsonObject The object to get the key value from.
 * @param key The name of the key to search the value for.
 * @return Returns the value for the key in the object .
 * @throws JSONException Thrown in case the key could not be found in the JSON object.
 */
public static Double getDoubleValue(JSONObject jsonObject, String key) throws JSONException {
    checkArguments(jsonObject, key);
    JSONValue value = jsonObject.get(key);
    if (value != null && value.isNumber() != null) {
        double number = ((JSONNumber) value).doubleValue();
        return number;
    }
    return null;
}

From source file:org.geomajas.gwt2.client.service.JsonService.java

License:Open Source License

/**
 * Get a long value from a {@link JSONObject}.
 * /*w w  w.j a v  a 2  s . c  o m*/
 * @param jsonObject The object to get the key value from.
 * @param key The name of the key to search the value for.
 * @return Returns the value for the key in the object .
 * @throws JSONException Thrown in case the key could not be found in the JSON object.
 */
public static Long getLongValue(JSONObject jsonObject, String key) throws JSONException {
    checkArguments(jsonObject, key);
    JSONValue value = jsonObject.get(key);
    if (value != null && value.isNumber() != null) {
        double number = ((JSONNumber) value).doubleValue();
        return new Long((long) number);
    }
    return null;
}

From source file:org.geomajas.gwt2.client.service.JsonService.java

License:Open Source License

/**
 * Get a short value from a {@link JSONObject}.
 * /*from w  ww .j  av a 2 s  .  c o  m*/
 * @param jsonObject The object to get the key value from.
 * @param key The name of the key to search the value for.
 * @return Returns the value for the key in the object .
 * @throws JSONException Thrown in case the key could not be found in the JSON object.
 */
public static Short getShortValue(JSONObject jsonObject, String key) throws JSONException {
    checkArguments(jsonObject, key);
    JSONValue value = jsonObject.get(key);
    if (value != null && value.isNumber() != null) {
        double number = ((JSONNumber) value).doubleValue();
        return new Short((short) number);
    }
    return null;
}

From source file:org.gwm.splice.client.service.json.JSONSerializer.java

License:Apache License

private Object convertValue(JSONValue val) {
    if (val.isBoolean() != null) {
        return new Boolean(val.isBoolean().booleanValue());
    } else if (val.isNumber() != null) {
        return new Double(val.isNumber().getValue());
    } else if (val.isString() != null) {
        return val.isString().stringValue();
    } else if (val.isObject() != null) {
        Attributes attrs = new Attributes();
        convertAttributes(val.isObject(), attrs);
        return attrs;
    } else if (val.isArray() != null) {
        JSONArray ja = val.isArray();
        Object[] array = new Object[ja.size()];
        for (int i = 0; i < ja.size(); i++) {
            array[i] = convertValue(ja.get(i));
        }//from  w w w .ja v  a2s.c o m
        return array;
    }
    return null;
}

From source file:org.gwt.json.serialization.client.primitives.JsonDoubleSerializer.java

License:Apache License

@Override
public Double deSerialize(JSONValue jsonObj, boolean forcePrimitiveValue) throws JSONException {
    Double defaultValue = forcePrimitiveValue ? .0 : null;
    return jsonObj == null ? defaultValue
            : (jsonObj.isNumber() == null ? defaultValue : jsonObj.isNumber().doubleValue());
}