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

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

Introduction

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

Prototype

public JSONBoolean isBoolean() 

Source Link

Document

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

Usage

From source file:org.eclipselabs.emfjson.gwt.map.EAtttributeDeserializer.java

License:Open Source License

void deSerializeValue(EObject eObject, EAttribute attribute, JSONValue value) {
    final String stringValue;
    if (value.isString() != null)
        stringValue = value.isString().stringValue();
    else if (value.isBoolean() != null)
        stringValue = Boolean.toString(value.isBoolean().booleanValue());
    else if (value.isNumber() != null)
        stringValue = value.toString();//from  w  w w .  ja va2  s.c  o m
    else
        stringValue = "";

    if (stringValue != null && !stringValue.trim().isEmpty()) {
        Object newValue;

        if (attribute.getEAttributeType().getInstanceClass().isEnum()) {
            newValue = EcoreUtil.createFromString(attribute.getEAttributeType(), stringValue.toUpperCase());
        } else {
            newValue = EcoreUtil.createFromString(attribute.getEAttributeType(), stringValue);
        }

        if (!attribute.isMany()) {
            eObject.eSet(attribute, newValue);
        } else {
            @SuppressWarnings("unchecked")
            Collection<Object> values = (Collection<Object>) eObject.eGet(attribute);
            values.add(newValue);
        }
    }
}

From source file:org.emfjson.gwt.map.Values.java

License:Open Source License

public void setOrAdd(EObject owner, EAttribute attribute, JSONValue value) {
    JSONString stringValue = value.isString();
    if (stringValue != null) {
        EObjects.setOrAdd(owner, attribute, stringValue.stringValue());
    }/*from   w w  w .  j  av  a  2  s.c om*/

    JSONBoolean booleanValue = value.isBoolean();
    if (booleanValue != null) {
        EObjects.setOrAdd(owner, attribute, booleanValue.toString());
    }

    JSONNumber numberValue = value.isNumber();
    if (numberValue != null) {
        EObjects.setOrAdd(owner, attribute, numberValue.toString());
    }
}

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  w  w w .java2 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();//w ww  .  j a  v a2s  .c  om
        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.geomajas.gwt2.client.map.feature.JsonFeatureFactory.java

License:Open Source License

private Feature createFeature(JSONObject jsonObject, FeaturesSupported layer) {
    String id = null;/*w ww .  j  a va 2  s  .  c o m*/
    // 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 boolean value from a {@link JSONObject}.
 * //from  w ww  .j  a  va2s  . co  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 Boolean getBooleanValue(JSONObject jsonObject, String key) throws JSONException {
    checkArguments(jsonObject, key);
    JSONValue value = jsonObject.get(key);
    Boolean result = null;
    if (value != null && value.isBoolean() != null) {
        return ((JSONBoolean) value).booleanValue();
    }
    return result;
}

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));
        }/*w w  w .j  a  va 2s .c om*/
        return array;
    }
    return null;
}

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

License:Apache License

public Boolean deSerialize(JSONValue jsonObj, boolean forcePrimitiveValue) throws JSONException {
    Boolean defaultValue = forcePrimitiveValue ? false : null;
    return jsonObj == null ? defaultValue
            : (jsonObj.isBoolean() == null ? defaultValue : jsonObj.isBoolean().booleanValue());
}

From source file:org.jboss.bpm.console.client.process.JSONTree.java

License:Open Source License

private void parseValue(TreeItem root, String key, JSONValue jsonValue) {
    if (jsonValue.isBoolean() != null) {
        TreeItem treeItem = root.addItem(key);
        treeItem.addItem(jsonValue.isBoolean().toString());
    } else if (jsonValue.isNumber() != null) {
        TreeItem fastTreeItem = root.addItem(key);
        fastTreeItem.addItem(jsonValue.isNumber().toString());
    } else if (jsonValue.isString() != null) {
        TreeItem treeItem = root.addItem(key);
        treeItem.addItem(jsonValue.isString().toString());
    } else {/*from  w  w  w.  j a  v a2 s  .  c o m*/
        ConsoleLog.warn("Unexpected JSON value: " + jsonValue);
    }

}

From source file:org.jboss.errai.common.client.json.JSONDecoderCli.java

License:Apache License

private static Object _decode(JSONValue v, DecodingContext ctx) {
    if (v.isString() != null) {
        return v.isString().stringValue();
    } else if (v.isNumber() != null) {
        return v.isNumber().doubleValue();
    } else if (v.isBoolean() != null) {
        return v.isBoolean().booleanValue();
    } else if (v.isNull() != null) {
        return null;
    } else if (v instanceof JSONObject) {
        return decodeObject(v.isObject(), ctx);
    } else if (v instanceof JSONArray) {
        return decodeList(v.isArray(), ctx);
    } else {//  ww  w .  j  av  a  2s.  c  om
        throw new RuntimeException("unknown encoding");
    }
}