List of usage examples for com.google.gwt.json.client JSONNumber JSONNumber
public JSONNumber(double value)
From source file:org.freemedsoftware.gwt.client.JsonUtil.java
License:Open Source License
/** * Create JSON adaptation of objects.// ww w.ja va 2 s .co m * * @param o * @return JSON formatted string */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static synchronized String jsonify(Object o) { if (o != null) { if (o instanceof HashMap && (((HashMap<String, HashMap<String, String>>) o) != null)) { try { JSONObject out = new JSONObject(); HashMap<String, HashMap<String, String>> ng = (HashMap<String, HashMap<String, String>>) o; Iterator<String> iter = ng.keySet().iterator(); while (iter.hasNext()) { String key = iter.next(); HashMap<String, String> ngInner = ng.get(key); Iterator<String> iterInner = ngInner.keySet().iterator(); JSONObject inner = new JSONObject(); while (iterInner.hasNext()) { String keyInner = iterInner.next(); inner.put(keyInner, new JSONString(ngInner.get(keyInner))); } out.put(key, inner); } return out.toString(); } catch (ClassCastException e) { e.printStackTrace(); } catch (Exception ex) { JsonUtil.debug(ex.getMessage()); } } if (o instanceof HashMap && (((HashMap<String, HashMap<String, Integer>>) o) != null)) { try { JSONObject out = new JSONObject(); HashMap<String, HashMap<String, Integer>> ng = (HashMap<String, HashMap<String, Integer>>) o; Iterator<String> iter = ng.keySet().iterator(); while (iter.hasNext()) { String key = iter.next(); HashMap<String, Integer> ngInner = ng.get(key); Iterator<String> iterInner = ngInner.keySet().iterator(); JSONObject inner = new JSONObject(); while (iterInner.hasNext()) { String keyInner = iterInner.next(); inner.put(keyInner, new JSONNumber(ngInner.get(keyInner))); } out.put(key, inner); } return out.toString(); } catch (ClassCastException e) { e.printStackTrace(); } catch (Exception ex) { JsonUtil.debug(ex.getMessage()); } } if (o instanceof HashMap && (((HashMap<String, String>) o) != null)) { try { JSONObject out = new JSONObject(); HashMap<String, String> ng = (HashMap<String, String>) o; Iterator<String> iter = ng.keySet().iterator(); while (iter.hasNext()) { String key = iter.next(); out.put(key, new JSONString(ng.get(key))); } return out.toString(); } catch (ClassCastException e) { e.printStackTrace(); } catch (Exception ex) { JsonUtil.debug(ex.getMessage()); } } if (o instanceof HashMap && (((HashMap<String, String[]>) o) != null)) { try { JSONObject out = new JSONObject(); HashMap<String, String[]> ng = (HashMap<String, String[]>) o; Iterator<String> iter = ng.keySet().iterator(); while (iter.hasNext()) { String key = iter.next(); String[] temparray = ng.get(key); JSONArray jsonArray = new JSONArray(); for (int index = 0; index < temparray.length; index++) { jsonArray.set(index, new JSONString(temparray[index])); } out.put(key, jsonArray); } return out.toString(); } catch (ClassCastException e) { e.printStackTrace(); } catch (Exception ex) { JsonUtil.debug(ex.getMessage()); } } if (o instanceof HashMap && (((HashMap<String, List>) o) != null)) { try { JSONObject out = new JSONObject(); HashMap<String, List> ng = (HashMap<String, List>) o; Iterator<String> iter = ng.keySet().iterator(); while (iter.hasNext()) { String key = iter.next(); Iterator<String> iterator = ng.get(key).iterator(); JSONArray jsonArray = new JSONArray(); for (int index = 0; iterator.hasNext(); index++) { String aa = iterator.next(); jsonArray.set(index, new JSONString(aa)); } out.put(key, jsonArray); } return out.toString(); } catch (ClassCastException e) { e.printStackTrace(); } catch (Exception ex) { JsonUtil.debug(ex.getMessage()); } } if (o instanceof HashMap[] && (((HashMap<String, String>[]) o) != null)) { JSONArray out = new JSONArray(); for (int oIter = 0; oIter < ((HashMap<String, String>[]) o).length; oIter++) { JSONObject a = new JSONObject(); HashMap<String, String> ng = ((HashMap<String, String>[]) o)[oIter]; Iterator<String> iter = ng.keySet().iterator(); while (iter.hasNext()) { String key = iter.next(); a.put(key, new JSONString(ng.get(key))); } out.set(oIter, a); } return out.toString(); } if (o instanceof Boolean) { return JSONBoolean.getInstance(((Boolean) o).booleanValue()).toString(); } if (o instanceof Long) { return new JSONNumber((Long) o).toString(); } if (o instanceof Integer) { return new JSONNumber((Integer) o).toString(); } if (o instanceof String) { return new JSONString((String) o).toString(); } if (o instanceof String[] && (((String[]) o) != null)) { JSONArray out = new JSONArray(); for (int iter = 0; iter < ((String[]) o).length; iter++) { out.set(iter, new JSONString(((String[]) o)[iter])); } return out.toString(); } if (o instanceof Integer[] && (((Integer[]) o) != null)) { JSONArray out = new JSONArray(); for (int iter = 0; iter < ((Integer[]) o).length; iter++) { out.set(iter, new JSONNumber(((Integer[]) o)[iter])); } return out.toString(); } } // All else fails, return "" return ""; }
From source file:org.freemedsoftware.gwt.client.widget.EmrPrintDialog.java
License:Open Source License
public int printToBrowser() { List<String> args = new ArrayList<String>(); JSONArray a = new JSONArray(); for (int iter = 0; iter < items.length; iter++) { a.set(iter, new JSONNumber(items[iter])); }/*from w w w .j a v a 2 s.c o m*/ args.add(a.toString()); String url = Util.getJsonRequest("org.freemedsoftware.api.ModuleInterface.PrintToBrowser", args.toArray(new String[0])); Window.open(url, "", ""); closeDialog(); return 0; }
From source file:org.fusesource.restygwt.client.ObjectEncoderDecoder.java
License:Apache License
@Override public JSONValue encode(Object value) throws org.fusesource.restygwt.client.JsonEncoderDecoder.EncodingException { if (value instanceof Number) return new JSONNumber(((Number) value).doubleValue()); else if (value instanceof Boolean) return JSONBoolean.getInstance(((Boolean) value).booleanValue()); else if (value instanceof Iterable) { JSONArray array = new JSONArray(); int ct = 0; for (Object v : (Iterable<?>) value) array.set(ct++, encode(v));/* www . ja va 2 s . com*/ return array; } else if (value instanceof Map) { JSONObject object = new JSONObject(); for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) object.put(entry.getKey().toString(), encode(entry.getValue())); return object; } else if (value == null) return JSONNull.getInstance(); else return new JSONString(value.toString()); }
From source file:org.geowe.client.local.main.tool.project.ProjectLayerStyle.java
License:Open Source License
public JSONObject getJSONObject() { JSONObject projectLayerObject = new JSONObject(); projectLayerObject.put(FILL_COLOR_NAME, new JSONString(getFillColor())); projectLayerObject.put(FILL_OPACITY_NAME, new JSONNumber(getFillOpacity())); projectLayerObject.put(STROKE_COLOR_NAME, new JSONString(getStrokeColor())); projectLayerObject.put(STROKE_WIDTH_NAME, new JSONNumber(getStrokeWidth())); return projectLayerObject; }
From source file:org.gwm.splice.client.service.json.JSONSerializer.java
License:Apache License
private JSONValue convertValue(Object val) { if (val instanceof Boolean) { return JSONBoolean.getInstance(((Boolean) val).booleanValue()); }//from w ww . j a v a 2s . com if (val instanceof Number) { return new JSONNumber(((Number) val).doubleValue()); } if (val instanceof String) { return new JSONString(val.toString()); } if (val instanceof Attributes) { JSONObject jobj = new JSONObject(); convertAttributes((Attributes) val, jobj); return jobj; } if (val instanceof Object[]) { JSONArray array = new JSONArray(); Object[] va = (Object[]) val; for (int i = 0; i < va.length; i++) { array.set(i, convertValue(va[i])); } return array; } if (val instanceof ISerializable) { Attributes attrs = ((ISerializable) val).serialize(); JSONObject jobj = new JSONObject(); convertAttributes(attrs, jobj); return jobj; } return JSONNull.getInstance(); }
From source file:org.gwt.json.serialization.client.primitives.JsonDoubleSerializer.java
License:Apache License
@Override public JSONValue serialize(Double obj, boolean forcePrimitiveValue) { JSONValue defaultValue = forcePrimitiveValue ? new JSONNumber(.0) : JSONNull.getInstance(); return obj == null ? defaultValue : new JSONNumber(obj.doubleValue()); }
From source file:org.gwt.json.serialization.client.primitives.JsonFloatSerializer.java
License:Apache License
@Override public JSONValue serialize(Float obj, boolean forcePrimitiveValue) { JSONValue defaultValue = forcePrimitiveValue ? new JSONNumber(.0) : JSONNull.getInstance(); return obj == null ? defaultValue : new JSONNumber(obj.floatValue()); }
From source file:org.gwt.json.serialization.client.primitives.JsonIntegerSerializer.java
License:Apache License
@Override public JSONValue serialize(Integer obj, boolean forcePrimitiveValue) { JSONValue defaultValue = forcePrimitiveValue ? new JSONNumber(0) : JSONNull.getInstance(); return obj == null ? defaultValue : new JSONNumber(obj.doubleValue()); }
From source file:org.gwt.json.serialization.client.primitives.JsonLongSerializer.java
License:Apache License
@Override public JSONValue serialize(Long obj, boolean forcePrimitiveValue) { JSONValue defaultValue = forcePrimitiveValue ? new JSONNumber(0) : JSONNull.getInstance(); return obj == null ? defaultValue : new JSONNumber(obj.doubleValue()); }
From source file:org.jboss.errai.enterprise.client.jaxrs.JacksonTransformer.java
License:Apache License
/** * The transformation from Errai JSON to Jackson's JSON contains the following steps: * <ul>/*from w w w . j a v a2 s . c o m*/ * <li>For all JSON objects, recursively remove the Errai specific OBJECT_ID and ENCODED_TYPE * values</li> * <li>Keep a reference to the removed OBJECT_IDs, so back-references can be resolved</li> * <li>If an array is encountered, process all its elements, then remove the Errai specific * QUALIFIED_VALUE key, by associating its actual value with the object's key directly: "list": * {"^Value": ["e1","e2"]} becomes "list": ["e1","e2"]</li> * <li>If an enum is encountered, remove the Errai specific ENUM_STRING_VALUE key, by associating * its actual value with the object's key directly: "gender": {"^EnumStringValue": "MALE"} becomes * "gender": "MALE"</li> * <li>If a number is encountered, remove the Errai specific NUMERIC_VALUE key, by associating its * actual value with the object's key directly: "id": {"^NumValue": "1"} becomes "id": "1"</li> * <li>If a date is encountered, remove the Errai specific QUALIFIED_VALUE key, by associating its * actual value with the object's key directly and turning it into a JSON number</li> * <li>If EMBEDDED_JSON is encountered, turn in into standard json</li> * </ul> * * @param val * the JSON value to transform * @param key * the key of the JSON value to transform * @param parent * the parent object of the current value * @param objectCache * a cache for removed objects, that is used to resolve backreferences * @return the modified JSON value */ private static JSONValue toJackson(JSONValue val, String key, JSONObject parent, Map<String, JSONValue> objectCache) { JSONObject obj; if ((obj = val.isObject()) != null) { JSONValue objectIdVal = obj.get(OBJECT_ID); if (objectIdVal != null) { String objectId = objectIdVal.isString().stringValue(); if (!objectId.equals("-1")) { JSONValue backRef = objectCache.get(objectId); if (backRef != null) { if (parent != null) { parent.put(key, backRef); } else { return backRef.isObject(); } } else { objectCache.put(objectId, obj); } } } JSONValue encType = obj.get(ENCODED_TYPE); obj.put(OBJECT_ID, null); obj.put(ENCODED_TYPE, null); for (String k : obj.keySet()) { JSONArray arr; if ((arr = obj.get(k).isArray()) != null) { for (int i = 0; i < arr.size(); i++) { if (arr.get(i).isObject() != null && arr.get(i).isObject().get(NUMERIC_VALUE) != null) { arr.set(i, arr.get(i).isObject().get(NUMERIC_VALUE)); } else if (arr.get(i).isObject() != null) { arr.set(i, toJackson(arr.get(i), null, null, objectCache)); } } if (k.equals(QUALIFIED_VALUE)) { if (parent != null) { parent.put(key, arr); } else { return arr; } } } else if (k.equals(ENUM_STRING_VALUE) || k.equals(NUMERIC_VALUE)) { if (parent != null) { parent.put(key, obj.get(k)); } } else if (k.equals(QUALIFIED_VALUE)) { if (parent != null) { if (encType.isString().stringValue().equals("java.util.Date")) { String dateValue = obj.get(k).isString().stringValue(); parent.put(key, new JSONNumber(Double.parseDouble(dateValue))); } else { parent.put(key, obj.get(k)); } } } else if (k.startsWith(SerializationParts.EMBEDDED_JSON)) { final JSONValue newKey = JSONParser .parseStrict((k.substring(SerializationParts.EMBEDDED_JSON.length()))); JSONValue value = obj.get(k); JSONObject tmpObject = new JSONObject(); toJackson(newKey, QUALIFIED_VALUE, tmpObject, objectCache); String embeddedKey = null; JSONValue qualVal = tmpObject.get(QUALIFIED_VALUE); if (qualVal.isString() != null) { embeddedKey = qualVal.isString().stringValue(); } else { embeddedKey = qualVal.toString(); } obj.put(embeddedKey, value); } toJackson(obj.get(k), k, obj, objectCache); } } return cleanUpEmbeddedJson(obj); }